1 /*
2 * hostapd / Configuration helper functions
3 * Copyright (c) 2003-2009, 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
92
hostapd_config_defaults(void)93 struct hostapd_config * hostapd_config_defaults(void)
94 {
95 #define ecw2cw(ecw) ((1 << (ecw)) - 1)
96
97 struct hostapd_config *conf;
98 struct hostapd_bss_config *bss;
99 const int aCWmin = 4, aCWmax = 10;
100 const struct hostapd_wmm_ac_params ac_bk =
101 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
102 const struct hostapd_wmm_ac_params ac_be =
103 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
104 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
105 { aCWmin - 1, aCWmin, 2, 3000 / 32, 1 };
106 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
107 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 1 };
108 const struct hostapd_tx_queue_params txq_bk =
109 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
110 const struct hostapd_tx_queue_params txq_be =
111 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
112 const struct hostapd_tx_queue_params txq_vi =
113 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
114 const struct hostapd_tx_queue_params txq_vo =
115 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
116 (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
117
118 #undef ecw2cw
119
120 conf = os_zalloc(sizeof(*conf));
121 bss = os_zalloc(sizeof(*bss));
122 if (conf == NULL || bss == NULL) {
123 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
124 "configuration data.");
125 os_free(conf);
126 os_free(bss);
127 return NULL;
128 }
129
130 bss->radius = os_zalloc(sizeof(*bss->radius));
131 if (bss->radius == NULL) {
132 os_free(conf);
133 os_free(bss);
134 return NULL;
135 }
136
137 hostapd_config_defaults_bss(bss);
138
139 conf->num_bss = 1;
140 conf->bss = bss;
141
142 conf->beacon_int = 100;
143 conf->rts_threshold = -1; /* use driver default: 2347 */
144 conf->fragm_threshold = -1; /* user driver default: 2346 */
145 conf->send_probe_response = 1;
146
147 conf->wmm_ac_params[0] = ac_be;
148 conf->wmm_ac_params[1] = ac_bk;
149 conf->wmm_ac_params[2] = ac_vi;
150 conf->wmm_ac_params[3] = ac_vo;
151
152 conf->tx_queue[0] = txq_vo;
153 conf->tx_queue[1] = txq_vi;
154 conf->tx_queue[2] = txq_be;
155 conf->tx_queue[3] = txq_bk;
156
157 conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
158
159 return conf;
160 }
161
162
hostapd_mac_comp(const void * a,const void * b)163 int hostapd_mac_comp(const void *a, const void *b)
164 {
165 return os_memcmp(a, b, sizeof(macaddr));
166 }
167
168
hostapd_mac_comp_empty(const void * a)169 int hostapd_mac_comp_empty(const void *a)
170 {
171 macaddr empty = { 0 };
172 return os_memcmp(a, empty, sizeof(macaddr));
173 }
174
175
hostapd_config_read_wpa_psk(const char * fname,struct hostapd_ssid * ssid)176 static int hostapd_config_read_wpa_psk(const char *fname,
177 struct hostapd_ssid *ssid)
178 {
179 FILE *f;
180 char buf[128], *pos;
181 int line = 0, ret = 0, len, ok;
182 u8 addr[ETH_ALEN];
183 struct hostapd_wpa_psk *psk;
184
185 if (!fname)
186 return 0;
187
188 f = fopen(fname, "r");
189 if (!f) {
190 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
191 return -1;
192 }
193
194 while (fgets(buf, sizeof(buf), f)) {
195 line++;
196
197 if (buf[0] == '#')
198 continue;
199 pos = buf;
200 while (*pos != '\0') {
201 if (*pos == '\n') {
202 *pos = '\0';
203 break;
204 }
205 pos++;
206 }
207 if (buf[0] == '\0')
208 continue;
209
210 if (hwaddr_aton(buf, addr)) {
211 wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
212 "line %d in '%s'", buf, line, fname);
213 ret = -1;
214 break;
215 }
216
217 psk = os_zalloc(sizeof(*psk));
218 if (psk == NULL) {
219 wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
220 ret = -1;
221 break;
222 }
223 if (is_zero_ether_addr(addr))
224 psk->group = 1;
225 else
226 os_memcpy(psk->addr, addr, ETH_ALEN);
227
228 pos = buf + 17;
229 if (*pos == '\0') {
230 wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
231 line, fname);
232 os_free(psk);
233 ret = -1;
234 break;
235 }
236 pos++;
237
238 ok = 0;
239 len = os_strlen(pos);
240 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
241 ok = 1;
242 else if (len >= 8 && len < 64) {
243 pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
244 4096, psk->psk, PMK_LEN);
245 ok = 1;
246 }
247 if (!ok) {
248 wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
249 "'%s'", pos, line, fname);
250 os_free(psk);
251 ret = -1;
252 break;
253 }
254
255 psk->next = ssid->wpa_psk;
256 ssid->wpa_psk = psk;
257 }
258
259 fclose(f);
260
261 return ret;
262 }
263
264
hostapd_derive_psk(struct hostapd_ssid * ssid)265 static int hostapd_derive_psk(struct hostapd_ssid *ssid)
266 {
267 ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
268 if (ssid->wpa_psk == NULL) {
269 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
270 return -1;
271 }
272 wpa_hexdump_ascii(MSG_DEBUG, "SSID",
273 (u8 *) ssid->ssid, ssid->ssid_len);
274 wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
275 (u8 *) ssid->wpa_passphrase,
276 os_strlen(ssid->wpa_passphrase));
277 pbkdf2_sha1(ssid->wpa_passphrase,
278 ssid->ssid, ssid->ssid_len,
279 4096, ssid->wpa_psk->psk, PMK_LEN);
280 wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
281 ssid->wpa_psk->psk, PMK_LEN);
282 return 0;
283 }
284
285
hostapd_setup_wpa_psk(struct hostapd_bss_config * conf)286 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
287 {
288 struct hostapd_ssid *ssid = &conf->ssid;
289
290 if (ssid->wpa_passphrase != NULL) {
291 if (ssid->wpa_psk != NULL) {
292 wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
293 "instead of passphrase");
294 } else {
295 wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
296 "passphrase");
297 if (hostapd_derive_psk(ssid) < 0)
298 return -1;
299 }
300 ssid->wpa_psk->group = 1;
301 }
302
303 if (ssid->wpa_psk_file) {
304 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
305 &conf->ssid))
306 return -1;
307 }
308
309 return 0;
310 }
311
312
hostapd_wep_key_cmp(struct hostapd_wep_keys * a,struct hostapd_wep_keys * b)313 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
314 {
315 int i;
316
317 if (a->idx != b->idx || a->default_len != b->default_len)
318 return 1;
319 for (i = 0; i < NUM_WEP_KEYS; i++)
320 if (a->len[i] != b->len[i] ||
321 os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
322 return 1;
323 return 0;
324 }
325
326
hostapd_config_free_radius(struct hostapd_radius_server * servers,int num_servers)327 static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
328 int num_servers)
329 {
330 int i;
331
332 for (i = 0; i < num_servers; i++) {
333 os_free(servers[i].shared_secret);
334 }
335 os_free(servers);
336 }
337
338
hostapd_config_free_eap_user(struct hostapd_eap_user * user)339 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
340 {
341 os_free(user->identity);
342 os_free(user->password);
343 os_free(user);
344 }
345
346
hostapd_config_free_wep(struct hostapd_wep_keys * keys)347 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
348 {
349 int i;
350 for (i = 0; i < NUM_WEP_KEYS; i++) {
351 os_free(keys->key[i]);
352 keys->key[i] = NULL;
353 }
354 }
355
356
hostapd_config_free_bss(struct hostapd_bss_config * conf)357 static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
358 {
359 struct hostapd_wpa_psk *psk, *prev;
360 struct hostapd_eap_user *user, *prev_user;
361
362 if (conf == NULL)
363 return;
364
365 psk = conf->ssid.wpa_psk;
366 while (psk) {
367 prev = psk;
368 psk = psk->next;
369 os_free(prev);
370 }
371
372 os_free(conf->ssid.wpa_passphrase);
373 os_free(conf->ssid.wpa_psk_file);
374 hostapd_config_free_wep(&conf->ssid.wep);
375 #ifdef CONFIG_FULL_DYNAMIC_VLAN
376 os_free(conf->ssid.vlan_tagged_interface);
377 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
378
379 user = conf->eap_user;
380 while (user) {
381 prev_user = user;
382 user = user->next;
383 hostapd_config_free_eap_user(prev_user);
384 }
385
386 os_free(conf->dump_log_name);
387 os_free(conf->eap_req_id_text);
388 os_free(conf->accept_mac);
389 os_free(conf->deny_mac);
390 os_free(conf->nas_identifier);
391 hostapd_config_free_radius(conf->radius->auth_servers,
392 conf->radius->num_auth_servers);
393 hostapd_config_free_radius(conf->radius->acct_servers,
394 conf->radius->num_acct_servers);
395 os_free(conf->rsn_preauth_interfaces);
396 os_free(conf->ctrl_interface);
397 os_free(conf->ca_cert);
398 os_free(conf->server_cert);
399 os_free(conf->private_key);
400 os_free(conf->private_key_passwd);
401 os_free(conf->dh_file);
402 os_free(conf->pac_opaque_encr_key);
403 os_free(conf->eap_fast_a_id);
404 os_free(conf->eap_fast_a_id_info);
405 os_free(conf->eap_sim_db);
406 os_free(conf->radius_server_clients);
407 os_free(conf->test_socket);
408 os_free(conf->radius);
409 hostapd_config_free_vlan(conf);
410 if (conf->ssid.dyn_vlan_keys) {
411 struct hostapd_ssid *ssid = &conf->ssid;
412 size_t i;
413 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
414 if (ssid->dyn_vlan_keys[i] == NULL)
415 continue;
416 hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
417 os_free(ssid->dyn_vlan_keys[i]);
418 }
419 os_free(ssid->dyn_vlan_keys);
420 ssid->dyn_vlan_keys = NULL;
421 }
422
423 os_free(conf->time_zone);
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 */
447
448 #ifdef ANDROID_P2P
449 os_free(conf->prioritize);
450 #endif
451 #ifdef CONFIG_WPS
452 os_free(conf->wps_pin_requests);
453 os_free(conf->device_name);
454 os_free(conf->manufacturer);
455 os_free(conf->model_name);
456 os_free(conf->model_number);
457 os_free(conf->serial_number);
458 os_free(conf->config_methods);
459 os_free(conf->ap_pin);
460 os_free(conf->extra_cred);
461 os_free(conf->ap_settings);
462 os_free(conf->upnp_iface);
463 os_free(conf->friendly_name);
464 os_free(conf->manufacturer_url);
465 os_free(conf->model_description);
466 os_free(conf->model_url);
467 os_free(conf->upc);
468 #endif /* CONFIG_WPS */
469
470 os_free(conf->roaming_consortium);
471
472 #ifdef CONFIG_RADIUS_TEST
473 os_free(conf->dump_msk_file);
474 #endif /* CONFIG_RADIUS_TEST */
475 }
476
477
478 /**
479 * hostapd_config_free - Free hostapd configuration
480 * @conf: Configuration data from hostapd_config_read().
481 */
hostapd_config_free(struct hostapd_config * conf)482 void hostapd_config_free(struct hostapd_config *conf)
483 {
484 size_t i;
485
486 if (conf == NULL)
487 return;
488
489 for (i = 0; i < conf->num_bss; i++)
490 hostapd_config_free_bss(&conf->bss[i]);
491 os_free(conf->bss);
492 os_free(conf->supported_rates);
493 os_free(conf->basic_rates);
494
495 os_free(conf);
496 }
497
498
499 /**
500 * hostapd_maclist_found - Find a MAC address from a list
501 * @list: MAC address list
502 * @num_entries: Number of addresses in the list
503 * @addr: Address to search for
504 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
505 * Returns: 1 if address is in the list or 0 if not.
506 *
507 * Perform a binary search for given MAC address from a pre-sorted list.
508 */
hostapd_maclist_found(struct mac_acl_entry * list,int num_entries,const u8 * addr,int * vlan_id)509 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
510 const u8 *addr, int *vlan_id)
511 {
512 int start, end, middle, res;
513
514 start = 0;
515 end = num_entries - 1;
516
517 while (start <= end) {
518 middle = (start + end) / 2;
519 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
520 if (res == 0) {
521 if (vlan_id)
522 *vlan_id = list[middle].vlan_id;
523 return 1;
524 }
525 if (res < 0)
526 start = middle + 1;
527 else
528 end = middle - 1;
529 }
530
531 return 0;
532 }
533
534
hostapd_rate_found(int * list,int rate)535 int hostapd_rate_found(int *list, int rate)
536 {
537 int i;
538
539 if (list == NULL)
540 return 0;
541
542 for (i = 0; list[i] >= 0; i++)
543 if (list[i] == rate)
544 return 1;
545
546 return 0;
547 }
548
549
hostapd_get_vlan_id_ifname(struct hostapd_vlan * vlan,int vlan_id)550 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
551 {
552 struct hostapd_vlan *v = vlan;
553 while (v) {
554 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
555 return v->ifname;
556 v = v->next;
557 }
558 return NULL;
559 }
560
561
hostapd_get_psk(const struct hostapd_bss_config * conf,const u8 * addr,const u8 * prev_psk)562 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
563 const u8 *addr, const u8 *prev_psk)
564 {
565 struct hostapd_wpa_psk *psk;
566 int next_ok = prev_psk == NULL;
567
568 for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
569 if (next_ok &&
570 (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
571 return psk->psk;
572
573 if (psk->psk == prev_psk)
574 next_ok = 1;
575 }
576
577 return NULL;
578 }
579
580
581 const struct hostapd_eap_user *
hostapd_get_eap_user(const struct hostapd_bss_config * conf,const u8 * identity,size_t identity_len,int phase2)582 hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
583 size_t identity_len, int phase2)
584 {
585 struct hostapd_eap_user *user = conf->eap_user;
586
587 #ifdef CONFIG_WPS
588 if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
589 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
590 static struct hostapd_eap_user wsc_enrollee;
591 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
592 wsc_enrollee.methods[0].method = eap_server_get_type(
593 "WSC", &wsc_enrollee.methods[0].vendor);
594 return &wsc_enrollee;
595 }
596
597 if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
598 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
599 static struct hostapd_eap_user wsc_registrar;
600 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
601 wsc_registrar.methods[0].method = eap_server_get_type(
602 "WSC", &wsc_registrar.methods[0].vendor);
603 wsc_registrar.password = (u8 *) conf->ap_pin;
604 wsc_registrar.password_len = conf->ap_pin ?
605 os_strlen(conf->ap_pin) : 0;
606 return &wsc_registrar;
607 }
608 #endif /* CONFIG_WPS */
609
610 while (user) {
611 if (!phase2 && user->identity == NULL) {
612 /* Wildcard match */
613 break;
614 }
615
616 if (user->phase2 == !!phase2 && user->wildcard_prefix &&
617 identity_len >= user->identity_len &&
618 os_memcmp(user->identity, identity, user->identity_len) ==
619 0) {
620 /* Wildcard prefix match */
621 break;
622 }
623
624 if (user->phase2 == !!phase2 &&
625 user->identity_len == identity_len &&
626 os_memcmp(user->identity, identity, identity_len) == 0)
627 break;
628 user = user->next;
629 }
630
631 return user;
632 }
633