WPS: Added control interface notification for available WPS APs
[libeap.git] / 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: New credential 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                         id = ssid->id;
205                 else
206                         id = -1;
207                 ssid = ssid->next;
208                 if (id >= 0)
209                         wpa_config_remove_network(wpa_s->conf, id);
210         }
211 }
212
213
214 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
215 {
216         struct wpa_supplicant *wpa_s = eloop_ctx;
217         wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
218         wpas_clear_wps(wpa_s);
219 }
220
221
222 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
223                                               int registrar, const u8 *bssid)
224 {
225         struct wpa_ssid *ssid;
226
227         ssid = wpa_config_add_network(wpa_s->conf);
228         if (ssid == NULL)
229                 return NULL;
230         wpa_config_set_network_defaults(ssid);
231         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
232             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
233             wpa_config_set(ssid, "identity", registrar ?
234                            "\"" WSC_ID_REGISTRAR "\"" :
235                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
236                 wpa_config_remove_network(wpa_s->conf, ssid->id);
237                 return NULL;
238         }
239
240         if (bssid) {
241                 size_t i;
242                 struct wpa_scan_res *res;
243
244                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
245
246                 /* Try to get SSID from scan results */
247                 if (wpa_s->scan_res == NULL &&
248                     wpa_supplicant_get_scan_results(wpa_s) < 0)
249                         return ssid; /* Could not find any scan results */
250
251                 for (i = 0; i < wpa_s->scan_res->num; i++) {
252                         const u8 *ie;
253
254                         res = wpa_s->scan_res->res[i];
255                         if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
256                                 continue;
257
258                         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
259                         if (ie == NULL)
260                                 break;
261                         os_free(ssid->ssid);
262                         ssid->ssid = os_malloc(ie[1]);
263                         if (ssid->ssid == NULL)
264                                 break;
265                         os_memcpy(ssid->ssid, ie + 2, ie[1]);
266                         ssid->ssid_len = ie[1];
267                         break;
268                 }
269         }
270
271         return ssid;
272 }
273
274
275 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
276                              struct wpa_ssid *selected)
277 {
278         struct wpa_ssid *ssid;
279
280         /* Mark all other networks disabled and trigger reassociation */
281         ssid = wpa_s->conf->ssid;
282         while (ssid) {
283                 ssid->disabled = ssid != selected;
284                 ssid = ssid->next;
285         }
286         wpa_s->disconnected = 0;
287         wpa_s->reassociate = 1;
288         wpa_supplicant_req_scan(wpa_s, 0, 0);
289 }
290
291
292 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
293 {
294         struct wpa_ssid *ssid;
295         wpas_clear_wps(wpa_s);
296         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
297         if (ssid == NULL)
298                 return -1;
299         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
300         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
301                                wpa_s, NULL);
302         wpas_wps_reassoc(wpa_s, ssid);
303         return 0;
304 }
305
306
307 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
308                        const char *pin)
309 {
310         struct wpa_ssid *ssid;
311         char val[30];
312         unsigned int rpin = 0;
313
314         wpas_clear_wps(wpa_s);
315         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
316         if (ssid == NULL)
317                 return -1;
318         if (pin)
319                 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
320         else {
321                 rpin = wps_generate_pin();
322                 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
323         }
324         wpa_config_set(ssid, "phase1", val, 0);
325         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
326                                wpa_s, NULL);
327         wpas_wps_reassoc(wpa_s, ssid);
328         return rpin;
329 }
330
331
332 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
333                        const char *pin)
334 {
335         struct wpa_ssid *ssid;
336         char val[30];
337
338         if (!pin)
339                 return -1;
340         wpas_clear_wps(wpa_s);
341         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
342         if (ssid == NULL)
343                 return -1;
344         os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
345         wpa_config_set(ssid, "phase1", val, 0);
346         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
347                                wpa_s, NULL);
348         wpas_wps_reassoc(wpa_s, ssid);
349         return 0;
350 }
351
352
353 int wpas_wps_init(struct wpa_supplicant *wpa_s)
354 {
355         struct wps_context *wps;
356
357         wps = os_zalloc(sizeof(*wps));
358         if (wps == NULL)
359                 return -1;
360
361         wps->cred_cb = wpa_supplicant_wps_cred;
362         wps->cb_ctx = wpa_s;
363
364         wps->dev.device_name = wpa_s->conf->device_name;
365         wps->dev.manufacturer = wpa_s->conf->manufacturer;
366         wps->dev.model_name = wpa_s->conf->model_name;
367         wps->dev.model_number = wpa_s->conf->model_number;
368         wps->dev.serial_number = wpa_s->conf->serial_number;
369         if (wpa_s->conf->device_type) {
370                 char *pos;
371                 u8 oui[4];
372                 /* <categ>-<OUI>-<subcateg> */
373                 wps->dev.categ = atoi(wpa_s->conf->device_type);
374                 pos = os_strchr(wpa_s->conf->device_type, '-');
375                 if (pos == NULL) {
376                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
377                         os_free(wps);
378                         return -1;
379                 }
380                 pos++;
381                 if (hexstr2bin(pos, oui, 4)) {
382                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
383                         os_free(wps);
384                         return -1;
385                 }
386                 wps->dev.oui = WPA_GET_BE32(oui);
387                 pos = os_strchr(pos, '-');
388                 if (pos == NULL) {
389                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
390                         os_free(wps);
391                         return -1;
392                 }
393                 pos++;
394                 wps->dev.sub_categ = atoi(pos);
395         }
396         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
397         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
398         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
399         os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
400
401         wpa_s->wps = wps;
402
403         return 0;
404 }
405
406
407 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
408 {
409         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
410
411         if (wpa_s->wps == NULL)
412                 return;
413
414         os_free(wpa_s->wps->network_key);
415         os_free(wpa_s->wps);
416         wpa_s->wps = NULL;
417 }
418
419
420 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
421 {
422         struct wpabuf *wps_ie;
423
424         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
425                 return -1;
426
427         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
428         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
429                 if (!wps_ie) {
430                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
431                         return 0;
432                 }
433
434                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
435                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
436                                    "without active PBC Registrar");
437                         wpabuf_free(wps_ie);
438                         return 0;
439                 }
440
441                 /* TODO: overlap detection */
442                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
443                            "(Active PBC)");
444                 wpabuf_free(wps_ie);
445                 return 1;
446         }
447
448         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
449                 if (!wps_ie) {
450                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
451                         return 0;
452                 }
453
454                 if (!wps_is_selected_pin_registrar(wps_ie)) {
455                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
456                                    "without active PIN Registrar");
457                         wpabuf_free(wps_ie);
458                         return 0;
459                 }
460                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
461                            "(Active PIN)");
462                 wpabuf_free(wps_ie);
463                 return 1;
464         }
465
466         if (wps_ie) {
467                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
468                 wpabuf_free(wps_ie);
469                 return 1;
470         }
471
472         return -1;
473 }
474
475
476 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
477                               struct wpa_scan_res *bss)
478 {
479         struct wpabuf *wps_ie = NULL;
480         int ret = 0;
481
482         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
483                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
484                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
485                         /* allow wildcard SSID for WPS PBC */
486                         ret = 1;
487                 }
488         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
489                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
490                 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
491                         /* allow wildcard SSID for WPS PIN */
492                         ret = 1;
493                 }
494         }
495
496         wpabuf_free(wps_ie);
497
498         return ret;
499 }
500
501
502 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
503                               struct wpa_scan_res *selected,
504                               struct wpa_ssid *ssid)
505 {
506         const u8 *sel_uuid, *uuid;
507         size_t i;
508         struct wpabuf *wps_ie;
509         int ret = 0;
510
511         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
512                 return 0;
513
514         /* Make sure that only one AP is in active PBC mode */
515         wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
516         if (wps_ie)
517                 sel_uuid = wps_get_uuid_e(wps_ie);
518         else
519                 sel_uuid = NULL;
520         if (!sel_uuid) {
521                 wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for PBC "
522                            "overlap detection");
523                 wpabuf_free(wps_ie);
524                 return 1;
525         }
526
527         for (i = 0; i < wpa_s->scan_res->num; i++) {
528                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
529                 struct wpabuf *ie;
530                 if (bss == selected)
531                         continue;
532                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
533                 if (!ie)
534                         continue;
535                 if (!wps_is_selected_pbc_registrar(ie)) {
536                         wpabuf_free(ie);
537                         continue;
538                 }
539                 uuid = wps_get_uuid_e(ie);
540                 if (uuid == NULL) {
541                         wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for "
542                                    "PBC overlap detection (other BSS)");
543                         ret = 1;
544                         wpabuf_free(ie);
545                         break;
546                 }
547                 if (os_memcmp(sel_uuid, uuid, 16) != 0) {
548                         ret = 1; /* PBC overlap */
549                         wpabuf_free(ie);
550                         break;
551                 }
552
553                 /* TODO: verify that this is reasonable dual-band situation */
554
555                 wpabuf_free(ie);
556         }
557
558         wpabuf_free(wps_ie);
559
560         return ret;
561 }
562
563
564 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
565 {
566         size_t i;
567
568         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
569                 return;
570
571         for (i = 0; i < wpa_s->scan_res->num; i++) {
572                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
573                 struct wpabuf *ie;
574                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
575                 if (!ie)
576                         continue;
577                 if (wps_is_selected_pbc_registrar(ie))
578                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
579                 else if (wps_is_selected_pin_registrar(ie))
580                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
581                 else
582                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
583                 wpabuf_free(ie);
584                 break;
585         }
586 }