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