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;
193 if ((wpa_s->conf->wps_cred_processing == 1 ||
194 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
195 size_t blen = cred->cred_attr_len * 2 + 1;
196 char *buf = os_malloc(blen);
198 wpa_snprintf_hex(buf, blen,
199 cred->cred_attr, cred->cred_attr_len);
200 wpa_msg(wpa_s, MSG_INFO, "%s%s",
201 WPS_EVENT_CRED_RECEIVED, buf);
204 wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
206 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
208 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
209 cred->cred_attr, cred->cred_attr_len);
211 if (wpa_s->conf->wps_cred_processing == 1)
214 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
215 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
217 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
218 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
219 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
220 cred->key, cred->key_len);
221 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
222 MAC2STR(cred->mac_addr));
224 auth_type = cred->auth_type;
225 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
226 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
227 "auth_type into WPA2PSK");
228 auth_type = WPS_AUTH_WPA2PSK;
231 if (auth_type != WPS_AUTH_OPEN &&
232 auth_type != WPS_AUTH_SHARED &&
233 auth_type != WPS_AUTH_WPAPSK &&
234 auth_type != WPS_AUTH_WPA2PSK) {
235 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
236 "unsupported authentication type 0x%x",
241 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
242 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
243 "on the received credential");
244 os_free(ssid->eap.identity);
245 ssid->eap.identity = NULL;
246 ssid->eap.identity_len = 0;
247 os_free(ssid->eap.phase1);
248 ssid->eap.phase1 = NULL;
249 os_free(ssid->eap.eap_methods);
250 ssid->eap.eap_methods = NULL;
252 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
253 "received credential");
254 ssid = wpa_config_add_network(wpa_s->conf);
259 wpa_config_set_network_defaults(ssid);
262 ssid->ssid = os_malloc(cred->ssid_len);
264 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
265 ssid->ssid_len = cred->ssid_len;
268 switch (cred->encr_type) {
272 if (cred->key_len <= 0)
274 if (cred->key_len != 5 && cred->key_len != 13 &&
275 cred->key_len != 10 && cred->key_len != 26) {
276 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
277 "%lu", (unsigned long) cred->key_len);
280 if (cred->key_idx > NUM_WEP_KEYS) {
281 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
286 key_idx = cred->key_idx - 1;
287 if (cred->key_len == 10 || cred->key_len == 26) {
288 if (hexstr2bin((char *) cred->key,
289 ssid->wep_key[key_idx],
290 cred->key_len / 2) < 0) {
291 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
295 ssid->wep_key_len[key_idx] = cred->key_len / 2;
297 os_memcpy(ssid->wep_key[key_idx], cred->key,
299 ssid->wep_key_len[key_idx] = cred->key_len;
301 ssid->wep_tx_keyidx = key_idx;
304 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
307 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
313 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
314 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
317 case WPS_AUTH_SHARED:
318 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
319 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
322 case WPS_AUTH_WPAPSK:
323 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
324 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
325 ssid->proto = WPA_PROTO_WPA;
328 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
329 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
330 ssid->proto = WPA_PROTO_WPA;
333 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
334 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
335 ssid->proto = WPA_PROTO_RSN;
337 case WPS_AUTH_WPA2PSK:
338 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
339 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
340 ssid->proto = WPA_PROTO_RSN;
344 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
345 if (cred->key_len == 2 * PMK_LEN) {
346 if (hexstr2bin((const char *) cred->key, ssid->psk,
348 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
353 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
354 os_free(ssid->passphrase);
355 ssid->passphrase = os_malloc(cred->key_len + 1);
356 if (ssid->passphrase == NULL)
358 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
359 ssid->passphrase[cred->key_len] = '\0';
360 wpa_config_update_psk(ssid);
362 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
364 (unsigned long) cred->key_len);
369 wpas_wps_security_workaround(wpa_s, ssid, cred);
371 #ifndef CONFIG_NO_CONFIG_WRITE
372 if (wpa_s->conf->update_config &&
373 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
374 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
377 #endif /* CONFIG_NO_CONFIG_WRITE */
383 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
384 struct wps_event_m2d *m2d)
386 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
387 "dev_password_id=%d config_error=%d",
388 m2d->dev_password_id, m2d->config_error);
392 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
393 struct wps_event_fail *fail)
395 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
396 wpas_clear_wps(wpa_s);
400 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
402 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
403 wpa_s->wps_success = 1;
407 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
408 union wps_event_data *data)
410 struct wpa_supplicant *wpa_s = ctx;
413 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
416 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
419 wpa_supplicant_wps_event_success(wpa_s);
421 case WPS_EV_PWD_AUTH_FAIL:
427 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
429 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
430 eap_is_wps_pin_enrollee(&ssid->eap))
431 return WPS_REQ_ENROLLEE;
433 return WPS_REQ_REGISTRAR;
437 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
440 struct wpa_ssid *ssid;
442 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
444 /* Remove any existing WPS network from configuration */
445 ssid = wpa_s->conf->ssid;
447 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
448 if (ssid == wpa_s->current_ssid)
449 wpa_s->current_ssid = NULL;
455 wpa_config_remove_network(wpa_s->conf, id);
460 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
462 struct wpa_supplicant *wpa_s = eloop_ctx;
463 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
465 wpas_clear_wps(wpa_s);
469 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
470 int registrar, const u8 *bssid)
472 struct wpa_ssid *ssid;
474 ssid = wpa_config_add_network(wpa_s->conf);
477 wpa_config_set_network_defaults(ssid);
478 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
479 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
480 wpa_config_set(ssid, "identity", registrar ?
481 "\"" WSC_ID_REGISTRAR "\"" :
482 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
483 wpa_config_remove_network(wpa_s->conf, ssid->id);
489 struct wpa_scan_res *res;
491 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
494 /* Try to get SSID from scan results */
495 if (wpa_s->scan_res == NULL &&
496 wpa_supplicant_get_scan_results(wpa_s) < 0)
497 return ssid; /* Could not find any scan results */
499 for (i = 0; i < wpa_s->scan_res->num; i++) {
502 res = wpa_s->scan_res->res[i];
503 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
506 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
510 ssid->ssid = os_malloc(ie[1]);
511 if (ssid->ssid == NULL)
513 os_memcpy(ssid->ssid, ie + 2, ie[1]);
514 ssid->ssid_len = ie[1];
523 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
524 struct wpa_ssid *selected)
526 struct wpa_ssid *ssid;
528 /* Mark all other networks disabled and trigger reassociation */
529 ssid = wpa_s->conf->ssid;
531 ssid->disabled = ssid != selected;
534 wpa_s->disconnected = 0;
535 wpa_s->reassociate = 1;
536 wpa_s->scan_runs = 0;
537 wpa_s->wps_success = 0;
538 wpa_s->blacklist_cleared = 0;
539 wpa_supplicant_req_scan(wpa_s, 0, 0);
543 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
545 struct wpa_ssid *ssid;
546 wpas_clear_wps(wpa_s);
547 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
550 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
551 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
553 wpas_wps_reassoc(wpa_s, ssid);
558 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
561 struct wpa_ssid *ssid;
563 unsigned int rpin = 0;
565 wpas_clear_wps(wpa_s);
566 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
570 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
572 rpin = wps_generate_pin();
573 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
575 wpa_config_set(ssid, "phase1", val, 0);
576 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
578 wpas_wps_reassoc(wpa_s, ssid);
583 #ifdef CONFIG_WPS_OOB
584 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
585 char *path, char *method, char *name)
587 struct wps_context *wps = wpa_s->wps;
588 struct oob_device_data *oob_dev;
590 oob_dev = wps_get_oob_device(device_type);
593 oob_dev->device_path = path;
594 oob_dev->device_name = name;
595 wps->oob_conf.oob_method = wps_get_oob_method(method);
597 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
599 * Use pre-configured DH keys in order to be able to write the
600 * key hash into the OOB file.
602 wpabuf_free(wps->dh_pubkey);
603 wpabuf_free(wps->dh_privkey);
604 wps->dh_privkey = NULL;
605 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
607 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
608 if (wps->dh_pubkey == NULL) {
609 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
610 "Diffie-Hellman handshake");
615 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
616 wpas_clear_wps(wpa_s);
618 if (wps_process_oob(wps, oob_dev, 0) < 0)
621 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
622 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
623 wpas_wps_start_pin(wpa_s, NULL,
624 wpabuf_head(wps->oob_conf.dev_password)) < 0)
629 #endif /* CONFIG_WPS_OOB */
632 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
635 struct wpa_ssid *ssid;
640 wpas_clear_wps(wpa_s);
641 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
644 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
645 wpa_config_set(ssid, "phase1", val, 0);
646 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
648 wpas_wps_reassoc(wpa_s, ssid);
653 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
656 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
657 "STA " MACSTR, MAC2STR(mac_addr));
658 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
666 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
667 const struct wps_device_data *dev)
669 char uuid[40], txt[400];
671 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
673 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
674 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
675 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
676 uuid, MAC2STR(dev->mac_addr), dev->device_name,
677 dev->manufacturer, dev->model_name,
678 dev->model_number, dev->serial_number,
679 dev->categ, dev->oui, dev->sub_categ);
680 if (len > 0 && len < (int) sizeof(txt))
681 wpa_printf(MSG_INFO, "%s", txt);
685 int wpas_wps_init(struct wpa_supplicant *wpa_s)
687 struct wps_context *wps;
688 struct wps_registrar_config rcfg;
690 wps = os_zalloc(sizeof(*wps));
694 wps->cred_cb = wpa_supplicant_wps_cred;
695 wps->event_cb = wpa_supplicant_wps_event;
698 wps->dev.device_name = wpa_s->conf->device_name;
699 wps->dev.manufacturer = wpa_s->conf->manufacturer;
700 wps->dev.model_name = wpa_s->conf->model_name;
701 wps->dev.model_number = wpa_s->conf->model_number;
702 wps->dev.serial_number = wpa_s->conf->serial_number;
703 if (wpa_s->conf->device_type) {
706 /* <categ>-<OUI>-<subcateg> */
707 wps->dev.categ = atoi(wpa_s->conf->device_type);
708 pos = os_strchr(wpa_s->conf->device_type, '-');
710 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
715 if (hexstr2bin(pos, oui, 4)) {
716 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
720 wps->dev.oui = WPA_GET_BE32(oui);
721 pos = os_strchr(pos, '-');
723 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
728 wps->dev.sub_categ = atoi(pos);
730 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
731 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
732 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
733 if (is_nil_uuid(wpa_s->conf->uuid)) {
734 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
735 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
736 wps->uuid, WPS_UUID_LEN);
738 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
740 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
741 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
743 os_memset(&rcfg, 0, sizeof(rcfg));
744 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
745 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
748 wps->registrar = wps_registrar_init(wps, &rcfg);
749 if (wps->registrar == NULL) {
750 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
761 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
763 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
765 if (wpa_s->wps == NULL)
768 wps_registrar_deinit(wpa_s->wps->registrar);
769 wpabuf_free(wpa_s->wps->dh_pubkey);
770 wpabuf_free(wpa_s->wps->dh_privkey);
771 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
772 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
773 os_free(wpa_s->wps->network_key);
779 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
780 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
782 struct wpabuf *wps_ie;
784 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
787 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
788 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
790 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
794 if (!wps_is_selected_pbc_registrar(wps_ie)) {
795 wpa_printf(MSG_DEBUG, " skip - WPS AP "
796 "without active PBC Registrar");
801 /* TODO: overlap detection */
802 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
808 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
810 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
815 * Start with WPS APs that advertise active PIN Registrar and
816 * allow any WPS AP after third scan since some APs do not set
817 * Selected Registrar attribute properly when using external
820 if (!wps_is_selected_pin_registrar(wps_ie)) {
821 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
822 wpa_printf(MSG_DEBUG, " skip - WPS AP "
823 "without active PIN Registrar");
827 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
829 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
837 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
846 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
847 struct wpa_ssid *ssid,
848 struct wpa_scan_res *bss)
850 struct wpabuf *wps_ie = NULL;
853 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
854 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
855 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
856 /* allow wildcard SSID for WPS PBC */
859 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
860 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
862 (wps_is_selected_pin_registrar(wps_ie) ||
863 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
864 /* allow wildcard SSID for WPS PIN */
869 if (!ret && ssid->bssid_set &&
870 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
871 /* allow wildcard SSID due to hardcoded BSSID match */
881 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
882 struct wpa_scan_res *selected,
883 struct wpa_ssid *ssid)
885 const u8 *sel_uuid, *uuid;
887 struct wpabuf *wps_ie;
890 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
893 /* Make sure that only one AP is in active PBC mode */
894 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
896 sel_uuid = wps_get_uuid_e(wps_ie);
900 for (i = 0; i < wpa_s->scan_res->num; i++) {
901 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
905 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
908 if (!wps_is_selected_pbc_registrar(ie)) {
912 uuid = wps_get_uuid_e(ie);
913 if (sel_uuid == NULL || uuid == NULL ||
914 os_memcmp(sel_uuid, uuid, 16) != 0) {
915 ret = 1; /* PBC overlap */
920 /* TODO: verify that this is reasonable dual-band situation */
931 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
935 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
938 for (i = 0; i < wpa_s->scan_res->num; i++) {
939 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
941 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
944 if (wps_is_selected_pbc_registrar(ie))
945 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
946 else if (wps_is_selected_pin_registrar(ie))
947 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
949 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
956 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
958 struct wpa_ssid *ssid;
960 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
961 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)