]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wps_supplicant.c
offchannel: Use wpas_get_tx_interface() src parameter more consistently
[thirdparty/hostap.git] / wpa_supplicant / wps_supplicant.c
CommitLineData
fa201b69
JM
1/*
2 * wpa_supplicant / WPS integration
95fb2db2 3 * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
fa201b69 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
fa201b69
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
03da66bd
JM
12#include "eloop.h"
13#include "uuid.h"
3f2c8ba6 14#include "crypto/random.h"
03da66bd 15#include "crypto/dh_group5.h"
90973fb2
JM
16#include "common/ieee802_11_defs.h"
17#include "common/ieee802_11_common.h"
18#include "common/wpa_common.h"
03da66bd
JM
19#include "common/wpa_ctrl.h"
20#include "eap_common/eap_wsc_common.h"
b01c18a8 21#include "eap_peer/eap.h"
20a0b03d 22#include "eapol_supp/eapol_supp_sm.h"
03da66bd 23#include "rsn_supp/wpa.h"
d7645d23 24#include "wps/wps_attr_parse.h"
03da66bd 25#include "config.h"
fa201b69 26#include "wpa_supplicant_i.h"
2d5b792d 27#include "driver_i.h"
8bac466b 28#include "notify.h"
a6099152 29#include "blacklist.h"
59f2caa9 30#include "bss.h"
9ba9fa07 31#include "scan.h"
2f9929ff 32#include "ap.h"
e9a7ae41 33#include "p2p/p2p.h"
b22128ef 34#include "p2p_supplicant.h"
fa201b69
JM
35#include "wps_supplicant.h"
36
f7e54365 37
31fcea93 38#ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
a6099152 39#define WPS_PIN_SCAN_IGNORE_SEL_REG 3
31fcea93 40#endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
fa201b69 41
fcc60db4 42static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
469fc3a4 43static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
fcc60db4
JM
44
45
f9f0526b
JM
46static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
47{
48 os_free(wpa_s->wps_ap);
49 wpa_s->wps_ap = NULL;
50 wpa_s->num_wps_ap = 0;
51 wpa_s->wps_ap_iter = 0;
52}
53
54
150fd0b2
JM
55static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
56{
57 struct wpa_supplicant *wpa_s = eloop_ctx;
58 int use_fast_assoc = timeout_ctx != NULL;
59
60 wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
61 if (!use_fast_assoc ||
62 wpa_supplicant_fast_associate(wpa_s) != 1)
63 wpa_supplicant_req_scan(wpa_s, 0, 0);
64}
65
66
67static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
68{
69 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
70 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
71}
72
73
fa201b69
JM
74int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
75{
95fb2db2
JM
76 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
77 return 1;
95fb2db2 78
a6099152
JM
79 if (!wpa_s->wps_success &&
80 wpa_s->current_ssid &&
81 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
82 const u8 *bssid = wpa_s->bssid;
83 if (is_zero_ether_addr(bssid))
84 bssid = wpa_s->pending_bssid;
85
86 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
87 " did not succeed - continue trying to find "
88 "suitable AP", MAC2STR(bssid));
89 wpa_blacklist_add(wpa_s, bssid);
90
91 wpa_supplicant_deauthenticate(wpa_s,
92 WLAN_REASON_DEAUTH_LEAVING);
93 wpa_s->reassociate = 1;
94 wpa_supplicant_req_scan(wpa_s,
95 wpa_s->blacklist_cleared ? 5 : 0, 0);
96 wpa_s->blacklist_cleared = 0;
97 return 1;
98 }
99
f9f0526b 100 wpas_wps_clear_ap_info(wpa_s);
fcc60db4 101 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
449adbac
CWY
102 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
103 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
fcc60db4 104
fa201b69
JM
105 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
106 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
f981eabc 107 int disabled = wpa_s->current_ssid->disabled;
a565c23b 108 unsigned int freq = wpa_s->assoc_freq;
97236cee
MH
109 struct wpa_bss *bss;
110 struct wpa_ssid *ssid = NULL;
111 int use_fast_assoc = 0;
112
fa201b69 113 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
a565c23b
JM
114 "try to associate with the received credential "
115 "(freq=%u)", freq);
fa201b69
JM
116 wpa_supplicant_deauthenticate(wpa_s,
117 WLAN_REASON_DEAUTH_LEAVING);
f981eabc
JM
118 if (disabled) {
119 wpa_printf(MSG_DEBUG, "WPS: Current network is "
120 "disabled - wait for user to enable");
121 return 1;
122 }
17a4734d 123 wpa_s->after_wps = 5;
a565c23b 124 wpa_s->wps_freq = freq;
0b7a25c0 125 wpa_s->normal_scans = 0;
fa201b69 126 wpa_s->reassociate = 1;
97236cee
MH
127
128 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
129 "without a new scan can be used");
130 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
131 if (bss) {
132 struct wpabuf *wps;
133 struct wps_parse_attr attr;
134
135 wps = wpa_bss_get_vendor_ie_multi(bss,
136 WPS_IE_VENDOR_TYPE);
137 if (wps && wps_parse_msg(wps, &attr) == 0 &&
138 attr.wps_state &&
139 *attr.wps_state == WPS_STATE_CONFIGURED)
140 use_fast_assoc = 1;
141 wpabuf_free(wps);
142 }
143
150fd0b2
JM
144 /*
145 * Complete the next step from an eloop timeout to allow pending
146 * driver events related to the disconnection to be processed
147 * first. This makes it less likely for disconnection event to
148 * cause problems with the following connection.
149 */
150 wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
151 wpas_wps_assoc_with_cred_cancel(wpa_s);
152 eloop_register_timeout(0, 10000,
153 wpas_wps_assoc_with_cred, wpa_s,
154 use_fast_assoc ? (void *) 1 :
155 (void *) 0);
fa201b69
JM
156 return 1;
157 }
158
47662164
JM
159 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
160 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
161 "for external credential processing");
162 wpas_clear_wps(wpa_s);
163 wpa_supplicant_deauthenticate(wpa_s,
164 WLAN_REASON_DEAUTH_LEAVING);
165 return 1;
166 }
167
fa201b69
JM
168 return 0;
169}
170
171
7cc1b6c9
JM
172static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
173 struct wpa_ssid *ssid,
174 const struct wps_credential *cred)
175{
176 struct wpa_driver_capa capa;
59f2caa9 177 struct wpa_bss *bss;
7cc1b6c9
JM
178 const u8 *ie;
179 struct wpa_ie_data adv;
180 int wpa2 = 0, ccmp = 0;
181
182 /*
183 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
184 * case they are configured for mixed mode operation (WPA+WPA2 and
185 * TKIP+CCMP). Try to use scan results to figure out whether the AP
186 * actually supports stronger security and select that if the client
187 * has support for it, too.
188 */
189
190 if (wpa_drv_get_capa(wpa_s, &capa))
191 return; /* Unknown what driver supports */
192
d87e90b6
JM
193 if (ssid->ssid == NULL)
194 return;
59f2caa9
JM
195 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
196 if (bss == NULL) {
197 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
198 "table - use credential as-is");
7cc1b6c9
JM
199 return;
200 }
201
59f2caa9
JM
202 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
203
204 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
7cc1b6c9
JM
205 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
206 wpa2 = 1;
207 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
208 ccmp = 1;
209 } else {
59f2caa9 210 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
7cc1b6c9
JM
211 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
212 adv.pairwise_cipher & WPA_CIPHER_CCMP)
213 ccmp = 1;
214 }
215
216 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
217 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
218 /*
219 * TODO: This could be the initial AP configuration and the
220 * Beacon contents could change shortly. Should request a new
221 * scan and delay addition of the network until the updated
222 * scan results are available.
223 */
224 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
225 "support - use credential as-is");
226 return;
227 }
228
229 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
230 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
231 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
232 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
233 "based on scan results");
234 if (wpa_s->conf->ap_scan == 1)
235 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
236 else
237 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
238 }
239
240 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
241 (ssid->proto & WPA_PROTO_WPA) &&
242 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
243 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
244 "based on scan results");
245 if (wpa_s->conf->ap_scan == 1)
246 ssid->proto |= WPA_PROTO_RSN;
247 else
248 ssid->proto = WPA_PROTO_RSN;
249 }
250}
251
252
741ed9fc
VK
253static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
254 struct wpa_ssid *new_ssid)
255{
256 struct wpa_ssid *ssid, *next;
257
258 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
259 ssid = next, next = ssid ? ssid->next : NULL) {
260 /*
261 * new_ssid has already been added to the list in
262 * wpas_wps_add_network(), so skip it.
263 */
264 if (ssid == new_ssid)
265 continue;
266
267 if (ssid->bssid_set || new_ssid->bssid_set) {
268 if (ssid->bssid_set != new_ssid->bssid_set)
269 continue;
270 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
271 0)
272 continue;
273 }
274
275 /* compare SSID */
276 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
277 continue;
278
279 if (ssid->ssid && new_ssid->ssid) {
280 if (os_memcmp(ssid->ssid, new_ssid->ssid,
281 ssid->ssid_len) != 0)
282 continue;
283 } else if (ssid->ssid || new_ssid->ssid)
284 continue;
285
286 /* compare security parameters */
287 if (ssid->auth_alg != new_ssid->auth_alg ||
288 ssid->key_mgmt != new_ssid->key_mgmt ||
d77419d2
AM
289 (ssid->group_cipher != new_ssid->group_cipher &&
290 !(ssid->group_cipher & new_ssid->group_cipher &
291 WPA_CIPHER_CCMP)))
741ed9fc
VK
292 continue;
293
e5a4b85b
HW
294 /*
295 * Some existing WPS APs will send two creds in case they are
296 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
297 * Try to merge these two creds if they are received in the same
298 * M8 message.
299 */
300 if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
301 wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
302 if (new_ssid->passphrase && ssid->passphrase &&
303 os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
304 0) {
305 wpa_printf(MSG_DEBUG,
306 "WPS: M8 Creds with different passphrase - do not merge");
307 continue;
308 }
309
310 if (new_ssid->psk_set &&
311 (!ssid->psk_set ||
312 os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
313 wpa_printf(MSG_DEBUG,
314 "WPS: M8 Creds with different PSK - do not merge");
315 continue;
316 }
317
318 if ((new_ssid->passphrase && !ssid->passphrase) ||
319 (!new_ssid->passphrase && ssid->passphrase)) {
320 wpa_printf(MSG_DEBUG,
321 "WPS: M8 Creds with different passphrase/PSK type - do not merge");
322 continue;
323 }
324
325 wpa_printf(MSG_DEBUG,
326 "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
327 new_ssid->proto |= ssid->proto;
328 new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
329 } else {
330 /*
331 * proto and pairwise_cipher difference matter for
332 * non-mixed-mode creds.
333 */
334 if (ssid->proto != new_ssid->proto ||
335 ssid->pairwise_cipher != new_ssid->pairwise_cipher)
336 continue;
337 }
338
741ed9fc
VK
339 /* Remove the duplicated older network entry. */
340 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
341 wpas_notify_network_removed(wpa_s, ssid);
4f9a5ea5
JM
342 if (wpa_s->current_ssid == ssid)
343 wpa_s->current_ssid = NULL;
741ed9fc
VK
344 wpa_config_remove_network(wpa_s->conf, ssid->id);
345 }
346}
347
348
bcbbc7af
JM
349static int wpa_supplicant_wps_cred(void *ctx,
350 const struct wps_credential *cred)
fa201b69
JM
351{
352 struct wpa_supplicant *wpa_s = ctx;
353 struct wpa_ssid *ssid = wpa_s->current_ssid;
49eba5f8 354 u16 auth_type;
2c4f80d5 355#ifdef CONFIG_WPS_REG_DISABLE_OPEN
f981eabc 356 int registrar = 0;
2c4f80d5 357#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
fa201b69 358
47662164
JM
359 if ((wpa_s->conf->wps_cred_processing == 1 ||
360 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
361 size_t blen = cred->cred_attr_len * 2 + 1;
362 char *buf = os_malloc(blen);
363 if (buf) {
364 wpa_snprintf_hex(buf, blen,
365 cred->cred_attr, cred->cred_attr_len);
366 wpa_msg(wpa_s, MSG_INFO, "%s%s",
367 WPS_EVENT_CRED_RECEIVED, buf);
368 os_free(buf);
369 }
8bac466b
JM
370
371 wpas_notify_wps_credential(wpa_s, cred);
47662164
JM
372 } else
373 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
fa201b69 374
eca6e0a9
JM
375 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
376 cred->cred_attr, cred->cred_attr_len);
377
47662164
JM
378 if (wpa_s->conf->wps_cred_processing == 1)
379 return 0;
380
73267b9c
JM
381 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
382 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
383 cred->auth_type);
384 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
385 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
386 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
387 cred->key, cred->key_len);
388 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
389 MAC2STR(cred->mac_addr));
390
49eba5f8
JM
391 auth_type = cred->auth_type;
392 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
393 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
394 "auth_type into WPA2PSK");
395 auth_type = WPS_AUTH_WPA2PSK;
396 }
397
398 if (auth_type != WPS_AUTH_OPEN &&
49eba5f8
JM
399 auth_type != WPS_AUTH_WPAPSK &&
400 auth_type != WPS_AUTH_WPA2PSK) {
f286077d 401 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
49eba5f8
JM
402 "unsupported authentication type 0x%x",
403 auth_type);
f286077d
JM
404 return 0;
405 }
406
b363121a
JM
407 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
408 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
409 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
410 "invalid Network Key length %lu",
411 (unsigned long) cred->key_len);
412 return -1;
413 }
414 }
415
fa201b69
JM
416 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
417 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
418 "on the received credential");
2c4f80d5 419#ifdef CONFIG_WPS_REG_DISABLE_OPEN
f981eabc
JM
420 if (ssid->eap.identity &&
421 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
422 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
423 WSC_ID_REGISTRAR_LEN) == 0)
424 registrar = 1;
2c4f80d5 425#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
fa201b69
JM
426 os_free(ssid->eap.identity);
427 ssid->eap.identity = NULL;
428 ssid->eap.identity_len = 0;
429 os_free(ssid->eap.phase1);
430 ssid->eap.phase1 = NULL;
431 os_free(ssid->eap.eap_methods);
432 ssid->eap.eap_methods = NULL;
cae2119c 433 if (!ssid->p2p_group) {
2ff99b3c 434 ssid->temporary = 0;
cae2119c
JM
435 ssid->bssid_set = 0;
436 }
00e5e3d5
JM
437 ssid->disabled_until.sec = 0;
438 ssid->disabled_until.usec = 0;
439 ssid->auth_failures = 0;
fa201b69
JM
440 } else {
441 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
442 "received credential");
443 ssid = wpa_config_add_network(wpa_s->conf);
444 if (ssid == NULL)
445 return -1;
ded4f944
JM
446 if (wpa_s->current_ssid) {
447 /*
448 * Should the GO issue multiple credentials for some
449 * reason, each credential should be marked as a
450 * temporary P2P group similarly to the one that gets
451 * marked as such based on the pre-configured values
452 * used for the WPS network block.
453 */
454 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
455 ssid->temporary = wpa_s->current_ssid->temporary;
456 }
8bac466b 457 wpas_notify_network_added(wpa_s, ssid);
fa201b69
JM
458 }
459
460 wpa_config_set_network_defaults(ssid);
e5a4b85b 461 ssid->wps_run = wpa_s->wps_run;
fa201b69
JM
462
463 os_free(ssid->ssid);
464 ssid->ssid = os_malloc(cred->ssid_len);
465 if (ssid->ssid) {
466 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
467 ssid->ssid_len = cred->ssid_len;
468 }
469
470 switch (cred->encr_type) {
471 case WPS_ENCR_NONE:
fa201b69 472 break;
fa201b69
JM
473 case WPS_ENCR_TKIP:
474 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
fa201b69
JM
475 break;
476 case WPS_ENCR_AES:
477 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
d77419d2
AM
478 if (wpa_s->drv_capa_known &&
479 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
480 ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
481 ssid->group_cipher |= WPA_CIPHER_GCMP;
482 }
fa201b69
JM
483 break;
484 }
485
49eba5f8 486 switch (auth_type) {
fa201b69
JM
487 case WPS_AUTH_OPEN:
488 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
489 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
490 ssid->proto = 0;
f981eabc
JM
491#ifdef CONFIG_WPS_REG_DISABLE_OPEN
492 if (registrar) {
493 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
494 "id=%d - Credentials for an open "
495 "network disabled by default - use "
496 "'select_network %d' to enable",
497 ssid->id, ssid->id);
498 ssid->disabled = 1;
499 }
500#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
fa201b69 501 break;
fa201b69
JM
502 case WPS_AUTH_WPAPSK:
503 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
504 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
505 ssid->proto = WPA_PROTO_WPA;
506 break;
fa201b69
JM
507 case WPS_AUTH_WPA2PSK:
508 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
509 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
510 ssid->proto = WPA_PROTO_RSN;
511 break;
512 }
513
514 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
515 if (cred->key_len == 2 * PMK_LEN) {
516 if (hexstr2bin((const char *) cred->key, ssid->psk,
517 PMK_LEN)) {
518 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
519 "Key");
520 return -1;
521 }
522 ssid->psk_set = 1;
d1c8ac88 523 ssid->export_keys = 1;
fa201b69
JM
524 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
525 os_free(ssid->passphrase);
526 ssid->passphrase = os_malloc(cred->key_len + 1);
527 if (ssid->passphrase == NULL)
528 return -1;
529 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
530 ssid->passphrase[cred->key_len] = '\0';
531 wpa_config_update_psk(ssid);
d1c8ac88 532 ssid->export_keys = 1;
fa201b69
JM
533 } else {
534 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
535 "length %lu",
536 (unsigned long) cred->key_len);
537 return -1;
538 }
539 }
540
7cc1b6c9
JM
541 wpas_wps_security_workaround(wpa_s, ssid, cred);
542
741ed9fc
VK
543 wpas_wps_remove_dup_network(wpa_s, ssid);
544
fa201b69
JM
545#ifndef CONFIG_NO_CONFIG_WRITE
546 if (wpa_s->conf->update_config &&
547 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
548 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
549 return -1;
550 }
551#endif /* CONFIG_NO_CONFIG_WRITE */
552
6fbcbc07
SD
553 /*
554 * Optimize the post-WPS scan based on the channel used during
555 * the provisioning in case EAP-Failure is not received.
556 */
557 wpa_s->after_wps = 5;
558 wpa_s->wps_freq = wpa_s->assoc_freq;
559
fa201b69
JM
560 return 0;
561}
562
563
4b68290e
JM
564static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
565 struct wps_event_m2d *m2d)
566{
567 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
568 "dev_password_id=%d config_error=%d",
569 m2d->dev_password_id, m2d->config_error);
8bac466b 570 wpas_notify_wps_event_m2d(wpa_s, m2d);
b89883a4
JM
571#ifdef CONFIG_P2P
572 if (wpa_s->parent && wpa_s->parent != wpa_s) {
573 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
574 "dev_password_id=%d config_error=%d",
575 m2d->dev_password_id, m2d->config_error);
576 }
577 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
578 /*
579 * Notify P2P from eloop timeout to avoid issues with the
580 * interface getting removed while processing a message.
581 */
ace0fbdb 582 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
b89883a4
JM
583 NULL);
584 }
585#endif /* CONFIG_P2P */
4b68290e
JM
586}
587
588
2e145e91
JM
589static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
590{
591 struct wpa_supplicant *wpa_s = eloop_ctx;
592 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
593 wpas_clear_wps(wpa_s);
594}
595
596
469fc3a4
JM
597static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
598 struct wps_event_fail *fail)
599{
c5cf0a18
CWY
600 if (fail->error_indication > 0 &&
601 fail->error_indication < NUM_WPS_EI_VALUES) {
602 wpa_msg(wpa_s, MSG_INFO,
603 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
604 fail->msg, fail->config_error, fail->error_indication,
961750c1 605 wps_ei_str(fail->error_indication));
c5cf0a18
CWY
606 if (wpa_s->parent && wpa_s->parent != wpa_s)
607 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
608 "msg=%d config_error=%d reason=%d (%s)",
609 fail->msg, fail->config_error,
610 fail->error_indication,
961750c1 611 wps_ei_str(fail->error_indication));
c5cf0a18
CWY
612 } else {
613 wpa_msg(wpa_s, MSG_INFO,
614 WPS_EVENT_FAIL "msg=%d config_error=%d",
545cc6af 615 fail->msg, fail->config_error);
c5cf0a18
CWY
616 if (wpa_s->parent && wpa_s->parent != wpa_s)
617 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
618 "msg=%d config_error=%d",
619 fail->msg, fail->config_error);
620 }
2e145e91
JM
621
622 /*
623 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
624 */
625 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
626 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
627 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
628
8bac466b 629 wpas_notify_wps_event_fail(wpa_s, fail);
3734552f 630 wpas_p2p_wps_failed(wpa_s, fail);
469fc3a4
JM
631}
632
633
ec947ffc
SD
634static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
635
636static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
637{
638 struct wpa_ssid *ssid;
318e772a 639 int changed = 0;
ec947ffc
SD
640
641 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
642
643 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
644 if (ssid->disabled_for_connect && ssid->disabled) {
645 ssid->disabled_for_connect = 0;
646 ssid->disabled = 0;
647 wpas_notify_network_enabled_changed(wpa_s, ssid);
318e772a 648 changed++;
ec947ffc
SD
649 }
650 }
318e772a
JM
651
652 if (changed) {
653#ifndef CONFIG_NO_CONFIG_WRITE
654 if (wpa_s->conf->update_config &&
655 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
656 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
657 "configuration");
658 }
659#endif /* CONFIG_NO_CONFIG_WRITE */
660 }
ec947ffc
SD
661}
662
663
664static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
665{
666 struct wpa_supplicant *wpa_s = eloop_ctx;
667 /* Enable the networks disabled during wpas_wps_reassoc */
668 wpas_wps_reenable_networks(wpa_s);
669}
670
671
ad5302a1
JM
672static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
673{
674 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
a6099152 675 wpa_s->wps_success = 1;
8bac466b 676 wpas_notify_wps_event_success(wpa_s);
f3e907a7
JM
677 if (wpa_s->current_ssid)
678 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
679 wpa_s->extra_blacklist_count = 0;
ec947ffc
SD
680
681 /*
682 * Enable the networks disabled during wpas_wps_reassoc after 10
683 * seconds. The 10 seconds timer is to allow the data connection to be
684 * formed before allowing other networks to be selected.
685 */
686 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
687 NULL);
688
b22128ef 689 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
ad5302a1
JM
690}
691
692
b78bc3a3
JM
693static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
694 struct wps_event_er_ap *ap)
695{
696 char uuid_str[100];
ed45947e 697 char dev_type[WPS_DEV_TYPE_BUFSIZE];
e694b344 698
b78bc3a3 699 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
e694b344 700 if (ap->pri_dev_type)
ed45947e
JM
701 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
702 sizeof(dev_type));
e694b344
JM
703 else
704 dev_type[0] = '\0';
705
706 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
707 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
708 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
b78bc3a3
JM
709 ap->friendly_name ? ap->friendly_name : "",
710 ap->manufacturer ? ap->manufacturer : "",
711 ap->model_description ? ap->model_description : "",
712 ap->model_name ? ap->model_name : "",
713 ap->manufacturer_url ? ap->manufacturer_url : "",
714 ap->model_url ? ap->model_url : "");
715}
716
717
718static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
719 struct wps_event_er_ap *ap)
720{
721 char uuid_str[100];
722 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
723 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
724}
725
726
727static void wpa_supplicant_wps_event_er_enrollee_add(
728 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
729{
730 char uuid_str[100];
ed45947e 731 char dev_type[WPS_DEV_TYPE_BUFSIZE];
b78bc3a3
JM
732
733 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
734 if (enrollee->pri_dev_type)
ed45947e
JM
735 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
736 sizeof(dev_type));
b78bc3a3
JM
737 else
738 dev_type[0] = '\0';
739
740 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
741 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
742 "|%s|%s|%s|%s|%s|",
743 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
744 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
745 enrollee->dev_name ? enrollee->dev_name : "",
746 enrollee->manufacturer ? enrollee->manufacturer : "",
747 enrollee->model_name ? enrollee->model_name : "",
748 enrollee->model_number ? enrollee->model_number : "",
749 enrollee->serial_number ? enrollee->serial_number : "");
750}
751
752
753static void wpa_supplicant_wps_event_er_enrollee_remove(
754 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
755{
756 char uuid_str[100];
757 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
758 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
759 uuid_str, MAC2STR(enrollee->mac_addr));
760}
761
762
15dbf129
JM
763static void wpa_supplicant_wps_event_er_ap_settings(
764 struct wpa_supplicant *wpa_s,
765 struct wps_event_er_ap_settings *ap_settings)
766{
767 char uuid_str[100];
768 char key_str[65];
769 const struct wps_credential *cred = ap_settings->cred;
770
771 key_str[0] = '\0';
772 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
773 if (cred->key_len >= 8 && cred->key_len <= 64) {
774 os_memcpy(key_str, cred->key, cred->key_len);
775 key_str[cred->key_len] = '\0';
776 }
777 }
778
779 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
780 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
781 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
782 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
783 "key=%s",
784 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
785 cred->auth_type, cred->encr_type, key_str);
786}
787
788
3e7533b3
JM
789static void wpa_supplicant_wps_event_er_set_sel_reg(
790 struct wpa_supplicant *wpa_s,
791 struct wps_event_er_set_selected_registrar *ev)
792{
793 char uuid_str[100];
794
795 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
796 switch (ev->state) {
797 case WPS_ER_SET_SEL_REG_START:
798 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
799 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
800 "sel_reg_config_methods=0x%x",
801 uuid_str, ev->sel_reg, ev->dev_passwd_id,
802 ev->sel_reg_config_methods);
803 break;
804 case WPS_ER_SET_SEL_REG_DONE:
805 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
806 "uuid=%s state=DONE", uuid_str);
807 break;
808 case WPS_ER_SET_SEL_REG_FAILED:
809 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
810 "uuid=%s state=FAILED", uuid_str);
811 break;
812 }
813}
814
815
4b68290e
JM
816static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
817 union wps_event_data *data)
818{
819 struct wpa_supplicant *wpa_s = ctx;
820 switch (event) {
821 case WPS_EV_M2D:
822 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
823 break;
469fc3a4
JM
824 case WPS_EV_FAIL:
825 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
826 break;
ad5302a1
JM
827 case WPS_EV_SUCCESS:
828 wpa_supplicant_wps_event_success(wpa_s);
829 break;
3b2cf800 830 case WPS_EV_PWD_AUTH_FAIL:
70d84f11
JM
831#ifdef CONFIG_AP
832 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
833 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
834#endif /* CONFIG_AP */
3b2cf800 835 break;
63330c68
OK
836 case WPS_EV_PBC_OVERLAP:
837 break;
838 case WPS_EV_PBC_TIMEOUT:
839 break;
50396e29
JM
840 case WPS_EV_PBC_ACTIVE:
841 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
842 break;
843 case WPS_EV_PBC_DISABLE:
844 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
845 break;
b78bc3a3
JM
846 case WPS_EV_ER_AP_ADD:
847 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
848 break;
849 case WPS_EV_ER_AP_REMOVE:
850 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
851 break;
852 case WPS_EV_ER_ENROLLEE_ADD:
853 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
854 &data->enrollee);
855 break;
856 case WPS_EV_ER_ENROLLEE_REMOVE:
857 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
858 &data->enrollee);
859 break;
15dbf129
JM
860 case WPS_EV_ER_AP_SETTINGS:
861 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
862 &data->ap_settings);
863 break;
3e7533b3
JM
864 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
865 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
866 &data->set_sel_reg);
867 break;
32cdcf15
JM
868 case WPS_EV_AP_PIN_SUCCESS:
869 break;
4b68290e
JM
870 }
871}
872
873
239abaf2
DS
874static int wpa_supplicant_wps_rf_band(void *ctx)
875{
876 struct wpa_supplicant *wpa_s = ctx;
877
878 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
879 return 0;
880
881 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
882}
883
884
f90c86d4 885enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
fa201b69 886{
b01c18a8
JM
887 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
888 eap_is_wps_pin_enrollee(&ssid->eap))
889 return WPS_REQ_ENROLLEE;
890 else
891 return WPS_REQ_REGISTRAR;
fa201b69 892}
116654ce
JM
893
894
fcc60db4
JM
895static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
896{
897 int id;
20a0b03d
JM
898 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
899
7255983b 900 wpa_s->after_wps = 0;
4d9fb08d 901 wpa_s->known_wps_freq = 0;
7255983b 902
20a0b03d 903 prev_current = wpa_s->current_ssid;
fcc60db4 904
ec947ffc
SD
905 /* Enable the networks disabled during wpas_wps_reassoc */
906 wpas_wps_reenable_networks(wpa_s);
907
fcc60db4 908 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
5890fa81 909 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
fcc60db4
JM
910
911 /* Remove any existing WPS network from configuration */
912 ssid = wpa_s->conf->ssid;
913 while (ssid) {
f0477201 914 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
8bac466b 915 if (ssid == wpa_s->current_ssid) {
c7a67a77
JZ
916 wpa_supplicant_deauthenticate(
917 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
8bac466b 918 }
fcc60db4 919 id = ssid->id;
8bac466b 920 remove_ssid = ssid;
f0477201 921 } else
fcc60db4
JM
922 id = -1;
923 ssid = ssid->next;
8bac466b 924 if (id >= 0) {
20a0b03d
JM
925 if (prev_current == remove_ssid) {
926 wpa_sm_set_config(wpa_s->wpa, NULL);
927 eapol_sm_notify_config(wpa_s->eapol, NULL,
928 NULL);
929 }
8bac466b 930 wpas_notify_network_removed(wpa_s, remove_ssid);
fcc60db4 931 wpa_config_remove_network(wpa_s->conf, id);
8bac466b 932 }
fcc60db4 933 }
f9f0526b
JM
934
935 wpas_wps_clear_ap_info(wpa_s);
fcc60db4
JM
936}
937
938
939static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
940{
941 struct wpa_supplicant *wpa_s = eloop_ctx;
014732ea
AC
942 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
943 "out");
fcc60db4
JM
944 wpas_clear_wps(wpa_s);
945}
946
947
948static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
23318bea
JM
949 int registrar, const u8 *dev_addr,
950 const u8 *bssid)
fcc60db4
JM
951{
952 struct wpa_ssid *ssid;
953
954 ssid = wpa_config_add_network(wpa_s->conf);
955 if (ssid == NULL)
956 return NULL;
8bac466b 957 wpas_notify_network_added(wpa_s, ssid);
fcc60db4 958 wpa_config_set_network_defaults(ssid);
2ff99b3c 959 ssid->temporary = 1;
fcc60db4
JM
960 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
961 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
962 wpa_config_set(ssid, "identity", registrar ?
963 "\"" WSC_ID_REGISTRAR "\"" :
964 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
8bac466b 965 wpas_notify_network_removed(wpa_s, ssid);
fcc60db4
JM
966 wpa_config_remove_network(wpa_s->conf, ssid->id);
967 return NULL;
968 }
969
23318bea
JM
970#ifdef CONFIG_P2P
971 if (dev_addr)
972 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
973#endif /* CONFIG_P2P */
974
fcc60db4 975 if (bssid) {
b2c5a4a3 976#ifndef CONFIG_P2P
59f2caa9 977 struct wpa_bss *bss;
f7e54365 978 int count = 0;
b2c5a4a3 979#endif /* CONFIG_P2P */
fcc60db4
JM
980
981 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
24c23d1b 982 ssid->bssid_set = 1;
fcc60db4 983
b2c5a4a3
JM
984 /*
985 * Note: With P2P, the SSID may change at the time the WPS
986 * provisioning is started, so better not filter the AP based
987 * on the current SSID in the scan results.
988 */
989#ifndef CONFIG_P2P
59f2caa9
JM
990 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
991 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
fcc60db4
JM
992 continue;
993
fcc60db4 994 os_free(ssid->ssid);
59f2caa9 995 ssid->ssid = os_malloc(bss->ssid_len);
fcc60db4
JM
996 if (ssid->ssid == NULL)
997 break;
59f2caa9
JM
998 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
999 ssid->ssid_len = bss->ssid_len;
f7e54365
JM
1000 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1001 "scan results",
1002 ssid->ssid, ssid->ssid_len);
1003 count++;
1004 }
1005
1006 if (count > 1) {
1007 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1008 "for the AP; use wildcard");
1009 os_free(ssid->ssid);
1010 ssid->ssid = NULL;
1011 ssid->ssid_len = 0;
fcc60db4 1012 }
b2c5a4a3 1013#endif /* CONFIG_P2P */
fcc60db4
JM
1014 }
1015
1016 return ssid;
1017}
1018
1019
170f5663
JM
1020static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1021 struct wpa_ssid *selected)
fcc60db4
JM
1022{
1023 struct wpa_ssid *ssid;
1024
d9d87c33
JM
1025 if (wpa_s->current_ssid)
1026 wpa_supplicant_deauthenticate(
1027 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1028
fcc60db4
JM
1029 /* Mark all other networks disabled and trigger reassociation */
1030 ssid = wpa_s->conf->ssid;
1031 while (ssid) {
8bac466b 1032 int was_disabled = ssid->disabled;
ec947ffc 1033 ssid->disabled_for_connect = 0;
c2762e41
JS
1034 /*
1035 * In case the network object corresponds to a persistent group
1036 * then do not send out network disabled signal. In addition,
1037 * do not change disabled status of persistent network objects
1038 * from 2 to 1 should we connect to another network.
1039 */
1040 if (was_disabled != 2) {
1041 ssid->disabled = ssid != selected;
ec947ffc
SD
1042 if (was_disabled != ssid->disabled) {
1043 if (ssid->disabled)
1044 ssid->disabled_for_connect = 1;
c2762e41
JS
1045 wpas_notify_network_enabled_changed(wpa_s,
1046 ssid);
ec947ffc 1047 }
c2762e41 1048 }
fcc60db4
JM
1049 ssid = ssid->next;
1050 }
170f5663
JM
1051}
1052
1053
1054static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
91a65018
JM
1055 struct wpa_ssid *selected, const u8 *bssid,
1056 int freq)
170f5663
JM
1057{
1058 struct wpa_bss *bss;
1059
e5a4b85b
HW
1060 wpa_s->wps_run++;
1061 if (wpa_s->wps_run == 0)
1062 wpa_s->wps_run++;
170f5663
JM
1063 wpa_s->after_wps = 0;
1064 wpa_s->known_wps_freq = 0;
91a65018
JM
1065 if (freq) {
1066 wpa_s->after_wps = 5;
1067 wpa_s->wps_freq = freq;
1068 } else if (bssid) {
170f5663
JM
1069 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1070 if (bss && bss->freq > 0) {
1071 wpa_s->known_wps_freq = 1;
1072 wpa_s->wps_freq = bss->freq;
1073 }
1074 }
1075
1076 wpas_wps_temp_disable(wpa_s, selected);
1077
fcc60db4
JM
1078 wpa_s->disconnected = 0;
1079 wpa_s->reassociate = 1;
a6099152 1080 wpa_s->scan_runs = 0;
0b7a25c0 1081 wpa_s->normal_scans = 0;
a6099152
JM
1082 wpa_s->wps_success = 0;
1083 wpa_s->blacklist_cleared = 0;
1cba9bea
JJ
1084
1085 wpa_supplicant_cancel_sched_scan(wpa_s);
fcc60db4
JM
1086 wpa_supplicant_req_scan(wpa_s, 0, 0);
1087}
1088
1089
9fa243b2
JM
1090int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1091 int p2p_group)
fcc60db4
JM
1092{
1093 struct wpa_ssid *ssid;
1094 wpas_clear_wps(wpa_s);
23318bea 1095 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
fcc60db4
JM
1096 if (ssid == NULL)
1097 return -1;
9fa243b2
JM
1098 ssid->temporary = 1;
1099 ssid->p2p_group = p2p_group;
812bf56a 1100#ifdef CONFIG_P2P
e9a7ae41
JM
1101 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1102 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1103 if (ssid->ssid) {
1104 ssid->ssid_len = wpa_s->go_params->ssid_len;
1105 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1106 ssid->ssid_len);
1107 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1108 "SSID", ssid->ssid, ssid->ssid_len);
1109 }
1110 }
812bf56a 1111#endif /* CONFIG_P2P */
9c75e9fb
JM
1112 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1113 return -1;
42f50264
JM
1114 if (wpa_s->wps_fragment_size)
1115 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
fcc60db4
JM
1116 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1117 wpa_s, NULL);
91a65018 1118 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
fcc60db4
JM
1119 return 0;
1120}
1121
1122
23318bea
JM
1123static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1124 const u8 *dev_addr, const u8 *bssid,
5f454557 1125 const char *pin, int p2p_group, u16 dev_pw_id,
57630e65 1126 const u8 *peer_pubkey_hash,
91a65018 1127 const u8 *ssid_val, size_t ssid_len, int freq)
fcc60db4
JM
1128{
1129 struct wpa_ssid *ssid;
5f454557 1130 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
fcc60db4 1131 unsigned int rpin = 0;
5f454557 1132 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
fcc60db4
JM
1133
1134 wpas_clear_wps(wpa_s);
23318bea
JM
1135 if (bssid && is_zero_ether_addr(bssid))
1136 bssid = NULL;
1137 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
57630e65
JM
1138 if (ssid == NULL) {
1139 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
fcc60db4 1140 return -1;
57630e65 1141 }
9fa243b2
JM
1142 ssid->temporary = 1;
1143 ssid->p2p_group = p2p_group;
57630e65
JM
1144 if (ssid_val) {
1145 ssid->ssid = os_malloc(ssid_len);
1146 if (ssid->ssid) {
1147 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1148 ssid->ssid_len = ssid_len;
1149 }
1150 }
5f454557
JM
1151 if (peer_pubkey_hash) {
1152 os_memcpy(hash, " pkhash=", 8);
1153 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1154 peer_pubkey_hash,
1155 WPS_OOB_PUBKEY_HASH_LEN);
1156 } else {
1157 hash[0] = '\0';
1158 }
812bf56a 1159#ifdef CONFIG_P2P
e9a7ae41
JM
1160 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1161 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1162 if (ssid->ssid) {
1163 ssid->ssid_len = wpa_s->go_params->ssid_len;
1164 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1165 ssid->ssid_len);
1166 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1167 "SSID", ssid->ssid, ssid->ssid_len);
1168 }
1169 }
812bf56a 1170#endif /* CONFIG_P2P */
fcc60db4 1171 if (pin)
5f454557
JM
1172 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1173 pin, dev_pw_id, hash);
fa4c2988
JM
1174 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1175 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1176 dev_pw_id, hash);
1177 } else {
fcc60db4 1178 rpin = wps_generate_pin();
5f454557
JM
1179 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1180 rpin, dev_pw_id, hash);
fcc60db4 1181 }
fa4c2988
JM
1182 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1183 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
9c75e9fb 1184 return -1;
fa4c2988 1185 }
42f50264
JM
1186 if (wpa_s->wps_fragment_size)
1187 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
fcc60db4
JM
1188 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1189 wpa_s, NULL);
f9f0526b 1190 wpa_s->wps_ap_iter = 1;
91a65018 1191 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
fcc60db4
JM
1192 return rpin;
1193}
1194
1195
5f454557
JM
1196int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1197 const char *pin, int p2p_group, u16 dev_pw_id)
1198{
23318bea 1199 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
91a65018 1200 dev_pw_id, NULL, NULL, 0, 0);
5f454557
JM
1201}
1202
1203
2f9929ff
AC
1204/* Cancel the wps pbc/pin requests */
1205int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1206{
1207#ifdef CONFIG_AP
1208 if (wpa_s->ap_iface) {
1209 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1210 return wpa_supplicant_ap_wps_cancel(wpa_s);
1211 }
1212#endif /* CONFIG_AP */
1213
476dea24
JM
1214 if (wpa_s->wpa_state == WPA_SCANNING ||
1215 wpa_s->wpa_state == WPA_DISCONNECTED) {
2f9929ff
AC
1216 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1217 wpa_supplicant_cancel_scan(wpa_s);
1218 wpas_clear_wps(wpa_s);
1219 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1220 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1221 "deauthenticate");
1222 wpa_supplicant_deauthenticate(wpa_s,
1223 WLAN_REASON_DEAUTH_LEAVING);
1224 wpas_clear_wps(wpa_s);
ec947ffc
SD
1225 } else {
1226 wpas_wps_reenable_networks(wpa_s);
f9f0526b 1227 wpas_wps_clear_ap_info(wpa_s);
5890fa81
JM
1228 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1229 0)
1230 wpas_clear_wps(wpa_s);
ec947ffc 1231 }
2f9929ff 1232
7255983b
JM
1233 wpa_s->after_wps = 0;
1234
2f9929ff
AC
1235 return 0;
1236}
1237
1238
fcc60db4 1239int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
52eb293d 1240 const char *pin, struct wps_new_ap_settings *settings)
fcc60db4
JM
1241{
1242 struct wpa_ssid *ssid;
52eb293d
JM
1243 char val[200];
1244 char *pos, *end;
1245 int res;
fcc60db4
JM
1246
1247 if (!pin)
1248 return -1;
1249 wpas_clear_wps(wpa_s);
23318bea 1250 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
fcc60db4
JM
1251 if (ssid == NULL)
1252 return -1;
2ff99b3c 1253 ssid->temporary = 1;
52eb293d
JM
1254 pos = val;
1255 end = pos + sizeof(val);
1256 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
d85e1fc8 1257 if (os_snprintf_error(end - pos, res))
52eb293d
JM
1258 return -1;
1259 pos += res;
1260 if (settings) {
1261 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1262 "new_encr=%s new_key=%s",
1263 settings->ssid_hex, settings->auth,
1264 settings->encr, settings->key_hex);
d85e1fc8 1265 if (os_snprintf_error(end - pos, res))
52eb293d
JM
1266 return -1;
1267 pos += res;
1268 }
1269 res = os_snprintf(pos, end - pos, "\"");
d85e1fc8 1270 if (os_snprintf_error(end - pos, res))
52eb293d 1271 return -1;
9c75e9fb
JM
1272 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1273 return -1;
42f50264
JM
1274 if (wpa_s->wps_fragment_size)
1275 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
fcc60db4
JM
1276 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1277 wpa_s, NULL);
91a65018 1278 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
fcc60db4
JM
1279 return 0;
1280}
1281
1282
52177fbb
JM
1283static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1284 const u8 *p2p_dev_addr, const u8 *psk,
c5adf528
JM
1285 size_t psk_len)
1286{
52177fbb
JM
1287 if (is_zero_ether_addr(p2p_dev_addr)) {
1288 wpa_printf(MSG_DEBUG,
1289 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1290 MAC2STR(mac_addr));
1291 } else {
1292 wpa_printf(MSG_DEBUG,
1293 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1294 " P2P Device Addr " MACSTR,
1295 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1296 }
c5adf528
JM
1297 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1298
1299 /* TODO */
1300
1301 return 0;
1302}
1303
1304
1305static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1306 const struct wps_device_data *dev)
1307{
1308 char uuid[40], txt[400];
1309 int len;
96750ea5 1310 char devtype[WPS_DEV_TYPE_BUFSIZE];
c5adf528
JM
1311 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1312 return;
1313 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1314 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
96750ea5 1315 " [%s|%s|%s|%s|%s|%s]",
c5adf528
JM
1316 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1317 dev->manufacturer, dev->model_name,
1318 dev->model_number, dev->serial_number,
96750ea5
JM
1319 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1320 sizeof(devtype)));
a80ba67a 1321 if (!os_snprintf_error(sizeof(txt), len))
c5adf528
JM
1322 wpa_printf(MSG_INFO, "%s", txt);
1323}
1324
1325
72df2f5f
JM
1326static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1327 u16 sel_reg_config_methods)
1328{
1329#ifdef CONFIG_WPS_ER
1330 struct wpa_supplicant *wpa_s = ctx;
1331
1332 if (wpa_s->wps_er == NULL)
1333 return;
ad474118
JM
1334 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1335 "dev_password_id=%u sel_reg_config_methods=0x%x",
1336 sel_reg, dev_passwd_id, sel_reg_config_methods);
72df2f5f
JM
1337 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1338 sel_reg_config_methods);
1339#endif /* CONFIG_WPS_ER */
1340}
1341
1342
092acb54
JM
1343static u16 wps_fix_config_methods(u16 config_methods)
1344{
092acb54
JM
1345 if ((config_methods &
1346 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1347 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1348 wpa_printf(MSG_INFO, "WPS: Converting display to "
1349 "virtual_display for WPS 2.0 compliance");
1350 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1351 }
1352 if ((config_methods &
1353 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1354 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1355 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1356 "virtual_push_button for WPS 2.0 compliance");
1357 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1358 }
092acb54
JM
1359
1360 return config_methods;
1361}
1362
1363
85a821d6
JM
1364static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1365 struct wps_context *wps)
1366{
ef93abde
JM
1367 char buf[50];
1368 const char *src;
1369
85a821d6
JM
1370 if (is_nil_uuid(wpa_s->conf->uuid)) {
1371 struct wpa_supplicant *first;
1372 first = wpa_s->global->ifaces;
1373 while (first && first->next)
1374 first = first->next;
1375 if (first && first != wpa_s) {
157cdad5
BG
1376 if (wps != wpa_s->global->ifaces->wps)
1377 os_memcpy(wps->uuid,
1378 wpa_s->global->ifaces->wps->uuid,
1379 WPS_UUID_LEN);
ef93abde 1380 src = "from the first interface";
85a821d6
JM
1381 } else {
1382 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
ef93abde 1383 src = "based on MAC address";
85a821d6
JM
1384 }
1385 } else {
1386 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
ef93abde 1387 src = "based on configuration";
85a821d6 1388 }
ef93abde
JM
1389
1390 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1391 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
85a821d6
JM
1392}
1393
1394
71dd3b78
AS
1395static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1396 struct wps_context *wps)
1397{
1398 wpabuf_free(wps->dev.vendor_ext_m1);
1399 wps->dev.vendor_ext_m1 = NULL;
1400
1401 if (wpa_s->conf->wps_vendor_ext_m1) {
1402 wps->dev.vendor_ext_m1 =
1403 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1404 if (!wps->dev.vendor_ext_m1) {
1405 wpa_printf(MSG_ERROR, "WPS: Cannot "
1406 "allocate memory for vendor_ext_m1");
1407 }
1408 }
1409}
1410
1411
116654ce
JM
1412int wpas_wps_init(struct wpa_supplicant *wpa_s)
1413{
1414 struct wps_context *wps;
c5adf528 1415 struct wps_registrar_config rcfg;
6fb6d842 1416 struct hostapd_hw_modes *modes;
6bf731e8 1417 u16 m;
116654ce
JM
1418
1419 wps = os_zalloc(sizeof(*wps));
1420 if (wps == NULL)
1421 return -1;
1422
1423 wps->cred_cb = wpa_supplicant_wps_cred;
4b68290e 1424 wps->event_cb = wpa_supplicant_wps_event;
239abaf2 1425 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
116654ce
JM
1426 wps->cb_ctx = wpa_s;
1427
3c0b7aa4
JM
1428 wps->dev.device_name = wpa_s->conf->device_name;
1429 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1430 wps->dev.model_name = wpa_s->conf->model_name;
1431 wps->dev.model_number = wpa_s->conf->model_number;
1432 wps->dev.serial_number = wpa_s->conf->serial_number;
c0e4dd9e
JM
1433 wps->config_methods =
1434 wps_config_methods_str2bin(wpa_s->conf->config_methods);
979b988e
JM
1435 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1436 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1437 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1438 "methods are not allowed at the same time");
1439 os_free(wps);
1440 return -1;
1441 }
092acb54 1442 wps->config_methods = wps_fix_config_methods(wps->config_methods);
a9355fac 1443 wps->dev.config_methods = wps->config_methods;
2f646b6e
JB
1444 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1445 WPS_DEV_TYPE_LEN);
a9e86bfb 1446
2f646b6e
JB
1447 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1448 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1449 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
a9e86bfb 1450
71dd3b78
AS
1451 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1452
3c0b7aa4 1453 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
6bf731e8 1454 modes = wpa_s->hw.modes;
6fb6d842 1455 if (modes) {
6bf731e8 1456 for (m = 0; m < wpa_s->hw.num_modes; m++) {
6fb6d842
BC
1457 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1458 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1459 wps->dev.rf_bands |= WPS_RF_24GHZ;
1460 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1461 wps->dev.rf_bands |= WPS_RF_50GHZ;
1462 }
6fb6d842
BC
1463 }
1464 if (wps->dev.rf_bands == 0) {
1465 /*
1466 * Default to claiming support for both bands if the driver
1467 * does not provide support for fetching supported bands.
1468 */
1469 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1470 }
398cfbf6 1471 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
85a821d6 1472 wpas_wps_set_uuid(wpa_s, wps);
116654ce 1473
c5adf528
JM
1474 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1475 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1476
1477 os_memset(&rcfg, 0, sizeof(rcfg));
1478 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1479 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
72df2f5f 1480 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
c5adf528
JM
1481 rcfg.cb_ctx = wpa_s;
1482
1483 wps->registrar = wps_registrar_init(wps, &rcfg);
1484 if (wps->registrar == NULL) {
1485 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1486 os_free(wps);
1487 return -1;
1488 }
1489
116654ce
JM
1490 wpa_s->wps = wps;
1491
1492 return 0;
1493}
1494
1495
50d1f890
JM
1496#ifdef CONFIG_WPS_ER
1497static void wpas_wps_nfc_clear(struct wps_context *wps)
1498{
1499 wps->ap_nfc_dev_pw_id = 0;
1500 wpabuf_free(wps->ap_nfc_dh_pubkey);
1501 wps->ap_nfc_dh_pubkey = NULL;
1502 wpabuf_free(wps->ap_nfc_dh_privkey);
1503 wps->ap_nfc_dh_privkey = NULL;
1504 wpabuf_free(wps->ap_nfc_dev_pw);
1505 wps->ap_nfc_dev_pw = NULL;
1506}
1507#endif /* CONFIG_WPS_ER */
1508
1509
116654ce
JM
1510void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1511{
150fd0b2 1512 wpas_wps_assoc_with_cred_cancel(wpa_s);
fcc60db4 1513 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
2e145e91 1514 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
ec947ffc 1515 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
f9f0526b 1516 wpas_wps_clear_ap_info(wpa_s);
fcc60db4 1517
fb2ac53d
AC
1518#ifdef CONFIG_P2P
1519 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1520#endif /* CONFIG_P2P */
1521
116654ce
JM
1522 if (wpa_s->wps == NULL)
1523 return;
1524
a6b63078 1525#ifdef CONFIG_WPS_ER
1a1bf008 1526 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
a6b63078 1527 wpa_s->wps_er = NULL;
50d1f890 1528 wpas_wps_nfc_clear(wpa_s->wps);
a6b63078
JM
1529#endif /* CONFIG_WPS_ER */
1530
c5adf528 1531 wps_registrar_deinit(wpa_s->wps->registrar);
46bdb83a
MH
1532 wpabuf_free(wpa_s->wps->dh_pubkey);
1533 wpabuf_free(wpa_s->wps->dh_privkey);
71dd3b78 1534 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
116654ce
JM
1535 os_free(wpa_s->wps->network_key);
1536 os_free(wpa_s->wps);
1537 wpa_s->wps = NULL;
1538}
351f09a2
JM
1539
1540
a6099152 1541int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
620c7837 1542 struct wpa_ssid *ssid, struct wpa_bss *bss)
351f09a2
JM
1543{
1544 struct wpabuf *wps_ie;
1545
1546 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1547 return -1;
1548
620c7837 1549 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
351f09a2
JM
1550 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1551 if (!wps_ie) {
1552 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1553 return 0;
1554 }
1555
1556 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1557 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1558 "without active PBC Registrar");
1559 wpabuf_free(wps_ie);
1560 return 0;
1561 }
1562
1563 /* TODO: overlap detection */
1564 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1565 "(Active PBC)");
1566 wpabuf_free(wps_ie);
1567 return 1;
1568 }
1569
1570 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1571 if (!wps_ie) {
1572 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1573 return 0;
1574 }
1575
a6099152 1576 /*
31fcea93
JM
1577 * Start with WPS APs that advertise our address as an
1578 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1579 * allow any WPS AP after couple of scans since some APs do not
1580 * set Selected Registrar attribute properly when using
1581 * external Registrar.
a6099152 1582 */
31fcea93 1583 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
a6099152
JM
1584 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1585 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1586 "without active PIN Registrar");
1587 wpabuf_free(wps_ie);
1588 return 0;
1589 }
1590 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1591 } else {
1592 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
31fcea93 1593 "(Authorized MAC or Active PIN)");
351f09a2 1594 }
351f09a2
JM
1595 wpabuf_free(wps_ie);
1596 return 1;
1597 }
1598
1599 if (wps_ie) {
1600 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1601 wpabuf_free(wps_ie);
1602 return 1;
1603 }
1604
1605 return -1;
1606}
1607
1608
a6099152
JM
1609int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1610 struct wpa_ssid *ssid,
620c7837 1611 struct wpa_bss *bss)
351f09a2
JM
1612{
1613 struct wpabuf *wps_ie = NULL;
1614 int ret = 0;
1615
1616 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
620c7837 1617 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
351f09a2
JM
1618 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1619 /* allow wildcard SSID for WPS PBC */
1620 ret = 1;
1621 }
1622 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
620c7837 1623 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
a6099152 1624 if (wps_ie &&
31fcea93 1625 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
a6099152 1626 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
351f09a2
JM
1627 /* allow wildcard SSID for WPS PIN */
1628 ret = 1;
1629 }
1630 }
1631
24c23d1b
JM
1632 if (!ret && ssid->bssid_set &&
1633 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1634 /* allow wildcard SSID due to hardcoded BSSID match */
1635 ret = 1;
1636 }
1637
54f489be
JM
1638#ifdef CONFIG_WPS_STRICT
1639 if (wps_ie) {
1640 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
ff28ccaf 1641 0, bss->bssid) < 0)
54f489be
JM
1642 ret = 0;
1643 if (bss->beacon_ie_len) {
1644 struct wpabuf *bcn_wps;
620c7837 1645 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
54f489be
JM
1646 bss, WPS_IE_VENDOR_TYPE);
1647 if (bcn_wps == NULL) {
1648 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1649 "missing from AP Beacon");
1650 ret = 0;
1651 } else {
1652 if (wps_validate_beacon(wps_ie) < 0)
1653 ret = 0;
1654 wpabuf_free(bcn_wps);
1655 }
1656 }
1657 }
1658#endif /* CONFIG_WPS_STRICT */
1659
351f09a2
JM
1660 wpabuf_free(wps_ie);
1661
1662 return ret;
1663}
1664
1665
1666int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
6fa81a3b 1667 struct wpa_bss *selected, struct wpa_ssid *ssid)
351f09a2
JM
1668{
1669 const u8 *sel_uuid, *uuid;
351f09a2
JM
1670 struct wpabuf *wps_ie;
1671 int ret = 0;
6fa81a3b 1672 struct wpa_bss *bss;
351f09a2
JM
1673
1674 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1675 return 0;
1676
9647120b
JM
1677 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1678 "present in scan results; selected BSSID " MACSTR,
1679 MAC2STR(selected->bssid));
1680
351f09a2 1681 /* Make sure that only one AP is in active PBC mode */
6fa81a3b 1682 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
9647120b 1683 if (wps_ie) {
351f09a2 1684 sel_uuid = wps_get_uuid_e(wps_ie);
9647120b
JM
1685 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1686 sel_uuid, UUID_LEN);
1687 } else {
1688 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1689 "WPS IE?!");
351f09a2 1690 sel_uuid = NULL;
9647120b 1691 }
351f09a2 1692
6fa81a3b 1693 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
351f09a2
JM
1694 struct wpabuf *ie;
1695 if (bss == selected)
1696 continue;
6fa81a3b 1697 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
351f09a2
JM
1698 if (!ie)
1699 continue;
1700 if (!wps_is_selected_pbc_registrar(ie)) {
1701 wpabuf_free(ie);
1702 continue;
1703 }
9647120b
JM
1704 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1705 MACSTR, MAC2STR(bss->bssid));
351f09a2 1706 uuid = wps_get_uuid_e(ie);
9647120b
JM
1707 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1708 uuid, UUID_LEN);
44cd430f 1709 if (sel_uuid == NULL || uuid == NULL ||
9647120b 1710 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
351f09a2 1711 ret = 1; /* PBC overlap */
9647120b
JM
1712 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1713 MACSTR " and " MACSTR,
1714 MAC2STR(selected->bssid),
1715 MAC2STR(bss->bssid));
351f09a2
JM
1716 wpabuf_free(ie);
1717 break;
1718 }
1719
1720 /* TODO: verify that this is reasonable dual-band situation */
484957ae
JM
1721
1722 wpabuf_free(ie);
351f09a2
JM
1723 }
1724
1725 wpabuf_free(wps_ie);
1726
1727 return ret;
1728}
a524f05e
JM
1729
1730
1731void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1732{
6fa81a3b 1733 struct wpa_bss *bss;
6c6ad81f 1734 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
a524f05e
JM
1735
1736 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1737 return;
1738
6fa81a3b 1739 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
a524f05e 1740 struct wpabuf *ie;
6fa81a3b 1741 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
a524f05e
JM
1742 if (!ie)
1743 continue;
1744 if (wps_is_selected_pbc_registrar(ie))
6c6ad81f 1745 pbc++;
31fcea93 1746 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
6c6ad81f 1747 auth++;
a524f05e 1748 else if (wps_is_selected_pin_registrar(ie))
6c6ad81f 1749 pin++;
a524f05e 1750 else
6c6ad81f 1751 wps++;
a524f05e 1752 wpabuf_free(ie);
a524f05e 1753 }
6c6ad81f
JM
1754
1755 if (pbc)
1756 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1757 else if (auth)
1758 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1759 else if (pin)
1760 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1761 else if (wps)
1762 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
a524f05e 1763}
24c23d1b
JM
1764
1765
1766int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1767{
1768 struct wpa_ssid *ssid;
1769
1770 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1771 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1772 return 1;
1773 }
1774
1775 return 0;
1776}
611ed491
JM
1777
1778
1779int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1780 char *end)
1781{
1782 struct wpabuf *wps_ie;
1783 int ret;
1784
1785 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1786 if (wps_ie == NULL)
1787 return 0;
1788
1789 ret = wps_attr_text(wps_ie, buf, end);
1790 wpabuf_free(wps_ie);
1791 return ret;
1792}
e9bcfebf
JM
1793
1794
08486685 1795int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
e9bcfebf
JM
1796{
1797#ifdef CONFIG_WPS_ER
1798 if (wpa_s->wps_er) {
b3f371ca 1799 wps_er_refresh(wpa_s->wps_er);
e9bcfebf
JM
1800 return 0;
1801 }
08486685 1802 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
e9bcfebf
JM
1803 if (wpa_s->wps_er == NULL)
1804 return -1;
1805 return 0;
1806#else /* CONFIG_WPS_ER */
1807 return 0;
1808#endif /* CONFIG_WPS_ER */
1809}
1810
1811
f77cedc1 1812void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
e9bcfebf
JM
1813{
1814#ifdef CONFIG_WPS_ER
1a1bf008 1815 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
e9bcfebf
JM
1816 wpa_s->wps_er = NULL;
1817#endif /* CONFIG_WPS_ER */
e9bcfebf 1818}
72df2f5f
JM
1819
1820
1821#ifdef CONFIG_WPS_ER
31fcea93
JM
1822int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1823 const char *uuid, const char *pin)
72df2f5f
JM
1824{
1825 u8 u[UUID_LEN];
e205401c
JM
1826 const u8 *use_uuid = NULL;
1827 u8 addr_buf[ETH_ALEN];
72df2f5f 1828
e205401c
JM
1829 if (os_strcmp(uuid, "any") == 0) {
1830 } else if (uuid_str2bin(uuid, u) == 0) {
1831 use_uuid = u;
1832 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1833 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1834 if (use_uuid == NULL)
1835 return -1;
1836 } else
72df2f5f 1837 return -1;
31fcea93 1838 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
e205401c 1839 use_uuid,
72df2f5f
JM
1840 (const u8 *) pin, os_strlen(pin), 300);
1841}
564cd7fa
JM
1842
1843
1844int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1845{
e205401c
JM
1846 u8 u[UUID_LEN], *use_uuid = NULL;
1847 u8 addr[ETH_ALEN], *use_addr = NULL;
564cd7fa 1848
e205401c
JM
1849 if (uuid_str2bin(uuid, u) == 0)
1850 use_uuid = u;
1851 else if (hwaddr_aton(uuid, addr) == 0)
1852 use_addr = addr;
1853 else
564cd7fa 1854 return -1;
e205401c 1855 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
564cd7fa 1856}
e64dcfd5
JM
1857
1858
1859int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1860 const char *pin)
1861{
59307b30
JM
1862 u8 u[UUID_LEN], *use_uuid = NULL;
1863 u8 addr[ETH_ALEN], *use_addr = NULL;
e64dcfd5 1864
59307b30
JM
1865 if (uuid_str2bin(uuid, u) == 0)
1866 use_uuid = u;
1867 else if (hwaddr_aton(uuid, addr) == 0)
1868 use_addr = addr;
1869 else
e64dcfd5 1870 return -1;
59307b30
JM
1871
1872 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
e64dcfd5
JM
1873 os_strlen(pin));
1874}
1a1bf008
JM
1875
1876
88c8bf31
JM
1877static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1878 struct wps_credential *cred)
1879{
1880 os_memset(cred, 0, sizeof(*cred));
1881 if (ssid->ssid_len > 32)
1882 return -1;
1883 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1884 cred->ssid_len = ssid->ssid_len;
1885 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1886 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1887 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1888 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1889 cred->encr_type = WPS_ENCR_AES;
1890 else
1891 cred->encr_type = WPS_ENCR_TKIP;
1892 if (ssid->passphrase) {
1893 cred->key_len = os_strlen(ssid->passphrase);
1894 if (cred->key_len >= 64)
1895 return -1;
1896 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1897 } else if (ssid->psk_set) {
1898 cred->key_len = 32;
1899 os_memcpy(cred->key, ssid->psk, 32);
1900 } else
1901 return -1;
1902 } else {
1903 cred->auth_type = WPS_AUTH_OPEN;
1904 cred->encr_type = WPS_ENCR_NONE;
1905 }
1906
1907 return 0;
1908}
1909
1910
ef10f473
JM
1911int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1912 int id)
1913{
59307b30
JM
1914 u8 u[UUID_LEN], *use_uuid = NULL;
1915 u8 addr[ETH_ALEN], *use_addr = NULL;
ef10f473
JM
1916 struct wpa_ssid *ssid;
1917 struct wps_credential cred;
1918
59307b30
JM
1919 if (uuid_str2bin(uuid, u) == 0)
1920 use_uuid = u;
1921 else if (hwaddr_aton(uuid, addr) == 0)
1922 use_addr = addr;
1923 else
ef10f473
JM
1924 return -1;
1925 ssid = wpa_config_get_network(wpa_s->conf, id);
1926 if (ssid == NULL || ssid->ssid == NULL)
1927 return -1;
1928
88c8bf31 1929 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
ef10f473 1930 return -1;
59307b30 1931 return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
ef10f473
JM
1932}
1933
1934
7d6640a6
JM
1935int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1936 const char *pin, struct wps_new_ap_settings *settings)
1937{
59307b30
JM
1938 u8 u[UUID_LEN], *use_uuid = NULL;
1939 u8 addr[ETH_ALEN], *use_addr = NULL;
7d6640a6
JM
1940 struct wps_credential cred;
1941 size_t len;
1942
59307b30
JM
1943 if (uuid_str2bin(uuid, u) == 0)
1944 use_uuid = u;
1945 else if (hwaddr_aton(uuid, addr) == 0)
1946 use_addr = addr;
1947 else
7d6640a6
JM
1948 return -1;
1949 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1950 settings->encr == NULL || settings->key_hex == NULL)
1951 return -1;
1952
1953 os_memset(&cred, 0, sizeof(cred));
1954 len = os_strlen(settings->ssid_hex);
1955 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1956 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1957 return -1;
1958 cred.ssid_len = len / 2;
1959
1960 len = os_strlen(settings->key_hex);
1961 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1962 hexstr2bin(settings->key_hex, cred.key, len / 2))
1963 return -1;
1964 cred.key_len = len / 2;
1965
1966 if (os_strcmp(settings->auth, "OPEN") == 0)
1967 cred.auth_type = WPS_AUTH_OPEN;
1968 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1969 cred.auth_type = WPS_AUTH_WPAPSK;
1970 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1971 cred.auth_type = WPS_AUTH_WPA2PSK;
1972 else
1973 return -1;
1974
1975 if (os_strcmp(settings->encr, "NONE") == 0)
1976 cred.encr_type = WPS_ENCR_NONE;
dc390043 1977#ifdef CONFIG_TESTING_OPTIONS
7d6640a6
JM
1978 else if (os_strcmp(settings->encr, "WEP") == 0)
1979 cred.encr_type = WPS_ENCR_WEP;
dc390043 1980#endif /* CONFIG_TESTING_OPTIONS */
7d6640a6
JM
1981 else if (os_strcmp(settings->encr, "TKIP") == 0)
1982 cred.encr_type = WPS_ENCR_TKIP;
1983 else if (os_strcmp(settings->encr, "CCMP") == 0)
1984 cred.encr_type = WPS_ENCR_AES;
1985 else
1986 return -1;
1987
59307b30
JM
1988 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1989 (const u8 *) pin, os_strlen(pin), &cred);
7d6640a6
JM
1990}
1991
1992
1cea09a9
JM
1993#ifdef CONFIG_WPS_NFC
1994struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1995 int ndef, const char *uuid)
1996{
1997 struct wpabuf *ret;
59307b30
JM
1998 u8 u[UUID_LEN], *use_uuid = NULL;
1999 u8 addr[ETH_ALEN], *use_addr = NULL;
1cea09a9
JM
2000
2001 if (!wpa_s->wps_er)
2002 return NULL;
2003
59307b30
JM
2004 if (uuid_str2bin(uuid, u) == 0)
2005 use_uuid = u;
2006 else if (hwaddr_aton(uuid, addr) == 0)
2007 use_addr = addr;
2008 else
1cea09a9
JM
2009 return NULL;
2010
59307b30 2011 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
1cea09a9
JM
2012 if (ndef && ret) {
2013 struct wpabuf *tmp;
2014 tmp = ndef_build_wifi(ret);
2015 wpabuf_free(ret);
2016 if (tmp == NULL)
2017 return NULL;
2018 ret = tmp;
2019 }
2020
2021 return ret;
2022}
2023#endif /* CONFIG_WPS_NFC */
2024
2025
4c9695be
BG
2026static int callbacks_pending = 0;
2027
1a1bf008
JM
2028static void wpas_wps_terminate_cb(void *ctx)
2029{
2030 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
4c9695be
BG
2031 if (--callbacks_pending <= 0)
2032 eloop_terminate();
1a1bf008
JM
2033}
2034#endif /* CONFIG_WPS_ER */
2035
2036
2037int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2038{
2039#ifdef CONFIG_WPS_ER
2040 if (wpa_s->wps_er) {
4c9695be 2041 callbacks_pending++;
1a1bf008
JM
2042 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2043 wpa_s->wps_er = NULL;
2044 return 1;
2045 }
72df2f5f 2046#endif /* CONFIG_WPS_ER */
1a1bf008
JM
2047 return 0;
2048}
41e650ae
JM
2049
2050
611aea7d
JM
2051void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2052{
2053 struct wps_context *wps = wpa_s->wps;
2054
2055 if (wps == NULL)
2056 return;
2057
2058 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2059 wps->config_methods = wps_config_methods_str2bin(
2060 wpa_s->conf->config_methods);
2061 if ((wps->config_methods &
2062 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2063 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2064 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2065 "config methods are not allowed at the "
2066 "same time");
2067 wps->config_methods &= ~WPS_CONFIG_LABEL;
2068 }
2069 }
092acb54 2070 wps->config_methods = wps_fix_config_methods(wps->config_methods);
c8b245b6 2071 wps->dev.config_methods = wps->config_methods;
611aea7d 2072
2f646b6e
JB
2073 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2074 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2075 WPS_DEV_TYPE_LEN);
611aea7d 2076
a9e86bfb 2077 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2f646b6e
JB
2078 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2079 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2080 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
a9e86bfb
JMB
2081 }
2082
71dd3b78
AS
2083 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2084 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2085
611aea7d
JM
2086 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2087 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2088
85a821d6
JM
2089 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2090 wpas_wps_set_uuid(wpa_s, wps);
dcf788d1 2091
1c9cb49f
JM
2092 if (wpa_s->conf->changed_parameters &
2093 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
dcf788d1
JM
2094 /* Update pointers to make sure they refer current values */
2095 wps->dev.device_name = wpa_s->conf->device_name;
2096 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2097 wps->dev.model_name = wpa_s->conf->model_name;
2098 wps->dev.model_number = wpa_s->conf->model_number;
2099 wps->dev.serial_number = wpa_s->conf->serial_number;
2100 }
611aea7d 2101}
3f2c8ba6
JM
2102
2103
2104#ifdef CONFIG_WPS_NFC
2105
88c8bf31
JM
2106#ifdef CONFIG_WPS_ER
2107static struct wpabuf *
2108wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2109 struct wpa_ssid *ssid)
2110{
2111 struct wpabuf *ret;
2112 struct wps_credential cred;
2113
2114 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2115 return NULL;
2116
2117 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2118
2119 if (ndef && ret) {
2120 struct wpabuf *tmp;
2121 tmp = ndef_build_wifi(ret);
2122 wpabuf_free(ret);
2123 if (tmp == NULL)
2124 return NULL;
2125 ret = tmp;
2126 }
2127
2128 return ret;
2129}
2130#endif /* CONFIG_WPS_ER */
2131
2132
bbf41865 2133struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
88c8bf31 2134 int ndef, const char *id_str)
bbf41865 2135{
88c8bf31
JM
2136#ifdef CONFIG_WPS_ER
2137 if (id_str) {
2138 int id;
2139 char *end = NULL;
2140 struct wpa_ssid *ssid;
2141
2142 id = strtol(id_str, &end, 10);
2143 if (end && *end)
2144 return NULL;
2145
2146 ssid = wpa_config_get_network(wpa_s->conf, id);
2147 if (ssid == NULL)
2148 return NULL;
2149 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2150 }
2151#endif /* CONFIG_WPS_ER */
bbf41865
JM
2152#ifdef CONFIG_AP
2153 if (wpa_s->ap_iface)
2154 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2155#endif /* CONFIG_AP */
2156 return NULL;
2157}
2158
2159
3f2c8ba6
JM
2160struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2161{
042ec551
JM
2162 if (wpa_s->conf->wps_nfc_pw_from_config) {
2163 return wps_nfc_token_build(ndef,
2164 wpa_s->conf->wps_nfc_dev_pw_id,
2165 wpa_s->conf->wps_nfc_dh_pubkey,
2166 wpa_s->conf->wps_nfc_dev_pw);
2167 }
2168
bfc62fe1
JM
2169 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2170 &wpa_s->conf->wps_nfc_dh_pubkey,
2171 &wpa_s->conf->wps_nfc_dh_privkey,
2172 &wpa_s->conf->wps_nfc_dev_pw);
3f2c8ba6
JM
2173}
2174
2175
23318bea
JM
2176int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2177 const u8 *bssid,
5f454557 2178 const struct wpabuf *dev_pw, u16 dev_pw_id,
57630e65 2179 int p2p_group, const u8 *peer_pubkey_hash,
91a65018 2180 const u8 *ssid, size_t ssid_len, int freq)
3f2c8ba6
JM
2181{
2182 struct wps_context *wps = wpa_s->wps;
2183 char pw[32 * 2 + 1];
2184
fa4c2988 2185 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
5f454557
JM
2186 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2187 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2188 }
2189
3f2c8ba6 2190 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
fa4c2988
JM
2191 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2192 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2193 "cannot start NFC-triggered connection");
2194 return -1;
2195 }
2196
2197 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2198 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2199 "cannot start NFC-triggered connection", dev_pw_id);
3f2c8ba6 2200 return -1;
fa4c2988 2201 }
3f2c8ba6
JM
2202
2203 dh5_free(wps->dh_ctx);
2204 wpabuf_free(wps->dh_pubkey);
2205 wpabuf_free(wps->dh_privkey);
2206 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2207 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2208 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2209 wps->dh_ctx = NULL;
2210 wpabuf_free(wps->dh_pubkey);
2211 wps->dh_pubkey = NULL;
2212 wpabuf_free(wps->dh_privkey);
2213 wps->dh_privkey = NULL;
fa4c2988 2214 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
3f2c8ba6
JM
2215 return -1;
2216 }
2217 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
5c9d63d4
JM
2218 if (wps->dh_ctx == NULL) {
2219 wpabuf_free(wps->dh_pubkey);
2220 wps->dh_pubkey = NULL;
2221 wpabuf_free(wps->dh_privkey);
2222 wps->dh_privkey = NULL;
fa4c2988 2223 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
3f2c8ba6 2224 return -1;
5c9d63d4 2225 }
3f2c8ba6 2226
fa4c2988
JM
2227 if (dev_pw) {
2228 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2229 wpabuf_head(dev_pw),
2230 wpabuf_len(dev_pw));
2231 }
23318bea
JM
2232 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2233 dev_pw ? pw : NULL,
fa4c2988 2234 p2p_group, dev_pw_id, peer_pubkey_hash,
91a65018 2235 ssid, ssid_len, freq);
3f2c8ba6
JM
2236}
2237
d7645d23
JM
2238
2239static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2240 struct wps_parse_attr *attr)
2241{
170f5663
JM
2242 /*
2243 * Disable existing networks temporarily to allow the newly learned
2244 * credential to be preferred. Enable the temporarily disabled networks
2245 * after 10 seconds.
2246 */
2247 wpas_wps_temp_disable(wpa_s, NULL);
2248 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2249 NULL);
2250
d7645d23
JM
2251 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2252 return -1;
2253
2254 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2255 return 0;
2256
0b5ff2ae
JM
2257 if (attr->ap_channel) {
2258 u16 chan = WPA_GET_BE16(attr->ap_channel);
bd3a3737
JM
2259 int freq = 0;
2260
2261 if (chan >= 1 && chan <= 13)
2262 freq = 2407 + 5 * chan;
2263 else if (chan == 14)
2264 freq = 2484;
2265 else if (chan >= 30)
2266 freq = 5000 + 5 * chan;
2267
2268 if (freq) {
0b5ff2ae
JM
2269 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2270 chan, freq);
bd3a3737
JM
2271 wpa_s->after_wps = 5;
2272 wpa_s->wps_freq = freq;
2273 }
2274 }
0b5ff2ae
JM
2275
2276 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2277 "based on the received credential added");
2278 wpa_s->normal_scans = 0;
2279 wpa_supplicant_reinit_autoscan(wpa_s);
d7645d23
JM
2280 wpa_s->disconnected = 0;
2281 wpa_s->reassociate = 1;
7e910b7b
JJ
2282
2283 wpa_supplicant_cancel_sched_scan(wpa_s);
d7645d23
JM
2284 wpa_supplicant_req_scan(wpa_s, 0, 0);
2285
2286 return 0;
2287}
2288
2289
6df865fa 2290#ifdef CONFIG_WPS_ER
e6ea2a45
JM
2291static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2292 struct wps_parse_attr *attr)
2293{
e6ea2a45
JM
2294 return wps_registrar_add_nfc_password_token(
2295 wpa_s->wps->registrar, attr->oob_dev_password,
2296 attr->oob_dev_password_len);
2297}
6df865fa 2298#endif /* CONFIG_WPS_ER */
e6ea2a45
JM
2299
2300
d7645d23
JM
2301static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2302 const struct wpabuf *wps)
2303{
2304 struct wps_parse_attr attr;
2305
2306 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2307
2308 if (wps_parse_msg(wps, &attr)) {
2309 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2310 return -1;
2311 }
2312
2313 if (attr.num_cred)
2314 return wpas_wps_use_cred(wpa_s, &attr);
2315
e6ea2a45
JM
2316#ifdef CONFIG_WPS_ER
2317 if (attr.oob_dev_password)
2318 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2319#endif /* CONFIG_WPS_ER */
2320
d7645d23
JM
2321 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2322 return -1;
2323}
2324
2325
2326int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
b56f6c88 2327 const struct wpabuf *data, int forced_freq)
d7645d23
JM
2328{
2329 const struct wpabuf *wps = data;
2330 struct wpabuf *tmp = NULL;
2331 int ret;
2332
2333 if (wpabuf_len(data) < 4)
2334 return -1;
2335
2336 if (*wpabuf_head_u8(data) != 0x10) {
2337 /* Assume this contains full NDEF record */
2338 tmp = ndef_parse_wifi(data);
2339 if (tmp == NULL) {
dd37a938
JM
2340#ifdef CONFIG_P2P
2341 tmp = ndef_parse_p2p(data);
2342 if (tmp) {
b56f6c88
JM
2343 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2344 forced_freq);
dd37a938
JM
2345 wpabuf_free(tmp);
2346 return ret;
2347 }
2348#endif /* CONFIG_P2P */
d7645d23
JM
2349 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2350 return -1;
2351 }
2352 wps = tmp;
2353 }
2354
2355 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2356 wpabuf_free(tmp);
2357 return ret;
2358}
2359
e65552dd 2360
41f9ffb6
JM
2361struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2362 int ndef)
e65552dd 2363{
41f9ffb6
JM
2364 struct wpabuf *ret;
2365
2366 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2367 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2368 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2369 return NULL;
2370
2371 ret = wps_build_nfc_handover_req(wpa_s->wps,
2372 wpa_s->conf->wps_nfc_dh_pubkey);
2373
2374 if (ndef && ret) {
2375 struct wpabuf *tmp;
2376 tmp = ndef_build_wifi(ret);
2377 wpabuf_free(ret);
2378 if (tmp == NULL)
2379 return NULL;
2380 ret = tmp;
2381 }
2382
2383 return ret;
e65552dd
JM
2384}
2385
2386
f3f2ba2e 2387#ifdef CONFIG_WPS_NFC
50d1f890 2388
c5838683
JM
2389static struct wpabuf *
2390wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2391 const char *uuid)
f3f2ba2e 2392{
94d7acf3 2393#ifdef CONFIG_WPS_ER
f3f2ba2e 2394 struct wpabuf *ret;
59307b30
JM
2395 u8 u[UUID_LEN], *use_uuid = NULL;
2396 u8 addr[ETH_ALEN], *use_addr = NULL;
d4b4d7fe 2397 struct wps_context *wps = wpa_s->wps;
f3f2ba2e 2398
d4b4d7fe 2399 if (wps == NULL)
f3f2ba2e
JM
2400 return NULL;
2401
59307b30
JM
2402 if (uuid == NULL)
2403 return NULL;
2404 if (uuid_str2bin(uuid, u) == 0)
2405 use_uuid = u;
2406 else if (hwaddr_aton(uuid, addr) == 0)
2407 use_addr = addr;
2408 else
f3f2ba2e
JM
2409 return NULL;
2410
50d1f890 2411 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
50d1f890
JM
2412 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2413 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2414 return NULL;
d4b4d7fe 2415 }
50d1f890 2416
d4b4d7fe
JM
2417 wpas_wps_nfc_clear(wps);
2418 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2419 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2420 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2421 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
50d1f890 2422 wpas_wps_nfc_clear(wps);
d4b4d7fe 2423 return NULL;
50d1f890
JM
2424 }
2425
2426 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2427 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
f3f2ba2e
JM
2428 if (ndef && ret) {
2429 struct wpabuf *tmp;
2430 tmp = ndef_build_wifi(ret);
2431 wpabuf_free(ret);
2432 if (tmp == NULL)
2433 return NULL;
2434 ret = tmp;
2435 }
2436
2437 return ret;
94d7acf3
JM
2438#else /* CONFIG_WPS_ER */
2439 return NULL;
2440#endif /* CONFIG_WPS_ER */
f3f2ba2e
JM
2441}
2442#endif /* CONFIG_WPS_NFC */
2443
2444
5ab9a6a5 2445struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
f3f2ba2e 2446 int ndef, int cr, const char *uuid)
e65552dd 2447{
f3f2ba2e 2448 struct wpabuf *ret;
5ab9a6a5
JM
2449 if (!cr)
2450 return NULL;
f3f2ba2e
JM
2451 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2452 if (ret)
2453 return ret;
2454 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
e65552dd
JM
2455}
2456
2457
679f2e7c
JM
2458static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2459 const struct wpabuf *data)
e65552dd
JM
2460{
2461 struct wpabuf *wps;
fa4c2988
JM
2462 int ret = -1;
2463 u16 wsc_len;
2464 const u8 *pos;
2465 struct wpabuf msg;
2466 struct wps_parse_attr attr;
2467 u16 dev_pw_id;
91a65018
JM
2468 const u8 *bssid = NULL;
2469 int freq = 0;
e65552dd
JM
2470
2471 wps = ndef_parse_wifi(data);
2472 if (wps == NULL)
2473 return -1;
2474 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2475 "payload from NFC connection handover");
fa4c2988
JM
2476 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2477 if (wpabuf_len(wps) < 2) {
2478 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2479 "Message");
2480 goto out;
2481 }
2482 pos = wpabuf_head(wps);
2483 wsc_len = WPA_GET_BE16(pos);
2484 if (wsc_len > wpabuf_len(wps) - 2) {
2485 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2486 "in Wi-Fi Handover Select Message", wsc_len);
2487 goto out;
2488 }
2489 pos += 2;
2490
2491 wpa_hexdump(MSG_DEBUG,
2492 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2493 pos, wsc_len);
2494 if (wsc_len < wpabuf_len(wps) - 2) {
2495 wpa_hexdump(MSG_DEBUG,
2496 "WPS: Ignore extra data after WSC attributes",
2497 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2498 }
2499
2500 wpabuf_set(&msg, pos, wsc_len);
2501 ret = wps_parse_msg(&msg, &attr);
2502 if (ret < 0) {
2503 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2504 "Wi-Fi Handover Select Message");
2505 goto out;
2506 }
2507
2508 if (attr.oob_dev_password == NULL ||
2509 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2510 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2511 "included in Wi-Fi Handover Select Message");
2512 ret = -1;
2513 goto out;
2514 }
2515
2516 if (attr.ssid == NULL) {
2517 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2518 "Select Message");
2519 ret = -1;
2520 goto out;
2521 }
2522
2523 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2524
91a65018
JM
2525 if (attr.mac_addr) {
2526 bssid = attr.mac_addr;
2527 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2528 MAC2STR(bssid));
2529 }
2530
2531 if (attr.rf_bands)
2532 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2533
2534 if (attr.ap_channel) {
2535 u16 chan = WPA_GET_BE16(attr.ap_channel);
2536
2537 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2538
2539 if (chan >= 1 && chan <= 13 &&
2540 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2541 freq = 2407 + 5 * chan;
2542 else if (chan == 14 &&
2543 (attr.rf_bands == NULL ||
2544 *attr.rf_bands & WPS_RF_24GHZ))
2545 freq = 2484;
2546 else if (chan >= 30 &&
2547 (attr.rf_bands == NULL ||
2548 *attr.rf_bands & WPS_RF_50GHZ))
2549 freq = 5000 + 5 * chan;
2550
2551 if (freq) {
2552 wpa_printf(MSG_DEBUG,
2553 "WPS: AP indicated channel %u -> %u MHz",
2554 chan, freq);
2555 }
2556 }
2557
fa4c2988
JM
2558 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2559 attr.oob_dev_password, attr.oob_dev_password_len);
2560 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2561 WPS_OOB_PUBKEY_HASH_LEN);
2562 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2563 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2564 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2565 ret = -1;
2566 goto out;
2567 }
2568 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2569 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2570
91a65018 2571 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
fa4c2988 2572 attr.oob_dev_password,
91a65018 2573 attr.ssid, attr.ssid_len, freq);
fa4c2988
JM
2574
2575out:
e65552dd 2576 wpabuf_free(wps);
e65552dd
JM
2577 return ret;
2578}
2579
e4758827
JM
2580
2581int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2582 const struct wpabuf *req,
2583 const struct wpabuf *sel)
2584{
2585 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2586 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2587 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2588 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2589}
2590
50d1f890
JM
2591
2592int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2593 const struct wpabuf *req,
2594 const struct wpabuf *sel)
2595{
2596 struct wpabuf *wps;
2597 int ret = -1;
2598 u16 wsc_len;
2599 const u8 *pos;
2600 struct wpabuf msg;
2601 struct wps_parse_attr attr;
2602 u16 dev_pw_id;
2603
2604 /*
2605 * Enrollee/station is always initiator of the NFC connection handover,
2606 * so use the request message here to find Enrollee public key hash.
2607 */
2608 wps = ndef_parse_wifi(req);
2609 if (wps == NULL)
2610 return -1;
2611 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2612 "payload from NFC connection handover");
2613 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2614 if (wpabuf_len(wps) < 2) {
2615 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2616 "Message");
2617 goto out;
2618 }
2619 pos = wpabuf_head(wps);
2620 wsc_len = WPA_GET_BE16(pos);
2621 if (wsc_len > wpabuf_len(wps) - 2) {
2622 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2623 "in rt Wi-Fi Handover Request Message", wsc_len);
2624 goto out;
2625 }
2626 pos += 2;
2627
2628 wpa_hexdump(MSG_DEBUG,
2629 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2630 pos, wsc_len);
2631 if (wsc_len < wpabuf_len(wps) - 2) {
2632 wpa_hexdump(MSG_DEBUG,
2633 "WPS: Ignore extra data after WSC attributes",
2634 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2635 }
2636
2637 wpabuf_set(&msg, pos, wsc_len);
2638 ret = wps_parse_msg(&msg, &attr);
2639 if (ret < 0) {
2640 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2641 "Wi-Fi Handover Request Message");
2642 goto out;
2643 }
2644
2645 if (attr.oob_dev_password == NULL ||
2646 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2647 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2648 "included in Wi-Fi Handover Request Message");
2649 ret = -1;
2650 goto out;
2651 }
2652
2653 if (attr.uuid_e == NULL) {
2654 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2655 "Handover Request Message");
2656 ret = -1;
2657 goto out;
2658 }
2659
2660 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2661
2662 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2663 attr.oob_dev_password, attr.oob_dev_password_len);
2664 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2665 WPS_OOB_PUBKEY_HASH_LEN);
2666 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2667 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2668 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2669 ret = -1;
2670 goto out;
2671 }
2672 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2673 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2674
2675 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2676 attr.oob_dev_password,
2677 DEV_PW_NFC_CONNECTION_HANDOVER,
2678 NULL, 0, 1);
2679
2680out:
2681 wpabuf_free(wps);
2682 return ret;
2683}
2684
3f2c8ba6 2685#endif /* CONFIG_WPS_NFC */
f9f0526b
JM
2686
2687
f9f0526b
JM
2688static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2689{
2690 size_t i;
51bffab1 2691 struct os_reltime now;
f9f0526b
JM
2692
2693 if (wpa_debug_level > MSG_DEBUG)
2694 return;
2695
2696 if (wpa_s->wps_ap == NULL)
2697 return;
2698
51bffab1 2699 os_get_reltime(&now);
f9f0526b
JM
2700
2701 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2702 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2703 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2704
2705 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2706 "tries=%d last_attempt=%d sec ago blacklist=%d",
2707 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2708 ap->last_attempt.sec > 0 ?
2709 (int) now.sec - (int) ap->last_attempt.sec : -1,
2710 e ? e->count : 0);
2711 }
2712}
2713
2714
2715static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2716 const u8 *bssid)
2717{
2718 size_t i;
2719
2720 if (wpa_s->wps_ap == NULL)
2721 return NULL;
2722
2723 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2724 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2725 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2726 return ap;
2727 }
2728
2729 return NULL;
2730}
2731
2732
2733static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2734 struct wpa_scan_res *res)
2735{
2736 struct wpabuf *wps;
2737 enum wps_ap_info_type type;
2738 struct wps_ap_info *ap;
2739 int r;
2740
2741 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2742 return;
2743
2744 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2745 if (wps == NULL)
2746 return;
2747
2748 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2749 if (r == 2)
2750 type = WPS_AP_SEL_REG_OUR;
2751 else if (r == 1)
2752 type = WPS_AP_SEL_REG;
2753 else
2754 type = WPS_AP_NOT_SEL_REG;
2755
2756 wpabuf_free(wps);
2757
2758 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2759 if (ap) {
2760 if (ap->type != type) {
2761 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2762 " changed type %d -> %d",
2763 MAC2STR(res->bssid), ap->type, type);
2764 ap->type = type;
a462036a
JM
2765 if (type != WPS_AP_NOT_SEL_REG)
2766 wpa_blacklist_del(wpa_s, ap->bssid);
f9f0526b
JM
2767 }
2768 return;
2769 }
2770
2771 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2772 sizeof(struct wps_ap_info));
2773 if (ap == NULL)
2774 return;
2775
2776 wpa_s->wps_ap = ap;
2777 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2778 wpa_s->num_wps_ap++;
2779
2780 os_memset(ap, 0, sizeof(*ap));
2781 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2782 ap->type = type;
2783 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2784 MAC2STR(ap->bssid), ap->type);
2785}
2786
2787
2788void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2789 struct wpa_scan_results *scan_res)
2790{
2791 size_t i;
2792
2793 for (i = 0; i < scan_res->num; i++)
2794 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2795
2796 wpas_wps_dump_ap_info(wpa_s);
2797}
2798
2799
2800void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2801{
2802 struct wps_ap_info *ap;
7255983b
JM
2803
2804 wpa_s->after_wps = 0;
2805
f9f0526b
JM
2806 if (!wpa_s->wps_ap_iter)
2807 return;
2808 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2809 if (ap == NULL)
2810 return;
2811 ap->tries++;
51bffab1 2812 os_get_reltime(&ap->last_attempt);
f9f0526b 2813}