]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wps_supplicant.c
Add attribute for dwell time in QCA vendor scan
[thirdparty/hostap.git] / wpa_supplicant / wps_supplicant.c
CommitLineData
fa201b69
JM
1/*
2 * wpa_supplicant / WPS integration
95fb2db2 3 * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
fa201b69 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
fa201b69
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
03da66bd
JM
12#include "eloop.h"
13#include "uuid.h"
3f2c8ba6 14#include "crypto/random.h"
03da66bd 15#include "crypto/dh_group5.h"
90973fb2
JM
16#include "common/ieee802_11_defs.h"
17#include "common/ieee802_11_common.h"
18#include "common/wpa_common.h"
03da66bd
JM
19#include "common/wpa_ctrl.h"
20#include "eap_common/eap_wsc_common.h"
b01c18a8 21#include "eap_peer/eap.h"
20a0b03d 22#include "eapol_supp/eapol_supp_sm.h"
03da66bd 23#include "rsn_supp/wpa.h"
d7645d23 24#include "wps/wps_attr_parse.h"
03da66bd 25#include "config.h"
fa201b69 26#include "wpa_supplicant_i.h"
2d5b792d 27#include "driver_i.h"
8bac466b 28#include "notify.h"
a6099152 29#include "blacklist.h"
59f2caa9 30#include "bss.h"
9ba9fa07 31#include "scan.h"
2f9929ff 32#include "ap.h"
e9a7ae41 33#include "p2p/p2p.h"
b22128ef 34#include "p2p_supplicant.h"
fa201b69
JM
35#include "wps_supplicant.h"
36
f7e54365 37
31fcea93 38#ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
a6099152 39#define WPS_PIN_SCAN_IGNORE_SEL_REG 3
31fcea93 40#endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
fa201b69 41
e7d20342
HW
42/*
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
fcc60db4 50static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
469fc3a4 51static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
fcc60db4
JM
52
53
f9f0526b
JM
54static 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
150fd0b2
JM
63static 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
fa201b69
JM
82int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
83{
95fb2db2
JM
84 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
85 return 1;
95fb2db2 86
a6099152
JM
87 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
f9f0526b 108 wpas_wps_clear_ap_info(wpa_s);
fcc60db4 109 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
449adbac
CWY
110 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
111 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
fcc60db4 112
fa201b69
JM
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)) {
f981eabc 115 int disabled = wpa_s->current_ssid->disabled;
a565c23b 116 unsigned int freq = wpa_s->assoc_freq;
97236cee
MH
117 struct wpa_bss *bss;
118 struct wpa_ssid *ssid = NULL;
119 int use_fast_assoc = 0;
120
fa201b69 121 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
a565c23b
JM
122 "try to associate with the received credential "
123 "(freq=%u)", freq);
c2805909 124 wpa_s->own_disconnect_req = 1;
fa201b69
JM
125 wpa_supplicant_deauthenticate(wpa_s,
126 WLAN_REASON_DEAUTH_LEAVING);
f981eabc
JM
127 if (disabled) {
128 wpa_printf(MSG_DEBUG, "WPS: Current network is "
129 "disabled - wait for user to enable");
130 return 1;
131 }
17a4734d 132 wpa_s->after_wps = 5;
a565c23b 133 wpa_s->wps_freq = freq;
0b7a25c0 134 wpa_s->normal_scans = 0;
fa201b69 135 wpa_s->reassociate = 1;
97236cee
MH
136
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
150fd0b2
JM
153 /*
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);
fa201b69
JM
165 return 1;
166 }
167
47662164
JM
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);
c2805909 172 wpa_s->own_disconnect_req = 1;
47662164
JM
173 wpa_supplicant_deauthenticate(wpa_s,
174 WLAN_REASON_DEAUTH_LEAVING);
175 return 1;
176 }
177
fa201b69
JM
178 return 0;
179}
180
181
7cc1b6c9
JM
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;
59f2caa9 187 struct wpa_bss *bss;
7cc1b6c9
JM
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
d87e90b6
JM
203 if (ssid->ssid == NULL)
204 return;
59f2caa9 205 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
5f481b6f
JM
206 if (!bss)
207 bss = wpa_bss_get(wpa_s, wpa_s->bssid,
208 ssid->ssid, ssid->ssid_len);
59f2caa9
JM
209 if (bss == NULL) {
210 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
211 "table - use credential as-is");
7cc1b6c9
JM
212 return;
213 }
214
59f2caa9
JM
215 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
216
217 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
7cc1b6c9
JM
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 {
59f2caa9 223 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
7cc1b6c9
JM
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
741ed9fc
VK
266static 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 ||
d77419d2
AM
302 (ssid->group_cipher != new_ssid->group_cipher &&
303 !(ssid->group_cipher & new_ssid->group_cipher &
304 WPA_CIPHER_CCMP)))
741ed9fc
VK
305 continue;
306
e5a4b85b
HW
307 /*
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
741ed9fc
VK
352 /* 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);
4f9a5ea5
JM
355 if (wpa_s->current_ssid == ssid)
356 wpa_s->current_ssid = NULL;
741ed9fc
VK
357 wpa_config_remove_network(wpa_s->conf, ssid->id);
358 }
359}
360
361
bcbbc7af
JM
362static int wpa_supplicant_wps_cred(void *ctx,
363 const struct wps_credential *cred)
fa201b69
JM
364{
365 struct wpa_supplicant *wpa_s = ctx;
366 struct wpa_ssid *ssid = wpa_s->current_ssid;
49eba5f8 367 u16 auth_type;
2c4f80d5 368#ifdef CONFIG_WPS_REG_DISABLE_OPEN
f981eabc 369 int registrar = 0;
2c4f80d5 370#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
fa201b69 371
47662164
JM
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 }
8bac466b
JM
383
384 wpas_notify_wps_credential(wpa_s, cred);
47662164
JM
385 } else
386 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
fa201b69 387
eca6e0a9
JM
388 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
389 cred->cred_attr, cred->cred_attr_len);
390
47662164
JM
391 if (wpa_s->conf->wps_cred_processing == 1)
392 return 0;
393
73267b9c
JM
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
49eba5f8
JM
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 &&
49eba5f8
JM
412 auth_type != WPS_AUTH_WPAPSK &&
413 auth_type != WPS_AUTH_WPA2PSK) {
f286077d 414 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
49eba5f8
JM
415 "unsupported authentication type 0x%x",
416 auth_type);
f286077d
JM
417 return 0;
418 }
419
b363121a
JM
420 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
fa201b69
JM
429 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");
2c4f80d5 432#ifdef CONFIG_WPS_REG_DISABLE_OPEN
f981eabc
JM
433 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;
2c4f80d5 438#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
fa201b69
JM
439 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;
cae2119c 446 if (!ssid->p2p_group) {
2ff99b3c 447 ssid->temporary = 0;
cae2119c
JM
448 ssid->bssid_set = 0;
449 }
00e5e3d5
JM
450 ssid->disabled_until.sec = 0;
451 ssid->disabled_until.usec = 0;
452 ssid->auth_failures = 0;
fa201b69
JM
453 } 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;
ded4f944
JM
459 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 }
8bac466b 470 wpas_notify_network_added(wpa_s, ssid);
fa201b69
JM
471 }
472
473 wpa_config_set_network_defaults(ssid);
e5a4b85b 474 ssid->wps_run = wpa_s->wps_run;
fa201b69
JM
475
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:
fa201b69 485 break;
fa201b69
JM
486 case WPS_ENCR_TKIP:
487 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
fa201b69
JM
488 break;
489 case WPS_ENCR_AES:
490 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
d77419d2
AM
491 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 }
afd3cf14
JM
496 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 }
fa201b69
JM
506 break;
507 }
508
49eba5f8 509 switch (auth_type) {
fa201b69
JM
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;
f981eabc
JM
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 */
fa201b69 524 break;
fa201b69
JM
525 case WPS_AUTH_WPAPSK:
526 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
527 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
528 ssid->proto = WPA_PROTO_WPA;
529 break;
fa201b69
JM
530 case WPS_AUTH_WPA2PSK:
531 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
532 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
339dc8bd
JM
533 if (wpa_s->conf->wps_cred_add_sae &&
534 cred->key_len != 2 * PMK_LEN) {
535 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
339dc8bd 536 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
339dc8bd 537 }
fa201b69
JM
538 ssid->proto = WPA_PROTO_RSN;
539 break;
540 }
541
339dc8bd 542 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
fa201b69
JM
543 if (cred->key_len == 2 * PMK_LEN) {
544 if (hexstr2bin((const char *) cred->key, ssid->psk,
545 PMK_LEN)) {
546 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
547 "Key");
548 return -1;
549 }
550 ssid->psk_set = 1;
d1c8ac88 551 ssid->export_keys = 1;
fa201b69
JM
552 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
553 os_free(ssid->passphrase);
554 ssid->passphrase = os_malloc(cred->key_len + 1);
555 if (ssid->passphrase == NULL)
556 return -1;
557 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
558 ssid->passphrase[cred->key_len] = '\0';
559 wpa_config_update_psk(ssid);
d1c8ac88 560 ssid->export_keys = 1;
fa201b69
JM
561 } else {
562 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
563 "length %lu",
564 (unsigned long) cred->key_len);
565 return -1;
566 }
567 }
94687a0a 568 ssid->priority = wpa_s->conf->wps_priority;
fa201b69 569
7cc1b6c9
JM
570 wpas_wps_security_workaround(wpa_s, ssid, cred);
571
741ed9fc
VK
572 wpas_wps_remove_dup_network(wpa_s, ssid);
573
fa201b69
JM
574#ifndef CONFIG_NO_CONFIG_WRITE
575 if (wpa_s->conf->update_config &&
576 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
577 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
578 return -1;
579 }
580#endif /* CONFIG_NO_CONFIG_WRITE */
581
94687a0a
SD
582 if (ssid->priority)
583 wpa_config_update_prio_list(wpa_s->conf);
584
6fbcbc07
SD
585 /*
586 * Optimize the post-WPS scan based on the channel used during
587 * the provisioning in case EAP-Failure is not received.
588 */
589 wpa_s->after_wps = 5;
590 wpa_s->wps_freq = wpa_s->assoc_freq;
591
fa201b69
JM
592 return 0;
593}
594
595
4b68290e
JM
596static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
597 struct wps_event_m2d *m2d)
598{
599 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
600 "dev_password_id=%d config_error=%d",
601 m2d->dev_password_id, m2d->config_error);
8bac466b 602 wpas_notify_wps_event_m2d(wpa_s, m2d);
b89883a4 603#ifdef CONFIG_P2P
ba307f85
LD
604 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
605 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
b89883a4
JM
606 "dev_password_id=%d config_error=%d",
607 m2d->dev_password_id, m2d->config_error);
608 }
609 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
610 /*
611 * Notify P2P from eloop timeout to avoid issues with the
612 * interface getting removed while processing a message.
613 */
ace0fbdb 614 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
b89883a4
JM
615 NULL);
616 }
617#endif /* CONFIG_P2P */
4b68290e
JM
618}
619
620
2e145e91
JM
621static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
622{
623 struct wpa_supplicant *wpa_s = eloop_ctx;
624 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
625 wpas_clear_wps(wpa_s);
626}
627
628
469fc3a4
JM
629static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
630 struct wps_event_fail *fail)
631{
c5cf0a18
CWY
632 if (fail->error_indication > 0 &&
633 fail->error_indication < NUM_WPS_EI_VALUES) {
634 wpa_msg(wpa_s, MSG_INFO,
635 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
636 fail->msg, fail->config_error, fail->error_indication,
961750c1 637 wps_ei_str(fail->error_indication));
ba307f85
LD
638 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
639 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
c5cf0a18
CWY
640 "msg=%d config_error=%d reason=%d (%s)",
641 fail->msg, fail->config_error,
642 fail->error_indication,
961750c1 643 wps_ei_str(fail->error_indication));
c5cf0a18
CWY
644 } else {
645 wpa_msg(wpa_s, MSG_INFO,
646 WPS_EVENT_FAIL "msg=%d config_error=%d",
545cc6af 647 fail->msg, fail->config_error);
ba307f85
LD
648 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
649 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
c5cf0a18
CWY
650 "msg=%d config_error=%d",
651 fail->msg, fail->config_error);
652 }
2e145e91
JM
653
654 /*
655 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
656 */
657 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
658 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
659 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
660
8bac466b 661 wpas_notify_wps_event_fail(wpa_s, fail);
3734552f 662 wpas_p2p_wps_failed(wpa_s, fail);
469fc3a4
JM
663}
664
665
ec947ffc
SD
666static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
667
668static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
669{
670 struct wpa_ssid *ssid;
318e772a 671 int changed = 0;
ec947ffc
SD
672
673 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
674
675 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
676 if (ssid->disabled_for_connect && ssid->disabled) {
677 ssid->disabled_for_connect = 0;
678 ssid->disabled = 0;
679 wpas_notify_network_enabled_changed(wpa_s, ssid);
318e772a 680 changed++;
ec947ffc
SD
681 }
682 }
318e772a
JM
683
684 if (changed) {
685#ifndef CONFIG_NO_CONFIG_WRITE
686 if (wpa_s->conf->update_config &&
687 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
688 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
689 "configuration");
690 }
691#endif /* CONFIG_NO_CONFIG_WRITE */
692 }
ec947ffc
SD
693}
694
695
696static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
697{
698 struct wpa_supplicant *wpa_s = eloop_ctx;
699 /* Enable the networks disabled during wpas_wps_reassoc */
700 wpas_wps_reenable_networks(wpa_s);
701}
702
703
5e238cc6
SD
704int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
705{
706 return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
707 wpa_s, NULL);
708}
709
710
ad5302a1
JM
711static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
712{
713 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
a6099152 714 wpa_s->wps_success = 1;
8bac466b 715 wpas_notify_wps_event_success(wpa_s);
f3e907a7
JM
716 if (wpa_s->current_ssid)
717 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
718 wpa_s->extra_blacklist_count = 0;
ec947ffc
SD
719
720 /*
721 * Enable the networks disabled during wpas_wps_reassoc after 10
722 * seconds. The 10 seconds timer is to allow the data connection to be
723 * formed before allowing other networks to be selected.
724 */
725 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
726 NULL);
727
b22128ef 728 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
ad5302a1
JM
729}
730
731
b78bc3a3
JM
732static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
733 struct wps_event_er_ap *ap)
734{
735 char uuid_str[100];
ed45947e 736 char dev_type[WPS_DEV_TYPE_BUFSIZE];
e694b344 737
b78bc3a3 738 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
e694b344 739 if (ap->pri_dev_type)
ed45947e
JM
740 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
741 sizeof(dev_type));
e694b344
JM
742 else
743 dev_type[0] = '\0';
744
745 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
746 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
747 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
b78bc3a3
JM
748 ap->friendly_name ? ap->friendly_name : "",
749 ap->manufacturer ? ap->manufacturer : "",
750 ap->model_description ? ap->model_description : "",
751 ap->model_name ? ap->model_name : "",
752 ap->manufacturer_url ? ap->manufacturer_url : "",
753 ap->model_url ? ap->model_url : "");
754}
755
756
757static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
758 struct wps_event_er_ap *ap)
759{
760 char uuid_str[100];
761 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
762 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
763}
764
765
766static void wpa_supplicant_wps_event_er_enrollee_add(
767 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
768{
769 char uuid_str[100];
ed45947e 770 char dev_type[WPS_DEV_TYPE_BUFSIZE];
b78bc3a3
JM
771
772 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
773 if (enrollee->pri_dev_type)
ed45947e
JM
774 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
775 sizeof(dev_type));
b78bc3a3
JM
776 else
777 dev_type[0] = '\0';
778
779 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
780 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
781 "|%s|%s|%s|%s|%s|",
782 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
783 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
784 enrollee->dev_name ? enrollee->dev_name : "",
785 enrollee->manufacturer ? enrollee->manufacturer : "",
786 enrollee->model_name ? enrollee->model_name : "",
787 enrollee->model_number ? enrollee->model_number : "",
788 enrollee->serial_number ? enrollee->serial_number : "");
789}
790
791
792static void wpa_supplicant_wps_event_er_enrollee_remove(
793 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
794{
795 char uuid_str[100];
796 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
797 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
798 uuid_str, MAC2STR(enrollee->mac_addr));
799}
800
801
15dbf129
JM
802static void wpa_supplicant_wps_event_er_ap_settings(
803 struct wpa_supplicant *wpa_s,
804 struct wps_event_er_ap_settings *ap_settings)
805{
806 char uuid_str[100];
807 char key_str[65];
808 const struct wps_credential *cred = ap_settings->cred;
809
810 key_str[0] = '\0';
811 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
812 if (cred->key_len >= 8 && cred->key_len <= 64) {
813 os_memcpy(key_str, cred->key, cred->key_len);
814 key_str[cred->key_len] = '\0';
815 }
816 }
817
818 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
819 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
820 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
821 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
822 "key=%s",
823 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
824 cred->auth_type, cred->encr_type, key_str);
825}
826
827
3e7533b3
JM
828static void wpa_supplicant_wps_event_er_set_sel_reg(
829 struct wpa_supplicant *wpa_s,
830 struct wps_event_er_set_selected_registrar *ev)
831{
832 char uuid_str[100];
833
834 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
835 switch (ev->state) {
836 case WPS_ER_SET_SEL_REG_START:
837 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
838 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
839 "sel_reg_config_methods=0x%x",
840 uuid_str, ev->sel_reg, ev->dev_passwd_id,
841 ev->sel_reg_config_methods);
842 break;
843 case WPS_ER_SET_SEL_REG_DONE:
844 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
845 "uuid=%s state=DONE", uuid_str);
846 break;
847 case WPS_ER_SET_SEL_REG_FAILED:
848 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
849 "uuid=%s state=FAILED", uuid_str);
850 break;
851 }
852}
853
854
4b68290e
JM
855static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
856 union wps_event_data *data)
857{
858 struct wpa_supplicant *wpa_s = ctx;
859 switch (event) {
860 case WPS_EV_M2D:
861 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
862 break;
469fc3a4
JM
863 case WPS_EV_FAIL:
864 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
865 break;
ad5302a1
JM
866 case WPS_EV_SUCCESS:
867 wpa_supplicant_wps_event_success(wpa_s);
868 break;
3b2cf800 869 case WPS_EV_PWD_AUTH_FAIL:
70d84f11
JM
870#ifdef CONFIG_AP
871 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
872 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
873#endif /* CONFIG_AP */
3b2cf800 874 break;
63330c68
OK
875 case WPS_EV_PBC_OVERLAP:
876 break;
877 case WPS_EV_PBC_TIMEOUT:
878 break;
50396e29
JM
879 case WPS_EV_PBC_ACTIVE:
880 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
881 break;
882 case WPS_EV_PBC_DISABLE:
883 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
884 break;
b78bc3a3
JM
885 case WPS_EV_ER_AP_ADD:
886 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
887 break;
888 case WPS_EV_ER_AP_REMOVE:
889 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
890 break;
891 case WPS_EV_ER_ENROLLEE_ADD:
892 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
893 &data->enrollee);
894 break;
895 case WPS_EV_ER_ENROLLEE_REMOVE:
896 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
897 &data->enrollee);
898 break;
15dbf129
JM
899 case WPS_EV_ER_AP_SETTINGS:
900 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
901 &data->ap_settings);
902 break;
3e7533b3
JM
903 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
904 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
905 &data->set_sel_reg);
906 break;
32cdcf15
JM
907 case WPS_EV_AP_PIN_SUCCESS:
908 break;
4b68290e
JM
909 }
910}
911
912
239abaf2
DS
913static int wpa_supplicant_wps_rf_band(void *ctx)
914{
915 struct wpa_supplicant *wpa_s = ctx;
916
917 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
918 return 0;
919
01a02593
HK
920 return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
921 (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
239abaf2
DS
922}
923
924
f90c86d4 925enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
fa201b69 926{
b01c18a8
JM
927 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
928 eap_is_wps_pin_enrollee(&ssid->eap))
929 return WPS_REQ_ENROLLEE;
930 else
931 return WPS_REQ_REGISTRAR;
fa201b69 932}
116654ce
JM
933
934
fcc60db4
JM
935static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
936{
937 int id;
20a0b03d
JM
938 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
939
7255983b 940 wpa_s->after_wps = 0;
4d9fb08d 941 wpa_s->known_wps_freq = 0;
7255983b 942
20a0b03d 943 prev_current = wpa_s->current_ssid;
fcc60db4 944
ec947ffc
SD
945 /* Enable the networks disabled during wpas_wps_reassoc */
946 wpas_wps_reenable_networks(wpa_s);
947
fcc60db4 948 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
5890fa81 949 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
fcc60db4
JM
950
951 /* Remove any existing WPS network from configuration */
952 ssid = wpa_s->conf->ssid;
953 while (ssid) {
f0477201 954 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
8bac466b 955 if (ssid == wpa_s->current_ssid) {
c2805909 956 wpa_s->own_disconnect_req = 1;
c7a67a77
JZ
957 wpa_supplicant_deauthenticate(
958 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
8bac466b 959 }
fcc60db4 960 id = ssid->id;
8bac466b 961 remove_ssid = ssid;
f0477201 962 } else
fcc60db4
JM
963 id = -1;
964 ssid = ssid->next;
8bac466b 965 if (id >= 0) {
20a0b03d
JM
966 if (prev_current == remove_ssid) {
967 wpa_sm_set_config(wpa_s->wpa, NULL);
968 eapol_sm_notify_config(wpa_s->eapol, NULL,
969 NULL);
970 }
8bac466b 971 wpas_notify_network_removed(wpa_s, remove_ssid);
fcc60db4 972 wpa_config_remove_network(wpa_s->conf, id);
8bac466b 973 }
fcc60db4 974 }
f9f0526b
JM
975
976 wpas_wps_clear_ap_info(wpa_s);
fcc60db4
JM
977}
978
979
980static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
981{
982 struct wpa_supplicant *wpa_s = eloop_ctx;
893e2cf9
SB
983 union wps_event_data data;
984
014732ea
AC
985 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
986 "out");
893e2cf9
SB
987 os_memset(&data, 0, sizeof(data));
988 data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
989 data.fail.error_indication = WPS_EI_NO_ERROR;
990 /*
991 * Call wpas_notify_wps_event_fail() directly instead of through
992 * wpa_supplicant_wps_event() which would end up registering unnecessary
993 * timeouts (those are only for the case where the failure happens
994 * during an EAP-WSC exchange).
995 */
996 wpas_notify_wps_event_fail(wpa_s, &data.fail);
fcc60db4
JM
997 wpas_clear_wps(wpa_s);
998}
999
1000
1001static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
23318bea
JM
1002 int registrar, const u8 *dev_addr,
1003 const u8 *bssid)
fcc60db4
JM
1004{
1005 struct wpa_ssid *ssid;
1006
1007 ssid = wpa_config_add_network(wpa_s->conf);
1008 if (ssid == NULL)
1009 return NULL;
8bac466b 1010 wpas_notify_network_added(wpa_s, ssid);
fcc60db4 1011 wpa_config_set_network_defaults(ssid);
2ff99b3c 1012 ssid->temporary = 1;
fcc60db4
JM
1013 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
1014 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
1015 wpa_config_set(ssid, "identity", registrar ?
1016 "\"" WSC_ID_REGISTRAR "\"" :
1017 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
8bac466b 1018 wpas_notify_network_removed(wpa_s, ssid);
fcc60db4
JM
1019 wpa_config_remove_network(wpa_s->conf, ssid->id);
1020 return NULL;
1021 }
1022
23318bea
JM
1023#ifdef CONFIG_P2P
1024 if (dev_addr)
1025 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1026#endif /* CONFIG_P2P */
1027
fcc60db4 1028 if (bssid) {
b2c5a4a3 1029#ifndef CONFIG_P2P
59f2caa9 1030 struct wpa_bss *bss;
f7e54365 1031 int count = 0;
b2c5a4a3 1032#endif /* CONFIG_P2P */
fcc60db4
JM
1033
1034 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
24c23d1b 1035 ssid->bssid_set = 1;
fcc60db4 1036
b2c5a4a3
JM
1037 /*
1038 * Note: With P2P, the SSID may change at the time the WPS
1039 * provisioning is started, so better not filter the AP based
1040 * on the current SSID in the scan results.
1041 */
1042#ifndef CONFIG_P2P
59f2caa9
JM
1043 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1044 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
fcc60db4
JM
1045 continue;
1046
fcc60db4 1047 os_free(ssid->ssid);
a1f11e34 1048 ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
fcc60db4
JM
1049 if (ssid->ssid == NULL)
1050 break;
59f2caa9 1051 ssid->ssid_len = bss->ssid_len;
f7e54365
JM
1052 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1053 "scan results",
1054 ssid->ssid, ssid->ssid_len);
1055 count++;
1056 }
1057
1058 if (count > 1) {
1059 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1060 "for the AP; use wildcard");
1061 os_free(ssid->ssid);
1062 ssid->ssid = NULL;
1063 ssid->ssid_len = 0;
fcc60db4 1064 }
b2c5a4a3 1065#endif /* CONFIG_P2P */
fcc60db4
JM
1066 }
1067
1068 return ssid;
1069}
1070
1071
170f5663
JM
1072static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1073 struct wpa_ssid *selected)
fcc60db4
JM
1074{
1075 struct wpa_ssid *ssid;
1076
c2805909
JM
1077 if (wpa_s->current_ssid) {
1078 wpa_s->own_disconnect_req = 1;
d9d87c33
JM
1079 wpa_supplicant_deauthenticate(
1080 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
c2805909 1081 }
d9d87c33 1082
fcc60db4
JM
1083 /* Mark all other networks disabled and trigger reassociation */
1084 ssid = wpa_s->conf->ssid;
1085 while (ssid) {
8bac466b 1086 int was_disabled = ssid->disabled;
ec947ffc 1087 ssid->disabled_for_connect = 0;
c2762e41
JS
1088 /*
1089 * In case the network object corresponds to a persistent group
1090 * then do not send out network disabled signal. In addition,
1091 * do not change disabled status of persistent network objects
1092 * from 2 to 1 should we connect to another network.
1093 */
1094 if (was_disabled != 2) {
1095 ssid->disabled = ssid != selected;
ec947ffc
SD
1096 if (was_disabled != ssid->disabled) {
1097 if (ssid->disabled)
1098 ssid->disabled_for_connect = 1;
c2762e41
JS
1099 wpas_notify_network_enabled_changed(wpa_s,
1100 ssid);
ec947ffc 1101 }
c2762e41 1102 }
fcc60db4
JM
1103 ssid = ssid->next;
1104 }
170f5663
JM
1105}
1106
1107
1108static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
91a65018
JM
1109 struct wpa_ssid *selected, const u8 *bssid,
1110 int freq)
170f5663
JM
1111{
1112 struct wpa_bss *bss;
1113
e5a4b85b
HW
1114 wpa_s->wps_run++;
1115 if (wpa_s->wps_run == 0)
1116 wpa_s->wps_run++;
170f5663
JM
1117 wpa_s->after_wps = 0;
1118 wpa_s->known_wps_freq = 0;
91a65018
JM
1119 if (freq) {
1120 wpa_s->after_wps = 5;
1121 wpa_s->wps_freq = freq;
1122 } else if (bssid) {
170f5663
JM
1123 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1124 if (bss && bss->freq > 0) {
1125 wpa_s->known_wps_freq = 1;
1126 wpa_s->wps_freq = bss->freq;
1127 }
1128 }
1129
1130 wpas_wps_temp_disable(wpa_s, selected);
1131
fcc60db4
JM
1132 wpa_s->disconnected = 0;
1133 wpa_s->reassociate = 1;
a6099152 1134 wpa_s->scan_runs = 0;
0b7a25c0 1135 wpa_s->normal_scans = 0;
a6099152
JM
1136 wpa_s->wps_success = 0;
1137 wpa_s->blacklist_cleared = 0;
1cba9bea
JJ
1138
1139 wpa_supplicant_cancel_sched_scan(wpa_s);
fcc60db4
JM
1140 wpa_supplicant_req_scan(wpa_s, 0, 0);
1141}
1142
1143
9fa243b2 1144int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
83ebf558 1145 int p2p_group, int multi_ap_backhaul_sta)
fcc60db4
JM
1146{
1147 struct wpa_ssid *ssid;
83ebf558 1148 char phase1[32];
bd47d680
JM
1149
1150#ifdef CONFIG_AP
1151 if (wpa_s->ap_iface) {
1152 wpa_printf(MSG_DEBUG,
1153 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1154 return -1;
1155 }
1156#endif /* CONFIG_AP */
fcc60db4 1157 wpas_clear_wps(wpa_s);
23318bea 1158 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
fcc60db4
JM
1159 if (ssid == NULL)
1160 return -1;
9fa243b2
JM
1161 ssid->temporary = 1;
1162 ssid->p2p_group = p2p_group;
90f14962
LD
1163 /*
1164 * When starting a regular WPS process (not P2P group formation)
1165 * the registrar/final station can be either AP or PCP
1166 * so use a "don't care" value for the pbss flag.
1167 */
1168 if (!p2p_group)
1169 ssid->pbss = 2;
812bf56a 1170#ifdef CONFIG_P2P
e9a7ae41
JM
1171 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1172 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1173 if (ssid->ssid) {
1174 ssid->ssid_len = wpa_s->go_params->ssid_len;
1175 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1176 ssid->ssid_len);
b9074912
LD
1177 if (wpa_s->go_params->freq > 56160) {
1178 /* P2P in 60 GHz uses PBSS */
1179 ssid->pbss = 1;
1180 }
99666225
AM
1181 if (wpa_s->go_params->edmg &&
1182 wpas_p2p_try_edmg_channel(wpa_s,
1183 wpa_s->go_params) == 0)
1184 ssid->enable_edmg = 1;
1185
e9a7ae41
JM
1186 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1187 "SSID", ssid->ssid, ssid->ssid_len);
1188 }
1189 }
812bf56a 1190#endif /* CONFIG_P2P */
83ebf558
DL
1191 os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
1192 multi_ap_backhaul_sta ? " multi_ap=1" : "");
1193 if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
9c75e9fb 1194 return -1;
42f50264
JM
1195 if (wpa_s->wps_fragment_size)
1196 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
83ebf558
DL
1197 if (multi_ap_backhaul_sta)
1198 ssid->multi_ap_backhaul_sta = 1;
acdf5021 1199 wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
fcc60db4
JM
1200 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1201 wpa_s, NULL);
91a65018 1202 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
fcc60db4
JM
1203 return 0;
1204}
1205
1206
23318bea
JM
1207static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1208 const u8 *dev_addr, const u8 *bssid,
5f454557 1209 const char *pin, int p2p_group, u16 dev_pw_id,
57630e65 1210 const u8 *peer_pubkey_hash,
91a65018 1211 const u8 *ssid_val, size_t ssid_len, int freq)
fcc60db4
JM
1212{
1213 struct wpa_ssid *ssid;
5f454557 1214 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
fcc60db4 1215 unsigned int rpin = 0;
5f454557 1216 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
fcc60db4 1217
bd47d680
JM
1218#ifdef CONFIG_AP
1219 if (wpa_s->ap_iface) {
1220 wpa_printf(MSG_DEBUG,
1221 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1222 return -1;
1223 }
1224#endif /* CONFIG_AP */
fcc60db4 1225 wpas_clear_wps(wpa_s);
23318bea
JM
1226 if (bssid && is_zero_ether_addr(bssid))
1227 bssid = NULL;
1228 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
57630e65
JM
1229 if (ssid == NULL) {
1230 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
fcc60db4 1231 return -1;
57630e65 1232 }
9fa243b2
JM
1233 ssid->temporary = 1;
1234 ssid->p2p_group = p2p_group;
90f14962
LD
1235 /*
1236 * When starting a regular WPS process (not P2P group formation)
1237 * the registrar/final station can be either AP or PCP
1238 * so use a "don't care" value for the pbss flag.
1239 */
1240 if (!p2p_group)
1241 ssid->pbss = 2;
57630e65
JM
1242 if (ssid_val) {
1243 ssid->ssid = os_malloc(ssid_len);
1244 if (ssid->ssid) {
1245 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1246 ssid->ssid_len = ssid_len;
1247 }
1248 }
5f454557
JM
1249 if (peer_pubkey_hash) {
1250 os_memcpy(hash, " pkhash=", 8);
1251 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1252 peer_pubkey_hash,
1253 WPS_OOB_PUBKEY_HASH_LEN);
1254 } else {
1255 hash[0] = '\0';
1256 }
812bf56a 1257#ifdef CONFIG_P2P
e9a7ae41 1258 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
eda9d84d 1259 os_free(ssid->ssid);
e9a7ae41
JM
1260 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1261 if (ssid->ssid) {
1262 ssid->ssid_len = wpa_s->go_params->ssid_len;
1263 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1264 ssid->ssid_len);
b9074912
LD
1265 if (wpa_s->go_params->freq > 56160) {
1266 /* P2P in 60 GHz uses PBSS */
1267 ssid->pbss = 1;
1268 }
99666225
AM
1269 if (wpa_s->go_params->edmg &&
1270 wpas_p2p_try_edmg_channel(wpa_s,
1271 wpa_s->go_params) == 0)
1272 ssid->enable_edmg = 1;
1273
e9a7ae41
JM
1274 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1275 "SSID", ssid->ssid, ssid->ssid_len);
1276 }
1277 }
812bf56a 1278#endif /* CONFIG_P2P */
fcc60db4 1279 if (pin)
5f454557
JM
1280 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1281 pin, dev_pw_id, hash);
fa4c2988
JM
1282 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1283 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1284 dev_pw_id, hash);
1285 } else {
98a516ea
NL
1286 if (wps_generate_pin(&rpin) < 0) {
1287 wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1288 return -1;
1289 }
5f454557
JM
1290 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1291 rpin, dev_pw_id, hash);
fcc60db4 1292 }
fa4c2988
JM
1293 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1294 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
9c75e9fb 1295 return -1;
fa4c2988 1296 }
b1b62a13
BH
1297
1298 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER)
1299 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_PIN_ACTIVE);
1300
42f50264
JM
1301 if (wpa_s->wps_fragment_size)
1302 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
fcc60db4
JM
1303 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1304 wpa_s, NULL);
f9f0526b 1305 wpa_s->wps_ap_iter = 1;
91a65018 1306 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
fcc60db4
JM
1307 return rpin;
1308}
1309
1310
5f454557
JM
1311int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1312 const char *pin, int p2p_group, u16 dev_pw_id)
1313{
e7d20342 1314 os_get_reltime(&wpa_s->wps_pin_start_time);
23318bea 1315 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
91a65018 1316 dev_pw_id, NULL, NULL, 0, 0);
5f454557
JM
1317}
1318
1319
0a7b2a02
SB
1320void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1321{
1322 union wps_event_data data;
1323
1324 os_memset(&data, 0, sizeof(data));
1325 data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1326 data.fail.error_indication = WPS_EI_NO_ERROR;
1327 /*
1328 * Call wpas_notify_wps_event_fail() directly instead of through
1329 * wpa_supplicant_wps_event() which would end up registering unnecessary
1330 * timeouts (those are only for the case where the failure happens
1331 * during an EAP-WSC exchange).
1332 */
1333 wpas_notify_wps_event_fail(wpa_s, &data.fail);
1334}
1335
2f9929ff
AC
1336/* Cancel the wps pbc/pin requests */
1337int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1338{
1339#ifdef CONFIG_AP
1340 if (wpa_s->ap_iface) {
1341 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1342 return wpa_supplicant_ap_wps_cancel(wpa_s);
1343 }
1344#endif /* CONFIG_AP */
1345
476dea24
JM
1346 if (wpa_s->wpa_state == WPA_SCANNING ||
1347 wpa_s->wpa_state == WPA_DISCONNECTED) {
2f9929ff
AC
1348 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1349 wpa_supplicant_cancel_scan(wpa_s);
1350 wpas_clear_wps(wpa_s);
1351 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1352 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1353 "deauthenticate");
c2805909 1354 wpa_s->own_disconnect_req = 1;
2f9929ff
AC
1355 wpa_supplicant_deauthenticate(wpa_s,
1356 WLAN_REASON_DEAUTH_LEAVING);
1357 wpas_clear_wps(wpa_s);
ec947ffc
SD
1358 } else {
1359 wpas_wps_reenable_networks(wpa_s);
f9f0526b 1360 wpas_wps_clear_ap_info(wpa_s);
5890fa81
JM
1361 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1362 0)
1363 wpas_clear_wps(wpa_s);
ec947ffc 1364 }
2f9929ff 1365
b1b62a13 1366 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CANCEL);
7255983b
JM
1367 wpa_s->after_wps = 0;
1368
2f9929ff
AC
1369 return 0;
1370}
1371
1372
fcc60db4 1373int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
52eb293d 1374 const char *pin, struct wps_new_ap_settings *settings)
fcc60db4
JM
1375{
1376 struct wpa_ssid *ssid;
52eb293d
JM
1377 char val[200];
1378 char *pos, *end;
1379 int res;
fcc60db4 1380
bd47d680
JM
1381#ifdef CONFIG_AP
1382 if (wpa_s->ap_iface) {
1383 wpa_printf(MSG_DEBUG,
1384 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1385 return -1;
1386 }
1387#endif /* CONFIG_AP */
fcc60db4
JM
1388 if (!pin)
1389 return -1;
1390 wpas_clear_wps(wpa_s);
23318bea 1391 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
fcc60db4
JM
1392 if (ssid == NULL)
1393 return -1;
2ff99b3c 1394 ssid->temporary = 1;
52eb293d
JM
1395 pos = val;
1396 end = pos + sizeof(val);
1397 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
d85e1fc8 1398 if (os_snprintf_error(end - pos, res))
52eb293d
JM
1399 return -1;
1400 pos += res;
1401 if (settings) {
1402 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1403 "new_encr=%s new_key=%s",
1404 settings->ssid_hex, settings->auth,
1405 settings->encr, settings->key_hex);
d85e1fc8 1406 if (os_snprintf_error(end - pos, res))
52eb293d
JM
1407 return -1;
1408 pos += res;
1409 }
1410 res = os_snprintf(pos, end - pos, "\"");
d85e1fc8 1411 if (os_snprintf_error(end - pos, res))
52eb293d 1412 return -1;
9c75e9fb
JM
1413 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1414 return -1;
42f50264
JM
1415 if (wpa_s->wps_fragment_size)
1416 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
fcc60db4
JM
1417 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1418 wpa_s, NULL);
91a65018 1419 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
fcc60db4
JM
1420 return 0;
1421}
1422
1423
52177fbb
JM
1424static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1425 const u8 *p2p_dev_addr, const u8 *psk,
c5adf528
JM
1426 size_t psk_len)
1427{
52177fbb
JM
1428 if (is_zero_ether_addr(p2p_dev_addr)) {
1429 wpa_printf(MSG_DEBUG,
1430 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1431 MAC2STR(mac_addr));
1432 } else {
1433 wpa_printf(MSG_DEBUG,
1434 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1435 " P2P Device Addr " MACSTR,
1436 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1437 }
c5adf528
JM
1438 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1439
1440 /* TODO */
1441
1442 return 0;
1443}
1444
1445
1446static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1447 const struct wps_device_data *dev)
1448{
1449 char uuid[40], txt[400];
1450 int len;
96750ea5 1451 char devtype[WPS_DEV_TYPE_BUFSIZE];
c5adf528
JM
1452 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1453 return;
1454 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1455 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
96750ea5 1456 " [%s|%s|%s|%s|%s|%s]",
c5adf528
JM
1457 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1458 dev->manufacturer, dev->model_name,
1459 dev->model_number, dev->serial_number,
96750ea5
JM
1460 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1461 sizeof(devtype)));
a80ba67a 1462 if (!os_snprintf_error(sizeof(txt), len))
c5adf528
JM
1463 wpa_printf(MSG_INFO, "%s", txt);
1464}
1465
1466
72df2f5f
JM
1467static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1468 u16 sel_reg_config_methods)
1469{
1470#ifdef CONFIG_WPS_ER
1471 struct wpa_supplicant *wpa_s = ctx;
1472
1473 if (wpa_s->wps_er == NULL)
1474 return;
ad474118
JM
1475 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1476 "dev_password_id=%u sel_reg_config_methods=0x%x",
1477 sel_reg, dev_passwd_id, sel_reg_config_methods);
72df2f5f
JM
1478 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1479 sel_reg_config_methods);
1480#endif /* CONFIG_WPS_ER */
1481}
1482
1483
092acb54
JM
1484static u16 wps_fix_config_methods(u16 config_methods)
1485{
092acb54
JM
1486 if ((config_methods &
1487 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1488 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1489 wpa_printf(MSG_INFO, "WPS: Converting display to "
1490 "virtual_display for WPS 2.0 compliance");
1491 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1492 }
1493 if ((config_methods &
1494 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1495 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1496 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1497 "virtual_push_button for WPS 2.0 compliance");
1498 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1499 }
092acb54
JM
1500
1501 return config_methods;
1502}
1503
1504
85a821d6
JM
1505static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1506 struct wps_context *wps)
1507{
ef93abde
JM
1508 char buf[50];
1509 const char *src;
1510
85a821d6
JM
1511 if (is_nil_uuid(wpa_s->conf->uuid)) {
1512 struct wpa_supplicant *first;
1513 first = wpa_s->global->ifaces;
1514 while (first && first->next)
1515 first = first->next;
1516 if (first && first != wpa_s) {
157cdad5
BG
1517 if (wps != wpa_s->global->ifaces->wps)
1518 os_memcpy(wps->uuid,
1519 wpa_s->global->ifaces->wps->uuid,
1520 WPS_UUID_LEN);
ef93abde 1521 src = "from the first interface";
183d3924
JM
1522 } else if (wpa_s->conf->auto_uuid == 1) {
1523 uuid_random(wps->uuid);
1524 src = "based on random data";
85a821d6
JM
1525 } else {
1526 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
ef93abde 1527 src = "based on MAC address";
85a821d6
JM
1528 }
1529 } else {
1530 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
ef93abde 1531 src = "based on configuration";
85a821d6 1532 }
ef93abde
JM
1533
1534 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1535 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
85a821d6
JM
1536}
1537
1538
71dd3b78
AS
1539static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1540 struct wps_context *wps)
1541{
1542 wpabuf_free(wps->dev.vendor_ext_m1);
1543 wps->dev.vendor_ext_m1 = NULL;
1544
1545 if (wpa_s->conf->wps_vendor_ext_m1) {
1546 wps->dev.vendor_ext_m1 =
1547 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1548 if (!wps->dev.vendor_ext_m1) {
1549 wpa_printf(MSG_ERROR, "WPS: Cannot "
1550 "allocate memory for vendor_ext_m1");
1551 }
1552 }
1553}
1554
1555
116654ce
JM
1556int wpas_wps_init(struct wpa_supplicant *wpa_s)
1557{
1558 struct wps_context *wps;
c5adf528 1559 struct wps_registrar_config rcfg;
6fb6d842 1560 struct hostapd_hw_modes *modes;
6bf731e8 1561 u16 m;
116654ce
JM
1562
1563 wps = os_zalloc(sizeof(*wps));
1564 if (wps == NULL)
1565 return -1;
1566
1567 wps->cred_cb = wpa_supplicant_wps_cred;
4b68290e 1568 wps->event_cb = wpa_supplicant_wps_event;
239abaf2 1569 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
116654ce
JM
1570 wps->cb_ctx = wpa_s;
1571
3c0b7aa4
JM
1572 wps->dev.device_name = wpa_s->conf->device_name;
1573 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1574 wps->dev.model_name = wpa_s->conf->model_name;
1575 wps->dev.model_number = wpa_s->conf->model_number;
1576 wps->dev.serial_number = wpa_s->conf->serial_number;
c0e4dd9e
JM
1577 wps->config_methods =
1578 wps_config_methods_str2bin(wpa_s->conf->config_methods);
979b988e
JM
1579 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1580 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1581 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1582 "methods are not allowed at the same time");
1583 os_free(wps);
1584 return -1;
1585 }
092acb54 1586 wps->config_methods = wps_fix_config_methods(wps->config_methods);
a9355fac 1587 wps->dev.config_methods = wps->config_methods;
2f646b6e
JB
1588 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1589 WPS_DEV_TYPE_LEN);
a9e86bfb 1590
2f646b6e
JB
1591 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1592 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1593 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
a9e86bfb 1594
71dd3b78
AS
1595 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1596
3c0b7aa4 1597 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
6bf731e8 1598 modes = wpa_s->hw.modes;
6fb6d842 1599 if (modes) {
6bf731e8 1600 for (m = 0; m < wpa_s->hw.num_modes; m++) {
6fb6d842
BC
1601 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1602 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1603 wps->dev.rf_bands |= WPS_RF_24GHZ;
1604 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1605 wps->dev.rf_bands |= WPS_RF_50GHZ;
01a02593
HK
1606 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1607 wps->dev.rf_bands |= WPS_RF_60GHZ;
6fb6d842 1608 }
6fb6d842
BC
1609 }
1610 if (wps->dev.rf_bands == 0) {
1611 /*
1612 * Default to claiming support for both bands if the driver
1613 * does not provide support for fetching supported bands.
1614 */
1615 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1616 }
398cfbf6 1617 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
85a821d6 1618 wpas_wps_set_uuid(wpa_s, wps);
116654ce 1619
c5adf528
JM
1620 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1621 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1622
1623 os_memset(&rcfg, 0, sizeof(rcfg));
1624 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1625 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
72df2f5f 1626 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
c5adf528
JM
1627 rcfg.cb_ctx = wpa_s;
1628
1629 wps->registrar = wps_registrar_init(wps, &rcfg);
1630 if (wps->registrar == NULL) {
1631 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1632 os_free(wps);
1633 return -1;
1634 }
1635
116654ce
JM
1636 wpa_s->wps = wps;
1637
1638 return 0;
1639}
1640
1641
50d1f890
JM
1642#ifdef CONFIG_WPS_ER
1643static void wpas_wps_nfc_clear(struct wps_context *wps)
1644{
1645 wps->ap_nfc_dev_pw_id = 0;
1646 wpabuf_free(wps->ap_nfc_dh_pubkey);
1647 wps->ap_nfc_dh_pubkey = NULL;
1648 wpabuf_free(wps->ap_nfc_dh_privkey);
1649 wps->ap_nfc_dh_privkey = NULL;
1650 wpabuf_free(wps->ap_nfc_dev_pw);
1651 wps->ap_nfc_dev_pw = NULL;
1652}
1653#endif /* CONFIG_WPS_ER */
1654
1655
116654ce
JM
1656void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1657{
150fd0b2 1658 wpas_wps_assoc_with_cred_cancel(wpa_s);
fcc60db4 1659 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
2e145e91 1660 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
ec947ffc 1661 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
f9f0526b 1662 wpas_wps_clear_ap_info(wpa_s);
fcc60db4 1663
fb2ac53d
AC
1664#ifdef CONFIG_P2P
1665 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1666#endif /* CONFIG_P2P */
1667
116654ce
JM
1668 if (wpa_s->wps == NULL)
1669 return;
1670
a6b63078 1671#ifdef CONFIG_WPS_ER
1a1bf008 1672 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
a6b63078 1673 wpa_s->wps_er = NULL;
50d1f890 1674 wpas_wps_nfc_clear(wpa_s->wps);
a6b63078
JM
1675#endif /* CONFIG_WPS_ER */
1676
c5adf528 1677 wps_registrar_deinit(wpa_s->wps->registrar);
46bdb83a
MH
1678 wpabuf_free(wpa_s->wps->dh_pubkey);
1679 wpabuf_free(wpa_s->wps->dh_privkey);
71dd3b78 1680 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
116654ce
JM
1681 os_free(wpa_s->wps->network_key);
1682 os_free(wpa_s->wps);
1683 wpa_s->wps = NULL;
1684}
351f09a2
JM
1685
1686
a6099152 1687int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
620c7837 1688 struct wpa_ssid *ssid, struct wpa_bss *bss)
351f09a2
JM
1689{
1690 struct wpabuf *wps_ie;
1691
1692 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1693 return -1;
1694
620c7837 1695 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
351f09a2
JM
1696 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1697 if (!wps_ie) {
1698 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1699 return 0;
1700 }
1701
1702 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1703 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1704 "without active PBC Registrar");
1705 wpabuf_free(wps_ie);
1706 return 0;
1707 }
1708
1709 /* TODO: overlap detection */
1710 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1711 "(Active PBC)");
1712 wpabuf_free(wps_ie);
1713 return 1;
1714 }
1715
1716 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1717 if (!wps_ie) {
1718 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1719 return 0;
1720 }
1721
a6099152 1722 /*
31fcea93
JM
1723 * Start with WPS APs that advertise our address as an
1724 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1725 * allow any WPS AP after couple of scans since some APs do not
1726 * set Selected Registrar attribute properly when using
1727 * external Registrar.
a6099152 1728 */
31fcea93 1729 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
e7d20342
HW
1730 struct os_reltime age;
1731
1732 os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1733
1734 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1735 age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1736 wpa_printf(MSG_DEBUG,
1737 " skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1738 wpa_s->scan_runs, (int) age.sec);
a6099152
JM
1739 wpabuf_free(wps_ie);
1740 return 0;
1741 }
1742 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1743 } else {
1744 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
31fcea93 1745 "(Authorized MAC or Active PIN)");
351f09a2 1746 }
351f09a2
JM
1747 wpabuf_free(wps_ie);
1748 return 1;
1749 }
1750
1751 if (wps_ie) {
1752 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1753 wpabuf_free(wps_ie);
1754 return 1;
1755 }
1756
1757 return -1;
1758}
1759
1760
a6099152
JM
1761int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1762 struct wpa_ssid *ssid,
620c7837 1763 struct wpa_bss *bss)
351f09a2
JM
1764{
1765 struct wpabuf *wps_ie = NULL;
1766 int ret = 0;
1767
1768 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
620c7837 1769 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
351f09a2
JM
1770 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1771 /* allow wildcard SSID for WPS PBC */
1772 ret = 1;
1773 }
1774 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
620c7837 1775 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
a6099152 1776 if (wps_ie &&
31fcea93 1777 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
a6099152 1778 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
351f09a2
JM
1779 /* allow wildcard SSID for WPS PIN */
1780 ret = 1;
1781 }
1782 }
1783
24c23d1b
JM
1784 if (!ret && ssid->bssid_set &&
1785 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1786 /* allow wildcard SSID due to hardcoded BSSID match */
1787 ret = 1;
1788 }
1789
54f489be
JM
1790#ifdef CONFIG_WPS_STRICT
1791 if (wps_ie) {
1792 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
ff28ccaf 1793 0, bss->bssid) < 0)
54f489be
JM
1794 ret = 0;
1795 if (bss->beacon_ie_len) {
1796 struct wpabuf *bcn_wps;
620c7837 1797 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
54f489be
JM
1798 bss, WPS_IE_VENDOR_TYPE);
1799 if (bcn_wps == NULL) {
1800 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1801 "missing from AP Beacon");
1802 ret = 0;
1803 } else {
1804 if (wps_validate_beacon(wps_ie) < 0)
1805 ret = 0;
1806 wpabuf_free(bcn_wps);
1807 }
1808 }
1809 }
1810#endif /* CONFIG_WPS_STRICT */
1811
351f09a2
JM
1812 wpabuf_free(wps_ie);
1813
1814 return ret;
1815}
1816
1817
1818int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
6fa81a3b 1819 struct wpa_bss *selected, struct wpa_ssid *ssid)
351f09a2 1820{
e1dffa3b 1821 const u8 *sel_uuid;
351f09a2
JM
1822 struct wpabuf *wps_ie;
1823 int ret = 0;
e1dffa3b 1824 size_t i;
351f09a2
JM
1825
1826 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1827 return 0;
1828
9647120b
JM
1829 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1830 "present in scan results; selected BSSID " MACSTR,
1831 MAC2STR(selected->bssid));
3fadb1dc
JM
1832 if (!is_zero_ether_addr(ssid->bssid))
1833 wpa_printf(MSG_DEBUG,
1834 "WPS: Network profile limited to accept only a single BSSID " MACSTR,
1835 MAC2STR(ssid->bssid));
9647120b 1836
351f09a2 1837 /* Make sure that only one AP is in active PBC mode */
6fa81a3b 1838 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
9647120b 1839 if (wps_ie) {
351f09a2 1840 sel_uuid = wps_get_uuid_e(wps_ie);
9647120b
JM
1841 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1842 sel_uuid, UUID_LEN);
1843 } else {
1844 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1845 "WPS IE?!");
351f09a2 1846 sel_uuid = NULL;
9647120b 1847 }
351f09a2 1848
e1dffa3b
AS
1849 for (i = 0; i < wpa_s->num_wps_ap; i++) {
1850 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
1851
1852 if (!ap->pbc_active ||
1853 os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
351f09a2 1854 continue;
e1dffa3b 1855
3fadb1dc
JM
1856 if (!is_zero_ether_addr(ssid->bssid) &&
1857 os_memcmp(ap->bssid, ssid->bssid, ETH_ALEN) != 0) {
1858 wpa_printf(MSG_DEBUG, "WPS: Ignore another BSS " MACSTR
1859 " in active PBC mode due to local BSSID limitation",
1860 MAC2STR(ap->bssid));
1861 continue;
1862 }
1863
9647120b 1864 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
e1dffa3b 1865 MACSTR, MAC2STR(ap->bssid));
9647120b 1866 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
e1dffa3b
AS
1867 ap->uuid, UUID_LEN);
1868 if (sel_uuid == NULL ||
1869 os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0) {
351f09a2 1870 ret = 1; /* PBC overlap */
9647120b
JM
1871 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1872 MACSTR " and " MACSTR,
1873 MAC2STR(selected->bssid),
e1dffa3b 1874 MAC2STR(ap->bssid));
351f09a2
JM
1875 break;
1876 }
1877
1878 /* TODO: verify that this is reasonable dual-band situation */
1879 }
1880
1881 wpabuf_free(wps_ie);
1882
1883 return ret;
1884}
a524f05e
JM
1885
1886
1887void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1888{
6fa81a3b 1889 struct wpa_bss *bss;
6c6ad81f 1890 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
a524f05e
JM
1891
1892 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1893 return;
1894
6fa81a3b 1895 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
a524f05e 1896 struct wpabuf *ie;
6fa81a3b 1897 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
a524f05e
JM
1898 if (!ie)
1899 continue;
1900 if (wps_is_selected_pbc_registrar(ie))
6c6ad81f 1901 pbc++;
31fcea93 1902 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
6c6ad81f 1903 auth++;
a524f05e 1904 else if (wps_is_selected_pin_registrar(ie))
6c6ad81f 1905 pin++;
a524f05e 1906 else
6c6ad81f 1907 wps++;
a524f05e 1908 wpabuf_free(ie);
a524f05e 1909 }
6c6ad81f
JM
1910
1911 if (pbc)
1912 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1913 else if (auth)
1914 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1915 else if (pin)
1916 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1917 else if (wps)
1918 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
a524f05e 1919}
24c23d1b
JM
1920
1921
1922int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1923{
1924 struct wpa_ssid *ssid;
1925
1926 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1927 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1928 return 1;
1929 }
1930
1931 return 0;
1932}
611ed491
JM
1933
1934
1935int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1936 char *end)
1937{
1938 struct wpabuf *wps_ie;
1939 int ret;
1940
1941 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1942 if (wps_ie == NULL)
1943 return 0;
1944
1945 ret = wps_attr_text(wps_ie, buf, end);
1946 wpabuf_free(wps_ie);
1947 return ret;
1948}
e9bcfebf
JM
1949
1950
08486685 1951int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
e9bcfebf
JM
1952{
1953#ifdef CONFIG_WPS_ER
1954 if (wpa_s->wps_er) {
b3f371ca 1955 wps_er_refresh(wpa_s->wps_er);
e9bcfebf
JM
1956 return 0;
1957 }
08486685 1958 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
e9bcfebf
JM
1959 if (wpa_s->wps_er == NULL)
1960 return -1;
1961 return 0;
1962#else /* CONFIG_WPS_ER */
1963 return 0;
1964#endif /* CONFIG_WPS_ER */
1965}
1966
1967
f77cedc1 1968void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
e9bcfebf
JM
1969{
1970#ifdef CONFIG_WPS_ER
1a1bf008 1971 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
e9bcfebf
JM
1972 wpa_s->wps_er = NULL;
1973#endif /* CONFIG_WPS_ER */
e9bcfebf 1974}
72df2f5f
JM
1975
1976
1977#ifdef CONFIG_WPS_ER
31fcea93
JM
1978int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1979 const char *uuid, const char *pin)
72df2f5f
JM
1980{
1981 u8 u[UUID_LEN];
e205401c
JM
1982 const u8 *use_uuid = NULL;
1983 u8 addr_buf[ETH_ALEN];
72df2f5f 1984
e205401c
JM
1985 if (os_strcmp(uuid, "any") == 0) {
1986 } else if (uuid_str2bin(uuid, u) == 0) {
1987 use_uuid = u;
1988 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1989 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1990 if (use_uuid == NULL)
1991 return -1;
1992 } else
72df2f5f 1993 return -1;
31fcea93 1994 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
e205401c 1995 use_uuid,
72df2f5f
JM
1996 (const u8 *) pin, os_strlen(pin), 300);
1997}
564cd7fa
JM
1998
1999
2000int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
2001{
e205401c
JM
2002 u8 u[UUID_LEN], *use_uuid = NULL;
2003 u8 addr[ETH_ALEN], *use_addr = NULL;
564cd7fa 2004
e205401c
JM
2005 if (uuid_str2bin(uuid, u) == 0)
2006 use_uuid = u;
2007 else if (hwaddr_aton(uuid, addr) == 0)
2008 use_addr = addr;
2009 else
564cd7fa 2010 return -1;
e205401c 2011 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
564cd7fa 2012}
e64dcfd5
JM
2013
2014
2015int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
2016 const char *pin)
2017{
59307b30
JM
2018 u8 u[UUID_LEN], *use_uuid = NULL;
2019 u8 addr[ETH_ALEN], *use_addr = NULL;
e64dcfd5 2020
59307b30
JM
2021 if (uuid_str2bin(uuid, u) == 0)
2022 use_uuid = u;
2023 else if (hwaddr_aton(uuid, addr) == 0)
2024 use_addr = addr;
2025 else
e64dcfd5 2026 return -1;
59307b30
JM
2027
2028 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
e64dcfd5
JM
2029 os_strlen(pin));
2030}
1a1bf008
JM
2031
2032
88c8bf31
JM
2033static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
2034 struct wps_credential *cred)
2035{
2036 os_memset(cred, 0, sizeof(*cred));
d9d1b952 2037 if (ssid->ssid_len > SSID_MAX_LEN)
88c8bf31
JM
2038 return -1;
2039 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
2040 cred->ssid_len = ssid->ssid_len;
2041 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
2042 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
2043 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
2044 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
2045 cred->encr_type = WPS_ENCR_AES;
2046 else
2047 cred->encr_type = WPS_ENCR_TKIP;
2048 if (ssid->passphrase) {
2049 cred->key_len = os_strlen(ssid->passphrase);
2050 if (cred->key_len >= 64)
2051 return -1;
2052 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2053 } else if (ssid->psk_set) {
2054 cred->key_len = 32;
2055 os_memcpy(cred->key, ssid->psk, 32);
2056 } else
2057 return -1;
2058 } else {
2059 cred->auth_type = WPS_AUTH_OPEN;
2060 cred->encr_type = WPS_ENCR_NONE;
2061 }
2062
2063 return 0;
2064}
2065
2066
ef10f473
JM
2067int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2068 int id)
2069{
59307b30
JM
2070 u8 u[UUID_LEN], *use_uuid = NULL;
2071 u8 addr[ETH_ALEN], *use_addr = NULL;
ef10f473
JM
2072 struct wpa_ssid *ssid;
2073 struct wps_credential cred;
f42df144 2074 int ret;
ef10f473 2075
59307b30
JM
2076 if (uuid_str2bin(uuid, u) == 0)
2077 use_uuid = u;
2078 else if (hwaddr_aton(uuid, addr) == 0)
2079 use_addr = addr;
2080 else
ef10f473
JM
2081 return -1;
2082 ssid = wpa_config_get_network(wpa_s->conf, id);
2083 if (ssid == NULL || ssid->ssid == NULL)
2084 return -1;
2085
88c8bf31 2086 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
ef10f473 2087 return -1;
f42df144
JM
2088 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2089 os_memset(&cred, 0, sizeof(cred));
2090 return ret;
ef10f473
JM
2091}
2092
2093
7d6640a6
JM
2094int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2095 const char *pin, struct wps_new_ap_settings *settings)
2096{
59307b30
JM
2097 u8 u[UUID_LEN], *use_uuid = NULL;
2098 u8 addr[ETH_ALEN], *use_addr = NULL;
7d6640a6
JM
2099 struct wps_credential cred;
2100 size_t len;
2101
59307b30
JM
2102 if (uuid_str2bin(uuid, u) == 0)
2103 use_uuid = u;
2104 else if (hwaddr_aton(uuid, addr) == 0)
2105 use_addr = addr;
2106 else
7d6640a6
JM
2107 return -1;
2108 if (settings->ssid_hex == NULL || settings->auth == NULL ||
2109 settings->encr == NULL || settings->key_hex == NULL)
2110 return -1;
2111
2112 os_memset(&cred, 0, sizeof(cred));
2113 len = os_strlen(settings->ssid_hex);
2114 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2115 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2116 return -1;
2117 cred.ssid_len = len / 2;
2118
2119 len = os_strlen(settings->key_hex);
2120 if ((len & 1) || len > 2 * sizeof(cred.key) ||
2121 hexstr2bin(settings->key_hex, cred.key, len / 2))
2122 return -1;
2123 cred.key_len = len / 2;
2124
2125 if (os_strcmp(settings->auth, "OPEN") == 0)
2126 cred.auth_type = WPS_AUTH_OPEN;
2127 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2128 cred.auth_type = WPS_AUTH_WPAPSK;
2129 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2130 cred.auth_type = WPS_AUTH_WPA2PSK;
2131 else
2132 return -1;
2133
2134 if (os_strcmp(settings->encr, "NONE") == 0)
2135 cred.encr_type = WPS_ENCR_NONE;
dc390043 2136#ifdef CONFIG_TESTING_OPTIONS
7d6640a6
JM
2137 else if (os_strcmp(settings->encr, "WEP") == 0)
2138 cred.encr_type = WPS_ENCR_WEP;
dc390043 2139#endif /* CONFIG_TESTING_OPTIONS */
7d6640a6
JM
2140 else if (os_strcmp(settings->encr, "TKIP") == 0)
2141 cred.encr_type = WPS_ENCR_TKIP;
2142 else if (os_strcmp(settings->encr, "CCMP") == 0)
2143 cred.encr_type = WPS_ENCR_AES;
2144 else
2145 return -1;
2146
59307b30
JM
2147 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2148 (const u8 *) pin, os_strlen(pin), &cred);
7d6640a6
JM
2149}
2150
2151
1cea09a9
JM
2152#ifdef CONFIG_WPS_NFC
2153struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2154 int ndef, const char *uuid)
2155{
2156 struct wpabuf *ret;
59307b30
JM
2157 u8 u[UUID_LEN], *use_uuid = NULL;
2158 u8 addr[ETH_ALEN], *use_addr = NULL;
1cea09a9
JM
2159
2160 if (!wpa_s->wps_er)
2161 return NULL;
2162
59307b30
JM
2163 if (uuid_str2bin(uuid, u) == 0)
2164 use_uuid = u;
2165 else if (hwaddr_aton(uuid, addr) == 0)
2166 use_addr = addr;
2167 else
1cea09a9
JM
2168 return NULL;
2169
59307b30 2170 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
1cea09a9
JM
2171 if (ndef && ret) {
2172 struct wpabuf *tmp;
2173 tmp = ndef_build_wifi(ret);
2174 wpabuf_free(ret);
2175 if (tmp == NULL)
2176 return NULL;
2177 ret = tmp;
2178 }
2179
2180 return ret;
2181}
2182#endif /* CONFIG_WPS_NFC */
2183
2184
4c9695be
BG
2185static int callbacks_pending = 0;
2186
1a1bf008
JM
2187static void wpas_wps_terminate_cb(void *ctx)
2188{
2189 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
4c9695be
BG
2190 if (--callbacks_pending <= 0)
2191 eloop_terminate();
1a1bf008
JM
2192}
2193#endif /* CONFIG_WPS_ER */
2194
2195
2196int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2197{
2198#ifdef CONFIG_WPS_ER
2199 if (wpa_s->wps_er) {
4c9695be 2200 callbacks_pending++;
1a1bf008
JM
2201 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2202 wpa_s->wps_er = NULL;
2203 return 1;
2204 }
72df2f5f 2205#endif /* CONFIG_WPS_ER */
1a1bf008
JM
2206 return 0;
2207}
41e650ae
JM
2208
2209
611aea7d
JM
2210void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2211{
2212 struct wps_context *wps = wpa_s->wps;
2213
2214 if (wps == NULL)
2215 return;
2216
2217 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2218 wps->config_methods = wps_config_methods_str2bin(
2219 wpa_s->conf->config_methods);
2220 if ((wps->config_methods &
2221 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2222 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2223 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2224 "config methods are not allowed at the "
2225 "same time");
2226 wps->config_methods &= ~WPS_CONFIG_LABEL;
2227 }
2228 }
092acb54 2229 wps->config_methods = wps_fix_config_methods(wps->config_methods);
c8b245b6 2230 wps->dev.config_methods = wps->config_methods;
611aea7d 2231
2f646b6e
JB
2232 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2233 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2234 WPS_DEV_TYPE_LEN);
611aea7d 2235
a9e86bfb 2236 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2f646b6e
JB
2237 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2238 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2239 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
a9e86bfb
JMB
2240 }
2241
71dd3b78
AS
2242 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2243 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2244
611aea7d
JM
2245 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2246 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2247
85a821d6
JM
2248 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2249 wpas_wps_set_uuid(wpa_s, wps);
dcf788d1 2250
1c9cb49f
JM
2251 if (wpa_s->conf->changed_parameters &
2252 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
dcf788d1
JM
2253 /* Update pointers to make sure they refer current values */
2254 wps->dev.device_name = wpa_s->conf->device_name;
2255 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2256 wps->dev.model_name = wpa_s->conf->model_name;
2257 wps->dev.model_number = wpa_s->conf->model_number;
2258 wps->dev.serial_number = wpa_s->conf->serial_number;
2259 }
611aea7d 2260}
3f2c8ba6
JM
2261
2262
8788a314
MK
2263void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2264{
2265 struct wps_context *wps;
2266
2267 wps = wpa_s->wps;
2268 if (wps)
2269 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2270}
2271
2272
3f2c8ba6
JM
2273#ifdef CONFIG_WPS_NFC
2274
88c8bf31
JM
2275#ifdef CONFIG_WPS_ER
2276static struct wpabuf *
2277wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2278 struct wpa_ssid *ssid)
2279{
2280 struct wpabuf *ret;
2281 struct wps_credential cred;
2282
2283 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2284 return NULL;
2285
2286 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2287
2288 if (ndef && ret) {
2289 struct wpabuf *tmp;
2290 tmp = ndef_build_wifi(ret);
2291 wpabuf_free(ret);
2292 if (tmp == NULL)
2293 return NULL;
2294 ret = tmp;
2295 }
2296
2297 return ret;
2298}
2299#endif /* CONFIG_WPS_ER */
2300
2301
bbf41865 2302struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
88c8bf31 2303 int ndef, const char *id_str)
bbf41865 2304{
88c8bf31
JM
2305#ifdef CONFIG_WPS_ER
2306 if (id_str) {
2307 int id;
2308 char *end = NULL;
2309 struct wpa_ssid *ssid;
2310
2311 id = strtol(id_str, &end, 10);
2312 if (end && *end)
2313 return NULL;
2314
2315 ssid = wpa_config_get_network(wpa_s->conf, id);
2316 if (ssid == NULL)
2317 return NULL;
2318 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2319 }
2320#endif /* CONFIG_WPS_ER */
bbf41865
JM
2321#ifdef CONFIG_AP
2322 if (wpa_s->ap_iface)
2323 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2324#endif /* CONFIG_AP */
2325 return NULL;
2326}
2327
2328
3f2c8ba6
JM
2329struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2330{
042ec551
JM
2331 if (wpa_s->conf->wps_nfc_pw_from_config) {
2332 return wps_nfc_token_build(ndef,
2333 wpa_s->conf->wps_nfc_dev_pw_id,
2334 wpa_s->conf->wps_nfc_dh_pubkey,
2335 wpa_s->conf->wps_nfc_dev_pw);
2336 }
2337
bfc62fe1
JM
2338 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2339 &wpa_s->conf->wps_nfc_dh_pubkey,
2340 &wpa_s->conf->wps_nfc_dh_privkey,
2341 &wpa_s->conf->wps_nfc_dev_pw);
3f2c8ba6
JM
2342}
2343
2344
23318bea
JM
2345int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2346 const u8 *bssid,
5f454557 2347 const struct wpabuf *dev_pw, u16 dev_pw_id,
57630e65 2348 int p2p_group, const u8 *peer_pubkey_hash,
91a65018 2349 const u8 *ssid, size_t ssid_len, int freq)
3f2c8ba6
JM
2350{
2351 struct wps_context *wps = wpa_s->wps;
2352 char pw[32 * 2 + 1];
2353
fa4c2988 2354 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
5f454557
JM
2355 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2356 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2357 }
2358
3f2c8ba6 2359 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
fa4c2988
JM
2360 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2361 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2362 "cannot start NFC-triggered connection");
2363 return -1;
2364 }
2365
2366 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2367 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2368 "cannot start NFC-triggered connection", dev_pw_id);
3f2c8ba6 2369 return -1;
fa4c2988 2370 }
3f2c8ba6
JM
2371
2372 dh5_free(wps->dh_ctx);
2373 wpabuf_free(wps->dh_pubkey);
2374 wpabuf_free(wps->dh_privkey);
2375 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2376 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2377 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2378 wps->dh_ctx = NULL;
2379 wpabuf_free(wps->dh_pubkey);
2380 wps->dh_pubkey = NULL;
2381 wpabuf_free(wps->dh_privkey);
2382 wps->dh_privkey = NULL;
fa4c2988 2383 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
3f2c8ba6
JM
2384 return -1;
2385 }
2386 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
5c9d63d4
JM
2387 if (wps->dh_ctx == NULL) {
2388 wpabuf_free(wps->dh_pubkey);
2389 wps->dh_pubkey = NULL;
2390 wpabuf_free(wps->dh_privkey);
2391 wps->dh_privkey = NULL;
fa4c2988 2392 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
3f2c8ba6 2393 return -1;
5c9d63d4 2394 }
3f2c8ba6 2395
fa4c2988
JM
2396 if (dev_pw) {
2397 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2398 wpabuf_head(dev_pw),
2399 wpabuf_len(dev_pw));
2400 }
23318bea
JM
2401 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2402 dev_pw ? pw : NULL,
fa4c2988 2403 p2p_group, dev_pw_id, peer_pubkey_hash,
91a65018 2404 ssid, ssid_len, freq);
3f2c8ba6
JM
2405}
2406
d7645d23
JM
2407
2408static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2409 struct wps_parse_attr *attr)
2410{
170f5663
JM
2411 /*
2412 * Disable existing networks temporarily to allow the newly learned
2413 * credential to be preferred. Enable the temporarily disabled networks
2414 * after 10 seconds.
2415 */
2416 wpas_wps_temp_disable(wpa_s, NULL);
2417 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2418 NULL);
2419
d7645d23
JM
2420 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2421 return -1;
2422
2423 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2424 return 0;
2425
0b5ff2ae
JM
2426 if (attr->ap_channel) {
2427 u16 chan = WPA_GET_BE16(attr->ap_channel);
bd3a3737
JM
2428 int freq = 0;
2429
2430 if (chan >= 1 && chan <= 13)
2431 freq = 2407 + 5 * chan;
2432 else if (chan == 14)
2433 freq = 2484;
2434 else if (chan >= 30)
2435 freq = 5000 + 5 * chan;
2436
2437 if (freq) {
0b5ff2ae
JM
2438 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2439 chan, freq);
bd3a3737
JM
2440 wpa_s->after_wps = 5;
2441 wpa_s->wps_freq = freq;
2442 }
2443 }
0b5ff2ae
JM
2444
2445 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2446 "based on the received credential added");
2447 wpa_s->normal_scans = 0;
2448 wpa_supplicant_reinit_autoscan(wpa_s);
d7645d23
JM
2449 wpa_s->disconnected = 0;
2450 wpa_s->reassociate = 1;
7e910b7b
JJ
2451
2452 wpa_supplicant_cancel_sched_scan(wpa_s);
d7645d23
JM
2453 wpa_supplicant_req_scan(wpa_s, 0, 0);
2454
2455 return 0;
2456}
2457
2458
6df865fa 2459#ifdef CONFIG_WPS_ER
e6ea2a45
JM
2460static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2461 struct wps_parse_attr *attr)
2462{
e6ea2a45
JM
2463 return wps_registrar_add_nfc_password_token(
2464 wpa_s->wps->registrar, attr->oob_dev_password,
2465 attr->oob_dev_password_len);
2466}
6df865fa 2467#endif /* CONFIG_WPS_ER */
e6ea2a45
JM
2468
2469
d7645d23
JM
2470static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2471 const struct wpabuf *wps)
2472{
2473 struct wps_parse_attr attr;
2474
2475 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2476
2477 if (wps_parse_msg(wps, &attr)) {
2478 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2479 return -1;
2480 }
2481
2482 if (attr.num_cred)
2483 return wpas_wps_use_cred(wpa_s, &attr);
2484
e6ea2a45
JM
2485#ifdef CONFIG_WPS_ER
2486 if (attr.oob_dev_password)
2487 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2488#endif /* CONFIG_WPS_ER */
2489
d7645d23
JM
2490 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2491 return -1;
2492}
2493
2494
2495int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
b56f6c88 2496 const struct wpabuf *data, int forced_freq)
d7645d23
JM
2497{
2498 const struct wpabuf *wps = data;
2499 struct wpabuf *tmp = NULL;
2500 int ret;
2501
2502 if (wpabuf_len(data) < 4)
2503 return -1;
2504
2505 if (*wpabuf_head_u8(data) != 0x10) {
2506 /* Assume this contains full NDEF record */
2507 tmp = ndef_parse_wifi(data);
2508 if (tmp == NULL) {
dd37a938
JM
2509#ifdef CONFIG_P2P
2510 tmp = ndef_parse_p2p(data);
2511 if (tmp) {
b56f6c88
JM
2512 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2513 forced_freq);
dd37a938
JM
2514 wpabuf_free(tmp);
2515 return ret;
2516 }
2517#endif /* CONFIG_P2P */
d7645d23
JM
2518 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2519 return -1;
2520 }
2521 wps = tmp;
2522 }
2523
2524 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2525 wpabuf_free(tmp);
2526 return ret;
2527}
2528
e65552dd 2529
41f9ffb6
JM
2530struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2531 int ndef)
e65552dd 2532{
41f9ffb6
JM
2533 struct wpabuf *ret;
2534
2535 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2536 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2537 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2538 return NULL;
2539
2540 ret = wps_build_nfc_handover_req(wpa_s->wps,
2541 wpa_s->conf->wps_nfc_dh_pubkey);
2542
2543 if (ndef && ret) {
2544 struct wpabuf *tmp;
2545 tmp = ndef_build_wifi(ret);
2546 wpabuf_free(ret);
2547 if (tmp == NULL)
2548 return NULL;
2549 ret = tmp;
2550 }
2551
2552 return ret;
e65552dd
JM
2553}
2554
2555
f3f2ba2e 2556#ifdef CONFIG_WPS_NFC
50d1f890 2557
c5838683
JM
2558static struct wpabuf *
2559wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2560 const char *uuid)
f3f2ba2e 2561{
94d7acf3 2562#ifdef CONFIG_WPS_ER
f3f2ba2e 2563 struct wpabuf *ret;
59307b30
JM
2564 u8 u[UUID_LEN], *use_uuid = NULL;
2565 u8 addr[ETH_ALEN], *use_addr = NULL;
d4b4d7fe 2566 struct wps_context *wps = wpa_s->wps;
f3f2ba2e 2567
d4b4d7fe 2568 if (wps == NULL)
f3f2ba2e
JM
2569 return NULL;
2570
59307b30
JM
2571 if (uuid == NULL)
2572 return NULL;
2573 if (uuid_str2bin(uuid, u) == 0)
2574 use_uuid = u;
2575 else if (hwaddr_aton(uuid, addr) == 0)
2576 use_addr = addr;
2577 else
f3f2ba2e
JM
2578 return NULL;
2579
50d1f890 2580 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
50d1f890
JM
2581 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2582 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2583 return NULL;
d4b4d7fe 2584 }
50d1f890 2585
d4b4d7fe
JM
2586 wpas_wps_nfc_clear(wps);
2587 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2588 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2589 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2590 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
50d1f890 2591 wpas_wps_nfc_clear(wps);
d4b4d7fe 2592 return NULL;
50d1f890
JM
2593 }
2594
2595 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2596 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
f3f2ba2e
JM
2597 if (ndef && ret) {
2598 struct wpabuf *tmp;
2599 tmp = ndef_build_wifi(ret);
2600 wpabuf_free(ret);
2601 if (tmp == NULL)
2602 return NULL;
2603 ret = tmp;
2604 }
2605
2606 return ret;
94d7acf3
JM
2607#else /* CONFIG_WPS_ER */
2608 return NULL;
2609#endif /* CONFIG_WPS_ER */
f3f2ba2e
JM
2610}
2611#endif /* CONFIG_WPS_NFC */
2612
2613
5ab9a6a5 2614struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
f3f2ba2e 2615 int ndef, int cr, const char *uuid)
e65552dd 2616{
f3f2ba2e 2617 struct wpabuf *ret;
5ab9a6a5
JM
2618 if (!cr)
2619 return NULL;
f3f2ba2e
JM
2620 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2621 if (ret)
2622 return ret;
2623 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
e65552dd
JM
2624}
2625
2626
679f2e7c
JM
2627static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2628 const struct wpabuf *data)
e65552dd
JM
2629{
2630 struct wpabuf *wps;
fa4c2988
JM
2631 int ret = -1;
2632 u16 wsc_len;
2633 const u8 *pos;
2634 struct wpabuf msg;
2635 struct wps_parse_attr attr;
2636 u16 dev_pw_id;
91a65018
JM
2637 const u8 *bssid = NULL;
2638 int freq = 0;
e65552dd
JM
2639
2640 wps = ndef_parse_wifi(data);
2641 if (wps == NULL)
2642 return -1;
2643 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2644 "payload from NFC connection handover");
fa4c2988
JM
2645 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2646 if (wpabuf_len(wps) < 2) {
2647 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2648 "Message");
2649 goto out;
2650 }
2651 pos = wpabuf_head(wps);
2652 wsc_len = WPA_GET_BE16(pos);
2653 if (wsc_len > wpabuf_len(wps) - 2) {
2654 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2655 "in Wi-Fi Handover Select Message", wsc_len);
2656 goto out;
2657 }
2658 pos += 2;
2659
2660 wpa_hexdump(MSG_DEBUG,
2661 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2662 pos, wsc_len);
2663 if (wsc_len < wpabuf_len(wps) - 2) {
2664 wpa_hexdump(MSG_DEBUG,
2665 "WPS: Ignore extra data after WSC attributes",
2666 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2667 }
2668
2669 wpabuf_set(&msg, pos, wsc_len);
2670 ret = wps_parse_msg(&msg, &attr);
2671 if (ret < 0) {
2672 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2673 "Wi-Fi Handover Select Message");
2674 goto out;
2675 }
2676
2677 if (attr.oob_dev_password == NULL ||
2678 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2679 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2680 "included in Wi-Fi Handover Select Message");
2681 ret = -1;
2682 goto out;
2683 }
2684
2685 if (attr.ssid == NULL) {
2686 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2687 "Select Message");
2688 ret = -1;
2689 goto out;
2690 }
2691
2692 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2693
91a65018
JM
2694 if (attr.mac_addr) {
2695 bssid = attr.mac_addr;
2696 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2697 MAC2STR(bssid));
2698 }
2699
2700 if (attr.rf_bands)
2701 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2702
2703 if (attr.ap_channel) {
2704 u16 chan = WPA_GET_BE16(attr.ap_channel);
2705
2706 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2707
2708 if (chan >= 1 && chan <= 13 &&
2709 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2710 freq = 2407 + 5 * chan;
2711 else if (chan == 14 &&
2712 (attr.rf_bands == NULL ||
2713 *attr.rf_bands & WPS_RF_24GHZ))
2714 freq = 2484;
2715 else if (chan >= 30 &&
2716 (attr.rf_bands == NULL ||
2717 *attr.rf_bands & WPS_RF_50GHZ))
2718 freq = 5000 + 5 * chan;
dc6c3be4 2719 else if (chan >= 1 && chan <= 6 &&
01a02593
HK
2720 (attr.rf_bands == NULL ||
2721 *attr.rf_bands & WPS_RF_60GHZ))
2722 freq = 56160 + 2160 * chan;
91a65018
JM
2723
2724 if (freq) {
2725 wpa_printf(MSG_DEBUG,
2726 "WPS: AP indicated channel %u -> %u MHz",
2727 chan, freq);
2728 }
2729 }
2730
fa4c2988
JM
2731 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2732 attr.oob_dev_password, attr.oob_dev_password_len);
2733 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2734 WPS_OOB_PUBKEY_HASH_LEN);
2735 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2736 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2737 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2738 ret = -1;
2739 goto out;
2740 }
2741 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2742 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2743
91a65018 2744 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
fa4c2988 2745 attr.oob_dev_password,
91a65018 2746 attr.ssid, attr.ssid_len, freq);
fa4c2988
JM
2747
2748out:
e65552dd 2749 wpabuf_free(wps);
e65552dd
JM
2750 return ret;
2751}
2752
e4758827
JM
2753
2754int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2755 const struct wpabuf *req,
2756 const struct wpabuf *sel)
2757{
2758 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2759 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2760 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2761 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2762}
2763
50d1f890
JM
2764
2765int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2766 const struct wpabuf *req,
2767 const struct wpabuf *sel)
2768{
2769 struct wpabuf *wps;
2770 int ret = -1;
2771 u16 wsc_len;
2772 const u8 *pos;
2773 struct wpabuf msg;
2774 struct wps_parse_attr attr;
2775 u16 dev_pw_id;
2776
2777 /*
2778 * Enrollee/station is always initiator of the NFC connection handover,
2779 * so use the request message here to find Enrollee public key hash.
2780 */
2781 wps = ndef_parse_wifi(req);
2782 if (wps == NULL)
2783 return -1;
2784 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2785 "payload from NFC connection handover");
2786 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2787 if (wpabuf_len(wps) < 2) {
2788 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2789 "Message");
2790 goto out;
2791 }
2792 pos = wpabuf_head(wps);
2793 wsc_len = WPA_GET_BE16(pos);
2794 if (wsc_len > wpabuf_len(wps) - 2) {
2795 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2796 "in rt Wi-Fi Handover Request Message", wsc_len);
2797 goto out;
2798 }
2799 pos += 2;
2800
2801 wpa_hexdump(MSG_DEBUG,
2802 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2803 pos, wsc_len);
2804 if (wsc_len < wpabuf_len(wps) - 2) {
2805 wpa_hexdump(MSG_DEBUG,
2806 "WPS: Ignore extra data after WSC attributes",
2807 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2808 }
2809
2810 wpabuf_set(&msg, pos, wsc_len);
2811 ret = wps_parse_msg(&msg, &attr);
2812 if (ret < 0) {
2813 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2814 "Wi-Fi Handover Request Message");
2815 goto out;
2816 }
2817
2818 if (attr.oob_dev_password == NULL ||
2819 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2820 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2821 "included in Wi-Fi Handover Request Message");
2822 ret = -1;
2823 goto out;
2824 }
2825
2826 if (attr.uuid_e == NULL) {
2827 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2828 "Handover Request Message");
2829 ret = -1;
2830 goto out;
2831 }
2832
2833 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2834
2835 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2836 attr.oob_dev_password, attr.oob_dev_password_len);
2837 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2838 WPS_OOB_PUBKEY_HASH_LEN);
2839 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2840 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2841 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2842 ret = -1;
2843 goto out;
2844 }
2845 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2846 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2847
2848 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2849 attr.oob_dev_password,
2850 DEV_PW_NFC_CONNECTION_HANDOVER,
2851 NULL, 0, 1);
2852
2853out:
2854 wpabuf_free(wps);
2855 return ret;
2856}
2857
3f2c8ba6 2858#endif /* CONFIG_WPS_NFC */
f9f0526b
JM
2859
2860
f9f0526b
JM
2861static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2862{
2863 size_t i;
51bffab1 2864 struct os_reltime now;
f9f0526b
JM
2865
2866 if (wpa_debug_level > MSG_DEBUG)
2867 return;
2868
2869 if (wpa_s->wps_ap == NULL)
2870 return;
2871
51bffab1 2872 os_get_reltime(&now);
f9f0526b
JM
2873
2874 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2875 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2876 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2877
2878 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2879 "tries=%d last_attempt=%d sec ago blacklist=%d",
2880 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2881 ap->last_attempt.sec > 0 ?
2882 (int) now.sec - (int) ap->last_attempt.sec : -1,
2883 e ? e->count : 0);
2884 }
2885}
2886
2887
2888static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2889 const u8 *bssid)
2890{
2891 size_t i;
2892
2893 if (wpa_s->wps_ap == NULL)
2894 return NULL;
2895
2896 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2897 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2898 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2899 return ap;
2900 }
2901
2902 return NULL;
2903}
2904
2905
2906static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2907 struct wpa_scan_res *res)
2908{
2909 struct wpabuf *wps;
2910 enum wps_ap_info_type type;
2911 struct wps_ap_info *ap;
e1dffa3b
AS
2912 int r, pbc_active;
2913 const u8 *uuid;
f9f0526b
JM
2914
2915 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2916 return;
2917
2918 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2919 if (wps == NULL)
2920 return;
2921
2922 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2923 if (r == 2)
2924 type = WPS_AP_SEL_REG_OUR;
2925 else if (r == 1)
2926 type = WPS_AP_SEL_REG;
2927 else
2928 type = WPS_AP_NOT_SEL_REG;
2929
e1dffa3b
AS
2930 uuid = wps_get_uuid_e(wps);
2931 pbc_active = wps_is_selected_pbc_registrar(wps);
f9f0526b
JM
2932
2933 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2934 if (ap) {
2935 if (ap->type != type) {
2936 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2937 " changed type %d -> %d",
2938 MAC2STR(res->bssid), ap->type, type);
2939 ap->type = type;
a462036a
JM
2940 if (type != WPS_AP_NOT_SEL_REG)
2941 wpa_blacklist_del(wpa_s, ap->bssid);
f9f0526b 2942 }
e1dffa3b
AS
2943 ap->pbc_active = pbc_active;
2944 if (uuid)
2945 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2946 goto out;
f9f0526b
JM
2947 }
2948
2949 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2950 sizeof(struct wps_ap_info));
2951 if (ap == NULL)
e1dffa3b 2952 goto out;
f9f0526b
JM
2953
2954 wpa_s->wps_ap = ap;
2955 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2956 wpa_s->num_wps_ap++;
2957
2958 os_memset(ap, 0, sizeof(*ap));
2959 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2960 ap->type = type;
e1dffa3b
AS
2961 ap->pbc_active = pbc_active;
2962 if (uuid)
2963 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
f9f0526b
JM
2964 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2965 MAC2STR(ap->bssid), ap->type);
e1dffa3b
AS
2966
2967out:
2968 wpabuf_free(wps);
f9f0526b
JM
2969}
2970
2971
2972void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2973 struct wpa_scan_results *scan_res)
2974{
2975 size_t i;
2976
2977 for (i = 0; i < scan_res->num; i++)
2978 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2979
2980 wpas_wps_dump_ap_info(wpa_s);
2981}
2982
2983
2984void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2985{
2986 struct wps_ap_info *ap;
7255983b
JM
2987
2988 wpa_s->after_wps = 0;
2989
f9f0526b
JM
2990 if (!wpa_s->wps_ap_iter)
2991 return;
2992 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2993 if (ap == NULL)
2994 return;
2995 ap->tries++;
51bffab1 2996 os_get_reltime(&ap->last_attempt);
f9f0526b 2997}