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