015158b3cf18eeb2bd2c04a36ba8c34dad297de8
[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", fail->msg);
395         wpas_clear_wps(wpa_s);
396         wpas_notify_wps_event_fail(wpa_s, fail);
397 }
398
399
400 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
401 {
402         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
403         wpa_s->wps_success = 1;
404         wpas_notify_wps_event_success(wpa_s);
405 #ifdef CONFIG_P2P
406         wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
407 #endif /* CONFIG_P2P */
408 }
409
410
411 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
412                                                struct wps_event_er_ap *ap)
413 {
414         char uuid_str[100];
415         char dev_type[WPS_DEV_TYPE_BUFSIZE];
416
417         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
418         if (ap->pri_dev_type)
419                 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
420                                      sizeof(dev_type));
421         else
422                 dev_type[0] = '\0';
423
424         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
425                 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
426                 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
427                 ap->friendly_name ? ap->friendly_name : "",
428                 ap->manufacturer ? ap->manufacturer : "",
429                 ap->model_description ? ap->model_description : "",
430                 ap->model_name ? ap->model_name : "",
431                 ap->manufacturer_url ? ap->manufacturer_url : "",
432                 ap->model_url ? ap->model_url : "");
433 }
434
435
436 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
437                                                   struct wps_event_er_ap *ap)
438 {
439         char uuid_str[100];
440         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
441         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
442 }
443
444
445 static void wpa_supplicant_wps_event_er_enrollee_add(
446         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
447 {
448         char uuid_str[100];
449         char dev_type[WPS_DEV_TYPE_BUFSIZE];
450
451         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
452         if (enrollee->pri_dev_type)
453                 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
454                                      sizeof(dev_type));
455         else
456                 dev_type[0] = '\0';
457
458         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
459                 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
460                 "|%s|%s|%s|%s|%s|",
461                 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
462                 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
463                 enrollee->dev_name ? enrollee->dev_name : "",
464                 enrollee->manufacturer ? enrollee->manufacturer : "",
465                 enrollee->model_name ? enrollee->model_name : "",
466                 enrollee->model_number ? enrollee->model_number : "",
467                 enrollee->serial_number ? enrollee->serial_number : "");
468 }
469
470
471 static void wpa_supplicant_wps_event_er_enrollee_remove(
472         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
473 {
474         char uuid_str[100];
475         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
476         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
477                 uuid_str, MAC2STR(enrollee->mac_addr));
478 }
479
480
481 static void wpa_supplicant_wps_event_er_ap_settings(
482         struct wpa_supplicant *wpa_s,
483         struct wps_event_er_ap_settings *ap_settings)
484 {
485         char uuid_str[100];
486         char key_str[65];
487         const struct wps_credential *cred = ap_settings->cred;
488
489         key_str[0] = '\0';
490         if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
491                 if (cred->key_len >= 8 && cred->key_len <= 64) {
492                         os_memcpy(key_str, cred->key, cred->key_len);
493                         key_str[cred->key_len] = '\0';
494                 }
495         }
496
497         uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
498         /* Use wpa_msg_ctrl to avoid showing the key in debug log */
499         wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
500                      "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
501                      "key=%s",
502                      uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
503                      cred->auth_type, cred->encr_type, key_str);
504 }
505
506
507 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
508                                      union wps_event_data *data)
509 {
510         struct wpa_supplicant *wpa_s = ctx;
511         switch (event) {
512         case WPS_EV_M2D:
513                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
514                 break;
515         case WPS_EV_FAIL:
516                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
517                 break;
518         case WPS_EV_SUCCESS:
519                 wpa_supplicant_wps_event_success(wpa_s);
520                 break;
521         case WPS_EV_PWD_AUTH_FAIL:
522                 break;
523         case WPS_EV_PBC_OVERLAP:
524                 break;
525         case WPS_EV_PBC_TIMEOUT:
526                 break;
527         case WPS_EV_ER_AP_ADD:
528                 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
529                 break;
530         case WPS_EV_ER_AP_REMOVE:
531                 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
532                 break;
533         case WPS_EV_ER_ENROLLEE_ADD:
534                 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
535                                                          &data->enrollee);
536                 break;
537         case WPS_EV_ER_ENROLLEE_REMOVE:
538                 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
539                                                             &data->enrollee);
540                 break;
541         case WPS_EV_ER_AP_SETTINGS:
542                 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
543                                                         &data->ap_settings);
544                 break;
545         }
546 }
547
548
549 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
550 {
551         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
552             eap_is_wps_pin_enrollee(&ssid->eap))
553                 return WPS_REQ_ENROLLEE;
554         else
555                 return WPS_REQ_REGISTRAR;
556 }
557
558
559 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
560 {
561         int id;
562         struct wpa_ssid *ssid, *remove_ssid = NULL;
563
564         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
565
566         /* Remove any existing WPS network from configuration */
567         ssid = wpa_s->conf->ssid;
568         while (ssid) {
569                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
570                         if (ssid == wpa_s->current_ssid) {
571                                 wpa_s->current_ssid = NULL;
572                                 if (ssid != NULL)
573                                         wpas_notify_network_changed(wpa_s);
574                         }
575                         id = ssid->id;
576                         remove_ssid = ssid;
577                 } else
578                         id = -1;
579                 ssid = ssid->next;
580                 if (id >= 0) {
581                         wpas_notify_network_removed(wpa_s, remove_ssid);
582                         wpa_config_remove_network(wpa_s->conf, id);
583                 }
584         }
585 }
586
587
588 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
589 {
590         struct wpa_supplicant *wpa_s = eloop_ctx;
591         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
592                 "out");
593         wpas_clear_wps(wpa_s);
594 }
595
596
597 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
598                                               int registrar, const u8 *bssid)
599 {
600         struct wpa_ssid *ssid;
601
602         ssid = wpa_config_add_network(wpa_s->conf);
603         if (ssid == NULL)
604                 return NULL;
605         wpas_notify_network_added(wpa_s, ssid);
606         wpa_config_set_network_defaults(ssid);
607         ssid->temporary = 1;
608         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
609             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
610             wpa_config_set(ssid, "identity", registrar ?
611                            "\"" WSC_ID_REGISTRAR "\"" :
612                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
613                 wpas_notify_network_removed(wpa_s, ssid);
614                 wpa_config_remove_network(wpa_s->conf, ssid->id);
615                 return NULL;
616         }
617
618         if (bssid) {
619 #ifndef CONFIG_P2P
620                 struct wpa_bss *bss;
621                 int count = 0;
622 #endif /* CONFIG_P2P */
623
624                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
625                 ssid->bssid_set = 1;
626
627                 /*
628                  * Note: With P2P, the SSID may change at the time the WPS
629                  * provisioning is started, so better not filter the AP based
630                  * on the current SSID in the scan results.
631                  */
632 #ifndef CONFIG_P2P
633                 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
634                         if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
635                                 continue;
636
637                         os_free(ssid->ssid);
638                         ssid->ssid = os_malloc(bss->ssid_len);
639                         if (ssid->ssid == NULL)
640                                 break;
641                         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
642                         ssid->ssid_len = bss->ssid_len;
643                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
644                                           "scan results",
645                                           ssid->ssid, ssid->ssid_len);
646                         count++;
647                 }
648
649                 if (count > 1) {
650                         wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
651                                    "for the AP; use wildcard");
652                         os_free(ssid->ssid);
653                         ssid->ssid = NULL;
654                         ssid->ssid_len = 0;
655                 }
656 #endif /* CONFIG_P2P */
657         }
658
659         return ssid;
660 }
661
662
663 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
664                              struct wpa_ssid *selected)
665 {
666         struct wpa_ssid *ssid;
667
668         /* Mark all other networks disabled and trigger reassociation */
669         ssid = wpa_s->conf->ssid;
670         while (ssid) {
671                 int was_disabled = ssid->disabled;
672                 ssid->disabled = ssid != selected;
673                 if (was_disabled != ssid->disabled)
674                         wpas_notify_network_enabled_changed(wpa_s, ssid);
675                 ssid = ssid->next;
676         }
677         wpa_s->disconnected = 0;
678         wpa_s->reassociate = 1;
679         wpa_s->scan_runs = 0;
680         wpa_s->wps_success = 0;
681         wpa_s->blacklist_cleared = 0;
682         wpa_supplicant_req_scan(wpa_s, 0, 0);
683 }
684
685
686 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
687                        int p2p_group)
688 {
689         struct wpa_ssid *ssid;
690         wpas_clear_wps(wpa_s);
691         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
692         if (ssid == NULL)
693                 return -1;
694         ssid->temporary = 1;
695         ssid->p2p_group = p2p_group;
696 #ifdef CONFIG_P2P
697         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
698                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
699                 if (ssid->ssid) {
700                         ssid->ssid_len = wpa_s->go_params->ssid_len;
701                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
702                                   ssid->ssid_len);
703                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
704                                           "SSID", ssid->ssid, ssid->ssid_len);
705                 }
706         }
707 #endif /* CONFIG_P2P */
708         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
709         if (wpa_s->wps_fragment_size)
710                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
711         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
712                                wpa_s, NULL);
713         wpas_wps_reassoc(wpa_s, ssid);
714         return 0;
715 }
716
717
718 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
719                        const char *pin, int p2p_group, u16 dev_pw_id)
720 {
721         struct wpa_ssid *ssid;
722         char val[128];
723         unsigned int rpin = 0;
724
725         wpas_clear_wps(wpa_s);
726         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
727         if (ssid == NULL)
728                 return -1;
729         ssid->temporary = 1;
730         ssid->p2p_group = p2p_group;
731 #ifdef CONFIG_P2P
732         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
733                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
734                 if (ssid->ssid) {
735                         ssid->ssid_len = wpa_s->go_params->ssid_len;
736                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
737                                   ssid->ssid_len);
738                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
739                                           "SSID", ssid->ssid, ssid->ssid_len);
740                 }
741         }
742 #endif /* CONFIG_P2P */
743         if (pin)
744                 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
745                             pin, dev_pw_id);
746         else {
747                 rpin = wps_generate_pin();
748                 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
749                             rpin, dev_pw_id);
750         }
751         wpa_config_set(ssid, "phase1", val, 0);
752         if (wpa_s->wps_fragment_size)
753                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
754         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
755                                wpa_s, NULL);
756         wpas_wps_reassoc(wpa_s, ssid);
757         return rpin;
758 }
759
760
761 /* Cancel the wps pbc/pin requests */
762 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
763 {
764 #ifdef CONFIG_AP
765         if (wpa_s->ap_iface) {
766                 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
767                 return wpa_supplicant_ap_wps_cancel(wpa_s);
768         }
769 #endif /* CONFIG_AP */
770
771         if (wpa_s->wpa_state == WPA_SCANNING) {
772                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
773                 wpa_supplicant_cancel_scan(wpa_s);
774                 wpas_clear_wps(wpa_s);
775         } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
776                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
777                            "deauthenticate");
778                 wpa_supplicant_deauthenticate(wpa_s,
779                                               WLAN_REASON_DEAUTH_LEAVING);
780                 wpas_clear_wps(wpa_s);
781         }
782
783         return 0;
784 }
785
786
787 #ifdef CONFIG_WPS_OOB
788 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
789                        char *path, char *method, char *name)
790 {
791         struct wps_context *wps = wpa_s->wps;
792         struct oob_device_data *oob_dev;
793
794         oob_dev = wps_get_oob_device(device_type);
795         if (oob_dev == NULL)
796                 return -1;
797         oob_dev->device_path = path;
798         oob_dev->device_name = name;
799         wps->oob_conf.oob_method = wps_get_oob_method(method);
800
801         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
802                 /*
803                  * Use pre-configured DH keys in order to be able to write the
804                  * key hash into the OOB file.
805                  */
806                 wpabuf_free(wps->dh_pubkey);
807                 wpabuf_free(wps->dh_privkey);
808                 wps->dh_privkey = NULL;
809                 wps->dh_pubkey = NULL;
810                 dh5_free(wps->dh_ctx);
811                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
812                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
813                 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
814                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
815                                    "Diffie-Hellman handshake");
816                         return -1;
817                 }
818         }
819
820         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
821                 wpas_clear_wps(wpa_s);
822
823         if (wps_process_oob(wps, oob_dev, 0) < 0)
824                 return -1;
825
826         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
827              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
828             wpas_wps_start_pin(wpa_s, NULL,
829                                wpabuf_head(wps->oob_conf.dev_password), 0) < 0)
830                         return -1;
831
832         return 0;
833 }
834 #endif /* CONFIG_WPS_OOB */
835
836
837 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
838                        const char *pin, struct wps_new_ap_settings *settings)
839 {
840         struct wpa_ssid *ssid;
841         char val[200];
842         char *pos, *end;
843         int res;
844
845         if (!pin)
846                 return -1;
847         wpas_clear_wps(wpa_s);
848         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
849         if (ssid == NULL)
850                 return -1;
851         ssid->temporary = 1;
852         pos = val;
853         end = pos + sizeof(val);
854         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
855         if (res < 0 || res >= end - pos)
856                 return -1;
857         pos += res;
858         if (settings) {
859                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
860                                   "new_encr=%s new_key=%s",
861                                   settings->ssid_hex, settings->auth,
862                                   settings->encr, settings->key_hex);
863                 if (res < 0 || res >= end - pos)
864                         return -1;
865                 pos += res;
866         }
867         res = os_snprintf(pos, end - pos, "\"");
868         if (res < 0 || res >= end - pos)
869                 return -1;
870         wpa_config_set(ssid, "phase1", val, 0);
871         if (wpa_s->wps_fragment_size)
872                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
873         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
874                                wpa_s, NULL);
875         wpas_wps_reassoc(wpa_s, ssid);
876         return 0;
877 }
878
879
880 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
881                                size_t psk_len)
882 {
883         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
884                    "STA " MACSTR, MAC2STR(mac_addr));
885         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
886
887         /* TODO */
888
889         return 0;
890 }
891
892
893 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
894                                    const struct wps_device_data *dev)
895 {
896         char uuid[40], txt[400];
897         int len;
898         char devtype[WPS_DEV_TYPE_BUFSIZE];
899         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
900                 return;
901         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
902         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
903                           " [%s|%s|%s|%s|%s|%s]",
904                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
905                           dev->manufacturer, dev->model_name,
906                           dev->model_number, dev->serial_number,
907                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
908                                                sizeof(devtype)));
909         if (len > 0 && len < (int) sizeof(txt))
910                 wpa_printf(MSG_INFO, "%s", txt);
911 }
912
913
914 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
915                                     u16 sel_reg_config_methods)
916 {
917 #ifdef CONFIG_WPS_ER
918         struct wpa_supplicant *wpa_s = ctx;
919
920         if (wpa_s->wps_er == NULL)
921                 return;
922         wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
923                    "dev_password_id=%u sel_reg_config_methods=0x%x",
924                    sel_reg, dev_passwd_id, sel_reg_config_methods);
925         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
926                            sel_reg_config_methods);
927 #endif /* CONFIG_WPS_ER */
928 }
929
930
931 int wpas_wps_init(struct wpa_supplicant *wpa_s)
932 {
933         struct wps_context *wps;
934         struct wps_registrar_config rcfg;
935
936         wps = os_zalloc(sizeof(*wps));
937         if (wps == NULL)
938                 return -1;
939
940         wps->cred_cb = wpa_supplicant_wps_cred;
941         wps->event_cb = wpa_supplicant_wps_event;
942         wps->cb_ctx = wpa_s;
943
944         wps->dev.device_name = wpa_s->conf->device_name;
945         wps->dev.manufacturer = wpa_s->conf->manufacturer;
946         wps->dev.model_name = wpa_s->conf->model_name;
947         wps->dev.model_number = wpa_s->conf->model_number;
948         wps->dev.serial_number = wpa_s->conf->serial_number;
949         wps->config_methods =
950                 wps_config_methods_str2bin(wpa_s->conf->config_methods);
951         if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
952             (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
953                 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
954                            "methods are not allowed at the same time");
955                 os_free(wps);
956                 return -1;
957         }
958         if (wpa_s->conf->device_type &&
959             wps_dev_type_str2bin(wpa_s->conf->device_type,
960                                  wps->dev.pri_dev_type) < 0) {
961                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
962                 os_free(wps);
963                 return -1;
964         }
965         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
966         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
967         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
968         if (is_nil_uuid(wpa_s->conf->uuid)) {
969                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
970                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
971                             wps->uuid, WPS_UUID_LEN);
972         } else
973                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
974
975         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
976         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
977
978         os_memset(&rcfg, 0, sizeof(rcfg));
979         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
980         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
981         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
982         rcfg.cb_ctx = wpa_s;
983
984         wps->registrar = wps_registrar_init(wps, &rcfg);
985         if (wps->registrar == NULL) {
986                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
987                 os_free(wps);
988                 return -1;
989         }
990
991         wpa_s->wps = wps;
992
993         return 0;
994 }
995
996
997 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
998 {
999         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1000
1001         if (wpa_s->wps == NULL)
1002                 return;
1003
1004 #ifdef CONFIG_WPS_ER
1005         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1006         wpa_s->wps_er = NULL;
1007 #endif /* CONFIG_WPS_ER */
1008
1009         wps_registrar_deinit(wpa_s->wps->registrar);
1010         wpabuf_free(wpa_s->wps->dh_pubkey);
1011         wpabuf_free(wpa_s->wps->dh_privkey);
1012         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1013         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1014         os_free(wpa_s->wps->network_key);
1015         os_free(wpa_s->wps);
1016         wpa_s->wps = NULL;
1017 }
1018
1019
1020 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1021                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1022 {
1023         struct wpabuf *wps_ie;
1024
1025         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1026                 return -1;
1027
1028         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1029         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1030                 if (!wps_ie) {
1031                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1032                         return 0;
1033                 }
1034
1035                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1036                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1037                                    "without active PBC Registrar");
1038                         wpabuf_free(wps_ie);
1039                         return 0;
1040                 }
1041
1042                 /* TODO: overlap detection */
1043                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1044                            "(Active PBC)");
1045                 wpabuf_free(wps_ie);
1046                 return 1;
1047         }
1048
1049         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1050                 if (!wps_ie) {
1051                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1052                         return 0;
1053                 }
1054
1055                 /*
1056                  * Start with WPS APs that advertise our address as an
1057                  * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1058                  * allow any WPS AP after couple of scans since some APs do not
1059                  * set Selected Registrar attribute properly when using
1060                  * external Registrar.
1061                  */
1062                 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1063                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1064                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1065                                            "without active PIN Registrar");
1066                                 wpabuf_free(wps_ie);
1067                                 return 0;
1068                         }
1069                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1070                 } else {
1071                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1072                                    "(Authorized MAC or Active PIN)");
1073                 }
1074                 wpabuf_free(wps_ie);
1075                 return 1;
1076         }
1077
1078         if (wps_ie) {
1079                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1080                 wpabuf_free(wps_ie);
1081                 return 1;
1082         }
1083
1084         return -1;
1085 }
1086
1087
1088 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1089                               struct wpa_ssid *ssid,
1090                               struct wpa_scan_res *bss)
1091 {
1092         struct wpabuf *wps_ie = NULL;
1093         int ret = 0;
1094
1095         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1096                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1097                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1098                         /* allow wildcard SSID for WPS PBC */
1099                         ret = 1;
1100                 }
1101         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1102                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1103                 if (wps_ie &&
1104                     (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1105                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1106                         /* allow wildcard SSID for WPS PIN */
1107                         ret = 1;
1108                 }
1109         }
1110
1111         if (!ret && ssid->bssid_set &&
1112             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1113                 /* allow wildcard SSID due to hardcoded BSSID match */
1114                 ret = 1;
1115         }
1116
1117 #ifdef CONFIG_WPS_STRICT
1118         if (wps_ie) {
1119                 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1120                                                    0, bss->bssid) < 0)
1121                         ret = 0;
1122                 if (bss->beacon_ie_len) {
1123                         struct wpabuf *bcn_wps;
1124                         bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1125                                 bss, WPS_IE_VENDOR_TYPE);
1126                         if (bcn_wps == NULL) {
1127                                 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1128                                            "missing from AP Beacon");
1129                                 ret = 0;
1130                         } else {
1131                                 if (wps_validate_beacon(wps_ie) < 0)
1132                                         ret = 0;
1133                                 wpabuf_free(bcn_wps);
1134                         }
1135                 }
1136         }
1137 #endif /* CONFIG_WPS_STRICT */
1138
1139         wpabuf_free(wps_ie);
1140
1141         return ret;
1142 }
1143
1144
1145 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1146                               struct wpa_bss *selected, struct wpa_ssid *ssid)
1147 {
1148         const u8 *sel_uuid, *uuid;
1149         struct wpabuf *wps_ie;
1150         int ret = 0;
1151         struct wpa_bss *bss;
1152
1153         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1154                 return 0;
1155
1156         wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1157                    "present in scan results; selected BSSID " MACSTR,
1158                    MAC2STR(selected->bssid));
1159
1160         /* Make sure that only one AP is in active PBC mode */
1161         wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1162         if (wps_ie) {
1163                 sel_uuid = wps_get_uuid_e(wps_ie);
1164                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1165                             sel_uuid, UUID_LEN);
1166         } else {
1167                 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1168                            "WPS IE?!");
1169                 sel_uuid = NULL;
1170         }
1171
1172         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1173                 struct wpabuf *ie;
1174                 if (bss == selected)
1175                         continue;
1176                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1177                 if (!ie)
1178                         continue;
1179                 if (!wps_is_selected_pbc_registrar(ie)) {
1180                         wpabuf_free(ie);
1181                         continue;
1182                 }
1183                 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1184                            MACSTR, MAC2STR(bss->bssid));
1185                 uuid = wps_get_uuid_e(ie);
1186                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1187                             uuid, UUID_LEN);
1188                 if (sel_uuid == NULL || uuid == NULL ||
1189                     os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1190                         ret = 1; /* PBC overlap */
1191                         wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1192                                 MACSTR " and " MACSTR,
1193                                 MAC2STR(selected->bssid),
1194                                 MAC2STR(bss->bssid));
1195                         wpabuf_free(ie);
1196                         break;
1197                 }
1198
1199                 /* TODO: verify that this is reasonable dual-band situation */
1200
1201                 wpabuf_free(ie);
1202         }
1203
1204         wpabuf_free(wps_ie);
1205
1206         return ret;
1207 }
1208
1209
1210 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1211 {
1212         struct wpa_bss *bss;
1213
1214         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1215                 return;
1216
1217         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1218                 struct wpabuf *ie;
1219                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1220                 if (!ie)
1221                         continue;
1222                 if (wps_is_selected_pbc_registrar(ie))
1223                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1224                                      WPS_EVENT_AP_AVAILABLE_PBC);
1225                 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1226                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1227                                      WPS_EVENT_AP_AVAILABLE_AUTH);
1228                 else if (wps_is_selected_pin_registrar(ie))
1229                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1230                                      WPS_EVENT_AP_AVAILABLE_PIN);
1231                 else
1232                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1233                                      WPS_EVENT_AP_AVAILABLE);
1234                 wpabuf_free(ie);
1235                 break;
1236         }
1237 }
1238
1239
1240 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1241 {
1242         struct wpa_ssid *ssid;
1243
1244         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1245                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1246                         return 1;
1247         }
1248
1249         return 0;
1250 }
1251
1252
1253 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1254                               char *end)
1255 {
1256         struct wpabuf *wps_ie;
1257         int ret;
1258
1259         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1260         if (wps_ie == NULL)
1261                 return 0;
1262
1263         ret = wps_attr_text(wps_ie, buf, end);
1264         wpabuf_free(wps_ie);
1265         return ret;
1266 }
1267
1268
1269 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1270 {
1271 #ifdef CONFIG_WPS_ER
1272         if (wpa_s->wps_er) {
1273                 wps_er_refresh(wpa_s->wps_er);
1274                 return 0;
1275         }
1276         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1277         if (wpa_s->wps_er == NULL)
1278                 return -1;
1279         return 0;
1280 #else /* CONFIG_WPS_ER */
1281         return 0;
1282 #endif /* CONFIG_WPS_ER */
1283 }
1284
1285
1286 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1287 {
1288 #ifdef CONFIG_WPS_ER
1289         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1290         wpa_s->wps_er = NULL;
1291 #endif /* CONFIG_WPS_ER */
1292         return 0;
1293 }
1294
1295
1296 #ifdef CONFIG_WPS_ER
1297 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1298                         const char *uuid, const char *pin)
1299 {
1300         u8 u[UUID_LEN];
1301         int any = 0;
1302
1303         if (os_strcmp(uuid, "any") == 0)
1304                 any = 1;
1305         else if (uuid_str2bin(uuid, u))
1306                 return -1;
1307         return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1308                                      any ? NULL : u,
1309                                      (const u8 *) pin, os_strlen(pin), 300);
1310 }
1311
1312
1313 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1314 {
1315         u8 u[UUID_LEN];
1316
1317         if (uuid_str2bin(uuid, u))
1318                 return -1;
1319         return wps_er_pbc(wpa_s->wps_er, u);
1320 }
1321
1322
1323 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1324                       const char *pin)
1325 {
1326         u8 u[UUID_LEN];
1327
1328         if (uuid_str2bin(uuid, u))
1329                 return -1;
1330         return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1331                             os_strlen(pin));
1332 }
1333
1334
1335 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1336                        const char *pin, struct wps_new_ap_settings *settings)
1337 {
1338         u8 u[UUID_LEN];
1339         struct wps_credential cred;
1340         size_t len;
1341
1342         if (uuid_str2bin(uuid, u))
1343                 return -1;
1344         if (settings->ssid_hex == NULL || settings->auth == NULL ||
1345             settings->encr == NULL || settings->key_hex == NULL)
1346                 return -1;
1347
1348         os_memset(&cred, 0, sizeof(cred));
1349         len = os_strlen(settings->ssid_hex);
1350         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1351             hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1352                 return -1;
1353         cred.ssid_len = len / 2;
1354
1355         len = os_strlen(settings->key_hex);
1356         if ((len & 1) || len > 2 * sizeof(cred.key) ||
1357             hexstr2bin(settings->key_hex, cred.key, len / 2))
1358                 return -1;
1359         cred.key_len = len / 2;
1360
1361         if (os_strcmp(settings->auth, "OPEN") == 0)
1362                 cred.auth_type = WPS_AUTH_OPEN;
1363         else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1364                 cred.auth_type = WPS_AUTH_WPAPSK;
1365         else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1366                 cred.auth_type = WPS_AUTH_WPA2PSK;
1367         else
1368                 return -1;
1369
1370         if (os_strcmp(settings->encr, "NONE") == 0)
1371                 cred.encr_type = WPS_ENCR_NONE;
1372         else if (os_strcmp(settings->encr, "WEP") == 0)
1373                 cred.encr_type = WPS_ENCR_WEP;
1374         else if (os_strcmp(settings->encr, "TKIP") == 0)
1375                 cred.encr_type = WPS_ENCR_TKIP;
1376         else if (os_strcmp(settings->encr, "CCMP") == 0)
1377                 cred.encr_type = WPS_ENCR_AES;
1378         else
1379                 return -1;
1380
1381         return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1382                              os_strlen(pin), &cred);
1383 }
1384
1385
1386 static void wpas_wps_terminate_cb(void *ctx)
1387 {
1388         wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1389         eloop_terminate();
1390 }
1391 #endif /* CONFIG_WPS_ER */
1392
1393
1394 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1395 {
1396 #ifdef CONFIG_WPS_ER
1397         if (wpa_s->wps_er) {
1398                 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1399                 wpa_s->wps_er = NULL;
1400                 return 1;
1401         }
1402 #endif /* CONFIG_WPS_ER */
1403         return 0;
1404 }
1405
1406
1407 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1408 {
1409         struct wpa_ssid *ssid;
1410
1411         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1412                 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1413                         return 1;
1414         }
1415
1416         return 0;
1417 }
1418
1419
1420 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1421 {
1422         struct wps_context *wps = wpa_s->wps;
1423
1424         if (wps == NULL)
1425                 return;
1426
1427         if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1428                 wps->config_methods = wps_config_methods_str2bin(
1429                         wpa_s->conf->config_methods);
1430                 if ((wps->config_methods &
1431                      (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1432                     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1433                         wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1434                                    "config methods are not allowed at the "
1435                                    "same time");
1436                         wps->config_methods &= ~WPS_CONFIG_LABEL;
1437                 }
1438         }
1439
1440         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
1441                 if (wpa_s->conf->device_type &&
1442                     wps_dev_type_str2bin(wpa_s->conf->device_type,
1443                                          wps->dev.pri_dev_type) < 0)
1444                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1445         }
1446
1447         if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1448                 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1449
1450         if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
1451                 if (is_nil_uuid(wpa_s->conf->uuid)) {
1452                         uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1453                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1454                                     "address", wps->uuid, WPS_UUID_LEN);
1455                 } else
1456                         os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1457         }
1458
1459         if (wpa_s->conf->changed_parameters &
1460             (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1461                 /* Update pointers to make sure they refer current values */
1462                 wps->dev.device_name = wpa_s->conf->device_name;
1463                 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1464                 wps->dev.model_name = wpa_s->conf->model_name;
1465                 wps->dev.model_number = wpa_s->conf->model_number;
1466                 wps->dev.serial_number = wpa_s->conf->serial_number;
1467         }
1468 }