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"
26 #include "eap_common/eap_wsc_common.h"
27 #include "wps_supplicant.h"
30 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
31 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
34 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
36 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
38 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
39 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
40 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
41 "try to associate with the received credential");
42 wpa_supplicant_deauthenticate(wpa_s,
43 WLAN_REASON_DEAUTH_LEAVING);
44 wpa_s->reassociate = 1;
45 wpa_supplicant_req_scan(wpa_s, 0, 0);
53 static int wpa_supplicant_wps_cred(void *ctx,
54 const struct wps_credential *cred)
56 struct wpa_supplicant *wpa_s = ctx;
57 struct wpa_ssid *ssid = wpa_s->current_ssid;
59 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
61 if (cred->auth_type != WPS_AUTH_OPEN &&
62 cred->auth_type != WPS_AUTH_SHARED &&
63 cred->auth_type != WPS_AUTH_WPAPSK &&
64 cred->auth_type != WPS_AUTH_WPA2PSK) {
65 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
66 "unsupported authentication type %d",
71 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
72 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
73 "on the received credential");
74 os_free(ssid->eap.identity);
75 ssid->eap.identity = NULL;
76 ssid->eap.identity_len = 0;
77 os_free(ssid->eap.phase1);
78 ssid->eap.phase1 = NULL;
79 os_free(ssid->eap.eap_methods);
80 ssid->eap.eap_methods = NULL;
82 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
83 "received credential");
84 ssid = wpa_config_add_network(wpa_s->conf);
89 wpa_config_set_network_defaults(ssid);
92 ssid->ssid = os_malloc(cred->ssid_len);
94 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
95 ssid->ssid_len = cred->ssid_len;
98 switch (cred->encr_type) {
102 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
103 cred->key_idx < NUM_WEP_KEYS) {
104 os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
106 ssid->wep_key_len[cred->key_idx] = cred->key_len;
107 ssid->wep_tx_keyidx = cred->key_idx;
111 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
114 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
118 switch (cred->auth_type) {
120 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
121 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
124 case WPS_AUTH_SHARED:
125 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
126 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
129 case WPS_AUTH_WPAPSK:
130 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
131 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
132 ssid->proto = WPA_PROTO_WPA;
135 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
136 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
137 ssid->proto = WPA_PROTO_WPA;
140 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
141 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
142 ssid->proto = WPA_PROTO_RSN;
144 case WPS_AUTH_WPA2PSK:
145 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
146 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
147 ssid->proto = WPA_PROTO_RSN;
151 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
152 if (cred->key_len == 2 * PMK_LEN) {
153 if (hexstr2bin((const char *) cred->key, ssid->psk,
155 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
160 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
161 os_free(ssid->passphrase);
162 ssid->passphrase = os_malloc(cred->key_len + 1);
163 if (ssid->passphrase == NULL)
165 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
166 ssid->passphrase[cred->key_len] = '\0';
167 wpa_config_update_psk(ssid);
169 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
171 (unsigned long) cred->key_len);
176 #ifndef CONFIG_NO_CONFIG_WRITE
177 if (wpa_s->conf->update_config &&
178 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
179 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
182 #endif /* CONFIG_NO_CONFIG_WRITE */
188 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
189 struct wps_event_m2d *m2d)
191 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
192 "dev_password_id=%d config_error=%d",
193 m2d->dev_password_id, m2d->config_error);
197 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
198 struct wps_event_fail *fail)
200 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
201 wpas_clear_wps(wpa_s);
205 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
207 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
211 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
212 union wps_event_data *data)
214 struct wpa_supplicant *wpa_s = ctx;
217 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
220 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
223 wpa_supplicant_wps_event_success(wpa_s);
229 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
231 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
232 eap_is_wps_pin_enrollee(&ssid->eap))
233 return WPS_REQ_ENROLLEE;
235 return WPS_REQ_REGISTRAR;
239 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
242 struct wpa_ssid *ssid;
244 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
246 /* Remove any existing WPS network from configuration */
247 ssid = wpa_s->conf->ssid;
249 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
250 if (ssid == wpa_s->current_ssid)
251 wpa_s->current_ssid = NULL;
257 wpa_config_remove_network(wpa_s->conf, id);
262 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
264 struct wpa_supplicant *wpa_s = eloop_ctx;
265 wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
266 wpas_clear_wps(wpa_s);
270 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
271 int registrar, const u8 *bssid)
273 struct wpa_ssid *ssid;
275 ssid = wpa_config_add_network(wpa_s->conf);
278 wpa_config_set_network_defaults(ssid);
279 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
280 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
281 wpa_config_set(ssid, "identity", registrar ?
282 "\"" WSC_ID_REGISTRAR "\"" :
283 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
284 wpa_config_remove_network(wpa_s->conf, ssid->id);
290 struct wpa_scan_res *res;
292 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
295 /* Try to get SSID from scan results */
296 if (wpa_s->scan_res == NULL &&
297 wpa_supplicant_get_scan_results(wpa_s) < 0)
298 return ssid; /* Could not find any scan results */
300 for (i = 0; i < wpa_s->scan_res->num; i++) {
303 res = wpa_s->scan_res->res[i];
304 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
307 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
311 ssid->ssid = os_malloc(ie[1]);
312 if (ssid->ssid == NULL)
314 os_memcpy(ssid->ssid, ie + 2, ie[1]);
315 ssid->ssid_len = ie[1];
324 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
325 struct wpa_ssid *selected)
327 struct wpa_ssid *ssid;
329 /* Mark all other networks disabled and trigger reassociation */
330 ssid = wpa_s->conf->ssid;
332 ssid->disabled = ssid != selected;
335 wpa_s->disconnected = 0;
336 wpa_s->reassociate = 1;
337 wpa_supplicant_req_scan(wpa_s, 0, 0);
341 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
343 struct wpa_ssid *ssid;
344 wpas_clear_wps(wpa_s);
345 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
348 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
349 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
351 wpas_wps_reassoc(wpa_s, ssid);
356 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
359 struct wpa_ssid *ssid;
361 unsigned int rpin = 0;
363 wpas_clear_wps(wpa_s);
364 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
368 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
370 rpin = wps_generate_pin();
371 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
373 wpa_config_set(ssid, "phase1", val, 0);
374 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
376 wpas_wps_reassoc(wpa_s, ssid);
381 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
384 struct wpa_ssid *ssid;
389 wpas_clear_wps(wpa_s);
390 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
393 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
394 wpa_config_set(ssid, "phase1", val, 0);
395 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
397 wpas_wps_reassoc(wpa_s, ssid);
402 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
405 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
406 "STA " MACSTR, MAC2STR(mac_addr));
407 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
415 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
416 const struct wps_device_data *dev)
418 char uuid[40], txt[400];
420 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
422 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
423 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
424 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
425 uuid, MAC2STR(dev->mac_addr), dev->device_name,
426 dev->manufacturer, dev->model_name,
427 dev->model_number, dev->serial_number,
428 dev->categ, dev->oui, dev->sub_categ);
429 if (len > 0 && len < (int) sizeof(txt))
430 wpa_printf(MSG_INFO, "%s", txt);
434 int wpas_wps_init(struct wpa_supplicant *wpa_s)
436 struct wps_context *wps;
437 struct wps_registrar_config rcfg;
439 wps = os_zalloc(sizeof(*wps));
443 wps->cred_cb = wpa_supplicant_wps_cred;
444 wps->event_cb = wpa_supplicant_wps_event;
447 wps->dev.device_name = wpa_s->conf->device_name;
448 wps->dev.manufacturer = wpa_s->conf->manufacturer;
449 wps->dev.model_name = wpa_s->conf->model_name;
450 wps->dev.model_number = wpa_s->conf->model_number;
451 wps->dev.serial_number = wpa_s->conf->serial_number;
452 if (wpa_s->conf->device_type) {
455 /* <categ>-<OUI>-<subcateg> */
456 wps->dev.categ = atoi(wpa_s->conf->device_type);
457 pos = os_strchr(wpa_s->conf->device_type, '-');
459 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
464 if (hexstr2bin(pos, oui, 4)) {
465 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
469 wps->dev.oui = WPA_GET_BE32(oui);
470 pos = os_strchr(pos, '-');
472 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
477 wps->dev.sub_categ = atoi(pos);
479 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
480 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
481 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
482 if (is_nil_uuid(wpa_s->conf->uuid)) {
483 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
484 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
485 wps->uuid, WPS_UUID_LEN);
487 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
489 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
490 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
492 os_memset(&rcfg, 0, sizeof(rcfg));
493 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
494 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
497 wps->registrar = wps_registrar_init(wps, &rcfg);
498 if (wps->registrar == NULL) {
499 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
510 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
512 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
514 if (wpa_s->wps == NULL)
517 wps_registrar_deinit(wpa_s->wps->registrar);
518 os_free(wpa_s->wps->network_key);
524 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
526 struct wpabuf *wps_ie;
528 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
531 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
532 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
534 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
538 if (!wps_is_selected_pbc_registrar(wps_ie)) {
539 wpa_printf(MSG_DEBUG, " skip - WPS AP "
540 "without active PBC Registrar");
545 /* TODO: overlap detection */
546 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
552 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
554 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
558 if (!wps_is_selected_pin_registrar(wps_ie)) {
559 wpa_printf(MSG_DEBUG, " skip - WPS AP "
560 "without active PIN Registrar");
564 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
571 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
580 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
581 struct wpa_scan_res *bss)
583 struct wpabuf *wps_ie = NULL;
586 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
587 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
588 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
589 /* allow wildcard SSID for WPS PBC */
592 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
593 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
594 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
595 /* allow wildcard SSID for WPS PIN */
600 if (!ret && ssid->bssid_set &&
601 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
602 /* allow wildcard SSID due to hardcoded BSSID match */
612 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
613 struct wpa_scan_res *selected,
614 struct wpa_ssid *ssid)
616 const u8 *sel_uuid, *uuid;
618 struct wpabuf *wps_ie;
621 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
624 /* Make sure that only one AP is in active PBC mode */
625 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
627 sel_uuid = wps_get_uuid_e(wps_ie);
631 for (i = 0; i < wpa_s->scan_res->num; i++) {
632 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
636 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
639 if (!wps_is_selected_pbc_registrar(ie)) {
643 uuid = wps_get_uuid_e(ie);
644 if (sel_uuid == NULL || uuid == NULL ||
645 os_memcmp(sel_uuid, uuid, 16) != 0) {
646 ret = 1; /* PBC overlap */
651 /* TODO: verify that this is reasonable dual-band situation */
662 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
666 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
669 for (i = 0; i < wpa_s->scan_res->num; i++) {
670 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
672 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
675 if (wps_is_selected_pbc_registrar(ie))
676 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
677 else if (wps_is_selected_pin_registrar(ie))
678 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
680 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
687 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
689 struct wpa_ssid *ssid;
691 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
692 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)