WPS: Add mechanism for indicating non-standard WPS errors
[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         if (hapd->conf->device_type &&
732             wps_dev_type_str2bin(hapd->conf->device_type,
733                                  wps->dev.pri_dev_type) < 0) {
734                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
735                 os_free(wps);
736                 return -1;
737         }
738         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
739         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
740                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
741
742         if (conf->wpa & WPA_PROTO_RSN) {
743                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
744                         wps->auth_types |= WPS_AUTH_WPA2PSK;
745                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
746                         wps->auth_types |= WPS_AUTH_WPA2;
747
748                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
749                         wps->encr_types |= WPS_ENCR_AES;
750                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
751                         wps->encr_types |= WPS_ENCR_TKIP;
752         }
753
754         if (conf->wpa & WPA_PROTO_WPA) {
755                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
756                         wps->auth_types |= WPS_AUTH_WPAPSK;
757                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
758                         wps->auth_types |= WPS_AUTH_WPA;
759
760                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
761                         wps->encr_types |= WPS_ENCR_AES;
762                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
763                         wps->encr_types |= WPS_ENCR_TKIP;
764         }
765
766         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
767                 wps->encr_types |= WPS_ENCR_NONE;
768                 wps->auth_types |= WPS_AUTH_OPEN;
769         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
770                 wps->encr_types |= WPS_ENCR_WEP;
771                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
772                         wps->auth_types |= WPS_AUTH_OPEN;
773                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
774                         wps->auth_types |= WPS_AUTH_SHARED;
775         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
776                 wps->auth_types |= WPS_AUTH_OPEN;
777                 if (conf->default_wep_key_len)
778                         wps->encr_types |= WPS_ENCR_WEP;
779                 else
780                         wps->encr_types |= WPS_ENCR_NONE;
781         }
782
783         if (conf->ssid.wpa_psk_file) {
784                 /* Use per-device PSKs */
785         } else if (conf->ssid.wpa_passphrase) {
786                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
787                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
788         } else if (conf->ssid.wpa_psk) {
789                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
790                 if (wps->network_key == NULL) {
791                         os_free(wps);
792                         return -1;
793                 }
794                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
795                                  conf->ssid.wpa_psk->psk, PMK_LEN);
796                 wps->network_key_len = 2 * PMK_LEN;
797         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
798                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
799                 if (wps->network_key == NULL) {
800                         os_free(wps);
801                         return -1;
802                 }
803                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
804                           conf->ssid.wep.len[0]);
805                 wps->network_key_len = conf->ssid.wep.len[0];
806         }
807
808         if (conf->ssid.wpa_psk) {
809                 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
810                 wps->psk_set = 1;
811         }
812
813         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
814                 /* Override parameters to enable security by default */
815                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
816                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
817         }
818
819         wps->ap_settings = conf->ap_settings;
820         wps->ap_settings_len = conf->ap_settings_len;
821
822         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
823         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
824         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
825         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
826         cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
827         cfg.cb_ctx = hapd;
828         cfg.skip_cred_build = conf->skip_cred_build;
829         cfg.extra_cred = conf->extra_cred;
830         cfg.extra_cred_len = conf->extra_cred_len;
831         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
832                 conf->skip_cred_build;
833         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
834                 cfg.static_wep_only = 1;
835         cfg.dualband = interface_count(hapd->iface) > 1;
836         if (cfg.dualband)
837                 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
838
839         wps->registrar = wps_registrar_init(wps, &cfg);
840         if (wps->registrar == NULL) {
841                 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
842                 os_free(wps->network_key);
843                 os_free(wps);
844                 return -1;
845         }
846
847 #ifdef CONFIG_WPS_UPNP
848         wps->friendly_name = hapd->conf->friendly_name;
849         wps->manufacturer_url = hapd->conf->manufacturer_url;
850         wps->model_description = hapd->conf->model_description;
851         wps->model_url = hapd->conf->model_url;
852         wps->upc = hapd->conf->upc;
853
854         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
855                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
856                 wps_registrar_deinit(wps->registrar);
857                 os_free(wps->network_key);
858                 os_free(wps);
859                 return -1;
860         }
861 #endif /* CONFIG_WPS_UPNP */
862
863         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
864
865         hapd->wps = wps;
866
867         return 0;
868 }
869
870
871 void hostapd_deinit_wps(struct hostapd_data *hapd)
872 {
873         eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
874         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
875         if (hapd->wps == NULL)
876                 return;
877 #ifdef CONFIG_WPS_UPNP
878         hostapd_wps_upnp_deinit(hapd);
879 #endif /* CONFIG_WPS_UPNP */
880         wps_registrar_deinit(hapd->wps->registrar);
881         os_free(hapd->wps->network_key);
882         wps_device_data_free(&hapd->wps->dev);
883         wpabuf_free(hapd->wps->dh_pubkey);
884         wpabuf_free(hapd->wps->dh_privkey);
885         wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
886         wpabuf_free(hapd->wps->oob_conf.dev_password);
887         wps_free_pending_msgs(hapd->wps->upnp_msgs);
888         os_free(hapd->wps);
889         hapd->wps = NULL;
890         hostapd_wps_clear_ies(hapd);
891 }
892
893
894 void hostapd_update_wps(struct hostapd_data *hapd)
895 {
896         if (hapd->wps == NULL)
897                 return;
898
899 #ifdef CONFIG_WPS_UPNP
900         hapd->wps->friendly_name = hapd->conf->friendly_name;
901         hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
902         hapd->wps->model_description = hapd->conf->model_description;
903         hapd->wps->model_url = hapd->conf->model_url;
904         hapd->wps->upc = hapd->conf->upc;
905 #endif /* CONFIG_WPS_UPNP */
906
907         if (hapd->conf->wps_state)
908                 wps_registrar_update_ie(hapd->wps->registrar);
909         else
910                 hostapd_deinit_wps(hapd);
911 }
912
913
914 struct wps_add_pin_data {
915         const u8 *addr;
916         const u8 *uuid;
917         const u8 *pin;
918         size_t pin_len;
919         int timeout;
920         int added;
921 };
922
923
924 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
925 {
926         struct wps_add_pin_data *data = ctx;
927         int ret;
928
929         if (hapd->wps == NULL)
930                 return 0;
931         ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
932                                     data->uuid, data->pin, data->pin_len,
933                                     data->timeout);
934         if (ret == 0)
935                 data->added++;
936         return ret;
937 }
938
939
940 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
941                         const char *uuid, const char *pin, int timeout)
942 {
943         u8 u[UUID_LEN];
944         struct wps_add_pin_data data;
945
946         data.addr = addr;
947         data.uuid = u;
948         data.pin = (const u8 *) pin;
949         data.pin_len = os_strlen(pin);
950         data.timeout = timeout;
951         data.added = 0;
952
953         if (os_strcmp(uuid, "any") == 0)
954                 data.uuid = NULL;
955         else {
956                 if (uuid_str2bin(uuid, u))
957                         return -1;
958                 data.uuid = u;
959         }
960         if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
961                 return -1;
962         return data.added ? 0 : -1;
963 }
964
965
966 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
967 {
968         if (hapd->wps == NULL)
969                 return 0;
970         return wps_registrar_button_pushed(hapd->wps->registrar);
971 }
972
973
974 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
975 {
976         return hostapd_wps_for_each(hapd, wps_button_pushed, NULL);
977 }
978
979
980 #ifdef CONFIG_WPS_OOB
981 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
982                           char *path, char *method, char *name)
983 {
984         struct wps_context *wps = hapd->wps;
985         struct oob_device_data *oob_dev;
986
987         oob_dev = wps_get_oob_device(device_type);
988         if (oob_dev == NULL)
989                 return -1;
990         oob_dev->device_path = path;
991         oob_dev->device_name = name;
992         wps->oob_conf.oob_method = wps_get_oob_method(method);
993
994         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
995                 /*
996                  * Use pre-configured DH keys in order to be able to write the
997                  * key hash into the OOB file.
998                  */
999                 wpabuf_free(wps->dh_pubkey);
1000                 wpabuf_free(wps->dh_privkey);
1001                 wps->dh_privkey = NULL;
1002                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
1003                                          &wps->dh_privkey);
1004                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
1005                 if (wps->dh_pubkey == NULL) {
1006                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
1007                                    "Diffie-Hellman handshake");
1008                         return -1;
1009                 }
1010         }
1011
1012         if (wps_process_oob(wps, oob_dev, 1) < 0)
1013                 goto error;
1014
1015         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
1016              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
1017             hostapd_wps_add_pin(hapd, NULL, "any",
1018                                 wpabuf_head(wps->oob_conf.dev_password), 0) <
1019             0)
1020                 goto error;
1021
1022         return 0;
1023
1024 error:
1025         wpabuf_free(wps->dh_pubkey);
1026         wps->dh_pubkey = NULL;
1027         wpabuf_free(wps->dh_privkey);
1028         wps->dh_privkey = NULL;
1029         return -1;
1030 }
1031 #endif /* CONFIG_WPS_OOB */
1032
1033
1034 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
1035                                     const u8 *ie, size_t ie_len)
1036 {
1037         struct hostapd_data *hapd = ctx;
1038         struct wpabuf *wps_ie;
1039         struct ieee802_11_elems elems;
1040
1041         if (hapd->wps == NULL)
1042                 return 0;
1043
1044         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1045                 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1046                            MACSTR, MAC2STR(addr));
1047                 return 0;
1048         }
1049
1050         if (elems.ssid && elems.ssid_len > 0 &&
1051             (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1052              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1053              0))
1054                 return 0; /* Not for us */
1055
1056         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1057         if (wps_ie == NULL)
1058                 return 0;
1059         if (wps_validate_probe_req(wps_ie, addr) < 0) {
1060                 wpabuf_free(wps_ie);
1061                 return 0;
1062         }
1063
1064         if (wpabuf_len(wps_ie) > 0) {
1065                 int p2p_wildcard = 0;
1066 #ifdef CONFIG_P2P
1067                 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1068                     os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1069                               P2P_WILDCARD_SSID_LEN) == 0)
1070                         p2p_wildcard = 1;
1071 #endif /* CONFIG_P2P */
1072                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1073                                            p2p_wildcard);
1074 #ifdef CONFIG_WPS_UPNP
1075                 /* FIX: what exactly should be included in the WLANEvent?
1076                  * WPS attributes? Full ProbeReq frame? */
1077                 if (!p2p_wildcard)
1078                         upnp_wps_device_send_wlan_event(
1079                                 hapd->wps_upnp, addr,
1080                                 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1081 #endif /* CONFIG_WPS_UPNP */
1082         }
1083
1084         wpabuf_free(wps_ie);
1085
1086         return 0;
1087 }
1088
1089
1090 #ifdef CONFIG_WPS_UPNP
1091
1092 static int hostapd_rx_req_put_wlan_response(
1093         void *priv, enum upnp_wps_wlanevent_type ev_type,
1094         const u8 *mac_addr, const struct wpabuf *msg,
1095         enum wps_msg_type msg_type)
1096 {
1097         struct hostapd_data *hapd = priv;
1098         struct sta_info *sta;
1099         struct upnp_pending_message *p;
1100
1101         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1102                    MACSTR, ev_type, MAC2STR(mac_addr));
1103         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1104                     wpabuf_head(msg), wpabuf_len(msg));
1105         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1106                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1107                            "PutWLANResponse WLANEventType %d", ev_type);
1108                 return -1;
1109         }
1110
1111         /*
1112          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1113          * server implementation for delivery to the peer.
1114          */
1115
1116         sta = ap_get_sta(hapd, mac_addr);
1117 #ifndef CONFIG_WPS_STRICT
1118         if (!sta) {
1119                 /*
1120                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1121                  * Pick STA that is in an ongoing WPS registration without
1122                  * checking the MAC address.
1123                  */
1124                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1125                            "on NewWLANEventMAC; try wildcard match");
1126                 for (sta = hapd->sta_list; sta; sta = sta->next) {
1127                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1128                                 break;
1129                 }
1130         }
1131 #endif /* CONFIG_WPS_STRICT */
1132
1133         if (!sta) {
1134                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1135                 return 0;
1136         }
1137
1138         p = os_zalloc(sizeof(*p));
1139         if (p == NULL)
1140                 return -1;
1141         os_memcpy(p->addr, sta->addr, ETH_ALEN);
1142         p->msg = wpabuf_dup(msg);
1143         p->type = msg_type;
1144         p->next = hapd->wps->upnp_msgs;
1145         hapd->wps->upnp_msgs = p;
1146
1147         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1148 }
1149
1150
1151 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1152                                  struct wps_context *wps)
1153 {
1154         struct upnp_wps_device_ctx *ctx;
1155
1156         if (!hapd->conf->upnp_iface)
1157                 return 0;
1158         ctx = os_zalloc(sizeof(*ctx));
1159         if (ctx == NULL)
1160                 return -1;
1161
1162         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1163         if (hapd->conf->ap_pin)
1164                 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1165
1166         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1167                                               hapd->conf->upnp_iface);
1168         if (hapd->wps_upnp == NULL)
1169                 return -1;
1170         wps->wps_upnp = hapd->wps_upnp;
1171
1172         return 0;
1173 }
1174
1175
1176 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1177 {
1178         upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1179 }
1180
1181 #endif /* CONFIG_WPS_UPNP */
1182
1183
1184 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1185                             char *buf, size_t buflen)
1186 {
1187         if (hapd->wps == NULL)
1188                 return 0;
1189         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1190 }
1191
1192
1193 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1194 {
1195         struct hostapd_data *hapd = eloop_data;
1196         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1197         hostapd_wps_ap_pin_disable(hapd);
1198 }
1199
1200
1201 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1202 {
1203         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1204         hapd->ap_pin_failures = 0;
1205         hapd->conf->ap_setup_locked = 0;
1206         if (hapd->wps->ap_setup_locked) {
1207                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1208                 hapd->wps->ap_setup_locked = 0;
1209                 wps_registrar_update_ie(hapd->wps->registrar);
1210         }
1211         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1212         if (timeout > 0)
1213                 eloop_register_timeout(timeout, 0,
1214                                        hostapd_wps_ap_pin_timeout, hapd, NULL);
1215 }
1216
1217
1218 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1219 {
1220         os_free(hapd->conf->ap_pin);
1221         hapd->conf->ap_pin = NULL;
1222 #ifdef CONFIG_WPS_UPNP
1223         upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1224 #endif /* CONFIG_WPS_UPNP */
1225         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1226         return 0;
1227 }
1228
1229
1230 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1231 {
1232         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1233         hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1234 }
1235
1236
1237 struct wps_ap_pin_data {
1238         char pin_txt[9];
1239         int timeout;
1240 };
1241
1242
1243 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1244 {
1245         struct wps_ap_pin_data *data = ctx;
1246         os_free(hapd->conf->ap_pin);
1247         hapd->conf->ap_pin = os_strdup(data->pin_txt);
1248 #ifdef CONFIG_WPS_UPNP
1249         upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1250 #endif /* CONFIG_WPS_UPNP */
1251         hostapd_wps_ap_pin_enable(hapd, data->timeout);
1252         return 0;
1253 }
1254
1255
1256 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1257 {
1258         unsigned int pin;
1259         struct wps_ap_pin_data data;
1260
1261         pin = wps_generate_pin();
1262         os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
1263         data.timeout = timeout;
1264         hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1265         return hapd->conf->ap_pin;
1266 }
1267
1268
1269 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1270 {
1271         return hapd->conf->ap_pin;
1272 }
1273
1274
1275 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1276                            int timeout)
1277 {
1278         struct wps_ap_pin_data data;
1279         int ret;
1280
1281         ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1282         if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1283                 return -1;
1284         data.timeout = timeout;
1285         return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1286 }
1287
1288
1289 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1290 {
1291         if (hapd->wps)
1292                 wps_registrar_update_ie(hapd->wps->registrar);
1293         return 0;
1294 }
1295
1296
1297 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1298 {
1299         hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1300 }
1301
1302
1303 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1304                           const char *auth, const char *encr, const char *key)
1305 {
1306         struct wps_credential cred;
1307         size_t len;
1308
1309         os_memset(&cred, 0, sizeof(cred));
1310
1311         len = os_strlen(ssid);
1312         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1313             hexstr2bin(ssid, cred.ssid, len / 2))
1314                 return -1;
1315         cred.ssid_len = len / 2;
1316
1317         if (os_strncmp(auth, "OPEN", 4) == 0)
1318                 cred.auth_type = WPS_AUTH_OPEN;
1319         else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1320                 cred.auth_type = WPS_AUTH_WPAPSK;
1321         else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1322                 cred.auth_type = WPS_AUTH_WPA2PSK;
1323         else
1324                 return -1;
1325
1326         if (encr) {
1327                 if (os_strncmp(encr, "NONE", 4) == 0)
1328                         cred.encr_type = WPS_ENCR_NONE;
1329                 else if (os_strncmp(encr, "WEP", 3) == 0)
1330                         cred.encr_type = WPS_ENCR_WEP;
1331                 else if (os_strncmp(encr, "TKIP", 4) == 0)
1332                         cred.encr_type = WPS_ENCR_TKIP;
1333                 else if (os_strncmp(encr, "CCMP", 4) == 0)
1334                         cred.encr_type = WPS_ENCR_AES;
1335                 else
1336                         return -1;
1337         } else
1338                 cred.encr_type = WPS_ENCR_NONE;
1339
1340         if (key) {
1341                 len = os_strlen(key);
1342                 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1343                     hexstr2bin(key, cred.key, len / 2))
1344                         return -1;
1345                 cred.key_len = len / 2;
1346         }
1347
1348         return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1349 }