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