config.c 14 KB

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