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