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