ap_config.c revision 61d9df3e62aaa0e87ad05452fcb95142159a17b6
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, 0 };
108	const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
109		{ aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
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#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
169	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
349struct 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
373static 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);
429	hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
430	hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
431	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);
445	os_free(conf->radius_das_shared_secret);
446	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
460	os_free(conf->time_zone);
461
462#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);
502	wpabuf_free(conf->wps_nfc_dh_pubkey);
503	wpabuf_free(conf->wps_nfc_dh_privkey);
504	wpabuf_free(conf->wps_nfc_dev_pw);
505#endif /* CONFIG_WPS */
506
507	os_free(conf->roaming_consortium);
508	os_free(conf->venue_name);
509	os_free(conf->nai_realm_data);
510	os_free(conf->network_auth_type);
511	os_free(conf->anqp_3gpp_cell_net);
512	os_free(conf->domain_name);
513
514#ifdef CONFIG_RADIUS_TEST
515	os_free(conf->dump_msk_file);
516#endif /* CONFIG_RADIUS_TEST */
517
518#ifdef CONFIG_HS20
519	os_free(conf->hs20_oper_friendly_name);
520	os_free(conf->hs20_wan_metrics);
521	os_free(conf->hs20_connection_capability);
522	os_free(conf->hs20_operating_class);
523#endif /* CONFIG_HS20 */
524
525	wpabuf_free(conf->vendor_elements);
526}
527
528
529/**
530 * hostapd_config_free - Free hostapd configuration
531 * @conf: Configuration data from hostapd_config_read().
532 */
533void hostapd_config_free(struct hostapd_config *conf)
534{
535	size_t i;
536
537	if (conf == NULL)
538		return;
539
540	for (i = 0; i < conf->num_bss; i++)
541		hostapd_config_free_bss(&conf->bss[i]);
542	os_free(conf->bss);
543	os_free(conf->supported_rates);
544	os_free(conf->basic_rates);
545
546	os_free(conf);
547}
548
549
550/**
551 * hostapd_maclist_found - Find a MAC address from a list
552 * @list: MAC address list
553 * @num_entries: Number of addresses in the list
554 * @addr: Address to search for
555 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
556 * Returns: 1 if address is in the list or 0 if not.
557 *
558 * Perform a binary search for given MAC address from a pre-sorted list.
559 */
560int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
561			  const u8 *addr, int *vlan_id)
562{
563	int start, end, middle, res;
564
565	start = 0;
566	end = num_entries - 1;
567
568	while (start <= end) {
569		middle = (start + end) / 2;
570		res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
571		if (res == 0) {
572			if (vlan_id)
573				*vlan_id = list[middle].vlan_id;
574			return 1;
575		}
576		if (res < 0)
577			start = middle + 1;
578		else
579			end = middle - 1;
580	}
581
582	return 0;
583}
584
585
586int hostapd_rate_found(int *list, int rate)
587{
588	int i;
589
590	if (list == NULL)
591		return 0;
592
593	for (i = 0; list[i] >= 0; i++)
594		if (list[i] == rate)
595			return 1;
596
597	return 0;
598}
599
600
601const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
602{
603	struct hostapd_vlan *v = vlan;
604	while (v) {
605		if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
606			return v->ifname;
607		v = v->next;
608	}
609	return NULL;
610}
611
612
613const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
614			   const u8 *addr, const u8 *prev_psk)
615{
616	struct hostapd_wpa_psk *psk;
617	int next_ok = prev_psk == NULL;
618
619	for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
620		if (next_ok &&
621		    (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
622			return psk->psk;
623
624		if (psk->psk == prev_psk)
625			next_ok = 1;
626	}
627
628	return NULL;
629}
630
631
632const struct hostapd_eap_user *
633hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
634		     size_t identity_len, int phase2)
635{
636	struct hostapd_eap_user *user = conf->eap_user;
637
638#ifdef CONFIG_WPS
639	if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
640	    os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
641		static struct hostapd_eap_user wsc_enrollee;
642		os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
643		wsc_enrollee.methods[0].method = eap_server_get_type(
644			"WSC", &wsc_enrollee.methods[0].vendor);
645		return &wsc_enrollee;
646	}
647
648	if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
649	    os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
650		static struct hostapd_eap_user wsc_registrar;
651		os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
652		wsc_registrar.methods[0].method = eap_server_get_type(
653			"WSC", &wsc_registrar.methods[0].vendor);
654		wsc_registrar.password = (u8 *) conf->ap_pin;
655		wsc_registrar.password_len = conf->ap_pin ?
656			os_strlen(conf->ap_pin) : 0;
657		return &wsc_registrar;
658	}
659#endif /* CONFIG_WPS */
660
661	while (user) {
662		if (!phase2 && user->identity == NULL) {
663			/* Wildcard match */
664			break;
665		}
666
667		if (user->phase2 == !!phase2 && user->wildcard_prefix &&
668		    identity_len >= user->identity_len &&
669		    os_memcmp(user->identity, identity, user->identity_len) ==
670		    0) {
671			/* Wildcard prefix match */
672			break;
673		}
674
675		if (user->phase2 == !!phase2 &&
676		    user->identity_len == identity_len &&
677		    os_memcmp(user->identity, identity, identity_len) == 0)
678			break;
679		user = user->next;
680	}
681
682	return user;
683}
684