config.c 14 KB

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