eiwd

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

wsc.c (33121B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2015-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 #define _GNU_SOURCE
     28 #include <errno.h>
     29 #include <stdio.h>
     30 #include <ell/ell.h>
     31 
     32 #include "ell/useful.h"
     33 #include "src/missing.h"
     34 #include "src/module.h"
     35 #include "src/dbus.h"
     36 #include "src/netdev.h"
     37 #include "src/wiphy.h"
     38 #include "src/station.h"
     39 #include "src/scan.h"
     40 #include "src/ie.h"
     41 #include "src/wscutil.h"
     42 #include "src/util.h"
     43 #include "src/handshake.h"
     44 #include "src/eap-wsc.h"
     45 #include "src/common.h"
     46 #include "src/storage.h"
     47 #include "src/iwd.h"
     48 #include "src/network.h"
     49 #include "src/wsc.h"
     50 #include "src/band.h"
     51 
     52 #define WALK_TIME 120
     53 
     54 static uint32_t netdev_watch = 0;
     55 
     56 struct wsc_enrollee {
     57 	struct netdev *netdev;
     58 	struct wsc_credentials_info creds[3];
     59 	uint32_t n_creds;
     60 	struct l_settings *eap_settings;
     61 	wsc_done_cb_t done_cb;
     62 	void *done_data;
     63 	bool disconnecting : 1;
     64 };
     65 
     66 static struct l_dbus_message *wsc_error_session_overlap(
     67 						struct l_dbus_message *msg)
     68 {
     69 	return l_dbus_message_new_error(msg,
     70 					IWD_WSC_INTERFACE ".SessionOverlap",
     71 					"Multiple sessions detected");
     72 }
     73 
     74 static struct l_dbus_message *wsc_error_no_credentials(
     75 						struct l_dbus_message *msg)
     76 {
     77 	return l_dbus_message_new_error(msg, IWD_WSC_INTERFACE ".NoCredentials",
     78 					"No usable credentials obtained");
     79 }
     80 
     81 static struct l_dbus_message *wsc_error_not_reachable(
     82 						struct l_dbus_message *msg)
     83 {
     84 	return l_dbus_message_new_error(msg, IWD_WSC_INTERFACE ".NotReachable",
     85 					"Credentials obtained, but network is "
     86 					"unreachable");
     87 }
     88 
     89 static struct l_dbus_message *wsc_error_walk_time_expired(
     90 						struct l_dbus_message *msg)
     91 {
     92 	return l_dbus_message_new_error(msg,
     93 					IWD_WSC_INTERFACE ".WalkTimeExpired",
     94 					"No APs in PushButton mode found in "
     95 					"the allotted time");
     96 }
     97 
     98 static struct l_dbus_message *wsc_error_time_expired(struct l_dbus_message *msg)
     99 {
    100 	return l_dbus_message_new_error(msg,
    101 					IWD_WSC_INTERFACE ".TimeExpired",
    102 					"No APs in PIN mode found in "
    103 					"the allotted time");
    104 }
    105 
    106 static void wsc_enrollee_connect_cb(struct netdev *netdev,
    107 					enum netdev_result result,
    108 					void *event_data, void *user_data)
    109 {
    110 	struct wsc_enrollee *wsce = user_data;
    111 
    112 	l_debug("%d, result: %d", netdev_get_ifindex(wsce->netdev), result);
    113 
    114 	if (wsce->disconnecting)
    115 		return;	/* Free the state in the disconnect callback */
    116 
    117 	if (!wsce->done_cb)
    118 		goto done;
    119 
    120 	if (result == NETDEV_RESULT_HANDSHAKE_FAILED && wsce->n_creds > 0) {
    121 		wsce->done_cb(0, wsce->creds, wsce->n_creds, wsce->done_data);
    122 		goto done;
    123 	}
    124 
    125 	switch (result) {
    126 	case NETDEV_RESULT_ABORTED:
    127 		wsce->done_cb(-ECANCELED, NULL, 0, wsce->done_data);
    128 		break;
    129 	case NETDEV_RESULT_HANDSHAKE_FAILED:
    130 		wsce->done_cb(-ENOKEY, NULL, 0, wsce->done_data);
    131 		break;
    132 	default:
    133 		wsce->done_cb(-EIO, NULL, 0, wsce->done_data);
    134 		break;
    135 	}
    136 
    137 done:
    138 	wsc_enrollee_free(wsce);
    139 }
    140 
    141 static void wsc_enrollee_credential_obtained(struct wsc_enrollee *wsce,
    142 					const struct wsc_credential *cred)
    143 {
    144 	uint16_t auth_mask;
    145 	unsigned int i;
    146 
    147 	l_debug("Obtained credenials for SSID: %s, address: %s",
    148 			util_ssid_to_utf8(cred->ssid_len, cred->ssid),
    149 			util_address_to_string(cred->addr));
    150 
    151 	l_debug("auth_type: %02x, encryption_type: %02x",
    152 			cred->auth_type, cred->encryption_type);
    153 
    154 	if (getenv("IWD_WSC_DEBUG_KEYS"))
    155 		l_debug("Key (%u): %.*s", cred->network_key_len,
    156 				cred->network_key_len, cred->network_key);
    157 
    158 	if (wsce->n_creds == L_ARRAY_SIZE(wsce->creds)) {
    159 		l_warn("Maximum number of credentials obtained, ignoring...");
    160 		return;
    161 	}
    162 
    163 	if (!util_ssid_is_utf8(cred->ssid_len, cred->ssid)) {
    164 		l_warn("Ignoring Credentials with non-UTF8 SSID");
    165 		return;
    166 	}
    167 
    168 	memcpy(wsce->creds[wsce->n_creds].ssid, cred->ssid, cred->ssid_len);
    169 	wsce->creds[wsce->n_creds].ssid[cred->ssid_len] = '\0';
    170 
    171 	/* We only support open/personal wpa/personal wpa2 */
    172 	auth_mask = WSC_AUTHENTICATION_TYPE_OPEN |
    173 			WSC_AUTHENTICATION_TYPE_WPA_PERSONAL |
    174 			WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL;
    175 	if ((cred->auth_type & auth_mask) == 0) {
    176 		l_warn("Ignoring Credentials with unsupported auth_type");
    177 		return;
    178 	}
    179 
    180 	if (cred->auth_type & WSC_AUTHENTICATION_TYPE_OPEN) {
    181 		auth_mask &= ~WSC_AUTHENTICATION_TYPE_OPEN;
    182 
    183 		if (cred->auth_type & auth_mask) {
    184 			l_warn("Ignoring mixed open/wpa credentials");
    185 			return;
    186 		}
    187 
    188 		wsce->creds[wsce->n_creds].security = SECURITY_NONE;
    189 	} else
    190 		wsce->creds[wsce->n_creds].security = SECURITY_PSK;
    191 
    192 	switch (wsce->creds[wsce->n_creds].security) {
    193 	case SECURITY_NONE:
    194 		if (cred->network_key_len != 0) {
    195 			l_warn("ignoring invalid open key length");
    196 			return;
    197 		}
    198 
    199 		break;
    200 	case SECURITY_PSK:
    201 		if (cred->network_key_len == 64) {
    202 			unsigned char *decoded;
    203 			const char *hex = (const char *) cred->network_key;
    204 
    205 			decoded = l_util_from_hexstring(hex, NULL);
    206 			if (!decoded) {
    207 				l_warn("Ignoring non-hex network_key");
    208 				return;
    209 			}
    210 
    211 			memcpy(wsce->creds[wsce->n_creds].psk, decoded, 32);
    212 			explicit_bzero(decoded, 32);
    213 			l_free(decoded);
    214 		} else {
    215 			strncpy(wsce->creds[wsce->n_creds].passphrase,
    216 					(const char *) cred->network_key,
    217 					cred->network_key_len);
    218 			wsce->creds[wsce->n_creds].has_passphrase = true;
    219 		}
    220 
    221 		break;
    222 	default:
    223 		return;
    224 	}
    225 
    226 	for (i = 0; i < wsce->n_creds; i++) {
    227 		if (strcmp(wsce->creds[i].ssid,
    228 				wsce->creds[wsce->n_creds].ssid))
    229 			continue;
    230 
    231 		l_warn("Found duplicate credentials for SSID: %s",
    232 				wsce->creds[i].ssid);
    233 		explicit_bzero(&wsce->creds[wsce->n_creds],
    234 				sizeof(wsce->creds[wsce->n_creds]));
    235 		return;
    236 	}
    237 
    238 	memcpy(wsce->creds[wsce->n_creds].addr, cred->addr, 6);
    239 	wsce->n_creds += 1;
    240 }
    241 
    242 static void wsc_enrollee_netdev_event(struct netdev *netdev,
    243 					enum netdev_event event,
    244 					void *event_data, void *user_data)
    245 {
    246 	struct wsc_enrollee *wsce = user_data;
    247 
    248 	switch (event) {
    249 	case NETDEV_EVENT_AUTHENTICATING:
    250 	case NETDEV_EVENT_ASSOCIATING:
    251 		break;
    252 	case NETDEV_EVENT_DISCONNECT_BY_AP:
    253 		l_debug("Disconnect by AP");
    254 		wsc_enrollee_connect_cb(wsce->netdev,
    255 					NETDEV_RESULT_HANDSHAKE_FAILED,
    256 					event_data, wsce);
    257 		break;
    258 	case NETDEV_EVENT_RSSI_THRESHOLD_LOW:
    259 	case NETDEV_EVENT_RSSI_THRESHOLD_HIGH:
    260 		break;
    261 	default:
    262 		l_debug("Unexpected event: %d", event);
    263 		break;
    264 	}
    265 }
    266 
    267 static void wsc_enrollee_handshake_event(struct handshake_state *hs,
    268 						enum handshake_event event,
    269 						void *user_data, ...)
    270 {
    271 	struct wsc_enrollee *wsce = user_data;
    272 	va_list args;
    273 
    274 	va_start(args, user_data);
    275 
    276 	switch (event) {
    277 	case HANDSHAKE_EVENT_FAILED:
    278 		netdev_handshake_failed(hs, va_arg(args, int));
    279 		break;
    280 	case HANDSHAKE_EVENT_EAP_NOTIFY:
    281 	{
    282 		unsigned int eap_event = va_arg(args, unsigned int);
    283 
    284 		switch (eap_event) {
    285 		case EAP_WSC_EVENT_CREDENTIAL_OBTAINED:
    286 			wsc_enrollee_credential_obtained(wsce,
    287 				va_arg(args, const struct wsc_credential *));
    288 			break;
    289 		default:
    290 			l_debug("Got event: %d", eap_event);
    291 		}
    292 
    293 		break;
    294 	}
    295 	default:
    296 		break;
    297 	}
    298 
    299 	va_end(args);
    300 }
    301 
    302 static inline enum wsc_rf_band freq_to_rf_band(uint32_t freq)
    303 {
    304 	enum band_freq band;
    305 
    306 	band_freq_to_channel(freq, &band);
    307 
    308 	switch (band) {
    309 	case BAND_FREQ_2_4_GHZ:
    310 		return WSC_RF_BAND_2_4_GHZ;
    311 	case BAND_FREQ_5_GHZ:
    312 	case BAND_FREQ_6_GHZ:
    313 		return WSC_RF_BAND_5_0_GHZ;
    314 	}
    315 
    316 	return WSC_RF_BAND_2_4_GHZ;
    317 }
    318 
    319 static int wsc_enrollee_connect(struct wsc_enrollee *wsce, struct scan_bss *bss,
    320 					const char *pin, struct iovec *ies,
    321 					unsigned int ies_num)
    322 {
    323 	struct handshake_state *hs;
    324 	struct l_settings *settings = l_settings_new();
    325 	int r;
    326 	struct wsc_association_request request;
    327 	uint8_t *pdu;
    328 	size_t pdu_len;
    329 	struct iovec ie_iov[1 + ies_num];
    330 
    331 	hs = netdev_handshake_state_new(wsce->netdev);
    332 
    333 	l_settings_set_string(settings, "Security", "EAP-Identity",
    334 					"WFA-SimpleConfig-Enrollee-1-0");
    335 	l_settings_set_string(settings, "Security", "EAP-Method", "WSC");
    336 
    337 	l_settings_set_uint(settings, "WSC", "RFBand",
    338 					freq_to_rf_band(bss->frequency));
    339 	l_settings_set_uint(settings, "WSC", "ConfigurationMethods",
    340 				WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
    341 				WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON |
    342 				WSC_CONFIGURATION_METHOD_KEYPAD);
    343 	l_settings_set_string(settings, "WSC", "PrimaryDeviceType",
    344 					"0-00000000-0");
    345 	l_settings_set_string(settings, "WSC", "EnrolleeMAC",
    346 		util_address_to_string(netdev_get_address(wsce->netdev)));
    347 
    348 	if (pin) {
    349 		enum wsc_device_password_id dpid;
    350 
    351 		if (strlen(pin) == 4 || wsc_pin_is_checksum_valid(pin))
    352 			dpid = WSC_DEVICE_PASSWORD_ID_DEFAULT;
    353 		else
    354 			dpid = WSC_DEVICE_PASSWORD_ID_USER_SPECIFIED;
    355 
    356 		l_settings_set_uint(settings, "WSC", "DevicePasswordId", dpid);
    357 		l_settings_set_string(settings, "WSC", "DevicePassword", pin);
    358 	}
    359 
    360 	handshake_state_set_event_func(hs, wsc_enrollee_handshake_event, wsce);
    361 	handshake_state_set_8021x_config(hs, settings);
    362 	handshake_state_set_ssid(hs, bss->ssid, bss->ssid_len);
    363 	wsce->eap_settings = settings;
    364 
    365 	request.version2 = true;
    366 	request.request_type = WSC_REQUEST_TYPE_ENROLLEE_OPEN_8021X;
    367 
    368 	pdu = wsc_build_association_request(&request, &pdu_len);
    369 	if (!pdu) {
    370 		r = -ENOMEM;
    371 		goto error;
    372 	}
    373 
    374 	ie_iov[0].iov_base = ie_tlv_encapsulate_wsc_payload(pdu, pdu_len,
    375 							&ie_iov[0].iov_len);
    376 	l_free(pdu);
    377 
    378 	if (!ie_iov[0].iov_base) {
    379 		r = -ENOMEM;
    380 		goto error;
    381 	}
    382 
    383 	if (ies_num)
    384 		memcpy(ie_iov + 1, ies, sizeof(struct iovec) * ies_num);
    385 
    386 	handshake_state_set_vendor_ies(hs, ie_iov, 1 + ies_num);
    387 
    388 	r = netdev_connect(wsce->netdev, bss, hs,
    389 				wsc_enrollee_netdev_event,
    390 				wsc_enrollee_connect_cb, wsce);
    391 	l_free(ie_iov[0].iov_base);
    392 
    393 	if (r == 0)
    394 		return 0;
    395 
    396 error:
    397 	handshake_state_unref(hs);
    398 	return r;
    399 }
    400 
    401 struct wsc_enrollee *wsc_enrollee_new(struct netdev *netdev,
    402 					struct scan_bss *target,
    403 					const char *pin,
    404 					struct iovec *ies, unsigned int ies_num,
    405 					wsc_done_cb_t done_cb, void *user_data)
    406 {
    407 	struct wsc_enrollee *wsce;
    408 
    409 	wsce = l_new(struct wsc_enrollee, 1);
    410 	wsce->netdev = netdev;
    411 	wsce->done_cb = done_cb;
    412 	wsce->done_data = user_data;
    413 
    414 	if (wsc_enrollee_connect(wsce, target, pin, ies, ies_num) == 0)
    415 		return wsce;
    416 
    417 	wsc_enrollee_free(wsce);
    418 	return NULL;
    419 }
    420 
    421 static void wsc_enrollee_disconnect_cb(struct netdev *netdev, bool result,
    422 					void *user_data)
    423 {
    424 	struct wsc_enrollee *wsce = user_data;
    425 
    426 	wsce->done_cb(-ECANCELED, NULL, 0, wsce->done_data);
    427 	wsc_enrollee_free(wsce);
    428 }
    429 
    430 void wsc_enrollee_cancel(struct wsc_enrollee *wsce, bool defer_cb)
    431 {
    432 	if (defer_cb) {
    433 		wsce->disconnecting = true;
    434 		netdev_disconnect(wsce->netdev, wsc_enrollee_disconnect_cb,
    435 					wsce);
    436 	} else {
    437 		wsce->done_cb(-ECANCELED, NULL, 0, wsce->done_data);
    438 		wsce->done_cb = NULL;
    439 		/*
    440 		 * Results in a call to
    441 		 * wsc_enrollee_connect_cb -> wsc_enrollee_free
    442 		 */
    443 		netdev_disconnect(wsce->netdev, NULL, NULL);
    444 	}
    445 }
    446 
    447 void wsc_enrollee_free(struct wsc_enrollee *wsce)
    448 {
    449 	l_settings_free(wsce->eap_settings);
    450 	explicit_bzero(wsce->creds, sizeof(wsce->creds));
    451 	l_free(wsce);
    452 }
    453 
    454 struct wsc_station_dbus {
    455 	struct wsc_dbus super;
    456 	struct wsc_enrollee *enrollee;
    457 	struct scan_bss *target;
    458 	struct netdev *netdev;
    459 	struct station *station;
    460 	uint8_t *wsc_ies;
    461 	size_t wsc_ies_size;
    462 	struct l_timeout *walk_timer;
    463 	uint32_t scan_id;
    464 	uint32_t station_state_watch;
    465 };
    466 
    467 #define CONNECT_REPLY(wsc, message)					\
    468 	if ((wsc)->super.pending_connect)				\
    469 		dbus_pending_reply(&(wsc)->super.pending_connect,	\
    470 				message((wsc)->super.pending_connect))	\
    471 
    472 #define CANCEL_REPLY(wsc, message)					\
    473 	if ((wsc)->super.pending_cancel)				\
    474 		dbus_pending_reply(&(wsc)->super.pending_cancel,	\
    475 				message((wsc)->super.pending_cancel))	\
    476 
    477 static void wsc_try_credentials(struct wsc_station_dbus *wsc,
    478 				struct wsc_credentials_info *creds,
    479 				unsigned int n_creds)
    480 {
    481 	unsigned int i;
    482 	struct network *network;
    483 	struct scan_bss *bss;
    484 
    485 	for (i = 0; i < n_creds; i++) {
    486 		network = station_network_find(wsc->station, creds[i].ssid,
    487 						creds[i].security);
    488 		if (!network)
    489 			continue;
    490 
    491 		bss = network_bss_find_by_addr(network, creds[i].addr);
    492 
    493 		if (!bss)
    494 			bss = network_bss_select(network, true);
    495 
    496 		if (!bss)
    497 			continue;
    498 
    499 		if (creds[i].security == SECURITY_PSK) {
    500 			bool ret;
    501 
    502 			/*
    503 			 * Prefer setting passphrase, this will work for both
    504 			 * WPA2 and WPA3 since the PSK can always be generated
    505 			 * if needed
    506 			 */
    507 			if (creds[i].has_passphrase)
    508 				ret = network_set_passphrase(network,
    509 							creds[i].passphrase);
    510 			else
    511 				ret = network_set_psk(network, creds[i].psk);
    512 
    513 			if (!ret)
    514 				continue;
    515 		}
    516 
    517 		station_connect_network(wsc->station, network, bss,
    518 						wsc->super.pending_connect);
    519 		l_dbus_message_unref(wsc->super.pending_connect);
    520 		wsc->super.pending_connect = NULL;
    521 
    522 		return;
    523 	}
    524 
    525 	CONNECT_REPLY(wsc, wsc_error_not_reachable);
    526 	station_set_autoconnect(wsc->station, true);
    527 }
    528 
    529 static void wsc_store_credentials(struct wsc_credentials_info *creds,
    530 					unsigned int n_creds)
    531 {
    532 	unsigned int i;
    533 
    534 	/* We don't store any non-open/psk credentials */
    535 	for (i = 0; i < n_creds; i++) {
    536 		enum security security = creds[i].security;
    537 		const char *ssid = creds[i].ssid;
    538 		_auto_(l_settings_free) struct l_settings *settings =
    539 							l_settings_new();
    540 		_auto_(l_free) char *path =
    541 				storage_get_network_file_path(security, ssid);
    542 
    543 		if (l_settings_load_from_file(settings, path)) {
    544 			/*
    545 			 * Nothing to do,
    546 			 * so don't overwrite any existing settings
    547 			 */
    548 			if (security == SECURITY_NONE)
    549 				continue;
    550 
    551 			/* Remove any existing Security keys */
    552 			l_settings_remove_group(settings, "Security");
    553 		}
    554 
    555 		if (security == SECURITY_PSK) {
    556 			if (creds[i].has_passphrase)
    557 				l_settings_set_string(settings, "Security",
    558 					"Passphrase", creds[i].passphrase);
    559 			else
    560 				l_settings_set_bytes(settings, "Security",
    561 						"PreSharedKey", creds[i].psk,
    562 						sizeof(creds[i].psk));
    563 		}
    564 
    565 		l_debug("Storing credential for '%s(%s)'", ssid,
    566 						security_to_str(security));
    567 		storage_network_sync(security, ssid, settings);
    568 	}
    569 }
    570 
    571 static void wsc_dbus_done_cb(int err, struct wsc_credentials_info *creds,
    572 				unsigned int n_creds, void *user_data)
    573 {
    574 	struct wsc_station_dbus *wsc = user_data;
    575 
    576 	wsc->enrollee = NULL;
    577 	wsc->target = NULL;
    578 
    579 	l_debug("err=%i", err);
    580 
    581 	if (err && wsc->station)
    582 		station_set_autoconnect(wsc->station, true);
    583 
    584 	switch (err) {
    585 	case 0:
    586 		break;
    587 	case -ECANCELED:
    588 		/* Send reply if we haven't already sent one e.g. in Cancel() */
    589 		CONNECT_REPLY(wsc, dbus_error_aborted);
    590 		CANCEL_REPLY(wsc, l_dbus_message_new_method_return);
    591 		return;
    592 	case -ENOKEY:
    593 		CONNECT_REPLY(wsc, wsc_error_no_credentials);
    594 		return;
    595 	case -EBUSY:
    596 		CONNECT_REPLY(wsc, dbus_error_busy);
    597 		return;
    598 	default:
    599 		CONNECT_REPLY(wsc, dbus_error_failed);
    600 		return;
    601 	}
    602 
    603 	wsc_store_credentials(creds, n_creds);
    604 	wsc_try_credentials(wsc, creds, n_creds);
    605 }
    606 
    607 static void wsc_connect(struct wsc_station_dbus *wsc)
    608 {
    609 	const char *pin = NULL;
    610 
    611 	if (!strcmp(l_dbus_message_get_member(wsc->super.pending_connect),
    612 			"StartPin"))
    613 		l_dbus_message_get_arguments(wsc->super.pending_connect, "s",
    614 						&pin);
    615 
    616 	wsc->enrollee = wsc_enrollee_new(wsc->netdev, wsc->target, pin, NULL, 0,
    617 						wsc_dbus_done_cb, wsc);
    618 	if (wsc->enrollee)
    619 		return;
    620 
    621 	wsc_dbus_done_cb(-EIO, NULL, 0, wsc);
    622 }
    623 
    624 static void station_state_watch(enum station_state state, void *userdata)
    625 {
    626 	struct wsc_station_dbus *wsc = userdata;
    627 
    628 	if (state != STATION_STATE_DISCONNECTED)
    629 		return;
    630 
    631 	l_debug("%p", wsc);
    632 
    633 	station_remove_state_watch(wsc->station, wsc->station_state_watch);
    634 	wsc->station_state_watch = 0;
    635 
    636 	wsc_connect(wsc);
    637 }
    638 
    639 static void wsc_check_can_connect(struct wsc_station_dbus *wsc,
    640 					struct scan_bss *target)
    641 {
    642 	l_debug("%p", wsc);
    643 
    644 	/*
    645 	 * For now we assign the target pointer directly, since we should not
    646 	 * be triggering any more scans while disconnecting / connecting
    647 	 */
    648 	wsc->target = target;
    649 	station_set_autoconnect(wsc->station, false);
    650 
    651 	switch (station_get_state(wsc->station)) {
    652 	case STATION_STATE_DISCONNECTED:
    653 		wsc_connect(wsc);
    654 		return;
    655 	case STATION_STATE_CONNECTING:
    656 	case STATION_STATE_CONNECTING_AUTO:
    657 	case STATION_STATE_CONNECTED:
    658 	case STATION_STATE_NETCONFIG:
    659 		if (station_disconnect(wsc->station) < 0)
    660 			goto error;
    661 
    662 		/* fall through */
    663 	case STATION_STATE_DISCONNECTING:
    664 		wsc->station_state_watch =
    665 			station_add_state_watch(wsc->station,
    666 						station_state_watch,
    667 						wsc, NULL);
    668 		return;
    669 	case STATION_STATE_AUTOCONNECT_QUICK:
    670 	case STATION_STATE_AUTOCONNECT_FULL:
    671 	case STATION_STATE_ROAMING:
    672 	case STATION_STATE_FT_ROAMING:
    673 	case STATION_STATE_FW_ROAMING:
    674 		l_warn("%s: invalid station state", __func__);
    675 		break;
    676 	}
    677 error:
    678 	wsc->target = NULL;
    679 	CONNECT_REPLY(wsc, dbus_error_failed);
    680 }
    681 
    682 static void wsc_cancel_scan(struct wsc_station_dbus *wsc)
    683 {
    684 	l_free(wsc->wsc_ies);
    685 	wsc->wsc_ies = 0;
    686 
    687 	if (wsc->scan_id > 0) {
    688 		scan_cancel(netdev_get_wdev_id(wsc->netdev), wsc->scan_id);
    689 		wsc->scan_id = 0;
    690 	}
    691 
    692 	if (wsc->walk_timer) {
    693 		l_timeout_remove(wsc->walk_timer);
    694 		wsc->walk_timer = NULL;
    695 	}
    696 }
    697 
    698 static void walk_timeout(struct l_timeout *timeout, void *user_data)
    699 {
    700 	struct wsc_station_dbus *wsc = user_data;
    701 
    702 	wsc_cancel_scan(wsc);
    703 	CONNECT_REPLY(wsc, wsc_error_walk_time_expired);
    704 }
    705 
    706 static void pin_timeout(struct l_timeout *timeout, void *user_data)
    707 {
    708 	struct wsc_station_dbus *wsc = user_data;
    709 
    710 	wsc_cancel_scan(wsc);
    711 	CONNECT_REPLY(wsc, wsc_error_time_expired);
    712 }
    713 
    714 static bool push_button_scan_results(int err, struct l_queue *bss_list,
    715 					const struct scan_freq_set *freqs,
    716 					void *userdata)
    717 {
    718 	struct wsc_station_dbus *wsc = userdata;
    719 	struct scan_bss *bss_2g;
    720 	struct scan_bss *bss_5g;
    721 	struct scan_bss *target;
    722 	uint8_t uuid_2g[16];
    723 	uint8_t uuid_5g[16];
    724 	const struct l_queue_entry *bss_entry;
    725 	struct wsc_probe_response probe_response;
    726 
    727 	if (err) {
    728 		wsc_cancel_scan(wsc);
    729 		CONNECT_REPLY(wsc, dbus_error_failed);
    730 
    731 		return false;
    732 	}
    733 
    734 	bss_2g = NULL;
    735 	bss_5g = NULL;
    736 
    737 	wsc->scan_id = 0;
    738 
    739 	for (bss_entry = l_queue_get_entries(bss_list); bss_entry;
    740 				bss_entry = bss_entry->next) {
    741 		struct scan_bss *bss = bss_entry->data;
    742 		enum band_freq band;
    743 		int err;
    744 
    745 		l_debug("bss '%s' with SSID: %s, freq: %u",
    746 			util_address_to_string(bss->addr),
    747 			util_ssid_to_utf8(bss->ssid_len, bss->ssid),
    748 			bss->frequency);
    749 
    750 		l_debug("bss->wsc: %p, %zu", bss->wsc, bss->wsc_size);
    751 
    752 		if (!bss->wsc)
    753 			continue;
    754 
    755 		err = wsc_parse_probe_response(bss->wsc, bss->wsc_size,
    756 						&probe_response);
    757 		if (err < 0) {
    758 			l_debug("ProbeResponse parse failed: %s",
    759 							strerror(-err));
    760 			continue;
    761 		}
    762 
    763 		l_debug("SelectedRegistar: %s",
    764 			probe_response.selected_registrar ? "true" : "false");
    765 
    766 		if (!probe_response.selected_registrar)
    767 			continue;
    768 
    769 		if (probe_response.device_password_id !=
    770 				WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON)
    771 			continue;
    772 
    773 		band_freq_to_channel(bss->frequency, &band);
    774 
    775 		switch (band) {
    776 		case BAND_FREQ_2_4_GHZ:
    777 			if (bss_2g) {
    778 				l_debug("2G Session overlap error");
    779 				goto session_overlap;
    780 			}
    781 
    782 			bss_2g = bss;
    783 			memcpy(uuid_2g, probe_response.uuid_e, 16);
    784 			break;
    785 
    786 		case BAND_FREQ_5_GHZ:
    787 			if (bss_5g) {
    788 				l_debug("5G Session overlap error");
    789 				goto session_overlap;
    790 			}
    791 
    792 			bss_5g = bss;
    793 			memcpy(uuid_5g, probe_response.uuid_e, 16);
    794 			break;
    795 
    796 		default:
    797 			return false;
    798 		}
    799 	}
    800 
    801 	if (bss_2g && bss_5g && memcmp(uuid_2g, uuid_5g, 16)) {
    802 		l_debug("Found two PBC APs on different bands");
    803 		goto session_overlap;
    804 	}
    805 
    806 	if (bss_5g)
    807 		target = bss_5g;
    808 	else if (bss_2g)
    809 		target = bss_2g;
    810 	else {
    811 		l_debug("No PBC APs found, running the scan again");
    812 		wsc->scan_id = scan_active(netdev_get_wdev_id(wsc->netdev),
    813 						wsc->wsc_ies, wsc->wsc_ies_size,
    814 						NULL, push_button_scan_results,
    815 						wsc, NULL);
    816 		return false;
    817 	}
    818 
    819 	wsc_cancel_scan(wsc);
    820 	station_set_scan_results(wsc->station, bss_list, freqs, false);
    821 
    822 	l_debug("Found AP to connect to: %s",
    823 			util_address_to_string(target->addr));
    824 	wsc_check_can_connect(wsc, target);
    825 
    826 	return true;
    827 
    828 session_overlap:
    829 	wsc_cancel_scan(wsc);
    830 	CONNECT_REPLY(wsc, wsc_error_session_overlap);
    831 
    832 	return false;
    833 }
    834 
    835 static const char *authorized_macs_to_string(const uint8_t *authorized_macs)
    836 {
    837 	unsigned int i;
    838 	unsigned int offset = 0;
    839 	/* Max of 5 addresses in AuthorizedMacs, 17 bytes / address */
    840 	static char buf[128];
    841 
    842 	for (i = 0; i < 5; i++) {
    843 		const uint8_t *addr = authorized_macs + i * 6;
    844 
    845 		if (l_memeqzero(addr, 6))
    846 			continue;
    847 
    848 		offset += sprintf(buf + offset, "%s",
    849 						util_address_to_string(addr));
    850 	}
    851 
    852 	return buf;
    853 }
    854 
    855 static bool authorized_macs_contains(const uint8_t *authorized_macs,
    856 							const uint8_t *target)
    857 {
    858 	unsigned int i;
    859 
    860 	for (i = 0; i < 5; i++) {
    861 		const uint8_t *addr = authorized_macs + i * 6;
    862 
    863 		if (!memcmp(addr, target, 6))
    864 			return true;
    865 	}
    866 
    867 	return false;
    868 }
    869 
    870 static bool pin_scan_results(int err, struct l_queue *bss_list,
    871 				const struct scan_freq_set *freqs,
    872 				void *userdata)
    873 {
    874 	static const uint8_t wildcard_address[] =
    875 					{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    876 	struct wsc_station_dbus *wsc = userdata;
    877 	struct scan_bss *target = NULL;
    878 	const struct l_queue_entry *bss_entry;
    879 	struct wsc_probe_response probe_response;
    880 
    881 	if (err) {
    882 		wsc_cancel_scan(wsc);
    883 		CONNECT_REPLY(wsc, dbus_error_failed);
    884 
    885 		return false;
    886 	}
    887 
    888 	wsc->scan_id = 0;
    889 
    890 	for (bss_entry = l_queue_get_entries(bss_list); bss_entry;
    891 				bss_entry = bss_entry->next) {
    892 		struct scan_bss *bss = bss_entry->data;
    893 		const uint8_t *amacs;
    894 		int err;
    895 
    896 		l_debug("bss '%s' with SSID: %s, freq: %u",
    897 			util_address_to_string(bss->addr),
    898 			util_ssid_to_utf8(bss->ssid_len, bss->ssid),
    899 			bss->frequency);
    900 
    901 		l_debug("bss->wsc: %p, %zu", bss->wsc, bss->wsc_size);
    902 
    903 		if (!bss->wsc)
    904 			continue;
    905 
    906 		err = wsc_parse_probe_response(bss->wsc, bss->wsc_size,
    907 						&probe_response);
    908 		if (err < 0) {
    909 			l_debug("ProbeResponse parse failed: %s",
    910 							strerror(-err));
    911 			continue;
    912 		}
    913 
    914 		if (probe_response.device_password_id !=
    915 					WSC_DEVICE_PASSWORD_ID_DEFAULT &&
    916 				probe_response.device_password_id !=
    917 					WSC_DEVICE_PASSWORD_ID_USER_SPECIFIED)
    918 			continue;
    919 
    920 		l_debug("SelectedRegistar: %s",
    921 			probe_response.selected_registrar ? "true" : "false");
    922 
    923 		/*
    924 		 * WSC Best Practices v2.0.1, Section 3.4:
    925 		 * In a mixed environment with both WSC 1.0 and WSC 2.0 APs, an
    926 		 * Enrollee should be prepared to run both the WSC 1.0 and
    927 		 * WSC 2.0 forms of discovery. An Enrollee may scan available
    928 		 * channels and then order PIN attempts with prospective APs
    929 		 * as follows:
    930 		 * 1. WSC 2.0 AP with the Selected Registrar attribute TRUE
    931 		 * and the Enrollee's MAC address in the AuthorizedMACs
    932 		 * sub-element in Beacons and Probe Responses.
    933 		 * 2. WSC 2.0 APs with the Selected Registrar attribute TRUE
    934 		 * and the wildcard MAC address in the AuthorizedMACs
    935 		 * sub-element in Beacons and Probe Responses, ordered by
    936 		 * decreasing RSSI.
    937 		 * 3. WSC 1.0 APs, ordered by decreasing RSSI.
    938 		 * If option 1 is available, options 2 and 3 should be
    939 		 * unnecessary.
    940 		 */
    941 		if (!probe_response.selected_registrar)
    942 			continue;
    943 
    944 		amacs = probe_response.authorized_macs;
    945 		l_debug("AuthorizedMacs: %s", authorized_macs_to_string(amacs));
    946 
    947 		if (authorized_macs_contains(amacs,
    948 					netdev_get_address(wsc->netdev))) {
    949 			target = bss;
    950 			break;
    951 		} else if (!target && authorized_macs_contains(amacs,
    952 							wildcard_address))
    953 			target = bss;
    954 	}
    955 
    956 	if (!target) {
    957 		l_debug("No PIN APs found, running the scan again");
    958 		wsc->scan_id = scan_active(netdev_get_wdev_id(wsc->netdev),
    959 						wsc->wsc_ies, wsc->wsc_ies_size,
    960 						NULL, pin_scan_results,
    961 						wsc, NULL);
    962 		return false;
    963 	}
    964 
    965 	wsc_cancel_scan(wsc);
    966 	station_set_scan_results(wsc->station, bss_list, freqs, false);
    967 
    968 	l_debug("Found AP to connect to: %s",
    969 			util_address_to_string(target->addr));
    970 	wsc_check_can_connect(wsc, target);
    971 
    972 	return true;
    973 }
    974 
    975 static bool wsc_initiate_scan(struct wsc_station_dbus *wsc,
    976 					enum wsc_device_password_id dpid,
    977 					scan_notify_func_t callback)
    978 {
    979 	static const uint8_t wfa_oui[] = { 0x00, 0x50, 0xF2 };
    980 	struct wsc_probe_request req;
    981 	struct wiphy *wiphy = netdev_get_wiphy(wsc->netdev);
    982 	uint32_t bands;
    983 	uint8_t *wsc_data;
    984 	size_t wsc_data_size;
    985 
    986 	memset(&req, 0, sizeof(req));
    987 
    988 	req.version2 = true;
    989 	req.request_type = WSC_REQUEST_TYPE_ENROLLEE_INFO;
    990 
    991 	/* TODO: Grab from configuration file ? */
    992 	req.config_methods = WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON |
    993 				WSC_CONFIGURATION_METHOD_KEYPAD;
    994 
    995 	if (!wsc_uuid_from_addr(netdev_get_address(wsc->netdev), req.uuid_e))
    996 		return false;
    997 
    998 	/* TODO: Grab from configuration file ? */
    999 	req.primary_device_type.category = 255;
   1000 	memcpy(req.primary_device_type.oui, wfa_oui, 3);
   1001 	req.primary_device_type.oui_type = 0x04;
   1002 	req.primary_device_type.subcategory = 0;
   1003 
   1004 	bands = wiphy_get_supported_bands(wiphy);
   1005 	if (bands & BAND_FREQ_2_4_GHZ)
   1006 		req.rf_bands |= WSC_RF_BAND_2_4_GHZ;
   1007 	if (bands & BAND_FREQ_5_GHZ)
   1008 		req.rf_bands |= WSC_RF_BAND_5_0_GHZ;
   1009 
   1010 	req.association_state = WSC_ASSOCIATION_STATE_NOT_ASSOCIATED;
   1011 	req.configuration_error = WSC_CONFIGURATION_ERROR_NO_ERROR;
   1012 	req.device_password_id = dpid;
   1013 	req.request_to_enroll = true;
   1014 
   1015 	wsc_data = wsc_build_probe_request(&req, &wsc_data_size);
   1016 	if (!wsc_data)
   1017 		return false;
   1018 
   1019 	wsc->wsc_ies = ie_tlv_encapsulate_wsc_payload(wsc_data, wsc_data_size,
   1020 							&wsc->wsc_ies_size);
   1021 	l_free(wsc_data);
   1022 
   1023 	if (!wsc->wsc_ies)
   1024 		return false;
   1025 
   1026 	wsc->scan_id = scan_active(netdev_get_wdev_id(wsc->netdev),
   1027 					wsc->wsc_ies, wsc->wsc_ies_size,
   1028 					NULL, callback, wsc, NULL);
   1029 	if (!wsc->scan_id) {
   1030 		l_free(wsc->wsc_ies);
   1031 		wsc->wsc_ies = NULL;
   1032 
   1033 		return false;
   1034 	}
   1035 
   1036 	return true;
   1037 }
   1038 
   1039 static const char *wsc_station_dbus_get_path(struct wsc_dbus *super)
   1040 {
   1041 	struct wsc_station_dbus *wsc =
   1042 		l_container_of(super, struct wsc_station_dbus, super);
   1043 
   1044 	return netdev_get_path(wsc->netdev);
   1045 }
   1046 
   1047 static void wsc_station_dbus_connect(struct wsc_dbus *super,
   1048 						const char *pin)
   1049 {
   1050 	struct wsc_station_dbus *wsc =
   1051 		l_container_of(super, struct wsc_station_dbus, super);
   1052 	scan_notify_func_t scan_callback;
   1053 	enum wsc_device_password_id dpid;
   1054 
   1055 	wsc->station = station_find(netdev_get_ifindex(wsc->netdev));
   1056 	if (!wsc->station) {
   1057 		CONNECT_REPLY(wsc, dbus_error_not_available);
   1058 		return;
   1059 	}
   1060 
   1061 	if (pin) {
   1062 		if (strlen(pin) == 4 || wsc_pin_is_checksum_valid(pin))
   1063 			dpid = WSC_DEVICE_PASSWORD_ID_DEFAULT;
   1064 		else
   1065 			dpid = WSC_DEVICE_PASSWORD_ID_USER_SPECIFIED;
   1066 
   1067 		scan_callback = pin_scan_results;
   1068 	} else {
   1069 		dpid = WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON;
   1070 		scan_callback = push_button_scan_results;
   1071 	}
   1072 
   1073 	if (!wsc_initiate_scan(wsc, dpid, scan_callback)) {
   1074 		CONNECT_REPLY(wsc, dbus_error_failed);
   1075 		return;
   1076 	}
   1077 
   1078 	if (pin) {
   1079 		wsc->walk_timer = l_timeout_create(60, pin_timeout, wsc, NULL);
   1080 	} else {
   1081 		wsc->walk_timer = l_timeout_create(WALK_TIME, walk_timeout, wsc,
   1082 							NULL);
   1083 	}
   1084 }
   1085 
   1086 static void wsc_station_dbus_cancel(struct wsc_dbus *super)
   1087 {
   1088 	struct wsc_station_dbus *wsc =
   1089 		l_container_of(super, struct wsc_station_dbus, super);
   1090 
   1091 	wsc_cancel_scan(wsc);
   1092 
   1093 	if (wsc->station_state_watch) {
   1094 		station_remove_state_watch(wsc->station,
   1095 						wsc->station_state_watch);
   1096 		wsc->station_state_watch = 0;
   1097 		wsc->target = NULL;
   1098 	}
   1099 
   1100 	CONNECT_REPLY(wsc, dbus_error_aborted);
   1101 
   1102 	if (wsc->enrollee)
   1103 		wsc_enrollee_cancel(wsc->enrollee, true);
   1104 	else
   1105 		CANCEL_REPLY(wsc, l_dbus_message_new_method_return);
   1106 }
   1107 
   1108 static void wsc_station_dbus_remove(struct wsc_dbus *super)
   1109 {
   1110 	struct wsc_station_dbus *wsc =
   1111 		l_container_of(super, struct wsc_station_dbus, super);
   1112 
   1113 	wsc_cancel_scan(wsc);
   1114 
   1115 	if (wsc->station_state_watch) {
   1116 		station_remove_state_watch(wsc->station,
   1117 						wsc->station_state_watch);
   1118 		wsc->station_state_watch = 0;
   1119 	}
   1120 
   1121 	if (wsc->enrollee)
   1122 		wsc_enrollee_free(wsc->enrollee);
   1123 
   1124 	l_free(wsc);
   1125 }
   1126 
   1127 static struct l_dbus_message *wsc_push_button(struct l_dbus *dbus,
   1128 						struct l_dbus_message *message,
   1129 						void *user_data)
   1130 {
   1131 	struct wsc_dbus *wsc = user_data;
   1132 
   1133 	l_debug("");
   1134 
   1135 	if (!l_dbus_message_get_arguments(message, ""))
   1136 		return dbus_error_invalid_args(message);
   1137 
   1138 	if (wsc->pending_connect || wsc->pending_cancel)
   1139 		return dbus_error_busy(message);
   1140 
   1141 	wsc->pending_connect = l_dbus_message_ref(message);
   1142 	wsc->connect(wsc, NULL);
   1143 	return NULL;
   1144 }
   1145 
   1146 static struct l_dbus_message *wsc_generate_pin(struct l_dbus *dbus,
   1147 						struct l_dbus_message *message,
   1148 						void *user_data)
   1149 {
   1150 	struct wsc_dbus *wsc = user_data;
   1151 	struct l_dbus_message *reply;
   1152 	char pin[9];
   1153 
   1154 	l_debug("");
   1155 
   1156 	if (wsc->pending_connect)
   1157 		return dbus_error_busy(message);
   1158 
   1159 	if (!wsc_pin_generate(pin))
   1160 		return dbus_error_failed(message);
   1161 
   1162 	reply = l_dbus_message_new_method_return(message);
   1163 	l_dbus_message_set_arguments(reply, "s", pin);
   1164 	explicit_bzero(pin, 9);
   1165 
   1166 	return reply;
   1167 }
   1168 
   1169 static struct l_dbus_message *wsc_start_pin(struct l_dbus *dbus,
   1170 						struct l_dbus_message *message,
   1171 						void *user_data)
   1172 {
   1173 	struct wsc_dbus *wsc = user_data;
   1174 	const char *pin;
   1175 
   1176 	l_debug("");
   1177 
   1178 	if (wsc->pending_connect || wsc->pending_cancel)
   1179 		return dbus_error_busy(message);
   1180 
   1181 	if (!l_dbus_message_get_arguments(message, "s", &pin))
   1182 		return dbus_error_invalid_args(message);
   1183 
   1184 	if (!wsc_pin_is_valid(pin))
   1185 		return dbus_error_invalid_format(message);
   1186 
   1187 	wsc->pending_connect = l_dbus_message_ref(message);
   1188 	wsc->connect(wsc, pin);
   1189 	return NULL;
   1190 }
   1191 
   1192 static struct l_dbus_message *wsc_cancel(struct l_dbus *dbus,
   1193 						struct l_dbus_message *message,
   1194 						void *user_data)
   1195 {
   1196 	struct wsc_dbus *wsc = user_data;
   1197 
   1198 	l_debug("");
   1199 
   1200 	if (!l_dbus_message_get_arguments(message, ""))
   1201 		return dbus_error_invalid_args(message);
   1202 
   1203 	if (!wsc->pending_connect)
   1204 		return dbus_error_not_available(message);
   1205 
   1206 	if (wsc->pending_cancel)
   1207 		return dbus_error_busy(message);
   1208 
   1209 	wsc->pending_cancel = l_dbus_message_ref(message);
   1210 	wsc->cancel(wsc);
   1211 	return NULL;
   1212 }
   1213 
   1214 static void setup_wsc_interface(struct l_dbus_interface *interface)
   1215 {
   1216 	l_dbus_interface_method(interface, "PushButton", 0,
   1217 				wsc_push_button, "", "");
   1218 	l_dbus_interface_method(interface, "GeneratePin", 0,
   1219 				wsc_generate_pin, "s", "", "pin");
   1220 	l_dbus_interface_method(interface, "StartPin", 0,
   1221 				wsc_start_pin, "", "s", "pin");
   1222 	l_dbus_interface_method(interface, "Cancel", 0,
   1223 				wsc_cancel, "", "");
   1224 }
   1225 
   1226 bool wsc_dbus_add_interface(struct wsc_dbus *wsc)
   1227 {
   1228 #ifdef HAVE_DBUS
   1229 	struct l_dbus *dbus = dbus_get_bus();
   1230 
   1231 	if (!l_dbus_object_add_interface(dbus, wsc->get_path(wsc),
   1232 						IWD_WSC_INTERFACE, wsc)) {
   1233 		l_info("Unable to register %s interface", IWD_WSC_INTERFACE);
   1234 		return false;
   1235 	}
   1236 #endif
   1237 
   1238 	return true;
   1239 }
   1240 
   1241 void wsc_dbus_remove_interface(struct wsc_dbus *wsc)
   1242 {
   1243 #ifdef HAVE_DBUS
   1244 	struct l_dbus *dbus = dbus_get_bus();
   1245 
   1246 	l_dbus_object_remove_interface(dbus, wsc->get_path(wsc),
   1247 					IWD_WSC_INTERFACE);
   1248 #endif
   1249 }
   1250 
   1251 static void wsc_dbus_free(void *user_data)
   1252 {
   1253 	struct wsc_dbus *wsc = user_data;
   1254 
   1255 	if (wsc->pending_connect)
   1256 		dbus_pending_reply(&wsc->pending_connect,
   1257 				dbus_error_not_available(wsc->pending_connect));
   1258 
   1259 	if (wsc->pending_cancel)
   1260 		dbus_pending_reply(&wsc->pending_cancel,
   1261 				dbus_error_aborted(wsc->pending_cancel));
   1262 
   1263 	wsc->remove(wsc);
   1264 }
   1265 
   1266 static void wsc_add_station(struct netdev *netdev)
   1267 {
   1268 	struct wsc_station_dbus *wsc;
   1269 
   1270 	if (!wiphy_get_max_scan_ie_len(netdev_get_wiphy(netdev))) {
   1271 		l_debug("Simple Configuration isn't supported by ifindex %u",
   1272 						netdev_get_ifindex(netdev));
   1273 
   1274 		return;
   1275 	}
   1276 
   1277 	wsc = l_new(struct wsc_station_dbus, 1);
   1278 	wsc->netdev = netdev;
   1279 	wsc->super.get_path = wsc_station_dbus_get_path;
   1280 	wsc->super.connect = wsc_station_dbus_connect;
   1281 	wsc->super.cancel = wsc_station_dbus_cancel;
   1282 	wsc->super.remove = wsc_station_dbus_remove;
   1283 
   1284 	if (!wsc_dbus_add_interface(&wsc->super))
   1285 		wsc_station_dbus_remove(&wsc->super);
   1286 }
   1287 
   1288 static void wsc_remove_station(struct netdev *netdev)
   1289 {
   1290 	struct l_dbus *dbus = dbus_get_bus();
   1291 
   1292 	l_dbus_object_remove_interface(dbus, netdev_get_path(netdev),
   1293 					IWD_WSC_INTERFACE);
   1294 }
   1295 
   1296 static void wsc_netdev_watch(struct netdev *netdev,
   1297 				enum netdev_watch_event event, void *userdata)
   1298 {
   1299 	switch (event) {
   1300 	case NETDEV_WATCH_EVENT_UP:
   1301 #ifdef HAVE_DBUS
   1302 	case NETDEV_WATCH_EVENT_NEW:
   1303 		if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_STATION &&
   1304 				netdev_get_is_up(netdev))
   1305 			wsc_add_station(netdev);
   1306 		break;
   1307 	case NETDEV_WATCH_EVENT_DOWN:
   1308 	case NETDEV_WATCH_EVENT_DEL:
   1309 		wsc_remove_station(netdev);
   1310 		break;
   1311 #endif
   1312 	default:
   1313 		break;
   1314 	}
   1315 }
   1316 
   1317 static int wsc_init(void)
   1318 {
   1319 	l_debug("");
   1320 	netdev_watch = netdev_watch_add(wsc_netdev_watch, NULL, NULL);
   1321 #ifdef HAVE_DBUS
   1322 	l_dbus_register_interface(dbus_get_bus(), IWD_WSC_INTERFACE,
   1323 					setup_wsc_interface,
   1324 					wsc_dbus_free, false);
   1325 #endif
   1326 	return 0;
   1327 }
   1328 
   1329 static void wsc_exit(void)
   1330 {
   1331 	l_debug("");
   1332 #ifdef HAVE_DBUS
   1333 	l_dbus_unregister_interface(dbus_get_bus(), IWD_WSC_INTERFACE);
   1334 #endif
   1335 	netdev_watch_remove(netdev_watch);
   1336 }
   1337 
   1338 IWD_MODULE(wsc, wsc_init, wsc_exit)
   1339 IWD_MODULE_DEPENDS(wsc, netdev);