ap_config.c revision cce06667447b5aec83452adb0c15100ada531095
1/*
2 * hostapd / Configuration helper functions
3 * Copyright (c) 2003-2013, 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	conf->bss = os_calloc(1, sizeof(struct hostapd_bss_config *));
134	if (conf->bss == NULL) {
135		os_free(conf);
136		os_free(bss);
137		return NULL;
138	}
139	conf->bss[0] = bss;
140
141	bss->radius = os_zalloc(sizeof(*bss->radius));
142	if (bss->radius == NULL) {
143		os_free(conf);
144		os_free(bss);
145		return NULL;
146	}
147
148	hostapd_config_defaults_bss(bss);
149
150	conf->num_bss = 1;
151
152	conf->beacon_int = 100;
153	conf->rts_threshold = -1; /* use driver default: 2347 */
154	conf->fragm_threshold = -1; /* user driver default: 2346 */
155	conf->send_probe_response = 1;
156
157	conf->wmm_ac_params[0] = ac_be;
158	conf->wmm_ac_params[1] = ac_bk;
159	conf->wmm_ac_params[2] = ac_vi;
160	conf->wmm_ac_params[3] = ac_vo;
161
162	conf->tx_queue[0] = txq_vo;
163	conf->tx_queue[1] = txq_vi;
164	conf->tx_queue[2] = txq_be;
165	conf->tx_queue[3] = txq_bk;
166
167	conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
168
169	conf->ap_table_max_size = 255;
170	conf->ap_table_expiration_time = 60;
171
172#ifdef CONFIG_TESTING_OPTIONS
173	conf->ignore_probe_probability = 0.0d;
174	conf->ignore_auth_probability = 0.0d;
175	conf->ignore_assoc_probability = 0.0d;
176	conf->ignore_reassoc_probability = 0.0d;
177	conf->corrupt_gtk_rekey_mic_probability = 0.0d;
178#endif /* CONFIG_TESTING_OPTIONS */
179
180#ifdef CONFIG_ACS
181	conf->acs_num_scans = 5;
182#endif /* CONFIG_ACS */
183
184	return conf;
185}
186
187
188int hostapd_mac_comp(const void *a, const void *b)
189{
190	return os_memcmp(a, b, sizeof(macaddr));
191}
192
193
194int hostapd_mac_comp_empty(const void *a)
195{
196	macaddr empty = { 0 };
197	return os_memcmp(a, empty, sizeof(macaddr));
198}
199
200
201static int hostapd_config_read_wpa_psk(const char *fname,
202				       struct hostapd_ssid *ssid)
203{
204	FILE *f;
205	char buf[128], *pos;
206	int line = 0, ret = 0, len, ok;
207	u8 addr[ETH_ALEN];
208	struct hostapd_wpa_psk *psk;
209
210	if (!fname)
211		return 0;
212
213	f = fopen(fname, "r");
214	if (!f) {
215		wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
216		return -1;
217	}
218
219	while (fgets(buf, sizeof(buf), f)) {
220		line++;
221
222		if (buf[0] == '#')
223			continue;
224		pos = buf;
225		while (*pos != '\0') {
226			if (*pos == '\n') {
227				*pos = '\0';
228				break;
229			}
230			pos++;
231		}
232		if (buf[0] == '\0')
233			continue;
234
235		if (hwaddr_aton(buf, addr)) {
236			wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
237				   "line %d in '%s'", buf, line, fname);
238			ret = -1;
239			break;
240		}
241
242		psk = os_zalloc(sizeof(*psk));
243		if (psk == NULL) {
244			wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
245			ret = -1;
246			break;
247		}
248		if (is_zero_ether_addr(addr))
249			psk->group = 1;
250		else
251			os_memcpy(psk->addr, addr, ETH_ALEN);
252
253		pos = buf + 17;
254		if (*pos == '\0') {
255			wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
256				   line, fname);
257			os_free(psk);
258			ret = -1;
259			break;
260		}
261		pos++;
262
263		ok = 0;
264		len = os_strlen(pos);
265		if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
266			ok = 1;
267		else if (len >= 8 && len < 64) {
268			pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
269				    4096, psk->psk, PMK_LEN);
270			ok = 1;
271		}
272		if (!ok) {
273			wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
274				   "'%s'", pos, line, fname);
275			os_free(psk);
276			ret = -1;
277			break;
278		}
279
280		psk->next = ssid->wpa_psk;
281		ssid->wpa_psk = psk;
282	}
283
284	fclose(f);
285
286	return ret;
287}
288
289
290static int hostapd_derive_psk(struct hostapd_ssid *ssid)
291{
292	ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
293	if (ssid->wpa_psk == NULL) {
294		wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
295		return -1;
296	}
297	wpa_hexdump_ascii(MSG_DEBUG, "SSID",
298			  (u8 *) ssid->ssid, ssid->ssid_len);
299	wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
300			      (u8 *) ssid->wpa_passphrase,
301			      os_strlen(ssid->wpa_passphrase));
302	pbkdf2_sha1(ssid->wpa_passphrase,
303		    ssid->ssid, ssid->ssid_len,
304		    4096, ssid->wpa_psk->psk, PMK_LEN);
305	wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
306			ssid->wpa_psk->psk, PMK_LEN);
307	return 0;
308}
309
310
311int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
312{
313	struct hostapd_ssid *ssid = &conf->ssid;
314
315	if (ssid->wpa_passphrase != NULL) {
316		if (ssid->wpa_psk != NULL) {
317			wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
318				   "instead of passphrase");
319		} else {
320			wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
321				   "passphrase");
322			if (hostapd_derive_psk(ssid) < 0)
323				return -1;
324		}
325		ssid->wpa_psk->group = 1;
326	}
327
328	if (ssid->wpa_psk_file) {
329		if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
330						&conf->ssid))
331			return -1;
332	}
333
334	return 0;
335}
336
337
338int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
339{
340	int i;
341
342	if (a->idx != b->idx || a->default_len != b->default_len)
343		return 1;
344	for (i = 0; i < NUM_WEP_KEYS; i++)
345		if (a->len[i] != b->len[i] ||
346		    os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
347			return 1;
348	return 0;
349}
350
351
352static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
353				       int num_servers)
354{
355	int i;
356
357	for (i = 0; i < num_servers; i++) {
358		os_free(servers[i].shared_secret);
359	}
360	os_free(servers);
361}
362
363
364struct hostapd_radius_attr *
365hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
366{
367	for (; attr; attr = attr->next) {
368		if (attr->type == type)
369			return attr;
370	}
371	return NULL;
372}
373
374
375static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
376{
377	struct hostapd_radius_attr *prev;
378
379	while (attr) {
380		prev = attr;
381		attr = attr->next;
382		wpabuf_free(prev->val);
383		os_free(prev);
384	}
385}
386
387
388static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
389{
390	os_free(user->identity);
391	os_free(user->password);
392	os_free(user);
393}
394
395
396static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
397{
398	int i;
399	for (i = 0; i < NUM_WEP_KEYS; i++) {
400		os_free(keys->key[i]);
401		keys->key[i] = NULL;
402	}
403}
404
405
406void hostapd_config_free_bss(struct hostapd_bss_config *conf)
407{
408	struct hostapd_wpa_psk *psk, *prev;
409	struct hostapd_eap_user *user, *prev_user;
410
411	if (conf == NULL)
412		return;
413
414	psk = conf->ssid.wpa_psk;
415	while (psk) {
416		prev = psk;
417		psk = psk->next;
418		os_free(prev);
419	}
420
421	os_free(conf->ssid.wpa_passphrase);
422	os_free(conf->ssid.wpa_psk_file);
423	hostapd_config_free_wep(&conf->ssid.wep);
424#ifdef CONFIG_FULL_DYNAMIC_VLAN
425	os_free(conf->ssid.vlan_tagged_interface);
426#endif /* CONFIG_FULL_DYNAMIC_VLAN */
427
428	user = conf->eap_user;
429	while (user) {
430		prev_user = user;
431		user = user->next;
432		hostapd_config_free_eap_user(prev_user);
433	}
434	os_free(conf->eap_user_sqlite);
435
436	os_free(conf->dump_log_name);
437	os_free(conf->eap_req_id_text);
438	os_free(conf->accept_mac);
439	os_free(conf->deny_mac);
440	os_free(conf->nas_identifier);
441	if (conf->radius) {
442		hostapd_config_free_radius(conf->radius->auth_servers,
443					   conf->radius->num_auth_servers);
444		hostapd_config_free_radius(conf->radius->acct_servers,
445					   conf->radius->num_acct_servers);
446	}
447	hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
448	hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
449	os_free(conf->rsn_preauth_interfaces);
450	os_free(conf->ctrl_interface);
451	os_free(conf->ca_cert);
452	os_free(conf->server_cert);
453	os_free(conf->private_key);
454	os_free(conf->private_key_passwd);
455	os_free(conf->ocsp_stapling_response);
456	os_free(conf->dh_file);
457	os_free(conf->pac_opaque_encr_key);
458	os_free(conf->eap_fast_a_id);
459	os_free(conf->eap_fast_a_id_info);
460	os_free(conf->eap_sim_db);
461	os_free(conf->radius_server_clients);
462	os_free(conf->test_socket);
463	os_free(conf->radius);
464	os_free(conf->radius_das_shared_secret);
465	hostapd_config_free_vlan(conf);
466	os_free(conf->time_zone);
467
468#ifdef CONFIG_IEEE80211R
469	{
470		struct ft_remote_r0kh *r0kh, *r0kh_prev;
471		struct ft_remote_r1kh *r1kh, *r1kh_prev;
472
473		r0kh = conf->r0kh_list;
474		conf->r0kh_list = NULL;
475		while (r0kh) {
476			r0kh_prev = r0kh;
477			r0kh = r0kh->next;
478			os_free(r0kh_prev);
479		}
480
481		r1kh = conf->r1kh_list;
482		conf->r1kh_list = NULL;
483		while (r1kh) {
484			r1kh_prev = r1kh;
485			r1kh = r1kh->next;
486			os_free(r1kh_prev);
487		}
488	}
489#endif /* CONFIG_IEEE80211R */
490
491#ifdef CONFIG_WPS
492	os_free(conf->wps_pin_requests);
493	os_free(conf->device_name);
494	os_free(conf->manufacturer);
495	os_free(conf->model_name);
496	os_free(conf->model_number);
497	os_free(conf->serial_number);
498	os_free(conf->config_methods);
499	os_free(conf->ap_pin);
500	os_free(conf->extra_cred);
501	os_free(conf->ap_settings);
502	os_free(conf->upnp_iface);
503	os_free(conf->friendly_name);
504	os_free(conf->manufacturer_url);
505	os_free(conf->model_description);
506	os_free(conf->model_url);
507	os_free(conf->upc);
508	wpabuf_free(conf->wps_nfc_dh_pubkey);
509	wpabuf_free(conf->wps_nfc_dh_privkey);
510	wpabuf_free(conf->wps_nfc_dev_pw);
511#endif /* CONFIG_WPS */
512
513	os_free(conf->roaming_consortium);
514	os_free(conf->venue_name);
515	os_free(conf->nai_realm_data);
516	os_free(conf->network_auth_type);
517	os_free(conf->anqp_3gpp_cell_net);
518	os_free(conf->domain_name);
519
520#ifdef CONFIG_RADIUS_TEST
521	os_free(conf->dump_msk_file);
522#endif /* CONFIG_RADIUS_TEST */
523
524#ifdef CONFIG_HS20
525	os_free(conf->hs20_oper_friendly_name);
526	os_free(conf->hs20_wan_metrics);
527	os_free(conf->hs20_connection_capability);
528	os_free(conf->hs20_operating_class);
529#endif /* CONFIG_HS20 */
530
531	wpabuf_free(conf->vendor_elements);
532
533	os_free(conf->sae_groups);
534
535	os_free(conf->server_id);
536
537	os_free(conf);
538}
539
540
541/**
542 * hostapd_config_free - Free hostapd configuration
543 * @conf: Configuration data from hostapd_config_read().
544 */
545void hostapd_config_free(struct hostapd_config *conf)
546{
547	size_t i;
548
549	if (conf == NULL)
550		return;
551
552	for (i = 0; i < conf->num_bss; i++)
553		hostapd_config_free_bss(conf->bss[i]);
554	os_free(conf->bss);
555	os_free(conf->supported_rates);
556	os_free(conf->basic_rates);
557
558	os_free(conf);
559}
560
561
562/**
563 * hostapd_maclist_found - Find a MAC address from a list
564 * @list: MAC address list
565 * @num_entries: Number of addresses in the list
566 * @addr: Address to search for
567 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
568 * Returns: 1 if address is in the list or 0 if not.
569 *
570 * Perform a binary search for given MAC address from a pre-sorted list.
571 */
572int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
573			  const u8 *addr, int *vlan_id)
574{
575	int start, end, middle, res;
576
577	start = 0;
578	end = num_entries - 1;
579
580	while (start <= end) {
581		middle = (start + end) / 2;
582		res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
583		if (res == 0) {
584			if (vlan_id)
585				*vlan_id = list[middle].vlan_id;
586			return 1;
587		}
588		if (res < 0)
589			start = middle + 1;
590		else
591			end = middle - 1;
592	}
593
594	return 0;
595}
596
597
598int hostapd_rate_found(int *list, int rate)
599{
600	int i;
601
602	if (list == NULL)
603		return 0;
604
605	for (i = 0; list[i] >= 0; i++)
606		if (list[i] == rate)
607			return 1;
608
609	return 0;
610}
611
612
613int hostapd_vlan_id_valid(struct hostapd_vlan *vlan, int vlan_id)
614{
615	struct hostapd_vlan *v = vlan;
616	while (v) {
617		if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
618			return 1;
619		v = v->next;
620	}
621	return 0;
622}
623
624
625const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
626{
627	struct hostapd_vlan *v = vlan;
628	while (v) {
629		if (v->vlan_id == vlan_id)
630			return v->ifname;
631		v = v->next;
632	}
633	return NULL;
634}
635
636
637const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
638			   const u8 *addr, const u8 *p2p_dev_addr,
639			   const u8 *prev_psk)
640{
641	struct hostapd_wpa_psk *psk;
642	int next_ok = prev_psk == NULL;
643
644	if (p2p_dev_addr) {
645		wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
646			   " p2p_dev_addr=" MACSTR " prev_psk=%p",
647			   MAC2STR(addr), MAC2STR(p2p_dev_addr), prev_psk);
648		if (!is_zero_ether_addr(p2p_dev_addr))
649			addr = NULL; /* Use P2P Device Address for matching */
650	} else {
651		wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
652			   " prev_psk=%p",
653			   MAC2STR(addr), prev_psk);
654	}
655
656	for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
657		if (next_ok &&
658		    (psk->group ||
659		     (addr && os_memcmp(psk->addr, addr, ETH_ALEN) == 0) ||
660		     (!addr && p2p_dev_addr &&
661		      os_memcmp(psk->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
662		      0)))
663			return psk->psk;
664
665		if (psk->psk == prev_psk)
666			next_ok = 1;
667	}
668
669	return NULL;
670}
671
672
673static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
674				    struct hostapd_config *conf)
675{
676	if (bss->ieee802_1x && !bss->eap_server &&
677	    !bss->radius->auth_servers) {
678		wpa_printf(MSG_ERROR, "Invalid IEEE 802.1X configuration (no "
679			   "EAP authenticator configured).");
680		return -1;
681	}
682
683	if (bss->wpa) {
684		int wep, i;
685
686		wep = bss->default_wep_key_len > 0 ||
687		       bss->individual_wep_key_len > 0;
688		for (i = 0; i < NUM_WEP_KEYS; i++) {
689			if (bss->ssid.wep.keys_set) {
690				wep = 1;
691				break;
692			}
693		}
694
695		if (wep) {
696			wpa_printf(MSG_ERROR, "WEP configuration in a WPA network is not supported");
697			return -1;
698		}
699	}
700
701	if (bss->wpa && bss->wpa_psk_radius != PSK_RADIUS_IGNORED &&
702	    bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
703		wpa_printf(MSG_ERROR, "WPA-PSK using RADIUS enabled, but no "
704			   "RADIUS checking (macaddr_acl=2) enabled.");
705		return -1;
706	}
707
708	if (bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
709	    bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
710	    bss->ssid.wpa_psk_file == NULL &&
711	    (bss->wpa_psk_radius != PSK_RADIUS_REQUIRED ||
712	     bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH)) {
713		wpa_printf(MSG_ERROR, "WPA-PSK enabled, but PSK or passphrase "
714			   "is not configured.");
715		return -1;
716	}
717
718	if (hostapd_mac_comp_empty(bss->bssid) != 0) {
719		size_t i;
720
721		for (i = 0; i < conf->num_bss; i++) {
722			if (conf->bss[i] != bss &&
723			    (hostapd_mac_comp(conf->bss[i]->bssid,
724					      bss->bssid) == 0)) {
725				wpa_printf(MSG_ERROR, "Duplicate BSSID " MACSTR
726					   " on interface '%s' and '%s'.",
727					   MAC2STR(bss->bssid),
728					   conf->bss[i]->iface, bss->iface);
729				return -1;
730			}
731		}
732	}
733
734#ifdef CONFIG_IEEE80211R
735	if (wpa_key_mgmt_ft(bss->wpa_key_mgmt) &&
736	    (bss->nas_identifier == NULL ||
737	     os_strlen(bss->nas_identifier) < 1 ||
738	     os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
739		wpa_printf(MSG_ERROR, "FT (IEEE 802.11r) requires "
740			   "nas_identifier to be configured as a 1..48 octet "
741			   "string");
742		return -1;
743	}
744#endif /* CONFIG_IEEE80211R */
745
746#ifdef CONFIG_IEEE80211N
747	if (conf->ieee80211n && conf->hw_mode == HOSTAPD_MODE_IEEE80211B) {
748		bss->disable_11n = 1;
749		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) in 11b mode is not "
750			   "allowed, disabling HT capabilites");
751	}
752
753	if (conf->ieee80211n &&
754	    bss->ssid.security_policy == SECURITY_STATIC_WEP) {
755		bss->disable_11n = 1;
756		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WEP is not "
757			   "allowed, disabling HT capabilities");
758	}
759
760	if (conf->ieee80211n && bss->wpa &&
761	    !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
762	    !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP))) {
763		bss->disable_11n = 1;
764		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WPA/WPA2 "
765			   "requires CCMP/GCMP to be enabled, disabling HT "
766			   "capabilities");
767	}
768#endif /* CONFIG_IEEE80211N */
769
770#ifdef CONFIG_WPS2
771	if (bss->wps_state && bss->ignore_broadcast_ssid) {
772		wpa_printf(MSG_INFO, "WPS: ignore_broadcast_ssid "
773			   "configuration forced WPS to be disabled");
774		bss->wps_state = 0;
775	}
776
777	if (bss->wps_state && bss->ssid.wep.keys_set && bss->wpa == 0) {
778		wpa_printf(MSG_INFO, "WPS: WEP configuration forced WPS to be "
779			   "disabled");
780		bss->wps_state = 0;
781	}
782
783	if (bss->wps_state && bss->wpa &&
784	    (!(bss->wpa & 2) ||
785	     !(bss->rsn_pairwise & WPA_CIPHER_CCMP))) {
786		wpa_printf(MSG_INFO, "WPS: WPA/TKIP configuration without "
787			   "WPA2/CCMP forced WPS to be disabled");
788		bss->wps_state = 0;
789	}
790#endif /* CONFIG_WPS2 */
791
792#ifdef CONFIG_HS20
793	if (bss->hs20 &&
794	    (!(bss->wpa & 2) ||
795	     !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)))) {
796		wpa_printf(MSG_ERROR, "HS 2.0: WPA2-Enterprise/CCMP "
797			   "configuration is required for Hotspot 2.0 "
798			   "functionality");
799		return -1;
800	}
801#endif /* CONFIG_HS20 */
802
803	return 0;
804}
805
806
807int hostapd_config_check(struct hostapd_config *conf)
808{
809	size_t i;
810
811	if (conf->ieee80211d && (!conf->country[0] || !conf->country[1])) {
812		wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11d without "
813			   "setting the country_code");
814		return -1;
815	}
816
817	if (conf->ieee80211h && !conf->ieee80211d) {
818		wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11h without "
819			   "IEEE 802.11d enabled");
820		return -1;
821	}
822
823	for (i = 0; i < conf->num_bss; i++) {
824		if (hostapd_config_check_bss(conf->bss[i], conf))
825			return -1;
826	}
827
828	return 0;
829}
830
831
832void hostapd_set_security_params(struct hostapd_bss_config *bss)
833{
834	if (bss->individual_wep_key_len == 0) {
835		/* individual keys are not use; can use key idx0 for
836		 * broadcast keys */
837		bss->broadcast_key_idx_min = 0;
838	}
839
840	if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
841		bss->rsn_pairwise = bss->wpa_pairwise;
842	bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
843						    bss->rsn_pairwise);
844
845	bss->radius->auth_server = bss->radius->auth_servers;
846	bss->radius->acct_server = bss->radius->acct_servers;
847
848	if (bss->wpa && bss->ieee802_1x) {
849		bss->ssid.security_policy = SECURITY_WPA;
850	} else if (bss->wpa) {
851		bss->ssid.security_policy = SECURITY_WPA_PSK;
852	} else if (bss->ieee802_1x) {
853		int cipher = WPA_CIPHER_NONE;
854		bss->ssid.security_policy = SECURITY_IEEE_802_1X;
855		bss->ssid.wep.default_len = bss->default_wep_key_len;
856		if (bss->default_wep_key_len)
857			cipher = bss->default_wep_key_len >= 13 ?
858				WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
859		bss->wpa_group = cipher;
860		bss->wpa_pairwise = cipher;
861		bss->rsn_pairwise = cipher;
862	} else if (bss->ssid.wep.keys_set) {
863		int cipher = WPA_CIPHER_WEP40;
864		if (bss->ssid.wep.len[0] >= 13)
865			cipher = WPA_CIPHER_WEP104;
866		bss->ssid.security_policy = SECURITY_STATIC_WEP;
867		bss->wpa_group = cipher;
868		bss->wpa_pairwise = cipher;
869		bss->rsn_pairwise = cipher;
870	} else {
871		bss->ssid.security_policy = SECURITY_PLAINTEXT;
872		bss->wpa_group = WPA_CIPHER_NONE;
873		bss->wpa_pairwise = WPA_CIPHER_NONE;
874		bss->rsn_pairwise = WPA_CIPHER_NONE;
875	}
876}
877