ap_config.c 25 KB

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