WPS: Remove unnecessary SetSelectedRegistrar callback
[libeap.git] / hostapd / wps_hostapd.c
1 /*
2  * hostapd / WPS integration
3  * Copyright (c) 2008, 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 "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "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 "driver_i.h"
31 #include "sta_flags.h"
32 #include "sta_info.h"
33 #include "wps_hostapd.h"
34
35
36 #ifdef CONFIG_WPS_UPNP
37 #include "wps/wps_upnp.h"
38 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
39                                  struct wps_context *wps);
40 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
41 #endif /* CONFIG_WPS_UPNP */
42
43 static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
44                                      const u8 *ie, size_t ie_len);
45
46
47 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
48                                   size_t psk_len)
49 {
50         struct hostapd_data *hapd = ctx;
51         struct hostapd_wpa_psk *p;
52         struct hostapd_ssid *ssid = &hapd->conf->ssid;
53
54         wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
55                    MACSTR, MAC2STR(mac_addr));
56         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
57
58         if (psk_len != PMK_LEN) {
59                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
60                            (unsigned long) psk_len);
61                 return -1;
62         }
63
64         /* Add the new PSK to runtime PSK list */
65         p = os_zalloc(sizeof(*p));
66         if (p == NULL)
67                 return -1;
68         os_memcpy(p->addr, mac_addr, ETH_ALEN);
69         os_memcpy(p->psk, psk, PMK_LEN);
70
71         p->next = ssid->wpa_psk;
72         ssid->wpa_psk = p;
73
74         if (ssid->wpa_psk_file) {
75                 FILE *f;
76                 char hex[PMK_LEN * 2 + 1];
77                 /* Add the new PSK to PSK list file */
78                 f = fopen(ssid->wpa_psk_file, "a");
79                 if (f == NULL) {
80                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
81                                    "'%s'", ssid->wpa_psk_file);
82                         return -1;
83                 }
84
85                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
86                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
87                 fclose(f);
88         }
89
90         return 0;
91 }
92
93
94 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
95                                  size_t beacon_ie_len, const u8 *probe_resp_ie,
96                                  size_t probe_resp_ie_len)
97 {
98         struct hostapd_data *hapd = ctx;
99
100         os_free(hapd->wps_beacon_ie);
101         if (beacon_ie_len == 0) {
102                 hapd->wps_beacon_ie = NULL;
103                 hapd->wps_beacon_ie_len = 0;
104         } else {
105                 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
106                 if (hapd->wps_beacon_ie == NULL) {
107                         hapd->wps_beacon_ie_len = 0;
108                         return -1;
109                 }
110                 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
111                 hapd->wps_beacon_ie_len = beacon_ie_len;
112         }
113         hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
114                                   hapd->wps_beacon_ie_len);
115
116         os_free(hapd->wps_probe_resp_ie);
117         if (probe_resp_ie_len == 0) {
118                 hapd->wps_probe_resp_ie = NULL;
119                 hapd->wps_probe_resp_ie_len = 0;
120         } else {
121                 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
122                 if (hapd->wps_probe_resp_ie == NULL) {
123                         hapd->wps_probe_resp_ie_len = 0;
124                         return -1;
125                 }
126                 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
127                           probe_resp_ie_len);
128                 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
129         }
130         hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
131                                       hapd->wps_probe_resp_ie_len);
132
133         return 0;
134 }
135
136
137 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
138                                       const struct wps_device_data *dev)
139 {
140         struct hostapd_data *hapd = ctx;
141         char uuid[40], txt[400];
142         int len;
143         char devtype[WPS_DEV_TYPE_BUFSIZE];
144         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
145                 return;
146         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
147         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
148                           "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
149                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
150                           dev->manufacturer, dev->model_name,
151                           dev->model_number, dev->serial_number,
152                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
153                                                sizeof(devtype)));
154         if (len > 0 && len < (int) sizeof(txt))
155                 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
156
157         if (hapd->conf->wps_pin_requests) {
158                 FILE *f;
159                 struct os_time t;
160                 f = fopen(hapd->conf->wps_pin_requests, "a");
161                 if (f == NULL)
162                         return;
163                 os_get_time(&t);
164                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
165                         "\t%s\n",
166                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
167                         dev->manufacturer, dev->model_name, dev->model_number,
168                         dev->serial_number,
169                         wps_dev_type_bin2str(dev->pri_dev_type, devtype,
170                                              sizeof(devtype)));
171                 fclose(f);
172         }
173 }
174
175
176 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
177                                        const u8 *uuid_e)
178 {
179         struct hostapd_data *hapd = ctx;
180         char uuid[40];
181         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
182                 return;
183         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
184                 MAC2STR(mac_addr), uuid);
185 }
186
187
188 static int str_starts(const char *str, const char *start)
189 {
190         return os_strncmp(str, start, os_strlen(start)) == 0;
191 }
192
193
194 static void wps_reload_config(void *eloop_data, void *user_ctx)
195 {
196         struct hostapd_iface *iface = eloop_data;
197
198         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
199         if (hostapd_reload_config(iface) < 0) {
200                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
201                            "configuration");
202         }
203 }
204
205
206 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
207 {
208         struct hostapd_data *hapd = ctx;
209         FILE *oconf, *nconf;
210         size_t len, i;
211         char *tmp_fname;
212         char buf[1024];
213         int multi_bss;
214         int wpa;
215
216         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
217                         cred->cred_attr, cred->cred_attr_len);
218
219         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
220         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
221         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
222                    cred->auth_type);
223         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
224         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
225         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
226                         cred->key, cred->key_len);
227         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
228                    MAC2STR(cred->mac_addr));
229
230         if ((hapd->conf->wps_cred_processing == 1 ||
231              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
232                 size_t blen = cred->cred_attr_len * 2 + 1;
233                 char *_buf = os_malloc(blen);
234                 if (_buf) {
235                         wpa_snprintf_hex(_buf, blen,
236                                          cred->cred_attr, cred->cred_attr_len);
237                         wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
238                                 WPS_EVENT_NEW_AP_SETTINGS, _buf);
239                         os_free(_buf);
240                 }
241         } else
242                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
243
244         if (hapd->conf->wps_cred_processing == 1)
245                 return 0;
246
247         os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
248         hapd->wps->ssid_len = cred->ssid_len;
249         hapd->wps->encr_types = cred->encr_type;
250         hapd->wps->auth_types = cred->auth_type;
251         if (cred->key_len == 0) {
252                 os_free(hapd->wps->network_key);
253                 hapd->wps->network_key = NULL;
254                 hapd->wps->network_key_len = 0;
255         } else {
256                 if (hapd->wps->network_key == NULL ||
257                     hapd->wps->network_key_len < cred->key_len) {
258                         hapd->wps->network_key_len = 0;
259                         os_free(hapd->wps->network_key);
260                         hapd->wps->network_key = os_malloc(cred->key_len);
261                         if (hapd->wps->network_key == NULL)
262                                 return -1;
263                 }
264                 hapd->wps->network_key_len = cred->key_len;
265                 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
266         }
267         hapd->wps->wps_state = WPS_STATE_CONFIGURED;
268
269         len = os_strlen(hapd->iface->config_fname) + 5;
270         tmp_fname = os_malloc(len);
271         if (tmp_fname == NULL)
272                 return -1;
273         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
274
275         oconf = fopen(hapd->iface->config_fname, "r");
276         if (oconf == NULL) {
277                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
278                            "configuration file");
279                 os_free(tmp_fname);
280                 return -1;
281         }
282
283         nconf = fopen(tmp_fname, "w");
284         if (nconf == NULL) {
285                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
286                            "configuration file");
287                 os_free(tmp_fname);
288                 fclose(oconf);
289                 return -1;
290         }
291
292         fprintf(nconf, "# WPS configuration - START\n");
293
294         fprintf(nconf, "wps_state=2\n");
295
296         fprintf(nconf, "ssid=");
297         for (i = 0; i < cred->ssid_len; i++)
298                 fputc(cred->ssid[i], nconf);
299         fprintf(nconf, "\n");
300
301         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
302             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
303                 wpa = 3;
304         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
305                 wpa = 2;
306         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
307                 wpa = 1;
308         else
309                 wpa = 0;
310
311         if (wpa) {
312                 char *prefix;
313                 fprintf(nconf, "wpa=%d\n", wpa);
314
315                 fprintf(nconf, "wpa_key_mgmt=");
316                 prefix = "";
317                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
318                         fprintf(nconf, "WPA-EAP");
319                         prefix = " ";
320                 }
321                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
322                         fprintf(nconf, "%sWPA-PSK", prefix);
323                 fprintf(nconf, "\n");
324
325                 fprintf(nconf, "wpa_pairwise=");
326                 prefix = "";
327                 if (cred->encr_type & WPS_ENCR_AES) {
328                         fprintf(nconf, "CCMP");
329                         prefix = " ";
330                 }
331                 if (cred->encr_type & WPS_ENCR_TKIP) {
332                         fprintf(nconf, "%sTKIP", prefix);
333                 }
334                 fprintf(nconf, "\n");
335
336                 if (cred->key_len >= 8 && cred->key_len < 64) {
337                         fprintf(nconf, "wpa_passphrase=");
338                         for (i = 0; i < cred->key_len; i++)
339                                 fputc(cred->key[i], nconf);
340                         fprintf(nconf, "\n");
341                 } else if (cred->key_len == 64) {
342                         fprintf(nconf, "wpa_psk=");
343                         for (i = 0; i < cred->key_len; i++)
344                                 fputc(cred->key[i], nconf);
345                         fprintf(nconf, "\n");
346                 } else {
347                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
348                                    "for WPA/WPA2",
349                                    (unsigned long) cred->key_len);
350                 }
351
352                 fprintf(nconf, "auth_algs=1\n");
353         } else {
354                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
355                     (cred->auth_type & WPS_AUTH_SHARED))
356                         fprintf(nconf, "auth_algs=3\n");
357                 else if (cred->auth_type & WPS_AUTH_SHARED)
358                         fprintf(nconf, "auth_algs=2\n");
359                 else
360                         fprintf(nconf, "auth_algs=1\n");
361
362                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
363                         int key_idx = cred->key_idx;
364                         if (key_idx)
365                                 key_idx--;
366                         fprintf(nconf, "wep_default_key=%d\n", key_idx);
367                         fprintf(nconf, "wep_key%d=", key_idx);
368                         if (cred->key_len == 10 || cred->key_len == 26) {
369                                 /* WEP key as a hex string */
370                                 for (i = 0; i < cred->key_len; i++)
371                                         fputc(cred->key[i], nconf);
372                         } else {
373                                 /* Raw WEP key; convert to hex */
374                                 for (i = 0; i < cred->key_len; i++)
375                                         fprintf(nconf, "%02x", cred->key[i]);
376                         }
377                         fprintf(nconf, "\n");
378                 }
379         }
380
381         fprintf(nconf, "# WPS configuration - END\n");
382
383         multi_bss = 0;
384         while (fgets(buf, sizeof(buf), oconf)) {
385                 if (os_strncmp(buf, "bss=", 4) == 0)
386                         multi_bss = 1;
387                 if (!multi_bss &&
388                     (str_starts(buf, "ssid=") ||
389                      str_starts(buf, "auth_algs=") ||
390                      str_starts(buf, "wps_state=") ||
391                      str_starts(buf, "wpa=") ||
392                      str_starts(buf, "wpa_psk=") ||
393                      str_starts(buf, "wpa_pairwise=") ||
394                      str_starts(buf, "rsn_pairwise=") ||
395                      str_starts(buf, "wpa_key_mgmt=") ||
396                      str_starts(buf, "wpa_passphrase="))) {
397                         fprintf(nconf, "#WPS# %s", buf);
398                 } else
399                         fprintf(nconf, "%s", buf);
400         }
401
402         fclose(nconf);
403         fclose(oconf);
404
405         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
406                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
407                            "configuration file: %s", strerror(errno));
408                 os_free(tmp_fname);
409                 return -1;
410         }
411
412         os_free(tmp_fname);
413
414         /* Schedule configuration reload after short period of time to allow
415          * EAP-WSC to be finished.
416          */
417         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
418                                NULL);
419
420         /* TODO: dualband AP may need to update multiple configuration files */
421
422         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
423
424         return 0;
425 }
426
427
428 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
429                                   struct wps_event_pwd_auth_fail *data)
430 {
431         FILE *f;
432
433         if (!data->enrollee)
434                 return;
435
436         /*
437          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
438          * if this happens multiple times.
439          */
440         hapd->ap_pin_failures++;
441         if (hapd->ap_pin_failures < 4)
442                 return;
443
444         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
445         hapd->wps->ap_setup_locked = 1;
446
447         wps_registrar_update_ie(hapd->wps->registrar);
448
449         if (hapd->conf->wps_cred_processing == 1)
450                 return;
451
452         f = fopen(hapd->iface->config_fname, "a");
453         if (f == NULL) {
454                 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
455                            "configuration file");
456                 return;
457         }
458
459         fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
460         fprintf(f, "ap_setup_locked=1\n");
461         fclose(f);
462
463         /* TODO: dualband AP may need to update multiple configuration files */
464
465         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
466 }
467
468
469 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
470                                  union wps_event_data *data)
471 {
472         struct hostapd_data *hapd = ctx;
473
474         if (event == WPS_EV_PWD_AUTH_FAIL)
475                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
476 }
477
478
479 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
480 {
481         os_free(hapd->wps_beacon_ie);
482         hapd->wps_beacon_ie = NULL;
483         hapd->wps_beacon_ie_len = 0;
484         hostapd_set_wps_beacon_ie(hapd, NULL, 0);
485
486         os_free(hapd->wps_probe_resp_ie);
487         hapd->wps_probe_resp_ie = NULL;
488         hapd->wps_probe_resp_ie_len = 0;
489         hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
490 }
491
492
493 int hostapd_init_wps(struct hostapd_data *hapd,
494                      struct hostapd_bss_config *conf)
495 {
496         struct wps_context *wps;
497         struct wps_registrar_config cfg;
498
499         if (conf->wps_state == 0) {
500                 hostapd_wps_clear_ies(hapd);
501                 return 0;
502         }
503
504         wps = os_zalloc(sizeof(*wps));
505         if (wps == NULL)
506                 return -1;
507
508         wps->cred_cb = hostapd_wps_cred_cb;
509         wps->event_cb = hostapd_wps_event_cb;
510         wps->cb_ctx = hapd;
511
512         os_memset(&cfg, 0, sizeof(cfg));
513         wps->wps_state = hapd->conf->wps_state;
514         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
515         if (is_nil_uuid(hapd->conf->uuid)) {
516                 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
517                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
518                             wps->uuid, UUID_LEN);
519         } else
520                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
521         wps->ssid_len = hapd->conf->ssid.ssid_len;
522         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
523         wps->ap = 1;
524         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
525         wps->dev.device_name = hapd->conf->device_name ?
526                 os_strdup(hapd->conf->device_name) : NULL;
527         wps->dev.manufacturer = hapd->conf->manufacturer ?
528                 os_strdup(hapd->conf->manufacturer) : NULL;
529         wps->dev.model_name = hapd->conf->model_name ?
530                 os_strdup(hapd->conf->model_name) : NULL;
531         wps->dev.model_number = hapd->conf->model_number ?
532                 os_strdup(hapd->conf->model_number) : NULL;
533         wps->dev.serial_number = hapd->conf->serial_number ?
534                 os_strdup(hapd->conf->serial_number) : NULL;
535         if (hapd->conf->config_methods) {
536                 char *m = hapd->conf->config_methods;
537                 if (os_strstr(m, "label"))
538                         wps->config_methods |= WPS_CONFIG_LABEL;
539                 if (os_strstr(m, "display"))
540                         wps->config_methods |= WPS_CONFIG_DISPLAY;
541                 if (os_strstr(m, "push_button"))
542                         wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
543                 if (os_strstr(m, "keypad"))
544                         wps->config_methods |= WPS_CONFIG_KEYPAD;
545         }
546         if (hapd->conf->device_type &&
547             wps_dev_type_str2bin(hapd->conf->device_type,
548                                  wps->dev.pri_dev_type) < 0) {
549                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
550                 os_free(wps);
551                 return -1;
552         }
553         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
554         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
555                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
556
557         if (conf->wpa & WPA_PROTO_RSN) {
558                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
559                         wps->auth_types |= WPS_AUTH_WPA2PSK;
560                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
561                         wps->auth_types |= WPS_AUTH_WPA2;
562
563                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
564                         wps->encr_types |= WPS_ENCR_AES;
565                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
566                         wps->encr_types |= WPS_ENCR_TKIP;
567         }
568
569         if (conf->wpa & WPA_PROTO_WPA) {
570                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
571                         wps->auth_types |= WPS_AUTH_WPAPSK;
572                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
573                         wps->auth_types |= WPS_AUTH_WPA;
574
575                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
576                         wps->encr_types |= WPS_ENCR_AES;
577                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
578                         wps->encr_types |= WPS_ENCR_TKIP;
579         }
580
581         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
582                 wps->encr_types |= WPS_ENCR_NONE;
583                 wps->auth_types |= WPS_AUTH_OPEN;
584         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
585                 wps->encr_types |= WPS_ENCR_WEP;
586                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
587                         wps->auth_types |= WPS_AUTH_OPEN;
588                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
589                         wps->auth_types |= WPS_AUTH_SHARED;
590         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
591                 wps->auth_types |= WPS_AUTH_OPEN;
592                 if (conf->default_wep_key_len)
593                         wps->encr_types |= WPS_ENCR_WEP;
594                 else
595                         wps->encr_types |= WPS_ENCR_NONE;
596         }
597
598         if (conf->ssid.wpa_psk_file) {
599                 /* Use per-device PSKs */
600         } else if (conf->ssid.wpa_passphrase) {
601                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
602                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
603         } else if (conf->ssid.wpa_psk) {
604                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
605                 if (wps->network_key == NULL) {
606                         os_free(wps);
607                         return -1;
608                 }
609                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
610                                  conf->ssid.wpa_psk->psk, PMK_LEN);
611                 wps->network_key_len = 2 * PMK_LEN;
612         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
613                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
614                 if (wps->network_key == NULL) {
615                         os_free(wps);
616                         return -1;
617                 }
618                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
619                           conf->ssid.wep.len[0]);
620                 wps->network_key_len = conf->ssid.wep.len[0];
621         }
622
623         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
624                 /* Override parameters to enable security by default */
625                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
626                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
627         }
628
629         wps->ap_settings = conf->ap_settings;
630         wps->ap_settings_len = conf->ap_settings_len;
631
632         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
633         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
634         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
635         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
636         cfg.cb_ctx = hapd;
637         cfg.skip_cred_build = conf->skip_cred_build;
638         cfg.extra_cred = conf->extra_cred;
639         cfg.extra_cred_len = conf->extra_cred_len;
640         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
641                 conf->skip_cred_build;
642         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
643                 cfg.static_wep_only = 1;
644
645         wps->registrar = wps_registrar_init(wps, &cfg);
646         if (wps->registrar == NULL) {
647                 printf("Failed to initialize WPS Registrar\n");
648                 os_free(wps->network_key);
649                 os_free(wps);
650                 return -1;
651         }
652
653 #ifdef CONFIG_WPS_UPNP
654         wps->friendly_name = hapd->conf->friendly_name;
655         wps->manufacturer_url = hapd->conf->manufacturer_url;
656         wps->model_description = hapd->conf->model_description;
657         wps->model_url = hapd->conf->model_url;
658         wps->upc = hapd->conf->upc;
659
660         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
661                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
662                 wps_registrar_deinit(wps->registrar);
663                 os_free(wps->network_key);
664                 os_free(wps);
665                 return -1;
666         }
667 #endif /* CONFIG_WPS_UPNP */
668
669         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
670
671         hapd->wps = wps;
672
673         return 0;
674 }
675
676
677 void hostapd_deinit_wps(struct hostapd_data *hapd)
678 {
679         if (hapd->wps == NULL)
680                 return;
681 #ifdef CONFIG_WPS_UPNP
682         hostapd_wps_upnp_deinit(hapd);
683 #endif /* CONFIG_WPS_UPNP */
684         wps_registrar_deinit(hapd->wps->registrar);
685         os_free(hapd->wps->network_key);
686         wps_device_data_free(&hapd->wps->dev);
687         wpabuf_free(hapd->wps->dh_pubkey);
688         wpabuf_free(hapd->wps->dh_privkey);
689         wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
690         wpabuf_free(hapd->wps->oob_conf.dev_password);
691         wps_free_pending_msgs(hapd->wps->upnp_msgs);
692         os_free(hapd->wps);
693         hapd->wps = NULL;
694         hostapd_wps_clear_ies(hapd);
695 }
696
697
698 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
699                         const char *pin, int timeout)
700 {
701         u8 u[UUID_LEN];
702         int any = 0;
703
704         if (hapd->wps == NULL)
705                 return -1;
706         if (os_strcmp(uuid, "any") == 0)
707                 any = 1;
708         else if (uuid_str2bin(uuid, u))
709                 return -1;
710         return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
711                                      (const u8 *) pin, os_strlen(pin),
712                                      timeout);
713 }
714
715
716 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
717 {
718         if (hapd->wps == NULL)
719                 return -1;
720         return wps_registrar_button_pushed(hapd->wps->registrar);
721 }
722
723
724 #ifdef CONFIG_WPS_OOB
725 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
726                           char *path, char *method, char *name)
727 {
728         struct wps_context *wps = hapd->wps;
729         struct oob_device_data *oob_dev;
730
731         oob_dev = wps_get_oob_device(device_type);
732         if (oob_dev == NULL)
733                 return -1;
734         oob_dev->device_path = path;
735         oob_dev->device_name = name;
736         wps->oob_conf.oob_method = wps_get_oob_method(method);
737
738         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
739                 /*
740                  * Use pre-configured DH keys in order to be able to write the
741                  * key hash into the OOB file.
742                  */
743                 wpabuf_free(wps->dh_pubkey);
744                 wpabuf_free(wps->dh_privkey);
745                 wps->dh_privkey = NULL;
746                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
747                                          &wps->dh_privkey);
748                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
749                 if (wps->dh_pubkey == NULL) {
750                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
751                                    "Diffie-Hellman handshake");
752                         return -1;
753                 }
754         }
755
756         if (wps_process_oob(wps, oob_dev, 1) < 0)
757                 goto error;
758
759         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
760              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
761             hostapd_wps_add_pin(hapd, "any",
762                                 wpabuf_head(wps->oob_conf.dev_password), 0) <
763             0)
764                 goto error;
765
766         return 0;
767
768 error:
769         wpabuf_free(wps->dh_pubkey);
770         wps->dh_pubkey = NULL;
771         wpabuf_free(wps->dh_privkey);
772         wps->dh_privkey = NULL;
773         return -1;
774 }
775 #endif /* CONFIG_WPS_OOB */
776
777
778 static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
779                                      const u8 *ie, size_t ie_len)
780 {
781         struct hostapd_data *hapd = ctx;
782         struct wpabuf *wps_ie;
783
784         if (hapd->wps == NULL)
785                 return;
786
787         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
788         if (wps_ie == NULL)
789                 return;
790
791         if (wpabuf_len(wps_ie) > 0) {
792                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
793 #ifdef CONFIG_WPS_UPNP
794                 /* FIX: what exactly should be included in the WLANEvent?
795                  * WPS attributes? Full ProbeReq frame? */
796                 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
797                                                 UPNP_WPS_WLANEVENT_TYPE_PROBE,
798                                                 wps_ie);
799 #endif /* CONFIG_WPS_UPNP */
800         }
801
802         wpabuf_free(wps_ie);
803 }
804
805
806 #ifdef CONFIG_WPS_UPNP
807
808 static struct wpabuf *
809 hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
810 {
811         struct hostapd_data *hapd = priv;
812         struct wps_config cfg;
813         struct wps_data *wps;
814         enum wsc_op_code op_code;
815         struct wpabuf *m1;
816
817         /*
818          * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
819          * registration over UPnP with the AP acting as an Enrollee. It should
820          * be noted that this is frequently used just to get the device data,
821          * i.e., there may not be any intent to actually complete the
822          * registration.
823          */
824
825         if (peer->wps)
826                 wps_deinit(peer->wps);
827
828         os_memset(&cfg, 0, sizeof(cfg));
829         cfg.wps = hapd->wps;
830         cfg.pin = (u8 *) hapd->conf->ap_pin;
831         cfg.pin_len = os_strlen(hapd->conf->ap_pin);
832         wps = wps_init(&cfg);
833         if (wps == NULL)
834                 return NULL;
835
836         m1 = wps_get_msg(wps, &op_code);
837         if (m1 == NULL) {
838                 wps_deinit(wps);
839                 return NULL;
840         }
841
842         peer->wps = wps;
843
844         return m1;
845 }
846
847
848 static struct wpabuf *
849 hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
850                            const struct wpabuf *msg)
851 {
852         enum wps_process_res res;
853         enum wsc_op_code op_code;
854
855         /* PutMessage: msg = InMessage, return OutMessage */
856         res = wps_process_msg(peer->wps, WSC_UPnP, msg);
857         if (res == WPS_FAILURE)
858                 return NULL;
859         return wps_get_msg(peer->wps, &op_code);
860 }
861
862
863 static int hostapd_rx_req_put_wlan_response(
864         void *priv, enum upnp_wps_wlanevent_type ev_type,
865         const u8 *mac_addr, const struct wpabuf *msg,
866         enum wps_msg_type msg_type)
867 {
868         struct hostapd_data *hapd = priv;
869         struct sta_info *sta;
870         struct upnp_pending_message *p;
871
872         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
873                    MACSTR, ev_type, MAC2STR(mac_addr));
874         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
875                     wpabuf_head(msg), wpabuf_len(msg));
876         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
877                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
878                            "PutWLANResponse WLANEventType %d", ev_type);
879                 return -1;
880         }
881
882         /*
883          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
884          * server implementation for delivery to the peer.
885          */
886
887         sta = ap_get_sta(hapd, mac_addr);
888         if (!sta) {
889                 /*
890                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
891                  * Pick STA that is in an ongoing WPS registration without
892                  * checking the MAC address.
893                  */
894                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
895                            "on NewWLANEventMAC; try wildcard match");
896                 for (sta = hapd->sta_list; sta; sta = sta->next) {
897                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
898                                 break;
899                 }
900         }
901
902         if (!sta) {
903                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
904                 return 0;
905         }
906
907         p = os_zalloc(sizeof(*p));
908         if (p == NULL)
909                 return -1;
910         os_memcpy(p->addr, sta->addr, ETH_ALEN);
911         p->msg = wpabuf_dup(msg);
912         p->type = msg_type;
913         p->next = hapd->wps->upnp_msgs;
914         hapd->wps->upnp_msgs = p;
915
916         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
917 }
918
919
920 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
921                                  struct wps_context *wps)
922 {
923         struct upnp_wps_device_ctx *ctx;
924
925         if (!hapd->conf->upnp_iface)
926                 return 0;
927         ctx = os_zalloc(sizeof(*ctx));
928         if (ctx == NULL)
929                 return -1;
930
931         ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
932         ctx->rx_req_put_message = hostapd_rx_req_put_message;
933         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
934
935         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
936         if (hapd->wps_upnp == NULL) {
937                 os_free(ctx);
938                 return -1;
939         }
940         wps->wps_upnp = hapd->wps_upnp;
941
942         if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
943                 upnp_wps_device_deinit(hapd->wps_upnp);
944                 hapd->wps_upnp = NULL;
945                 return -1;
946         }
947
948         return 0;
949 }
950
951
952 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
953 {
954         upnp_wps_device_deinit(hapd->wps_upnp);
955 }
956
957 #endif /* CONFIG_WPS_UPNP */
958
959
960 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
961                             char *buf, size_t buflen)
962 {
963         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
964 }