blob: eabe986541c38ceb61e701a20501545cb83b4a6a [file] [log] [blame]
Jouni Malinenfa201b62008-11-28 19:46:22 +02001/*
2 * wpa_supplicant / WPS integration
Jouni Malinen95fb2db2014-01-06 17:56:50 +02003 * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
Jouni Malinenfa201b62008-11-28 19:46:22 +02004 *
Jouni Malinen0f3d5782012-02-11 16:46:35 +02005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Jouni Malinenfa201b62008-11-28 19:46:22 +02007 */
8
9#include "includes.h"
10
11#include "common.h"
Jouni Malinen03da66b2009-11-29 23:04:43 +020012#include "eloop.h"
13#include "uuid.h"
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +030014#include "crypto/random.h"
Jouni Malinen03da66b2009-11-29 23:04:43 +020015#include "crypto/dh_group5.h"
Jouni Malinen90973fb2009-11-29 17:51:55 +020016#include "common/ieee802_11_defs.h"
17#include "common/ieee802_11_common.h"
18#include "common/wpa_common.h"
Jouni Malinen03da66b2009-11-29 23:04:43 +020019#include "common/wpa_ctrl.h"
20#include "eap_common/eap_wsc_common.h"
Jouni Malinenb01c18a2008-11-29 13:38:03 +020021#include "eap_peer/eap.h"
Jouni Malinen20a0b032011-06-27 19:02:24 +030022#include "eapol_supp/eapol_supp_sm.h"
Jouni Malinen03da66b2009-11-29 23:04:43 +020023#include "rsn_supp/wpa.h"
Jouni Malinend7645d22012-06-28 13:59:29 +030024#include "wps/wps_attr_parse.h"
Jouni Malinen03da66b2009-11-29 23:04:43 +020025#include "config.h"
Jouni Malinenfa201b62008-11-28 19:46:22 +020026#include "wpa_supplicant_i.h"
Jouni Malinen2d5b7922009-03-26 20:37:05 +020027#include "driver_i.h"
Jouni Malinen8bac4662009-09-13 20:53:32 +030028#include "notify.h"
Jouni Malinena6099152009-01-23 13:10:58 +020029#include "blacklist.h"
Jouni Malinen59f2caa2010-01-02 13:36:12 +020030#include "bss.h"
Jouni Malinen9ba9fa02010-01-03 18:48:11 +020031#include "scan.h"
Ardong Chen2f9929f2010-09-06 20:51:06 +030032#include "ap.h"
Jouni Malinene9a7ae42010-08-05 09:15:11 -070033#include "p2p/p2p.h"
Jouni Malinenb22128e2010-07-18 14:30:25 -070034#include "p2p_supplicant.h"
Jouni Malinenfa201b62008-11-28 19:46:22 +020035#include "wps_supplicant.h"
36
Jouni Malinenf7e54362009-12-28 16:07:15 +020037
Jouni Malinen31fcea92009-12-12 16:40:10 +020038#ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
Jouni Malinena6099152009-01-23 13:10:58 +020039#define WPS_PIN_SCAN_IGNORE_SEL_REG 3
Jouni Malinen31fcea92009-12-12 16:40:10 +020040#endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
Jouni Malinenfa201b62008-11-28 19:46:22 +020041
Jouni Malinenfcc60db2008-11-29 20:59:45 +020042static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
Jouni Malinen469fc3a2008-12-19 22:19:41 +020043static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
Jouni Malinenfcc60db2008-11-29 20:59:45 +020044
45
Jouni Malinenf9f05262012-08-27 13:48:11 +030046static 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
Jouni Malinen150fd0b2014-04-13 11:47:03 +030055static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
56{
57 struct wpa_supplicant *wpa_s = eloop_ctx;
58 int use_fast_assoc = timeout_ctx != NULL;
59
60 wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
61 if (!use_fast_assoc ||
62 wpa_supplicant_fast_associate(wpa_s) != 1)
63 wpa_supplicant_req_scan(wpa_s, 0, 0);
64}
65
66
67static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
68{
69 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
70 eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
71}
72
73
Jouni Malinenfa201b62008-11-28 19:46:22 +020074int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
75{
Jouni Malinen95fb2db2014-01-06 17:56:50 +020076 if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
77 return 1;
Jouni Malinen95fb2db2014-01-06 17:56:50 +020078
Jouni Malinena6099152009-01-23 13:10:58 +020079 if (!wpa_s->wps_success &&
80 wpa_s->current_ssid &&
81 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
82 const u8 *bssid = wpa_s->bssid;
83 if (is_zero_ether_addr(bssid))
84 bssid = wpa_s->pending_bssid;
85
86 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
87 " did not succeed - continue trying to find "
88 "suitable AP", MAC2STR(bssid));
89 wpa_blacklist_add(wpa_s, bssid);
90
91 wpa_supplicant_deauthenticate(wpa_s,
92 WLAN_REASON_DEAUTH_LEAVING);
93 wpa_s->reassociate = 1;
94 wpa_supplicant_req_scan(wpa_s,
95 wpa_s->blacklist_cleared ? 5 : 0, 0);
96 wpa_s->blacklist_cleared = 0;
97 return 1;
98 }
99
Jouni Malinenf9f05262012-08-27 13:48:11 +0300100 wpas_wps_clear_ap_info(wpa_s);
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200101 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Chao-Wen Yang449adba2011-01-13 18:04:33 +0200102 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
103 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200104
Jouni Malinenfa201b62008-11-28 19:46:22 +0200105 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
106 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
Jouni Malinenf981eab2010-12-22 11:33:59 +0200107 int disabled = wpa_s->current_ssid->disabled;
Jouni Malinena565c232012-02-12 13:54:15 +0200108 unsigned int freq = wpa_s->assoc_freq;
Masashi Honma97236ce2013-03-31 20:06:42 +0300109 struct wpa_bss *bss;
110 struct wpa_ssid *ssid = NULL;
111 int use_fast_assoc = 0;
112
Jouni Malinenfa201b62008-11-28 19:46:22 +0200113 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
Jouni Malinena565c232012-02-12 13:54:15 +0200114 "try to associate with the received credential "
115 "(freq=%u)", freq);
Jouni Malinenc2805902015-03-01 15:54:24 +0200116 wpa_s->own_disconnect_req = 1;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200117 wpa_supplicant_deauthenticate(wpa_s,
118 WLAN_REASON_DEAUTH_LEAVING);
Jouni Malinenf981eab2010-12-22 11:33:59 +0200119 if (disabled) {
120 wpa_printf(MSG_DEBUG, "WPS: Current network is "
121 "disabled - wait for user to enable");
122 return 1;
123 }
Jouni Malinen17a47342010-04-11 19:06:42 +0300124 wpa_s->after_wps = 5;
Jouni Malinena565c232012-02-12 13:54:15 +0200125 wpa_s->wps_freq = freq;
Jouni Malinen0b7a25c2011-11-28 18:36:36 +0200126 wpa_s->normal_scans = 0;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200127 wpa_s->reassociate = 1;
Masashi Honma97236ce2013-03-31 20:06:42 +0300128
129 wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
130 "without a new scan can be used");
131 bss = wpa_supplicant_pick_network(wpa_s, &ssid);
132 if (bss) {
133 struct wpabuf *wps;
134 struct wps_parse_attr attr;
135
136 wps = wpa_bss_get_vendor_ie_multi(bss,
137 WPS_IE_VENDOR_TYPE);
138 if (wps && wps_parse_msg(wps, &attr) == 0 &&
139 attr.wps_state &&
140 *attr.wps_state == WPS_STATE_CONFIGURED)
141 use_fast_assoc = 1;
142 wpabuf_free(wps);
143 }
144
Jouni Malinen150fd0b2014-04-13 11:47:03 +0300145 /*
146 * Complete the next step from an eloop timeout to allow pending
147 * driver events related to the disconnection to be processed
148 * first. This makes it less likely for disconnection event to
149 * cause problems with the following connection.
150 */
151 wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
152 wpas_wps_assoc_with_cred_cancel(wpa_s);
153 eloop_register_timeout(0, 10000,
154 wpas_wps_assoc_with_cred, wpa_s,
155 use_fast_assoc ? (void *) 1 :
156 (void *) 0);
Jouni Malinenfa201b62008-11-28 19:46:22 +0200157 return 1;
158 }
159
Jouni Malinen47662162009-01-18 12:27:12 +0200160 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
161 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
162 "for external credential processing");
163 wpas_clear_wps(wpa_s);
Jouni Malinenc2805902015-03-01 15:54:24 +0200164 wpa_s->own_disconnect_req = 1;
Jouni Malinen47662162009-01-18 12:27:12 +0200165 wpa_supplicant_deauthenticate(wpa_s,
166 WLAN_REASON_DEAUTH_LEAVING);
167 return 1;
168 }
169
Jouni Malinenfa201b62008-11-28 19:46:22 +0200170 return 0;
171}
172
173
Jouni Malinen7cc1b6c2009-03-05 21:39:39 +0200174static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
175 struct wpa_ssid *ssid,
176 const struct wps_credential *cred)
177{
178 struct wpa_driver_capa capa;
Jouni Malinen59f2caa2010-01-02 13:36:12 +0200179 struct wpa_bss *bss;
Jouni Malinen7cc1b6c2009-03-05 21:39:39 +0200180 const u8 *ie;
181 struct wpa_ie_data adv;
182 int wpa2 = 0, ccmp = 0;
183
184 /*
185 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
186 * case they are configured for mixed mode operation (WPA+WPA2 and
187 * TKIP+CCMP). Try to use scan results to figure out whether the AP
188 * actually supports stronger security and select that if the client
189 * has support for it, too.
190 */
191
192 if (wpa_drv_get_capa(wpa_s, &capa))
193 return; /* Unknown what driver supports */
194
Jouni Malinend87e90b2011-07-15 14:21:50 +0300195 if (ssid->ssid == NULL)
196 return;
Jouni Malinen59f2caa2010-01-02 13:36:12 +0200197 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
198 if (bss == NULL) {
199 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
200 "table - use credential as-is");
Jouni Malinen7cc1b6c2009-03-05 21:39:39 +0200201 return;
202 }
203
Jouni Malinen59f2caa2010-01-02 13:36:12 +0200204 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
205
206 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Jouni Malinen7cc1b6c2009-03-05 21:39:39 +0200207 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
208 wpa2 = 1;
209 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
210 ccmp = 1;
211 } else {
Jouni Malinen59f2caa2010-01-02 13:36:12 +0200212 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Jouni Malinen7cc1b6c2009-03-05 21:39:39 +0200213 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
214 adv.pairwise_cipher & WPA_CIPHER_CCMP)
215 ccmp = 1;
216 }
217
218 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
219 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
220 /*
221 * TODO: This could be the initial AP configuration and the
222 * Beacon contents could change shortly. Should request a new
223 * scan and delay addition of the network until the updated
224 * scan results are available.
225 */
226 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
227 "support - use credential as-is");
228 return;
229 }
230
231 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
232 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
233 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
234 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
235 "based on scan results");
236 if (wpa_s->conf->ap_scan == 1)
237 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
238 else
239 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
240 }
241
242 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
243 (ssid->proto & WPA_PROTO_WPA) &&
244 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
245 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
246 "based on scan results");
247 if (wpa_s->conf->ap_scan == 1)
248 ssid->proto |= WPA_PROTO_RSN;
249 else
250 ssid->proto = WPA_PROTO_RSN;
251 }
252}
253
254
Vinayak Kamath741ed9f2013-04-30 13:35:50 +0300255static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
256 struct wpa_ssid *new_ssid)
257{
258 struct wpa_ssid *ssid, *next;
259
260 for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
261 ssid = next, next = ssid ? ssid->next : NULL) {
262 /*
263 * new_ssid has already been added to the list in
264 * wpas_wps_add_network(), so skip it.
265 */
266 if (ssid == new_ssid)
267 continue;
268
269 if (ssid->bssid_set || new_ssid->bssid_set) {
270 if (ssid->bssid_set != new_ssid->bssid_set)
271 continue;
272 if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
273 0)
274 continue;
275 }
276
277 /* compare SSID */
278 if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
279 continue;
280
281 if (ssid->ssid && new_ssid->ssid) {
282 if (os_memcmp(ssid->ssid, new_ssid->ssid,
283 ssid->ssid_len) != 0)
284 continue;
285 } else if (ssid->ssid || new_ssid->ssid)
286 continue;
287
288 /* compare security parameters */
289 if (ssid->auth_alg != new_ssid->auth_alg ||
290 ssid->key_mgmt != new_ssid->key_mgmt ||
Ahmad Masrid77419d2014-10-26 12:04:29 +0000291 (ssid->group_cipher != new_ssid->group_cipher &&
292 !(ssid->group_cipher & new_ssid->group_cipher &
293 WPA_CIPHER_CCMP)))
Vinayak Kamath741ed9f2013-04-30 13:35:50 +0300294 continue;
295
Hu Wange5a4b852014-08-29 20:11:13 +0300296 /*
297 * Some existing WPS APs will send two creds in case they are
298 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
299 * Try to merge these two creds if they are received in the same
300 * M8 message.
301 */
302 if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
303 wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
304 if (new_ssid->passphrase && ssid->passphrase &&
305 os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
306 0) {
307 wpa_printf(MSG_DEBUG,
308 "WPS: M8 Creds with different passphrase - do not merge");
309 continue;
310 }
311
312 if (new_ssid->psk_set &&
313 (!ssid->psk_set ||
314 os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
315 wpa_printf(MSG_DEBUG,
316 "WPS: M8 Creds with different PSK - do not merge");
317 continue;
318 }
319
320 if ((new_ssid->passphrase && !ssid->passphrase) ||
321 (!new_ssid->passphrase && ssid->passphrase)) {
322 wpa_printf(MSG_DEBUG,
323 "WPS: M8 Creds with different passphrase/PSK type - do not merge");
324 continue;
325 }
326
327 wpa_printf(MSG_DEBUG,
328 "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
329 new_ssid->proto |= ssid->proto;
330 new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
331 } else {
332 /*
333 * proto and pairwise_cipher difference matter for
334 * non-mixed-mode creds.
335 */
336 if (ssid->proto != new_ssid->proto ||
337 ssid->pairwise_cipher != new_ssid->pairwise_cipher)
338 continue;
339 }
340
Vinayak Kamath741ed9f2013-04-30 13:35:50 +0300341 /* Remove the duplicated older network entry. */
342 wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
343 wpas_notify_network_removed(wpa_s, ssid);
Jouni Malinen4f9a5ea2014-11-30 15:48:37 +0200344 if (wpa_s->current_ssid == ssid)
345 wpa_s->current_ssid = NULL;
Vinayak Kamath741ed9f2013-04-30 13:35:50 +0300346 wpa_config_remove_network(wpa_s->conf, ssid->id);
347 }
348}
349
350
Jouni Malinenbcbbc7a2008-11-28 20:02:32 +0200351static int wpa_supplicant_wps_cred(void *ctx,
352 const struct wps_credential *cred)
Jouni Malinenfa201b62008-11-28 19:46:22 +0200353{
354 struct wpa_supplicant *wpa_s = ctx;
355 struct wpa_ssid *ssid = wpa_s->current_ssid;
Jouni Malinen49eba5f2009-08-15 20:40:45 +0300356 u16 auth_type;
Andrii Bordunov2c4f80d2011-10-22 22:21:53 +0300357#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Jouni Malinenf981eab2010-12-22 11:33:59 +0200358 int registrar = 0;
Andrii Bordunov2c4f80d2011-10-22 22:21:53 +0300359#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Jouni Malinenfa201b62008-11-28 19:46:22 +0200360
Jouni Malinen47662162009-01-18 12:27:12 +0200361 if ((wpa_s->conf->wps_cred_processing == 1 ||
362 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
363 size_t blen = cred->cred_attr_len * 2 + 1;
364 char *buf = os_malloc(blen);
365 if (buf) {
366 wpa_snprintf_hex(buf, blen,
367 cred->cred_attr, cred->cred_attr_len);
368 wpa_msg(wpa_s, MSG_INFO, "%s%s",
369 WPS_EVENT_CRED_RECEIVED, buf);
370 os_free(buf);
371 }
Jouni Malinen8bac4662009-09-13 20:53:32 +0300372
373 wpas_notify_wps_credential(wpa_s, cred);
Jouni Malinen47662162009-01-18 12:27:12 +0200374 } else
375 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
Jouni Malinenfa201b62008-11-28 19:46:22 +0200376
Jouni Malineneca6e0a2009-01-17 22:17:12 +0200377 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
378 cred->cred_attr, cred->cred_attr_len);
379
Jouni Malinen47662162009-01-18 12:27:12 +0200380 if (wpa_s->conf->wps_cred_processing == 1)
381 return 0;
382
Jouni Malinen73267b92009-03-30 19:00:55 +0300383 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
384 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
385 cred->auth_type);
386 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
387 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
388 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
389 cred->key, cred->key_len);
390 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
391 MAC2STR(cred->mac_addr));
392
Jouni Malinen49eba5f2009-08-15 20:40:45 +0300393 auth_type = cred->auth_type;
394 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
395 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
396 "auth_type into WPA2PSK");
397 auth_type = WPS_AUTH_WPA2PSK;
398 }
399
400 if (auth_type != WPS_AUTH_OPEN &&
Jouni Malinen49eba5f2009-08-15 20:40:45 +0300401 auth_type != WPS_AUTH_WPAPSK &&
402 auth_type != WPS_AUTH_WPA2PSK) {
Jouni Malinenf2860772009-01-17 21:45:51 +0200403 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
Jouni Malinen49eba5f2009-08-15 20:40:45 +0300404 "unsupported authentication type 0x%x",
405 auth_type);
Jouni Malinenf2860772009-01-17 21:45:51 +0200406 return 0;
407 }
408
Jouni Malinenb3631212012-11-01 16:30:35 +0200409 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
410 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
411 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
412 "invalid Network Key length %lu",
413 (unsigned long) cred->key_len);
414 return -1;
415 }
416 }
417
Jouni Malinenfa201b62008-11-28 19:46:22 +0200418 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
419 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
420 "on the received credential");
Andrii Bordunov2c4f80d2011-10-22 22:21:53 +0300421#ifdef CONFIG_WPS_REG_DISABLE_OPEN
Jouni Malinenf981eab2010-12-22 11:33:59 +0200422 if (ssid->eap.identity &&
423 ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
424 os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
425 WSC_ID_REGISTRAR_LEN) == 0)
426 registrar = 1;
Andrii Bordunov2c4f80d2011-10-22 22:21:53 +0300427#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Jouni Malinenfa201b62008-11-28 19:46:22 +0200428 os_free(ssid->eap.identity);
429 ssid->eap.identity = NULL;
430 ssid->eap.identity_len = 0;
431 os_free(ssid->eap.phase1);
432 ssid->eap.phase1 = NULL;
433 os_free(ssid->eap.eap_methods);
434 ssid->eap.eap_methods = NULL;
Jouni Malinencae21192012-07-25 16:18:12 +0300435 if (!ssid->p2p_group) {
Jouni Malinen2ff99b32010-07-18 14:30:24 -0700436 ssid->temporary = 0;
Jouni Malinencae21192012-07-25 16:18:12 +0300437 ssid->bssid_set = 0;
438 }
Jouni Malinen00e5e3d2012-08-26 23:01:26 +0300439 ssid->disabled_until.sec = 0;
440 ssid->disabled_until.usec = 0;
441 ssid->auth_failures = 0;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200442 } else {
443 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
444 "received credential");
445 ssid = wpa_config_add_network(wpa_s->conf);
446 if (ssid == NULL)
447 return -1;
Jouni Malinended4f942014-04-13 11:44:57 +0300448 if (wpa_s->current_ssid) {
449 /*
450 * Should the GO issue multiple credentials for some
451 * reason, each credential should be marked as a
452 * temporary P2P group similarly to the one that gets
453 * marked as such based on the pre-configured values
454 * used for the WPS network block.
455 */
456 ssid->p2p_group = wpa_s->current_ssid->p2p_group;
457 ssid->temporary = wpa_s->current_ssid->temporary;
458 }
Jouni Malinen8bac4662009-09-13 20:53:32 +0300459 wpas_notify_network_added(wpa_s, ssid);
Jouni Malinenfa201b62008-11-28 19:46:22 +0200460 }
461
462 wpa_config_set_network_defaults(ssid);
Hu Wange5a4b852014-08-29 20:11:13 +0300463 ssid->wps_run = wpa_s->wps_run;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200464
465 os_free(ssid->ssid);
466 ssid->ssid = os_malloc(cred->ssid_len);
467 if (ssid->ssid) {
468 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
469 ssid->ssid_len = cred->ssid_len;
470 }
471
472 switch (cred->encr_type) {
473 case WPS_ENCR_NONE:
Jouni Malinenfa201b62008-11-28 19:46:22 +0200474 break;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200475 case WPS_ENCR_TKIP:
476 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200477 break;
478 case WPS_ENCR_AES:
479 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Ahmad Masrid77419d2014-10-26 12:04:29 +0000480 if (wpa_s->drv_capa_known &&
481 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
482 ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
483 ssid->group_cipher |= WPA_CIPHER_GCMP;
484 }
Jouni Malinenfa201b62008-11-28 19:46:22 +0200485 break;
486 }
487
Jouni Malinen49eba5f2009-08-15 20:40:45 +0300488 switch (auth_type) {
Jouni Malinenfa201b62008-11-28 19:46:22 +0200489 case WPS_AUTH_OPEN:
490 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
491 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
492 ssid->proto = 0;
Jouni Malinenf981eab2010-12-22 11:33:59 +0200493#ifdef CONFIG_WPS_REG_DISABLE_OPEN
494 if (registrar) {
495 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
496 "id=%d - Credentials for an open "
497 "network disabled by default - use "
498 "'select_network %d' to enable",
499 ssid->id, ssid->id);
500 ssid->disabled = 1;
501 }
502#endif /* CONFIG_WPS_REG_DISABLE_OPEN */
Jouni Malinenfa201b62008-11-28 19:46:22 +0200503 break;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200504 case WPS_AUTH_WPAPSK:
505 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
506 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
507 ssid->proto = WPA_PROTO_WPA;
508 break;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200509 case WPS_AUTH_WPA2PSK:
510 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
511 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
512 ssid->proto = WPA_PROTO_RSN;
513 break;
514 }
515
516 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
517 if (cred->key_len == 2 * PMK_LEN) {
518 if (hexstr2bin((const char *) cred->key, ssid->psk,
519 PMK_LEN)) {
520 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
521 "Key");
522 return -1;
523 }
524 ssid->psk_set = 1;
Johannes Bergd1c8ac82011-02-02 17:00:12 +0200525 ssid->export_keys = 1;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200526 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
527 os_free(ssid->passphrase);
528 ssid->passphrase = os_malloc(cred->key_len + 1);
529 if (ssid->passphrase == NULL)
530 return -1;
531 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
532 ssid->passphrase[cred->key_len] = '\0';
533 wpa_config_update_psk(ssid);
Johannes Bergd1c8ac82011-02-02 17:00:12 +0200534 ssid->export_keys = 1;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200535 } else {
536 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
537 "length %lu",
538 (unsigned long) cred->key_len);
539 return -1;
540 }
541 }
542
Jouni Malinen7cc1b6c2009-03-05 21:39:39 +0200543 wpas_wps_security_workaround(wpa_s, ssid, cred);
544
Vinayak Kamath741ed9f2013-04-30 13:35:50 +0300545 wpas_wps_remove_dup_network(wpa_s, ssid);
546
Jouni Malinenfa201b62008-11-28 19:46:22 +0200547#ifndef CONFIG_NO_CONFIG_WRITE
548 if (wpa_s->conf->update_config &&
549 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
550 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
551 return -1;
552 }
553#endif /* CONFIG_NO_CONFIG_WRITE */
554
Sunil Dutt6fbcbc02012-12-21 15:30:39 +0200555 /*
556 * Optimize the post-WPS scan based on the channel used during
557 * the provisioning in case EAP-Failure is not received.
558 */
559 wpa_s->after_wps = 5;
560 wpa_s->wps_freq = wpa_s->assoc_freq;
561
Jouni Malinenfa201b62008-11-28 19:46:22 +0200562 return 0;
563}
564
565
Jouni Malinen4b682902008-12-18 21:58:42 +0200566static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
567 struct wps_event_m2d *m2d)
568{
569 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
570 "dev_password_id=%d config_error=%d",
571 m2d->dev_password_id, m2d->config_error);
Jouni Malinen8bac4662009-09-13 20:53:32 +0300572 wpas_notify_wps_event_m2d(wpa_s, m2d);
Jouni Malinenb89883a2011-02-22 12:20:16 +0200573#ifdef CONFIG_P2P
574 if (wpa_s->parent && wpa_s->parent != wpa_s) {
575 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
576 "dev_password_id=%d config_error=%d",
577 m2d->dev_password_id, m2d->config_error);
578 }
579 if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
580 /*
581 * Notify P2P from eloop timeout to avoid issues with the
582 * interface getting removed while processing a message.
583 */
Avraham Sternace0fbd2014-03-27 08:58:30 +0200584 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
Jouni Malinenb89883a2011-02-22 12:20:16 +0200585 NULL);
586 }
587#endif /* CONFIG_P2P */
Jouni Malinen4b682902008-12-18 21:58:42 +0200588}
589
590
Jouni Malinen2e145e92013-08-23 17:43:01 +0300591static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
592{
593 struct wpa_supplicant *wpa_s = eloop_ctx;
594 wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
595 wpas_clear_wps(wpa_s);
596}
597
598
Jouni Malinen469fc3a2008-12-19 22:19:41 +0200599static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
600 struct wps_event_fail *fail)
601{
Chao-Wen Yangc5cf0a12011-01-13 17:50:59 +0200602 if (fail->error_indication > 0 &&
603 fail->error_indication < NUM_WPS_EI_VALUES) {
604 wpa_msg(wpa_s, MSG_INFO,
605 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
606 fail->msg, fail->config_error, fail->error_indication,
Jouni Malinen961750c2013-07-31 17:30:45 +0300607 wps_ei_str(fail->error_indication));
Chao-Wen Yangc5cf0a12011-01-13 17:50:59 +0200608 if (wpa_s->parent && wpa_s->parent != wpa_s)
609 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
610 "msg=%d config_error=%d reason=%d (%s)",
611 fail->msg, fail->config_error,
612 fail->error_indication,
Jouni Malinen961750c2013-07-31 17:30:45 +0300613 wps_ei_str(fail->error_indication));
Chao-Wen Yangc5cf0a12011-01-13 17:50:59 +0200614 } else {
615 wpa_msg(wpa_s, MSG_INFO,
616 WPS_EVENT_FAIL "msg=%d config_error=%d",
Jouni Malinen545cc6a2010-10-14 20:52:31 +0300617 fail->msg, fail->config_error);
Chao-Wen Yangc5cf0a12011-01-13 17:50:59 +0200618 if (wpa_s->parent && wpa_s->parent != wpa_s)
619 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
620 "msg=%d config_error=%d",
621 fail->msg, fail->config_error);
622 }
Jouni Malinen2e145e92013-08-23 17:43:01 +0300623
624 /*
625 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
626 */
627 wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
628 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
629 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
630
Jouni Malinen8bac4662009-09-13 20:53:32 +0300631 wpas_notify_wps_event_fail(wpa_s, fail);
Jayant Sane37345522011-06-25 11:47:04 +0300632 wpas_p2p_wps_failed(wpa_s, fail);
Jouni Malinen469fc3a2008-12-19 22:19:41 +0200633}
634
635
Sunil Duttec947ff2012-10-04 21:11:04 +0300636static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
637
638static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
639{
640 struct wpa_ssid *ssid;
Jouni Malinen318e7722012-12-21 20:11:26 +0200641 int changed = 0;
Sunil Duttec947ff2012-10-04 21:11:04 +0300642
643 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
644
645 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
646 if (ssid->disabled_for_connect && ssid->disabled) {
647 ssid->disabled_for_connect = 0;
648 ssid->disabled = 0;
649 wpas_notify_network_enabled_changed(wpa_s, ssid);
Jouni Malinen318e7722012-12-21 20:11:26 +0200650 changed++;
Sunil Duttec947ff2012-10-04 21:11:04 +0300651 }
652 }
Jouni Malinen318e7722012-12-21 20:11:26 +0200653
654 if (changed) {
655#ifndef CONFIG_NO_CONFIG_WRITE
656 if (wpa_s->conf->update_config &&
657 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
658 wpa_printf(MSG_DEBUG, "WPS: Failed to update "
659 "configuration");
660 }
661#endif /* CONFIG_NO_CONFIG_WRITE */
662 }
Sunil Duttec947ff2012-10-04 21:11:04 +0300663}
664
665
666static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
667{
668 struct wpa_supplicant *wpa_s = eloop_ctx;
669 /* Enable the networks disabled during wpas_wps_reassoc */
670 wpas_wps_reenable_networks(wpa_s);
671}
672
673
Jouni Malinenad5302a2008-12-19 22:34:18 +0200674static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
675{
676 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
Jouni Malinena6099152009-01-23 13:10:58 +0200677 wpa_s->wps_success = 1;
Jouni Malinen8bac4662009-09-13 20:53:32 +0300678 wpas_notify_wps_event_success(wpa_s);
Jouni Malinenf3e907a2013-05-15 17:33:50 +0300679 if (wpa_s->current_ssid)
680 wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
681 wpa_s->extra_blacklist_count = 0;
Sunil Duttec947ff2012-10-04 21:11:04 +0300682
683 /*
684 * Enable the networks disabled during wpas_wps_reassoc after 10
685 * seconds. The 10 seconds timer is to allow the data connection to be
686 * formed before allowing other networks to be selected.
687 */
688 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
689 NULL);
690
Jouni Malinenb22128e2010-07-18 14:30:25 -0700691 wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
Jouni Malinenad5302a2008-12-19 22:34:18 +0200692}
693
694
Jouni Malinenb78bc3a2009-11-15 12:07:27 +0200695static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
696 struct wps_event_er_ap *ap)
697{
698 char uuid_str[100];
Jouni Malinened459472009-11-26 11:54:37 +0200699 char dev_type[WPS_DEV_TYPE_BUFSIZE];
Jouni Malinene694b342009-11-21 13:34:23 +0200700
Jouni Malinenb78bc3a2009-11-15 12:07:27 +0200701 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
Jouni Malinene694b342009-11-21 13:34:23 +0200702 if (ap->pri_dev_type)
Jouni Malinened459472009-11-26 11:54:37 +0200703 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
704 sizeof(dev_type));
Jouni Malinene694b342009-11-21 13:34:23 +0200705 else
706 dev_type[0] = '\0';
707
708 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
709 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
710 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
Jouni Malinenb78bc3a2009-11-15 12:07:27 +0200711 ap->friendly_name ? ap->friendly_name : "",
712 ap->manufacturer ? ap->manufacturer : "",
713 ap->model_description ? ap->model_description : "",
714 ap->model_name ? ap->model_name : "",
715 ap->manufacturer_url ? ap->manufacturer_url : "",
716 ap->model_url ? ap->model_url : "");
717}
718
719
720static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
721 struct wps_event_er_ap *ap)
722{
723 char uuid_str[100];
724 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
725 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
726}
727
728
729static void wpa_supplicant_wps_event_er_enrollee_add(
730 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
731{
732 char uuid_str[100];
Jouni Malinened459472009-11-26 11:54:37 +0200733 char dev_type[WPS_DEV_TYPE_BUFSIZE];
Jouni Malinenb78bc3a2009-11-15 12:07:27 +0200734
735 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
736 if (enrollee->pri_dev_type)
Jouni Malinened459472009-11-26 11:54:37 +0200737 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
738 sizeof(dev_type));
Jouni Malinenb78bc3a2009-11-15 12:07:27 +0200739 else
740 dev_type[0] = '\0';
741
742 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
743 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
744 "|%s|%s|%s|%s|%s|",
745 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
746 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
747 enrollee->dev_name ? enrollee->dev_name : "",
748 enrollee->manufacturer ? enrollee->manufacturer : "",
749 enrollee->model_name ? enrollee->model_name : "",
750 enrollee->model_number ? enrollee->model_number : "",
751 enrollee->serial_number ? enrollee->serial_number : "");
752}
753
754
755static void wpa_supplicant_wps_event_er_enrollee_remove(
756 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
757{
758 char uuid_str[100];
759 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
760 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
761 uuid_str, MAC2STR(enrollee->mac_addr));
762}
763
764
Jouni Malinen15dbf122010-05-27 15:24:45 +0300765static void wpa_supplicant_wps_event_er_ap_settings(
766 struct wpa_supplicant *wpa_s,
767 struct wps_event_er_ap_settings *ap_settings)
768{
769 char uuid_str[100];
770 char key_str[65];
771 const struct wps_credential *cred = ap_settings->cred;
772
773 key_str[0] = '\0';
774 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
775 if (cred->key_len >= 8 && cred->key_len <= 64) {
776 os_memcpy(key_str, cred->key, cred->key_len);
777 key_str[cred->key_len] = '\0';
778 }
779 }
780
781 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
782 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
783 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
784 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
785 "key=%s",
786 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
787 cred->auth_type, cred->encr_type, key_str);
788}
789
790
Jouni Malinen3e7533b2010-10-25 21:29:22 +0300791static void wpa_supplicant_wps_event_er_set_sel_reg(
792 struct wpa_supplicant *wpa_s,
793 struct wps_event_er_set_selected_registrar *ev)
794{
795 char uuid_str[100];
796
797 uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
798 switch (ev->state) {
799 case WPS_ER_SET_SEL_REG_START:
800 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
801 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
802 "sel_reg_config_methods=0x%x",
803 uuid_str, ev->sel_reg, ev->dev_passwd_id,
804 ev->sel_reg_config_methods);
805 break;
806 case WPS_ER_SET_SEL_REG_DONE:
807 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
808 "uuid=%s state=DONE", uuid_str);
809 break;
810 case WPS_ER_SET_SEL_REG_FAILED:
811 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
812 "uuid=%s state=FAILED", uuid_str);
813 break;
814 }
815}
816
817
Jouni Malinen4b682902008-12-18 21:58:42 +0200818static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
819 union wps_event_data *data)
820{
821 struct wpa_supplicant *wpa_s = ctx;
822 switch (event) {
823 case WPS_EV_M2D:
824 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
825 break;
Jouni Malinen469fc3a2008-12-19 22:19:41 +0200826 case WPS_EV_FAIL:
827 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
828 break;
Jouni Malinenad5302a2008-12-19 22:34:18 +0200829 case WPS_EV_SUCCESS:
830 wpa_supplicant_wps_event_success(wpa_s);
831 break;
Jouni Malinen3b2cf802009-01-23 21:57:43 +0200832 case WPS_EV_PWD_AUTH_FAIL:
Jouni Malinen70d84f12010-11-17 16:46:55 +0200833#ifdef CONFIG_AP
834 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
835 wpa_supplicant_ap_pwd_auth_fail(wpa_s);
836#endif /* CONFIG_AP */
Jouni Malinen3b2cf802009-01-23 21:57:43 +0200837 break;
Oleg Kravtsov63330c62009-11-01 21:26:13 +0200838 case WPS_EV_PBC_OVERLAP:
839 break;
840 case WPS_EV_PBC_TIMEOUT:
841 break;
Jeffin Mammen50396e22013-08-23 16:01:30 +0300842 case WPS_EV_PBC_ACTIVE:
843 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
844 break;
845 case WPS_EV_PBC_DISABLE:
846 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
847 break;
Jouni Malinenb78bc3a2009-11-15 12:07:27 +0200848 case WPS_EV_ER_AP_ADD:
849 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
850 break;
851 case WPS_EV_ER_AP_REMOVE:
852 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
853 break;
854 case WPS_EV_ER_ENROLLEE_ADD:
855 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
856 &data->enrollee);
857 break;
858 case WPS_EV_ER_ENROLLEE_REMOVE:
859 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
860 &data->enrollee);
861 break;
Jouni Malinen15dbf122010-05-27 15:24:45 +0300862 case WPS_EV_ER_AP_SETTINGS:
863 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
864 &data->ap_settings);
865 break;
Jouni Malinen3e7533b2010-10-25 21:29:22 +0300866 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
867 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
868 &data->set_sel_reg);
869 break;
Jouni Malinen32cdcf12012-01-30 17:31:06 +0200870 case WPS_EV_AP_PIN_SUCCESS:
871 break;
Jouni Malinen4b682902008-12-18 21:58:42 +0200872 }
873}
874
875
David Spinadel239abaf2013-08-25 10:55:53 +0300876static int wpa_supplicant_wps_rf_band(void *ctx)
877{
878 struct wpa_supplicant *wpa_s = ctx;
879
880 if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
881 return 0;
882
883 return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
884}
885
886
Jouni Malinenf90c86d2009-01-03 19:50:49 +0200887enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
Jouni Malinenfa201b62008-11-28 19:46:22 +0200888{
Jouni Malinenb01c18a2008-11-29 13:38:03 +0200889 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
890 eap_is_wps_pin_enrollee(&ssid->eap))
891 return WPS_REQ_ENROLLEE;
892 else
893 return WPS_REQ_REGISTRAR;
Jouni Malinenfa201b62008-11-28 19:46:22 +0200894}
Jouni Malinen116654c2008-11-28 20:32:13 +0200895
896
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200897static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
898{
899 int id;
Jouni Malinen20a0b032011-06-27 19:02:24 +0300900 struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
901
Jouni Malinen72559832013-10-20 21:34:39 +0300902 wpa_s->after_wps = 0;
Jouni Malinen4d9fb082013-10-27 12:56:56 +0200903 wpa_s->known_wps_freq = 0;
Jouni Malinen72559832013-10-20 21:34:39 +0300904
Jouni Malinen20a0b032011-06-27 19:02:24 +0300905 prev_current = wpa_s->current_ssid;
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200906
Sunil Duttec947ff2012-10-04 21:11:04 +0300907 /* Enable the networks disabled during wpas_wps_reassoc */
908 wpas_wps_reenable_networks(wpa_s);
909
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200910 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Jouni Malinen5890fa82014-01-01 22:00:09 +0200911 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200912
913 /* Remove any existing WPS network from configuration */
914 ssid = wpa_s->conf->ssid;
915 while (ssid) {
Jouni Malinenf0477202008-12-18 01:06:06 +0200916 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
Jouni Malinen8bac4662009-09-13 20:53:32 +0300917 if (ssid == wpa_s->current_ssid) {
Jouni Malinenc2805902015-03-01 15:54:24 +0200918 wpa_s->own_disconnect_req = 1;
Junli Zhaoc7a67a72013-07-08 12:09:18 +0300919 wpa_supplicant_deauthenticate(
920 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Jouni Malinen8bac4662009-09-13 20:53:32 +0300921 }
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200922 id = ssid->id;
Jouni Malinen8bac4662009-09-13 20:53:32 +0300923 remove_ssid = ssid;
Jouni Malinenf0477202008-12-18 01:06:06 +0200924 } else
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200925 id = -1;
926 ssid = ssid->next;
Jouni Malinen8bac4662009-09-13 20:53:32 +0300927 if (id >= 0) {
Jouni Malinen20a0b032011-06-27 19:02:24 +0300928 if (prev_current == remove_ssid) {
929 wpa_sm_set_config(wpa_s->wpa, NULL);
930 eapol_sm_notify_config(wpa_s->eapol, NULL,
931 NULL);
932 }
Jouni Malinen8bac4662009-09-13 20:53:32 +0300933 wpas_notify_network_removed(wpa_s, remove_ssid);
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200934 wpa_config_remove_network(wpa_s->conf, id);
Jouni Malinen8bac4662009-09-13 20:53:32 +0300935 }
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200936 }
Jouni Malinenf9f05262012-08-27 13:48:11 +0300937
938 wpas_wps_clear_ap_info(wpa_s);
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200939}
940
941
942static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
943{
944 struct wpa_supplicant *wpa_s = eloop_ctx;
Ardong Chen014732e2010-09-06 18:13:18 +0300945 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
946 "out");
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200947 wpas_clear_wps(wpa_s);
948}
949
950
951static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
Jouni Malinen23318be2013-09-07 15:44:29 -0700952 int registrar, const u8 *dev_addr,
953 const u8 *bssid)
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200954{
955 struct wpa_ssid *ssid;
956
957 ssid = wpa_config_add_network(wpa_s->conf);
958 if (ssid == NULL)
959 return NULL;
Jouni Malinen8bac4662009-09-13 20:53:32 +0300960 wpas_notify_network_added(wpa_s, ssid);
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200961 wpa_config_set_network_defaults(ssid);
Jouni Malinen2ff99b32010-07-18 14:30:24 -0700962 ssid->temporary = 1;
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200963 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
964 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
965 wpa_config_set(ssid, "identity", registrar ?
966 "\"" WSC_ID_REGISTRAR "\"" :
967 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
Jouni Malinen8bac4662009-09-13 20:53:32 +0300968 wpas_notify_network_removed(wpa_s, ssid);
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200969 wpa_config_remove_network(wpa_s->conf, ssid->id);
970 return NULL;
971 }
972
Jouni Malinen23318be2013-09-07 15:44:29 -0700973#ifdef CONFIG_P2P
974 if (dev_addr)
975 os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
976#endif /* CONFIG_P2P */
977
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200978 if (bssid) {
Jouni Malinenb2c5a4a2010-07-18 14:30:24 -0700979#ifndef CONFIG_P2P
Jouni Malinen59f2caa2010-01-02 13:36:12 +0200980 struct wpa_bss *bss;
Jouni Malinenf7e54362009-12-28 16:07:15 +0200981 int count = 0;
Jouni Malinenb2c5a4a2010-07-18 14:30:24 -0700982#endif /* CONFIG_P2P */
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200983
984 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
Jouni Malinen24c23d12008-12-20 22:55:02 +0200985 ssid->bssid_set = 1;
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200986
Jouni Malinenb2c5a4a2010-07-18 14:30:24 -0700987 /*
988 * Note: With P2P, the SSID may change at the time the WPS
989 * provisioning is started, so better not filter the AP based
990 * on the current SSID in the scan results.
991 */
992#ifndef CONFIG_P2P
Jouni Malinen59f2caa2010-01-02 13:36:12 +0200993 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
994 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200995 continue;
996
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200997 os_free(ssid->ssid);
Jouni Malinen59f2caa2010-01-02 13:36:12 +0200998 ssid->ssid = os_malloc(bss->ssid_len);
Jouni Malinenfcc60db2008-11-29 20:59:45 +0200999 if (ssid->ssid == NULL)
1000 break;
Jouni Malinen59f2caa2010-01-02 13:36:12 +02001001 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1002 ssid->ssid_len = bss->ssid_len;
Jouni Malinenf7e54362009-12-28 16:07:15 +02001003 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1004 "scan results",
1005 ssid->ssid, ssid->ssid_len);
1006 count++;
1007 }
1008
1009 if (count > 1) {
1010 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1011 "for the AP; use wildcard");
1012 os_free(ssid->ssid);
1013 ssid->ssid = NULL;
1014 ssid->ssid_len = 0;
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001015 }
Jouni Malinenb2c5a4a2010-07-18 14:30:24 -07001016#endif /* CONFIG_P2P */
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001017 }
1018
1019 return ssid;
1020}
1021
1022
Jouni Malinen170f5662013-05-15 16:46:45 +03001023static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1024 struct wpa_ssid *selected)
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001025{
1026 struct wpa_ssid *ssid;
1027
Jouni Malinenc2805902015-03-01 15:54:24 +02001028 if (wpa_s->current_ssid) {
1029 wpa_s->own_disconnect_req = 1;
Jouni Malinend9d87c32011-12-11 18:30:47 +02001030 wpa_supplicant_deauthenticate(
1031 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Jouni Malinenc2805902015-03-01 15:54:24 +02001032 }
Jouni Malinend9d87c32011-12-11 18:30:47 +02001033
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001034 /* Mark all other networks disabled and trigger reassociation */
1035 ssid = wpa_s->conf->ssid;
1036 while (ssid) {
Jouni Malinen8bac4662009-09-13 20:53:32 +03001037 int was_disabled = ssid->disabled;
Sunil Duttec947ff2012-10-04 21:11:04 +03001038 ssid->disabled_for_connect = 0;
Jayant Sanec2762e42011-06-23 21:25:13 +03001039 /*
1040 * In case the network object corresponds to a persistent group
1041 * then do not send out network disabled signal. In addition,
1042 * do not change disabled status of persistent network objects
1043 * from 2 to 1 should we connect to another network.
1044 */
1045 if (was_disabled != 2) {
1046 ssid->disabled = ssid != selected;
Sunil Duttec947ff2012-10-04 21:11:04 +03001047 if (was_disabled != ssid->disabled) {
1048 if (ssid->disabled)
1049 ssid->disabled_for_connect = 1;
Jayant Sanec2762e42011-06-23 21:25:13 +03001050 wpas_notify_network_enabled_changed(wpa_s,
1051 ssid);
Sunil Duttec947ff2012-10-04 21:11:04 +03001052 }
Jayant Sanec2762e42011-06-23 21:25:13 +03001053 }
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001054 ssid = ssid->next;
1055 }
Jouni Malinen170f5662013-05-15 16:46:45 +03001056}
1057
1058
1059static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
Jouni Malinen91a65012013-11-28 17:48:35 +02001060 struct wpa_ssid *selected, const u8 *bssid,
1061 int freq)
Jouni Malinen170f5662013-05-15 16:46:45 +03001062{
1063 struct wpa_bss *bss;
1064
Hu Wange5a4b852014-08-29 20:11:13 +03001065 wpa_s->wps_run++;
1066 if (wpa_s->wps_run == 0)
1067 wpa_s->wps_run++;
Jouni Malinen170f5662013-05-15 16:46:45 +03001068 wpa_s->after_wps = 0;
1069 wpa_s->known_wps_freq = 0;
Jouni Malinen91a65012013-11-28 17:48:35 +02001070 if (freq) {
1071 wpa_s->after_wps = 5;
1072 wpa_s->wps_freq = freq;
1073 } else if (bssid) {
Jouni Malinen170f5662013-05-15 16:46:45 +03001074 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1075 if (bss && bss->freq > 0) {
1076 wpa_s->known_wps_freq = 1;
1077 wpa_s->wps_freq = bss->freq;
1078 }
1079 }
1080
1081 wpas_wps_temp_disable(wpa_s, selected);
1082
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001083 wpa_s->disconnected = 0;
1084 wpa_s->reassociate = 1;
Jouni Malinena6099152009-01-23 13:10:58 +02001085 wpa_s->scan_runs = 0;
Jouni Malinen0b7a25c2011-11-28 18:36:36 +02001086 wpa_s->normal_scans = 0;
Jouni Malinena6099152009-01-23 13:10:58 +02001087 wpa_s->wps_success = 0;
1088 wpa_s->blacklist_cleared = 0;
Jithu Jance1cba9be2013-11-20 19:20:56 +05301089
1090 wpa_supplicant_cancel_sched_scan(wpa_s);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001091 wpa_supplicant_req_scan(wpa_s, 0, 0);
1092}
1093
1094
Jouni Malinen9fa243b2010-07-18 14:30:24 -07001095int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1096 int p2p_group)
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001097{
1098 struct wpa_ssid *ssid;
Jouni Malinenbd47d682015-01-02 17:09:38 +02001099
1100#ifdef CONFIG_AP
1101 if (wpa_s->ap_iface) {
1102 wpa_printf(MSG_DEBUG,
1103 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1104 return -1;
1105 }
1106#endif /* CONFIG_AP */
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001107 wpas_clear_wps(wpa_s);
Jouni Malinen23318be2013-09-07 15:44:29 -07001108 ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001109 if (ssid == NULL)
1110 return -1;
Jouni Malinen9fa243b2010-07-18 14:30:24 -07001111 ssid->temporary = 1;
1112 ssid->p2p_group = p2p_group;
Jouni Malinen812bf562010-09-09 07:20:28 -07001113#ifdef CONFIG_P2P
Jouni Malinene9a7ae42010-08-05 09:15:11 -07001114 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1115 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1116 if (ssid->ssid) {
1117 ssid->ssid_len = wpa_s->go_params->ssid_len;
1118 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1119 ssid->ssid_len);
1120 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1121 "SSID", ssid->ssid, ssid->ssid_len);
1122 }
1123 }
Jouni Malinen812bf562010-09-09 07:20:28 -07001124#endif /* CONFIG_P2P */
Jouni Malinen9c75e9f2013-01-01 20:30:17 +02001125 if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
1126 return -1;
Jouni Malinen42f50262010-07-22 10:24:23 -07001127 if (wpa_s->wps_fragment_size)
1128 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001129 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1130 wpa_s, NULL);
Jouni Malinen91a65012013-11-28 17:48:35 +02001131 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001132 return 0;
1133}
1134
1135
Jouni Malinen23318be2013-09-07 15:44:29 -07001136static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1137 const u8 *dev_addr, const u8 *bssid,
Jouni Malinen5f454552013-02-15 21:29:22 +02001138 const char *pin, int p2p_group, u16 dev_pw_id,
Jouni Malinen57630e62013-04-02 18:29:23 +03001139 const u8 *peer_pubkey_hash,
Jouni Malinen91a65012013-11-28 17:48:35 +02001140 const u8 *ssid_val, size_t ssid_len, int freq)
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001141{
1142 struct wpa_ssid *ssid;
Jouni Malinen5f454552013-02-15 21:29:22 +02001143 char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001144 unsigned int rpin = 0;
Jouni Malinen5f454552013-02-15 21:29:22 +02001145 char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001146
Jouni Malinenbd47d682015-01-02 17:09:38 +02001147#ifdef CONFIG_AP
1148 if (wpa_s->ap_iface) {
1149 wpa_printf(MSG_DEBUG,
1150 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1151 return -1;
1152 }
1153#endif /* CONFIG_AP */
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001154 wpas_clear_wps(wpa_s);
Jouni Malinen23318be2013-09-07 15:44:29 -07001155 if (bssid && is_zero_ether_addr(bssid))
1156 bssid = NULL;
1157 ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
Jouni Malinen57630e62013-04-02 18:29:23 +03001158 if (ssid == NULL) {
1159 wpa_printf(MSG_DEBUG, "WPS: Could not add network");
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001160 return -1;
Jouni Malinen57630e62013-04-02 18:29:23 +03001161 }
Jouni Malinen9fa243b2010-07-18 14:30:24 -07001162 ssid->temporary = 1;
1163 ssid->p2p_group = p2p_group;
Jouni Malinen57630e62013-04-02 18:29:23 +03001164 if (ssid_val) {
1165 ssid->ssid = os_malloc(ssid_len);
1166 if (ssid->ssid) {
1167 os_memcpy(ssid->ssid, ssid_val, ssid_len);
1168 ssid->ssid_len = ssid_len;
1169 }
1170 }
Jouni Malinen5f454552013-02-15 21:29:22 +02001171 if (peer_pubkey_hash) {
1172 os_memcpy(hash, " pkhash=", 8);
1173 wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1174 peer_pubkey_hash,
1175 WPS_OOB_PUBKEY_HASH_LEN);
1176 } else {
1177 hash[0] = '\0';
1178 }
Jouni Malinen812bf562010-09-09 07:20:28 -07001179#ifdef CONFIG_P2P
Jouni Malinene9a7ae42010-08-05 09:15:11 -07001180 if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1181 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1182 if (ssid->ssid) {
1183 ssid->ssid_len = wpa_s->go_params->ssid_len;
1184 os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1185 ssid->ssid_len);
1186 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1187 "SSID", ssid->ssid, ssid->ssid_len);
1188 }
1189 }
Jouni Malinen812bf562010-09-09 07:20:28 -07001190#endif /* CONFIG_P2P */
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001191 if (pin)
Jouni Malinen5f454552013-02-15 21:29:22 +02001192 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1193 pin, dev_pw_id, hash);
Jouni Malinenfa4c2982013-04-02 18:30:58 +03001194 else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1195 os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1196 dev_pw_id, hash);
1197 } else {
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001198 rpin = wps_generate_pin();
Jouni Malinen5f454552013-02-15 21:29:22 +02001199 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1200 rpin, dev_pw_id, hash);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001201 }
Jouni Malinenfa4c2982013-04-02 18:30:58 +03001202 if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1203 wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
Jouni Malinen9c75e9f2013-01-01 20:30:17 +02001204 return -1;
Jouni Malinenfa4c2982013-04-02 18:30:58 +03001205 }
Jouni Malinen42f50262010-07-22 10:24:23 -07001206 if (wpa_s->wps_fragment_size)
1207 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001208 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1209 wpa_s, NULL);
Jouni Malinenf9f05262012-08-27 13:48:11 +03001210 wpa_s->wps_ap_iter = 1;
Jouni Malinen91a65012013-11-28 17:48:35 +02001211 wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001212 return rpin;
1213}
1214
1215
Jouni Malinen5f454552013-02-15 21:29:22 +02001216int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1217 const char *pin, int p2p_group, u16 dev_pw_id)
1218{
Jouni Malinen23318be2013-09-07 15:44:29 -07001219 return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
Jouni Malinen91a65012013-11-28 17:48:35 +02001220 dev_pw_id, NULL, NULL, 0, 0);
Jouni Malinen5f454552013-02-15 21:29:22 +02001221}
1222
1223
Ardong Chen2f9929f2010-09-06 20:51:06 +03001224/* Cancel the wps pbc/pin requests */
1225int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1226{
1227#ifdef CONFIG_AP
1228 if (wpa_s->ap_iface) {
1229 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1230 return wpa_supplicant_ap_wps_cancel(wpa_s);
1231 }
1232#endif /* CONFIG_AP */
1233
Jouni Malinen476dea22012-06-06 22:07:11 +03001234 if (wpa_s->wpa_state == WPA_SCANNING ||
1235 wpa_s->wpa_state == WPA_DISCONNECTED) {
Ardong Chen2f9929f2010-09-06 20:51:06 +03001236 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1237 wpa_supplicant_cancel_scan(wpa_s);
1238 wpas_clear_wps(wpa_s);
1239 } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1240 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1241 "deauthenticate");
Jouni Malinenc2805902015-03-01 15:54:24 +02001242 wpa_s->own_disconnect_req = 1;
Ardong Chen2f9929f2010-09-06 20:51:06 +03001243 wpa_supplicant_deauthenticate(wpa_s,
1244 WLAN_REASON_DEAUTH_LEAVING);
1245 wpas_clear_wps(wpa_s);
Sunil Duttec947ff2012-10-04 21:11:04 +03001246 } else {
1247 wpas_wps_reenable_networks(wpa_s);
Jouni Malinenf9f05262012-08-27 13:48:11 +03001248 wpas_wps_clear_ap_info(wpa_s);
Jouni Malinen5890fa82014-01-01 22:00:09 +02001249 if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1250 0)
1251 wpas_clear_wps(wpa_s);
Sunil Duttec947ff2012-10-04 21:11:04 +03001252 }
Ardong Chen2f9929f2010-09-06 20:51:06 +03001253
Jouni Malinen72559832013-10-20 21:34:39 +03001254 wpa_s->after_wps = 0;
1255
Ardong Chen2f9929f2010-09-06 20:51:06 +03001256 return 0;
1257}
1258
1259
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001260int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
Jouni Malinen52eb2932009-09-06 13:58:15 +03001261 const char *pin, struct wps_new_ap_settings *settings)
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001262{
1263 struct wpa_ssid *ssid;
Jouni Malinen52eb2932009-09-06 13:58:15 +03001264 char val[200];
1265 char *pos, *end;
1266 int res;
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001267
Jouni Malinenbd47d682015-01-02 17:09:38 +02001268#ifdef CONFIG_AP
1269 if (wpa_s->ap_iface) {
1270 wpa_printf(MSG_DEBUG,
1271 "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1272 return -1;
1273 }
1274#endif /* CONFIG_AP */
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001275 if (!pin)
1276 return -1;
1277 wpas_clear_wps(wpa_s);
Jouni Malinen23318be2013-09-07 15:44:29 -07001278 ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001279 if (ssid == NULL)
1280 return -1;
Jouni Malinen2ff99b32010-07-18 14:30:24 -07001281 ssid->temporary = 1;
Jouni Malinen52eb2932009-09-06 13:58:15 +03001282 pos = val;
1283 end = pos + sizeof(val);
1284 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
Jouni Malinend85e1fc2014-12-08 11:15:51 +02001285 if (os_snprintf_error(end - pos, res))
Jouni Malinen52eb2932009-09-06 13:58:15 +03001286 return -1;
1287 pos += res;
1288 if (settings) {
1289 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1290 "new_encr=%s new_key=%s",
1291 settings->ssid_hex, settings->auth,
1292 settings->encr, settings->key_hex);
Jouni Malinend85e1fc2014-12-08 11:15:51 +02001293 if (os_snprintf_error(end - pos, res))
Jouni Malinen52eb2932009-09-06 13:58:15 +03001294 return -1;
1295 pos += res;
1296 }
1297 res = os_snprintf(pos, end - pos, "\"");
Jouni Malinend85e1fc2014-12-08 11:15:51 +02001298 if (os_snprintf_error(end - pos, res))
Jouni Malinen52eb2932009-09-06 13:58:15 +03001299 return -1;
Jouni Malinen9c75e9f2013-01-01 20:30:17 +02001300 if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1301 return -1;
Jouni Malinen42f50262010-07-22 10:24:23 -07001302 if (wpa_s->wps_fragment_size)
1303 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001304 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1305 wpa_s, NULL);
Jouni Malinen91a65012013-11-28 17:48:35 +02001306 wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001307 return 0;
1308}
1309
1310
Jouni Malinen52177fb2013-09-01 10:47:34 +03001311static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1312 const u8 *p2p_dev_addr, const u8 *psk,
Jouni Malinenc5adf522009-01-03 20:18:35 +02001313 size_t psk_len)
1314{
Jouni Malinen52177fb2013-09-01 10:47:34 +03001315 if (is_zero_ether_addr(p2p_dev_addr)) {
1316 wpa_printf(MSG_DEBUG,
1317 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1318 MAC2STR(mac_addr));
1319 } else {
1320 wpa_printf(MSG_DEBUG,
1321 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1322 " P2P Device Addr " MACSTR,
1323 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1324 }
Jouni Malinenc5adf522009-01-03 20:18:35 +02001325 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1326
1327 /* TODO */
1328
1329 return 0;
1330}
1331
1332
1333static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1334 const struct wps_device_data *dev)
1335{
1336 char uuid[40], txt[400];
1337 int len;
Jouni Malinen96750ea2009-11-26 11:39:29 +02001338 char devtype[WPS_DEV_TYPE_BUFSIZE];
Jouni Malinenc5adf522009-01-03 20:18:35 +02001339 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1340 return;
1341 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1342 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
Jouni Malinen96750ea2009-11-26 11:39:29 +02001343 " [%s|%s|%s|%s|%s|%s]",
Jouni Malinenc5adf522009-01-03 20:18:35 +02001344 uuid, MAC2STR(dev->mac_addr), dev->device_name,
1345 dev->manufacturer, dev->model_name,
1346 dev->model_number, dev->serial_number,
Jouni Malinen96750ea2009-11-26 11:39:29 +02001347 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1348 sizeof(devtype)));
Jouni Malinena80ba672014-12-07 12:15:34 +02001349 if (!os_snprintf_error(sizeof(txt), len))
Jouni Malinenc5adf522009-01-03 20:18:35 +02001350 wpa_printf(MSG_INFO, "%s", txt);
1351}
1352
1353
Jouni Malinen72df2f52009-11-13 22:07:11 +02001354static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1355 u16 sel_reg_config_methods)
1356{
1357#ifdef CONFIG_WPS_ER
1358 struct wpa_supplicant *wpa_s = ctx;
1359
1360 if (wpa_s->wps_er == NULL)
1361 return;
Jouni Malinenad474112010-07-18 22:53:15 -07001362 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1363 "dev_password_id=%u sel_reg_config_methods=0x%x",
1364 sel_reg, dev_passwd_id, sel_reg_config_methods);
Jouni Malinen72df2f52009-11-13 22:07:11 +02001365 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1366 sel_reg_config_methods);
1367#endif /* CONFIG_WPS_ER */
1368}
1369
1370
Jouni Malinen092acb52010-10-16 12:57:47 +03001371static u16 wps_fix_config_methods(u16 config_methods)
1372{
Jouni Malinen092acb52010-10-16 12:57:47 +03001373 if ((config_methods &
1374 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1375 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1376 wpa_printf(MSG_INFO, "WPS: Converting display to "
1377 "virtual_display for WPS 2.0 compliance");
1378 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1379 }
1380 if ((config_methods &
1381 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1382 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1383 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1384 "virtual_push_button for WPS 2.0 compliance");
1385 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1386 }
Jouni Malinen092acb52010-10-16 12:57:47 +03001387
1388 return config_methods;
1389}
1390
1391
Jouni Malinen85a821d2011-03-17 18:07:00 +02001392static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1393 struct wps_context *wps)
1394{
Jouni Malinenef93abd2013-12-30 16:50:08 +02001395 char buf[50];
1396 const char *src;
1397
Jouni Malinen85a821d2011-03-17 18:07:00 +02001398 if (is_nil_uuid(wpa_s->conf->uuid)) {
1399 struct wpa_supplicant *first;
1400 first = wpa_s->global->ifaces;
1401 while (first && first->next)
1402 first = first->next;
1403 if (first && first != wpa_s) {
Ben Greear157cdad2012-05-10 10:56:51 +03001404 if (wps != wpa_s->global->ifaces->wps)
1405 os_memcpy(wps->uuid,
1406 wpa_s->global->ifaces->wps->uuid,
1407 WPS_UUID_LEN);
Jouni Malinenef93abd2013-12-30 16:50:08 +02001408 src = "from the first interface";
Jouni Malinen85a821d2011-03-17 18:07:00 +02001409 } else {
1410 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
Jouni Malinenef93abd2013-12-30 16:50:08 +02001411 src = "based on MAC address";
Jouni Malinen85a821d2011-03-17 18:07:00 +02001412 }
1413 } else {
1414 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
Jouni Malinenef93abd2013-12-30 16:50:08 +02001415 src = "based on configuration";
Jouni Malinen85a821d2011-03-17 18:07:00 +02001416 }
Jouni Malinenef93abd2013-12-30 16:50:08 +02001417
1418 uuid_bin2str(wps->uuid, buf, sizeof(buf));
1419 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
Jouni Malinen85a821d2011-03-17 18:07:00 +02001420}
1421
1422
Anirban Sirkhell71dd3b72012-04-04 00:08:57 +03001423static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1424 struct wps_context *wps)
1425{
1426 wpabuf_free(wps->dev.vendor_ext_m1);
1427 wps->dev.vendor_ext_m1 = NULL;
1428
1429 if (wpa_s->conf->wps_vendor_ext_m1) {
1430 wps->dev.vendor_ext_m1 =
1431 wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1432 if (!wps->dev.vendor_ext_m1) {
1433 wpa_printf(MSG_ERROR, "WPS: Cannot "
1434 "allocate memory for vendor_ext_m1");
1435 }
1436 }
1437}
1438
1439
Jouni Malinen116654c2008-11-28 20:32:13 +02001440int wpas_wps_init(struct wpa_supplicant *wpa_s)
1441{
1442 struct wps_context *wps;
Jouni Malinenc5adf522009-01-03 20:18:35 +02001443 struct wps_registrar_config rcfg;
Bharat Chakravarty6fb6d842011-08-31 17:53:55 +03001444 struct hostapd_hw_modes *modes;
Christian Lamparter6bf731e2011-10-23 11:58:54 +03001445 u16 m;
Jouni Malinen116654c2008-11-28 20:32:13 +02001446
1447 wps = os_zalloc(sizeof(*wps));
1448 if (wps == NULL)
1449 return -1;
1450
1451 wps->cred_cb = wpa_supplicant_wps_cred;
Jouni Malinen4b682902008-12-18 21:58:42 +02001452 wps->event_cb = wpa_supplicant_wps_event;
David Spinadel239abaf2013-08-25 10:55:53 +03001453 wps->rf_band_cb = wpa_supplicant_wps_rf_band;
Jouni Malinen116654c2008-11-28 20:32:13 +02001454 wps->cb_ctx = wpa_s;
1455
Jouni Malinen3c0b7aa2008-12-13 17:20:46 +02001456 wps->dev.device_name = wpa_s->conf->device_name;
1457 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1458 wps->dev.model_name = wpa_s->conf->model_name;
1459 wps->dev.model_number = wpa_s->conf->model_number;
1460 wps->dev.serial_number = wpa_s->conf->serial_number;
Jouni Malinenc0e4dd92009-12-21 15:59:25 +02001461 wps->config_methods =
1462 wps_config_methods_str2bin(wpa_s->conf->config_methods);
Jouni Malinen979b9882010-07-17 20:30:49 -07001463 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1464 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1465 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1466 "methods are not allowed at the same time");
1467 os_free(wps);
1468 return -1;
1469 }
Jouni Malinen092acb52010-10-16 12:57:47 +03001470 wps->config_methods = wps_fix_config_methods(wps->config_methods);
Bharat Chakravartya9355fa2011-09-01 15:24:45 +03001471 wps->dev.config_methods = wps->config_methods;
Johannes Berg2f646b62011-03-17 18:50:22 +02001472 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1473 WPS_DEV_TYPE_LEN);
Jean-Michel Bachota9e86bf2011-03-17 11:09:49 +02001474
Johannes Berg2f646b62011-03-17 18:50:22 +02001475 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1476 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1477 WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
Jean-Michel Bachota9e86bf2011-03-17 11:09:49 +02001478
Anirban Sirkhell71dd3b72012-04-04 00:08:57 +03001479 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1480
Jouni Malinen3c0b7aa2008-12-13 17:20:46 +02001481 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
Christian Lamparter6bf731e2011-10-23 11:58:54 +03001482 modes = wpa_s->hw.modes;
Bharat Chakravarty6fb6d842011-08-31 17:53:55 +03001483 if (modes) {
Christian Lamparter6bf731e2011-10-23 11:58:54 +03001484 for (m = 0; m < wpa_s->hw.num_modes; m++) {
Bharat Chakravarty6fb6d842011-08-31 17:53:55 +03001485 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1486 modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1487 wps->dev.rf_bands |= WPS_RF_24GHZ;
1488 else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1489 wps->dev.rf_bands |= WPS_RF_50GHZ;
1490 }
Bharat Chakravarty6fb6d842011-08-31 17:53:55 +03001491 }
1492 if (wps->dev.rf_bands == 0) {
1493 /*
1494 * Default to claiming support for both bands if the driver
1495 * does not provide support for fetching supported bands.
1496 */
1497 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1498 }
Jouni Malinen398cfbf2008-11-28 20:44:47 +02001499 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
Jouni Malinen85a821d2011-03-17 18:07:00 +02001500 wpas_wps_set_uuid(wpa_s, wps);
Jouni Malinen116654c2008-11-28 20:32:13 +02001501
Jouni Malinenc5adf522009-01-03 20:18:35 +02001502 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1503 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1504
1505 os_memset(&rcfg, 0, sizeof(rcfg));
1506 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1507 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
Jouni Malinen72df2f52009-11-13 22:07:11 +02001508 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
Jouni Malinenc5adf522009-01-03 20:18:35 +02001509 rcfg.cb_ctx = wpa_s;
1510
1511 wps->registrar = wps_registrar_init(wps, &rcfg);
1512 if (wps->registrar == NULL) {
1513 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1514 os_free(wps);
1515 return -1;
1516 }
1517
Jouni Malinen116654c2008-11-28 20:32:13 +02001518 wpa_s->wps = wps;
1519
1520 return 0;
1521}
1522
1523
Jouni Malinen50d1f892013-04-11 21:37:41 -07001524#ifdef CONFIG_WPS_ER
1525static void wpas_wps_nfc_clear(struct wps_context *wps)
1526{
1527 wps->ap_nfc_dev_pw_id = 0;
1528 wpabuf_free(wps->ap_nfc_dh_pubkey);
1529 wps->ap_nfc_dh_pubkey = NULL;
1530 wpabuf_free(wps->ap_nfc_dh_privkey);
1531 wps->ap_nfc_dh_privkey = NULL;
1532 wpabuf_free(wps->ap_nfc_dev_pw);
1533 wps->ap_nfc_dev_pw = NULL;
1534}
1535#endif /* CONFIG_WPS_ER */
1536
1537
Jouni Malinen116654c2008-11-28 20:32:13 +02001538void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1539{
Jouni Malinen150fd0b2014-04-13 11:47:03 +03001540 wpas_wps_assoc_with_cred_cancel(wpa_s);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001541 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
Jouni Malinen2e145e92013-08-23 17:43:01 +03001542 eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
Sunil Duttec947ff2012-10-04 21:11:04 +03001543 eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
Jouni Malinenf9f05262012-08-27 13:48:11 +03001544 wpas_wps_clear_ap_info(wpa_s);
Jouni Malinenfcc60db2008-11-29 20:59:45 +02001545
Alexandru Costachefb2ac532014-05-12 19:14:55 +03001546#ifdef CONFIG_P2P
1547 eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1548#endif /* CONFIG_P2P */
1549
Jouni Malinen116654c2008-11-28 20:32:13 +02001550 if (wpa_s->wps == NULL)
1551 return;
1552
Jouni Malinena6b63072009-11-15 22:52:17 +02001553#ifdef CONFIG_WPS_ER
Jouni Malinen1a1bf002009-12-19 23:47:54 +02001554 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
Jouni Malinena6b63072009-11-15 22:52:17 +02001555 wpa_s->wps_er = NULL;
Jouni Malinen50d1f892013-04-11 21:37:41 -07001556 wpas_wps_nfc_clear(wpa_s->wps);
Jouni Malinena6b63072009-11-15 22:52:17 +02001557#endif /* CONFIG_WPS_ER */
1558
Jouni Malinenc5adf522009-01-03 20:18:35 +02001559 wps_registrar_deinit(wpa_s->wps->registrar);
Masashi Honma46bdb832009-02-26 21:57:38 +02001560 wpabuf_free(wpa_s->wps->dh_pubkey);
1561 wpabuf_free(wpa_s->wps->dh_privkey);
Anirban Sirkhell71dd3b72012-04-04 00:08:57 +03001562 wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
Jouni Malinen116654c2008-11-28 20:32:13 +02001563 os_free(wpa_s->wps->network_key);
1564 os_free(wpa_s->wps);
1565 wpa_s->wps = NULL;
1566}
Jouni Malinen351f09a2008-11-29 22:06:34 +02001567
1568
Jouni Malinena6099152009-01-23 13:10:58 +02001569int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
Jouni Malinen620c7832012-09-02 19:56:57 +03001570 struct wpa_ssid *ssid, struct wpa_bss *bss)
Jouni Malinen351f09a2008-11-29 22:06:34 +02001571{
1572 struct wpabuf *wps_ie;
1573
1574 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1575 return -1;
1576
Jouni Malinen620c7832012-09-02 19:56:57 +03001577 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Jouni Malinen351f09a2008-11-29 22:06:34 +02001578 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1579 if (!wps_ie) {
1580 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1581 return 0;
1582 }
1583
1584 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1585 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1586 "without active PBC Registrar");
1587 wpabuf_free(wps_ie);
1588 return 0;
1589 }
1590
1591 /* TODO: overlap detection */
1592 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1593 "(Active PBC)");
1594 wpabuf_free(wps_ie);
1595 return 1;
1596 }
1597
1598 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1599 if (!wps_ie) {
1600 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
1601 return 0;
1602 }
1603
Jouni Malinena6099152009-01-23 13:10:58 +02001604 /*
Jouni Malinen31fcea92009-12-12 16:40:10 +02001605 * Start with WPS APs that advertise our address as an
1606 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1607 * allow any WPS AP after couple of scans since some APs do not
1608 * set Selected Registrar attribute properly when using
1609 * external Registrar.
Jouni Malinena6099152009-01-23 13:10:58 +02001610 */
Jouni Malinen31fcea92009-12-12 16:40:10 +02001611 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
Jouni Malinena6099152009-01-23 13:10:58 +02001612 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1613 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1614 "without active PIN Registrar");
1615 wpabuf_free(wps_ie);
1616 return 0;
1617 }
1618 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1619 } else {
1620 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
Jouni Malinen31fcea92009-12-12 16:40:10 +02001621 "(Authorized MAC or Active PIN)");
Jouni Malinen351f09a2008-11-29 22:06:34 +02001622 }
Jouni Malinen351f09a2008-11-29 22:06:34 +02001623 wpabuf_free(wps_ie);
1624 return 1;
1625 }
1626
1627 if (wps_ie) {
1628 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1629 wpabuf_free(wps_ie);
1630 return 1;
1631 }
1632
1633 return -1;
1634}
1635
1636
Jouni Malinena6099152009-01-23 13:10:58 +02001637int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1638 struct wpa_ssid *ssid,
Jouni Malinen620c7832012-09-02 19:56:57 +03001639 struct wpa_bss *bss)
Jouni Malinen351f09a2008-11-29 22:06:34 +02001640{
1641 struct wpabuf *wps_ie = NULL;
1642 int ret = 0;
1643
1644 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
Jouni Malinen620c7832012-09-02 19:56:57 +03001645 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Jouni Malinen351f09a2008-11-29 22:06:34 +02001646 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1647 /* allow wildcard SSID for WPS PBC */
1648 ret = 1;
1649 }
1650 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
Jouni Malinen620c7832012-09-02 19:56:57 +03001651 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Jouni Malinena6099152009-01-23 13:10:58 +02001652 if (wps_ie &&
Jouni Malinen31fcea92009-12-12 16:40:10 +02001653 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
Jouni Malinena6099152009-01-23 13:10:58 +02001654 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
Jouni Malinen351f09a2008-11-29 22:06:34 +02001655 /* allow wildcard SSID for WPS PIN */
1656 ret = 1;
1657 }
1658 }
1659
Jouni Malinen24c23d12008-12-20 22:55:02 +02001660 if (!ret && ssid->bssid_set &&
1661 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1662 /* allow wildcard SSID due to hardcoded BSSID match */
1663 ret = 1;
1664 }
1665
Jouni Malinen54f489b2010-05-26 17:16:14 +03001666#ifdef CONFIG_WPS_STRICT
1667 if (wps_ie) {
1668 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
Jouni Malinenff28cca2010-09-20 14:54:22 -07001669 0, bss->bssid) < 0)
Jouni Malinen54f489b2010-05-26 17:16:14 +03001670 ret = 0;
1671 if (bss->beacon_ie_len) {
1672 struct wpabuf *bcn_wps;
Jouni Malinen620c7832012-09-02 19:56:57 +03001673 bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
Jouni Malinen54f489b2010-05-26 17:16:14 +03001674 bss, WPS_IE_VENDOR_TYPE);
1675 if (bcn_wps == NULL) {
1676 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1677 "missing from AP Beacon");
1678 ret = 0;
1679 } else {
1680 if (wps_validate_beacon(wps_ie) < 0)
1681 ret = 0;
1682 wpabuf_free(bcn_wps);
1683 }
1684 }
1685 }
1686#endif /* CONFIG_WPS_STRICT */
1687
Jouni Malinen351f09a2008-11-29 22:06:34 +02001688 wpabuf_free(wps_ie);
1689
1690 return ret;
1691}
1692
1693
1694int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
Jouni Malinen6fa81a32010-01-02 16:16:02 +02001695 struct wpa_bss *selected, struct wpa_ssid *ssid)
Jouni Malinen351f09a2008-11-29 22:06:34 +02001696{
1697 const u8 *sel_uuid, *uuid;
Jouni Malinen351f09a2008-11-29 22:06:34 +02001698 struct wpabuf *wps_ie;
1699 int ret = 0;
Jouni Malinen6fa81a32010-01-02 16:16:02 +02001700 struct wpa_bss *bss;
Jouni Malinen351f09a2008-11-29 22:06:34 +02001701
1702 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1703 return 0;
1704
Jouni Malinen96471202010-09-20 15:08:52 -07001705 wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1706 "present in scan results; selected BSSID " MACSTR,
1707 MAC2STR(selected->bssid));
1708
Jouni Malinen351f09a2008-11-29 22:06:34 +02001709 /* Make sure that only one AP is in active PBC mode */
Jouni Malinen6fa81a32010-01-02 16:16:02 +02001710 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
Jouni Malinen96471202010-09-20 15:08:52 -07001711 if (wps_ie) {
Jouni Malinen351f09a2008-11-29 22:06:34 +02001712 sel_uuid = wps_get_uuid_e(wps_ie);
Jouni Malinen96471202010-09-20 15:08:52 -07001713 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1714 sel_uuid, UUID_LEN);
1715 } else {
1716 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1717 "WPS IE?!");
Jouni Malinen351f09a2008-11-29 22:06:34 +02001718 sel_uuid = NULL;
Jouni Malinen96471202010-09-20 15:08:52 -07001719 }
Jouni Malinen351f09a2008-11-29 22:06:34 +02001720
Jouni Malinen6fa81a32010-01-02 16:16:02 +02001721 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Jouni Malinen351f09a2008-11-29 22:06:34 +02001722 struct wpabuf *ie;
1723 if (bss == selected)
1724 continue;
Jouni Malinen6fa81a32010-01-02 16:16:02 +02001725 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Jouni Malinen351f09a2008-11-29 22:06:34 +02001726 if (!ie)
1727 continue;
1728 if (!wps_is_selected_pbc_registrar(ie)) {
1729 wpabuf_free(ie);
1730 continue;
1731 }
Jouni Malinen96471202010-09-20 15:08:52 -07001732 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1733 MACSTR, MAC2STR(bss->bssid));
Jouni Malinen351f09a2008-11-29 22:06:34 +02001734 uuid = wps_get_uuid_e(ie);
Jouni Malinen96471202010-09-20 15:08:52 -07001735 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1736 uuid, UUID_LEN);
Jouni Malinen4a393fe2014-12-24 18:40:25 +02001737 if (os_memcmp(selected->bssid, bss->bssid, ETH_ALEN) == 0) {
1738 wpabuf_free(ie);
1739 continue;
1740 }
Jouni Malinen44cd4302008-12-21 17:28:01 +02001741 if (sel_uuid == NULL || uuid == NULL ||
Jouni Malinen96471202010-09-20 15:08:52 -07001742 os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
Jouni Malinen351f09a2008-11-29 22:06:34 +02001743 ret = 1; /* PBC overlap */
Jouni Malinen96471202010-09-20 15:08:52 -07001744 wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1745 MACSTR " and " MACSTR,
1746 MAC2STR(selected->bssid),
1747 MAC2STR(bss->bssid));
Jouni Malinen351f09a2008-11-29 22:06:34 +02001748 wpabuf_free(ie);
1749 break;
1750 }
1751
1752 /* TODO: verify that this is reasonable dual-band situation */
Jouni Malinen484957a2008-12-15 19:50:53 +02001753
1754 wpabuf_free(ie);
Jouni Malinen351f09a2008-11-29 22:06:34 +02001755 }
1756
1757 wpabuf_free(wps_ie);
1758
1759 return ret;
1760}
Jouni Malinena524f052008-12-15 20:09:57 +02001761
1762
1763void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1764{
Jouni Malinen6fa81a32010-01-02 16:16:02 +02001765 struct wpa_bss *bss;
Jouni Malinen6c6ad812011-01-05 11:49:03 +02001766 unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
Jouni Malinena524f052008-12-15 20:09:57 +02001767
1768 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1769 return;
1770
Jouni Malinen6fa81a32010-01-02 16:16:02 +02001771 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Jouni Malinena524f052008-12-15 20:09:57 +02001772 struct wpabuf *ie;
Jouni Malinen6fa81a32010-01-02 16:16:02 +02001773 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
Jouni Malinena524f052008-12-15 20:09:57 +02001774 if (!ie)
1775 continue;
1776 if (wps_is_selected_pbc_registrar(ie))
Jouni Malinen6c6ad812011-01-05 11:49:03 +02001777 pbc++;
Jouni Malinen31fcea92009-12-12 16:40:10 +02001778 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
Jouni Malinen6c6ad812011-01-05 11:49:03 +02001779 auth++;
Jouni Malinena524f052008-12-15 20:09:57 +02001780 else if (wps_is_selected_pin_registrar(ie))
Jouni Malinen6c6ad812011-01-05 11:49:03 +02001781 pin++;
Jouni Malinena524f052008-12-15 20:09:57 +02001782 else
Jouni Malinen6c6ad812011-01-05 11:49:03 +02001783 wps++;
Jouni Malinena524f052008-12-15 20:09:57 +02001784 wpabuf_free(ie);
Jouni Malinena524f052008-12-15 20:09:57 +02001785 }
Jouni Malinen6c6ad812011-01-05 11:49:03 +02001786
1787 if (pbc)
1788 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1789 else if (auth)
1790 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1791 else if (pin)
1792 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1793 else if (wps)
1794 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
Jouni Malinena524f052008-12-15 20:09:57 +02001795}
Jouni Malinen24c23d12008-12-20 22:55:02 +02001796
1797
1798int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1799{
1800 struct wpa_ssid *ssid;
1801
1802 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1803 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1804 return 1;
1805 }
1806
1807 return 0;
1808}
Jouni Malinen611ed492009-09-11 17:14:49 +03001809
1810
1811int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1812 char *end)
1813{
1814 struct wpabuf *wps_ie;
1815 int ret;
1816
1817 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1818 if (wps_ie == NULL)
1819 return 0;
1820
1821 ret = wps_attr_text(wps_ie, buf, end);
1822 wpabuf_free(wps_ie);
1823 return ret;
1824}
Jouni Malinene9bcfeb2009-11-07 12:41:01 +02001825
1826
Jouni Malinen08486682010-05-27 15:23:55 +03001827int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
Jouni Malinene9bcfeb2009-11-07 12:41:01 +02001828{
1829#ifdef CONFIG_WPS_ER
1830 if (wpa_s->wps_er) {
Jouni Malinenb3f371c2009-11-20 21:57:30 +02001831 wps_er_refresh(wpa_s->wps_er);
Jouni Malinene9bcfeb2009-11-07 12:41:01 +02001832 return 0;
1833 }
Jouni Malinen08486682010-05-27 15:23:55 +03001834 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
Jouni Malinene9bcfeb2009-11-07 12:41:01 +02001835 if (wpa_s->wps_er == NULL)
1836 return -1;
1837 return 0;
1838#else /* CONFIG_WPS_ER */
1839 return 0;
1840#endif /* CONFIG_WPS_ER */
1841}
1842
1843
Jouni Malinenf77cedc2014-12-13 19:50:16 +02001844void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
Jouni Malinene9bcfeb2009-11-07 12:41:01 +02001845{
1846#ifdef CONFIG_WPS_ER
Jouni Malinen1a1bf002009-12-19 23:47:54 +02001847 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
Jouni Malinene9bcfeb2009-11-07 12:41:01 +02001848 wpa_s->wps_er = NULL;
1849#endif /* CONFIG_WPS_ER */
Jouni Malinene9bcfeb2009-11-07 12:41:01 +02001850}
Jouni Malinen72df2f52009-11-13 22:07:11 +02001851
1852
1853#ifdef CONFIG_WPS_ER
Jouni Malinen31fcea92009-12-12 16:40:10 +02001854int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1855 const char *uuid, const char *pin)
Jouni Malinen72df2f52009-11-13 22:07:11 +02001856{
1857 u8 u[UUID_LEN];
Jouni Malinene2054012013-04-01 20:52:44 +03001858 const u8 *use_uuid = NULL;
1859 u8 addr_buf[ETH_ALEN];
Jouni Malinen72df2f52009-11-13 22:07:11 +02001860
Jouni Malinene2054012013-04-01 20:52:44 +03001861 if (os_strcmp(uuid, "any") == 0) {
1862 } else if (uuid_str2bin(uuid, u) == 0) {
1863 use_uuid = u;
1864 } else if (hwaddr_aton(uuid, addr_buf) == 0) {
1865 use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
1866 if (use_uuid == NULL)
1867 return -1;
1868 } else
Jouni Malinen72df2f52009-11-13 22:07:11 +02001869 return -1;
Jouni Malinen31fcea92009-12-12 16:40:10 +02001870 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
Jouni Malinene2054012013-04-01 20:52:44 +03001871 use_uuid,
Jouni Malinen72df2f52009-11-13 22:07:11 +02001872 (const u8 *) pin, os_strlen(pin), 300);
1873}
Jouni Malinen564cd7f2009-11-15 18:46:03 +02001874
1875
1876int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1877{
Jouni Malinene2054012013-04-01 20:52:44 +03001878 u8 u[UUID_LEN], *use_uuid = NULL;
1879 u8 addr[ETH_ALEN], *use_addr = NULL;
Jouni Malinen564cd7f2009-11-15 18:46:03 +02001880
Jouni Malinene2054012013-04-01 20:52:44 +03001881 if (uuid_str2bin(uuid, u) == 0)
1882 use_uuid = u;
1883 else if (hwaddr_aton(uuid, addr) == 0)
1884 use_addr = addr;
1885 else
Jouni Malinen564cd7f2009-11-15 18:46:03 +02001886 return -1;
Jouni Malinene2054012013-04-01 20:52:44 +03001887 return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
Jouni Malinen564cd7f2009-11-15 18:46:03 +02001888}
Jouni Malinene64dcfd2009-11-15 22:27:06 +02001889
1890
1891int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1892 const char *pin)
1893{
Jouni Malinen59307b32013-04-01 20:32:09 +03001894 u8 u[UUID_LEN], *use_uuid = NULL;
1895 u8 addr[ETH_ALEN], *use_addr = NULL;
Jouni Malinene64dcfd2009-11-15 22:27:06 +02001896
Jouni Malinen59307b32013-04-01 20:32:09 +03001897 if (uuid_str2bin(uuid, u) == 0)
1898 use_uuid = u;
1899 else if (hwaddr_aton(uuid, addr) == 0)
1900 use_addr = addr;
1901 else
Jouni Malinene64dcfd2009-11-15 22:27:06 +02001902 return -1;
Jouni Malinen59307b32013-04-01 20:32:09 +03001903
1904 return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
Jouni Malinene64dcfd2009-11-15 22:27:06 +02001905 os_strlen(pin));
1906}
Jouni Malinen1a1bf002009-12-19 23:47:54 +02001907
1908
Jouni Malinen88c8bf32013-04-01 21:28:57 +03001909static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
1910 struct wps_credential *cred)
1911{
1912 os_memset(cred, 0, sizeof(*cred));
1913 if (ssid->ssid_len > 32)
1914 return -1;
1915 os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
1916 cred->ssid_len = ssid->ssid_len;
1917 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1918 cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1919 WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1920 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1921 cred->encr_type = WPS_ENCR_AES;
1922 else
1923 cred->encr_type = WPS_ENCR_TKIP;
1924 if (ssid->passphrase) {
1925 cred->key_len = os_strlen(ssid->passphrase);
1926 if (cred->key_len >= 64)
1927 return -1;
1928 os_memcpy(cred->key, ssid->passphrase, cred->key_len);
1929 } else if (ssid->psk_set) {
1930 cred->key_len = 32;
1931 os_memcpy(cred->key, ssid->psk, 32);
1932 } else
1933 return -1;
1934 } else {
1935 cred->auth_type = WPS_AUTH_OPEN;
1936 cred->encr_type = WPS_ENCR_NONE;
1937 }
1938
1939 return 0;
1940}
1941
1942
Jouni Malinenef10f472010-10-20 13:37:01 +03001943int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1944 int id)
1945{
Jouni Malinen59307b32013-04-01 20:32:09 +03001946 u8 u[UUID_LEN], *use_uuid = NULL;
1947 u8 addr[ETH_ALEN], *use_addr = NULL;
Jouni Malinenef10f472010-10-20 13:37:01 +03001948 struct wpa_ssid *ssid;
1949 struct wps_credential cred;
Jouni Malinenf42df142014-12-29 13:07:00 +02001950 int ret;
Jouni Malinenef10f472010-10-20 13:37:01 +03001951
Jouni Malinen59307b32013-04-01 20:32:09 +03001952 if (uuid_str2bin(uuid, u) == 0)
1953 use_uuid = u;
1954 else if (hwaddr_aton(uuid, addr) == 0)
1955 use_addr = addr;
1956 else
Jouni Malinenef10f472010-10-20 13:37:01 +03001957 return -1;
1958 ssid = wpa_config_get_network(wpa_s->conf, id);
1959 if (ssid == NULL || ssid->ssid == NULL)
1960 return -1;
1961
Jouni Malinen88c8bf32013-04-01 21:28:57 +03001962 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
Jouni Malinenef10f472010-10-20 13:37:01 +03001963 return -1;
Jouni Malinenf42df142014-12-29 13:07:00 +02001964 ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
1965 os_memset(&cred, 0, sizeof(cred));
1966 return ret;
Jouni Malinenef10f472010-10-20 13:37:01 +03001967}
1968
1969
Jouni Malinen7d6640a2010-05-28 00:01:48 +03001970int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1971 const char *pin, struct wps_new_ap_settings *settings)
1972{
Jouni Malinen59307b32013-04-01 20:32:09 +03001973 u8 u[UUID_LEN], *use_uuid = NULL;
1974 u8 addr[ETH_ALEN], *use_addr = NULL;
Jouni Malinen7d6640a2010-05-28 00:01:48 +03001975 struct wps_credential cred;
1976 size_t len;
1977
Jouni Malinen59307b32013-04-01 20:32:09 +03001978 if (uuid_str2bin(uuid, u) == 0)
1979 use_uuid = u;
1980 else if (hwaddr_aton(uuid, addr) == 0)
1981 use_addr = addr;
1982 else
Jouni Malinen7d6640a2010-05-28 00:01:48 +03001983 return -1;
1984 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1985 settings->encr == NULL || settings->key_hex == NULL)
1986 return -1;
1987
1988 os_memset(&cred, 0, sizeof(cred));
1989 len = os_strlen(settings->ssid_hex);
1990 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1991 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1992 return -1;
1993 cred.ssid_len = len / 2;
1994
1995 len = os_strlen(settings->key_hex);
1996 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1997 hexstr2bin(settings->key_hex, cred.key, len / 2))
1998 return -1;
1999 cred.key_len = len / 2;
2000
2001 if (os_strcmp(settings->auth, "OPEN") == 0)
2002 cred.auth_type = WPS_AUTH_OPEN;
2003 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2004 cred.auth_type = WPS_AUTH_WPAPSK;
2005 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2006 cred.auth_type = WPS_AUTH_WPA2PSK;
2007 else
2008 return -1;
2009
2010 if (os_strcmp(settings->encr, "NONE") == 0)
2011 cred.encr_type = WPS_ENCR_NONE;
Jouni Malinendc390042014-04-06 11:18:10 +03002012#ifdef CONFIG_TESTING_OPTIONS
Jouni Malinen7d6640a2010-05-28 00:01:48 +03002013 else if (os_strcmp(settings->encr, "WEP") == 0)
2014 cred.encr_type = WPS_ENCR_WEP;
Jouni Malinendc390042014-04-06 11:18:10 +03002015#endif /* CONFIG_TESTING_OPTIONS */
Jouni Malinen7d6640a2010-05-28 00:01:48 +03002016 else if (os_strcmp(settings->encr, "TKIP") == 0)
2017 cred.encr_type = WPS_ENCR_TKIP;
2018 else if (os_strcmp(settings->encr, "CCMP") == 0)
2019 cred.encr_type = WPS_ENCR_AES;
2020 else
2021 return -1;
2022
Jouni Malinen59307b32013-04-01 20:32:09 +03002023 return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2024 (const u8 *) pin, os_strlen(pin), &cred);
Jouni Malinen7d6640a2010-05-28 00:01:48 +03002025}
2026
2027
Jouni Malinen1cea09a2012-06-28 16:35:51 +03002028#ifdef CONFIG_WPS_NFC
2029struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2030 int ndef, const char *uuid)
2031{
2032 struct wpabuf *ret;
Jouni Malinen59307b32013-04-01 20:32:09 +03002033 u8 u[UUID_LEN], *use_uuid = NULL;
2034 u8 addr[ETH_ALEN], *use_addr = NULL;
Jouni Malinen1cea09a2012-06-28 16:35:51 +03002035
2036 if (!wpa_s->wps_er)
2037 return NULL;
2038
Jouni Malinen59307b32013-04-01 20:32:09 +03002039 if (uuid_str2bin(uuid, u) == 0)
2040 use_uuid = u;
2041 else if (hwaddr_aton(uuid, addr) == 0)
2042 use_addr = addr;
2043 else
Jouni Malinen1cea09a2012-06-28 16:35:51 +03002044 return NULL;
2045
Jouni Malinen59307b32013-04-01 20:32:09 +03002046 ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
Jouni Malinen1cea09a2012-06-28 16:35:51 +03002047 if (ndef && ret) {
2048 struct wpabuf *tmp;
2049 tmp = ndef_build_wifi(ret);
2050 wpabuf_free(ret);
2051 if (tmp == NULL)
2052 return NULL;
2053 ret = tmp;
2054 }
2055
2056 return ret;
2057}
2058#endif /* CONFIG_WPS_NFC */
2059
2060
Ben Greear4c9695b2011-02-21 21:47:46 +02002061static int callbacks_pending = 0;
2062
Jouni Malinen1a1bf002009-12-19 23:47:54 +02002063static void wpas_wps_terminate_cb(void *ctx)
2064{
2065 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
Ben Greear4c9695b2011-02-21 21:47:46 +02002066 if (--callbacks_pending <= 0)
2067 eloop_terminate();
Jouni Malinen1a1bf002009-12-19 23:47:54 +02002068}
Jouni Malinen72df2f52009-11-13 22:07:11 +02002069#endif /* CONFIG_WPS_ER */
Jouni Malinen1a1bf002009-12-19 23:47:54 +02002070
2071
2072int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2073{
2074#ifdef CONFIG_WPS_ER
2075 if (wpa_s->wps_er) {
Ben Greear4c9695b2011-02-21 21:47:46 +02002076 callbacks_pending++;
Jouni Malinen1a1bf002009-12-19 23:47:54 +02002077 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2078 wpa_s->wps_er = NULL;
2079 return 1;
2080 }
2081#endif /* CONFIG_WPS_ER */
2082 return 0;
2083}
Jouni Malinen41e650a2010-06-11 13:50:13 -07002084
2085
Jouni Malinen611aea72010-07-18 14:30:24 -07002086void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2087{
2088 struct wps_context *wps = wpa_s->wps;
2089
2090 if (wps == NULL)
2091 return;
2092
2093 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2094 wps->config_methods = wps_config_methods_str2bin(
2095 wpa_s->conf->config_methods);
2096 if ((wps->config_methods &
2097 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2098 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2099 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2100 "config methods are not allowed at the "
2101 "same time");
2102 wps->config_methods &= ~WPS_CONFIG_LABEL;
2103 }
2104 }
Jouni Malinen092acb52010-10-16 12:57:47 +03002105 wps->config_methods = wps_fix_config_methods(wps->config_methods);
jim1_linc8b245b2012-08-04 20:14:59 +03002106 wps->dev.config_methods = wps->config_methods;
Jouni Malinen611aea72010-07-18 14:30:24 -07002107
Johannes Berg2f646b62011-03-17 18:50:22 +02002108 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2109 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2110 WPS_DEV_TYPE_LEN);
Jouni Malinen611aea72010-07-18 14:30:24 -07002111
Jean-Michel Bachota9e86bf2011-03-17 11:09:49 +02002112 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
Johannes Berg2f646b62011-03-17 18:50:22 +02002113 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2114 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2115 wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
Jean-Michel Bachota9e86bf2011-03-17 11:09:49 +02002116 }
2117
Anirban Sirkhell71dd3b72012-04-04 00:08:57 +03002118 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2119 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2120
Jouni Malinen611aea72010-07-18 14:30:24 -07002121 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2122 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2123
Jouni Malinen85a821d2011-03-17 18:07:00 +02002124 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2125 wpas_wps_set_uuid(wpa_s, wps);
Jouni Malinendcf788d2010-07-06 16:32:55 -07002126
Jouni Malinen1c9cb492010-07-29 17:13:11 -07002127 if (wpa_s->conf->changed_parameters &
2128 (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
Jouni Malinendcf788d2010-07-06 16:32:55 -07002129 /* Update pointers to make sure they refer current values */
2130 wps->dev.device_name = wpa_s->conf->device_name;
2131 wps->dev.manufacturer = wpa_s->conf->manufacturer;
2132 wps->dev.model_name = wpa_s->conf->model_name;
2133 wps->dev.model_number = wpa_s->conf->model_number;
2134 wps->dev.serial_number = wpa_s->conf->serial_number;
2135 }
Jouni Malinen611aea72010-07-18 14:30:24 -07002136}
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002137
2138
2139#ifdef CONFIG_WPS_NFC
2140
Jouni Malinen88c8bf32013-04-01 21:28:57 +03002141#ifdef CONFIG_WPS_ER
2142static struct wpabuf *
2143wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2144 struct wpa_ssid *ssid)
Jouni Malinenbbf41862013-02-15 11:45:29 +02002145{
Jouni Malinen88c8bf32013-04-01 21:28:57 +03002146 struct wpabuf *ret;
2147 struct wps_credential cred;
2148
2149 if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2150 return NULL;
2151
2152 ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2153
2154 if (ndef && ret) {
2155 struct wpabuf *tmp;
2156 tmp = ndef_build_wifi(ret);
2157 wpabuf_free(ret);
2158 if (tmp == NULL)
2159 return NULL;
2160 ret = tmp;
2161 }
2162
2163 return ret;
2164}
2165#endif /* CONFIG_WPS_ER */
2166
2167
2168struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2169 int ndef, const char *id_str)
2170{
2171#ifdef CONFIG_WPS_ER
2172 if (id_str) {
2173 int id;
2174 char *end = NULL;
2175 struct wpa_ssid *ssid;
2176
2177 id = strtol(id_str, &end, 10);
2178 if (end && *end)
2179 return NULL;
2180
2181 ssid = wpa_config_get_network(wpa_s->conf, id);
2182 if (ssid == NULL)
2183 return NULL;
2184 return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2185 }
2186#endif /* CONFIG_WPS_ER */
Jouni Malinenbbf41862013-02-15 11:45:29 +02002187#ifdef CONFIG_AP
2188 if (wpa_s->ap_iface)
2189 return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2190#endif /* CONFIG_AP */
2191 return NULL;
2192}
2193
2194
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002195struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2196{
Jouni Malinen042ec552013-02-15 11:24:29 +02002197 if (wpa_s->conf->wps_nfc_pw_from_config) {
2198 return wps_nfc_token_build(ndef,
2199 wpa_s->conf->wps_nfc_dev_pw_id,
2200 wpa_s->conf->wps_nfc_dh_pubkey,
2201 wpa_s->conf->wps_nfc_dev_pw);
2202 }
2203
Jouni Malinenbfc62fe2012-06-28 20:54:53 +03002204 return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2205 &wpa_s->conf->wps_nfc_dh_pubkey,
2206 &wpa_s->conf->wps_nfc_dh_privkey,
2207 &wpa_s->conf->wps_nfc_dev_pw);
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002208}
2209
2210
Jouni Malinen23318be2013-09-07 15:44:29 -07002211int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2212 const u8 *bssid,
Jouni Malinen5f454552013-02-15 21:29:22 +02002213 const struct wpabuf *dev_pw, u16 dev_pw_id,
Jouni Malinen57630e62013-04-02 18:29:23 +03002214 int p2p_group, const u8 *peer_pubkey_hash,
Jouni Malinen91a65012013-11-28 17:48:35 +02002215 const u8 *ssid, size_t ssid_len, int freq)
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002216{
2217 struct wps_context *wps = wpa_s->wps;
2218 char pw[32 * 2 + 1];
2219
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002220 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
Jouni Malinen5f454552013-02-15 21:29:22 +02002221 dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2222 dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2223 }
2224
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002225 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002226 wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2227 wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2228 "cannot start NFC-triggered connection");
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002229 return -1;
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002230 }
2231
2232 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2233 wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2234 "cannot start NFC-triggered connection", dev_pw_id);
2235 return -1;
2236 }
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002237
2238 dh5_free(wps->dh_ctx);
2239 wpabuf_free(wps->dh_pubkey);
2240 wpabuf_free(wps->dh_privkey);
2241 wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2242 wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2243 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2244 wps->dh_ctx = NULL;
2245 wpabuf_free(wps->dh_pubkey);
2246 wps->dh_pubkey = NULL;
2247 wpabuf_free(wps->dh_privkey);
2248 wps->dh_privkey = NULL;
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002249 wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002250 return -1;
2251 }
2252 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
Jouni Malinen5c9d63d2013-04-01 19:27:32 +03002253 if (wps->dh_ctx == NULL) {
2254 wpabuf_free(wps->dh_pubkey);
2255 wps->dh_pubkey = NULL;
2256 wpabuf_free(wps->dh_privkey);
2257 wps->dh_privkey = NULL;
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002258 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002259 return -1;
Jouni Malinen5c9d63d2013-04-01 19:27:32 +03002260 }
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002261
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002262 if (dev_pw) {
2263 wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2264 wpabuf_head(dev_pw),
2265 wpabuf_len(dev_pw));
2266 }
Jouni Malinen23318be2013-09-07 15:44:29 -07002267 return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2268 dev_pw ? pw : NULL,
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002269 p2p_group, dev_pw_id, peer_pubkey_hash,
Jouni Malinen91a65012013-11-28 17:48:35 +02002270 ssid, ssid_len, freq);
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002271}
2272
Jouni Malinend7645d22012-06-28 13:59:29 +03002273
2274static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2275 struct wps_parse_attr *attr)
2276{
Jouni Malinen170f5662013-05-15 16:46:45 +03002277 /*
2278 * Disable existing networks temporarily to allow the newly learned
2279 * credential to be preferred. Enable the temporarily disabled networks
2280 * after 10 seconds.
2281 */
2282 wpas_wps_temp_disable(wpa_s, NULL);
2283 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2284 NULL);
2285
Jouni Malinend7645d22012-06-28 13:59:29 +03002286 if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2287 return -1;
2288
2289 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2290 return 0;
2291
Jouni Malinen0b5ff2a2014-02-03 10:54:21 +02002292 if (attr->ap_channel) {
2293 u16 chan = WPA_GET_BE16(attr->ap_channel);
Jouni Malinenbd3a3732012-10-28 18:02:04 +02002294 int freq = 0;
2295
2296 if (chan >= 1 && chan <= 13)
2297 freq = 2407 + 5 * chan;
2298 else if (chan == 14)
2299 freq = 2484;
2300 else if (chan >= 30)
2301 freq = 5000 + 5 * chan;
2302
2303 if (freq) {
Jouni Malinen0b5ff2a2014-02-03 10:54:21 +02002304 wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2305 chan, freq);
Jouni Malinenbd3a3732012-10-28 18:02:04 +02002306 wpa_s->after_wps = 5;
2307 wpa_s->wps_freq = freq;
2308 }
2309 }
Jouni Malinen0b5ff2a2014-02-03 10:54:21 +02002310
2311 wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2312 "based on the received credential added");
2313 wpa_s->normal_scans = 0;
2314 wpa_supplicant_reinit_autoscan(wpa_s);
Jouni Malinend7645d22012-06-28 13:59:29 +03002315 wpa_s->disconnected = 0;
2316 wpa_s->reassociate = 1;
Jithu Jance7e910b72013-10-14 20:42:27 +03002317
2318 wpa_supplicant_cancel_sched_scan(wpa_s);
Jouni Malinend7645d22012-06-28 13:59:29 +03002319 wpa_supplicant_req_scan(wpa_s, 0, 0);
2320
2321 return 0;
2322}
2323
2324
Masashi Honma6df865f2012-09-15 22:02:09 -07002325#ifdef CONFIG_WPS_ER
Jouni Malinene6ea2a42012-06-28 17:34:46 +03002326static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2327 struct wps_parse_attr *attr)
2328{
Jouni Malinene6ea2a42012-06-28 17:34:46 +03002329 return wps_registrar_add_nfc_password_token(
2330 wpa_s->wps->registrar, attr->oob_dev_password,
2331 attr->oob_dev_password_len);
2332}
Masashi Honma6df865f2012-09-15 22:02:09 -07002333#endif /* CONFIG_WPS_ER */
Jouni Malinene6ea2a42012-06-28 17:34:46 +03002334
2335
Jouni Malinend7645d22012-06-28 13:59:29 +03002336static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2337 const struct wpabuf *wps)
2338{
2339 struct wps_parse_attr attr;
2340
2341 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2342
2343 if (wps_parse_msg(wps, &attr)) {
2344 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2345 return -1;
2346 }
2347
2348 if (attr.num_cred)
2349 return wpas_wps_use_cred(wpa_s, &attr);
2350
Jouni Malinene6ea2a42012-06-28 17:34:46 +03002351#ifdef CONFIG_WPS_ER
2352 if (attr.oob_dev_password)
2353 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2354#endif /* CONFIG_WPS_ER */
2355
Jouni Malinend7645d22012-06-28 13:59:29 +03002356 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2357 return -1;
2358}
2359
2360
2361int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
Jouni Malinenb56f6c82013-12-04 05:44:56 +02002362 const struct wpabuf *data, int forced_freq)
Jouni Malinend7645d22012-06-28 13:59:29 +03002363{
2364 const struct wpabuf *wps = data;
2365 struct wpabuf *tmp = NULL;
2366 int ret;
2367
2368 if (wpabuf_len(data) < 4)
2369 return -1;
2370
2371 if (*wpabuf_head_u8(data) != 0x10) {
2372 /* Assume this contains full NDEF record */
2373 tmp = ndef_parse_wifi(data);
2374 if (tmp == NULL) {
Jouni Malinendd37a932013-04-27 18:38:41 +03002375#ifdef CONFIG_P2P
2376 tmp = ndef_parse_p2p(data);
2377 if (tmp) {
Jouni Malinenb56f6c82013-12-04 05:44:56 +02002378 ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2379 forced_freq);
Jouni Malinendd37a932013-04-27 18:38:41 +03002380 wpabuf_free(tmp);
2381 return ret;
2382 }
2383#endif /* CONFIG_P2P */
Jouni Malinend7645d22012-06-28 13:59:29 +03002384 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2385 return -1;
2386 }
2387 wps = tmp;
2388 }
2389
2390 ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2391 wpabuf_free(tmp);
2392 return ret;
2393}
2394
Jouni Malinene65552d2012-10-28 17:39:46 +02002395
Jouni Malinen41f9ffb2013-04-02 18:30:58 +03002396struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2397 int ndef)
Jouni Malinene65552d2012-10-28 17:39:46 +02002398{
Jouni Malinen41f9ffb2013-04-02 18:30:58 +03002399 struct wpabuf *ret;
2400
2401 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2402 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2403 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2404 return NULL;
2405
2406 ret = wps_build_nfc_handover_req(wpa_s->wps,
2407 wpa_s->conf->wps_nfc_dh_pubkey);
2408
2409 if (ndef && ret) {
2410 struct wpabuf *tmp;
2411 tmp = ndef_build_wifi(ret);
2412 wpabuf_free(ret);
2413 if (tmp == NULL)
2414 return NULL;
2415 ret = tmp;
2416 }
2417
2418 return ret;
Jouni Malinene65552d2012-10-28 17:39:46 +02002419}
2420
2421
Jouni Malinenf3f2ba22013-02-24 10:56:29 +02002422#ifdef CONFIG_WPS_NFC
Jouni Malinen50d1f892013-04-11 21:37:41 -07002423
Jouni Malinenc5838682013-12-31 19:37:15 +02002424static struct wpabuf *
2425wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2426 const char *uuid)
Jouni Malinene65552d2012-10-28 17:39:46 +02002427{
Jouni Malinen94d7acf2013-03-13 00:01:32 +02002428#ifdef CONFIG_WPS_ER
Jouni Malinenf3f2ba22013-02-24 10:56:29 +02002429 struct wpabuf *ret;
Jouni Malinen59307b32013-04-01 20:32:09 +03002430 u8 u[UUID_LEN], *use_uuid = NULL;
2431 u8 addr[ETH_ALEN], *use_addr = NULL;
Jouni Malinend4b4d7f2013-07-11 16:33:05 +03002432 struct wps_context *wps = wpa_s->wps;
Jouni Malinenf3f2ba22013-02-24 10:56:29 +02002433
Jouni Malinend4b4d7f2013-07-11 16:33:05 +03002434 if (wps == NULL)
Jouni Malinenf3f2ba22013-02-24 10:56:29 +02002435 return NULL;
2436
Jouni Malinen59307b32013-04-01 20:32:09 +03002437 if (uuid == NULL)
2438 return NULL;
2439 if (uuid_str2bin(uuid, u) == 0)
2440 use_uuid = u;
2441 else if (hwaddr_aton(uuid, addr) == 0)
2442 use_addr = addr;
2443 else
Jouni Malinenf3f2ba22013-02-24 10:56:29 +02002444 return NULL;
2445
Jouni Malinen50d1f892013-04-11 21:37:41 -07002446 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
Jouni Malinen50d1f892013-04-11 21:37:41 -07002447 if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2448 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2449 return NULL;
Jouni Malinend4b4d7f2013-07-11 16:33:05 +03002450 }
Jouni Malinen50d1f892013-04-11 21:37:41 -07002451
Jouni Malinend4b4d7f2013-07-11 16:33:05 +03002452 wpas_wps_nfc_clear(wps);
2453 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2454 wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2455 wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2456 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
Jouni Malinen50d1f892013-04-11 21:37:41 -07002457 wpas_wps_nfc_clear(wps);
Jouni Malinend4b4d7f2013-07-11 16:33:05 +03002458 return NULL;
Jouni Malinen50d1f892013-04-11 21:37:41 -07002459 }
2460
2461 ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2462 use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
Jouni Malinenf3f2ba22013-02-24 10:56:29 +02002463 if (ndef && ret) {
2464 struct wpabuf *tmp;
2465 tmp = ndef_build_wifi(ret);
2466 wpabuf_free(ret);
2467 if (tmp == NULL)
2468 return NULL;
2469 ret = tmp;
2470 }
2471
2472 return ret;
Jouni Malinen94d7acf2013-03-13 00:01:32 +02002473#else /* CONFIG_WPS_ER */
2474 return NULL;
2475#endif /* CONFIG_WPS_ER */
Jouni Malinenf3f2ba22013-02-24 10:56:29 +02002476}
2477#endif /* CONFIG_WPS_NFC */
2478
2479
2480struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2481 int ndef, int cr, const char *uuid)
2482{
2483 struct wpabuf *ret;
Jouni Malinen5ab9a6a2013-02-15 12:34:55 +02002484 if (!cr)
2485 return NULL;
Jouni Malinenf3f2ba22013-02-24 10:56:29 +02002486 ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2487 if (ret)
2488 return ret;
2489 return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
Jouni Malinene65552d2012-10-28 17:39:46 +02002490}
2491
2492
Jouni Malinen679f2e72014-04-06 16:42:27 +03002493static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2494 const struct wpabuf *data)
Jouni Malinene65552d2012-10-28 17:39:46 +02002495{
2496 struct wpabuf *wps;
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002497 int ret = -1;
2498 u16 wsc_len;
2499 const u8 *pos;
2500 struct wpabuf msg;
2501 struct wps_parse_attr attr;
2502 u16 dev_pw_id;
Jouni Malinen91a65012013-11-28 17:48:35 +02002503 const u8 *bssid = NULL;
2504 int freq = 0;
Jouni Malinene65552d2012-10-28 17:39:46 +02002505
2506 wps = ndef_parse_wifi(data);
2507 if (wps == NULL)
2508 return -1;
2509 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2510 "payload from NFC connection handover");
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002511 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2512 if (wpabuf_len(wps) < 2) {
2513 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2514 "Message");
2515 goto out;
2516 }
2517 pos = wpabuf_head(wps);
2518 wsc_len = WPA_GET_BE16(pos);
2519 if (wsc_len > wpabuf_len(wps) - 2) {
2520 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2521 "in Wi-Fi Handover Select Message", wsc_len);
2522 goto out;
2523 }
2524 pos += 2;
Jouni Malinene65552d2012-10-28 17:39:46 +02002525
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002526 wpa_hexdump(MSG_DEBUG,
2527 "WPS: WSC attributes in Wi-Fi Handover Select Message",
2528 pos, wsc_len);
2529 if (wsc_len < wpabuf_len(wps) - 2) {
2530 wpa_hexdump(MSG_DEBUG,
2531 "WPS: Ignore extra data after WSC attributes",
2532 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2533 }
2534
2535 wpabuf_set(&msg, pos, wsc_len);
2536 ret = wps_parse_msg(&msg, &attr);
2537 if (ret < 0) {
2538 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2539 "Wi-Fi Handover Select Message");
2540 goto out;
2541 }
2542
2543 if (attr.oob_dev_password == NULL ||
2544 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2545 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2546 "included in Wi-Fi Handover Select Message");
2547 ret = -1;
2548 goto out;
2549 }
2550
2551 if (attr.ssid == NULL) {
2552 wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2553 "Select Message");
2554 ret = -1;
2555 goto out;
2556 }
2557
2558 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2559
Jouni Malinen91a65012013-11-28 17:48:35 +02002560 if (attr.mac_addr) {
2561 bssid = attr.mac_addr;
2562 wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2563 MAC2STR(bssid));
2564 }
2565
2566 if (attr.rf_bands)
2567 wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2568
2569 if (attr.ap_channel) {
2570 u16 chan = WPA_GET_BE16(attr.ap_channel);
2571
2572 wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2573
2574 if (chan >= 1 && chan <= 13 &&
2575 (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2576 freq = 2407 + 5 * chan;
2577 else if (chan == 14 &&
2578 (attr.rf_bands == NULL ||
2579 *attr.rf_bands & WPS_RF_24GHZ))
2580 freq = 2484;
2581 else if (chan >= 30 &&
2582 (attr.rf_bands == NULL ||
2583 *attr.rf_bands & WPS_RF_50GHZ))
2584 freq = 5000 + 5 * chan;
2585
2586 if (freq) {
2587 wpa_printf(MSG_DEBUG,
2588 "WPS: AP indicated channel %u -> %u MHz",
2589 chan, freq);
2590 }
2591 }
2592
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002593 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2594 attr.oob_dev_password, attr.oob_dev_password_len);
2595 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2596 WPS_OOB_PUBKEY_HASH_LEN);
2597 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2598 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2599 "%u in Wi-Fi Handover Select Message", dev_pw_id);
2600 ret = -1;
2601 goto out;
2602 }
2603 wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2604 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2605
Jouni Malinen91a65012013-11-28 17:48:35 +02002606 ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002607 attr.oob_dev_password,
Jouni Malinen91a65012013-11-28 17:48:35 +02002608 attr.ssid, attr.ssid_len, freq);
Jouni Malinenfa4c2982013-04-02 18:30:58 +03002609
2610out:
2611 wpabuf_free(wps);
Jouni Malinene65552d2012-10-28 17:39:46 +02002612 return ret;
2613}
2614
Jouni Malinene4758822013-02-11 18:43:46 +02002615
2616int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2617 const struct wpabuf *req,
2618 const struct wpabuf *sel)
2619{
2620 wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2621 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2622 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2623 return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2624}
2625
Jouni Malinen50d1f892013-04-11 21:37:41 -07002626
2627int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2628 const struct wpabuf *req,
2629 const struct wpabuf *sel)
2630{
2631 struct wpabuf *wps;
2632 int ret = -1;
2633 u16 wsc_len;
2634 const u8 *pos;
2635 struct wpabuf msg;
2636 struct wps_parse_attr attr;
2637 u16 dev_pw_id;
2638
2639 /*
2640 * Enrollee/station is always initiator of the NFC connection handover,
2641 * so use the request message here to find Enrollee public key hash.
2642 */
2643 wps = ndef_parse_wifi(req);
2644 if (wps == NULL)
2645 return -1;
2646 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2647 "payload from NFC connection handover");
2648 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2649 if (wpabuf_len(wps) < 2) {
2650 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2651 "Message");
2652 goto out;
2653 }
2654 pos = wpabuf_head(wps);
2655 wsc_len = WPA_GET_BE16(pos);
2656 if (wsc_len > wpabuf_len(wps) - 2) {
2657 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2658 "in rt Wi-Fi Handover Request Message", wsc_len);
2659 goto out;
2660 }
2661 pos += 2;
2662
2663 wpa_hexdump(MSG_DEBUG,
2664 "WPS: WSC attributes in Wi-Fi Handover Request Message",
2665 pos, wsc_len);
2666 if (wsc_len < wpabuf_len(wps) - 2) {
2667 wpa_hexdump(MSG_DEBUG,
2668 "WPS: Ignore extra data after WSC attributes",
2669 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2670 }
2671
2672 wpabuf_set(&msg, pos, wsc_len);
2673 ret = wps_parse_msg(&msg, &attr);
2674 if (ret < 0) {
2675 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2676 "Wi-Fi Handover Request Message");
2677 goto out;
2678 }
2679
2680 if (attr.oob_dev_password == NULL ||
2681 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2682 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2683 "included in Wi-Fi Handover Request Message");
2684 ret = -1;
2685 goto out;
2686 }
2687
2688 if (attr.uuid_e == NULL) {
2689 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2690 "Handover Request Message");
2691 ret = -1;
2692 goto out;
2693 }
2694
2695 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2696
2697 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2698 attr.oob_dev_password, attr.oob_dev_password_len);
2699 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2700 WPS_OOB_PUBKEY_HASH_LEN);
2701 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2702 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2703 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2704 ret = -1;
2705 goto out;
2706 }
2707 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2708 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2709
2710 ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2711 attr.oob_dev_password,
2712 DEV_PW_NFC_CONNECTION_HANDOVER,
2713 NULL, 0, 1);
2714
2715out:
2716 wpabuf_free(wps);
2717 return ret;
2718}
2719
Jouni Malinen3f2c8ba2012-06-27 18:56:41 +03002720#endif /* CONFIG_WPS_NFC */
Jouni Malinenf9f05262012-08-27 13:48:11 +03002721
2722
Jouni Malinenf9f05262012-08-27 13:48:11 +03002723static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2724{
2725 size_t i;
Johannes Berg51bffab2013-12-16 21:08:43 +01002726 struct os_reltime now;
Jouni Malinenf9f05262012-08-27 13:48:11 +03002727
2728 if (wpa_debug_level > MSG_DEBUG)
2729 return;
2730
2731 if (wpa_s->wps_ap == NULL)
2732 return;
2733
Johannes Berg51bffab2013-12-16 21:08:43 +01002734 os_get_reltime(&now);
Jouni Malinenf9f05262012-08-27 13:48:11 +03002735
2736 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2737 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2738 struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
2739
2740 wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2741 "tries=%d last_attempt=%d sec ago blacklist=%d",
2742 (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2743 ap->last_attempt.sec > 0 ?
2744 (int) now.sec - (int) ap->last_attempt.sec : -1,
2745 e ? e->count : 0);
2746 }
2747}
2748
2749
2750static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2751 const u8 *bssid)
2752{
2753 size_t i;
2754
2755 if (wpa_s->wps_ap == NULL)
2756 return NULL;
2757
2758 for (i = 0; i < wpa_s->num_wps_ap; i++) {
2759 struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2760 if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2761 return ap;
2762 }
2763
2764 return NULL;
2765}
2766
2767
2768static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2769 struct wpa_scan_res *res)
2770{
2771 struct wpabuf *wps;
2772 enum wps_ap_info_type type;
2773 struct wps_ap_info *ap;
2774 int r;
2775
2776 if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2777 return;
2778
2779 wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2780 if (wps == NULL)
2781 return;
2782
2783 r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2784 if (r == 2)
2785 type = WPS_AP_SEL_REG_OUR;
2786 else if (r == 1)
2787 type = WPS_AP_SEL_REG;
2788 else
2789 type = WPS_AP_NOT_SEL_REG;
2790
2791 wpabuf_free(wps);
2792
2793 ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2794 if (ap) {
2795 if (ap->type != type) {
2796 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2797 " changed type %d -> %d",
2798 MAC2STR(res->bssid), ap->type, type);
2799 ap->type = type;
Jouni Malinena4620362012-08-27 13:51:35 +03002800 if (type != WPS_AP_NOT_SEL_REG)
2801 wpa_blacklist_del(wpa_s, ap->bssid);
Jouni Malinenf9f05262012-08-27 13:48:11 +03002802 }
2803 return;
2804 }
2805
2806 ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2807 sizeof(struct wps_ap_info));
2808 if (ap == NULL)
2809 return;
2810
2811 wpa_s->wps_ap = ap;
2812 ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2813 wpa_s->num_wps_ap++;
2814
2815 os_memset(ap, 0, sizeof(*ap));
2816 os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2817 ap->type = type;
2818 wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2819 MAC2STR(ap->bssid), ap->type);
2820}
2821
2822
2823void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2824 struct wpa_scan_results *scan_res)
2825{
2826 size_t i;
2827
2828 for (i = 0; i < scan_res->num; i++)
2829 wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2830
2831 wpas_wps_dump_ap_info(wpa_s);
2832}
2833
2834
2835void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2836{
2837 struct wps_ap_info *ap;
Jouni Malinen72559832013-10-20 21:34:39 +03002838
2839 wpa_s->after_wps = 0;
2840
Jouni Malinenf9f05262012-08-27 13:48:11 +03002841 if (!wpa_s->wps_ap_iter)
2842 return;
2843 ap = wpas_wps_get_ap_info(wpa_s, bssid);
2844 if (ap == NULL)
2845 return;
2846 ap->tries++;
Johannes Berg51bffab2013-12-16 21:08:43 +01002847 os_get_reltime(&ap->last_attempt);
Jouni Malinenf9f05262012-08-27 13:48:11 +03002848}