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