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