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