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