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