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