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