ap_config.c 24 KB

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