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)
76 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
79 if (!wpa_s->wps_success &&
80 wpa_s->current_ssid &&
81 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
82 const u8 *bssid = wpa_s->bssid;
83 if (is_zero_ether_addr(bssid))
84 bssid = wpa_s->pending_bssid;
86 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
87 " did not succeed - continue trying to find "
88 "suitable AP", MAC2STR(bssid));
89 wpa_blacklist_add(wpa_s, bssid);
91 wpa_supplicant_deauthenticate(wpa_s,
92 WLAN_REASON_DEAUTH_LEAVING);
93 wpa_s->reassociate = 1;
94 wpa_supplicant_req_scan(wpa_s,
95 wpa_s->blacklist_cleared ? 5 : 0, 0);
96 wpa_s->blacklist_cleared = 0;
100 wpas_wps_clear_ap_info(wpa_s);
101 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
102 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
103 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
105 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
106 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
107 int disabled = wpa_s->current_ssid->disabled;
108 unsigned int freq = wpa_s->assoc_freq;
110 struct wpa_ssid *ssid = NULL;
111 int use_fast_assoc = 0;
113 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
114 "try to associate with the received credential "
116 wpa_supplicant_deauthenticate(wpa_s,
117 WLAN_REASON_DEAUTH_LEAVING);
119 wpa_printf(MSG_DEBUG, "WPS: Current network is "
120 "disabled - wait for user to enable");
123 wpa_s->after_wps = 5;
124 wpa_s->wps_freq = freq;
125 wpa_s->normal_scans = 0;
126 wpa_s->reassociate = 1;
128 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
129 "without a new scan can be used");
130 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
133 struct wps_parse_attr attr;
135 wps = wpa_bss_get_vendor_ie_multi(bss,
137 if (wps && wps_parse_msg(wps, &attr) == 0 &&
139 *attr.wps_state == WPS_STATE_CONFIGURED)
145 * Complete the next step from an eloop timeout to allow pending
146 * driver events related to the disconnection to be processed
147 * first. This makes it less likely for disconnection event to
148 * cause problems with the following connection.
150 wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
151 wpas_wps_assoc_with_cred_cancel(wpa_s);
152 eloop_register_timeout(0, 10000,
153 wpas_wps_assoc_with_cred, wpa_s,
154 use_fast_assoc ? (void *) 1 :
159 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
160 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
161 "for external credential processing");
162 wpas_clear_wps(wpa_s);
163 wpa_supplicant_deauthenticate(wpa_s,
164 WLAN_REASON_DEAUTH_LEAVING);
172 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
173 struct wpa_ssid *ssid,
174 const struct wps_credential *cred)
176 struct wpa_driver_capa capa;
179 struct wpa_ie_data adv;
180 int wpa2 = 0, ccmp = 0;
183 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
184 * case they are configured for mixed mode operation (WPA+WPA2 and
185 * TKIP+CCMP). Try to use scan results to figure out whether the AP
186 * actually supports stronger security and select that if the client
187 * has support for it, too.
190 if (wpa_drv_get_capa(wpa_s, &capa))
191 return; /* Unknown what driver supports */
193 if (ssid->ssid == NULL)
195 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
197 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
198 "table - use credential as-is");
202 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
204 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
205 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
207 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
210 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
211 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
212 adv.pairwise_cipher & WPA_CIPHER_CCMP)
216 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
217 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
219 * TODO: This could be the initial AP configuration and the
220 * Beacon contents could change shortly. Should request a new
221 * scan and delay addition of the network until the updated
222 * scan results are available.
224 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
225 "support - use credential as-is");
229 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
230 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
231 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
232 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
233 "based on scan results");
234 if (wpa_s->conf->ap_scan == 1)
235 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
237 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
240 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
241 (ssid->proto & WPA_PROTO_WPA) &&
242 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
243 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
244 "based on scan results");
245 if (wpa_s->conf->ap_scan == 1)
246 ssid->proto |= WPA_PROTO_RSN;
248 ssid->proto = WPA_PROTO_RSN;
253 static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
254 struct wpa_ssid *new_ssid)
256 struct wpa_ssid *ssid, *next;
258 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
259 ssid = next, next = ssid ? ssid->next : NULL) {
261 * new_ssid has already been added to the list in
262 * wpas_wps_add_network(), so skip it.
264 if (ssid == new_ssid)
267 if (ssid->bssid_set || new_ssid->bssid_set) {
268 if (ssid->bssid_set != new_ssid->bssid_set)
270 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
276 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
279 if (ssid->ssid && new_ssid->ssid) {
280 if (os_memcmp(ssid->ssid, new_ssid->ssid,
281 ssid->ssid_len) != 0)
283 } else if (ssid->ssid || new_ssid->ssid)
286 /* compare security parameters */
287 if (ssid->auth_alg != new_ssid->auth_alg ||
288 ssid->key_mgmt != new_ssid->key_mgmt ||
289 ssid->proto != new_ssid->proto ||
290 ssid->pairwise_cipher != new_ssid->pairwise_cipher ||
291 ssid->group_cipher != new_ssid->group_cipher)
294 /* Remove the duplicated older network entry. */
295 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
296 wpas_notify_network_removed(wpa_s, ssid);
297 wpa_config_remove_network(wpa_s->conf, ssid->id);
302 static int wpa_supplicant_wps_cred(void *ctx,
303 const struct wps_credential *cred)
305 struct wpa_supplicant *wpa_s = ctx;
306 struct wpa_ssid *ssid = wpa_s->current_ssid;
308 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
310 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
312 if ((wpa_s->conf->wps_cred_processing == 1 ||
313 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
314 size_t blen = cred->cred_attr_len * 2 + 1;
315 char *buf = os_malloc(blen);
317 wpa_snprintf_hex(buf, blen,
318 cred->cred_attr, cred->cred_attr_len);
319 wpa_msg(wpa_s, MSG_INFO, "%s%s",
320 WPS_EVENT_CRED_RECEIVED, buf);
324 wpas_notify_wps_credential(wpa_s, cred);
326 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
328 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
329 cred->cred_attr, cred->cred_attr_len);
331 if (wpa_s->conf->wps_cred_processing == 1)
334 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
335 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
337 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
338 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
339 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
340 cred->key, cred->key_len);
341 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
342 MAC2STR(cred->mac_addr));
344 auth_type = cred->auth_type;
345 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
346 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
347 "auth_type into WPA2PSK");
348 auth_type = WPS_AUTH_WPA2PSK;
351 if (auth_type != WPS_AUTH_OPEN &&
352 auth_type != WPS_AUTH_WPAPSK &&
353 auth_type != WPS_AUTH_WPA2PSK) {
354 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
355 "unsupported authentication type 0x%x",
360 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
361 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
362 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
363 "invalid Network Key length %lu",
364 (unsigned long) cred->key_len);
369 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
370 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
371 "on the received credential");
372 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
373 if (ssid->eap.identity &&
374 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
375 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
376 WSC_ID_REGISTRAR_LEN) == 0)
378 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
379 os_free(ssid->eap.identity);
380 ssid->eap.identity = NULL;
381 ssid->eap.identity_len = 0;
382 os_free(ssid->eap.phase1);
383 ssid->eap.phase1 = NULL;
384 os_free(ssid->eap.eap_methods);
385 ssid->eap.eap_methods = NULL;
386 if (!ssid->p2p_group) {
390 ssid->disabled_until.sec = 0;
391 ssid->disabled_until.usec = 0;
392 ssid->auth_failures = 0;
394 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
395 "received credential");
396 ssid = wpa_config_add_network(wpa_s->conf);
399 if (wpa_s->current_ssid) {
401 * Should the GO issue multiple credentials for some
402 * reason, each credential should be marked as a
403 * temporary P2P group similarly to the one that gets
404 * marked as such based on the pre-configured values
405 * used for the WPS network block.
407 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
408 ssid->temporary = wpa_s->current_ssid->temporary;
410 wpas_notify_network_added(wpa_s, ssid);
413 wpa_config_set_network_defaults(ssid);
416 ssid->ssid = os_malloc(cred->ssid_len);
418 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
419 ssid->ssid_len = cred->ssid_len;
422 switch (cred->encr_type) {
426 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
429 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
435 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
436 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
438 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
440 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
441 "id=%d - Credentials for an open "
442 "network disabled by default - use "
443 "'select_network %d' to enable",
447 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
449 case WPS_AUTH_WPAPSK:
450 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
451 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
452 ssid->proto = WPA_PROTO_WPA;
454 case WPS_AUTH_WPA2PSK:
455 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
456 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
457 ssid->proto = WPA_PROTO_RSN;
461 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
462 if (cred->key_len == 2 * PMK_LEN) {
463 if (hexstr2bin((const char *) cred->key, ssid->psk,
465 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
470 ssid->export_keys = 1;
471 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
472 os_free(ssid->passphrase);
473 ssid->passphrase = os_malloc(cred->key_len + 1);
474 if (ssid->passphrase == NULL)
476 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
477 ssid->passphrase[cred->key_len] = '\0';
478 wpa_config_update_psk(ssid);
479 ssid->export_keys = 1;
481 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
483 (unsigned long) cred->key_len);
488 wpas_wps_security_workaround(wpa_s, ssid, cred);
490 wpas_wps_remove_dup_network(wpa_s, ssid);
492 #ifndef CONFIG_NO_CONFIG_WRITE
493 if (wpa_s->conf->update_config &&
494 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
495 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
498 #endif /* CONFIG_NO_CONFIG_WRITE */
501 * Optimize the post-WPS scan based on the channel used during
502 * the provisioning in case EAP-Failure is not received.
504 wpa_s->after_wps = 5;
505 wpa_s->wps_freq = wpa_s->assoc_freq;
511 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
512 struct wps_event_m2d *m2d)
514 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
515 "dev_password_id=%d config_error=%d",
516 m2d->dev_password_id, m2d->config_error);
517 wpas_notify_wps_event_m2d(wpa_s, m2d);
519 if (wpa_s->parent && wpa_s->parent != wpa_s) {
520 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
521 "dev_password_id=%d config_error=%d",
522 m2d->dev_password_id, m2d->config_error);
524 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
526 * Notify P2P from eloop timeout to avoid issues with the
527 * interface getting removed while processing a message.
529 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
532 #endif /* CONFIG_P2P */
536 static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
538 struct wpa_supplicant *wpa_s = eloop_ctx;
539 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
540 wpas_clear_wps(wpa_s);
544 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
545 struct wps_event_fail *fail)
547 if (fail->error_indication > 0 &&
548 fail->error_indication < NUM_WPS_EI_VALUES) {
549 wpa_msg(wpa_s, MSG_INFO,
550 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
551 fail->msg, fail->config_error, fail->error_indication,
552 wps_ei_str(fail->error_indication));
553 if (wpa_s->parent && wpa_s->parent != wpa_s)
554 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
555 "msg=%d config_error=%d reason=%d (%s)",
556 fail->msg, fail->config_error,
557 fail->error_indication,
558 wps_ei_str(fail->error_indication));
560 wpa_msg(wpa_s, MSG_INFO,
561 WPS_EVENT_FAIL "msg=%d config_error=%d",
562 fail->msg, fail->config_error);
563 if (wpa_s->parent && wpa_s->parent != wpa_s)
564 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
565 "msg=%d config_error=%d",
566 fail->msg, fail->config_error);
570 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
572 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
573 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
574 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
576 wpas_notify_wps_event_fail(wpa_s, fail);
577 wpas_p2p_wps_failed(wpa_s, fail);
581 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
583 static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
585 struct wpa_ssid *ssid;
588 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
590 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
591 if (ssid->disabled_for_connect && ssid->disabled) {
592 ssid->disabled_for_connect = 0;
594 wpas_notify_network_enabled_changed(wpa_s, ssid);
600 #ifndef CONFIG_NO_CONFIG_WRITE
601 if (wpa_s->conf->update_config &&
602 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
603 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
606 #endif /* CONFIG_NO_CONFIG_WRITE */
611 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
613 struct wpa_supplicant *wpa_s = eloop_ctx;
614 /* Enable the networks disabled during wpas_wps_reassoc */
615 wpas_wps_reenable_networks(wpa_s);
619 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
621 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
622 wpa_s->wps_success = 1;
623 wpas_notify_wps_event_success(wpa_s);
624 if (wpa_s->current_ssid)
625 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
626 wpa_s->extra_blacklist_count = 0;
629 * Enable the networks disabled during wpas_wps_reassoc after 10
630 * seconds. The 10 seconds timer is to allow the data connection to be
631 * formed before allowing other networks to be selected.
633 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
636 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
640 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
641 struct wps_event_er_ap *ap)
644 char dev_type[WPS_DEV_TYPE_BUFSIZE];
646 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
647 if (ap->pri_dev_type)
648 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
653 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
654 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
655 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
656 ap->friendly_name ? ap->friendly_name : "",
657 ap->manufacturer ? ap->manufacturer : "",
658 ap->model_description ? ap->model_description : "",
659 ap->model_name ? ap->model_name : "",
660 ap->manufacturer_url ? ap->manufacturer_url : "",
661 ap->model_url ? ap->model_url : "");
665 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
666 struct wps_event_er_ap *ap)
669 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
670 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
674 static void wpa_supplicant_wps_event_er_enrollee_add(
675 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
678 char dev_type[WPS_DEV_TYPE_BUFSIZE];
680 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
681 if (enrollee->pri_dev_type)
682 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
687 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
688 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
690 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
691 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
692 enrollee->dev_name ? enrollee->dev_name : "",
693 enrollee->manufacturer ? enrollee->manufacturer : "",
694 enrollee->model_name ? enrollee->model_name : "",
695 enrollee->model_number ? enrollee->model_number : "",
696 enrollee->serial_number ? enrollee->serial_number : "");
700 static void wpa_supplicant_wps_event_er_enrollee_remove(
701 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
704 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
705 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
706 uuid_str, MAC2STR(enrollee->mac_addr));
710 static void wpa_supplicant_wps_event_er_ap_settings(
711 struct wpa_supplicant *wpa_s,
712 struct wps_event_er_ap_settings *ap_settings)
716 const struct wps_credential *cred = ap_settings->cred;
719 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
720 if (cred->key_len >= 8 && cred->key_len <= 64) {
721 os_memcpy(key_str, cred->key, cred->key_len);
722 key_str[cred->key_len] = '\0';
726 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
727 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
728 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
729 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
731 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
732 cred->auth_type, cred->encr_type, key_str);
736 static void wpa_supplicant_wps_event_er_set_sel_reg(
737 struct wpa_supplicant *wpa_s,
738 struct wps_event_er_set_selected_registrar *ev)
742 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
744 case WPS_ER_SET_SEL_REG_START:
745 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
746 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
747 "sel_reg_config_methods=0x%x",
748 uuid_str, ev->sel_reg, ev->dev_passwd_id,
749 ev->sel_reg_config_methods);
751 case WPS_ER_SET_SEL_REG_DONE:
752 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
753 "uuid=%s state=DONE", uuid_str);
755 case WPS_ER_SET_SEL_REG_FAILED:
756 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
757 "uuid=%s state=FAILED", uuid_str);
763 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
764 union wps_event_data *data)
766 struct wpa_supplicant *wpa_s = ctx;
769 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
772 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
775 wpa_supplicant_wps_event_success(wpa_s);
777 case WPS_EV_PWD_AUTH_FAIL:
779 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
780 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
781 #endif /* CONFIG_AP */
783 case WPS_EV_PBC_OVERLAP:
785 case WPS_EV_PBC_TIMEOUT:
787 case WPS_EV_PBC_ACTIVE:
788 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
790 case WPS_EV_PBC_DISABLE:
791 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
793 case WPS_EV_ER_AP_ADD:
794 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
796 case WPS_EV_ER_AP_REMOVE:
797 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
799 case WPS_EV_ER_ENROLLEE_ADD:
800 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
803 case WPS_EV_ER_ENROLLEE_REMOVE:
804 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
807 case WPS_EV_ER_AP_SETTINGS:
808 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
811 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
812 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
815 case WPS_EV_AP_PIN_SUCCESS:
821 static int wpa_supplicant_wps_rf_band(void *ctx)
823 struct wpa_supplicant *wpa_s = ctx;
825 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
828 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
832 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
834 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
835 eap_is_wps_pin_enrollee(&ssid->eap))
836 return WPS_REQ_ENROLLEE;
838 return WPS_REQ_REGISTRAR;
842 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
845 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
847 wpa_s->after_wps = 0;
848 wpa_s->known_wps_freq = 0;
850 prev_current = wpa_s->current_ssid;
852 /* Enable the networks disabled during wpas_wps_reassoc */
853 wpas_wps_reenable_networks(wpa_s);
855 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
856 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
858 /* Remove any existing WPS network from configuration */
859 ssid = wpa_s->conf->ssid;
861 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
862 if (ssid == wpa_s->current_ssid) {
863 wpa_supplicant_deauthenticate(
864 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
872 if (prev_current == remove_ssid) {
873 wpa_sm_set_config(wpa_s->wpa, NULL);
874 eapol_sm_notify_config(wpa_s->eapol, NULL,
877 wpas_notify_network_removed(wpa_s, remove_ssid);
878 wpa_config_remove_network(wpa_s->conf, id);
882 wpas_wps_clear_ap_info(wpa_s);
886 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
888 struct wpa_supplicant *wpa_s = eloop_ctx;
889 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
891 wpas_clear_wps(wpa_s);
895 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
896 int registrar, const u8 *dev_addr,
899 struct wpa_ssid *ssid;
901 ssid = wpa_config_add_network(wpa_s->conf);
904 wpas_notify_network_added(wpa_s, ssid);
905 wpa_config_set_network_defaults(ssid);
907 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
908 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
909 wpa_config_set(ssid, "identity", registrar ?
910 "\"" WSC_ID_REGISTRAR "\"" :
911 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
912 wpas_notify_network_removed(wpa_s, ssid);
913 wpa_config_remove_network(wpa_s->conf, ssid->id);
919 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
920 #endif /* CONFIG_P2P */
926 #endif /* CONFIG_P2P */
928 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
932 * Note: With P2P, the SSID may change at the time the WPS
933 * provisioning is started, so better not filter the AP based
934 * on the current SSID in the scan results.
937 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
938 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
942 ssid->ssid = os_malloc(bss->ssid_len);
943 if (ssid->ssid == NULL)
945 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
946 ssid->ssid_len = bss->ssid_len;
947 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
949 ssid->ssid, ssid->ssid_len);
954 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
955 "for the AP; use wildcard");
960 #endif /* CONFIG_P2P */
967 static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
968 struct wpa_ssid *selected)
970 struct wpa_ssid *ssid;
972 if (wpa_s->current_ssid)
973 wpa_supplicant_deauthenticate(
974 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
976 /* Mark all other networks disabled and trigger reassociation */
977 ssid = wpa_s->conf->ssid;
979 int was_disabled = ssid->disabled;
980 ssid->disabled_for_connect = 0;
982 * In case the network object corresponds to a persistent group
983 * then do not send out network disabled signal. In addition,
984 * do not change disabled status of persistent network objects
985 * from 2 to 1 should we connect to another network.
987 if (was_disabled != 2) {
988 ssid->disabled = ssid != selected;
989 if (was_disabled != ssid->disabled) {
991 ssid->disabled_for_connect = 1;
992 wpas_notify_network_enabled_changed(wpa_s,
1001 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
1002 struct wpa_ssid *selected, const u8 *bssid,
1005 struct wpa_bss *bss;
1007 wpa_s->after_wps = 0;
1008 wpa_s->known_wps_freq = 0;
1010 wpa_s->after_wps = 5;
1011 wpa_s->wps_freq = freq;
1013 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1014 if (bss && bss->freq > 0) {
1015 wpa_s->known_wps_freq = 1;
1016 wpa_s->wps_freq = bss->freq;
1020 wpas_wps_temp_disable(wpa_s, selected);
1022 wpa_s->disconnected = 0;
1023 wpa_s->reassociate = 1;
1024 wpa_s->scan_runs = 0;
1025 wpa_s->normal_scans = 0;
1026 wpa_s->wps_success = 0;
1027 wpa_s->blacklist_cleared = 0;
1029 wpa_supplicant_cancel_sched_scan(wpa_s);
1030 wpa_supplicant_req_scan(wpa_s, 0, 0);
1034 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1037 struct wpa_ssid *ssid;
1038 wpas_clear_wps(wpa_s);
1039 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
1042 ssid->temporary = 1;
1043 ssid->p2p_group = p2p_group;
1045 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1046 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1048 ssid->ssid_len = wpa_s->go_params->ssid_len;
1049 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1051 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1052 "SSID", ssid->ssid, ssid->ssid_len);
1055 #endif /* CONFIG_P2P */
1056 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1058 if (wpa_s->wps_fragment_size)
1059 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1060 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1062 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
1067 static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1068 const u8 *dev_addr, const u8 *bssid,
1069 const char *pin, int p2p_group, u16 dev_pw_id,
1070 const u8 *peer_pubkey_hash,
1071 const u8 *ssid_val, size_t ssid_len, int freq)
1073 struct wpa_ssid *ssid;
1074 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
1075 unsigned int rpin = 0;
1076 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
1078 wpas_clear_wps(wpa_s);
1079 if (bssid && is_zero_ether_addr(bssid))
1081 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1083 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
1086 ssid->temporary = 1;
1087 ssid->p2p_group = p2p_group;
1089 ssid->ssid = os_malloc(ssid_len);
1091 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1092 ssid->ssid_len = ssid_len;
1095 if (peer_pubkey_hash) {
1096 os_memcpy(hash, " pkhash=", 8);
1097 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1099 WPS_OOB_PUBKEY_HASH_LEN);
1104 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1105 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1107 ssid->ssid_len = wpa_s->go_params->ssid_len;
1108 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1110 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1111 "SSID", ssid->ssid, ssid->ssid_len);
1114 #endif /* CONFIG_P2P */
1116 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1117 pin, dev_pw_id, hash);
1118 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1119 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1122 rpin = wps_generate_pin();
1123 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1124 rpin, dev_pw_id, hash);
1126 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1127 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
1130 if (wpa_s->wps_fragment_size)
1131 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1132 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1134 wpa_s->wps_ap_iter = 1;
1135 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
1140 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1141 const char *pin, int p2p_group, u16 dev_pw_id)
1143 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1144 dev_pw_id, NULL, NULL, 0, 0);
1148 /* Cancel the wps pbc/pin requests */
1149 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1152 if (wpa_s->ap_iface) {
1153 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1154 return wpa_supplicant_ap_wps_cancel(wpa_s);
1156 #endif /* CONFIG_AP */
1158 if (wpa_s->wpa_state == WPA_SCANNING ||
1159 wpa_s->wpa_state == WPA_DISCONNECTED) {
1160 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1161 wpa_supplicant_cancel_scan(wpa_s);
1162 wpas_clear_wps(wpa_s);
1163 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1164 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1166 wpa_supplicant_deauthenticate(wpa_s,
1167 WLAN_REASON_DEAUTH_LEAVING);
1168 wpas_clear_wps(wpa_s);
1170 wpas_wps_reenable_networks(wpa_s);
1171 wpas_wps_clear_ap_info(wpa_s);
1172 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1174 wpas_clear_wps(wpa_s);
1177 wpa_s->after_wps = 0;
1183 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1184 const char *pin, struct wps_new_ap_settings *settings)
1186 struct wpa_ssid *ssid;
1193 wpas_clear_wps(wpa_s);
1194 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
1197 ssid->temporary = 1;
1199 end = pos + sizeof(val);
1200 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1201 if (res < 0 || res >= end - pos)
1205 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1206 "new_encr=%s new_key=%s",
1207 settings->ssid_hex, settings->auth,
1208 settings->encr, settings->key_hex);
1209 if (res < 0 || res >= end - pos)
1213 res = os_snprintf(pos, end - pos, "\"");
1214 if (res < 0 || res >= end - pos)
1216 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1218 if (wpa_s->wps_fragment_size)
1219 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1220 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1222 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
1227 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1228 const u8 *p2p_dev_addr, const u8 *psk,
1231 if (is_zero_ether_addr(p2p_dev_addr)) {
1232 wpa_printf(MSG_DEBUG,
1233 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1236 wpa_printf(MSG_DEBUG,
1237 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1238 " P2P Device Addr " MACSTR,
1239 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1241 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1249 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1250 const struct wps_device_data *dev)
1252 char uuid[40], txt[400];
1254 char devtype[WPS_DEV_TYPE_BUFSIZE];
1255 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1257 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1258 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1259 " [%s|%s|%s|%s|%s|%s]",
1260 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1261 dev->manufacturer, dev->model_name,
1262 dev->model_number, dev->serial_number,
1263 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1265 if (len > 0 && len < (int) sizeof(txt))
1266 wpa_printf(MSG_INFO, "%s", txt);
1270 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1271 u16 sel_reg_config_methods)
1273 #ifdef CONFIG_WPS_ER
1274 struct wpa_supplicant *wpa_s = ctx;
1276 if (wpa_s->wps_er == NULL)
1278 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1279 "dev_password_id=%u sel_reg_config_methods=0x%x",
1280 sel_reg, dev_passwd_id, sel_reg_config_methods);
1281 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1282 sel_reg_config_methods);
1283 #endif /* CONFIG_WPS_ER */
1287 static u16 wps_fix_config_methods(u16 config_methods)
1289 if ((config_methods &
1290 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1291 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1292 wpa_printf(MSG_INFO, "WPS: Converting display to "
1293 "virtual_display for WPS 2.0 compliance");
1294 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1296 if ((config_methods &
1297 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1298 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1299 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1300 "virtual_push_button for WPS 2.0 compliance");
1301 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1304 return config_methods;
1308 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1309 struct wps_context *wps)
1314 if (is_nil_uuid(wpa_s->conf->uuid)) {
1315 struct wpa_supplicant *first;
1316 first = wpa_s->global->ifaces;
1317 while (first && first->next)
1318 first = first->next;
1319 if (first && first != wpa_s) {
1320 if (wps != wpa_s->global->ifaces->wps)
1321 os_memcpy(wps->uuid,
1322 wpa_s->global->ifaces->wps->uuid,
1324 src = "from the first interface";
1326 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1327 src = "based on MAC address";
1330 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1331 src = "based on configuration";
1334 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1335 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
1339 static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1340 struct wps_context *wps)
1342 wpabuf_free(wps->dev.vendor_ext_m1);
1343 wps->dev.vendor_ext_m1 = NULL;
1345 if (wpa_s->conf->wps_vendor_ext_m1) {
1346 wps->dev.vendor_ext_m1 =
1347 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1348 if (!wps->dev.vendor_ext_m1) {
1349 wpa_printf(MSG_ERROR, "WPS: Cannot "
1350 "allocate memory for vendor_ext_m1");
1356 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1358 struct wps_context *wps;
1359 struct wps_registrar_config rcfg;
1360 struct hostapd_hw_modes *modes;
1363 wps = os_zalloc(sizeof(*wps));
1367 wps->cred_cb = wpa_supplicant_wps_cred;
1368 wps->event_cb = wpa_supplicant_wps_event;
1369 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
1370 wps->cb_ctx = wpa_s;
1372 wps->dev.device_name = wpa_s->conf->device_name;
1373 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1374 wps->dev.model_name = wpa_s->conf->model_name;
1375 wps->dev.model_number = wpa_s->conf->model_number;
1376 wps->dev.serial_number = wpa_s->conf->serial_number;
1377 wps->config_methods =
1378 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1379 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1380 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1381 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1382 "methods are not allowed at the same time");
1386 wps->config_methods = wps_fix_config_methods(wps->config_methods);
1387 wps->dev.config_methods = wps->config_methods;
1388 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1391 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1392 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1393 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1395 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1397 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1398 modes = wpa_s->hw.modes;
1400 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1401 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1402 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1403 wps->dev.rf_bands |= WPS_RF_24GHZ;
1404 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1405 wps->dev.rf_bands |= WPS_RF_50GHZ;
1408 if (wps->dev.rf_bands == 0) {
1410 * Default to claiming support for both bands if the driver
1411 * does not provide support for fetching supported bands.
1413 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1415 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1416 wpas_wps_set_uuid(wpa_s, wps);
1418 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1419 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1421 os_memset(&rcfg, 0, sizeof(rcfg));
1422 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1423 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1424 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1425 rcfg.cb_ctx = wpa_s;
1427 wps->registrar = wps_registrar_init(wps, &rcfg);
1428 if (wps->registrar == NULL) {
1429 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1440 #ifdef CONFIG_WPS_ER
1441 static void wpas_wps_nfc_clear(struct wps_context *wps)
1443 wps->ap_nfc_dev_pw_id = 0;
1444 wpabuf_free(wps->ap_nfc_dh_pubkey);
1445 wps->ap_nfc_dh_pubkey = NULL;
1446 wpabuf_free(wps->ap_nfc_dh_privkey);
1447 wps->ap_nfc_dh_privkey = NULL;
1448 wpabuf_free(wps->ap_nfc_dev_pw);
1449 wps->ap_nfc_dev_pw = NULL;
1451 #endif /* CONFIG_WPS_ER */
1454 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1456 wpas_wps_assoc_with_cred_cancel(wpa_s);
1457 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1458 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
1459 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
1460 wpas_wps_clear_ap_info(wpa_s);
1463 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1464 #endif /* CONFIG_P2P */
1466 if (wpa_s->wps == NULL)
1469 #ifdef CONFIG_WPS_ER
1470 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1471 wpa_s->wps_er = NULL;
1472 wpas_wps_nfc_clear(wpa_s->wps);
1473 #endif /* CONFIG_WPS_ER */
1475 wps_registrar_deinit(wpa_s->wps->registrar);
1476 wpabuf_free(wpa_s->wps->dh_pubkey);
1477 wpabuf_free(wpa_s->wps->dh_privkey);
1478 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
1479 os_free(wpa_s->wps->network_key);
1480 os_free(wpa_s->wps);
1485 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1486 struct wpa_ssid *ssid, struct wpa_bss *bss)
1488 struct wpabuf *wps_ie;
1490 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1493 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1494 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1496 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1500 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1501 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1502 "without active PBC Registrar");
1503 wpabuf_free(wps_ie);
1507 /* TODO: overlap detection */
1508 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1510 wpabuf_free(wps_ie);
1514 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1516 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1521 * Start with WPS APs that advertise our address as an
1522 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1523 * allow any WPS AP after couple of scans since some APs do not
1524 * set Selected Registrar attribute properly when using
1525 * external Registrar.
1527 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1528 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1529 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1530 "without active PIN Registrar");
1531 wpabuf_free(wps_ie);
1534 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1536 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1537 "(Authorized MAC or Active PIN)");
1539 wpabuf_free(wps_ie);
1544 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1545 wpabuf_free(wps_ie);
1553 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1554 struct wpa_ssid *ssid,
1555 struct wpa_bss *bss)
1557 struct wpabuf *wps_ie = NULL;
1560 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1561 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1562 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1563 /* allow wildcard SSID for WPS PBC */
1566 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1567 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1569 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1570 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1571 /* allow wildcard SSID for WPS PIN */
1576 if (!ret && ssid->bssid_set &&
1577 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1578 /* allow wildcard SSID due to hardcoded BSSID match */
1582 #ifdef CONFIG_WPS_STRICT
1584 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1587 if (bss->beacon_ie_len) {
1588 struct wpabuf *bcn_wps;
1589 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
1590 bss, WPS_IE_VENDOR_TYPE);
1591 if (bcn_wps == NULL) {
1592 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1593 "missing from AP Beacon");
1596 if (wps_validate_beacon(wps_ie) < 0)
1598 wpabuf_free(bcn_wps);
1602 #endif /* CONFIG_WPS_STRICT */
1604 wpabuf_free(wps_ie);
1610 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1611 struct wpa_bss *selected, struct wpa_ssid *ssid)
1613 const u8 *sel_uuid, *uuid;
1614 struct wpabuf *wps_ie;
1616 struct wpa_bss *bss;
1618 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1621 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1622 "present in scan results; selected BSSID " MACSTR,
1623 MAC2STR(selected->bssid));
1625 /* Make sure that only one AP is in active PBC mode */
1626 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1628 sel_uuid = wps_get_uuid_e(wps_ie);
1629 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1630 sel_uuid, UUID_LEN);
1632 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1637 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1639 if (bss == selected)
1641 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1644 if (!wps_is_selected_pbc_registrar(ie)) {
1648 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1649 MACSTR, MAC2STR(bss->bssid));
1650 uuid = wps_get_uuid_e(ie);
1651 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1653 if (sel_uuid == NULL || uuid == NULL ||
1654 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1655 ret = 1; /* PBC overlap */
1656 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1657 MACSTR " and " MACSTR,
1658 MAC2STR(selected->bssid),
1659 MAC2STR(bss->bssid));
1664 /* TODO: verify that this is reasonable dual-band situation */
1669 wpabuf_free(wps_ie);
1675 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1677 struct wpa_bss *bss;
1678 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1680 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1683 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1685 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1688 if (wps_is_selected_pbc_registrar(ie))
1690 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1692 else if (wps_is_selected_pin_registrar(ie))
1700 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1702 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1704 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1706 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1710 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1712 struct wpa_ssid *ssid;
1714 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1715 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1723 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1726 struct wpabuf *wps_ie;
1729 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1733 ret = wps_attr_text(wps_ie, buf, end);
1734 wpabuf_free(wps_ie);
1739 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1741 #ifdef CONFIG_WPS_ER
1742 if (wpa_s->wps_er) {
1743 wps_er_refresh(wpa_s->wps_er);
1746 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1747 if (wpa_s->wps_er == NULL)
1750 #else /* CONFIG_WPS_ER */
1752 #endif /* CONFIG_WPS_ER */
1756 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1758 #ifdef CONFIG_WPS_ER
1759 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1760 wpa_s->wps_er = NULL;
1761 #endif /* CONFIG_WPS_ER */
1766 #ifdef CONFIG_WPS_ER
1767 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1768 const char *uuid, const char *pin)
1771 const u8 *use_uuid = NULL;
1772 u8 addr_buf[ETH_ALEN];
1774 if (os_strcmp(uuid, "any") == 0) {
1775 } else if (uuid_str2bin(uuid, u) == 0) {
1777 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1778 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1779 if (use_uuid == NULL)
1783 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1785 (const u8 *) pin, os_strlen(pin), 300);
1789 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1791 u8 u[UUID_LEN], *use_uuid = NULL;
1792 u8 addr[ETH_ALEN], *use_addr = NULL;
1794 if (uuid_str2bin(uuid, u) == 0)
1796 else if (hwaddr_aton(uuid, addr) == 0)
1800 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
1804 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1807 u8 u[UUID_LEN], *use_uuid = NULL;
1808 u8 addr[ETH_ALEN], *use_addr = NULL;
1810 if (uuid_str2bin(uuid, u) == 0)
1812 else if (hwaddr_aton(uuid, addr) == 0)
1817 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
1822 static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1823 struct wps_credential *cred)
1825 os_memset(cred, 0, sizeof(*cred));
1826 if (ssid->ssid_len > 32)
1828 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1829 cred->ssid_len = ssid->ssid_len;
1830 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1831 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1832 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1833 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1834 cred->encr_type = WPS_ENCR_AES;
1836 cred->encr_type = WPS_ENCR_TKIP;
1837 if (ssid->passphrase) {
1838 cred->key_len = os_strlen(ssid->passphrase);
1839 if (cred->key_len >= 64)
1841 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1842 } else if (ssid->psk_set) {
1844 os_memcpy(cred->key, ssid->psk, 32);
1848 cred->auth_type = WPS_AUTH_OPEN;
1849 cred->encr_type = WPS_ENCR_NONE;
1856 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1859 u8 u[UUID_LEN], *use_uuid = NULL;
1860 u8 addr[ETH_ALEN], *use_addr = NULL;
1861 struct wpa_ssid *ssid;
1862 struct wps_credential cred;
1864 if (uuid_str2bin(uuid, u) == 0)
1866 else if (hwaddr_aton(uuid, addr) == 0)
1870 ssid = wpa_config_get_network(wpa_s->conf, id);
1871 if (ssid == NULL || ssid->ssid == NULL)
1874 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
1876 return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
1880 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1881 const char *pin, struct wps_new_ap_settings *settings)
1883 u8 u[UUID_LEN], *use_uuid = NULL;
1884 u8 addr[ETH_ALEN], *use_addr = NULL;
1885 struct wps_credential cred;
1888 if (uuid_str2bin(uuid, u) == 0)
1890 else if (hwaddr_aton(uuid, addr) == 0)
1894 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1895 settings->encr == NULL || settings->key_hex == NULL)
1898 os_memset(&cred, 0, sizeof(cred));
1899 len = os_strlen(settings->ssid_hex);
1900 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1901 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1903 cred.ssid_len = len / 2;
1905 len = os_strlen(settings->key_hex);
1906 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1907 hexstr2bin(settings->key_hex, cred.key, len / 2))
1909 cred.key_len = len / 2;
1911 if (os_strcmp(settings->auth, "OPEN") == 0)
1912 cred.auth_type = WPS_AUTH_OPEN;
1913 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1914 cred.auth_type = WPS_AUTH_WPAPSK;
1915 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1916 cred.auth_type = WPS_AUTH_WPA2PSK;
1920 if (os_strcmp(settings->encr, "NONE") == 0)
1921 cred.encr_type = WPS_ENCR_NONE;
1922 #ifdef CONFIG_TESTING_OPTIONS
1923 else if (os_strcmp(settings->encr, "WEP") == 0)
1924 cred.encr_type = WPS_ENCR_WEP;
1925 #endif /* CONFIG_TESTING_OPTIONS */
1926 else if (os_strcmp(settings->encr, "TKIP") == 0)
1927 cred.encr_type = WPS_ENCR_TKIP;
1928 else if (os_strcmp(settings->encr, "CCMP") == 0)
1929 cred.encr_type = WPS_ENCR_AES;
1933 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1934 (const u8 *) pin, os_strlen(pin), &cred);
1938 #ifdef CONFIG_WPS_NFC
1939 struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1940 int ndef, const char *uuid)
1943 u8 u[UUID_LEN], *use_uuid = NULL;
1944 u8 addr[ETH_ALEN], *use_addr = NULL;
1949 if (uuid_str2bin(uuid, u) == 0)
1951 else if (hwaddr_aton(uuid, addr) == 0)
1956 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
1959 tmp = ndef_build_wifi(ret);
1968 #endif /* CONFIG_WPS_NFC */
1971 static int callbacks_pending = 0;
1973 static void wpas_wps_terminate_cb(void *ctx)
1975 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1976 if (--callbacks_pending <= 0)
1979 #endif /* CONFIG_WPS_ER */
1982 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1984 #ifdef CONFIG_WPS_ER
1985 if (wpa_s->wps_er) {
1986 callbacks_pending++;
1987 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1988 wpa_s->wps_er = NULL;
1991 #endif /* CONFIG_WPS_ER */
1996 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1998 struct wps_context *wps = wpa_s->wps;
2003 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2004 wps->config_methods = wps_config_methods_str2bin(
2005 wpa_s->conf->config_methods);
2006 if ((wps->config_methods &
2007 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2008 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2009 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2010 "config methods are not allowed at the "
2012 wps->config_methods &= ~WPS_CONFIG_LABEL;
2015 wps->config_methods = wps_fix_config_methods(wps->config_methods);
2016 wps->dev.config_methods = wps->config_methods;
2018 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2019 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2022 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2023 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2024 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2025 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2028 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2029 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2031 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2032 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2034 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2035 wpas_wps_set_uuid(wpa_s, wps);
2037 if (wpa_s->conf->changed_parameters &
2038 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2039 /* Update pointers to make sure they refer current values */
2040 wps->dev.device_name = wpa_s->conf->device_name;
2041 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2042 wps->dev.model_name = wpa_s->conf->model_name;
2043 wps->dev.model_number = wpa_s->conf->model_number;
2044 wps->dev.serial_number = wpa_s->conf->serial_number;
2049 #ifdef CONFIG_WPS_NFC
2051 #ifdef CONFIG_WPS_ER
2052 static struct wpabuf *
2053 wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2054 struct wpa_ssid *ssid)
2057 struct wps_credential cred;
2059 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2062 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2066 tmp = ndef_build_wifi(ret);
2075 #endif /* CONFIG_WPS_ER */
2078 struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2079 int ndef, const char *id_str)
2081 #ifdef CONFIG_WPS_ER
2085 struct wpa_ssid *ssid;
2087 id = strtol(id_str, &end, 10);
2091 ssid = wpa_config_get_network(wpa_s->conf, id);
2094 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2096 #endif /* CONFIG_WPS_ER */
2098 if (wpa_s->ap_iface)
2099 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2100 #endif /* CONFIG_AP */
2105 struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2107 if (wpa_s->conf->wps_nfc_pw_from_config) {
2108 return wps_nfc_token_build(ndef,
2109 wpa_s->conf->wps_nfc_dev_pw_id,
2110 wpa_s->conf->wps_nfc_dh_pubkey,
2111 wpa_s->conf->wps_nfc_dev_pw);
2114 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2115 &wpa_s->conf->wps_nfc_dh_pubkey,
2116 &wpa_s->conf->wps_nfc_dh_privkey,
2117 &wpa_s->conf->wps_nfc_dev_pw);
2121 int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2123 const struct wpabuf *dev_pw, u16 dev_pw_id,
2124 int p2p_group, const u8 *peer_pubkey_hash,
2125 const u8 *ssid, size_t ssid_len, int freq)
2127 struct wps_context *wps = wpa_s->wps;
2128 char pw[32 * 2 + 1];
2130 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2131 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2132 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2135 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
2136 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2137 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2138 "cannot start NFC-triggered connection");
2142 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2143 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2144 "cannot start NFC-triggered connection", dev_pw_id);
2148 dh5_free(wps->dh_ctx);
2149 wpabuf_free(wps->dh_pubkey);
2150 wpabuf_free(wps->dh_privkey);
2151 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2152 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2153 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2155 wpabuf_free(wps->dh_pubkey);
2156 wps->dh_pubkey = NULL;
2157 wpabuf_free(wps->dh_privkey);
2158 wps->dh_privkey = NULL;
2159 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
2162 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
2163 if (wps->dh_ctx == NULL) {
2164 wpabuf_free(wps->dh_pubkey);
2165 wps->dh_pubkey = NULL;
2166 wpabuf_free(wps->dh_privkey);
2167 wps->dh_privkey = NULL;
2168 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
2173 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2174 wpabuf_head(dev_pw),
2175 wpabuf_len(dev_pw));
2177 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2179 p2p_group, dev_pw_id, peer_pubkey_hash,
2180 ssid, ssid_len, freq);
2184 static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2185 struct wps_parse_attr *attr)
2188 * Disable existing networks temporarily to allow the newly learned
2189 * credential to be preferred. Enable the temporarily disabled networks
2192 wpas_wps_temp_disable(wpa_s, NULL);
2193 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2196 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2199 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2202 if (attr->ap_channel) {
2203 u16 chan = WPA_GET_BE16(attr->ap_channel);
2206 if (chan >= 1 && chan <= 13)
2207 freq = 2407 + 5 * chan;
2208 else if (chan == 14)
2210 else if (chan >= 30)
2211 freq = 5000 + 5 * chan;
2214 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2216 wpa_s->after_wps = 5;
2217 wpa_s->wps_freq = freq;
2221 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2222 "based on the received credential added");
2223 wpa_s->normal_scans = 0;
2224 wpa_supplicant_reinit_autoscan(wpa_s);
2225 wpa_s->disconnected = 0;
2226 wpa_s->reassociate = 1;
2228 wpa_supplicant_cancel_sched_scan(wpa_s);
2229 wpa_supplicant_req_scan(wpa_s, 0, 0);
2235 #ifdef CONFIG_WPS_ER
2236 static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2237 struct wps_parse_attr *attr)
2239 return wps_registrar_add_nfc_password_token(
2240 wpa_s->wps->registrar, attr->oob_dev_password,
2241 attr->oob_dev_password_len);
2243 #endif /* CONFIG_WPS_ER */
2246 static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2247 const struct wpabuf *wps)
2249 struct wps_parse_attr attr;
2251 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2253 if (wps_parse_msg(wps, &attr)) {
2254 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2259 return wpas_wps_use_cred(wpa_s, &attr);
2261 #ifdef CONFIG_WPS_ER
2262 if (attr.oob_dev_password)
2263 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2264 #endif /* CONFIG_WPS_ER */
2266 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2271 int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
2272 const struct wpabuf *data, int forced_freq)
2274 const struct wpabuf *wps = data;
2275 struct wpabuf *tmp = NULL;
2278 if (wpabuf_len(data) < 4)
2281 if (*wpabuf_head_u8(data) != 0x10) {
2282 /* Assume this contains full NDEF record */
2283 tmp = ndef_parse_wifi(data);
2286 tmp = ndef_parse_p2p(data);
2288 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2293 #endif /* CONFIG_P2P */
2294 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2300 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2306 struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2311 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2312 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2313 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2316 ret = wps_build_nfc_handover_req(wpa_s->wps,
2317 wpa_s->conf->wps_nfc_dh_pubkey);
2321 tmp = ndef_build_wifi(ret);
2332 #ifdef CONFIG_WPS_NFC
2334 static struct wpabuf *
2335 wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2338 #ifdef CONFIG_WPS_ER
2340 u8 u[UUID_LEN], *use_uuid = NULL;
2341 u8 addr[ETH_ALEN], *use_addr = NULL;
2342 struct wps_context *wps = wpa_s->wps;
2349 if (uuid_str2bin(uuid, u) == 0)
2351 else if (hwaddr_aton(uuid, addr) == 0)
2356 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2357 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2358 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2362 wpas_wps_nfc_clear(wps);
2363 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2364 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2365 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2366 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2367 wpas_wps_nfc_clear(wps);
2371 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2372 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
2375 tmp = ndef_build_wifi(ret);
2383 #else /* CONFIG_WPS_ER */
2385 #endif /* CONFIG_WPS_ER */
2387 #endif /* CONFIG_WPS_NFC */
2390 struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2391 int ndef, int cr, const char *uuid)
2396 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2399 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
2403 static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2404 const struct wpabuf *data)
2411 struct wps_parse_attr attr;
2413 const u8 *bssid = NULL;
2416 wps = ndef_parse_wifi(data);
2419 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2420 "payload from NFC connection handover");
2421 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2422 if (wpabuf_len(wps) < 2) {
2423 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2427 pos = wpabuf_head(wps);
2428 wsc_len = WPA_GET_BE16(pos);
2429 if (wsc_len > wpabuf_len(wps) - 2) {
2430 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2431 "in Wi-Fi Handover Select Message", wsc_len);
2436 wpa_hexdump(MSG_DEBUG,
2437 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2439 if (wsc_len < wpabuf_len(wps) - 2) {
2440 wpa_hexdump(MSG_DEBUG,
2441 "WPS: Ignore extra data after WSC attributes",
2442 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2445 wpabuf_set(&msg, pos, wsc_len);
2446 ret = wps_parse_msg(&msg, &attr);
2448 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2449 "Wi-Fi Handover Select Message");
2453 if (attr.oob_dev_password == NULL ||
2454 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2455 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2456 "included in Wi-Fi Handover Select Message");
2461 if (attr.ssid == NULL) {
2462 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2468 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2470 if (attr.mac_addr) {
2471 bssid = attr.mac_addr;
2472 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2477 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2479 if (attr.ap_channel) {
2480 u16 chan = WPA_GET_BE16(attr.ap_channel);
2482 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2484 if (chan >= 1 && chan <= 13 &&
2485 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2486 freq = 2407 + 5 * chan;
2487 else if (chan == 14 &&
2488 (attr.rf_bands == NULL ||
2489 *attr.rf_bands & WPS_RF_24GHZ))
2491 else if (chan >= 30 &&
2492 (attr.rf_bands == NULL ||
2493 *attr.rf_bands & WPS_RF_50GHZ))
2494 freq = 5000 + 5 * chan;
2497 wpa_printf(MSG_DEBUG,
2498 "WPS: AP indicated channel %u -> %u MHz",
2503 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2504 attr.oob_dev_password, attr.oob_dev_password_len);
2505 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2506 WPS_OOB_PUBKEY_HASH_LEN);
2507 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2508 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2509 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2513 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2514 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2516 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2517 attr.oob_dev_password,
2518 attr.ssid, attr.ssid_len, freq);
2526 int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2527 const struct wpabuf *req,
2528 const struct wpabuf *sel)
2530 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2531 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2532 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2533 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2537 int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2538 const struct wpabuf *req,
2539 const struct wpabuf *sel)
2546 struct wps_parse_attr attr;
2550 * Enrollee/station is always initiator of the NFC connection handover,
2551 * so use the request message here to find Enrollee public key hash.
2553 wps = ndef_parse_wifi(req);
2556 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2557 "payload from NFC connection handover");
2558 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2559 if (wpabuf_len(wps) < 2) {
2560 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2564 pos = wpabuf_head(wps);
2565 wsc_len = WPA_GET_BE16(pos);
2566 if (wsc_len > wpabuf_len(wps) - 2) {
2567 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2568 "in rt Wi-Fi Handover Request Message", wsc_len);
2573 wpa_hexdump(MSG_DEBUG,
2574 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2576 if (wsc_len < wpabuf_len(wps) - 2) {
2577 wpa_hexdump(MSG_DEBUG,
2578 "WPS: Ignore extra data after WSC attributes",
2579 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2582 wpabuf_set(&msg, pos, wsc_len);
2583 ret = wps_parse_msg(&msg, &attr);
2585 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2586 "Wi-Fi Handover Request Message");
2590 if (attr.oob_dev_password == NULL ||
2591 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2592 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2593 "included in Wi-Fi Handover Request Message");
2598 if (attr.uuid_e == NULL) {
2599 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2600 "Handover Request Message");
2605 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2607 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2608 attr.oob_dev_password, attr.oob_dev_password_len);
2609 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2610 WPS_OOB_PUBKEY_HASH_LEN);
2611 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2612 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2613 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2617 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2618 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2620 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2621 attr.oob_dev_password,
2622 DEV_PW_NFC_CONNECTION_HANDOVER,
2630 #endif /* CONFIG_WPS_NFC */
2633 static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2636 struct os_reltime now;
2638 if (wpa_debug_level > MSG_DEBUG)
2641 if (wpa_s->wps_ap == NULL)
2644 os_get_reltime(&now);
2646 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2647 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2648 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2650 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2651 "tries=%d last_attempt=%d sec ago blacklist=%d",
2652 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2653 ap->last_attempt.sec > 0 ?
2654 (int) now.sec - (int) ap->last_attempt.sec : -1,
2660 static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2665 if (wpa_s->wps_ap == NULL)
2668 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2669 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2670 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2678 static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2679 struct wpa_scan_res *res)
2682 enum wps_ap_info_type type;
2683 struct wps_ap_info *ap;
2686 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2689 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2693 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2695 type = WPS_AP_SEL_REG_OUR;
2697 type = WPS_AP_SEL_REG;
2699 type = WPS_AP_NOT_SEL_REG;
2703 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2705 if (ap->type != type) {
2706 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2707 " changed type %d -> %d",
2708 MAC2STR(res->bssid), ap->type, type);
2710 if (type != WPS_AP_NOT_SEL_REG)
2711 wpa_blacklist_del(wpa_s, ap->bssid);
2716 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2717 sizeof(struct wps_ap_info));
2722 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2723 wpa_s->num_wps_ap++;
2725 os_memset(ap, 0, sizeof(*ap));
2726 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2728 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2729 MAC2STR(ap->bssid), ap->type);
2733 void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2734 struct wpa_scan_results *scan_res)
2738 for (i = 0; i < scan_res->num; i++)
2739 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2741 wpas_wps_dump_ap_info(wpa_s);
2745 void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2747 struct wps_ap_info *ap;
2749 wpa_s->after_wps = 0;
2751 if (!wpa_s->wps_ap_iter)
2753 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2757 os_get_reltime(&ap->last_attempt);