blob: 1dd3d82f0c985bdeb22b362e35d25e8a7a2fd0bb [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 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
376 if ((wpa_s->conf->wps_cred_processing == 1 ||
377 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
378 size_t blen = cred->cred_attr_len * 2 + 1;
379 char *buf = os_malloc(blen);
380 if (buf) {
381 wpa_snprintf_hex(buf, blen,
382 cred->cred_attr, cred->cred_attr_len);
383 wpa_msg(wpa_s, MSG_INFO, "%s%s",
384 WPS_EVENT_CRED_RECEIVED, buf);
385 os_free(buf);
386 }
387
388 wpas_notify_wps_credential(wpa_s, cred);
389 } else
390 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
391
392 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
393 cred->cred_attr, cred->cred_attr_len);
394
395 if (wpa_s->conf->wps_cred_processing == 1)
396 return 0;
397
398 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
399 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
400 cred->auth_type);
401 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
402 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
403 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
404 cred->key, cred->key_len);
405 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
406 MAC2STR(cred->mac_addr));
407
408 auth_type = cred->auth_type;
409 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
410 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
411 "auth_type into WPA2PSK");
412 auth_type = WPS_AUTH_WPA2PSK;
413 }
414
415 if (auth_type != WPS_AUTH_OPEN &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 auth_type != WPS_AUTH_WPAPSK &&
417 auth_type != WPS_AUTH_WPA2PSK) {
418 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
419 "unsupported authentication type 0x%x",
420 auth_type);
421 return 0;
422 }
423
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800424 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
425 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
426 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
427 "invalid Network Key length %lu",
428 (unsigned long) cred->key_len);
429 return -1;
430 }
431 }
432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
434 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
435 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800436#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 if (ssid->eap.identity &&
438 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
439 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
440 WSC_ID_REGISTRAR_LEN) == 0)
441 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800442#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443 os_free(ssid->eap.identity);
444 ssid->eap.identity = NULL;
445 ssid->eap.identity_len = 0;
446 os_free(ssid->eap.phase1);
447 ssid->eap.phase1 = NULL;
448 os_free(ssid->eap.eap_methods);
449 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700450 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700452 ssid->bssid_set = 0;
453 }
454 ssid->disabled_until.sec = 0;
455 ssid->disabled_until.usec = 0;
456 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 } else {
458 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
459 "received credential");
460 ssid = wpa_config_add_network(wpa_s->conf);
461 if (ssid == NULL)
462 return -1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700463 if (wpa_s->current_ssid) {
464 /*
465 * Should the GO issue multiple credentials for some
466 * reason, each credential should be marked as a
467 * temporary P2P group similarly to the one that gets
468 * marked as such based on the pre-configured values
469 * used for the WPS network block.
470 */
471 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
472 ssid->temporary = wpa_s->current_ssid->temporary;
473 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 wpas_notify_network_added(wpa_s, ssid);
475 }
476
477 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700478 ssid->wps_run = wpa_s->wps_run;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479
480 os_free(ssid->ssid);
481 ssid->ssid = os_malloc(cred->ssid_len);
482 if (ssid->ssid) {
483 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
484 ssid->ssid_len = cred->ssid_len;
485 }
486
487 switch (cred->encr_type) {
488 case WPS_ENCR_NONE:
489 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 case WPS_ENCR_TKIP:
Hai Shalomb755a2a2020-04-23 21:49:02 -0700491 ssid->pairwise_cipher = WPA_CIPHER_TKIP | WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492 break;
493 case WPS_ENCR_AES:
494 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800495 if (wpa_s->drv_capa_known &&
496 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
497 ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
498 ssid->group_cipher |= WPA_CIPHER_GCMP;
499 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700500 if (wpa_s->drv_capa_known &&
501 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256)) {
502 ssid->pairwise_cipher |= WPA_CIPHER_GCMP_256;
503 ssid->group_cipher |= WPA_CIPHER_GCMP_256;
504 }
505 if (wpa_s->drv_capa_known &&
506 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256)) {
507 ssid->pairwise_cipher |= WPA_CIPHER_CCMP_256;
508 ssid->group_cipher |= WPA_CIPHER_CCMP_256;
509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700510 break;
511 }
512
513 switch (auth_type) {
514 case WPS_AUTH_OPEN:
515 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
516 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
517 ssid->proto = 0;
518#ifdef CONFIG_WPS_REG_DISABLE_OPEN
519 if (registrar) {
520 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
521 "id=%d - Credentials for an open "
522 "network disabled by default - use "
523 "'select_network %d' to enable",
524 ssid->id, ssid->id);
525 ssid->disabled = 1;
526 }
527#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
528 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529 case WPS_AUTH_WPAPSK:
530 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
531 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700532 ssid->proto = WPA_PROTO_WPA | WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534 case WPS_AUTH_WPA2PSK:
535 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
536 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalom021b0b52019-04-10 11:17:58 -0700537 if (wpa_s->conf->wps_cred_add_sae &&
538 cred->key_len != 2 * PMK_LEN) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700539 ssid->auth_alg = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700540 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
Hai Shalom021b0b52019-04-10 11:17:58 -0700541 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
Hai Shalom021b0b52019-04-10 11:17:58 -0700542 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 ssid->proto = WPA_PROTO_RSN;
544 break;
545 }
546
Hai Shalom021b0b52019-04-10 11:17:58 -0700547 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548 if (cred->key_len == 2 * PMK_LEN) {
549 if (hexstr2bin((const char *) cred->key, ssid->psk,
550 PMK_LEN)) {
551 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
552 "Key");
553 return -1;
554 }
555 ssid->psk_set = 1;
556 ssid->export_keys = 1;
557 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
558 os_free(ssid->passphrase);
559 ssid->passphrase = os_malloc(cred->key_len + 1);
560 if (ssid->passphrase == NULL)
561 return -1;
562 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
563 ssid->passphrase[cred->key_len] = '\0';
564 wpa_config_update_psk(ssid);
565 ssid->export_keys = 1;
566 } else {
567 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
568 "length %lu",
569 (unsigned long) cred->key_len);
570 return -1;
571 }
572 }
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700573 ssid->priority = wpa_s->conf->wps_priority;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574
575 wpas_wps_security_workaround(wpa_s, ssid, cred);
576
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700577 wpas_wps_remove_dup_network(wpa_s, ssid);
578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579#ifndef CONFIG_NO_CONFIG_WRITE
580 if (wpa_s->conf->update_config &&
581 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
582 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
583 return -1;
584 }
585#endif /* CONFIG_NO_CONFIG_WRITE */
586
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700587 if (ssid->priority)
588 wpa_config_update_prio_list(wpa_s->conf);
589
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800590 /*
591 * Optimize the post-WPS scan based on the channel used during
592 * the provisioning in case EAP-Failure is not received.
593 */
594 wpa_s->after_wps = 5;
595 wpa_s->wps_freq = wpa_s->assoc_freq;
596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597 return 0;
598}
599
600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
602 struct wps_event_m2d *m2d)
603{
604 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
605 "dev_password_id=%d config_error=%d",
606 m2d->dev_password_id, m2d->config_error);
607 wpas_notify_wps_event_m2d(wpa_s, m2d);
608#ifdef CONFIG_P2P
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800609 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
610 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 "dev_password_id=%d config_error=%d",
612 m2d->dev_password_id, m2d->config_error);
613 }
614 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
615 /*
616 * Notify P2P from eloop timeout to avoid issues with the
617 * interface getting removed while processing a message.
618 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700619 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 NULL);
621 }
622#endif /* CONFIG_P2P */
623}
624
625
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700626static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
627{
628 struct wpa_supplicant *wpa_s = eloop_ctx;
629 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
630 wpas_clear_wps(wpa_s);
631}
632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633
634static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
635 struct wps_event_fail *fail)
636{
637 if (fail->error_indication > 0 &&
638 fail->error_indication < NUM_WPS_EI_VALUES) {
639 wpa_msg(wpa_s, MSG_INFO,
640 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
641 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700642 wps_ei_str(fail->error_indication));
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800643 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
644 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 "msg=%d config_error=%d reason=%d (%s)",
646 fail->msg, fail->config_error,
647 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700648 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 } else {
650 wpa_msg(wpa_s, MSG_INFO,
651 WPS_EVENT_FAIL "msg=%d config_error=%d",
652 fail->msg, fail->config_error);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800653 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
654 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 "msg=%d config_error=%d",
656 fail->msg, fail->config_error);
657 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700658
659 /*
660 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
661 */
662 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
663 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
664 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700667 wpas_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668}
669
670
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800671static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
672
673static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
674{
675 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800676 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800677
678 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
679
680 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
681 if (ssid->disabled_for_connect && ssid->disabled) {
682 ssid->disabled_for_connect = 0;
683 ssid->disabled = 0;
684 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800685 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800686 }
687 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800688
689 if (changed) {
690#ifndef CONFIG_NO_CONFIG_WRITE
691 if (wpa_s->conf->update_config &&
692 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
693 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
694 "configuration");
695 }
696#endif /* CONFIG_NO_CONFIG_WRITE */
697 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800698}
699
700
701static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
702{
703 struct wpa_supplicant *wpa_s = eloop_ctx;
704 /* Enable the networks disabled during wpas_wps_reassoc */
705 wpas_wps_reenable_networks(wpa_s);
706}
707
708
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800709int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
710{
711 return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
712 wpa_s, NULL);
713}
714
715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
717{
718 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
719 wpa_s->wps_success = 1;
720 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700721 if (wpa_s->current_ssid)
722 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
Hai Shalom899fcc72020-10-19 14:38:18 -0700723 wpa_s->consecutive_conn_failures = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800724
725 /*
726 * Enable the networks disabled during wpas_wps_reassoc after 10
727 * seconds. The 10 seconds timer is to allow the data connection to be
728 * formed before allowing other networks to be selected.
729 */
730 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
731 NULL);
732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734}
735
736
737static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
738 struct wps_event_er_ap *ap)
739{
740 char uuid_str[100];
741 char dev_type[WPS_DEV_TYPE_BUFSIZE];
742
743 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
744 if (ap->pri_dev_type)
745 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
746 sizeof(dev_type));
747 else
748 dev_type[0] = '\0';
749
750 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
751 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
752 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
753 ap->friendly_name ? ap->friendly_name : "",
754 ap->manufacturer ? ap->manufacturer : "",
755 ap->model_description ? ap->model_description : "",
756 ap->model_name ? ap->model_name : "",
757 ap->manufacturer_url ? ap->manufacturer_url : "",
758 ap->model_url ? ap->model_url : "");
759}
760
761
762static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
763 struct wps_event_er_ap *ap)
764{
765 char uuid_str[100];
766 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
767 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
768}
769
770
771static void wpa_supplicant_wps_event_er_enrollee_add(
772 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
773{
774 char uuid_str[100];
775 char dev_type[WPS_DEV_TYPE_BUFSIZE];
776
777 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
778 if (enrollee->pri_dev_type)
779 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
780 sizeof(dev_type));
781 else
782 dev_type[0] = '\0';
783
784 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
785 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
786 "|%s|%s|%s|%s|%s|",
787 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
788 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
789 enrollee->dev_name ? enrollee->dev_name : "",
790 enrollee->manufacturer ? enrollee->manufacturer : "",
791 enrollee->model_name ? enrollee->model_name : "",
792 enrollee->model_number ? enrollee->model_number : "",
793 enrollee->serial_number ? enrollee->serial_number : "");
794}
795
796
797static void wpa_supplicant_wps_event_er_enrollee_remove(
798 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
799{
800 char uuid_str[100];
801 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
802 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
803 uuid_str, MAC2STR(enrollee->mac_addr));
804}
805
806
807static void wpa_supplicant_wps_event_er_ap_settings(
808 struct wpa_supplicant *wpa_s,
809 struct wps_event_er_ap_settings *ap_settings)
810{
811 char uuid_str[100];
812 char key_str[65];
813 const struct wps_credential *cred = ap_settings->cred;
814
815 key_str[0] = '\0';
816 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
817 if (cred->key_len >= 8 && cred->key_len <= 64) {
818 os_memcpy(key_str, cred->key, cred->key_len);
819 key_str[cred->key_len] = '\0';
820 }
821 }
822
823 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
824 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
825 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
826 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
827 "key=%s",
828 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
829 cred->auth_type, cred->encr_type, key_str);
830}
831
832
833static void wpa_supplicant_wps_event_er_set_sel_reg(
834 struct wpa_supplicant *wpa_s,
835 struct wps_event_er_set_selected_registrar *ev)
836{
837 char uuid_str[100];
838
839 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
840 switch (ev->state) {
841 case WPS_ER_SET_SEL_REG_START:
842 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
843 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
844 "sel_reg_config_methods=0x%x",
845 uuid_str, ev->sel_reg, ev->dev_passwd_id,
846 ev->sel_reg_config_methods);
847 break;
848 case WPS_ER_SET_SEL_REG_DONE:
849 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
850 "uuid=%s state=DONE", uuid_str);
851 break;
852 case WPS_ER_SET_SEL_REG_FAILED:
853 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
854 "uuid=%s state=FAILED", uuid_str);
855 break;
856 }
857}
858
859
860static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
861 union wps_event_data *data)
862{
863 struct wpa_supplicant *wpa_s = ctx;
864 switch (event) {
865 case WPS_EV_M2D:
866 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
867 break;
868 case WPS_EV_FAIL:
869 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
870 break;
871 case WPS_EV_SUCCESS:
872 wpa_supplicant_wps_event_success(wpa_s);
873 break;
874 case WPS_EV_PWD_AUTH_FAIL:
875#ifdef CONFIG_AP
876 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
877 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
878#endif /* CONFIG_AP */
879 break;
880 case WPS_EV_PBC_OVERLAP:
881 break;
882 case WPS_EV_PBC_TIMEOUT:
883 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700884 case WPS_EV_PBC_ACTIVE:
885 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
886 break;
887 case WPS_EV_PBC_DISABLE:
888 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
889 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 case WPS_EV_ER_AP_ADD:
891 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
892 break;
893 case WPS_EV_ER_AP_REMOVE:
894 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
895 break;
896 case WPS_EV_ER_ENROLLEE_ADD:
897 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
898 &data->enrollee);
899 break;
900 case WPS_EV_ER_ENROLLEE_REMOVE:
901 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
902 &data->enrollee);
903 break;
904 case WPS_EV_ER_AP_SETTINGS:
905 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
906 &data->ap_settings);
907 break;
908 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
909 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
910 &data->set_sel_reg);
911 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800912 case WPS_EV_AP_PIN_SUCCESS:
913 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 }
915}
916
917
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700918static int wpa_supplicant_wps_rf_band(void *ctx)
919{
920 struct wpa_supplicant *wpa_s = ctx;
921
922 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
923 return 0;
924
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700925 return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
926 (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700927}
928
929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
931{
932 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
933 eap_is_wps_pin_enrollee(&ssid->eap))
934 return WPS_REQ_ENROLLEE;
935 else
936 return WPS_REQ_REGISTRAR;
937}
938
939
940static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
941{
942 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700943 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
944
Dmitry Shmidt051af732013-10-22 13:52:46 -0700945 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700946 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700947
Jouni Malinen75ecf522011-06-27 15:19:46 -0700948 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800950 /* Enable the networks disabled during wpas_wps_reassoc */
951 wpas_wps_reenable_networks(wpa_s);
952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800954 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955
956 /* Remove any existing WPS network from configuration */
957 ssid = wpa_s->conf->ssid;
958 while (ssid) {
959 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
960 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800961 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700962 wpa_supplicant_deauthenticate(
963 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964 }
965 id = ssid->id;
966 remove_ssid = ssid;
967 } else
968 id = -1;
969 ssid = ssid->next;
970 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700971 if (prev_current == remove_ssid) {
972 wpa_sm_set_config(wpa_s->wpa, NULL);
973 eapol_sm_notify_config(wpa_s->eapol, NULL,
974 NULL);
975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 wpas_notify_network_removed(wpa_s, remove_ssid);
977 wpa_config_remove_network(wpa_s->conf, id);
978 }
979 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980
981 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982}
983
984
985static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
986{
987 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800988 union wps_event_data data;
989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
991 "out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800992 os_memset(&data, 0, sizeof(data));
993 data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
994 data.fail.error_indication = WPS_EI_NO_ERROR;
995 /*
996 * Call wpas_notify_wps_event_fail() directly instead of through
997 * wpa_supplicant_wps_event() which would end up registering unnecessary
998 * timeouts (those are only for the case where the failure happens
999 * during an EAP-WSC exchange).
1000 */
1001 wpas_notify_wps_event_fail(wpa_s, &data.fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 wpas_clear_wps(wpa_s);
1003}
1004
1005
1006static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001007 int registrar, const u8 *dev_addr,
1008 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009{
1010 struct wpa_ssid *ssid;
1011
1012 ssid = wpa_config_add_network(wpa_s->conf);
1013 if (ssid == NULL)
1014 return NULL;
1015 wpas_notify_network_added(wpa_s, ssid);
1016 wpa_config_set_network_defaults(ssid);
1017 ssid->temporary = 1;
1018 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
1019 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
1020 wpa_config_set(ssid, "identity", registrar ?
1021 "\"" WSC_ID_REGISTRAR "\"" :
1022 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1023 wpas_notify_network_removed(wpa_s, ssid);
1024 wpa_config_remove_network(wpa_s->conf, ssid->id);
1025 return NULL;
1026 }
1027
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001028#ifdef CONFIG_P2P
1029 if (dev_addr)
1030 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1031#endif /* CONFIG_P2P */
1032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 if (bssid) {
1034#ifndef CONFIG_P2P
1035 struct wpa_bss *bss;
1036 int count = 0;
1037#endif /* CONFIG_P2P */
1038
1039 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1040 ssid->bssid_set = 1;
1041
1042 /*
1043 * Note: With P2P, the SSID may change at the time the WPS
1044 * provisioning is started, so better not filter the AP based
1045 * on the current SSID in the scan results.
1046 */
1047#ifndef CONFIG_P2P
1048 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1049 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
1050 continue;
1051
1052 os_free(ssid->ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001053 ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 if (ssid->ssid == NULL)
1055 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 ssid->ssid_len = bss->ssid_len;
1057 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1058 "scan results",
1059 ssid->ssid, ssid->ssid_len);
1060 count++;
1061 }
1062
1063 if (count > 1) {
1064 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1065 "for the AP; use wildcard");
1066 os_free(ssid->ssid);
1067 ssid->ssid = NULL;
1068 ssid->ssid_len = 0;
1069 }
1070#endif /* CONFIG_P2P */
1071 }
1072
1073 return ssid;
1074}
1075
1076
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001077static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1078 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079{
1080 struct wpa_ssid *ssid;
1081
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001082 if (wpa_s->current_ssid) {
1083 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084 wpa_supplicant_deauthenticate(
1085 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001086 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 /* Mark all other networks disabled and trigger reassociation */
1089 ssid = wpa_s->conf->ssid;
1090 while (ssid) {
1091 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001092 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001093 /*
1094 * In case the network object corresponds to a persistent group
1095 * then do not send out network disabled signal. In addition,
1096 * do not change disabled status of persistent network objects
1097 * from 2 to 1 should we connect to another network.
1098 */
1099 if (was_disabled != 2) {
1100 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001101 if (was_disabled != ssid->disabled) {
1102 if (ssid->disabled)
1103 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001104 wpas_notify_network_enabled_changed(wpa_s,
1105 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001106 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001107 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 ssid = ssid->next;
1109 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001110}
1111
1112
1113static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001114 struct wpa_ssid *selected, const u8 *bssid,
1115 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001116{
1117 struct wpa_bss *bss;
1118
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001119 wpa_s->wps_run++;
1120 if (wpa_s->wps_run == 0)
1121 wpa_s->wps_run++;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001122 wpa_s->after_wps = 0;
1123 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001124 if (freq) {
1125 wpa_s->after_wps = 5;
1126 wpa_s->wps_freq = freq;
1127 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001128 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1129 if (bss && bss->freq > 0) {
1130 wpa_s->known_wps_freq = 1;
1131 wpa_s->wps_freq = bss->freq;
1132 }
1133 }
1134
1135 wpas_wps_temp_disable(wpa_s, selected);
1136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 wpa_s->disconnected = 0;
1138 wpa_s->reassociate = 1;
1139 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001140 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 wpa_s->wps_success = 0;
Hai Shalom60840252021-02-19 19:02:11 -08001142 wpa_s->bssid_ignore_cleared = false;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001143
1144 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 wpa_supplicant_req_scan(wpa_s, 0, 0);
1146}
1147
1148
1149int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
Hai Shalom021b0b52019-04-10 11:17:58 -07001150 int p2p_group, int multi_ap_backhaul_sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151{
1152 struct wpa_ssid *ssid;
Hai Shalom021b0b52019-04-10 11:17:58 -07001153 char phase1[32];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001154
1155#ifdef CONFIG_AP
1156 if (wpa_s->ap_iface) {
1157 wpa_printf(MSG_DEBUG,
1158 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1159 return -1;
1160 }
1161#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001163 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 if (ssid == NULL)
1165 return -1;
1166 ssid->temporary = 1;
1167 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001168 /*
1169 * When starting a regular WPS process (not P2P group formation)
1170 * the registrar/final station can be either AP or PCP
1171 * so use a "don't care" value for the pbss flag.
1172 */
1173 if (!p2p_group)
1174 ssid->pbss = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175#ifdef CONFIG_P2P
1176 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1177 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1178 if (ssid->ssid) {
1179 ssid->ssid_len = wpa_s->go_params->ssid_len;
1180 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1181 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001182 if (wpa_s->go_params->freq > 56160) {
1183 /* P2P in 60 GHz uses PBSS */
1184 ssid->pbss = 1;
1185 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001186 if (wpa_s->go_params->edmg &&
1187 wpas_p2p_try_edmg_channel(wpa_s,
1188 wpa_s->go_params) == 0)
1189 ssid->enable_edmg = 1;
1190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1192 "SSID", ssid->ssid, ssid->ssid_len);
1193 }
1194 }
1195#endif /* CONFIG_P2P */
Hai Shalom021b0b52019-04-10 11:17:58 -07001196 os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
1197 multi_ap_backhaul_sta ? " multi_ap=1" : "");
1198 if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001199 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 if (wpa_s->wps_fragment_size)
1201 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Hai Shalom021b0b52019-04-10 11:17:58 -07001202 if (multi_ap_backhaul_sta)
1203 ssid->multi_ap_backhaul_sta = 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001204 wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1206 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001207 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208 return 0;
1209}
1210
1211
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001212static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1213 const u8 *dev_addr, const u8 *bssid,
1214 const char *pin, int p2p_group, u16 dev_pw_id,
1215 const u8 *peer_pubkey_hash,
1216 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217{
1218 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001219 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001221 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001223#ifdef CONFIG_AP
1224 if (wpa_s->ap_iface) {
1225 wpa_printf(MSG_DEBUG,
1226 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1227 return -1;
1228 }
1229#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001231 if (bssid && is_zero_ether_addr(bssid))
1232 bssid = NULL;
1233 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1234 if (ssid == NULL) {
1235 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001237 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 ssid->temporary = 1;
1239 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001240 /*
1241 * When starting a regular WPS process (not P2P group formation)
1242 * the registrar/final station can be either AP or PCP
1243 * so use a "don't care" value for the pbss flag.
1244 */
1245 if (!p2p_group)
1246 ssid->pbss = 2;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001247 if (ssid_val) {
1248 ssid->ssid = os_malloc(ssid_len);
1249 if (ssid->ssid) {
1250 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1251 ssid->ssid_len = ssid_len;
1252 }
1253 }
1254 if (peer_pubkey_hash) {
1255 os_memcpy(hash, " pkhash=", 8);
1256 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1257 peer_pubkey_hash,
1258 WPS_OOB_PUBKEY_HASH_LEN);
1259 } else {
1260 hash[0] = '\0';
1261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262#ifdef CONFIG_P2P
1263 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001264 os_free(ssid->ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1266 if (ssid->ssid) {
1267 ssid->ssid_len = wpa_s->go_params->ssid_len;
1268 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1269 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001270 if (wpa_s->go_params->freq > 56160) {
1271 /* P2P in 60 GHz uses PBSS */
1272 ssid->pbss = 1;
1273 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001274 if (wpa_s->go_params->edmg &&
1275 wpas_p2p_try_edmg_channel(wpa_s,
1276 wpa_s->go_params) == 0)
1277 ssid->enable_edmg = 1;
1278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1280 "SSID", ssid->ssid, ssid->ssid_len);
1281 }
1282 }
1283#endif /* CONFIG_P2P */
1284 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001285 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1286 pin, dev_pw_id, hash);
1287 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1288 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1289 dev_pw_id, hash);
1290 } else {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001291 if (wps_generate_pin(&rpin) < 0) {
1292 wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1293 return -1;
1294 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001295 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1296 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001298 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1299 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001300 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001301 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001302
1303 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER)
1304 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_PIN_ACTIVE);
1305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 if (wpa_s->wps_fragment_size)
1307 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1308 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1309 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001310 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001311 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 return rpin;
1313}
1314
1315
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001316int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1317 const char *pin, int p2p_group, u16 dev_pw_id)
1318{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001319 os_get_reltime(&wpa_s->wps_pin_start_time);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001320 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1321 dev_pw_id, NULL, NULL, 0, 0);
1322}
1323
1324
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001325void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1326{
1327 union wps_event_data data;
1328
1329 os_memset(&data, 0, sizeof(data));
1330 data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1331 data.fail.error_indication = WPS_EI_NO_ERROR;
1332 /*
1333 * Call wpas_notify_wps_event_fail() directly instead of through
1334 * wpa_supplicant_wps_event() which would end up registering unnecessary
1335 * timeouts (those are only for the case where the failure happens
1336 * during an EAP-WSC exchange).
1337 */
1338 wpas_notify_wps_event_fail(wpa_s, &data.fail);
1339}
1340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341/* Cancel the wps pbc/pin requests */
1342int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1343{
1344#ifdef CONFIG_AP
1345 if (wpa_s->ap_iface) {
1346 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1347 return wpa_supplicant_ap_wps_cancel(wpa_s);
1348 }
1349#endif /* CONFIG_AP */
1350
Dmitry Shmidt04949592012-07-19 12:16:46 -07001351 if (wpa_s->wpa_state == WPA_SCANNING ||
1352 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1354 wpa_supplicant_cancel_scan(wpa_s);
1355 wpas_clear_wps(wpa_s);
1356 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1357 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1358 "deauthenticate");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001359 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 wpa_supplicant_deauthenticate(wpa_s,
1361 WLAN_REASON_DEAUTH_LEAVING);
1362 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001363 } else {
1364 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001365 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001366 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1367 0)
1368 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 }
1370
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001371 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CANCEL);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001372 wpa_s->after_wps = 0;
1373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 return 0;
1375}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376
1377
1378int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1379 const char *pin, struct wps_new_ap_settings *settings)
1380{
1381 struct wpa_ssid *ssid;
1382 char val[200];
1383 char *pos, *end;
1384 int res;
1385
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001386#ifdef CONFIG_AP
1387 if (wpa_s->ap_iface) {
1388 wpa_printf(MSG_DEBUG,
1389 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1390 return -1;
1391 }
1392#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 if (!pin)
1394 return -1;
1395 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001396 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 if (ssid == NULL)
1398 return -1;
1399 ssid->temporary = 1;
1400 pos = val;
1401 end = pos + sizeof(val);
1402 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001403 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 return -1;
1405 pos += res;
1406 if (settings) {
1407 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1408 "new_encr=%s new_key=%s",
1409 settings->ssid_hex, settings->auth,
1410 settings->encr, settings->key_hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001411 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 return -1;
1413 pos += res;
1414 }
1415 res = os_snprintf(pos, end - pos, "\"");
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;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001418 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1419 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 if (wpa_s->wps_fragment_size)
1421 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1422 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1423 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001424 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 return 0;
1426}
1427
1428
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001429static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1430 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 size_t psk_len)
1432{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001433 if (is_zero_ether_addr(p2p_dev_addr)) {
1434 wpa_printf(MSG_DEBUG,
1435 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1436 MAC2STR(mac_addr));
1437 } else {
1438 wpa_printf(MSG_DEBUG,
1439 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1440 " P2P Device Addr " MACSTR,
1441 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1444
1445 /* TODO */
1446
1447 return 0;
1448}
1449
1450
1451static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1452 const struct wps_device_data *dev)
1453{
1454 char uuid[40], txt[400];
1455 int len;
1456 char devtype[WPS_DEV_TYPE_BUFSIZE];
1457 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1458 return;
1459 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1460 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1461 " [%s|%s|%s|%s|%s|%s]",
1462 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1463 dev->manufacturer, dev->model_name,
1464 dev->model_number, dev->serial_number,
1465 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1466 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001467 if (!os_snprintf_error(sizeof(txt), len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 wpa_printf(MSG_INFO, "%s", txt);
1469}
1470
1471
1472static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1473 u16 sel_reg_config_methods)
1474{
1475#ifdef CONFIG_WPS_ER
1476 struct wpa_supplicant *wpa_s = ctx;
1477
1478 if (wpa_s->wps_er == NULL)
1479 return;
1480 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1481 "dev_password_id=%u sel_reg_config_methods=0x%x",
1482 sel_reg, dev_passwd_id, sel_reg_config_methods);
1483 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1484 sel_reg_config_methods);
1485#endif /* CONFIG_WPS_ER */
1486}
1487
1488
1489static u16 wps_fix_config_methods(u16 config_methods)
1490{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 if ((config_methods &
1492 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1493 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1494 wpa_printf(MSG_INFO, "WPS: Converting display to "
1495 "virtual_display for WPS 2.0 compliance");
1496 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1497 }
1498 if ((config_methods &
1499 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1500 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1501 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1502 "virtual_push_button for WPS 2.0 compliance");
1503 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505
1506 return config_methods;
1507}
1508
1509
1510static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1511 struct wps_context *wps)
1512{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001513 char buf[50];
1514 const char *src;
1515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 if (is_nil_uuid(wpa_s->conf->uuid)) {
1517 struct wpa_supplicant *first;
1518 first = wpa_s->global->ifaces;
1519 while (first && first->next)
1520 first = first->next;
1521 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001522 if (wps != wpa_s->global->ifaces->wps)
1523 os_memcpy(wps->uuid,
1524 wpa_s->global->ifaces->wps->uuid,
1525 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001526 src = "from the first interface";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001527 } else if (wpa_s->conf->auto_uuid == 1) {
1528 uuid_random(wps->uuid);
1529 src = "based on random data";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 } else {
1531 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001532 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 }
1534 } else {
1535 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001536 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001538
1539 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1540 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541}
1542
1543
Dmitry Shmidt04949592012-07-19 12:16:46 -07001544static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1545 struct wps_context *wps)
1546{
1547 wpabuf_free(wps->dev.vendor_ext_m1);
1548 wps->dev.vendor_ext_m1 = NULL;
1549
1550 if (wpa_s->conf->wps_vendor_ext_m1) {
1551 wps->dev.vendor_ext_m1 =
1552 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1553 if (!wps->dev.vendor_ext_m1) {
1554 wpa_printf(MSG_ERROR, "WPS: Cannot "
1555 "allocate memory for vendor_ext_m1");
1556 }
1557 }
1558}
1559
1560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561int wpas_wps_init(struct wpa_supplicant *wpa_s)
1562{
1563 struct wps_context *wps;
1564 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001565 struct hostapd_hw_modes *modes;
1566 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567
1568 wps = os_zalloc(sizeof(*wps));
1569 if (wps == NULL)
1570 return -1;
1571
1572 wps->cred_cb = wpa_supplicant_wps_cred;
1573 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001574 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 wps->cb_ctx = wpa_s;
1576
1577 wps->dev.device_name = wpa_s->conf->device_name;
1578 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1579 wps->dev.model_name = wpa_s->conf->model_name;
1580 wps->dev.model_number = wpa_s->conf->model_number;
1581 wps->dev.serial_number = wpa_s->conf->serial_number;
1582 wps->config_methods =
1583 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1584 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1585 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1586 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1587 "methods are not allowed at the same time");
1588 os_free(wps);
1589 return -1;
1590 }
1591 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001592 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1594 WPS_DEV_TYPE_LEN);
1595
1596 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1597 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1598 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1599
Dmitry Shmidt04949592012-07-19 12:16:46 -07001600 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001603 modes = wpa_s->hw.modes;
1604 if (modes) {
1605 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1606 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1607 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1608 wps->dev.rf_bands |= WPS_RF_24GHZ;
1609 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1610 wps->dev.rf_bands |= WPS_RF_50GHZ;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001611 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1612 wps->dev.rf_bands |= WPS_RF_60GHZ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001613 }
1614 }
1615 if (wps->dev.rf_bands == 0) {
1616 /*
1617 * Default to claiming support for both bands if the driver
1618 * does not provide support for fetching supported bands.
1619 */
1620 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1621 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1623 wpas_wps_set_uuid(wpa_s, wps);
1624
Hai Shalomb755a2a2020-04-23 21:49:02 -07001625#ifdef CONFIG_NO_TKIP
1626 wps->auth_types = WPS_AUTH_WPA2PSK;
1627 wps->encr_types = WPS_ENCR_AES;
1628#else /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1630 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
Hai Shalomb755a2a2020-04-23 21:49:02 -07001631#endif /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632
1633 os_memset(&rcfg, 0, sizeof(rcfg));
1634 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1635 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1636 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1637 rcfg.cb_ctx = wpa_s;
1638
1639 wps->registrar = wps_registrar_init(wps, &rcfg);
1640 if (wps->registrar == NULL) {
1641 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1642 os_free(wps);
1643 return -1;
1644 }
1645
1646 wpa_s->wps = wps;
1647
1648 return 0;
1649}
1650
1651
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001652#ifdef CONFIG_WPS_ER
1653static void wpas_wps_nfc_clear(struct wps_context *wps)
1654{
1655 wps->ap_nfc_dev_pw_id = 0;
1656 wpabuf_free(wps->ap_nfc_dh_pubkey);
1657 wps->ap_nfc_dh_pubkey = NULL;
1658 wpabuf_free(wps->ap_nfc_dh_privkey);
1659 wps->ap_nfc_dh_privkey = NULL;
1660 wpabuf_free(wps->ap_nfc_dev_pw);
1661 wps->ap_nfc_dev_pw = NULL;
1662}
1663#endif /* CONFIG_WPS_ER */
1664
1665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1667{
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001668 wpas_wps_assoc_with_cred_cancel(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001670 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001671 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001672 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001674#ifdef CONFIG_P2P
1675 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1676#endif /* CONFIG_P2P */
1677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 if (wpa_s->wps == NULL)
1679 return;
1680
1681#ifdef CONFIG_WPS_ER
1682 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1683 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001684 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685#endif /* CONFIG_WPS_ER */
1686
1687 wps_registrar_deinit(wpa_s->wps->registrar);
1688 wpabuf_free(wpa_s->wps->dh_pubkey);
1689 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001690 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 os_free(wpa_s->wps->network_key);
1692 os_free(wpa_s->wps);
1693 wpa_s->wps = NULL;
1694}
1695
1696
1697int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001698 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699{
1700 struct wpabuf *wps_ie;
1701
1702 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1703 return -1;
1704
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001705 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1707 if (!wps_ie) {
1708 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1709 return 0;
1710 }
1711
1712 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1713 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1714 "without active PBC Registrar");
1715 wpabuf_free(wps_ie);
1716 return 0;
1717 }
1718
1719 /* TODO: overlap detection */
1720 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1721 "(Active PBC)");
1722 wpabuf_free(wps_ie);
1723 return 1;
1724 }
1725
1726 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1727 if (!wps_ie) {
1728 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1729 return 0;
1730 }
1731
1732 /*
1733 * Start with WPS APs that advertise our address as an
1734 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1735 * allow any WPS AP after couple of scans since some APs do not
1736 * set Selected Registrar attribute properly when using
1737 * external Registrar.
1738 */
1739 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001740 struct os_reltime age;
1741
1742 os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1743
1744 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1745 age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1746 wpa_printf(MSG_DEBUG,
1747 " skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1748 wpa_s->scan_runs, (int) age.sec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749 wpabuf_free(wps_ie);
1750 return 0;
1751 }
1752 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1753 } else {
1754 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1755 "(Authorized MAC or Active PIN)");
1756 }
1757 wpabuf_free(wps_ie);
1758 return 1;
1759 }
1760
1761 if (wps_ie) {
1762 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1763 wpabuf_free(wps_ie);
1764 return 1;
1765 }
1766
1767 return -1;
1768}
1769
1770
1771int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1772 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001773 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774{
1775 struct wpabuf *wps_ie = NULL;
1776 int ret = 0;
1777
1778 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001779 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1781 /* allow wildcard SSID for WPS PBC */
1782 ret = 1;
1783 }
1784 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001785 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 if (wps_ie &&
1787 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1788 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1789 /* allow wildcard SSID for WPS PIN */
1790 ret = 1;
1791 }
1792 }
1793
1794 if (!ret && ssid->bssid_set &&
1795 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1796 /* allow wildcard SSID due to hardcoded BSSID match */
1797 ret = 1;
1798 }
1799
1800#ifdef CONFIG_WPS_STRICT
1801 if (wps_ie) {
1802 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1803 0, bss->bssid) < 0)
1804 ret = 0;
1805 if (bss->beacon_ie_len) {
1806 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001807 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 bss, WPS_IE_VENDOR_TYPE);
1809 if (bcn_wps == NULL) {
1810 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1811 "missing from AP Beacon");
1812 ret = 0;
1813 } else {
1814 if (wps_validate_beacon(wps_ie) < 0)
1815 ret = 0;
1816 wpabuf_free(bcn_wps);
1817 }
1818 }
1819 }
1820#endif /* CONFIG_WPS_STRICT */
1821
1822 wpabuf_free(wps_ie);
1823
1824 return ret;
1825}
1826
1827
1828int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1829 struct wpa_bss *selected, struct wpa_ssid *ssid)
1830{
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001831 const u8 *sel_uuid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832 struct wpabuf *wps_ie;
1833 int ret = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001834 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835
1836 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1837 return 0;
1838
1839 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1840 "present in scan results; selected BSSID " MACSTR,
1841 MAC2STR(selected->bssid));
Hai Shalomfdcde762020-04-02 11:19:20 -07001842 if (!is_zero_ether_addr(ssid->bssid))
1843 wpa_printf(MSG_DEBUG,
1844 "WPS: Network profile limited to accept only a single BSSID " MACSTR,
1845 MAC2STR(ssid->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846
1847 /* Make sure that only one AP is in active PBC mode */
1848 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1849 if (wps_ie) {
1850 sel_uuid = wps_get_uuid_e(wps_ie);
1851 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1852 sel_uuid, UUID_LEN);
1853 } else {
1854 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1855 "WPS IE?!");
1856 sel_uuid = NULL;
1857 }
1858
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001859 for (i = 0; i < wpa_s->num_wps_ap; i++) {
1860 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
1861
1862 if (!ap->pbc_active ||
1863 os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 continue;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001865
Hai Shalomfdcde762020-04-02 11:19:20 -07001866 if (!is_zero_ether_addr(ssid->bssid) &&
1867 os_memcmp(ap->bssid, ssid->bssid, ETH_ALEN) != 0) {
1868 wpa_printf(MSG_DEBUG, "WPS: Ignore another BSS " MACSTR
1869 " in active PBC mode due to local BSSID limitation",
1870 MAC2STR(ap->bssid));
1871 continue;
1872 }
1873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001875 MACSTR, MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001877 ap->uuid, UUID_LEN);
1878 if (sel_uuid == NULL ||
1879 os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880 ret = 1; /* PBC overlap */
1881 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1882 MACSTR " and " MACSTR,
1883 MAC2STR(selected->bssid),
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001884 MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 break;
1886 }
1887
1888 /* TODO: verify that this is reasonable dual-band situation */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 }
1890
1891 wpabuf_free(wps_ie);
1892
1893 return ret;
1894}
1895
1896
1897void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1898{
1899 struct wpa_bss *bss;
1900 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1901
1902 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1903 return;
1904
1905 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1906 struct wpabuf *ie;
1907 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1908 if (!ie)
1909 continue;
1910 if (wps_is_selected_pbc_registrar(ie))
1911 pbc++;
1912 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1913 auth++;
1914 else if (wps_is_selected_pin_registrar(ie))
1915 pin++;
1916 else
1917 wps++;
1918 wpabuf_free(ie);
1919 }
1920
1921 if (pbc)
1922 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1923 else if (auth)
1924 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1925 else if (pin)
1926 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1927 else if (wps)
1928 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1929}
1930
1931
1932int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1933{
1934 struct wpa_ssid *ssid;
1935
1936 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1937 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1938 return 1;
1939 }
1940
1941 return 0;
1942}
1943
1944
1945int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1946 char *end)
1947{
1948 struct wpabuf *wps_ie;
1949 int ret;
1950
1951 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1952 if (wps_ie == NULL)
1953 return 0;
1954
1955 ret = wps_attr_text(wps_ie, buf, end);
1956 wpabuf_free(wps_ie);
1957 return ret;
1958}
1959
1960
1961int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1962{
1963#ifdef CONFIG_WPS_ER
1964 if (wpa_s->wps_er) {
1965 wps_er_refresh(wpa_s->wps_er);
1966 return 0;
1967 }
1968 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1969 if (wpa_s->wps_er == NULL)
1970 return -1;
1971 return 0;
1972#else /* CONFIG_WPS_ER */
1973 return 0;
1974#endif /* CONFIG_WPS_ER */
1975}
1976
1977
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001978void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979{
1980#ifdef CONFIG_WPS_ER
1981 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1982 wpa_s->wps_er = NULL;
1983#endif /* CONFIG_WPS_ER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984}
1985
1986
1987#ifdef CONFIG_WPS_ER
1988int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1989 const char *uuid, const char *pin)
1990{
1991 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001992 const u8 *use_uuid = NULL;
1993 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001995 if (os_strcmp(uuid, "any") == 0) {
1996 } else if (uuid_str2bin(uuid, u) == 0) {
1997 use_uuid = u;
1998 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1999 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
2000 if (use_uuid == NULL)
2001 return -1;
2002 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 return -1;
2004 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002005 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006 (const u8 *) pin, os_strlen(pin), 300);
2007}
2008
2009
2010int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
2011{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002012 u8 u[UUID_LEN], *use_uuid = NULL;
2013 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002015 if (uuid_str2bin(uuid, u) == 0)
2016 use_uuid = u;
2017 else if (hwaddr_aton(uuid, addr) == 0)
2018 use_addr = addr;
2019 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002021 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022}
2023
2024
2025int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
2026 const char *pin)
2027{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002028 u8 u[UUID_LEN], *use_uuid = NULL;
2029 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002031 if (uuid_str2bin(uuid, u) == 0)
2032 use_uuid = u;
2033 else if (hwaddr_aton(uuid, addr) == 0)
2034 use_addr = addr;
2035 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002037
2038 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 os_strlen(pin));
2040}
2041
2042
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002043static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
2044 struct wps_credential *cred)
2045{
2046 os_memset(cred, 0, sizeof(*cred));
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002047 if (ssid->ssid_len > SSID_MAX_LEN)
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002048 return -1;
2049 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
2050 cred->ssid_len = ssid->ssid_len;
2051 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
2052 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
2053 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
2054 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
2055 cred->encr_type = WPS_ENCR_AES;
2056 else
2057 cred->encr_type = WPS_ENCR_TKIP;
2058 if (ssid->passphrase) {
2059 cred->key_len = os_strlen(ssid->passphrase);
2060 if (cred->key_len >= 64)
2061 return -1;
2062 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2063 } else if (ssid->psk_set) {
2064 cred->key_len = 32;
2065 os_memcpy(cred->key, ssid->psk, 32);
2066 } else
2067 return -1;
2068 } else {
2069 cred->auth_type = WPS_AUTH_OPEN;
2070 cred->encr_type = WPS_ENCR_NONE;
2071 }
2072
2073 return 0;
2074}
2075
2076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2078 int id)
2079{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002080 u8 u[UUID_LEN], *use_uuid = NULL;
2081 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 struct wpa_ssid *ssid;
2083 struct wps_credential cred;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002084 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002086 if (uuid_str2bin(uuid, u) == 0)
2087 use_uuid = u;
2088 else if (hwaddr_aton(uuid, addr) == 0)
2089 use_addr = addr;
2090 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091 return -1;
2092 ssid = wpa_config_get_network(wpa_s->conf, id);
2093 if (ssid == NULL || ssid->ssid == NULL)
2094 return -1;
2095
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002096 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002098 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2099 os_memset(&cred, 0, sizeof(cred));
2100 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101}
2102
2103
2104int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2105 const char *pin, struct wps_new_ap_settings *settings)
2106{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002107 u8 u[UUID_LEN], *use_uuid = NULL;
2108 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 struct wps_credential cred;
2110 size_t len;
2111
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002112 if (uuid_str2bin(uuid, u) == 0)
2113 use_uuid = u;
2114 else if (hwaddr_aton(uuid, addr) == 0)
2115 use_addr = addr;
2116 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 return -1;
2118 if (settings->ssid_hex == NULL || settings->auth == NULL ||
2119 settings->encr == NULL || settings->key_hex == NULL)
2120 return -1;
2121
2122 os_memset(&cred, 0, sizeof(cred));
2123 len = os_strlen(settings->ssid_hex);
2124 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2125 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2126 return -1;
2127 cred.ssid_len = len / 2;
2128
2129 len = os_strlen(settings->key_hex);
2130 if ((len & 1) || len > 2 * sizeof(cred.key) ||
2131 hexstr2bin(settings->key_hex, cred.key, len / 2))
2132 return -1;
2133 cred.key_len = len / 2;
2134
2135 if (os_strcmp(settings->auth, "OPEN") == 0)
2136 cred.auth_type = WPS_AUTH_OPEN;
2137 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2138 cred.auth_type = WPS_AUTH_WPAPSK;
2139 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2140 cred.auth_type = WPS_AUTH_WPA2PSK;
2141 else
2142 return -1;
2143
2144 if (os_strcmp(settings->encr, "NONE") == 0)
2145 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002146#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 else if (os_strcmp(settings->encr, "WEP") == 0)
2148 cred.encr_type = WPS_ENCR_WEP;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002149#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 else if (os_strcmp(settings->encr, "TKIP") == 0)
2151 cred.encr_type = WPS_ENCR_TKIP;
2152 else if (os_strcmp(settings->encr, "CCMP") == 0)
2153 cred.encr_type = WPS_ENCR_AES;
2154 else
2155 return -1;
2156
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002157 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2158 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159}
2160
2161
Dmitry Shmidt04949592012-07-19 12:16:46 -07002162#ifdef CONFIG_WPS_NFC
2163struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2164 int ndef, const char *uuid)
2165{
2166 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002167 u8 u[UUID_LEN], *use_uuid = NULL;
2168 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002169
2170 if (!wpa_s->wps_er)
2171 return NULL;
2172
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002173 if (uuid_str2bin(uuid, u) == 0)
2174 use_uuid = u;
2175 else if (hwaddr_aton(uuid, addr) == 0)
2176 use_addr = addr;
2177 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002178 return NULL;
2179
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002180 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002181 if (ndef && ret) {
2182 struct wpabuf *tmp;
2183 tmp = ndef_build_wifi(ret);
2184 wpabuf_free(ret);
2185 if (tmp == NULL)
2186 return NULL;
2187 ret = tmp;
2188 }
2189
2190 return ret;
2191}
2192#endif /* CONFIG_WPS_NFC */
2193
2194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195static int callbacks_pending = 0;
2196
2197static void wpas_wps_terminate_cb(void *ctx)
2198{
2199 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2200 if (--callbacks_pending <= 0)
2201 eloop_terminate();
2202}
2203#endif /* CONFIG_WPS_ER */
2204
2205
2206int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2207{
2208#ifdef CONFIG_WPS_ER
2209 if (wpa_s->wps_er) {
2210 callbacks_pending++;
2211 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2212 wpa_s->wps_er = NULL;
2213 return 1;
2214 }
2215#endif /* CONFIG_WPS_ER */
2216 return 0;
2217}
2218
2219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2221{
2222 struct wps_context *wps = wpa_s->wps;
2223
2224 if (wps == NULL)
2225 return;
2226
2227 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2228 wps->config_methods = wps_config_methods_str2bin(
2229 wpa_s->conf->config_methods);
2230 if ((wps->config_methods &
2231 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2232 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2233 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2234 "config methods are not allowed at the "
2235 "same time");
2236 wps->config_methods &= ~WPS_CONFIG_LABEL;
2237 }
2238 }
2239 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002240 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241
2242 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2243 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2244 WPS_DEV_TYPE_LEN);
2245
2246 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2247 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2248 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2249 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2250 }
2251
Dmitry Shmidt04949592012-07-19 12:16:46 -07002252 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2253 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2256 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2257
2258 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2259 wpas_wps_set_uuid(wpa_s, wps);
2260
2261 if (wpa_s->conf->changed_parameters &
2262 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2263 /* Update pointers to make sure they refer current values */
2264 wps->dev.device_name = wpa_s->conf->device_name;
2265 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2266 wps->dev.model_name = wpa_s->conf->model_name;
2267 wps->dev.model_number = wpa_s->conf->model_number;
2268 wps->dev.serial_number = wpa_s->conf->serial_number;
2269 }
2270}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002271
2272
Mikael Kanstrupcc779b82019-08-16 08:50:54 +02002273void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2274{
2275 struct wps_context *wps;
2276
2277 wps = wpa_s->wps;
2278 if (wps)
2279 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2280}
2281
2282
Dmitry Shmidt04949592012-07-19 12:16:46 -07002283#ifdef CONFIG_WPS_NFC
2284
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002285#ifdef CONFIG_WPS_ER
Roshan Pius8894db22017-02-13 14:43:20 -08002286struct wpabuf *
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002287wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2288 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002289{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002290 struct wpabuf *ret;
2291 struct wps_credential cred;
2292
2293 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2294 return NULL;
2295
2296 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2297
2298 if (ndef && ret) {
2299 struct wpabuf *tmp;
2300 tmp = ndef_build_wifi(ret);
2301 wpabuf_free(ret);
2302 if (tmp == NULL)
2303 return NULL;
2304 ret = tmp;
2305 }
2306
2307 return ret;
2308}
2309#endif /* CONFIG_WPS_ER */
2310
2311
2312struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2313 int ndef, const char *id_str)
2314{
2315#ifdef CONFIG_WPS_ER
2316 if (id_str) {
2317 int id;
2318 char *end = NULL;
2319 struct wpa_ssid *ssid;
2320
2321 id = strtol(id_str, &end, 10);
2322 if (end && *end)
2323 return NULL;
2324
2325 ssid = wpa_config_get_network(wpa_s->conf, id);
2326 if (ssid == NULL)
2327 return NULL;
2328 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2329 }
2330#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002331#ifdef CONFIG_AP
2332 if (wpa_s->ap_iface)
2333 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2334#endif /* CONFIG_AP */
2335 return NULL;
2336}
2337
2338
Dmitry Shmidt04949592012-07-19 12:16:46 -07002339struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2340{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002341 if (wpa_s->conf->wps_nfc_pw_from_config) {
2342 return wps_nfc_token_build(ndef,
2343 wpa_s->conf->wps_nfc_dev_pw_id,
2344 wpa_s->conf->wps_nfc_dh_pubkey,
2345 wpa_s->conf->wps_nfc_dev_pw);
2346 }
2347
Dmitry Shmidt04949592012-07-19 12:16:46 -07002348 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2349 &wpa_s->conf->wps_nfc_dh_pubkey,
2350 &wpa_s->conf->wps_nfc_dh_privkey,
2351 &wpa_s->conf->wps_nfc_dev_pw);
2352}
2353
2354
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002355int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2356 const u8 *bssid,
2357 const struct wpabuf *dev_pw, u16 dev_pw_id,
2358 int p2p_group, const u8 *peer_pubkey_hash,
2359 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002360{
2361 struct wps_context *wps = wpa_s->wps;
2362 char pw[32 * 2 + 1];
2363
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002364 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2365 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2366 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2367 }
2368
Dmitry Shmidt04949592012-07-19 12:16:46 -07002369 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002370 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2371 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2372 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002373 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002374 }
2375
2376 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2377 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2378 "cannot start NFC-triggered connection", dev_pw_id);
2379 return -1;
2380 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002381
2382 dh5_free(wps->dh_ctx);
2383 wpabuf_free(wps->dh_pubkey);
2384 wpabuf_free(wps->dh_privkey);
2385 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2386 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2387 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2388 wps->dh_ctx = NULL;
2389 wpabuf_free(wps->dh_pubkey);
2390 wps->dh_pubkey = NULL;
2391 wpabuf_free(wps->dh_privkey);
2392 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002393 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002394 return -1;
2395 }
2396 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002397 if (wps->dh_ctx == NULL) {
2398 wpabuf_free(wps->dh_pubkey);
2399 wps->dh_pubkey = NULL;
2400 wpabuf_free(wps->dh_privkey);
2401 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002402 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002403 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002404 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002405
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002406 if (dev_pw) {
2407 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2408 wpabuf_head(dev_pw),
2409 wpabuf_len(dev_pw));
2410 }
2411 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2412 dev_pw ? pw : NULL,
2413 p2p_group, dev_pw_id, peer_pubkey_hash,
2414 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002415}
2416
2417
2418static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2419 struct wps_parse_attr *attr)
2420{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002421 /*
2422 * Disable existing networks temporarily to allow the newly learned
2423 * credential to be preferred. Enable the temporarily disabled networks
2424 * after 10 seconds.
2425 */
2426 wpas_wps_temp_disable(wpa_s, NULL);
2427 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2428 NULL);
2429
Dmitry Shmidt04949592012-07-19 12:16:46 -07002430 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2431 return -1;
2432
2433 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2434 return 0;
2435
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002436 if (attr->ap_channel) {
2437 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002438 int freq = 0;
2439
2440 if (chan >= 1 && chan <= 13)
2441 freq = 2407 + 5 * chan;
2442 else if (chan == 14)
2443 freq = 2484;
2444 else if (chan >= 30)
2445 freq = 5000 + 5 * chan;
2446
2447 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002448 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2449 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002450 wpa_s->after_wps = 5;
2451 wpa_s->wps_freq = freq;
2452 }
2453 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002454
2455 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2456 "based on the received credential added");
2457 wpa_s->normal_scans = 0;
2458 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002459 wpa_s->disconnected = 0;
2460 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002461
2462 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002463 wpa_supplicant_req_scan(wpa_s, 0, 0);
2464
2465 return 0;
2466}
2467
2468
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002469#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002470static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2471 struct wps_parse_attr *attr)
2472{
2473 return wps_registrar_add_nfc_password_token(
2474 wpa_s->wps->registrar, attr->oob_dev_password,
2475 attr->oob_dev_password_len);
2476}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002477#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002478
2479
2480static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2481 const struct wpabuf *wps)
2482{
2483 struct wps_parse_attr attr;
2484
2485 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2486
2487 if (wps_parse_msg(wps, &attr)) {
2488 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2489 return -1;
2490 }
2491
2492 if (attr.num_cred)
2493 return wpas_wps_use_cred(wpa_s, &attr);
2494
2495#ifdef CONFIG_WPS_ER
2496 if (attr.oob_dev_password)
2497 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2498#endif /* CONFIG_WPS_ER */
2499
2500 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2501 return -1;
2502}
2503
2504
2505int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002506 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002507{
2508 const struct wpabuf *wps = data;
2509 struct wpabuf *tmp = NULL;
2510 int ret;
2511
2512 if (wpabuf_len(data) < 4)
2513 return -1;
2514
2515 if (*wpabuf_head_u8(data) != 0x10) {
2516 /* Assume this contains full NDEF record */
2517 tmp = ndef_parse_wifi(data);
2518 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002519#ifdef CONFIG_P2P
2520 tmp = ndef_parse_p2p(data);
2521 if (tmp) {
2522 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2523 forced_freq);
2524 wpabuf_free(tmp);
2525 return ret;
2526 }
2527#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002528 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2529 return -1;
2530 }
2531 wps = tmp;
2532 }
2533
2534 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2535 wpabuf_free(tmp);
2536 return ret;
2537}
2538
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002539
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002540struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2541 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002542{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002543 struct wpabuf *ret;
2544
2545 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2546 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2547 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2548 return NULL;
2549
2550 ret = wps_build_nfc_handover_req(wpa_s->wps,
2551 wpa_s->conf->wps_nfc_dh_pubkey);
2552
2553 if (ndef && ret) {
2554 struct wpabuf *tmp;
2555 tmp = ndef_build_wifi(ret);
2556 wpabuf_free(ret);
2557 if (tmp == NULL)
2558 return NULL;
2559 ret = tmp;
2560 }
2561
2562 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002563}
2564
2565
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002566#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002567
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002568static struct wpabuf *
2569wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2570 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002571{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002572#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002573 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002574 u8 u[UUID_LEN], *use_uuid = NULL;
2575 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002576 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002577
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002578 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002579 return NULL;
2580
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002581 if (uuid == NULL)
2582 return NULL;
2583 if (uuid_str2bin(uuid, u) == 0)
2584 use_uuid = u;
2585 else if (hwaddr_aton(uuid, addr) == 0)
2586 use_addr = addr;
2587 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002588 return NULL;
2589
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002590 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2591 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2592 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2593 return NULL;
2594 }
2595
2596 wpas_wps_nfc_clear(wps);
2597 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2598 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2599 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2600 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2601 wpas_wps_nfc_clear(wps);
2602 return NULL;
2603 }
2604
2605 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2606 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002607 if (ndef && ret) {
2608 struct wpabuf *tmp;
2609 tmp = ndef_build_wifi(ret);
2610 wpabuf_free(ret);
2611 if (tmp == NULL)
2612 return NULL;
2613 ret = tmp;
2614 }
2615
2616 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002617#else /* CONFIG_WPS_ER */
2618 return NULL;
2619#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002620}
2621#endif /* CONFIG_WPS_NFC */
2622
2623
2624struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2625 int ndef, int cr, const char *uuid)
2626{
2627 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002628 if (!cr)
2629 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002630 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2631 if (ret)
2632 return ret;
2633 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002634}
2635
2636
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002637static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2638 const struct wpabuf *data)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002639{
2640 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002641 int ret = -1;
2642 u16 wsc_len;
2643 const u8 *pos;
2644 struct wpabuf msg;
2645 struct wps_parse_attr attr;
2646 u16 dev_pw_id;
2647 const u8 *bssid = NULL;
2648 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002649
2650 wps = ndef_parse_wifi(data);
2651 if (wps == NULL)
2652 return -1;
2653 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2654 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002655 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2656 if (wpabuf_len(wps) < 2) {
2657 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2658 "Message");
2659 goto out;
2660 }
2661 pos = wpabuf_head(wps);
2662 wsc_len = WPA_GET_BE16(pos);
2663 if (wsc_len > wpabuf_len(wps) - 2) {
2664 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2665 "in Wi-Fi Handover Select Message", wsc_len);
2666 goto out;
2667 }
2668 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002669
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002670 wpa_hexdump(MSG_DEBUG,
2671 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2672 pos, wsc_len);
2673 if (wsc_len < wpabuf_len(wps) - 2) {
2674 wpa_hexdump(MSG_DEBUG,
2675 "WPS: Ignore extra data after WSC attributes",
2676 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2677 }
2678
2679 wpabuf_set(&msg, pos, wsc_len);
2680 ret = wps_parse_msg(&msg, &attr);
2681 if (ret < 0) {
2682 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2683 "Wi-Fi Handover Select Message");
2684 goto out;
2685 }
2686
2687 if (attr.oob_dev_password == NULL ||
2688 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2689 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2690 "included in Wi-Fi Handover Select Message");
2691 ret = -1;
2692 goto out;
2693 }
2694
2695 if (attr.ssid == NULL) {
2696 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2697 "Select Message");
2698 ret = -1;
2699 goto out;
2700 }
2701
2702 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2703
2704 if (attr.mac_addr) {
2705 bssid = attr.mac_addr;
2706 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2707 MAC2STR(bssid));
2708 }
2709
2710 if (attr.rf_bands)
2711 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2712
2713 if (attr.ap_channel) {
2714 u16 chan = WPA_GET_BE16(attr.ap_channel);
2715
2716 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2717
2718 if (chan >= 1 && chan <= 13 &&
2719 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2720 freq = 2407 + 5 * chan;
2721 else if (chan == 14 &&
2722 (attr.rf_bands == NULL ||
2723 *attr.rf_bands & WPS_RF_24GHZ))
2724 freq = 2484;
2725 else if (chan >= 30 &&
2726 (attr.rf_bands == NULL ||
2727 *attr.rf_bands & WPS_RF_50GHZ))
2728 freq = 5000 + 5 * chan;
Hai Shalomc3565922019-10-28 11:58:20 -07002729 else if (chan >= 1 && chan <= 6 &&
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002730 (attr.rf_bands == NULL ||
2731 *attr.rf_bands & WPS_RF_60GHZ))
2732 freq = 56160 + 2160 * chan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002733
2734 if (freq) {
2735 wpa_printf(MSG_DEBUG,
2736 "WPS: AP indicated channel %u -> %u MHz",
2737 chan, freq);
2738 }
2739 }
2740
2741 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2742 attr.oob_dev_password, attr.oob_dev_password_len);
2743 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2744 WPS_OOB_PUBKEY_HASH_LEN);
2745 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2746 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2747 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2748 ret = -1;
2749 goto out;
2750 }
2751 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2752 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2753
2754 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2755 attr.oob_dev_password,
2756 attr.ssid, attr.ssid_len, freq);
2757
2758out:
2759 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002760 return ret;
2761}
2762
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002763
2764int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2765 const struct wpabuf *req,
2766 const struct wpabuf *sel)
2767{
2768 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2769 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2770 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2771 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2772}
2773
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002774
2775int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2776 const struct wpabuf *req,
2777 const struct wpabuf *sel)
2778{
2779 struct wpabuf *wps;
2780 int ret = -1;
2781 u16 wsc_len;
2782 const u8 *pos;
2783 struct wpabuf msg;
2784 struct wps_parse_attr attr;
2785 u16 dev_pw_id;
2786
2787 /*
2788 * Enrollee/station is always initiator of the NFC connection handover,
2789 * so use the request message here to find Enrollee public key hash.
2790 */
2791 wps = ndef_parse_wifi(req);
2792 if (wps == NULL)
2793 return -1;
2794 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2795 "payload from NFC connection handover");
2796 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2797 if (wpabuf_len(wps) < 2) {
2798 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2799 "Message");
2800 goto out;
2801 }
2802 pos = wpabuf_head(wps);
2803 wsc_len = WPA_GET_BE16(pos);
2804 if (wsc_len > wpabuf_len(wps) - 2) {
2805 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2806 "in rt Wi-Fi Handover Request Message", wsc_len);
2807 goto out;
2808 }
2809 pos += 2;
2810
2811 wpa_hexdump(MSG_DEBUG,
2812 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2813 pos, wsc_len);
2814 if (wsc_len < wpabuf_len(wps) - 2) {
2815 wpa_hexdump(MSG_DEBUG,
2816 "WPS: Ignore extra data after WSC attributes",
2817 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2818 }
2819
2820 wpabuf_set(&msg, pos, wsc_len);
2821 ret = wps_parse_msg(&msg, &attr);
2822 if (ret < 0) {
2823 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2824 "Wi-Fi Handover Request Message");
2825 goto out;
2826 }
2827
2828 if (attr.oob_dev_password == NULL ||
2829 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2830 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2831 "included in Wi-Fi Handover Request Message");
2832 ret = -1;
2833 goto out;
2834 }
2835
2836 if (attr.uuid_e == NULL) {
2837 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2838 "Handover Request Message");
2839 ret = -1;
2840 goto out;
2841 }
2842
2843 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2844
2845 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2846 attr.oob_dev_password, attr.oob_dev_password_len);
2847 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2848 WPS_OOB_PUBKEY_HASH_LEN);
2849 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2850 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2851 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2852 ret = -1;
2853 goto out;
2854 }
2855 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2856 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2857
2858 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2859 attr.oob_dev_password,
2860 DEV_PW_NFC_CONNECTION_HANDOVER,
2861 NULL, 0, 1);
2862
2863out:
2864 wpabuf_free(wps);
2865 return ret;
2866}
2867
Dmitry Shmidt04949592012-07-19 12:16:46 -07002868#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002869
2870
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002871static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2872{
2873 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002874 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002875
2876 if (wpa_debug_level > MSG_DEBUG)
2877 return;
2878
2879 if (wpa_s->wps_ap == NULL)
2880 return;
2881
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002882 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002883
2884 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2885 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
Hai Shalom60840252021-02-19 19:02:11 -08002886 struct wpa_bssid_ignore *e = wpa_bssid_ignore_get(wpa_s,
2887 ap->bssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002888
2889 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
Hai Shalom60840252021-02-19 19:02:11 -08002890 "tries=%d last_attempt=%d sec ago bssid_ignore=%d",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002891 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2892 ap->last_attempt.sec > 0 ?
2893 (int) now.sec - (int) ap->last_attempt.sec : -1,
2894 e ? e->count : 0);
2895 }
2896}
2897
2898
2899static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2900 const u8 *bssid)
2901{
2902 size_t i;
2903
2904 if (wpa_s->wps_ap == NULL)
2905 return NULL;
2906
2907 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2908 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2909 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2910 return ap;
2911 }
2912
2913 return NULL;
2914}
2915
2916
2917static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2918 struct wpa_scan_res *res)
2919{
2920 struct wpabuf *wps;
2921 enum wps_ap_info_type type;
2922 struct wps_ap_info *ap;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002923 int r, pbc_active;
2924 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002925
2926 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2927 return;
2928
2929 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2930 if (wps == NULL)
2931 return;
2932
2933 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2934 if (r == 2)
2935 type = WPS_AP_SEL_REG_OUR;
2936 else if (r == 1)
2937 type = WPS_AP_SEL_REG;
2938 else
2939 type = WPS_AP_NOT_SEL_REG;
2940
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002941 uuid = wps_get_uuid_e(wps);
2942 pbc_active = wps_is_selected_pbc_registrar(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002943
2944 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2945 if (ap) {
2946 if (ap->type != type) {
2947 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2948 " changed type %d -> %d",
2949 MAC2STR(res->bssid), ap->type, type);
2950 ap->type = type;
2951 if (type != WPS_AP_NOT_SEL_REG)
Hai Shalom60840252021-02-19 19:02:11 -08002952 wpa_bssid_ignore_del(wpa_s, ap->bssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002954 ap->pbc_active = pbc_active;
2955 if (uuid)
2956 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2957 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002958 }
2959
2960 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2961 sizeof(struct wps_ap_info));
2962 if (ap == NULL)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002963 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002964
2965 wpa_s->wps_ap = ap;
2966 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2967 wpa_s->num_wps_ap++;
2968
2969 os_memset(ap, 0, sizeof(*ap));
2970 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2971 ap->type = type;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002972 ap->pbc_active = pbc_active;
2973 if (uuid)
2974 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002975 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2976 MAC2STR(ap->bssid), ap->type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002977
2978out:
2979 wpabuf_free(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002980}
2981
2982
2983void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2984 struct wpa_scan_results *scan_res)
2985{
2986 size_t i;
2987
2988 for (i = 0; i < scan_res->num; i++)
2989 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2990
2991 wpas_wps_dump_ap_info(wpa_s);
2992}
2993
2994
2995void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2996{
2997 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002998
2999 wpa_s->after_wps = 0;
3000
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003001 if (!wpa_s->wps_ap_iter)
3002 return;
3003 ap = wpas_wps_get_ap_info(wpa_s, bssid);
3004 if (ap == NULL)
3005 return;
3006 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003007 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003008}