blob: 3a40125b8627fa54b659ea3a07d96a584da22909 [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);
43
44
45struct wps_for_each_data {
46 int (*func)(struct hostapd_data *h, void *ctx);
47 void *ctx;
Dmitry Shmidt444d5672013-04-01 13:08:44 -070048 struct hostapd_data *calling_hapd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049};
50
51
52static int wps_for_each(struct hostapd_iface *iface, void *ctx)
53{
54 struct wps_for_each_data *data = ctx;
55 size_t j;
56
57 if (iface == NULL)
58 return 0;
59 for (j = 0; j < iface->num_bss; j++) {
60 struct hostapd_data *hapd = iface->bss[j];
Dmitry Shmidt444d5672013-04-01 13:08:44 -070061 int ret;
62
63 if (hapd != data->calling_hapd &&
64 (hapd->conf->wps_independent ||
65 data->calling_hapd->conf->wps_independent))
66 continue;
67
68 ret = data->func(hapd, data->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069 if (ret)
70 return ret;
71 }
72
73 return 0;
74}
75
76
77static int hostapd_wps_for_each(struct hostapd_data *hapd,
78 int (*func)(struct hostapd_data *h, void *ctx),
79 void *ctx)
80{
81 struct hostapd_iface *iface = hapd->iface;
82 struct wps_for_each_data data;
83 data.func = func;
84 data.ctx = ctx;
Dmitry Shmidt444d5672013-04-01 13:08:44 -070085 data.calling_hapd = hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086 if (iface->interfaces == NULL ||
87 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088 return wps_for_each(iface, &data);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070089 return iface->interfaces->for_each_interface(iface->interfaces,
90 wps_for_each, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091}
92
93
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070094static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
95 const u8 *p2p_dev_addr, const u8 *psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096 size_t psk_len)
97{
98 struct hostapd_data *hapd = ctx;
99 struct hostapd_wpa_psk *p;
100 struct hostapd_ssid *ssid = &hapd->conf->ssid;
101
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700102 if (is_zero_ether_addr(p2p_dev_addr)) {
103 wpa_printf(MSG_DEBUG,
104 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
105 MAC2STR(mac_addr));
106 } else {
107 wpa_printf(MSG_DEBUG,
108 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
109 " P2P Device Addr " MACSTR,
110 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
113
114 if (psk_len != PMK_LEN) {
115 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
116 (unsigned long) psk_len);
117 return -1;
118 }
119
120 /* Add the new PSK to runtime PSK list */
121 p = os_zalloc(sizeof(*p));
122 if (p == NULL)
123 return -1;
124 os_memcpy(p->addr, mac_addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700125 os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 os_memcpy(p->psk, psk, PMK_LEN);
127
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700128 if (hapd->new_psk_cb) {
129 hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
130 psk, psk_len);
131 }
132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 p->next = ssid->wpa_psk;
134 ssid->wpa_psk = p;
135
136 if (ssid->wpa_psk_file) {
137 FILE *f;
138 char hex[PMK_LEN * 2 + 1];
139 /* Add the new PSK to PSK list file */
140 f = fopen(ssid->wpa_psk_file, "a");
141 if (f == NULL) {
142 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
143 "'%s'", ssid->wpa_psk_file);
144 return -1;
145 }
146
147 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
148 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
149 fclose(f);
150 }
151
152 return 0;
153}
154
155
156static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
157 struct wpabuf *probe_resp_ie)
158{
159 struct hostapd_data *hapd = ctx;
160 wpabuf_free(hapd->wps_beacon_ie);
161 hapd->wps_beacon_ie = beacon_ie;
162 wpabuf_free(hapd->wps_probe_resp_ie);
163 hapd->wps_probe_resp_ie = probe_resp_ie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800164 if (hapd->beacon_set_done)
165 ieee802_11_set_beacon(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166 return hostapd_set_ap_wps_ie(hapd);
167}
168
169
170static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
171 const struct wps_device_data *dev)
172{
173 struct hostapd_data *hapd = ctx;
174 char uuid[40], txt[400];
175 int len;
176 char devtype[WPS_DEV_TYPE_BUFSIZE];
177 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
178 return;
179 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
180 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
181 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
182 uuid, MAC2STR(dev->mac_addr), dev->device_name,
183 dev->manufacturer, dev->model_name,
184 dev->model_number, dev->serial_number,
185 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
186 sizeof(devtype)));
187 if (len > 0 && len < (int) sizeof(txt))
188 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
189
190 if (hapd->conf->wps_pin_requests) {
191 FILE *f;
192 struct os_time t;
193 f = fopen(hapd->conf->wps_pin_requests, "a");
194 if (f == NULL)
195 return;
196 os_get_time(&t);
197 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
198 "\t%s\n",
199 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
200 dev->manufacturer, dev->model_name, dev->model_number,
201 dev->serial_number,
202 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
203 sizeof(devtype)));
204 fclose(f);
205 }
206}
207
208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209struct wps_stop_reg_data {
210 struct hostapd_data *current_hapd;
211 const u8 *uuid_e;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700212 const u8 *dev_pw;
213 size_t dev_pw_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800214};
215
216static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
217{
218 struct wps_stop_reg_data *data = ctx;
219 if (hapd != data->current_hapd && hapd->wps != NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700220 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
221 data->dev_pw, data->dev_pw_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800222 return 0;
223}
224
225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700227 const u8 *uuid_e, const u8 *dev_pw,
228 size_t dev_pw_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229{
230 struct hostapd_data *hapd = ctx;
231 char uuid[40];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800232 struct wps_stop_reg_data data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
234 return;
235 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
236 MAC2STR(mac_addr), uuid);
237 if (hapd->wps_reg_success_cb)
238 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
239 mac_addr, uuid_e);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240 data.current_hapd = hapd;
241 data.uuid_e = uuid_e;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700242 data.dev_pw = dev_pw;
243 data.dev_pw_len = dev_pw_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244 hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245}
246
247
248static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
249 const u8 *uuid_e,
250 const u8 *pri_dev_type,
251 u16 config_methods,
252 u16 dev_password_id, u8 request_type,
253 const char *dev_name)
254{
255 struct hostapd_data *hapd = ctx;
256 char uuid[40];
257 char devtype[WPS_DEV_TYPE_BUFSIZE];
258 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
259 return;
260 if (dev_name == NULL)
261 dev_name = "";
262 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
263 " %s %s 0x%x %u %u [%s]",
264 MAC2STR(addr), uuid,
265 wps_dev_type_bin2str(pri_dev_type, devtype,
266 sizeof(devtype)),
267 config_methods, dev_password_id, request_type, dev_name);
268}
269
270
271static int str_starts(const char *str, const char *start)
272{
273 return os_strncmp(str, start, os_strlen(start)) == 0;
274}
275
276
277static void wps_reload_config(void *eloop_data, void *user_ctx)
278{
279 struct hostapd_iface *iface = eloop_data;
280
281 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700282 if (iface->interfaces == NULL ||
283 iface->interfaces->reload_config(iface) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
285 "configuration");
286 }
287}
288
289
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800290void hostapd_wps_eap_completed(struct hostapd_data *hapd)
291{
292 /*
293 * Reduce race condition of the station trying to reconnect immediately
294 * after AP reconfiguration through WPS by rescheduling the reload
295 * timeout to happen after EAP completion rather than the originally
296 * scheduled 100 ms after new configuration became known.
297 */
298 if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
299 1)
300 wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
301}
302
303
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800304static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
305 size_t attr_len)
306{
307 size_t blen = attr_len * 2 + 1;
308 char *buf = os_malloc(blen);
309 if (buf) {
310 wpa_snprintf_hex(buf, blen, attr, attr_len);
311 wpa_msg(hapd->msg_ctx, MSG_INFO,
312 WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
313 os_free(buf);
314 }
315}
316
317
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700318static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
319 const struct wps_credential *cred)
320{
321 struct hostapd_bss_config *bss = hapd->conf;
322
323 wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
324
325 bss->wps_state = 2;
326 if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
327 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
328 bss->ssid.ssid_len = cred->ssid_len;
329 bss->ssid.ssid_set = 1;
330 }
331
332 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
333 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
334 bss->wpa = 3;
335 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
336 bss->wpa = 2;
337 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
338 bss->wpa = 1;
339 else
340 bss->wpa = 0;
341
342 if (bss->wpa) {
343 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
344 bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
345 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
346 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
347
348 bss->wpa_pairwise = 0;
349 if (cred->encr_type & WPS_ENCR_AES)
350 bss->wpa_pairwise |= WPA_CIPHER_CCMP;
351 if (cred->encr_type & WPS_ENCR_TKIP)
352 bss->wpa_pairwise |= WPA_CIPHER_TKIP;
353 bss->rsn_pairwise = bss->wpa_pairwise;
354 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
355 bss->wpa_pairwise,
356 bss->rsn_pairwise);
357
358 if (cred->key_len >= 8 && cred->key_len < 64) {
359 os_free(bss->ssid.wpa_passphrase);
360 bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
361 if (bss->ssid.wpa_passphrase)
362 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
363 cred->key_len);
364 os_free(bss->ssid.wpa_psk);
365 bss->ssid.wpa_psk = NULL;
366 } else if (cred->key_len == 64) {
367 os_free(bss->ssid.wpa_psk);
368 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 {
380 if ((cred->auth_type & WPS_AUTH_OPEN) &&
381 (cred->auth_type & WPS_AUTH_SHARED))
382 bss->auth_algs = 3;
383 else if (cred->auth_type & WPS_AUTH_SHARED)
384 bss->auth_algs = 2;
385 else
386 bss->auth_algs = 1;
387 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx > 0 &&
388 cred->key_idx <= 4) {
389 struct hostapd_wep_keys *wep = &bss->ssid.wep;
390 int idx = cred->key_idx;
391 if (idx)
392 idx--;
393 wep->idx = idx;
394 if (cred->key_len == 10 || cred->key_len == 26) {
395 os_free(wep->key[idx]);
396 wep->key[idx] = os_malloc(cred->key_len / 2);
397 if (wep->key[idx] == NULL ||
398 hexstr2bin((const char *) cred->key,
399 wep->key[idx],
400 cred->key_len / 2))
401 return -1;
402 wep->len[idx] = cred->key_len / 2;
403 } else {
404 os_free(wep->key[idx]);
405 wep->key[idx] = os_malloc(cred->key_len);
406 if (wep->key[idx] == NULL)
407 return -1;
408 os_memcpy(wep->key[idx], cred->key,
409 cred->key_len);
410 wep->len[idx] = cred->key_len;
411 }
412 wep->keys_set = 1;
413 }
414 }
415
416 /* Schedule configuration reload after short period of time to allow
417 * EAP-WSC to be finished.
418 */
419 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
420 NULL);
421
422 return 0;
423}
424
425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
427{
428 const struct wps_credential *cred = ctx;
429 FILE *oconf, *nconf;
430 size_t len, i;
431 char *tmp_fname;
432 char buf[1024];
433 int multi_bss;
434 int wpa;
435
436 if (hapd->wps == NULL)
437 return 0;
438
439 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
440 cred->cred_attr, cred->cred_attr_len);
441
442 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
443 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
444 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
445 cred->auth_type);
446 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
447 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
448 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
449 cred->key, cred->key_len);
450 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
451 MAC2STR(cred->mac_addr));
452
453 if ((hapd->conf->wps_cred_processing == 1 ||
454 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800455 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
456 } else if (hapd->conf->wps_cred_processing == 1 ||
457 hapd->conf->wps_cred_processing == 2) {
458 struct wpabuf *attr;
459 attr = wpabuf_alloc(200);
460 if (attr && wps_build_credential_wrap(attr, cred) == 0)
461 hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
462 wpabuf_len(attr));
463 wpabuf_free(attr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 } else
465 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
466
467 if (hapd->conf->wps_cred_processing == 1)
468 return 0;
469
470 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
471 hapd->wps->ssid_len = cred->ssid_len;
472 hapd->wps->encr_types = cred->encr_type;
473 hapd->wps->auth_types = cred->auth_type;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800474 hapd->wps->ap_encr_type = cred->encr_type;
475 hapd->wps->ap_auth_type = cred->auth_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 if (cred->key_len == 0) {
477 os_free(hapd->wps->network_key);
478 hapd->wps->network_key = NULL;
479 hapd->wps->network_key_len = 0;
480 } else {
481 if (hapd->wps->network_key == NULL ||
482 hapd->wps->network_key_len < cred->key_len) {
483 hapd->wps->network_key_len = 0;
484 os_free(hapd->wps->network_key);
485 hapd->wps->network_key = os_malloc(cred->key_len);
486 if (hapd->wps->network_key == NULL)
487 return -1;
488 }
489 hapd->wps->network_key_len = cred->key_len;
490 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
491 }
492 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
493
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700494 if (hapd->iface->config_fname == NULL)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700495 return hapd_wps_reconfig_in_memory(hapd, cred);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 len = os_strlen(hapd->iface->config_fname) + 5;
497 tmp_fname = os_malloc(len);
498 if (tmp_fname == NULL)
499 return -1;
500 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
501
502 oconf = fopen(hapd->iface->config_fname, "r");
503 if (oconf == NULL) {
504 wpa_printf(MSG_WARNING, "WPS: Could not open current "
505 "configuration file");
506 os_free(tmp_fname);
507 return -1;
508 }
509
510 nconf = fopen(tmp_fname, "w");
511 if (nconf == NULL) {
512 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
513 "configuration file");
514 os_free(tmp_fname);
515 fclose(oconf);
516 return -1;
517 }
518
519 fprintf(nconf, "# WPS configuration - START\n");
520
521 fprintf(nconf, "wps_state=2\n");
522
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700523 if (is_hex(cred->ssid, cred->ssid_len)) {
524 fprintf(nconf, "ssid2=");
525 for (i = 0; i < cred->ssid_len; i++)
526 fprintf(nconf, "%02x", cred->ssid[i]);
527 fprintf(nconf, "\n");
528 } else {
529 fprintf(nconf, "ssid=");
530 for (i = 0; i < cred->ssid_len; i++)
531 fputc(cred->ssid[i], nconf);
532 fprintf(nconf, "\n");
533 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534
535 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
536 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
537 wpa = 3;
538 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
539 wpa = 2;
540 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
541 wpa = 1;
542 else
543 wpa = 0;
544
545 if (wpa) {
546 char *prefix;
547 fprintf(nconf, "wpa=%d\n", wpa);
548
549 fprintf(nconf, "wpa_key_mgmt=");
550 prefix = "";
551 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
552 fprintf(nconf, "WPA-EAP");
553 prefix = " ";
554 }
555 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
556 fprintf(nconf, "%sWPA-PSK", prefix);
557 fprintf(nconf, "\n");
558
559 fprintf(nconf, "wpa_pairwise=");
560 prefix = "";
561 if (cred->encr_type & WPS_ENCR_AES) {
562 fprintf(nconf, "CCMP");
563 prefix = " ";
564 }
565 if (cred->encr_type & WPS_ENCR_TKIP) {
566 fprintf(nconf, "%sTKIP", prefix);
567 }
568 fprintf(nconf, "\n");
569
570 if (cred->key_len >= 8 && cred->key_len < 64) {
571 fprintf(nconf, "wpa_passphrase=");
572 for (i = 0; i < cred->key_len; i++)
573 fputc(cred->key[i], nconf);
574 fprintf(nconf, "\n");
575 } else if (cred->key_len == 64) {
576 fprintf(nconf, "wpa_psk=");
577 for (i = 0; i < cred->key_len; i++)
578 fputc(cred->key[i], nconf);
579 fprintf(nconf, "\n");
580 } else {
581 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
582 "for WPA/WPA2",
583 (unsigned long) cred->key_len);
584 }
585
586 fprintf(nconf, "auth_algs=1\n");
587 } else {
588 if ((cred->auth_type & WPS_AUTH_OPEN) &&
589 (cred->auth_type & WPS_AUTH_SHARED))
590 fprintf(nconf, "auth_algs=3\n");
591 else if (cred->auth_type & WPS_AUTH_SHARED)
592 fprintf(nconf, "auth_algs=2\n");
593 else
594 fprintf(nconf, "auth_algs=1\n");
595
596 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
597 int key_idx = cred->key_idx;
598 if (key_idx)
599 key_idx--;
600 fprintf(nconf, "wep_default_key=%d\n", key_idx);
601 fprintf(nconf, "wep_key%d=", key_idx);
602 if (cred->key_len == 10 || cred->key_len == 26) {
603 /* WEP key as a hex string */
604 for (i = 0; i < cred->key_len; i++)
605 fputc(cred->key[i], nconf);
606 } else {
607 /* Raw WEP key; convert to hex */
608 for (i = 0; i < cred->key_len; i++)
609 fprintf(nconf, "%02x", cred->key[i]);
610 }
611 fprintf(nconf, "\n");
612 }
613 }
614
615 fprintf(nconf, "# WPS configuration - END\n");
616
617 multi_bss = 0;
618 while (fgets(buf, sizeof(buf), oconf)) {
619 if (os_strncmp(buf, "bss=", 4) == 0)
620 multi_bss = 1;
621 if (!multi_bss &&
622 (str_starts(buf, "ssid=") ||
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700623 str_starts(buf, "ssid2=") ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624 str_starts(buf, "auth_algs=") ||
625 str_starts(buf, "wep_default_key=") ||
626 str_starts(buf, "wep_key") ||
627 str_starts(buf, "wps_state=") ||
628 str_starts(buf, "wpa=") ||
629 str_starts(buf, "wpa_psk=") ||
630 str_starts(buf, "wpa_pairwise=") ||
631 str_starts(buf, "rsn_pairwise=") ||
632 str_starts(buf, "wpa_key_mgmt=") ||
633 str_starts(buf, "wpa_passphrase="))) {
634 fprintf(nconf, "#WPS# %s", buf);
635 } else
636 fprintf(nconf, "%s", buf);
637 }
638
639 fclose(nconf);
640 fclose(oconf);
641
642 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
643 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
644 "configuration file: %s", strerror(errno));
645 os_free(tmp_fname);
646 return -1;
647 }
648
649 os_free(tmp_fname);
650
651 /* Schedule configuration reload after short period of time to allow
652 * EAP-WSC to be finished.
653 */
654 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
655 NULL);
656
657 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
658
659 return 0;
660}
661
662
663static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
664{
665 struct hostapd_data *hapd = ctx;
666 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
667}
668
669
670static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
671{
672 struct hostapd_data *hapd = eloop_data;
673
674 if (hapd->conf->ap_setup_locked)
675 return;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800676 if (hapd->ap_pin_failures_consecutive >= 10)
677 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678
679 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
680 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
681 hapd->wps->ap_setup_locked = 0;
682 wps_registrar_update_ie(hapd->wps->registrar);
683}
684
685
686static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
687{
688 struct wps_event_pwd_auth_fail *data = ctx;
689
690 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
691 return 0;
692
693 /*
694 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
695 * for some time if this happens multiple times to slow down brute
696 * force attacks.
697 */
698 hapd->ap_pin_failures++;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800699 hapd->ap_pin_failures_consecutive++;
700 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
701 "(%u consecutive)",
702 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 if (hapd->ap_pin_failures < 3)
704 return 0;
705
706 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
707 hapd->wps->ap_setup_locked = 1;
708
709 wps_registrar_update_ie(hapd->wps->registrar);
710
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800711 if (!hapd->conf->ap_setup_locked &&
712 hapd->ap_pin_failures_consecutive >= 10) {
713 /*
714 * In indefinite lockdown - disable automatic AP PIN
715 * reenablement.
716 */
717 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
718 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
719 } else if (!hapd->conf->ap_setup_locked) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 if (hapd->ap_pin_lockout_time == 0)
721 hapd->ap_pin_lockout_time = 60;
722 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
723 (hapd->ap_pin_failures % 3) == 0)
724 hapd->ap_pin_lockout_time *= 2;
725
726 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
727 hapd->ap_pin_lockout_time);
728 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
729 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
730 hostapd_wps_reenable_ap_pin, hapd,
731 NULL);
732 }
733
734 return 0;
735}
736
737
738static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
739 struct wps_event_pwd_auth_fail *data)
740{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700741 /* Update WPS Status - Authentication Failure */
742 wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
743 hapd->wps_stats.status = WPS_STATUS_FAILURE;
744 hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
745 os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
748}
749
750
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800751static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
752{
753 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
754 return 0;
755
756 if (hapd->ap_pin_failures_consecutive == 0)
757 return 0;
758
759 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
760 "- total validation failures %u (%u consecutive)",
761 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
762 hapd->ap_pin_failures_consecutive = 0;
763
764 return 0;
765}
766
767
768static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
769{
770 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
771}
772
773
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700774static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
775{
776 /* Update WPS Status - PBC Overlap */
777 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
778}
779
780
781static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
782{
783 /* Update WPS PBC Status:PBC Timeout */
784 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
785}
786
787
788static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
789{
790 /* Update WPS PBC status - Active */
791 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
792}
793
794
795static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
796{
797 /* Update WPS PBC status - Active */
798 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
799}
800
801
802static void hostapd_wps_event_success(struct hostapd_data *hapd,
803 struct wps_event_success *success)
804{
805 /* Update WPS status - Success */
806 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
807 hapd->wps_stats.status = WPS_STATUS_SUCCESS;
808 os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
809}
810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811
812static void hostapd_wps_event_fail(struct hostapd_data *hapd,
813 struct wps_event_fail *fail)
814{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700815 /* Update WPS status - Failure */
816 hapd->wps_stats.status = WPS_STATUS_FAILURE;
817 os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
818
819 hapd->wps_stats.failure_reason = fail->error_indication;
820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 if (fail->error_indication > 0 &&
822 fail->error_indication < NUM_WPS_EI_VALUES) {
823 wpa_msg(hapd->msg_ctx, MSG_INFO,
824 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
825 fail->msg, fail->config_error, fail->error_indication,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700826 wps_ei_str(fail->error_indication));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 } else {
828 wpa_msg(hapd->msg_ctx, MSG_INFO,
829 WPS_EVENT_FAIL "msg=%d config_error=%d",
830 fail->msg, fail->config_error);
831 }
832}
833
834
835static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
836 union wps_event_data *data)
837{
838 struct hostapd_data *hapd = ctx;
839
840 switch (event) {
841 case WPS_EV_M2D:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800842 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843 break;
844 case WPS_EV_FAIL:
845 hostapd_wps_event_fail(hapd, &data->fail);
846 break;
847 case WPS_EV_SUCCESS:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700848 hostapd_wps_event_success(hapd, &data->success);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800849 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 break;
851 case WPS_EV_PWD_AUTH_FAIL:
852 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
853 break;
854 case WPS_EV_PBC_OVERLAP:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700855 hostapd_wps_event_pbc_overlap(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 break;
858 case WPS_EV_PBC_TIMEOUT:
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700859 hostapd_wps_event_pbc_timeout(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800860 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700862 case WPS_EV_PBC_ACTIVE:
863 hostapd_wps_event_pbc_active(hapd);
864 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
865 break;
866 case WPS_EV_PBC_DISABLE:
867 hostapd_wps_event_pbc_disable(hapd);
868 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
869 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 case WPS_EV_ER_AP_ADD:
871 break;
872 case WPS_EV_ER_AP_REMOVE:
873 break;
874 case WPS_EV_ER_ENROLLEE_ADD:
875 break;
876 case WPS_EV_ER_ENROLLEE_REMOVE:
877 break;
878 case WPS_EV_ER_AP_SETTINGS:
879 break;
880 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
881 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800882 case WPS_EV_AP_PIN_SUCCESS:
883 hostapd_wps_ap_pin_success(hapd);
884 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 }
886 if (hapd->wps_event_cb)
887 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
888}
889
890
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700891static int hostapd_wps_rf_band_cb(void *ctx)
892{
893 struct hostapd_data *hapd = ctx;
894
895 return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
896 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
897}
898
899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
901{
902 wpabuf_free(hapd->wps_beacon_ie);
903 hapd->wps_beacon_ie = NULL;
904
905 wpabuf_free(hapd->wps_probe_resp_ie);
906 hapd->wps_probe_resp_ie = NULL;
907
908 hostapd_set_ap_wps_ie(hapd);
909}
910
911
912static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
913{
914 const u8 **uuid = ctx;
915 size_t j;
916
917 if (iface == NULL)
918 return 0;
919 for (j = 0; j < iface->num_bss; j++) {
920 struct hostapd_data *hapd = iface->bss[j];
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700921 if (hapd->wps && !hapd->conf->wps_independent &&
922 !is_nil_uuid(hapd->wps->uuid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 *uuid = hapd->wps->uuid;
924 return 1;
925 }
926 }
927
928 return 0;
929}
930
931
932static const u8 * get_own_uuid(struct hostapd_iface *iface)
933{
934 const u8 *uuid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700935 if (iface->interfaces == NULL ||
936 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 return NULL;
938 uuid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700939 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
940 &uuid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 return uuid;
942}
943
944
945static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
946{
947 int *count= ctx;
948 (*count)++;
949 return 0;
950}
951
952
953static int interface_count(struct hostapd_iface *iface)
954{
955 int count = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700956 if (iface->interfaces == NULL ||
957 iface->interfaces->for_each_interface == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700959 iface->interfaces->for_each_interface(iface->interfaces,
960 count_interface_cb, &count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 return count;
962}
963
964
965static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
966 struct wps_context *wps)
967{
968 int i;
969
970 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
971 wpabuf_free(wps->dev.vendor_ext[i]);
972 wps->dev.vendor_ext[i] = NULL;
973
974 if (hapd->conf->wps_vendor_ext[i] == NULL)
975 continue;
976
977 wps->dev.vendor_ext[i] =
978 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
979 if (wps->dev.vendor_ext[i] == NULL) {
980 while (--i >= 0)
981 wpabuf_free(wps->dev.vendor_ext[i]);
982 return -1;
983 }
984 }
985
986 return 0;
987}
988
989
990int hostapd_init_wps(struct hostapd_data *hapd,
991 struct hostapd_bss_config *conf)
992{
993 struct wps_context *wps;
994 struct wps_registrar_config cfg;
995
996 if (conf->wps_state == 0) {
997 hostapd_wps_clear_ies(hapd);
998 return 0;
999 }
1000
1001 wps = os_zalloc(sizeof(*wps));
1002 if (wps == NULL)
1003 return -1;
1004
1005 wps->cred_cb = hostapd_wps_cred_cb;
1006 wps->event_cb = hostapd_wps_event_cb;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001007 wps->rf_band_cb = hostapd_wps_rf_band_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 wps->cb_ctx = hapd;
1009
1010 os_memset(&cfg, 0, sizeof(cfg));
1011 wps->wps_state = hapd->conf->wps_state;
1012 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
1013 if (is_nil_uuid(hapd->conf->uuid)) {
1014 const u8 *uuid;
1015 uuid = get_own_uuid(hapd->iface);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001016 if (uuid && !conf->wps_independent) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 os_memcpy(wps->uuid, uuid, UUID_LEN);
1018 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
1019 "interface", wps->uuid, UUID_LEN);
1020 } else {
1021 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
1022 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1023 "address", wps->uuid, UUID_LEN);
1024 }
1025 } else {
1026 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
1027 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
1028 wps->uuid, UUID_LEN);
1029 }
1030 wps->ssid_len = hapd->conf->ssid.ssid_len;
1031 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1032 wps->ap = 1;
1033 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1034 wps->dev.device_name = hapd->conf->device_name ?
1035 os_strdup(hapd->conf->device_name) : NULL;
1036 wps->dev.manufacturer = hapd->conf->manufacturer ?
1037 os_strdup(hapd->conf->manufacturer) : NULL;
1038 wps->dev.model_name = hapd->conf->model_name ?
1039 os_strdup(hapd->conf->model_name) : NULL;
1040 wps->dev.model_number = hapd->conf->model_number ?
1041 os_strdup(hapd->conf->model_number) : NULL;
1042 wps->dev.serial_number = hapd->conf->serial_number ?
1043 os_strdup(hapd->conf->serial_number) : NULL;
1044 wps->config_methods =
1045 wps_config_methods_str2bin(hapd->conf->config_methods);
1046#ifdef CONFIG_WPS2
1047 if ((wps->config_methods &
1048 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1049 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1050 wpa_printf(MSG_INFO, "WPS: Converting display to "
1051 "virtual_display for WPS 2.0 compliance");
1052 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1053 }
1054 if ((wps->config_methods &
1055 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1056 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1057 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1058 "virtual_push_button for WPS 2.0 compliance");
1059 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1060 }
1061#endif /* CONFIG_WPS2 */
1062 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1063 WPS_DEV_TYPE_LEN);
1064
1065 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
1066 os_free(wps);
1067 return -1;
1068 }
1069
1070 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001071
1072 if (conf->wps_rf_bands) {
1073 wps->dev.rf_bands = conf->wps_rf_bands;
1074 } else {
1075 wps->dev.rf_bands =
1076 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
1077 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
1078 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079
1080 if (conf->wpa & WPA_PROTO_RSN) {
1081 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1082 wps->auth_types |= WPS_AUTH_WPA2PSK;
1083 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1084 wps->auth_types |= WPS_AUTH_WPA2;
1085
1086 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
1087 wps->encr_types |= WPS_ENCR_AES;
1088 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
1089 wps->encr_types |= WPS_ENCR_TKIP;
1090 }
1091
1092 if (conf->wpa & WPA_PROTO_WPA) {
1093 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1094 wps->auth_types |= WPS_AUTH_WPAPSK;
1095 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1096 wps->auth_types |= WPS_AUTH_WPA;
1097
1098 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
1099 wps->encr_types |= WPS_ENCR_AES;
1100 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
1101 wps->encr_types |= WPS_ENCR_TKIP;
1102 }
1103
1104 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1105 wps->encr_types |= WPS_ENCR_NONE;
1106 wps->auth_types |= WPS_AUTH_OPEN;
1107 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
1108 wps->encr_types |= WPS_ENCR_WEP;
1109 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
1110 wps->auth_types |= WPS_AUTH_OPEN;
1111 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
1112 wps->auth_types |= WPS_AUTH_SHARED;
1113 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
1114 wps->auth_types |= WPS_AUTH_OPEN;
1115 if (conf->default_wep_key_len)
1116 wps->encr_types |= WPS_ENCR_WEP;
1117 else
1118 wps->encr_types |= WPS_ENCR_NONE;
1119 }
1120
1121 if (conf->ssid.wpa_psk_file) {
1122 /* Use per-device PSKs */
1123 } else if (conf->ssid.wpa_passphrase) {
1124 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1125 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1126 } else if (conf->ssid.wpa_psk) {
1127 wps->network_key = os_malloc(2 * PMK_LEN + 1);
1128 if (wps->network_key == NULL) {
1129 os_free(wps);
1130 return -1;
1131 }
1132 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1133 conf->ssid.wpa_psk->psk, PMK_LEN);
1134 wps->network_key_len = 2 * PMK_LEN;
1135 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1136 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1137 if (wps->network_key == NULL) {
1138 os_free(wps);
1139 return -1;
1140 }
1141 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1142 conf->ssid.wep.len[0]);
1143 wps->network_key_len = conf->ssid.wep.len[0];
1144 }
1145
1146 if (conf->ssid.wpa_psk) {
1147 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1148 wps->psk_set = 1;
1149 }
1150
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001151 wps->ap_auth_type = wps->auth_types;
1152 wps->ap_encr_type = wps->encr_types;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1154 /* Override parameters to enable security by default */
1155 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1156 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1157 }
1158
1159 wps->ap_settings = conf->ap_settings;
1160 wps->ap_settings_len = conf->ap_settings_len;
1161
1162 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1163 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1164 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1165 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1166 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1167 cfg.cb_ctx = hapd;
1168 cfg.skip_cred_build = conf->skip_cred_build;
1169 cfg.extra_cred = conf->extra_cred;
1170 cfg.extra_cred_len = conf->extra_cred_len;
1171 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1172 conf->skip_cred_build;
1173 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
1174 cfg.static_wep_only = 1;
1175 cfg.dualband = interface_count(hapd->iface) > 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001176 if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1177 (WPS_RF_50GHZ | WPS_RF_24GHZ))
1178 cfg.dualband = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 if (cfg.dualband)
1180 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001181 cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182
1183 wps->registrar = wps_registrar_init(wps, &cfg);
1184 if (wps->registrar == NULL) {
1185 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
1186 os_free(wps->network_key);
1187 os_free(wps);
1188 return -1;
1189 }
1190
1191#ifdef CONFIG_WPS_UPNP
1192 wps->friendly_name = hapd->conf->friendly_name;
1193 wps->manufacturer_url = hapd->conf->manufacturer_url;
1194 wps->model_description = hapd->conf->model_description;
1195 wps->model_url = hapd->conf->model_url;
1196 wps->upc = hapd->conf->upc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197#endif /* CONFIG_WPS_UPNP */
1198
1199 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1200
1201 hapd->wps = wps;
1202
1203 return 0;
1204}
1205
1206
Jouni Malinen87fd2792011-05-16 18:35:42 +03001207int hostapd_init_wps_complete(struct hostapd_data *hapd)
1208{
1209 struct wps_context *wps = hapd->wps;
1210
Jouni Malinen75ecf522011-06-27 15:19:46 -07001211 if (wps == NULL)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001212 return 0;
1213
1214#ifdef CONFIG_WPS_UPNP
1215 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1216 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1217 wps_registrar_deinit(wps->registrar);
1218 os_free(wps->network_key);
1219 os_free(wps);
1220 hapd->wps = NULL;
1221 return -1;
1222 }
1223#endif /* CONFIG_WPS_UPNP */
1224
1225 return 0;
1226}
1227
1228
Dmitry Shmidt04949592012-07-19 12:16:46 -07001229static void hostapd_wps_nfc_clear(struct wps_context *wps)
1230{
1231#ifdef CONFIG_WPS_NFC
Dmitry Shmidt54605472013-11-08 11:10:19 -08001232 wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001233 wps->ap_nfc_dev_pw_id = 0;
1234 wpabuf_free(wps->ap_nfc_dh_pubkey);
1235 wps->ap_nfc_dh_pubkey = NULL;
1236 wpabuf_free(wps->ap_nfc_dh_privkey);
1237 wps->ap_nfc_dh_privkey = NULL;
1238 wpabuf_free(wps->ap_nfc_dev_pw);
1239 wps->ap_nfc_dev_pw = NULL;
1240#endif /* CONFIG_WPS_NFC */
1241}
1242
1243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244void hostapd_deinit_wps(struct hostapd_data *hapd)
1245{
1246 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1247 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001248 eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 if (hapd->wps == NULL)
1250 return;
1251#ifdef CONFIG_WPS_UPNP
1252 hostapd_wps_upnp_deinit(hapd);
1253#endif /* CONFIG_WPS_UPNP */
1254 wps_registrar_deinit(hapd->wps->registrar);
1255 os_free(hapd->wps->network_key);
1256 wps_device_data_free(&hapd->wps->dev);
1257 wpabuf_free(hapd->wps->dh_pubkey);
1258 wpabuf_free(hapd->wps->dh_privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 wps_free_pending_msgs(hapd->wps->upnp_msgs);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001260 hostapd_wps_nfc_clear(hapd->wps);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 os_free(hapd->wps);
1262 hapd->wps = NULL;
1263 hostapd_wps_clear_ies(hapd);
1264}
1265
1266
1267void hostapd_update_wps(struct hostapd_data *hapd)
1268{
1269 if (hapd->wps == NULL)
1270 return;
1271
1272#ifdef CONFIG_WPS_UPNP
1273 hapd->wps->friendly_name = hapd->conf->friendly_name;
1274 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1275 hapd->wps->model_description = hapd->conf->model_description;
1276 hapd->wps->model_url = hapd->conf->model_url;
1277 hapd->wps->upc = hapd->conf->upc;
1278#endif /* CONFIG_WPS_UPNP */
1279
1280 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1281
1282 if (hapd->conf->wps_state)
1283 wps_registrar_update_ie(hapd->wps->registrar);
1284 else
1285 hostapd_deinit_wps(hapd);
1286}
1287
1288
1289struct wps_add_pin_data {
1290 const u8 *addr;
1291 const u8 *uuid;
1292 const u8 *pin;
1293 size_t pin_len;
1294 int timeout;
1295 int added;
1296};
1297
1298
1299static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1300{
1301 struct wps_add_pin_data *data = ctx;
1302 int ret;
1303
1304 if (hapd->wps == NULL)
1305 return 0;
1306 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1307 data->uuid, data->pin, data->pin_len,
1308 data->timeout);
1309 if (ret == 0)
1310 data->added++;
1311 return ret;
1312}
1313
1314
1315int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1316 const char *uuid, const char *pin, int timeout)
1317{
1318 u8 u[UUID_LEN];
1319 struct wps_add_pin_data data;
1320
1321 data.addr = addr;
1322 data.uuid = u;
1323 data.pin = (const u8 *) pin;
1324 data.pin_len = os_strlen(pin);
1325 data.timeout = timeout;
1326 data.added = 0;
1327
1328 if (os_strcmp(uuid, "any") == 0)
1329 data.uuid = NULL;
1330 else {
1331 if (uuid_str2bin(uuid, u))
1332 return -1;
1333 data.uuid = u;
1334 }
1335 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1336 return -1;
1337 return data.added ? 0 : -1;
1338}
1339
1340
1341static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1342{
1343 const u8 *p2p_dev_addr = ctx;
1344 if (hapd->wps == NULL)
1345 return 0;
1346 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1347}
1348
1349
1350int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1351 const u8 *p2p_dev_addr)
1352{
1353 return hostapd_wps_for_each(hapd, wps_button_pushed,
1354 (void *) p2p_dev_addr);
1355}
1356
1357
Dmitry Shmidt04949592012-07-19 12:16:46 -07001358static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1359{
1360 if (hapd->wps == NULL)
1361 return 0;
1362
1363 wps_registrar_wps_cancel(hapd->wps->registrar);
1364 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1365
1366 return 0;
1367}
1368
1369
1370int hostapd_wps_cancel(struct hostapd_data *hapd)
1371{
1372 return hostapd_wps_for_each(hapd, wps_cancel, NULL);
1373}
1374
1375
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001376static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1377 const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001378 const u8 *ie, size_t ie_len,
1379 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380{
1381 struct hostapd_data *hapd = ctx;
1382 struct wpabuf *wps_ie;
1383 struct ieee802_11_elems elems;
1384
1385 if (hapd->wps == NULL)
1386 return 0;
1387
1388 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1389 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1390 MACSTR, MAC2STR(addr));
1391 return 0;
1392 }
1393
1394 if (elems.ssid && elems.ssid_len > 0 &&
1395 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1396 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1397 0))
1398 return 0; /* Not for us */
1399
1400 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1401 if (wps_ie == NULL)
1402 return 0;
1403 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1404 wpabuf_free(wps_ie);
1405 return 0;
1406 }
1407
1408 if (wpabuf_len(wps_ie) > 0) {
1409 int p2p_wildcard = 0;
1410#ifdef CONFIG_P2P
1411 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1412 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1413 P2P_WILDCARD_SSID_LEN) == 0)
1414 p2p_wildcard = 1;
1415#endif /* CONFIG_P2P */
1416 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1417 p2p_wildcard);
1418#ifdef CONFIG_WPS_UPNP
1419 /* FIX: what exactly should be included in the WLANEvent?
1420 * WPS attributes? Full ProbeReq frame? */
1421 if (!p2p_wildcard)
1422 upnp_wps_device_send_wlan_event(
1423 hapd->wps_upnp, addr,
1424 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1425#endif /* CONFIG_WPS_UPNP */
1426 }
1427
1428 wpabuf_free(wps_ie);
1429
1430 return 0;
1431}
1432
1433
1434#ifdef CONFIG_WPS_UPNP
1435
1436static int hostapd_rx_req_put_wlan_response(
1437 void *priv, enum upnp_wps_wlanevent_type ev_type,
1438 const u8 *mac_addr, const struct wpabuf *msg,
1439 enum wps_msg_type msg_type)
1440{
1441 struct hostapd_data *hapd = priv;
1442 struct sta_info *sta;
1443 struct upnp_pending_message *p;
1444
1445 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1446 MACSTR, ev_type, MAC2STR(mac_addr));
1447 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1448 wpabuf_head(msg), wpabuf_len(msg));
1449 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1450 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1451 "PutWLANResponse WLANEventType %d", ev_type);
1452 return -1;
1453 }
1454
1455 /*
1456 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1457 * server implementation for delivery to the peer.
1458 */
1459
1460 sta = ap_get_sta(hapd, mac_addr);
1461#ifndef CONFIG_WPS_STRICT
1462 if (!sta) {
1463 /*
1464 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1465 * Pick STA that is in an ongoing WPS registration without
1466 * checking the MAC address.
1467 */
1468 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1469 "on NewWLANEventMAC; try wildcard match");
1470 for (sta = hapd->sta_list; sta; sta = sta->next) {
1471 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1472 break;
1473 }
1474 }
1475#endif /* CONFIG_WPS_STRICT */
1476
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1479 return 0;
1480 }
1481
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001482 if (!sta->eapol_sm) {
1483 /*
1484 * This can happen, e.g., if an ER sends an extra message after
1485 * the station has disassociated (but not fully
1486 * deauthenticated).
1487 */
1488 wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
1489 return 0;
1490 }
1491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492 p = os_zalloc(sizeof(*p));
1493 if (p == NULL)
1494 return -1;
1495 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1496 p->msg = wpabuf_dup(msg);
1497 p->type = msg_type;
1498 p->next = hapd->wps->upnp_msgs;
1499 hapd->wps->upnp_msgs = p;
1500
1501 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1502}
1503
1504
1505static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1506 struct wps_context *wps)
1507{
1508 struct upnp_wps_device_ctx *ctx;
1509
1510 if (!hapd->conf->upnp_iface)
1511 return 0;
1512 ctx = os_zalloc(sizeof(*ctx));
1513 if (ctx == NULL)
1514 return -1;
1515
1516 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1517 if (hapd->conf->ap_pin)
1518 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1519
1520 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1521 hapd->conf->upnp_iface);
1522 if (hapd->wps_upnp == NULL)
1523 return -1;
1524 wps->wps_upnp = hapd->wps_upnp;
1525
1526 return 0;
1527}
1528
1529
1530static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1531{
1532 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1533}
1534
1535#endif /* CONFIG_WPS_UPNP */
1536
1537
1538int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1539 char *buf, size_t buflen)
1540{
1541 if (hapd->wps == NULL)
1542 return 0;
1543 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1544}
1545
1546
1547static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1548{
1549 struct hostapd_data *hapd = eloop_data;
1550 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1551 hostapd_wps_ap_pin_disable(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001552 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553}
1554
1555
1556static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1557{
1558 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1559 hapd->ap_pin_failures = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001560 hapd->ap_pin_failures_consecutive = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561 hapd->conf->ap_setup_locked = 0;
1562 if (hapd->wps->ap_setup_locked) {
1563 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1564 hapd->wps->ap_setup_locked = 0;
1565 wps_registrar_update_ie(hapd->wps->registrar);
1566 }
1567 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1568 if (timeout > 0)
1569 eloop_register_timeout(timeout, 0,
1570 hostapd_wps_ap_pin_timeout, hapd, NULL);
1571}
1572
1573
1574static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1575{
1576 os_free(hapd->conf->ap_pin);
1577 hapd->conf->ap_pin = NULL;
1578#ifdef CONFIG_WPS_UPNP
1579 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1580#endif /* CONFIG_WPS_UPNP */
1581 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1582 return 0;
1583}
1584
1585
1586void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1587{
1588 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1589 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1590}
1591
1592
1593struct wps_ap_pin_data {
1594 char pin_txt[9];
1595 int timeout;
1596};
1597
1598
1599static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1600{
1601 struct wps_ap_pin_data *data = ctx;
1602 os_free(hapd->conf->ap_pin);
1603 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1604#ifdef CONFIG_WPS_UPNP
1605 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1606#endif /* CONFIG_WPS_UPNP */
1607 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1608 return 0;
1609}
1610
1611
1612const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1613{
1614 unsigned int pin;
1615 struct wps_ap_pin_data data;
1616
1617 pin = wps_generate_pin();
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001618 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 data.timeout = timeout;
1620 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1621 return hapd->conf->ap_pin;
1622}
1623
1624
1625const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1626{
1627 return hapd->conf->ap_pin;
1628}
1629
1630
1631int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1632 int timeout)
1633{
1634 struct wps_ap_pin_data data;
1635 int ret;
1636
1637 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1638 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1639 return -1;
1640 data.timeout = timeout;
1641 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1642}
1643
1644
1645static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1646{
1647 if (hapd->wps)
1648 wps_registrar_update_ie(hapd->wps->registrar);
1649 return 0;
1650}
1651
1652
1653void hostapd_wps_update_ie(struct hostapd_data *hapd)
1654{
1655 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1656}
1657
1658
1659int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1660 const char *auth, const char *encr, const char *key)
1661{
1662 struct wps_credential cred;
1663 size_t len;
1664
1665 os_memset(&cred, 0, sizeof(cred));
1666
1667 len = os_strlen(ssid);
1668 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1669 hexstr2bin(ssid, cred.ssid, len / 2))
1670 return -1;
1671 cred.ssid_len = len / 2;
1672
1673 if (os_strncmp(auth, "OPEN", 4) == 0)
1674 cred.auth_type = WPS_AUTH_OPEN;
1675 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1676 cred.auth_type = WPS_AUTH_WPAPSK;
1677 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1678 cred.auth_type = WPS_AUTH_WPA2PSK;
1679 else
1680 return -1;
1681
1682 if (encr) {
1683 if (os_strncmp(encr, "NONE", 4) == 0)
1684 cred.encr_type = WPS_ENCR_NONE;
1685 else if (os_strncmp(encr, "WEP", 3) == 0)
1686 cred.encr_type = WPS_ENCR_WEP;
1687 else if (os_strncmp(encr, "TKIP", 4) == 0)
1688 cred.encr_type = WPS_ENCR_TKIP;
1689 else if (os_strncmp(encr, "CCMP", 4) == 0)
1690 cred.encr_type = WPS_ENCR_AES;
1691 else
1692 return -1;
1693 } else
1694 cred.encr_type = WPS_ENCR_NONE;
1695
1696 if (key) {
1697 len = os_strlen(key);
1698 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1699 hexstr2bin(key, cred.key, len / 2))
1700 return -1;
1701 cred.key_len = len / 2;
1702 }
1703
1704 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1705}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001706
1707
1708#ifdef CONFIG_WPS_NFC
1709
1710struct wps_nfc_password_token_data {
1711 const u8 *oob_dev_pw;
1712 size_t oob_dev_pw_len;
1713 int added;
1714};
1715
1716
1717static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1718{
1719 struct wps_nfc_password_token_data *data = ctx;
1720 int ret;
1721
1722 if (hapd->wps == NULL)
1723 return 0;
1724 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1725 data->oob_dev_pw,
1726 data->oob_dev_pw_len);
1727 if (ret == 0)
1728 data->added++;
1729 return ret;
1730}
1731
1732
1733static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1734 struct wps_parse_attr *attr)
1735{
1736 struct wps_nfc_password_token_data data;
1737
1738 data.oob_dev_pw = attr->oob_dev_password;
1739 data.oob_dev_pw_len = attr->oob_dev_password_len;
1740 data.added = 0;
1741 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1742 return -1;
1743 return data.added ? 0 : -1;
1744}
1745
1746
1747static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1748 const struct wpabuf *wps)
1749{
1750 struct wps_parse_attr attr;
1751
1752 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1753
1754 if (wps_parse_msg(wps, &attr)) {
1755 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1756 return -1;
1757 }
1758
1759 if (attr.oob_dev_password)
1760 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1761
1762 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1763 return -1;
1764}
1765
1766
1767int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1768 const struct wpabuf *data)
1769{
1770 const struct wpabuf *wps = data;
1771 struct wpabuf *tmp = NULL;
1772 int ret;
1773
1774 if (wpabuf_len(data) < 4)
1775 return -1;
1776
1777 if (*wpabuf_head_u8(data) != 0x10) {
1778 /* Assume this contains full NDEF record */
1779 tmp = ndef_parse_wifi(data);
1780 if (tmp == NULL) {
1781 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1782 return -1;
1783 }
1784 wps = tmp;
1785 }
1786
1787 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1788 wpabuf_free(tmp);
1789 return ret;
1790}
1791
1792
1793struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1794 int ndef)
1795{
1796 struct wpabuf *ret;
1797
1798 if (hapd->wps == NULL)
1799 return NULL;
1800
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001801 ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
1802 hapd->iconf->channel);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001803 if (ndef && ret) {
1804 struct wpabuf *tmp;
1805 tmp = ndef_build_wifi(ret);
1806 wpabuf_free(ret);
1807 if (tmp == NULL)
1808 return NULL;
1809 ret = tmp;
1810 }
1811
1812 return ret;
1813}
1814
1815
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001816struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
1817{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001818 struct wpabuf *ret;
1819
1820 if (hapd->wps == NULL)
1821 return NULL;
1822
1823 if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
1824 struct wps_context *wps = hapd->wps;
1825 if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
1826 &hapd->conf->wps_nfc_dh_privkey) < 0)
1827 return NULL;
1828 hostapd_wps_nfc_clear(wps);
1829 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
1830 wps->ap_nfc_dh_pubkey =
1831 wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1832 wps->ap_nfc_dh_privkey =
1833 wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1834 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
1835 hostapd_wps_nfc_clear(wps);
1836 return NULL;
1837 }
1838 }
1839
1840 ret = wps_build_nfc_handover_sel(hapd->wps,
1841 hapd->conf->wps_nfc_dh_pubkey,
1842 hapd->own_addr, hapd->iface->freq);
1843
1844 if (ndef && ret) {
1845 struct wpabuf *tmp;
1846 tmp = ndef_build_wifi(ret);
1847 wpabuf_free(ret);
1848 if (tmp == NULL)
1849 return NULL;
1850 ret = tmp;
1851 }
1852
1853 return ret;
1854}
1855
1856
1857int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
1858 const struct wpabuf *req,
1859 const struct wpabuf *sel)
1860{
1861 struct wpabuf *wps;
1862 int ret = -1;
1863 u16 wsc_len;
1864 const u8 *pos;
1865 struct wpabuf msg;
1866 struct wps_parse_attr attr;
1867 u16 dev_pw_id;
1868
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001869 /*
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001870 * Enrollee/station is always initiator of the NFC connection handover,
1871 * so use the request message here to find Enrollee public key hash.
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001872 */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001873 wps = ndef_parse_wifi(req);
1874 if (wps == NULL)
1875 return -1;
1876 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
1877 "payload from NFC connection handover");
1878 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
1879 if (wpabuf_len(wps) < 2) {
1880 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
1881 "Message");
1882 goto out;
1883 }
1884 pos = wpabuf_head(wps);
1885 wsc_len = WPA_GET_BE16(pos);
1886 if (wsc_len > wpabuf_len(wps) - 2) {
1887 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
1888 "in rt Wi-Fi Handover Request Message", wsc_len);
1889 goto out;
1890 }
1891 pos += 2;
1892
1893 wpa_hexdump(MSG_DEBUG,
1894 "WPS: WSC attributes in Wi-Fi Handover Request Message",
1895 pos, wsc_len);
1896 if (wsc_len < wpabuf_len(wps) - 2) {
1897 wpa_hexdump(MSG_DEBUG,
1898 "WPS: Ignore extra data after WSC attributes",
1899 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
1900 }
1901
1902 wpabuf_set(&msg, pos, wsc_len);
1903 ret = wps_parse_msg(&msg, &attr);
1904 if (ret < 0) {
1905 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
1906 "Wi-Fi Handover Request Message");
1907 goto out;
1908 }
1909
1910 if (attr.oob_dev_password == NULL ||
1911 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
1912 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
1913 "included in Wi-Fi Handover Request Message");
1914 ret = -1;
1915 goto out;
1916 }
1917
1918 if (attr.uuid_e == NULL) {
1919 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
1920 "Handover Request Message");
1921 ret = -1;
1922 goto out;
1923 }
1924
1925 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
1926
1927 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
1928 attr.oob_dev_password, attr.oob_dev_password_len);
1929 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
1930 WPS_OOB_PUBKEY_HASH_LEN);
1931 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
1932 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
1933 "%u in Wi-Fi Handover Request Message", dev_pw_id);
1934 ret = -1;
1935 goto out;
1936 }
1937 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
1938 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
1939
1940 ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
1941 attr.oob_dev_password,
1942 DEV_PW_NFC_CONNECTION_HANDOVER,
1943 NULL, 0, 1);
1944
1945out:
1946 wpabuf_free(wps);
1947 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001948}
1949
1950
Dmitry Shmidt04949592012-07-19 12:16:46 -07001951struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1952{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001953 if (hapd->conf->wps_nfc_pw_from_config) {
1954 return wps_nfc_token_build(ndef,
1955 hapd->conf->wps_nfc_dev_pw_id,
1956 hapd->conf->wps_nfc_dh_pubkey,
1957 hapd->conf->wps_nfc_dev_pw);
1958 }
1959
Dmitry Shmidt04949592012-07-19 12:16:46 -07001960 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1961 &hapd->conf->wps_nfc_dh_pubkey,
1962 &hapd->conf->wps_nfc_dh_privkey,
1963 &hapd->conf->wps_nfc_dev_pw);
1964}
1965
1966
1967int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1968{
1969 struct wps_context *wps = hapd->wps;
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001970 struct wpabuf *pw;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001971
1972 if (wps == NULL)
1973 return -1;
1974
1975 if (!hapd->conf->wps_nfc_dh_pubkey ||
1976 !hapd->conf->wps_nfc_dh_privkey ||
1977 !hapd->conf->wps_nfc_dev_pw ||
1978 !hapd->conf->wps_nfc_dev_pw_id)
1979 return -1;
1980
1981 hostapd_wps_nfc_clear(wps);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001982 wpa_printf(MSG_DEBUG,
1983 "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
1984 hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001985 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1986 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1987 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
Dmitry Shmidt33e38bf2013-02-27 12:56:00 -08001988 pw = hapd->conf->wps_nfc_dev_pw;
1989 wps->ap_nfc_dev_pw = wpabuf_alloc(
1990 wpabuf_len(pw) * 2 + 1);
1991 if (wps->ap_nfc_dev_pw) {
1992 wpa_snprintf_hex_uppercase(
1993 (char *) wpabuf_put(wps->ap_nfc_dev_pw,
1994 wpabuf_len(pw) * 2),
1995 wpabuf_len(pw) * 2 + 1,
1996 wpabuf_head(pw), wpabuf_len(pw));
1997 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001998
1999 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
2000 !wps->ap_nfc_dev_pw) {
2001 hostapd_wps_nfc_clear(wps);
2002 return -1;
2003 }
2004
2005 return 0;
2006}
2007
2008
2009void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
2010{
Dmitry Shmidt54605472013-11-08 11:10:19 -08002011 wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
2012 hapd->conf->iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002013 hostapd_wps_nfc_clear(hapd->wps);
2014}
2015
2016#endif /* CONFIG_WPS_NFC */