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