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