• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "crypto/tls.h"
14 #include "radius/radius_client.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_1x_defs.h"
17 #include "common/eapol_common.h"
18 #include "common/dhcp.h"
19 #include "common/sae.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "eap_server/eap.h"
22 #include "wpa_auth.h"
23 #include "sta_info.h"
24 #include "airtime_policy.h"
25 #include "ap_config.h"
26 
27 
hostapd_config_free_vlan(struct hostapd_bss_config * bss)28 static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
29 {
30 	struct hostapd_vlan *vlan, *prev;
31 
32 	vlan = bss->vlan;
33 	prev = NULL;
34 	while (vlan) {
35 		prev = vlan;
36 		vlan = vlan->next;
37 		os_free(prev);
38 	}
39 
40 	bss->vlan = NULL;
41 }
42 
43 
44 #ifndef DEFAULT_WPA_DISABLE_EAPOL_KEY_RETRIES
45 #define DEFAULT_WPA_DISABLE_EAPOL_KEY_RETRIES 0
46 #endif /* DEFAULT_WPA_DISABLE_EAPOL_KEY_RETRIES */
47 
hostapd_config_defaults_bss(struct hostapd_bss_config * bss)48 void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
49 {
50 	dl_list_init(&bss->anqp_elem);
51 
52 	bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
53 	bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
54 	bss->logger_syslog = (unsigned int) -1;
55 	bss->logger_stdout = (unsigned int) -1;
56 
57 #ifdef CONFIG_WEP
58 	bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
59 
60 	bss->wep_rekeying_period = 300;
61 	/* use key0 in individual key and key1 in broadcast key */
62 	bss->broadcast_key_idx_min = 1;
63 	bss->broadcast_key_idx_max = 2;
64 #else /* CONFIG_WEP */
65 	bss->auth_algs = WPA_AUTH_ALG_OPEN;
66 #endif /* CONFIG_WEP */
67 	bss->eap_reauth_period = 3600;
68 
69 	bss->wpa_group_rekey = 600;
70 	bss->wpa_gmk_rekey = 86400;
71 	bss->wpa_deny_ptk0_rekey = PTK0_REKEY_ALLOW_ALWAYS;
72 	bss->wpa_group_update_count = 4;
73 	bss->wpa_pairwise_update_count = 4;
74 	bss->wpa_disable_eapol_key_retries =
75 		DEFAULT_WPA_DISABLE_EAPOL_KEY_RETRIES;
76 	bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
77 #ifdef CONFIG_NO_TKIP
78 	bss->wpa_pairwise = WPA_CIPHER_CCMP;
79 	bss->wpa_group = WPA_CIPHER_CCMP;
80 #else /* CONFIG_NO_TKIP */
81 	bss->wpa_pairwise = WPA_CIPHER_TKIP;
82 	bss->wpa_group = WPA_CIPHER_TKIP;
83 #endif /* CONFIG_NO_TKIP */
84 	bss->rsn_pairwise = 0;
85 
86 	bss->max_num_sta = MAX_STA_COUNT;
87 
88 	bss->dtim_period = 2;
89 
90 	bss->radius_server_auth_port = 1812;
91 	bss->eap_sim_db_timeout = 1;
92 	bss->eap_sim_id = 3;
93 	bss->ap_max_inactivity = AP_MAX_INACTIVITY;
94 	bss->eapol_version = EAPOL_VERSION;
95 
96 	bss->max_listen_interval = 65535;
97 
98 	bss->pwd_group = 19; /* ECC: GF(p=256) */
99 
100 	bss->assoc_sa_query_max_timeout = 1000;
101 	bss->assoc_sa_query_retry_timeout = 201;
102 	bss->group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
103 #ifdef EAP_SERVER_FAST
104 	 /* both anonymous and authenticated provisioning */
105 	bss->eap_fast_prov = 3;
106 	bss->pac_key_lifetime = 7 * 24 * 60 * 60;
107 	bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
108 #endif /* EAP_SERVER_FAST */
109 
110 	/* Set to -1 as defaults depends on HT in setup */
111 	bss->wmm_enabled = -1;
112 
113 #ifdef CONFIG_IEEE80211R_AP
114 	bss->ft_over_ds = 1;
115 	bss->rkh_pos_timeout = 86400;
116 	bss->rkh_neg_timeout = 60;
117 	bss->rkh_pull_timeout = 1000;
118 	bss->rkh_pull_retries = 4;
119 	bss->r0_key_lifetime = 1209600;
120 #endif /* CONFIG_IEEE80211R_AP */
121 
122 	bss->radius_das_time_window = 300;
123 
124 	bss->sae_anti_clogging_threshold = 5;
125 	bss->sae_sync = 5;
126 
127 	bss->gas_frag_limit = 1400;
128 
129 #ifdef CONFIG_FILS
130 	dl_list_init(&bss->fils_realms);
131 	bss->fils_hlp_wait_time = 30;
132 	bss->dhcp_server_port = DHCP_SERVER_PORT;
133 	bss->dhcp_relay_port = DHCP_SERVER_PORT;
134 #endif /* CONFIG_FILS */
135 
136 	bss->broadcast_deauth = 1;
137 
138 #ifdef CONFIG_MBO
139 	bss->mbo_cell_data_conn_pref = -1;
140 #endif /* CONFIG_MBO */
141 
142 	/* Disable TLS v1.3 by default for now to avoid interoperability issue.
143 	 * This can be enabled by default once the implementation has been fully
144 	 * completed and tested with other implementations. */
145 	bss->tls_flags = TLS_CONN_DISABLE_TLSv1_3;
146 
147 	bss->max_auth_rounds = 100;
148 	bss->max_auth_rounds_short = 50;
149 
150 	bss->send_probe_response = 1;
151 
152 #ifdef CONFIG_HS20
153 	bss->hs20_release = (HS20_VERSION >> 4) + 1;
154 #endif /* CONFIG_HS20 */
155 
156 #ifdef CONFIG_MACSEC
157 	bss->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
158 	bss->macsec_port = 1;
159 #endif /* CONFIG_MACSEC */
160 
161 	/* Default to strict CRL checking. */
162 	bss->check_crl_strict = 1;
163 }
164 
165 
hostapd_config_defaults(void)166 struct hostapd_config * hostapd_config_defaults(void)
167 {
168 #define ecw2cw(ecw) ((1 << (ecw)) - 1)
169 
170 	struct hostapd_config *conf;
171 	struct hostapd_bss_config *bss;
172 	const int aCWmin = 4, aCWmax = 10;
173 	const struct hostapd_wmm_ac_params ac_bk =
174 		{ aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
175 	const struct hostapd_wmm_ac_params ac_be =
176 		{ aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
177 	const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
178 		{ aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
179 	const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
180 		{ aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
181 	const struct hostapd_tx_queue_params txq_bk =
182 		{ 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
183 	const struct hostapd_tx_queue_params txq_be =
184 		{ 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
185 	const struct hostapd_tx_queue_params txq_vi =
186 		{ 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
187 	const struct hostapd_tx_queue_params txq_vo =
188 		{ 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
189 		  (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
190 
191 #undef ecw2cw
192 
193 	conf = os_zalloc(sizeof(*conf));
194 	bss = os_zalloc(sizeof(*bss));
195 	if (conf == NULL || bss == NULL) {
196 		wpa_printf(MSG_ERROR, "Failed to allocate memory for "
197 			   "configuration data.");
198 		os_free(conf);
199 		os_free(bss);
200 		return NULL;
201 	}
202 	conf->bss = os_calloc(1, sizeof(struct hostapd_bss_config *));
203 	if (conf->bss == NULL) {
204 		os_free(conf);
205 		os_free(bss);
206 		return NULL;
207 	}
208 	conf->bss[0] = bss;
209 
210 	bss->radius = os_zalloc(sizeof(*bss->radius));
211 	if (bss->radius == NULL) {
212 		os_free(conf->bss);
213 		os_free(conf);
214 		os_free(bss);
215 		return NULL;
216 	}
217 
218 	hostapd_config_defaults_bss(bss);
219 
220 	conf->num_bss = 1;
221 
222 	conf->beacon_int = 100;
223 	conf->rts_threshold = -2; /* use driver default: 2347 */
224 	conf->fragm_threshold = -2; /* user driver default: 2346 */
225 	/* Set to invalid value means do not add Power Constraint IE */
226 	conf->local_pwr_constraint = -1;
227 
228 	conf->wmm_ac_params[0] = ac_be;
229 	conf->wmm_ac_params[1] = ac_bk;
230 	conf->wmm_ac_params[2] = ac_vi;
231 	conf->wmm_ac_params[3] = ac_vo;
232 
233 	conf->tx_queue[0] = txq_vo;
234 	conf->tx_queue[1] = txq_vi;
235 	conf->tx_queue[2] = txq_be;
236 	conf->tx_queue[3] = txq_bk;
237 
238 	conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
239 
240 	conf->ap_table_max_size = 255;
241 	conf->ap_table_expiration_time = 60;
242 	conf->track_sta_max_age = 180;
243 
244 #ifdef CONFIG_TESTING_OPTIONS
245 	conf->ignore_probe_probability = 0.0;
246 	conf->ignore_auth_probability = 0.0;
247 	conf->ignore_assoc_probability = 0.0;
248 	conf->ignore_reassoc_probability = 0.0;
249 	conf->corrupt_gtk_rekey_mic_probability = 0.0;
250 	conf->ecsa_ie_only = 0;
251 #endif /* CONFIG_TESTING_OPTIONS */
252 
253 	conf->acs = 0;
254 	conf->acs_ch_list.num = 0;
255 #ifdef CONFIG_ACS
256 	conf->acs_num_scans = 5;
257 #endif /* CONFIG_ACS */
258 
259 #ifdef CONFIG_IEEE80211AX
260 	conf->he_op.he_rts_threshold = HE_OPERATION_RTS_THRESHOLD_MASK >>
261 		HE_OPERATION_RTS_THRESHOLD_OFFSET;
262 	/* Set default basic MCS/NSS set to single stream MCS 0-7 */
263 	conf->he_op.he_basic_mcs_nss_set = 0xfffc;
264 	conf->he_op.he_bss_color_disabled = 1;
265 	conf->he_op.he_bss_color_partial = 0;
266 	conf->he_op.he_bss_color = 1;
267 #endif /* CONFIG_IEEE80211AX */
268 
269 	/* The third octet of the country string uses an ASCII space character
270 	 * by default to indicate that the regulations encompass all
271 	 * environments for the current frequency band in the country. */
272 	conf->country[2] = ' ';
273 
274 	conf->rssi_reject_assoc_rssi = 0;
275 	conf->rssi_reject_assoc_timeout = 30;
276 
277 #ifdef CONFIG_AIRTIME_POLICY
278 	conf->airtime_update_interval = AIRTIME_DEFAULT_UPDATE_INTERVAL;
279 #endif /* CONFIG_AIRTIME_POLICY */
280 
281 	return conf;
282 }
283 
284 
hostapd_mac_comp(const void * a,const void * b)285 int hostapd_mac_comp(const void *a, const void *b)
286 {
287 	return os_memcmp(a, b, sizeof(macaddr));
288 }
289 
290 
hostapd_config_read_wpa_psk(const char * fname,struct hostapd_ssid * ssid)291 static int hostapd_config_read_wpa_psk(const char *fname,
292 				       struct hostapd_ssid *ssid)
293 {
294 	FILE *f;
295 	char buf[128], *pos;
296 	const char *keyid;
297 	char *context;
298 	char *context2;
299 	char *token;
300 	char *name;
301 	char *value;
302 	int line = 0, ret = 0, len, ok;
303 	u8 addr[ETH_ALEN];
304 	struct hostapd_wpa_psk *psk;
305 
306 	if (!fname)
307 		return 0;
308 
309 	f = fopen(fname, "r");
310 	if (!f) {
311 		wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
312 		return -1;
313 	}
314 
315 	while (fgets(buf, sizeof(buf), f)) {
316 		int vlan_id = 0;
317 		int wps = 0;
318 
319 		line++;
320 
321 		if (buf[0] == '#')
322 			continue;
323 		pos = buf;
324 		while (*pos != '\0') {
325 			if (*pos == '\n') {
326 				*pos = '\0';
327 				break;
328 			}
329 			pos++;
330 		}
331 		if (buf[0] == '\0')
332 			continue;
333 
334 		context = NULL;
335 		keyid = NULL;
336 		while ((token = str_token(buf, " ", &context))) {
337 			if (!os_strchr(token, '='))
338 				break;
339 			context2 = NULL;
340 			name = str_token(token, "=", &context2);
341 			if (!name)
342 				break;
343 			value = str_token(token, "", &context2);
344 			if (!value)
345 				value = "";
346 			if (!os_strcmp(name, "keyid")) {
347 				keyid = value;
348 			} else if (!os_strcmp(name, "wps")) {
349 				wps = atoi(value);
350 			} else if (!os_strcmp(name, "vlanid")) {
351 				vlan_id = atoi(value);
352 			} else {
353 				wpa_printf(MSG_ERROR,
354 					   "Unrecognized '%s=%s' on line %d in '%s'",
355 					   name, value, line, fname);
356 				ret = -1;
357 				break;
358 			}
359 		}
360 
361 		if (ret == -1)
362 			break;
363 
364 		if (!token)
365 			token = "";
366 		if (hwaddr_aton(token, addr)) {
367 			wpa_printf(MSG_ERROR,
368 				   "Invalid MAC address '%s' on line %d in '%s'",
369 				   token, line, fname);
370 			ret = -1;
371 			break;
372 		}
373 
374 		psk = os_zalloc(sizeof(*psk));
375 		if (psk == NULL) {
376 			wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
377 			ret = -1;
378 			break;
379 		}
380 		psk->vlan_id = vlan_id;
381 		if (is_zero_ether_addr(addr))
382 			psk->group = 1;
383 		else
384 			os_memcpy(psk->addr, addr, ETH_ALEN);
385 
386 		pos = str_token(buf, "", &context);
387 		if (!pos) {
388 			wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
389 				   line, fname);
390 			os_free(psk);
391 			ret = -1;
392 			break;
393 		}
394 
395 		ok = 0;
396 		len = os_strlen(pos);
397 		if (len == 2 * PMK_LEN &&
398 		    hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
399 			ok = 1;
400 		else if (len >= 8 && len < 64 &&
401 			 pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
402 				     4096, psk->psk, PMK_LEN) == 0)
403 			ok = 1;
404 		if (!ok) {
405 			wpa_printf(MSG_ERROR,
406 				   "Invalid PSK '%s' on line %d in '%s'",
407 				   pos, line, fname);
408 			os_free(psk);
409 			ret = -1;
410 			break;
411 		}
412 
413 		if (keyid) {
414 			len = os_strlcpy(psk->keyid, keyid, sizeof(psk->keyid));
415 			if ((size_t) len >= sizeof(psk->keyid)) {
416 				wpa_printf(MSG_ERROR,
417 					   "PSK keyid too long on line %d in '%s'",
418 					   line, fname);
419 				os_free(psk);
420 				ret = -1;
421 				break;
422 			}
423 		}
424 
425 		psk->wps = wps;
426 
427 		psk->next = ssid->wpa_psk;
428 		ssid->wpa_psk = psk;
429 	}
430 
431 	fclose(f);
432 
433 	return ret;
434 }
435 
436 
hostapd_derive_psk(struct hostapd_ssid * ssid)437 static int hostapd_derive_psk(struct hostapd_ssid *ssid)
438 {
439 	ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
440 	if (ssid->wpa_psk == NULL) {
441 		wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
442 		return -1;
443 	}
444 	wpa_hexdump_ascii(MSG_DEBUG, "SSID",
445 			  (u8 *) ssid->ssid, ssid->ssid_len);
446 	wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
447 			      (u8 *) ssid->wpa_passphrase,
448 			      os_strlen(ssid->wpa_passphrase));
449 	pbkdf2_sha1(ssid->wpa_passphrase,
450 		    ssid->ssid, ssid->ssid_len,
451 		    4096, ssid->wpa_psk->psk, PMK_LEN);
452 	wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
453 			ssid->wpa_psk->psk, PMK_LEN);
454 	return 0;
455 }
456 
457 
hostapd_setup_sae_pt(struct hostapd_bss_config * conf)458 int hostapd_setup_sae_pt(struct hostapd_bss_config *conf)
459 {
460 #ifdef CONFIG_SAE
461 	struct hostapd_ssid *ssid = &conf->ssid;
462 	struct sae_password_entry *pw;
463 
464 	if ((conf->sae_pwe == 0 && !hostapd_sae_pw_id_in_use(conf)) ||
465 	    conf->sae_pwe == 3 ||
466 	    !wpa_key_mgmt_sae(conf->wpa_key_mgmt))
467 		return 0; /* PT not needed */
468 
469 	sae_deinit_pt(ssid->pt);
470 	ssid->pt = NULL;
471 	if (ssid->wpa_passphrase) {
472 		ssid->pt = sae_derive_pt(conf->sae_groups, ssid->ssid,
473 					 ssid->ssid_len,
474 					 (const u8 *) ssid->wpa_passphrase,
475 					 os_strlen(ssid->wpa_passphrase),
476 					 NULL);
477 		if (!ssid->pt)
478 			return -1;
479 	}
480 
481 	for (pw = conf->sae_passwords; pw; pw = pw->next) {
482 		sae_deinit_pt(pw->pt);
483 		pw->pt = sae_derive_pt(conf->sae_groups, ssid->ssid,
484 				       ssid->ssid_len,
485 				       (const u8 *) pw->password,
486 				       os_strlen(pw->password),
487 				       pw->identifier);
488 		if (!pw->pt)
489 			return -1;
490 	}
491 #endif /* CONFIG_SAE */
492 
493 	return 0;
494 }
495 
496 
hostapd_setup_wpa_psk(struct hostapd_bss_config * conf)497 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
498 {
499 	struct hostapd_ssid *ssid = &conf->ssid;
500 
501 	if (hostapd_setup_sae_pt(conf) < 0)
502 		return -1;
503 
504 	if (ssid->wpa_passphrase != NULL) {
505 		if (ssid->wpa_psk != NULL) {
506 			wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
507 				   "instead of passphrase");
508 		} else {
509 			wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
510 				   "passphrase");
511 			if (hostapd_derive_psk(ssid) < 0)
512 				return -1;
513 		}
514 		ssid->wpa_psk->group = 1;
515 	}
516 
517 	return hostapd_config_read_wpa_psk(ssid->wpa_psk_file, &conf->ssid);
518 }
519 
520 
hostapd_config_free_radius(struct hostapd_radius_server * servers,int num_servers)521 static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
522 				       int num_servers)
523 {
524 	int i;
525 
526 	for (i = 0; i < num_servers; i++) {
527 		os_free(servers[i].shared_secret);
528 	}
529 	os_free(servers);
530 }
531 
532 
533 struct hostapd_radius_attr *
hostapd_config_get_radius_attr(struct hostapd_radius_attr * attr,u8 type)534 hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
535 {
536 	for (; attr; attr = attr->next) {
537 		if (attr->type == type)
538 			return attr;
539 	}
540 	return NULL;
541 }
542 
543 
hostapd_parse_radius_attr(const char * value)544 struct hostapd_radius_attr * hostapd_parse_radius_attr(const char *value)
545 {
546 	const char *pos;
547 	char syntax;
548 	struct hostapd_radius_attr *attr;
549 	size_t len;
550 
551 	attr = os_zalloc(sizeof(*attr));
552 	if (!attr)
553 		return NULL;
554 
555 	attr->type = atoi(value);
556 
557 	pos = os_strchr(value, ':');
558 	if (!pos) {
559 		attr->val = wpabuf_alloc(1);
560 		if (!attr->val) {
561 			os_free(attr);
562 			return NULL;
563 		}
564 		wpabuf_put_u8(attr->val, 0);
565 		return attr;
566 	}
567 
568 	pos++;
569 	if (pos[0] == '\0' || pos[1] != ':') {
570 		os_free(attr);
571 		return NULL;
572 	}
573 	syntax = *pos++;
574 	pos++;
575 
576 	switch (syntax) {
577 	case 's':
578 		attr->val = wpabuf_alloc_copy(pos, os_strlen(pos));
579 		break;
580 	case 'x':
581 		len = os_strlen(pos);
582 		if (len & 1)
583 			break;
584 		len /= 2;
585 		attr->val = wpabuf_alloc(len);
586 		if (!attr->val)
587 			break;
588 		if (hexstr2bin(pos, wpabuf_put(attr->val, len), len) < 0) {
589 			wpabuf_free(attr->val);
590 			os_free(attr);
591 			return NULL;
592 		}
593 		break;
594 	case 'd':
595 		attr->val = wpabuf_alloc(4);
596 		if (attr->val)
597 			wpabuf_put_be32(attr->val, atoi(pos));
598 		break;
599 	default:
600 		os_free(attr);
601 		return NULL;
602 	}
603 
604 	if (!attr->val) {
605 		os_free(attr);
606 		return NULL;
607 	}
608 
609 	return attr;
610 }
611 
612 
hostapd_config_free_radius_attr(struct hostapd_radius_attr * attr)613 void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
614 {
615 	struct hostapd_radius_attr *prev;
616 
617 	while (attr) {
618 		prev = attr;
619 		attr = attr->next;
620 		wpabuf_free(prev->val);
621 		os_free(prev);
622 	}
623 }
624 
625 
hostapd_config_free_eap_user(struct hostapd_eap_user * user)626 void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
627 {
628 	hostapd_config_free_radius_attr(user->accept_attr);
629 	os_free(user->identity);
630 	bin_clear_free(user->password, user->password_len);
631 	bin_clear_free(user->salt, user->salt_len);
632 	os_free(user);
633 }
634 
635 
hostapd_config_free_eap_users(struct hostapd_eap_user * user)636 void hostapd_config_free_eap_users(struct hostapd_eap_user *user)
637 {
638 	struct hostapd_eap_user *prev_user;
639 
640 	while (user) {
641 		prev_user = user;
642 		user = user->next;
643 		hostapd_config_free_eap_user(prev_user);
644 	}
645 }
646 
647 
648 #ifdef CONFIG_WEP
hostapd_config_free_wep(struct hostapd_wep_keys * keys)649 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
650 {
651 	int i;
652 	for (i = 0; i < NUM_WEP_KEYS; i++) {
653 		bin_clear_free(keys->key[i], keys->len[i]);
654 		keys->key[i] = NULL;
655 	}
656 }
657 #endif /* CONFIG_WEP */
658 
659 
hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk ** l)660 void hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk **l)
661 {
662 	struct hostapd_wpa_psk *psk, *tmp;
663 
664 	for (psk = *l; psk;) {
665 		tmp = psk;
666 		psk = psk->next;
667 		bin_clear_free(tmp, sizeof(*tmp));
668 	}
669 	*l = NULL;
670 }
671 
672 
hostapd_config_free_anqp_elem(struct hostapd_bss_config * conf)673 static void hostapd_config_free_anqp_elem(struct hostapd_bss_config *conf)
674 {
675 	struct anqp_element *elem;
676 
677 	while ((elem = dl_list_first(&conf->anqp_elem, struct anqp_element,
678 				     list))) {
679 		dl_list_del(&elem->list);
680 		wpabuf_free(elem->payload);
681 		os_free(elem);
682 	}
683 }
684 
685 
hostapd_config_free_fils_realms(struct hostapd_bss_config * conf)686 static void hostapd_config_free_fils_realms(struct hostapd_bss_config *conf)
687 {
688 #ifdef CONFIG_FILS
689 	struct fils_realm *realm;
690 
691 	while ((realm = dl_list_first(&conf->fils_realms, struct fils_realm,
692 				      list))) {
693 		dl_list_del(&realm->list);
694 		os_free(realm);
695 	}
696 #endif /* CONFIG_FILS */
697 }
698 
699 
hostapd_config_free_sae_passwords(struct hostapd_bss_config * conf)700 static void hostapd_config_free_sae_passwords(struct hostapd_bss_config *conf)
701 {
702 	struct sae_password_entry *pw, *tmp;
703 
704 	pw = conf->sae_passwords;
705 	conf->sae_passwords = NULL;
706 	while (pw) {
707 		tmp = pw;
708 		pw = pw->next;
709 		str_clear_free(tmp->password);
710 		os_free(tmp->identifier);
711 #ifdef CONFIG_SAE
712 		sae_deinit_pt(tmp->pt);
713 #endif /* CONFIG_SAE */
714 		os_free(tmp);
715 	}
716 }
717 
718 
719 #ifdef CONFIG_DPP2
hostapd_dpp_controller_conf_free(struct dpp_controller_conf * conf)720 static void hostapd_dpp_controller_conf_free(struct dpp_controller_conf *conf)
721 {
722 	struct dpp_controller_conf *prev;
723 
724 	while (conf) {
725 		prev = conf;
726 		conf = conf->next;
727 		os_free(prev);
728 	}
729 }
730 #endif /* CONFIG_DPP2 */
731 
732 
hostapd_config_free_bss(struct hostapd_bss_config * conf)733 void hostapd_config_free_bss(struct hostapd_bss_config *conf)
734 {
735 #if defined(CONFIG_WPS) || defined(CONFIG_HS20)
736 	size_t i;
737 #endif
738 
739 	if (conf == NULL)
740 		return;
741 
742 	hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
743 
744 	str_clear_free(conf->ssid.wpa_passphrase);
745 	os_free(conf->ssid.wpa_psk_file);
746 #ifdef CONFIG_WEP
747 	hostapd_config_free_wep(&conf->ssid.wep);
748 #endif /* CONFIG_WEP */
749 #ifdef CONFIG_FULL_DYNAMIC_VLAN
750 	os_free(conf->ssid.vlan_tagged_interface);
751 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
752 #ifdef CONFIG_SAE
753 	sae_deinit_pt(conf->ssid.pt);
754 #endif /* CONFIG_SAE */
755 
756 	hostapd_config_free_eap_users(conf->eap_user);
757 	os_free(conf->eap_user_sqlite);
758 
759 	os_free(conf->eap_req_id_text);
760 	os_free(conf->erp_domain);
761 	os_free(conf->accept_mac);
762 	os_free(conf->deny_mac);
763 	os_free(conf->nas_identifier);
764 	if (conf->radius) {
765 		hostapd_config_free_radius(conf->radius->auth_servers,
766 					   conf->radius->num_auth_servers);
767 		hostapd_config_free_radius(conf->radius->acct_servers,
768 					   conf->radius->num_acct_servers);
769 	}
770 	hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
771 	hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
772 	os_free(conf->radius_req_attr_sqlite);
773 	os_free(conf->rsn_preauth_interfaces);
774 	os_free(conf->ctrl_interface);
775 	os_free(conf->ca_cert);
776 	os_free(conf->server_cert);
777 	os_free(conf->server_cert2);
778 	os_free(conf->private_key);
779 	os_free(conf->private_key2);
780 	os_free(conf->private_key_passwd);
781 	os_free(conf->private_key_passwd2);
782 	os_free(conf->check_cert_subject);
783 	os_free(conf->ocsp_stapling_response);
784 	os_free(conf->ocsp_stapling_response_multi);
785 	os_free(conf->dh_file);
786 	os_free(conf->openssl_ciphers);
787 	os_free(conf->openssl_ecdh_curves);
788 	os_free(conf->pac_opaque_encr_key);
789 	os_free(conf->eap_fast_a_id);
790 	os_free(conf->eap_fast_a_id_info);
791 	os_free(conf->eap_sim_db);
792 	os_free(conf->radius_server_clients);
793 	os_free(conf->radius);
794 	os_free(conf->radius_das_shared_secret);
795 	hostapd_config_free_vlan(conf);
796 	os_free(conf->time_zone);
797 
798 #ifdef CONFIG_IEEE80211R_AP
799 	{
800 		struct ft_remote_r0kh *r0kh, *r0kh_prev;
801 		struct ft_remote_r1kh *r1kh, *r1kh_prev;
802 
803 		r0kh = conf->r0kh_list;
804 		conf->r0kh_list = NULL;
805 		while (r0kh) {
806 			r0kh_prev = r0kh;
807 			r0kh = r0kh->next;
808 			os_free(r0kh_prev);
809 		}
810 
811 		r1kh = conf->r1kh_list;
812 		conf->r1kh_list = NULL;
813 		while (r1kh) {
814 			r1kh_prev = r1kh;
815 			r1kh = r1kh->next;
816 			os_free(r1kh_prev);
817 		}
818 	}
819 #endif /* CONFIG_IEEE80211R_AP */
820 
821 #ifdef CONFIG_WPS
822 	os_free(conf->wps_pin_requests);
823 	os_free(conf->device_name);
824 	os_free(conf->manufacturer);
825 	os_free(conf->model_name);
826 	os_free(conf->model_number);
827 	os_free(conf->serial_number);
828 	os_free(conf->config_methods);
829 	os_free(conf->ap_pin);
830 	os_free(conf->extra_cred);
831 	os_free(conf->ap_settings);
832 	hostapd_config_clear_wpa_psk(&conf->multi_ap_backhaul_ssid.wpa_psk);
833 	str_clear_free(conf->multi_ap_backhaul_ssid.wpa_passphrase);
834 	os_free(conf->upnp_iface);
835 	os_free(conf->friendly_name);
836 	os_free(conf->manufacturer_url);
837 	os_free(conf->model_description);
838 	os_free(conf->model_url);
839 	os_free(conf->upc);
840 	for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
841 		wpabuf_free(conf->wps_vendor_ext[i]);
842 	wpabuf_free(conf->wps_application_ext);
843 	wpabuf_free(conf->wps_nfc_dh_pubkey);
844 	wpabuf_free(conf->wps_nfc_dh_privkey);
845 	wpabuf_free(conf->wps_nfc_dev_pw);
846 #endif /* CONFIG_WPS */
847 
848 	os_free(conf->roaming_consortium);
849 	os_free(conf->venue_name);
850 	os_free(conf->venue_url);
851 	os_free(conf->nai_realm_data);
852 	os_free(conf->network_auth_type);
853 	os_free(conf->anqp_3gpp_cell_net);
854 	os_free(conf->domain_name);
855 	hostapd_config_free_anqp_elem(conf);
856 
857 #ifdef CONFIG_RADIUS_TEST
858 	os_free(conf->dump_msk_file);
859 #endif /* CONFIG_RADIUS_TEST */
860 
861 #ifdef CONFIG_HS20
862 	os_free(conf->hs20_oper_friendly_name);
863 	os_free(conf->hs20_wan_metrics);
864 	os_free(conf->hs20_connection_capability);
865 	os_free(conf->hs20_operating_class);
866 	os_free(conf->hs20_icons);
867 	if (conf->hs20_osu_providers) {
868 		for (i = 0; i < conf->hs20_osu_providers_count; i++) {
869 			struct hs20_osu_provider *p;
870 			size_t j;
871 			p = &conf->hs20_osu_providers[i];
872 			os_free(p->friendly_name);
873 			os_free(p->server_uri);
874 			os_free(p->method_list);
875 			for (j = 0; j < p->icons_count; j++)
876 				os_free(p->icons[j]);
877 			os_free(p->icons);
878 			os_free(p->osu_nai);
879 			os_free(p->osu_nai2);
880 			os_free(p->service_desc);
881 		}
882 		os_free(conf->hs20_osu_providers);
883 	}
884 	if (conf->hs20_operator_icon) {
885 		for (i = 0; i < conf->hs20_operator_icon_count; i++)
886 			os_free(conf->hs20_operator_icon[i]);
887 		os_free(conf->hs20_operator_icon);
888 	}
889 	os_free(conf->subscr_remediation_url);
890 	os_free(conf->hs20_sim_provisioning_url);
891 	os_free(conf->t_c_filename);
892 	os_free(conf->t_c_server_url);
893 #endif /* CONFIG_HS20 */
894 
895 	wpabuf_free(conf->vendor_elements);
896 	wpabuf_free(conf->assocresp_elements);
897 
898 	os_free(conf->sae_groups);
899 #ifdef CONFIG_OWE
900 	os_free(conf->owe_groups);
901 #endif /* CONFIG_OWE */
902 
903 	os_free(conf->wowlan_triggers);
904 
905 	os_free(conf->server_id);
906 
907 #ifdef CONFIG_TESTING_OPTIONS
908 	wpabuf_free(conf->own_ie_override);
909 	wpabuf_free(conf->sae_commit_override);
910 	wpabuf_free(conf->rsne_override_eapol);
911 	wpabuf_free(conf->rsnxe_override_eapol);
912 	wpabuf_free(conf->rsne_override_ft);
913 	wpabuf_free(conf->rsnxe_override_ft);
914 	wpabuf_free(conf->gtk_rsc_override);
915 	wpabuf_free(conf->igtk_rsc_override);
916 #endif /* CONFIG_TESTING_OPTIONS */
917 
918 	os_free(conf->no_probe_resp_if_seen_on);
919 	os_free(conf->no_auth_if_seen_on);
920 
921 	hostapd_config_free_fils_realms(conf);
922 
923 #ifdef CONFIG_DPP
924 	os_free(conf->dpp_name);
925 	os_free(conf->dpp_mud_url);
926 	os_free(conf->dpp_connector);
927 	wpabuf_free(conf->dpp_netaccesskey);
928 	wpabuf_free(conf->dpp_csign);
929 #ifdef CONFIG_DPP2
930 	hostapd_dpp_controller_conf_free(conf->dpp_controller);
931 #endif /* CONFIG_DPP2 */
932 #endif /* CONFIG_DPP */
933 
934 	hostapd_config_free_sae_passwords(conf);
935 
936 #ifdef CONFIG_AIRTIME_POLICY
937 	{
938 		struct airtime_sta_weight *wt, *wt_prev;
939 
940 		wt = conf->airtime_weight_list;
941 		conf->airtime_weight_list = NULL;
942 		while (wt) {
943 			wt_prev = wt;
944 			wt = wt->next;
945 			os_free(wt_prev);
946 		}
947 	}
948 #endif /* CONFIG_AIRTIME_POLICY */
949 
950 	os_free(conf);
951 }
952 
953 
954 /**
955  * hostapd_config_free - Free hostapd configuration
956  * @conf: Configuration data from hostapd_config_read().
957  */
hostapd_config_free(struct hostapd_config * conf)958 void hostapd_config_free(struct hostapd_config *conf)
959 {
960 	size_t i;
961 
962 	if (conf == NULL)
963 		return;
964 
965 	for (i = 0; i < conf->num_bss; i++)
966 		hostapd_config_free_bss(conf->bss[i]);
967 	os_free(conf->bss);
968 	os_free(conf->supported_rates);
969 	os_free(conf->basic_rates);
970 	os_free(conf->acs_ch_list.range);
971 	os_free(conf->acs_freq_list.range);
972 	os_free(conf->driver_params);
973 #ifdef CONFIG_ACS
974 	os_free(conf->acs_chan_bias);
975 #endif /* CONFIG_ACS */
976 	wpabuf_free(conf->lci);
977 	wpabuf_free(conf->civic);
978 
979 	os_free(conf);
980 }
981 
982 
983 /**
984  * hostapd_maclist_found - Find a MAC address from a list
985  * @list: MAC address list
986  * @num_entries: Number of addresses in the list
987  * @addr: Address to search for
988  * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
989  * Returns: 1 if address is in the list or 0 if not.
990  *
991  * Perform a binary search for given MAC address from a pre-sorted list.
992  */
hostapd_maclist_found(struct mac_acl_entry * list,int num_entries,const u8 * addr,struct vlan_description * vlan_id)993 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
994 			  const u8 *addr, struct vlan_description *vlan_id)
995 {
996 	int start, end, middle, res;
997 
998 	start = 0;
999 	end = num_entries - 1;
1000 
1001 	while (start <= end) {
1002 		middle = (start + end) / 2;
1003 		res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
1004 		if (res == 0) {
1005 			if (vlan_id)
1006 				*vlan_id = list[middle].vlan_id;
1007 			return 1;
1008 		}
1009 		if (res < 0)
1010 			start = middle + 1;
1011 		else
1012 			end = middle - 1;
1013 	}
1014 
1015 	return 0;
1016 }
1017 
1018 
hostapd_rate_found(int * list,int rate)1019 int hostapd_rate_found(int *list, int rate)
1020 {
1021 	int i;
1022 
1023 	if (list == NULL)
1024 		return 0;
1025 
1026 	for (i = 0; list[i] >= 0; i++)
1027 		if (list[i] == rate)
1028 			return 1;
1029 
1030 	return 0;
1031 }
1032 
1033 
hostapd_vlan_valid(struct hostapd_vlan * vlan,struct vlan_description * vlan_desc)1034 int hostapd_vlan_valid(struct hostapd_vlan *vlan,
1035 		       struct vlan_description *vlan_desc)
1036 {
1037 	struct hostapd_vlan *v = vlan;
1038 	int i;
1039 
1040 	if (!vlan_desc->notempty || vlan_desc->untagged < 0 ||
1041 	    vlan_desc->untagged > MAX_VLAN_ID)
1042 		return 0;
1043 	for (i = 0; i < MAX_NUM_TAGGED_VLAN; i++) {
1044 		if (vlan_desc->tagged[i] < 0 ||
1045 		    vlan_desc->tagged[i] > MAX_VLAN_ID)
1046 			return 0;
1047 	}
1048 	if (!vlan_desc->untagged && !vlan_desc->tagged[0])
1049 		return 0;
1050 
1051 	while (v) {
1052 		if (!vlan_compare(&v->vlan_desc, vlan_desc) ||
1053 		    v->vlan_id == VLAN_ID_WILDCARD)
1054 			return 1;
1055 		v = v->next;
1056 	}
1057 	return 0;
1058 }
1059 
1060 
hostapd_get_vlan_id_ifname(struct hostapd_vlan * vlan,int vlan_id)1061 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
1062 {
1063 	struct hostapd_vlan *v = vlan;
1064 	while (v) {
1065 		if (v->vlan_id == vlan_id)
1066 			return v->ifname;
1067 		v = v->next;
1068 	}
1069 	return NULL;
1070 }
1071 
1072 
hostapd_get_psk(const struct hostapd_bss_config * conf,const u8 * addr,const u8 * p2p_dev_addr,const u8 * prev_psk,int * vlan_id)1073 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
1074 			   const u8 *addr, const u8 *p2p_dev_addr,
1075 			   const u8 *prev_psk, int *vlan_id)
1076 {
1077 	struct hostapd_wpa_psk *psk;
1078 	int next_ok = prev_psk == NULL;
1079 
1080 	if (vlan_id)
1081 		*vlan_id = 0;
1082 
1083 	if (p2p_dev_addr && !is_zero_ether_addr(p2p_dev_addr)) {
1084 		wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
1085 			   " p2p_dev_addr=" MACSTR " prev_psk=%p",
1086 			   MAC2STR(addr), MAC2STR(p2p_dev_addr), prev_psk);
1087 		addr = NULL; /* Use P2P Device Address for matching */
1088 	} else {
1089 		wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
1090 			   " prev_psk=%p",
1091 			   MAC2STR(addr), prev_psk);
1092 	}
1093 
1094 	for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
1095 		if (next_ok &&
1096 		    (psk->group ||
1097 		     (addr && os_memcmp(psk->addr, addr, ETH_ALEN) == 0) ||
1098 		     (!addr && p2p_dev_addr &&
1099 		      os_memcmp(psk->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
1100 		      0))) {
1101 			if (vlan_id)
1102 				*vlan_id = psk->vlan_id;
1103 			return psk->psk;
1104 		}
1105 
1106 		if (psk->psk == prev_psk)
1107 			next_ok = 1;
1108 	}
1109 
1110 	return NULL;
1111 }
1112 
1113 
hostapd_config_check_bss(struct hostapd_bss_config * bss,struct hostapd_config * conf,int full_config)1114 static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
1115 				    struct hostapd_config *conf,
1116 				    int full_config)
1117 {
1118 	if (full_config && bss->ieee802_1x && !bss->eap_server &&
1119 	    !bss->radius->auth_servers) {
1120 		wpa_printf(MSG_ERROR, "Invalid IEEE 802.1X configuration (no "
1121 			   "EAP authenticator configured).");
1122 		return -1;
1123 	}
1124 
1125 #ifdef CONFIG_WEP
1126 	if (bss->wpa) {
1127 		int wep, i;
1128 
1129 		wep = bss->default_wep_key_len > 0 ||
1130 		       bss->individual_wep_key_len > 0;
1131 		for (i = 0; i < NUM_WEP_KEYS; i++) {
1132 			if (bss->ssid.wep.keys_set) {
1133 				wep = 1;
1134 				break;
1135 			}
1136 		}
1137 
1138 		if (wep) {
1139 			wpa_printf(MSG_ERROR, "WEP configuration in a WPA network is not supported");
1140 			return -1;
1141 		}
1142 	}
1143 #endif /* CONFIG_WEP */
1144 
1145 	if (full_config && bss->wpa &&
1146 	    bss->wpa_psk_radius != PSK_RADIUS_IGNORED &&
1147 	    bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
1148 		wpa_printf(MSG_ERROR, "WPA-PSK using RADIUS enabled, but no "
1149 			   "RADIUS checking (macaddr_acl=2) enabled.");
1150 		return -1;
1151 	}
1152 
1153 	if (full_config && bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
1154 	    bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
1155 	    bss->ssid.wpa_psk_file == NULL &&
1156 	    (bss->wpa_psk_radius != PSK_RADIUS_REQUIRED ||
1157 	     bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH)) {
1158 		wpa_printf(MSG_ERROR, "WPA-PSK enabled, but PSK or passphrase "
1159 			   "is not configured.");
1160 		return -1;
1161 	}
1162 
1163 	if (full_config && !is_zero_ether_addr(bss->bssid)) {
1164 		size_t i;
1165 
1166 		for (i = 0; i < conf->num_bss; i++) {
1167 			if (conf->bss[i] != bss &&
1168 			    (hostapd_mac_comp(conf->bss[i]->bssid,
1169 					      bss->bssid) == 0)) {
1170 				wpa_printf(MSG_ERROR, "Duplicate BSSID " MACSTR
1171 					   " on interface '%s' and '%s'.",
1172 					   MAC2STR(bss->bssid),
1173 					   conf->bss[i]->iface, bss->iface);
1174 				return -1;
1175 			}
1176 		}
1177 	}
1178 
1179 #ifdef CONFIG_IEEE80211R_AP
1180 	if (full_config && wpa_key_mgmt_ft(bss->wpa_key_mgmt) &&
1181 	    (bss->nas_identifier == NULL ||
1182 	     os_strlen(bss->nas_identifier) < 1 ||
1183 	     os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
1184 		wpa_printf(MSG_ERROR, "FT (IEEE 802.11r) requires "
1185 			   "nas_identifier to be configured as a 1..48 octet "
1186 			   "string");
1187 		return -1;
1188 	}
1189 #endif /* CONFIG_IEEE80211R_AP */
1190 
1191 	if (full_config && conf->ieee80211n &&
1192 	    conf->hw_mode == HOSTAPD_MODE_IEEE80211B) {
1193 		bss->disable_11n = 1;
1194 		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) in 11b mode is not "
1195 			   "allowed, disabling HT capabilities");
1196 	}
1197 
1198 #ifdef CONFIG_WEP
1199 	if (full_config && conf->ieee80211n &&
1200 	    bss->ssid.security_policy == SECURITY_STATIC_WEP) {
1201 		bss->disable_11n = 1;
1202 		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WEP is not "
1203 			   "allowed, disabling HT capabilities");
1204 	}
1205 #endif /* CONFIG_WEP */
1206 
1207 	if (full_config && conf->ieee80211n && bss->wpa &&
1208 	    !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
1209 	    !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1210 				   WPA_CIPHER_CCMP_256 | WPA_CIPHER_GCMP_256)))
1211 	{
1212 		bss->disable_11n = 1;
1213 		wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WPA/WPA2 "
1214 			   "requires CCMP/GCMP to be enabled, disabling HT "
1215 			   "capabilities");
1216 	}
1217 
1218 #ifdef CONFIG_IEEE80211AC
1219 #ifdef CONFIG_WEP
1220 	if (full_config && conf->ieee80211ac &&
1221 	    bss->ssid.security_policy == SECURITY_STATIC_WEP) {
1222 		bss->disable_11ac = 1;
1223 		wpa_printf(MSG_ERROR,
1224 			   "VHT (IEEE 802.11ac) with WEP is not allowed, disabling VHT capabilities");
1225 	}
1226 #endif /* CONFIG_WEP */
1227 
1228 	if (full_config && conf->ieee80211ac && bss->wpa &&
1229 	    !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
1230 	    !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1231 				   WPA_CIPHER_CCMP_256 | WPA_CIPHER_GCMP_256)))
1232 	{
1233 		bss->disable_11ac = 1;
1234 		wpa_printf(MSG_ERROR,
1235 			   "VHT (IEEE 802.11ac) with WPA/WPA2 requires CCMP/GCMP to be enabled, disabling VHT capabilities");
1236 	}
1237 #endif /* CONFIG_IEEE80211AC */
1238 
1239 #ifdef CONFIG_WPS
1240 	if (full_config && bss->wps_state && bss->ignore_broadcast_ssid) {
1241 		wpa_printf(MSG_INFO, "WPS: ignore_broadcast_ssid "
1242 			   "configuration forced WPS to be disabled");
1243 		bss->wps_state = 0;
1244 	}
1245 
1246 #ifdef CONFIG_WEP
1247 	if (full_config && bss->wps_state &&
1248 	    bss->ssid.wep.keys_set && bss->wpa == 0) {
1249 		wpa_printf(MSG_INFO, "WPS: WEP configuration forced WPS to be "
1250 			   "disabled");
1251 		bss->wps_state = 0;
1252 	}
1253 #endif /* CONFIG_WEP */
1254 
1255 	if (full_config && bss->wps_state && bss->wpa &&
1256 	    (!(bss->wpa & 2) ||
1257 	     !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1258 				    WPA_CIPHER_CCMP_256 |
1259 				    WPA_CIPHER_GCMP_256)))) {
1260 		wpa_printf(MSG_INFO, "WPS: WPA/TKIP configuration without "
1261 			   "WPA2/CCMP/GCMP forced WPS to be disabled");
1262 		bss->wps_state = 0;
1263 	}
1264 #endif /* CONFIG_WPS */
1265 
1266 #ifdef CONFIG_HS20
1267 	if (full_config && bss->hs20 &&
1268 	    (!(bss->wpa & 2) ||
1269 	     !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1270 				    WPA_CIPHER_CCMP_256 |
1271 				    WPA_CIPHER_GCMP_256)))) {
1272 		wpa_printf(MSG_ERROR, "HS 2.0: WPA2-Enterprise/CCMP "
1273 			   "configuration is required for Hotspot 2.0 "
1274 			   "functionality");
1275 		return -1;
1276 	}
1277 #endif /* CONFIG_HS20 */
1278 
1279 #ifdef CONFIG_MBO
1280 	if (full_config && bss->mbo_enabled && (bss->wpa & 2) &&
1281 	    bss->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
1282 		wpa_printf(MSG_ERROR,
1283 			   "MBO: PMF needs to be enabled whenever using WPA2 with MBO");
1284 		return -1;
1285 	}
1286 #endif /* CONFIG_MBO */
1287 
1288 #ifdef CONFIG_OCV
1289 	if (full_config && bss->ieee80211w == NO_MGMT_FRAME_PROTECTION &&
1290 	    bss->ocv) {
1291 		wpa_printf(MSG_ERROR,
1292 			   "OCV: PMF needs to be enabled whenever using OCV");
1293 		return -1;
1294 	}
1295 #endif /* CONFIG_OCV */
1296 
1297 	return 0;
1298 }
1299 
1300 
hostapd_config_check_cw(struct hostapd_config * conf,int queue)1301 static int hostapd_config_check_cw(struct hostapd_config *conf, int queue)
1302 {
1303 	int tx_cwmin = conf->tx_queue[queue].cwmin;
1304 	int tx_cwmax = conf->tx_queue[queue].cwmax;
1305 	int ac_cwmin = conf->wmm_ac_params[queue].cwmin;
1306 	int ac_cwmax = conf->wmm_ac_params[queue].cwmax;
1307 
1308 	if (tx_cwmin > tx_cwmax) {
1309 		wpa_printf(MSG_ERROR,
1310 			   "Invalid TX queue cwMin/cwMax values. cwMin(%d) greater than cwMax(%d)",
1311 			   tx_cwmin, tx_cwmax);
1312 		return -1;
1313 	}
1314 	if (ac_cwmin > ac_cwmax) {
1315 		wpa_printf(MSG_ERROR,
1316 			   "Invalid WMM AC cwMin/cwMax values. cwMin(%d) greater than cwMax(%d)",
1317 			   ac_cwmin, ac_cwmax);
1318 		return -1;
1319 	}
1320 	return 0;
1321 }
1322 
1323 
hostapd_config_check(struct hostapd_config * conf,int full_config)1324 int hostapd_config_check(struct hostapd_config *conf, int full_config)
1325 {
1326 	size_t i;
1327 
1328 	if (full_config && conf->ieee80211d &&
1329 	    (!conf->country[0] || !conf->country[1])) {
1330 		wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11d without "
1331 			   "setting the country_code");
1332 		return -1;
1333 	}
1334 
1335 	if (full_config && conf->ieee80211h && !conf->ieee80211d) {
1336 		wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11h without "
1337 			   "IEEE 802.11d enabled");
1338 		return -1;
1339 	}
1340 
1341 	if (full_config && conf->local_pwr_constraint != -1 &&
1342 	    !conf->ieee80211d) {
1343 		wpa_printf(MSG_ERROR, "Cannot add Power Constraint element without Country element");
1344 		return -1;
1345 	}
1346 
1347 	if (full_config && conf->spectrum_mgmt_required &&
1348 	    conf->local_pwr_constraint == -1) {
1349 		wpa_printf(MSG_ERROR, "Cannot set Spectrum Management bit without Country and Power Constraint elements");
1350 		return -1;
1351 	}
1352 
1353 #ifdef CONFIG_AIRTIME_POLICY
1354 	if (full_config && conf->airtime_mode > AIRTIME_MODE_STATIC &&
1355 	    !conf->airtime_update_interval) {
1356 		wpa_printf(MSG_ERROR, "Airtime update interval cannot be zero");
1357 		return -1;
1358 	}
1359 #endif /* CONFIG_AIRTIME_POLICY */
1360 	for (i = 0; i < NUM_TX_QUEUES; i++) {
1361 		if (hostapd_config_check_cw(conf, i))
1362 			return -1;
1363 	}
1364 
1365 	for (i = 0; i < conf->num_bss; i++) {
1366 		if (hostapd_config_check_bss(conf->bss[i], conf, full_config))
1367 			return -1;
1368 	}
1369 
1370 	return 0;
1371 }
1372 
1373 
hostapd_set_security_params(struct hostapd_bss_config * bss,int full_config)1374 void hostapd_set_security_params(struct hostapd_bss_config *bss,
1375 				 int full_config)
1376 {
1377 #ifdef CONFIG_WEP
1378 	if (bss->individual_wep_key_len == 0) {
1379 		/* individual keys are not use; can use key idx0 for
1380 		 * broadcast keys */
1381 		bss->broadcast_key_idx_min = 0;
1382 	}
1383 #endif /* CONFIG_WEP */
1384 
1385 	if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
1386 		bss->rsn_pairwise = bss->wpa_pairwise;
1387 	if (bss->group_cipher)
1388 		bss->wpa_group = bss->group_cipher;
1389 	else
1390 		bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
1391 							    bss->wpa_pairwise,
1392 							    bss->rsn_pairwise);
1393 	if (!bss->wpa_group_rekey_set)
1394 		bss->wpa_group_rekey = bss->wpa_group == WPA_CIPHER_TKIP ?
1395 			600 : 86400;
1396 
1397 	if (full_config) {
1398 		bss->radius->auth_server = bss->radius->auth_servers;
1399 		bss->radius->acct_server = bss->radius->acct_servers;
1400 	}
1401 
1402 	if (bss->wpa && bss->ieee802_1x) {
1403 		bss->ssid.security_policy = SECURITY_WPA;
1404 	} else if (bss->wpa) {
1405 		bss->ssid.security_policy = SECURITY_WPA_PSK;
1406 	} else if (bss->ieee802_1x) {
1407 		int cipher = WPA_CIPHER_NONE;
1408 		bss->ssid.security_policy = SECURITY_IEEE_802_1X;
1409 #ifdef CONFIG_WEP
1410 		bss->ssid.wep.default_len = bss->default_wep_key_len;
1411 		if (full_config && bss->default_wep_key_len) {
1412 			cipher = bss->default_wep_key_len >= 13 ?
1413 				WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
1414 		} else if (full_config && bss->ssid.wep.keys_set) {
1415 			if (bss->ssid.wep.len[0] >= 13)
1416 				cipher = WPA_CIPHER_WEP104;
1417 			else
1418 				cipher = WPA_CIPHER_WEP40;
1419 		}
1420 #endif /* CONFIG_WEP */
1421 		bss->wpa_group = cipher;
1422 		bss->wpa_pairwise = cipher;
1423 		bss->rsn_pairwise = cipher;
1424 		if (full_config)
1425 			bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
1426 #ifdef CONFIG_WEP
1427 	} else if (bss->ssid.wep.keys_set) {
1428 		int cipher = WPA_CIPHER_WEP40;
1429 		if (bss->ssid.wep.len[0] >= 13)
1430 			cipher = WPA_CIPHER_WEP104;
1431 		bss->ssid.security_policy = SECURITY_STATIC_WEP;
1432 		bss->wpa_group = cipher;
1433 		bss->wpa_pairwise = cipher;
1434 		bss->rsn_pairwise = cipher;
1435 		if (full_config)
1436 			bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
1437 #endif /* CONFIG_WEP */
1438 	} else if (bss->osen) {
1439 		bss->ssid.security_policy = SECURITY_OSEN;
1440 		bss->wpa_group = WPA_CIPHER_CCMP;
1441 		bss->wpa_pairwise = 0;
1442 		bss->rsn_pairwise = WPA_CIPHER_CCMP;
1443 	} else {
1444 		bss->ssid.security_policy = SECURITY_PLAINTEXT;
1445 		if (full_config) {
1446 			bss->wpa_group = WPA_CIPHER_NONE;
1447 			bss->wpa_pairwise = WPA_CIPHER_NONE;
1448 			bss->rsn_pairwise = WPA_CIPHER_NONE;
1449 			bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
1450 		}
1451 	}
1452 }
1453 
1454 
hostapd_sae_pw_id_in_use(struct hostapd_bss_config * conf)1455 int hostapd_sae_pw_id_in_use(struct hostapd_bss_config *conf)
1456 {
1457 	int with_id = 0, without_id = 0;
1458 	struct sae_password_entry *pw;
1459 
1460 	if (conf->ssid.wpa_passphrase)
1461 		without_id = 1;
1462 
1463 	for (pw = conf->sae_passwords; pw; pw = pw->next) {
1464 		if (pw->identifier)
1465 			with_id = 1;
1466 		else
1467 			without_id = 1;
1468 		if (with_id && without_id)
1469 			break;
1470 	}
1471 
1472 	if (with_id && !without_id)
1473 		return 2;
1474 	return with_id;
1475 }
1476