139beb93cSSam Leffler /*
239beb93cSSam Leffler  * wpa_supplicant / WPS integration
35b9c547cSRui Paulo  * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
739beb93cSSam Leffler  */
839beb93cSSam Leffler 
939beb93cSSam Leffler #include "includes.h"
1039beb93cSSam Leffler 
1139beb93cSSam Leffler #include "common.h"
1239beb93cSSam Leffler #include "eloop.h"
1339beb93cSSam Leffler #include "uuid.h"
14f05cddf9SRui Paulo #include "crypto/random.h"
15e28a4053SRui Paulo #include "crypto/dh_group5.h"
16e28a4053SRui Paulo #include "common/ieee802_11_defs.h"
17e28a4053SRui Paulo #include "common/ieee802_11_common.h"
18e28a4053SRui Paulo #include "common/wpa_common.h"
19e28a4053SRui Paulo #include "common/wpa_ctrl.h"
2039beb93cSSam Leffler #include "eap_common/eap_wsc_common.h"
21e28a4053SRui Paulo #include "eap_peer/eap.h"
22f05cddf9SRui Paulo #include "eapol_supp/eapol_supp_sm.h"
23e28a4053SRui Paulo #include "rsn_supp/wpa.h"
24f05cddf9SRui Paulo #include "wps/wps_attr_parse.h"
25e28a4053SRui Paulo #include "config.h"
26e28a4053SRui Paulo #include "wpa_supplicant_i.h"
27e28a4053SRui Paulo #include "driver_i.h"
28e28a4053SRui Paulo #include "notify.h"
29*c1d255d3SCy Schubert #include "bssid_ignore.h"
30e28a4053SRui Paulo #include "bss.h"
31e28a4053SRui Paulo #include "scan.h"
32f05cddf9SRui Paulo #include "ap.h"
33f05cddf9SRui Paulo #include "p2p/p2p.h"
34f05cddf9SRui Paulo #include "p2p_supplicant.h"
3539beb93cSSam Leffler #include "wps_supplicant.h"
3639beb93cSSam Leffler 
373157ba21SRui Paulo 
38f05cddf9SRui Paulo #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
3939beb93cSSam Leffler #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40f05cddf9SRui Paulo #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
4139beb93cSSam Leffler 
42325151a3SRui Paulo /*
43325151a3SRui Paulo  * The minimum time in seconds before trying to associate to a WPS PIN AP that
44325151a3SRui Paulo  * does not have Selected Registrar TRUE.
45325151a3SRui Paulo  */
46325151a3SRui Paulo #ifndef WPS_PIN_TIME_IGNORE_SEL_REG
47325151a3SRui Paulo #define WPS_PIN_TIME_IGNORE_SEL_REG 5
48325151a3SRui Paulo #endif /* WPS_PIN_TIME_IGNORE_SEL_REG */
49325151a3SRui Paulo 
5039beb93cSSam Leffler static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
5139beb93cSSam Leffler static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
5239beb93cSSam Leffler 
5339beb93cSSam Leffler 
wpas_wps_clear_ap_info(struct wpa_supplicant * wpa_s)54f05cddf9SRui Paulo static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
55f05cddf9SRui Paulo {
56f05cddf9SRui Paulo 	os_free(wpa_s->wps_ap);
57f05cddf9SRui Paulo 	wpa_s->wps_ap = NULL;
58f05cddf9SRui Paulo 	wpa_s->num_wps_ap = 0;
59f05cddf9SRui Paulo 	wpa_s->wps_ap_iter = 0;
60f05cddf9SRui Paulo }
61f05cddf9SRui Paulo 
62f05cddf9SRui Paulo 
wpas_wps_assoc_with_cred(void * eloop_ctx,void * timeout_ctx)635b9c547cSRui Paulo static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
645b9c547cSRui Paulo {
655b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
665b9c547cSRui Paulo 	int use_fast_assoc = timeout_ctx != NULL;
675b9c547cSRui Paulo 
685b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
695b9c547cSRui Paulo 	if (!use_fast_assoc ||
705b9c547cSRui Paulo 	    wpa_supplicant_fast_associate(wpa_s) != 1)
715b9c547cSRui Paulo 		wpa_supplicant_req_scan(wpa_s, 0, 0);
725b9c547cSRui Paulo }
735b9c547cSRui Paulo 
745b9c547cSRui Paulo 
wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant * wpa_s)755b9c547cSRui Paulo static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
765b9c547cSRui Paulo {
775b9c547cSRui Paulo 	eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
785b9c547cSRui Paulo 	eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
795b9c547cSRui Paulo }
805b9c547cSRui Paulo 
815b9c547cSRui Paulo 
wpas_wps_eapol_cb(struct wpa_supplicant * wpa_s)8239beb93cSSam Leffler int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
8339beb93cSSam Leffler {
845b9c547cSRui Paulo 	if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
855b9c547cSRui Paulo 		return 1;
865b9c547cSRui Paulo 
8739beb93cSSam Leffler 	if (!wpa_s->wps_success &&
8839beb93cSSam Leffler 	    wpa_s->current_ssid &&
8939beb93cSSam Leffler 	    eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
9039beb93cSSam Leffler 		const u8 *bssid = wpa_s->bssid;
9139beb93cSSam Leffler 		if (is_zero_ether_addr(bssid))
9239beb93cSSam Leffler 			bssid = wpa_s->pending_bssid;
9339beb93cSSam Leffler 
9439beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
9539beb93cSSam Leffler 			   " did not succeed - continue trying to find "
9639beb93cSSam Leffler 			   "suitable AP", MAC2STR(bssid));
97*c1d255d3SCy Schubert 		wpa_bssid_ignore_add(wpa_s, bssid);
9839beb93cSSam Leffler 
9939beb93cSSam Leffler 		wpa_supplicant_deauthenticate(wpa_s,
10039beb93cSSam Leffler 					      WLAN_REASON_DEAUTH_LEAVING);
10139beb93cSSam Leffler 		wpa_s->reassociate = 1;
10239beb93cSSam Leffler 		wpa_supplicant_req_scan(wpa_s,
103*c1d255d3SCy Schubert 					wpa_s->bssid_ignore_cleared ? 5 : 0, 0);
104*c1d255d3SCy Schubert 		wpa_s->bssid_ignore_cleared = false;
10539beb93cSSam Leffler 		return 1;
10639beb93cSSam Leffler 	}
10739beb93cSSam Leffler 
108f05cddf9SRui Paulo 	wpas_wps_clear_ap_info(wpa_s);
10939beb93cSSam Leffler 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
110f05cddf9SRui Paulo 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
111f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
11239beb93cSSam Leffler 
11339beb93cSSam Leffler 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
11439beb93cSSam Leffler 	    !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
115f05cddf9SRui Paulo 		int disabled = wpa_s->current_ssid->disabled;
116f05cddf9SRui Paulo 		unsigned int freq = wpa_s->assoc_freq;
1175b9c547cSRui Paulo 		struct wpa_bss *bss;
1185b9c547cSRui Paulo 		struct wpa_ssid *ssid = NULL;
1195b9c547cSRui Paulo 		int use_fast_assoc = 0;
1205b9c547cSRui Paulo 
12139beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
122f05cddf9SRui Paulo 			   "try to associate with the received credential "
123f05cddf9SRui Paulo 			   "(freq=%u)", freq);
1245b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
12539beb93cSSam Leffler 		wpa_supplicant_deauthenticate(wpa_s,
12639beb93cSSam Leffler 					      WLAN_REASON_DEAUTH_LEAVING);
127f05cddf9SRui Paulo 		if (disabled) {
128f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "WPS: Current network is "
129f05cddf9SRui Paulo 				   "disabled - wait for user to enable");
130f05cddf9SRui Paulo 			return 1;
131f05cddf9SRui Paulo 		}
132e28a4053SRui Paulo 		wpa_s->after_wps = 5;
133f05cddf9SRui Paulo 		wpa_s->wps_freq = freq;
134f05cddf9SRui Paulo 		wpa_s->normal_scans = 0;
13539beb93cSSam Leffler 		wpa_s->reassociate = 1;
1365b9c547cSRui Paulo 
1375b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
1385b9c547cSRui Paulo 			   "without a new scan can be used");
1395b9c547cSRui Paulo 		bss = wpa_supplicant_pick_network(wpa_s, &ssid);
1405b9c547cSRui Paulo 		if (bss) {
1415b9c547cSRui Paulo 			struct wpabuf *wps;
1425b9c547cSRui Paulo 			struct wps_parse_attr attr;
1435b9c547cSRui Paulo 
1445b9c547cSRui Paulo 			wps = wpa_bss_get_vendor_ie_multi(bss,
1455b9c547cSRui Paulo 							  WPS_IE_VENDOR_TYPE);
1465b9c547cSRui Paulo 			if (wps && wps_parse_msg(wps, &attr) == 0 &&
1475b9c547cSRui Paulo 			    attr.wps_state &&
1485b9c547cSRui Paulo 			    *attr.wps_state == WPS_STATE_CONFIGURED)
1495b9c547cSRui Paulo 				use_fast_assoc = 1;
1505b9c547cSRui Paulo 			wpabuf_free(wps);
1515b9c547cSRui Paulo 		}
1525b9c547cSRui Paulo 
1535b9c547cSRui Paulo 		/*
1545b9c547cSRui Paulo 		 * Complete the next step from an eloop timeout to allow pending
1555b9c547cSRui Paulo 		 * driver events related to the disconnection to be processed
1565b9c547cSRui Paulo 		 * first. This makes it less likely for disconnection event to
1575b9c547cSRui Paulo 		 * cause problems with the following connection.
1585b9c547cSRui Paulo 		 */
1595b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
1605b9c547cSRui Paulo 		wpas_wps_assoc_with_cred_cancel(wpa_s);
1615b9c547cSRui Paulo 		eloop_register_timeout(0, 10000,
1625b9c547cSRui Paulo 				       wpas_wps_assoc_with_cred, wpa_s,
1635b9c547cSRui Paulo 				       use_fast_assoc ? (void *) 1 :
1645b9c547cSRui Paulo 				       (void *) 0);
16539beb93cSSam Leffler 		return 1;
16639beb93cSSam Leffler 	}
16739beb93cSSam Leffler 
16839beb93cSSam Leffler 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
16939beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
17039beb93cSSam Leffler 			   "for external credential processing");
17139beb93cSSam Leffler 		wpas_clear_wps(wpa_s);
1725b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
17339beb93cSSam Leffler 		wpa_supplicant_deauthenticate(wpa_s,
17439beb93cSSam Leffler 					      WLAN_REASON_DEAUTH_LEAVING);
17539beb93cSSam Leffler 		return 1;
17639beb93cSSam Leffler 	}
17739beb93cSSam Leffler 
17839beb93cSSam Leffler 	return 0;
17939beb93cSSam Leffler }
18039beb93cSSam Leffler 
18139beb93cSSam Leffler 
wpas_wps_security_workaround(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const struct wps_credential * cred)1823157ba21SRui Paulo static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
1833157ba21SRui Paulo 					 struct wpa_ssid *ssid,
1843157ba21SRui Paulo 					 const struct wps_credential *cred)
1853157ba21SRui Paulo {
1863157ba21SRui Paulo 	struct wpa_driver_capa capa;
187e28a4053SRui Paulo 	struct wpa_bss *bss;
1883157ba21SRui Paulo 	const u8 *ie;
1893157ba21SRui Paulo 	struct wpa_ie_data adv;
1903157ba21SRui Paulo 	int wpa2 = 0, ccmp = 0;
191*c1d255d3SCy Schubert 	enum wpa_driver_if_type iftype;
1923157ba21SRui Paulo 
1933157ba21SRui Paulo 	/*
1943157ba21SRui Paulo 	 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
1953157ba21SRui Paulo 	 * case they are configured for mixed mode operation (WPA+WPA2 and
1963157ba21SRui Paulo 	 * TKIP+CCMP). Try to use scan results to figure out whether the AP
1973157ba21SRui Paulo 	 * actually supports stronger security and select that if the client
1983157ba21SRui Paulo 	 * has support for it, too.
1993157ba21SRui Paulo 	 */
2003157ba21SRui Paulo 
2013157ba21SRui Paulo 	if (wpa_drv_get_capa(wpa_s, &capa))
2023157ba21SRui Paulo 		return; /* Unknown what driver supports */
2033157ba21SRui Paulo 
204f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
205f05cddf9SRui Paulo 		return;
206e28a4053SRui Paulo 	bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
20785732ac8SCy Schubert 	if (!bss)
20885732ac8SCy Schubert 		bss = wpa_bss_get(wpa_s, wpa_s->bssid,
20985732ac8SCy Schubert 				  ssid->ssid, ssid->ssid_len);
210e28a4053SRui Paulo 	if (bss == NULL) {
211e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
212e28a4053SRui Paulo 			   "table - use credential as-is");
2133157ba21SRui Paulo 		return;
2143157ba21SRui Paulo 	}
2153157ba21SRui Paulo 
216e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
217e28a4053SRui Paulo 
218e28a4053SRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2193157ba21SRui Paulo 	if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
2203157ba21SRui Paulo 		wpa2 = 1;
2213157ba21SRui Paulo 		if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
2223157ba21SRui Paulo 			ccmp = 1;
2233157ba21SRui Paulo 	} else {
224e28a4053SRui Paulo 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2253157ba21SRui Paulo 		if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
2263157ba21SRui Paulo 		    adv.pairwise_cipher & WPA_CIPHER_CCMP)
2273157ba21SRui Paulo 			ccmp = 1;
2283157ba21SRui Paulo 	}
2293157ba21SRui Paulo 
2303157ba21SRui Paulo 	if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
2313157ba21SRui Paulo 	    (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
2323157ba21SRui Paulo 		/*
2333157ba21SRui Paulo 		 * TODO: This could be the initial AP configuration and the
2343157ba21SRui Paulo 		 * Beacon contents could change shortly. Should request a new
2353157ba21SRui Paulo 		 * scan and delay addition of the network until the updated
2363157ba21SRui Paulo 		 * scan results are available.
2373157ba21SRui Paulo 		 */
2383157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
2393157ba21SRui Paulo 			   "support - use credential as-is");
2403157ba21SRui Paulo 		return;
2413157ba21SRui Paulo 	}
2423157ba21SRui Paulo 
243*c1d255d3SCy Schubert 	iftype = ssid->p2p_group ? WPA_IF_P2P_CLIENT : WPA_IF_STATION;
244*c1d255d3SCy Schubert 
2453157ba21SRui Paulo 	if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
2463157ba21SRui Paulo 	    (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
247*c1d255d3SCy Schubert 	    (capa.key_mgmt_iftype[iftype] &
248*c1d255d3SCy Schubert 	     WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2493157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
2503157ba21SRui Paulo 			   "based on scan results");
2513157ba21SRui Paulo 		if (wpa_s->conf->ap_scan == 1)
2523157ba21SRui Paulo 			ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
2533157ba21SRui Paulo 		else
2543157ba21SRui Paulo 			ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2553157ba21SRui Paulo 	}
2563157ba21SRui Paulo 
2573157ba21SRui Paulo 	if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
2583157ba21SRui Paulo 	    (ssid->proto & WPA_PROTO_WPA) &&
2593157ba21SRui Paulo 	    (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
2603157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
2613157ba21SRui Paulo 			   "based on scan results");
2623157ba21SRui Paulo 		if (wpa_s->conf->ap_scan == 1)
2633157ba21SRui Paulo 			ssid->proto |= WPA_PROTO_RSN;
2643157ba21SRui Paulo 		else
2653157ba21SRui Paulo 			ssid->proto = WPA_PROTO_RSN;
2663157ba21SRui Paulo 	}
2673157ba21SRui Paulo }
2683157ba21SRui Paulo 
2693157ba21SRui Paulo 
wpas_wps_remove_dup_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * new_ssid)2705b9c547cSRui Paulo static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
2715b9c547cSRui Paulo 					struct wpa_ssid *new_ssid)
2725b9c547cSRui Paulo {
2735b9c547cSRui Paulo 	struct wpa_ssid *ssid, *next;
2745b9c547cSRui Paulo 
2755b9c547cSRui Paulo 	for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
2765b9c547cSRui Paulo 	     ssid = next, next = ssid ? ssid->next : NULL) {
2775b9c547cSRui Paulo 		/*
2785b9c547cSRui Paulo 		 * new_ssid has already been added to the list in
2795b9c547cSRui Paulo 		 * wpas_wps_add_network(), so skip it.
2805b9c547cSRui Paulo 		 */
2815b9c547cSRui Paulo 		if (ssid == new_ssid)
2825b9c547cSRui Paulo 			continue;
2835b9c547cSRui Paulo 
2845b9c547cSRui Paulo 		if (ssid->bssid_set || new_ssid->bssid_set) {
2855b9c547cSRui Paulo 			if (ssid->bssid_set != new_ssid->bssid_set)
2865b9c547cSRui Paulo 				continue;
2875b9c547cSRui Paulo 			if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
2885b9c547cSRui Paulo 			    0)
2895b9c547cSRui Paulo 				continue;
2905b9c547cSRui Paulo 		}
2915b9c547cSRui Paulo 
2925b9c547cSRui Paulo 		/* compare SSID */
2935b9c547cSRui Paulo 		if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
2945b9c547cSRui Paulo 			continue;
2955b9c547cSRui Paulo 
2965b9c547cSRui Paulo 		if (ssid->ssid && new_ssid->ssid) {
2975b9c547cSRui Paulo 			if (os_memcmp(ssid->ssid, new_ssid->ssid,
2985b9c547cSRui Paulo 				      ssid->ssid_len) != 0)
2995b9c547cSRui Paulo 				continue;
3005b9c547cSRui Paulo 		} else if (ssid->ssid || new_ssid->ssid)
3015b9c547cSRui Paulo 			continue;
3025b9c547cSRui Paulo 
3035b9c547cSRui Paulo 		/* compare security parameters */
3045b9c547cSRui Paulo 		if (ssid->auth_alg != new_ssid->auth_alg ||
3055b9c547cSRui Paulo 		    ssid->key_mgmt != new_ssid->key_mgmt ||
3065b9c547cSRui Paulo 		    (ssid->group_cipher != new_ssid->group_cipher &&
3075b9c547cSRui Paulo 		     !(ssid->group_cipher & new_ssid->group_cipher &
3085b9c547cSRui Paulo 		       WPA_CIPHER_CCMP)))
3095b9c547cSRui Paulo 			continue;
3105b9c547cSRui Paulo 
3115b9c547cSRui Paulo 		/*
3125b9c547cSRui Paulo 		 * Some existing WPS APs will send two creds in case they are
3135b9c547cSRui Paulo 		 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
3145b9c547cSRui Paulo 		 * Try to merge these two creds if they are received in the same
3155b9c547cSRui Paulo 		 * M8 message.
3165b9c547cSRui Paulo 		 */
3175b9c547cSRui Paulo 		if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
3185b9c547cSRui Paulo 		    wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
3195b9c547cSRui Paulo 			if (new_ssid->passphrase && ssid->passphrase &&
3205b9c547cSRui Paulo 			    os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
3215b9c547cSRui Paulo 			    0) {
3225b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG,
3235b9c547cSRui Paulo 					   "WPS: M8 Creds with different passphrase - do not merge");
3245b9c547cSRui Paulo 				continue;
3255b9c547cSRui Paulo 			}
3265b9c547cSRui Paulo 
3275b9c547cSRui Paulo 			if (new_ssid->psk_set &&
3285b9c547cSRui Paulo 			    (!ssid->psk_set ||
3295b9c547cSRui Paulo 			     os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
3305b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG,
3315b9c547cSRui Paulo 					   "WPS: M8 Creds with different PSK - do not merge");
3325b9c547cSRui Paulo 				continue;
3335b9c547cSRui Paulo 			}
3345b9c547cSRui Paulo 
3355b9c547cSRui Paulo 			if ((new_ssid->passphrase && !ssid->passphrase) ||
3365b9c547cSRui Paulo 			    (!new_ssid->passphrase && ssid->passphrase)) {
3375b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG,
3385b9c547cSRui Paulo 					   "WPS: M8 Creds with different passphrase/PSK type - do not merge");
3395b9c547cSRui Paulo 				continue;
3405b9c547cSRui Paulo 			}
3415b9c547cSRui Paulo 
3425b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3435b9c547cSRui Paulo 				   "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
3445b9c547cSRui Paulo 			new_ssid->proto |= ssid->proto;
3455b9c547cSRui Paulo 			new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
3465b9c547cSRui Paulo 		} else {
3475b9c547cSRui Paulo 			/*
3485b9c547cSRui Paulo 			 * proto and pairwise_cipher difference matter for
3495b9c547cSRui Paulo 			 * non-mixed-mode creds.
3505b9c547cSRui Paulo 			 */
3515b9c547cSRui Paulo 			if (ssid->proto != new_ssid->proto ||
3525b9c547cSRui Paulo 			    ssid->pairwise_cipher != new_ssid->pairwise_cipher)
3535b9c547cSRui Paulo 				continue;
3545b9c547cSRui Paulo 		}
3555b9c547cSRui Paulo 
3565b9c547cSRui Paulo 		/* Remove the duplicated older network entry. */
3575b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
3585b9c547cSRui Paulo 		wpas_notify_network_removed(wpa_s, ssid);
3595b9c547cSRui Paulo 		if (wpa_s->current_ssid == ssid)
3605b9c547cSRui Paulo 			wpa_s->current_ssid = NULL;
3615b9c547cSRui Paulo 		wpa_config_remove_network(wpa_s->conf, ssid->id);
3625b9c547cSRui Paulo 	}
3635b9c547cSRui Paulo }
3645b9c547cSRui Paulo 
3655b9c547cSRui Paulo 
wpa_supplicant_wps_cred(void * ctx,const struct wps_credential * cred)36639beb93cSSam Leffler static int wpa_supplicant_wps_cred(void *ctx,
36739beb93cSSam Leffler 				   const struct wps_credential *cred)
36839beb93cSSam Leffler {
36939beb93cSSam Leffler 	struct wpa_supplicant *wpa_s = ctx;
37039beb93cSSam Leffler 	struct wpa_ssid *ssid = wpa_s->current_ssid;
3713157ba21SRui Paulo 	u16 auth_type;
372f05cddf9SRui Paulo #ifdef CONFIG_WPS_REG_DISABLE_OPEN
373f05cddf9SRui Paulo 	int registrar = 0;
374f05cddf9SRui Paulo #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
375*c1d255d3SCy Schubert 	bool add_sae;
37639beb93cSSam Leffler 
37739beb93cSSam Leffler 	if ((wpa_s->conf->wps_cred_processing == 1 ||
37839beb93cSSam Leffler 	     wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
37939beb93cSSam Leffler 		size_t blen = cred->cred_attr_len * 2 + 1;
38039beb93cSSam Leffler 		char *buf = os_malloc(blen);
38139beb93cSSam Leffler 		if (buf) {
38239beb93cSSam Leffler 			wpa_snprintf_hex(buf, blen,
38339beb93cSSam Leffler 					 cred->cred_attr, cred->cred_attr_len);
38439beb93cSSam Leffler 			wpa_msg(wpa_s, MSG_INFO, "%s%s",
38539beb93cSSam Leffler 				WPS_EVENT_CRED_RECEIVED, buf);
38639beb93cSSam Leffler 			os_free(buf);
38739beb93cSSam Leffler 		}
388e28a4053SRui Paulo 
389e28a4053SRui Paulo 		wpas_notify_wps_credential(wpa_s, cred);
39039beb93cSSam Leffler 	} else
39139beb93cSSam Leffler 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
39239beb93cSSam Leffler 
39339beb93cSSam Leffler 	wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
39439beb93cSSam Leffler 			cred->cred_attr, cred->cred_attr_len);
39539beb93cSSam Leffler 
39639beb93cSSam Leffler 	if (wpa_s->conf->wps_cred_processing == 1)
39739beb93cSSam Leffler 		return 0;
39839beb93cSSam Leffler 
3993157ba21SRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
4003157ba21SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
40139beb93cSSam Leffler 		   cred->auth_type);
4023157ba21SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
4033157ba21SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
4043157ba21SRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
4053157ba21SRui Paulo 			cred->key, cred->key_len);
4063157ba21SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
4073157ba21SRui Paulo 		   MAC2STR(cred->mac_addr));
4083157ba21SRui Paulo 
4093157ba21SRui Paulo 	auth_type = cred->auth_type;
4103157ba21SRui Paulo 	if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
4113157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
4123157ba21SRui Paulo 			   "auth_type into WPA2PSK");
4133157ba21SRui Paulo 		auth_type = WPS_AUTH_WPA2PSK;
4143157ba21SRui Paulo 	}
4153157ba21SRui Paulo 
4163157ba21SRui Paulo 	if (auth_type != WPS_AUTH_OPEN &&
4173157ba21SRui Paulo 	    auth_type != WPS_AUTH_WPAPSK &&
4183157ba21SRui Paulo 	    auth_type != WPS_AUTH_WPA2PSK) {
4193157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
4203157ba21SRui Paulo 			   "unsupported authentication type 0x%x",
4213157ba21SRui Paulo 			   auth_type);
42239beb93cSSam Leffler 		return 0;
42339beb93cSSam Leffler 	}
42439beb93cSSam Leffler 
425f05cddf9SRui Paulo 	if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
426f05cddf9SRui Paulo 		if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
427f05cddf9SRui Paulo 			wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
428f05cddf9SRui Paulo 				   "invalid Network Key length %lu",
429f05cddf9SRui Paulo 				   (unsigned long) cred->key_len);
430f05cddf9SRui Paulo 			return -1;
431f05cddf9SRui Paulo 		}
432f05cddf9SRui Paulo 	}
433f05cddf9SRui Paulo 
43439beb93cSSam Leffler 	if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
43539beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
43639beb93cSSam Leffler 			   "on the received credential");
437f05cddf9SRui Paulo #ifdef CONFIG_WPS_REG_DISABLE_OPEN
438f05cddf9SRui Paulo 		if (ssid->eap.identity &&
439f05cddf9SRui Paulo 		    ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
440f05cddf9SRui Paulo 		    os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
441f05cddf9SRui Paulo 			      WSC_ID_REGISTRAR_LEN) == 0)
442f05cddf9SRui Paulo 			registrar = 1;
443f05cddf9SRui Paulo #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
44439beb93cSSam Leffler 		os_free(ssid->eap.identity);
44539beb93cSSam Leffler 		ssid->eap.identity = NULL;
44639beb93cSSam Leffler 		ssid->eap.identity_len = 0;
44739beb93cSSam Leffler 		os_free(ssid->eap.phase1);
44839beb93cSSam Leffler 		ssid->eap.phase1 = NULL;
44939beb93cSSam Leffler 		os_free(ssid->eap.eap_methods);
45039beb93cSSam Leffler 		ssid->eap.eap_methods = NULL;
451f05cddf9SRui Paulo 		if (!ssid->p2p_group) {
452f05cddf9SRui Paulo 			ssid->temporary = 0;
453f05cddf9SRui Paulo 			ssid->bssid_set = 0;
454f05cddf9SRui Paulo 		}
455f05cddf9SRui Paulo 		ssid->disabled_until.sec = 0;
456f05cddf9SRui Paulo 		ssid->disabled_until.usec = 0;
457f05cddf9SRui Paulo 		ssid->auth_failures = 0;
45839beb93cSSam Leffler 	} else {
45939beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
46039beb93cSSam Leffler 			   "received credential");
46139beb93cSSam Leffler 		ssid = wpa_config_add_network(wpa_s->conf);
46239beb93cSSam Leffler 		if (ssid == NULL)
46339beb93cSSam Leffler 			return -1;
4645b9c547cSRui Paulo 		if (wpa_s->current_ssid) {
4655b9c547cSRui Paulo 			/*
4665b9c547cSRui Paulo 			 * Should the GO issue multiple credentials for some
4675b9c547cSRui Paulo 			 * reason, each credential should be marked as a
4685b9c547cSRui Paulo 			 * temporary P2P group similarly to the one that gets
4695b9c547cSRui Paulo 			 * marked as such based on the pre-configured values
4705b9c547cSRui Paulo 			 * used for the WPS network block.
4715b9c547cSRui Paulo 			 */
4725b9c547cSRui Paulo 			ssid->p2p_group = wpa_s->current_ssid->p2p_group;
4735b9c547cSRui Paulo 			ssid->temporary = wpa_s->current_ssid->temporary;
4745b9c547cSRui Paulo 		}
475e28a4053SRui Paulo 		wpas_notify_network_added(wpa_s, ssid);
47639beb93cSSam Leffler 	}
47739beb93cSSam Leffler 
47839beb93cSSam Leffler 	wpa_config_set_network_defaults(ssid);
4795b9c547cSRui Paulo 	ssid->wps_run = wpa_s->wps_run;
48039beb93cSSam Leffler 
48139beb93cSSam Leffler 	os_free(ssid->ssid);
48239beb93cSSam Leffler 	ssid->ssid = os_malloc(cred->ssid_len);
48339beb93cSSam Leffler 	if (ssid->ssid) {
48439beb93cSSam Leffler 		os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
48539beb93cSSam Leffler 		ssid->ssid_len = cred->ssid_len;
48639beb93cSSam Leffler 	}
48739beb93cSSam Leffler 
48839beb93cSSam Leffler 	switch (cred->encr_type) {
48939beb93cSSam Leffler 	case WPS_ENCR_NONE:
49039beb93cSSam Leffler 		break;
49139beb93cSSam Leffler 	case WPS_ENCR_TKIP:
492*c1d255d3SCy Schubert 		ssid->pairwise_cipher = WPA_CIPHER_TKIP | WPA_CIPHER_CCMP;
49339beb93cSSam Leffler 		break;
49439beb93cSSam Leffler 	case WPS_ENCR_AES:
49539beb93cSSam Leffler 		ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4965b9c547cSRui Paulo 		if (wpa_s->drv_capa_known &&
4975b9c547cSRui Paulo 		    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
4985b9c547cSRui Paulo 			ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
4995b9c547cSRui Paulo 			ssid->group_cipher |= WPA_CIPHER_GCMP;
5005b9c547cSRui Paulo 		}
50185732ac8SCy Schubert 		if (wpa_s->drv_capa_known &&
50285732ac8SCy Schubert 		    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256)) {
50385732ac8SCy Schubert 			ssid->pairwise_cipher |= WPA_CIPHER_GCMP_256;
50485732ac8SCy Schubert 			ssid->group_cipher |= WPA_CIPHER_GCMP_256;
50585732ac8SCy Schubert 		}
50685732ac8SCy Schubert 		if (wpa_s->drv_capa_known &&
50785732ac8SCy Schubert 		    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256)) {
50885732ac8SCy Schubert 			ssid->pairwise_cipher |= WPA_CIPHER_CCMP_256;
50985732ac8SCy Schubert 			ssid->group_cipher |= WPA_CIPHER_CCMP_256;
51085732ac8SCy Schubert 		}
51139beb93cSSam Leffler 		break;
51239beb93cSSam Leffler 	}
51339beb93cSSam Leffler 
5143157ba21SRui Paulo 	switch (auth_type) {
51539beb93cSSam Leffler 	case WPS_AUTH_OPEN:
51639beb93cSSam Leffler 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
51739beb93cSSam Leffler 		ssid->key_mgmt = WPA_KEY_MGMT_NONE;
51839beb93cSSam Leffler 		ssid->proto = 0;
519f05cddf9SRui Paulo #ifdef CONFIG_WPS_REG_DISABLE_OPEN
520f05cddf9SRui Paulo 		if (registrar) {
521f05cddf9SRui Paulo 			wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
522f05cddf9SRui Paulo 				"id=%d - Credentials for an open "
523f05cddf9SRui Paulo 				"network disabled by default - use "
524f05cddf9SRui Paulo 				"'select_network %d' to enable",
525f05cddf9SRui Paulo 				ssid->id, ssid->id);
526f05cddf9SRui Paulo 			ssid->disabled = 1;
527f05cddf9SRui Paulo 		}
528f05cddf9SRui Paulo #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
52939beb93cSSam Leffler 		break;
53039beb93cSSam Leffler 	case WPS_AUTH_WPAPSK:
53139beb93cSSam Leffler 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
53239beb93cSSam Leffler 		ssid->key_mgmt = WPA_KEY_MGMT_PSK;
533*c1d255d3SCy Schubert 		ssid->proto = WPA_PROTO_WPA | WPA_PROTO_RSN;
53439beb93cSSam Leffler 		break;
53539beb93cSSam Leffler 	case WPS_AUTH_WPA2PSK:
53639beb93cSSam Leffler 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
53739beb93cSSam Leffler 		ssid->key_mgmt = WPA_KEY_MGMT_PSK;
538*c1d255d3SCy Schubert 		add_sae = wpa_s->conf->wps_cred_add_sae;
539*c1d255d3SCy Schubert #ifdef CONFIG_P2P
540*c1d255d3SCy Schubert 		if (ssid->p2p_group && is_p2p_6ghz_capable(wpa_s->global->p2p))
541*c1d255d3SCy Schubert 			add_sae = true;
542*c1d255d3SCy Schubert #endif /* CONFIG_P2P */
543*c1d255d3SCy Schubert 		if (add_sae && cred->key_len != 2 * PMK_LEN) {
544*c1d255d3SCy Schubert 			ssid->auth_alg = 0;
5454bc52338SCy Schubert 			ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
5464bc52338SCy Schubert 			ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
5474bc52338SCy Schubert 		}
54839beb93cSSam Leffler 		ssid->proto = WPA_PROTO_RSN;
54939beb93cSSam Leffler 		break;
55039beb93cSSam Leffler 	}
55139beb93cSSam Leffler 
5524bc52338SCy Schubert 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
55339beb93cSSam Leffler 		if (cred->key_len == 2 * PMK_LEN) {
55439beb93cSSam Leffler 			if (hexstr2bin((const char *) cred->key, ssid->psk,
55539beb93cSSam Leffler 				       PMK_LEN)) {
55639beb93cSSam Leffler 				wpa_printf(MSG_ERROR, "WPS: Invalid Network "
55739beb93cSSam Leffler 					   "Key");
55839beb93cSSam Leffler 				return -1;
55939beb93cSSam Leffler 			}
56039beb93cSSam Leffler 			ssid->psk_set = 1;
561f05cddf9SRui Paulo 			ssid->export_keys = 1;
56239beb93cSSam Leffler 		} else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
56339beb93cSSam Leffler 			os_free(ssid->passphrase);
56439beb93cSSam Leffler 			ssid->passphrase = os_malloc(cred->key_len + 1);
56539beb93cSSam Leffler 			if (ssid->passphrase == NULL)
56639beb93cSSam Leffler 				return -1;
56739beb93cSSam Leffler 			os_memcpy(ssid->passphrase, cred->key, cred->key_len);
56839beb93cSSam Leffler 			ssid->passphrase[cred->key_len] = '\0';
56939beb93cSSam Leffler 			wpa_config_update_psk(ssid);
570f05cddf9SRui Paulo 			ssid->export_keys = 1;
57139beb93cSSam Leffler 		} else {
57239beb93cSSam Leffler 			wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
57339beb93cSSam Leffler 				   "length %lu",
57439beb93cSSam Leffler 				   (unsigned long) cred->key_len);
57539beb93cSSam Leffler 			return -1;
57639beb93cSSam Leffler 		}
57739beb93cSSam Leffler 	}
578325151a3SRui Paulo 	ssid->priority = wpa_s->conf->wps_priority;
57939beb93cSSam Leffler 
5803157ba21SRui Paulo 	wpas_wps_security_workaround(wpa_s, ssid, cred);
5813157ba21SRui Paulo 
5825b9c547cSRui Paulo 	wpas_wps_remove_dup_network(wpa_s, ssid);
583f05cddf9SRui Paulo 
58439beb93cSSam Leffler #ifndef CONFIG_NO_CONFIG_WRITE
58539beb93cSSam Leffler 	if (wpa_s->conf->update_config &&
58639beb93cSSam Leffler 	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
58739beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
58839beb93cSSam Leffler 		return -1;
58939beb93cSSam Leffler 	}
59039beb93cSSam Leffler #endif /* CONFIG_NO_CONFIG_WRITE */
59139beb93cSSam Leffler 
592325151a3SRui Paulo 	if (ssid->priority)
593325151a3SRui Paulo 		wpa_config_update_prio_list(wpa_s->conf);
594325151a3SRui Paulo 
595f05cddf9SRui Paulo 	/*
596f05cddf9SRui Paulo 	 * Optimize the post-WPS scan based on the channel used during
597f05cddf9SRui Paulo 	 * the provisioning in case EAP-Failure is not received.
598f05cddf9SRui Paulo 	 */
599f05cddf9SRui Paulo 	wpa_s->after_wps = 5;
600f05cddf9SRui Paulo 	wpa_s->wps_freq = wpa_s->assoc_freq;
601f05cddf9SRui Paulo 
60239beb93cSSam Leffler 	return 0;
60339beb93cSSam Leffler }
60439beb93cSSam Leffler 
60539beb93cSSam Leffler 
wpa_supplicant_wps_event_m2d(struct wpa_supplicant * wpa_s,struct wps_event_m2d * m2d)60639beb93cSSam Leffler static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
60739beb93cSSam Leffler 					 struct wps_event_m2d *m2d)
60839beb93cSSam Leffler {
60939beb93cSSam Leffler 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
61039beb93cSSam Leffler 		"dev_password_id=%d config_error=%d",
61139beb93cSSam Leffler 		m2d->dev_password_id, m2d->config_error);
612e28a4053SRui Paulo 	wpas_notify_wps_event_m2d(wpa_s, m2d);
613f05cddf9SRui Paulo #ifdef CONFIG_P2P
614780fb4a2SCy Schubert 	if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
615780fb4a2SCy Schubert 		wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
616f05cddf9SRui Paulo 			"dev_password_id=%d config_error=%d",
617f05cddf9SRui Paulo 			m2d->dev_password_id, m2d->config_error);
618f05cddf9SRui Paulo 	}
619f05cddf9SRui Paulo 	if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
620f05cddf9SRui Paulo 		/*
621f05cddf9SRui Paulo 		 * Notify P2P from eloop timeout to avoid issues with the
622f05cddf9SRui Paulo 		 * interface getting removed while processing a message.
623f05cddf9SRui Paulo 		 */
6245b9c547cSRui Paulo 		eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
625f05cddf9SRui Paulo 				       NULL);
626f05cddf9SRui Paulo 	}
627f05cddf9SRui Paulo #endif /* CONFIG_P2P */
62839beb93cSSam Leffler }
62939beb93cSSam Leffler 
63039beb93cSSam Leffler 
wpas_wps_clear_timeout(void * eloop_ctx,void * timeout_ctx)6315b9c547cSRui Paulo static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
6325b9c547cSRui Paulo {
6335b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
6345b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
6355b9c547cSRui Paulo 	wpas_clear_wps(wpa_s);
6365b9c547cSRui Paulo }
6375b9c547cSRui Paulo 
638f05cddf9SRui Paulo 
wpa_supplicant_wps_event_fail(struct wpa_supplicant * wpa_s,struct wps_event_fail * fail)63939beb93cSSam Leffler static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
64039beb93cSSam Leffler 					  struct wps_event_fail *fail)
64139beb93cSSam Leffler {
642f05cddf9SRui Paulo 	if (fail->error_indication > 0 &&
643f05cddf9SRui Paulo 	    fail->error_indication < NUM_WPS_EI_VALUES) {
644f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO,
645f05cddf9SRui Paulo 			WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
646f05cddf9SRui Paulo 			fail->msg, fail->config_error, fail->error_indication,
6475b9c547cSRui Paulo 			wps_ei_str(fail->error_indication));
648780fb4a2SCy Schubert 		if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
649780fb4a2SCy Schubert 			wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
650f05cddf9SRui Paulo 				"msg=%d config_error=%d reason=%d (%s)",
651f05cddf9SRui Paulo 				fail->msg, fail->config_error,
652f05cddf9SRui Paulo 				fail->error_indication,
6535b9c547cSRui Paulo 				wps_ei_str(fail->error_indication));
654f05cddf9SRui Paulo 	} else {
655f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO,
656f05cddf9SRui Paulo 			WPS_EVENT_FAIL "msg=%d config_error=%d",
657f05cddf9SRui Paulo 			fail->msg, fail->config_error);
658780fb4a2SCy Schubert 		if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
659780fb4a2SCy Schubert 			wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
660f05cddf9SRui Paulo 				"msg=%d config_error=%d",
661f05cddf9SRui Paulo 				fail->msg, fail->config_error);
662f05cddf9SRui Paulo 	}
6635b9c547cSRui Paulo 
6645b9c547cSRui Paulo 	/*
6655b9c547cSRui Paulo 	 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
6665b9c547cSRui Paulo 	 */
6675b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
6685b9c547cSRui Paulo 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
6695b9c547cSRui Paulo 	eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
6705b9c547cSRui Paulo 
671e28a4053SRui Paulo 	wpas_notify_wps_event_fail(wpa_s, fail);
672f05cddf9SRui Paulo 	wpas_p2p_wps_failed(wpa_s, fail);
673f05cddf9SRui Paulo }
674f05cddf9SRui Paulo 
675f05cddf9SRui Paulo 
676f05cddf9SRui Paulo static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
677f05cddf9SRui Paulo 
wpas_wps_reenable_networks(struct wpa_supplicant * wpa_s)678f05cddf9SRui Paulo static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
679f05cddf9SRui Paulo {
680f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
681f05cddf9SRui Paulo 	int changed = 0;
682f05cddf9SRui Paulo 
683f05cddf9SRui Paulo 	eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
684f05cddf9SRui Paulo 
685f05cddf9SRui Paulo 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
686f05cddf9SRui Paulo 		if (ssid->disabled_for_connect && ssid->disabled) {
687f05cddf9SRui Paulo 			ssid->disabled_for_connect = 0;
688f05cddf9SRui Paulo 			ssid->disabled = 0;
689f05cddf9SRui Paulo 			wpas_notify_network_enabled_changed(wpa_s, ssid);
690f05cddf9SRui Paulo 			changed++;
691f05cddf9SRui Paulo 		}
692f05cddf9SRui Paulo 	}
693f05cddf9SRui Paulo 
694f05cddf9SRui Paulo 	if (changed) {
695f05cddf9SRui Paulo #ifndef CONFIG_NO_CONFIG_WRITE
696f05cddf9SRui Paulo 		if (wpa_s->conf->update_config &&
697f05cddf9SRui Paulo 		    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
698f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "WPS: Failed to update "
699f05cddf9SRui Paulo 				   "configuration");
700f05cddf9SRui Paulo 		}
701f05cddf9SRui Paulo #endif /* CONFIG_NO_CONFIG_WRITE */
702f05cddf9SRui Paulo 	}
703f05cddf9SRui Paulo }
704f05cddf9SRui Paulo 
705f05cddf9SRui Paulo 
wpas_wps_reenable_networks_cb(void * eloop_ctx,void * timeout_ctx)706f05cddf9SRui Paulo static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
707f05cddf9SRui Paulo {
708f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
709f05cddf9SRui Paulo 	/* Enable the networks disabled during wpas_wps_reassoc */
710f05cddf9SRui Paulo 	wpas_wps_reenable_networks(wpa_s);
71139beb93cSSam Leffler }
71239beb93cSSam Leffler 
71339beb93cSSam Leffler 
wpas_wps_reenable_networks_pending(struct wpa_supplicant * wpa_s)714780fb4a2SCy Schubert int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
715780fb4a2SCy Schubert {
716780fb4a2SCy Schubert 	return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
717780fb4a2SCy Schubert 					   wpa_s, NULL);
718780fb4a2SCy Schubert }
719780fb4a2SCy Schubert 
720780fb4a2SCy Schubert 
wpa_supplicant_wps_event_success(struct wpa_supplicant * wpa_s)72139beb93cSSam Leffler static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
72239beb93cSSam Leffler {
72339beb93cSSam Leffler 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
72439beb93cSSam Leffler 	wpa_s->wps_success = 1;
725e28a4053SRui Paulo 	wpas_notify_wps_event_success(wpa_s);
7265b9c547cSRui Paulo 	if (wpa_s->current_ssid)
7275b9c547cSRui Paulo 		wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
728*c1d255d3SCy Schubert 	wpa_s->consecutive_conn_failures = 0;
729f05cddf9SRui Paulo 
730f05cddf9SRui Paulo 	/*
731f05cddf9SRui Paulo 	 * Enable the networks disabled during wpas_wps_reassoc after 10
732f05cddf9SRui Paulo 	 * seconds. The 10 seconds timer is to allow the data connection to be
733f05cddf9SRui Paulo 	 * formed before allowing other networks to be selected.
734f05cddf9SRui Paulo 	 */
735f05cddf9SRui Paulo 	eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
736f05cddf9SRui Paulo 			       NULL);
737f05cddf9SRui Paulo 
738f05cddf9SRui Paulo 	wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
739e28a4053SRui Paulo }
740e28a4053SRui Paulo 
741e28a4053SRui Paulo 
wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant * wpa_s,struct wps_event_er_ap * ap)742e28a4053SRui Paulo static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
743e28a4053SRui Paulo 					       struct wps_event_er_ap *ap)
744e28a4053SRui Paulo {
745e28a4053SRui Paulo 	char uuid_str[100];
746e28a4053SRui Paulo 	char dev_type[WPS_DEV_TYPE_BUFSIZE];
747e28a4053SRui Paulo 
748e28a4053SRui Paulo 	uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
749e28a4053SRui Paulo 	if (ap->pri_dev_type)
750e28a4053SRui Paulo 		wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
751e28a4053SRui Paulo 				     sizeof(dev_type));
752e28a4053SRui Paulo 	else
753e28a4053SRui Paulo 		dev_type[0] = '\0';
754e28a4053SRui Paulo 
755e28a4053SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
756e28a4053SRui Paulo 		" pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
757e28a4053SRui Paulo 		uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
758e28a4053SRui Paulo 		ap->friendly_name ? ap->friendly_name : "",
759e28a4053SRui Paulo 		ap->manufacturer ? ap->manufacturer : "",
760e28a4053SRui Paulo 		ap->model_description ? ap->model_description : "",
761e28a4053SRui Paulo 		ap->model_name ? ap->model_name : "",
762e28a4053SRui Paulo 		ap->manufacturer_url ? ap->manufacturer_url : "",
763e28a4053SRui Paulo 		ap->model_url ? ap->model_url : "");
764e28a4053SRui Paulo }
765e28a4053SRui Paulo 
766e28a4053SRui Paulo 
wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant * wpa_s,struct wps_event_er_ap * ap)767e28a4053SRui Paulo static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
768e28a4053SRui Paulo 						  struct wps_event_er_ap *ap)
769e28a4053SRui Paulo {
770e28a4053SRui Paulo 	char uuid_str[100];
771e28a4053SRui Paulo 	uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
772e28a4053SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
773e28a4053SRui Paulo }
774e28a4053SRui Paulo 
775e28a4053SRui Paulo 
wpa_supplicant_wps_event_er_enrollee_add(struct wpa_supplicant * wpa_s,struct wps_event_er_enrollee * enrollee)776e28a4053SRui Paulo static void wpa_supplicant_wps_event_er_enrollee_add(
777e28a4053SRui Paulo 	struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
778e28a4053SRui Paulo {
779e28a4053SRui Paulo 	char uuid_str[100];
780e28a4053SRui Paulo 	char dev_type[WPS_DEV_TYPE_BUFSIZE];
781e28a4053SRui Paulo 
782e28a4053SRui Paulo 	uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
783e28a4053SRui Paulo 	if (enrollee->pri_dev_type)
784e28a4053SRui Paulo 		wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
785e28a4053SRui Paulo 				     sizeof(dev_type));
786e28a4053SRui Paulo 	else
787e28a4053SRui Paulo 		dev_type[0] = '\0';
788e28a4053SRui Paulo 
789e28a4053SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
790e28a4053SRui Paulo 		" M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
791e28a4053SRui Paulo 		"|%s|%s|%s|%s|%s|",
792e28a4053SRui Paulo 		uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
793e28a4053SRui Paulo 		enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
794e28a4053SRui Paulo 		enrollee->dev_name ? enrollee->dev_name : "",
795e28a4053SRui Paulo 		enrollee->manufacturer ? enrollee->manufacturer : "",
796e28a4053SRui Paulo 		enrollee->model_name ? enrollee->model_name : "",
797e28a4053SRui Paulo 		enrollee->model_number ? enrollee->model_number : "",
798e28a4053SRui Paulo 		enrollee->serial_number ? enrollee->serial_number : "");
799e28a4053SRui Paulo }
800e28a4053SRui Paulo 
801e28a4053SRui Paulo 
wpa_supplicant_wps_event_er_enrollee_remove(struct wpa_supplicant * wpa_s,struct wps_event_er_enrollee * enrollee)802e28a4053SRui Paulo static void wpa_supplicant_wps_event_er_enrollee_remove(
803e28a4053SRui Paulo 	struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
804e28a4053SRui Paulo {
805e28a4053SRui Paulo 	char uuid_str[100];
806e28a4053SRui Paulo 	uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
807e28a4053SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
808e28a4053SRui Paulo 		uuid_str, MAC2STR(enrollee->mac_addr));
80939beb93cSSam Leffler }
81039beb93cSSam Leffler 
81139beb93cSSam Leffler 
wpa_supplicant_wps_event_er_ap_settings(struct wpa_supplicant * wpa_s,struct wps_event_er_ap_settings * ap_settings)812f05cddf9SRui Paulo static void wpa_supplicant_wps_event_er_ap_settings(
813f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s,
814f05cddf9SRui Paulo 	struct wps_event_er_ap_settings *ap_settings)
815f05cddf9SRui Paulo {
816f05cddf9SRui Paulo 	char uuid_str[100];
817f05cddf9SRui Paulo 	char key_str[65];
818f05cddf9SRui Paulo 	const struct wps_credential *cred = ap_settings->cred;
819f05cddf9SRui Paulo 
820f05cddf9SRui Paulo 	key_str[0] = '\0';
821f05cddf9SRui Paulo 	if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
822f05cddf9SRui Paulo 		if (cred->key_len >= 8 && cred->key_len <= 64) {
823f05cddf9SRui Paulo 			os_memcpy(key_str, cred->key, cred->key_len);
824f05cddf9SRui Paulo 			key_str[cred->key_len] = '\0';
825f05cddf9SRui Paulo 		}
826f05cddf9SRui Paulo 	}
827f05cddf9SRui Paulo 
828f05cddf9SRui Paulo 	uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
829f05cddf9SRui Paulo 	/* Use wpa_msg_ctrl to avoid showing the key in debug log */
830f05cddf9SRui Paulo 	wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
831f05cddf9SRui Paulo 		     "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
832f05cddf9SRui Paulo 		     "key=%s",
833f05cddf9SRui Paulo 		     uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
834f05cddf9SRui Paulo 		     cred->auth_type, cred->encr_type, key_str);
835f05cddf9SRui Paulo }
836f05cddf9SRui Paulo 
837f05cddf9SRui Paulo 
wpa_supplicant_wps_event_er_set_sel_reg(struct wpa_supplicant * wpa_s,struct wps_event_er_set_selected_registrar * ev)838f05cddf9SRui Paulo static void wpa_supplicant_wps_event_er_set_sel_reg(
839f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s,
840f05cddf9SRui Paulo 	struct wps_event_er_set_selected_registrar *ev)
841f05cddf9SRui Paulo {
842f05cddf9SRui Paulo 	char uuid_str[100];
843f05cddf9SRui Paulo 
844f05cddf9SRui Paulo 	uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
845f05cddf9SRui Paulo 	switch (ev->state) {
846f05cddf9SRui Paulo 	case WPS_ER_SET_SEL_REG_START:
847f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
848f05cddf9SRui Paulo 			"uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
849f05cddf9SRui Paulo 			"sel_reg_config_methods=0x%x",
850f05cddf9SRui Paulo 			uuid_str, ev->sel_reg, ev->dev_passwd_id,
851f05cddf9SRui Paulo 			ev->sel_reg_config_methods);
852f05cddf9SRui Paulo 		break;
853f05cddf9SRui Paulo 	case WPS_ER_SET_SEL_REG_DONE:
854f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
855f05cddf9SRui Paulo 			"uuid=%s state=DONE", uuid_str);
856f05cddf9SRui Paulo 		break;
857f05cddf9SRui Paulo 	case WPS_ER_SET_SEL_REG_FAILED:
858f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
859f05cddf9SRui Paulo 			"uuid=%s state=FAILED", uuid_str);
860f05cddf9SRui Paulo 		break;
861f05cddf9SRui Paulo 	}
862f05cddf9SRui Paulo }
863f05cddf9SRui Paulo 
864f05cddf9SRui Paulo 
wpa_supplicant_wps_event(void * ctx,enum wps_event event,union wps_event_data * data)86539beb93cSSam Leffler static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
86639beb93cSSam Leffler 				     union wps_event_data *data)
86739beb93cSSam Leffler {
86839beb93cSSam Leffler 	struct wpa_supplicant *wpa_s = ctx;
86939beb93cSSam Leffler 	switch (event) {
87039beb93cSSam Leffler 	case WPS_EV_M2D:
87139beb93cSSam Leffler 		wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
87239beb93cSSam Leffler 		break;
87339beb93cSSam Leffler 	case WPS_EV_FAIL:
87439beb93cSSam Leffler 		wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
87539beb93cSSam Leffler 		break;
87639beb93cSSam Leffler 	case WPS_EV_SUCCESS:
87739beb93cSSam Leffler 		wpa_supplicant_wps_event_success(wpa_s);
87839beb93cSSam Leffler 		break;
87939beb93cSSam Leffler 	case WPS_EV_PWD_AUTH_FAIL:
880f05cddf9SRui Paulo #ifdef CONFIG_AP
881f05cddf9SRui Paulo 		if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
882f05cddf9SRui Paulo 			wpa_supplicant_ap_pwd_auth_fail(wpa_s);
883f05cddf9SRui Paulo #endif /* CONFIG_AP */
88439beb93cSSam Leffler 		break;
8853157ba21SRui Paulo 	case WPS_EV_PBC_OVERLAP:
8863157ba21SRui Paulo 		break;
8873157ba21SRui Paulo 	case WPS_EV_PBC_TIMEOUT:
8883157ba21SRui Paulo 		break;
8895b9c547cSRui Paulo 	case WPS_EV_PBC_ACTIVE:
8905b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
8915b9c547cSRui Paulo 		break;
8925b9c547cSRui Paulo 	case WPS_EV_PBC_DISABLE:
8935b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
8945b9c547cSRui Paulo 		break;
895e28a4053SRui Paulo 	case WPS_EV_ER_AP_ADD:
896e28a4053SRui Paulo 		wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
897e28a4053SRui Paulo 		break;
898e28a4053SRui Paulo 	case WPS_EV_ER_AP_REMOVE:
899e28a4053SRui Paulo 		wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
900e28a4053SRui Paulo 		break;
901e28a4053SRui Paulo 	case WPS_EV_ER_ENROLLEE_ADD:
902e28a4053SRui Paulo 		wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
903e28a4053SRui Paulo 							 &data->enrollee);
904e28a4053SRui Paulo 		break;
905e28a4053SRui Paulo 	case WPS_EV_ER_ENROLLEE_REMOVE:
906e28a4053SRui Paulo 		wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
907e28a4053SRui Paulo 							    &data->enrollee);
908e28a4053SRui Paulo 		break;
909f05cddf9SRui Paulo 	case WPS_EV_ER_AP_SETTINGS:
910f05cddf9SRui Paulo 		wpa_supplicant_wps_event_er_ap_settings(wpa_s,
911f05cddf9SRui Paulo 							&data->ap_settings);
912f05cddf9SRui Paulo 		break;
913f05cddf9SRui Paulo 	case WPS_EV_ER_SET_SELECTED_REGISTRAR:
914f05cddf9SRui Paulo 		wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
915f05cddf9SRui Paulo 							&data->set_sel_reg);
916f05cddf9SRui Paulo 		break;
917f05cddf9SRui Paulo 	case WPS_EV_AP_PIN_SUCCESS:
918f05cddf9SRui Paulo 		break;
91939beb93cSSam Leffler 	}
92039beb93cSSam Leffler }
92139beb93cSSam Leffler 
92239beb93cSSam Leffler 
wpa_supplicant_wps_rf_band(void * ctx)9235b9c547cSRui Paulo static int wpa_supplicant_wps_rf_band(void *ctx)
9245b9c547cSRui Paulo {
9255b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
9265b9c547cSRui Paulo 
9275b9c547cSRui Paulo 	if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
9285b9c547cSRui Paulo 		return 0;
9295b9c547cSRui Paulo 
930325151a3SRui Paulo 	return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
931325151a3SRui Paulo 		(wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
9325b9c547cSRui Paulo }
9335b9c547cSRui Paulo 
9345b9c547cSRui Paulo 
wpas_wps_get_req_type(struct wpa_ssid * ssid)93539beb93cSSam Leffler enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
93639beb93cSSam Leffler {
93739beb93cSSam Leffler 	if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
93839beb93cSSam Leffler 	    eap_is_wps_pin_enrollee(&ssid->eap))
93939beb93cSSam Leffler 		return WPS_REQ_ENROLLEE;
94039beb93cSSam Leffler 	else
94139beb93cSSam Leffler 		return WPS_REQ_REGISTRAR;
94239beb93cSSam Leffler }
94339beb93cSSam Leffler 
94439beb93cSSam Leffler 
wpas_clear_wps(struct wpa_supplicant * wpa_s)94539beb93cSSam Leffler static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
94639beb93cSSam Leffler {
94739beb93cSSam Leffler 	int id;
948f05cddf9SRui Paulo 	struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
949f05cddf9SRui Paulo 
9505b9c547cSRui Paulo 	wpa_s->after_wps = 0;
9515b9c547cSRui Paulo 	wpa_s->known_wps_freq = 0;
9525b9c547cSRui Paulo 
953f05cddf9SRui Paulo 	prev_current = wpa_s->current_ssid;
954f05cddf9SRui Paulo 
955f05cddf9SRui Paulo 	/* Enable the networks disabled during wpas_wps_reassoc */
956f05cddf9SRui Paulo 	wpas_wps_reenable_networks(wpa_s);
95739beb93cSSam Leffler 
95839beb93cSSam Leffler 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
9595b9c547cSRui Paulo 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
96039beb93cSSam Leffler 
96139beb93cSSam Leffler 	/* Remove any existing WPS network from configuration */
96239beb93cSSam Leffler 	ssid = wpa_s->conf->ssid;
96339beb93cSSam Leffler 	while (ssid) {
96439beb93cSSam Leffler 		if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
965e28a4053SRui Paulo 			if (ssid == wpa_s->current_ssid) {
9665b9c547cSRui Paulo 				wpa_s->own_disconnect_req = 1;
9675b9c547cSRui Paulo 				wpa_supplicant_deauthenticate(
9685b9c547cSRui Paulo 					wpa_s, WLAN_REASON_DEAUTH_LEAVING);
969e28a4053SRui Paulo 			}
97039beb93cSSam Leffler 			id = ssid->id;
971e28a4053SRui Paulo 			remove_ssid = ssid;
97239beb93cSSam Leffler 		} else
97339beb93cSSam Leffler 			id = -1;
97439beb93cSSam Leffler 		ssid = ssid->next;
975e28a4053SRui Paulo 		if (id >= 0) {
976f05cddf9SRui Paulo 			if (prev_current == remove_ssid) {
977f05cddf9SRui Paulo 				wpa_sm_set_config(wpa_s->wpa, NULL);
978f05cddf9SRui Paulo 				eapol_sm_notify_config(wpa_s->eapol, NULL,
979f05cddf9SRui Paulo 						       NULL);
980f05cddf9SRui Paulo 			}
981e28a4053SRui Paulo 			wpas_notify_network_removed(wpa_s, remove_ssid);
98239beb93cSSam Leffler 			wpa_config_remove_network(wpa_s->conf, id);
98339beb93cSSam Leffler 		}
98439beb93cSSam Leffler 	}
985f05cddf9SRui Paulo 
986f05cddf9SRui Paulo 	wpas_wps_clear_ap_info(wpa_s);
987e28a4053SRui Paulo }
98839beb93cSSam Leffler 
98939beb93cSSam Leffler 
wpas_wps_timeout(void * eloop_ctx,void * timeout_ctx)99039beb93cSSam Leffler static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
99139beb93cSSam Leffler {
99239beb93cSSam Leffler 	struct wpa_supplicant *wpa_s = eloop_ctx;
993325151a3SRui Paulo 	union wps_event_data data;
994325151a3SRui Paulo 
995f05cddf9SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
99639beb93cSSam Leffler 		"out");
997325151a3SRui Paulo 	os_memset(&data, 0, sizeof(data));
998325151a3SRui Paulo 	data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
999325151a3SRui Paulo 	data.fail.error_indication = WPS_EI_NO_ERROR;
1000325151a3SRui Paulo 	/*
1001325151a3SRui Paulo 	 * Call wpas_notify_wps_event_fail() directly instead of through
1002325151a3SRui Paulo 	 * wpa_supplicant_wps_event() which would end up registering unnecessary
1003325151a3SRui Paulo 	 * timeouts (those are only for the case where the failure happens
1004325151a3SRui Paulo 	 * during an EAP-WSC exchange).
1005325151a3SRui Paulo 	 */
1006325151a3SRui Paulo 	wpas_notify_wps_event_fail(wpa_s, &data.fail);
100739beb93cSSam Leffler 	wpas_clear_wps(wpa_s);
100839beb93cSSam Leffler }
100939beb93cSSam Leffler 
101039beb93cSSam Leffler 
wpas_wps_add_network(struct wpa_supplicant * wpa_s,int registrar,const u8 * dev_addr,const u8 * bssid)101139beb93cSSam Leffler static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
10125b9c547cSRui Paulo 					      int registrar, const u8 *dev_addr,
10135b9c547cSRui Paulo 					      const u8 *bssid)
101439beb93cSSam Leffler {
101539beb93cSSam Leffler 	struct wpa_ssid *ssid;
101639beb93cSSam Leffler 
101739beb93cSSam Leffler 	ssid = wpa_config_add_network(wpa_s->conf);
101839beb93cSSam Leffler 	if (ssid == NULL)
101939beb93cSSam Leffler 		return NULL;
1020e28a4053SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
102139beb93cSSam Leffler 	wpa_config_set_network_defaults(ssid);
1022f05cddf9SRui Paulo 	ssid->temporary = 1;
102339beb93cSSam Leffler 	if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
102439beb93cSSam Leffler 	    wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
102539beb93cSSam Leffler 	    wpa_config_set(ssid, "identity", registrar ?
102639beb93cSSam Leffler 			   "\"" WSC_ID_REGISTRAR "\"" :
102739beb93cSSam Leffler 			   "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1028e28a4053SRui Paulo 		wpas_notify_network_removed(wpa_s, ssid);
102939beb93cSSam Leffler 		wpa_config_remove_network(wpa_s->conf, ssid->id);
103039beb93cSSam Leffler 		return NULL;
103139beb93cSSam Leffler 	}
103239beb93cSSam Leffler 
10335b9c547cSRui Paulo #ifdef CONFIG_P2P
10345b9c547cSRui Paulo 	if (dev_addr)
10355b9c547cSRui Paulo 		os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
10365b9c547cSRui Paulo #endif /* CONFIG_P2P */
10375b9c547cSRui Paulo 
103839beb93cSSam Leffler 	if (bssid) {
1039f05cddf9SRui Paulo #ifndef CONFIG_P2P
1040e28a4053SRui Paulo 		struct wpa_bss *bss;
10413157ba21SRui Paulo 		int count = 0;
1042f05cddf9SRui Paulo #endif /* CONFIG_P2P */
104339beb93cSSam Leffler 
104439beb93cSSam Leffler 		os_memcpy(ssid->bssid, bssid, ETH_ALEN);
104539beb93cSSam Leffler 		ssid->bssid_set = 1;
104639beb93cSSam Leffler 
1047f05cddf9SRui Paulo 		/*
1048f05cddf9SRui Paulo 		 * Note: With P2P, the SSID may change at the time the WPS
1049f05cddf9SRui Paulo 		 * provisioning is started, so better not filter the AP based
1050f05cddf9SRui Paulo 		 * on the current SSID in the scan results.
1051f05cddf9SRui Paulo 		 */
1052f05cddf9SRui Paulo #ifndef CONFIG_P2P
1053e28a4053SRui Paulo 		dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1054e28a4053SRui Paulo 			if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
105539beb93cSSam Leffler 				continue;
105639beb93cSSam Leffler 
105739beb93cSSam Leffler 			os_free(ssid->ssid);
105885732ac8SCy Schubert 			ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
105939beb93cSSam Leffler 			if (ssid->ssid == NULL)
106039beb93cSSam Leffler 				break;
1061e28a4053SRui Paulo 			ssid->ssid_len = bss->ssid_len;
10623157ba21SRui Paulo 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
10633157ba21SRui Paulo 					  "scan results",
10643157ba21SRui Paulo 					  ssid->ssid, ssid->ssid_len);
10653157ba21SRui Paulo 			count++;
10663157ba21SRui Paulo 		}
10673157ba21SRui Paulo 
10683157ba21SRui Paulo 		if (count > 1) {
10693157ba21SRui Paulo 			wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
10703157ba21SRui Paulo 				   "for the AP; use wildcard");
10713157ba21SRui Paulo 			os_free(ssid->ssid);
10723157ba21SRui Paulo 			ssid->ssid = NULL;
10733157ba21SRui Paulo 			ssid->ssid_len = 0;
107439beb93cSSam Leffler 		}
1075f05cddf9SRui Paulo #endif /* CONFIG_P2P */
107639beb93cSSam Leffler 	}
107739beb93cSSam Leffler 
107839beb93cSSam Leffler 	return ssid;
107939beb93cSSam Leffler }
108039beb93cSSam Leffler 
108139beb93cSSam Leffler 
wpas_wps_temp_disable(struct wpa_supplicant * wpa_s,struct wpa_ssid * selected)10825b9c547cSRui Paulo static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
10835b9c547cSRui Paulo 				  struct wpa_ssid *selected)
108439beb93cSSam Leffler {
108539beb93cSSam Leffler 	struct wpa_ssid *ssid;
1086f05cddf9SRui Paulo 
10875b9c547cSRui Paulo 	if (wpa_s->current_ssid) {
10885b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
1089f05cddf9SRui Paulo 		wpa_supplicant_deauthenticate(
1090f05cddf9SRui Paulo 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
10915b9c547cSRui Paulo 	}
109239beb93cSSam Leffler 
109339beb93cSSam Leffler 	/* Mark all other networks disabled and trigger reassociation */
109439beb93cSSam Leffler 	ssid = wpa_s->conf->ssid;
109539beb93cSSam Leffler 	while (ssid) {
1096e28a4053SRui Paulo 		int was_disabled = ssid->disabled;
1097f05cddf9SRui Paulo 		ssid->disabled_for_connect = 0;
1098f05cddf9SRui Paulo 		/*
1099f05cddf9SRui Paulo 		 * In case the network object corresponds to a persistent group
1100f05cddf9SRui Paulo 		 * then do not send out network disabled signal. In addition,
1101f05cddf9SRui Paulo 		 * do not change disabled status of persistent network objects
1102f05cddf9SRui Paulo 		 * from 2 to 1 should we connect to another network.
1103f05cddf9SRui Paulo 		 */
1104f05cddf9SRui Paulo 		if (was_disabled != 2) {
110539beb93cSSam Leffler 			ssid->disabled = ssid != selected;
1106f05cddf9SRui Paulo 			if (was_disabled != ssid->disabled) {
1107f05cddf9SRui Paulo 				if (ssid->disabled)
1108f05cddf9SRui Paulo 					ssid->disabled_for_connect = 1;
1109f05cddf9SRui Paulo 				wpas_notify_network_enabled_changed(wpa_s,
1110f05cddf9SRui Paulo 								    ssid);
1111f05cddf9SRui Paulo 			}
1112f05cddf9SRui Paulo 		}
111339beb93cSSam Leffler 		ssid = ssid->next;
111439beb93cSSam Leffler 	}
11155b9c547cSRui Paulo }
11165b9c547cSRui Paulo 
11175b9c547cSRui Paulo 
wpas_wps_reassoc(struct wpa_supplicant * wpa_s,struct wpa_ssid * selected,const u8 * bssid,int freq)11185b9c547cSRui Paulo static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
11195b9c547cSRui Paulo 			     struct wpa_ssid *selected, const u8 *bssid,
11205b9c547cSRui Paulo 			     int freq)
11215b9c547cSRui Paulo {
11225b9c547cSRui Paulo 	struct wpa_bss *bss;
11235b9c547cSRui Paulo 
11245b9c547cSRui Paulo 	wpa_s->wps_run++;
11255b9c547cSRui Paulo 	if (wpa_s->wps_run == 0)
11265b9c547cSRui Paulo 		wpa_s->wps_run++;
11275b9c547cSRui Paulo 	wpa_s->after_wps = 0;
11285b9c547cSRui Paulo 	wpa_s->known_wps_freq = 0;
11295b9c547cSRui Paulo 	if (freq) {
11305b9c547cSRui Paulo 		wpa_s->after_wps = 5;
11315b9c547cSRui Paulo 		wpa_s->wps_freq = freq;
11325b9c547cSRui Paulo 	} else if (bssid) {
11335b9c547cSRui Paulo 		bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
11345b9c547cSRui Paulo 		if (bss && bss->freq > 0) {
11355b9c547cSRui Paulo 			wpa_s->known_wps_freq = 1;
11365b9c547cSRui Paulo 			wpa_s->wps_freq = bss->freq;
11375b9c547cSRui Paulo 		}
11385b9c547cSRui Paulo 	}
11395b9c547cSRui Paulo 
11405b9c547cSRui Paulo 	wpas_wps_temp_disable(wpa_s, selected);
11415b9c547cSRui Paulo 
114239beb93cSSam Leffler 	wpa_s->disconnected = 0;
114339beb93cSSam Leffler 	wpa_s->reassociate = 1;
114439beb93cSSam Leffler 	wpa_s->scan_runs = 0;
1145f05cddf9SRui Paulo 	wpa_s->normal_scans = 0;
114639beb93cSSam Leffler 	wpa_s->wps_success = 0;
1147*c1d255d3SCy Schubert 	wpa_s->bssid_ignore_cleared = false;
11485b9c547cSRui Paulo 
11495b9c547cSRui Paulo 	wpa_supplicant_cancel_sched_scan(wpa_s);
115039beb93cSSam Leffler 	wpa_supplicant_req_scan(wpa_s, 0, 0);
115139beb93cSSam Leffler }
115239beb93cSSam Leffler 
115339beb93cSSam Leffler 
wpas_wps_start_pbc(struct wpa_supplicant * wpa_s,const u8 * bssid,int p2p_group,int multi_ap_backhaul_sta)1154f05cddf9SRui Paulo int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
11554bc52338SCy Schubert 		       int p2p_group, int multi_ap_backhaul_sta)
115639beb93cSSam Leffler {
115739beb93cSSam Leffler 	struct wpa_ssid *ssid;
11584bc52338SCy Schubert 	char phase1[32];
11595b9c547cSRui Paulo 
11605b9c547cSRui Paulo #ifdef CONFIG_AP
11615b9c547cSRui Paulo 	if (wpa_s->ap_iface) {
11625b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
11635b9c547cSRui Paulo 			   "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
11645b9c547cSRui Paulo 		return -1;
11655b9c547cSRui Paulo 	}
11665b9c547cSRui Paulo #endif /* CONFIG_AP */
116739beb93cSSam Leffler 	wpas_clear_wps(wpa_s);
11685b9c547cSRui Paulo 	ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
116939beb93cSSam Leffler 	if (ssid == NULL)
117039beb93cSSam Leffler 		return -1;
1171f05cddf9SRui Paulo 	ssid->temporary = 1;
1172f05cddf9SRui Paulo 	ssid->p2p_group = p2p_group;
1173780fb4a2SCy Schubert 	/*
1174780fb4a2SCy Schubert 	 * When starting a regular WPS process (not P2P group formation)
1175780fb4a2SCy Schubert 	 * the registrar/final station can be either AP or PCP
1176780fb4a2SCy Schubert 	 * so use a "don't care" value for the pbss flag.
1177780fb4a2SCy Schubert 	 */
1178780fb4a2SCy Schubert 	if (!p2p_group)
1179780fb4a2SCy Schubert 		ssid->pbss = 2;
1180f05cddf9SRui Paulo #ifdef CONFIG_P2P
1181f05cddf9SRui Paulo 	if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1182f05cddf9SRui Paulo 		ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1183f05cddf9SRui Paulo 		if (ssid->ssid) {
1184f05cddf9SRui Paulo 			ssid->ssid_len = wpa_s->go_params->ssid_len;
1185f05cddf9SRui Paulo 			os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1186f05cddf9SRui Paulo 				  ssid->ssid_len);
1187780fb4a2SCy Schubert 			if (wpa_s->go_params->freq > 56160) {
1188780fb4a2SCy Schubert 				/* P2P in 60 GHz uses PBSS */
1189780fb4a2SCy Schubert 				ssid->pbss = 1;
1190780fb4a2SCy Schubert 			}
1191*c1d255d3SCy Schubert 			if (wpa_s->go_params->edmg &&
1192*c1d255d3SCy Schubert 			    wpas_p2p_try_edmg_channel(wpa_s,
1193*c1d255d3SCy Schubert 						      wpa_s->go_params) == 0)
1194*c1d255d3SCy Schubert 				ssid->enable_edmg = 1;
1195*c1d255d3SCy Schubert 
1196f05cddf9SRui Paulo 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1197f05cddf9SRui Paulo 					  "SSID", ssid->ssid, ssid->ssid_len);
1198f05cddf9SRui Paulo 		}
1199f05cddf9SRui Paulo 	}
1200f05cddf9SRui Paulo #endif /* CONFIG_P2P */
12014bc52338SCy Schubert 	os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
12024bc52338SCy Schubert 		    multi_ap_backhaul_sta ? " multi_ap=1" : "");
12034bc52338SCy Schubert 	if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
12045b9c547cSRui Paulo 		return -1;
1205f05cddf9SRui Paulo 	if (wpa_s->wps_fragment_size)
1206f05cddf9SRui Paulo 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
12074bc52338SCy Schubert 	if (multi_ap_backhaul_sta)
12084bc52338SCy Schubert 		ssid->multi_ap_backhaul_sta = 1;
120985732ac8SCy Schubert 	wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
121039beb93cSSam Leffler 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
121139beb93cSSam Leffler 			       wpa_s, NULL);
12125b9c547cSRui Paulo 	wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
121339beb93cSSam Leffler 	return 0;
121439beb93cSSam Leffler }
121539beb93cSSam Leffler 
121639beb93cSSam Leffler 
wpas_wps_start_dev_pw(struct wpa_supplicant * wpa_s,const u8 * dev_addr,const u8 * bssid,const char * pin,int p2p_group,u16 dev_pw_id,const u8 * peer_pubkey_hash,const u8 * ssid_val,size_t ssid_len,int freq)12175b9c547cSRui Paulo static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
12185b9c547cSRui Paulo 				 const u8 *dev_addr, const u8 *bssid,
12195b9c547cSRui Paulo 				 const char *pin, int p2p_group, u16 dev_pw_id,
12205b9c547cSRui Paulo 				 const u8 *peer_pubkey_hash,
12215b9c547cSRui Paulo 				 const u8 *ssid_val, size_t ssid_len, int freq)
122239beb93cSSam Leffler {
122339beb93cSSam Leffler 	struct wpa_ssid *ssid;
12245b9c547cSRui Paulo 	char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
122539beb93cSSam Leffler 	unsigned int rpin = 0;
12265b9c547cSRui Paulo 	char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
122739beb93cSSam Leffler 
12285b9c547cSRui Paulo #ifdef CONFIG_AP
12295b9c547cSRui Paulo 	if (wpa_s->ap_iface) {
12305b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
12315b9c547cSRui Paulo 			   "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
123239beb93cSSam Leffler 		return -1;
12335b9c547cSRui Paulo 	}
12345b9c547cSRui Paulo #endif /* CONFIG_AP */
12355b9c547cSRui Paulo 	wpas_clear_wps(wpa_s);
12365b9c547cSRui Paulo 	if (bssid && is_zero_ether_addr(bssid))
12375b9c547cSRui Paulo 		bssid = NULL;
12385b9c547cSRui Paulo 	ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
12395b9c547cSRui Paulo 	if (ssid == NULL) {
12405b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Could not add network");
12415b9c547cSRui Paulo 		return -1;
12425b9c547cSRui Paulo 	}
1243f05cddf9SRui Paulo 	ssid->temporary = 1;
1244f05cddf9SRui Paulo 	ssid->p2p_group = p2p_group;
1245780fb4a2SCy Schubert 	/*
1246780fb4a2SCy Schubert 	 * When starting a regular WPS process (not P2P group formation)
1247780fb4a2SCy Schubert 	 * the registrar/final station can be either AP or PCP
1248780fb4a2SCy Schubert 	 * so use a "don't care" value for the pbss flag.
1249780fb4a2SCy Schubert 	 */
1250780fb4a2SCy Schubert 	if (!p2p_group)
1251780fb4a2SCy Schubert 		ssid->pbss = 2;
12525b9c547cSRui Paulo 	if (ssid_val) {
12535b9c547cSRui Paulo 		ssid->ssid = os_malloc(ssid_len);
12545b9c547cSRui Paulo 		if (ssid->ssid) {
12555b9c547cSRui Paulo 			os_memcpy(ssid->ssid, ssid_val, ssid_len);
12565b9c547cSRui Paulo 			ssid->ssid_len = ssid_len;
12575b9c547cSRui Paulo 		}
12585b9c547cSRui Paulo 	}
12595b9c547cSRui Paulo 	if (peer_pubkey_hash) {
12605b9c547cSRui Paulo 		os_memcpy(hash, " pkhash=", 8);
12615b9c547cSRui Paulo 		wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
12625b9c547cSRui Paulo 					   peer_pubkey_hash,
12635b9c547cSRui Paulo 					   WPS_OOB_PUBKEY_HASH_LEN);
12645b9c547cSRui Paulo 	} else {
12655b9c547cSRui Paulo 		hash[0] = '\0';
12665b9c547cSRui Paulo 	}
1267f05cddf9SRui Paulo #ifdef CONFIG_P2P
1268f05cddf9SRui Paulo 	if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1269325151a3SRui Paulo 		os_free(ssid->ssid);
1270f05cddf9SRui Paulo 		ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1271f05cddf9SRui Paulo 		if (ssid->ssid) {
1272f05cddf9SRui Paulo 			ssid->ssid_len = wpa_s->go_params->ssid_len;
1273f05cddf9SRui Paulo 			os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1274f05cddf9SRui Paulo 				  ssid->ssid_len);
1275780fb4a2SCy Schubert 			if (wpa_s->go_params->freq > 56160) {
1276780fb4a2SCy Schubert 				/* P2P in 60 GHz uses PBSS */
1277780fb4a2SCy Schubert 				ssid->pbss = 1;
1278780fb4a2SCy Schubert 			}
1279*c1d255d3SCy Schubert 			if (wpa_s->go_params->edmg &&
1280*c1d255d3SCy Schubert 			    wpas_p2p_try_edmg_channel(wpa_s,
1281*c1d255d3SCy Schubert 						      wpa_s->go_params) == 0)
1282*c1d255d3SCy Schubert 				ssid->enable_edmg = 1;
1283*c1d255d3SCy Schubert 
1284f05cddf9SRui Paulo 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1285f05cddf9SRui Paulo 					  "SSID", ssid->ssid, ssid->ssid_len);
1286f05cddf9SRui Paulo 		}
1287f05cddf9SRui Paulo 	}
1288f05cddf9SRui Paulo #endif /* CONFIG_P2P */
128939beb93cSSam Leffler 	if (pin)
12905b9c547cSRui Paulo 		os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
12915b9c547cSRui Paulo 			    pin, dev_pw_id, hash);
12925b9c547cSRui Paulo 	else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
12935b9c547cSRui Paulo 		os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
12945b9c547cSRui Paulo 			    dev_pw_id, hash);
12955b9c547cSRui Paulo 	} else {
1296780fb4a2SCy Schubert 		if (wps_generate_pin(&rpin) < 0) {
1297780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1298780fb4a2SCy Schubert 			return -1;
1299780fb4a2SCy Schubert 		}
13005b9c547cSRui Paulo 		os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
13015b9c547cSRui Paulo 			    rpin, dev_pw_id, hash);
130239beb93cSSam Leffler 	}
13035b9c547cSRui Paulo 	if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
13045b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
13055b9c547cSRui Paulo 		return -1;
13065b9c547cSRui Paulo 	}
1307*c1d255d3SCy Schubert 
1308*c1d255d3SCy Schubert 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER)
1309*c1d255d3SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_PIN_ACTIVE);
1310*c1d255d3SCy Schubert 
1311f05cddf9SRui Paulo 	if (wpa_s->wps_fragment_size)
1312f05cddf9SRui Paulo 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
131339beb93cSSam Leffler 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
131439beb93cSSam Leffler 			       wpa_s, NULL);
1315f05cddf9SRui Paulo 	wpa_s->wps_ap_iter = 1;
13165b9c547cSRui Paulo 	wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
131739beb93cSSam Leffler 	return rpin;
131839beb93cSSam Leffler }
131939beb93cSSam Leffler 
132039beb93cSSam Leffler 
wpas_wps_start_pin(struct wpa_supplicant * wpa_s,const u8 * bssid,const char * pin,int p2p_group,u16 dev_pw_id)13215b9c547cSRui Paulo int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
13225b9c547cSRui Paulo 		       const char *pin, int p2p_group, u16 dev_pw_id)
13235b9c547cSRui Paulo {
1324325151a3SRui Paulo 	os_get_reltime(&wpa_s->wps_pin_start_time);
13255b9c547cSRui Paulo 	return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
13265b9c547cSRui Paulo 				     dev_pw_id, NULL, NULL, 0, 0);
13275b9c547cSRui Paulo }
13285b9c547cSRui Paulo 
13295b9c547cSRui Paulo 
wpas_wps_pbc_overlap(struct wpa_supplicant * wpa_s)1330325151a3SRui Paulo void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1331325151a3SRui Paulo {
1332325151a3SRui Paulo 	union wps_event_data data;
1333325151a3SRui Paulo 
1334325151a3SRui Paulo 	os_memset(&data, 0, sizeof(data));
1335325151a3SRui Paulo 	data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1336325151a3SRui Paulo 	data.fail.error_indication = WPS_EI_NO_ERROR;
1337325151a3SRui Paulo 	/*
1338325151a3SRui Paulo 	 * Call wpas_notify_wps_event_fail() directly instead of through
1339325151a3SRui Paulo 	 * wpa_supplicant_wps_event() which would end up registering unnecessary
1340325151a3SRui Paulo 	 * timeouts (those are only for the case where the failure happens
1341325151a3SRui Paulo 	 * during an EAP-WSC exchange).
1342325151a3SRui Paulo 	 */
1343325151a3SRui Paulo 	wpas_notify_wps_event_fail(wpa_s, &data.fail);
1344325151a3SRui Paulo }
1345325151a3SRui Paulo 
1346f05cddf9SRui Paulo /* Cancel the wps pbc/pin requests */
wpas_wps_cancel(struct wpa_supplicant * wpa_s)1347f05cddf9SRui Paulo int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1348e28a4053SRui Paulo {
1349f05cddf9SRui Paulo #ifdef CONFIG_AP
1350f05cddf9SRui Paulo 	if (wpa_s->ap_iface) {
1351f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1352f05cddf9SRui Paulo 		return wpa_supplicant_ap_wps_cancel(wpa_s);
1353e28a4053SRui Paulo 	}
1354f05cddf9SRui Paulo #endif /* CONFIG_AP */
1355e28a4053SRui Paulo 
1356f05cddf9SRui Paulo 	if (wpa_s->wpa_state == WPA_SCANNING ||
1357f05cddf9SRui Paulo 	    wpa_s->wpa_state == WPA_DISCONNECTED) {
1358f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1359f05cddf9SRui Paulo 		wpa_supplicant_cancel_scan(wpa_s);
1360e28a4053SRui Paulo 		wpas_clear_wps(wpa_s);
1361f05cddf9SRui Paulo 	} else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1362f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1363f05cddf9SRui Paulo 			   "deauthenticate");
13645b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
1365f05cddf9SRui Paulo 		wpa_supplicant_deauthenticate(wpa_s,
1366f05cddf9SRui Paulo 					      WLAN_REASON_DEAUTH_LEAVING);
1367f05cddf9SRui Paulo 		wpas_clear_wps(wpa_s);
1368f05cddf9SRui Paulo 	} else {
1369f05cddf9SRui Paulo 		wpas_wps_reenable_networks(wpa_s);
1370f05cddf9SRui Paulo 		wpas_wps_clear_ap_info(wpa_s);
13715b9c547cSRui Paulo 		if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
13725b9c547cSRui Paulo 		    0)
13735b9c547cSRui Paulo 			wpas_clear_wps(wpa_s);
1374f05cddf9SRui Paulo 	}
1375e28a4053SRui Paulo 
1376*c1d255d3SCy Schubert 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CANCEL);
13775b9c547cSRui Paulo 	wpa_s->after_wps = 0;
13785b9c547cSRui Paulo 
1379e28a4053SRui Paulo 	return 0;
1380e28a4053SRui Paulo }
1381e28a4053SRui Paulo 
1382e28a4053SRui Paulo 
wpas_wps_start_reg(struct wpa_supplicant * wpa_s,const u8 * bssid,const char * pin,struct wps_new_ap_settings * settings)138339beb93cSSam Leffler int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1384e28a4053SRui Paulo 		       const char *pin, struct wps_new_ap_settings *settings)
138539beb93cSSam Leffler {
138639beb93cSSam Leffler 	struct wpa_ssid *ssid;
1387e28a4053SRui Paulo 	char val[200];
1388e28a4053SRui Paulo 	char *pos, *end;
1389e28a4053SRui Paulo 	int res;
139039beb93cSSam Leffler 
13915b9c547cSRui Paulo #ifdef CONFIG_AP
13925b9c547cSRui Paulo 	if (wpa_s->ap_iface) {
13935b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
13945b9c547cSRui Paulo 			   "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
13955b9c547cSRui Paulo 		return -1;
13965b9c547cSRui Paulo 	}
13975b9c547cSRui Paulo #endif /* CONFIG_AP */
139839beb93cSSam Leffler 	if (!pin)
139939beb93cSSam Leffler 		return -1;
140039beb93cSSam Leffler 	wpas_clear_wps(wpa_s);
14015b9c547cSRui Paulo 	ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
140239beb93cSSam Leffler 	if (ssid == NULL)
140339beb93cSSam Leffler 		return -1;
1404f05cddf9SRui Paulo 	ssid->temporary = 1;
1405e28a4053SRui Paulo 	pos = val;
1406e28a4053SRui Paulo 	end = pos + sizeof(val);
1407e28a4053SRui Paulo 	res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
14085b9c547cSRui Paulo 	if (os_snprintf_error(end - pos, res))
1409e28a4053SRui Paulo 		return -1;
1410e28a4053SRui Paulo 	pos += res;
1411e28a4053SRui Paulo 	if (settings) {
1412e28a4053SRui Paulo 		res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1413e28a4053SRui Paulo 				  "new_encr=%s new_key=%s",
1414e28a4053SRui Paulo 				  settings->ssid_hex, settings->auth,
1415e28a4053SRui Paulo 				  settings->encr, settings->key_hex);
14165b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, res))
1417e28a4053SRui Paulo 			return -1;
1418e28a4053SRui Paulo 		pos += res;
1419e28a4053SRui Paulo 	}
1420e28a4053SRui Paulo 	res = os_snprintf(pos, end - pos, "\"");
14215b9c547cSRui Paulo 	if (os_snprintf_error(end - pos, res))
1422e28a4053SRui Paulo 		return -1;
14235b9c547cSRui Paulo 	if (wpa_config_set(ssid, "phase1", val, 0) < 0)
14245b9c547cSRui Paulo 		return -1;
1425f05cddf9SRui Paulo 	if (wpa_s->wps_fragment_size)
1426f05cddf9SRui Paulo 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
142739beb93cSSam Leffler 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
142839beb93cSSam Leffler 			       wpa_s, NULL);
14295b9c547cSRui Paulo 	wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
143039beb93cSSam Leffler 	return 0;
143139beb93cSSam Leffler }
143239beb93cSSam Leffler 
143339beb93cSSam Leffler 
wpas_wps_new_psk_cb(void * ctx,const u8 * mac_addr,const u8 * p2p_dev_addr,const u8 * psk,size_t psk_len)14345b9c547cSRui Paulo static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
14355b9c547cSRui Paulo 			       const u8 *p2p_dev_addr, const u8 *psk,
143639beb93cSSam Leffler 			       size_t psk_len)
143739beb93cSSam Leffler {
14385b9c547cSRui Paulo 	if (is_zero_ether_addr(p2p_dev_addr)) {
14395b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
14405b9c547cSRui Paulo 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
14415b9c547cSRui Paulo 			   MAC2STR(mac_addr));
14425b9c547cSRui Paulo 	} else {
14435b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
14445b9c547cSRui Paulo 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
14455b9c547cSRui Paulo 			   " P2P Device Addr " MACSTR,
14465b9c547cSRui Paulo 			   MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
14475b9c547cSRui Paulo 	}
144839beb93cSSam Leffler 	wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
144939beb93cSSam Leffler 
145039beb93cSSam Leffler 	/* TODO */
145139beb93cSSam Leffler 
145239beb93cSSam Leffler 	return 0;
145339beb93cSSam Leffler }
145439beb93cSSam Leffler 
145539beb93cSSam Leffler 
wpas_wps_pin_needed_cb(void * ctx,const u8 * uuid_e,const struct wps_device_data * dev)145639beb93cSSam Leffler static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
145739beb93cSSam Leffler 				   const struct wps_device_data *dev)
145839beb93cSSam Leffler {
145939beb93cSSam Leffler 	char uuid[40], txt[400];
146039beb93cSSam Leffler 	int len;
1461e28a4053SRui Paulo 	char devtype[WPS_DEV_TYPE_BUFSIZE];
146239beb93cSSam Leffler 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
146339beb93cSSam Leffler 		return;
146439beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
146539beb93cSSam Leffler 	len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1466e28a4053SRui Paulo 			  " [%s|%s|%s|%s|%s|%s]",
146739beb93cSSam Leffler 			  uuid, MAC2STR(dev->mac_addr), dev->device_name,
146839beb93cSSam Leffler 			  dev->manufacturer, dev->model_name,
146939beb93cSSam Leffler 			  dev->model_number, dev->serial_number,
1470e28a4053SRui Paulo 			  wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1471e28a4053SRui Paulo 					       sizeof(devtype)));
14725b9c547cSRui Paulo 	if (!os_snprintf_error(sizeof(txt), len))
147339beb93cSSam Leffler 		wpa_printf(MSG_INFO, "%s", txt);
147439beb93cSSam Leffler }
147539beb93cSSam Leffler 
147639beb93cSSam Leffler 
wpas_wps_set_sel_reg_cb(void * ctx,int sel_reg,u16 dev_passwd_id,u16 sel_reg_config_methods)1477e28a4053SRui Paulo static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1478e28a4053SRui Paulo 				    u16 sel_reg_config_methods)
1479e28a4053SRui Paulo {
1480e28a4053SRui Paulo #ifdef CONFIG_WPS_ER
1481e28a4053SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
1482e28a4053SRui Paulo 
1483e28a4053SRui Paulo 	if (wpa_s->wps_er == NULL)
1484e28a4053SRui Paulo 		return;
1485f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1486f05cddf9SRui Paulo 		   "dev_password_id=%u sel_reg_config_methods=0x%x",
1487f05cddf9SRui Paulo 		   sel_reg, dev_passwd_id, sel_reg_config_methods);
1488e28a4053SRui Paulo 	wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1489e28a4053SRui Paulo 			   sel_reg_config_methods);
1490e28a4053SRui Paulo #endif /* CONFIG_WPS_ER */
1491e28a4053SRui Paulo }
1492e28a4053SRui Paulo 
1493e28a4053SRui Paulo 
wps_fix_config_methods(u16 config_methods)1494f05cddf9SRui Paulo static u16 wps_fix_config_methods(u16 config_methods)
1495f05cddf9SRui Paulo {
1496f05cddf9SRui Paulo 	if ((config_methods &
1497f05cddf9SRui Paulo 	     (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1498f05cddf9SRui Paulo 	      WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1499f05cddf9SRui Paulo 		wpa_printf(MSG_INFO, "WPS: Converting display to "
1500f05cddf9SRui Paulo 			   "virtual_display for WPS 2.0 compliance");
1501f05cddf9SRui Paulo 		config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1502f05cddf9SRui Paulo 	}
1503f05cddf9SRui Paulo 	if ((config_methods &
1504f05cddf9SRui Paulo 	     (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1505f05cddf9SRui Paulo 	      WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1506f05cddf9SRui Paulo 		wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1507f05cddf9SRui Paulo 			   "virtual_push_button for WPS 2.0 compliance");
1508f05cddf9SRui Paulo 		config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1509f05cddf9SRui Paulo 	}
1510f05cddf9SRui Paulo 
1511f05cddf9SRui Paulo 	return config_methods;
1512f05cddf9SRui Paulo }
1513f05cddf9SRui Paulo 
1514f05cddf9SRui Paulo 
wpas_wps_set_uuid(struct wpa_supplicant * wpa_s,struct wps_context * wps)1515f05cddf9SRui Paulo static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1516f05cddf9SRui Paulo 			      struct wps_context *wps)
1517f05cddf9SRui Paulo {
15185b9c547cSRui Paulo 	char buf[50];
15195b9c547cSRui Paulo 	const char *src;
15205b9c547cSRui Paulo 
1521f05cddf9SRui Paulo 	if (is_nil_uuid(wpa_s->conf->uuid)) {
1522f05cddf9SRui Paulo 		struct wpa_supplicant *first;
1523f05cddf9SRui Paulo 		first = wpa_s->global->ifaces;
1524f05cddf9SRui Paulo 		while (first && first->next)
1525f05cddf9SRui Paulo 			first = first->next;
1526f05cddf9SRui Paulo 		if (first && first != wpa_s) {
1527f05cddf9SRui Paulo 			if (wps != wpa_s->global->ifaces->wps)
1528f05cddf9SRui Paulo 				os_memcpy(wps->uuid,
1529f05cddf9SRui Paulo 					  wpa_s->global->ifaces->wps->uuid,
1530f05cddf9SRui Paulo 					  WPS_UUID_LEN);
15315b9c547cSRui Paulo 			src = "from the first interface";
153285732ac8SCy Schubert 		} else if (wpa_s->conf->auto_uuid == 1) {
153385732ac8SCy Schubert 			uuid_random(wps->uuid);
153485732ac8SCy Schubert 			src = "based on random data";
1535f05cddf9SRui Paulo 		} else {
1536f05cddf9SRui Paulo 			uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
15375b9c547cSRui Paulo 			src = "based on MAC address";
1538f05cddf9SRui Paulo 		}
1539f05cddf9SRui Paulo 	} else {
1540f05cddf9SRui Paulo 		os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
15415b9c547cSRui Paulo 		src = "based on configuration";
1542f05cddf9SRui Paulo 	}
15435b9c547cSRui Paulo 
15445b9c547cSRui Paulo 	uuid_bin2str(wps->uuid, buf, sizeof(buf));
15455b9c547cSRui Paulo 	wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
1546f05cddf9SRui Paulo }
1547f05cddf9SRui Paulo 
1548f05cddf9SRui Paulo 
wpas_wps_set_vendor_ext_m1(struct wpa_supplicant * wpa_s,struct wps_context * wps)1549f05cddf9SRui Paulo static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1550f05cddf9SRui Paulo 				       struct wps_context *wps)
1551f05cddf9SRui Paulo {
1552f05cddf9SRui Paulo 	wpabuf_free(wps->dev.vendor_ext_m1);
1553f05cddf9SRui Paulo 	wps->dev.vendor_ext_m1 = NULL;
1554f05cddf9SRui Paulo 
1555f05cddf9SRui Paulo 	if (wpa_s->conf->wps_vendor_ext_m1) {
1556f05cddf9SRui Paulo 		wps->dev.vendor_ext_m1 =
1557f05cddf9SRui Paulo 			wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1558f05cddf9SRui Paulo 		if (!wps->dev.vendor_ext_m1) {
1559f05cddf9SRui Paulo 			wpa_printf(MSG_ERROR, "WPS: Cannot "
1560f05cddf9SRui Paulo 				   "allocate memory for vendor_ext_m1");
1561f05cddf9SRui Paulo 		}
1562f05cddf9SRui Paulo 	}
1563f05cddf9SRui Paulo }
1564f05cddf9SRui Paulo 
1565f05cddf9SRui Paulo 
wpas_wps_init(struct wpa_supplicant * wpa_s)156639beb93cSSam Leffler int wpas_wps_init(struct wpa_supplicant *wpa_s)
156739beb93cSSam Leffler {
156839beb93cSSam Leffler 	struct wps_context *wps;
156939beb93cSSam Leffler 	struct wps_registrar_config rcfg;
1570f05cddf9SRui Paulo 	struct hostapd_hw_modes *modes;
1571f05cddf9SRui Paulo 	u16 m;
157239beb93cSSam Leffler 
157339beb93cSSam Leffler 	wps = os_zalloc(sizeof(*wps));
157439beb93cSSam Leffler 	if (wps == NULL)
157539beb93cSSam Leffler 		return -1;
157639beb93cSSam Leffler 
157739beb93cSSam Leffler 	wps->cred_cb = wpa_supplicant_wps_cred;
157839beb93cSSam Leffler 	wps->event_cb = wpa_supplicant_wps_event;
15795b9c547cSRui Paulo 	wps->rf_band_cb = wpa_supplicant_wps_rf_band;
158039beb93cSSam Leffler 	wps->cb_ctx = wpa_s;
158139beb93cSSam Leffler 
158239beb93cSSam Leffler 	wps->dev.device_name = wpa_s->conf->device_name;
158339beb93cSSam Leffler 	wps->dev.manufacturer = wpa_s->conf->manufacturer;
158439beb93cSSam Leffler 	wps->dev.model_name = wpa_s->conf->model_name;
158539beb93cSSam Leffler 	wps->dev.model_number = wpa_s->conf->model_number;
158639beb93cSSam Leffler 	wps->dev.serial_number = wpa_s->conf->serial_number;
1587e28a4053SRui Paulo 	wps->config_methods =
1588e28a4053SRui Paulo 		wps_config_methods_str2bin(wpa_s->conf->config_methods);
1589f05cddf9SRui Paulo 	if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1590f05cddf9SRui Paulo 	    (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1591f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1592f05cddf9SRui Paulo 			   "methods are not allowed at the same time");
159339beb93cSSam Leffler 		os_free(wps);
159439beb93cSSam Leffler 		return -1;
159539beb93cSSam Leffler 	}
1596f05cddf9SRui Paulo 	wps->config_methods = wps_fix_config_methods(wps->config_methods);
1597f05cddf9SRui Paulo 	wps->dev.config_methods = wps->config_methods;
1598f05cddf9SRui Paulo 	os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1599f05cddf9SRui Paulo 		  WPS_DEV_TYPE_LEN);
1600f05cddf9SRui Paulo 
1601f05cddf9SRui Paulo 	wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1602f05cddf9SRui Paulo 	os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1603f05cddf9SRui Paulo 		  WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1604f05cddf9SRui Paulo 
1605f05cddf9SRui Paulo 	wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1606f05cddf9SRui Paulo 
160739beb93cSSam Leffler 	wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1608f05cddf9SRui Paulo 	modes = wpa_s->hw.modes;
1609f05cddf9SRui Paulo 	if (modes) {
1610f05cddf9SRui Paulo 		for (m = 0; m < wpa_s->hw.num_modes; m++) {
1611f05cddf9SRui Paulo 			if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1612f05cddf9SRui Paulo 			    modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1613f05cddf9SRui Paulo 				wps->dev.rf_bands |= WPS_RF_24GHZ;
1614f05cddf9SRui Paulo 			else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1615f05cddf9SRui Paulo 				wps->dev.rf_bands |= WPS_RF_50GHZ;
1616325151a3SRui Paulo 			else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1617325151a3SRui Paulo 				wps->dev.rf_bands |= WPS_RF_60GHZ;
1618f05cddf9SRui Paulo 		}
1619f05cddf9SRui Paulo 	}
1620f05cddf9SRui Paulo 	if (wps->dev.rf_bands == 0) {
1621f05cddf9SRui Paulo 		/*
1622f05cddf9SRui Paulo 		 * Default to claiming support for both bands if the driver
1623f05cddf9SRui Paulo 		 * does not provide support for fetching supported bands.
1624f05cddf9SRui Paulo 		 */
1625f05cddf9SRui Paulo 		wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1626f05cddf9SRui Paulo 	}
162739beb93cSSam Leffler 	os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1628f05cddf9SRui Paulo 	wpas_wps_set_uuid(wpa_s, wps);
162939beb93cSSam Leffler 
1630*c1d255d3SCy Schubert #ifdef CONFIG_NO_TKIP
1631*c1d255d3SCy Schubert 	wps->auth_types = WPS_AUTH_WPA2PSK;
1632*c1d255d3SCy Schubert 	wps->encr_types = WPS_ENCR_AES;
1633*c1d255d3SCy Schubert #else /* CONFIG_NO_TKIP */
163439beb93cSSam Leffler 	wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
163539beb93cSSam Leffler 	wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1636*c1d255d3SCy Schubert #endif /* CONFIG_NO_TKIP */
163739beb93cSSam Leffler 
163839beb93cSSam Leffler 	os_memset(&rcfg, 0, sizeof(rcfg));
163939beb93cSSam Leffler 	rcfg.new_psk_cb = wpas_wps_new_psk_cb;
164039beb93cSSam Leffler 	rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1641e28a4053SRui Paulo 	rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
164239beb93cSSam Leffler 	rcfg.cb_ctx = wpa_s;
164339beb93cSSam Leffler 
164439beb93cSSam Leffler 	wps->registrar = wps_registrar_init(wps, &rcfg);
164539beb93cSSam Leffler 	if (wps->registrar == NULL) {
164639beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
164739beb93cSSam Leffler 		os_free(wps);
164839beb93cSSam Leffler 		return -1;
164939beb93cSSam Leffler 	}
165039beb93cSSam Leffler 
165139beb93cSSam Leffler 	wpa_s->wps = wps;
165239beb93cSSam Leffler 
165339beb93cSSam Leffler 	return 0;
165439beb93cSSam Leffler }
165539beb93cSSam Leffler 
165639beb93cSSam Leffler 
16575b9c547cSRui Paulo #ifdef CONFIG_WPS_ER
wpas_wps_nfc_clear(struct wps_context * wps)16585b9c547cSRui Paulo static void wpas_wps_nfc_clear(struct wps_context *wps)
16595b9c547cSRui Paulo {
16605b9c547cSRui Paulo 	wps->ap_nfc_dev_pw_id = 0;
16615b9c547cSRui Paulo 	wpabuf_free(wps->ap_nfc_dh_pubkey);
16625b9c547cSRui Paulo 	wps->ap_nfc_dh_pubkey = NULL;
16635b9c547cSRui Paulo 	wpabuf_free(wps->ap_nfc_dh_privkey);
16645b9c547cSRui Paulo 	wps->ap_nfc_dh_privkey = NULL;
16655b9c547cSRui Paulo 	wpabuf_free(wps->ap_nfc_dev_pw);
16665b9c547cSRui Paulo 	wps->ap_nfc_dev_pw = NULL;
16675b9c547cSRui Paulo }
16685b9c547cSRui Paulo #endif /* CONFIG_WPS_ER */
16695b9c547cSRui Paulo 
16705b9c547cSRui Paulo 
wpas_wps_deinit(struct wpa_supplicant * wpa_s)167139beb93cSSam Leffler void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
167239beb93cSSam Leffler {
16735b9c547cSRui Paulo 	wpas_wps_assoc_with_cred_cancel(wpa_s);
167439beb93cSSam Leffler 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
16755b9c547cSRui Paulo 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
1676f05cddf9SRui Paulo 	eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
1677f05cddf9SRui Paulo 	wpas_wps_clear_ap_info(wpa_s);
167839beb93cSSam Leffler 
16795b9c547cSRui Paulo #ifdef CONFIG_P2P
16805b9c547cSRui Paulo 	eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
16815b9c547cSRui Paulo #endif /* CONFIG_P2P */
16825b9c547cSRui Paulo 
168339beb93cSSam Leffler 	if (wpa_s->wps == NULL)
168439beb93cSSam Leffler 		return;
168539beb93cSSam Leffler 
1686e28a4053SRui Paulo #ifdef CONFIG_WPS_ER
1687e28a4053SRui Paulo 	wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1688e28a4053SRui Paulo 	wpa_s->wps_er = NULL;
16895b9c547cSRui Paulo 	wpas_wps_nfc_clear(wpa_s->wps);
1690e28a4053SRui Paulo #endif /* CONFIG_WPS_ER */
1691e28a4053SRui Paulo 
169239beb93cSSam Leffler 	wps_registrar_deinit(wpa_s->wps->registrar);
1693e28a4053SRui Paulo 	wpabuf_free(wpa_s->wps->dh_pubkey);
1694e28a4053SRui Paulo 	wpabuf_free(wpa_s->wps->dh_privkey);
1695f05cddf9SRui Paulo 	wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
169639beb93cSSam Leffler 	os_free(wpa_s->wps->network_key);
169739beb93cSSam Leffler 	os_free(wpa_s->wps);
169839beb93cSSam Leffler 	wpa_s->wps = NULL;
169939beb93cSSam Leffler }
170039beb93cSSam Leffler 
170139beb93cSSam Leffler 
wpas_wps_ssid_bss_match(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_bss * bss)170239beb93cSSam Leffler int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1703f05cddf9SRui Paulo 			    struct wpa_ssid *ssid, struct wpa_bss *bss)
170439beb93cSSam Leffler {
170539beb93cSSam Leffler 	struct wpabuf *wps_ie;
170639beb93cSSam Leffler 
170739beb93cSSam Leffler 	if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
170839beb93cSSam Leffler 		return -1;
170939beb93cSSam Leffler 
1710f05cddf9SRui Paulo 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
171139beb93cSSam Leffler 	if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
171239beb93cSSam Leffler 		if (!wps_ie) {
171339beb93cSSam Leffler 			wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
171439beb93cSSam Leffler 			return 0;
171539beb93cSSam Leffler 		}
171639beb93cSSam Leffler 
171739beb93cSSam Leffler 		if (!wps_is_selected_pbc_registrar(wps_ie)) {
171839beb93cSSam Leffler 			wpa_printf(MSG_DEBUG, "   skip - WPS AP "
171939beb93cSSam Leffler 				   "without active PBC Registrar");
172039beb93cSSam Leffler 			wpabuf_free(wps_ie);
172139beb93cSSam Leffler 			return 0;
172239beb93cSSam Leffler 		}
172339beb93cSSam Leffler 
172439beb93cSSam Leffler 		/* TODO: overlap detection */
172539beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
172639beb93cSSam Leffler 			   "(Active PBC)");
172739beb93cSSam Leffler 		wpabuf_free(wps_ie);
172839beb93cSSam Leffler 		return 1;
172939beb93cSSam Leffler 	}
173039beb93cSSam Leffler 
173139beb93cSSam Leffler 	if (eap_is_wps_pin_enrollee(&ssid->eap)) {
173239beb93cSSam Leffler 		if (!wps_ie) {
173339beb93cSSam Leffler 			wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
173439beb93cSSam Leffler 			return 0;
173539beb93cSSam Leffler 		}
173639beb93cSSam Leffler 
173739beb93cSSam Leffler 		/*
1738f05cddf9SRui Paulo 		 * Start with WPS APs that advertise our address as an
1739f05cddf9SRui Paulo 		 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1740f05cddf9SRui Paulo 		 * allow any WPS AP after couple of scans since some APs do not
1741f05cddf9SRui Paulo 		 * set Selected Registrar attribute properly when using
1742f05cddf9SRui Paulo 		 * external Registrar.
174339beb93cSSam Leffler 		 */
1744f05cddf9SRui Paulo 		if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1745325151a3SRui Paulo 			struct os_reltime age;
1746325151a3SRui Paulo 
1747325151a3SRui Paulo 			os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1748325151a3SRui Paulo 
1749325151a3SRui Paulo 			if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1750325151a3SRui Paulo 			    age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1751325151a3SRui Paulo 				wpa_printf(MSG_DEBUG,
1752325151a3SRui Paulo 					   "   skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1753325151a3SRui Paulo 					   wpa_s->scan_runs, (int) age.sec);
175439beb93cSSam Leffler 				wpabuf_free(wps_ie);
175539beb93cSSam Leffler 				return 0;
175639beb93cSSam Leffler 			}
175739beb93cSSam Leffler 			wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
175839beb93cSSam Leffler 		} else {
175939beb93cSSam Leffler 			wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1760f05cddf9SRui Paulo 				   "(Authorized MAC or Active PIN)");
176139beb93cSSam Leffler 		}
176239beb93cSSam Leffler 		wpabuf_free(wps_ie);
176339beb93cSSam Leffler 		return 1;
176439beb93cSSam Leffler 	}
176539beb93cSSam Leffler 
176639beb93cSSam Leffler 	if (wps_ie) {
176739beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
176839beb93cSSam Leffler 		wpabuf_free(wps_ie);
176939beb93cSSam Leffler 		return 1;
177039beb93cSSam Leffler 	}
177139beb93cSSam Leffler 
177239beb93cSSam Leffler 	return -1;
177339beb93cSSam Leffler }
177439beb93cSSam Leffler 
177539beb93cSSam Leffler 
wpas_wps_ssid_wildcard_ok(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_bss * bss)177639beb93cSSam Leffler int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
177739beb93cSSam Leffler 			      struct wpa_ssid *ssid,
1778f05cddf9SRui Paulo 			      struct wpa_bss *bss)
177939beb93cSSam Leffler {
178039beb93cSSam Leffler 	struct wpabuf *wps_ie = NULL;
178139beb93cSSam Leffler 	int ret = 0;
178239beb93cSSam Leffler 
178339beb93cSSam Leffler 	if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1784f05cddf9SRui Paulo 		wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
178539beb93cSSam Leffler 		if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
178639beb93cSSam Leffler 			/* allow wildcard SSID for WPS PBC */
178739beb93cSSam Leffler 			ret = 1;
178839beb93cSSam Leffler 		}
178939beb93cSSam Leffler 	} else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1790f05cddf9SRui Paulo 		wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
179139beb93cSSam Leffler 		if (wps_ie &&
1792f05cddf9SRui Paulo 		    (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
179339beb93cSSam Leffler 		     wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
179439beb93cSSam Leffler 			/* allow wildcard SSID for WPS PIN */
179539beb93cSSam Leffler 			ret = 1;
179639beb93cSSam Leffler 		}
179739beb93cSSam Leffler 	}
179839beb93cSSam Leffler 
179939beb93cSSam Leffler 	if (!ret && ssid->bssid_set &&
180039beb93cSSam Leffler 	    os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
180139beb93cSSam Leffler 		/* allow wildcard SSID due to hardcoded BSSID match */
180239beb93cSSam Leffler 		ret = 1;
180339beb93cSSam Leffler 	}
180439beb93cSSam Leffler 
1805f05cddf9SRui Paulo #ifdef CONFIG_WPS_STRICT
1806f05cddf9SRui Paulo 	if (wps_ie) {
1807f05cddf9SRui Paulo 		if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1808f05cddf9SRui Paulo 						   0, bss->bssid) < 0)
1809f05cddf9SRui Paulo 			ret = 0;
1810f05cddf9SRui Paulo 		if (bss->beacon_ie_len) {
1811f05cddf9SRui Paulo 			struct wpabuf *bcn_wps;
1812f05cddf9SRui Paulo 			bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
1813f05cddf9SRui Paulo 				bss, WPS_IE_VENDOR_TYPE);
1814f05cddf9SRui Paulo 			if (bcn_wps == NULL) {
1815f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1816f05cddf9SRui Paulo 					   "missing from AP Beacon");
1817f05cddf9SRui Paulo 				ret = 0;
1818f05cddf9SRui Paulo 			} else {
1819f05cddf9SRui Paulo 				if (wps_validate_beacon(wps_ie) < 0)
1820f05cddf9SRui Paulo 					ret = 0;
1821f05cddf9SRui Paulo 				wpabuf_free(bcn_wps);
1822f05cddf9SRui Paulo 			}
1823f05cddf9SRui Paulo 		}
1824f05cddf9SRui Paulo 	}
1825f05cddf9SRui Paulo #endif /* CONFIG_WPS_STRICT */
1826f05cddf9SRui Paulo 
182739beb93cSSam Leffler 	wpabuf_free(wps_ie);
182839beb93cSSam Leffler 
182939beb93cSSam Leffler 	return ret;
183039beb93cSSam Leffler }
183139beb93cSSam Leffler 
183239beb93cSSam Leffler 
wpas_wps_scan_pbc_overlap(struct wpa_supplicant * wpa_s,struct wpa_bss * selected,struct wpa_ssid * ssid)183339beb93cSSam Leffler int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1834e28a4053SRui Paulo 			      struct wpa_bss *selected, struct wpa_ssid *ssid)
183539beb93cSSam Leffler {
1836325151a3SRui Paulo 	const u8 *sel_uuid;
183739beb93cSSam Leffler 	struct wpabuf *wps_ie;
183839beb93cSSam Leffler 	int ret = 0;
1839325151a3SRui Paulo 	size_t i;
184039beb93cSSam Leffler 
184139beb93cSSam Leffler 	if (!eap_is_wps_pbc_enrollee(&ssid->eap))
184239beb93cSSam Leffler 		return 0;
184339beb93cSSam Leffler 
1844f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1845f05cddf9SRui Paulo 		   "present in scan results; selected BSSID " MACSTR,
1846f05cddf9SRui Paulo 		   MAC2STR(selected->bssid));
1847*c1d255d3SCy Schubert 	if (!is_zero_ether_addr(ssid->bssid))
1848*c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG,
1849*c1d255d3SCy Schubert 			   "WPS: Network profile limited to accept only a single BSSID " MACSTR,
1850*c1d255d3SCy Schubert 			   MAC2STR(ssid->bssid));
1851f05cddf9SRui Paulo 
185239beb93cSSam Leffler 	/* Make sure that only one AP is in active PBC mode */
1853e28a4053SRui Paulo 	wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1854f05cddf9SRui Paulo 	if (wps_ie) {
185539beb93cSSam Leffler 		sel_uuid = wps_get_uuid_e(wps_ie);
1856f05cddf9SRui Paulo 		wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1857f05cddf9SRui Paulo 			    sel_uuid, UUID_LEN);
1858f05cddf9SRui Paulo 	} else {
1859f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1860f05cddf9SRui Paulo 			   "WPS IE?!");
186139beb93cSSam Leffler 		sel_uuid = NULL;
1862f05cddf9SRui Paulo 	}
186339beb93cSSam Leffler 
1864325151a3SRui Paulo 	for (i = 0; i < wpa_s->num_wps_ap; i++) {
1865325151a3SRui Paulo 		struct wps_ap_info *ap = &wpa_s->wps_ap[i];
1866325151a3SRui Paulo 
1867325151a3SRui Paulo 		if (!ap->pbc_active ||
1868325151a3SRui Paulo 		    os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
186939beb93cSSam Leffler 			continue;
1870325151a3SRui Paulo 
1871*c1d255d3SCy Schubert 		if (!is_zero_ether_addr(ssid->bssid) &&
1872*c1d255d3SCy Schubert 		    os_memcmp(ap->bssid, ssid->bssid, ETH_ALEN) != 0) {
1873*c1d255d3SCy Schubert 			wpa_printf(MSG_DEBUG, "WPS: Ignore another BSS " MACSTR
1874*c1d255d3SCy Schubert 				   " in active PBC mode due to local BSSID limitation",
1875*c1d255d3SCy Schubert 				   MAC2STR(ap->bssid));
1876*c1d255d3SCy Schubert 			continue;
1877*c1d255d3SCy Schubert 		}
1878*c1d255d3SCy Schubert 
1879f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1880325151a3SRui Paulo 			   MACSTR, MAC2STR(ap->bssid));
1881f05cddf9SRui Paulo 		wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1882325151a3SRui Paulo 			    ap->uuid, UUID_LEN);
1883325151a3SRui Paulo 		if (sel_uuid == NULL ||
1884325151a3SRui Paulo 		    os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0) {
188539beb93cSSam Leffler 			ret = 1; /* PBC overlap */
1886f05cddf9SRui Paulo 			wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1887f05cddf9SRui Paulo 				MACSTR " and " MACSTR,
1888f05cddf9SRui Paulo 				MAC2STR(selected->bssid),
1889325151a3SRui Paulo 				MAC2STR(ap->bssid));
189039beb93cSSam Leffler 			break;
189139beb93cSSam Leffler 		}
189239beb93cSSam Leffler 
189339beb93cSSam Leffler 		/* TODO: verify that this is reasonable dual-band situation */
189439beb93cSSam Leffler 	}
189539beb93cSSam Leffler 
189639beb93cSSam Leffler 	wpabuf_free(wps_ie);
189739beb93cSSam Leffler 
189839beb93cSSam Leffler 	return ret;
189939beb93cSSam Leffler }
190039beb93cSSam Leffler 
190139beb93cSSam Leffler 
wpas_wps_notify_scan_results(struct wpa_supplicant * wpa_s)190239beb93cSSam Leffler void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
190339beb93cSSam Leffler {
1904e28a4053SRui Paulo 	struct wpa_bss *bss;
1905f05cddf9SRui Paulo 	unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
190639beb93cSSam Leffler 
190739beb93cSSam Leffler 	if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
190839beb93cSSam Leffler 		return;
190939beb93cSSam Leffler 
1910e28a4053SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
191139beb93cSSam Leffler 		struct wpabuf *ie;
1912e28a4053SRui Paulo 		ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
191339beb93cSSam Leffler 		if (!ie)
191439beb93cSSam Leffler 			continue;
191539beb93cSSam Leffler 		if (wps_is_selected_pbc_registrar(ie))
1916f05cddf9SRui Paulo 			pbc++;
1917f05cddf9SRui Paulo 		else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1918f05cddf9SRui Paulo 			auth++;
191939beb93cSSam Leffler 		else if (wps_is_selected_pin_registrar(ie))
1920f05cddf9SRui Paulo 			pin++;
192139beb93cSSam Leffler 		else
1922f05cddf9SRui Paulo 			wps++;
192339beb93cSSam Leffler 		wpabuf_free(ie);
192439beb93cSSam Leffler 	}
1925f05cddf9SRui Paulo 
1926f05cddf9SRui Paulo 	if (pbc)
1927f05cddf9SRui Paulo 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1928f05cddf9SRui Paulo 	else if (auth)
1929f05cddf9SRui Paulo 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1930f05cddf9SRui Paulo 	else if (pin)
1931f05cddf9SRui Paulo 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1932f05cddf9SRui Paulo 	else if (wps)
1933f05cddf9SRui Paulo 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
193439beb93cSSam Leffler }
193539beb93cSSam Leffler 
193639beb93cSSam Leffler 
wpas_wps_searching(struct wpa_supplicant * wpa_s)193739beb93cSSam Leffler int wpas_wps_searching(struct wpa_supplicant *wpa_s)
193839beb93cSSam Leffler {
193939beb93cSSam Leffler 	struct wpa_ssid *ssid;
194039beb93cSSam Leffler 
194139beb93cSSam Leffler 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
194239beb93cSSam Leffler 		if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
194339beb93cSSam Leffler 			return 1;
194439beb93cSSam Leffler 	}
194539beb93cSSam Leffler 
194639beb93cSSam Leffler 	return 0;
194739beb93cSSam Leffler }
1948e28a4053SRui Paulo 
1949e28a4053SRui Paulo 
wpas_wps_scan_result_text(const u8 * ies,size_t ies_len,char * buf,char * end)1950e28a4053SRui Paulo int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1951e28a4053SRui Paulo 			      char *end)
1952e28a4053SRui Paulo {
1953e28a4053SRui Paulo 	struct wpabuf *wps_ie;
1954e28a4053SRui Paulo 	int ret;
1955e28a4053SRui Paulo 
1956e28a4053SRui Paulo 	wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1957e28a4053SRui Paulo 	if (wps_ie == NULL)
1958e28a4053SRui Paulo 		return 0;
1959e28a4053SRui Paulo 
1960e28a4053SRui Paulo 	ret = wps_attr_text(wps_ie, buf, end);
1961e28a4053SRui Paulo 	wpabuf_free(wps_ie);
1962e28a4053SRui Paulo 	return ret;
1963e28a4053SRui Paulo }
1964e28a4053SRui Paulo 
1965e28a4053SRui Paulo 
wpas_wps_er_start(struct wpa_supplicant * wpa_s,const char * filter)1966f05cddf9SRui Paulo int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1967e28a4053SRui Paulo {
1968e28a4053SRui Paulo #ifdef CONFIG_WPS_ER
1969e28a4053SRui Paulo 	if (wpa_s->wps_er) {
1970e28a4053SRui Paulo 		wps_er_refresh(wpa_s->wps_er);
1971e28a4053SRui Paulo 		return 0;
1972e28a4053SRui Paulo 	}
1973f05cddf9SRui Paulo 	wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1974e28a4053SRui Paulo 	if (wpa_s->wps_er == NULL)
1975e28a4053SRui Paulo 		return -1;
1976e28a4053SRui Paulo 	return 0;
1977e28a4053SRui Paulo #else /* CONFIG_WPS_ER */
1978e28a4053SRui Paulo 	return 0;
1979e28a4053SRui Paulo #endif /* CONFIG_WPS_ER */
1980e28a4053SRui Paulo }
1981e28a4053SRui Paulo 
1982e28a4053SRui Paulo 
wpas_wps_er_stop(struct wpa_supplicant * wpa_s)19835b9c547cSRui Paulo void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1984e28a4053SRui Paulo {
1985e28a4053SRui Paulo #ifdef CONFIG_WPS_ER
1986e28a4053SRui Paulo 	wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1987e28a4053SRui Paulo 	wpa_s->wps_er = NULL;
1988e28a4053SRui Paulo #endif /* CONFIG_WPS_ER */
1989e28a4053SRui Paulo }
1990e28a4053SRui Paulo 
1991e28a4053SRui Paulo 
1992e28a4053SRui Paulo #ifdef CONFIG_WPS_ER
wpas_wps_er_add_pin(struct wpa_supplicant * wpa_s,const u8 * addr,const char * uuid,const char * pin)1993f05cddf9SRui Paulo int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1994f05cddf9SRui Paulo 			const char *uuid, const char *pin)
1995e28a4053SRui Paulo {
1996e28a4053SRui Paulo 	u8 u[UUID_LEN];
19975b9c547cSRui Paulo 	const u8 *use_uuid = NULL;
19985b9c547cSRui Paulo 	u8 addr_buf[ETH_ALEN];
1999e28a4053SRui Paulo 
20005b9c547cSRui Paulo 	if (os_strcmp(uuid, "any") == 0) {
20015b9c547cSRui Paulo 	} else if (uuid_str2bin(uuid, u) == 0) {
20025b9c547cSRui Paulo 		use_uuid = u;
20035b9c547cSRui Paulo 	} else if (hwaddr_aton(uuid, addr_buf) == 0) {
20045b9c547cSRui Paulo 		use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
20055b9c547cSRui Paulo 		if (use_uuid == NULL)
20065b9c547cSRui Paulo 			return -1;
20075b9c547cSRui Paulo 	} else
2008e28a4053SRui Paulo 		return -1;
2009f05cddf9SRui Paulo 	return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
20105b9c547cSRui Paulo 				     use_uuid,
2011e28a4053SRui Paulo 				     (const u8 *) pin, os_strlen(pin), 300);
2012e28a4053SRui Paulo }
2013e28a4053SRui Paulo 
2014e28a4053SRui Paulo 
wpas_wps_er_pbc(struct wpa_supplicant * wpa_s,const char * uuid)2015e28a4053SRui Paulo int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
2016e28a4053SRui Paulo {
20175b9c547cSRui Paulo 	u8 u[UUID_LEN], *use_uuid = NULL;
20185b9c547cSRui Paulo 	u8 addr[ETH_ALEN], *use_addr = NULL;
2019e28a4053SRui Paulo 
20205b9c547cSRui Paulo 	if (uuid_str2bin(uuid, u) == 0)
20215b9c547cSRui Paulo 		use_uuid = u;
20225b9c547cSRui Paulo 	else if (hwaddr_aton(uuid, addr) == 0)
20235b9c547cSRui Paulo 		use_addr = addr;
20245b9c547cSRui Paulo 	else
2025e28a4053SRui Paulo 		return -1;
20265b9c547cSRui Paulo 	return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
2027e28a4053SRui Paulo }
2028e28a4053SRui Paulo 
2029e28a4053SRui Paulo 
wpas_wps_er_learn(struct wpa_supplicant * wpa_s,const char * uuid,const char * pin)2030e28a4053SRui Paulo int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
2031e28a4053SRui Paulo 		      const char *pin)
2032e28a4053SRui Paulo {
20335b9c547cSRui Paulo 	u8 u[UUID_LEN], *use_uuid = NULL;
20345b9c547cSRui Paulo 	u8 addr[ETH_ALEN], *use_addr = NULL;
2035e28a4053SRui Paulo 
20365b9c547cSRui Paulo 	if (uuid_str2bin(uuid, u) == 0)
20375b9c547cSRui Paulo 		use_uuid = u;
20385b9c547cSRui Paulo 	else if (hwaddr_aton(uuid, addr) == 0)
20395b9c547cSRui Paulo 		use_addr = addr;
20405b9c547cSRui Paulo 	else
2041e28a4053SRui Paulo 		return -1;
20425b9c547cSRui Paulo 
20435b9c547cSRui Paulo 	return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
2044e28a4053SRui Paulo 			    os_strlen(pin));
2045e28a4053SRui Paulo }
2046e28a4053SRui Paulo 
2047e28a4053SRui Paulo 
wpas_wps_network_to_cred(struct wpa_ssid * ssid,struct wps_credential * cred)20485b9c547cSRui Paulo static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
20495b9c547cSRui Paulo 				    struct wps_credential *cred)
20505b9c547cSRui Paulo {
20515b9c547cSRui Paulo 	os_memset(cred, 0, sizeof(*cred));
2052325151a3SRui Paulo 	if (ssid->ssid_len > SSID_MAX_LEN)
20535b9c547cSRui Paulo 		return -1;
20545b9c547cSRui Paulo 	os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
20555b9c547cSRui Paulo 	cred->ssid_len = ssid->ssid_len;
20565b9c547cSRui Paulo 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
20575b9c547cSRui Paulo 		cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
20585b9c547cSRui Paulo 			WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
20595b9c547cSRui Paulo 		if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
20605b9c547cSRui Paulo 			cred->encr_type = WPS_ENCR_AES;
20615b9c547cSRui Paulo 		else
20625b9c547cSRui Paulo 			cred->encr_type = WPS_ENCR_TKIP;
20635b9c547cSRui Paulo 		if (ssid->passphrase) {
20645b9c547cSRui Paulo 			cred->key_len = os_strlen(ssid->passphrase);
20655b9c547cSRui Paulo 			if (cred->key_len >= 64)
20665b9c547cSRui Paulo 				return -1;
20675b9c547cSRui Paulo 			os_memcpy(cred->key, ssid->passphrase, cred->key_len);
20685b9c547cSRui Paulo 		} else if (ssid->psk_set) {
20695b9c547cSRui Paulo 			cred->key_len = 32;
20705b9c547cSRui Paulo 			os_memcpy(cred->key, ssid->psk, 32);
20715b9c547cSRui Paulo 		} else
20725b9c547cSRui Paulo 			return -1;
20735b9c547cSRui Paulo 	} else {
20745b9c547cSRui Paulo 		cred->auth_type = WPS_AUTH_OPEN;
20755b9c547cSRui Paulo 		cred->encr_type = WPS_ENCR_NONE;
20765b9c547cSRui Paulo 	}
20775b9c547cSRui Paulo 
20785b9c547cSRui Paulo 	return 0;
20795b9c547cSRui Paulo }
20805b9c547cSRui Paulo 
20815b9c547cSRui Paulo 
wpas_wps_er_set_config(struct wpa_supplicant * wpa_s,const char * uuid,int id)2082f05cddf9SRui Paulo int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2083f05cddf9SRui Paulo 			   int id)
2084f05cddf9SRui Paulo {
20855b9c547cSRui Paulo 	u8 u[UUID_LEN], *use_uuid = NULL;
20865b9c547cSRui Paulo 	u8 addr[ETH_ALEN], *use_addr = NULL;
2087f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
2088f05cddf9SRui Paulo 	struct wps_credential cred;
20895b9c547cSRui Paulo 	int ret;
2090f05cddf9SRui Paulo 
20915b9c547cSRui Paulo 	if (uuid_str2bin(uuid, u) == 0)
20925b9c547cSRui Paulo 		use_uuid = u;
20935b9c547cSRui Paulo 	else if (hwaddr_aton(uuid, addr) == 0)
20945b9c547cSRui Paulo 		use_addr = addr;
20955b9c547cSRui Paulo 	else
2096f05cddf9SRui Paulo 		return -1;
2097f05cddf9SRui Paulo 	ssid = wpa_config_get_network(wpa_s->conf, id);
2098f05cddf9SRui Paulo 	if (ssid == NULL || ssid->ssid == NULL)
2099f05cddf9SRui Paulo 		return -1;
2100f05cddf9SRui Paulo 
21015b9c547cSRui Paulo 	if (wpas_wps_network_to_cred(ssid, &cred) < 0)
21025b9c547cSRui Paulo 		return -1;
21035b9c547cSRui Paulo 	ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2104f05cddf9SRui Paulo 	os_memset(&cred, 0, sizeof(cred));
21055b9c547cSRui Paulo 	return ret;
2106f05cddf9SRui Paulo }
2107f05cddf9SRui Paulo 
2108f05cddf9SRui Paulo 
wpas_wps_er_config(struct wpa_supplicant * wpa_s,const char * uuid,const char * pin,struct wps_new_ap_settings * settings)2109f05cddf9SRui Paulo int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2110f05cddf9SRui Paulo 		       const char *pin, struct wps_new_ap_settings *settings)
2111f05cddf9SRui Paulo {
21125b9c547cSRui Paulo 	u8 u[UUID_LEN], *use_uuid = NULL;
21135b9c547cSRui Paulo 	u8 addr[ETH_ALEN], *use_addr = NULL;
2114f05cddf9SRui Paulo 	struct wps_credential cred;
2115f05cddf9SRui Paulo 	size_t len;
2116f05cddf9SRui Paulo 
21175b9c547cSRui Paulo 	if (uuid_str2bin(uuid, u) == 0)
21185b9c547cSRui Paulo 		use_uuid = u;
21195b9c547cSRui Paulo 	else if (hwaddr_aton(uuid, addr) == 0)
21205b9c547cSRui Paulo 		use_addr = addr;
21215b9c547cSRui Paulo 	else
2122f05cddf9SRui Paulo 		return -1;
2123f05cddf9SRui Paulo 	if (settings->ssid_hex == NULL || settings->auth == NULL ||
2124f05cddf9SRui Paulo 	    settings->encr == NULL || settings->key_hex == NULL)
2125f05cddf9SRui Paulo 		return -1;
2126f05cddf9SRui Paulo 
2127f05cddf9SRui Paulo 	os_memset(&cred, 0, sizeof(cred));
2128f05cddf9SRui Paulo 	len = os_strlen(settings->ssid_hex);
2129f05cddf9SRui Paulo 	if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2130f05cddf9SRui Paulo 	    hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2131f05cddf9SRui Paulo 		return -1;
2132f05cddf9SRui Paulo 	cred.ssid_len = len / 2;
2133f05cddf9SRui Paulo 
2134f05cddf9SRui Paulo 	len = os_strlen(settings->key_hex);
2135f05cddf9SRui Paulo 	if ((len & 1) || len > 2 * sizeof(cred.key) ||
2136f05cddf9SRui Paulo 	    hexstr2bin(settings->key_hex, cred.key, len / 2))
2137f05cddf9SRui Paulo 		return -1;
2138f05cddf9SRui Paulo 	cred.key_len = len / 2;
2139f05cddf9SRui Paulo 
2140f05cddf9SRui Paulo 	if (os_strcmp(settings->auth, "OPEN") == 0)
2141f05cddf9SRui Paulo 		cred.auth_type = WPS_AUTH_OPEN;
2142f05cddf9SRui Paulo 	else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2143f05cddf9SRui Paulo 		cred.auth_type = WPS_AUTH_WPAPSK;
2144f05cddf9SRui Paulo 	else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2145f05cddf9SRui Paulo 		cred.auth_type = WPS_AUTH_WPA2PSK;
2146f05cddf9SRui Paulo 	else
2147f05cddf9SRui Paulo 		return -1;
2148f05cddf9SRui Paulo 
2149f05cddf9SRui Paulo 	if (os_strcmp(settings->encr, "NONE") == 0)
2150f05cddf9SRui Paulo 		cred.encr_type = WPS_ENCR_NONE;
21515b9c547cSRui Paulo #ifdef CONFIG_TESTING_OPTIONS
2152f05cddf9SRui Paulo 	else if (os_strcmp(settings->encr, "WEP") == 0)
2153f05cddf9SRui Paulo 		cred.encr_type = WPS_ENCR_WEP;
21545b9c547cSRui Paulo #endif /* CONFIG_TESTING_OPTIONS */
2155f05cddf9SRui Paulo 	else if (os_strcmp(settings->encr, "TKIP") == 0)
2156f05cddf9SRui Paulo 		cred.encr_type = WPS_ENCR_TKIP;
2157f05cddf9SRui Paulo 	else if (os_strcmp(settings->encr, "CCMP") == 0)
2158f05cddf9SRui Paulo 		cred.encr_type = WPS_ENCR_AES;
2159f05cddf9SRui Paulo 	else
2160f05cddf9SRui Paulo 		return -1;
2161f05cddf9SRui Paulo 
21625b9c547cSRui Paulo 	return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
21635b9c547cSRui Paulo 			     (const u8 *) pin, os_strlen(pin), &cred);
2164f05cddf9SRui Paulo }
2165f05cddf9SRui Paulo 
2166f05cddf9SRui Paulo 
2167f05cddf9SRui Paulo #ifdef CONFIG_WPS_NFC
wpas_wps_er_nfc_config_token(struct wpa_supplicant * wpa_s,int ndef,const char * uuid)2168f05cddf9SRui Paulo struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2169f05cddf9SRui Paulo 					     int ndef, const char *uuid)
2170f05cddf9SRui Paulo {
2171f05cddf9SRui Paulo 	struct wpabuf *ret;
21725b9c547cSRui Paulo 	u8 u[UUID_LEN], *use_uuid = NULL;
21735b9c547cSRui Paulo 	u8 addr[ETH_ALEN], *use_addr = NULL;
2174f05cddf9SRui Paulo 
2175f05cddf9SRui Paulo 	if (!wpa_s->wps_er)
2176f05cddf9SRui Paulo 		return NULL;
2177f05cddf9SRui Paulo 
21785b9c547cSRui Paulo 	if (uuid_str2bin(uuid, u) == 0)
21795b9c547cSRui Paulo 		use_uuid = u;
21805b9c547cSRui Paulo 	else if (hwaddr_aton(uuid, addr) == 0)
21815b9c547cSRui Paulo 		use_addr = addr;
21825b9c547cSRui Paulo 	else
2183f05cddf9SRui Paulo 		return NULL;
2184f05cddf9SRui Paulo 
21855b9c547cSRui Paulo 	ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
2186f05cddf9SRui Paulo 	if (ndef && ret) {
2187f05cddf9SRui Paulo 		struct wpabuf *tmp;
2188f05cddf9SRui Paulo 		tmp = ndef_build_wifi(ret);
2189f05cddf9SRui Paulo 		wpabuf_free(ret);
2190f05cddf9SRui Paulo 		if (tmp == NULL)
2191f05cddf9SRui Paulo 			return NULL;
2192f05cddf9SRui Paulo 		ret = tmp;
2193f05cddf9SRui Paulo 	}
2194f05cddf9SRui Paulo 
2195f05cddf9SRui Paulo 	return ret;
2196f05cddf9SRui Paulo }
2197f05cddf9SRui Paulo #endif /* CONFIG_WPS_NFC */
2198f05cddf9SRui Paulo 
2199f05cddf9SRui Paulo 
2200f05cddf9SRui Paulo static int callbacks_pending = 0;
2201f05cddf9SRui Paulo 
wpas_wps_terminate_cb(void * ctx)2202e28a4053SRui Paulo static void wpas_wps_terminate_cb(void *ctx)
2203e28a4053SRui Paulo {
2204e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2205f05cddf9SRui Paulo 	if (--callbacks_pending <= 0)
2206e28a4053SRui Paulo 		eloop_terminate();
2207e28a4053SRui Paulo }
2208e28a4053SRui Paulo #endif /* CONFIG_WPS_ER */
2209e28a4053SRui Paulo 
2210e28a4053SRui Paulo 
wpas_wps_terminate_pending(struct wpa_supplicant * wpa_s)2211e28a4053SRui Paulo int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2212e28a4053SRui Paulo {
2213e28a4053SRui Paulo #ifdef CONFIG_WPS_ER
2214e28a4053SRui Paulo 	if (wpa_s->wps_er) {
2215f05cddf9SRui Paulo 		callbacks_pending++;
2216e28a4053SRui Paulo 		wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2217e28a4053SRui Paulo 		wpa_s->wps_er = NULL;
2218e28a4053SRui Paulo 		return 1;
2219e28a4053SRui Paulo 	}
2220e28a4053SRui Paulo #endif /* CONFIG_WPS_ER */
2221e28a4053SRui Paulo 	return 0;
2222e28a4053SRui Paulo }
2223f05cddf9SRui Paulo 
2224f05cddf9SRui Paulo 
wpas_wps_update_config(struct wpa_supplicant * wpa_s)2225f05cddf9SRui Paulo void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2226f05cddf9SRui Paulo {
2227f05cddf9SRui Paulo 	struct wps_context *wps = wpa_s->wps;
2228f05cddf9SRui Paulo 
2229f05cddf9SRui Paulo 	if (wps == NULL)
2230f05cddf9SRui Paulo 		return;
2231f05cddf9SRui Paulo 
2232f05cddf9SRui Paulo 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2233f05cddf9SRui Paulo 		wps->config_methods = wps_config_methods_str2bin(
2234f05cddf9SRui Paulo 			wpa_s->conf->config_methods);
2235f05cddf9SRui Paulo 		if ((wps->config_methods &
2236f05cddf9SRui Paulo 		     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2237f05cddf9SRui Paulo 		    (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2238f05cddf9SRui Paulo 			wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2239f05cddf9SRui Paulo 				   "config methods are not allowed at the "
2240f05cddf9SRui Paulo 				   "same time");
2241f05cddf9SRui Paulo 			wps->config_methods &= ~WPS_CONFIG_LABEL;
2242f05cddf9SRui Paulo 		}
2243f05cddf9SRui Paulo 	}
2244f05cddf9SRui Paulo 	wps->config_methods = wps_fix_config_methods(wps->config_methods);
2245f05cddf9SRui Paulo 	wps->dev.config_methods = wps->config_methods;
2246f05cddf9SRui Paulo 
2247f05cddf9SRui Paulo 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2248f05cddf9SRui Paulo 		os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2249f05cddf9SRui Paulo 			  WPS_DEV_TYPE_LEN);
2250f05cddf9SRui Paulo 
2251f05cddf9SRui Paulo 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2252f05cddf9SRui Paulo 		wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2253f05cddf9SRui Paulo 		os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2254f05cddf9SRui Paulo 			  wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2255f05cddf9SRui Paulo 	}
2256f05cddf9SRui Paulo 
2257f05cddf9SRui Paulo 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2258f05cddf9SRui Paulo 		wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2259f05cddf9SRui Paulo 
2260f05cddf9SRui Paulo 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2261f05cddf9SRui Paulo 		wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2262f05cddf9SRui Paulo 
2263f05cddf9SRui Paulo 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2264f05cddf9SRui Paulo 		wpas_wps_set_uuid(wpa_s, wps);
2265f05cddf9SRui Paulo 
2266f05cddf9SRui Paulo 	if (wpa_s->conf->changed_parameters &
2267f05cddf9SRui Paulo 	    (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2268f05cddf9SRui Paulo 		/* Update pointers to make sure they refer current values */
2269f05cddf9SRui Paulo 		wps->dev.device_name = wpa_s->conf->device_name;
2270f05cddf9SRui Paulo 		wps->dev.manufacturer = wpa_s->conf->manufacturer;
2271f05cddf9SRui Paulo 		wps->dev.model_name = wpa_s->conf->model_name;
2272f05cddf9SRui Paulo 		wps->dev.model_number = wpa_s->conf->model_number;
2273f05cddf9SRui Paulo 		wps->dev.serial_number = wpa_s->conf->serial_number;
2274f05cddf9SRui Paulo 	}
2275f05cddf9SRui Paulo }
2276f05cddf9SRui Paulo 
2277f05cddf9SRui Paulo 
wpas_wps_update_mac_addr(struct wpa_supplicant * wpa_s)2278*c1d255d3SCy Schubert void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2279*c1d255d3SCy Schubert {
2280*c1d255d3SCy Schubert 	struct wps_context *wps;
2281*c1d255d3SCy Schubert 
2282*c1d255d3SCy Schubert 	wps = wpa_s->wps;
2283*c1d255d3SCy Schubert 	if (wps)
2284*c1d255d3SCy Schubert 		os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2285*c1d255d3SCy Schubert }
2286*c1d255d3SCy Schubert 
2287*c1d255d3SCy Schubert 
2288f05cddf9SRui Paulo #ifdef CONFIG_WPS_NFC
2289f05cddf9SRui Paulo 
22905b9c547cSRui Paulo #ifdef CONFIG_WPS_ER
22915b9c547cSRui Paulo static struct wpabuf *
wpas_wps_network_config_token(struct wpa_supplicant * wpa_s,int ndef,struct wpa_ssid * ssid)22925b9c547cSRui Paulo wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
22935b9c547cSRui Paulo 			      struct wpa_ssid *ssid)
22945b9c547cSRui Paulo {
22955b9c547cSRui Paulo 	struct wpabuf *ret;
22965b9c547cSRui Paulo 	struct wps_credential cred;
22975b9c547cSRui Paulo 
22985b9c547cSRui Paulo 	if (wpas_wps_network_to_cred(ssid, &cred) < 0)
22995b9c547cSRui Paulo 		return NULL;
23005b9c547cSRui Paulo 
23015b9c547cSRui Paulo 	ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
23025b9c547cSRui Paulo 
23035b9c547cSRui Paulo 	if (ndef && ret) {
23045b9c547cSRui Paulo 		struct wpabuf *tmp;
23055b9c547cSRui Paulo 		tmp = ndef_build_wifi(ret);
23065b9c547cSRui Paulo 		wpabuf_free(ret);
23075b9c547cSRui Paulo 		if (tmp == NULL)
23085b9c547cSRui Paulo 			return NULL;
23095b9c547cSRui Paulo 		ret = tmp;
23105b9c547cSRui Paulo 	}
23115b9c547cSRui Paulo 
23125b9c547cSRui Paulo 	return ret;
23135b9c547cSRui Paulo }
23145b9c547cSRui Paulo #endif /* CONFIG_WPS_ER */
23155b9c547cSRui Paulo 
23165b9c547cSRui Paulo 
wpas_wps_nfc_config_token(struct wpa_supplicant * wpa_s,int ndef,const char * id_str)23175b9c547cSRui Paulo struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
23185b9c547cSRui Paulo 					  int ndef, const char *id_str)
23195b9c547cSRui Paulo {
23205b9c547cSRui Paulo #ifdef CONFIG_WPS_ER
23215b9c547cSRui Paulo 	if (id_str) {
23225b9c547cSRui Paulo 		int id;
23235b9c547cSRui Paulo 		char *end = NULL;
23245b9c547cSRui Paulo 		struct wpa_ssid *ssid;
23255b9c547cSRui Paulo 
23265b9c547cSRui Paulo 		id = strtol(id_str, &end, 10);
23275b9c547cSRui Paulo 		if (end && *end)
23285b9c547cSRui Paulo 			return NULL;
23295b9c547cSRui Paulo 
23305b9c547cSRui Paulo 		ssid = wpa_config_get_network(wpa_s->conf, id);
23315b9c547cSRui Paulo 		if (ssid == NULL)
23325b9c547cSRui Paulo 			return NULL;
23335b9c547cSRui Paulo 		return wpas_wps_network_config_token(wpa_s, ndef, ssid);
23345b9c547cSRui Paulo 	}
23355b9c547cSRui Paulo #endif /* CONFIG_WPS_ER */
23365b9c547cSRui Paulo #ifdef CONFIG_AP
23375b9c547cSRui Paulo 	if (wpa_s->ap_iface)
23385b9c547cSRui Paulo 		return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
23395b9c547cSRui Paulo #endif /* CONFIG_AP */
23405b9c547cSRui Paulo 	return NULL;
23415b9c547cSRui Paulo }
23425b9c547cSRui Paulo 
23435b9c547cSRui Paulo 
wpas_wps_nfc_token(struct wpa_supplicant * wpa_s,int ndef)2344f05cddf9SRui Paulo struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2345f05cddf9SRui Paulo {
23465b9c547cSRui Paulo 	if (wpa_s->conf->wps_nfc_pw_from_config) {
23475b9c547cSRui Paulo 		return wps_nfc_token_build(ndef,
23485b9c547cSRui Paulo 					   wpa_s->conf->wps_nfc_dev_pw_id,
23495b9c547cSRui Paulo 					   wpa_s->conf->wps_nfc_dh_pubkey,
23505b9c547cSRui Paulo 					   wpa_s->conf->wps_nfc_dev_pw);
23515b9c547cSRui Paulo 	}
23525b9c547cSRui Paulo 
2353f05cddf9SRui Paulo 	return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2354f05cddf9SRui Paulo 				 &wpa_s->conf->wps_nfc_dh_pubkey,
2355f05cddf9SRui Paulo 				 &wpa_s->conf->wps_nfc_dh_privkey,
2356f05cddf9SRui Paulo 				 &wpa_s->conf->wps_nfc_dev_pw);
2357f05cddf9SRui Paulo }
2358f05cddf9SRui Paulo 
2359f05cddf9SRui Paulo 
wpas_wps_start_nfc(struct wpa_supplicant * wpa_s,const u8 * go_dev_addr,const u8 * bssid,const struct wpabuf * dev_pw,u16 dev_pw_id,int p2p_group,const u8 * peer_pubkey_hash,const u8 * ssid,size_t ssid_len,int freq)23605b9c547cSRui Paulo int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
23615b9c547cSRui Paulo 		       const u8 *bssid,
23625b9c547cSRui Paulo 		       const struct wpabuf *dev_pw, u16 dev_pw_id,
23635b9c547cSRui Paulo 		       int p2p_group, const u8 *peer_pubkey_hash,
23645b9c547cSRui Paulo 		       const u8 *ssid, size_t ssid_len, int freq)
2365f05cddf9SRui Paulo {
2366f05cddf9SRui Paulo 	struct wps_context *wps = wpa_s->wps;
2367f05cddf9SRui Paulo 	char pw[32 * 2 + 1];
2368f05cddf9SRui Paulo 
23695b9c547cSRui Paulo 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
23705b9c547cSRui Paulo 		dev_pw = wpa_s->conf->wps_nfc_dev_pw;
23715b9c547cSRui Paulo 		dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
23725b9c547cSRui Paulo 	}
23735b9c547cSRui Paulo 
2374f05cddf9SRui Paulo 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
23755b9c547cSRui Paulo 	    wpa_s->conf->wps_nfc_dh_privkey == NULL) {
23765b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
23775b9c547cSRui Paulo 			   "cannot start NFC-triggered connection");
2378f05cddf9SRui Paulo 		return -1;
23795b9c547cSRui Paulo 	}
23805b9c547cSRui Paulo 
23815b9c547cSRui Paulo 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
23825b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
23835b9c547cSRui Paulo 			   "cannot start NFC-triggered connection", dev_pw_id);
23845b9c547cSRui Paulo 		return -1;
23855b9c547cSRui Paulo 	}
2386f05cddf9SRui Paulo 
2387f05cddf9SRui Paulo 	dh5_free(wps->dh_ctx);
2388f05cddf9SRui Paulo 	wpabuf_free(wps->dh_pubkey);
2389f05cddf9SRui Paulo 	wpabuf_free(wps->dh_privkey);
2390f05cddf9SRui Paulo 	wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2391f05cddf9SRui Paulo 	wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2392f05cddf9SRui Paulo 	if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2393f05cddf9SRui Paulo 		wps->dh_ctx = NULL;
2394f05cddf9SRui Paulo 		wpabuf_free(wps->dh_pubkey);
2395f05cddf9SRui Paulo 		wps->dh_pubkey = NULL;
2396f05cddf9SRui Paulo 		wpabuf_free(wps->dh_privkey);
2397f05cddf9SRui Paulo 		wps->dh_privkey = NULL;
23985b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
2399f05cddf9SRui Paulo 		return -1;
2400f05cddf9SRui Paulo 	}
2401f05cddf9SRui Paulo 	wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
24025b9c547cSRui Paulo 	if (wps->dh_ctx == NULL) {
24035b9c547cSRui Paulo 		wpabuf_free(wps->dh_pubkey);
24045b9c547cSRui Paulo 		wps->dh_pubkey = NULL;
24055b9c547cSRui Paulo 		wpabuf_free(wps->dh_privkey);
24065b9c547cSRui Paulo 		wps->dh_privkey = NULL;
24075b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
2408f05cddf9SRui Paulo 		return -1;
24095b9c547cSRui Paulo 	}
2410f05cddf9SRui Paulo 
24115b9c547cSRui Paulo 	if (dev_pw) {
2412f05cddf9SRui Paulo 		wpa_snprintf_hex_uppercase(pw, sizeof(pw),
24135b9c547cSRui Paulo 					   wpabuf_head(dev_pw),
24145b9c547cSRui Paulo 					   wpabuf_len(dev_pw));
24155b9c547cSRui Paulo 	}
24165b9c547cSRui Paulo 	return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
24175b9c547cSRui Paulo 				     dev_pw ? pw : NULL,
24185b9c547cSRui Paulo 				     p2p_group, dev_pw_id, peer_pubkey_hash,
24195b9c547cSRui Paulo 				     ssid, ssid_len, freq);
2420f05cddf9SRui Paulo }
2421f05cddf9SRui Paulo 
2422f05cddf9SRui Paulo 
wpas_wps_use_cred(struct wpa_supplicant * wpa_s,struct wps_parse_attr * attr)2423f05cddf9SRui Paulo static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2424f05cddf9SRui Paulo 			     struct wps_parse_attr *attr)
2425f05cddf9SRui Paulo {
24265b9c547cSRui Paulo 	/*
24275b9c547cSRui Paulo 	 * Disable existing networks temporarily to allow the newly learned
24285b9c547cSRui Paulo 	 * credential to be preferred. Enable the temporarily disabled networks
24295b9c547cSRui Paulo 	 * after 10 seconds.
24305b9c547cSRui Paulo 	 */
24315b9c547cSRui Paulo 	wpas_wps_temp_disable(wpa_s, NULL);
24325b9c547cSRui Paulo 	eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
24335b9c547cSRui Paulo 			       NULL);
2434f05cddf9SRui Paulo 
2435f05cddf9SRui Paulo 	if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2436f05cddf9SRui Paulo 		return -1;
2437f05cddf9SRui Paulo 
2438f05cddf9SRui Paulo 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2439f05cddf9SRui Paulo 		return 0;
2440f05cddf9SRui Paulo 
24415b9c547cSRui Paulo 	if (attr->ap_channel) {
24425b9c547cSRui Paulo 		u16 chan = WPA_GET_BE16(attr->ap_channel);
2443f05cddf9SRui Paulo 		int freq = 0;
2444f05cddf9SRui Paulo 
2445f05cddf9SRui Paulo 		if (chan >= 1 && chan <= 13)
2446f05cddf9SRui Paulo 			freq = 2407 + 5 * chan;
2447f05cddf9SRui Paulo 		else if (chan == 14)
2448f05cddf9SRui Paulo 			freq = 2484;
2449f05cddf9SRui Paulo 		else if (chan >= 30)
2450f05cddf9SRui Paulo 			freq = 5000 + 5 * chan;
2451f05cddf9SRui Paulo 
2452f05cddf9SRui Paulo 		if (freq) {
24535b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
24545b9c547cSRui Paulo 				   chan, freq);
2455f05cddf9SRui Paulo 			wpa_s->after_wps = 5;
2456f05cddf9SRui Paulo 			wpa_s->wps_freq = freq;
2457f05cddf9SRui Paulo 		}
2458f05cddf9SRui Paulo 	}
24595b9c547cSRui Paulo 
24605b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
24615b9c547cSRui Paulo 		   "based on the received credential added");
24625b9c547cSRui Paulo 	wpa_s->normal_scans = 0;
24635b9c547cSRui Paulo 	wpa_supplicant_reinit_autoscan(wpa_s);
2464f05cddf9SRui Paulo 	wpa_s->disconnected = 0;
2465f05cddf9SRui Paulo 	wpa_s->reassociate = 1;
24665b9c547cSRui Paulo 
24675b9c547cSRui Paulo 	wpa_supplicant_cancel_sched_scan(wpa_s);
2468f05cddf9SRui Paulo 	wpa_supplicant_req_scan(wpa_s, 0, 0);
2469f05cddf9SRui Paulo 
2470f05cddf9SRui Paulo 	return 0;
2471f05cddf9SRui Paulo }
2472f05cddf9SRui Paulo 
2473f05cddf9SRui Paulo 
2474f05cddf9SRui Paulo #ifdef CONFIG_WPS_ER
wpas_wps_add_nfc_password_token(struct wpa_supplicant * wpa_s,struct wps_parse_attr * attr)2475f05cddf9SRui Paulo static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2476f05cddf9SRui Paulo 					   struct wps_parse_attr *attr)
2477f05cddf9SRui Paulo {
2478f05cddf9SRui Paulo 	return wps_registrar_add_nfc_password_token(
2479f05cddf9SRui Paulo 		wpa_s->wps->registrar, attr->oob_dev_password,
2480f05cddf9SRui Paulo 		attr->oob_dev_password_len);
2481f05cddf9SRui Paulo }
2482f05cddf9SRui Paulo #endif /* CONFIG_WPS_ER */
2483f05cddf9SRui Paulo 
2484f05cddf9SRui Paulo 
wpas_wps_nfc_tag_process(struct wpa_supplicant * wpa_s,const struct wpabuf * wps)2485f05cddf9SRui Paulo static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2486f05cddf9SRui Paulo 				    const struct wpabuf *wps)
2487f05cddf9SRui Paulo {
2488f05cddf9SRui Paulo 	struct wps_parse_attr attr;
2489f05cddf9SRui Paulo 
2490f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2491f05cddf9SRui Paulo 
2492f05cddf9SRui Paulo 	if (wps_parse_msg(wps, &attr)) {
2493f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2494f05cddf9SRui Paulo 		return -1;
2495f05cddf9SRui Paulo 	}
2496f05cddf9SRui Paulo 
2497f05cddf9SRui Paulo 	if (attr.num_cred)
2498f05cddf9SRui Paulo 		return wpas_wps_use_cred(wpa_s, &attr);
2499f05cddf9SRui Paulo 
2500f05cddf9SRui Paulo #ifdef CONFIG_WPS_ER
2501f05cddf9SRui Paulo 	if (attr.oob_dev_password)
2502f05cddf9SRui Paulo 		return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2503f05cddf9SRui Paulo #endif /* CONFIG_WPS_ER */
2504f05cddf9SRui Paulo 
2505f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2506f05cddf9SRui Paulo 	return -1;
2507f05cddf9SRui Paulo }
2508f05cddf9SRui Paulo 
2509f05cddf9SRui Paulo 
wpas_wps_nfc_tag_read(struct wpa_supplicant * wpa_s,const struct wpabuf * data,int forced_freq)2510f05cddf9SRui Paulo int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
25115b9c547cSRui Paulo 			  const struct wpabuf *data, int forced_freq)
2512f05cddf9SRui Paulo {
2513f05cddf9SRui Paulo 	const struct wpabuf *wps = data;
2514f05cddf9SRui Paulo 	struct wpabuf *tmp = NULL;
2515f05cddf9SRui Paulo 	int ret;
2516f05cddf9SRui Paulo 
2517f05cddf9SRui Paulo 	if (wpabuf_len(data) < 4)
2518f05cddf9SRui Paulo 		return -1;
2519f05cddf9SRui Paulo 
2520f05cddf9SRui Paulo 	if (*wpabuf_head_u8(data) != 0x10) {
2521f05cddf9SRui Paulo 		/* Assume this contains full NDEF record */
2522f05cddf9SRui Paulo 		tmp = ndef_parse_wifi(data);
2523f05cddf9SRui Paulo 		if (tmp == NULL) {
25245b9c547cSRui Paulo #ifdef CONFIG_P2P
25255b9c547cSRui Paulo 			tmp = ndef_parse_p2p(data);
25265b9c547cSRui Paulo 			if (tmp) {
25275b9c547cSRui Paulo 				ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
25285b9c547cSRui Paulo 							       forced_freq);
25295b9c547cSRui Paulo 				wpabuf_free(tmp);
25305b9c547cSRui Paulo 				return ret;
25315b9c547cSRui Paulo 			}
25325b9c547cSRui Paulo #endif /* CONFIG_P2P */
2533f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2534f05cddf9SRui Paulo 			return -1;
2535f05cddf9SRui Paulo 		}
2536f05cddf9SRui Paulo 		wps = tmp;
2537f05cddf9SRui Paulo 	}
2538f05cddf9SRui Paulo 
2539f05cddf9SRui Paulo 	ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2540f05cddf9SRui Paulo 	wpabuf_free(tmp);
2541f05cddf9SRui Paulo 	return ret;
2542f05cddf9SRui Paulo }
2543f05cddf9SRui Paulo 
2544f05cddf9SRui Paulo 
wpas_wps_nfc_handover_req(struct wpa_supplicant * wpa_s,int ndef)25455b9c547cSRui Paulo struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
25465b9c547cSRui Paulo 					  int ndef)
2547f05cddf9SRui Paulo {
25485b9c547cSRui Paulo 	struct wpabuf *ret;
25495b9c547cSRui Paulo 
25505b9c547cSRui Paulo 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
25515b9c547cSRui Paulo 	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
25525b9c547cSRui Paulo 			   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
25535b9c547cSRui Paulo 		return NULL;
25545b9c547cSRui Paulo 
25555b9c547cSRui Paulo 	ret = wps_build_nfc_handover_req(wpa_s->wps,
25565b9c547cSRui Paulo 					 wpa_s->conf->wps_nfc_dh_pubkey);
25575b9c547cSRui Paulo 
25585b9c547cSRui Paulo 	if (ndef && ret) {
25595b9c547cSRui Paulo 		struct wpabuf *tmp;
25605b9c547cSRui Paulo 		tmp = ndef_build_wifi(ret);
25615b9c547cSRui Paulo 		wpabuf_free(ret);
25625b9c547cSRui Paulo 		if (tmp == NULL)
25635b9c547cSRui Paulo 			return NULL;
25645b9c547cSRui Paulo 		ret = tmp;
25655b9c547cSRui Paulo 	}
25665b9c547cSRui Paulo 
25675b9c547cSRui Paulo 	return ret;
2568f05cddf9SRui Paulo }
2569f05cddf9SRui Paulo 
2570f05cddf9SRui Paulo 
25715b9c547cSRui Paulo #ifdef CONFIG_WPS_NFC
25725b9c547cSRui Paulo 
25735b9c547cSRui Paulo static struct wpabuf *
wpas_wps_er_nfc_handover_sel(struct wpa_supplicant * wpa_s,int ndef,const char * uuid)25745b9c547cSRui Paulo wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
25755b9c547cSRui Paulo 			     const char *uuid)
2576f05cddf9SRui Paulo {
25775b9c547cSRui Paulo #ifdef CONFIG_WPS_ER
25785b9c547cSRui Paulo 	struct wpabuf *ret;
25795b9c547cSRui Paulo 	u8 u[UUID_LEN], *use_uuid = NULL;
25805b9c547cSRui Paulo 	u8 addr[ETH_ALEN], *use_addr = NULL;
25815b9c547cSRui Paulo 	struct wps_context *wps = wpa_s->wps;
25825b9c547cSRui Paulo 
25835b9c547cSRui Paulo 	if (wps == NULL)
25845b9c547cSRui Paulo 		return NULL;
25855b9c547cSRui Paulo 
25865b9c547cSRui Paulo 	if (uuid == NULL)
25875b9c547cSRui Paulo 		return NULL;
25885b9c547cSRui Paulo 	if (uuid_str2bin(uuid, u) == 0)
25895b9c547cSRui Paulo 		use_uuid = u;
25905b9c547cSRui Paulo 	else if (hwaddr_aton(uuid, addr) == 0)
25915b9c547cSRui Paulo 		use_addr = addr;
25925b9c547cSRui Paulo 	else
25935b9c547cSRui Paulo 		return NULL;
25945b9c547cSRui Paulo 
25955b9c547cSRui Paulo 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
25965b9c547cSRui Paulo 		if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
25975b9c547cSRui Paulo 				   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2598f05cddf9SRui Paulo 			return NULL;
2599f05cddf9SRui Paulo 	}
2600f05cddf9SRui Paulo 
26015b9c547cSRui Paulo 	wpas_wps_nfc_clear(wps);
26025b9c547cSRui Paulo 	wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
26035b9c547cSRui Paulo 	wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
26045b9c547cSRui Paulo 	wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
26055b9c547cSRui Paulo 	if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
26065b9c547cSRui Paulo 		wpas_wps_nfc_clear(wps);
26075b9c547cSRui Paulo 		return NULL;
26085b9c547cSRui Paulo 	}
2609f05cddf9SRui Paulo 
26105b9c547cSRui Paulo 	ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
26115b9c547cSRui Paulo 				      use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
26125b9c547cSRui Paulo 	if (ndef && ret) {
26135b9c547cSRui Paulo 		struct wpabuf *tmp;
26145b9c547cSRui Paulo 		tmp = ndef_build_wifi(ret);
26155b9c547cSRui Paulo 		wpabuf_free(ret);
26165b9c547cSRui Paulo 		if (tmp == NULL)
26175b9c547cSRui Paulo 			return NULL;
26185b9c547cSRui Paulo 		ret = tmp;
26195b9c547cSRui Paulo 	}
26205b9c547cSRui Paulo 
26215b9c547cSRui Paulo 	return ret;
26225b9c547cSRui Paulo #else /* CONFIG_WPS_ER */
26235b9c547cSRui Paulo 	return NULL;
26245b9c547cSRui Paulo #endif /* CONFIG_WPS_ER */
26255b9c547cSRui Paulo }
26265b9c547cSRui Paulo #endif /* CONFIG_WPS_NFC */
26275b9c547cSRui Paulo 
26285b9c547cSRui Paulo 
wpas_wps_nfc_handover_sel(struct wpa_supplicant * wpa_s,int ndef,int cr,const char * uuid)26295b9c547cSRui Paulo struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
26305b9c547cSRui Paulo 					  int ndef, int cr, const char *uuid)
2631f05cddf9SRui Paulo {
26325b9c547cSRui Paulo 	struct wpabuf *ret;
26335b9c547cSRui Paulo 	if (!cr)
26345b9c547cSRui Paulo 		return NULL;
26355b9c547cSRui Paulo 	ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
26365b9c547cSRui Paulo 	if (ret)
26375b9c547cSRui Paulo 		return ret;
26385b9c547cSRui Paulo 	return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
2639f05cddf9SRui Paulo }
2640f05cddf9SRui Paulo 
2641f05cddf9SRui Paulo 
wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant * wpa_s,const struct wpabuf * data)26425b9c547cSRui Paulo static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2643f05cddf9SRui Paulo 					const struct wpabuf *data)
2644f05cddf9SRui Paulo {
2645f05cddf9SRui Paulo 	struct wpabuf *wps;
26465b9c547cSRui Paulo 	int ret = -1;
26475b9c547cSRui Paulo 	u16 wsc_len;
26485b9c547cSRui Paulo 	const u8 *pos;
26495b9c547cSRui Paulo 	struct wpabuf msg;
26505b9c547cSRui Paulo 	struct wps_parse_attr attr;
26515b9c547cSRui Paulo 	u16 dev_pw_id;
26525b9c547cSRui Paulo 	const u8 *bssid = NULL;
26535b9c547cSRui Paulo 	int freq = 0;
2654f05cddf9SRui Paulo 
2655f05cddf9SRui Paulo 	wps = ndef_parse_wifi(data);
2656f05cddf9SRui Paulo 	if (wps == NULL)
2657f05cddf9SRui Paulo 		return -1;
2658f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2659f05cddf9SRui Paulo 		   "payload from NFC connection handover");
26605b9c547cSRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
26615b9c547cSRui Paulo 	if (wpabuf_len(wps) < 2) {
26625b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
26635b9c547cSRui Paulo 			   "Message");
26645b9c547cSRui Paulo 		goto out;
26655b9c547cSRui Paulo 	}
26665b9c547cSRui Paulo 	pos = wpabuf_head(wps);
26675b9c547cSRui Paulo 	wsc_len = WPA_GET_BE16(pos);
26685b9c547cSRui Paulo 	if (wsc_len > wpabuf_len(wps) - 2) {
26695b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
26705b9c547cSRui Paulo 			   "in Wi-Fi Handover Select Message", wsc_len);
26715b9c547cSRui Paulo 		goto out;
26725b9c547cSRui Paulo 	}
26735b9c547cSRui Paulo 	pos += 2;
2674f05cddf9SRui Paulo 
26755b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG,
26765b9c547cSRui Paulo 		    "WPS: WSC attributes in Wi-Fi Handover Select Message",
26775b9c547cSRui Paulo 		    pos, wsc_len);
26785b9c547cSRui Paulo 	if (wsc_len < wpabuf_len(wps) - 2) {
26795b9c547cSRui Paulo 		wpa_hexdump(MSG_DEBUG,
26805b9c547cSRui Paulo 			    "WPS: Ignore extra data after WSC attributes",
26815b9c547cSRui Paulo 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
26825b9c547cSRui Paulo 	}
26835b9c547cSRui Paulo 
26845b9c547cSRui Paulo 	wpabuf_set(&msg, pos, wsc_len);
26855b9c547cSRui Paulo 	ret = wps_parse_msg(&msg, &attr);
26865b9c547cSRui Paulo 	if (ret < 0) {
26875b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
26885b9c547cSRui Paulo 			   "Wi-Fi Handover Select Message");
26895b9c547cSRui Paulo 		goto out;
26905b9c547cSRui Paulo 	}
26915b9c547cSRui Paulo 
26925b9c547cSRui Paulo 	if (attr.oob_dev_password == NULL ||
26935b9c547cSRui Paulo 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
26945b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
26955b9c547cSRui Paulo 			   "included in Wi-Fi Handover Select Message");
26965b9c547cSRui Paulo 		ret = -1;
26975b9c547cSRui Paulo 		goto out;
26985b9c547cSRui Paulo 	}
26995b9c547cSRui Paulo 
27005b9c547cSRui Paulo 	if (attr.ssid == NULL) {
27015b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
27025b9c547cSRui Paulo 			   "Select Message");
27035b9c547cSRui Paulo 		ret = -1;
27045b9c547cSRui Paulo 		goto out;
27055b9c547cSRui Paulo 	}
27065b9c547cSRui Paulo 
27075b9c547cSRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
27085b9c547cSRui Paulo 
27095b9c547cSRui Paulo 	if (attr.mac_addr) {
27105b9c547cSRui Paulo 		bssid = attr.mac_addr;
27115b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
27125b9c547cSRui Paulo 			   MAC2STR(bssid));
27135b9c547cSRui Paulo 	}
27145b9c547cSRui Paulo 
27155b9c547cSRui Paulo 	if (attr.rf_bands)
27165b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
27175b9c547cSRui Paulo 
27185b9c547cSRui Paulo 	if (attr.ap_channel) {
27195b9c547cSRui Paulo 		u16 chan = WPA_GET_BE16(attr.ap_channel);
27205b9c547cSRui Paulo 
27215b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
27225b9c547cSRui Paulo 
27235b9c547cSRui Paulo 		if (chan >= 1 && chan <= 13 &&
27245b9c547cSRui Paulo 		    (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
27255b9c547cSRui Paulo 			freq = 2407 + 5 * chan;
27265b9c547cSRui Paulo 		else if (chan == 14 &&
27275b9c547cSRui Paulo 			 (attr.rf_bands == NULL ||
27285b9c547cSRui Paulo 			  *attr.rf_bands & WPS_RF_24GHZ))
27295b9c547cSRui Paulo 			freq = 2484;
27305b9c547cSRui Paulo 		else if (chan >= 30 &&
27315b9c547cSRui Paulo 			 (attr.rf_bands == NULL ||
27325b9c547cSRui Paulo 			  *attr.rf_bands & WPS_RF_50GHZ))
27335b9c547cSRui Paulo 			freq = 5000 + 5 * chan;
2734*c1d255d3SCy Schubert 		else if (chan >= 1 && chan <= 6 &&
2735325151a3SRui Paulo 			 (attr.rf_bands == NULL ||
2736325151a3SRui Paulo 			  *attr.rf_bands & WPS_RF_60GHZ))
2737325151a3SRui Paulo 			freq = 56160 + 2160 * chan;
27385b9c547cSRui Paulo 
27395b9c547cSRui Paulo 		if (freq) {
27405b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
27415b9c547cSRui Paulo 				   "WPS: AP indicated channel %u -> %u MHz",
27425b9c547cSRui Paulo 				   chan, freq);
27435b9c547cSRui Paulo 		}
27445b9c547cSRui Paulo 	}
27455b9c547cSRui Paulo 
27465b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
27475b9c547cSRui Paulo 		    attr.oob_dev_password, attr.oob_dev_password_len);
27485b9c547cSRui Paulo 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
27495b9c547cSRui Paulo 				 WPS_OOB_PUBKEY_HASH_LEN);
27505b9c547cSRui Paulo 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
27515b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
27525b9c547cSRui Paulo 			   "%u in Wi-Fi Handover Select Message", dev_pw_id);
27535b9c547cSRui Paulo 		ret = -1;
27545b9c547cSRui Paulo 		goto out;
27555b9c547cSRui Paulo 	}
27565b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
27575b9c547cSRui Paulo 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
27585b9c547cSRui Paulo 
27595b9c547cSRui Paulo 	ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
27605b9c547cSRui Paulo 				 attr.oob_dev_password,
27615b9c547cSRui Paulo 				 attr.ssid, attr.ssid_len, freq);
27625b9c547cSRui Paulo 
27635b9c547cSRui Paulo out:
27645b9c547cSRui Paulo 	wpabuf_free(wps);
27655b9c547cSRui Paulo 	return ret;
27665b9c547cSRui Paulo }
27675b9c547cSRui Paulo 
27685b9c547cSRui Paulo 
wpas_wps_nfc_report_handover(struct wpa_supplicant * wpa_s,const struct wpabuf * req,const struct wpabuf * sel)27695b9c547cSRui Paulo int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
27705b9c547cSRui Paulo 				 const struct wpabuf *req,
27715b9c547cSRui Paulo 				 const struct wpabuf *sel)
27725b9c547cSRui Paulo {
27735b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
27745b9c547cSRui Paulo 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
27755b9c547cSRui Paulo 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
27765b9c547cSRui Paulo 	return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
27775b9c547cSRui Paulo }
27785b9c547cSRui Paulo 
27795b9c547cSRui Paulo 
wpas_er_wps_nfc_report_handover(struct wpa_supplicant * wpa_s,const struct wpabuf * req,const struct wpabuf * sel)27805b9c547cSRui Paulo int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
27815b9c547cSRui Paulo 				    const struct wpabuf *req,
27825b9c547cSRui Paulo 				    const struct wpabuf *sel)
27835b9c547cSRui Paulo {
27845b9c547cSRui Paulo 	struct wpabuf *wps;
27855b9c547cSRui Paulo 	int ret = -1;
27865b9c547cSRui Paulo 	u16 wsc_len;
27875b9c547cSRui Paulo 	const u8 *pos;
27885b9c547cSRui Paulo 	struct wpabuf msg;
27895b9c547cSRui Paulo 	struct wps_parse_attr attr;
27905b9c547cSRui Paulo 	u16 dev_pw_id;
27915b9c547cSRui Paulo 
27925b9c547cSRui Paulo 	/*
27935b9c547cSRui Paulo 	 * Enrollee/station is always initiator of the NFC connection handover,
27945b9c547cSRui Paulo 	 * so use the request message here to find Enrollee public key hash.
27955b9c547cSRui Paulo 	 */
27965b9c547cSRui Paulo 	wps = ndef_parse_wifi(req);
27975b9c547cSRui Paulo 	if (wps == NULL)
27985b9c547cSRui Paulo 		return -1;
27995b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
28005b9c547cSRui Paulo 		   "payload from NFC connection handover");
28015b9c547cSRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
28025b9c547cSRui Paulo 	if (wpabuf_len(wps) < 2) {
28035b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
28045b9c547cSRui Paulo 			   "Message");
28055b9c547cSRui Paulo 		goto out;
28065b9c547cSRui Paulo 	}
28075b9c547cSRui Paulo 	pos = wpabuf_head(wps);
28085b9c547cSRui Paulo 	wsc_len = WPA_GET_BE16(pos);
28095b9c547cSRui Paulo 	if (wsc_len > wpabuf_len(wps) - 2) {
28105b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
28115b9c547cSRui Paulo 			   "in rt Wi-Fi Handover Request Message", wsc_len);
28125b9c547cSRui Paulo 		goto out;
28135b9c547cSRui Paulo 	}
28145b9c547cSRui Paulo 	pos += 2;
28155b9c547cSRui Paulo 
28165b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG,
28175b9c547cSRui Paulo 		    "WPS: WSC attributes in Wi-Fi Handover Request Message",
28185b9c547cSRui Paulo 		    pos, wsc_len);
28195b9c547cSRui Paulo 	if (wsc_len < wpabuf_len(wps) - 2) {
28205b9c547cSRui Paulo 		wpa_hexdump(MSG_DEBUG,
28215b9c547cSRui Paulo 			    "WPS: Ignore extra data after WSC attributes",
28225b9c547cSRui Paulo 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
28235b9c547cSRui Paulo 	}
28245b9c547cSRui Paulo 
28255b9c547cSRui Paulo 	wpabuf_set(&msg, pos, wsc_len);
28265b9c547cSRui Paulo 	ret = wps_parse_msg(&msg, &attr);
28275b9c547cSRui Paulo 	if (ret < 0) {
28285b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
28295b9c547cSRui Paulo 			   "Wi-Fi Handover Request Message");
28305b9c547cSRui Paulo 		goto out;
28315b9c547cSRui Paulo 	}
28325b9c547cSRui Paulo 
28335b9c547cSRui Paulo 	if (attr.oob_dev_password == NULL ||
28345b9c547cSRui Paulo 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
28355b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
28365b9c547cSRui Paulo 			   "included in Wi-Fi Handover Request Message");
28375b9c547cSRui Paulo 		ret = -1;
28385b9c547cSRui Paulo 		goto out;
28395b9c547cSRui Paulo 	}
28405b9c547cSRui Paulo 
28415b9c547cSRui Paulo 	if (attr.uuid_e == NULL) {
28425b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
28435b9c547cSRui Paulo 			   "Handover Request Message");
28445b9c547cSRui Paulo 		ret = -1;
28455b9c547cSRui Paulo 		goto out;
28465b9c547cSRui Paulo 	}
28475b9c547cSRui Paulo 
28485b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
28495b9c547cSRui Paulo 
28505b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
28515b9c547cSRui Paulo 		    attr.oob_dev_password, attr.oob_dev_password_len);
28525b9c547cSRui Paulo 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
28535b9c547cSRui Paulo 				 WPS_OOB_PUBKEY_HASH_LEN);
28545b9c547cSRui Paulo 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
28555b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
28565b9c547cSRui Paulo 			   "%u in Wi-Fi Handover Request Message", dev_pw_id);
28575b9c547cSRui Paulo 		ret = -1;
28585b9c547cSRui Paulo 		goto out;
28595b9c547cSRui Paulo 	}
28605b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
28615b9c547cSRui Paulo 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
28625b9c547cSRui Paulo 
28635b9c547cSRui Paulo 	ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
28645b9c547cSRui Paulo 					     attr.oob_dev_password,
28655b9c547cSRui Paulo 					     DEV_PW_NFC_CONNECTION_HANDOVER,
28665b9c547cSRui Paulo 					     NULL, 0, 1);
28675b9c547cSRui Paulo 
28685b9c547cSRui Paulo out:
28695b9c547cSRui Paulo 	wpabuf_free(wps);
2870f05cddf9SRui Paulo 	return ret;
2871f05cddf9SRui Paulo }
2872f05cddf9SRui Paulo 
2873f05cddf9SRui Paulo #endif /* CONFIG_WPS_NFC */
2874f05cddf9SRui Paulo 
2875f05cddf9SRui Paulo 
wpas_wps_dump_ap_info(struct wpa_supplicant * wpa_s)2876f05cddf9SRui Paulo static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2877f05cddf9SRui Paulo {
2878f05cddf9SRui Paulo 	size_t i;
28795b9c547cSRui Paulo 	struct os_reltime now;
2880f05cddf9SRui Paulo 
2881f05cddf9SRui Paulo 	if (wpa_debug_level > MSG_DEBUG)
2882f05cddf9SRui Paulo 		return;
2883f05cddf9SRui Paulo 
2884f05cddf9SRui Paulo 	if (wpa_s->wps_ap == NULL)
2885f05cddf9SRui Paulo 		return;
2886f05cddf9SRui Paulo 
28875b9c547cSRui Paulo 	os_get_reltime(&now);
2888f05cddf9SRui Paulo 
2889f05cddf9SRui Paulo 	for (i = 0; i < wpa_s->num_wps_ap; i++) {
2890f05cddf9SRui Paulo 		struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2891*c1d255d3SCy Schubert 		struct wpa_bssid_ignore *e = wpa_bssid_ignore_get(wpa_s,
2892*c1d255d3SCy Schubert 								  ap->bssid);
2893f05cddf9SRui Paulo 
2894f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2895*c1d255d3SCy Schubert 			   "tries=%d last_attempt=%d sec ago bssid_ignore=%d",
2896f05cddf9SRui Paulo 			   (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2897f05cddf9SRui Paulo 			   ap->last_attempt.sec > 0 ?
2898f05cddf9SRui Paulo 			   (int) now.sec - (int) ap->last_attempt.sec : -1,
2899f05cddf9SRui Paulo 			   e ? e->count : 0);
2900f05cddf9SRui Paulo 	}
2901f05cddf9SRui Paulo }
2902f05cddf9SRui Paulo 
2903f05cddf9SRui Paulo 
wpas_wps_get_ap_info(struct wpa_supplicant * wpa_s,const u8 * bssid)2904f05cddf9SRui Paulo static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2905f05cddf9SRui Paulo 						 const u8 *bssid)
2906f05cddf9SRui Paulo {
2907f05cddf9SRui Paulo 	size_t i;
2908f05cddf9SRui Paulo 
2909f05cddf9SRui Paulo 	if (wpa_s->wps_ap == NULL)
2910f05cddf9SRui Paulo 		return NULL;
2911f05cddf9SRui Paulo 
2912f05cddf9SRui Paulo 	for (i = 0; i < wpa_s->num_wps_ap; i++) {
2913f05cddf9SRui Paulo 		struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2914f05cddf9SRui Paulo 		if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2915f05cddf9SRui Paulo 			return ap;
2916f05cddf9SRui Paulo 	}
2917f05cddf9SRui Paulo 
2918f05cddf9SRui Paulo 	return NULL;
2919f05cddf9SRui Paulo }
2920f05cddf9SRui Paulo 
2921f05cddf9SRui Paulo 
wpas_wps_update_ap_info_bss(struct wpa_supplicant * wpa_s,struct wpa_scan_res * res)2922f05cddf9SRui Paulo static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2923f05cddf9SRui Paulo 					struct wpa_scan_res *res)
2924f05cddf9SRui Paulo {
2925f05cddf9SRui Paulo 	struct wpabuf *wps;
2926f05cddf9SRui Paulo 	enum wps_ap_info_type type;
2927f05cddf9SRui Paulo 	struct wps_ap_info *ap;
2928325151a3SRui Paulo 	int r, pbc_active;
2929325151a3SRui Paulo 	const u8 *uuid;
2930f05cddf9SRui Paulo 
2931f05cddf9SRui Paulo 	if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2932f05cddf9SRui Paulo 		return;
2933f05cddf9SRui Paulo 
2934f05cddf9SRui Paulo 	wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2935f05cddf9SRui Paulo 	if (wps == NULL)
2936f05cddf9SRui Paulo 		return;
2937f05cddf9SRui Paulo 
2938f05cddf9SRui Paulo 	r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2939f05cddf9SRui Paulo 	if (r == 2)
2940f05cddf9SRui Paulo 		type = WPS_AP_SEL_REG_OUR;
2941f05cddf9SRui Paulo 	else if (r == 1)
2942f05cddf9SRui Paulo 		type = WPS_AP_SEL_REG;
2943f05cddf9SRui Paulo 	else
2944f05cddf9SRui Paulo 		type = WPS_AP_NOT_SEL_REG;
2945f05cddf9SRui Paulo 
2946325151a3SRui Paulo 	uuid = wps_get_uuid_e(wps);
2947325151a3SRui Paulo 	pbc_active = wps_is_selected_pbc_registrar(wps);
2948f05cddf9SRui Paulo 
2949f05cddf9SRui Paulo 	ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2950f05cddf9SRui Paulo 	if (ap) {
2951f05cddf9SRui Paulo 		if (ap->type != type) {
2952f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2953f05cddf9SRui Paulo 				   " changed type %d -> %d",
2954f05cddf9SRui Paulo 				   MAC2STR(res->bssid), ap->type, type);
2955f05cddf9SRui Paulo 			ap->type = type;
2956f05cddf9SRui Paulo 			if (type != WPS_AP_NOT_SEL_REG)
2957*c1d255d3SCy Schubert 				wpa_bssid_ignore_del(wpa_s, ap->bssid);
2958f05cddf9SRui Paulo 		}
2959325151a3SRui Paulo 		ap->pbc_active = pbc_active;
2960325151a3SRui Paulo 		if (uuid)
2961325151a3SRui Paulo 			os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2962325151a3SRui Paulo 		goto out;
2963f05cddf9SRui Paulo 	}
2964f05cddf9SRui Paulo 
2965f05cddf9SRui Paulo 	ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2966f05cddf9SRui Paulo 			      sizeof(struct wps_ap_info));
2967f05cddf9SRui Paulo 	if (ap == NULL)
2968325151a3SRui Paulo 		goto out;
2969f05cddf9SRui Paulo 
2970f05cddf9SRui Paulo 	wpa_s->wps_ap = ap;
2971f05cddf9SRui Paulo 	ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2972f05cddf9SRui Paulo 	wpa_s->num_wps_ap++;
2973f05cddf9SRui Paulo 
2974f05cddf9SRui Paulo 	os_memset(ap, 0, sizeof(*ap));
2975f05cddf9SRui Paulo 	os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2976f05cddf9SRui Paulo 	ap->type = type;
2977325151a3SRui Paulo 	ap->pbc_active = pbc_active;
2978325151a3SRui Paulo 	if (uuid)
2979325151a3SRui Paulo 		os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2980f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2981f05cddf9SRui Paulo 		   MAC2STR(ap->bssid), ap->type);
2982325151a3SRui Paulo 
2983325151a3SRui Paulo out:
2984325151a3SRui Paulo 	wpabuf_free(wps);
2985f05cddf9SRui Paulo }
2986f05cddf9SRui Paulo 
2987f05cddf9SRui Paulo 
wpas_wps_update_ap_info(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)2988f05cddf9SRui Paulo void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2989f05cddf9SRui Paulo 			     struct wpa_scan_results *scan_res)
2990f05cddf9SRui Paulo {
2991f05cddf9SRui Paulo 	size_t i;
2992f05cddf9SRui Paulo 
2993f05cddf9SRui Paulo 	for (i = 0; i < scan_res->num; i++)
2994f05cddf9SRui Paulo 		wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2995f05cddf9SRui Paulo 
2996f05cddf9SRui Paulo 	wpas_wps_dump_ap_info(wpa_s);
2997f05cddf9SRui Paulo }
2998f05cddf9SRui Paulo 
2999f05cddf9SRui Paulo 
wpas_wps_notify_assoc(struct wpa_supplicant * wpa_s,const u8 * bssid)3000f05cddf9SRui Paulo void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
3001f05cddf9SRui Paulo {
3002f05cddf9SRui Paulo 	struct wps_ap_info *ap;
30035b9c547cSRui Paulo 
30045b9c547cSRui Paulo 	wpa_s->after_wps = 0;
30055b9c547cSRui Paulo 
3006f05cddf9SRui Paulo 	if (!wpa_s->wps_ap_iter)
3007f05cddf9SRui Paulo 		return;
3008f05cddf9SRui Paulo 	ap = wpas_wps_get_ap_info(wpa_s, bssid);
3009f05cddf9SRui Paulo 	if (ap == NULL)
3010f05cddf9SRui Paulo 		return;
3011f05cddf9SRui Paulo 	ap->tries++;
30125b9c547cSRui Paulo 	os_get_reltime(&ap->last_attempt);
3013f05cddf9SRui Paulo }
3014