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