blob: 4c40dac4b1346e2d64dbf2efafa39583bf872c24 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant / WPS integration
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07003 * Copyright (c) 2008-2013, 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{
57 if (!wpa_s->wps_success &&
58 wpa_s->current_ssid &&
59 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
60 const u8 *bssid = wpa_s->bssid;
61 if (is_zero_ether_addr(bssid))
62 bssid = wpa_s->pending_bssid;
63
64 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
65 " did not succeed - continue trying to find "
66 "suitable AP", MAC2STR(bssid));
67 wpa_blacklist_add(wpa_s, bssid);
68
69 wpa_supplicant_deauthenticate(wpa_s,
70 WLAN_REASON_DEAUTH_LEAVING);
71 wpa_s->reassociate = 1;
72 wpa_supplicant_req_scan(wpa_s,
73 wpa_s->blacklist_cleared ? 5 : 0, 0);
74 wpa_s->blacklist_cleared = 0;
75 return 1;
76 }
77
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070078 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
80 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
81 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
82
83 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
84 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
85 int disabled = wpa_s->current_ssid->disabled;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080086 unsigned int freq = wpa_s->assoc_freq;
Dmitry Shmidt444d5672013-04-01 13:08:44 -070087 struct wpa_bss *bss;
88 struct wpa_ssid *ssid = NULL;
89 int use_fast_assoc = 0;
90
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080092 "try to associate with the received credential "
93 "(freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094 wpa_supplicant_deauthenticate(wpa_s,
95 WLAN_REASON_DEAUTH_LEAVING);
96 if (disabled) {
97 wpa_printf(MSG_DEBUG, "WPS: Current network is "
98 "disabled - wait for user to enable");
99 return 1;
100 }
101 wpa_s->after_wps = 5;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800102 wpa_s->wps_freq = freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800103 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 wpa_s->reassociate = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700105
106 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
107 "without a new scan can be used");
108 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
109 if (bss) {
110 struct wpabuf *wps;
111 struct wps_parse_attr attr;
112
113 wps = wpa_bss_get_vendor_ie_multi(bss,
114 WPS_IE_VENDOR_TYPE);
115 if (wps && wps_parse_msg(wps, &attr) == 0 &&
116 attr.wps_state &&
117 *attr.wps_state == WPS_STATE_CONFIGURED)
118 use_fast_assoc = 1;
119 wpabuf_free(wps);
120 }
121
122 if (!use_fast_assoc ||
123 wpa_supplicant_fast_associate(wpa_s) != 1)
124 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 return 1;
126 }
127
128 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
129 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
130 "for external credential processing");
131 wpas_clear_wps(wpa_s);
132 wpa_supplicant_deauthenticate(wpa_s,
133 WLAN_REASON_DEAUTH_LEAVING);
134 return 1;
135 }
136
137 return 0;
138}
139
140
141static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
142 struct wpa_ssid *ssid,
143 const struct wps_credential *cred)
144{
145 struct wpa_driver_capa capa;
146 struct wpa_bss *bss;
147 const u8 *ie;
148 struct wpa_ie_data adv;
149 int wpa2 = 0, ccmp = 0;
150
151 /*
152 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
153 * case they are configured for mixed mode operation (WPA+WPA2 and
154 * TKIP+CCMP). Try to use scan results to figure out whether the AP
155 * actually supports stronger security and select that if the client
156 * has support for it, too.
157 */
158
159 if (wpa_drv_get_capa(wpa_s, &capa))
160 return; /* Unknown what driver supports */
161
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800162 if (ssid->ssid == NULL)
163 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
165 if (bss == NULL) {
166 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
167 "table - use credential as-is");
168 return;
169 }
170
171 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
172
173 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
174 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
175 wpa2 = 1;
176 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
177 ccmp = 1;
178 } else {
179 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
180 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
181 adv.pairwise_cipher & WPA_CIPHER_CCMP)
182 ccmp = 1;
183 }
184
185 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
186 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
187 /*
188 * TODO: This could be the initial AP configuration and the
189 * Beacon contents could change shortly. Should request a new
190 * scan and delay addition of the network until the updated
191 * scan results are available.
192 */
193 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
194 "support - use credential as-is");
195 return;
196 }
197
198 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
199 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
200 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
201 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
202 "based on scan results");
203 if (wpa_s->conf->ap_scan == 1)
204 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
205 else
206 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
207 }
208
209 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
210 (ssid->proto & WPA_PROTO_WPA) &&
211 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
212 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
213 "based on scan results");
214 if (wpa_s->conf->ap_scan == 1)
215 ssid->proto |= WPA_PROTO_RSN;
216 else
217 ssid->proto = WPA_PROTO_RSN;
218 }
219}
220
221
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700222static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
223 struct wpa_ssid *new_ssid)
224{
225 struct wpa_ssid *ssid, *next;
226
227 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
228 ssid = next, next = ssid ? ssid->next : NULL) {
229 /*
230 * new_ssid has already been added to the list in
231 * wpas_wps_add_network(), so skip it.
232 */
233 if (ssid == new_ssid)
234 continue;
235
236 if (ssid->bssid_set || new_ssid->bssid_set) {
237 if (ssid->bssid_set != new_ssid->bssid_set)
238 continue;
239 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
240 0)
241 continue;
242 }
243
244 /* compare SSID */
245 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
246 continue;
247
248 if (ssid->ssid && new_ssid->ssid) {
249 if (os_memcmp(ssid->ssid, new_ssid->ssid,
250 ssid->ssid_len) != 0)
251 continue;
252 } else if (ssid->ssid || new_ssid->ssid)
253 continue;
254
255 /* compare security parameters */
256 if (ssid->auth_alg != new_ssid->auth_alg ||
257 ssid->key_mgmt != new_ssid->key_mgmt ||
258 ssid->proto != new_ssid->proto ||
259 ssid->pairwise_cipher != new_ssid->pairwise_cipher ||
260 ssid->group_cipher != new_ssid->group_cipher)
261 continue;
262
263 if (ssid->passphrase && new_ssid->passphrase) {
264 if (os_strlen(ssid->passphrase) !=
265 os_strlen(new_ssid->passphrase))
266 continue;
267 if (os_strcmp(ssid->passphrase, new_ssid->passphrase) !=
268 0)
269 continue;
270 } else if (ssid->passphrase || new_ssid->passphrase)
271 continue;
272
273 if ((ssid->psk_set || new_ssid->psk_set) &&
274 os_memcmp(ssid->psk, new_ssid->psk, sizeof(ssid->psk)) != 0)
275 continue;
276
277 if (ssid->auth_alg == WPA_ALG_WEP) {
278 if (ssid->wep_tx_keyidx != new_ssid->wep_tx_keyidx)
279 continue;
280 if (os_memcmp(ssid->wep_key, new_ssid->wep_key,
281 sizeof(ssid->wep_key)))
282 continue;
283 if (os_memcmp(ssid->wep_key_len, new_ssid->wep_key_len,
284 sizeof(ssid->wep_key_len)))
285 continue;
286 }
287
288 /* Remove the duplicated older network entry. */
289 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
290 wpas_notify_network_removed(wpa_s, ssid);
291 wpa_config_remove_network(wpa_s->conf, ssid->id);
292 }
293}
294
295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296static int wpa_supplicant_wps_cred(void *ctx,
297 const struct wps_credential *cred)
298{
299 struct wpa_supplicant *wpa_s = ctx;
300 struct wpa_ssid *ssid = wpa_s->current_ssid;
301 u8 key_idx = 0;
302 u16 auth_type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 int registrar = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800305#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306
307 if ((wpa_s->conf->wps_cred_processing == 1 ||
308 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
309 size_t blen = cred->cred_attr_len * 2 + 1;
310 char *buf = os_malloc(blen);
311 if (buf) {
312 wpa_snprintf_hex(buf, blen,
313 cred->cred_attr, cred->cred_attr_len);
314 wpa_msg(wpa_s, MSG_INFO, "%s%s",
315 WPS_EVENT_CRED_RECEIVED, buf);
316 os_free(buf);
317 }
318
319 wpas_notify_wps_credential(wpa_s, cred);
320 } else
321 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
322
323 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
324 cred->cred_attr, cred->cred_attr_len);
325
326 if (wpa_s->conf->wps_cred_processing == 1)
327 return 0;
328
329 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
330 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
331 cred->auth_type);
332 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
333 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
334 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
335 cred->key, cred->key_len);
336 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
337 MAC2STR(cred->mac_addr));
338
339 auth_type = cred->auth_type;
340 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
341 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
342 "auth_type into WPA2PSK");
343 auth_type = WPS_AUTH_WPA2PSK;
344 }
345
346 if (auth_type != WPS_AUTH_OPEN &&
347 auth_type != WPS_AUTH_SHARED &&
348 auth_type != WPS_AUTH_WPAPSK &&
349 auth_type != WPS_AUTH_WPA2PSK) {
350 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
351 "unsupported authentication type 0x%x",
352 auth_type);
353 return 0;
354 }
355
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800356 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
357 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
358 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
359 "invalid Network Key length %lu",
360 (unsigned long) cred->key_len);
361 return -1;
362 }
363 }
364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
366 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
367 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800368#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 if (ssid->eap.identity &&
370 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
371 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
372 WSC_ID_REGISTRAR_LEN) == 0)
373 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 os_free(ssid->eap.identity);
376 ssid->eap.identity = NULL;
377 ssid->eap.identity_len = 0;
378 os_free(ssid->eap.phase1);
379 ssid->eap.phase1 = NULL;
380 os_free(ssid->eap.eap_methods);
381 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700382 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700384 ssid->bssid_set = 0;
385 }
386 ssid->disabled_until.sec = 0;
387 ssid->disabled_until.usec = 0;
388 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 } else {
390 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
391 "received credential");
392 ssid = wpa_config_add_network(wpa_s->conf);
393 if (ssid == NULL)
394 return -1;
395 wpas_notify_network_added(wpa_s, ssid);
396 }
397
398 wpa_config_set_network_defaults(ssid);
399
400 os_free(ssid->ssid);
401 ssid->ssid = os_malloc(cred->ssid_len);
402 if (ssid->ssid) {
403 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
404 ssid->ssid_len = cred->ssid_len;
405 }
406
407 switch (cred->encr_type) {
408 case WPS_ENCR_NONE:
409 break;
410 case WPS_ENCR_WEP:
411 if (cred->key_len <= 0)
412 break;
413 if (cred->key_len != 5 && cred->key_len != 13 &&
414 cred->key_len != 10 && cred->key_len != 26) {
415 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
416 "%lu", (unsigned long) cred->key_len);
417 return -1;
418 }
419 if (cred->key_idx > NUM_WEP_KEYS) {
420 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
421 cred->key_idx);
422 return -1;
423 }
424 if (cred->key_idx)
425 key_idx = cred->key_idx - 1;
426 if (cred->key_len == 10 || cred->key_len == 26) {
427 if (hexstr2bin((char *) cred->key,
428 ssid->wep_key[key_idx],
429 cred->key_len / 2) < 0) {
430 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
431 "%d", key_idx);
432 return -1;
433 }
434 ssid->wep_key_len[key_idx] = cred->key_len / 2;
435 } else {
436 os_memcpy(ssid->wep_key[key_idx], cred->key,
437 cred->key_len);
438 ssid->wep_key_len[key_idx] = cred->key_len;
439 }
440 ssid->wep_tx_keyidx = key_idx;
441 break;
442 case WPS_ENCR_TKIP:
443 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
444 break;
445 case WPS_ENCR_AES:
446 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
447 break;
448 }
449
450 switch (auth_type) {
451 case WPS_AUTH_OPEN:
452 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
453 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
454 ssid->proto = 0;
455#ifdef CONFIG_WPS_REG_DISABLE_OPEN
456 if (registrar) {
457 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
458 "id=%d - Credentials for an open "
459 "network disabled by default - use "
460 "'select_network %d' to enable",
461 ssid->id, ssid->id);
462 ssid->disabled = 1;
463 }
464#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
465 break;
466 case WPS_AUTH_SHARED:
467 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
468 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
469 ssid->proto = 0;
470 break;
471 case WPS_AUTH_WPAPSK:
472 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
473 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
474 ssid->proto = WPA_PROTO_WPA;
475 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 case WPS_AUTH_WPA2PSK:
477 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
478 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
479 ssid->proto = WPA_PROTO_RSN;
480 break;
481 }
482
483 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
484 if (cred->key_len == 2 * PMK_LEN) {
485 if (hexstr2bin((const char *) cred->key, ssid->psk,
486 PMK_LEN)) {
487 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
488 "Key");
489 return -1;
490 }
491 ssid->psk_set = 1;
492 ssid->export_keys = 1;
493 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
494 os_free(ssid->passphrase);
495 ssid->passphrase = os_malloc(cred->key_len + 1);
496 if (ssid->passphrase == NULL)
497 return -1;
498 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
499 ssid->passphrase[cred->key_len] = '\0';
500 wpa_config_update_psk(ssid);
501 ssid->export_keys = 1;
502 } else {
503 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
504 "length %lu",
505 (unsigned long) cred->key_len);
506 return -1;
507 }
508 }
509
510 wpas_wps_security_workaround(wpa_s, ssid, cred);
511
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800512 if (cred->ap_channel)
513 wpa_s->wps_ap_channel = cred->ap_channel;
514
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700515 wpas_wps_remove_dup_network(wpa_s, ssid);
516
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517#ifndef CONFIG_NO_CONFIG_WRITE
518 if (wpa_s->conf->update_config &&
519 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
520 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
521 return -1;
522 }
523#endif /* CONFIG_NO_CONFIG_WRITE */
524
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800525 /*
526 * Optimize the post-WPS scan based on the channel used during
527 * the provisioning in case EAP-Failure is not received.
528 */
529 wpa_s->after_wps = 5;
530 wpa_s->wps_freq = wpa_s->assoc_freq;
531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 return 0;
533}
534
535
536#ifdef CONFIG_P2P
537static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
538{
539 struct wpa_supplicant *wpa_s = eloop_ctx;
540 wpas_p2p_notif_pbc_overlap(wpa_s);
541}
542#endif /* CONFIG_P2P */
543
544
545static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
546 struct wps_event_m2d *m2d)
547{
548 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
549 "dev_password_id=%d config_error=%d",
550 m2d->dev_password_id, m2d->config_error);
551 wpas_notify_wps_event_m2d(wpa_s, m2d);
552#ifdef CONFIG_P2P
553 if (wpa_s->parent && wpa_s->parent != wpa_s) {
554 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
555 "dev_password_id=%d config_error=%d",
556 m2d->dev_password_id, m2d->config_error);
557 }
558 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
559 /*
560 * Notify P2P from eloop timeout to avoid issues with the
561 * interface getting removed while processing a message.
562 */
563 eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
564 NULL);
565 }
566#endif /* CONFIG_P2P */
567}
568
569
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700570static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
571{
572 struct wpa_supplicant *wpa_s = eloop_ctx;
573 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
574 wpas_clear_wps(wpa_s);
575}
576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577
578static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
579 struct wps_event_fail *fail)
580{
581 if (fail->error_indication > 0 &&
582 fail->error_indication < NUM_WPS_EI_VALUES) {
583 wpa_msg(wpa_s, MSG_INFO,
584 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
585 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700586 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 if (wpa_s->parent && wpa_s->parent != wpa_s)
588 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
589 "msg=%d config_error=%d reason=%d (%s)",
590 fail->msg, fail->config_error,
591 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700592 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 } else {
594 wpa_msg(wpa_s, MSG_INFO,
595 WPS_EVENT_FAIL "msg=%d config_error=%d",
596 fail->msg, fail->config_error);
597 if (wpa_s->parent && wpa_s->parent != wpa_s)
598 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
599 "msg=%d config_error=%d",
600 fail->msg, fail->config_error);
601 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700602
603 /*
604 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
605 */
606 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
607 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
608 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700611#ifdef CONFIG_P2P
612 wpas_p2p_wps_failed(wpa_s, fail);
613#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614}
615
616
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800617static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
618
619static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
620{
621 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800622 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800623
624 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
625
626 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
627 if (ssid->disabled_for_connect && ssid->disabled) {
628 ssid->disabled_for_connect = 0;
629 ssid->disabled = 0;
630 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800631 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800632 }
633 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800634
635 if (changed) {
636#ifndef CONFIG_NO_CONFIG_WRITE
637 if (wpa_s->conf->update_config &&
638 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
639 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
640 "configuration");
641 }
642#endif /* CONFIG_NO_CONFIG_WRITE */
643 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800644}
645
646
647static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
648{
649 struct wpa_supplicant *wpa_s = eloop_ctx;
650 /* Enable the networks disabled during wpas_wps_reassoc */
651 wpas_wps_reenable_networks(wpa_s);
652}
653
654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
656{
657 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
658 wpa_s->wps_success = 1;
659 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700660 if (wpa_s->current_ssid)
661 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
662 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800663
664 /*
665 * Enable the networks disabled during wpas_wps_reassoc after 10
666 * seconds. The 10 seconds timer is to allow the data connection to be
667 * formed before allowing other networks to be selected.
668 */
669 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
670 NULL);
671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672#ifdef CONFIG_P2P
673 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
674#endif /* CONFIG_P2P */
675}
676
677
678static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
679 struct wps_event_er_ap *ap)
680{
681 char uuid_str[100];
682 char dev_type[WPS_DEV_TYPE_BUFSIZE];
683
684 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
685 if (ap->pri_dev_type)
686 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
687 sizeof(dev_type));
688 else
689 dev_type[0] = '\0';
690
691 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
692 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
693 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
694 ap->friendly_name ? ap->friendly_name : "",
695 ap->manufacturer ? ap->manufacturer : "",
696 ap->model_description ? ap->model_description : "",
697 ap->model_name ? ap->model_name : "",
698 ap->manufacturer_url ? ap->manufacturer_url : "",
699 ap->model_url ? ap->model_url : "");
700}
701
702
703static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
704 struct wps_event_er_ap *ap)
705{
706 char uuid_str[100];
707 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
708 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
709}
710
711
712static void wpa_supplicant_wps_event_er_enrollee_add(
713 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
714{
715 char uuid_str[100];
716 char dev_type[WPS_DEV_TYPE_BUFSIZE];
717
718 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
719 if (enrollee->pri_dev_type)
720 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
721 sizeof(dev_type));
722 else
723 dev_type[0] = '\0';
724
725 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
726 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
727 "|%s|%s|%s|%s|%s|",
728 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
729 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
730 enrollee->dev_name ? enrollee->dev_name : "",
731 enrollee->manufacturer ? enrollee->manufacturer : "",
732 enrollee->model_name ? enrollee->model_name : "",
733 enrollee->model_number ? enrollee->model_number : "",
734 enrollee->serial_number ? enrollee->serial_number : "");
735}
736
737
738static void wpa_supplicant_wps_event_er_enrollee_remove(
739 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
740{
741 char uuid_str[100];
742 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
743 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
744 uuid_str, MAC2STR(enrollee->mac_addr));
745}
746
747
748static void wpa_supplicant_wps_event_er_ap_settings(
749 struct wpa_supplicant *wpa_s,
750 struct wps_event_er_ap_settings *ap_settings)
751{
752 char uuid_str[100];
753 char key_str[65];
754 const struct wps_credential *cred = ap_settings->cred;
755
756 key_str[0] = '\0';
757 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
758 if (cred->key_len >= 8 && cred->key_len <= 64) {
759 os_memcpy(key_str, cred->key, cred->key_len);
760 key_str[cred->key_len] = '\0';
761 }
762 }
763
764 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
765 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
766 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
767 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
768 "key=%s",
769 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
770 cred->auth_type, cred->encr_type, key_str);
771}
772
773
774static void wpa_supplicant_wps_event_er_set_sel_reg(
775 struct wpa_supplicant *wpa_s,
776 struct wps_event_er_set_selected_registrar *ev)
777{
778 char uuid_str[100];
779
780 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
781 switch (ev->state) {
782 case WPS_ER_SET_SEL_REG_START:
783 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
784 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
785 "sel_reg_config_methods=0x%x",
786 uuid_str, ev->sel_reg, ev->dev_passwd_id,
787 ev->sel_reg_config_methods);
788 break;
789 case WPS_ER_SET_SEL_REG_DONE:
790 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
791 "uuid=%s state=DONE", uuid_str);
792 break;
793 case WPS_ER_SET_SEL_REG_FAILED:
794 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
795 "uuid=%s state=FAILED", uuid_str);
796 break;
797 }
798}
799
800
801static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
802 union wps_event_data *data)
803{
804 struct wpa_supplicant *wpa_s = ctx;
805 switch (event) {
806 case WPS_EV_M2D:
807 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
808 break;
809 case WPS_EV_FAIL:
810 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
811 break;
812 case WPS_EV_SUCCESS:
813 wpa_supplicant_wps_event_success(wpa_s);
814 break;
815 case WPS_EV_PWD_AUTH_FAIL:
816#ifdef CONFIG_AP
817 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
818 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
819#endif /* CONFIG_AP */
820 break;
821 case WPS_EV_PBC_OVERLAP:
822 break;
823 case WPS_EV_PBC_TIMEOUT:
824 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700825 case WPS_EV_PBC_ACTIVE:
826 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
827 break;
828 case WPS_EV_PBC_DISABLE:
829 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
830 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 case WPS_EV_ER_AP_ADD:
832 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
833 break;
834 case WPS_EV_ER_AP_REMOVE:
835 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
836 break;
837 case WPS_EV_ER_ENROLLEE_ADD:
838 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
839 &data->enrollee);
840 break;
841 case WPS_EV_ER_ENROLLEE_REMOVE:
842 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
843 &data->enrollee);
844 break;
845 case WPS_EV_ER_AP_SETTINGS:
846 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
847 &data->ap_settings);
848 break;
849 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
850 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
851 &data->set_sel_reg);
852 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800853 case WPS_EV_AP_PIN_SUCCESS:
854 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 }
856}
857
858
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700859static int wpa_supplicant_wps_rf_band(void *ctx)
860{
861 struct wpa_supplicant *wpa_s = ctx;
862
863 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
864 return 0;
865
866 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
867}
868
869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
871{
872 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
873 eap_is_wps_pin_enrollee(&ssid->eap))
874 return WPS_REQ_ENROLLEE;
875 else
876 return WPS_REQ_REGISTRAR;
877}
878
879
880static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
881{
882 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700883 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
884
Dmitry Shmidt051af732013-10-22 13:52:46 -0700885 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700886 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700887
Jouni Malinen75ecf522011-06-27 15:19:46 -0700888 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800890 /* Enable the networks disabled during wpas_wps_reassoc */
891 wpas_wps_reenable_networks(wpa_s);
892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
894
895 /* Remove any existing WPS network from configuration */
896 ssid = wpa_s->conf->ssid;
897 while (ssid) {
898 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
899 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700900 wpa_supplicant_deauthenticate(
901 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 }
903 id = ssid->id;
904 remove_ssid = ssid;
905 } else
906 id = -1;
907 ssid = ssid->next;
908 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700909 if (prev_current == remove_ssid) {
910 wpa_sm_set_config(wpa_s->wpa, NULL);
911 eapol_sm_notify_config(wpa_s->eapol, NULL,
912 NULL);
913 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 wpas_notify_network_removed(wpa_s, remove_ssid);
915 wpa_config_remove_network(wpa_s->conf, id);
916 }
917 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700918
919 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920}
921
922
923static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
924{
925 struct wpa_supplicant *wpa_s = eloop_ctx;
926 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
927 "out");
928 wpas_clear_wps(wpa_s);
929}
930
931
932static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
933 int registrar, const u8 *bssid)
934{
935 struct wpa_ssid *ssid;
936
937 ssid = wpa_config_add_network(wpa_s->conf);
938 if (ssid == NULL)
939 return NULL;
940 wpas_notify_network_added(wpa_s, ssid);
941 wpa_config_set_network_defaults(ssid);
942 ssid->temporary = 1;
943 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
944 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
945 wpa_config_set(ssid, "identity", registrar ?
946 "\"" WSC_ID_REGISTRAR "\"" :
947 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
948 wpas_notify_network_removed(wpa_s, ssid);
949 wpa_config_remove_network(wpa_s->conf, ssid->id);
950 return NULL;
951 }
952
953 if (bssid) {
954#ifndef CONFIG_P2P
955 struct wpa_bss *bss;
956 int count = 0;
957#endif /* CONFIG_P2P */
958
959 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
960 ssid->bssid_set = 1;
961
962 /*
963 * Note: With P2P, the SSID may change at the time the WPS
964 * provisioning is started, so better not filter the AP based
965 * on the current SSID in the scan results.
966 */
967#ifndef CONFIG_P2P
968 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
969 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
970 continue;
971
972 os_free(ssid->ssid);
973 ssid->ssid = os_malloc(bss->ssid_len);
974 if (ssid->ssid == NULL)
975 break;
976 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
977 ssid->ssid_len = bss->ssid_len;
978 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
979 "scan results",
980 ssid->ssid, ssid->ssid_len);
981 count++;
982 }
983
984 if (count > 1) {
985 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
986 "for the AP; use wildcard");
987 os_free(ssid->ssid);
988 ssid->ssid = NULL;
989 ssid->ssid_len = 0;
990 }
991#endif /* CONFIG_P2P */
992 }
993
994 return ssid;
995}
996
997
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700998static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
999 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000{
1001 struct wpa_ssid *ssid;
1002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001003 if (wpa_s->current_ssid)
1004 wpa_supplicant_deauthenticate(
1005 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 /* Mark all other networks disabled and trigger reassociation */
1008 ssid = wpa_s->conf->ssid;
1009 while (ssid) {
1010 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001011 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001012 /*
1013 * In case the network object corresponds to a persistent group
1014 * then do not send out network disabled signal. In addition,
1015 * do not change disabled status of persistent network objects
1016 * from 2 to 1 should we connect to another network.
1017 */
1018 if (was_disabled != 2) {
1019 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001020 if (was_disabled != ssid->disabled) {
1021 if (ssid->disabled)
1022 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001023 wpas_notify_network_enabled_changed(wpa_s,
1024 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001025 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001026 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027 ssid = ssid->next;
1028 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001029}
1030
1031
1032static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
1033 struct wpa_ssid *selected, const u8 *bssid)
1034{
1035 struct wpa_bss *bss;
1036
1037 wpa_s->after_wps = 0;
1038 wpa_s->known_wps_freq = 0;
1039 if (bssid) {
1040 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1041 if (bss && bss->freq > 0) {
1042 wpa_s->known_wps_freq = 1;
1043 wpa_s->wps_freq = bss->freq;
1044 }
1045 }
1046
1047 wpas_wps_temp_disable(wpa_s, selected);
1048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpa_s->disconnected = 0;
1050 wpa_s->reassociate = 1;
1051 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001052 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 wpa_s->wps_success = 0;
1054 wpa_s->blacklist_cleared = 0;
1055 wpa_supplicant_req_scan(wpa_s, 0, 0);
1056}
1057
1058
1059int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1060 int p2p_group)
1061{
1062 struct wpa_ssid *ssid;
1063 wpas_clear_wps(wpa_s);
1064 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
1065 if (ssid == NULL)
1066 return -1;
1067 ssid->temporary = 1;
1068 ssid->p2p_group = p2p_group;
1069#ifdef CONFIG_P2P
1070 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1071 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1072 if (ssid->ssid) {
1073 ssid->ssid_len = wpa_s->go_params->ssid_len;
1074 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1075 ssid->ssid_len);
1076 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1077 "SSID", ssid->ssid, ssid->ssid_len);
1078 }
1079 }
1080#endif /* CONFIG_P2P */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001081 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1082 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 if (wpa_s->wps_fragment_size)
1084 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1085 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1086 wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001087 wpas_wps_reassoc(wpa_s, ssid, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 return 0;
1089}
1090
1091
1092int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1093 const char *pin, int p2p_group, u16 dev_pw_id)
1094{
1095 struct wpa_ssid *ssid;
1096 char val[128];
1097 unsigned int rpin = 0;
1098
1099 wpas_clear_wps(wpa_s);
1100 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
1101 if (ssid == NULL)
1102 return -1;
1103 ssid->temporary = 1;
1104 ssid->p2p_group = p2p_group;
1105#ifdef CONFIG_P2P
1106 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1107 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1108 if (ssid->ssid) {
1109 ssid->ssid_len = wpa_s->go_params->ssid_len;
1110 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1111 ssid->ssid_len);
1112 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1113 "SSID", ssid->ssid, ssid->ssid_len);
1114 }
1115 }
1116#endif /* CONFIG_P2P */
1117 if (pin)
1118 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
1119 pin, dev_pw_id);
1120 else {
1121 rpin = wps_generate_pin();
1122 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
1123 rpin, dev_pw_id);
1124 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001125 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1126 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 if (wpa_s->wps_fragment_size)
1128 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1129 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1130 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001131 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001132 wpas_wps_reassoc(wpa_s, ssid, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 return rpin;
1134}
1135
1136
1137/* Cancel the wps pbc/pin requests */
1138int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1139{
1140#ifdef CONFIG_AP
1141 if (wpa_s->ap_iface) {
1142 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1143 return wpa_supplicant_ap_wps_cancel(wpa_s);
1144 }
1145#endif /* CONFIG_AP */
1146
Dmitry Shmidt04949592012-07-19 12:16:46 -07001147 if (wpa_s->wpa_state == WPA_SCANNING ||
1148 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1150 wpa_supplicant_cancel_scan(wpa_s);
1151 wpas_clear_wps(wpa_s);
1152 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1153 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1154 "deauthenticate");
1155 wpa_supplicant_deauthenticate(wpa_s,
1156 WLAN_REASON_DEAUTH_LEAVING);
1157 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001158 } else {
1159 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001160 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 }
1162
Dmitry Shmidt051af732013-10-22 13:52:46 -07001163 wpa_s->after_wps = 0;
1164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 return 0;
1166}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167
1168
1169int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1170 const char *pin, struct wps_new_ap_settings *settings)
1171{
1172 struct wpa_ssid *ssid;
1173 char val[200];
1174 char *pos, *end;
1175 int res;
1176
1177 if (!pin)
1178 return -1;
1179 wpas_clear_wps(wpa_s);
1180 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
1181 if (ssid == NULL)
1182 return -1;
1183 ssid->temporary = 1;
1184 pos = val;
1185 end = pos + sizeof(val);
1186 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1187 if (res < 0 || res >= end - pos)
1188 return -1;
1189 pos += res;
1190 if (settings) {
1191 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1192 "new_encr=%s new_key=%s",
1193 settings->ssid_hex, settings->auth,
1194 settings->encr, settings->key_hex);
1195 if (res < 0 || res >= end - pos)
1196 return -1;
1197 pos += res;
1198 }
1199 res = os_snprintf(pos, end - pos, "\"");
1200 if (res < 0 || res >= end - pos)
1201 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001202 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1203 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 if (wpa_s->wps_fragment_size)
1205 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1206 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1207 wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001208 wpas_wps_reassoc(wpa_s, ssid, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 return 0;
1210}
1211
1212
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001213static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1214 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 size_t psk_len)
1216{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001217 if (is_zero_ether_addr(p2p_dev_addr)) {
1218 wpa_printf(MSG_DEBUG,
1219 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1220 MAC2STR(mac_addr));
1221 } else {
1222 wpa_printf(MSG_DEBUG,
1223 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1224 " P2P Device Addr " MACSTR,
1225 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1226 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1228
1229 /* TODO */
1230
1231 return 0;
1232}
1233
1234
1235static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1236 const struct wps_device_data *dev)
1237{
1238 char uuid[40], txt[400];
1239 int len;
1240 char devtype[WPS_DEV_TYPE_BUFSIZE];
1241 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1242 return;
1243 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1244 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1245 " [%s|%s|%s|%s|%s|%s]",
1246 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1247 dev->manufacturer, dev->model_name,
1248 dev->model_number, dev->serial_number,
1249 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1250 sizeof(devtype)));
1251 if (len > 0 && len < (int) sizeof(txt))
1252 wpa_printf(MSG_INFO, "%s", txt);
1253}
1254
1255
1256static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1257 u16 sel_reg_config_methods)
1258{
1259#ifdef CONFIG_WPS_ER
1260 struct wpa_supplicant *wpa_s = ctx;
1261
1262 if (wpa_s->wps_er == NULL)
1263 return;
1264 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1265 "dev_password_id=%u sel_reg_config_methods=0x%x",
1266 sel_reg, dev_passwd_id, sel_reg_config_methods);
1267 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1268 sel_reg_config_methods);
1269#endif /* CONFIG_WPS_ER */
1270}
1271
1272
1273static u16 wps_fix_config_methods(u16 config_methods)
1274{
1275#ifdef CONFIG_WPS2
1276 if ((config_methods &
1277 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1278 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1279 wpa_printf(MSG_INFO, "WPS: Converting display to "
1280 "virtual_display for WPS 2.0 compliance");
1281 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1282 }
1283 if ((config_methods &
1284 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1285 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1286 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1287 "virtual_push_button for WPS 2.0 compliance");
1288 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1289 }
1290#endif /* CONFIG_WPS2 */
1291
1292 return config_methods;
1293}
1294
1295
1296static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1297 struct wps_context *wps)
1298{
1299 wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
1300 if (is_nil_uuid(wpa_s->conf->uuid)) {
1301 struct wpa_supplicant *first;
1302 first = wpa_s->global->ifaces;
1303 while (first && first->next)
1304 first = first->next;
1305 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001306 if (wps != wpa_s->global->ifaces->wps)
1307 os_memcpy(wps->uuid,
1308 wpa_s->global->ifaces->wps->uuid,
1309 WPS_UUID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
1311 "interface", wps->uuid, WPS_UUID_LEN);
1312 } else {
1313 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1314 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1315 "address", wps->uuid, WPS_UUID_LEN);
1316 }
1317 } else {
1318 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1319 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
1320 wps->uuid, WPS_UUID_LEN);
1321 }
1322}
1323
1324
Dmitry Shmidt04949592012-07-19 12:16:46 -07001325static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1326 struct wps_context *wps)
1327{
1328 wpabuf_free(wps->dev.vendor_ext_m1);
1329 wps->dev.vendor_ext_m1 = NULL;
1330
1331 if (wpa_s->conf->wps_vendor_ext_m1) {
1332 wps->dev.vendor_ext_m1 =
1333 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1334 if (!wps->dev.vendor_ext_m1) {
1335 wpa_printf(MSG_ERROR, "WPS: Cannot "
1336 "allocate memory for vendor_ext_m1");
1337 }
1338 }
1339}
1340
1341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342int wpas_wps_init(struct wpa_supplicant *wpa_s)
1343{
1344 struct wps_context *wps;
1345 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001346 struct hostapd_hw_modes *modes;
1347 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348
1349 wps = os_zalloc(sizeof(*wps));
1350 if (wps == NULL)
1351 return -1;
1352
1353 wps->cred_cb = wpa_supplicant_wps_cred;
1354 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001355 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 wps->cb_ctx = wpa_s;
1357
1358 wps->dev.device_name = wpa_s->conf->device_name;
1359 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1360 wps->dev.model_name = wpa_s->conf->model_name;
1361 wps->dev.model_number = wpa_s->conf->model_number;
1362 wps->dev.serial_number = wpa_s->conf->serial_number;
1363 wps->config_methods =
1364 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1365 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1366 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1367 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1368 "methods are not allowed at the same time");
1369 os_free(wps);
1370 return -1;
1371 }
1372 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001373 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1375 WPS_DEV_TYPE_LEN);
1376
1377 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1378 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1379 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1380
Dmitry Shmidt04949592012-07-19 12:16:46 -07001381 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001384 modes = wpa_s->hw.modes;
1385 if (modes) {
1386 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1387 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1388 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1389 wps->dev.rf_bands |= WPS_RF_24GHZ;
1390 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1391 wps->dev.rf_bands |= WPS_RF_50GHZ;
1392 }
1393 }
1394 if (wps->dev.rf_bands == 0) {
1395 /*
1396 * Default to claiming support for both bands if the driver
1397 * does not provide support for fetching supported bands.
1398 */
1399 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1400 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1402 wpas_wps_set_uuid(wpa_s, wps);
1403
1404 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1405 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1406
1407 os_memset(&rcfg, 0, sizeof(rcfg));
1408 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1409 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1410 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1411 rcfg.cb_ctx = wpa_s;
1412
1413 wps->registrar = wps_registrar_init(wps, &rcfg);
1414 if (wps->registrar == NULL) {
1415 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1416 os_free(wps);
1417 return -1;
1418 }
1419
1420 wpa_s->wps = wps;
1421
1422 return 0;
1423}
1424
1425
1426void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1427{
1428 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001429 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001430 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001431 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432
1433 if (wpa_s->wps == NULL)
1434 return;
1435
1436#ifdef CONFIG_WPS_ER
1437 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1438 wpa_s->wps_er = NULL;
1439#endif /* CONFIG_WPS_ER */
1440
1441 wps_registrar_deinit(wpa_s->wps->registrar);
1442 wpabuf_free(wpa_s->wps->dh_pubkey);
1443 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001444 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 os_free(wpa_s->wps->network_key);
1446 os_free(wpa_s->wps);
1447 wpa_s->wps = NULL;
1448}
1449
1450
1451int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001452 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453{
1454 struct wpabuf *wps_ie;
1455
1456 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1457 return -1;
1458
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001459 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1461 if (!wps_ie) {
1462 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1463 return 0;
1464 }
1465
1466 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1467 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1468 "without active PBC Registrar");
1469 wpabuf_free(wps_ie);
1470 return 0;
1471 }
1472
1473 /* TODO: overlap detection */
1474 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1475 "(Active PBC)");
1476 wpabuf_free(wps_ie);
1477 return 1;
1478 }
1479
1480 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1481 if (!wps_ie) {
1482 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1483 return 0;
1484 }
1485
1486 /*
1487 * Start with WPS APs that advertise our address as an
1488 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1489 * allow any WPS AP after couple of scans since some APs do not
1490 * set Selected Registrar attribute properly when using
1491 * external Registrar.
1492 */
1493 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1494 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1495 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1496 "without active PIN Registrar");
1497 wpabuf_free(wps_ie);
1498 return 0;
1499 }
1500 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1501 } else {
1502 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1503 "(Authorized MAC or Active PIN)");
1504 }
1505 wpabuf_free(wps_ie);
1506 return 1;
1507 }
1508
1509 if (wps_ie) {
1510 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1511 wpabuf_free(wps_ie);
1512 return 1;
1513 }
1514
1515 return -1;
1516}
1517
1518
1519int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1520 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001521 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522{
1523 struct wpabuf *wps_ie = NULL;
1524 int ret = 0;
1525
1526 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001527 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1529 /* allow wildcard SSID for WPS PBC */
1530 ret = 1;
1531 }
1532 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001533 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 if (wps_ie &&
1535 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1536 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1537 /* allow wildcard SSID for WPS PIN */
1538 ret = 1;
1539 }
1540 }
1541
1542 if (!ret && ssid->bssid_set &&
1543 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1544 /* allow wildcard SSID due to hardcoded BSSID match */
1545 ret = 1;
1546 }
1547
1548#ifdef CONFIG_WPS_STRICT
1549 if (wps_ie) {
1550 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1551 0, bss->bssid) < 0)
1552 ret = 0;
1553 if (bss->beacon_ie_len) {
1554 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001555 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 bss, WPS_IE_VENDOR_TYPE);
1557 if (bcn_wps == NULL) {
1558 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1559 "missing from AP Beacon");
1560 ret = 0;
1561 } else {
1562 if (wps_validate_beacon(wps_ie) < 0)
1563 ret = 0;
1564 wpabuf_free(bcn_wps);
1565 }
1566 }
1567 }
1568#endif /* CONFIG_WPS_STRICT */
1569
1570 wpabuf_free(wps_ie);
1571
1572 return ret;
1573}
1574
1575
1576int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1577 struct wpa_bss *selected, struct wpa_ssid *ssid)
1578{
1579 const u8 *sel_uuid, *uuid;
1580 struct wpabuf *wps_ie;
1581 int ret = 0;
1582 struct wpa_bss *bss;
1583
1584 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1585 return 0;
1586
1587 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1588 "present in scan results; selected BSSID " MACSTR,
1589 MAC2STR(selected->bssid));
1590
1591 /* Make sure that only one AP is in active PBC mode */
1592 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1593 if (wps_ie) {
1594 sel_uuid = wps_get_uuid_e(wps_ie);
1595 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1596 sel_uuid, UUID_LEN);
1597 } else {
1598 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1599 "WPS IE?!");
1600 sel_uuid = NULL;
1601 }
1602
1603 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1604 struct wpabuf *ie;
1605 if (bss == selected)
1606 continue;
1607 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1608 if (!ie)
1609 continue;
1610 if (!wps_is_selected_pbc_registrar(ie)) {
1611 wpabuf_free(ie);
1612 continue;
1613 }
1614 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1615 MACSTR, MAC2STR(bss->bssid));
1616 uuid = wps_get_uuid_e(ie);
1617 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1618 uuid, UUID_LEN);
1619 if (sel_uuid == NULL || uuid == NULL ||
1620 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1621 ret = 1; /* PBC overlap */
1622 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1623 MACSTR " and " MACSTR,
1624 MAC2STR(selected->bssid),
1625 MAC2STR(bss->bssid));
1626 wpabuf_free(ie);
1627 break;
1628 }
1629
1630 /* TODO: verify that this is reasonable dual-band situation */
1631
1632 wpabuf_free(ie);
1633 }
1634
1635 wpabuf_free(wps_ie);
1636
1637 return ret;
1638}
1639
1640
1641void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1642{
1643 struct wpa_bss *bss;
1644 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1645
1646 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1647 return;
1648
1649 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1650 struct wpabuf *ie;
1651 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1652 if (!ie)
1653 continue;
1654 if (wps_is_selected_pbc_registrar(ie))
1655 pbc++;
1656 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1657 auth++;
1658 else if (wps_is_selected_pin_registrar(ie))
1659 pin++;
1660 else
1661 wps++;
1662 wpabuf_free(ie);
1663 }
1664
1665 if (pbc)
1666 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1667 else if (auth)
1668 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1669 else if (pin)
1670 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1671 else if (wps)
1672 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1673}
1674
1675
1676int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1677{
1678 struct wpa_ssid *ssid;
1679
1680 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1681 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1682 return 1;
1683 }
1684
1685 return 0;
1686}
1687
1688
1689int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1690 char *end)
1691{
1692 struct wpabuf *wps_ie;
1693 int ret;
1694
1695 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1696 if (wps_ie == NULL)
1697 return 0;
1698
1699 ret = wps_attr_text(wps_ie, buf, end);
1700 wpabuf_free(wps_ie);
1701 return ret;
1702}
1703
1704
1705int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1706{
1707#ifdef CONFIG_WPS_ER
1708 if (wpa_s->wps_er) {
1709 wps_er_refresh(wpa_s->wps_er);
1710 return 0;
1711 }
1712 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1713 if (wpa_s->wps_er == NULL)
1714 return -1;
1715 return 0;
1716#else /* CONFIG_WPS_ER */
1717 return 0;
1718#endif /* CONFIG_WPS_ER */
1719}
1720
1721
1722int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1723{
1724#ifdef CONFIG_WPS_ER
1725 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1726 wpa_s->wps_er = NULL;
1727#endif /* CONFIG_WPS_ER */
1728 return 0;
1729}
1730
1731
1732#ifdef CONFIG_WPS_ER
1733int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1734 const char *uuid, const char *pin)
1735{
1736 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001737 const u8 *use_uuid = NULL;
1738 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001740 if (os_strcmp(uuid, "any") == 0) {
1741 } else if (uuid_str2bin(uuid, u) == 0) {
1742 use_uuid = u;
1743 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1744 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1745 if (use_uuid == NULL)
1746 return -1;
1747 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 return -1;
1749 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001750 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751 (const u8 *) pin, os_strlen(pin), 300);
1752}
1753
1754
1755int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1756{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001757 u8 u[UUID_LEN], *use_uuid = NULL;
1758 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001760 if (uuid_str2bin(uuid, u) == 0)
1761 use_uuid = u;
1762 else if (hwaddr_aton(uuid, addr) == 0)
1763 use_addr = addr;
1764 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001766 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767}
1768
1769
1770int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1771 const char *pin)
1772{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001773 u8 u[UUID_LEN], *use_uuid = NULL;
1774 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001776 if (uuid_str2bin(uuid, u) == 0)
1777 use_uuid = u;
1778 else if (hwaddr_aton(uuid, addr) == 0)
1779 use_addr = addr;
1780 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001782
1783 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 os_strlen(pin));
1785}
1786
1787
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001788static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1789 struct wps_credential *cred)
1790{
1791 os_memset(cred, 0, sizeof(*cred));
1792 if (ssid->ssid_len > 32)
1793 return -1;
1794 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1795 cred->ssid_len = ssid->ssid_len;
1796 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1797 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1798 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1799 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1800 cred->encr_type = WPS_ENCR_AES;
1801 else
1802 cred->encr_type = WPS_ENCR_TKIP;
1803 if (ssid->passphrase) {
1804 cred->key_len = os_strlen(ssid->passphrase);
1805 if (cred->key_len >= 64)
1806 return -1;
1807 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1808 } else if (ssid->psk_set) {
1809 cred->key_len = 32;
1810 os_memcpy(cred->key, ssid->psk, 32);
1811 } else
1812 return -1;
1813 } else {
1814 cred->auth_type = WPS_AUTH_OPEN;
1815 cred->encr_type = WPS_ENCR_NONE;
1816 }
1817
1818 return 0;
1819}
1820
1821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1823 int id)
1824{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001825 u8 u[UUID_LEN], *use_uuid = NULL;
1826 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 struct wpa_ssid *ssid;
1828 struct wps_credential cred;
1829
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001830 if (uuid_str2bin(uuid, u) == 0)
1831 use_uuid = u;
1832 else if (hwaddr_aton(uuid, addr) == 0)
1833 use_addr = addr;
1834 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 return -1;
1836 ssid = wpa_config_get_network(wpa_s->conf, id);
1837 if (ssid == NULL || ssid->ssid == NULL)
1838 return -1;
1839
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001840 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001842 return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843}
1844
1845
1846int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1847 const char *pin, struct wps_new_ap_settings *settings)
1848{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001849 u8 u[UUID_LEN], *use_uuid = NULL;
1850 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 struct wps_credential cred;
1852 size_t len;
1853
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001854 if (uuid_str2bin(uuid, u) == 0)
1855 use_uuid = u;
1856 else if (hwaddr_aton(uuid, addr) == 0)
1857 use_addr = addr;
1858 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 return -1;
1860 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1861 settings->encr == NULL || settings->key_hex == NULL)
1862 return -1;
1863
1864 os_memset(&cred, 0, sizeof(cred));
1865 len = os_strlen(settings->ssid_hex);
1866 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1867 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1868 return -1;
1869 cred.ssid_len = len / 2;
1870
1871 len = os_strlen(settings->key_hex);
1872 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1873 hexstr2bin(settings->key_hex, cred.key, len / 2))
1874 return -1;
1875 cred.key_len = len / 2;
1876
1877 if (os_strcmp(settings->auth, "OPEN") == 0)
1878 cred.auth_type = WPS_AUTH_OPEN;
1879 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1880 cred.auth_type = WPS_AUTH_WPAPSK;
1881 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1882 cred.auth_type = WPS_AUTH_WPA2PSK;
1883 else
1884 return -1;
1885
1886 if (os_strcmp(settings->encr, "NONE") == 0)
1887 cred.encr_type = WPS_ENCR_NONE;
1888 else if (os_strcmp(settings->encr, "WEP") == 0)
1889 cred.encr_type = WPS_ENCR_WEP;
1890 else if (os_strcmp(settings->encr, "TKIP") == 0)
1891 cred.encr_type = WPS_ENCR_TKIP;
1892 else if (os_strcmp(settings->encr, "CCMP") == 0)
1893 cred.encr_type = WPS_ENCR_AES;
1894 else
1895 return -1;
1896
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001897 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1898 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899}
1900
1901
Dmitry Shmidt04949592012-07-19 12:16:46 -07001902#ifdef CONFIG_WPS_NFC
1903struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1904 int ndef, const char *uuid)
1905{
1906 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001907 u8 u[UUID_LEN], *use_uuid = NULL;
1908 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001909
1910 if (!wpa_s->wps_er)
1911 return NULL;
1912
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001913 if (uuid_str2bin(uuid, u) == 0)
1914 use_uuid = u;
1915 else if (hwaddr_aton(uuid, addr) == 0)
1916 use_addr = addr;
1917 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07001918 return NULL;
1919
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001920 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001921 if (ndef && ret) {
1922 struct wpabuf *tmp;
1923 tmp = ndef_build_wifi(ret);
1924 wpabuf_free(ret);
1925 if (tmp == NULL)
1926 return NULL;
1927 ret = tmp;
1928 }
1929
1930 return ret;
1931}
1932#endif /* CONFIG_WPS_NFC */
1933
1934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935static int callbacks_pending = 0;
1936
1937static void wpas_wps_terminate_cb(void *ctx)
1938{
1939 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1940 if (--callbacks_pending <= 0)
1941 eloop_terminate();
1942}
1943#endif /* CONFIG_WPS_ER */
1944
1945
1946int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1947{
1948#ifdef CONFIG_WPS_ER
1949 if (wpa_s->wps_er) {
1950 callbacks_pending++;
1951 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1952 wpa_s->wps_er = NULL;
1953 return 1;
1954 }
1955#endif /* CONFIG_WPS_ER */
1956 return 0;
1957}
1958
1959
1960int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1961{
1962 struct wpa_ssid *ssid;
1963
1964 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1965 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1966 return 1;
1967 }
1968
1969 return 0;
1970}
1971
1972
1973void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1974{
1975 struct wps_context *wps = wpa_s->wps;
1976
1977 if (wps == NULL)
1978 return;
1979
1980 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1981 wps->config_methods = wps_config_methods_str2bin(
1982 wpa_s->conf->config_methods);
1983 if ((wps->config_methods &
1984 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1985 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1986 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1987 "config methods are not allowed at the "
1988 "same time");
1989 wps->config_methods &= ~WPS_CONFIG_LABEL;
1990 }
1991 }
1992 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08001993 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994
1995 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
1996 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1997 WPS_DEV_TYPE_LEN);
1998
1999 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2000 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2001 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2002 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2003 }
2004
Dmitry Shmidt04949592012-07-19 12:16:46 -07002005 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2006 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2009 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2010
2011 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2012 wpas_wps_set_uuid(wpa_s, wps);
2013
2014 if (wpa_s->conf->changed_parameters &
2015 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2016 /* Update pointers to make sure they refer current values */
2017 wps->dev.device_name = wpa_s->conf->device_name;
2018 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2019 wps->dev.model_name = wpa_s->conf->model_name;
2020 wps->dev.model_number = wpa_s->conf->model_number;
2021 wps->dev.serial_number = wpa_s->conf->serial_number;
2022 }
2023}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002024
2025
2026#ifdef CONFIG_WPS_NFC
2027
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002028#ifdef CONFIG_WPS_ER
2029static struct wpabuf *
2030wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2031 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002032{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002033 struct wpabuf *ret;
2034 struct wps_credential cred;
2035
2036 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2037 return NULL;
2038
2039 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2040
2041 if (ndef && ret) {
2042 struct wpabuf *tmp;
2043 tmp = ndef_build_wifi(ret);
2044 wpabuf_free(ret);
2045 if (tmp == NULL)
2046 return NULL;
2047 ret = tmp;
2048 }
2049
2050 return ret;
2051}
2052#endif /* CONFIG_WPS_ER */
2053
2054
2055struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2056 int ndef, const char *id_str)
2057{
2058#ifdef CONFIG_WPS_ER
2059 if (id_str) {
2060 int id;
2061 char *end = NULL;
2062 struct wpa_ssid *ssid;
2063
2064 id = strtol(id_str, &end, 10);
2065 if (end && *end)
2066 return NULL;
2067
2068 ssid = wpa_config_get_network(wpa_s->conf, id);
2069 if (ssid == NULL)
2070 return NULL;
2071 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2072 }
2073#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002074#ifdef CONFIG_AP
2075 if (wpa_s->ap_iface)
2076 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2077#endif /* CONFIG_AP */
2078 return NULL;
2079}
2080
2081
Dmitry Shmidt04949592012-07-19 12:16:46 -07002082struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2083{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002084 if (wpa_s->conf->wps_nfc_pw_from_config) {
2085 return wps_nfc_token_build(ndef,
2086 wpa_s->conf->wps_nfc_dev_pw_id,
2087 wpa_s->conf->wps_nfc_dh_pubkey,
2088 wpa_s->conf->wps_nfc_dev_pw);
2089 }
2090
Dmitry Shmidt04949592012-07-19 12:16:46 -07002091 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2092 &wpa_s->conf->wps_nfc_dh_pubkey,
2093 &wpa_s->conf->wps_nfc_dh_privkey,
2094 &wpa_s->conf->wps_nfc_dev_pw);
2095}
2096
2097
2098int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2099{
2100 struct wps_context *wps = wpa_s->wps;
2101 char pw[32 * 2 + 1];
2102
2103 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
2104 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
2105 wpa_s->conf->wps_nfc_dev_pw == NULL)
2106 return -1;
2107
2108 dh5_free(wps->dh_ctx);
2109 wpabuf_free(wps->dh_pubkey);
2110 wpabuf_free(wps->dh_privkey);
2111 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2112 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2113 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2114 wps->dh_ctx = NULL;
2115 wpabuf_free(wps->dh_pubkey);
2116 wps->dh_pubkey = NULL;
2117 wpabuf_free(wps->dh_privkey);
2118 wps->dh_privkey = NULL;
2119 return -1;
2120 }
2121 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002122 if (wps->dh_ctx == NULL) {
2123 wpabuf_free(wps->dh_pubkey);
2124 wps->dh_pubkey = NULL;
2125 wpabuf_free(wps->dh_privkey);
2126 wps->dh_privkey = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002127 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002128 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002129
2130 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2131 wpabuf_head(wpa_s->conf->wps_nfc_dev_pw),
2132 wpabuf_len(wpa_s->conf->wps_nfc_dev_pw));
2133 return wpas_wps_start_pin(wpa_s, bssid, pw, 0,
2134 wpa_s->conf->wps_nfc_dev_pw_id);
2135}
2136
2137
2138static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2139 struct wps_parse_attr *attr)
2140{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002141 wpa_s->wps_ap_channel = 0;
2142
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002143 /*
2144 * Disable existing networks temporarily to allow the newly learned
2145 * credential to be preferred. Enable the temporarily disabled networks
2146 * after 10 seconds.
2147 */
2148 wpas_wps_temp_disable(wpa_s, NULL);
2149 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2150 NULL);
2151
Dmitry Shmidt04949592012-07-19 12:16:46 -07002152 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2153 return -1;
2154
2155 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2156 return 0;
2157
2158 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2159 "based on the received credential added");
2160 wpa_s->normal_scans = 0;
2161 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002162 if (wpa_s->wps_ap_channel) {
2163 u16 chan = wpa_s->wps_ap_channel;
2164 int freq = 0;
2165
2166 if (chan >= 1 && chan <= 13)
2167 freq = 2407 + 5 * chan;
2168 else if (chan == 14)
2169 freq = 2484;
2170 else if (chan >= 30)
2171 freq = 5000 + 5 * chan;
2172
2173 if (freq) {
2174 wpa_printf(MSG_DEBUG, "WPS: Credential indicated "
2175 "AP channel %u -> %u MHz", chan, freq);
2176 wpa_s->after_wps = 5;
2177 wpa_s->wps_freq = freq;
2178 }
2179 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002180 wpa_s->disconnected = 0;
2181 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002182
2183 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002184 wpa_supplicant_req_scan(wpa_s, 0, 0);
2185
2186 return 0;
2187}
2188
2189
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002190#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002191static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2192 struct wps_parse_attr *attr)
2193{
2194 return wps_registrar_add_nfc_password_token(
2195 wpa_s->wps->registrar, attr->oob_dev_password,
2196 attr->oob_dev_password_len);
2197}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002198#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002199
2200
2201static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2202 const struct wpabuf *wps)
2203{
2204 struct wps_parse_attr attr;
2205
2206 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2207
2208 if (wps_parse_msg(wps, &attr)) {
2209 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2210 return -1;
2211 }
2212
2213 if (attr.num_cred)
2214 return wpas_wps_use_cred(wpa_s, &attr);
2215
2216#ifdef CONFIG_WPS_ER
2217 if (attr.oob_dev_password)
2218 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2219#endif /* CONFIG_WPS_ER */
2220
2221 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2222 return -1;
2223}
2224
2225
2226int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
2227 const struct wpabuf *data)
2228{
2229 const struct wpabuf *wps = data;
2230 struct wpabuf *tmp = NULL;
2231 int ret;
2232
2233 if (wpabuf_len(data) < 4)
2234 return -1;
2235
2236 if (*wpabuf_head_u8(data) != 0x10) {
2237 /* Assume this contains full NDEF record */
2238 tmp = ndef_parse_wifi(data);
2239 if (tmp == NULL) {
2240 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2241 return -1;
2242 }
2243 wps = tmp;
2244 }
2245
2246 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2247 wpabuf_free(tmp);
2248 return ret;
2249}
2250
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002251
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002252struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s, int cr)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002253{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002254 if (cr)
2255 return ndef_build_wifi_hc(1);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002256 return ndef_build_wifi_hr();
2257}
2258
2259
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002260#ifdef CONFIG_WPS_NFC
2261struct wpabuf * wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2262 int ndef, const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002263{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002264#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002265 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002266 u8 u[UUID_LEN], *use_uuid = NULL;
2267 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002268
2269 if (!wpa_s->wps_er)
2270 return NULL;
2271
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002272 if (uuid == NULL)
2273 return NULL;
2274 if (uuid_str2bin(uuid, u) == 0)
2275 use_uuid = u;
2276 else if (hwaddr_aton(uuid, addr) == 0)
2277 use_addr = addr;
2278 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002279 return NULL;
2280
2281 /*
2282 * Handover Select carrier record for WPS uses the same format as
2283 * configuration token.
2284 */
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002285 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002286 if (ndef && ret) {
2287 struct wpabuf *tmp;
2288 tmp = ndef_build_wifi(ret);
2289 wpabuf_free(ret);
2290 if (tmp == NULL)
2291 return NULL;
2292 ret = tmp;
2293 }
2294
2295 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002296#else /* CONFIG_WPS_ER */
2297 return NULL;
2298#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002299}
2300#endif /* CONFIG_WPS_NFC */
2301
2302
2303struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2304 int ndef, int cr, const char *uuid)
2305{
2306 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002307 if (!cr)
2308 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002309 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2310 if (ret)
2311 return ret;
2312 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002313}
2314
2315
2316int wpas_wps_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
2317 const struct wpabuf *data)
2318{
2319 /* TODO */
2320 return -1;
2321}
2322
2323
2324int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2325 const struct wpabuf *data)
2326{
2327 struct wpabuf *wps;
2328 int ret;
2329
2330 wps = ndef_parse_wifi(data);
2331 if (wps == NULL)
2332 return -1;
2333 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2334 "payload from NFC connection handover");
2335 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: NFC payload", wps);
2336 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2337 wpabuf_free(wps);
2338
2339 return ret;
2340}
2341
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002342
2343int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2344 const struct wpabuf *req,
2345 const struct wpabuf *sel)
2346{
2347 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2348 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2349 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2350 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2351}
2352
Dmitry Shmidt04949592012-07-19 12:16:46 -07002353#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002354
2355
2356extern int wpa_debug_level;
2357
2358static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2359{
2360 size_t i;
2361 struct os_time now;
2362
2363 if (wpa_debug_level > MSG_DEBUG)
2364 return;
2365
2366 if (wpa_s->wps_ap == NULL)
2367 return;
2368
2369 os_get_time(&now);
2370
2371 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2372 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2373 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2374
2375 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2376 "tries=%d last_attempt=%d sec ago blacklist=%d",
2377 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2378 ap->last_attempt.sec > 0 ?
2379 (int) now.sec - (int) ap->last_attempt.sec : -1,
2380 e ? e->count : 0);
2381 }
2382}
2383
2384
2385static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2386 const u8 *bssid)
2387{
2388 size_t i;
2389
2390 if (wpa_s->wps_ap == NULL)
2391 return NULL;
2392
2393 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2394 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2395 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2396 return ap;
2397 }
2398
2399 return NULL;
2400}
2401
2402
2403static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2404 struct wpa_scan_res *res)
2405{
2406 struct wpabuf *wps;
2407 enum wps_ap_info_type type;
2408 struct wps_ap_info *ap;
2409 int r;
2410
2411 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2412 return;
2413
2414 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2415 if (wps == NULL)
2416 return;
2417
2418 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2419 if (r == 2)
2420 type = WPS_AP_SEL_REG_OUR;
2421 else if (r == 1)
2422 type = WPS_AP_SEL_REG;
2423 else
2424 type = WPS_AP_NOT_SEL_REG;
2425
2426 wpabuf_free(wps);
2427
2428 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2429 if (ap) {
2430 if (ap->type != type) {
2431 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2432 " changed type %d -> %d",
2433 MAC2STR(res->bssid), ap->type, type);
2434 ap->type = type;
2435 if (type != WPS_AP_NOT_SEL_REG)
2436 wpa_blacklist_del(wpa_s, ap->bssid);
2437 }
2438 return;
2439 }
2440
2441 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2442 sizeof(struct wps_ap_info));
2443 if (ap == NULL)
2444 return;
2445
2446 wpa_s->wps_ap = ap;
2447 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2448 wpa_s->num_wps_ap++;
2449
2450 os_memset(ap, 0, sizeof(*ap));
2451 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2452 ap->type = type;
2453 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2454 MAC2STR(ap->bssid), ap->type);
2455}
2456
2457
2458void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2459 struct wpa_scan_results *scan_res)
2460{
2461 size_t i;
2462
2463 for (i = 0; i < scan_res->num; i++)
2464 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2465
2466 wpas_wps_dump_ap_info(wpa_s);
2467}
2468
2469
2470void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2471{
2472 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002473
2474 wpa_s->after_wps = 0;
2475
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002476 if (!wpa_s->wps_ap_iter)
2477 return;
2478 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2479 if (ap == NULL)
2480 return;
2481 ap->tries++;
2482 os_get_time(&ap->last_attempt);
2483}