blob: de7dc981209c36a8e5be32f1faf912ac8da6d77f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant / WPS integration
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "uuid.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070014#include "crypto/random.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "crypto/dh_group5.h"
16#include "common/ieee802_11_defs.h"
17#include "common/ieee802_11_common.h"
18#include "common/wpa_common.h"
19#include "common/wpa_ctrl.h"
20#include "eap_common/eap_wsc_common.h"
21#include "eap_peer/eap.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070022#include "eapol_supp/eapol_supp_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "rsn_supp/wpa.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070024#include "wps/wps_attr_parse.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "config.h"
26#include "wpa_supplicant_i.h"
27#include "driver_i.h"
28#include "notify.h"
Hai Shalom60840252021-02-19 19:02:11 -080029#include "bssid_ignore.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "bss.h"
31#include "scan.h"
32#include "ap.h"
33#include "p2p/p2p.h"
34#include "p2p_supplicant.h"
35#include "wps_supplicant.h"
36
37
38#ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
39#define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40#endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
41
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070042/*
43 * The minimum time in seconds before trying to associate to a WPS PIN AP that
44 * does not have Selected Registrar TRUE.
45 */
46#ifndef WPS_PIN_TIME_IGNORE_SEL_REG
47#define WPS_PIN_TIME_IGNORE_SEL_REG 5
48#endif /* WPS_PIN_TIME_IGNORE_SEL_REG */
49
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
51static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
52
53
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070054static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
55{
56 os_free(wpa_s->wps_ap);
57 wpa_s->wps_ap = NULL;
58 wpa_s->num_wps_ap = 0;
59 wpa_s->wps_ap_iter = 0;
60}
61
62
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070063static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
64{
65 struct wpa_supplicant *wpa_s = eloop_ctx;
66 int use_fast_assoc = timeout_ctx != NULL;
67
68 wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
69 if (!use_fast_assoc ||
70 wpa_supplicant_fast_associate(wpa_s) != 1)
71 wpa_supplicant_req_scan(wpa_s, 0, 0);
72}
73
74
75static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
76{
77 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
78 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
79}
80
81
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000082static struct wpabuf * wpas_wps_get_wps_ie(struct wpa_bss *bss)
83{
84 /* Return the latest receive WPS IE from the AP regardless of whether
85 * it was from a Beacon frame or Probe Response frame to avoid using
86 * stale information. */
87 if (bss->beacon_newer)
88 return wpa_bss_get_vendor_ie_multi_beacon(bss,
89 WPS_IE_VENDOR_TYPE);
90 return wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
91}
92
93
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
95{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080096 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
97 return 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 if (!wpa_s->wps_success &&
100 wpa_s->current_ssid &&
101 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
102 const u8 *bssid = wpa_s->bssid;
103 if (is_zero_ether_addr(bssid))
104 bssid = wpa_s->pending_bssid;
105
106 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
107 " did not succeed - continue trying to find "
108 "suitable AP", MAC2STR(bssid));
Hai Shalom60840252021-02-19 19:02:11 -0800109 wpa_bssid_ignore_add(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110
111 wpa_supplicant_deauthenticate(wpa_s,
112 WLAN_REASON_DEAUTH_LEAVING);
113 wpa_s->reassociate = 1;
114 wpa_supplicant_req_scan(wpa_s,
Hai Shalom60840252021-02-19 19:02:11 -0800115 wpa_s->bssid_ignore_cleared ? 5 : 0, 0);
116 wpa_s->bssid_ignore_cleared = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117 return 1;
118 }
119
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700120 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
122 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
123 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
124
125 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
126 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
127 int disabled = wpa_s->current_ssid->disabled;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800128 unsigned int freq = wpa_s->assoc_freq;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700129 struct wpa_bss *bss;
130 struct wpa_ssid *ssid = NULL;
131 int use_fast_assoc = 0;
132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800134 "try to associate with the received credential "
135 "(freq=%u)", freq);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800136 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137 wpa_supplicant_deauthenticate(wpa_s,
138 WLAN_REASON_DEAUTH_LEAVING);
139 if (disabled) {
140 wpa_printf(MSG_DEBUG, "WPS: Current network is "
141 "disabled - wait for user to enable");
142 return 1;
143 }
144 wpa_s->after_wps = 5;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800145 wpa_s->wps_freq = freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800146 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147 wpa_s->reassociate = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700148
149 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
150 "without a new scan can be used");
151 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
152 if (bss) {
153 struct wpabuf *wps;
154 struct wps_parse_attr attr;
155
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000156 wps = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700157 if (wps && wps_parse_msg(wps, &attr) == 0 &&
158 attr.wps_state &&
159 *attr.wps_state == WPS_STATE_CONFIGURED)
160 use_fast_assoc = 1;
161 wpabuf_free(wps);
162 }
163
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700164 /*
165 * Complete the next step from an eloop timeout to allow pending
166 * driver events related to the disconnection to be processed
167 * first. This makes it less likely for disconnection event to
168 * cause problems with the following connection.
169 */
170 wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
171 wpas_wps_assoc_with_cred_cancel(wpa_s);
172 eloop_register_timeout(0, 10000,
173 wpas_wps_assoc_with_cred, wpa_s,
174 use_fast_assoc ? (void *) 1 :
175 (void *) 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176 return 1;
177 }
178
179 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
180 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
181 "for external credential processing");
182 wpas_clear_wps(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800183 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 wpa_supplicant_deauthenticate(wpa_s,
185 WLAN_REASON_DEAUTH_LEAVING);
186 return 1;
187 }
188
189 return 0;
190}
191
192
193static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
194 struct wpa_ssid *ssid,
195 const struct wps_credential *cred)
196{
197 struct wpa_driver_capa capa;
198 struct wpa_bss *bss;
199 const u8 *ie;
200 struct wpa_ie_data adv;
201 int wpa2 = 0, ccmp = 0;
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700202 enum wpa_driver_if_type iftype;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203
204 /*
205 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
206 * case they are configured for mixed mode operation (WPA+WPA2 and
207 * TKIP+CCMP). Try to use scan results to figure out whether the AP
208 * actually supports stronger security and select that if the client
209 * has support for it, too.
210 */
211
212 if (wpa_drv_get_capa(wpa_s, &capa))
213 return; /* Unknown what driver supports */
214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800215 if (ssid->ssid == NULL)
216 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700218 if (!bss)
219 bss = wpa_bss_get(wpa_s, wpa_s->bssid,
220 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221 if (bss == NULL) {
222 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
223 "table - use credential as-is");
224 return;
225 }
226
227 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
228
229 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
230 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
231 wpa2 = 1;
232 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
233 ccmp = 1;
234 } else {
235 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
236 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
237 adv.pairwise_cipher & WPA_CIPHER_CCMP)
238 ccmp = 1;
239 }
240
241 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
242 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
243 /*
244 * TODO: This could be the initial AP configuration and the
245 * Beacon contents could change shortly. Should request a new
246 * scan and delay addition of the network until the updated
247 * scan results are available.
248 */
249 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
250 "support - use credential as-is");
251 return;
252 }
253
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700254 iftype = ssid->p2p_group ? WPA_IF_P2P_CLIENT : WPA_IF_STATION;
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
257 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700258 (capa.key_mgmt_iftype[iftype] &
259 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
261 "based on scan results");
262 if (wpa_s->conf->ap_scan == 1)
263 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
264 else
265 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
266 }
267
268 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
269 (ssid->proto & WPA_PROTO_WPA) &&
270 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
271 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
272 "based on scan results");
273 if (wpa_s->conf->ap_scan == 1)
274 ssid->proto |= WPA_PROTO_RSN;
275 else
276 ssid->proto = WPA_PROTO_RSN;
277 }
278}
279
280
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700281static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
282 struct wpa_ssid *new_ssid)
283{
284 struct wpa_ssid *ssid, *next;
285
286 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
287 ssid = next, next = ssid ? ssid->next : NULL) {
288 /*
289 * new_ssid has already been added to the list in
290 * wpas_wps_add_network(), so skip it.
291 */
292 if (ssid == new_ssid)
293 continue;
294
295 if (ssid->bssid_set || new_ssid->bssid_set) {
296 if (ssid->bssid_set != new_ssid->bssid_set)
297 continue;
298 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
299 0)
300 continue;
301 }
302
303 /* compare SSID */
304 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
305 continue;
306
307 if (ssid->ssid && new_ssid->ssid) {
308 if (os_memcmp(ssid->ssid, new_ssid->ssid,
309 ssid->ssid_len) != 0)
310 continue;
311 } else if (ssid->ssid || new_ssid->ssid)
312 continue;
313
314 /* compare security parameters */
315 if (ssid->auth_alg != new_ssid->auth_alg ||
316 ssid->key_mgmt != new_ssid->key_mgmt ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800317 (ssid->group_cipher != new_ssid->group_cipher &&
318 !(ssid->group_cipher & new_ssid->group_cipher &
319 WPA_CIPHER_CCMP)))
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700320 continue;
321
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700322 /*
323 * Some existing WPS APs will send two creds in case they are
324 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
325 * Try to merge these two creds if they are received in the same
326 * M8 message.
327 */
328 if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
329 wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
330 if (new_ssid->passphrase && ssid->passphrase &&
331 os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
332 0) {
333 wpa_printf(MSG_DEBUG,
334 "WPS: M8 Creds with different passphrase - do not merge");
335 continue;
336 }
337
338 if (new_ssid->psk_set &&
339 (!ssid->psk_set ||
340 os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
341 wpa_printf(MSG_DEBUG,
342 "WPS: M8 Creds with different PSK - do not merge");
343 continue;
344 }
345
346 if ((new_ssid->passphrase && !ssid->passphrase) ||
347 (!new_ssid->passphrase && ssid->passphrase)) {
348 wpa_printf(MSG_DEBUG,
349 "WPS: M8 Creds with different passphrase/PSK type - do not merge");
350 continue;
351 }
352
353 wpa_printf(MSG_DEBUG,
354 "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
355 new_ssid->proto |= ssid->proto;
356 new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
357 } else {
358 /*
359 * proto and pairwise_cipher difference matter for
360 * non-mixed-mode creds.
361 */
362 if (ssid->proto != new_ssid->proto ||
363 ssid->pairwise_cipher != new_ssid->pairwise_cipher)
364 continue;
365 }
366
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700367 /* Remove the duplicated older network entry. */
368 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
369 wpas_notify_network_removed(wpa_s, ssid);
370 wpa_config_remove_network(wpa_s->conf, ssid->id);
371 }
372}
373
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375static int wpa_supplicant_wps_cred(void *ctx,
376 const struct wps_credential *cred)
377{
378 struct wpa_supplicant *wpa_s = ctx;
379 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 u16 auth_type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 int registrar = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Hai Shaloma20dcd72022-02-04 13:43:00 -0800384 bool add_sae;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385
386 if ((wpa_s->conf->wps_cred_processing == 1 ||
387 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
388 size_t blen = cred->cred_attr_len * 2 + 1;
389 char *buf = os_malloc(blen);
390 if (buf) {
391 wpa_snprintf_hex(buf, blen,
392 cred->cred_attr, cred->cred_attr_len);
393 wpa_msg(wpa_s, MSG_INFO, "%s%s",
394 WPS_EVENT_CRED_RECEIVED, buf);
395 os_free(buf);
396 }
397
398 wpas_notify_wps_credential(wpa_s, cred);
399 } else
400 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
401
402 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
403 cred->cred_attr, cred->cred_attr_len);
404
405 if (wpa_s->conf->wps_cred_processing == 1)
406 return 0;
407
408 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
409 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
410 cred->auth_type);
411 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
412 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
413 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
414 cred->key, cred->key_len);
415 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
416 MAC2STR(cred->mac_addr));
417
418 auth_type = cred->auth_type;
419 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
420 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
421 "auth_type into WPA2PSK");
422 auth_type = WPS_AUTH_WPA2PSK;
423 }
424
425 if (auth_type != WPS_AUTH_OPEN &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 auth_type != WPS_AUTH_WPAPSK &&
427 auth_type != WPS_AUTH_WPA2PSK) {
428 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
429 "unsupported authentication type 0x%x",
430 auth_type);
431 return 0;
432 }
433
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800434 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
435 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
436 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
437 "invalid Network Key length %lu",
438 (unsigned long) cred->key_len);
439 return -1;
440 }
441 }
442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
444 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
445 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800446#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 if (ssid->eap.identity &&
448 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
449 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
450 WSC_ID_REGISTRAR_LEN) == 0)
451 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800452#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 os_free(ssid->eap.identity);
454 ssid->eap.identity = NULL;
455 ssid->eap.identity_len = 0;
456 os_free(ssid->eap.phase1);
457 ssid->eap.phase1 = NULL;
458 os_free(ssid->eap.eap_methods);
459 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700460 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700462 ssid->bssid_set = 0;
463 }
464 ssid->disabled_until.sec = 0;
465 ssid->disabled_until.usec = 0;
466 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 } else {
468 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
469 "received credential");
470 ssid = wpa_config_add_network(wpa_s->conf);
471 if (ssid == NULL)
472 return -1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700473 if (wpa_s->current_ssid) {
474 /*
475 * Should the GO issue multiple credentials for some
476 * reason, each credential should be marked as a
477 * temporary P2P group similarly to the one that gets
478 * marked as such based on the pre-configured values
479 * used for the WPS network block.
480 */
481 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
482 ssid->temporary = wpa_s->current_ssid->temporary;
483 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 wpas_notify_network_added(wpa_s, ssid);
485 }
486
487 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700488 ssid->wps_run = wpa_s->wps_run;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489
490 os_free(ssid->ssid);
491 ssid->ssid = os_malloc(cred->ssid_len);
492 if (ssid->ssid) {
493 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
494 ssid->ssid_len = cred->ssid_len;
495 }
496
497 switch (cred->encr_type) {
498 case WPS_ENCR_NONE:
499 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 case WPS_ENCR_TKIP:
Hai Shalomb755a2a2020-04-23 21:49:02 -0700501 ssid->pairwise_cipher = WPA_CIPHER_TKIP | WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 break;
503 case WPS_ENCR_AES:
504 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800505 if (wpa_s->drv_capa_known &&
506 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
507 ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
508 ssid->group_cipher |= WPA_CIPHER_GCMP;
509 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700510 if (wpa_s->drv_capa_known &&
511 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256)) {
512 ssid->pairwise_cipher |= WPA_CIPHER_GCMP_256;
513 ssid->group_cipher |= WPA_CIPHER_GCMP_256;
514 }
515 if (wpa_s->drv_capa_known &&
516 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256)) {
517 ssid->pairwise_cipher |= WPA_CIPHER_CCMP_256;
518 ssid->group_cipher |= WPA_CIPHER_CCMP_256;
519 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520 break;
521 }
522
523 switch (auth_type) {
524 case WPS_AUTH_OPEN:
525 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
526 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
527 ssid->proto = 0;
528#ifdef CONFIG_WPS_REG_DISABLE_OPEN
529 if (registrar) {
530 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
531 "id=%d - Credentials for an open "
532 "network disabled by default - use "
533 "'select_network %d' to enable",
534 ssid->id, ssid->id);
535 ssid->disabled = 1;
536 }
537#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
538 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 case WPS_AUTH_WPAPSK:
540 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
541 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700542 ssid->proto = WPA_PROTO_WPA | WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 case WPS_AUTH_WPA2PSK:
545 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
546 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800547 add_sae = wpa_s->conf->wps_cred_add_sae;
548#ifdef CONFIG_P2P
549 if (ssid->p2p_group && is_p2p_6ghz_capable(wpa_s->global->p2p))
550 add_sae = true;
551#endif /* CONFIG_P2P */
552 if (add_sae && cred->key_len != 2 * PMK_LEN) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700553 ssid->auth_alg = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700554 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
Hai Shalom021b0b52019-04-10 11:17:58 -0700555 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
Hai Shalom021b0b52019-04-10 11:17:58 -0700556 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557 ssid->proto = WPA_PROTO_RSN;
558 break;
559 }
560
Hai Shalom021b0b52019-04-10 11:17:58 -0700561 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 if (cred->key_len == 2 * PMK_LEN) {
563 if (hexstr2bin((const char *) cred->key, ssid->psk,
564 PMK_LEN)) {
565 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
566 "Key");
567 return -1;
568 }
569 ssid->psk_set = 1;
570 ssid->export_keys = 1;
571 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
572 os_free(ssid->passphrase);
573 ssid->passphrase = os_malloc(cred->key_len + 1);
574 if (ssid->passphrase == NULL)
575 return -1;
576 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
577 ssid->passphrase[cred->key_len] = '\0';
578 wpa_config_update_psk(ssid);
579 ssid->export_keys = 1;
580 } else {
581 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
582 "length %lu",
583 (unsigned long) cred->key_len);
584 return -1;
585 }
586 }
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700587 ssid->priority = wpa_s->conf->wps_priority;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588
589 wpas_wps_security_workaround(wpa_s, ssid, cred);
590
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700591 wpas_wps_remove_dup_network(wpa_s, ssid);
592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593#ifndef CONFIG_NO_CONFIG_WRITE
594 if (wpa_s->conf->update_config &&
595 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
596 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
597 return -1;
598 }
599#endif /* CONFIG_NO_CONFIG_WRITE */
600
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700601 if (ssid->priority)
602 wpa_config_update_prio_list(wpa_s->conf);
603
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800604 /*
605 * Optimize the post-WPS scan based on the channel used during
606 * the provisioning in case EAP-Failure is not received.
607 */
608 wpa_s->after_wps = 5;
609 wpa_s->wps_freq = wpa_s->assoc_freq;
610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 return 0;
612}
613
614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
616 struct wps_event_m2d *m2d)
617{
618 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
619 "dev_password_id=%d config_error=%d",
620 m2d->dev_password_id, m2d->config_error);
621 wpas_notify_wps_event_m2d(wpa_s, m2d);
622#ifdef CONFIG_P2P
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800623 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
624 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 "dev_password_id=%d config_error=%d",
626 m2d->dev_password_id, m2d->config_error);
627 }
628 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
629 /*
630 * Notify P2P from eloop timeout to avoid issues with the
631 * interface getting removed while processing a message.
632 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700633 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 NULL);
635 }
636#endif /* CONFIG_P2P */
637}
638
639
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700640static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
641{
642 struct wpa_supplicant *wpa_s = eloop_ctx;
643 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
644 wpas_clear_wps(wpa_s);
645}
646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700647
648static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
649 struct wps_event_fail *fail)
650{
651 if (fail->error_indication > 0 &&
652 fail->error_indication < NUM_WPS_EI_VALUES) {
653 wpa_msg(wpa_s, MSG_INFO,
654 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
655 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700656 wps_ei_str(fail->error_indication));
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800657 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
658 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659 "msg=%d config_error=%d reason=%d (%s)",
660 fail->msg, fail->config_error,
661 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700662 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 } else {
664 wpa_msg(wpa_s, MSG_INFO,
665 WPS_EVENT_FAIL "msg=%d config_error=%d",
666 fail->msg, fail->config_error);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800667 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
668 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 "msg=%d config_error=%d",
670 fail->msg, fail->config_error);
671 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700672
673 /*
674 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
675 */
676 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
677 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
678 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700681 wpas_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682}
683
684
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800685static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
686
687static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
688{
689 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800690 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800691
692 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
693
694 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
695 if (ssid->disabled_for_connect && ssid->disabled) {
696 ssid->disabled_for_connect = 0;
697 ssid->disabled = 0;
698 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800699 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800700 }
701 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800702
703 if (changed) {
704#ifndef CONFIG_NO_CONFIG_WRITE
705 if (wpa_s->conf->update_config &&
706 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
707 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
708 "configuration");
709 }
710#endif /* CONFIG_NO_CONFIG_WRITE */
711 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800712}
713
714
715static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
716{
717 struct wpa_supplicant *wpa_s = eloop_ctx;
718 /* Enable the networks disabled during wpas_wps_reassoc */
719 wpas_wps_reenable_networks(wpa_s);
720}
721
722
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800723int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
724{
725 return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
726 wpa_s, NULL);
727}
728
729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
731{
732 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
733 wpa_s->wps_success = 1;
734 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700735 if (wpa_s->current_ssid)
736 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
Hai Shalom899fcc72020-10-19 14:38:18 -0700737 wpa_s->consecutive_conn_failures = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800738
739 /*
740 * Enable the networks disabled during wpas_wps_reassoc after 10
741 * seconds. The 10 seconds timer is to allow the data connection to be
742 * formed before allowing other networks to be selected.
743 */
744 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
745 NULL);
746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748}
749
750
751static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
752 struct wps_event_er_ap *ap)
753{
754 char uuid_str[100];
755 char dev_type[WPS_DEV_TYPE_BUFSIZE];
756
757 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
758 if (ap->pri_dev_type)
759 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
760 sizeof(dev_type));
761 else
762 dev_type[0] = '\0';
763
764 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
765 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
766 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
767 ap->friendly_name ? ap->friendly_name : "",
768 ap->manufacturer ? ap->manufacturer : "",
769 ap->model_description ? ap->model_description : "",
770 ap->model_name ? ap->model_name : "",
771 ap->manufacturer_url ? ap->manufacturer_url : "",
772 ap->model_url ? ap->model_url : "");
773}
774
775
776static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
777 struct wps_event_er_ap *ap)
778{
779 char uuid_str[100];
780 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
781 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
782}
783
784
785static void wpa_supplicant_wps_event_er_enrollee_add(
786 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
787{
788 char uuid_str[100];
789 char dev_type[WPS_DEV_TYPE_BUFSIZE];
790
791 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
792 if (enrollee->pri_dev_type)
793 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
794 sizeof(dev_type));
795 else
796 dev_type[0] = '\0';
797
798 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
799 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
800 "|%s|%s|%s|%s|%s|",
801 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
802 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
803 enrollee->dev_name ? enrollee->dev_name : "",
804 enrollee->manufacturer ? enrollee->manufacturer : "",
805 enrollee->model_name ? enrollee->model_name : "",
806 enrollee->model_number ? enrollee->model_number : "",
807 enrollee->serial_number ? enrollee->serial_number : "");
808}
809
810
811static void wpa_supplicant_wps_event_er_enrollee_remove(
812 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
813{
814 char uuid_str[100];
815 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
816 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
817 uuid_str, MAC2STR(enrollee->mac_addr));
818}
819
820
821static void wpa_supplicant_wps_event_er_ap_settings(
822 struct wpa_supplicant *wpa_s,
823 struct wps_event_er_ap_settings *ap_settings)
824{
825 char uuid_str[100];
826 char key_str[65];
827 const struct wps_credential *cred = ap_settings->cred;
828
829 key_str[0] = '\0';
830 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
831 if (cred->key_len >= 8 && cred->key_len <= 64) {
832 os_memcpy(key_str, cred->key, cred->key_len);
833 key_str[cred->key_len] = '\0';
834 }
835 }
836
837 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
838 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
839 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
840 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
841 "key=%s",
842 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
843 cred->auth_type, cred->encr_type, key_str);
844}
845
846
847static void wpa_supplicant_wps_event_er_set_sel_reg(
848 struct wpa_supplicant *wpa_s,
849 struct wps_event_er_set_selected_registrar *ev)
850{
851 char uuid_str[100];
852
853 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
854 switch (ev->state) {
855 case WPS_ER_SET_SEL_REG_START:
856 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
857 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
858 "sel_reg_config_methods=0x%x",
859 uuid_str, ev->sel_reg, ev->dev_passwd_id,
860 ev->sel_reg_config_methods);
861 break;
862 case WPS_ER_SET_SEL_REG_DONE:
863 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
864 "uuid=%s state=DONE", uuid_str);
865 break;
866 case WPS_ER_SET_SEL_REG_FAILED:
867 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
868 "uuid=%s state=FAILED", uuid_str);
869 break;
870 }
871}
872
873
874static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
875 union wps_event_data *data)
876{
877 struct wpa_supplicant *wpa_s = ctx;
878 switch (event) {
879 case WPS_EV_M2D:
880 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
881 break;
882 case WPS_EV_FAIL:
883 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
884 break;
885 case WPS_EV_SUCCESS:
886 wpa_supplicant_wps_event_success(wpa_s);
887 break;
888 case WPS_EV_PWD_AUTH_FAIL:
889#ifdef CONFIG_AP
890 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
891 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
892#endif /* CONFIG_AP */
893 break;
894 case WPS_EV_PBC_OVERLAP:
895 break;
896 case WPS_EV_PBC_TIMEOUT:
897 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700898 case WPS_EV_PBC_ACTIVE:
899 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
900 break;
901 case WPS_EV_PBC_DISABLE:
902 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
903 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 case WPS_EV_ER_AP_ADD:
905 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
906 break;
907 case WPS_EV_ER_AP_REMOVE:
908 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
909 break;
910 case WPS_EV_ER_ENROLLEE_ADD:
911 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
912 &data->enrollee);
913 break;
914 case WPS_EV_ER_ENROLLEE_REMOVE:
915 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
916 &data->enrollee);
917 break;
918 case WPS_EV_ER_AP_SETTINGS:
919 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
920 &data->ap_settings);
921 break;
922 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
923 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
924 &data->set_sel_reg);
925 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800926 case WPS_EV_AP_PIN_SUCCESS:
927 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 }
929}
930
931
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700932static int wpa_supplicant_wps_rf_band(void *ctx)
933{
934 struct wpa_supplicant *wpa_s = ctx;
935
936 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
937 return 0;
938
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700939 return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
940 (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700941}
942
943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
945{
946 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
947 eap_is_wps_pin_enrollee(&ssid->eap))
948 return WPS_REQ_ENROLLEE;
949 else
950 return WPS_REQ_REGISTRAR;
951}
952
953
954static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
955{
956 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700957 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
958
Dmitry Shmidt051af732013-10-22 13:52:46 -0700959 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700960 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700961
Jouni Malinen75ecf522011-06-27 15:19:46 -0700962 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800964 /* Enable the networks disabled during wpas_wps_reassoc */
965 wpas_wps_reenable_networks(wpa_s);
966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800968 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969
970 /* Remove any existing WPS network from configuration */
971 ssid = wpa_s->conf->ssid;
972 while (ssid) {
973 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
974 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800975 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700976 wpa_supplicant_deauthenticate(
977 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 }
979 id = ssid->id;
980 remove_ssid = ssid;
981 } else
982 id = -1;
983 ssid = ssid->next;
984 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700985 if (prev_current == remove_ssid) {
986 wpa_sm_set_config(wpa_s->wpa, NULL);
987 eapol_sm_notify_config(wpa_s->eapol, NULL,
988 NULL);
989 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 wpas_notify_network_removed(wpa_s, remove_ssid);
991 wpa_config_remove_network(wpa_s->conf, id);
992 }
993 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700994
995 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996}
997
998
999static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
1000{
1001 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001002 union wps_event_data data;
1003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
1005 "out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001006 os_memset(&data, 0, sizeof(data));
1007 data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
1008 data.fail.error_indication = WPS_EI_NO_ERROR;
1009 /*
1010 * Call wpas_notify_wps_event_fail() directly instead of through
1011 * wpa_supplicant_wps_event() which would end up registering unnecessary
1012 * timeouts (those are only for the case where the failure happens
1013 * during an EAP-WSC exchange).
1014 */
1015 wpas_notify_wps_event_fail(wpa_s, &data.fail);
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001016 wpa_s->supp_pbc_active = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 wpas_clear_wps(wpa_s);
1018}
1019
1020
1021static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001022 int registrar, const u8 *dev_addr,
1023 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024{
1025 struct wpa_ssid *ssid;
1026
1027 ssid = wpa_config_add_network(wpa_s->conf);
1028 if (ssid == NULL)
1029 return NULL;
1030 wpas_notify_network_added(wpa_s, ssid);
1031 wpa_config_set_network_defaults(ssid);
1032 ssid->temporary = 1;
1033 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
1034 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
1035 wpa_config_set(ssid, "identity", registrar ?
1036 "\"" WSC_ID_REGISTRAR "\"" :
1037 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1038 wpas_notify_network_removed(wpa_s, ssid);
1039 wpa_config_remove_network(wpa_s->conf, ssid->id);
1040 return NULL;
1041 }
1042
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001043#ifdef CONFIG_P2P
1044 if (dev_addr)
1045 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1046#endif /* CONFIG_P2P */
1047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 if (bssid) {
1049#ifndef CONFIG_P2P
1050 struct wpa_bss *bss;
1051 int count = 0;
1052#endif /* CONFIG_P2P */
1053
1054 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1055 ssid->bssid_set = 1;
1056
1057 /*
1058 * Note: With P2P, the SSID may change at the time the WPS
1059 * provisioning is started, so better not filter the AP based
1060 * on the current SSID in the scan results.
1061 */
1062#ifndef CONFIG_P2P
1063 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1064 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
1065 continue;
1066
1067 os_free(ssid->ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001068 ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 if (ssid->ssid == NULL)
1070 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 ssid->ssid_len = bss->ssid_len;
1072 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1073 "scan results",
1074 ssid->ssid, ssid->ssid_len);
1075 count++;
1076 }
1077
1078 if (count > 1) {
1079 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1080 "for the AP; use wildcard");
1081 os_free(ssid->ssid);
1082 ssid->ssid = NULL;
1083 ssid->ssid_len = 0;
1084 }
1085#endif /* CONFIG_P2P */
1086 }
1087
1088 return ssid;
1089}
1090
1091
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001092static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1093 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094{
1095 struct wpa_ssid *ssid;
1096
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001097 if (wpa_s->current_ssid) {
1098 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001099 wpa_supplicant_deauthenticate(
1100 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001101 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 /* Mark all other networks disabled and trigger reassociation */
1104 ssid = wpa_s->conf->ssid;
1105 while (ssid) {
1106 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001107 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001108 /*
1109 * In case the network object corresponds to a persistent group
1110 * then do not send out network disabled signal. In addition,
1111 * do not change disabled status of persistent network objects
1112 * from 2 to 1 should we connect to another network.
1113 */
1114 if (was_disabled != 2) {
1115 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001116 if (was_disabled != ssid->disabled) {
1117 if (ssid->disabled)
1118 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001119 wpas_notify_network_enabled_changed(wpa_s,
1120 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001121 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 ssid = ssid->next;
1124 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001125}
1126
1127
1128static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001129 struct wpa_ssid *selected, const u8 *bssid,
1130 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001131{
1132 struct wpa_bss *bss;
1133
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001134 wpa_s->wps_run++;
1135 if (wpa_s->wps_run == 0)
1136 wpa_s->wps_run++;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001137 wpa_s->after_wps = 0;
1138 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001139 if (freq) {
1140 wpa_s->after_wps = 5;
1141 wpa_s->wps_freq = freq;
1142 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001143 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1144 if (bss && bss->freq > 0) {
1145 wpa_s->known_wps_freq = 1;
1146 wpa_s->wps_freq = bss->freq;
1147 }
1148 }
1149
1150 wpas_wps_temp_disable(wpa_s, selected);
1151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 wpa_s->disconnected = 0;
1153 wpa_s->reassociate = 1;
1154 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001155 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 wpa_s->wps_success = 0;
Hai Shalom60840252021-02-19 19:02:11 -08001157 wpa_s->bssid_ignore_cleared = false;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001158
1159 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 wpa_supplicant_req_scan(wpa_s, 0, 0);
1161}
1162
1163
1164int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
Hai Shalom021b0b52019-04-10 11:17:58 -07001165 int p2p_group, int multi_ap_backhaul_sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166{
1167 struct wpa_ssid *ssid;
Hai Shalom021b0b52019-04-10 11:17:58 -07001168 char phase1[32];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001169
1170#ifdef CONFIG_AP
1171 if (wpa_s->ap_iface) {
1172 wpa_printf(MSG_DEBUG,
1173 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1174 return -1;
1175 }
1176#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001178 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 if (ssid == NULL)
1180 return -1;
1181 ssid->temporary = 1;
1182 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001183 /*
1184 * When starting a regular WPS process (not P2P group formation)
1185 * the registrar/final station can be either AP or PCP
1186 * so use a "don't care" value for the pbss flag.
1187 */
1188 if (!p2p_group)
1189 ssid->pbss = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190#ifdef CONFIG_P2P
1191 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1192 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1193 if (ssid->ssid) {
1194 ssid->ssid_len = wpa_s->go_params->ssid_len;
1195 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1196 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001197 if (wpa_s->go_params->freq > 56160) {
1198 /* P2P in 60 GHz uses PBSS */
1199 ssid->pbss = 1;
1200 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001201 if (wpa_s->go_params->edmg &&
1202 wpas_p2p_try_edmg_channel(wpa_s,
1203 wpa_s->go_params) == 0)
1204 ssid->enable_edmg = 1;
1205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1207 "SSID", ssid->ssid, ssid->ssid_len);
1208 }
1209 }
1210#endif /* CONFIG_P2P */
Hai Shalom021b0b52019-04-10 11:17:58 -07001211 os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
1212 multi_ap_backhaul_sta ? " multi_ap=1" : "");
1213 if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001214 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 if (wpa_s->wps_fragment_size)
1216 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Hai Shalom021b0b52019-04-10 11:17:58 -07001217 if (multi_ap_backhaul_sta)
1218 ssid->multi_ap_backhaul_sta = 1;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001219 wpa_s->supp_pbc_active = true;
1220 wpa_s->wps_overlap = false;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001221 wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1223 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001224 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 return 0;
1226}
1227
1228
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001229static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1230 const u8 *dev_addr, const u8 *bssid,
1231 const char *pin, int p2p_group, u16 dev_pw_id,
1232 const u8 *peer_pubkey_hash,
1233 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234{
1235 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001236 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001238 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001240#ifdef CONFIG_AP
1241 if (wpa_s->ap_iface) {
1242 wpa_printf(MSG_DEBUG,
1243 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1244 return -1;
1245 }
1246#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001248 if (bssid && is_zero_ether_addr(bssid))
1249 bssid = NULL;
1250 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1251 if (ssid == NULL) {
1252 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001254 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 ssid->temporary = 1;
1256 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001257 /*
1258 * When starting a regular WPS process (not P2P group formation)
1259 * the registrar/final station can be either AP or PCP
1260 * so use a "don't care" value for the pbss flag.
1261 */
1262 if (!p2p_group)
1263 ssid->pbss = 2;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001264 if (ssid_val) {
1265 ssid->ssid = os_malloc(ssid_len);
1266 if (ssid->ssid) {
1267 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1268 ssid->ssid_len = ssid_len;
1269 }
1270 }
1271 if (peer_pubkey_hash) {
1272 os_memcpy(hash, " pkhash=", 8);
1273 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1274 peer_pubkey_hash,
1275 WPS_OOB_PUBKEY_HASH_LEN);
1276 } else {
1277 hash[0] = '\0';
1278 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279#ifdef CONFIG_P2P
1280 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001281 os_free(ssid->ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1283 if (ssid->ssid) {
1284 ssid->ssid_len = wpa_s->go_params->ssid_len;
1285 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1286 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001287 if (wpa_s->go_params->freq > 56160) {
1288 /* P2P in 60 GHz uses PBSS */
1289 ssid->pbss = 1;
1290 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001291 if (wpa_s->go_params->edmg &&
1292 wpas_p2p_try_edmg_channel(wpa_s,
1293 wpa_s->go_params) == 0)
1294 ssid->enable_edmg = 1;
1295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1297 "SSID", ssid->ssid, ssid->ssid_len);
1298 }
1299 }
1300#endif /* CONFIG_P2P */
1301 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001302 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1303 pin, dev_pw_id, hash);
1304 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1305 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1306 dev_pw_id, hash);
1307 } else {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001308 if (wps_generate_pin(&rpin) < 0) {
1309 wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1310 return -1;
1311 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001312 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1313 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001315 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1316 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001317 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001318 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001319
1320 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER)
1321 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_PIN_ACTIVE);
1322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 if (wpa_s->wps_fragment_size)
1324 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1325 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1326 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001327 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001328 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 return rpin;
1330}
1331
1332
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001333int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1334 const char *pin, int p2p_group, u16 dev_pw_id)
1335{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001336 os_get_reltime(&wpa_s->wps_pin_start_time);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001337 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1338 dev_pw_id, NULL, NULL, 0, 0);
1339}
1340
1341
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001342void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1343{
1344 union wps_event_data data;
1345
1346 os_memset(&data, 0, sizeof(data));
1347 data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1348 data.fail.error_indication = WPS_EI_NO_ERROR;
1349 /*
1350 * Call wpas_notify_wps_event_fail() directly instead of through
1351 * wpa_supplicant_wps_event() which would end up registering unnecessary
1352 * timeouts (those are only for the case where the failure happens
1353 * during an EAP-WSC exchange).
1354 */
1355 wpas_notify_wps_event_fail(wpa_s, &data.fail);
1356}
1357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358/* Cancel the wps pbc/pin requests */
1359int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1360{
1361#ifdef CONFIG_AP
1362 if (wpa_s->ap_iface) {
1363 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1364 return wpa_supplicant_ap_wps_cancel(wpa_s);
1365 }
1366#endif /* CONFIG_AP */
1367
Dmitry Shmidt04949592012-07-19 12:16:46 -07001368 if (wpa_s->wpa_state == WPA_SCANNING ||
1369 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1371 wpa_supplicant_cancel_scan(wpa_s);
1372 wpas_clear_wps(wpa_s);
1373 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1374 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1375 "deauthenticate");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001376 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 wpa_supplicant_deauthenticate(wpa_s,
1378 WLAN_REASON_DEAUTH_LEAVING);
1379 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001380 } else {
1381 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001382 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001383 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1384 0)
1385 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 }
1387
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001388 wpa_s->supp_pbc_active = false;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001389 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CANCEL);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001390 wpa_s->after_wps = 0;
1391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 return 0;
1393}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394
1395
1396int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1397 const char *pin, struct wps_new_ap_settings *settings)
1398{
1399 struct wpa_ssid *ssid;
1400 char val[200];
1401 char *pos, *end;
1402 int res;
1403
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001404#ifdef CONFIG_AP
1405 if (wpa_s->ap_iface) {
1406 wpa_printf(MSG_DEBUG,
1407 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1408 return -1;
1409 }
1410#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 if (!pin)
1412 return -1;
1413 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001414 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 if (ssid == NULL)
1416 return -1;
1417 ssid->temporary = 1;
1418 pos = val;
1419 end = pos + sizeof(val);
1420 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001421 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 return -1;
1423 pos += res;
1424 if (settings) {
1425 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1426 "new_encr=%s new_key=%s",
1427 settings->ssid_hex, settings->auth,
1428 settings->encr, settings->key_hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001429 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 return -1;
1431 pos += res;
1432 }
1433 res = os_snprintf(pos, end - pos, "\"");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001434 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001436 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1437 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 if (wpa_s->wps_fragment_size)
1439 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1440 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1441 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001442 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 return 0;
1444}
1445
1446
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001447static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1448 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 size_t psk_len)
1450{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001451 if (is_zero_ether_addr(p2p_dev_addr)) {
1452 wpa_printf(MSG_DEBUG,
1453 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1454 MAC2STR(mac_addr));
1455 } else {
1456 wpa_printf(MSG_DEBUG,
1457 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1458 " P2P Device Addr " MACSTR,
1459 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1462
1463 /* TODO */
1464
1465 return 0;
1466}
1467
1468
1469static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1470 const struct wps_device_data *dev)
1471{
1472 char uuid[40], txt[400];
1473 int len;
1474 char devtype[WPS_DEV_TYPE_BUFSIZE];
1475 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1476 return;
1477 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1478 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1479 " [%s|%s|%s|%s|%s|%s]",
1480 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1481 dev->manufacturer, dev->model_name,
1482 dev->model_number, dev->serial_number,
1483 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1484 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001485 if (!os_snprintf_error(sizeof(txt), len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 wpa_printf(MSG_INFO, "%s", txt);
1487}
1488
1489
1490static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1491 u16 sel_reg_config_methods)
1492{
1493#ifdef CONFIG_WPS_ER
1494 struct wpa_supplicant *wpa_s = ctx;
1495
1496 if (wpa_s->wps_er == NULL)
1497 return;
1498 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1499 "dev_password_id=%u sel_reg_config_methods=0x%x",
1500 sel_reg, dev_passwd_id, sel_reg_config_methods);
1501 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1502 sel_reg_config_methods);
1503#endif /* CONFIG_WPS_ER */
1504}
1505
1506
1507static u16 wps_fix_config_methods(u16 config_methods)
1508{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 if ((config_methods &
1510 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1511 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1512 wpa_printf(MSG_INFO, "WPS: Converting display to "
1513 "virtual_display for WPS 2.0 compliance");
1514 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1515 }
1516 if ((config_methods &
1517 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1518 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1519 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1520 "virtual_push_button for WPS 2.0 compliance");
1521 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1522 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523
1524 return config_methods;
1525}
1526
1527
1528static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1529 struct wps_context *wps)
1530{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001531 char buf[50];
1532 const char *src;
1533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 if (is_nil_uuid(wpa_s->conf->uuid)) {
1535 struct wpa_supplicant *first;
1536 first = wpa_s->global->ifaces;
1537 while (first && first->next)
1538 first = first->next;
1539 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001540 if (wps != wpa_s->global->ifaces->wps)
1541 os_memcpy(wps->uuid,
1542 wpa_s->global->ifaces->wps->uuid,
1543 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001544 src = "from the first interface";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001545 } else if (wpa_s->conf->auto_uuid == 1) {
1546 uuid_random(wps->uuid);
1547 src = "based on random data";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 } else {
1549 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001550 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551 }
1552 } else {
1553 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001554 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001556
1557 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1558 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559}
1560
1561
Dmitry Shmidt04949592012-07-19 12:16:46 -07001562static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1563 struct wps_context *wps)
1564{
1565 wpabuf_free(wps->dev.vendor_ext_m1);
1566 wps->dev.vendor_ext_m1 = NULL;
1567
1568 if (wpa_s->conf->wps_vendor_ext_m1) {
1569 wps->dev.vendor_ext_m1 =
1570 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1571 if (!wps->dev.vendor_ext_m1) {
1572 wpa_printf(MSG_ERROR, "WPS: Cannot "
1573 "allocate memory for vendor_ext_m1");
1574 }
1575 }
1576}
1577
1578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579int wpas_wps_init(struct wpa_supplicant *wpa_s)
1580{
1581 struct wps_context *wps;
1582 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001583 struct hostapd_hw_modes *modes;
1584 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585
1586 wps = os_zalloc(sizeof(*wps));
1587 if (wps == NULL)
1588 return -1;
1589
1590 wps->cred_cb = wpa_supplicant_wps_cred;
1591 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001592 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 wps->cb_ctx = wpa_s;
1594
1595 wps->dev.device_name = wpa_s->conf->device_name;
1596 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1597 wps->dev.model_name = wpa_s->conf->model_name;
1598 wps->dev.model_number = wpa_s->conf->model_number;
1599 wps->dev.serial_number = wpa_s->conf->serial_number;
1600 wps->config_methods =
1601 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1602 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1603 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1604 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1605 "methods are not allowed at the same time");
1606 os_free(wps);
1607 return -1;
1608 }
1609 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001610 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1612 WPS_DEV_TYPE_LEN);
1613
1614 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1615 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1616 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1617
Dmitry Shmidt04949592012-07-19 12:16:46 -07001618 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001621 modes = wpa_s->hw.modes;
1622 if (modes) {
1623 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1624 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1625 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1626 wps->dev.rf_bands |= WPS_RF_24GHZ;
1627 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1628 wps->dev.rf_bands |= WPS_RF_50GHZ;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001629 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1630 wps->dev.rf_bands |= WPS_RF_60GHZ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001631 }
1632 }
1633 if (wps->dev.rf_bands == 0) {
1634 /*
1635 * Default to claiming support for both bands if the driver
1636 * does not provide support for fetching supported bands.
1637 */
1638 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1639 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1641 wpas_wps_set_uuid(wpa_s, wps);
1642
Hai Shalomb755a2a2020-04-23 21:49:02 -07001643#ifdef CONFIG_NO_TKIP
1644 wps->auth_types = WPS_AUTH_WPA2PSK;
1645 wps->encr_types = WPS_ENCR_AES;
1646#else /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1648 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
Hai Shalomb755a2a2020-04-23 21:49:02 -07001649#endif /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650
1651 os_memset(&rcfg, 0, sizeof(rcfg));
1652 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1653 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1654 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1655 rcfg.cb_ctx = wpa_s;
1656
1657 wps->registrar = wps_registrar_init(wps, &rcfg);
1658 if (wps->registrar == NULL) {
1659 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1660 os_free(wps);
1661 return -1;
1662 }
1663
1664 wpa_s->wps = wps;
1665
1666 return 0;
1667}
1668
1669
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001670#ifdef CONFIG_WPS_ER
1671static void wpas_wps_nfc_clear(struct wps_context *wps)
1672{
1673 wps->ap_nfc_dev_pw_id = 0;
1674 wpabuf_free(wps->ap_nfc_dh_pubkey);
1675 wps->ap_nfc_dh_pubkey = NULL;
1676 wpabuf_free(wps->ap_nfc_dh_privkey);
1677 wps->ap_nfc_dh_privkey = NULL;
1678 wpabuf_free(wps->ap_nfc_dev_pw);
1679 wps->ap_nfc_dev_pw = NULL;
1680}
1681#endif /* CONFIG_WPS_ER */
1682
1683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1685{
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001686 wpas_wps_assoc_with_cred_cancel(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001688 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001689 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001690 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001692#ifdef CONFIG_P2P
1693 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1694#endif /* CONFIG_P2P */
1695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 if (wpa_s->wps == NULL)
1697 return;
1698
1699#ifdef CONFIG_WPS_ER
1700 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1701 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001702 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703#endif /* CONFIG_WPS_ER */
1704
1705 wps_registrar_deinit(wpa_s->wps->registrar);
1706 wpabuf_free(wpa_s->wps->dh_pubkey);
1707 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001708 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 os_free(wpa_s->wps->network_key);
1710 os_free(wpa_s->wps);
1711 wpa_s->wps = NULL;
1712}
1713
1714
1715int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001716 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717{
1718 struct wpabuf *wps_ie;
1719
1720 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1721 return -1;
1722
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001723 wps_ie = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1725 if (!wps_ie) {
1726 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1727 return 0;
1728 }
1729
1730 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1731 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1732 "without active PBC Registrar");
1733 wpabuf_free(wps_ie);
1734 return 0;
1735 }
1736
1737 /* TODO: overlap detection */
1738 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1739 "(Active PBC)");
1740 wpabuf_free(wps_ie);
1741 return 1;
1742 }
1743
1744 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1745 if (!wps_ie) {
1746 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1747 return 0;
1748 }
1749
1750 /*
1751 * Start with WPS APs that advertise our address as an
1752 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1753 * allow any WPS AP after couple of scans since some APs do not
1754 * set Selected Registrar attribute properly when using
1755 * external Registrar.
1756 */
1757 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001758 struct os_reltime age;
1759
1760 os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1761
1762 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1763 age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1764 wpa_printf(MSG_DEBUG,
1765 " skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1766 wpa_s->scan_runs, (int) age.sec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 wpabuf_free(wps_ie);
1768 return 0;
1769 }
1770 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1771 } else {
1772 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1773 "(Authorized MAC or Active PIN)");
1774 }
1775 wpabuf_free(wps_ie);
1776 return 1;
1777 }
1778
1779 if (wps_ie) {
1780 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1781 wpabuf_free(wps_ie);
1782 return 1;
1783 }
1784
1785 return -1;
1786}
1787
1788
1789int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1790 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001791 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792{
1793 struct wpabuf *wps_ie = NULL;
1794 int ret = 0;
1795
1796 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001797 wps_ie = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1799 /* allow wildcard SSID for WPS PBC */
1800 ret = 1;
1801 }
1802 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001803 wps_ie = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 if (wps_ie &&
1805 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1806 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1807 /* allow wildcard SSID for WPS PIN */
1808 ret = 1;
1809 }
1810 }
1811
1812 if (!ret && ssid->bssid_set &&
1813 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1814 /* allow wildcard SSID due to hardcoded BSSID match */
1815 ret = 1;
1816 }
1817
1818#ifdef CONFIG_WPS_STRICT
1819 if (wps_ie) {
1820 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1821 0, bss->bssid) < 0)
1822 ret = 0;
1823 if (bss->beacon_ie_len) {
1824 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001825 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826 bss, WPS_IE_VENDOR_TYPE);
1827 if (bcn_wps == NULL) {
1828 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1829 "missing from AP Beacon");
1830 ret = 0;
1831 } else {
1832 if (wps_validate_beacon(wps_ie) < 0)
1833 ret = 0;
1834 wpabuf_free(bcn_wps);
1835 }
1836 }
1837 }
1838#endif /* CONFIG_WPS_STRICT */
1839
1840 wpabuf_free(wps_ie);
1841
1842 return ret;
1843}
1844
1845
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001846static bool wpas_wps_is_pbc_overlap(struct wps_ap_info *ap,
1847 struct wpa_bss *selected,
1848 struct wpa_ssid *ssid,
1849 const u8 *sel_uuid)
1850{
1851 if (!ap->pbc_active ||
1852 os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
1853 return false;
1854
1855 if (!is_zero_ether_addr(ssid->bssid) &&
1856 os_memcmp(ap->bssid, ssid->bssid, ETH_ALEN) != 0) {
1857 wpa_printf(MSG_DEBUG, "WPS: Ignore another BSS " MACSTR
1858 " in active PBC mode due to local BSSID limitation",
1859 MAC2STR(ap->bssid));
1860 return 0;
1861 }
1862
1863 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: " MACSTR,
1864 MAC2STR(ap->bssid));
1865 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1866 ap->uuid, UUID_LEN);
1867 if (!sel_uuid || os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0)
1868 return true;
1869
1870 /* TODO: verify that this is reasonable dual-band situation */
1871
1872 return false;
1873}
1874
1875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1877 struct wpa_bss *selected, struct wpa_ssid *ssid)
1878{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001879 struct wpa_supplicant *iface;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001880 const u8 *sel_uuid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 struct wpabuf *wps_ie;
1882 int ret = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001883 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884
1885 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1886 return 0;
1887
1888 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1889 "present in scan results; selected BSSID " MACSTR,
1890 MAC2STR(selected->bssid));
Hai Shalomfdcde762020-04-02 11:19:20 -07001891 if (!is_zero_ether_addr(ssid->bssid))
1892 wpa_printf(MSG_DEBUG,
1893 "WPS: Network profile limited to accept only a single BSSID " MACSTR,
1894 MAC2STR(ssid->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895
1896 /* Make sure that only one AP is in active PBC mode */
1897 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1898 if (wps_ie) {
1899 sel_uuid = wps_get_uuid_e(wps_ie);
1900 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1901 sel_uuid, UUID_LEN);
1902 } else {
1903 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1904 "WPS IE?!");
1905 sel_uuid = NULL;
1906 }
1907
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001908 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
1909 for (i = 0; i < iface->num_wps_ap; i++) {
1910 struct wps_ap_info *ap = &iface->wps_ap[i];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001911
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001912 if (wpas_wps_is_pbc_overlap(ap, selected, ssid,
1913 sel_uuid)) {
1914 ret = 1; /* PBC overlap */
1915 wpa_msg(iface, MSG_INFO,
1916 "WPS: PBC overlap detected: "
1917 MACSTR " and " MACSTR,
1918 MAC2STR(selected->bssid),
1919 MAC2STR(ap->bssid));
1920 break;
1921 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001922 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 }
1924
1925 wpabuf_free(wps_ie);
1926
1927 return ret;
1928}
1929
1930
1931void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1932{
1933 struct wpa_bss *bss;
1934 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1935
1936 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1937 return;
1938
1939 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1940 struct wpabuf *ie;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001941
1942 ie = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943 if (!ie)
1944 continue;
1945 if (wps_is_selected_pbc_registrar(ie))
1946 pbc++;
1947 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1948 auth++;
1949 else if (wps_is_selected_pin_registrar(ie))
1950 pin++;
1951 else
1952 wps++;
1953 wpabuf_free(ie);
1954 }
1955
1956 if (pbc)
1957 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1958 else if (auth)
1959 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1960 else if (pin)
1961 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1962 else if (wps)
1963 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1964}
1965
1966
1967int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1968{
1969 struct wpa_ssid *ssid;
1970
1971 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1972 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1973 return 1;
1974 }
1975
1976 return 0;
1977}
1978
1979
1980int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1981 char *end)
1982{
1983 struct wpabuf *wps_ie;
1984 int ret;
1985
1986 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1987 if (wps_ie == NULL)
1988 return 0;
1989
1990 ret = wps_attr_text(wps_ie, buf, end);
1991 wpabuf_free(wps_ie);
1992 return ret;
1993}
1994
1995
1996int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1997{
1998#ifdef CONFIG_WPS_ER
1999 if (wpa_s->wps_er) {
2000 wps_er_refresh(wpa_s->wps_er);
2001 return 0;
2002 }
2003 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
2004 if (wpa_s->wps_er == NULL)
2005 return -1;
2006 return 0;
2007#else /* CONFIG_WPS_ER */
2008 return 0;
2009#endif /* CONFIG_WPS_ER */
2010}
2011
2012
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002013void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014{
2015#ifdef CONFIG_WPS_ER
2016 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
2017 wpa_s->wps_er = NULL;
2018#endif /* CONFIG_WPS_ER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019}
2020
2021
2022#ifdef CONFIG_WPS_ER
2023int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
2024 const char *uuid, const char *pin)
2025{
2026 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002027 const u8 *use_uuid = NULL;
2028 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002030 if (os_strcmp(uuid, "any") == 0) {
2031 } else if (uuid_str2bin(uuid, u) == 0) {
2032 use_uuid = u;
2033 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
2034 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
2035 if (use_uuid == NULL)
2036 return -1;
2037 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 return -1;
2039 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002040 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 (const u8 *) pin, os_strlen(pin), 300);
2042}
2043
2044
2045int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
2046{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002047 u8 u[UUID_LEN], *use_uuid = NULL;
2048 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002050 if (uuid_str2bin(uuid, u) == 0)
2051 use_uuid = u;
2052 else if (hwaddr_aton(uuid, addr) == 0)
2053 use_addr = addr;
2054 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002056 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057}
2058
2059
2060int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
2061 const char *pin)
2062{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002063 u8 u[UUID_LEN], *use_uuid = NULL;
2064 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002066 if (uuid_str2bin(uuid, u) == 0)
2067 use_uuid = u;
2068 else if (hwaddr_aton(uuid, addr) == 0)
2069 use_addr = addr;
2070 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002072
2073 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 os_strlen(pin));
2075}
2076
2077
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002078static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
2079 struct wps_credential *cred)
2080{
2081 os_memset(cred, 0, sizeof(*cred));
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002082 if (ssid->ssid_len > SSID_MAX_LEN)
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002083 return -1;
2084 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
2085 cred->ssid_len = ssid->ssid_len;
2086 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
2087 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
2088 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
2089 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
2090 cred->encr_type = WPS_ENCR_AES;
2091 else
2092 cred->encr_type = WPS_ENCR_TKIP;
2093 if (ssid->passphrase) {
2094 cred->key_len = os_strlen(ssid->passphrase);
2095 if (cred->key_len >= 64)
2096 return -1;
2097 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2098 } else if (ssid->psk_set) {
2099 cred->key_len = 32;
2100 os_memcpy(cred->key, ssid->psk, 32);
2101 } else
2102 return -1;
2103 } else {
2104 cred->auth_type = WPS_AUTH_OPEN;
2105 cred->encr_type = WPS_ENCR_NONE;
2106 }
2107
2108 return 0;
2109}
2110
2111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2113 int id)
2114{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002115 u8 u[UUID_LEN], *use_uuid = NULL;
2116 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 struct wpa_ssid *ssid;
2118 struct wps_credential cred;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002119 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002121 if (uuid_str2bin(uuid, u) == 0)
2122 use_uuid = u;
2123 else if (hwaddr_aton(uuid, addr) == 0)
2124 use_addr = addr;
2125 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 return -1;
2127 ssid = wpa_config_get_network(wpa_s->conf, id);
2128 if (ssid == NULL || ssid->ssid == NULL)
2129 return -1;
2130
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002131 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002133 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2134 os_memset(&cred, 0, sizeof(cred));
2135 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136}
2137
2138
2139int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2140 const char *pin, struct wps_new_ap_settings *settings)
2141{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002142 u8 u[UUID_LEN], *use_uuid = NULL;
2143 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144 struct wps_credential cred;
2145 size_t len;
2146
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002147 if (uuid_str2bin(uuid, u) == 0)
2148 use_uuid = u;
2149 else if (hwaddr_aton(uuid, addr) == 0)
2150 use_addr = addr;
2151 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 return -1;
2153 if (settings->ssid_hex == NULL || settings->auth == NULL ||
2154 settings->encr == NULL || settings->key_hex == NULL)
2155 return -1;
2156
2157 os_memset(&cred, 0, sizeof(cred));
2158 len = os_strlen(settings->ssid_hex);
2159 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2160 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2161 return -1;
2162 cred.ssid_len = len / 2;
2163
2164 len = os_strlen(settings->key_hex);
2165 if ((len & 1) || len > 2 * sizeof(cred.key) ||
2166 hexstr2bin(settings->key_hex, cred.key, len / 2))
2167 return -1;
2168 cred.key_len = len / 2;
2169
2170 if (os_strcmp(settings->auth, "OPEN") == 0)
2171 cred.auth_type = WPS_AUTH_OPEN;
2172 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2173 cred.auth_type = WPS_AUTH_WPAPSK;
2174 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2175 cred.auth_type = WPS_AUTH_WPA2PSK;
2176 else
2177 return -1;
2178
2179 if (os_strcmp(settings->encr, "NONE") == 0)
2180 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002181#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 else if (os_strcmp(settings->encr, "WEP") == 0)
2183 cred.encr_type = WPS_ENCR_WEP;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002184#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 else if (os_strcmp(settings->encr, "TKIP") == 0)
2186 cred.encr_type = WPS_ENCR_TKIP;
2187 else if (os_strcmp(settings->encr, "CCMP") == 0)
2188 cred.encr_type = WPS_ENCR_AES;
2189 else
2190 return -1;
2191
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002192 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2193 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194}
2195
2196
Dmitry Shmidt04949592012-07-19 12:16:46 -07002197#ifdef CONFIG_WPS_NFC
2198struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2199 int ndef, const char *uuid)
2200{
2201 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002202 u8 u[UUID_LEN], *use_uuid = NULL;
2203 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002204
2205 if (!wpa_s->wps_er)
2206 return NULL;
2207
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002208 if (uuid_str2bin(uuid, u) == 0)
2209 use_uuid = u;
2210 else if (hwaddr_aton(uuid, addr) == 0)
2211 use_addr = addr;
2212 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002213 return NULL;
2214
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002215 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002216 if (ndef && ret) {
2217 struct wpabuf *tmp;
2218 tmp = ndef_build_wifi(ret);
2219 wpabuf_free(ret);
2220 if (tmp == NULL)
2221 return NULL;
2222 ret = tmp;
2223 }
2224
2225 return ret;
2226}
2227#endif /* CONFIG_WPS_NFC */
2228
2229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230static int callbacks_pending = 0;
2231
2232static void wpas_wps_terminate_cb(void *ctx)
2233{
2234 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2235 if (--callbacks_pending <= 0)
2236 eloop_terminate();
2237}
2238#endif /* CONFIG_WPS_ER */
2239
2240
2241int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2242{
2243#ifdef CONFIG_WPS_ER
2244 if (wpa_s->wps_er) {
2245 callbacks_pending++;
2246 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2247 wpa_s->wps_er = NULL;
2248 return 1;
2249 }
2250#endif /* CONFIG_WPS_ER */
2251 return 0;
2252}
2253
2254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2256{
2257 struct wps_context *wps = wpa_s->wps;
2258
2259 if (wps == NULL)
2260 return;
2261
2262 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2263 wps->config_methods = wps_config_methods_str2bin(
2264 wpa_s->conf->config_methods);
2265 if ((wps->config_methods &
2266 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2267 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2268 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2269 "config methods are not allowed at the "
2270 "same time");
2271 wps->config_methods &= ~WPS_CONFIG_LABEL;
2272 }
2273 }
2274 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002275 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276
2277 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2278 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2279 WPS_DEV_TYPE_LEN);
2280
2281 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2282 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2283 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2284 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2285 }
2286
Dmitry Shmidt04949592012-07-19 12:16:46 -07002287 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2288 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2291 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2292
2293 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2294 wpas_wps_set_uuid(wpa_s, wps);
2295
2296 if (wpa_s->conf->changed_parameters &
2297 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2298 /* Update pointers to make sure they refer current values */
2299 wps->dev.device_name = wpa_s->conf->device_name;
2300 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2301 wps->dev.model_name = wpa_s->conf->model_name;
2302 wps->dev.model_number = wpa_s->conf->model_number;
2303 wps->dev.serial_number = wpa_s->conf->serial_number;
2304 }
2305}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002306
2307
Mikael Kanstrupcc779b82019-08-16 08:50:54 +02002308void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2309{
2310 struct wps_context *wps;
2311
2312 wps = wpa_s->wps;
2313 if (wps)
2314 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2315}
2316
2317
Dmitry Shmidt04949592012-07-19 12:16:46 -07002318#ifdef CONFIG_WPS_NFC
2319
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002320#ifdef CONFIG_WPS_ER
Roshan Pius8894db22017-02-13 14:43:20 -08002321struct wpabuf *
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002322wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2323 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002324{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002325 struct wpabuf *ret;
2326 struct wps_credential cred;
2327
2328 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2329 return NULL;
2330
2331 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2332
2333 if (ndef && ret) {
2334 struct wpabuf *tmp;
2335 tmp = ndef_build_wifi(ret);
2336 wpabuf_free(ret);
2337 if (tmp == NULL)
2338 return NULL;
2339 ret = tmp;
2340 }
2341
2342 return ret;
2343}
2344#endif /* CONFIG_WPS_ER */
2345
2346
2347struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2348 int ndef, const char *id_str)
2349{
2350#ifdef CONFIG_WPS_ER
2351 if (id_str) {
2352 int id;
2353 char *end = NULL;
2354 struct wpa_ssid *ssid;
2355
2356 id = strtol(id_str, &end, 10);
2357 if (end && *end)
2358 return NULL;
2359
2360 ssid = wpa_config_get_network(wpa_s->conf, id);
2361 if (ssid == NULL)
2362 return NULL;
2363 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2364 }
2365#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002366#ifdef CONFIG_AP
2367 if (wpa_s->ap_iface)
2368 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2369#endif /* CONFIG_AP */
2370 return NULL;
2371}
2372
2373
Dmitry Shmidt04949592012-07-19 12:16:46 -07002374struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2375{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002376 if (wpa_s->conf->wps_nfc_pw_from_config) {
2377 return wps_nfc_token_build(ndef,
2378 wpa_s->conf->wps_nfc_dev_pw_id,
2379 wpa_s->conf->wps_nfc_dh_pubkey,
2380 wpa_s->conf->wps_nfc_dev_pw);
2381 }
2382
Dmitry Shmidt04949592012-07-19 12:16:46 -07002383 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2384 &wpa_s->conf->wps_nfc_dh_pubkey,
2385 &wpa_s->conf->wps_nfc_dh_privkey,
2386 &wpa_s->conf->wps_nfc_dev_pw);
2387}
2388
2389
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002390int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2391 const u8 *bssid,
2392 const struct wpabuf *dev_pw, u16 dev_pw_id,
2393 int p2p_group, const u8 *peer_pubkey_hash,
2394 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002395{
2396 struct wps_context *wps = wpa_s->wps;
2397 char pw[32 * 2 + 1];
2398
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002399 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2400 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2401 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2402 }
2403
Dmitry Shmidt04949592012-07-19 12:16:46 -07002404 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002405 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2406 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2407 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002408 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002409 }
2410
2411 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2412 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2413 "cannot start NFC-triggered connection", dev_pw_id);
2414 return -1;
2415 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002416
2417 dh5_free(wps->dh_ctx);
2418 wpabuf_free(wps->dh_pubkey);
2419 wpabuf_free(wps->dh_privkey);
2420 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2421 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2422 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2423 wps->dh_ctx = NULL;
2424 wpabuf_free(wps->dh_pubkey);
2425 wps->dh_pubkey = NULL;
2426 wpabuf_free(wps->dh_privkey);
2427 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002428 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002429 return -1;
2430 }
2431 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002432 if (wps->dh_ctx == NULL) {
2433 wpabuf_free(wps->dh_pubkey);
2434 wps->dh_pubkey = NULL;
2435 wpabuf_free(wps->dh_privkey);
2436 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002437 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002438 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002439 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002440
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002441 if (dev_pw) {
2442 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2443 wpabuf_head(dev_pw),
2444 wpabuf_len(dev_pw));
2445 }
2446 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2447 dev_pw ? pw : NULL,
2448 p2p_group, dev_pw_id, peer_pubkey_hash,
2449 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002450}
2451
2452
2453static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2454 struct wps_parse_attr *attr)
2455{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002456 /*
2457 * Disable existing networks temporarily to allow the newly learned
2458 * credential to be preferred. Enable the temporarily disabled networks
2459 * after 10 seconds.
2460 */
2461 wpas_wps_temp_disable(wpa_s, NULL);
2462 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2463 NULL);
2464
Dmitry Shmidt04949592012-07-19 12:16:46 -07002465 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2466 return -1;
2467
2468 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2469 return 0;
2470
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002471 if (attr->ap_channel) {
2472 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002473 int freq = 0;
2474
2475 if (chan >= 1 && chan <= 13)
2476 freq = 2407 + 5 * chan;
2477 else if (chan == 14)
2478 freq = 2484;
2479 else if (chan >= 30)
2480 freq = 5000 + 5 * chan;
2481
2482 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002483 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2484 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002485 wpa_s->after_wps = 5;
2486 wpa_s->wps_freq = freq;
2487 }
2488 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002489
2490 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2491 "based on the received credential added");
2492 wpa_s->normal_scans = 0;
2493 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002494 wpa_s->disconnected = 0;
2495 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002496
2497 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002498 wpa_supplicant_req_scan(wpa_s, 0, 0);
2499
2500 return 0;
2501}
2502
2503
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002504#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002505static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2506 struct wps_parse_attr *attr)
2507{
2508 return wps_registrar_add_nfc_password_token(
2509 wpa_s->wps->registrar, attr->oob_dev_password,
2510 attr->oob_dev_password_len);
2511}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002512#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002513
2514
2515static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2516 const struct wpabuf *wps)
2517{
2518 struct wps_parse_attr attr;
2519
2520 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2521
2522 if (wps_parse_msg(wps, &attr)) {
2523 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2524 return -1;
2525 }
2526
2527 if (attr.num_cred)
2528 return wpas_wps_use_cred(wpa_s, &attr);
2529
2530#ifdef CONFIG_WPS_ER
2531 if (attr.oob_dev_password)
2532 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2533#endif /* CONFIG_WPS_ER */
2534
2535 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2536 return -1;
2537}
2538
2539
2540int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002541 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002542{
2543 const struct wpabuf *wps = data;
2544 struct wpabuf *tmp = NULL;
2545 int ret;
2546
2547 if (wpabuf_len(data) < 4)
2548 return -1;
2549
2550 if (*wpabuf_head_u8(data) != 0x10) {
2551 /* Assume this contains full NDEF record */
2552 tmp = ndef_parse_wifi(data);
2553 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002554#ifdef CONFIG_P2P
2555 tmp = ndef_parse_p2p(data);
2556 if (tmp) {
2557 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2558 forced_freq);
2559 wpabuf_free(tmp);
2560 return ret;
2561 }
2562#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002563 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2564 return -1;
2565 }
2566 wps = tmp;
2567 }
2568
2569 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2570 wpabuf_free(tmp);
2571 return ret;
2572}
2573
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002574
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002575struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2576 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002577{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002578 struct wpabuf *ret;
2579
2580 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2581 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2582 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2583 return NULL;
2584
2585 ret = wps_build_nfc_handover_req(wpa_s->wps,
2586 wpa_s->conf->wps_nfc_dh_pubkey);
2587
2588 if (ndef && ret) {
2589 struct wpabuf *tmp;
2590 tmp = ndef_build_wifi(ret);
2591 wpabuf_free(ret);
2592 if (tmp == NULL)
2593 return NULL;
2594 ret = tmp;
2595 }
2596
2597 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002598}
2599
2600
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002601#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002602
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002603static struct wpabuf *
2604wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2605 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002606{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002607#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002608 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002609 u8 u[UUID_LEN], *use_uuid = NULL;
2610 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002611 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002612
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002613 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002614 return NULL;
2615
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002616 if (uuid == NULL)
2617 return NULL;
2618 if (uuid_str2bin(uuid, u) == 0)
2619 use_uuid = u;
2620 else if (hwaddr_aton(uuid, addr) == 0)
2621 use_addr = addr;
2622 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002623 return NULL;
2624
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002625 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2626 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2627 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2628 return NULL;
2629 }
2630
2631 wpas_wps_nfc_clear(wps);
2632 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2633 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2634 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2635 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2636 wpas_wps_nfc_clear(wps);
2637 return NULL;
2638 }
2639
2640 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2641 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002642 if (ndef && ret) {
2643 struct wpabuf *tmp;
2644 tmp = ndef_build_wifi(ret);
2645 wpabuf_free(ret);
2646 if (tmp == NULL)
2647 return NULL;
2648 ret = tmp;
2649 }
2650
2651 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002652#else /* CONFIG_WPS_ER */
2653 return NULL;
2654#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002655}
2656#endif /* CONFIG_WPS_NFC */
2657
2658
2659struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2660 int ndef, int cr, const char *uuid)
2661{
2662 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002663 if (!cr)
2664 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002665 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2666 if (ret)
2667 return ret;
2668 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002669}
2670
2671
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002672static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2673 const struct wpabuf *data)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002674{
2675 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002676 int ret = -1;
2677 u16 wsc_len;
2678 const u8 *pos;
2679 struct wpabuf msg;
2680 struct wps_parse_attr attr;
2681 u16 dev_pw_id;
2682 const u8 *bssid = NULL;
2683 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002684
2685 wps = ndef_parse_wifi(data);
2686 if (wps == NULL)
2687 return -1;
2688 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2689 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002690 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2691 if (wpabuf_len(wps) < 2) {
2692 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2693 "Message");
2694 goto out;
2695 }
2696 pos = wpabuf_head(wps);
2697 wsc_len = WPA_GET_BE16(pos);
2698 if (wsc_len > wpabuf_len(wps) - 2) {
2699 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2700 "in Wi-Fi Handover Select Message", wsc_len);
2701 goto out;
2702 }
2703 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002704
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002705 wpa_hexdump(MSG_DEBUG,
2706 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2707 pos, wsc_len);
2708 if (wsc_len < wpabuf_len(wps) - 2) {
2709 wpa_hexdump(MSG_DEBUG,
2710 "WPS: Ignore extra data after WSC attributes",
2711 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2712 }
2713
2714 wpabuf_set(&msg, pos, wsc_len);
2715 ret = wps_parse_msg(&msg, &attr);
2716 if (ret < 0) {
2717 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2718 "Wi-Fi Handover Select Message");
2719 goto out;
2720 }
2721
2722 if (attr.oob_dev_password == NULL ||
2723 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2724 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2725 "included in Wi-Fi Handover Select Message");
2726 ret = -1;
2727 goto out;
2728 }
2729
2730 if (attr.ssid == NULL) {
2731 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2732 "Select Message");
2733 ret = -1;
2734 goto out;
2735 }
2736
2737 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2738
2739 if (attr.mac_addr) {
2740 bssid = attr.mac_addr;
2741 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2742 MAC2STR(bssid));
2743 }
2744
2745 if (attr.rf_bands)
2746 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2747
2748 if (attr.ap_channel) {
2749 u16 chan = WPA_GET_BE16(attr.ap_channel);
2750
2751 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2752
2753 if (chan >= 1 && chan <= 13 &&
2754 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2755 freq = 2407 + 5 * chan;
2756 else if (chan == 14 &&
2757 (attr.rf_bands == NULL ||
2758 *attr.rf_bands & WPS_RF_24GHZ))
2759 freq = 2484;
2760 else if (chan >= 30 &&
2761 (attr.rf_bands == NULL ||
2762 *attr.rf_bands & WPS_RF_50GHZ))
2763 freq = 5000 + 5 * chan;
Hai Shalomc3565922019-10-28 11:58:20 -07002764 else if (chan >= 1 && chan <= 6 &&
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002765 (attr.rf_bands == NULL ||
2766 *attr.rf_bands & WPS_RF_60GHZ))
2767 freq = 56160 + 2160 * chan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002768
2769 if (freq) {
2770 wpa_printf(MSG_DEBUG,
2771 "WPS: AP indicated channel %u -> %u MHz",
2772 chan, freq);
2773 }
2774 }
2775
2776 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2777 attr.oob_dev_password, attr.oob_dev_password_len);
2778 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2779 WPS_OOB_PUBKEY_HASH_LEN);
2780 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2781 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2782 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2783 ret = -1;
2784 goto out;
2785 }
2786 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2787 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2788
2789 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2790 attr.oob_dev_password,
2791 attr.ssid, attr.ssid_len, freq);
2792
2793out:
2794 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002795 return ret;
2796}
2797
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002798
2799int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2800 const struct wpabuf *req,
2801 const struct wpabuf *sel)
2802{
2803 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2804 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2805 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2806 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2807}
2808
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002809
2810int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2811 const struct wpabuf *req,
2812 const struct wpabuf *sel)
2813{
2814 struct wpabuf *wps;
2815 int ret = -1;
2816 u16 wsc_len;
2817 const u8 *pos;
2818 struct wpabuf msg;
2819 struct wps_parse_attr attr;
2820 u16 dev_pw_id;
2821
2822 /*
2823 * Enrollee/station is always initiator of the NFC connection handover,
2824 * so use the request message here to find Enrollee public key hash.
2825 */
2826 wps = ndef_parse_wifi(req);
2827 if (wps == NULL)
2828 return -1;
2829 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2830 "payload from NFC connection handover");
2831 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2832 if (wpabuf_len(wps) < 2) {
2833 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2834 "Message");
2835 goto out;
2836 }
2837 pos = wpabuf_head(wps);
2838 wsc_len = WPA_GET_BE16(pos);
2839 if (wsc_len > wpabuf_len(wps) - 2) {
2840 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2841 "in rt Wi-Fi Handover Request Message", wsc_len);
2842 goto out;
2843 }
2844 pos += 2;
2845
2846 wpa_hexdump(MSG_DEBUG,
2847 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2848 pos, wsc_len);
2849 if (wsc_len < wpabuf_len(wps) - 2) {
2850 wpa_hexdump(MSG_DEBUG,
2851 "WPS: Ignore extra data after WSC attributes",
2852 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2853 }
2854
2855 wpabuf_set(&msg, pos, wsc_len);
2856 ret = wps_parse_msg(&msg, &attr);
2857 if (ret < 0) {
2858 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2859 "Wi-Fi Handover Request Message");
2860 goto out;
2861 }
2862
2863 if (attr.oob_dev_password == NULL ||
2864 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2865 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2866 "included in Wi-Fi Handover Request Message");
2867 ret = -1;
2868 goto out;
2869 }
2870
2871 if (attr.uuid_e == NULL) {
2872 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2873 "Handover Request Message");
2874 ret = -1;
2875 goto out;
2876 }
2877
2878 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2879
2880 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2881 attr.oob_dev_password, attr.oob_dev_password_len);
2882 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2883 WPS_OOB_PUBKEY_HASH_LEN);
2884 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2885 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2886 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2887 ret = -1;
2888 goto out;
2889 }
2890 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2891 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2892
2893 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2894 attr.oob_dev_password,
2895 DEV_PW_NFC_CONNECTION_HANDOVER,
2896 NULL, 0, 1);
2897
2898out:
2899 wpabuf_free(wps);
2900 return ret;
2901}
2902
Dmitry Shmidt04949592012-07-19 12:16:46 -07002903#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002904
2905
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002906static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2907{
2908 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002909 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002910
2911 if (wpa_debug_level > MSG_DEBUG)
2912 return;
2913
2914 if (wpa_s->wps_ap == NULL)
2915 return;
2916
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002917 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002918
2919 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2920 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
Hai Shalom60840252021-02-19 19:02:11 -08002921 struct wpa_bssid_ignore *e = wpa_bssid_ignore_get(wpa_s,
2922 ap->bssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002923
2924 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
Hai Shalom60840252021-02-19 19:02:11 -08002925 "tries=%d last_attempt=%d sec ago bssid_ignore=%d",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002926 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2927 ap->last_attempt.sec > 0 ?
2928 (int) now.sec - (int) ap->last_attempt.sec : -1,
2929 e ? e->count : 0);
2930 }
2931}
2932
2933
2934static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2935 const u8 *bssid)
2936{
2937 size_t i;
2938
2939 if (wpa_s->wps_ap == NULL)
2940 return NULL;
2941
2942 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2943 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2944 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2945 return ap;
2946 }
2947
2948 return NULL;
2949}
2950
2951
2952static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2953 struct wpa_scan_res *res)
2954{
2955 struct wpabuf *wps;
2956 enum wps_ap_info_type type;
2957 struct wps_ap_info *ap;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002958 int r, pbc_active;
2959 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002960
2961 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2962 return;
2963
2964 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2965 if (wps == NULL)
2966 return;
2967
2968 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2969 if (r == 2)
2970 type = WPS_AP_SEL_REG_OUR;
2971 else if (r == 1)
2972 type = WPS_AP_SEL_REG;
2973 else
2974 type = WPS_AP_NOT_SEL_REG;
2975
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002976 uuid = wps_get_uuid_e(wps);
2977 pbc_active = wps_is_selected_pbc_registrar(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002978
2979 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2980 if (ap) {
2981 if (ap->type != type) {
2982 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2983 " changed type %d -> %d",
2984 MAC2STR(res->bssid), ap->type, type);
2985 ap->type = type;
2986 if (type != WPS_AP_NOT_SEL_REG)
Hai Shalom60840252021-02-19 19:02:11 -08002987 wpa_bssid_ignore_del(wpa_s, ap->bssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002988 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002989 ap->pbc_active = pbc_active;
2990 if (uuid)
2991 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2992 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002993 }
2994
2995 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2996 sizeof(struct wps_ap_info));
2997 if (ap == NULL)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002998 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002999
3000 wpa_s->wps_ap = ap;
3001 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
3002 wpa_s->num_wps_ap++;
3003
3004 os_memset(ap, 0, sizeof(*ap));
3005 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
3006 ap->type = type;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003007 ap->pbc_active = pbc_active;
3008 if (uuid)
3009 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003010 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
3011 MAC2STR(ap->bssid), ap->type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003012
3013out:
3014 wpabuf_free(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003015}
3016
3017
3018void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
3019 struct wpa_scan_results *scan_res)
3020{
3021 size_t i;
3022
3023 for (i = 0; i < scan_res->num; i++)
3024 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
3025
3026 wpas_wps_dump_ap_info(wpa_s);
3027}
3028
3029
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00003030bool wpas_wps_partner_link_scan_done(struct wpa_supplicant *wpa_s)
3031{
3032 struct wpa_global *global = wpa_s->global;
3033 struct wpa_supplicant *iface;
3034
3035 for (iface = global->ifaces; iface; iface = iface->next) {
3036 if (iface == wpa_s)
3037 continue;
3038
3039 if (!iface->supp_pbc_active)
3040 continue;
3041
3042 /* Scan results are available for both links. While the current
3043 * link will proceed for network selection, ensure the partner
3044 * link also gets an attempt at network selection and connect
3045 * with the selected BSS. */
3046 if (iface->wps_scan_done)
3047 wpa_wps_supplicant_fast_associate(iface);
3048 else
3049 return false;
3050 }
3051
3052 return true;
3053}
3054
3055
3056bool wpas_wps_partner_link_overlap_detect(struct wpa_supplicant *wpa_s)
3057{
3058 struct wpa_global *global = wpa_s->global;
3059 struct wpa_supplicant *iface;
3060
3061 for (iface = global->ifaces; iface; iface = iface->next) {
3062 if (iface == wpa_s)
3063 continue;
3064 if (iface->wps_overlap)
3065 return true;
3066 }
3067
3068 return false;
3069}
3070
3071
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003072void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
3073{
3074 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003075
3076 wpa_s->after_wps = 0;
3077
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003078 if (!wpa_s->wps_ap_iter)
3079 return;
3080 ap = wpas_wps_get_ap_info(wpa_s, bssid);
3081 if (ap == NULL)
3082 return;
3083 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003084 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003085}