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