blob: 7e74829138bf71f24b1065915956906780128aa7 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / WPS integration
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2008-2012, 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
272static int str_starts(const char *str, const char *start)
273{
274 return os_strncmp(str, start, os_strlen(start)) == 0;
275}
276
277
278static void wps_reload_config(void *eloop_data, void *user_ctx)
279{
280 struct hostapd_iface *iface = eloop_data;
281
282 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700283 if (iface->interfaces == NULL ||
284 iface->interfaces->reload_config(iface) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
286 "configuration");
287 }
288}
289
290
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800291void hostapd_wps_eap_completed(struct hostapd_data *hapd)
292{
293 /*
294 * Reduce race condition of the station trying to reconnect immediately
295 * after AP reconfiguration through WPS by rescheduling the reload
296 * timeout to happen after EAP completion rather than the originally
297 * scheduled 100 ms after new configuration became known.
298 */
299 if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
300 1)
301 wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
302}
303
304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800305static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
306 size_t attr_len)
307{
308 size_t blen = attr_len * 2 + 1;
309 char *buf = os_malloc(blen);
310 if (buf) {
311 wpa_snprintf_hex(buf, blen, attr, attr_len);
312 wpa_msg(hapd->msg_ctx, MSG_INFO,
313 WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
314 os_free(buf);
315 }
316}
317
318
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700319static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
320 const struct wps_credential *cred)
321{
322 struct hostapd_bss_config *bss = hapd->conf;
323
324 wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
325
326 bss->wps_state = 2;
327 if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
328 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
329 bss->ssid.ssid_len = cred->ssid_len;
330 bss->ssid.ssid_set = 1;
331 }
332
333 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
334 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
335 bss->wpa = 3;
336 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
337 bss->wpa = 2;
338 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
339 bss->wpa = 1;
340 else
341 bss->wpa = 0;
342
343 if (bss->wpa) {
344 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
345 bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
346 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
347 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
348
349 bss->wpa_pairwise = 0;
350 if (cred->encr_type & WPS_ENCR_AES)
351 bss->wpa_pairwise |= WPA_CIPHER_CCMP;
352 if (cred->encr_type & WPS_ENCR_TKIP)
353 bss->wpa_pairwise |= WPA_CIPHER_TKIP;
354 bss->rsn_pairwise = bss->wpa_pairwise;
355 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
356 bss->wpa_pairwise,
357 bss->rsn_pairwise);
358
359 if (cred->key_len >= 8 && cred->key_len < 64) {
360 os_free(bss->ssid.wpa_passphrase);
361 bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
362 if (bss->ssid.wpa_passphrase)
363 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
364 cred->key_len);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800365 hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700366 } else if (cred->key_len == 64) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800367 hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700368 bss->ssid.wpa_psk =
369 os_zalloc(sizeof(struct hostapd_wpa_psk));
370 if (bss->ssid.wpa_psk &&
371 hexstr2bin((const char *) cred->key,
372 bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
373 bss->ssid.wpa_psk->group = 1;
374 os_free(bss->ssid.wpa_passphrase);
375 bss->ssid.wpa_passphrase = NULL;
376 }
377 }
378 bss->auth_algs = 1;
379 } else {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700380 /*
381 * WPS 2.0 does not allow WEP to be configured, so no need to
382 * process that option here either.
383 */
384 bss->auth_algs = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700385 }
386
387 /* Schedule configuration reload after short period of time to allow
388 * EAP-WSC to be finished.
389 */
390 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
391 NULL);
392
393 return 0;
394}
395
396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
398{
399 const struct wps_credential *cred = ctx;
400 FILE *oconf, *nconf;
401 size_t len, i;
402 char *tmp_fname;
403 char buf[1024];
404 int multi_bss;
405 int wpa;
406
407 if (hapd->wps == NULL)
408 return 0;
409
410 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
411 cred->cred_attr, cred->cred_attr_len);
412
413 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
414 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
415 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
416 cred->auth_type);
417 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
418 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
419 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
420 cred->key, cred->key_len);
421 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
422 MAC2STR(cred->mac_addr));
423
424 if ((hapd->conf->wps_cred_processing == 1 ||
425 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800426 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
427 } else if (hapd->conf->wps_cred_processing == 1 ||
428 hapd->conf->wps_cred_processing == 2) {
429 struct wpabuf *attr;
430 attr = wpabuf_alloc(200);
431 if (attr && wps_build_credential_wrap(attr, cred) == 0)
432 hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
433 wpabuf_len(attr));
434 wpabuf_free(attr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 } else
436 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
437
438 if (hapd->conf->wps_cred_processing == 1)
439 return 0;
440
441 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
442 hapd->wps->ssid_len = cred->ssid_len;
443 hapd->wps->encr_types = cred->encr_type;
444 hapd->wps->auth_types = cred->auth_type;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800445 hapd->wps->ap_encr_type = cred->encr_type;
446 hapd->wps->ap_auth_type = cred->auth_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 if (cred->key_len == 0) {
448 os_free(hapd->wps->network_key);
449 hapd->wps->network_key = NULL;
450 hapd->wps->network_key_len = 0;
451 } else {
452 if (hapd->wps->network_key == NULL ||
453 hapd->wps->network_key_len < cred->key_len) {
454 hapd->wps->network_key_len = 0;
455 os_free(hapd->wps->network_key);
456 hapd->wps->network_key = os_malloc(cred->key_len);
457 if (hapd->wps->network_key == NULL)
458 return -1;
459 }
460 hapd->wps->network_key_len = cred->key_len;
461 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
462 }
463 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
464
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 if (hapd->iface->config_fname == NULL)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700466 return hapd_wps_reconfig_in_memory(hapd, cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 len = os_strlen(hapd->iface->config_fname) + 5;
468 tmp_fname = os_malloc(len);
469 if (tmp_fname == NULL)
470 return -1;
471 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
472
473 oconf = fopen(hapd->iface->config_fname, "r");
474 if (oconf == NULL) {
475 wpa_printf(MSG_WARNING, "WPS: Could not open current "
476 "configuration file");
477 os_free(tmp_fname);
478 return -1;
479 }
480
481 nconf = fopen(tmp_fname, "w");
482 if (nconf == NULL) {
483 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
484 "configuration file");
485 os_free(tmp_fname);
486 fclose(oconf);
487 return -1;
488 }
489
490 fprintf(nconf, "# WPS configuration - START\n");
491
492 fprintf(nconf, "wps_state=2\n");
493
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700494 if (is_hex(cred->ssid, cred->ssid_len)) {
495 fprintf(nconf, "ssid2=");
496 for (i = 0; i < cred->ssid_len; i++)
497 fprintf(nconf, "%02x", cred->ssid[i]);
498 fprintf(nconf, "\n");
499 } else {
500 fprintf(nconf, "ssid=");
501 for (i = 0; i < cred->ssid_len; i++)
502 fputc(cred->ssid[i], nconf);
503 fprintf(nconf, "\n");
504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505
506 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
507 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
508 wpa = 3;
509 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
510 wpa = 2;
511 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
512 wpa = 1;
513 else
514 wpa = 0;
515
516 if (wpa) {
517 char *prefix;
518 fprintf(nconf, "wpa=%d\n", wpa);
519
520 fprintf(nconf, "wpa_key_mgmt=");
521 prefix = "";
522 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
523 fprintf(nconf, "WPA-EAP");
524 prefix = " ";
525 }
526 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
527 fprintf(nconf, "%sWPA-PSK", prefix);
528 fprintf(nconf, "\n");
529
530 fprintf(nconf, "wpa_pairwise=");
531 prefix = "";
532 if (cred->encr_type & WPS_ENCR_AES) {
533 fprintf(nconf, "CCMP");
534 prefix = " ";
535 }
536 if (cred->encr_type & WPS_ENCR_TKIP) {
537 fprintf(nconf, "%sTKIP", prefix);
538 }
539 fprintf(nconf, "\n");
540
541 if (cred->key_len >= 8 && cred->key_len < 64) {
542 fprintf(nconf, "wpa_passphrase=");
543 for (i = 0; i < cred->key_len; i++)
544 fputc(cred->key[i], nconf);
545 fprintf(nconf, "\n");
546 } else if (cred->key_len == 64) {
547 fprintf(nconf, "wpa_psk=");
548 for (i = 0; i < cred->key_len; i++)
549 fputc(cred->key[i], nconf);
550 fprintf(nconf, "\n");
551 } else {
552 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
553 "for WPA/WPA2",
554 (unsigned long) cred->key_len);
555 }
556
557 fprintf(nconf, "auth_algs=1\n");
558 } else {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700559 /*
560 * WPS 2.0 does not allow WEP to be configured, so no need to
561 * process that option here either.
562 */
563 fprintf(nconf, "auth_algs=1\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 }
565
566 fprintf(nconf, "# WPS configuration - END\n");
567
568 multi_bss = 0;
569 while (fgets(buf, sizeof(buf), oconf)) {
570 if (os_strncmp(buf, "bss=", 4) == 0)
571 multi_bss = 1;
572 if (!multi_bss &&
573 (str_starts(buf, "ssid=") ||
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700574 str_starts(buf, "ssid2=") ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575 str_starts(buf, "auth_algs=") ||
576 str_starts(buf, "wep_default_key=") ||
577 str_starts(buf, "wep_key") ||
578 str_starts(buf, "wps_state=") ||
579 str_starts(buf, "wpa=") ||
580 str_starts(buf, "wpa_psk=") ||
581 str_starts(buf, "wpa_pairwise=") ||
582 str_starts(buf, "rsn_pairwise=") ||
583 str_starts(buf, "wpa_key_mgmt=") ||
584 str_starts(buf, "wpa_passphrase="))) {
585 fprintf(nconf, "#WPS# %s", buf);
586 } else
587 fprintf(nconf, "%s", buf);
588 }
589
590 fclose(nconf);
591 fclose(oconf);
592
593 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
594 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
595 "configuration file: %s", strerror(errno));
596 os_free(tmp_fname);
597 return -1;
598 }
599
600 os_free(tmp_fname);
601
602 /* Schedule configuration reload after short period of time to allow
603 * EAP-WSC to be finished.
604 */
605 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
606 NULL);
607
608 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
609
610 return 0;
611}
612
613
614static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
615{
616 struct hostapd_data *hapd = ctx;
617 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
618}
619
620
621static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
622{
623 struct hostapd_data *hapd = eloop_data;
624
625 if (hapd->conf->ap_setup_locked)
626 return;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800627 if (hapd->ap_pin_failures_consecutive >= 10)
628 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629
630 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
631 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
632 hapd->wps->ap_setup_locked = 0;
633 wps_registrar_update_ie(hapd->wps->registrar);
634}
635
636
637static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
638{
639 struct wps_event_pwd_auth_fail *data = ctx;
640
641 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
642 return 0;
643
644 /*
645 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
646 * for some time if this happens multiple times to slow down brute
647 * force attacks.
648 */
649 hapd->ap_pin_failures++;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800650 hapd->ap_pin_failures_consecutive++;
651 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
652 "(%u consecutive)",
653 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 if (hapd->ap_pin_failures < 3)
655 return 0;
656
657 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
658 hapd->wps->ap_setup_locked = 1;
659
660 wps_registrar_update_ie(hapd->wps->registrar);
661
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800662 if (!hapd->conf->ap_setup_locked &&
663 hapd->ap_pin_failures_consecutive >= 10) {
664 /*
665 * In indefinite lockdown - disable automatic AP PIN
666 * reenablement.
667 */
668 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
669 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
670 } else if (!hapd->conf->ap_setup_locked) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 if (hapd->ap_pin_lockout_time == 0)
672 hapd->ap_pin_lockout_time = 60;
673 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
674 (hapd->ap_pin_failures % 3) == 0)
675 hapd->ap_pin_lockout_time *= 2;
676
677 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
678 hapd->ap_pin_lockout_time);
679 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
680 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
681 hostapd_wps_reenable_ap_pin, hapd,
682 NULL);
683 }
684
685 return 0;
686}
687
688
689static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
690 struct wps_event_pwd_auth_fail *data)
691{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700692 /* Update WPS Status - Authentication Failure */
693 wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
694 hapd->wps_stats.status = WPS_STATUS_FAILURE;
695 hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
696 os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
699}
700
701
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800702static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
703{
704 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
705 return 0;
706
707 if (hapd->ap_pin_failures_consecutive == 0)
708 return 0;
709
710 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
711 "- total validation failures %u (%u consecutive)",
712 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
713 hapd->ap_pin_failures_consecutive = 0;
714
715 return 0;
716}
717
718
719static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
720{
721 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
722}
723
724
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700725static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
726{
727 /* Update WPS Status - PBC Overlap */
728 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
729}
730
731
732static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
733{
734 /* Update WPS PBC Status:PBC Timeout */
735 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
736}
737
738
739static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
740{
741 /* Update WPS PBC status - Active */
742 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
743}
744
745
746static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
747{
748 /* Update WPS PBC status - Active */
749 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
750}
751
752
753static void hostapd_wps_event_success(struct hostapd_data *hapd,
754 struct wps_event_success *success)
755{
756 /* Update WPS status - Success */
757 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
758 hapd->wps_stats.status = WPS_STATUS_SUCCESS;
759 os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
760}
761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762
763static void hostapd_wps_event_fail(struct hostapd_data *hapd,
764 struct wps_event_fail *fail)
765{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700766 /* Update WPS status - Failure */
767 hapd->wps_stats.status = WPS_STATUS_FAILURE;
768 os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
769
770 hapd->wps_stats.failure_reason = fail->error_indication;
771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772 if (fail->error_indication > 0 &&
773 fail->error_indication < NUM_WPS_EI_VALUES) {
774 wpa_msg(hapd->msg_ctx, MSG_INFO,
775 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
776 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700777 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 } else {
779 wpa_msg(hapd->msg_ctx, MSG_INFO,
780 WPS_EVENT_FAIL "msg=%d config_error=%d",
781 fail->msg, fail->config_error);
782 }
783}
784
785
786static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
787 union wps_event_data *data)
788{
789 struct hostapd_data *hapd = ctx;
790
791 switch (event) {
792 case WPS_EV_M2D:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800793 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794 break;
795 case WPS_EV_FAIL:
796 hostapd_wps_event_fail(hapd, &data->fail);
797 break;
798 case WPS_EV_SUCCESS:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700799 hostapd_wps_event_success(hapd, &data->success);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801 break;
802 case WPS_EV_PWD_AUTH_FAIL:
803 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
804 break;
805 case WPS_EV_PBC_OVERLAP:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700806 hostapd_wps_event_pbc_overlap(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800807 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 break;
809 case WPS_EV_PBC_TIMEOUT:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700810 hostapd_wps_event_pbc_timeout(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800811 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700813 case WPS_EV_PBC_ACTIVE:
814 hostapd_wps_event_pbc_active(hapd);
815 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
816 break;
817 case WPS_EV_PBC_DISABLE:
818 hostapd_wps_event_pbc_disable(hapd);
819 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
820 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 case WPS_EV_ER_AP_ADD:
822 break;
823 case WPS_EV_ER_AP_REMOVE:
824 break;
825 case WPS_EV_ER_ENROLLEE_ADD:
826 break;
827 case WPS_EV_ER_ENROLLEE_REMOVE:
828 break;
829 case WPS_EV_ER_AP_SETTINGS:
830 break;
831 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
832 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800833 case WPS_EV_AP_PIN_SUCCESS:
834 hostapd_wps_ap_pin_success(hapd);
835 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 }
837 if (hapd->wps_event_cb)
838 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
839}
840
841
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700842static int hostapd_wps_rf_band_cb(void *ctx)
843{
844 struct hostapd_data *hapd = ctx;
845
846 return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
847 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
848}
849
850
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700851static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852{
853 wpabuf_free(hapd->wps_beacon_ie);
854 hapd->wps_beacon_ie = NULL;
855
856 wpabuf_free(hapd->wps_probe_resp_ie);
857 hapd->wps_probe_resp_ie = NULL;
858
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700859 if (deinit_only) {
860 hostapd_reset_ap_wps_ie(hapd);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700861 return;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700862 }
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864 hostapd_set_ap_wps_ie(hapd);
865}
866
867
868static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
869{
870 const u8 **uuid = ctx;
871 size_t j;
872
873 if (iface == NULL)
874 return 0;
875 for (j = 0; j < iface->num_bss; j++) {
876 struct hostapd_data *hapd = iface->bss[j];
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700877 if (hapd->wps && !hapd->conf->wps_independent &&
878 !is_nil_uuid(hapd->wps->uuid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 *uuid = hapd->wps->uuid;
880 return 1;
881 }
882 }
883
884 return 0;
885}
886
887
888static const u8 * get_own_uuid(struct hostapd_iface *iface)
889{
890 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700891 if (iface->interfaces == NULL ||
892 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 return NULL;
894 uuid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700895 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
896 &uuid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 return uuid;
898}
899
900
901static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
902{
903 int *count= ctx;
904 (*count)++;
905 return 0;
906}
907
908
909static int interface_count(struct hostapd_iface *iface)
910{
911 int count = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700912 if (iface->interfaces == NULL ||
913 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700915 iface->interfaces->for_each_interface(iface->interfaces,
916 count_interface_cb, &count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 return count;
918}
919
920
921static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
922 struct wps_context *wps)
923{
924 int i;
925
926 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
927 wpabuf_free(wps->dev.vendor_ext[i]);
928 wps->dev.vendor_ext[i] = NULL;
929
930 if (hapd->conf->wps_vendor_ext[i] == NULL)
931 continue;
932
933 wps->dev.vendor_ext[i] =
934 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
935 if (wps->dev.vendor_ext[i] == NULL) {
936 while (--i >= 0)
937 wpabuf_free(wps->dev.vendor_ext[i]);
938 return -1;
939 }
940 }
941
942 return 0;
943}
944
945
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700946static void hostapd_free_wps(struct wps_context *wps)
947{
948 int i;
949
950 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
951 wpabuf_free(wps->dev.vendor_ext[i]);
952 wps_device_data_free(&wps->dev);
953 os_free(wps->network_key);
954 hostapd_wps_nfc_clear(wps);
955 wpabuf_free(wps->dh_pubkey);
956 wpabuf_free(wps->dh_privkey);
957 os_free(wps);
958}
959
960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961int hostapd_init_wps(struct hostapd_data *hapd,
962 struct hostapd_bss_config *conf)
963{
964 struct wps_context *wps;
965 struct wps_registrar_config cfg;
966
967 if (conf->wps_state == 0) {
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700968 hostapd_wps_clear_ies(hapd, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 return 0;
970 }
971
972 wps = os_zalloc(sizeof(*wps));
973 if (wps == NULL)
974 return -1;
975
976 wps->cred_cb = hostapd_wps_cred_cb;
977 wps->event_cb = hostapd_wps_event_cb;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700978 wps->rf_band_cb = hostapd_wps_rf_band_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 wps->cb_ctx = hapd;
980
981 os_memset(&cfg, 0, sizeof(cfg));
982 wps->wps_state = hapd->conf->wps_state;
983 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
984 if (is_nil_uuid(hapd->conf->uuid)) {
985 const u8 *uuid;
986 uuid = get_own_uuid(hapd->iface);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700987 if (uuid && !conf->wps_independent) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 os_memcpy(wps->uuid, uuid, UUID_LEN);
989 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
990 "interface", wps->uuid, UUID_LEN);
991 } else {
992 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
993 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
994 "address", wps->uuid, UUID_LEN);
995 }
996 } else {
997 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
998 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
999 wps->uuid, UUID_LEN);
1000 }
1001 wps->ssid_len = hapd->conf->ssid.ssid_len;
1002 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1003 wps->ap = 1;
1004 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1005 wps->dev.device_name = hapd->conf->device_name ?
1006 os_strdup(hapd->conf->device_name) : NULL;
1007 wps->dev.manufacturer = hapd->conf->manufacturer ?
1008 os_strdup(hapd->conf->manufacturer) : NULL;
1009 wps->dev.model_name = hapd->conf->model_name ?
1010 os_strdup(hapd->conf->model_name) : NULL;
1011 wps->dev.model_number = hapd->conf->model_number ?
1012 os_strdup(hapd->conf->model_number) : NULL;
1013 wps->dev.serial_number = hapd->conf->serial_number ?
1014 os_strdup(hapd->conf->serial_number) : NULL;
1015 wps->config_methods =
1016 wps_config_methods_str2bin(hapd->conf->config_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 if ((wps->config_methods &
1018 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1019 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1020 wpa_printf(MSG_INFO, "WPS: Converting display to "
1021 "virtual_display for WPS 2.0 compliance");
1022 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1023 }
1024 if ((wps->config_methods &
1025 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1026 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1027 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1028 "virtual_push_button for WPS 2.0 compliance");
1029 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1032 WPS_DEV_TYPE_LEN);
1033
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001034 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0)
1035 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036
1037 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001038
1039 if (conf->wps_rf_bands) {
1040 wps->dev.rf_bands = conf->wps_rf_bands;
1041 } else {
1042 wps->dev.rf_bands =
1043 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
1044 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
1045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046
1047 if (conf->wpa & WPA_PROTO_RSN) {
1048 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1049 wps->auth_types |= WPS_AUTH_WPA2PSK;
1050 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1051 wps->auth_types |= WPS_AUTH_WPA2;
1052
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001053 if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 wps->encr_types |= WPS_ENCR_AES;
1055 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
1056 wps->encr_types |= WPS_ENCR_TKIP;
1057 }
1058
1059 if (conf->wpa & WPA_PROTO_WPA) {
1060 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1061 wps->auth_types |= WPS_AUTH_WPAPSK;
1062 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1063 wps->auth_types |= WPS_AUTH_WPA;
1064
1065 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
1066 wps->encr_types |= WPS_ENCR_AES;
1067 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
1068 wps->encr_types |= WPS_ENCR_TKIP;
1069 }
1070
1071 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1072 wps->encr_types |= WPS_ENCR_NONE;
1073 wps->auth_types |= WPS_AUTH_OPEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 }
1075
1076 if (conf->ssid.wpa_psk_file) {
1077 /* Use per-device PSKs */
1078 } else if (conf->ssid.wpa_passphrase) {
1079 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1080 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1081 } else if (conf->ssid.wpa_psk) {
1082 wps->network_key = os_malloc(2 * PMK_LEN + 1);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001083 if (wps->network_key == NULL)
1084 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1086 conf->ssid.wpa_psk->psk, PMK_LEN);
1087 wps->network_key_len = 2 * PMK_LEN;
1088 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1089 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001090 if (wps->network_key == NULL)
1091 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1093 conf->ssid.wep.len[0]);
1094 wps->network_key_len = conf->ssid.wep.len[0];
1095 }
1096
1097 if (conf->ssid.wpa_psk) {
1098 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1099 wps->psk_set = 1;
1100 }
1101
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001102 wps->ap_auth_type = wps->auth_types;
1103 wps->ap_encr_type = wps->encr_types;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1105 /* Override parameters to enable security by default */
1106 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1107 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1108 }
1109
1110 wps->ap_settings = conf->ap_settings;
1111 wps->ap_settings_len = conf->ap_settings_len;
1112
1113 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1114 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1115 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1116 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1117 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1118 cfg.cb_ctx = hapd;
1119 cfg.skip_cred_build = conf->skip_cred_build;
1120 cfg.extra_cred = conf->extra_cred;
1121 cfg.extra_cred_len = conf->extra_cred_len;
1122 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1123 conf->skip_cred_build;
1124 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
1125 cfg.static_wep_only = 1;
1126 cfg.dualband = interface_count(hapd->iface) > 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001127 if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1128 (WPS_RF_50GHZ | WPS_RF_24GHZ))
1129 cfg.dualband = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 if (cfg.dualband)
1131 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001132 cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133
1134 wps->registrar = wps_registrar_init(wps, &cfg);
1135 if (wps->registrar == NULL) {
1136 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001137 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 }
1139
1140#ifdef CONFIG_WPS_UPNP
1141 wps->friendly_name = hapd->conf->friendly_name;
1142 wps->manufacturer_url = hapd->conf->manufacturer_url;
1143 wps->model_description = hapd->conf->model_description;
1144 wps->model_url = hapd->conf->model_url;
1145 wps->upc = hapd->conf->upc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146#endif /* CONFIG_WPS_UPNP */
1147
1148 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1149
1150 hapd->wps = wps;
1151
1152 return 0;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001153
1154fail:
1155 hostapd_free_wps(wps);
1156 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157}
1158
1159
Jouni Malinen87fd2792011-05-16 18:35:42 +03001160int hostapd_init_wps_complete(struct hostapd_data *hapd)
1161{
1162 struct wps_context *wps = hapd->wps;
1163
Jouni Malinen75ecf522011-06-27 15:19:46 -07001164 if (wps == NULL)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001165 return 0;
1166
1167#ifdef CONFIG_WPS_UPNP
1168 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1169 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1170 wps_registrar_deinit(wps->registrar);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001171 hostapd_free_wps(wps);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001172 hapd->wps = NULL;
1173 return -1;
1174 }
1175#endif /* CONFIG_WPS_UPNP */
1176
1177 return 0;
1178}
1179
1180
Dmitry Shmidt04949592012-07-19 12:16:46 -07001181static void hostapd_wps_nfc_clear(struct wps_context *wps)
1182{
1183#ifdef CONFIG_WPS_NFC
Dmitry Shmidt54605472013-11-08 11:10:19 -08001184 wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001185 wps->ap_nfc_dev_pw_id = 0;
1186 wpabuf_free(wps->ap_nfc_dh_pubkey);
1187 wps->ap_nfc_dh_pubkey = NULL;
1188 wpabuf_free(wps->ap_nfc_dh_privkey);
1189 wps->ap_nfc_dh_privkey = NULL;
1190 wpabuf_free(wps->ap_nfc_dev_pw);
1191 wps->ap_nfc_dev_pw = NULL;
1192#endif /* CONFIG_WPS_NFC */
1193}
1194
1195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196void hostapd_deinit_wps(struct hostapd_data *hapd)
1197{
1198 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1199 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001200 eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001201 if (hapd->wps == NULL) {
1202 hostapd_wps_clear_ies(hapd, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 return;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001204 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205#ifdef CONFIG_WPS_UPNP
1206 hostapd_wps_upnp_deinit(hapd);
1207#endif /* CONFIG_WPS_UPNP */
1208 wps_registrar_deinit(hapd->wps->registrar);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 wps_free_pending_msgs(hapd->wps->upnp_msgs);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001210 hostapd_free_wps(hapd->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 hapd->wps = NULL;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07001212 hostapd_wps_clear_ies(hapd, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213}
1214
1215
1216void hostapd_update_wps(struct hostapd_data *hapd)
1217{
1218 if (hapd->wps == NULL)
1219 return;
1220
1221#ifdef CONFIG_WPS_UPNP
1222 hapd->wps->friendly_name = hapd->conf->friendly_name;
1223 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1224 hapd->wps->model_description = hapd->conf->model_description;
1225 hapd->wps->model_url = hapd->conf->model_url;
1226 hapd->wps->upc = hapd->conf->upc;
1227#endif /* CONFIG_WPS_UPNP */
1228
1229 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1230
1231 if (hapd->conf->wps_state)
1232 wps_registrar_update_ie(hapd->wps->registrar);
1233 else
1234 hostapd_deinit_wps(hapd);
1235}
1236
1237
1238struct wps_add_pin_data {
1239 const u8 *addr;
1240 const u8 *uuid;
1241 const u8 *pin;
1242 size_t pin_len;
1243 int timeout;
1244 int added;
1245};
1246
1247
1248static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1249{
1250 struct wps_add_pin_data *data = ctx;
1251 int ret;
1252
1253 if (hapd->wps == NULL)
1254 return 0;
1255 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1256 data->uuid, data->pin, data->pin_len,
1257 data->timeout);
1258 if (ret == 0)
1259 data->added++;
1260 return ret;
1261}
1262
1263
1264int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1265 const char *uuid, const char *pin, int timeout)
1266{
1267 u8 u[UUID_LEN];
1268 struct wps_add_pin_data data;
1269
1270 data.addr = addr;
1271 data.uuid = u;
1272 data.pin = (const u8 *) pin;
1273 data.pin_len = os_strlen(pin);
1274 data.timeout = timeout;
1275 data.added = 0;
1276
1277 if (os_strcmp(uuid, "any") == 0)
1278 data.uuid = NULL;
1279 else {
1280 if (uuid_str2bin(uuid, u))
1281 return -1;
1282 data.uuid = u;
1283 }
1284 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1285 return -1;
1286 return data.added ? 0 : -1;
1287}
1288
1289
1290static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1291{
1292 const u8 *p2p_dev_addr = ctx;
1293 if (hapd->wps == NULL)
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001294 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1296}
1297
1298
1299int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1300 const u8 *p2p_dev_addr)
1301{
1302 return hostapd_wps_for_each(hapd, wps_button_pushed,
1303 (void *) p2p_dev_addr);
1304}
1305
1306
Dmitry Shmidt04949592012-07-19 12:16:46 -07001307static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1308{
1309 if (hapd->wps == NULL)
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001310 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001311
1312 wps_registrar_wps_cancel(hapd->wps->registrar);
1313 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1314
1315 return 0;
1316}
1317
1318
1319int hostapd_wps_cancel(struct hostapd_data *hapd)
1320{
1321 return hostapd_wps_for_each(hapd, wps_cancel, NULL);
1322}
1323
1324
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001325static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1326 const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001327 const u8 *ie, size_t ie_len,
1328 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329{
1330 struct hostapd_data *hapd = ctx;
1331 struct wpabuf *wps_ie;
1332 struct ieee802_11_elems elems;
1333
1334 if (hapd->wps == NULL)
1335 return 0;
1336
1337 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1338 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1339 MACSTR, MAC2STR(addr));
1340 return 0;
1341 }
1342
1343 if (elems.ssid && elems.ssid_len > 0 &&
1344 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1345 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1346 0))
1347 return 0; /* Not for us */
1348
1349 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1350 if (wps_ie == NULL)
1351 return 0;
1352 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1353 wpabuf_free(wps_ie);
1354 return 0;
1355 }
1356
1357 if (wpabuf_len(wps_ie) > 0) {
1358 int p2p_wildcard = 0;
1359#ifdef CONFIG_P2P
1360 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1361 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1362 P2P_WILDCARD_SSID_LEN) == 0)
1363 p2p_wildcard = 1;
1364#endif /* CONFIG_P2P */
1365 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1366 p2p_wildcard);
1367#ifdef CONFIG_WPS_UPNP
1368 /* FIX: what exactly should be included in the WLANEvent?
1369 * WPS attributes? Full ProbeReq frame? */
1370 if (!p2p_wildcard)
1371 upnp_wps_device_send_wlan_event(
1372 hapd->wps_upnp, addr,
1373 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1374#endif /* CONFIG_WPS_UPNP */
1375 }
1376
1377 wpabuf_free(wps_ie);
1378
1379 return 0;
1380}
1381
1382
1383#ifdef CONFIG_WPS_UPNP
1384
1385static int hostapd_rx_req_put_wlan_response(
1386 void *priv, enum upnp_wps_wlanevent_type ev_type,
1387 const u8 *mac_addr, const struct wpabuf *msg,
1388 enum wps_msg_type msg_type)
1389{
1390 struct hostapd_data *hapd = priv;
1391 struct sta_info *sta;
1392 struct upnp_pending_message *p;
1393
1394 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1395 MACSTR, ev_type, MAC2STR(mac_addr));
1396 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1397 wpabuf_head(msg), wpabuf_len(msg));
1398 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1399 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1400 "PutWLANResponse WLANEventType %d", ev_type);
1401 return -1;
1402 }
1403
1404 /*
1405 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1406 * server implementation for delivery to the peer.
1407 */
1408
1409 sta = ap_get_sta(hapd, mac_addr);
1410#ifndef CONFIG_WPS_STRICT
1411 if (!sta) {
1412 /*
1413 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1414 * Pick STA that is in an ongoing WPS registration without
1415 * checking the MAC address.
1416 */
1417 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1418 "on NewWLANEventMAC; try wildcard match");
1419 for (sta = hapd->sta_list; sta; sta = sta->next) {
1420 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1421 break;
1422 }
1423 }
1424#endif /* CONFIG_WPS_STRICT */
1425
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001426 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1428 return 0;
1429 }
1430
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001431 if (!sta->eapol_sm) {
1432 /*
1433 * This can happen, e.g., if an ER sends an extra message after
1434 * the station has disassociated (but not fully
1435 * deauthenticated).
1436 */
1437 wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
1438 return 0;
1439 }
1440
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 p = os_zalloc(sizeof(*p));
1442 if (p == NULL)
1443 return -1;
1444 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1445 p->msg = wpabuf_dup(msg);
1446 p->type = msg_type;
1447 p->next = hapd->wps->upnp_msgs;
1448 hapd->wps->upnp_msgs = p;
1449
1450 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1451}
1452
1453
1454static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1455 struct wps_context *wps)
1456{
1457 struct upnp_wps_device_ctx *ctx;
1458
1459 if (!hapd->conf->upnp_iface)
1460 return 0;
1461 ctx = os_zalloc(sizeof(*ctx));
1462 if (ctx == NULL)
1463 return -1;
1464
1465 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1466 if (hapd->conf->ap_pin)
1467 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1468
1469 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1470 hapd->conf->upnp_iface);
1471 if (hapd->wps_upnp == NULL)
1472 return -1;
1473 wps->wps_upnp = hapd->wps_upnp;
1474
1475 return 0;
1476}
1477
1478
1479static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1480{
1481 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1482}
1483
1484#endif /* CONFIG_WPS_UPNP */
1485
1486
1487int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1488 char *buf, size_t buflen)
1489{
1490 if (hapd->wps == NULL)
1491 return 0;
1492 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1493}
1494
1495
1496static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1497{
1498 struct hostapd_data *hapd = eloop_data;
1499 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1500 hostapd_wps_ap_pin_disable(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001501 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502}
1503
1504
1505static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1506{
1507 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1508 hapd->ap_pin_failures = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001509 hapd->ap_pin_failures_consecutive = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 hapd->conf->ap_setup_locked = 0;
1511 if (hapd->wps->ap_setup_locked) {
1512 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1513 hapd->wps->ap_setup_locked = 0;
1514 wps_registrar_update_ie(hapd->wps->registrar);
1515 }
1516 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1517 if (timeout > 0)
1518 eloop_register_timeout(timeout, 0,
1519 hostapd_wps_ap_pin_timeout, hapd, NULL);
1520}
1521
1522
1523static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1524{
1525 os_free(hapd->conf->ap_pin);
1526 hapd->conf->ap_pin = NULL;
1527#ifdef CONFIG_WPS_UPNP
1528 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1529#endif /* CONFIG_WPS_UPNP */
1530 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1531 return 0;
1532}
1533
1534
1535void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1536{
1537 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1538 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1539}
1540
1541
1542struct wps_ap_pin_data {
1543 char pin_txt[9];
1544 int timeout;
1545};
1546
1547
1548static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1549{
1550 struct wps_ap_pin_data *data = ctx;
1551 os_free(hapd->conf->ap_pin);
1552 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1553#ifdef CONFIG_WPS_UPNP
1554 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1555#endif /* CONFIG_WPS_UPNP */
1556 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1557 return 0;
1558}
1559
1560
1561const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1562{
1563 unsigned int pin;
1564 struct wps_ap_pin_data data;
1565
1566 pin = wps_generate_pin();
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001567 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 data.timeout = timeout;
1569 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1570 return hapd->conf->ap_pin;
1571}
1572
1573
1574const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1575{
1576 return hapd->conf->ap_pin;
1577}
1578
1579
1580int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1581 int timeout)
1582{
1583 struct wps_ap_pin_data data;
1584 int ret;
1585
1586 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001587 if (os_snprintf_error(sizeof(data.pin_txt), ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588 return -1;
1589 data.timeout = timeout;
1590 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1591}
1592
1593
1594static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1595{
1596 if (hapd->wps)
1597 wps_registrar_update_ie(hapd->wps->registrar);
1598 return 0;
1599}
1600
1601
1602void hostapd_wps_update_ie(struct hostapd_data *hapd)
1603{
1604 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1605}
1606
1607
1608int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1609 const char *auth, const char *encr, const char *key)
1610{
1611 struct wps_credential cred;
1612 size_t len;
1613
1614 os_memset(&cred, 0, sizeof(cred));
1615
1616 len = os_strlen(ssid);
1617 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1618 hexstr2bin(ssid, cred.ssid, len / 2))
1619 return -1;
1620 cred.ssid_len = len / 2;
1621
1622 if (os_strncmp(auth, "OPEN", 4) == 0)
1623 cred.auth_type = WPS_AUTH_OPEN;
1624 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1625 cred.auth_type = WPS_AUTH_WPAPSK;
1626 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1627 cred.auth_type = WPS_AUTH_WPA2PSK;
1628 else
1629 return -1;
1630
1631 if (encr) {
1632 if (os_strncmp(encr, "NONE", 4) == 0)
1633 cred.encr_type = WPS_ENCR_NONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 else if (os_strncmp(encr, "TKIP", 4) == 0)
1635 cred.encr_type = WPS_ENCR_TKIP;
1636 else if (os_strncmp(encr, "CCMP", 4) == 0)
1637 cred.encr_type = WPS_ENCR_AES;
1638 else
1639 return -1;
1640 } else
1641 cred.encr_type = WPS_ENCR_NONE;
1642
1643 if (key) {
1644 len = os_strlen(key);
1645 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1646 hexstr2bin(key, cred.key, len / 2))
1647 return -1;
1648 cred.key_len = len / 2;
1649 }
1650
1651 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1652}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001653
1654
1655#ifdef CONFIG_WPS_NFC
1656
1657struct wps_nfc_password_token_data {
1658 const u8 *oob_dev_pw;
1659 size_t oob_dev_pw_len;
1660 int added;
1661};
1662
1663
1664static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1665{
1666 struct wps_nfc_password_token_data *data = ctx;
1667 int ret;
1668
1669 if (hapd->wps == NULL)
1670 return 0;
1671 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1672 data->oob_dev_pw,
1673 data->oob_dev_pw_len);
1674 if (ret == 0)
1675 data->added++;
1676 return ret;
1677}
1678
1679
1680static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1681 struct wps_parse_attr *attr)
1682{
1683 struct wps_nfc_password_token_data data;
1684
1685 data.oob_dev_pw = attr->oob_dev_password;
1686 data.oob_dev_pw_len = attr->oob_dev_password_len;
1687 data.added = 0;
1688 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1689 return -1;
1690 return data.added ? 0 : -1;
1691}
1692
1693
1694static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1695 const struct wpabuf *wps)
1696{
1697 struct wps_parse_attr attr;
1698
1699 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1700
1701 if (wps_parse_msg(wps, &attr)) {
1702 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1703 return -1;
1704 }
1705
1706 if (attr.oob_dev_password)
1707 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1708
1709 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1710 return -1;
1711}
1712
1713
1714int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1715 const struct wpabuf *data)
1716{
1717 const struct wpabuf *wps = data;
1718 struct wpabuf *tmp = NULL;
1719 int ret;
1720
1721 if (wpabuf_len(data) < 4)
1722 return -1;
1723
1724 if (*wpabuf_head_u8(data) != 0x10) {
1725 /* Assume this contains full NDEF record */
1726 tmp = ndef_parse_wifi(data);
1727 if (tmp == NULL) {
1728 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1729 return -1;
1730 }
1731 wps = tmp;
1732 }
1733
1734 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1735 wpabuf_free(tmp);
1736 return ret;
1737}
1738
1739
1740struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1741 int ndef)
1742{
1743 struct wpabuf *ret;
1744
1745 if (hapd->wps == NULL)
1746 return NULL;
1747
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001748 ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
1749 hapd->iconf->channel);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001750 if (ndef && ret) {
1751 struct wpabuf *tmp;
1752 tmp = ndef_build_wifi(ret);
1753 wpabuf_free(ret);
1754 if (tmp == NULL)
1755 return NULL;
1756 ret = tmp;
1757 }
1758
1759 return ret;
1760}
1761
1762
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001763struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
1764{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001765 struct wpabuf *ret;
1766
1767 if (hapd->wps == NULL)
1768 return NULL;
1769
1770 if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
1771 struct wps_context *wps = hapd->wps;
1772 if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
1773 &hapd->conf->wps_nfc_dh_privkey) < 0)
1774 return NULL;
1775 hostapd_wps_nfc_clear(wps);
1776 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
1777 wps->ap_nfc_dh_pubkey =
1778 wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1779 wps->ap_nfc_dh_privkey =
1780 wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1781 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
1782 hostapd_wps_nfc_clear(wps);
1783 return NULL;
1784 }
1785 }
1786
1787 ret = wps_build_nfc_handover_sel(hapd->wps,
1788 hapd->conf->wps_nfc_dh_pubkey,
1789 hapd->own_addr, hapd->iface->freq);
1790
1791 if (ndef && ret) {
1792 struct wpabuf *tmp;
1793 tmp = ndef_build_wifi(ret);
1794 wpabuf_free(ret);
1795 if (tmp == NULL)
1796 return NULL;
1797 ret = tmp;
1798 }
1799
1800 return ret;
1801}
1802
1803
1804int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
1805 const struct wpabuf *req,
1806 const struct wpabuf *sel)
1807{
1808 struct wpabuf *wps;
1809 int ret = -1;
1810 u16 wsc_len;
1811 const u8 *pos;
1812 struct wpabuf msg;
1813 struct wps_parse_attr attr;
1814 u16 dev_pw_id;
1815
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001816 /*
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001817 * Enrollee/station is always initiator of the NFC connection handover,
1818 * so use the request message here to find Enrollee public key hash.
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001819 */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001820 wps = ndef_parse_wifi(req);
1821 if (wps == NULL)
1822 return -1;
1823 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
1824 "payload from NFC connection handover");
1825 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
1826 if (wpabuf_len(wps) < 2) {
1827 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
1828 "Message");
1829 goto out;
1830 }
1831 pos = wpabuf_head(wps);
1832 wsc_len = WPA_GET_BE16(pos);
1833 if (wsc_len > wpabuf_len(wps) - 2) {
1834 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
1835 "in rt Wi-Fi Handover Request Message", wsc_len);
1836 goto out;
1837 }
1838 pos += 2;
1839
1840 wpa_hexdump(MSG_DEBUG,
1841 "WPS: WSC attributes in Wi-Fi Handover Request Message",
1842 pos, wsc_len);
1843 if (wsc_len < wpabuf_len(wps) - 2) {
1844 wpa_hexdump(MSG_DEBUG,
1845 "WPS: Ignore extra data after WSC attributes",
1846 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
1847 }
1848
1849 wpabuf_set(&msg, pos, wsc_len);
1850 ret = wps_parse_msg(&msg, &attr);
1851 if (ret < 0) {
1852 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
1853 "Wi-Fi Handover Request Message");
1854 goto out;
1855 }
1856
1857 if (attr.oob_dev_password == NULL ||
1858 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
1859 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
1860 "included in Wi-Fi Handover Request Message");
1861 ret = -1;
1862 goto out;
1863 }
1864
1865 if (attr.uuid_e == NULL) {
1866 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
1867 "Handover Request Message");
1868 ret = -1;
1869 goto out;
1870 }
1871
1872 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
1873
1874 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
1875 attr.oob_dev_password, attr.oob_dev_password_len);
1876 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
1877 WPS_OOB_PUBKEY_HASH_LEN);
1878 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
1879 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
1880 "%u in Wi-Fi Handover Request Message", dev_pw_id);
1881 ret = -1;
1882 goto out;
1883 }
1884 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
1885 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
1886
1887 ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
1888 attr.oob_dev_password,
1889 DEV_PW_NFC_CONNECTION_HANDOVER,
1890 NULL, 0, 1);
1891
1892out:
1893 wpabuf_free(wps);
1894 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001895}
1896
1897
Dmitry Shmidt04949592012-07-19 12:16:46 -07001898struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1899{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001900 if (hapd->conf->wps_nfc_pw_from_config) {
1901 return wps_nfc_token_build(ndef,
1902 hapd->conf->wps_nfc_dev_pw_id,
1903 hapd->conf->wps_nfc_dh_pubkey,
1904 hapd->conf->wps_nfc_dev_pw);
1905 }
1906
Dmitry Shmidt04949592012-07-19 12:16:46 -07001907 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1908 &hapd->conf->wps_nfc_dh_pubkey,
1909 &hapd->conf->wps_nfc_dh_privkey,
1910 &hapd->conf->wps_nfc_dev_pw);
1911}
1912
1913
1914int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1915{
1916 struct wps_context *wps = hapd->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001917 struct wpabuf *pw;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001918
1919 if (wps == NULL)
1920 return -1;
1921
1922 if (!hapd->conf->wps_nfc_dh_pubkey ||
1923 !hapd->conf->wps_nfc_dh_privkey ||
1924 !hapd->conf->wps_nfc_dev_pw ||
1925 !hapd->conf->wps_nfc_dev_pw_id)
1926 return -1;
1927
1928 hostapd_wps_nfc_clear(wps);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001929 wpa_printf(MSG_DEBUG,
1930 "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
1931 hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001932 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1933 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1934 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001935 pw = hapd->conf->wps_nfc_dev_pw;
1936 wps->ap_nfc_dev_pw = wpabuf_alloc(
1937 wpabuf_len(pw) * 2 + 1);
1938 if (wps->ap_nfc_dev_pw) {
1939 wpa_snprintf_hex_uppercase(
1940 (char *) wpabuf_put(wps->ap_nfc_dev_pw,
1941 wpabuf_len(pw) * 2),
1942 wpabuf_len(pw) * 2 + 1,
1943 wpabuf_head(pw), wpabuf_len(pw));
1944 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001945
1946 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
1947 !wps->ap_nfc_dev_pw) {
1948 hostapd_wps_nfc_clear(wps);
1949 return -1;
1950 }
1951
1952 return 0;
1953}
1954
1955
1956void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
1957{
Dmitry Shmidt54605472013-11-08 11:10:19 -08001958 wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
1959 hapd->conf->iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001960 hostapd_wps_nfc_clear(hapd->wps);
1961}
1962
1963#endif /* CONFIG_WPS_NFC */