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"
25 #include "eap_common/eap_wsc_common.h"
27 #include "wps/wps_defs.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);
54 static int wpa_supplicant_wps_cred(void *ctx,
55 const struct wps_credential *cred)
57 struct wpa_supplicant *wpa_s = ctx;
58 struct wpa_ssid *ssid = wpa_s->current_ssid;
60 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
62 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
63 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
64 "on the received credential");
65 os_free(ssid->eap.identity);
66 ssid->eap.identity = NULL;
67 ssid->eap.identity_len = 0;
68 os_free(ssid->eap.phase1);
69 ssid->eap.phase1 = NULL;
70 os_free(ssid->eap.eap_methods);
71 ssid->eap.eap_methods = NULL;
73 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
74 "received credential");
75 ssid = wpa_config_add_network(wpa_s->conf);
80 wpa_config_set_network_defaults(ssid);
83 ssid->ssid = os_malloc(cred->ssid_len);
85 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
86 ssid->ssid_len = cred->ssid_len;
89 switch (cred->encr_type) {
91 ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
94 ssid->pairwise_cipher = ssid->group_cipher =
95 WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
96 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
97 cred->key_idx < NUM_WEP_KEYS) {
98 os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
100 ssid->wep_key_len[cred->key_idx] = cred->key_len;
101 ssid->wep_tx_keyidx = cred->key_idx;
105 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
106 ssid->group_cipher = WPA_CIPHER_TKIP;
109 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
110 ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
114 switch (cred->auth_type) {
116 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
117 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
120 case WPS_AUTH_SHARED:
121 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
122 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
125 case WPS_AUTH_WPAPSK:
126 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
127 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
128 ssid->proto = WPA_PROTO_WPA;
131 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
132 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
133 ssid->proto = WPA_PROTO_WPA;
136 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
137 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
138 ssid->proto = WPA_PROTO_RSN;
140 case WPS_AUTH_WPA2PSK:
141 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
142 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
143 ssid->proto = WPA_PROTO_RSN;
147 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
148 if (cred->key_len == 2 * PMK_LEN) {
149 if (hexstr2bin((const char *) cred->key, ssid->psk,
151 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
156 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
157 os_free(ssid->passphrase);
158 ssid->passphrase = os_malloc(cred->key_len + 1);
159 if (ssid->passphrase == NULL)
161 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
162 ssid->passphrase[cred->key_len] = '\0';
163 wpa_config_update_psk(ssid);
165 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
167 (unsigned long) cred->key_len);
172 #ifndef CONFIG_NO_CONFIG_WRITE
173 if (wpa_s->conf->update_config &&
174 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
175 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
178 #endif /* CONFIG_NO_CONFIG_WRITE */
184 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
185 struct wps_event_m2d *m2d)
187 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
188 "dev_password_id=%d config_error=%d",
189 m2d->dev_password_id, m2d->config_error);
193 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
194 struct wps_event_fail *fail)
196 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
197 wpas_clear_wps(wpa_s);
201 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
203 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
207 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
208 union wps_event_data *data)
210 struct wpa_supplicant *wpa_s = ctx;
213 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
216 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
219 wpa_supplicant_wps_event_success(wpa_s);
225 u8 wpas_wps_get_req_type(struct wpa_ssid *ssid)
227 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
228 eap_is_wps_pin_enrollee(&ssid->eap))
229 return WPS_REQ_ENROLLEE;
231 return WPS_REQ_REGISTRAR;
235 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
238 struct wpa_ssid *ssid;
240 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
242 /* Remove any existing WPS network from configuration */
243 ssid = wpa_s->conf->ssid;
245 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
246 if (ssid == wpa_s->current_ssid)
247 wpa_s->current_ssid = NULL;
253 wpa_config_remove_network(wpa_s->conf, id);
258 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
260 struct wpa_supplicant *wpa_s = eloop_ctx;
261 wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
262 wpas_clear_wps(wpa_s);
266 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
267 int registrar, const u8 *bssid)
269 struct wpa_ssid *ssid;
271 ssid = wpa_config_add_network(wpa_s->conf);
274 wpa_config_set_network_defaults(ssid);
275 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
276 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
277 wpa_config_set(ssid, "identity", registrar ?
278 "\"" WSC_ID_REGISTRAR "\"" :
279 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
280 wpa_config_remove_network(wpa_s->conf, ssid->id);
286 struct wpa_scan_res *res;
288 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
291 /* Try to get SSID from scan results */
292 if (wpa_s->scan_res == NULL &&
293 wpa_supplicant_get_scan_results(wpa_s) < 0)
294 return ssid; /* Could not find any scan results */
296 for (i = 0; i < wpa_s->scan_res->num; i++) {
299 res = wpa_s->scan_res->res[i];
300 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
303 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
307 ssid->ssid = os_malloc(ie[1]);
308 if (ssid->ssid == NULL)
310 os_memcpy(ssid->ssid, ie + 2, ie[1]);
311 ssid->ssid_len = ie[1];
320 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
321 struct wpa_ssid *selected)
323 struct wpa_ssid *ssid;
325 /* Mark all other networks disabled and trigger reassociation */
326 ssid = wpa_s->conf->ssid;
328 ssid->disabled = ssid != selected;
331 wpa_s->disconnected = 0;
332 wpa_s->reassociate = 1;
333 wpa_supplicant_req_scan(wpa_s, 0, 0);
337 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
339 struct wpa_ssid *ssid;
340 wpas_clear_wps(wpa_s);
341 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
344 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
345 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
347 wpas_wps_reassoc(wpa_s, ssid);
352 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
355 struct wpa_ssid *ssid;
357 unsigned int rpin = 0;
359 wpas_clear_wps(wpa_s);
360 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
364 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
366 rpin = wps_generate_pin();
367 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
369 wpa_config_set(ssid, "phase1", val, 0);
370 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
372 wpas_wps_reassoc(wpa_s, ssid);
377 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
380 struct wpa_ssid *ssid;
385 wpas_clear_wps(wpa_s);
386 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
389 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
390 wpa_config_set(ssid, "phase1", val, 0);
391 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
393 wpas_wps_reassoc(wpa_s, ssid);
398 int wpas_wps_init(struct wpa_supplicant *wpa_s)
400 struct wps_context *wps;
402 wps = os_zalloc(sizeof(*wps));
406 wps->cred_cb = wpa_supplicant_wps_cred;
407 wps->event_cb = wpa_supplicant_wps_event;
410 wps->dev.device_name = wpa_s->conf->device_name;
411 wps->dev.manufacturer = wpa_s->conf->manufacturer;
412 wps->dev.model_name = wpa_s->conf->model_name;
413 wps->dev.model_number = wpa_s->conf->model_number;
414 wps->dev.serial_number = wpa_s->conf->serial_number;
415 if (wpa_s->conf->device_type) {
418 /* <categ>-<OUI>-<subcateg> */
419 wps->dev.categ = atoi(wpa_s->conf->device_type);
420 pos = os_strchr(wpa_s->conf->device_type, '-');
422 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
427 if (hexstr2bin(pos, oui, 4)) {
428 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
432 wps->dev.oui = WPA_GET_BE32(oui);
433 pos = os_strchr(pos, '-');
435 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
440 wps->dev.sub_categ = atoi(pos);
442 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
443 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
444 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
445 os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
453 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
455 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
457 if (wpa_s->wps == NULL)
460 os_free(wpa_s->wps->network_key);
466 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
468 struct wpabuf *wps_ie;
470 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
473 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
474 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
476 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
480 if (!wps_is_selected_pbc_registrar(wps_ie)) {
481 wpa_printf(MSG_DEBUG, " skip - WPS AP "
482 "without active PBC Registrar");
487 /* TODO: overlap detection */
488 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
494 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
496 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
500 if (!wps_is_selected_pin_registrar(wps_ie)) {
501 wpa_printf(MSG_DEBUG, " skip - WPS AP "
502 "without active PIN Registrar");
506 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
513 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
522 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
523 struct wpa_scan_res *bss)
525 struct wpabuf *wps_ie = NULL;
528 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
529 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
530 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
531 /* allow wildcard SSID for WPS PBC */
534 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
535 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
536 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
537 /* allow wildcard SSID for WPS PIN */
542 if (!ret && ssid->bssid_set &&
543 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
544 /* allow wildcard SSID due to hardcoded BSSID match */
554 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
555 struct wpa_scan_res *selected,
556 struct wpa_ssid *ssid)
558 const u8 *sel_uuid, *uuid;
560 struct wpabuf *wps_ie;
563 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
566 /* Make sure that only one AP is in active PBC mode */
567 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
569 sel_uuid = wps_get_uuid_e(wps_ie);
573 wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for PBC "
574 "overlap detection");
579 for (i = 0; i < wpa_s->scan_res->num; i++) {
580 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
584 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
587 if (!wps_is_selected_pbc_registrar(ie)) {
591 uuid = wps_get_uuid_e(ie);
593 wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for "
594 "PBC overlap detection (other BSS)");
599 if (os_memcmp(sel_uuid, uuid, 16) != 0) {
600 ret = 1; /* PBC overlap */
605 /* TODO: verify that this is reasonable dual-band situation */
616 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
620 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
623 for (i = 0; i < wpa_s->scan_res->num; i++) {
624 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
626 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
629 if (wps_is_selected_pbc_registrar(ie))
630 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
631 else if (wps_is_selected_pin_registrar(ie))
632 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
634 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
641 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
643 struct wpa_ssid *ssid;
645 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
646 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)