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