blob: 1ba360e895c71beafd32be35b25eb03c49a9a0da [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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
83{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080084 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
85 return 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087 if (!wpa_s->wps_success &&
88 wpa_s->current_ssid &&
89 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
90 const u8 *bssid = wpa_s->bssid;
91 if (is_zero_ether_addr(bssid))
92 bssid = wpa_s->pending_bssid;
93
94 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
95 " did not succeed - continue trying to find "
96 "suitable AP", MAC2STR(bssid));
Hai Shalom60840252021-02-19 19:02:11 -080097 wpa_bssid_ignore_add(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098
99 wpa_supplicant_deauthenticate(wpa_s,
100 WLAN_REASON_DEAUTH_LEAVING);
101 wpa_s->reassociate = 1;
102 wpa_supplicant_req_scan(wpa_s,
Hai Shalom60840252021-02-19 19:02:11 -0800103 wpa_s->bssid_ignore_cleared ? 5 : 0, 0);
104 wpa_s->bssid_ignore_cleared = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 return 1;
106 }
107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700108 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
110 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
111 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
112
113 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
114 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
115 int disabled = wpa_s->current_ssid->disabled;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800116 unsigned int freq = wpa_s->assoc_freq;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700117 struct wpa_bss *bss;
118 struct wpa_ssid *ssid = NULL;
119 int use_fast_assoc = 0;
120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800122 "try to associate with the received credential "
123 "(freq=%u)", freq);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800124 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 wpa_supplicant_deauthenticate(wpa_s,
126 WLAN_REASON_DEAUTH_LEAVING);
127 if (disabled) {
128 wpa_printf(MSG_DEBUG, "WPS: Current network is "
129 "disabled - wait for user to enable");
130 return 1;
131 }
132 wpa_s->after_wps = 5;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800133 wpa_s->wps_freq = freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800134 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 wpa_s->reassociate = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700136
137 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
138 "without a new scan can be used");
139 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
140 if (bss) {
141 struct wpabuf *wps;
142 struct wps_parse_attr attr;
143
144 wps = wpa_bss_get_vendor_ie_multi(bss,
145 WPS_IE_VENDOR_TYPE);
146 if (wps && wps_parse_msg(wps, &attr) == 0 &&
147 attr.wps_state &&
148 *attr.wps_state == WPS_STATE_CONFIGURED)
149 use_fast_assoc = 1;
150 wpabuf_free(wps);
151 }
152
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700153 /*
154 * Complete the next step from an eloop timeout to allow pending
155 * driver events related to the disconnection to be processed
156 * first. This makes it less likely for disconnection event to
157 * cause problems with the following connection.
158 */
159 wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
160 wpas_wps_assoc_with_cred_cancel(wpa_s);
161 eloop_register_timeout(0, 10000,
162 wpas_wps_assoc_with_cred, wpa_s,
163 use_fast_assoc ? (void *) 1 :
164 (void *) 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 return 1;
166 }
167
168 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
169 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
170 "for external credential processing");
171 wpas_clear_wps(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800172 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 wpa_supplicant_deauthenticate(wpa_s,
174 WLAN_REASON_DEAUTH_LEAVING);
175 return 1;
176 }
177
178 return 0;
179}
180
181
182static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
183 struct wpa_ssid *ssid,
184 const struct wps_credential *cred)
185{
186 struct wpa_driver_capa capa;
187 struct wpa_bss *bss;
188 const u8 *ie;
189 struct wpa_ie_data adv;
190 int wpa2 = 0, ccmp = 0;
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700191 enum wpa_driver_if_type iftype;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192
193 /*
194 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
195 * case they are configured for mixed mode operation (WPA+WPA2 and
196 * TKIP+CCMP). Try to use scan results to figure out whether the AP
197 * actually supports stronger security and select that if the client
198 * has support for it, too.
199 */
200
201 if (wpa_drv_get_capa(wpa_s, &capa))
202 return; /* Unknown what driver supports */
203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800204 if (ssid->ssid == NULL)
205 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700207 if (!bss)
208 bss = wpa_bss_get(wpa_s, wpa_s->bssid,
209 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 if (bss == NULL) {
211 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
212 "table - use credential as-is");
213 return;
214 }
215
216 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
217
218 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
219 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
220 wpa2 = 1;
221 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
222 ccmp = 1;
223 } else {
224 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
225 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
226 adv.pairwise_cipher & WPA_CIPHER_CCMP)
227 ccmp = 1;
228 }
229
230 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
231 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
232 /*
233 * TODO: This could be the initial AP configuration and the
234 * Beacon contents could change shortly. Should request a new
235 * scan and delay addition of the network until the updated
236 * scan results are available.
237 */
238 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
239 "support - use credential as-is");
240 return;
241 }
242
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700243 iftype = ssid->p2p_group ? WPA_IF_P2P_CLIENT : WPA_IF_STATION;
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
246 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700247 (capa.key_mgmt_iftype[iftype] &
248 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
250 "based on scan results");
251 if (wpa_s->conf->ap_scan == 1)
252 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
253 else
254 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
255 }
256
257 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
258 (ssid->proto & WPA_PROTO_WPA) &&
259 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
260 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
261 "based on scan results");
262 if (wpa_s->conf->ap_scan == 1)
263 ssid->proto |= WPA_PROTO_RSN;
264 else
265 ssid->proto = WPA_PROTO_RSN;
266 }
267}
268
269
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700270static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
271 struct wpa_ssid *new_ssid)
272{
273 struct wpa_ssid *ssid, *next;
274
275 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
276 ssid = next, next = ssid ? ssid->next : NULL) {
277 /*
278 * new_ssid has already been added to the list in
279 * wpas_wps_add_network(), so skip it.
280 */
281 if (ssid == new_ssid)
282 continue;
283
284 if (ssid->bssid_set || new_ssid->bssid_set) {
285 if (ssid->bssid_set != new_ssid->bssid_set)
286 continue;
287 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
288 0)
289 continue;
290 }
291
292 /* compare SSID */
293 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
294 continue;
295
296 if (ssid->ssid && new_ssid->ssid) {
297 if (os_memcmp(ssid->ssid, new_ssid->ssid,
298 ssid->ssid_len) != 0)
299 continue;
300 } else if (ssid->ssid || new_ssid->ssid)
301 continue;
302
303 /* compare security parameters */
304 if (ssid->auth_alg != new_ssid->auth_alg ||
305 ssid->key_mgmt != new_ssid->key_mgmt ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800306 (ssid->group_cipher != new_ssid->group_cipher &&
307 !(ssid->group_cipher & new_ssid->group_cipher &
308 WPA_CIPHER_CCMP)))
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700309 continue;
310
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700311 /*
312 * Some existing WPS APs will send two creds in case they are
313 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
314 * Try to merge these two creds if they are received in the same
315 * M8 message.
316 */
317 if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
318 wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
319 if (new_ssid->passphrase && ssid->passphrase &&
320 os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
321 0) {
322 wpa_printf(MSG_DEBUG,
323 "WPS: M8 Creds with different passphrase - do not merge");
324 continue;
325 }
326
327 if (new_ssid->psk_set &&
328 (!ssid->psk_set ||
329 os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
330 wpa_printf(MSG_DEBUG,
331 "WPS: M8 Creds with different PSK - do not merge");
332 continue;
333 }
334
335 if ((new_ssid->passphrase && !ssid->passphrase) ||
336 (!new_ssid->passphrase && ssid->passphrase)) {
337 wpa_printf(MSG_DEBUG,
338 "WPS: M8 Creds with different passphrase/PSK type - do not merge");
339 continue;
340 }
341
342 wpa_printf(MSG_DEBUG,
343 "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
344 new_ssid->proto |= ssid->proto;
345 new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
346 } else {
347 /*
348 * proto and pairwise_cipher difference matter for
349 * non-mixed-mode creds.
350 */
351 if (ssid->proto != new_ssid->proto ||
352 ssid->pairwise_cipher != new_ssid->pairwise_cipher)
353 continue;
354 }
355
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700356 /* Remove the duplicated older network entry. */
357 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
358 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800359 if (wpa_s->current_ssid == ssid)
360 wpa_s->current_ssid = NULL;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700361 wpa_config_remove_network(wpa_s->conf, ssid->id);
362 }
363}
364
365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366static int wpa_supplicant_wps_cred(void *ctx,
367 const struct wps_credential *cred)
368{
369 struct wpa_supplicant *wpa_s = ctx;
370 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371 u16 auth_type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800372#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 int registrar = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Hai Shaloma20dcd72022-02-04 13:43:00 -0800375 bool add_sae;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376
377 if ((wpa_s->conf->wps_cred_processing == 1 ||
378 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
379 size_t blen = cred->cred_attr_len * 2 + 1;
380 char *buf = os_malloc(blen);
381 if (buf) {
382 wpa_snprintf_hex(buf, blen,
383 cred->cred_attr, cred->cred_attr_len);
384 wpa_msg(wpa_s, MSG_INFO, "%s%s",
385 WPS_EVENT_CRED_RECEIVED, buf);
386 os_free(buf);
387 }
388
389 wpas_notify_wps_credential(wpa_s, cred);
390 } else
391 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
392
393 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
394 cred->cred_attr, cred->cred_attr_len);
395
396 if (wpa_s->conf->wps_cred_processing == 1)
397 return 0;
398
399 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
400 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
401 cred->auth_type);
402 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
403 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
404 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
405 cred->key, cred->key_len);
406 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
407 MAC2STR(cred->mac_addr));
408
409 auth_type = cred->auth_type;
410 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
411 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
412 "auth_type into WPA2PSK");
413 auth_type = WPS_AUTH_WPA2PSK;
414 }
415
416 if (auth_type != WPS_AUTH_OPEN &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 auth_type != WPS_AUTH_WPAPSK &&
418 auth_type != WPS_AUTH_WPA2PSK) {
419 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
420 "unsupported authentication type 0x%x",
421 auth_type);
422 return 0;
423 }
424
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800425 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
426 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
427 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
428 "invalid Network Key length %lu",
429 (unsigned long) cred->key_len);
430 return -1;
431 }
432 }
433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
435 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
436 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800437#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438 if (ssid->eap.identity &&
439 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
440 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
441 WSC_ID_REGISTRAR_LEN) == 0)
442 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800443#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 os_free(ssid->eap.identity);
445 ssid->eap.identity = NULL;
446 ssid->eap.identity_len = 0;
447 os_free(ssid->eap.phase1);
448 ssid->eap.phase1 = NULL;
449 os_free(ssid->eap.eap_methods);
450 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700451 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700453 ssid->bssid_set = 0;
454 }
455 ssid->disabled_until.sec = 0;
456 ssid->disabled_until.usec = 0;
457 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 } else {
459 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
460 "received credential");
461 ssid = wpa_config_add_network(wpa_s->conf);
462 if (ssid == NULL)
463 return -1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700464 if (wpa_s->current_ssid) {
465 /*
466 * Should the GO issue multiple credentials for some
467 * reason, each credential should be marked as a
468 * temporary P2P group similarly to the one that gets
469 * marked as such based on the pre-configured values
470 * used for the WPS network block.
471 */
472 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
473 ssid->temporary = wpa_s->current_ssid->temporary;
474 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 wpas_notify_network_added(wpa_s, ssid);
476 }
477
478 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700479 ssid->wps_run = wpa_s->wps_run;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480
481 os_free(ssid->ssid);
482 ssid->ssid = os_malloc(cred->ssid_len);
483 if (ssid->ssid) {
484 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
485 ssid->ssid_len = cred->ssid_len;
486 }
487
488 switch (cred->encr_type) {
489 case WPS_ENCR_NONE:
490 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 case WPS_ENCR_TKIP:
Hai Shalomb755a2a2020-04-23 21:49:02 -0700492 ssid->pairwise_cipher = WPA_CIPHER_TKIP | WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 break;
494 case WPS_ENCR_AES:
495 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800496 if (wpa_s->drv_capa_known &&
497 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
498 ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
499 ssid->group_cipher |= WPA_CIPHER_GCMP;
500 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700501 if (wpa_s->drv_capa_known &&
502 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256)) {
503 ssid->pairwise_cipher |= WPA_CIPHER_GCMP_256;
504 ssid->group_cipher |= WPA_CIPHER_GCMP_256;
505 }
506 if (wpa_s->drv_capa_known &&
507 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256)) {
508 ssid->pairwise_cipher |= WPA_CIPHER_CCMP_256;
509 ssid->group_cipher |= WPA_CIPHER_CCMP_256;
510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 break;
512 }
513
514 switch (auth_type) {
515 case WPS_AUTH_OPEN:
516 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
517 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
518 ssid->proto = 0;
519#ifdef CONFIG_WPS_REG_DISABLE_OPEN
520 if (registrar) {
521 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
522 "id=%d - Credentials for an open "
523 "network disabled by default - use "
524 "'select_network %d' to enable",
525 ssid->id, ssid->id);
526 ssid->disabled = 1;
527 }
528#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
529 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530 case WPS_AUTH_WPAPSK:
531 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
532 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700533 ssid->proto = WPA_PROTO_WPA | WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 case WPS_AUTH_WPA2PSK:
536 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
537 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800538 add_sae = wpa_s->conf->wps_cred_add_sae;
539#ifdef CONFIG_P2P
540 if (ssid->p2p_group && is_p2p_6ghz_capable(wpa_s->global->p2p))
541 add_sae = true;
542#endif /* CONFIG_P2P */
543 if (add_sae && cred->key_len != 2 * PMK_LEN) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700544 ssid->auth_alg = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700545 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
Hai Shalom021b0b52019-04-10 11:17:58 -0700546 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
Hai Shalom021b0b52019-04-10 11:17:58 -0700547 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548 ssid->proto = WPA_PROTO_RSN;
549 break;
550 }
551
Hai Shalom021b0b52019-04-10 11:17:58 -0700552 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 if (cred->key_len == 2 * PMK_LEN) {
554 if (hexstr2bin((const char *) cred->key, ssid->psk,
555 PMK_LEN)) {
556 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
557 "Key");
558 return -1;
559 }
560 ssid->psk_set = 1;
561 ssid->export_keys = 1;
562 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
563 os_free(ssid->passphrase);
564 ssid->passphrase = os_malloc(cred->key_len + 1);
565 if (ssid->passphrase == NULL)
566 return -1;
567 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
568 ssid->passphrase[cred->key_len] = '\0';
569 wpa_config_update_psk(ssid);
570 ssid->export_keys = 1;
571 } else {
572 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
573 "length %lu",
574 (unsigned long) cred->key_len);
575 return -1;
576 }
577 }
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700578 ssid->priority = wpa_s->conf->wps_priority;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579
580 wpas_wps_security_workaround(wpa_s, ssid, cred);
581
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700582 wpas_wps_remove_dup_network(wpa_s, ssid);
583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584#ifndef CONFIG_NO_CONFIG_WRITE
585 if (wpa_s->conf->update_config &&
586 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
587 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
588 return -1;
589 }
590#endif /* CONFIG_NO_CONFIG_WRITE */
591
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700592 if (ssid->priority)
593 wpa_config_update_prio_list(wpa_s->conf);
594
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800595 /*
596 * Optimize the post-WPS scan based on the channel used during
597 * the provisioning in case EAP-Failure is not received.
598 */
599 wpa_s->after_wps = 5;
600 wpa_s->wps_freq = wpa_s->assoc_freq;
601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602 return 0;
603}
604
605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
607 struct wps_event_m2d *m2d)
608{
609 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
610 "dev_password_id=%d config_error=%d",
611 m2d->dev_password_id, m2d->config_error);
612 wpas_notify_wps_event_m2d(wpa_s, m2d);
613#ifdef CONFIG_P2P
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800614 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
615 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 "dev_password_id=%d config_error=%d",
617 m2d->dev_password_id, m2d->config_error);
618 }
619 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
620 /*
621 * Notify P2P from eloop timeout to avoid issues with the
622 * interface getting removed while processing a message.
623 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700624 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 NULL);
626 }
627#endif /* CONFIG_P2P */
628}
629
630
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700631static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
632{
633 struct wpa_supplicant *wpa_s = eloop_ctx;
634 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
635 wpas_clear_wps(wpa_s);
636}
637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638
639static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
640 struct wps_event_fail *fail)
641{
642 if (fail->error_indication > 0 &&
643 fail->error_indication < NUM_WPS_EI_VALUES) {
644 wpa_msg(wpa_s, MSG_INFO,
645 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
646 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700647 wps_ei_str(fail->error_indication));
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800648 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
649 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 "msg=%d config_error=%d reason=%d (%s)",
651 fail->msg, fail->config_error,
652 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700653 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 } else {
655 wpa_msg(wpa_s, MSG_INFO,
656 WPS_EVENT_FAIL "msg=%d config_error=%d",
657 fail->msg, fail->config_error);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800658 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
659 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660 "msg=%d config_error=%d",
661 fail->msg, fail->config_error);
662 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700663
664 /*
665 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
666 */
667 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
668 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
669 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700672 wpas_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673}
674
675
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800676static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
677
678static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
679{
680 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800681 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800682
683 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
684
685 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
686 if (ssid->disabled_for_connect && ssid->disabled) {
687 ssid->disabled_for_connect = 0;
688 ssid->disabled = 0;
689 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800690 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800691 }
692 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800693
694 if (changed) {
695#ifndef CONFIG_NO_CONFIG_WRITE
696 if (wpa_s->conf->update_config &&
697 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
698 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
699 "configuration");
700 }
701#endif /* CONFIG_NO_CONFIG_WRITE */
702 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800703}
704
705
706static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
707{
708 struct wpa_supplicant *wpa_s = eloop_ctx;
709 /* Enable the networks disabled during wpas_wps_reassoc */
710 wpas_wps_reenable_networks(wpa_s);
711}
712
713
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800714int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
715{
716 return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
717 wpa_s, NULL);
718}
719
720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
722{
723 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
724 wpa_s->wps_success = 1;
725 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700726 if (wpa_s->current_ssid)
727 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
Hai Shalom899fcc72020-10-19 14:38:18 -0700728 wpa_s->consecutive_conn_failures = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800729
730 /*
731 * Enable the networks disabled during wpas_wps_reassoc after 10
732 * seconds. The 10 seconds timer is to allow the data connection to be
733 * formed before allowing other networks to be selected.
734 */
735 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
736 NULL);
737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739}
740
741
742static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
743 struct wps_event_er_ap *ap)
744{
745 char uuid_str[100];
746 char dev_type[WPS_DEV_TYPE_BUFSIZE];
747
748 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
749 if (ap->pri_dev_type)
750 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
751 sizeof(dev_type));
752 else
753 dev_type[0] = '\0';
754
755 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
756 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
757 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
758 ap->friendly_name ? ap->friendly_name : "",
759 ap->manufacturer ? ap->manufacturer : "",
760 ap->model_description ? ap->model_description : "",
761 ap->model_name ? ap->model_name : "",
762 ap->manufacturer_url ? ap->manufacturer_url : "",
763 ap->model_url ? ap->model_url : "");
764}
765
766
767static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
768 struct wps_event_er_ap *ap)
769{
770 char uuid_str[100];
771 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
772 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
773}
774
775
776static void wpa_supplicant_wps_event_er_enrollee_add(
777 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
778{
779 char uuid_str[100];
780 char dev_type[WPS_DEV_TYPE_BUFSIZE];
781
782 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
783 if (enrollee->pri_dev_type)
784 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
785 sizeof(dev_type));
786 else
787 dev_type[0] = '\0';
788
789 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
790 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
791 "|%s|%s|%s|%s|%s|",
792 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
793 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
794 enrollee->dev_name ? enrollee->dev_name : "",
795 enrollee->manufacturer ? enrollee->manufacturer : "",
796 enrollee->model_name ? enrollee->model_name : "",
797 enrollee->model_number ? enrollee->model_number : "",
798 enrollee->serial_number ? enrollee->serial_number : "");
799}
800
801
802static void wpa_supplicant_wps_event_er_enrollee_remove(
803 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
804{
805 char uuid_str[100];
806 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
807 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
808 uuid_str, MAC2STR(enrollee->mac_addr));
809}
810
811
812static void wpa_supplicant_wps_event_er_ap_settings(
813 struct wpa_supplicant *wpa_s,
814 struct wps_event_er_ap_settings *ap_settings)
815{
816 char uuid_str[100];
817 char key_str[65];
818 const struct wps_credential *cred = ap_settings->cred;
819
820 key_str[0] = '\0';
821 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
822 if (cred->key_len >= 8 && cred->key_len <= 64) {
823 os_memcpy(key_str, cred->key, cred->key_len);
824 key_str[cred->key_len] = '\0';
825 }
826 }
827
828 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
829 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
830 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
831 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
832 "key=%s",
833 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
834 cred->auth_type, cred->encr_type, key_str);
835}
836
837
838static void wpa_supplicant_wps_event_er_set_sel_reg(
839 struct wpa_supplicant *wpa_s,
840 struct wps_event_er_set_selected_registrar *ev)
841{
842 char uuid_str[100];
843
844 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
845 switch (ev->state) {
846 case WPS_ER_SET_SEL_REG_START:
847 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
848 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
849 "sel_reg_config_methods=0x%x",
850 uuid_str, ev->sel_reg, ev->dev_passwd_id,
851 ev->sel_reg_config_methods);
852 break;
853 case WPS_ER_SET_SEL_REG_DONE:
854 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
855 "uuid=%s state=DONE", uuid_str);
856 break;
857 case WPS_ER_SET_SEL_REG_FAILED:
858 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
859 "uuid=%s state=FAILED", uuid_str);
860 break;
861 }
862}
863
864
865static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
866 union wps_event_data *data)
867{
868 struct wpa_supplicant *wpa_s = ctx;
869 switch (event) {
870 case WPS_EV_M2D:
871 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
872 break;
873 case WPS_EV_FAIL:
874 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
875 break;
876 case WPS_EV_SUCCESS:
877 wpa_supplicant_wps_event_success(wpa_s);
878 break;
879 case WPS_EV_PWD_AUTH_FAIL:
880#ifdef CONFIG_AP
881 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
882 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
883#endif /* CONFIG_AP */
884 break;
885 case WPS_EV_PBC_OVERLAP:
886 break;
887 case WPS_EV_PBC_TIMEOUT:
888 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700889 case WPS_EV_PBC_ACTIVE:
890 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
891 break;
892 case WPS_EV_PBC_DISABLE:
893 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
894 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 case WPS_EV_ER_AP_ADD:
896 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
897 break;
898 case WPS_EV_ER_AP_REMOVE:
899 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
900 break;
901 case WPS_EV_ER_ENROLLEE_ADD:
902 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
903 &data->enrollee);
904 break;
905 case WPS_EV_ER_ENROLLEE_REMOVE:
906 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
907 &data->enrollee);
908 break;
909 case WPS_EV_ER_AP_SETTINGS:
910 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
911 &data->ap_settings);
912 break;
913 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
914 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
915 &data->set_sel_reg);
916 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800917 case WPS_EV_AP_PIN_SUCCESS:
918 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 }
920}
921
922
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700923static int wpa_supplicant_wps_rf_band(void *ctx)
924{
925 struct wpa_supplicant *wpa_s = ctx;
926
927 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
928 return 0;
929
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700930 return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
931 (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700932}
933
934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
936{
937 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
938 eap_is_wps_pin_enrollee(&ssid->eap))
939 return WPS_REQ_ENROLLEE;
940 else
941 return WPS_REQ_REGISTRAR;
942}
943
944
945static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
946{
947 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700948 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
949
Dmitry Shmidt051af732013-10-22 13:52:46 -0700950 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700951 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700952
Jouni Malinen75ecf522011-06-27 15:19:46 -0700953 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800955 /* Enable the networks disabled during wpas_wps_reassoc */
956 wpas_wps_reenable_networks(wpa_s);
957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800959 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960
961 /* Remove any existing WPS network from configuration */
962 ssid = wpa_s->conf->ssid;
963 while (ssid) {
964 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
965 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800966 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700967 wpa_supplicant_deauthenticate(
968 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 }
970 id = ssid->id;
971 remove_ssid = ssid;
972 } else
973 id = -1;
974 ssid = ssid->next;
975 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700976 if (prev_current == remove_ssid) {
977 wpa_sm_set_config(wpa_s->wpa, NULL);
978 eapol_sm_notify_config(wpa_s->eapol, NULL,
979 NULL);
980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 wpas_notify_network_removed(wpa_s, remove_ssid);
982 wpa_config_remove_network(wpa_s->conf, id);
983 }
984 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700985
986 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987}
988
989
990static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
991{
992 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800993 union wps_event_data data;
994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
996 "out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800997 os_memset(&data, 0, sizeof(data));
998 data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
999 data.fail.error_indication = WPS_EI_NO_ERROR;
1000 /*
1001 * Call wpas_notify_wps_event_fail() directly instead of through
1002 * wpa_supplicant_wps_event() which would end up registering unnecessary
1003 * timeouts (those are only for the case where the failure happens
1004 * during an EAP-WSC exchange).
1005 */
1006 wpas_notify_wps_event_fail(wpa_s, &data.fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 wpas_clear_wps(wpa_s);
1008}
1009
1010
1011static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001012 int registrar, const u8 *dev_addr,
1013 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014{
1015 struct wpa_ssid *ssid;
1016
1017 ssid = wpa_config_add_network(wpa_s->conf);
1018 if (ssid == NULL)
1019 return NULL;
1020 wpas_notify_network_added(wpa_s, ssid);
1021 wpa_config_set_network_defaults(ssid);
1022 ssid->temporary = 1;
1023 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
1024 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
1025 wpa_config_set(ssid, "identity", registrar ?
1026 "\"" WSC_ID_REGISTRAR "\"" :
1027 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1028 wpas_notify_network_removed(wpa_s, ssid);
1029 wpa_config_remove_network(wpa_s->conf, ssid->id);
1030 return NULL;
1031 }
1032
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001033#ifdef CONFIG_P2P
1034 if (dev_addr)
1035 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1036#endif /* CONFIG_P2P */
1037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 if (bssid) {
1039#ifndef CONFIG_P2P
1040 struct wpa_bss *bss;
1041 int count = 0;
1042#endif /* CONFIG_P2P */
1043
1044 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1045 ssid->bssid_set = 1;
1046
1047 /*
1048 * Note: With P2P, the SSID may change at the time the WPS
1049 * provisioning is started, so better not filter the AP based
1050 * on the current SSID in the scan results.
1051 */
1052#ifndef CONFIG_P2P
1053 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1054 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
1055 continue;
1056
1057 os_free(ssid->ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001058 ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 if (ssid->ssid == NULL)
1060 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 ssid->ssid_len = bss->ssid_len;
1062 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1063 "scan results",
1064 ssid->ssid, ssid->ssid_len);
1065 count++;
1066 }
1067
1068 if (count > 1) {
1069 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1070 "for the AP; use wildcard");
1071 os_free(ssid->ssid);
1072 ssid->ssid = NULL;
1073 ssid->ssid_len = 0;
1074 }
1075#endif /* CONFIG_P2P */
1076 }
1077
1078 return ssid;
1079}
1080
1081
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001082static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1083 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084{
1085 struct wpa_ssid *ssid;
1086
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001087 if (wpa_s->current_ssid) {
1088 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001089 wpa_supplicant_deauthenticate(
1090 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001091 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 /* Mark all other networks disabled and trigger reassociation */
1094 ssid = wpa_s->conf->ssid;
1095 while (ssid) {
1096 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001097 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001098 /*
1099 * In case the network object corresponds to a persistent group
1100 * then do not send out network disabled signal. In addition,
1101 * do not change disabled status of persistent network objects
1102 * from 2 to 1 should we connect to another network.
1103 */
1104 if (was_disabled != 2) {
1105 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001106 if (was_disabled != ssid->disabled) {
1107 if (ssid->disabled)
1108 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001109 wpas_notify_network_enabled_changed(wpa_s,
1110 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001111 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 ssid = ssid->next;
1114 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001115}
1116
1117
1118static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001119 struct wpa_ssid *selected, const u8 *bssid,
1120 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001121{
1122 struct wpa_bss *bss;
1123
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001124 wpa_s->wps_run++;
1125 if (wpa_s->wps_run == 0)
1126 wpa_s->wps_run++;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001127 wpa_s->after_wps = 0;
1128 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001129 if (freq) {
1130 wpa_s->after_wps = 5;
1131 wpa_s->wps_freq = freq;
1132 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001133 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1134 if (bss && bss->freq > 0) {
1135 wpa_s->known_wps_freq = 1;
1136 wpa_s->wps_freq = bss->freq;
1137 }
1138 }
1139
1140 wpas_wps_temp_disable(wpa_s, selected);
1141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 wpa_s->disconnected = 0;
1143 wpa_s->reassociate = 1;
1144 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001145 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 wpa_s->wps_success = 0;
Hai Shalom60840252021-02-19 19:02:11 -08001147 wpa_s->bssid_ignore_cleared = false;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001148
1149 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 wpa_supplicant_req_scan(wpa_s, 0, 0);
1151}
1152
1153
1154int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
Hai Shalom021b0b52019-04-10 11:17:58 -07001155 int p2p_group, int multi_ap_backhaul_sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156{
1157 struct wpa_ssid *ssid;
Hai Shalom021b0b52019-04-10 11:17:58 -07001158 char phase1[32];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001159
1160#ifdef CONFIG_AP
1161 if (wpa_s->ap_iface) {
1162 wpa_printf(MSG_DEBUG,
1163 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1164 return -1;
1165 }
1166#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001168 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (ssid == NULL)
1170 return -1;
1171 ssid->temporary = 1;
1172 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001173 /*
1174 * When starting a regular WPS process (not P2P group formation)
1175 * the registrar/final station can be either AP or PCP
1176 * so use a "don't care" value for the pbss flag.
1177 */
1178 if (!p2p_group)
1179 ssid->pbss = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180#ifdef CONFIG_P2P
1181 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1182 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1183 if (ssid->ssid) {
1184 ssid->ssid_len = wpa_s->go_params->ssid_len;
1185 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1186 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001187 if (wpa_s->go_params->freq > 56160) {
1188 /* P2P in 60 GHz uses PBSS */
1189 ssid->pbss = 1;
1190 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001191 if (wpa_s->go_params->edmg &&
1192 wpas_p2p_try_edmg_channel(wpa_s,
1193 wpa_s->go_params) == 0)
1194 ssid->enable_edmg = 1;
1195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1197 "SSID", ssid->ssid, ssid->ssid_len);
1198 }
1199 }
1200#endif /* CONFIG_P2P */
Hai Shalom021b0b52019-04-10 11:17:58 -07001201 os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
1202 multi_ap_backhaul_sta ? " multi_ap=1" : "");
1203 if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001204 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 if (wpa_s->wps_fragment_size)
1206 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Hai Shalom021b0b52019-04-10 11:17:58 -07001207 if (multi_ap_backhaul_sta)
1208 ssid->multi_ap_backhaul_sta = 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001209 wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1211 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001212 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 return 0;
1214}
1215
1216
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001217static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1218 const u8 *dev_addr, const u8 *bssid,
1219 const char *pin, int p2p_group, u16 dev_pw_id,
1220 const u8 *peer_pubkey_hash,
1221 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222{
1223 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001224 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001226 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001228#ifdef CONFIG_AP
1229 if (wpa_s->ap_iface) {
1230 wpa_printf(MSG_DEBUG,
1231 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1232 return -1;
1233 }
1234#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001236 if (bssid && is_zero_ether_addr(bssid))
1237 bssid = NULL;
1238 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1239 if (ssid == NULL) {
1240 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001242 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 ssid->temporary = 1;
1244 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001245 /*
1246 * When starting a regular WPS process (not P2P group formation)
1247 * the registrar/final station can be either AP or PCP
1248 * so use a "don't care" value for the pbss flag.
1249 */
1250 if (!p2p_group)
1251 ssid->pbss = 2;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001252 if (ssid_val) {
1253 ssid->ssid = os_malloc(ssid_len);
1254 if (ssid->ssid) {
1255 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1256 ssid->ssid_len = ssid_len;
1257 }
1258 }
1259 if (peer_pubkey_hash) {
1260 os_memcpy(hash, " pkhash=", 8);
1261 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1262 peer_pubkey_hash,
1263 WPS_OOB_PUBKEY_HASH_LEN);
1264 } else {
1265 hash[0] = '\0';
1266 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267#ifdef CONFIG_P2P
1268 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001269 os_free(ssid->ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1271 if (ssid->ssid) {
1272 ssid->ssid_len = wpa_s->go_params->ssid_len;
1273 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1274 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001275 if (wpa_s->go_params->freq > 56160) {
1276 /* P2P in 60 GHz uses PBSS */
1277 ssid->pbss = 1;
1278 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001279 if (wpa_s->go_params->edmg &&
1280 wpas_p2p_try_edmg_channel(wpa_s,
1281 wpa_s->go_params) == 0)
1282 ssid->enable_edmg = 1;
1283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1285 "SSID", ssid->ssid, ssid->ssid_len);
1286 }
1287 }
1288#endif /* CONFIG_P2P */
1289 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001290 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1291 pin, dev_pw_id, hash);
1292 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1293 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1294 dev_pw_id, hash);
1295 } else {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001296 if (wps_generate_pin(&rpin) < 0) {
1297 wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1298 return -1;
1299 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001300 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1301 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001303 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1304 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001305 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001306 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001307
1308 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER)
1309 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_PIN_ACTIVE);
1310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 if (wpa_s->wps_fragment_size)
1312 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1313 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1314 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001315 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001316 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 return rpin;
1318}
1319
1320
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001321int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1322 const char *pin, int p2p_group, u16 dev_pw_id)
1323{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001324 os_get_reltime(&wpa_s->wps_pin_start_time);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001325 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1326 dev_pw_id, NULL, NULL, 0, 0);
1327}
1328
1329
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001330void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1331{
1332 union wps_event_data data;
1333
1334 os_memset(&data, 0, sizeof(data));
1335 data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1336 data.fail.error_indication = WPS_EI_NO_ERROR;
1337 /*
1338 * Call wpas_notify_wps_event_fail() directly instead of through
1339 * wpa_supplicant_wps_event() which would end up registering unnecessary
1340 * timeouts (those are only for the case where the failure happens
1341 * during an EAP-WSC exchange).
1342 */
1343 wpas_notify_wps_event_fail(wpa_s, &data.fail);
1344}
1345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346/* Cancel the wps pbc/pin requests */
1347int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1348{
1349#ifdef CONFIG_AP
1350 if (wpa_s->ap_iface) {
1351 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1352 return wpa_supplicant_ap_wps_cancel(wpa_s);
1353 }
1354#endif /* CONFIG_AP */
1355
Dmitry Shmidt04949592012-07-19 12:16:46 -07001356 if (wpa_s->wpa_state == WPA_SCANNING ||
1357 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1359 wpa_supplicant_cancel_scan(wpa_s);
1360 wpas_clear_wps(wpa_s);
1361 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1362 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1363 "deauthenticate");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001364 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 wpa_supplicant_deauthenticate(wpa_s,
1366 WLAN_REASON_DEAUTH_LEAVING);
1367 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001368 } else {
1369 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001370 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001371 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1372 0)
1373 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 }
1375
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001376 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CANCEL);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001377 wpa_s->after_wps = 0;
1378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 return 0;
1380}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001381
1382
1383int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1384 const char *pin, struct wps_new_ap_settings *settings)
1385{
1386 struct wpa_ssid *ssid;
1387 char val[200];
1388 char *pos, *end;
1389 int res;
1390
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001391#ifdef CONFIG_AP
1392 if (wpa_s->ap_iface) {
1393 wpa_printf(MSG_DEBUG,
1394 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1395 return -1;
1396 }
1397#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 if (!pin)
1399 return -1;
1400 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001401 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 if (ssid == NULL)
1403 return -1;
1404 ssid->temporary = 1;
1405 pos = val;
1406 end = pos + sizeof(val);
1407 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001408 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409 return -1;
1410 pos += res;
1411 if (settings) {
1412 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1413 "new_encr=%s new_key=%s",
1414 settings->ssid_hex, settings->auth,
1415 settings->encr, settings->key_hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001416 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 return -1;
1418 pos += res;
1419 }
1420 res = os_snprintf(pos, end - pos, "\"");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001421 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001423 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1424 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 if (wpa_s->wps_fragment_size)
1426 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1427 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1428 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001429 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 return 0;
1431}
1432
1433
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001434static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1435 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436 size_t psk_len)
1437{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001438 if (is_zero_ether_addr(p2p_dev_addr)) {
1439 wpa_printf(MSG_DEBUG,
1440 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1441 MAC2STR(mac_addr));
1442 } else {
1443 wpa_printf(MSG_DEBUG,
1444 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1445 " P2P Device Addr " MACSTR,
1446 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1449
1450 /* TODO */
1451
1452 return 0;
1453}
1454
1455
1456static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1457 const struct wps_device_data *dev)
1458{
1459 char uuid[40], txt[400];
1460 int len;
1461 char devtype[WPS_DEV_TYPE_BUFSIZE];
1462 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1463 return;
1464 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1465 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1466 " [%s|%s|%s|%s|%s|%s]",
1467 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1468 dev->manufacturer, dev->model_name,
1469 dev->model_number, dev->serial_number,
1470 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1471 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001472 if (!os_snprintf_error(sizeof(txt), len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 wpa_printf(MSG_INFO, "%s", txt);
1474}
1475
1476
1477static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1478 u16 sel_reg_config_methods)
1479{
1480#ifdef CONFIG_WPS_ER
1481 struct wpa_supplicant *wpa_s = ctx;
1482
1483 if (wpa_s->wps_er == NULL)
1484 return;
1485 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1486 "dev_password_id=%u sel_reg_config_methods=0x%x",
1487 sel_reg, dev_passwd_id, sel_reg_config_methods);
1488 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1489 sel_reg_config_methods);
1490#endif /* CONFIG_WPS_ER */
1491}
1492
1493
1494static u16 wps_fix_config_methods(u16 config_methods)
1495{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 if ((config_methods &
1497 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1498 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1499 wpa_printf(MSG_INFO, "WPS: Converting display to "
1500 "virtual_display for WPS 2.0 compliance");
1501 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1502 }
1503 if ((config_methods &
1504 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1505 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1506 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1507 "virtual_push_button for WPS 2.0 compliance");
1508 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510
1511 return config_methods;
1512}
1513
1514
1515static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1516 struct wps_context *wps)
1517{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001518 char buf[50];
1519 const char *src;
1520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521 if (is_nil_uuid(wpa_s->conf->uuid)) {
1522 struct wpa_supplicant *first;
1523 first = wpa_s->global->ifaces;
1524 while (first && first->next)
1525 first = first->next;
1526 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001527 if (wps != wpa_s->global->ifaces->wps)
1528 os_memcpy(wps->uuid,
1529 wpa_s->global->ifaces->wps->uuid,
1530 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001531 src = "from the first interface";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001532 } else if (wpa_s->conf->auto_uuid == 1) {
1533 uuid_random(wps->uuid);
1534 src = "based on random data";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 } else {
1536 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001537 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538 }
1539 } else {
1540 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001541 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001543
1544 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1545 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546}
1547
1548
Dmitry Shmidt04949592012-07-19 12:16:46 -07001549static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1550 struct wps_context *wps)
1551{
1552 wpabuf_free(wps->dev.vendor_ext_m1);
1553 wps->dev.vendor_ext_m1 = NULL;
1554
1555 if (wpa_s->conf->wps_vendor_ext_m1) {
1556 wps->dev.vendor_ext_m1 =
1557 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1558 if (!wps->dev.vendor_ext_m1) {
1559 wpa_printf(MSG_ERROR, "WPS: Cannot "
1560 "allocate memory for vendor_ext_m1");
1561 }
1562 }
1563}
1564
1565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566int wpas_wps_init(struct wpa_supplicant *wpa_s)
1567{
1568 struct wps_context *wps;
1569 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001570 struct hostapd_hw_modes *modes;
1571 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572
1573 wps = os_zalloc(sizeof(*wps));
1574 if (wps == NULL)
1575 return -1;
1576
1577 wps->cred_cb = wpa_supplicant_wps_cred;
1578 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001579 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 wps->cb_ctx = wpa_s;
1581
1582 wps->dev.device_name = wpa_s->conf->device_name;
1583 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1584 wps->dev.model_name = wpa_s->conf->model_name;
1585 wps->dev.model_number = wpa_s->conf->model_number;
1586 wps->dev.serial_number = wpa_s->conf->serial_number;
1587 wps->config_methods =
1588 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1589 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1590 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1591 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1592 "methods are not allowed at the same time");
1593 os_free(wps);
1594 return -1;
1595 }
1596 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001597 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1599 WPS_DEV_TYPE_LEN);
1600
1601 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1602 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1603 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1604
Dmitry Shmidt04949592012-07-19 12:16:46 -07001605 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608 modes = wpa_s->hw.modes;
1609 if (modes) {
1610 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1611 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1612 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1613 wps->dev.rf_bands |= WPS_RF_24GHZ;
1614 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1615 wps->dev.rf_bands |= WPS_RF_50GHZ;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001616 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1617 wps->dev.rf_bands |= WPS_RF_60GHZ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001618 }
1619 }
1620 if (wps->dev.rf_bands == 0) {
1621 /*
1622 * Default to claiming support for both bands if the driver
1623 * does not provide support for fetching supported bands.
1624 */
1625 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1626 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1628 wpas_wps_set_uuid(wpa_s, wps);
1629
Hai Shalomb755a2a2020-04-23 21:49:02 -07001630#ifdef CONFIG_NO_TKIP
1631 wps->auth_types = WPS_AUTH_WPA2PSK;
1632 wps->encr_types = WPS_ENCR_AES;
1633#else /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1635 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
Hai Shalomb755a2a2020-04-23 21:49:02 -07001636#endif /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637
1638 os_memset(&rcfg, 0, sizeof(rcfg));
1639 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1640 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1641 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1642 rcfg.cb_ctx = wpa_s;
1643
1644 wps->registrar = wps_registrar_init(wps, &rcfg);
1645 if (wps->registrar == NULL) {
1646 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1647 os_free(wps);
1648 return -1;
1649 }
1650
1651 wpa_s->wps = wps;
1652
1653 return 0;
1654}
1655
1656
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001657#ifdef CONFIG_WPS_ER
1658static void wpas_wps_nfc_clear(struct wps_context *wps)
1659{
1660 wps->ap_nfc_dev_pw_id = 0;
1661 wpabuf_free(wps->ap_nfc_dh_pubkey);
1662 wps->ap_nfc_dh_pubkey = NULL;
1663 wpabuf_free(wps->ap_nfc_dh_privkey);
1664 wps->ap_nfc_dh_privkey = NULL;
1665 wpabuf_free(wps->ap_nfc_dev_pw);
1666 wps->ap_nfc_dev_pw = NULL;
1667}
1668#endif /* CONFIG_WPS_ER */
1669
1670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1672{
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001673 wpas_wps_assoc_with_cred_cancel(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001675 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001676 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001677 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001679#ifdef CONFIG_P2P
1680 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1681#endif /* CONFIG_P2P */
1682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 if (wpa_s->wps == NULL)
1684 return;
1685
1686#ifdef CONFIG_WPS_ER
1687 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1688 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001689 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690#endif /* CONFIG_WPS_ER */
1691
1692 wps_registrar_deinit(wpa_s->wps->registrar);
1693 wpabuf_free(wpa_s->wps->dh_pubkey);
1694 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001695 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 os_free(wpa_s->wps->network_key);
1697 os_free(wpa_s->wps);
1698 wpa_s->wps = NULL;
1699}
1700
1701
1702int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001703 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704{
1705 struct wpabuf *wps_ie;
1706
1707 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1708 return -1;
1709
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001710 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1712 if (!wps_ie) {
1713 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1714 return 0;
1715 }
1716
1717 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1718 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1719 "without active PBC Registrar");
1720 wpabuf_free(wps_ie);
1721 return 0;
1722 }
1723
1724 /* TODO: overlap detection */
1725 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1726 "(Active PBC)");
1727 wpabuf_free(wps_ie);
1728 return 1;
1729 }
1730
1731 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1732 if (!wps_ie) {
1733 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1734 return 0;
1735 }
1736
1737 /*
1738 * Start with WPS APs that advertise our address as an
1739 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1740 * allow any WPS AP after couple of scans since some APs do not
1741 * set Selected Registrar attribute properly when using
1742 * external Registrar.
1743 */
1744 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001745 struct os_reltime age;
1746
1747 os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1748
1749 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1750 age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1751 wpa_printf(MSG_DEBUG,
1752 " skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1753 wpa_s->scan_runs, (int) age.sec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 wpabuf_free(wps_ie);
1755 return 0;
1756 }
1757 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1758 } else {
1759 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1760 "(Authorized MAC or Active PIN)");
1761 }
1762 wpabuf_free(wps_ie);
1763 return 1;
1764 }
1765
1766 if (wps_ie) {
1767 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1768 wpabuf_free(wps_ie);
1769 return 1;
1770 }
1771
1772 return -1;
1773}
1774
1775
1776int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1777 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001778 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779{
1780 struct wpabuf *wps_ie = NULL;
1781 int ret = 0;
1782
1783 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001784 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1786 /* allow wildcard SSID for WPS PBC */
1787 ret = 1;
1788 }
1789 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001790 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 if (wps_ie &&
1792 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1793 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1794 /* allow wildcard SSID for WPS PIN */
1795 ret = 1;
1796 }
1797 }
1798
1799 if (!ret && ssid->bssid_set &&
1800 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1801 /* allow wildcard SSID due to hardcoded BSSID match */
1802 ret = 1;
1803 }
1804
1805#ifdef CONFIG_WPS_STRICT
1806 if (wps_ie) {
1807 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1808 0, bss->bssid) < 0)
1809 ret = 0;
1810 if (bss->beacon_ie_len) {
1811 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001812 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 bss, WPS_IE_VENDOR_TYPE);
1814 if (bcn_wps == NULL) {
1815 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1816 "missing from AP Beacon");
1817 ret = 0;
1818 } else {
1819 if (wps_validate_beacon(wps_ie) < 0)
1820 ret = 0;
1821 wpabuf_free(bcn_wps);
1822 }
1823 }
1824 }
1825#endif /* CONFIG_WPS_STRICT */
1826
1827 wpabuf_free(wps_ie);
1828
1829 return ret;
1830}
1831
1832
1833int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1834 struct wpa_bss *selected, struct wpa_ssid *ssid)
1835{
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001836 const u8 *sel_uuid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 struct wpabuf *wps_ie;
1838 int ret = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001839 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840
1841 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1842 return 0;
1843
1844 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1845 "present in scan results; selected BSSID " MACSTR,
1846 MAC2STR(selected->bssid));
Hai Shalomfdcde762020-04-02 11:19:20 -07001847 if (!is_zero_ether_addr(ssid->bssid))
1848 wpa_printf(MSG_DEBUG,
1849 "WPS: Network profile limited to accept only a single BSSID " MACSTR,
1850 MAC2STR(ssid->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851
1852 /* Make sure that only one AP is in active PBC mode */
1853 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1854 if (wps_ie) {
1855 sel_uuid = wps_get_uuid_e(wps_ie);
1856 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1857 sel_uuid, UUID_LEN);
1858 } else {
1859 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1860 "WPS IE?!");
1861 sel_uuid = NULL;
1862 }
1863
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001864 for (i = 0; i < wpa_s->num_wps_ap; i++) {
1865 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
1866
1867 if (!ap->pbc_active ||
1868 os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 continue;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001870
Hai Shalomfdcde762020-04-02 11:19:20 -07001871 if (!is_zero_ether_addr(ssid->bssid) &&
1872 os_memcmp(ap->bssid, ssid->bssid, ETH_ALEN) != 0) {
1873 wpa_printf(MSG_DEBUG, "WPS: Ignore another BSS " MACSTR
1874 " in active PBC mode due to local BSSID limitation",
1875 MAC2STR(ap->bssid));
1876 continue;
1877 }
1878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001880 MACSTR, MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001882 ap->uuid, UUID_LEN);
1883 if (sel_uuid == NULL ||
1884 os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 ret = 1; /* PBC overlap */
1886 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1887 MACSTR " and " MACSTR,
1888 MAC2STR(selected->bssid),
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001889 MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890 break;
1891 }
1892
1893 /* TODO: verify that this is reasonable dual-band situation */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 }
1895
1896 wpabuf_free(wps_ie);
1897
1898 return ret;
1899}
1900
1901
1902void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1903{
1904 struct wpa_bss *bss;
1905 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1906
1907 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1908 return;
1909
1910 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1911 struct wpabuf *ie;
1912 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1913 if (!ie)
1914 continue;
1915 if (wps_is_selected_pbc_registrar(ie))
1916 pbc++;
1917 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1918 auth++;
1919 else if (wps_is_selected_pin_registrar(ie))
1920 pin++;
1921 else
1922 wps++;
1923 wpabuf_free(ie);
1924 }
1925
1926 if (pbc)
1927 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1928 else if (auth)
1929 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1930 else if (pin)
1931 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1932 else if (wps)
1933 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1934}
1935
1936
1937int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1938{
1939 struct wpa_ssid *ssid;
1940
1941 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1942 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1943 return 1;
1944 }
1945
1946 return 0;
1947}
1948
1949
1950int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1951 char *end)
1952{
1953 struct wpabuf *wps_ie;
1954 int ret;
1955
1956 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1957 if (wps_ie == NULL)
1958 return 0;
1959
1960 ret = wps_attr_text(wps_ie, buf, end);
1961 wpabuf_free(wps_ie);
1962 return ret;
1963}
1964
1965
1966int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1967{
1968#ifdef CONFIG_WPS_ER
1969 if (wpa_s->wps_er) {
1970 wps_er_refresh(wpa_s->wps_er);
1971 return 0;
1972 }
1973 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1974 if (wpa_s->wps_er == NULL)
1975 return -1;
1976 return 0;
1977#else /* CONFIG_WPS_ER */
1978 return 0;
1979#endif /* CONFIG_WPS_ER */
1980}
1981
1982
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001983void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984{
1985#ifdef CONFIG_WPS_ER
1986 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1987 wpa_s->wps_er = NULL;
1988#endif /* CONFIG_WPS_ER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989}
1990
1991
1992#ifdef CONFIG_WPS_ER
1993int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1994 const char *uuid, const char *pin)
1995{
1996 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001997 const u8 *use_uuid = NULL;
1998 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002000 if (os_strcmp(uuid, "any") == 0) {
2001 } else if (uuid_str2bin(uuid, u) == 0) {
2002 use_uuid = u;
2003 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
2004 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
2005 if (use_uuid == NULL)
2006 return -1;
2007 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 return -1;
2009 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002010 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 (const u8 *) pin, os_strlen(pin), 300);
2012}
2013
2014
2015int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
2016{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002017 u8 u[UUID_LEN], *use_uuid = NULL;
2018 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002020 if (uuid_str2bin(uuid, u) == 0)
2021 use_uuid = u;
2022 else if (hwaddr_aton(uuid, addr) == 0)
2023 use_addr = addr;
2024 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002026 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027}
2028
2029
2030int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
2031 const char *pin)
2032{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002033 u8 u[UUID_LEN], *use_uuid = NULL;
2034 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002036 if (uuid_str2bin(uuid, u) == 0)
2037 use_uuid = u;
2038 else if (hwaddr_aton(uuid, addr) == 0)
2039 use_addr = addr;
2040 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002042
2043 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 os_strlen(pin));
2045}
2046
2047
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002048static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
2049 struct wps_credential *cred)
2050{
2051 os_memset(cred, 0, sizeof(*cred));
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002052 if (ssid->ssid_len > SSID_MAX_LEN)
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002053 return -1;
2054 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
2055 cred->ssid_len = ssid->ssid_len;
2056 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
2057 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
2058 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
2059 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
2060 cred->encr_type = WPS_ENCR_AES;
2061 else
2062 cred->encr_type = WPS_ENCR_TKIP;
2063 if (ssid->passphrase) {
2064 cred->key_len = os_strlen(ssid->passphrase);
2065 if (cred->key_len >= 64)
2066 return -1;
2067 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2068 } else if (ssid->psk_set) {
2069 cred->key_len = 32;
2070 os_memcpy(cred->key, ssid->psk, 32);
2071 } else
2072 return -1;
2073 } else {
2074 cred->auth_type = WPS_AUTH_OPEN;
2075 cred->encr_type = WPS_ENCR_NONE;
2076 }
2077
2078 return 0;
2079}
2080
2081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2083 int id)
2084{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002085 u8 u[UUID_LEN], *use_uuid = NULL;
2086 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087 struct wpa_ssid *ssid;
2088 struct wps_credential cred;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002089 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002091 if (uuid_str2bin(uuid, u) == 0)
2092 use_uuid = u;
2093 else if (hwaddr_aton(uuid, addr) == 0)
2094 use_addr = addr;
2095 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 return -1;
2097 ssid = wpa_config_get_network(wpa_s->conf, id);
2098 if (ssid == NULL || ssid->ssid == NULL)
2099 return -1;
2100
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002101 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002103 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2104 os_memset(&cred, 0, sizeof(cred));
2105 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106}
2107
2108
2109int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2110 const char *pin, struct wps_new_ap_settings *settings)
2111{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002112 u8 u[UUID_LEN], *use_uuid = NULL;
2113 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 struct wps_credential cred;
2115 size_t len;
2116
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002117 if (uuid_str2bin(uuid, u) == 0)
2118 use_uuid = u;
2119 else if (hwaddr_aton(uuid, addr) == 0)
2120 use_addr = addr;
2121 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 return -1;
2123 if (settings->ssid_hex == NULL || settings->auth == NULL ||
2124 settings->encr == NULL || settings->key_hex == NULL)
2125 return -1;
2126
2127 os_memset(&cred, 0, sizeof(cred));
2128 len = os_strlen(settings->ssid_hex);
2129 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2130 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2131 return -1;
2132 cred.ssid_len = len / 2;
2133
2134 len = os_strlen(settings->key_hex);
2135 if ((len & 1) || len > 2 * sizeof(cred.key) ||
2136 hexstr2bin(settings->key_hex, cred.key, len / 2))
2137 return -1;
2138 cred.key_len = len / 2;
2139
2140 if (os_strcmp(settings->auth, "OPEN") == 0)
2141 cred.auth_type = WPS_AUTH_OPEN;
2142 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2143 cred.auth_type = WPS_AUTH_WPAPSK;
2144 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2145 cred.auth_type = WPS_AUTH_WPA2PSK;
2146 else
2147 return -1;
2148
2149 if (os_strcmp(settings->encr, "NONE") == 0)
2150 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002151#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 else if (os_strcmp(settings->encr, "WEP") == 0)
2153 cred.encr_type = WPS_ENCR_WEP;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002154#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 else if (os_strcmp(settings->encr, "TKIP") == 0)
2156 cred.encr_type = WPS_ENCR_TKIP;
2157 else if (os_strcmp(settings->encr, "CCMP") == 0)
2158 cred.encr_type = WPS_ENCR_AES;
2159 else
2160 return -1;
2161
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002162 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2163 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164}
2165
2166
Dmitry Shmidt04949592012-07-19 12:16:46 -07002167#ifdef CONFIG_WPS_NFC
2168struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2169 int ndef, const char *uuid)
2170{
2171 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002172 u8 u[UUID_LEN], *use_uuid = NULL;
2173 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002174
2175 if (!wpa_s->wps_er)
2176 return NULL;
2177
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002178 if (uuid_str2bin(uuid, u) == 0)
2179 use_uuid = u;
2180 else if (hwaddr_aton(uuid, addr) == 0)
2181 use_addr = addr;
2182 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002183 return NULL;
2184
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002185 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002186 if (ndef && ret) {
2187 struct wpabuf *tmp;
2188 tmp = ndef_build_wifi(ret);
2189 wpabuf_free(ret);
2190 if (tmp == NULL)
2191 return NULL;
2192 ret = tmp;
2193 }
2194
2195 return ret;
2196}
2197#endif /* CONFIG_WPS_NFC */
2198
2199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200static int callbacks_pending = 0;
2201
2202static void wpas_wps_terminate_cb(void *ctx)
2203{
2204 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2205 if (--callbacks_pending <= 0)
2206 eloop_terminate();
2207}
2208#endif /* CONFIG_WPS_ER */
2209
2210
2211int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2212{
2213#ifdef CONFIG_WPS_ER
2214 if (wpa_s->wps_er) {
2215 callbacks_pending++;
2216 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2217 wpa_s->wps_er = NULL;
2218 return 1;
2219 }
2220#endif /* CONFIG_WPS_ER */
2221 return 0;
2222}
2223
2224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2226{
2227 struct wps_context *wps = wpa_s->wps;
2228
2229 if (wps == NULL)
2230 return;
2231
2232 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2233 wps->config_methods = wps_config_methods_str2bin(
2234 wpa_s->conf->config_methods);
2235 if ((wps->config_methods &
2236 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2237 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2238 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2239 "config methods are not allowed at the "
2240 "same time");
2241 wps->config_methods &= ~WPS_CONFIG_LABEL;
2242 }
2243 }
2244 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002245 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246
2247 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2248 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2249 WPS_DEV_TYPE_LEN);
2250
2251 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2252 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2253 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2254 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2255 }
2256
Dmitry Shmidt04949592012-07-19 12:16:46 -07002257 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2258 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2261 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2262
2263 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2264 wpas_wps_set_uuid(wpa_s, wps);
2265
2266 if (wpa_s->conf->changed_parameters &
2267 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2268 /* Update pointers to make sure they refer current values */
2269 wps->dev.device_name = wpa_s->conf->device_name;
2270 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2271 wps->dev.model_name = wpa_s->conf->model_name;
2272 wps->dev.model_number = wpa_s->conf->model_number;
2273 wps->dev.serial_number = wpa_s->conf->serial_number;
2274 }
2275}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002276
2277
Mikael Kanstrupcc779b82019-08-16 08:50:54 +02002278void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2279{
2280 struct wps_context *wps;
2281
2282 wps = wpa_s->wps;
2283 if (wps)
2284 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2285}
2286
2287
Dmitry Shmidt04949592012-07-19 12:16:46 -07002288#ifdef CONFIG_WPS_NFC
2289
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002290#ifdef CONFIG_WPS_ER
Roshan Pius8894db22017-02-13 14:43:20 -08002291struct wpabuf *
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002292wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2293 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002294{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002295 struct wpabuf *ret;
2296 struct wps_credential cred;
2297
2298 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2299 return NULL;
2300
2301 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2302
2303 if (ndef && ret) {
2304 struct wpabuf *tmp;
2305 tmp = ndef_build_wifi(ret);
2306 wpabuf_free(ret);
2307 if (tmp == NULL)
2308 return NULL;
2309 ret = tmp;
2310 }
2311
2312 return ret;
2313}
2314#endif /* CONFIG_WPS_ER */
2315
2316
2317struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2318 int ndef, const char *id_str)
2319{
2320#ifdef CONFIG_WPS_ER
2321 if (id_str) {
2322 int id;
2323 char *end = NULL;
2324 struct wpa_ssid *ssid;
2325
2326 id = strtol(id_str, &end, 10);
2327 if (end && *end)
2328 return NULL;
2329
2330 ssid = wpa_config_get_network(wpa_s->conf, id);
2331 if (ssid == NULL)
2332 return NULL;
2333 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2334 }
2335#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002336#ifdef CONFIG_AP
2337 if (wpa_s->ap_iface)
2338 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2339#endif /* CONFIG_AP */
2340 return NULL;
2341}
2342
2343
Dmitry Shmidt04949592012-07-19 12:16:46 -07002344struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2345{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002346 if (wpa_s->conf->wps_nfc_pw_from_config) {
2347 return wps_nfc_token_build(ndef,
2348 wpa_s->conf->wps_nfc_dev_pw_id,
2349 wpa_s->conf->wps_nfc_dh_pubkey,
2350 wpa_s->conf->wps_nfc_dev_pw);
2351 }
2352
Dmitry Shmidt04949592012-07-19 12:16:46 -07002353 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2354 &wpa_s->conf->wps_nfc_dh_pubkey,
2355 &wpa_s->conf->wps_nfc_dh_privkey,
2356 &wpa_s->conf->wps_nfc_dev_pw);
2357}
2358
2359
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002360int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2361 const u8 *bssid,
2362 const struct wpabuf *dev_pw, u16 dev_pw_id,
2363 int p2p_group, const u8 *peer_pubkey_hash,
2364 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002365{
2366 struct wps_context *wps = wpa_s->wps;
2367 char pw[32 * 2 + 1];
2368
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002369 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2370 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2371 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2372 }
2373
Dmitry Shmidt04949592012-07-19 12:16:46 -07002374 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002375 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2376 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2377 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002378 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002379 }
2380
2381 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2382 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2383 "cannot start NFC-triggered connection", dev_pw_id);
2384 return -1;
2385 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002386
2387 dh5_free(wps->dh_ctx);
2388 wpabuf_free(wps->dh_pubkey);
2389 wpabuf_free(wps->dh_privkey);
2390 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2391 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2392 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2393 wps->dh_ctx = NULL;
2394 wpabuf_free(wps->dh_pubkey);
2395 wps->dh_pubkey = NULL;
2396 wpabuf_free(wps->dh_privkey);
2397 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002398 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002399 return -1;
2400 }
2401 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002402 if (wps->dh_ctx == NULL) {
2403 wpabuf_free(wps->dh_pubkey);
2404 wps->dh_pubkey = NULL;
2405 wpabuf_free(wps->dh_privkey);
2406 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002407 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002408 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002409 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002410
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002411 if (dev_pw) {
2412 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2413 wpabuf_head(dev_pw),
2414 wpabuf_len(dev_pw));
2415 }
2416 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2417 dev_pw ? pw : NULL,
2418 p2p_group, dev_pw_id, peer_pubkey_hash,
2419 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002420}
2421
2422
2423static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2424 struct wps_parse_attr *attr)
2425{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002426 /*
2427 * Disable existing networks temporarily to allow the newly learned
2428 * credential to be preferred. Enable the temporarily disabled networks
2429 * after 10 seconds.
2430 */
2431 wpas_wps_temp_disable(wpa_s, NULL);
2432 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2433 NULL);
2434
Dmitry Shmidt04949592012-07-19 12:16:46 -07002435 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2436 return -1;
2437
2438 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2439 return 0;
2440
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002441 if (attr->ap_channel) {
2442 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002443 int freq = 0;
2444
2445 if (chan >= 1 && chan <= 13)
2446 freq = 2407 + 5 * chan;
2447 else if (chan == 14)
2448 freq = 2484;
2449 else if (chan >= 30)
2450 freq = 5000 + 5 * chan;
2451
2452 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002453 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2454 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002455 wpa_s->after_wps = 5;
2456 wpa_s->wps_freq = freq;
2457 }
2458 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002459
2460 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2461 "based on the received credential added");
2462 wpa_s->normal_scans = 0;
2463 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002464 wpa_s->disconnected = 0;
2465 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002466
2467 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002468 wpa_supplicant_req_scan(wpa_s, 0, 0);
2469
2470 return 0;
2471}
2472
2473
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002474#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002475static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2476 struct wps_parse_attr *attr)
2477{
2478 return wps_registrar_add_nfc_password_token(
2479 wpa_s->wps->registrar, attr->oob_dev_password,
2480 attr->oob_dev_password_len);
2481}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002482#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002483
2484
2485static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2486 const struct wpabuf *wps)
2487{
2488 struct wps_parse_attr attr;
2489
2490 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2491
2492 if (wps_parse_msg(wps, &attr)) {
2493 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2494 return -1;
2495 }
2496
2497 if (attr.num_cred)
2498 return wpas_wps_use_cred(wpa_s, &attr);
2499
2500#ifdef CONFIG_WPS_ER
2501 if (attr.oob_dev_password)
2502 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2503#endif /* CONFIG_WPS_ER */
2504
2505 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2506 return -1;
2507}
2508
2509
2510int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002511 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002512{
2513 const struct wpabuf *wps = data;
2514 struct wpabuf *tmp = NULL;
2515 int ret;
2516
2517 if (wpabuf_len(data) < 4)
2518 return -1;
2519
2520 if (*wpabuf_head_u8(data) != 0x10) {
2521 /* Assume this contains full NDEF record */
2522 tmp = ndef_parse_wifi(data);
2523 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002524#ifdef CONFIG_P2P
2525 tmp = ndef_parse_p2p(data);
2526 if (tmp) {
2527 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2528 forced_freq);
2529 wpabuf_free(tmp);
2530 return ret;
2531 }
2532#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002533 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2534 return -1;
2535 }
2536 wps = tmp;
2537 }
2538
2539 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2540 wpabuf_free(tmp);
2541 return ret;
2542}
2543
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002544
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002545struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2546 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002547{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002548 struct wpabuf *ret;
2549
2550 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2551 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2552 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2553 return NULL;
2554
2555 ret = wps_build_nfc_handover_req(wpa_s->wps,
2556 wpa_s->conf->wps_nfc_dh_pubkey);
2557
2558 if (ndef && ret) {
2559 struct wpabuf *tmp;
2560 tmp = ndef_build_wifi(ret);
2561 wpabuf_free(ret);
2562 if (tmp == NULL)
2563 return NULL;
2564 ret = tmp;
2565 }
2566
2567 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002568}
2569
2570
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002571#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002572
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002573static struct wpabuf *
2574wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2575 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002576{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002577#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002578 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002579 u8 u[UUID_LEN], *use_uuid = NULL;
2580 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002581 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002582
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002583 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002584 return NULL;
2585
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002586 if (uuid == NULL)
2587 return NULL;
2588 if (uuid_str2bin(uuid, u) == 0)
2589 use_uuid = u;
2590 else if (hwaddr_aton(uuid, addr) == 0)
2591 use_addr = addr;
2592 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002593 return NULL;
2594
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002595 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2596 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2597 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2598 return NULL;
2599 }
2600
2601 wpas_wps_nfc_clear(wps);
2602 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2603 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2604 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2605 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2606 wpas_wps_nfc_clear(wps);
2607 return NULL;
2608 }
2609
2610 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2611 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002612 if (ndef && ret) {
2613 struct wpabuf *tmp;
2614 tmp = ndef_build_wifi(ret);
2615 wpabuf_free(ret);
2616 if (tmp == NULL)
2617 return NULL;
2618 ret = tmp;
2619 }
2620
2621 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002622#else /* CONFIG_WPS_ER */
2623 return NULL;
2624#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002625}
2626#endif /* CONFIG_WPS_NFC */
2627
2628
2629struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2630 int ndef, int cr, const char *uuid)
2631{
2632 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002633 if (!cr)
2634 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002635 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2636 if (ret)
2637 return ret;
2638 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002639}
2640
2641
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002642static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2643 const struct wpabuf *data)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002644{
2645 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002646 int ret = -1;
2647 u16 wsc_len;
2648 const u8 *pos;
2649 struct wpabuf msg;
2650 struct wps_parse_attr attr;
2651 u16 dev_pw_id;
2652 const u8 *bssid = NULL;
2653 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002654
2655 wps = ndef_parse_wifi(data);
2656 if (wps == NULL)
2657 return -1;
2658 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2659 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002660 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2661 if (wpabuf_len(wps) < 2) {
2662 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2663 "Message");
2664 goto out;
2665 }
2666 pos = wpabuf_head(wps);
2667 wsc_len = WPA_GET_BE16(pos);
2668 if (wsc_len > wpabuf_len(wps) - 2) {
2669 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2670 "in Wi-Fi Handover Select Message", wsc_len);
2671 goto out;
2672 }
2673 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002674
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002675 wpa_hexdump(MSG_DEBUG,
2676 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2677 pos, wsc_len);
2678 if (wsc_len < wpabuf_len(wps) - 2) {
2679 wpa_hexdump(MSG_DEBUG,
2680 "WPS: Ignore extra data after WSC attributes",
2681 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2682 }
2683
2684 wpabuf_set(&msg, pos, wsc_len);
2685 ret = wps_parse_msg(&msg, &attr);
2686 if (ret < 0) {
2687 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2688 "Wi-Fi Handover Select Message");
2689 goto out;
2690 }
2691
2692 if (attr.oob_dev_password == NULL ||
2693 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2694 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2695 "included in Wi-Fi Handover Select Message");
2696 ret = -1;
2697 goto out;
2698 }
2699
2700 if (attr.ssid == NULL) {
2701 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2702 "Select Message");
2703 ret = -1;
2704 goto out;
2705 }
2706
2707 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2708
2709 if (attr.mac_addr) {
2710 bssid = attr.mac_addr;
2711 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2712 MAC2STR(bssid));
2713 }
2714
2715 if (attr.rf_bands)
2716 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2717
2718 if (attr.ap_channel) {
2719 u16 chan = WPA_GET_BE16(attr.ap_channel);
2720
2721 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2722
2723 if (chan >= 1 && chan <= 13 &&
2724 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2725 freq = 2407 + 5 * chan;
2726 else if (chan == 14 &&
2727 (attr.rf_bands == NULL ||
2728 *attr.rf_bands & WPS_RF_24GHZ))
2729 freq = 2484;
2730 else if (chan >= 30 &&
2731 (attr.rf_bands == NULL ||
2732 *attr.rf_bands & WPS_RF_50GHZ))
2733 freq = 5000 + 5 * chan;
Hai Shalomc3565922019-10-28 11:58:20 -07002734 else if (chan >= 1 && chan <= 6 &&
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002735 (attr.rf_bands == NULL ||
2736 *attr.rf_bands & WPS_RF_60GHZ))
2737 freq = 56160 + 2160 * chan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002738
2739 if (freq) {
2740 wpa_printf(MSG_DEBUG,
2741 "WPS: AP indicated channel %u -> %u MHz",
2742 chan, freq);
2743 }
2744 }
2745
2746 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2747 attr.oob_dev_password, attr.oob_dev_password_len);
2748 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2749 WPS_OOB_PUBKEY_HASH_LEN);
2750 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2751 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2752 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2753 ret = -1;
2754 goto out;
2755 }
2756 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2757 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2758
2759 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2760 attr.oob_dev_password,
2761 attr.ssid, attr.ssid_len, freq);
2762
2763out:
2764 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002765 return ret;
2766}
2767
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002768
2769int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2770 const struct wpabuf *req,
2771 const struct wpabuf *sel)
2772{
2773 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2774 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2775 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2776 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2777}
2778
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002779
2780int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2781 const struct wpabuf *req,
2782 const struct wpabuf *sel)
2783{
2784 struct wpabuf *wps;
2785 int ret = -1;
2786 u16 wsc_len;
2787 const u8 *pos;
2788 struct wpabuf msg;
2789 struct wps_parse_attr attr;
2790 u16 dev_pw_id;
2791
2792 /*
2793 * Enrollee/station is always initiator of the NFC connection handover,
2794 * so use the request message here to find Enrollee public key hash.
2795 */
2796 wps = ndef_parse_wifi(req);
2797 if (wps == NULL)
2798 return -1;
2799 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2800 "payload from NFC connection handover");
2801 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2802 if (wpabuf_len(wps) < 2) {
2803 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2804 "Message");
2805 goto out;
2806 }
2807 pos = wpabuf_head(wps);
2808 wsc_len = WPA_GET_BE16(pos);
2809 if (wsc_len > wpabuf_len(wps) - 2) {
2810 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2811 "in rt Wi-Fi Handover Request Message", wsc_len);
2812 goto out;
2813 }
2814 pos += 2;
2815
2816 wpa_hexdump(MSG_DEBUG,
2817 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2818 pos, wsc_len);
2819 if (wsc_len < wpabuf_len(wps) - 2) {
2820 wpa_hexdump(MSG_DEBUG,
2821 "WPS: Ignore extra data after WSC attributes",
2822 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2823 }
2824
2825 wpabuf_set(&msg, pos, wsc_len);
2826 ret = wps_parse_msg(&msg, &attr);
2827 if (ret < 0) {
2828 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2829 "Wi-Fi Handover Request Message");
2830 goto out;
2831 }
2832
2833 if (attr.oob_dev_password == NULL ||
2834 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2835 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2836 "included in Wi-Fi Handover Request Message");
2837 ret = -1;
2838 goto out;
2839 }
2840
2841 if (attr.uuid_e == NULL) {
2842 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2843 "Handover Request Message");
2844 ret = -1;
2845 goto out;
2846 }
2847
2848 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2849
2850 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2851 attr.oob_dev_password, attr.oob_dev_password_len);
2852 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2853 WPS_OOB_PUBKEY_HASH_LEN);
2854 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2855 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2856 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2857 ret = -1;
2858 goto out;
2859 }
2860 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2861 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2862
2863 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2864 attr.oob_dev_password,
2865 DEV_PW_NFC_CONNECTION_HANDOVER,
2866 NULL, 0, 1);
2867
2868out:
2869 wpabuf_free(wps);
2870 return ret;
2871}
2872
Dmitry Shmidt04949592012-07-19 12:16:46 -07002873#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002874
2875
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002876static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2877{
2878 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002879 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002880
2881 if (wpa_debug_level > MSG_DEBUG)
2882 return;
2883
2884 if (wpa_s->wps_ap == NULL)
2885 return;
2886
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002887 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002888
2889 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2890 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
Hai Shalom60840252021-02-19 19:02:11 -08002891 struct wpa_bssid_ignore *e = wpa_bssid_ignore_get(wpa_s,
2892 ap->bssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893
2894 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
Hai Shalom60840252021-02-19 19:02:11 -08002895 "tries=%d last_attempt=%d sec ago bssid_ignore=%d",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002896 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2897 ap->last_attempt.sec > 0 ?
2898 (int) now.sec - (int) ap->last_attempt.sec : -1,
2899 e ? e->count : 0);
2900 }
2901}
2902
2903
2904static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2905 const u8 *bssid)
2906{
2907 size_t i;
2908
2909 if (wpa_s->wps_ap == NULL)
2910 return NULL;
2911
2912 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2913 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2914 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2915 return ap;
2916 }
2917
2918 return NULL;
2919}
2920
2921
2922static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2923 struct wpa_scan_res *res)
2924{
2925 struct wpabuf *wps;
2926 enum wps_ap_info_type type;
2927 struct wps_ap_info *ap;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002928 int r, pbc_active;
2929 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002930
2931 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2932 return;
2933
2934 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2935 if (wps == NULL)
2936 return;
2937
2938 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2939 if (r == 2)
2940 type = WPS_AP_SEL_REG_OUR;
2941 else if (r == 1)
2942 type = WPS_AP_SEL_REG;
2943 else
2944 type = WPS_AP_NOT_SEL_REG;
2945
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002946 uuid = wps_get_uuid_e(wps);
2947 pbc_active = wps_is_selected_pbc_registrar(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002948
2949 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2950 if (ap) {
2951 if (ap->type != type) {
2952 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2953 " changed type %d -> %d",
2954 MAC2STR(res->bssid), ap->type, type);
2955 ap->type = type;
2956 if (type != WPS_AP_NOT_SEL_REG)
Hai Shalom60840252021-02-19 19:02:11 -08002957 wpa_bssid_ignore_del(wpa_s, ap->bssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002958 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002959 ap->pbc_active = pbc_active;
2960 if (uuid)
2961 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2962 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002963 }
2964
2965 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2966 sizeof(struct wps_ap_info));
2967 if (ap == NULL)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002968 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002969
2970 wpa_s->wps_ap = ap;
2971 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2972 wpa_s->num_wps_ap++;
2973
2974 os_memset(ap, 0, sizeof(*ap));
2975 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2976 ap->type = type;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002977 ap->pbc_active = pbc_active;
2978 if (uuid)
2979 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002980 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2981 MAC2STR(ap->bssid), ap->type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002982
2983out:
2984 wpabuf_free(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002985}
2986
2987
2988void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2989 struct wpa_scan_results *scan_res)
2990{
2991 size_t i;
2992
2993 for (i = 0; i < scan_res->num; i++)
2994 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2995
2996 wpas_wps_dump_ap_info(wpa_s);
2997}
2998
2999
3000void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
3001{
3002 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003003
3004 wpa_s->after_wps = 0;
3005
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003006 if (!wpa_s->wps_ap_iter)
3007 return;
3008 ap = wpas_wps_get_ap_info(wpa_s, bssid);
3009 if (ap == NULL)
3010 return;
3011 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003012 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003013}