Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * wpa_supplicant / WPS integration
      3  * Copyright (c) 2008-2014, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include "includes.h"
     10 
     11 #include "common.h"
     12 #include "eloop.h"
     13 #include "uuid.h"
     14 #include "crypto/random.h"
     15 #include "crypto/dh_group5.h"
     16 #include "common/ieee802_11_defs.h"
     17 #include "common/ieee802_11_common.h"
     18 #include "common/wpa_common.h"
     19 #include "common/wpa_ctrl.h"
     20 #include "eap_common/eap_wsc_common.h"
     21 #include "eap_peer/eap.h"
     22 #include "eapol_supp/eapol_supp_sm.h"
     23 #include "rsn_supp/wpa.h"
     24 #include "wps/wps_attr_parse.h"
     25 #include "config.h"
     26 #include "wpa_supplicant_i.h"
     27 #include "driver_i.h"
     28 #include "notify.h"
     29 #include "blacklist.h"
     30 #include "bss.h"
     31 #include "scan.h"
     32 #include "ap.h"
     33 #include "p2p/p2p.h"
     34 #include "p2p_supplicant.h"
     35 #include "wps_supplicant.h"
     36 
     37 
     38 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
     39 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
     40 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
     41 
     42 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
     43 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
     44 
     45 
     46 static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
     47 {
     48 	os_free(wpa_s->wps_ap);
     49 	wpa_s->wps_ap = NULL;
     50 	wpa_s->num_wps_ap = 0;
     51 	wpa_s->wps_ap_iter = 0;
     52 }
     53 
     54 
     55 static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
     56 {
     57 	struct wpa_supplicant *wpa_s = eloop_ctx;
     58 	int use_fast_assoc = timeout_ctx != NULL;
     59 
     60 	wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
     61 	if (!use_fast_assoc ||
     62 	    wpa_supplicant_fast_associate(wpa_s) != 1)
     63 		wpa_supplicant_req_scan(wpa_s, 0, 0);
     64 }
     65 
     66 
     67 static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
     68 {
     69 	eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
     70 	eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
     71 }
     72 
     73 
     74 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
     75 {
     76 	if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
     77 		return 1;
     78 
     79 	if (!wpa_s->wps_success &&
     80 	    wpa_s->current_ssid &&
     81 	    eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
     82 		const u8 *bssid = wpa_s->bssid;
     83 		if (is_zero_ether_addr(bssid))
     84 			bssid = wpa_s->pending_bssid;
     85 
     86 		wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
     87 			   " did not succeed - continue trying to find "
     88 			   "suitable AP", MAC2STR(bssid));
     89 		wpa_blacklist_add(wpa_s, bssid);
     90 
     91 		wpa_supplicant_deauthenticate(wpa_s,
     92 					      WLAN_REASON_DEAUTH_LEAVING);
     93 		wpa_s->reassociate = 1;
     94 		wpa_supplicant_req_scan(wpa_s,
     95 					wpa_s->blacklist_cleared ? 5 : 0, 0);
     96 		wpa_s->blacklist_cleared = 0;
     97 		return 1;
     98 	}
     99 
    100 	wpas_wps_clear_ap_info(wpa_s);
    101 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
    102 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
    103 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
    104 
    105 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
    106 	    !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
    107 		int disabled = wpa_s->current_ssid->disabled;
    108 		unsigned int freq = wpa_s->assoc_freq;
    109 		struct wpa_bss *bss;
    110 		struct wpa_ssid *ssid = NULL;
    111 		int use_fast_assoc = 0;
    112 
    113 		wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
    114 			   "try to associate with the received credential "
    115 			   "(freq=%u)", freq);
    116 		wpa_supplicant_deauthenticate(wpa_s,
    117 					      WLAN_REASON_DEAUTH_LEAVING);
    118 		if (disabled) {
    119 			wpa_printf(MSG_DEBUG, "WPS: Current network is "
    120 				   "disabled - wait for user to enable");
    121 			return 1;
    122 		}
    123 		wpa_s->after_wps = 5;
    124 		wpa_s->wps_freq = freq;
    125 		wpa_s->normal_scans = 0;
    126 		wpa_s->reassociate = 1;
    127 
    128 		wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
    129 			   "without a new scan can be used");
    130 		bss = wpa_supplicant_pick_network(wpa_s, &ssid);
    131 		if (bss) {
    132 			struct wpabuf *wps;
    133 			struct wps_parse_attr attr;
    134 
    135 			wps = wpa_bss_get_vendor_ie_multi(bss,
    136 							  WPS_IE_VENDOR_TYPE);
    137 			if (wps && wps_parse_msg(wps, &attr) == 0 &&
    138 			    attr.wps_state &&
    139 			    *attr.wps_state == WPS_STATE_CONFIGURED)
    140 				use_fast_assoc = 1;
    141 			wpabuf_free(wps);
    142 		}
    143 
    144 		/*
    145 		 * Complete the next step from an eloop timeout to allow pending
    146 		 * driver events related to the disconnection to be processed
    147 		 * first. This makes it less likely for disconnection event to
    148 		 * cause problems with the following connection.
    149 		 */
    150 		wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
    151 		wpas_wps_assoc_with_cred_cancel(wpa_s);
    152 		eloop_register_timeout(0, 10000,
    153 				       wpas_wps_assoc_with_cred, wpa_s,
    154 				       use_fast_assoc ? (void *) 1 :
    155 				       (void *) 0);
    156 		return 1;
    157 	}
    158 
    159 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
    160 		wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
    161 			   "for external credential processing");
    162 		wpas_clear_wps(wpa_s);
    163 		wpa_supplicant_deauthenticate(wpa_s,
    164 					      WLAN_REASON_DEAUTH_LEAVING);
    165 		return 1;
    166 	}
    167 
    168 	return 0;
    169 }
    170 
    171 
    172 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
    173 					 struct wpa_ssid *ssid,
    174 					 const struct wps_credential *cred)
    175 {
    176 	struct wpa_driver_capa capa;
    177 	struct wpa_bss *bss;
    178 	const u8 *ie;
    179 	struct wpa_ie_data adv;
    180 	int wpa2 = 0, ccmp = 0;
    181 
    182 	/*
    183 	 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
    184 	 * case they are configured for mixed mode operation (WPA+WPA2 and
    185 	 * TKIP+CCMP). Try to use scan results to figure out whether the AP
    186 	 * actually supports stronger security and select that if the client
    187 	 * has support for it, too.
    188 	 */
    189 
    190 	if (wpa_drv_get_capa(wpa_s, &capa))
    191 		return; /* Unknown what driver supports */
    192 
    193 	if (ssid->ssid == NULL)
    194 		return;
    195 	bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
    196 	if (bss == NULL) {
    197 		wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
    198 			   "table - use credential as-is");
    199 		return;
    200 	}
    201 
    202 	wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
    203 
    204 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
    205 	if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
    206 		wpa2 = 1;
    207 		if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
    208 			ccmp = 1;
    209 	} else {
    210 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
    211 		if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
    212 		    adv.pairwise_cipher & WPA_CIPHER_CCMP)
    213 			ccmp = 1;
    214 	}
    215 
    216 	if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
    217 	    (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
    218 		/*
    219 		 * TODO: This could be the initial AP configuration and the
    220 		 * Beacon contents could change shortly. Should request a new
    221 		 * scan and delay addition of the network until the updated
    222 		 * scan results are available.
    223 		 */
    224 		wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
    225 			   "support - use credential as-is");
    226 		return;
    227 	}
    228 
    229 	if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
    230 	    (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
    231 	    (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
    232 		wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
    233 			   "based on scan results");
    234 		if (wpa_s->conf->ap_scan == 1)
    235 			ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
    236 		else
    237 			ssid->pairwise_cipher = WPA_CIPHER_CCMP;
    238 	}
    239 
    240 	if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
    241 	    (ssid->proto & WPA_PROTO_WPA) &&
    242 	    (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
    243 		wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
    244 			   "based on scan results");
    245 		if (wpa_s->conf->ap_scan == 1)
    246 			ssid->proto |= WPA_PROTO_RSN;
    247 		else
    248 			ssid->proto = WPA_PROTO_RSN;
    249 	}
    250 }
    251 
    252 
    253 static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
    254 					struct wpa_ssid *new_ssid)
    255 {
    256 	struct wpa_ssid *ssid, *next;
    257 
    258 	for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
    259 	     ssid = next, next = ssid ? ssid->next : NULL) {
    260 		/*
    261 		 * new_ssid has already been added to the list in
    262 		 * wpas_wps_add_network(), so skip it.
    263 		 */
    264 		if (ssid == new_ssid)
    265 			continue;
    266 
    267 		if (ssid->bssid_set || new_ssid->bssid_set) {
    268 			if (ssid->bssid_set != new_ssid->bssid_set)
    269 				continue;
    270 			if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
    271 			    0)
    272 				continue;
    273 		}
    274 
    275 		/* compare SSID */
    276 		if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
    277 			continue;
    278 
    279 		if (ssid->ssid && new_ssid->ssid) {
    280 			if (os_memcmp(ssid->ssid, new_ssid->ssid,
    281 				      ssid->ssid_len) != 0)
    282 				continue;
    283 		} else if (ssid->ssid || new_ssid->ssid)
    284 			continue;
    285 
    286 		/* compare security parameters */
    287 		if (ssid->auth_alg != new_ssid->auth_alg ||
    288 		    ssid->key_mgmt != new_ssid->key_mgmt ||
    289 		    ssid->group_cipher != new_ssid->group_cipher)
    290 			continue;
    291 
    292 		/*
    293 		 * Some existing WPS APs will send two creds in case they are
    294 		 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
    295 		 * Try to merge these two creds if they are received in the same
    296 		 * M8 message.
    297 		 */
    298 		if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
    299 		    wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
    300 			if (new_ssid->passphrase && ssid->passphrase &&
    301 			    os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
    302 			    0) {
    303 				wpa_printf(MSG_DEBUG,
    304 					   "WPS: M8 Creds with different passphrase - do not merge");
    305 				continue;
    306 			}
    307 
    308 			if (new_ssid->psk_set &&
    309 			    (!ssid->psk_set ||
    310 			     os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
    311 				wpa_printf(MSG_DEBUG,
    312 					   "WPS: M8 Creds with different PSK - do not merge");
    313 				continue;
    314 			}
    315 
    316 			if ((new_ssid->passphrase && !ssid->passphrase) ||
    317 			    (!new_ssid->passphrase && ssid->passphrase)) {
    318 				wpa_printf(MSG_DEBUG,
    319 					   "WPS: M8 Creds with different passphrase/PSK type - do not merge");
    320 				continue;
    321 			}
    322 
    323 			wpa_printf(MSG_DEBUG,
    324 				   "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
    325 			new_ssid->proto |= ssid->proto;
    326 			new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
    327 		} else {
    328 			/*
    329 			 * proto and pairwise_cipher difference matter for
    330 			 * non-mixed-mode creds.
    331 			 */
    332 			if (ssid->proto != new_ssid->proto ||
    333 			    ssid->pairwise_cipher != new_ssid->pairwise_cipher)
    334 				continue;
    335 		}
    336 
    337 		/* Remove the duplicated older network entry. */
    338 		wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
    339 		wpas_notify_network_removed(wpa_s, ssid);
    340 		wpa_config_remove_network(wpa_s->conf, ssid->id);
    341 	}
    342 }
    343 
    344 
    345 static int wpa_supplicant_wps_cred(void *ctx,
    346 				   const struct wps_credential *cred)
    347 {
    348 	struct wpa_supplicant *wpa_s = ctx;
    349 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    350 	u16 auth_type;
    351 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
    352 	int registrar = 0;
    353 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
    354 
    355 	if ((wpa_s->conf->wps_cred_processing == 1 ||
    356 	     wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
    357 		size_t blen = cred->cred_attr_len * 2 + 1;
    358 		char *buf = os_malloc(blen);
    359 		if (buf) {
    360 			wpa_snprintf_hex(buf, blen,
    361 					 cred->cred_attr, cred->cred_attr_len);
    362 			wpa_msg(wpa_s, MSG_INFO, "%s%s",
    363 				WPS_EVENT_CRED_RECEIVED, buf);
    364 			os_free(buf);
    365 		}
    366 
    367 		wpas_notify_wps_credential(wpa_s, cred);
    368 	} else
    369 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
    370 
    371 	wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
    372 			cred->cred_attr, cred->cred_attr_len);
    373 
    374 	if (wpa_s->conf->wps_cred_processing == 1)
    375 		return 0;
    376 
    377 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
    378 	wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
    379 		   cred->auth_type);
    380 	wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
    381 	wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
    382 	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
    383 			cred->key, cred->key_len);
    384 	wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
    385 		   MAC2STR(cred->mac_addr));
    386 
    387 	auth_type = cred->auth_type;
    388 	if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
    389 		wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
    390 			   "auth_type into WPA2PSK");
    391 		auth_type = WPS_AUTH_WPA2PSK;
    392 	}
    393 
    394 	if (auth_type != WPS_AUTH_OPEN &&
    395 	    auth_type != WPS_AUTH_WPAPSK &&
    396 	    auth_type != WPS_AUTH_WPA2PSK) {
    397 		wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
    398 			   "unsupported authentication type 0x%x",
    399 			   auth_type);
    400 		return 0;
    401 	}
    402 
    403 	if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
    404 		if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
    405 			wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
    406 				   "invalid Network Key length %lu",
    407 				   (unsigned long) cred->key_len);
    408 			return -1;
    409 		}
    410 	}
    411 
    412 	if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
    413 		wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
    414 			   "on the received credential");
    415 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
    416 		if (ssid->eap.identity &&
    417 		    ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
    418 		    os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
    419 			      WSC_ID_REGISTRAR_LEN) == 0)
    420 			registrar = 1;
    421 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
    422 		os_free(ssid->eap.identity);
    423 		ssid->eap.identity = NULL;
    424 		ssid->eap.identity_len = 0;
    425 		os_free(ssid->eap.phase1);
    426 		ssid->eap.phase1 = NULL;
    427 		os_free(ssid->eap.eap_methods);
    428 		ssid->eap.eap_methods = NULL;
    429 		if (!ssid->p2p_group) {
    430 			ssid->temporary = 0;
    431 			ssid->bssid_set = 0;
    432 		}
    433 		ssid->disabled_until.sec = 0;
    434 		ssid->disabled_until.usec = 0;
    435 		ssid->auth_failures = 0;
    436 	} else {
    437 		wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
    438 			   "received credential");
    439 		ssid = wpa_config_add_network(wpa_s->conf);
    440 		if (ssid == NULL)
    441 			return -1;
    442 		if (wpa_s->current_ssid) {
    443 			/*
    444 			 * Should the GO issue multiple credentials for some
    445 			 * reason, each credential should be marked as a
    446 			 * temporary P2P group similarly to the one that gets
    447 			 * marked as such based on the pre-configured values
    448 			 * used for the WPS network block.
    449 			 */
    450 			ssid->p2p_group = wpa_s->current_ssid->p2p_group;
    451 			ssid->temporary = wpa_s->current_ssid->temporary;
    452 		}
    453 		wpas_notify_network_added(wpa_s, ssid);
    454 	}
    455 
    456 	wpa_config_set_network_defaults(ssid);
    457 	ssid->wps_run = wpa_s->wps_run;
    458 
    459 	os_free(ssid->ssid);
    460 	ssid->ssid = os_malloc(cred->ssid_len);
    461 	if (ssid->ssid) {
    462 		os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
    463 		ssid->ssid_len = cred->ssid_len;
    464 	}
    465 
    466 	switch (cred->encr_type) {
    467 	case WPS_ENCR_NONE:
    468 		break;
    469 	case WPS_ENCR_TKIP:
    470 		ssid->pairwise_cipher = WPA_CIPHER_TKIP;
    471 		break;
    472 	case WPS_ENCR_AES:
    473 		ssid->pairwise_cipher = WPA_CIPHER_CCMP;
    474 		break;
    475 	}
    476 
    477 	switch (auth_type) {
    478 	case WPS_AUTH_OPEN:
    479 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
    480 		ssid->key_mgmt = WPA_KEY_MGMT_NONE;
    481 		ssid->proto = 0;
    482 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
    483 		if (registrar) {
    484 			wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
    485 				"id=%d - Credentials for an open "
    486 				"network disabled by default - use "
    487 				"'select_network %d' to enable",
    488 				ssid->id, ssid->id);
    489 			ssid->disabled = 1;
    490 		}
    491 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
    492 		break;
    493 	case WPS_AUTH_WPAPSK:
    494 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
    495 		ssid->key_mgmt = WPA_KEY_MGMT_PSK;
    496 		ssid->proto = WPA_PROTO_WPA;
    497 		break;
    498 	case WPS_AUTH_WPA2PSK:
    499 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
    500 		ssid->key_mgmt = WPA_KEY_MGMT_PSK;
    501 		ssid->proto = WPA_PROTO_RSN;
    502 		break;
    503 	}
    504 
    505 	if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
    506 		if (cred->key_len == 2 * PMK_LEN) {
    507 			if (hexstr2bin((const char *) cred->key, ssid->psk,
    508 				       PMK_LEN)) {
    509 				wpa_printf(MSG_ERROR, "WPS: Invalid Network "
    510 					   "Key");
    511 				return -1;
    512 			}
    513 			ssid->psk_set = 1;
    514 			ssid->export_keys = 1;
    515 		} else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
    516 			os_free(ssid->passphrase);
    517 			ssid->passphrase = os_malloc(cred->key_len + 1);
    518 			if (ssid->passphrase == NULL)
    519 				return -1;
    520 			os_memcpy(ssid->passphrase, cred->key, cred->key_len);
    521 			ssid->passphrase[cred->key_len] = '\0';
    522 			wpa_config_update_psk(ssid);
    523 			ssid->export_keys = 1;
    524 		} else {
    525 			wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
    526 				   "length %lu",
    527 				   (unsigned long) cred->key_len);
    528 			return -1;
    529 		}
    530 	}
    531 
    532 	wpas_wps_security_workaround(wpa_s, ssid, cred);
    533 
    534 	wpas_wps_remove_dup_network(wpa_s, ssid);
    535 
    536 #ifndef CONFIG_NO_CONFIG_WRITE
    537 	if (wpa_s->conf->update_config &&
    538 	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
    539 		wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
    540 		return -1;
    541 	}
    542 #endif /* CONFIG_NO_CONFIG_WRITE */
    543 
    544 	/*
    545 	 * Optimize the post-WPS scan based on the channel used during
    546 	 * the provisioning in case EAP-Failure is not received.
    547 	 */
    548 	wpa_s->after_wps = 5;
    549 	wpa_s->wps_freq = wpa_s->assoc_freq;
    550 
    551 	return 0;
    552 }
    553 
    554 
    555 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
    556 					 struct wps_event_m2d *m2d)
    557 {
    558 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
    559 		"dev_password_id=%d config_error=%d",
    560 		m2d->dev_password_id, m2d->config_error);
    561 	wpas_notify_wps_event_m2d(wpa_s, m2d);
    562 #ifdef CONFIG_P2P
    563 	if (wpa_s->parent && wpa_s->parent != wpa_s) {
    564 		wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
    565 			"dev_password_id=%d config_error=%d",
    566 			m2d->dev_password_id, m2d->config_error);
    567 	}
    568 	if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
    569 		/*
    570 		 * Notify P2P from eloop timeout to avoid issues with the
    571 		 * interface getting removed while processing a message.
    572 		 */
    573 		eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
    574 				       NULL);
    575 	}
    576 #endif /* CONFIG_P2P */
    577 }
    578 
    579 
    580 static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
    581 {
    582 	struct wpa_supplicant *wpa_s = eloop_ctx;
    583 	wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
    584 	wpas_clear_wps(wpa_s);
    585 }
    586 
    587 
    588 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
    589 					  struct wps_event_fail *fail)
    590 {
    591 	if (fail->error_indication > 0 &&
    592 	    fail->error_indication < NUM_WPS_EI_VALUES) {
    593 		wpa_msg(wpa_s, MSG_INFO,
    594 			WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
    595 			fail->msg, fail->config_error, fail->error_indication,
    596 			wps_ei_str(fail->error_indication));
    597 		if (wpa_s->parent && wpa_s->parent != wpa_s)
    598 			wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
    599 				"msg=%d config_error=%d reason=%d (%s)",
    600 				fail->msg, fail->config_error,
    601 				fail->error_indication,
    602 				wps_ei_str(fail->error_indication));
    603 	} else {
    604 		wpa_msg(wpa_s, MSG_INFO,
    605 			WPS_EVENT_FAIL "msg=%d config_error=%d",
    606 			fail->msg, fail->config_error);
    607 		if (wpa_s->parent && wpa_s->parent != wpa_s)
    608 			wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
    609 				"msg=%d config_error=%d",
    610 				fail->msg, fail->config_error);
    611 	}
    612 
    613 	/*
    614 	 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
    615 	 */
    616 	wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
    617 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
    618 	eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
    619 
    620 	wpas_notify_wps_event_fail(wpa_s, fail);
    621 	wpas_p2p_wps_failed(wpa_s, fail);
    622 }
    623 
    624 
    625 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
    626 
    627 static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
    628 {
    629 	struct wpa_ssid *ssid;
    630 	int changed = 0;
    631 
    632 	eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
    633 
    634 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
    635 		if (ssid->disabled_for_connect && ssid->disabled) {
    636 			ssid->disabled_for_connect = 0;
    637 			ssid->disabled = 0;
    638 			wpas_notify_network_enabled_changed(wpa_s, ssid);
    639 			changed++;
    640 		}
    641 	}
    642 
    643 	if (changed) {
    644 #ifndef CONFIG_NO_CONFIG_WRITE
    645 		if (wpa_s->conf->update_config &&
    646 		    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
    647 			wpa_printf(MSG_DEBUG, "WPS: Failed to update "
    648 				   "configuration");
    649 		}
    650 #endif /* CONFIG_NO_CONFIG_WRITE */
    651 	}
    652 }
    653 
    654 
    655 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
    656 {
    657 	struct wpa_supplicant *wpa_s = eloop_ctx;
    658 	/* Enable the networks disabled during wpas_wps_reassoc */
    659 	wpas_wps_reenable_networks(wpa_s);
    660 }
    661 
    662 
    663 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
    664 {
    665 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
    666 	wpa_s->wps_success = 1;
    667 	wpas_notify_wps_event_success(wpa_s);
    668 	if (wpa_s->current_ssid)
    669 		wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
    670 	wpa_s->extra_blacklist_count = 0;
    671 
    672 	/*
    673 	 * Enable the networks disabled during wpas_wps_reassoc after 10
    674 	 * seconds. The 10 seconds timer is to allow the data connection to be
    675 	 * formed before allowing other networks to be selected.
    676 	 */
    677 	eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
    678 			       NULL);
    679 
    680 	wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
    681 }
    682 
    683 
    684 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
    685 					       struct wps_event_er_ap *ap)
    686 {
    687 	char uuid_str[100];
    688 	char dev_type[WPS_DEV_TYPE_BUFSIZE];
    689 
    690 	uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
    691 	if (ap->pri_dev_type)
    692 		wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
    693 				     sizeof(dev_type));
    694 	else
    695 		dev_type[0] = '\0';
    696 
    697 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
    698 		" pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
    699 		uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
    700 		ap->friendly_name ? ap->friendly_name : "",
    701 		ap->manufacturer ? ap->manufacturer : "",
    702 		ap->model_description ? ap->model_description : "",
    703 		ap->model_name ? ap->model_name : "",
    704 		ap->manufacturer_url ? ap->manufacturer_url : "",
    705 		ap->model_url ? ap->model_url : "");
    706 }
    707 
    708 
    709 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
    710 						  struct wps_event_er_ap *ap)
    711 {
    712 	char uuid_str[100];
    713 	uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
    714 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
    715 }
    716 
    717 
    718 static void wpa_supplicant_wps_event_er_enrollee_add(
    719 	struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
    720 {
    721 	char uuid_str[100];
    722 	char dev_type[WPS_DEV_TYPE_BUFSIZE];
    723 
    724 	uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
    725 	if (enrollee->pri_dev_type)
    726 		wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
    727 				     sizeof(dev_type));
    728 	else
    729 		dev_type[0] = '\0';
    730 
    731 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
    732 		" M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
    733 		"|%s|%s|%s|%s|%s|",
    734 		uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
    735 		enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
    736 		enrollee->dev_name ? enrollee->dev_name : "",
    737 		enrollee->manufacturer ? enrollee->manufacturer : "",
    738 		enrollee->model_name ? enrollee->model_name : "",
    739 		enrollee->model_number ? enrollee->model_number : "",
    740 		enrollee->serial_number ? enrollee->serial_number : "");
    741 }
    742 
    743 
    744 static void wpa_supplicant_wps_event_er_enrollee_remove(
    745 	struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
    746 {
    747 	char uuid_str[100];
    748 	uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
    749 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
    750 		uuid_str, MAC2STR(enrollee->mac_addr));
    751 }
    752 
    753 
    754 static void wpa_supplicant_wps_event_er_ap_settings(
    755 	struct wpa_supplicant *wpa_s,
    756 	struct wps_event_er_ap_settings *ap_settings)
    757 {
    758 	char uuid_str[100];
    759 	char key_str[65];
    760 	const struct wps_credential *cred = ap_settings->cred;
    761 
    762 	key_str[0] = '\0';
    763 	if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
    764 		if (cred->key_len >= 8 && cred->key_len <= 64) {
    765 			os_memcpy(key_str, cred->key, cred->key_len);
    766 			key_str[cred->key_len] = '\0';
    767 		}
    768 	}
    769 
    770 	uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
    771 	/* Use wpa_msg_ctrl to avoid showing the key in debug log */
    772 	wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
    773 		     "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
    774 		     "key=%s",
    775 		     uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
    776 		     cred->auth_type, cred->encr_type, key_str);
    777 }
    778 
    779 
    780 static void wpa_supplicant_wps_event_er_set_sel_reg(
    781 	struct wpa_supplicant *wpa_s,
    782 	struct wps_event_er_set_selected_registrar *ev)
    783 {
    784 	char uuid_str[100];
    785 
    786 	uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
    787 	switch (ev->state) {
    788 	case WPS_ER_SET_SEL_REG_START:
    789 		wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
    790 			"uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
    791 			"sel_reg_config_methods=0x%x",
    792 			uuid_str, ev->sel_reg, ev->dev_passwd_id,
    793 			ev->sel_reg_config_methods);
    794 		break;
    795 	case WPS_ER_SET_SEL_REG_DONE:
    796 		wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
    797 			"uuid=%s state=DONE", uuid_str);
    798 		break;
    799 	case WPS_ER_SET_SEL_REG_FAILED:
    800 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
    801 			"uuid=%s state=FAILED", uuid_str);
    802 		break;
    803 	}
    804 }
    805 
    806 
    807 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
    808 				     union wps_event_data *data)
    809 {
    810 	struct wpa_supplicant *wpa_s = ctx;
    811 	switch (event) {
    812 	case WPS_EV_M2D:
    813 		wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
    814 		break;
    815 	case WPS_EV_FAIL:
    816 		wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
    817 		break;
    818 	case WPS_EV_SUCCESS:
    819 		wpa_supplicant_wps_event_success(wpa_s);
    820 		break;
    821 	case WPS_EV_PWD_AUTH_FAIL:
    822 #ifdef CONFIG_AP
    823 		if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
    824 			wpa_supplicant_ap_pwd_auth_fail(wpa_s);
    825 #endif /* CONFIG_AP */
    826 		break;
    827 	case WPS_EV_PBC_OVERLAP:
    828 		break;
    829 	case WPS_EV_PBC_TIMEOUT:
    830 		break;
    831 	case WPS_EV_PBC_ACTIVE:
    832 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
    833 		break;
    834 	case WPS_EV_PBC_DISABLE:
    835 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
    836 		break;
    837 	case WPS_EV_ER_AP_ADD:
    838 		wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
    839 		break;
    840 	case WPS_EV_ER_AP_REMOVE:
    841 		wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
    842 		break;
    843 	case WPS_EV_ER_ENROLLEE_ADD:
    844 		wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
    845 							 &data->enrollee);
    846 		break;
    847 	case WPS_EV_ER_ENROLLEE_REMOVE:
    848 		wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
    849 							    &data->enrollee);
    850 		break;
    851 	case WPS_EV_ER_AP_SETTINGS:
    852 		wpa_supplicant_wps_event_er_ap_settings(wpa_s,
    853 							&data->ap_settings);
    854 		break;
    855 	case WPS_EV_ER_SET_SELECTED_REGISTRAR:
    856 		wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
    857 							&data->set_sel_reg);
    858 		break;
    859 	case WPS_EV_AP_PIN_SUCCESS:
    860 		break;
    861 	}
    862 }
    863 
    864 
    865 static int wpa_supplicant_wps_rf_band(void *ctx)
    866 {
    867 	struct wpa_supplicant *wpa_s = ctx;
    868 
    869 	if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
    870 		return 0;
    871 
    872 	return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
    873 }
    874 
    875 
    876 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
    877 {
    878 	if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
    879 	    eap_is_wps_pin_enrollee(&ssid->eap))
    880 		return WPS_REQ_ENROLLEE;
    881 	else
    882 		return WPS_REQ_REGISTRAR;
    883 }
    884 
    885 
    886 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
    887 {
    888 	int id;
    889 	struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
    890 
    891 	wpa_s->after_wps = 0;
    892 	wpa_s->known_wps_freq = 0;
    893 
    894 	prev_current = wpa_s->current_ssid;
    895 
    896 	/* Enable the networks disabled during wpas_wps_reassoc */
    897 	wpas_wps_reenable_networks(wpa_s);
    898 
    899 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
    900 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
    901 
    902 	/* Remove any existing WPS network from configuration */
    903 	ssid = wpa_s->conf->ssid;
    904 	while (ssid) {
    905 		if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
    906 			if (ssid == wpa_s->current_ssid) {
    907 				wpa_supplicant_deauthenticate(
    908 					wpa_s, WLAN_REASON_DEAUTH_LEAVING);
    909 			}
    910 			id = ssid->id;
    911 			remove_ssid = ssid;
    912 		} else
    913 			id = -1;
    914 		ssid = ssid->next;
    915 		if (id >= 0) {
    916 			if (prev_current == remove_ssid) {
    917 				wpa_sm_set_config(wpa_s->wpa, NULL);
    918 				eapol_sm_notify_config(wpa_s->eapol, NULL,
    919 						       NULL);
    920 			}
    921 			wpas_notify_network_removed(wpa_s, remove_ssid);
    922 			wpa_config_remove_network(wpa_s->conf, id);
    923 		}
    924 	}
    925 
    926 	wpas_wps_clear_ap_info(wpa_s);
    927 }
    928 
    929 
    930 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
    931 {
    932 	struct wpa_supplicant *wpa_s = eloop_ctx;
    933 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
    934 		"out");
    935 	wpas_clear_wps(wpa_s);
    936 }
    937 
    938 
    939 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
    940 					      int registrar, const u8 *dev_addr,
    941 					      const u8 *bssid)
    942 {
    943 	struct wpa_ssid *ssid;
    944 
    945 	ssid = wpa_config_add_network(wpa_s->conf);
    946 	if (ssid == NULL)
    947 		return NULL;
    948 	wpas_notify_network_added(wpa_s, ssid);
    949 	wpa_config_set_network_defaults(ssid);
    950 	ssid->temporary = 1;
    951 	if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
    952 	    wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
    953 	    wpa_config_set(ssid, "identity", registrar ?
    954 			   "\"" WSC_ID_REGISTRAR "\"" :
    955 			   "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
    956 		wpas_notify_network_removed(wpa_s, ssid);
    957 		wpa_config_remove_network(wpa_s->conf, ssid->id);
    958 		return NULL;
    959 	}
    960 
    961 #ifdef CONFIG_P2P
    962 	if (dev_addr)
    963 		os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
    964 #endif /* CONFIG_P2P */
    965 
    966 	if (bssid) {
    967 #ifndef CONFIG_P2P
    968 		struct wpa_bss *bss;
    969 		int count = 0;
    970 #endif /* CONFIG_P2P */
    971 
    972 		os_memcpy(ssid->bssid, bssid, ETH_ALEN);
    973 		ssid->bssid_set = 1;
    974 
    975 		/*
    976 		 * Note: With P2P, the SSID may change at the time the WPS
    977 		 * provisioning is started, so better not filter the AP based
    978 		 * on the current SSID in the scan results.
    979 		 */
    980 #ifndef CONFIG_P2P
    981 		dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
    982 			if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
    983 				continue;
    984 
    985 			os_free(ssid->ssid);
    986 			ssid->ssid = os_malloc(bss->ssid_len);
    987 			if (ssid->ssid == NULL)
    988 				break;
    989 			os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
    990 			ssid->ssid_len = bss->ssid_len;
    991 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
    992 					  "scan results",
    993 					  ssid->ssid, ssid->ssid_len);
    994 			count++;
    995 		}
    996 
    997 		if (count > 1) {
    998 			wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
    999 				   "for the AP; use wildcard");
   1000 			os_free(ssid->ssid);
   1001 			ssid->ssid = NULL;
   1002 			ssid->ssid_len = 0;
   1003 		}
   1004 #endif /* CONFIG_P2P */
   1005 	}
   1006 
   1007 	return ssid;
   1008 }
   1009 
   1010 
   1011 static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
   1012 				  struct wpa_ssid *selected)
   1013 {
   1014 	struct wpa_ssid *ssid;
   1015 
   1016 	if (wpa_s->current_ssid)
   1017 		wpa_supplicant_deauthenticate(
   1018 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   1019 
   1020 	/* Mark all other networks disabled and trigger reassociation */
   1021 	ssid = wpa_s->conf->ssid;
   1022 	while (ssid) {
   1023 		int was_disabled = ssid->disabled;
   1024 		ssid->disabled_for_connect = 0;
   1025 		/*
   1026 		 * In case the network object corresponds to a persistent group
   1027 		 * then do not send out network disabled signal. In addition,
   1028 		 * do not change disabled status of persistent network objects
   1029 		 * from 2 to 1 should we connect to another network.
   1030 		 */
   1031 		if (was_disabled != 2) {
   1032 			ssid->disabled = ssid != selected;
   1033 			if (was_disabled != ssid->disabled) {
   1034 				if (ssid->disabled)
   1035 					ssid->disabled_for_connect = 1;
   1036 				wpas_notify_network_enabled_changed(wpa_s,
   1037 								    ssid);
   1038 			}
   1039 		}
   1040 		ssid = ssid->next;
   1041 	}
   1042 }
   1043 
   1044 
   1045 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
   1046 			     struct wpa_ssid *selected, const u8 *bssid,
   1047 			     int freq)
   1048 {
   1049 	struct wpa_bss *bss;
   1050 
   1051 	wpa_s->wps_run++;
   1052 	if (wpa_s->wps_run == 0)
   1053 		wpa_s->wps_run++;
   1054 	wpa_s->after_wps = 0;
   1055 	wpa_s->known_wps_freq = 0;
   1056 	if (freq) {
   1057 		wpa_s->after_wps = 5;
   1058 		wpa_s->wps_freq = freq;
   1059 	} else if (bssid) {
   1060 		bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
   1061 		if (bss && bss->freq > 0) {
   1062 			wpa_s->known_wps_freq = 1;
   1063 			wpa_s->wps_freq = bss->freq;
   1064 		}
   1065 	}
   1066 
   1067 	wpas_wps_temp_disable(wpa_s, selected);
   1068 
   1069 	wpa_s->disconnected = 0;
   1070 	wpa_s->reassociate = 1;
   1071 	wpa_s->scan_runs = 0;
   1072 	wpa_s->normal_scans = 0;
   1073 	wpa_s->wps_success = 0;
   1074 	wpa_s->blacklist_cleared = 0;
   1075 
   1076 	wpa_supplicant_cancel_sched_scan(wpa_s);
   1077 	wpa_supplicant_req_scan(wpa_s, 0, 0);
   1078 }
   1079 
   1080 
   1081 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
   1082 		       int p2p_group)
   1083 {
   1084 	struct wpa_ssid *ssid;
   1085 	wpas_clear_wps(wpa_s);
   1086 	ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
   1087 	if (ssid == NULL)
   1088 		return -1;
   1089 	ssid->temporary = 1;
   1090 	ssid->p2p_group = p2p_group;
   1091 #ifdef CONFIG_P2P
   1092 	if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
   1093 		ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
   1094 		if (ssid->ssid) {
   1095 			ssid->ssid_len = wpa_s->go_params->ssid_len;
   1096 			os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
   1097 				  ssid->ssid_len);
   1098 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
   1099 					  "SSID", ssid->ssid, ssid->ssid_len);
   1100 		}
   1101 	}
   1102 #endif /* CONFIG_P2P */
   1103 	if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
   1104 		return -1;
   1105 	if (wpa_s->wps_fragment_size)
   1106 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
   1107 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
   1108 			       wpa_s, NULL);
   1109 	wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
   1110 	return 0;
   1111 }
   1112 
   1113 
   1114 static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
   1115 				 const u8 *dev_addr, const u8 *bssid,
   1116 				 const char *pin, int p2p_group, u16 dev_pw_id,
   1117 				 const u8 *peer_pubkey_hash,
   1118 				 const u8 *ssid_val, size_t ssid_len, int freq)
   1119 {
   1120 	struct wpa_ssid *ssid;
   1121 	char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
   1122 	unsigned int rpin = 0;
   1123 	char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
   1124 
   1125 	wpas_clear_wps(wpa_s);
   1126 	if (bssid && is_zero_ether_addr(bssid))
   1127 		bssid = NULL;
   1128 	ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
   1129 	if (ssid == NULL) {
   1130 		wpa_printf(MSG_DEBUG, "WPS: Could not add network");
   1131 		return -1;
   1132 	}
   1133 	ssid->temporary = 1;
   1134 	ssid->p2p_group = p2p_group;
   1135 	if (ssid_val) {
   1136 		ssid->ssid = os_malloc(ssid_len);
   1137 		if (ssid->ssid) {
   1138 			os_memcpy(ssid->ssid, ssid_val, ssid_len);
   1139 			ssid->ssid_len = ssid_len;
   1140 		}
   1141 	}
   1142 	if (peer_pubkey_hash) {
   1143 		os_memcpy(hash, " pkhash=", 8);
   1144 		wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
   1145 					   peer_pubkey_hash,
   1146 					   WPS_OOB_PUBKEY_HASH_LEN);
   1147 	} else {
   1148 		hash[0] = '\0';
   1149 	}
   1150 #ifdef CONFIG_P2P
   1151 	if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
   1152 		ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
   1153 		if (ssid->ssid) {
   1154 			ssid->ssid_len = wpa_s->go_params->ssid_len;
   1155 			os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
   1156 				  ssid->ssid_len);
   1157 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
   1158 					  "SSID", ssid->ssid, ssid->ssid_len);
   1159 		}
   1160 	}
   1161 #endif /* CONFIG_P2P */
   1162 	if (pin)
   1163 		os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
   1164 			    pin, dev_pw_id, hash);
   1165 	else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
   1166 		os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
   1167 			    dev_pw_id, hash);
   1168 	} else {
   1169 		rpin = wps_generate_pin();
   1170 		os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
   1171 			    rpin, dev_pw_id, hash);
   1172 	}
   1173 	if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
   1174 		wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
   1175 		return -1;
   1176 	}
   1177 	if (wpa_s->wps_fragment_size)
   1178 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
   1179 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
   1180 			       wpa_s, NULL);
   1181 	wpa_s->wps_ap_iter = 1;
   1182 	wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
   1183 	return rpin;
   1184 }
   1185 
   1186 
   1187 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
   1188 		       const char *pin, int p2p_group, u16 dev_pw_id)
   1189 {
   1190 	return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
   1191 				     dev_pw_id, NULL, NULL, 0, 0);
   1192 }
   1193 
   1194 
   1195 /* Cancel the wps pbc/pin requests */
   1196 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
   1197 {
   1198 #ifdef CONFIG_AP
   1199 	if (wpa_s->ap_iface) {
   1200 		wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
   1201 		return wpa_supplicant_ap_wps_cancel(wpa_s);
   1202 	}
   1203 #endif /* CONFIG_AP */
   1204 
   1205 	if (wpa_s->wpa_state == WPA_SCANNING ||
   1206 	    wpa_s->wpa_state == WPA_DISCONNECTED) {
   1207 		wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
   1208 		wpa_supplicant_cancel_scan(wpa_s);
   1209 		wpas_clear_wps(wpa_s);
   1210 	} else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
   1211 		wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
   1212 			   "deauthenticate");
   1213 		wpa_supplicant_deauthenticate(wpa_s,
   1214 					      WLAN_REASON_DEAUTH_LEAVING);
   1215 		wpas_clear_wps(wpa_s);
   1216 	} else {
   1217 		wpas_wps_reenable_networks(wpa_s);
   1218 		wpas_wps_clear_ap_info(wpa_s);
   1219 		if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
   1220 		    0)
   1221 			wpas_clear_wps(wpa_s);
   1222 	}
   1223 
   1224 	wpa_s->after_wps = 0;
   1225 
   1226 	return 0;
   1227 }
   1228 
   1229 
   1230 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
   1231 		       const char *pin, struct wps_new_ap_settings *settings)
   1232 {
   1233 	struct wpa_ssid *ssid;
   1234 	char val[200];
   1235 	char *pos, *end;
   1236 	int res;
   1237 
   1238 	if (!pin)
   1239 		return -1;
   1240 	wpas_clear_wps(wpa_s);
   1241 	ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
   1242 	if (ssid == NULL)
   1243 		return -1;
   1244 	ssid->temporary = 1;
   1245 	pos = val;
   1246 	end = pos + sizeof(val);
   1247 	res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
   1248 	if (res < 0 || res >= end - pos)
   1249 		return -1;
   1250 	pos += res;
   1251 	if (settings) {
   1252 		res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
   1253 				  "new_encr=%s new_key=%s",
   1254 				  settings->ssid_hex, settings->auth,
   1255 				  settings->encr, settings->key_hex);
   1256 		if (res < 0 || res >= end - pos)
   1257 			return -1;
   1258 		pos += res;
   1259 	}
   1260 	res = os_snprintf(pos, end - pos, "\"");
   1261 	if (res < 0 || res >= end - pos)
   1262 		return -1;
   1263 	if (wpa_config_set(ssid, "phase1", val, 0) < 0)
   1264 		return -1;
   1265 	if (wpa_s->wps_fragment_size)
   1266 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
   1267 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
   1268 			       wpa_s, NULL);
   1269 	wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
   1270 	return 0;
   1271 }
   1272 
   1273 
   1274 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
   1275 			       const u8 *p2p_dev_addr, const u8 *psk,
   1276 			       size_t psk_len)
   1277 {
   1278 	if (is_zero_ether_addr(p2p_dev_addr)) {
   1279 		wpa_printf(MSG_DEBUG,
   1280 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
   1281 			   MAC2STR(mac_addr));
   1282 	} else {
   1283 		wpa_printf(MSG_DEBUG,
   1284 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
   1285 			   " P2P Device Addr " MACSTR,
   1286 			   MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
   1287 	}
   1288 	wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
   1289 
   1290 	/* TODO */
   1291 
   1292 	return 0;
   1293 }
   1294 
   1295 
   1296 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
   1297 				   const struct wps_device_data *dev)
   1298 {
   1299 	char uuid[40], txt[400];
   1300 	int len;
   1301 	char devtype[WPS_DEV_TYPE_BUFSIZE];
   1302 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
   1303 		return;
   1304 	wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
   1305 	len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
   1306 			  " [%s|%s|%s|%s|%s|%s]",
   1307 			  uuid, MAC2STR(dev->mac_addr), dev->device_name,
   1308 			  dev->manufacturer, dev->model_name,
   1309 			  dev->model_number, dev->serial_number,
   1310 			  wps_dev_type_bin2str(dev->pri_dev_type, devtype,
   1311 					       sizeof(devtype)));
   1312 	if (len > 0 && len < (int) sizeof(txt))
   1313 		wpa_printf(MSG_INFO, "%s", txt);
   1314 }
   1315 
   1316 
   1317 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
   1318 				    u16 sel_reg_config_methods)
   1319 {
   1320 #ifdef CONFIG_WPS_ER
   1321 	struct wpa_supplicant *wpa_s = ctx;
   1322 
   1323 	if (wpa_s->wps_er == NULL)
   1324 		return;
   1325 	wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
   1326 		   "dev_password_id=%u sel_reg_config_methods=0x%x",
   1327 		   sel_reg, dev_passwd_id, sel_reg_config_methods);
   1328 	wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
   1329 			   sel_reg_config_methods);
   1330 #endif /* CONFIG_WPS_ER */
   1331 }
   1332 
   1333 
   1334 static u16 wps_fix_config_methods(u16 config_methods)
   1335 {
   1336 	if ((config_methods &
   1337 	     (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
   1338 	      WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
   1339 		wpa_printf(MSG_INFO, "WPS: Converting display to "
   1340 			   "virtual_display for WPS 2.0 compliance");
   1341 		config_methods |= WPS_CONFIG_VIRT_DISPLAY;
   1342 	}
   1343 	if ((config_methods &
   1344 	     (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
   1345 	      WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
   1346 		wpa_printf(MSG_INFO, "WPS: Converting push_button to "
   1347 			   "virtual_push_button for WPS 2.0 compliance");
   1348 		config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
   1349 	}
   1350 
   1351 	return config_methods;
   1352 }
   1353 
   1354 
   1355 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
   1356 			      struct wps_context *wps)
   1357 {
   1358 	char buf[50];
   1359 	const char *src;
   1360 
   1361 	if (is_nil_uuid(wpa_s->conf->uuid)) {
   1362 		struct wpa_supplicant *first;
   1363 		first = wpa_s->global->ifaces;
   1364 		while (first && first->next)
   1365 			first = first->next;
   1366 		if (first && first != wpa_s) {
   1367 			if (wps != wpa_s->global->ifaces->wps)
   1368 				os_memcpy(wps->uuid,
   1369 					  wpa_s->global->ifaces->wps->uuid,
   1370 					  WPS_UUID_LEN);
   1371 			src = "from the first interface";
   1372 		} else {
   1373 			uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
   1374 			src = "based on MAC address";
   1375 		}
   1376 	} else {
   1377 		os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
   1378 		src = "based on configuration";
   1379 	}
   1380 
   1381 	uuid_bin2str(wps->uuid, buf, sizeof(buf));
   1382 	wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
   1383 }
   1384 
   1385 
   1386 static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
   1387 				       struct wps_context *wps)
   1388 {
   1389 	wpabuf_free(wps->dev.vendor_ext_m1);
   1390 	wps->dev.vendor_ext_m1 = NULL;
   1391 
   1392 	if (wpa_s->conf->wps_vendor_ext_m1) {
   1393 		wps->dev.vendor_ext_m1 =
   1394 			wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
   1395 		if (!wps->dev.vendor_ext_m1) {
   1396 			wpa_printf(MSG_ERROR, "WPS: Cannot "
   1397 				   "allocate memory for vendor_ext_m1");
   1398 		}
   1399 	}
   1400 }
   1401 
   1402 
   1403 int wpas_wps_init(struct wpa_supplicant *wpa_s)
   1404 {
   1405 	struct wps_context *wps;
   1406 	struct wps_registrar_config rcfg;
   1407 	struct hostapd_hw_modes *modes;
   1408 	u16 m;
   1409 
   1410 	wps = os_zalloc(sizeof(*wps));
   1411 	if (wps == NULL)
   1412 		return -1;
   1413 
   1414 	wps->cred_cb = wpa_supplicant_wps_cred;
   1415 	wps->event_cb = wpa_supplicant_wps_event;
   1416 	wps->rf_band_cb = wpa_supplicant_wps_rf_band;
   1417 	wps->cb_ctx = wpa_s;
   1418 
   1419 	wps->dev.device_name = wpa_s->conf->device_name;
   1420 	wps->dev.manufacturer = wpa_s->conf->manufacturer;
   1421 	wps->dev.model_name = wpa_s->conf->model_name;
   1422 	wps->dev.model_number = wpa_s->conf->model_number;
   1423 	wps->dev.serial_number = wpa_s->conf->serial_number;
   1424 	wps->config_methods =
   1425 		wps_config_methods_str2bin(wpa_s->conf->config_methods);
   1426 	if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
   1427 	    (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
   1428 		wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
   1429 			   "methods are not allowed at the same time");
   1430 		os_free(wps);
   1431 		return -1;
   1432 	}
   1433 	wps->config_methods = wps_fix_config_methods(wps->config_methods);
   1434 	wps->dev.config_methods = wps->config_methods;
   1435 	os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
   1436 		  WPS_DEV_TYPE_LEN);
   1437 
   1438 	wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
   1439 	os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
   1440 		  WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
   1441 
   1442 	wpas_wps_set_vendor_ext_m1(wpa_s, wps);
   1443 
   1444 	wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
   1445 	modes = wpa_s->hw.modes;
   1446 	if (modes) {
   1447 		for (m = 0; m < wpa_s->hw.num_modes; m++) {
   1448 			if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
   1449 			    modes[m].mode == HOSTAPD_MODE_IEEE80211G)
   1450 				wps->dev.rf_bands |= WPS_RF_24GHZ;
   1451 			else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
   1452 				wps->dev.rf_bands |= WPS_RF_50GHZ;
   1453 		}
   1454 	}
   1455 	if (wps->dev.rf_bands == 0) {
   1456 		/*
   1457 		 * Default to claiming support for both bands if the driver
   1458 		 * does not provide support for fetching supported bands.
   1459 		 */
   1460 		wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
   1461 	}
   1462 	os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
   1463 	wpas_wps_set_uuid(wpa_s, wps);
   1464 
   1465 	wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
   1466 	wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
   1467 
   1468 	os_memset(&rcfg, 0, sizeof(rcfg));
   1469 	rcfg.new_psk_cb = wpas_wps_new_psk_cb;
   1470 	rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
   1471 	rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
   1472 	rcfg.cb_ctx = wpa_s;
   1473 
   1474 	wps->registrar = wps_registrar_init(wps, &rcfg);
   1475 	if (wps->registrar == NULL) {
   1476 		wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
   1477 		os_free(wps);
   1478 		return -1;
   1479 	}
   1480 
   1481 	wpa_s->wps = wps;
   1482 
   1483 	return 0;
   1484 }
   1485 
   1486 
   1487 #ifdef CONFIG_WPS_ER
   1488 static void wpas_wps_nfc_clear(struct wps_context *wps)
   1489 {
   1490 	wps->ap_nfc_dev_pw_id = 0;
   1491 	wpabuf_free(wps->ap_nfc_dh_pubkey);
   1492 	wps->ap_nfc_dh_pubkey = NULL;
   1493 	wpabuf_free(wps->ap_nfc_dh_privkey);
   1494 	wps->ap_nfc_dh_privkey = NULL;
   1495 	wpabuf_free(wps->ap_nfc_dev_pw);
   1496 	wps->ap_nfc_dev_pw = NULL;
   1497 }
   1498 #endif /* CONFIG_WPS_ER */
   1499 
   1500 
   1501 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
   1502 {
   1503 	wpas_wps_assoc_with_cred_cancel(wpa_s);
   1504 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
   1505 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
   1506 	eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
   1507 	wpas_wps_clear_ap_info(wpa_s);
   1508 
   1509 #ifdef CONFIG_P2P
   1510 	eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
   1511 #endif /* CONFIG_P2P */
   1512 
   1513 	if (wpa_s->wps == NULL)
   1514 		return;
   1515 
   1516 #ifdef CONFIG_WPS_ER
   1517 	wps_er_deinit(wpa_s->wps_er, NULL, NULL);
   1518 	wpa_s->wps_er = NULL;
   1519 	wpas_wps_nfc_clear(wpa_s->wps);
   1520 #endif /* CONFIG_WPS_ER */
   1521 
   1522 	wps_registrar_deinit(wpa_s->wps->registrar);
   1523 	wpabuf_free(wpa_s->wps->dh_pubkey);
   1524 	wpabuf_free(wpa_s->wps->dh_privkey);
   1525 	wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
   1526 	os_free(wpa_s->wps->network_key);
   1527 	os_free(wpa_s->wps);
   1528 	wpa_s->wps = NULL;
   1529 }
   1530 
   1531 
   1532 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
   1533 			    struct wpa_ssid *ssid, struct wpa_bss *bss)
   1534 {
   1535 	struct wpabuf *wps_ie;
   1536 
   1537 	if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
   1538 		return -1;
   1539 
   1540 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
   1541 	if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
   1542 		if (!wps_ie) {
   1543 			wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
   1544 			return 0;
   1545 		}
   1546 
   1547 		if (!wps_is_selected_pbc_registrar(wps_ie)) {
   1548 			wpa_printf(MSG_DEBUG, "   skip - WPS AP "
   1549 				   "without active PBC Registrar");
   1550 			wpabuf_free(wps_ie);
   1551 			return 0;
   1552 		}
   1553 
   1554 		/* TODO: overlap detection */
   1555 		wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
   1556 			   "(Active PBC)");
   1557 		wpabuf_free(wps_ie);
   1558 		return 1;
   1559 	}
   1560 
   1561 	if (eap_is_wps_pin_enrollee(&ssid->eap)) {
   1562 		if (!wps_ie) {
   1563 			wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
   1564 			return 0;
   1565 		}
   1566 
   1567 		/*
   1568 		 * Start with WPS APs that advertise our address as an
   1569 		 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
   1570 		 * allow any WPS AP after couple of scans since some APs do not
   1571 		 * set Selected Registrar attribute properly when using
   1572 		 * external Registrar.
   1573 		 */
   1574 		if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
   1575 			if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
   1576 				wpa_printf(MSG_DEBUG, "   skip - WPS AP "
   1577 					   "without active PIN Registrar");
   1578 				wpabuf_free(wps_ie);
   1579 				return 0;
   1580 			}
   1581 			wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
   1582 		} else {
   1583 			wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
   1584 				   "(Authorized MAC or Active PIN)");
   1585 		}
   1586 		wpabuf_free(wps_ie);
   1587 		return 1;
   1588 	}
   1589 
   1590 	if (wps_ie) {
   1591 		wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
   1592 		wpabuf_free(wps_ie);
   1593 		return 1;
   1594 	}
   1595 
   1596 	return -1;
   1597 }
   1598 
   1599 
   1600 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
   1601 			      struct wpa_ssid *ssid,
   1602 			      struct wpa_bss *bss)
   1603 {
   1604 	struct wpabuf *wps_ie = NULL;
   1605 	int ret = 0;
   1606 
   1607 	if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
   1608 		wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
   1609 		if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
   1610 			/* allow wildcard SSID for WPS PBC */
   1611 			ret = 1;
   1612 		}
   1613 	} else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
   1614 		wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
   1615 		if (wps_ie &&
   1616 		    (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
   1617 		     wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
   1618 			/* allow wildcard SSID for WPS PIN */
   1619 			ret = 1;
   1620 		}
   1621 	}
   1622 
   1623 	if (!ret && ssid->bssid_set &&
   1624 	    os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
   1625 		/* allow wildcard SSID due to hardcoded BSSID match */
   1626 		ret = 1;
   1627 	}
   1628 
   1629 #ifdef CONFIG_WPS_STRICT
   1630 	if (wps_ie) {
   1631 		if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
   1632 						   0, bss->bssid) < 0)
   1633 			ret = 0;
   1634 		if (bss->beacon_ie_len) {
   1635 			struct wpabuf *bcn_wps;
   1636 			bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
   1637 				bss, WPS_IE_VENDOR_TYPE);
   1638 			if (bcn_wps == NULL) {
   1639 				wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
   1640 					   "missing from AP Beacon");
   1641 				ret = 0;
   1642 			} else {
   1643 				if (wps_validate_beacon(wps_ie) < 0)
   1644 					ret = 0;
   1645 				wpabuf_free(bcn_wps);
   1646 			}
   1647 		}
   1648 	}
   1649 #endif /* CONFIG_WPS_STRICT */
   1650 
   1651 	wpabuf_free(wps_ie);
   1652 
   1653 	return ret;
   1654 }
   1655 
   1656 
   1657 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
   1658 			      struct wpa_bss *selected, struct wpa_ssid *ssid)
   1659 {
   1660 	const u8 *sel_uuid, *uuid;
   1661 	struct wpabuf *wps_ie;
   1662 	int ret = 0;
   1663 	struct wpa_bss *bss;
   1664 
   1665 	if (!eap_is_wps_pbc_enrollee(&ssid->eap))
   1666 		return 0;
   1667 
   1668 	wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
   1669 		   "present in scan results; selected BSSID " MACSTR,
   1670 		   MAC2STR(selected->bssid));
   1671 
   1672 	/* Make sure that only one AP is in active PBC mode */
   1673 	wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
   1674 	if (wps_ie) {
   1675 		sel_uuid = wps_get_uuid_e(wps_ie);
   1676 		wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
   1677 			    sel_uuid, UUID_LEN);
   1678 	} else {
   1679 		wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
   1680 			   "WPS IE?!");
   1681 		sel_uuid = NULL;
   1682 	}
   1683 
   1684 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
   1685 		struct wpabuf *ie;
   1686 		if (bss == selected)
   1687 			continue;
   1688 		ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
   1689 		if (!ie)
   1690 			continue;
   1691 		if (!wps_is_selected_pbc_registrar(ie)) {
   1692 			wpabuf_free(ie);
   1693 			continue;
   1694 		}
   1695 		wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
   1696 			   MACSTR, MAC2STR(bss->bssid));
   1697 		uuid = wps_get_uuid_e(ie);
   1698 		wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
   1699 			    uuid, UUID_LEN);
   1700 		if (sel_uuid == NULL || uuid == NULL ||
   1701 		    os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
   1702 			ret = 1; /* PBC overlap */
   1703 			wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
   1704 				MACSTR " and " MACSTR,
   1705 				MAC2STR(selected->bssid),
   1706 				MAC2STR(bss->bssid));
   1707 			wpabuf_free(ie);
   1708 			break;
   1709 		}
   1710 
   1711 		/* TODO: verify that this is reasonable dual-band situation */
   1712 
   1713 		wpabuf_free(ie);
   1714 	}
   1715 
   1716 	wpabuf_free(wps_ie);
   1717 
   1718 	return ret;
   1719 }
   1720 
   1721 
   1722 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
   1723 {
   1724 	struct wpa_bss *bss;
   1725 	unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
   1726 
   1727 	if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
   1728 		return;
   1729 
   1730 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
   1731 		struct wpabuf *ie;
   1732 		ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
   1733 		if (!ie)
   1734 			continue;
   1735 		if (wps_is_selected_pbc_registrar(ie))
   1736 			pbc++;
   1737 		else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
   1738 			auth++;
   1739 		else if (wps_is_selected_pin_registrar(ie))
   1740 			pin++;
   1741 		else
   1742 			wps++;
   1743 		wpabuf_free(ie);
   1744 	}
   1745 
   1746 	if (pbc)
   1747 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
   1748 	else if (auth)
   1749 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
   1750 	else if (pin)
   1751 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
   1752 	else if (wps)
   1753 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
   1754 }
   1755 
   1756 
   1757 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
   1758 {
   1759 	struct wpa_ssid *ssid;
   1760 
   1761 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
   1762 		if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
   1763 			return 1;
   1764 	}
   1765 
   1766 	return 0;
   1767 }
   1768 
   1769 
   1770 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
   1771 			      char *end)
   1772 {
   1773 	struct wpabuf *wps_ie;
   1774 	int ret;
   1775 
   1776 	wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
   1777 	if (wps_ie == NULL)
   1778 		return 0;
   1779 
   1780 	ret = wps_attr_text(wps_ie, buf, end);
   1781 	wpabuf_free(wps_ie);
   1782 	return ret;
   1783 }
   1784 
   1785 
   1786 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
   1787 {
   1788 #ifdef CONFIG_WPS_ER
   1789 	if (wpa_s->wps_er) {
   1790 		wps_er_refresh(wpa_s->wps_er);
   1791 		return 0;
   1792 	}
   1793 	wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
   1794 	if (wpa_s->wps_er == NULL)
   1795 		return -1;
   1796 	return 0;
   1797 #else /* CONFIG_WPS_ER */
   1798 	return 0;
   1799 #endif /* CONFIG_WPS_ER */
   1800 }
   1801 
   1802 
   1803 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
   1804 {
   1805 #ifdef CONFIG_WPS_ER
   1806 	wps_er_deinit(wpa_s->wps_er, NULL, NULL);
   1807 	wpa_s->wps_er = NULL;
   1808 #endif /* CONFIG_WPS_ER */
   1809 	return 0;
   1810 }
   1811 
   1812 
   1813 #ifdef CONFIG_WPS_ER
   1814 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
   1815 			const char *uuid, const char *pin)
   1816 {
   1817 	u8 u[UUID_LEN];
   1818 	const u8 *use_uuid = NULL;
   1819 	u8 addr_buf[ETH_ALEN];
   1820 
   1821 	if (os_strcmp(uuid, "any") == 0) {
   1822 	} else if (uuid_str2bin(uuid, u) == 0) {
   1823 		use_uuid = u;
   1824 	} else if (hwaddr_aton(uuid, addr_buf) == 0) {
   1825 		use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
   1826 		if (use_uuid == NULL)
   1827 			return -1;
   1828 	} else
   1829 		return -1;
   1830 	return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
   1831 				     use_uuid,
   1832 				     (const u8 *) pin, os_strlen(pin), 300);
   1833 }
   1834 
   1835 
   1836 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
   1837 {
   1838 	u8 u[UUID_LEN], *use_uuid = NULL;
   1839 	u8 addr[ETH_ALEN], *use_addr = NULL;
   1840 
   1841 	if (uuid_str2bin(uuid, u) == 0)
   1842 		use_uuid = u;
   1843 	else if (hwaddr_aton(uuid, addr) == 0)
   1844 		use_addr = addr;
   1845 	else
   1846 		return -1;
   1847 	return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
   1848 }
   1849 
   1850 
   1851 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
   1852 		      const char *pin)
   1853 {
   1854 	u8 u[UUID_LEN], *use_uuid = NULL;
   1855 	u8 addr[ETH_ALEN], *use_addr = NULL;
   1856 
   1857 	if (uuid_str2bin(uuid, u) == 0)
   1858 		use_uuid = u;
   1859 	else if (hwaddr_aton(uuid, addr) == 0)
   1860 		use_addr = addr;
   1861 	else
   1862 		return -1;
   1863 
   1864 	return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
   1865 			    os_strlen(pin));
   1866 }
   1867 
   1868 
   1869 static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
   1870 				    struct wps_credential *cred)
   1871 {
   1872 	os_memset(cred, 0, sizeof(*cred));
   1873 	if (ssid->ssid_len > 32)
   1874 		return -1;
   1875 	os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
   1876 	cred->ssid_len = ssid->ssid_len;
   1877 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
   1878 		cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
   1879 			WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
   1880 		if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
   1881 			cred->encr_type = WPS_ENCR_AES;
   1882 		else
   1883 			cred->encr_type = WPS_ENCR_TKIP;
   1884 		if (ssid->passphrase) {
   1885 			cred->key_len = os_strlen(ssid->passphrase);
   1886 			if (cred->key_len >= 64)
   1887 				return -1;
   1888 			os_memcpy(cred->key, ssid->passphrase, cred->key_len);
   1889 		} else if (ssid->psk_set) {
   1890 			cred->key_len = 32;
   1891 			os_memcpy(cred->key, ssid->psk, 32);
   1892 		} else
   1893 			return -1;
   1894 	} else {
   1895 		cred->auth_type = WPS_AUTH_OPEN;
   1896 		cred->encr_type = WPS_ENCR_NONE;
   1897 	}
   1898 
   1899 	return 0;
   1900 }
   1901 
   1902 
   1903 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
   1904 			   int id)
   1905 {
   1906 	u8 u[UUID_LEN], *use_uuid = NULL;
   1907 	u8 addr[ETH_ALEN], *use_addr = NULL;
   1908 	struct wpa_ssid *ssid;
   1909 	struct wps_credential cred;
   1910 
   1911 	if (uuid_str2bin(uuid, u) == 0)
   1912 		use_uuid = u;
   1913 	else if (hwaddr_aton(uuid, addr) == 0)
   1914 		use_addr = addr;
   1915 	else
   1916 		return -1;
   1917 	ssid = wpa_config_get_network(wpa_s->conf, id);
   1918 	if (ssid == NULL || ssid->ssid == NULL)
   1919 		return -1;
   1920 
   1921 	if (wpas_wps_network_to_cred(ssid, &cred) < 0)
   1922 		return -1;
   1923 	return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
   1924 }
   1925 
   1926 
   1927 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
   1928 		       const char *pin, struct wps_new_ap_settings *settings)
   1929 {
   1930 	u8 u[UUID_LEN], *use_uuid = NULL;
   1931 	u8 addr[ETH_ALEN], *use_addr = NULL;
   1932 	struct wps_credential cred;
   1933 	size_t len;
   1934 
   1935 	if (uuid_str2bin(uuid, u) == 0)
   1936 		use_uuid = u;
   1937 	else if (hwaddr_aton(uuid, addr) == 0)
   1938 		use_addr = addr;
   1939 	else
   1940 		return -1;
   1941 	if (settings->ssid_hex == NULL || settings->auth == NULL ||
   1942 	    settings->encr == NULL || settings->key_hex == NULL)
   1943 		return -1;
   1944 
   1945 	os_memset(&cred, 0, sizeof(cred));
   1946 	len = os_strlen(settings->ssid_hex);
   1947 	if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
   1948 	    hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
   1949 		return -1;
   1950 	cred.ssid_len = len / 2;
   1951 
   1952 	len = os_strlen(settings->key_hex);
   1953 	if ((len & 1) || len > 2 * sizeof(cred.key) ||
   1954 	    hexstr2bin(settings->key_hex, cred.key, len / 2))
   1955 		return -1;
   1956 	cred.key_len = len / 2;
   1957 
   1958 	if (os_strcmp(settings->auth, "OPEN") == 0)
   1959 		cred.auth_type = WPS_AUTH_OPEN;
   1960 	else if (os_strcmp(settings->auth, "WPAPSK") == 0)
   1961 		cred.auth_type = WPS_AUTH_WPAPSK;
   1962 	else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
   1963 		cred.auth_type = WPS_AUTH_WPA2PSK;
   1964 	else
   1965 		return -1;
   1966 
   1967 	if (os_strcmp(settings->encr, "NONE") == 0)
   1968 		cred.encr_type = WPS_ENCR_NONE;
   1969 #ifdef CONFIG_TESTING_OPTIONS
   1970 	else if (os_strcmp(settings->encr, "WEP") == 0)
   1971 		cred.encr_type = WPS_ENCR_WEP;
   1972 #endif /* CONFIG_TESTING_OPTIONS */
   1973 	else if (os_strcmp(settings->encr, "TKIP") == 0)
   1974 		cred.encr_type = WPS_ENCR_TKIP;
   1975 	else if (os_strcmp(settings->encr, "CCMP") == 0)
   1976 		cred.encr_type = WPS_ENCR_AES;
   1977 	else
   1978 		return -1;
   1979 
   1980 	return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
   1981 			     (const u8 *) pin, os_strlen(pin), &cred);
   1982 }
   1983 
   1984 
   1985 #ifdef CONFIG_WPS_NFC
   1986 struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
   1987 					     int ndef, const char *uuid)
   1988 {
   1989 	struct wpabuf *ret;
   1990 	u8 u[UUID_LEN], *use_uuid = NULL;
   1991 	u8 addr[ETH_ALEN], *use_addr = NULL;
   1992 
   1993 	if (!wpa_s->wps_er)
   1994 		return NULL;
   1995 
   1996 	if (uuid_str2bin(uuid, u) == 0)
   1997 		use_uuid = u;
   1998 	else if (hwaddr_aton(uuid, addr) == 0)
   1999 		use_addr = addr;
   2000 	else
   2001 		return NULL;
   2002 
   2003 	ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
   2004 	if (ndef && ret) {
   2005 		struct wpabuf *tmp;
   2006 		tmp = ndef_build_wifi(ret);
   2007 		wpabuf_free(ret);
   2008 		if (tmp == NULL)
   2009 			return NULL;
   2010 		ret = tmp;
   2011 	}
   2012 
   2013 	return ret;
   2014 }
   2015 #endif /* CONFIG_WPS_NFC */
   2016 
   2017 
   2018 static int callbacks_pending = 0;
   2019 
   2020 static void wpas_wps_terminate_cb(void *ctx)
   2021 {
   2022 	wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
   2023 	if (--callbacks_pending <= 0)
   2024 		eloop_terminate();
   2025 }
   2026 #endif /* CONFIG_WPS_ER */
   2027 
   2028 
   2029 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
   2030 {
   2031 #ifdef CONFIG_WPS_ER
   2032 	if (wpa_s->wps_er) {
   2033 		callbacks_pending++;
   2034 		wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
   2035 		wpa_s->wps_er = NULL;
   2036 		return 1;
   2037 	}
   2038 #endif /* CONFIG_WPS_ER */
   2039 	return 0;
   2040 }
   2041 
   2042 
   2043 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
   2044 {
   2045 	struct wps_context *wps = wpa_s->wps;
   2046 
   2047 	if (wps == NULL)
   2048 		return;
   2049 
   2050 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
   2051 		wps->config_methods = wps_config_methods_str2bin(
   2052 			wpa_s->conf->config_methods);
   2053 		if ((wps->config_methods &
   2054 		     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
   2055 		    (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
   2056 			wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
   2057 				   "config methods are not allowed at the "
   2058 				   "same time");
   2059 			wps->config_methods &= ~WPS_CONFIG_LABEL;
   2060 		}
   2061 	}
   2062 	wps->config_methods = wps_fix_config_methods(wps->config_methods);
   2063 	wps->dev.config_methods = wps->config_methods;
   2064 
   2065 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
   2066 		os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
   2067 			  WPS_DEV_TYPE_LEN);
   2068 
   2069 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
   2070 		wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
   2071 		os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
   2072 			  wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
   2073 	}
   2074 
   2075 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
   2076 		wpas_wps_set_vendor_ext_m1(wpa_s, wps);
   2077 
   2078 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
   2079 		wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
   2080 
   2081 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
   2082 		wpas_wps_set_uuid(wpa_s, wps);
   2083 
   2084 	if (wpa_s->conf->changed_parameters &
   2085 	    (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
   2086 		/* Update pointers to make sure they refer current values */
   2087 		wps->dev.device_name = wpa_s->conf->device_name;
   2088 		wps->dev.manufacturer = wpa_s->conf->manufacturer;
   2089 		wps->dev.model_name = wpa_s->conf->model_name;
   2090 		wps->dev.model_number = wpa_s->conf->model_number;
   2091 		wps->dev.serial_number = wpa_s->conf->serial_number;
   2092 	}
   2093 }
   2094 
   2095 
   2096 #ifdef CONFIG_WPS_NFC
   2097 
   2098 #ifdef CONFIG_WPS_ER
   2099 static struct wpabuf *
   2100 wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
   2101 			      struct wpa_ssid *ssid)
   2102 {
   2103 	struct wpabuf *ret;
   2104 	struct wps_credential cred;
   2105 
   2106 	if (wpas_wps_network_to_cred(ssid, &cred) < 0)
   2107 		return NULL;
   2108 
   2109 	ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
   2110 
   2111 	if (ndef && ret) {
   2112 		struct wpabuf *tmp;
   2113 		tmp = ndef_build_wifi(ret);
   2114 		wpabuf_free(ret);
   2115 		if (tmp == NULL)
   2116 			return NULL;
   2117 		ret = tmp;
   2118 	}
   2119 
   2120 	return ret;
   2121 }
   2122 #endif /* CONFIG_WPS_ER */
   2123 
   2124 
   2125 struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
   2126 					  int ndef, const char *id_str)
   2127 {
   2128 #ifdef CONFIG_WPS_ER
   2129 	if (id_str) {
   2130 		int id;
   2131 		char *end = NULL;
   2132 		struct wpa_ssid *ssid;
   2133 
   2134 		id = strtol(id_str, &end, 10);
   2135 		if (end && *end)
   2136 			return NULL;
   2137 
   2138 		ssid = wpa_config_get_network(wpa_s->conf, id);
   2139 		if (ssid == NULL)
   2140 			return NULL;
   2141 		return wpas_wps_network_config_token(wpa_s, ndef, ssid);
   2142 	}
   2143 #endif /* CONFIG_WPS_ER */
   2144 #ifdef CONFIG_AP
   2145 	if (wpa_s->ap_iface)
   2146 		return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
   2147 #endif /* CONFIG_AP */
   2148 	return NULL;
   2149 }
   2150 
   2151 
   2152 struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
   2153 {
   2154 	if (wpa_s->conf->wps_nfc_pw_from_config) {
   2155 		return wps_nfc_token_build(ndef,
   2156 					   wpa_s->conf->wps_nfc_dev_pw_id,
   2157 					   wpa_s->conf->wps_nfc_dh_pubkey,
   2158 					   wpa_s->conf->wps_nfc_dev_pw);
   2159 	}
   2160 
   2161 	return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
   2162 				 &wpa_s->conf->wps_nfc_dh_pubkey,
   2163 				 &wpa_s->conf->wps_nfc_dh_privkey,
   2164 				 &wpa_s->conf->wps_nfc_dev_pw);
   2165 }
   2166 
   2167 
   2168 int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
   2169 		       const u8 *bssid,
   2170 		       const struct wpabuf *dev_pw, u16 dev_pw_id,
   2171 		       int p2p_group, const u8 *peer_pubkey_hash,
   2172 		       const u8 *ssid, size_t ssid_len, int freq)
   2173 {
   2174 	struct wps_context *wps = wpa_s->wps;
   2175 	char pw[32 * 2 + 1];
   2176 
   2177 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
   2178 		dev_pw = wpa_s->conf->wps_nfc_dev_pw;
   2179 		dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
   2180 	}
   2181 
   2182 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
   2183 	    wpa_s->conf->wps_nfc_dh_privkey == NULL) {
   2184 		wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
   2185 			   "cannot start NFC-triggered connection");
   2186 		return -1;
   2187 	}
   2188 
   2189 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
   2190 		wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
   2191 			   "cannot start NFC-triggered connection", dev_pw_id);
   2192 		return -1;
   2193 	}
   2194 
   2195 	dh5_free(wps->dh_ctx);
   2196 	wpabuf_free(wps->dh_pubkey);
   2197 	wpabuf_free(wps->dh_privkey);
   2198 	wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
   2199 	wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
   2200 	if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
   2201 		wps->dh_ctx = NULL;
   2202 		wpabuf_free(wps->dh_pubkey);
   2203 		wps->dh_pubkey = NULL;
   2204 		wpabuf_free(wps->dh_privkey);
   2205 		wps->dh_privkey = NULL;
   2206 		wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
   2207 		return -1;
   2208 	}
   2209 	wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
   2210 	if (wps->dh_ctx == NULL) {
   2211 		wpabuf_free(wps->dh_pubkey);
   2212 		wps->dh_pubkey = NULL;
   2213 		wpabuf_free(wps->dh_privkey);
   2214 		wps->dh_privkey = NULL;
   2215 		wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
   2216 		return -1;
   2217 	}
   2218 
   2219 	if (dev_pw) {
   2220 		wpa_snprintf_hex_uppercase(pw, sizeof(pw),
   2221 					   wpabuf_head(dev_pw),
   2222 					   wpabuf_len(dev_pw));
   2223 	}
   2224 	return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
   2225 				     dev_pw ? pw : NULL,
   2226 				     p2p_group, dev_pw_id, peer_pubkey_hash,
   2227 				     ssid, ssid_len, freq);
   2228 }
   2229 
   2230 
   2231 static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
   2232 			     struct wps_parse_attr *attr)
   2233 {
   2234 	/*
   2235 	 * Disable existing networks temporarily to allow the newly learned
   2236 	 * credential to be preferred. Enable the temporarily disabled networks
   2237 	 * after 10 seconds.
   2238 	 */
   2239 	wpas_wps_temp_disable(wpa_s, NULL);
   2240 	eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
   2241 			       NULL);
   2242 
   2243 	if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
   2244 		return -1;
   2245 
   2246 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
   2247 		return 0;
   2248 
   2249 	if (attr->ap_channel) {
   2250 		u16 chan = WPA_GET_BE16(attr->ap_channel);
   2251 		int freq = 0;
   2252 
   2253 		if (chan >= 1 && chan <= 13)
   2254 			freq = 2407 + 5 * chan;
   2255 		else if (chan == 14)
   2256 			freq = 2484;
   2257 		else if (chan >= 30)
   2258 			freq = 5000 + 5 * chan;
   2259 
   2260 		if (freq) {
   2261 			wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
   2262 				   chan, freq);
   2263 			wpa_s->after_wps = 5;
   2264 			wpa_s->wps_freq = freq;
   2265 		}
   2266 	}
   2267 
   2268 	wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
   2269 		   "based on the received credential added");
   2270 	wpa_s->normal_scans = 0;
   2271 	wpa_supplicant_reinit_autoscan(wpa_s);
   2272 	wpa_s->disconnected = 0;
   2273 	wpa_s->reassociate = 1;
   2274 
   2275 	wpa_supplicant_cancel_sched_scan(wpa_s);
   2276 	wpa_supplicant_req_scan(wpa_s, 0, 0);
   2277 
   2278 	return 0;
   2279 }
   2280 
   2281 
   2282 #ifdef CONFIG_WPS_ER
   2283 static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
   2284 					   struct wps_parse_attr *attr)
   2285 {
   2286 	return wps_registrar_add_nfc_password_token(
   2287 		wpa_s->wps->registrar, attr->oob_dev_password,
   2288 		attr->oob_dev_password_len);
   2289 }
   2290 #endif /* CONFIG_WPS_ER */
   2291 
   2292 
   2293 static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
   2294 				    const struct wpabuf *wps)
   2295 {
   2296 	struct wps_parse_attr attr;
   2297 
   2298 	wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
   2299 
   2300 	if (wps_parse_msg(wps, &attr)) {
   2301 		wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
   2302 		return -1;
   2303 	}
   2304 
   2305 	if (attr.num_cred)
   2306 		return wpas_wps_use_cred(wpa_s, &attr);
   2307 
   2308 #ifdef CONFIG_WPS_ER
   2309 	if (attr.oob_dev_password)
   2310 		return wpas_wps_add_nfc_password_token(wpa_s, &attr);
   2311 #endif /* CONFIG_WPS_ER */
   2312 
   2313 	wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
   2314 	return -1;
   2315 }
   2316 
   2317 
   2318 int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
   2319 			  const struct wpabuf *data, int forced_freq)
   2320 {
   2321 	const struct wpabuf *wps = data;
   2322 	struct wpabuf *tmp = NULL;
   2323 	int ret;
   2324 
   2325 	if (wpabuf_len(data) < 4)
   2326 		return -1;
   2327 
   2328 	if (*wpabuf_head_u8(data) != 0x10) {
   2329 		/* Assume this contains full NDEF record */
   2330 		tmp = ndef_parse_wifi(data);
   2331 		if (tmp == NULL) {
   2332 #ifdef CONFIG_P2P
   2333 			tmp = ndef_parse_p2p(data);
   2334 			if (tmp) {
   2335 				ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
   2336 							       forced_freq);
   2337 				wpabuf_free(tmp);
   2338 				return ret;
   2339 			}
   2340 #endif /* CONFIG_P2P */
   2341 			wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
   2342 			return -1;
   2343 		}
   2344 		wps = tmp;
   2345 	}
   2346 
   2347 	ret = wpas_wps_nfc_tag_process(wpa_s, wps);
   2348 	wpabuf_free(tmp);
   2349 	return ret;
   2350 }
   2351 
   2352 
   2353 struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
   2354 					  int ndef)
   2355 {
   2356 	struct wpabuf *ret;
   2357 
   2358 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
   2359 	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
   2360 			   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
   2361 		return NULL;
   2362 
   2363 	ret = wps_build_nfc_handover_req(wpa_s->wps,
   2364 					 wpa_s->conf->wps_nfc_dh_pubkey);
   2365 
   2366 	if (ndef && ret) {
   2367 		struct wpabuf *tmp;
   2368 		tmp = ndef_build_wifi(ret);
   2369 		wpabuf_free(ret);
   2370 		if (tmp == NULL)
   2371 			return NULL;
   2372 		ret = tmp;
   2373 	}
   2374 
   2375 	return ret;
   2376 }
   2377 
   2378 
   2379 #ifdef CONFIG_WPS_NFC
   2380 
   2381 static struct wpabuf *
   2382 wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
   2383 			     const char *uuid)
   2384 {
   2385 #ifdef CONFIG_WPS_ER
   2386 	struct wpabuf *ret;
   2387 	u8 u[UUID_LEN], *use_uuid = NULL;
   2388 	u8 addr[ETH_ALEN], *use_addr = NULL;
   2389 	struct wps_context *wps = wpa_s->wps;
   2390 
   2391 	if (wps == NULL)
   2392 		return NULL;
   2393 
   2394 	if (uuid == NULL)
   2395 		return NULL;
   2396 	if (uuid_str2bin(uuid, u) == 0)
   2397 		use_uuid = u;
   2398 	else if (hwaddr_aton(uuid, addr) == 0)
   2399 		use_addr = addr;
   2400 	else
   2401 		return NULL;
   2402 
   2403 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
   2404 		if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
   2405 				   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
   2406 			return NULL;
   2407 	}
   2408 
   2409 	wpas_wps_nfc_clear(wps);
   2410 	wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
   2411 	wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
   2412 	wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
   2413 	if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
   2414 		wpas_wps_nfc_clear(wps);
   2415 		return NULL;
   2416 	}
   2417 
   2418 	ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
   2419 				      use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
   2420 	if (ndef && ret) {
   2421 		struct wpabuf *tmp;
   2422 		tmp = ndef_build_wifi(ret);
   2423 		wpabuf_free(ret);
   2424 		if (tmp == NULL)
   2425 			return NULL;
   2426 		ret = tmp;
   2427 	}
   2428 
   2429 	return ret;
   2430 #else /* CONFIG_WPS_ER */
   2431 	return NULL;
   2432 #endif /* CONFIG_WPS_ER */
   2433 }
   2434 #endif /* CONFIG_WPS_NFC */
   2435 
   2436 
   2437 struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
   2438 					  int ndef, int cr, const char *uuid)
   2439 {
   2440 	struct wpabuf *ret;
   2441 	if (!cr)
   2442 		return NULL;
   2443 	ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
   2444 	if (ret)
   2445 		return ret;
   2446 	return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
   2447 }
   2448 
   2449 
   2450 static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
   2451 					const struct wpabuf *data)
   2452 {
   2453 	struct wpabuf *wps;
   2454 	int ret = -1;
   2455 	u16 wsc_len;
   2456 	const u8 *pos;
   2457 	struct wpabuf msg;
   2458 	struct wps_parse_attr attr;
   2459 	u16 dev_pw_id;
   2460 	const u8 *bssid = NULL;
   2461 	int freq = 0;
   2462 
   2463 	wps = ndef_parse_wifi(data);
   2464 	if (wps == NULL)
   2465 		return -1;
   2466 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
   2467 		   "payload from NFC connection handover");
   2468 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
   2469 	if (wpabuf_len(wps) < 2) {
   2470 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
   2471 			   "Message");
   2472 		goto out;
   2473 	}
   2474 	pos = wpabuf_head(wps);
   2475 	wsc_len = WPA_GET_BE16(pos);
   2476 	if (wsc_len > wpabuf_len(wps) - 2) {
   2477 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
   2478 			   "in Wi-Fi Handover Select Message", wsc_len);
   2479 		goto out;
   2480 	}
   2481 	pos += 2;
   2482 
   2483 	wpa_hexdump(MSG_DEBUG,
   2484 		    "WPS: WSC attributes in Wi-Fi Handover Select Message",
   2485 		    pos, wsc_len);
   2486 	if (wsc_len < wpabuf_len(wps) - 2) {
   2487 		wpa_hexdump(MSG_DEBUG,
   2488 			    "WPS: Ignore extra data after WSC attributes",
   2489 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
   2490 	}
   2491 
   2492 	wpabuf_set(&msg, pos, wsc_len);
   2493 	ret = wps_parse_msg(&msg, &attr);
   2494 	if (ret < 0) {
   2495 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
   2496 			   "Wi-Fi Handover Select Message");
   2497 		goto out;
   2498 	}
   2499 
   2500 	if (attr.oob_dev_password == NULL ||
   2501 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
   2502 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
   2503 			   "included in Wi-Fi Handover Select Message");
   2504 		ret = -1;
   2505 		goto out;
   2506 	}
   2507 
   2508 	if (attr.ssid == NULL) {
   2509 		wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
   2510 			   "Select Message");
   2511 		ret = -1;
   2512 		goto out;
   2513 	}
   2514 
   2515 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
   2516 
   2517 	if (attr.mac_addr) {
   2518 		bssid = attr.mac_addr;
   2519 		wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
   2520 			   MAC2STR(bssid));
   2521 	}
   2522 
   2523 	if (attr.rf_bands)
   2524 		wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
   2525 
   2526 	if (attr.ap_channel) {
   2527 		u16 chan = WPA_GET_BE16(attr.ap_channel);
   2528 
   2529 		wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
   2530 
   2531 		if (chan >= 1 && chan <= 13 &&
   2532 		    (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
   2533 			freq = 2407 + 5 * chan;
   2534 		else if (chan == 14 &&
   2535 			 (attr.rf_bands == NULL ||
   2536 			  *attr.rf_bands & WPS_RF_24GHZ))
   2537 			freq = 2484;
   2538 		else if (chan >= 30 &&
   2539 			 (attr.rf_bands == NULL ||
   2540 			  *attr.rf_bands & WPS_RF_50GHZ))
   2541 			freq = 5000 + 5 * chan;
   2542 
   2543 		if (freq) {
   2544 			wpa_printf(MSG_DEBUG,
   2545 				   "WPS: AP indicated channel %u -> %u MHz",
   2546 				   chan, freq);
   2547 		}
   2548 	}
   2549 
   2550 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
   2551 		    attr.oob_dev_password, attr.oob_dev_password_len);
   2552 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
   2553 				 WPS_OOB_PUBKEY_HASH_LEN);
   2554 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
   2555 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
   2556 			   "%u in Wi-Fi Handover Select Message", dev_pw_id);
   2557 		ret = -1;
   2558 		goto out;
   2559 	}
   2560 	wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
   2561 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
   2562 
   2563 	ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
   2564 				 attr.oob_dev_password,
   2565 				 attr.ssid, attr.ssid_len, freq);
   2566 
   2567 out:
   2568 	wpabuf_free(wps);
   2569 	return ret;
   2570 }
   2571 
   2572 
   2573 int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
   2574 				 const struct wpabuf *req,
   2575 				 const struct wpabuf *sel)
   2576 {
   2577 	wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
   2578 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
   2579 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
   2580 	return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
   2581 }
   2582 
   2583 
   2584 int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
   2585 				    const struct wpabuf *req,
   2586 				    const struct wpabuf *sel)
   2587 {
   2588 	struct wpabuf *wps;
   2589 	int ret = -1;
   2590 	u16 wsc_len;
   2591 	const u8 *pos;
   2592 	struct wpabuf msg;
   2593 	struct wps_parse_attr attr;
   2594 	u16 dev_pw_id;
   2595 
   2596 	/*
   2597 	 * Enrollee/station is always initiator of the NFC connection handover,
   2598 	 * so use the request message here to find Enrollee public key hash.
   2599 	 */
   2600 	wps = ndef_parse_wifi(req);
   2601 	if (wps == NULL)
   2602 		return -1;
   2603 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
   2604 		   "payload from NFC connection handover");
   2605 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
   2606 	if (wpabuf_len(wps) < 2) {
   2607 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
   2608 			   "Message");
   2609 		goto out;
   2610 	}
   2611 	pos = wpabuf_head(wps);
   2612 	wsc_len = WPA_GET_BE16(pos);
   2613 	if (wsc_len > wpabuf_len(wps) - 2) {
   2614 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
   2615 			   "in rt Wi-Fi Handover Request Message", wsc_len);
   2616 		goto out;
   2617 	}
   2618 	pos += 2;
   2619 
   2620 	wpa_hexdump(MSG_DEBUG,
   2621 		    "WPS: WSC attributes in Wi-Fi Handover Request Message",
   2622 		    pos, wsc_len);
   2623 	if (wsc_len < wpabuf_len(wps) - 2) {
   2624 		wpa_hexdump(MSG_DEBUG,
   2625 			    "WPS: Ignore extra data after WSC attributes",
   2626 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
   2627 	}
   2628 
   2629 	wpabuf_set(&msg, pos, wsc_len);
   2630 	ret = wps_parse_msg(&msg, &attr);
   2631 	if (ret < 0) {
   2632 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
   2633 			   "Wi-Fi Handover Request Message");
   2634 		goto out;
   2635 	}
   2636 
   2637 	if (attr.oob_dev_password == NULL ||
   2638 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
   2639 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
   2640 			   "included in Wi-Fi Handover Request Message");
   2641 		ret = -1;
   2642 		goto out;
   2643 	}
   2644 
   2645 	if (attr.uuid_e == NULL) {
   2646 		wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
   2647 			   "Handover Request Message");
   2648 		ret = -1;
   2649 		goto out;
   2650 	}
   2651 
   2652 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
   2653 
   2654 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
   2655 		    attr.oob_dev_password, attr.oob_dev_password_len);
   2656 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
   2657 				 WPS_OOB_PUBKEY_HASH_LEN);
   2658 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
   2659 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
   2660 			   "%u in Wi-Fi Handover Request Message", dev_pw_id);
   2661 		ret = -1;
   2662 		goto out;
   2663 	}
   2664 	wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
   2665 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
   2666 
   2667 	ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
   2668 					     attr.oob_dev_password,
   2669 					     DEV_PW_NFC_CONNECTION_HANDOVER,
   2670 					     NULL, 0, 1);
   2671 
   2672 out:
   2673 	wpabuf_free(wps);
   2674 	return ret;
   2675 }
   2676 
   2677 #endif /* CONFIG_WPS_NFC */
   2678 
   2679 
   2680 static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
   2681 {
   2682 	size_t i;
   2683 	struct os_reltime now;
   2684 
   2685 	if (wpa_debug_level > MSG_DEBUG)
   2686 		return;
   2687 
   2688 	if (wpa_s->wps_ap == NULL)
   2689 		return;
   2690 
   2691 	os_get_reltime(&now);
   2692 
   2693 	for (i = 0; i < wpa_s->num_wps_ap; i++) {
   2694 		struct wps_ap_info *ap = &wpa_s->wps_ap[i];
   2695 		struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
   2696 
   2697 		wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
   2698 			   "tries=%d last_attempt=%d sec ago blacklist=%d",
   2699 			   (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
   2700 			   ap->last_attempt.sec > 0 ?
   2701 			   (int) now.sec - (int) ap->last_attempt.sec : -1,
   2702 			   e ? e->count : 0);
   2703 	}
   2704 }
   2705 
   2706 
   2707 static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
   2708 						 const u8 *bssid)
   2709 {
   2710 	size_t i;
   2711 
   2712 	if (wpa_s->wps_ap == NULL)
   2713 		return NULL;
   2714 
   2715 	for (i = 0; i < wpa_s->num_wps_ap; i++) {
   2716 		struct wps_ap_info *ap = &wpa_s->wps_ap[i];
   2717 		if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
   2718 			return ap;
   2719 	}
   2720 
   2721 	return NULL;
   2722 }
   2723 
   2724 
   2725 static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
   2726 					struct wpa_scan_res *res)
   2727 {
   2728 	struct wpabuf *wps;
   2729 	enum wps_ap_info_type type;
   2730 	struct wps_ap_info *ap;
   2731 	int r;
   2732 
   2733 	if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
   2734 		return;
   2735 
   2736 	wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
   2737 	if (wps == NULL)
   2738 		return;
   2739 
   2740 	r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
   2741 	if (r == 2)
   2742 		type = WPS_AP_SEL_REG_OUR;
   2743 	else if (r == 1)
   2744 		type = WPS_AP_SEL_REG;
   2745 	else
   2746 		type = WPS_AP_NOT_SEL_REG;
   2747 
   2748 	wpabuf_free(wps);
   2749 
   2750 	ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
   2751 	if (ap) {
   2752 		if (ap->type != type) {
   2753 			wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
   2754 				   " changed type %d -> %d",
   2755 				   MAC2STR(res->bssid), ap->type, type);
   2756 			ap->type = type;
   2757 			if (type != WPS_AP_NOT_SEL_REG)
   2758 				wpa_blacklist_del(wpa_s, ap->bssid);
   2759 		}
   2760 		return;
   2761 	}
   2762 
   2763 	ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
   2764 			      sizeof(struct wps_ap_info));
   2765 	if (ap == NULL)
   2766 		return;
   2767 
   2768 	wpa_s->wps_ap = ap;
   2769 	ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
   2770 	wpa_s->num_wps_ap++;
   2771 
   2772 	os_memset(ap, 0, sizeof(*ap));
   2773 	os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
   2774 	ap->type = type;
   2775 	wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
   2776 		   MAC2STR(ap->bssid), ap->type);
   2777 }
   2778 
   2779 
   2780 void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
   2781 			     struct wpa_scan_results *scan_res)
   2782 {
   2783 	size_t i;
   2784 
   2785 	for (i = 0; i < scan_res->num; i++)
   2786 		wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
   2787 
   2788 	wpas_wps_dump_ap_info(wpa_s);
   2789 }
   2790 
   2791 
   2792 void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
   2793 {
   2794 	struct wps_ap_info *ap;
   2795 
   2796 	wpa_s->after_wps = 0;
   2797 
   2798 	if (!wpa_s->wps_ap_iter)
   2799 		return;
   2800 	ap = wpas_wps_get_ap_info(wpa_s, bssid);
   2801 	if (ap == NULL)
   2802 		return;
   2803 	ap->tries++;
   2804 	os_get_reltime(&ap->last_attempt);
   2805 }
   2806