2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008, 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.
18 #include "ieee802_11_defs.h"
19 #include "wpa_common.h"
21 #include "eap_peer/eap.h"
22 #include "wpa_supplicant_i.h"
27 #include "ctrl_iface_dbus.h"
28 #include "eap_common/eap_wsc_common.h"
29 #include "blacklist.h"
31 #include "wps_supplicant.h"
32 #include "dh_groups.h"
34 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
36 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
37 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
40 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
42 if (!wpa_s->wps_success &&
43 wpa_s->current_ssid &&
44 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
45 const u8 *bssid = wpa_s->bssid;
46 if (is_zero_ether_addr(bssid))
47 bssid = wpa_s->pending_bssid;
49 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
50 " did not succeed - continue trying to find "
51 "suitable AP", MAC2STR(bssid));
52 wpa_blacklist_add(wpa_s, bssid);
54 wpa_supplicant_deauthenticate(wpa_s,
55 WLAN_REASON_DEAUTH_LEAVING);
56 wpa_s->reassociate = 1;
57 wpa_supplicant_req_scan(wpa_s,
58 wpa_s->blacklist_cleared ? 5 : 0, 0);
59 wpa_s->blacklist_cleared = 0;
63 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
65 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
66 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
67 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
68 "try to associate with the received credential");
69 wpa_supplicant_deauthenticate(wpa_s,
70 WLAN_REASON_DEAUTH_LEAVING);
71 wpa_s->reassociate = 1;
72 wpa_supplicant_req_scan(wpa_s, 0, 0);
76 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
77 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
78 "for external credential processing");
79 wpas_clear_wps(wpa_s);
80 wpa_supplicant_deauthenticate(wpa_s,
81 WLAN_REASON_DEAUTH_LEAVING);
89 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
90 struct wpa_ssid *ssid,
91 const struct wps_credential *cred)
93 struct wpa_driver_capa capa;
95 struct wpa_scan_res *bss;
97 struct wpa_ie_data adv;
98 int wpa2 = 0, ccmp = 0;
101 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
102 * case they are configured for mixed mode operation (WPA+WPA2 and
103 * TKIP+CCMP). Try to use scan results to figure out whether the AP
104 * actually supports stronger security and select that if the client
105 * has support for it, too.
108 if (wpa_drv_get_capa(wpa_s, &capa))
109 return; /* Unknown what driver supports */
111 if (wpa_supplicant_get_scan_results(wpa_s) || wpa_s->scan_res == NULL)
112 return; /* Could not get scan results for checking advertised
115 for (i = 0; i < wpa_s->scan_res->num; i++) {
116 bss = wpa_s->scan_res->res[i];
117 if (os_memcmp(bss->bssid, cred->mac_addr, ETH_ALEN) != 0)
119 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
122 if (ie[1] != ssid->ssid_len || ssid->ssid == NULL ||
123 os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) != 0)
126 wpa_printf(MSG_DEBUG, "WPS: AP found from scan results");
130 if (i == wpa_s->scan_res->num) {
131 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from scan "
132 "results - use credential as-is");
136 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
137 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
139 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
142 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
143 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
144 adv.pairwise_cipher & WPA_CIPHER_CCMP)
148 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
149 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
151 * TODO: This could be the initial AP configuration and the
152 * Beacon contents could change shortly. Should request a new
153 * scan and delay addition of the network until the updated
154 * scan results are available.
156 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
157 "support - use credential as-is");
161 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
162 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
163 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
164 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
165 "based on scan results");
166 if (wpa_s->conf->ap_scan == 1)
167 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
169 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
172 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
173 (ssid->proto & WPA_PROTO_WPA) &&
174 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
175 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
176 "based on scan results");
177 if (wpa_s->conf->ap_scan == 1)
178 ssid->proto |= WPA_PROTO_RSN;
180 ssid->proto = WPA_PROTO_RSN;
185 static int wpa_supplicant_wps_cred(void *ctx,
186 const struct wps_credential *cred)
188 struct wpa_supplicant *wpa_s = ctx;
189 struct wpa_ssid *ssid = wpa_s->current_ssid;
192 if ((wpa_s->conf->wps_cred_processing == 1 ||
193 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
194 size_t blen = cred->cred_attr_len * 2 + 1;
195 char *buf = os_malloc(blen);
197 wpa_snprintf_hex(buf, blen,
198 cred->cred_attr, cred->cred_attr_len);
199 wpa_msg(wpa_s, MSG_INFO, "%s%s",
200 WPS_EVENT_CRED_RECEIVED, buf);
203 wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
205 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
207 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
208 cred->cred_attr, cred->cred_attr_len);
210 if (wpa_s->conf->wps_cred_processing == 1)
213 if (cred->auth_type != WPS_AUTH_OPEN &&
214 cred->auth_type != WPS_AUTH_SHARED &&
215 cred->auth_type != WPS_AUTH_WPAPSK &&
216 cred->auth_type != WPS_AUTH_WPA2PSK) {
217 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
218 "unsupported authentication type %d",
223 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
224 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
225 "on the received credential");
226 os_free(ssid->eap.identity);
227 ssid->eap.identity = NULL;
228 ssid->eap.identity_len = 0;
229 os_free(ssid->eap.phase1);
230 ssid->eap.phase1 = NULL;
231 os_free(ssid->eap.eap_methods);
232 ssid->eap.eap_methods = NULL;
234 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
235 "received credential");
236 ssid = wpa_config_add_network(wpa_s->conf);
241 wpa_config_set_network_defaults(ssid);
244 ssid->ssid = os_malloc(cred->ssid_len);
246 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
247 ssid->ssid_len = cred->ssid_len;
250 switch (cred->encr_type) {
254 if (cred->key_len <= 0)
256 if (cred->key_len != 5 && cred->key_len != 13 &&
257 cred->key_len != 10 && cred->key_len != 26) {
258 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
259 "%lu", (unsigned long) cred->key_len);
262 if (cred->key_idx > NUM_WEP_KEYS) {
263 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
268 key_idx = cred->key_idx - 1;
269 if (cred->key_len == 10 || cred->key_len == 26) {
270 if (hexstr2bin((char *) cred->key,
271 ssid->wep_key[key_idx],
272 cred->key_len / 2) < 0) {
273 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
277 ssid->wep_key_len[key_idx] = cred->key_len / 2;
279 os_memcpy(ssid->wep_key[key_idx], cred->key,
281 ssid->wep_key_len[key_idx] = cred->key_len;
283 ssid->wep_tx_keyidx = key_idx;
286 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
289 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
293 switch (cred->auth_type) {
295 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
296 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
299 case WPS_AUTH_SHARED:
300 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
301 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
304 case WPS_AUTH_WPAPSK:
305 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
306 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
307 ssid->proto = WPA_PROTO_WPA;
310 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
311 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
312 ssid->proto = WPA_PROTO_WPA;
315 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
316 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
317 ssid->proto = WPA_PROTO_RSN;
319 case WPS_AUTH_WPA2PSK:
320 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
321 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
322 ssid->proto = WPA_PROTO_RSN;
326 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
327 if (cred->key_len == 2 * PMK_LEN) {
328 if (hexstr2bin((const char *) cred->key, ssid->psk,
330 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
335 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
336 os_free(ssid->passphrase);
337 ssid->passphrase = os_malloc(cred->key_len + 1);
338 if (ssid->passphrase == NULL)
340 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
341 ssid->passphrase[cred->key_len] = '\0';
342 wpa_config_update_psk(ssid);
344 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
346 (unsigned long) cred->key_len);
351 wpas_wps_security_workaround(wpa_s, ssid, cred);
353 #ifndef CONFIG_NO_CONFIG_WRITE
354 if (wpa_s->conf->update_config &&
355 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
356 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
359 #endif /* CONFIG_NO_CONFIG_WRITE */
365 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
366 struct wps_event_m2d *m2d)
368 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
369 "dev_password_id=%d config_error=%d",
370 m2d->dev_password_id, m2d->config_error);
374 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
375 struct wps_event_fail *fail)
377 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
378 wpas_clear_wps(wpa_s);
382 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
384 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
385 wpa_s->wps_success = 1;
389 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
390 union wps_event_data *data)
392 struct wpa_supplicant *wpa_s = ctx;
395 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
398 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
401 wpa_supplicant_wps_event_success(wpa_s);
403 case WPS_EV_PWD_AUTH_FAIL:
409 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
411 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
412 eap_is_wps_pin_enrollee(&ssid->eap))
413 return WPS_REQ_ENROLLEE;
415 return WPS_REQ_REGISTRAR;
419 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
422 struct wpa_ssid *ssid;
424 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
426 /* Remove any existing WPS network from configuration */
427 ssid = wpa_s->conf->ssid;
429 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
430 if (ssid == wpa_s->current_ssid)
431 wpa_s->current_ssid = NULL;
437 wpa_config_remove_network(wpa_s->conf, id);
442 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
444 struct wpa_supplicant *wpa_s = eloop_ctx;
445 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
447 wpas_clear_wps(wpa_s);
451 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
452 int registrar, const u8 *bssid)
454 struct wpa_ssid *ssid;
456 ssid = wpa_config_add_network(wpa_s->conf);
459 wpa_config_set_network_defaults(ssid);
460 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
461 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
462 wpa_config_set(ssid, "identity", registrar ?
463 "\"" WSC_ID_REGISTRAR "\"" :
464 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
465 wpa_config_remove_network(wpa_s->conf, ssid->id);
471 struct wpa_scan_res *res;
473 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
476 /* Try to get SSID from scan results */
477 if (wpa_s->scan_res == NULL &&
478 wpa_supplicant_get_scan_results(wpa_s) < 0)
479 return ssid; /* Could not find any scan results */
481 for (i = 0; i < wpa_s->scan_res->num; i++) {
484 res = wpa_s->scan_res->res[i];
485 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
488 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
492 ssid->ssid = os_malloc(ie[1]);
493 if (ssid->ssid == NULL)
495 os_memcpy(ssid->ssid, ie + 2, ie[1]);
496 ssid->ssid_len = ie[1];
505 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
506 struct wpa_ssid *selected)
508 struct wpa_ssid *ssid;
510 /* Mark all other networks disabled and trigger reassociation */
511 ssid = wpa_s->conf->ssid;
513 ssid->disabled = ssid != selected;
516 wpa_s->disconnected = 0;
517 wpa_s->reassociate = 1;
518 wpa_s->scan_runs = 0;
519 wpa_s->wps_success = 0;
520 wpa_s->blacklist_cleared = 0;
521 wpa_supplicant_req_scan(wpa_s, 0, 0);
525 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
527 struct wpa_ssid *ssid;
528 wpas_clear_wps(wpa_s);
529 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
532 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
533 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
535 wpas_wps_reassoc(wpa_s, ssid);
540 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
543 struct wpa_ssid *ssid;
545 unsigned int rpin = 0;
547 wpas_clear_wps(wpa_s);
548 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
552 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
554 rpin = wps_generate_pin();
555 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
557 wpa_config_set(ssid, "phase1", val, 0);
558 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
560 wpas_wps_reassoc(wpa_s, ssid);
565 #ifdef CONFIG_WPS_OOB
566 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
567 char *path, char *method, char *name)
569 struct wps_context *wps = wpa_s->wps;
570 struct oob_device_data *oob_dev;
572 oob_dev = wps_get_oob_device(device_type);
575 oob_dev->device_path = path;
576 oob_dev->device_name = name;
577 wps->oob_conf.oob_method = wps_get_oob_method(method);
579 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
581 * Use pre-configured DH keys in order to be able to write the
582 * key hash into the OOB file.
584 wpabuf_free(wps->dh_pubkey);
585 wpabuf_free(wps->dh_privkey);
586 wps->dh_privkey = NULL;
587 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
589 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
590 if (wps->dh_pubkey == NULL) {
591 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
592 "Diffie-Hellman handshake");
597 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
598 wpas_clear_wps(wpa_s);
600 if (wps_process_oob(wps, oob_dev, 0) < 0)
603 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
604 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
605 wpas_wps_start_pin(wpa_s, NULL,
606 wpabuf_head(wps->oob_conf.dev_password)) < 0)
611 #endif /* CONFIG_WPS_OOB */
614 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
617 struct wpa_ssid *ssid;
622 wpas_clear_wps(wpa_s);
623 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
626 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
627 wpa_config_set(ssid, "phase1", val, 0);
628 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
630 wpas_wps_reassoc(wpa_s, ssid);
635 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
638 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
639 "STA " MACSTR, MAC2STR(mac_addr));
640 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
648 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
649 const struct wps_device_data *dev)
651 char uuid[40], txt[400];
653 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
655 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
656 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
657 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
658 uuid, MAC2STR(dev->mac_addr), dev->device_name,
659 dev->manufacturer, dev->model_name,
660 dev->model_number, dev->serial_number,
661 dev->categ, dev->oui, dev->sub_categ);
662 if (len > 0 && len < (int) sizeof(txt))
663 wpa_printf(MSG_INFO, "%s", txt);
667 int wpas_wps_init(struct wpa_supplicant *wpa_s)
669 struct wps_context *wps;
670 struct wps_registrar_config rcfg;
672 wps = os_zalloc(sizeof(*wps));
676 wps->cred_cb = wpa_supplicant_wps_cred;
677 wps->event_cb = wpa_supplicant_wps_event;
680 wps->dev.device_name = wpa_s->conf->device_name;
681 wps->dev.manufacturer = wpa_s->conf->manufacturer;
682 wps->dev.model_name = wpa_s->conf->model_name;
683 wps->dev.model_number = wpa_s->conf->model_number;
684 wps->dev.serial_number = wpa_s->conf->serial_number;
685 if (wpa_s->conf->device_type) {
688 /* <categ>-<OUI>-<subcateg> */
689 wps->dev.categ = atoi(wpa_s->conf->device_type);
690 pos = os_strchr(wpa_s->conf->device_type, '-');
692 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
697 if (hexstr2bin(pos, oui, 4)) {
698 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
702 wps->dev.oui = WPA_GET_BE32(oui);
703 pos = os_strchr(pos, '-');
705 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
710 wps->dev.sub_categ = atoi(pos);
712 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
713 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
714 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
715 if (is_nil_uuid(wpa_s->conf->uuid)) {
716 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
717 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
718 wps->uuid, WPS_UUID_LEN);
720 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
722 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
723 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
725 os_memset(&rcfg, 0, sizeof(rcfg));
726 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
727 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
730 wps->registrar = wps_registrar_init(wps, &rcfg);
731 if (wps->registrar == NULL) {
732 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
743 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
745 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
747 if (wpa_s->wps == NULL)
750 wps_registrar_deinit(wpa_s->wps->registrar);
751 wpabuf_free(wpa_s->wps->dh_pubkey);
752 wpabuf_free(wpa_s->wps->dh_privkey);
753 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
754 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
755 os_free(wpa_s->wps->network_key);
761 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
762 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
764 struct wpabuf *wps_ie;
766 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
769 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
770 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
772 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
776 if (!wps_is_selected_pbc_registrar(wps_ie)) {
777 wpa_printf(MSG_DEBUG, " skip - WPS AP "
778 "without active PBC Registrar");
783 /* TODO: overlap detection */
784 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
790 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
792 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
797 * Start with WPS APs that advertise active PIN Registrar and
798 * allow any WPS AP after third scan since some APs do not set
799 * Selected Registrar attribute properly when using external
802 if (!wps_is_selected_pin_registrar(wps_ie)) {
803 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
804 wpa_printf(MSG_DEBUG, " skip - WPS AP "
805 "without active PIN Registrar");
809 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
811 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
819 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
828 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
829 struct wpa_ssid *ssid,
830 struct wpa_scan_res *bss)
832 struct wpabuf *wps_ie = NULL;
835 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
836 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
837 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
838 /* allow wildcard SSID for WPS PBC */
841 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
842 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
844 (wps_is_selected_pin_registrar(wps_ie) ||
845 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
846 /* allow wildcard SSID for WPS PIN */
851 if (!ret && ssid->bssid_set &&
852 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
853 /* allow wildcard SSID due to hardcoded BSSID match */
863 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
864 struct wpa_scan_res *selected,
865 struct wpa_ssid *ssid)
867 const u8 *sel_uuid, *uuid;
869 struct wpabuf *wps_ie;
872 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
875 /* Make sure that only one AP is in active PBC mode */
876 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
878 sel_uuid = wps_get_uuid_e(wps_ie);
882 for (i = 0; i < wpa_s->scan_res->num; i++) {
883 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
887 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
890 if (!wps_is_selected_pbc_registrar(ie)) {
894 uuid = wps_get_uuid_e(ie);
895 if (sel_uuid == NULL || uuid == NULL ||
896 os_memcmp(sel_uuid, uuid, 16) != 0) {
897 ret = 1; /* PBC overlap */
902 /* TODO: verify that this is reasonable dual-band situation */
913 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
917 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
920 for (i = 0; i < wpa_s->scan_res->num; i++) {
921 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
923 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
926 if (wps_is_selected_pbc_registrar(ie))
927 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
928 else if (wps_is_selected_pin_registrar(ie))
929 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
931 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
938 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
940 struct wpa_ssid *ssid;
942 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
943 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)