ap_config.c revision b5e8f06e18446918f6d801566e5709a8c87f1780
1/*
2 * hostapd / Configuration helper functions
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
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 */
90
91	bss->radius_das_time_window = 300;
92}
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
161	return conf;
162}
163
164
165int hostapd_mac_comp(const void *a, const void *b)
166{
167	return os_memcmp(a, b, sizeof(macaddr));
168}
169
170
171int hostapd_mac_comp_empty(const void *a)
172{
173	macaddr empty = { 0 };
174	return os_memcmp(a, empty, sizeof(macaddr));
175}
176
177
178static int hostapd_config_read_wpa_psk(const char *fname,
179				       struct hostapd_ssid *ssid)
180{
181	FILE *f;
182	char buf[128], *pos;
183	int line = 0, ret = 0, len, ok;
184	u8 addr[ETH_ALEN];
185	struct hostapd_wpa_psk *psk;
186
187	if (!fname)
188		return 0;
189
190	f = fopen(fname, "r");
191	if (!f) {
192		wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
193		return -1;
194	}
195
196	while (fgets(buf, sizeof(buf), f)) {
197		line++;
198
199		if (buf[0] == '#')
200			continue;
201		pos = buf;
202		while (*pos != '\0') {
203			if (*pos == '\n') {
204				*pos = '\0';
205				break;
206			}
207			pos++;
208		}
209		if (buf[0] == '\0')
210			continue;
211
212		if (hwaddr_aton(buf, addr)) {
213			wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
214				   "line %d in '%s'", buf, line, fname);
215			ret = -1;
216			break;
217		}
218
219		psk = os_zalloc(sizeof(*psk));
220		if (psk == NULL) {
221			wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
222			ret = -1;
223			break;
224		}
225		if (is_zero_ether_addr(addr))
226			psk->group = 1;
227		else
228			os_memcpy(psk->addr, addr, ETH_ALEN);
229
230		pos = buf + 17;
231		if (*pos == '\0') {
232			wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
233				   line, fname);
234			os_free(psk);
235			ret = -1;
236			break;
237		}
238		pos++;
239
240		ok = 0;
241		len = os_strlen(pos);
242		if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
243			ok = 1;
244		else if (len >= 8 && len < 64) {
245			pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
246				    4096, psk->psk, PMK_LEN);
247			ok = 1;
248		}
249		if (!ok) {
250			wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
251				   "'%s'", pos, line, fname);
252			os_free(psk);
253			ret = -1;
254			break;
255		}
256
257		psk->next = ssid->wpa_psk;
258		ssid->wpa_psk = psk;
259	}
260
261	fclose(f);
262
263	return ret;
264}
265
266
267static int hostapd_derive_psk(struct hostapd_ssid *ssid)
268{
269	ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
270	if (ssid->wpa_psk == NULL) {
271		wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
272		return -1;
273	}
274	wpa_hexdump_ascii(MSG_DEBUG, "SSID",
275			  (u8 *) ssid->ssid, ssid->ssid_len);
276	wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
277			      (u8 *) ssid->wpa_passphrase,
278			      os_strlen(ssid->wpa_passphrase));
279	pbkdf2_sha1(ssid->wpa_passphrase,
280		    ssid->ssid, ssid->ssid_len,
281		    4096, ssid->wpa_psk->psk, PMK_LEN);
282	wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
283			ssid->wpa_psk->psk, PMK_LEN);
284	return 0;
285}
286
287
288int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
289{
290	struct hostapd_ssid *ssid = &conf->ssid;
291
292	if (ssid->wpa_passphrase != NULL) {
293		if (ssid->wpa_psk != NULL) {
294			wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
295				   "instead of passphrase");
296		} else {
297			wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
298				   "passphrase");
299			if (hostapd_derive_psk(ssid) < 0)
300				return -1;
301		}
302		ssid->wpa_psk->group = 1;
303	}
304
305	if (ssid->wpa_psk_file) {
306		if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
307						&conf->ssid))
308			return -1;
309	}
310
311	return 0;
312}
313
314
315int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
316{
317	int i;
318
319	if (a->idx != b->idx || a->default_len != b->default_len)
320		return 1;
321	for (i = 0; i < NUM_WEP_KEYS; i++)
322		if (a->len[i] != b->len[i] ||
323		    os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
324			return 1;
325	return 0;
326}
327
328
329static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
330				       int num_servers)
331{
332	int i;
333
334	for (i = 0; i < num_servers; i++) {
335		os_free(servers[i].shared_secret);
336	}
337	os_free(servers);
338}
339
340
341struct hostapd_radius_attr *
342hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
343{
344	for (; attr; attr = attr->next) {
345		if (attr->type == type)
346			return attr;
347	}
348	return NULL;
349}
350
351
352static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
353{
354	struct hostapd_radius_attr *prev;
355
356	while (attr) {
357		prev = attr;
358		attr = attr->next;
359		wpabuf_free(prev->val);
360		os_free(prev);
361	}
362}
363
364
365static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
366{
367	os_free(user->identity);
368	os_free(user->password);
369	os_free(user);
370}
371
372
373static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
374{
375	int i;
376	for (i = 0; i < NUM_WEP_KEYS; i++) {
377		os_free(keys->key[i]);
378		keys->key[i] = NULL;
379	}
380}
381
382
383static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
384{
385	struct hostapd_wpa_psk *psk, *prev;
386	struct hostapd_eap_user *user, *prev_user;
387
388	if (conf == NULL)
389		return;
390
391	psk = conf->ssid.wpa_psk;
392	while (psk) {
393		prev = psk;
394		psk = psk->next;
395		os_free(prev);
396	}
397
398	os_free(conf->ssid.wpa_passphrase);
399	os_free(conf->ssid.wpa_psk_file);
400	hostapd_config_free_wep(&conf->ssid.wep);
401#ifdef CONFIG_FULL_DYNAMIC_VLAN
402	os_free(conf->ssid.vlan_tagged_interface);
403#endif /* CONFIG_FULL_DYNAMIC_VLAN */
404
405	user = conf->eap_user;
406	while (user) {
407		prev_user = user;
408		user = user->next;
409		hostapd_config_free_eap_user(prev_user);
410	}
411
412	os_free(conf->dump_log_name);
413	os_free(conf->eap_req_id_text);
414	os_free(conf->accept_mac);
415	os_free(conf->deny_mac);
416	os_free(conf->nas_identifier);
417	hostapd_config_free_radius(conf->radius->auth_servers,
418				   conf->radius->num_auth_servers);
419	hostapd_config_free_radius(conf->radius->acct_servers,
420				   conf->radius->num_acct_servers);
421	hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
422	hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
423	os_free(conf->rsn_preauth_interfaces);
424	os_free(conf->ctrl_interface);
425	os_free(conf->ca_cert);
426	os_free(conf->server_cert);
427	os_free(conf->private_key);
428	os_free(conf->private_key_passwd);
429	os_free(conf->dh_file);
430	os_free(conf->pac_opaque_encr_key);
431	os_free(conf->eap_fast_a_id);
432	os_free(conf->eap_fast_a_id_info);
433	os_free(conf->eap_sim_db);
434	os_free(conf->radius_server_clients);
435	os_free(conf->test_socket);
436	os_free(conf->radius);
437	os_free(conf->radius_das_shared_secret);
438	hostapd_config_free_vlan(conf);
439	if (conf->ssid.dyn_vlan_keys) {
440		struct hostapd_ssid *ssid = &conf->ssid;
441		size_t i;
442		for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
443			if (ssid->dyn_vlan_keys[i] == NULL)
444				continue;
445			hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
446			os_free(ssid->dyn_vlan_keys[i]);
447		}
448		os_free(ssid->dyn_vlan_keys);
449		ssid->dyn_vlan_keys = NULL;
450	}
451
452	os_free(conf->time_zone);
453
454#ifdef CONFIG_IEEE80211R
455	{
456		struct ft_remote_r0kh *r0kh, *r0kh_prev;
457		struct ft_remote_r1kh *r1kh, *r1kh_prev;
458
459		r0kh = conf->r0kh_list;
460		conf->r0kh_list = NULL;
461		while (r0kh) {
462			r0kh_prev = r0kh;
463			r0kh = r0kh->next;
464			os_free(r0kh_prev);
465		}
466
467		r1kh = conf->r1kh_list;
468		conf->r1kh_list = NULL;
469		while (r1kh) {
470			r1kh_prev = r1kh;
471			r1kh = r1kh->next;
472			os_free(r1kh_prev);
473		}
474	}
475#endif /* CONFIG_IEEE80211R */
476
477#ifdef CONFIG_WPS
478	os_free(conf->wps_pin_requests);
479	os_free(conf->device_name);
480	os_free(conf->manufacturer);
481	os_free(conf->model_name);
482	os_free(conf->model_number);
483	os_free(conf->serial_number);
484	os_free(conf->config_methods);
485	os_free(conf->ap_pin);
486	os_free(conf->extra_cred);
487	os_free(conf->ap_settings);
488	os_free(conf->upnp_iface);
489	os_free(conf->friendly_name);
490	os_free(conf->manufacturer_url);
491	os_free(conf->model_description);
492	os_free(conf->model_url);
493	os_free(conf->upc);
494	wpabuf_free(conf->wps_nfc_dh_pubkey);
495	wpabuf_free(conf->wps_nfc_dh_privkey);
496	wpabuf_free(conf->wps_nfc_dev_pw);
497#endif /* CONFIG_WPS */
498
499	os_free(conf->roaming_consortium);
500	os_free(conf->venue_name);
501
502#ifdef CONFIG_RADIUS_TEST
503	os_free(conf->dump_msk_file);
504#endif /* CONFIG_RADIUS_TEST */
505}
506
507
508/**
509 * hostapd_config_free - Free hostapd configuration
510 * @conf: Configuration data from hostapd_config_read().
511 */
512void hostapd_config_free(struct hostapd_config *conf)
513{
514	size_t i;
515
516	if (conf == NULL)
517		return;
518
519	for (i = 0; i < conf->num_bss; i++)
520		hostapd_config_free_bss(&conf->bss[i]);
521	os_free(conf->bss);
522	os_free(conf->supported_rates);
523	os_free(conf->basic_rates);
524
525	os_free(conf);
526}
527
528
529/**
530 * hostapd_maclist_found - Find a MAC address from a list
531 * @list: MAC address list
532 * @num_entries: Number of addresses in the list
533 * @addr: Address to search for
534 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
535 * Returns: 1 if address is in the list or 0 if not.
536 *
537 * Perform a binary search for given MAC address from a pre-sorted list.
538 */
539int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
540			  const u8 *addr, int *vlan_id)
541{
542	int start, end, middle, res;
543
544	start = 0;
545	end = num_entries - 1;
546
547	while (start <= end) {
548		middle = (start + end) / 2;
549		res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
550		if (res == 0) {
551			if (vlan_id)
552				*vlan_id = list[middle].vlan_id;
553			return 1;
554		}
555		if (res < 0)
556			start = middle + 1;
557		else
558			end = middle - 1;
559	}
560
561	return 0;
562}
563
564
565int hostapd_rate_found(int *list, int rate)
566{
567	int i;
568
569	if (list == NULL)
570		return 0;
571
572	for (i = 0; list[i] >= 0; i++)
573		if (list[i] == rate)
574			return 1;
575
576	return 0;
577}
578
579
580const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
581{
582	struct hostapd_vlan *v = vlan;
583	while (v) {
584		if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
585			return v->ifname;
586		v = v->next;
587	}
588	return NULL;
589}
590
591
592const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
593			   const u8 *addr, const u8 *prev_psk)
594{
595	struct hostapd_wpa_psk *psk;
596	int next_ok = prev_psk == NULL;
597
598	for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
599		if (next_ok &&
600		    (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
601			return psk->psk;
602
603		if (psk->psk == prev_psk)
604			next_ok = 1;
605	}
606
607	return NULL;
608}
609
610
611const struct hostapd_eap_user *
612hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
613		     size_t identity_len, int phase2)
614{
615	struct hostapd_eap_user *user = conf->eap_user;
616
617#ifdef CONFIG_WPS
618	if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
619	    os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
620		static struct hostapd_eap_user wsc_enrollee;
621		os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
622		wsc_enrollee.methods[0].method = eap_server_get_type(
623			"WSC", &wsc_enrollee.methods[0].vendor);
624		return &wsc_enrollee;
625	}
626
627	if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
628	    os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
629		static struct hostapd_eap_user wsc_registrar;
630		os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
631		wsc_registrar.methods[0].method = eap_server_get_type(
632			"WSC", &wsc_registrar.methods[0].vendor);
633		wsc_registrar.password = (u8 *) conf->ap_pin;
634		wsc_registrar.password_len = conf->ap_pin ?
635			os_strlen(conf->ap_pin) : 0;
636		return &wsc_registrar;
637	}
638#endif /* CONFIG_WPS */
639
640	while (user) {
641		if (!phase2 && user->identity == NULL) {
642			/* Wildcard match */
643			break;
644		}
645
646		if (user->phase2 == !!phase2 && user->wildcard_prefix &&
647		    identity_len >= user->identity_len &&
648		    os_memcmp(user->identity, identity, user->identity_len) ==
649		    0) {
650			/* Wildcard prefix match */
651			break;
652		}
653
654		if (user->phase2 == !!phase2 &&
655		    user->identity_len == identity_len &&
656		    os_memcmp(user->identity, identity, identity_len) == 0)
657			break;
658		user = user->next;
659	}
660
661	return user;
662}
663