remove @EAP_LDFLAGS@, no longer exists
[mech_eap.orig] / libeap / src / ap / wps_hostapd.c
1 /*
2  * hostapd / WPS integration
3  * Copyright (c) 2008-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "utils/uuid.h"
20 #include "crypto/dh_groups.h"
21 #include "common/wpa_ctrl.h"
22 #include "common/ieee802_11_defs.h"
23 #include "common/ieee802_11_common.h"
24 #include "eapol_auth/eapol_auth_sm.h"
25 #include "eapol_auth/eapol_auth_sm_i.h"
26 #include "wps/wps.h"
27 #include "wps/wps_defs.h"
28 #include "wps/wps_dev_attr.h"
29 #include "hostapd.h"
30 #include "ap_config.h"
31 #include "beacon.h"
32 #include "sta_info.h"
33 #include "wps_hostapd.h"
34
35
36 #ifdef CONFIG_WPS_UPNP
37 #include "wps/wps_upnp.h"
38 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
39                                  struct wps_context *wps);
40 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
41 #endif /* CONFIG_WPS_UPNP */
42
43 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
44                                     const u8 *ie, size_t ie_len);
45 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
46
47
48 struct wps_for_each_data {
49         int (*func)(struct hostapd_data *h, void *ctx);
50         void *ctx;
51 };
52
53
54 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
55 {
56         struct wps_for_each_data *data = ctx;
57         size_t j;
58
59         if (iface == NULL)
60                 return 0;
61         for (j = 0; j < iface->num_bss; j++) {
62                 struct hostapd_data *hapd = iface->bss[j];
63                 int ret = data->func(hapd, data->ctx);
64                 if (ret)
65                         return ret;
66         }
67
68         return 0;
69 }
70
71
72 static int hostapd_wps_for_each(struct hostapd_data *hapd,
73                                 int (*func)(struct hostapd_data *h, void *ctx),
74                                 void *ctx)
75 {
76         struct hostapd_iface *iface = hapd->iface;
77         struct wps_for_each_data data;
78         data.func = func;
79         data.ctx = ctx;
80         if (iface->for_each_interface == NULL)
81                 return wps_for_each(iface, &data);
82         return iface->for_each_interface(iface->interfaces, wps_for_each,
83                                          &data);
84 }
85
86
87 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
88                                   size_t psk_len)
89 {
90         struct hostapd_data *hapd = ctx;
91         struct hostapd_wpa_psk *p;
92         struct hostapd_ssid *ssid = &hapd->conf->ssid;
93
94         wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
95                    MACSTR, MAC2STR(mac_addr));
96         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
97
98         if (psk_len != PMK_LEN) {
99                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
100                            (unsigned long) psk_len);
101                 return -1;
102         }
103
104         /* Add the new PSK to runtime PSK list */
105         p = os_zalloc(sizeof(*p));
106         if (p == NULL)
107                 return -1;
108         os_memcpy(p->addr, mac_addr, ETH_ALEN);
109         os_memcpy(p->psk, psk, PMK_LEN);
110
111         p->next = ssid->wpa_psk;
112         ssid->wpa_psk = p;
113
114         if (ssid->wpa_psk_file) {
115                 FILE *f;
116                 char hex[PMK_LEN * 2 + 1];
117                 /* Add the new PSK to PSK list file */
118                 f = fopen(ssid->wpa_psk_file, "a");
119                 if (f == NULL) {
120                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
121                                    "'%s'", ssid->wpa_psk_file);
122                         return -1;
123                 }
124
125                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
126                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
127                 fclose(f);
128         }
129
130         return 0;
131 }
132
133
134 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
135                                  struct wpabuf *probe_resp_ie)
136 {
137         struct hostapd_data *hapd = ctx;
138         wpabuf_free(hapd->wps_beacon_ie);
139         hapd->wps_beacon_ie = beacon_ie;
140         wpabuf_free(hapd->wps_probe_resp_ie);
141         hapd->wps_probe_resp_ie = probe_resp_ie;
142         ieee802_11_set_beacon(hapd);
143         return hapd->drv.set_ap_wps_ie(hapd);
144 }
145
146
147 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
148                                       const struct wps_device_data *dev)
149 {
150         struct hostapd_data *hapd = ctx;
151         char uuid[40], txt[400];
152         int len;
153         char devtype[WPS_DEV_TYPE_BUFSIZE];
154         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
155                 return;
156         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
157         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
158                           "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
159                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
160                           dev->manufacturer, dev->model_name,
161                           dev->model_number, dev->serial_number,
162                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
163                                                sizeof(devtype)));
164         if (len > 0 && len < (int) sizeof(txt))
165                 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
166
167         if (hapd->conf->wps_pin_requests) {
168                 FILE *f;
169                 struct os_time t;
170                 f = fopen(hapd->conf->wps_pin_requests, "a");
171                 if (f == NULL)
172                         return;
173                 os_get_time(&t);
174                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
175                         "\t%s\n",
176                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
177                         dev->manufacturer, dev->model_name, dev->model_number,
178                         dev->serial_number,
179                         wps_dev_type_bin2str(dev->pri_dev_type, devtype,
180                                              sizeof(devtype)));
181                 fclose(f);
182         }
183 }
184
185
186 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
187                                        const u8 *uuid_e)
188 {
189         struct hostapd_data *hapd = ctx;
190         char uuid[40];
191         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
192                 return;
193         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
194                 MAC2STR(mac_addr), uuid);
195         if (hapd->wps_reg_success_cb)
196                 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
197                                          mac_addr, uuid_e);
198 }
199
200
201 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
202                                          const u8 *uuid_e,
203                                          const u8 *pri_dev_type,
204                                          u16 config_methods,
205                                          u16 dev_password_id, u8 request_type,
206                                          const char *dev_name)
207 {
208         struct hostapd_data *hapd = ctx;
209         char uuid[40];
210         char devtype[WPS_DEV_TYPE_BUFSIZE];
211         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
212                 return;
213         if (dev_name == NULL)
214                 dev_name = "";
215         wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
216                      " %s %s 0x%x %u %u [%s]",
217                      MAC2STR(addr), uuid,
218                      wps_dev_type_bin2str(pri_dev_type, devtype,
219                                           sizeof(devtype)),
220                      config_methods, dev_password_id, request_type, dev_name);
221 }
222
223
224 static int str_starts(const char *str, const char *start)
225 {
226         return os_strncmp(str, start, os_strlen(start)) == 0;
227 }
228
229
230 static void wps_reload_config(void *eloop_data, void *user_ctx)
231 {
232         struct hostapd_iface *iface = eloop_data;
233
234         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
235         if (iface->reload_config(iface) < 0) {
236                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
237                            "configuration");
238         }
239 }
240
241
242 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
243 {
244         const struct wps_credential *cred = ctx;
245         FILE *oconf, *nconf;
246         size_t len, i;
247         char *tmp_fname;
248         char buf[1024];
249         int multi_bss;
250         int wpa;
251
252         if (hapd->wps == NULL)
253                 return 0;
254
255         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
256                         cred->cred_attr, cred->cred_attr_len);
257
258         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
259         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
260         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
261                    cred->auth_type);
262         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
263         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
264         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
265                         cred->key, cred->key_len);
266         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
267                    MAC2STR(cred->mac_addr));
268
269         if ((hapd->conf->wps_cred_processing == 1 ||
270              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
271                 size_t blen = cred->cred_attr_len * 2 + 1;
272                 char *_buf = os_malloc(blen);
273                 if (_buf) {
274                         wpa_snprintf_hex(_buf, blen,
275                                          cred->cred_attr, cred->cred_attr_len);
276                         wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
277                                 WPS_EVENT_NEW_AP_SETTINGS, _buf);
278                         os_free(_buf);
279                 }
280         } else
281                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
282
283         if (hapd->conf->wps_cred_processing == 1)
284                 return 0;
285
286         os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
287         hapd->wps->ssid_len = cred->ssid_len;
288         hapd->wps->encr_types = cred->encr_type;
289         hapd->wps->auth_types = cred->auth_type;
290         if (cred->key_len == 0) {
291                 os_free(hapd->wps->network_key);
292                 hapd->wps->network_key = NULL;
293                 hapd->wps->network_key_len = 0;
294         } else {
295                 if (hapd->wps->network_key == NULL ||
296                     hapd->wps->network_key_len < cred->key_len) {
297                         hapd->wps->network_key_len = 0;
298                         os_free(hapd->wps->network_key);
299                         hapd->wps->network_key = os_malloc(cred->key_len);
300                         if (hapd->wps->network_key == NULL)
301                                 return -1;
302                 }
303                 hapd->wps->network_key_len = cred->key_len;
304                 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
305         }
306         hapd->wps->wps_state = WPS_STATE_CONFIGURED;
307
308         len = os_strlen(hapd->iface->config_fname) + 5;
309         tmp_fname = os_malloc(len);
310         if (tmp_fname == NULL)
311                 return -1;
312         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
313
314         oconf = fopen(hapd->iface->config_fname, "r");
315         if (oconf == NULL) {
316                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
317                            "configuration file");
318                 os_free(tmp_fname);
319                 return -1;
320         }
321
322         nconf = fopen(tmp_fname, "w");
323         if (nconf == NULL) {
324                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
325                            "configuration file");
326                 os_free(tmp_fname);
327                 fclose(oconf);
328                 return -1;
329         }
330
331         fprintf(nconf, "# WPS configuration - START\n");
332
333         fprintf(nconf, "wps_state=2\n");
334
335         fprintf(nconf, "ssid=");
336         for (i = 0; i < cred->ssid_len; i++)
337                 fputc(cred->ssid[i], nconf);
338         fprintf(nconf, "\n");
339
340         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
341             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
342                 wpa = 3;
343         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
344                 wpa = 2;
345         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
346                 wpa = 1;
347         else
348                 wpa = 0;
349
350         if (wpa) {
351                 char *prefix;
352                 fprintf(nconf, "wpa=%d\n", wpa);
353
354                 fprintf(nconf, "wpa_key_mgmt=");
355                 prefix = "";
356                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
357                         fprintf(nconf, "WPA-EAP");
358                         prefix = " ";
359                 }
360                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
361                         fprintf(nconf, "%sWPA-PSK", prefix);
362                 fprintf(nconf, "\n");
363
364                 fprintf(nconf, "wpa_pairwise=");
365                 prefix = "";
366                 if (cred->encr_type & WPS_ENCR_AES) {
367                         fprintf(nconf, "CCMP");
368                         prefix = " ";
369                 }
370                 if (cred->encr_type & WPS_ENCR_TKIP) {
371                         fprintf(nconf, "%sTKIP", prefix);
372                 }
373                 fprintf(nconf, "\n");
374
375                 if (cred->key_len >= 8 && cred->key_len < 64) {
376                         fprintf(nconf, "wpa_passphrase=");
377                         for (i = 0; i < cred->key_len; i++)
378                                 fputc(cred->key[i], nconf);
379                         fprintf(nconf, "\n");
380                 } else if (cred->key_len == 64) {
381                         fprintf(nconf, "wpa_psk=");
382                         for (i = 0; i < cred->key_len; i++)
383                                 fputc(cred->key[i], nconf);
384                         fprintf(nconf, "\n");
385                 } else {
386                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
387                                    "for WPA/WPA2",
388                                    (unsigned long) cred->key_len);
389                 }
390
391                 fprintf(nconf, "auth_algs=1\n");
392         } else {
393                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
394                     (cred->auth_type & WPS_AUTH_SHARED))
395                         fprintf(nconf, "auth_algs=3\n");
396                 else if (cred->auth_type & WPS_AUTH_SHARED)
397                         fprintf(nconf, "auth_algs=2\n");
398                 else
399                         fprintf(nconf, "auth_algs=1\n");
400
401                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
402                         int key_idx = cred->key_idx;
403                         if (key_idx)
404                                 key_idx--;
405                         fprintf(nconf, "wep_default_key=%d\n", key_idx);
406                         fprintf(nconf, "wep_key%d=", key_idx);
407                         if (cred->key_len == 10 || cred->key_len == 26) {
408                                 /* WEP key as a hex string */
409                                 for (i = 0; i < cred->key_len; i++)
410                                         fputc(cred->key[i], nconf);
411                         } else {
412                                 /* Raw WEP key; convert to hex */
413                                 for (i = 0; i < cred->key_len; i++)
414                                         fprintf(nconf, "%02x", cred->key[i]);
415                         }
416                         fprintf(nconf, "\n");
417                 }
418         }
419
420         fprintf(nconf, "# WPS configuration - END\n");
421
422         multi_bss = 0;
423         while (fgets(buf, sizeof(buf), oconf)) {
424                 if (os_strncmp(buf, "bss=", 4) == 0)
425                         multi_bss = 1;
426                 if (!multi_bss &&
427                     (str_starts(buf, "ssid=") ||
428                      str_starts(buf, "auth_algs=") ||
429                      str_starts(buf, "wps_state=") ||
430                      str_starts(buf, "wpa=") ||
431                      str_starts(buf, "wpa_psk=") ||
432                      str_starts(buf, "wpa_pairwise=") ||
433                      str_starts(buf, "rsn_pairwise=") ||
434                      str_starts(buf, "wpa_key_mgmt=") ||
435                      str_starts(buf, "wpa_passphrase="))) {
436                         fprintf(nconf, "#WPS# %s", buf);
437                 } else
438                         fprintf(nconf, "%s", buf);
439         }
440
441         fclose(nconf);
442         fclose(oconf);
443
444         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
445                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
446                            "configuration file: %s", strerror(errno));
447                 os_free(tmp_fname);
448                 return -1;
449         }
450
451         os_free(tmp_fname);
452
453         /* Schedule configuration reload after short period of time to allow
454          * EAP-WSC to be finished.
455          */
456         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
457                                NULL);
458
459         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
460
461         return 0;
462 }
463
464
465 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
466 {
467         struct hostapd_data *hapd = ctx;
468         return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
469 }
470
471
472 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
473 {
474         struct hostapd_data *hapd = eloop_data;
475
476         if (hapd->conf->ap_setup_locked)
477                 return;
478
479         wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
480         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
481         hapd->wps->ap_setup_locked = 0;
482         wps_registrar_update_ie(hapd->wps->registrar);
483 }
484
485
486 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
487 {
488         struct wps_event_pwd_auth_fail *data = ctx;
489
490         if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
491                 return 0;
492
493         /*
494          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
495          * for some time if this happens multiple times to slow down brute
496          * force attacks.
497          */
498         hapd->ap_pin_failures++;
499         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
500                    hapd->ap_pin_failures);
501         if (hapd->ap_pin_failures < 3)
502                 return 0;
503
504         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
505         hapd->wps->ap_setup_locked = 1;
506
507         wps_registrar_update_ie(hapd->wps->registrar);
508
509         if (!hapd->conf->ap_setup_locked) {
510                 if (hapd->ap_pin_lockout_time == 0)
511                         hapd->ap_pin_lockout_time = 60;
512                 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
513                          (hapd->ap_pin_failures % 3) == 0)
514                         hapd->ap_pin_lockout_time *= 2;
515
516                 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
517                            hapd->ap_pin_lockout_time);
518                 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
519                 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
520                                        hostapd_wps_reenable_ap_pin, hapd,
521                                        NULL);
522         }
523
524         return 0;
525 }
526
527
528 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
529                                   struct wps_event_pwd_auth_fail *data)
530 {
531         hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
532 }
533
534
535 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
536                                    struct wps_event_fail *fail)
537 {
538         wpa_msg(hapd->msg_ctx, MSG_INFO,
539                 WPS_EVENT_FAIL "msg=%d config_error=%d",
540                 fail->msg, fail->config_error);
541 }
542
543
544 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
545                                  union wps_event_data *data)
546 {
547         struct hostapd_data *hapd = ctx;
548
549         switch (event) {
550         case WPS_EV_M2D:
551                 break;
552         case WPS_EV_FAIL:
553                 hostapd_wps_event_fail(hapd, &data->fail);
554                 break;
555         case WPS_EV_SUCCESS:
556                 break;
557         case WPS_EV_PWD_AUTH_FAIL:
558                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
559                 break;
560         case WPS_EV_PBC_OVERLAP:
561                 break;
562         case WPS_EV_PBC_TIMEOUT:
563                 break;
564         case WPS_EV_ER_AP_ADD:
565                 break;
566         case WPS_EV_ER_AP_REMOVE:
567                 break;
568         case WPS_EV_ER_ENROLLEE_ADD:
569                 break;
570         case WPS_EV_ER_ENROLLEE_REMOVE:
571                 break;
572         case WPS_EV_ER_AP_SETTINGS:
573                 break;
574         }
575         if (hapd->wps_event_cb)
576                 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
577 }
578
579
580 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
581 {
582         wpabuf_free(hapd->wps_beacon_ie);
583         hapd->wps_beacon_ie = NULL;
584
585         wpabuf_free(hapd->wps_probe_resp_ie);
586         hapd->wps_probe_resp_ie = NULL;
587
588         hapd->drv.set_ap_wps_ie(hapd);
589 }
590
591
592 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
593 {
594         const u8 **uuid = ctx;
595         size_t j;
596
597         if (iface == NULL)
598                 return 0;
599         for (j = 0; j < iface->num_bss; j++) {
600                 struct hostapd_data *hapd = iface->bss[j];
601                 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
602                         *uuid = hapd->wps->uuid;
603                         return 1;
604                 }
605         }
606
607         return 0;
608 }
609
610
611 static const u8 * get_own_uuid(struct hostapd_iface *iface)
612 {
613         const u8 *uuid;
614         if (iface->for_each_interface == NULL)
615                 return NULL;
616         uuid = NULL;
617         iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid);
618         return uuid;
619 }
620
621
622 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
623 {
624         int *count= ctx;
625         (*count)++;
626         return 0;
627 }
628
629
630 static int interface_count(struct hostapd_iface *iface)
631 {
632         int count = 0;
633         if (iface->for_each_interface == NULL)
634                 return 0;
635         iface->for_each_interface(iface->interfaces, count_interface_cb,
636                                   &count);
637         return count;
638 }
639
640
641 int hostapd_init_wps(struct hostapd_data *hapd,
642                      struct hostapd_bss_config *conf)
643 {
644         struct wps_context *wps;
645         struct wps_registrar_config cfg;
646
647         if (conf->wps_state == 0) {
648                 hostapd_wps_clear_ies(hapd);
649                 return 0;
650         }
651
652         wps = os_zalloc(sizeof(*wps));
653         if (wps == NULL)
654                 return -1;
655
656         wps->cred_cb = hostapd_wps_cred_cb;
657         wps->event_cb = hostapd_wps_event_cb;
658         wps->cb_ctx = hapd;
659
660         os_memset(&cfg, 0, sizeof(cfg));
661         wps->wps_state = hapd->conf->wps_state;
662         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
663         if (is_nil_uuid(hapd->conf->uuid)) {
664                 const u8 *uuid;
665                 uuid = get_own_uuid(hapd->iface);
666                 if (uuid) {
667                         os_memcpy(wps->uuid, uuid, UUID_LEN);
668                         wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
669                                     "interface", wps->uuid, UUID_LEN);
670                 } else {
671                         uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
672                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
673                                     "address", wps->uuid, UUID_LEN);
674                 }
675         } else {
676                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
677                 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
678                             wps->uuid, UUID_LEN);
679         }
680         wps->ssid_len = hapd->conf->ssid.ssid_len;
681         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
682         wps->ap = 1;
683         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
684         wps->dev.device_name = hapd->conf->device_name ?
685                 os_strdup(hapd->conf->device_name) : NULL;
686         wps->dev.manufacturer = hapd->conf->manufacturer ?
687                 os_strdup(hapd->conf->manufacturer) : NULL;
688         wps->dev.model_name = hapd->conf->model_name ?
689                 os_strdup(hapd->conf->model_name) : NULL;
690         wps->dev.model_number = hapd->conf->model_number ?
691                 os_strdup(hapd->conf->model_number) : NULL;
692         wps->dev.serial_number = hapd->conf->serial_number ?
693                 os_strdup(hapd->conf->serial_number) : NULL;
694         wps->config_methods =
695                 wps_config_methods_str2bin(hapd->conf->config_methods);
696 #ifdef CONFIG_WPS2
697         if ((wps->config_methods &
698              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
699               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
700                 wpa_printf(MSG_INFO, "WPS: Converting display to "
701                            "virtual_display for WPS 2.0 compliance");
702                 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
703         }
704         if ((wps->config_methods &
705              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
706               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
707                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
708                            "virtual_push_button for WPS 2.0 compliance");
709                 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
710         }
711 #endif /* CONFIG_WPS2 */
712         if (hapd->conf->device_type &&
713             wps_dev_type_str2bin(hapd->conf->device_type,
714                                  wps->dev.pri_dev_type) < 0) {
715                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
716                 os_free(wps);
717                 return -1;
718         }
719         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
720         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
721                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
722
723         if (conf->wpa & WPA_PROTO_RSN) {
724                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
725                         wps->auth_types |= WPS_AUTH_WPA2PSK;
726                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
727                         wps->auth_types |= WPS_AUTH_WPA2;
728
729                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
730                         wps->encr_types |= WPS_ENCR_AES;
731                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
732                         wps->encr_types |= WPS_ENCR_TKIP;
733         }
734
735         if (conf->wpa & WPA_PROTO_WPA) {
736                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
737                         wps->auth_types |= WPS_AUTH_WPAPSK;
738                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
739                         wps->auth_types |= WPS_AUTH_WPA;
740
741                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
742                         wps->encr_types |= WPS_ENCR_AES;
743                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
744                         wps->encr_types |= WPS_ENCR_TKIP;
745         }
746
747         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
748                 wps->encr_types |= WPS_ENCR_NONE;
749                 wps->auth_types |= WPS_AUTH_OPEN;
750         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
751                 wps->encr_types |= WPS_ENCR_WEP;
752                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
753                         wps->auth_types |= WPS_AUTH_OPEN;
754                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
755                         wps->auth_types |= WPS_AUTH_SHARED;
756         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
757                 wps->auth_types |= WPS_AUTH_OPEN;
758                 if (conf->default_wep_key_len)
759                         wps->encr_types |= WPS_ENCR_WEP;
760                 else
761                         wps->encr_types |= WPS_ENCR_NONE;
762         }
763
764         if (conf->ssid.wpa_psk_file) {
765                 /* Use per-device PSKs */
766         } else if (conf->ssid.wpa_passphrase) {
767                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
768                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
769         } else if (conf->ssid.wpa_psk) {
770                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
771                 if (wps->network_key == NULL) {
772                         os_free(wps);
773                         return -1;
774                 }
775                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
776                                  conf->ssid.wpa_psk->psk, PMK_LEN);
777                 wps->network_key_len = 2 * PMK_LEN;
778         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
779                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
780                 if (wps->network_key == NULL) {
781                         os_free(wps);
782                         return -1;
783                 }
784                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
785                           conf->ssid.wep.len[0]);
786                 wps->network_key_len = conf->ssid.wep.len[0];
787         }
788
789         if (conf->ssid.wpa_psk) {
790                 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
791                 wps->psk_set = 1;
792         }
793
794         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
795                 /* Override parameters to enable security by default */
796                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
797                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
798         }
799
800         wps->ap_settings = conf->ap_settings;
801         wps->ap_settings_len = conf->ap_settings_len;
802
803         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
804         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
805         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
806         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
807         cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
808         cfg.cb_ctx = hapd;
809         cfg.skip_cred_build = conf->skip_cred_build;
810         cfg.extra_cred = conf->extra_cred;
811         cfg.extra_cred_len = conf->extra_cred_len;
812         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
813                 conf->skip_cred_build;
814         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
815                 cfg.static_wep_only = 1;
816         cfg.dualband = interface_count(hapd->iface) > 1;
817         if (cfg.dualband)
818                 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
819
820         wps->registrar = wps_registrar_init(wps, &cfg);
821         if (wps->registrar == NULL) {
822                 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
823                 os_free(wps->network_key);
824                 os_free(wps);
825                 return -1;
826         }
827
828 #ifdef CONFIG_WPS_UPNP
829         wps->friendly_name = hapd->conf->friendly_name;
830         wps->manufacturer_url = hapd->conf->manufacturer_url;
831         wps->model_description = hapd->conf->model_description;
832         wps->model_url = hapd->conf->model_url;
833         wps->upc = hapd->conf->upc;
834
835         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
836                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
837                 wps_registrar_deinit(wps->registrar);
838                 os_free(wps->network_key);
839                 os_free(wps);
840                 return -1;
841         }
842 #endif /* CONFIG_WPS_UPNP */
843
844         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
845
846         hapd->wps = wps;
847
848         return 0;
849 }
850
851
852 void hostapd_deinit_wps(struct hostapd_data *hapd)
853 {
854         eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
855         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
856         if (hapd->wps == NULL)
857                 return;
858 #ifdef CONFIG_WPS_UPNP
859         hostapd_wps_upnp_deinit(hapd);
860 #endif /* CONFIG_WPS_UPNP */
861         wps_registrar_deinit(hapd->wps->registrar);
862         os_free(hapd->wps->network_key);
863         wps_device_data_free(&hapd->wps->dev);
864         wpabuf_free(hapd->wps->dh_pubkey);
865         wpabuf_free(hapd->wps->dh_privkey);
866         wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
867         wpabuf_free(hapd->wps->oob_conf.dev_password);
868         wps_free_pending_msgs(hapd->wps->upnp_msgs);
869         os_free(hapd->wps);
870         hapd->wps = NULL;
871         hostapd_wps_clear_ies(hapd);
872 }
873
874
875 void hostapd_update_wps(struct hostapd_data *hapd)
876 {
877         if (hapd->wps == NULL)
878                 return;
879
880 #ifdef CONFIG_WPS_UPNP
881         hapd->wps->friendly_name = hapd->conf->friendly_name;
882         hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
883         hapd->wps->model_description = hapd->conf->model_description;
884         hapd->wps->model_url = hapd->conf->model_url;
885         hapd->wps->upc = hapd->conf->upc;
886 #endif /* CONFIG_WPS_UPNP */
887
888         if (hapd->conf->wps_state)
889                 wps_registrar_update_ie(hapd->wps->registrar);
890         else
891                 hostapd_deinit_wps(hapd);
892 }
893
894
895 struct wps_add_pin_data {
896         const u8 *addr;
897         const u8 *uuid;
898         const u8 *pin;
899         size_t pin_len;
900         int timeout;
901         int added;
902 };
903
904
905 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
906 {
907         struct wps_add_pin_data *data = ctx;
908         int ret;
909
910         if (hapd->wps == NULL)
911                 return 0;
912         ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
913                                     data->uuid, data->pin, data->pin_len,
914                                     data->timeout);
915         if (ret == 0)
916                 data->added++;
917         return ret;
918 }
919
920
921 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
922                         const char *uuid, const char *pin, int timeout)
923 {
924         u8 u[UUID_LEN];
925         struct wps_add_pin_data data;
926
927         data.addr = addr;
928         data.uuid = u;
929         data.pin = (const u8 *) pin;
930         data.pin_len = os_strlen(pin);
931         data.timeout = timeout;
932         data.added = 0;
933
934         if (os_strcmp(uuid, "any") == 0)
935                 data.uuid = NULL;
936         else {
937                 if (uuid_str2bin(uuid, u))
938                         return -1;
939                 data.uuid = u;
940         }
941         if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
942                 return -1;
943         return data.added ? 0 : -1;
944 }
945
946
947 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
948 {
949         if (hapd->wps == NULL)
950                 return 0;
951         return wps_registrar_button_pushed(hapd->wps->registrar);
952 }
953
954
955 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
956 {
957         return hostapd_wps_for_each(hapd, wps_button_pushed, NULL);
958 }
959
960
961 #ifdef CONFIG_WPS_OOB
962 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
963                           char *path, char *method, char *name)
964 {
965         struct wps_context *wps = hapd->wps;
966         struct oob_device_data *oob_dev;
967
968         oob_dev = wps_get_oob_device(device_type);
969         if (oob_dev == NULL)
970                 return -1;
971         oob_dev->device_path = path;
972         oob_dev->device_name = name;
973         wps->oob_conf.oob_method = wps_get_oob_method(method);
974
975         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
976                 /*
977                  * Use pre-configured DH keys in order to be able to write the
978                  * key hash into the OOB file.
979                  */
980                 wpabuf_free(wps->dh_pubkey);
981                 wpabuf_free(wps->dh_privkey);
982                 wps->dh_privkey = NULL;
983                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
984                                          &wps->dh_privkey);
985                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
986                 if (wps->dh_pubkey == NULL) {
987                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
988                                    "Diffie-Hellman handshake");
989                         return -1;
990                 }
991         }
992
993         if (wps_process_oob(wps, oob_dev, 1) < 0)
994                 goto error;
995
996         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
997              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
998             hostapd_wps_add_pin(hapd, NULL, "any",
999                                 wpabuf_head(wps->oob_conf.dev_password), 0) <
1000             0)
1001                 goto error;
1002
1003         return 0;
1004
1005 error:
1006         wpabuf_free(wps->dh_pubkey);
1007         wps->dh_pubkey = NULL;
1008         wpabuf_free(wps->dh_privkey);
1009         wps->dh_privkey = NULL;
1010         return -1;
1011 }
1012 #endif /* CONFIG_WPS_OOB */
1013
1014
1015 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
1016                                     const u8 *ie, size_t ie_len)
1017 {
1018         struct hostapd_data *hapd = ctx;
1019         struct wpabuf *wps_ie;
1020         struct ieee802_11_elems elems;
1021
1022         if (hapd->wps == NULL)
1023                 return 0;
1024
1025         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1026                 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1027                            MACSTR, MAC2STR(addr));
1028                 return 0;
1029         }
1030
1031         if (elems.ssid && elems.ssid_len > 0 &&
1032             (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1033              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1034              0))
1035                 return 0; /* Not for us */
1036
1037         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1038         if (wps_ie == NULL)
1039                 return 0;
1040         if (wps_validate_probe_req(wps_ie, addr) < 0) {
1041                 wpabuf_free(wps_ie);
1042                 return 0;
1043         }
1044
1045         if (wpabuf_len(wps_ie) > 0) {
1046                 int p2p_wildcard = 0;
1047 #ifdef CONFIG_P2P
1048                 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1049                     os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1050                               P2P_WILDCARD_SSID_LEN) == 0)
1051                         p2p_wildcard = 1;
1052 #endif /* CONFIG_P2P */
1053                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1054                                            p2p_wildcard);
1055 #ifdef CONFIG_WPS_UPNP
1056                 /* FIX: what exactly should be included in the WLANEvent?
1057                  * WPS attributes? Full ProbeReq frame? */
1058                 if (!p2p_wildcard)
1059                         upnp_wps_device_send_wlan_event(
1060                                 hapd->wps_upnp, addr,
1061                                 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1062 #endif /* CONFIG_WPS_UPNP */
1063         }
1064
1065         wpabuf_free(wps_ie);
1066
1067         return 0;
1068 }
1069
1070
1071 #ifdef CONFIG_WPS_UPNP
1072
1073 static int hostapd_rx_req_put_wlan_response(
1074         void *priv, enum upnp_wps_wlanevent_type ev_type,
1075         const u8 *mac_addr, const struct wpabuf *msg,
1076         enum wps_msg_type msg_type)
1077 {
1078         struct hostapd_data *hapd = priv;
1079         struct sta_info *sta;
1080         struct upnp_pending_message *p;
1081
1082         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1083                    MACSTR, ev_type, MAC2STR(mac_addr));
1084         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1085                     wpabuf_head(msg), wpabuf_len(msg));
1086         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1087                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1088                            "PutWLANResponse WLANEventType %d", ev_type);
1089                 return -1;
1090         }
1091
1092         /*
1093          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1094          * server implementation for delivery to the peer.
1095          */
1096
1097         sta = ap_get_sta(hapd, mac_addr);
1098 #ifndef CONFIG_WPS_STRICT
1099         if (!sta) {
1100                 /*
1101                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1102                  * Pick STA that is in an ongoing WPS registration without
1103                  * checking the MAC address.
1104                  */
1105                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1106                            "on NewWLANEventMAC; try wildcard match");
1107                 for (sta = hapd->sta_list; sta; sta = sta->next) {
1108                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1109                                 break;
1110                 }
1111         }
1112 #endif /* CONFIG_WPS_STRICT */
1113
1114         if (!sta) {
1115                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1116                 return 0;
1117         }
1118
1119         p = os_zalloc(sizeof(*p));
1120         if (p == NULL)
1121                 return -1;
1122         os_memcpy(p->addr, sta->addr, ETH_ALEN);
1123         p->msg = wpabuf_dup(msg);
1124         p->type = msg_type;
1125         p->next = hapd->wps->upnp_msgs;
1126         hapd->wps->upnp_msgs = p;
1127
1128         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1129 }
1130
1131
1132 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1133                                  struct wps_context *wps)
1134 {
1135         struct upnp_wps_device_ctx *ctx;
1136
1137         if (!hapd->conf->upnp_iface)
1138                 return 0;
1139         ctx = os_zalloc(sizeof(*ctx));
1140         if (ctx == NULL)
1141                 return -1;
1142
1143         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1144         if (hapd->conf->ap_pin)
1145                 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1146
1147         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1148         if (hapd->wps_upnp == NULL) {
1149                 os_free(ctx);
1150                 return -1;
1151         }
1152         wps->wps_upnp = hapd->wps_upnp;
1153
1154         if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1155                 upnp_wps_device_deinit(hapd->wps_upnp);
1156                 hapd->wps_upnp = NULL;
1157                 return -1;
1158         }
1159
1160         return 0;
1161 }
1162
1163
1164 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1165 {
1166         upnp_wps_device_deinit(hapd->wps_upnp);
1167 }
1168
1169 #endif /* CONFIG_WPS_UPNP */
1170
1171
1172 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1173                             char *buf, size_t buflen)
1174 {
1175         if (hapd->wps == NULL)
1176                 return 0;
1177         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1178 }
1179
1180
1181 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1182 {
1183         struct hostapd_data *hapd = eloop_data;
1184         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1185         hostapd_wps_ap_pin_disable(hapd);
1186 }
1187
1188
1189 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1190 {
1191         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1192         hapd->ap_pin_failures = 0;
1193         hapd->conf->ap_setup_locked = 0;
1194         if (hapd->wps->ap_setup_locked) {
1195                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1196                 hapd->wps->ap_setup_locked = 0;
1197                 wps_registrar_update_ie(hapd->wps->registrar);
1198         }
1199         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1200         if (timeout > 0)
1201                 eloop_register_timeout(timeout, 0,
1202                                        hostapd_wps_ap_pin_timeout, hapd, NULL);
1203 }
1204
1205
1206 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1207 {
1208         os_free(hapd->conf->ap_pin);
1209         hapd->conf->ap_pin = NULL;
1210 #ifdef CONFIG_WPS_UPNP
1211         upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1212 #endif /* CONFIG_WPS_UPNP */
1213         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1214         return 0;
1215 }
1216
1217
1218 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1219 {
1220         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1221         hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1222 }
1223
1224
1225 struct wps_ap_pin_data {
1226         char pin_txt[9];
1227         int timeout;
1228 };
1229
1230
1231 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1232 {
1233         struct wps_ap_pin_data *data = ctx;
1234         os_free(hapd->conf->ap_pin);
1235         hapd->conf->ap_pin = os_strdup(data->pin_txt);
1236 #ifdef CONFIG_WPS_UPNP
1237         upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1238 #endif /* CONFIG_WPS_UPNP */
1239         hostapd_wps_ap_pin_enable(hapd, data->timeout);
1240         return 0;
1241 }
1242
1243
1244 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1245 {
1246         unsigned int pin;
1247         struct wps_ap_pin_data data;
1248
1249         pin = wps_generate_pin();
1250         os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
1251         data.timeout = timeout;
1252         hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1253         return hapd->conf->ap_pin;
1254 }
1255
1256
1257 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1258 {
1259         return hapd->conf->ap_pin;
1260 }
1261
1262
1263 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1264                            int timeout)
1265 {
1266         struct wps_ap_pin_data data;
1267         int ret;
1268
1269         ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1270         if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1271                 return -1;
1272         data.timeout = timeout;
1273         return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1274 }