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