Disable AP PIN on all interfaces controlled by the same process
[libeap.git] / 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         if (iface->for_each_interface == NULL)
79                 return -1;
80         data.func = func;
81         data.ctx = ctx;
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 hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
243 {
244         struct hostapd_data *hapd = 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         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
253                         cred->cred_attr, cred->cred_attr_len);
254
255         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
256         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
257         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
258                    cred->auth_type);
259         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
260         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
261         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
262                         cred->key, cred->key_len);
263         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
264                    MAC2STR(cred->mac_addr));
265
266         if ((hapd->conf->wps_cred_processing == 1 ||
267              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
268                 size_t blen = cred->cred_attr_len * 2 + 1;
269                 char *_buf = os_malloc(blen);
270                 if (_buf) {
271                         wpa_snprintf_hex(_buf, blen,
272                                          cred->cred_attr, cred->cred_attr_len);
273                         wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
274                                 WPS_EVENT_NEW_AP_SETTINGS, _buf);
275                         os_free(_buf);
276                 }
277         } else
278                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
279
280         if (hapd->conf->wps_cred_processing == 1)
281                 return 0;
282
283         os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
284         hapd->wps->ssid_len = cred->ssid_len;
285         hapd->wps->encr_types = cred->encr_type;
286         hapd->wps->auth_types = cred->auth_type;
287         if (cred->key_len == 0) {
288                 os_free(hapd->wps->network_key);
289                 hapd->wps->network_key = NULL;
290                 hapd->wps->network_key_len = 0;
291         } else {
292                 if (hapd->wps->network_key == NULL ||
293                     hapd->wps->network_key_len < cred->key_len) {
294                         hapd->wps->network_key_len = 0;
295                         os_free(hapd->wps->network_key);
296                         hapd->wps->network_key = os_malloc(cred->key_len);
297                         if (hapd->wps->network_key == NULL)
298                                 return -1;
299                 }
300                 hapd->wps->network_key_len = cred->key_len;
301                 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
302         }
303         hapd->wps->wps_state = WPS_STATE_CONFIGURED;
304
305         len = os_strlen(hapd->iface->config_fname) + 5;
306         tmp_fname = os_malloc(len);
307         if (tmp_fname == NULL)
308                 return -1;
309         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
310
311         oconf = fopen(hapd->iface->config_fname, "r");
312         if (oconf == NULL) {
313                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
314                            "configuration file");
315                 os_free(tmp_fname);
316                 return -1;
317         }
318
319         nconf = fopen(tmp_fname, "w");
320         if (nconf == NULL) {
321                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
322                            "configuration file");
323                 os_free(tmp_fname);
324                 fclose(oconf);
325                 return -1;
326         }
327
328         fprintf(nconf, "# WPS configuration - START\n");
329
330         fprintf(nconf, "wps_state=2\n");
331
332         fprintf(nconf, "ssid=");
333         for (i = 0; i < cred->ssid_len; i++)
334                 fputc(cred->ssid[i], nconf);
335         fprintf(nconf, "\n");
336
337         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
338             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
339                 wpa = 3;
340         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
341                 wpa = 2;
342         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
343                 wpa = 1;
344         else
345                 wpa = 0;
346
347         if (wpa) {
348                 char *prefix;
349                 fprintf(nconf, "wpa=%d\n", wpa);
350
351                 fprintf(nconf, "wpa_key_mgmt=");
352                 prefix = "";
353                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
354                         fprintf(nconf, "WPA-EAP");
355                         prefix = " ";
356                 }
357                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
358                         fprintf(nconf, "%sWPA-PSK", prefix);
359                 fprintf(nconf, "\n");
360
361                 fprintf(nconf, "wpa_pairwise=");
362                 prefix = "";
363                 if (cred->encr_type & WPS_ENCR_AES) {
364                         fprintf(nconf, "CCMP");
365                         prefix = " ";
366                 }
367                 if (cred->encr_type & WPS_ENCR_TKIP) {
368                         fprintf(nconf, "%sTKIP", prefix);
369                 }
370                 fprintf(nconf, "\n");
371
372                 if (cred->key_len >= 8 && cred->key_len < 64) {
373                         fprintf(nconf, "wpa_passphrase=");
374                         for (i = 0; i < cred->key_len; i++)
375                                 fputc(cred->key[i], nconf);
376                         fprintf(nconf, "\n");
377                 } else if (cred->key_len == 64) {
378                         fprintf(nconf, "wpa_psk=");
379                         for (i = 0; i < cred->key_len; i++)
380                                 fputc(cred->key[i], nconf);
381                         fprintf(nconf, "\n");
382                 } else {
383                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
384                                    "for WPA/WPA2",
385                                    (unsigned long) cred->key_len);
386                 }
387
388                 fprintf(nconf, "auth_algs=1\n");
389         } else {
390                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
391                     (cred->auth_type & WPS_AUTH_SHARED))
392                         fprintf(nconf, "auth_algs=3\n");
393                 else if (cred->auth_type & WPS_AUTH_SHARED)
394                         fprintf(nconf, "auth_algs=2\n");
395                 else
396                         fprintf(nconf, "auth_algs=1\n");
397
398                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
399                         int key_idx = cred->key_idx;
400                         if (key_idx)
401                                 key_idx--;
402                         fprintf(nconf, "wep_default_key=%d\n", key_idx);
403                         fprintf(nconf, "wep_key%d=", key_idx);
404                         if (cred->key_len == 10 || cred->key_len == 26) {
405                                 /* WEP key as a hex string */
406                                 for (i = 0; i < cred->key_len; i++)
407                                         fputc(cred->key[i], nconf);
408                         } else {
409                                 /* Raw WEP key; convert to hex */
410                                 for (i = 0; i < cred->key_len; i++)
411                                         fprintf(nconf, "%02x", cred->key[i]);
412                         }
413                         fprintf(nconf, "\n");
414                 }
415         }
416
417         fprintf(nconf, "# WPS configuration - END\n");
418
419         multi_bss = 0;
420         while (fgets(buf, sizeof(buf), oconf)) {
421                 if (os_strncmp(buf, "bss=", 4) == 0)
422                         multi_bss = 1;
423                 if (!multi_bss &&
424                     (str_starts(buf, "ssid=") ||
425                      str_starts(buf, "auth_algs=") ||
426                      str_starts(buf, "wps_state=") ||
427                      str_starts(buf, "wpa=") ||
428                      str_starts(buf, "wpa_psk=") ||
429                      str_starts(buf, "wpa_pairwise=") ||
430                      str_starts(buf, "rsn_pairwise=") ||
431                      str_starts(buf, "wpa_key_mgmt=") ||
432                      str_starts(buf, "wpa_passphrase="))) {
433                         fprintf(nconf, "#WPS# %s", buf);
434                 } else
435                         fprintf(nconf, "%s", buf);
436         }
437
438         fclose(nconf);
439         fclose(oconf);
440
441         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
442                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
443                            "configuration file: %s", strerror(errno));
444                 os_free(tmp_fname);
445                 return -1;
446         }
447
448         os_free(tmp_fname);
449
450         /* Schedule configuration reload after short period of time to allow
451          * EAP-WSC to be finished.
452          */
453         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
454                                NULL);
455
456         /* TODO: dualband AP may need to update multiple configuration files */
457
458         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
459
460         return 0;
461 }
462
463
464 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
465 {
466         struct hostapd_data *hapd = eloop_data;
467
468         if (hapd->conf->ap_setup_locked)
469                 return;
470
471         wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
472         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
473         hapd->wps->ap_setup_locked = 0;
474         wps_registrar_update_ie(hapd->wps->registrar);
475 }
476
477
478 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
479 {
480         struct wps_event_pwd_auth_fail *data = ctx;
481
482         if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
483                 return 0;
484
485         /*
486          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
487          * for some time if this happens multiple times to slow down brute
488          * force attacks.
489          */
490         hapd->ap_pin_failures++;
491         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
492                    hapd->ap_pin_failures);
493         if (hapd->ap_pin_failures < 3)
494                 return 0;
495
496         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
497         hapd->wps->ap_setup_locked = 1;
498
499         wps_registrar_update_ie(hapd->wps->registrar);
500
501         if (!hapd->conf->ap_setup_locked) {
502                 if (hapd->ap_pin_lockout_time == 0)
503                         hapd->ap_pin_lockout_time = 60;
504                 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
505                          (hapd->ap_pin_failures % 3) == 0)
506                         hapd->ap_pin_lockout_time *= 2;
507
508                 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
509                            hapd->ap_pin_lockout_time);
510                 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
511                 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
512                                        hostapd_wps_reenable_ap_pin, hapd,
513                                        NULL);
514         }
515
516         return 0;
517 }
518
519
520 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
521                                   struct wps_event_pwd_auth_fail *data)
522 {
523         hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
524 }
525
526
527 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
528                                  union wps_event_data *data)
529 {
530         struct hostapd_data *hapd = ctx;
531
532         if (event == WPS_EV_PWD_AUTH_FAIL)
533                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
534 }
535
536
537 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
538 {
539         wpabuf_free(hapd->wps_beacon_ie);
540         hapd->wps_beacon_ie = NULL;
541
542         wpabuf_free(hapd->wps_probe_resp_ie);
543         hapd->wps_probe_resp_ie = NULL;
544
545         hapd->drv.set_ap_wps_ie(hapd);
546 }
547
548
549 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
550 {
551         const u8 **uuid = ctx;
552         size_t j;
553
554         if (iface == NULL)
555                 return 0;
556         for (j = 0; j < iface->num_bss; j++) {
557                 struct hostapd_data *hapd = iface->bss[j];
558                 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
559                         *uuid = hapd->wps->uuid;
560                         return 1;
561                 }
562         }
563
564         return 0;
565 }
566
567
568 static const u8 * get_own_uuid(struct hostapd_iface *iface)
569 {
570         const u8 *uuid;
571         if (iface->for_each_interface == NULL)
572                 return NULL;
573         uuid = NULL;
574         iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid);
575         return uuid;
576 }
577
578
579 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
580 {
581         int *count= ctx;
582         (*count)++;
583         return 0;
584 }
585
586
587 static int interface_count(struct hostapd_iface *iface)
588 {
589         int count = 0;
590         if (iface->for_each_interface == NULL)
591                 return 0;
592         iface->for_each_interface(iface->interfaces, count_interface_cb,
593                                   &count);
594         return count;
595 }
596
597
598 int hostapd_init_wps(struct hostapd_data *hapd,
599                      struct hostapd_bss_config *conf)
600 {
601         struct wps_context *wps;
602         struct wps_registrar_config cfg;
603
604         if (conf->wps_state == 0) {
605                 hostapd_wps_clear_ies(hapd);
606                 return 0;
607         }
608
609         wps = os_zalloc(sizeof(*wps));
610         if (wps == NULL)
611                 return -1;
612
613         wps->cred_cb = hostapd_wps_cred_cb;
614         wps->event_cb = hostapd_wps_event_cb;
615         wps->cb_ctx = hapd;
616
617         os_memset(&cfg, 0, sizeof(cfg));
618         wps->wps_state = hapd->conf->wps_state;
619         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
620         if (is_nil_uuid(hapd->conf->uuid)) {
621                 const u8 *uuid;
622                 uuid = get_own_uuid(hapd->iface);
623                 if (uuid) {
624                         os_memcpy(wps->uuid, uuid, UUID_LEN);
625                         wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
626                                     "interface", wps->uuid, UUID_LEN);
627                 } else {
628                         uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
629                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
630                                     "address", wps->uuid, UUID_LEN);
631                 }
632         } else {
633                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
634                 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
635                             wps->uuid, UUID_LEN);
636         }
637         wps->ssid_len = hapd->conf->ssid.ssid_len;
638         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
639         wps->ap = 1;
640         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
641         wps->dev.device_name = hapd->conf->device_name ?
642                 os_strdup(hapd->conf->device_name) : NULL;
643         wps->dev.manufacturer = hapd->conf->manufacturer ?
644                 os_strdup(hapd->conf->manufacturer) : NULL;
645         wps->dev.model_name = hapd->conf->model_name ?
646                 os_strdup(hapd->conf->model_name) : NULL;
647         wps->dev.model_number = hapd->conf->model_number ?
648                 os_strdup(hapd->conf->model_number) : NULL;
649         wps->dev.serial_number = hapd->conf->serial_number ?
650                 os_strdup(hapd->conf->serial_number) : NULL;
651         wps->config_methods =
652                 wps_config_methods_str2bin(hapd->conf->config_methods);
653 #ifdef CONFIG_WPS2
654         if ((wps->config_methods &
655              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
656               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
657                 wpa_printf(MSG_INFO, "WPS: Converting display to "
658                            "virtual_display for WPS 2.0 compliance");
659                 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
660         }
661         if ((wps->config_methods &
662              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
663               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
664                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
665                            "virtual_push_button for WPS 2.0 compliance");
666                 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
667         }
668 #endif /* CONFIG_WPS2 */
669         if (hapd->conf->device_type &&
670             wps_dev_type_str2bin(hapd->conf->device_type,
671                                  wps->dev.pri_dev_type) < 0) {
672                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
673                 os_free(wps);
674                 return -1;
675         }
676         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
677         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
678                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
679
680         if (conf->wpa & WPA_PROTO_RSN) {
681                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
682                         wps->auth_types |= WPS_AUTH_WPA2PSK;
683                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
684                         wps->auth_types |= WPS_AUTH_WPA2;
685
686                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
687                         wps->encr_types |= WPS_ENCR_AES;
688                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
689                         wps->encr_types |= WPS_ENCR_TKIP;
690         }
691
692         if (conf->wpa & WPA_PROTO_WPA) {
693                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
694                         wps->auth_types |= WPS_AUTH_WPAPSK;
695                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
696                         wps->auth_types |= WPS_AUTH_WPA;
697
698                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
699                         wps->encr_types |= WPS_ENCR_AES;
700                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
701                         wps->encr_types |= WPS_ENCR_TKIP;
702         }
703
704         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
705                 wps->encr_types |= WPS_ENCR_NONE;
706                 wps->auth_types |= WPS_AUTH_OPEN;
707         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
708                 wps->encr_types |= WPS_ENCR_WEP;
709                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
710                         wps->auth_types |= WPS_AUTH_OPEN;
711                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
712                         wps->auth_types |= WPS_AUTH_SHARED;
713         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
714                 wps->auth_types |= WPS_AUTH_OPEN;
715                 if (conf->default_wep_key_len)
716                         wps->encr_types |= WPS_ENCR_WEP;
717                 else
718                         wps->encr_types |= WPS_ENCR_NONE;
719         }
720
721         if (conf->ssid.wpa_psk_file) {
722                 /* Use per-device PSKs */
723         } else if (conf->ssid.wpa_passphrase) {
724                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
725                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
726         } else if (conf->ssid.wpa_psk) {
727                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
728                 if (wps->network_key == NULL) {
729                         os_free(wps);
730                         return -1;
731                 }
732                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
733                                  conf->ssid.wpa_psk->psk, PMK_LEN);
734                 wps->network_key_len = 2 * PMK_LEN;
735         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
736                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
737                 if (wps->network_key == NULL) {
738                         os_free(wps);
739                         return -1;
740                 }
741                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
742                           conf->ssid.wep.len[0]);
743                 wps->network_key_len = conf->ssid.wep.len[0];
744         }
745
746         if (conf->ssid.wpa_psk) {
747                 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
748                 wps->psk_set = 1;
749         }
750
751         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
752                 /* Override parameters to enable security by default */
753                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
754                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
755         }
756
757         wps->ap_settings = conf->ap_settings;
758         wps->ap_settings_len = conf->ap_settings_len;
759
760         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
761         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
762         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
763         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
764         cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
765         cfg.cb_ctx = hapd;
766         cfg.skip_cred_build = conf->skip_cred_build;
767         cfg.extra_cred = conf->extra_cred;
768         cfg.extra_cred_len = conf->extra_cred_len;
769         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
770                 conf->skip_cred_build;
771         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
772                 cfg.static_wep_only = 1;
773         cfg.dualband = interface_count(hapd->iface) > 1;
774         if (cfg.dualband)
775                 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
776
777         wps->registrar = wps_registrar_init(wps, &cfg);
778         if (wps->registrar == NULL) {
779                 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
780                 os_free(wps->network_key);
781                 os_free(wps);
782                 return -1;
783         }
784
785 #ifdef CONFIG_WPS_UPNP
786         wps->friendly_name = hapd->conf->friendly_name;
787         wps->manufacturer_url = hapd->conf->manufacturer_url;
788         wps->model_description = hapd->conf->model_description;
789         wps->model_url = hapd->conf->model_url;
790         wps->upc = hapd->conf->upc;
791
792         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
793                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
794                 wps_registrar_deinit(wps->registrar);
795                 os_free(wps->network_key);
796                 os_free(wps);
797                 return -1;
798         }
799 #endif /* CONFIG_WPS_UPNP */
800
801         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
802
803         hapd->wps = wps;
804
805         return 0;
806 }
807
808
809 void hostapd_deinit_wps(struct hostapd_data *hapd)
810 {
811         eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
812         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
813         if (hapd->wps == NULL)
814                 return;
815 #ifdef CONFIG_WPS_UPNP
816         hostapd_wps_upnp_deinit(hapd);
817 #endif /* CONFIG_WPS_UPNP */
818         wps_registrar_deinit(hapd->wps->registrar);
819         os_free(hapd->wps->network_key);
820         wps_device_data_free(&hapd->wps->dev);
821         wpabuf_free(hapd->wps->dh_pubkey);
822         wpabuf_free(hapd->wps->dh_privkey);
823         wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
824         wpabuf_free(hapd->wps->oob_conf.dev_password);
825         wps_free_pending_msgs(hapd->wps->upnp_msgs);
826         os_free(hapd->wps);
827         hapd->wps = NULL;
828         hostapd_wps_clear_ies(hapd);
829 }
830
831
832 void hostapd_update_wps(struct hostapd_data *hapd)
833 {
834         if (hapd->wps == NULL)
835                 return;
836
837 #ifdef CONFIG_WPS_UPNP
838         hapd->wps->friendly_name = hapd->conf->friendly_name;
839         hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
840         hapd->wps->model_description = hapd->conf->model_description;
841         hapd->wps->model_url = hapd->conf->model_url;
842         hapd->wps->upc = hapd->conf->upc;
843 #endif /* CONFIG_WPS_UPNP */
844
845         if (hapd->conf->wps_state)
846                 wps_registrar_update_ie(hapd->wps->registrar);
847         else
848                 hostapd_deinit_wps(hapd);
849 }
850
851
852 struct wps_add_pin_data {
853         const u8 *addr;
854         const u8 *uuid;
855         const u8 *pin;
856         size_t pin_len;
857         int timeout;
858         int added;
859 };
860
861
862 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
863 {
864         struct wps_add_pin_data *data = ctx;
865         int ret;
866
867         if (hapd->wps == NULL)
868                 return 0;
869         ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
870                                     data->uuid, data->pin, data->pin_len,
871                                     data->timeout);
872         if (ret == 0)
873                 data->added++;
874         return ret;
875 }
876
877
878 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
879                         const char *uuid, const char *pin, int timeout)
880 {
881         u8 u[UUID_LEN];
882         struct wps_add_pin_data data;
883
884         data.addr = addr;
885         data.uuid = u;
886         data.pin = (const u8 *) pin;
887         data.pin_len = os_strlen(pin);
888         data.timeout = timeout;
889         data.added = 0;
890
891         if (os_strcmp(uuid, "any") == 0)
892                 data.uuid = NULL;
893         else {
894                 if (uuid_str2bin(uuid, u))
895                         return -1;
896                 data.uuid = u;
897         }
898         if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
899                 return -1;
900         return data.added ? 0 : -1;
901 }
902
903
904 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
905 {
906         if (hapd->wps == NULL)
907                 return 0;
908         return wps_registrar_button_pushed(hapd->wps->registrar);
909 }
910
911
912 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
913 {
914         return hostapd_wps_for_each(hapd, wps_button_pushed, NULL);
915 }
916
917
918 #ifdef CONFIG_WPS_OOB
919 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
920                           char *path, char *method, char *name)
921 {
922         struct wps_context *wps = hapd->wps;
923         struct oob_device_data *oob_dev;
924
925         oob_dev = wps_get_oob_device(device_type);
926         if (oob_dev == NULL)
927                 return -1;
928         oob_dev->device_path = path;
929         oob_dev->device_name = name;
930         wps->oob_conf.oob_method = wps_get_oob_method(method);
931
932         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
933                 /*
934                  * Use pre-configured DH keys in order to be able to write the
935                  * key hash into the OOB file.
936                  */
937                 wpabuf_free(wps->dh_pubkey);
938                 wpabuf_free(wps->dh_privkey);
939                 wps->dh_privkey = NULL;
940                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
941                                          &wps->dh_privkey);
942                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
943                 if (wps->dh_pubkey == NULL) {
944                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
945                                    "Diffie-Hellman handshake");
946                         return -1;
947                 }
948         }
949
950         if (wps_process_oob(wps, oob_dev, 1) < 0)
951                 goto error;
952
953         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
954              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
955             hostapd_wps_add_pin(hapd, NULL, "any",
956                                 wpabuf_head(wps->oob_conf.dev_password), 0) <
957             0)
958                 goto error;
959
960         return 0;
961
962 error:
963         wpabuf_free(wps->dh_pubkey);
964         wps->dh_pubkey = NULL;
965         wpabuf_free(wps->dh_privkey);
966         wps->dh_privkey = NULL;
967         return -1;
968 }
969 #endif /* CONFIG_WPS_OOB */
970
971
972 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
973                                     const u8 *ie, size_t ie_len)
974 {
975         struct hostapd_data *hapd = ctx;
976         struct wpabuf *wps_ie;
977         struct ieee802_11_elems elems;
978
979         if (hapd->wps == NULL)
980                 return 0;
981
982         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
983                 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
984                            MACSTR, MAC2STR(addr));
985                 return 0;
986         }
987
988         if (elems.ssid && elems.ssid_len > 0 &&
989             (elems.ssid_len != hapd->conf->ssid.ssid_len ||
990              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
991              0))
992                 return 0; /* Not for us */
993
994         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
995         if (wps_ie == NULL)
996                 return 0;
997         if (wps_validate_probe_req(wps_ie, addr) < 0) {
998                 wpabuf_free(wps_ie);
999                 return 0;
1000         }
1001
1002         if (wpabuf_len(wps_ie) > 0) {
1003                 int p2p_wildcard = 0;
1004 #ifdef CONFIG_P2P
1005                 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1006                     os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1007                               P2P_WILDCARD_SSID_LEN) == 0)
1008                         p2p_wildcard = 1;
1009 #endif /* CONFIG_P2P */
1010                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1011                                            p2p_wildcard);
1012 #ifdef CONFIG_WPS_UPNP
1013                 /* FIX: what exactly should be included in the WLANEvent?
1014                  * WPS attributes? Full ProbeReq frame? */
1015                 if (!p2p_wildcard)
1016                         upnp_wps_device_send_wlan_event(
1017                                 hapd->wps_upnp, addr,
1018                                 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1019 #endif /* CONFIG_WPS_UPNP */
1020         }
1021
1022         wpabuf_free(wps_ie);
1023
1024         return 0;
1025 }
1026
1027
1028 #ifdef CONFIG_WPS_UPNP
1029
1030 static int hostapd_rx_req_put_wlan_response(
1031         void *priv, enum upnp_wps_wlanevent_type ev_type,
1032         const u8 *mac_addr, const struct wpabuf *msg,
1033         enum wps_msg_type msg_type)
1034 {
1035         struct hostapd_data *hapd = priv;
1036         struct sta_info *sta;
1037         struct upnp_pending_message *p;
1038
1039         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1040                    MACSTR, ev_type, MAC2STR(mac_addr));
1041         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1042                     wpabuf_head(msg), wpabuf_len(msg));
1043         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1044                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1045                            "PutWLANResponse WLANEventType %d", ev_type);
1046                 return -1;
1047         }
1048
1049         /*
1050          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1051          * server implementation for delivery to the peer.
1052          */
1053
1054         sta = ap_get_sta(hapd, mac_addr);
1055 #ifndef CONFIG_WPS_STRICT
1056         if (!sta) {
1057                 /*
1058                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1059                  * Pick STA that is in an ongoing WPS registration without
1060                  * checking the MAC address.
1061                  */
1062                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1063                            "on NewWLANEventMAC; try wildcard match");
1064                 for (sta = hapd->sta_list; sta; sta = sta->next) {
1065                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1066                                 break;
1067                 }
1068         }
1069 #endif /* CONFIG_WPS_STRICT */
1070
1071         if (!sta) {
1072                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1073                 return 0;
1074         }
1075
1076         p = os_zalloc(sizeof(*p));
1077         if (p == NULL)
1078                 return -1;
1079         os_memcpy(p->addr, sta->addr, ETH_ALEN);
1080         p->msg = wpabuf_dup(msg);
1081         p->type = msg_type;
1082         p->next = hapd->wps->upnp_msgs;
1083         hapd->wps->upnp_msgs = p;
1084
1085         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1086 }
1087
1088
1089 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1090                                  struct wps_context *wps)
1091 {
1092         struct upnp_wps_device_ctx *ctx;
1093
1094         if (!hapd->conf->upnp_iface)
1095                 return 0;
1096         ctx = os_zalloc(sizeof(*ctx));
1097         if (ctx == NULL)
1098                 return -1;
1099
1100         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1101         if (hapd->conf->ap_pin)
1102                 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1103
1104         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1105         if (hapd->wps_upnp == NULL) {
1106                 os_free(ctx);
1107                 return -1;
1108         }
1109         wps->wps_upnp = hapd->wps_upnp;
1110
1111         if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1112                 upnp_wps_device_deinit(hapd->wps_upnp);
1113                 hapd->wps_upnp = NULL;
1114                 return -1;
1115         }
1116
1117         return 0;
1118 }
1119
1120
1121 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1122 {
1123         upnp_wps_device_deinit(hapd->wps_upnp);
1124 }
1125
1126 #endif /* CONFIG_WPS_UPNP */
1127
1128
1129 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1130                             char *buf, size_t buflen)
1131 {
1132         if (hapd->wps == NULL)
1133                 return 0;
1134         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1135 }
1136
1137
1138 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1139 {
1140         struct hostapd_data *hapd = eloop_data;
1141         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1142         hostapd_wps_ap_pin_disable(hapd);
1143 }
1144
1145
1146 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1147 {
1148         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1149         hapd->ap_pin_failures = 0;
1150         hapd->conf->ap_setup_locked = 0;
1151         if (hapd->wps->ap_setup_locked) {
1152                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1153                 hapd->wps->ap_setup_locked = 0;
1154                 wps_registrar_update_ie(hapd->wps->registrar);
1155         }
1156         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1157         if (timeout > 0)
1158                 eloop_register_timeout(timeout, 0,
1159                                        hostapd_wps_ap_pin_timeout, hapd, NULL);
1160 }
1161
1162
1163 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1164 {
1165         os_free(hapd->conf->ap_pin);
1166         hapd->conf->ap_pin = NULL;
1167 #ifdef CONFIG_WPS_UPNP
1168         upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1169 #endif /* CONFIG_WPS_UPNP */
1170         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1171         return 0;
1172 }
1173
1174
1175 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1176 {
1177         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1178         hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1179 }
1180
1181
1182 struct wps_ap_pin_data {
1183         char pin_txt[9];
1184         int timeout;
1185 };
1186
1187
1188 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1189 {
1190         struct wps_ap_pin_data *data = ctx;
1191         os_free(hapd->conf->ap_pin);
1192         hapd->conf->ap_pin = os_strdup(data->pin_txt);
1193 #ifdef CONFIG_WPS_UPNP
1194         upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1195 #endif /* CONFIG_WPS_UPNP */
1196         hostapd_wps_ap_pin_enable(hapd, data->timeout);
1197         return 0;
1198 }
1199
1200
1201 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1202 {
1203         unsigned int pin;
1204         struct wps_ap_pin_data data;
1205
1206         pin = wps_generate_pin();
1207         os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
1208         data.timeout = timeout;
1209         hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1210         return hapd->conf->ap_pin;
1211 }
1212
1213
1214 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1215 {
1216         return hapd->conf->ap_pin;
1217 }
1218
1219
1220 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1221                            int timeout)
1222 {
1223         struct wps_ap_pin_data data;
1224         int ret;
1225
1226         ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1227         if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1228                 return -1;
1229         data.timeout = timeout;
1230         return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1231 }