blob: 038c7fae9d5debb98d4f06b5a5a54c4fc3d51fda [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 Shmidtd5e49232012-12-03 15:08:10 -0800492 if (cred->ap_channel)
493 wpa_s->wps_ap_channel = cred->ap_channel;
494
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700495 wpas_wps_remove_dup_network(wpa_s, ssid);
496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497#ifndef CONFIG_NO_CONFIG_WRITE
498 if (wpa_s->conf->update_config &&
499 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
500 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
501 return -1;
502 }
503#endif /* CONFIG_NO_CONFIG_WRITE */
504
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800505 /*
506 * Optimize the post-WPS scan based on the channel used during
507 * the provisioning in case EAP-Failure is not received.
508 */
509 wpa_s->after_wps = 5;
510 wpa_s->wps_freq = wpa_s->assoc_freq;
511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512 return 0;
513}
514
515
516#ifdef CONFIG_P2P
517static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
518{
519 struct wpa_supplicant *wpa_s = eloop_ctx;
520 wpas_p2p_notif_pbc_overlap(wpa_s);
521}
522#endif /* CONFIG_P2P */
523
524
525static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
526 struct wps_event_m2d *m2d)
527{
528 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
529 "dev_password_id=%d config_error=%d",
530 m2d->dev_password_id, m2d->config_error);
531 wpas_notify_wps_event_m2d(wpa_s, m2d);
532#ifdef CONFIG_P2P
533 if (wpa_s->parent && wpa_s->parent != wpa_s) {
534 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
535 "dev_password_id=%d config_error=%d",
536 m2d->dev_password_id, m2d->config_error);
537 }
538 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
539 /*
540 * Notify P2P from eloop timeout to avoid issues with the
541 * interface getting removed while processing a message.
542 */
543 eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
544 NULL);
545 }
546#endif /* CONFIG_P2P */
547}
548
549
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700550static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
551{
552 struct wpa_supplicant *wpa_s = eloop_ctx;
553 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
554 wpas_clear_wps(wpa_s);
555}
556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557
558static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
559 struct wps_event_fail *fail)
560{
561 if (fail->error_indication > 0 &&
562 fail->error_indication < NUM_WPS_EI_VALUES) {
563 wpa_msg(wpa_s, MSG_INFO,
564 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
565 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700566 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 if (wpa_s->parent && wpa_s->parent != wpa_s)
568 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
569 "msg=%d config_error=%d reason=%d (%s)",
570 fail->msg, fail->config_error,
571 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700572 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573 } else {
574 wpa_msg(wpa_s, MSG_INFO,
575 WPS_EVENT_FAIL "msg=%d config_error=%d",
576 fail->msg, fail->config_error);
577 if (wpa_s->parent && wpa_s->parent != wpa_s)
578 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
579 "msg=%d config_error=%d",
580 fail->msg, fail->config_error);
581 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700582
583 /*
584 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
585 */
586 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
587 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
588 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700591#ifdef CONFIG_P2P
592 wpas_p2p_wps_failed(wpa_s, fail);
593#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594}
595
596
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800597static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
598
599static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
600{
601 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800602 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800603
604 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
605
606 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
607 if (ssid->disabled_for_connect && ssid->disabled) {
608 ssid->disabled_for_connect = 0;
609 ssid->disabled = 0;
610 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800611 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800612 }
613 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800614
615 if (changed) {
616#ifndef CONFIG_NO_CONFIG_WRITE
617 if (wpa_s->conf->update_config &&
618 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
619 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
620 "configuration");
621 }
622#endif /* CONFIG_NO_CONFIG_WRITE */
623 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800624}
625
626
627static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
628{
629 struct wpa_supplicant *wpa_s = eloop_ctx;
630 /* Enable the networks disabled during wpas_wps_reassoc */
631 wpas_wps_reenable_networks(wpa_s);
632}
633
634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
636{
637 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
638 wpa_s->wps_success = 1;
639 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700640 if (wpa_s->current_ssid)
641 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
642 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800643
644 /*
645 * Enable the networks disabled during wpas_wps_reassoc after 10
646 * seconds. The 10 seconds timer is to allow the data connection to be
647 * formed before allowing other networks to be selected.
648 */
649 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
650 NULL);
651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652#ifdef CONFIG_P2P
653 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
654#endif /* CONFIG_P2P */
655}
656
657
658static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
659 struct wps_event_er_ap *ap)
660{
661 char uuid_str[100];
662 char dev_type[WPS_DEV_TYPE_BUFSIZE];
663
664 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
665 if (ap->pri_dev_type)
666 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
667 sizeof(dev_type));
668 else
669 dev_type[0] = '\0';
670
671 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
672 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
673 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
674 ap->friendly_name ? ap->friendly_name : "",
675 ap->manufacturer ? ap->manufacturer : "",
676 ap->model_description ? ap->model_description : "",
677 ap->model_name ? ap->model_name : "",
678 ap->manufacturer_url ? ap->manufacturer_url : "",
679 ap->model_url ? ap->model_url : "");
680}
681
682
683static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
684 struct wps_event_er_ap *ap)
685{
686 char uuid_str[100];
687 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
688 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
689}
690
691
692static void wpa_supplicant_wps_event_er_enrollee_add(
693 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
694{
695 char uuid_str[100];
696 char dev_type[WPS_DEV_TYPE_BUFSIZE];
697
698 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
699 if (enrollee->pri_dev_type)
700 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
701 sizeof(dev_type));
702 else
703 dev_type[0] = '\0';
704
705 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
706 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
707 "|%s|%s|%s|%s|%s|",
708 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
709 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
710 enrollee->dev_name ? enrollee->dev_name : "",
711 enrollee->manufacturer ? enrollee->manufacturer : "",
712 enrollee->model_name ? enrollee->model_name : "",
713 enrollee->model_number ? enrollee->model_number : "",
714 enrollee->serial_number ? enrollee->serial_number : "");
715}
716
717
718static void wpa_supplicant_wps_event_er_enrollee_remove(
719 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
720{
721 char uuid_str[100];
722 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
723 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
724 uuid_str, MAC2STR(enrollee->mac_addr));
725}
726
727
728static void wpa_supplicant_wps_event_er_ap_settings(
729 struct wpa_supplicant *wpa_s,
730 struct wps_event_er_ap_settings *ap_settings)
731{
732 char uuid_str[100];
733 char key_str[65];
734 const struct wps_credential *cred = ap_settings->cred;
735
736 key_str[0] = '\0';
737 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
738 if (cred->key_len >= 8 && cred->key_len <= 64) {
739 os_memcpy(key_str, cred->key, cred->key_len);
740 key_str[cred->key_len] = '\0';
741 }
742 }
743
744 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
745 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
746 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
747 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
748 "key=%s",
749 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
750 cred->auth_type, cred->encr_type, key_str);
751}
752
753
754static void wpa_supplicant_wps_event_er_set_sel_reg(
755 struct wpa_supplicant *wpa_s,
756 struct wps_event_er_set_selected_registrar *ev)
757{
758 char uuid_str[100];
759
760 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
761 switch (ev->state) {
762 case WPS_ER_SET_SEL_REG_START:
763 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
764 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
765 "sel_reg_config_methods=0x%x",
766 uuid_str, ev->sel_reg, ev->dev_passwd_id,
767 ev->sel_reg_config_methods);
768 break;
769 case WPS_ER_SET_SEL_REG_DONE:
770 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
771 "uuid=%s state=DONE", uuid_str);
772 break;
773 case WPS_ER_SET_SEL_REG_FAILED:
774 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
775 "uuid=%s state=FAILED", uuid_str);
776 break;
777 }
778}
779
780
781static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
782 union wps_event_data *data)
783{
784 struct wpa_supplicant *wpa_s = ctx;
785 switch (event) {
786 case WPS_EV_M2D:
787 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
788 break;
789 case WPS_EV_FAIL:
790 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
791 break;
792 case WPS_EV_SUCCESS:
793 wpa_supplicant_wps_event_success(wpa_s);
794 break;
795 case WPS_EV_PWD_AUTH_FAIL:
796#ifdef CONFIG_AP
797 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
798 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
799#endif /* CONFIG_AP */
800 break;
801 case WPS_EV_PBC_OVERLAP:
802 break;
803 case WPS_EV_PBC_TIMEOUT:
804 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700805 case WPS_EV_PBC_ACTIVE:
806 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
807 break;
808 case WPS_EV_PBC_DISABLE:
809 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
810 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811 case WPS_EV_ER_AP_ADD:
812 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
813 break;
814 case WPS_EV_ER_AP_REMOVE:
815 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
816 break;
817 case WPS_EV_ER_ENROLLEE_ADD:
818 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
819 &data->enrollee);
820 break;
821 case WPS_EV_ER_ENROLLEE_REMOVE:
822 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
823 &data->enrollee);
824 break;
825 case WPS_EV_ER_AP_SETTINGS:
826 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
827 &data->ap_settings);
828 break;
829 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
830 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
831 &data->set_sel_reg);
832 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800833 case WPS_EV_AP_PIN_SUCCESS:
834 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 }
836}
837
838
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700839static int wpa_supplicant_wps_rf_band(void *ctx)
840{
841 struct wpa_supplicant *wpa_s = ctx;
842
843 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
844 return 0;
845
846 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
847}
848
849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
851{
852 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
853 eap_is_wps_pin_enrollee(&ssid->eap))
854 return WPS_REQ_ENROLLEE;
855 else
856 return WPS_REQ_REGISTRAR;
857}
858
859
860static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
861{
862 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700863 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
864
Dmitry Shmidt051af732013-10-22 13:52:46 -0700865 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700866 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700867
Jouni Malinen75ecf522011-06-27 15:19:46 -0700868 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800870 /* Enable the networks disabled during wpas_wps_reassoc */
871 wpas_wps_reenable_networks(wpa_s);
872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800874 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875
876 /* Remove any existing WPS network from configuration */
877 ssid = wpa_s->conf->ssid;
878 while (ssid) {
879 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
880 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700881 wpa_supplicant_deauthenticate(
882 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 }
884 id = ssid->id;
885 remove_ssid = ssid;
886 } else
887 id = -1;
888 ssid = ssid->next;
889 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700890 if (prev_current == remove_ssid) {
891 wpa_sm_set_config(wpa_s->wpa, NULL);
892 eapol_sm_notify_config(wpa_s->eapol, NULL,
893 NULL);
894 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 wpas_notify_network_removed(wpa_s, remove_ssid);
896 wpa_config_remove_network(wpa_s->conf, id);
897 }
898 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700899
900 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901}
902
903
904static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
905{
906 struct wpa_supplicant *wpa_s = eloop_ctx;
907 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
908 "out");
909 wpas_clear_wps(wpa_s);
910}
911
912
913static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
914 int registrar, const u8 *bssid)
915{
916 struct wpa_ssid *ssid;
917
918 ssid = wpa_config_add_network(wpa_s->conf);
919 if (ssid == NULL)
920 return NULL;
921 wpas_notify_network_added(wpa_s, ssid);
922 wpa_config_set_network_defaults(ssid);
923 ssid->temporary = 1;
924 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
925 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
926 wpa_config_set(ssid, "identity", registrar ?
927 "\"" WSC_ID_REGISTRAR "\"" :
928 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
929 wpas_notify_network_removed(wpa_s, ssid);
930 wpa_config_remove_network(wpa_s->conf, ssid->id);
931 return NULL;
932 }
933
934 if (bssid) {
935#ifndef CONFIG_P2P
936 struct wpa_bss *bss;
937 int count = 0;
938#endif /* CONFIG_P2P */
939
940 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
941 ssid->bssid_set = 1;
942
943 /*
944 * Note: With P2P, the SSID may change at the time the WPS
945 * provisioning is started, so better not filter the AP based
946 * on the current SSID in the scan results.
947 */
948#ifndef CONFIG_P2P
949 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
950 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
951 continue;
952
953 os_free(ssid->ssid);
954 ssid->ssid = os_malloc(bss->ssid_len);
955 if (ssid->ssid == NULL)
956 break;
957 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
958 ssid->ssid_len = bss->ssid_len;
959 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
960 "scan results",
961 ssid->ssid, ssid->ssid_len);
962 count++;
963 }
964
965 if (count > 1) {
966 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
967 "for the AP; use wildcard");
968 os_free(ssid->ssid);
969 ssid->ssid = NULL;
970 ssid->ssid_len = 0;
971 }
972#endif /* CONFIG_P2P */
973 }
974
975 return ssid;
976}
977
978
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700979static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
980 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981{
982 struct wpa_ssid *ssid;
983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800984 if (wpa_s->current_ssid)
985 wpa_supplicant_deauthenticate(
986 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 /* Mark all other networks disabled and trigger reassociation */
989 ssid = wpa_s->conf->ssid;
990 while (ssid) {
991 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800992 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700993 /*
994 * In case the network object corresponds to a persistent group
995 * then do not send out network disabled signal. In addition,
996 * do not change disabled status of persistent network objects
997 * from 2 to 1 should we connect to another network.
998 */
999 if (was_disabled != 2) {
1000 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001001 if (was_disabled != ssid->disabled) {
1002 if (ssid->disabled)
1003 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001004 wpas_notify_network_enabled_changed(wpa_s,
1005 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001006 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001007 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 ssid = ssid->next;
1009 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001010}
1011
1012
1013static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
1014 struct wpa_ssid *selected, const u8 *bssid)
1015{
1016 struct wpa_bss *bss;
1017
1018 wpa_s->after_wps = 0;
1019 wpa_s->known_wps_freq = 0;
1020 if (bssid) {
1021 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1022 if (bss && bss->freq > 0) {
1023 wpa_s->known_wps_freq = 1;
1024 wpa_s->wps_freq = bss->freq;
1025 }
1026 }
1027
1028 wpas_wps_temp_disable(wpa_s, selected);
1029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 wpa_s->disconnected = 0;
1031 wpa_s->reassociate = 1;
1032 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001033 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 wpa_s->wps_success = 0;
1035 wpa_s->blacklist_cleared = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001036
1037 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 wpa_supplicant_req_scan(wpa_s, 0, 0);
1039}
1040
1041
1042int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1043 int p2p_group)
1044{
1045 struct wpa_ssid *ssid;
1046 wpas_clear_wps(wpa_s);
1047 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
1048 if (ssid == NULL)
1049 return -1;
1050 ssid->temporary = 1;
1051 ssid->p2p_group = p2p_group;
1052#ifdef CONFIG_P2P
1053 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1054 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1055 if (ssid->ssid) {
1056 ssid->ssid_len = wpa_s->go_params->ssid_len;
1057 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1058 ssid->ssid_len);
1059 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1060 "SSID", ssid->ssid, ssid->ssid_len);
1061 }
1062 }
1063#endif /* CONFIG_P2P */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001064 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1065 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 if (wpa_s->wps_fragment_size)
1067 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1068 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1069 wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001070 wpas_wps_reassoc(wpa_s, ssid, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 return 0;
1072}
1073
1074
1075int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1076 const char *pin, int p2p_group, u16 dev_pw_id)
1077{
1078 struct wpa_ssid *ssid;
1079 char val[128];
1080 unsigned int rpin = 0;
1081
1082 wpas_clear_wps(wpa_s);
1083 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
1084 if (ssid == NULL)
1085 return -1;
1086 ssid->temporary = 1;
1087 ssid->p2p_group = p2p_group;
1088#ifdef CONFIG_P2P
1089 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1090 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1091 if (ssid->ssid) {
1092 ssid->ssid_len = wpa_s->go_params->ssid_len;
1093 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1094 ssid->ssid_len);
1095 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1096 "SSID", ssid->ssid, ssid->ssid_len);
1097 }
1098 }
1099#endif /* CONFIG_P2P */
1100 if (pin)
1101 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
1102 pin, dev_pw_id);
1103 else {
1104 rpin = wps_generate_pin();
1105 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
1106 rpin, dev_pw_id);
1107 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001108 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1109 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 if (wpa_s->wps_fragment_size)
1111 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1112 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1113 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001114 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001115 wpas_wps_reassoc(wpa_s, ssid, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 return rpin;
1117}
1118
1119
1120/* Cancel the wps pbc/pin requests */
1121int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1122{
1123#ifdef CONFIG_AP
1124 if (wpa_s->ap_iface) {
1125 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1126 return wpa_supplicant_ap_wps_cancel(wpa_s);
1127 }
1128#endif /* CONFIG_AP */
1129
Dmitry Shmidt04949592012-07-19 12:16:46 -07001130 if (wpa_s->wpa_state == WPA_SCANNING ||
1131 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1133 wpa_supplicant_cancel_scan(wpa_s);
1134 wpas_clear_wps(wpa_s);
1135 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1136 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1137 "deauthenticate");
1138 wpa_supplicant_deauthenticate(wpa_s,
1139 WLAN_REASON_DEAUTH_LEAVING);
1140 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001141 } else {
1142 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001143 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001144 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1145 0)
1146 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 }
1148
Dmitry Shmidt051af732013-10-22 13:52:46 -07001149 wpa_s->after_wps = 0;
1150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 return 0;
1152}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153
1154
1155int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1156 const char *pin, struct wps_new_ap_settings *settings)
1157{
1158 struct wpa_ssid *ssid;
1159 char val[200];
1160 char *pos, *end;
1161 int res;
1162
1163 if (!pin)
1164 return -1;
1165 wpas_clear_wps(wpa_s);
1166 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
1167 if (ssid == NULL)
1168 return -1;
1169 ssid->temporary = 1;
1170 pos = val;
1171 end = pos + sizeof(val);
1172 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1173 if (res < 0 || res >= end - pos)
1174 return -1;
1175 pos += res;
1176 if (settings) {
1177 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1178 "new_encr=%s new_key=%s",
1179 settings->ssid_hex, settings->auth,
1180 settings->encr, settings->key_hex);
1181 if (res < 0 || res >= end - pos)
1182 return -1;
1183 pos += res;
1184 }
1185 res = os_snprintf(pos, end - pos, "\"");
1186 if (res < 0 || res >= end - pos)
1187 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001188 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1189 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 if (wpa_s->wps_fragment_size)
1191 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1192 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1193 wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001194 wpas_wps_reassoc(wpa_s, ssid, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 return 0;
1196}
1197
1198
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001199static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1200 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 size_t psk_len)
1202{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001203 if (is_zero_ether_addr(p2p_dev_addr)) {
1204 wpa_printf(MSG_DEBUG,
1205 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1206 MAC2STR(mac_addr));
1207 } else {
1208 wpa_printf(MSG_DEBUG,
1209 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1210 " P2P Device Addr " MACSTR,
1211 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1212 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1214
1215 /* TODO */
1216
1217 return 0;
1218}
1219
1220
1221static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1222 const struct wps_device_data *dev)
1223{
1224 char uuid[40], txt[400];
1225 int len;
1226 char devtype[WPS_DEV_TYPE_BUFSIZE];
1227 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1228 return;
1229 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1230 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1231 " [%s|%s|%s|%s|%s|%s]",
1232 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1233 dev->manufacturer, dev->model_name,
1234 dev->model_number, dev->serial_number,
1235 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1236 sizeof(devtype)));
1237 if (len > 0 && len < (int) sizeof(txt))
1238 wpa_printf(MSG_INFO, "%s", txt);
1239}
1240
1241
1242static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1243 u16 sel_reg_config_methods)
1244{
1245#ifdef CONFIG_WPS_ER
1246 struct wpa_supplicant *wpa_s = ctx;
1247
1248 if (wpa_s->wps_er == NULL)
1249 return;
1250 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1251 "dev_password_id=%u sel_reg_config_methods=0x%x",
1252 sel_reg, dev_passwd_id, sel_reg_config_methods);
1253 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1254 sel_reg_config_methods);
1255#endif /* CONFIG_WPS_ER */
1256}
1257
1258
1259static u16 wps_fix_config_methods(u16 config_methods)
1260{
1261#ifdef CONFIG_WPS2
1262 if ((config_methods &
1263 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1264 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1265 wpa_printf(MSG_INFO, "WPS: Converting display to "
1266 "virtual_display for WPS 2.0 compliance");
1267 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1268 }
1269 if ((config_methods &
1270 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1271 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1272 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1273 "virtual_push_button for WPS 2.0 compliance");
1274 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1275 }
1276#endif /* CONFIG_WPS2 */
1277
1278 return config_methods;
1279}
1280
1281
1282static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1283 struct wps_context *wps)
1284{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001285 char buf[50];
1286 const char *src;
1287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 if (is_nil_uuid(wpa_s->conf->uuid)) {
1289 struct wpa_supplicant *first;
1290 first = wpa_s->global->ifaces;
1291 while (first && first->next)
1292 first = first->next;
1293 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001294 if (wps != wpa_s->global->ifaces->wps)
1295 os_memcpy(wps->uuid,
1296 wpa_s->global->ifaces->wps->uuid,
1297 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001298 src = "from the first interface";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 } else {
1300 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001301 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 }
1303 } else {
1304 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001305 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001307
1308 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1309 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310}
1311
1312
Dmitry Shmidt04949592012-07-19 12:16:46 -07001313static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1314 struct wps_context *wps)
1315{
1316 wpabuf_free(wps->dev.vendor_ext_m1);
1317 wps->dev.vendor_ext_m1 = NULL;
1318
1319 if (wpa_s->conf->wps_vendor_ext_m1) {
1320 wps->dev.vendor_ext_m1 =
1321 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1322 if (!wps->dev.vendor_ext_m1) {
1323 wpa_printf(MSG_ERROR, "WPS: Cannot "
1324 "allocate memory for vendor_ext_m1");
1325 }
1326 }
1327}
1328
1329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330int wpas_wps_init(struct wpa_supplicant *wpa_s)
1331{
1332 struct wps_context *wps;
1333 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001334 struct hostapd_hw_modes *modes;
1335 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336
1337 wps = os_zalloc(sizeof(*wps));
1338 if (wps == NULL)
1339 return -1;
1340
1341 wps->cred_cb = wpa_supplicant_wps_cred;
1342 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001343 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 wps->cb_ctx = wpa_s;
1345
1346 wps->dev.device_name = wpa_s->conf->device_name;
1347 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1348 wps->dev.model_name = wpa_s->conf->model_name;
1349 wps->dev.model_number = wpa_s->conf->model_number;
1350 wps->dev.serial_number = wpa_s->conf->serial_number;
1351 wps->config_methods =
1352 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1353 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1354 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1355 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1356 "methods are not allowed at the same time");
1357 os_free(wps);
1358 return -1;
1359 }
1360 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001361 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1363 WPS_DEV_TYPE_LEN);
1364
1365 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1366 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1367 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1368
Dmitry Shmidt04949592012-07-19 12:16:46 -07001369 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001372 modes = wpa_s->hw.modes;
1373 if (modes) {
1374 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1375 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1376 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1377 wps->dev.rf_bands |= WPS_RF_24GHZ;
1378 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1379 wps->dev.rf_bands |= WPS_RF_50GHZ;
1380 }
1381 }
1382 if (wps->dev.rf_bands == 0) {
1383 /*
1384 * Default to claiming support for both bands if the driver
1385 * does not provide support for fetching supported bands.
1386 */
1387 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1388 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1390 wpas_wps_set_uuid(wpa_s, wps);
1391
1392 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1393 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1394
1395 os_memset(&rcfg, 0, sizeof(rcfg));
1396 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1397 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1398 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1399 rcfg.cb_ctx = wpa_s;
1400
1401 wps->registrar = wps_registrar_init(wps, &rcfg);
1402 if (wps->registrar == NULL) {
1403 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1404 os_free(wps);
1405 return -1;
1406 }
1407
1408 wpa_s->wps = wps;
1409
1410 return 0;
1411}
1412
1413
1414void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1415{
1416 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001417 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001418 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001419 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420
1421 if (wpa_s->wps == NULL)
1422 return;
1423
1424#ifdef CONFIG_WPS_ER
1425 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1426 wpa_s->wps_er = NULL;
1427#endif /* CONFIG_WPS_ER */
1428
1429 wps_registrar_deinit(wpa_s->wps->registrar);
1430 wpabuf_free(wpa_s->wps->dh_pubkey);
1431 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001432 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 os_free(wpa_s->wps->network_key);
1434 os_free(wpa_s->wps);
1435 wpa_s->wps = NULL;
1436}
1437
1438
1439int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001440 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441{
1442 struct wpabuf *wps_ie;
1443
1444 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1445 return -1;
1446
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001447 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1449 if (!wps_ie) {
1450 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1451 return 0;
1452 }
1453
1454 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1455 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1456 "without active PBC Registrar");
1457 wpabuf_free(wps_ie);
1458 return 0;
1459 }
1460
1461 /* TODO: overlap detection */
1462 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1463 "(Active PBC)");
1464 wpabuf_free(wps_ie);
1465 return 1;
1466 }
1467
1468 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1469 if (!wps_ie) {
1470 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1471 return 0;
1472 }
1473
1474 /*
1475 * Start with WPS APs that advertise our address as an
1476 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1477 * allow any WPS AP after couple of scans since some APs do not
1478 * set Selected Registrar attribute properly when using
1479 * external Registrar.
1480 */
1481 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1482 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1483 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1484 "without active PIN Registrar");
1485 wpabuf_free(wps_ie);
1486 return 0;
1487 }
1488 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1489 } else {
1490 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1491 "(Authorized MAC or Active PIN)");
1492 }
1493 wpabuf_free(wps_ie);
1494 return 1;
1495 }
1496
1497 if (wps_ie) {
1498 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1499 wpabuf_free(wps_ie);
1500 return 1;
1501 }
1502
1503 return -1;
1504}
1505
1506
1507int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1508 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001509 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510{
1511 struct wpabuf *wps_ie = NULL;
1512 int ret = 0;
1513
1514 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001515 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1517 /* allow wildcard SSID for WPS PBC */
1518 ret = 1;
1519 }
1520 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001521 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522 if (wps_ie &&
1523 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1524 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1525 /* allow wildcard SSID for WPS PIN */
1526 ret = 1;
1527 }
1528 }
1529
1530 if (!ret && ssid->bssid_set &&
1531 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1532 /* allow wildcard SSID due to hardcoded BSSID match */
1533 ret = 1;
1534 }
1535
1536#ifdef CONFIG_WPS_STRICT
1537 if (wps_ie) {
1538 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1539 0, bss->bssid) < 0)
1540 ret = 0;
1541 if (bss->beacon_ie_len) {
1542 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001543 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 bss, WPS_IE_VENDOR_TYPE);
1545 if (bcn_wps == NULL) {
1546 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1547 "missing from AP Beacon");
1548 ret = 0;
1549 } else {
1550 if (wps_validate_beacon(wps_ie) < 0)
1551 ret = 0;
1552 wpabuf_free(bcn_wps);
1553 }
1554 }
1555 }
1556#endif /* CONFIG_WPS_STRICT */
1557
1558 wpabuf_free(wps_ie);
1559
1560 return ret;
1561}
1562
1563
1564int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1565 struct wpa_bss *selected, struct wpa_ssid *ssid)
1566{
1567 const u8 *sel_uuid, *uuid;
1568 struct wpabuf *wps_ie;
1569 int ret = 0;
1570 struct wpa_bss *bss;
1571
1572 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1573 return 0;
1574
1575 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1576 "present in scan results; selected BSSID " MACSTR,
1577 MAC2STR(selected->bssid));
1578
1579 /* Make sure that only one AP is in active PBC mode */
1580 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1581 if (wps_ie) {
1582 sel_uuid = wps_get_uuid_e(wps_ie);
1583 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1584 sel_uuid, UUID_LEN);
1585 } else {
1586 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1587 "WPS IE?!");
1588 sel_uuid = NULL;
1589 }
1590
1591 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1592 struct wpabuf *ie;
1593 if (bss == selected)
1594 continue;
1595 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1596 if (!ie)
1597 continue;
1598 if (!wps_is_selected_pbc_registrar(ie)) {
1599 wpabuf_free(ie);
1600 continue;
1601 }
1602 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1603 MACSTR, MAC2STR(bss->bssid));
1604 uuid = wps_get_uuid_e(ie);
1605 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1606 uuid, UUID_LEN);
1607 if (sel_uuid == NULL || uuid == NULL ||
1608 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1609 ret = 1; /* PBC overlap */
1610 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1611 MACSTR " and " MACSTR,
1612 MAC2STR(selected->bssid),
1613 MAC2STR(bss->bssid));
1614 wpabuf_free(ie);
1615 break;
1616 }
1617
1618 /* TODO: verify that this is reasonable dual-band situation */
1619
1620 wpabuf_free(ie);
1621 }
1622
1623 wpabuf_free(wps_ie);
1624
1625 return ret;
1626}
1627
1628
1629void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1630{
1631 struct wpa_bss *bss;
1632 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1633
1634 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1635 return;
1636
1637 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1638 struct wpabuf *ie;
1639 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1640 if (!ie)
1641 continue;
1642 if (wps_is_selected_pbc_registrar(ie))
1643 pbc++;
1644 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1645 auth++;
1646 else if (wps_is_selected_pin_registrar(ie))
1647 pin++;
1648 else
1649 wps++;
1650 wpabuf_free(ie);
1651 }
1652
1653 if (pbc)
1654 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1655 else if (auth)
1656 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1657 else if (pin)
1658 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1659 else if (wps)
1660 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1661}
1662
1663
1664int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1665{
1666 struct wpa_ssid *ssid;
1667
1668 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1669 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1670 return 1;
1671 }
1672
1673 return 0;
1674}
1675
1676
1677int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1678 char *end)
1679{
1680 struct wpabuf *wps_ie;
1681 int ret;
1682
1683 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1684 if (wps_ie == NULL)
1685 return 0;
1686
1687 ret = wps_attr_text(wps_ie, buf, end);
1688 wpabuf_free(wps_ie);
1689 return ret;
1690}
1691
1692
1693int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1694{
1695#ifdef CONFIG_WPS_ER
1696 if (wpa_s->wps_er) {
1697 wps_er_refresh(wpa_s->wps_er);
1698 return 0;
1699 }
1700 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1701 if (wpa_s->wps_er == NULL)
1702 return -1;
1703 return 0;
1704#else /* CONFIG_WPS_ER */
1705 return 0;
1706#endif /* CONFIG_WPS_ER */
1707}
1708
1709
1710int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1711{
1712#ifdef CONFIG_WPS_ER
1713 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1714 wpa_s->wps_er = NULL;
1715#endif /* CONFIG_WPS_ER */
1716 return 0;
1717}
1718
1719
1720#ifdef CONFIG_WPS_ER
1721int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1722 const char *uuid, const char *pin)
1723{
1724 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001725 const u8 *use_uuid = NULL;
1726 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001728 if (os_strcmp(uuid, "any") == 0) {
1729 } else if (uuid_str2bin(uuid, u) == 0) {
1730 use_uuid = u;
1731 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1732 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1733 if (use_uuid == NULL)
1734 return -1;
1735 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001736 return -1;
1737 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001738 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 (const u8 *) pin, os_strlen(pin), 300);
1740}
1741
1742
1743int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1744{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001745 u8 u[UUID_LEN], *use_uuid = NULL;
1746 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001748 if (uuid_str2bin(uuid, u) == 0)
1749 use_uuid = u;
1750 else if (hwaddr_aton(uuid, addr) == 0)
1751 use_addr = addr;
1752 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001754 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755}
1756
1757
1758int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1759 const char *pin)
1760{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001761 u8 u[UUID_LEN], *use_uuid = NULL;
1762 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001764 if (uuid_str2bin(uuid, u) == 0)
1765 use_uuid = u;
1766 else if (hwaddr_aton(uuid, addr) == 0)
1767 use_addr = addr;
1768 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001769 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001770
1771 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772 os_strlen(pin));
1773}
1774
1775
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001776static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1777 struct wps_credential *cred)
1778{
1779 os_memset(cred, 0, sizeof(*cred));
1780 if (ssid->ssid_len > 32)
1781 return -1;
1782 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1783 cred->ssid_len = ssid->ssid_len;
1784 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1785 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1786 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1787 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1788 cred->encr_type = WPS_ENCR_AES;
1789 else
1790 cred->encr_type = WPS_ENCR_TKIP;
1791 if (ssid->passphrase) {
1792 cred->key_len = os_strlen(ssid->passphrase);
1793 if (cred->key_len >= 64)
1794 return -1;
1795 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1796 } else if (ssid->psk_set) {
1797 cred->key_len = 32;
1798 os_memcpy(cred->key, ssid->psk, 32);
1799 } else
1800 return -1;
1801 } else {
1802 cred->auth_type = WPS_AUTH_OPEN;
1803 cred->encr_type = WPS_ENCR_NONE;
1804 }
1805
1806 return 0;
1807}
1808
1809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1811 int id)
1812{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001813 u8 u[UUID_LEN], *use_uuid = NULL;
1814 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815 struct wpa_ssid *ssid;
1816 struct wps_credential cred;
1817
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001818 if (uuid_str2bin(uuid, u) == 0)
1819 use_uuid = u;
1820 else if (hwaddr_aton(uuid, addr) == 0)
1821 use_addr = addr;
1822 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 return -1;
1824 ssid = wpa_config_get_network(wpa_s->conf, id);
1825 if (ssid == NULL || ssid->ssid == NULL)
1826 return -1;
1827
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001828 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001830 return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831}
1832
1833
1834int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1835 const char *pin, struct wps_new_ap_settings *settings)
1836{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001837 u8 u[UUID_LEN], *use_uuid = NULL;
1838 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 struct wps_credential cred;
1840 size_t len;
1841
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001842 if (uuid_str2bin(uuid, u) == 0)
1843 use_uuid = u;
1844 else if (hwaddr_aton(uuid, addr) == 0)
1845 use_addr = addr;
1846 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847 return -1;
1848 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1849 settings->encr == NULL || settings->key_hex == NULL)
1850 return -1;
1851
1852 os_memset(&cred, 0, sizeof(cred));
1853 len = os_strlen(settings->ssid_hex);
1854 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1855 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1856 return -1;
1857 cred.ssid_len = len / 2;
1858
1859 len = os_strlen(settings->key_hex);
1860 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1861 hexstr2bin(settings->key_hex, cred.key, len / 2))
1862 return -1;
1863 cred.key_len = len / 2;
1864
1865 if (os_strcmp(settings->auth, "OPEN") == 0)
1866 cred.auth_type = WPS_AUTH_OPEN;
1867 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1868 cred.auth_type = WPS_AUTH_WPAPSK;
1869 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1870 cred.auth_type = WPS_AUTH_WPA2PSK;
1871 else
1872 return -1;
1873
1874 if (os_strcmp(settings->encr, "NONE") == 0)
1875 cred.encr_type = WPS_ENCR_NONE;
1876 else if (os_strcmp(settings->encr, "WEP") == 0)
1877 cred.encr_type = WPS_ENCR_WEP;
1878 else if (os_strcmp(settings->encr, "TKIP") == 0)
1879 cred.encr_type = WPS_ENCR_TKIP;
1880 else if (os_strcmp(settings->encr, "CCMP") == 0)
1881 cred.encr_type = WPS_ENCR_AES;
1882 else
1883 return -1;
1884
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001885 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1886 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887}
1888
1889
Dmitry Shmidt04949592012-07-19 12:16:46 -07001890#ifdef CONFIG_WPS_NFC
1891struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1892 int ndef, const char *uuid)
1893{
1894 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001895 u8 u[UUID_LEN], *use_uuid = NULL;
1896 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001897
1898 if (!wpa_s->wps_er)
1899 return NULL;
1900
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001901 if (uuid_str2bin(uuid, u) == 0)
1902 use_uuid = u;
1903 else if (hwaddr_aton(uuid, addr) == 0)
1904 use_addr = addr;
1905 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07001906 return NULL;
1907
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001908 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001909 if (ndef && ret) {
1910 struct wpabuf *tmp;
1911 tmp = ndef_build_wifi(ret);
1912 wpabuf_free(ret);
1913 if (tmp == NULL)
1914 return NULL;
1915 ret = tmp;
1916 }
1917
1918 return ret;
1919}
1920#endif /* CONFIG_WPS_NFC */
1921
1922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923static int callbacks_pending = 0;
1924
1925static void wpas_wps_terminate_cb(void *ctx)
1926{
1927 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1928 if (--callbacks_pending <= 0)
1929 eloop_terminate();
1930}
1931#endif /* CONFIG_WPS_ER */
1932
1933
1934int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1935{
1936#ifdef CONFIG_WPS_ER
1937 if (wpa_s->wps_er) {
1938 callbacks_pending++;
1939 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1940 wpa_s->wps_er = NULL;
1941 return 1;
1942 }
1943#endif /* CONFIG_WPS_ER */
1944 return 0;
1945}
1946
1947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1949{
1950 struct wps_context *wps = wpa_s->wps;
1951
1952 if (wps == NULL)
1953 return;
1954
1955 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1956 wps->config_methods = wps_config_methods_str2bin(
1957 wpa_s->conf->config_methods);
1958 if ((wps->config_methods &
1959 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1960 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1961 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1962 "config methods are not allowed at the "
1963 "same time");
1964 wps->config_methods &= ~WPS_CONFIG_LABEL;
1965 }
1966 }
1967 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08001968 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969
1970 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
1971 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1972 WPS_DEV_TYPE_LEN);
1973
1974 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
1975 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1976 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1977 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
1978 }
1979
Dmitry Shmidt04949592012-07-19 12:16:46 -07001980 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
1981 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1984 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1985
1986 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
1987 wpas_wps_set_uuid(wpa_s, wps);
1988
1989 if (wpa_s->conf->changed_parameters &
1990 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1991 /* Update pointers to make sure they refer current values */
1992 wps->dev.device_name = wpa_s->conf->device_name;
1993 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1994 wps->dev.model_name = wpa_s->conf->model_name;
1995 wps->dev.model_number = wpa_s->conf->model_number;
1996 wps->dev.serial_number = wpa_s->conf->serial_number;
1997 }
1998}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001999
2000
2001#ifdef CONFIG_WPS_NFC
2002
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002003#ifdef CONFIG_WPS_ER
2004static struct wpabuf *
2005wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2006 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002007{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002008 struct wpabuf *ret;
2009 struct wps_credential cred;
2010
2011 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2012 return NULL;
2013
2014 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2015
2016 if (ndef && ret) {
2017 struct wpabuf *tmp;
2018 tmp = ndef_build_wifi(ret);
2019 wpabuf_free(ret);
2020 if (tmp == NULL)
2021 return NULL;
2022 ret = tmp;
2023 }
2024
2025 return ret;
2026}
2027#endif /* CONFIG_WPS_ER */
2028
2029
2030struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2031 int ndef, const char *id_str)
2032{
2033#ifdef CONFIG_WPS_ER
2034 if (id_str) {
2035 int id;
2036 char *end = NULL;
2037 struct wpa_ssid *ssid;
2038
2039 id = strtol(id_str, &end, 10);
2040 if (end && *end)
2041 return NULL;
2042
2043 ssid = wpa_config_get_network(wpa_s->conf, id);
2044 if (ssid == NULL)
2045 return NULL;
2046 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2047 }
2048#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002049#ifdef CONFIG_AP
2050 if (wpa_s->ap_iface)
2051 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2052#endif /* CONFIG_AP */
2053 return NULL;
2054}
2055
2056
Dmitry Shmidt04949592012-07-19 12:16:46 -07002057struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2058{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002059 if (wpa_s->conf->wps_nfc_pw_from_config) {
2060 return wps_nfc_token_build(ndef,
2061 wpa_s->conf->wps_nfc_dev_pw_id,
2062 wpa_s->conf->wps_nfc_dh_pubkey,
2063 wpa_s->conf->wps_nfc_dev_pw);
2064 }
2065
Dmitry Shmidt04949592012-07-19 12:16:46 -07002066 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2067 &wpa_s->conf->wps_nfc_dh_pubkey,
2068 &wpa_s->conf->wps_nfc_dh_privkey,
2069 &wpa_s->conf->wps_nfc_dev_pw);
2070}
2071
2072
2073int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2074{
2075 struct wps_context *wps = wpa_s->wps;
2076 char pw[32 * 2 + 1];
2077
2078 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
2079 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
2080 wpa_s->conf->wps_nfc_dev_pw == NULL)
2081 return -1;
2082
2083 dh5_free(wps->dh_ctx);
2084 wpabuf_free(wps->dh_pubkey);
2085 wpabuf_free(wps->dh_privkey);
2086 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2087 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2088 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2089 wps->dh_ctx = NULL;
2090 wpabuf_free(wps->dh_pubkey);
2091 wps->dh_pubkey = NULL;
2092 wpabuf_free(wps->dh_privkey);
2093 wps->dh_privkey = NULL;
2094 return -1;
2095 }
2096 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002097 if (wps->dh_ctx == NULL) {
2098 wpabuf_free(wps->dh_pubkey);
2099 wps->dh_pubkey = NULL;
2100 wpabuf_free(wps->dh_privkey);
2101 wps->dh_privkey = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002102 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002103 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002104
2105 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2106 wpabuf_head(wpa_s->conf->wps_nfc_dev_pw),
2107 wpabuf_len(wpa_s->conf->wps_nfc_dev_pw));
2108 return wpas_wps_start_pin(wpa_s, bssid, pw, 0,
2109 wpa_s->conf->wps_nfc_dev_pw_id);
2110}
2111
2112
2113static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2114 struct wps_parse_attr *attr)
2115{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002116 wpa_s->wps_ap_channel = 0;
2117
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002118 /*
2119 * Disable existing networks temporarily to allow the newly learned
2120 * credential to be preferred. Enable the temporarily disabled networks
2121 * after 10 seconds.
2122 */
2123 wpas_wps_temp_disable(wpa_s, NULL);
2124 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2125 NULL);
2126
Dmitry Shmidt04949592012-07-19 12:16:46 -07002127 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2128 return -1;
2129
2130 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2131 return 0;
2132
2133 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2134 "based on the received credential added");
2135 wpa_s->normal_scans = 0;
2136 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002137 if (wpa_s->wps_ap_channel) {
2138 u16 chan = wpa_s->wps_ap_channel;
2139 int freq = 0;
2140
2141 if (chan >= 1 && chan <= 13)
2142 freq = 2407 + 5 * chan;
2143 else if (chan == 14)
2144 freq = 2484;
2145 else if (chan >= 30)
2146 freq = 5000 + 5 * chan;
2147
2148 if (freq) {
2149 wpa_printf(MSG_DEBUG, "WPS: Credential indicated "
2150 "AP channel %u -> %u MHz", chan, freq);
2151 wpa_s->after_wps = 5;
2152 wpa_s->wps_freq = freq;
2153 }
2154 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002155 wpa_s->disconnected = 0;
2156 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002157
2158 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002159 wpa_supplicant_req_scan(wpa_s, 0, 0);
2160
2161 return 0;
2162}
2163
2164
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002165#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002166static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2167 struct wps_parse_attr *attr)
2168{
2169 return wps_registrar_add_nfc_password_token(
2170 wpa_s->wps->registrar, attr->oob_dev_password,
2171 attr->oob_dev_password_len);
2172}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002173#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002174
2175
2176static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2177 const struct wpabuf *wps)
2178{
2179 struct wps_parse_attr attr;
2180
2181 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2182
2183 if (wps_parse_msg(wps, &attr)) {
2184 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2185 return -1;
2186 }
2187
2188 if (attr.num_cred)
2189 return wpas_wps_use_cred(wpa_s, &attr);
2190
2191#ifdef CONFIG_WPS_ER
2192 if (attr.oob_dev_password)
2193 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2194#endif /* CONFIG_WPS_ER */
2195
2196 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2197 return -1;
2198}
2199
2200
2201int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
2202 const struct wpabuf *data)
2203{
2204 const struct wpabuf *wps = data;
2205 struct wpabuf *tmp = NULL;
2206 int ret;
2207
2208 if (wpabuf_len(data) < 4)
2209 return -1;
2210
2211 if (*wpabuf_head_u8(data) != 0x10) {
2212 /* Assume this contains full NDEF record */
2213 tmp = ndef_parse_wifi(data);
2214 if (tmp == NULL) {
2215 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2216 return -1;
2217 }
2218 wps = tmp;
2219 }
2220
2221 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2222 wpabuf_free(tmp);
2223 return ret;
2224}
2225
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002226
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002227struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s, int cr)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002228{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002229 if (cr)
2230 return ndef_build_wifi_hc(1);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002231 return ndef_build_wifi_hr();
2232}
2233
2234
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002235#ifdef CONFIG_WPS_NFC
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002236static struct wpabuf *
2237wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2238 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002239{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002240#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002241 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002242 u8 u[UUID_LEN], *use_uuid = NULL;
2243 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002244
2245 if (!wpa_s->wps_er)
2246 return NULL;
2247
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002248 if (uuid == NULL)
2249 return NULL;
2250 if (uuid_str2bin(uuid, u) == 0)
2251 use_uuid = u;
2252 else if (hwaddr_aton(uuid, addr) == 0)
2253 use_addr = addr;
2254 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002255 return NULL;
2256
2257 /*
2258 * Handover Select carrier record for WPS uses the same format as
2259 * configuration token.
2260 */
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002261 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002262 if (ndef && ret) {
2263 struct wpabuf *tmp;
2264 tmp = ndef_build_wifi(ret);
2265 wpabuf_free(ret);
2266 if (tmp == NULL)
2267 return NULL;
2268 ret = tmp;
2269 }
2270
2271 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002272#else /* CONFIG_WPS_ER */
2273 return NULL;
2274#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002275}
2276#endif /* CONFIG_WPS_NFC */
2277
2278
2279struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2280 int ndef, int cr, const char *uuid)
2281{
2282 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002283 if (!cr)
2284 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002285 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2286 if (ret)
2287 return ret;
2288 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002289}
2290
2291
2292int wpas_wps_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
2293 const struct wpabuf *data)
2294{
2295 /* TODO */
2296 return -1;
2297}
2298
2299
2300int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2301 const struct wpabuf *data)
2302{
2303 struct wpabuf *wps;
2304 int ret;
2305
2306 wps = ndef_parse_wifi(data);
2307 if (wps == NULL)
2308 return -1;
2309 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2310 "payload from NFC connection handover");
2311 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: NFC payload", wps);
2312 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2313 wpabuf_free(wps);
2314
2315 return ret;
2316}
2317
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002318
2319int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2320 const struct wpabuf *req,
2321 const struct wpabuf *sel)
2322{
2323 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2324 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2325 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2326 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2327}
2328
Dmitry Shmidt04949592012-07-19 12:16:46 -07002329#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002330
2331
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002332static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2333{
2334 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002335 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002336
2337 if (wpa_debug_level > MSG_DEBUG)
2338 return;
2339
2340 if (wpa_s->wps_ap == NULL)
2341 return;
2342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002343 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002344
2345 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2346 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2347 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2348
2349 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2350 "tries=%d last_attempt=%d sec ago blacklist=%d",
2351 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2352 ap->last_attempt.sec > 0 ?
2353 (int) now.sec - (int) ap->last_attempt.sec : -1,
2354 e ? e->count : 0);
2355 }
2356}
2357
2358
2359static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2360 const u8 *bssid)
2361{
2362 size_t i;
2363
2364 if (wpa_s->wps_ap == NULL)
2365 return NULL;
2366
2367 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2368 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2369 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2370 return ap;
2371 }
2372
2373 return NULL;
2374}
2375
2376
2377static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2378 struct wpa_scan_res *res)
2379{
2380 struct wpabuf *wps;
2381 enum wps_ap_info_type type;
2382 struct wps_ap_info *ap;
2383 int r;
2384
2385 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2386 return;
2387
2388 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2389 if (wps == NULL)
2390 return;
2391
2392 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2393 if (r == 2)
2394 type = WPS_AP_SEL_REG_OUR;
2395 else if (r == 1)
2396 type = WPS_AP_SEL_REG;
2397 else
2398 type = WPS_AP_NOT_SEL_REG;
2399
2400 wpabuf_free(wps);
2401
2402 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2403 if (ap) {
2404 if (ap->type != type) {
2405 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2406 " changed type %d -> %d",
2407 MAC2STR(res->bssid), ap->type, type);
2408 ap->type = type;
2409 if (type != WPS_AP_NOT_SEL_REG)
2410 wpa_blacklist_del(wpa_s, ap->bssid);
2411 }
2412 return;
2413 }
2414
2415 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2416 sizeof(struct wps_ap_info));
2417 if (ap == NULL)
2418 return;
2419
2420 wpa_s->wps_ap = ap;
2421 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2422 wpa_s->num_wps_ap++;
2423
2424 os_memset(ap, 0, sizeof(*ap));
2425 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2426 ap->type = type;
2427 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2428 MAC2STR(ap->bssid), ap->type);
2429}
2430
2431
2432void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2433 struct wpa_scan_results *scan_res)
2434{
2435 size_t i;
2436
2437 for (i = 0; i < scan_res->num; i++)
2438 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2439
2440 wpas_wps_dump_ap_info(wpa_s);
2441}
2442
2443
2444void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2445{
2446 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002447
2448 wpa_s->after_wps = 0;
2449
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002450 if (!wpa_s->wps_ap_iter)
2451 return;
2452 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2453 if (ap == NULL)
2454 return;
2455 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002456 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002457}