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 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
214 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
216 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
217 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
218 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
219 cred->key, cred->key_len);
220 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
221 MAC2STR(cred->mac_addr));
223 if (cred->auth_type != WPS_AUTH_OPEN &&
224 cred->auth_type != WPS_AUTH_SHARED &&
225 cred->auth_type != WPS_AUTH_WPAPSK &&
226 cred->auth_type != WPS_AUTH_WPA2PSK) {
227 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
228 "unsupported authentication type %d",
233 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
234 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
235 "on the received credential");
236 os_free(ssid->eap.identity);
237 ssid->eap.identity = NULL;
238 ssid->eap.identity_len = 0;
239 os_free(ssid->eap.phase1);
240 ssid->eap.phase1 = NULL;
241 os_free(ssid->eap.eap_methods);
242 ssid->eap.eap_methods = NULL;
244 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
245 "received credential");
246 ssid = wpa_config_add_network(wpa_s->conf);
251 wpa_config_set_network_defaults(ssid);
254 ssid->ssid = os_malloc(cred->ssid_len);
256 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
257 ssid->ssid_len = cred->ssid_len;
260 switch (cred->encr_type) {
264 if (cred->key_len <= 0)
266 if (cred->key_len != 5 && cred->key_len != 13 &&
267 cred->key_len != 10 && cred->key_len != 26) {
268 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
269 "%lu", (unsigned long) cred->key_len);
272 if (cred->key_idx > NUM_WEP_KEYS) {
273 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
278 key_idx = cred->key_idx - 1;
279 if (cred->key_len == 10 || cred->key_len == 26) {
280 if (hexstr2bin((char *) cred->key,
281 ssid->wep_key[key_idx],
282 cred->key_len / 2) < 0) {
283 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
287 ssid->wep_key_len[key_idx] = cred->key_len / 2;
289 os_memcpy(ssid->wep_key[key_idx], cred->key,
291 ssid->wep_key_len[key_idx] = cred->key_len;
293 ssid->wep_tx_keyidx = key_idx;
296 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
299 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
303 switch (cred->auth_type) {
305 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
306 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
309 case WPS_AUTH_SHARED:
310 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
311 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
314 case WPS_AUTH_WPAPSK:
315 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
316 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
317 ssid->proto = WPA_PROTO_WPA;
320 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
321 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
322 ssid->proto = WPA_PROTO_WPA;
325 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
326 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
327 ssid->proto = WPA_PROTO_RSN;
329 case WPS_AUTH_WPA2PSK:
330 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
331 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
332 ssid->proto = WPA_PROTO_RSN;
336 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
337 if (cred->key_len == 2 * PMK_LEN) {
338 if (hexstr2bin((const char *) cred->key, ssid->psk,
340 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
345 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
346 os_free(ssid->passphrase);
347 ssid->passphrase = os_malloc(cred->key_len + 1);
348 if (ssid->passphrase == NULL)
350 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
351 ssid->passphrase[cred->key_len] = '\0';
352 wpa_config_update_psk(ssid);
354 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
356 (unsigned long) cred->key_len);
361 wpas_wps_security_workaround(wpa_s, ssid, cred);
363 #ifndef CONFIG_NO_CONFIG_WRITE
364 if (wpa_s->conf->update_config &&
365 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
366 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
369 #endif /* CONFIG_NO_CONFIG_WRITE */
375 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
376 struct wps_event_m2d *m2d)
378 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
379 "dev_password_id=%d config_error=%d",
380 m2d->dev_password_id, m2d->config_error);
384 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
385 struct wps_event_fail *fail)
387 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
388 wpas_clear_wps(wpa_s);
392 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
394 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
395 wpa_s->wps_success = 1;
399 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
400 union wps_event_data *data)
402 struct wpa_supplicant *wpa_s = ctx;
405 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
408 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
411 wpa_supplicant_wps_event_success(wpa_s);
413 case WPS_EV_PWD_AUTH_FAIL:
419 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
421 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
422 eap_is_wps_pin_enrollee(&ssid->eap))
423 return WPS_REQ_ENROLLEE;
425 return WPS_REQ_REGISTRAR;
429 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
432 struct wpa_ssid *ssid;
434 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
436 /* Remove any existing WPS network from configuration */
437 ssid = wpa_s->conf->ssid;
439 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
440 if (ssid == wpa_s->current_ssid)
441 wpa_s->current_ssid = NULL;
447 wpa_config_remove_network(wpa_s->conf, id);
452 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
454 struct wpa_supplicant *wpa_s = eloop_ctx;
455 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
457 wpas_clear_wps(wpa_s);
461 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
462 int registrar, const u8 *bssid)
464 struct wpa_ssid *ssid;
466 ssid = wpa_config_add_network(wpa_s->conf);
469 wpa_config_set_network_defaults(ssid);
470 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
471 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
472 wpa_config_set(ssid, "identity", registrar ?
473 "\"" WSC_ID_REGISTRAR "\"" :
474 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
475 wpa_config_remove_network(wpa_s->conf, ssid->id);
481 struct wpa_scan_res *res;
483 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
486 /* Try to get SSID from scan results */
487 if (wpa_s->scan_res == NULL &&
488 wpa_supplicant_get_scan_results(wpa_s) < 0)
489 return ssid; /* Could not find any scan results */
491 for (i = 0; i < wpa_s->scan_res->num; i++) {
494 res = wpa_s->scan_res->res[i];
495 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
498 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
502 ssid->ssid = os_malloc(ie[1]);
503 if (ssid->ssid == NULL)
505 os_memcpy(ssid->ssid, ie + 2, ie[1]);
506 ssid->ssid_len = ie[1];
515 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
516 struct wpa_ssid *selected)
518 struct wpa_ssid *ssid;
520 /* Mark all other networks disabled and trigger reassociation */
521 ssid = wpa_s->conf->ssid;
523 ssid->disabled = ssid != selected;
526 wpa_s->disconnected = 0;
527 wpa_s->reassociate = 1;
528 wpa_s->scan_runs = 0;
529 wpa_s->wps_success = 0;
530 wpa_s->blacklist_cleared = 0;
531 wpa_supplicant_req_scan(wpa_s, 0, 0);
535 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
537 struct wpa_ssid *ssid;
538 wpas_clear_wps(wpa_s);
539 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
542 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
543 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
545 wpas_wps_reassoc(wpa_s, ssid);
550 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
553 struct wpa_ssid *ssid;
555 unsigned int rpin = 0;
557 wpas_clear_wps(wpa_s);
558 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
562 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
564 rpin = wps_generate_pin();
565 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
567 wpa_config_set(ssid, "phase1", val, 0);
568 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
570 wpas_wps_reassoc(wpa_s, ssid);
575 #ifdef CONFIG_WPS_OOB
576 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
577 char *path, char *method, char *name)
579 struct wps_context *wps = wpa_s->wps;
580 struct oob_device_data *oob_dev;
582 oob_dev = wps_get_oob_device(device_type);
585 oob_dev->device_path = path;
586 oob_dev->device_name = name;
587 wps->oob_conf.oob_method = wps_get_oob_method(method);
589 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
591 * Use pre-configured DH keys in order to be able to write the
592 * key hash into the OOB file.
594 wpabuf_free(wps->dh_pubkey);
595 wpabuf_free(wps->dh_privkey);
596 wps->dh_privkey = NULL;
597 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
599 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
600 if (wps->dh_pubkey == NULL) {
601 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
602 "Diffie-Hellman handshake");
607 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
608 wpas_clear_wps(wpa_s);
610 if (wps_process_oob(wps, oob_dev, 0) < 0)
613 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
614 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
615 wpas_wps_start_pin(wpa_s, NULL,
616 wpabuf_head(wps->oob_conf.dev_password)) < 0)
621 #endif /* CONFIG_WPS_OOB */
624 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
627 struct wpa_ssid *ssid;
632 wpas_clear_wps(wpa_s);
633 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
636 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
637 wpa_config_set(ssid, "phase1", val, 0);
638 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
640 wpas_wps_reassoc(wpa_s, ssid);
645 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
648 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
649 "STA " MACSTR, MAC2STR(mac_addr));
650 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
658 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
659 const struct wps_device_data *dev)
661 char uuid[40], txt[400];
663 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
665 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
666 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
667 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
668 uuid, MAC2STR(dev->mac_addr), dev->device_name,
669 dev->manufacturer, dev->model_name,
670 dev->model_number, dev->serial_number,
671 dev->categ, dev->oui, dev->sub_categ);
672 if (len > 0 && len < (int) sizeof(txt))
673 wpa_printf(MSG_INFO, "%s", txt);
677 int wpas_wps_init(struct wpa_supplicant *wpa_s)
679 struct wps_context *wps;
680 struct wps_registrar_config rcfg;
682 wps = os_zalloc(sizeof(*wps));
686 wps->cred_cb = wpa_supplicant_wps_cred;
687 wps->event_cb = wpa_supplicant_wps_event;
690 wps->dev.device_name = wpa_s->conf->device_name;
691 wps->dev.manufacturer = wpa_s->conf->manufacturer;
692 wps->dev.model_name = wpa_s->conf->model_name;
693 wps->dev.model_number = wpa_s->conf->model_number;
694 wps->dev.serial_number = wpa_s->conf->serial_number;
695 if (wpa_s->conf->device_type) {
698 /* <categ>-<OUI>-<subcateg> */
699 wps->dev.categ = atoi(wpa_s->conf->device_type);
700 pos = os_strchr(wpa_s->conf->device_type, '-');
702 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
707 if (hexstr2bin(pos, oui, 4)) {
708 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
712 wps->dev.oui = WPA_GET_BE32(oui);
713 pos = os_strchr(pos, '-');
715 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
720 wps->dev.sub_categ = atoi(pos);
722 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
723 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
724 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
725 if (is_nil_uuid(wpa_s->conf->uuid)) {
726 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
727 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
728 wps->uuid, WPS_UUID_LEN);
730 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
732 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
733 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
735 os_memset(&rcfg, 0, sizeof(rcfg));
736 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
737 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
740 wps->registrar = wps_registrar_init(wps, &rcfg);
741 if (wps->registrar == NULL) {
742 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
753 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
755 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
757 if (wpa_s->wps == NULL)
760 wps_registrar_deinit(wpa_s->wps->registrar);
761 wpabuf_free(wpa_s->wps->dh_pubkey);
762 wpabuf_free(wpa_s->wps->dh_privkey);
763 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
764 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
765 os_free(wpa_s->wps->network_key);
771 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
772 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
774 struct wpabuf *wps_ie;
776 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
779 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
780 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
782 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
786 if (!wps_is_selected_pbc_registrar(wps_ie)) {
787 wpa_printf(MSG_DEBUG, " skip - WPS AP "
788 "without active PBC Registrar");
793 /* TODO: overlap detection */
794 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
800 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
802 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
807 * Start with WPS APs that advertise active PIN Registrar and
808 * allow any WPS AP after third scan since some APs do not set
809 * Selected Registrar attribute properly when using external
812 if (!wps_is_selected_pin_registrar(wps_ie)) {
813 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
814 wpa_printf(MSG_DEBUG, " skip - WPS AP "
815 "without active PIN Registrar");
819 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
821 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
829 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
838 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
839 struct wpa_ssid *ssid,
840 struct wpa_scan_res *bss)
842 struct wpabuf *wps_ie = NULL;
845 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
846 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
847 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
848 /* allow wildcard SSID for WPS PBC */
851 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
852 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
854 (wps_is_selected_pin_registrar(wps_ie) ||
855 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
856 /* allow wildcard SSID for WPS PIN */
861 if (!ret && ssid->bssid_set &&
862 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
863 /* allow wildcard SSID due to hardcoded BSSID match */
873 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
874 struct wpa_scan_res *selected,
875 struct wpa_ssid *ssid)
877 const u8 *sel_uuid, *uuid;
879 struct wpabuf *wps_ie;
882 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
885 /* Make sure that only one AP is in active PBC mode */
886 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
888 sel_uuid = wps_get_uuid_e(wps_ie);
892 for (i = 0; i < wpa_s->scan_res->num; i++) {
893 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
897 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
900 if (!wps_is_selected_pbc_registrar(ie)) {
904 uuid = wps_get_uuid_e(ie);
905 if (sel_uuid == NULL || uuid == NULL ||
906 os_memcmp(sel_uuid, uuid, 16) != 0) {
907 ret = 1; /* PBC overlap */
912 /* TODO: verify that this is reasonable dual-band situation */
923 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
927 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
930 for (i = 0; i < wpa_s->scan_res->num; i++) {
931 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
933 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
936 if (wps_is_selected_pbc_registrar(ie))
937 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
938 else if (wps_is_selected_pin_registrar(ie))
939 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
941 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
948 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
950 struct wpa_ssid *ssid;
952 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
953 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)