Fixed WPS credential conversion to wpa_supplicant configuration
[wpasupplicant] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "ieee802_11_defs.h"
19 #include "wpa_common.h"
20 #include "config.h"
21 #include "eap_peer/eap.h"
22 #include "wpa_supplicant_i.h"
23 #include "eloop.h"
24 #include "uuid.h"
25 #include "wpa_ctrl.h"
26 #include "eap_common/eap_wsc_common.h"
27 #include "wps_supplicant.h"
28
29
30 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
31 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
32
33
34 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
35 {
36         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
37
38         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
39             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
40                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
41                            "try to associate with the received credential");
42                 wpa_supplicant_deauthenticate(wpa_s,
43                                               WLAN_REASON_DEAUTH_LEAVING);
44                 wpa_s->reassociate = 1;
45                 wpa_supplicant_req_scan(wpa_s, 0, 0);
46                 return 1;
47         }
48
49         return 0;
50 }
51
52
53 static int wpa_supplicant_wps_cred(void *ctx,
54                                    const struct wps_credential *cred)
55 {
56         struct wpa_supplicant *wpa_s = ctx;
57         struct wpa_ssid *ssid = wpa_s->current_ssid;
58
59         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
60
61         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
62                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
63                            "on the received credential");
64                 os_free(ssid->eap.identity);
65                 ssid->eap.identity = NULL;
66                 ssid->eap.identity_len = 0;
67                 os_free(ssid->eap.phase1);
68                 ssid->eap.phase1 = NULL;
69                 os_free(ssid->eap.eap_methods);
70                 ssid->eap.eap_methods = NULL;
71         } else {
72                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
73                            "received credential");
74                 ssid = wpa_config_add_network(wpa_s->conf);
75                 if (ssid == NULL)
76                         return -1;
77         }
78
79         wpa_config_set_network_defaults(ssid);
80
81         os_free(ssid->ssid);
82         ssid->ssid = os_malloc(cred->ssid_len);
83         if (ssid->ssid) {
84                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
85                 ssid->ssid_len = cred->ssid_len;
86         }
87
88         switch (cred->encr_type) {
89         case WPS_ENCR_NONE:
90                 break;
91         case WPS_ENCR_WEP:
92                 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
93                     cred->key_idx < NUM_WEP_KEYS) {
94                         os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
95                                   cred->key_len);
96                         ssid->wep_key_len[cred->key_idx] = cred->key_len;
97                         ssid->wep_tx_keyidx = cred->key_idx;
98                 }
99                 break;
100         case WPS_ENCR_TKIP:
101                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
102                 break;
103         case WPS_ENCR_AES:
104                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
105                 break;
106         }
107
108         switch (cred->auth_type) {
109         case WPS_AUTH_OPEN:
110                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
111                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
112                 ssid->proto = 0;
113                 break;
114         case WPS_AUTH_SHARED:
115                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
116                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
117                 ssid->proto = 0;
118                 break;
119         case WPS_AUTH_WPAPSK:
120                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
121                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
122                 ssid->proto = WPA_PROTO_WPA;
123                 break;
124         case WPS_AUTH_WPA:
125                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
126                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
127                 ssid->proto = WPA_PROTO_WPA;
128                 break;
129         case WPS_AUTH_WPA2:
130                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
131                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
132                 ssid->proto = WPA_PROTO_RSN;
133                 break;
134         case WPS_AUTH_WPA2PSK:
135                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
136                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
137                 ssid->proto = WPA_PROTO_RSN;
138                 break;
139         }
140
141         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
142                 if (cred->key_len == 2 * PMK_LEN) {
143                         if (hexstr2bin((const char *) cred->key, ssid->psk,
144                                        PMK_LEN)) {
145                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
146                                            "Key");
147                                 return -1;
148                         }
149                         ssid->psk_set = 1;
150                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
151                         os_free(ssid->passphrase);
152                         ssid->passphrase = os_malloc(cred->key_len + 1);
153                         if (ssid->passphrase == NULL)
154                                 return -1;
155                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
156                         ssid->passphrase[cred->key_len] = '\0';
157                         wpa_config_update_psk(ssid);
158                 } else {
159                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
160                                    "length %lu",
161                                    (unsigned long) cred->key_len);
162                         return -1;
163                 }
164         }
165
166 #ifndef CONFIG_NO_CONFIG_WRITE
167         if (wpa_s->conf->update_config &&
168             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
169                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
170                 return -1;
171         }
172 #endif /* CONFIG_NO_CONFIG_WRITE */
173
174         return 0;
175 }
176
177
178 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
179                                          struct wps_event_m2d *m2d)
180 {
181         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
182                 "dev_password_id=%d config_error=%d",
183                 m2d->dev_password_id, m2d->config_error);
184 }
185
186
187 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
188                                           struct wps_event_fail *fail)
189 {
190         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
191         wpas_clear_wps(wpa_s);
192 }
193
194
195 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
196 {
197         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
198 }
199
200
201 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
202                                      union wps_event_data *data)
203 {
204         struct wpa_supplicant *wpa_s = ctx;
205         switch (event) {
206         case WPS_EV_M2D:
207                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
208                 break;
209         case WPS_EV_FAIL:
210                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
211                 break;
212         case WPS_EV_SUCCESS:
213                 wpa_supplicant_wps_event_success(wpa_s);
214                 break;
215         }
216 }
217
218
219 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
220 {
221         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
222             eap_is_wps_pin_enrollee(&ssid->eap))
223                 return WPS_REQ_ENROLLEE;
224         else
225                 return WPS_REQ_REGISTRAR;
226 }
227
228
229 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
230 {
231         int id;
232         struct wpa_ssid *ssid;
233
234         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
235
236         /* Remove any existing WPS network from configuration */
237         ssid = wpa_s->conf->ssid;
238         while (ssid) {
239                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
240                         if (ssid == wpa_s->current_ssid)
241                                 wpa_s->current_ssid = NULL;
242                         id = ssid->id;
243                 } else
244                         id = -1;
245                 ssid = ssid->next;
246                 if (id >= 0)
247                         wpa_config_remove_network(wpa_s->conf, id);
248         }
249 }
250
251
252 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
253 {
254         struct wpa_supplicant *wpa_s = eloop_ctx;
255         wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
256         wpas_clear_wps(wpa_s);
257 }
258
259
260 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
261                                               int registrar, const u8 *bssid)
262 {
263         struct wpa_ssid *ssid;
264
265         ssid = wpa_config_add_network(wpa_s->conf);
266         if (ssid == NULL)
267                 return NULL;
268         wpa_config_set_network_defaults(ssid);
269         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
270             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
271             wpa_config_set(ssid, "identity", registrar ?
272                            "\"" WSC_ID_REGISTRAR "\"" :
273                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
274                 wpa_config_remove_network(wpa_s->conf, ssid->id);
275                 return NULL;
276         }
277
278         if (bssid) {
279                 size_t i;
280                 struct wpa_scan_res *res;
281
282                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
283                 ssid->bssid_set = 1;
284
285                 /* Try to get SSID from scan results */
286                 if (wpa_s->scan_res == NULL &&
287                     wpa_supplicant_get_scan_results(wpa_s) < 0)
288                         return ssid; /* Could not find any scan results */
289
290                 for (i = 0; i < wpa_s->scan_res->num; i++) {
291                         const u8 *ie;
292
293                         res = wpa_s->scan_res->res[i];
294                         if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
295                                 continue;
296
297                         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
298                         if (ie == NULL)
299                                 break;
300                         os_free(ssid->ssid);
301                         ssid->ssid = os_malloc(ie[1]);
302                         if (ssid->ssid == NULL)
303                                 break;
304                         os_memcpy(ssid->ssid, ie + 2, ie[1]);
305                         ssid->ssid_len = ie[1];
306                         break;
307                 }
308         }
309
310         return ssid;
311 }
312
313
314 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
315                              struct wpa_ssid *selected)
316 {
317         struct wpa_ssid *ssid;
318
319         /* Mark all other networks disabled and trigger reassociation */
320         ssid = wpa_s->conf->ssid;
321         while (ssid) {
322                 ssid->disabled = ssid != selected;
323                 ssid = ssid->next;
324         }
325         wpa_s->disconnected = 0;
326         wpa_s->reassociate = 1;
327         wpa_supplicant_req_scan(wpa_s, 0, 0);
328 }
329
330
331 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
332 {
333         struct wpa_ssid *ssid;
334         wpas_clear_wps(wpa_s);
335         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
336         if (ssid == NULL)
337                 return -1;
338         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
339         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
340                                wpa_s, NULL);
341         wpas_wps_reassoc(wpa_s, ssid);
342         return 0;
343 }
344
345
346 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
347                        const char *pin)
348 {
349         struct wpa_ssid *ssid;
350         char val[30];
351         unsigned int rpin = 0;
352
353         wpas_clear_wps(wpa_s);
354         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
355         if (ssid == NULL)
356                 return -1;
357         if (pin)
358                 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
359         else {
360                 rpin = wps_generate_pin();
361                 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
362         }
363         wpa_config_set(ssid, "phase1", val, 0);
364         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
365                                wpa_s, NULL);
366         wpas_wps_reassoc(wpa_s, ssid);
367         return rpin;
368 }
369
370
371 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
372                        const char *pin)
373 {
374         struct wpa_ssid *ssid;
375         char val[30];
376
377         if (!pin)
378                 return -1;
379         wpas_clear_wps(wpa_s);
380         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
381         if (ssid == NULL)
382                 return -1;
383         os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
384         wpa_config_set(ssid, "phase1", val, 0);
385         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
386                                wpa_s, NULL);
387         wpas_wps_reassoc(wpa_s, ssid);
388         return 0;
389 }
390
391
392 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
393                                size_t psk_len)
394 {
395         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
396                    "STA " MACSTR, MAC2STR(mac_addr));
397         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
398
399         /* TODO */
400
401         return 0;
402 }
403
404
405 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
406                                    const struct wps_device_data *dev)
407 {
408         char uuid[40], txt[400];
409         int len;
410         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
411                 return;
412         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
413         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
414                           " [%s|%s|%s|%s|%s|%d-%08X-%d]",
415                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
416                           dev->manufacturer, dev->model_name,
417                           dev->model_number, dev->serial_number,
418                           dev->categ, dev->oui, dev->sub_categ);
419         if (len > 0 && len < (int) sizeof(txt))
420                 wpa_printf(MSG_INFO, "%s", txt);
421 }
422
423
424 int wpas_wps_init(struct wpa_supplicant *wpa_s)
425 {
426         struct wps_context *wps;
427         struct wps_registrar_config rcfg;
428
429         wps = os_zalloc(sizeof(*wps));
430         if (wps == NULL)
431                 return -1;
432
433         wps->cred_cb = wpa_supplicant_wps_cred;
434         wps->event_cb = wpa_supplicant_wps_event;
435         wps->cb_ctx = wpa_s;
436
437         wps->dev.device_name = wpa_s->conf->device_name;
438         wps->dev.manufacturer = wpa_s->conf->manufacturer;
439         wps->dev.model_name = wpa_s->conf->model_name;
440         wps->dev.model_number = wpa_s->conf->model_number;
441         wps->dev.serial_number = wpa_s->conf->serial_number;
442         if (wpa_s->conf->device_type) {
443                 char *pos;
444                 u8 oui[4];
445                 /* <categ>-<OUI>-<subcateg> */
446                 wps->dev.categ = atoi(wpa_s->conf->device_type);
447                 pos = os_strchr(wpa_s->conf->device_type, '-');
448                 if (pos == NULL) {
449                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
450                         os_free(wps);
451                         return -1;
452                 }
453                 pos++;
454                 if (hexstr2bin(pos, oui, 4)) {
455                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
456                         os_free(wps);
457                         return -1;
458                 }
459                 wps->dev.oui = WPA_GET_BE32(oui);
460                 pos = os_strchr(pos, '-');
461                 if (pos == NULL) {
462                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
463                         os_free(wps);
464                         return -1;
465                 }
466                 pos++;
467                 wps->dev.sub_categ = atoi(pos);
468         }
469         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
470         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
471         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
472         if (is_nil_uuid(wpa_s->conf->uuid)) {
473                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
474                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
475                             wps->uuid, WPS_UUID_LEN);
476         } else
477                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
478
479         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
480         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
481
482         os_memset(&rcfg, 0, sizeof(rcfg));
483         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
484         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
485         rcfg.cb_ctx = wpa_s;
486
487         wps->registrar = wps_registrar_init(wps, &rcfg);
488         if (wps->registrar == NULL) {
489                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
490                 os_free(wps);
491                 return -1;
492         }
493
494         wpa_s->wps = wps;
495
496         return 0;
497 }
498
499
500 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
501 {
502         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
503
504         if (wpa_s->wps == NULL)
505                 return;
506
507         wps_registrar_deinit(wpa_s->wps->registrar);
508         os_free(wpa_s->wps->network_key);
509         os_free(wpa_s->wps);
510         wpa_s->wps = NULL;
511 }
512
513
514 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
515 {
516         struct wpabuf *wps_ie;
517
518         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
519                 return -1;
520
521         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
522         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
523                 if (!wps_ie) {
524                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
525                         return 0;
526                 }
527
528                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
529                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
530                                    "without active PBC Registrar");
531                         wpabuf_free(wps_ie);
532                         return 0;
533                 }
534
535                 /* TODO: overlap detection */
536                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
537                            "(Active PBC)");
538                 wpabuf_free(wps_ie);
539                 return 1;
540         }
541
542         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
543                 if (!wps_ie) {
544                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
545                         return 0;
546                 }
547
548                 if (!wps_is_selected_pin_registrar(wps_ie)) {
549                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
550                                    "without active PIN Registrar");
551                         wpabuf_free(wps_ie);
552                         return 0;
553                 }
554                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
555                            "(Active PIN)");
556                 wpabuf_free(wps_ie);
557                 return 1;
558         }
559
560         if (wps_ie) {
561                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
562                 wpabuf_free(wps_ie);
563                 return 1;
564         }
565
566         return -1;
567 }
568
569
570 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
571                               struct wpa_scan_res *bss)
572 {
573         struct wpabuf *wps_ie = NULL;
574         int ret = 0;
575
576         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
577                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
578                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
579                         /* allow wildcard SSID for WPS PBC */
580                         ret = 1;
581                 }
582         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
583                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
584                 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
585                         /* allow wildcard SSID for WPS PIN */
586                         ret = 1;
587                 }
588         }
589
590         if (!ret && ssid->bssid_set &&
591             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
592                 /* allow wildcard SSID due to hardcoded BSSID match */
593                 ret = 1;
594         }
595
596         wpabuf_free(wps_ie);
597
598         return ret;
599 }
600
601
602 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
603                               struct wpa_scan_res *selected,
604                               struct wpa_ssid *ssid)
605 {
606         const u8 *sel_uuid, *uuid;
607         size_t i;
608         struct wpabuf *wps_ie;
609         int ret = 0;
610
611         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
612                 return 0;
613
614         /* Make sure that only one AP is in active PBC mode */
615         wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
616         if (wps_ie)
617                 sel_uuid = wps_get_uuid_e(wps_ie);
618         else
619                 sel_uuid = NULL;
620
621         for (i = 0; i < wpa_s->scan_res->num; i++) {
622                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
623                 struct wpabuf *ie;
624                 if (bss == selected)
625                         continue;
626                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
627                 if (!ie)
628                         continue;
629                 if (!wps_is_selected_pbc_registrar(ie)) {
630                         wpabuf_free(ie);
631                         continue;
632                 }
633                 uuid = wps_get_uuid_e(ie);
634                 if (sel_uuid == NULL || uuid == NULL ||
635                     os_memcmp(sel_uuid, uuid, 16) != 0) {
636                         ret = 1; /* PBC overlap */
637                         wpabuf_free(ie);
638                         break;
639                 }
640
641                 /* TODO: verify that this is reasonable dual-band situation */
642
643                 wpabuf_free(ie);
644         }
645
646         wpabuf_free(wps_ie);
647
648         return ret;
649 }
650
651
652 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
653 {
654         size_t i;
655
656         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
657                 return;
658
659         for (i = 0; i < wpa_s->scan_res->num; i++) {
660                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
661                 struct wpabuf *ie;
662                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
663                 if (!ie)
664                         continue;
665                 if (wps_is_selected_pbc_registrar(ie))
666                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
667                 else if (wps_is_selected_pin_registrar(ie))
668                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
669                 else
670                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
671                 wpabuf_free(ie);
672                 break;
673         }
674 }
675
676
677 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
678 {
679         struct wpa_ssid *ssid;
680
681         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
682                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
683                         return 1;
684         }
685
686         return 0;
687 }