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 (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
62 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
63 "on the received credential");
64 os_free(ssid->eap.identity);
65 ssid->eap.identity = NULL;
66 ssid->eap.identity_len = 0;
67 os_free(ssid->eap.phase1);
68 ssid->eap.phase1 = NULL;
69 os_free(ssid->eap.eap_methods);
70 ssid->eap.eap_methods = NULL;
72 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
73 "received credential");
74 ssid = wpa_config_add_network(wpa_s->conf);
79 wpa_config_set_network_defaults(ssid);
82 ssid->ssid = os_malloc(cred->ssid_len);
84 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
85 ssid->ssid_len = cred->ssid_len;
88 switch (cred->encr_type) {
92 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
93 cred->key_idx < NUM_WEP_KEYS) {
94 os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
96 ssid->wep_key_len[cred->key_idx] = cred->key_len;
97 ssid->wep_tx_keyidx = cred->key_idx;
101 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
104 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
108 switch (cred->auth_type) {
110 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
111 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
114 case WPS_AUTH_SHARED:
115 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
116 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
119 case WPS_AUTH_WPAPSK:
120 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
121 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
122 ssid->proto = WPA_PROTO_WPA;
125 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
126 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
127 ssid->proto = WPA_PROTO_WPA;
130 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
131 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
132 ssid->proto = WPA_PROTO_RSN;
134 case WPS_AUTH_WPA2PSK:
135 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
136 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
137 ssid->proto = WPA_PROTO_RSN;
141 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
142 if (cred->key_len == 2 * PMK_LEN) {
143 if (hexstr2bin((const char *) cred->key, ssid->psk,
145 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
150 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
151 os_free(ssid->passphrase);
152 ssid->passphrase = os_malloc(cred->key_len + 1);
153 if (ssid->passphrase == NULL)
155 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
156 ssid->passphrase[cred->key_len] = '\0';
157 wpa_config_update_psk(ssid);
159 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
161 (unsigned long) cred->key_len);
166 #ifndef CONFIG_NO_CONFIG_WRITE
167 if (wpa_s->conf->update_config &&
168 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
169 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
172 #endif /* CONFIG_NO_CONFIG_WRITE */
178 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
179 struct wps_event_m2d *m2d)
181 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
182 "dev_password_id=%d config_error=%d",
183 m2d->dev_password_id, m2d->config_error);
187 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
188 struct wps_event_fail *fail)
190 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
191 wpas_clear_wps(wpa_s);
195 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
197 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
201 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
202 union wps_event_data *data)
204 struct wpa_supplicant *wpa_s = ctx;
207 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
210 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
213 wpa_supplicant_wps_event_success(wpa_s);
219 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
221 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
222 eap_is_wps_pin_enrollee(&ssid->eap))
223 return WPS_REQ_ENROLLEE;
225 return WPS_REQ_REGISTRAR;
229 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
232 struct wpa_ssid *ssid;
234 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
236 /* Remove any existing WPS network from configuration */
237 ssid = wpa_s->conf->ssid;
239 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
240 if (ssid == wpa_s->current_ssid)
241 wpa_s->current_ssid = NULL;
247 wpa_config_remove_network(wpa_s->conf, id);
252 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
254 struct wpa_supplicant *wpa_s = eloop_ctx;
255 wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
256 wpas_clear_wps(wpa_s);
260 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
261 int registrar, const u8 *bssid)
263 struct wpa_ssid *ssid;
265 ssid = wpa_config_add_network(wpa_s->conf);
268 wpa_config_set_network_defaults(ssid);
269 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
270 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
271 wpa_config_set(ssid, "identity", registrar ?
272 "\"" WSC_ID_REGISTRAR "\"" :
273 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
274 wpa_config_remove_network(wpa_s->conf, ssid->id);
280 struct wpa_scan_res *res;
282 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
285 /* Try to get SSID from scan results */
286 if (wpa_s->scan_res == NULL &&
287 wpa_supplicant_get_scan_results(wpa_s) < 0)
288 return ssid; /* Could not find any scan results */
290 for (i = 0; i < wpa_s->scan_res->num; i++) {
293 res = wpa_s->scan_res->res[i];
294 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
297 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
301 ssid->ssid = os_malloc(ie[1]);
302 if (ssid->ssid == NULL)
304 os_memcpy(ssid->ssid, ie + 2, ie[1]);
305 ssid->ssid_len = ie[1];
314 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
315 struct wpa_ssid *selected)
317 struct wpa_ssid *ssid;
319 /* Mark all other networks disabled and trigger reassociation */
320 ssid = wpa_s->conf->ssid;
322 ssid->disabled = ssid != selected;
325 wpa_s->disconnected = 0;
326 wpa_s->reassociate = 1;
327 wpa_supplicant_req_scan(wpa_s, 0, 0);
331 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
333 struct wpa_ssid *ssid;
334 wpas_clear_wps(wpa_s);
335 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
338 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
339 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
341 wpas_wps_reassoc(wpa_s, ssid);
346 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
349 struct wpa_ssid *ssid;
351 unsigned int rpin = 0;
353 wpas_clear_wps(wpa_s);
354 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
358 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
360 rpin = wps_generate_pin();
361 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
363 wpa_config_set(ssid, "phase1", val, 0);
364 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
366 wpas_wps_reassoc(wpa_s, ssid);
371 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
374 struct wpa_ssid *ssid;
379 wpas_clear_wps(wpa_s);
380 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
383 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
384 wpa_config_set(ssid, "phase1", val, 0);
385 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
387 wpas_wps_reassoc(wpa_s, ssid);
392 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
395 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
396 "STA " MACSTR, MAC2STR(mac_addr));
397 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
405 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
406 const struct wps_device_data *dev)
408 char uuid[40], txt[400];
410 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
412 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
413 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
414 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
415 uuid, MAC2STR(dev->mac_addr), dev->device_name,
416 dev->manufacturer, dev->model_name,
417 dev->model_number, dev->serial_number,
418 dev->categ, dev->oui, dev->sub_categ);
419 if (len > 0 && len < (int) sizeof(txt))
420 wpa_printf(MSG_INFO, "%s", txt);
424 int wpas_wps_init(struct wpa_supplicant *wpa_s)
426 struct wps_context *wps;
427 struct wps_registrar_config rcfg;
429 wps = os_zalloc(sizeof(*wps));
433 wps->cred_cb = wpa_supplicant_wps_cred;
434 wps->event_cb = wpa_supplicant_wps_event;
437 wps->dev.device_name = wpa_s->conf->device_name;
438 wps->dev.manufacturer = wpa_s->conf->manufacturer;
439 wps->dev.model_name = wpa_s->conf->model_name;
440 wps->dev.model_number = wpa_s->conf->model_number;
441 wps->dev.serial_number = wpa_s->conf->serial_number;
442 if (wpa_s->conf->device_type) {
445 /* <categ>-<OUI>-<subcateg> */
446 wps->dev.categ = atoi(wpa_s->conf->device_type);
447 pos = os_strchr(wpa_s->conf->device_type, '-');
449 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
454 if (hexstr2bin(pos, oui, 4)) {
455 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
459 wps->dev.oui = WPA_GET_BE32(oui);
460 pos = os_strchr(pos, '-');
462 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
467 wps->dev.sub_categ = atoi(pos);
469 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
470 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
471 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
472 if (is_nil_uuid(wpa_s->conf->uuid)) {
473 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
474 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
475 wps->uuid, WPS_UUID_LEN);
477 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
479 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
480 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
482 os_memset(&rcfg, 0, sizeof(rcfg));
483 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
484 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
487 wps->registrar = wps_registrar_init(wps, &rcfg);
488 if (wps->registrar == NULL) {
489 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
500 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
502 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
504 if (wpa_s->wps == NULL)
507 wps_registrar_deinit(wpa_s->wps->registrar);
508 os_free(wpa_s->wps->network_key);
514 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
516 struct wpabuf *wps_ie;
518 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
521 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
522 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
524 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
528 if (!wps_is_selected_pbc_registrar(wps_ie)) {
529 wpa_printf(MSG_DEBUG, " skip - WPS AP "
530 "without active PBC Registrar");
535 /* TODO: overlap detection */
536 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
542 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
544 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
548 if (!wps_is_selected_pin_registrar(wps_ie)) {
549 wpa_printf(MSG_DEBUG, " skip - WPS AP "
550 "without active PIN Registrar");
554 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
561 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
570 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
571 struct wpa_scan_res *bss)
573 struct wpabuf *wps_ie = NULL;
576 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
577 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
578 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
579 /* allow wildcard SSID for WPS PBC */
582 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
583 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
584 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
585 /* allow wildcard SSID for WPS PIN */
590 if (!ret && ssid->bssid_set &&
591 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
592 /* allow wildcard SSID due to hardcoded BSSID match */
602 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
603 struct wpa_scan_res *selected,
604 struct wpa_ssid *ssid)
606 const u8 *sel_uuid, *uuid;
608 struct wpabuf *wps_ie;
611 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
614 /* Make sure that only one AP is in active PBC mode */
615 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
617 sel_uuid = wps_get_uuid_e(wps_ie);
621 for (i = 0; i < wpa_s->scan_res->num; i++) {
622 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)) {
633 uuid = wps_get_uuid_e(ie);
634 if (sel_uuid == NULL || uuid == NULL ||
635 os_memcmp(sel_uuid, uuid, 16) != 0) {
636 ret = 1; /* PBC overlap */
641 /* TODO: verify that this is reasonable dual-band situation */
652 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
656 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
659 for (i = 0; i < wpa_s->scan_res->num; i++) {
660 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
662 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
665 if (wps_is_selected_pbc_registrar(ie))
666 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
667 else if (wps_is_selected_pin_registrar(ie))
668 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
670 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
677 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
679 struct wpa_ssid *ssid;
681 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
682 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)