• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / Configuration helper functions
3  * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "crypto/sha1.h"
13 #include "radius/radius_client.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/eapol_common.h"
16 #include "eap_common/eap_wsc_common.h"
17 #include "eap_server/eap.h"
18 #include "wpa_auth.h"
19 #include "sta_info.h"
20 #include "ap_config.h"
21 
22 
hostapd_config_free_vlan(struct hostapd_bss_config * bss)23 static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
24 {
25 	struct hostapd_vlan *vlan, *prev;
26 
27 	vlan = bss->vlan;
28 	prev = NULL;
29 	while (vlan) {
30 		prev = vlan;
31 		vlan = vlan->next;
32 		os_free(prev);
33 	}
34 
35 	bss->vlan = NULL;
36 }
37 
38 
hostapd_config_defaults_bss(struct hostapd_bss_config * bss)39 void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
40 {
41 	bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
42 	bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
43 	bss->logger_syslog = (unsigned int) -1;
44 	bss->logger_stdout = (unsigned int) -1;
45 
46 	bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
47 
48 	bss->wep_rekeying_period = 300;
49 	/* use key0 in individual key and key1 in broadcast key */
50 	bss->broadcast_key_idx_min = 1;
51 	bss->broadcast_key_idx_max = 2;
52 	bss->eap_reauth_period = 3600;
53 
54 	bss->wpa_group_rekey = 600;
55 	bss->wpa_gmk_rekey = 86400;
56 	bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
57 	bss->wpa_pairwise = WPA_CIPHER_TKIP;
58 	bss->wpa_group = WPA_CIPHER_TKIP;
59 	bss->rsn_pairwise = 0;
60 
61 	bss->max_num_sta = MAX_STA_COUNT;
62 
63 	bss->dtim_period = 2;
64 
65 	bss->radius_server_auth_port = 1812;
66 	bss->ap_max_inactivity = AP_MAX_INACTIVITY;
67 	bss->eapol_version = EAPOL_VERSION;
68 
69 	bss->max_listen_interval = 65535;
70 
71 	bss->pwd_group = 19; /* ECC: GF(p=256) */
72 
73 #ifdef CONFIG_IEEE80211W
74 	bss->assoc_sa_query_max_timeout = 1000;
75 	bss->assoc_sa_query_retry_timeout = 201;
76 #endif /* CONFIG_IEEE80211W */
77 #ifdef EAP_SERVER_FAST
78 	 /* both anonymous and authenticated provisioning */
79 	bss->eap_fast_prov = 3;
80 	bss->pac_key_lifetime = 7 * 24 * 60 * 60;
81 	bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
82 #endif /* EAP_SERVER_FAST */
83 
84 	/* Set to -1 as defaults depends on HT in setup */
85 	bss->wmm_enabled = -1;
86 
87 #ifdef CONFIG_IEEE80211R
88 	bss->ft_over_ds = 1;
89 #endif /* CONFIG_IEEE80211R */
90 
91 	bss->radius_das_time_window = 300;
92 
93 	bss->sae_anti_clogging_threshold = 5;
94 }
95 
96 
hostapd_config_defaults(void)97 struct hostapd_config * hostapd_config_defaults(void)
98 {
99 #define ecw2cw(ecw) ((1 << (ecw)) - 1)
100 
101 	struct hostapd_config *conf;
102 	struct hostapd_bss_config *bss;
103 	const int aCWmin = 4, aCWmax = 10;
104 	const struct hostapd_wmm_ac_params ac_bk =
105 		{ aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
106 	const struct hostapd_wmm_ac_params ac_be =
107 		{ aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
108 	const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
109 		{ aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
110 	const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
111 		{ aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
112 	const struct hostapd_tx_queue_params txq_bk =
113 		{ 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
114 	const struct hostapd_tx_queue_params txq_be =
115 		{ 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
116 	const struct hostapd_tx_queue_params txq_vi =
117 		{ 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
118 	const struct hostapd_tx_queue_params txq_vo =
119 		{ 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
120 		  (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
121 
122 #undef ecw2cw
123 
124 	conf = os_zalloc(sizeof(*conf));
125 	bss = os_zalloc(sizeof(*bss));
126 	if (conf == NULL || bss == NULL) {
127 		wpa_printf(MSG_ERROR, "Failed to allocate memory for "
128 			   "configuration data.");
129 		os_free(conf);
130 		os_free(bss);
131 		return NULL;
132 	}
133 
134 	bss->radius = os_zalloc(sizeof(*bss->radius));
135 	if (bss->radius == NULL) {
136 		os_free(conf);
137 		os_free(bss);
138 		return NULL;
139 	}
140 
141 	hostapd_config_defaults_bss(bss);
142 
143 	conf->num_bss = 1;
144 	conf->bss = bss;
145 
146 	conf->beacon_int = 100;
147 	conf->rts_threshold = -1; /* use driver default: 2347 */
148 	conf->fragm_threshold = -1; /* user driver default: 2346 */
149 	conf->send_probe_response = 1;
150 
151 	conf->wmm_ac_params[0] = ac_be;
152 	conf->wmm_ac_params[1] = ac_bk;
153 	conf->wmm_ac_params[2] = ac_vi;
154 	conf->wmm_ac_params[3] = ac_vo;
155 
156 	conf->tx_queue[0] = txq_vo;
157 	conf->tx_queue[1] = txq_vi;
158 	conf->tx_queue[2] = txq_be;
159 	conf->tx_queue[3] = txq_bk;
160 
161 	conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
162 
163 	conf->ap_table_max_size = 255;
164 	conf->ap_table_expiration_time = 60;
165 
166 	return conf;
167 }
168 
169 
hostapd_mac_comp(const void * a,const void * b)170 int hostapd_mac_comp(const void *a, const void *b)
171 {
172 	return os_memcmp(a, b, sizeof(macaddr));
173 }
174 
175 
hostapd_mac_comp_empty(const void * a)176 int hostapd_mac_comp_empty(const void *a)
177 {
178 	macaddr empty = { 0 };
179 	return os_memcmp(a, empty, sizeof(macaddr));
180 }
181 
182 
hostapd_config_read_wpa_psk(const char * fname,struct hostapd_ssid * ssid)183 static int hostapd_config_read_wpa_psk(const char *fname,
184 				       struct hostapd_ssid *ssid)
185 {
186 	FILE *f;
187 	char buf[128], *pos;
188 	int line = 0, ret = 0, len, ok;
189 	u8 addr[ETH_ALEN];
190 	struct hostapd_wpa_psk *psk;
191 
192 	if (!fname)
193 		return 0;
194 
195 	f = fopen(fname, "r");
196 	if (!f) {
197 		wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
198 		return -1;
199 	}
200 
201 	while (fgets(buf, sizeof(buf), f)) {
202 		line++;
203 
204 		if (buf[0] == '#')
205 			continue;
206 		pos = buf;
207 		while (*pos != '\0') {
208 			if (*pos == '\n') {
209 				*pos = '\0';
210 				break;
211 			}
212 			pos++;
213 		}
214 		if (buf[0] == '\0')
215 			continue;
216 
217 		if (hwaddr_aton(buf, addr)) {
218 			wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
219 				   "line %d in '%s'", buf, line, fname);
220 			ret = -1;
221 			break;
222 		}
223 
224 		psk = os_zalloc(sizeof(*psk));
225 		if (psk == NULL) {
226 			wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
227 			ret = -1;
228 			break;
229 		}
230 		if (is_zero_ether_addr(addr))
231 			psk->group = 1;
232 		else
233 			os_memcpy(psk->addr, addr, ETH_ALEN);
234 
235 		pos = buf + 17;
236 		if (*pos == '\0') {
237 			wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
238 				   line, fname);
239 			os_free(psk);
240 			ret = -1;
241 			break;
242 		}
243 		pos++;
244 
245 		ok = 0;
246 		len = os_strlen(pos);
247 		if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
248 			ok = 1;
249 		else if (len >= 8 && len < 64) {
250 			pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
251 				    4096, psk->psk, PMK_LEN);
252 			ok = 1;
253 		}
254 		if (!ok) {
255 			wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
256 				   "'%s'", pos, line, fname);
257 			os_free(psk);
258 			ret = -1;
259 			break;
260 		}
261 
262 		psk->next = ssid->wpa_psk;
263 		ssid->wpa_psk = psk;
264 	}
265 
266 	fclose(f);
267 
268 	return ret;
269 }
270 
271 
hostapd_derive_psk(struct hostapd_ssid * ssid)272 static int hostapd_derive_psk(struct hostapd_ssid *ssid)
273 {
274 	ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
275 	if (ssid->wpa_psk == NULL) {
276 		wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
277 		return -1;
278 	}
279 	wpa_hexdump_ascii(MSG_DEBUG, "SSID",
280 			  (u8 *) ssid->ssid, ssid->ssid_len);
281 	wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
282 			      (u8 *) ssid->wpa_passphrase,
283 			      os_strlen(ssid->wpa_passphrase));
284 	pbkdf2_sha1(ssid->wpa_passphrase,
285 		    ssid->ssid, ssid->ssid_len,
286 		    4096, ssid->wpa_psk->psk, PMK_LEN);
287 	wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
288 			ssid->wpa_psk->psk, PMK_LEN);
289 	return 0;
290 }
291 
292 
hostapd_setup_wpa_psk(struct hostapd_bss_config * conf)293 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
294 {
295 	struct hostapd_ssid *ssid = &conf->ssid;
296 
297 	if (ssid->wpa_passphrase != NULL) {
298 		if (ssid->wpa_psk != NULL) {
299 			wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
300 				   "instead of passphrase");
301 		} else {
302 			wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
303 				   "passphrase");
304 			if (hostapd_derive_psk(ssid) < 0)
305 				return -1;
306 		}
307 		ssid->wpa_psk->group = 1;
308 	}
309 
310 	if (ssid->wpa_psk_file) {
311 		if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
312 						&conf->ssid))
313 			return -1;
314 	}
315 
316 	return 0;
317 }
318 
319 
hostapd_wep_key_cmp(struct hostapd_wep_keys * a,struct hostapd_wep_keys * b)320 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
321 {
322 	int i;
323 
324 	if (a->idx != b->idx || a->default_len != b->default_len)
325 		return 1;
326 	for (i = 0; i < NUM_WEP_KEYS; i++)
327 		if (a->len[i] != b->len[i] ||
328 		    os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
329 			return 1;
330 	return 0;
331 }
332 
333 
hostapd_config_free_radius(struct hostapd_radius_server * servers,int num_servers)334 static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
335 				       int num_servers)
336 {
337 	int i;
338 
339 	for (i = 0; i < num_servers; i++) {
340 		os_free(servers[i].shared_secret);
341 	}
342 	os_free(servers);
343 }
344 
345 
346 struct hostapd_radius_attr *
hostapd_config_get_radius_attr(struct hostapd_radius_attr * attr,u8 type)347 hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
348 {
349 	for (; attr; attr = attr->next) {
350 		if (attr->type == type)
351 			return attr;
352 	}
353 	return NULL;
354 }
355 
356 
hostapd_config_free_radius_attr(struct hostapd_radius_attr * attr)357 static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
358 {
359 	struct hostapd_radius_attr *prev;
360 
361 	while (attr) {
362 		prev = attr;
363 		attr = attr->next;
364 		wpabuf_free(prev->val);
365 		os_free(prev);
366 	}
367 }
368 
369 
hostapd_config_free_eap_user(struct hostapd_eap_user * user)370 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
371 {
372 	os_free(user->identity);
373 	os_free(user->password);
374 	os_free(user);
375 }
376 
377 
hostapd_config_free_wep(struct hostapd_wep_keys * keys)378 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
379 {
380 	int i;
381 	for (i = 0; i < NUM_WEP_KEYS; i++) {
382 		os_free(keys->key[i]);
383 		keys->key[i] = NULL;
384 	}
385 }
386 
387 
hostapd_config_free_bss(struct hostapd_bss_config * conf)388 static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
389 {
390 	struct hostapd_wpa_psk *psk, *prev;
391 	struct hostapd_eap_user *user, *prev_user;
392 
393 	if (conf == NULL)
394 		return;
395 
396 	psk = conf->ssid.wpa_psk;
397 	while (psk) {
398 		prev = psk;
399 		psk = psk->next;
400 		os_free(prev);
401 	}
402 
403 	os_free(conf->ssid.wpa_passphrase);
404 	os_free(conf->ssid.wpa_psk_file);
405 	hostapd_config_free_wep(&conf->ssid.wep);
406 #ifdef CONFIG_FULL_DYNAMIC_VLAN
407 	os_free(conf->ssid.vlan_tagged_interface);
408 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
409 
410 	user = conf->eap_user;
411 	while (user) {
412 		prev_user = user;
413 		user = user->next;
414 		hostapd_config_free_eap_user(prev_user);
415 	}
416 	os_free(conf->eap_user_sqlite);
417 
418 	os_free(conf->dump_log_name);
419 	os_free(conf->eap_req_id_text);
420 	os_free(conf->accept_mac);
421 	os_free(conf->deny_mac);
422 	os_free(conf->nas_identifier);
423 	hostapd_config_free_radius(conf->radius->auth_servers,
424 				   conf->radius->num_auth_servers);
425 	hostapd_config_free_radius(conf->radius->acct_servers,
426 				   conf->radius->num_acct_servers);
427 	hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
428 	hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
429 	os_free(conf->rsn_preauth_interfaces);
430 	os_free(conf->ctrl_interface);
431 	os_free(conf->ca_cert);
432 	os_free(conf->server_cert);
433 	os_free(conf->private_key);
434 	os_free(conf->private_key_passwd);
435 	os_free(conf->dh_file);
436 	os_free(conf->pac_opaque_encr_key);
437 	os_free(conf->eap_fast_a_id);
438 	os_free(conf->eap_fast_a_id_info);
439 	os_free(conf->eap_sim_db);
440 	os_free(conf->radius_server_clients);
441 	os_free(conf->test_socket);
442 	os_free(conf->radius);
443 	os_free(conf->radius_das_shared_secret);
444 	hostapd_config_free_vlan(conf);
445 	if (conf->ssid.dyn_vlan_keys) {
446 		struct hostapd_ssid *ssid = &conf->ssid;
447 		size_t i;
448 		for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
449 			if (ssid->dyn_vlan_keys[i] == NULL)
450 				continue;
451 			hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
452 			os_free(ssid->dyn_vlan_keys[i]);
453 		}
454 		os_free(ssid->dyn_vlan_keys);
455 		ssid->dyn_vlan_keys = NULL;
456 	}
457 
458 	os_free(conf->time_zone);
459 
460 #ifdef CONFIG_IEEE80211R
461 	{
462 		struct ft_remote_r0kh *r0kh, *r0kh_prev;
463 		struct ft_remote_r1kh *r1kh, *r1kh_prev;
464 
465 		r0kh = conf->r0kh_list;
466 		conf->r0kh_list = NULL;
467 		while (r0kh) {
468 			r0kh_prev = r0kh;
469 			r0kh = r0kh->next;
470 			os_free(r0kh_prev);
471 		}
472 
473 		r1kh = conf->r1kh_list;
474 		conf->r1kh_list = NULL;
475 		while (r1kh) {
476 			r1kh_prev = r1kh;
477 			r1kh = r1kh->next;
478 			os_free(r1kh_prev);
479 		}
480 	}
481 #endif /* CONFIG_IEEE80211R */
482 
483 #ifdef CONFIG_WPS
484 	os_free(conf->wps_pin_requests);
485 	os_free(conf->device_name);
486 	os_free(conf->manufacturer);
487 	os_free(conf->model_name);
488 	os_free(conf->model_number);
489 	os_free(conf->serial_number);
490 	os_free(conf->config_methods);
491 	os_free(conf->ap_pin);
492 	os_free(conf->extra_cred);
493 	os_free(conf->ap_settings);
494 	os_free(conf->upnp_iface);
495 	os_free(conf->friendly_name);
496 	os_free(conf->manufacturer_url);
497 	os_free(conf->model_description);
498 	os_free(conf->model_url);
499 	os_free(conf->upc);
500 	wpabuf_free(conf->wps_nfc_dh_pubkey);
501 	wpabuf_free(conf->wps_nfc_dh_privkey);
502 	wpabuf_free(conf->wps_nfc_dev_pw);
503 #endif /* CONFIG_WPS */
504 
505 	os_free(conf->roaming_consortium);
506 	os_free(conf->venue_name);
507 	os_free(conf->nai_realm_data);
508 	os_free(conf->network_auth_type);
509 	os_free(conf->anqp_3gpp_cell_net);
510 	os_free(conf->domain_name);
511 
512 #ifdef CONFIG_RADIUS_TEST
513 	os_free(conf->dump_msk_file);
514 #endif /* CONFIG_RADIUS_TEST */
515 
516 #ifdef CONFIG_HS20
517 	os_free(conf->hs20_oper_friendly_name);
518 	os_free(conf->hs20_wan_metrics);
519 	os_free(conf->hs20_connection_capability);
520 	os_free(conf->hs20_operating_class);
521 #endif /* CONFIG_HS20 */
522 
523 	wpabuf_free(conf->vendor_elements);
524 
525 	os_free(conf->sae_groups);
526 }
527 
528 
529 /**
530  * hostapd_config_free - Free hostapd configuration
531  * @conf: Configuration data from hostapd_config_read().
532  */
hostapd_config_free(struct hostapd_config * conf)533 void hostapd_config_free(struct hostapd_config *conf)
534 {
535 	size_t i;
536 
537 	if (conf == NULL)
538 		return;
539 
540 	for (i = 0; i < conf->num_bss; i++)
541 		hostapd_config_free_bss(&conf->bss[i]);
542 	os_free(conf->bss);
543 	os_free(conf->supported_rates);
544 	os_free(conf->basic_rates);
545 
546 	os_free(conf);
547 }
548 
549 
550 /**
551  * hostapd_maclist_found - Find a MAC address from a list
552  * @list: MAC address list
553  * @num_entries: Number of addresses in the list
554  * @addr: Address to search for
555  * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
556  * Returns: 1 if address is in the list or 0 if not.
557  *
558  * Perform a binary search for given MAC address from a pre-sorted list.
559  */
hostapd_maclist_found(struct mac_acl_entry * list,int num_entries,const u8 * addr,int * vlan_id)560 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
561 			  const u8 *addr, int *vlan_id)
562 {
563 	int start, end, middle, res;
564 
565 	start = 0;
566 	end = num_entries - 1;
567 
568 	while (start <= end) {
569 		middle = (start + end) / 2;
570 		res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
571 		if (res == 0) {
572 			if (vlan_id)
573 				*vlan_id = list[middle].vlan_id;
574 			return 1;
575 		}
576 		if (res < 0)
577 			start = middle + 1;
578 		else
579 			end = middle - 1;
580 	}
581 
582 	return 0;
583 }
584 
585 
hostapd_rate_found(int * list,int rate)586 int hostapd_rate_found(int *list, int rate)
587 {
588 	int i;
589 
590 	if (list == NULL)
591 		return 0;
592 
593 	for (i = 0; list[i] >= 0; i++)
594 		if (list[i] == rate)
595 			return 1;
596 
597 	return 0;
598 }
599 
600 
hostapd_get_vlan_id_ifname(struct hostapd_vlan * vlan,int vlan_id)601 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
602 {
603 	struct hostapd_vlan *v = vlan;
604 	while (v) {
605 		if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
606 			return v->ifname;
607 		v = v->next;
608 	}
609 	return NULL;
610 }
611 
612 
hostapd_get_psk(const struct hostapd_bss_config * conf,const u8 * addr,const u8 * prev_psk)613 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
614 			   const u8 *addr, const u8 *prev_psk)
615 {
616 	struct hostapd_wpa_psk *psk;
617 	int next_ok = prev_psk == NULL;
618 
619 	for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
620 		if (next_ok &&
621 		    (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
622 			return psk->psk;
623 
624 		if (psk->psk == prev_psk)
625 			next_ok = 1;
626 	}
627 
628 	return NULL;
629 }
630