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