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