ap_config.c revision 8da800a193fb6f8832218715f82a7b4e2d2ad338
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	bss->sae_anti_clogging_threshold = 5;
94}
95
96
97struct hostapd_config * hostapd_config_defaults(void)
98{
99#define ecw2cw(ecw) ((1 << (ecw)) - 1)
100
101	struct hostapd_config *conf;
102	struct hostapd_bss_config *bss;
103	const int aCWmin = 4, aCWmax = 10;
104	const struct hostapd_wmm_ac_params ac_bk =
105		{ aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
106	const struct hostapd_wmm_ac_params ac_be =
107		{ aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
108	const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
109		{ aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
110	const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
111		{ aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
112	const struct hostapd_tx_queue_params txq_bk =
113		{ 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
114	const struct hostapd_tx_queue_params txq_be =
115		{ 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
116	const struct hostapd_tx_queue_params txq_vi =
117		{ 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
118	const struct hostapd_tx_queue_params txq_vo =
119		{ 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
120		  (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
121
122#undef ecw2cw
123
124	conf = os_zalloc(sizeof(*conf));
125	bss = os_zalloc(sizeof(*bss));
126	if (conf == NULL || bss == NULL) {
127		wpa_printf(MSG_ERROR, "Failed to allocate memory for "
128			   "configuration data.");
129		os_free(conf);
130		os_free(bss);
131		return NULL;
132	}
133
134	bss->radius = os_zalloc(sizeof(*bss->radius));
135	if (bss->radius == NULL) {
136		os_free(conf);
137		os_free(bss);
138		return NULL;
139	}
140
141	hostapd_config_defaults_bss(bss);
142
143	conf->num_bss = 1;
144	conf->bss = bss;
145
146	conf->beacon_int = 100;
147	conf->rts_threshold = -1; /* use driver default: 2347 */
148	conf->fragm_threshold = -1; /* user driver default: 2346 */
149	conf->send_probe_response = 1;
150
151	conf->wmm_ac_params[0] = ac_be;
152	conf->wmm_ac_params[1] = ac_bk;
153	conf->wmm_ac_params[2] = ac_vi;
154	conf->wmm_ac_params[3] = ac_vo;
155
156	conf->tx_queue[0] = txq_vo;
157	conf->tx_queue[1] = txq_vi;
158	conf->tx_queue[2] = txq_be;
159	conf->tx_queue[3] = txq_bk;
160
161	conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
162
163	conf->ap_table_max_size = 255;
164	conf->ap_table_expiration_time = 60;
165
166#ifdef CONFIG_TESTING_OPTIONS
167	conf->ignore_probe_probability = 0.0d;
168	conf->ignore_auth_probability = 0.0d;
169	conf->ignore_assoc_probability = 0.0d;
170	conf->ignore_reassoc_probability = 0.0d;
171#endif /* CONFIG_TESTING_OPTIONS */
172
173	return conf;
174}
175
176
177int hostapd_mac_comp(const void *a, const void *b)
178{
179	return os_memcmp(a, b, sizeof(macaddr));
180}
181
182
183int hostapd_mac_comp_empty(const void *a)
184{
185	macaddr empty = { 0 };
186	return os_memcmp(a, empty, sizeof(macaddr));
187}
188
189
190static int hostapd_config_read_wpa_psk(const char *fname,
191				       struct hostapd_ssid *ssid)
192{
193	FILE *f;
194	char buf[128], *pos;
195	int line = 0, ret = 0, len, ok;
196	u8 addr[ETH_ALEN];
197	struct hostapd_wpa_psk *psk;
198
199	if (!fname)
200		return 0;
201
202	f = fopen(fname, "r");
203	if (!f) {
204		wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
205		return -1;
206	}
207
208	while (fgets(buf, sizeof(buf), f)) {
209		line++;
210
211		if (buf[0] == '#')
212			continue;
213		pos = buf;
214		while (*pos != '\0') {
215			if (*pos == '\n') {
216				*pos = '\0';
217				break;
218			}
219			pos++;
220		}
221		if (buf[0] == '\0')
222			continue;
223
224		if (hwaddr_aton(buf, addr)) {
225			wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
226				   "line %d in '%s'", buf, line, fname);
227			ret = -1;
228			break;
229		}
230
231		psk = os_zalloc(sizeof(*psk));
232		if (psk == NULL) {
233			wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
234			ret = -1;
235			break;
236		}
237		if (is_zero_ether_addr(addr))
238			psk->group = 1;
239		else
240			os_memcpy(psk->addr, addr, ETH_ALEN);
241
242		pos = buf + 17;
243		if (*pos == '\0') {
244			wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
245				   line, fname);
246			os_free(psk);
247			ret = -1;
248			break;
249		}
250		pos++;
251
252		ok = 0;
253		len = os_strlen(pos);
254		if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
255			ok = 1;
256		else if (len >= 8 && len < 64) {
257			pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
258				    4096, psk->psk, PMK_LEN);
259			ok = 1;
260		}
261		if (!ok) {
262			wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
263				   "'%s'", pos, line, fname);
264			os_free(psk);
265			ret = -1;
266			break;
267		}
268
269		psk->next = ssid->wpa_psk;
270		ssid->wpa_psk = psk;
271	}
272
273	fclose(f);
274
275	return ret;
276}
277
278
279static int hostapd_derive_psk(struct hostapd_ssid *ssid)
280{
281	ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
282	if (ssid->wpa_psk == NULL) {
283		wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
284		return -1;
285	}
286	wpa_hexdump_ascii(MSG_DEBUG, "SSID",
287			  (u8 *) ssid->ssid, ssid->ssid_len);
288	wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
289			      (u8 *) ssid->wpa_passphrase,
290			      os_strlen(ssid->wpa_passphrase));
291	pbkdf2_sha1(ssid->wpa_passphrase,
292		    ssid->ssid, ssid->ssid_len,
293		    4096, ssid->wpa_psk->psk, PMK_LEN);
294	wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
295			ssid->wpa_psk->psk, PMK_LEN);
296	return 0;
297}
298
299
300int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
301{
302	struct hostapd_ssid *ssid = &conf->ssid;
303
304	if (ssid->wpa_passphrase != NULL) {
305		if (ssid->wpa_psk != NULL) {
306			wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
307				   "instead of passphrase");
308		} else {
309			wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
310				   "passphrase");
311			if (hostapd_derive_psk(ssid) < 0)
312				return -1;
313		}
314		ssid->wpa_psk->group = 1;
315	}
316
317	if (ssid->wpa_psk_file) {
318		if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
319						&conf->ssid))
320			return -1;
321	}
322
323	return 0;
324}
325
326
327int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
328{
329	int i;
330
331	if (a->idx != b->idx || a->default_len != b->default_len)
332		return 1;
333	for (i = 0; i < NUM_WEP_KEYS; i++)
334		if (a->len[i] != b->len[i] ||
335		    os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
336			return 1;
337	return 0;
338}
339
340
341static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
342				       int num_servers)
343{
344	int i;
345
346	for (i = 0; i < num_servers; i++) {
347		os_free(servers[i].shared_secret);
348	}
349	os_free(servers);
350}
351
352
353struct hostapd_radius_attr *
354hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
355{
356	for (; attr; attr = attr->next) {
357		if (attr->type == type)
358			return attr;
359	}
360	return NULL;
361}
362
363
364static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
365{
366	struct hostapd_radius_attr *prev;
367
368	while (attr) {
369		prev = attr;
370		attr = attr->next;
371		wpabuf_free(prev->val);
372		os_free(prev);
373	}
374}
375
376
377static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
378{
379	os_free(user->identity);
380	os_free(user->password);
381	os_free(user);
382}
383
384
385static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
386{
387	int i;
388	for (i = 0; i < NUM_WEP_KEYS; i++) {
389		os_free(keys->key[i]);
390		keys->key[i] = NULL;
391	}
392}
393
394
395static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
396{
397	struct hostapd_wpa_psk *psk, *prev;
398	struct hostapd_eap_user *user, *prev_user;
399
400	if (conf == NULL)
401		return;
402
403	psk = conf->ssid.wpa_psk;
404	while (psk) {
405		prev = psk;
406		psk = psk->next;
407		os_free(prev);
408	}
409
410	os_free(conf->ssid.wpa_passphrase);
411	os_free(conf->ssid.wpa_psk_file);
412	hostapd_config_free_wep(&conf->ssid.wep);
413#ifdef CONFIG_FULL_DYNAMIC_VLAN
414	os_free(conf->ssid.vlan_tagged_interface);
415#endif /* CONFIG_FULL_DYNAMIC_VLAN */
416
417	user = conf->eap_user;
418	while (user) {
419		prev_user = user;
420		user = user->next;
421		hostapd_config_free_eap_user(prev_user);
422	}
423	os_free(conf->eap_user_sqlite);
424
425	os_free(conf->dump_log_name);
426	os_free(conf->eap_req_id_text);
427	os_free(conf->accept_mac);
428	os_free(conf->deny_mac);
429	os_free(conf->nas_identifier);
430	hostapd_config_free_radius(conf->radius->auth_servers,
431				   conf->radius->num_auth_servers);
432	hostapd_config_free_radius(conf->radius->acct_servers,
433				   conf->radius->num_acct_servers);
434	hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
435	hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
436	os_free(conf->rsn_preauth_interfaces);
437	os_free(conf->ctrl_interface);
438	os_free(conf->ca_cert);
439	os_free(conf->server_cert);
440	os_free(conf->private_key);
441	os_free(conf->private_key_passwd);
442	os_free(conf->dh_file);
443	os_free(conf->pac_opaque_encr_key);
444	os_free(conf->eap_fast_a_id);
445	os_free(conf->eap_fast_a_id_info);
446	os_free(conf->eap_sim_db);
447	os_free(conf->radius_server_clients);
448	os_free(conf->test_socket);
449	os_free(conf->radius);
450	os_free(conf->radius_das_shared_secret);
451	hostapd_config_free_vlan(conf);
452	if (conf->ssid.dyn_vlan_keys) {
453		struct hostapd_ssid *ssid = &conf->ssid;
454		size_t i;
455		for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
456			if (ssid->dyn_vlan_keys[i] == NULL)
457				continue;
458			hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
459			os_free(ssid->dyn_vlan_keys[i]);
460		}
461		os_free(ssid->dyn_vlan_keys);
462		ssid->dyn_vlan_keys = NULL;
463	}
464
465	os_free(conf->time_zone);
466
467#ifdef CONFIG_IEEE80211R
468	{
469		struct ft_remote_r0kh *r0kh, *r0kh_prev;
470		struct ft_remote_r1kh *r1kh, *r1kh_prev;
471
472		r0kh = conf->r0kh_list;
473		conf->r0kh_list = NULL;
474		while (r0kh) {
475			r0kh_prev = r0kh;
476			r0kh = r0kh->next;
477			os_free(r0kh_prev);
478		}
479
480		r1kh = conf->r1kh_list;
481		conf->r1kh_list = NULL;
482		while (r1kh) {
483			r1kh_prev = r1kh;
484			r1kh = r1kh->next;
485			os_free(r1kh_prev);
486		}
487	}
488#endif /* CONFIG_IEEE80211R */
489
490#ifdef CONFIG_WPS
491	os_free(conf->wps_pin_requests);
492	os_free(conf->device_name);
493	os_free(conf->manufacturer);
494	os_free(conf->model_name);
495	os_free(conf->model_number);
496	os_free(conf->serial_number);
497	os_free(conf->config_methods);
498	os_free(conf->ap_pin);
499	os_free(conf->extra_cred);
500	os_free(conf->ap_settings);
501	os_free(conf->upnp_iface);
502	os_free(conf->friendly_name);
503	os_free(conf->manufacturer_url);
504	os_free(conf->model_description);
505	os_free(conf->model_url);
506	os_free(conf->upc);
507	wpabuf_free(conf->wps_nfc_dh_pubkey);
508	wpabuf_free(conf->wps_nfc_dh_privkey);
509	wpabuf_free(conf->wps_nfc_dev_pw);
510#endif /* CONFIG_WPS */
511
512	os_free(conf->roaming_consortium);
513	os_free(conf->venue_name);
514	os_free(conf->nai_realm_data);
515	os_free(conf->network_auth_type);
516	os_free(conf->anqp_3gpp_cell_net);
517	os_free(conf->domain_name);
518
519#ifdef CONFIG_RADIUS_TEST
520	os_free(conf->dump_msk_file);
521#endif /* CONFIG_RADIUS_TEST */
522
523#ifdef CONFIG_HS20
524	os_free(conf->hs20_oper_friendly_name);
525	os_free(conf->hs20_wan_metrics);
526	os_free(conf->hs20_connection_capability);
527	os_free(conf->hs20_operating_class);
528#endif /* CONFIG_HS20 */
529
530	wpabuf_free(conf->vendor_elements);
531
532	os_free(conf->sae_groups);
533}
534
535
536/**
537 * hostapd_config_free - Free hostapd configuration
538 * @conf: Configuration data from hostapd_config_read().
539 */
540void hostapd_config_free(struct hostapd_config *conf)
541{
542	size_t i;
543
544	if (conf == NULL)
545		return;
546
547	for (i = 0; i < conf->num_bss; i++)
548		hostapd_config_free_bss(&conf->bss[i]);
549	os_free(conf->bss);
550	os_free(conf->supported_rates);
551	os_free(conf->basic_rates);
552
553	os_free(conf);
554}
555
556
557/**
558 * hostapd_maclist_found - Find a MAC address from a list
559 * @list: MAC address list
560 * @num_entries: Number of addresses in the list
561 * @addr: Address to search for
562 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
563 * Returns: 1 if address is in the list or 0 if not.
564 *
565 * Perform a binary search for given MAC address from a pre-sorted list.
566 */
567int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
568			  const u8 *addr, int *vlan_id)
569{
570	int start, end, middle, res;
571
572	start = 0;
573	end = num_entries - 1;
574
575	while (start <= end) {
576		middle = (start + end) / 2;
577		res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
578		if (res == 0) {
579			if (vlan_id)
580				*vlan_id = list[middle].vlan_id;
581			return 1;
582		}
583		if (res < 0)
584			start = middle + 1;
585		else
586			end = middle - 1;
587	}
588
589	return 0;
590}
591
592
593int hostapd_rate_found(int *list, int rate)
594{
595	int i;
596
597	if (list == NULL)
598		return 0;
599
600	for (i = 0; list[i] >= 0; i++)
601		if (list[i] == rate)
602			return 1;
603
604	return 0;
605}
606
607
608const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
609{
610	struct hostapd_vlan *v = vlan;
611	while (v) {
612		if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
613			return v->ifname;
614		v = v->next;
615	}
616	return NULL;
617}
618
619
620const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
621			   const u8 *addr, const u8 *prev_psk)
622{
623	struct hostapd_wpa_psk *psk;
624	int next_ok = prev_psk == NULL;
625
626	for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
627		if (next_ok &&
628		    (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
629			return psk->psk;
630
631		if (psk->psk == prev_psk)
632			next_ok = 1;
633	}
634
635	return NULL;
636}
637