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