ap_config.c 24 KB

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