blob: bd8af830c085b0e7683bbb03dd9016be6b5cf712 [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
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070042/*
43 * The minimum time in seconds before trying to associate to a WPS PIN AP that
44 * does not have Selected Registrar TRUE.
45 */
46#ifndef WPS_PIN_TIME_IGNORE_SEL_REG
47#define WPS_PIN_TIME_IGNORE_SEL_REG 5
48#endif /* WPS_PIN_TIME_IGNORE_SEL_REG */
49
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
51static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
52
53
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070054static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
55{
56 os_free(wpa_s->wps_ap);
57 wpa_s->wps_ap = NULL;
58 wpa_s->num_wps_ap = 0;
59 wpa_s->wps_ap_iter = 0;
60}
61
62
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070063static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
64{
65 struct wpa_supplicant *wpa_s = eloop_ctx;
66 int use_fast_assoc = timeout_ctx != NULL;
67
68 wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
69 if (!use_fast_assoc ||
70 wpa_supplicant_fast_associate(wpa_s) != 1)
71 wpa_supplicant_req_scan(wpa_s, 0, 0);
72}
73
74
75static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
76{
77 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
78 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
79}
80
81
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
83{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080084 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
85 return 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087 if (!wpa_s->wps_success &&
88 wpa_s->current_ssid &&
89 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
90 const u8 *bssid = wpa_s->bssid;
91 if (is_zero_ether_addr(bssid))
92 bssid = wpa_s->pending_bssid;
93
94 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
95 " did not succeed - continue trying to find "
96 "suitable AP", MAC2STR(bssid));
97 wpa_blacklist_add(wpa_s, bssid);
98
99 wpa_supplicant_deauthenticate(wpa_s,
100 WLAN_REASON_DEAUTH_LEAVING);
101 wpa_s->reassociate = 1;
102 wpa_supplicant_req_scan(wpa_s,
103 wpa_s->blacklist_cleared ? 5 : 0, 0);
104 wpa_s->blacklist_cleared = 0;
105 return 1;
106 }
107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700108 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
110 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
111 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
112
113 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
114 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
115 int disabled = wpa_s->current_ssid->disabled;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800116 unsigned int freq = wpa_s->assoc_freq;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700117 struct wpa_bss *bss;
118 struct wpa_ssid *ssid = NULL;
119 int use_fast_assoc = 0;
120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800122 "try to associate with the received credential "
123 "(freq=%u)", freq);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800124 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 wpa_supplicant_deauthenticate(wpa_s,
126 WLAN_REASON_DEAUTH_LEAVING);
127 if (disabled) {
128 wpa_printf(MSG_DEBUG, "WPS: Current network is "
129 "disabled - wait for user to enable");
130 return 1;
131 }
132 wpa_s->after_wps = 5;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800133 wpa_s->wps_freq = freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800134 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 wpa_s->reassociate = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700136
137 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
138 "without a new scan can be used");
139 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
140 if (bss) {
141 struct wpabuf *wps;
142 struct wps_parse_attr attr;
143
144 wps = wpa_bss_get_vendor_ie_multi(bss,
145 WPS_IE_VENDOR_TYPE);
146 if (wps && wps_parse_msg(wps, &attr) == 0 &&
147 attr.wps_state &&
148 *attr.wps_state == WPS_STATE_CONFIGURED)
149 use_fast_assoc = 1;
150 wpabuf_free(wps);
151 }
152
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700153 /*
154 * Complete the next step from an eloop timeout to allow pending
155 * driver events related to the disconnection to be processed
156 * first. This makes it less likely for disconnection event to
157 * cause problems with the following connection.
158 */
159 wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
160 wpas_wps_assoc_with_cred_cancel(wpa_s);
161 eloop_register_timeout(0, 10000,
162 wpas_wps_assoc_with_cred, wpa_s,
163 use_fast_assoc ? (void *) 1 :
164 (void *) 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 return 1;
166 }
167
168 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
169 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
170 "for external credential processing");
171 wpas_clear_wps(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800172 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 wpa_supplicant_deauthenticate(wpa_s,
174 WLAN_REASON_DEAUTH_LEAVING);
175 return 1;
176 }
177
178 return 0;
179}
180
181
182static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
183 struct wpa_ssid *ssid,
184 const struct wps_credential *cred)
185{
186 struct wpa_driver_capa capa;
187 struct wpa_bss *bss;
188 const u8 *ie;
189 struct wpa_ie_data adv;
190 int wpa2 = 0, ccmp = 0;
191
192 /*
193 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
194 * case they are configured for mixed mode operation (WPA+WPA2 and
195 * TKIP+CCMP). Try to use scan results to figure out whether the AP
196 * actually supports stronger security and select that if the client
197 * has support for it, too.
198 */
199
200 if (wpa_drv_get_capa(wpa_s, &capa))
201 return; /* Unknown what driver supports */
202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203 if (ssid->ssid == NULL)
204 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
206 if (bss == NULL) {
207 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
208 "table - use credential as-is");
209 return;
210 }
211
212 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
213
214 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
215 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
216 wpa2 = 1;
217 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
218 ccmp = 1;
219 } else {
220 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
221 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
222 adv.pairwise_cipher & WPA_CIPHER_CCMP)
223 ccmp = 1;
224 }
225
226 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
227 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
228 /*
229 * TODO: This could be the initial AP configuration and the
230 * Beacon contents could change shortly. Should request a new
231 * scan and delay addition of the network until the updated
232 * scan results are available.
233 */
234 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
235 "support - use credential as-is");
236 return;
237 }
238
239 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
240 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
241 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
242 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
243 "based on scan results");
244 if (wpa_s->conf->ap_scan == 1)
245 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
246 else
247 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
248 }
249
250 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
251 (ssid->proto & WPA_PROTO_WPA) &&
252 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
253 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
254 "based on scan results");
255 if (wpa_s->conf->ap_scan == 1)
256 ssid->proto |= WPA_PROTO_RSN;
257 else
258 ssid->proto = WPA_PROTO_RSN;
259 }
260}
261
262
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700263static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
264 struct wpa_ssid *new_ssid)
265{
266 struct wpa_ssid *ssid, *next;
267
268 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
269 ssid = next, next = ssid ? ssid->next : NULL) {
270 /*
271 * new_ssid has already been added to the list in
272 * wpas_wps_add_network(), so skip it.
273 */
274 if (ssid == new_ssid)
275 continue;
276
277 if (ssid->bssid_set || new_ssid->bssid_set) {
278 if (ssid->bssid_set != new_ssid->bssid_set)
279 continue;
280 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
281 0)
282 continue;
283 }
284
285 /* compare SSID */
286 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
287 continue;
288
289 if (ssid->ssid && new_ssid->ssid) {
290 if (os_memcmp(ssid->ssid, new_ssid->ssid,
291 ssid->ssid_len) != 0)
292 continue;
293 } else if (ssid->ssid || new_ssid->ssid)
294 continue;
295
296 /* compare security parameters */
297 if (ssid->auth_alg != new_ssid->auth_alg ||
298 ssid->key_mgmt != new_ssid->key_mgmt ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800299 (ssid->group_cipher != new_ssid->group_cipher &&
300 !(ssid->group_cipher & new_ssid->group_cipher &
301 WPA_CIPHER_CCMP)))
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700302 continue;
303
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700304 /*
305 * Some existing WPS APs will send two creds in case they are
306 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
307 * Try to merge these two creds if they are received in the same
308 * M8 message.
309 */
310 if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
311 wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
312 if (new_ssid->passphrase && ssid->passphrase &&
313 os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
314 0) {
315 wpa_printf(MSG_DEBUG,
316 "WPS: M8 Creds with different passphrase - do not merge");
317 continue;
318 }
319
320 if (new_ssid->psk_set &&
321 (!ssid->psk_set ||
322 os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
323 wpa_printf(MSG_DEBUG,
324 "WPS: M8 Creds with different PSK - do not merge");
325 continue;
326 }
327
328 if ((new_ssid->passphrase && !ssid->passphrase) ||
329 (!new_ssid->passphrase && ssid->passphrase)) {
330 wpa_printf(MSG_DEBUG,
331 "WPS: M8 Creds with different passphrase/PSK type - do not merge");
332 continue;
333 }
334
335 wpa_printf(MSG_DEBUG,
336 "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
337 new_ssid->proto |= ssid->proto;
338 new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
339 } else {
340 /*
341 * proto and pairwise_cipher difference matter for
342 * non-mixed-mode creds.
343 */
344 if (ssid->proto != new_ssid->proto ||
345 ssid->pairwise_cipher != new_ssid->pairwise_cipher)
346 continue;
347 }
348
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700349 /* Remove the duplicated older network entry. */
350 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
351 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800352 if (wpa_s->current_ssid == ssid)
353 wpa_s->current_ssid = NULL;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700354 wpa_config_remove_network(wpa_s->conf, ssid->id);
355 }
356}
357
358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359static int wpa_supplicant_wps_cred(void *ctx,
360 const struct wps_credential *cred)
361{
362 struct wpa_supplicant *wpa_s = ctx;
363 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 u16 auth_type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800365#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 int registrar = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800367#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368
369 if ((wpa_s->conf->wps_cred_processing == 1 ||
370 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
371 size_t blen = cred->cred_attr_len * 2 + 1;
372 char *buf = os_malloc(blen);
373 if (buf) {
374 wpa_snprintf_hex(buf, blen,
375 cred->cred_attr, cred->cred_attr_len);
376 wpa_msg(wpa_s, MSG_INFO, "%s%s",
377 WPS_EVENT_CRED_RECEIVED, buf);
378 os_free(buf);
379 }
380
381 wpas_notify_wps_credential(wpa_s, cred);
382 } else
383 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
384
385 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
386 cred->cred_attr, cred->cred_attr_len);
387
388 if (wpa_s->conf->wps_cred_processing == 1)
389 return 0;
390
391 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
392 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
393 cred->auth_type);
394 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
395 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
396 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
397 cred->key, cred->key_len);
398 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
399 MAC2STR(cred->mac_addr));
400
401 auth_type = cred->auth_type;
402 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
403 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
404 "auth_type into WPA2PSK");
405 auth_type = WPS_AUTH_WPA2PSK;
406 }
407
408 if (auth_type != WPS_AUTH_OPEN &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 auth_type != WPS_AUTH_WPAPSK &&
410 auth_type != WPS_AUTH_WPA2PSK) {
411 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
412 "unsupported authentication type 0x%x",
413 auth_type);
414 return 0;
415 }
416
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800417 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
418 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
419 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
420 "invalid Network Key length %lu",
421 (unsigned long) cred->key_len);
422 return -1;
423 }
424 }
425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
427 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
428 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800429#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430 if (ssid->eap.identity &&
431 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
432 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
433 WSC_ID_REGISTRAR_LEN) == 0)
434 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800435#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 os_free(ssid->eap.identity);
437 ssid->eap.identity = NULL;
438 ssid->eap.identity_len = 0;
439 os_free(ssid->eap.phase1);
440 ssid->eap.phase1 = NULL;
441 os_free(ssid->eap.eap_methods);
442 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700443 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700445 ssid->bssid_set = 0;
446 }
447 ssid->disabled_until.sec = 0;
448 ssid->disabled_until.usec = 0;
449 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 } else {
451 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
452 "received credential");
453 ssid = wpa_config_add_network(wpa_s->conf);
454 if (ssid == NULL)
455 return -1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700456 if (wpa_s->current_ssid) {
457 /*
458 * Should the GO issue multiple credentials for some
459 * reason, each credential should be marked as a
460 * temporary P2P group similarly to the one that gets
461 * marked as such based on the pre-configured values
462 * used for the WPS network block.
463 */
464 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
465 ssid->temporary = wpa_s->current_ssid->temporary;
466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 wpas_notify_network_added(wpa_s, ssid);
468 }
469
470 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700471 ssid->wps_run = wpa_s->wps_run;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472
473 os_free(ssid->ssid);
474 ssid->ssid = os_malloc(cred->ssid_len);
475 if (ssid->ssid) {
476 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
477 ssid->ssid_len = cred->ssid_len;
478 }
479
480 switch (cred->encr_type) {
481 case WPS_ENCR_NONE:
482 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 case WPS_ENCR_TKIP:
484 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
485 break;
486 case WPS_ENCR_AES:
487 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800488 if (wpa_s->drv_capa_known &&
489 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
490 ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
491 ssid->group_cipher |= WPA_CIPHER_GCMP;
492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 break;
494 }
495
496 switch (auth_type) {
497 case WPS_AUTH_OPEN:
498 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
499 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
500 ssid->proto = 0;
501#ifdef CONFIG_WPS_REG_DISABLE_OPEN
502 if (registrar) {
503 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
504 "id=%d - Credentials for an open "
505 "network disabled by default - use "
506 "'select_network %d' to enable",
507 ssid->id, ssid->id);
508 ssid->disabled = 1;
509 }
510#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
511 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512 case WPS_AUTH_WPAPSK:
513 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
514 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
515 ssid->proto = WPA_PROTO_WPA;
516 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517 case WPS_AUTH_WPA2PSK:
518 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
519 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
520 ssid->proto = WPA_PROTO_RSN;
521 break;
522 }
523
524 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
525 if (cred->key_len == 2 * PMK_LEN) {
526 if (hexstr2bin((const char *) cred->key, ssid->psk,
527 PMK_LEN)) {
528 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
529 "Key");
530 return -1;
531 }
532 ssid->psk_set = 1;
533 ssid->export_keys = 1;
534 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
535 os_free(ssid->passphrase);
536 ssid->passphrase = os_malloc(cred->key_len + 1);
537 if (ssid->passphrase == NULL)
538 return -1;
539 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
540 ssid->passphrase[cred->key_len] = '\0';
541 wpa_config_update_psk(ssid);
542 ssid->export_keys = 1;
543 } else {
544 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
545 "length %lu",
546 (unsigned long) cred->key_len);
547 return -1;
548 }
549 }
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700550 ssid->priority = wpa_s->conf->wps_priority;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551
552 wpas_wps_security_workaround(wpa_s, ssid, cred);
553
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700554 wpas_wps_remove_dup_network(wpa_s, ssid);
555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556#ifndef CONFIG_NO_CONFIG_WRITE
557 if (wpa_s->conf->update_config &&
558 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
559 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
560 return -1;
561 }
562#endif /* CONFIG_NO_CONFIG_WRITE */
563
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700564 if (ssid->priority)
565 wpa_config_update_prio_list(wpa_s->conf);
566
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800567 /*
568 * Optimize the post-WPS scan based on the channel used during
569 * the provisioning in case EAP-Failure is not received.
570 */
571 wpa_s->after_wps = 5;
572 wpa_s->wps_freq = wpa_s->assoc_freq;
573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574 return 0;
575}
576
577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
579 struct wps_event_m2d *m2d)
580{
581 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
582 "dev_password_id=%d config_error=%d",
583 m2d->dev_password_id, m2d->config_error);
584 wpas_notify_wps_event_m2d(wpa_s, m2d);
585#ifdef CONFIG_P2P
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800586 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
587 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 "dev_password_id=%d config_error=%d",
589 m2d->dev_password_id, m2d->config_error);
590 }
591 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
592 /*
593 * Notify P2P from eloop timeout to avoid issues with the
594 * interface getting removed while processing a message.
595 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700596 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597 NULL);
598 }
599#endif /* CONFIG_P2P */
600}
601
602
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700603static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
604{
605 struct wpa_supplicant *wpa_s = eloop_ctx;
606 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
607 wpas_clear_wps(wpa_s);
608}
609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610
611static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
612 struct wps_event_fail *fail)
613{
614 if (fail->error_indication > 0 &&
615 fail->error_indication < NUM_WPS_EI_VALUES) {
616 wpa_msg(wpa_s, MSG_INFO,
617 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
618 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700619 wps_ei_str(fail->error_indication));
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800620 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
621 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 "msg=%d config_error=%d reason=%d (%s)",
623 fail->msg, fail->config_error,
624 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700625 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 } else {
627 wpa_msg(wpa_s, MSG_INFO,
628 WPS_EVENT_FAIL "msg=%d config_error=%d",
629 fail->msg, fail->config_error);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800630 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
631 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 "msg=%d config_error=%d",
633 fail->msg, fail->config_error);
634 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700635
636 /*
637 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
638 */
639 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
640 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
641 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700644 wpas_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645}
646
647
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800648static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
649
650static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
651{
652 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800653 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800654
655 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
656
657 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
658 if (ssid->disabled_for_connect && ssid->disabled) {
659 ssid->disabled_for_connect = 0;
660 ssid->disabled = 0;
661 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800662 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800663 }
664 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800665
666 if (changed) {
667#ifndef CONFIG_NO_CONFIG_WRITE
668 if (wpa_s->conf->update_config &&
669 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
670 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
671 "configuration");
672 }
673#endif /* CONFIG_NO_CONFIG_WRITE */
674 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800675}
676
677
678static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
679{
680 struct wpa_supplicant *wpa_s = eloop_ctx;
681 /* Enable the networks disabled during wpas_wps_reassoc */
682 wpas_wps_reenable_networks(wpa_s);
683}
684
685
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800686int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
687{
688 return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
689 wpa_s, NULL);
690}
691
692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
694{
695 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
696 wpa_s->wps_success = 1;
697 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700698 if (wpa_s->current_ssid)
699 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
700 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800701
702 /*
703 * Enable the networks disabled during wpas_wps_reassoc after 10
704 * seconds. The 10 seconds timer is to allow the data connection to be
705 * formed before allowing other networks to be selected.
706 */
707 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
708 NULL);
709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711}
712
713
714static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
715 struct wps_event_er_ap *ap)
716{
717 char uuid_str[100];
718 char dev_type[WPS_DEV_TYPE_BUFSIZE];
719
720 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
721 if (ap->pri_dev_type)
722 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
723 sizeof(dev_type));
724 else
725 dev_type[0] = '\0';
726
727 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
728 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
729 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
730 ap->friendly_name ? ap->friendly_name : "",
731 ap->manufacturer ? ap->manufacturer : "",
732 ap->model_description ? ap->model_description : "",
733 ap->model_name ? ap->model_name : "",
734 ap->manufacturer_url ? ap->manufacturer_url : "",
735 ap->model_url ? ap->model_url : "");
736}
737
738
739static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
740 struct wps_event_er_ap *ap)
741{
742 char uuid_str[100];
743 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
744 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
745}
746
747
748static void wpa_supplicant_wps_event_er_enrollee_add(
749 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
750{
751 char uuid_str[100];
752 char dev_type[WPS_DEV_TYPE_BUFSIZE];
753
754 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
755 if (enrollee->pri_dev_type)
756 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
757 sizeof(dev_type));
758 else
759 dev_type[0] = '\0';
760
761 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
762 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
763 "|%s|%s|%s|%s|%s|",
764 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
765 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
766 enrollee->dev_name ? enrollee->dev_name : "",
767 enrollee->manufacturer ? enrollee->manufacturer : "",
768 enrollee->model_name ? enrollee->model_name : "",
769 enrollee->model_number ? enrollee->model_number : "",
770 enrollee->serial_number ? enrollee->serial_number : "");
771}
772
773
774static void wpa_supplicant_wps_event_er_enrollee_remove(
775 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
776{
777 char uuid_str[100];
778 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
779 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
780 uuid_str, MAC2STR(enrollee->mac_addr));
781}
782
783
784static void wpa_supplicant_wps_event_er_ap_settings(
785 struct wpa_supplicant *wpa_s,
786 struct wps_event_er_ap_settings *ap_settings)
787{
788 char uuid_str[100];
789 char key_str[65];
790 const struct wps_credential *cred = ap_settings->cred;
791
792 key_str[0] = '\0';
793 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
794 if (cred->key_len >= 8 && cred->key_len <= 64) {
795 os_memcpy(key_str, cred->key, cred->key_len);
796 key_str[cred->key_len] = '\0';
797 }
798 }
799
800 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
801 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
802 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
803 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
804 "key=%s",
805 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
806 cred->auth_type, cred->encr_type, key_str);
807}
808
809
810static void wpa_supplicant_wps_event_er_set_sel_reg(
811 struct wpa_supplicant *wpa_s,
812 struct wps_event_er_set_selected_registrar *ev)
813{
814 char uuid_str[100];
815
816 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
817 switch (ev->state) {
818 case WPS_ER_SET_SEL_REG_START:
819 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
820 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
821 "sel_reg_config_methods=0x%x",
822 uuid_str, ev->sel_reg, ev->dev_passwd_id,
823 ev->sel_reg_config_methods);
824 break;
825 case WPS_ER_SET_SEL_REG_DONE:
826 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
827 "uuid=%s state=DONE", uuid_str);
828 break;
829 case WPS_ER_SET_SEL_REG_FAILED:
830 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
831 "uuid=%s state=FAILED", uuid_str);
832 break;
833 }
834}
835
836
837static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
838 union wps_event_data *data)
839{
840 struct wpa_supplicant *wpa_s = ctx;
841 switch (event) {
842 case WPS_EV_M2D:
843 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
844 break;
845 case WPS_EV_FAIL:
846 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
847 break;
848 case WPS_EV_SUCCESS:
849 wpa_supplicant_wps_event_success(wpa_s);
850 break;
851 case WPS_EV_PWD_AUTH_FAIL:
852#ifdef CONFIG_AP
853 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
854 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
855#endif /* CONFIG_AP */
856 break;
857 case WPS_EV_PBC_OVERLAP:
858 break;
859 case WPS_EV_PBC_TIMEOUT:
860 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700861 case WPS_EV_PBC_ACTIVE:
862 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
863 break;
864 case WPS_EV_PBC_DISABLE:
865 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
866 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 case WPS_EV_ER_AP_ADD:
868 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
869 break;
870 case WPS_EV_ER_AP_REMOVE:
871 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
872 break;
873 case WPS_EV_ER_ENROLLEE_ADD:
874 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
875 &data->enrollee);
876 break;
877 case WPS_EV_ER_ENROLLEE_REMOVE:
878 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
879 &data->enrollee);
880 break;
881 case WPS_EV_ER_AP_SETTINGS:
882 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
883 &data->ap_settings);
884 break;
885 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
886 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
887 &data->set_sel_reg);
888 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800889 case WPS_EV_AP_PIN_SUCCESS:
890 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 }
892}
893
894
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700895static int wpa_supplicant_wps_rf_band(void *ctx)
896{
897 struct wpa_supplicant *wpa_s = ctx;
898
899 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
900 return 0;
901
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700902 return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
903 (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700904}
905
906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
908{
909 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
910 eap_is_wps_pin_enrollee(&ssid->eap))
911 return WPS_REQ_ENROLLEE;
912 else
913 return WPS_REQ_REGISTRAR;
914}
915
916
917static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
918{
919 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700920 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
921
Dmitry Shmidt051af732013-10-22 13:52:46 -0700922 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700923 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700924
Jouni Malinen75ecf522011-06-27 15:19:46 -0700925 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800927 /* Enable the networks disabled during wpas_wps_reassoc */
928 wpas_wps_reenable_networks(wpa_s);
929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800931 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932
933 /* Remove any existing WPS network from configuration */
934 ssid = wpa_s->conf->ssid;
935 while (ssid) {
936 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
937 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800938 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700939 wpa_supplicant_deauthenticate(
940 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 }
942 id = ssid->id;
943 remove_ssid = ssid;
944 } else
945 id = -1;
946 ssid = ssid->next;
947 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700948 if (prev_current == remove_ssid) {
949 wpa_sm_set_config(wpa_s->wpa, NULL);
950 eapol_sm_notify_config(wpa_s->eapol, NULL,
951 NULL);
952 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 wpas_notify_network_removed(wpa_s, remove_ssid);
954 wpa_config_remove_network(wpa_s->conf, id);
955 }
956 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700957
958 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959}
960
961
962static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
963{
964 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800965 union wps_event_data data;
966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
968 "out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800969 os_memset(&data, 0, sizeof(data));
970 data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
971 data.fail.error_indication = WPS_EI_NO_ERROR;
972 /*
973 * Call wpas_notify_wps_event_fail() directly instead of through
974 * wpa_supplicant_wps_event() which would end up registering unnecessary
975 * timeouts (those are only for the case where the failure happens
976 * during an EAP-WSC exchange).
977 */
978 wpas_notify_wps_event_fail(wpa_s, &data.fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 wpas_clear_wps(wpa_s);
980}
981
982
983static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800984 int registrar, const u8 *dev_addr,
985 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986{
987 struct wpa_ssid *ssid;
988
989 ssid = wpa_config_add_network(wpa_s->conf);
990 if (ssid == NULL)
991 return NULL;
992 wpas_notify_network_added(wpa_s, ssid);
993 wpa_config_set_network_defaults(ssid);
994 ssid->temporary = 1;
995 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
996 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
997 wpa_config_set(ssid, "identity", registrar ?
998 "\"" WSC_ID_REGISTRAR "\"" :
999 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1000 wpas_notify_network_removed(wpa_s, ssid);
1001 wpa_config_remove_network(wpa_s->conf, ssid->id);
1002 return NULL;
1003 }
1004
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001005#ifdef CONFIG_P2P
1006 if (dev_addr)
1007 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1008#endif /* CONFIG_P2P */
1009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 if (bssid) {
1011#ifndef CONFIG_P2P
1012 struct wpa_bss *bss;
1013 int count = 0;
1014#endif /* CONFIG_P2P */
1015
1016 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1017 ssid->bssid_set = 1;
1018
1019 /*
1020 * Note: With P2P, the SSID may change at the time the WPS
1021 * provisioning is started, so better not filter the AP based
1022 * on the current SSID in the scan results.
1023 */
1024#ifndef CONFIG_P2P
1025 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1026 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
1027 continue;
1028
1029 os_free(ssid->ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001030 ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 if (ssid->ssid == NULL)
1032 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 ssid->ssid_len = bss->ssid_len;
1034 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1035 "scan results",
1036 ssid->ssid, ssid->ssid_len);
1037 count++;
1038 }
1039
1040 if (count > 1) {
1041 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1042 "for the AP; use wildcard");
1043 os_free(ssid->ssid);
1044 ssid->ssid = NULL;
1045 ssid->ssid_len = 0;
1046 }
1047#endif /* CONFIG_P2P */
1048 }
1049
1050 return ssid;
1051}
1052
1053
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001054static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1055 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056{
1057 struct wpa_ssid *ssid;
1058
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001059 if (wpa_s->current_ssid) {
1060 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001061 wpa_supplicant_deauthenticate(
1062 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001063 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 /* Mark all other networks disabled and trigger reassociation */
1066 ssid = wpa_s->conf->ssid;
1067 while (ssid) {
1068 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001069 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001070 /*
1071 * In case the network object corresponds to a persistent group
1072 * then do not send out network disabled signal. In addition,
1073 * do not change disabled status of persistent network objects
1074 * from 2 to 1 should we connect to another network.
1075 */
1076 if (was_disabled != 2) {
1077 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001078 if (was_disabled != ssid->disabled) {
1079 if (ssid->disabled)
1080 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001081 wpas_notify_network_enabled_changed(wpa_s,
1082 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001083 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001084 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 ssid = ssid->next;
1086 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001087}
1088
1089
1090static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001091 struct wpa_ssid *selected, const u8 *bssid,
1092 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001093{
1094 struct wpa_bss *bss;
1095
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001096 wpa_s->wps_run++;
1097 if (wpa_s->wps_run == 0)
1098 wpa_s->wps_run++;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001099 wpa_s->after_wps = 0;
1100 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001101 if (freq) {
1102 wpa_s->after_wps = 5;
1103 wpa_s->wps_freq = freq;
1104 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001105 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1106 if (bss && bss->freq > 0) {
1107 wpa_s->known_wps_freq = 1;
1108 wpa_s->wps_freq = bss->freq;
1109 }
1110 }
1111
1112 wpas_wps_temp_disable(wpa_s, selected);
1113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 wpa_s->disconnected = 0;
1115 wpa_s->reassociate = 1;
1116 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001117 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 wpa_s->wps_success = 0;
1119 wpa_s->blacklist_cleared = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001120
1121 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 wpa_supplicant_req_scan(wpa_s, 0, 0);
1123}
1124
1125
1126int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1127 int p2p_group)
1128{
1129 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001130
1131#ifdef CONFIG_AP
1132 if (wpa_s->ap_iface) {
1133 wpa_printf(MSG_DEBUG,
1134 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1135 return -1;
1136 }
1137#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001139 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 if (ssid == NULL)
1141 return -1;
1142 ssid->temporary = 1;
1143 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001144 /*
1145 * When starting a regular WPS process (not P2P group formation)
1146 * the registrar/final station can be either AP or PCP
1147 * so use a "don't care" value for the pbss flag.
1148 */
1149 if (!p2p_group)
1150 ssid->pbss = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151#ifdef CONFIG_P2P
1152 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1153 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1154 if (ssid->ssid) {
1155 ssid->ssid_len = wpa_s->go_params->ssid_len;
1156 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1157 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001158 if (wpa_s->go_params->freq > 56160) {
1159 /* P2P in 60 GHz uses PBSS */
1160 ssid->pbss = 1;
1161 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1163 "SSID", ssid->ssid, ssid->ssid_len);
1164 }
1165 }
1166#endif /* CONFIG_P2P */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001167 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1168 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (wpa_s->wps_fragment_size)
1170 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001171 wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1173 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001174 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 return 0;
1176}
1177
1178
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001179static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1180 const u8 *dev_addr, const u8 *bssid,
1181 const char *pin, int p2p_group, u16 dev_pw_id,
1182 const u8 *peer_pubkey_hash,
1183 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184{
1185 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001186 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001188 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001190#ifdef CONFIG_AP
1191 if (wpa_s->ap_iface) {
1192 wpa_printf(MSG_DEBUG,
1193 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1194 return -1;
1195 }
1196#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001198 if (bssid && is_zero_ether_addr(bssid))
1199 bssid = NULL;
1200 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1201 if (ssid == NULL) {
1202 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001204 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 ssid->temporary = 1;
1206 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001207 /*
1208 * When starting a regular WPS process (not P2P group formation)
1209 * the registrar/final station can be either AP or PCP
1210 * so use a "don't care" value for the pbss flag.
1211 */
1212 if (!p2p_group)
1213 ssid->pbss = 2;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001214 if (ssid_val) {
1215 ssid->ssid = os_malloc(ssid_len);
1216 if (ssid->ssid) {
1217 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1218 ssid->ssid_len = ssid_len;
1219 }
1220 }
1221 if (peer_pubkey_hash) {
1222 os_memcpy(hash, " pkhash=", 8);
1223 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1224 peer_pubkey_hash,
1225 WPS_OOB_PUBKEY_HASH_LEN);
1226 } else {
1227 hash[0] = '\0';
1228 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229#ifdef CONFIG_P2P
1230 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001231 os_free(ssid->ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1233 if (ssid->ssid) {
1234 ssid->ssid_len = wpa_s->go_params->ssid_len;
1235 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1236 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001237 if (wpa_s->go_params->freq > 56160) {
1238 /* P2P in 60 GHz uses PBSS */
1239 ssid->pbss = 1;
1240 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1242 "SSID", ssid->ssid, ssid->ssid_len);
1243 }
1244 }
1245#endif /* CONFIG_P2P */
1246 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001247 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1248 pin, dev_pw_id, hash);
1249 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1250 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1251 dev_pw_id, hash);
1252 } else {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001253 if (wps_generate_pin(&rpin) < 0) {
1254 wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1255 return -1;
1256 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001257 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1258 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001260 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1261 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001262 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001263 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 if (wpa_s->wps_fragment_size)
1265 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1266 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1267 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001268 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001269 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 return rpin;
1271}
1272
1273
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001274int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1275 const char *pin, int p2p_group, u16 dev_pw_id)
1276{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001277 os_get_reltime(&wpa_s->wps_pin_start_time);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001278 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1279 dev_pw_id, NULL, NULL, 0, 0);
1280}
1281
1282
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001283void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1284{
1285 union wps_event_data data;
1286
1287 os_memset(&data, 0, sizeof(data));
1288 data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1289 data.fail.error_indication = WPS_EI_NO_ERROR;
1290 /*
1291 * Call wpas_notify_wps_event_fail() directly instead of through
1292 * wpa_supplicant_wps_event() which would end up registering unnecessary
1293 * timeouts (those are only for the case where the failure happens
1294 * during an EAP-WSC exchange).
1295 */
1296 wpas_notify_wps_event_fail(wpa_s, &data.fail);
1297}
1298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299/* Cancel the wps pbc/pin requests */
1300int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1301{
1302#ifdef CONFIG_AP
1303 if (wpa_s->ap_iface) {
1304 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1305 return wpa_supplicant_ap_wps_cancel(wpa_s);
1306 }
1307#endif /* CONFIG_AP */
1308
Dmitry Shmidt04949592012-07-19 12:16:46 -07001309 if (wpa_s->wpa_state == WPA_SCANNING ||
1310 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1312 wpa_supplicant_cancel_scan(wpa_s);
1313 wpas_clear_wps(wpa_s);
1314 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1315 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1316 "deauthenticate");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001317 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 wpa_supplicant_deauthenticate(wpa_s,
1319 WLAN_REASON_DEAUTH_LEAVING);
1320 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001321 } else {
1322 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001323 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001324 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1325 0)
1326 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327 }
1328
Dmitry Shmidt051af732013-10-22 13:52:46 -07001329 wpa_s->after_wps = 0;
1330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 return 0;
1332}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333
1334
1335int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1336 const char *pin, struct wps_new_ap_settings *settings)
1337{
1338 struct wpa_ssid *ssid;
1339 char val[200];
1340 char *pos, *end;
1341 int res;
1342
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001343#ifdef CONFIG_AP
1344 if (wpa_s->ap_iface) {
1345 wpa_printf(MSG_DEBUG,
1346 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1347 return -1;
1348 }
1349#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 if (!pin)
1351 return -1;
1352 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001353 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 if (ssid == NULL)
1355 return -1;
1356 ssid->temporary = 1;
1357 pos = val;
1358 end = pos + sizeof(val);
1359 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001360 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 return -1;
1362 pos += res;
1363 if (settings) {
1364 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1365 "new_encr=%s new_key=%s",
1366 settings->ssid_hex, settings->auth,
1367 settings->encr, settings->key_hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001368 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 return -1;
1370 pos += res;
1371 }
1372 res = os_snprintf(pos, end - pos, "\"");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001373 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001375 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1376 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 if (wpa_s->wps_fragment_size)
1378 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1379 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1380 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001381 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 return 0;
1383}
1384
1385
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001386static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1387 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 size_t psk_len)
1389{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001390 if (is_zero_ether_addr(p2p_dev_addr)) {
1391 wpa_printf(MSG_DEBUG,
1392 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1393 MAC2STR(mac_addr));
1394 } else {
1395 wpa_printf(MSG_DEBUG,
1396 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1397 " P2P Device Addr " MACSTR,
1398 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1401
1402 /* TODO */
1403
1404 return 0;
1405}
1406
1407
1408static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1409 const struct wps_device_data *dev)
1410{
1411 char uuid[40], txt[400];
1412 int len;
1413 char devtype[WPS_DEV_TYPE_BUFSIZE];
1414 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1415 return;
1416 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1417 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1418 " [%s|%s|%s|%s|%s|%s]",
1419 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1420 dev->manufacturer, dev->model_name,
1421 dev->model_number, dev->serial_number,
1422 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1423 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001424 if (!os_snprintf_error(sizeof(txt), len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 wpa_printf(MSG_INFO, "%s", txt);
1426}
1427
1428
1429static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1430 u16 sel_reg_config_methods)
1431{
1432#ifdef CONFIG_WPS_ER
1433 struct wpa_supplicant *wpa_s = ctx;
1434
1435 if (wpa_s->wps_er == NULL)
1436 return;
1437 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1438 "dev_password_id=%u sel_reg_config_methods=0x%x",
1439 sel_reg, dev_passwd_id, sel_reg_config_methods);
1440 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1441 sel_reg_config_methods);
1442#endif /* CONFIG_WPS_ER */
1443}
1444
1445
1446static u16 wps_fix_config_methods(u16 config_methods)
1447{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 if ((config_methods &
1449 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1450 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1451 wpa_printf(MSG_INFO, "WPS: Converting display to "
1452 "virtual_display for WPS 2.0 compliance");
1453 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1454 }
1455 if ((config_methods &
1456 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1457 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1458 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1459 "virtual_push_button for WPS 2.0 compliance");
1460 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1461 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462
1463 return config_methods;
1464}
1465
1466
1467static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1468 struct wps_context *wps)
1469{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001470 char buf[50];
1471 const char *src;
1472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 if (is_nil_uuid(wpa_s->conf->uuid)) {
1474 struct wpa_supplicant *first;
1475 first = wpa_s->global->ifaces;
1476 while (first && first->next)
1477 first = first->next;
1478 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001479 if (wps != wpa_s->global->ifaces->wps)
1480 os_memcpy(wps->uuid,
1481 wpa_s->global->ifaces->wps->uuid,
1482 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001483 src = "from the first interface";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001484 } else if (wpa_s->conf->auto_uuid == 1) {
1485 uuid_random(wps->uuid);
1486 src = "based on random data";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487 } else {
1488 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001489 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 }
1491 } else {
1492 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001493 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001495
1496 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1497 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498}
1499
1500
Dmitry Shmidt04949592012-07-19 12:16:46 -07001501static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1502 struct wps_context *wps)
1503{
1504 wpabuf_free(wps->dev.vendor_ext_m1);
1505 wps->dev.vendor_ext_m1 = NULL;
1506
1507 if (wpa_s->conf->wps_vendor_ext_m1) {
1508 wps->dev.vendor_ext_m1 =
1509 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1510 if (!wps->dev.vendor_ext_m1) {
1511 wpa_printf(MSG_ERROR, "WPS: Cannot "
1512 "allocate memory for vendor_ext_m1");
1513 }
1514 }
1515}
1516
1517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518int wpas_wps_init(struct wpa_supplicant *wpa_s)
1519{
1520 struct wps_context *wps;
1521 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001522 struct hostapd_hw_modes *modes;
1523 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524
1525 wps = os_zalloc(sizeof(*wps));
1526 if (wps == NULL)
1527 return -1;
1528
1529 wps->cred_cb = wpa_supplicant_wps_cred;
1530 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001531 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 wps->cb_ctx = wpa_s;
1533
1534 wps->dev.device_name = wpa_s->conf->device_name;
1535 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1536 wps->dev.model_name = wpa_s->conf->model_name;
1537 wps->dev.model_number = wpa_s->conf->model_number;
1538 wps->dev.serial_number = wpa_s->conf->serial_number;
1539 wps->config_methods =
1540 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1541 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1542 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1543 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1544 "methods are not allowed at the same time");
1545 os_free(wps);
1546 return -1;
1547 }
1548 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1551 WPS_DEV_TYPE_LEN);
1552
1553 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1554 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1555 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1556
Dmitry Shmidt04949592012-07-19 12:16:46 -07001557 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001560 modes = wpa_s->hw.modes;
1561 if (modes) {
1562 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1563 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1564 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1565 wps->dev.rf_bands |= WPS_RF_24GHZ;
1566 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1567 wps->dev.rf_bands |= WPS_RF_50GHZ;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001568 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1569 wps->dev.rf_bands |= WPS_RF_60GHZ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001570 }
1571 }
1572 if (wps->dev.rf_bands == 0) {
1573 /*
1574 * Default to claiming support for both bands if the driver
1575 * does not provide support for fetching supported bands.
1576 */
1577 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1578 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1580 wpas_wps_set_uuid(wpa_s, wps);
1581
1582 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1583 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1584
1585 os_memset(&rcfg, 0, sizeof(rcfg));
1586 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1587 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1588 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1589 rcfg.cb_ctx = wpa_s;
1590
1591 wps->registrar = wps_registrar_init(wps, &rcfg);
1592 if (wps->registrar == NULL) {
1593 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1594 os_free(wps);
1595 return -1;
1596 }
1597
1598 wpa_s->wps = wps;
1599
1600 return 0;
1601}
1602
1603
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001604#ifdef CONFIG_WPS_ER
1605static void wpas_wps_nfc_clear(struct wps_context *wps)
1606{
1607 wps->ap_nfc_dev_pw_id = 0;
1608 wpabuf_free(wps->ap_nfc_dh_pubkey);
1609 wps->ap_nfc_dh_pubkey = NULL;
1610 wpabuf_free(wps->ap_nfc_dh_privkey);
1611 wps->ap_nfc_dh_privkey = NULL;
1612 wpabuf_free(wps->ap_nfc_dev_pw);
1613 wps->ap_nfc_dev_pw = NULL;
1614}
1615#endif /* CONFIG_WPS_ER */
1616
1617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1619{
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001620 wpas_wps_assoc_with_cred_cancel(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001622 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001623 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001624 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001626#ifdef CONFIG_P2P
1627 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1628#endif /* CONFIG_P2P */
1629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630 if (wpa_s->wps == NULL)
1631 return;
1632
1633#ifdef CONFIG_WPS_ER
1634 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1635 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001636 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637#endif /* CONFIG_WPS_ER */
1638
1639 wps_registrar_deinit(wpa_s->wps->registrar);
1640 wpabuf_free(wpa_s->wps->dh_pubkey);
1641 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001642 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 os_free(wpa_s->wps->network_key);
1644 os_free(wpa_s->wps);
1645 wpa_s->wps = NULL;
1646}
1647
1648
1649int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001650 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651{
1652 struct wpabuf *wps_ie;
1653
1654 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1655 return -1;
1656
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001657 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1659 if (!wps_ie) {
1660 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1661 return 0;
1662 }
1663
1664 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1665 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1666 "without active PBC Registrar");
1667 wpabuf_free(wps_ie);
1668 return 0;
1669 }
1670
1671 /* TODO: overlap detection */
1672 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1673 "(Active PBC)");
1674 wpabuf_free(wps_ie);
1675 return 1;
1676 }
1677
1678 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1679 if (!wps_ie) {
1680 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1681 return 0;
1682 }
1683
1684 /*
1685 * Start with WPS APs that advertise our address as an
1686 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1687 * allow any WPS AP after couple of scans since some APs do not
1688 * set Selected Registrar attribute properly when using
1689 * external Registrar.
1690 */
1691 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001692 struct os_reltime age;
1693
1694 os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1695
1696 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1697 age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1698 wpa_printf(MSG_DEBUG,
1699 " skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1700 wpa_s->scan_runs, (int) age.sec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 wpabuf_free(wps_ie);
1702 return 0;
1703 }
1704 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1705 } else {
1706 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1707 "(Authorized MAC or Active PIN)");
1708 }
1709 wpabuf_free(wps_ie);
1710 return 1;
1711 }
1712
1713 if (wps_ie) {
1714 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1715 wpabuf_free(wps_ie);
1716 return 1;
1717 }
1718
1719 return -1;
1720}
1721
1722
1723int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1724 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001725 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726{
1727 struct wpabuf *wps_ie = NULL;
1728 int ret = 0;
1729
1730 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001731 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1733 /* allow wildcard SSID for WPS PBC */
1734 ret = 1;
1735 }
1736 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001737 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 if (wps_ie &&
1739 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1740 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1741 /* allow wildcard SSID for WPS PIN */
1742 ret = 1;
1743 }
1744 }
1745
1746 if (!ret && ssid->bssid_set &&
1747 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1748 /* allow wildcard SSID due to hardcoded BSSID match */
1749 ret = 1;
1750 }
1751
1752#ifdef CONFIG_WPS_STRICT
1753 if (wps_ie) {
1754 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1755 0, bss->bssid) < 0)
1756 ret = 0;
1757 if (bss->beacon_ie_len) {
1758 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001759 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760 bss, WPS_IE_VENDOR_TYPE);
1761 if (bcn_wps == NULL) {
1762 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1763 "missing from AP Beacon");
1764 ret = 0;
1765 } else {
1766 if (wps_validate_beacon(wps_ie) < 0)
1767 ret = 0;
1768 wpabuf_free(bcn_wps);
1769 }
1770 }
1771 }
1772#endif /* CONFIG_WPS_STRICT */
1773
1774 wpabuf_free(wps_ie);
1775
1776 return ret;
1777}
1778
1779
1780int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1781 struct wpa_bss *selected, struct wpa_ssid *ssid)
1782{
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001783 const u8 *sel_uuid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 struct wpabuf *wps_ie;
1785 int ret = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001786 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787
1788 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1789 return 0;
1790
1791 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1792 "present in scan results; selected BSSID " MACSTR,
1793 MAC2STR(selected->bssid));
1794
1795 /* Make sure that only one AP is in active PBC mode */
1796 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1797 if (wps_ie) {
1798 sel_uuid = wps_get_uuid_e(wps_ie);
1799 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1800 sel_uuid, UUID_LEN);
1801 } else {
1802 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1803 "WPS IE?!");
1804 sel_uuid = NULL;
1805 }
1806
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001807 for (i = 0; i < wpa_s->num_wps_ap; i++) {
1808 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
1809
1810 if (!ap->pbc_active ||
1811 os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001812 continue;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001814 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001815 MACSTR, MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001817 ap->uuid, UUID_LEN);
1818 if (sel_uuid == NULL ||
1819 os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 ret = 1; /* PBC overlap */
1821 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1822 MACSTR " and " MACSTR,
1823 MAC2STR(selected->bssid),
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001824 MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 break;
1826 }
1827
1828 /* TODO: verify that this is reasonable dual-band situation */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 }
1830
1831 wpabuf_free(wps_ie);
1832
1833 return ret;
1834}
1835
1836
1837void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1838{
1839 struct wpa_bss *bss;
1840 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1841
1842 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1843 return;
1844
1845 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1846 struct wpabuf *ie;
1847 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1848 if (!ie)
1849 continue;
1850 if (wps_is_selected_pbc_registrar(ie))
1851 pbc++;
1852 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1853 auth++;
1854 else if (wps_is_selected_pin_registrar(ie))
1855 pin++;
1856 else
1857 wps++;
1858 wpabuf_free(ie);
1859 }
1860
1861 if (pbc)
1862 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1863 else if (auth)
1864 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1865 else if (pin)
1866 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1867 else if (wps)
1868 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1869}
1870
1871
1872int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1873{
1874 struct wpa_ssid *ssid;
1875
1876 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1877 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1878 return 1;
1879 }
1880
1881 return 0;
1882}
1883
1884
1885int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1886 char *end)
1887{
1888 struct wpabuf *wps_ie;
1889 int ret;
1890
1891 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1892 if (wps_ie == NULL)
1893 return 0;
1894
1895 ret = wps_attr_text(wps_ie, buf, end);
1896 wpabuf_free(wps_ie);
1897 return ret;
1898}
1899
1900
1901int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1902{
1903#ifdef CONFIG_WPS_ER
1904 if (wpa_s->wps_er) {
1905 wps_er_refresh(wpa_s->wps_er);
1906 return 0;
1907 }
1908 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1909 if (wpa_s->wps_er == NULL)
1910 return -1;
1911 return 0;
1912#else /* CONFIG_WPS_ER */
1913 return 0;
1914#endif /* CONFIG_WPS_ER */
1915}
1916
1917
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001918void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919{
1920#ifdef CONFIG_WPS_ER
1921 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1922 wpa_s->wps_er = NULL;
1923#endif /* CONFIG_WPS_ER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924}
1925
1926
1927#ifdef CONFIG_WPS_ER
1928int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1929 const char *uuid, const char *pin)
1930{
1931 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001932 const u8 *use_uuid = NULL;
1933 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001935 if (os_strcmp(uuid, "any") == 0) {
1936 } else if (uuid_str2bin(uuid, u) == 0) {
1937 use_uuid = u;
1938 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1939 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1940 if (use_uuid == NULL)
1941 return -1;
1942 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943 return -1;
1944 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001945 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 (const u8 *) pin, os_strlen(pin), 300);
1947}
1948
1949
1950int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1951{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001952 u8 u[UUID_LEN], *use_uuid = NULL;
1953 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001955 if (uuid_str2bin(uuid, u) == 0)
1956 use_uuid = u;
1957 else if (hwaddr_aton(uuid, addr) == 0)
1958 use_addr = addr;
1959 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001961 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962}
1963
1964
1965int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1966 const char *pin)
1967{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001968 u8 u[UUID_LEN], *use_uuid = NULL;
1969 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001971 if (uuid_str2bin(uuid, u) == 0)
1972 use_uuid = u;
1973 else if (hwaddr_aton(uuid, addr) == 0)
1974 use_addr = addr;
1975 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001977
1978 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 os_strlen(pin));
1980}
1981
1982
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001983static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1984 struct wps_credential *cred)
1985{
1986 os_memset(cred, 0, sizeof(*cred));
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001987 if (ssid->ssid_len > SSID_MAX_LEN)
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001988 return -1;
1989 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1990 cred->ssid_len = ssid->ssid_len;
1991 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1992 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1993 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1994 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1995 cred->encr_type = WPS_ENCR_AES;
1996 else
1997 cred->encr_type = WPS_ENCR_TKIP;
1998 if (ssid->passphrase) {
1999 cred->key_len = os_strlen(ssid->passphrase);
2000 if (cred->key_len >= 64)
2001 return -1;
2002 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2003 } else if (ssid->psk_set) {
2004 cred->key_len = 32;
2005 os_memcpy(cred->key, ssid->psk, 32);
2006 } else
2007 return -1;
2008 } else {
2009 cred->auth_type = WPS_AUTH_OPEN;
2010 cred->encr_type = WPS_ENCR_NONE;
2011 }
2012
2013 return 0;
2014}
2015
2016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2018 int id)
2019{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002020 u8 u[UUID_LEN], *use_uuid = NULL;
2021 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 struct wpa_ssid *ssid;
2023 struct wps_credential cred;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002024 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002026 if (uuid_str2bin(uuid, u) == 0)
2027 use_uuid = u;
2028 else if (hwaddr_aton(uuid, addr) == 0)
2029 use_addr = addr;
2030 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 return -1;
2032 ssid = wpa_config_get_network(wpa_s->conf, id);
2033 if (ssid == NULL || ssid->ssid == NULL)
2034 return -1;
2035
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002036 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002038 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2039 os_memset(&cred, 0, sizeof(cred));
2040 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041}
2042
2043
2044int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2045 const char *pin, struct wps_new_ap_settings *settings)
2046{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002047 u8 u[UUID_LEN], *use_uuid = NULL;
2048 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 struct wps_credential cred;
2050 size_t len;
2051
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002052 if (uuid_str2bin(uuid, u) == 0)
2053 use_uuid = u;
2054 else if (hwaddr_aton(uuid, addr) == 0)
2055 use_addr = addr;
2056 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 return -1;
2058 if (settings->ssid_hex == NULL || settings->auth == NULL ||
2059 settings->encr == NULL || settings->key_hex == NULL)
2060 return -1;
2061
2062 os_memset(&cred, 0, sizeof(cred));
2063 len = os_strlen(settings->ssid_hex);
2064 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2065 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2066 return -1;
2067 cred.ssid_len = len / 2;
2068
2069 len = os_strlen(settings->key_hex);
2070 if ((len & 1) || len > 2 * sizeof(cred.key) ||
2071 hexstr2bin(settings->key_hex, cred.key, len / 2))
2072 return -1;
2073 cred.key_len = len / 2;
2074
2075 if (os_strcmp(settings->auth, "OPEN") == 0)
2076 cred.auth_type = WPS_AUTH_OPEN;
2077 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2078 cred.auth_type = WPS_AUTH_WPAPSK;
2079 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2080 cred.auth_type = WPS_AUTH_WPA2PSK;
2081 else
2082 return -1;
2083
2084 if (os_strcmp(settings->encr, "NONE") == 0)
2085 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002086#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087 else if (os_strcmp(settings->encr, "WEP") == 0)
2088 cred.encr_type = WPS_ENCR_WEP;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002089#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 else if (os_strcmp(settings->encr, "TKIP") == 0)
2091 cred.encr_type = WPS_ENCR_TKIP;
2092 else if (os_strcmp(settings->encr, "CCMP") == 0)
2093 cred.encr_type = WPS_ENCR_AES;
2094 else
2095 return -1;
2096
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002097 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2098 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099}
2100
2101
Dmitry Shmidt04949592012-07-19 12:16:46 -07002102#ifdef CONFIG_WPS_NFC
2103struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2104 int ndef, const char *uuid)
2105{
2106 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002107 u8 u[UUID_LEN], *use_uuid = NULL;
2108 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002109
2110 if (!wpa_s->wps_er)
2111 return NULL;
2112
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002113 if (uuid_str2bin(uuid, u) == 0)
2114 use_uuid = u;
2115 else if (hwaddr_aton(uuid, addr) == 0)
2116 use_addr = addr;
2117 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002118 return NULL;
2119
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002120 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002121 if (ndef && ret) {
2122 struct wpabuf *tmp;
2123 tmp = ndef_build_wifi(ret);
2124 wpabuf_free(ret);
2125 if (tmp == NULL)
2126 return NULL;
2127 ret = tmp;
2128 }
2129
2130 return ret;
2131}
2132#endif /* CONFIG_WPS_NFC */
2133
2134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135static int callbacks_pending = 0;
2136
2137static void wpas_wps_terminate_cb(void *ctx)
2138{
2139 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2140 if (--callbacks_pending <= 0)
2141 eloop_terminate();
2142}
2143#endif /* CONFIG_WPS_ER */
2144
2145
2146int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2147{
2148#ifdef CONFIG_WPS_ER
2149 if (wpa_s->wps_er) {
2150 callbacks_pending++;
2151 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2152 wpa_s->wps_er = NULL;
2153 return 1;
2154 }
2155#endif /* CONFIG_WPS_ER */
2156 return 0;
2157}
2158
2159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2161{
2162 struct wps_context *wps = wpa_s->wps;
2163
2164 if (wps == NULL)
2165 return;
2166
2167 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2168 wps->config_methods = wps_config_methods_str2bin(
2169 wpa_s->conf->config_methods);
2170 if ((wps->config_methods &
2171 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2172 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2173 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2174 "config methods are not allowed at the "
2175 "same time");
2176 wps->config_methods &= ~WPS_CONFIG_LABEL;
2177 }
2178 }
2179 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002180 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181
2182 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2183 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2184 WPS_DEV_TYPE_LEN);
2185
2186 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2187 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2188 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2189 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2190 }
2191
Dmitry Shmidt04949592012-07-19 12:16:46 -07002192 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2193 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2196 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2197
2198 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2199 wpas_wps_set_uuid(wpa_s, wps);
2200
2201 if (wpa_s->conf->changed_parameters &
2202 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2203 /* Update pointers to make sure they refer current values */
2204 wps->dev.device_name = wpa_s->conf->device_name;
2205 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2206 wps->dev.model_name = wpa_s->conf->model_name;
2207 wps->dev.model_number = wpa_s->conf->model_number;
2208 wps->dev.serial_number = wpa_s->conf->serial_number;
2209 }
2210}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002211
2212
2213#ifdef CONFIG_WPS_NFC
2214
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002215#ifdef CONFIG_WPS_ER
Roshan Pius8894db22017-02-13 14:43:20 -08002216struct wpabuf *
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002217wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2218 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002219{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002220 struct wpabuf *ret;
2221 struct wps_credential cred;
2222
2223 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2224 return NULL;
2225
2226 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2227
2228 if (ndef && ret) {
2229 struct wpabuf *tmp;
2230 tmp = ndef_build_wifi(ret);
2231 wpabuf_free(ret);
2232 if (tmp == NULL)
2233 return NULL;
2234 ret = tmp;
2235 }
2236
2237 return ret;
2238}
2239#endif /* CONFIG_WPS_ER */
2240
2241
2242struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2243 int ndef, const char *id_str)
2244{
2245#ifdef CONFIG_WPS_ER
2246 if (id_str) {
2247 int id;
2248 char *end = NULL;
2249 struct wpa_ssid *ssid;
2250
2251 id = strtol(id_str, &end, 10);
2252 if (end && *end)
2253 return NULL;
2254
2255 ssid = wpa_config_get_network(wpa_s->conf, id);
2256 if (ssid == NULL)
2257 return NULL;
2258 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2259 }
2260#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002261#ifdef CONFIG_AP
2262 if (wpa_s->ap_iface)
2263 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2264#endif /* CONFIG_AP */
2265 return NULL;
2266}
2267
2268
Dmitry Shmidt04949592012-07-19 12:16:46 -07002269struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2270{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002271 if (wpa_s->conf->wps_nfc_pw_from_config) {
2272 return wps_nfc_token_build(ndef,
2273 wpa_s->conf->wps_nfc_dev_pw_id,
2274 wpa_s->conf->wps_nfc_dh_pubkey,
2275 wpa_s->conf->wps_nfc_dev_pw);
2276 }
2277
Dmitry Shmidt04949592012-07-19 12:16:46 -07002278 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2279 &wpa_s->conf->wps_nfc_dh_pubkey,
2280 &wpa_s->conf->wps_nfc_dh_privkey,
2281 &wpa_s->conf->wps_nfc_dev_pw);
2282}
2283
2284
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002285int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2286 const u8 *bssid,
2287 const struct wpabuf *dev_pw, u16 dev_pw_id,
2288 int p2p_group, const u8 *peer_pubkey_hash,
2289 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002290{
2291 struct wps_context *wps = wpa_s->wps;
2292 char pw[32 * 2 + 1];
2293
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002294 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2295 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2296 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2297 }
2298
Dmitry Shmidt04949592012-07-19 12:16:46 -07002299 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002300 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2301 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2302 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002303 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002304 }
2305
2306 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2307 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2308 "cannot start NFC-triggered connection", dev_pw_id);
2309 return -1;
2310 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002311
2312 dh5_free(wps->dh_ctx);
2313 wpabuf_free(wps->dh_pubkey);
2314 wpabuf_free(wps->dh_privkey);
2315 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2316 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2317 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2318 wps->dh_ctx = NULL;
2319 wpabuf_free(wps->dh_pubkey);
2320 wps->dh_pubkey = NULL;
2321 wpabuf_free(wps->dh_privkey);
2322 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002323 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002324 return -1;
2325 }
2326 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002327 if (wps->dh_ctx == NULL) {
2328 wpabuf_free(wps->dh_pubkey);
2329 wps->dh_pubkey = NULL;
2330 wpabuf_free(wps->dh_privkey);
2331 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002332 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002333 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002334 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002335
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002336 if (dev_pw) {
2337 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2338 wpabuf_head(dev_pw),
2339 wpabuf_len(dev_pw));
2340 }
2341 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2342 dev_pw ? pw : NULL,
2343 p2p_group, dev_pw_id, peer_pubkey_hash,
2344 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002345}
2346
2347
2348static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2349 struct wps_parse_attr *attr)
2350{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002351 /*
2352 * Disable existing networks temporarily to allow the newly learned
2353 * credential to be preferred. Enable the temporarily disabled networks
2354 * after 10 seconds.
2355 */
2356 wpas_wps_temp_disable(wpa_s, NULL);
2357 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2358 NULL);
2359
Dmitry Shmidt04949592012-07-19 12:16:46 -07002360 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2361 return -1;
2362
2363 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2364 return 0;
2365
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002366 if (attr->ap_channel) {
2367 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002368 int freq = 0;
2369
2370 if (chan >= 1 && chan <= 13)
2371 freq = 2407 + 5 * chan;
2372 else if (chan == 14)
2373 freq = 2484;
2374 else if (chan >= 30)
2375 freq = 5000 + 5 * chan;
2376
2377 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002378 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2379 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002380 wpa_s->after_wps = 5;
2381 wpa_s->wps_freq = freq;
2382 }
2383 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002384
2385 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2386 "based on the received credential added");
2387 wpa_s->normal_scans = 0;
2388 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002389 wpa_s->disconnected = 0;
2390 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002391
2392 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002393 wpa_supplicant_req_scan(wpa_s, 0, 0);
2394
2395 return 0;
2396}
2397
2398
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002399#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002400static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2401 struct wps_parse_attr *attr)
2402{
2403 return wps_registrar_add_nfc_password_token(
2404 wpa_s->wps->registrar, attr->oob_dev_password,
2405 attr->oob_dev_password_len);
2406}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002407#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002408
2409
2410static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2411 const struct wpabuf *wps)
2412{
2413 struct wps_parse_attr attr;
2414
2415 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2416
2417 if (wps_parse_msg(wps, &attr)) {
2418 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2419 return -1;
2420 }
2421
2422 if (attr.num_cred)
2423 return wpas_wps_use_cred(wpa_s, &attr);
2424
2425#ifdef CONFIG_WPS_ER
2426 if (attr.oob_dev_password)
2427 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2428#endif /* CONFIG_WPS_ER */
2429
2430 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2431 return -1;
2432}
2433
2434
2435int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002436 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002437{
2438 const struct wpabuf *wps = data;
2439 struct wpabuf *tmp = NULL;
2440 int ret;
2441
2442 if (wpabuf_len(data) < 4)
2443 return -1;
2444
2445 if (*wpabuf_head_u8(data) != 0x10) {
2446 /* Assume this contains full NDEF record */
2447 tmp = ndef_parse_wifi(data);
2448 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002449#ifdef CONFIG_P2P
2450 tmp = ndef_parse_p2p(data);
2451 if (tmp) {
2452 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2453 forced_freq);
2454 wpabuf_free(tmp);
2455 return ret;
2456 }
2457#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002458 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2459 return -1;
2460 }
2461 wps = tmp;
2462 }
2463
2464 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2465 wpabuf_free(tmp);
2466 return ret;
2467}
2468
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002469
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002470struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2471 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002472{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002473 struct wpabuf *ret;
2474
2475 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2476 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2477 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2478 return NULL;
2479
2480 ret = wps_build_nfc_handover_req(wpa_s->wps,
2481 wpa_s->conf->wps_nfc_dh_pubkey);
2482
2483 if (ndef && ret) {
2484 struct wpabuf *tmp;
2485 tmp = ndef_build_wifi(ret);
2486 wpabuf_free(ret);
2487 if (tmp == NULL)
2488 return NULL;
2489 ret = tmp;
2490 }
2491
2492 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002493}
2494
2495
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002496#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002497
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002498static struct wpabuf *
2499wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2500 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002501{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002502#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002503 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002504 u8 u[UUID_LEN], *use_uuid = NULL;
2505 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002506 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002507
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002508 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002509 return NULL;
2510
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002511 if (uuid == NULL)
2512 return NULL;
2513 if (uuid_str2bin(uuid, u) == 0)
2514 use_uuid = u;
2515 else if (hwaddr_aton(uuid, addr) == 0)
2516 use_addr = addr;
2517 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002518 return NULL;
2519
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002520 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2521 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2522 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2523 return NULL;
2524 }
2525
2526 wpas_wps_nfc_clear(wps);
2527 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2528 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2529 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2530 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2531 wpas_wps_nfc_clear(wps);
2532 return NULL;
2533 }
2534
2535 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2536 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002537 if (ndef && ret) {
2538 struct wpabuf *tmp;
2539 tmp = ndef_build_wifi(ret);
2540 wpabuf_free(ret);
2541 if (tmp == NULL)
2542 return NULL;
2543 ret = tmp;
2544 }
2545
2546 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002547#else /* CONFIG_WPS_ER */
2548 return NULL;
2549#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002550}
2551#endif /* CONFIG_WPS_NFC */
2552
2553
2554struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2555 int ndef, int cr, const char *uuid)
2556{
2557 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002558 if (!cr)
2559 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002560 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2561 if (ret)
2562 return ret;
2563 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002564}
2565
2566
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002567static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2568 const struct wpabuf *data)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002569{
2570 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002571 int ret = -1;
2572 u16 wsc_len;
2573 const u8 *pos;
2574 struct wpabuf msg;
2575 struct wps_parse_attr attr;
2576 u16 dev_pw_id;
2577 const u8 *bssid = NULL;
2578 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002579
2580 wps = ndef_parse_wifi(data);
2581 if (wps == NULL)
2582 return -1;
2583 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2584 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002585 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2586 if (wpabuf_len(wps) < 2) {
2587 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2588 "Message");
2589 goto out;
2590 }
2591 pos = wpabuf_head(wps);
2592 wsc_len = WPA_GET_BE16(pos);
2593 if (wsc_len > wpabuf_len(wps) - 2) {
2594 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2595 "in Wi-Fi Handover Select Message", wsc_len);
2596 goto out;
2597 }
2598 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002599
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002600 wpa_hexdump(MSG_DEBUG,
2601 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2602 pos, wsc_len);
2603 if (wsc_len < wpabuf_len(wps) - 2) {
2604 wpa_hexdump(MSG_DEBUG,
2605 "WPS: Ignore extra data after WSC attributes",
2606 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2607 }
2608
2609 wpabuf_set(&msg, pos, wsc_len);
2610 ret = wps_parse_msg(&msg, &attr);
2611 if (ret < 0) {
2612 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2613 "Wi-Fi Handover Select Message");
2614 goto out;
2615 }
2616
2617 if (attr.oob_dev_password == NULL ||
2618 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2619 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2620 "included in Wi-Fi Handover Select Message");
2621 ret = -1;
2622 goto out;
2623 }
2624
2625 if (attr.ssid == NULL) {
2626 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2627 "Select Message");
2628 ret = -1;
2629 goto out;
2630 }
2631
2632 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2633
2634 if (attr.mac_addr) {
2635 bssid = attr.mac_addr;
2636 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2637 MAC2STR(bssid));
2638 }
2639
2640 if (attr.rf_bands)
2641 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2642
2643 if (attr.ap_channel) {
2644 u16 chan = WPA_GET_BE16(attr.ap_channel);
2645
2646 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2647
2648 if (chan >= 1 && chan <= 13 &&
2649 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2650 freq = 2407 + 5 * chan;
2651 else if (chan == 14 &&
2652 (attr.rf_bands == NULL ||
2653 *attr.rf_bands & WPS_RF_24GHZ))
2654 freq = 2484;
2655 else if (chan >= 30 &&
2656 (attr.rf_bands == NULL ||
2657 *attr.rf_bands & WPS_RF_50GHZ))
2658 freq = 5000 + 5 * chan;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002659 else if (chan >= 1 && chan <= 4 &&
2660 (attr.rf_bands == NULL ||
2661 *attr.rf_bands & WPS_RF_60GHZ))
2662 freq = 56160 + 2160 * chan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002663
2664 if (freq) {
2665 wpa_printf(MSG_DEBUG,
2666 "WPS: AP indicated channel %u -> %u MHz",
2667 chan, freq);
2668 }
2669 }
2670
2671 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2672 attr.oob_dev_password, attr.oob_dev_password_len);
2673 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2674 WPS_OOB_PUBKEY_HASH_LEN);
2675 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2676 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2677 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2678 ret = -1;
2679 goto out;
2680 }
2681 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2682 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2683
2684 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2685 attr.oob_dev_password,
2686 attr.ssid, attr.ssid_len, freq);
2687
2688out:
2689 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002690 return ret;
2691}
2692
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002693
2694int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2695 const struct wpabuf *req,
2696 const struct wpabuf *sel)
2697{
2698 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2699 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2700 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2701 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2702}
2703
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002704
2705int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2706 const struct wpabuf *req,
2707 const struct wpabuf *sel)
2708{
2709 struct wpabuf *wps;
2710 int ret = -1;
2711 u16 wsc_len;
2712 const u8 *pos;
2713 struct wpabuf msg;
2714 struct wps_parse_attr attr;
2715 u16 dev_pw_id;
2716
2717 /*
2718 * Enrollee/station is always initiator of the NFC connection handover,
2719 * so use the request message here to find Enrollee public key hash.
2720 */
2721 wps = ndef_parse_wifi(req);
2722 if (wps == NULL)
2723 return -1;
2724 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2725 "payload from NFC connection handover");
2726 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2727 if (wpabuf_len(wps) < 2) {
2728 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2729 "Message");
2730 goto out;
2731 }
2732 pos = wpabuf_head(wps);
2733 wsc_len = WPA_GET_BE16(pos);
2734 if (wsc_len > wpabuf_len(wps) - 2) {
2735 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2736 "in rt Wi-Fi Handover Request Message", wsc_len);
2737 goto out;
2738 }
2739 pos += 2;
2740
2741 wpa_hexdump(MSG_DEBUG,
2742 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2743 pos, wsc_len);
2744 if (wsc_len < wpabuf_len(wps) - 2) {
2745 wpa_hexdump(MSG_DEBUG,
2746 "WPS: Ignore extra data after WSC attributes",
2747 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2748 }
2749
2750 wpabuf_set(&msg, pos, wsc_len);
2751 ret = wps_parse_msg(&msg, &attr);
2752 if (ret < 0) {
2753 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2754 "Wi-Fi Handover Request Message");
2755 goto out;
2756 }
2757
2758 if (attr.oob_dev_password == NULL ||
2759 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2760 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2761 "included in Wi-Fi Handover Request Message");
2762 ret = -1;
2763 goto out;
2764 }
2765
2766 if (attr.uuid_e == NULL) {
2767 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2768 "Handover Request Message");
2769 ret = -1;
2770 goto out;
2771 }
2772
2773 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2774
2775 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2776 attr.oob_dev_password, attr.oob_dev_password_len);
2777 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2778 WPS_OOB_PUBKEY_HASH_LEN);
2779 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2780 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2781 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2782 ret = -1;
2783 goto out;
2784 }
2785 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2786 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2787
2788 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2789 attr.oob_dev_password,
2790 DEV_PW_NFC_CONNECTION_HANDOVER,
2791 NULL, 0, 1);
2792
2793out:
2794 wpabuf_free(wps);
2795 return ret;
2796}
2797
Dmitry Shmidt04949592012-07-19 12:16:46 -07002798#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002799
2800
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002801static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2802{
2803 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002804 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002805
2806 if (wpa_debug_level > MSG_DEBUG)
2807 return;
2808
2809 if (wpa_s->wps_ap == NULL)
2810 return;
2811
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002812 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002813
2814 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2815 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2816 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2817
2818 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2819 "tries=%d last_attempt=%d sec ago blacklist=%d",
2820 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2821 ap->last_attempt.sec > 0 ?
2822 (int) now.sec - (int) ap->last_attempt.sec : -1,
2823 e ? e->count : 0);
2824 }
2825}
2826
2827
2828static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2829 const u8 *bssid)
2830{
2831 size_t i;
2832
2833 if (wpa_s->wps_ap == NULL)
2834 return NULL;
2835
2836 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2837 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2838 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2839 return ap;
2840 }
2841
2842 return NULL;
2843}
2844
2845
2846static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2847 struct wpa_scan_res *res)
2848{
2849 struct wpabuf *wps;
2850 enum wps_ap_info_type type;
2851 struct wps_ap_info *ap;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002852 int r, pbc_active;
2853 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002854
2855 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2856 return;
2857
2858 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2859 if (wps == NULL)
2860 return;
2861
2862 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2863 if (r == 2)
2864 type = WPS_AP_SEL_REG_OUR;
2865 else if (r == 1)
2866 type = WPS_AP_SEL_REG;
2867 else
2868 type = WPS_AP_NOT_SEL_REG;
2869
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002870 uuid = wps_get_uuid_e(wps);
2871 pbc_active = wps_is_selected_pbc_registrar(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002872
2873 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2874 if (ap) {
2875 if (ap->type != type) {
2876 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2877 " changed type %d -> %d",
2878 MAC2STR(res->bssid), ap->type, type);
2879 ap->type = type;
2880 if (type != WPS_AP_NOT_SEL_REG)
2881 wpa_blacklist_del(wpa_s, ap->bssid);
2882 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002883 ap->pbc_active = pbc_active;
2884 if (uuid)
2885 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2886 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002887 }
2888
2889 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2890 sizeof(struct wps_ap_info));
2891 if (ap == NULL)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002892 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893
2894 wpa_s->wps_ap = ap;
2895 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2896 wpa_s->num_wps_ap++;
2897
2898 os_memset(ap, 0, sizeof(*ap));
2899 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2900 ap->type = type;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002901 ap->pbc_active = pbc_active;
2902 if (uuid)
2903 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002904 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2905 MAC2STR(ap->bssid), ap->type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002906
2907out:
2908 wpabuf_free(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002909}
2910
2911
2912void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2913 struct wpa_scan_results *scan_res)
2914{
2915 size_t i;
2916
2917 for (i = 0; i < scan_res->num; i++)
2918 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2919
2920 wpas_wps_dump_ap_info(wpa_s);
2921}
2922
2923
2924void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2925{
2926 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002927
2928 wpa_s->after_wps = 0;
2929
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002930 if (!wpa_s->wps_ap_iter)
2931 return;
2932 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2933 if (ap == NULL)
2934 return;
2935 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002936 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002937}