blob: dfcc0693faf32e3b0b3081155fb8c03e6798cce4 [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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
514 struct wps_event_m2d *m2d)
515{
516 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
517 "dev_password_id=%d config_error=%d",
518 m2d->dev_password_id, m2d->config_error);
519 wpas_notify_wps_event_m2d(wpa_s, m2d);
520#ifdef CONFIG_P2P
521 if (wpa_s->parent && wpa_s->parent != wpa_s) {
522 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
523 "dev_password_id=%d config_error=%d",
524 m2d->dev_password_id, m2d->config_error);
525 }
526 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
527 /*
528 * Notify P2P from eloop timeout to avoid issues with the
529 * interface getting removed while processing a message.
530 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700531 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 NULL);
533 }
534#endif /* CONFIG_P2P */
535}
536
537
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700538static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
539{
540 struct wpa_supplicant *wpa_s = eloop_ctx;
541 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
542 wpas_clear_wps(wpa_s);
543}
544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545
546static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
547 struct wps_event_fail *fail)
548{
549 if (fail->error_indication > 0 &&
550 fail->error_indication < NUM_WPS_EI_VALUES) {
551 wpa_msg(wpa_s, MSG_INFO,
552 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
553 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700554 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 if (wpa_s->parent && wpa_s->parent != wpa_s)
556 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
557 "msg=%d config_error=%d reason=%d (%s)",
558 fail->msg, fail->config_error,
559 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700560 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 } else {
562 wpa_msg(wpa_s, MSG_INFO,
563 WPS_EVENT_FAIL "msg=%d config_error=%d",
564 fail->msg, fail->config_error);
565 if (wpa_s->parent && wpa_s->parent != wpa_s)
566 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
567 "msg=%d config_error=%d",
568 fail->msg, fail->config_error);
569 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700570
571 /*
572 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
573 */
574 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
575 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
576 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700579#ifdef CONFIG_P2P
580 wpas_p2p_wps_failed(wpa_s, fail);
581#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582}
583
584
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800585static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
586
587static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
588{
589 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800590 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800591
592 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
593
594 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
595 if (ssid->disabled_for_connect && ssid->disabled) {
596 ssid->disabled_for_connect = 0;
597 ssid->disabled = 0;
598 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800599 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800600 }
601 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800602
603 if (changed) {
604#ifndef CONFIG_NO_CONFIG_WRITE
605 if (wpa_s->conf->update_config &&
606 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
607 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
608 "configuration");
609 }
610#endif /* CONFIG_NO_CONFIG_WRITE */
611 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800612}
613
614
615static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
616{
617 struct wpa_supplicant *wpa_s = eloop_ctx;
618 /* Enable the networks disabled during wpas_wps_reassoc */
619 wpas_wps_reenable_networks(wpa_s);
620}
621
622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
624{
625 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
626 wpa_s->wps_success = 1;
627 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700628 if (wpa_s->current_ssid)
629 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
630 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800631
632 /*
633 * Enable the networks disabled during wpas_wps_reassoc after 10
634 * seconds. The 10 seconds timer is to allow the data connection to be
635 * formed before allowing other networks to be selected.
636 */
637 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
638 NULL);
639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640#ifdef CONFIG_P2P
641 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
642#endif /* CONFIG_P2P */
643}
644
645
646static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
647 struct wps_event_er_ap *ap)
648{
649 char uuid_str[100];
650 char dev_type[WPS_DEV_TYPE_BUFSIZE];
651
652 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
653 if (ap->pri_dev_type)
654 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
655 sizeof(dev_type));
656 else
657 dev_type[0] = '\0';
658
659 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
660 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
661 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
662 ap->friendly_name ? ap->friendly_name : "",
663 ap->manufacturer ? ap->manufacturer : "",
664 ap->model_description ? ap->model_description : "",
665 ap->model_name ? ap->model_name : "",
666 ap->manufacturer_url ? ap->manufacturer_url : "",
667 ap->model_url ? ap->model_url : "");
668}
669
670
671static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
672 struct wps_event_er_ap *ap)
673{
674 char uuid_str[100];
675 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
676 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
677}
678
679
680static void wpa_supplicant_wps_event_er_enrollee_add(
681 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
682{
683 char uuid_str[100];
684 char dev_type[WPS_DEV_TYPE_BUFSIZE];
685
686 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
687 if (enrollee->pri_dev_type)
688 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
689 sizeof(dev_type));
690 else
691 dev_type[0] = '\0';
692
693 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
694 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
695 "|%s|%s|%s|%s|%s|",
696 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
697 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
698 enrollee->dev_name ? enrollee->dev_name : "",
699 enrollee->manufacturer ? enrollee->manufacturer : "",
700 enrollee->model_name ? enrollee->model_name : "",
701 enrollee->model_number ? enrollee->model_number : "",
702 enrollee->serial_number ? enrollee->serial_number : "");
703}
704
705
706static void wpa_supplicant_wps_event_er_enrollee_remove(
707 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
708{
709 char uuid_str[100];
710 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
711 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
712 uuid_str, MAC2STR(enrollee->mac_addr));
713}
714
715
716static void wpa_supplicant_wps_event_er_ap_settings(
717 struct wpa_supplicant *wpa_s,
718 struct wps_event_er_ap_settings *ap_settings)
719{
720 char uuid_str[100];
721 char key_str[65];
722 const struct wps_credential *cred = ap_settings->cred;
723
724 key_str[0] = '\0';
725 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
726 if (cred->key_len >= 8 && cred->key_len <= 64) {
727 os_memcpy(key_str, cred->key, cred->key_len);
728 key_str[cred->key_len] = '\0';
729 }
730 }
731
732 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
733 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
734 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
735 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
736 "key=%s",
737 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
738 cred->auth_type, cred->encr_type, key_str);
739}
740
741
742static void wpa_supplicant_wps_event_er_set_sel_reg(
743 struct wpa_supplicant *wpa_s,
744 struct wps_event_er_set_selected_registrar *ev)
745{
746 char uuid_str[100];
747
748 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
749 switch (ev->state) {
750 case WPS_ER_SET_SEL_REG_START:
751 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
752 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
753 "sel_reg_config_methods=0x%x",
754 uuid_str, ev->sel_reg, ev->dev_passwd_id,
755 ev->sel_reg_config_methods);
756 break;
757 case WPS_ER_SET_SEL_REG_DONE:
758 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
759 "uuid=%s state=DONE", uuid_str);
760 break;
761 case WPS_ER_SET_SEL_REG_FAILED:
762 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
763 "uuid=%s state=FAILED", uuid_str);
764 break;
765 }
766}
767
768
769static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
770 union wps_event_data *data)
771{
772 struct wpa_supplicant *wpa_s = ctx;
773 switch (event) {
774 case WPS_EV_M2D:
775 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
776 break;
777 case WPS_EV_FAIL:
778 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
779 break;
780 case WPS_EV_SUCCESS:
781 wpa_supplicant_wps_event_success(wpa_s);
782 break;
783 case WPS_EV_PWD_AUTH_FAIL:
784#ifdef CONFIG_AP
785 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
786 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
787#endif /* CONFIG_AP */
788 break;
789 case WPS_EV_PBC_OVERLAP:
790 break;
791 case WPS_EV_PBC_TIMEOUT:
792 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700793 case WPS_EV_PBC_ACTIVE:
794 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
795 break;
796 case WPS_EV_PBC_DISABLE:
797 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
798 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 case WPS_EV_ER_AP_ADD:
800 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
801 break;
802 case WPS_EV_ER_AP_REMOVE:
803 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
804 break;
805 case WPS_EV_ER_ENROLLEE_ADD:
806 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
807 &data->enrollee);
808 break;
809 case WPS_EV_ER_ENROLLEE_REMOVE:
810 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
811 &data->enrollee);
812 break;
813 case WPS_EV_ER_AP_SETTINGS:
814 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
815 &data->ap_settings);
816 break;
817 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
818 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
819 &data->set_sel_reg);
820 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800821 case WPS_EV_AP_PIN_SUCCESS:
822 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823 }
824}
825
826
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700827static int wpa_supplicant_wps_rf_band(void *ctx)
828{
829 struct wpa_supplicant *wpa_s = ctx;
830
831 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
832 return 0;
833
834 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
835}
836
837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
839{
840 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
841 eap_is_wps_pin_enrollee(&ssid->eap))
842 return WPS_REQ_ENROLLEE;
843 else
844 return WPS_REQ_REGISTRAR;
845}
846
847
848static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
849{
850 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700851 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
852
Dmitry Shmidt051af732013-10-22 13:52:46 -0700853 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700854 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700855
Jouni Malinen75ecf522011-06-27 15:19:46 -0700856 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800858 /* Enable the networks disabled during wpas_wps_reassoc */
859 wpas_wps_reenable_networks(wpa_s);
860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800862 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863
864 /* Remove any existing WPS network from configuration */
865 ssid = wpa_s->conf->ssid;
866 while (ssid) {
867 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
868 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700869 wpa_supplicant_deauthenticate(
870 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 }
872 id = ssid->id;
873 remove_ssid = ssid;
874 } else
875 id = -1;
876 ssid = ssid->next;
877 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700878 if (prev_current == remove_ssid) {
879 wpa_sm_set_config(wpa_s->wpa, NULL);
880 eapol_sm_notify_config(wpa_s->eapol, NULL,
881 NULL);
882 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 wpas_notify_network_removed(wpa_s, remove_ssid);
884 wpa_config_remove_network(wpa_s->conf, id);
885 }
886 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700887
888 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889}
890
891
892static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
893{
894 struct wpa_supplicant *wpa_s = eloop_ctx;
895 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
896 "out");
897 wpas_clear_wps(wpa_s);
898}
899
900
901static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800902 int registrar, const u8 *dev_addr,
903 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904{
905 struct wpa_ssid *ssid;
906
907 ssid = wpa_config_add_network(wpa_s->conf);
908 if (ssid == NULL)
909 return NULL;
910 wpas_notify_network_added(wpa_s, ssid);
911 wpa_config_set_network_defaults(ssid);
912 ssid->temporary = 1;
913 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
914 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
915 wpa_config_set(ssid, "identity", registrar ?
916 "\"" WSC_ID_REGISTRAR "\"" :
917 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
918 wpas_notify_network_removed(wpa_s, ssid);
919 wpa_config_remove_network(wpa_s->conf, ssid->id);
920 return NULL;
921 }
922
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800923#ifdef CONFIG_P2P
924 if (dev_addr)
925 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
926#endif /* CONFIG_P2P */
927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 if (bssid) {
929#ifndef CONFIG_P2P
930 struct wpa_bss *bss;
931 int count = 0;
932#endif /* CONFIG_P2P */
933
934 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
935 ssid->bssid_set = 1;
936
937 /*
938 * Note: With P2P, the SSID may change at the time the WPS
939 * provisioning is started, so better not filter the AP based
940 * on the current SSID in the scan results.
941 */
942#ifndef CONFIG_P2P
943 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
944 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
945 continue;
946
947 os_free(ssid->ssid);
948 ssid->ssid = os_malloc(bss->ssid_len);
949 if (ssid->ssid == NULL)
950 break;
951 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
952 ssid->ssid_len = bss->ssid_len;
953 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
954 "scan results",
955 ssid->ssid, ssid->ssid_len);
956 count++;
957 }
958
959 if (count > 1) {
960 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
961 "for the AP; use wildcard");
962 os_free(ssid->ssid);
963 ssid->ssid = NULL;
964 ssid->ssid_len = 0;
965 }
966#endif /* CONFIG_P2P */
967 }
968
969 return ssid;
970}
971
972
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700973static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
974 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975{
976 struct wpa_ssid *ssid;
977
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978 if (wpa_s->current_ssid)
979 wpa_supplicant_deauthenticate(
980 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 /* Mark all other networks disabled and trigger reassociation */
983 ssid = wpa_s->conf->ssid;
984 while (ssid) {
985 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800986 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700987 /*
988 * In case the network object corresponds to a persistent group
989 * then do not send out network disabled signal. In addition,
990 * do not change disabled status of persistent network objects
991 * from 2 to 1 should we connect to another network.
992 */
993 if (was_disabled != 2) {
994 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800995 if (was_disabled != ssid->disabled) {
996 if (ssid->disabled)
997 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700998 wpas_notify_network_enabled_changed(wpa_s,
999 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001000 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001001 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 ssid = ssid->next;
1003 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001004}
1005
1006
1007static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001008 struct wpa_ssid *selected, const u8 *bssid,
1009 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001010{
1011 struct wpa_bss *bss;
1012
1013 wpa_s->after_wps = 0;
1014 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001015 if (freq) {
1016 wpa_s->after_wps = 5;
1017 wpa_s->wps_freq = freq;
1018 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001019 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1020 if (bss && bss->freq > 0) {
1021 wpa_s->known_wps_freq = 1;
1022 wpa_s->wps_freq = bss->freq;
1023 }
1024 }
1025
1026 wpas_wps_temp_disable(wpa_s, selected);
1027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 wpa_s->disconnected = 0;
1029 wpa_s->reassociate = 1;
1030 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001031 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 wpa_s->wps_success = 0;
1033 wpa_s->blacklist_cleared = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001034
1035 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 wpa_supplicant_req_scan(wpa_s, 0, 0);
1037}
1038
1039
1040int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1041 int p2p_group)
1042{
1043 struct wpa_ssid *ssid;
1044 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001045 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 if (ssid == NULL)
1047 return -1;
1048 ssid->temporary = 1;
1049 ssid->p2p_group = p2p_group;
1050#ifdef CONFIG_P2P
1051 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1052 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1053 if (ssid->ssid) {
1054 ssid->ssid_len = wpa_s->go_params->ssid_len;
1055 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1056 ssid->ssid_len);
1057 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1058 "SSID", ssid->ssid, ssid->ssid_len);
1059 }
1060 }
1061#endif /* CONFIG_P2P */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001062 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1063 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 if (wpa_s->wps_fragment_size)
1065 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1066 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1067 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001068 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 return 0;
1070}
1071
1072
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001073static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1074 const u8 *dev_addr, const u8 *bssid,
1075 const char *pin, int p2p_group, u16 dev_pw_id,
1076 const u8 *peer_pubkey_hash,
1077 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078{
1079 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001080 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001082 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083
1084 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001085 if (bssid && is_zero_ether_addr(bssid))
1086 bssid = NULL;
1087 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1088 if (ssid == NULL) {
1089 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001091 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 ssid->temporary = 1;
1093 ssid->p2p_group = p2p_group;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001094 if (ssid_val) {
1095 ssid->ssid = os_malloc(ssid_len);
1096 if (ssid->ssid) {
1097 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1098 ssid->ssid_len = ssid_len;
1099 }
1100 }
1101 if (peer_pubkey_hash) {
1102 os_memcpy(hash, " pkhash=", 8);
1103 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1104 peer_pubkey_hash,
1105 WPS_OOB_PUBKEY_HASH_LEN);
1106 } else {
1107 hash[0] = '\0';
1108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109#ifdef CONFIG_P2P
1110 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1111 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1112 if (ssid->ssid) {
1113 ssid->ssid_len = wpa_s->go_params->ssid_len;
1114 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1115 ssid->ssid_len);
1116 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1117 "SSID", ssid->ssid, ssid->ssid_len);
1118 }
1119 }
1120#endif /* CONFIG_P2P */
1121 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001122 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1123 pin, dev_pw_id, hash);
1124 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1125 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1126 dev_pw_id, hash);
1127 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 rpin = wps_generate_pin();
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001129 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1130 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001132 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1133 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001134 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001135 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 if (wpa_s->wps_fragment_size)
1137 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1138 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1139 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001140 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001141 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 return rpin;
1143}
1144
1145
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001146int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1147 const char *pin, int p2p_group, u16 dev_pw_id)
1148{
1149 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1150 dev_pw_id, NULL, NULL, 0, 0);
1151}
1152
1153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154/* Cancel the wps pbc/pin requests */
1155int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1156{
1157#ifdef CONFIG_AP
1158 if (wpa_s->ap_iface) {
1159 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1160 return wpa_supplicant_ap_wps_cancel(wpa_s);
1161 }
1162#endif /* CONFIG_AP */
1163
Dmitry Shmidt04949592012-07-19 12:16:46 -07001164 if (wpa_s->wpa_state == WPA_SCANNING ||
1165 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1167 wpa_supplicant_cancel_scan(wpa_s);
1168 wpas_clear_wps(wpa_s);
1169 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1170 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1171 "deauthenticate");
1172 wpa_supplicant_deauthenticate(wpa_s,
1173 WLAN_REASON_DEAUTH_LEAVING);
1174 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001175 } else {
1176 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001177 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001178 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1179 0)
1180 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181 }
1182
Dmitry Shmidt051af732013-10-22 13:52:46 -07001183 wpa_s->after_wps = 0;
1184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 return 0;
1186}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187
1188
1189int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1190 const char *pin, struct wps_new_ap_settings *settings)
1191{
1192 struct wpa_ssid *ssid;
1193 char val[200];
1194 char *pos, *end;
1195 int res;
1196
1197 if (!pin)
1198 return -1;
1199 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001200 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 if (ssid == NULL)
1202 return -1;
1203 ssid->temporary = 1;
1204 pos = val;
1205 end = pos + sizeof(val);
1206 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1207 if (res < 0 || res >= end - pos)
1208 return -1;
1209 pos += res;
1210 if (settings) {
1211 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1212 "new_encr=%s new_key=%s",
1213 settings->ssid_hex, settings->auth,
1214 settings->encr, settings->key_hex);
1215 if (res < 0 || res >= end - pos)
1216 return -1;
1217 pos += res;
1218 }
1219 res = os_snprintf(pos, end - pos, "\"");
1220 if (res < 0 || res >= end - pos)
1221 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001222 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1223 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 if (wpa_s->wps_fragment_size)
1225 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1226 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1227 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001228 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 return 0;
1230}
1231
1232
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001233static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1234 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 size_t psk_len)
1236{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001237 if (is_zero_ether_addr(p2p_dev_addr)) {
1238 wpa_printf(MSG_DEBUG,
1239 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1240 MAC2STR(mac_addr));
1241 } else {
1242 wpa_printf(MSG_DEBUG,
1243 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1244 " P2P Device Addr " MACSTR,
1245 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1248
1249 /* TODO */
1250
1251 return 0;
1252}
1253
1254
1255static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1256 const struct wps_device_data *dev)
1257{
1258 char uuid[40], txt[400];
1259 int len;
1260 char devtype[WPS_DEV_TYPE_BUFSIZE];
1261 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1262 return;
1263 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1264 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1265 " [%s|%s|%s|%s|%s|%s]",
1266 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1267 dev->manufacturer, dev->model_name,
1268 dev->model_number, dev->serial_number,
1269 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1270 sizeof(devtype)));
1271 if (len > 0 && len < (int) sizeof(txt))
1272 wpa_printf(MSG_INFO, "%s", txt);
1273}
1274
1275
1276static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1277 u16 sel_reg_config_methods)
1278{
1279#ifdef CONFIG_WPS_ER
1280 struct wpa_supplicant *wpa_s = ctx;
1281
1282 if (wpa_s->wps_er == NULL)
1283 return;
1284 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1285 "dev_password_id=%u sel_reg_config_methods=0x%x",
1286 sel_reg, dev_passwd_id, sel_reg_config_methods);
1287 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1288 sel_reg_config_methods);
1289#endif /* CONFIG_WPS_ER */
1290}
1291
1292
1293static u16 wps_fix_config_methods(u16 config_methods)
1294{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295 if ((config_methods &
1296 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1297 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1298 wpa_printf(MSG_INFO, "WPS: Converting display to "
1299 "virtual_display for WPS 2.0 compliance");
1300 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1301 }
1302 if ((config_methods &
1303 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1304 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1305 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1306 "virtual_push_button for WPS 2.0 compliance");
1307 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309
1310 return config_methods;
1311}
1312
1313
1314static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1315 struct wps_context *wps)
1316{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001317 char buf[50];
1318 const char *src;
1319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 if (is_nil_uuid(wpa_s->conf->uuid)) {
1321 struct wpa_supplicant *first;
1322 first = wpa_s->global->ifaces;
1323 while (first && first->next)
1324 first = first->next;
1325 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001326 if (wps != wpa_s->global->ifaces->wps)
1327 os_memcpy(wps->uuid,
1328 wpa_s->global->ifaces->wps->uuid,
1329 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001330 src = "from the first interface";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 } else {
1332 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001333 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 }
1335 } else {
1336 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001337 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001339
1340 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1341 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342}
1343
1344
Dmitry Shmidt04949592012-07-19 12:16:46 -07001345static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1346 struct wps_context *wps)
1347{
1348 wpabuf_free(wps->dev.vendor_ext_m1);
1349 wps->dev.vendor_ext_m1 = NULL;
1350
1351 if (wpa_s->conf->wps_vendor_ext_m1) {
1352 wps->dev.vendor_ext_m1 =
1353 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1354 if (!wps->dev.vendor_ext_m1) {
1355 wpa_printf(MSG_ERROR, "WPS: Cannot "
1356 "allocate memory for vendor_ext_m1");
1357 }
1358 }
1359}
1360
1361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362int wpas_wps_init(struct wpa_supplicant *wpa_s)
1363{
1364 struct wps_context *wps;
1365 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001366 struct hostapd_hw_modes *modes;
1367 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368
1369 wps = os_zalloc(sizeof(*wps));
1370 if (wps == NULL)
1371 return -1;
1372
1373 wps->cred_cb = wpa_supplicant_wps_cred;
1374 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001375 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376 wps->cb_ctx = wpa_s;
1377
1378 wps->dev.device_name = wpa_s->conf->device_name;
1379 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1380 wps->dev.model_name = wpa_s->conf->model_name;
1381 wps->dev.model_number = wpa_s->conf->model_number;
1382 wps->dev.serial_number = wpa_s->conf->serial_number;
1383 wps->config_methods =
1384 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1385 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1386 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1387 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1388 "methods are not allowed at the same time");
1389 os_free(wps);
1390 return -1;
1391 }
1392 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001393 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1395 WPS_DEV_TYPE_LEN);
1396
1397 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1398 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1399 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1400
Dmitry Shmidt04949592012-07-19 12:16:46 -07001401 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001404 modes = wpa_s->hw.modes;
1405 if (modes) {
1406 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1407 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1408 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1409 wps->dev.rf_bands |= WPS_RF_24GHZ;
1410 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1411 wps->dev.rf_bands |= WPS_RF_50GHZ;
1412 }
1413 }
1414 if (wps->dev.rf_bands == 0) {
1415 /*
1416 * Default to claiming support for both bands if the driver
1417 * does not provide support for fetching supported bands.
1418 */
1419 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1420 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1422 wpas_wps_set_uuid(wpa_s, wps);
1423
1424 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1425 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1426
1427 os_memset(&rcfg, 0, sizeof(rcfg));
1428 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1429 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1430 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1431 rcfg.cb_ctx = wpa_s;
1432
1433 wps->registrar = wps_registrar_init(wps, &rcfg);
1434 if (wps->registrar == NULL) {
1435 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1436 os_free(wps);
1437 return -1;
1438 }
1439
1440 wpa_s->wps = wps;
1441
1442 return 0;
1443}
1444
1445
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001446#ifdef CONFIG_WPS_ER
1447static void wpas_wps_nfc_clear(struct wps_context *wps)
1448{
1449 wps->ap_nfc_dev_pw_id = 0;
1450 wpabuf_free(wps->ap_nfc_dh_pubkey);
1451 wps->ap_nfc_dh_pubkey = NULL;
1452 wpabuf_free(wps->ap_nfc_dh_privkey);
1453 wps->ap_nfc_dh_privkey = NULL;
1454 wpabuf_free(wps->ap_nfc_dev_pw);
1455 wps->ap_nfc_dev_pw = NULL;
1456}
1457#endif /* CONFIG_WPS_ER */
1458
1459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1461{
1462 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001463 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001464 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001465 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466
1467 if (wpa_s->wps == NULL)
1468 return;
1469
1470#ifdef CONFIG_WPS_ER
1471 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1472 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001473 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474#endif /* CONFIG_WPS_ER */
1475
1476 wps_registrar_deinit(wpa_s->wps->registrar);
1477 wpabuf_free(wpa_s->wps->dh_pubkey);
1478 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001479 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480 os_free(wpa_s->wps->network_key);
1481 os_free(wpa_s->wps);
1482 wpa_s->wps = NULL;
1483}
1484
1485
1486int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001487 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488{
1489 struct wpabuf *wps_ie;
1490
1491 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1492 return -1;
1493
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001494 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1496 if (!wps_ie) {
1497 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1498 return 0;
1499 }
1500
1501 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1502 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1503 "without active PBC Registrar");
1504 wpabuf_free(wps_ie);
1505 return 0;
1506 }
1507
1508 /* TODO: overlap detection */
1509 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1510 "(Active PBC)");
1511 wpabuf_free(wps_ie);
1512 return 1;
1513 }
1514
1515 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1516 if (!wps_ie) {
1517 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1518 return 0;
1519 }
1520
1521 /*
1522 * Start with WPS APs that advertise our address as an
1523 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1524 * allow any WPS AP after couple of scans since some APs do not
1525 * set Selected Registrar attribute properly when using
1526 * external Registrar.
1527 */
1528 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1529 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1530 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1531 "without active PIN Registrar");
1532 wpabuf_free(wps_ie);
1533 return 0;
1534 }
1535 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1536 } else {
1537 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1538 "(Authorized MAC or Active PIN)");
1539 }
1540 wpabuf_free(wps_ie);
1541 return 1;
1542 }
1543
1544 if (wps_ie) {
1545 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1546 wpabuf_free(wps_ie);
1547 return 1;
1548 }
1549
1550 return -1;
1551}
1552
1553
1554int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1555 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001556 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557{
1558 struct wpabuf *wps_ie = NULL;
1559 int ret = 0;
1560
1561 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001562 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1564 /* allow wildcard SSID for WPS PBC */
1565 ret = 1;
1566 }
1567 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001568 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 if (wps_ie &&
1570 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1571 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1572 /* allow wildcard SSID for WPS PIN */
1573 ret = 1;
1574 }
1575 }
1576
1577 if (!ret && ssid->bssid_set &&
1578 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1579 /* allow wildcard SSID due to hardcoded BSSID match */
1580 ret = 1;
1581 }
1582
1583#ifdef CONFIG_WPS_STRICT
1584 if (wps_ie) {
1585 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1586 0, bss->bssid) < 0)
1587 ret = 0;
1588 if (bss->beacon_ie_len) {
1589 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001590 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 bss, WPS_IE_VENDOR_TYPE);
1592 if (bcn_wps == NULL) {
1593 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1594 "missing from AP Beacon");
1595 ret = 0;
1596 } else {
1597 if (wps_validate_beacon(wps_ie) < 0)
1598 ret = 0;
1599 wpabuf_free(bcn_wps);
1600 }
1601 }
1602 }
1603#endif /* CONFIG_WPS_STRICT */
1604
1605 wpabuf_free(wps_ie);
1606
1607 return ret;
1608}
1609
1610
1611int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1612 struct wpa_bss *selected, struct wpa_ssid *ssid)
1613{
1614 const u8 *sel_uuid, *uuid;
1615 struct wpabuf *wps_ie;
1616 int ret = 0;
1617 struct wpa_bss *bss;
1618
1619 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1620 return 0;
1621
1622 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1623 "present in scan results; selected BSSID " MACSTR,
1624 MAC2STR(selected->bssid));
1625
1626 /* Make sure that only one AP is in active PBC mode */
1627 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1628 if (wps_ie) {
1629 sel_uuid = wps_get_uuid_e(wps_ie);
1630 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1631 sel_uuid, UUID_LEN);
1632 } else {
1633 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1634 "WPS IE?!");
1635 sel_uuid = NULL;
1636 }
1637
1638 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1639 struct wpabuf *ie;
1640 if (bss == selected)
1641 continue;
1642 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1643 if (!ie)
1644 continue;
1645 if (!wps_is_selected_pbc_registrar(ie)) {
1646 wpabuf_free(ie);
1647 continue;
1648 }
1649 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1650 MACSTR, MAC2STR(bss->bssid));
1651 uuid = wps_get_uuid_e(ie);
1652 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1653 uuid, UUID_LEN);
1654 if (sel_uuid == NULL || uuid == NULL ||
1655 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1656 ret = 1; /* PBC overlap */
1657 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1658 MACSTR " and " MACSTR,
1659 MAC2STR(selected->bssid),
1660 MAC2STR(bss->bssid));
1661 wpabuf_free(ie);
1662 break;
1663 }
1664
1665 /* TODO: verify that this is reasonable dual-band situation */
1666
1667 wpabuf_free(ie);
1668 }
1669
1670 wpabuf_free(wps_ie);
1671
1672 return ret;
1673}
1674
1675
1676void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1677{
1678 struct wpa_bss *bss;
1679 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1680
1681 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1682 return;
1683
1684 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1685 struct wpabuf *ie;
1686 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1687 if (!ie)
1688 continue;
1689 if (wps_is_selected_pbc_registrar(ie))
1690 pbc++;
1691 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1692 auth++;
1693 else if (wps_is_selected_pin_registrar(ie))
1694 pin++;
1695 else
1696 wps++;
1697 wpabuf_free(ie);
1698 }
1699
1700 if (pbc)
1701 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1702 else if (auth)
1703 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1704 else if (pin)
1705 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1706 else if (wps)
1707 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1708}
1709
1710
1711int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1712{
1713 struct wpa_ssid *ssid;
1714
1715 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1716 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1717 return 1;
1718 }
1719
1720 return 0;
1721}
1722
1723
1724int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1725 char *end)
1726{
1727 struct wpabuf *wps_ie;
1728 int ret;
1729
1730 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1731 if (wps_ie == NULL)
1732 return 0;
1733
1734 ret = wps_attr_text(wps_ie, buf, end);
1735 wpabuf_free(wps_ie);
1736 return ret;
1737}
1738
1739
1740int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1741{
1742#ifdef CONFIG_WPS_ER
1743 if (wpa_s->wps_er) {
1744 wps_er_refresh(wpa_s->wps_er);
1745 return 0;
1746 }
1747 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1748 if (wpa_s->wps_er == NULL)
1749 return -1;
1750 return 0;
1751#else /* CONFIG_WPS_ER */
1752 return 0;
1753#endif /* CONFIG_WPS_ER */
1754}
1755
1756
1757int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1758{
1759#ifdef CONFIG_WPS_ER
1760 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1761 wpa_s->wps_er = NULL;
1762#endif /* CONFIG_WPS_ER */
1763 return 0;
1764}
1765
1766
1767#ifdef CONFIG_WPS_ER
1768int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1769 const char *uuid, const char *pin)
1770{
1771 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001772 const u8 *use_uuid = NULL;
1773 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001775 if (os_strcmp(uuid, "any") == 0) {
1776 } else if (uuid_str2bin(uuid, u) == 0) {
1777 use_uuid = u;
1778 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1779 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1780 if (use_uuid == NULL)
1781 return -1;
1782 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 return -1;
1784 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001785 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 (const u8 *) pin, os_strlen(pin), 300);
1787}
1788
1789
1790int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1791{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001792 u8 u[UUID_LEN], *use_uuid = NULL;
1793 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001795 if (uuid_str2bin(uuid, u) == 0)
1796 use_uuid = u;
1797 else if (hwaddr_aton(uuid, addr) == 0)
1798 use_addr = addr;
1799 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001801 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802}
1803
1804
1805int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1806 const char *pin)
1807{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001808 u8 u[UUID_LEN], *use_uuid = NULL;
1809 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001811 if (uuid_str2bin(uuid, u) == 0)
1812 use_uuid = u;
1813 else if (hwaddr_aton(uuid, addr) == 0)
1814 use_addr = addr;
1815 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001817
1818 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 os_strlen(pin));
1820}
1821
1822
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001823static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1824 struct wps_credential *cred)
1825{
1826 os_memset(cred, 0, sizeof(*cred));
1827 if (ssid->ssid_len > 32)
1828 return -1;
1829 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1830 cred->ssid_len = ssid->ssid_len;
1831 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1832 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1833 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1834 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1835 cred->encr_type = WPS_ENCR_AES;
1836 else
1837 cred->encr_type = WPS_ENCR_TKIP;
1838 if (ssid->passphrase) {
1839 cred->key_len = os_strlen(ssid->passphrase);
1840 if (cred->key_len >= 64)
1841 return -1;
1842 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1843 } else if (ssid->psk_set) {
1844 cred->key_len = 32;
1845 os_memcpy(cred->key, ssid->psk, 32);
1846 } else
1847 return -1;
1848 } else {
1849 cred->auth_type = WPS_AUTH_OPEN;
1850 cred->encr_type = WPS_ENCR_NONE;
1851 }
1852
1853 return 0;
1854}
1855
1856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1858 int id)
1859{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001860 u8 u[UUID_LEN], *use_uuid = NULL;
1861 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 struct wpa_ssid *ssid;
1863 struct wps_credential cred;
1864
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001865 if (uuid_str2bin(uuid, u) == 0)
1866 use_uuid = u;
1867 else if (hwaddr_aton(uuid, addr) == 0)
1868 use_addr = addr;
1869 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 return -1;
1871 ssid = wpa_config_get_network(wpa_s->conf, id);
1872 if (ssid == NULL || ssid->ssid == NULL)
1873 return -1;
1874
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001875 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001877 return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878}
1879
1880
1881int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1882 const char *pin, struct wps_new_ap_settings *settings)
1883{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001884 u8 u[UUID_LEN], *use_uuid = NULL;
1885 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 struct wps_credential cred;
1887 size_t len;
1888
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001889 if (uuid_str2bin(uuid, u) == 0)
1890 use_uuid = u;
1891 else if (hwaddr_aton(uuid, addr) == 0)
1892 use_addr = addr;
1893 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 return -1;
1895 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1896 settings->encr == NULL || settings->key_hex == NULL)
1897 return -1;
1898
1899 os_memset(&cred, 0, sizeof(cred));
1900 len = os_strlen(settings->ssid_hex);
1901 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1902 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1903 return -1;
1904 cred.ssid_len = len / 2;
1905
1906 len = os_strlen(settings->key_hex);
1907 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1908 hexstr2bin(settings->key_hex, cred.key, len / 2))
1909 return -1;
1910 cred.key_len = len / 2;
1911
1912 if (os_strcmp(settings->auth, "OPEN") == 0)
1913 cred.auth_type = WPS_AUTH_OPEN;
1914 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1915 cred.auth_type = WPS_AUTH_WPAPSK;
1916 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1917 cred.auth_type = WPS_AUTH_WPA2PSK;
1918 else
1919 return -1;
1920
1921 if (os_strcmp(settings->encr, "NONE") == 0)
1922 cred.encr_type = WPS_ENCR_NONE;
1923 else if (os_strcmp(settings->encr, "WEP") == 0)
1924 cred.encr_type = WPS_ENCR_WEP;
1925 else if (os_strcmp(settings->encr, "TKIP") == 0)
1926 cred.encr_type = WPS_ENCR_TKIP;
1927 else if (os_strcmp(settings->encr, "CCMP") == 0)
1928 cred.encr_type = WPS_ENCR_AES;
1929 else
1930 return -1;
1931
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001932 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1933 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934}
1935
1936
Dmitry Shmidt04949592012-07-19 12:16:46 -07001937#ifdef CONFIG_WPS_NFC
1938struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1939 int ndef, const char *uuid)
1940{
1941 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001942 u8 u[UUID_LEN], *use_uuid = NULL;
1943 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001944
1945 if (!wpa_s->wps_er)
1946 return NULL;
1947
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001948 if (uuid_str2bin(uuid, u) == 0)
1949 use_uuid = u;
1950 else if (hwaddr_aton(uuid, addr) == 0)
1951 use_addr = addr;
1952 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07001953 return NULL;
1954
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001955 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001956 if (ndef && ret) {
1957 struct wpabuf *tmp;
1958 tmp = ndef_build_wifi(ret);
1959 wpabuf_free(ret);
1960 if (tmp == NULL)
1961 return NULL;
1962 ret = tmp;
1963 }
1964
1965 return ret;
1966}
1967#endif /* CONFIG_WPS_NFC */
1968
1969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970static int callbacks_pending = 0;
1971
1972static void wpas_wps_terminate_cb(void *ctx)
1973{
1974 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1975 if (--callbacks_pending <= 0)
1976 eloop_terminate();
1977}
1978#endif /* CONFIG_WPS_ER */
1979
1980
1981int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1982{
1983#ifdef CONFIG_WPS_ER
1984 if (wpa_s->wps_er) {
1985 callbacks_pending++;
1986 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1987 wpa_s->wps_er = NULL;
1988 return 1;
1989 }
1990#endif /* CONFIG_WPS_ER */
1991 return 0;
1992}
1993
1994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1996{
1997 struct wps_context *wps = wpa_s->wps;
1998
1999 if (wps == NULL)
2000 return;
2001
2002 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2003 wps->config_methods = wps_config_methods_str2bin(
2004 wpa_s->conf->config_methods);
2005 if ((wps->config_methods &
2006 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2007 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2008 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2009 "config methods are not allowed at the "
2010 "same time");
2011 wps->config_methods &= ~WPS_CONFIG_LABEL;
2012 }
2013 }
2014 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002015 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016
2017 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2018 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2019 WPS_DEV_TYPE_LEN);
2020
2021 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2022 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2023 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2024 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2025 }
2026
Dmitry Shmidt04949592012-07-19 12:16:46 -07002027 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2028 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2031 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2032
2033 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2034 wpas_wps_set_uuid(wpa_s, wps);
2035
2036 if (wpa_s->conf->changed_parameters &
2037 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2038 /* Update pointers to make sure they refer current values */
2039 wps->dev.device_name = wpa_s->conf->device_name;
2040 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2041 wps->dev.model_name = wpa_s->conf->model_name;
2042 wps->dev.model_number = wpa_s->conf->model_number;
2043 wps->dev.serial_number = wpa_s->conf->serial_number;
2044 }
2045}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002046
2047
2048#ifdef CONFIG_WPS_NFC
2049
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002050#ifdef CONFIG_WPS_ER
2051static struct wpabuf *
2052wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2053 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002054{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002055 struct wpabuf *ret;
2056 struct wps_credential cred;
2057
2058 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2059 return NULL;
2060
2061 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2062
2063 if (ndef && ret) {
2064 struct wpabuf *tmp;
2065 tmp = ndef_build_wifi(ret);
2066 wpabuf_free(ret);
2067 if (tmp == NULL)
2068 return NULL;
2069 ret = tmp;
2070 }
2071
2072 return ret;
2073}
2074#endif /* CONFIG_WPS_ER */
2075
2076
2077struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2078 int ndef, const char *id_str)
2079{
2080#ifdef CONFIG_WPS_ER
2081 if (id_str) {
2082 int id;
2083 char *end = NULL;
2084 struct wpa_ssid *ssid;
2085
2086 id = strtol(id_str, &end, 10);
2087 if (end && *end)
2088 return NULL;
2089
2090 ssid = wpa_config_get_network(wpa_s->conf, id);
2091 if (ssid == NULL)
2092 return NULL;
2093 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2094 }
2095#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002096#ifdef CONFIG_AP
2097 if (wpa_s->ap_iface)
2098 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2099#endif /* CONFIG_AP */
2100 return NULL;
2101}
2102
2103
Dmitry Shmidt04949592012-07-19 12:16:46 -07002104struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2105{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002106 if (wpa_s->conf->wps_nfc_pw_from_config) {
2107 return wps_nfc_token_build(ndef,
2108 wpa_s->conf->wps_nfc_dev_pw_id,
2109 wpa_s->conf->wps_nfc_dh_pubkey,
2110 wpa_s->conf->wps_nfc_dev_pw);
2111 }
2112
Dmitry Shmidt04949592012-07-19 12:16:46 -07002113 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2114 &wpa_s->conf->wps_nfc_dh_pubkey,
2115 &wpa_s->conf->wps_nfc_dh_privkey,
2116 &wpa_s->conf->wps_nfc_dev_pw);
2117}
2118
2119
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002120int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2121 const u8 *bssid,
2122 const struct wpabuf *dev_pw, u16 dev_pw_id,
2123 int p2p_group, const u8 *peer_pubkey_hash,
2124 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002125{
2126 struct wps_context *wps = wpa_s->wps;
2127 char pw[32 * 2 + 1];
2128
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002129 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2130 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2131 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2132 }
2133
Dmitry Shmidt04949592012-07-19 12:16:46 -07002134 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002135 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2136 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2137 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002138 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002139 }
2140
2141 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2142 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2143 "cannot start NFC-triggered connection", dev_pw_id);
2144 return -1;
2145 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002146
2147 dh5_free(wps->dh_ctx);
2148 wpabuf_free(wps->dh_pubkey);
2149 wpabuf_free(wps->dh_privkey);
2150 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2151 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2152 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2153 wps->dh_ctx = NULL;
2154 wpabuf_free(wps->dh_pubkey);
2155 wps->dh_pubkey = NULL;
2156 wpabuf_free(wps->dh_privkey);
2157 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002158 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002159 return -1;
2160 }
2161 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002162 if (wps->dh_ctx == NULL) {
2163 wpabuf_free(wps->dh_pubkey);
2164 wps->dh_pubkey = NULL;
2165 wpabuf_free(wps->dh_privkey);
2166 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002167 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002168 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002169 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002170
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002171 if (dev_pw) {
2172 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2173 wpabuf_head(dev_pw),
2174 wpabuf_len(dev_pw));
2175 }
2176 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2177 dev_pw ? pw : NULL,
2178 p2p_group, dev_pw_id, peer_pubkey_hash,
2179 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002180}
2181
2182
2183static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2184 struct wps_parse_attr *attr)
2185{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002186 /*
2187 * Disable existing networks temporarily to allow the newly learned
2188 * credential to be preferred. Enable the temporarily disabled networks
2189 * after 10 seconds.
2190 */
2191 wpas_wps_temp_disable(wpa_s, NULL);
2192 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2193 NULL);
2194
Dmitry Shmidt04949592012-07-19 12:16:46 -07002195 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2196 return -1;
2197
2198 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2199 return 0;
2200
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002201 if (attr->ap_channel) {
2202 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002203 int freq = 0;
2204
2205 if (chan >= 1 && chan <= 13)
2206 freq = 2407 + 5 * chan;
2207 else if (chan == 14)
2208 freq = 2484;
2209 else if (chan >= 30)
2210 freq = 5000 + 5 * chan;
2211
2212 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002213 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2214 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002215 wpa_s->after_wps = 5;
2216 wpa_s->wps_freq = freq;
2217 }
2218 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002219
2220 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2221 "based on the received credential added");
2222 wpa_s->normal_scans = 0;
2223 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002224 wpa_s->disconnected = 0;
2225 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002226
2227 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002228 wpa_supplicant_req_scan(wpa_s, 0, 0);
2229
2230 return 0;
2231}
2232
2233
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002234#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002235static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2236 struct wps_parse_attr *attr)
2237{
2238 return wps_registrar_add_nfc_password_token(
2239 wpa_s->wps->registrar, attr->oob_dev_password,
2240 attr->oob_dev_password_len);
2241}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002242#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002243
2244
2245static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2246 const struct wpabuf *wps)
2247{
2248 struct wps_parse_attr attr;
2249
2250 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2251
2252 if (wps_parse_msg(wps, &attr)) {
2253 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2254 return -1;
2255 }
2256
2257 if (attr.num_cred)
2258 return wpas_wps_use_cred(wpa_s, &attr);
2259
2260#ifdef CONFIG_WPS_ER
2261 if (attr.oob_dev_password)
2262 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2263#endif /* CONFIG_WPS_ER */
2264
2265 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2266 return -1;
2267}
2268
2269
2270int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002271 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002272{
2273 const struct wpabuf *wps = data;
2274 struct wpabuf *tmp = NULL;
2275 int ret;
2276
2277 if (wpabuf_len(data) < 4)
2278 return -1;
2279
2280 if (*wpabuf_head_u8(data) != 0x10) {
2281 /* Assume this contains full NDEF record */
2282 tmp = ndef_parse_wifi(data);
2283 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002284#ifdef CONFIG_P2P
2285 tmp = ndef_parse_p2p(data);
2286 if (tmp) {
2287 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2288 forced_freq);
2289 wpabuf_free(tmp);
2290 return ret;
2291 }
2292#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002293 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2294 return -1;
2295 }
2296 wps = tmp;
2297 }
2298
2299 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2300 wpabuf_free(tmp);
2301 return ret;
2302}
2303
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002304
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002305struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2306 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002307{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002308 struct wpabuf *ret;
2309
2310 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2311 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2312 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2313 return NULL;
2314
2315 ret = wps_build_nfc_handover_req(wpa_s->wps,
2316 wpa_s->conf->wps_nfc_dh_pubkey);
2317
2318 if (ndef && ret) {
2319 struct wpabuf *tmp;
2320 tmp = ndef_build_wifi(ret);
2321 wpabuf_free(ret);
2322 if (tmp == NULL)
2323 return NULL;
2324 ret = tmp;
2325 }
2326
2327 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002328}
2329
2330
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002331#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002332
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002333static struct wpabuf *
2334wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2335 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002336{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002337#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002338 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002339 u8 u[UUID_LEN], *use_uuid = NULL;
2340 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002341 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002342
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002343 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002344 return NULL;
2345
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002346 if (uuid == NULL)
2347 return NULL;
2348 if (uuid_str2bin(uuid, u) == 0)
2349 use_uuid = u;
2350 else if (hwaddr_aton(uuid, addr) == 0)
2351 use_addr = addr;
2352 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002353 return NULL;
2354
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002355 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2356 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2357 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2358 return NULL;
2359 }
2360
2361 wpas_wps_nfc_clear(wps);
2362 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2363 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2364 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2365 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2366 wpas_wps_nfc_clear(wps);
2367 return NULL;
2368 }
2369
2370 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2371 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002372 if (ndef && ret) {
2373 struct wpabuf *tmp;
2374 tmp = ndef_build_wifi(ret);
2375 wpabuf_free(ret);
2376 if (tmp == NULL)
2377 return NULL;
2378 ret = tmp;
2379 }
2380
2381 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002382#else /* CONFIG_WPS_ER */
2383 return NULL;
2384#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002385}
2386#endif /* CONFIG_WPS_NFC */
2387
2388
2389struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2390 int ndef, int cr, const char *uuid)
2391{
2392 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002393 if (!cr)
2394 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002395 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2396 if (ret)
2397 return ret;
2398 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002399}
2400
2401
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002402int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2403 const struct wpabuf *data)
2404{
2405 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002406 int ret = -1;
2407 u16 wsc_len;
2408 const u8 *pos;
2409 struct wpabuf msg;
2410 struct wps_parse_attr attr;
2411 u16 dev_pw_id;
2412 const u8 *bssid = NULL;
2413 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002414
2415 wps = ndef_parse_wifi(data);
2416 if (wps == NULL)
2417 return -1;
2418 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2419 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002420 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2421 if (wpabuf_len(wps) < 2) {
2422 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2423 "Message");
2424 goto out;
2425 }
2426 pos = wpabuf_head(wps);
2427 wsc_len = WPA_GET_BE16(pos);
2428 if (wsc_len > wpabuf_len(wps) - 2) {
2429 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2430 "in Wi-Fi Handover Select Message", wsc_len);
2431 goto out;
2432 }
2433 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002434
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002435 wpa_hexdump(MSG_DEBUG,
2436 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2437 pos, wsc_len);
2438 if (wsc_len < wpabuf_len(wps) - 2) {
2439 wpa_hexdump(MSG_DEBUG,
2440 "WPS: Ignore extra data after WSC attributes",
2441 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2442 }
2443
2444 wpabuf_set(&msg, pos, wsc_len);
2445 ret = wps_parse_msg(&msg, &attr);
2446 if (ret < 0) {
2447 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2448 "Wi-Fi Handover Select Message");
2449 goto out;
2450 }
2451
2452 if (attr.oob_dev_password == NULL ||
2453 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2454 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2455 "included in Wi-Fi Handover Select Message");
2456 ret = -1;
2457 goto out;
2458 }
2459
2460 if (attr.ssid == NULL) {
2461 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2462 "Select Message");
2463 ret = -1;
2464 goto out;
2465 }
2466
2467 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2468
2469 if (attr.mac_addr) {
2470 bssid = attr.mac_addr;
2471 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2472 MAC2STR(bssid));
2473 }
2474
2475 if (attr.rf_bands)
2476 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2477
2478 if (attr.ap_channel) {
2479 u16 chan = WPA_GET_BE16(attr.ap_channel);
2480
2481 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2482
2483 if (chan >= 1 && chan <= 13 &&
2484 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2485 freq = 2407 + 5 * chan;
2486 else if (chan == 14 &&
2487 (attr.rf_bands == NULL ||
2488 *attr.rf_bands & WPS_RF_24GHZ))
2489 freq = 2484;
2490 else if (chan >= 30 &&
2491 (attr.rf_bands == NULL ||
2492 *attr.rf_bands & WPS_RF_50GHZ))
2493 freq = 5000 + 5 * chan;
2494
2495 if (freq) {
2496 wpa_printf(MSG_DEBUG,
2497 "WPS: AP indicated channel %u -> %u MHz",
2498 chan, freq);
2499 }
2500 }
2501
2502 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2503 attr.oob_dev_password, attr.oob_dev_password_len);
2504 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2505 WPS_OOB_PUBKEY_HASH_LEN);
2506 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2507 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2508 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2509 ret = -1;
2510 goto out;
2511 }
2512 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2513 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2514
2515 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2516 attr.oob_dev_password,
2517 attr.ssid, attr.ssid_len, freq);
2518
2519out:
2520 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002521 return ret;
2522}
2523
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002524
2525int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2526 const struct wpabuf *req,
2527 const struct wpabuf *sel)
2528{
2529 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2530 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2531 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2532 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2533}
2534
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002535
2536int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2537 const struct wpabuf *req,
2538 const struct wpabuf *sel)
2539{
2540 struct wpabuf *wps;
2541 int ret = -1;
2542 u16 wsc_len;
2543 const u8 *pos;
2544 struct wpabuf msg;
2545 struct wps_parse_attr attr;
2546 u16 dev_pw_id;
2547
2548 /*
2549 * Enrollee/station is always initiator of the NFC connection handover,
2550 * so use the request message here to find Enrollee public key hash.
2551 */
2552 wps = ndef_parse_wifi(req);
2553 if (wps == NULL)
2554 return -1;
2555 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2556 "payload from NFC connection handover");
2557 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2558 if (wpabuf_len(wps) < 2) {
2559 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2560 "Message");
2561 goto out;
2562 }
2563 pos = wpabuf_head(wps);
2564 wsc_len = WPA_GET_BE16(pos);
2565 if (wsc_len > wpabuf_len(wps) - 2) {
2566 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2567 "in rt Wi-Fi Handover Request Message", wsc_len);
2568 goto out;
2569 }
2570 pos += 2;
2571
2572 wpa_hexdump(MSG_DEBUG,
2573 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2574 pos, wsc_len);
2575 if (wsc_len < wpabuf_len(wps) - 2) {
2576 wpa_hexdump(MSG_DEBUG,
2577 "WPS: Ignore extra data after WSC attributes",
2578 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2579 }
2580
2581 wpabuf_set(&msg, pos, wsc_len);
2582 ret = wps_parse_msg(&msg, &attr);
2583 if (ret < 0) {
2584 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2585 "Wi-Fi Handover Request Message");
2586 goto out;
2587 }
2588
2589 if (attr.oob_dev_password == NULL ||
2590 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2591 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2592 "included in Wi-Fi Handover Request Message");
2593 ret = -1;
2594 goto out;
2595 }
2596
2597 if (attr.uuid_e == NULL) {
2598 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2599 "Handover Request Message");
2600 ret = -1;
2601 goto out;
2602 }
2603
2604 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2605
2606 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2607 attr.oob_dev_password, attr.oob_dev_password_len);
2608 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2609 WPS_OOB_PUBKEY_HASH_LEN);
2610 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2611 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2612 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2613 ret = -1;
2614 goto out;
2615 }
2616 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2617 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2618
2619 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2620 attr.oob_dev_password,
2621 DEV_PW_NFC_CONNECTION_HANDOVER,
2622 NULL, 0, 1);
2623
2624out:
2625 wpabuf_free(wps);
2626 return ret;
2627}
2628
Dmitry Shmidt04949592012-07-19 12:16:46 -07002629#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002630
2631
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002632static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2633{
2634 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002635 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002636
2637 if (wpa_debug_level > MSG_DEBUG)
2638 return;
2639
2640 if (wpa_s->wps_ap == NULL)
2641 return;
2642
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002643 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002644
2645 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2646 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2647 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2648
2649 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2650 "tries=%d last_attempt=%d sec ago blacklist=%d",
2651 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2652 ap->last_attempt.sec > 0 ?
2653 (int) now.sec - (int) ap->last_attempt.sec : -1,
2654 e ? e->count : 0);
2655 }
2656}
2657
2658
2659static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2660 const u8 *bssid)
2661{
2662 size_t i;
2663
2664 if (wpa_s->wps_ap == NULL)
2665 return NULL;
2666
2667 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2668 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2669 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2670 return ap;
2671 }
2672
2673 return NULL;
2674}
2675
2676
2677static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2678 struct wpa_scan_res *res)
2679{
2680 struct wpabuf *wps;
2681 enum wps_ap_info_type type;
2682 struct wps_ap_info *ap;
2683 int r;
2684
2685 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2686 return;
2687
2688 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2689 if (wps == NULL)
2690 return;
2691
2692 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2693 if (r == 2)
2694 type = WPS_AP_SEL_REG_OUR;
2695 else if (r == 1)
2696 type = WPS_AP_SEL_REG;
2697 else
2698 type = WPS_AP_NOT_SEL_REG;
2699
2700 wpabuf_free(wps);
2701
2702 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2703 if (ap) {
2704 if (ap->type != type) {
2705 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2706 " changed type %d -> %d",
2707 MAC2STR(res->bssid), ap->type, type);
2708 ap->type = type;
2709 if (type != WPS_AP_NOT_SEL_REG)
2710 wpa_blacklist_del(wpa_s, ap->bssid);
2711 }
2712 return;
2713 }
2714
2715 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2716 sizeof(struct wps_ap_info));
2717 if (ap == NULL)
2718 return;
2719
2720 wpa_s->wps_ap = ap;
2721 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2722 wpa_s->num_wps_ap++;
2723
2724 os_memset(ap, 0, sizeof(*ap));
2725 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2726 ap->type = type;
2727 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2728 MAC2STR(ap->bssid), ap->type);
2729}
2730
2731
2732void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2733 struct wpa_scan_results *scan_res)
2734{
2735 size_t i;
2736
2737 for (i = 0; i < scan_res->num; i++)
2738 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2739
2740 wpas_wps_dump_ap_info(wpa_s);
2741}
2742
2743
2744void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2745{
2746 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002747
2748 wpa_s->after_wps = 0;
2749
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002750 if (!wpa_s->wps_ap_iter)
2751 return;
2752 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2753 if (ap == NULL)
2754 return;
2755 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002756 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002757}