1/*
2 * hostapd / Configuration helper functions
3 * Copyright (c) 2003-2014, 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 "common/dhcp.h"
17#include "eap_common/eap_wsc_common.h"
18#include "eap_server/eap.h"
19#include "wpa_auth.h"
20#include "sta_info.h"
21#include "ap_config.h"
22
23
24static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
25{
26	struct hostapd_vlan *vlan, *prev;
27
28	vlan = bss->vlan;
29	prev = NULL;
30	while (vlan) {
31		prev = vlan;
32		vlan = vlan->next;
33		os_free(prev);
34	}
35
36	bss->vlan = NULL;
37}
38
39
40void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
41{
42	dl_list_init(&bss->anqp_elem);
43
44	bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
45	bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
46	bss->logger_syslog = (unsigned int) -1;
47	bss->logger_stdout = (unsigned int) -1;
48
49	bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
50
51	bss->wep_rekeying_period = 300;
52	/* use key0 in individual key and key1 in broadcast key */
53	bss->broadcast_key_idx_min = 1;
54	bss->broadcast_key_idx_max = 2;
55	bss->eap_reauth_period = 3600;
56
57	bss->wpa_group_rekey = 600;
58	bss->wpa_gmk_rekey = 86400;
59	bss->wpa_group_update_count = 4;
60	bss->wpa_pairwise_update_count = 4;
61	bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
62	bss->wpa_pairwise = WPA_CIPHER_TKIP;
63	bss->wpa_group = WPA_CIPHER_TKIP;
64	bss->rsn_pairwise = 0;
65
66	bss->max_num_sta = MAX_STA_COUNT;
67
68	bss->dtim_period = 2;
69
70	bss->radius_server_auth_port = 1812;
71	bss->eap_sim_db_timeout = 1;
72	bss->ap_max_inactivity = AP_MAX_INACTIVITY;
73	bss->eapol_version = EAPOL_VERSION;
74
75	bss->max_listen_interval = 65535;
76
77	bss->pwd_group = 19; /* ECC: GF(p=256) */
78
79#ifdef CONFIG_IEEE80211W
80	bss->assoc_sa_query_max_timeout = 1000;
81	bss->assoc_sa_query_retry_timeout = 201;
82	bss->group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
83#endif /* CONFIG_IEEE80211W */
84#ifdef EAP_SERVER_FAST
85	 /* both anonymous and authenticated provisioning */
86	bss->eap_fast_prov = 3;
87	bss->pac_key_lifetime = 7 * 24 * 60 * 60;
88	bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
89#endif /* EAP_SERVER_FAST */
90
91	/* Set to -1 as defaults depends on HT in setup */
92	bss->wmm_enabled = -1;
93
94#ifdef CONFIG_IEEE80211R_AP
95	bss->ft_over_ds = 1;
96#endif /* CONFIG_IEEE80211R_AP */
97
98	bss->radius_das_time_window = 300;
99
100	bss->sae_anti_clogging_threshold = 5;
101
102	bss->gas_frag_limit = 1400;
103
104#ifdef CONFIG_FILS
105	dl_list_init(&bss->fils_realms);
106	bss->fils_hlp_wait_time = 30;
107	bss->dhcp_server_port = DHCP_SERVER_PORT;
108	bss->dhcp_relay_port = DHCP_SERVER_PORT;
109#endif /* CONFIG_FILS */
110}
111
112
113struct hostapd_config * hostapd_config_defaults(void)
114{
115#define ecw2cw(ecw) ((1 << (ecw)) - 1)
116
117	struct hostapd_config *conf;
118	struct hostapd_bss_config *bss;
119	const int aCWmin = 4, aCWmax = 10;
120	const struct hostapd_wmm_ac_params ac_bk =
121		{ aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
122	const struct hostapd_wmm_ac_params ac_be =
123		{ aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
124	const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
125		{ aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
126	const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
127		{ aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
128	const struct hostapd_tx_queue_params txq_bk =
129		{ 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
130	const struct hostapd_tx_queue_params txq_be =
131		{ 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
132	const struct hostapd_tx_queue_params txq_vi =
133		{ 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
134	const struct hostapd_tx_queue_params txq_vo =
135		{ 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
136		  (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
137
138#undef ecw2cw
139
140	conf = os_zalloc(sizeof(*conf));
141	bss = os_zalloc(sizeof(*bss));
142	if (conf == NULL || bss == NULL) {
143		wpa_printf(MSG_ERROR, "Failed to allocate memory for "
144			   "configuration data.");
145		os_free(conf);
146		os_free(bss);
147		return NULL;
148	}
149	conf->bss = os_calloc(1, sizeof(struct hostapd_bss_config *));
150	if (conf->bss == NULL) {
151		os_free(conf);
152		os_free(bss);
153		return NULL;
154	}
155	conf->bss[0] = bss;
156
157	bss->radius = os_zalloc(sizeof(*bss->radius));
158	if (bss->radius == NULL) {
159		os_free(conf->bss);
160		os_free(conf);
161		os_free(bss);
162		return NULL;
163	}
164
165	hostapd_config_defaults_bss(bss);
166
167	conf->num_bss = 1;
168
169	conf->beacon_int = 100;
170	conf->rts_threshold = -1; /* use driver default: 2347 */
171	conf->fragm_threshold = -1; /* user driver default: 2346 */
172	conf->send_probe_response = 1;
173	/* Set to invalid value means do not add Power Constraint IE */
174	conf->local_pwr_constraint = -1;
175
176	conf->wmm_ac_params[0] = ac_be;
177	conf->wmm_ac_params[1] = ac_bk;
178	conf->wmm_ac_params[2] = ac_vi;
179	conf->wmm_ac_params[3] = ac_vo;
180
181	conf->tx_queue[0] = txq_vo;
182	conf->tx_queue[1] = txq_vi;
183	conf->tx_queue[2] = txq_be;
184	conf->tx_queue[3] = txq_bk;
185
186	conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
187
188	conf->ap_table_max_size = 255;
189	conf->ap_table_expiration_time = 60;
190	conf->track_sta_max_age = 180;
191
192#ifdef CONFIG_TESTING_OPTIONS
193	conf->ignore_probe_probability = 0.0;
194	conf->ignore_auth_probability = 0.0;
195	conf->ignore_assoc_probability = 0.0;
196	conf->ignore_reassoc_probability = 0.0;
197	conf->corrupt_gtk_rekey_mic_probability = 0.0;
198	conf->ecsa_ie_only = 0;
199#endif /* CONFIG_TESTING_OPTIONS */
200
201	conf->acs = 0;
202	conf->acs_ch_list.num = 0;
203#ifdef CONFIG_ACS
204	conf->acs_num_scans = 5;
205#endif /* CONFIG_ACS */
206
207	return conf;
208}
209
210
211int hostapd_mac_comp(const void *a, const void *b)
212{
213	return os_memcmp(a, b, sizeof(macaddr));
214}
215
216
217static int hostapd_config_read_wpa_psk(const char *fname,
218				       struct hostapd_ssid *ssid)
219{
220	FILE *f;
221	char buf[128], *pos;
222	int line = 0, ret = 0, len, ok;
223	u8 addr[ETH_ALEN];
224	struct hostapd_wpa_psk *psk;
225
226	if (!fname)
227		return 0;
228
229	f = fopen(fname, "r");
230	if (!f) {
231		wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
232		return -1;
233	}
234
235	while (fgets(buf, sizeof(buf), f)) {
236		line++;
237
238		if (buf[0] == '#')
239			continue;
240		pos = buf;
241		while (*pos != '\0') {
242			if (*pos == '\n') {
243				*pos = '\0';
244				break;
245			}
246			pos++;
247		}
248		if (buf[0] == '\0')
249			continue;
250
251		if (hwaddr_aton(buf, addr)) {
252			wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
253				   "line %d in '%s'", buf, line, fname);
254			ret = -1;
255			break;
256		}
257
258		psk = os_zalloc(sizeof(*psk));
259		if (psk == NULL) {
260			wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
261			ret = -1;
262			break;
263		}
264		if (is_zero_ether_addr(addr))
265			psk->group = 1;
266		else
267			os_memcpy(psk->addr, addr, ETH_ALEN);
268
269		pos = buf + 17;
270		if (*pos == '\0') {
271			wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
272				   line, fname);
273			os_free(psk);
274			ret = -1;
275			break;
276		}
277		pos++;
278
279		ok = 0;
280		len = os_strlen(pos);
281		if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
282			ok = 1;
283		else if (len >= 8 && len < 64) {
284			pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
285				    4096, psk->psk, PMK_LEN);
286			ok = 1;
287		}
288		if (!ok) {
289			wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
290				   "'%s'", pos, line, fname);
291			os_free(psk);
292			ret = -1;
293			break;
294		}
295
296		psk->next = ssid->wpa_psk;
297		ssid->wpa_psk = psk;
298	}
299
300	fclose(f);
301
302	return ret;
303}
304
305
306static int hostapd_derive_psk(struct hostapd_ssid *ssid)
307{
308	ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
309	if (ssid->wpa_psk == NULL) {
310		wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
311		return -1;
312	}
313	wpa_hexdump_ascii(MSG_DEBUG, "SSID",
314			  (u8 *) ssid->ssid, ssid->ssid_len);
315	wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
316			      (u8 *) ssid->wpa_passphrase,
317			      os_strlen(ssid->wpa_passphrase));
318	pbkdf2_sha1(ssid->wpa_passphrase,
319		    ssid->ssid, ssid->ssid_len,
320		    4096, ssid->wpa_psk->psk, PMK_LEN);
321	wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
322			ssid->wpa_psk->psk, PMK_LEN);
323	return 0;
324}
325
326
327int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
328{
329	struct hostapd_ssid *ssid = &conf->ssid;
330
331	if (ssid->wpa_passphrase != NULL) {
332		if (ssid->wpa_psk != NULL) {
333			wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
334				   "instead of passphrase");
335		} else {
336			wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
337				   "passphrase");
338			if (hostapd_derive_psk(ssid) < 0)
339				return -1;
340		}
341		ssid->wpa_psk->group = 1;
342	}
343
344	return hostapd_config_read_wpa_psk(ssid->wpa_psk_file, &conf->ssid);
345}
346
347
348static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
349				       int num_servers)
350{
351	int i;
352
353	for (i = 0; i < num_servers; i++) {
354		os_free(servers[i].shared_secret);
355	}
356	os_free(servers);
357}
358
359
360struct hostapd_radius_attr *
361hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
362{
363	for (; attr; attr = attr->next) {
364		if (attr->type == type)
365			return attr;
366	}
367	return NULL;
368}
369
370
371static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
372{
373	struct hostapd_radius_attr *prev;
374
375	while (attr) {
376		prev = attr;
377		attr = attr->next;
378		wpabuf_free(prev->val);
379		os_free(prev);
380	}
381}
382
383
384void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
385{
386	hostapd_config_free_radius_attr(user->accept_attr);
387	os_free(user->identity);
388	bin_clear_free(user->password, user->password_len);
389	os_free(user);
390}
391
392
393void hostapd_config_free_eap_users(struct hostapd_eap_user *user)
394{
395	struct hostapd_eap_user *prev_user;
396
397	while (user) {
398		prev_user = user;
399		user = user->next;
400		hostapd_config_free_eap_user(prev_user);
401	}
402}
403
404
405static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
406{
407	int i;
408	for (i = 0; i < NUM_WEP_KEYS; i++) {
409		bin_clear_free(keys->key[i], keys->len[i]);
410		keys->key[i] = NULL;
411	}
412}
413
414
415void hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk **l)
416{
417	struct hostapd_wpa_psk *psk, *tmp;
418
419	for (psk = *l; psk;) {
420		tmp = psk;
421		psk = psk->next;
422		bin_clear_free(tmp, sizeof(*tmp));
423	}
424	*l = NULL;
425}
426
427
428static void hostapd_config_free_anqp_elem(struct hostapd_bss_config *conf)
429{
430	struct anqp_element *elem;
431
432	while ((elem = dl_list_first(&conf->anqp_elem, struct anqp_element,
433				     list))) {
434		dl_list_del(&elem->list);
435		wpabuf_free(elem->payload);
436		os_free(elem);
437	}
438}
439
440
441static void hostapd_config_free_fils_realms(struct hostapd_bss_config *conf)
442{
443#ifdef CONFIG_FILS
444	struct fils_realm *realm;
445
446	while ((realm = dl_list_first(&conf->fils_realms, struct fils_realm,
447				      list))) {
448		dl_list_del(&realm->list);
449		os_free(realm);
450	}
451#endif /* CONFIG_FILS */
452}
453
454
455void hostapd_config_free_bss(struct hostapd_bss_config *conf)
456{
457	if (conf == NULL)
458		return;
459
460	hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
461
462	str_clear_free(conf->ssid.wpa_passphrase);
463	os_free(conf->ssid.wpa_psk_file);
464	hostapd_config_free_wep(&conf->ssid.wep);
465#ifdef CONFIG_FULL_DYNAMIC_VLAN
466	os_free(conf->ssid.vlan_tagged_interface);
467#endif /* CONFIG_FULL_DYNAMIC_VLAN */
468
469	hostapd_config_free_eap_users(conf->eap_user);
470	os_free(conf->eap_user_sqlite);
471
472	os_free(conf->eap_req_id_text);
473	os_free(conf->erp_domain);
474	os_free(conf->accept_mac);
475	os_free(conf->deny_mac);
476	os_free(conf->nas_identifier);
477	if (conf->radius) {
478		hostapd_config_free_radius(conf->radius->auth_servers,
479					   conf->radius->num_auth_servers);
480		hostapd_config_free_radius(conf->radius->acct_servers,
481					   conf->radius->num_acct_servers);
482	}
483	hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
484	hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
485	os_free(conf->rsn_preauth_interfaces);
486	os_free(conf->ctrl_interface);
487	os_free(conf->ca_cert);
488	os_free(conf->server_cert);
489	os_free(conf->private_key);
490	os_free(conf->private_key_passwd);
491	os_free(conf->ocsp_stapling_response);
492	os_free(conf->ocsp_stapling_response_multi);
493	os_free(conf->dh_file);
494	os_free(conf->openssl_ciphers);
495	os_free(conf->pac_opaque_encr_key);
496	os_free(conf->eap_fast_a_id);
497	os_free(conf->eap_fast_a_id_info);
498	os_free(conf->eap_sim_db);
499	os_free(conf->radius_server_clients);
500	os_free(conf->radius);
501	os_free(conf->radius_das_shared_secret);
502	hostapd_config_free_vlan(conf);
503	os_free(conf->time_zone);
504
505#ifdef CONFIG_IEEE80211R_AP
506	{
507		struct ft_remote_r0kh *r0kh, *r0kh_prev;
508		struct ft_remote_r1kh *r1kh, *r1kh_prev;
509
510		r0kh = conf->r0kh_list;
511		conf->r0kh_list = NULL;
512		while (r0kh) {
513			r0kh_prev = r0kh;
514			r0kh = r0kh->next;
515			os_free(r0kh_prev);
516		}
517
518		r1kh = conf->r1kh_list;
519		conf->r1kh_list = NULL;
520		while (r1kh) {
521			r1kh_prev = r1kh;
522			r1kh = r1kh->next;
523			os_free(r1kh_prev);
524		}
525	}
526#endif /* CONFIG_IEEE80211R_AP */
527
528#ifdef CONFIG_WPS
529	os_free(conf->wps_pin_requests);
530	os_free(conf->device_name);
531	os_free(conf->manufacturer);
532	os_free(conf->model_name);
533	os_free(conf->model_number);
534	os_free(conf->serial_number);
535	os_free(conf->config_methods);
536	os_free(conf->ap_pin);
537	os_free(conf->extra_cred);
538	os_free(conf->ap_settings);
539	os_free(conf->upnp_iface);
540	os_free(conf->friendly_name);
541	os_free(conf->manufacturer_url);
542	os_free(conf->model_description);
543	os_free(conf->model_url);
544	os_free(conf->upc);
545	{
546		unsigned int i;
547
548		for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
549			wpabuf_free(conf->wps_vendor_ext[i]);
550	}
551	wpabuf_free(conf->wps_nfc_dh_pubkey);
552	wpabuf_free(conf->wps_nfc_dh_privkey);
553	wpabuf_free(conf->wps_nfc_dev_pw);
554#endif /* CONFIG_WPS */
555
556	os_free(conf->roaming_consortium);
557	os_free(conf->venue_name);
558	os_free(conf->nai_realm_data);
559	os_free(conf->network_auth_type);
560	os_free(conf->anqp_3gpp_cell_net);
561	os_free(conf->domain_name);
562	hostapd_config_free_anqp_elem(conf);
563
564#ifdef CONFIG_RADIUS_TEST
565	os_free(conf->dump_msk_file);
566#endif /* CONFIG_RADIUS_TEST */
567
568#ifdef CONFIG_HS20
569	os_free(conf->hs20_oper_friendly_name);
570	os_free(conf->hs20_wan_metrics);
571	os_free(conf->hs20_connection_capability);
572	os_free(conf->hs20_operating_class);
573	os_free(conf->hs20_icons);
574	if (conf->hs20_osu_providers) {
575		size_t i;
576		for (i = 0; i < conf->hs20_osu_providers_count; i++) {
577			struct hs20_osu_provider *p;
578			size_t j;
579			p = &conf->hs20_osu_providers[i];
580			os_free(p->friendly_name);
581			os_free(p->server_uri);
582			os_free(p->method_list);
583			for (j = 0; j < p->icons_count; j++)
584				os_free(p->icons[j]);
585			os_free(p->icons);
586			os_free(p->osu_nai);
587			os_free(p->service_desc);
588		}
589		os_free(conf->hs20_osu_providers);
590	}
591	os_free(conf->subscr_remediation_url);
592#endif /* CONFIG_HS20 */
593
594	wpabuf_free(conf->vendor_elements);
595	wpabuf_free(conf->assocresp_elements);
596
597	os_free(conf->sae_groups);
598
599	os_free(conf->wowlan_triggers);
600
601	os_free(conf->server_id);
602
603#ifdef CONFIG_TESTING_OPTIONS
604	wpabuf_free(conf->own_ie_override);
605#endif /* CONFIG_TESTING_OPTIONS */
606
607	os_free(conf->no_probe_resp_if_seen_on);
608	os_free(conf->no_auth_if_seen_on);
609
610	hostapd_config_free_fils_realms(conf);
611
612	os_free(conf);
613}
614
615
616/**
617 * hostapd_config_free - Free hostapd configuration
618 * @conf: Configuration data from hostapd_config_read().
619 */
620void hostapd_config_free(struct hostapd_config *conf)
621{
622	size_t i;
623
624	if (conf == NULL)
625		return;
626
627	for (i = 0; i < conf->num_bss; i++)
628		hostapd_config_free_bss(conf->bss[i]);
629	os_free(conf->bss);
630	os_free(conf->supported_rates);
631	os_free(conf->basic_rates);
632	os_free(conf->acs_ch_list.range);
633	os_free(conf->driver_params);
634#ifdef CONFIG_ACS
635	os_free(conf->acs_chan_bias);
636#endif /* CONFIG_ACS */
637	wpabuf_free(conf->lci);
638	wpabuf_free(conf->civic);
639
640	os_free(conf);
641}
642
643
644/**
645 * hostapd_maclist_found - Find a MAC address from a list
646 * @list: MAC address list
647 * @num_entries: Number of addresses in the list
648 * @addr: Address to search for
649 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
650 * Returns: 1 if address is in the list or 0 if not.
651 *
652 * Perform a binary search for given MAC address from a pre-sorted list.
653 */
654int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
655			  const u8 *addr, struct vlan_description *vlan_id)
656{
657	int start, end, middle, res;
658
659	start = 0;
660	end = num_entries - 1;
661
662	while (start <= end) {
663		middle = (start + end) / 2;
664		res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
665		if (res == 0) {
666			if (vlan_id)
667				*vlan_id = list[middle].vlan_id;
668			return 1;
669		}
670		if (res < 0)
671			start = middle + 1;
672		else
673			end = middle - 1;
674	}
675
676	return 0;
677}
678
679
680int hostapd_rate_found(int *list, int rate)
681{
682	int i;
683
684	if (list == NULL)
685		return 0;
686
687	for (i = 0; list[i] >= 0; i++)
688		if (list[i] == rate)
689			return 1;
690
691	return 0;
692}
693
694
695int hostapd_vlan_valid(struct hostapd_vlan *vlan,
696		       struct vlan_description *vlan_desc)
697{
698	struct hostapd_vlan *v = vlan;
699	int i;
700
701	if (!vlan_desc->notempty || vlan_desc->untagged < 0 ||
702	    vlan_desc->untagged > MAX_VLAN_ID)
703		return 0;
704	for (i = 0; i < MAX_NUM_TAGGED_VLAN; i++) {
705		if (vlan_desc->tagged[i] < 0 ||
706		    vlan_desc->tagged[i] > MAX_VLAN_ID)
707			return 0;
708	}
709	if (!vlan_desc->untagged && !vlan_desc->tagged[0])
710		return 0;
711
712	while (v) {
713		if (!vlan_compare(&v->vlan_desc, vlan_desc) ||
714		    v->vlan_id == VLAN_ID_WILDCARD)
715			return 1;
716		v = v->next;
717	}
718	return 0;
719}
720
721
722const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
723{
724	struct hostapd_vlan *v = vlan;
725	while (v) {
726		if (v->vlan_id == vlan_id)
727			return v->ifname;
728		v = v->next;
729	}
730	return NULL;
731}
732
733
734const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
735			   const u8 *addr, const u8 *p2p_dev_addr,
736			   const u8 *prev_psk)
737{
738	struct hostapd_wpa_psk *psk;
739	int next_ok = prev_psk == NULL;
740
741	if (p2p_dev_addr && !is_zero_ether_addr(p2p_dev_addr)) {
742		wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
743			   " p2p_dev_addr=" MACSTR " prev_psk=%p",
744			   MAC2STR(addr), MAC2STR(p2p_dev_addr), prev_psk);
745		addr = NULL; /* Use P2P Device Address for matching */
746	} else {
747		wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
748			   " prev_psk=%p",
749			   MAC2STR(addr), prev_psk);
750	}
751
752	for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
753		if (next_ok &&
754		    (psk->group ||
755		     (addr && os_memcmp(psk->addr, addr, ETH_ALEN) == 0) ||
756		     (!addr && p2p_dev_addr &&
757		      os_memcmp(psk->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
758		      0)))
759			return psk->psk;
760
761		if (psk->psk == prev_psk)
762			next_ok = 1;
763	}
764
765	return NULL;
766}
767
768
769static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
770				    struct hostapd_config *conf,
771				    int full_config)
772{
773	if (full_config && bss->ieee802_1x && !bss->eap_server &&
774	    !bss->radius->auth_servers) {
775		wpa_printf(MSG_ERROR, "Invalid IEEE 802.1X configuration (no "
776			   "EAP authenticator configured).");
777		return -1;
778	}
779
780	if (bss->wpa) {
781		int wep, i;
782
783		wep = bss->default_wep_key_len > 0 ||
784		       bss->individual_wep_key_len > 0;
785		for (i = 0; i < NUM_WEP_KEYS; i++) {
786			if (bss->ssid.wep.keys_set) {
787				wep = 1;
788				break;
789			}
790		}
791
792		if (wep) {
793			wpa_printf(MSG_ERROR, "WEP configuration in a WPA network is not supported");
794			return -1;
795		}
796	}
797
798	if (full_config && bss->wpa &&
799	    bss->wpa_psk_radius != PSK_RADIUS_IGNORED &&
800	    bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
801		wpa_printf(MSG_ERROR, "WPA-PSK using RADIUS enabled, but no "
802			   "RADIUS checking (macaddr_acl=2) enabled.");
803		return -1;
804	}
805
806	if (full_config && bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
807	    bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
808	    bss->ssid.wpa_psk_file == NULL &&
809	    (bss->wpa_psk_radius != PSK_RADIUS_REQUIRED ||
810	     bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH)) {
811		wpa_printf(MSG_ERROR, "WPA-PSK enabled, but PSK or passphrase "
812			   "is not configured.");
813		return -1;
814	}
815
816	if (full_config && !is_zero_ether_addr(bss->bssid)) {
817		size_t i;
818
819		for (i = 0; i < conf->num_bss; i++) {
820			if (conf->bss[i] != bss &&
821			    (hostapd_mac_comp(conf->bss[i]->bssid,
822					      bss->bssid) == 0)) {
823				wpa_printf(MSG_ERROR, "Duplicate BSSID " MACSTR
824					   " on interface '%s' and '%s'.",
825					   MAC2STR(bss->bssid),
826					   conf->bss[i]->iface, bss->iface);
827				return -1;
828			}
829		}
830	}
831
832#ifdef CONFIG_IEEE80211R_AP
833	if (full_config && wpa_key_mgmt_ft(bss->wpa_key_mgmt) &&
834	    (bss->nas_identifier == NULL ||
835	     os_strlen(bss->nas_identifier) < 1 ||
836	     os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
837		wpa_printf(MSG_ERROR, "FT (IEEE 802.11r) requires "
838			   "nas_identifier to be configured as a 1..48 octet "
839			   "string");
840		return -1;
841	}
842#endif /* CONFIG_IEEE80211R_AP */
843
844#ifdef CONFIG_IEEE80211N
845	if (full_config && conf->ieee80211n &&
846	    conf->hw_mode == HOSTAPD_MODE_IEEE80211B) {
847		bss->disable_11n = 1;
848		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) in 11b mode is not "
849			   "allowed, disabling HT capabilities");
850	}
851
852	if (full_config && conf->ieee80211n &&
853	    bss->ssid.security_policy == SECURITY_STATIC_WEP) {
854		bss->disable_11n = 1;
855		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WEP is not "
856			   "allowed, disabling HT capabilities");
857	}
858
859	if (full_config && conf->ieee80211n && bss->wpa &&
860	    !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
861	    !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
862				   WPA_CIPHER_CCMP_256 | WPA_CIPHER_GCMP_256)))
863	{
864		bss->disable_11n = 1;
865		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WPA/WPA2 "
866			   "requires CCMP/GCMP to be enabled, disabling HT "
867			   "capabilities");
868	}
869#endif /* CONFIG_IEEE80211N */
870
871#ifdef CONFIG_IEEE80211AC
872	if (full_config && conf->ieee80211ac &&
873	    bss->ssid.security_policy == SECURITY_STATIC_WEP) {
874		bss->disable_11ac = 1;
875		wpa_printf(MSG_ERROR,
876			   "VHT (IEEE 802.11ac) with WEP is not allowed, disabling VHT capabilities");
877	}
878
879	if (full_config && conf->ieee80211ac && bss->wpa &&
880	    !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
881	    !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
882				   WPA_CIPHER_CCMP_256 | WPA_CIPHER_GCMP_256)))
883	{
884		bss->disable_11ac = 1;
885		wpa_printf(MSG_ERROR,
886			   "VHT (IEEE 802.11ac) with WPA/WPA2 requires CCMP/GCMP to be enabled, disabling VHT capabilities");
887	}
888#endif /* CONFIG_IEEE80211AC */
889
890#ifdef CONFIG_WPS
891	if (full_config && bss->wps_state && bss->ignore_broadcast_ssid) {
892		wpa_printf(MSG_INFO, "WPS: ignore_broadcast_ssid "
893			   "configuration forced WPS to be disabled");
894		bss->wps_state = 0;
895	}
896
897	if (full_config && bss->wps_state &&
898	    bss->ssid.wep.keys_set && bss->wpa == 0) {
899		wpa_printf(MSG_INFO, "WPS: WEP configuration forced WPS to be "
900			   "disabled");
901		bss->wps_state = 0;
902	}
903
904	if (full_config && bss->wps_state && bss->wpa &&
905	    (!(bss->wpa & 2) ||
906	     !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)))) {
907		wpa_printf(MSG_INFO, "WPS: WPA/TKIP configuration without "
908			   "WPA2/CCMP/GCMP forced WPS to be disabled");
909		bss->wps_state = 0;
910	}
911#endif /* CONFIG_WPS */
912
913#ifdef CONFIG_HS20
914	if (full_config && bss->hs20 &&
915	    (!(bss->wpa & 2) ||
916	     !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
917				    WPA_CIPHER_CCMP_256 |
918				    WPA_CIPHER_GCMP_256)))) {
919		wpa_printf(MSG_ERROR, "HS 2.0: WPA2-Enterprise/CCMP "
920			   "configuration is required for Hotspot 2.0 "
921			   "functionality");
922		return -1;
923	}
924#endif /* CONFIG_HS20 */
925
926#ifdef CONFIG_MBO
927	if (full_config && bss->mbo_enabled && (bss->wpa & 2) &&
928	    bss->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
929		wpa_printf(MSG_ERROR,
930			   "MBO: PMF needs to be enabled whenever using WPA2 with MBO");
931		return -1;
932	}
933#endif /* CONFIG_MBO */
934
935	return 0;
936}
937
938
939static int hostapd_config_check_cw(struct hostapd_config *conf, int queue)
940{
941	int tx_cwmin = conf->tx_queue[queue].cwmin;
942	int tx_cwmax = conf->tx_queue[queue].cwmax;
943	int ac_cwmin = conf->wmm_ac_params[queue].cwmin;
944	int ac_cwmax = conf->wmm_ac_params[queue].cwmax;
945
946	if (tx_cwmin > tx_cwmax) {
947		wpa_printf(MSG_ERROR,
948			   "Invalid TX queue cwMin/cwMax values. cwMin(%d) greater than cwMax(%d)",
949			   tx_cwmin, tx_cwmax);
950		return -1;
951	}
952	if (ac_cwmin > ac_cwmax) {
953		wpa_printf(MSG_ERROR,
954			   "Invalid WMM AC cwMin/cwMax values. cwMin(%d) greater than cwMax(%d)",
955			   ac_cwmin, ac_cwmax);
956		return -1;
957	}
958	return 0;
959}
960
961
962int hostapd_config_check(struct hostapd_config *conf, int full_config)
963{
964	size_t i;
965
966	if (full_config && conf->ieee80211d &&
967	    (!conf->country[0] || !conf->country[1])) {
968		wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11d without "
969			   "setting the country_code");
970		return -1;
971	}
972
973	if (full_config && conf->ieee80211h && !conf->ieee80211d) {
974		wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11h without "
975			   "IEEE 802.11d enabled");
976		return -1;
977	}
978
979	if (full_config && conf->local_pwr_constraint != -1 &&
980	    !conf->ieee80211d) {
981		wpa_printf(MSG_ERROR, "Cannot add Power Constraint element without Country element");
982		return -1;
983	}
984
985	if (full_config && conf->spectrum_mgmt_required &&
986	    conf->local_pwr_constraint == -1) {
987		wpa_printf(MSG_ERROR, "Cannot set Spectrum Management bit without Country and Power Constraint elements");
988		return -1;
989	}
990
991	for (i = 0; i < NUM_TX_QUEUES; i++) {
992		if (hostapd_config_check_cw(conf, i))
993			return -1;
994	}
995
996	for (i = 0; i < conf->num_bss; i++) {
997		if (hostapd_config_check_bss(conf->bss[i], conf, full_config))
998			return -1;
999	}
1000
1001	return 0;
1002}
1003
1004
1005void hostapd_set_security_params(struct hostapd_bss_config *bss,
1006				 int full_config)
1007{
1008	if (bss->individual_wep_key_len == 0) {
1009		/* individual keys are not use; can use key idx0 for
1010		 * broadcast keys */
1011		bss->broadcast_key_idx_min = 0;
1012	}
1013
1014	if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
1015		bss->rsn_pairwise = bss->wpa_pairwise;
1016	bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
1017						    bss->rsn_pairwise);
1018
1019	if (full_config) {
1020		bss->radius->auth_server = bss->radius->auth_servers;
1021		bss->radius->acct_server = bss->radius->acct_servers;
1022	}
1023
1024	if (bss->wpa && bss->ieee802_1x) {
1025		bss->ssid.security_policy = SECURITY_WPA;
1026	} else if (bss->wpa) {
1027		bss->ssid.security_policy = SECURITY_WPA_PSK;
1028	} else if (bss->ieee802_1x) {
1029		int cipher = WPA_CIPHER_NONE;
1030		bss->ssid.security_policy = SECURITY_IEEE_802_1X;
1031		bss->ssid.wep.default_len = bss->default_wep_key_len;
1032		if (full_config && bss->default_wep_key_len) {
1033			cipher = bss->default_wep_key_len >= 13 ?
1034				WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
1035		} else if (full_config && bss->ssid.wep.keys_set) {
1036			if (bss->ssid.wep.len[0] >= 13)
1037				cipher = WPA_CIPHER_WEP104;
1038			else
1039				cipher = WPA_CIPHER_WEP40;
1040		}
1041		bss->wpa_group = cipher;
1042		bss->wpa_pairwise = cipher;
1043		bss->rsn_pairwise = cipher;
1044		if (full_config)
1045			bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
1046	} else if (bss->ssid.wep.keys_set) {
1047		int cipher = WPA_CIPHER_WEP40;
1048		if (bss->ssid.wep.len[0] >= 13)
1049			cipher = WPA_CIPHER_WEP104;
1050		bss->ssid.security_policy = SECURITY_STATIC_WEP;
1051		bss->wpa_group = cipher;
1052		bss->wpa_pairwise = cipher;
1053		bss->rsn_pairwise = cipher;
1054		if (full_config)
1055			bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
1056	} else if (bss->osen) {
1057		bss->ssid.security_policy = SECURITY_OSEN;
1058		bss->wpa_group = WPA_CIPHER_CCMP;
1059		bss->wpa_pairwise = 0;
1060		bss->rsn_pairwise = WPA_CIPHER_CCMP;
1061	} else {
1062		bss->ssid.security_policy = SECURITY_PLAINTEXT;
1063		if (full_config) {
1064			bss->wpa_group = WPA_CIPHER_NONE;
1065			bss->wpa_pairwise = WPA_CIPHER_NONE;
1066			bss->rsn_pairwise = WPA_CIPHER_NONE;
1067			bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
1068		}
1069	}
1070}
1071