ap_config.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. /* Set to -1 as defaults depends on HT in setup */
  73. bss->wmm_enabled = -1;
  74. #ifdef CONFIG_IEEE80211R
  75. bss->ft_over_ds = 1;
  76. #endif /* CONFIG_IEEE80211R */
  77. }
  78. struct hostapd_config * hostapd_config_defaults(void)
  79. {
  80. #define ecw2cw(ecw) ((1 << (ecw)) - 1)
  81. struct hostapd_config *conf;
  82. struct hostapd_bss_config *bss;
  83. const int aCWmin = 4, aCWmax = 10;
  84. const struct hostapd_wmm_ac_params ac_bk =
  85. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  86. const struct hostapd_wmm_ac_params ac_be =
  87. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  88. const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
  89. { aCWmin - 1, aCWmin, 2, 3000 / 32, 1 };
  90. const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
  91. { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 1 };
  92. const struct hostapd_tx_queue_params txq_bk =
  93. { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
  94. const struct hostapd_tx_queue_params txq_be =
  95. { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
  96. const struct hostapd_tx_queue_params txq_vi =
  97. { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
  98. const struct hostapd_tx_queue_params txq_vo =
  99. { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
  100. (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
  101. #undef ecw2cw
  102. conf = os_zalloc(sizeof(*conf));
  103. bss = os_zalloc(sizeof(*bss));
  104. if (conf == NULL || bss == NULL) {
  105. wpa_printf(MSG_ERROR, "Failed to allocate memory for "
  106. "configuration data.");
  107. os_free(conf);
  108. os_free(bss);
  109. return NULL;
  110. }
  111. bss->radius = os_zalloc(sizeof(*bss->radius));
  112. if (bss->radius == NULL) {
  113. os_free(conf);
  114. os_free(bss);
  115. return NULL;
  116. }
  117. hostapd_config_defaults_bss(bss);
  118. conf->num_bss = 1;
  119. conf->bss = bss;
  120. conf->beacon_int = 100;
  121. conf->rts_threshold = -1; /* use driver default: 2347 */
  122. conf->fragm_threshold = -1; /* user driver default: 2346 */
  123. conf->send_probe_response = 1;
  124. conf->wmm_ac_params[0] = ac_be;
  125. conf->wmm_ac_params[1] = ac_bk;
  126. conf->wmm_ac_params[2] = ac_vi;
  127. conf->wmm_ac_params[3] = ac_vo;
  128. conf->tx_queue[0] = txq_vo;
  129. conf->tx_queue[1] = txq_vi;
  130. conf->tx_queue[2] = txq_be;
  131. conf->tx_queue[3] = txq_bk;
  132. conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
  133. return conf;
  134. }
  135. int hostapd_mac_comp(const void *a, const void *b)
  136. {
  137. return os_memcmp(a, b, sizeof(macaddr));
  138. }
  139. int hostapd_mac_comp_empty(const void *a)
  140. {
  141. macaddr empty = { 0 };
  142. return os_memcmp(a, empty, sizeof(macaddr));
  143. }
  144. static int hostapd_config_read_wpa_psk(const char *fname,
  145. struct hostapd_ssid *ssid)
  146. {
  147. FILE *f;
  148. char buf[128], *pos;
  149. int line = 0, ret = 0, len, ok;
  150. u8 addr[ETH_ALEN];
  151. struct hostapd_wpa_psk *psk;
  152. if (!fname)
  153. return 0;
  154. f = fopen(fname, "r");
  155. if (!f) {
  156. wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
  157. return -1;
  158. }
  159. while (fgets(buf, sizeof(buf), f)) {
  160. line++;
  161. if (buf[0] == '#')
  162. continue;
  163. pos = buf;
  164. while (*pos != '\0') {
  165. if (*pos == '\n') {
  166. *pos = '\0';
  167. break;
  168. }
  169. pos++;
  170. }
  171. if (buf[0] == '\0')
  172. continue;
  173. if (hwaddr_aton(buf, addr)) {
  174. wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
  175. "line %d in '%s'", buf, line, fname);
  176. ret = -1;
  177. break;
  178. }
  179. psk = os_zalloc(sizeof(*psk));
  180. if (psk == NULL) {
  181. wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
  182. ret = -1;
  183. break;
  184. }
  185. if (is_zero_ether_addr(addr))
  186. psk->group = 1;
  187. else
  188. os_memcpy(psk->addr, addr, ETH_ALEN);
  189. pos = buf + 17;
  190. if (*pos == '\0') {
  191. wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
  192. line, fname);
  193. os_free(psk);
  194. ret = -1;
  195. break;
  196. }
  197. pos++;
  198. ok = 0;
  199. len = os_strlen(pos);
  200. if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
  201. ok = 1;
  202. else if (len >= 8 && len < 64) {
  203. pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
  204. 4096, psk->psk, PMK_LEN);
  205. ok = 1;
  206. }
  207. if (!ok) {
  208. wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
  209. "'%s'", pos, line, fname);
  210. os_free(psk);
  211. ret = -1;
  212. break;
  213. }
  214. psk->next = ssid->wpa_psk;
  215. ssid->wpa_psk = psk;
  216. }
  217. fclose(f);
  218. return ret;
  219. }
  220. static int hostapd_derive_psk(struct hostapd_ssid *ssid)
  221. {
  222. ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  223. if (ssid->wpa_psk == NULL) {
  224. wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
  225. return -1;
  226. }
  227. wpa_hexdump_ascii(MSG_DEBUG, "SSID",
  228. (u8 *) ssid->ssid, ssid->ssid_len);
  229. wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
  230. (u8 *) ssid->wpa_passphrase,
  231. os_strlen(ssid->wpa_passphrase));
  232. pbkdf2_sha1(ssid->wpa_passphrase,
  233. ssid->ssid, ssid->ssid_len,
  234. 4096, ssid->wpa_psk->psk, PMK_LEN);
  235. wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
  236. ssid->wpa_psk->psk, PMK_LEN);
  237. return 0;
  238. }
  239. int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
  240. {
  241. struct hostapd_ssid *ssid = &conf->ssid;
  242. if (ssid->wpa_passphrase != NULL) {
  243. if (ssid->wpa_psk != NULL) {
  244. wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
  245. "instead of passphrase");
  246. } else {
  247. wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
  248. "passphrase");
  249. if (hostapd_derive_psk(ssid) < 0)
  250. return -1;
  251. }
  252. ssid->wpa_psk->group = 1;
  253. }
  254. if (ssid->wpa_psk_file) {
  255. if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
  256. &conf->ssid))
  257. return -1;
  258. }
  259. return 0;
  260. }
  261. int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
  262. {
  263. int i;
  264. if (a->idx != b->idx || a->default_len != b->default_len)
  265. return 1;
  266. for (i = 0; i < NUM_WEP_KEYS; i++)
  267. if (a->len[i] != b->len[i] ||
  268. os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
  269. return 1;
  270. return 0;
  271. }
  272. static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
  273. int num_servers)
  274. {
  275. int i;
  276. for (i = 0; i < num_servers; i++) {
  277. os_free(servers[i].shared_secret);
  278. }
  279. os_free(servers);
  280. }
  281. static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
  282. {
  283. os_free(user->identity);
  284. os_free(user->password);
  285. os_free(user);
  286. }
  287. static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
  288. {
  289. int i;
  290. for (i = 0; i < NUM_WEP_KEYS; i++) {
  291. os_free(keys->key[i]);
  292. keys->key[i] = NULL;
  293. }
  294. }
  295. static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
  296. {
  297. struct hostapd_wpa_psk *psk, *prev;
  298. struct hostapd_eap_user *user, *prev_user;
  299. if (conf == NULL)
  300. return;
  301. psk = conf->ssid.wpa_psk;
  302. while (psk) {
  303. prev = psk;
  304. psk = psk->next;
  305. os_free(prev);
  306. }
  307. os_free(conf->ssid.wpa_passphrase);
  308. os_free(conf->ssid.wpa_psk_file);
  309. hostapd_config_free_wep(&conf->ssid.wep);
  310. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  311. os_free(conf->ssid.vlan_tagged_interface);
  312. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  313. user = conf->eap_user;
  314. while (user) {
  315. prev_user = user;
  316. user = user->next;
  317. hostapd_config_free_eap_user(prev_user);
  318. }
  319. os_free(conf->dump_log_name);
  320. os_free(conf->eap_req_id_text);
  321. os_free(conf->accept_mac);
  322. os_free(conf->deny_mac);
  323. os_free(conf->nas_identifier);
  324. hostapd_config_free_radius(conf->radius->auth_servers,
  325. conf->radius->num_auth_servers);
  326. hostapd_config_free_radius(conf->radius->acct_servers,
  327. conf->radius->num_acct_servers);
  328. os_free(conf->rsn_preauth_interfaces);
  329. os_free(conf->ctrl_interface);
  330. os_free(conf->ca_cert);
  331. os_free(conf->server_cert);
  332. os_free(conf->private_key);
  333. os_free(conf->private_key_passwd);
  334. os_free(conf->dh_file);
  335. os_free(conf->pac_opaque_encr_key);
  336. os_free(conf->eap_fast_a_id);
  337. os_free(conf->eap_fast_a_id_info);
  338. os_free(conf->eap_sim_db);
  339. os_free(conf->radius_server_clients);
  340. os_free(conf->test_socket);
  341. os_free(conf->radius);
  342. hostapd_config_free_vlan(conf);
  343. if (conf->ssid.dyn_vlan_keys) {
  344. struct hostapd_ssid *ssid = &conf->ssid;
  345. size_t i;
  346. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  347. if (ssid->dyn_vlan_keys[i] == NULL)
  348. continue;
  349. hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
  350. os_free(ssid->dyn_vlan_keys[i]);
  351. }
  352. os_free(ssid->dyn_vlan_keys);
  353. ssid->dyn_vlan_keys = NULL;
  354. }
  355. os_free(conf->time_zone);
  356. #ifdef CONFIG_IEEE80211R
  357. {
  358. struct ft_remote_r0kh *r0kh, *r0kh_prev;
  359. struct ft_remote_r1kh *r1kh, *r1kh_prev;
  360. r0kh = conf->r0kh_list;
  361. conf->r0kh_list = NULL;
  362. while (r0kh) {
  363. r0kh_prev = r0kh;
  364. r0kh = r0kh->next;
  365. os_free(r0kh_prev);
  366. }
  367. r1kh = conf->r1kh_list;
  368. conf->r1kh_list = NULL;
  369. while (r1kh) {
  370. r1kh_prev = r1kh;
  371. r1kh = r1kh->next;
  372. os_free(r1kh_prev);
  373. }
  374. }
  375. #endif /* CONFIG_IEEE80211R */
  376. #ifdef CONFIG_WPS
  377. os_free(conf->wps_pin_requests);
  378. os_free(conf->device_name);
  379. os_free(conf->manufacturer);
  380. os_free(conf->model_name);
  381. os_free(conf->model_number);
  382. os_free(conf->serial_number);
  383. os_free(conf->config_methods);
  384. os_free(conf->ap_pin);
  385. os_free(conf->extra_cred);
  386. os_free(conf->ap_settings);
  387. os_free(conf->upnp_iface);
  388. os_free(conf->friendly_name);
  389. os_free(conf->manufacturer_url);
  390. os_free(conf->model_description);
  391. os_free(conf->model_url);
  392. os_free(conf->upc);
  393. #endif /* CONFIG_WPS */
  394. os_free(conf->roaming_consortium);
  395. }
  396. /**
  397. * hostapd_config_free - Free hostapd configuration
  398. * @conf: Configuration data from hostapd_config_read().
  399. */
  400. void hostapd_config_free(struct hostapd_config *conf)
  401. {
  402. size_t i;
  403. if (conf == NULL)
  404. return;
  405. for (i = 0; i < conf->num_bss; i++)
  406. hostapd_config_free_bss(&conf->bss[i]);
  407. os_free(conf->bss);
  408. os_free(conf->supported_rates);
  409. os_free(conf->basic_rates);
  410. os_free(conf);
  411. }
  412. /**
  413. * hostapd_maclist_found - Find a MAC address from a list
  414. * @list: MAC address list
  415. * @num_entries: Number of addresses in the list
  416. * @addr: Address to search for
  417. * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
  418. * Returns: 1 if address is in the list or 0 if not.
  419. *
  420. * Perform a binary search for given MAC address from a pre-sorted list.
  421. */
  422. int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
  423. const u8 *addr, int *vlan_id)
  424. {
  425. int start, end, middle, res;
  426. start = 0;
  427. end = num_entries - 1;
  428. while (start <= end) {
  429. middle = (start + end) / 2;
  430. res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
  431. if (res == 0) {
  432. if (vlan_id)
  433. *vlan_id = list[middle].vlan_id;
  434. return 1;
  435. }
  436. if (res < 0)
  437. start = middle + 1;
  438. else
  439. end = middle - 1;
  440. }
  441. return 0;
  442. }
  443. int hostapd_rate_found(int *list, int rate)
  444. {
  445. int i;
  446. if (list == NULL)
  447. return 0;
  448. for (i = 0; list[i] >= 0; i++)
  449. if (list[i] == rate)
  450. return 1;
  451. return 0;
  452. }
  453. const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
  454. {
  455. struct hostapd_vlan *v = vlan;
  456. while (v) {
  457. if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
  458. return v->ifname;
  459. v = v->next;
  460. }
  461. return NULL;
  462. }
  463. const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
  464. const u8 *addr, const u8 *prev_psk)
  465. {
  466. struct hostapd_wpa_psk *psk;
  467. int next_ok = prev_psk == NULL;
  468. for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
  469. if (next_ok &&
  470. (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
  471. return psk->psk;
  472. if (psk->psk == prev_psk)
  473. next_ok = 1;
  474. }
  475. return NULL;
  476. }
  477. const struct hostapd_eap_user *
  478. hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
  479. size_t identity_len, int phase2)
  480. {
  481. struct hostapd_eap_user *user = conf->eap_user;
  482. #ifdef CONFIG_WPS
  483. if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
  484. os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
  485. static struct hostapd_eap_user wsc_enrollee;
  486. os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
  487. wsc_enrollee.methods[0].method = eap_server_get_type(
  488. "WSC", &wsc_enrollee.methods[0].vendor);
  489. return &wsc_enrollee;
  490. }
  491. if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
  492. os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
  493. static struct hostapd_eap_user wsc_registrar;
  494. os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
  495. wsc_registrar.methods[0].method = eap_server_get_type(
  496. "WSC", &wsc_registrar.methods[0].vendor);
  497. wsc_registrar.password = (u8 *) conf->ap_pin;
  498. wsc_registrar.password_len = conf->ap_pin ?
  499. os_strlen(conf->ap_pin) : 0;
  500. return &wsc_registrar;
  501. }
  502. #endif /* CONFIG_WPS */
  503. while (user) {
  504. if (!phase2 && user->identity == NULL) {
  505. /* Wildcard match */
  506. break;
  507. }
  508. if (user->phase2 == !!phase2 && user->wildcard_prefix &&
  509. identity_len >= user->identity_len &&
  510. os_memcmp(user->identity, identity, user->identity_len) ==
  511. 0) {
  512. /* Wildcard prefix match */
  513. break;
  514. }
  515. if (user->phase2 == !!phase2 &&
  516. user->identity_len == identity_len &&
  517. os_memcmp(user->identity, identity, identity_len) == 0)
  518. break;
  519. user = user->next;
  520. }
  521. return user;
  522. }