b9e72764af67c5cc1de5355ef0e16ec6e8f511f3
[libeap.git] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008-2010, 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 "eloop.h"
19 #include "uuid.h"
20 #include "crypto/dh_group5.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/ieee802_11_common.h"
23 #include "common/wpa_common.h"
24 #include "common/wpa_ctrl.h"
25 #include "eap_common/eap_wsc_common.h"
26 #include "eap_peer/eap.h"
27 #include "rsn_supp/wpa.h"
28 #include "config.h"
29 #include "wpa_supplicant_i.h"
30 #include "driver_i.h"
31 #include "notify.h"
32 #include "blacklist.h"
33 #include "bss.h"
34 #include "scan.h"
35 #include "ap.h"
36 #include "p2p/p2p.h"
37 #include "p2p_supplicant.h"
38 #include "wps_supplicant.h"
39
40
41 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
42 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
43 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
44
45 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
46 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
47
48
49 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
50 {
51         if (!wpa_s->wps_success &&
52             wpa_s->current_ssid &&
53             eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
54                 const u8 *bssid = wpa_s->bssid;
55                 if (is_zero_ether_addr(bssid))
56                         bssid = wpa_s->pending_bssid;
57
58                 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
59                            " did not succeed - continue trying to find "
60                            "suitable AP", MAC2STR(bssid));
61                 wpa_blacklist_add(wpa_s, bssid);
62
63                 wpa_supplicant_deauthenticate(wpa_s,
64                                               WLAN_REASON_DEAUTH_LEAVING);
65                 wpa_s->reassociate = 1;
66                 wpa_supplicant_req_scan(wpa_s,
67                                         wpa_s->blacklist_cleared ? 5 : 0, 0);
68                 wpa_s->blacklist_cleared = 0;
69                 return 1;
70         }
71
72         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
73
74         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
75             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
76                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
77                            "try to associate with the received credential");
78                 wpa_supplicant_deauthenticate(wpa_s,
79                                               WLAN_REASON_DEAUTH_LEAVING);
80                 wpa_s->after_wps = 5;
81                 wpa_s->wps_freq = wpa_s->assoc_freq;
82                 wpa_s->reassociate = 1;
83                 wpa_supplicant_req_scan(wpa_s, 0, 0);
84                 return 1;
85         }
86
87         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
88                 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
89                            "for external credential processing");
90                 wpas_clear_wps(wpa_s);
91                 wpa_supplicant_deauthenticate(wpa_s,
92                                               WLAN_REASON_DEAUTH_LEAVING);
93                 return 1;
94         }
95
96         return 0;
97 }
98
99
100 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
101                                          struct wpa_ssid *ssid,
102                                          const struct wps_credential *cred)
103 {
104         struct wpa_driver_capa capa;
105         struct wpa_bss *bss;
106         const u8 *ie;
107         struct wpa_ie_data adv;
108         int wpa2 = 0, ccmp = 0;
109
110         /*
111          * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
112          * case they are configured for mixed mode operation (WPA+WPA2 and
113          * TKIP+CCMP). Try to use scan results to figure out whether the AP
114          * actually supports stronger security and select that if the client
115          * has support for it, too.
116          */
117
118         if (wpa_drv_get_capa(wpa_s, &capa))
119                 return; /* Unknown what driver supports */
120
121         bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
122         if (bss == NULL) {
123                 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
124                            "table - use credential as-is");
125                 return;
126         }
127
128         wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
129
130         ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
131         if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
132                 wpa2 = 1;
133                 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
134                         ccmp = 1;
135         } else {
136                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
137                 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
138                     adv.pairwise_cipher & WPA_CIPHER_CCMP)
139                         ccmp = 1;
140         }
141
142         if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
143             (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
144                 /*
145                  * TODO: This could be the initial AP configuration and the
146                  * Beacon contents could change shortly. Should request a new
147                  * scan and delay addition of the network until the updated
148                  * scan results are available.
149                  */
150                 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
151                            "support - use credential as-is");
152                 return;
153         }
154
155         if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
156             (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
157             (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
158                 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
159                            "based on scan results");
160                 if (wpa_s->conf->ap_scan == 1)
161                         ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
162                 else
163                         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
164         }
165
166         if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
167             (ssid->proto & WPA_PROTO_WPA) &&
168             (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
169                 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
170                            "based on scan results");
171                 if (wpa_s->conf->ap_scan == 1)
172                         ssid->proto |= WPA_PROTO_RSN;
173                 else
174                         ssid->proto = WPA_PROTO_RSN;
175         }
176 }
177
178
179 static int wpa_supplicant_wps_cred(void *ctx,
180                                    const struct wps_credential *cred)
181 {
182         struct wpa_supplicant *wpa_s = ctx;
183         struct wpa_ssid *ssid = wpa_s->current_ssid;
184         u8 key_idx = 0;
185         u16 auth_type;
186
187         if ((wpa_s->conf->wps_cred_processing == 1 ||
188              wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
189                 size_t blen = cred->cred_attr_len * 2 + 1;
190                 char *buf = os_malloc(blen);
191                 if (buf) {
192                         wpa_snprintf_hex(buf, blen,
193                                          cred->cred_attr, cred->cred_attr_len);
194                         wpa_msg(wpa_s, MSG_INFO, "%s%s",
195                                 WPS_EVENT_CRED_RECEIVED, buf);
196                         os_free(buf);
197                 }
198
199                 wpas_notify_wps_credential(wpa_s, cred);
200         } else
201                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
202
203         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
204                         cred->cred_attr, cred->cred_attr_len);
205
206         if (wpa_s->conf->wps_cred_processing == 1)
207                 return 0;
208
209         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
210         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
211                    cred->auth_type);
212         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
213         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
214         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
215                         cred->key, cred->key_len);
216         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
217                    MAC2STR(cred->mac_addr));
218
219         auth_type = cred->auth_type;
220         if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
221                 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
222                            "auth_type into WPA2PSK");
223                 auth_type = WPS_AUTH_WPA2PSK;
224         }
225
226         if (auth_type != WPS_AUTH_OPEN &&
227             auth_type != WPS_AUTH_SHARED &&
228             auth_type != WPS_AUTH_WPAPSK &&
229             auth_type != WPS_AUTH_WPA2PSK) {
230                 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
231                            "unsupported authentication type 0x%x",
232                            auth_type);
233                 return 0;
234         }
235
236         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
237                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
238                            "on the received credential");
239                 os_free(ssid->eap.identity);
240                 ssid->eap.identity = NULL;
241                 ssid->eap.identity_len = 0;
242                 os_free(ssid->eap.phase1);
243                 ssid->eap.phase1 = NULL;
244                 os_free(ssid->eap.eap_methods);
245                 ssid->eap.eap_methods = NULL;
246                 if (!ssid->p2p_group)
247                         ssid->temporary = 0;
248         } else {
249                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
250                            "received credential");
251                 ssid = wpa_config_add_network(wpa_s->conf);
252                 if (ssid == NULL)
253                         return -1;
254                 wpas_notify_network_added(wpa_s, ssid);
255         }
256
257         wpa_config_set_network_defaults(ssid);
258
259         os_free(ssid->ssid);
260         ssid->ssid = os_malloc(cred->ssid_len);
261         if (ssid->ssid) {
262                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
263                 ssid->ssid_len = cred->ssid_len;
264         }
265
266         switch (cred->encr_type) {
267         case WPS_ENCR_NONE:
268                 break;
269         case WPS_ENCR_WEP:
270                 if (cred->key_len <= 0)
271                         break;
272                 if (cred->key_len != 5 && cred->key_len != 13 &&
273                     cred->key_len != 10 && cred->key_len != 26) {
274                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
275                                    "%lu", (unsigned long) cred->key_len);
276                         return -1;
277                 }
278                 if (cred->key_idx > NUM_WEP_KEYS) {
279                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
280                                    cred->key_idx);
281                         return -1;
282                 }
283                 if (cred->key_idx)
284                         key_idx = cred->key_idx - 1;
285                 if (cred->key_len == 10 || cred->key_len == 26) {
286                         if (hexstr2bin((char *) cred->key,
287                                        ssid->wep_key[key_idx],
288                                        cred->key_len / 2) < 0) {
289                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
290                                            "%d", key_idx);
291                                 return -1;
292                         }
293                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
294                 } else {
295                         os_memcpy(ssid->wep_key[key_idx], cred->key,
296                                   cred->key_len);
297                         ssid->wep_key_len[key_idx] = cred->key_len;
298                 }
299                 ssid->wep_tx_keyidx = key_idx;
300                 break;
301         case WPS_ENCR_TKIP:
302                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
303                 break;
304         case WPS_ENCR_AES:
305                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
306                 break;
307         }
308
309         switch (auth_type) {
310         case WPS_AUTH_OPEN:
311                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
312                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
313                 ssid->proto = 0;
314                 break;
315         case WPS_AUTH_SHARED:
316                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
317                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
318                 ssid->proto = 0;
319                 break;
320         case WPS_AUTH_WPAPSK:
321                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
322                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
323                 ssid->proto = WPA_PROTO_WPA;
324                 break;
325         case WPS_AUTH_WPA:
326                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
327                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
328                 ssid->proto = WPA_PROTO_WPA;
329                 break;
330         case WPS_AUTH_WPA2:
331                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
332                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
333                 ssid->proto = WPA_PROTO_RSN;
334                 break;
335         case WPS_AUTH_WPA2PSK:
336                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
337                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
338                 ssid->proto = WPA_PROTO_RSN;
339                 break;
340         }
341
342         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
343                 if (cred->key_len == 2 * PMK_LEN) {
344                         if (hexstr2bin((const char *) cred->key, ssid->psk,
345                                        PMK_LEN)) {
346                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
347                                            "Key");
348                                 return -1;
349                         }
350                         ssid->psk_set = 1;
351                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
352                         os_free(ssid->passphrase);
353                         ssid->passphrase = os_malloc(cred->key_len + 1);
354                         if (ssid->passphrase == NULL)
355                                 return -1;
356                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
357                         ssid->passphrase[cred->key_len] = '\0';
358                         wpa_config_update_psk(ssid);
359                 } else {
360                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
361                                    "length %lu",
362                                    (unsigned long) cred->key_len);
363                         return -1;
364                 }
365         }
366
367         wpas_wps_security_workaround(wpa_s, ssid, cred);
368
369 #ifndef CONFIG_NO_CONFIG_WRITE
370         if (wpa_s->conf->update_config &&
371             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
372                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
373                 return -1;
374         }
375 #endif /* CONFIG_NO_CONFIG_WRITE */
376
377         return 0;
378 }
379
380
381 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
382                                          struct wps_event_m2d *m2d)
383 {
384         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
385                 "dev_password_id=%d config_error=%d",
386                 m2d->dev_password_id, m2d->config_error);
387         wpas_notify_wps_event_m2d(wpa_s, m2d);
388 }
389
390
391 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
392                                           struct wps_event_fail *fail)
393 {
394         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d config_error=%d",
395                 fail->msg, fail->config_error);
396         wpas_clear_wps(wpa_s);
397         wpas_notify_wps_event_fail(wpa_s, fail);
398 }
399
400
401 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
402 {
403         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
404         wpa_s->wps_success = 1;
405         wpas_notify_wps_event_success(wpa_s);
406 #ifdef CONFIG_P2P
407         wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
408 #endif /* CONFIG_P2P */
409 }
410
411
412 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
413                                                struct wps_event_er_ap *ap)
414 {
415         char uuid_str[100];
416         char dev_type[WPS_DEV_TYPE_BUFSIZE];
417
418         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
419         if (ap->pri_dev_type)
420                 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
421                                      sizeof(dev_type));
422         else
423                 dev_type[0] = '\0';
424
425         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
426                 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
427                 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
428                 ap->friendly_name ? ap->friendly_name : "",
429                 ap->manufacturer ? ap->manufacturer : "",
430                 ap->model_description ? ap->model_description : "",
431                 ap->model_name ? ap->model_name : "",
432                 ap->manufacturer_url ? ap->manufacturer_url : "",
433                 ap->model_url ? ap->model_url : "");
434 }
435
436
437 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
438                                                   struct wps_event_er_ap *ap)
439 {
440         char uuid_str[100];
441         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
442         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
443 }
444
445
446 static void wpa_supplicant_wps_event_er_enrollee_add(
447         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
448 {
449         char uuid_str[100];
450         char dev_type[WPS_DEV_TYPE_BUFSIZE];
451
452         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
453         if (enrollee->pri_dev_type)
454                 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
455                                      sizeof(dev_type));
456         else
457                 dev_type[0] = '\0';
458
459         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
460                 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
461                 "|%s|%s|%s|%s|%s|",
462                 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
463                 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
464                 enrollee->dev_name ? enrollee->dev_name : "",
465                 enrollee->manufacturer ? enrollee->manufacturer : "",
466                 enrollee->model_name ? enrollee->model_name : "",
467                 enrollee->model_number ? enrollee->model_number : "",
468                 enrollee->serial_number ? enrollee->serial_number : "");
469 }
470
471
472 static void wpa_supplicant_wps_event_er_enrollee_remove(
473         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
474 {
475         char uuid_str[100];
476         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
477         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
478                 uuid_str, MAC2STR(enrollee->mac_addr));
479 }
480
481
482 static void wpa_supplicant_wps_event_er_ap_settings(
483         struct wpa_supplicant *wpa_s,
484         struct wps_event_er_ap_settings *ap_settings)
485 {
486         char uuid_str[100];
487         char key_str[65];
488         const struct wps_credential *cred = ap_settings->cred;
489
490         key_str[0] = '\0';
491         if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
492                 if (cred->key_len >= 8 && cred->key_len <= 64) {
493                         os_memcpy(key_str, cred->key, cred->key_len);
494                         key_str[cred->key_len] = '\0';
495                 }
496         }
497
498         uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
499         /* Use wpa_msg_ctrl to avoid showing the key in debug log */
500         wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
501                      "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
502                      "key=%s",
503                      uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
504                      cred->auth_type, cred->encr_type, key_str);
505 }
506
507
508 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
509                                      union wps_event_data *data)
510 {
511         struct wpa_supplicant *wpa_s = ctx;
512         switch (event) {
513         case WPS_EV_M2D:
514                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
515                 break;
516         case WPS_EV_FAIL:
517                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
518                 break;
519         case WPS_EV_SUCCESS:
520                 wpa_supplicant_wps_event_success(wpa_s);
521                 break;
522         case WPS_EV_PWD_AUTH_FAIL:
523                 break;
524         case WPS_EV_PBC_OVERLAP:
525                 break;
526         case WPS_EV_PBC_TIMEOUT:
527                 break;
528         case WPS_EV_ER_AP_ADD:
529                 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
530                 break;
531         case WPS_EV_ER_AP_REMOVE:
532                 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
533                 break;
534         case WPS_EV_ER_ENROLLEE_ADD:
535                 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
536                                                          &data->enrollee);
537                 break;
538         case WPS_EV_ER_ENROLLEE_REMOVE:
539                 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
540                                                             &data->enrollee);
541                 break;
542         case WPS_EV_ER_AP_SETTINGS:
543                 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
544                                                         &data->ap_settings);
545                 break;
546         }
547 }
548
549
550 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
551 {
552         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
553             eap_is_wps_pin_enrollee(&ssid->eap))
554                 return WPS_REQ_ENROLLEE;
555         else
556                 return WPS_REQ_REGISTRAR;
557 }
558
559
560 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
561 {
562         int id;
563         struct wpa_ssid *ssid, *remove_ssid = NULL;
564
565         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
566
567         /* Remove any existing WPS network from configuration */
568         ssid = wpa_s->conf->ssid;
569         while (ssid) {
570                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
571                         if (ssid == wpa_s->current_ssid) {
572                                 wpa_s->current_ssid = NULL;
573                                 if (ssid != NULL)
574                                         wpas_notify_network_changed(wpa_s);
575                         }
576                         id = ssid->id;
577                         remove_ssid = ssid;
578                 } else
579                         id = -1;
580                 ssid = ssid->next;
581                 if (id >= 0) {
582                         wpas_notify_network_removed(wpa_s, remove_ssid);
583                         wpa_config_remove_network(wpa_s->conf, id);
584                 }
585         }
586 }
587
588
589 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
590 {
591         struct wpa_supplicant *wpa_s = eloop_ctx;
592         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
593                 "out");
594         wpas_clear_wps(wpa_s);
595 }
596
597
598 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
599                                               int registrar, const u8 *bssid)
600 {
601         struct wpa_ssid *ssid;
602
603         ssid = wpa_config_add_network(wpa_s->conf);
604         if (ssid == NULL)
605                 return NULL;
606         wpas_notify_network_added(wpa_s, ssid);
607         wpa_config_set_network_defaults(ssid);
608         ssid->temporary = 1;
609         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
610             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
611             wpa_config_set(ssid, "identity", registrar ?
612                            "\"" WSC_ID_REGISTRAR "\"" :
613                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
614                 wpas_notify_network_removed(wpa_s, ssid);
615                 wpa_config_remove_network(wpa_s->conf, ssid->id);
616                 return NULL;
617         }
618
619         if (bssid) {
620 #ifndef CONFIG_P2P
621                 struct wpa_bss *bss;
622                 int count = 0;
623 #endif /* CONFIG_P2P */
624
625                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
626                 ssid->bssid_set = 1;
627
628                 /*
629                  * Note: With P2P, the SSID may change at the time the WPS
630                  * provisioning is started, so better not filter the AP based
631                  * on the current SSID in the scan results.
632                  */
633 #ifndef CONFIG_P2P
634                 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
635                         if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
636                                 continue;
637
638                         os_free(ssid->ssid);
639                         ssid->ssid = os_malloc(bss->ssid_len);
640                         if (ssid->ssid == NULL)
641                                 break;
642                         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
643                         ssid->ssid_len = bss->ssid_len;
644                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
645                                           "scan results",
646                                           ssid->ssid, ssid->ssid_len);
647                         count++;
648                 }
649
650                 if (count > 1) {
651                         wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
652                                    "for the AP; use wildcard");
653                         os_free(ssid->ssid);
654                         ssid->ssid = NULL;
655                         ssid->ssid_len = 0;
656                 }
657 #endif /* CONFIG_P2P */
658         }
659
660         return ssid;
661 }
662
663
664 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
665                              struct wpa_ssid *selected)
666 {
667         struct wpa_ssid *ssid;
668
669         /* Mark all other networks disabled and trigger reassociation */
670         ssid = wpa_s->conf->ssid;
671         while (ssid) {
672                 int was_disabled = ssid->disabled;
673                 ssid->disabled = ssid != selected;
674                 if (was_disabled != ssid->disabled)
675                         wpas_notify_network_enabled_changed(wpa_s, ssid);
676                 ssid = ssid->next;
677         }
678         wpa_s->disconnected = 0;
679         wpa_s->reassociate = 1;
680         wpa_s->scan_runs = 0;
681         wpa_s->wps_success = 0;
682         wpa_s->blacklist_cleared = 0;
683         wpa_supplicant_req_scan(wpa_s, 0, 0);
684 }
685
686
687 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
688                        int p2p_group)
689 {
690         struct wpa_ssid *ssid;
691         wpas_clear_wps(wpa_s);
692         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
693         if (ssid == NULL)
694                 return -1;
695         ssid->temporary = 1;
696         ssid->p2p_group = p2p_group;
697 #ifdef CONFIG_P2P
698         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
699                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
700                 if (ssid->ssid) {
701                         ssid->ssid_len = wpa_s->go_params->ssid_len;
702                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
703                                   ssid->ssid_len);
704                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
705                                           "SSID", ssid->ssid, ssid->ssid_len);
706                 }
707         }
708 #endif /* CONFIG_P2P */
709         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
710         if (wpa_s->wps_fragment_size)
711                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
712         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
713                                wpa_s, NULL);
714         wpas_wps_reassoc(wpa_s, ssid);
715         return 0;
716 }
717
718
719 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
720                        const char *pin, int p2p_group, u16 dev_pw_id)
721 {
722         struct wpa_ssid *ssid;
723         char val[128];
724         unsigned int rpin = 0;
725
726         wpas_clear_wps(wpa_s);
727         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
728         if (ssid == NULL)
729                 return -1;
730         ssid->temporary = 1;
731         ssid->p2p_group = p2p_group;
732 #ifdef CONFIG_P2P
733         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
734                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
735                 if (ssid->ssid) {
736                         ssid->ssid_len = wpa_s->go_params->ssid_len;
737                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
738                                   ssid->ssid_len);
739                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
740                                           "SSID", ssid->ssid, ssid->ssid_len);
741                 }
742         }
743 #endif /* CONFIG_P2P */
744         if (pin)
745                 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
746                             pin, dev_pw_id);
747         else {
748                 rpin = wps_generate_pin();
749                 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
750                             rpin, dev_pw_id);
751         }
752         wpa_config_set(ssid, "phase1", val, 0);
753         if (wpa_s->wps_fragment_size)
754                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
755         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
756                                wpa_s, NULL);
757         wpas_wps_reassoc(wpa_s, ssid);
758         return rpin;
759 }
760
761
762 /* Cancel the wps pbc/pin requests */
763 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
764 {
765 #ifdef CONFIG_AP
766         if (wpa_s->ap_iface) {
767                 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
768                 return wpa_supplicant_ap_wps_cancel(wpa_s);
769         }
770 #endif /* CONFIG_AP */
771
772         if (wpa_s->wpa_state == WPA_SCANNING) {
773                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
774                 wpa_supplicant_cancel_scan(wpa_s);
775                 wpas_clear_wps(wpa_s);
776         } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
777                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
778                            "deauthenticate");
779                 wpa_supplicant_deauthenticate(wpa_s,
780                                               WLAN_REASON_DEAUTH_LEAVING);
781                 wpas_clear_wps(wpa_s);
782         }
783
784         return 0;
785 }
786
787
788 #ifdef CONFIG_WPS_OOB
789 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
790                        char *path, char *method, char *name)
791 {
792         struct wps_context *wps = wpa_s->wps;
793         struct oob_device_data *oob_dev;
794
795         oob_dev = wps_get_oob_device(device_type);
796         if (oob_dev == NULL)
797                 return -1;
798         oob_dev->device_path = path;
799         oob_dev->device_name = name;
800         wps->oob_conf.oob_method = wps_get_oob_method(method);
801
802         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
803                 /*
804                  * Use pre-configured DH keys in order to be able to write the
805                  * key hash into the OOB file.
806                  */
807                 wpabuf_free(wps->dh_pubkey);
808                 wpabuf_free(wps->dh_privkey);
809                 wps->dh_privkey = NULL;
810                 wps->dh_pubkey = NULL;
811                 dh5_free(wps->dh_ctx);
812                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
813                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
814                 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
815                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
816                                    "Diffie-Hellman handshake");
817                         return -1;
818                 }
819         }
820
821         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
822                 wpas_clear_wps(wpa_s);
823
824         if (wps_process_oob(wps, oob_dev, 0) < 0)
825                 return -1;
826
827         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
828              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
829             wpas_wps_start_pin(wpa_s, NULL,
830                                wpabuf_head(wps->oob_conf.dev_password), 0,
831                                DEV_PW_DEFAULT) < 0)
832                         return -1;
833
834         return 0;
835 }
836 #endif /* CONFIG_WPS_OOB */
837
838
839 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
840                        const char *pin, struct wps_new_ap_settings *settings)
841 {
842         struct wpa_ssid *ssid;
843         char val[200];
844         char *pos, *end;
845         int res;
846
847         if (!pin)
848                 return -1;
849         wpas_clear_wps(wpa_s);
850         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
851         if (ssid == NULL)
852                 return -1;
853         ssid->temporary = 1;
854         pos = val;
855         end = pos + sizeof(val);
856         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
857         if (res < 0 || res >= end - pos)
858                 return -1;
859         pos += res;
860         if (settings) {
861                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
862                                   "new_encr=%s new_key=%s",
863                                   settings->ssid_hex, settings->auth,
864                                   settings->encr, settings->key_hex);
865                 if (res < 0 || res >= end - pos)
866                         return -1;
867                 pos += res;
868         }
869         res = os_snprintf(pos, end - pos, "\"");
870         if (res < 0 || res >= end - pos)
871                 return -1;
872         wpa_config_set(ssid, "phase1", val, 0);
873         if (wpa_s->wps_fragment_size)
874                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
875         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
876                                wpa_s, NULL);
877         wpas_wps_reassoc(wpa_s, ssid);
878         return 0;
879 }
880
881
882 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
883                                size_t psk_len)
884 {
885         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
886                    "STA " MACSTR, MAC2STR(mac_addr));
887         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
888
889         /* TODO */
890
891         return 0;
892 }
893
894
895 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
896                                    const struct wps_device_data *dev)
897 {
898         char uuid[40], txt[400];
899         int len;
900         char devtype[WPS_DEV_TYPE_BUFSIZE];
901         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
902                 return;
903         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
904         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
905                           " [%s|%s|%s|%s|%s|%s]",
906                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
907                           dev->manufacturer, dev->model_name,
908                           dev->model_number, dev->serial_number,
909                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
910                                                sizeof(devtype)));
911         if (len > 0 && len < (int) sizeof(txt))
912                 wpa_printf(MSG_INFO, "%s", txt);
913 }
914
915
916 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
917                                     u16 sel_reg_config_methods)
918 {
919 #ifdef CONFIG_WPS_ER
920         struct wpa_supplicant *wpa_s = ctx;
921
922         if (wpa_s->wps_er == NULL)
923                 return;
924         wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
925                    "dev_password_id=%u sel_reg_config_methods=0x%x",
926                    sel_reg, dev_passwd_id, sel_reg_config_methods);
927         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
928                            sel_reg_config_methods);
929 #endif /* CONFIG_WPS_ER */
930 }
931
932
933 int wpas_wps_init(struct wpa_supplicant *wpa_s)
934 {
935         struct wps_context *wps;
936         struct wps_registrar_config rcfg;
937
938         wps = os_zalloc(sizeof(*wps));
939         if (wps == NULL)
940                 return -1;
941
942         wps->cred_cb = wpa_supplicant_wps_cred;
943         wps->event_cb = wpa_supplicant_wps_event;
944         wps->cb_ctx = wpa_s;
945
946         wps->dev.device_name = wpa_s->conf->device_name;
947         wps->dev.manufacturer = wpa_s->conf->manufacturer;
948         wps->dev.model_name = wpa_s->conf->model_name;
949         wps->dev.model_number = wpa_s->conf->model_number;
950         wps->dev.serial_number = wpa_s->conf->serial_number;
951         wps->config_methods =
952                 wps_config_methods_str2bin(wpa_s->conf->config_methods);
953         if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
954             (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
955                 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
956                            "methods are not allowed at the same time");
957                 os_free(wps);
958                 return -1;
959         }
960         if (wpa_s->conf->device_type &&
961             wps_dev_type_str2bin(wpa_s->conf->device_type,
962                                  wps->dev.pri_dev_type) < 0) {
963                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
964                 os_free(wps);
965                 return -1;
966         }
967         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
968         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
969         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
970         if (is_nil_uuid(wpa_s->conf->uuid)) {
971                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
972                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
973                             wps->uuid, WPS_UUID_LEN);
974         } else
975                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
976
977         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
978         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
979
980         os_memset(&rcfg, 0, sizeof(rcfg));
981         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
982         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
983         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
984         rcfg.cb_ctx = wpa_s;
985
986         wps->registrar = wps_registrar_init(wps, &rcfg);
987         if (wps->registrar == NULL) {
988                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
989                 os_free(wps);
990                 return -1;
991         }
992
993         wpa_s->wps = wps;
994
995         return 0;
996 }
997
998
999 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1000 {
1001         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1002
1003         if (wpa_s->wps == NULL)
1004                 return;
1005
1006 #ifdef CONFIG_WPS_ER
1007         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1008         wpa_s->wps_er = NULL;
1009 #endif /* CONFIG_WPS_ER */
1010
1011         wps_registrar_deinit(wpa_s->wps->registrar);
1012         wpabuf_free(wpa_s->wps->dh_pubkey);
1013         wpabuf_free(wpa_s->wps->dh_privkey);
1014         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1015         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1016         os_free(wpa_s->wps->network_key);
1017         os_free(wpa_s->wps);
1018         wpa_s->wps = NULL;
1019 }
1020
1021
1022 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1023                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1024 {
1025         struct wpabuf *wps_ie;
1026
1027         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1028                 return -1;
1029
1030         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1031         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1032                 if (!wps_ie) {
1033                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1034                         return 0;
1035                 }
1036
1037                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1038                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1039                                    "without active PBC Registrar");
1040                         wpabuf_free(wps_ie);
1041                         return 0;
1042                 }
1043
1044                 /* TODO: overlap detection */
1045                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1046                            "(Active PBC)");
1047                 wpabuf_free(wps_ie);
1048                 return 1;
1049         }
1050
1051         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1052                 if (!wps_ie) {
1053                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1054                         return 0;
1055                 }
1056
1057                 /*
1058                  * Start with WPS APs that advertise our address as an
1059                  * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1060                  * allow any WPS AP after couple of scans since some APs do not
1061                  * set Selected Registrar attribute properly when using
1062                  * external Registrar.
1063                  */
1064                 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1065                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1066                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1067                                            "without active PIN Registrar");
1068                                 wpabuf_free(wps_ie);
1069                                 return 0;
1070                         }
1071                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1072                 } else {
1073                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1074                                    "(Authorized MAC or Active PIN)");
1075                 }
1076                 wpabuf_free(wps_ie);
1077                 return 1;
1078         }
1079
1080         if (wps_ie) {
1081                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1082                 wpabuf_free(wps_ie);
1083                 return 1;
1084         }
1085
1086         return -1;
1087 }
1088
1089
1090 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1091                               struct wpa_ssid *ssid,
1092                               struct wpa_scan_res *bss)
1093 {
1094         struct wpabuf *wps_ie = NULL;
1095         int ret = 0;
1096
1097         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1098                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1099                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1100                         /* allow wildcard SSID for WPS PBC */
1101                         ret = 1;
1102                 }
1103         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1104                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1105                 if (wps_ie &&
1106                     (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1107                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1108                         /* allow wildcard SSID for WPS PIN */
1109                         ret = 1;
1110                 }
1111         }
1112
1113         if (!ret && ssid->bssid_set &&
1114             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1115                 /* allow wildcard SSID due to hardcoded BSSID match */
1116                 ret = 1;
1117         }
1118
1119 #ifdef CONFIG_WPS_STRICT
1120         if (wps_ie) {
1121                 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1122                                                    0, bss->bssid) < 0)
1123                         ret = 0;
1124                 if (bss->beacon_ie_len) {
1125                         struct wpabuf *bcn_wps;
1126                         bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1127                                 bss, WPS_IE_VENDOR_TYPE);
1128                         if (bcn_wps == NULL) {
1129                                 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1130                                            "missing from AP Beacon");
1131                                 ret = 0;
1132                         } else {
1133                                 if (wps_validate_beacon(wps_ie) < 0)
1134                                         ret = 0;
1135                                 wpabuf_free(bcn_wps);
1136                         }
1137                 }
1138         }
1139 #endif /* CONFIG_WPS_STRICT */
1140
1141         wpabuf_free(wps_ie);
1142
1143         return ret;
1144 }
1145
1146
1147 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1148                               struct wpa_bss *selected, struct wpa_ssid *ssid)
1149 {
1150         const u8 *sel_uuid, *uuid;
1151         struct wpabuf *wps_ie;
1152         int ret = 0;
1153         struct wpa_bss *bss;
1154
1155         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1156                 return 0;
1157
1158         wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1159                    "present in scan results; selected BSSID " MACSTR,
1160                    MAC2STR(selected->bssid));
1161
1162         /* Make sure that only one AP is in active PBC mode */
1163         wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1164         if (wps_ie) {
1165                 sel_uuid = wps_get_uuid_e(wps_ie);
1166                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1167                             sel_uuid, UUID_LEN);
1168         } else {
1169                 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1170                            "WPS IE?!");
1171                 sel_uuid = NULL;
1172         }
1173
1174         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1175                 struct wpabuf *ie;
1176                 if (bss == selected)
1177                         continue;
1178                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1179                 if (!ie)
1180                         continue;
1181                 if (!wps_is_selected_pbc_registrar(ie)) {
1182                         wpabuf_free(ie);
1183                         continue;
1184                 }
1185                 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1186                            MACSTR, MAC2STR(bss->bssid));
1187                 uuid = wps_get_uuid_e(ie);
1188                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1189                             uuid, UUID_LEN);
1190                 if (sel_uuid == NULL || uuid == NULL ||
1191                     os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1192                         ret = 1; /* PBC overlap */
1193                         wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1194                                 MACSTR " and " MACSTR,
1195                                 MAC2STR(selected->bssid),
1196                                 MAC2STR(bss->bssid));
1197                         wpabuf_free(ie);
1198                         break;
1199                 }
1200
1201                 /* TODO: verify that this is reasonable dual-band situation */
1202
1203                 wpabuf_free(ie);
1204         }
1205
1206         wpabuf_free(wps_ie);
1207
1208         return ret;
1209 }
1210
1211
1212 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1213 {
1214         struct wpa_bss *bss;
1215
1216         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1217                 return;
1218
1219         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1220                 struct wpabuf *ie;
1221                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1222                 if (!ie)
1223                         continue;
1224                 if (wps_is_selected_pbc_registrar(ie))
1225                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1226                                      WPS_EVENT_AP_AVAILABLE_PBC);
1227                 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1228                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1229                                      WPS_EVENT_AP_AVAILABLE_AUTH);
1230                 else if (wps_is_selected_pin_registrar(ie))
1231                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1232                                      WPS_EVENT_AP_AVAILABLE_PIN);
1233                 else
1234                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1235                                      WPS_EVENT_AP_AVAILABLE);
1236                 wpabuf_free(ie);
1237                 break;
1238         }
1239 }
1240
1241
1242 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1243 {
1244         struct wpa_ssid *ssid;
1245
1246         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1247                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1248                         return 1;
1249         }
1250
1251         return 0;
1252 }
1253
1254
1255 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1256                               char *end)
1257 {
1258         struct wpabuf *wps_ie;
1259         int ret;
1260
1261         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1262         if (wps_ie == NULL)
1263                 return 0;
1264
1265         ret = wps_attr_text(wps_ie, buf, end);
1266         wpabuf_free(wps_ie);
1267         return ret;
1268 }
1269
1270
1271 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1272 {
1273 #ifdef CONFIG_WPS_ER
1274         if (wpa_s->wps_er) {
1275                 wps_er_refresh(wpa_s->wps_er);
1276                 return 0;
1277         }
1278         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1279         if (wpa_s->wps_er == NULL)
1280                 return -1;
1281         return 0;
1282 #else /* CONFIG_WPS_ER */
1283         return 0;
1284 #endif /* CONFIG_WPS_ER */
1285 }
1286
1287
1288 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1289 {
1290 #ifdef CONFIG_WPS_ER
1291         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1292         wpa_s->wps_er = NULL;
1293 #endif /* CONFIG_WPS_ER */
1294         return 0;
1295 }
1296
1297
1298 #ifdef CONFIG_WPS_ER
1299 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1300                         const char *uuid, const char *pin)
1301 {
1302         u8 u[UUID_LEN];
1303         int any = 0;
1304
1305         if (os_strcmp(uuid, "any") == 0)
1306                 any = 1;
1307         else if (uuid_str2bin(uuid, u))
1308                 return -1;
1309         return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1310                                      any ? NULL : u,
1311                                      (const u8 *) pin, os_strlen(pin), 300);
1312 }
1313
1314
1315 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1316 {
1317         u8 u[UUID_LEN];
1318
1319         if (uuid_str2bin(uuid, u))
1320                 return -1;
1321         return wps_er_pbc(wpa_s->wps_er, u);
1322 }
1323
1324
1325 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1326                       const char *pin)
1327 {
1328         u8 u[UUID_LEN];
1329
1330         if (uuid_str2bin(uuid, u))
1331                 return -1;
1332         return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1333                             os_strlen(pin));
1334 }
1335
1336
1337 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1338                        const char *pin, struct wps_new_ap_settings *settings)
1339 {
1340         u8 u[UUID_LEN];
1341         struct wps_credential cred;
1342         size_t len;
1343
1344         if (uuid_str2bin(uuid, u))
1345                 return -1;
1346         if (settings->ssid_hex == NULL || settings->auth == NULL ||
1347             settings->encr == NULL || settings->key_hex == NULL)
1348                 return -1;
1349
1350         os_memset(&cred, 0, sizeof(cred));
1351         len = os_strlen(settings->ssid_hex);
1352         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1353             hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1354                 return -1;
1355         cred.ssid_len = len / 2;
1356
1357         len = os_strlen(settings->key_hex);
1358         if ((len & 1) || len > 2 * sizeof(cred.key) ||
1359             hexstr2bin(settings->key_hex, cred.key, len / 2))
1360                 return -1;
1361         cred.key_len = len / 2;
1362
1363         if (os_strcmp(settings->auth, "OPEN") == 0)
1364                 cred.auth_type = WPS_AUTH_OPEN;
1365         else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1366                 cred.auth_type = WPS_AUTH_WPAPSK;
1367         else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1368                 cred.auth_type = WPS_AUTH_WPA2PSK;
1369         else
1370                 return -1;
1371
1372         if (os_strcmp(settings->encr, "NONE") == 0)
1373                 cred.encr_type = WPS_ENCR_NONE;
1374         else if (os_strcmp(settings->encr, "WEP") == 0)
1375                 cred.encr_type = WPS_ENCR_WEP;
1376         else if (os_strcmp(settings->encr, "TKIP") == 0)
1377                 cred.encr_type = WPS_ENCR_TKIP;
1378         else if (os_strcmp(settings->encr, "CCMP") == 0)
1379                 cred.encr_type = WPS_ENCR_AES;
1380         else
1381                 return -1;
1382
1383         return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1384                              os_strlen(pin), &cred);
1385 }
1386
1387
1388 static void wpas_wps_terminate_cb(void *ctx)
1389 {
1390         wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1391         eloop_terminate();
1392 }
1393 #endif /* CONFIG_WPS_ER */
1394
1395
1396 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1397 {
1398 #ifdef CONFIG_WPS_ER
1399         if (wpa_s->wps_er) {
1400                 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1401                 wpa_s->wps_er = NULL;
1402                 return 1;
1403         }
1404 #endif /* CONFIG_WPS_ER */
1405         return 0;
1406 }
1407
1408
1409 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1410 {
1411         struct wpa_ssid *ssid;
1412
1413         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1414                 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1415                         return 1;
1416         }
1417
1418         return 0;
1419 }
1420
1421
1422 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1423 {
1424         struct wps_context *wps = wpa_s->wps;
1425
1426         if (wps == NULL)
1427                 return;
1428
1429         if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1430                 wps->config_methods = wps_config_methods_str2bin(
1431                         wpa_s->conf->config_methods);
1432                 if ((wps->config_methods &
1433                      (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1434                     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1435                         wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1436                                    "config methods are not allowed at the "
1437                                    "same time");
1438                         wps->config_methods &= ~WPS_CONFIG_LABEL;
1439                 }
1440         }
1441
1442         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
1443                 if (wpa_s->conf->device_type &&
1444                     wps_dev_type_str2bin(wpa_s->conf->device_type,
1445                                          wps->dev.pri_dev_type) < 0)
1446                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1447         }
1448
1449         if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1450                 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1451
1452         if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
1453                 if (is_nil_uuid(wpa_s->conf->uuid)) {
1454                         uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1455                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1456                                     "address", wps->uuid, WPS_UUID_LEN);
1457                 } else
1458                         os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1459         }
1460
1461         if (wpa_s->conf->changed_parameters &
1462             (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1463                 /* Update pointers to make sure they refer current values */
1464                 wps->dev.device_name = wpa_s->conf->device_name;
1465                 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1466                 wps->dev.model_name = wpa_s->conf->model_name;
1467                 wps->dev.model_number = wpa_s->conf->model_number;
1468                 wps->dev.serial_number = wpa_s->conf->serial_number;
1469         }
1470 }