blob: 130c2789bf3a0a6948b95fafdbcadce9168814a6 [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"
29#include "blacklist.h"
30#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));
97 wpa_blacklist_add(wpa_s, bssid);
98
99 wpa_supplicant_deauthenticate(wpa_s,
100 WLAN_REASON_DEAUTH_LEAVING);
101 wpa_s->reassociate = 1;
102 wpa_supplicant_req_scan(wpa_s,
103 wpa_s->blacklist_cleared ? 5 : 0, 0);
104 wpa_s->blacklist_cleared = 0;
105 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;
191
192 /*
193 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
194 * case they are configured for mixed mode operation (WPA+WPA2 and
195 * TKIP+CCMP). Try to use scan results to figure out whether the AP
196 * actually supports stronger security and select that if the client
197 * has support for it, too.
198 */
199
200 if (wpa_drv_get_capa(wpa_s, &capa))
201 return; /* Unknown what driver supports */
202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203 if (ssid->ssid == NULL)
204 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700206 if (!bss)
207 bss = wpa_bss_get(wpa_s, wpa_s->bssid,
208 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 if (bss == NULL) {
210 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
211 "table - use credential as-is");
212 return;
213 }
214
215 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
216
217 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
218 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
219 wpa2 = 1;
220 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
221 ccmp = 1;
222 } else {
223 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
224 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
225 adv.pairwise_cipher & WPA_CIPHER_CCMP)
226 ccmp = 1;
227 }
228
229 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
230 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
231 /*
232 * TODO: This could be the initial AP configuration and the
233 * Beacon contents could change shortly. Should request a new
234 * scan and delay addition of the network until the updated
235 * scan results are available.
236 */
237 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
238 "support - use credential as-is");
239 return;
240 }
241
242 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
243 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
244 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
245 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
246 "based on scan results");
247 if (wpa_s->conf->ap_scan == 1)
248 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
249 else
250 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
251 }
252
253 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
254 (ssid->proto & WPA_PROTO_WPA) &&
255 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
256 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
257 "based on scan results");
258 if (wpa_s->conf->ap_scan == 1)
259 ssid->proto |= WPA_PROTO_RSN;
260 else
261 ssid->proto = WPA_PROTO_RSN;
262 }
263}
264
265
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700266static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
267 struct wpa_ssid *new_ssid)
268{
269 struct wpa_ssid *ssid, *next;
270
271 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
272 ssid = next, next = ssid ? ssid->next : NULL) {
273 /*
274 * new_ssid has already been added to the list in
275 * wpas_wps_add_network(), so skip it.
276 */
277 if (ssid == new_ssid)
278 continue;
279
280 if (ssid->bssid_set || new_ssid->bssid_set) {
281 if (ssid->bssid_set != new_ssid->bssid_set)
282 continue;
283 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
284 0)
285 continue;
286 }
287
288 /* compare SSID */
289 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
290 continue;
291
292 if (ssid->ssid && new_ssid->ssid) {
293 if (os_memcmp(ssid->ssid, new_ssid->ssid,
294 ssid->ssid_len) != 0)
295 continue;
296 } else if (ssid->ssid || new_ssid->ssid)
297 continue;
298
299 /* compare security parameters */
300 if (ssid->auth_alg != new_ssid->auth_alg ||
301 ssid->key_mgmt != new_ssid->key_mgmt ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800302 (ssid->group_cipher != new_ssid->group_cipher &&
303 !(ssid->group_cipher & new_ssid->group_cipher &
304 WPA_CIPHER_CCMP)))
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700305 continue;
306
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700307 /*
308 * Some existing WPS APs will send two creds in case they are
309 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
310 * Try to merge these two creds if they are received in the same
311 * M8 message.
312 */
313 if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
314 wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
315 if (new_ssid->passphrase && ssid->passphrase &&
316 os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
317 0) {
318 wpa_printf(MSG_DEBUG,
319 "WPS: M8 Creds with different passphrase - do not merge");
320 continue;
321 }
322
323 if (new_ssid->psk_set &&
324 (!ssid->psk_set ||
325 os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
326 wpa_printf(MSG_DEBUG,
327 "WPS: M8 Creds with different PSK - do not merge");
328 continue;
329 }
330
331 if ((new_ssid->passphrase && !ssid->passphrase) ||
332 (!new_ssid->passphrase && ssid->passphrase)) {
333 wpa_printf(MSG_DEBUG,
334 "WPS: M8 Creds with different passphrase/PSK type - do not merge");
335 continue;
336 }
337
338 wpa_printf(MSG_DEBUG,
339 "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
340 new_ssid->proto |= ssid->proto;
341 new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
342 } else {
343 /*
344 * proto and pairwise_cipher difference matter for
345 * non-mixed-mode creds.
346 */
347 if (ssid->proto != new_ssid->proto ||
348 ssid->pairwise_cipher != new_ssid->pairwise_cipher)
349 continue;
350 }
351
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700352 /* Remove the duplicated older network entry. */
353 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
354 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800355 if (wpa_s->current_ssid == ssid)
356 wpa_s->current_ssid = NULL;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700357 wpa_config_remove_network(wpa_s->conf, ssid->id);
358 }
359}
360
361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362static int wpa_supplicant_wps_cred(void *ctx,
363 const struct wps_credential *cred)
364{
365 struct wpa_supplicant *wpa_s = ctx;
366 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 u16 auth_type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800368#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 int registrar = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800370#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371
372 if ((wpa_s->conf->wps_cred_processing == 1 ||
373 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
374 size_t blen = cred->cred_attr_len * 2 + 1;
375 char *buf = os_malloc(blen);
376 if (buf) {
377 wpa_snprintf_hex(buf, blen,
378 cred->cred_attr, cred->cred_attr_len);
379 wpa_msg(wpa_s, MSG_INFO, "%s%s",
380 WPS_EVENT_CRED_RECEIVED, buf);
381 os_free(buf);
382 }
383
384 wpas_notify_wps_credential(wpa_s, cred);
385 } else
386 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
387
388 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
389 cred->cred_attr, cred->cred_attr_len);
390
391 if (wpa_s->conf->wps_cred_processing == 1)
392 return 0;
393
394 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
395 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
396 cred->auth_type);
397 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
398 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
399 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
400 cred->key, cred->key_len);
401 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
402 MAC2STR(cred->mac_addr));
403
404 auth_type = cred->auth_type;
405 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
406 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
407 "auth_type into WPA2PSK");
408 auth_type = WPS_AUTH_WPA2PSK;
409 }
410
411 if (auth_type != WPS_AUTH_OPEN &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 auth_type != WPS_AUTH_WPAPSK &&
413 auth_type != WPS_AUTH_WPA2PSK) {
414 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
415 "unsupported authentication type 0x%x",
416 auth_type);
417 return 0;
418 }
419
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800420 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
421 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
422 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
423 "invalid Network Key length %lu",
424 (unsigned long) cred->key_len);
425 return -1;
426 }
427 }
428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
430 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
431 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800432#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 if (ssid->eap.identity &&
434 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
435 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
436 WSC_ID_REGISTRAR_LEN) == 0)
437 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800438#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 os_free(ssid->eap.identity);
440 ssid->eap.identity = NULL;
441 ssid->eap.identity_len = 0;
442 os_free(ssid->eap.phase1);
443 ssid->eap.phase1 = NULL;
444 os_free(ssid->eap.eap_methods);
445 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700446 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700448 ssid->bssid_set = 0;
449 }
450 ssid->disabled_until.sec = 0;
451 ssid->disabled_until.usec = 0;
452 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 } else {
454 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
455 "received credential");
456 ssid = wpa_config_add_network(wpa_s->conf);
457 if (ssid == NULL)
458 return -1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700459 if (wpa_s->current_ssid) {
460 /*
461 * Should the GO issue multiple credentials for some
462 * reason, each credential should be marked as a
463 * temporary P2P group similarly to the one that gets
464 * marked as such based on the pre-configured values
465 * used for the WPS network block.
466 */
467 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
468 ssid->temporary = wpa_s->current_ssid->temporary;
469 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 wpas_notify_network_added(wpa_s, ssid);
471 }
472
473 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700474 ssid->wps_run = wpa_s->wps_run;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475
476 os_free(ssid->ssid);
477 ssid->ssid = os_malloc(cred->ssid_len);
478 if (ssid->ssid) {
479 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
480 ssid->ssid_len = cred->ssid_len;
481 }
482
483 switch (cred->encr_type) {
484 case WPS_ENCR_NONE:
485 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 case WPS_ENCR_TKIP:
Hai Shalomb755a2a2020-04-23 21:49:02 -0700487 ssid->pairwise_cipher = WPA_CIPHER_TKIP | WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 break;
489 case WPS_ENCR_AES:
490 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800491 if (wpa_s->drv_capa_known &&
492 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
493 ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
494 ssid->group_cipher |= WPA_CIPHER_GCMP;
495 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700496 if (wpa_s->drv_capa_known &&
497 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256)) {
498 ssid->pairwise_cipher |= WPA_CIPHER_GCMP_256;
499 ssid->group_cipher |= WPA_CIPHER_GCMP_256;
500 }
501 if (wpa_s->drv_capa_known &&
502 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256)) {
503 ssid->pairwise_cipher |= WPA_CIPHER_CCMP_256;
504 ssid->group_cipher |= WPA_CIPHER_CCMP_256;
505 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 break;
507 }
508
509 switch (auth_type) {
510 case WPS_AUTH_OPEN:
511 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
512 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
513 ssid->proto = 0;
514#ifdef CONFIG_WPS_REG_DISABLE_OPEN
515 if (registrar) {
516 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
517 "id=%d - Credentials for an open "
518 "network disabled by default - use "
519 "'select_network %d' to enable",
520 ssid->id, ssid->id);
521 ssid->disabled = 1;
522 }
523#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
524 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 case WPS_AUTH_WPAPSK:
526 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
527 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700528 ssid->proto = WPA_PROTO_WPA | WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530 case WPS_AUTH_WPA2PSK:
531 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
532 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalom021b0b52019-04-10 11:17:58 -0700533 if (wpa_s->conf->wps_cred_add_sae &&
534 cred->key_len != 2 * PMK_LEN) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700535 ssid->auth_alg = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700536 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
Hai Shalom021b0b52019-04-10 11:17:58 -0700537 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
Hai Shalom021b0b52019-04-10 11:17:58 -0700538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 ssid->proto = WPA_PROTO_RSN;
540 break;
541 }
542
Hai Shalom021b0b52019-04-10 11:17:58 -0700543 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 if (cred->key_len == 2 * PMK_LEN) {
545 if (hexstr2bin((const char *) cred->key, ssid->psk,
546 PMK_LEN)) {
547 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
548 "Key");
549 return -1;
550 }
551 ssid->psk_set = 1;
552 ssid->export_keys = 1;
553 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
554 os_free(ssid->passphrase);
555 ssid->passphrase = os_malloc(cred->key_len + 1);
556 if (ssid->passphrase == NULL)
557 return -1;
558 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
559 ssid->passphrase[cred->key_len] = '\0';
560 wpa_config_update_psk(ssid);
561 ssid->export_keys = 1;
562 } else {
563 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
564 "length %lu",
565 (unsigned long) cred->key_len);
566 return -1;
567 }
568 }
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700569 ssid->priority = wpa_s->conf->wps_priority;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570
571 wpas_wps_security_workaround(wpa_s, ssid, cred);
572
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700573 wpas_wps_remove_dup_network(wpa_s, ssid);
574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575#ifndef CONFIG_NO_CONFIG_WRITE
576 if (wpa_s->conf->update_config &&
577 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
578 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
579 return -1;
580 }
581#endif /* CONFIG_NO_CONFIG_WRITE */
582
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700583 if (ssid->priority)
584 wpa_config_update_prio_list(wpa_s->conf);
585
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800586 /*
587 * Optimize the post-WPS scan based on the channel used during
588 * the provisioning in case EAP-Failure is not received.
589 */
590 wpa_s->after_wps = 5;
591 wpa_s->wps_freq = wpa_s->assoc_freq;
592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 return 0;
594}
595
596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
598 struct wps_event_m2d *m2d)
599{
600 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
601 "dev_password_id=%d config_error=%d",
602 m2d->dev_password_id, m2d->config_error);
603 wpas_notify_wps_event_m2d(wpa_s, m2d);
604#ifdef CONFIG_P2P
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800605 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
606 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607 "dev_password_id=%d config_error=%d",
608 m2d->dev_password_id, m2d->config_error);
609 }
610 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
611 /*
612 * Notify P2P from eloop timeout to avoid issues with the
613 * interface getting removed while processing a message.
614 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700615 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 NULL);
617 }
618#endif /* CONFIG_P2P */
619}
620
621
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700622static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
623{
624 struct wpa_supplicant *wpa_s = eloop_ctx;
625 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
626 wpas_clear_wps(wpa_s);
627}
628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629
630static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
631 struct wps_event_fail *fail)
632{
633 if (fail->error_indication > 0 &&
634 fail->error_indication < NUM_WPS_EI_VALUES) {
635 wpa_msg(wpa_s, MSG_INFO,
636 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
637 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700638 wps_ei_str(fail->error_indication));
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800639 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
640 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 "msg=%d config_error=%d reason=%d (%s)",
642 fail->msg, fail->config_error,
643 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700644 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 } else {
646 wpa_msg(wpa_s, MSG_INFO,
647 WPS_EVENT_FAIL "msg=%d config_error=%d",
648 fail->msg, fail->config_error);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800649 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
650 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651 "msg=%d config_error=%d",
652 fail->msg, fail->config_error);
653 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700654
655 /*
656 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
657 */
658 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
659 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
660 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700663 wpas_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664}
665
666
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800667static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
668
669static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
670{
671 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800672 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800673
674 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
675
676 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
677 if (ssid->disabled_for_connect && ssid->disabled) {
678 ssid->disabled_for_connect = 0;
679 ssid->disabled = 0;
680 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800681 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800682 }
683 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800684
685 if (changed) {
686#ifndef CONFIG_NO_CONFIG_WRITE
687 if (wpa_s->conf->update_config &&
688 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
689 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
690 "configuration");
691 }
692#endif /* CONFIG_NO_CONFIG_WRITE */
693 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800694}
695
696
697static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
698{
699 struct wpa_supplicant *wpa_s = eloop_ctx;
700 /* Enable the networks disabled during wpas_wps_reassoc */
701 wpas_wps_reenable_networks(wpa_s);
702}
703
704
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800705int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
706{
707 return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
708 wpa_s, NULL);
709}
710
711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
713{
714 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
715 wpa_s->wps_success = 1;
716 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700717 if (wpa_s->current_ssid)
718 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
719 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800720
721 /*
722 * Enable the networks disabled during wpas_wps_reassoc after 10
723 * seconds. The 10 seconds timer is to allow the data connection to be
724 * formed before allowing other networks to be selected.
725 */
726 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
727 NULL);
728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730}
731
732
733static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
734 struct wps_event_er_ap *ap)
735{
736 char uuid_str[100];
737 char dev_type[WPS_DEV_TYPE_BUFSIZE];
738
739 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
740 if (ap->pri_dev_type)
741 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
742 sizeof(dev_type));
743 else
744 dev_type[0] = '\0';
745
746 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
747 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
748 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
749 ap->friendly_name ? ap->friendly_name : "",
750 ap->manufacturer ? ap->manufacturer : "",
751 ap->model_description ? ap->model_description : "",
752 ap->model_name ? ap->model_name : "",
753 ap->manufacturer_url ? ap->manufacturer_url : "",
754 ap->model_url ? ap->model_url : "");
755}
756
757
758static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
759 struct wps_event_er_ap *ap)
760{
761 char uuid_str[100];
762 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
763 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
764}
765
766
767static void wpa_supplicant_wps_event_er_enrollee_add(
768 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
769{
770 char uuid_str[100];
771 char dev_type[WPS_DEV_TYPE_BUFSIZE];
772
773 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
774 if (enrollee->pri_dev_type)
775 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
776 sizeof(dev_type));
777 else
778 dev_type[0] = '\0';
779
780 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
781 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
782 "|%s|%s|%s|%s|%s|",
783 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
784 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
785 enrollee->dev_name ? enrollee->dev_name : "",
786 enrollee->manufacturer ? enrollee->manufacturer : "",
787 enrollee->model_name ? enrollee->model_name : "",
788 enrollee->model_number ? enrollee->model_number : "",
789 enrollee->serial_number ? enrollee->serial_number : "");
790}
791
792
793static void wpa_supplicant_wps_event_er_enrollee_remove(
794 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
795{
796 char uuid_str[100];
797 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
798 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
799 uuid_str, MAC2STR(enrollee->mac_addr));
800}
801
802
803static void wpa_supplicant_wps_event_er_ap_settings(
804 struct wpa_supplicant *wpa_s,
805 struct wps_event_er_ap_settings *ap_settings)
806{
807 char uuid_str[100];
808 char key_str[65];
809 const struct wps_credential *cred = ap_settings->cred;
810
811 key_str[0] = '\0';
812 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
813 if (cred->key_len >= 8 && cred->key_len <= 64) {
814 os_memcpy(key_str, cred->key, cred->key_len);
815 key_str[cred->key_len] = '\0';
816 }
817 }
818
819 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
820 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
821 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
822 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
823 "key=%s",
824 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
825 cred->auth_type, cred->encr_type, key_str);
826}
827
828
829static void wpa_supplicant_wps_event_er_set_sel_reg(
830 struct wpa_supplicant *wpa_s,
831 struct wps_event_er_set_selected_registrar *ev)
832{
833 char uuid_str[100];
834
835 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
836 switch (ev->state) {
837 case WPS_ER_SET_SEL_REG_START:
838 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
839 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
840 "sel_reg_config_methods=0x%x",
841 uuid_str, ev->sel_reg, ev->dev_passwd_id,
842 ev->sel_reg_config_methods);
843 break;
844 case WPS_ER_SET_SEL_REG_DONE:
845 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
846 "uuid=%s state=DONE", uuid_str);
847 break;
848 case WPS_ER_SET_SEL_REG_FAILED:
849 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
850 "uuid=%s state=FAILED", uuid_str);
851 break;
852 }
853}
854
855
856static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
857 union wps_event_data *data)
858{
859 struct wpa_supplicant *wpa_s = ctx;
860 switch (event) {
861 case WPS_EV_M2D:
862 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
863 break;
864 case WPS_EV_FAIL:
865 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
866 break;
867 case WPS_EV_SUCCESS:
868 wpa_supplicant_wps_event_success(wpa_s);
869 break;
870 case WPS_EV_PWD_AUTH_FAIL:
871#ifdef CONFIG_AP
872 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
873 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
874#endif /* CONFIG_AP */
875 break;
876 case WPS_EV_PBC_OVERLAP:
877 break;
878 case WPS_EV_PBC_TIMEOUT:
879 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700880 case WPS_EV_PBC_ACTIVE:
881 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
882 break;
883 case WPS_EV_PBC_DISABLE:
884 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
885 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 case WPS_EV_ER_AP_ADD:
887 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
888 break;
889 case WPS_EV_ER_AP_REMOVE:
890 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
891 break;
892 case WPS_EV_ER_ENROLLEE_ADD:
893 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
894 &data->enrollee);
895 break;
896 case WPS_EV_ER_ENROLLEE_REMOVE:
897 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
898 &data->enrollee);
899 break;
900 case WPS_EV_ER_AP_SETTINGS:
901 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
902 &data->ap_settings);
903 break;
904 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
905 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
906 &data->set_sel_reg);
907 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800908 case WPS_EV_AP_PIN_SUCCESS:
909 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 }
911}
912
913
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700914static int wpa_supplicant_wps_rf_band(void *ctx)
915{
916 struct wpa_supplicant *wpa_s = ctx;
917
918 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
919 return 0;
920
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700921 return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
922 (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700923}
924
925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
927{
928 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
929 eap_is_wps_pin_enrollee(&ssid->eap))
930 return WPS_REQ_ENROLLEE;
931 else
932 return WPS_REQ_REGISTRAR;
933}
934
935
936static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
937{
938 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700939 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
940
Dmitry Shmidt051af732013-10-22 13:52:46 -0700941 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700942 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700943
Jouni Malinen75ecf522011-06-27 15:19:46 -0700944 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800946 /* Enable the networks disabled during wpas_wps_reassoc */
947 wpas_wps_reenable_networks(wpa_s);
948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800950 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951
952 /* Remove any existing WPS network from configuration */
953 ssid = wpa_s->conf->ssid;
954 while (ssid) {
955 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
956 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800957 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700958 wpa_supplicant_deauthenticate(
959 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 }
961 id = ssid->id;
962 remove_ssid = ssid;
963 } else
964 id = -1;
965 ssid = ssid->next;
966 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700967 if (prev_current == remove_ssid) {
968 wpa_sm_set_config(wpa_s->wpa, NULL);
969 eapol_sm_notify_config(wpa_s->eapol, NULL,
970 NULL);
971 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 wpas_notify_network_removed(wpa_s, remove_ssid);
973 wpa_config_remove_network(wpa_s->conf, id);
974 }
975 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700976
977 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978}
979
980
981static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
982{
983 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800984 union wps_event_data data;
985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
987 "out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800988 os_memset(&data, 0, sizeof(data));
989 data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
990 data.fail.error_indication = WPS_EI_NO_ERROR;
991 /*
992 * Call wpas_notify_wps_event_fail() directly instead of through
993 * wpa_supplicant_wps_event() which would end up registering unnecessary
994 * timeouts (those are only for the case where the failure happens
995 * during an EAP-WSC exchange).
996 */
997 wpas_notify_wps_event_fail(wpa_s, &data.fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 wpas_clear_wps(wpa_s);
999}
1000
1001
1002static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001003 int registrar, const u8 *dev_addr,
1004 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005{
1006 struct wpa_ssid *ssid;
1007
1008 ssid = wpa_config_add_network(wpa_s->conf);
1009 if (ssid == NULL)
1010 return NULL;
1011 wpas_notify_network_added(wpa_s, ssid);
1012 wpa_config_set_network_defaults(ssid);
1013 ssid->temporary = 1;
1014 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
1015 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
1016 wpa_config_set(ssid, "identity", registrar ?
1017 "\"" WSC_ID_REGISTRAR "\"" :
1018 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1019 wpas_notify_network_removed(wpa_s, ssid);
1020 wpa_config_remove_network(wpa_s->conf, ssid->id);
1021 return NULL;
1022 }
1023
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001024#ifdef CONFIG_P2P
1025 if (dev_addr)
1026 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1027#endif /* CONFIG_P2P */
1028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 if (bssid) {
1030#ifndef CONFIG_P2P
1031 struct wpa_bss *bss;
1032 int count = 0;
1033#endif /* CONFIG_P2P */
1034
1035 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1036 ssid->bssid_set = 1;
1037
1038 /*
1039 * Note: With P2P, the SSID may change at the time the WPS
1040 * provisioning is started, so better not filter the AP based
1041 * on the current SSID in the scan results.
1042 */
1043#ifndef CONFIG_P2P
1044 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1045 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
1046 continue;
1047
1048 os_free(ssid->ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001049 ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 if (ssid->ssid == NULL)
1051 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 ssid->ssid_len = bss->ssid_len;
1053 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1054 "scan results",
1055 ssid->ssid, ssid->ssid_len);
1056 count++;
1057 }
1058
1059 if (count > 1) {
1060 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1061 "for the AP; use wildcard");
1062 os_free(ssid->ssid);
1063 ssid->ssid = NULL;
1064 ssid->ssid_len = 0;
1065 }
1066#endif /* CONFIG_P2P */
1067 }
1068
1069 return ssid;
1070}
1071
1072
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001073static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1074 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001075{
1076 struct wpa_ssid *ssid;
1077
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001078 if (wpa_s->current_ssid) {
1079 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001080 wpa_supplicant_deauthenticate(
1081 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001082 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 /* Mark all other networks disabled and trigger reassociation */
1085 ssid = wpa_s->conf->ssid;
1086 while (ssid) {
1087 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001088 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001089 /*
1090 * In case the network object corresponds to a persistent group
1091 * then do not send out network disabled signal. In addition,
1092 * do not change disabled status of persistent network objects
1093 * from 2 to 1 should we connect to another network.
1094 */
1095 if (was_disabled != 2) {
1096 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001097 if (was_disabled != ssid->disabled) {
1098 if (ssid->disabled)
1099 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001100 wpas_notify_network_enabled_changed(wpa_s,
1101 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001102 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 ssid = ssid->next;
1105 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001106}
1107
1108
1109static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001110 struct wpa_ssid *selected, const u8 *bssid,
1111 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001112{
1113 struct wpa_bss *bss;
1114
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001115 wpa_s->wps_run++;
1116 if (wpa_s->wps_run == 0)
1117 wpa_s->wps_run++;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001118 wpa_s->after_wps = 0;
1119 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001120 if (freq) {
1121 wpa_s->after_wps = 5;
1122 wpa_s->wps_freq = freq;
1123 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001124 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1125 if (bss && bss->freq > 0) {
1126 wpa_s->known_wps_freq = 1;
1127 wpa_s->wps_freq = bss->freq;
1128 }
1129 }
1130
1131 wpas_wps_temp_disable(wpa_s, selected);
1132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 wpa_s->disconnected = 0;
1134 wpa_s->reassociate = 1;
1135 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001136 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 wpa_s->wps_success = 0;
1138 wpa_s->blacklist_cleared = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001139
1140 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 wpa_supplicant_req_scan(wpa_s, 0, 0);
1142}
1143
1144
1145int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
Hai Shalom021b0b52019-04-10 11:17:58 -07001146 int p2p_group, int multi_ap_backhaul_sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147{
1148 struct wpa_ssid *ssid;
Hai Shalom021b0b52019-04-10 11:17:58 -07001149 char phase1[32];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001150
1151#ifdef CONFIG_AP
1152 if (wpa_s->ap_iface) {
1153 wpa_printf(MSG_DEBUG,
1154 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1155 return -1;
1156 }
1157#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001159 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 if (ssid == NULL)
1161 return -1;
1162 ssid->temporary = 1;
1163 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001164 /*
1165 * When starting a regular WPS process (not P2P group formation)
1166 * the registrar/final station can be either AP or PCP
1167 * so use a "don't care" value for the pbss flag.
1168 */
1169 if (!p2p_group)
1170 ssid->pbss = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171#ifdef CONFIG_P2P
1172 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1173 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1174 if (ssid->ssid) {
1175 ssid->ssid_len = wpa_s->go_params->ssid_len;
1176 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1177 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001178 if (wpa_s->go_params->freq > 56160) {
1179 /* P2P in 60 GHz uses PBSS */
1180 ssid->pbss = 1;
1181 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001182 if (wpa_s->go_params->edmg &&
1183 wpas_p2p_try_edmg_channel(wpa_s,
1184 wpa_s->go_params) == 0)
1185 ssid->enable_edmg = 1;
1186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1188 "SSID", ssid->ssid, ssid->ssid_len);
1189 }
1190 }
1191#endif /* CONFIG_P2P */
Hai Shalom021b0b52019-04-10 11:17:58 -07001192 os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
1193 multi_ap_backhaul_sta ? " multi_ap=1" : "");
1194 if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001195 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 if (wpa_s->wps_fragment_size)
1197 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Hai Shalom021b0b52019-04-10 11:17:58 -07001198 if (multi_ap_backhaul_sta)
1199 ssid->multi_ap_backhaul_sta = 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001200 wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1202 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001203 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 return 0;
1205}
1206
1207
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001208static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1209 const u8 *dev_addr, const u8 *bssid,
1210 const char *pin, int p2p_group, u16 dev_pw_id,
1211 const u8 *peer_pubkey_hash,
1212 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213{
1214 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001215 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001217 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001219#ifdef CONFIG_AP
1220 if (wpa_s->ap_iface) {
1221 wpa_printf(MSG_DEBUG,
1222 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1223 return -1;
1224 }
1225#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001227 if (bssid && is_zero_ether_addr(bssid))
1228 bssid = NULL;
1229 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1230 if (ssid == NULL) {
1231 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 ssid->temporary = 1;
1235 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001236 /*
1237 * When starting a regular WPS process (not P2P group formation)
1238 * the registrar/final station can be either AP or PCP
1239 * so use a "don't care" value for the pbss flag.
1240 */
1241 if (!p2p_group)
1242 ssid->pbss = 2;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001243 if (ssid_val) {
1244 ssid->ssid = os_malloc(ssid_len);
1245 if (ssid->ssid) {
1246 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1247 ssid->ssid_len = ssid_len;
1248 }
1249 }
1250 if (peer_pubkey_hash) {
1251 os_memcpy(hash, " pkhash=", 8);
1252 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1253 peer_pubkey_hash,
1254 WPS_OOB_PUBKEY_HASH_LEN);
1255 } else {
1256 hash[0] = '\0';
1257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258#ifdef CONFIG_P2P
1259 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001260 os_free(ssid->ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1262 if (ssid->ssid) {
1263 ssid->ssid_len = wpa_s->go_params->ssid_len;
1264 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1265 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001266 if (wpa_s->go_params->freq > 56160) {
1267 /* P2P in 60 GHz uses PBSS */
1268 ssid->pbss = 1;
1269 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001270 if (wpa_s->go_params->edmg &&
1271 wpas_p2p_try_edmg_channel(wpa_s,
1272 wpa_s->go_params) == 0)
1273 ssid->enable_edmg = 1;
1274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1276 "SSID", ssid->ssid, ssid->ssid_len);
1277 }
1278 }
1279#endif /* CONFIG_P2P */
1280 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001281 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1282 pin, dev_pw_id, hash);
1283 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1284 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1285 dev_pw_id, hash);
1286 } else {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001287 if (wps_generate_pin(&rpin) < 0) {
1288 wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1289 return -1;
1290 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001291 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1292 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001294 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1295 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001296 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001297 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001298
1299 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER)
1300 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_PIN_ACTIVE);
1301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 if (wpa_s->wps_fragment_size)
1303 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1304 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1305 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001306 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001307 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 return rpin;
1309}
1310
1311
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001312int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1313 const char *pin, int p2p_group, u16 dev_pw_id)
1314{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001315 os_get_reltime(&wpa_s->wps_pin_start_time);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001316 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1317 dev_pw_id, NULL, NULL, 0, 0);
1318}
1319
1320
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001321void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1322{
1323 union wps_event_data data;
1324
1325 os_memset(&data, 0, sizeof(data));
1326 data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1327 data.fail.error_indication = WPS_EI_NO_ERROR;
1328 /*
1329 * Call wpas_notify_wps_event_fail() directly instead of through
1330 * wpa_supplicant_wps_event() which would end up registering unnecessary
1331 * timeouts (those are only for the case where the failure happens
1332 * during an EAP-WSC exchange).
1333 */
1334 wpas_notify_wps_event_fail(wpa_s, &data.fail);
1335}
1336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337/* Cancel the wps pbc/pin requests */
1338int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1339{
1340#ifdef CONFIG_AP
1341 if (wpa_s->ap_iface) {
1342 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1343 return wpa_supplicant_ap_wps_cancel(wpa_s);
1344 }
1345#endif /* CONFIG_AP */
1346
Dmitry Shmidt04949592012-07-19 12:16:46 -07001347 if (wpa_s->wpa_state == WPA_SCANNING ||
1348 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1350 wpa_supplicant_cancel_scan(wpa_s);
1351 wpas_clear_wps(wpa_s);
1352 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1353 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1354 "deauthenticate");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001355 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 wpa_supplicant_deauthenticate(wpa_s,
1357 WLAN_REASON_DEAUTH_LEAVING);
1358 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001359 } else {
1360 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001361 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001362 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1363 0)
1364 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 }
1366
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001367 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CANCEL);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001368 wpa_s->after_wps = 0;
1369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 return 0;
1371}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372
1373
1374int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1375 const char *pin, struct wps_new_ap_settings *settings)
1376{
1377 struct wpa_ssid *ssid;
1378 char val[200];
1379 char *pos, *end;
1380 int res;
1381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001382#ifdef CONFIG_AP
1383 if (wpa_s->ap_iface) {
1384 wpa_printf(MSG_DEBUG,
1385 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1386 return -1;
1387 }
1388#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 if (!pin)
1390 return -1;
1391 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001392 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 if (ssid == NULL)
1394 return -1;
1395 ssid->temporary = 1;
1396 pos = val;
1397 end = pos + sizeof(val);
1398 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001399 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 return -1;
1401 pos += res;
1402 if (settings) {
1403 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1404 "new_encr=%s new_key=%s",
1405 settings->ssid_hex, settings->auth,
1406 settings->encr, settings->key_hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001407 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 return -1;
1409 pos += res;
1410 }
1411 res = os_snprintf(pos, end - pos, "\"");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001412 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001414 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1415 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 if (wpa_s->wps_fragment_size)
1417 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1418 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1419 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001420 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 return 0;
1422}
1423
1424
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001425static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1426 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 size_t psk_len)
1428{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001429 if (is_zero_ether_addr(p2p_dev_addr)) {
1430 wpa_printf(MSG_DEBUG,
1431 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1432 MAC2STR(mac_addr));
1433 } else {
1434 wpa_printf(MSG_DEBUG,
1435 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1436 " P2P Device Addr " MACSTR,
1437 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1438 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1440
1441 /* TODO */
1442
1443 return 0;
1444}
1445
1446
1447static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1448 const struct wps_device_data *dev)
1449{
1450 char uuid[40], txt[400];
1451 int len;
1452 char devtype[WPS_DEV_TYPE_BUFSIZE];
1453 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1454 return;
1455 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1456 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1457 " [%s|%s|%s|%s|%s|%s]",
1458 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1459 dev->manufacturer, dev->model_name,
1460 dev->model_number, dev->serial_number,
1461 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1462 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001463 if (!os_snprintf_error(sizeof(txt), len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 wpa_printf(MSG_INFO, "%s", txt);
1465}
1466
1467
1468static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1469 u16 sel_reg_config_methods)
1470{
1471#ifdef CONFIG_WPS_ER
1472 struct wpa_supplicant *wpa_s = ctx;
1473
1474 if (wpa_s->wps_er == NULL)
1475 return;
1476 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1477 "dev_password_id=%u sel_reg_config_methods=0x%x",
1478 sel_reg, dev_passwd_id, sel_reg_config_methods);
1479 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1480 sel_reg_config_methods);
1481#endif /* CONFIG_WPS_ER */
1482}
1483
1484
1485static u16 wps_fix_config_methods(u16 config_methods)
1486{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487 if ((config_methods &
1488 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1489 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1490 wpa_printf(MSG_INFO, "WPS: Converting display to "
1491 "virtual_display for WPS 2.0 compliance");
1492 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1493 }
1494 if ((config_methods &
1495 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1496 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1497 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1498 "virtual_push_button for WPS 2.0 compliance");
1499 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1500 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501
1502 return config_methods;
1503}
1504
1505
1506static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1507 struct wps_context *wps)
1508{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001509 char buf[50];
1510 const char *src;
1511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512 if (is_nil_uuid(wpa_s->conf->uuid)) {
1513 struct wpa_supplicant *first;
1514 first = wpa_s->global->ifaces;
1515 while (first && first->next)
1516 first = first->next;
1517 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001518 if (wps != wpa_s->global->ifaces->wps)
1519 os_memcpy(wps->uuid,
1520 wpa_s->global->ifaces->wps->uuid,
1521 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001522 src = "from the first interface";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001523 } else if (wpa_s->conf->auto_uuid == 1) {
1524 uuid_random(wps->uuid);
1525 src = "based on random data";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 } else {
1527 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001528 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529 }
1530 } else {
1531 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001532 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001534
1535 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1536 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537}
1538
1539
Dmitry Shmidt04949592012-07-19 12:16:46 -07001540static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1541 struct wps_context *wps)
1542{
1543 wpabuf_free(wps->dev.vendor_ext_m1);
1544 wps->dev.vendor_ext_m1 = NULL;
1545
1546 if (wpa_s->conf->wps_vendor_ext_m1) {
1547 wps->dev.vendor_ext_m1 =
1548 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1549 if (!wps->dev.vendor_ext_m1) {
1550 wpa_printf(MSG_ERROR, "WPS: Cannot "
1551 "allocate memory for vendor_ext_m1");
1552 }
1553 }
1554}
1555
1556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557int wpas_wps_init(struct wpa_supplicant *wpa_s)
1558{
1559 struct wps_context *wps;
1560 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561 struct hostapd_hw_modes *modes;
1562 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563
1564 wps = os_zalloc(sizeof(*wps));
1565 if (wps == NULL)
1566 return -1;
1567
1568 wps->cred_cb = wpa_supplicant_wps_cred;
1569 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001570 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 wps->cb_ctx = wpa_s;
1572
1573 wps->dev.device_name = wpa_s->conf->device_name;
1574 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1575 wps->dev.model_name = wpa_s->conf->model_name;
1576 wps->dev.model_number = wpa_s->conf->model_number;
1577 wps->dev.serial_number = wpa_s->conf->serial_number;
1578 wps->config_methods =
1579 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1580 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1581 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1582 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1583 "methods are not allowed at the same time");
1584 os_free(wps);
1585 return -1;
1586 }
1587 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001588 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1590 WPS_DEV_TYPE_LEN);
1591
1592 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1593 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1594 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1595
Dmitry Shmidt04949592012-07-19 12:16:46 -07001596 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001599 modes = wpa_s->hw.modes;
1600 if (modes) {
1601 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1602 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1603 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1604 wps->dev.rf_bands |= WPS_RF_24GHZ;
1605 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1606 wps->dev.rf_bands |= WPS_RF_50GHZ;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001607 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1608 wps->dev.rf_bands |= WPS_RF_60GHZ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001609 }
1610 }
1611 if (wps->dev.rf_bands == 0) {
1612 /*
1613 * Default to claiming support for both bands if the driver
1614 * does not provide support for fetching supported bands.
1615 */
1616 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1619 wpas_wps_set_uuid(wpa_s, wps);
1620
Hai Shalomb755a2a2020-04-23 21:49:02 -07001621#ifdef CONFIG_NO_TKIP
1622 wps->auth_types = WPS_AUTH_WPA2PSK;
1623 wps->encr_types = WPS_ENCR_AES;
1624#else /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1626 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
Hai Shalomb755a2a2020-04-23 21:49:02 -07001627#endif /* CONFIG_NO_TKIP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628
1629 os_memset(&rcfg, 0, sizeof(rcfg));
1630 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1631 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1632 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1633 rcfg.cb_ctx = wpa_s;
1634
1635 wps->registrar = wps_registrar_init(wps, &rcfg);
1636 if (wps->registrar == NULL) {
1637 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1638 os_free(wps);
1639 return -1;
1640 }
1641
1642 wpa_s->wps = wps;
1643
1644 return 0;
1645}
1646
1647
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001648#ifdef CONFIG_WPS_ER
1649static void wpas_wps_nfc_clear(struct wps_context *wps)
1650{
1651 wps->ap_nfc_dev_pw_id = 0;
1652 wpabuf_free(wps->ap_nfc_dh_pubkey);
1653 wps->ap_nfc_dh_pubkey = NULL;
1654 wpabuf_free(wps->ap_nfc_dh_privkey);
1655 wps->ap_nfc_dh_privkey = NULL;
1656 wpabuf_free(wps->ap_nfc_dev_pw);
1657 wps->ap_nfc_dev_pw = NULL;
1658}
1659#endif /* CONFIG_WPS_ER */
1660
1661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1663{
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001664 wpas_wps_assoc_with_cred_cancel(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001666 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001667 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001668 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001670#ifdef CONFIG_P2P
1671 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1672#endif /* CONFIG_P2P */
1673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 if (wpa_s->wps == NULL)
1675 return;
1676
1677#ifdef CONFIG_WPS_ER
1678 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1679 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001680 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681#endif /* CONFIG_WPS_ER */
1682
1683 wps_registrar_deinit(wpa_s->wps->registrar);
1684 wpabuf_free(wpa_s->wps->dh_pubkey);
1685 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001686 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 os_free(wpa_s->wps->network_key);
1688 os_free(wpa_s->wps);
1689 wpa_s->wps = NULL;
1690}
1691
1692
1693int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001694 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695{
1696 struct wpabuf *wps_ie;
1697
1698 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1699 return -1;
1700
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001701 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1703 if (!wps_ie) {
1704 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1705 return 0;
1706 }
1707
1708 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1709 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1710 "without active PBC Registrar");
1711 wpabuf_free(wps_ie);
1712 return 0;
1713 }
1714
1715 /* TODO: overlap detection */
1716 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1717 "(Active PBC)");
1718 wpabuf_free(wps_ie);
1719 return 1;
1720 }
1721
1722 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1723 if (!wps_ie) {
1724 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1725 return 0;
1726 }
1727
1728 /*
1729 * Start with WPS APs that advertise our address as an
1730 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1731 * allow any WPS AP after couple of scans since some APs do not
1732 * set Selected Registrar attribute properly when using
1733 * external Registrar.
1734 */
1735 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001736 struct os_reltime age;
1737
1738 os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1739
1740 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1741 age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1742 wpa_printf(MSG_DEBUG,
1743 " skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1744 wpa_s->scan_runs, (int) age.sec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 wpabuf_free(wps_ie);
1746 return 0;
1747 }
1748 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1749 } else {
1750 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1751 "(Authorized MAC or Active PIN)");
1752 }
1753 wpabuf_free(wps_ie);
1754 return 1;
1755 }
1756
1757 if (wps_ie) {
1758 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1759 wpabuf_free(wps_ie);
1760 return 1;
1761 }
1762
1763 return -1;
1764}
1765
1766
1767int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1768 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001769 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770{
1771 struct wpabuf *wps_ie = NULL;
1772 int ret = 0;
1773
1774 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001775 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1777 /* allow wildcard SSID for WPS PBC */
1778 ret = 1;
1779 }
1780 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001781 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 if (wps_ie &&
1783 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1784 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1785 /* allow wildcard SSID for WPS PIN */
1786 ret = 1;
1787 }
1788 }
1789
1790 if (!ret && ssid->bssid_set &&
1791 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1792 /* allow wildcard SSID due to hardcoded BSSID match */
1793 ret = 1;
1794 }
1795
1796#ifdef CONFIG_WPS_STRICT
1797 if (wps_ie) {
1798 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1799 0, bss->bssid) < 0)
1800 ret = 0;
1801 if (bss->beacon_ie_len) {
1802 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001803 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 bss, WPS_IE_VENDOR_TYPE);
1805 if (bcn_wps == NULL) {
1806 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1807 "missing from AP Beacon");
1808 ret = 0;
1809 } else {
1810 if (wps_validate_beacon(wps_ie) < 0)
1811 ret = 0;
1812 wpabuf_free(bcn_wps);
1813 }
1814 }
1815 }
1816#endif /* CONFIG_WPS_STRICT */
1817
1818 wpabuf_free(wps_ie);
1819
1820 return ret;
1821}
1822
1823
1824int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1825 struct wpa_bss *selected, struct wpa_ssid *ssid)
1826{
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001827 const u8 *sel_uuid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 struct wpabuf *wps_ie;
1829 int ret = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001830 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831
1832 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1833 return 0;
1834
1835 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1836 "present in scan results; selected BSSID " MACSTR,
1837 MAC2STR(selected->bssid));
Hai Shalomfdcde762020-04-02 11:19:20 -07001838 if (!is_zero_ether_addr(ssid->bssid))
1839 wpa_printf(MSG_DEBUG,
1840 "WPS: Network profile limited to accept only a single BSSID " MACSTR,
1841 MAC2STR(ssid->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842
1843 /* Make sure that only one AP is in active PBC mode */
1844 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1845 if (wps_ie) {
1846 sel_uuid = wps_get_uuid_e(wps_ie);
1847 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1848 sel_uuid, UUID_LEN);
1849 } else {
1850 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1851 "WPS IE?!");
1852 sel_uuid = NULL;
1853 }
1854
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001855 for (i = 0; i < wpa_s->num_wps_ap; i++) {
1856 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
1857
1858 if (!ap->pbc_active ||
1859 os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860 continue;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001861
Hai Shalomfdcde762020-04-02 11:19:20 -07001862 if (!is_zero_ether_addr(ssid->bssid) &&
1863 os_memcmp(ap->bssid, ssid->bssid, ETH_ALEN) != 0) {
1864 wpa_printf(MSG_DEBUG, "WPS: Ignore another BSS " MACSTR
1865 " in active PBC mode due to local BSSID limitation",
1866 MAC2STR(ap->bssid));
1867 continue;
1868 }
1869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001871 MACSTR, MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001873 ap->uuid, UUID_LEN);
1874 if (sel_uuid == NULL ||
1875 os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 ret = 1; /* PBC overlap */
1877 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1878 MACSTR " and " MACSTR,
1879 MAC2STR(selected->bssid),
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001880 MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 break;
1882 }
1883
1884 /* TODO: verify that this is reasonable dual-band situation */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 }
1886
1887 wpabuf_free(wps_ie);
1888
1889 return ret;
1890}
1891
1892
1893void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1894{
1895 struct wpa_bss *bss;
1896 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1897
1898 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1899 return;
1900
1901 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1902 struct wpabuf *ie;
1903 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1904 if (!ie)
1905 continue;
1906 if (wps_is_selected_pbc_registrar(ie))
1907 pbc++;
1908 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1909 auth++;
1910 else if (wps_is_selected_pin_registrar(ie))
1911 pin++;
1912 else
1913 wps++;
1914 wpabuf_free(ie);
1915 }
1916
1917 if (pbc)
1918 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1919 else if (auth)
1920 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1921 else if (pin)
1922 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1923 else if (wps)
1924 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1925}
1926
1927
1928int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1929{
1930 struct wpa_ssid *ssid;
1931
1932 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1933 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1934 return 1;
1935 }
1936
1937 return 0;
1938}
1939
1940
1941int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1942 char *end)
1943{
1944 struct wpabuf *wps_ie;
1945 int ret;
1946
1947 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1948 if (wps_ie == NULL)
1949 return 0;
1950
1951 ret = wps_attr_text(wps_ie, buf, end);
1952 wpabuf_free(wps_ie);
1953 return ret;
1954}
1955
1956
1957int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1958{
1959#ifdef CONFIG_WPS_ER
1960 if (wpa_s->wps_er) {
1961 wps_er_refresh(wpa_s->wps_er);
1962 return 0;
1963 }
1964 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1965 if (wpa_s->wps_er == NULL)
1966 return -1;
1967 return 0;
1968#else /* CONFIG_WPS_ER */
1969 return 0;
1970#endif /* CONFIG_WPS_ER */
1971}
1972
1973
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001974void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975{
1976#ifdef CONFIG_WPS_ER
1977 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1978 wpa_s->wps_er = NULL;
1979#endif /* CONFIG_WPS_ER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980}
1981
1982
1983#ifdef CONFIG_WPS_ER
1984int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1985 const char *uuid, const char *pin)
1986{
1987 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001988 const u8 *use_uuid = NULL;
1989 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001991 if (os_strcmp(uuid, "any") == 0) {
1992 } else if (uuid_str2bin(uuid, u) == 0) {
1993 use_uuid = u;
1994 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1995 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1996 if (use_uuid == NULL)
1997 return -1;
1998 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 return -1;
2000 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002001 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 (const u8 *) pin, os_strlen(pin), 300);
2003}
2004
2005
2006int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
2007{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002008 u8 u[UUID_LEN], *use_uuid = NULL;
2009 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002011 if (uuid_str2bin(uuid, u) == 0)
2012 use_uuid = u;
2013 else if (hwaddr_aton(uuid, addr) == 0)
2014 use_addr = addr;
2015 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002017 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018}
2019
2020
2021int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
2022 const char *pin)
2023{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002024 u8 u[UUID_LEN], *use_uuid = NULL;
2025 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002027 if (uuid_str2bin(uuid, u) == 0)
2028 use_uuid = u;
2029 else if (hwaddr_aton(uuid, addr) == 0)
2030 use_addr = addr;
2031 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002033
2034 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 os_strlen(pin));
2036}
2037
2038
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002039static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
2040 struct wps_credential *cred)
2041{
2042 os_memset(cred, 0, sizeof(*cred));
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002043 if (ssid->ssid_len > SSID_MAX_LEN)
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002044 return -1;
2045 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
2046 cred->ssid_len = ssid->ssid_len;
2047 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
2048 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
2049 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
2050 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
2051 cred->encr_type = WPS_ENCR_AES;
2052 else
2053 cred->encr_type = WPS_ENCR_TKIP;
2054 if (ssid->passphrase) {
2055 cred->key_len = os_strlen(ssid->passphrase);
2056 if (cred->key_len >= 64)
2057 return -1;
2058 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2059 } else if (ssid->psk_set) {
2060 cred->key_len = 32;
2061 os_memcpy(cred->key, ssid->psk, 32);
2062 } else
2063 return -1;
2064 } else {
2065 cred->auth_type = WPS_AUTH_OPEN;
2066 cred->encr_type = WPS_ENCR_NONE;
2067 }
2068
2069 return 0;
2070}
2071
2072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2074 int id)
2075{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002076 u8 u[UUID_LEN], *use_uuid = NULL;
2077 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 struct wpa_ssid *ssid;
2079 struct wps_credential cred;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002080 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002082 if (uuid_str2bin(uuid, u) == 0)
2083 use_uuid = u;
2084 else if (hwaddr_aton(uuid, addr) == 0)
2085 use_addr = addr;
2086 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087 return -1;
2088 ssid = wpa_config_get_network(wpa_s->conf, id);
2089 if (ssid == NULL || ssid->ssid == NULL)
2090 return -1;
2091
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002092 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002094 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2095 os_memset(&cred, 0, sizeof(cred));
2096 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097}
2098
2099
2100int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2101 const char *pin, struct wps_new_ap_settings *settings)
2102{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002103 u8 u[UUID_LEN], *use_uuid = NULL;
2104 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 struct wps_credential cred;
2106 size_t len;
2107
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002108 if (uuid_str2bin(uuid, u) == 0)
2109 use_uuid = u;
2110 else if (hwaddr_aton(uuid, addr) == 0)
2111 use_addr = addr;
2112 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 return -1;
2114 if (settings->ssid_hex == NULL || settings->auth == NULL ||
2115 settings->encr == NULL || settings->key_hex == NULL)
2116 return -1;
2117
2118 os_memset(&cred, 0, sizeof(cred));
2119 len = os_strlen(settings->ssid_hex);
2120 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2121 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2122 return -1;
2123 cred.ssid_len = len / 2;
2124
2125 len = os_strlen(settings->key_hex);
2126 if ((len & 1) || len > 2 * sizeof(cred.key) ||
2127 hexstr2bin(settings->key_hex, cred.key, len / 2))
2128 return -1;
2129 cred.key_len = len / 2;
2130
2131 if (os_strcmp(settings->auth, "OPEN") == 0)
2132 cred.auth_type = WPS_AUTH_OPEN;
2133 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2134 cred.auth_type = WPS_AUTH_WPAPSK;
2135 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2136 cred.auth_type = WPS_AUTH_WPA2PSK;
2137 else
2138 return -1;
2139
2140 if (os_strcmp(settings->encr, "NONE") == 0)
2141 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002142#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 else if (os_strcmp(settings->encr, "WEP") == 0)
2144 cred.encr_type = WPS_ENCR_WEP;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002145#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 else if (os_strcmp(settings->encr, "TKIP") == 0)
2147 cred.encr_type = WPS_ENCR_TKIP;
2148 else if (os_strcmp(settings->encr, "CCMP") == 0)
2149 cred.encr_type = WPS_ENCR_AES;
2150 else
2151 return -1;
2152
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002153 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2154 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155}
2156
2157
Dmitry Shmidt04949592012-07-19 12:16:46 -07002158#ifdef CONFIG_WPS_NFC
2159struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2160 int ndef, const char *uuid)
2161{
2162 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002163 u8 u[UUID_LEN], *use_uuid = NULL;
2164 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002165
2166 if (!wpa_s->wps_er)
2167 return NULL;
2168
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002169 if (uuid_str2bin(uuid, u) == 0)
2170 use_uuid = u;
2171 else if (hwaddr_aton(uuid, addr) == 0)
2172 use_addr = addr;
2173 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002174 return NULL;
2175
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002176 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002177 if (ndef && ret) {
2178 struct wpabuf *tmp;
2179 tmp = ndef_build_wifi(ret);
2180 wpabuf_free(ret);
2181 if (tmp == NULL)
2182 return NULL;
2183 ret = tmp;
2184 }
2185
2186 return ret;
2187}
2188#endif /* CONFIG_WPS_NFC */
2189
2190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191static int callbacks_pending = 0;
2192
2193static void wpas_wps_terminate_cb(void *ctx)
2194{
2195 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2196 if (--callbacks_pending <= 0)
2197 eloop_terminate();
2198}
2199#endif /* CONFIG_WPS_ER */
2200
2201
2202int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2203{
2204#ifdef CONFIG_WPS_ER
2205 if (wpa_s->wps_er) {
2206 callbacks_pending++;
2207 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2208 wpa_s->wps_er = NULL;
2209 return 1;
2210 }
2211#endif /* CONFIG_WPS_ER */
2212 return 0;
2213}
2214
2215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2217{
2218 struct wps_context *wps = wpa_s->wps;
2219
2220 if (wps == NULL)
2221 return;
2222
2223 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2224 wps->config_methods = wps_config_methods_str2bin(
2225 wpa_s->conf->config_methods);
2226 if ((wps->config_methods &
2227 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2228 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2229 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2230 "config methods are not allowed at the "
2231 "same time");
2232 wps->config_methods &= ~WPS_CONFIG_LABEL;
2233 }
2234 }
2235 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002236 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237
2238 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2239 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2240 WPS_DEV_TYPE_LEN);
2241
2242 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2243 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2244 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2245 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2246 }
2247
Dmitry Shmidt04949592012-07-19 12:16:46 -07002248 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2249 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2252 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2253
2254 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2255 wpas_wps_set_uuid(wpa_s, wps);
2256
2257 if (wpa_s->conf->changed_parameters &
2258 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2259 /* Update pointers to make sure they refer current values */
2260 wps->dev.device_name = wpa_s->conf->device_name;
2261 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2262 wps->dev.model_name = wpa_s->conf->model_name;
2263 wps->dev.model_number = wpa_s->conf->model_number;
2264 wps->dev.serial_number = wpa_s->conf->serial_number;
2265 }
2266}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002267
2268
Mikael Kanstrupcc779b82019-08-16 08:50:54 +02002269void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2270{
2271 struct wps_context *wps;
2272
2273 wps = wpa_s->wps;
2274 if (wps)
2275 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2276}
2277
2278
Dmitry Shmidt04949592012-07-19 12:16:46 -07002279#ifdef CONFIG_WPS_NFC
2280
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002281#ifdef CONFIG_WPS_ER
Roshan Pius8894db22017-02-13 14:43:20 -08002282struct wpabuf *
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002283wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2284 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002285{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002286 struct wpabuf *ret;
2287 struct wps_credential cred;
2288
2289 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2290 return NULL;
2291
2292 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2293
2294 if (ndef && ret) {
2295 struct wpabuf *tmp;
2296 tmp = ndef_build_wifi(ret);
2297 wpabuf_free(ret);
2298 if (tmp == NULL)
2299 return NULL;
2300 ret = tmp;
2301 }
2302
2303 return ret;
2304}
2305#endif /* CONFIG_WPS_ER */
2306
2307
2308struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2309 int ndef, const char *id_str)
2310{
2311#ifdef CONFIG_WPS_ER
2312 if (id_str) {
2313 int id;
2314 char *end = NULL;
2315 struct wpa_ssid *ssid;
2316
2317 id = strtol(id_str, &end, 10);
2318 if (end && *end)
2319 return NULL;
2320
2321 ssid = wpa_config_get_network(wpa_s->conf, id);
2322 if (ssid == NULL)
2323 return NULL;
2324 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2325 }
2326#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002327#ifdef CONFIG_AP
2328 if (wpa_s->ap_iface)
2329 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2330#endif /* CONFIG_AP */
2331 return NULL;
2332}
2333
2334
Dmitry Shmidt04949592012-07-19 12:16:46 -07002335struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2336{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002337 if (wpa_s->conf->wps_nfc_pw_from_config) {
2338 return wps_nfc_token_build(ndef,
2339 wpa_s->conf->wps_nfc_dev_pw_id,
2340 wpa_s->conf->wps_nfc_dh_pubkey,
2341 wpa_s->conf->wps_nfc_dev_pw);
2342 }
2343
Dmitry Shmidt04949592012-07-19 12:16:46 -07002344 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2345 &wpa_s->conf->wps_nfc_dh_pubkey,
2346 &wpa_s->conf->wps_nfc_dh_privkey,
2347 &wpa_s->conf->wps_nfc_dev_pw);
2348}
2349
2350
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002351int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2352 const u8 *bssid,
2353 const struct wpabuf *dev_pw, u16 dev_pw_id,
2354 int p2p_group, const u8 *peer_pubkey_hash,
2355 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002356{
2357 struct wps_context *wps = wpa_s->wps;
2358 char pw[32 * 2 + 1];
2359
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002360 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2361 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2362 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2363 }
2364
Dmitry Shmidt04949592012-07-19 12:16:46 -07002365 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002366 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2367 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2368 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002369 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002370 }
2371
2372 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2373 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2374 "cannot start NFC-triggered connection", dev_pw_id);
2375 return -1;
2376 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002377
2378 dh5_free(wps->dh_ctx);
2379 wpabuf_free(wps->dh_pubkey);
2380 wpabuf_free(wps->dh_privkey);
2381 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2382 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2383 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2384 wps->dh_ctx = NULL;
2385 wpabuf_free(wps->dh_pubkey);
2386 wps->dh_pubkey = NULL;
2387 wpabuf_free(wps->dh_privkey);
2388 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002389 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002390 return -1;
2391 }
2392 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002393 if (wps->dh_ctx == NULL) {
2394 wpabuf_free(wps->dh_pubkey);
2395 wps->dh_pubkey = NULL;
2396 wpabuf_free(wps->dh_privkey);
2397 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002398 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002399 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002400 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002401
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002402 if (dev_pw) {
2403 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2404 wpabuf_head(dev_pw),
2405 wpabuf_len(dev_pw));
2406 }
2407 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2408 dev_pw ? pw : NULL,
2409 p2p_group, dev_pw_id, peer_pubkey_hash,
2410 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002411}
2412
2413
2414static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2415 struct wps_parse_attr *attr)
2416{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002417 /*
2418 * Disable existing networks temporarily to allow the newly learned
2419 * credential to be preferred. Enable the temporarily disabled networks
2420 * after 10 seconds.
2421 */
2422 wpas_wps_temp_disable(wpa_s, NULL);
2423 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2424 NULL);
2425
Dmitry Shmidt04949592012-07-19 12:16:46 -07002426 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2427 return -1;
2428
2429 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2430 return 0;
2431
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002432 if (attr->ap_channel) {
2433 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002434 int freq = 0;
2435
2436 if (chan >= 1 && chan <= 13)
2437 freq = 2407 + 5 * chan;
2438 else if (chan == 14)
2439 freq = 2484;
2440 else if (chan >= 30)
2441 freq = 5000 + 5 * chan;
2442
2443 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002444 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2445 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002446 wpa_s->after_wps = 5;
2447 wpa_s->wps_freq = freq;
2448 }
2449 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002450
2451 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2452 "based on the received credential added");
2453 wpa_s->normal_scans = 0;
2454 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002455 wpa_s->disconnected = 0;
2456 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002457
2458 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002459 wpa_supplicant_req_scan(wpa_s, 0, 0);
2460
2461 return 0;
2462}
2463
2464
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002465#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002466static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2467 struct wps_parse_attr *attr)
2468{
2469 return wps_registrar_add_nfc_password_token(
2470 wpa_s->wps->registrar, attr->oob_dev_password,
2471 attr->oob_dev_password_len);
2472}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002473#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002474
2475
2476static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2477 const struct wpabuf *wps)
2478{
2479 struct wps_parse_attr attr;
2480
2481 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2482
2483 if (wps_parse_msg(wps, &attr)) {
2484 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2485 return -1;
2486 }
2487
2488 if (attr.num_cred)
2489 return wpas_wps_use_cred(wpa_s, &attr);
2490
2491#ifdef CONFIG_WPS_ER
2492 if (attr.oob_dev_password)
2493 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2494#endif /* CONFIG_WPS_ER */
2495
2496 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2497 return -1;
2498}
2499
2500
2501int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002502 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002503{
2504 const struct wpabuf *wps = data;
2505 struct wpabuf *tmp = NULL;
2506 int ret;
2507
2508 if (wpabuf_len(data) < 4)
2509 return -1;
2510
2511 if (*wpabuf_head_u8(data) != 0x10) {
2512 /* Assume this contains full NDEF record */
2513 tmp = ndef_parse_wifi(data);
2514 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002515#ifdef CONFIG_P2P
2516 tmp = ndef_parse_p2p(data);
2517 if (tmp) {
2518 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2519 forced_freq);
2520 wpabuf_free(tmp);
2521 return ret;
2522 }
2523#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002524 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2525 return -1;
2526 }
2527 wps = tmp;
2528 }
2529
2530 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2531 wpabuf_free(tmp);
2532 return ret;
2533}
2534
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002535
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002536struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2537 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002538{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002539 struct wpabuf *ret;
2540
2541 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2542 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2543 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2544 return NULL;
2545
2546 ret = wps_build_nfc_handover_req(wpa_s->wps,
2547 wpa_s->conf->wps_nfc_dh_pubkey);
2548
2549 if (ndef && ret) {
2550 struct wpabuf *tmp;
2551 tmp = ndef_build_wifi(ret);
2552 wpabuf_free(ret);
2553 if (tmp == NULL)
2554 return NULL;
2555 ret = tmp;
2556 }
2557
2558 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002559}
2560
2561
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002562#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002563
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002564static struct wpabuf *
2565wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2566 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002567{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002568#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002569 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002570 u8 u[UUID_LEN], *use_uuid = NULL;
2571 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002572 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002573
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002574 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002575 return NULL;
2576
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002577 if (uuid == NULL)
2578 return NULL;
2579 if (uuid_str2bin(uuid, u) == 0)
2580 use_uuid = u;
2581 else if (hwaddr_aton(uuid, addr) == 0)
2582 use_addr = addr;
2583 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002584 return NULL;
2585
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002586 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2587 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2588 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2589 return NULL;
2590 }
2591
2592 wpas_wps_nfc_clear(wps);
2593 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2594 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2595 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2596 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2597 wpas_wps_nfc_clear(wps);
2598 return NULL;
2599 }
2600
2601 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2602 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002603 if (ndef && ret) {
2604 struct wpabuf *tmp;
2605 tmp = ndef_build_wifi(ret);
2606 wpabuf_free(ret);
2607 if (tmp == NULL)
2608 return NULL;
2609 ret = tmp;
2610 }
2611
2612 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002613#else /* CONFIG_WPS_ER */
2614 return NULL;
2615#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002616}
2617#endif /* CONFIG_WPS_NFC */
2618
2619
2620struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2621 int ndef, int cr, const char *uuid)
2622{
2623 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002624 if (!cr)
2625 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002626 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2627 if (ret)
2628 return ret;
2629 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002630}
2631
2632
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002633static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2634 const struct wpabuf *data)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002635{
2636 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002637 int ret = -1;
2638 u16 wsc_len;
2639 const u8 *pos;
2640 struct wpabuf msg;
2641 struct wps_parse_attr attr;
2642 u16 dev_pw_id;
2643 const u8 *bssid = NULL;
2644 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002645
2646 wps = ndef_parse_wifi(data);
2647 if (wps == NULL)
2648 return -1;
2649 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2650 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002651 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2652 if (wpabuf_len(wps) < 2) {
2653 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2654 "Message");
2655 goto out;
2656 }
2657 pos = wpabuf_head(wps);
2658 wsc_len = WPA_GET_BE16(pos);
2659 if (wsc_len > wpabuf_len(wps) - 2) {
2660 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2661 "in Wi-Fi Handover Select Message", wsc_len);
2662 goto out;
2663 }
2664 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002665
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002666 wpa_hexdump(MSG_DEBUG,
2667 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2668 pos, wsc_len);
2669 if (wsc_len < wpabuf_len(wps) - 2) {
2670 wpa_hexdump(MSG_DEBUG,
2671 "WPS: Ignore extra data after WSC attributes",
2672 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2673 }
2674
2675 wpabuf_set(&msg, pos, wsc_len);
2676 ret = wps_parse_msg(&msg, &attr);
2677 if (ret < 0) {
2678 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2679 "Wi-Fi Handover Select Message");
2680 goto out;
2681 }
2682
2683 if (attr.oob_dev_password == NULL ||
2684 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2685 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2686 "included in Wi-Fi Handover Select Message");
2687 ret = -1;
2688 goto out;
2689 }
2690
2691 if (attr.ssid == NULL) {
2692 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2693 "Select Message");
2694 ret = -1;
2695 goto out;
2696 }
2697
2698 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2699
2700 if (attr.mac_addr) {
2701 bssid = attr.mac_addr;
2702 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2703 MAC2STR(bssid));
2704 }
2705
2706 if (attr.rf_bands)
2707 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2708
2709 if (attr.ap_channel) {
2710 u16 chan = WPA_GET_BE16(attr.ap_channel);
2711
2712 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2713
2714 if (chan >= 1 && chan <= 13 &&
2715 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2716 freq = 2407 + 5 * chan;
2717 else if (chan == 14 &&
2718 (attr.rf_bands == NULL ||
2719 *attr.rf_bands & WPS_RF_24GHZ))
2720 freq = 2484;
2721 else if (chan >= 30 &&
2722 (attr.rf_bands == NULL ||
2723 *attr.rf_bands & WPS_RF_50GHZ))
2724 freq = 5000 + 5 * chan;
Hai Shalomc3565922019-10-28 11:58:20 -07002725 else if (chan >= 1 && chan <= 6 &&
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002726 (attr.rf_bands == NULL ||
2727 *attr.rf_bands & WPS_RF_60GHZ))
2728 freq = 56160 + 2160 * chan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002729
2730 if (freq) {
2731 wpa_printf(MSG_DEBUG,
2732 "WPS: AP indicated channel %u -> %u MHz",
2733 chan, freq);
2734 }
2735 }
2736
2737 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2738 attr.oob_dev_password, attr.oob_dev_password_len);
2739 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2740 WPS_OOB_PUBKEY_HASH_LEN);
2741 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2742 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2743 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2744 ret = -1;
2745 goto out;
2746 }
2747 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2748 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2749
2750 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2751 attr.oob_dev_password,
2752 attr.ssid, attr.ssid_len, freq);
2753
2754out:
2755 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002756 return ret;
2757}
2758
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002759
2760int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2761 const struct wpabuf *req,
2762 const struct wpabuf *sel)
2763{
2764 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2765 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2766 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2767 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2768}
2769
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002770
2771int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2772 const struct wpabuf *req,
2773 const struct wpabuf *sel)
2774{
2775 struct wpabuf *wps;
2776 int ret = -1;
2777 u16 wsc_len;
2778 const u8 *pos;
2779 struct wpabuf msg;
2780 struct wps_parse_attr attr;
2781 u16 dev_pw_id;
2782
2783 /*
2784 * Enrollee/station is always initiator of the NFC connection handover,
2785 * so use the request message here to find Enrollee public key hash.
2786 */
2787 wps = ndef_parse_wifi(req);
2788 if (wps == NULL)
2789 return -1;
2790 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2791 "payload from NFC connection handover");
2792 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2793 if (wpabuf_len(wps) < 2) {
2794 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2795 "Message");
2796 goto out;
2797 }
2798 pos = wpabuf_head(wps);
2799 wsc_len = WPA_GET_BE16(pos);
2800 if (wsc_len > wpabuf_len(wps) - 2) {
2801 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2802 "in rt Wi-Fi Handover Request Message", wsc_len);
2803 goto out;
2804 }
2805 pos += 2;
2806
2807 wpa_hexdump(MSG_DEBUG,
2808 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2809 pos, wsc_len);
2810 if (wsc_len < wpabuf_len(wps) - 2) {
2811 wpa_hexdump(MSG_DEBUG,
2812 "WPS: Ignore extra data after WSC attributes",
2813 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2814 }
2815
2816 wpabuf_set(&msg, pos, wsc_len);
2817 ret = wps_parse_msg(&msg, &attr);
2818 if (ret < 0) {
2819 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2820 "Wi-Fi Handover Request Message");
2821 goto out;
2822 }
2823
2824 if (attr.oob_dev_password == NULL ||
2825 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2826 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2827 "included in Wi-Fi Handover Request Message");
2828 ret = -1;
2829 goto out;
2830 }
2831
2832 if (attr.uuid_e == NULL) {
2833 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2834 "Handover Request Message");
2835 ret = -1;
2836 goto out;
2837 }
2838
2839 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2840
2841 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2842 attr.oob_dev_password, attr.oob_dev_password_len);
2843 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2844 WPS_OOB_PUBKEY_HASH_LEN);
2845 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2846 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2847 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2848 ret = -1;
2849 goto out;
2850 }
2851 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2852 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2853
2854 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2855 attr.oob_dev_password,
2856 DEV_PW_NFC_CONNECTION_HANDOVER,
2857 NULL, 0, 1);
2858
2859out:
2860 wpabuf_free(wps);
2861 return ret;
2862}
2863
Dmitry Shmidt04949592012-07-19 12:16:46 -07002864#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002865
2866
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002867static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2868{
2869 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002870 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002871
2872 if (wpa_debug_level > MSG_DEBUG)
2873 return;
2874
2875 if (wpa_s->wps_ap == NULL)
2876 return;
2877
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002878 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002879
2880 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2881 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2882 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2883
2884 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2885 "tries=%d last_attempt=%d sec ago blacklist=%d",
2886 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2887 ap->last_attempt.sec > 0 ?
2888 (int) now.sec - (int) ap->last_attempt.sec : -1,
2889 e ? e->count : 0);
2890 }
2891}
2892
2893
2894static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2895 const u8 *bssid)
2896{
2897 size_t i;
2898
2899 if (wpa_s->wps_ap == NULL)
2900 return NULL;
2901
2902 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2903 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2904 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2905 return ap;
2906 }
2907
2908 return NULL;
2909}
2910
2911
2912static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2913 struct wpa_scan_res *res)
2914{
2915 struct wpabuf *wps;
2916 enum wps_ap_info_type type;
2917 struct wps_ap_info *ap;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002918 int r, pbc_active;
2919 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002920
2921 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2922 return;
2923
2924 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2925 if (wps == NULL)
2926 return;
2927
2928 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2929 if (r == 2)
2930 type = WPS_AP_SEL_REG_OUR;
2931 else if (r == 1)
2932 type = WPS_AP_SEL_REG;
2933 else
2934 type = WPS_AP_NOT_SEL_REG;
2935
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002936 uuid = wps_get_uuid_e(wps);
2937 pbc_active = wps_is_selected_pbc_registrar(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002938
2939 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2940 if (ap) {
2941 if (ap->type != type) {
2942 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2943 " changed type %d -> %d",
2944 MAC2STR(res->bssid), ap->type, type);
2945 ap->type = type;
2946 if (type != WPS_AP_NOT_SEL_REG)
2947 wpa_blacklist_del(wpa_s, ap->bssid);
2948 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002949 ap->pbc_active = pbc_active;
2950 if (uuid)
2951 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2952 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953 }
2954
2955 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2956 sizeof(struct wps_ap_info));
2957 if (ap == NULL)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002958 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002959
2960 wpa_s->wps_ap = ap;
2961 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2962 wpa_s->num_wps_ap++;
2963
2964 os_memset(ap, 0, sizeof(*ap));
2965 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2966 ap->type = type;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002967 ap->pbc_active = pbc_active;
2968 if (uuid)
2969 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002970 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2971 MAC2STR(ap->bssid), ap->type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002972
2973out:
2974 wpabuf_free(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002975}
2976
2977
2978void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2979 struct wpa_scan_results *scan_res)
2980{
2981 size_t i;
2982
2983 for (i = 0; i < scan_res->num; i++)
2984 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2985
2986 wpas_wps_dump_ap_info(wpa_s);
2987}
2988
2989
2990void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2991{
2992 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002993
2994 wpa_s->after_wps = 0;
2995
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002996 if (!wpa_s->wps_ap_iter)
2997 return;
2998 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2999 if (ap == NULL)
3000 return;
3001 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003002 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003003}