ap_config.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * hostapd / Configuration helper functions
  3. * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "crypto/sha1.h"
  11. #include "radius/radius_client.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/eapol_common.h"
  14. #include "eap_common/eap_wsc_common.h"
  15. #include "eap_server/eap.h"
  16. #include "wpa_auth.h"
  17. #include "sta_info.h"
  18. #include "ap_config.h"
  19. static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
  20. {
  21. struct hostapd_vlan *vlan, *prev;
  22. vlan = bss->vlan;
  23. prev = NULL;
  24. while (vlan) {
  25. prev = vlan;
  26. vlan = vlan->next;
  27. os_free(prev);
  28. }
  29. bss->vlan = NULL;
  30. }
  31. void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
  32. {
  33. dl_list_init(&bss->anqp_elem);
  34. bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
  35. bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
  36. bss->logger_syslog = (unsigned int) -1;
  37. bss->logger_stdout = (unsigned int) -1;
  38. bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
  39. bss->wep_rekeying_period = 300;
  40. /* use key0 in individual key and key1 in broadcast key */
  41. bss->broadcast_key_idx_min = 1;
  42. bss->broadcast_key_idx_max = 2;
  43. bss->eap_reauth_period = 3600;
  44. bss->wpa_group_rekey = 600;
  45. bss->wpa_gmk_rekey = 86400;
  46. bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  47. bss->wpa_pairwise = WPA_CIPHER_TKIP;
  48. bss->wpa_group = WPA_CIPHER_TKIP;
  49. bss->rsn_pairwise = 0;
  50. bss->max_num_sta = MAX_STA_COUNT;
  51. bss->dtim_period = 2;
  52. bss->radius_server_auth_port = 1812;
  53. bss->ap_max_inactivity = AP_MAX_INACTIVITY;
  54. bss->eapol_version = EAPOL_VERSION;
  55. bss->max_listen_interval = 65535;
  56. bss->pwd_group = 19; /* ECC: GF(p=256) */
  57. #ifdef CONFIG_IEEE80211W
  58. bss->assoc_sa_query_max_timeout = 1000;
  59. bss->assoc_sa_query_retry_timeout = 201;
  60. bss->group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
  61. #endif /* CONFIG_IEEE80211W */
  62. #ifdef EAP_SERVER_FAST
  63. /* both anonymous and authenticated provisioning */
  64. bss->eap_fast_prov = 3;
  65. bss->pac_key_lifetime = 7 * 24 * 60 * 60;
  66. bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
  67. #endif /* EAP_SERVER_FAST */
  68. /* Set to -1 as defaults depends on HT in setup */
  69. bss->wmm_enabled = -1;
  70. #ifdef CONFIG_IEEE80211R
  71. bss->ft_over_ds = 1;
  72. #endif /* CONFIG_IEEE80211R */
  73. bss->radius_das_time_window = 300;
  74. bss->sae_anti_clogging_threshold = 5;
  75. }
  76. struct hostapd_config * hostapd_config_defaults(void)
  77. {
  78. #define ecw2cw(ecw) ((1 << (ecw)) - 1)
  79. struct hostapd_config *conf;
  80. struct hostapd_bss_config *bss;
  81. const int aCWmin = 4, aCWmax = 10;
  82. const struct hostapd_wmm_ac_params ac_bk =
  83. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  84. const struct hostapd_wmm_ac_params ac_be =
  85. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  86. const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
  87. { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
  88. const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
  89. { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
  90. const struct hostapd_tx_queue_params txq_bk =
  91. { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
  92. const struct hostapd_tx_queue_params txq_be =
  93. { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
  94. const struct hostapd_tx_queue_params txq_vi =
  95. { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
  96. const struct hostapd_tx_queue_params txq_vo =
  97. { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
  98. (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
  99. #undef ecw2cw
  100. conf = os_zalloc(sizeof(*conf));
  101. bss = os_zalloc(sizeof(*bss));
  102. if (conf == NULL || bss == NULL) {
  103. wpa_printf(MSG_ERROR, "Failed to allocate memory for "
  104. "configuration data.");
  105. os_free(conf);
  106. os_free(bss);
  107. return NULL;
  108. }
  109. conf->bss = os_calloc(1, sizeof(struct hostapd_bss_config *));
  110. if (conf->bss == NULL) {
  111. os_free(conf);
  112. os_free(bss);
  113. return NULL;
  114. }
  115. conf->bss[0] = bss;
  116. bss->radius = os_zalloc(sizeof(*bss->radius));
  117. if (bss->radius == NULL) {
  118. os_free(conf->bss);
  119. os_free(conf);
  120. os_free(bss);
  121. return NULL;
  122. }
  123. hostapd_config_defaults_bss(bss);
  124. conf->num_bss = 1;
  125. conf->beacon_int = 100;
  126. conf->rts_threshold = -1; /* use driver default: 2347 */
  127. conf->fragm_threshold = -1; /* user driver default: 2346 */
  128. conf->send_probe_response = 1;
  129. /* Set to invalid value means do not add Power Constraint IE */
  130. conf->local_pwr_constraint = -1;
  131. conf->wmm_ac_params[0] = ac_be;
  132. conf->wmm_ac_params[1] = ac_bk;
  133. conf->wmm_ac_params[2] = ac_vi;
  134. conf->wmm_ac_params[3] = ac_vo;
  135. conf->tx_queue[0] = txq_vo;
  136. conf->tx_queue[1] = txq_vi;
  137. conf->tx_queue[2] = txq_be;
  138. conf->tx_queue[3] = txq_bk;
  139. conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
  140. conf->ap_table_max_size = 255;
  141. conf->ap_table_expiration_time = 60;
  142. conf->track_sta_max_age = 180;
  143. #ifdef CONFIG_TESTING_OPTIONS
  144. conf->ignore_probe_probability = 0.0;
  145. conf->ignore_auth_probability = 0.0;
  146. conf->ignore_assoc_probability = 0.0;
  147. conf->ignore_reassoc_probability = 0.0;
  148. conf->corrupt_gtk_rekey_mic_probability = 0.0;
  149. conf->ecsa_ie_only = 0;
  150. #endif /* CONFIG_TESTING_OPTIONS */
  151. conf->acs = 0;
  152. conf->acs_ch_list.num = 0;
  153. #ifdef CONFIG_ACS
  154. conf->acs_num_scans = 5;
  155. #endif /* CONFIG_ACS */
  156. return conf;
  157. }
  158. int hostapd_mac_comp(const void *a, const void *b)
  159. {
  160. return os_memcmp(a, b, sizeof(macaddr));
  161. }
  162. int hostapd_mac_comp_empty(const void *a)
  163. {
  164. macaddr empty = { 0 };
  165. return os_memcmp(a, empty, sizeof(macaddr));
  166. }
  167. static int hostapd_config_read_wpa_psk(const char *fname,
  168. struct hostapd_ssid *ssid)
  169. {
  170. FILE *f;
  171. char buf[128], *pos;
  172. int line = 0, ret = 0, len, ok;
  173. u8 addr[ETH_ALEN];
  174. struct hostapd_wpa_psk *psk;
  175. if (!fname)
  176. return 0;
  177. f = fopen(fname, "r");
  178. if (!f) {
  179. wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
  180. return -1;
  181. }
  182. while (fgets(buf, sizeof(buf), f)) {
  183. line++;
  184. if (buf[0] == '#')
  185. continue;
  186. pos = buf;
  187. while (*pos != '\0') {
  188. if (*pos == '\n') {
  189. *pos = '\0';
  190. break;
  191. }
  192. pos++;
  193. }
  194. if (buf[0] == '\0')
  195. continue;
  196. if (hwaddr_aton(buf, addr)) {
  197. wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
  198. "line %d in '%s'", buf, line, fname);
  199. ret = -1;
  200. break;
  201. }
  202. psk = os_zalloc(sizeof(*psk));
  203. if (psk == NULL) {
  204. wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
  205. ret = -1;
  206. break;
  207. }
  208. if (is_zero_ether_addr(addr))
  209. psk->group = 1;
  210. else
  211. os_memcpy(psk->addr, addr, ETH_ALEN);
  212. pos = buf + 17;
  213. if (*pos == '\0') {
  214. wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
  215. line, fname);
  216. os_free(psk);
  217. ret = -1;
  218. break;
  219. }
  220. pos++;
  221. ok = 0;
  222. len = os_strlen(pos);
  223. if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
  224. ok = 1;
  225. else if (len >= 8 && len < 64) {
  226. pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
  227. 4096, psk->psk, PMK_LEN);
  228. ok = 1;
  229. }
  230. if (!ok) {
  231. wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
  232. "'%s'", pos, line, fname);
  233. os_free(psk);
  234. ret = -1;
  235. break;
  236. }
  237. psk->next = ssid->wpa_psk;
  238. ssid->wpa_psk = psk;
  239. }
  240. fclose(f);
  241. return ret;
  242. }
  243. static int hostapd_derive_psk(struct hostapd_ssid *ssid)
  244. {
  245. ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  246. if (ssid->wpa_psk == NULL) {
  247. wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
  248. return -1;
  249. }
  250. wpa_hexdump_ascii(MSG_DEBUG, "SSID",
  251. (u8 *) ssid->ssid, ssid->ssid_len);
  252. wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
  253. (u8 *) ssid->wpa_passphrase,
  254. os_strlen(ssid->wpa_passphrase));
  255. pbkdf2_sha1(ssid->wpa_passphrase,
  256. ssid->ssid, ssid->ssid_len,
  257. 4096, ssid->wpa_psk->psk, PMK_LEN);
  258. wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
  259. ssid->wpa_psk->psk, PMK_LEN);
  260. return 0;
  261. }
  262. int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
  263. {
  264. struct hostapd_ssid *ssid = &conf->ssid;
  265. if (ssid->wpa_passphrase != NULL) {
  266. if (ssid->wpa_psk != NULL) {
  267. wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
  268. "instead of passphrase");
  269. } else {
  270. wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
  271. "passphrase");
  272. if (hostapd_derive_psk(ssid) < 0)
  273. return -1;
  274. }
  275. ssid->wpa_psk->group = 1;
  276. }
  277. if (ssid->wpa_psk_file) {
  278. if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
  279. &conf->ssid))
  280. return -1;
  281. }
  282. return 0;
  283. }
  284. static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
  285. int num_servers)
  286. {
  287. int i;
  288. for (i = 0; i < num_servers; i++) {
  289. os_free(servers[i].shared_secret);
  290. }
  291. os_free(servers);
  292. }
  293. struct hostapd_radius_attr *
  294. hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
  295. {
  296. for (; attr; attr = attr->next) {
  297. if (attr->type == type)
  298. return attr;
  299. }
  300. return NULL;
  301. }
  302. static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
  303. {
  304. struct hostapd_radius_attr *prev;
  305. while (attr) {
  306. prev = attr;
  307. attr = attr->next;
  308. wpabuf_free(prev->val);
  309. os_free(prev);
  310. }
  311. }
  312. void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
  313. {
  314. hostapd_config_free_radius_attr(user->accept_attr);
  315. os_free(user->identity);
  316. bin_clear_free(user->password, user->password_len);
  317. os_free(user);
  318. }
  319. static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
  320. {
  321. int i;
  322. for (i = 0; i < NUM_WEP_KEYS; i++) {
  323. bin_clear_free(keys->key[i], keys->len[i]);
  324. keys->key[i] = NULL;
  325. }
  326. }
  327. void hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk **l)
  328. {
  329. struct hostapd_wpa_psk *psk, *tmp;
  330. for (psk = *l; psk;) {
  331. tmp = psk;
  332. psk = psk->next;
  333. bin_clear_free(tmp, sizeof(*tmp));
  334. }
  335. *l = NULL;
  336. }
  337. static void hostapd_config_free_anqp_elem(struct hostapd_bss_config *conf)
  338. {
  339. struct anqp_element *elem;
  340. while ((elem = dl_list_first(&conf->anqp_elem, struct anqp_element,
  341. list))) {
  342. dl_list_del(&elem->list);
  343. wpabuf_free(elem->payload);
  344. os_free(elem);
  345. }
  346. }
  347. void hostapd_config_free_bss(struct hostapd_bss_config *conf)
  348. {
  349. struct hostapd_eap_user *user, *prev_user;
  350. if (conf == NULL)
  351. return;
  352. hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
  353. str_clear_free(conf->ssid.wpa_passphrase);
  354. os_free(conf->ssid.wpa_psk_file);
  355. hostapd_config_free_wep(&conf->ssid.wep);
  356. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  357. os_free(conf->ssid.vlan_tagged_interface);
  358. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  359. user = conf->eap_user;
  360. while (user) {
  361. prev_user = user;
  362. user = user->next;
  363. hostapd_config_free_eap_user(prev_user);
  364. }
  365. os_free(conf->eap_user_sqlite);
  366. os_free(conf->eap_req_id_text);
  367. os_free(conf->erp_domain);
  368. os_free(conf->accept_mac);
  369. os_free(conf->deny_mac);
  370. os_free(conf->nas_identifier);
  371. if (conf->radius) {
  372. hostapd_config_free_radius(conf->radius->auth_servers,
  373. conf->radius->num_auth_servers);
  374. hostapd_config_free_radius(conf->radius->acct_servers,
  375. conf->radius->num_acct_servers);
  376. }
  377. hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
  378. hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
  379. os_free(conf->rsn_preauth_interfaces);
  380. os_free(conf->ctrl_interface);
  381. os_free(conf->ca_cert);
  382. os_free(conf->server_cert);
  383. os_free(conf->private_key);
  384. os_free(conf->private_key_passwd);
  385. os_free(conf->ocsp_stapling_response);
  386. os_free(conf->dh_file);
  387. os_free(conf->openssl_ciphers);
  388. os_free(conf->pac_opaque_encr_key);
  389. os_free(conf->eap_fast_a_id);
  390. os_free(conf->eap_fast_a_id_info);
  391. os_free(conf->eap_sim_db);
  392. os_free(conf->radius_server_clients);
  393. os_free(conf->radius);
  394. os_free(conf->radius_das_shared_secret);
  395. hostapd_config_free_vlan(conf);
  396. os_free(conf->time_zone);
  397. #ifdef CONFIG_IEEE80211R
  398. {
  399. struct ft_remote_r0kh *r0kh, *r0kh_prev;
  400. struct ft_remote_r1kh *r1kh, *r1kh_prev;
  401. r0kh = conf->r0kh_list;
  402. conf->r0kh_list = NULL;
  403. while (r0kh) {
  404. r0kh_prev = r0kh;
  405. r0kh = r0kh->next;
  406. os_free(r0kh_prev);
  407. }
  408. r1kh = conf->r1kh_list;
  409. conf->r1kh_list = NULL;
  410. while (r1kh) {
  411. r1kh_prev = r1kh;
  412. r1kh = r1kh->next;
  413. os_free(r1kh_prev);
  414. }
  415. }
  416. #endif /* CONFIG_IEEE80211R */
  417. #ifdef CONFIG_WPS
  418. os_free(conf->wps_pin_requests);
  419. os_free(conf->device_name);
  420. os_free(conf->manufacturer);
  421. os_free(conf->model_name);
  422. os_free(conf->model_number);
  423. os_free(conf->serial_number);
  424. os_free(conf->config_methods);
  425. os_free(conf->ap_pin);
  426. os_free(conf->extra_cred);
  427. os_free(conf->ap_settings);
  428. os_free(conf->upnp_iface);
  429. os_free(conf->friendly_name);
  430. os_free(conf->manufacturer_url);
  431. os_free(conf->model_description);
  432. os_free(conf->model_url);
  433. os_free(conf->upc);
  434. {
  435. unsigned int i;
  436. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
  437. wpabuf_free(conf->wps_vendor_ext[i]);
  438. }
  439. wpabuf_free(conf->wps_nfc_dh_pubkey);
  440. wpabuf_free(conf->wps_nfc_dh_privkey);
  441. wpabuf_free(conf->wps_nfc_dev_pw);
  442. #endif /* CONFIG_WPS */
  443. os_free(conf->roaming_consortium);
  444. os_free(conf->venue_name);
  445. os_free(conf->nai_realm_data);
  446. os_free(conf->network_auth_type);
  447. os_free(conf->anqp_3gpp_cell_net);
  448. os_free(conf->domain_name);
  449. hostapd_config_free_anqp_elem(conf);
  450. #ifdef CONFIG_RADIUS_TEST
  451. os_free(conf->dump_msk_file);
  452. #endif /* CONFIG_RADIUS_TEST */
  453. #ifdef CONFIG_HS20
  454. os_free(conf->hs20_oper_friendly_name);
  455. os_free(conf->hs20_wan_metrics);
  456. os_free(conf->hs20_connection_capability);
  457. os_free(conf->hs20_operating_class);
  458. os_free(conf->hs20_icons);
  459. if (conf->hs20_osu_providers) {
  460. size_t i;
  461. for (i = 0; i < conf->hs20_osu_providers_count; i++) {
  462. struct hs20_osu_provider *p;
  463. size_t j;
  464. p = &conf->hs20_osu_providers[i];
  465. os_free(p->friendly_name);
  466. os_free(p->server_uri);
  467. os_free(p->method_list);
  468. for (j = 0; j < p->icons_count; j++)
  469. os_free(p->icons[j]);
  470. os_free(p->icons);
  471. os_free(p->osu_nai);
  472. os_free(p->service_desc);
  473. }
  474. os_free(conf->hs20_osu_providers);
  475. }
  476. os_free(conf->subscr_remediation_url);
  477. #endif /* CONFIG_HS20 */
  478. wpabuf_free(conf->vendor_elements);
  479. os_free(conf->sae_groups);
  480. os_free(conf->wowlan_triggers);
  481. os_free(conf->server_id);
  482. #ifdef CONFIG_TESTING_OPTIONS
  483. wpabuf_free(conf->own_ie_override);
  484. #endif /* CONFIG_TESTING_OPTIONS */
  485. os_free(conf->no_probe_resp_if_seen_on);
  486. os_free(conf->no_auth_if_seen_on);
  487. os_free(conf);
  488. }
  489. /**
  490. * hostapd_config_free - Free hostapd configuration
  491. * @conf: Configuration data from hostapd_config_read().
  492. */
  493. void hostapd_config_free(struct hostapd_config *conf)
  494. {
  495. size_t i;
  496. if (conf == NULL)
  497. return;
  498. for (i = 0; i < conf->num_bss; i++)
  499. hostapd_config_free_bss(conf->bss[i]);
  500. os_free(conf->bss);
  501. os_free(conf->supported_rates);
  502. os_free(conf->basic_rates);
  503. os_free(conf->acs_ch_list.range);
  504. os_free(conf->driver_params);
  505. #ifdef CONFIG_ACS
  506. os_free(conf->acs_chan_bias);
  507. #endif /* CONFIG_ACS */
  508. os_free(conf);
  509. }
  510. /**
  511. * hostapd_maclist_found - Find a MAC address from a list
  512. * @list: MAC address list
  513. * @num_entries: Number of addresses in the list
  514. * @addr: Address to search for
  515. * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
  516. * Returns: 1 if address is in the list or 0 if not.
  517. *
  518. * Perform a binary search for given MAC address from a pre-sorted list.
  519. */
  520. int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
  521. const u8 *addr, int *vlan_id)
  522. {
  523. int start, end, middle, res;
  524. start = 0;
  525. end = num_entries - 1;
  526. while (start <= end) {
  527. middle = (start + end) / 2;
  528. res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
  529. if (res == 0) {
  530. if (vlan_id)
  531. *vlan_id = list[middle].vlan_id;
  532. return 1;
  533. }
  534. if (res < 0)
  535. start = middle + 1;
  536. else
  537. end = middle - 1;
  538. }
  539. return 0;
  540. }
  541. int hostapd_rate_found(int *list, int rate)
  542. {
  543. int i;
  544. if (list == NULL)
  545. return 0;
  546. for (i = 0; list[i] >= 0; i++)
  547. if (list[i] == rate)
  548. return 1;
  549. return 0;
  550. }
  551. int hostapd_vlan_id_valid(struct hostapd_vlan *vlan, int vlan_id)
  552. {
  553. struct hostapd_vlan *v = vlan;
  554. while (v) {
  555. if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
  556. return 1;
  557. v = v->next;
  558. }
  559. return 0;
  560. }
  561. const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
  562. {
  563. struct hostapd_vlan *v = vlan;
  564. while (v) {
  565. if (v->vlan_id == vlan_id)
  566. return v->ifname;
  567. v = v->next;
  568. }
  569. return NULL;
  570. }
  571. const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
  572. const u8 *addr, const u8 *p2p_dev_addr,
  573. const u8 *prev_psk)
  574. {
  575. struct hostapd_wpa_psk *psk;
  576. int next_ok = prev_psk == NULL;
  577. if (p2p_dev_addr && !is_zero_ether_addr(p2p_dev_addr)) {
  578. wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
  579. " p2p_dev_addr=" MACSTR " prev_psk=%p",
  580. MAC2STR(addr), MAC2STR(p2p_dev_addr), prev_psk);
  581. addr = NULL; /* Use P2P Device Address for matching */
  582. } else {
  583. wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
  584. " prev_psk=%p",
  585. MAC2STR(addr), prev_psk);
  586. }
  587. for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
  588. if (next_ok &&
  589. (psk->group ||
  590. (addr && os_memcmp(psk->addr, addr, ETH_ALEN) == 0) ||
  591. (!addr && p2p_dev_addr &&
  592. os_memcmp(psk->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
  593. 0)))
  594. return psk->psk;
  595. if (psk->psk == prev_psk)
  596. next_ok = 1;
  597. }
  598. return NULL;
  599. }
  600. static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
  601. struct hostapd_config *conf,
  602. int full_config)
  603. {
  604. if (full_config && bss->ieee802_1x && !bss->eap_server &&
  605. !bss->radius->auth_servers) {
  606. wpa_printf(MSG_ERROR, "Invalid IEEE 802.1X configuration (no "
  607. "EAP authenticator configured).");
  608. return -1;
  609. }
  610. if (bss->wpa) {
  611. int wep, i;
  612. wep = bss->default_wep_key_len > 0 ||
  613. bss->individual_wep_key_len > 0;
  614. for (i = 0; i < NUM_WEP_KEYS; i++) {
  615. if (bss->ssid.wep.keys_set) {
  616. wep = 1;
  617. break;
  618. }
  619. }
  620. if (wep) {
  621. wpa_printf(MSG_ERROR, "WEP configuration in a WPA network is not supported");
  622. return -1;
  623. }
  624. }
  625. if (full_config && bss->wpa &&
  626. bss->wpa_psk_radius != PSK_RADIUS_IGNORED &&
  627. bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
  628. wpa_printf(MSG_ERROR, "WPA-PSK using RADIUS enabled, but no "
  629. "RADIUS checking (macaddr_acl=2) enabled.");
  630. return -1;
  631. }
  632. if (full_config && bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
  633. bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
  634. bss->ssid.wpa_psk_file == NULL &&
  635. (bss->wpa_psk_radius != PSK_RADIUS_REQUIRED ||
  636. bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH)) {
  637. wpa_printf(MSG_ERROR, "WPA-PSK enabled, but PSK or passphrase "
  638. "is not configured.");
  639. return -1;
  640. }
  641. if (full_config && hostapd_mac_comp_empty(bss->bssid) != 0) {
  642. size_t i;
  643. for (i = 0; i < conf->num_bss; i++) {
  644. if (conf->bss[i] != bss &&
  645. (hostapd_mac_comp(conf->bss[i]->bssid,
  646. bss->bssid) == 0)) {
  647. wpa_printf(MSG_ERROR, "Duplicate BSSID " MACSTR
  648. " on interface '%s' and '%s'.",
  649. MAC2STR(bss->bssid),
  650. conf->bss[i]->iface, bss->iface);
  651. return -1;
  652. }
  653. }
  654. }
  655. #ifdef CONFIG_IEEE80211R
  656. if (full_config && wpa_key_mgmt_ft(bss->wpa_key_mgmt) &&
  657. (bss->nas_identifier == NULL ||
  658. os_strlen(bss->nas_identifier) < 1 ||
  659. os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
  660. wpa_printf(MSG_ERROR, "FT (IEEE 802.11r) requires "
  661. "nas_identifier to be configured as a 1..48 octet "
  662. "string");
  663. return -1;
  664. }
  665. #endif /* CONFIG_IEEE80211R */
  666. #ifdef CONFIG_IEEE80211N
  667. if (full_config && conf->ieee80211n &&
  668. conf->hw_mode == HOSTAPD_MODE_IEEE80211B) {
  669. bss->disable_11n = 1;
  670. wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) in 11b mode is not "
  671. "allowed, disabling HT capabilities");
  672. }
  673. if (full_config && conf->ieee80211n &&
  674. bss->ssid.security_policy == SECURITY_STATIC_WEP) {
  675. bss->disable_11n = 1;
  676. wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WEP is not "
  677. "allowed, disabling HT capabilities");
  678. }
  679. if (full_config && conf->ieee80211n && bss->wpa &&
  680. !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
  681. !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
  682. WPA_CIPHER_CCMP_256 | WPA_CIPHER_GCMP_256)))
  683. {
  684. bss->disable_11n = 1;
  685. wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WPA/WPA2 "
  686. "requires CCMP/GCMP to be enabled, disabling HT "
  687. "capabilities");
  688. }
  689. #endif /* CONFIG_IEEE80211N */
  690. #ifdef CONFIG_WPS
  691. if (full_config && bss->wps_state && bss->ignore_broadcast_ssid) {
  692. wpa_printf(MSG_INFO, "WPS: ignore_broadcast_ssid "
  693. "configuration forced WPS to be disabled");
  694. bss->wps_state = 0;
  695. }
  696. if (full_config && bss->wps_state &&
  697. bss->ssid.wep.keys_set && bss->wpa == 0) {
  698. wpa_printf(MSG_INFO, "WPS: WEP configuration forced WPS to be "
  699. "disabled");
  700. bss->wps_state = 0;
  701. }
  702. if (full_config && bss->wps_state && bss->wpa &&
  703. (!(bss->wpa & 2) ||
  704. !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)))) {
  705. wpa_printf(MSG_INFO, "WPS: WPA/TKIP configuration without "
  706. "WPA2/CCMP/GCMP forced WPS to be disabled");
  707. bss->wps_state = 0;
  708. }
  709. #endif /* CONFIG_WPS */
  710. #ifdef CONFIG_HS20
  711. if (full_config && bss->hs20 &&
  712. (!(bss->wpa & 2) ||
  713. !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
  714. WPA_CIPHER_CCMP_256 |
  715. WPA_CIPHER_GCMP_256)))) {
  716. wpa_printf(MSG_ERROR, "HS 2.0: WPA2-Enterprise/CCMP "
  717. "configuration is required for Hotspot 2.0 "
  718. "functionality");
  719. return -1;
  720. }
  721. #endif /* CONFIG_HS20 */
  722. return 0;
  723. }
  724. static int hostapd_config_check_cw(struct hostapd_config *conf, int queue)
  725. {
  726. int tx_cwmin = conf->tx_queue[queue].cwmin;
  727. int tx_cwmax = conf->tx_queue[queue].cwmax;
  728. int ac_cwmin = conf->wmm_ac_params[queue].cwmin;
  729. int ac_cwmax = conf->wmm_ac_params[queue].cwmax;
  730. if (tx_cwmin > tx_cwmax) {
  731. wpa_printf(MSG_ERROR,
  732. "Invalid TX queue cwMin/cwMax values. cwMin(%d) greater than cwMax(%d)",
  733. tx_cwmin, tx_cwmax);
  734. return -1;
  735. }
  736. if (ac_cwmin > ac_cwmax) {
  737. wpa_printf(MSG_ERROR,
  738. "Invalid WMM AC cwMin/cwMax values. cwMin(%d) greater than cwMax(%d)",
  739. ac_cwmin, ac_cwmax);
  740. return -1;
  741. }
  742. return 0;
  743. }
  744. int hostapd_config_check(struct hostapd_config *conf, int full_config)
  745. {
  746. size_t i;
  747. if (full_config && conf->ieee80211d &&
  748. (!conf->country[0] || !conf->country[1])) {
  749. wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11d without "
  750. "setting the country_code");
  751. return -1;
  752. }
  753. if (full_config && conf->ieee80211h && !conf->ieee80211d) {
  754. wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11h without "
  755. "IEEE 802.11d enabled");
  756. return -1;
  757. }
  758. if (full_config && conf->local_pwr_constraint != -1 &&
  759. !conf->ieee80211d) {
  760. wpa_printf(MSG_ERROR, "Cannot add Power Constraint element without Country element");
  761. return -1;
  762. }
  763. if (full_config && conf->spectrum_mgmt_required &&
  764. conf->local_pwr_constraint == -1) {
  765. wpa_printf(MSG_ERROR, "Cannot set Spectrum Management bit without Country and Power Constraint elements");
  766. return -1;
  767. }
  768. for (i = 0; i < NUM_TX_QUEUES; i++) {
  769. if (hostapd_config_check_cw(conf, i))
  770. return -1;
  771. }
  772. for (i = 0; i < conf->num_bss; i++) {
  773. if (hostapd_config_check_bss(conf->bss[i], conf, full_config))
  774. return -1;
  775. }
  776. return 0;
  777. }
  778. void hostapd_set_security_params(struct hostapd_bss_config *bss,
  779. int full_config)
  780. {
  781. if (bss->individual_wep_key_len == 0) {
  782. /* individual keys are not use; can use key idx0 for
  783. * broadcast keys */
  784. bss->broadcast_key_idx_min = 0;
  785. }
  786. if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
  787. bss->rsn_pairwise = bss->wpa_pairwise;
  788. bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
  789. bss->rsn_pairwise);
  790. if (full_config) {
  791. bss->radius->auth_server = bss->radius->auth_servers;
  792. bss->radius->acct_server = bss->radius->acct_servers;
  793. }
  794. if (bss->wpa && bss->ieee802_1x) {
  795. bss->ssid.security_policy = SECURITY_WPA;
  796. } else if (bss->wpa) {
  797. bss->ssid.security_policy = SECURITY_WPA_PSK;
  798. } else if (bss->ieee802_1x) {
  799. int cipher = WPA_CIPHER_NONE;
  800. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  801. bss->ssid.wep.default_len = bss->default_wep_key_len;
  802. if (full_config && bss->default_wep_key_len) {
  803. cipher = bss->default_wep_key_len >= 13 ?
  804. WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
  805. } else if (full_config && bss->ssid.wep.keys_set) {
  806. if (bss->ssid.wep.len[0] >= 13)
  807. cipher = WPA_CIPHER_WEP104;
  808. else
  809. cipher = WPA_CIPHER_WEP40;
  810. }
  811. bss->wpa_group = cipher;
  812. bss->wpa_pairwise = cipher;
  813. bss->rsn_pairwise = cipher;
  814. if (full_config)
  815. bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  816. } else if (bss->ssid.wep.keys_set) {
  817. int cipher = WPA_CIPHER_WEP40;
  818. if (bss->ssid.wep.len[0] >= 13)
  819. cipher = WPA_CIPHER_WEP104;
  820. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  821. bss->wpa_group = cipher;
  822. bss->wpa_pairwise = cipher;
  823. bss->rsn_pairwise = cipher;
  824. if (full_config)
  825. bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
  826. } else if (bss->osen) {
  827. bss->ssid.security_policy = SECURITY_OSEN;
  828. bss->wpa_group = WPA_CIPHER_CCMP;
  829. bss->wpa_pairwise = 0;
  830. bss->rsn_pairwise = WPA_CIPHER_CCMP;
  831. } else {
  832. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  833. if (full_config) {
  834. bss->wpa_group = WPA_CIPHER_NONE;
  835. bss->wpa_pairwise = WPA_CIPHER_NONE;
  836. bss->rsn_pairwise = WPA_CIPHER_NONE;
  837. bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
  838. }
  839. }
  840. }