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