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