WPS: Fixed deinit code for freeing config and registrar data
[mech_eap.orig] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008, 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 "ieee802_11_defs.h"
19 #include "wpa_common.h"
20 #include "config.h"
21 #include "eap_peer/eap.h"
22 #include "wpa_supplicant_i.h"
23 #include "eloop.h"
24 #include "wpa_ctrl.h"
25 #include "eap_common/eap_wsc_common.h"
26 #include "wps/wps.h"
27 #include "wps/wps_defs.h"
28 #include "wps_supplicant.h"
29
30
31 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
32
33
34 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
35 {
36         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
37
38         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
39             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
40                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
41                            "try to associate with the received credential");
42                 wpa_supplicant_deauthenticate(wpa_s,
43                                               WLAN_REASON_DEAUTH_LEAVING);
44                 wpa_s->reassociate = 1;
45                 wpa_supplicant_req_scan(wpa_s, 0, 0);
46                 return 1;
47         }
48
49         return 0;
50 }
51
52
53 static int wpa_supplicant_wps_cred(void *ctx,
54                                    const struct wps_credential *cred)
55 {
56         struct wpa_supplicant *wpa_s = ctx;
57         struct wpa_ssid *ssid = wpa_s->current_ssid;
58
59         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
60
61         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
62                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
63                            "on the received credential");
64                 os_free(ssid->eap.identity);
65                 ssid->eap.identity = NULL;
66                 ssid->eap.identity_len = 0;
67                 os_free(ssid->eap.phase1);
68                 ssid->eap.phase1 = NULL;
69                 os_free(ssid->eap.eap_methods);
70                 ssid->eap.eap_methods = NULL;
71         } else {
72                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
73                            "received credential");
74                 ssid = wpa_config_add_network(wpa_s->conf);
75                 if (ssid == NULL)
76                         return -1;
77         }
78
79         wpa_config_set_network_defaults(ssid);
80
81         os_free(ssid->ssid);
82         ssid->ssid = os_malloc(cred->ssid_len);
83         if (ssid->ssid) {
84                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
85                 ssid->ssid_len = cred->ssid_len;
86         }
87
88         switch (cred->encr_type) {
89         case WPS_ENCR_NONE:
90                 ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
91                 break;
92         case WPS_ENCR_WEP:
93                 ssid->pairwise_cipher = ssid->group_cipher =
94                         WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
95                 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
96                     cred->key_idx < NUM_WEP_KEYS) {
97                         os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
98                                   cred->key_len);
99                         ssid->wep_key_len[cred->key_idx] = cred->key_len;
100                         ssid->wep_tx_keyidx = cred->key_idx;
101                 }
102                 break;
103         case WPS_ENCR_TKIP:
104                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
105                 ssid->group_cipher = WPA_CIPHER_TKIP;
106                 break;
107         case WPS_ENCR_AES:
108                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
109                 ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
110                 break;
111         }
112
113         switch (cred->auth_type) {
114         case WPS_AUTH_OPEN:
115                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
116                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
117                 ssid->proto = 0;
118                 break;
119         case WPS_AUTH_SHARED:
120                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
121                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
122                 ssid->proto = 0;
123                 break;
124         case WPS_AUTH_WPAPSK:
125                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
126                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
127                 ssid->proto = WPA_PROTO_WPA;
128                 break;
129         case WPS_AUTH_WPA:
130                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
131                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
132                 ssid->proto = WPA_PROTO_WPA;
133                 break;
134         case WPS_AUTH_WPA2:
135                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
136                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
137                 ssid->proto = WPA_PROTO_RSN;
138                 break;
139         case WPS_AUTH_WPA2PSK:
140                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
141                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
142                 ssid->proto = WPA_PROTO_RSN;
143                 break;
144         }
145
146         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
147                 if (cred->key_len == 2 * PMK_LEN) {
148                         if (hexstr2bin((const char *) cred->key, ssid->psk,
149                                        PMK_LEN)) {
150                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
151                                            "Key");
152                                 return -1;
153                         }
154                         ssid->psk_set = 1;
155                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
156                         os_free(ssid->passphrase);
157                         ssid->passphrase = os_malloc(cred->key_len + 1);
158                         if (ssid->passphrase == NULL)
159                                 return -1;
160                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
161                         ssid->passphrase[cred->key_len] = '\0';
162                         wpa_config_update_psk(ssid);
163                 } else {
164                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
165                                    "length %lu",
166                                    (unsigned long) cred->key_len);
167                         return -1;
168                 }
169         }
170
171 #ifndef CONFIG_NO_CONFIG_WRITE
172         if (wpa_s->conf->update_config &&
173             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
174                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
175                 return -1;
176         }
177 #endif /* CONFIG_NO_CONFIG_WRITE */
178
179         return 0;
180 }
181
182
183 u8 wpas_wps_get_req_type(struct wpa_ssid *ssid)
184 {
185         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
186             eap_is_wps_pin_enrollee(&ssid->eap))
187                 return WPS_REQ_ENROLLEE;
188         else
189                 return WPS_REQ_REGISTRAR;
190 }
191
192
193 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
194 {
195         int id;
196         struct wpa_ssid *ssid;
197
198         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
199
200         /* Remove any existing WPS network from configuration */
201         ssid = wpa_s->conf->ssid;
202         while (ssid) {
203                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
204                         if (ssid == wpa_s->current_ssid)
205                                 wpa_s->current_ssid = NULL;
206                         id = ssid->id;
207                 } else
208                         id = -1;
209                 ssid = ssid->next;
210                 if (id >= 0)
211                         wpa_config_remove_network(wpa_s->conf, id);
212         }
213 }
214
215
216 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
217 {
218         struct wpa_supplicant *wpa_s = eloop_ctx;
219         wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
220         wpas_clear_wps(wpa_s);
221 }
222
223
224 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
225                                               int registrar, const u8 *bssid)
226 {
227         struct wpa_ssid *ssid;
228
229         ssid = wpa_config_add_network(wpa_s->conf);
230         if (ssid == NULL)
231                 return NULL;
232         wpa_config_set_network_defaults(ssid);
233         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
234             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
235             wpa_config_set(ssid, "identity", registrar ?
236                            "\"" WSC_ID_REGISTRAR "\"" :
237                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
238                 wpa_config_remove_network(wpa_s->conf, ssid->id);
239                 return NULL;
240         }
241
242         if (bssid) {
243                 size_t i;
244                 struct wpa_scan_res *res;
245
246                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
247
248                 /* Try to get SSID from scan results */
249                 if (wpa_s->scan_res == NULL &&
250                     wpa_supplicant_get_scan_results(wpa_s) < 0)
251                         return ssid; /* Could not find any scan results */
252
253                 for (i = 0; i < wpa_s->scan_res->num; i++) {
254                         const u8 *ie;
255
256                         res = wpa_s->scan_res->res[i];
257                         if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
258                                 continue;
259
260                         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
261                         if (ie == NULL)
262                                 break;
263                         os_free(ssid->ssid);
264                         ssid->ssid = os_malloc(ie[1]);
265                         if (ssid->ssid == NULL)
266                                 break;
267                         os_memcpy(ssid->ssid, ie + 2, ie[1]);
268                         ssid->ssid_len = ie[1];
269                         break;
270                 }
271         }
272
273         return ssid;
274 }
275
276
277 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
278                              struct wpa_ssid *selected)
279 {
280         struct wpa_ssid *ssid;
281
282         /* Mark all other networks disabled and trigger reassociation */
283         ssid = wpa_s->conf->ssid;
284         while (ssid) {
285                 ssid->disabled = ssid != selected;
286                 ssid = ssid->next;
287         }
288         wpa_s->disconnected = 0;
289         wpa_s->reassociate = 1;
290         wpa_supplicant_req_scan(wpa_s, 0, 0);
291 }
292
293
294 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
295 {
296         struct wpa_ssid *ssid;
297         wpas_clear_wps(wpa_s);
298         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
299         if (ssid == NULL)
300                 return -1;
301         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
302         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
303                                wpa_s, NULL);
304         wpas_wps_reassoc(wpa_s, ssid);
305         return 0;
306 }
307
308
309 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
310                        const char *pin)
311 {
312         struct wpa_ssid *ssid;
313         char val[30];
314         unsigned int rpin = 0;
315
316         wpas_clear_wps(wpa_s);
317         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
318         if (ssid == NULL)
319                 return -1;
320         if (pin)
321                 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
322         else {
323                 rpin = wps_generate_pin();
324                 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
325         }
326         wpa_config_set(ssid, "phase1", val, 0);
327         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
328                                wpa_s, NULL);
329         wpas_wps_reassoc(wpa_s, ssid);
330         return rpin;
331 }
332
333
334 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
335                        const char *pin)
336 {
337         struct wpa_ssid *ssid;
338         char val[30];
339
340         if (!pin)
341                 return -1;
342         wpas_clear_wps(wpa_s);
343         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
344         if (ssid == NULL)
345                 return -1;
346         os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
347         wpa_config_set(ssid, "phase1", val, 0);
348         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
349                                wpa_s, NULL);
350         wpas_wps_reassoc(wpa_s, ssid);
351         return 0;
352 }
353
354
355 int wpas_wps_init(struct wpa_supplicant *wpa_s)
356 {
357         struct wps_context *wps;
358
359         wps = os_zalloc(sizeof(*wps));
360         if (wps == NULL)
361                 return -1;
362
363         wps->cred_cb = wpa_supplicant_wps_cred;
364         wps->cb_ctx = wpa_s;
365
366         wps->dev.device_name = wpa_s->conf->device_name;
367         wps->dev.manufacturer = wpa_s->conf->manufacturer;
368         wps->dev.model_name = wpa_s->conf->model_name;
369         wps->dev.model_number = wpa_s->conf->model_number;
370         wps->dev.serial_number = wpa_s->conf->serial_number;
371         if (wpa_s->conf->device_type) {
372                 char *pos;
373                 u8 oui[4];
374                 /* <categ>-<OUI>-<subcateg> */
375                 wps->dev.categ = atoi(wpa_s->conf->device_type);
376                 pos = os_strchr(wpa_s->conf->device_type, '-');
377                 if (pos == NULL) {
378                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
379                         os_free(wps);
380                         return -1;
381                 }
382                 pos++;
383                 if (hexstr2bin(pos, oui, 4)) {
384                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
385                         os_free(wps);
386                         return -1;
387                 }
388                 wps->dev.oui = WPA_GET_BE32(oui);
389                 pos = os_strchr(pos, '-');
390                 if (pos == NULL) {
391                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
392                         os_free(wps);
393                         return -1;
394                 }
395                 pos++;
396                 wps->dev.sub_categ = atoi(pos);
397         }
398         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
399         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
400         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
401         os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
402
403         wpa_s->wps = wps;
404
405         return 0;
406 }
407
408
409 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
410 {
411         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
412
413         if (wpa_s->wps == NULL)
414                 return;
415
416         os_free(wpa_s->wps->network_key);
417         os_free(wpa_s->wps);
418         wpa_s->wps = NULL;
419 }
420
421
422 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
423 {
424         struct wpabuf *wps_ie;
425
426         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
427                 return -1;
428
429         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
430         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
431                 if (!wps_ie) {
432                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
433                         return 0;
434                 }
435
436                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
437                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
438                                    "without active PBC Registrar");
439                         wpabuf_free(wps_ie);
440                         return 0;
441                 }
442
443                 /* TODO: overlap detection */
444                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
445                            "(Active PBC)");
446                 wpabuf_free(wps_ie);
447                 return 1;
448         }
449
450         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
451                 if (!wps_ie) {
452                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
453                         return 0;
454                 }
455
456                 if (!wps_is_selected_pin_registrar(wps_ie)) {
457                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
458                                    "without active PIN Registrar");
459                         wpabuf_free(wps_ie);
460                         return 0;
461                 }
462                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
463                            "(Active PIN)");
464                 wpabuf_free(wps_ie);
465                 return 1;
466         }
467
468         if (wps_ie) {
469                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
470                 wpabuf_free(wps_ie);
471                 return 1;
472         }
473
474         return -1;
475 }
476
477
478 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
479                               struct wpa_scan_res *bss)
480 {
481         struct wpabuf *wps_ie = NULL;
482         int ret = 0;
483
484         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
485                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
486                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
487                         /* allow wildcard SSID for WPS PBC */
488                         ret = 1;
489                 }
490         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
491                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
492                 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
493                         /* allow wildcard SSID for WPS PIN */
494                         ret = 1;
495                 }
496         }
497
498         wpabuf_free(wps_ie);
499
500         return ret;
501 }
502
503
504 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
505                               struct wpa_scan_res *selected,
506                               struct wpa_ssid *ssid)
507 {
508         const u8 *sel_uuid, *uuid;
509         size_t i;
510         struct wpabuf *wps_ie;
511         int ret = 0;
512
513         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
514                 return 0;
515
516         /* Make sure that only one AP is in active PBC mode */
517         wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
518         if (wps_ie)
519                 sel_uuid = wps_get_uuid_e(wps_ie);
520         else
521                 sel_uuid = NULL;
522         if (!sel_uuid) {
523                 wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for PBC "
524                            "overlap detection");
525                 wpabuf_free(wps_ie);
526                 return 1;
527         }
528
529         for (i = 0; i < wpa_s->scan_res->num; i++) {
530                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
531                 struct wpabuf *ie;
532                 if (bss == selected)
533                         continue;
534                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
535                 if (!ie)
536                         continue;
537                 if (!wps_is_selected_pbc_registrar(ie)) {
538                         wpabuf_free(ie);
539                         continue;
540                 }
541                 uuid = wps_get_uuid_e(ie);
542                 if (uuid == NULL) {
543                         wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for "
544                                    "PBC overlap detection (other BSS)");
545                         ret = 1;
546                         wpabuf_free(ie);
547                         break;
548                 }
549                 if (os_memcmp(sel_uuid, uuid, 16) != 0) {
550                         ret = 1; /* PBC overlap */
551                         wpabuf_free(ie);
552                         break;
553                 }
554
555                 /* TODO: verify that this is reasonable dual-band situation */
556
557                 wpabuf_free(ie);
558         }
559
560         wpabuf_free(wps_ie);
561
562         return ret;
563 }
564
565
566 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
567 {
568         size_t i;
569
570         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
571                 return;
572
573         for (i = 0; i < wpa_s->scan_res->num; i++) {
574                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
575                 struct wpabuf *ie;
576                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
577                 if (!ie)
578                         continue;
579                 if (wps_is_selected_pbc_registrar(ie))
580                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
581                 else if (wps_is_selected_pin_registrar(ie))
582                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
583                 else
584                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
585                 wpabuf_free(ie);
586                 break;
587         }
588 }