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