OSDN Git Service

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