eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

hwsim.c (78462B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2013-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <getopt.h>
     30 #include <signal.h>
     31 #include <time.h>
     32 #include <sys/time.h>
     33 #include <linux/if_ether.h>
     34 #include <linux/rtnetlink.h>
     35 #include <net/if_arp.h>
     36 
     37 #include <ell/ell.h>
     38 #include "ell/useful.h"
     39 
     40 #include "linux/nl80211.h"
     41 
     42 #include "src/util.h"
     43 #include "src/storage.h"
     44 #include "src/mpdu.h"
     45 #include "src/crypto.h"
     46 #include "src/nl80211util.h"
     47 #include "src/nl80211cmd.h"
     48 
     49 #define HWSIM_SERVICE "net.connman.hwsim"
     50 
     51 #define HWSIM_RADIO_MANAGER_INTERFACE HWSIM_SERVICE ".RadioManager"
     52 #define HWSIM_RADIO_INTERFACE HWSIM_SERVICE ".Radio"
     53 #define HWSIM_INTERFACE_INTERFACE HWSIM_SERVICE ".Interface"
     54 #define HWSIM_RULE_MANAGER_INTERFACE HWSIM_SERVICE ".RuleManager"
     55 #define HWSIM_RULE_INTERFACE HWSIM_SERVICE ".Rule"
     56 
     57 enum {
     58 	HWSIM_CMD_UNSPEC,
     59 	HWSIM_CMD_REGISTER,
     60 	HWSIM_CMD_FRAME,
     61 	HWSIM_CMD_TX_INFO_FRAME,
     62 	HWSIM_CMD_NEW_RADIO,
     63 	HWSIM_CMD_DEL_RADIO,
     64 	HWSIM_CMD_GET_RADIO,
     65 	HWSIM_CMD_ADD_MAC_ADDR,
     66 	HWSIM_CMD_DEL_MAC_ADDR,
     67 	__HWSIM_CMD_MAX,
     68 };
     69 #define HWSIM_CMD_MAX (__HWSIM_CMD_MAX - 1)
     70 
     71 enum {
     72 	HWSIM_ATTR_UNSPEC,
     73 	HWSIM_ATTR_ADDR_RECEIVER,
     74 	HWSIM_ATTR_ADDR_TRANSMITTER,
     75 	HWSIM_ATTR_FRAME,
     76 	HWSIM_ATTR_FLAGS,
     77 	HWSIM_ATTR_RX_RATE,
     78 	HWSIM_ATTR_SIGNAL,
     79 	HWSIM_ATTR_TX_INFO,
     80 	HWSIM_ATTR_COOKIE,
     81 	HWSIM_ATTR_CHANNELS,
     82 	HWSIM_ATTR_RADIO_ID,
     83 	HWSIM_ATTR_REG_HINT_ALPHA2,
     84 	HWSIM_ATTR_REG_CUSTOM_REG,
     85 	HWSIM_ATTR_REG_STRICT_REG,
     86 	HWSIM_ATTR_SUPPORT_P2P_DEVICE,
     87 	HWSIM_ATTR_USE_CHANCTX,
     88 	HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE,
     89 	HWSIM_ATTR_RADIO_NAME,
     90 	HWSIM_ATTR_NO_VIF,
     91 	HWSIM_ATTR_FREQ,
     92 	HWSIM_ATTR_PAD,
     93 	HWSIM_ATTR_TX_INFO_FLAGS,
     94 	HWSIM_ATTR_PERM_ADDR,
     95 	HWSIM_ATTR_IFTYPE_SUPPORT,
     96 	HWSIM_ATTR_CIPHER_SUPPORT,
     97 	__HWSIM_ATTR_MAX,
     98 };
     99 #define HWSIM_ATTR_MAX (__HWSIM_ATTR_MAX - 1)
    100 
    101 /*
    102  * Should be kept in sync with HWSIM_IFTYPE_SUPPORT_MASK in mac80211_hwsim
    103  */
    104 #define HWSIM_DEFAULT_IFTYPES \
    105 	( \
    106 		(1 << NL80211_IFTYPE_STATION) | \
    107 		(1 << NL80211_IFTYPE_AP) | \
    108 		(1 << NL80211_IFTYPE_P2P_CLIENT) | \
    109 		(1 << NL80211_IFTYPE_P2P_GO) | \
    110 		(1 << NL80211_IFTYPE_ADHOC) | \
    111 		(1 << NL80211_IFTYPE_MESH_POINT) \
    112 	)
    113 
    114 enum hwsim_tx_control_flags {
    115 	HWSIM_TX_CTL_REQ_TX_STATUS		= 1 << 0,
    116 	HWSIM_TX_CTL_NO_ACK			= 1 << 1,
    117 	HWSIM_TX_STAT_ACK			= 1 << 2,
    118 };
    119 
    120 #define IEEE80211_TX_RATE_TABLE_SIZE	4
    121 #define HWSIM_DELAY_MIN_MS		1
    122 #define HWSIM_MAX_PREFIX_LEN		128
    123 
    124 struct hwsim_rule {
    125 	unsigned int id;
    126 	uint8_t source[ETH_ALEN];
    127 	uint8_t destination[ETH_ALEN];
    128 	bool source_any : 1;
    129 	bool destination_any : 1;
    130 	bool bidirectional : 1;
    131 	bool drop : 1;
    132 	bool drop_ack : 1;
    133 	bool enabled : 1;
    134 	uint32_t frequency;
    135 	int priority;
    136 	int signal;
    137 	int delay;
    138 	uint8_t *prefix;
    139 	size_t prefix_len;
    140 	uint8_t *match;
    141 	size_t match_len;
    142 	uint16_t match_offset;
    143 	int match_times; /* negative value indicates unused */
    144 };
    145 
    146 struct hwsim_support {
    147 	const char *name;
    148 	uint32_t value;
    149 };
    150 
    151 static struct l_genl *genl;
    152 static struct l_genl_family *hwsim;
    153 static struct l_genl_family *nl80211;
    154 static struct l_netlink *rtnl;
    155 
    156 static const char *options;
    157 static int exit_status;
    158 
    159 static enum action {
    160 	ACTION_NONE,
    161 	ACTION_CREATE,
    162 	ACTION_DESTROY,
    163 	ACTION_LIST,
    164 } action;
    165 
    166 static bool no_vif_attr;
    167 static bool p2p_attr;
    168 static bool no_register = false;
    169 static const char *radio_name_attr;
    170 static struct l_dbus *dbus;
    171 static struct l_queue *rules;
    172 static unsigned int next_rule_id;
    173 
    174 static uint32_t hwsim_iftypes = HWSIM_DEFAULT_IFTYPES;
    175 static const uint32_t hwsim_supported_ciphers[] = {
    176 	CRYPTO_CIPHER_WEP40,
    177 	CRYPTO_CIPHER_WEP104,
    178 	CRYPTO_CIPHER_TKIP,
    179 	CRYPTO_CIPHER_CCMP,
    180 	CRYPTO_CIPHER_BIP_CMAC,
    181 	CRYPTO_CIPHER_GCMP,
    182 	CRYPTO_CIPHER_GCMP_256,
    183 	CRYPTO_CIPHER_CCMP_256,
    184 	CRYPTO_CIPHER_BIP_GMAC,
    185 	CRYPTO_CIPHER_BIP_GMAC_256,
    186 	CRYPTO_CIPHER_BIP_CMAC_256,
    187 };
    188 static uint32_t hwsim_ciphers[L_ARRAY_SIZE(hwsim_supported_ciphers)];
    189 static int hwsim_num_ciphers = 0;
    190 
    191 /* list of disableable iftypes */
    192 static const struct hwsim_support iftype_map[] = {
    193 	{ "station", 1 << NL80211_IFTYPE_STATION },
    194 	{ "ap", 1 << NL80211_IFTYPE_AP },
    195 	{ "adhoc", 1 << NL80211_IFTYPE_ADHOC },
    196 	{ "p2p_client", 1 << NL80211_IFTYPE_P2P_CLIENT },
    197 	{ "p2p_go", 1 << NL80211_IFTYPE_P2P_GO },
    198 	{ "mesh_point", 1 << NL80211_IFTYPE_MESH_POINT },
    199 	{ }
    200 };
    201 
    202 static const struct hwsim_support cipher_map[] = {
    203 	{ "wep40", CRYPTO_CIPHER_WEP40 },
    204 	{ "wep104", CRYPTO_CIPHER_WEP104 },
    205 	{ "tkip", CRYPTO_CIPHER_TKIP },
    206 	{ "ccmp", CRYPTO_CIPHER_CCMP },
    207 	{ "bip_cmac", CRYPTO_CIPHER_BIP_CMAC },
    208 	{ "gcmp", CRYPTO_CIPHER_GCMP },
    209 	{ "gcmp_256", CRYPTO_CIPHER_GCMP_256 },
    210 	{ "ccmp_256", CRYPTO_CIPHER_CCMP_256 },
    211 	{ "bip_gmac", CRYPTO_CIPHER_BIP_GMAC },
    212 	{ "bip_gmac_256", CRYPTO_CIPHER_BIP_GMAC_256 },
    213 	{ "bip_cmac_256", CRYPTO_CIPHER_BIP_CMAC_256 },
    214 	{ }
    215 };
    216 
    217 static void do_debug(const char *str, void *user_data)
    218 {
    219 	const char *prefix = user_data;
    220 
    221 	l_info("%s%s", prefix, str);
    222 }
    223 
    224 static void hwsim_disable_support(const char *disable,
    225 		const struct hwsim_support *map, uint32_t *mask)
    226 {
    227 	char **list = l_strsplit(disable, ',');
    228 	char **iter = list;
    229 	int i;
    230 
    231 	while (*iter) {
    232 		for (i = 0; map[i].name; i++) {
    233 			if (!strcmp(map[i].name, *iter))
    234 				*mask &= ~(map[i].value);
    235 		}
    236 
    237 		iter++;
    238 	}
    239 
    240 	l_strfreev(list);
    241 }
    242 
    243 static bool is_cipher_disabled(const char *args, enum crypto_cipher cipher)
    244 {
    245 	char **list = l_strsplit(args, ',');
    246 	char **iter = list;
    247 	int i;
    248 
    249 	while (*iter) {
    250 		for (i = 0; cipher_map[i].name; i++) {
    251 			if (!strcmp(*iter, cipher_map[i].name) &&
    252 					cipher == cipher_map[i].value) {
    253 				printf("disable cipher: %s\n", cipher_map[i].name);
    254 				l_strfreev(list);
    255 				return true;
    256 			}
    257 		}
    258 
    259 		iter++;
    260 	}
    261 
    262 	l_strfreev(list);
    263 
    264 	return false;
    265 }
    266 
    267 static void hwsim_disable_ciphers(const char *disable)
    268 {
    269 	uint8_t i;
    270 
    271 	for (i = 0; i < L_ARRAY_SIZE(hwsim_supported_ciphers); i++) {
    272 		if (is_cipher_disabled(disable, hwsim_supported_ciphers[i]))
    273 			continue;
    274 
    275 		hwsim_ciphers[hwsim_num_ciphers] = hwsim_supported_ciphers[i];
    276 		hwsim_num_ciphers++;
    277 	}
    278 }
    279 
    280 static void create_callback(struct l_genl_msg *msg, void *user_data)
    281 {
    282 	struct l_genl_attr attr;
    283 	uint32_t radio_id = 0;
    284 
    285 	/*
    286 	 * Note that the radio id is returned in the error field of
    287 	 * the returned message.
    288 	 */
    289 	if (!l_genl_attr_init(&attr, msg)) {
    290 		int err = l_genl_msg_get_error(msg);
    291 
    292 		if (err < 0) {
    293 			l_warn("Failed to initialize create return attributes"
    294 				" [%d/%s]", -err, strerror(-err));
    295 			exit_status = EXIT_FAILURE;
    296 			goto done;
    297 		}
    298 
    299 		radio_id = err;
    300 
    301 		l_info("Created new radio with id %u", radio_id);
    302 	} else {
    303 		l_warn("Failed to get create return value");
    304 		exit_status = EXIT_FAILURE;
    305 		goto done;
    306 	}
    307 
    308 done:
    309 	l_main_quit();
    310 }
    311 
    312 static void destroy_callback(struct l_genl_msg *msg, void *user_data)
    313 {
    314 	struct l_genl_attr attr;
    315 	uint16_t type, len;
    316 	const void *data;
    317 
    318 	if (!l_genl_attr_init(&attr, msg)) {
    319 		int err = l_genl_msg_get_error(msg);
    320 
    321 		if (err < 0) {
    322 			l_warn("Failed to destroy radio [%d/%s]",
    323 				-err, strerror(-err));
    324 			exit_status = EXIT_FAILURE;
    325 			goto done;
    326 		}
    327 
    328 		l_info("Destroyed radio");
    329 		goto done;
    330 	}
    331 
    332 	while (l_genl_attr_next(&attr, &type, &len, &data))
    333 	;
    334 
    335 done:
    336 	l_main_quit();
    337 }
    338 
    339 static void list_callback_done(void *user_data)
    340 {
    341 	l_main_quit();
    342 }
    343 
    344 static void list_callback(struct l_genl_msg *msg, void *user_data)
    345 {
    346 	struct l_genl_attr attr;
    347 	uint16_t type, len;
    348 	const void *data;
    349 	uint32_t idx = 0, channels = 0, custom_reg = 0;
    350 	bool reg_strict = false, p2p = false, chanctx = false;
    351 	char alpha2[2] = { };
    352 	char *hwname = NULL;
    353 
    354 	if (!l_genl_attr_init(&attr, msg)) {
    355 		int err = l_genl_msg_get_error(msg);
    356 
    357 		if (err < 0) {
    358 			l_warn("Failed to list radio [%d/%s]",
    359 				-err, strerror(-err));
    360 			exit_status = EXIT_FAILURE;
    361 			return;
    362 		}
    363 	}
    364 
    365 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
    366 		switch (type) {
    367 		case HWSIM_ATTR_RADIO_ID:
    368 			if (len == 4)
    369 				idx = *(int *)data;
    370 			break;
    371 
    372 		case HWSIM_ATTR_CHANNELS:
    373 			if (len == 4)
    374 				channels = *(uint32_t *)data;
    375 			break;
    376 
    377 		case HWSIM_ATTR_REG_HINT_ALPHA2:
    378 			if (len == 2)
    379 				memcpy(&alpha2, data, len);
    380 			break;
    381 
    382 		case HWSIM_ATTR_REG_CUSTOM_REG:
    383 			if (len == 4)
    384 				custom_reg = *(uint32_t *)data;
    385 			break;
    386 
    387 		case HWSIM_ATTR_REG_STRICT_REG:
    388 			reg_strict = true;
    389 			break;
    390 
    391 		case HWSIM_ATTR_SUPPORT_P2P_DEVICE:
    392 			p2p = true;
    393 			break;
    394 
    395 		case HWSIM_ATTR_USE_CHANCTX:
    396 			chanctx = true;
    397 			break;
    398 
    399 		case HWSIM_ATTR_RADIO_NAME:
    400 			if (!hwname) {
    401 				hwname = l_new(char, len + 1);
    402 
    403 				strncpy(hwname, data, len);
    404 			}
    405 
    406 			break;
    407 
    408 		default:
    409 			break;
    410 		}
    411 	}
    412 
    413 	printf("%s radio id %d channels %d alpha2 %d %d custom reg %d "
    414 		"reg strict %d p2p %d chanctx %d\n",
    415 		hwname, idx, channels, alpha2[0], alpha2[1], custom_reg,
    416 		reg_strict, p2p, chanctx);
    417 
    418 	if (hwname)
    419 		l_free(hwname);
    420 }
    421 
    422 struct radio_info_rec {
    423 	int32_t id;
    424 	uint32_t wiphy_id;
    425 	char alpha2[2];
    426 	bool p2p;
    427 	bool custom_regdom;
    428 	uint32_t regdom_idx;
    429 	int channels;
    430 	uint8_t addrs[2][ETH_ALEN];
    431 	char *name;
    432 	bool ap_only;
    433 	struct l_dbus_message *pending;
    434 	uint32_t cmd_id;
    435 };
    436 
    437 struct interface_info_rec {
    438 	uint32_t id;
    439 	struct radio_info_rec *radio_rec;
    440 	uint8_t addr[ETH_ALEN];
    441 	char *name;
    442 	uint32_t iftype;
    443 	int ref;
    444 };
    445 
    446 static struct l_queue *radio_info;
    447 static struct l_queue *interface_info;
    448 
    449 static void radio_free(void *user_data)
    450 {
    451 	struct radio_info_rec *rec = user_data;
    452 
    453 	if (rec->cmd_id)
    454 		l_genl_family_cancel(nl80211, rec->cmd_id);
    455 
    456 	if (rec->pending)
    457 		l_dbus_message_unref(rec->pending);
    458 
    459 	l_free(rec->name);
    460 	l_free(rec);
    461 }
    462 
    463 static void interface_free(void *user_data)
    464 {
    465 	struct interface_info_rec *rec = user_data;
    466 
    467 	l_free(rec->name);
    468 	l_free(rec);
    469 }
    470 
    471 static void hwsim_radio_cache_cleanup(void)
    472 {
    473 	l_queue_destroy(radio_info, radio_free);
    474 	l_queue_destroy(interface_info, interface_free);
    475 	radio_info = NULL;
    476 	interface_info = NULL;
    477 }
    478 
    479 static bool radio_info_match_id(const void *a, const void *b)
    480 {
    481 	const struct radio_info_rec *rec = a;
    482 	int32_t id = L_PTR_TO_INT(b);
    483 
    484 	return rec->id == id;
    485 }
    486 
    487 static bool radio_info_match_wiphy_id(const void *a, const void *b)
    488 {
    489 	const struct radio_info_rec *rec = a;
    490 	uint32_t id = L_PTR_TO_UINT(b);
    491 
    492 	return rec->wiphy_id == id;
    493 }
    494 
    495 static bool radio_info_match_addr0(const void *a, const void *b)
    496 {
    497 	const struct radio_info_rec *rec = a;
    498 	const uint8_t *addr0 = b;
    499 
    500 	return !memcmp(rec->addrs[0], addr0, ETH_ALEN);
    501 }
    502 
    503 static bool radio_info_match_addr1(const void *a, const void *b)
    504 {
    505 	const struct radio_info_rec *rec = a;
    506 	const uint8_t *addr1 = b;
    507 
    508 	return !memcmp(rec->addrs[1], addr1, ETH_ALEN);
    509 }
    510 
    511 static bool interface_info_match_id(const void *a, const void *b)
    512 {
    513 	const struct interface_info_rec *rec = a;
    514 	uint32_t id = L_PTR_TO_UINT(b);
    515 
    516 	return rec->id == id;
    517 }
    518 
    519 static bool interface_info_match_addr(const void *a, const void *b)
    520 {
    521 	const struct interface_info_rec *rec = a;
    522 	const uint8_t *addr = b;
    523 
    524 	return memcmp(rec->addr, addr, ETH_ALEN) == 0;
    525 }
    526 
    527 static const char *radio_get_path(const struct radio_info_rec *rec)
    528 {
    529 	static char path[15];
    530 
    531 	snprintf(path, sizeof(path), "/radio%u", rec->id);
    532 	return path;
    533 }
    534 
    535 static const char *interface_get_path(const struct interface_info_rec *rec)
    536 {
    537 	static char path[25];
    538 
    539 	snprintf(path, sizeof(path), "%s/%u",
    540 			radio_get_path(rec->radio_rec), rec->id);
    541 	return path;
    542 }
    543 
    544 static struct l_dbus_message *dbus_error_failed(struct l_dbus_message *msg)
    545 {
    546 	return l_dbus_message_new_error(msg, HWSIM_SERVICE ".Failed",
    547 					"Operation failed");
    548 }
    549 
    550 static struct l_dbus_message *dbus_error_invalid_args(
    551 						struct l_dbus_message *msg)
    552 {
    553 	return l_dbus_message_new_error(msg, HWSIM_SERVICE ".InvalidArgs",
    554 					"Argument type is wrong");
    555 }
    556 
    557 static void dbus_pending_reply(struct l_dbus_message **msg,
    558 				struct l_dbus_message *reply)
    559 {
    560 	l_dbus_send(dbus, reply);
    561 	l_dbus_message_unref(*msg);
    562 	*msg = NULL;
    563 }
    564 
    565 static const char *rule_get_path(struct hwsim_rule *rule)
    566 {
    567 	static char path[16];
    568 
    569 	snprintf(path, sizeof(path), "/rule%u", rule->id);
    570 
    571 	return path;
    572 }
    573 
    574 static bool parse_addresses(const uint8_t *buf, size_t len,
    575 				struct radio_info_rec *rec)
    576 {
    577 	unsigned int pos = 0, addr_idx = 0;
    578 
    579 	while (pos < len) {
    580 		int start_pos = pos;
    581 		char addr[20];
    582 
    583 		/* Find first word start and end */
    584 		while (pos < len && !l_ascii_isspace(buf[pos]))
    585 			pos++;
    586 
    587 		if (pos - start_pos > sizeof(addr) - 1) {
    588 			l_error("Can't parse a %s address from sysfs",
    589 				rec->name);
    590 			return false;
    591 		}
    592 
    593 		memcpy(addr, buf + start_pos, pos - start_pos);
    594 		addr[pos - start_pos] = '\0';
    595 
    596 		if (addr_idx >= 2) {
    597 			l_error("Hwsim wiphy %s has too many addresses listed "
    598 				" in sysfs - only 2 supported", rec->name);
    599 			return false;
    600 		}
    601 
    602 		if (!util_string_to_address(addr, rec->addrs[addr_idx])) {
    603 			l_error("Can't parse hwsim wiphy %s address from sysfs",
    604 				rec->name);
    605 			return false;
    606 		}
    607 
    608 		addr_idx++;
    609 
    610 		/* Skip until the start of the next word */
    611 		while (pos < len && l_ascii_isspace(buf[pos]))
    612 			pos++;
    613 	}
    614 
    615 	if (addr_idx < 2) {
    616 		l_error("Hwsim wiphy %s has too few addresses listed "
    617 			" in sysfs - only 2 supported", rec->name);
    618 		return false;
    619 	}
    620 
    621 	return true;
    622 }
    623 
    624 static void get_radio_callback(struct l_genl_msg *msg, void *user_data)
    625 {
    626 	struct l_genl_attr attr;
    627 	uint16_t type, len;
    628 	const void *data;
    629 	_auto_(l_free) char *name = NULL;
    630 	const int32_t *id = NULL;
    631 	struct radio_info_rec *rec = NULL;
    632 	uint8_t file_buffer[128];
    633 	int bytes, consumed;
    634 	unsigned int uintval;
    635 	bool changed = false;
    636 	bool new = false;
    637 	struct radio_info_rec prev_rec;
    638 	bool name_change = false;
    639 	const char *path;
    640 	struct l_dbus_message *reply;
    641 	const struct l_queue_entry *entry;
    642 
    643 	if (!l_genl_attr_init(&attr, msg))
    644 		return;
    645 
    646 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
    647 		switch (type) {
    648 		case HWSIM_ATTR_RADIO_ID:
    649 			if (len != 4)
    650 				break;
    651 
    652 			id = data;
    653 
    654 			/*
    655 			 * ID of -1 denotes a pending creation, so if somehow
    656 			 * the kernel ID counter reaches an extremely high
    657 			 * number of radios we just bail.
    658 			 */
    659 			if (L_WARN_ON(*id < 0))
    660 				return;
    661 
    662 			break;
    663 
    664 		case HWSIM_ATTR_RADIO_NAME:
    665 			if (name)
    666 				break;
    667 
    668 			name = l_strndup(data, len);
    669 			break;
    670 		}
    671 	}
    672 
    673 	if (!id || !name)
    674 		return;
    675 
    676 	for (entry = l_queue_get_entries(radio_info); entry;
    677 						entry = entry->next) {
    678 		struct radio_info_rec *r = entry->data;
    679 
    680 		if (*id == r->id) {
    681 			changed = true;
    682 			memcpy(&prev_rec, r, sizeof(prev_rec));
    683 
    684 			if (strcmp(r->name, name))
    685 				name_change = true;
    686 
    687 			l_free(r->name);
    688 			r->name = l_steal_ptr(name);
    689 
    690 			rec = r;
    691 			break;
    692 		} else if (!strcmp(r->name, name)) {
    693 			rec = r;
    694 			rec->id = *id;
    695 
    696 			break;
    697 		}
    698 	}
    699 
    700 	if (!rec) {
    701 		new = true;
    702 		rec = l_new(struct radio_info_rec, 1);
    703 		rec->id = *id;
    704 		rec->name = l_steal_ptr(name);
    705 	}
    706 
    707 	l_genl_attr_init(&attr, msg);
    708 
    709 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
    710 		switch (type) {
    711 		case HWSIM_ATTR_CHANNELS:
    712 			if (len != 4)
    713 				break;
    714 
    715 			rec->channels = *(uint32_t *) data;
    716 			break;
    717 
    718 		case HWSIM_ATTR_REG_HINT_ALPHA2:
    719 			if (len != 2)
    720 				break;
    721 
    722 			memcpy(rec->alpha2, data, 2);
    723 
    724 			break;
    725 
    726 		case HWSIM_ATTR_SUPPORT_P2P_DEVICE:
    727 			rec->p2p = true;
    728 			break;
    729 
    730 		case HWSIM_ATTR_REG_CUSTOM_REG:
    731 			if (len != 4)
    732 				break;
    733 
    734 			rec->custom_regdom = true;
    735 			rec->regdom_idx = *(uint32_t *) data;
    736 			break;
    737 		}
    738 	}
    739 
    740 	/*
    741 	 * Assuming that the radio name is the wiphy name read the wiphy index
    742 	 * associated with the radio and the wiphy's hardware addresses from
    743 	 * sysfs.  The index could be obtained through NL80211_CMD_GET_WIPHY
    744 	 * but that is costly and reading the index synchronously simplifies
    745 	 * the job a lot.  We have to resort to sysfs anyway to obtain the
    746 	 * radio addresses.
    747 	 */
    748 
    749 	bytes = read_file((char *) file_buffer, sizeof(file_buffer) - 1,
    750 				"/sys/class/ieee80211/%s/index", rec->name);
    751 	if (bytes < 0) {
    752 		l_error("Error reading index for %s from sysfs", rec->name);
    753 		goto err_free_radio;
    754 	}
    755 
    756 	file_buffer[bytes] = '\0';
    757 	if (sscanf((char *) file_buffer, "%u %n", &uintval, &consumed) != 1 ||
    758 			consumed != bytes) {
    759 		l_error("Error parsing index for %s from sysfs", rec->name);
    760 		goto err_free_radio;
    761 	}
    762 
    763 	rec->wiphy_id = uintval;
    764 
    765 	bytes = read_file(file_buffer, sizeof(file_buffer),
    766 				"/sys/class/ieee80211/%s/addresses", rec->name);
    767 	if (bytes < 0) {
    768 		l_error("Error reading addresses for %s from sysfs", rec->name);
    769 		goto err_free_radio;
    770 	}
    771 
    772 	if (!parse_addresses(file_buffer, bytes, rec))
    773 		goto err_free_radio;
    774 
    775 	if (!radio_info)
    776 		radio_info = l_queue_new();
    777 
    778 	if (new)
    779 		l_queue_push_tail(radio_info, rec);
    780 
    781 	path = radio_get_path(rec);
    782 
    783 	if (!changed) {
    784 		/* Create Dbus object */
    785 
    786 		if (!l_dbus_object_add_interface(dbus, path,
    787 						HWSIM_RADIO_INTERFACE, rec))
    788 			l_info("Unable to add the %s interface to %s",
    789 					HWSIM_RADIO_INTERFACE, path);
    790 
    791 		if (!l_dbus_object_add_interface(dbus, path,
    792 						L_DBUS_INTERFACE_PROPERTIES,
    793 						NULL))
    794 			l_info("Unable to add the %s interface to %s",
    795 					L_DBUS_INTERFACE_PROPERTIES, path);
    796 	} else {
    797 		/* Emit property change events */
    798 
    799 		if (memcmp(&prev_rec.addrs, &rec->addrs, sizeof(rec->addrs)))
    800 			l_dbus_property_changed(dbus, path,
    801 						HWSIM_RADIO_INTERFACE,
    802 						"Addresses");
    803 
    804 		if (name_change)
    805 			l_dbus_property_changed(dbus, path,
    806 						HWSIM_RADIO_INTERFACE, "Name");
    807 	}
    808 
    809 	/* Send pending CreateRadio reply */
    810 	if (rec->pending) {
    811 		reply = l_dbus_message_new_method_return(rec->pending);
    812 
    813 		l_dbus_message_set_arguments(reply, "o", path);
    814 		dbus_pending_reply(&rec->pending, reply);
    815 	}
    816 
    817 	return;
    818 
    819 err_free_radio:
    820 	if (rec->pending)
    821 		dbus_pending_reply(&rec->pending,
    822 					dbus_error_failed(rec->pending));
    823 
    824 	if (!new)
    825 		l_queue_remove(radio_info, rec);
    826 
    827 	radio_free(rec);
    828 }
    829 
    830 static bool radio_ap_only(struct radio_info_rec *rec)
    831 {
    832 	const struct l_queue_entry *i;
    833 	unsigned int n = 0;
    834 	bool have_ap = false;
    835 
    836 	for (i = l_queue_get_entries(interface_info); i; i = i->next) {
    837 		struct interface_info_rec *interface = i->data;
    838 
    839 		if (interface->radio_rec != rec)
    840 			continue;
    841 
    842 		if (interface->iftype == NL80211_IFTYPE_AP)
    843 			have_ap = true;
    844 
    845 		n += 1;
    846 	}
    847 
    848 	return n == 1 && have_ap;
    849 }
    850 
    851 static void get_wiphy_callback(struct l_genl_msg *msg, void *user_data)
    852 {
    853 	const char *name;
    854 	uint32_t id;
    855 	struct radio_info_rec *rec;
    856 
    857 	if (nl80211_parse_attrs(msg, NL80211_ATTR_WIPHY, &id,
    858 					NL80211_ATTR_WIPHY_NAME, &name,
    859 					NL80211_ATTR_UNSPEC) < 0)
    860 		return;
    861 
    862 	rec = l_queue_find(radio_info, radio_info_match_wiphy_id,
    863 				L_UINT_TO_PTR(id));
    864 	if (!rec)
    865 		return;
    866 
    867 	if (!strcmp(rec->name, name))
    868 		return;
    869 
    870 	l_free(rec->name);
    871 	rec->name = l_strdup(name);
    872 
    873 	l_dbus_property_changed(dbus, radio_get_path(rec),
    874 				HWSIM_RADIO_INTERFACE, "Name");
    875 }
    876 
    877 static void get_interface_callback(struct l_genl_msg *msg, void *user_data)
    878 {
    879 	uint32_t ifindex;
    880 	uint32_t wiphy_id;
    881 	uint32_t iftype;
    882 	const uint8_t *addr;
    883 	const char *ifname;
    884 	struct interface_info_rec *rec;
    885 	struct radio_info_rec *radio_rec;
    886 	bool old;
    887 	const char *path;
    888 	struct interface_info_rec prev_rec;
    889 	bool name_change = false;
    890 
    891 	if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex,
    892 					NL80211_ATTR_IFNAME, &ifname,
    893 					NL80211_ATTR_WIPHY, &wiphy_id,
    894 					NL80211_ATTR_IFTYPE, &iftype,
    895 					NL80211_ATTR_MAC, &addr,
    896 					NL80211_ATTR_UNSPEC) < 0)
    897 		return;
    898 
    899 	radio_rec = l_queue_find(radio_info, radio_info_match_wiphy_id,
    900 				L_UINT_TO_PTR(wiphy_id));
    901 	if (!radio_rec)
    902 		/* This is not a hwsim interface, don't track it */
    903 		return;
    904 
    905 	rec = l_queue_find(interface_info, interface_info_match_id,
    906 				L_UINT_TO_PTR(ifindex));
    907 	if (rec) {
    908 		old = true;
    909 
    910 		memcpy(&prev_rec, rec, sizeof(prev_rec));
    911 
    912 		if (strcmp(rec->name, ifname))
    913 			name_change = true;
    914 
    915 		l_free(rec->name);
    916 	} else {
    917 		old = false;
    918 
    919 		rec = l_new(struct interface_info_rec, 1);
    920 
    921 		rec->id = ifindex;
    922 		rec->radio_rec = radio_rec;
    923 	}
    924 
    925 	memcpy(rec->addr, addr, ETH_ALEN);
    926 	rec->name = l_strdup(ifname);
    927 	rec->iftype = iftype;
    928 
    929 	if (!interface_info)
    930 		interface_info = l_queue_new();
    931 
    932 	if (!old)
    933 		l_queue_push_tail(interface_info, rec);
    934 
    935 	path = interface_get_path(rec);
    936 
    937 	if (!old) {
    938 		/* Create Dbus object */
    939 
    940 		if (!l_dbus_object_add_interface(dbus, path,
    941 						HWSIM_INTERFACE_INTERFACE, rec))
    942 			l_info("Unable to add the %s interface to %s",
    943 					HWSIM_INTERFACE_INTERFACE, path);
    944 
    945 		if (!l_dbus_object_add_interface(dbus, path,
    946 						L_DBUS_INTERFACE_PROPERTIES,
    947 						NULL))
    948 			l_info("Unable to add the %s interface to %s",
    949 					L_DBUS_INTERFACE_PROPERTIES, path);
    950 	} else {
    951 		/* Emit property change events */
    952 
    953 		if (memcmp(prev_rec.addr, rec->addr, ETH_ALEN))
    954 			l_dbus_property_changed(dbus, path,
    955 						HWSIM_INTERFACE_INTERFACE,
    956 						"Address");
    957 
    958 		if (name_change)
    959 			l_dbus_property_changed(dbus, path,
    960 						HWSIM_INTERFACE_INTERFACE,
    961 						"Name");
    962 	}
    963 
    964 	radio_rec->ap_only = radio_ap_only(radio_rec);
    965 }
    966 
    967 static bool interface_info_destroy_by_radio(void *data, void *user_data)
    968 {
    969 	struct interface_info_rec *rec = data;
    970 	struct radio_info_rec *radio_rec = user_data;
    971 
    972 	if (rec->radio_rec != radio_rec)
    973 		return false;
    974 
    975 	l_dbus_unregister_object(dbus, interface_get_path(rec));
    976 	interface_free(rec);
    977 
    978 	return true;
    979 }
    980 
    981 static void del_radio_event(struct l_genl_msg *msg)
    982 {
    983 	struct radio_info_rec *radio;
    984 	struct l_genl_attr attr;
    985 	uint16_t type, len;
    986 	const void *data;
    987 	const int32_t *id = NULL;
    988 
    989 	if (!l_genl_attr_init(&attr, msg))
    990 		return;
    991 
    992 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
    993 		switch (type) {
    994 		case HWSIM_ATTR_RADIO_ID:
    995 			if (len != 4)
    996 				break;
    997 
    998 			id = data;
    999 
   1000 			if (L_WARN_ON(*id < 0))
   1001 				return;
   1002 
   1003 			break;
   1004 		}
   1005 	}
   1006 
   1007 	if (!id)
   1008 		return;
   1009 
   1010 	radio = l_queue_find(radio_info, radio_info_match_id,
   1011 				L_INT_TO_PTR(*id));
   1012 	if (!radio)
   1013 		return;
   1014 
   1015 	l_queue_foreach_remove(interface_info, interface_info_destroy_by_radio,
   1016 				radio);
   1017 	l_dbus_unregister_object(dbus, radio_get_path(radio));
   1018 	l_queue_remove(radio_info, radio);
   1019 	radio_free(radio);
   1020 }
   1021 
   1022 static void set_interface_event(struct l_genl_msg *msg)
   1023 {
   1024 	struct interface_info_rec *interface;
   1025 	uint32_t ifindex;
   1026 	uint32_t iftype;
   1027 
   1028 	if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex,
   1029 					NL80211_ATTR_IFTYPE, &iftype,
   1030 					NL80211_ATTR_UNSPEC) < 0)
   1031 		return;
   1032 
   1033 	interface = l_queue_find(interface_info, interface_info_match_id,
   1034 					L_UINT_TO_PTR(ifindex));
   1035 	if (!interface)
   1036 		return;
   1037 
   1038 	if (interface->iftype == iftype)
   1039 		return;
   1040 
   1041 	l_debug("Interface iftype changed for ifindex: %u, iftype: %u",
   1042 			ifindex, iftype);
   1043 	interface->iftype = iftype;
   1044 
   1045 	interface->radio_rec->ap_only = radio_ap_only(interface->radio_rec);
   1046 }
   1047 
   1048 static void del_interface_event(struct l_genl_msg *msg)
   1049 {
   1050 	struct interface_info_rec *interface;
   1051 	uint32_t ifindex;
   1052 
   1053 	if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex,
   1054 					NL80211_ATTR_UNSPEC) < 0)
   1055 		return;
   1056 
   1057 	interface = l_queue_find(interface_info, interface_info_match_id,
   1058 					L_UINT_TO_PTR(ifindex));
   1059 	if (!interface)
   1060 		return;
   1061 
   1062 	l_dbus_unregister_object(dbus, interface_get_path(interface));
   1063 	l_queue_remove(interface_info, interface);
   1064 	interface_free(interface);
   1065 }
   1066 
   1067 static void hwsim_config(struct l_genl_msg *msg, void *user_data)
   1068 {
   1069 	struct l_genl_attr attr;
   1070 	uint16_t type, len;
   1071 	const void *data;
   1072 	uint8_t cmd;
   1073 
   1074 	cmd = l_genl_msg_get_command(msg);
   1075 
   1076 	l_debug("Config changed cmd %u", cmd);
   1077 
   1078 	if (!l_genl_attr_init(&attr, msg))
   1079 		return;
   1080 
   1081 	while (l_genl_attr_next(&attr, &type, &len, &data))
   1082 		l_debug("\tattr type %d len %d", type, len);
   1083 
   1084 	switch (cmd) {
   1085 	case HWSIM_CMD_NEW_RADIO:
   1086 		get_radio_callback(msg, NULL);
   1087 		break;
   1088 	case HWSIM_CMD_DEL_RADIO:
   1089 		del_radio_event(msg);
   1090 		break;
   1091 	}
   1092 }
   1093 
   1094 static void nl80211_config_notify(struct l_genl_msg *msg, void *user_data)
   1095 {
   1096 	uint8_t cmd = l_genl_msg_get_command(msg);
   1097 
   1098 	l_debug("Config notification %s(%u)", nl80211cmd_to_string(cmd), cmd);
   1099 
   1100 	switch (cmd) {
   1101 	case NL80211_CMD_NEW_WIPHY:
   1102 		get_wiphy_callback(msg, NULL);
   1103 		break;
   1104 	case NL80211_CMD_NEW_INTERFACE:
   1105 		get_interface_callback(msg, NULL);
   1106 		break;
   1107 	case NL80211_CMD_SET_INTERFACE:
   1108 		set_interface_event(msg);
   1109 		break;
   1110 	case NL80211_CMD_DEL_INTERFACE:
   1111 		del_interface_event(msg);
   1112 		break;
   1113 	}
   1114 }
   1115 
   1116 static void rtnl_newlink_notify(const struct ifinfomsg *ifi, int bytes)
   1117 {
   1118 	struct rtattr *attr;
   1119 	struct interface_info_rec *rec;
   1120 	bool addr_change = false, name_change = false;
   1121 	const char *path;
   1122 
   1123 	rec = l_queue_find(interface_info, interface_info_match_id,
   1124 				L_UINT_TO_PTR(ifi->ifi_index));
   1125 	if (!rec)
   1126 		return;
   1127 
   1128 	for (attr = IFLA_RTA(ifi); RTA_OK(attr, bytes);
   1129 			attr = RTA_NEXT(attr, bytes)) {
   1130 		switch (attr->rta_type) {
   1131 		case IFLA_IFNAME:
   1132 			if (!strcmp(rec->name, RTA_DATA(attr)))
   1133 				continue;
   1134 
   1135 			name_change = true;
   1136 			l_free(rec->name);
   1137 			rec->name = l_strdup(RTA_DATA(attr));
   1138 			break;
   1139 		case IFLA_ADDRESS:
   1140 			if (RTA_PAYLOAD(attr) < ETH_ALEN)
   1141 				break;
   1142 
   1143 			if (!memcmp(rec->addr, RTA_DATA(attr), ETH_ALEN))
   1144 				continue;
   1145 
   1146 			addr_change = true;
   1147 			memcpy(rec->addr, RTA_DATA(attr), ETH_ALEN);
   1148 			break;
   1149 		}
   1150 	}
   1151 
   1152 	if (!addr_change && !name_change)
   1153 		return;
   1154 
   1155 	path = interface_get_path(rec);
   1156 
   1157 	if (addr_change)
   1158 		l_dbus_property_changed(dbus, path, HWSIM_INTERFACE_INTERFACE,
   1159 					"Address");
   1160 
   1161 	if (name_change)
   1162 		l_dbus_property_changed(dbus, path, HWSIM_INTERFACE_INTERFACE,
   1163 					"Name");
   1164 }
   1165 
   1166 static void rtnl_link_notify(uint16_t type, const void *data, uint32_t len,
   1167 				void *user_data)
   1168 {
   1169 	const struct ifinfomsg *ifi = data;
   1170 	unsigned int bytes;
   1171 
   1172 	if (ifi->ifi_type != ARPHRD_ETHER)
   1173 		return;
   1174 
   1175 	bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
   1176 
   1177 	switch (type) {
   1178 	case RTM_NEWLINK:
   1179 		rtnl_newlink_notify(ifi, bytes);
   1180 		break;
   1181 	}
   1182 }
   1183 
   1184 struct hwsim_tx_info {
   1185 	int8_t idx;
   1186 	uint8_t count;
   1187 };
   1188 
   1189 struct hwsim_frame {
   1190 	int refcount;
   1191 	uint8_t src_ether_addr[ETH_ALEN];
   1192 	uint8_t dst_ether_addr[ETH_ALEN];
   1193 	struct radio_info_rec *src_radio;
   1194 	struct radio_info_rec *ack_radio;
   1195 	uint32_t flags;
   1196 	const uint64_t *cookie;
   1197 	int32_t signal;
   1198 	uint32_t frequency;
   1199 	uint16_t tx_info_len;
   1200 	const struct hwsim_tx_info *tx_info;
   1201 	uint16_t payload_len;
   1202 	const uint8_t *payload;
   1203 	bool acked;
   1204 	struct l_genl_msg *msg;
   1205 	int pending_callback_count;
   1206 };
   1207 
   1208 static bool radio_match_addr(const struct radio_info_rec *radio,
   1209 				const uint8_t *addr)
   1210 {
   1211 	if (!radio || util_is_broadcast_address(addr))
   1212 		return !radio && util_is_broadcast_address(addr);
   1213 
   1214 	return !memcmp(addr, radio->addrs[0], ETH_ALEN) ||
   1215 		!memcmp(addr, radio->addrs[1], ETH_ALEN);
   1216 }
   1217 
   1218 static void process_rules(const struct radio_info_rec *src_radio,
   1219 				const struct radio_info_rec *dst_radio,
   1220 				struct hwsim_frame *frame, bool ack, bool *drop,
   1221 				uint32_t *delay)
   1222 {
   1223 	const struct l_queue_entry *rule_entry;
   1224 
   1225 	for (rule_entry = l_queue_get_entries(rules); rule_entry;
   1226 			rule_entry = rule_entry->next) {
   1227 		struct hwsim_rule *rule = rule_entry->data;
   1228 
   1229 		if (!rule->enabled)
   1230 			continue;
   1231 
   1232 		if (!rule->source_any &&
   1233 				!radio_match_addr(src_radio, rule->source) &&
   1234 				(!rule->bidirectional ||
   1235 				 !radio_match_addr(dst_radio, rule->source)))
   1236 			continue;
   1237 
   1238 		if (!rule->destination_any &&
   1239 				!radio_match_addr(dst_radio,
   1240 							rule->destination) &&
   1241 				(!rule->bidirectional ||
   1242 				 !radio_match_addr(src_radio,
   1243 							rule->destination)))
   1244 			continue;
   1245 
   1246 		/*
   1247 		 * If source matches only because rule->bidirectional was
   1248 		 * true, make sure destination is "any" or matches source
   1249 		 * radio's address.
   1250 		 */
   1251 		if (!rule->source_any && rule->bidirectional &&
   1252 				radio_match_addr(dst_radio, rule->source))
   1253 			if (!rule->destination_any &&
   1254 					!radio_match_addr(dst_radio,
   1255 							rule->destination))
   1256 				continue;
   1257 
   1258 		if (rule->frequency && rule->frequency != frame->frequency)
   1259 			continue;
   1260 
   1261 		if (rule->prefix && frame->payload_len >= rule->prefix_len) {
   1262 			if (memcmp(rule->prefix, frame->payload,
   1263 					rule->prefix_len) != 0)
   1264 				continue;
   1265 		}
   1266 
   1267 		if (rule->match && frame->payload_len >=
   1268 					rule->match_len + rule->match_offset) {
   1269 			if (memcmp(rule->match,
   1270 					frame->payload + rule->match_offset,
   1271 					rule->match_len))
   1272 				continue;
   1273 		}
   1274 
   1275 		/* Rule deemed to match frame, apply any changes */
   1276 		if (rule->match_times == 0)
   1277 			continue;
   1278 
   1279 		if (rule->signal)
   1280 			frame->signal = rule->signal / 100;
   1281 
   1282 		/* Don't drop if this is an ACK, unless drop_ack is set */
   1283 		if (!ack || (ack && rule->drop_ack))
   1284 			*drop = rule->drop;
   1285 
   1286 		if (delay)
   1287 			*delay = rule->delay;
   1288 
   1289 		if (rule->match_times > 0)
   1290 			rule->match_times--;
   1291 	}
   1292 }
   1293 
   1294 struct send_frame_info {
   1295 	struct hwsim_frame *frame;
   1296 	struct radio_info_rec *radio;
   1297 	void *user_data;
   1298 };
   1299 
   1300 static bool send_frame_tx_info(struct hwsim_frame *frame)
   1301 {
   1302 	struct l_genl_msg *msg;
   1303 
   1304 	msg = l_genl_msg_new_sized(HWSIM_CMD_TX_INFO_FRAME,
   1305 					128 + frame->tx_info_len);
   1306 	l_genl_msg_append_attr(msg, HWSIM_ATTR_ADDR_TRANSMITTER, ETH_ALEN,
   1307 				frame->src_radio->addrs[1]);
   1308 	l_genl_msg_append_attr(msg, HWSIM_ATTR_FLAGS, 4, &frame->flags);
   1309 	l_genl_msg_append_attr(msg, HWSIM_ATTR_SIGNAL, 4, &frame->signal);
   1310 	l_genl_msg_append_attr(msg, HWSIM_ATTR_COOKIE, 8, frame->cookie);
   1311 	l_genl_msg_append_attr(msg, HWSIM_ATTR_TX_INFO, frame->tx_info_len,
   1312 				frame->tx_info);
   1313 
   1314 	if (!l_genl_family_send(hwsim, msg, NULL, NULL, NULL)) {
   1315 		l_error("Sending HWSIM_CMD_TX_INFO_FRAME failed");
   1316 		return false;
   1317 	}
   1318 
   1319 	return true;
   1320 }
   1321 
   1322 static bool send_frame(struct send_frame_info *info,
   1323 			l_genl_msg_func_t callback,
   1324 			l_genl_destroy_func_t destroy)
   1325 {
   1326 	struct l_genl_msg *msg;
   1327 	uint32_t rx_rate = 2;
   1328 	unsigned int id;
   1329 
   1330 	msg = l_genl_msg_new_sized(HWSIM_CMD_FRAME,
   1331 					128 + info->frame->payload_len);
   1332 	l_genl_msg_append_attr(msg, HWSIM_ATTR_ADDR_RECEIVER, ETH_ALEN,
   1333 				info->radio->addrs[1]);
   1334 	l_genl_msg_append_attr(msg, HWSIM_ATTR_FRAME, info->frame->payload_len,
   1335 				info->frame->payload);
   1336 	l_genl_msg_append_attr(msg, HWSIM_ATTR_RX_RATE, 4,
   1337 				&rx_rate);
   1338 	l_genl_msg_append_attr(msg, HWSIM_ATTR_SIGNAL, 4,
   1339 				&info->frame->signal);
   1340 	l_genl_msg_append_attr(msg, HWSIM_ATTR_FREQ, 4,
   1341 				&info->frame->frequency);
   1342 
   1343 	id = l_genl_family_send(hwsim, msg, callback, info, destroy);
   1344 	if (!id) {
   1345 		l_error("Sending HWSIM_CMD_FRAME failed");
   1346 		return false;
   1347 	}
   1348 
   1349 	return true;
   1350 }
   1351 
   1352 static struct hwsim_frame *hwsim_frame_ref(struct hwsim_frame *frame)
   1353 {
   1354 	__sync_fetch_and_add(&frame->refcount, 1);
   1355 
   1356 	return frame;
   1357 }
   1358 
   1359 static void hwsim_frame_unref(struct hwsim_frame *frame)
   1360 {
   1361 	if (__sync_sub_and_fetch(&frame->refcount, 1))
   1362 		return;
   1363 
   1364 	if (!frame->pending_callback_count) {
   1365 		/*
   1366 		 * Apparently done with this frame, send tx info and signal
   1367 		 * the returning of an ACK frame in the opposite direction.
   1368 		 */
   1369 
   1370 		if (!(frame->flags & HWSIM_TX_CTL_NO_ACK) && frame->acked) {
   1371 			bool drop = false;
   1372 
   1373 			process_rules(frame->ack_radio, frame->src_radio,
   1374 					frame, true, &drop, NULL);
   1375 
   1376 			if (!drop)
   1377 				frame->flags |= HWSIM_TX_STAT_ACK;
   1378 		}
   1379 
   1380 		if (frame->src_radio)
   1381 			send_frame_tx_info(frame);
   1382 	}
   1383 
   1384 	l_genl_msg_unref(frame->msg);
   1385 	l_free(frame);
   1386 }
   1387 
   1388 static void send_frame_callback(struct l_genl_msg *msg, void *user_data)
   1389 {
   1390 	struct send_frame_info *info = user_data;
   1391 
   1392 	if (l_genl_msg_get_error(msg) == 0) {
   1393 		info->frame->acked = true;
   1394 		info->frame->ack_radio = info->radio;
   1395 	}
   1396 
   1397 	info->frame->pending_callback_count--;
   1398 }
   1399 
   1400 static void send_frame_destroy(void *user_data)
   1401 {
   1402 	struct send_frame_info *info = user_data;
   1403 
   1404 	hwsim_frame_unref(info->frame);
   1405 	l_free(info);
   1406 }
   1407 
   1408 static void send_custom_frame_callback(struct l_genl_msg *msg, void *user_data)
   1409 {
   1410 	struct send_frame_info *info = user_data;
   1411 	struct l_dbus_message *message = info->user_data;
   1412 	struct l_dbus_message *reply;
   1413 
   1414 	info->user_data = NULL;
   1415 
   1416 	if (l_genl_msg_get_error(msg) < 0) {
   1417 		/* Radio address or frequency didn't match */
   1418 		l_debug("HWSIM_CMD_FRAME failed for destination %s: %d",
   1419 				util_address_to_string(info->radio->addrs[0]),
   1420 				l_genl_msg_get_error(msg));
   1421 		dbus_pending_reply(&message, dbus_error_invalid_args(message));
   1422 		return;
   1423 	}
   1424 
   1425 	reply = l_dbus_message_new_method_return(message);
   1426 	l_dbus_message_set_arguments(reply, "");
   1427 	dbus_pending_reply(&message, reply);
   1428 }
   1429 
   1430 static void send_custom_frame_destroy(void *user_data)
   1431 {
   1432 	struct send_frame_info *info = user_data;
   1433 
   1434 	if (info->user_data)
   1435 		l_dbus_message_unref(info->user_data);
   1436 
   1437 	l_free(info->frame);
   1438 	l_free(info);
   1439 }
   1440 
   1441 static bool send_custom_frame(const uint8_t *addr, uint32_t freq,
   1442 		int32_t signal, const void *payload, uint32_t len,
   1443 		void *user_data)
   1444 {
   1445 	struct hwsim_frame *frame = l_new(struct hwsim_frame, 1);
   1446 	struct send_frame_info *info = l_new(struct send_frame_info, 1);
   1447 
   1448 	frame->frequency = freq;
   1449 	frame->signal = signal;
   1450 	frame->payload_len = len;
   1451 	frame->payload = payload;
   1452 
   1453 	info->frame = frame;
   1454 	info->user_data = user_data;
   1455 
   1456 	info->radio = l_queue_find(radio_info, radio_info_match_addr0, addr) ?:
   1457 		l_queue_find(radio_info, radio_info_match_addr1, addr);
   1458 	if (!info->radio)
   1459 		goto error;
   1460 
   1461 	if (!send_frame(info, send_custom_frame_callback,
   1462 			send_custom_frame_destroy))
   1463 		goto error;
   1464 
   1465 	return true;
   1466 
   1467 error:
   1468 	l_free(frame);
   1469 	l_free(info);
   1470 
   1471 	return false;
   1472 }
   1473 
   1474 static void frame_delay_callback(struct l_timeout *timeout, void *user_data)
   1475 {
   1476 	struct send_frame_info *send_info = user_data;
   1477 
   1478 	if (send_frame(send_info, send_frame_callback,
   1479 					send_frame_destroy))
   1480 		send_info->frame->pending_callback_count++;
   1481 	else
   1482 		send_frame_destroy(send_info);
   1483 
   1484 	if (timeout)
   1485 		l_timeout_remove(timeout);
   1486 }
   1487 
   1488 
   1489 /*
   1490  * Process frames in a similar way to how the kernel built-in hwsim medium
   1491  * does this, with an additional optimization for unicast frames and
   1492  * additional modifications to frames decided by user-configurable rules.
   1493  */
   1494 static void process_frame(struct hwsim_frame *frame)
   1495 {
   1496 	const struct l_queue_entry *entry;
   1497 	bool drop_mcast = false;
   1498 	bool beacon = false;
   1499 
   1500 	if (util_is_broadcast_address(frame->dst_ether_addr))
   1501 		process_rules(frame->src_radio, NULL, frame, false,
   1502 				&drop_mcast, NULL);
   1503 
   1504 	if (frame->payload_len >= 2 &&
   1505 			frame->payload[0] == 0x80 &&
   1506 			frame->payload[1] == 0x00)
   1507 		beacon = true;
   1508 
   1509 	for (entry = l_queue_get_entries(radio_info); entry;
   1510 			entry = entry->next) {
   1511 		struct radio_info_rec *radio = entry->data;
   1512 		struct send_frame_info *send_info;
   1513 		bool drop = drop_mcast;
   1514 		uint32_t delay = 0;
   1515 		const struct l_queue_entry *i;
   1516 
   1517 		if (radio == frame->src_radio)
   1518 			continue;
   1519 
   1520 		/*
   1521 		 * The kernel hwsim medium passes multicast frames to all
   1522 		 * radios that are on the same frequency as this frame but
   1523 		 * the netlink medium API only lets userspace pass frames to
   1524 		 * radios by known hardware address.  It does check that the
   1525 		 * receiving radio is on the same frequency though so we can
   1526 		 * send to all known addresses.
   1527 		 *
   1528 		 * If the frame's Receiver Address (RA) is a multicast
   1529 		 * address, then send the frame to every radio that is
   1530 		 * registered.  If it's a unicast address then optimize
   1531 		 * by only forwarding the frame to the radios that have
   1532 		 * at least one interface with this specific address.
   1533 		 */
   1534 		if (!util_is_broadcast_address(frame->dst_ether_addr)) {
   1535 			for (i = l_queue_get_entries(interface_info);
   1536 					i; i = i->next) {
   1537 				struct interface_info_rec *interface = i->data;
   1538 
   1539 				if (interface->radio_rec != radio)
   1540 					continue;
   1541 
   1542 				if (!memcmp(interface->addr,
   1543 						frame->dst_ether_addr,
   1544 						ETH_ALEN))
   1545 					break;
   1546 			}
   1547 
   1548 			if (!i)
   1549 				continue;
   1550 		}
   1551 
   1552 		process_rules(frame->src_radio, radio, frame, false,
   1553 				&drop, &delay);
   1554 
   1555 		if (drop)
   1556 			continue;
   1557 
   1558 		/*
   1559 		 * Don't bother sending beacons to other AP interfaces
   1560 		 * if the AP interface is the only one on this phy
   1561 		 */
   1562 		if (beacon && radio->ap_only)
   1563 			continue;
   1564 
   1565 		send_info = l_new(struct send_frame_info, 1);
   1566 		send_info->radio = radio;
   1567 		send_info->frame = hwsim_frame_ref(frame);
   1568 
   1569 		if (delay) {
   1570 			if (!l_timeout_create_ms(delay, frame_delay_callback,
   1571 							send_info, NULL)) {
   1572 				l_error("Error delaying frame %ums, "
   1573 						"frame will be dropped", delay);
   1574 				send_frame_destroy(send_info);
   1575 			}
   1576 		} else
   1577 			frame_delay_callback(NULL, send_info);
   1578 
   1579 	}
   1580 
   1581 	hwsim_frame_unref(frame);
   1582 }
   1583 
   1584 
   1585 
   1586 static void hwsim_frame_event(struct l_genl_msg *msg)
   1587 {
   1588 	struct hwsim_frame *frame;
   1589 	const struct mmpdu_header *mpdu;
   1590 	struct l_genl_attr attr;
   1591 	uint16_t type, len;
   1592 	const void *data;
   1593 	const uint8_t *transmitter = NULL, *freq = NULL, *flags = NULL;
   1594 
   1595 	if (!l_genl_attr_init(&attr, msg))
   1596 		return;
   1597 
   1598 	frame = l_new(struct hwsim_frame, 1);
   1599 
   1600 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   1601 		switch (type) {
   1602 		case HWSIM_ATTR_ADDR_TRANSMITTER:
   1603 			if (len != ETH_ALEN)
   1604 				break;
   1605 
   1606 			transmitter = data;
   1607 			break;
   1608 
   1609 		case HWSIM_ATTR_FREQ:
   1610 			if (len != 4)
   1611 				break;
   1612 
   1613 			freq = data;
   1614 			break;
   1615 
   1616 		case HWSIM_ATTR_FLAGS:
   1617 			if (len != 4)
   1618 				break;
   1619 
   1620 			flags = data;
   1621 			break;
   1622 
   1623 		case HWSIM_ATTR_COOKIE:
   1624 			if (len != 8)
   1625 				break;
   1626 
   1627 			frame->cookie = (const uint64_t *) data;
   1628 			break;
   1629 
   1630 		case HWSIM_ATTR_FRAME:
   1631 			if (len > IEEE80211_MAX_DATA_LEN)
   1632 				break;
   1633 
   1634 			/* Duration + Address1 + Address2 + Address3 + SeqCtl */
   1635 			if (len < sizeof(struct mpdu_fc) + 22) {
   1636 				l_error("Frame payload too short for header");
   1637 				break;
   1638 			}
   1639 
   1640 			frame->payload_len = len;
   1641 			frame->payload = data;
   1642 
   1643 			break;
   1644 
   1645 		case HWSIM_ATTR_TX_INFO:
   1646 			if (len > sizeof(struct hwsim_tx_info) *
   1647 					IEEE80211_TX_RATE_TABLE_SIZE)
   1648 				break;
   1649 
   1650 			frame->tx_info_len = len;
   1651 			frame->tx_info = data;
   1652 
   1653 			break;
   1654 
   1655 		default:
   1656 			if (type >= __HWSIM_ATTR_MAX)
   1657 				l_warn("Unknown attribute type: %u", type);
   1658 			break;
   1659 		}
   1660 	}
   1661 
   1662 	if (!frame->payload || !frame->tx_info || !frame->cookie ||
   1663 			!flags || !freq || !transmitter) {
   1664 		l_error("Incomplete HWSIM_CMD_FRAME");
   1665 		l_free(frame);
   1666 		return;
   1667 	}
   1668 
   1669 	frame->signal = -30;
   1670 	frame->msg = l_genl_msg_ref(msg);
   1671 	frame->refcount = 1;
   1672 
   1673 	frame->src_radio = l_queue_find(radio_info, radio_info_match_addr1,
   1674 					transmitter);
   1675 	if (!frame->src_radio) {
   1676 		l_error("Unknown transmitter address %s, probably need to "
   1677 			"update radio dump code for this kernel",
   1678 			util_address_to_string(transmitter));
   1679 		hwsim_frame_unref(frame);
   1680 		return;
   1681 	}
   1682 
   1683 	frame->frequency = *(uint32_t *) freq;
   1684 	frame->flags = *(uint32_t *) flags;
   1685 
   1686 	mpdu = (const struct mmpdu_header *) frame->payload;
   1687 
   1688 	memcpy(frame->src_ether_addr, mpdu->address_2, ETH_ALEN);
   1689 	memcpy(frame->dst_ether_addr, mpdu->address_1, ETH_ALEN);
   1690 
   1691 	process_frame(frame);
   1692 }
   1693 
   1694 static bool get_tx_rx_addrs(struct l_genl_msg *msg, const uint8_t **tx_out,
   1695 				const uint8_t **rx_out)
   1696 {
   1697 	struct l_genl_attr attr;
   1698 	uint16_t type, len;
   1699 	const void *data;
   1700 	const uint8_t *tx = NULL, *rx = NULL;
   1701 
   1702 	if (!l_genl_attr_init(&attr, msg))
   1703 		return false;
   1704 
   1705 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   1706 		switch (type) {
   1707 		case HWSIM_ATTR_ADDR_TRANSMITTER:
   1708 			if (len != ETH_ALEN)
   1709 				return false;
   1710 
   1711 			tx = data;
   1712 			break;
   1713 		case HWSIM_ATTR_ADDR_RECEIVER:
   1714 			if (len != ETH_ALEN)
   1715 				return false;
   1716 
   1717 			rx = data;
   1718 			break;
   1719 		default:
   1720 			break;
   1721 		}
   1722 	}
   1723 
   1724 	if (!tx || !rx)
   1725 		return false;
   1726 
   1727 	*tx_out = tx;
   1728 	*rx_out = rx;
   1729 
   1730 	return true;
   1731 }
   1732 
   1733 static void hwsim_add_mac_event(struct l_genl_msg *msg)
   1734 {
   1735 	const uint8_t *tx = NULL, *rx = NULL;
   1736 	struct radio_info_rec *radio_rec;
   1737 	struct interface_info_rec *interface_rec;
   1738 
   1739 	if (!get_tx_rx_addrs(msg, &tx, &rx))
   1740 		return;
   1741 
   1742 	/* No radio matches the TX address, hwsim must not have created it */
   1743 	radio_rec = l_queue_find(radio_info, radio_info_match_addr1, tx);
   1744 	if (!radio_rec)
   1745 		return;
   1746 
   1747 	interface_rec = l_queue_find(interface_info,
   1748 					interface_info_match_addr, rx);
   1749 	if (interface_rec) {
   1750 		/* Existing interface, address changes handled via nl80211 */
   1751 		if (interface_rec->name)
   1752 			return;
   1753 
   1754 		/*
   1755 		 * Transient/dummy interface we already know about. This likely
   1756 		 * was created, then a scan changed the address temporarily.
   1757 		 * Reflect this change and increment the ref so the following
   1758 		 * DEL event doesn't destroy it
   1759 		 */
   1760 		__atomic_fetch_add(&interface_rec->ref, 1, __ATOMIC_SEQ_CST);
   1761 		return;
   1762 	}
   1763 
   1764 	/*
   1765 	 * Create a dummy interface entry for this address that only contains
   1766 	 * the radio and address. This is either a transient entry due to scan
   1767 	 * randomization or an interface created outside this namespace.
   1768 	 */
   1769 	interface_rec = l_new(struct interface_info_rec, 1);
   1770 	interface_rec->radio_rec = radio_rec;
   1771 	interface_rec->ref = 1;
   1772 	memcpy(interface_rec->addr, rx, ETH_ALEN);
   1773 
   1774 	l_queue_push_tail(interface_info, interface_rec);
   1775 }
   1776 
   1777 static void hwsim_del_mac_event(struct l_genl_msg *msg)
   1778 {
   1779 	const uint8_t *tx = NULL, *rx = NULL;
   1780 	struct radio_info_rec *radio_rec;
   1781 	struct interface_info_rec *interface_rec;
   1782 
   1783 	if (!get_tx_rx_addrs(msg, &tx, &rx))
   1784 		return;
   1785 
   1786 	/* No radio matches the TX address, hwsim must not have created it */
   1787 	radio_rec = l_queue_find(radio_info, radio_info_match_addr1, tx);
   1788 	if (!radio_rec)
   1789 		return;
   1790 
   1791 	interface_rec = l_queue_find(interface_info,
   1792 						interface_info_match_addr, rx);
   1793 	if (!interface_rec)
   1794 		return;
   1795 
   1796 	/*
   1797 	 * This change is handled via nl80211 so we don't want to touch this
   1798 	 * interface here.
   1799 	 */
   1800 	if (interface_rec->name)
   1801 		return;
   1802 
   1803 	if (__atomic_sub_fetch(&interface_rec->ref, 1, __ATOMIC_SEQ_CST))
   1804 		return;
   1805 
   1806 	l_queue_remove(interface_info, interface_rec);
   1807 	l_free(interface_rec);
   1808 }
   1809 
   1810 static void hwsim_unicast_handler(struct l_genl_msg *msg, void *user_data)
   1811 {
   1812 	uint8_t cmd;
   1813 
   1814 	if (l_genl_msg_get_error(msg) < 0)
   1815 		return;
   1816 
   1817 	cmd = l_genl_msg_get_command(msg);
   1818 
   1819 	switch (cmd) {
   1820 	case HWSIM_CMD_FRAME:
   1821 		hwsim_frame_event(msg);
   1822 		break;
   1823 	case HWSIM_CMD_ADD_MAC_ADDR:
   1824 		hwsim_add_mac_event(msg);
   1825 		break;
   1826 	case HWSIM_CMD_DEL_MAC_ADDR:
   1827 		hwsim_del_mac_event(msg);
   1828 		break;
   1829 	default:
   1830 		break;
   1831 	}
   1832 }
   1833 
   1834 static void radio_manager_create_callback(struct l_genl_msg *msg,
   1835 						void *user_data)
   1836 {
   1837 	struct radio_info_rec *radio = user_data;
   1838 	struct l_dbus_message *reply;
   1839 
   1840 	radio->cmd_id = 0;
   1841 
   1842 	if (l_genl_msg_get_error(msg) >= 0)
   1843 		return;
   1844 
   1845 	/*
   1846 	 * In theory pending should always be set. This is to handle the
   1847 	 * NEW_RADIO event coming prior to this callback and this callback
   1848 	 * also having an error. It doesn't seem possible for this to happen,
   1849 	 * but who knows.
   1850 	 */
   1851 	if (radio->pending) {
   1852 		reply = dbus_error_failed(radio->pending);
   1853 		dbus_pending_reply(&radio->pending, reply);
   1854 	}
   1855 
   1856 	l_queue_remove(radio_info, radio);
   1857 	radio_free(radio);
   1858 }
   1859 
   1860 static struct l_dbus_message *radio_manager_create(struct l_dbus *dbus,
   1861 					struct l_dbus_message *message,
   1862 					void *user_data)
   1863 {
   1864 	struct l_genl_msg *new_msg;
   1865 	struct l_dbus_message_iter dict;
   1866 	struct l_dbus_message_iter variant;
   1867 	const char *key;
   1868 	const char *name = NULL;
   1869 	bool p2p = false;
   1870 	const char *disabled_iftypes = NULL;
   1871 	const char *disabled_ciphers = NULL;
   1872 	bool no_vif = false;
   1873 	struct radio_info_rec *radio;
   1874 
   1875 	if (!l_dbus_message_get_arguments(message, "a{sv}", &dict))
   1876 		goto invalid;
   1877 
   1878 	while (l_dbus_message_iter_next_entry(&dict, &key, &variant)) {
   1879 		bool ret = false;
   1880 
   1881 		if (!strcmp(key, "Name"))
   1882 			ret = l_dbus_message_iter_get_variant(&variant,
   1883 								"s", &name);
   1884 		else if (!strcmp(key, "P2P"))
   1885 			ret = l_dbus_message_iter_get_variant(&variant,
   1886 								"b", &p2p);
   1887 		else if (!strcmp(key, "InterfaceTypeDisable"))
   1888 			ret = l_dbus_message_iter_get_variant(&variant, "s",
   1889 							&disabled_iftypes);
   1890 		else if (!strcmp(key, "CipherTypeDisable"))
   1891 			ret = l_dbus_message_iter_get_variant(&variant, "s",
   1892 							&disabled_ciphers);
   1893 		else if (!strcmp(key, "NoVirtualInterface"))
   1894 			ret = l_dbus_message_iter_get_variant(&variant, "b",
   1895 							&no_vif);
   1896 
   1897 		if (!ret)
   1898 			goto invalid;
   1899 	}
   1900 
   1901 	if (!name)
   1902 		goto invalid;
   1903 
   1904 	new_msg = l_genl_msg_new(HWSIM_CMD_NEW_RADIO);
   1905 	l_genl_msg_append_attr(new_msg, HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE,
   1906 				0, NULL);
   1907 
   1908 	l_genl_msg_append_attr(new_msg, HWSIM_ATTR_RADIO_NAME,
   1909 				strlen(name) + 1, name);
   1910 
   1911 	if (p2p)
   1912 		l_genl_msg_append_attr(new_msg, HWSIM_ATTR_SUPPORT_P2P_DEVICE,
   1913 					0, NULL);
   1914 
   1915 	if (disabled_iftypes) {
   1916 		hwsim_disable_support(disabled_iftypes, iftype_map,
   1917 						&hwsim_iftypes);
   1918 
   1919 		if (hwsim_iftypes != HWSIM_DEFAULT_IFTYPES)
   1920 			l_genl_msg_append_attr(new_msg,
   1921 						HWSIM_ATTR_IFTYPE_SUPPORT,
   1922 						4, &hwsim_iftypes);
   1923 	}
   1924 
   1925 	if (disabled_ciphers) {
   1926 		hwsim_disable_ciphers(disabled_ciphers);
   1927 
   1928 		if (hwsim_num_ciphers)
   1929 			l_genl_msg_append_attr(new_msg, HWSIM_ATTR_CIPHER_SUPPORT,
   1930 					sizeof(uint32_t) * hwsim_num_ciphers,
   1931 					hwsim_ciphers);
   1932 
   1933 	}
   1934 
   1935 	if (no_vif)
   1936 		l_genl_msg_append_attr(new_msg, HWSIM_ATTR_NO_VIF, 0, NULL);
   1937 
   1938 	radio = l_new(struct radio_info_rec, 1);
   1939 	radio->pending = l_dbus_message_ref(message);
   1940 	radio->name = l_strdup(name);
   1941 	radio->id = -1;
   1942 
   1943 	if (!radio_info)
   1944 		radio_info = l_queue_new();
   1945 
   1946 	l_queue_push_tail(radio_info, radio);
   1947 
   1948 	radio->cmd_id = l_genl_family_send(hwsim, new_msg,
   1949 						radio_manager_create_callback,
   1950 						radio, NULL);
   1951 
   1952 	return NULL;
   1953 
   1954 invalid:
   1955 	return dbus_error_invalid_args(message);
   1956 }
   1957 
   1958 static void setup_radio_manager_interface(struct l_dbus_interface *interface)
   1959 {
   1960 	l_dbus_interface_method(interface, "CreateRadio", 0,
   1961 				radio_manager_create, "o", "a{sv}",
   1962 				"path", "name", "p2p_device");
   1963 }
   1964 
   1965 static void radio_destroy_callback(struct l_genl_msg *msg, void *user_data)
   1966 {
   1967 	struct l_dbus_message *message = user_data;
   1968 	struct l_dbus_message *reply;
   1969 	struct l_genl_attr attr;
   1970 	int err;
   1971 
   1972 	if (l_genl_attr_init(&attr, msg))
   1973 		goto error;
   1974 
   1975 	err = l_genl_msg_get_error(msg);
   1976 	if (err < 0)
   1977 		goto error;
   1978 
   1979 	reply = l_dbus_message_new_method_return(message);
   1980 	l_dbus_message_set_arguments(reply, "");
   1981 	dbus_pending_reply(&message, reply);
   1982 
   1983 	return;
   1984 
   1985 error:
   1986 	reply = dbus_error_failed(message);
   1987 	dbus_pending_reply(&message, reply);
   1988 }
   1989 
   1990 static struct l_dbus_message *radio_destroy(struct l_dbus *dbus,
   1991 					struct l_dbus_message *message,
   1992 					void *user_data)
   1993 {
   1994 	struct l_genl_msg *del_msg;
   1995 	struct radio_info_rec *radio = user_data;
   1996 
   1997 	del_msg = l_genl_msg_new_sized(HWSIM_CMD_DEL_RADIO, 8);
   1998 
   1999 	l_genl_msg_append_attr(del_msg, HWSIM_ATTR_RADIO_ID, 4, &radio->id);
   2000 
   2001 	l_genl_family_send(hwsim, del_msg, radio_destroy_callback,
   2002 				l_dbus_message_ref(message), NULL);
   2003 
   2004 	return NULL;
   2005 }
   2006 
   2007 static bool radio_property_get_name(struct l_dbus *dbus,
   2008 					struct l_dbus_message *message,
   2009 					struct l_dbus_message_builder *builder,
   2010 					void *user_data)
   2011 {
   2012 	const struct radio_info_rec *rec = user_data;
   2013 
   2014 	l_dbus_message_builder_append_basic(builder, 's', rec->name);
   2015 
   2016 	return true;
   2017 }
   2018 
   2019 static bool radio_property_get_addresses(struct l_dbus *dbus,
   2020 					struct l_dbus_message *message,
   2021 					struct l_dbus_message_builder *builder,
   2022 					void *user_data)
   2023 {
   2024 	const struct radio_info_rec *rec = user_data;
   2025 	unsigned int i;
   2026 
   2027 	l_dbus_message_builder_enter_array(builder, "s");
   2028 
   2029 	for (i = 0; i < sizeof(rec->addrs) / ETH_ALEN; i++) {
   2030 		const char *str = util_address_to_string(rec->addrs[i]);
   2031 
   2032 		l_dbus_message_builder_append_basic(builder, 's', str);
   2033 	}
   2034 
   2035 	l_dbus_message_builder_leave_array(builder);
   2036 
   2037 	return true;
   2038 }
   2039 
   2040 static bool radio_property_get_channels(struct l_dbus *dbus,
   2041 					struct l_dbus_message *message,
   2042 					struct l_dbus_message_builder *builder,
   2043 					void *user_data)
   2044 {
   2045 	const struct radio_info_rec *rec = user_data;
   2046 	uint16_t val = rec->channels;
   2047 
   2048 	l_dbus_message_builder_append_basic(builder, 'q', &val);
   2049 
   2050 	return true;
   2051 }
   2052 
   2053 static bool radio_property_get_alpha2(struct l_dbus *dbus,
   2054 					struct l_dbus_message *message,
   2055 					struct l_dbus_message_builder *builder,
   2056 					void *user_data)
   2057 {
   2058 	const struct radio_info_rec *rec = user_data;
   2059 
   2060 	if (rec->alpha2[0] == 0 || rec->alpha2[1] == 0)
   2061 		return false;
   2062 
   2063 	l_dbus_message_builder_enter_struct(builder, "yy");
   2064 	l_dbus_message_builder_append_basic(builder, 'y', &rec->alpha2[0]);
   2065 	l_dbus_message_builder_append_basic(builder, 'y', &rec->alpha2[1]);
   2066 	l_dbus_message_builder_leave_struct(builder);
   2067 
   2068 	return true;
   2069 }
   2070 
   2071 static bool radio_property_get_p2p(struct l_dbus *dbus,
   2072 					struct l_dbus_message *message,
   2073 					struct l_dbus_message_builder *builder,
   2074 					void *user_data)
   2075 {
   2076 	const struct radio_info_rec *rec = user_data;
   2077 	bool val = rec->p2p;
   2078 
   2079 	l_dbus_message_builder_append_basic(builder, 'b', &val);
   2080 
   2081 	return true;
   2082 }
   2083 
   2084 static bool radio_property_get_regdom(struct l_dbus *dbus,
   2085 					struct l_dbus_message *message,
   2086 					struct l_dbus_message_builder *builder,
   2087 					void *user_data)
   2088 {
   2089 	const struct radio_info_rec *rec = user_data;
   2090 
   2091 	if (!rec->custom_regdom)
   2092 		return false;
   2093 
   2094 	l_dbus_message_builder_append_basic(builder, 'u', &rec->regdom_idx);
   2095 
   2096 	return true;
   2097 }
   2098 
   2099 static void setup_radio_interface(struct l_dbus_interface *interface)
   2100 {
   2101 	l_dbus_interface_method(interface, "Destroy", 0, radio_destroy, "", "");
   2102 
   2103 	l_dbus_interface_property(interface, "Name", 0, "s",
   2104 					radio_property_get_name, NULL);
   2105 	l_dbus_interface_property(interface, "Addresses", 0, "as",
   2106 					radio_property_get_addresses, NULL);
   2107 	l_dbus_interface_property(interface, "Channels", 0, "q",
   2108 					radio_property_get_channels, NULL);
   2109 	l_dbus_interface_property(interface, "Alpha2", 0, "(yy)",
   2110 					radio_property_get_alpha2, NULL);
   2111 	l_dbus_interface_property(interface, "P2PDevice", 0, "b",
   2112 					radio_property_get_p2p, NULL);
   2113 	l_dbus_interface_property(interface, "RegulatoryDomainIndex", 0, "u",
   2114 					radio_property_get_regdom, NULL);
   2115 }
   2116 
   2117 static struct l_dbus_message *interface_send_frame(struct l_dbus *dbus,
   2118 		struct l_dbus_message *message,
   2119 		void *user_data)
   2120 {
   2121 	struct l_dbus_message_iter addr;
   2122 	struct l_dbus_message_iter data;
   2123 	const void *frame;
   2124 	const uint8_t *receiver;
   2125 	uint32_t len;
   2126 	uint32_t freq;
   2127 	int32_t signal;
   2128 
   2129 	if (!l_dbus_message_get_arguments(message, "ayuiay", &addr, &freq,
   2130 			&signal, &data))
   2131 		goto invalid_args;
   2132 
   2133 	if (!l_dbus_message_iter_get_fixed_array(&addr,
   2134 			(const void **)&receiver, &len))
   2135 		goto invalid_args;
   2136 
   2137 	if (len != 6)
   2138 		goto invalid_args;
   2139 
   2140 	if (!l_dbus_message_iter_get_fixed_array(&data, &frame, &len))
   2141 		goto invalid_args;
   2142 
   2143 	if (!send_custom_frame(receiver, freq, signal, frame, len,
   2144 			l_dbus_message_ref(message))) {
   2145 		l_dbus_message_unref(message);
   2146 		goto invalid_args;
   2147 	}
   2148 
   2149 	return NULL;
   2150 
   2151 invalid_args:
   2152 	return dbus_error_invalid_args(message);
   2153 }
   2154 
   2155 static bool interface_property_get_name(struct l_dbus *dbus,
   2156 					struct l_dbus_message *message,
   2157 					struct l_dbus_message_builder *builder,
   2158 					void *user_data)
   2159 {
   2160 	const struct interface_info_rec *rec = user_data;
   2161 
   2162 	l_dbus_message_builder_append_basic(builder, 's', rec->name);
   2163 
   2164 	return true;
   2165 }
   2166 
   2167 static bool interface_property_get_address(struct l_dbus *dbus,
   2168 					struct l_dbus_message *message,
   2169 					struct l_dbus_message_builder *builder,
   2170 					void *user_data)
   2171 {
   2172 	const struct interface_info_rec *rec = user_data;
   2173 	const char *str = util_address_to_string(rec->addr);
   2174 
   2175 	l_dbus_message_builder_append_basic(builder, 's', str);
   2176 
   2177 	return true;
   2178 }
   2179 
   2180 static void setup_interface_interface(struct l_dbus_interface *interface)
   2181 {
   2182 	l_dbus_interface_method(interface, "SendFrame", 0,
   2183 			interface_send_frame, "", "ayuiay", "station",
   2184 			"frequency", "signal", "frame");
   2185 
   2186 	l_dbus_interface_property(interface, "Name", 0, "s",
   2187 					interface_property_get_name, NULL);
   2188 	l_dbus_interface_property(interface, "Address", 0, "s",
   2189 					interface_property_get_address, NULL);
   2190 }
   2191 
   2192 static int rule_compare_priority(const void *a, const void *b, void *user)
   2193 {
   2194 	const struct hwsim_rule *rule_a = a;
   2195 	const struct hwsim_rule *rule_b = b;
   2196 
   2197 	return (rule_a->priority > rule_b->priority) ? 1 : -1;
   2198 }
   2199 
   2200 static struct l_dbus_message *rule_add(struct l_dbus *dbus,
   2201 					struct l_dbus_message *message,
   2202 					void *user_data)
   2203 {
   2204 	struct hwsim_rule *rule;
   2205 	const char *path;
   2206 	struct l_dbus_message *reply;
   2207 
   2208 	rule = l_new(struct hwsim_rule, 1);
   2209 	rule->id = next_rule_id++;
   2210 	rule->source_any = true;
   2211 	rule->destination_any = true;
   2212 	rule->delay = 0;
   2213 	rule->enabled = false;
   2214 	rule->match_times = -1;
   2215 	rule->drop_ack = true;
   2216 
   2217 	if (!rules)
   2218 		rules = l_queue_new();
   2219 
   2220 	l_queue_insert(rules, rule, rule_compare_priority, NULL);
   2221 	path = rule_get_path(rule);
   2222 
   2223 	if (!l_dbus_object_add_interface(dbus, path,
   2224 					HWSIM_RULE_INTERFACE, rule))
   2225 		l_info("Unable to add the %s interface to %s",
   2226 				HWSIM_RULE_INTERFACE, path);
   2227 
   2228 	if (!l_dbus_object_add_interface(dbus, path,
   2229 					L_DBUS_INTERFACE_PROPERTIES, NULL))
   2230 		l_info("Unable to add the %s interface to %s",
   2231 				L_DBUS_INTERFACE_PROPERTIES, path);
   2232 
   2233 	reply = l_dbus_message_new_method_return(message);
   2234 	l_dbus_message_set_arguments(reply, "o", path);
   2235 
   2236 	return reply;
   2237 }
   2238 
   2239 static void setup_rule_manager_interface(struct l_dbus_interface *interface)
   2240 {
   2241 	l_dbus_interface_method(interface, "AddRule", 0,
   2242 				rule_add, "o", "", "path");
   2243 }
   2244 
   2245 static void destroy_rule(void *user_data)
   2246 {
   2247 	struct hwsim_rule *rule = user_data;
   2248 
   2249 	if (rule->prefix)
   2250 		l_free(rule->prefix);
   2251 
   2252 	if (rule->match)
   2253 		l_free(rule->match);
   2254 
   2255 	l_free(rule);
   2256 }
   2257 
   2258 static struct l_dbus_message *rule_remove(struct l_dbus *dbus,
   2259 						struct l_dbus_message *message,
   2260 						void *user_data)
   2261 {
   2262 	struct hwsim_rule *rule = user_data;
   2263 	const char *path;
   2264 
   2265 	path = rule_get_path(rule);
   2266 	l_queue_remove(rules, rule);
   2267 
   2268 	destroy_rule(rule);
   2269 
   2270 	l_dbus_unregister_object(dbus, path);
   2271 
   2272 	return l_dbus_message_new_method_return(message);
   2273 }
   2274 
   2275 static bool rule_property_get_source(struct l_dbus *dbus,
   2276 					struct l_dbus_message *message,
   2277 					struct l_dbus_message_builder *builder,
   2278 					void *user_data)
   2279 {
   2280 	struct hwsim_rule *rule = user_data;
   2281 	const char *str;
   2282 
   2283 	if (rule->source_any)
   2284 		str = "any";
   2285 	else
   2286 		str = util_address_to_string(rule->source);
   2287 
   2288 	l_dbus_message_builder_append_basic(builder, 's', str);
   2289 
   2290 	return true;
   2291 }
   2292 
   2293 static struct l_dbus_message *rule_property_set_source(struct l_dbus *dbus,
   2294 					struct l_dbus_message *message,
   2295 					struct l_dbus_message_iter *new_value,
   2296 					l_dbus_property_complete_cb_t complete,
   2297 					void *user_data)
   2298 {
   2299 	struct hwsim_rule *rule = user_data;
   2300 	const char *str;
   2301 
   2302 	if (!l_dbus_message_iter_get_variant(new_value, "s", &str))
   2303 		return dbus_error_invalid_args(message);
   2304 
   2305 	if (!strcmp(str, "any"))
   2306 		rule->source_any = true;
   2307 	else {
   2308 		if (!util_string_to_address(str, rule->source))
   2309 			return dbus_error_invalid_args(message);
   2310 
   2311 		rule->source_any = false;
   2312 	}
   2313 
   2314 	return l_dbus_message_new_method_return(message);
   2315 }
   2316 
   2317 static bool rule_property_get_destination(struct l_dbus *dbus,
   2318 					struct l_dbus_message *message,
   2319 					struct l_dbus_message_builder *builder,
   2320 					void *user_data)
   2321 {
   2322 	struct hwsim_rule *rule = user_data;
   2323 	const char *str;
   2324 
   2325 	if (rule->destination_any)
   2326 		str = "any";
   2327 	else if (util_is_broadcast_address(rule->destination))
   2328 		str = "multicast";
   2329 	else
   2330 		str = util_address_to_string(rule->destination);
   2331 
   2332 	l_dbus_message_builder_append_basic(builder, 's', str);
   2333 
   2334 	return true;
   2335 }
   2336 
   2337 static struct l_dbus_message *rule_property_set_destination(
   2338 					struct l_dbus *dbus,
   2339 					struct l_dbus_message *message,
   2340 					struct l_dbus_message_iter *new_value,
   2341 					l_dbus_property_complete_cb_t complete,
   2342 					void *user_data)
   2343 {
   2344 	struct hwsim_rule *rule = user_data;
   2345 	const char *str;
   2346 
   2347 	if (!l_dbus_message_iter_get_variant(new_value, "s", &str))
   2348 		return dbus_error_invalid_args(message);
   2349 
   2350 	if (!strcmp(str, "any"))
   2351 		rule->destination_any = true;
   2352 	else if (!strcmp(str, "multicast")) {
   2353 		rule->destination[0] = 0x80;
   2354 		rule->destination_any = false;
   2355 	} else {
   2356 		if (!util_string_to_address(str, rule->destination))
   2357 			return dbus_error_invalid_args(message);
   2358 
   2359 		rule->destination_any = false;
   2360 	}
   2361 
   2362 	return l_dbus_message_new_method_return(message);
   2363 }
   2364 
   2365 static bool rule_property_get_bidirectional(struct l_dbus *dbus,
   2366 					struct l_dbus_message *message,
   2367 					struct l_dbus_message_builder *builder,
   2368 					void *user_data)
   2369 {
   2370 	struct hwsim_rule *rule = user_data;
   2371 	bool bval = rule->bidirectional;
   2372 
   2373 	l_dbus_message_builder_append_basic(builder, 'b', &bval);
   2374 
   2375 	return true;
   2376 }
   2377 
   2378 static struct l_dbus_message *rule_property_set_bidirectional(
   2379 					struct l_dbus *dbus,
   2380 					struct l_dbus_message *message,
   2381 					struct l_dbus_message_iter *new_value,
   2382 					l_dbus_property_complete_cb_t complete,
   2383 					void *user_data)
   2384 {
   2385 	struct hwsim_rule *rule = user_data;
   2386 	bool bval;
   2387 
   2388 	if (!l_dbus_message_iter_get_variant(new_value, "b", &bval))
   2389 		return dbus_error_invalid_args(message);
   2390 
   2391 	rule->bidirectional = bval;
   2392 
   2393 	return l_dbus_message_new_method_return(message);
   2394 }
   2395 
   2396 static bool rule_property_get_frequency(struct l_dbus *dbus,
   2397 					struct l_dbus_message *message,
   2398 					struct l_dbus_message_builder *builder,
   2399 					void *user_data)
   2400 {
   2401 	struct hwsim_rule *rule = user_data;
   2402 
   2403 	l_dbus_message_builder_append_basic(builder, 'u', &rule->frequency);
   2404 
   2405 	return true;
   2406 }
   2407 
   2408 static struct l_dbus_message *rule_property_set_frequency(struct l_dbus *dbus,
   2409 					struct l_dbus_message *message,
   2410 					struct l_dbus_message_iter *new_value,
   2411 					l_dbus_property_complete_cb_t complete,
   2412 					void *user_data)
   2413 {
   2414 	struct hwsim_rule *rule = user_data;
   2415 
   2416 	if (!l_dbus_message_iter_get_variant(new_value, "u", &rule->frequency))
   2417 		return dbus_error_invalid_args(message);
   2418 
   2419 	return l_dbus_message_new_method_return(message);
   2420 }
   2421 
   2422 static bool rule_property_get_priority(struct l_dbus *dbus,
   2423 					struct l_dbus_message *message,
   2424 					struct l_dbus_message_builder *builder,
   2425 					void *user_data)
   2426 {
   2427 	struct hwsim_rule *rule = user_data;
   2428 	int16_t intval = rule->priority;
   2429 
   2430 	l_dbus_message_builder_append_basic(builder, 'n', &intval);
   2431 
   2432 	return true;
   2433 }
   2434 
   2435 static struct l_dbus_message *rule_property_set_priority(struct l_dbus *dbus,
   2436 					struct l_dbus_message *message,
   2437 					struct l_dbus_message_iter *new_value,
   2438 					l_dbus_property_complete_cb_t complete,
   2439 					void *user_data)
   2440 {
   2441 	struct hwsim_rule *rule = user_data;
   2442 	int16_t intval;
   2443 
   2444 	if (!l_dbus_message_iter_get_variant(new_value, "n", &intval))
   2445 		return dbus_error_invalid_args(message);
   2446 
   2447 	rule->priority = intval;
   2448 	l_queue_remove(rules, rule);
   2449 	l_queue_insert(rules, rule, rule_compare_priority, NULL);
   2450 
   2451 	return l_dbus_message_new_method_return(message);
   2452 }
   2453 
   2454 static bool rule_property_get_signal(struct l_dbus *dbus,
   2455 					struct l_dbus_message *message,
   2456 					struct l_dbus_message_builder *builder,
   2457 					void *user_data)
   2458 {
   2459 	struct hwsim_rule *rule = user_data;
   2460 	int16_t intval = rule->signal;
   2461 
   2462 	l_dbus_message_builder_append_basic(builder, 'n', &intval);
   2463 
   2464 	return true;
   2465 }
   2466 
   2467 static struct l_dbus_message *rule_property_set_signal(struct l_dbus *dbus,
   2468 					struct l_dbus_message *message,
   2469 					struct l_dbus_message_iter *new_value,
   2470 					l_dbus_property_complete_cb_t complete,
   2471 					void *user_data)
   2472 {
   2473 	struct hwsim_rule *rule = user_data;
   2474 	int16_t intval;
   2475 
   2476 	if (!l_dbus_message_iter_get_variant(new_value, "n", &intval) ||
   2477 			intval > 0 || intval < -10000)
   2478 		return dbus_error_invalid_args(message);
   2479 
   2480 	rule->signal = intval;
   2481 
   2482 	return l_dbus_message_new_method_return(message);
   2483 }
   2484 
   2485 static bool rule_property_get_drop(struct l_dbus *dbus,
   2486 					struct l_dbus_message *message,
   2487 					struct l_dbus_message_builder *builder,
   2488 					void *user_data)
   2489 {
   2490 	struct hwsim_rule *rule = user_data;
   2491 	bool bval = rule->drop;
   2492 
   2493 	l_dbus_message_builder_append_basic(builder, 'b', &bval);
   2494 
   2495 	return true;
   2496 }
   2497 
   2498 static struct l_dbus_message *rule_property_set_drop(
   2499 					struct l_dbus *dbus,
   2500 					struct l_dbus_message *message,
   2501 					struct l_dbus_message_iter *new_value,
   2502 					l_dbus_property_complete_cb_t complete,
   2503 					void *user_data)
   2504 {
   2505 	struct hwsim_rule *rule = user_data;
   2506 	bool bval;
   2507 
   2508 	if (!l_dbus_message_iter_get_variant(new_value, "b", &bval))
   2509 		return dbus_error_invalid_args(message);
   2510 
   2511 	rule->drop = bval;
   2512 
   2513 	return l_dbus_message_new_method_return(message);
   2514 }
   2515 
   2516 static bool rule_property_get_delay(struct l_dbus *dbus,
   2517 					struct l_dbus_message *message,
   2518 					struct l_dbus_message_builder *builder,
   2519 					void *user_data)
   2520 {
   2521 	struct hwsim_rule *rule = user_data;
   2522 
   2523 	l_dbus_message_builder_append_basic(builder, 'u', &rule->delay);
   2524 
   2525 	return true;
   2526 }
   2527 
   2528 static struct l_dbus_message *rule_property_set_delay(
   2529 					struct l_dbus *dbus,
   2530 					struct l_dbus_message *message,
   2531 					struct l_dbus_message_iter *new_value,
   2532 					l_dbus_property_complete_cb_t complete,
   2533 					void *user_data)
   2534 {
   2535 	struct hwsim_rule *rule = user_data;
   2536 	uint32_t val;
   2537 
   2538 	if (!l_dbus_message_iter_get_variant(new_value, "u", &val) ||
   2539 				val < HWSIM_DELAY_MIN_MS)
   2540 		return dbus_error_invalid_args(message);
   2541 
   2542 	rule->delay = val;
   2543 
   2544 	return l_dbus_message_new_method_return(message);
   2545 }
   2546 
   2547 static bool rule_property_get_prefix(struct l_dbus *dbus,
   2548 					struct l_dbus_message *message,
   2549 					struct l_dbus_message_builder *builder,
   2550 					void *user_data)
   2551 {
   2552 	struct hwsim_rule *rule = user_data;
   2553 	size_t i;
   2554 
   2555 	l_dbus_message_builder_enter_array(builder, "y");
   2556 
   2557 	for (i = 0; i < rule->prefix_len; i++)
   2558 		l_dbus_message_builder_append_basic(builder, 'y',
   2559 							rule->prefix + i);
   2560 
   2561 	l_dbus_message_builder_leave_array(builder);
   2562 
   2563 	return true;
   2564 }
   2565 
   2566 static struct l_dbus_message *rule_property_set_prefix(
   2567 					struct l_dbus *dbus,
   2568 					struct l_dbus_message *message,
   2569 					struct l_dbus_message_iter *new_value,
   2570 					l_dbus_property_complete_cb_t complete,
   2571 					void *user_data)
   2572 {
   2573 	struct hwsim_rule *rule = user_data;
   2574 	struct l_dbus_message_iter iter;
   2575 	const uint8_t *prefix;
   2576 	uint32_t len;
   2577 
   2578 	if (!l_dbus_message_iter_get_variant(new_value, "ay", &iter))
   2579 		goto invalid_args;
   2580 
   2581 	if (!l_dbus_message_iter_get_fixed_array(&iter,
   2582 						(const void **)&prefix, &len))
   2583 		goto invalid_args;
   2584 
   2585 	if (len > HWSIM_MAX_PREFIX_LEN)
   2586 		goto invalid_args;
   2587 
   2588 	if (rule->prefix)
   2589 		l_free(rule->prefix);
   2590 
   2591 	rule->prefix = l_memdup(prefix, len);
   2592 	rule->prefix_len = len;
   2593 
   2594 	return l_dbus_message_new_method_return(message);
   2595 
   2596 invalid_args:
   2597 	return dbus_error_invalid_args(message);
   2598 }
   2599 
   2600 static bool rule_property_get_match(struct l_dbus *dbus,
   2601 					struct l_dbus_message *message,
   2602 					struct l_dbus_message_builder *builder,
   2603 					void *user_data)
   2604 {
   2605 	struct hwsim_rule *rule = user_data;
   2606 	size_t i;
   2607 
   2608 	l_dbus_message_builder_enter_array(builder, "y");
   2609 
   2610 	for (i = 0; i < rule->match_len; i++)
   2611 		l_dbus_message_builder_append_basic(builder, 'y',
   2612 							rule->match + i);
   2613 
   2614 	l_dbus_message_builder_leave_array(builder);
   2615 
   2616 	return true;
   2617 }
   2618 
   2619 static struct l_dbus_message *rule_property_set_match(
   2620 					struct l_dbus *dbus,
   2621 					struct l_dbus_message *message,
   2622 					struct l_dbus_message_iter *new_value,
   2623 					l_dbus_property_complete_cb_t complete,
   2624 					void *user_data)
   2625 {
   2626 	struct hwsim_rule *rule = user_data;
   2627 	struct l_dbus_message_iter iter;
   2628 	const uint8_t *match;
   2629 	uint32_t len;
   2630 
   2631 	if (!l_dbus_message_iter_get_variant(new_value, "ay", &iter))
   2632 		goto invalid_args;
   2633 
   2634 	if (!l_dbus_message_iter_get_fixed_array(&iter,
   2635 						(const void **)&match, &len))
   2636 		goto invalid_args;
   2637 
   2638 	if (len > HWSIM_MAX_PREFIX_LEN)
   2639 		goto invalid_args;
   2640 
   2641 	if (rule->match)
   2642 		l_free(rule->match);
   2643 
   2644 	rule->match = l_memdup(match, len);
   2645 	rule->match_len = len;
   2646 
   2647 	return l_dbus_message_new_method_return(message);
   2648 
   2649 invalid_args:
   2650 	return dbus_error_invalid_args(message);
   2651 }
   2652 
   2653 static bool rule_property_get_match_offset(struct l_dbus *dbus,
   2654 					struct l_dbus_message *message,
   2655 					struct l_dbus_message_builder *builder,
   2656 					void *user_data)
   2657 {
   2658 	struct hwsim_rule *rule = user_data;
   2659 	uint16_t val = rule->match_offset;
   2660 
   2661 	l_dbus_message_builder_append_basic(builder, 'q', &val);
   2662 
   2663 	return true;
   2664 }
   2665 
   2666 static struct l_dbus_message *rule_property_set_match_offset(
   2667 					struct l_dbus *dbus,
   2668 					struct l_dbus_message *message,
   2669 					struct l_dbus_message_iter *new_value,
   2670 					l_dbus_property_complete_cb_t complete,
   2671 					void *user_data)
   2672 {
   2673 	struct hwsim_rule *rule = user_data;
   2674 	uint16_t val;
   2675 
   2676 	if (!l_dbus_message_iter_get_variant(new_value, "q", &val))
   2677 		return dbus_error_invalid_args(message);
   2678 
   2679 	rule->match_offset = val;
   2680 
   2681 	return l_dbus_message_new_method_return(message);
   2682 }
   2683 
   2684 static bool rule_property_get_enabled(struct l_dbus *dbus,
   2685 					struct l_dbus_message *message,
   2686 					struct l_dbus_message_builder *builder,
   2687 					void *user_data)
   2688 {
   2689 	struct hwsim_rule *rule = user_data;
   2690 	bool bval = rule->enabled;
   2691 
   2692 	l_dbus_message_builder_append_basic(builder, 'b', &bval);
   2693 
   2694 	return true;
   2695 }
   2696 
   2697 static struct l_dbus_message *rule_property_set_enabled(
   2698 					struct l_dbus *dbus,
   2699 					struct l_dbus_message *message,
   2700 					struct l_dbus_message_iter *new_value,
   2701 					l_dbus_property_complete_cb_t complete,
   2702 					void *user_data)
   2703 {
   2704 	struct hwsim_rule *rule = user_data;
   2705 	bool bval;
   2706 
   2707 	if (!l_dbus_message_iter_get_variant(new_value, "b", &bval))
   2708 		return dbus_error_invalid_args(message);
   2709 
   2710 	rule->enabled = bval;
   2711 
   2712 	return l_dbus_message_new_method_return(message);
   2713 }
   2714 
   2715 static bool rule_property_get_match_times(struct l_dbus *dbus,
   2716 					struct l_dbus_message *message,
   2717 					struct l_dbus_message_builder *builder,
   2718 					void *user_data)
   2719 {
   2720 	struct hwsim_rule *rule = user_data;
   2721 	uint16_t val = rule->match_times;
   2722 
   2723 	l_dbus_message_builder_append_basic(builder, 'q', &val);
   2724 
   2725 	return true;
   2726 }
   2727 
   2728 static struct l_dbus_message *rule_property_set_match_times(
   2729 					struct l_dbus *dbus,
   2730 					struct l_dbus_message *message,
   2731 					struct l_dbus_message_iter *new_value,
   2732 					l_dbus_property_complete_cb_t complete,
   2733 					void *user_data)
   2734 {
   2735 	struct hwsim_rule *rule = user_data;
   2736 	uint16_t val;
   2737 
   2738 	if (!l_dbus_message_iter_get_variant(new_value, "q", &val))
   2739 		return dbus_error_invalid_args(message);
   2740 
   2741 	rule->match_times = val;
   2742 
   2743 	return l_dbus_message_new_method_return(message);
   2744 }
   2745 
   2746 static bool rule_property_get_drop_ack(struct l_dbus *dbus,
   2747 					struct l_dbus_message *message,
   2748 					struct l_dbus_message_builder *builder,
   2749 					void *user_data)
   2750 {
   2751 	struct hwsim_rule *rule = user_data;
   2752 	bool bval = rule->drop_ack;
   2753 
   2754 	l_dbus_message_builder_append_basic(builder, 'b', &bval);
   2755 
   2756 	return true;
   2757 }
   2758 
   2759 static struct l_dbus_message *rule_property_set_drop_ack(
   2760 					struct l_dbus *dbus,
   2761 					struct l_dbus_message *message,
   2762 					struct l_dbus_message_iter *new_value,
   2763 					l_dbus_property_complete_cb_t complete,
   2764 					void *user_data)
   2765 {
   2766 	struct hwsim_rule *rule = user_data;
   2767 	bool bval;
   2768 
   2769 	if (!l_dbus_message_iter_get_variant(new_value, "b", &bval))
   2770 		return dbus_error_invalid_args(message);
   2771 
   2772 	rule->drop_ack = bval;
   2773 
   2774 	return l_dbus_message_new_method_return(message);
   2775 }
   2776 
   2777 static void setup_rule_interface(struct l_dbus_interface *interface)
   2778 {
   2779 	l_dbus_interface_method(interface, "Remove", 0, rule_remove, "", "");
   2780 
   2781 	l_dbus_interface_property(interface, "Source",
   2782 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "s",
   2783 					rule_property_get_source,
   2784 					rule_property_set_source);
   2785 	l_dbus_interface_property(interface, "Destination",
   2786 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "s",
   2787 					rule_property_get_destination,
   2788 					rule_property_set_destination);
   2789 	l_dbus_interface_property(interface, "Bidirectional",
   2790 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "b",
   2791 					rule_property_get_bidirectional,
   2792 					rule_property_set_bidirectional);
   2793 	l_dbus_interface_property(interface, "Frequency",
   2794 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "u",
   2795 					rule_property_get_frequency,
   2796 					rule_property_set_frequency);
   2797 	l_dbus_interface_property(interface, "Priority",
   2798 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "n",
   2799 					rule_property_get_priority,
   2800 					rule_property_set_priority);
   2801 	l_dbus_interface_property(interface, "SignalStrength",
   2802 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "n",
   2803 					rule_property_get_signal,
   2804 					rule_property_set_signal);
   2805 	l_dbus_interface_property(interface, "Drop",
   2806 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "b",
   2807 					rule_property_get_drop,
   2808 					rule_property_set_drop);
   2809 	l_dbus_interface_property(interface, "Delay",
   2810 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "u",
   2811 					rule_property_get_delay,
   2812 					rule_property_set_delay);
   2813 	l_dbus_interface_property(interface, "Prefix",
   2814 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "ay",
   2815 					rule_property_get_prefix,
   2816 					rule_property_set_prefix);
   2817 	l_dbus_interface_property(interface, "MatchBytes",
   2818 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "ay",
   2819 					rule_property_get_match,
   2820 					rule_property_set_match);
   2821 	l_dbus_interface_property(interface, "MatchBytesOffset",
   2822 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "q",
   2823 					rule_property_get_match_offset,
   2824 					rule_property_set_match_offset);
   2825 	l_dbus_interface_property(interface, "Enabled",
   2826 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "b",
   2827 					rule_property_get_enabled,
   2828 					rule_property_set_enabled);
   2829 	l_dbus_interface_property(interface, "MatchTimes",
   2830 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "q",
   2831 					rule_property_get_match_times,
   2832 					rule_property_set_match_times);
   2833 	l_dbus_interface_property(interface, "DropAck",
   2834 					L_DBUS_PROPERTY_FLAG_AUTO_EMIT, "b",
   2835 					rule_property_get_drop_ack,
   2836 					rule_property_set_drop_ack);
   2837 }
   2838 
   2839 static void request_name_callback(struct l_dbus *dbus, bool success,
   2840 					bool queued, void *user_data)
   2841 {
   2842 	if (!success)
   2843 		l_error("Name request failed");
   2844 }
   2845 
   2846 static void ready_callback(void *user_data)
   2847 {
   2848 	l_dbus_name_acquire(dbus, HWSIM_SERVICE, false, false, true,
   2849 				request_name_callback, NULL);
   2850 
   2851 	if (!l_dbus_object_manager_enable(dbus, "/"))
   2852 		l_info("Unable to register the ObjectManager");
   2853 }
   2854 
   2855 static void disconnect_callback(void *user_data)
   2856 {
   2857 	l_info("D-Bus disconnected, quitting...");
   2858 	l_main_quit();
   2859 }
   2860 
   2861 static bool setup_dbus_hwsim(void)
   2862 {
   2863 	dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
   2864 	if (!dbus) {
   2865 		l_error("Unable to connect to Dbus");
   2866 		return false;
   2867 	}
   2868 
   2869 	if (!l_dbus_register_interface(dbus, HWSIM_RADIO_MANAGER_INTERFACE,
   2870 					setup_radio_manager_interface,
   2871 					NULL, false)) {
   2872 		l_error("Unable to register the %s interface",
   2873 			HWSIM_RADIO_MANAGER_INTERFACE);
   2874 		return false;
   2875 	}
   2876 
   2877 	if (!l_dbus_register_interface(dbus, HWSIM_RADIO_INTERFACE,
   2878 					setup_radio_interface, NULL, false)) {
   2879 		l_error("Unable to register the %s interface",
   2880 			HWSIM_RADIO_INTERFACE);
   2881 		return false;
   2882 	}
   2883 
   2884 	if (!l_dbus_register_interface(dbus, HWSIM_INTERFACE_INTERFACE,
   2885 					setup_interface_interface,
   2886 					NULL, false)) {
   2887 		l_error("Unable to register the %s interface",
   2888 			HWSIM_INTERFACE_INTERFACE);
   2889 		return false;
   2890 	}
   2891 
   2892 	if (!l_dbus_register_interface(dbus, HWSIM_RULE_MANAGER_INTERFACE,
   2893 					setup_rule_manager_interface,
   2894 					NULL, false)) {
   2895 		l_error("Unable to register the %s interface",
   2896 			HWSIM_RULE_MANAGER_INTERFACE);
   2897 		return false;
   2898 	}
   2899 
   2900 	if (!l_dbus_register_interface(dbus, HWSIM_RULE_INTERFACE,
   2901 					setup_rule_interface, NULL, false)) {
   2902 		l_error("Unable to register the %s interface",
   2903 			HWSIM_RULE_INTERFACE);
   2904 		return false;
   2905 	}
   2906 
   2907 	if (!l_dbus_object_add_interface(dbus, "/",
   2908 						HWSIM_RADIO_MANAGER_INTERFACE,
   2909 						NULL)) {
   2910 		l_info("Unable to add the %s interface to /",
   2911 			HWSIM_RADIO_MANAGER_INTERFACE);
   2912 		return false;
   2913 	}
   2914 
   2915 	if (!l_dbus_object_add_interface(dbus, "/",
   2916 						HWSIM_RULE_MANAGER_INTERFACE,
   2917 						NULL)) {
   2918 		l_info("Unable to add the %s interface to /",
   2919 			HWSIM_RULE_MANAGER_INTERFACE);
   2920 		return false;
   2921 	}
   2922 
   2923 	l_dbus_set_ready_handler(dbus, ready_callback, dbus, NULL);
   2924 	l_dbus_set_disconnect_handler(dbus, disconnect_callback, NULL, NULL);
   2925 
   2926 	return true;
   2927 }
   2928 
   2929 static void register_callback(struct l_genl_msg *msg, void *user_data)
   2930 {
   2931 	int err = l_genl_msg_get_error(msg);
   2932 
   2933 	if (err < 0) {
   2934 		l_error("HWSIM_CMD_REGISTER failed: %s (%d)",
   2935 			strerror(-err), -err);
   2936 
   2937 		exit_status = EXIT_FAILURE;
   2938 		l_main_quit();
   2939 		return;
   2940 	}
   2941 
   2942 	l_info("Registered as a transmission medium");
   2943 }
   2944 
   2945 static void get_interface_done_initial(void *user_data)
   2946 {
   2947 	struct l_genl_msg *msg;
   2948 
   2949 	if (no_register)
   2950 		return;
   2951 
   2952 	msg = l_genl_msg_new_sized(HWSIM_CMD_REGISTER, 4);
   2953 	l_genl_family_send(hwsim, msg, register_callback, NULL, NULL);
   2954 }
   2955 
   2956 static void get_radio_done_initial(void *user_data)
   2957 {
   2958 	struct l_genl_msg *msg;
   2959 
   2960 	/*
   2961 	 * Query interfaces now that we know we have all the radio data
   2962 	 * for radio lookups inside get_interface_callback, and we know
   2963 	 * nl80211_ready has already been called.
   2964 	 */
   2965 	msg = l_genl_msg_new(NL80211_CMD_GET_INTERFACE);
   2966 	if (!l_genl_family_dump(nl80211, msg, get_interface_callback,
   2967 				NULL, get_interface_done_initial)) {
   2968 		l_error("Getting nl80211 interface information failed");
   2969 		goto error;
   2970 	}
   2971 
   2972 	if (!l_genl_family_register(nl80211, "config", nl80211_config_notify,
   2973 					NULL, NULL)) {
   2974 		l_error("Registering for nl80211 config notification "
   2975 			"failed");
   2976 		goto error;
   2977 	}
   2978 
   2979 	rtnl = l_netlink_new(NETLINK_ROUTE);
   2980 	if (!rtnl) {
   2981 		l_error("Failed to open route netlink socket");
   2982 		goto error;
   2983 	}
   2984 
   2985 	if (!l_netlink_register(rtnl, RTNLGRP_LINK,
   2986 				rtnl_link_notify, NULL, NULL)) {
   2987 		l_error("Failed to register for RTNL link notifications");
   2988 		goto error;
   2989 	}
   2990 
   2991 	return;
   2992 
   2993 error:
   2994 	exit_status = EXIT_FAILURE;
   2995 	l_main_quit();
   2996 }
   2997 
   2998 static void hwsim_ready(void)
   2999 {
   3000 	struct l_genl_msg *msg;
   3001 	size_t msg_size;
   3002 	uint32_t radio_id;
   3003 
   3004 	switch (action) {
   3005 	case ACTION_LIST:
   3006 		msg = l_genl_msg_new_sized(HWSIM_CMD_GET_RADIO,
   3007 					options ? 8 : 4);
   3008 
   3009 		if (options) {
   3010 			radio_id = atoi(options);
   3011 
   3012 			l_genl_msg_append_attr(msg, HWSIM_ATTR_RADIO_ID,
   3013 					4, &radio_id);
   3014 			l_genl_family_send(hwsim, msg, list_callback,
   3015 						NULL, list_callback_done);
   3016 		} else {
   3017 			l_genl_family_dump(hwsim, msg, list_callback,
   3018 						NULL, list_callback_done);
   3019 		}
   3020 
   3021 		break;
   3022 
   3023 	case ACTION_CREATE:
   3024 		msg_size = 0;
   3025 
   3026 		if (radio_name_attr)
   3027 			msg_size += strlen(radio_name_attr) + 8;
   3028 
   3029 		if (no_vif_attr)
   3030 			msg_size += 4;
   3031 
   3032 		if (p2p_attr)
   3033 			msg_size += 4;
   3034 
   3035 		msg = l_genl_msg_new_sized(HWSIM_CMD_NEW_RADIO, msg_size);
   3036 
   3037 		if (radio_name_attr)
   3038 			l_genl_msg_append_attr(msg, HWSIM_ATTR_RADIO_NAME,
   3039 						strlen(radio_name_attr) + 1,
   3040 							radio_name_attr);
   3041 
   3042 		if (no_vif_attr)
   3043 			l_genl_msg_append_attr(msg, HWSIM_ATTR_NO_VIF, 0, NULL);
   3044 
   3045 		if (p2p_attr)
   3046 			l_genl_msg_append_attr(msg,
   3047 						HWSIM_ATTR_SUPPORT_P2P_DEVICE,
   3048 						0, NULL);
   3049 
   3050 		if (hwsim_iftypes != HWSIM_DEFAULT_IFTYPES)
   3051 			l_genl_msg_append_attr(msg, HWSIM_ATTR_IFTYPE_SUPPORT,
   3052 						4, &hwsim_iftypes);
   3053 
   3054 		if (hwsim_num_ciphers)
   3055 			l_genl_msg_append_attr(msg, HWSIM_ATTR_CIPHER_SUPPORT,
   3056 					sizeof(uint32_t) * hwsim_num_ciphers,
   3057 					hwsim_ciphers);
   3058 
   3059 		l_genl_family_send(hwsim, msg, create_callback, NULL, NULL);
   3060 
   3061 		break;
   3062 
   3063 	case ACTION_DESTROY:
   3064 		radio_id = atoi(options);
   3065 
   3066 		msg = l_genl_msg_new_sized(HWSIM_CMD_DEL_RADIO, 8);
   3067 		l_genl_msg_append_attr(msg, HWSIM_ATTR_RADIO_ID, 4, &radio_id);
   3068 		l_genl_family_send(hwsim, msg, destroy_callback, NULL, NULL);
   3069 
   3070 		break;
   3071 
   3072 	case ACTION_NONE:
   3073 		if (!setup_dbus_hwsim())
   3074 			goto error;
   3075 
   3076 		if (!l_genl_family_register(hwsim, "config", hwsim_config,
   3077 						NULL, NULL)) {
   3078 			l_error("Failed to create hwsim config listener\n");
   3079 			goto error;
   3080 		}
   3081 
   3082 		msg = l_genl_msg_new(HWSIM_CMD_GET_RADIO);
   3083 		if (!l_genl_family_dump(hwsim, msg, get_radio_callback,
   3084 					NULL, get_radio_done_initial)) {
   3085 			l_error("Getting hwsim radio information failed");
   3086 			goto error;
   3087 		}
   3088 
   3089 		if (!l_genl_add_unicast_watch(genl, "MAC80211_HWSIM",
   3090 						hwsim_unicast_handler,
   3091 						NULL, NULL)) {
   3092 			l_error("Failed to set hwsim unicast handler");
   3093 			goto error;
   3094 		}
   3095 
   3096 		break;
   3097 	}
   3098 
   3099 	return;
   3100 
   3101 error:
   3102 	exit_status = EXIT_FAILURE;
   3103 	l_main_quit();
   3104 }
   3105 
   3106 static void family_discovered(const struct l_genl_family_info *info,
   3107 							void *user_data)
   3108 {
   3109 	if (!strcmp(l_genl_family_info_get_name(info), "MAC80211_HWSIM"))
   3110 		hwsim = l_genl_family_new(genl, "MAC80211_HWSIM");
   3111 	else if (!strcmp(l_genl_family_info_get_name(info), NL80211_GENL_NAME))
   3112 		nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME);
   3113 }
   3114 
   3115 static void discovery_done(void *user_data)
   3116 {
   3117 	if (!hwsim) {
   3118 		fprintf(stderr, "MAC80211_HWSIM doesn't exist.\n"
   3119 			"Load it manually using modprobe mac80211_hwsim\n");
   3120 		goto quit;
   3121 	}
   3122 
   3123 	if (!nl80211) {
   3124 		fprintf(stderr, "nl80211 doesn't exist.\n"
   3125 			"Load it manually using modprobe cfg80211\n");
   3126 		goto quit;
   3127 	}
   3128 
   3129 	hwsim_ready();
   3130 
   3131 	return;
   3132 
   3133 quit:
   3134 	exit_status = EXIT_FAILURE;
   3135 	l_main_quit();
   3136 }
   3137 
   3138 static void signal_handler(uint32_t signo, void *user_data)
   3139 {
   3140 	switch (signo) {
   3141 	case SIGINT:
   3142 	case SIGTERM:
   3143 		l_main_quit();
   3144 		break;
   3145 	}
   3146 }
   3147 
   3148 static void usage(void)
   3149 {
   3150 	printf("hwsim - Wireless simulator\n"
   3151 		"Usage:\n");
   3152 	printf("\thwsim [options]\n");
   3153 	printf("Options:\n"
   3154 		"\t-L, --list [id]        List simulated radios\n"
   3155 		"\t-C, --create           Create new simulated radio\n"
   3156 		"\t-D, --destroy <id>     Destroy existing radio\n"
   3157 		"\t-n, --name <name>      Name of a radio to be created\n"
   3158 		"\t-i, --nointerface      Do not create VIF\n"
   3159 		"\t-p, --p2p              Support P2P\n"
   3160 		"\t-t, --iftype-disable   List of disabled iftypes\n"
   3161 		"\t-c, --cipher-disable   List of disabled ciphers\n"
   3162 		"\t-h, --help             Show help options\n");
   3163 }
   3164 
   3165 static const struct option main_options[] = {
   3166 	{ "list",	 optional_argument,	NULL, 'L' },
   3167 	{ "create",	 no_argument,		NULL, 'C' },
   3168 	{ "destroy",	 required_argument,	NULL, 'D' },
   3169 	{ "name",	 required_argument,	NULL, 'n' },
   3170 	{ "nointerface", no_argument,		NULL, 'i' },
   3171 	{ "p2p",	 no_argument,		NULL, 'p' },
   3172 	{ "version",	 no_argument,		NULL, 'v' },
   3173 	{ "iftype-disable", required_argument,	NULL, 't' },
   3174 	{ "cipher-disable", required_argument,	NULL, 'c' },
   3175 	{ "no-register", no_argument,		NULL, 'r' },
   3176 	{ "help",	 no_argument,		NULL, 'h' },
   3177 	{ }
   3178 };
   3179 
   3180 int main(int argc, char *argv[])
   3181 {
   3182 	int actions = 0;
   3183 
   3184 	for (;;) {
   3185 		int opt;
   3186 
   3187 		opt = getopt_long(argc, argv, ":L:CD:kndetrc:ipvh", main_options,
   3188 									NULL);
   3189 		if (opt < 0)
   3190 			break;
   3191 
   3192 		switch (opt) {
   3193 		case ':':
   3194 			if (optopt == 'L') {
   3195 				action = ACTION_LIST;
   3196 				actions++;
   3197 			} else {
   3198 				printf("option '-%c' requires an argument\n",
   3199 					optopt);
   3200 				return EXIT_FAILURE;
   3201 			}
   3202 			break;
   3203 		case 'L':
   3204 			action = ACTION_LIST;
   3205 			options = optarg;
   3206 			actions++;
   3207 			break;
   3208 		case 'C':
   3209 			action = ACTION_CREATE;
   3210 			actions++;
   3211 			break;
   3212 		case 'D':
   3213 			action = ACTION_DESTROY;
   3214 			options = optarg;
   3215 			actions++;
   3216 			break;
   3217 		case 'n':
   3218 			radio_name_attr = optarg;
   3219 			break;
   3220 		case 'i':
   3221 			no_vif_attr = true;
   3222 			break;
   3223 		case 'p':
   3224 			p2p_attr = true;
   3225 			break;
   3226 		case 't':
   3227 			hwsim_disable_support(optarg, iftype_map,
   3228 						&hwsim_iftypes);
   3229 			break;
   3230 		case 'c':
   3231 			hwsim_disable_ciphers(optarg);
   3232 			break;
   3233 		case 'r':
   3234 			no_register = true;
   3235 			break;
   3236 		case 'v':
   3237 			printf("%s\n", VERSION);
   3238 			return EXIT_SUCCESS;
   3239 		case 'h':
   3240 			usage();
   3241 			return EXIT_SUCCESS;
   3242 		default:
   3243 			printf("unrecognized argument '%s'\n",
   3244 				argv[optind - 1]);
   3245 			return EXIT_FAILURE;
   3246 		}
   3247 	}
   3248 
   3249 	if (argc - optind > 0) {
   3250 		fprintf(stderr, "Invalid command line parameters\n");
   3251 		return EXIT_FAILURE;
   3252 	}
   3253 
   3254 	if (actions > 1) {
   3255 		fprintf(stderr, "Only one action can be specified\n");
   3256 		return EXIT_FAILURE;
   3257 	}
   3258 
   3259 	if (!l_main_init())
   3260 		return EXIT_FAILURE;
   3261 
   3262 	l_log_set_stderr();
   3263 	l_debug_enable("*");
   3264 
   3265 	exit_status = EXIT_FAILURE;
   3266 
   3267 	printf("Wireless simulator ver %s\n", VERSION);
   3268 
   3269 	genl = l_genl_new();
   3270 	if (!genl) {
   3271 		fprintf(stderr, "Failed to initialize generic netlink\n");
   3272 		goto done;
   3273 	}
   3274 
   3275 	if (getenv("HWSIM_DEBUG"))
   3276 		l_genl_set_debug(genl, do_debug, "[GENL] ", NULL);
   3277 
   3278 	if (!l_genl_discover_families(genl, family_discovered, NULL,
   3279 						discovery_done)) {
   3280 		fprintf(stderr, "Unable to start family discovery\n");
   3281 		l_genl_unref(genl);
   3282 		goto done;
   3283 	}
   3284 
   3285 	exit_status = l_main_run_with_signal(signal_handler, NULL);
   3286 
   3287 	l_genl_family_free(hwsim);
   3288 	l_genl_family_free(nl80211);
   3289 	l_genl_unref(genl);
   3290 
   3291 	l_dbus_destroy(dbus);
   3292 	hwsim_radio_cache_cleanup();
   3293 	l_queue_destroy(rules, destroy_rule);
   3294 
   3295 	l_netlink_destroy(rtnl);
   3296 
   3297 done:
   3298 	l_main_exit();
   3299 
   3300 	return exit_status;
   3301 }