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 "ctrl_iface_dbus.h"
27 #include "eap_common/eap_wsc_common.h"
28 #include "wps_supplicant.h"
31 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
32 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
35 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
37 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
39 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
40 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
41 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
42 "try to associate with the received credential");
43 wpa_supplicant_deauthenticate(wpa_s,
44 WLAN_REASON_DEAUTH_LEAVING);
45 wpa_s->reassociate = 1;
46 wpa_supplicant_req_scan(wpa_s, 0, 0);
50 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
51 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
52 "for external credential processing");
53 wpas_clear_wps(wpa_s);
54 wpa_supplicant_deauthenticate(wpa_s,
55 WLAN_REASON_DEAUTH_LEAVING);
63 static int wpa_supplicant_wps_cred(void *ctx,
64 const struct wps_credential *cred)
66 struct wpa_supplicant *wpa_s = ctx;
67 struct wpa_ssid *ssid = wpa_s->current_ssid;
69 if ((wpa_s->conf->wps_cred_processing == 1 ||
70 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
71 size_t blen = cred->cred_attr_len * 2 + 1;
72 char *buf = os_malloc(blen);
74 wpa_snprintf_hex(buf, blen,
75 cred->cred_attr, cred->cred_attr_len);
76 wpa_msg(wpa_s, MSG_INFO, "%s%s",
77 WPS_EVENT_CRED_RECEIVED, buf);
80 wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
82 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
84 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
85 cred->cred_attr, cred->cred_attr_len);
87 if (wpa_s->conf->wps_cred_processing == 1)
90 if (cred->auth_type != WPS_AUTH_OPEN &&
91 cred->auth_type != WPS_AUTH_SHARED &&
92 cred->auth_type != WPS_AUTH_WPAPSK &&
93 cred->auth_type != WPS_AUTH_WPA2PSK) {
94 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
95 "unsupported authentication type %d",
100 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
101 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
102 "on the received credential");
103 os_free(ssid->eap.identity);
104 ssid->eap.identity = NULL;
105 ssid->eap.identity_len = 0;
106 os_free(ssid->eap.phase1);
107 ssid->eap.phase1 = NULL;
108 os_free(ssid->eap.eap_methods);
109 ssid->eap.eap_methods = NULL;
111 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
112 "received credential");
113 ssid = wpa_config_add_network(wpa_s->conf);
118 wpa_config_set_network_defaults(ssid);
121 ssid->ssid = os_malloc(cred->ssid_len);
123 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
124 ssid->ssid_len = cred->ssid_len;
127 switch (cred->encr_type) {
131 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
132 cred->key_idx < NUM_WEP_KEYS) {
133 os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
135 ssid->wep_key_len[cred->key_idx] = cred->key_len;
136 ssid->wep_tx_keyidx = cred->key_idx;
140 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
143 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
147 switch (cred->auth_type) {
149 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
150 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
153 case WPS_AUTH_SHARED:
154 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
155 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
158 case WPS_AUTH_WPAPSK:
159 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
160 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
161 ssid->proto = WPA_PROTO_WPA;
164 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
165 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
166 ssid->proto = WPA_PROTO_WPA;
169 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
170 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
171 ssid->proto = WPA_PROTO_RSN;
173 case WPS_AUTH_WPA2PSK:
174 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
175 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
176 ssid->proto = WPA_PROTO_RSN;
180 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
181 if (cred->key_len == 2 * PMK_LEN) {
182 if (hexstr2bin((const char *) cred->key, ssid->psk,
184 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
189 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
190 os_free(ssid->passphrase);
191 ssid->passphrase = os_malloc(cred->key_len + 1);
192 if (ssid->passphrase == NULL)
194 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
195 ssid->passphrase[cred->key_len] = '\0';
196 wpa_config_update_psk(ssid);
198 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
200 (unsigned long) cred->key_len);
205 #ifndef CONFIG_NO_CONFIG_WRITE
206 if (wpa_s->conf->update_config &&
207 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
208 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
211 #endif /* CONFIG_NO_CONFIG_WRITE */
217 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
218 struct wps_event_m2d *m2d)
220 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
221 "dev_password_id=%d config_error=%d",
222 m2d->dev_password_id, m2d->config_error);
226 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
227 struct wps_event_fail *fail)
229 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
230 wpas_clear_wps(wpa_s);
234 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
236 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
240 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
241 union wps_event_data *data)
243 struct wpa_supplicant *wpa_s = ctx;
246 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
249 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
252 wpa_supplicant_wps_event_success(wpa_s);
258 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
260 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
261 eap_is_wps_pin_enrollee(&ssid->eap))
262 return WPS_REQ_ENROLLEE;
264 return WPS_REQ_REGISTRAR;
268 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
271 struct wpa_ssid *ssid;
273 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
275 /* Remove any existing WPS network from configuration */
276 ssid = wpa_s->conf->ssid;
278 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
279 if (ssid == wpa_s->current_ssid)
280 wpa_s->current_ssid = NULL;
286 wpa_config_remove_network(wpa_s->conf, id);
291 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
293 struct wpa_supplicant *wpa_s = eloop_ctx;
294 wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
295 wpas_clear_wps(wpa_s);
299 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
300 int registrar, const u8 *bssid)
302 struct wpa_ssid *ssid;
304 ssid = wpa_config_add_network(wpa_s->conf);
307 wpa_config_set_network_defaults(ssid);
308 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
309 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
310 wpa_config_set(ssid, "identity", registrar ?
311 "\"" WSC_ID_REGISTRAR "\"" :
312 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
313 wpa_config_remove_network(wpa_s->conf, ssid->id);
319 struct wpa_scan_res *res;
321 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
324 /* Try to get SSID from scan results */
325 if (wpa_s->scan_res == NULL &&
326 wpa_supplicant_get_scan_results(wpa_s) < 0)
327 return ssid; /* Could not find any scan results */
329 for (i = 0; i < wpa_s->scan_res->num; i++) {
332 res = wpa_s->scan_res->res[i];
333 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
336 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
340 ssid->ssid = os_malloc(ie[1]);
341 if (ssid->ssid == NULL)
343 os_memcpy(ssid->ssid, ie + 2, ie[1]);
344 ssid->ssid_len = ie[1];
353 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
354 struct wpa_ssid *selected)
356 struct wpa_ssid *ssid;
358 /* Mark all other networks disabled and trigger reassociation */
359 ssid = wpa_s->conf->ssid;
361 ssid->disabled = ssid != selected;
364 wpa_s->disconnected = 0;
365 wpa_s->reassociate = 1;
366 wpa_supplicant_req_scan(wpa_s, 0, 0);
370 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
372 struct wpa_ssid *ssid;
373 wpas_clear_wps(wpa_s);
374 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
377 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
378 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
380 wpas_wps_reassoc(wpa_s, ssid);
385 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
388 struct wpa_ssid *ssid;
390 unsigned int rpin = 0;
392 wpas_clear_wps(wpa_s);
393 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
397 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
399 rpin = wps_generate_pin();
400 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
402 wpa_config_set(ssid, "phase1", val, 0);
403 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
405 wpas_wps_reassoc(wpa_s, ssid);
410 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
413 struct wpa_ssid *ssid;
418 wpas_clear_wps(wpa_s);
419 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
422 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
423 wpa_config_set(ssid, "phase1", val, 0);
424 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
426 wpas_wps_reassoc(wpa_s, ssid);
431 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
434 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
435 "STA " MACSTR, MAC2STR(mac_addr));
436 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
444 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
445 const struct wps_device_data *dev)
447 char uuid[40], txt[400];
449 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
451 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
452 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
453 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
454 uuid, MAC2STR(dev->mac_addr), dev->device_name,
455 dev->manufacturer, dev->model_name,
456 dev->model_number, dev->serial_number,
457 dev->categ, dev->oui, dev->sub_categ);
458 if (len > 0 && len < (int) sizeof(txt))
459 wpa_printf(MSG_INFO, "%s", txt);
463 int wpas_wps_init(struct wpa_supplicant *wpa_s)
465 struct wps_context *wps;
466 struct wps_registrar_config rcfg;
468 wps = os_zalloc(sizeof(*wps));
472 wps->cred_cb = wpa_supplicant_wps_cred;
473 wps->event_cb = wpa_supplicant_wps_event;
476 wps->dev.device_name = wpa_s->conf->device_name;
477 wps->dev.manufacturer = wpa_s->conf->manufacturer;
478 wps->dev.model_name = wpa_s->conf->model_name;
479 wps->dev.model_number = wpa_s->conf->model_number;
480 wps->dev.serial_number = wpa_s->conf->serial_number;
481 if (wpa_s->conf->device_type) {
484 /* <categ>-<OUI>-<subcateg> */
485 wps->dev.categ = atoi(wpa_s->conf->device_type);
486 pos = os_strchr(wpa_s->conf->device_type, '-');
488 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
493 if (hexstr2bin(pos, oui, 4)) {
494 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
498 wps->dev.oui = WPA_GET_BE32(oui);
499 pos = os_strchr(pos, '-');
501 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
506 wps->dev.sub_categ = atoi(pos);
508 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
509 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
510 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
511 if (is_nil_uuid(wpa_s->conf->uuid)) {
512 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
513 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
514 wps->uuid, WPS_UUID_LEN);
516 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
518 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
519 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
521 os_memset(&rcfg, 0, sizeof(rcfg));
522 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
523 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
526 wps->registrar = wps_registrar_init(wps, &rcfg);
527 if (wps->registrar == NULL) {
528 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
539 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
541 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
543 if (wpa_s->wps == NULL)
546 wps_registrar_deinit(wpa_s->wps->registrar);
547 os_free(wpa_s->wps->network_key);
553 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
555 struct wpabuf *wps_ie;
557 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
560 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
561 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
563 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
567 if (!wps_is_selected_pbc_registrar(wps_ie)) {
568 wpa_printf(MSG_DEBUG, " skip - WPS AP "
569 "without active PBC Registrar");
574 /* TODO: overlap detection */
575 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
581 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
583 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
587 if (!wps_is_selected_pin_registrar(wps_ie)) {
588 wpa_printf(MSG_DEBUG, " skip - WPS AP "
589 "without active PIN Registrar");
593 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
600 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
609 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
610 struct wpa_scan_res *bss)
612 struct wpabuf *wps_ie = NULL;
615 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
616 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
617 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
618 /* allow wildcard SSID for WPS PBC */
621 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
622 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
623 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
624 /* allow wildcard SSID for WPS PIN */
629 if (!ret && ssid->bssid_set &&
630 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
631 /* allow wildcard SSID due to hardcoded BSSID match */
641 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
642 struct wpa_scan_res *selected,
643 struct wpa_ssid *ssid)
645 const u8 *sel_uuid, *uuid;
647 struct wpabuf *wps_ie;
650 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
653 /* Make sure that only one AP is in active PBC mode */
654 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
656 sel_uuid = wps_get_uuid_e(wps_ie);
660 for (i = 0; i < wpa_s->scan_res->num; i++) {
661 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
665 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
668 if (!wps_is_selected_pbc_registrar(ie)) {
672 uuid = wps_get_uuid_e(ie);
673 if (sel_uuid == NULL || uuid == NULL ||
674 os_memcmp(sel_uuid, uuid, 16) != 0) {
675 ret = 1; /* PBC overlap */
680 /* TODO: verify that this is reasonable dual-band situation */
691 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
695 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
698 for (i = 0; i < wpa_s->scan_res->num; i++) {
699 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
701 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
704 if (wps_is_selected_pbc_registrar(ie))
705 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
706 else if (wps_is_selected_pin_registrar(ie))
707 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
709 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
716 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
718 struct wpa_ssid *ssid;
720 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
721 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)