Added preliminary Wi-Fi Protected Setup (WPS) implementation
[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.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_printf(MSG_DEBUG, "WPS: Received new AP Settings");
185         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
186         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
187                    cred->auth_type);
188         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
189         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
190         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
191                         cred->key, cred->key_len);
192         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
193                    MAC2STR(cred->mac_addr));
194
195         hostapd_ctrl_iface_send(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS,
196                                 os_strlen(WPS_EVENT_NEW_AP_SETTINGS));
197
198         len = os_strlen(hapd->iface->config_fname) + 5;
199         tmp_fname = os_malloc(len);
200         if (tmp_fname == NULL)
201                 return -1;
202         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
203
204         oconf = fopen(hapd->iface->config_fname, "r");
205         if (oconf == NULL) {
206                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
207                            "configuration file");
208                 os_free(tmp_fname);
209                 return -1;
210         }
211
212         nconf = fopen(tmp_fname, "w");
213         if (nconf == NULL) {
214                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
215                            "configuration file");
216                 os_free(tmp_fname);
217                 fclose(oconf);
218                 return -1;
219         }
220
221         fprintf(nconf, "# WPS configuration - START\n");
222
223         fprintf(nconf, "wps_state=2\n");
224
225         fprintf(nconf, "ssid=");
226         for (i = 0; i < cred->ssid_len; i++)
227                 fputc(cred->ssid[i], nconf);
228         fprintf(nconf, "\n");
229
230         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
231             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
232                 wpa = 3;
233         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
234                 wpa = 2;
235         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
236                 wpa = 1;
237         else
238                 wpa = 0;
239
240         if (wpa) {
241                 char *prefix;
242                 fprintf(nconf, "wpa=%d\n", wpa);
243
244                 fprintf(nconf, "wpa_key_mgmt=");
245                 prefix = "";
246                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
247                         fprintf(nconf, "WPA-EAP");
248                         prefix = " ";
249                 }
250                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
251                         fprintf(nconf, "%sWPA-PSK", prefix);
252                 fprintf(nconf, "\n");
253
254                 fprintf(nconf, "wpa_pairwise=");
255                 prefix = "";
256                 if (cred->encr_type & WPS_ENCR_AES) {
257                         fprintf(nconf, "CCMP");
258                         prefix = " ";
259                 }
260                 if (cred->encr_type & WPS_ENCR_TKIP) {
261                         fprintf(nconf, "%sTKIP", prefix);
262                 }
263                 fprintf(nconf, "\n");
264
265                 if (cred->key_len >= 8 && cred->key_len < 64) {
266                         fprintf(nconf, "wpa_passphrase=");
267                         for (i = 0; i < cred->key_len; i++)
268                                 fputc(cred->key[i], nconf);
269                         fprintf(nconf, "\n");
270                 } else if (cred->key_len == 64) {
271                         fprintf(nconf, "wpa_psk=");
272                         for (i = 0; i < cred->key_len; i++)
273                                 fputc(cred->key[i], nconf);
274                         fprintf(nconf, "\n");
275                 } else {
276                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
277                                    "for WPA/WPA2",
278                                    (unsigned long) cred->key_len);
279                 }
280
281                 fprintf(nconf, "auth_algs=1\n");
282         } else {
283                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
284                     (cred->auth_type & WPS_AUTH_SHARED))
285                         fprintf(nconf, "auth_algs=3\n");
286                 else if (cred->auth_type & WPS_AUTH_SHARED)
287                         fprintf(nconf, "auth_algs=2\n");
288                 else
289                         fprintf(nconf, "auth_algs=1\n");
290
291                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
292                         fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
293                         fprintf(nconf, "wep_key%d=", cred->key_idx);
294                         if (cred->key_len != 10 && cred->key_len != 26)
295                                 fputc('"', nconf);
296                         for (i = 0; i < cred->key_len; i++)
297                                 fputc(cred->key[i], nconf);
298                         if (cred->key_len != 10 && cred->key_len != 26)
299                                 fputc('"', nconf);
300                         fprintf(nconf, "\n");
301                 }
302         }
303
304         fprintf(nconf, "# WPS configuration - END\n");
305
306         multi_bss = 0;
307         while (fgets(buf, sizeof(buf), oconf)) {
308                 if (os_strncmp(buf, "bss=", 4) == 0)
309                         multi_bss = 1;
310                 if (!multi_bss &&
311                     (str_starts(buf, "ssid=") ||
312                      str_starts(buf, "auth_algs=") ||
313                      str_starts(buf, "wps_state=") ||
314                      str_starts(buf, "wpa=") ||
315                      str_starts(buf, "wpa_psk=") ||
316                      str_starts(buf, "wpa_pairwise=") ||
317                      str_starts(buf, "rsn_pairwise=") ||
318                      str_starts(buf, "wpa_key_mgmt=") ||
319                      str_starts(buf, "wpa_passphrase="))) {
320                         fprintf(nconf, "#WPS# %s", buf);
321                 } else
322                         fprintf(nconf, "%s", buf);
323         }
324
325         fclose(nconf);
326         fclose(oconf);
327
328         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
329                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
330                            "configuration file: %s", strerror(errno));
331                 os_free(tmp_fname);
332                 return -1;
333         }
334
335         os_free(tmp_fname);
336
337         /* Schedule configuration reload after short period of time to allow
338          * EAP-WSC to be finished.
339          */
340         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
341                                NULL);
342
343         /* TODO: dualband AP may need to update multiple configuration files */
344
345         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
346
347         return 0;
348 }
349
350
351 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
352 {
353         os_free(hapd->wps_beacon_ie);
354         hapd->wps_beacon_ie = NULL;
355         hapd->wps_beacon_ie_len = 0;
356         hostapd_set_wps_beacon_ie(hapd, NULL, 0);
357
358         os_free(hapd->wps_probe_resp_ie);
359         hapd->wps_probe_resp_ie = NULL;
360         hapd->wps_probe_resp_ie_len = 0;
361         hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
362 }
363
364
365 int hostapd_init_wps(struct hostapd_data *hapd,
366                      struct hostapd_bss_config *conf)
367 {
368         struct wps_context *wps;
369         struct wps_registrar_config cfg;
370
371         if (conf->wps_state == 0) {
372                 hostapd_wps_clear_ies(hapd);
373                 return 0;
374         }
375
376         wps = os_zalloc(sizeof(*wps));
377         if (wps == NULL)
378                 return -1;
379
380         wps->cred_cb = hostapd_wps_cred_cb;
381         wps->cb_ctx = hapd;
382
383         os_memset(&cfg, 0, sizeof(cfg));
384         wps->wps_state = hapd->conf->wps_state;
385         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
386         os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
387         wps->ssid_len = hapd->conf->ssid.ssid_len;
388         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
389         wps->ap = 1;
390         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
391         wps->dev.device_name = hapd->conf->device_name;
392         wps->dev.manufacturer = hapd->conf->manufacturer;
393         wps->dev.model_name = hapd->conf->model_name;
394         wps->dev.model_number = hapd->conf->model_number;
395         wps->dev.serial_number = hapd->conf->serial_number;
396         if (hapd->conf->config_methods) {
397                 char *m = hapd->conf->config_methods;
398                 if (os_strstr(m, "label"))
399                         wps->config_methods |= WPS_CONFIG_LABEL;
400                 if (os_strstr(m, "display"))
401                         wps->config_methods |= WPS_CONFIG_DISPLAY;
402                 if (os_strstr(m, "push_button"))
403                         wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
404                 if (os_strstr(m, "keypad"))
405                         wps->config_methods |= WPS_CONFIG_KEYPAD;
406         }
407         if (hapd->conf->device_type) {
408                 char *pos;
409                 u8 oui[4];
410                 /* <categ>-<OUI>-<subcateg> */
411                 wps->dev.categ = atoi(hapd->conf->device_type);
412                 pos = os_strchr(hapd->conf->device_type, '-');
413                 if (pos == NULL) {
414                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
415                         os_free(wps);
416                         return -1;
417                 }
418                 pos++;
419                 if (hexstr2bin(pos, oui, 4)) {
420                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
421                         os_free(wps);
422                         return -1;
423                 }
424                 wps->dev.oui = WPA_GET_BE32(oui);
425                 pos = os_strchr(pos, '-');
426                 if (pos == NULL) {
427                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
428                         os_free(wps);
429                         return -1;
430                 }
431                 pos++;
432                 wps->dev.sub_categ = atoi(pos);
433         }
434         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
435
436         if (conf->wpa & WPA_PROTO_RSN) {
437                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
438                         wps->auth_types |= WPS_AUTH_WPA2PSK;
439                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
440                         wps->auth_types |= WPS_AUTH_WPA2;
441
442                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
443                         wps->encr_types |= WPS_ENCR_AES;
444                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
445                         wps->encr_types |= WPS_ENCR_TKIP;
446         }
447
448         if (conf->wpa & WPA_PROTO_WPA) {
449                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
450                         wps->auth_types |= WPS_AUTH_WPAPSK;
451                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
452                         wps->auth_types |= WPS_AUTH_WPA;
453
454                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
455                         wps->encr_types |= WPS_ENCR_AES;
456                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
457                         wps->encr_types |= WPS_ENCR_TKIP;
458         }
459
460         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
461                 wps->encr_types |= WPS_ENCR_NONE;
462                 wps->auth_types |= WPS_AUTH_OPEN;
463         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
464                 wps->encr_types |= WPS_ENCR_WEP;
465                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
466                         wps->auth_types |= WPS_AUTH_OPEN;
467                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
468                         wps->auth_types |= WPS_AUTH_SHARED;
469         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
470                 wps->auth_types |= WPS_AUTH_OPEN;
471                 if (conf->default_wep_key_len)
472                         wps->encr_types |= WPS_ENCR_WEP;
473                 else
474                         wps->encr_types |= WPS_ENCR_NONE;
475         }
476
477         if (conf->ssid.wpa_psk_file) {
478                 /* Use per-device PSKs */
479         } else if (conf->ssid.wpa_passphrase) {
480                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
481                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
482         } else if (conf->ssid.wpa_psk) {
483                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
484                 if (wps->network_key == NULL) {
485                         os_free(wps);
486                         return -1;
487                 }
488                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
489                                  conf->ssid.wpa_psk->psk, PMK_LEN);
490                 wps->network_key_len = 2 * PMK_LEN;
491         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
492                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
493                 if (wps->network_key == NULL) {
494                         os_free(wps);
495                         return -1;
496                 }
497                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
498                           conf->ssid.wep.len[0]);
499                 wps->network_key_len = conf->ssid.wep.len[0];
500         }
501
502         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
503                 /* Override parameters to enable security by default */
504                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
505                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
506         }
507
508         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
509         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
510         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
511         cfg.cb_ctx = hapd;
512
513         wps->registrar = wps_registrar_init(wps, &cfg);
514         if (wps->registrar == NULL) {
515                 printf("Failed to initialize WPS Registrar\n");
516                 os_free(wps->network_key);
517                 os_free(wps);
518                 return -1;
519         }
520
521         hapd->wps = wps;
522
523         return 0;
524 }
525
526
527 void hostapd_deinit_wps(struct hostapd_data *hapd)
528 {
529         if (hapd->wps == NULL)
530                 return;
531         wps_registrar_deinit(hapd->wps->registrar);
532         os_free(hapd->wps->network_key);
533         os_free(hapd->wps);
534         hapd->wps = NULL;
535         hostapd_wps_clear_ies(hapd);
536 }
537
538
539 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
540                         const char *pin)
541 {
542         u8 u[UUID_LEN];
543         if (hapd->wps == NULL || uuid_str2bin(uuid, u))
544                 return -1;
545         return wps_registrar_add_pin(hapd->wps->registrar, u,
546                                      (const u8 *) pin, os_strlen(pin));
547 }
548
549
550 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
551 {
552         if (hapd->wps == NULL)
553                 return -1;
554         return wps_registrar_button_pushed(hapd->wps->registrar);
555 }
556
557
558 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
559                               const u8 *ie, size_t ie_len)
560 {
561         struct wpabuf *wps_ie;
562         const u8 *end, *pos, *wps;
563
564         if (hapd->wps == NULL)
565                 return;
566
567         pos = ie;
568         end = ie + ie_len;
569         wps = NULL;
570
571         while (pos + 1 < end) {
572                 if (pos + 2 + pos[1] > end)
573                         return;
574                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
575                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
576                         wps = pos;
577                         break;
578                 }
579                 pos += 2 + pos[1];
580         }
581
582         if (wps == NULL)
583                 return; /* No WPS IE in Probe Request */
584
585         wps_ie = wpabuf_alloc(ie_len);
586         if (wps_ie == NULL)
587                 return;
588
589         /* There may be multiple WPS IEs in the message, so need to concatenate
590          * their WPS Data fields */
591         while (pos + 1 < end) {
592                 if (pos + 2 + pos[1] > end)
593                         break;
594                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
595                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
596                         wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
597                 pos += 2 + pos[1];
598         }
599
600         if (wpabuf_len(wps_ie) > 0)
601                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
602
603         wpabuf_free(wps_ie);
604 }