blob: 22b8e71a54cd877d22e9e378a6163dfb0c45e738 [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
42static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
43static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
44
45
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070046static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
47{
48 os_free(wpa_s->wps_ap);
49 wpa_s->wps_ap = NULL;
50 wpa_s->num_wps_ap = 0;
51 wpa_s->wps_ap_iter = 0;
52}
53
54
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
56{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080057#ifdef CONFIG_P2P
58 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
59 return 1;
60#endif /* CONFIG_P2P */
61
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062 if (!wpa_s->wps_success &&
63 wpa_s->current_ssid &&
64 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
65 const u8 *bssid = wpa_s->bssid;
66 if (is_zero_ether_addr(bssid))
67 bssid = wpa_s->pending_bssid;
68
69 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
70 " did not succeed - continue trying to find "
71 "suitable AP", MAC2STR(bssid));
72 wpa_blacklist_add(wpa_s, bssid);
73
74 wpa_supplicant_deauthenticate(wpa_s,
75 WLAN_REASON_DEAUTH_LEAVING);
76 wpa_s->reassociate = 1;
77 wpa_supplicant_req_scan(wpa_s,
78 wpa_s->blacklist_cleared ? 5 : 0, 0);
79 wpa_s->blacklist_cleared = 0;
80 return 1;
81 }
82
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070083 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
85 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
86 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
87
88 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
89 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
90 int disabled = wpa_s->current_ssid->disabled;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080091 unsigned int freq = wpa_s->assoc_freq;
Dmitry Shmidt444d5672013-04-01 13:08:44 -070092 struct wpa_bss *bss;
93 struct wpa_ssid *ssid = NULL;
94 int use_fast_assoc = 0;
95
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080097 "try to associate with the received credential "
98 "(freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 wpa_supplicant_deauthenticate(wpa_s,
100 WLAN_REASON_DEAUTH_LEAVING);
101 if (disabled) {
102 wpa_printf(MSG_DEBUG, "WPS: Current network is "
103 "disabled - wait for user to enable");
104 return 1;
105 }
106 wpa_s->after_wps = 5;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800107 wpa_s->wps_freq = freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800108 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 wpa_s->reassociate = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700110
111 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
112 "without a new scan can be used");
113 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
114 if (bss) {
115 struct wpabuf *wps;
116 struct wps_parse_attr attr;
117
118 wps = wpa_bss_get_vendor_ie_multi(bss,
119 WPS_IE_VENDOR_TYPE);
120 if (wps && wps_parse_msg(wps, &attr) == 0 &&
121 attr.wps_state &&
122 *attr.wps_state == WPS_STATE_CONFIGURED)
123 use_fast_assoc = 1;
124 wpabuf_free(wps);
125 }
126
127 if (!use_fast_assoc ||
128 wpa_supplicant_fast_associate(wpa_s) != 1)
129 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 return 1;
131 }
132
133 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
134 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
135 "for external credential processing");
136 wpas_clear_wps(wpa_s);
137 wpa_supplicant_deauthenticate(wpa_s,
138 WLAN_REASON_DEAUTH_LEAVING);
139 return 1;
140 }
141
142 return 0;
143}
144
145
146static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
147 struct wpa_ssid *ssid,
148 const struct wps_credential *cred)
149{
150 struct wpa_driver_capa capa;
151 struct wpa_bss *bss;
152 const u8 *ie;
153 struct wpa_ie_data adv;
154 int wpa2 = 0, ccmp = 0;
155
156 /*
157 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
158 * case they are configured for mixed mode operation (WPA+WPA2 and
159 * TKIP+CCMP). Try to use scan results to figure out whether the AP
160 * actually supports stronger security and select that if the client
161 * has support for it, too.
162 */
163
164 if (wpa_drv_get_capa(wpa_s, &capa))
165 return; /* Unknown what driver supports */
166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167 if (ssid->ssid == NULL)
168 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
170 if (bss == NULL) {
171 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
172 "table - use credential as-is");
173 return;
174 }
175
176 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
177
178 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
179 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
180 wpa2 = 1;
181 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
182 ccmp = 1;
183 } else {
184 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
185 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
186 adv.pairwise_cipher & WPA_CIPHER_CCMP)
187 ccmp = 1;
188 }
189
190 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
191 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
192 /*
193 * TODO: This could be the initial AP configuration and the
194 * Beacon contents could change shortly. Should request a new
195 * scan and delay addition of the network until the updated
196 * scan results are available.
197 */
198 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
199 "support - use credential as-is");
200 return;
201 }
202
203 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
204 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
205 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
206 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
207 "based on scan results");
208 if (wpa_s->conf->ap_scan == 1)
209 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
210 else
211 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
212 }
213
214 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
215 (ssid->proto & WPA_PROTO_WPA) &&
216 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
217 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
218 "based on scan results");
219 if (wpa_s->conf->ap_scan == 1)
220 ssid->proto |= WPA_PROTO_RSN;
221 else
222 ssid->proto = WPA_PROTO_RSN;
223 }
224}
225
226
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700227static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
228 struct wpa_ssid *new_ssid)
229{
230 struct wpa_ssid *ssid, *next;
231
232 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
233 ssid = next, next = ssid ? ssid->next : NULL) {
234 /*
235 * new_ssid has already been added to the list in
236 * wpas_wps_add_network(), so skip it.
237 */
238 if (ssid == new_ssid)
239 continue;
240
241 if (ssid->bssid_set || new_ssid->bssid_set) {
242 if (ssid->bssid_set != new_ssid->bssid_set)
243 continue;
244 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
245 0)
246 continue;
247 }
248
249 /* compare SSID */
250 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
251 continue;
252
253 if (ssid->ssid && new_ssid->ssid) {
254 if (os_memcmp(ssid->ssid, new_ssid->ssid,
255 ssid->ssid_len) != 0)
256 continue;
257 } else if (ssid->ssid || new_ssid->ssid)
258 continue;
259
260 /* compare security parameters */
261 if (ssid->auth_alg != new_ssid->auth_alg ||
262 ssid->key_mgmt != new_ssid->key_mgmt ||
263 ssid->proto != new_ssid->proto ||
264 ssid->pairwise_cipher != new_ssid->pairwise_cipher ||
265 ssid->group_cipher != new_ssid->group_cipher)
266 continue;
267
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700268 /* Remove the duplicated older network entry. */
269 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
270 wpas_notify_network_removed(wpa_s, ssid);
271 wpa_config_remove_network(wpa_s->conf, ssid->id);
272 }
273}
274
275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276static int wpa_supplicant_wps_cred(void *ctx,
277 const struct wps_credential *cred)
278{
279 struct wpa_supplicant *wpa_s = ctx;
280 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 u16 auth_type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800282#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283 int registrar = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800284#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285
286 if ((wpa_s->conf->wps_cred_processing == 1 ||
287 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
288 size_t blen = cred->cred_attr_len * 2 + 1;
289 char *buf = os_malloc(blen);
290 if (buf) {
291 wpa_snprintf_hex(buf, blen,
292 cred->cred_attr, cred->cred_attr_len);
293 wpa_msg(wpa_s, MSG_INFO, "%s%s",
294 WPS_EVENT_CRED_RECEIVED, buf);
295 os_free(buf);
296 }
297
298 wpas_notify_wps_credential(wpa_s, cred);
299 } else
300 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
301
302 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
303 cred->cred_attr, cred->cred_attr_len);
304
305 if (wpa_s->conf->wps_cred_processing == 1)
306 return 0;
307
308 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
309 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
310 cred->auth_type);
311 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
312 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
313 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
314 cred->key, cred->key_len);
315 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
316 MAC2STR(cred->mac_addr));
317
318 auth_type = cred->auth_type;
319 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
320 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
321 "auth_type into WPA2PSK");
322 auth_type = WPS_AUTH_WPA2PSK;
323 }
324
325 if (auth_type != WPS_AUTH_OPEN &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 auth_type != WPS_AUTH_WPAPSK &&
327 auth_type != WPS_AUTH_WPA2PSK) {
328 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
329 "unsupported authentication type 0x%x",
330 auth_type);
331 return 0;
332 }
333
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800334 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
335 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
336 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
337 "invalid Network Key length %lu",
338 (unsigned long) cred->key_len);
339 return -1;
340 }
341 }
342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
344 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
345 "on the received credential");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800346#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 if (ssid->eap.identity &&
348 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
349 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
350 WSC_ID_REGISTRAR_LEN) == 0)
351 registrar = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800352#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 os_free(ssid->eap.identity);
354 ssid->eap.identity = NULL;
355 ssid->eap.identity_len = 0;
356 os_free(ssid->eap.phase1);
357 ssid->eap.phase1 = NULL;
358 os_free(ssid->eap.eap_methods);
359 ssid->eap.eap_methods = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700360 if (!ssid->p2p_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361 ssid->temporary = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700362 ssid->bssid_set = 0;
363 }
364 ssid->disabled_until.sec = 0;
365 ssid->disabled_until.usec = 0;
366 ssid->auth_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 } else {
368 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
369 "received credential");
370 ssid = wpa_config_add_network(wpa_s->conf);
371 if (ssid == NULL)
372 return -1;
373 wpas_notify_network_added(wpa_s, ssid);
374 }
375
376 wpa_config_set_network_defaults(ssid);
377
378 os_free(ssid->ssid);
379 ssid->ssid = os_malloc(cred->ssid_len);
380 if (ssid->ssid) {
381 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
382 ssid->ssid_len = cred->ssid_len;
383 }
384
385 switch (cred->encr_type) {
386 case WPS_ENCR_NONE:
387 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 case WPS_ENCR_TKIP:
389 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
390 break;
391 case WPS_ENCR_AES:
392 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
393 break;
394 }
395
396 switch (auth_type) {
397 case WPS_AUTH_OPEN:
398 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
399 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
400 ssid->proto = 0;
401#ifdef CONFIG_WPS_REG_DISABLE_OPEN
402 if (registrar) {
403 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
404 "id=%d - Credentials for an open "
405 "network disabled by default - use "
406 "'select_network %d' to enable",
407 ssid->id, ssid->id);
408 ssid->disabled = 1;
409 }
410#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
411 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 case WPS_AUTH_WPAPSK:
413 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
414 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
415 ssid->proto = WPA_PROTO_WPA;
416 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 case WPS_AUTH_WPA2PSK:
418 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
419 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
420 ssid->proto = WPA_PROTO_RSN;
421 break;
422 }
423
424 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
425 if (cred->key_len == 2 * PMK_LEN) {
426 if (hexstr2bin((const char *) cred->key, ssid->psk,
427 PMK_LEN)) {
428 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
429 "Key");
430 return -1;
431 }
432 ssid->psk_set = 1;
433 ssid->export_keys = 1;
434 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
435 os_free(ssid->passphrase);
436 ssid->passphrase = os_malloc(cred->key_len + 1);
437 if (ssid->passphrase == NULL)
438 return -1;
439 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
440 ssid->passphrase[cred->key_len] = '\0';
441 wpa_config_update_psk(ssid);
442 ssid->export_keys = 1;
443 } else {
444 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
445 "length %lu",
446 (unsigned long) cred->key_len);
447 return -1;
448 }
449 }
450
451 wpas_wps_security_workaround(wpa_s, ssid, cred);
452
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700453 wpas_wps_remove_dup_network(wpa_s, ssid);
454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455#ifndef CONFIG_NO_CONFIG_WRITE
456 if (wpa_s->conf->update_config &&
457 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
458 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
459 return -1;
460 }
461#endif /* CONFIG_NO_CONFIG_WRITE */
462
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800463 /*
464 * Optimize the post-WPS scan based on the channel used during
465 * the provisioning in case EAP-Failure is not received.
466 */
467 wpa_s->after_wps = 5;
468 wpa_s->wps_freq = wpa_s->assoc_freq;
469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 return 0;
471}
472
473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
475 struct wps_event_m2d *m2d)
476{
477 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
478 "dev_password_id=%d config_error=%d",
479 m2d->dev_password_id, m2d->config_error);
480 wpas_notify_wps_event_m2d(wpa_s, m2d);
481#ifdef CONFIG_P2P
482 if (wpa_s->parent && wpa_s->parent != wpa_s) {
483 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
484 "dev_password_id=%d config_error=%d",
485 m2d->dev_password_id, m2d->config_error);
486 }
487 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
488 /*
489 * Notify P2P from eloop timeout to avoid issues with the
490 * interface getting removed while processing a message.
491 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700492 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 NULL);
494 }
495#endif /* CONFIG_P2P */
496}
497
498
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700499static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
500{
501 struct wpa_supplicant *wpa_s = eloop_ctx;
502 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
503 wpas_clear_wps(wpa_s);
504}
505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506
507static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
508 struct wps_event_fail *fail)
509{
510 if (fail->error_indication > 0 &&
511 fail->error_indication < NUM_WPS_EI_VALUES) {
512 wpa_msg(wpa_s, MSG_INFO,
513 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
514 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700515 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516 if (wpa_s->parent && wpa_s->parent != wpa_s)
517 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
518 "msg=%d config_error=%d reason=%d (%s)",
519 fail->msg, fail->config_error,
520 fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700521 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522 } else {
523 wpa_msg(wpa_s, MSG_INFO,
524 WPS_EVENT_FAIL "msg=%d config_error=%d",
525 fail->msg, fail->config_error);
526 if (wpa_s->parent && wpa_s->parent != wpa_s)
527 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
528 "msg=%d config_error=%d",
529 fail->msg, fail->config_error);
530 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700531
532 /*
533 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
534 */
535 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
536 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
537 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 wpas_notify_wps_event_fail(wpa_s, fail);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700540#ifdef CONFIG_P2P
541 wpas_p2p_wps_failed(wpa_s, fail);
542#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543}
544
545
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800546static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
547
548static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
549{
550 struct wpa_ssid *ssid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800551 int changed = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800552
553 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
554
555 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
556 if (ssid->disabled_for_connect && ssid->disabled) {
557 ssid->disabled_for_connect = 0;
558 ssid->disabled = 0;
559 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800560 changed++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800561 }
562 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800563
564 if (changed) {
565#ifndef CONFIG_NO_CONFIG_WRITE
566 if (wpa_s->conf->update_config &&
567 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
568 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
569 "configuration");
570 }
571#endif /* CONFIG_NO_CONFIG_WRITE */
572 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800573}
574
575
576static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
577{
578 struct wpa_supplicant *wpa_s = eloop_ctx;
579 /* Enable the networks disabled during wpas_wps_reassoc */
580 wpas_wps_reenable_networks(wpa_s);
581}
582
583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
585{
586 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
587 wpa_s->wps_success = 1;
588 wpas_notify_wps_event_success(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700589 if (wpa_s->current_ssid)
590 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
591 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800592
593 /*
594 * Enable the networks disabled during wpas_wps_reassoc after 10
595 * seconds. The 10 seconds timer is to allow the data connection to be
596 * formed before allowing other networks to be selected.
597 */
598 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
599 NULL);
600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601#ifdef CONFIG_P2P
602 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
603#endif /* CONFIG_P2P */
604}
605
606
607static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
608 struct wps_event_er_ap *ap)
609{
610 char uuid_str[100];
611 char dev_type[WPS_DEV_TYPE_BUFSIZE];
612
613 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
614 if (ap->pri_dev_type)
615 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
616 sizeof(dev_type));
617 else
618 dev_type[0] = '\0';
619
620 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
621 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
622 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
623 ap->friendly_name ? ap->friendly_name : "",
624 ap->manufacturer ? ap->manufacturer : "",
625 ap->model_description ? ap->model_description : "",
626 ap->model_name ? ap->model_name : "",
627 ap->manufacturer_url ? ap->manufacturer_url : "",
628 ap->model_url ? ap->model_url : "");
629}
630
631
632static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
633 struct wps_event_er_ap *ap)
634{
635 char uuid_str[100];
636 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
637 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
638}
639
640
641static void wpa_supplicant_wps_event_er_enrollee_add(
642 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
643{
644 char uuid_str[100];
645 char dev_type[WPS_DEV_TYPE_BUFSIZE];
646
647 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
648 if (enrollee->pri_dev_type)
649 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
650 sizeof(dev_type));
651 else
652 dev_type[0] = '\0';
653
654 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
655 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
656 "|%s|%s|%s|%s|%s|",
657 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
658 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
659 enrollee->dev_name ? enrollee->dev_name : "",
660 enrollee->manufacturer ? enrollee->manufacturer : "",
661 enrollee->model_name ? enrollee->model_name : "",
662 enrollee->model_number ? enrollee->model_number : "",
663 enrollee->serial_number ? enrollee->serial_number : "");
664}
665
666
667static void wpa_supplicant_wps_event_er_enrollee_remove(
668 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
669{
670 char uuid_str[100];
671 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
672 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
673 uuid_str, MAC2STR(enrollee->mac_addr));
674}
675
676
677static void wpa_supplicant_wps_event_er_ap_settings(
678 struct wpa_supplicant *wpa_s,
679 struct wps_event_er_ap_settings *ap_settings)
680{
681 char uuid_str[100];
682 char key_str[65];
683 const struct wps_credential *cred = ap_settings->cred;
684
685 key_str[0] = '\0';
686 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
687 if (cred->key_len >= 8 && cred->key_len <= 64) {
688 os_memcpy(key_str, cred->key, cred->key_len);
689 key_str[cred->key_len] = '\0';
690 }
691 }
692
693 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
694 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
695 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
696 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
697 "key=%s",
698 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
699 cred->auth_type, cred->encr_type, key_str);
700}
701
702
703static void wpa_supplicant_wps_event_er_set_sel_reg(
704 struct wpa_supplicant *wpa_s,
705 struct wps_event_er_set_selected_registrar *ev)
706{
707 char uuid_str[100];
708
709 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
710 switch (ev->state) {
711 case WPS_ER_SET_SEL_REG_START:
712 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
713 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
714 "sel_reg_config_methods=0x%x",
715 uuid_str, ev->sel_reg, ev->dev_passwd_id,
716 ev->sel_reg_config_methods);
717 break;
718 case WPS_ER_SET_SEL_REG_DONE:
719 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
720 "uuid=%s state=DONE", uuid_str);
721 break;
722 case WPS_ER_SET_SEL_REG_FAILED:
723 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
724 "uuid=%s state=FAILED", uuid_str);
725 break;
726 }
727}
728
729
730static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
731 union wps_event_data *data)
732{
733 struct wpa_supplicant *wpa_s = ctx;
734 switch (event) {
735 case WPS_EV_M2D:
736 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
737 break;
738 case WPS_EV_FAIL:
739 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
740 break;
741 case WPS_EV_SUCCESS:
742 wpa_supplicant_wps_event_success(wpa_s);
743 break;
744 case WPS_EV_PWD_AUTH_FAIL:
745#ifdef CONFIG_AP
746 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
747 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
748#endif /* CONFIG_AP */
749 break;
750 case WPS_EV_PBC_OVERLAP:
751 break;
752 case WPS_EV_PBC_TIMEOUT:
753 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700754 case WPS_EV_PBC_ACTIVE:
755 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
756 break;
757 case WPS_EV_PBC_DISABLE:
758 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
759 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 case WPS_EV_ER_AP_ADD:
761 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
762 break;
763 case WPS_EV_ER_AP_REMOVE:
764 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
765 break;
766 case WPS_EV_ER_ENROLLEE_ADD:
767 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
768 &data->enrollee);
769 break;
770 case WPS_EV_ER_ENROLLEE_REMOVE:
771 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
772 &data->enrollee);
773 break;
774 case WPS_EV_ER_AP_SETTINGS:
775 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
776 &data->ap_settings);
777 break;
778 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
779 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
780 &data->set_sel_reg);
781 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800782 case WPS_EV_AP_PIN_SUCCESS:
783 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 }
785}
786
787
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700788static int wpa_supplicant_wps_rf_band(void *ctx)
789{
790 struct wpa_supplicant *wpa_s = ctx;
791
792 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
793 return 0;
794
795 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
796}
797
798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
800{
801 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
802 eap_is_wps_pin_enrollee(&ssid->eap))
803 return WPS_REQ_ENROLLEE;
804 else
805 return WPS_REQ_REGISTRAR;
806}
807
808
809static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
810{
811 int id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700812 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
813
Dmitry Shmidt051af732013-10-22 13:52:46 -0700814 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700815 wpa_s->known_wps_freq = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700816
Jouni Malinen75ecf522011-06-27 15:19:46 -0700817 prev_current = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800819 /* Enable the networks disabled during wpas_wps_reassoc */
820 wpas_wps_reenable_networks(wpa_s);
821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800823 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824
825 /* Remove any existing WPS network from configuration */
826 ssid = wpa_s->conf->ssid;
827 while (ssid) {
828 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
829 if (ssid == wpa_s->current_ssid) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700830 wpa_supplicant_deauthenticate(
831 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 }
833 id = ssid->id;
834 remove_ssid = ssid;
835 } else
836 id = -1;
837 ssid = ssid->next;
838 if (id >= 0) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700839 if (prev_current == remove_ssid) {
840 wpa_sm_set_config(wpa_s->wpa, NULL);
841 eapol_sm_notify_config(wpa_s->eapol, NULL,
842 NULL);
843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 wpas_notify_network_removed(wpa_s, remove_ssid);
845 wpa_config_remove_network(wpa_s->conf, id);
846 }
847 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700848
849 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850}
851
852
853static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
854{
855 struct wpa_supplicant *wpa_s = eloop_ctx;
856 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
857 "out");
858 wpas_clear_wps(wpa_s);
859}
860
861
862static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800863 int registrar, const u8 *dev_addr,
864 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865{
866 struct wpa_ssid *ssid;
867
868 ssid = wpa_config_add_network(wpa_s->conf);
869 if (ssid == NULL)
870 return NULL;
871 wpas_notify_network_added(wpa_s, ssid);
872 wpa_config_set_network_defaults(ssid);
873 ssid->temporary = 1;
874 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
875 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
876 wpa_config_set(ssid, "identity", registrar ?
877 "\"" WSC_ID_REGISTRAR "\"" :
878 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
879 wpas_notify_network_removed(wpa_s, ssid);
880 wpa_config_remove_network(wpa_s->conf, ssid->id);
881 return NULL;
882 }
883
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800884#ifdef CONFIG_P2P
885 if (dev_addr)
886 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
887#endif /* CONFIG_P2P */
888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889 if (bssid) {
890#ifndef CONFIG_P2P
891 struct wpa_bss *bss;
892 int count = 0;
893#endif /* CONFIG_P2P */
894
895 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
896 ssid->bssid_set = 1;
897
898 /*
899 * Note: With P2P, the SSID may change at the time the WPS
900 * provisioning is started, so better not filter the AP based
901 * on the current SSID in the scan results.
902 */
903#ifndef CONFIG_P2P
904 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
905 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
906 continue;
907
908 os_free(ssid->ssid);
909 ssid->ssid = os_malloc(bss->ssid_len);
910 if (ssid->ssid == NULL)
911 break;
912 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
913 ssid->ssid_len = bss->ssid_len;
914 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
915 "scan results",
916 ssid->ssid, ssid->ssid_len);
917 count++;
918 }
919
920 if (count > 1) {
921 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
922 "for the AP; use wildcard");
923 os_free(ssid->ssid);
924 ssid->ssid = NULL;
925 ssid->ssid_len = 0;
926 }
927#endif /* CONFIG_P2P */
928 }
929
930 return ssid;
931}
932
933
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700934static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
935 struct wpa_ssid *selected)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936{
937 struct wpa_ssid *ssid;
938
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800939 if (wpa_s->current_ssid)
940 wpa_supplicant_deauthenticate(
941 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 /* Mark all other networks disabled and trigger reassociation */
944 ssid = wpa_s->conf->ssid;
945 while (ssid) {
946 int was_disabled = ssid->disabled;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800947 ssid->disabled_for_connect = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700948 /*
949 * In case the network object corresponds to a persistent group
950 * then do not send out network disabled signal. In addition,
951 * do not change disabled status of persistent network objects
952 * from 2 to 1 should we connect to another network.
953 */
954 if (was_disabled != 2) {
955 ssid->disabled = ssid != selected;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800956 if (was_disabled != ssid->disabled) {
957 if (ssid->disabled)
958 ssid->disabled_for_connect = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700959 wpas_notify_network_enabled_changed(wpa_s,
960 ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800961 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700962 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 ssid = ssid->next;
964 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700965}
966
967
968static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800969 struct wpa_ssid *selected, const u8 *bssid,
970 int freq)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700971{
972 struct wpa_bss *bss;
973
974 wpa_s->after_wps = 0;
975 wpa_s->known_wps_freq = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800976 if (freq) {
977 wpa_s->after_wps = 5;
978 wpa_s->wps_freq = freq;
979 } else if (bssid) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700980 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
981 if (bss && bss->freq > 0) {
982 wpa_s->known_wps_freq = 1;
983 wpa_s->wps_freq = bss->freq;
984 }
985 }
986
987 wpas_wps_temp_disable(wpa_s, selected);
988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989 wpa_s->disconnected = 0;
990 wpa_s->reassociate = 1;
991 wpa_s->scan_runs = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800992 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993 wpa_s->wps_success = 0;
994 wpa_s->blacklist_cleared = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800995
996 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 wpa_supplicant_req_scan(wpa_s, 0, 0);
998}
999
1000
1001int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1002 int p2p_group)
1003{
1004 struct wpa_ssid *ssid;
1005 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001006 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 if (ssid == NULL)
1008 return -1;
1009 ssid->temporary = 1;
1010 ssid->p2p_group = p2p_group;
1011#ifdef CONFIG_P2P
1012 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1013 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1014 if (ssid->ssid) {
1015 ssid->ssid_len = wpa_s->go_params->ssid_len;
1016 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1017 ssid->ssid_len);
1018 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1019 "SSID", ssid->ssid, ssid->ssid_len);
1020 }
1021 }
1022#endif /* CONFIG_P2P */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001023 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1024 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 if (wpa_s->wps_fragment_size)
1026 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1027 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1028 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001029 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 return 0;
1031}
1032
1033
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001034static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1035 const u8 *dev_addr, const u8 *bssid,
1036 const char *pin, int p2p_group, u16 dev_pw_id,
1037 const u8 *peer_pubkey_hash,
1038 const u8 *ssid_val, size_t ssid_len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039{
1040 struct wpa_ssid *ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001041 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 unsigned int rpin = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001043 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044
1045 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001046 if (bssid && is_zero_ether_addr(bssid))
1047 bssid = NULL;
1048 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1049 if (ssid == NULL) {
1050 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 ssid->temporary = 1;
1054 ssid->p2p_group = p2p_group;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001055 if (ssid_val) {
1056 ssid->ssid = os_malloc(ssid_len);
1057 if (ssid->ssid) {
1058 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1059 ssid->ssid_len = ssid_len;
1060 }
1061 }
1062 if (peer_pubkey_hash) {
1063 os_memcpy(hash, " pkhash=", 8);
1064 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1065 peer_pubkey_hash,
1066 WPS_OOB_PUBKEY_HASH_LEN);
1067 } else {
1068 hash[0] = '\0';
1069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070#ifdef CONFIG_P2P
1071 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1072 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1073 if (ssid->ssid) {
1074 ssid->ssid_len = wpa_s->go_params->ssid_len;
1075 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1076 ssid->ssid_len);
1077 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1078 "SSID", ssid->ssid, ssid->ssid_len);
1079 }
1080 }
1081#endif /* CONFIG_P2P */
1082 if (pin)
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001083 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1084 pin, dev_pw_id, hash);
1085 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1086 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1087 dev_pw_id, hash);
1088 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 rpin = wps_generate_pin();
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001090 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1091 rpin, dev_pw_id, hash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001093 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1094 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001095 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001096 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 if (wpa_s->wps_fragment_size)
1098 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1099 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1100 wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001101 wpa_s->wps_ap_iter = 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001102 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 return rpin;
1104}
1105
1106
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001107int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1108 const char *pin, int p2p_group, u16 dev_pw_id)
1109{
1110 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1111 dev_pw_id, NULL, NULL, 0, 0);
1112}
1113
1114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115/* Cancel the wps pbc/pin requests */
1116int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1117{
1118#ifdef CONFIG_AP
1119 if (wpa_s->ap_iface) {
1120 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1121 return wpa_supplicant_ap_wps_cancel(wpa_s);
1122 }
1123#endif /* CONFIG_AP */
1124
Dmitry Shmidt04949592012-07-19 12:16:46 -07001125 if (wpa_s->wpa_state == WPA_SCANNING ||
1126 wpa_s->wpa_state == WPA_DISCONNECTED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1128 wpa_supplicant_cancel_scan(wpa_s);
1129 wpas_clear_wps(wpa_s);
1130 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1131 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1132 "deauthenticate");
1133 wpa_supplicant_deauthenticate(wpa_s,
1134 WLAN_REASON_DEAUTH_LEAVING);
1135 wpas_clear_wps(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001136 } else {
1137 wpas_wps_reenable_networks(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001138 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001139 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1140 0)
1141 wpas_clear_wps(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 }
1143
Dmitry Shmidt051af732013-10-22 13:52:46 -07001144 wpa_s->after_wps = 0;
1145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 return 0;
1147}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148
1149
1150int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1151 const char *pin, struct wps_new_ap_settings *settings)
1152{
1153 struct wpa_ssid *ssid;
1154 char val[200];
1155 char *pos, *end;
1156 int res;
1157
1158 if (!pin)
1159 return -1;
1160 wpas_clear_wps(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001161 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 if (ssid == NULL)
1163 return -1;
1164 ssid->temporary = 1;
1165 pos = val;
1166 end = pos + sizeof(val);
1167 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1168 if (res < 0 || res >= end - pos)
1169 return -1;
1170 pos += res;
1171 if (settings) {
1172 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1173 "new_encr=%s new_key=%s",
1174 settings->ssid_hex, settings->auth,
1175 settings->encr, settings->key_hex);
1176 if (res < 0 || res >= end - pos)
1177 return -1;
1178 pos += res;
1179 }
1180 res = os_snprintf(pos, end - pos, "\"");
1181 if (res < 0 || res >= end - pos)
1182 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001183 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1184 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 if (wpa_s->wps_fragment_size)
1186 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1187 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1188 wpa_s, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001189 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 return 0;
1191}
1192
1193
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001194static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1195 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 size_t psk_len)
1197{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001198 if (is_zero_ether_addr(p2p_dev_addr)) {
1199 wpa_printf(MSG_DEBUG,
1200 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1201 MAC2STR(mac_addr));
1202 } else {
1203 wpa_printf(MSG_DEBUG,
1204 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1205 " P2P Device Addr " MACSTR,
1206 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1207 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1209
1210 /* TODO */
1211
1212 return 0;
1213}
1214
1215
1216static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1217 const struct wps_device_data *dev)
1218{
1219 char uuid[40], txt[400];
1220 int len;
1221 char devtype[WPS_DEV_TYPE_BUFSIZE];
1222 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1223 return;
1224 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1225 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1226 " [%s|%s|%s|%s|%s|%s]",
1227 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1228 dev->manufacturer, dev->model_name,
1229 dev->model_number, dev->serial_number,
1230 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1231 sizeof(devtype)));
1232 if (len > 0 && len < (int) sizeof(txt))
1233 wpa_printf(MSG_INFO, "%s", txt);
1234}
1235
1236
1237static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1238 u16 sel_reg_config_methods)
1239{
1240#ifdef CONFIG_WPS_ER
1241 struct wpa_supplicant *wpa_s = ctx;
1242
1243 if (wpa_s->wps_er == NULL)
1244 return;
1245 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1246 "dev_password_id=%u sel_reg_config_methods=0x%x",
1247 sel_reg, dev_passwd_id, sel_reg_config_methods);
1248 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1249 sel_reg_config_methods);
1250#endif /* CONFIG_WPS_ER */
1251}
1252
1253
1254static u16 wps_fix_config_methods(u16 config_methods)
1255{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 if ((config_methods &
1257 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1258 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1259 wpa_printf(MSG_INFO, "WPS: Converting display to "
1260 "virtual_display for WPS 2.0 compliance");
1261 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1262 }
1263 if ((config_methods &
1264 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1265 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1266 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1267 "virtual_push_button for WPS 2.0 compliance");
1268 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1269 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270
1271 return config_methods;
1272}
1273
1274
1275static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1276 struct wps_context *wps)
1277{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001278 char buf[50];
1279 const char *src;
1280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 if (is_nil_uuid(wpa_s->conf->uuid)) {
1282 struct wpa_supplicant *first;
1283 first = wpa_s->global->ifaces;
1284 while (first && first->next)
1285 first = first->next;
1286 if (first && first != wpa_s) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001287 if (wps != wpa_s->global->ifaces->wps)
1288 os_memcpy(wps->uuid,
1289 wpa_s->global->ifaces->wps->uuid,
1290 WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001291 src = "from the first interface";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 } else {
1293 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001294 src = "based on MAC address";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295 }
1296 } else {
1297 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001298 src = "based on configuration";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001300
1301 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1302 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303}
1304
1305
Dmitry Shmidt04949592012-07-19 12:16:46 -07001306static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1307 struct wps_context *wps)
1308{
1309 wpabuf_free(wps->dev.vendor_ext_m1);
1310 wps->dev.vendor_ext_m1 = NULL;
1311
1312 if (wpa_s->conf->wps_vendor_ext_m1) {
1313 wps->dev.vendor_ext_m1 =
1314 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1315 if (!wps->dev.vendor_ext_m1) {
1316 wpa_printf(MSG_ERROR, "WPS: Cannot "
1317 "allocate memory for vendor_ext_m1");
1318 }
1319 }
1320}
1321
1322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323int wpas_wps_init(struct wpa_supplicant *wpa_s)
1324{
1325 struct wps_context *wps;
1326 struct wps_registrar_config rcfg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001327 struct hostapd_hw_modes *modes;
1328 u16 m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329
1330 wps = os_zalloc(sizeof(*wps));
1331 if (wps == NULL)
1332 return -1;
1333
1334 wps->cred_cb = wpa_supplicant_wps_cred;
1335 wps->event_cb = wpa_supplicant_wps_event;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001336 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 wps->cb_ctx = wpa_s;
1338
1339 wps->dev.device_name = wpa_s->conf->device_name;
1340 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1341 wps->dev.model_name = wpa_s->conf->model_name;
1342 wps->dev.model_number = wpa_s->conf->model_number;
1343 wps->dev.serial_number = wpa_s->conf->serial_number;
1344 wps->config_methods =
1345 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1346 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1347 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1348 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1349 "methods are not allowed at the same time");
1350 os_free(wps);
1351 return -1;
1352 }
1353 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001354 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1356 WPS_DEV_TYPE_LEN);
1357
1358 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1359 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1360 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1361
Dmitry Shmidt04949592012-07-19 12:16:46 -07001362 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001365 modes = wpa_s->hw.modes;
1366 if (modes) {
1367 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1368 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1369 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1370 wps->dev.rf_bands |= WPS_RF_24GHZ;
1371 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1372 wps->dev.rf_bands |= WPS_RF_50GHZ;
1373 }
1374 }
1375 if (wps->dev.rf_bands == 0) {
1376 /*
1377 * Default to claiming support for both bands if the driver
1378 * does not provide support for fetching supported bands.
1379 */
1380 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1381 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1383 wpas_wps_set_uuid(wpa_s, wps);
1384
1385 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1386 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1387
1388 os_memset(&rcfg, 0, sizeof(rcfg));
1389 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1390 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1391 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1392 rcfg.cb_ctx = wpa_s;
1393
1394 wps->registrar = wps_registrar_init(wps, &rcfg);
1395 if (wps->registrar == NULL) {
1396 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1397 os_free(wps);
1398 return -1;
1399 }
1400
1401 wpa_s->wps = wps;
1402
1403 return 0;
1404}
1405
1406
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001407#ifdef CONFIG_WPS_ER
1408static void wpas_wps_nfc_clear(struct wps_context *wps)
1409{
1410 wps->ap_nfc_dev_pw_id = 0;
1411 wpabuf_free(wps->ap_nfc_dh_pubkey);
1412 wps->ap_nfc_dh_pubkey = NULL;
1413 wpabuf_free(wps->ap_nfc_dh_privkey);
1414 wps->ap_nfc_dh_privkey = NULL;
1415 wpabuf_free(wps->ap_nfc_dev_pw);
1416 wps->ap_nfc_dev_pw = NULL;
1417}
1418#endif /* CONFIG_WPS_ER */
1419
1420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1422{
1423 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001424 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001425 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001426 wpas_wps_clear_ap_info(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427
1428 if (wpa_s->wps == NULL)
1429 return;
1430
1431#ifdef CONFIG_WPS_ER
1432 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1433 wpa_s->wps_er = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001434 wpas_wps_nfc_clear(wpa_s->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435#endif /* CONFIG_WPS_ER */
1436
1437 wps_registrar_deinit(wpa_s->wps->registrar);
1438 wpabuf_free(wpa_s->wps->dh_pubkey);
1439 wpabuf_free(wpa_s->wps->dh_privkey);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001440 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 os_free(wpa_s->wps->network_key);
1442 os_free(wpa_s->wps);
1443 wpa_s->wps = NULL;
1444}
1445
1446
1447int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001448 struct wpa_ssid *ssid, struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449{
1450 struct wpabuf *wps_ie;
1451
1452 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1453 return -1;
1454
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001455 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1457 if (!wps_ie) {
1458 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1459 return 0;
1460 }
1461
1462 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1463 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1464 "without active PBC Registrar");
1465 wpabuf_free(wps_ie);
1466 return 0;
1467 }
1468
1469 /* TODO: overlap detection */
1470 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1471 "(Active PBC)");
1472 wpabuf_free(wps_ie);
1473 return 1;
1474 }
1475
1476 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1477 if (!wps_ie) {
1478 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1479 return 0;
1480 }
1481
1482 /*
1483 * Start with WPS APs that advertise our address as an
1484 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1485 * allow any WPS AP after couple of scans since some APs do not
1486 * set Selected Registrar attribute properly when using
1487 * external Registrar.
1488 */
1489 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1490 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1491 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1492 "without active PIN Registrar");
1493 wpabuf_free(wps_ie);
1494 return 0;
1495 }
1496 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1497 } else {
1498 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1499 "(Authorized MAC or Active PIN)");
1500 }
1501 wpabuf_free(wps_ie);
1502 return 1;
1503 }
1504
1505 if (wps_ie) {
1506 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1507 wpabuf_free(wps_ie);
1508 return 1;
1509 }
1510
1511 return -1;
1512}
1513
1514
1515int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1516 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001517 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518{
1519 struct wpabuf *wps_ie = NULL;
1520 int ret = 0;
1521
1522 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001523 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1525 /* allow wildcard SSID for WPS PBC */
1526 ret = 1;
1527 }
1528 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001529 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 if (wps_ie &&
1531 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1532 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1533 /* allow wildcard SSID for WPS PIN */
1534 ret = 1;
1535 }
1536 }
1537
1538 if (!ret && ssid->bssid_set &&
1539 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1540 /* allow wildcard SSID due to hardcoded BSSID match */
1541 ret = 1;
1542 }
1543
1544#ifdef CONFIG_WPS_STRICT
1545 if (wps_ie) {
1546 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1547 0, bss->bssid) < 0)
1548 ret = 0;
1549 if (bss->beacon_ie_len) {
1550 struct wpabuf *bcn_wps;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001551 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001552 bss, WPS_IE_VENDOR_TYPE);
1553 if (bcn_wps == NULL) {
1554 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1555 "missing from AP Beacon");
1556 ret = 0;
1557 } else {
1558 if (wps_validate_beacon(wps_ie) < 0)
1559 ret = 0;
1560 wpabuf_free(bcn_wps);
1561 }
1562 }
1563 }
1564#endif /* CONFIG_WPS_STRICT */
1565
1566 wpabuf_free(wps_ie);
1567
1568 return ret;
1569}
1570
1571
1572int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1573 struct wpa_bss *selected, struct wpa_ssid *ssid)
1574{
1575 const u8 *sel_uuid, *uuid;
1576 struct wpabuf *wps_ie;
1577 int ret = 0;
1578 struct wpa_bss *bss;
1579
1580 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1581 return 0;
1582
1583 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1584 "present in scan results; selected BSSID " MACSTR,
1585 MAC2STR(selected->bssid));
1586
1587 /* Make sure that only one AP is in active PBC mode */
1588 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1589 if (wps_ie) {
1590 sel_uuid = wps_get_uuid_e(wps_ie);
1591 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1592 sel_uuid, UUID_LEN);
1593 } else {
1594 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1595 "WPS IE?!");
1596 sel_uuid = NULL;
1597 }
1598
1599 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1600 struct wpabuf *ie;
1601 if (bss == selected)
1602 continue;
1603 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1604 if (!ie)
1605 continue;
1606 if (!wps_is_selected_pbc_registrar(ie)) {
1607 wpabuf_free(ie);
1608 continue;
1609 }
1610 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1611 MACSTR, MAC2STR(bss->bssid));
1612 uuid = wps_get_uuid_e(ie);
1613 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1614 uuid, UUID_LEN);
1615 if (sel_uuid == NULL || uuid == NULL ||
1616 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1617 ret = 1; /* PBC overlap */
1618 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1619 MACSTR " and " MACSTR,
1620 MAC2STR(selected->bssid),
1621 MAC2STR(bss->bssid));
1622 wpabuf_free(ie);
1623 break;
1624 }
1625
1626 /* TODO: verify that this is reasonable dual-band situation */
1627
1628 wpabuf_free(ie);
1629 }
1630
1631 wpabuf_free(wps_ie);
1632
1633 return ret;
1634}
1635
1636
1637void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1638{
1639 struct wpa_bss *bss;
1640 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1641
1642 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1643 return;
1644
1645 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1646 struct wpabuf *ie;
1647 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1648 if (!ie)
1649 continue;
1650 if (wps_is_selected_pbc_registrar(ie))
1651 pbc++;
1652 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1653 auth++;
1654 else if (wps_is_selected_pin_registrar(ie))
1655 pin++;
1656 else
1657 wps++;
1658 wpabuf_free(ie);
1659 }
1660
1661 if (pbc)
1662 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1663 else if (auth)
1664 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1665 else if (pin)
1666 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1667 else if (wps)
1668 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1669}
1670
1671
1672int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1673{
1674 struct wpa_ssid *ssid;
1675
1676 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1677 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1678 return 1;
1679 }
1680
1681 return 0;
1682}
1683
1684
1685int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1686 char *end)
1687{
1688 struct wpabuf *wps_ie;
1689 int ret;
1690
1691 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1692 if (wps_ie == NULL)
1693 return 0;
1694
1695 ret = wps_attr_text(wps_ie, buf, end);
1696 wpabuf_free(wps_ie);
1697 return ret;
1698}
1699
1700
1701int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1702{
1703#ifdef CONFIG_WPS_ER
1704 if (wpa_s->wps_er) {
1705 wps_er_refresh(wpa_s->wps_er);
1706 return 0;
1707 }
1708 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1709 if (wpa_s->wps_er == NULL)
1710 return -1;
1711 return 0;
1712#else /* CONFIG_WPS_ER */
1713 return 0;
1714#endif /* CONFIG_WPS_ER */
1715}
1716
1717
1718int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1719{
1720#ifdef CONFIG_WPS_ER
1721 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1722 wpa_s->wps_er = NULL;
1723#endif /* CONFIG_WPS_ER */
1724 return 0;
1725}
1726
1727
1728#ifdef CONFIG_WPS_ER
1729int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1730 const char *uuid, const char *pin)
1731{
1732 u8 u[UUID_LEN];
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001733 const u8 *use_uuid = NULL;
1734 u8 addr_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001736 if (os_strcmp(uuid, "any") == 0) {
1737 } else if (uuid_str2bin(uuid, u) == 0) {
1738 use_uuid = u;
1739 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1740 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1741 if (use_uuid == NULL)
1742 return -1;
1743 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001744 return -1;
1745 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001746 use_uuid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 (const u8 *) pin, os_strlen(pin), 300);
1748}
1749
1750
1751int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1752{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001753 u8 u[UUID_LEN], *use_uuid = NULL;
1754 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001756 if (uuid_str2bin(uuid, u) == 0)
1757 use_uuid = u;
1758 else if (hwaddr_aton(uuid, addr) == 0)
1759 use_addr = addr;
1760 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001762 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763}
1764
1765
1766int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1767 const char *pin)
1768{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001769 u8 u[UUID_LEN], *use_uuid = NULL;
1770 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001772 if (uuid_str2bin(uuid, u) == 0)
1773 use_uuid = u;
1774 else if (hwaddr_aton(uuid, addr) == 0)
1775 use_addr = addr;
1776 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001778
1779 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 os_strlen(pin));
1781}
1782
1783
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001784static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1785 struct wps_credential *cred)
1786{
1787 os_memset(cred, 0, sizeof(*cred));
1788 if (ssid->ssid_len > 32)
1789 return -1;
1790 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1791 cred->ssid_len = ssid->ssid_len;
1792 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1793 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1794 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1795 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1796 cred->encr_type = WPS_ENCR_AES;
1797 else
1798 cred->encr_type = WPS_ENCR_TKIP;
1799 if (ssid->passphrase) {
1800 cred->key_len = os_strlen(ssid->passphrase);
1801 if (cred->key_len >= 64)
1802 return -1;
1803 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1804 } else if (ssid->psk_set) {
1805 cred->key_len = 32;
1806 os_memcpy(cred->key, ssid->psk, 32);
1807 } else
1808 return -1;
1809 } else {
1810 cred->auth_type = WPS_AUTH_OPEN;
1811 cred->encr_type = WPS_ENCR_NONE;
1812 }
1813
1814 return 0;
1815}
1816
1817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1819 int id)
1820{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001821 u8 u[UUID_LEN], *use_uuid = NULL;
1822 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 struct wpa_ssid *ssid;
1824 struct wps_credential cred;
1825
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001826 if (uuid_str2bin(uuid, u) == 0)
1827 use_uuid = u;
1828 else if (hwaddr_aton(uuid, addr) == 0)
1829 use_addr = addr;
1830 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831 return -1;
1832 ssid = wpa_config_get_network(wpa_s->conf, id);
1833 if (ssid == NULL || ssid->ssid == NULL)
1834 return -1;
1835
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001836 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001838 return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839}
1840
1841
1842int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1843 const char *pin, struct wps_new_ap_settings *settings)
1844{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001845 u8 u[UUID_LEN], *use_uuid = NULL;
1846 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847 struct wps_credential cred;
1848 size_t len;
1849
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001850 if (uuid_str2bin(uuid, u) == 0)
1851 use_uuid = u;
1852 else if (hwaddr_aton(uuid, addr) == 0)
1853 use_addr = addr;
1854 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 return -1;
1856 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1857 settings->encr == NULL || settings->key_hex == NULL)
1858 return -1;
1859
1860 os_memset(&cred, 0, sizeof(cred));
1861 len = os_strlen(settings->ssid_hex);
1862 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1863 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1864 return -1;
1865 cred.ssid_len = len / 2;
1866
1867 len = os_strlen(settings->key_hex);
1868 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1869 hexstr2bin(settings->key_hex, cred.key, len / 2))
1870 return -1;
1871 cred.key_len = len / 2;
1872
1873 if (os_strcmp(settings->auth, "OPEN") == 0)
1874 cred.auth_type = WPS_AUTH_OPEN;
1875 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1876 cred.auth_type = WPS_AUTH_WPAPSK;
1877 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1878 cred.auth_type = WPS_AUTH_WPA2PSK;
1879 else
1880 return -1;
1881
1882 if (os_strcmp(settings->encr, "NONE") == 0)
1883 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001884#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 else if (os_strcmp(settings->encr, "WEP") == 0)
1886 cred.encr_type = WPS_ENCR_WEP;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001887#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 else if (os_strcmp(settings->encr, "TKIP") == 0)
1889 cred.encr_type = WPS_ENCR_TKIP;
1890 else if (os_strcmp(settings->encr, "CCMP") == 0)
1891 cred.encr_type = WPS_ENCR_AES;
1892 else
1893 return -1;
1894
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001895 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
1896 (const u8 *) pin, os_strlen(pin), &cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897}
1898
1899
Dmitry Shmidt04949592012-07-19 12:16:46 -07001900#ifdef CONFIG_WPS_NFC
1901struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1902 int ndef, const char *uuid)
1903{
1904 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001905 u8 u[UUID_LEN], *use_uuid = NULL;
1906 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001907
1908 if (!wpa_s->wps_er)
1909 return NULL;
1910
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001911 if (uuid_str2bin(uuid, u) == 0)
1912 use_uuid = u;
1913 else if (hwaddr_aton(uuid, addr) == 0)
1914 use_addr = addr;
1915 else
Dmitry Shmidt04949592012-07-19 12:16:46 -07001916 return NULL;
1917
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07001918 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001919 if (ndef && ret) {
1920 struct wpabuf *tmp;
1921 tmp = ndef_build_wifi(ret);
1922 wpabuf_free(ret);
1923 if (tmp == NULL)
1924 return NULL;
1925 ret = tmp;
1926 }
1927
1928 return ret;
1929}
1930#endif /* CONFIG_WPS_NFC */
1931
1932
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933static int callbacks_pending = 0;
1934
1935static void wpas_wps_terminate_cb(void *ctx)
1936{
1937 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1938 if (--callbacks_pending <= 0)
1939 eloop_terminate();
1940}
1941#endif /* CONFIG_WPS_ER */
1942
1943
1944int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1945{
1946#ifdef CONFIG_WPS_ER
1947 if (wpa_s->wps_er) {
1948 callbacks_pending++;
1949 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1950 wpa_s->wps_er = NULL;
1951 return 1;
1952 }
1953#endif /* CONFIG_WPS_ER */
1954 return 0;
1955}
1956
1957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001958void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1959{
1960 struct wps_context *wps = wpa_s->wps;
1961
1962 if (wps == NULL)
1963 return;
1964
1965 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1966 wps->config_methods = wps_config_methods_str2bin(
1967 wpa_s->conf->config_methods);
1968 if ((wps->config_methods &
1969 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1970 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1971 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1972 "config methods are not allowed at the "
1973 "same time");
1974 wps->config_methods &= ~WPS_CONFIG_LABEL;
1975 }
1976 }
1977 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linff2bda62012-06-26 11:04:38 +08001978 wps->dev.config_methods = wps->config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979
1980 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
1981 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1982 WPS_DEV_TYPE_LEN);
1983
1984 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
1985 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1986 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1987 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
1988 }
1989
Dmitry Shmidt04949592012-07-19 12:16:46 -07001990 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
1991 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1994 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1995
1996 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
1997 wpas_wps_set_uuid(wpa_s, wps);
1998
1999 if (wpa_s->conf->changed_parameters &
2000 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2001 /* Update pointers to make sure they refer current values */
2002 wps->dev.device_name = wpa_s->conf->device_name;
2003 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2004 wps->dev.model_name = wpa_s->conf->model_name;
2005 wps->dev.model_number = wpa_s->conf->model_number;
2006 wps->dev.serial_number = wpa_s->conf->serial_number;
2007 }
2008}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002009
2010
2011#ifdef CONFIG_WPS_NFC
2012
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002013#ifdef CONFIG_WPS_ER
2014static struct wpabuf *
2015wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2016 struct wpa_ssid *ssid)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002017{
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002018 struct wpabuf *ret;
2019 struct wps_credential cred;
2020
2021 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2022 return NULL;
2023
2024 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2025
2026 if (ndef && ret) {
2027 struct wpabuf *tmp;
2028 tmp = ndef_build_wifi(ret);
2029 wpabuf_free(ret);
2030 if (tmp == NULL)
2031 return NULL;
2032 ret = tmp;
2033 }
2034
2035 return ret;
2036}
2037#endif /* CONFIG_WPS_ER */
2038
2039
2040struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2041 int ndef, const char *id_str)
2042{
2043#ifdef CONFIG_WPS_ER
2044 if (id_str) {
2045 int id;
2046 char *end = NULL;
2047 struct wpa_ssid *ssid;
2048
2049 id = strtol(id_str, &end, 10);
2050 if (end && *end)
2051 return NULL;
2052
2053 ssid = wpa_config_get_network(wpa_s->conf, id);
2054 if (ssid == NULL)
2055 return NULL;
2056 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2057 }
2058#endif /* CONFIG_WPS_ER */
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002059#ifdef CONFIG_AP
2060 if (wpa_s->ap_iface)
2061 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2062#endif /* CONFIG_AP */
2063 return NULL;
2064}
2065
2066
Dmitry Shmidt04949592012-07-19 12:16:46 -07002067struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2068{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002069 if (wpa_s->conf->wps_nfc_pw_from_config) {
2070 return wps_nfc_token_build(ndef,
2071 wpa_s->conf->wps_nfc_dev_pw_id,
2072 wpa_s->conf->wps_nfc_dh_pubkey,
2073 wpa_s->conf->wps_nfc_dev_pw);
2074 }
2075
Dmitry Shmidt04949592012-07-19 12:16:46 -07002076 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2077 &wpa_s->conf->wps_nfc_dh_pubkey,
2078 &wpa_s->conf->wps_nfc_dh_privkey,
2079 &wpa_s->conf->wps_nfc_dev_pw);
2080}
2081
2082
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002083int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2084 const u8 *bssid,
2085 const struct wpabuf *dev_pw, u16 dev_pw_id,
2086 int p2p_group, const u8 *peer_pubkey_hash,
2087 const u8 *ssid, size_t ssid_len, int freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002088{
2089 struct wps_context *wps = wpa_s->wps;
2090 char pw[32 * 2 + 1];
2091
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002092 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2093 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2094 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2095 }
2096
Dmitry Shmidt04949592012-07-19 12:16:46 -07002097 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002098 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2099 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2100 "cannot start NFC-triggered connection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002101 return -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002102 }
2103
2104 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2105 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2106 "cannot start NFC-triggered connection", dev_pw_id);
2107 return -1;
2108 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002109
2110 dh5_free(wps->dh_ctx);
2111 wpabuf_free(wps->dh_pubkey);
2112 wpabuf_free(wps->dh_privkey);
2113 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2114 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2115 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2116 wps->dh_ctx = NULL;
2117 wpabuf_free(wps->dh_pubkey);
2118 wps->dh_pubkey = NULL;
2119 wpabuf_free(wps->dh_privkey);
2120 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002121 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002122 return -1;
2123 }
2124 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002125 if (wps->dh_ctx == NULL) {
2126 wpabuf_free(wps->dh_pubkey);
2127 wps->dh_pubkey = NULL;
2128 wpabuf_free(wps->dh_privkey);
2129 wps->dh_privkey = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002130 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002131 return -1;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002132 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002133
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002134 if (dev_pw) {
2135 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2136 wpabuf_head(dev_pw),
2137 wpabuf_len(dev_pw));
2138 }
2139 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2140 dev_pw ? pw : NULL,
2141 p2p_group, dev_pw_id, peer_pubkey_hash,
2142 ssid, ssid_len, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002143}
2144
2145
2146static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2147 struct wps_parse_attr *attr)
2148{
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002149 /*
2150 * Disable existing networks temporarily to allow the newly learned
2151 * credential to be preferred. Enable the temporarily disabled networks
2152 * after 10 seconds.
2153 */
2154 wpas_wps_temp_disable(wpa_s, NULL);
2155 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2156 NULL);
2157
Dmitry Shmidt04949592012-07-19 12:16:46 -07002158 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2159 return -1;
2160
2161 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2162 return 0;
2163
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002164 if (attr->ap_channel) {
2165 u16 chan = WPA_GET_BE16(attr->ap_channel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002166 int freq = 0;
2167
2168 if (chan >= 1 && chan <= 13)
2169 freq = 2407 + 5 * chan;
2170 else if (chan == 14)
2171 freq = 2484;
2172 else if (chan >= 30)
2173 freq = 5000 + 5 * chan;
2174
2175 if (freq) {
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002176 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2177 chan, freq);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002178 wpa_s->after_wps = 5;
2179 wpa_s->wps_freq = freq;
2180 }
2181 }
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002182
2183 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2184 "based on the received credential added");
2185 wpa_s->normal_scans = 0;
2186 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002187 wpa_s->disconnected = 0;
2188 wpa_s->reassociate = 1;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002189
2190 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002191 wpa_supplicant_req_scan(wpa_s, 0, 0);
2192
2193 return 0;
2194}
2195
2196
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002197#ifdef CONFIG_WPS_ER
Dmitry Shmidt04949592012-07-19 12:16:46 -07002198static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2199 struct wps_parse_attr *attr)
2200{
2201 return wps_registrar_add_nfc_password_token(
2202 wpa_s->wps->registrar, attr->oob_dev_password,
2203 attr->oob_dev_password_len);
2204}
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002205#endif /* CONFIG_WPS_ER */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002206
2207
2208static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2209 const struct wpabuf *wps)
2210{
2211 struct wps_parse_attr attr;
2212
2213 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2214
2215 if (wps_parse_msg(wps, &attr)) {
2216 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2217 return -1;
2218 }
2219
2220 if (attr.num_cred)
2221 return wpas_wps_use_cred(wpa_s, &attr);
2222
2223#ifdef CONFIG_WPS_ER
2224 if (attr.oob_dev_password)
2225 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2226#endif /* CONFIG_WPS_ER */
2227
2228 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2229 return -1;
2230}
2231
2232
2233int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002234 const struct wpabuf *data, int forced_freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002235{
2236 const struct wpabuf *wps = data;
2237 struct wpabuf *tmp = NULL;
2238 int ret;
2239
2240 if (wpabuf_len(data) < 4)
2241 return -1;
2242
2243 if (*wpabuf_head_u8(data) != 0x10) {
2244 /* Assume this contains full NDEF record */
2245 tmp = ndef_parse_wifi(data);
2246 if (tmp == NULL) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002247#ifdef CONFIG_P2P
2248 tmp = ndef_parse_p2p(data);
2249 if (tmp) {
2250 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2251 forced_freq);
2252 wpabuf_free(tmp);
2253 return ret;
2254 }
2255#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002256 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2257 return -1;
2258 }
2259 wps = tmp;
2260 }
2261
2262 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2263 wpabuf_free(tmp);
2264 return ret;
2265}
2266
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002267
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002268struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2269 int ndef)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002270{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002271 struct wpabuf *ret;
2272
2273 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2274 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2275 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2276 return NULL;
2277
2278 ret = wps_build_nfc_handover_req(wpa_s->wps,
2279 wpa_s->conf->wps_nfc_dh_pubkey);
2280
2281 if (ndef && ret) {
2282 struct wpabuf *tmp;
2283 tmp = ndef_build_wifi(ret);
2284 wpabuf_free(ret);
2285 if (tmp == NULL)
2286 return NULL;
2287 ret = tmp;
2288 }
2289
2290 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002291}
2292
2293
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002294#ifdef CONFIG_WPS_NFC
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002295
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002296static struct wpabuf *
2297wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2298 const char *uuid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002299{
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002300#ifdef CONFIG_WPS_ER
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002301 struct wpabuf *ret;
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002302 u8 u[UUID_LEN], *use_uuid = NULL;
2303 u8 addr[ETH_ALEN], *use_addr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002304 struct wps_context *wps = wpa_s->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002305
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002306 if (wps == NULL)
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002307 return NULL;
2308
Dmitry Shmidt1e78e762013-04-02 11:05:36 -07002309 if (uuid == NULL)
2310 return NULL;
2311 if (uuid_str2bin(uuid, u) == 0)
2312 use_uuid = u;
2313 else if (hwaddr_aton(uuid, addr) == 0)
2314 use_addr = addr;
2315 else
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002316 return NULL;
2317
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002318 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2319 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2320 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2321 return NULL;
2322 }
2323
2324 wpas_wps_nfc_clear(wps);
2325 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2326 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2327 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2328 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2329 wpas_wps_nfc_clear(wps);
2330 return NULL;
2331 }
2332
2333 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2334 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002335 if (ndef && ret) {
2336 struct wpabuf *tmp;
2337 tmp = ndef_build_wifi(ret);
2338 wpabuf_free(ret);
2339 if (tmp == NULL)
2340 return NULL;
2341 ret = tmp;
2342 }
2343
2344 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002345#else /* CONFIG_WPS_ER */
2346 return NULL;
2347#endif /* CONFIG_WPS_ER */
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002348}
2349#endif /* CONFIG_WPS_NFC */
2350
2351
2352struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2353 int ndef, int cr, const char *uuid)
2354{
2355 struct wpabuf *ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002356 if (!cr)
2357 return NULL;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002358 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2359 if (ret)
2360 return ret;
2361 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002362}
2363
2364
Dmitry Shmidt21de2142014-04-08 10:50:52 -07002365static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2366 const struct wpabuf *data)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002367{
2368 struct wpabuf *wps;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002369 int ret = -1;
2370 u16 wsc_len;
2371 const u8 *pos;
2372 struct wpabuf msg;
2373 struct wps_parse_attr attr;
2374 u16 dev_pw_id;
2375 const u8 *bssid = NULL;
2376 int freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002377
2378 wps = ndef_parse_wifi(data);
2379 if (wps == NULL)
2380 return -1;
2381 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2382 "payload from NFC connection handover");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002383 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2384 if (wpabuf_len(wps) < 2) {
2385 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2386 "Message");
2387 goto out;
2388 }
2389 pos = wpabuf_head(wps);
2390 wsc_len = WPA_GET_BE16(pos);
2391 if (wsc_len > wpabuf_len(wps) - 2) {
2392 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2393 "in Wi-Fi Handover Select Message", wsc_len);
2394 goto out;
2395 }
2396 pos += 2;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002397
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002398 wpa_hexdump(MSG_DEBUG,
2399 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2400 pos, wsc_len);
2401 if (wsc_len < wpabuf_len(wps) - 2) {
2402 wpa_hexdump(MSG_DEBUG,
2403 "WPS: Ignore extra data after WSC attributes",
2404 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2405 }
2406
2407 wpabuf_set(&msg, pos, wsc_len);
2408 ret = wps_parse_msg(&msg, &attr);
2409 if (ret < 0) {
2410 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2411 "Wi-Fi Handover Select Message");
2412 goto out;
2413 }
2414
2415 if (attr.oob_dev_password == NULL ||
2416 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2417 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2418 "included in Wi-Fi Handover Select Message");
2419 ret = -1;
2420 goto out;
2421 }
2422
2423 if (attr.ssid == NULL) {
2424 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2425 "Select Message");
2426 ret = -1;
2427 goto out;
2428 }
2429
2430 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2431
2432 if (attr.mac_addr) {
2433 bssid = attr.mac_addr;
2434 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2435 MAC2STR(bssid));
2436 }
2437
2438 if (attr.rf_bands)
2439 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2440
2441 if (attr.ap_channel) {
2442 u16 chan = WPA_GET_BE16(attr.ap_channel);
2443
2444 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2445
2446 if (chan >= 1 && chan <= 13 &&
2447 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2448 freq = 2407 + 5 * chan;
2449 else if (chan == 14 &&
2450 (attr.rf_bands == NULL ||
2451 *attr.rf_bands & WPS_RF_24GHZ))
2452 freq = 2484;
2453 else if (chan >= 30 &&
2454 (attr.rf_bands == NULL ||
2455 *attr.rf_bands & WPS_RF_50GHZ))
2456 freq = 5000 + 5 * chan;
2457
2458 if (freq) {
2459 wpa_printf(MSG_DEBUG,
2460 "WPS: AP indicated channel %u -> %u MHz",
2461 chan, freq);
2462 }
2463 }
2464
2465 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2466 attr.oob_dev_password, attr.oob_dev_password_len);
2467 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2468 WPS_OOB_PUBKEY_HASH_LEN);
2469 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2470 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2471 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2472 ret = -1;
2473 goto out;
2474 }
2475 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2476 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2477
2478 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2479 attr.oob_dev_password,
2480 attr.ssid, attr.ssid_len, freq);
2481
2482out:
2483 wpabuf_free(wps);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002484 return ret;
2485}
2486
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002487
2488int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2489 const struct wpabuf *req,
2490 const struct wpabuf *sel)
2491{
2492 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2493 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2494 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2495 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2496}
2497
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002498
2499int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2500 const struct wpabuf *req,
2501 const struct wpabuf *sel)
2502{
2503 struct wpabuf *wps;
2504 int ret = -1;
2505 u16 wsc_len;
2506 const u8 *pos;
2507 struct wpabuf msg;
2508 struct wps_parse_attr attr;
2509 u16 dev_pw_id;
2510
2511 /*
2512 * Enrollee/station is always initiator of the NFC connection handover,
2513 * so use the request message here to find Enrollee public key hash.
2514 */
2515 wps = ndef_parse_wifi(req);
2516 if (wps == NULL)
2517 return -1;
2518 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2519 "payload from NFC connection handover");
2520 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2521 if (wpabuf_len(wps) < 2) {
2522 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2523 "Message");
2524 goto out;
2525 }
2526 pos = wpabuf_head(wps);
2527 wsc_len = WPA_GET_BE16(pos);
2528 if (wsc_len > wpabuf_len(wps) - 2) {
2529 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2530 "in rt Wi-Fi Handover Request Message", wsc_len);
2531 goto out;
2532 }
2533 pos += 2;
2534
2535 wpa_hexdump(MSG_DEBUG,
2536 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2537 pos, wsc_len);
2538 if (wsc_len < wpabuf_len(wps) - 2) {
2539 wpa_hexdump(MSG_DEBUG,
2540 "WPS: Ignore extra data after WSC attributes",
2541 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2542 }
2543
2544 wpabuf_set(&msg, pos, wsc_len);
2545 ret = wps_parse_msg(&msg, &attr);
2546 if (ret < 0) {
2547 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2548 "Wi-Fi Handover Request Message");
2549 goto out;
2550 }
2551
2552 if (attr.oob_dev_password == NULL ||
2553 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2554 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2555 "included in Wi-Fi Handover Request Message");
2556 ret = -1;
2557 goto out;
2558 }
2559
2560 if (attr.uuid_e == NULL) {
2561 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2562 "Handover Request Message");
2563 ret = -1;
2564 goto out;
2565 }
2566
2567 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2568
2569 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2570 attr.oob_dev_password, attr.oob_dev_password_len);
2571 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2572 WPS_OOB_PUBKEY_HASH_LEN);
2573 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2574 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2575 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2576 ret = -1;
2577 goto out;
2578 }
2579 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2580 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2581
2582 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2583 attr.oob_dev_password,
2584 DEV_PW_NFC_CONNECTION_HANDOVER,
2585 NULL, 0, 1);
2586
2587out:
2588 wpabuf_free(wps);
2589 return ret;
2590}
2591
Dmitry Shmidt04949592012-07-19 12:16:46 -07002592#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002593
2594
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002595static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2596{
2597 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002598 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002599
2600 if (wpa_debug_level > MSG_DEBUG)
2601 return;
2602
2603 if (wpa_s->wps_ap == NULL)
2604 return;
2605
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002606 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002607
2608 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2609 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2610 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2611
2612 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2613 "tries=%d last_attempt=%d sec ago blacklist=%d",
2614 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2615 ap->last_attempt.sec > 0 ?
2616 (int) now.sec - (int) ap->last_attempt.sec : -1,
2617 e ? e->count : 0);
2618 }
2619}
2620
2621
2622static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2623 const u8 *bssid)
2624{
2625 size_t i;
2626
2627 if (wpa_s->wps_ap == NULL)
2628 return NULL;
2629
2630 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2631 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2632 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2633 return ap;
2634 }
2635
2636 return NULL;
2637}
2638
2639
2640static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2641 struct wpa_scan_res *res)
2642{
2643 struct wpabuf *wps;
2644 enum wps_ap_info_type type;
2645 struct wps_ap_info *ap;
2646 int r;
2647
2648 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2649 return;
2650
2651 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2652 if (wps == NULL)
2653 return;
2654
2655 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2656 if (r == 2)
2657 type = WPS_AP_SEL_REG_OUR;
2658 else if (r == 1)
2659 type = WPS_AP_SEL_REG;
2660 else
2661 type = WPS_AP_NOT_SEL_REG;
2662
2663 wpabuf_free(wps);
2664
2665 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2666 if (ap) {
2667 if (ap->type != type) {
2668 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2669 " changed type %d -> %d",
2670 MAC2STR(res->bssid), ap->type, type);
2671 ap->type = type;
2672 if (type != WPS_AP_NOT_SEL_REG)
2673 wpa_blacklist_del(wpa_s, ap->bssid);
2674 }
2675 return;
2676 }
2677
2678 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2679 sizeof(struct wps_ap_info));
2680 if (ap == NULL)
2681 return;
2682
2683 wpa_s->wps_ap = ap;
2684 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2685 wpa_s->num_wps_ap++;
2686
2687 os_memset(ap, 0, sizeof(*ap));
2688 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2689 ap->type = type;
2690 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2691 MAC2STR(ap->bssid), ap->type);
2692}
2693
2694
2695void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2696 struct wpa_scan_results *scan_res)
2697{
2698 size_t i;
2699
2700 for (i = 0; i < scan_res->num; i++)
2701 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2702
2703 wpas_wps_dump_ap_info(wpa_s);
2704}
2705
2706
2707void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2708{
2709 struct wps_ap_info *ap;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002710
2711 wpa_s->after_wps = 0;
2712
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002713 if (!wpa_s->wps_ap_iter)
2714 return;
2715 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2716 if (ap == NULL)
2717 return;
2718 ap->tries++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002719 os_get_reltime(&ap->last_attempt);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002720}