blob: 778ccaf37a20c4137b4b689ef31fad2bb1817df4 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008-2010, Jouni Malinen <j@w1.fi>
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
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 "ap.h"
36#include "p2p/p2p.h"
37#include "p2p_supplicant.h"
38#include "wps_supplicant.h"
39
40
41#ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
42#define WPS_PIN_SCAN_IGNORE_SEL_REG 3
43#endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
44
45static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
46static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
47
48
49int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
50{
51 if (!wpa_s->wps_success &&
52 wpa_s->current_ssid &&
53 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
54 const u8 *bssid = wpa_s->bssid;
55 if (is_zero_ether_addr(bssid))
56 bssid = wpa_s->pending_bssid;
57
58 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
59 " did not succeed - continue trying to find "
60 "suitable AP", MAC2STR(bssid));
61 wpa_blacklist_add(wpa_s, bssid);
62
63 wpa_supplicant_deauthenticate(wpa_s,
64 WLAN_REASON_DEAUTH_LEAVING);
65 wpa_s->reassociate = 1;
66 wpa_supplicant_req_scan(wpa_s,
67 wpa_s->blacklist_cleared ? 5 : 0, 0);
68 wpa_s->blacklist_cleared = 0;
69 return 1;
70 }
71
72 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
73 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
74 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
75
76 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
77 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
78 int disabled = wpa_s->current_ssid->disabled;
79 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
80 "try to associate with the received credential");
81 wpa_supplicant_deauthenticate(wpa_s,
82 WLAN_REASON_DEAUTH_LEAVING);
83 if (disabled) {
84 wpa_printf(MSG_DEBUG, "WPS: Current network is "
85 "disabled - wait for user to enable");
86 return 1;
87 }
88 wpa_s->after_wps = 5;
89 wpa_s->wps_freq = wpa_s->assoc_freq;
90 wpa_s->reassociate = 1;
91 wpa_supplicant_req_scan(wpa_s, 0, 0);
92 return 1;
93 }
94
95 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
96 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
97 "for external credential processing");
98 wpas_clear_wps(wpa_s);
99 wpa_supplicant_deauthenticate(wpa_s,
100 WLAN_REASON_DEAUTH_LEAVING);
101 return 1;
102 }
103
104 return 0;
105}
106
107
108static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
109 struct wpa_ssid *ssid,
110 const struct wps_credential *cred)
111{
112 struct wpa_driver_capa capa;
113 struct wpa_bss *bss;
114 const u8 *ie;
115 struct wpa_ie_data adv;
116 int wpa2 = 0, ccmp = 0;
117
118 /*
119 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
120 * case they are configured for mixed mode operation (WPA+WPA2 and
121 * TKIP+CCMP). Try to use scan results to figure out whether the AP
122 * actually supports stronger security and select that if the client
123 * has support for it, too.
124 */
125
126 if (wpa_drv_get_capa(wpa_s, &capa))
127 return; /* Unknown what driver supports */
128
129 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
130 if (bss == NULL) {
131 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
132 "table - use credential as-is");
133 return;
134 }
135
136 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
137
138 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
139 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
140 wpa2 = 1;
141 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
142 ccmp = 1;
143 } else {
144 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
145 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
146 adv.pairwise_cipher & WPA_CIPHER_CCMP)
147 ccmp = 1;
148 }
149
150 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
151 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
152 /*
153 * TODO: This could be the initial AP configuration and the
154 * Beacon contents could change shortly. Should request a new
155 * scan and delay addition of the network until the updated
156 * scan results are available.
157 */
158 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
159 "support - use credential as-is");
160 return;
161 }
162
163 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
164 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
165 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
166 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
167 "based on scan results");
168 if (wpa_s->conf->ap_scan == 1)
169 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
170 else
171 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
172 }
173
174 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
175 (ssid->proto & WPA_PROTO_WPA) &&
176 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
177 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
178 "based on scan results");
179 if (wpa_s->conf->ap_scan == 1)
180 ssid->proto |= WPA_PROTO_RSN;
181 else
182 ssid->proto = WPA_PROTO_RSN;
183 }
184}
185
186
187static int wpa_supplicant_wps_cred(void *ctx,
188 const struct wps_credential *cred)
189{
190 struct wpa_supplicant *wpa_s = ctx;
191 struct wpa_ssid *ssid = wpa_s->current_ssid;
192 u8 key_idx = 0;
193 u16 auth_type;
194 int registrar = 0;
195
196 if ((wpa_s->conf->wps_cred_processing == 1 ||
197 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
198 size_t blen = cred->cred_attr_len * 2 + 1;
199 char *buf = os_malloc(blen);
200 if (buf) {
201 wpa_snprintf_hex(buf, blen,
202 cred->cred_attr, cred->cred_attr_len);
203 wpa_msg(wpa_s, MSG_INFO, "%s%s",
204 WPS_EVENT_CRED_RECEIVED, buf);
205 os_free(buf);
206 }
207
208 wpas_notify_wps_credential(wpa_s, cred);
209 } else
210 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
211
212 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
213 cred->cred_attr, cred->cred_attr_len);
214
215 if (wpa_s->conf->wps_cred_processing == 1)
216 return 0;
217
218 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
219 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
220 cred->auth_type);
221 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
222 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
223 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
224 cred->key, cred->key_len);
225 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
226 MAC2STR(cred->mac_addr));
227
228 auth_type = cred->auth_type;
229 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
230 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
231 "auth_type into WPA2PSK");
232 auth_type = WPS_AUTH_WPA2PSK;
233 }
234
235 if (auth_type != WPS_AUTH_OPEN &&
236 auth_type != WPS_AUTH_SHARED &&
237 auth_type != WPS_AUTH_WPAPSK &&
238 auth_type != WPS_AUTH_WPA2PSK) {
239 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
240 "unsupported authentication type 0x%x",
241 auth_type);
242 return 0;
243 }
244
245 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
246 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
247 "on the received credential");
248 if (ssid->eap.identity &&
249 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
250 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
251 WSC_ID_REGISTRAR_LEN) == 0)
252 registrar = 1;
253 os_free(ssid->eap.identity);
254 ssid->eap.identity = NULL;
255 ssid->eap.identity_len = 0;
256 os_free(ssid->eap.phase1);
257 ssid->eap.phase1 = NULL;
258 os_free(ssid->eap.eap_methods);
259 ssid->eap.eap_methods = NULL;
260 if (!ssid->p2p_group)
261 ssid->temporary = 0;
262 } else {
263 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
264 "received credential");
265 ssid = wpa_config_add_network(wpa_s->conf);
266 if (ssid == NULL)
267 return -1;
268 wpas_notify_network_added(wpa_s, ssid);
269 }
270
271 wpa_config_set_network_defaults(ssid);
272
273 os_free(ssid->ssid);
274 ssid->ssid = os_malloc(cred->ssid_len);
275 if (ssid->ssid) {
276 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
277 ssid->ssid_len = cred->ssid_len;
278 }
279
280 switch (cred->encr_type) {
281 case WPS_ENCR_NONE:
282 break;
283 case WPS_ENCR_WEP:
284 if (cred->key_len <= 0)
285 break;
286 if (cred->key_len != 5 && cred->key_len != 13 &&
287 cred->key_len != 10 && cred->key_len != 26) {
288 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
289 "%lu", (unsigned long) cred->key_len);
290 return -1;
291 }
292 if (cred->key_idx > NUM_WEP_KEYS) {
293 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
294 cred->key_idx);
295 return -1;
296 }
297 if (cred->key_idx)
298 key_idx = cred->key_idx - 1;
299 if (cred->key_len == 10 || cred->key_len == 26) {
300 if (hexstr2bin((char *) cred->key,
301 ssid->wep_key[key_idx],
302 cred->key_len / 2) < 0) {
303 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
304 "%d", key_idx);
305 return -1;
306 }
307 ssid->wep_key_len[key_idx] = cred->key_len / 2;
308 } else {
309 os_memcpy(ssid->wep_key[key_idx], cred->key,
310 cred->key_len);
311 ssid->wep_key_len[key_idx] = cred->key_len;
312 }
313 ssid->wep_tx_keyidx = key_idx;
314 break;
315 case WPS_ENCR_TKIP:
316 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
317 break;
318 case WPS_ENCR_AES:
319 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
320 break;
321 }
322
323 switch (auth_type) {
324 case WPS_AUTH_OPEN:
325 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
326 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
327 ssid->proto = 0;
328#ifdef CONFIG_WPS_REG_DISABLE_OPEN
329 if (registrar) {
330 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
331 "id=%d - Credentials for an open "
332 "network disabled by default - use "
333 "'select_network %d' to enable",
334 ssid->id, ssid->id);
335 ssid->disabled = 1;
336 }
337#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
338 break;
339 case WPS_AUTH_SHARED:
340 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
341 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
342 ssid->proto = 0;
343 break;
344 case WPS_AUTH_WPAPSK:
345 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
346 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
347 ssid->proto = WPA_PROTO_WPA;
348 break;
349 case WPS_AUTH_WPA:
350 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
351 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
352 ssid->proto = WPA_PROTO_WPA;
353 break;
354 case WPS_AUTH_WPA2:
355 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
356 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
357 ssid->proto = WPA_PROTO_RSN;
358 break;
359 case WPS_AUTH_WPA2PSK:
360 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
361 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
362 ssid->proto = WPA_PROTO_RSN;
363 break;
364 }
365
366 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
367 if (cred->key_len == 2 * PMK_LEN) {
368 if (hexstr2bin((const char *) cred->key, ssid->psk,
369 PMK_LEN)) {
370 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
371 "Key");
372 return -1;
373 }
374 ssid->psk_set = 1;
375 ssid->export_keys = 1;
376 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
377 os_free(ssid->passphrase);
378 ssid->passphrase = os_malloc(cred->key_len + 1);
379 if (ssid->passphrase == NULL)
380 return -1;
381 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
382 ssid->passphrase[cred->key_len] = '\0';
383 wpa_config_update_psk(ssid);
384 ssid->export_keys = 1;
385 } else {
386 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
387 "length %lu",
388 (unsigned long) cred->key_len);
389 return -1;
390 }
391 }
392
393 wpas_wps_security_workaround(wpa_s, ssid, cred);
394
395#ifndef CONFIG_NO_CONFIG_WRITE
396 if (wpa_s->conf->update_config &&
397 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
398 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
399 return -1;
400 }
401#endif /* CONFIG_NO_CONFIG_WRITE */
402
403 return 0;
404}
405
406
407#ifdef CONFIG_P2P
408static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
409{
410 struct wpa_supplicant *wpa_s = eloop_ctx;
411 wpas_p2p_notif_pbc_overlap(wpa_s);
412}
413#endif /* CONFIG_P2P */
414
415
416static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
417 struct wps_event_m2d *m2d)
418{
419 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
420 "dev_password_id=%d config_error=%d",
421 m2d->dev_password_id, m2d->config_error);
422 wpas_notify_wps_event_m2d(wpa_s, m2d);
423#ifdef CONFIG_P2P
424 if (wpa_s->parent && wpa_s->parent != wpa_s) {
425 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
426 "dev_password_id=%d config_error=%d",
427 m2d->dev_password_id, m2d->config_error);
428 }
429 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
430 /*
431 * Notify P2P from eloop timeout to avoid issues with the
432 * interface getting removed while processing a message.
433 */
434 eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
435 NULL);
436 }
437#endif /* CONFIG_P2P */
438}
439
440
441static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
442 "No Error", /* WPS_EI_NO_ERROR */
443 "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
444 "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
445};
446
447static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
448 struct wps_event_fail *fail)
449{
450 if (fail->error_indication > 0 &&
451 fail->error_indication < NUM_WPS_EI_VALUES) {
452 wpa_msg(wpa_s, MSG_INFO,
453 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
454 fail->msg, fail->config_error, fail->error_indication,
455 wps_event_fail_reason[fail->error_indication]);
456 if (wpa_s->parent && wpa_s->parent != wpa_s)
457 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
458 "msg=%d config_error=%d reason=%d (%s)",
459 fail->msg, fail->config_error,
460 fail->error_indication,
461 wps_event_fail_reason[fail->error_indication]);
462 } else {
463 wpa_msg(wpa_s, MSG_INFO,
464 WPS_EVENT_FAIL "msg=%d config_error=%d",
465 fail->msg, fail->config_error);
466 if (wpa_s->parent && wpa_s->parent != wpa_s)
467 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
468 "msg=%d config_error=%d",
469 fail->msg, fail->config_error);
470 }
471 wpas_clear_wps(wpa_s);
472 wpas_notify_wps_event_fail(wpa_s, fail);
473}
474
475
476static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
477{
478 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
479 wpa_s->wps_success = 1;
480 wpas_notify_wps_event_success(wpa_s);
481#ifdef CONFIG_P2P
482 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
483#endif /* CONFIG_P2P */
484}
485
486
487static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
488 struct wps_event_er_ap *ap)
489{
490 char uuid_str[100];
491 char dev_type[WPS_DEV_TYPE_BUFSIZE];
492
493 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
494 if (ap->pri_dev_type)
495 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
496 sizeof(dev_type));
497 else
498 dev_type[0] = '\0';
499
500 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
501 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
502 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
503 ap->friendly_name ? ap->friendly_name : "",
504 ap->manufacturer ? ap->manufacturer : "",
505 ap->model_description ? ap->model_description : "",
506 ap->model_name ? ap->model_name : "",
507 ap->manufacturer_url ? ap->manufacturer_url : "",
508 ap->model_url ? ap->model_url : "");
509}
510
511
512static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
513 struct wps_event_er_ap *ap)
514{
515 char uuid_str[100];
516 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
517 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
518}
519
520
521static void wpa_supplicant_wps_event_er_enrollee_add(
522 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
523{
524 char uuid_str[100];
525 char dev_type[WPS_DEV_TYPE_BUFSIZE];
526
527 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
528 if (enrollee->pri_dev_type)
529 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
530 sizeof(dev_type));
531 else
532 dev_type[0] = '\0';
533
534 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
535 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
536 "|%s|%s|%s|%s|%s|",
537 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
538 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
539 enrollee->dev_name ? enrollee->dev_name : "",
540 enrollee->manufacturer ? enrollee->manufacturer : "",
541 enrollee->model_name ? enrollee->model_name : "",
542 enrollee->model_number ? enrollee->model_number : "",
543 enrollee->serial_number ? enrollee->serial_number : "");
544}
545
546
547static void wpa_supplicant_wps_event_er_enrollee_remove(
548 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
549{
550 char uuid_str[100];
551 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
552 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
553 uuid_str, MAC2STR(enrollee->mac_addr));
554}
555
556
557static void wpa_supplicant_wps_event_er_ap_settings(
558 struct wpa_supplicant *wpa_s,
559 struct wps_event_er_ap_settings *ap_settings)
560{
561 char uuid_str[100];
562 char key_str[65];
563 const struct wps_credential *cred = ap_settings->cred;
564
565 key_str[0] = '\0';
566 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
567 if (cred->key_len >= 8 && cred->key_len <= 64) {
568 os_memcpy(key_str, cred->key, cred->key_len);
569 key_str[cred->key_len] = '\0';
570 }
571 }
572
573 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
574 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
575 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
576 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
577 "key=%s",
578 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
579 cred->auth_type, cred->encr_type, key_str);
580}
581
582
583static void wpa_supplicant_wps_event_er_set_sel_reg(
584 struct wpa_supplicant *wpa_s,
585 struct wps_event_er_set_selected_registrar *ev)
586{
587 char uuid_str[100];
588
589 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
590 switch (ev->state) {
591 case WPS_ER_SET_SEL_REG_START:
592 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
593 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
594 "sel_reg_config_methods=0x%x",
595 uuid_str, ev->sel_reg, ev->dev_passwd_id,
596 ev->sel_reg_config_methods);
597 break;
598 case WPS_ER_SET_SEL_REG_DONE:
599 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
600 "uuid=%s state=DONE", uuid_str);
601 break;
602 case WPS_ER_SET_SEL_REG_FAILED:
603 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
604 "uuid=%s state=FAILED", uuid_str);
605 break;
606 }
607}
608
609
610static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
611 union wps_event_data *data)
612{
613 struct wpa_supplicant *wpa_s = ctx;
614 switch (event) {
615 case WPS_EV_M2D:
616 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
617 break;
618 case WPS_EV_FAIL:
619 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
620 break;
621 case WPS_EV_SUCCESS:
622 wpa_supplicant_wps_event_success(wpa_s);
623 break;
624 case WPS_EV_PWD_AUTH_FAIL:
625#ifdef CONFIG_AP
626 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
627 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
628#endif /* CONFIG_AP */
629 break;
630 case WPS_EV_PBC_OVERLAP:
631 break;
632 case WPS_EV_PBC_TIMEOUT:
633 break;
634 case WPS_EV_ER_AP_ADD:
635 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
636 break;
637 case WPS_EV_ER_AP_REMOVE:
638 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
639 break;
640 case WPS_EV_ER_ENROLLEE_ADD:
641 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
642 &data->enrollee);
643 break;
644 case WPS_EV_ER_ENROLLEE_REMOVE:
645 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
646 &data->enrollee);
647 break;
648 case WPS_EV_ER_AP_SETTINGS:
649 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
650 &data->ap_settings);
651 break;
652 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
653 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
654 &data->set_sel_reg);
655 break;
656 }
657}
658
659
660enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
661{
662 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
663 eap_is_wps_pin_enrollee(&ssid->eap))
664 return WPS_REQ_ENROLLEE;
665 else
666 return WPS_REQ_REGISTRAR;
667}
668
669
670static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
671{
672 int id;
673 struct wpa_ssid *ssid, *remove_ssid = NULL;
674
675 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
676
677 /* Remove any existing WPS network from configuration */
678 ssid = wpa_s->conf->ssid;
679 while (ssid) {
680 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
681 if (ssid == wpa_s->current_ssid) {
682 wpa_s->current_ssid = NULL;
683 if (ssid != NULL)
684 wpas_notify_network_changed(wpa_s);
685 }
686 id = ssid->id;
687 remove_ssid = ssid;
688 } else
689 id = -1;
690 ssid = ssid->next;
691 if (id >= 0) {
692 wpas_notify_network_removed(wpa_s, remove_ssid);
693 wpa_config_remove_network(wpa_s->conf, id);
694 }
695 }
696}
697
698
699static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
700{
701 struct wpa_supplicant *wpa_s = eloop_ctx;
702 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
703 "out");
704 wpas_clear_wps(wpa_s);
705}
706
707
708static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
709 int registrar, const u8 *bssid)
710{
711 struct wpa_ssid *ssid;
712
713 ssid = wpa_config_add_network(wpa_s->conf);
714 if (ssid == NULL)
715 return NULL;
716 wpas_notify_network_added(wpa_s, ssid);
717 wpa_config_set_network_defaults(ssid);
718 ssid->temporary = 1;
719 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
720 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
721 wpa_config_set(ssid, "identity", registrar ?
722 "\"" WSC_ID_REGISTRAR "\"" :
723 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
724 wpas_notify_network_removed(wpa_s, ssid);
725 wpa_config_remove_network(wpa_s->conf, ssid->id);
726 return NULL;
727 }
728
729 if (bssid) {
730#ifndef CONFIG_P2P
731 struct wpa_bss *bss;
732 int count = 0;
733#endif /* CONFIG_P2P */
734
735 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
736 ssid->bssid_set = 1;
737
738 /*
739 * Note: With P2P, the SSID may change at the time the WPS
740 * provisioning is started, so better not filter the AP based
741 * on the current SSID in the scan results.
742 */
743#ifndef CONFIG_P2P
744 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
745 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
746 continue;
747
748 os_free(ssid->ssid);
749 ssid->ssid = os_malloc(bss->ssid_len);
750 if (ssid->ssid == NULL)
751 break;
752 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
753 ssid->ssid_len = bss->ssid_len;
754 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
755 "scan results",
756 ssid->ssid, ssid->ssid_len);
757 count++;
758 }
759
760 if (count > 1) {
761 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
762 "for the AP; use wildcard");
763 os_free(ssid->ssid);
764 ssid->ssid = NULL;
765 ssid->ssid_len = 0;
766 }
767#endif /* CONFIG_P2P */
768 }
769
770 return ssid;
771}
772
773
774static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
775 struct wpa_ssid *selected)
776{
777 struct wpa_ssid *ssid;
778
779 /* Mark all other networks disabled and trigger reassociation */
780 ssid = wpa_s->conf->ssid;
781 while (ssid) {
782 int was_disabled = ssid->disabled;
783 ssid->disabled = ssid != selected;
784 if (was_disabled != ssid->disabled)
785 wpas_notify_network_enabled_changed(wpa_s, ssid);
786 ssid = ssid->next;
787 }
788 wpa_s->disconnected = 0;
789 wpa_s->reassociate = 1;
790 wpa_s->scan_runs = 0;
791 wpa_s->wps_success = 0;
792 wpa_s->blacklist_cleared = 0;
793 wpa_supplicant_req_scan(wpa_s, 0, 0);
794}
795
796
797int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
798 int p2p_group)
799{
800 struct wpa_ssid *ssid;
801 wpas_clear_wps(wpa_s);
802 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
803 if (ssid == NULL)
804 return -1;
805 ssid->temporary = 1;
806 ssid->p2p_group = p2p_group;
807#ifdef CONFIG_P2P
808 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
809 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
810 if (ssid->ssid) {
811 ssid->ssid_len = wpa_s->go_params->ssid_len;
812 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
813 ssid->ssid_len);
814 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
815 "SSID", ssid->ssid, ssid->ssid_len);
816 }
817 }
818#endif /* CONFIG_P2P */
819 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
820 if (wpa_s->wps_fragment_size)
821 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
822 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
823 wpa_s, NULL);
824 wpas_wps_reassoc(wpa_s, ssid);
825 return 0;
826}
827
828
829int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
830 const char *pin, int p2p_group, u16 dev_pw_id)
831{
832 struct wpa_ssid *ssid;
833 char val[128];
834 unsigned int rpin = 0;
835
836 wpas_clear_wps(wpa_s);
837 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
838 if (ssid == NULL)
839 return -1;
840 ssid->temporary = 1;
841 ssid->p2p_group = p2p_group;
842#ifdef CONFIG_P2P
843 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
844 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
845 if (ssid->ssid) {
846 ssid->ssid_len = wpa_s->go_params->ssid_len;
847 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
848 ssid->ssid_len);
849 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
850 "SSID", ssid->ssid, ssid->ssid_len);
851 }
852 }
853#endif /* CONFIG_P2P */
854 if (pin)
855 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
856 pin, dev_pw_id);
857 else {
858 rpin = wps_generate_pin();
859 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
860 rpin, dev_pw_id);
861 }
862 wpa_config_set(ssid, "phase1", val, 0);
863 if (wpa_s->wps_fragment_size)
864 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
865 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
866 wpa_s, NULL);
867 wpas_wps_reassoc(wpa_s, ssid);
868 return rpin;
869}
870
871
872/* Cancel the wps pbc/pin requests */
873int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
874{
875#ifdef CONFIG_AP
876 if (wpa_s->ap_iface) {
877 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
878 return wpa_supplicant_ap_wps_cancel(wpa_s);
879 }
880#endif /* CONFIG_AP */
881
882 if (wpa_s->wpa_state == WPA_SCANNING) {
883 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
884 wpa_supplicant_cancel_scan(wpa_s);
885 wpas_clear_wps(wpa_s);
886 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
887 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
888 "deauthenticate");
889 wpa_supplicant_deauthenticate(wpa_s,
890 WLAN_REASON_DEAUTH_LEAVING);
891 wpas_clear_wps(wpa_s);
892 }
893
894 return 0;
895}
896
897
898#ifdef CONFIG_WPS_OOB
899int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
900 char *path, char *method, char *name)
901{
902 struct wps_context *wps = wpa_s->wps;
903 struct oob_device_data *oob_dev;
904
905 oob_dev = wps_get_oob_device(device_type);
906 if (oob_dev == NULL)
907 return -1;
908 oob_dev->device_path = path;
909 oob_dev->device_name = name;
910 wps->oob_conf.oob_method = wps_get_oob_method(method);
911
912 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
913 /*
914 * Use pre-configured DH keys in order to be able to write the
915 * key hash into the OOB file.
916 */
917 wpabuf_free(wps->dh_pubkey);
918 wpabuf_free(wps->dh_privkey);
919 wps->dh_privkey = NULL;
920 wps->dh_pubkey = NULL;
921 dh5_free(wps->dh_ctx);
922 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
923 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
924 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
925 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
926 "Diffie-Hellman handshake");
927 return -1;
928 }
929 }
930
931 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
932 wpas_clear_wps(wpa_s);
933
934 if (wps_process_oob(wps, oob_dev, 0) < 0)
935 return -1;
936
937 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
938 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
939 wpas_wps_start_pin(wpa_s, NULL,
940 wpabuf_head(wps->oob_conf.dev_password), 0,
941 DEV_PW_DEFAULT) < 0)
942 return -1;
943
944 return 0;
945}
946#endif /* CONFIG_WPS_OOB */
947
948
949int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
950 const char *pin, struct wps_new_ap_settings *settings)
951{
952 struct wpa_ssid *ssid;
953 char val[200];
954 char *pos, *end;
955 int res;
956
957 if (!pin)
958 return -1;
959 wpas_clear_wps(wpa_s);
960 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
961 if (ssid == NULL)
962 return -1;
963 ssid->temporary = 1;
964 pos = val;
965 end = pos + sizeof(val);
966 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
967 if (res < 0 || res >= end - pos)
968 return -1;
969 pos += res;
970 if (settings) {
971 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
972 "new_encr=%s new_key=%s",
973 settings->ssid_hex, settings->auth,
974 settings->encr, settings->key_hex);
975 if (res < 0 || res >= end - pos)
976 return -1;
977 pos += res;
978 }
979 res = os_snprintf(pos, end - pos, "\"");
980 if (res < 0 || res >= end - pos)
981 return -1;
982 wpa_config_set(ssid, "phase1", val, 0);
983 if (wpa_s->wps_fragment_size)
984 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
985 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
986 wpa_s, NULL);
987 wpas_wps_reassoc(wpa_s, ssid);
988 return 0;
989}
990
991
992static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
993 size_t psk_len)
994{
995 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
996 "STA " MACSTR, MAC2STR(mac_addr));
997 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
998
999 /* TODO */
1000
1001 return 0;
1002}
1003
1004
1005static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1006 const struct wps_device_data *dev)
1007{
1008 char uuid[40], txt[400];
1009 int len;
1010 char devtype[WPS_DEV_TYPE_BUFSIZE];
1011 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1012 return;
1013 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1014 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1015 " [%s|%s|%s|%s|%s|%s]",
1016 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1017 dev->manufacturer, dev->model_name,
1018 dev->model_number, dev->serial_number,
1019 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1020 sizeof(devtype)));
1021 if (len > 0 && len < (int) sizeof(txt))
1022 wpa_printf(MSG_INFO, "%s", txt);
1023}
1024
1025
1026static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1027 u16 sel_reg_config_methods)
1028{
1029#ifdef CONFIG_WPS_ER
1030 struct wpa_supplicant *wpa_s = ctx;
1031
1032 if (wpa_s->wps_er == NULL)
1033 return;
1034 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1035 "dev_password_id=%u sel_reg_config_methods=0x%x",
1036 sel_reg, dev_passwd_id, sel_reg_config_methods);
1037 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1038 sel_reg_config_methods);
1039#endif /* CONFIG_WPS_ER */
1040}
1041
1042
1043static u16 wps_fix_config_methods(u16 config_methods)
1044{
1045#ifdef CONFIG_WPS2
1046 if ((config_methods &
1047 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1048 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1049 wpa_printf(MSG_INFO, "WPS: Converting display to "
1050 "virtual_display for WPS 2.0 compliance");
1051 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1052 }
1053 if ((config_methods &
1054 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1055 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1056 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1057 "virtual_push_button for WPS 2.0 compliance");
1058 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1059 }
1060#endif /* CONFIG_WPS2 */
1061
1062 return config_methods;
1063}
1064
1065
1066static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1067 struct wps_context *wps)
1068{
1069 wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
1070 if (is_nil_uuid(wpa_s->conf->uuid)) {
1071 struct wpa_supplicant *first;
1072 first = wpa_s->global->ifaces;
1073 while (first && first->next)
1074 first = first->next;
1075 if (first && first != wpa_s) {
1076 os_memcpy(wps->uuid, wpa_s->global->ifaces->wps->uuid,
1077 WPS_UUID_LEN);
1078 wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
1079 "interface", wps->uuid, WPS_UUID_LEN);
1080 } else {
1081 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1082 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1083 "address", wps->uuid, WPS_UUID_LEN);
1084 }
1085 } else {
1086 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1087 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
1088 wps->uuid, WPS_UUID_LEN);
1089 }
1090}
1091
1092
1093int wpas_wps_init(struct wpa_supplicant *wpa_s)
1094{
1095 struct wps_context *wps;
1096 struct wps_registrar_config rcfg;
1097
1098 wps = os_zalloc(sizeof(*wps));
1099 if (wps == NULL)
1100 return -1;
1101
1102 wps->cred_cb = wpa_supplicant_wps_cred;
1103 wps->event_cb = wpa_supplicant_wps_event;
1104 wps->cb_ctx = wpa_s;
1105
1106 wps->dev.device_name = wpa_s->conf->device_name;
1107 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1108 wps->dev.model_name = wpa_s->conf->model_name;
1109 wps->dev.model_number = wpa_s->conf->model_number;
1110 wps->dev.serial_number = wpa_s->conf->serial_number;
1111 wps->config_methods =
1112 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1113 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1114 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1115 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1116 "methods are not allowed at the same time");
1117 os_free(wps);
1118 return -1;
1119 }
1120 wps->config_methods = wps_fix_config_methods(wps->config_methods);
1121 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1122 WPS_DEV_TYPE_LEN);
1123
1124 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1125 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1126 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1127
1128 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1129 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
1130 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1131 wpas_wps_set_uuid(wpa_s, wps);
1132
1133 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1134 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1135
1136 os_memset(&rcfg, 0, sizeof(rcfg));
1137 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1138 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1139 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1140 rcfg.cb_ctx = wpa_s;
1141
1142 wps->registrar = wps_registrar_init(wps, &rcfg);
1143 if (wps->registrar == NULL) {
1144 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1145 os_free(wps);
1146 return -1;
1147 }
1148
1149 wpa_s->wps = wps;
1150
1151 return 0;
1152}
1153
1154
1155void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1156{
1157 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1158
1159 if (wpa_s->wps == NULL)
1160 return;
1161
1162#ifdef CONFIG_WPS_ER
1163 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1164 wpa_s->wps_er = NULL;
1165#endif /* CONFIG_WPS_ER */
1166
1167 wps_registrar_deinit(wpa_s->wps->registrar);
1168 wpabuf_free(wpa_s->wps->dh_pubkey);
1169 wpabuf_free(wpa_s->wps->dh_privkey);
1170 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1171 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1172 os_free(wpa_s->wps->network_key);
1173 os_free(wpa_s->wps);
1174 wpa_s->wps = NULL;
1175}
1176
1177
1178int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1179 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1180{
1181 struct wpabuf *wps_ie;
1182
1183 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1184 return -1;
1185
1186 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1187 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1188 if (!wps_ie) {
1189 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1190 return 0;
1191 }
1192
1193 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1194 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1195 "without active PBC Registrar");
1196 wpabuf_free(wps_ie);
1197 return 0;
1198 }
1199
1200 /* TODO: overlap detection */
1201 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1202 "(Active PBC)");
1203 wpabuf_free(wps_ie);
1204 return 1;
1205 }
1206
1207 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1208 if (!wps_ie) {
1209 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1210 return 0;
1211 }
1212
1213 /*
1214 * Start with WPS APs that advertise our address as an
1215 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1216 * allow any WPS AP after couple of scans since some APs do not
1217 * set Selected Registrar attribute properly when using
1218 * external Registrar.
1219 */
1220 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1221 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1222 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1223 "without active PIN Registrar");
1224 wpabuf_free(wps_ie);
1225 return 0;
1226 }
1227 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1228 } else {
1229 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1230 "(Authorized MAC or Active PIN)");
1231 }
1232 wpabuf_free(wps_ie);
1233 return 1;
1234 }
1235
1236 if (wps_ie) {
1237 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1238 wpabuf_free(wps_ie);
1239 return 1;
1240 }
1241
1242 return -1;
1243}
1244
1245
1246int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1247 struct wpa_ssid *ssid,
1248 struct wpa_scan_res *bss)
1249{
1250 struct wpabuf *wps_ie = NULL;
1251 int ret = 0;
1252
1253 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1254 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1255 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1256 /* allow wildcard SSID for WPS PBC */
1257 ret = 1;
1258 }
1259 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1260 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1261 if (wps_ie &&
1262 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1263 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1264 /* allow wildcard SSID for WPS PIN */
1265 ret = 1;
1266 }
1267 }
1268
1269 if (!ret && ssid->bssid_set &&
1270 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1271 /* allow wildcard SSID due to hardcoded BSSID match */
1272 ret = 1;
1273 }
1274
1275#ifdef CONFIG_WPS_STRICT
1276 if (wps_ie) {
1277 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1278 0, bss->bssid) < 0)
1279 ret = 0;
1280 if (bss->beacon_ie_len) {
1281 struct wpabuf *bcn_wps;
1282 bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1283 bss, WPS_IE_VENDOR_TYPE);
1284 if (bcn_wps == NULL) {
1285 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1286 "missing from AP Beacon");
1287 ret = 0;
1288 } else {
1289 if (wps_validate_beacon(wps_ie) < 0)
1290 ret = 0;
1291 wpabuf_free(bcn_wps);
1292 }
1293 }
1294 }
1295#endif /* CONFIG_WPS_STRICT */
1296
1297 wpabuf_free(wps_ie);
1298
1299 return ret;
1300}
1301
1302
1303int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1304 struct wpa_bss *selected, struct wpa_ssid *ssid)
1305{
1306 const u8 *sel_uuid, *uuid;
1307 struct wpabuf *wps_ie;
1308 int ret = 0;
1309 struct wpa_bss *bss;
1310
1311 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1312 return 0;
1313
1314 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1315 "present in scan results; selected BSSID " MACSTR,
1316 MAC2STR(selected->bssid));
1317
1318 /* Make sure that only one AP is in active PBC mode */
1319 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1320 if (wps_ie) {
1321 sel_uuid = wps_get_uuid_e(wps_ie);
1322 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1323 sel_uuid, UUID_LEN);
1324 } else {
1325 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1326 "WPS IE?!");
1327 sel_uuid = NULL;
1328 }
1329
1330 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1331 struct wpabuf *ie;
1332 if (bss == selected)
1333 continue;
1334 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1335 if (!ie)
1336 continue;
1337 if (!wps_is_selected_pbc_registrar(ie)) {
1338 wpabuf_free(ie);
1339 continue;
1340 }
1341 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1342 MACSTR, MAC2STR(bss->bssid));
1343 uuid = wps_get_uuid_e(ie);
1344 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1345 uuid, UUID_LEN);
1346 if (sel_uuid == NULL || uuid == NULL ||
1347 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1348 ret = 1; /* PBC overlap */
1349 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1350 MACSTR " and " MACSTR,
1351 MAC2STR(selected->bssid),
1352 MAC2STR(bss->bssid));
1353 wpabuf_free(ie);
1354 break;
1355 }
1356
1357 /* TODO: verify that this is reasonable dual-band situation */
1358
1359 wpabuf_free(ie);
1360 }
1361
1362 wpabuf_free(wps_ie);
1363
1364 return ret;
1365}
1366
1367
1368void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1369{
1370 struct wpa_bss *bss;
1371 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1372
1373 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1374 return;
1375
1376 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1377 struct wpabuf *ie;
1378 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1379 if (!ie)
1380 continue;
1381 if (wps_is_selected_pbc_registrar(ie))
1382 pbc++;
1383 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1384 auth++;
1385 else if (wps_is_selected_pin_registrar(ie))
1386 pin++;
1387 else
1388 wps++;
1389 wpabuf_free(ie);
1390 }
1391
1392 if (pbc)
1393 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1394 else if (auth)
1395 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1396 else if (pin)
1397 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1398 else if (wps)
1399 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1400}
1401
1402
1403int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1404{
1405 struct wpa_ssid *ssid;
1406
1407 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1408 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1409 return 1;
1410 }
1411
1412 return 0;
1413}
1414
1415
1416int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1417 char *end)
1418{
1419 struct wpabuf *wps_ie;
1420 int ret;
1421
1422 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1423 if (wps_ie == NULL)
1424 return 0;
1425
1426 ret = wps_attr_text(wps_ie, buf, end);
1427 wpabuf_free(wps_ie);
1428 return ret;
1429}
1430
1431
1432int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1433{
1434#ifdef CONFIG_WPS_ER
1435 if (wpa_s->wps_er) {
1436 wps_er_refresh(wpa_s->wps_er);
1437 return 0;
1438 }
1439 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1440 if (wpa_s->wps_er == NULL)
1441 return -1;
1442 return 0;
1443#else /* CONFIG_WPS_ER */
1444 return 0;
1445#endif /* CONFIG_WPS_ER */
1446}
1447
1448
1449int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1450{
1451#ifdef CONFIG_WPS_ER
1452 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1453 wpa_s->wps_er = NULL;
1454#endif /* CONFIG_WPS_ER */
1455 return 0;
1456}
1457
1458
1459#ifdef CONFIG_WPS_ER
1460int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1461 const char *uuid, const char *pin)
1462{
1463 u8 u[UUID_LEN];
1464 int any = 0;
1465
1466 if (os_strcmp(uuid, "any") == 0)
1467 any = 1;
1468 else if (uuid_str2bin(uuid, u))
1469 return -1;
1470 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1471 any ? NULL : u,
1472 (const u8 *) pin, os_strlen(pin), 300);
1473}
1474
1475
1476int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1477{
1478 u8 u[UUID_LEN];
1479
1480 if (uuid_str2bin(uuid, u))
1481 return -1;
1482 return wps_er_pbc(wpa_s->wps_er, u);
1483}
1484
1485
1486int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1487 const char *pin)
1488{
1489 u8 u[UUID_LEN];
1490
1491 if (uuid_str2bin(uuid, u))
1492 return -1;
1493 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1494 os_strlen(pin));
1495}
1496
1497
1498int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1499 int id)
1500{
1501 u8 u[UUID_LEN];
1502 struct wpa_ssid *ssid;
1503 struct wps_credential cred;
1504
1505 if (uuid_str2bin(uuid, u))
1506 return -1;
1507 ssid = wpa_config_get_network(wpa_s->conf, id);
1508 if (ssid == NULL || ssid->ssid == NULL)
1509 return -1;
1510
1511 os_memset(&cred, 0, sizeof(cred));
1512 if (ssid->ssid_len > 32)
1513 return -1;
1514 os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
1515 cred.ssid_len = ssid->ssid_len;
1516 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1517 cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1518 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1519 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1520 cred.encr_type = WPS_ENCR_AES;
1521 else
1522 cred.encr_type = WPS_ENCR_TKIP;
1523 if (ssid->passphrase) {
1524 cred.key_len = os_strlen(ssid->passphrase);
1525 if (cred.key_len >= 64)
1526 return -1;
1527 os_memcpy(cred.key, ssid->passphrase, cred.key_len);
1528 } else if (ssid->psk_set) {
1529 cred.key_len = 32;
1530 os_memcpy(cred.key, ssid->psk, 32);
1531 } else
1532 return -1;
1533 } else {
1534 cred.auth_type = WPS_AUTH_OPEN;
1535 cred.encr_type = WPS_ENCR_NONE;
1536 }
1537 return wps_er_set_config(wpa_s->wps_er, u, &cred);
1538}
1539
1540
1541int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1542 const char *pin, struct wps_new_ap_settings *settings)
1543{
1544 u8 u[UUID_LEN];
1545 struct wps_credential cred;
1546 size_t len;
1547
1548 if (uuid_str2bin(uuid, u))
1549 return -1;
1550 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1551 settings->encr == NULL || settings->key_hex == NULL)
1552 return -1;
1553
1554 os_memset(&cred, 0, sizeof(cred));
1555 len = os_strlen(settings->ssid_hex);
1556 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1557 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1558 return -1;
1559 cred.ssid_len = len / 2;
1560
1561 len = os_strlen(settings->key_hex);
1562 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1563 hexstr2bin(settings->key_hex, cred.key, len / 2))
1564 return -1;
1565 cred.key_len = len / 2;
1566
1567 if (os_strcmp(settings->auth, "OPEN") == 0)
1568 cred.auth_type = WPS_AUTH_OPEN;
1569 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1570 cred.auth_type = WPS_AUTH_WPAPSK;
1571 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1572 cred.auth_type = WPS_AUTH_WPA2PSK;
1573 else
1574 return -1;
1575
1576 if (os_strcmp(settings->encr, "NONE") == 0)
1577 cred.encr_type = WPS_ENCR_NONE;
1578 else if (os_strcmp(settings->encr, "WEP") == 0)
1579 cred.encr_type = WPS_ENCR_WEP;
1580 else if (os_strcmp(settings->encr, "TKIP") == 0)
1581 cred.encr_type = WPS_ENCR_TKIP;
1582 else if (os_strcmp(settings->encr, "CCMP") == 0)
1583 cred.encr_type = WPS_ENCR_AES;
1584 else
1585 return -1;
1586
1587 return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1588 os_strlen(pin), &cred);
1589}
1590
1591
1592static int callbacks_pending = 0;
1593
1594static void wpas_wps_terminate_cb(void *ctx)
1595{
1596 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1597 if (--callbacks_pending <= 0)
1598 eloop_terminate();
1599}
1600#endif /* CONFIG_WPS_ER */
1601
1602
1603int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1604{
1605#ifdef CONFIG_WPS_ER
1606 if (wpa_s->wps_er) {
1607 callbacks_pending++;
1608 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1609 wpa_s->wps_er = NULL;
1610 return 1;
1611 }
1612#endif /* CONFIG_WPS_ER */
1613 return 0;
1614}
1615
1616
1617int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1618{
1619 struct wpa_ssid *ssid;
1620
1621 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1622 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1623 return 1;
1624 }
1625
1626 return 0;
1627}
1628
1629
1630void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1631{
1632 struct wps_context *wps = wpa_s->wps;
1633
1634 if (wps == NULL)
1635 return;
1636
1637 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1638 wps->config_methods = wps_config_methods_str2bin(
1639 wpa_s->conf->config_methods);
1640 if ((wps->config_methods &
1641 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1642 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1643 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1644 "config methods are not allowed at the "
1645 "same time");
1646 wps->config_methods &= ~WPS_CONFIG_LABEL;
1647 }
1648 }
1649 wps->config_methods = wps_fix_config_methods(wps->config_methods);
1650
1651 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
1652 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1653 WPS_DEV_TYPE_LEN);
1654
1655 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
1656 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1657 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1658 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
1659 }
1660
1661 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1662 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1663
1664 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
1665 wpas_wps_set_uuid(wpa_s, wps);
1666
1667 if (wpa_s->conf->changed_parameters &
1668 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1669 /* Update pointers to make sure they refer current values */
1670 wps->dev.device_name = wpa_s->conf->device_name;
1671 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1672 wps->dev.model_name = wpa_s->conf->model_name;
1673 wps->dev.model_number = wpa_s->conf->model_number;
1674 wps->dev.serial_number = wpa_s->conf->serial_number;
1675 }
1676}