2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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"
26 #include "wpa_supplicant_i.h"
29 #include "blacklist.h"
34 #include "p2p_supplicant.h"
35 #include "wps_supplicant.h"
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 */
42 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
43 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
46 static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
48 os_free(wpa_s->wps_ap);
50 wpa_s->num_wps_ap = 0;
51 wpa_s->wps_ap_iter = 0;
55 static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
57 struct wpa_supplicant *wpa_s = eloop_ctx;
58 int use_fast_assoc = timeout_ctx != NULL;
60 wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
61 if (!use_fast_assoc ||
62 wpa_supplicant_fast_associate(wpa_s) != 1)
63 wpa_supplicant_req_scan(wpa_s, 0, 0);
67 static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
69 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
70 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
74 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
77 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
79 #endif /* CONFIG_P2P */
81 if (!wpa_s->wps_success &&
82 wpa_s->current_ssid &&
83 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
84 const u8 *bssid = wpa_s->bssid;
85 if (is_zero_ether_addr(bssid))
86 bssid = wpa_s->pending_bssid;
88 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
89 " did not succeed - continue trying to find "
90 "suitable AP", MAC2STR(bssid));
91 wpa_blacklist_add(wpa_s, bssid);
93 wpa_supplicant_deauthenticate(wpa_s,
94 WLAN_REASON_DEAUTH_LEAVING);
95 wpa_s->reassociate = 1;
96 wpa_supplicant_req_scan(wpa_s,
97 wpa_s->blacklist_cleared ? 5 : 0, 0);
98 wpa_s->blacklist_cleared = 0;
102 wpas_wps_clear_ap_info(wpa_s);
103 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
104 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
105 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
107 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
108 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
109 int disabled = wpa_s->current_ssid->disabled;
110 unsigned int freq = wpa_s->assoc_freq;
112 struct wpa_ssid *ssid = NULL;
113 int use_fast_assoc = 0;
115 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
116 "try to associate with the received credential "
118 wpa_supplicant_deauthenticate(wpa_s,
119 WLAN_REASON_DEAUTH_LEAVING);
121 wpa_printf(MSG_DEBUG, "WPS: Current network is "
122 "disabled - wait for user to enable");
125 wpa_s->after_wps = 5;
126 wpa_s->wps_freq = freq;
127 wpa_s->normal_scans = 0;
128 wpa_s->reassociate = 1;
130 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
131 "without a new scan can be used");
132 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
135 struct wps_parse_attr attr;
137 wps = wpa_bss_get_vendor_ie_multi(bss,
139 if (wps && wps_parse_msg(wps, &attr) == 0 &&
141 *attr.wps_state == WPS_STATE_CONFIGURED)
147 * Complete the next step from an eloop timeout to allow pending
148 * driver events related to the disconnection to be processed
149 * first. This makes it less likely for disconnection event to
150 * cause problems with the following connection.
152 wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
153 wpas_wps_assoc_with_cred_cancel(wpa_s);
154 eloop_register_timeout(0, 10000,
155 wpas_wps_assoc_with_cred, wpa_s,
156 use_fast_assoc ? (void *) 1 :
161 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
162 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
163 "for external credential processing");
164 wpas_clear_wps(wpa_s);
165 wpa_supplicant_deauthenticate(wpa_s,
166 WLAN_REASON_DEAUTH_LEAVING);
174 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
175 struct wpa_ssid *ssid,
176 const struct wps_credential *cred)
178 struct wpa_driver_capa capa;
181 struct wpa_ie_data adv;
182 int wpa2 = 0, ccmp = 0;
185 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
186 * case they are configured for mixed mode operation (WPA+WPA2 and
187 * TKIP+CCMP). Try to use scan results to figure out whether the AP
188 * actually supports stronger security and select that if the client
189 * has support for it, too.
192 if (wpa_drv_get_capa(wpa_s, &capa))
193 return; /* Unknown what driver supports */
195 if (ssid->ssid == NULL)
197 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
199 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
200 "table - use credential as-is");
204 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
206 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
207 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
209 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
212 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
213 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
214 adv.pairwise_cipher & WPA_CIPHER_CCMP)
218 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
219 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
221 * TODO: This could be the initial AP configuration and the
222 * Beacon contents could change shortly. Should request a new
223 * scan and delay addition of the network until the updated
224 * scan results are available.
226 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
227 "support - use credential as-is");
231 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
232 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
233 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
234 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
235 "based on scan results");
236 if (wpa_s->conf->ap_scan == 1)
237 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
239 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
242 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
243 (ssid->proto & WPA_PROTO_WPA) &&
244 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
245 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
246 "based on scan results");
247 if (wpa_s->conf->ap_scan == 1)
248 ssid->proto |= WPA_PROTO_RSN;
250 ssid->proto = WPA_PROTO_RSN;
255 static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
256 struct wpa_ssid *new_ssid)
258 struct wpa_ssid *ssid, *next;
260 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
261 ssid = next, next = ssid ? ssid->next : NULL) {
263 * new_ssid has already been added to the list in
264 * wpas_wps_add_network(), so skip it.
266 if (ssid == new_ssid)
269 if (ssid->bssid_set || new_ssid->bssid_set) {
270 if (ssid->bssid_set != new_ssid->bssid_set)
272 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
278 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
281 if (ssid->ssid && new_ssid->ssid) {
282 if (os_memcmp(ssid->ssid, new_ssid->ssid,
283 ssid->ssid_len) != 0)
285 } else if (ssid->ssid || new_ssid->ssid)
288 /* compare security parameters */
289 if (ssid->auth_alg != new_ssid->auth_alg ||
290 ssid->key_mgmt != new_ssid->key_mgmt ||
291 ssid->proto != new_ssid->proto ||
292 ssid->pairwise_cipher != new_ssid->pairwise_cipher ||
293 ssid->group_cipher != new_ssid->group_cipher)
296 /* Remove the duplicated older network entry. */
297 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
298 wpas_notify_network_removed(wpa_s, ssid);
299 wpa_config_remove_network(wpa_s->conf, ssid->id);
304 static int wpa_supplicant_wps_cred(void *ctx,
305 const struct wps_credential *cred)
307 struct wpa_supplicant *wpa_s = ctx;
308 struct wpa_ssid *ssid = wpa_s->current_ssid;
310 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
312 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
314 if ((wpa_s->conf->wps_cred_processing == 1 ||
315 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
316 size_t blen = cred->cred_attr_len * 2 + 1;
317 char *buf = os_malloc(blen);
319 wpa_snprintf_hex(buf, blen,
320 cred->cred_attr, cred->cred_attr_len);
321 wpa_msg(wpa_s, MSG_INFO, "%s%s",
322 WPS_EVENT_CRED_RECEIVED, buf);
326 wpas_notify_wps_credential(wpa_s, cred);
328 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
330 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
331 cred->cred_attr, cred->cred_attr_len);
333 if (wpa_s->conf->wps_cred_processing == 1)
336 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
337 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
339 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
340 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
341 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
342 cred->key, cred->key_len);
343 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
344 MAC2STR(cred->mac_addr));
346 auth_type = cred->auth_type;
347 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
348 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
349 "auth_type into WPA2PSK");
350 auth_type = WPS_AUTH_WPA2PSK;
353 if (auth_type != WPS_AUTH_OPEN &&
354 auth_type != WPS_AUTH_WPAPSK &&
355 auth_type != WPS_AUTH_WPA2PSK) {
356 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
357 "unsupported authentication type 0x%x",
362 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
363 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
364 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
365 "invalid Network Key length %lu",
366 (unsigned long) cred->key_len);
371 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
372 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
373 "on the received credential");
374 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
375 if (ssid->eap.identity &&
376 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
377 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
378 WSC_ID_REGISTRAR_LEN) == 0)
380 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
381 os_free(ssid->eap.identity);
382 ssid->eap.identity = NULL;
383 ssid->eap.identity_len = 0;
384 os_free(ssid->eap.phase1);
385 ssid->eap.phase1 = NULL;
386 os_free(ssid->eap.eap_methods);
387 ssid->eap.eap_methods = NULL;
388 if (!ssid->p2p_group) {
392 ssid->disabled_until.sec = 0;
393 ssid->disabled_until.usec = 0;
394 ssid->auth_failures = 0;
396 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
397 "received credential");
398 ssid = wpa_config_add_network(wpa_s->conf);
401 if (wpa_s->current_ssid) {
403 * Should the GO issue multiple credentials for some
404 * reason, each credential should be marked as a
405 * temporary P2P group similarly to the one that gets
406 * marked as such based on the pre-configured values
407 * used for the WPS network block.
409 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
410 ssid->temporary = wpa_s->current_ssid->temporary;
412 wpas_notify_network_added(wpa_s, ssid);
415 wpa_config_set_network_defaults(ssid);
418 ssid->ssid = os_malloc(cred->ssid_len);
420 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
421 ssid->ssid_len = cred->ssid_len;
424 switch (cred->encr_type) {
428 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
431 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
437 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
438 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
440 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
442 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
443 "id=%d - Credentials for an open "
444 "network disabled by default - use "
445 "'select_network %d' to enable",
449 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
451 case WPS_AUTH_WPAPSK:
452 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
453 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
454 ssid->proto = WPA_PROTO_WPA;
456 case WPS_AUTH_WPA2PSK:
457 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
458 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
459 ssid->proto = WPA_PROTO_RSN;
463 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
464 if (cred->key_len == 2 * PMK_LEN) {
465 if (hexstr2bin((const char *) cred->key, ssid->psk,
467 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
472 ssid->export_keys = 1;
473 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
474 os_free(ssid->passphrase);
475 ssid->passphrase = os_malloc(cred->key_len + 1);
476 if (ssid->passphrase == NULL)
478 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
479 ssid->passphrase[cred->key_len] = '\0';
480 wpa_config_update_psk(ssid);
481 ssid->export_keys = 1;
483 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
485 (unsigned long) cred->key_len);
490 wpas_wps_security_workaround(wpa_s, ssid, cred);
492 wpas_wps_remove_dup_network(wpa_s, ssid);
494 #ifndef CONFIG_NO_CONFIG_WRITE
495 if (wpa_s->conf->update_config &&
496 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
497 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
500 #endif /* CONFIG_NO_CONFIG_WRITE */
503 * Optimize the post-WPS scan based on the channel used during
504 * the provisioning in case EAP-Failure is not received.
506 wpa_s->after_wps = 5;
507 wpa_s->wps_freq = wpa_s->assoc_freq;
513 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
514 struct wps_event_m2d *m2d)
516 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
517 "dev_password_id=%d config_error=%d",
518 m2d->dev_password_id, m2d->config_error);
519 wpas_notify_wps_event_m2d(wpa_s, m2d);
521 if (wpa_s->parent && wpa_s->parent != wpa_s) {
522 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
523 "dev_password_id=%d config_error=%d",
524 m2d->dev_password_id, m2d->config_error);
526 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
528 * Notify P2P from eloop timeout to avoid issues with the
529 * interface getting removed while processing a message.
531 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
534 #endif /* CONFIG_P2P */
538 static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
540 struct wpa_supplicant *wpa_s = eloop_ctx;
541 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
542 wpas_clear_wps(wpa_s);
546 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
547 struct wps_event_fail *fail)
549 if (fail->error_indication > 0 &&
550 fail->error_indication < NUM_WPS_EI_VALUES) {
551 wpa_msg(wpa_s, MSG_INFO,
552 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
553 fail->msg, fail->config_error, fail->error_indication,
554 wps_ei_str(fail->error_indication));
555 if (wpa_s->parent && wpa_s->parent != wpa_s)
556 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
557 "msg=%d config_error=%d reason=%d (%s)",
558 fail->msg, fail->config_error,
559 fail->error_indication,
560 wps_ei_str(fail->error_indication));
562 wpa_msg(wpa_s, MSG_INFO,
563 WPS_EVENT_FAIL "msg=%d config_error=%d",
564 fail->msg, fail->config_error);
565 if (wpa_s->parent && wpa_s->parent != wpa_s)
566 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
567 "msg=%d config_error=%d",
568 fail->msg, fail->config_error);
572 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
574 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
575 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
576 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
578 wpas_notify_wps_event_fail(wpa_s, fail);
580 wpas_p2p_wps_failed(wpa_s, fail);
581 #endif /* CONFIG_P2P */
585 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
587 static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
589 struct wpa_ssid *ssid;
592 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
594 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
595 if (ssid->disabled_for_connect && ssid->disabled) {
596 ssid->disabled_for_connect = 0;
598 wpas_notify_network_enabled_changed(wpa_s, ssid);
604 #ifndef CONFIG_NO_CONFIG_WRITE
605 if (wpa_s->conf->update_config &&
606 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
607 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
610 #endif /* CONFIG_NO_CONFIG_WRITE */
615 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
617 struct wpa_supplicant *wpa_s = eloop_ctx;
618 /* Enable the networks disabled during wpas_wps_reassoc */
619 wpas_wps_reenable_networks(wpa_s);
623 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
625 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
626 wpa_s->wps_success = 1;
627 wpas_notify_wps_event_success(wpa_s);
628 if (wpa_s->current_ssid)
629 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
630 wpa_s->extra_blacklist_count = 0;
633 * Enable the networks disabled during wpas_wps_reassoc after 10
634 * seconds. The 10 seconds timer is to allow the data connection to be
635 * formed before allowing other networks to be selected.
637 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
641 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
642 #endif /* CONFIG_P2P */
646 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
647 struct wps_event_er_ap *ap)
650 char dev_type[WPS_DEV_TYPE_BUFSIZE];
652 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
653 if (ap->pri_dev_type)
654 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
659 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
660 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
661 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
662 ap->friendly_name ? ap->friendly_name : "",
663 ap->manufacturer ? ap->manufacturer : "",
664 ap->model_description ? ap->model_description : "",
665 ap->model_name ? ap->model_name : "",
666 ap->manufacturer_url ? ap->manufacturer_url : "",
667 ap->model_url ? ap->model_url : "");
671 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
672 struct wps_event_er_ap *ap)
675 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
676 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
680 static void wpa_supplicant_wps_event_er_enrollee_add(
681 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
684 char dev_type[WPS_DEV_TYPE_BUFSIZE];
686 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
687 if (enrollee->pri_dev_type)
688 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
693 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
694 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
696 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
697 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
698 enrollee->dev_name ? enrollee->dev_name : "",
699 enrollee->manufacturer ? enrollee->manufacturer : "",
700 enrollee->model_name ? enrollee->model_name : "",
701 enrollee->model_number ? enrollee->model_number : "",
702 enrollee->serial_number ? enrollee->serial_number : "");
706 static void wpa_supplicant_wps_event_er_enrollee_remove(
707 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
710 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
711 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
712 uuid_str, MAC2STR(enrollee->mac_addr));
716 static void wpa_supplicant_wps_event_er_ap_settings(
717 struct wpa_supplicant *wpa_s,
718 struct wps_event_er_ap_settings *ap_settings)
722 const struct wps_credential *cred = ap_settings->cred;
725 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
726 if (cred->key_len >= 8 && cred->key_len <= 64) {
727 os_memcpy(key_str, cred->key, cred->key_len);
728 key_str[cred->key_len] = '\0';
732 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
733 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
734 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
735 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
737 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
738 cred->auth_type, cred->encr_type, key_str);
742 static void wpa_supplicant_wps_event_er_set_sel_reg(
743 struct wpa_supplicant *wpa_s,
744 struct wps_event_er_set_selected_registrar *ev)
748 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
750 case WPS_ER_SET_SEL_REG_START:
751 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
752 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
753 "sel_reg_config_methods=0x%x",
754 uuid_str, ev->sel_reg, ev->dev_passwd_id,
755 ev->sel_reg_config_methods);
757 case WPS_ER_SET_SEL_REG_DONE:
758 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
759 "uuid=%s state=DONE", uuid_str);
761 case WPS_ER_SET_SEL_REG_FAILED:
762 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
763 "uuid=%s state=FAILED", uuid_str);
769 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
770 union wps_event_data *data)
772 struct wpa_supplicant *wpa_s = ctx;
775 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
778 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
781 wpa_supplicant_wps_event_success(wpa_s);
783 case WPS_EV_PWD_AUTH_FAIL:
785 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
786 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
787 #endif /* CONFIG_AP */
789 case WPS_EV_PBC_OVERLAP:
791 case WPS_EV_PBC_TIMEOUT:
793 case WPS_EV_PBC_ACTIVE:
794 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
796 case WPS_EV_PBC_DISABLE:
797 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
799 case WPS_EV_ER_AP_ADD:
800 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
802 case WPS_EV_ER_AP_REMOVE:
803 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
805 case WPS_EV_ER_ENROLLEE_ADD:
806 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
809 case WPS_EV_ER_ENROLLEE_REMOVE:
810 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
813 case WPS_EV_ER_AP_SETTINGS:
814 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
817 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
818 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
821 case WPS_EV_AP_PIN_SUCCESS:
827 static int wpa_supplicant_wps_rf_band(void *ctx)
829 struct wpa_supplicant *wpa_s = ctx;
831 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
834 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
838 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
840 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
841 eap_is_wps_pin_enrollee(&ssid->eap))
842 return WPS_REQ_ENROLLEE;
844 return WPS_REQ_REGISTRAR;
848 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
851 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
853 wpa_s->after_wps = 0;
854 wpa_s->known_wps_freq = 0;
856 prev_current = wpa_s->current_ssid;
858 /* Enable the networks disabled during wpas_wps_reassoc */
859 wpas_wps_reenable_networks(wpa_s);
861 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
862 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
864 /* Remove any existing WPS network from configuration */
865 ssid = wpa_s->conf->ssid;
867 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
868 if (ssid == wpa_s->current_ssid) {
869 wpa_supplicant_deauthenticate(
870 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
878 if (prev_current == remove_ssid) {
879 wpa_sm_set_config(wpa_s->wpa, NULL);
880 eapol_sm_notify_config(wpa_s->eapol, NULL,
883 wpas_notify_network_removed(wpa_s, remove_ssid);
884 wpa_config_remove_network(wpa_s->conf, id);
888 wpas_wps_clear_ap_info(wpa_s);
892 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
894 struct wpa_supplicant *wpa_s = eloop_ctx;
895 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
897 wpas_clear_wps(wpa_s);
901 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
902 int registrar, const u8 *dev_addr,
905 struct wpa_ssid *ssid;
907 ssid = wpa_config_add_network(wpa_s->conf);
910 wpas_notify_network_added(wpa_s, ssid);
911 wpa_config_set_network_defaults(ssid);
913 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
914 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
915 wpa_config_set(ssid, "identity", registrar ?
916 "\"" WSC_ID_REGISTRAR "\"" :
917 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
918 wpas_notify_network_removed(wpa_s, ssid);
919 wpa_config_remove_network(wpa_s->conf, ssid->id);
925 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
926 #endif /* CONFIG_P2P */
932 #endif /* CONFIG_P2P */
934 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
938 * Note: With P2P, the SSID may change at the time the WPS
939 * provisioning is started, so better not filter the AP based
940 * on the current SSID in the scan results.
943 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
944 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
948 ssid->ssid = os_malloc(bss->ssid_len);
949 if (ssid->ssid == NULL)
951 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
952 ssid->ssid_len = bss->ssid_len;
953 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
955 ssid->ssid, ssid->ssid_len);
960 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
961 "for the AP; use wildcard");
966 #endif /* CONFIG_P2P */
973 static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
974 struct wpa_ssid *selected)
976 struct wpa_ssid *ssid;
978 if (wpa_s->current_ssid)
979 wpa_supplicant_deauthenticate(
980 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
982 /* Mark all other networks disabled and trigger reassociation */
983 ssid = wpa_s->conf->ssid;
985 int was_disabled = ssid->disabled;
986 ssid->disabled_for_connect = 0;
988 * In case the network object corresponds to a persistent group
989 * then do not send out network disabled signal. In addition,
990 * do not change disabled status of persistent network objects
991 * from 2 to 1 should we connect to another network.
993 if (was_disabled != 2) {
994 ssid->disabled = ssid != selected;
995 if (was_disabled != ssid->disabled) {
997 ssid->disabled_for_connect = 1;
998 wpas_notify_network_enabled_changed(wpa_s,
1007 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
1008 struct wpa_ssid *selected, const u8 *bssid,
1011 struct wpa_bss *bss;
1013 wpa_s->after_wps = 0;
1014 wpa_s->known_wps_freq = 0;
1016 wpa_s->after_wps = 5;
1017 wpa_s->wps_freq = freq;
1019 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1020 if (bss && bss->freq > 0) {
1021 wpa_s->known_wps_freq = 1;
1022 wpa_s->wps_freq = bss->freq;
1026 wpas_wps_temp_disable(wpa_s, selected);
1028 wpa_s->disconnected = 0;
1029 wpa_s->reassociate = 1;
1030 wpa_s->scan_runs = 0;
1031 wpa_s->normal_scans = 0;
1032 wpa_s->wps_success = 0;
1033 wpa_s->blacklist_cleared = 0;
1035 wpa_supplicant_cancel_sched_scan(wpa_s);
1036 wpa_supplicant_req_scan(wpa_s, 0, 0);
1040 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1043 struct wpa_ssid *ssid;
1044 wpas_clear_wps(wpa_s);
1045 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
1048 ssid->temporary = 1;
1049 ssid->p2p_group = p2p_group;
1051 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1052 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1054 ssid->ssid_len = wpa_s->go_params->ssid_len;
1055 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1057 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1058 "SSID", ssid->ssid, ssid->ssid_len);
1061 #endif /* CONFIG_P2P */
1062 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1064 if (wpa_s->wps_fragment_size)
1065 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1066 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1068 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
1073 static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1074 const u8 *dev_addr, const u8 *bssid,
1075 const char *pin, int p2p_group, u16 dev_pw_id,
1076 const u8 *peer_pubkey_hash,
1077 const u8 *ssid_val, size_t ssid_len, int freq)
1079 struct wpa_ssid *ssid;
1080 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
1081 unsigned int rpin = 0;
1082 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
1084 wpas_clear_wps(wpa_s);
1085 if (bssid && is_zero_ether_addr(bssid))
1087 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1089 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
1092 ssid->temporary = 1;
1093 ssid->p2p_group = p2p_group;
1095 ssid->ssid = os_malloc(ssid_len);
1097 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1098 ssid->ssid_len = ssid_len;
1101 if (peer_pubkey_hash) {
1102 os_memcpy(hash, " pkhash=", 8);
1103 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1105 WPS_OOB_PUBKEY_HASH_LEN);
1110 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1111 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1113 ssid->ssid_len = wpa_s->go_params->ssid_len;
1114 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1116 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1117 "SSID", ssid->ssid, ssid->ssid_len);
1120 #endif /* CONFIG_P2P */
1122 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1123 pin, dev_pw_id, hash);
1124 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1125 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1128 rpin = wps_generate_pin();
1129 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1130 rpin, dev_pw_id, hash);
1132 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1133 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
1136 if (wpa_s->wps_fragment_size)
1137 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1138 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1140 wpa_s->wps_ap_iter = 1;
1141 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
1146 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1147 const char *pin, int p2p_group, u16 dev_pw_id)
1149 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1150 dev_pw_id, NULL, NULL, 0, 0);
1154 /* Cancel the wps pbc/pin requests */
1155 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1158 if (wpa_s->ap_iface) {
1159 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1160 return wpa_supplicant_ap_wps_cancel(wpa_s);
1162 #endif /* CONFIG_AP */
1164 if (wpa_s->wpa_state == WPA_SCANNING ||
1165 wpa_s->wpa_state == WPA_DISCONNECTED) {
1166 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1167 wpa_supplicant_cancel_scan(wpa_s);
1168 wpas_clear_wps(wpa_s);
1169 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1170 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1172 wpa_supplicant_deauthenticate(wpa_s,
1173 WLAN_REASON_DEAUTH_LEAVING);
1174 wpas_clear_wps(wpa_s);
1176 wpas_wps_reenable_networks(wpa_s);
1177 wpas_wps_clear_ap_info(wpa_s);
1178 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1180 wpas_clear_wps(wpa_s);
1183 wpa_s->after_wps = 0;
1189 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1190 const char *pin, struct wps_new_ap_settings *settings)
1192 struct wpa_ssid *ssid;
1199 wpas_clear_wps(wpa_s);
1200 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
1203 ssid->temporary = 1;
1205 end = pos + sizeof(val);
1206 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1207 if (res < 0 || res >= end - pos)
1211 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1212 "new_encr=%s new_key=%s",
1213 settings->ssid_hex, settings->auth,
1214 settings->encr, settings->key_hex);
1215 if (res < 0 || res >= end - pos)
1219 res = os_snprintf(pos, end - pos, "\"");
1220 if (res < 0 || res >= end - pos)
1222 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1224 if (wpa_s->wps_fragment_size)
1225 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1226 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1228 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
1233 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1234 const u8 *p2p_dev_addr, const u8 *psk,
1237 if (is_zero_ether_addr(p2p_dev_addr)) {
1238 wpa_printf(MSG_DEBUG,
1239 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1242 wpa_printf(MSG_DEBUG,
1243 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1244 " P2P Device Addr " MACSTR,
1245 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1247 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1255 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1256 const struct wps_device_data *dev)
1258 char uuid[40], txt[400];
1260 char devtype[WPS_DEV_TYPE_BUFSIZE];
1261 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1263 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1264 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1265 " [%s|%s|%s|%s|%s|%s]",
1266 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1267 dev->manufacturer, dev->model_name,
1268 dev->model_number, dev->serial_number,
1269 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1271 if (len > 0 && len < (int) sizeof(txt))
1272 wpa_printf(MSG_INFO, "%s", txt);
1276 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1277 u16 sel_reg_config_methods)
1279 #ifdef CONFIG_WPS_ER
1280 struct wpa_supplicant *wpa_s = ctx;
1282 if (wpa_s->wps_er == NULL)
1284 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1285 "dev_password_id=%u sel_reg_config_methods=0x%x",
1286 sel_reg, dev_passwd_id, sel_reg_config_methods);
1287 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1288 sel_reg_config_methods);
1289 #endif /* CONFIG_WPS_ER */
1293 static u16 wps_fix_config_methods(u16 config_methods)
1295 if ((config_methods &
1296 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1297 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1298 wpa_printf(MSG_INFO, "WPS: Converting display to "
1299 "virtual_display for WPS 2.0 compliance");
1300 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1302 if ((config_methods &
1303 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1304 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1305 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1306 "virtual_push_button for WPS 2.0 compliance");
1307 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1310 return config_methods;
1314 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1315 struct wps_context *wps)
1320 if (is_nil_uuid(wpa_s->conf->uuid)) {
1321 struct wpa_supplicant *first;
1322 first = wpa_s->global->ifaces;
1323 while (first && first->next)
1324 first = first->next;
1325 if (first && first != wpa_s) {
1326 if (wps != wpa_s->global->ifaces->wps)
1327 os_memcpy(wps->uuid,
1328 wpa_s->global->ifaces->wps->uuid,
1330 src = "from the first interface";
1332 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1333 src = "based on MAC address";
1336 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1337 src = "based on configuration";
1340 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1341 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
1345 static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1346 struct wps_context *wps)
1348 wpabuf_free(wps->dev.vendor_ext_m1);
1349 wps->dev.vendor_ext_m1 = NULL;
1351 if (wpa_s->conf->wps_vendor_ext_m1) {
1352 wps->dev.vendor_ext_m1 =
1353 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1354 if (!wps->dev.vendor_ext_m1) {
1355 wpa_printf(MSG_ERROR, "WPS: Cannot "
1356 "allocate memory for vendor_ext_m1");
1362 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1364 struct wps_context *wps;
1365 struct wps_registrar_config rcfg;
1366 struct hostapd_hw_modes *modes;
1369 wps = os_zalloc(sizeof(*wps));
1373 wps->cred_cb = wpa_supplicant_wps_cred;
1374 wps->event_cb = wpa_supplicant_wps_event;
1375 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
1376 wps->cb_ctx = wpa_s;
1378 wps->dev.device_name = wpa_s->conf->device_name;
1379 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1380 wps->dev.model_name = wpa_s->conf->model_name;
1381 wps->dev.model_number = wpa_s->conf->model_number;
1382 wps->dev.serial_number = wpa_s->conf->serial_number;
1383 wps->config_methods =
1384 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1385 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1386 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1387 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1388 "methods are not allowed at the same time");
1392 wps->config_methods = wps_fix_config_methods(wps->config_methods);
1393 wps->dev.config_methods = wps->config_methods;
1394 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1397 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1398 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1399 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1401 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1403 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1404 modes = wpa_s->hw.modes;
1406 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1407 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1408 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1409 wps->dev.rf_bands |= WPS_RF_24GHZ;
1410 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1411 wps->dev.rf_bands |= WPS_RF_50GHZ;
1414 if (wps->dev.rf_bands == 0) {
1416 * Default to claiming support for both bands if the driver
1417 * does not provide support for fetching supported bands.
1419 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1421 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1422 wpas_wps_set_uuid(wpa_s, wps);
1424 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1425 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1427 os_memset(&rcfg, 0, sizeof(rcfg));
1428 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1429 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1430 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1431 rcfg.cb_ctx = wpa_s;
1433 wps->registrar = wps_registrar_init(wps, &rcfg);
1434 if (wps->registrar == NULL) {
1435 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1446 #ifdef CONFIG_WPS_ER
1447 static void wpas_wps_nfc_clear(struct wps_context *wps)
1449 wps->ap_nfc_dev_pw_id = 0;
1450 wpabuf_free(wps->ap_nfc_dh_pubkey);
1451 wps->ap_nfc_dh_pubkey = NULL;
1452 wpabuf_free(wps->ap_nfc_dh_privkey);
1453 wps->ap_nfc_dh_privkey = NULL;
1454 wpabuf_free(wps->ap_nfc_dev_pw);
1455 wps->ap_nfc_dev_pw = NULL;
1457 #endif /* CONFIG_WPS_ER */
1460 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1462 wpas_wps_assoc_with_cred_cancel(wpa_s);
1463 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1464 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
1465 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
1466 wpas_wps_clear_ap_info(wpa_s);
1469 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1470 #endif /* CONFIG_P2P */
1472 if (wpa_s->wps == NULL)
1475 #ifdef CONFIG_WPS_ER
1476 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1477 wpa_s->wps_er = NULL;
1478 wpas_wps_nfc_clear(wpa_s->wps);
1479 #endif /* CONFIG_WPS_ER */
1481 wps_registrar_deinit(wpa_s->wps->registrar);
1482 wpabuf_free(wpa_s->wps->dh_pubkey);
1483 wpabuf_free(wpa_s->wps->dh_privkey);
1484 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
1485 os_free(wpa_s->wps->network_key);
1486 os_free(wpa_s->wps);
1491 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1492 struct wpa_ssid *ssid, struct wpa_bss *bss)
1494 struct wpabuf *wps_ie;
1496 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1499 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1500 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1502 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1506 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1507 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1508 "without active PBC Registrar");
1509 wpabuf_free(wps_ie);
1513 /* TODO: overlap detection */
1514 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1516 wpabuf_free(wps_ie);
1520 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1522 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1527 * Start with WPS APs that advertise our address as an
1528 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1529 * allow any WPS AP after couple of scans since some APs do not
1530 * set Selected Registrar attribute properly when using
1531 * external Registrar.
1533 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1534 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1535 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1536 "without active PIN Registrar");
1537 wpabuf_free(wps_ie);
1540 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1542 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1543 "(Authorized MAC or Active PIN)");
1545 wpabuf_free(wps_ie);
1550 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1551 wpabuf_free(wps_ie);
1559 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1560 struct wpa_ssid *ssid,
1561 struct wpa_bss *bss)
1563 struct wpabuf *wps_ie = NULL;
1566 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1567 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1568 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1569 /* allow wildcard SSID for WPS PBC */
1572 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1573 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1575 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1576 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1577 /* allow wildcard SSID for WPS PIN */
1582 if (!ret && ssid->bssid_set &&
1583 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1584 /* allow wildcard SSID due to hardcoded BSSID match */
1588 #ifdef CONFIG_WPS_STRICT
1590 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1593 if (bss->beacon_ie_len) {
1594 struct wpabuf *bcn_wps;
1595 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
1596 bss, WPS_IE_VENDOR_TYPE);
1597 if (bcn_wps == NULL) {
1598 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1599 "missing from AP Beacon");
1602 if (wps_validate_beacon(wps_ie) < 0)
1604 wpabuf_free(bcn_wps);
1608 #endif /* CONFIG_WPS_STRICT */
1610 wpabuf_free(wps_ie);
1616 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1617 struct wpa_bss *selected, struct wpa_ssid *ssid)
1619 const u8 *sel_uuid, *uuid;
1620 struct wpabuf *wps_ie;
1622 struct wpa_bss *bss;
1624 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1627 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1628 "present in scan results; selected BSSID " MACSTR,
1629 MAC2STR(selected->bssid));
1631 /* Make sure that only one AP is in active PBC mode */
1632 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1634 sel_uuid = wps_get_uuid_e(wps_ie);
1635 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1636 sel_uuid, UUID_LEN);
1638 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1643 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1645 if (bss == selected)
1647 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1650 if (!wps_is_selected_pbc_registrar(ie)) {
1654 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1655 MACSTR, MAC2STR(bss->bssid));
1656 uuid = wps_get_uuid_e(ie);
1657 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1659 if (sel_uuid == NULL || uuid == NULL ||
1660 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1661 ret = 1; /* PBC overlap */
1662 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1663 MACSTR " and " MACSTR,
1664 MAC2STR(selected->bssid),
1665 MAC2STR(bss->bssid));
1670 /* TODO: verify that this is reasonable dual-band situation */
1675 wpabuf_free(wps_ie);
1681 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1683 struct wpa_bss *bss;
1684 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1686 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1689 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1691 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1694 if (wps_is_selected_pbc_registrar(ie))
1696 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1698 else if (wps_is_selected_pin_registrar(ie))
1706 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1708 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1710 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1712 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1716 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1718 struct wpa_ssid *ssid;
1720 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1721 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1729 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1732 struct wpabuf *wps_ie;
1735 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1739 ret = wps_attr_text(wps_ie, buf, end);
1740 wpabuf_free(wps_ie);
1745 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1747 #ifdef CONFIG_WPS_ER
1748 if (wpa_s->wps_er) {
1749 wps_er_refresh(wpa_s->wps_er);
1752 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1753 if (wpa_s->wps_er == NULL)
1756 #else /* CONFIG_WPS_ER */
1758 #endif /* CONFIG_WPS_ER */
1762 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1764 #ifdef CONFIG_WPS_ER
1765 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1766 wpa_s->wps_er = NULL;
1767 #endif /* CONFIG_WPS_ER */
1772 #ifdef CONFIG_WPS_ER
1773 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1774 const char *uuid, const char *pin)
1777 const u8 *use_uuid = NULL;
1778 u8 addr_buf[ETH_ALEN];
1780 if (os_strcmp(uuid, "any") == 0) {
1781 } else if (uuid_str2bin(uuid, u) == 0) {
1783 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1784 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1785 if (use_uuid == NULL)
1789 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1791 (const u8 *) pin, os_strlen(pin), 300);
1795 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1797 u8 u[UUID_LEN], *use_uuid = NULL;
1798 u8 addr[ETH_ALEN], *use_addr = NULL;
1800 if (uuid_str2bin(uuid, u) == 0)
1802 else if (hwaddr_aton(uuid, addr) == 0)
1806 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
1810 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1813 u8 u[UUID_LEN], *use_uuid = NULL;
1814 u8 addr[ETH_ALEN], *use_addr = NULL;
1816 if (uuid_str2bin(uuid, u) == 0)
1818 else if (hwaddr_aton(uuid, addr) == 0)
1823 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
1828 static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1829 struct wps_credential *cred)
1831 os_memset(cred, 0, sizeof(*cred));
1832 if (ssid->ssid_len > 32)
1834 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1835 cred->ssid_len = ssid->ssid_len;
1836 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1837 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1838 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1839 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1840 cred->encr_type = WPS_ENCR_AES;
1842 cred->encr_type = WPS_ENCR_TKIP;
1843 if (ssid->passphrase) {
1844 cred->key_len = os_strlen(ssid->passphrase);
1845 if (cred->key_len >= 64)
1847 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1848 } else if (ssid->psk_set) {
1850 os_memcpy(cred->key, ssid->psk, 32);
1854 cred->auth_type = WPS_AUTH_OPEN;
1855 cred->encr_type = WPS_ENCR_NONE;
1862 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1865 u8 u[UUID_LEN], *use_uuid = NULL;
1866 u8 addr[ETH_ALEN], *use_addr = NULL;
1867 struct wpa_ssid *ssid;
1868 struct wps_credential cred;
1870 if (uuid_str2bin(uuid, u) == 0)
1872 else if (hwaddr_aton(uuid, addr) == 0)
1876 ssid = wpa_config_get_network(wpa_s->conf, id);
1877 if (ssid == NULL || ssid->ssid == NULL)
1880 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
1882 return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
1886 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1887 const char *pin, struct wps_new_ap_settings *settings)
1889 u8 u[UUID_LEN], *use_uuid = NULL;
1890 u8 addr[ETH_ALEN], *use_addr = NULL;
1891 struct wps_credential cred;
1894 if (uuid_str2bin(uuid, u) == 0)
1896 else if (hwaddr_aton(uuid, addr) == 0)
1900 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1901 settings->encr == NULL || settings->key_hex == NULL)
1904 os_memset(&cred, 0, sizeof(cred));
1905 len = os_strlen(settings->ssid_hex);
1906 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1907 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1909 cred.ssid_len = len / 2;
1911 len = os_strlen(settings->key_hex);
1912 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1913 hexstr2bin(settings->key_hex, cred.key, len / 2))
1915 cred.key_len = len / 2;
1917 if (os_strcmp(settings->auth, "OPEN") == 0)
1918 cred.auth_type = WPS_AUTH_OPEN;
1919 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1920 cred.auth_type = WPS_AUTH_WPAPSK;
1921 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1922 cred.auth_type = WPS_AUTH_WPA2PSK;
1926 if (os_strcmp(settings->encr, "NONE") == 0)
1927 cred.encr_type = WPS_ENCR_NONE;
1928 #ifdef CONFIG_TESTING_OPTIONS
1929 else if (os_strcmp(settings->encr, "WEP") == 0)
1930 cred.encr_type = WPS_ENCR_WEP;
1931 #endif /* CONFIG_TESTING_OPTIONS */
1932 else if (os_strcmp(settings->encr, "TKIP") == 0)
1933 cred.encr_type = WPS_ENCR_TKIP;
1934 else if (os_strcmp(settings->encr, "CCMP") == 0)
1935 cred.encr_type = WPS_ENCR_AES;
1939 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1940 (const u8 *) pin, os_strlen(pin), &cred);
1944 #ifdef CONFIG_WPS_NFC
1945 struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1946 int ndef, const char *uuid)
1949 u8 u[UUID_LEN], *use_uuid = NULL;
1950 u8 addr[ETH_ALEN], *use_addr = NULL;
1955 if (uuid_str2bin(uuid, u) == 0)
1957 else if (hwaddr_aton(uuid, addr) == 0)
1962 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
1965 tmp = ndef_build_wifi(ret);
1974 #endif /* CONFIG_WPS_NFC */
1977 static int callbacks_pending = 0;
1979 static void wpas_wps_terminate_cb(void *ctx)
1981 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1982 if (--callbacks_pending <= 0)
1985 #endif /* CONFIG_WPS_ER */
1988 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1990 #ifdef CONFIG_WPS_ER
1991 if (wpa_s->wps_er) {
1992 callbacks_pending++;
1993 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1994 wpa_s->wps_er = NULL;
1997 #endif /* CONFIG_WPS_ER */
2002 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2004 struct wps_context *wps = wpa_s->wps;
2009 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2010 wps->config_methods = wps_config_methods_str2bin(
2011 wpa_s->conf->config_methods);
2012 if ((wps->config_methods &
2013 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2014 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2015 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2016 "config methods are not allowed at the "
2018 wps->config_methods &= ~WPS_CONFIG_LABEL;
2021 wps->config_methods = wps_fix_config_methods(wps->config_methods);
2022 wps->dev.config_methods = wps->config_methods;
2024 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2025 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2028 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2029 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2030 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2031 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2034 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2035 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2037 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2038 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2040 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2041 wpas_wps_set_uuid(wpa_s, wps);
2043 if (wpa_s->conf->changed_parameters &
2044 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2045 /* Update pointers to make sure they refer current values */
2046 wps->dev.device_name = wpa_s->conf->device_name;
2047 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2048 wps->dev.model_name = wpa_s->conf->model_name;
2049 wps->dev.model_number = wpa_s->conf->model_number;
2050 wps->dev.serial_number = wpa_s->conf->serial_number;
2055 #ifdef CONFIG_WPS_NFC
2057 #ifdef CONFIG_WPS_ER
2058 static struct wpabuf *
2059 wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2060 struct wpa_ssid *ssid)
2063 struct wps_credential cred;
2065 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2068 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2072 tmp = ndef_build_wifi(ret);
2081 #endif /* CONFIG_WPS_ER */
2084 struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2085 int ndef, const char *id_str)
2087 #ifdef CONFIG_WPS_ER
2091 struct wpa_ssid *ssid;
2093 id = strtol(id_str, &end, 10);
2097 ssid = wpa_config_get_network(wpa_s->conf, id);
2100 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2102 #endif /* CONFIG_WPS_ER */
2104 if (wpa_s->ap_iface)
2105 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2106 #endif /* CONFIG_AP */
2111 struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2113 if (wpa_s->conf->wps_nfc_pw_from_config) {
2114 return wps_nfc_token_build(ndef,
2115 wpa_s->conf->wps_nfc_dev_pw_id,
2116 wpa_s->conf->wps_nfc_dh_pubkey,
2117 wpa_s->conf->wps_nfc_dev_pw);
2120 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2121 &wpa_s->conf->wps_nfc_dh_pubkey,
2122 &wpa_s->conf->wps_nfc_dh_privkey,
2123 &wpa_s->conf->wps_nfc_dev_pw);
2127 int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2129 const struct wpabuf *dev_pw, u16 dev_pw_id,
2130 int p2p_group, const u8 *peer_pubkey_hash,
2131 const u8 *ssid, size_t ssid_len, int freq)
2133 struct wps_context *wps = wpa_s->wps;
2134 char pw[32 * 2 + 1];
2136 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2137 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2138 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2141 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
2142 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2143 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2144 "cannot start NFC-triggered connection");
2148 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2149 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2150 "cannot start NFC-triggered connection", dev_pw_id);
2154 dh5_free(wps->dh_ctx);
2155 wpabuf_free(wps->dh_pubkey);
2156 wpabuf_free(wps->dh_privkey);
2157 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2158 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2159 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2161 wpabuf_free(wps->dh_pubkey);
2162 wps->dh_pubkey = NULL;
2163 wpabuf_free(wps->dh_privkey);
2164 wps->dh_privkey = NULL;
2165 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
2168 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
2169 if (wps->dh_ctx == NULL) {
2170 wpabuf_free(wps->dh_pubkey);
2171 wps->dh_pubkey = NULL;
2172 wpabuf_free(wps->dh_privkey);
2173 wps->dh_privkey = NULL;
2174 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
2179 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2180 wpabuf_head(dev_pw),
2181 wpabuf_len(dev_pw));
2183 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2185 p2p_group, dev_pw_id, peer_pubkey_hash,
2186 ssid, ssid_len, freq);
2190 static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2191 struct wps_parse_attr *attr)
2194 * Disable existing networks temporarily to allow the newly learned
2195 * credential to be preferred. Enable the temporarily disabled networks
2198 wpas_wps_temp_disable(wpa_s, NULL);
2199 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2202 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2205 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2208 if (attr->ap_channel) {
2209 u16 chan = WPA_GET_BE16(attr->ap_channel);
2212 if (chan >= 1 && chan <= 13)
2213 freq = 2407 + 5 * chan;
2214 else if (chan == 14)
2216 else if (chan >= 30)
2217 freq = 5000 + 5 * chan;
2220 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2222 wpa_s->after_wps = 5;
2223 wpa_s->wps_freq = freq;
2227 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2228 "based on the received credential added");
2229 wpa_s->normal_scans = 0;
2230 wpa_supplicant_reinit_autoscan(wpa_s);
2231 wpa_s->disconnected = 0;
2232 wpa_s->reassociate = 1;
2234 wpa_supplicant_cancel_sched_scan(wpa_s);
2235 wpa_supplicant_req_scan(wpa_s, 0, 0);
2241 #ifdef CONFIG_WPS_ER
2242 static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2243 struct wps_parse_attr *attr)
2245 return wps_registrar_add_nfc_password_token(
2246 wpa_s->wps->registrar, attr->oob_dev_password,
2247 attr->oob_dev_password_len);
2249 #endif /* CONFIG_WPS_ER */
2252 static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2253 const struct wpabuf *wps)
2255 struct wps_parse_attr attr;
2257 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2259 if (wps_parse_msg(wps, &attr)) {
2260 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2265 return wpas_wps_use_cred(wpa_s, &attr);
2267 #ifdef CONFIG_WPS_ER
2268 if (attr.oob_dev_password)
2269 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2270 #endif /* CONFIG_WPS_ER */
2272 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2277 int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
2278 const struct wpabuf *data, int forced_freq)
2280 const struct wpabuf *wps = data;
2281 struct wpabuf *tmp = NULL;
2284 if (wpabuf_len(data) < 4)
2287 if (*wpabuf_head_u8(data) != 0x10) {
2288 /* Assume this contains full NDEF record */
2289 tmp = ndef_parse_wifi(data);
2292 tmp = ndef_parse_p2p(data);
2294 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2299 #endif /* CONFIG_P2P */
2300 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2306 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2312 struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2317 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2318 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2319 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2322 ret = wps_build_nfc_handover_req(wpa_s->wps,
2323 wpa_s->conf->wps_nfc_dh_pubkey);
2327 tmp = ndef_build_wifi(ret);
2338 #ifdef CONFIG_WPS_NFC
2340 static struct wpabuf *
2341 wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2344 #ifdef CONFIG_WPS_ER
2346 u8 u[UUID_LEN], *use_uuid = NULL;
2347 u8 addr[ETH_ALEN], *use_addr = NULL;
2348 struct wps_context *wps = wpa_s->wps;
2355 if (uuid_str2bin(uuid, u) == 0)
2357 else if (hwaddr_aton(uuid, addr) == 0)
2362 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2363 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2364 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2368 wpas_wps_nfc_clear(wps);
2369 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2370 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2371 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2372 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2373 wpas_wps_nfc_clear(wps);
2377 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2378 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
2381 tmp = ndef_build_wifi(ret);
2389 #else /* CONFIG_WPS_ER */
2391 #endif /* CONFIG_WPS_ER */
2393 #endif /* CONFIG_WPS_NFC */
2396 struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2397 int ndef, int cr, const char *uuid)
2402 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2405 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
2409 static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2410 const struct wpabuf *data)
2417 struct wps_parse_attr attr;
2419 const u8 *bssid = NULL;
2422 wps = ndef_parse_wifi(data);
2425 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2426 "payload from NFC connection handover");
2427 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2428 if (wpabuf_len(wps) < 2) {
2429 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2433 pos = wpabuf_head(wps);
2434 wsc_len = WPA_GET_BE16(pos);
2435 if (wsc_len > wpabuf_len(wps) - 2) {
2436 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2437 "in Wi-Fi Handover Select Message", wsc_len);
2442 wpa_hexdump(MSG_DEBUG,
2443 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2445 if (wsc_len < wpabuf_len(wps) - 2) {
2446 wpa_hexdump(MSG_DEBUG,
2447 "WPS: Ignore extra data after WSC attributes",
2448 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2451 wpabuf_set(&msg, pos, wsc_len);
2452 ret = wps_parse_msg(&msg, &attr);
2454 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2455 "Wi-Fi Handover Select Message");
2459 if (attr.oob_dev_password == NULL ||
2460 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2461 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2462 "included in Wi-Fi Handover Select Message");
2467 if (attr.ssid == NULL) {
2468 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2474 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2476 if (attr.mac_addr) {
2477 bssid = attr.mac_addr;
2478 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2483 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2485 if (attr.ap_channel) {
2486 u16 chan = WPA_GET_BE16(attr.ap_channel);
2488 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2490 if (chan >= 1 && chan <= 13 &&
2491 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2492 freq = 2407 + 5 * chan;
2493 else if (chan == 14 &&
2494 (attr.rf_bands == NULL ||
2495 *attr.rf_bands & WPS_RF_24GHZ))
2497 else if (chan >= 30 &&
2498 (attr.rf_bands == NULL ||
2499 *attr.rf_bands & WPS_RF_50GHZ))
2500 freq = 5000 + 5 * chan;
2503 wpa_printf(MSG_DEBUG,
2504 "WPS: AP indicated channel %u -> %u MHz",
2509 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2510 attr.oob_dev_password, attr.oob_dev_password_len);
2511 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2512 WPS_OOB_PUBKEY_HASH_LEN);
2513 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2514 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2515 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2519 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2520 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2522 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2523 attr.oob_dev_password,
2524 attr.ssid, attr.ssid_len, freq);
2532 int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2533 const struct wpabuf *req,
2534 const struct wpabuf *sel)
2536 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2537 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2538 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2539 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2543 int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2544 const struct wpabuf *req,
2545 const struct wpabuf *sel)
2552 struct wps_parse_attr attr;
2556 * Enrollee/station is always initiator of the NFC connection handover,
2557 * so use the request message here to find Enrollee public key hash.
2559 wps = ndef_parse_wifi(req);
2562 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2563 "payload from NFC connection handover");
2564 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2565 if (wpabuf_len(wps) < 2) {
2566 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2570 pos = wpabuf_head(wps);
2571 wsc_len = WPA_GET_BE16(pos);
2572 if (wsc_len > wpabuf_len(wps) - 2) {
2573 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2574 "in rt Wi-Fi Handover Request Message", wsc_len);
2579 wpa_hexdump(MSG_DEBUG,
2580 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2582 if (wsc_len < wpabuf_len(wps) - 2) {
2583 wpa_hexdump(MSG_DEBUG,
2584 "WPS: Ignore extra data after WSC attributes",
2585 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2588 wpabuf_set(&msg, pos, wsc_len);
2589 ret = wps_parse_msg(&msg, &attr);
2591 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2592 "Wi-Fi Handover Request Message");
2596 if (attr.oob_dev_password == NULL ||
2597 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2598 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2599 "included in Wi-Fi Handover Request Message");
2604 if (attr.uuid_e == NULL) {
2605 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2606 "Handover Request Message");
2611 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2613 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2614 attr.oob_dev_password, attr.oob_dev_password_len);
2615 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2616 WPS_OOB_PUBKEY_HASH_LEN);
2617 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2618 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2619 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2623 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2624 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2626 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2627 attr.oob_dev_password,
2628 DEV_PW_NFC_CONNECTION_HANDOVER,
2636 #endif /* CONFIG_WPS_NFC */
2639 static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2642 struct os_reltime now;
2644 if (wpa_debug_level > MSG_DEBUG)
2647 if (wpa_s->wps_ap == NULL)
2650 os_get_reltime(&now);
2652 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2653 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2654 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2656 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2657 "tries=%d last_attempt=%d sec ago blacklist=%d",
2658 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2659 ap->last_attempt.sec > 0 ?
2660 (int) now.sec - (int) ap->last_attempt.sec : -1,
2666 static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2671 if (wpa_s->wps_ap == NULL)
2674 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2675 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2676 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2684 static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2685 struct wpa_scan_res *res)
2688 enum wps_ap_info_type type;
2689 struct wps_ap_info *ap;
2692 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2695 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2699 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2701 type = WPS_AP_SEL_REG_OUR;
2703 type = WPS_AP_SEL_REG;
2705 type = WPS_AP_NOT_SEL_REG;
2709 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2711 if (ap->type != type) {
2712 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2713 " changed type %d -> %d",
2714 MAC2STR(res->bssid), ap->type, type);
2716 if (type != WPS_AP_NOT_SEL_REG)
2717 wpa_blacklist_del(wpa_s, ap->bssid);
2722 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2723 sizeof(struct wps_ap_info));
2728 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2729 wpa_s->num_wps_ap++;
2731 os_memset(ap, 0, sizeof(*ap));
2732 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2734 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2735 MAC2STR(ap->bssid), ap->type);
2739 void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2740 struct wpa_scan_results *scan_res)
2744 for (i = 0; i < scan_res->num; i++)
2745 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2747 wpas_wps_dump_ap_info(wpa_s);
2751 void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2753 struct wps_ap_info *ap;
2755 wpa_s->after_wps = 0;
2757 if (!wpa_s->wps_ap_iter)
2759 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2763 os_get_reltime(&ap->last_attempt);