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