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