blob: f51340f97dc4002592d9198a99f5fc566d3ec823 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant / WPS integration
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "uuid.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070014#include "crypto/random.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "crypto/dh_group5.h"
16#include "common/ieee802_11_defs.h"
17#include "common/ieee802_11_common.h"
18#include "common/wpa_common.h"
19#include "common/wpa_ctrl.h"
20#include "eap_common/eap_wsc_common.h"
21#include "eap_peer/eap.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070022#include "eapol_supp/eapol_supp_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "rsn_supp/wpa.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070024#include "wps/wps_attr_parse.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "config.h"
26#include "wpa_supplicant_i.h"
27#include "driver_i.h"
28#include "notify.h"
29#include "blacklist.h"
30#include "bss.h"
31#include "scan.h"
32#include "ap.h"
33#include "p2p/p2p.h"
34#include "p2p_supplicant.h"
35#include "wps_supplicant.h"
36
37
38#ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
39#define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40#endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
41
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070042/*
43 * The minimum time in seconds before trying to associate to a WPS PIN AP that
44 * does not have Selected Registrar TRUE.
45 */
46#ifndef WPS_PIN_TIME_IGNORE_SEL_REG
47#define WPS_PIN_TIME_IGNORE_SEL_REG 5
48#endif /* WPS_PIN_TIME_IGNORE_SEL_REG */
49
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
51static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
52
53
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070054static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
55{
56 os_free(wpa_s->wps_ap);
57 wpa_s->wps_ap = NULL;
58 wpa_s->num_wps_ap = 0;
59 wpa_s->wps_ap_iter = 0;
60}
61
62
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070063static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
64{
65 struct wpa_supplicant *wpa_s = eloop_ctx;
66 int use_fast_assoc = timeout_ctx != NULL;
67
68 wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
69 if (!use_fast_assoc ||
70 wpa_supplicant_fast_associate(wpa_s) != 1)
71 wpa_supplicant_req_scan(wpa_s, 0, 0);
72}
73
74
75static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
76{
77 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
78 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
79}
80
81
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
83{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080084 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
85 return 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087 if (!wpa_s->wps_success &&
88 wpa_s->current_ssid &&
89 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
90 const u8 *bssid = wpa_s->bssid;
91 if (is_zero_ether_addr(bssid))
92 bssid = wpa_s->pending_bssid;
93
94 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
95 " did not succeed - continue trying to find "
96 "suitable AP", MAC2STR(bssid));
97 wpa_blacklist_add(wpa_s, bssid);
98
99 wpa_supplicant_deauthenticate(wpa_s,
100 WLAN_REASON_DEAUTH_LEAVING);
101 wpa_s->reassociate = 1;
102 wpa_supplicant_req_scan(wpa_s,
103 wpa_s->blacklist_cleared ? 5 : 0, 0);
104 wpa_s->blacklist_cleared = 0;
105 return 1;
106 }
107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700108 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
110 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
111 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
112
113 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
114 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
115 int disabled = wpa_s->current_ssid->disabled;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800116 unsigned int freq = wpa_s->assoc_freq;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700117 struct wpa_bss *bss;
118 struct wpa_ssid *ssid = NULL;
119 int use_fast_assoc = 0;
120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800122 "try to associate with the received credential "
123 "(freq=%u)", freq);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800124 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 wpa_supplicant_deauthenticate(wpa_s,
126 WLAN_REASON_DEAUTH_LEAVING);
127 if (disabled) {
128 wpa_printf(MSG_DEBUG, "WPS: Current network is "
129 "disabled - wait for user to enable");
130 return 1;
131 }
132 wpa_s->after_wps = 5;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800133 wpa_s->wps_freq = freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800134 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 wpa_s->reassociate = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700136
137 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
138 "without a new scan can be used");
139 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
140 if (bss) {
141 struct wpabuf *wps;
142 struct wps_parse_attr attr;
143
144 wps = wpa_bss_get_vendor_ie_multi(bss,
145 WPS_IE_VENDOR_TYPE);
146 if (wps && wps_parse_msg(wps, &attr) == 0 &&
147 attr.wps_state &&
148 *attr.wps_state == WPS_STATE_CONFIGURED)
149 use_fast_assoc = 1;
150 wpabuf_free(wps);
151 }
152
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700153 /*
154 * Complete the next step from an eloop timeout to allow pending
155 * driver events related to the disconnection to be processed
156 * first. This makes it less likely for disconnection event to
157 * cause problems with the following connection.
158 */
159 wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
160 wpas_wps_assoc_with_cred_cancel(wpa_s);
161 eloop_register_timeout(0, 10000,
162 wpas_wps_assoc_with_cred, wpa_s,
163 use_fast_assoc ? (void *) 1 :
164 (void *) 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 return 1;
166 }
167
168 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
169 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
170 "for external credential processing");
171 wpas_clear_wps(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800172 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 wpa_supplicant_deauthenticate(wpa_s,
174 WLAN_REASON_DEAUTH_LEAVING);
175 return 1;
176 }
177
178 return 0;
179}
180
181
182static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
183 struct wpa_ssid *ssid,
184 const struct wps_credential *cred)
185{
186 struct wpa_driver_capa capa;
187 struct wpa_bss *bss;
188 const u8 *ie;
189 struct wpa_ie_data adv;
190 int wpa2 = 0, ccmp = 0;
191
192 /*
193 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
194 * case they are configured for mixed mode operation (WPA+WPA2 and
195 * TKIP+CCMP). Try to use scan results to figure out whether the AP
196 * actually supports stronger security and select that if the client
197 * has support for it, too.
198 */
199
200 if (wpa_drv_get_capa(wpa_s, &capa))
201 return; /* Unknown what driver supports */
202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203 if (ssid->ssid == NULL)
204 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700206 if (!bss)
207 bss = wpa_bss_get(wpa_s, wpa_s->bssid,
208 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 if (bss == NULL) {
210 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
211 "table - use credential as-is");
212 return;
213 }
214
215 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
216
217 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
218 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
219 wpa2 = 1;
220 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
221 ccmp = 1;
222 } else {
223 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
224 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
225 adv.pairwise_cipher & WPA_CIPHER_CCMP)
226 ccmp = 1;
227 }
228
229 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
230 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
231 /*
232 * TODO: This could be the initial AP configuration and the
233 * Beacon contents could change shortly. Should request a new
234 * scan and delay addition of the network until the updated
235 * scan results are available.
236 */
237 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
238 "support - use credential as-is");
239 return;
240 }
241
242 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
243 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
244 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
245 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
246 "based on scan results");
247 if (wpa_s->conf->ap_scan == 1)
248 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
249 else
250 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
251 }
252
253 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
254 (ssid->proto & WPA_PROTO_WPA) &&
255 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
256 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
257 "based on scan results");
258 if (wpa_s->conf->ap_scan == 1)
259 ssid->proto |= WPA_PROTO_RSN;
260 else
261 ssid->proto = WPA_PROTO_RSN;
262 }
263}
264
265
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700266static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
267 struct wpa_ssid *new_ssid)
268{
269 struct wpa_ssid *ssid, *next;
270
271 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
272 ssid = next, next = ssid ? ssid->next : NULL) {
273 /*
274 * new_ssid has already been added to the list in
275 * wpas_wps_add_network(), so skip it.
276 */
277 if (ssid == new_ssid)
278 continue;
279
280 if (ssid->bssid_set || new_ssid->bssid_set) {
281 if (ssid->bssid_set != new_ssid->bssid_set)
282 continue;
283 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
284 0)
285 continue;
286 }
287
288 /* compare SSID */
289 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
290 continue;
291
292 if (ssid->ssid && new_ssid->ssid) {
293 if (os_memcmp(ssid->ssid, new_ssid->ssid,
294 ssid->ssid_len) != 0)
295 continue;
296 } else if (ssid->ssid || new_ssid->ssid)
297 continue;
298
299 /* compare security parameters */
300 if (ssid->auth_alg != new_ssid->auth_alg ||
301 ssid->key_mgmt != new_ssid->key_mgmt ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800302 (ssid->group_cipher != new_ssid->group_cipher &&
303 !(ssid->group_cipher & new_ssid->group_cipher &
304 WPA_CIPHER_CCMP)))
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700305 continue;
306
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700307 /*
308 * Some existing WPS APs will send two creds in case they are
309 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
310 * Try to merge these two creds if they are received in the same
311 * M8 message.
312 */
313 if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
314 wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
315 if (new_ssid->passphrase && ssid->passphrase &&
316 os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
317 0) {
318 wpa_printf(MSG_DEBUG,
319 "WPS: M8 Creds with different passphrase - do not merge");
320 continue;
321 }
322
323 if (new_ssid->psk_set &&
324 (!ssid->psk_set ||
325 os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
326 wpa_printf(MSG_DEBUG,
327 "WPS: M8 Creds with different PSK - do not merge");
328 continue;
329 }
330
331 if ((new_ssid->passphrase && !ssid->passphrase) ||
332 (!new_ssid->passphrase && ssid->passphrase)) {
333 wpa_printf(MSG_DEBUG,
334 "WPS: M8 Creds with different passphrase/PSK type - do not merge");
335 continue;
336 }
337
338 wpa_printf(MSG_DEBUG,
339 "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
340 new_ssid->proto |= ssid->proto;
341 new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
342 } else {
343 /*
344 * proto and pairwise_cipher difference matter for
345 * non-mixed-mode creds.
346 */
347 if (ssid->proto != new_ssid->proto ||
348 ssid->pairwise_cipher != new_ssid->pairwise_cipher)
349 continue;
350 }
351
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700352 /* Remove the duplicated older network entry. */
353 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
354 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800355 if (wpa_s->current_ssid == ssid)
356 wpa_s->current_ssid = NULL;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700357 wpa_config_remove_network(wpa_s->conf, ssid->id);
358 }
359}
360
361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362static int wpa_supplicant_wps_cred(void *ctx,
363 const struct wps_credential *cred)
364{
365 struct wpa_supplicant *wpa_s = ctx;
366 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 u16 auth_type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800368#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 int registrar = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800370#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371
372 if ((wpa_s->conf->wps_cred_processing == 1 ||
373 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
374 size_t blen = cred->cred_attr_len * 2 + 1;
375 char *buf = os_malloc(blen);
376 if (buf) {
377 wpa_snprintf_hex(buf, blen,
378 cred->cred_attr, cred->cred_attr_len);
379 wpa_msg(wpa_s, MSG_INFO, "%s%s",
380 WPS_EVENT_CRED_RECEIVED, buf);
381 os_free(buf);
382 }
383
384 wpas_notify_wps_credential(wpa_s, cred);
385 } else
386 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
387
388 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
389 cred->cred_attr, cred->cred_attr_len);
390
391 if (wpa_s->conf->wps_cred_processing == 1)
392 return 0;
393
394 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
395 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
396 cred->auth_type);
397 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
398 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
399 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
400 cred->key, cred->key_len);
401 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
402 MAC2STR(cred->mac_addr));
403
404 auth_type = cred->auth_type;
405 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
406 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
407 "auth_type into WPA2PSK");
408 auth_type = WPS_AUTH_WPA2PSK;
409 }
410
411 if (auth_type != WPS_AUTH_OPEN &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 auth_type != WPS_AUTH_WPAPSK &&
413 auth_type != WPS_AUTH_WPA2PSK) {
414 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
415 "unsupported authentication type 0x%x",
416 auth_type);
417 return 0;
418 }
419
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800420 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
421 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
422 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
423 "invalid Network Key length %lu",
424 (unsigned long) cred->key_len);
425 return -1;
426 }
427 }
428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
430 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
431 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800432#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 if (ssid->eap.identity &&
434 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
435 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
436 WSC_ID_REGISTRAR_LEN) == 0)
437 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800438#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 os_free(ssid->eap.identity);
440 ssid->eap.identity = NULL;
441 ssid->eap.identity_len = 0;
442 os_free(ssid->eap.phase1);
443 ssid->eap.phase1 = NULL;
444 os_free(ssid->eap.eap_methods);
445 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700446 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700448 ssid->bssid_set = 0;
449 }
450 ssid->disabled_until.sec = 0;
451 ssid->disabled_until.usec = 0;
452 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 } else {
454 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
455 "received credential");
456 ssid = wpa_config_add_network(wpa_s->conf);
457 if (ssid == NULL)
458 return -1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700459 if (wpa_s->current_ssid) {
460 /*
461 * Should the GO issue multiple credentials for some
462 * reason, each credential should be marked as a
463 * temporary P2P group similarly to the one that gets
464 * marked as such based on the pre-configured values
465 * used for the WPS network block.
466 */
467 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
468 ssid->temporary = wpa_s->current_ssid->temporary;
469 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 wpas_notify_network_added(wpa_s, ssid);
471 }
472
473 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700474 ssid->wps_run = wpa_s->wps_run;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475
476 os_free(ssid->ssid);
477 ssid->ssid = os_malloc(cred->ssid_len);
478 if (ssid->ssid) {
479 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
480 ssid->ssid_len = cred->ssid_len;
481 }
482
483 switch (cred->encr_type) {
484 case WPS_ENCR_NONE:
485 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 case WPS_ENCR_TKIP:
487 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
488 break;
489 case WPS_ENCR_AES:
490 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800491 if (wpa_s->drv_capa_known &&
492 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
493 ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
494 ssid->group_cipher |= WPA_CIPHER_GCMP;
495 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700496 if (wpa_s->drv_capa_known &&
497 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256)) {
498 ssid->pairwise_cipher |= WPA_CIPHER_GCMP_256;
499 ssid->group_cipher |= WPA_CIPHER_GCMP_256;
500 }
501 if (wpa_s->drv_capa_known &&
502 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256)) {
503 ssid->pairwise_cipher |= WPA_CIPHER_CCMP_256;
504 ssid->group_cipher |= WPA_CIPHER_CCMP_256;
505 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 break;
507 }
508
509 switch (auth_type) {
510 case WPS_AUTH_OPEN:
511 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
512 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
513 ssid->proto = 0;
514#ifdef CONFIG_WPS_REG_DISABLE_OPEN
515 if (registrar) {
516 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
517 "id=%d - Credentials for an open "
518 "network disabled by default - use "
519 "'select_network %d' to enable",
520 ssid->id, ssid->id);
521 ssid->disabled = 1;
522 }
523#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
524 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 case WPS_AUTH_WPAPSK:
526 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
527 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
528 ssid->proto = WPA_PROTO_WPA;
529 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530 case WPS_AUTH_WPA2PSK:
531 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
532 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalom021b0b52019-04-10 11:17:58 -0700533 if (wpa_s->conf->wps_cred_add_sae &&
534 cred->key_len != 2 * PMK_LEN) {
535 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
Hai Shalom021b0b52019-04-10 11:17:58 -0700536 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
Hai Shalom021b0b52019-04-10 11:17:58 -0700537 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 ssid->proto = WPA_PROTO_RSN;
539 break;
540 }
541
Hai Shalom021b0b52019-04-10 11:17:58 -0700542 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 if (cred->key_len == 2 * PMK_LEN) {
544 if (hexstr2bin((const char *) cred->key, ssid->psk,
545 PMK_LEN)) {
546 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
547 "Key");
548 return -1;
549 }
550 ssid->psk_set = 1;
551 ssid->export_keys = 1;
552 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
553 os_free(ssid->passphrase);
554 ssid->passphrase = os_malloc(cred->key_len + 1);
555 if (ssid->passphrase == NULL)
556 return -1;
557 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
558 ssid->passphrase[cred->key_len] = '\0';
559 wpa_config_update_psk(ssid);
560 ssid->export_keys = 1;
561 } else {
562 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
563 "length %lu",
564 (unsigned long) cred->key_len);
565 return -1;
566 }
567 }
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700568 ssid->priority = wpa_s->conf->wps_priority;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569
570 wpas_wps_security_workaround(wpa_s, ssid, cred);
571
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700572 wpas_wps_remove_dup_network(wpa_s, ssid);
573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574#ifndef CONFIG_NO_CONFIG_WRITE
575 if (wpa_s->conf->update_config &&
576 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
577 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
578 return -1;
579 }
580#endif /* CONFIG_NO_CONFIG_WRITE */
581
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700582 if (ssid->priority)
583 wpa_config_update_prio_list(wpa_s->conf);
584
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800585 /*
586 * Optimize the post-WPS scan based on the channel used during
587 * the provisioning in case EAP-Failure is not received.
588 */
589 wpa_s->after_wps = 5;
590 wpa_s->wps_freq = wpa_s->assoc_freq;
591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592 return 0;
593}
594
595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
597 struct wps_event_m2d *m2d)
598{
599 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
600 "dev_password_id=%d config_error=%d",
601 m2d->dev_password_id, m2d->config_error);
602 wpas_notify_wps_event_m2d(wpa_s, m2d);
603#ifdef CONFIG_P2P
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800604 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
605 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 "dev_password_id=%d config_error=%d",
607 m2d->dev_password_id, m2d->config_error);
608 }
609 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
610 /*
611 * Notify P2P from eloop timeout to avoid issues with the
612 * interface getting removed while processing a message.
613 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700614 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 NULL);
616 }
617#endif /* CONFIG_P2P */
618}
619
620
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700621static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
622{
623 struct wpa_supplicant *wpa_s = eloop_ctx;
624 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
625 wpas_clear_wps(wpa_s);
626}
627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628
629static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
630 struct wps_event_fail *fail)
631{
632 if (fail->error_indication > 0 &&
633 fail->error_indication < NUM_WPS_EI_VALUES) {
634 wpa_msg(wpa_s, MSG_INFO,
635 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
636 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700637 wps_ei_str(fail->error_indication));
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800638 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
639 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 "msg=%d config_error=%d reason=%d (%s)",
641 fail->msg, fail->config_error,
642 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700643 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 } else {
645 wpa_msg(wpa_s, MSG_INFO,
646 WPS_EVENT_FAIL "msg=%d config_error=%d",
647 fail->msg, fail->config_error);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800648 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
649 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 "msg=%d config_error=%d",
651 fail->msg, fail->config_error);
652 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700653
654 /*
655 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
656 */
657 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
658 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
659 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700662 wpas_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663}
664
665
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800666static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
667
668static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
669{
670 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800671 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800672
673 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
674
675 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
676 if (ssid->disabled_for_connect && ssid->disabled) {
677 ssid->disabled_for_connect = 0;
678 ssid->disabled = 0;
679 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800680 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800681 }
682 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800683
684 if (changed) {
685#ifndef CONFIG_NO_CONFIG_WRITE
686 if (wpa_s->conf->update_config &&
687 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
688 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
689 "configuration");
690 }
691#endif /* CONFIG_NO_CONFIG_WRITE */
692 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800693}
694
695
696static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
697{
698 struct wpa_supplicant *wpa_s = eloop_ctx;
699 /* Enable the networks disabled during wpas_wps_reassoc */
700 wpas_wps_reenable_networks(wpa_s);
701}
702
703
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800704int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
705{
706 return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
707 wpa_s, NULL);
708}
709
710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
712{
713 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
714 wpa_s->wps_success = 1;
715 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700716 if (wpa_s->current_ssid)
717 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
718 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800719
720 /*
721 * Enable the networks disabled during wpas_wps_reassoc after 10
722 * seconds. The 10 seconds timer is to allow the data connection to be
723 * formed before allowing other networks to be selected.
724 */
725 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
726 NULL);
727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729}
730
731
732static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
733 struct wps_event_er_ap *ap)
734{
735 char uuid_str[100];
736 char dev_type[WPS_DEV_TYPE_BUFSIZE];
737
738 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
739 if (ap->pri_dev_type)
740 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
741 sizeof(dev_type));
742 else
743 dev_type[0] = '\0';
744
745 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
746 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
747 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
748 ap->friendly_name ? ap->friendly_name : "",
749 ap->manufacturer ? ap->manufacturer : "",
750 ap->model_description ? ap->model_description : "",
751 ap->model_name ? ap->model_name : "",
752 ap->manufacturer_url ? ap->manufacturer_url : "",
753 ap->model_url ? ap->model_url : "");
754}
755
756
757static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
758 struct wps_event_er_ap *ap)
759{
760 char uuid_str[100];
761 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
762 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
763}
764
765
766static void wpa_supplicant_wps_event_er_enrollee_add(
767 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
768{
769 char uuid_str[100];
770 char dev_type[WPS_DEV_TYPE_BUFSIZE];
771
772 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
773 if (enrollee->pri_dev_type)
774 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
775 sizeof(dev_type));
776 else
777 dev_type[0] = '\0';
778
779 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
780 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
781 "|%s|%s|%s|%s|%s|",
782 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
783 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
784 enrollee->dev_name ? enrollee->dev_name : "",
785 enrollee->manufacturer ? enrollee->manufacturer : "",
786 enrollee->model_name ? enrollee->model_name : "",
787 enrollee->model_number ? enrollee->model_number : "",
788 enrollee->serial_number ? enrollee->serial_number : "");
789}
790
791
792static void wpa_supplicant_wps_event_er_enrollee_remove(
793 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
794{
795 char uuid_str[100];
796 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
797 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
798 uuid_str, MAC2STR(enrollee->mac_addr));
799}
800
801
802static void wpa_supplicant_wps_event_er_ap_settings(
803 struct wpa_supplicant *wpa_s,
804 struct wps_event_er_ap_settings *ap_settings)
805{
806 char uuid_str[100];
807 char key_str[65];
808 const struct wps_credential *cred = ap_settings->cred;
809
810 key_str[0] = '\0';
811 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
812 if (cred->key_len >= 8 && cred->key_len <= 64) {
813 os_memcpy(key_str, cred->key, cred->key_len);
814 key_str[cred->key_len] = '\0';
815 }
816 }
817
818 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
819 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
820 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
821 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
822 "key=%s",
823 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
824 cred->auth_type, cred->encr_type, key_str);
825}
826
827
828static void wpa_supplicant_wps_event_er_set_sel_reg(
829 struct wpa_supplicant *wpa_s,
830 struct wps_event_er_set_selected_registrar *ev)
831{
832 char uuid_str[100];
833
834 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
835 switch (ev->state) {
836 case WPS_ER_SET_SEL_REG_START:
837 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
838 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
839 "sel_reg_config_methods=0x%x",
840 uuid_str, ev->sel_reg, ev->dev_passwd_id,
841 ev->sel_reg_config_methods);
842 break;
843 case WPS_ER_SET_SEL_REG_DONE:
844 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
845 "uuid=%s state=DONE", uuid_str);
846 break;
847 case WPS_ER_SET_SEL_REG_FAILED:
848 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
849 "uuid=%s state=FAILED", uuid_str);
850 break;
851 }
852}
853
854
855static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
856 union wps_event_data *data)
857{
858 struct wpa_supplicant *wpa_s = ctx;
859 switch (event) {
860 case WPS_EV_M2D:
861 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
862 break;
863 case WPS_EV_FAIL:
864 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
865 break;
866 case WPS_EV_SUCCESS:
867 wpa_supplicant_wps_event_success(wpa_s);
868 break;
869 case WPS_EV_PWD_AUTH_FAIL:
870#ifdef CONFIG_AP
871 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
872 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
873#endif /* CONFIG_AP */
874 break;
875 case WPS_EV_PBC_OVERLAP:
876 break;
877 case WPS_EV_PBC_TIMEOUT:
878 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700879 case WPS_EV_PBC_ACTIVE:
880 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
881 break;
882 case WPS_EV_PBC_DISABLE:
883 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
884 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 case WPS_EV_ER_AP_ADD:
886 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
887 break;
888 case WPS_EV_ER_AP_REMOVE:
889 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
890 break;
891 case WPS_EV_ER_ENROLLEE_ADD:
892 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
893 &data->enrollee);
894 break;
895 case WPS_EV_ER_ENROLLEE_REMOVE:
896 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
897 &data->enrollee);
898 break;
899 case WPS_EV_ER_AP_SETTINGS:
900 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
901 &data->ap_settings);
902 break;
903 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
904 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
905 &data->set_sel_reg);
906 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800907 case WPS_EV_AP_PIN_SUCCESS:
908 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 }
910}
911
912
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700913static int wpa_supplicant_wps_rf_band(void *ctx)
914{
915 struct wpa_supplicant *wpa_s = ctx;
916
917 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
918 return 0;
919
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700920 return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
921 (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700922}
923
924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
926{
927 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
928 eap_is_wps_pin_enrollee(&ssid->eap))
929 return WPS_REQ_ENROLLEE;
930 else
931 return WPS_REQ_REGISTRAR;
932}
933
934
935static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
936{
937 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700938 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
939
Dmitry Shmidt051af732013-10-22 13:52:46 -0700940 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700941 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700942
Jouni Malinen75ecf522011-06-27 15:19:46 -0700943 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800945 /* Enable the networks disabled during wpas_wps_reassoc */
946 wpas_wps_reenable_networks(wpa_s);
947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800949 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950
951 /* Remove any existing WPS network from configuration */
952 ssid = wpa_s->conf->ssid;
953 while (ssid) {
954 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
955 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800956 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700957 wpa_supplicant_deauthenticate(
958 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 }
960 id = ssid->id;
961 remove_ssid = ssid;
962 } else
963 id = -1;
964 ssid = ssid->next;
965 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700966 if (prev_current == remove_ssid) {
967 wpa_sm_set_config(wpa_s->wpa, NULL);
968 eapol_sm_notify_config(wpa_s->eapol, NULL,
969 NULL);
970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 wpas_notify_network_removed(wpa_s, remove_ssid);
972 wpa_config_remove_network(wpa_s->conf, id);
973 }
974 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700975
976 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977}
978
979
980static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
981{
982 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800983 union wps_event_data data;
984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
986 "out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800987 os_memset(&data, 0, sizeof(data));
988 data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
989 data.fail.error_indication = WPS_EI_NO_ERROR;
990 /*
991 * Call wpas_notify_wps_event_fail() directly instead of through
992 * wpa_supplicant_wps_event() which would end up registering unnecessary
993 * timeouts (those are only for the case where the failure happens
994 * during an EAP-WSC exchange).
995 */
996 wpas_notify_wps_event_fail(wpa_s, &data.fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 wpas_clear_wps(wpa_s);
998}
999
1000
1001static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001002 int registrar, const u8 *dev_addr,
1003 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004{
1005 struct wpa_ssid *ssid;
1006
1007 ssid = wpa_config_add_network(wpa_s->conf);
1008 if (ssid == NULL)
1009 return NULL;
1010 wpas_notify_network_added(wpa_s, ssid);
1011 wpa_config_set_network_defaults(ssid);
1012 ssid->temporary = 1;
1013 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
1014 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
1015 wpa_config_set(ssid, "identity", registrar ?
1016 "\"" WSC_ID_REGISTRAR "\"" :
1017 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1018 wpas_notify_network_removed(wpa_s, ssid);
1019 wpa_config_remove_network(wpa_s->conf, ssid->id);
1020 return NULL;
1021 }
1022
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001023#ifdef CONFIG_P2P
1024 if (dev_addr)
1025 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1026#endif /* CONFIG_P2P */
1027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 if (bssid) {
1029#ifndef CONFIG_P2P
1030 struct wpa_bss *bss;
1031 int count = 0;
1032#endif /* CONFIG_P2P */
1033
1034 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1035 ssid->bssid_set = 1;
1036
1037 /*
1038 * Note: With P2P, the SSID may change at the time the WPS
1039 * provisioning is started, so better not filter the AP based
1040 * on the current SSID in the scan results.
1041 */
1042#ifndef CONFIG_P2P
1043 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1044 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
1045 continue;
1046
1047 os_free(ssid->ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001048 ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 if (ssid->ssid == NULL)
1050 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 ssid->ssid_len = bss->ssid_len;
1052 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1053 "scan results",
1054 ssid->ssid, ssid->ssid_len);
1055 count++;
1056 }
1057
1058 if (count > 1) {
1059 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1060 "for the AP; use wildcard");
1061 os_free(ssid->ssid);
1062 ssid->ssid = NULL;
1063 ssid->ssid_len = 0;
1064 }
1065#endif /* CONFIG_P2P */
1066 }
1067
1068 return ssid;
1069}
1070
1071
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001072static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1073 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074{
1075 struct wpa_ssid *ssid;
1076
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001077 if (wpa_s->current_ssid) {
1078 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001079 wpa_supplicant_deauthenticate(
1080 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001081 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 /* Mark all other networks disabled and trigger reassociation */
1084 ssid = wpa_s->conf->ssid;
1085 while (ssid) {
1086 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001087 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001088 /*
1089 * In case the network object corresponds to a persistent group
1090 * then do not send out network disabled signal. In addition,
1091 * do not change disabled status of persistent network objects
1092 * from 2 to 1 should we connect to another network.
1093 */
1094 if (was_disabled != 2) {
1095 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001096 if (was_disabled != ssid->disabled) {
1097 if (ssid->disabled)
1098 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001099 wpas_notify_network_enabled_changed(wpa_s,
1100 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001101 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001102 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 ssid = ssid->next;
1104 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001105}
1106
1107
1108static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001109 struct wpa_ssid *selected, const u8 *bssid,
1110 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001111{
1112 struct wpa_bss *bss;
1113
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001114 wpa_s->wps_run++;
1115 if (wpa_s->wps_run == 0)
1116 wpa_s->wps_run++;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001117 wpa_s->after_wps = 0;
1118 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001119 if (freq) {
1120 wpa_s->after_wps = 5;
1121 wpa_s->wps_freq = freq;
1122 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001123 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1124 if (bss && bss->freq > 0) {
1125 wpa_s->known_wps_freq = 1;
1126 wpa_s->wps_freq = bss->freq;
1127 }
1128 }
1129
1130 wpas_wps_temp_disable(wpa_s, selected);
1131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 wpa_s->disconnected = 0;
1133 wpa_s->reassociate = 1;
1134 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001135 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 wpa_s->wps_success = 0;
1137 wpa_s->blacklist_cleared = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001138
1139 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 wpa_supplicant_req_scan(wpa_s, 0, 0);
1141}
1142
1143
1144int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
Hai Shalom021b0b52019-04-10 11:17:58 -07001145 int p2p_group, int multi_ap_backhaul_sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146{
1147 struct wpa_ssid *ssid;
Hai Shalom021b0b52019-04-10 11:17:58 -07001148 char phase1[32];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001149
1150#ifdef CONFIG_AP
1151 if (wpa_s->ap_iface) {
1152 wpa_printf(MSG_DEBUG,
1153 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1154 return -1;
1155 }
1156#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001158 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 if (ssid == NULL)
1160 return -1;
1161 ssid->temporary = 1;
1162 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001163 /*
1164 * When starting a regular WPS process (not P2P group formation)
1165 * the registrar/final station can be either AP or PCP
1166 * so use a "don't care" value for the pbss flag.
1167 */
1168 if (!p2p_group)
1169 ssid->pbss = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170#ifdef CONFIG_P2P
1171 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1172 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1173 if (ssid->ssid) {
1174 ssid->ssid_len = wpa_s->go_params->ssid_len;
1175 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1176 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001177 if (wpa_s->go_params->freq > 56160) {
1178 /* P2P in 60 GHz uses PBSS */
1179 ssid->pbss = 1;
1180 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1182 "SSID", ssid->ssid, ssid->ssid_len);
1183 }
1184 }
1185#endif /* CONFIG_P2P */
Hai Shalom021b0b52019-04-10 11:17:58 -07001186 os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
1187 multi_ap_backhaul_sta ? " multi_ap=1" : "");
1188 if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001189 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 if (wpa_s->wps_fragment_size)
1191 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Hai Shalom021b0b52019-04-10 11:17:58 -07001192 if (multi_ap_backhaul_sta)
1193 ssid->multi_ap_backhaul_sta = 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001194 wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1196 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001197 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 return 0;
1199}
1200
1201
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001202static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1203 const u8 *dev_addr, const u8 *bssid,
1204 const char *pin, int p2p_group, u16 dev_pw_id,
1205 const u8 *peer_pubkey_hash,
1206 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207{
1208 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001209 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001211 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001213#ifdef CONFIG_AP
1214 if (wpa_s->ap_iface) {
1215 wpa_printf(MSG_DEBUG,
1216 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1217 return -1;
1218 }
1219#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001221 if (bssid && is_zero_ether_addr(bssid))
1222 bssid = NULL;
1223 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1224 if (ssid == NULL) {
1225 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228 ssid->temporary = 1;
1229 ssid->p2p_group = p2p_group;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001230 /*
1231 * When starting a regular WPS process (not P2P group formation)
1232 * the registrar/final station can be either AP or PCP
1233 * so use a "don't care" value for the pbss flag.
1234 */
1235 if (!p2p_group)
1236 ssid->pbss = 2;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001237 if (ssid_val) {
1238 ssid->ssid = os_malloc(ssid_len);
1239 if (ssid->ssid) {
1240 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1241 ssid->ssid_len = ssid_len;
1242 }
1243 }
1244 if (peer_pubkey_hash) {
1245 os_memcpy(hash, " pkhash=", 8);
1246 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1247 peer_pubkey_hash,
1248 WPS_OOB_PUBKEY_HASH_LEN);
1249 } else {
1250 hash[0] = '\0';
1251 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252#ifdef CONFIG_P2P
1253 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07001254 os_free(ssid->ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1256 if (ssid->ssid) {
1257 ssid->ssid_len = wpa_s->go_params->ssid_len;
1258 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1259 ssid->ssid_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001260 if (wpa_s->go_params->freq > 56160) {
1261 /* P2P in 60 GHz uses PBSS */
1262 ssid->pbss = 1;
1263 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1265 "SSID", ssid->ssid, ssid->ssid_len);
1266 }
1267 }
1268#endif /* CONFIG_P2P */
1269 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001270 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1271 pin, dev_pw_id, hash);
1272 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1273 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1274 dev_pw_id, hash);
1275 } else {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001276 if (wps_generate_pin(&rpin) < 0) {
1277 wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1278 return -1;
1279 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001280 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1281 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001283 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1284 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001285 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001286 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 if (wpa_s->wps_fragment_size)
1288 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1289 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1290 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001291 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001292 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 return rpin;
1294}
1295
1296
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001297int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1298 const char *pin, int p2p_group, u16 dev_pw_id)
1299{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001300 os_get_reltime(&wpa_s->wps_pin_start_time);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001301 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1302 dev_pw_id, NULL, NULL, 0, 0);
1303}
1304
1305
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001306void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1307{
1308 union wps_event_data data;
1309
1310 os_memset(&data, 0, sizeof(data));
1311 data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1312 data.fail.error_indication = WPS_EI_NO_ERROR;
1313 /*
1314 * Call wpas_notify_wps_event_fail() directly instead of through
1315 * wpa_supplicant_wps_event() which would end up registering unnecessary
1316 * timeouts (those are only for the case where the failure happens
1317 * during an EAP-WSC exchange).
1318 */
1319 wpas_notify_wps_event_fail(wpa_s, &data.fail);
1320}
1321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322/* Cancel the wps pbc/pin requests */
1323int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1324{
1325#ifdef CONFIG_AP
1326 if (wpa_s->ap_iface) {
1327 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1328 return wpa_supplicant_ap_wps_cancel(wpa_s);
1329 }
1330#endif /* CONFIG_AP */
1331
Dmitry Shmidt04949592012-07-19 12:16:46 -07001332 if (wpa_s->wpa_state == WPA_SCANNING ||
1333 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1335 wpa_supplicant_cancel_scan(wpa_s);
1336 wpas_clear_wps(wpa_s);
1337 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1338 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1339 "deauthenticate");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001340 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 wpa_supplicant_deauthenticate(wpa_s,
1342 WLAN_REASON_DEAUTH_LEAVING);
1343 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001344 } else {
1345 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001346 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001347 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1348 0)
1349 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 }
1351
Dmitry Shmidt051af732013-10-22 13:52:46 -07001352 wpa_s->after_wps = 0;
1353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 return 0;
1355}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356
1357
1358int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1359 const char *pin, struct wps_new_ap_settings *settings)
1360{
1361 struct wpa_ssid *ssid;
1362 char val[200];
1363 char *pos, *end;
1364 int res;
1365
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001366#ifdef CONFIG_AP
1367 if (wpa_s->ap_iface) {
1368 wpa_printf(MSG_DEBUG,
1369 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1370 return -1;
1371 }
1372#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373 if (!pin)
1374 return -1;
1375 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001376 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 if (ssid == NULL)
1378 return -1;
1379 ssid->temporary = 1;
1380 pos = val;
1381 end = pos + sizeof(val);
1382 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001383 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 return -1;
1385 pos += res;
1386 if (settings) {
1387 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1388 "new_encr=%s new_key=%s",
1389 settings->ssid_hex, settings->auth,
1390 settings->encr, settings->key_hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001391 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 return -1;
1393 pos += res;
1394 }
1395 res = os_snprintf(pos, end - pos, "\"");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001396 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001398 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1399 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 if (wpa_s->wps_fragment_size)
1401 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1402 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1403 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001404 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 return 0;
1406}
1407
1408
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001409static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1410 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 size_t psk_len)
1412{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001413 if (is_zero_ether_addr(p2p_dev_addr)) {
1414 wpa_printf(MSG_DEBUG,
1415 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1416 MAC2STR(mac_addr));
1417 } else {
1418 wpa_printf(MSG_DEBUG,
1419 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1420 " P2P Device Addr " MACSTR,
1421 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1422 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1424
1425 /* TODO */
1426
1427 return 0;
1428}
1429
1430
1431static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1432 const struct wps_device_data *dev)
1433{
1434 char uuid[40], txt[400];
1435 int len;
1436 char devtype[WPS_DEV_TYPE_BUFSIZE];
1437 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1438 return;
1439 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1440 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1441 " [%s|%s|%s|%s|%s|%s]",
1442 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1443 dev->manufacturer, dev->model_name,
1444 dev->model_number, dev->serial_number,
1445 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1446 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001447 if (!os_snprintf_error(sizeof(txt), len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 wpa_printf(MSG_INFO, "%s", txt);
1449}
1450
1451
1452static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1453 u16 sel_reg_config_methods)
1454{
1455#ifdef CONFIG_WPS_ER
1456 struct wpa_supplicant *wpa_s = ctx;
1457
1458 if (wpa_s->wps_er == NULL)
1459 return;
1460 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1461 "dev_password_id=%u sel_reg_config_methods=0x%x",
1462 sel_reg, dev_passwd_id, sel_reg_config_methods);
1463 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1464 sel_reg_config_methods);
1465#endif /* CONFIG_WPS_ER */
1466}
1467
1468
1469static u16 wps_fix_config_methods(u16 config_methods)
1470{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 if ((config_methods &
1472 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1473 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1474 wpa_printf(MSG_INFO, "WPS: Converting display to "
1475 "virtual_display for WPS 2.0 compliance");
1476 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1477 }
1478 if ((config_methods &
1479 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1480 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1481 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1482 "virtual_push_button for WPS 2.0 compliance");
1483 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1484 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001485
1486 return config_methods;
1487}
1488
1489
1490static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1491 struct wps_context *wps)
1492{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001493 char buf[50];
1494 const char *src;
1495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 if (is_nil_uuid(wpa_s->conf->uuid)) {
1497 struct wpa_supplicant *first;
1498 first = wpa_s->global->ifaces;
1499 while (first && first->next)
1500 first = first->next;
1501 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001502 if (wps != wpa_s->global->ifaces->wps)
1503 os_memcpy(wps->uuid,
1504 wpa_s->global->ifaces->wps->uuid,
1505 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001506 src = "from the first interface";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001507 } else if (wpa_s->conf->auto_uuid == 1) {
1508 uuid_random(wps->uuid);
1509 src = "based on random data";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 } else {
1511 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001512 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 }
1514 } else {
1515 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001516 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001517 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001518
1519 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1520 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521}
1522
1523
Dmitry Shmidt04949592012-07-19 12:16:46 -07001524static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1525 struct wps_context *wps)
1526{
1527 wpabuf_free(wps->dev.vendor_ext_m1);
1528 wps->dev.vendor_ext_m1 = NULL;
1529
1530 if (wpa_s->conf->wps_vendor_ext_m1) {
1531 wps->dev.vendor_ext_m1 =
1532 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1533 if (!wps->dev.vendor_ext_m1) {
1534 wpa_printf(MSG_ERROR, "WPS: Cannot "
1535 "allocate memory for vendor_ext_m1");
1536 }
1537 }
1538}
1539
1540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541int wpas_wps_init(struct wpa_supplicant *wpa_s)
1542{
1543 struct wps_context *wps;
1544 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001545 struct hostapd_hw_modes *modes;
1546 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547
1548 wps = os_zalloc(sizeof(*wps));
1549 if (wps == NULL)
1550 return -1;
1551
1552 wps->cred_cb = wpa_supplicant_wps_cred;
1553 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001554 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555 wps->cb_ctx = wpa_s;
1556
1557 wps->dev.device_name = wpa_s->conf->device_name;
1558 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1559 wps->dev.model_name = wpa_s->conf->model_name;
1560 wps->dev.model_number = wpa_s->conf->model_number;
1561 wps->dev.serial_number = wpa_s->conf->serial_number;
1562 wps->config_methods =
1563 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1564 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1565 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1566 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1567 "methods are not allowed at the same time");
1568 os_free(wps);
1569 return -1;
1570 }
1571 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001572 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1574 WPS_DEV_TYPE_LEN);
1575
1576 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1577 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1578 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1579
Dmitry Shmidt04949592012-07-19 12:16:46 -07001580 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001583 modes = wpa_s->hw.modes;
1584 if (modes) {
1585 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1586 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1587 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1588 wps->dev.rf_bands |= WPS_RF_24GHZ;
1589 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1590 wps->dev.rf_bands |= WPS_RF_50GHZ;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001591 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1592 wps->dev.rf_bands |= WPS_RF_60GHZ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 }
1594 }
1595 if (wps->dev.rf_bands == 0) {
1596 /*
1597 * Default to claiming support for both bands if the driver
1598 * does not provide support for fetching supported bands.
1599 */
1600 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1601 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1603 wpas_wps_set_uuid(wpa_s, wps);
1604
1605 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1606 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1607
1608 os_memset(&rcfg, 0, sizeof(rcfg));
1609 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1610 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1611 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1612 rcfg.cb_ctx = wpa_s;
1613
1614 wps->registrar = wps_registrar_init(wps, &rcfg);
1615 if (wps->registrar == NULL) {
1616 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1617 os_free(wps);
1618 return -1;
1619 }
1620
1621 wpa_s->wps = wps;
1622
1623 return 0;
1624}
1625
1626
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001627#ifdef CONFIG_WPS_ER
1628static void wpas_wps_nfc_clear(struct wps_context *wps)
1629{
1630 wps->ap_nfc_dev_pw_id = 0;
1631 wpabuf_free(wps->ap_nfc_dh_pubkey);
1632 wps->ap_nfc_dh_pubkey = NULL;
1633 wpabuf_free(wps->ap_nfc_dh_privkey);
1634 wps->ap_nfc_dh_privkey = NULL;
1635 wpabuf_free(wps->ap_nfc_dev_pw);
1636 wps->ap_nfc_dev_pw = NULL;
1637}
1638#endif /* CONFIG_WPS_ER */
1639
1640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1642{
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001643 wpas_wps_assoc_with_cred_cancel(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001645 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001646 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001647 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001649#ifdef CONFIG_P2P
1650 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1651#endif /* CONFIG_P2P */
1652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 if (wpa_s->wps == NULL)
1654 return;
1655
1656#ifdef CONFIG_WPS_ER
1657 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1658 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001659 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660#endif /* CONFIG_WPS_ER */
1661
1662 wps_registrar_deinit(wpa_s->wps->registrar);
1663 wpabuf_free(wpa_s->wps->dh_pubkey);
1664 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001665 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 os_free(wpa_s->wps->network_key);
1667 os_free(wpa_s->wps);
1668 wpa_s->wps = NULL;
1669}
1670
1671
1672int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001673 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674{
1675 struct wpabuf *wps_ie;
1676
1677 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1678 return -1;
1679
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001680 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1682 if (!wps_ie) {
1683 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1684 return 0;
1685 }
1686
1687 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1688 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1689 "without active PBC Registrar");
1690 wpabuf_free(wps_ie);
1691 return 0;
1692 }
1693
1694 /* TODO: overlap detection */
1695 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1696 "(Active PBC)");
1697 wpabuf_free(wps_ie);
1698 return 1;
1699 }
1700
1701 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1702 if (!wps_ie) {
1703 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1704 return 0;
1705 }
1706
1707 /*
1708 * Start with WPS APs that advertise our address as an
1709 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1710 * allow any WPS AP after couple of scans since some APs do not
1711 * set Selected Registrar attribute properly when using
1712 * external Registrar.
1713 */
1714 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001715 struct os_reltime age;
1716
1717 os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1718
1719 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1720 age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1721 wpa_printf(MSG_DEBUG,
1722 " skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1723 wpa_s->scan_runs, (int) age.sec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724 wpabuf_free(wps_ie);
1725 return 0;
1726 }
1727 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1728 } else {
1729 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1730 "(Authorized MAC or Active PIN)");
1731 }
1732 wpabuf_free(wps_ie);
1733 return 1;
1734 }
1735
1736 if (wps_ie) {
1737 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1738 wpabuf_free(wps_ie);
1739 return 1;
1740 }
1741
1742 return -1;
1743}
1744
1745
1746int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1747 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001748 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749{
1750 struct wpabuf *wps_ie = NULL;
1751 int ret = 0;
1752
1753 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001754 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1756 /* allow wildcard SSID for WPS PBC */
1757 ret = 1;
1758 }
1759 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001760 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 if (wps_ie &&
1762 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1763 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1764 /* allow wildcard SSID for WPS PIN */
1765 ret = 1;
1766 }
1767 }
1768
1769 if (!ret && ssid->bssid_set &&
1770 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1771 /* allow wildcard SSID due to hardcoded BSSID match */
1772 ret = 1;
1773 }
1774
1775#ifdef CONFIG_WPS_STRICT
1776 if (wps_ie) {
1777 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1778 0, bss->bssid) < 0)
1779 ret = 0;
1780 if (bss->beacon_ie_len) {
1781 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001782 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 bss, WPS_IE_VENDOR_TYPE);
1784 if (bcn_wps == NULL) {
1785 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1786 "missing from AP Beacon");
1787 ret = 0;
1788 } else {
1789 if (wps_validate_beacon(wps_ie) < 0)
1790 ret = 0;
1791 wpabuf_free(bcn_wps);
1792 }
1793 }
1794 }
1795#endif /* CONFIG_WPS_STRICT */
1796
1797 wpabuf_free(wps_ie);
1798
1799 return ret;
1800}
1801
1802
1803int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1804 struct wpa_bss *selected, struct wpa_ssid *ssid)
1805{
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001806 const u8 *sel_uuid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 struct wpabuf *wps_ie;
1808 int ret = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001809 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810
1811 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1812 return 0;
1813
1814 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1815 "present in scan results; selected BSSID " MACSTR,
1816 MAC2STR(selected->bssid));
1817
1818 /* Make sure that only one AP is in active PBC mode */
1819 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1820 if (wps_ie) {
1821 sel_uuid = wps_get_uuid_e(wps_ie);
1822 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1823 sel_uuid, UUID_LEN);
1824 } else {
1825 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1826 "WPS IE?!");
1827 sel_uuid = NULL;
1828 }
1829
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001830 for (i = 0; i < wpa_s->num_wps_ap; i++) {
1831 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
1832
1833 if (!ap->pbc_active ||
1834 os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 continue;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001838 MACSTR, MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001840 ap->uuid, UUID_LEN);
1841 if (sel_uuid == NULL ||
1842 os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 ret = 1; /* PBC overlap */
1844 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1845 MACSTR " and " MACSTR,
1846 MAC2STR(selected->bssid),
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001847 MAC2STR(ap->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848 break;
1849 }
1850
1851 /* TODO: verify that this is reasonable dual-band situation */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852 }
1853
1854 wpabuf_free(wps_ie);
1855
1856 return ret;
1857}
1858
1859
1860void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1861{
1862 struct wpa_bss *bss;
1863 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1864
1865 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1866 return;
1867
1868 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1869 struct wpabuf *ie;
1870 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1871 if (!ie)
1872 continue;
1873 if (wps_is_selected_pbc_registrar(ie))
1874 pbc++;
1875 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1876 auth++;
1877 else if (wps_is_selected_pin_registrar(ie))
1878 pin++;
1879 else
1880 wps++;
1881 wpabuf_free(ie);
1882 }
1883
1884 if (pbc)
1885 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1886 else if (auth)
1887 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1888 else if (pin)
1889 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1890 else if (wps)
1891 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1892}
1893
1894
1895int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1896{
1897 struct wpa_ssid *ssid;
1898
1899 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1900 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1901 return 1;
1902 }
1903
1904 return 0;
1905}
1906
1907
1908int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1909 char *end)
1910{
1911 struct wpabuf *wps_ie;
1912 int ret;
1913
1914 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1915 if (wps_ie == NULL)
1916 return 0;
1917
1918 ret = wps_attr_text(wps_ie, buf, end);
1919 wpabuf_free(wps_ie);
1920 return ret;
1921}
1922
1923
1924int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1925{
1926#ifdef CONFIG_WPS_ER
1927 if (wpa_s->wps_er) {
1928 wps_er_refresh(wpa_s->wps_er);
1929 return 0;
1930 }
1931 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1932 if (wpa_s->wps_er == NULL)
1933 return -1;
1934 return 0;
1935#else /* CONFIG_WPS_ER */
1936 return 0;
1937#endif /* CONFIG_WPS_ER */
1938}
1939
1940
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001941void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942{
1943#ifdef CONFIG_WPS_ER
1944 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1945 wpa_s->wps_er = NULL;
1946#endif /* CONFIG_WPS_ER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947}
1948
1949
1950#ifdef CONFIG_WPS_ER
1951int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1952 const char *uuid, const char *pin)
1953{
1954 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001955 const u8 *use_uuid = NULL;
1956 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001958 if (os_strcmp(uuid, "any") == 0) {
1959 } else if (uuid_str2bin(uuid, u) == 0) {
1960 use_uuid = u;
1961 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1962 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1963 if (use_uuid == NULL)
1964 return -1;
1965 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 return -1;
1967 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001968 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 (const u8 *) pin, os_strlen(pin), 300);
1970}
1971
1972
1973int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1974{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001975 u8 u[UUID_LEN], *use_uuid = NULL;
1976 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001978 if (uuid_str2bin(uuid, u) == 0)
1979 use_uuid = u;
1980 else if (hwaddr_aton(uuid, addr) == 0)
1981 use_addr = addr;
1982 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001984 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985}
1986
1987
1988int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1989 const char *pin)
1990{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001991 u8 u[UUID_LEN], *use_uuid = NULL;
1992 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001994 if (uuid_str2bin(uuid, u) == 0)
1995 use_uuid = u;
1996 else if (hwaddr_aton(uuid, addr) == 0)
1997 use_addr = addr;
1998 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002000
2001 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 os_strlen(pin));
2003}
2004
2005
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002006static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
2007 struct wps_credential *cred)
2008{
2009 os_memset(cred, 0, sizeof(*cred));
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002010 if (ssid->ssid_len > SSID_MAX_LEN)
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002011 return -1;
2012 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
2013 cred->ssid_len = ssid->ssid_len;
2014 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
2015 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
2016 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
2017 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
2018 cred->encr_type = WPS_ENCR_AES;
2019 else
2020 cred->encr_type = WPS_ENCR_TKIP;
2021 if (ssid->passphrase) {
2022 cred->key_len = os_strlen(ssid->passphrase);
2023 if (cred->key_len >= 64)
2024 return -1;
2025 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2026 } else if (ssid->psk_set) {
2027 cred->key_len = 32;
2028 os_memcpy(cred->key, ssid->psk, 32);
2029 } else
2030 return -1;
2031 } else {
2032 cred->auth_type = WPS_AUTH_OPEN;
2033 cred->encr_type = WPS_ENCR_NONE;
2034 }
2035
2036 return 0;
2037}
2038
2039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2041 int id)
2042{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002043 u8 u[UUID_LEN], *use_uuid = NULL;
2044 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 struct wpa_ssid *ssid;
2046 struct wps_credential cred;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002047 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002049 if (uuid_str2bin(uuid, u) == 0)
2050 use_uuid = u;
2051 else if (hwaddr_aton(uuid, addr) == 0)
2052 use_addr = addr;
2053 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 return -1;
2055 ssid = wpa_config_get_network(wpa_s->conf, id);
2056 if (ssid == NULL || ssid->ssid == NULL)
2057 return -1;
2058
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002059 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002061 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2062 os_memset(&cred, 0, sizeof(cred));
2063 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064}
2065
2066
2067int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2068 const char *pin, struct wps_new_ap_settings *settings)
2069{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002070 u8 u[UUID_LEN], *use_uuid = NULL;
2071 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 struct wps_credential cred;
2073 size_t len;
2074
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002075 if (uuid_str2bin(uuid, u) == 0)
2076 use_uuid = u;
2077 else if (hwaddr_aton(uuid, addr) == 0)
2078 use_addr = addr;
2079 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 return -1;
2081 if (settings->ssid_hex == NULL || settings->auth == NULL ||
2082 settings->encr == NULL || settings->key_hex == NULL)
2083 return -1;
2084
2085 os_memset(&cred, 0, sizeof(cred));
2086 len = os_strlen(settings->ssid_hex);
2087 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2088 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2089 return -1;
2090 cred.ssid_len = len / 2;
2091
2092 len = os_strlen(settings->key_hex);
2093 if ((len & 1) || len > 2 * sizeof(cred.key) ||
2094 hexstr2bin(settings->key_hex, cred.key, len / 2))
2095 return -1;
2096 cred.key_len = len / 2;
2097
2098 if (os_strcmp(settings->auth, "OPEN") == 0)
2099 cred.auth_type = WPS_AUTH_OPEN;
2100 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2101 cred.auth_type = WPS_AUTH_WPAPSK;
2102 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2103 cred.auth_type = WPS_AUTH_WPA2PSK;
2104 else
2105 return -1;
2106
2107 if (os_strcmp(settings->encr, "NONE") == 0)
2108 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002109#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 else if (os_strcmp(settings->encr, "WEP") == 0)
2111 cred.encr_type = WPS_ENCR_WEP;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002112#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 else if (os_strcmp(settings->encr, "TKIP") == 0)
2114 cred.encr_type = WPS_ENCR_TKIP;
2115 else if (os_strcmp(settings->encr, "CCMP") == 0)
2116 cred.encr_type = WPS_ENCR_AES;
2117 else
2118 return -1;
2119
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002120 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2121 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122}
2123
2124
Dmitry Shmidt04949592012-07-19 12:16:46 -07002125#ifdef CONFIG_WPS_NFC
2126struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2127 int ndef, const char *uuid)
2128{
2129 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002130 u8 u[UUID_LEN], *use_uuid = NULL;
2131 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002132
2133 if (!wpa_s->wps_er)
2134 return NULL;
2135
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002136 if (uuid_str2bin(uuid, u) == 0)
2137 use_uuid = u;
2138 else if (hwaddr_aton(uuid, addr) == 0)
2139 use_addr = addr;
2140 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002141 return NULL;
2142
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002143 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002144 if (ndef && ret) {
2145 struct wpabuf *tmp;
2146 tmp = ndef_build_wifi(ret);
2147 wpabuf_free(ret);
2148 if (tmp == NULL)
2149 return NULL;
2150 ret = tmp;
2151 }
2152
2153 return ret;
2154}
2155#endif /* CONFIG_WPS_NFC */
2156
2157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158static int callbacks_pending = 0;
2159
2160static void wpas_wps_terminate_cb(void *ctx)
2161{
2162 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2163 if (--callbacks_pending <= 0)
2164 eloop_terminate();
2165}
2166#endif /* CONFIG_WPS_ER */
2167
2168
2169int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2170{
2171#ifdef CONFIG_WPS_ER
2172 if (wpa_s->wps_er) {
2173 callbacks_pending++;
2174 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2175 wpa_s->wps_er = NULL;
2176 return 1;
2177 }
2178#endif /* CONFIG_WPS_ER */
2179 return 0;
2180}
2181
2182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2184{
2185 struct wps_context *wps = wpa_s->wps;
2186
2187 if (wps == NULL)
2188 return;
2189
2190 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2191 wps->config_methods = wps_config_methods_str2bin(
2192 wpa_s->conf->config_methods);
2193 if ((wps->config_methods &
2194 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2195 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2196 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2197 "config methods are not allowed at the "
2198 "same time");
2199 wps->config_methods &= ~WPS_CONFIG_LABEL;
2200 }
2201 }
2202 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08002203 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204
2205 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2206 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2207 WPS_DEV_TYPE_LEN);
2208
2209 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2210 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2211 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2212 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2213 }
2214
Dmitry Shmidt04949592012-07-19 12:16:46 -07002215 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2216 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2219 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2220
2221 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2222 wpas_wps_set_uuid(wpa_s, wps);
2223
2224 if (wpa_s->conf->changed_parameters &
2225 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2226 /* Update pointers to make sure they refer current values */
2227 wps->dev.device_name = wpa_s->conf->device_name;
2228 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2229 wps->dev.model_name = wpa_s->conf->model_name;
2230 wps->dev.model_number = wpa_s->conf->model_number;
2231 wps->dev.serial_number = wpa_s->conf->serial_number;
2232 }
2233}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002234
2235
Mikael Kanstrupcc779b82019-08-16 08:50:54 +02002236void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2237{
2238 struct wps_context *wps;
2239
2240 wps = wpa_s->wps;
2241 if (wps)
2242 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2243}
2244
2245
Dmitry Shmidt04949592012-07-19 12:16:46 -07002246#ifdef CONFIG_WPS_NFC
2247
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002248#ifdef CONFIG_WPS_ER
Roshan Pius8894db22017-02-13 14:43:20 -08002249struct wpabuf *
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002250wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2251 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002252{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002253 struct wpabuf *ret;
2254 struct wps_credential cred;
2255
2256 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2257 return NULL;
2258
2259 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2260
2261 if (ndef && ret) {
2262 struct wpabuf *tmp;
2263 tmp = ndef_build_wifi(ret);
2264 wpabuf_free(ret);
2265 if (tmp == NULL)
2266 return NULL;
2267 ret = tmp;
2268 }
2269
2270 return ret;
2271}
2272#endif /* CONFIG_WPS_ER */
2273
2274
2275struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2276 int ndef, const char *id_str)
2277{
2278#ifdef CONFIG_WPS_ER
2279 if (id_str) {
2280 int id;
2281 char *end = NULL;
2282 struct wpa_ssid *ssid;
2283
2284 id = strtol(id_str, &end, 10);
2285 if (end && *end)
2286 return NULL;
2287
2288 ssid = wpa_config_get_network(wpa_s->conf, id);
2289 if (ssid == NULL)
2290 return NULL;
2291 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2292 }
2293#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002294#ifdef CONFIG_AP
2295 if (wpa_s->ap_iface)
2296 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2297#endif /* CONFIG_AP */
2298 return NULL;
2299}
2300
2301
Dmitry Shmidt04949592012-07-19 12:16:46 -07002302struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2303{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002304 if (wpa_s->conf->wps_nfc_pw_from_config) {
2305 return wps_nfc_token_build(ndef,
2306 wpa_s->conf->wps_nfc_dev_pw_id,
2307 wpa_s->conf->wps_nfc_dh_pubkey,
2308 wpa_s->conf->wps_nfc_dev_pw);
2309 }
2310
Dmitry Shmidt04949592012-07-19 12:16:46 -07002311 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2312 &wpa_s->conf->wps_nfc_dh_pubkey,
2313 &wpa_s->conf->wps_nfc_dh_privkey,
2314 &wpa_s->conf->wps_nfc_dev_pw);
2315}
2316
2317
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002318int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2319 const u8 *bssid,
2320 const struct wpabuf *dev_pw, u16 dev_pw_id,
2321 int p2p_group, const u8 *peer_pubkey_hash,
2322 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002323{
2324 struct wps_context *wps = wpa_s->wps;
2325 char pw[32 * 2 + 1];
2326
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002327 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2328 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2329 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2330 }
2331
Dmitry Shmidt04949592012-07-19 12:16:46 -07002332 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002333 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2334 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2335 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002336 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002337 }
2338
2339 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2340 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2341 "cannot start NFC-triggered connection", dev_pw_id);
2342 return -1;
2343 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002344
2345 dh5_free(wps->dh_ctx);
2346 wpabuf_free(wps->dh_pubkey);
2347 wpabuf_free(wps->dh_privkey);
2348 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2349 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2350 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2351 wps->dh_ctx = NULL;
2352 wpabuf_free(wps->dh_pubkey);
2353 wps->dh_pubkey = NULL;
2354 wpabuf_free(wps->dh_privkey);
2355 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002356 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002357 return -1;
2358 }
2359 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002360 if (wps->dh_ctx == NULL) {
2361 wpabuf_free(wps->dh_pubkey);
2362 wps->dh_pubkey = NULL;
2363 wpabuf_free(wps->dh_privkey);
2364 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002365 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002366 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002367 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002368
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002369 if (dev_pw) {
2370 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2371 wpabuf_head(dev_pw),
2372 wpabuf_len(dev_pw));
2373 }
2374 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2375 dev_pw ? pw : NULL,
2376 p2p_group, dev_pw_id, peer_pubkey_hash,
2377 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002378}
2379
2380
2381static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2382 struct wps_parse_attr *attr)
2383{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002384 /*
2385 * Disable existing networks temporarily to allow the newly learned
2386 * credential to be preferred. Enable the temporarily disabled networks
2387 * after 10 seconds.
2388 */
2389 wpas_wps_temp_disable(wpa_s, NULL);
2390 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2391 NULL);
2392
Dmitry Shmidt04949592012-07-19 12:16:46 -07002393 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2394 return -1;
2395
2396 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2397 return 0;
2398
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002399 if (attr->ap_channel) {
2400 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002401 int freq = 0;
2402
2403 if (chan >= 1 && chan <= 13)
2404 freq = 2407 + 5 * chan;
2405 else if (chan == 14)
2406 freq = 2484;
2407 else if (chan >= 30)
2408 freq = 5000 + 5 * chan;
2409
2410 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002411 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2412 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002413 wpa_s->after_wps = 5;
2414 wpa_s->wps_freq = freq;
2415 }
2416 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002417
2418 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2419 "based on the received credential added");
2420 wpa_s->normal_scans = 0;
2421 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002422 wpa_s->disconnected = 0;
2423 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002424
2425 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002426 wpa_supplicant_req_scan(wpa_s, 0, 0);
2427
2428 return 0;
2429}
2430
2431
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002432#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002433static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2434 struct wps_parse_attr *attr)
2435{
2436 return wps_registrar_add_nfc_password_token(
2437 wpa_s->wps->registrar, attr->oob_dev_password,
2438 attr->oob_dev_password_len);
2439}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002440#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002441
2442
2443static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2444 const struct wpabuf *wps)
2445{
2446 struct wps_parse_attr attr;
2447
2448 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2449
2450 if (wps_parse_msg(wps, &attr)) {
2451 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2452 return -1;
2453 }
2454
2455 if (attr.num_cred)
2456 return wpas_wps_use_cred(wpa_s, &attr);
2457
2458#ifdef CONFIG_WPS_ER
2459 if (attr.oob_dev_password)
2460 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2461#endif /* CONFIG_WPS_ER */
2462
2463 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2464 return -1;
2465}
2466
2467
2468int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002469 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002470{
2471 const struct wpabuf *wps = data;
2472 struct wpabuf *tmp = NULL;
2473 int ret;
2474
2475 if (wpabuf_len(data) < 4)
2476 return -1;
2477
2478 if (*wpabuf_head_u8(data) != 0x10) {
2479 /* Assume this contains full NDEF record */
2480 tmp = ndef_parse_wifi(data);
2481 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002482#ifdef CONFIG_P2P
2483 tmp = ndef_parse_p2p(data);
2484 if (tmp) {
2485 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2486 forced_freq);
2487 wpabuf_free(tmp);
2488 return ret;
2489 }
2490#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002491 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2492 return -1;
2493 }
2494 wps = tmp;
2495 }
2496
2497 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2498 wpabuf_free(tmp);
2499 return ret;
2500}
2501
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002502
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002503struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2504 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002505{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002506 struct wpabuf *ret;
2507
2508 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2509 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2510 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2511 return NULL;
2512
2513 ret = wps_build_nfc_handover_req(wpa_s->wps,
2514 wpa_s->conf->wps_nfc_dh_pubkey);
2515
2516 if (ndef && ret) {
2517 struct wpabuf *tmp;
2518 tmp = ndef_build_wifi(ret);
2519 wpabuf_free(ret);
2520 if (tmp == NULL)
2521 return NULL;
2522 ret = tmp;
2523 }
2524
2525 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002526}
2527
2528
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002529#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002530
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002531static struct wpabuf *
2532wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2533 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002534{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002535#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002536 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002537 u8 u[UUID_LEN], *use_uuid = NULL;
2538 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002539 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002540
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002541 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002542 return NULL;
2543
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002544 if (uuid == NULL)
2545 return NULL;
2546 if (uuid_str2bin(uuid, u) == 0)
2547 use_uuid = u;
2548 else if (hwaddr_aton(uuid, addr) == 0)
2549 use_addr = addr;
2550 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002551 return NULL;
2552
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002553 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2554 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2555 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2556 return NULL;
2557 }
2558
2559 wpas_wps_nfc_clear(wps);
2560 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2561 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2562 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2563 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2564 wpas_wps_nfc_clear(wps);
2565 return NULL;
2566 }
2567
2568 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2569 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002570 if (ndef && ret) {
2571 struct wpabuf *tmp;
2572 tmp = ndef_build_wifi(ret);
2573 wpabuf_free(ret);
2574 if (tmp == NULL)
2575 return NULL;
2576 ret = tmp;
2577 }
2578
2579 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002580#else /* CONFIG_WPS_ER */
2581 return NULL;
2582#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002583}
2584#endif /* CONFIG_WPS_NFC */
2585
2586
2587struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2588 int ndef, int cr, const char *uuid)
2589{
2590 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002591 if (!cr)
2592 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002593 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2594 if (ret)
2595 return ret;
2596 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002597}
2598
2599
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002600static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2601 const struct wpabuf *data)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002602{
2603 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002604 int ret = -1;
2605 u16 wsc_len;
2606 const u8 *pos;
2607 struct wpabuf msg;
2608 struct wps_parse_attr attr;
2609 u16 dev_pw_id;
2610 const u8 *bssid = NULL;
2611 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002612
2613 wps = ndef_parse_wifi(data);
2614 if (wps == NULL)
2615 return -1;
2616 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2617 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002618 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2619 if (wpabuf_len(wps) < 2) {
2620 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2621 "Message");
2622 goto out;
2623 }
2624 pos = wpabuf_head(wps);
2625 wsc_len = WPA_GET_BE16(pos);
2626 if (wsc_len > wpabuf_len(wps) - 2) {
2627 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2628 "in Wi-Fi Handover Select Message", wsc_len);
2629 goto out;
2630 }
2631 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002632
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002633 wpa_hexdump(MSG_DEBUG,
2634 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2635 pos, wsc_len);
2636 if (wsc_len < wpabuf_len(wps) - 2) {
2637 wpa_hexdump(MSG_DEBUG,
2638 "WPS: Ignore extra data after WSC attributes",
2639 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2640 }
2641
2642 wpabuf_set(&msg, pos, wsc_len);
2643 ret = wps_parse_msg(&msg, &attr);
2644 if (ret < 0) {
2645 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2646 "Wi-Fi Handover Select Message");
2647 goto out;
2648 }
2649
2650 if (attr.oob_dev_password == NULL ||
2651 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2652 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2653 "included in Wi-Fi Handover Select Message");
2654 ret = -1;
2655 goto out;
2656 }
2657
2658 if (attr.ssid == NULL) {
2659 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2660 "Select Message");
2661 ret = -1;
2662 goto out;
2663 }
2664
2665 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2666
2667 if (attr.mac_addr) {
2668 bssid = attr.mac_addr;
2669 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2670 MAC2STR(bssid));
2671 }
2672
2673 if (attr.rf_bands)
2674 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2675
2676 if (attr.ap_channel) {
2677 u16 chan = WPA_GET_BE16(attr.ap_channel);
2678
2679 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2680
2681 if (chan >= 1 && chan <= 13 &&
2682 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2683 freq = 2407 + 5 * chan;
2684 else if (chan == 14 &&
2685 (attr.rf_bands == NULL ||
2686 *attr.rf_bands & WPS_RF_24GHZ))
2687 freq = 2484;
2688 else if (chan >= 30 &&
2689 (attr.rf_bands == NULL ||
2690 *attr.rf_bands & WPS_RF_50GHZ))
2691 freq = 5000 + 5 * chan;
Hai Shalomc3565922019-10-28 11:58:20 -07002692 else if (chan >= 1 && chan <= 6 &&
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002693 (attr.rf_bands == NULL ||
2694 *attr.rf_bands & WPS_RF_60GHZ))
2695 freq = 56160 + 2160 * chan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002696
2697 if (freq) {
2698 wpa_printf(MSG_DEBUG,
2699 "WPS: AP indicated channel %u -> %u MHz",
2700 chan, freq);
2701 }
2702 }
2703
2704 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2705 attr.oob_dev_password, attr.oob_dev_password_len);
2706 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2707 WPS_OOB_PUBKEY_HASH_LEN);
2708 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2709 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2710 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2711 ret = -1;
2712 goto out;
2713 }
2714 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2715 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2716
2717 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2718 attr.oob_dev_password,
2719 attr.ssid, attr.ssid_len, freq);
2720
2721out:
2722 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002723 return ret;
2724}
2725
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002726
2727int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2728 const struct wpabuf *req,
2729 const struct wpabuf *sel)
2730{
2731 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2732 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2733 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2734 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2735}
2736
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002737
2738int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2739 const struct wpabuf *req,
2740 const struct wpabuf *sel)
2741{
2742 struct wpabuf *wps;
2743 int ret = -1;
2744 u16 wsc_len;
2745 const u8 *pos;
2746 struct wpabuf msg;
2747 struct wps_parse_attr attr;
2748 u16 dev_pw_id;
2749
2750 /*
2751 * Enrollee/station is always initiator of the NFC connection handover,
2752 * so use the request message here to find Enrollee public key hash.
2753 */
2754 wps = ndef_parse_wifi(req);
2755 if (wps == NULL)
2756 return -1;
2757 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2758 "payload from NFC connection handover");
2759 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2760 if (wpabuf_len(wps) < 2) {
2761 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2762 "Message");
2763 goto out;
2764 }
2765 pos = wpabuf_head(wps);
2766 wsc_len = WPA_GET_BE16(pos);
2767 if (wsc_len > wpabuf_len(wps) - 2) {
2768 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2769 "in rt Wi-Fi Handover Request Message", wsc_len);
2770 goto out;
2771 }
2772 pos += 2;
2773
2774 wpa_hexdump(MSG_DEBUG,
2775 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2776 pos, wsc_len);
2777 if (wsc_len < wpabuf_len(wps) - 2) {
2778 wpa_hexdump(MSG_DEBUG,
2779 "WPS: Ignore extra data after WSC attributes",
2780 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2781 }
2782
2783 wpabuf_set(&msg, pos, wsc_len);
2784 ret = wps_parse_msg(&msg, &attr);
2785 if (ret < 0) {
2786 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2787 "Wi-Fi Handover Request Message");
2788 goto out;
2789 }
2790
2791 if (attr.oob_dev_password == NULL ||
2792 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2793 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2794 "included in Wi-Fi Handover Request Message");
2795 ret = -1;
2796 goto out;
2797 }
2798
2799 if (attr.uuid_e == NULL) {
2800 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2801 "Handover Request Message");
2802 ret = -1;
2803 goto out;
2804 }
2805
2806 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2807
2808 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2809 attr.oob_dev_password, attr.oob_dev_password_len);
2810 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2811 WPS_OOB_PUBKEY_HASH_LEN);
2812 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2813 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2814 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2815 ret = -1;
2816 goto out;
2817 }
2818 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2819 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2820
2821 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2822 attr.oob_dev_password,
2823 DEV_PW_NFC_CONNECTION_HANDOVER,
2824 NULL, 0, 1);
2825
2826out:
2827 wpabuf_free(wps);
2828 return ret;
2829}
2830
Dmitry Shmidt04949592012-07-19 12:16:46 -07002831#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002832
2833
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002834static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2835{
2836 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002837 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002838
2839 if (wpa_debug_level > MSG_DEBUG)
2840 return;
2841
2842 if (wpa_s->wps_ap == NULL)
2843 return;
2844
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002845 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002846
2847 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2848 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2849 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2850
2851 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2852 "tries=%d last_attempt=%d sec ago blacklist=%d",
2853 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2854 ap->last_attempt.sec > 0 ?
2855 (int) now.sec - (int) ap->last_attempt.sec : -1,
2856 e ? e->count : 0);
2857 }
2858}
2859
2860
2861static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2862 const u8 *bssid)
2863{
2864 size_t i;
2865
2866 if (wpa_s->wps_ap == NULL)
2867 return NULL;
2868
2869 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2870 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2871 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2872 return ap;
2873 }
2874
2875 return NULL;
2876}
2877
2878
2879static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2880 struct wpa_scan_res *res)
2881{
2882 struct wpabuf *wps;
2883 enum wps_ap_info_type type;
2884 struct wps_ap_info *ap;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002885 int r, pbc_active;
2886 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002887
2888 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2889 return;
2890
2891 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2892 if (wps == NULL)
2893 return;
2894
2895 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2896 if (r == 2)
2897 type = WPS_AP_SEL_REG_OUR;
2898 else if (r == 1)
2899 type = WPS_AP_SEL_REG;
2900 else
2901 type = WPS_AP_NOT_SEL_REG;
2902
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002903 uuid = wps_get_uuid_e(wps);
2904 pbc_active = wps_is_selected_pbc_registrar(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002905
2906 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2907 if (ap) {
2908 if (ap->type != type) {
2909 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2910 " changed type %d -> %d",
2911 MAC2STR(res->bssid), ap->type, type);
2912 ap->type = type;
2913 if (type != WPS_AP_NOT_SEL_REG)
2914 wpa_blacklist_del(wpa_s, ap->bssid);
2915 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002916 ap->pbc_active = pbc_active;
2917 if (uuid)
2918 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2919 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002920 }
2921
2922 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2923 sizeof(struct wps_ap_info));
2924 if (ap == NULL)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002925 goto out;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002926
2927 wpa_s->wps_ap = ap;
2928 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2929 wpa_s->num_wps_ap++;
2930
2931 os_memset(ap, 0, sizeof(*ap));
2932 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2933 ap->type = type;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002934 ap->pbc_active = pbc_active;
2935 if (uuid)
2936 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002937 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2938 MAC2STR(ap->bssid), ap->type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002939
2940out:
2941 wpabuf_free(wps);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002942}
2943
2944
2945void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2946 struct wpa_scan_results *scan_res)
2947{
2948 size_t i;
2949
2950 for (i = 0; i < scan_res->num; i++)
2951 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2952
2953 wpas_wps_dump_ap_info(wpa_s);
2954}
2955
2956
2957void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2958{
2959 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002960
2961 wpa_s->after_wps = 0;
2962
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002963 if (!wpa_s->wps_ap_iter)
2964 return;
2965 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2966 if (ap == NULL)
2967 return;
2968 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002969 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002970}