nl80211: Fix a typo
[hostap-gosc2009.git] / wpa_supplicant / wps_supplicant.c
blobfa0123aeb217cf840ea426aa51ffb5a5a8851e29
1 /*
2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008-2010, 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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "eloop.h"
19 #include "uuid.h"
20 #include "crypto/dh_group5.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/ieee802_11_common.h"
23 #include "common/wpa_common.h"
24 #include "common/wpa_ctrl.h"
25 #include "eap_common/eap_wsc_common.h"
26 #include "eap_peer/eap.h"
27 #include "rsn_supp/wpa.h"
28 #include "config.h"
29 #include "wpa_supplicant_i.h"
30 #include "driver_i.h"
31 #include "notify.h"
32 #include "blacklist.h"
33 #include "bss.h"
34 #include "scan.h"
35 #include "wps_supplicant.h"
38 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
41 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
44 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
46 if (!wpa_s->wps_success &&
47 wpa_s->current_ssid &&
48 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
49 const u8 *bssid = wpa_s->bssid;
50 if (is_zero_ether_addr(bssid))
51 bssid = wpa_s->pending_bssid;
53 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
54 " did not succeed - continue trying to find "
55 "suitable AP", MAC2STR(bssid));
56 wpa_blacklist_add(wpa_s, bssid);
58 wpa_supplicant_deauthenticate(wpa_s,
59 WLAN_REASON_DEAUTH_LEAVING);
60 wpa_s->reassociate = 1;
61 wpa_supplicant_req_scan(wpa_s,
62 wpa_s->blacklist_cleared ? 5 : 0, 0);
63 wpa_s->blacklist_cleared = 0;
64 return 1;
67 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
69 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
70 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
71 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
72 "try to associate with the received credential");
73 wpa_supplicant_deauthenticate(wpa_s,
74 WLAN_REASON_DEAUTH_LEAVING);
75 wpa_s->reassociate = 1;
76 wpa_supplicant_req_scan(wpa_s, 0, 0);
77 return 1;
80 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
81 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
82 "for external credential processing");
83 wpas_clear_wps(wpa_s);
84 wpa_supplicant_deauthenticate(wpa_s,
85 WLAN_REASON_DEAUTH_LEAVING);
86 return 1;
89 return 0;
93 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
94 struct wpa_ssid *ssid,
95 const struct wps_credential *cred)
97 struct wpa_driver_capa capa;
98 struct wpa_bss *bss;
99 const u8 *ie;
100 struct wpa_ie_data adv;
101 int wpa2 = 0, ccmp = 0;
104 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
105 * case they are configured for mixed mode operation (WPA+WPA2 and
106 * TKIP+CCMP). Try to use scan results to figure out whether the AP
107 * actually supports stronger security and select that if the client
108 * has support for it, too.
111 if (wpa_drv_get_capa(wpa_s, &capa))
112 return; /* Unknown what driver supports */
114 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
115 if (bss == NULL) {
116 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
117 "table - use credential as-is");
118 return;
121 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
123 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
124 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
125 wpa2 = 1;
126 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
127 ccmp = 1;
128 } else {
129 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
130 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
131 adv.pairwise_cipher & WPA_CIPHER_CCMP)
132 ccmp = 1;
135 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
136 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
138 * TODO: This could be the initial AP configuration and the
139 * Beacon contents could change shortly. Should request a new
140 * scan and delay addition of the network until the updated
141 * scan results are available.
143 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
144 "support - use credential as-is");
145 return;
148 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
149 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
150 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
151 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
152 "based on scan results");
153 if (wpa_s->conf->ap_scan == 1)
154 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
155 else
156 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
159 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
160 (ssid->proto & WPA_PROTO_WPA) &&
161 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
162 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
163 "based on scan results");
164 if (wpa_s->conf->ap_scan == 1)
165 ssid->proto |= WPA_PROTO_RSN;
166 else
167 ssid->proto = WPA_PROTO_RSN;
172 static int wpa_supplicant_wps_cred(void *ctx,
173 const struct wps_credential *cred)
175 struct wpa_supplicant *wpa_s = ctx;
176 struct wpa_ssid *ssid = wpa_s->current_ssid;
177 u8 key_idx = 0;
178 u16 auth_type;
180 if ((wpa_s->conf->wps_cred_processing == 1 ||
181 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
182 size_t blen = cred->cred_attr_len * 2 + 1;
183 char *buf = os_malloc(blen);
184 if (buf) {
185 wpa_snprintf_hex(buf, blen,
186 cred->cred_attr, cred->cred_attr_len);
187 wpa_msg(wpa_s, MSG_INFO, "%s%s",
188 WPS_EVENT_CRED_RECEIVED, buf);
189 os_free(buf);
192 wpas_notify_wps_credential(wpa_s, cred);
193 } else
194 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
196 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
197 cred->cred_attr, cred->cred_attr_len);
199 if (wpa_s->conf->wps_cred_processing == 1)
200 return 0;
202 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
203 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
204 cred->auth_type);
205 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
206 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
207 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
208 cred->key, cred->key_len);
209 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
210 MAC2STR(cred->mac_addr));
212 auth_type = cred->auth_type;
213 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
214 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
215 "auth_type into WPA2PSK");
216 auth_type = WPS_AUTH_WPA2PSK;
219 if (auth_type != WPS_AUTH_OPEN &&
220 auth_type != WPS_AUTH_SHARED &&
221 auth_type != WPS_AUTH_WPAPSK &&
222 auth_type != WPS_AUTH_WPA2PSK) {
223 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
224 "unsupported authentication type 0x%x",
225 auth_type);
226 return 0;
229 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
230 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
231 "on the received credential");
232 os_free(ssid->eap.identity);
233 ssid->eap.identity = NULL;
234 ssid->eap.identity_len = 0;
235 os_free(ssid->eap.phase1);
236 ssid->eap.phase1 = NULL;
237 os_free(ssid->eap.eap_methods);
238 ssid->eap.eap_methods = NULL;
239 } else {
240 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
241 "received credential");
242 ssid = wpa_config_add_network(wpa_s->conf);
243 if (ssid == NULL)
244 return -1;
245 wpas_notify_network_added(wpa_s, ssid);
248 wpa_config_set_network_defaults(ssid);
250 os_free(ssid->ssid);
251 ssid->ssid = os_malloc(cred->ssid_len);
252 if (ssid->ssid) {
253 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
254 ssid->ssid_len = cred->ssid_len;
257 switch (cred->encr_type) {
258 case WPS_ENCR_NONE:
259 break;
260 case WPS_ENCR_WEP:
261 if (cred->key_len <= 0)
262 break;
263 if (cred->key_len != 5 && cred->key_len != 13 &&
264 cred->key_len != 10 && cred->key_len != 26) {
265 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
266 "%lu", (unsigned long) cred->key_len);
267 return -1;
269 if (cred->key_idx > NUM_WEP_KEYS) {
270 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
271 cred->key_idx);
272 return -1;
274 if (cred->key_idx)
275 key_idx = cred->key_idx - 1;
276 if (cred->key_len == 10 || cred->key_len == 26) {
277 if (hexstr2bin((char *) cred->key,
278 ssid->wep_key[key_idx],
279 cred->key_len / 2) < 0) {
280 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
281 "%d", key_idx);
282 return -1;
284 ssid->wep_key_len[key_idx] = cred->key_len / 2;
285 } else {
286 os_memcpy(ssid->wep_key[key_idx], cred->key,
287 cred->key_len);
288 ssid->wep_key_len[key_idx] = cred->key_len;
290 ssid->wep_tx_keyidx = key_idx;
291 break;
292 case WPS_ENCR_TKIP:
293 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
294 break;
295 case WPS_ENCR_AES:
296 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
297 break;
300 switch (auth_type) {
301 case WPS_AUTH_OPEN:
302 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
303 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
304 ssid->proto = 0;
305 break;
306 case WPS_AUTH_SHARED:
307 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
308 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
309 ssid->proto = 0;
310 break;
311 case WPS_AUTH_WPAPSK:
312 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
313 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
314 ssid->proto = WPA_PROTO_WPA;
315 break;
316 case WPS_AUTH_WPA:
317 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
318 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
319 ssid->proto = WPA_PROTO_WPA;
320 break;
321 case WPS_AUTH_WPA2:
322 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
323 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
324 ssid->proto = WPA_PROTO_RSN;
325 break;
326 case WPS_AUTH_WPA2PSK:
327 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
328 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
329 ssid->proto = WPA_PROTO_RSN;
330 break;
333 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
334 if (cred->key_len == 2 * PMK_LEN) {
335 if (hexstr2bin((const char *) cred->key, ssid->psk,
336 PMK_LEN)) {
337 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
338 "Key");
339 return -1;
341 ssid->psk_set = 1;
342 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
343 os_free(ssid->passphrase);
344 ssid->passphrase = os_malloc(cred->key_len + 1);
345 if (ssid->passphrase == NULL)
346 return -1;
347 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
348 ssid->passphrase[cred->key_len] = '\0';
349 wpa_config_update_psk(ssid);
350 } else {
351 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
352 "length %lu",
353 (unsigned long) cred->key_len);
354 return -1;
358 wpas_wps_security_workaround(wpa_s, ssid, cred);
360 #ifndef CONFIG_NO_CONFIG_WRITE
361 if (wpa_s->conf->update_config &&
362 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
363 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
364 return -1;
366 #endif /* CONFIG_NO_CONFIG_WRITE */
368 return 0;
372 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
373 struct wps_event_m2d *m2d)
375 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
376 "dev_password_id=%d config_error=%d",
377 m2d->dev_password_id, m2d->config_error);
378 wpas_notify_wps_event_m2d(wpa_s, m2d);
382 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
383 struct wps_event_fail *fail)
385 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
386 wpas_clear_wps(wpa_s);
387 wpas_notify_wps_event_fail(wpa_s, fail);
391 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
393 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
394 wpa_s->wps_success = 1;
395 wpas_notify_wps_event_success(wpa_s);
399 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
400 struct wps_event_er_ap *ap)
402 char uuid_str[100];
403 char dev_type[WPS_DEV_TYPE_BUFSIZE];
405 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
406 if (ap->pri_dev_type)
407 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
408 sizeof(dev_type));
409 else
410 dev_type[0] = '\0';
412 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
413 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
414 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
415 ap->friendly_name ? ap->friendly_name : "",
416 ap->manufacturer ? ap->manufacturer : "",
417 ap->model_description ? ap->model_description : "",
418 ap->model_name ? ap->model_name : "",
419 ap->manufacturer_url ? ap->manufacturer_url : "",
420 ap->model_url ? ap->model_url : "");
424 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
425 struct wps_event_er_ap *ap)
427 char uuid_str[100];
428 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
429 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
433 static void wpa_supplicant_wps_event_er_enrollee_add(
434 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
436 char uuid_str[100];
437 char dev_type[WPS_DEV_TYPE_BUFSIZE];
439 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
440 if (enrollee->pri_dev_type)
441 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
442 sizeof(dev_type));
443 else
444 dev_type[0] = '\0';
446 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
447 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
448 "|%s|%s|%s|%s|%s|",
449 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
450 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
451 enrollee->dev_name ? enrollee->dev_name : "",
452 enrollee->manufacturer ? enrollee->manufacturer : "",
453 enrollee->model_name ? enrollee->model_name : "",
454 enrollee->model_number ? enrollee->model_number : "",
455 enrollee->serial_number ? enrollee->serial_number : "");
459 static void wpa_supplicant_wps_event_er_enrollee_remove(
460 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
462 char uuid_str[100];
463 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
464 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
465 uuid_str, MAC2STR(enrollee->mac_addr));
469 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
470 union wps_event_data *data)
472 struct wpa_supplicant *wpa_s = ctx;
473 switch (event) {
474 case WPS_EV_M2D:
475 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
476 break;
477 case WPS_EV_FAIL:
478 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
479 break;
480 case WPS_EV_SUCCESS:
481 wpa_supplicant_wps_event_success(wpa_s);
482 break;
483 case WPS_EV_PWD_AUTH_FAIL:
484 break;
485 case WPS_EV_PBC_OVERLAP:
486 break;
487 case WPS_EV_PBC_TIMEOUT:
488 break;
489 case WPS_EV_ER_AP_ADD:
490 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
491 break;
492 case WPS_EV_ER_AP_REMOVE:
493 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
494 break;
495 case WPS_EV_ER_ENROLLEE_ADD:
496 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
497 &data->enrollee);
498 break;
499 case WPS_EV_ER_ENROLLEE_REMOVE:
500 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
501 &data->enrollee);
502 break;
507 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
509 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
510 eap_is_wps_pin_enrollee(&ssid->eap))
511 return WPS_REQ_ENROLLEE;
512 else
513 return WPS_REQ_REGISTRAR;
517 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
519 int id;
520 struct wpa_ssid *ssid, *remove_ssid = NULL;
522 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
524 /* Remove any existing WPS network from configuration */
525 ssid = wpa_s->conf->ssid;
526 while (ssid) {
527 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
528 if (ssid == wpa_s->current_ssid) {
529 wpa_s->current_ssid = NULL;
530 if (ssid != NULL)
531 wpas_notify_network_changed(wpa_s);
533 id = ssid->id;
534 remove_ssid = ssid;
535 } else
536 id = -1;
537 ssid = ssid->next;
538 if (id >= 0) {
539 wpas_notify_network_removed(wpa_s, remove_ssid);
540 wpa_config_remove_network(wpa_s->conf, id);
546 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
548 struct wpa_supplicant *wpa_s = eloop_ctx;
549 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
550 "out");
551 wpas_clear_wps(wpa_s);
555 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
556 int registrar, const u8 *bssid)
558 struct wpa_ssid *ssid;
560 ssid = wpa_config_add_network(wpa_s->conf);
561 if (ssid == NULL)
562 return NULL;
563 wpas_notify_network_added(wpa_s, ssid);
564 wpa_config_set_network_defaults(ssid);
565 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
566 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
567 wpa_config_set(ssid, "identity", registrar ?
568 "\"" WSC_ID_REGISTRAR "\"" :
569 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
570 wpas_notify_network_removed(wpa_s, ssid);
571 wpa_config_remove_network(wpa_s->conf, ssid->id);
572 return NULL;
575 if (bssid) {
576 struct wpa_bss *bss;
577 int count = 0;
579 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
580 ssid->bssid_set = 1;
582 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
583 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
584 continue;
586 os_free(ssid->ssid);
587 ssid->ssid = os_malloc(bss->ssid_len);
588 if (ssid->ssid == NULL)
589 break;
590 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
591 ssid->ssid_len = bss->ssid_len;
592 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
593 "scan results",
594 ssid->ssid, ssid->ssid_len);
595 count++;
598 if (count > 1) {
599 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
600 "for the AP; use wildcard");
601 os_free(ssid->ssid);
602 ssid->ssid = NULL;
603 ssid->ssid_len = 0;
607 return ssid;
611 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
612 struct wpa_ssid *selected)
614 struct wpa_ssid *ssid;
616 /* Mark all other networks disabled and trigger reassociation */
617 ssid = wpa_s->conf->ssid;
618 while (ssid) {
619 int was_disabled = ssid->disabled;
620 ssid->disabled = ssid != selected;
621 if (was_disabled != ssid->disabled)
622 wpas_notify_network_enabled_changed(wpa_s, ssid);
623 ssid = ssid->next;
625 wpa_s->disconnected = 0;
626 wpa_s->reassociate = 1;
627 wpa_s->scan_runs = 0;
628 wpa_s->wps_success = 0;
629 wpa_s->blacklist_cleared = 0;
630 wpa_supplicant_req_scan(wpa_s, 0, 0);
634 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
636 struct wpa_ssid *ssid;
637 wpas_clear_wps(wpa_s);
638 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
639 if (ssid == NULL)
640 return -1;
641 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
642 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
643 wpa_s, NULL);
644 wpas_wps_reassoc(wpa_s, ssid);
645 return 0;
649 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
650 const char *pin)
652 struct wpa_ssid *ssid;
653 char val[128];
654 unsigned int rpin = 0;
656 wpas_clear_wps(wpa_s);
657 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
658 if (ssid == NULL)
659 return -1;
660 if (pin)
661 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
662 else {
663 rpin = wps_generate_pin();
664 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
666 wpa_config_set(ssid, "phase1", val, 0);
667 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
668 wpa_s, NULL);
669 wpas_wps_reassoc(wpa_s, ssid);
670 return rpin;
674 #ifdef CONFIG_WPS_OOB
675 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
676 char *path, char *method, char *name)
678 struct wps_context *wps = wpa_s->wps;
679 struct oob_device_data *oob_dev;
681 oob_dev = wps_get_oob_device(device_type);
682 if (oob_dev == NULL)
683 return -1;
684 oob_dev->device_path = path;
685 oob_dev->device_name = name;
686 wps->oob_conf.oob_method = wps_get_oob_method(method);
688 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
690 * Use pre-configured DH keys in order to be able to write the
691 * key hash into the OOB file.
693 wpabuf_free(wps->dh_pubkey);
694 wpabuf_free(wps->dh_privkey);
695 wps->dh_privkey = NULL;
696 wps->dh_pubkey = NULL;
697 dh5_free(wps->dh_ctx);
698 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
699 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
700 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
701 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
702 "Diffie-Hellman handshake");
703 return -1;
707 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
708 wpas_clear_wps(wpa_s);
710 if (wps_process_oob(wps, oob_dev, 0) < 0)
711 return -1;
713 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
714 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
715 wpas_wps_start_pin(wpa_s, NULL,
716 wpabuf_head(wps->oob_conf.dev_password)) < 0)
717 return -1;
719 return 0;
721 #endif /* CONFIG_WPS_OOB */
724 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
725 const char *pin, struct wps_new_ap_settings *settings)
727 struct wpa_ssid *ssid;
728 char val[200];
729 char *pos, *end;
730 int res;
732 if (!pin)
733 return -1;
734 wpas_clear_wps(wpa_s);
735 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
736 if (ssid == NULL)
737 return -1;
738 pos = val;
739 end = pos + sizeof(val);
740 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
741 if (res < 0 || res >= end - pos)
742 return -1;
743 pos += res;
744 if (settings) {
745 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
746 "new_encr=%s new_key=%s",
747 settings->ssid_hex, settings->auth,
748 settings->encr, settings->key_hex);
749 if (res < 0 || res >= end - pos)
750 return -1;
751 pos += res;
753 res = os_snprintf(pos, end - pos, "\"");
754 if (res < 0 || res >= end - pos)
755 return -1;
756 wpa_config_set(ssid, "phase1", val, 0);
757 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
758 wpa_s, NULL);
759 wpas_wps_reassoc(wpa_s, ssid);
760 return 0;
764 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
765 size_t psk_len)
767 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
768 "STA " MACSTR, MAC2STR(mac_addr));
769 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
771 /* TODO */
773 return 0;
777 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
778 const struct wps_device_data *dev)
780 char uuid[40], txt[400];
781 int len;
782 char devtype[WPS_DEV_TYPE_BUFSIZE];
783 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
784 return;
785 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
786 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
787 " [%s|%s|%s|%s|%s|%s]",
788 uuid, MAC2STR(dev->mac_addr), dev->device_name,
789 dev->manufacturer, dev->model_name,
790 dev->model_number, dev->serial_number,
791 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
792 sizeof(devtype)));
793 if (len > 0 && len < (int) sizeof(txt))
794 wpa_printf(MSG_INFO, "%s", txt);
798 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
799 u16 sel_reg_config_methods)
801 #ifdef CONFIG_WPS_ER
802 struct wpa_supplicant *wpa_s = ctx;
804 if (wpa_s->wps_er == NULL)
805 return;
806 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
807 sel_reg_config_methods);
808 #endif /* CONFIG_WPS_ER */
812 int wpas_wps_init(struct wpa_supplicant *wpa_s)
814 struct wps_context *wps;
815 struct wps_registrar_config rcfg;
817 wps = os_zalloc(sizeof(*wps));
818 if (wps == NULL)
819 return -1;
821 wps->cred_cb = wpa_supplicant_wps_cred;
822 wps->event_cb = wpa_supplicant_wps_event;
823 wps->cb_ctx = wpa_s;
825 wps->dev.device_name = wpa_s->conf->device_name;
826 wps->dev.manufacturer = wpa_s->conf->manufacturer;
827 wps->dev.model_name = wpa_s->conf->model_name;
828 wps->dev.model_number = wpa_s->conf->model_number;
829 wps->dev.serial_number = wpa_s->conf->serial_number;
830 wps->config_methods =
831 wps_config_methods_str2bin(wpa_s->conf->config_methods);
832 if (wpa_s->conf->device_type &&
833 wps_dev_type_str2bin(wpa_s->conf->device_type,
834 wps->dev.pri_dev_type) < 0) {
835 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
836 os_free(wps);
837 return -1;
839 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
840 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
841 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
842 if (is_nil_uuid(wpa_s->conf->uuid)) {
843 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
844 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
845 wps->uuid, WPS_UUID_LEN);
846 } else
847 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
849 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
850 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
852 os_memset(&rcfg, 0, sizeof(rcfg));
853 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
854 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
855 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
856 rcfg.cb_ctx = wpa_s;
858 wps->registrar = wps_registrar_init(wps, &rcfg);
859 if (wps->registrar == NULL) {
860 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
861 os_free(wps);
862 return -1;
865 wpa_s->wps = wps;
867 return 0;
871 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
873 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
875 if (wpa_s->wps == NULL)
876 return;
878 #ifdef CONFIG_WPS_ER
879 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
880 wpa_s->wps_er = NULL;
881 #endif /* CONFIG_WPS_ER */
883 wps_registrar_deinit(wpa_s->wps->registrar);
884 wpabuf_free(wpa_s->wps->dh_pubkey);
885 wpabuf_free(wpa_s->wps->dh_privkey);
886 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
887 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
888 os_free(wpa_s->wps->network_key);
889 os_free(wpa_s->wps);
890 wpa_s->wps = NULL;
894 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
895 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
897 struct wpabuf *wps_ie;
899 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
900 return -1;
902 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
903 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
904 if (!wps_ie) {
905 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
906 return 0;
909 if (!wps_is_selected_pbc_registrar(wps_ie)) {
910 wpa_printf(MSG_DEBUG, " skip - WPS AP "
911 "without active PBC Registrar");
912 wpabuf_free(wps_ie);
913 return 0;
916 /* TODO: overlap detection */
917 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
918 "(Active PBC)");
919 wpabuf_free(wps_ie);
920 return 1;
923 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
924 if (!wps_ie) {
925 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
926 return 0;
930 * Start with WPS APs that advertise active PIN Registrar and
931 * allow any WPS AP after third scan since some APs do not set
932 * Selected Registrar attribute properly when using external
933 * Registrar.
935 if (!wps_is_selected_pin_registrar(wps_ie)) {
936 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
937 wpa_printf(MSG_DEBUG, " skip - WPS AP "
938 "without active PIN Registrar");
939 wpabuf_free(wps_ie);
940 return 0;
942 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
943 } else {
944 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
945 "(Active PIN)");
947 wpabuf_free(wps_ie);
948 return 1;
951 if (wps_ie) {
952 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
953 wpabuf_free(wps_ie);
954 return 1;
957 return -1;
961 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
962 struct wpa_ssid *ssid,
963 struct wpa_scan_res *bss)
965 struct wpabuf *wps_ie = NULL;
966 int ret = 0;
968 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
969 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
970 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
971 /* allow wildcard SSID for WPS PBC */
972 ret = 1;
974 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
975 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
976 if (wps_ie &&
977 (wps_is_selected_pin_registrar(wps_ie) ||
978 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
979 /* allow wildcard SSID for WPS PIN */
980 ret = 1;
984 if (!ret && ssid->bssid_set &&
985 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
986 /* allow wildcard SSID due to hardcoded BSSID match */
987 ret = 1;
990 wpabuf_free(wps_ie);
992 return ret;
996 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
997 struct wpa_bss *selected, struct wpa_ssid *ssid)
999 const u8 *sel_uuid, *uuid;
1000 struct wpabuf *wps_ie;
1001 int ret = 0;
1002 struct wpa_bss *bss;
1004 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1005 return 0;
1007 /* Make sure that only one AP is in active PBC mode */
1008 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1009 if (wps_ie)
1010 sel_uuid = wps_get_uuid_e(wps_ie);
1011 else
1012 sel_uuid = NULL;
1014 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1015 struct wpabuf *ie;
1016 if (bss == selected)
1017 continue;
1018 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1019 if (!ie)
1020 continue;
1021 if (!wps_is_selected_pbc_registrar(ie)) {
1022 wpabuf_free(ie);
1023 continue;
1025 uuid = wps_get_uuid_e(ie);
1026 if (sel_uuid == NULL || uuid == NULL ||
1027 os_memcmp(sel_uuid, uuid, 16) != 0) {
1028 ret = 1; /* PBC overlap */
1029 wpabuf_free(ie);
1030 break;
1033 /* TODO: verify that this is reasonable dual-band situation */
1035 wpabuf_free(ie);
1038 wpabuf_free(wps_ie);
1040 return ret;
1044 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1046 struct wpa_bss *bss;
1048 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1049 return;
1051 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1052 struct wpabuf *ie;
1053 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1054 if (!ie)
1055 continue;
1056 if (wps_is_selected_pbc_registrar(ie))
1057 wpa_msg_ctrl(wpa_s, MSG_INFO,
1058 WPS_EVENT_AP_AVAILABLE_PBC);
1059 else if (wps_is_selected_pin_registrar(ie))
1060 wpa_msg_ctrl(wpa_s, MSG_INFO,
1061 WPS_EVENT_AP_AVAILABLE_PIN);
1062 else
1063 wpa_msg_ctrl(wpa_s, MSG_INFO,
1064 WPS_EVENT_AP_AVAILABLE);
1065 wpabuf_free(ie);
1066 break;
1071 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1073 struct wpa_ssid *ssid;
1075 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1076 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1077 return 1;
1080 return 0;
1084 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1085 char *end)
1087 struct wpabuf *wps_ie;
1088 int ret;
1090 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1091 if (wps_ie == NULL)
1092 return 0;
1094 ret = wps_attr_text(wps_ie, buf, end);
1095 wpabuf_free(wps_ie);
1096 return ret;
1100 int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
1102 #ifdef CONFIG_WPS_ER
1103 if (wpa_s->wps_er) {
1104 wps_er_refresh(wpa_s->wps_er);
1105 return 0;
1107 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
1108 if (wpa_s->wps_er == NULL)
1109 return -1;
1110 return 0;
1111 #else /* CONFIG_WPS_ER */
1112 return 0;
1113 #endif /* CONFIG_WPS_ER */
1117 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1119 #ifdef CONFIG_WPS_ER
1120 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1121 wpa_s->wps_er = NULL;
1122 #endif /* CONFIG_WPS_ER */
1123 return 0;
1127 #ifdef CONFIG_WPS_ER
1128 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const char *uuid,
1129 const char *pin)
1131 u8 u[UUID_LEN];
1132 int any = 0;
1134 if (os_strcmp(uuid, "any") == 0)
1135 any = 1;
1136 else if (uuid_str2bin(uuid, u))
1137 return -1;
1138 return wps_registrar_add_pin(wpa_s->wps->registrar, any ? NULL : u,
1139 (const u8 *) pin, os_strlen(pin), 300);
1143 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1145 u8 u[UUID_LEN];
1147 if (uuid_str2bin(uuid, u))
1148 return -1;
1149 return wps_er_pbc(wpa_s->wps_er, u);
1153 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1154 const char *pin)
1156 u8 u[UUID_LEN];
1158 if (uuid_str2bin(uuid, u))
1159 return -1;
1160 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1161 os_strlen(pin));
1165 static void wpas_wps_terminate_cb(void *ctx)
1167 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1168 eloop_terminate();
1170 #endif /* CONFIG_WPS_ER */
1173 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1175 #ifdef CONFIG_WPS_ER
1176 if (wpa_s->wps_er) {
1177 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1178 wpa_s->wps_er = NULL;
1179 return 1;
1181 #endif /* CONFIG_WPS_ER */
1182 return 0;