Move hostapd configuration parser into separate file
[libeap.git] / hostapd / config.c
1 /*
2  * hostapd / Configuration helper functions
3  * Copyright (c) 2003-2009, 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 "crypto/sha1.h"
19 #include "radius/radius_client.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/eapol_common.h"
22 #include "eap_common/eap_wsc_common.h"
23 #include "eap_server/eap.h"
24 #include "wpa.h"
25 #include "sta_info.h"
26 #include "config.h"
27
28
29 extern struct wpa_driver_ops *wpa_drivers[];
30
31
32 static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
33 {
34         struct hostapd_vlan *vlan, *prev;
35
36         vlan = bss->vlan;
37         prev = NULL;
38         while (vlan) {
39                 prev = vlan;
40                 vlan = vlan->next;
41                 os_free(prev);
42         }
43
44         bss->vlan = NULL;
45 }
46
47
48 void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
49 {
50         bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
51         bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
52         bss->logger_syslog = (unsigned int) -1;
53         bss->logger_stdout = (unsigned int) -1;
54
55         bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
56
57         bss->wep_rekeying_period = 300;
58         /* use key0 in individual key and key1 in broadcast key */
59         bss->broadcast_key_idx_min = 1;
60         bss->broadcast_key_idx_max = 2;
61         bss->eap_reauth_period = 3600;
62
63         bss->wpa_group_rekey = 600;
64         bss->wpa_gmk_rekey = 86400;
65         bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
66         bss->wpa_pairwise = WPA_CIPHER_TKIP;
67         bss->wpa_group = WPA_CIPHER_TKIP;
68         bss->rsn_pairwise = 0;
69
70         bss->max_num_sta = MAX_STA_COUNT;
71
72         bss->dtim_period = 2;
73
74         bss->radius_server_auth_port = 1812;
75         bss->ap_max_inactivity = AP_MAX_INACTIVITY;
76         bss->eapol_version = EAPOL_VERSION;
77
78         bss->max_listen_interval = 65535;
79
80 #ifdef CONFIG_IEEE80211W
81         bss->assoc_sa_query_max_timeout = 1000;
82         bss->assoc_sa_query_retry_timeout = 201;
83 #endif /* CONFIG_IEEE80211W */
84 #ifdef EAP_SERVER_FAST
85          /* both anonymous and authenticated provisioning */
86         bss->eap_fast_prov = 3;
87         bss->pac_key_lifetime = 7 * 24 * 60 * 60;
88         bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
89 #endif /* EAP_SERVER_FAST */
90 }
91
92
93 struct hostapd_config * hostapd_config_defaults(void)
94 {
95         struct hostapd_config *conf;
96         struct hostapd_bss_config *bss;
97         int i;
98         const int aCWmin = 15, aCWmax = 1024;
99         const struct hostapd_wmm_ac_params ac_bk =
100                 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
101         const struct hostapd_wmm_ac_params ac_be =
102                 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
103         const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
104                 { aCWmin >> 1, aCWmin, 2, 3000 / 32, 1 };
105         const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
106                 { aCWmin >> 2, aCWmin >> 1, 2, 1500 / 32, 1 };
107
108         conf = os_zalloc(sizeof(*conf));
109         bss = os_zalloc(sizeof(*bss));
110         if (conf == NULL || bss == NULL) {
111                 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
112                            "configuration data.");
113                 os_free(conf);
114                 os_free(bss);
115                 return NULL;
116         }
117
118         /* set default driver based on configuration */
119         conf->driver = wpa_drivers[0];
120         if (conf->driver == NULL) {
121                 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
122                 os_free(conf);
123                 os_free(bss);
124                 return NULL;
125         }
126
127         bss->radius = os_zalloc(sizeof(*bss->radius));
128         if (bss->radius == NULL) {
129                 os_free(conf);
130                 os_free(bss);
131                 return NULL;
132         }
133
134         hostapd_config_defaults_bss(bss);
135
136         conf->num_bss = 1;
137         conf->bss = bss;
138
139         conf->beacon_int = 100;
140         conf->rts_threshold = -1; /* use driver default: 2347 */
141         conf->fragm_threshold = -1; /* user driver default: 2346 */
142         conf->send_probe_response = 1;
143
144         for (i = 0; i < NUM_TX_QUEUES; i++)
145                 conf->tx_queue[i].aifs = -1; /* use hw default */
146
147         conf->wmm_ac_params[0] = ac_be;
148         conf->wmm_ac_params[1] = ac_bk;
149         conf->wmm_ac_params[2] = ac_vi;
150         conf->wmm_ac_params[3] = ac_vo;
151
152         conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
153
154         return conf;
155 }
156
157
158 int hostapd_mac_comp(const void *a, const void *b)
159 {
160         return os_memcmp(a, b, sizeof(macaddr));
161 }
162
163
164 int hostapd_mac_comp_empty(const void *a)
165 {
166         macaddr empty = { 0 };
167         return os_memcmp(a, empty, sizeof(macaddr));
168 }
169
170
171 static int hostapd_config_read_wpa_psk(const char *fname,
172                                        struct hostapd_ssid *ssid)
173 {
174         FILE *f;
175         char buf[128], *pos;
176         int line = 0, ret = 0, len, ok;
177         u8 addr[ETH_ALEN];
178         struct hostapd_wpa_psk *psk;
179
180         if (!fname)
181                 return 0;
182
183         f = fopen(fname, "r");
184         if (!f) {
185                 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
186                 return -1;
187         }
188
189         while (fgets(buf, sizeof(buf), f)) {
190                 line++;
191
192                 if (buf[0] == '#')
193                         continue;
194                 pos = buf;
195                 while (*pos != '\0') {
196                         if (*pos == '\n') {
197                                 *pos = '\0';
198                                 break;
199                         }
200                         pos++;
201                 }
202                 if (buf[0] == '\0')
203                         continue;
204
205                 if (hwaddr_aton(buf, addr)) {
206                         wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
207                                    "line %d in '%s'", buf, line, fname);
208                         ret = -1;
209                         break;
210                 }
211
212                 psk = os_zalloc(sizeof(*psk));
213                 if (psk == NULL) {
214                         wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
215                         ret = -1;
216                         break;
217                 }
218                 if (is_zero_ether_addr(addr))
219                         psk->group = 1;
220                 else
221                         os_memcpy(psk->addr, addr, ETH_ALEN);
222
223                 pos = buf + 17;
224                 if (*pos == '\0') {
225                         wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
226                                    line, fname);
227                         os_free(psk);
228                         ret = -1;
229                         break;
230                 }
231                 pos++;
232
233                 ok = 0;
234                 len = os_strlen(pos);
235                 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
236                         ok = 1;
237                 else if (len >= 8 && len < 64) {
238                         pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
239                                     4096, psk->psk, PMK_LEN);
240                         ok = 1;
241                 }
242                 if (!ok) {
243                         wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
244                                    "'%s'", pos, line, fname);
245                         os_free(psk);
246                         ret = -1;
247                         break;
248                 }
249
250                 psk->next = ssid->wpa_psk;
251                 ssid->wpa_psk = psk;
252         }
253
254         fclose(f);
255
256         return ret;
257 }
258
259
260 static int hostapd_derive_psk(struct hostapd_ssid *ssid)
261 {
262         ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
263         if (ssid->wpa_psk == NULL) {
264                 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
265                 return -1;
266         }
267         wpa_hexdump_ascii(MSG_DEBUG, "SSID",
268                           (u8 *) ssid->ssid, ssid->ssid_len);
269         wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
270                               (u8 *) ssid->wpa_passphrase,
271                               os_strlen(ssid->wpa_passphrase));
272         pbkdf2_sha1(ssid->wpa_passphrase,
273                     ssid->ssid, ssid->ssid_len,
274                     4096, ssid->wpa_psk->psk, PMK_LEN);
275         wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
276                         ssid->wpa_psk->psk, PMK_LEN);
277         return 0;
278 }
279
280
281 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
282 {
283         struct hostapd_ssid *ssid = &conf->ssid;
284
285         if (ssid->wpa_passphrase != NULL) {
286                 if (ssid->wpa_psk != NULL) {
287                         wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
288                                    "instead of passphrase");
289                 } else {
290                         wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
291                                    "passphrase");
292                         if (hostapd_derive_psk(ssid) < 0)
293                                 return -1;
294                 }
295                 ssid->wpa_psk->group = 1;
296         }
297
298         if (ssid->wpa_psk_file) {
299                 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
300                                                 &conf->ssid))
301                         return -1;
302         }
303
304         return 0;
305 }
306
307
308 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
309 {
310         int i;
311
312         if (a->idx != b->idx || a->default_len != b->default_len)
313                 return 1;
314         for (i = 0; i < NUM_WEP_KEYS; i++)
315                 if (a->len[i] != b->len[i] ||
316                     os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
317                         return 1;
318         return 0;
319 }
320
321
322 static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
323                                        int num_servers)
324 {
325         int i;
326
327         for (i = 0; i < num_servers; i++) {
328                 os_free(servers[i].shared_secret);
329         }
330         os_free(servers);
331 }
332
333
334 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
335 {
336         os_free(user->identity);
337         os_free(user->password);
338         os_free(user);
339 }
340
341
342 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
343 {
344         int i;
345         for (i = 0; i < NUM_WEP_KEYS; i++) {
346                 os_free(keys->key[i]);
347                 keys->key[i] = NULL;
348         }
349 }
350
351
352 static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
353 {
354         struct hostapd_wpa_psk *psk, *prev;
355         struct hostapd_eap_user *user, *prev_user;
356
357         if (conf == NULL)
358                 return;
359
360         psk = conf->ssid.wpa_psk;
361         while (psk) {
362                 prev = psk;
363                 psk = psk->next;
364                 os_free(prev);
365         }
366
367         os_free(conf->ssid.wpa_passphrase);
368         os_free(conf->ssid.wpa_psk_file);
369 #ifdef CONFIG_FULL_DYNAMIC_VLAN
370         os_free(conf->ssid.vlan_tagged_interface);
371 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
372
373         user = conf->eap_user;
374         while (user) {
375                 prev_user = user;
376                 user = user->next;
377                 hostapd_config_free_eap_user(prev_user);
378         }
379
380         os_free(conf->dump_log_name);
381         os_free(conf->eap_req_id_text);
382         os_free(conf->accept_mac);
383         os_free(conf->deny_mac);
384         os_free(conf->nas_identifier);
385         hostapd_config_free_radius(conf->radius->auth_servers,
386                                    conf->radius->num_auth_servers);
387         hostapd_config_free_radius(conf->radius->acct_servers,
388                                    conf->radius->num_acct_servers);
389         os_free(conf->rsn_preauth_interfaces);
390         os_free(conf->ctrl_interface);
391         os_free(conf->ca_cert);
392         os_free(conf->server_cert);
393         os_free(conf->private_key);
394         os_free(conf->private_key_passwd);
395         os_free(conf->dh_file);
396         os_free(conf->pac_opaque_encr_key);
397         os_free(conf->eap_fast_a_id);
398         os_free(conf->eap_fast_a_id_info);
399         os_free(conf->eap_sim_db);
400         os_free(conf->radius_server_clients);
401         os_free(conf->test_socket);
402         os_free(conf->radius);
403         hostapd_config_free_vlan(conf);
404         if (conf->ssid.dyn_vlan_keys) {
405                 struct hostapd_ssid *ssid = &conf->ssid;
406                 size_t i;
407                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
408                         if (ssid->dyn_vlan_keys[i] == NULL)
409                                 continue;
410                         hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
411                         os_free(ssid->dyn_vlan_keys[i]);
412                 }
413                 os_free(ssid->dyn_vlan_keys);
414                 ssid->dyn_vlan_keys = NULL;
415         }
416
417 #ifdef CONFIG_IEEE80211R
418         {
419                 struct ft_remote_r0kh *r0kh, *r0kh_prev;
420                 struct ft_remote_r1kh *r1kh, *r1kh_prev;
421
422                 r0kh = conf->r0kh_list;
423                 conf->r0kh_list = NULL;
424                 while (r0kh) {
425                         r0kh_prev = r0kh;
426                         r0kh = r0kh->next;
427                         os_free(r0kh_prev);
428                 }
429
430                 r1kh = conf->r1kh_list;
431                 conf->r1kh_list = NULL;
432                 while (r1kh) {
433                         r1kh_prev = r1kh;
434                         r1kh = r1kh->next;
435                         os_free(r1kh_prev);
436                 }
437         }
438 #endif /* CONFIG_IEEE80211R */
439
440 #ifdef CONFIG_WPS
441         os_free(conf->wps_pin_requests);
442         os_free(conf->device_name);
443         os_free(conf->manufacturer);
444         os_free(conf->model_name);
445         os_free(conf->model_number);
446         os_free(conf->serial_number);
447         os_free(conf->device_type);
448         os_free(conf->config_methods);
449         os_free(conf->ap_pin);
450         os_free(conf->extra_cred);
451         os_free(conf->ap_settings);
452         os_free(conf->upnp_iface);
453         os_free(conf->friendly_name);
454         os_free(conf->manufacturer_url);
455         os_free(conf->model_description);
456         os_free(conf->model_url);
457         os_free(conf->upc);
458 #endif /* CONFIG_WPS */
459 }
460
461
462 /**
463  * hostapd_config_free - Free hostapd configuration
464  * @conf: Configuration data from hostapd_config_read().
465  */
466 void hostapd_config_free(struct hostapd_config *conf)
467 {
468         size_t i;
469
470         if (conf == NULL)
471                 return;
472
473         for (i = 0; i < conf->num_bss; i++)
474                 hostapd_config_free_bss(&conf->bss[i]);
475         os_free(conf->bss);
476         os_free(conf->supported_rates);
477         os_free(conf->basic_rates);
478
479         os_free(conf);
480 }
481
482
483 /**
484  * hostapd_maclist_found - Find a MAC address from a list
485  * @list: MAC address list
486  * @num_entries: Number of addresses in the list
487  * @addr: Address to search for
488  * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
489  * Returns: 1 if address is in the list or 0 if not.
490  *
491  * Perform a binary search for given MAC address from a pre-sorted list.
492  */
493 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
494                           const u8 *addr, int *vlan_id)
495 {
496         int start, end, middle, res;
497
498         start = 0;
499         end = num_entries - 1;
500
501         while (start <= end) {
502                 middle = (start + end) / 2;
503                 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
504                 if (res == 0) {
505                         if (vlan_id)
506                                 *vlan_id = list[middle].vlan_id;
507                         return 1;
508                 }
509                 if (res < 0)
510                         start = middle + 1;
511                 else
512                         end = middle - 1;
513         }
514
515         return 0;
516 }
517
518
519 int hostapd_rate_found(int *list, int rate)
520 {
521         int i;
522
523         if (list == NULL)
524                 return 0;
525
526         for (i = 0; list[i] >= 0; i++)
527                 if (list[i] == rate)
528                         return 1;
529
530         return 0;
531 }
532
533
534 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
535 {
536         struct hostapd_vlan *v = vlan;
537         while (v) {
538                 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
539                         return v->ifname;
540                 v = v->next;
541         }
542         return NULL;
543 }
544
545
546 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
547                            const u8 *addr, const u8 *prev_psk)
548 {
549         struct hostapd_wpa_psk *psk;
550         int next_ok = prev_psk == NULL;
551
552         for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
553                 if (next_ok &&
554                     (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
555                         return psk->psk;
556
557                 if (psk->psk == prev_psk)
558                         next_ok = 1;
559         }
560
561         return NULL;
562 }
563
564
565 const struct hostapd_eap_user *
566 hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
567                      size_t identity_len, int phase2)
568 {
569         struct hostapd_eap_user *user = conf->eap_user;
570
571 #ifdef CONFIG_WPS
572         if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
573             os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
574                 static struct hostapd_eap_user wsc_enrollee;
575                 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
576                 wsc_enrollee.methods[0].method = eap_server_get_type(
577                         "WSC", &wsc_enrollee.methods[0].vendor);
578                 return &wsc_enrollee;
579         }
580
581         if (conf->wps_state && conf->ap_pin &&
582             identity_len == WSC_ID_REGISTRAR_LEN &&
583             os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
584                 static struct hostapd_eap_user wsc_registrar;
585                 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
586                 wsc_registrar.methods[0].method = eap_server_get_type(
587                         "WSC", &wsc_registrar.methods[0].vendor);
588                 wsc_registrar.password = (u8 *) conf->ap_pin;
589                 wsc_registrar.password_len = os_strlen(conf->ap_pin);
590                 return &wsc_registrar;
591         }
592 #endif /* CONFIG_WPS */
593
594         while (user) {
595                 if (!phase2 && user->identity == NULL) {
596                         /* Wildcard match */
597                         break;
598                 }
599
600                 if (user->phase2 == !!phase2 && user->wildcard_prefix &&
601                     identity_len >= user->identity_len &&
602                     os_memcmp(user->identity, identity, user->identity_len) ==
603                     0) {
604                         /* Wildcard prefix match */
605                         break;
606                 }
607
608                 if (user->phase2 == !!phase2 &&
609                     user->identity_len == identity_len &&
610                     os_memcmp(user->identity, identity, identity_len) == 0)
611                         break;
612                 user = user->next;
613         }
614
615         return user;
616 }