blob: 3c14bb75a9a685b84b46ad10b2b98198ac2d4305 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant / WPS integration
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "uuid.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070014#include "crypto/random.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "crypto/dh_group5.h"
16#include "common/ieee802_11_defs.h"
17#include "common/ieee802_11_common.h"
18#include "common/wpa_common.h"
19#include "common/wpa_ctrl.h"
20#include "eap_common/eap_wsc_common.h"
21#include "eap_peer/eap.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070022#include "eapol_supp/eapol_supp_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "rsn_supp/wpa.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070024#include "wps/wps_attr_parse.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "config.h"
26#include "wpa_supplicant_i.h"
27#include "driver_i.h"
28#include "notify.h"
29#include "blacklist.h"
30#include "bss.h"
31#include "scan.h"
32#include "ap.h"
33#include "p2p/p2p.h"
34#include "p2p_supplicant.h"
35#include "wps_supplicant.h"
36
37
38#ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
39#define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40#endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
41
42static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
43static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
44
45
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070046static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
47{
48 os_free(wpa_s->wps_ap);
49 wpa_s->wps_ap = NULL;
50 wpa_s->num_wps_ap = 0;
51 wpa_s->wps_ap_iter = 0;
52}
53
54
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
56{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080057#ifdef CONFIG_P2P
58 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
59 return 1;
60#endif /* CONFIG_P2P */
61
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062 if (!wpa_s->wps_success &&
63 wpa_s->current_ssid &&
64 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
65 const u8 *bssid = wpa_s->bssid;
66 if (is_zero_ether_addr(bssid))
67 bssid = wpa_s->pending_bssid;
68
69 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
70 " did not succeed - continue trying to find "
71 "suitable AP", MAC2STR(bssid));
72 wpa_blacklist_add(wpa_s, bssid);
73
74 wpa_supplicant_deauthenticate(wpa_s,
75 WLAN_REASON_DEAUTH_LEAVING);
76 wpa_s->reassociate = 1;
77 wpa_supplicant_req_scan(wpa_s,
78 wpa_s->blacklist_cleared ? 5 : 0, 0);
79 wpa_s->blacklist_cleared = 0;
80 return 1;
81 }
82
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070083 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
85 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
86 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
87
88 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
89 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
90 int disabled = wpa_s->current_ssid->disabled;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080091 unsigned int freq = wpa_s->assoc_freq;
Dmitry Shmidt444d5672013-04-01 13:08:44 -070092 struct wpa_bss *bss;
93 struct wpa_ssid *ssid = NULL;
94 int use_fast_assoc = 0;
95
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080097 "try to associate with the received credential "
98 "(freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 wpa_supplicant_deauthenticate(wpa_s,
100 WLAN_REASON_DEAUTH_LEAVING);
101 if (disabled) {
102 wpa_printf(MSG_DEBUG, "WPS: Current network is "
103 "disabled - wait for user to enable");
104 return 1;
105 }
106 wpa_s->after_wps = 5;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800107 wpa_s->wps_freq = freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800108 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 wpa_s->reassociate = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700110
111 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
112 "without a new scan can be used");
113 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
114 if (bss) {
115 struct wpabuf *wps;
116 struct wps_parse_attr attr;
117
118 wps = wpa_bss_get_vendor_ie_multi(bss,
119 WPS_IE_VENDOR_TYPE);
120 if (wps && wps_parse_msg(wps, &attr) == 0 &&
121 attr.wps_state &&
122 *attr.wps_state == WPS_STATE_CONFIGURED)
123 use_fast_assoc = 1;
124 wpabuf_free(wps);
125 }
126
127 if (!use_fast_assoc ||
128 wpa_supplicant_fast_associate(wpa_s) != 1)
129 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 return 1;
131 }
132
133 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
134 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
135 "for external credential processing");
136 wpas_clear_wps(wpa_s);
137 wpa_supplicant_deauthenticate(wpa_s,
138 WLAN_REASON_DEAUTH_LEAVING);
139 return 1;
140 }
141
142 return 0;
143}
144
145
146static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
147 struct wpa_ssid *ssid,
148 const struct wps_credential *cred)
149{
150 struct wpa_driver_capa capa;
151 struct wpa_bss *bss;
152 const u8 *ie;
153 struct wpa_ie_data adv;
154 int wpa2 = 0, ccmp = 0;
155
156 /*
157 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
158 * case they are configured for mixed mode operation (WPA+WPA2 and
159 * TKIP+CCMP). Try to use scan results to figure out whether the AP
160 * actually supports stronger security and select that if the client
161 * has support for it, too.
162 */
163
164 if (wpa_drv_get_capa(wpa_s, &capa))
165 return; /* Unknown what driver supports */
166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167 if (ssid->ssid == NULL)
168 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
170 if (bss == NULL) {
171 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
172 "table - use credential as-is");
173 return;
174 }
175
176 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
177
178 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
179 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
180 wpa2 = 1;
181 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
182 ccmp = 1;
183 } else {
184 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
185 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
186 adv.pairwise_cipher & WPA_CIPHER_CCMP)
187 ccmp = 1;
188 }
189
190 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
191 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
192 /*
193 * TODO: This could be the initial AP configuration and the
194 * Beacon contents could change shortly. Should request a new
195 * scan and delay addition of the network until the updated
196 * scan results are available.
197 */
198 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
199 "support - use credential as-is");
200 return;
201 }
202
203 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
204 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
205 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
206 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
207 "based on scan results");
208 if (wpa_s->conf->ap_scan == 1)
209 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
210 else
211 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
212 }
213
214 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
215 (ssid->proto & WPA_PROTO_WPA) &&
216 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
217 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
218 "based on scan results");
219 if (wpa_s->conf->ap_scan == 1)
220 ssid->proto |= WPA_PROTO_RSN;
221 else
222 ssid->proto = WPA_PROTO_RSN;
223 }
224}
225
226
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700227static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
228 struct wpa_ssid *new_ssid)
229{
230 struct wpa_ssid *ssid, *next;
231
232 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
233 ssid = next, next = ssid ? ssid->next : NULL) {
234 /*
235 * new_ssid has already been added to the list in
236 * wpas_wps_add_network(), so skip it.
237 */
238 if (ssid == new_ssid)
239 continue;
240
241 if (ssid->bssid_set || new_ssid->bssid_set) {
242 if (ssid->bssid_set != new_ssid->bssid_set)
243 continue;
244 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
245 0)
246 continue;
247 }
248
249 /* compare SSID */
250 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
251 continue;
252
253 if (ssid->ssid && new_ssid->ssid) {
254 if (os_memcmp(ssid->ssid, new_ssid->ssid,
255 ssid->ssid_len) != 0)
256 continue;
257 } else if (ssid->ssid || new_ssid->ssid)
258 continue;
259
260 /* compare security parameters */
261 if (ssid->auth_alg != new_ssid->auth_alg ||
262 ssid->key_mgmt != new_ssid->key_mgmt ||
263 ssid->proto != new_ssid->proto ||
264 ssid->pairwise_cipher != new_ssid->pairwise_cipher ||
265 ssid->group_cipher != new_ssid->group_cipher)
266 continue;
267
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700268 /* Remove the duplicated older network entry. */
269 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
270 wpas_notify_network_removed(wpa_s, ssid);
271 wpa_config_remove_network(wpa_s->conf, ssid->id);
272 }
273}
274
275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276static int wpa_supplicant_wps_cred(void *ctx,
277 const struct wps_credential *cred)
278{
279 struct wpa_supplicant *wpa_s = ctx;
280 struct wpa_ssid *ssid = wpa_s->current_ssid;
281 u8 key_idx = 0;
282 u16 auth_type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800283#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 int registrar = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800285#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286
287 if ((wpa_s->conf->wps_cred_processing == 1 ||
288 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
289 size_t blen = cred->cred_attr_len * 2 + 1;
290 char *buf = os_malloc(blen);
291 if (buf) {
292 wpa_snprintf_hex(buf, blen,
293 cred->cred_attr, cred->cred_attr_len);
294 wpa_msg(wpa_s, MSG_INFO, "%s%s",
295 WPS_EVENT_CRED_RECEIVED, buf);
296 os_free(buf);
297 }
298
299 wpas_notify_wps_credential(wpa_s, cred);
300 } else
301 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
302
303 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
304 cred->cred_attr, cred->cred_attr_len);
305
306 if (wpa_s->conf->wps_cred_processing == 1)
307 return 0;
308
309 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
310 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
311 cred->auth_type);
312 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
313 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
314 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
315 cred->key, cred->key_len);
316 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
317 MAC2STR(cred->mac_addr));
318
319 auth_type = cred->auth_type;
320 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
321 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
322 "auth_type into WPA2PSK");
323 auth_type = WPS_AUTH_WPA2PSK;
324 }
325
326 if (auth_type != WPS_AUTH_OPEN &&
327 auth_type != WPS_AUTH_SHARED &&
328 auth_type != WPS_AUTH_WPAPSK &&
329 auth_type != WPS_AUTH_WPA2PSK) {
330 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
331 "unsupported authentication type 0x%x",
332 auth_type);
333 return 0;
334 }
335
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800336 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
337 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
338 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
339 "invalid Network Key length %lu",
340 (unsigned long) cred->key_len);
341 return -1;
342 }
343 }
344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
346 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
347 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800348#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 if (ssid->eap.identity &&
350 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
351 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
352 WSC_ID_REGISTRAR_LEN) == 0)
353 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 os_free(ssid->eap.identity);
356 ssid->eap.identity = NULL;
357 ssid->eap.identity_len = 0;
358 os_free(ssid->eap.phase1);
359 ssid->eap.phase1 = NULL;
360 os_free(ssid->eap.eap_methods);
361 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700362 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700364 ssid->bssid_set = 0;
365 }
366 ssid->disabled_until.sec = 0;
367 ssid->disabled_until.usec = 0;
368 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 } else {
370 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
371 "received credential");
372 ssid = wpa_config_add_network(wpa_s->conf);
373 if (ssid == NULL)
374 return -1;
375 wpas_notify_network_added(wpa_s, ssid);
376 }
377
378 wpa_config_set_network_defaults(ssid);
379
380 os_free(ssid->ssid);
381 ssid->ssid = os_malloc(cred->ssid_len);
382 if (ssid->ssid) {
383 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
384 ssid->ssid_len = cred->ssid_len;
385 }
386
387 switch (cred->encr_type) {
388 case WPS_ENCR_NONE:
389 break;
390 case WPS_ENCR_WEP:
391 if (cred->key_len <= 0)
392 break;
393 if (cred->key_len != 5 && cred->key_len != 13 &&
394 cred->key_len != 10 && cred->key_len != 26) {
395 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
396 "%lu", (unsigned long) cred->key_len);
397 return -1;
398 }
399 if (cred->key_idx > NUM_WEP_KEYS) {
400 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
401 cred->key_idx);
402 return -1;
403 }
404 if (cred->key_idx)
405 key_idx = cred->key_idx - 1;
406 if (cred->key_len == 10 || cred->key_len == 26) {
407 if (hexstr2bin((char *) cred->key,
408 ssid->wep_key[key_idx],
409 cred->key_len / 2) < 0) {
410 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
411 "%d", key_idx);
412 return -1;
413 }
414 ssid->wep_key_len[key_idx] = cred->key_len / 2;
415 } else {
416 os_memcpy(ssid->wep_key[key_idx], cred->key,
417 cred->key_len);
418 ssid->wep_key_len[key_idx] = cred->key_len;
419 }
420 ssid->wep_tx_keyidx = key_idx;
421 break;
422 case WPS_ENCR_TKIP:
423 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
424 break;
425 case WPS_ENCR_AES:
426 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
427 break;
428 }
429
430 switch (auth_type) {
431 case WPS_AUTH_OPEN:
432 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
433 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
434 ssid->proto = 0;
435#ifdef CONFIG_WPS_REG_DISABLE_OPEN
436 if (registrar) {
437 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
438 "id=%d - Credentials for an open "
439 "network disabled by default - use "
440 "'select_network %d' to enable",
441 ssid->id, ssid->id);
442 ssid->disabled = 1;
443 }
444#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
445 break;
446 case WPS_AUTH_SHARED:
447 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
448 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
449 ssid->proto = 0;
450 break;
451 case WPS_AUTH_WPAPSK:
452 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
453 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
454 ssid->proto = WPA_PROTO_WPA;
455 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456 case WPS_AUTH_WPA2PSK:
457 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
458 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
459 ssid->proto = WPA_PROTO_RSN;
460 break;
461 }
462
463 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
464 if (cred->key_len == 2 * PMK_LEN) {
465 if (hexstr2bin((const char *) cred->key, ssid->psk,
466 PMK_LEN)) {
467 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
468 "Key");
469 return -1;
470 }
471 ssid->psk_set = 1;
472 ssid->export_keys = 1;
473 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
474 os_free(ssid->passphrase);
475 ssid->passphrase = os_malloc(cred->key_len + 1);
476 if (ssid->passphrase == NULL)
477 return -1;
478 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
479 ssid->passphrase[cred->key_len] = '\0';
480 wpa_config_update_psk(ssid);
481 ssid->export_keys = 1;
482 } else {
483 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
484 "length %lu",
485 (unsigned long) cred->key_len);
486 return -1;
487 }
488 }
489
490 wpas_wps_security_workaround(wpa_s, ssid, cred);
491
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700492 wpas_wps_remove_dup_network(wpa_s, ssid);
493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494#ifndef CONFIG_NO_CONFIG_WRITE
495 if (wpa_s->conf->update_config &&
496 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
497 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
498 return -1;
499 }
500#endif /* CONFIG_NO_CONFIG_WRITE */
501
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800502 /*
503 * Optimize the post-WPS scan based on the channel used during
504 * the provisioning in case EAP-Failure is not received.
505 */
506 wpa_s->after_wps = 5;
507 wpa_s->wps_freq = wpa_s->assoc_freq;
508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 return 0;
510}
511
512
513#ifdef CONFIG_P2P
514static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
515{
516 struct wpa_supplicant *wpa_s = eloop_ctx;
517 wpas_p2p_notif_pbc_overlap(wpa_s);
518}
519#endif /* CONFIG_P2P */
520
521
522static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
523 struct wps_event_m2d *m2d)
524{
525 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
526 "dev_password_id=%d config_error=%d",
527 m2d->dev_password_id, m2d->config_error);
528 wpas_notify_wps_event_m2d(wpa_s, m2d);
529#ifdef CONFIG_P2P
530 if (wpa_s->parent && wpa_s->parent != wpa_s) {
531 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
532 "dev_password_id=%d config_error=%d",
533 m2d->dev_password_id, m2d->config_error);
534 }
535 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
536 /*
537 * Notify P2P from eloop timeout to avoid issues with the
538 * interface getting removed while processing a message.
539 */
540 eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
541 NULL);
542 }
543#endif /* CONFIG_P2P */
544}
545
546
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700547static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
548{
549 struct wpa_supplicant *wpa_s = eloop_ctx;
550 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
551 wpas_clear_wps(wpa_s);
552}
553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554
555static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
556 struct wps_event_fail *fail)
557{
558 if (fail->error_indication > 0 &&
559 fail->error_indication < NUM_WPS_EI_VALUES) {
560 wpa_msg(wpa_s, MSG_INFO,
561 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
562 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700563 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 if (wpa_s->parent && wpa_s->parent != wpa_s)
565 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
566 "msg=%d config_error=%d reason=%d (%s)",
567 fail->msg, fail->config_error,
568 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700569 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 } else {
571 wpa_msg(wpa_s, MSG_INFO,
572 WPS_EVENT_FAIL "msg=%d config_error=%d",
573 fail->msg, fail->config_error);
574 if (wpa_s->parent && wpa_s->parent != wpa_s)
575 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
576 "msg=%d config_error=%d",
577 fail->msg, fail->config_error);
578 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700579
580 /*
581 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
582 */
583 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
584 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
585 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700588#ifdef CONFIG_P2P
589 wpas_p2p_wps_failed(wpa_s, fail);
590#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591}
592
593
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800594static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
595
596static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
597{
598 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800599 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800600
601 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
602
603 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
604 if (ssid->disabled_for_connect && ssid->disabled) {
605 ssid->disabled_for_connect = 0;
606 ssid->disabled = 0;
607 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800608 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800609 }
610 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800611
612 if (changed) {
613#ifndef CONFIG_NO_CONFIG_WRITE
614 if (wpa_s->conf->update_config &&
615 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
616 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
617 "configuration");
618 }
619#endif /* CONFIG_NO_CONFIG_WRITE */
620 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800621}
622
623
624static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
625{
626 struct wpa_supplicant *wpa_s = eloop_ctx;
627 /* Enable the networks disabled during wpas_wps_reassoc */
628 wpas_wps_reenable_networks(wpa_s);
629}
630
631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
633{
634 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
635 wpa_s->wps_success = 1;
636 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700637 if (wpa_s->current_ssid)
638 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
639 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800640
641 /*
642 * Enable the networks disabled during wpas_wps_reassoc after 10
643 * seconds. The 10 seconds timer is to allow the data connection to be
644 * formed before allowing other networks to be selected.
645 */
646 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
647 NULL);
648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649#ifdef CONFIG_P2P
650 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
651#endif /* CONFIG_P2P */
652}
653
654
655static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
656 struct wps_event_er_ap *ap)
657{
658 char uuid_str[100];
659 char dev_type[WPS_DEV_TYPE_BUFSIZE];
660
661 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
662 if (ap->pri_dev_type)
663 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
664 sizeof(dev_type));
665 else
666 dev_type[0] = '\0';
667
668 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
669 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
670 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
671 ap->friendly_name ? ap->friendly_name : "",
672 ap->manufacturer ? ap->manufacturer : "",
673 ap->model_description ? ap->model_description : "",
674 ap->model_name ? ap->model_name : "",
675 ap->manufacturer_url ? ap->manufacturer_url : "",
676 ap->model_url ? ap->model_url : "");
677}
678
679
680static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
681 struct wps_event_er_ap *ap)
682{
683 char uuid_str[100];
684 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
685 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
686}
687
688
689static void wpa_supplicant_wps_event_er_enrollee_add(
690 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
691{
692 char uuid_str[100];
693 char dev_type[WPS_DEV_TYPE_BUFSIZE];
694
695 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
696 if (enrollee->pri_dev_type)
697 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
698 sizeof(dev_type));
699 else
700 dev_type[0] = '\0';
701
702 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
703 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
704 "|%s|%s|%s|%s|%s|",
705 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
706 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
707 enrollee->dev_name ? enrollee->dev_name : "",
708 enrollee->manufacturer ? enrollee->manufacturer : "",
709 enrollee->model_name ? enrollee->model_name : "",
710 enrollee->model_number ? enrollee->model_number : "",
711 enrollee->serial_number ? enrollee->serial_number : "");
712}
713
714
715static void wpa_supplicant_wps_event_er_enrollee_remove(
716 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
717{
718 char uuid_str[100];
719 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
720 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
721 uuid_str, MAC2STR(enrollee->mac_addr));
722}
723
724
725static void wpa_supplicant_wps_event_er_ap_settings(
726 struct wpa_supplicant *wpa_s,
727 struct wps_event_er_ap_settings *ap_settings)
728{
729 char uuid_str[100];
730 char key_str[65];
731 const struct wps_credential *cred = ap_settings->cred;
732
733 key_str[0] = '\0';
734 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
735 if (cred->key_len >= 8 && cred->key_len <= 64) {
736 os_memcpy(key_str, cred->key, cred->key_len);
737 key_str[cred->key_len] = '\0';
738 }
739 }
740
741 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
742 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
743 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
744 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
745 "key=%s",
746 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
747 cred->auth_type, cred->encr_type, key_str);
748}
749
750
751static void wpa_supplicant_wps_event_er_set_sel_reg(
752 struct wpa_supplicant *wpa_s,
753 struct wps_event_er_set_selected_registrar *ev)
754{
755 char uuid_str[100];
756
757 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
758 switch (ev->state) {
759 case WPS_ER_SET_SEL_REG_START:
760 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
761 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
762 "sel_reg_config_methods=0x%x",
763 uuid_str, ev->sel_reg, ev->dev_passwd_id,
764 ev->sel_reg_config_methods);
765 break;
766 case WPS_ER_SET_SEL_REG_DONE:
767 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
768 "uuid=%s state=DONE", uuid_str);
769 break;
770 case WPS_ER_SET_SEL_REG_FAILED:
771 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
772 "uuid=%s state=FAILED", uuid_str);
773 break;
774 }
775}
776
777
778static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
779 union wps_event_data *data)
780{
781 struct wpa_supplicant *wpa_s = ctx;
782 switch (event) {
783 case WPS_EV_M2D:
784 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
785 break;
786 case WPS_EV_FAIL:
787 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
788 break;
789 case WPS_EV_SUCCESS:
790 wpa_supplicant_wps_event_success(wpa_s);
791 break;
792 case WPS_EV_PWD_AUTH_FAIL:
793#ifdef CONFIG_AP
794 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
795 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
796#endif /* CONFIG_AP */
797 break;
798 case WPS_EV_PBC_OVERLAP:
799 break;
800 case WPS_EV_PBC_TIMEOUT:
801 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700802 case WPS_EV_PBC_ACTIVE:
803 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
804 break;
805 case WPS_EV_PBC_DISABLE:
806 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
807 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 case WPS_EV_ER_AP_ADD:
809 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
810 break;
811 case WPS_EV_ER_AP_REMOVE:
812 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
813 break;
814 case WPS_EV_ER_ENROLLEE_ADD:
815 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
816 &data->enrollee);
817 break;
818 case WPS_EV_ER_ENROLLEE_REMOVE:
819 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
820 &data->enrollee);
821 break;
822 case WPS_EV_ER_AP_SETTINGS:
823 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
824 &data->ap_settings);
825 break;
826 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
827 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
828 &data->set_sel_reg);
829 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800830 case WPS_EV_AP_PIN_SUCCESS:
831 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 }
833}
834
835
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700836static int wpa_supplicant_wps_rf_band(void *ctx)
837{
838 struct wpa_supplicant *wpa_s = ctx;
839
840 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
841 return 0;
842
843 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
844}
845
846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
848{
849 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
850 eap_is_wps_pin_enrollee(&ssid->eap))
851 return WPS_REQ_ENROLLEE;
852 else
853 return WPS_REQ_REGISTRAR;
854}
855
856
857static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
858{
859 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700860 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
861
Dmitry Shmidt051af732013-10-22 13:52:46 -0700862 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700863 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700864
Jouni Malinen75ecf522011-06-27 15:19:46 -0700865 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800867 /* Enable the networks disabled during wpas_wps_reassoc */
868 wpas_wps_reenable_networks(wpa_s);
869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800871 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872
873 /* Remove any existing WPS network from configuration */
874 ssid = wpa_s->conf->ssid;
875 while (ssid) {
876 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
877 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700878 wpa_supplicant_deauthenticate(
879 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 }
881 id = ssid->id;
882 remove_ssid = ssid;
883 } else
884 id = -1;
885 ssid = ssid->next;
886 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700887 if (prev_current == remove_ssid) {
888 wpa_sm_set_config(wpa_s->wpa, NULL);
889 eapol_sm_notify_config(wpa_s->eapol, NULL,
890 NULL);
891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 wpas_notify_network_removed(wpa_s, remove_ssid);
893 wpa_config_remove_network(wpa_s->conf, id);
894 }
895 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700896
897 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898}
899
900
901static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
902{
903 struct wpa_supplicant *wpa_s = eloop_ctx;
904 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
905 "out");
906 wpas_clear_wps(wpa_s);
907}
908
909
910static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800911 int registrar, const u8 *dev_addr,
912 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913{
914 struct wpa_ssid *ssid;
915
916 ssid = wpa_config_add_network(wpa_s->conf);
917 if (ssid == NULL)
918 return NULL;
919 wpas_notify_network_added(wpa_s, ssid);
920 wpa_config_set_network_defaults(ssid);
921 ssid->temporary = 1;
922 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
923 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
924 wpa_config_set(ssid, "identity", registrar ?
925 "\"" WSC_ID_REGISTRAR "\"" :
926 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
927 wpas_notify_network_removed(wpa_s, ssid);
928 wpa_config_remove_network(wpa_s->conf, ssid->id);
929 return NULL;
930 }
931
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800932#ifdef CONFIG_P2P
933 if (dev_addr)
934 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
935#endif /* CONFIG_P2P */
936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 if (bssid) {
938#ifndef CONFIG_P2P
939 struct wpa_bss *bss;
940 int count = 0;
941#endif /* CONFIG_P2P */
942
943 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
944 ssid->bssid_set = 1;
945
946 /*
947 * Note: With P2P, the SSID may change at the time the WPS
948 * provisioning is started, so better not filter the AP based
949 * on the current SSID in the scan results.
950 */
951#ifndef CONFIG_P2P
952 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
953 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
954 continue;
955
956 os_free(ssid->ssid);
957 ssid->ssid = os_malloc(bss->ssid_len);
958 if (ssid->ssid == NULL)
959 break;
960 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
961 ssid->ssid_len = bss->ssid_len;
962 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
963 "scan results",
964 ssid->ssid, ssid->ssid_len);
965 count++;
966 }
967
968 if (count > 1) {
969 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
970 "for the AP; use wildcard");
971 os_free(ssid->ssid);
972 ssid->ssid = NULL;
973 ssid->ssid_len = 0;
974 }
975#endif /* CONFIG_P2P */
976 }
977
978 return ssid;
979}
980
981
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700982static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
983 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984{
985 struct wpa_ssid *ssid;
986
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800987 if (wpa_s->current_ssid)
988 wpa_supplicant_deauthenticate(
989 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 /* Mark all other networks disabled and trigger reassociation */
992 ssid = wpa_s->conf->ssid;
993 while (ssid) {
994 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800995 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700996 /*
997 * In case the network object corresponds to a persistent group
998 * then do not send out network disabled signal. In addition,
999 * do not change disabled status of persistent network objects
1000 * from 2 to 1 should we connect to another network.
1001 */
1002 if (was_disabled != 2) {
1003 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001004 if (was_disabled != ssid->disabled) {
1005 if (ssid->disabled)
1006 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001007 wpas_notify_network_enabled_changed(wpa_s,
1008 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001009 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001010 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 ssid = ssid->next;
1012 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001013}
1014
1015
1016static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001017 struct wpa_ssid *selected, const u8 *bssid,
1018 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001019{
1020 struct wpa_bss *bss;
1021
1022 wpa_s->after_wps = 0;
1023 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001024 if (freq) {
1025 wpa_s->after_wps = 5;
1026 wpa_s->wps_freq = freq;
1027 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001028 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1029 if (bss && bss->freq > 0) {
1030 wpa_s->known_wps_freq = 1;
1031 wpa_s->wps_freq = bss->freq;
1032 }
1033 }
1034
1035 wpas_wps_temp_disable(wpa_s, selected);
1036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 wpa_s->disconnected = 0;
1038 wpa_s->reassociate = 1;
1039 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001040 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 wpa_s->wps_success = 0;
1042 wpa_s->blacklist_cleared = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001043
1044 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 wpa_supplicant_req_scan(wpa_s, 0, 0);
1046}
1047
1048
1049int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1050 int p2p_group)
1051{
1052 struct wpa_ssid *ssid;
1053 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001054 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 if (ssid == NULL)
1056 return -1;
1057 ssid->temporary = 1;
1058 ssid->p2p_group = p2p_group;
1059#ifdef CONFIG_P2P
1060 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1061 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1062 if (ssid->ssid) {
1063 ssid->ssid_len = wpa_s->go_params->ssid_len;
1064 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1065 ssid->ssid_len);
1066 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1067 "SSID", ssid->ssid, ssid->ssid_len);
1068 }
1069 }
1070#endif /* CONFIG_P2P */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001071 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1072 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 if (wpa_s->wps_fragment_size)
1074 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1075 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1076 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001077 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 return 0;
1079}
1080
1081
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001082static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1083 const u8 *dev_addr, const u8 *bssid,
1084 const char *pin, int p2p_group, u16 dev_pw_id,
1085 const u8 *peer_pubkey_hash,
1086 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087{
1088 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001089 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001091 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092
1093 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001094 if (bssid && is_zero_ether_addr(bssid))
1095 bssid = NULL;
1096 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1097 if (ssid == NULL) {
1098 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001100 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 ssid->temporary = 1;
1102 ssid->p2p_group = p2p_group;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001103 if (ssid_val) {
1104 ssid->ssid = os_malloc(ssid_len);
1105 if (ssid->ssid) {
1106 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1107 ssid->ssid_len = ssid_len;
1108 }
1109 }
1110 if (peer_pubkey_hash) {
1111 os_memcpy(hash, " pkhash=", 8);
1112 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1113 peer_pubkey_hash,
1114 WPS_OOB_PUBKEY_HASH_LEN);
1115 } else {
1116 hash[0] = '\0';
1117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118#ifdef CONFIG_P2P
1119 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1120 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1121 if (ssid->ssid) {
1122 ssid->ssid_len = wpa_s->go_params->ssid_len;
1123 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1124 ssid->ssid_len);
1125 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1126 "SSID", ssid->ssid, ssid->ssid_len);
1127 }
1128 }
1129#endif /* CONFIG_P2P */
1130 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001131 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1132 pin, dev_pw_id, hash);
1133 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1134 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1135 dev_pw_id, hash);
1136 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 rpin = wps_generate_pin();
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001138 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1139 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001141 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1142 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001143 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 if (wpa_s->wps_fragment_size)
1146 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1147 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1148 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001149 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001150 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 return rpin;
1152}
1153
1154
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001155int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1156 const char *pin, int p2p_group, u16 dev_pw_id)
1157{
1158 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1159 dev_pw_id, NULL, NULL, 0, 0);
1160}
1161
1162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163/* Cancel the wps pbc/pin requests */
1164int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1165{
1166#ifdef CONFIG_AP
1167 if (wpa_s->ap_iface) {
1168 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1169 return wpa_supplicant_ap_wps_cancel(wpa_s);
1170 }
1171#endif /* CONFIG_AP */
1172
Dmitry Shmidt04949592012-07-19 12:16:46 -07001173 if (wpa_s->wpa_state == WPA_SCANNING ||
1174 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1176 wpa_supplicant_cancel_scan(wpa_s);
1177 wpas_clear_wps(wpa_s);
1178 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1179 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1180 "deauthenticate");
1181 wpa_supplicant_deauthenticate(wpa_s,
1182 WLAN_REASON_DEAUTH_LEAVING);
1183 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001184 } else {
1185 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001186 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001187 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1188 0)
1189 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 }
1191
Dmitry Shmidt051af732013-10-22 13:52:46 -07001192 wpa_s->after_wps = 0;
1193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 return 0;
1195}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196
1197
1198int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1199 const char *pin, struct wps_new_ap_settings *settings)
1200{
1201 struct wpa_ssid *ssid;
1202 char val[200];
1203 char *pos, *end;
1204 int res;
1205
1206 if (!pin)
1207 return -1;
1208 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001209 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 if (ssid == NULL)
1211 return -1;
1212 ssid->temporary = 1;
1213 pos = val;
1214 end = pos + sizeof(val);
1215 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1216 if (res < 0 || res >= end - pos)
1217 return -1;
1218 pos += res;
1219 if (settings) {
1220 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1221 "new_encr=%s new_key=%s",
1222 settings->ssid_hex, settings->auth,
1223 settings->encr, settings->key_hex);
1224 if (res < 0 || res >= end - pos)
1225 return -1;
1226 pos += res;
1227 }
1228 res = os_snprintf(pos, end - pos, "\"");
1229 if (res < 0 || res >= end - pos)
1230 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001231 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1232 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 if (wpa_s->wps_fragment_size)
1234 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1235 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1236 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001237 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 return 0;
1239}
1240
1241
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001242static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1243 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 size_t psk_len)
1245{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001246 if (is_zero_ether_addr(p2p_dev_addr)) {
1247 wpa_printf(MSG_DEBUG,
1248 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1249 MAC2STR(mac_addr));
1250 } else {
1251 wpa_printf(MSG_DEBUG,
1252 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1253 " P2P Device Addr " MACSTR,
1254 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1257
1258 /* TODO */
1259
1260 return 0;
1261}
1262
1263
1264static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1265 const struct wps_device_data *dev)
1266{
1267 char uuid[40], txt[400];
1268 int len;
1269 char devtype[WPS_DEV_TYPE_BUFSIZE];
1270 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1271 return;
1272 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1273 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1274 " [%s|%s|%s|%s|%s|%s]",
1275 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1276 dev->manufacturer, dev->model_name,
1277 dev->model_number, dev->serial_number,
1278 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1279 sizeof(devtype)));
1280 if (len > 0 && len < (int) sizeof(txt))
1281 wpa_printf(MSG_INFO, "%s", txt);
1282}
1283
1284
1285static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1286 u16 sel_reg_config_methods)
1287{
1288#ifdef CONFIG_WPS_ER
1289 struct wpa_supplicant *wpa_s = ctx;
1290
1291 if (wpa_s->wps_er == NULL)
1292 return;
1293 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1294 "dev_password_id=%u sel_reg_config_methods=0x%x",
1295 sel_reg, dev_passwd_id, sel_reg_config_methods);
1296 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1297 sel_reg_config_methods);
1298#endif /* CONFIG_WPS_ER */
1299}
1300
1301
1302static u16 wps_fix_config_methods(u16 config_methods)
1303{
1304#ifdef CONFIG_WPS2
1305 if ((config_methods &
1306 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1307 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1308 wpa_printf(MSG_INFO, "WPS: Converting display to "
1309 "virtual_display for WPS 2.0 compliance");
1310 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1311 }
1312 if ((config_methods &
1313 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1314 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1315 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1316 "virtual_push_button for WPS 2.0 compliance");
1317 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1318 }
1319#endif /* CONFIG_WPS2 */
1320
1321 return config_methods;
1322}
1323
1324
1325static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1326 struct wps_context *wps)
1327{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001328 char buf[50];
1329 const char *src;
1330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 if (is_nil_uuid(wpa_s->conf->uuid)) {
1332 struct wpa_supplicant *first;
1333 first = wpa_s->global->ifaces;
1334 while (first && first->next)
1335 first = first->next;
1336 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001337 if (wps != wpa_s->global->ifaces->wps)
1338 os_memcpy(wps->uuid,
1339 wpa_s->global->ifaces->wps->uuid,
1340 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001341 src = "from the first interface";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 } else {
1343 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001344 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 }
1346 } else {
1347 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001348 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001350
1351 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1352 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353}
1354
1355
Dmitry Shmidt04949592012-07-19 12:16:46 -07001356static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1357 struct wps_context *wps)
1358{
1359 wpabuf_free(wps->dev.vendor_ext_m1);
1360 wps->dev.vendor_ext_m1 = NULL;
1361
1362 if (wpa_s->conf->wps_vendor_ext_m1) {
1363 wps->dev.vendor_ext_m1 =
1364 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1365 if (!wps->dev.vendor_ext_m1) {
1366 wpa_printf(MSG_ERROR, "WPS: Cannot "
1367 "allocate memory for vendor_ext_m1");
1368 }
1369 }
1370}
1371
1372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373int wpas_wps_init(struct wpa_supplicant *wpa_s)
1374{
1375 struct wps_context *wps;
1376 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001377 struct hostapd_hw_modes *modes;
1378 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379
1380 wps = os_zalloc(sizeof(*wps));
1381 if (wps == NULL)
1382 return -1;
1383
1384 wps->cred_cb = wpa_supplicant_wps_cred;
1385 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001386 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 wps->cb_ctx = wpa_s;
1388
1389 wps->dev.device_name = wpa_s->conf->device_name;
1390 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1391 wps->dev.model_name = wpa_s->conf->model_name;
1392 wps->dev.model_number = wpa_s->conf->model_number;
1393 wps->dev.serial_number = wpa_s->conf->serial_number;
1394 wps->config_methods =
1395 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1396 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1397 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1398 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1399 "methods are not allowed at the same time");
1400 os_free(wps);
1401 return -1;
1402 }
1403 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001404 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1406 WPS_DEV_TYPE_LEN);
1407
1408 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1409 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1410 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1411
Dmitry Shmidt04949592012-07-19 12:16:46 -07001412 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001415 modes = wpa_s->hw.modes;
1416 if (modes) {
1417 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1418 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1419 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1420 wps->dev.rf_bands |= WPS_RF_24GHZ;
1421 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1422 wps->dev.rf_bands |= WPS_RF_50GHZ;
1423 }
1424 }
1425 if (wps->dev.rf_bands == 0) {
1426 /*
1427 * Default to claiming support for both bands if the driver
1428 * does not provide support for fetching supported bands.
1429 */
1430 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1433 wpas_wps_set_uuid(wpa_s, wps);
1434
1435 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1436 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1437
1438 os_memset(&rcfg, 0, sizeof(rcfg));
1439 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1440 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1441 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1442 rcfg.cb_ctx = wpa_s;
1443
1444 wps->registrar = wps_registrar_init(wps, &rcfg);
1445 if (wps->registrar == NULL) {
1446 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1447 os_free(wps);
1448 return -1;
1449 }
1450
1451 wpa_s->wps = wps;
1452
1453 return 0;
1454}
1455
1456
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001457#ifdef CONFIG_WPS_ER
1458static void wpas_wps_nfc_clear(struct wps_context *wps)
1459{
1460 wps->ap_nfc_dev_pw_id = 0;
1461 wpabuf_free(wps->ap_nfc_dh_pubkey);
1462 wps->ap_nfc_dh_pubkey = NULL;
1463 wpabuf_free(wps->ap_nfc_dh_privkey);
1464 wps->ap_nfc_dh_privkey = NULL;
1465 wpabuf_free(wps->ap_nfc_dev_pw);
1466 wps->ap_nfc_dev_pw = NULL;
1467}
1468#endif /* CONFIG_WPS_ER */
1469
1470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1472{
1473 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001474 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001475 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001476 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477
1478 if (wpa_s->wps == NULL)
1479 return;
1480
1481#ifdef CONFIG_WPS_ER
1482 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1483 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001484 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001485#endif /* CONFIG_WPS_ER */
1486
1487 wps_registrar_deinit(wpa_s->wps->registrar);
1488 wpabuf_free(wpa_s->wps->dh_pubkey);
1489 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001490 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 os_free(wpa_s->wps->network_key);
1492 os_free(wpa_s->wps);
1493 wpa_s->wps = NULL;
1494}
1495
1496
1497int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001498 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499{
1500 struct wpabuf *wps_ie;
1501
1502 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1503 return -1;
1504
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001505 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1507 if (!wps_ie) {
1508 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1509 return 0;
1510 }
1511
1512 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1513 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1514 "without active PBC Registrar");
1515 wpabuf_free(wps_ie);
1516 return 0;
1517 }
1518
1519 /* TODO: overlap detection */
1520 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1521 "(Active PBC)");
1522 wpabuf_free(wps_ie);
1523 return 1;
1524 }
1525
1526 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1527 if (!wps_ie) {
1528 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1529 return 0;
1530 }
1531
1532 /*
1533 * Start with WPS APs that advertise our address as an
1534 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1535 * allow any WPS AP after couple of scans since some APs do not
1536 * set Selected Registrar attribute properly when using
1537 * external Registrar.
1538 */
1539 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1540 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1541 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1542 "without active PIN Registrar");
1543 wpabuf_free(wps_ie);
1544 return 0;
1545 }
1546 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1547 } else {
1548 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1549 "(Authorized MAC or Active PIN)");
1550 }
1551 wpabuf_free(wps_ie);
1552 return 1;
1553 }
1554
1555 if (wps_ie) {
1556 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1557 wpabuf_free(wps_ie);
1558 return 1;
1559 }
1560
1561 return -1;
1562}
1563
1564
1565int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1566 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001567 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568{
1569 struct wpabuf *wps_ie = NULL;
1570 int ret = 0;
1571
1572 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001573 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1575 /* allow wildcard SSID for WPS PBC */
1576 ret = 1;
1577 }
1578 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001579 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 if (wps_ie &&
1581 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1582 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1583 /* allow wildcard SSID for WPS PIN */
1584 ret = 1;
1585 }
1586 }
1587
1588 if (!ret && ssid->bssid_set &&
1589 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1590 /* allow wildcard SSID due to hardcoded BSSID match */
1591 ret = 1;
1592 }
1593
1594#ifdef CONFIG_WPS_STRICT
1595 if (wps_ie) {
1596 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1597 0, bss->bssid) < 0)
1598 ret = 0;
1599 if (bss->beacon_ie_len) {
1600 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001601 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 bss, WPS_IE_VENDOR_TYPE);
1603 if (bcn_wps == NULL) {
1604 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1605 "missing from AP Beacon");
1606 ret = 0;
1607 } else {
1608 if (wps_validate_beacon(wps_ie) < 0)
1609 ret = 0;
1610 wpabuf_free(bcn_wps);
1611 }
1612 }
1613 }
1614#endif /* CONFIG_WPS_STRICT */
1615
1616 wpabuf_free(wps_ie);
1617
1618 return ret;
1619}
1620
1621
1622int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1623 struct wpa_bss *selected, struct wpa_ssid *ssid)
1624{
1625 const u8 *sel_uuid, *uuid;
1626 struct wpabuf *wps_ie;
1627 int ret = 0;
1628 struct wpa_bss *bss;
1629
1630 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1631 return 0;
1632
1633 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1634 "present in scan results; selected BSSID " MACSTR,
1635 MAC2STR(selected->bssid));
1636
1637 /* Make sure that only one AP is in active PBC mode */
1638 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1639 if (wps_ie) {
1640 sel_uuid = wps_get_uuid_e(wps_ie);
1641 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1642 sel_uuid, UUID_LEN);
1643 } else {
1644 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1645 "WPS IE?!");
1646 sel_uuid = NULL;
1647 }
1648
1649 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1650 struct wpabuf *ie;
1651 if (bss == selected)
1652 continue;
1653 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1654 if (!ie)
1655 continue;
1656 if (!wps_is_selected_pbc_registrar(ie)) {
1657 wpabuf_free(ie);
1658 continue;
1659 }
1660 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1661 MACSTR, MAC2STR(bss->bssid));
1662 uuid = wps_get_uuid_e(ie);
1663 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1664 uuid, UUID_LEN);
1665 if (sel_uuid == NULL || uuid == NULL ||
1666 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1667 ret = 1; /* PBC overlap */
1668 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1669 MACSTR " and " MACSTR,
1670 MAC2STR(selected->bssid),
1671 MAC2STR(bss->bssid));
1672 wpabuf_free(ie);
1673 break;
1674 }
1675
1676 /* TODO: verify that this is reasonable dual-band situation */
1677
1678 wpabuf_free(ie);
1679 }
1680
1681 wpabuf_free(wps_ie);
1682
1683 return ret;
1684}
1685
1686
1687void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1688{
1689 struct wpa_bss *bss;
1690 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1691
1692 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1693 return;
1694
1695 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1696 struct wpabuf *ie;
1697 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1698 if (!ie)
1699 continue;
1700 if (wps_is_selected_pbc_registrar(ie))
1701 pbc++;
1702 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1703 auth++;
1704 else if (wps_is_selected_pin_registrar(ie))
1705 pin++;
1706 else
1707 wps++;
1708 wpabuf_free(ie);
1709 }
1710
1711 if (pbc)
1712 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1713 else if (auth)
1714 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1715 else if (pin)
1716 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1717 else if (wps)
1718 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1719}
1720
1721
1722int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1723{
1724 struct wpa_ssid *ssid;
1725
1726 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1727 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1728 return 1;
1729 }
1730
1731 return 0;
1732}
1733
1734
1735int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1736 char *end)
1737{
1738 struct wpabuf *wps_ie;
1739 int ret;
1740
1741 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1742 if (wps_ie == NULL)
1743 return 0;
1744
1745 ret = wps_attr_text(wps_ie, buf, end);
1746 wpabuf_free(wps_ie);
1747 return ret;
1748}
1749
1750
1751int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1752{
1753#ifdef CONFIG_WPS_ER
1754 if (wpa_s->wps_er) {
1755 wps_er_refresh(wpa_s->wps_er);
1756 return 0;
1757 }
1758 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1759 if (wpa_s->wps_er == NULL)
1760 return -1;
1761 return 0;
1762#else /* CONFIG_WPS_ER */
1763 return 0;
1764#endif /* CONFIG_WPS_ER */
1765}
1766
1767
1768int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1769{
1770#ifdef CONFIG_WPS_ER
1771 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1772 wpa_s->wps_er = NULL;
1773#endif /* CONFIG_WPS_ER */
1774 return 0;
1775}
1776
1777
1778#ifdef CONFIG_WPS_ER
1779int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1780 const char *uuid, const char *pin)
1781{
1782 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001783 const u8 *use_uuid = NULL;
1784 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001786 if (os_strcmp(uuid, "any") == 0) {
1787 } else if (uuid_str2bin(uuid, u) == 0) {
1788 use_uuid = u;
1789 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1790 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1791 if (use_uuid == NULL)
1792 return -1;
1793 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 return -1;
1795 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001796 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797 (const u8 *) pin, os_strlen(pin), 300);
1798}
1799
1800
1801int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1802{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001803 u8 u[UUID_LEN], *use_uuid = NULL;
1804 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001806 if (uuid_str2bin(uuid, u) == 0)
1807 use_uuid = u;
1808 else if (hwaddr_aton(uuid, addr) == 0)
1809 use_addr = addr;
1810 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001812 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813}
1814
1815
1816int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1817 const char *pin)
1818{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001819 u8 u[UUID_LEN], *use_uuid = NULL;
1820 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001822 if (uuid_str2bin(uuid, u) == 0)
1823 use_uuid = u;
1824 else if (hwaddr_aton(uuid, addr) == 0)
1825 use_addr = addr;
1826 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001828
1829 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 os_strlen(pin));
1831}
1832
1833
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001834static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1835 struct wps_credential *cred)
1836{
1837 os_memset(cred, 0, sizeof(*cred));
1838 if (ssid->ssid_len > 32)
1839 return -1;
1840 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1841 cred->ssid_len = ssid->ssid_len;
1842 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1843 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1844 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1845 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1846 cred->encr_type = WPS_ENCR_AES;
1847 else
1848 cred->encr_type = WPS_ENCR_TKIP;
1849 if (ssid->passphrase) {
1850 cred->key_len = os_strlen(ssid->passphrase);
1851 if (cred->key_len >= 64)
1852 return -1;
1853 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1854 } else if (ssid->psk_set) {
1855 cred->key_len = 32;
1856 os_memcpy(cred->key, ssid->psk, 32);
1857 } else
1858 return -1;
1859 } else {
1860 cred->auth_type = WPS_AUTH_OPEN;
1861 cred->encr_type = WPS_ENCR_NONE;
1862 }
1863
1864 return 0;
1865}
1866
1867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1869 int id)
1870{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001871 u8 u[UUID_LEN], *use_uuid = NULL;
1872 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 struct wpa_ssid *ssid;
1874 struct wps_credential cred;
1875
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001876 if (uuid_str2bin(uuid, u) == 0)
1877 use_uuid = u;
1878 else if (hwaddr_aton(uuid, addr) == 0)
1879 use_addr = addr;
1880 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 return -1;
1882 ssid = wpa_config_get_network(wpa_s->conf, id);
1883 if (ssid == NULL || ssid->ssid == NULL)
1884 return -1;
1885
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001886 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001888 return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889}
1890
1891
1892int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1893 const char *pin, struct wps_new_ap_settings *settings)
1894{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001895 u8 u[UUID_LEN], *use_uuid = NULL;
1896 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 struct wps_credential cred;
1898 size_t len;
1899
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001900 if (uuid_str2bin(uuid, u) == 0)
1901 use_uuid = u;
1902 else if (hwaddr_aton(uuid, addr) == 0)
1903 use_addr = addr;
1904 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905 return -1;
1906 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1907 settings->encr == NULL || settings->key_hex == NULL)
1908 return -1;
1909
1910 os_memset(&cred, 0, sizeof(cred));
1911 len = os_strlen(settings->ssid_hex);
1912 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1913 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1914 return -1;
1915 cred.ssid_len = len / 2;
1916
1917 len = os_strlen(settings->key_hex);
1918 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1919 hexstr2bin(settings->key_hex, cred.key, len / 2))
1920 return -1;
1921 cred.key_len = len / 2;
1922
1923 if (os_strcmp(settings->auth, "OPEN") == 0)
1924 cred.auth_type = WPS_AUTH_OPEN;
1925 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1926 cred.auth_type = WPS_AUTH_WPAPSK;
1927 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1928 cred.auth_type = WPS_AUTH_WPA2PSK;
1929 else
1930 return -1;
1931
1932 if (os_strcmp(settings->encr, "NONE") == 0)
1933 cred.encr_type = WPS_ENCR_NONE;
1934 else if (os_strcmp(settings->encr, "WEP") == 0)
1935 cred.encr_type = WPS_ENCR_WEP;
1936 else if (os_strcmp(settings->encr, "TKIP") == 0)
1937 cred.encr_type = WPS_ENCR_TKIP;
1938 else if (os_strcmp(settings->encr, "CCMP") == 0)
1939 cred.encr_type = WPS_ENCR_AES;
1940 else
1941 return -1;
1942
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001943 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1944 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945}
1946
1947
Dmitry Shmidt04949592012-07-19 12:16:46 -07001948#ifdef CONFIG_WPS_NFC
1949struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1950 int ndef, const char *uuid)
1951{
1952 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001953 u8 u[UUID_LEN], *use_uuid = NULL;
1954 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001955
1956 if (!wpa_s->wps_er)
1957 return NULL;
1958
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001959 if (uuid_str2bin(uuid, u) == 0)
1960 use_uuid = u;
1961 else if (hwaddr_aton(uuid, addr) == 0)
1962 use_addr = addr;
1963 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07001964 return NULL;
1965
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001966 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001967 if (ndef && ret) {
1968 struct wpabuf *tmp;
1969 tmp = ndef_build_wifi(ret);
1970 wpabuf_free(ret);
1971 if (tmp == NULL)
1972 return NULL;
1973 ret = tmp;
1974 }
1975
1976 return ret;
1977}
1978#endif /* CONFIG_WPS_NFC */
1979
1980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981static int callbacks_pending = 0;
1982
1983static void wpas_wps_terminate_cb(void *ctx)
1984{
1985 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1986 if (--callbacks_pending <= 0)
1987 eloop_terminate();
1988}
1989#endif /* CONFIG_WPS_ER */
1990
1991
1992int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1993{
1994#ifdef CONFIG_WPS_ER
1995 if (wpa_s->wps_er) {
1996 callbacks_pending++;
1997 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1998 wpa_s->wps_er = NULL;
1999 return 1;
2000 }
2001#endif /* CONFIG_WPS_ER */
2002 return 0;
2003}
2004
2005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2007{
2008 struct wps_context *wps = wpa_s->wps;
2009
2010 if (wps == NULL)
2011 return;
2012
2013 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2014 wps->config_methods = wps_config_methods_str2bin(
2015 wpa_s->conf->config_methods);
2016 if ((wps->config_methods &
2017 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2018 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2019 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2020 "config methods are not allowed at the "
2021 "same time");
2022 wps->config_methods &= ~WPS_CONFIG_LABEL;
2023 }
2024 }
2025 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002026 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027
2028 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2029 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2030 WPS_DEV_TYPE_LEN);
2031
2032 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2033 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2034 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2035 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2036 }
2037
Dmitry Shmidt04949592012-07-19 12:16:46 -07002038 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2039 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2042 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2043
2044 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2045 wpas_wps_set_uuid(wpa_s, wps);
2046
2047 if (wpa_s->conf->changed_parameters &
2048 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2049 /* Update pointers to make sure they refer current values */
2050 wps->dev.device_name = wpa_s->conf->device_name;
2051 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2052 wps->dev.model_name = wpa_s->conf->model_name;
2053 wps->dev.model_number = wpa_s->conf->model_number;
2054 wps->dev.serial_number = wpa_s->conf->serial_number;
2055 }
2056}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002057
2058
2059#ifdef CONFIG_WPS_NFC
2060
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002061#ifdef CONFIG_WPS_ER
2062static struct wpabuf *
2063wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2064 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002065{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002066 struct wpabuf *ret;
2067 struct wps_credential cred;
2068
2069 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2070 return NULL;
2071
2072 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2073
2074 if (ndef && ret) {
2075 struct wpabuf *tmp;
2076 tmp = ndef_build_wifi(ret);
2077 wpabuf_free(ret);
2078 if (tmp == NULL)
2079 return NULL;
2080 ret = tmp;
2081 }
2082
2083 return ret;
2084}
2085#endif /* CONFIG_WPS_ER */
2086
2087
2088struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2089 int ndef, const char *id_str)
2090{
2091#ifdef CONFIG_WPS_ER
2092 if (id_str) {
2093 int id;
2094 char *end = NULL;
2095 struct wpa_ssid *ssid;
2096
2097 id = strtol(id_str, &end, 10);
2098 if (end && *end)
2099 return NULL;
2100
2101 ssid = wpa_config_get_network(wpa_s->conf, id);
2102 if (ssid == NULL)
2103 return NULL;
2104 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2105 }
2106#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002107#ifdef CONFIG_AP
2108 if (wpa_s->ap_iface)
2109 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2110#endif /* CONFIG_AP */
2111 return NULL;
2112}
2113
2114
Dmitry Shmidt04949592012-07-19 12:16:46 -07002115struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2116{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002117 if (wpa_s->conf->wps_nfc_pw_from_config) {
2118 return wps_nfc_token_build(ndef,
2119 wpa_s->conf->wps_nfc_dev_pw_id,
2120 wpa_s->conf->wps_nfc_dh_pubkey,
2121 wpa_s->conf->wps_nfc_dev_pw);
2122 }
2123
Dmitry Shmidt04949592012-07-19 12:16:46 -07002124 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2125 &wpa_s->conf->wps_nfc_dh_pubkey,
2126 &wpa_s->conf->wps_nfc_dh_privkey,
2127 &wpa_s->conf->wps_nfc_dev_pw);
2128}
2129
2130
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002131int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2132 const u8 *bssid,
2133 const struct wpabuf *dev_pw, u16 dev_pw_id,
2134 int p2p_group, const u8 *peer_pubkey_hash,
2135 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002136{
2137 struct wps_context *wps = wpa_s->wps;
2138 char pw[32 * 2 + 1];
2139
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002140 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2141 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2142 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2143 }
2144
Dmitry Shmidt04949592012-07-19 12:16:46 -07002145 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002146 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2147 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2148 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002149 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002150 }
2151
2152 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2153 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2154 "cannot start NFC-triggered connection", dev_pw_id);
2155 return -1;
2156 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002157
2158 dh5_free(wps->dh_ctx);
2159 wpabuf_free(wps->dh_pubkey);
2160 wpabuf_free(wps->dh_privkey);
2161 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2162 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2163 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2164 wps->dh_ctx = NULL;
2165 wpabuf_free(wps->dh_pubkey);
2166 wps->dh_pubkey = NULL;
2167 wpabuf_free(wps->dh_privkey);
2168 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002169 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002170 return -1;
2171 }
2172 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002173 if (wps->dh_ctx == NULL) {
2174 wpabuf_free(wps->dh_pubkey);
2175 wps->dh_pubkey = NULL;
2176 wpabuf_free(wps->dh_privkey);
2177 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002178 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002179 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002180 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002181
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002182 if (dev_pw) {
2183 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2184 wpabuf_head(dev_pw),
2185 wpabuf_len(dev_pw));
2186 }
2187 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2188 dev_pw ? pw : NULL,
2189 p2p_group, dev_pw_id, peer_pubkey_hash,
2190 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002191}
2192
2193
2194static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2195 struct wps_parse_attr *attr)
2196{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002197 /*
2198 * Disable existing networks temporarily to allow the newly learned
2199 * credential to be preferred. Enable the temporarily disabled networks
2200 * after 10 seconds.
2201 */
2202 wpas_wps_temp_disable(wpa_s, NULL);
2203 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2204 NULL);
2205
Dmitry Shmidt04949592012-07-19 12:16:46 -07002206 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2207 return -1;
2208
2209 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2210 return 0;
2211
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002212 if (attr->ap_channel) {
2213 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002214 int freq = 0;
2215
2216 if (chan >= 1 && chan <= 13)
2217 freq = 2407 + 5 * chan;
2218 else if (chan == 14)
2219 freq = 2484;
2220 else if (chan >= 30)
2221 freq = 5000 + 5 * chan;
2222
2223 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002224 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2225 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002226 wpa_s->after_wps = 5;
2227 wpa_s->wps_freq = freq;
2228 }
2229 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002230
2231 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2232 "based on the received credential added");
2233 wpa_s->normal_scans = 0;
2234 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002235 wpa_s->disconnected = 0;
2236 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002237
2238 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002239 wpa_supplicant_req_scan(wpa_s, 0, 0);
2240
2241 return 0;
2242}
2243
2244
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002245#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002246static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2247 struct wps_parse_attr *attr)
2248{
2249 return wps_registrar_add_nfc_password_token(
2250 wpa_s->wps->registrar, attr->oob_dev_password,
2251 attr->oob_dev_password_len);
2252}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002253#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002254
2255
2256static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2257 const struct wpabuf *wps)
2258{
2259 struct wps_parse_attr attr;
2260
2261 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2262
2263 if (wps_parse_msg(wps, &attr)) {
2264 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2265 return -1;
2266 }
2267
2268 if (attr.num_cred)
2269 return wpas_wps_use_cred(wpa_s, &attr);
2270
2271#ifdef CONFIG_WPS_ER
2272 if (attr.oob_dev_password)
2273 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2274#endif /* CONFIG_WPS_ER */
2275
2276 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2277 return -1;
2278}
2279
2280
2281int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002282 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002283{
2284 const struct wpabuf *wps = data;
2285 struct wpabuf *tmp = NULL;
2286 int ret;
2287
2288 if (wpabuf_len(data) < 4)
2289 return -1;
2290
2291 if (*wpabuf_head_u8(data) != 0x10) {
2292 /* Assume this contains full NDEF record */
2293 tmp = ndef_parse_wifi(data);
2294 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002295#ifdef CONFIG_P2P
2296 tmp = ndef_parse_p2p(data);
2297 if (tmp) {
2298 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2299 forced_freq);
2300 wpabuf_free(tmp);
2301 return ret;
2302 }
2303#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002304 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2305 return -1;
2306 }
2307 wps = tmp;
2308 }
2309
2310 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2311 wpabuf_free(tmp);
2312 return ret;
2313}
2314
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002315
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002316struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2317 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002318{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002319 struct wpabuf *ret;
2320
2321 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2322 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2323 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2324 return NULL;
2325
2326 ret = wps_build_nfc_handover_req(wpa_s->wps,
2327 wpa_s->conf->wps_nfc_dh_pubkey);
2328
2329 if (ndef && ret) {
2330 struct wpabuf *tmp;
2331 tmp = ndef_build_wifi(ret);
2332 wpabuf_free(ret);
2333 if (tmp == NULL)
2334 return NULL;
2335 ret = tmp;
2336 }
2337
2338 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002339}
2340
2341
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002342#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002343
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002344static struct wpabuf *
2345wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2346 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002347{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002348#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002349 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002350 u8 u[UUID_LEN], *use_uuid = NULL;
2351 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002352 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002353
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002354 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002355 return NULL;
2356
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002357 if (uuid == NULL)
2358 return NULL;
2359 if (uuid_str2bin(uuid, u) == 0)
2360 use_uuid = u;
2361 else if (hwaddr_aton(uuid, addr) == 0)
2362 use_addr = addr;
2363 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002364 return NULL;
2365
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002366 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2367 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2368 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2369 return NULL;
2370 }
2371
2372 wpas_wps_nfc_clear(wps);
2373 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2374 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2375 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2376 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2377 wpas_wps_nfc_clear(wps);
2378 return NULL;
2379 }
2380
2381 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2382 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002383 if (ndef && ret) {
2384 struct wpabuf *tmp;
2385 tmp = ndef_build_wifi(ret);
2386 wpabuf_free(ret);
2387 if (tmp == NULL)
2388 return NULL;
2389 ret = tmp;
2390 }
2391
2392 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002393#else /* CONFIG_WPS_ER */
2394 return NULL;
2395#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002396}
2397#endif /* CONFIG_WPS_NFC */
2398
2399
2400struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2401 int ndef, int cr, const char *uuid)
2402{
2403 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002404 if (!cr)
2405 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002406 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2407 if (ret)
2408 return ret;
2409 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002410}
2411
2412
2413int wpas_wps_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
2414 const struct wpabuf *data)
2415{
2416 /* TODO */
2417 return -1;
2418}
2419
2420
2421int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2422 const struct wpabuf *data)
2423{
2424 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002425 int ret = -1;
2426 u16 wsc_len;
2427 const u8 *pos;
2428 struct wpabuf msg;
2429 struct wps_parse_attr attr;
2430 u16 dev_pw_id;
2431 const u8 *bssid = NULL;
2432 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002433
2434 wps = ndef_parse_wifi(data);
2435 if (wps == NULL)
2436 return -1;
2437 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2438 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002439 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2440 if (wpabuf_len(wps) < 2) {
2441 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2442 "Message");
2443 goto out;
2444 }
2445 pos = wpabuf_head(wps);
2446 wsc_len = WPA_GET_BE16(pos);
2447 if (wsc_len > wpabuf_len(wps) - 2) {
2448 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2449 "in Wi-Fi Handover Select Message", wsc_len);
2450 goto out;
2451 }
2452 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002453
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002454 wpa_hexdump(MSG_DEBUG,
2455 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2456 pos, wsc_len);
2457 if (wsc_len < wpabuf_len(wps) - 2) {
2458 wpa_hexdump(MSG_DEBUG,
2459 "WPS: Ignore extra data after WSC attributes",
2460 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2461 }
2462
2463 wpabuf_set(&msg, pos, wsc_len);
2464 ret = wps_parse_msg(&msg, &attr);
2465 if (ret < 0) {
2466 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2467 "Wi-Fi Handover Select Message");
2468 goto out;
2469 }
2470
2471 if (attr.oob_dev_password == NULL ||
2472 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2473 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2474 "included in Wi-Fi Handover Select Message");
2475 ret = -1;
2476 goto out;
2477 }
2478
2479 if (attr.ssid == NULL) {
2480 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2481 "Select Message");
2482 ret = -1;
2483 goto out;
2484 }
2485
2486 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2487
2488 if (attr.mac_addr) {
2489 bssid = attr.mac_addr;
2490 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2491 MAC2STR(bssid));
2492 }
2493
2494 if (attr.rf_bands)
2495 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2496
2497 if (attr.ap_channel) {
2498 u16 chan = WPA_GET_BE16(attr.ap_channel);
2499
2500 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2501
2502 if (chan >= 1 && chan <= 13 &&
2503 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2504 freq = 2407 + 5 * chan;
2505 else if (chan == 14 &&
2506 (attr.rf_bands == NULL ||
2507 *attr.rf_bands & WPS_RF_24GHZ))
2508 freq = 2484;
2509 else if (chan >= 30 &&
2510 (attr.rf_bands == NULL ||
2511 *attr.rf_bands & WPS_RF_50GHZ))
2512 freq = 5000 + 5 * chan;
2513
2514 if (freq) {
2515 wpa_printf(MSG_DEBUG,
2516 "WPS: AP indicated channel %u -> %u MHz",
2517 chan, freq);
2518 }
2519 }
2520
2521 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2522 attr.oob_dev_password, attr.oob_dev_password_len);
2523 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2524 WPS_OOB_PUBKEY_HASH_LEN);
2525 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2526 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2527 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2528 ret = -1;
2529 goto out;
2530 }
2531 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2532 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2533
2534 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2535 attr.oob_dev_password,
2536 attr.ssid, attr.ssid_len, freq);
2537
2538out:
2539 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002540 return ret;
2541}
2542
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002543
2544int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2545 const struct wpabuf *req,
2546 const struct wpabuf *sel)
2547{
2548 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2549 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2550 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2551 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2552}
2553
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002554
2555int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2556 const struct wpabuf *req,
2557 const struct wpabuf *sel)
2558{
2559 struct wpabuf *wps;
2560 int ret = -1;
2561 u16 wsc_len;
2562 const u8 *pos;
2563 struct wpabuf msg;
2564 struct wps_parse_attr attr;
2565 u16 dev_pw_id;
2566
2567 /*
2568 * Enrollee/station is always initiator of the NFC connection handover,
2569 * so use the request message here to find Enrollee public key hash.
2570 */
2571 wps = ndef_parse_wifi(req);
2572 if (wps == NULL)
2573 return -1;
2574 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2575 "payload from NFC connection handover");
2576 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2577 if (wpabuf_len(wps) < 2) {
2578 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2579 "Message");
2580 goto out;
2581 }
2582 pos = wpabuf_head(wps);
2583 wsc_len = WPA_GET_BE16(pos);
2584 if (wsc_len > wpabuf_len(wps) - 2) {
2585 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2586 "in rt Wi-Fi Handover Request Message", wsc_len);
2587 goto out;
2588 }
2589 pos += 2;
2590
2591 wpa_hexdump(MSG_DEBUG,
2592 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2593 pos, wsc_len);
2594 if (wsc_len < wpabuf_len(wps) - 2) {
2595 wpa_hexdump(MSG_DEBUG,
2596 "WPS: Ignore extra data after WSC attributes",
2597 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2598 }
2599
2600 wpabuf_set(&msg, pos, wsc_len);
2601 ret = wps_parse_msg(&msg, &attr);
2602 if (ret < 0) {
2603 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2604 "Wi-Fi Handover Request Message");
2605 goto out;
2606 }
2607
2608 if (attr.oob_dev_password == NULL ||
2609 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2610 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2611 "included in Wi-Fi Handover Request Message");
2612 ret = -1;
2613 goto out;
2614 }
2615
2616 if (attr.uuid_e == NULL) {
2617 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2618 "Handover Request Message");
2619 ret = -1;
2620 goto out;
2621 }
2622
2623 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2624
2625 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2626 attr.oob_dev_password, attr.oob_dev_password_len);
2627 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2628 WPS_OOB_PUBKEY_HASH_LEN);
2629 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2630 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2631 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2632 ret = -1;
2633 goto out;
2634 }
2635 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2636 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2637
2638 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2639 attr.oob_dev_password,
2640 DEV_PW_NFC_CONNECTION_HANDOVER,
2641 NULL, 0, 1);
2642
2643out:
2644 wpabuf_free(wps);
2645 return ret;
2646}
2647
Dmitry Shmidt04949592012-07-19 12:16:46 -07002648#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002649
2650
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002651static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2652{
2653 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002654 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002655
2656 if (wpa_debug_level > MSG_DEBUG)
2657 return;
2658
2659 if (wpa_s->wps_ap == NULL)
2660 return;
2661
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002662 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002663
2664 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2665 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2666 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2667
2668 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2669 "tries=%d last_attempt=%d sec ago blacklist=%d",
2670 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2671 ap->last_attempt.sec > 0 ?
2672 (int) now.sec - (int) ap->last_attempt.sec : -1,
2673 e ? e->count : 0);
2674 }
2675}
2676
2677
2678static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2679 const u8 *bssid)
2680{
2681 size_t i;
2682
2683 if (wpa_s->wps_ap == NULL)
2684 return NULL;
2685
2686 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2687 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2688 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2689 return ap;
2690 }
2691
2692 return NULL;
2693}
2694
2695
2696static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2697 struct wpa_scan_res *res)
2698{
2699 struct wpabuf *wps;
2700 enum wps_ap_info_type type;
2701 struct wps_ap_info *ap;
2702 int r;
2703
2704 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2705 return;
2706
2707 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2708 if (wps == NULL)
2709 return;
2710
2711 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2712 if (r == 2)
2713 type = WPS_AP_SEL_REG_OUR;
2714 else if (r == 1)
2715 type = WPS_AP_SEL_REG;
2716 else
2717 type = WPS_AP_NOT_SEL_REG;
2718
2719 wpabuf_free(wps);
2720
2721 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2722 if (ap) {
2723 if (ap->type != type) {
2724 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2725 " changed type %d -> %d",
2726 MAC2STR(res->bssid), ap->type, type);
2727 ap->type = type;
2728 if (type != WPS_AP_NOT_SEL_REG)
2729 wpa_blacklist_del(wpa_s, ap->bssid);
2730 }
2731 return;
2732 }
2733
2734 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2735 sizeof(struct wps_ap_info));
2736 if (ap == NULL)
2737 return;
2738
2739 wpa_s->wps_ap = ap;
2740 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2741 wpa_s->num_wps_ap++;
2742
2743 os_memset(ap, 0, sizeof(*ap));
2744 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2745 ap->type = type;
2746 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2747 MAC2STR(ap->bssid), ap->type);
2748}
2749
2750
2751void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2752 struct wpa_scan_results *scan_res)
2753{
2754 size_t i;
2755
2756 for (i = 0; i < scan_res->num; i++)
2757 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2758
2759 wpas_wps_dump_ap_info(wpa_s);
2760}
2761
2762
2763void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2764{
2765 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002766
2767 wpa_s->after_wps = 0;
2768
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002769 if (!wpa_s->wps_ap_iter)
2770 return;
2771 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2772 if (ap == NULL)
2773 return;
2774 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002775 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002776}