Line data Source code
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 "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 :
23 1492 : static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
24 : {
25 : struct hostapd_vlan *vlan, *prev;
26 :
27 1492 : vlan = bss->vlan;
28 1492 : prev = NULL;
29 2991 : while (vlan) {
30 7 : prev = vlan;
31 7 : vlan = vlan->next;
32 7 : os_free(prev);
33 : }
34 :
35 1492 : bss->vlan = NULL;
36 1492 : }
37 :
38 :
39 1492 : void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
40 : {
41 1492 : bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
42 1492 : bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
43 1492 : bss->logger_syslog = (unsigned int) -1;
44 1492 : bss->logger_stdout = (unsigned int) -1;
45 :
46 1492 : bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
47 :
48 1492 : bss->wep_rekeying_period = 300;
49 : /* use key0 in individual key and key1 in broadcast key */
50 1492 : bss->broadcast_key_idx_min = 1;
51 1492 : bss->broadcast_key_idx_max = 2;
52 1492 : bss->eap_reauth_period = 3600;
53 :
54 1492 : bss->wpa_group_rekey = 600;
55 1492 : bss->wpa_gmk_rekey = 86400;
56 1492 : bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
57 1492 : bss->wpa_pairwise = WPA_CIPHER_TKIP;
58 1492 : bss->wpa_group = WPA_CIPHER_TKIP;
59 1492 : bss->rsn_pairwise = 0;
60 :
61 1492 : bss->max_num_sta = MAX_STA_COUNT;
62 :
63 1492 : bss->dtim_period = 2;
64 :
65 1492 : bss->radius_server_auth_port = 1812;
66 1492 : bss->ap_max_inactivity = AP_MAX_INACTIVITY;
67 1492 : bss->eapol_version = EAPOL_VERSION;
68 :
69 1492 : bss->max_listen_interval = 65535;
70 :
71 1492 : bss->pwd_group = 19; /* ECC: GF(p=256) */
72 :
73 : #ifdef CONFIG_IEEE80211W
74 1492 : bss->assoc_sa_query_max_timeout = 1000;
75 1492 : bss->assoc_sa_query_retry_timeout = 201;
76 1492 : bss->group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
77 : #endif /* CONFIG_IEEE80211W */
78 : #ifdef EAP_SERVER_FAST
79 : /* both anonymous and authenticated provisioning */
80 1147 : bss->eap_fast_prov = 3;
81 1147 : bss->pac_key_lifetime = 7 * 24 * 60 * 60;
82 1147 : bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
83 : #endif /* EAP_SERVER_FAST */
84 :
85 : /* Set to -1 as defaults depends on HT in setup */
86 1492 : bss->wmm_enabled = -1;
87 :
88 : #ifdef CONFIG_IEEE80211R
89 1492 : bss->ft_over_ds = 1;
90 : #endif /* CONFIG_IEEE80211R */
91 :
92 1492 : bss->radius_das_time_window = 300;
93 :
94 1492 : bss->sae_anti_clogging_threshold = 5;
95 1492 : }
96 :
97 :
98 1500 : struct hostapd_config * hostapd_config_defaults(void)
99 : {
100 : #define ecw2cw(ecw) ((1 << (ecw)) - 1)
101 :
102 : struct hostapd_config *conf;
103 : struct hostapd_bss_config *bss;
104 1500 : const int aCWmin = 4, aCWmax = 10;
105 1500 : const struct hostapd_wmm_ac_params ac_bk =
106 : { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
107 1500 : const struct hostapd_wmm_ac_params ac_be =
108 : { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
109 1500 : const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
110 1500 : { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
111 3000 : const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
112 3000 : { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
113 4500 : const struct hostapd_tx_queue_params txq_bk =
114 3000 : { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
115 4500 : const struct hostapd_tx_queue_params txq_be =
116 3000 : { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
117 4500 : const struct hostapd_tx_queue_params txq_vi =
118 3000 : { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
119 4500 : const struct hostapd_tx_queue_params txq_vo =
120 1500 : { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
121 1500 : (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
122 :
123 : #undef ecw2cw
124 :
125 1500 : conf = os_zalloc(sizeof(*conf));
126 1500 : bss = os_zalloc(sizeof(*bss));
127 1500 : if (conf == NULL || bss == NULL) {
128 10 : wpa_printf(MSG_ERROR, "Failed to allocate memory for "
129 : "configuration data.");
130 10 : os_free(conf);
131 10 : os_free(bss);
132 10 : return NULL;
133 : }
134 1490 : conf->bss = os_calloc(1, sizeof(struct hostapd_bss_config *));
135 1490 : if (conf->bss == NULL) {
136 4 : os_free(conf);
137 4 : os_free(bss);
138 4 : return NULL;
139 : }
140 1486 : conf->bss[0] = bss;
141 :
142 1486 : bss->radius = os_zalloc(sizeof(*bss->radius));
143 1486 : if (bss->radius == NULL) {
144 4 : os_free(conf->bss);
145 4 : os_free(conf);
146 4 : os_free(bss);
147 4 : return NULL;
148 : }
149 :
150 1482 : hostapd_config_defaults_bss(bss);
151 :
152 1482 : conf->num_bss = 1;
153 :
154 1482 : conf->beacon_int = 100;
155 1482 : conf->rts_threshold = -1; /* use driver default: 2347 */
156 1482 : conf->fragm_threshold = -1; /* user driver default: 2346 */
157 1482 : conf->send_probe_response = 1;
158 : /* Set to invalid value means do not add Power Constraint IE */
159 1482 : conf->local_pwr_constraint = -1;
160 :
161 1482 : conf->wmm_ac_params[0] = ac_be;
162 1482 : conf->wmm_ac_params[1] = ac_bk;
163 1482 : conf->wmm_ac_params[2] = ac_vi;
164 1482 : conf->wmm_ac_params[3] = ac_vo;
165 :
166 1482 : conf->tx_queue[0] = txq_vo;
167 1482 : conf->tx_queue[1] = txq_vi;
168 1482 : conf->tx_queue[2] = txq_be;
169 1482 : conf->tx_queue[3] = txq_bk;
170 :
171 1482 : conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
172 :
173 1482 : conf->ap_table_max_size = 255;
174 1482 : conf->ap_table_expiration_time = 60;
175 :
176 : #ifdef CONFIG_TESTING_OPTIONS
177 1482 : conf->ignore_probe_probability = 0.0;
178 1482 : conf->ignore_auth_probability = 0.0;
179 1482 : conf->ignore_assoc_probability = 0.0;
180 1482 : conf->ignore_reassoc_probability = 0.0;
181 1482 : conf->corrupt_gtk_rekey_mic_probability = 0.0;
182 : #endif /* CONFIG_TESTING_OPTIONS */
183 :
184 : #ifdef CONFIG_ACS
185 1137 : conf->acs_num_scans = 5;
186 : #endif /* CONFIG_ACS */
187 :
188 1482 : return conf;
189 : }
190 :
191 :
192 68 : int hostapd_mac_comp(const void *a, const void *b)
193 : {
194 68 : return os_memcmp(a, b, sizeof(macaddr));
195 : }
196 :
197 :
198 3835 : int hostapd_mac_comp_empty(const void *a)
199 : {
200 3835 : macaddr empty = { 0 };
201 3835 : return os_memcmp(a, empty, sizeof(macaddr));
202 : }
203 :
204 :
205 18 : static int hostapd_config_read_wpa_psk(const char *fname,
206 : struct hostapd_ssid *ssid)
207 : {
208 : FILE *f;
209 : char buf[128], *pos;
210 18 : int line = 0, ret = 0, len, ok;
211 : u8 addr[ETH_ALEN];
212 : struct hostapd_wpa_psk *psk;
213 :
214 18 : if (!fname)
215 0 : return 0;
216 :
217 18 : f = fopen(fname, "r");
218 18 : if (!f) {
219 1 : wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
220 1 : return -1;
221 : }
222 :
223 96 : while (fgets(buf, sizeof(buf), f)) {
224 65 : line++;
225 :
226 65 : if (buf[0] == '#')
227 2 : continue;
228 63 : pos = buf;
229 2875 : while (*pos != '\0') {
230 2812 : if (*pos == '\n') {
231 63 : *pos = '\0';
232 63 : break;
233 : }
234 2749 : pos++;
235 : }
236 63 : if (buf[0] == '\0')
237 1 : continue;
238 :
239 62 : if (hwaddr_aton(buf, addr)) {
240 1 : wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
241 : "line %d in '%s'", buf, line, fname);
242 1 : ret = -1;
243 1 : break;
244 : }
245 :
246 61 : psk = os_zalloc(sizeof(*psk));
247 61 : if (psk == NULL) {
248 0 : wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
249 0 : ret = -1;
250 0 : break;
251 : }
252 61 : if (is_zero_ether_addr(addr))
253 22 : psk->group = 1;
254 : else
255 39 : os_memcpy(psk->addr, addr, ETH_ALEN);
256 :
257 61 : pos = buf + 17;
258 61 : if (*pos == '\0') {
259 1 : wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
260 : line, fname);
261 1 : os_free(psk);
262 1 : ret = -1;
263 1 : break;
264 : }
265 60 : pos++;
266 :
267 60 : ok = 0;
268 60 : len = os_strlen(pos);
269 60 : if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
270 12 : ok = 1;
271 48 : else if (len >= 8 && len < 64) {
272 47 : pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
273 47 : 4096, psk->psk, PMK_LEN);
274 47 : ok = 1;
275 : }
276 60 : if (!ok) {
277 1 : wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
278 : "'%s'", pos, line, fname);
279 1 : os_free(psk);
280 1 : ret = -1;
281 1 : break;
282 : }
283 :
284 59 : psk->next = ssid->wpa_psk;
285 59 : ssid->wpa_psk = psk;
286 : }
287 :
288 17 : fclose(f);
289 :
290 17 : return ret;
291 : }
292 :
293 :
294 251 : static int hostapd_derive_psk(struct hostapd_ssid *ssid)
295 : {
296 251 : ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
297 251 : if (ssid->wpa_psk == NULL) {
298 0 : wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
299 0 : return -1;
300 : }
301 502 : wpa_hexdump_ascii(MSG_DEBUG, "SSID",
302 251 : (u8 *) ssid->ssid, ssid->ssid_len);
303 502 : wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
304 251 : (u8 *) ssid->wpa_passphrase,
305 251 : os_strlen(ssid->wpa_passphrase));
306 502 : pbkdf2_sha1(ssid->wpa_passphrase,
307 251 : ssid->ssid, ssid->ssid_len,
308 251 : 4096, ssid->wpa_psk->psk, PMK_LEN);
309 251 : wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
310 251 : ssid->wpa_psk->psk, PMK_LEN);
311 251 : return 0;
312 : }
313 :
314 :
315 1407 : int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
316 : {
317 1407 : struct hostapd_ssid *ssid = &conf->ssid;
318 :
319 1407 : if (ssid->wpa_passphrase != NULL) {
320 252 : if (ssid->wpa_psk != NULL) {
321 1 : wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
322 : "instead of passphrase");
323 : } else {
324 251 : wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
325 : "passphrase");
326 251 : if (hostapd_derive_psk(ssid) < 0)
327 0 : return -1;
328 : }
329 252 : ssid->wpa_psk->group = 1;
330 : }
331 :
332 1407 : if (ssid->wpa_psk_file) {
333 18 : if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
334 : &conf->ssid))
335 4 : return -1;
336 : }
337 :
338 1403 : return 0;
339 : }
340 :
341 :
342 2984 : static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
343 : int num_servers)
344 : {
345 : int i;
346 :
347 3417 : for (i = 0; i < num_servers; i++) {
348 433 : os_free(servers[i].shared_secret);
349 : }
350 2984 : os_free(servers);
351 2984 : }
352 :
353 :
354 : struct hostapd_radius_attr *
355 43141 : hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
356 : {
357 43480 : for (; attr; attr = attr->next) {
358 354 : if (attr->type == type)
359 15 : return attr;
360 : }
361 43126 : return NULL;
362 : }
363 :
364 :
365 6819 : static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
366 : {
367 : struct hostapd_radius_attr *prev;
368 :
369 14455 : while (attr) {
370 817 : prev = attr;
371 817 : attr = attr->next;
372 817 : wpabuf_free(prev->val);
373 817 : os_free(prev);
374 : }
375 6819 : }
376 :
377 :
378 3835 : void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
379 : {
380 3835 : hostapd_config_free_radius_attr(user->accept_attr);
381 3835 : os_free(user->identity);
382 3835 : bin_clear_free(user->password, user->password_len);
383 3835 : os_free(user);
384 3835 : }
385 :
386 :
387 1492 : static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
388 : {
389 : int i;
390 7460 : for (i = 0; i < NUM_WEP_KEYS; i++) {
391 5968 : bin_clear_free(keys->key[i], keys->len[i]);
392 5968 : keys->key[i] = NULL;
393 : }
394 1492 : }
395 :
396 :
397 1789 : void hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk **l)
398 : {
399 : struct hostapd_wpa_psk *psk, *tmp;
400 :
401 4236 : for (psk = *l; psk;) {
402 658 : tmp = psk;
403 658 : psk = psk->next;
404 658 : bin_clear_free(tmp, sizeof(*tmp));
405 : }
406 1789 : *l = NULL;
407 1789 : }
408 :
409 :
410 1492 : void hostapd_config_free_bss(struct hostapd_bss_config *conf)
411 : {
412 : struct hostapd_eap_user *user, *prev_user;
413 :
414 1492 : if (conf == NULL)
415 1492 : return;
416 :
417 1492 : hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
418 :
419 1492 : str_clear_free(conf->ssid.wpa_passphrase);
420 1492 : os_free(conf->ssid.wpa_psk_file);
421 1492 : hostapd_config_free_wep(&conf->ssid.wep);
422 : #ifdef CONFIG_FULL_DYNAMIC_VLAN
423 1147 : os_free(conf->ssid.vlan_tagged_interface);
424 : #endif /* CONFIG_FULL_DYNAMIC_VLAN */
425 :
426 1492 : user = conf->eap_user;
427 6748 : while (user) {
428 3764 : prev_user = user;
429 3764 : user = user->next;
430 3764 : hostapd_config_free_eap_user(prev_user);
431 : }
432 1492 : os_free(conf->eap_user_sqlite);
433 :
434 1492 : os_free(conf->eap_req_id_text);
435 1492 : os_free(conf->erp_domain);
436 1492 : os_free(conf->accept_mac);
437 1492 : os_free(conf->deny_mac);
438 1492 : os_free(conf->nas_identifier);
439 1492 : if (conf->radius) {
440 1492 : hostapd_config_free_radius(conf->radius->auth_servers,
441 1492 : conf->radius->num_auth_servers);
442 1492 : hostapd_config_free_radius(conf->radius->acct_servers,
443 1492 : conf->radius->num_acct_servers);
444 : }
445 1492 : hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
446 1492 : hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
447 1492 : os_free(conf->rsn_preauth_interfaces);
448 1492 : os_free(conf->ctrl_interface);
449 1492 : os_free(conf->ca_cert);
450 1492 : os_free(conf->server_cert);
451 1492 : os_free(conf->private_key);
452 1492 : os_free(conf->private_key_passwd);
453 1492 : os_free(conf->ocsp_stapling_response);
454 1492 : os_free(conf->dh_file);
455 1492 : os_free(conf->openssl_ciphers);
456 1492 : os_free(conf->pac_opaque_encr_key);
457 1492 : os_free(conf->eap_fast_a_id);
458 1492 : os_free(conf->eap_fast_a_id_info);
459 1492 : os_free(conf->eap_sim_db);
460 1492 : os_free(conf->radius_server_clients);
461 1492 : os_free(conf->radius);
462 1492 : os_free(conf->radius_das_shared_secret);
463 1492 : hostapd_config_free_vlan(conf);
464 1492 : os_free(conf->time_zone);
465 :
466 : #ifdef CONFIG_IEEE80211R
467 : {
468 : struct ft_remote_r0kh *r0kh, *r0kh_prev;
469 : struct ft_remote_r1kh *r1kh, *r1kh_prev;
470 :
471 1492 : r0kh = conf->r0kh_list;
472 1492 : conf->r0kh_list = NULL;
473 3066 : while (r0kh) {
474 82 : r0kh_prev = r0kh;
475 82 : r0kh = r0kh->next;
476 82 : os_free(r0kh_prev);
477 : }
478 :
479 1492 : r1kh = conf->r1kh_list;
480 1492 : conf->r1kh_list = NULL;
481 3025 : while (r1kh) {
482 41 : r1kh_prev = r1kh;
483 41 : r1kh = r1kh->next;
484 41 : os_free(r1kh_prev);
485 : }
486 : }
487 : #endif /* CONFIG_IEEE80211R */
488 :
489 : #ifdef CONFIG_WPS
490 1492 : os_free(conf->wps_pin_requests);
491 1492 : os_free(conf->device_name);
492 1492 : os_free(conf->manufacturer);
493 1492 : os_free(conf->model_name);
494 1492 : os_free(conf->model_number);
495 1492 : os_free(conf->serial_number);
496 1492 : os_free(conf->config_methods);
497 1492 : os_free(conf->ap_pin);
498 1492 : os_free(conf->extra_cred);
499 1492 : os_free(conf->ap_settings);
500 1492 : os_free(conf->upnp_iface);
501 1492 : os_free(conf->friendly_name);
502 1492 : os_free(conf->manufacturer_url);
503 1492 : os_free(conf->model_description);
504 1492 : os_free(conf->model_url);
505 1492 : os_free(conf->upc);
506 : {
507 : unsigned int i;
508 :
509 16412 : for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
510 14920 : wpabuf_free(conf->wps_vendor_ext[i]);
511 : }
512 1492 : wpabuf_free(conf->wps_nfc_dh_pubkey);
513 1492 : wpabuf_free(conf->wps_nfc_dh_privkey);
514 1492 : wpabuf_free(conf->wps_nfc_dev_pw);
515 : #endif /* CONFIG_WPS */
516 :
517 1492 : os_free(conf->roaming_consortium);
518 1492 : os_free(conf->venue_name);
519 1492 : os_free(conf->nai_realm_data);
520 1492 : os_free(conf->network_auth_type);
521 1492 : os_free(conf->anqp_3gpp_cell_net);
522 1492 : os_free(conf->domain_name);
523 :
524 : #ifdef CONFIG_RADIUS_TEST
525 : os_free(conf->dump_msk_file);
526 : #endif /* CONFIG_RADIUS_TEST */
527 :
528 : #ifdef CONFIG_HS20
529 1492 : os_free(conf->hs20_oper_friendly_name);
530 1492 : os_free(conf->hs20_wan_metrics);
531 1492 : os_free(conf->hs20_connection_capability);
532 1492 : os_free(conf->hs20_operating_class);
533 1492 : os_free(conf->hs20_icons);
534 1492 : if (conf->hs20_osu_providers) {
535 : size_t i;
536 8 : for (i = 0; i < conf->hs20_osu_providers_count; i++) {
537 : struct hs20_osu_provider *p;
538 : size_t j;
539 4 : p = &conf->hs20_osu_providers[i];
540 4 : os_free(p->friendly_name);
541 4 : os_free(p->server_uri);
542 4 : os_free(p->method_list);
543 7 : for (j = 0; j < p->icons_count; j++)
544 3 : os_free(p->icons[j]);
545 4 : os_free(p->icons);
546 4 : os_free(p->osu_nai);
547 4 : os_free(p->service_desc);
548 : }
549 4 : os_free(conf->hs20_osu_providers);
550 : }
551 1492 : os_free(conf->subscr_remediation_url);
552 : #endif /* CONFIG_HS20 */
553 :
554 1492 : wpabuf_free(conf->vendor_elements);
555 :
556 1492 : os_free(conf->sae_groups);
557 :
558 1492 : os_free(conf->wowlan_triggers);
559 :
560 1492 : os_free(conf->server_id);
561 :
562 1492 : os_free(conf);
563 : }
564 :
565 :
566 : /**
567 : * hostapd_config_free - Free hostapd configuration
568 : * @conf: Configuration data from hostapd_config_read().
569 : */
570 1509 : void hostapd_config_free(struct hostapd_config *conf)
571 : {
572 : size_t i;
573 :
574 1509 : if (conf == NULL)
575 1536 : return;
576 :
577 2934 : for (i = 0; i < conf->num_bss; i++)
578 1452 : hostapd_config_free_bss(conf->bss[i]);
579 1482 : os_free(conf->bss);
580 1482 : os_free(conf->supported_rates);
581 1482 : os_free(conf->basic_rates);
582 1482 : os_free(conf->chanlist);
583 1482 : os_free(conf->driver_params);
584 : #ifdef CONFIG_ACS
585 1137 : os_free(conf->acs_chan_bias);
586 : #endif /* CONFIG_ACS */
587 :
588 1482 : os_free(conf);
589 : }
590 :
591 :
592 : /**
593 : * hostapd_maclist_found - Find a MAC address from a list
594 : * @list: MAC address list
595 : * @num_entries: Number of addresses in the list
596 : * @addr: Address to search for
597 : * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
598 : * Returns: 1 if address is in the list or 0 if not.
599 : *
600 : * Perform a binary search for given MAC address from a pre-sorted list.
601 : */
602 5863 : int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
603 : const u8 *addr, int *vlan_id)
604 : {
605 : int start, end, middle, res;
606 :
607 5863 : start = 0;
608 5863 : end = num_entries - 1;
609 :
610 11747 : while (start <= end) {
611 141 : middle = (start + end) / 2;
612 141 : res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
613 141 : if (res == 0) {
614 120 : if (vlan_id)
615 120 : *vlan_id = list[middle].vlan_id;
616 120 : return 1;
617 : }
618 21 : if (res < 0)
619 21 : start = middle + 1;
620 : else
621 0 : end = middle - 1;
622 : }
623 :
624 5743 : return 0;
625 : }
626 :
627 :
628 18120 : int hostapd_rate_found(int *list, int rate)
629 : {
630 : int i;
631 :
632 18120 : if (list == NULL)
633 0 : return 0;
634 :
635 79951 : for (i = 0; list[i] >= 0; i++)
636 69256 : if (list[i] == rate)
637 7425 : return 1;
638 :
639 10695 : return 0;
640 : }
641 :
642 :
643 13 : int hostapd_vlan_id_valid(struct hostapd_vlan *vlan, int vlan_id)
644 : {
645 13 : struct hostapd_vlan *v = vlan;
646 31 : while (v) {
647 18 : if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
648 13 : return 1;
649 5 : v = v->next;
650 : }
651 0 : return 0;
652 : }
653 :
654 :
655 10 : const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
656 : {
657 10 : struct hostapd_vlan *v = vlan;
658 20 : while (v) {
659 10 : if (v->vlan_id == vlan_id)
660 10 : return v->ifname;
661 0 : v = v->next;
662 : }
663 0 : return NULL;
664 : }
665 :
666 :
667 2020 : const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
668 : const u8 *addr, const u8 *p2p_dev_addr,
669 : const u8 *prev_psk)
670 : {
671 : struct hostapd_wpa_psk *psk;
672 2020 : int next_ok = prev_psk == NULL;
673 :
674 2020 : if (p2p_dev_addr && !is_zero_ether_addr(p2p_dev_addr)) {
675 7356 : wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
676 : " p2p_dev_addr=" MACSTR " prev_psk=%p",
677 7356 : MAC2STR(addr), MAC2STR(p2p_dev_addr), prev_psk);
678 613 : addr = NULL; /* Use P2P Device Address for matching */
679 : } else {
680 8442 : wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
681 : " prev_psk=%p",
682 8442 : MAC2STR(addr), prev_psk);
683 : }
684 :
685 2064 : for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
686 4064 : if (next_ok &&
687 2112 : (psk->group ||
688 42 : (addr && os_memcmp(psk->addr, addr, ETH_ALEN) == 0) ||
689 96 : (!addr && p2p_dev_addr &&
690 48 : os_memcmp(psk->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
691 : 0)))
692 1998 : return psk->psk;
693 :
694 44 : if (psk->psk == prev_psk)
695 16 : next_ok = 1;
696 : }
697 :
698 22 : return NULL;
699 : }
700 :
701 :
702 17506 : static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
703 : struct hostapd_config *conf,
704 : int full_config)
705 : {
706 17867 : if (full_config && bss->ieee802_1x && !bss->eap_server &&
707 361 : !bss->radius->auth_servers) {
708 1 : wpa_printf(MSG_ERROR, "Invalid IEEE 802.1X configuration (no "
709 : "EAP authenticator configured).");
710 1 : return -1;
711 : }
712 :
713 17505 : if (bss->wpa) {
714 : int wep, i;
715 :
716 15848 : wep = bss->default_wep_key_len > 0 ||
717 7924 : bss->individual_wep_key_len > 0;
718 39616 : for (i = 0; i < NUM_WEP_KEYS; i++) {
719 31693 : if (bss->ssid.wep.keys_set) {
720 1 : wep = 1;
721 1 : break;
722 : }
723 : }
724 :
725 7924 : if (wep) {
726 1 : wpa_printf(MSG_ERROR, "WEP configuration in a WPA network is not supported");
727 1 : return -1;
728 : }
729 : }
730 :
731 18175 : if (full_config && bss->wpa &&
732 673 : bss->wpa_psk_radius != PSK_RADIUS_IGNORED &&
733 2 : bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
734 1 : wpa_printf(MSG_ERROR, "WPA-PSK using RADIUS enabled, but no "
735 : "RADIUS checking (macaddr_acl=2) enabled.");
736 1 : return -1;
737 : }
738 :
739 17723 : if (full_config && bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
740 420 : bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
741 10 : bss->ssid.wpa_psk_file == NULL &&
742 2 : (bss->wpa_psk_radius != PSK_RADIUS_REQUIRED ||
743 1 : bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH)) {
744 0 : wpa_printf(MSG_ERROR, "WPA-PSK enabled, but PSK or passphrase "
745 : "is not configured.");
746 0 : return -1;
747 : }
748 :
749 17503 : if (full_config && hostapd_mac_comp_empty(bss->bssid) != 0) {
750 : size_t i;
751 :
752 224 : for (i = 0; i < conf->num_bss; i++) {
753 151 : if (conf->bss[i] != bss &&
754 26 : (hostapd_mac_comp(conf->bss[i]->bssid,
755 26 : bss->bssid) == 0)) {
756 0 : wpa_printf(MSG_ERROR, "Duplicate BSSID " MACSTR
757 : " on interface '%s' and '%s'.",
758 0 : MAC2STR(bss->bssid),
759 0 : conf->bss[i]->iface, bss->iface);
760 0 : return -1;
761 : }
762 : }
763 : }
764 :
765 : #ifdef CONFIG_IEEE80211R
766 17547 : if (full_config && wpa_key_mgmt_ft(bss->wpa_key_mgmt) &&
767 87 : (bss->nas_identifier == NULL ||
768 86 : os_strlen(bss->nas_identifier) < 1 ||
769 43 : os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
770 1 : wpa_printf(MSG_ERROR, "FT (IEEE 802.11r) requires "
771 : "nas_identifier to be configured as a 1..48 octet "
772 : "string");
773 1 : return -1;
774 : }
775 : #endif /* CONFIG_IEEE80211R */
776 :
777 : #ifdef CONFIG_IEEE80211N
778 18576 : if (full_config && conf->ieee80211n &&
779 1074 : conf->hw_mode == HOSTAPD_MODE_IEEE80211B) {
780 3 : bss->disable_11n = 1;
781 3 : wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) in 11b mode is not "
782 : "allowed, disabling HT capabilities");
783 : }
784 :
785 18576 : if (full_config && conf->ieee80211n &&
786 1074 : bss->ssid.security_policy == SECURITY_STATIC_WEP) {
787 11 : bss->disable_11n = 1;
788 11 : wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WEP is not "
789 : "allowed, disabling HT capabilities");
790 : }
791 :
792 18171 : if (full_config && conf->ieee80211n && bss->wpa &&
793 1336 : !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
794 667 : !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
795 : WPA_CIPHER_CCMP_256 | WPA_CIPHER_GCMP_256)))
796 : {
797 12 : bss->disable_11n = 1;
798 12 : wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WPA/WPA2 "
799 : "requires CCMP/GCMP to be enabled, disabling HT "
800 : "capabilities");
801 : }
802 : #endif /* CONFIG_IEEE80211N */
803 :
804 : #ifdef CONFIG_WPS
805 17502 : if (full_config && bss->wps_state && bss->ignore_broadcast_ssid) {
806 0 : wpa_printf(MSG_INFO, "WPS: ignore_broadcast_ssid "
807 : "configuration forced WPS to be disabled");
808 0 : bss->wps_state = 0;
809 : }
810 :
811 17590 : if (full_config && bss->wps_state &&
812 88 : bss->ssid.wep.keys_set && bss->wpa == 0) {
813 0 : wpa_printf(MSG_INFO, "WPS: WEP configuration forced WPS to be "
814 : "disabled");
815 0 : bss->wps_state = 0;
816 : }
817 :
818 17566 : if (full_config && bss->wps_state && bss->wpa &&
819 128 : (!(bss->wpa & 2) ||
820 64 : !(bss->rsn_pairwise & WPA_CIPHER_CCMP))) {
821 0 : wpa_printf(MSG_INFO, "WPS: WPA/TKIP configuration without "
822 : "WPA2/CCMP forced WPS to be disabled");
823 0 : bss->wps_state = 0;
824 : }
825 : #endif /* CONFIG_WPS */
826 :
827 : #ifdef CONFIG_HS20
828 17625 : if (full_config && bss->hs20 &&
829 245 : (!(bss->wpa & 2) ||
830 122 : !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
831 : WPA_CIPHER_CCMP_256 |
832 : WPA_CIPHER_GCMP_256)))) {
833 1 : wpa_printf(MSG_ERROR, "HS 2.0: WPA2-Enterprise/CCMP "
834 : "configuration is required for Hotspot 2.0 "
835 : "functionality");
836 1 : return -1;
837 : }
838 : #endif /* CONFIG_HS20 */
839 :
840 17501 : return 0;
841 : }
842 :
843 :
844 17485 : int hostapd_config_check(struct hostapd_config *conf, int full_config)
845 : {
846 : size_t i;
847 :
848 17501 : if (full_config && conf->ieee80211d &&
849 31 : (!conf->country[0] || !conf->country[1])) {
850 1 : wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11d without "
851 : "setting the country_code");
852 1 : return -1;
853 : }
854 :
855 17484 : if (full_config && conf->ieee80211h && !conf->ieee80211d) {
856 1 : wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11h without "
857 : "IEEE 802.11d enabled");
858 1 : return -1;
859 : }
860 :
861 17485 : if (full_config && conf->local_pwr_constraint != -1 &&
862 2 : !conf->ieee80211d) {
863 1 : wpa_printf(MSG_ERROR, "Cannot add Power Constraint element without Country element");
864 1 : return -1;
865 : }
866 :
867 17484 : if (full_config && conf->spectrum_mgmt_required &&
868 2 : conf->local_pwr_constraint == -1) {
869 1 : wpa_printf(MSG_ERROR, "Cannot set Spectrum Management bit without Country and Power Constraint elements");
870 1 : return -1;
871 : }
872 :
873 34982 : for (i = 0; i < conf->num_bss; i++) {
874 17506 : if (hostapd_config_check_bss(conf->bss[i], conf, full_config))
875 5 : return -1;
876 : }
877 :
878 17476 : return 0;
879 : }
880 :
881 :
882 17506 : void hostapd_set_security_params(struct hostapd_bss_config *bss,
883 : int full_config)
884 : {
885 17506 : if (bss->individual_wep_key_len == 0) {
886 : /* individual keys are not use; can use key idx0 for
887 : * broadcast keys */
888 17500 : bss->broadcast_key_idx_min = 0;
889 : }
890 :
891 17506 : if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
892 1 : bss->rsn_pairwise = bss->wpa_pairwise;
893 17506 : bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
894 : bss->rsn_pairwise);
895 :
896 17506 : if (full_config) {
897 1093 : bss->radius->auth_server = bss->radius->auth_servers;
898 1093 : bss->radius->acct_server = bss->radius->acct_servers;
899 : }
900 :
901 17506 : if (bss->wpa && bss->ieee802_1x) {
902 2741 : bss->ssid.security_policy = SECURITY_WPA;
903 14765 : } else if (bss->wpa) {
904 5183 : bss->ssid.security_policy = SECURITY_WPA_PSK;
905 9582 : } else if (bss->ieee802_1x) {
906 31 : int cipher = WPA_CIPHER_NONE;
907 31 : bss->ssid.security_policy = SECURITY_IEEE_802_1X;
908 31 : bss->ssid.wep.default_len = bss->default_wep_key_len;
909 31 : if (full_config && bss->default_wep_key_len) {
910 4 : cipher = bss->default_wep_key_len >= 13 ?
911 2 : WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
912 29 : } else if (full_config && bss->ssid.wep.keys_set) {
913 1 : if (bss->ssid.wep.len[0] >= 13)
914 0 : cipher = WPA_CIPHER_WEP104;
915 : else
916 1 : cipher = WPA_CIPHER_WEP40;
917 : }
918 31 : bss->wpa_group = cipher;
919 31 : bss->wpa_pairwise = cipher;
920 31 : bss->rsn_pairwise = cipher;
921 31 : if (full_config)
922 7 : bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
923 9551 : } else if (bss->ssid.wep.keys_set) {
924 24 : int cipher = WPA_CIPHER_WEP40;
925 24 : if (bss->ssid.wep.len[0] >= 13)
926 12 : cipher = WPA_CIPHER_WEP104;
927 24 : bss->ssid.security_policy = SECURITY_STATIC_WEP;
928 24 : bss->wpa_group = cipher;
929 24 : bss->wpa_pairwise = cipher;
930 24 : bss->rsn_pairwise = cipher;
931 24 : if (full_config)
932 11 : bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
933 9527 : } else if (bss->osen) {
934 2 : bss->ssid.security_policy = SECURITY_OSEN;
935 2 : bss->wpa_group = WPA_CIPHER_CCMP;
936 2 : bss->wpa_pairwise = 0;
937 2 : bss->rsn_pairwise = WPA_CIPHER_CCMP;
938 : } else {
939 9525 : bss->ssid.security_policy = SECURITY_PLAINTEXT;
940 9525 : bss->wpa_group = WPA_CIPHER_NONE;
941 9525 : bss->wpa_pairwise = WPA_CIPHER_NONE;
942 9525 : bss->rsn_pairwise = WPA_CIPHER_NONE;
943 9525 : if (full_config)
944 403 : bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
945 : }
946 17506 : }
|