blob: 175b9fc09d2b05f47edb25dad1c4904b3beb13dd [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / WPS integration
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003 * Copyright (c) 2008-2016, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/uuid.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "common/wpa_ctrl.h"
15#include "common/ieee802_11_defs.h"
16#include "common/ieee802_11_common.h"
17#include "eapol_auth/eapol_auth_sm.h"
18#include "eapol_auth/eapol_auth_sm_i.h"
19#include "wps/wps.h"
20#include "wps/wps_defs.h"
21#include "wps/wps_dev_attr.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070022#include "wps/wps_attr_parse.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "hostapd.h"
24#include "ap_config.h"
25#include "ap_drv_ops.h"
26#include "beacon.h"
27#include "sta_info.h"
28#include "wps_hostapd.h"
29
30
31#ifdef CONFIG_WPS_UPNP
32#include "wps/wps_upnp.h"
33static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
34 struct wps_context *wps);
35static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
36#endif /* CONFIG_WPS_UPNP */
37
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
39 const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -070040 const u8 *ie, size_t ie_len,
41 int ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -070043static void hostapd_wps_nfc_clear(struct wps_context *wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
45
46struct wps_for_each_data {
47 int (*func)(struct hostapd_data *h, void *ctx);
48 void *ctx;
Dmitry Shmidt444d5672013-04-01 13:08:44 -070049 struct hostapd_data *calling_hapd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050};
51
52
53static int wps_for_each(struct hostapd_iface *iface, void *ctx)
54{
55 struct wps_for_each_data *data = ctx;
56 size_t j;
57
58 if (iface == NULL)
59 return 0;
60 for (j = 0; j < iface->num_bss; j++) {
61 struct hostapd_data *hapd = iface->bss[j];
Dmitry Shmidt444d5672013-04-01 13:08:44 -070062 int ret;
63
64 if (hapd != data->calling_hapd &&
65 (hapd->conf->wps_independent ||
66 data->calling_hapd->conf->wps_independent))
67 continue;
68
69 ret = data->func(hapd, data->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070 if (ret)
71 return ret;
72 }
73
74 return 0;
75}
76
77
78static int hostapd_wps_for_each(struct hostapd_data *hapd,
79 int (*func)(struct hostapd_data *h, void *ctx),
80 void *ctx)
81{
82 struct hostapd_iface *iface = hapd->iface;
83 struct wps_for_each_data data;
84 data.func = func;
85 data.ctx = ctx;
Dmitry Shmidt444d5672013-04-01 13:08:44 -070086 data.calling_hapd = hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087 if (iface->interfaces == NULL ||
88 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089 return wps_for_each(iface, &data);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070090 return iface->interfaces->for_each_interface(iface->interfaces,
91 wps_for_each, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092}
93
94
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070095static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
96 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 size_t psk_len)
98{
99 struct hostapd_data *hapd = ctx;
100 struct hostapd_wpa_psk *p;
101 struct hostapd_ssid *ssid = &hapd->conf->ssid;
102
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700103 if (is_zero_ether_addr(p2p_dev_addr)) {
104 wpa_printf(MSG_DEBUG,
105 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
106 MAC2STR(mac_addr));
107 } else {
108 wpa_printf(MSG_DEBUG,
109 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
110 " P2P Device Addr " MACSTR,
111 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
114
115 if (psk_len != PMK_LEN) {
116 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
117 (unsigned long) psk_len);
118 return -1;
119 }
120
121 /* Add the new PSK to runtime PSK list */
122 p = os_zalloc(sizeof(*p));
123 if (p == NULL)
124 return -1;
125 os_memcpy(p->addr, mac_addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700126 os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127 os_memcpy(p->psk, psk, PMK_LEN);
128
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700129 if (hapd->new_psk_cb) {
130 hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
131 psk, psk_len);
132 }
133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 p->next = ssid->wpa_psk;
135 ssid->wpa_psk = p;
136
137 if (ssid->wpa_psk_file) {
138 FILE *f;
139 char hex[PMK_LEN * 2 + 1];
140 /* Add the new PSK to PSK list file */
141 f = fopen(ssid->wpa_psk_file, "a");
142 if (f == NULL) {
143 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
144 "'%s'", ssid->wpa_psk_file);
145 return -1;
146 }
147
148 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
149 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
150 fclose(f);
151 }
152
153 return 0;
154}
155
156
157static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
158 struct wpabuf *probe_resp_ie)
159{
160 struct hostapd_data *hapd = ctx;
161 wpabuf_free(hapd->wps_beacon_ie);
162 hapd->wps_beacon_ie = beacon_ie;
163 wpabuf_free(hapd->wps_probe_resp_ie);
164 hapd->wps_probe_resp_ie = probe_resp_ie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165 if (hapd->beacon_set_done)
166 ieee802_11_set_beacon(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167 return hostapd_set_ap_wps_ie(hapd);
168}
169
170
171static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
172 const struct wps_device_data *dev)
173{
174 struct hostapd_data *hapd = ctx;
175 char uuid[40], txt[400];
176 int len;
177 char devtype[WPS_DEV_TYPE_BUFSIZE];
178 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
179 return;
180 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
181 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
182 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
183 uuid, MAC2STR(dev->mac_addr), dev->device_name,
184 dev->manufacturer, dev->model_name,
185 dev->model_number, dev->serial_number,
186 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
187 sizeof(devtype)));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800188 if (!os_snprintf_error(sizeof(txt), len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
190
191 if (hapd->conf->wps_pin_requests) {
192 FILE *f;
193 struct os_time t;
194 f = fopen(hapd->conf->wps_pin_requests, "a");
195 if (f == NULL)
196 return;
197 os_get_time(&t);
198 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
199 "\t%s\n",
200 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
201 dev->manufacturer, dev->model_name, dev->model_number,
202 dev->serial_number,
203 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
204 sizeof(devtype)));
205 fclose(f);
206 }
207}
208
209
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800210struct wps_stop_reg_data {
211 struct hostapd_data *current_hapd;
212 const u8 *uuid_e;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700213 const u8 *dev_pw;
214 size_t dev_pw_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800215};
216
217static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
218{
219 struct wps_stop_reg_data *data = ctx;
220 if (hapd != data->current_hapd && hapd->wps != NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700221 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
222 data->dev_pw, data->dev_pw_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800223 return 0;
224}
225
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700228 const u8 *uuid_e, const u8 *dev_pw,
229 size_t dev_pw_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230{
231 struct hostapd_data *hapd = ctx;
232 char uuid[40];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233 struct wps_stop_reg_data data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
235 return;
236 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
237 MAC2STR(mac_addr), uuid);
238 if (hapd->wps_reg_success_cb)
239 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
240 mac_addr, uuid_e);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241 data.current_hapd = hapd;
242 data.uuid_e = uuid_e;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700243 data.dev_pw = dev_pw;
244 data.dev_pw_len = dev_pw_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800245 hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246}
247
248
249static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
250 const u8 *uuid_e,
251 const u8 *pri_dev_type,
252 u16 config_methods,
253 u16 dev_password_id, u8 request_type,
254 const char *dev_name)
255{
256 struct hostapd_data *hapd = ctx;
257 char uuid[40];
258 char devtype[WPS_DEV_TYPE_BUFSIZE];
259 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
260 return;
261 if (dev_name == NULL)
262 dev_name = "";
263 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
264 " %s %s 0x%x %u %u [%s]",
265 MAC2STR(addr), uuid,
266 wps_dev_type_bin2str(pri_dev_type, devtype,
267 sizeof(devtype)),
268 config_methods, dev_password_id, request_type, dev_name);
269}
270
271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272static void wps_reload_config(void *eloop_data, void *user_ctx)
273{
274 struct hostapd_iface *iface = eloop_data;
275
276 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700277 if (iface->interfaces == NULL ||
278 iface->interfaces->reload_config(iface) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
280 "configuration");
281 }
282}
283
284
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800285void hostapd_wps_eap_completed(struct hostapd_data *hapd)
286{
287 /*
288 * Reduce race condition of the station trying to reconnect immediately
289 * after AP reconfiguration through WPS by rescheduling the reload
290 * timeout to happen after EAP completion rather than the originally
291 * scheduled 100 ms after new configuration became known.
292 */
293 if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
294 1)
295 wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
296}
297
298
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800299static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
300 size_t attr_len)
301{
302 size_t blen = attr_len * 2 + 1;
303 char *buf = os_malloc(blen);
304 if (buf) {
305 wpa_snprintf_hex(buf, blen, attr, attr_len);
306 wpa_msg(hapd->msg_ctx, MSG_INFO,
307 WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
308 os_free(buf);
309 }
310}
311
312
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700313static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
314 const struct wps_credential *cred)
315{
316 struct hostapd_bss_config *bss = hapd->conf;
317
318 wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
319
320 bss->wps_state = 2;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700321 if (cred->ssid_len <= SSID_MAX_LEN) {
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700322 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
323 bss->ssid.ssid_len = cred->ssid_len;
324 bss->ssid.ssid_set = 1;
325 }
326
327 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
328 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
329 bss->wpa = 3;
330 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
331 bss->wpa = 2;
332 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
333 bss->wpa = 1;
334 else
335 bss->wpa = 0;
336
337 if (bss->wpa) {
338 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
339 bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
340 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
341 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
342
343 bss->wpa_pairwise = 0;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700344 if (cred->encr_type & WPS_ENCR_AES) {
345 if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
346 bss->wpa_pairwise |= WPA_CIPHER_GCMP;
347 else
348 bss->wpa_pairwise |= WPA_CIPHER_CCMP;
349 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700350 if (cred->encr_type & WPS_ENCR_TKIP)
351 bss->wpa_pairwise |= WPA_CIPHER_TKIP;
352 bss->rsn_pairwise = bss->wpa_pairwise;
353 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
354 bss->wpa_pairwise,
355 bss->rsn_pairwise);
356
Hai Shalom021b0b52019-04-10 11:17:58 -0700357 if (hapd->conf->wps_cred_add_sae &&
358 (cred->auth_type & WPS_AUTH_WPA2PSK) &&
359 cred->key_len != 2 * PMK_LEN) {
360 bss->wpa_key_mgmt |= WPA_KEY_MGMT_SAE;
Hai Shalom021b0b52019-04-10 11:17:58 -0700361 if (bss->ieee80211w == NO_MGMT_FRAME_PROTECTION)
362 bss->ieee80211w =
363 MGMT_FRAME_PROTECTION_OPTIONAL;
364 bss->sae_require_mfp = 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700365 }
366
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700367 if (cred->key_len >= 8 && cred->key_len < 64) {
368 os_free(bss->ssid.wpa_passphrase);
369 bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
370 if (bss->ssid.wpa_passphrase)
371 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
372 cred->key_len);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800373 hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700374 } else if (cred->key_len == 64) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800375 hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700376 bss->ssid.wpa_psk =
377 os_zalloc(sizeof(struct hostapd_wpa_psk));
378 if (bss->ssid.wpa_psk &&
379 hexstr2bin((const char *) cred->key,
380 bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
381 bss->ssid.wpa_psk->group = 1;
382 os_free(bss->ssid.wpa_passphrase);
383 bss->ssid.wpa_passphrase = NULL;
384 }
385 }
386 bss->auth_algs = 1;
387 } else {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700388 /*
389 * WPS 2.0 does not allow WEP to be configured, so no need to
390 * process that option here either.
391 */
392 bss->auth_algs = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700393 }
394
395 /* Schedule configuration reload after short period of time to allow
396 * EAP-WSC to be finished.
397 */
398 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
399 NULL);
400
401 return 0;
402}
403
404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
406{
407 const struct wps_credential *cred = ctx;
408 FILE *oconf, *nconf;
409 size_t len, i;
410 char *tmp_fname;
411 char buf[1024];
412 int multi_bss;
413 int wpa;
Hai Shalom021b0b52019-04-10 11:17:58 -0700414 int pmf_changed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415
416 if (hapd->wps == NULL)
417 return 0;
418
419 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
420 cred->cred_attr, cred->cred_attr_len);
421
422 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
423 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
424 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
425 cred->auth_type);
426 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
427 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
428 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
429 cred->key, cred->key_len);
430 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
431 MAC2STR(cred->mac_addr));
432
433 if ((hapd->conf->wps_cred_processing == 1 ||
434 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800435 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
436 } else if (hapd->conf->wps_cred_processing == 1 ||
437 hapd->conf->wps_cred_processing == 2) {
438 struct wpabuf *attr;
439 attr = wpabuf_alloc(200);
440 if (attr && wps_build_credential_wrap(attr, cred) == 0)
441 hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
442 wpabuf_len(attr));
443 wpabuf_free(attr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 } else
445 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
446
447 if (hapd->conf->wps_cred_processing == 1)
448 return 0;
449
450 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
451 hapd->wps->ssid_len = cred->ssid_len;
452 hapd->wps->encr_types = cred->encr_type;
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800453 hapd->wps->encr_types_rsn = cred->encr_type;
454 hapd->wps->encr_types_wpa = cred->encr_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 hapd->wps->auth_types = cred->auth_type;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800456 hapd->wps->ap_encr_type = cred->encr_type;
457 hapd->wps->ap_auth_type = cred->auth_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 if (cred->key_len == 0) {
459 os_free(hapd->wps->network_key);
460 hapd->wps->network_key = NULL;
461 hapd->wps->network_key_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800462 } else if ((cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) &&
463 (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN)) {
464 wpa_printf(MSG_INFO, "WPS: Invalid key length %lu for WPA/WPA2",
465 (unsigned long) cred->key_len);
466 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 } else {
468 if (hapd->wps->network_key == NULL ||
469 hapd->wps->network_key_len < cred->key_len) {
470 hapd->wps->network_key_len = 0;
471 os_free(hapd->wps->network_key);
472 hapd->wps->network_key = os_malloc(cred->key_len);
473 if (hapd->wps->network_key == NULL)
474 return -1;
475 }
476 hapd->wps->network_key_len = cred->key_len;
477 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
478 }
479 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
480
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700481 if (hapd->iface->config_fname == NULL)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700482 return hapd_wps_reconfig_in_memory(hapd, cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 len = os_strlen(hapd->iface->config_fname) + 5;
484 tmp_fname = os_malloc(len);
485 if (tmp_fname == NULL)
486 return -1;
487 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
488
489 oconf = fopen(hapd->iface->config_fname, "r");
490 if (oconf == NULL) {
491 wpa_printf(MSG_WARNING, "WPS: Could not open current "
492 "configuration file");
493 os_free(tmp_fname);
494 return -1;
495 }
496
497 nconf = fopen(tmp_fname, "w");
498 if (nconf == NULL) {
499 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
500 "configuration file");
501 os_free(tmp_fname);
502 fclose(oconf);
503 return -1;
504 }
505
506 fprintf(nconf, "# WPS configuration - START\n");
507
508 fprintf(nconf, "wps_state=2\n");
509
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700510 if (is_hex(cred->ssid, cred->ssid_len)) {
511 fprintf(nconf, "ssid2=");
512 for (i = 0; i < cred->ssid_len; i++)
513 fprintf(nconf, "%02x", cred->ssid[i]);
514 fprintf(nconf, "\n");
515 } else {
516 fprintf(nconf, "ssid=");
517 for (i = 0; i < cred->ssid_len; i++)
518 fputc(cred->ssid[i], nconf);
519 fprintf(nconf, "\n");
520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521
522 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
523 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
524 wpa = 3;
525 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
526 wpa = 2;
527 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
528 wpa = 1;
529 else
530 wpa = 0;
531
532 if (wpa) {
533 char *prefix;
Hai Shalom021b0b52019-04-10 11:17:58 -0700534 int sae = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 fprintf(nconf, "wpa=%d\n", wpa);
537
538 fprintf(nconf, "wpa_key_mgmt=");
539 prefix = "";
540 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
541 fprintf(nconf, "WPA-EAP");
542 prefix = " ";
543 }
Hai Shalom021b0b52019-04-10 11:17:58 -0700544 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 fprintf(nconf, "%sWPA-PSK", prefix);
Hai Shalom021b0b52019-04-10 11:17:58 -0700546 prefix = " ";
547 }
548 if (hapd->conf->wps_cred_add_sae &&
549 (cred->auth_type & WPS_AUTH_WPA2PSK) &&
550 cred->key_len != 2 * PMK_LEN) {
551 fprintf(nconf, "%sSAE", prefix);
Hai Shalom021b0b52019-04-10 11:17:58 -0700552 sae = 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 fprintf(nconf, "\n");
555
Hai Shalom021b0b52019-04-10 11:17:58 -0700556 if (sae && hapd->conf->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
557 fprintf(nconf, "ieee80211w=%d\n",
558 MGMT_FRAME_PROTECTION_OPTIONAL);
559 pmf_changed = 1;
560 }
561 if (sae)
562 fprintf(nconf, "sae_require_mfp=1\n");
Hai Shalom021b0b52019-04-10 11:17:58 -0700563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 fprintf(nconf, "wpa_pairwise=");
565 prefix = "";
566 if (cred->encr_type & WPS_ENCR_AES) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700567 if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
568 fprintf(nconf, "GCMP");
569 else
570 fprintf(nconf, "CCMP");
571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 prefix = " ";
573 }
574 if (cred->encr_type & WPS_ENCR_TKIP) {
575 fprintf(nconf, "%sTKIP", prefix);
576 }
577 fprintf(nconf, "\n");
578
579 if (cred->key_len >= 8 && cred->key_len < 64) {
580 fprintf(nconf, "wpa_passphrase=");
581 for (i = 0; i < cred->key_len; i++)
582 fputc(cred->key[i], nconf);
583 fprintf(nconf, "\n");
584 } else if (cred->key_len == 64) {
585 fprintf(nconf, "wpa_psk=");
586 for (i = 0; i < cred->key_len; i++)
587 fputc(cred->key[i], nconf);
588 fprintf(nconf, "\n");
589 } else {
590 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
591 "for WPA/WPA2",
592 (unsigned long) cred->key_len);
593 }
594
595 fprintf(nconf, "auth_algs=1\n");
596 } else {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700597 /*
598 * WPS 2.0 does not allow WEP to be configured, so no need to
599 * process that option here either.
600 */
601 fprintf(nconf, "auth_algs=1\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602 }
603
604 fprintf(nconf, "# WPS configuration - END\n");
605
606 multi_bss = 0;
607 while (fgets(buf, sizeof(buf), oconf)) {
608 if (os_strncmp(buf, "bss=", 4) == 0)
609 multi_bss = 1;
610 if (!multi_bss &&
611 (str_starts(buf, "ssid=") ||
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700612 str_starts(buf, "ssid2=") ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 str_starts(buf, "auth_algs=") ||
614 str_starts(buf, "wep_default_key=") ||
615 str_starts(buf, "wep_key") ||
616 str_starts(buf, "wps_state=") ||
Hai Shalom021b0b52019-04-10 11:17:58 -0700617 (pmf_changed && str_starts(buf, "ieee80211w=")) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 str_starts(buf, "wpa=") ||
619 str_starts(buf, "wpa_psk=") ||
620 str_starts(buf, "wpa_pairwise=") ||
621 str_starts(buf, "rsn_pairwise=") ||
622 str_starts(buf, "wpa_key_mgmt=") ||
623 str_starts(buf, "wpa_passphrase="))) {
624 fprintf(nconf, "#WPS# %s", buf);
625 } else
626 fprintf(nconf, "%s", buf);
627 }
628
629 fclose(nconf);
630 fclose(oconf);
631
632 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
633 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
634 "configuration file: %s", strerror(errno));
635 os_free(tmp_fname);
636 return -1;
637 }
638
639 os_free(tmp_fname);
640
641 /* Schedule configuration reload after short period of time to allow
642 * EAP-WSC to be finished.
643 */
644 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
645 NULL);
646
647 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
648
649 return 0;
650}
651
652
653static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
654{
655 struct hostapd_data *hapd = ctx;
656 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
657}
658
659
660static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
661{
662 struct hostapd_data *hapd = eloop_data;
663
664 if (hapd->conf->ap_setup_locked)
665 return;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800666 if (hapd->ap_pin_failures_consecutive >= 10)
667 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668
669 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
670 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
671 hapd->wps->ap_setup_locked = 0;
672 wps_registrar_update_ie(hapd->wps->registrar);
673}
674
675
676static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
677{
678 struct wps_event_pwd_auth_fail *data = ctx;
679
680 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
681 return 0;
682
683 /*
684 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
685 * for some time if this happens multiple times to slow down brute
686 * force attacks.
687 */
688 hapd->ap_pin_failures++;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800689 hapd->ap_pin_failures_consecutive++;
690 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
691 "(%u consecutive)",
692 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 if (hapd->ap_pin_failures < 3)
694 return 0;
695
696 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
697 hapd->wps->ap_setup_locked = 1;
698
699 wps_registrar_update_ie(hapd->wps->registrar);
700
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800701 if (!hapd->conf->ap_setup_locked &&
702 hapd->ap_pin_failures_consecutive >= 10) {
703 /*
704 * In indefinite lockdown - disable automatic AP PIN
705 * reenablement.
706 */
707 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
708 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
709 } else if (!hapd->conf->ap_setup_locked) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 if (hapd->ap_pin_lockout_time == 0)
711 hapd->ap_pin_lockout_time = 60;
712 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
713 (hapd->ap_pin_failures % 3) == 0)
714 hapd->ap_pin_lockout_time *= 2;
715
716 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
717 hapd->ap_pin_lockout_time);
718 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
719 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
720 hostapd_wps_reenable_ap_pin, hapd,
721 NULL);
722 }
723
724 return 0;
725}
726
727
728static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
729 struct wps_event_pwd_auth_fail *data)
730{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700731 /* Update WPS Status - Authentication Failure */
732 wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
733 hapd->wps_stats.status = WPS_STATUS_FAILURE;
734 hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
735 os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
738}
739
740
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800741static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
742{
743 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
744 return 0;
745
746 if (hapd->ap_pin_failures_consecutive == 0)
747 return 0;
748
749 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
750 "- total validation failures %u (%u consecutive)",
751 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
752 hapd->ap_pin_failures_consecutive = 0;
753
754 return 0;
755}
756
757
758static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
759{
760 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
761}
762
763
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700764static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
765{
766 /* Update WPS Status - PBC Overlap */
767 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
768}
769
770
771static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
772{
773 /* Update WPS PBC Status:PBC Timeout */
774 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
775}
776
777
778static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
779{
780 /* Update WPS PBC status - Active */
781 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
782}
783
784
785static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
786{
787 /* Update WPS PBC status - Active */
788 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
789}
790
791
792static void hostapd_wps_event_success(struct hostapd_data *hapd,
793 struct wps_event_success *success)
794{
795 /* Update WPS status - Success */
796 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
797 hapd->wps_stats.status = WPS_STATUS_SUCCESS;
798 os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
799}
800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
802static void hostapd_wps_event_fail(struct hostapd_data *hapd,
803 struct wps_event_fail *fail)
804{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700805 /* Update WPS status - Failure */
806 hapd->wps_stats.status = WPS_STATUS_FAILURE;
807 os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
808
809 hapd->wps_stats.failure_reason = fail->error_indication;
810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811 if (fail->error_indication > 0 &&
812 fail->error_indication < NUM_WPS_EI_VALUES) {
813 wpa_msg(hapd->msg_ctx, MSG_INFO,
814 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
815 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700816 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 } else {
818 wpa_msg(hapd->msg_ctx, MSG_INFO,
819 WPS_EVENT_FAIL "msg=%d config_error=%d",
820 fail->msg, fail->config_error);
821 }
822}
823
824
825static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
826 union wps_event_data *data)
827{
828 struct hostapd_data *hapd = ctx;
829
830 switch (event) {
831 case WPS_EV_M2D:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800832 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 break;
834 case WPS_EV_FAIL:
835 hostapd_wps_event_fail(hapd, &data->fail);
836 break;
837 case WPS_EV_SUCCESS:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700838 hostapd_wps_event_success(hapd, &data->success);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800839 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 break;
841 case WPS_EV_PWD_AUTH_FAIL:
842 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
843 break;
844 case WPS_EV_PBC_OVERLAP:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700845 hostapd_wps_event_pbc_overlap(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 break;
848 case WPS_EV_PBC_TIMEOUT:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700849 hostapd_wps_event_pbc_timeout(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800850 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700852 case WPS_EV_PBC_ACTIVE:
853 hostapd_wps_event_pbc_active(hapd);
854 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
855 break;
856 case WPS_EV_PBC_DISABLE:
857 hostapd_wps_event_pbc_disable(hapd);
858 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
859 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 case WPS_EV_ER_AP_ADD:
861 break;
862 case WPS_EV_ER_AP_REMOVE:
863 break;
864 case WPS_EV_ER_ENROLLEE_ADD:
865 break;
866 case WPS_EV_ER_ENROLLEE_REMOVE:
867 break;
868 case WPS_EV_ER_AP_SETTINGS:
869 break;
870 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
871 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800872 case WPS_EV_AP_PIN_SUCCESS:
873 hostapd_wps_ap_pin_success(hapd);
874 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 }
876 if (hapd->wps_event_cb)
877 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
878}
879
880
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700881static int hostapd_wps_rf_band_cb(void *ctx)
882{
883 struct hostapd_data *hapd = ctx;
884
885 return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700886 WPS_RF_50GHZ :
887 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
888 WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700889}
890
891
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700892static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893{
894 wpabuf_free(hapd->wps_beacon_ie);
895 hapd->wps_beacon_ie = NULL;
896
897 wpabuf_free(hapd->wps_probe_resp_ie);
898 hapd->wps_probe_resp_ie = NULL;
899
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700900 if (deinit_only) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800901 if (hapd->drv_priv)
902 hostapd_reset_ap_wps_ie(hapd);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700903 return;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700904 }
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 hostapd_set_ap_wps_ie(hapd);
907}
908
909
910static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
911{
912 const u8 **uuid = ctx;
913 size_t j;
914
915 if (iface == NULL)
916 return 0;
917 for (j = 0; j < iface->num_bss; j++) {
918 struct hostapd_data *hapd = iface->bss[j];
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700919 if (hapd->wps && !hapd->conf->wps_independent &&
920 !is_nil_uuid(hapd->wps->uuid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 *uuid = hapd->wps->uuid;
922 return 1;
923 }
924 }
925
926 return 0;
927}
928
929
930static const u8 * get_own_uuid(struct hostapd_iface *iface)
931{
932 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700933 if (iface->interfaces == NULL ||
934 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 return NULL;
936 uuid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700937 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
938 &uuid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 return uuid;
940}
941
942
943static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
944{
945 int *count= ctx;
946 (*count)++;
947 return 0;
948}
949
950
951static int interface_count(struct hostapd_iface *iface)
952{
953 int count = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700954 if (iface->interfaces == NULL ||
955 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700957 iface->interfaces->for_each_interface(iface->interfaces,
958 count_interface_cb, &count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 return count;
960}
961
962
963static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
964 struct wps_context *wps)
965{
966 int i;
967
968 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
969 wpabuf_free(wps->dev.vendor_ext[i]);
970 wps->dev.vendor_ext[i] = NULL;
971
972 if (hapd->conf->wps_vendor_ext[i] == NULL)
973 continue;
974
975 wps->dev.vendor_ext[i] =
976 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
977 if (wps->dev.vendor_ext[i] == NULL) {
978 while (--i >= 0)
979 wpabuf_free(wps->dev.vendor_ext[i]);
980 return -1;
981 }
982 }
983
984 return 0;
985}
986
987
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700988static void hostapd_free_wps(struct wps_context *wps)
989{
990 int i;
991
992 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
993 wpabuf_free(wps->dev.vendor_ext[i]);
994 wps_device_data_free(&wps->dev);
995 os_free(wps->network_key);
996 hostapd_wps_nfc_clear(wps);
997 wpabuf_free(wps->dh_pubkey);
998 wpabuf_free(wps->dh_privkey);
999 os_free(wps);
1000}
1001
1002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003int hostapd_init_wps(struct hostapd_data *hapd,
1004 struct hostapd_bss_config *conf)
1005{
1006 struct wps_context *wps;
1007 struct wps_registrar_config cfg;
Hai Shalom021b0b52019-04-10 11:17:58 -07001008 u8 *multi_ap_netw_key = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009
1010 if (conf->wps_state == 0) {
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001011 hostapd_wps_clear_ies(hapd, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012 return 0;
1013 }
1014
1015 wps = os_zalloc(sizeof(*wps));
1016 if (wps == NULL)
1017 return -1;
1018
1019 wps->cred_cb = hostapd_wps_cred_cb;
1020 wps->event_cb = hostapd_wps_event_cb;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001021 wps->rf_band_cb = hostapd_wps_rf_band_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 wps->cb_ctx = hapd;
1023
1024 os_memset(&cfg, 0, sizeof(cfg));
1025 wps->wps_state = hapd->conf->wps_state;
1026 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
1027 if (is_nil_uuid(hapd->conf->uuid)) {
1028 const u8 *uuid;
1029 uuid = get_own_uuid(hapd->iface);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001030 if (uuid && !conf->wps_independent) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 os_memcpy(wps->uuid, uuid, UUID_LEN);
1032 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
1033 "interface", wps->uuid, UUID_LEN);
1034 } else {
1035 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
1036 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1037 "address", wps->uuid, UUID_LEN);
1038 }
1039 } else {
1040 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
1041 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
1042 wps->uuid, UUID_LEN);
1043 }
1044 wps->ssid_len = hapd->conf->ssid.ssid_len;
1045 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1046 wps->ap = 1;
1047 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1048 wps->dev.device_name = hapd->conf->device_name ?
1049 os_strdup(hapd->conf->device_name) : NULL;
1050 wps->dev.manufacturer = hapd->conf->manufacturer ?
1051 os_strdup(hapd->conf->manufacturer) : NULL;
1052 wps->dev.model_name = hapd->conf->model_name ?
1053 os_strdup(hapd->conf->model_name) : NULL;
1054 wps->dev.model_number = hapd->conf->model_number ?
1055 os_strdup(hapd->conf->model_number) : NULL;
1056 wps->dev.serial_number = hapd->conf->serial_number ?
1057 os_strdup(hapd->conf->serial_number) : NULL;
1058 wps->config_methods =
1059 wps_config_methods_str2bin(hapd->conf->config_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 if ((wps->config_methods &
1061 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1062 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1063 wpa_printf(MSG_INFO, "WPS: Converting display to "
1064 "virtual_display for WPS 2.0 compliance");
1065 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1066 }
1067 if ((wps->config_methods &
1068 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1069 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1070 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1071 "virtual_push_button for WPS 2.0 compliance");
1072 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1075 WPS_DEV_TYPE_LEN);
1076
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001077 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0)
1078 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079
1080 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001081
1082 if (conf->wps_rf_bands) {
1083 wps->dev.rf_bands = conf->wps_rf_bands;
1084 } else {
1085 wps->dev.rf_bands =
1086 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001087 WPS_RF_50GHZ :
1088 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
1089 WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001090 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091
1092 if (conf->wpa & WPA_PROTO_RSN) {
1093 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1094 wps->auth_types |= WPS_AUTH_WPA2PSK;
1095 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1096 wps->auth_types |= WPS_AUTH_WPA2;
1097
Roshan Pius3a1667e2018-07-03 15:17:14 -07001098 if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1099 WPA_CIPHER_CCMP_256 |
1100 WPA_CIPHER_GCMP_256)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 wps->encr_types |= WPS_ENCR_AES;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001102 wps->encr_types_rsn |= WPS_ENCR_AES;
1103 }
1104 if (conf->rsn_pairwise & WPA_CIPHER_TKIP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 wps->encr_types |= WPS_ENCR_TKIP;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001106 wps->encr_types_rsn |= WPS_ENCR_TKIP;
1107 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 }
1109
1110 if (conf->wpa & WPA_PROTO_WPA) {
1111 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1112 wps->auth_types |= WPS_AUTH_WPAPSK;
1113 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1114 wps->auth_types |= WPS_AUTH_WPA;
1115
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001116 if (conf->wpa_pairwise & WPA_CIPHER_CCMP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 wps->encr_types |= WPS_ENCR_AES;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001118 wps->encr_types_wpa |= WPS_ENCR_AES;
1119 }
1120 if (conf->wpa_pairwise & WPA_CIPHER_TKIP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 wps->encr_types |= WPS_ENCR_TKIP;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001122 wps->encr_types_wpa |= WPS_ENCR_TKIP;
1123 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 }
1125
1126 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1127 wps->encr_types |= WPS_ENCR_NONE;
1128 wps->auth_types |= WPS_AUTH_OPEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 }
1130
1131 if (conf->ssid.wpa_psk_file) {
1132 /* Use per-device PSKs */
1133 } else if (conf->ssid.wpa_passphrase) {
1134 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1135 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1136 } else if (conf->ssid.wpa_psk) {
1137 wps->network_key = os_malloc(2 * PMK_LEN + 1);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001138 if (wps->network_key == NULL)
1139 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1141 conf->ssid.wpa_psk->psk, PMK_LEN);
1142 wps->network_key_len = 2 * PMK_LEN;
1143 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1144 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001145 if (wps->network_key == NULL)
1146 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1148 conf->ssid.wep.len[0]);
1149 wps->network_key_len = conf->ssid.wep.len[0];
1150 }
1151
1152 if (conf->ssid.wpa_psk) {
1153 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1154 wps->psk_set = 1;
1155 }
1156
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001157 wps->ap_auth_type = wps->auth_types;
1158 wps->ap_encr_type = wps->encr_types;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1160 /* Override parameters to enable security by default */
1161 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1162 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001163 wps->encr_types_rsn = WPS_ENCR_AES | WPS_ENCR_TKIP;
1164 wps->encr_types_wpa = WPS_ENCR_AES | WPS_ENCR_TKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 }
1166
Hai Shalom021b0b52019-04-10 11:17:58 -07001167 if ((hapd->conf->multi_ap & FRONTHAUL_BSS) &&
1168 hapd->conf->multi_ap_backhaul_ssid.ssid_len) {
1169 cfg.multi_ap_backhaul_ssid_len =
1170 hapd->conf->multi_ap_backhaul_ssid.ssid_len;
1171 cfg.multi_ap_backhaul_ssid =
1172 hapd->conf->multi_ap_backhaul_ssid.ssid;
1173
1174 if (conf->multi_ap_backhaul_ssid.wpa_passphrase) {
1175 cfg.multi_ap_backhaul_network_key = (const u8 *)
1176 conf->multi_ap_backhaul_ssid.wpa_passphrase;
1177 cfg.multi_ap_backhaul_network_key_len =
1178 os_strlen(conf->multi_ap_backhaul_ssid.wpa_passphrase);
1179 } else if (conf->multi_ap_backhaul_ssid.wpa_psk) {
1180 multi_ap_netw_key = os_malloc(2 * PMK_LEN + 1);
1181 if (!multi_ap_netw_key)
1182 goto fail;
1183 wpa_snprintf_hex((char *) multi_ap_netw_key,
1184 2 * PMK_LEN + 1,
1185 conf->multi_ap_backhaul_ssid.wpa_psk->psk,
1186 PMK_LEN);
1187 cfg.multi_ap_backhaul_network_key = multi_ap_netw_key;
1188 cfg.multi_ap_backhaul_network_key_len = 2 * PMK_LEN;
1189 }
1190 }
1191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 wps->ap_settings = conf->ap_settings;
1193 wps->ap_settings_len = conf->ap_settings_len;
1194
1195 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1196 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1197 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1198 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1199 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1200 cfg.cb_ctx = hapd;
1201 cfg.skip_cred_build = conf->skip_cred_build;
1202 cfg.extra_cred = conf->extra_cred;
1203 cfg.extra_cred_len = conf->extra_cred_len;
1204 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1205 conf->skip_cred_build;
1206 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
1207 cfg.static_wep_only = 1;
1208 cfg.dualband = interface_count(hapd->iface) > 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001209 if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1210 (WPS_RF_50GHZ | WPS_RF_24GHZ))
1211 cfg.dualband = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 if (cfg.dualband)
1213 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001214 cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215
1216 wps->registrar = wps_registrar_init(wps, &cfg);
1217 if (wps->registrar == NULL) {
1218 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001219 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 }
1221
1222#ifdef CONFIG_WPS_UPNP
1223 wps->friendly_name = hapd->conf->friendly_name;
1224 wps->manufacturer_url = hapd->conf->manufacturer_url;
1225 wps->model_description = hapd->conf->model_description;
1226 wps->model_url = hapd->conf->model_url;
1227 wps->upc = hapd->conf->upc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228#endif /* CONFIG_WPS_UPNP */
1229
1230 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1231
1232 hapd->wps = wps;
Hai Shalom021b0b52019-04-10 11:17:58 -07001233 bin_clear_free(multi_ap_netw_key, 2 * PMK_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234
1235 return 0;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001236
1237fail:
Hai Shalom021b0b52019-04-10 11:17:58 -07001238 bin_clear_free(multi_ap_netw_key, 2 * PMK_LEN);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001239 hostapd_free_wps(wps);
1240 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241}
1242
1243
Jouni Malinen87fd2792011-05-16 18:35:42 +03001244int hostapd_init_wps_complete(struct hostapd_data *hapd)
1245{
1246 struct wps_context *wps = hapd->wps;
1247
Jouni Malinen75ecf522011-06-27 15:19:46 -07001248 if (wps == NULL)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001249 return 0;
1250
1251#ifdef CONFIG_WPS_UPNP
1252 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1253 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1254 wps_registrar_deinit(wps->registrar);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001255 hostapd_free_wps(wps);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001256 hapd->wps = NULL;
1257 return -1;
1258 }
1259#endif /* CONFIG_WPS_UPNP */
1260
1261 return 0;
1262}
1263
1264
Dmitry Shmidt04949592012-07-19 12:16:46 -07001265static void hostapd_wps_nfc_clear(struct wps_context *wps)
1266{
1267#ifdef CONFIG_WPS_NFC
Dmitry Shmidt54605472013-11-08 11:10:19 -08001268 wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001269 wps->ap_nfc_dev_pw_id = 0;
1270 wpabuf_free(wps->ap_nfc_dh_pubkey);
1271 wps->ap_nfc_dh_pubkey = NULL;
1272 wpabuf_free(wps->ap_nfc_dh_privkey);
1273 wps->ap_nfc_dh_privkey = NULL;
1274 wpabuf_free(wps->ap_nfc_dev_pw);
1275 wps->ap_nfc_dev_pw = NULL;
1276#endif /* CONFIG_WPS_NFC */
1277}
1278
1279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280void hostapd_deinit_wps(struct hostapd_data *hapd)
1281{
1282 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1283 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001284 eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001285 if (hapd->wps == NULL) {
1286 hostapd_wps_clear_ies(hapd, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 return;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001288 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289#ifdef CONFIG_WPS_UPNP
1290 hostapd_wps_upnp_deinit(hapd);
1291#endif /* CONFIG_WPS_UPNP */
1292 wps_registrar_deinit(hapd->wps->registrar);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 wps_free_pending_msgs(hapd->wps->upnp_msgs);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001294 hostapd_free_wps(hapd->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295 hapd->wps = NULL;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001296 hostapd_wps_clear_ies(hapd, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297}
1298
1299
1300void hostapd_update_wps(struct hostapd_data *hapd)
1301{
1302 if (hapd->wps == NULL)
1303 return;
1304
1305#ifdef CONFIG_WPS_UPNP
1306 hapd->wps->friendly_name = hapd->conf->friendly_name;
1307 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1308 hapd->wps->model_description = hapd->conf->model_description;
1309 hapd->wps->model_url = hapd->conf->model_url;
1310 hapd->wps->upc = hapd->conf->upc;
1311#endif /* CONFIG_WPS_UPNP */
1312
1313 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1314
1315 if (hapd->conf->wps_state)
1316 wps_registrar_update_ie(hapd->wps->registrar);
1317 else
1318 hostapd_deinit_wps(hapd);
1319}
1320
1321
1322struct wps_add_pin_data {
1323 const u8 *addr;
1324 const u8 *uuid;
1325 const u8 *pin;
1326 size_t pin_len;
1327 int timeout;
1328 int added;
1329};
1330
1331
1332static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1333{
1334 struct wps_add_pin_data *data = ctx;
1335 int ret;
1336
1337 if (hapd->wps == NULL)
1338 return 0;
1339 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1340 data->uuid, data->pin, data->pin_len,
1341 data->timeout);
1342 if (ret == 0)
1343 data->added++;
1344 return ret;
1345}
1346
1347
1348int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1349 const char *uuid, const char *pin, int timeout)
1350{
1351 u8 u[UUID_LEN];
1352 struct wps_add_pin_data data;
1353
1354 data.addr = addr;
1355 data.uuid = u;
1356 data.pin = (const u8 *) pin;
1357 data.pin_len = os_strlen(pin);
1358 data.timeout = timeout;
1359 data.added = 0;
1360
1361 if (os_strcmp(uuid, "any") == 0)
1362 data.uuid = NULL;
1363 else {
1364 if (uuid_str2bin(uuid, u))
1365 return -1;
1366 data.uuid = u;
1367 }
1368 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1369 return -1;
1370 return data.added ? 0 : -1;
1371}
1372
1373
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001374struct wps_button_pushed_ctx {
1375 const u8 *p2p_dev_addr;
1376 unsigned int count;
1377};
1378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1380{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001381 struct wps_button_pushed_ctx *data = ctx;
1382
1383 if (hapd->wps) {
1384 data->count++;
1385 return wps_registrar_button_pushed(hapd->wps->registrar,
1386 data->p2p_dev_addr);
1387 }
1388
1389 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390}
1391
1392
1393int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1394 const u8 *p2p_dev_addr)
1395{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001396 struct wps_button_pushed_ctx ctx;
1397 int ret;
1398
1399 os_memset(&ctx, 0, sizeof(ctx));
1400 ctx.p2p_dev_addr = p2p_dev_addr;
1401 ret = hostapd_wps_for_each(hapd, wps_button_pushed, &ctx);
1402 if (ret == 0 && !ctx.count)
1403 ret = -1;
1404 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405}
1406
1407
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001408struct wps_cancel_ctx {
1409 unsigned int count;
1410};
1411
Dmitry Shmidt04949592012-07-19 12:16:46 -07001412static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1413{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001414 struct wps_cancel_ctx *data = ctx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001415
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001416 if (hapd->wps) {
1417 data->count++;
1418 wps_registrar_wps_cancel(hapd->wps->registrar);
1419 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001420 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_CANCEL);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001421 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001422
1423 return 0;
1424}
1425
1426
1427int hostapd_wps_cancel(struct hostapd_data *hapd)
1428{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001429 struct wps_cancel_ctx ctx;
1430 int ret;
1431
1432 os_memset(&ctx, 0, sizeof(ctx));
1433 ret = hostapd_wps_for_each(hapd, wps_cancel, &ctx);
1434 if (ret == 0 && !ctx.count)
1435 ret = -1;
1436 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001437}
1438
1439
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001440static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1441 const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001442 const u8 *ie, size_t ie_len,
1443 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444{
1445 struct hostapd_data *hapd = ctx;
1446 struct wpabuf *wps_ie;
1447 struct ieee802_11_elems elems;
1448
1449 if (hapd->wps == NULL)
1450 return 0;
1451
1452 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1453 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1454 MACSTR, MAC2STR(addr));
1455 return 0;
1456 }
1457
1458 if (elems.ssid && elems.ssid_len > 0 &&
1459 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1460 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1461 0))
1462 return 0; /* Not for us */
1463
1464 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1465 if (wps_ie == NULL)
1466 return 0;
1467 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1468 wpabuf_free(wps_ie);
1469 return 0;
1470 }
1471
1472 if (wpabuf_len(wps_ie) > 0) {
1473 int p2p_wildcard = 0;
1474#ifdef CONFIG_P2P
1475 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1476 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1477 P2P_WILDCARD_SSID_LEN) == 0)
1478 p2p_wildcard = 1;
1479#endif /* CONFIG_P2P */
1480 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1481 p2p_wildcard);
1482#ifdef CONFIG_WPS_UPNP
1483 /* FIX: what exactly should be included in the WLANEvent?
1484 * WPS attributes? Full ProbeReq frame? */
1485 if (!p2p_wildcard)
1486 upnp_wps_device_send_wlan_event(
1487 hapd->wps_upnp, addr,
1488 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1489#endif /* CONFIG_WPS_UPNP */
1490 }
1491
1492 wpabuf_free(wps_ie);
1493
1494 return 0;
1495}
1496
1497
1498#ifdef CONFIG_WPS_UPNP
1499
1500static int hostapd_rx_req_put_wlan_response(
1501 void *priv, enum upnp_wps_wlanevent_type ev_type,
1502 const u8 *mac_addr, const struct wpabuf *msg,
1503 enum wps_msg_type msg_type)
1504{
1505 struct hostapd_data *hapd = priv;
1506 struct sta_info *sta;
1507 struct upnp_pending_message *p;
1508
1509 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1510 MACSTR, ev_type, MAC2STR(mac_addr));
1511 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1512 wpabuf_head(msg), wpabuf_len(msg));
1513 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1514 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1515 "PutWLANResponse WLANEventType %d", ev_type);
1516 return -1;
1517 }
1518
1519 /*
1520 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1521 * server implementation for delivery to the peer.
1522 */
1523
1524 sta = ap_get_sta(hapd, mac_addr);
1525#ifndef CONFIG_WPS_STRICT
1526 if (!sta) {
1527 /*
1528 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1529 * Pick STA that is in an ongoing WPS registration without
1530 * checking the MAC address.
1531 */
1532 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1533 "on NewWLANEventMAC; try wildcard match");
1534 for (sta = hapd->sta_list; sta; sta = sta->next) {
1535 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1536 break;
1537 }
1538 }
1539#endif /* CONFIG_WPS_STRICT */
1540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001541 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1543 return 0;
1544 }
1545
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001546 if (!sta->eapol_sm) {
1547 /*
1548 * This can happen, e.g., if an ER sends an extra message after
1549 * the station has disassociated (but not fully
1550 * deauthenticated).
1551 */
1552 wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
1553 return 0;
1554 }
1555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 p = os_zalloc(sizeof(*p));
1557 if (p == NULL)
1558 return -1;
1559 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1560 p->msg = wpabuf_dup(msg);
1561 p->type = msg_type;
1562 p->next = hapd->wps->upnp_msgs;
1563 hapd->wps->upnp_msgs = p;
1564
1565 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1566}
1567
1568
1569static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1570 struct wps_context *wps)
1571{
1572 struct upnp_wps_device_ctx *ctx;
1573
1574 if (!hapd->conf->upnp_iface)
1575 return 0;
1576 ctx = os_zalloc(sizeof(*ctx));
1577 if (ctx == NULL)
1578 return -1;
1579
1580 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1581 if (hapd->conf->ap_pin)
1582 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1583
1584 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1585 hapd->conf->upnp_iface);
1586 if (hapd->wps_upnp == NULL)
1587 return -1;
1588 wps->wps_upnp = hapd->wps_upnp;
1589
1590 return 0;
1591}
1592
1593
1594static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1595{
1596 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1597}
1598
1599#endif /* CONFIG_WPS_UPNP */
1600
1601
1602int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1603 char *buf, size_t buflen)
1604{
1605 if (hapd->wps == NULL)
1606 return 0;
1607 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1608}
1609
1610
1611static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1612{
1613 struct hostapd_data *hapd = eloop_data;
1614 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1615 hostapd_wps_ap_pin_disable(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001616 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617}
1618
1619
1620static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1621{
1622 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1623 hapd->ap_pin_failures = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001624 hapd->ap_pin_failures_consecutive = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 hapd->conf->ap_setup_locked = 0;
1626 if (hapd->wps->ap_setup_locked) {
1627 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1628 hapd->wps->ap_setup_locked = 0;
1629 wps_registrar_update_ie(hapd->wps->registrar);
1630 }
1631 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1632 if (timeout > 0)
1633 eloop_register_timeout(timeout, 0,
1634 hostapd_wps_ap_pin_timeout, hapd, NULL);
1635}
1636
1637
1638static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1639{
1640 os_free(hapd->conf->ap_pin);
1641 hapd->conf->ap_pin = NULL;
1642#ifdef CONFIG_WPS_UPNP
1643 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1644#endif /* CONFIG_WPS_UPNP */
1645 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1646 return 0;
1647}
1648
1649
1650void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1651{
1652 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1653 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1654}
1655
1656
1657struct wps_ap_pin_data {
1658 char pin_txt[9];
1659 int timeout;
1660};
1661
1662
1663static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1664{
1665 struct wps_ap_pin_data *data = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001666
1667 if (!hapd->wps)
1668 return 0;
1669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 os_free(hapd->conf->ap_pin);
1671 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1672#ifdef CONFIG_WPS_UPNP
1673 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1674#endif /* CONFIG_WPS_UPNP */
1675 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1676 return 0;
1677}
1678
1679
1680const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1681{
1682 unsigned int pin;
1683 struct wps_ap_pin_data data;
1684
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001685 if (wps_generate_pin(&pin) < 0)
1686 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001687 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 data.timeout = timeout;
1689 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1690 return hapd->conf->ap_pin;
1691}
1692
1693
1694const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1695{
1696 return hapd->conf->ap_pin;
1697}
1698
1699
1700int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1701 int timeout)
1702{
1703 struct wps_ap_pin_data data;
1704 int ret;
1705
1706 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001707 if (os_snprintf_error(sizeof(data.pin_txt), ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 return -1;
1709 data.timeout = timeout;
1710 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1711}
1712
1713
1714static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1715{
1716 if (hapd->wps)
1717 wps_registrar_update_ie(hapd->wps->registrar);
1718 return 0;
1719}
1720
1721
1722void hostapd_wps_update_ie(struct hostapd_data *hapd)
1723{
1724 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1725}
1726
1727
1728int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1729 const char *auth, const char *encr, const char *key)
1730{
1731 struct wps_credential cred;
1732 size_t len;
1733
1734 os_memset(&cred, 0, sizeof(cred));
1735
1736 len = os_strlen(ssid);
1737 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1738 hexstr2bin(ssid, cred.ssid, len / 2))
1739 return -1;
1740 cred.ssid_len = len / 2;
1741
1742 if (os_strncmp(auth, "OPEN", 4) == 0)
1743 cred.auth_type = WPS_AUTH_OPEN;
1744 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1745 cred.auth_type = WPS_AUTH_WPAPSK;
1746 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1747 cred.auth_type = WPS_AUTH_WPA2PSK;
1748 else
1749 return -1;
1750
1751 if (encr) {
1752 if (os_strncmp(encr, "NONE", 4) == 0)
1753 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 else if (os_strncmp(encr, "TKIP", 4) == 0)
1755 cred.encr_type = WPS_ENCR_TKIP;
1756 else if (os_strncmp(encr, "CCMP", 4) == 0)
1757 cred.encr_type = WPS_ENCR_AES;
1758 else
1759 return -1;
1760 } else
1761 cred.encr_type = WPS_ENCR_NONE;
1762
1763 if (key) {
1764 len = os_strlen(key);
1765 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1766 hexstr2bin(key, cred.key, len / 2))
1767 return -1;
1768 cred.key_len = len / 2;
1769 }
1770
1771 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1772}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001773
1774
1775#ifdef CONFIG_WPS_NFC
1776
1777struct wps_nfc_password_token_data {
1778 const u8 *oob_dev_pw;
1779 size_t oob_dev_pw_len;
1780 int added;
1781};
1782
1783
1784static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1785{
1786 struct wps_nfc_password_token_data *data = ctx;
1787 int ret;
1788
1789 if (hapd->wps == NULL)
1790 return 0;
1791 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1792 data->oob_dev_pw,
1793 data->oob_dev_pw_len);
1794 if (ret == 0)
1795 data->added++;
1796 return ret;
1797}
1798
1799
1800static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1801 struct wps_parse_attr *attr)
1802{
1803 struct wps_nfc_password_token_data data;
1804
1805 data.oob_dev_pw = attr->oob_dev_password;
1806 data.oob_dev_pw_len = attr->oob_dev_password_len;
1807 data.added = 0;
1808 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1809 return -1;
1810 return data.added ? 0 : -1;
1811}
1812
1813
1814static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1815 const struct wpabuf *wps)
1816{
1817 struct wps_parse_attr attr;
1818
1819 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1820
1821 if (wps_parse_msg(wps, &attr)) {
1822 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1823 return -1;
1824 }
1825
1826 if (attr.oob_dev_password)
1827 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1828
1829 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1830 return -1;
1831}
1832
1833
1834int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1835 const struct wpabuf *data)
1836{
1837 const struct wpabuf *wps = data;
1838 struct wpabuf *tmp = NULL;
1839 int ret;
1840
1841 if (wpabuf_len(data) < 4)
1842 return -1;
1843
1844 if (*wpabuf_head_u8(data) != 0x10) {
1845 /* Assume this contains full NDEF record */
1846 tmp = ndef_parse_wifi(data);
1847 if (tmp == NULL) {
1848 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1849 return -1;
1850 }
1851 wps = tmp;
1852 }
1853
1854 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1855 wpabuf_free(tmp);
1856 return ret;
1857}
1858
1859
1860struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1861 int ndef)
1862{
1863 struct wpabuf *ret;
1864
1865 if (hapd->wps == NULL)
1866 return NULL;
1867
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001868 ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
1869 hapd->iconf->channel);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001870 if (ndef && ret) {
1871 struct wpabuf *tmp;
1872 tmp = ndef_build_wifi(ret);
1873 wpabuf_free(ret);
1874 if (tmp == NULL)
1875 return NULL;
1876 ret = tmp;
1877 }
1878
1879 return ret;
1880}
1881
1882
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001883struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
1884{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001885 struct wpabuf *ret;
1886
1887 if (hapd->wps == NULL)
1888 return NULL;
1889
1890 if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
1891 struct wps_context *wps = hapd->wps;
1892 if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
1893 &hapd->conf->wps_nfc_dh_privkey) < 0)
1894 return NULL;
1895 hostapd_wps_nfc_clear(wps);
1896 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
1897 wps->ap_nfc_dh_pubkey =
1898 wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1899 wps->ap_nfc_dh_privkey =
1900 wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1901 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
1902 hostapd_wps_nfc_clear(wps);
1903 return NULL;
1904 }
1905 }
1906
1907 ret = wps_build_nfc_handover_sel(hapd->wps,
1908 hapd->conf->wps_nfc_dh_pubkey,
1909 hapd->own_addr, hapd->iface->freq);
1910
1911 if (ndef && ret) {
1912 struct wpabuf *tmp;
1913 tmp = ndef_build_wifi(ret);
1914 wpabuf_free(ret);
1915 if (tmp == NULL)
1916 return NULL;
1917 ret = tmp;
1918 }
1919
1920 return ret;
1921}
1922
1923
1924int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
1925 const struct wpabuf *req,
1926 const struct wpabuf *sel)
1927{
1928 struct wpabuf *wps;
1929 int ret = -1;
1930 u16 wsc_len;
1931 const u8 *pos;
1932 struct wpabuf msg;
1933 struct wps_parse_attr attr;
1934 u16 dev_pw_id;
1935
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001936 /*
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001937 * Enrollee/station is always initiator of the NFC connection handover,
1938 * so use the request message here to find Enrollee public key hash.
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001939 */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001940 wps = ndef_parse_wifi(req);
1941 if (wps == NULL)
1942 return -1;
1943 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
1944 "payload from NFC connection handover");
1945 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
1946 if (wpabuf_len(wps) < 2) {
1947 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
1948 "Message");
1949 goto out;
1950 }
1951 pos = wpabuf_head(wps);
1952 wsc_len = WPA_GET_BE16(pos);
1953 if (wsc_len > wpabuf_len(wps) - 2) {
1954 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
1955 "in rt Wi-Fi Handover Request Message", wsc_len);
1956 goto out;
1957 }
1958 pos += 2;
1959
1960 wpa_hexdump(MSG_DEBUG,
1961 "WPS: WSC attributes in Wi-Fi Handover Request Message",
1962 pos, wsc_len);
1963 if (wsc_len < wpabuf_len(wps) - 2) {
1964 wpa_hexdump(MSG_DEBUG,
1965 "WPS: Ignore extra data after WSC attributes",
1966 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
1967 }
1968
1969 wpabuf_set(&msg, pos, wsc_len);
1970 ret = wps_parse_msg(&msg, &attr);
1971 if (ret < 0) {
1972 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
1973 "Wi-Fi Handover Request Message");
1974 goto out;
1975 }
1976
1977 if (attr.oob_dev_password == NULL ||
1978 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
1979 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
1980 "included in Wi-Fi Handover Request Message");
1981 ret = -1;
1982 goto out;
1983 }
1984
1985 if (attr.uuid_e == NULL) {
1986 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
1987 "Handover Request Message");
1988 ret = -1;
1989 goto out;
1990 }
1991
1992 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
1993
1994 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
1995 attr.oob_dev_password, attr.oob_dev_password_len);
1996 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
1997 WPS_OOB_PUBKEY_HASH_LEN);
1998 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
1999 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2000 "%u in Wi-Fi Handover Request Message", dev_pw_id);
2001 ret = -1;
2002 goto out;
2003 }
2004 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2005 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2006
2007 ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
2008 attr.oob_dev_password,
2009 DEV_PW_NFC_CONNECTION_HANDOVER,
2010 NULL, 0, 1);
2011
2012out:
2013 wpabuf_free(wps);
2014 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002015}
2016
2017
Dmitry Shmidt04949592012-07-19 12:16:46 -07002018struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
2019{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002020 if (hapd->conf->wps_nfc_pw_from_config) {
2021 return wps_nfc_token_build(ndef,
2022 hapd->conf->wps_nfc_dev_pw_id,
2023 hapd->conf->wps_nfc_dh_pubkey,
2024 hapd->conf->wps_nfc_dev_pw);
2025 }
2026
Dmitry Shmidt04949592012-07-19 12:16:46 -07002027 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
2028 &hapd->conf->wps_nfc_dh_pubkey,
2029 &hapd->conf->wps_nfc_dh_privkey,
2030 &hapd->conf->wps_nfc_dev_pw);
2031}
2032
2033
2034int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
2035{
2036 struct wps_context *wps = hapd->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002037 struct wpabuf *pw;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002038
2039 if (wps == NULL)
2040 return -1;
2041
2042 if (!hapd->conf->wps_nfc_dh_pubkey ||
2043 !hapd->conf->wps_nfc_dh_privkey ||
2044 !hapd->conf->wps_nfc_dev_pw ||
2045 !hapd->conf->wps_nfc_dev_pw_id)
2046 return -1;
2047
2048 hostapd_wps_nfc_clear(wps);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002049 wpa_printf(MSG_DEBUG,
2050 "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
2051 hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002052 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
2053 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
2054 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08002055 pw = hapd->conf->wps_nfc_dev_pw;
2056 wps->ap_nfc_dev_pw = wpabuf_alloc(
2057 wpabuf_len(pw) * 2 + 1);
2058 if (wps->ap_nfc_dev_pw) {
2059 wpa_snprintf_hex_uppercase(
2060 (char *) wpabuf_put(wps->ap_nfc_dev_pw,
2061 wpabuf_len(pw) * 2),
2062 wpabuf_len(pw) * 2 + 1,
2063 wpabuf_head(pw), wpabuf_len(pw));
2064 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002065
2066 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
2067 !wps->ap_nfc_dev_pw) {
2068 hostapd_wps_nfc_clear(wps);
2069 return -1;
2070 }
2071
2072 return 0;
2073}
2074
2075
2076void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
2077{
Dmitry Shmidt54605472013-11-08 11:10:19 -08002078 wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
2079 hapd->conf->iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002080 hostapd_wps_nfc_clear(hapd->wps);
2081}
2082
2083#endif /* CONFIG_WPS_NFC */