Enable wpa_msg() for hostapd
[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 "hostapd.h"
18 #include "driver_i.h"
19 #include "eloop.h"
20 #include "uuid.h"
21 #include "wpa_ctrl.h"
22 #include "ieee802_11_defs.h"
23 #include "wps/wps.h"
24 #include "wps/wps_defs.h"
25 #include "wps/wps_dev_attr.h"
26 #include "wps_hostapd.h"
27
28
29 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
30                                   size_t psk_len)
31 {
32         struct hostapd_data *hapd = ctx;
33         struct hostapd_wpa_psk *p;
34         struct hostapd_ssid *ssid = &hapd->conf->ssid;
35
36         wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
37                    MACSTR, MAC2STR(mac_addr));
38         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
39
40         if (psk_len != PMK_LEN) {
41                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
42                            (unsigned long) psk_len);
43                 return -1;
44         }
45
46         /* Add the new PSK to runtime PSK list */
47         p = os_zalloc(sizeof(*p));
48         if (p == NULL)
49                 return -1;
50         os_memcpy(p->addr, mac_addr, ETH_ALEN);
51         os_memcpy(p->psk, psk, PMK_LEN);
52
53         p->next = ssid->wpa_psk;
54         ssid->wpa_psk = p;
55
56         if (ssid->wpa_psk_file) {
57                 FILE *f;
58                 char hex[PMK_LEN * 2 + 1];
59                 /* Add the new PSK to PSK list file */
60                 f = fopen(ssid->wpa_psk_file, "a");
61                 if (f == NULL) {
62                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
63                                    "'%s'", ssid->wpa_psk_file);
64                         return -1;
65                 }
66
67                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
68                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
69                 fclose(f);
70         }
71
72         return 0;
73 }
74
75
76 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
77                                  size_t beacon_ie_len, const u8 *probe_resp_ie,
78                                  size_t probe_resp_ie_len)
79 {
80         struct hostapd_data *hapd = ctx;
81
82         os_free(hapd->wps_beacon_ie);
83         if (beacon_ie_len == 0) {
84                 hapd->wps_beacon_ie = NULL;
85                 hapd->wps_beacon_ie_len = 0;
86         } else {
87                 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
88                 if (hapd->wps_beacon_ie == NULL) {
89                         hapd->wps_beacon_ie_len = 0;
90                         return -1;
91                 }
92                 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
93                 hapd->wps_beacon_ie_len = beacon_ie_len;
94         }
95         hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
96                                   hapd->wps_beacon_ie_len);
97
98         os_free(hapd->wps_probe_resp_ie);
99         if (probe_resp_ie_len == 0) {
100                 hapd->wps_probe_resp_ie = NULL;
101                 hapd->wps_probe_resp_ie_len = 0;
102         } else {
103                 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
104                 if (hapd->wps_probe_resp_ie == NULL) {
105                         hapd->wps_probe_resp_ie_len = 0;
106                         return -1;
107                 }
108                 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
109                           probe_resp_ie_len);
110                 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
111         }
112         hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
113                                       hapd->wps_probe_resp_ie_len);
114
115         return 0;
116 }
117
118
119 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
120                                       const struct wps_device_data *dev)
121 {
122         struct hostapd_data *hapd = ctx;
123         char uuid[40], txt[400];
124         int len;
125         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
126                 return;
127         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
128         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
129                           "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
130                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
131                           dev->manufacturer, dev->model_name,
132                           dev->model_number, dev->serial_number,
133                           dev->categ, dev->oui, dev->sub_categ);
134         if (len > 0 && len < (int) sizeof(txt))
135                 wpa_msg(hapd, MSG_INFO, "%s", txt);
136
137         if (hapd->conf->wps_pin_requests) {
138                 FILE *f;
139                 struct os_time t;
140                 f = fopen(hapd->conf->wps_pin_requests, "a");
141                 if (f == NULL)
142                         return;
143                 os_get_time(&t);
144                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
145                         "\t%d-%08X-%d\n",
146                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
147                         dev->manufacturer, dev->model_name, dev->model_number,
148                         dev->serial_number,
149                         dev->categ, dev->oui, dev->sub_categ);
150                 fclose(f);
151         }
152 }
153
154
155 static int str_starts(const char *str, const char *start)
156 {
157         return os_strncmp(str, start, os_strlen(start)) == 0;
158 }
159
160
161 static void wps_reload_config(void *eloop_data, void *user_ctx)
162 {
163         struct hostapd_iface *iface = eloop_data;
164
165         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
166         if (hostapd_reload_config(iface) < 0) {
167                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
168                            "configuration");
169         }
170 }
171
172
173 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
174 {
175         struct hostapd_data *hapd = ctx;
176         FILE *oconf, *nconf;
177         size_t len, i;
178         char *tmp_fname;
179         char buf[1024];
180         int multi_bss;
181         int wpa;
182
183         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
184                         cred->cred_attr, cred->cred_attr_len);
185
186         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
187         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
188         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
189                    cred->auth_type);
190         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
191         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
192         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
193                         cred->key, cred->key_len);
194         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
195                    MAC2STR(cred->mac_addr));
196
197         wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
198
199         len = os_strlen(hapd->iface->config_fname) + 5;
200         tmp_fname = os_malloc(len);
201         if (tmp_fname == NULL)
202                 return -1;
203         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
204
205         oconf = fopen(hapd->iface->config_fname, "r");
206         if (oconf == NULL) {
207                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
208                            "configuration file");
209                 os_free(tmp_fname);
210                 return -1;
211         }
212
213         nconf = fopen(tmp_fname, "w");
214         if (nconf == NULL) {
215                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
216                            "configuration file");
217                 os_free(tmp_fname);
218                 fclose(oconf);
219                 return -1;
220         }
221
222         fprintf(nconf, "# WPS configuration - START\n");
223
224         fprintf(nconf, "wps_state=2\n");
225
226         fprintf(nconf, "ssid=");
227         for (i = 0; i < cred->ssid_len; i++)
228                 fputc(cred->ssid[i], nconf);
229         fprintf(nconf, "\n");
230
231         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
232             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
233                 wpa = 3;
234         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
235                 wpa = 2;
236         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
237                 wpa = 1;
238         else
239                 wpa = 0;
240
241         if (wpa) {
242                 char *prefix;
243                 fprintf(nconf, "wpa=%d\n", wpa);
244
245                 fprintf(nconf, "wpa_key_mgmt=");
246                 prefix = "";
247                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
248                         fprintf(nconf, "WPA-EAP");
249                         prefix = " ";
250                 }
251                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
252                         fprintf(nconf, "%sWPA-PSK", prefix);
253                 fprintf(nconf, "\n");
254
255                 fprintf(nconf, "wpa_pairwise=");
256                 prefix = "";
257                 if (cred->encr_type & WPS_ENCR_AES) {
258                         fprintf(nconf, "CCMP");
259                         prefix = " ";
260                 }
261                 if (cred->encr_type & WPS_ENCR_TKIP) {
262                         fprintf(nconf, "%sTKIP", prefix);
263                 }
264                 fprintf(nconf, "\n");
265
266                 if (cred->key_len >= 8 && cred->key_len < 64) {
267                         fprintf(nconf, "wpa_passphrase=");
268                         for (i = 0; i < cred->key_len; i++)
269                                 fputc(cred->key[i], nconf);
270                         fprintf(nconf, "\n");
271                 } else if (cred->key_len == 64) {
272                         fprintf(nconf, "wpa_psk=");
273                         for (i = 0; i < cred->key_len; i++)
274                                 fputc(cred->key[i], nconf);
275                         fprintf(nconf, "\n");
276                 } else {
277                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
278                                    "for WPA/WPA2",
279                                    (unsigned long) cred->key_len);
280                 }
281
282                 fprintf(nconf, "auth_algs=1\n");
283         } else {
284                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
285                     (cred->auth_type & WPS_AUTH_SHARED))
286                         fprintf(nconf, "auth_algs=3\n");
287                 else if (cred->auth_type & WPS_AUTH_SHARED)
288                         fprintf(nconf, "auth_algs=2\n");
289                 else
290                         fprintf(nconf, "auth_algs=1\n");
291
292                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
293                         fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
294                         fprintf(nconf, "wep_key%d=", cred->key_idx);
295                         if (cred->key_len != 10 && cred->key_len != 26)
296                                 fputc('"', nconf);
297                         for (i = 0; i < cred->key_len; i++)
298                                 fputc(cred->key[i], nconf);
299                         if (cred->key_len != 10 && cred->key_len != 26)
300                                 fputc('"', nconf);
301                         fprintf(nconf, "\n");
302                 }
303         }
304
305         fprintf(nconf, "# WPS configuration - END\n");
306
307         multi_bss = 0;
308         while (fgets(buf, sizeof(buf), oconf)) {
309                 if (os_strncmp(buf, "bss=", 4) == 0)
310                         multi_bss = 1;
311                 if (!multi_bss &&
312                     (str_starts(buf, "ssid=") ||
313                      str_starts(buf, "auth_algs=") ||
314                      str_starts(buf, "wps_state=") ||
315                      str_starts(buf, "wpa=") ||
316                      str_starts(buf, "wpa_psk=") ||
317                      str_starts(buf, "wpa_pairwise=") ||
318                      str_starts(buf, "rsn_pairwise=") ||
319                      str_starts(buf, "wpa_key_mgmt=") ||
320                      str_starts(buf, "wpa_passphrase="))) {
321                         fprintf(nconf, "#WPS# %s", buf);
322                 } else
323                         fprintf(nconf, "%s", buf);
324         }
325
326         fclose(nconf);
327         fclose(oconf);
328
329         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
330                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
331                            "configuration file: %s", strerror(errno));
332                 os_free(tmp_fname);
333                 return -1;
334         }
335
336         os_free(tmp_fname);
337
338         /* Schedule configuration reload after short period of time to allow
339          * EAP-WSC to be finished.
340          */
341         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
342                                NULL);
343
344         /* TODO: dualband AP may need to update multiple configuration files */
345
346         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
347
348         return 0;
349 }
350
351
352 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
353 {
354         os_free(hapd->wps_beacon_ie);
355         hapd->wps_beacon_ie = NULL;
356         hapd->wps_beacon_ie_len = 0;
357         hostapd_set_wps_beacon_ie(hapd, NULL, 0);
358
359         os_free(hapd->wps_probe_resp_ie);
360         hapd->wps_probe_resp_ie = NULL;
361         hapd->wps_probe_resp_ie_len = 0;
362         hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
363 }
364
365
366 int hostapd_init_wps(struct hostapd_data *hapd,
367                      struct hostapd_bss_config *conf)
368 {
369         struct wps_context *wps;
370         struct wps_registrar_config cfg;
371
372         if (conf->wps_state == 0) {
373                 hostapd_wps_clear_ies(hapd);
374                 return 0;
375         }
376
377         wps = os_zalloc(sizeof(*wps));
378         if (wps == NULL)
379                 return -1;
380
381         wps->cred_cb = hostapd_wps_cred_cb;
382         wps->cb_ctx = hapd;
383
384         os_memset(&cfg, 0, sizeof(cfg));
385         wps->wps_state = hapd->conf->wps_state;
386         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
387         if (is_nil_uuid(hapd->conf->uuid)) {
388                 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
389                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
390                             wps->uuid, UUID_LEN);
391         } else
392                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
393         wps->ssid_len = hapd->conf->ssid.ssid_len;
394         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
395         wps->ap = 1;
396         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
397         wps->dev.device_name = os_strdup(hapd->conf->device_name);
398         wps->dev.manufacturer = os_strdup(hapd->conf->manufacturer);
399         wps->dev.model_name = os_strdup(hapd->conf->model_name);
400         wps->dev.model_number = os_strdup(hapd->conf->model_number);
401         wps->dev.serial_number = os_strdup(hapd->conf->serial_number);
402         if (hapd->conf->config_methods) {
403                 char *m = hapd->conf->config_methods;
404                 if (os_strstr(m, "label"))
405                         wps->config_methods |= WPS_CONFIG_LABEL;
406                 if (os_strstr(m, "display"))
407                         wps->config_methods |= WPS_CONFIG_DISPLAY;
408                 if (os_strstr(m, "push_button"))
409                         wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
410                 if (os_strstr(m, "keypad"))
411                         wps->config_methods |= WPS_CONFIG_KEYPAD;
412         }
413         if (hapd->conf->device_type) {
414                 char *pos;
415                 u8 oui[4];
416                 /* <categ>-<OUI>-<subcateg> */
417                 wps->dev.categ = atoi(hapd->conf->device_type);
418                 pos = os_strchr(hapd->conf->device_type, '-');
419                 if (pos == NULL) {
420                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
421                         os_free(wps);
422                         return -1;
423                 }
424                 pos++;
425                 if (hexstr2bin(pos, oui, 4)) {
426                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
427                         os_free(wps);
428                         return -1;
429                 }
430                 wps->dev.oui = WPA_GET_BE32(oui);
431                 pos = os_strchr(pos, '-');
432                 if (pos == NULL) {
433                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
434                         os_free(wps);
435                         return -1;
436                 }
437                 pos++;
438                 wps->dev.sub_categ = atoi(pos);
439         }
440         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
441         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
442                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
443
444         if (conf->wpa & WPA_PROTO_RSN) {
445                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
446                         wps->auth_types |= WPS_AUTH_WPA2PSK;
447                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
448                         wps->auth_types |= WPS_AUTH_WPA2;
449
450                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
451                         wps->encr_types |= WPS_ENCR_AES;
452                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
453                         wps->encr_types |= WPS_ENCR_TKIP;
454         }
455
456         if (conf->wpa & WPA_PROTO_WPA) {
457                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
458                         wps->auth_types |= WPS_AUTH_WPAPSK;
459                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
460                         wps->auth_types |= WPS_AUTH_WPA;
461
462                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
463                         wps->encr_types |= WPS_ENCR_AES;
464                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
465                         wps->encr_types |= WPS_ENCR_TKIP;
466         }
467
468         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
469                 wps->encr_types |= WPS_ENCR_NONE;
470                 wps->auth_types |= WPS_AUTH_OPEN;
471         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
472                 wps->encr_types |= WPS_ENCR_WEP;
473                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
474                         wps->auth_types |= WPS_AUTH_OPEN;
475                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
476                         wps->auth_types |= WPS_AUTH_SHARED;
477         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
478                 wps->auth_types |= WPS_AUTH_OPEN;
479                 if (conf->default_wep_key_len)
480                         wps->encr_types |= WPS_ENCR_WEP;
481                 else
482                         wps->encr_types |= WPS_ENCR_NONE;
483         }
484
485         if (conf->ssid.wpa_psk_file) {
486                 /* Use per-device PSKs */
487         } else if (conf->ssid.wpa_passphrase) {
488                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
489                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
490         } else if (conf->ssid.wpa_psk) {
491                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
492                 if (wps->network_key == NULL) {
493                         os_free(wps);
494                         return -1;
495                 }
496                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
497                                  conf->ssid.wpa_psk->psk, PMK_LEN);
498                 wps->network_key_len = 2 * PMK_LEN;
499         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
500                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
501                 if (wps->network_key == NULL) {
502                         os_free(wps);
503                         return -1;
504                 }
505                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
506                           conf->ssid.wep.len[0]);
507                 wps->network_key_len = conf->ssid.wep.len[0];
508         }
509
510         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
511                 /* Override parameters to enable security by default */
512                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
513                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
514         }
515
516         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
517         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
518         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
519         cfg.cb_ctx = hapd;
520         cfg.skip_cred_build = conf->skip_cred_build;
521         cfg.extra_cred = conf->extra_cred;
522         cfg.extra_cred_len = conf->extra_cred_len;
523
524         wps->registrar = wps_registrar_init(wps, &cfg);
525         if (wps->registrar == NULL) {
526                 printf("Failed to initialize WPS Registrar\n");
527                 os_free(wps->network_key);
528                 os_free(wps);
529                 return -1;
530         }
531
532         hapd->wps = wps;
533
534         return 0;
535 }
536
537
538 void hostapd_deinit_wps(struct hostapd_data *hapd)
539 {
540         if (hapd->wps == NULL)
541                 return;
542         wps_registrar_deinit(hapd->wps->registrar);
543         os_free(hapd->wps->network_key);
544         wps_device_data_free(&hapd->wps->dev);
545         os_free(hapd->wps);
546         hapd->wps = NULL;
547         hostapd_wps_clear_ies(hapd);
548 }
549
550
551 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
552                         const char *pin)
553 {
554         u8 u[UUID_LEN];
555         int any = 0;
556
557         if (hapd->wps == NULL)
558                 return -1;
559         if (os_strcmp(uuid, "any") == 0)
560                 any = 1;
561         else if (uuid_str2bin(uuid, u))
562                 return -1;
563         return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
564                                      (const u8 *) pin, os_strlen(pin));
565 }
566
567
568 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
569 {
570         if (hapd->wps == NULL)
571                 return -1;
572         return wps_registrar_button_pushed(hapd->wps->registrar);
573 }
574
575
576 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
577                               const u8 *ie, size_t ie_len)
578 {
579         struct wpabuf *wps_ie;
580         const u8 *end, *pos, *wps;
581
582         if (hapd->wps == NULL)
583                 return;
584
585         pos = ie;
586         end = ie + ie_len;
587         wps = NULL;
588
589         while (pos + 1 < end) {
590                 if (pos + 2 + pos[1] > end)
591                         return;
592                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
593                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
594                         wps = pos;
595                         break;
596                 }
597                 pos += 2 + pos[1];
598         }
599
600         if (wps == NULL)
601                 return; /* No WPS IE in Probe Request */
602
603         wps_ie = wpabuf_alloc(ie_len);
604         if (wps_ie == NULL)
605                 return;
606
607         /* There may be multiple WPS IEs in the message, so need to concatenate
608          * their WPS Data fields */
609         while (pos + 1 < end) {
610                 if (pos + 2 + pos[1] > end)
611                         break;
612                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
613                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
614                         wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
615                 pos += 2 + pos[1];
616         }
617
618         if (wpabuf_len(wps_ie) > 0)
619                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
620
621         wpabuf_free(wps_ie);
622 }