2014-08-09 00:40:43 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
2017-02-22 04:04:52 +01:00
|
|
|
#include <linux/if_ether.h>
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
#include <ell/ell.h>
|
|
|
|
|
2017-02-22 04:04:52 +01:00
|
|
|
#include "linux/nl80211.h"
|
|
|
|
|
|
|
|
#include "src/util.h"
|
2017-03-03 12:42:25 +01:00
|
|
|
#include "src/storage.h"
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
enum {
|
|
|
|
HWSIM_CMD_UNSPEC,
|
|
|
|
HWSIM_CMD_REGISTER,
|
|
|
|
HWSIM_CMD_FRAME,
|
|
|
|
HWSIM_CMD_TX_INFO_FRAME,
|
2014-10-31 14:42:40 +01:00
|
|
|
HWSIM_CMD_NEW_RADIO,
|
|
|
|
HWSIM_CMD_DEL_RADIO,
|
2014-11-13 07:55:46 +01:00
|
|
|
HWSIM_CMD_GET_RADIO,
|
2014-08-09 00:40:43 +02:00
|
|
|
__HWSIM_CMD_MAX,
|
|
|
|
};
|
2015-04-17 17:17:50 +02:00
|
|
|
#define HWSIM_CMD_MAX (__HWSIM_CMD_MAX - 1)
|
2014-08-09 00:40:43 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
HWSIM_ATTR_UNSPEC,
|
|
|
|
HWSIM_ATTR_ADDR_RECEIVER,
|
|
|
|
HWSIM_ATTR_ADDR_TRANSMITTER,
|
|
|
|
HWSIM_ATTR_FRAME,
|
|
|
|
HWSIM_ATTR_FLAGS,
|
|
|
|
HWSIM_ATTR_RX_RATE,
|
|
|
|
HWSIM_ATTR_SIGNAL,
|
|
|
|
HWSIM_ATTR_TX_INFO,
|
|
|
|
HWSIM_ATTR_COOKIE,
|
|
|
|
HWSIM_ATTR_CHANNELS,
|
|
|
|
HWSIM_ATTR_RADIO_ID,
|
|
|
|
HWSIM_ATTR_REG_HINT_ALPHA2,
|
|
|
|
HWSIM_ATTR_REG_CUSTOM_REG,
|
|
|
|
HWSIM_ATTR_REG_STRICT_REG,
|
|
|
|
HWSIM_ATTR_SUPPORT_P2P_DEVICE,
|
|
|
|
HWSIM_ATTR_USE_CHANCTX,
|
2014-10-10 09:47:19 +02:00
|
|
|
HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE,
|
2014-11-13 07:55:46 +01:00
|
|
|
HWSIM_ATTR_RADIO_NAME,
|
|
|
|
HWSIM_ATTR_NO_VIF,
|
|
|
|
HWSIM_ATTR_FREQ,
|
2014-08-09 00:40:43 +02:00
|
|
|
__HWSIM_ATTR_MAX,
|
|
|
|
};
|
|
|
|
#define HWSIM_ATTR_MAX (__HWSIM_ATTR_MAX - 1)
|
|
|
|
|
2017-02-22 04:04:52 +01:00
|
|
|
static struct l_genl *genl;
|
2014-08-09 00:40:43 +02:00
|
|
|
static struct l_genl_family *hwsim;
|
2017-02-22 04:04:52 +01:00
|
|
|
static struct l_genl_family *nl80211;
|
2014-08-09 00:40:43 +02:00
|
|
|
|
2016-07-07 20:33:38 +02:00
|
|
|
static const char *options;
|
2016-07-09 01:19:29 +02:00
|
|
|
static int exit_status;
|
2016-07-07 20:33:38 +02:00
|
|
|
|
|
|
|
static enum action {
|
2017-02-22 04:04:52 +01:00
|
|
|
ACTION_NONE,
|
2016-07-07 20:33:38 +02:00
|
|
|
ACTION_CREATE,
|
|
|
|
ACTION_DESTROY,
|
|
|
|
ACTION_LIST,
|
|
|
|
} action;
|
2014-08-09 00:40:43 +02:00
|
|
|
|
2016-05-17 23:41:38 +02:00
|
|
|
static bool keep_radios_attr;
|
|
|
|
static bool no_vif_attr;
|
2016-07-09 01:21:48 +02:00
|
|
|
static bool p2p_attr;
|
2016-05-17 23:41:38 +02:00
|
|
|
static const char *radio_name_attr;
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
static void do_debug(const char *str, void *user_data)
|
|
|
|
{
|
|
|
|
const char *prefix = user_data;
|
|
|
|
|
|
|
|
l_info("%s%s", prefix, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void create_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint32_t radio_id = 0;
|
|
|
|
|
2014-10-08 04:36:10 +02:00
|
|
|
/*
|
|
|
|
* Note that the radio id is returned in the error field of
|
2014-10-07 14:07:15 +02:00
|
|
|
* the returned message.
|
|
|
|
*/
|
|
|
|
if (!l_genl_attr_init(&attr, msg)) {
|
|
|
|
int err = l_genl_msg_get_error(msg);
|
2016-04-20 22:30:59 +02:00
|
|
|
|
2014-10-07 14:07:15 +02:00
|
|
|
if (err < 0) {
|
|
|
|
l_warn("Failed to initialize create return attributes"
|
|
|
|
" [%d/%s]", -err, strerror(-err));
|
2016-07-09 01:19:29 +02:00
|
|
|
exit_status = EXIT_FAILURE;
|
2014-10-07 14:07:15 +02:00
|
|
|
goto done;
|
2014-08-09 00:40:43 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 14:07:15 +02:00
|
|
|
radio_id = err;
|
|
|
|
|
|
|
|
l_info("Created new radio with id %u", radio_id);
|
|
|
|
} else {
|
|
|
|
l_warn("Failed to get create return value");
|
2016-07-09 01:19:29 +02:00
|
|
|
exit_status = EXIT_FAILURE;
|
2014-10-07 14:07:15 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2014-08-09 00:40:43 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg)) {
|
2014-10-07 14:07:15 +02:00
|
|
|
int err = l_genl_msg_get_error(msg);
|
2016-04-20 22:30:59 +02:00
|
|
|
|
2014-10-07 14:07:15 +02:00
|
|
|
if (err < 0) {
|
|
|
|
l_warn("Failed to destroy radio [%d/%s]",
|
|
|
|
-err, strerror(-err));
|
2016-07-09 01:19:29 +02:00
|
|
|
exit_status = EXIT_FAILURE;
|
2014-10-07 14:07:15 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_info("Destroyed radio");
|
2015-04-17 17:17:50 +02:00
|
|
|
goto done;
|
2014-08-09 00:40:43 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 22:30:59 +02:00
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data))
|
|
|
|
;
|
2014-08-09 00:40:43 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
2014-11-13 07:55:46 +01:00
|
|
|
static void list_callback_done(void *user_data)
|
|
|
|
{
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void list_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
uint32_t idx = 0, channels = 0, custom_reg = 0;
|
|
|
|
bool reg_strict = false, p2p = false, chanctx = false;
|
|
|
|
char alpha2[2] = { };
|
|
|
|
char *hwname = NULL;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg)) {
|
|
|
|
int err = l_genl_msg_get_error(msg);
|
2016-04-20 22:30:59 +02:00
|
|
|
|
2014-11-13 07:55:46 +01:00
|
|
|
if (err < 0) {
|
|
|
|
l_warn("Failed to list radio [%d/%s]",
|
|
|
|
-err, strerror(-err));
|
2016-07-09 01:19:29 +02:00
|
|
|
exit_status = EXIT_FAILURE;
|
2014-11-13 07:55:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case HWSIM_ATTR_RADIO_ID:
|
|
|
|
if (len == 4)
|
|
|
|
idx = *(int *)data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_CHANNELS:
|
|
|
|
if (len == 4)
|
|
|
|
channels = *(uint32_t *)data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_REG_HINT_ALPHA2:
|
|
|
|
if (len == 2)
|
|
|
|
memcpy(&alpha2, data, len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_REG_CUSTOM_REG:
|
|
|
|
if (len == 4)
|
|
|
|
custom_reg = *(uint32_t *)data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_REG_STRICT_REG:
|
|
|
|
reg_strict = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_SUPPORT_P2P_DEVICE:
|
|
|
|
p2p = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_USE_CHANCTX:
|
|
|
|
chanctx = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_RADIO_NAME:
|
|
|
|
hwname = l_malloc(len + 1);
|
|
|
|
if (hwname) {
|
|
|
|
strncpy(hwname, data, len);
|
|
|
|
hwname[len] = '\0';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s radio id %d channels %d alpha2 %d %d custom reg %d "
|
|
|
|
"reg strict %d p2p %d chanctx %d\n",
|
|
|
|
hwname, idx, channels, alpha2[0], alpha2[1], custom_reg,
|
|
|
|
reg_strict, p2p, chanctx);
|
|
|
|
|
|
|
|
if (hwname)
|
|
|
|
l_free(hwname);
|
|
|
|
}
|
|
|
|
|
2017-02-22 04:04:52 +01:00
|
|
|
struct radio_info_rec {
|
|
|
|
uint32_t id;
|
2017-03-03 12:42:25 +01:00
|
|
|
uint32_t wiphy_id;
|
2017-02-22 04:04:52 +01:00
|
|
|
char alpha2[2];
|
|
|
|
bool p2p;
|
|
|
|
uint32_t regdom;
|
|
|
|
int channels;
|
2017-03-03 12:42:25 +01:00
|
|
|
uint8_t addrs[ETH_ALEN * 2];
|
|
|
|
char *name;
|
2017-02-22 04:04:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct interface_info_rec {
|
|
|
|
uint32_t id;
|
2017-03-03 12:42:25 +01:00
|
|
|
struct radio_info_rec *radio_rec;
|
2017-02-22 04:04:52 +01:00
|
|
|
uint8_t addr[ETH_ALEN];
|
2017-03-03 12:42:25 +01:00
|
|
|
char *name;
|
2017-02-22 04:04:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct l_queue *radio_info;
|
|
|
|
static struct l_queue *interface_info;
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
static void radio_free(void *user_data)
|
2017-02-22 04:04:52 +01:00
|
|
|
{
|
2017-03-03 12:42:25 +01:00
|
|
|
struct radio_info_rec *rec = user_data;
|
|
|
|
|
|
|
|
l_free(rec->name);
|
|
|
|
l_free(rec);
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
static void interface_free(void *user_data)
|
2017-02-22 04:04:52 +01:00
|
|
|
{
|
2017-03-03 12:42:25 +01:00
|
|
|
struct interface_info_rec *rec = user_data;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
l_free(rec->name);
|
|
|
|
l_free(rec);
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
static void hwsim_radio_cache_cleanup(void)
|
2017-02-22 04:04:52 +01:00
|
|
|
{
|
2017-03-03 12:42:25 +01:00
|
|
|
l_queue_destroy(radio_info, radio_free);
|
|
|
|
l_queue_destroy(interface_info, interface_free);
|
|
|
|
radio_info = NULL;
|
|
|
|
interface_info = NULL;
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
static bool radio_info_match_id(const void *a, const void *b)
|
2017-02-22 04:04:52 +01:00
|
|
|
{
|
2017-03-03 12:42:25 +01:00
|
|
|
const struct radio_info_rec *rec = a;
|
2017-02-22 04:04:52 +01:00
|
|
|
uint32_t id = L_PTR_TO_UINT(b);
|
|
|
|
|
|
|
|
return rec->id == id;
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
static bool radio_info_match_wiphy_id(const void *a, const void *b)
|
2017-02-22 04:04:52 +01:00
|
|
|
{
|
2017-03-03 12:42:25 +01:00
|
|
|
const struct radio_info_rec *rec = a;
|
|
|
|
uint32_t id = L_PTR_TO_UINT(b);
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
return rec->wiphy_id == id;
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool interface_info_match_id(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct interface_info_rec *rec = a;
|
|
|
|
uint32_t id = L_PTR_TO_UINT(b);
|
|
|
|
|
|
|
|
return rec->id == id;
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
static bool parse_addresses(const uint8_t *buf, size_t len,
|
|
|
|
struct radio_info_rec *rec)
|
2017-02-22 04:04:52 +01:00
|
|
|
{
|
2017-03-03 12:42:25 +01:00
|
|
|
unsigned int pos = 0, addr_idx = 0;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
while (pos < len) {
|
|
|
|
int start_pos = pos;
|
|
|
|
char addr[20];
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-08 13:33:18 +01:00
|
|
|
/* Find first word start and end */
|
|
|
|
while (pos < len && !l_ascii_isspace(buf[pos]))
|
2017-03-03 12:42:25 +01:00
|
|
|
pos++;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
if (pos - start_pos > sizeof(addr) - 1) {
|
|
|
|
l_error("Can't parse a %s address from sysfs",
|
|
|
|
rec->name);
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
memcpy(addr, buf + start_pos, pos - start_pos);
|
|
|
|
addr[pos - start_pos] = '\0';
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
if (addr_idx >= 2) {
|
|
|
|
l_error("Hwsim wiphy %s has too many addresses listed "
|
|
|
|
" in sysfs - only 2 supported", rec->name);
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
if (!util_string_to_address(addr, rec->addrs +
|
2017-03-08 13:33:18 +01:00
|
|
|
(addr_idx * ETH_ALEN))) {
|
2017-03-03 12:42:25 +01:00
|
|
|
l_error("Can't parse hwsim wiphy %s address from sysfs",
|
|
|
|
rec->name);
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-08 13:33:18 +01:00
|
|
|
addr_idx++;
|
|
|
|
|
|
|
|
/* Skip until the start of the next word */
|
|
|
|
while (pos < len && l_ascii_isspace(buf[pos]))
|
2017-03-03 12:42:25 +01:00
|
|
|
pos++;
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
if (addr_idx < 2) {
|
|
|
|
l_error("Hwsim wiphy %s has too few addresses listed "
|
|
|
|
" in sysfs - only 2 supported", rec->name);
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
return true;
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_radio_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
const char *name = NULL;
|
|
|
|
const uint32_t *id = NULL;
|
|
|
|
size_t name_len = 0;
|
|
|
|
struct radio_info_rec *rec;
|
2017-03-03 12:42:25 +01:00
|
|
|
uint8_t file_buffer[128];
|
|
|
|
int bytes, consumed;
|
|
|
|
unsigned int uintval;
|
|
|
|
bool old;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case HWSIM_ATTR_RADIO_ID:
|
|
|
|
if (len != 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
id = data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_RADIO_NAME:
|
|
|
|
name = data;
|
|
|
|
name_len = len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!id || !name)
|
|
|
|
return;
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
rec = l_queue_find(radio_info, radio_info_match_id, L_UINT_TO_PTR(*id));
|
|
|
|
if (rec) {
|
|
|
|
old = true;
|
|
|
|
|
|
|
|
l_free(rec->name);
|
|
|
|
} else {
|
|
|
|
old = false;
|
|
|
|
|
|
|
|
rec = l_new(struct radio_info_rec, 1);
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
rec->id = *id;
|
|
|
|
}
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
rec->name = l_strndup(name, name_len);
|
2017-02-22 04:04:52 +01:00
|
|
|
|
|
|
|
l_genl_attr_init(&attr, msg);
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case HWSIM_ATTR_CHANNELS:
|
|
|
|
if (len != 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rec->channels = *(uint32_t *) data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_REG_HINT_ALPHA2:
|
|
|
|
if (len != 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
memcpy(rec->alpha2, data, 2);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_SUPPORT_P2P_DEVICE:
|
|
|
|
rec->p2p = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HWSIM_ATTR_REG_CUSTOM_REG:
|
|
|
|
if (len != 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rec->regdom = *(uint32_t *) data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
/*
|
|
|
|
* Assuming that the radio name is the wiphy name read the wiphy index
|
|
|
|
* associated with the radio and the wiphy's hardware addresses from
|
|
|
|
* sysfs. The index could be obtained through NL80211_CMD_GET_WIPHY
|
|
|
|
* but that is costly and reading the index synchronously simplifies
|
|
|
|
* the job a lot. We have to resort to sysfs anyway to obtain the
|
|
|
|
* radio addresses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bytes = read_file((char *) file_buffer, sizeof(file_buffer) - 1,
|
|
|
|
"/sys/class/ieee80211/%s/index", rec->name);
|
|
|
|
if (bytes < 0) {
|
|
|
|
l_error("Error reading index for %s from sysfs", rec->name);
|
|
|
|
goto free_radio;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_buffer[bytes] = '\0';
|
|
|
|
if (sscanf((char *) file_buffer, "%u %n", &uintval, &consumed) != 1 ||
|
|
|
|
consumed != bytes) {
|
|
|
|
l_error("Error parsing index for %s from sysfs", rec->name);
|
|
|
|
goto free_radio;
|
|
|
|
}
|
|
|
|
|
|
|
|
rec->wiphy_id = uintval;
|
|
|
|
|
|
|
|
bytes = read_file(file_buffer, sizeof(file_buffer),
|
|
|
|
"/sys/class/ieee80211/%s/addresses", rec->name);
|
|
|
|
if (bytes < 0) {
|
|
|
|
l_error("Error reading addresses for %s from sysfs", rec->name);
|
|
|
|
goto free_radio;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parse_addresses(file_buffer, bytes, rec))
|
|
|
|
goto free_radio;
|
|
|
|
|
2017-02-22 04:04:52 +01:00
|
|
|
if (!radio_info)
|
|
|
|
radio_info = l_queue_new();
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
if (!old)
|
|
|
|
l_queue_push_tail(radio_info, rec);
|
|
|
|
|
|
|
|
/* TODO: Create DBus object */
|
|
|
|
|
|
|
|
return;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
free_radio:
|
|
|
|
if (!old)
|
|
|
|
radio_free(rec);
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_wiphy_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
const char *name = NULL;
|
|
|
|
uint16_t name_len = 0;
|
|
|
|
const uint32_t *id = NULL;
|
2017-03-03 12:42:25 +01:00
|
|
|
struct radio_info_rec *rec;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_WIPHY:
|
|
|
|
id = data;
|
|
|
|
break;
|
|
|
|
case NL80211_ATTR_WIPHY_NAME:
|
|
|
|
name = data;
|
|
|
|
name_len = len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name || !id)
|
|
|
|
return;
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
rec = l_queue_find(radio_info, radio_info_match_wiphy_id,
|
|
|
|
L_UINT_TO_PTR(*id));
|
|
|
|
if (!rec)
|
|
|
|
return;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
if (strlen(rec->name) == name_len && !memcmp(rec->name, name, name_len))
|
|
|
|
return;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
l_free(rec->name);
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
rec->name = l_strndup(name, name_len);
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_interface_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
const uint8_t *addr = NULL;
|
|
|
|
const uint32_t *wiphy_id = NULL;
|
|
|
|
const uint32_t *ifindex = NULL;
|
|
|
|
const char *ifname = NULL;
|
|
|
|
size_t ifname_len = 0;
|
|
|
|
struct interface_info_rec *rec;
|
2017-03-03 12:42:25 +01:00
|
|
|
struct radio_info_rec *radio_rec;
|
|
|
|
bool old;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_MAC:
|
|
|
|
if (len != ETH_ALEN)
|
|
|
|
break;
|
|
|
|
|
|
|
|
addr = data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_WIPHY:
|
|
|
|
if (len != 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
wiphy_id = data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_IFINDEX:
|
|
|
|
if (len != 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ifindex = data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NL80211_ATTR_IFNAME:
|
|
|
|
ifname = data;
|
|
|
|
ifname_len = len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!addr || !wiphy_id || !ifindex || !ifname)
|
|
|
|
return;
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
radio_rec = l_queue_find(radio_info, radio_info_match_wiphy_id,
|
|
|
|
L_UINT_TO_PTR(*wiphy_id));
|
|
|
|
if (!radio_rec)
|
|
|
|
/* This is not a hwsim interface, don't track it */
|
|
|
|
return;
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
rec = l_queue_find(interface_info, interface_info_match_id,
|
|
|
|
L_UINT_TO_PTR(*ifindex));
|
|
|
|
if (rec) {
|
|
|
|
old = true;
|
|
|
|
|
|
|
|
l_free(rec->name);
|
|
|
|
} else {
|
|
|
|
old = false;
|
|
|
|
|
|
|
|
rec = l_new(struct interface_info_rec, 1);
|
|
|
|
|
|
|
|
rec->id = *ifindex;
|
|
|
|
rec->radio_rec = radio_rec;
|
|
|
|
}
|
2017-02-22 04:04:52 +01:00
|
|
|
|
|
|
|
memcpy(rec->addr, addr, ETH_ALEN);
|
2017-03-03 12:42:25 +01:00
|
|
|
rec->name = l_strndup(ifname, ifname_len);
|
2017-02-22 04:04:52 +01:00
|
|
|
|
|
|
|
if (!interface_info)
|
|
|
|
interface_info = l_queue_new();
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
if (!old)
|
|
|
|
l_queue_push_tail(interface_info, rec);
|
|
|
|
|
|
|
|
/* TODO: Create DBus object */
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool interface_info_destroy_by_radio(void *data, void *user_data)
|
|
|
|
{
|
|
|
|
struct interface_info_rec *rec = data;
|
|
|
|
struct radio_info_rec *radio_rec = user_data;
|
|
|
|
|
|
|
|
if (rec->radio_rec != radio_rec)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* TODO: Drop DBus object */
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
interface_free(rec);
|
|
|
|
|
|
|
|
return true;
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void del_radio_event(struct l_genl_msg *msg)
|
|
|
|
{
|
|
|
|
struct radio_info_rec *radio;
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
const uint32_t *id = NULL;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case HWSIM_ATTR_RADIO_ID:
|
|
|
|
if (len != 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
id = data;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
radio = l_queue_find(radio_info, radio_info_match_id,
|
|
|
|
L_UINT_TO_PTR(*id));
|
|
|
|
if (!radio)
|
|
|
|
return;
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
l_queue_foreach_remove(interface_info, interface_info_destroy_by_radio,
|
|
|
|
radio);
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
/* TODO: Drop DBus object */
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
radio_free(radio);
|
|
|
|
l_queue_remove(radio_info, radio);
|
2017-02-22 04:04:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void del_interface_event(struct l_genl_msg *msg)
|
|
|
|
{
|
|
|
|
struct interface_info_rec *interface;
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
const uint32_t *ifindex = NULL;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
switch (type) {
|
|
|
|
case NL80211_ATTR_IFINDEX:
|
|
|
|
if (len != 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ifindex = data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ifindex)
|
|
|
|
return;
|
|
|
|
|
|
|
|
interface = l_queue_find(interface_info, interface_info_match_id,
|
|
|
|
L_UINT_TO_PTR(*ifindex));
|
|
|
|
if (!interface)
|
|
|
|
return;
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
/* TODO: Drop DBus object */
|
2017-02-22 04:04:52 +01:00
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
interface_free(interface);
|
2017-02-22 04:04:52 +01:00
|
|
|
l_queue_remove(interface_info, interface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hwsim_config(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
uint8_t cmd;
|
|
|
|
|
|
|
|
cmd = l_genl_msg_get_command(msg);
|
|
|
|
|
|
|
|
l_debug("Config changed cmd %u", cmd);
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data))
|
|
|
|
l_debug("\tattr type %d len %d", type, len);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case HWSIM_CMD_NEW_RADIO:
|
|
|
|
get_radio_callback(msg, NULL);
|
|
|
|
break;
|
|
|
|
case HWSIM_CMD_DEL_RADIO:
|
|
|
|
del_radio_event(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nl80211_config_notify(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
uint8_t cmd;
|
|
|
|
|
|
|
|
cmd = l_genl_msg_get_command(msg);
|
|
|
|
|
|
|
|
l_debug("Notification of command %u", cmd);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case NL80211_CMD_NEW_WIPHY:
|
|
|
|
get_wiphy_callback(msg, NULL);
|
|
|
|
break;
|
|
|
|
case NL80211_CMD_NEW_INTERFACE:
|
|
|
|
get_interface_callback(msg, NULL);
|
|
|
|
break;
|
|
|
|
case NL80211_CMD_DEL_INTERFACE:
|
|
|
|
del_interface_event(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
static void get_radio_done_initial(void *user_data)
|
2017-02-22 04:04:52 +01:00
|
|
|
{
|
|
|
|
struct l_genl_msg *msg;
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
/*
|
|
|
|
* Query interfaces now that we know we have all the radio data
|
|
|
|
* for radio lookups inside get_interface_callback, and we know
|
|
|
|
* nl80211_ready has already been called.
|
|
|
|
*/
|
2017-02-22 04:04:52 +01:00
|
|
|
msg = l_genl_msg_new(NL80211_CMD_GET_INTERFACE);
|
|
|
|
if (!l_genl_family_dump(nl80211, msg, get_interface_callback,
|
|
|
|
NULL, NULL)) {
|
|
|
|
l_error("Getting nl80211 interface information failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!l_genl_family_register(nl80211, "config", nl80211_config_notify,
|
|
|
|
NULL, NULL)) {
|
|
|
|
l_error("Registering for nl80211 config notification "
|
|
|
|
"failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:25 +01:00
|
|
|
static void nl80211_ready(void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_msg *msg;
|
|
|
|
|
|
|
|
msg = l_genl_msg_new(HWSIM_CMD_GET_RADIO);
|
|
|
|
if (!l_genl_family_dump(hwsim, msg, get_radio_callback,
|
|
|
|
NULL, get_radio_done_initial)) {
|
|
|
|
l_error("Getting hwsim radio information failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
static void hwsim_ready(void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_msg *msg;
|
2014-10-31 14:42:40 +01:00
|
|
|
int ret;
|
2016-07-07 20:33:38 +02:00
|
|
|
size_t msg_size;
|
|
|
|
uint32_t radio_id;
|
2014-10-31 14:42:40 +01:00
|
|
|
|
|
|
|
ret = l_genl_family_register(hwsim, "config", hwsim_config,
|
2014-11-02 03:27:51 +01:00
|
|
|
NULL, NULL);
|
2017-02-22 04:04:50 +01:00
|
|
|
if (!ret) {
|
2014-10-31 14:42:40 +01:00
|
|
|
fprintf(stderr, "Failed to create hwsim config listener\n");
|
2016-07-09 01:19:29 +02:00
|
|
|
exit_status = EXIT_FAILURE;
|
2014-10-31 14:42:40 +01:00
|
|
|
l_main_quit();
|
|
|
|
return;
|
|
|
|
}
|
2014-08-09 00:40:43 +02:00
|
|
|
|
2016-07-07 20:33:38 +02:00
|
|
|
switch (action) {
|
|
|
|
case ACTION_LIST:
|
|
|
|
msg = l_genl_msg_new_sized(HWSIM_CMD_GET_RADIO,
|
|
|
|
options ? 8 : 4);
|
|
|
|
|
|
|
|
if (options) {
|
|
|
|
radio_id = atoi(options);
|
|
|
|
|
|
|
|
l_genl_msg_append_attr(msg, HWSIM_ATTR_RADIO_ID,
|
|
|
|
4, &radio_id);
|
|
|
|
l_genl_family_send(hwsim, msg, list_callback,
|
|
|
|
NULL, list_callback_done);
|
|
|
|
} else {
|
|
|
|
l_genl_family_dump(hwsim, msg, list_callback,
|
|
|
|
NULL, list_callback_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_CREATE:
|
|
|
|
msg_size = 0;
|
2016-05-17 23:41:38 +02:00
|
|
|
|
|
|
|
if (!keep_radios_attr)
|
|
|
|
msg_size += 4;
|
|
|
|
|
|
|
|
if (radio_name_attr)
|
2017-02-22 04:04:51 +01:00
|
|
|
msg_size += strlen(radio_name_attr) + 1;
|
2016-05-17 23:41:38 +02:00
|
|
|
|
|
|
|
if (no_vif_attr)
|
|
|
|
msg_size += 4;
|
|
|
|
|
2016-07-09 01:21:48 +02:00
|
|
|
if (p2p_attr)
|
2016-07-07 19:25:56 +02:00
|
|
|
msg_size += 4;
|
|
|
|
|
2016-05-17 23:41:38 +02:00
|
|
|
msg = l_genl_msg_new_sized(HWSIM_CMD_NEW_RADIO, msg_size);
|
2014-10-13 19:56:27 +02:00
|
|
|
|
2016-05-17 23:41:38 +02:00
|
|
|
if (!keep_radios_attr)
|
2014-10-10 09:47:19 +02:00
|
|
|
l_genl_msg_append_attr(msg,
|
|
|
|
HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE,
|
|
|
|
0, NULL);
|
|
|
|
|
2016-05-17 23:41:38 +02:00
|
|
|
if (radio_name_attr)
|
|
|
|
l_genl_msg_append_attr(msg, HWSIM_ATTR_RADIO_NAME,
|
2017-03-03 12:42:24 +01:00
|
|
|
strlen(radio_name_attr) + 1,
|
2016-05-17 23:41:38 +02:00
|
|
|
radio_name_attr);
|
|
|
|
|
|
|
|
if (no_vif_attr)
|
|
|
|
l_genl_msg_append_attr(msg, HWSIM_ATTR_NO_VIF, 0, NULL);
|
|
|
|
|
2016-07-09 01:21:48 +02:00
|
|
|
if (p2p_attr)
|
2016-07-07 19:25:56 +02:00
|
|
|
l_genl_msg_append_attr(msg,
|
|
|
|
HWSIM_ATTR_SUPPORT_P2P_DEVICE,
|
|
|
|
0, NULL);
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
l_genl_family_send(hwsim, msg, create_callback, NULL, NULL);
|
2016-07-07 20:33:38 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_DESTROY:
|
|
|
|
radio_id = atoi(options);
|
2014-08-09 00:40:43 +02:00
|
|
|
|
2014-10-31 14:42:40 +01:00
|
|
|
msg = l_genl_msg_new_sized(HWSIM_CMD_DEL_RADIO, 8);
|
2016-07-07 20:33:38 +02:00
|
|
|
l_genl_msg_append_attr(msg, HWSIM_ATTR_RADIO_ID, 4, &radio_id);
|
2014-08-09 00:40:43 +02:00
|
|
|
l_genl_family_send(hwsim, msg, destroy_callback, NULL, NULL);
|
2014-11-15 05:17:30 +01:00
|
|
|
|
2016-07-07 20:33:38 +02:00
|
|
|
break;
|
2016-04-20 22:30:59 +02:00
|
|
|
|
2017-02-22 04:04:52 +01:00
|
|
|
case ACTION_NONE:
|
|
|
|
l_genl_family_set_watches(nl80211, nl80211_ready, NULL,
|
|
|
|
NULL, NULL);
|
2016-07-07 20:33:38 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2017-02-22 04:04:52 +01:00
|
|
|
|
|
|
|
return;
|
2014-08-09 00:40:43 +02:00
|
|
|
}
|
|
|
|
|
2014-10-27 14:01:20 +01:00
|
|
|
static void hwsim_disappeared(void *user_data)
|
|
|
|
{
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
static void signal_handler(struct l_signal *signal, uint32_t signo,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
switch (signo) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
l_main_quit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf("hwsim - Wireless simulator\n"
|
|
|
|
"Usage:\n");
|
|
|
|
printf("\thwsim [options]\n");
|
|
|
|
printf("Options:\n"
|
2014-11-13 07:55:46 +01:00
|
|
|
"\t-L, --list [id] List simulated radios\n"
|
2014-08-09 00:40:43 +02:00
|
|
|
"\t-C, --create Create new simulated radio\n"
|
|
|
|
"\t-D, --destroy <id> Destroy existing radio\n"
|
2014-10-10 09:47:19 +02:00
|
|
|
"\t-k, --keep Do not destroy radios when "
|
2016-05-17 23:41:38 +02:00
|
|
|
"program exits\n"
|
|
|
|
"\t-n, --name <name> Name of a radio to be created\n"
|
|
|
|
"\t-i, --nointerface Do not create VIF\n"
|
2016-07-07 19:25:56 +02:00
|
|
|
"\t-p, --p2p Support P2P\n"
|
2014-08-09 00:40:43 +02:00
|
|
|
"\t-h, --help Show help options\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct option main_options[] = {
|
2016-05-17 23:41:38 +02:00
|
|
|
{ "list", optional_argument, NULL, 'L' },
|
|
|
|
{ "create", no_argument, NULL, 'C' },
|
|
|
|
{ "destroy", required_argument, NULL, 'D' },
|
|
|
|
{ "keep", no_argument, NULL, 'k' },
|
|
|
|
{ "name", required_argument, NULL, 'n' },
|
|
|
|
{ "nointerface", no_argument, NULL, 'i' },
|
2016-07-07 19:25:56 +02:00
|
|
|
{ "p2p", no_argument, NULL, 'p' },
|
2016-05-17 23:41:38 +02:00
|
|
|
{ "version", no_argument, NULL, 'v' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2014-08-09 00:40:43 +02:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct l_signal *signal;
|
|
|
|
sigset_t mask;
|
2016-07-09 01:19:29 +02:00
|
|
|
int actions = 0;
|
2014-08-09 00:40:43 +02:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int opt;
|
|
|
|
|
2016-07-07 19:25:56 +02:00
|
|
|
opt = getopt_long(argc, argv, ":L:CD:kn:ipv", main_options,
|
2016-05-17 23:41:38 +02:00
|
|
|
NULL);
|
2014-08-09 00:40:43 +02:00
|
|
|
if (opt < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (opt) {
|
2014-11-13 07:55:46 +01:00
|
|
|
case ':':
|
|
|
|
if (optopt == 'L') {
|
2016-07-07 20:33:38 +02:00
|
|
|
action = ACTION_LIST;
|
2014-11-13 07:55:46 +01:00
|
|
|
actions++;
|
|
|
|
} else {
|
|
|
|
printf("option '-%c' requires an argument\n",
|
|
|
|
optopt);
|
2016-07-09 01:19:29 +02:00
|
|
|
return EXIT_FAILURE;
|
2014-11-13 07:55:46 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'L':
|
2016-07-07 20:33:38 +02:00
|
|
|
action = ACTION_LIST;
|
|
|
|
options = optarg;
|
2014-11-13 07:55:46 +01:00
|
|
|
actions++;
|
|
|
|
break;
|
2014-08-09 00:40:43 +02:00
|
|
|
case 'C':
|
2016-07-07 20:33:38 +02:00
|
|
|
action = ACTION_CREATE;
|
2014-11-13 07:55:46 +01:00
|
|
|
actions++;
|
2014-08-09 00:40:43 +02:00
|
|
|
break;
|
|
|
|
case 'D':
|
2016-07-07 20:33:38 +02:00
|
|
|
action = ACTION_DESTROY;
|
|
|
|
options = optarg;
|
2014-11-13 07:55:46 +01:00
|
|
|
actions++;
|
2014-08-09 00:40:43 +02:00
|
|
|
break;
|
2014-10-10 09:47:19 +02:00
|
|
|
case 'k':
|
2016-05-17 23:41:38 +02:00
|
|
|
keep_radios_attr = true;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
radio_name_attr = optarg;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
no_vif_attr = true;
|
2014-10-10 09:47:19 +02:00
|
|
|
break;
|
2016-07-07 19:25:56 +02:00
|
|
|
case 'p':
|
2016-07-09 01:21:48 +02:00
|
|
|
p2p_attr = true;
|
2016-07-07 19:25:56 +02:00
|
|
|
break;
|
2014-08-09 00:40:43 +02:00
|
|
|
case 'v':
|
|
|
|
printf("%s\n", VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
default:
|
2014-11-13 07:55:46 +01:00
|
|
|
printf("unrecognized argument '%s'\n",
|
|
|
|
argv[optind - 1]);
|
2014-08-09 00:40:43 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind > 0) {
|
|
|
|
fprintf(stderr, "Invalid command line parameters\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-11-13 07:55:46 +01:00
|
|
|
if (actions > 1) {
|
2014-08-09 00:40:43 +02:00
|
|
|
fprintf(stderr, "Only one action can be specified\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-06-09 00:41:38 +02:00
|
|
|
if (!l_main_init())
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
|
|
|
|
|
|
|
signal = l_signal_create(&mask, signal_handler, NULL, NULL);
|
|
|
|
|
|
|
|
l_log_set_stderr();
|
|
|
|
|
|
|
|
printf("Wireless simulator ver %s\n", VERSION);
|
|
|
|
|
|
|
|
genl = l_genl_new_default();
|
|
|
|
if (!genl) {
|
|
|
|
fprintf(stderr, "Failed to initialize generic netlink\n");
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getenv("HWSIM_DEBUG"))
|
|
|
|
l_genl_set_debug(genl, do_debug, "[GENL] ", NULL);
|
|
|
|
|
|
|
|
hwsim = l_genl_family_new(genl, "MAC80211_HWSIM");
|
|
|
|
if (!hwsim) {
|
|
|
|
fprintf(stderr, "Failed to create generic netlink family\n");
|
|
|
|
l_genl_unref(genl);
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2017-02-22 04:04:52 +01:00
|
|
|
nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME);
|
|
|
|
if (!nl80211) {
|
|
|
|
fprintf(stderr, "Failed to create nl80211 genl family\n");
|
|
|
|
l_genl_family_unref(hwsim);
|
|
|
|
l_genl_unref(genl);
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:01:20 +01:00
|
|
|
l_genl_family_set_watches(hwsim, hwsim_ready, hwsim_disappeared,
|
|
|
|
NULL, NULL);
|
2014-08-09 00:40:43 +02:00
|
|
|
|
2017-02-22 04:04:52 +01:00
|
|
|
exit_status = EXIT_SUCCESS;
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
l_main_run();
|
|
|
|
|
|
|
|
l_genl_family_unref(hwsim);
|
2017-02-22 04:04:52 +01:00
|
|
|
l_genl_family_unref(nl80211);
|
2014-08-09 00:40:43 +02:00
|
|
|
l_genl_unref(genl);
|
|
|
|
|
2017-02-22 04:04:52 +01:00
|
|
|
hwsim_radio_cache_cleanup();
|
2014-08-09 00:40:43 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
l_signal_remove(signal);
|
|
|
|
|
2016-06-09 00:41:38 +02:00
|
|
|
l_main_exit();
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
return exit_status;
|
|
|
|
}
|