ap_config.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * hostapd / Configuration helper functions
  3. * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "crypto/sha1.h"
  17. #include "radius/radius_client.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/eapol_common.h"
  20. #include "eap_common/eap_wsc_common.h"
  21. #include "eap_server/eap.h"
  22. #include "wpa_auth.h"
  23. #include "sta_info.h"
  24. #include "ap_config.h"
  25. static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
  26. {
  27. struct hostapd_vlan *vlan, *prev;
  28. vlan = bss->vlan;
  29. prev = NULL;
  30. while (vlan) {
  31. prev = vlan;
  32. vlan = vlan->next;
  33. os_free(prev);
  34. }
  35. bss->vlan = NULL;
  36. }
  37. void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
  38. {
  39. bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
  40. bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
  41. bss->logger_syslog = (unsigned int) -1;
  42. bss->logger_stdout = (unsigned int) -1;
  43. bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
  44. bss->wep_rekeying_period = 300;
  45. /* use key0 in individual key and key1 in broadcast key */
  46. bss->broadcast_key_idx_min = 1;
  47. bss->broadcast_key_idx_max = 2;
  48. bss->eap_reauth_period = 3600;
  49. bss->wpa_group_rekey = 600;
  50. bss->wpa_gmk_rekey = 86400;
  51. bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  52. bss->wpa_pairwise = WPA_CIPHER_TKIP;
  53. bss->wpa_group = WPA_CIPHER_TKIP;
  54. bss->rsn_pairwise = 0;
  55. bss->max_num_sta = MAX_STA_COUNT;
  56. bss->dtim_period = 2;
  57. bss->radius_server_auth_port = 1812;
  58. bss->ap_max_inactivity = AP_MAX_INACTIVITY;
  59. bss->eapol_version = EAPOL_VERSION;
  60. bss->max_listen_interval = 65535;
  61. bss->pwd_group = 19; /* ECC: GF(p=256) */
  62. #ifdef CONFIG_IEEE80211W
  63. bss->assoc_sa_query_max_timeout = 1000;
  64. bss->assoc_sa_query_retry_timeout = 201;
  65. #endif /* CONFIG_IEEE80211W */
  66. #ifdef EAP_SERVER_FAST
  67. /* both anonymous and authenticated provisioning */
  68. bss->eap_fast_prov = 3;
  69. bss->pac_key_lifetime = 7 * 24 * 60 * 60;
  70. bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
  71. #endif /* EAP_SERVER_FAST */
  72. }
  73. struct hostapd_config * hostapd_config_defaults(void)
  74. {
  75. struct hostapd_config *conf;
  76. struct hostapd_bss_config *bss;
  77. int i;
  78. const int aCWmin = 4, aCWmax = 10;
  79. const struct hostapd_wmm_ac_params ac_bk =
  80. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  81. const struct hostapd_wmm_ac_params ac_be =
  82. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  83. const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
  84. { aCWmin - 1, aCWmin, 2, 3000 / 32, 1 };
  85. const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
  86. { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 1 };
  87. conf = os_zalloc(sizeof(*conf));
  88. bss = os_zalloc(sizeof(*bss));
  89. if (conf == NULL || bss == NULL) {
  90. wpa_printf(MSG_ERROR, "Failed to allocate memory for "
  91. "configuration data.");
  92. os_free(conf);
  93. os_free(bss);
  94. return NULL;
  95. }
  96. bss->radius = os_zalloc(sizeof(*bss->radius));
  97. if (bss->radius == NULL) {
  98. os_free(conf);
  99. os_free(bss);
  100. return NULL;
  101. }
  102. hostapd_config_defaults_bss(bss);
  103. conf->num_bss = 1;
  104. conf->bss = bss;
  105. conf->beacon_int = 100;
  106. conf->rts_threshold = -1; /* use driver default: 2347 */
  107. conf->fragm_threshold = -1; /* user driver default: 2346 */
  108. conf->send_probe_response = 1;
  109. for (i = 0; i < NUM_TX_QUEUES; i++)
  110. conf->tx_queue[i].aifs = -1; /* use hw default */
  111. conf->wmm_ac_params[0] = ac_be;
  112. conf->wmm_ac_params[1] = ac_bk;
  113. conf->wmm_ac_params[2] = ac_vi;
  114. conf->wmm_ac_params[3] = ac_vo;
  115. conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
  116. return conf;
  117. }
  118. int hostapd_mac_comp(const void *a, const void *b)
  119. {
  120. return os_memcmp(a, b, sizeof(macaddr));
  121. }
  122. int hostapd_mac_comp_empty(const void *a)
  123. {
  124. macaddr empty = { 0 };
  125. return os_memcmp(a, empty, sizeof(macaddr));
  126. }
  127. static int hostapd_config_read_wpa_psk(const char *fname,
  128. struct hostapd_ssid *ssid)
  129. {
  130. FILE *f;
  131. char buf[128], *pos;
  132. int line = 0, ret = 0, len, ok;
  133. u8 addr[ETH_ALEN];
  134. struct hostapd_wpa_psk *psk;
  135. if (!fname)
  136. return 0;
  137. f = fopen(fname, "r");
  138. if (!f) {
  139. wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
  140. return -1;
  141. }
  142. while (fgets(buf, sizeof(buf), f)) {
  143. line++;
  144. if (buf[0] == '#')
  145. continue;
  146. pos = buf;
  147. while (*pos != '\0') {
  148. if (*pos == '\n') {
  149. *pos = '\0';
  150. break;
  151. }
  152. pos++;
  153. }
  154. if (buf[0] == '\0')
  155. continue;
  156. if (hwaddr_aton(buf, addr)) {
  157. wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
  158. "line %d in '%s'", buf, line, fname);
  159. ret = -1;
  160. break;
  161. }
  162. psk = os_zalloc(sizeof(*psk));
  163. if (psk == NULL) {
  164. wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
  165. ret = -1;
  166. break;
  167. }
  168. if (is_zero_ether_addr(addr))
  169. psk->group = 1;
  170. else
  171. os_memcpy(psk->addr, addr, ETH_ALEN);
  172. pos = buf + 17;
  173. if (*pos == '\0') {
  174. wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
  175. line, fname);
  176. os_free(psk);
  177. ret = -1;
  178. break;
  179. }
  180. pos++;
  181. ok = 0;
  182. len = os_strlen(pos);
  183. if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
  184. ok = 1;
  185. else if (len >= 8 && len < 64) {
  186. pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
  187. 4096, psk->psk, PMK_LEN);
  188. ok = 1;
  189. }
  190. if (!ok) {
  191. wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
  192. "'%s'", pos, line, fname);
  193. os_free(psk);
  194. ret = -1;
  195. break;
  196. }
  197. psk->next = ssid->wpa_psk;
  198. ssid->wpa_psk = psk;
  199. }
  200. fclose(f);
  201. return ret;
  202. }
  203. static int hostapd_derive_psk(struct hostapd_ssid *ssid)
  204. {
  205. ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  206. if (ssid->wpa_psk == NULL) {
  207. wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
  208. return -1;
  209. }
  210. wpa_hexdump_ascii(MSG_DEBUG, "SSID",
  211. (u8 *) ssid->ssid, ssid->ssid_len);
  212. wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
  213. (u8 *) ssid->wpa_passphrase,
  214. os_strlen(ssid->wpa_passphrase));
  215. pbkdf2_sha1(ssid->wpa_passphrase,
  216. ssid->ssid, ssid->ssid_len,
  217. 4096, ssid->wpa_psk->psk, PMK_LEN);
  218. wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
  219. ssid->wpa_psk->psk, PMK_LEN);
  220. return 0;
  221. }
  222. int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
  223. {
  224. struct hostapd_ssid *ssid = &conf->ssid;
  225. if (ssid->wpa_passphrase != NULL) {
  226. if (ssid->wpa_psk != NULL) {
  227. wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
  228. "instead of passphrase");
  229. } else {
  230. wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
  231. "passphrase");
  232. if (hostapd_derive_psk(ssid) < 0)
  233. return -1;
  234. }
  235. ssid->wpa_psk->group = 1;
  236. }
  237. if (ssid->wpa_psk_file) {
  238. if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
  239. &conf->ssid))
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
  245. {
  246. int i;
  247. if (a->idx != b->idx || a->default_len != b->default_len)
  248. return 1;
  249. for (i = 0; i < NUM_WEP_KEYS; i++)
  250. if (a->len[i] != b->len[i] ||
  251. os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
  252. return 1;
  253. return 0;
  254. }
  255. static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
  256. int num_servers)
  257. {
  258. int i;
  259. for (i = 0; i < num_servers; i++) {
  260. os_free(servers[i].shared_secret);
  261. }
  262. os_free(servers);
  263. }
  264. static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
  265. {
  266. os_free(user->identity);
  267. os_free(user->password);
  268. os_free(user);
  269. }
  270. static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
  271. {
  272. int i;
  273. for (i = 0; i < NUM_WEP_KEYS; i++) {
  274. os_free(keys->key[i]);
  275. keys->key[i] = NULL;
  276. }
  277. }
  278. static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
  279. {
  280. struct hostapd_wpa_psk *psk, *prev;
  281. struct hostapd_eap_user *user, *prev_user;
  282. if (conf == NULL)
  283. return;
  284. psk = conf->ssid.wpa_psk;
  285. while (psk) {
  286. prev = psk;
  287. psk = psk->next;
  288. os_free(prev);
  289. }
  290. os_free(conf->ssid.wpa_passphrase);
  291. os_free(conf->ssid.wpa_psk_file);
  292. hostapd_config_free_wep(&conf->ssid.wep);
  293. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  294. os_free(conf->ssid.vlan_tagged_interface);
  295. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  296. user = conf->eap_user;
  297. while (user) {
  298. prev_user = user;
  299. user = user->next;
  300. hostapd_config_free_eap_user(prev_user);
  301. }
  302. os_free(conf->dump_log_name);
  303. os_free(conf->eap_req_id_text);
  304. os_free(conf->accept_mac);
  305. os_free(conf->deny_mac);
  306. os_free(conf->nas_identifier);
  307. hostapd_config_free_radius(conf->radius->auth_servers,
  308. conf->radius->num_auth_servers);
  309. hostapd_config_free_radius(conf->radius->acct_servers,
  310. conf->radius->num_acct_servers);
  311. os_free(conf->rsn_preauth_interfaces);
  312. os_free(conf->ctrl_interface);
  313. os_free(conf->ca_cert);
  314. os_free(conf->server_cert);
  315. os_free(conf->private_key);
  316. os_free(conf->private_key_passwd);
  317. os_free(conf->dh_file);
  318. os_free(conf->pac_opaque_encr_key);
  319. os_free(conf->eap_fast_a_id);
  320. os_free(conf->eap_fast_a_id_info);
  321. os_free(conf->eap_sim_db);
  322. os_free(conf->radius_server_clients);
  323. os_free(conf->test_socket);
  324. os_free(conf->radius);
  325. hostapd_config_free_vlan(conf);
  326. if (conf->ssid.dyn_vlan_keys) {
  327. struct hostapd_ssid *ssid = &conf->ssid;
  328. size_t i;
  329. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  330. if (ssid->dyn_vlan_keys[i] == NULL)
  331. continue;
  332. hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
  333. os_free(ssid->dyn_vlan_keys[i]);
  334. }
  335. os_free(ssid->dyn_vlan_keys);
  336. ssid->dyn_vlan_keys = NULL;
  337. }
  338. #ifdef CONFIG_IEEE80211R
  339. {
  340. struct ft_remote_r0kh *r0kh, *r0kh_prev;
  341. struct ft_remote_r1kh *r1kh, *r1kh_prev;
  342. r0kh = conf->r0kh_list;
  343. conf->r0kh_list = NULL;
  344. while (r0kh) {
  345. r0kh_prev = r0kh;
  346. r0kh = r0kh->next;
  347. os_free(r0kh_prev);
  348. }
  349. r1kh = conf->r1kh_list;
  350. conf->r1kh_list = NULL;
  351. while (r1kh) {
  352. r1kh_prev = r1kh;
  353. r1kh = r1kh->next;
  354. os_free(r1kh_prev);
  355. }
  356. }
  357. #endif /* CONFIG_IEEE80211R */
  358. #ifdef CONFIG_WPS
  359. os_free(conf->wps_pin_requests);
  360. os_free(conf->device_name);
  361. os_free(conf->manufacturer);
  362. os_free(conf->model_name);
  363. os_free(conf->model_number);
  364. os_free(conf->serial_number);
  365. os_free(conf->device_type);
  366. os_free(conf->config_methods);
  367. os_free(conf->ap_pin);
  368. os_free(conf->extra_cred);
  369. os_free(conf->ap_settings);
  370. os_free(conf->upnp_iface);
  371. os_free(conf->friendly_name);
  372. os_free(conf->manufacturer_url);
  373. os_free(conf->model_description);
  374. os_free(conf->model_url);
  375. os_free(conf->upc);
  376. #endif /* CONFIG_WPS */
  377. }
  378. /**
  379. * hostapd_config_free - Free hostapd configuration
  380. * @conf: Configuration data from hostapd_config_read().
  381. */
  382. void hostapd_config_free(struct hostapd_config *conf)
  383. {
  384. size_t i;
  385. if (conf == NULL)
  386. return;
  387. for (i = 0; i < conf->num_bss; i++)
  388. hostapd_config_free_bss(&conf->bss[i]);
  389. os_free(conf->bss);
  390. os_free(conf->supported_rates);
  391. os_free(conf->basic_rates);
  392. os_free(conf);
  393. }
  394. /**
  395. * hostapd_maclist_found - Find a MAC address from a list
  396. * @list: MAC address list
  397. * @num_entries: Number of addresses in the list
  398. * @addr: Address to search for
  399. * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
  400. * Returns: 1 if address is in the list or 0 if not.
  401. *
  402. * Perform a binary search for given MAC address from a pre-sorted list.
  403. */
  404. int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
  405. const u8 *addr, int *vlan_id)
  406. {
  407. int start, end, middle, res;
  408. start = 0;
  409. end = num_entries - 1;
  410. while (start <= end) {
  411. middle = (start + end) / 2;
  412. res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
  413. if (res == 0) {
  414. if (vlan_id)
  415. *vlan_id = list[middle].vlan_id;
  416. return 1;
  417. }
  418. if (res < 0)
  419. start = middle + 1;
  420. else
  421. end = middle - 1;
  422. }
  423. return 0;
  424. }
  425. int hostapd_rate_found(int *list, int rate)
  426. {
  427. int i;
  428. if (list == NULL)
  429. return 0;
  430. for (i = 0; list[i] >= 0; i++)
  431. if (list[i] == rate)
  432. return 1;
  433. return 0;
  434. }
  435. const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
  436. {
  437. struct hostapd_vlan *v = vlan;
  438. while (v) {
  439. if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
  440. return v->ifname;
  441. v = v->next;
  442. }
  443. return NULL;
  444. }
  445. const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
  446. const u8 *addr, const u8 *prev_psk)
  447. {
  448. struct hostapd_wpa_psk *psk;
  449. int next_ok = prev_psk == NULL;
  450. for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
  451. if (next_ok &&
  452. (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
  453. return psk->psk;
  454. if (psk->psk == prev_psk)
  455. next_ok = 1;
  456. }
  457. return NULL;
  458. }
  459. const struct hostapd_eap_user *
  460. hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
  461. size_t identity_len, int phase2)
  462. {
  463. struct hostapd_eap_user *user = conf->eap_user;
  464. #ifdef CONFIG_WPS
  465. if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
  466. os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
  467. static struct hostapd_eap_user wsc_enrollee;
  468. os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
  469. wsc_enrollee.methods[0].method = eap_server_get_type(
  470. "WSC", &wsc_enrollee.methods[0].vendor);
  471. return &wsc_enrollee;
  472. }
  473. if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
  474. os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
  475. static struct hostapd_eap_user wsc_registrar;
  476. os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
  477. wsc_registrar.methods[0].method = eap_server_get_type(
  478. "WSC", &wsc_registrar.methods[0].vendor);
  479. wsc_registrar.password = (u8 *) conf->ap_pin;
  480. wsc_registrar.password_len = conf->ap_pin ?
  481. os_strlen(conf->ap_pin) : 0;
  482. return &wsc_registrar;
  483. }
  484. #endif /* CONFIG_WPS */
  485. while (user) {
  486. if (!phase2 && user->identity == NULL) {
  487. /* Wildcard match */
  488. break;
  489. }
  490. if (user->phase2 == !!phase2 && user->wildcard_prefix &&
  491. identity_len >= user->identity_len &&
  492. os_memcmp(user->identity, identity, user->identity_len) ==
  493. 0) {
  494. /* Wildcard prefix match */
  495. break;
  496. }
  497. if (user->phase2 == !!phase2 &&
  498. user->identity_len == identity_len &&
  499. os_memcmp(user->identity, identity, identity_len) == 0)
  500. break;
  501. user = user->next;
  502. }
  503. return user;
  504. }