hostapd: Fix WPA, IEEE 802.1X, and WPS deinit in cases where init fails
[mech_eap.git] / src / ap / wps_hostapd.c
1 /*
2  * hostapd / WPS integration
3  * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/uuid.h"
14 #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"
22 #include "wps/wps_attr_parse.h"
23 #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"
33 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
34                                  struct wps_context *wps);
35 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
36 #endif /* CONFIG_WPS_UPNP */
37
38 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
39                                     const u8 *bssid,
40                                     const u8 *ie, size_t ie_len,
41                                     int ssi_signal);
42 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
43 static void hostapd_wps_nfc_clear(struct wps_context *wps);
44
45
46 struct wps_for_each_data {
47         int (*func)(struct hostapd_data *h, void *ctx);
48         void *ctx;
49         struct hostapd_data *calling_hapd;
50 };
51
52
53 static 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];
62                 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);
70                 if (ret)
71                         return ret;
72         }
73
74         return 0;
75 }
76
77
78 static 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;
86         data.calling_hapd = hapd;
87         if (iface->interfaces == NULL ||
88             iface->interfaces->for_each_interface == NULL)
89                 return wps_for_each(iface, &data);
90         return iface->interfaces->for_each_interface(iface->interfaces,
91                                                      wps_for_each, &data);
92 }
93
94
95 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
96                                   const u8 *p2p_dev_addr, const u8 *psk,
97                                   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
103         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         }
113         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);
126         os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
127         os_memcpy(p->psk, psk, PMK_LEN);
128
129         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
134         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
157 static 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;
165         if (hapd->beacon_set_done)
166                 ieee802_11_set_beacon(hapd);
167         return hostapd_set_ap_wps_ie(hapd);
168 }
169
170
171 static 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)));
188         if (!os_snprintf_error(sizeof(txt), len))
189                 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
210 struct wps_stop_reg_data {
211         struct hostapd_data *current_hapd;
212         const u8 *uuid_e;
213         const u8 *dev_pw;
214         size_t dev_pw_len;
215 };
216
217 static 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)
221                 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
222                                        data->dev_pw, data->dev_pw_len);
223         return 0;
224 }
225
226
227 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
228                                        const u8 *uuid_e, const u8 *dev_pw,
229                                        size_t dev_pw_len)
230 {
231         struct hostapd_data *hapd = ctx;
232         char uuid[40];
233         struct wps_stop_reg_data data;
234         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);
241         data.current_hapd = hapd;
242         data.uuid_e = uuid_e;
243         data.dev_pw = dev_pw;
244         data.dev_pw_len = dev_pw_len;
245         hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
246 }
247
248
249 static 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
272 static int str_starts(const char *str, const char *start)
273 {
274         return os_strncmp(str, start, os_strlen(start)) == 0;
275 }
276
277
278 static 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");
283         if (iface->interfaces == NULL ||
284             iface->interfaces->reload_config(iface) < 0) {
285                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
286                            "configuration");
287         }
288 }
289
290
291 void 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
305 static 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
319 static 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 <= SSID_MAX_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                         if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
352                                 bss->wpa_pairwise |= WPA_CIPHER_GCMP;
353                         else
354                                 bss->wpa_pairwise |= WPA_CIPHER_CCMP;
355                 }
356                 if (cred->encr_type & WPS_ENCR_TKIP)
357                         bss->wpa_pairwise |= WPA_CIPHER_TKIP;
358                 bss->rsn_pairwise = bss->wpa_pairwise;
359                 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
360                                                             bss->wpa_pairwise,
361                                                             bss->rsn_pairwise);
362
363                 if (cred->key_len >= 8 && cred->key_len < 64) {
364                         os_free(bss->ssid.wpa_passphrase);
365                         bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
366                         if (bss->ssid.wpa_passphrase)
367                                 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
368                                           cred->key_len);
369                         hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
370                 } else if (cred->key_len == 64) {
371                         hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
372                         bss->ssid.wpa_psk =
373                                 os_zalloc(sizeof(struct hostapd_wpa_psk));
374                         if (bss->ssid.wpa_psk &&
375                             hexstr2bin((const char *) cred->key,
376                                        bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
377                                 bss->ssid.wpa_psk->group = 1;
378                                 os_free(bss->ssid.wpa_passphrase);
379                                 bss->ssid.wpa_passphrase = NULL;
380                         }
381                 }
382                 bss->auth_algs = 1;
383         } else {
384                 /*
385                  * WPS 2.0 does not allow WEP to be configured, so no need to
386                  * process that option here either.
387                  */
388                 bss->auth_algs = 1;
389         }
390
391         /* Schedule configuration reload after short period of time to allow
392          * EAP-WSC to be finished.
393          */
394         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
395                                NULL);
396
397         return 0;
398 }
399
400
401 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
402 {
403         const struct wps_credential *cred = ctx;
404         FILE *oconf, *nconf;
405         size_t len, i;
406         char *tmp_fname;
407         char buf[1024];
408         int multi_bss;
409         int wpa;
410
411         if (hapd->wps == NULL)
412                 return 0;
413
414         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
415                         cred->cred_attr, cred->cred_attr_len);
416
417         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
418         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
419         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
420                    cred->auth_type);
421         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
422         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
423         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
424                         cred->key, cred->key_len);
425         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
426                    MAC2STR(cred->mac_addr));
427
428         if ((hapd->conf->wps_cred_processing == 1 ||
429              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
430                 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
431         } else if (hapd->conf->wps_cred_processing == 1 ||
432                    hapd->conf->wps_cred_processing == 2) {
433                 struct wpabuf *attr;
434                 attr = wpabuf_alloc(200);
435                 if (attr && wps_build_credential_wrap(attr, cred) == 0)
436                         hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
437                                           wpabuf_len(attr));
438                 wpabuf_free(attr);
439         } else
440                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
441
442         if (hapd->conf->wps_cred_processing == 1)
443                 return 0;
444
445         os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
446         hapd->wps->ssid_len = cred->ssid_len;
447         hapd->wps->encr_types = cred->encr_type;
448         hapd->wps->auth_types = cred->auth_type;
449         hapd->wps->ap_encr_type = cred->encr_type;
450         hapd->wps->ap_auth_type = cred->auth_type;
451         if (cred->key_len == 0) {
452                 os_free(hapd->wps->network_key);
453                 hapd->wps->network_key = NULL;
454                 hapd->wps->network_key_len = 0;
455         } else if ((cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) &&
456                    (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN)) {
457                 wpa_printf(MSG_INFO, "WPS: Invalid key length %lu for WPA/WPA2",
458                            (unsigned long) cred->key_len);
459                 return -1;
460         } else {
461                 if (hapd->wps->network_key == NULL ||
462                     hapd->wps->network_key_len < cred->key_len) {
463                         hapd->wps->network_key_len = 0;
464                         os_free(hapd->wps->network_key);
465                         hapd->wps->network_key = os_malloc(cred->key_len);
466                         if (hapd->wps->network_key == NULL)
467                                 return -1;
468                 }
469                 hapd->wps->network_key_len = cred->key_len;
470                 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
471         }
472         hapd->wps->wps_state = WPS_STATE_CONFIGURED;
473
474         if (hapd->iface->config_fname == NULL)
475                 return hapd_wps_reconfig_in_memory(hapd, cred);
476         len = os_strlen(hapd->iface->config_fname) + 5;
477         tmp_fname = os_malloc(len);
478         if (tmp_fname == NULL)
479                 return -1;
480         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
481
482         oconf = fopen(hapd->iface->config_fname, "r");
483         if (oconf == NULL) {
484                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
485                            "configuration file");
486                 os_free(tmp_fname);
487                 return -1;
488         }
489
490         nconf = fopen(tmp_fname, "w");
491         if (nconf == NULL) {
492                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
493                            "configuration file");
494                 os_free(tmp_fname);
495                 fclose(oconf);
496                 return -1;
497         }
498
499         fprintf(nconf, "# WPS configuration - START\n");
500
501         fprintf(nconf, "wps_state=2\n");
502
503         if (is_hex(cred->ssid, cred->ssid_len)) {
504                 fprintf(nconf, "ssid2=");
505                 for (i = 0; i < cred->ssid_len; i++)
506                         fprintf(nconf, "%02x", cred->ssid[i]);
507                 fprintf(nconf, "\n");
508         } else {
509                 fprintf(nconf, "ssid=");
510                 for (i = 0; i < cred->ssid_len; i++)
511                         fputc(cred->ssid[i], nconf);
512                 fprintf(nconf, "\n");
513         }
514
515         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
516             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
517                 wpa = 3;
518         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
519                 wpa = 2;
520         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
521                 wpa = 1;
522         else
523                 wpa = 0;
524
525         if (wpa) {
526                 char *prefix;
527                 fprintf(nconf, "wpa=%d\n", wpa);
528
529                 fprintf(nconf, "wpa_key_mgmt=");
530                 prefix = "";
531                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
532                         fprintf(nconf, "WPA-EAP");
533                         prefix = " ";
534                 }
535                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
536                         fprintf(nconf, "%sWPA-PSK", prefix);
537                 fprintf(nconf, "\n");
538
539                 fprintf(nconf, "wpa_pairwise=");
540                 prefix = "";
541                 if (cred->encr_type & WPS_ENCR_AES) {
542                         if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
543                                 fprintf(nconf, "GCMP");
544                         else
545                                 fprintf(nconf, "CCMP");
546
547                         prefix = " ";
548                 }
549                 if (cred->encr_type & WPS_ENCR_TKIP) {
550                         fprintf(nconf, "%sTKIP", prefix);
551                 }
552                 fprintf(nconf, "\n");
553
554                 if (cred->key_len >= 8 && cred->key_len < 64) {
555                         fprintf(nconf, "wpa_passphrase=");
556                         for (i = 0; i < cred->key_len; i++)
557                                 fputc(cred->key[i], nconf);
558                         fprintf(nconf, "\n");
559                 } else if (cred->key_len == 64) {
560                         fprintf(nconf, "wpa_psk=");
561                         for (i = 0; i < cred->key_len; i++)
562                                 fputc(cred->key[i], nconf);
563                         fprintf(nconf, "\n");
564                 } else {
565                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
566                                    "for WPA/WPA2",
567                                    (unsigned long) cred->key_len);
568                 }
569
570                 fprintf(nconf, "auth_algs=1\n");
571         } else {
572                 /*
573                  * WPS 2.0 does not allow WEP to be configured, so no need to
574                  * process that option here either.
575                  */
576                 fprintf(nconf, "auth_algs=1\n");
577         }
578
579         fprintf(nconf, "# WPS configuration - END\n");
580
581         multi_bss = 0;
582         while (fgets(buf, sizeof(buf), oconf)) {
583                 if (os_strncmp(buf, "bss=", 4) == 0)
584                         multi_bss = 1;
585                 if (!multi_bss &&
586                     (str_starts(buf, "ssid=") ||
587                      str_starts(buf, "ssid2=") ||
588                      str_starts(buf, "auth_algs=") ||
589                      str_starts(buf, "wep_default_key=") ||
590                      str_starts(buf, "wep_key") ||
591                      str_starts(buf, "wps_state=") ||
592                      str_starts(buf, "wpa=") ||
593                      str_starts(buf, "wpa_psk=") ||
594                      str_starts(buf, "wpa_pairwise=") ||
595                      str_starts(buf, "rsn_pairwise=") ||
596                      str_starts(buf, "wpa_key_mgmt=") ||
597                      str_starts(buf, "wpa_passphrase="))) {
598                         fprintf(nconf, "#WPS# %s", buf);
599                 } else
600                         fprintf(nconf, "%s", buf);
601         }
602
603         fclose(nconf);
604         fclose(oconf);
605
606         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
607                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
608                            "configuration file: %s", strerror(errno));
609                 os_free(tmp_fname);
610                 return -1;
611         }
612
613         os_free(tmp_fname);
614
615         /* Schedule configuration reload after short period of time to allow
616          * EAP-WSC to be finished.
617          */
618         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
619                                NULL);
620
621         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
622
623         return 0;
624 }
625
626
627 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
628 {
629         struct hostapd_data *hapd = ctx;
630         return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
631 }
632
633
634 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
635 {
636         struct hostapd_data *hapd = eloop_data;
637
638         if (hapd->conf->ap_setup_locked)
639                 return;
640         if (hapd->ap_pin_failures_consecutive >= 10)
641                 return;
642
643         wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
644         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
645         hapd->wps->ap_setup_locked = 0;
646         wps_registrar_update_ie(hapd->wps->registrar);
647 }
648
649
650 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
651 {
652         struct wps_event_pwd_auth_fail *data = ctx;
653
654         if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
655                 return 0;
656
657         /*
658          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
659          * for some time if this happens multiple times to slow down brute
660          * force attacks.
661          */
662         hapd->ap_pin_failures++;
663         hapd->ap_pin_failures_consecutive++;
664         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
665                    "(%u consecutive)",
666                    hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
667         if (hapd->ap_pin_failures < 3)
668                 return 0;
669
670         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
671         hapd->wps->ap_setup_locked = 1;
672
673         wps_registrar_update_ie(hapd->wps->registrar);
674
675         if (!hapd->conf->ap_setup_locked &&
676             hapd->ap_pin_failures_consecutive >= 10) {
677                 /*
678                  * In indefinite lockdown - disable automatic AP PIN
679                  * reenablement.
680                  */
681                 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
682                 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
683         } else if (!hapd->conf->ap_setup_locked) {
684                 if (hapd->ap_pin_lockout_time == 0)
685                         hapd->ap_pin_lockout_time = 60;
686                 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
687                          (hapd->ap_pin_failures % 3) == 0)
688                         hapd->ap_pin_lockout_time *= 2;
689
690                 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
691                            hapd->ap_pin_lockout_time);
692                 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
693                 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
694                                        hostapd_wps_reenable_ap_pin, hapd,
695                                        NULL);
696         }
697
698         return 0;
699 }
700
701
702 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
703                                   struct wps_event_pwd_auth_fail *data)
704 {
705         /* Update WPS Status - Authentication Failure */
706         wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
707         hapd->wps_stats.status = WPS_STATUS_FAILURE;
708         hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
709         os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
710
711         hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
712 }
713
714
715 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
716 {
717         if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
718                 return 0;
719
720         if (hapd->ap_pin_failures_consecutive == 0)
721                 return 0;
722
723         wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
724                    "- total validation failures %u (%u consecutive)",
725                    hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
726         hapd->ap_pin_failures_consecutive = 0;
727
728         return 0;
729 }
730
731
732 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
733 {
734         hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
735 }
736
737
738 static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
739 {
740         /* Update WPS Status - PBC Overlap */
741         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
742 }
743
744
745 static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
746 {
747         /* Update WPS PBC Status:PBC Timeout */
748         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
749 }
750
751
752 static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
753 {
754         /* Update WPS PBC status - Active */
755         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
756 }
757
758
759 static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
760 {
761         /* Update WPS PBC status - Active */
762         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
763 }
764
765
766 static void hostapd_wps_event_success(struct hostapd_data *hapd,
767                                       struct wps_event_success *success)
768 {
769         /* Update WPS status - Success */
770         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
771         hapd->wps_stats.status = WPS_STATUS_SUCCESS;
772         os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
773 }
774
775
776 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
777                                    struct wps_event_fail *fail)
778 {
779         /* Update WPS status - Failure */
780         hapd->wps_stats.status = WPS_STATUS_FAILURE;
781         os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
782
783         hapd->wps_stats.failure_reason = fail->error_indication;
784
785         if (fail->error_indication > 0 &&
786             fail->error_indication < NUM_WPS_EI_VALUES) {
787                 wpa_msg(hapd->msg_ctx, MSG_INFO,
788                         WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
789                         fail->msg, fail->config_error, fail->error_indication,
790                         wps_ei_str(fail->error_indication));
791         } else {
792                 wpa_msg(hapd->msg_ctx, MSG_INFO,
793                         WPS_EVENT_FAIL "msg=%d config_error=%d",
794                         fail->msg, fail->config_error);
795         }
796 }
797
798
799 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
800                                  union wps_event_data *data)
801 {
802         struct hostapd_data *hapd = ctx;
803
804         switch (event) {
805         case WPS_EV_M2D:
806                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
807                 break;
808         case WPS_EV_FAIL:
809                 hostapd_wps_event_fail(hapd, &data->fail);
810                 break;
811         case WPS_EV_SUCCESS:
812                 hostapd_wps_event_success(hapd, &data->success);
813                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
814                 break;
815         case WPS_EV_PWD_AUTH_FAIL:
816                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
817                 break;
818         case WPS_EV_PBC_OVERLAP:
819                 hostapd_wps_event_pbc_overlap(hapd);
820                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
821                 break;
822         case WPS_EV_PBC_TIMEOUT:
823                 hostapd_wps_event_pbc_timeout(hapd);
824                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
825                 break;
826         case WPS_EV_PBC_ACTIVE:
827                 hostapd_wps_event_pbc_active(hapd);
828                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
829                 break;
830         case WPS_EV_PBC_DISABLE:
831                 hostapd_wps_event_pbc_disable(hapd);
832                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
833                 break;
834         case WPS_EV_ER_AP_ADD:
835                 break;
836         case WPS_EV_ER_AP_REMOVE:
837                 break;
838         case WPS_EV_ER_ENROLLEE_ADD:
839                 break;
840         case WPS_EV_ER_ENROLLEE_REMOVE:
841                 break;
842         case WPS_EV_ER_AP_SETTINGS:
843                 break;
844         case WPS_EV_ER_SET_SELECTED_REGISTRAR:
845                 break;
846         case WPS_EV_AP_PIN_SUCCESS:
847                 hostapd_wps_ap_pin_success(hapd);
848                 break;
849         }
850         if (hapd->wps_event_cb)
851                 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
852 }
853
854
855 static int hostapd_wps_rf_band_cb(void *ctx)
856 {
857         struct hostapd_data *hapd = ctx;
858
859         return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
860                 WPS_RF_50GHZ :
861                 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
862                 WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
863 }
864
865
866 static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only)
867 {
868         wpabuf_free(hapd->wps_beacon_ie);
869         hapd->wps_beacon_ie = NULL;
870
871         wpabuf_free(hapd->wps_probe_resp_ie);
872         hapd->wps_probe_resp_ie = NULL;
873
874         if (deinit_only) {
875                 if (hapd->drv_priv)
876                         hostapd_reset_ap_wps_ie(hapd);
877                 return;
878         }
879
880         hostapd_set_ap_wps_ie(hapd);
881 }
882
883
884 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
885 {
886         const u8 **uuid = ctx;
887         size_t j;
888
889         if (iface == NULL)
890                 return 0;
891         for (j = 0; j < iface->num_bss; j++) {
892                 struct hostapd_data *hapd = iface->bss[j];
893                 if (hapd->wps && !hapd->conf->wps_independent &&
894                     !is_nil_uuid(hapd->wps->uuid)) {
895                         *uuid = hapd->wps->uuid;
896                         return 1;
897                 }
898         }
899
900         return 0;
901 }
902
903
904 static const u8 * get_own_uuid(struct hostapd_iface *iface)
905 {
906         const u8 *uuid;
907         if (iface->interfaces == NULL ||
908             iface->interfaces->for_each_interface == NULL)
909                 return NULL;
910         uuid = NULL;
911         iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
912                                               &uuid);
913         return uuid;
914 }
915
916
917 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
918 {
919         int *count= ctx;
920         (*count)++;
921         return 0;
922 }
923
924
925 static int interface_count(struct hostapd_iface *iface)
926 {
927         int count = 0;
928         if (iface->interfaces == NULL ||
929             iface->interfaces->for_each_interface == NULL)
930                 return 0;
931         iface->interfaces->for_each_interface(iface->interfaces,
932                                               count_interface_cb, &count);
933         return count;
934 }
935
936
937 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
938                                       struct wps_context *wps)
939 {
940         int i;
941
942         for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
943                 wpabuf_free(wps->dev.vendor_ext[i]);
944                 wps->dev.vendor_ext[i] = NULL;
945
946                 if (hapd->conf->wps_vendor_ext[i] == NULL)
947                         continue;
948
949                 wps->dev.vendor_ext[i] =
950                         wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
951                 if (wps->dev.vendor_ext[i] == NULL) {
952                         while (--i >= 0)
953                                 wpabuf_free(wps->dev.vendor_ext[i]);
954                         return -1;
955                 }
956         }
957
958         return 0;
959 }
960
961
962 static void hostapd_free_wps(struct wps_context *wps)
963 {
964         int i;
965
966         for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
967                 wpabuf_free(wps->dev.vendor_ext[i]);
968         wps_device_data_free(&wps->dev);
969         os_free(wps->network_key);
970         hostapd_wps_nfc_clear(wps);
971         wpabuf_free(wps->dh_pubkey);
972         wpabuf_free(wps->dh_privkey);
973         os_free(wps);
974 }
975
976
977 int hostapd_init_wps(struct hostapd_data *hapd,
978                      struct hostapd_bss_config *conf)
979 {
980         struct wps_context *wps;
981         struct wps_registrar_config cfg;
982
983         if (conf->wps_state == 0) {
984                 hostapd_wps_clear_ies(hapd, 0);
985                 return 0;
986         }
987
988         wps = os_zalloc(sizeof(*wps));
989         if (wps == NULL)
990                 return -1;
991
992         wps->cred_cb = hostapd_wps_cred_cb;
993         wps->event_cb = hostapd_wps_event_cb;
994         wps->rf_band_cb = hostapd_wps_rf_band_cb;
995         wps->cb_ctx = hapd;
996
997         os_memset(&cfg, 0, sizeof(cfg));
998         wps->wps_state = hapd->conf->wps_state;
999         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
1000         if (is_nil_uuid(hapd->conf->uuid)) {
1001                 const u8 *uuid;
1002                 uuid = get_own_uuid(hapd->iface);
1003                 if (uuid && !conf->wps_independent) {
1004                         os_memcpy(wps->uuid, uuid, UUID_LEN);
1005                         wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
1006                                     "interface", wps->uuid, UUID_LEN);
1007                 } else {
1008                         uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
1009                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1010                                     "address", wps->uuid, UUID_LEN);
1011                 }
1012         } else {
1013                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
1014                 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
1015                             wps->uuid, UUID_LEN);
1016         }
1017         wps->ssid_len = hapd->conf->ssid.ssid_len;
1018         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1019         wps->ap = 1;
1020         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1021         wps->dev.device_name = hapd->conf->device_name ?
1022                 os_strdup(hapd->conf->device_name) : NULL;
1023         wps->dev.manufacturer = hapd->conf->manufacturer ?
1024                 os_strdup(hapd->conf->manufacturer) : NULL;
1025         wps->dev.model_name = hapd->conf->model_name ?
1026                 os_strdup(hapd->conf->model_name) : NULL;
1027         wps->dev.model_number = hapd->conf->model_number ?
1028                 os_strdup(hapd->conf->model_number) : NULL;
1029         wps->dev.serial_number = hapd->conf->serial_number ?
1030                 os_strdup(hapd->conf->serial_number) : NULL;
1031         wps->config_methods =
1032                 wps_config_methods_str2bin(hapd->conf->config_methods);
1033         if ((wps->config_methods &
1034              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1035               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1036                 wpa_printf(MSG_INFO, "WPS: Converting display to "
1037                            "virtual_display for WPS 2.0 compliance");
1038                 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1039         }
1040         if ((wps->config_methods &
1041              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1042               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1043                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1044                            "virtual_push_button for WPS 2.0 compliance");
1045                 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1046         }
1047         os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1048                   WPS_DEV_TYPE_LEN);
1049
1050         if (hostapd_wps_set_vendor_ext(hapd, wps) < 0)
1051                 goto fail;
1052
1053         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
1054
1055         if (conf->wps_rf_bands) {
1056                 wps->dev.rf_bands = conf->wps_rf_bands;
1057         } else {
1058                 wps->dev.rf_bands =
1059                         hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
1060                         WPS_RF_50GHZ :
1061                         hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
1062                         WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
1063         }
1064
1065         if (conf->wpa & WPA_PROTO_RSN) {
1066                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1067                         wps->auth_types |= WPS_AUTH_WPA2PSK;
1068                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1069                         wps->auth_types |= WPS_AUTH_WPA2;
1070
1071                 if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP))
1072                         wps->encr_types |= WPS_ENCR_AES;
1073                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
1074                         wps->encr_types |= WPS_ENCR_TKIP;
1075         }
1076
1077         if (conf->wpa & WPA_PROTO_WPA) {
1078                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1079                         wps->auth_types |= WPS_AUTH_WPAPSK;
1080                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1081                         wps->auth_types |= WPS_AUTH_WPA;
1082
1083                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
1084                         wps->encr_types |= WPS_ENCR_AES;
1085                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
1086                         wps->encr_types |= WPS_ENCR_TKIP;
1087         }
1088
1089         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1090                 wps->encr_types |= WPS_ENCR_NONE;
1091                 wps->auth_types |= WPS_AUTH_OPEN;
1092         }
1093
1094         if (conf->ssid.wpa_psk_file) {
1095                 /* Use per-device PSKs */
1096         } else if (conf->ssid.wpa_passphrase) {
1097                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1098                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1099         } else if (conf->ssid.wpa_psk) {
1100                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
1101                 if (wps->network_key == NULL)
1102                         goto fail;
1103                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1104                                  conf->ssid.wpa_psk->psk, PMK_LEN);
1105                 wps->network_key_len = 2 * PMK_LEN;
1106         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1107                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1108                 if (wps->network_key == NULL)
1109                         goto fail;
1110                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1111                           conf->ssid.wep.len[0]);
1112                 wps->network_key_len = conf->ssid.wep.len[0];
1113         }
1114
1115         if (conf->ssid.wpa_psk) {
1116                 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1117                 wps->psk_set = 1;
1118         }
1119
1120         wps->ap_auth_type = wps->auth_types;
1121         wps->ap_encr_type = wps->encr_types;
1122         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1123                 /* Override parameters to enable security by default */
1124                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1125                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1126         }
1127
1128         wps->ap_settings = conf->ap_settings;
1129         wps->ap_settings_len = conf->ap_settings_len;
1130
1131         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1132         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1133         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1134         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1135         cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1136         cfg.cb_ctx = hapd;
1137         cfg.skip_cred_build = conf->skip_cred_build;
1138         cfg.extra_cred = conf->extra_cred;
1139         cfg.extra_cred_len = conf->extra_cred_len;
1140         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1141                 conf->skip_cred_build;
1142         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
1143                 cfg.static_wep_only = 1;
1144         cfg.dualband = interface_count(hapd->iface) > 1;
1145         if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1146             (WPS_RF_50GHZ | WPS_RF_24GHZ))
1147                 cfg.dualband = 1;
1148         if (cfg.dualband)
1149                 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
1150         cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
1151
1152         wps->registrar = wps_registrar_init(wps, &cfg);
1153         if (wps->registrar == NULL) {
1154                 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
1155                 goto fail;
1156         }
1157
1158 #ifdef CONFIG_WPS_UPNP
1159         wps->friendly_name = hapd->conf->friendly_name;
1160         wps->manufacturer_url = hapd->conf->manufacturer_url;
1161         wps->model_description = hapd->conf->model_description;
1162         wps->model_url = hapd->conf->model_url;
1163         wps->upc = hapd->conf->upc;
1164 #endif /* CONFIG_WPS_UPNP */
1165
1166         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1167
1168         hapd->wps = wps;
1169
1170         return 0;
1171
1172 fail:
1173         hostapd_free_wps(wps);
1174         return -1;
1175 }
1176
1177
1178 int hostapd_init_wps_complete(struct hostapd_data *hapd)
1179 {
1180         struct wps_context *wps = hapd->wps;
1181
1182         if (wps == NULL)
1183                 return 0;
1184
1185 #ifdef CONFIG_WPS_UPNP
1186         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1187                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1188                 wps_registrar_deinit(wps->registrar);
1189                 hostapd_free_wps(wps);
1190                 hapd->wps = NULL;
1191                 return -1;
1192         }
1193 #endif /* CONFIG_WPS_UPNP */
1194
1195         return 0;
1196 }
1197
1198
1199 static void hostapd_wps_nfc_clear(struct wps_context *wps)
1200 {
1201 #ifdef CONFIG_WPS_NFC
1202         wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
1203         wps->ap_nfc_dev_pw_id = 0;
1204         wpabuf_free(wps->ap_nfc_dh_pubkey);
1205         wps->ap_nfc_dh_pubkey = NULL;
1206         wpabuf_free(wps->ap_nfc_dh_privkey);
1207         wps->ap_nfc_dh_privkey = NULL;
1208         wpabuf_free(wps->ap_nfc_dev_pw);
1209         wps->ap_nfc_dev_pw = NULL;
1210 #endif /* CONFIG_WPS_NFC */
1211 }
1212
1213
1214 void hostapd_deinit_wps(struct hostapd_data *hapd)
1215 {
1216         eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1217         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1218         eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
1219         if (hapd->wps == NULL) {
1220                 hostapd_wps_clear_ies(hapd, 1);
1221                 return;
1222         }
1223 #ifdef CONFIG_WPS_UPNP
1224         hostapd_wps_upnp_deinit(hapd);
1225 #endif /* CONFIG_WPS_UPNP */
1226         wps_registrar_deinit(hapd->wps->registrar);
1227         wps_free_pending_msgs(hapd->wps->upnp_msgs);
1228         hostapd_free_wps(hapd->wps);
1229         hapd->wps = NULL;
1230         hostapd_wps_clear_ies(hapd, 1);
1231 }
1232
1233
1234 void hostapd_update_wps(struct hostapd_data *hapd)
1235 {
1236         if (hapd->wps == NULL)
1237                 return;
1238
1239 #ifdef CONFIG_WPS_UPNP
1240         hapd->wps->friendly_name = hapd->conf->friendly_name;
1241         hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1242         hapd->wps->model_description = hapd->conf->model_description;
1243         hapd->wps->model_url = hapd->conf->model_url;
1244         hapd->wps->upc = hapd->conf->upc;
1245 #endif /* CONFIG_WPS_UPNP */
1246
1247         hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1248
1249         if (hapd->conf->wps_state)
1250                 wps_registrar_update_ie(hapd->wps->registrar);
1251         else
1252                 hostapd_deinit_wps(hapd);
1253 }
1254
1255
1256 struct wps_add_pin_data {
1257         const u8 *addr;
1258         const u8 *uuid;
1259         const u8 *pin;
1260         size_t pin_len;
1261         int timeout;
1262         int added;
1263 };
1264
1265
1266 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1267 {
1268         struct wps_add_pin_data *data = ctx;
1269         int ret;
1270
1271         if (hapd->wps == NULL)
1272                 return 0;
1273         ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1274                                     data->uuid, data->pin, data->pin_len,
1275                                     data->timeout);
1276         if (ret == 0)
1277                 data->added++;
1278         return ret;
1279 }
1280
1281
1282 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1283                         const char *uuid, const char *pin, int timeout)
1284 {
1285         u8 u[UUID_LEN];
1286         struct wps_add_pin_data data;
1287
1288         data.addr = addr;
1289         data.uuid = u;
1290         data.pin = (const u8 *) pin;
1291         data.pin_len = os_strlen(pin);
1292         data.timeout = timeout;
1293         data.added = 0;
1294
1295         if (os_strcmp(uuid, "any") == 0)
1296                 data.uuid = NULL;
1297         else {
1298                 if (uuid_str2bin(uuid, u))
1299                         return -1;
1300                 data.uuid = u;
1301         }
1302         if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1303                 return -1;
1304         return data.added ? 0 : -1;
1305 }
1306
1307
1308 struct wps_button_pushed_ctx {
1309         const u8 *p2p_dev_addr;
1310         unsigned int count;
1311 };
1312
1313 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1314 {
1315         struct wps_button_pushed_ctx *data = ctx;
1316
1317         if (hapd->wps) {
1318                 data->count++;
1319                 return wps_registrar_button_pushed(hapd->wps->registrar,
1320                                                    data->p2p_dev_addr);
1321         }
1322
1323         return 0;
1324 }
1325
1326
1327 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1328                               const u8 *p2p_dev_addr)
1329 {
1330         struct wps_button_pushed_ctx ctx;
1331         int ret;
1332
1333         os_memset(&ctx, 0, sizeof(ctx));
1334         ctx.p2p_dev_addr = p2p_dev_addr;
1335         ret = hostapd_wps_for_each(hapd, wps_button_pushed, &ctx);
1336         if (ret == 0 && !ctx.count)
1337                 ret = -1;
1338         return ret;
1339 }
1340
1341
1342 struct wps_cancel_ctx {
1343         unsigned int count;
1344 };
1345
1346 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1347 {
1348         struct wps_cancel_ctx *data = ctx;
1349
1350         if (hapd->wps) {
1351                 data->count++;
1352                 wps_registrar_wps_cancel(hapd->wps->registrar);
1353                 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1354         }
1355
1356         return 0;
1357 }
1358
1359
1360 int hostapd_wps_cancel(struct hostapd_data *hapd)
1361 {
1362         struct wps_cancel_ctx ctx;
1363         int ret;
1364
1365         os_memset(&ctx, 0, sizeof(ctx));
1366         ret = hostapd_wps_for_each(hapd, wps_cancel, &ctx);
1367         if (ret == 0 && !ctx.count)
1368                 ret = -1;
1369         return ret;
1370 }
1371
1372
1373 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1374                                     const u8 *bssid,
1375                                     const u8 *ie, size_t ie_len,
1376                                     int ssi_signal)
1377 {
1378         struct hostapd_data *hapd = ctx;
1379         struct wpabuf *wps_ie;
1380         struct ieee802_11_elems elems;
1381
1382         if (hapd->wps == NULL)
1383                 return 0;
1384
1385         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1386                 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1387                            MACSTR, MAC2STR(addr));
1388                 return 0;
1389         }
1390
1391         if (elems.ssid && elems.ssid_len > 0 &&
1392             (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1393              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1394              0))
1395                 return 0; /* Not for us */
1396
1397         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1398         if (wps_ie == NULL)
1399                 return 0;
1400         if (wps_validate_probe_req(wps_ie, addr) < 0) {
1401                 wpabuf_free(wps_ie);
1402                 return 0;
1403         }
1404
1405         if (wpabuf_len(wps_ie) > 0) {
1406                 int p2p_wildcard = 0;
1407 #ifdef CONFIG_P2P
1408                 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1409                     os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1410                               P2P_WILDCARD_SSID_LEN) == 0)
1411                         p2p_wildcard = 1;
1412 #endif /* CONFIG_P2P */
1413                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1414                                            p2p_wildcard);
1415 #ifdef CONFIG_WPS_UPNP
1416                 /* FIX: what exactly should be included in the WLANEvent?
1417                  * WPS attributes? Full ProbeReq frame? */
1418                 if (!p2p_wildcard)
1419                         upnp_wps_device_send_wlan_event(
1420                                 hapd->wps_upnp, addr,
1421                                 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1422 #endif /* CONFIG_WPS_UPNP */
1423         }
1424
1425         wpabuf_free(wps_ie);
1426
1427         return 0;
1428 }
1429
1430
1431 #ifdef CONFIG_WPS_UPNP
1432
1433 static int hostapd_rx_req_put_wlan_response(
1434         void *priv, enum upnp_wps_wlanevent_type ev_type,
1435         const u8 *mac_addr, const struct wpabuf *msg,
1436         enum wps_msg_type msg_type)
1437 {
1438         struct hostapd_data *hapd = priv;
1439         struct sta_info *sta;
1440         struct upnp_pending_message *p;
1441
1442         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1443                    MACSTR, ev_type, MAC2STR(mac_addr));
1444         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1445                     wpabuf_head(msg), wpabuf_len(msg));
1446         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1447                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1448                            "PutWLANResponse WLANEventType %d", ev_type);
1449                 return -1;
1450         }
1451
1452         /*
1453          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1454          * server implementation for delivery to the peer.
1455          */
1456
1457         sta = ap_get_sta(hapd, mac_addr);
1458 #ifndef CONFIG_WPS_STRICT
1459         if (!sta) {
1460                 /*
1461                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1462                  * Pick STA that is in an ongoing WPS registration without
1463                  * checking the MAC address.
1464                  */
1465                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1466                            "on NewWLANEventMAC; try wildcard match");
1467                 for (sta = hapd->sta_list; sta; sta = sta->next) {
1468                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1469                                 break;
1470                 }
1471         }
1472 #endif /* CONFIG_WPS_STRICT */
1473
1474         if (!sta || !(sta->flags & WLAN_STA_WPS)) {
1475                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1476                 return 0;
1477         }
1478
1479         if (!sta->eapol_sm) {
1480                 /*
1481                  * This can happen, e.g., if an ER sends an extra message after
1482                  * the station has disassociated (but not fully
1483                  * deauthenticated).
1484                  */
1485                 wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
1486                 return 0;
1487         }
1488
1489         p = os_zalloc(sizeof(*p));
1490         if (p == NULL)
1491                 return -1;
1492         os_memcpy(p->addr, sta->addr, ETH_ALEN);
1493         p->msg = wpabuf_dup(msg);
1494         p->type = msg_type;
1495         p->next = hapd->wps->upnp_msgs;
1496         hapd->wps->upnp_msgs = p;
1497
1498         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1499 }
1500
1501
1502 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1503                                  struct wps_context *wps)
1504 {
1505         struct upnp_wps_device_ctx *ctx;
1506
1507         if (!hapd->conf->upnp_iface)
1508                 return 0;
1509         ctx = os_zalloc(sizeof(*ctx));
1510         if (ctx == NULL)
1511                 return -1;
1512
1513         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1514         if (hapd->conf->ap_pin)
1515                 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1516
1517         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1518                                               hapd->conf->upnp_iface);
1519         if (hapd->wps_upnp == NULL)
1520                 return -1;
1521         wps->wps_upnp = hapd->wps_upnp;
1522
1523         return 0;
1524 }
1525
1526
1527 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1528 {
1529         upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1530 }
1531
1532 #endif /* CONFIG_WPS_UPNP */
1533
1534
1535 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1536                             char *buf, size_t buflen)
1537 {
1538         if (hapd->wps == NULL)
1539                 return 0;
1540         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1541 }
1542
1543
1544 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1545 {
1546         struct hostapd_data *hapd = eloop_data;
1547         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1548         hostapd_wps_ap_pin_disable(hapd);
1549         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
1550 }
1551
1552
1553 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1554 {
1555         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1556         hapd->ap_pin_failures = 0;
1557         hapd->ap_pin_failures_consecutive = 0;
1558         hapd->conf->ap_setup_locked = 0;
1559         if (hapd->wps->ap_setup_locked) {
1560                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1561                 hapd->wps->ap_setup_locked = 0;
1562                 wps_registrar_update_ie(hapd->wps->registrar);
1563         }
1564         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1565         if (timeout > 0)
1566                 eloop_register_timeout(timeout, 0,
1567                                        hostapd_wps_ap_pin_timeout, hapd, NULL);
1568 }
1569
1570
1571 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1572 {
1573         os_free(hapd->conf->ap_pin);
1574         hapd->conf->ap_pin = NULL;
1575 #ifdef CONFIG_WPS_UPNP
1576         upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1577 #endif /* CONFIG_WPS_UPNP */
1578         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1579         return 0;
1580 }
1581
1582
1583 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1584 {
1585         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1586         hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1587 }
1588
1589
1590 struct wps_ap_pin_data {
1591         char pin_txt[9];
1592         int timeout;
1593 };
1594
1595
1596 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1597 {
1598         struct wps_ap_pin_data *data = ctx;
1599
1600         if (!hapd->wps)
1601                 return 0;
1602
1603         os_free(hapd->conf->ap_pin);
1604         hapd->conf->ap_pin = os_strdup(data->pin_txt);
1605 #ifdef CONFIG_WPS_UPNP
1606         upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1607 #endif /* CONFIG_WPS_UPNP */
1608         hostapd_wps_ap_pin_enable(hapd, data->timeout);
1609         return 0;
1610 }
1611
1612
1613 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1614 {
1615         unsigned int pin;
1616         struct wps_ap_pin_data data;
1617
1618         pin = wps_generate_pin();
1619         os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
1620         data.timeout = timeout;
1621         hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1622         return hapd->conf->ap_pin;
1623 }
1624
1625
1626 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1627 {
1628         return hapd->conf->ap_pin;
1629 }
1630
1631
1632 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1633                            int timeout)
1634 {
1635         struct wps_ap_pin_data data;
1636         int ret;
1637
1638         ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1639         if (os_snprintf_error(sizeof(data.pin_txt), ret))
1640                 return -1;
1641         data.timeout = timeout;
1642         return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1643 }
1644
1645
1646 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1647 {
1648         if (hapd->wps)
1649                 wps_registrar_update_ie(hapd->wps->registrar);
1650         return 0;
1651 }
1652
1653
1654 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1655 {
1656         hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1657 }
1658
1659
1660 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1661                           const char *auth, const char *encr, const char *key)
1662 {
1663         struct wps_credential cred;
1664         size_t len;
1665
1666         os_memset(&cred, 0, sizeof(cred));
1667
1668         len = os_strlen(ssid);
1669         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1670             hexstr2bin(ssid, cred.ssid, len / 2))
1671                 return -1;
1672         cred.ssid_len = len / 2;
1673
1674         if (os_strncmp(auth, "OPEN", 4) == 0)
1675                 cred.auth_type = WPS_AUTH_OPEN;
1676         else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1677                 cred.auth_type = WPS_AUTH_WPAPSK;
1678         else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1679                 cred.auth_type = WPS_AUTH_WPA2PSK;
1680         else
1681                 return -1;
1682
1683         if (encr) {
1684                 if (os_strncmp(encr, "NONE", 4) == 0)
1685                         cred.encr_type = WPS_ENCR_NONE;
1686                 else if (os_strncmp(encr, "TKIP", 4) == 0)
1687                         cred.encr_type = WPS_ENCR_TKIP;
1688                 else if (os_strncmp(encr, "CCMP", 4) == 0)
1689                         cred.encr_type = WPS_ENCR_AES;
1690                 else
1691                         return -1;
1692         } else
1693                 cred.encr_type = WPS_ENCR_NONE;
1694
1695         if (key) {
1696                 len = os_strlen(key);
1697                 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1698                     hexstr2bin(key, cred.key, len / 2))
1699                         return -1;
1700                 cred.key_len = len / 2;
1701         }
1702
1703         return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1704 }
1705
1706
1707 #ifdef CONFIG_WPS_NFC
1708
1709 struct wps_nfc_password_token_data {
1710         const u8 *oob_dev_pw;
1711         size_t oob_dev_pw_len;
1712         int added;
1713 };
1714
1715
1716 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1717 {
1718         struct wps_nfc_password_token_data *data = ctx;
1719         int ret;
1720
1721         if (hapd->wps == NULL)
1722                 return 0;
1723         ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1724                                                    data->oob_dev_pw,
1725                                                    data->oob_dev_pw_len);
1726         if (ret == 0)
1727                 data->added++;
1728         return ret;
1729 }
1730
1731
1732 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1733                                               struct wps_parse_attr *attr)
1734 {
1735         struct wps_nfc_password_token_data data;
1736
1737         data.oob_dev_pw = attr->oob_dev_password;
1738         data.oob_dev_pw_len = attr->oob_dev_password_len;
1739         data.added = 0;
1740         if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1741                 return -1;
1742         return data.added ? 0 : -1;
1743 }
1744
1745
1746 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1747                                        const struct wpabuf *wps)
1748 {
1749         struct wps_parse_attr attr;
1750
1751         wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1752
1753         if (wps_parse_msg(wps, &attr)) {
1754                 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1755                 return -1;
1756         }
1757
1758         if (attr.oob_dev_password)
1759                 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1760
1761         wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1762         return -1;
1763 }
1764
1765
1766 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1767                              const struct wpabuf *data)
1768 {
1769         const struct wpabuf *wps = data;
1770         struct wpabuf *tmp = NULL;
1771         int ret;
1772
1773         if (wpabuf_len(data) < 4)
1774                 return -1;
1775
1776         if (*wpabuf_head_u8(data) != 0x10) {
1777                 /* Assume this contains full NDEF record */
1778                 tmp = ndef_parse_wifi(data);
1779                 if (tmp == NULL) {
1780                         wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1781                         return -1;
1782                 }
1783                 wps = tmp;
1784         }
1785
1786         ret = hostapd_wps_nfc_tag_process(hapd, wps);
1787         wpabuf_free(tmp);
1788         return ret;
1789 }
1790
1791
1792 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1793                                              int ndef)
1794 {
1795         struct wpabuf *ret;
1796
1797         if (hapd->wps == NULL)
1798                 return NULL;
1799
1800         ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
1801                                hapd->iconf->channel);
1802         if (ndef && ret) {
1803                 struct wpabuf *tmp;
1804                 tmp = ndef_build_wifi(ret);
1805                 wpabuf_free(ret);
1806                 if (tmp == NULL)
1807                         return NULL;
1808                 ret = tmp;
1809         }
1810
1811         return ret;
1812 }
1813
1814
1815 struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
1816 {
1817         struct wpabuf *ret;
1818
1819         if (hapd->wps == NULL)
1820                 return NULL;
1821
1822         if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
1823                 struct wps_context *wps = hapd->wps;
1824                 if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
1825                                    &hapd->conf->wps_nfc_dh_privkey) < 0)
1826                         return NULL;
1827                 hostapd_wps_nfc_clear(wps);
1828                 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
1829                 wps->ap_nfc_dh_pubkey =
1830                         wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1831                 wps->ap_nfc_dh_privkey =
1832                         wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1833                 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
1834                         hostapd_wps_nfc_clear(wps);
1835                         return NULL;
1836                 }
1837         }
1838
1839         ret = wps_build_nfc_handover_sel(hapd->wps,
1840                                          hapd->conf->wps_nfc_dh_pubkey,
1841                                          hapd->own_addr, hapd->iface->freq);
1842
1843         if (ndef && ret) {
1844                 struct wpabuf *tmp;
1845                 tmp = ndef_build_wifi(ret);
1846                 wpabuf_free(ret);
1847                 if (tmp == NULL)
1848                         return NULL;
1849                 ret = tmp;
1850         }
1851
1852         return ret;
1853 }
1854
1855
1856 int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
1857                                     const struct wpabuf *req,
1858                                     const struct wpabuf *sel)
1859 {
1860         struct wpabuf *wps;
1861         int ret = -1;
1862         u16 wsc_len;
1863         const u8 *pos;
1864         struct wpabuf msg;
1865         struct wps_parse_attr attr;
1866         u16 dev_pw_id;
1867
1868         /*
1869          * Enrollee/station is always initiator of the NFC connection handover,
1870          * so use the request message here to find Enrollee public key hash.
1871          */
1872         wps = ndef_parse_wifi(req);
1873         if (wps == NULL)
1874                 return -1;
1875         wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
1876                    "payload from NFC connection handover");
1877         wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
1878         if (wpabuf_len(wps) < 2) {
1879                 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
1880                            "Message");
1881                 goto out;
1882         }
1883         pos = wpabuf_head(wps);
1884         wsc_len = WPA_GET_BE16(pos);
1885         if (wsc_len > wpabuf_len(wps) - 2) {
1886                 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
1887                            "in rt Wi-Fi Handover Request Message", wsc_len);
1888                 goto out;
1889         }
1890         pos += 2;
1891
1892         wpa_hexdump(MSG_DEBUG,
1893                     "WPS: WSC attributes in Wi-Fi Handover Request Message",
1894                     pos, wsc_len);
1895         if (wsc_len < wpabuf_len(wps) - 2) {
1896                 wpa_hexdump(MSG_DEBUG,
1897                             "WPS: Ignore extra data after WSC attributes",
1898                             pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
1899         }
1900
1901         wpabuf_set(&msg, pos, wsc_len);
1902         ret = wps_parse_msg(&msg, &attr);
1903         if (ret < 0) {
1904                 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
1905                            "Wi-Fi Handover Request Message");
1906                 goto out;
1907         }
1908
1909         if (attr.oob_dev_password == NULL ||
1910             attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
1911                 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
1912                            "included in Wi-Fi Handover Request Message");
1913                 ret = -1;
1914                 goto out;
1915         }
1916
1917         if (attr.uuid_e == NULL) {
1918                 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
1919                            "Handover Request Message");
1920                 ret = -1;
1921                 goto out;
1922         }
1923
1924         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
1925
1926         wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
1927                     attr.oob_dev_password, attr.oob_dev_password_len);
1928         dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
1929                                  WPS_OOB_PUBKEY_HASH_LEN);
1930         if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
1931                 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
1932                            "%u in Wi-Fi Handover Request Message", dev_pw_id);
1933                 ret = -1;
1934                 goto out;
1935         }
1936         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
1937                     attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
1938
1939         ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
1940                                              attr.oob_dev_password,
1941                                              DEV_PW_NFC_CONNECTION_HANDOVER,
1942                                              NULL, 0, 1);
1943
1944 out:
1945         wpabuf_free(wps);
1946         return ret;
1947 }
1948
1949
1950 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1951 {
1952         if (hapd->conf->wps_nfc_pw_from_config) {
1953                 return wps_nfc_token_build(ndef,
1954                                            hapd->conf->wps_nfc_dev_pw_id,
1955                                            hapd->conf->wps_nfc_dh_pubkey,
1956                                            hapd->conf->wps_nfc_dev_pw);
1957         }
1958
1959         return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1960                                  &hapd->conf->wps_nfc_dh_pubkey,
1961                                  &hapd->conf->wps_nfc_dh_privkey,
1962                                  &hapd->conf->wps_nfc_dev_pw);
1963 }
1964
1965
1966 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1967 {
1968         struct wps_context *wps = hapd->wps;
1969         struct wpabuf *pw;
1970
1971         if (wps == NULL)
1972                 return -1;
1973
1974         if (!hapd->conf->wps_nfc_dh_pubkey ||
1975             !hapd->conf->wps_nfc_dh_privkey ||
1976             !hapd->conf->wps_nfc_dev_pw ||
1977             !hapd->conf->wps_nfc_dev_pw_id)
1978                 return -1;
1979
1980         hostapd_wps_nfc_clear(wps);
1981         wpa_printf(MSG_DEBUG,
1982                    "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
1983                    hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
1984         wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1985         wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1986         wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1987         pw = hapd->conf->wps_nfc_dev_pw;
1988         wps->ap_nfc_dev_pw = wpabuf_alloc(
1989                 wpabuf_len(pw) * 2 + 1);
1990         if (wps->ap_nfc_dev_pw) {
1991                 wpa_snprintf_hex_uppercase(
1992                         (char *) wpabuf_put(wps->ap_nfc_dev_pw,
1993                                             wpabuf_len(pw) * 2),
1994                         wpabuf_len(pw) * 2 + 1,
1995                         wpabuf_head(pw), wpabuf_len(pw));
1996         }
1997
1998         if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
1999             !wps->ap_nfc_dev_pw) {
2000                 hostapd_wps_nfc_clear(wps);
2001                 return -1;
2002         }
2003
2004         return 0;
2005 }
2006
2007
2008 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
2009 {
2010         wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
2011                    hapd->conf->iface);
2012         hostapd_wps_nfc_clear(hapd->wps);
2013 }
2014
2015 #endif /* CONFIG_WPS_NFC */