blob: cd94b647a7f616aea9df666936102b8025336c3e [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;
Sunil Raviaf8751c2023-03-29 11:35:17 -07001017 wpa_s->wps_overlap = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 wpas_clear_wps(wpa_s);
1019}
1020
1021
1022static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001023 int registrar, const u8 *dev_addr,
1024 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025{
1026 struct wpa_ssid *ssid;
1027
1028 ssid = wpa_config_add_network(wpa_s->conf);
1029 if (ssid == NULL)
1030 return NULL;
1031 wpas_notify_network_added(wpa_s, ssid);
1032 wpa_config_set_network_defaults(ssid);
1033 ssid->temporary = 1;
1034 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
1035 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
1036 wpa_config_set(ssid, "identity", registrar ?
1037 "\"" WSC_ID_REGISTRAR "\"" :
1038 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1039 wpas_notify_network_removed(wpa_s, ssid);
1040 wpa_config_remove_network(wpa_s->conf, ssid->id);
1041 return NULL;
1042 }
1043
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001044#ifdef CONFIG_P2P
1045 if (dev_addr)
1046 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1047#endif /* CONFIG_P2P */
1048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 if (bssid) {
1050#ifndef CONFIG_P2P
1051 struct wpa_bss *bss;
1052 int count = 0;
1053#endif /* CONFIG_P2P */
1054
1055 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1056 ssid->bssid_set = 1;
1057
1058 /*
1059 * Note: With P2P, the SSID may change at the time the WPS
1060 * provisioning is started, so better not filter the AP based
1061 * on the current SSID in the scan results.
1062 */
1063#ifndef CONFIG_P2P
1064 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1065 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
1066 continue;
1067
1068 os_free(ssid->ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001069 ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 if (ssid->ssid == NULL)
1071 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 ssid->ssid_len = bss->ssid_len;
1073 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1074 "scan results",
1075 ssid->ssid, ssid->ssid_len);
1076 count++;
1077 }
1078
1079 if (count > 1) {
1080 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1081 "for the AP; use wildcard");
1082 os_free(ssid->ssid);
1083 ssid->ssid = NULL;
1084 ssid->ssid_len = 0;
1085 }
1086#endif /* CONFIG_P2P */
1087 }
1088
1089 return ssid;
1090}
1091
1092
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001093static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1094 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095{
1096 struct wpa_ssid *ssid;
1097
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001098 if (wpa_s->current_ssid) {
1099 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001100 wpa_supplicant_deauthenticate(
1101 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001102 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 /* Mark all other networks disabled and trigger reassociation */
1105 ssid = wpa_s->conf->ssid;
1106 while (ssid) {
1107 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001108 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001109 /*
1110 * In case the network object corresponds to a persistent group
1111 * then do not send out network disabled signal. In addition,
1112 * do not change disabled status of persistent network objects
1113 * from 2 to 1 should we connect to another network.
1114 */
1115 if (was_disabled != 2) {
1116 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001117 if (was_disabled != ssid->disabled) {
1118 if (ssid->disabled)
1119 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001120 wpas_notify_network_enabled_changed(wpa_s,
1121 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001122 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001123 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 ssid = ssid->next;
1125 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001126}
1127
1128
1129static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001130 struct wpa_ssid *selected, const u8 *bssid,
1131 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001132{
1133 struct wpa_bss *bss;
1134
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001135 wpa_s->wps_run++;
1136 if (wpa_s->wps_run == 0)
1137 wpa_s->wps_run++;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001138 wpa_s->after_wps = 0;
1139 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001140 if (freq) {
1141 wpa_s->after_wps = 5;
1142 wpa_s->wps_freq = freq;
1143 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001144 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1145 if (bss && bss->freq > 0) {
1146 wpa_s->known_wps_freq = 1;
1147 wpa_s->wps_freq = bss->freq;
1148 }
1149 }
1150
1151 wpas_wps_temp_disable(wpa_s, selected);
1152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 wpa_s->disconnected = 0;
1154 wpa_s->reassociate = 1;
1155 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001156 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 wpa_s->wps_success = 0;
Hai Shalom60840252021-02-19 19:02:11 -08001158 wpa_s->bssid_ignore_cleared = false;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001159
1160 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 wpa_supplicant_req_scan(wpa_s, 0, 0);
1162}
1163
1164
1165int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
Hai Shalom021b0b52019-04-10 11:17:58 -07001166 int p2p_group, int multi_ap_backhaul_sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167{
1168 struct wpa_ssid *ssid;
Hai Shalom021b0b52019-04-10 11:17:58 -07001169 char phase1[32];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001170
1171#ifdef CONFIG_AP
1172 if (wpa_s->ap_iface) {
1173 wpa_printf(MSG_DEBUG,
1174 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1175 return -1;
1176 }
1177#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001179 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 if (ssid == NULL)
1181 return -1;
1182 ssid->temporary = 1;
1183 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001184 /*
1185 * When starting a regular WPS process (not P2P group formation)
1186 * the registrar/final station can be either AP or PCP
1187 * so use a "don't care" value for the pbss flag.
1188 */
1189 if (!p2p_group)
1190 ssid->pbss = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191#ifdef CONFIG_P2P
1192 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1193 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1194 if (ssid->ssid) {
1195 ssid->ssid_len = wpa_s->go_params->ssid_len;
1196 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1197 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001198 if (wpa_s->go_params->freq > 56160) {
1199 /* P2P in 60 GHz uses PBSS */
1200 ssid->pbss = 1;
1201 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001202 if (wpa_s->go_params->edmg &&
1203 wpas_p2p_try_edmg_channel(wpa_s,
1204 wpa_s->go_params) == 0)
1205 ssid->enable_edmg = 1;
1206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1208 "SSID", ssid->ssid, ssid->ssid_len);
1209 }
1210 }
1211#endif /* CONFIG_P2P */
Hai Shalom021b0b52019-04-10 11:17:58 -07001212 os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
1213 multi_ap_backhaul_sta ? " multi_ap=1" : "");
1214 if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001215 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 if (wpa_s->wps_fragment_size)
1217 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Hai Shalom021b0b52019-04-10 11:17:58 -07001218 if (multi_ap_backhaul_sta)
1219 ssid->multi_ap_backhaul_sta = 1;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001220 wpa_s->supp_pbc_active = true;
1221 wpa_s->wps_overlap = false;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001222 wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1224 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001225 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 return 0;
1227}
1228
1229
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001230static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1231 const u8 *dev_addr, const u8 *bssid,
1232 const char *pin, int p2p_group, u16 dev_pw_id,
1233 const u8 *peer_pubkey_hash,
1234 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235{
1236 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001237 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001239 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001241#ifdef CONFIG_AP
1242 if (wpa_s->ap_iface) {
1243 wpa_printf(MSG_DEBUG,
1244 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1245 return -1;
1246 }
1247#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001249 if (bssid && is_zero_ether_addr(bssid))
1250 bssid = NULL;
1251 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1252 if (ssid == NULL) {
1253 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 ssid->temporary = 1;
1257 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001258 /*
1259 * When starting a regular WPS process (not P2P group formation)
1260 * the registrar/final station can be either AP or PCP
1261 * so use a "don't care" value for the pbss flag.
1262 */
1263 if (!p2p_group)
1264 ssid->pbss = 2;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001265 if (ssid_val) {
1266 ssid->ssid = os_malloc(ssid_len);
1267 if (ssid->ssid) {
1268 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1269 ssid->ssid_len = ssid_len;
1270 }
1271 }
1272 if (peer_pubkey_hash) {
1273 os_memcpy(hash, " pkhash=", 8);
1274 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1275 peer_pubkey_hash,
1276 WPS_OOB_PUBKEY_HASH_LEN);
1277 } else {
1278 hash[0] = '\0';
1279 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280#ifdef CONFIG_P2P
1281 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001282 os_free(ssid->ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1284 if (ssid->ssid) {
1285 ssid->ssid_len = wpa_s->go_params->ssid_len;
1286 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1287 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001288 if (wpa_s->go_params->freq > 56160) {
1289 /* P2P in 60 GHz uses PBSS */
1290 ssid->pbss = 1;
1291 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001292 if (wpa_s->go_params->edmg &&
1293 wpas_p2p_try_edmg_channel(wpa_s,
1294 wpa_s->go_params) == 0)
1295 ssid->enable_edmg = 1;
1296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1298 "SSID", ssid->ssid, ssid->ssid_len);
1299 }
1300 }
1301#endif /* CONFIG_P2P */
1302 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001303 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1304 pin, dev_pw_id, hash);
1305 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1306 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1307 dev_pw_id, hash);
1308 } else {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001309 if (wps_generate_pin(&rpin) < 0) {
1310 wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1311 return -1;
1312 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001313 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1314 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001316 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1317 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001318 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001319 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001320
1321 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER)
1322 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_PIN_ACTIVE);
1323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 if (wpa_s->wps_fragment_size)
1325 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1326 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1327 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001328 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001329 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330 return rpin;
1331}
1332
1333
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001334int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1335 const char *pin, int p2p_group, u16 dev_pw_id)
1336{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001337 os_get_reltime(&wpa_s->wps_pin_start_time);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001338 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1339 dev_pw_id, NULL, NULL, 0, 0);
1340}
1341
1342
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001343void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1344{
1345 union wps_event_data data;
1346
1347 os_memset(&data, 0, sizeof(data));
1348 data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1349 data.fail.error_indication = WPS_EI_NO_ERROR;
1350 /*
1351 * Call wpas_notify_wps_event_fail() directly instead of through
1352 * wpa_supplicant_wps_event() which would end up registering unnecessary
1353 * timeouts (those are only for the case where the failure happens
1354 * during an EAP-WSC exchange).
1355 */
1356 wpas_notify_wps_event_fail(wpa_s, &data.fail);
1357}
1358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359/* Cancel the wps pbc/pin requests */
1360int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1361{
1362#ifdef CONFIG_AP
1363 if (wpa_s->ap_iface) {
1364 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1365 return wpa_supplicant_ap_wps_cancel(wpa_s);
1366 }
1367#endif /* CONFIG_AP */
1368
Dmitry Shmidt04949592012-07-19 12:16:46 -07001369 if (wpa_s->wpa_state == WPA_SCANNING ||
1370 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1372 wpa_supplicant_cancel_scan(wpa_s);
1373 wpas_clear_wps(wpa_s);
1374 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1375 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1376 "deauthenticate");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001377 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 wpa_supplicant_deauthenticate(wpa_s,
1379 WLAN_REASON_DEAUTH_LEAVING);
1380 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001381 } else {
1382 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001383 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001384 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1385 0)
1386 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 }
1388
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001389 wpa_s->supp_pbc_active = false;
Sunil Raviaf8751c2023-03-29 11:35:17 -07001390 wpa_s->wps_overlap = false;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001391 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CANCEL);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001392 wpa_s->after_wps = 0;
1393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 return 0;
1395}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396
1397
1398int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1399 const char *pin, struct wps_new_ap_settings *settings)
1400{
1401 struct wpa_ssid *ssid;
1402 char val[200];
1403 char *pos, *end;
1404 int res;
1405
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001406#ifdef CONFIG_AP
1407 if (wpa_s->ap_iface) {
1408 wpa_printf(MSG_DEBUG,
1409 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1410 return -1;
1411 }
1412#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 if (!pin)
1414 return -1;
1415 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001416 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 if (ssid == NULL)
1418 return -1;
1419 ssid->temporary = 1;
1420 pos = val;
1421 end = pos + sizeof(val);
1422 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001423 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 return -1;
1425 pos += res;
1426 if (settings) {
1427 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1428 "new_encr=%s new_key=%s",
1429 settings->ssid_hex, settings->auth,
1430 settings->encr, settings->key_hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001431 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 return -1;
1433 pos += res;
1434 }
1435 res = os_snprintf(pos, end - pos, "\"");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001436 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001438 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1439 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 if (wpa_s->wps_fragment_size)
1441 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1442 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1443 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001444 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 return 0;
1446}
1447
1448
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001449static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1450 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 size_t psk_len)
1452{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001453 if (is_zero_ether_addr(p2p_dev_addr)) {
1454 wpa_printf(MSG_DEBUG,
1455 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1456 MAC2STR(mac_addr));
1457 } else {
1458 wpa_printf(MSG_DEBUG,
1459 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1460 " P2P Device Addr " MACSTR,
1461 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1464
1465 /* TODO */
1466
1467 return 0;
1468}
1469
1470
1471static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1472 const struct wps_device_data *dev)
1473{
1474 char uuid[40], txt[400];
1475 int len;
1476 char devtype[WPS_DEV_TYPE_BUFSIZE];
1477 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1478 return;
1479 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1480 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1481 " [%s|%s|%s|%s|%s|%s]",
1482 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1483 dev->manufacturer, dev->model_name,
1484 dev->model_number, dev->serial_number,
1485 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1486 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001487 if (!os_snprintf_error(sizeof(txt), len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488 wpa_printf(MSG_INFO, "%s", txt);
1489}
1490
1491
1492static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1493 u16 sel_reg_config_methods)
1494{
1495#ifdef CONFIG_WPS_ER
1496 struct wpa_supplicant *wpa_s = ctx;
1497
1498 if (wpa_s->wps_er == NULL)
1499 return;
1500 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1501 "dev_password_id=%u sel_reg_config_methods=0x%x",
1502 sel_reg, dev_passwd_id, sel_reg_config_methods);
1503 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1504 sel_reg_config_methods);
1505#endif /* CONFIG_WPS_ER */
1506}
1507
1508
1509static u16 wps_fix_config_methods(u16 config_methods)
1510{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 if ((config_methods &
1512 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1513 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1514 wpa_printf(MSG_INFO, "WPS: Converting display to "
1515 "virtual_display for WPS 2.0 compliance");
1516 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1517 }
1518 if ((config_methods &
1519 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1520 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1521 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1522 "virtual_push_button for WPS 2.0 compliance");
1523 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1524 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525
1526 return config_methods;
1527}
1528
1529
1530static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1531 struct wps_context *wps)
1532{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001533 char buf[50];
1534 const char *src;
1535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536 if (is_nil_uuid(wpa_s->conf->uuid)) {
1537 struct wpa_supplicant *first;
1538 first = wpa_s->global->ifaces;
1539 while (first && first->next)
1540 first = first->next;
1541 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001542 if (wps != wpa_s->global->ifaces->wps)
1543 os_memcpy(wps->uuid,
1544 wpa_s->global->ifaces->wps->uuid,
1545 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001546 src = "from the first interface";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001547 } else if (wpa_s->conf->auto_uuid == 1) {
1548 uuid_random(wps->uuid);
1549 src = "based on random data";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 } else {
1551 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001552 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 }
1554 } else {
1555 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001556 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001558
1559 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1560 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561}
1562
1563
Dmitry Shmidt04949592012-07-19 12:16:46 -07001564static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1565 struct wps_context *wps)
1566{
1567 wpabuf_free(wps->dev.vendor_ext_m1);
1568 wps->dev.vendor_ext_m1 = NULL;
1569
1570 if (wpa_s->conf->wps_vendor_ext_m1) {
1571 wps->dev.vendor_ext_m1 =
1572 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1573 if (!wps->dev.vendor_ext_m1) {
1574 wpa_printf(MSG_ERROR, "WPS: Cannot "
1575 "allocate memory for vendor_ext_m1");
1576 }
1577 }
1578}
1579
1580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581int wpas_wps_init(struct wpa_supplicant *wpa_s)
1582{
1583 struct wps_context *wps;
1584 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001585 struct hostapd_hw_modes *modes;
1586 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587
1588 wps = os_zalloc(sizeof(*wps));
1589 if (wps == NULL)
1590 return -1;
1591
1592 wps->cred_cb = wpa_supplicant_wps_cred;
1593 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001594 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 wps->cb_ctx = wpa_s;
1596
1597 wps->dev.device_name = wpa_s->conf->device_name;
1598 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1599 wps->dev.model_name = wpa_s->conf->model_name;
1600 wps->dev.model_number = wpa_s->conf->model_number;
1601 wps->dev.serial_number = wpa_s->conf->serial_number;
1602 wps->config_methods =
1603 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1604 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1605 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1606 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1607 "methods are not allowed at the same time");
1608 os_free(wps);
1609 return -1;
1610 }
1611 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001612 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1614 WPS_DEV_TYPE_LEN);
1615
1616 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1617 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1618 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1619
Dmitry Shmidt04949592012-07-19 12:16:46 -07001620 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001623 modes = wpa_s->hw.modes;
1624 if (modes) {
1625 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1626 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1627 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1628 wps->dev.rf_bands |= WPS_RF_24GHZ;
1629 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1630 wps->dev.rf_bands |= WPS_RF_50GHZ;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001631 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1632 wps->dev.rf_bands |= WPS_RF_60GHZ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001633 }
1634 }
1635 if (wps->dev.rf_bands == 0) {
1636 /*
1637 * Default to claiming support for both bands if the driver
1638 * does not provide support for fetching supported bands.
1639 */
1640 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1643 wpas_wps_set_uuid(wpa_s, wps);
1644
Hai Shalomb755a2a2020-04-23 21:49:02 -07001645#ifdef CONFIG_NO_TKIP
1646 wps->auth_types = WPS_AUTH_WPA2PSK;
1647 wps->encr_types = WPS_ENCR_AES;
1648#else /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1650 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
Hai Shalomb755a2a2020-04-23 21:49:02 -07001651#endif /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652
1653 os_memset(&rcfg, 0, sizeof(rcfg));
1654 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1655 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1656 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1657 rcfg.cb_ctx = wpa_s;
1658
1659 wps->registrar = wps_registrar_init(wps, &rcfg);
1660 if (wps->registrar == NULL) {
1661 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1662 os_free(wps);
1663 return -1;
1664 }
1665
1666 wpa_s->wps = wps;
1667
1668 return 0;
1669}
1670
1671
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001672#ifdef CONFIG_WPS_ER
1673static void wpas_wps_nfc_clear(struct wps_context *wps)
1674{
1675 wps->ap_nfc_dev_pw_id = 0;
1676 wpabuf_free(wps->ap_nfc_dh_pubkey);
1677 wps->ap_nfc_dh_pubkey = NULL;
1678 wpabuf_free(wps->ap_nfc_dh_privkey);
1679 wps->ap_nfc_dh_privkey = NULL;
1680 wpabuf_free(wps->ap_nfc_dev_pw);
1681 wps->ap_nfc_dev_pw = NULL;
1682}
1683#endif /* CONFIG_WPS_ER */
1684
1685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1687{
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001688 wpas_wps_assoc_with_cred_cancel(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001690 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001691 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001692 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001694#ifdef CONFIG_P2P
1695 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1696#endif /* CONFIG_P2P */
1697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 if (wpa_s->wps == NULL)
1699 return;
1700
1701#ifdef CONFIG_WPS_ER
1702 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1703 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001704 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705#endif /* CONFIG_WPS_ER */
1706
1707 wps_registrar_deinit(wpa_s->wps->registrar);
1708 wpabuf_free(wpa_s->wps->dh_pubkey);
1709 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001710 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 os_free(wpa_s->wps->network_key);
1712 os_free(wpa_s->wps);
1713 wpa_s->wps = NULL;
1714}
1715
1716
1717int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001718 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719{
1720 struct wpabuf *wps_ie;
1721
1722 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1723 return -1;
1724
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001725 wps_ie = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1727 if (!wps_ie) {
1728 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1729 return 0;
1730 }
1731
1732 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1733 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1734 "without active PBC Registrar");
1735 wpabuf_free(wps_ie);
1736 return 0;
1737 }
1738
1739 /* TODO: overlap detection */
1740 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1741 "(Active PBC)");
1742 wpabuf_free(wps_ie);
1743 return 1;
1744 }
1745
1746 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1747 if (!wps_ie) {
1748 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1749 return 0;
1750 }
1751
1752 /*
1753 * Start with WPS APs that advertise our address as an
1754 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1755 * allow any WPS AP after couple of scans since some APs do not
1756 * set Selected Registrar attribute properly when using
1757 * external Registrar.
1758 */
1759 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001760 struct os_reltime age;
1761
1762 os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1763
1764 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1765 age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1766 wpa_printf(MSG_DEBUG,
1767 " skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1768 wpa_s->scan_runs, (int) age.sec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001769 wpabuf_free(wps_ie);
1770 return 0;
1771 }
1772 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1773 } else {
1774 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1775 "(Authorized MAC or Active PIN)");
1776 }
1777 wpabuf_free(wps_ie);
1778 return 1;
1779 }
1780
1781 if (wps_ie) {
1782 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1783 wpabuf_free(wps_ie);
1784 return 1;
1785 }
1786
1787 return -1;
1788}
1789
1790
1791int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1792 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001793 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794{
1795 struct wpabuf *wps_ie = NULL;
1796 int ret = 0;
1797
1798 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001799 wps_ie = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1801 /* allow wildcard SSID for WPS PBC */
1802 ret = 1;
1803 }
1804 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001805 wps_ie = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 if (wps_ie &&
1807 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1808 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1809 /* allow wildcard SSID for WPS PIN */
1810 ret = 1;
1811 }
1812 }
1813
1814 if (!ret && ssid->bssid_set &&
1815 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1816 /* allow wildcard SSID due to hardcoded BSSID match */
1817 ret = 1;
1818 }
1819
1820#ifdef CONFIG_WPS_STRICT
1821 if (wps_ie) {
1822 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1823 0, bss->bssid) < 0)
1824 ret = 0;
1825 if (bss->beacon_ie_len) {
1826 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001827 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 bss, WPS_IE_VENDOR_TYPE);
1829 if (bcn_wps == NULL) {
1830 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1831 "missing from AP Beacon");
1832 ret = 0;
1833 } else {
1834 if (wps_validate_beacon(wps_ie) < 0)
1835 ret = 0;
1836 wpabuf_free(bcn_wps);
1837 }
1838 }
1839 }
1840#endif /* CONFIG_WPS_STRICT */
1841
1842 wpabuf_free(wps_ie);
1843
1844 return ret;
1845}
1846
1847
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001848static bool wpas_wps_is_pbc_overlap(struct wps_ap_info *ap,
1849 struct wpa_bss *selected,
1850 struct wpa_ssid *ssid,
1851 const u8 *sel_uuid)
1852{
1853 if (!ap->pbc_active ||
1854 os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
1855 return false;
1856
1857 if (!is_zero_ether_addr(ssid->bssid) &&
1858 os_memcmp(ap->bssid, ssid->bssid, ETH_ALEN) != 0) {
1859 wpa_printf(MSG_DEBUG, "WPS: Ignore another BSS " MACSTR
1860 " in active PBC mode due to local BSSID limitation",
1861 MAC2STR(ap->bssid));
1862 return 0;
1863 }
1864
1865 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: " MACSTR,
1866 MAC2STR(ap->bssid));
1867 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1868 ap->uuid, UUID_LEN);
1869 if (!sel_uuid || os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0)
1870 return true;
1871
1872 /* TODO: verify that this is reasonable dual-band situation */
1873
1874 return false;
1875}
1876
1877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1879 struct wpa_bss *selected, struct wpa_ssid *ssid)
1880{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001881 struct wpa_supplicant *iface;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001882 const u8 *sel_uuid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 struct wpabuf *wps_ie;
1884 int ret = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001885 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886
1887 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1888 return 0;
1889
1890 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1891 "present in scan results; selected BSSID " MACSTR,
1892 MAC2STR(selected->bssid));
Hai Shalomfdcde762020-04-02 11:19:20 -07001893 if (!is_zero_ether_addr(ssid->bssid))
1894 wpa_printf(MSG_DEBUG,
1895 "WPS: Network profile limited to accept only a single BSSID " MACSTR,
1896 MAC2STR(ssid->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897
1898 /* Make sure that only one AP is in active PBC mode */
1899 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1900 if (wps_ie) {
1901 sel_uuid = wps_get_uuid_e(wps_ie);
1902 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1903 sel_uuid, UUID_LEN);
1904 } else {
1905 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1906 "WPS IE?!");
1907 sel_uuid = NULL;
1908 }
1909
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001910 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
1911 for (i = 0; i < iface->num_wps_ap; i++) {
1912 struct wps_ap_info *ap = &iface->wps_ap[i];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001913
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001914 if (wpas_wps_is_pbc_overlap(ap, selected, ssid,
1915 sel_uuid)) {
1916 ret = 1; /* PBC overlap */
1917 wpa_msg(iface, MSG_INFO,
1918 "WPS: PBC overlap detected: "
1919 MACSTR " and " MACSTR,
1920 MAC2STR(selected->bssid),
1921 MAC2STR(ap->bssid));
1922 break;
1923 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001924 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 }
1926
1927 wpabuf_free(wps_ie);
1928
1929 return ret;
1930}
1931
1932
1933void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1934{
1935 struct wpa_bss *bss;
1936 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1937
1938 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1939 return;
1940
1941 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1942 struct wpabuf *ie;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001943
1944 ie = wpas_wps_get_wps_ie(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 if (!ie)
1946 continue;
1947 if (wps_is_selected_pbc_registrar(ie))
1948 pbc++;
1949 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1950 auth++;
1951 else if (wps_is_selected_pin_registrar(ie))
1952 pin++;
1953 else
1954 wps++;
1955 wpabuf_free(ie);
1956 }
1957
1958 if (pbc)
1959 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1960 else if (auth)
1961 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1962 else if (pin)
1963 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1964 else if (wps)
1965 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1966}
1967
1968
1969int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1970{
1971 struct wpa_ssid *ssid;
1972
1973 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1974 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1975 return 1;
1976 }
1977
1978 return 0;
1979}
1980
1981
1982int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1983 char *end)
1984{
1985 struct wpabuf *wps_ie;
1986 int ret;
1987
1988 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1989 if (wps_ie == NULL)
1990 return 0;
1991
1992 ret = wps_attr_text(wps_ie, buf, end);
1993 wpabuf_free(wps_ie);
1994 return ret;
1995}
1996
1997
1998int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1999{
2000#ifdef CONFIG_WPS_ER
2001 if (wpa_s->wps_er) {
2002 wps_er_refresh(wpa_s->wps_er);
2003 return 0;
2004 }
2005 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
2006 if (wpa_s->wps_er == NULL)
2007 return -1;
2008 return 0;
2009#else /* CONFIG_WPS_ER */
2010 return 0;
2011#endif /* CONFIG_WPS_ER */
2012}
2013
2014
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002015void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016{
2017#ifdef CONFIG_WPS_ER
2018 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
2019 wpa_s->wps_er = NULL;
2020#endif /* CONFIG_WPS_ER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021}
2022
2023
2024#ifdef CONFIG_WPS_ER
2025int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
2026 const char *uuid, const char *pin)
2027{
2028 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002029 const u8 *use_uuid = NULL;
2030 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002032 if (os_strcmp(uuid, "any") == 0) {
2033 } else if (uuid_str2bin(uuid, u) == 0) {
2034 use_uuid = u;
2035 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
2036 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
2037 if (use_uuid == NULL)
2038 return -1;
2039 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 return -1;
2041 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002042 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 (const u8 *) pin, os_strlen(pin), 300);
2044}
2045
2046
2047int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
2048{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002049 u8 u[UUID_LEN], *use_uuid = NULL;
2050 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002052 if (uuid_str2bin(uuid, u) == 0)
2053 use_uuid = u;
2054 else if (hwaddr_aton(uuid, addr) == 0)
2055 use_addr = addr;
2056 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002058 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059}
2060
2061
2062int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
2063 const char *pin)
2064{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002065 u8 u[UUID_LEN], *use_uuid = NULL;
2066 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002068 if (uuid_str2bin(uuid, u) == 0)
2069 use_uuid = u;
2070 else if (hwaddr_aton(uuid, addr) == 0)
2071 use_addr = addr;
2072 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002074
2075 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 os_strlen(pin));
2077}
2078
2079
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002080static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
2081 struct wps_credential *cred)
2082{
2083 os_memset(cred, 0, sizeof(*cred));
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002084 if (ssid->ssid_len > SSID_MAX_LEN)
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002085 return -1;
2086 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
2087 cred->ssid_len = ssid->ssid_len;
2088 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
2089 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
2090 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
2091 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
2092 cred->encr_type = WPS_ENCR_AES;
2093 else
2094 cred->encr_type = WPS_ENCR_TKIP;
2095 if (ssid->passphrase) {
2096 cred->key_len = os_strlen(ssid->passphrase);
2097 if (cred->key_len >= 64)
2098 return -1;
2099 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2100 } else if (ssid->psk_set) {
2101 cred->key_len = 32;
2102 os_memcpy(cred->key, ssid->psk, 32);
2103 } else
2104 return -1;
2105 } else {
2106 cred->auth_type = WPS_AUTH_OPEN;
2107 cred->encr_type = WPS_ENCR_NONE;
2108 }
2109
2110 return 0;
2111}
2112
2113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2115 int id)
2116{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002117 u8 u[UUID_LEN], *use_uuid = NULL;
2118 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 struct wpa_ssid *ssid;
2120 struct wps_credential cred;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002121 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002123 if (uuid_str2bin(uuid, u) == 0)
2124 use_uuid = u;
2125 else if (hwaddr_aton(uuid, addr) == 0)
2126 use_addr = addr;
2127 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128 return -1;
2129 ssid = wpa_config_get_network(wpa_s->conf, id);
2130 if (ssid == NULL || ssid->ssid == NULL)
2131 return -1;
2132
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002133 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002135 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2136 os_memset(&cred, 0, sizeof(cred));
2137 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138}
2139
2140
2141int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2142 const char *pin, struct wps_new_ap_settings *settings)
2143{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002144 u8 u[UUID_LEN], *use_uuid = NULL;
2145 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 struct wps_credential cred;
2147 size_t len;
2148
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002149 if (uuid_str2bin(uuid, u) == 0)
2150 use_uuid = u;
2151 else if (hwaddr_aton(uuid, addr) == 0)
2152 use_addr = addr;
2153 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 return -1;
2155 if (settings->ssid_hex == NULL || settings->auth == NULL ||
2156 settings->encr == NULL || settings->key_hex == NULL)
2157 return -1;
2158
2159 os_memset(&cred, 0, sizeof(cred));
2160 len = os_strlen(settings->ssid_hex);
2161 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2162 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2163 return -1;
2164 cred.ssid_len = len / 2;
2165
2166 len = os_strlen(settings->key_hex);
2167 if ((len & 1) || len > 2 * sizeof(cred.key) ||
2168 hexstr2bin(settings->key_hex, cred.key, len / 2))
2169 return -1;
2170 cred.key_len = len / 2;
2171
2172 if (os_strcmp(settings->auth, "OPEN") == 0)
2173 cred.auth_type = WPS_AUTH_OPEN;
2174 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2175 cred.auth_type = WPS_AUTH_WPAPSK;
2176 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2177 cred.auth_type = WPS_AUTH_WPA2PSK;
2178 else
2179 return -1;
2180
2181 if (os_strcmp(settings->encr, "NONE") == 0)
2182 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002183#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 else if (os_strcmp(settings->encr, "WEP") == 0)
2185 cred.encr_type = WPS_ENCR_WEP;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002186#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 else if (os_strcmp(settings->encr, "TKIP") == 0)
2188 cred.encr_type = WPS_ENCR_TKIP;
2189 else if (os_strcmp(settings->encr, "CCMP") == 0)
2190 cred.encr_type = WPS_ENCR_AES;
2191 else
2192 return -1;
2193
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002194 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2195 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196}
2197
2198
Dmitry Shmidt04949592012-07-19 12:16:46 -07002199#ifdef CONFIG_WPS_NFC
2200struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2201 int ndef, const char *uuid)
2202{
2203 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002204 u8 u[UUID_LEN], *use_uuid = NULL;
2205 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002206
2207 if (!wpa_s->wps_er)
2208 return NULL;
2209
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002210 if (uuid_str2bin(uuid, u) == 0)
2211 use_uuid = u;
2212 else if (hwaddr_aton(uuid, addr) == 0)
2213 use_addr = addr;
2214 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002215 return NULL;
2216
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002217 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002218 if (ndef && ret) {
2219 struct wpabuf *tmp;
2220 tmp = ndef_build_wifi(ret);
2221 wpabuf_free(ret);
2222 if (tmp == NULL)
2223 return NULL;
2224 ret = tmp;
2225 }
2226
2227 return ret;
2228}
2229#endif /* CONFIG_WPS_NFC */
2230
2231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232static int callbacks_pending = 0;
2233
2234static void wpas_wps_terminate_cb(void *ctx)
2235{
2236 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2237 if (--callbacks_pending <= 0)
2238 eloop_terminate();
2239}
2240#endif /* CONFIG_WPS_ER */
2241
2242
2243int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2244{
2245#ifdef CONFIG_WPS_ER
2246 if (wpa_s->wps_er) {
2247 callbacks_pending++;
2248 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2249 wpa_s->wps_er = NULL;
2250 return 1;
2251 }
2252#endif /* CONFIG_WPS_ER */
2253 return 0;
2254}
2255
2256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2258{
2259 struct wps_context *wps = wpa_s->wps;
2260
2261 if (wps == NULL)
2262 return;
2263
2264 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2265 wps->config_methods = wps_config_methods_str2bin(
2266 wpa_s->conf->config_methods);
2267 if ((wps->config_methods &
2268 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2269 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2270 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2271 "config methods are not allowed at the "
2272 "same time");
2273 wps->config_methods &= ~WPS_CONFIG_LABEL;
2274 }
2275 }
2276 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002277 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278
2279 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2280 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2281 WPS_DEV_TYPE_LEN);
2282
2283 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2284 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2285 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2286 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2287 }
2288
Dmitry Shmidt04949592012-07-19 12:16:46 -07002289 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2290 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2293 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2294
2295 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2296 wpas_wps_set_uuid(wpa_s, wps);
2297
2298 if (wpa_s->conf->changed_parameters &
2299 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2300 /* Update pointers to make sure they refer current values */
2301 wps->dev.device_name = wpa_s->conf->device_name;
2302 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2303 wps->dev.model_name = wpa_s->conf->model_name;
2304 wps->dev.model_number = wpa_s->conf->model_number;
2305 wps->dev.serial_number = wpa_s->conf->serial_number;
2306 }
2307}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002308
2309
Mikael Kanstrupcc779b82019-08-16 08:50:54 +02002310void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2311{
2312 struct wps_context *wps;
2313
2314 wps = wpa_s->wps;
2315 if (wps)
2316 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2317}
2318
2319
Dmitry Shmidt04949592012-07-19 12:16:46 -07002320#ifdef CONFIG_WPS_NFC
2321
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002322#ifdef CONFIG_WPS_ER
Roshan Pius8894db22017-02-13 14:43:20 -08002323struct wpabuf *
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002324wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2325 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002326{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002327 struct wpabuf *ret;
2328 struct wps_credential cred;
2329
2330 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2331 return NULL;
2332
2333 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2334
2335 if (ndef && ret) {
2336 struct wpabuf *tmp;
2337 tmp = ndef_build_wifi(ret);
2338 wpabuf_free(ret);
2339 if (tmp == NULL)
2340 return NULL;
2341 ret = tmp;
2342 }
2343
2344 return ret;
2345}
2346#endif /* CONFIG_WPS_ER */
2347
2348
2349struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2350 int ndef, const char *id_str)
2351{
2352#ifdef CONFIG_WPS_ER
2353 if (id_str) {
2354 int id;
2355 char *end = NULL;
2356 struct wpa_ssid *ssid;
2357
2358 id = strtol(id_str, &end, 10);
2359 if (end && *end)
2360 return NULL;
2361
2362 ssid = wpa_config_get_network(wpa_s->conf, id);
2363 if (ssid == NULL)
2364 return NULL;
2365 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2366 }
2367#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002368#ifdef CONFIG_AP
2369 if (wpa_s->ap_iface)
2370 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2371#endif /* CONFIG_AP */
2372 return NULL;
2373}
2374
2375
Dmitry Shmidt04949592012-07-19 12:16:46 -07002376struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2377{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002378 if (wpa_s->conf->wps_nfc_pw_from_config) {
2379 return wps_nfc_token_build(ndef,
2380 wpa_s->conf->wps_nfc_dev_pw_id,
2381 wpa_s->conf->wps_nfc_dh_pubkey,
2382 wpa_s->conf->wps_nfc_dev_pw);
2383 }
2384
Dmitry Shmidt04949592012-07-19 12:16:46 -07002385 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2386 &wpa_s->conf->wps_nfc_dh_pubkey,
2387 &wpa_s->conf->wps_nfc_dh_privkey,
2388 &wpa_s->conf->wps_nfc_dev_pw);
2389}
2390
2391
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002392int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2393 const u8 *bssid,
2394 const struct wpabuf *dev_pw, u16 dev_pw_id,
2395 int p2p_group, const u8 *peer_pubkey_hash,
2396 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002397{
2398 struct wps_context *wps = wpa_s->wps;
2399 char pw[32 * 2 + 1];
2400
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002401 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2402 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2403 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2404 }
2405
Dmitry Shmidt04949592012-07-19 12:16:46 -07002406 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002407 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2408 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2409 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002410 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002411 }
2412
2413 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2414 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2415 "cannot start NFC-triggered connection", dev_pw_id);
2416 return -1;
2417 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002418
2419 dh5_free(wps->dh_ctx);
2420 wpabuf_free(wps->dh_pubkey);
2421 wpabuf_free(wps->dh_privkey);
2422 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2423 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2424 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2425 wps->dh_ctx = NULL;
2426 wpabuf_free(wps->dh_pubkey);
2427 wps->dh_pubkey = NULL;
2428 wpabuf_free(wps->dh_privkey);
2429 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002430 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002431 return -1;
2432 }
2433 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002434 if (wps->dh_ctx == NULL) {
2435 wpabuf_free(wps->dh_pubkey);
2436 wps->dh_pubkey = NULL;
2437 wpabuf_free(wps->dh_privkey);
2438 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002439 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002440 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002441 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002442
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002443 if (dev_pw) {
2444 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2445 wpabuf_head(dev_pw),
2446 wpabuf_len(dev_pw));
2447 }
2448 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2449 dev_pw ? pw : NULL,
2450 p2p_group, dev_pw_id, peer_pubkey_hash,
2451 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002452}
2453
2454
2455static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2456 struct wps_parse_attr *attr)
2457{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002458 /*
2459 * Disable existing networks temporarily to allow the newly learned
2460 * credential to be preferred. Enable the temporarily disabled networks
2461 * after 10 seconds.
2462 */
2463 wpas_wps_temp_disable(wpa_s, NULL);
2464 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2465 NULL);
2466
Dmitry Shmidt04949592012-07-19 12:16:46 -07002467 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2468 return -1;
2469
2470 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2471 return 0;
2472
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002473 if (attr->ap_channel) {
2474 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002475 int freq = 0;
2476
2477 if (chan >= 1 && chan <= 13)
2478 freq = 2407 + 5 * chan;
2479 else if (chan == 14)
2480 freq = 2484;
2481 else if (chan >= 30)
2482 freq = 5000 + 5 * chan;
2483
2484 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002485 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2486 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002487 wpa_s->after_wps = 5;
2488 wpa_s->wps_freq = freq;
2489 }
2490 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002491
2492 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2493 "based on the received credential added");
2494 wpa_s->normal_scans = 0;
2495 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002496 wpa_s->disconnected = 0;
2497 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002498
2499 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002500 wpa_supplicant_req_scan(wpa_s, 0, 0);
2501
2502 return 0;
2503}
2504
2505
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002506#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002507static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2508 struct wps_parse_attr *attr)
2509{
2510 return wps_registrar_add_nfc_password_token(
2511 wpa_s->wps->registrar, attr->oob_dev_password,
2512 attr->oob_dev_password_len);
2513}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002514#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002515
2516
2517static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2518 const struct wpabuf *wps)
2519{
2520 struct wps_parse_attr attr;
2521
2522 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2523
2524 if (wps_parse_msg(wps, &attr)) {
2525 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2526 return -1;
2527 }
2528
2529 if (attr.num_cred)
2530 return wpas_wps_use_cred(wpa_s, &attr);
2531
2532#ifdef CONFIG_WPS_ER
2533 if (attr.oob_dev_password)
2534 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2535#endif /* CONFIG_WPS_ER */
2536
2537 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2538 return -1;
2539}
2540
2541
2542int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002543 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002544{
2545 const struct wpabuf *wps = data;
2546 struct wpabuf *tmp = NULL;
2547 int ret;
2548
2549 if (wpabuf_len(data) < 4)
2550 return -1;
2551
2552 if (*wpabuf_head_u8(data) != 0x10) {
2553 /* Assume this contains full NDEF record */
2554 tmp = ndef_parse_wifi(data);
2555 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002556#ifdef CONFIG_P2P
2557 tmp = ndef_parse_p2p(data);
2558 if (tmp) {
2559 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2560 forced_freq);
2561 wpabuf_free(tmp);
2562 return ret;
2563 }
2564#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002565 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2566 return -1;
2567 }
2568 wps = tmp;
2569 }
2570
2571 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2572 wpabuf_free(tmp);
2573 return ret;
2574}
2575
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002576
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002577struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2578 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002579{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002580 struct wpabuf *ret;
2581
2582 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2583 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2584 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2585 return NULL;
2586
2587 ret = wps_build_nfc_handover_req(wpa_s->wps,
2588 wpa_s->conf->wps_nfc_dh_pubkey);
2589
2590 if (ndef && ret) {
2591 struct wpabuf *tmp;
2592 tmp = ndef_build_wifi(ret);
2593 wpabuf_free(ret);
2594 if (tmp == NULL)
2595 return NULL;
2596 ret = tmp;
2597 }
2598
2599 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002600}
2601
2602
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002603#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002604
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002605static struct wpabuf *
2606wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2607 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002608{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002609#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002610 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002611 u8 u[UUID_LEN], *use_uuid = NULL;
2612 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002613 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002614
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002615 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002616 return NULL;
2617
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002618 if (uuid == NULL)
2619 return NULL;
2620 if (uuid_str2bin(uuid, u) == 0)
2621 use_uuid = u;
2622 else if (hwaddr_aton(uuid, addr) == 0)
2623 use_addr = addr;
2624 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002625 return NULL;
2626
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002627 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2628 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2629 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2630 return NULL;
2631 }
2632
2633 wpas_wps_nfc_clear(wps);
2634 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2635 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2636 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2637 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2638 wpas_wps_nfc_clear(wps);
2639 return NULL;
2640 }
2641
2642 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2643 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002644 if (ndef && ret) {
2645 struct wpabuf *tmp;
2646 tmp = ndef_build_wifi(ret);
2647 wpabuf_free(ret);
2648 if (tmp == NULL)
2649 return NULL;
2650 ret = tmp;
2651 }
2652
2653 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002654#else /* CONFIG_WPS_ER */
2655 return NULL;
2656#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002657}
2658#endif /* CONFIG_WPS_NFC */
2659
2660
2661struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2662 int ndef, int cr, const char *uuid)
2663{
2664 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002665 if (!cr)
2666 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002667 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2668 if (ret)
2669 return ret;
2670 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002671}
2672
2673
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002674static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2675 const struct wpabuf *data)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002676{
2677 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002678 int ret = -1;
2679 u16 wsc_len;
2680 const u8 *pos;
2681 struct wpabuf msg;
2682 struct wps_parse_attr attr;
2683 u16 dev_pw_id;
2684 const u8 *bssid = NULL;
2685 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002686
2687 wps = ndef_parse_wifi(data);
2688 if (wps == NULL)
2689 return -1;
2690 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2691 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002692 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2693 if (wpabuf_len(wps) < 2) {
2694 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2695 "Message");
2696 goto out;
2697 }
2698 pos = wpabuf_head(wps);
2699 wsc_len = WPA_GET_BE16(pos);
2700 if (wsc_len > wpabuf_len(wps) - 2) {
2701 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2702 "in Wi-Fi Handover Select Message", wsc_len);
2703 goto out;
2704 }
2705 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002706
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002707 wpa_hexdump(MSG_DEBUG,
2708 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2709 pos, wsc_len);
2710 if (wsc_len < wpabuf_len(wps) - 2) {
2711 wpa_hexdump(MSG_DEBUG,
2712 "WPS: Ignore extra data after WSC attributes",
2713 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2714 }
2715
2716 wpabuf_set(&msg, pos, wsc_len);
2717 ret = wps_parse_msg(&msg, &attr);
2718 if (ret < 0) {
2719 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2720 "Wi-Fi Handover Select Message");
2721 goto out;
2722 }
2723
2724 if (attr.oob_dev_password == NULL ||
2725 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2726 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2727 "included in Wi-Fi Handover Select Message");
2728 ret = -1;
2729 goto out;
2730 }
2731
2732 if (attr.ssid == NULL) {
2733 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2734 "Select Message");
2735 ret = -1;
2736 goto out;
2737 }
2738
2739 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2740
2741 if (attr.mac_addr) {
2742 bssid = attr.mac_addr;
2743 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2744 MAC2STR(bssid));
2745 }
2746
2747 if (attr.rf_bands)
2748 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2749
2750 if (attr.ap_channel) {
2751 u16 chan = WPA_GET_BE16(attr.ap_channel);
2752
2753 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2754
2755 if (chan >= 1 && chan <= 13 &&
2756 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2757 freq = 2407 + 5 * chan;
2758 else if (chan == 14 &&
2759 (attr.rf_bands == NULL ||
2760 *attr.rf_bands & WPS_RF_24GHZ))
2761 freq = 2484;
2762 else if (chan >= 30 &&
2763 (attr.rf_bands == NULL ||
2764 *attr.rf_bands & WPS_RF_50GHZ))
2765 freq = 5000 + 5 * chan;
Hai Shalomc3565922019-10-28 11:58:20 -07002766 else if (chan >= 1 && chan <= 6 &&
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002767 (attr.rf_bands == NULL ||
2768 *attr.rf_bands & WPS_RF_60GHZ))
2769 freq = 56160 + 2160 * chan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002770
2771 if (freq) {
2772 wpa_printf(MSG_DEBUG,
2773 "WPS: AP indicated channel %u -> %u MHz",
2774 chan, freq);
2775 }
2776 }
2777
2778 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2779 attr.oob_dev_password, attr.oob_dev_password_len);
2780 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2781 WPS_OOB_PUBKEY_HASH_LEN);
2782 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2783 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2784 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2785 ret = -1;
2786 goto out;
2787 }
2788 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2789 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2790
2791 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2792 attr.oob_dev_password,
2793 attr.ssid, attr.ssid_len, freq);
2794
2795out:
2796 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002797 return ret;
2798}
2799
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002800
2801int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2802 const struct wpabuf *req,
2803 const struct wpabuf *sel)
2804{
2805 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2806 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2807 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2808 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2809}
2810
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002811
2812int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2813 const struct wpabuf *req,
2814 const struct wpabuf *sel)
2815{
2816 struct wpabuf *wps;
2817 int ret = -1;
2818 u16 wsc_len;
2819 const u8 *pos;
2820 struct wpabuf msg;
2821 struct wps_parse_attr attr;
2822 u16 dev_pw_id;
2823
2824 /*
2825 * Enrollee/station is always initiator of the NFC connection handover,
2826 * so use the request message here to find Enrollee public key hash.
2827 */
2828 wps = ndef_parse_wifi(req);
2829 if (wps == NULL)
2830 return -1;
2831 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2832 "payload from NFC connection handover");
2833 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2834 if (wpabuf_len(wps) < 2) {
2835 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2836 "Message");
2837 goto out;
2838 }
2839 pos = wpabuf_head(wps);
2840 wsc_len = WPA_GET_BE16(pos);
2841 if (wsc_len > wpabuf_len(wps) - 2) {
2842 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2843 "in rt Wi-Fi Handover Request Message", wsc_len);
2844 goto out;
2845 }
2846 pos += 2;
2847
2848 wpa_hexdump(MSG_DEBUG,
2849 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2850 pos, wsc_len);
2851 if (wsc_len < wpabuf_len(wps) - 2) {
2852 wpa_hexdump(MSG_DEBUG,
2853 "WPS: Ignore extra data after WSC attributes",
2854 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2855 }
2856
2857 wpabuf_set(&msg, pos, wsc_len);
2858 ret = wps_parse_msg(&msg, &attr);
2859 if (ret < 0) {
2860 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2861 "Wi-Fi Handover Request Message");
2862 goto out;
2863 }
2864
2865 if (attr.oob_dev_password == NULL ||
2866 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2867 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2868 "included in Wi-Fi Handover Request Message");
2869 ret = -1;
2870 goto out;
2871 }
2872
2873 if (attr.uuid_e == NULL) {
2874 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2875 "Handover Request Message");
2876 ret = -1;
2877 goto out;
2878 }
2879
2880 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2881
2882 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2883 attr.oob_dev_password, attr.oob_dev_password_len);
2884 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2885 WPS_OOB_PUBKEY_HASH_LEN);
2886 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2887 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2888 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2889 ret = -1;
2890 goto out;
2891 }
2892 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2893 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2894
2895 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2896 attr.oob_dev_password,
2897 DEV_PW_NFC_CONNECTION_HANDOVER,
2898 NULL, 0, 1);
2899
2900out:
2901 wpabuf_free(wps);
2902 return ret;
2903}
2904
Dmitry Shmidt04949592012-07-19 12:16:46 -07002905#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002906
2907
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002908static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2909{
2910 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002911 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002912
2913 if (wpa_debug_level > MSG_DEBUG)
2914 return;
2915
2916 if (wpa_s->wps_ap == NULL)
2917 return;
2918
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002919 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002920
2921 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2922 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
Hai Shalom60840252021-02-19 19:02:11 -08002923 struct wpa_bssid_ignore *e = wpa_bssid_ignore_get(wpa_s,
2924 ap->bssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002925
2926 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
Hai Shalom60840252021-02-19 19:02:11 -08002927 "tries=%d last_attempt=%d sec ago bssid_ignore=%d",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002928 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2929 ap->last_attempt.sec > 0 ?
2930 (int) now.sec - (int) ap->last_attempt.sec : -1,
2931 e ? e->count : 0);
2932 }
2933}
2934
2935
2936static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2937 const u8 *bssid)
2938{
2939 size_t i;
2940
2941 if (wpa_s->wps_ap == NULL)
2942 return NULL;
2943
2944 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2945 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2946 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2947 return ap;
2948 }
2949
2950 return NULL;
2951}
2952
2953
2954static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2955 struct wpa_scan_res *res)
2956{
2957 struct wpabuf *wps;
2958 enum wps_ap_info_type type;
2959 struct wps_ap_info *ap;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002960 int r, pbc_active;
2961 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002962
2963 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2964 return;
2965
2966 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2967 if (wps == NULL)
2968 return;
2969
2970 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2971 if (r == 2)
2972 type = WPS_AP_SEL_REG_OUR;
2973 else if (r == 1)
2974 type = WPS_AP_SEL_REG;
2975 else
2976 type = WPS_AP_NOT_SEL_REG;
2977
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002978 uuid = wps_get_uuid_e(wps);
2979 pbc_active = wps_is_selected_pbc_registrar(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002980
2981 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2982 if (ap) {
2983 if (ap->type != type) {
2984 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2985 " changed type %d -> %d",
2986 MAC2STR(res->bssid), ap->type, type);
2987 ap->type = type;
2988 if (type != WPS_AP_NOT_SEL_REG)
Hai Shalom60840252021-02-19 19:02:11 -08002989 wpa_bssid_ignore_del(wpa_s, ap->bssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002990 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002991 ap->pbc_active = pbc_active;
2992 if (uuid)
2993 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2994 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002995 }
2996
2997 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2998 sizeof(struct wps_ap_info));
2999 if (ap == NULL)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003000 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003001
3002 wpa_s->wps_ap = ap;
3003 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
3004 wpa_s->num_wps_ap++;
3005
3006 os_memset(ap, 0, sizeof(*ap));
3007 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
3008 ap->type = type;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003009 ap->pbc_active = pbc_active;
3010 if (uuid)
3011 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003012 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
3013 MAC2STR(ap->bssid), ap->type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003014
3015out:
3016 wpabuf_free(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003017}
3018
3019
3020void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
3021 struct wpa_scan_results *scan_res)
3022{
3023 size_t i;
3024
3025 for (i = 0; i < scan_res->num; i++)
3026 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
3027
3028 wpas_wps_dump_ap_info(wpa_s);
3029}
3030
3031
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00003032bool wpas_wps_partner_link_scan_done(struct wpa_supplicant *wpa_s)
3033{
3034 struct wpa_global *global = wpa_s->global;
3035 struct wpa_supplicant *iface;
3036
3037 for (iface = global->ifaces; iface; iface = iface->next) {
3038 if (iface == wpa_s)
3039 continue;
3040
3041 if (!iface->supp_pbc_active)
3042 continue;
3043
3044 /* Scan results are available for both links. While the current
3045 * link will proceed for network selection, ensure the partner
3046 * link also gets an attempt at network selection and connect
3047 * with the selected BSS. */
3048 if (iface->wps_scan_done)
3049 wpa_wps_supplicant_fast_associate(iface);
3050 else
3051 return false;
3052 }
3053
3054 return true;
3055}
3056
3057
3058bool wpas_wps_partner_link_overlap_detect(struct wpa_supplicant *wpa_s)
3059{
3060 struct wpa_global *global = wpa_s->global;
3061 struct wpa_supplicant *iface;
3062
3063 for (iface = global->ifaces; iface; iface = iface->next) {
3064 if (iface == wpa_s)
3065 continue;
3066 if (iface->wps_overlap)
3067 return true;
3068 }
3069
3070 return false;
3071}
3072
3073
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003074void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
3075{
3076 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003077
3078 wpa_s->after_wps = 0;
3079
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003080 if (!wpa_s->wps_ap_iter)
3081 return;
3082 ap = wpas_wps_get_ap_info(wpa_s, bssid);
3083 if (ap == NULL)
3084 return;
3085 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003086 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003087}