eiwd

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

adhoc.c (19782B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2018-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 <linux/rtnetlink.h>
     28 #include <sys/socket.h>
     29 #include <linux/if.h>
     30 
     31 #include <ell/ell.h>
     32 
     33 #include "linux/nl80211.h"
     34 
     35 #include "src/iwd.h"
     36 #include "src/module.h"
     37 #include "src/netdev.h"
     38 #include "src/wiphy.h"
     39 #include "src/crypto.h"
     40 #include "src/ie.h"
     41 #include "src/util.h"
     42 #include "src/eapol.h"
     43 #include "src/handshake.h"
     44 #include "src/mpdu.h"
     45 #include "src/dbus.h"
     46 #include "src/nl80211util.h"
     47 
     48 struct adhoc_state {
     49 	struct netdev *netdev;
     50 	struct l_genl_family *nl80211;
     51 	char *ssid;
     52 	uint8_t pmk[32];
     53 	struct l_queue *sta_states;
     54 	uint32_t sta_watch_id;
     55 	uint32_t netdev_watch_id;
     56 	unsigned int mlme_watch;
     57 	struct l_dbus_message *pending;
     58 	uint32_t ciphers;
     59 	uint32_t group_cipher;
     60 	uint8_t gtk[CRYPTO_MAX_GTK_LEN];
     61 	uint8_t gtk_index;
     62 	bool started : 1;
     63 	bool open : 1;
     64 	bool gtk_set : 1;
     65 };
     66 
     67 struct sta_state {
     68 	uint8_t addr[6];
     69 	struct adhoc_state *adhoc;
     70 	struct eapol_sm *sm;
     71 	struct handshake_state *hs_sta;
     72 	struct eapol_sm *sm_a;
     73 	struct handshake_state *hs_auth;
     74 	uint32_t gtk_query_cmd_id;
     75 	bool hs_sta_done : 1;
     76 	bool hs_auth_done : 1;
     77 	bool authenticated : 1;
     78 };
     79 
     80 static uint32_t netdev_watch;
     81 
     82 static void adhoc_sta_free(void *data)
     83 {
     84 	struct sta_state *sta = data;
     85 
     86 	if (sta->adhoc->open)
     87 		goto end;
     88 
     89 	if (sta->gtk_query_cmd_id)
     90 		l_genl_family_cancel(sta->adhoc->nl80211,
     91 						sta->gtk_query_cmd_id);
     92 
     93 	if (sta->sm)
     94 		eapol_sm_free(sta->sm);
     95 
     96 	if (sta->hs_sta)
     97 		handshake_state_unref(sta->hs_sta);
     98 
     99 	if (sta->sm_a)
    100 		eapol_sm_free(sta->sm_a);
    101 
    102 	if (sta->hs_auth)
    103 		handshake_state_unref(sta->hs_auth);
    104 
    105 end:
    106 	l_free(sta);
    107 }
    108 
    109 static void adhoc_remove_sta(struct sta_state *sta)
    110 {
    111 	if (!l_queue_remove(sta->adhoc->sta_states, sta)) {
    112 		l_error("station %p was not found", sta);
    113 		return;
    114 	}
    115 
    116 	if (sta->gtk_query_cmd_id) {
    117 		l_genl_family_cancel(sta->adhoc->nl80211,
    118 						sta->gtk_query_cmd_id);
    119 		sta->gtk_query_cmd_id = 0;
    120 	}
    121 
    122 	/* signal station has been removed */
    123 	if (sta->authenticated) {
    124 		l_dbus_property_changed(dbus_get_bus(),
    125 				netdev_get_path(sta->adhoc->netdev),
    126 				IWD_ADHOC_INTERFACE, "ConnectedPeers");
    127 	}
    128 
    129 	adhoc_sta_free(sta);
    130 }
    131 
    132 static void adhoc_reset(struct adhoc_state *adhoc)
    133 {
    134 	if (adhoc->pending)
    135 		dbus_pending_reply(&adhoc->pending,
    136 				dbus_error_aborted(adhoc->pending));
    137 
    138 	l_free(adhoc->ssid);
    139 	adhoc->ssid = NULL;
    140 
    141 	netdev_station_watch_remove(adhoc->netdev, adhoc->sta_watch_id);
    142 	adhoc->sta_watch_id = 0;
    143 
    144 	l_queue_destroy(adhoc->sta_states, adhoc_sta_free);
    145 	adhoc->sta_states = NULL;
    146 
    147 	adhoc->started = false;
    148 
    149 	l_dbus_property_changed(dbus_get_bus(), netdev_get_path(adhoc->netdev),
    150 						IWD_ADHOC_INTERFACE, "Started");
    151 
    152 	if (adhoc->mlme_watch)
    153 		l_genl_family_unregister(adhoc->nl80211, adhoc->mlme_watch);
    154 }
    155 
    156 static void adhoc_set_rsn_info(struct adhoc_state *adhoc,
    157 						struct ie_rsn_info *rsn)
    158 {
    159 	memset(rsn, 0, sizeof(*rsn));
    160 	rsn->akm_suites = IE_RSN_AKM_SUITE_PSK;
    161 	rsn->pairwise_ciphers = adhoc->ciphers;
    162 	rsn->group_cipher = adhoc->group_cipher;
    163 }
    164 
    165 static bool ap_sta_match_addr(const void *a, const void *b)
    166 {
    167 	const struct sta_state *sta = a;
    168 
    169 	return !memcmp(sta->addr, b, 6);
    170 }
    171 
    172 static void adhoc_operstate_cb(int error, uint16_t type,
    173 					const void *data,
    174 					uint32_t len, void *user_data)
    175 {
    176 	if (!error)
    177 		return;
    178 
    179 	l_debug("netdev: %u, error: %s", L_PTR_TO_UINT(user_data),
    180 							strerror(-error));
    181 }
    182 
    183 static void adhoc_handshake_event(struct handshake_state *hs,
    184 		enum handshake_event event, void *user_data, ...)
    185 {
    186 	struct sta_state *sta = user_data;
    187 	struct adhoc_state *adhoc = sta->adhoc;
    188 	va_list args;
    189 
    190 	switch (event) {
    191 	case HANDSHAKE_EVENT_FAILED:
    192 		va_start(args, user_data);
    193 
    194 		l_error("handshake failed with STA "MAC" (%d)",
    195 						MAC_STR(sta->addr),
    196 						va_arg(args, int));
    197 
    198 		va_end(args);
    199 		/*
    200 		 * eapol frees the state machines upon handshake failure. Since
    201 		 * this is only a failure on one of the handshakes we need to
    202 		 * set the failing SM to NULL so it will not get double freed
    203 		 * by adhoc_remove_sta.
    204 		 */
    205 		if (sta->hs_auth == hs)
    206 			sta->sm_a = NULL;
    207 		else
    208 			sta->sm = NULL;
    209 
    210 		/* fall through */
    211 	case HANDSHAKE_EVENT_SETTING_KEYS_FAILED:
    212 		adhoc_remove_sta(sta);
    213 
    214 		return;
    215 	case HANDSHAKE_EVENT_COMPLETE:
    216 		if (sta->hs_auth == hs)
    217 			sta->hs_auth_done = true;
    218 
    219 		if (sta->hs_sta == hs)
    220 			sta->hs_sta_done = true;
    221 
    222 		if ((sta->hs_auth_done && sta->hs_sta_done) &&
    223 				!sta->authenticated) {
    224 			sta->authenticated = true;
    225 			l_dbus_property_changed(dbus_get_bus(),
    226 					netdev_get_path(adhoc->netdev),
    227 					IWD_ADHOC_INTERFACE, "ConnectedPeers");
    228 		}
    229 		break;
    230 	default:
    231 		break;
    232 	}
    233 }
    234 
    235 static struct eapol_sm *adhoc_new_sm(struct sta_state *sta, bool authenticator,
    236 					const uint8_t *gtk_rsc)
    237 {
    238 	struct adhoc_state *adhoc = sta->adhoc;
    239 	struct netdev *netdev = adhoc->netdev;
    240 	const uint8_t *own_addr = netdev_get_address(netdev);
    241 	struct ie_rsn_info rsn;
    242 	uint8_t bss_rsne[24];
    243 	struct handshake_state *hs;
    244 	struct eapol_sm *sm;
    245 
    246 	/* fill in only what handshake setup requires */
    247 	adhoc_set_rsn_info(adhoc, &rsn);
    248 	ie_build_rsne(&rsn, bss_rsne);
    249 
    250 	hs = netdev_handshake_state_new(netdev);
    251 	if (!hs) {
    252 		l_error("could not create handshake object");
    253 		return NULL;
    254 	}
    255 
    256 	handshake_state_set_event_func(hs, adhoc_handshake_event, sta);
    257 	handshake_state_set_ssid(hs, (void *)adhoc->ssid, strlen(adhoc->ssid));
    258 	/* we don't have the connecting peer rsn info, so just set ap == own */
    259 	handshake_state_set_authenticator_ie(hs, bss_rsne);
    260 	handshake_state_set_supplicant_ie(hs, bss_rsne);
    261 	handshake_state_set_pmk(hs, adhoc->pmk, 32);
    262 
    263 	if (authenticator) {
    264 		handshake_state_set_authenticator_address(hs, own_addr);
    265 		handshake_state_set_supplicant_address(hs, sta->addr);
    266 		handshake_state_set_authenticator(hs, true);
    267 	} else {
    268 		handshake_state_set_authenticator_address(hs, sta->addr);
    269 		handshake_state_set_supplicant_address(hs, own_addr);
    270 	}
    271 
    272 	if (gtk_rsc)
    273 		handshake_state_set_gtk(hs, adhoc->gtk, adhoc->gtk_index,
    274 					gtk_rsc);
    275 
    276 	sm = eapol_sm_new(hs);
    277 	if (!sm) {
    278 		l_error("could not create sm object");
    279 		return NULL;
    280 	}
    281 
    282 	eapol_sm_set_listen_interval(sm, 100);
    283 
    284 	if (authenticator)
    285 		sta->hs_auth = hs;
    286 	else
    287 		sta->hs_sta = hs;
    288 
    289 	return sm;
    290 }
    291 
    292 static void adhoc_free(struct adhoc_state *adhoc)
    293 {
    294 	adhoc_reset(adhoc);
    295 	l_genl_family_free(adhoc->nl80211);
    296 	l_free(adhoc);
    297 }
    298 
    299 static void adhoc_start_rsna(struct sta_state *sta, const uint8_t *gtk_rsc)
    300 {
    301 	sta->sm_a = adhoc_new_sm(sta, true, gtk_rsc);
    302 	if (!sta->sm_a) {
    303 		l_error("could not create authenticator state machine");
    304 		goto failed;
    305 	}
    306 
    307 	sta->sm = adhoc_new_sm(sta, false, NULL);
    308 	if (!sta->sm) {
    309 		l_error("could not create station state machine");
    310 		goto failed;
    311 	}
    312 
    313 	eapol_register(sta->sm);
    314 	eapol_register(sta->sm_a);
    315 
    316 	eapol_start(sta->sm);
    317 	eapol_start(sta->sm_a);
    318 
    319 	return;
    320 
    321 failed:
    322 	adhoc_remove_sta(sta);
    323 }
    324 
    325 static void adhoc_gtk_op_cb(struct l_genl_msg *msg, void *user_data)
    326 {
    327 	if (l_genl_msg_get_error(msg) < 0) {
    328 		uint8_t cmd = l_genl_msg_get_command(msg);
    329 		const char *cmd_name =
    330 			cmd == NL80211_CMD_NEW_KEY ? "NEW_KEY" : "SET_KEY";
    331 
    332 		l_error("%s failed for the GTK: %i",
    333 			cmd_name, l_genl_msg_get_error(msg));
    334 	}
    335 }
    336 
    337 static void adhoc_gtk_query_cb(struct l_genl_msg *msg, void *user_data)
    338 {
    339 	struct sta_state *sta = user_data;
    340 	const void *gtk_rsc;
    341 
    342 	sta->gtk_query_cmd_id = 0;
    343 
    344 	gtk_rsc = nl80211_parse_get_key_seq(msg);
    345 	if (!gtk_rsc)
    346 		goto error;
    347 
    348 	adhoc_start_rsna(sta, gtk_rsc);
    349 	return;
    350 
    351 error:
    352 	adhoc_remove_sta(sta);
    353 }
    354 
    355 static void adhoc_new_station(struct adhoc_state *adhoc, const uint8_t *mac)
    356 {
    357 	struct sta_state *sta;
    358 	struct l_genl_msg *msg;
    359 
    360 	sta = l_queue_find(adhoc->sta_states, ap_sta_match_addr, mac);
    361 	if (sta) {
    362 		l_warn("new station event with already connected STA");
    363 		return;
    364 	}
    365 
    366 	/*
    367 	 * Follows same logic as AP. If this is the first station we create and
    368 	 * set a group key. Any subsequent connections will use GET_KEY for this
    369 	 * tx GTK.
    370 	 */
    371 	if (adhoc->group_cipher != IE_RSN_CIPHER_SUITE_NO_GROUP_TRAFFIC &&
    372 			!adhoc->gtk_set && !adhoc->open) {
    373 		enum crypto_cipher group_cipher =
    374 			ie_rsn_cipher_suite_to_cipher(adhoc->group_cipher);
    375 		int gtk_len = crypto_cipher_key_len(group_cipher);
    376 
    377 		/*
    378 		 * Generate our GTK.  Not following the example derivation
    379 		 * method in 802.11-2016 section 12.7.1.4 because a simple
    380 		 * l_getrandom is just as good.
    381 		 */
    382 		l_getrandom(adhoc->gtk, gtk_len);
    383 		adhoc->gtk_index = 1;
    384 
    385 		msg = nl80211_build_new_key_group(
    386 					netdev_get_ifindex(adhoc->netdev),
    387 					group_cipher, adhoc->gtk_index,
    388 					adhoc->gtk, gtk_len, NULL,
    389 					0, NULL);
    390 
    391 		if (!l_genl_family_send(adhoc->nl80211, msg, adhoc_gtk_op_cb,
    392 					NULL, NULL)) {
    393 			l_genl_msg_unref(msg);
    394 			l_error("Issuing NEW_KEY failed");
    395 			return;
    396 		}
    397 
    398 		msg = nl80211_build_set_key(netdev_get_ifindex(adhoc->netdev),
    399 						adhoc->gtk_index);
    400 		if (!l_genl_family_send(adhoc->nl80211, msg, adhoc_gtk_op_cb,
    401 					NULL, NULL)) {
    402 			l_genl_msg_unref(msg);
    403 			l_error("Issuing SET_KEY failed");
    404 			return;
    405 		}
    406 
    407 		/*
    408 		 * Set the flag now because any new associating STA will
    409 		 * just use NL80211_CMD_GET_KEY from now.
    410 		 */
    411 		adhoc->gtk_set = true;
    412 	}
    413 
    414 	sta = l_new(struct sta_state, 1);
    415 
    416 	memset(sta, 0, sizeof(struct sta_state));
    417 
    418 	memcpy(sta->addr, mac, 6);
    419 	sta->adhoc = adhoc;
    420 
    421 	l_queue_push_tail(adhoc->sta_states, sta);
    422 
    423 	l_info("new Station: "MAC" adhoc=%p", MAC_STR(mac), adhoc);
    424 
    425 	/* with open networks nothing else is required */
    426 	if (sta->adhoc->open) {
    427 		int ifindex = netdev_get_ifindex(adhoc->netdev);
    428 
    429 		sta->authenticated = true;
    430 
    431 		l_rtnl_set_linkmode_and_operstate(iwd_get_rtnl(), ifindex,
    432 					IF_LINK_MODE_DORMANT, IF_OPER_UP,
    433 					adhoc_operstate_cb,
    434 					L_UINT_TO_PTR(ifindex), NULL);
    435 
    436 		l_dbus_property_changed(dbus_get_bus(),
    437 					netdev_get_path(adhoc->netdev),
    438 					IWD_ADHOC_INTERFACE, "ConnectedPeers");
    439 		return;
    440 	}
    441 
    442 	if (adhoc->group_cipher == IE_RSN_CIPHER_SUITE_NO_GROUP_TRAFFIC)
    443 		adhoc_start_rsna(sta, NULL);
    444 	else {
    445 		msg = nl80211_build_get_key(netdev_get_ifindex(adhoc->netdev),
    446 					adhoc->gtk_index);
    447 		sta->gtk_query_cmd_id = l_genl_family_send(adhoc->nl80211, msg,
    448 							adhoc_gtk_query_cb,
    449 							sta, NULL);
    450 		if (!sta->gtk_query_cmd_id) {
    451 			l_genl_msg_unref(msg);
    452 			l_error("Issuing GET_KEY failed");
    453 
    454 			adhoc_remove_sta(sta);
    455 			return;
    456 		}
    457 	}
    458 }
    459 
    460 static void adhoc_del_station(struct adhoc_state *adhoc, const uint8_t *mac)
    461 {
    462 	struct sta_state *sta;
    463 
    464 	sta = l_queue_find(adhoc->sta_states, ap_sta_match_addr, mac);
    465 	if (!sta) {
    466 		l_warn("could not find station "MAC" in list", MAC_STR(mac));
    467 		return;
    468 	}
    469 
    470 	l_debug("lost station "MAC, MAC_STR(mac));
    471 
    472 	adhoc_remove_sta(sta);
    473 }
    474 
    475 static void adhoc_station_changed_cb(struct netdev *netdev,
    476 		const uint8_t *mac, bool added, void *user_data)
    477 {
    478 	struct adhoc_state *adhoc = user_data;
    479 
    480 	if (added)
    481 		adhoc_new_station(adhoc, mac);
    482 	else
    483 		adhoc_del_station(adhoc, mac);
    484 }
    485 
    486 static void adhoc_join_cb(struct netdev *netdev, int result, void *user_data)
    487 {
    488 	struct adhoc_state *adhoc = user_data;
    489 	struct l_dbus_message *reply;
    490 
    491 	if (result < 0) {
    492 		l_error("Failed to join adhoc network, %i", result);
    493 		dbus_pending_reply(&adhoc->pending,
    494 					dbus_error_failed(adhoc->pending));
    495 		return;
    496 	}
    497 
    498 	l_rtnl_set_linkmode_and_operstate(iwd_get_rtnl(),
    499 					netdev_get_ifindex(adhoc->netdev),
    500 					IF_LINK_MODE_DEFAULT, IF_OPER_UP,
    501 					NULL, NULL, NULL);
    502 
    503 	adhoc->sta_watch_id = netdev_station_watch_add(netdev,
    504 			adhoc_station_changed_cb, adhoc);
    505 
    506 	reply = l_dbus_message_new_method_return(adhoc->pending);
    507 	dbus_pending_reply(&adhoc->pending, reply);
    508 }
    509 
    510 static void adhoc_mlme_notify(struct l_genl_msg *msg, void *user_data)
    511 {
    512 	struct adhoc_state *adhoc = user_data;
    513 	uint32_t ifindex;
    514 
    515 	if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex,
    516 				NL80211_ATTR_UNSPEC) < 0 ||
    517 			ifindex != netdev_get_ifindex(adhoc->netdev))
    518 		return;
    519 
    520 	switch (l_genl_msg_get_command(msg)) {
    521 	case NL80211_CMD_JOIN_IBSS:
    522 		/*
    523 		 * if watch is set the join_ibss_cb has come back. This event
    524 		 * will come in for each new STA joining the IBSS so we only
    525 		 * want to set it once
    526 		 */
    527 		if (adhoc->sta_watch_id && !adhoc->started) {
    528 			adhoc->started = true;
    529 
    530 			l_dbus_property_changed(dbus_get_bus(),
    531 						netdev_get_path(adhoc->netdev),
    532 						IWD_ADHOC_INTERFACE, "Started");
    533 		}
    534 
    535 		break;
    536 	}
    537 }
    538 
    539 static struct l_dbus_message *adhoc_dbus_start(struct l_dbus *dbus,
    540 						struct l_dbus_message *message,
    541 						void *user_data)
    542 {
    543 	struct adhoc_state *adhoc = user_data;
    544 	struct netdev *netdev = adhoc->netdev;
    545 	struct wiphy *wiphy = netdev_get_wiphy(netdev);
    546 	const char *ssid, *wpa2_psk;
    547 	struct ie_rsn_info rsn;
    548 	struct iovec rsn_ie;
    549 	uint8_t ie_elems[32];
    550 
    551 	if (adhoc->pending)
    552 		return dbus_error_busy(message);
    553 
    554 	if (!l_dbus_message_get_arguments(message, "ss", &ssid, &wpa2_psk))
    555 		return dbus_error_invalid_args(message);
    556 
    557 	adhoc->ssid = l_strdup(ssid);
    558 	adhoc->pending = l_dbus_message_ref(message);
    559 	adhoc->sta_states = l_queue_new();
    560 	adhoc->ciphers = wiphy_select_cipher(wiphy, 0xffff);
    561 	adhoc->group_cipher = wiphy_select_cipher(wiphy, 0xffff);
    562 
    563 	adhoc_set_rsn_info(adhoc, &rsn);
    564 	ie_build_rsne(&rsn, ie_elems);
    565 
    566 	rsn_ie.iov_base = ie_elems;
    567 	rsn_ie.iov_len = ie_elems[1] + 2;
    568 
    569 	if (crypto_psk_from_passphrase(wpa2_psk, (uint8_t *) ssid,
    570 			strlen(ssid), adhoc->pmk))
    571 		return dbus_error_invalid_args(message);
    572 
    573 	if (netdev_join_adhoc(netdev, ssid, &rsn_ie, 1, true, adhoc_join_cb,
    574 			adhoc))
    575 		return dbus_error_invalid_args(message);
    576 
    577 	adhoc->mlme_watch = l_genl_family_register(adhoc->nl80211, "mlme",
    578 						adhoc_mlme_notify, adhoc, NULL);
    579 	if (!adhoc->mlme_watch)
    580 		return dbus_error_failed(message);
    581 
    582 	return NULL;
    583 }
    584 
    585 static struct l_dbus_message *adhoc_dbus_start_open(struct l_dbus *dbus,
    586 				struct l_dbus_message *message, void *user_data)
    587 {
    588 	struct adhoc_state *adhoc = user_data;
    589 	struct netdev *netdev = adhoc->netdev;
    590 	const char *ssid;
    591 	struct iovec rsn_ie;
    592 	uint8_t ie_elems[10];
    593 
    594 	if (adhoc->pending)
    595 		return dbus_error_busy(message);
    596 
    597 	if (!l_dbus_message_get_arguments(message, "s", &ssid))
    598 		return dbus_error_invalid_args(message);
    599 
    600 	adhoc->ssid = l_strdup(ssid);
    601 	adhoc->pending = l_dbus_message_ref(message);
    602 	adhoc->sta_states = l_queue_new();
    603 	adhoc->open = true;
    604 
    605 	/* Mac/iPhone seem to require the extended capabilities field */
    606 	memset(ie_elems, 0, sizeof(ie_elems));
    607 	ie_elems[0] = IE_TYPE_EXTENDED_CAPABILITIES;
    608 	ie_elems[1] = 8;
    609 
    610 	rsn_ie.iov_base = ie_elems;
    611 	rsn_ie.iov_len = ie_elems[1] + 2;
    612 
    613 	if (netdev_join_adhoc(netdev, ssid, &rsn_ie, 1, false, adhoc_join_cb,
    614 			adhoc))
    615 		return dbus_error_invalid_args(message);
    616 
    617 	adhoc->mlme_watch = l_genl_family_register(adhoc->nl80211, "mlme",
    618 						adhoc_mlme_notify, adhoc, NULL);
    619 	if (!adhoc->mlme_watch)
    620 		return dbus_error_failed(message);
    621 
    622 	return NULL;
    623 }
    624 
    625 static void adhoc_leave_cb(struct netdev *netdev, int result, void *user_data)
    626 {
    627 	struct adhoc_state *adhoc = user_data;
    628 
    629 	if (result < 0) {
    630 		l_error("Failed to leave adhoc network, %i", result);
    631 		dbus_pending_reply(&adhoc->pending,
    632 				dbus_error_failed(adhoc->pending));
    633 		return;
    634 	}
    635 
    636 	dbus_pending_reply(&adhoc->pending,
    637 			l_dbus_message_new_method_return(adhoc->pending));
    638 
    639 	adhoc_reset(adhoc);
    640 }
    641 
    642 static struct l_dbus_message *adhoc_dbus_stop(struct l_dbus *dbus,
    643 						struct l_dbus_message *message,
    644 						void *user_data)
    645 {
    646 	struct adhoc_state *adhoc = user_data;
    647 
    648 	if (adhoc->pending)
    649 		return dbus_error_busy(message);
    650 
    651 	/* already stopped, no-op */
    652 	if (!adhoc->started)
    653 		return l_dbus_message_new_method_return(message);
    654 
    655 	if (netdev_leave_adhoc(adhoc->netdev, adhoc_leave_cb, adhoc))
    656 		return dbus_error_failed(message);
    657 
    658 	l_rtnl_set_linkmode_and_operstate(iwd_get_rtnl(),
    659 					netdev_get_ifindex(adhoc->netdev),
    660 					IF_LINK_MODE_DORMANT, IF_OPER_DOWN,
    661 					NULL, NULL, NULL);
    662 
    663 	adhoc->pending = l_dbus_message_ref(message);
    664 
    665 	return NULL;
    666 }
    667 
    668 static void sta_append(void *data, void *user_data)
    669 {
    670 	struct sta_state *sta = data;
    671 	struct l_dbus_message_builder *builder = user_data;
    672 	const char *macstr;
    673 
    674 	if (!sta->authenticated)
    675 		return;
    676 
    677 	macstr = util_address_to_string(sta->addr);
    678 
    679 	l_dbus_message_builder_append_basic(builder, 's', macstr);
    680 }
    681 
    682 static bool adhoc_property_get_peers(struct l_dbus *dbus,
    683 					struct l_dbus_message *message,
    684 					struct l_dbus_message_builder *builder,
    685 					void *user_data)
    686 {
    687 	struct adhoc_state *adhoc = user_data;
    688 
    689 	l_dbus_message_builder_enter_array(builder, "s");
    690 
    691 	l_queue_foreach(adhoc->sta_states, sta_append, builder);
    692 
    693 	l_dbus_message_builder_leave_array(builder);
    694 
    695 	return true;
    696 }
    697 
    698 static bool adhoc_property_get_started(struct l_dbus *dbus,
    699 					struct l_dbus_message *message,
    700 					struct l_dbus_message_builder *builder,
    701 					void *user_data)
    702 {
    703 	struct adhoc_state *adhoc = user_data;
    704 	bool started = adhoc->started;
    705 
    706 	l_dbus_message_builder_append_basic(builder, 'b', &started);
    707 
    708 	return true;
    709 }
    710 
    711 static void adhoc_setup_interface(struct l_dbus_interface *interface)
    712 {
    713 	l_dbus_interface_method(interface, "Start", 0, adhoc_dbus_start, "",
    714 					"ss", "ssid", "wpa2_psk");
    715 	l_dbus_interface_method(interface, "Stop", 0, adhoc_dbus_stop, "", "");
    716 	l_dbus_interface_method(interface, "StartOpen", 0,
    717 					adhoc_dbus_start_open, "", "s", "ssid");
    718 	l_dbus_interface_property(interface, "ConnectedPeers", 0, "as",
    719 					adhoc_property_get_peers, NULL);
    720 	l_dbus_interface_property(interface, "Started", 0, "b",
    721 					adhoc_property_get_started, NULL);
    722 }
    723 
    724 static void adhoc_destroy_interface(void *user_data)
    725 {
    726 	struct adhoc_state *adhoc = user_data;
    727 
    728 	adhoc_free(adhoc);
    729 }
    730 
    731 static void adhoc_add_interface(struct netdev *netdev)
    732 {
    733 	struct adhoc_state *adhoc;
    734 
    735 	/* just allocate/set device, Start method will complete setup */
    736 	adhoc = l_new(struct adhoc_state, 1);
    737 	adhoc->netdev = netdev;
    738 	adhoc->nl80211 = l_genl_family_new(iwd_get_genl(), NL80211_GENL_NAME);
    739 
    740 	/* setup adhoc dbus interface */
    741 	l_dbus_object_add_interface(dbus_get_bus(),
    742 			netdev_get_path(netdev), IWD_ADHOC_INTERFACE, adhoc);
    743 }
    744 
    745 static void adhoc_remove_interface(struct netdev *netdev)
    746 {
    747 	l_dbus_object_remove_interface(dbus_get_bus(),
    748 			netdev_get_path(netdev), IWD_ADHOC_INTERFACE);
    749 }
    750 
    751 static void adhoc_netdev_watch(struct netdev *netdev,
    752 				enum netdev_watch_event event, void *userdata)
    753 {
    754 	switch (event) {
    755 	case NETDEV_WATCH_EVENT_UP:
    756 #ifdef HAVE_DBUS
    757 	case NETDEV_WATCH_EVENT_NEW:
    758 		if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_ADHOC &&
    759 				netdev_get_is_up(netdev))
    760 			adhoc_add_interface(netdev);
    761 		break;
    762 	case NETDEV_WATCH_EVENT_DOWN:
    763 	case NETDEV_WATCH_EVENT_DEL:
    764 		adhoc_remove_interface(netdev);
    765 		break;
    766 #endif
    767 	default:
    768 		break;
    769 	}
    770 }
    771 
    772 static int adhoc_init(void)
    773 {
    774 	netdev_watch = netdev_watch_add(adhoc_netdev_watch, NULL, NULL);
    775 #ifdef HAVE_DBUS
    776 	l_dbus_register_interface(dbus_get_bus(), IWD_ADHOC_INTERFACE,
    777 			adhoc_setup_interface, adhoc_destroy_interface, false);
    778 #endif
    779 
    780 	return 0;
    781 }
    782 
    783 static void adhoc_exit(void)
    784 {
    785 	netdev_watch_remove(netdev_watch);
    786 #ifdef HAVE_DBUS
    787 	l_dbus_unregister_interface(dbus_get_bus(), IWD_ADHOC_INTERFACE);
    788 #endif
    789 }
    790 
    791 IWD_MODULE(adhoc, adhoc_init, adhoc_exit)
    792 IWD_MODULE_DEPENDS(adhoc, netdev);