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