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