ap_config.c 24 KB

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