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;
784 * In case the network object corresponds to a persistent group
785 * then do not send out network disabled signal. In addition,
786 * do not change disabled status of persistent network objects
787 * from 2 to 1 should we connect to another network.
789 if (was_disabled != 2) {
790 ssid->disabled = ssid != selected;
791 if (was_disabled != ssid->disabled)
792 wpas_notify_network_enabled_changed(wpa_s,
797 wpa_s->disconnected = 0;
798 wpa_s->reassociate = 1;
799 wpa_s->scan_runs = 0;
800 wpa_s->wps_success = 0;
801 wpa_s->blacklist_cleared = 0;
802 wpa_supplicant_req_scan(wpa_s, 0, 0);
806 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
809 struct wpa_ssid *ssid;
810 wpas_clear_wps(wpa_s);
811 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
815 ssid->p2p_group = p2p_group;
817 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
818 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
820 ssid->ssid_len = wpa_s->go_params->ssid_len;
821 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
823 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
824 "SSID", ssid->ssid, ssid->ssid_len);
827 #endif /* CONFIG_P2P */
828 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
829 if (wpa_s->wps_fragment_size)
830 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
831 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
833 wpas_wps_reassoc(wpa_s, ssid);
838 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
839 const char *pin, int p2p_group, u16 dev_pw_id)
841 struct wpa_ssid *ssid;
843 unsigned int rpin = 0;
845 wpas_clear_wps(wpa_s);
846 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
850 ssid->p2p_group = p2p_group;
852 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
853 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
855 ssid->ssid_len = wpa_s->go_params->ssid_len;
856 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
858 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
859 "SSID", ssid->ssid, ssid->ssid_len);
862 #endif /* CONFIG_P2P */
864 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
867 rpin = wps_generate_pin();
868 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
871 wpa_config_set(ssid, "phase1", val, 0);
872 if (wpa_s->wps_fragment_size)
873 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
874 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
876 wpas_wps_reassoc(wpa_s, ssid);
881 /* Cancel the wps pbc/pin requests */
882 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
885 if (wpa_s->ap_iface) {
886 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
887 return wpa_supplicant_ap_wps_cancel(wpa_s);
889 #endif /* CONFIG_AP */
891 if (wpa_s->wpa_state == WPA_SCANNING) {
892 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
893 wpa_supplicant_cancel_scan(wpa_s);
894 wpas_clear_wps(wpa_s);
895 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
896 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
898 wpa_supplicant_deauthenticate(wpa_s,
899 WLAN_REASON_DEAUTH_LEAVING);
900 wpas_clear_wps(wpa_s);
907 #ifdef CONFIG_WPS_OOB
908 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
909 char *path, char *method, char *name)
911 struct wps_context *wps = wpa_s->wps;
912 struct oob_device_data *oob_dev;
914 oob_dev = wps_get_oob_device(device_type);
917 oob_dev->device_path = path;
918 oob_dev->device_name = name;
919 wps->oob_conf.oob_method = wps_get_oob_method(method);
921 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
923 * Use pre-configured DH keys in order to be able to write the
924 * key hash into the OOB file.
926 wpabuf_free(wps->dh_pubkey);
927 wpabuf_free(wps->dh_privkey);
928 wps->dh_privkey = NULL;
929 wps->dh_pubkey = NULL;
930 dh5_free(wps->dh_ctx);
931 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
932 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
933 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
934 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
935 "Diffie-Hellman handshake");
940 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
941 wpas_clear_wps(wpa_s);
943 if (wps_process_oob(wps, oob_dev, 0) < 0)
946 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
947 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
948 wpas_wps_start_pin(wpa_s, NULL,
949 wpabuf_head(wps->oob_conf.dev_password), 0,
955 #endif /* CONFIG_WPS_OOB */
958 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
959 const char *pin, struct wps_new_ap_settings *settings)
961 struct wpa_ssid *ssid;
968 wpas_clear_wps(wpa_s);
969 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
974 end = pos + sizeof(val);
975 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
976 if (res < 0 || res >= end - pos)
980 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
981 "new_encr=%s new_key=%s",
982 settings->ssid_hex, settings->auth,
983 settings->encr, settings->key_hex);
984 if (res < 0 || res >= end - pos)
988 res = os_snprintf(pos, end - pos, "\"");
989 if (res < 0 || res >= end - pos)
991 wpa_config_set(ssid, "phase1", val, 0);
992 if (wpa_s->wps_fragment_size)
993 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
994 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
996 wpas_wps_reassoc(wpa_s, ssid);
1001 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
1004 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
1005 "STA " MACSTR, MAC2STR(mac_addr));
1006 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1014 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1015 const struct wps_device_data *dev)
1017 char uuid[40], txt[400];
1019 char devtype[WPS_DEV_TYPE_BUFSIZE];
1020 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1022 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1023 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1024 " [%s|%s|%s|%s|%s|%s]",
1025 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1026 dev->manufacturer, dev->model_name,
1027 dev->model_number, dev->serial_number,
1028 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1030 if (len > 0 && len < (int) sizeof(txt))
1031 wpa_printf(MSG_INFO, "%s", txt);
1035 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1036 u16 sel_reg_config_methods)
1038 #ifdef CONFIG_WPS_ER
1039 struct wpa_supplicant *wpa_s = ctx;
1041 if (wpa_s->wps_er == NULL)
1043 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1044 "dev_password_id=%u sel_reg_config_methods=0x%x",
1045 sel_reg, dev_passwd_id, sel_reg_config_methods);
1046 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1047 sel_reg_config_methods);
1048 #endif /* CONFIG_WPS_ER */
1052 static u16 wps_fix_config_methods(u16 config_methods)
1055 if ((config_methods &
1056 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1057 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1058 wpa_printf(MSG_INFO, "WPS: Converting display to "
1059 "virtual_display for WPS 2.0 compliance");
1060 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1062 if ((config_methods &
1063 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1064 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1065 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1066 "virtual_push_button for WPS 2.0 compliance");
1067 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1069 #endif /* CONFIG_WPS2 */
1071 return config_methods;
1075 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1076 struct wps_context *wps)
1078 wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
1079 if (is_nil_uuid(wpa_s->conf->uuid)) {
1080 struct wpa_supplicant *first;
1081 first = wpa_s->global->ifaces;
1082 while (first && first->next)
1083 first = first->next;
1084 if (first && first != wpa_s) {
1085 os_memcpy(wps->uuid, wpa_s->global->ifaces->wps->uuid,
1087 wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
1088 "interface", wps->uuid, WPS_UUID_LEN);
1090 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1091 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1092 "address", wps->uuid, WPS_UUID_LEN);
1095 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1096 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
1097 wps->uuid, WPS_UUID_LEN);
1102 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1104 struct wps_context *wps;
1105 struct wps_registrar_config rcfg;
1107 wps = os_zalloc(sizeof(*wps));
1111 wps->cred_cb = wpa_supplicant_wps_cred;
1112 wps->event_cb = wpa_supplicant_wps_event;
1113 wps->cb_ctx = wpa_s;
1115 wps->dev.device_name = wpa_s->conf->device_name;
1116 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1117 wps->dev.model_name = wpa_s->conf->model_name;
1118 wps->dev.model_number = wpa_s->conf->model_number;
1119 wps->dev.serial_number = wpa_s->conf->serial_number;
1120 wps->config_methods =
1121 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1122 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1123 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1124 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1125 "methods are not allowed at the same time");
1129 wps->config_methods = wps_fix_config_methods(wps->config_methods);
1130 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1133 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1134 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1135 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1137 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1138 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
1139 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1140 wpas_wps_set_uuid(wpa_s, wps);
1142 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1143 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1145 os_memset(&rcfg, 0, sizeof(rcfg));
1146 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1147 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1148 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1149 rcfg.cb_ctx = wpa_s;
1151 wps->registrar = wps_registrar_init(wps, &rcfg);
1152 if (wps->registrar == NULL) {
1153 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1164 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1166 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1168 if (wpa_s->wps == NULL)
1171 #ifdef CONFIG_WPS_ER
1172 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1173 wpa_s->wps_er = NULL;
1174 #endif /* CONFIG_WPS_ER */
1176 wps_registrar_deinit(wpa_s->wps->registrar);
1177 wpabuf_free(wpa_s->wps->dh_pubkey);
1178 wpabuf_free(wpa_s->wps->dh_privkey);
1179 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1180 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1181 os_free(wpa_s->wps->network_key);
1182 os_free(wpa_s->wps);
1187 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1188 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1190 struct wpabuf *wps_ie;
1192 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1195 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1196 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1198 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1202 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1203 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1204 "without active PBC Registrar");
1205 wpabuf_free(wps_ie);
1209 /* TODO: overlap detection */
1210 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1212 wpabuf_free(wps_ie);
1216 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1218 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1223 * Start with WPS APs that advertise our address as an
1224 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1225 * allow any WPS AP after couple of scans since some APs do not
1226 * set Selected Registrar attribute properly when using
1227 * external Registrar.
1229 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1230 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1231 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1232 "without active PIN Registrar");
1233 wpabuf_free(wps_ie);
1236 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1238 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1239 "(Authorized MAC or Active PIN)");
1241 wpabuf_free(wps_ie);
1246 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1247 wpabuf_free(wps_ie);
1255 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1256 struct wpa_ssid *ssid,
1257 struct wpa_scan_res *bss)
1259 struct wpabuf *wps_ie = NULL;
1262 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1263 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1264 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1265 /* allow wildcard SSID for WPS PBC */
1268 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1269 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1271 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1272 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1273 /* allow wildcard SSID for WPS PIN */
1278 if (!ret && ssid->bssid_set &&
1279 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1280 /* allow wildcard SSID due to hardcoded BSSID match */
1284 #ifdef CONFIG_WPS_STRICT
1286 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1289 if (bss->beacon_ie_len) {
1290 struct wpabuf *bcn_wps;
1291 bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1292 bss, WPS_IE_VENDOR_TYPE);
1293 if (bcn_wps == NULL) {
1294 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1295 "missing from AP Beacon");
1298 if (wps_validate_beacon(wps_ie) < 0)
1300 wpabuf_free(bcn_wps);
1304 #endif /* CONFIG_WPS_STRICT */
1306 wpabuf_free(wps_ie);
1312 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1313 struct wpa_bss *selected, struct wpa_ssid *ssid)
1315 const u8 *sel_uuid, *uuid;
1316 struct wpabuf *wps_ie;
1318 struct wpa_bss *bss;
1320 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1323 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1324 "present in scan results; selected BSSID " MACSTR,
1325 MAC2STR(selected->bssid));
1327 /* Make sure that only one AP is in active PBC mode */
1328 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1330 sel_uuid = wps_get_uuid_e(wps_ie);
1331 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1332 sel_uuid, UUID_LEN);
1334 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1339 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1341 if (bss == selected)
1343 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1346 if (!wps_is_selected_pbc_registrar(ie)) {
1350 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1351 MACSTR, MAC2STR(bss->bssid));
1352 uuid = wps_get_uuid_e(ie);
1353 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1355 if (sel_uuid == NULL || uuid == NULL ||
1356 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1357 ret = 1; /* PBC overlap */
1358 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1359 MACSTR " and " MACSTR,
1360 MAC2STR(selected->bssid),
1361 MAC2STR(bss->bssid));
1366 /* TODO: verify that this is reasonable dual-band situation */
1371 wpabuf_free(wps_ie);
1377 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1379 struct wpa_bss *bss;
1380 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1382 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1385 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1387 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1390 if (wps_is_selected_pbc_registrar(ie))
1392 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1394 else if (wps_is_selected_pin_registrar(ie))
1402 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1404 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1406 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1408 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1412 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1414 struct wpa_ssid *ssid;
1416 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1417 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1425 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1428 struct wpabuf *wps_ie;
1431 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1435 ret = wps_attr_text(wps_ie, buf, end);
1436 wpabuf_free(wps_ie);
1441 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1443 #ifdef CONFIG_WPS_ER
1444 if (wpa_s->wps_er) {
1445 wps_er_refresh(wpa_s->wps_er);
1448 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1449 if (wpa_s->wps_er == NULL)
1452 #else /* CONFIG_WPS_ER */
1454 #endif /* CONFIG_WPS_ER */
1458 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1460 #ifdef CONFIG_WPS_ER
1461 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1462 wpa_s->wps_er = NULL;
1463 #endif /* CONFIG_WPS_ER */
1468 #ifdef CONFIG_WPS_ER
1469 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1470 const char *uuid, const char *pin)
1475 if (os_strcmp(uuid, "any") == 0)
1477 else if (uuid_str2bin(uuid, u))
1479 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1481 (const u8 *) pin, os_strlen(pin), 300);
1485 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1489 if (uuid_str2bin(uuid, u))
1491 return wps_er_pbc(wpa_s->wps_er, u);
1495 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1500 if (uuid_str2bin(uuid, u))
1502 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1507 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1511 struct wpa_ssid *ssid;
1512 struct wps_credential cred;
1514 if (uuid_str2bin(uuid, u))
1516 ssid = wpa_config_get_network(wpa_s->conf, id);
1517 if (ssid == NULL || ssid->ssid == NULL)
1520 os_memset(&cred, 0, sizeof(cred));
1521 if (ssid->ssid_len > 32)
1523 os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
1524 cred.ssid_len = ssid->ssid_len;
1525 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1526 cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1527 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1528 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1529 cred.encr_type = WPS_ENCR_AES;
1531 cred.encr_type = WPS_ENCR_TKIP;
1532 if (ssid->passphrase) {
1533 cred.key_len = os_strlen(ssid->passphrase);
1534 if (cred.key_len >= 64)
1536 os_memcpy(cred.key, ssid->passphrase, cred.key_len);
1537 } else if (ssid->psk_set) {
1539 os_memcpy(cred.key, ssid->psk, 32);
1543 cred.auth_type = WPS_AUTH_OPEN;
1544 cred.encr_type = WPS_ENCR_NONE;
1546 return wps_er_set_config(wpa_s->wps_er, u, &cred);
1550 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1551 const char *pin, struct wps_new_ap_settings *settings)
1554 struct wps_credential cred;
1557 if (uuid_str2bin(uuid, u))
1559 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1560 settings->encr == NULL || settings->key_hex == NULL)
1563 os_memset(&cred, 0, sizeof(cred));
1564 len = os_strlen(settings->ssid_hex);
1565 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1566 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1568 cred.ssid_len = len / 2;
1570 len = os_strlen(settings->key_hex);
1571 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1572 hexstr2bin(settings->key_hex, cred.key, len / 2))
1574 cred.key_len = len / 2;
1576 if (os_strcmp(settings->auth, "OPEN") == 0)
1577 cred.auth_type = WPS_AUTH_OPEN;
1578 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1579 cred.auth_type = WPS_AUTH_WPAPSK;
1580 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1581 cred.auth_type = WPS_AUTH_WPA2PSK;
1585 if (os_strcmp(settings->encr, "NONE") == 0)
1586 cred.encr_type = WPS_ENCR_NONE;
1587 else if (os_strcmp(settings->encr, "WEP") == 0)
1588 cred.encr_type = WPS_ENCR_WEP;
1589 else if (os_strcmp(settings->encr, "TKIP") == 0)
1590 cred.encr_type = WPS_ENCR_TKIP;
1591 else if (os_strcmp(settings->encr, "CCMP") == 0)
1592 cred.encr_type = WPS_ENCR_AES;
1596 return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1597 os_strlen(pin), &cred);
1601 static int callbacks_pending = 0;
1603 static void wpas_wps_terminate_cb(void *ctx)
1605 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1606 if (--callbacks_pending <= 0)
1609 #endif /* CONFIG_WPS_ER */
1612 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1614 #ifdef CONFIG_WPS_ER
1615 if (wpa_s->wps_er) {
1616 callbacks_pending++;
1617 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1618 wpa_s->wps_er = NULL;
1621 #endif /* CONFIG_WPS_ER */
1626 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1628 struct wpa_ssid *ssid;
1630 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1631 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1639 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1641 struct wps_context *wps = wpa_s->wps;
1646 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1647 wps->config_methods = wps_config_methods_str2bin(
1648 wpa_s->conf->config_methods);
1649 if ((wps->config_methods &
1650 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1651 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1652 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1653 "config methods are not allowed at the "
1655 wps->config_methods &= ~WPS_CONFIG_LABEL;
1658 wps->config_methods = wps_fix_config_methods(wps->config_methods);
1660 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
1661 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1664 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
1665 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1666 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1667 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
1670 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1671 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1673 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
1674 wpas_wps_set_uuid(wpa_s, wps);
1676 if (wpa_s->conf->changed_parameters &
1677 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1678 /* Update pointers to make sure they refer current values */
1679 wps->dev.device_name = wpa_s->conf->device_name;
1680 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1681 wps->dev.model_name = wpa_s->conf->model_name;
1682 wps->dev.model_number = wpa_s->conf->model_number;
1683 wps->dev.serial_number = wpa_s->conf->serial_number;