2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "crypto/dh_group5.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/ieee802_11_common.h"
23 #include "common/wpa_common.h"
24 #include "common/wpa_ctrl.h"
25 #include "eap_common/eap_wsc_common.h"
26 #include "eap_peer/eap.h"
27 #include "rsn_supp/wpa.h"
29 #include "wpa_supplicant_i.h"
32 #include "blacklist.h"
37 #include "p2p_supplicant.h"
38 #include "wps_supplicant.h"
41 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
42 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
43 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
45 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
46 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
49 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
51 if (!wpa_s->wps_success &&
52 wpa_s->current_ssid &&
53 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
54 const u8 *bssid = wpa_s->bssid;
55 if (is_zero_ether_addr(bssid))
56 bssid = wpa_s->pending_bssid;
58 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
59 " did not succeed - continue trying to find "
60 "suitable AP", MAC2STR(bssid));
61 wpa_blacklist_add(wpa_s, bssid);
63 wpa_supplicant_deauthenticate(wpa_s,
64 WLAN_REASON_DEAUTH_LEAVING);
65 wpa_s->reassociate = 1;
66 wpa_supplicant_req_scan(wpa_s,
67 wpa_s->blacklist_cleared ? 5 : 0, 0);
68 wpa_s->blacklist_cleared = 0;
72 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
73 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
74 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
76 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
77 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
78 int disabled = wpa_s->current_ssid->disabled;
79 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
80 "try to associate with the received credential");
81 wpa_supplicant_deauthenticate(wpa_s,
82 WLAN_REASON_DEAUTH_LEAVING);
84 wpa_printf(MSG_DEBUG, "WPS: Current network is "
85 "disabled - wait for user to enable");
89 wpa_s->wps_freq = wpa_s->assoc_freq;
90 wpa_s->reassociate = 1;
91 wpa_supplicant_req_scan(wpa_s, 0, 0);
95 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
96 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
97 "for external credential processing");
98 wpas_clear_wps(wpa_s);
99 wpa_supplicant_deauthenticate(wpa_s,
100 WLAN_REASON_DEAUTH_LEAVING);
108 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
109 struct wpa_ssid *ssid,
110 const struct wps_credential *cred)
112 struct wpa_driver_capa capa;
115 struct wpa_ie_data adv;
116 int wpa2 = 0, ccmp = 0;
119 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
120 * case they are configured for mixed mode operation (WPA+WPA2 and
121 * TKIP+CCMP). Try to use scan results to figure out whether the AP
122 * actually supports stronger security and select that if the client
123 * has support for it, too.
126 if (wpa_drv_get_capa(wpa_s, &capa))
127 return; /* Unknown what driver supports */
129 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
131 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
132 "table - use credential as-is");
136 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
138 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
139 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
141 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
144 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
145 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
146 adv.pairwise_cipher & WPA_CIPHER_CCMP)
150 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
151 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
153 * TODO: This could be the initial AP configuration and the
154 * Beacon contents could change shortly. Should request a new
155 * scan and delay addition of the network until the updated
156 * scan results are available.
158 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
159 "support - use credential as-is");
163 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
164 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
165 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
166 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
167 "based on scan results");
168 if (wpa_s->conf->ap_scan == 1)
169 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
171 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
174 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
175 (ssid->proto & WPA_PROTO_WPA) &&
176 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
177 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
178 "based on scan results");
179 if (wpa_s->conf->ap_scan == 1)
180 ssid->proto |= WPA_PROTO_RSN;
182 ssid->proto = WPA_PROTO_RSN;
187 static int wpa_supplicant_wps_cred(void *ctx,
188 const struct wps_credential *cred)
190 struct wpa_supplicant *wpa_s = ctx;
191 struct wpa_ssid *ssid = wpa_s->current_ssid;
196 if ((wpa_s->conf->wps_cred_processing == 1 ||
197 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
198 size_t blen = cred->cred_attr_len * 2 + 1;
199 char *buf = os_malloc(blen);
201 wpa_snprintf_hex(buf, blen,
202 cred->cred_attr, cred->cred_attr_len);
203 wpa_msg(wpa_s, MSG_INFO, "%s%s",
204 WPS_EVENT_CRED_RECEIVED, buf);
208 wpas_notify_wps_credential(wpa_s, cred);
210 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
212 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
213 cred->cred_attr, cred->cred_attr_len);
215 if (wpa_s->conf->wps_cred_processing == 1)
218 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
219 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
221 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
222 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
223 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
224 cred->key, cred->key_len);
225 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
226 MAC2STR(cred->mac_addr));
228 auth_type = cred->auth_type;
229 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
230 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
231 "auth_type into WPA2PSK");
232 auth_type = WPS_AUTH_WPA2PSK;
235 if (auth_type != WPS_AUTH_OPEN &&
236 auth_type != WPS_AUTH_SHARED &&
237 auth_type != WPS_AUTH_WPAPSK &&
238 auth_type != WPS_AUTH_WPA2PSK) {
239 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
240 "unsupported authentication type 0x%x",
245 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
246 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
247 "on the received credential");
248 if (ssid->eap.identity &&
249 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
250 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
251 WSC_ID_REGISTRAR_LEN) == 0)
253 os_free(ssid->eap.identity);
254 ssid->eap.identity = NULL;
255 ssid->eap.identity_len = 0;
256 os_free(ssid->eap.phase1);
257 ssid->eap.phase1 = NULL;
258 os_free(ssid->eap.eap_methods);
259 ssid->eap.eap_methods = NULL;
260 if (!ssid->p2p_group)
263 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
264 "received credential");
265 ssid = wpa_config_add_network(wpa_s->conf);
268 wpas_notify_network_added(wpa_s, ssid);
271 wpa_config_set_network_defaults(ssid);
274 ssid->ssid = os_malloc(cred->ssid_len);
276 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
277 ssid->ssid_len = cred->ssid_len;
280 switch (cred->encr_type) {
284 if (cred->key_len <= 0)
286 if (cred->key_len != 5 && cred->key_len != 13 &&
287 cred->key_len != 10 && cred->key_len != 26) {
288 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
289 "%lu", (unsigned long) cred->key_len);
292 if (cred->key_idx > NUM_WEP_KEYS) {
293 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
298 key_idx = cred->key_idx - 1;
299 if (cred->key_len == 10 || cred->key_len == 26) {
300 if (hexstr2bin((char *) cred->key,
301 ssid->wep_key[key_idx],
302 cred->key_len / 2) < 0) {
303 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
307 ssid->wep_key_len[key_idx] = cred->key_len / 2;
309 os_memcpy(ssid->wep_key[key_idx], cred->key,
311 ssid->wep_key_len[key_idx] = cred->key_len;
313 ssid->wep_tx_keyidx = key_idx;
316 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
319 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
325 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
326 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
328 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
330 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
331 "id=%d - Credentials for an open "
332 "network disabled by default - use "
333 "'select_network %d' to enable",
337 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
339 case WPS_AUTH_SHARED:
340 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
341 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
344 case WPS_AUTH_WPAPSK:
345 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
346 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
347 ssid->proto = WPA_PROTO_WPA;
350 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
351 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
352 ssid->proto = WPA_PROTO_WPA;
355 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
356 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
357 ssid->proto = WPA_PROTO_RSN;
359 case WPS_AUTH_WPA2PSK:
360 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
361 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
362 ssid->proto = WPA_PROTO_RSN;
366 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
367 if (cred->key_len == 2 * PMK_LEN) {
368 if (hexstr2bin((const char *) cred->key, ssid->psk,
370 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
375 ssid->export_keys = 1;
376 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
377 os_free(ssid->passphrase);
378 ssid->passphrase = os_malloc(cred->key_len + 1);
379 if (ssid->passphrase == NULL)
381 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
382 ssid->passphrase[cred->key_len] = '\0';
383 wpa_config_update_psk(ssid);
384 ssid->export_keys = 1;
386 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
388 (unsigned long) cred->key_len);
393 wpas_wps_security_workaround(wpa_s, ssid, cred);
395 #ifndef CONFIG_NO_CONFIG_WRITE
396 if (wpa_s->conf->update_config &&
397 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
398 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
401 #endif /* CONFIG_NO_CONFIG_WRITE */
408 static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
410 struct wpa_supplicant *wpa_s = eloop_ctx;
411 wpas_p2p_notif_pbc_overlap(wpa_s);
413 #endif /* CONFIG_P2P */
416 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
417 struct wps_event_m2d *m2d)
419 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
420 "dev_password_id=%d config_error=%d",
421 m2d->dev_password_id, m2d->config_error);
422 wpas_notify_wps_event_m2d(wpa_s, m2d);
424 if (wpa_s->parent && wpa_s->parent != wpa_s) {
425 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
426 "dev_password_id=%d config_error=%d",
427 m2d->dev_password_id, m2d->config_error);
429 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
431 * Notify P2P from eloop timeout to avoid issues with the
432 * interface getting removed while processing a message.
434 eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
437 #endif /* CONFIG_P2P */
441 static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
442 "No Error", /* WPS_EI_NO_ERROR */
443 "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
444 "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
447 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
448 struct wps_event_fail *fail)
450 if (fail->error_indication > 0 &&
451 fail->error_indication < NUM_WPS_EI_VALUES) {
452 wpa_msg(wpa_s, MSG_INFO,
453 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
454 fail->msg, fail->config_error, fail->error_indication,
455 wps_event_fail_reason[fail->error_indication]);
456 if (wpa_s->parent && wpa_s->parent != wpa_s)
457 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
458 "msg=%d config_error=%d reason=%d (%s)",
459 fail->msg, fail->config_error,
460 fail->error_indication,
461 wps_event_fail_reason[fail->error_indication]);
463 wpa_msg(wpa_s, MSG_INFO,
464 WPS_EVENT_FAIL "msg=%d config_error=%d",
465 fail->msg, fail->config_error);
466 if (wpa_s->parent && wpa_s->parent != wpa_s)
467 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
468 "msg=%d config_error=%d",
469 fail->msg, fail->config_error);
471 wpas_clear_wps(wpa_s);
472 wpas_notify_wps_event_fail(wpa_s, fail);
476 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
478 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
479 wpa_s->wps_success = 1;
480 wpas_notify_wps_event_success(wpa_s);
482 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
483 #endif /* CONFIG_P2P */
487 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
488 struct wps_event_er_ap *ap)
491 char dev_type[WPS_DEV_TYPE_BUFSIZE];
493 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
494 if (ap->pri_dev_type)
495 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
500 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
501 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
502 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
503 ap->friendly_name ? ap->friendly_name : "",
504 ap->manufacturer ? ap->manufacturer : "",
505 ap->model_description ? ap->model_description : "",
506 ap->model_name ? ap->model_name : "",
507 ap->manufacturer_url ? ap->manufacturer_url : "",
508 ap->model_url ? ap->model_url : "");
512 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
513 struct wps_event_er_ap *ap)
516 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
517 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
521 static void wpa_supplicant_wps_event_er_enrollee_add(
522 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
525 char dev_type[WPS_DEV_TYPE_BUFSIZE];
527 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
528 if (enrollee->pri_dev_type)
529 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
534 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
535 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
537 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
538 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
539 enrollee->dev_name ? enrollee->dev_name : "",
540 enrollee->manufacturer ? enrollee->manufacturer : "",
541 enrollee->model_name ? enrollee->model_name : "",
542 enrollee->model_number ? enrollee->model_number : "",
543 enrollee->serial_number ? enrollee->serial_number : "");
547 static void wpa_supplicant_wps_event_er_enrollee_remove(
548 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
551 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
552 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
553 uuid_str, MAC2STR(enrollee->mac_addr));
557 static void wpa_supplicant_wps_event_er_ap_settings(
558 struct wpa_supplicant *wpa_s,
559 struct wps_event_er_ap_settings *ap_settings)
563 const struct wps_credential *cred = ap_settings->cred;
566 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
567 if (cred->key_len >= 8 && cred->key_len <= 64) {
568 os_memcpy(key_str, cred->key, cred->key_len);
569 key_str[cred->key_len] = '\0';
573 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
574 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
575 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
576 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
578 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
579 cred->auth_type, cred->encr_type, key_str);
583 static void wpa_supplicant_wps_event_er_set_sel_reg(
584 struct wpa_supplicant *wpa_s,
585 struct wps_event_er_set_selected_registrar *ev)
589 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
591 case WPS_ER_SET_SEL_REG_START:
592 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
593 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
594 "sel_reg_config_methods=0x%x",
595 uuid_str, ev->sel_reg, ev->dev_passwd_id,
596 ev->sel_reg_config_methods);
598 case WPS_ER_SET_SEL_REG_DONE:
599 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
600 "uuid=%s state=DONE", uuid_str);
602 case WPS_ER_SET_SEL_REG_FAILED:
603 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
604 "uuid=%s state=FAILED", uuid_str);
610 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
611 union wps_event_data *data)
613 struct wpa_supplicant *wpa_s = ctx;
616 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
619 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
622 wpa_supplicant_wps_event_success(wpa_s);
624 case WPS_EV_PWD_AUTH_FAIL:
626 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
627 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
628 #endif /* CONFIG_AP */
630 case WPS_EV_PBC_OVERLAP:
632 case WPS_EV_PBC_TIMEOUT:
634 case WPS_EV_ER_AP_ADD:
635 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
637 case WPS_EV_ER_AP_REMOVE:
638 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
640 case WPS_EV_ER_ENROLLEE_ADD:
641 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
644 case WPS_EV_ER_ENROLLEE_REMOVE:
645 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
648 case WPS_EV_ER_AP_SETTINGS:
649 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
652 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
653 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
660 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
662 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
663 eap_is_wps_pin_enrollee(&ssid->eap))
664 return WPS_REQ_ENROLLEE;
666 return WPS_REQ_REGISTRAR;
670 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
673 struct wpa_ssid *ssid, *remove_ssid = NULL;
675 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
677 /* Remove any existing WPS network from configuration */
678 ssid = wpa_s->conf->ssid;
680 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
681 if (ssid == wpa_s->current_ssid) {
682 wpa_s->current_ssid = NULL;
684 wpas_notify_network_changed(wpa_s);
692 wpas_notify_network_removed(wpa_s, remove_ssid);
693 wpa_config_remove_network(wpa_s->conf, id);
699 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
701 struct wpa_supplicant *wpa_s = eloop_ctx;
702 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
704 wpas_clear_wps(wpa_s);
708 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
709 int registrar, const u8 *bssid)
711 struct wpa_ssid *ssid;
713 ssid = wpa_config_add_network(wpa_s->conf);
716 wpas_notify_network_added(wpa_s, ssid);
717 wpa_config_set_network_defaults(ssid);
719 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
720 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
721 wpa_config_set(ssid, "identity", registrar ?
722 "\"" WSC_ID_REGISTRAR "\"" :
723 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
724 wpas_notify_network_removed(wpa_s, ssid);
725 wpa_config_remove_network(wpa_s->conf, ssid->id);
733 #endif /* CONFIG_P2P */
735 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
739 * Note: With P2P, the SSID may change at the time the WPS
740 * provisioning is started, so better not filter the AP based
741 * on the current SSID in the scan results.
744 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
745 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
749 ssid->ssid = os_malloc(bss->ssid_len);
750 if (ssid->ssid == NULL)
752 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
753 ssid->ssid_len = bss->ssid_len;
754 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
756 ssid->ssid, ssid->ssid_len);
761 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
762 "for the AP; use wildcard");
767 #endif /* CONFIG_P2P */
774 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
775 struct wpa_ssid *selected)
777 struct wpa_ssid *ssid;
779 /* Mark all other networks disabled and trigger reassociation */
780 ssid = wpa_s->conf->ssid;
782 int was_disabled = ssid->disabled;
783 ssid->disabled = ssid != selected;
784 if (was_disabled != ssid->disabled)
785 wpas_notify_network_enabled_changed(wpa_s, ssid);
788 wpa_s->disconnected = 0;
789 wpa_s->reassociate = 1;
790 wpa_s->scan_runs = 0;
791 wpa_s->wps_success = 0;
792 wpa_s->blacklist_cleared = 0;
793 wpa_supplicant_req_scan(wpa_s, 0, 0);
797 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
800 struct wpa_ssid *ssid;
801 wpas_clear_wps(wpa_s);
802 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
806 ssid->p2p_group = p2p_group;
808 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
809 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
811 ssid->ssid_len = wpa_s->go_params->ssid_len;
812 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
814 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
815 "SSID", ssid->ssid, ssid->ssid_len);
818 #endif /* CONFIG_P2P */
819 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
820 if (wpa_s->wps_fragment_size)
821 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
822 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
824 wpas_wps_reassoc(wpa_s, ssid);
829 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
830 const char *pin, int p2p_group, u16 dev_pw_id)
832 struct wpa_ssid *ssid;
834 unsigned int rpin = 0;
836 wpas_clear_wps(wpa_s);
837 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
841 ssid->p2p_group = p2p_group;
843 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
844 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
846 ssid->ssid_len = wpa_s->go_params->ssid_len;
847 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
849 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
850 "SSID", ssid->ssid, ssid->ssid_len);
853 #endif /* CONFIG_P2P */
855 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
858 rpin = wps_generate_pin();
859 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
862 wpa_config_set(ssid, "phase1", val, 0);
863 if (wpa_s->wps_fragment_size)
864 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
865 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
867 wpas_wps_reassoc(wpa_s, ssid);
872 /* Cancel the wps pbc/pin requests */
873 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
876 if (wpa_s->ap_iface) {
877 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
878 return wpa_supplicant_ap_wps_cancel(wpa_s);
880 #endif /* CONFIG_AP */
882 if (wpa_s->wpa_state == WPA_SCANNING) {
883 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
884 wpa_supplicant_cancel_scan(wpa_s);
885 wpas_clear_wps(wpa_s);
886 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
887 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
889 wpa_supplicant_deauthenticate(wpa_s,
890 WLAN_REASON_DEAUTH_LEAVING);
891 wpas_clear_wps(wpa_s);
898 #ifdef CONFIG_WPS_OOB
899 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
900 char *path, char *method, char *name)
902 struct wps_context *wps = wpa_s->wps;
903 struct oob_device_data *oob_dev;
905 oob_dev = wps_get_oob_device(device_type);
908 oob_dev->device_path = path;
909 oob_dev->device_name = name;
910 wps->oob_conf.oob_method = wps_get_oob_method(method);
912 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
914 * Use pre-configured DH keys in order to be able to write the
915 * key hash into the OOB file.
917 wpabuf_free(wps->dh_pubkey);
918 wpabuf_free(wps->dh_privkey);
919 wps->dh_privkey = NULL;
920 wps->dh_pubkey = NULL;
921 dh5_free(wps->dh_ctx);
922 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
923 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
924 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
925 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
926 "Diffie-Hellman handshake");
931 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
932 wpas_clear_wps(wpa_s);
934 if (wps_process_oob(wps, oob_dev, 0) < 0)
937 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
938 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
939 wpas_wps_start_pin(wpa_s, NULL,
940 wpabuf_head(wps->oob_conf.dev_password), 0,
946 #endif /* CONFIG_WPS_OOB */
949 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
950 const char *pin, struct wps_new_ap_settings *settings)
952 struct wpa_ssid *ssid;
959 wpas_clear_wps(wpa_s);
960 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
965 end = pos + sizeof(val);
966 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
967 if (res < 0 || res >= end - pos)
971 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
972 "new_encr=%s new_key=%s",
973 settings->ssid_hex, settings->auth,
974 settings->encr, settings->key_hex);
975 if (res < 0 || res >= end - pos)
979 res = os_snprintf(pos, end - pos, "\"");
980 if (res < 0 || res >= end - pos)
982 wpa_config_set(ssid, "phase1", val, 0);
983 if (wpa_s->wps_fragment_size)
984 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
985 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
987 wpas_wps_reassoc(wpa_s, ssid);
992 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
995 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
996 "STA " MACSTR, MAC2STR(mac_addr));
997 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1005 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1006 const struct wps_device_data *dev)
1008 char uuid[40], txt[400];
1010 char devtype[WPS_DEV_TYPE_BUFSIZE];
1011 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1013 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1014 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1015 " [%s|%s|%s|%s|%s|%s]",
1016 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1017 dev->manufacturer, dev->model_name,
1018 dev->model_number, dev->serial_number,
1019 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1021 if (len > 0 && len < (int) sizeof(txt))
1022 wpa_printf(MSG_INFO, "%s", txt);
1026 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1027 u16 sel_reg_config_methods)
1029 #ifdef CONFIG_WPS_ER
1030 struct wpa_supplicant *wpa_s = ctx;
1032 if (wpa_s->wps_er == NULL)
1034 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1035 "dev_password_id=%u sel_reg_config_methods=0x%x",
1036 sel_reg, dev_passwd_id, sel_reg_config_methods);
1037 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1038 sel_reg_config_methods);
1039 #endif /* CONFIG_WPS_ER */
1043 static u16 wps_fix_config_methods(u16 config_methods)
1046 if ((config_methods &
1047 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1048 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1049 wpa_printf(MSG_INFO, "WPS: Converting display to "
1050 "virtual_display for WPS 2.0 compliance");
1051 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1053 if ((config_methods &
1054 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1055 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1056 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1057 "virtual_push_button for WPS 2.0 compliance");
1058 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1060 #endif /* CONFIG_WPS2 */
1062 return config_methods;
1066 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1067 struct wps_context *wps)
1069 wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
1070 if (is_nil_uuid(wpa_s->conf->uuid)) {
1071 struct wpa_supplicant *first;
1072 first = wpa_s->global->ifaces;
1073 while (first && first->next)
1074 first = first->next;
1075 if (first && first != wpa_s) {
1076 os_memcpy(wps->uuid, wpa_s->global->ifaces->wps->uuid,
1078 wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
1079 "interface", wps->uuid, WPS_UUID_LEN);
1081 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1082 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1083 "address", wps->uuid, WPS_UUID_LEN);
1086 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1087 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
1088 wps->uuid, WPS_UUID_LEN);
1093 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1095 struct wps_context *wps;
1096 struct wps_registrar_config rcfg;
1099 wps = os_zalloc(sizeof(*wps));
1103 wps->cred_cb = wpa_supplicant_wps_cred;
1104 wps->event_cb = wpa_supplicant_wps_event;
1105 wps->cb_ctx = wpa_s;
1107 wps->dev.device_name = wpa_s->conf->device_name;
1108 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1109 wps->dev.model_name = wpa_s->conf->model_name;
1110 wps->dev.model_number = wpa_s->conf->model_number;
1111 wps->dev.serial_number = wpa_s->conf->serial_number;
1112 wps->config_methods =
1113 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1114 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1115 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1116 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1117 "methods are not allowed at the same time");
1121 wps->config_methods = wps_fix_config_methods(wps->config_methods);
1122 if (wpa_s->conf->device_type &&
1123 wps_dev_type_str2bin(wpa_s->conf->device_type,
1124 wps->dev.pri_dev_type) < 0) {
1125 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1130 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
1131 if (wpa_s->conf->sec_device_type[i] == NULL)
1133 if (wps_dev_type_str2bin(
1134 wpa_s->conf->sec_device_type[i],
1135 wps->dev.sec_dev_type[wps->dev.num_sec_dev_types])
1137 wpa_printf(MSG_ERROR, "WPS: Invalid sec_device_type");
1140 wps->dev.num_sec_dev_types++;
1141 if (wps->dev.num_sec_dev_types == WPS_SEC_DEVICE_TYPES)
1145 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1146 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
1147 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1148 wpas_wps_set_uuid(wpa_s, wps);
1150 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1151 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1153 os_memset(&rcfg, 0, sizeof(rcfg));
1154 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1155 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1156 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1157 rcfg.cb_ctx = wpa_s;
1159 wps->registrar = wps_registrar_init(wps, &rcfg);
1160 if (wps->registrar == NULL) {
1161 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1172 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1174 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1176 if (wpa_s->wps == NULL)
1179 #ifdef CONFIG_WPS_ER
1180 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1181 wpa_s->wps_er = NULL;
1182 #endif /* CONFIG_WPS_ER */
1184 wps_registrar_deinit(wpa_s->wps->registrar);
1185 wpabuf_free(wpa_s->wps->dh_pubkey);
1186 wpabuf_free(wpa_s->wps->dh_privkey);
1187 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1188 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1189 os_free(wpa_s->wps->network_key);
1190 os_free(wpa_s->wps);
1195 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1196 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1198 struct wpabuf *wps_ie;
1200 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1203 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1204 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1206 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1210 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1211 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1212 "without active PBC Registrar");
1213 wpabuf_free(wps_ie);
1217 /* TODO: overlap detection */
1218 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1220 wpabuf_free(wps_ie);
1224 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1226 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1231 * Start with WPS APs that advertise our address as an
1232 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1233 * allow any WPS AP after couple of scans since some APs do not
1234 * set Selected Registrar attribute properly when using
1235 * external Registrar.
1237 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1238 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1239 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1240 "without active PIN Registrar");
1241 wpabuf_free(wps_ie);
1244 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1246 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1247 "(Authorized MAC or Active PIN)");
1249 wpabuf_free(wps_ie);
1254 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1255 wpabuf_free(wps_ie);
1263 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1264 struct wpa_ssid *ssid,
1265 struct wpa_scan_res *bss)
1267 struct wpabuf *wps_ie = NULL;
1270 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1271 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1272 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1273 /* allow wildcard SSID for WPS PBC */
1276 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1277 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1279 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1280 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1281 /* allow wildcard SSID for WPS PIN */
1286 if (!ret && ssid->bssid_set &&
1287 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1288 /* allow wildcard SSID due to hardcoded BSSID match */
1292 #ifdef CONFIG_WPS_STRICT
1294 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1297 if (bss->beacon_ie_len) {
1298 struct wpabuf *bcn_wps;
1299 bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1300 bss, WPS_IE_VENDOR_TYPE);
1301 if (bcn_wps == NULL) {
1302 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1303 "missing from AP Beacon");
1306 if (wps_validate_beacon(wps_ie) < 0)
1308 wpabuf_free(bcn_wps);
1312 #endif /* CONFIG_WPS_STRICT */
1314 wpabuf_free(wps_ie);
1320 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1321 struct wpa_bss *selected, struct wpa_ssid *ssid)
1323 const u8 *sel_uuid, *uuid;
1324 struct wpabuf *wps_ie;
1326 struct wpa_bss *bss;
1328 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1331 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1332 "present in scan results; selected BSSID " MACSTR,
1333 MAC2STR(selected->bssid));
1335 /* Make sure that only one AP is in active PBC mode */
1336 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1338 sel_uuid = wps_get_uuid_e(wps_ie);
1339 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1340 sel_uuid, UUID_LEN);
1342 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1347 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1349 if (bss == selected)
1351 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1354 if (!wps_is_selected_pbc_registrar(ie)) {
1358 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1359 MACSTR, MAC2STR(bss->bssid));
1360 uuid = wps_get_uuid_e(ie);
1361 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1363 if (sel_uuid == NULL || uuid == NULL ||
1364 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1365 ret = 1; /* PBC overlap */
1366 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1367 MACSTR " and " MACSTR,
1368 MAC2STR(selected->bssid),
1369 MAC2STR(bss->bssid));
1374 /* TODO: verify that this is reasonable dual-band situation */
1379 wpabuf_free(wps_ie);
1385 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1387 struct wpa_bss *bss;
1388 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1390 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1393 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1395 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1398 if (wps_is_selected_pbc_registrar(ie))
1400 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1402 else if (wps_is_selected_pin_registrar(ie))
1410 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1412 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1414 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1416 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1420 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1422 struct wpa_ssid *ssid;
1424 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1425 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1433 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1436 struct wpabuf *wps_ie;
1439 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1443 ret = wps_attr_text(wps_ie, buf, end);
1444 wpabuf_free(wps_ie);
1449 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1451 #ifdef CONFIG_WPS_ER
1452 if (wpa_s->wps_er) {
1453 wps_er_refresh(wpa_s->wps_er);
1456 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1457 if (wpa_s->wps_er == NULL)
1460 #else /* CONFIG_WPS_ER */
1462 #endif /* CONFIG_WPS_ER */
1466 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1468 #ifdef CONFIG_WPS_ER
1469 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1470 wpa_s->wps_er = NULL;
1471 #endif /* CONFIG_WPS_ER */
1476 #ifdef CONFIG_WPS_ER
1477 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1478 const char *uuid, const char *pin)
1483 if (os_strcmp(uuid, "any") == 0)
1485 else if (uuid_str2bin(uuid, u))
1487 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1489 (const u8 *) pin, os_strlen(pin), 300);
1493 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1497 if (uuid_str2bin(uuid, u))
1499 return wps_er_pbc(wpa_s->wps_er, u);
1503 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1508 if (uuid_str2bin(uuid, u))
1510 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1515 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1519 struct wpa_ssid *ssid;
1520 struct wps_credential cred;
1522 if (uuid_str2bin(uuid, u))
1524 ssid = wpa_config_get_network(wpa_s->conf, id);
1525 if (ssid == NULL || ssid->ssid == NULL)
1528 os_memset(&cred, 0, sizeof(cred));
1529 if (ssid->ssid_len > 32)
1531 os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
1532 cred.ssid_len = ssid->ssid_len;
1533 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1534 cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1535 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1536 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1537 cred.encr_type = WPS_ENCR_AES;
1539 cred.encr_type = WPS_ENCR_TKIP;
1540 if (ssid->passphrase) {
1541 cred.key_len = os_strlen(ssid->passphrase);
1542 if (cred.key_len >= 64)
1544 os_memcpy(cred.key, ssid->passphrase, cred.key_len);
1545 } else if (ssid->psk_set) {
1547 os_memcpy(cred.key, ssid->psk, 32);
1551 cred.auth_type = WPS_AUTH_OPEN;
1552 cred.encr_type = WPS_ENCR_NONE;
1554 return wps_er_set_config(wpa_s->wps_er, u, &cred);
1558 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1559 const char *pin, struct wps_new_ap_settings *settings)
1562 struct wps_credential cred;
1565 if (uuid_str2bin(uuid, u))
1567 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1568 settings->encr == NULL || settings->key_hex == NULL)
1571 os_memset(&cred, 0, sizeof(cred));
1572 len = os_strlen(settings->ssid_hex);
1573 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1574 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1576 cred.ssid_len = len / 2;
1578 len = os_strlen(settings->key_hex);
1579 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1580 hexstr2bin(settings->key_hex, cred.key, len / 2))
1582 cred.key_len = len / 2;
1584 if (os_strcmp(settings->auth, "OPEN") == 0)
1585 cred.auth_type = WPS_AUTH_OPEN;
1586 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1587 cred.auth_type = WPS_AUTH_WPAPSK;
1588 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1589 cred.auth_type = WPS_AUTH_WPA2PSK;
1593 if (os_strcmp(settings->encr, "NONE") == 0)
1594 cred.encr_type = WPS_ENCR_NONE;
1595 else if (os_strcmp(settings->encr, "WEP") == 0)
1596 cred.encr_type = WPS_ENCR_WEP;
1597 else if (os_strcmp(settings->encr, "TKIP") == 0)
1598 cred.encr_type = WPS_ENCR_TKIP;
1599 else if (os_strcmp(settings->encr, "CCMP") == 0)
1600 cred.encr_type = WPS_ENCR_AES;
1604 return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1605 os_strlen(pin), &cred);
1609 static int callbacks_pending = 0;
1611 static void wpas_wps_terminate_cb(void *ctx)
1613 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1614 if (--callbacks_pending <= 0)
1617 #endif /* CONFIG_WPS_ER */
1620 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1622 #ifdef CONFIG_WPS_ER
1623 if (wpa_s->wps_er) {
1624 callbacks_pending++;
1625 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1626 wpa_s->wps_er = NULL;
1629 #endif /* CONFIG_WPS_ER */
1634 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1636 struct wpa_ssid *ssid;
1638 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1639 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1647 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1649 struct wps_context *wps = wpa_s->wps;
1654 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1655 wps->config_methods = wps_config_methods_str2bin(
1656 wpa_s->conf->config_methods);
1657 if ((wps->config_methods &
1658 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1659 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1660 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1661 "config methods are not allowed at the "
1663 wps->config_methods &= ~WPS_CONFIG_LABEL;
1666 wps->config_methods = wps_fix_config_methods(wps->config_methods);
1668 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
1669 if (wpa_s->conf->device_type &&
1670 wps_dev_type_str2bin(wpa_s->conf->device_type,
1671 wps->dev.pri_dev_type) < 0)
1672 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1675 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
1678 wps->dev.num_sec_dev_types = 0;
1680 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
1681 if (wpa_s->conf->sec_device_type[i] == NULL)
1683 if (wps_dev_type_str2bin(
1684 wpa_s->conf->sec_device_type[i],
1685 wps->dev.sec_dev_type[
1686 wps->dev.num_sec_dev_types]) < 0) {
1687 wpa_printf(MSG_ERROR,
1688 "WPS: Invalid sec_device_type");
1691 wps->dev.num_sec_dev_types++;
1692 if (wps->dev.num_sec_dev_types == WPS_SEC_DEVICE_TYPES)
1697 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1698 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1700 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
1701 wpas_wps_set_uuid(wpa_s, wps);
1703 if (wpa_s->conf->changed_parameters &
1704 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1705 /* Update pointers to make sure they refer current values */
1706 wps->dev.device_name = wpa_s->conf->device_name;
1707 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1708 wps->dev.model_name = wpa_s->conf->model_name;
1709 wps->dev.model_number = wpa_s->conf->model_number;
1710 wps->dev.serial_number = wpa_s->conf->serial_number;