config.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346
  1. /*
  2. * hostapd / Configuration file
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2007-2008, Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #ifndef CONFIG_NATIVE_WINDOWS
  17. #include <grp.h>
  18. #endif /* CONFIG_NATIVE_WINDOWS */
  19. #include "hostapd.h"
  20. #include "driver.h"
  21. #include "sha1.h"
  22. #include "eap_server/eap.h"
  23. #include "radius/radius_client.h"
  24. #include "wpa_common.h"
  25. #include "wpa.h"
  26. #include "uuid.h"
  27. #define MAX_STA_COUNT 2007
  28. extern struct wpa_driver_ops *hostapd_drivers[];
  29. static int hostapd_config_read_vlan_file(struct hostapd_bss_config *bss,
  30. const char *fname)
  31. {
  32. FILE *f;
  33. char buf[128], *pos, *pos2;
  34. int line = 0, vlan_id;
  35. struct hostapd_vlan *vlan;
  36. f = fopen(fname, "r");
  37. if (!f) {
  38. printf("VLAN file '%s' not readable.\n", fname);
  39. return -1;
  40. }
  41. while (fgets(buf, sizeof(buf), f)) {
  42. line++;
  43. if (buf[0] == '#')
  44. continue;
  45. pos = buf;
  46. while (*pos != '\0') {
  47. if (*pos == '\n') {
  48. *pos = '\0';
  49. break;
  50. }
  51. pos++;
  52. }
  53. if (buf[0] == '\0')
  54. continue;
  55. if (buf[0] == '*') {
  56. vlan_id = VLAN_ID_WILDCARD;
  57. pos = buf + 1;
  58. } else {
  59. vlan_id = strtol(buf, &pos, 10);
  60. if (buf == pos || vlan_id < 1 ||
  61. vlan_id > MAX_VLAN_ID) {
  62. printf("Invalid VLAN ID at line %d in '%s'\n",
  63. line, fname);
  64. fclose(f);
  65. return -1;
  66. }
  67. }
  68. while (*pos == ' ' || *pos == '\t')
  69. pos++;
  70. pos2 = pos;
  71. while (*pos2 != ' ' && *pos2 != '\t' && *pos2 != '\0')
  72. pos2++;
  73. *pos2 = '\0';
  74. if (*pos == '\0' || os_strlen(pos) > IFNAMSIZ) {
  75. printf("Invalid VLAN ifname at line %d in '%s'\n",
  76. line, fname);
  77. fclose(f);
  78. return -1;
  79. }
  80. vlan = os_malloc(sizeof(*vlan));
  81. if (vlan == NULL) {
  82. printf("Out of memory while reading VLAN interfaces "
  83. "from '%s'\n", fname);
  84. fclose(f);
  85. return -1;
  86. }
  87. os_memset(vlan, 0, sizeof(*vlan));
  88. vlan->vlan_id = vlan_id;
  89. os_strlcpy(vlan->ifname, pos, sizeof(vlan->ifname));
  90. if (bss->vlan_tail)
  91. bss->vlan_tail->next = vlan;
  92. else
  93. bss->vlan = vlan;
  94. bss->vlan_tail = vlan;
  95. }
  96. fclose(f);
  97. return 0;
  98. }
  99. static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
  100. {
  101. struct hostapd_vlan *vlan, *prev;
  102. vlan = bss->vlan;
  103. prev = NULL;
  104. while (vlan) {
  105. prev = vlan;
  106. vlan = vlan->next;
  107. os_free(prev);
  108. }
  109. bss->vlan = NULL;
  110. }
  111. /* convert floats with one decimal place to value*10 int, i.e.,
  112. * "1.5" will return 15 */
  113. static int hostapd_config_read_int10(const char *value)
  114. {
  115. int i, d;
  116. char *pos;
  117. i = atoi(value);
  118. pos = os_strchr(value, '.');
  119. d = 0;
  120. if (pos) {
  121. pos++;
  122. if (*pos >= '0' && *pos <= '9')
  123. d = *pos - '0';
  124. }
  125. return i * 10 + d;
  126. }
  127. static void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
  128. {
  129. bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
  130. bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
  131. bss->logger_syslog = (unsigned int) -1;
  132. bss->logger_stdout = (unsigned int) -1;
  133. bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
  134. bss->wep_rekeying_period = 300;
  135. /* use key0 in individual key and key1 in broadcast key */
  136. bss->broadcast_key_idx_min = 1;
  137. bss->broadcast_key_idx_max = 2;
  138. bss->eap_reauth_period = 3600;
  139. bss->wpa_group_rekey = 600;
  140. bss->wpa_gmk_rekey = 86400;
  141. bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  142. bss->wpa_pairwise = WPA_CIPHER_TKIP;
  143. bss->wpa_group = WPA_CIPHER_TKIP;
  144. bss->rsn_pairwise = 0;
  145. bss->max_num_sta = MAX_STA_COUNT;
  146. bss->dtim_period = 2;
  147. bss->radius_server_auth_port = 1812;
  148. bss->ap_max_inactivity = AP_MAX_INACTIVITY;
  149. bss->eapol_version = EAPOL_VERSION;
  150. bss->max_listen_interval = 65535;
  151. #ifdef CONFIG_IEEE80211W
  152. bss->assoc_ping_timeout = 1000;
  153. bss->assoc_ping_attempts = 3;
  154. #endif /* CONFIG_IEEE80211W */
  155. #ifdef EAP_FAST
  156. /* both anonymous and authenticated provisioning */
  157. bss->eap_fast_prov = 3;
  158. bss->pac_key_lifetime = 7 * 24 * 60 * 60;
  159. bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
  160. #endif /* EAP_FAST */
  161. }
  162. static struct hostapd_config * hostapd_config_defaults(void)
  163. {
  164. struct hostapd_config *conf;
  165. struct hostapd_bss_config *bss;
  166. int i;
  167. const int aCWmin = 15, aCWmax = 1024;
  168. const struct hostapd_wme_ac_params ac_bk =
  169. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  170. const struct hostapd_wme_ac_params ac_be =
  171. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  172. const struct hostapd_wme_ac_params ac_vi = /* video traffic */
  173. { aCWmin >> 1, aCWmin, 2, 3000 / 32, 1 };
  174. const struct hostapd_wme_ac_params ac_vo = /* voice traffic */
  175. { aCWmin >> 2, aCWmin >> 1, 2, 1500 / 32, 1 };
  176. conf = os_zalloc(sizeof(*conf));
  177. bss = os_zalloc(sizeof(*bss));
  178. if (conf == NULL || bss == NULL) {
  179. printf("Failed to allocate memory for configuration data.\n");
  180. os_free(conf);
  181. os_free(bss);
  182. return NULL;
  183. }
  184. /* set default driver based on configuration */
  185. conf->driver = hostapd_drivers[0];
  186. if (conf->driver == NULL) {
  187. printf("No driver wrappers registered!\n");
  188. os_free(conf);
  189. os_free(bss);
  190. return NULL;
  191. }
  192. bss->radius = os_zalloc(sizeof(*bss->radius));
  193. if (bss->radius == NULL) {
  194. os_free(conf);
  195. os_free(bss);
  196. return NULL;
  197. }
  198. hostapd_config_defaults_bss(bss);
  199. conf->num_bss = 1;
  200. conf->bss = bss;
  201. conf->beacon_int = 100;
  202. conf->rts_threshold = -1; /* use driver default: 2347 */
  203. conf->fragm_threshold = -1; /* user driver default: 2346 */
  204. conf->send_probe_response = 1;
  205. conf->bridge_packets = INTERNAL_BRIDGE_DO_NOT_CONTROL;
  206. os_memcpy(conf->country, "US ", 3);
  207. for (i = 0; i < NUM_TX_QUEUES; i++)
  208. conf->tx_queue[i].aifs = -1; /* use hw default */
  209. conf->wme_ac_params[0] = ac_be;
  210. conf->wme_ac_params[1] = ac_bk;
  211. conf->wme_ac_params[2] = ac_vi;
  212. conf->wme_ac_params[3] = ac_vo;
  213. #ifdef CONFIG_IEEE80211N
  214. SET_2BIT_LE16(&conf->ht_capab,
  215. HT_CAP_INFO_MIMO_PWR_SAVE_OFFSET,
  216. MIMO_PWR_NO_LIMIT_ON_MIMO_SEQS);
  217. conf->ht_capab |= HT_CAP_INFO_GREEN_FIELD;
  218. #endif /* CONFIG_IEEE80211N */
  219. return conf;
  220. }
  221. int hostapd_mac_comp(const void *a, const void *b)
  222. {
  223. return os_memcmp(a, b, sizeof(macaddr));
  224. }
  225. int hostapd_mac_comp_empty(const void *a)
  226. {
  227. macaddr empty = { 0 };
  228. return os_memcmp(a, empty, sizeof(macaddr));
  229. }
  230. static int hostapd_acl_comp(const void *a, const void *b)
  231. {
  232. const struct mac_acl_entry *aa = a;
  233. const struct mac_acl_entry *bb = b;
  234. return os_memcmp(aa->addr, bb->addr, sizeof(macaddr));
  235. }
  236. static int hostapd_config_read_maclist(const char *fname,
  237. struct mac_acl_entry **acl, int *num)
  238. {
  239. FILE *f;
  240. char buf[128], *pos;
  241. int line = 0;
  242. u8 addr[ETH_ALEN];
  243. struct mac_acl_entry *newacl;
  244. int vlan_id;
  245. if (!fname)
  246. return 0;
  247. f = fopen(fname, "r");
  248. if (!f) {
  249. printf("MAC list file '%s' not found.\n", fname);
  250. return -1;
  251. }
  252. while (fgets(buf, sizeof(buf), f)) {
  253. line++;
  254. if (buf[0] == '#')
  255. continue;
  256. pos = buf;
  257. while (*pos != '\0') {
  258. if (*pos == '\n') {
  259. *pos = '\0';
  260. break;
  261. }
  262. pos++;
  263. }
  264. if (buf[0] == '\0')
  265. continue;
  266. if (hwaddr_aton(buf, addr)) {
  267. printf("Invalid MAC address '%s' at line %d in '%s'\n",
  268. buf, line, fname);
  269. fclose(f);
  270. return -1;
  271. }
  272. vlan_id = 0;
  273. pos = buf;
  274. while (*pos != '\0' && *pos != ' ' && *pos != '\t')
  275. pos++;
  276. while (*pos == ' ' || *pos == '\t')
  277. pos++;
  278. if (*pos != '\0')
  279. vlan_id = atoi(pos);
  280. newacl = os_realloc(*acl, (*num + 1) * sizeof(**acl));
  281. if (newacl == NULL) {
  282. printf("MAC list reallocation failed\n");
  283. fclose(f);
  284. return -1;
  285. }
  286. *acl = newacl;
  287. os_memcpy((*acl)[*num].addr, addr, ETH_ALEN);
  288. (*acl)[*num].vlan_id = vlan_id;
  289. (*num)++;
  290. }
  291. fclose(f);
  292. qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
  293. return 0;
  294. }
  295. static int hostapd_config_read_wpa_psk(const char *fname,
  296. struct hostapd_ssid *ssid)
  297. {
  298. FILE *f;
  299. char buf[128], *pos;
  300. int line = 0, ret = 0, len, ok;
  301. u8 addr[ETH_ALEN];
  302. struct hostapd_wpa_psk *psk;
  303. if (!fname)
  304. return 0;
  305. f = fopen(fname, "r");
  306. if (!f) {
  307. printf("WPA PSK file '%s' not found.\n", fname);
  308. return -1;
  309. }
  310. while (fgets(buf, sizeof(buf), f)) {
  311. line++;
  312. if (buf[0] == '#')
  313. continue;
  314. pos = buf;
  315. while (*pos != '\0') {
  316. if (*pos == '\n') {
  317. *pos = '\0';
  318. break;
  319. }
  320. pos++;
  321. }
  322. if (buf[0] == '\0')
  323. continue;
  324. if (hwaddr_aton(buf, addr)) {
  325. printf("Invalid MAC address '%s' on line %d in '%s'\n",
  326. buf, line, fname);
  327. ret = -1;
  328. break;
  329. }
  330. psk = os_zalloc(sizeof(*psk));
  331. if (psk == NULL) {
  332. printf("WPA PSK allocation failed\n");
  333. ret = -1;
  334. break;
  335. }
  336. if (is_zero_ether_addr(addr))
  337. psk->group = 1;
  338. else
  339. os_memcpy(psk->addr, addr, ETH_ALEN);
  340. pos = buf + 17;
  341. if (pos == '\0') {
  342. printf("No PSK on line %d in '%s'\n", line, fname);
  343. os_free(psk);
  344. ret = -1;
  345. break;
  346. }
  347. pos++;
  348. ok = 0;
  349. len = os_strlen(pos);
  350. if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
  351. ok = 1;
  352. else if (len >= 8 && len < 64) {
  353. pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
  354. 4096, psk->psk, PMK_LEN);
  355. ok = 1;
  356. }
  357. if (!ok) {
  358. printf("Invalid PSK '%s' on line %d in '%s'\n",
  359. pos, line, fname);
  360. os_free(psk);
  361. ret = -1;
  362. break;
  363. }
  364. psk->next = ssid->wpa_psk;
  365. ssid->wpa_psk = psk;
  366. }
  367. fclose(f);
  368. return ret;
  369. }
  370. int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
  371. {
  372. struct hostapd_ssid *ssid = &conf->ssid;
  373. if (ssid->wpa_passphrase != NULL) {
  374. if (ssid->wpa_psk != NULL) {
  375. printf("Warning: both WPA PSK and passphrase set. "
  376. "Using passphrase.\n");
  377. os_free(ssid->wpa_psk);
  378. }
  379. ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  380. if (ssid->wpa_psk == NULL) {
  381. printf("Unable to alloc space for PSK\n");
  382. return -1;
  383. }
  384. wpa_hexdump_ascii(MSG_DEBUG, "SSID",
  385. (u8 *) ssid->ssid, ssid->ssid_len);
  386. wpa_hexdump_ascii(MSG_DEBUG, "PSK (ASCII passphrase)",
  387. (u8 *) ssid->wpa_passphrase,
  388. os_strlen(ssid->wpa_passphrase));
  389. pbkdf2_sha1(ssid->wpa_passphrase,
  390. ssid->ssid, ssid->ssid_len,
  391. 4096, ssid->wpa_psk->psk, PMK_LEN);
  392. wpa_hexdump(MSG_DEBUG, "PSK (from passphrase)",
  393. ssid->wpa_psk->psk, PMK_LEN);
  394. ssid->wpa_psk->group = 1;
  395. os_memset(ssid->wpa_passphrase, 0,
  396. os_strlen(ssid->wpa_passphrase));
  397. os_free(ssid->wpa_passphrase);
  398. ssid->wpa_passphrase = NULL;
  399. }
  400. if (ssid->wpa_psk_file) {
  401. if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
  402. &conf->ssid))
  403. return -1;
  404. }
  405. return 0;
  406. }
  407. #ifdef EAP_SERVER
  408. static int hostapd_config_read_eap_user(const char *fname,
  409. struct hostapd_bss_config *conf)
  410. {
  411. FILE *f;
  412. char buf[512], *pos, *start, *pos2;
  413. int line = 0, ret = 0, num_methods;
  414. struct hostapd_eap_user *user, *tail = NULL;
  415. if (!fname)
  416. return 0;
  417. f = fopen(fname, "r");
  418. if (!f) {
  419. printf("EAP user file '%s' not found.\n", fname);
  420. return -1;
  421. }
  422. /* Lines: "user" METHOD,METHOD2 "password" (password optional) */
  423. while (fgets(buf, sizeof(buf), f)) {
  424. line++;
  425. if (buf[0] == '#')
  426. continue;
  427. pos = buf;
  428. while (*pos != '\0') {
  429. if (*pos == '\n') {
  430. *pos = '\0';
  431. break;
  432. }
  433. pos++;
  434. }
  435. if (buf[0] == '\0')
  436. continue;
  437. user = NULL;
  438. if (buf[0] != '"' && buf[0] != '*') {
  439. printf("Invalid EAP identity (no \" in start) on "
  440. "line %d in '%s'\n", line, fname);
  441. goto failed;
  442. }
  443. user = os_zalloc(sizeof(*user));
  444. if (user == NULL) {
  445. printf("EAP user allocation failed\n");
  446. goto failed;
  447. }
  448. user->force_version = -1;
  449. if (buf[0] == '*') {
  450. pos = buf;
  451. } else {
  452. pos = buf + 1;
  453. start = pos;
  454. while (*pos != '"' && *pos != '\0')
  455. pos++;
  456. if (*pos == '\0') {
  457. printf("Invalid EAP identity (no \" in end) on"
  458. " line %d in '%s'\n", line, fname);
  459. goto failed;
  460. }
  461. user->identity = os_malloc(pos - start);
  462. if (user->identity == NULL) {
  463. printf("Failed to allocate memory for EAP "
  464. "identity\n");
  465. goto failed;
  466. }
  467. os_memcpy(user->identity, start, pos - start);
  468. user->identity_len = pos - start;
  469. if (pos[0] == '"' && pos[1] == '*') {
  470. user->wildcard_prefix = 1;
  471. pos++;
  472. }
  473. }
  474. pos++;
  475. while (*pos == ' ' || *pos == '\t')
  476. pos++;
  477. if (*pos == '\0') {
  478. printf("No EAP method on line %d in '%s'\n",
  479. line, fname);
  480. goto failed;
  481. }
  482. start = pos;
  483. while (*pos != ' ' && *pos != '\t' && *pos != '\0')
  484. pos++;
  485. if (*pos == '\0') {
  486. pos = NULL;
  487. } else {
  488. *pos = '\0';
  489. pos++;
  490. }
  491. num_methods = 0;
  492. while (*start) {
  493. char *pos3 = os_strchr(start, ',');
  494. if (pos3) {
  495. *pos3++ = '\0';
  496. }
  497. user->methods[num_methods].method =
  498. eap_server_get_type(
  499. start,
  500. &user->methods[num_methods].vendor);
  501. if (user->methods[num_methods].vendor ==
  502. EAP_VENDOR_IETF &&
  503. user->methods[num_methods].method == EAP_TYPE_NONE)
  504. {
  505. if (os_strcmp(start, "TTLS-PAP") == 0) {
  506. user->ttls_auth |= EAP_TTLS_AUTH_PAP;
  507. goto skip_eap;
  508. }
  509. if (os_strcmp(start, "TTLS-CHAP") == 0) {
  510. user->ttls_auth |= EAP_TTLS_AUTH_CHAP;
  511. goto skip_eap;
  512. }
  513. if (os_strcmp(start, "TTLS-MSCHAP") == 0) {
  514. user->ttls_auth |=
  515. EAP_TTLS_AUTH_MSCHAP;
  516. goto skip_eap;
  517. }
  518. if (os_strcmp(start, "TTLS-MSCHAPV2") == 0) {
  519. user->ttls_auth |=
  520. EAP_TTLS_AUTH_MSCHAPV2;
  521. goto skip_eap;
  522. }
  523. printf("Unsupported EAP type '%s' on line %d "
  524. "in '%s'\n", start, line, fname);
  525. goto failed;
  526. }
  527. num_methods++;
  528. if (num_methods >= EAP_USER_MAX_METHODS)
  529. break;
  530. skip_eap:
  531. if (pos3 == NULL)
  532. break;
  533. start = pos3;
  534. }
  535. if (num_methods == 0 && user->ttls_auth == 0) {
  536. printf("No EAP types configured on line %d in '%s'\n",
  537. line, fname);
  538. goto failed;
  539. }
  540. if (pos == NULL)
  541. goto done;
  542. while (*pos == ' ' || *pos == '\t')
  543. pos++;
  544. if (*pos == '\0')
  545. goto done;
  546. if (os_strncmp(pos, "[ver=0]", 7) == 0) {
  547. user->force_version = 0;
  548. goto done;
  549. }
  550. if (os_strncmp(pos, "[ver=1]", 7) == 0) {
  551. user->force_version = 1;
  552. goto done;
  553. }
  554. if (os_strncmp(pos, "[2]", 3) == 0) {
  555. user->phase2 = 1;
  556. goto done;
  557. }
  558. if (*pos == '"') {
  559. pos++;
  560. start = pos;
  561. while (*pos != '"' && *pos != '\0')
  562. pos++;
  563. if (*pos == '\0') {
  564. printf("Invalid EAP password (no \" in end) "
  565. "on line %d in '%s'\n", line, fname);
  566. goto failed;
  567. }
  568. user->password = os_malloc(pos - start);
  569. if (user->password == NULL) {
  570. printf("Failed to allocate memory for EAP "
  571. "password\n");
  572. goto failed;
  573. }
  574. os_memcpy(user->password, start, pos - start);
  575. user->password_len = pos - start;
  576. pos++;
  577. } else if (os_strncmp(pos, "hash:", 5) == 0) {
  578. pos += 5;
  579. pos2 = pos;
  580. while (*pos2 != '\0' && *pos2 != ' ' &&
  581. *pos2 != '\t' && *pos2 != '#')
  582. pos2++;
  583. if (pos2 - pos != 32) {
  584. printf("Invalid password hash on line %d in "
  585. "'%s'\n", line, fname);
  586. goto failed;
  587. }
  588. user->password = os_malloc(16);
  589. if (user->password == NULL) {
  590. printf("Failed to allocate memory for EAP "
  591. "password hash\n");
  592. goto failed;
  593. }
  594. if (hexstr2bin(pos, user->password, 16) < 0) {
  595. printf("Invalid hash password on line %d in "
  596. "'%s'\n", line, fname);
  597. goto failed;
  598. }
  599. user->password_len = 16;
  600. user->password_hash = 1;
  601. pos = pos2;
  602. } else {
  603. pos2 = pos;
  604. while (*pos2 != '\0' && *pos2 != ' ' &&
  605. *pos2 != '\t' && *pos2 != '#')
  606. pos2++;
  607. if ((pos2 - pos) & 1) {
  608. printf("Invalid hex password on line %d in "
  609. "'%s'\n", line, fname);
  610. goto failed;
  611. }
  612. user->password = os_malloc((pos2 - pos) / 2);
  613. if (user->password == NULL) {
  614. printf("Failed to allocate memory for EAP "
  615. "password\n");
  616. goto failed;
  617. }
  618. if (hexstr2bin(pos, user->password,
  619. (pos2 - pos) / 2) < 0) {
  620. printf("Invalid hex password on line %d in "
  621. "'%s'\n", line, fname);
  622. goto failed;
  623. }
  624. user->password_len = (pos2 - pos) / 2;
  625. pos = pos2;
  626. }
  627. while (*pos == ' ' || *pos == '\t')
  628. pos++;
  629. if (os_strncmp(pos, "[2]", 3) == 0) {
  630. user->phase2 = 1;
  631. }
  632. done:
  633. if (tail == NULL) {
  634. tail = conf->eap_user = user;
  635. } else {
  636. tail->next = user;
  637. tail = user;
  638. }
  639. continue;
  640. failed:
  641. if (user) {
  642. os_free(user->password);
  643. os_free(user->identity);
  644. os_free(user);
  645. }
  646. ret = -1;
  647. break;
  648. }
  649. fclose(f);
  650. return ret;
  651. }
  652. #endif /* EAP_SERVER */
  653. static int
  654. hostapd_config_read_radius_addr(struct hostapd_radius_server **server,
  655. int *num_server, const char *val, int def_port,
  656. struct hostapd_radius_server **curr_serv)
  657. {
  658. struct hostapd_radius_server *nserv;
  659. int ret;
  660. static int server_index = 1;
  661. nserv = os_realloc(*server, (*num_server + 1) * sizeof(*nserv));
  662. if (nserv == NULL)
  663. return -1;
  664. *server = nserv;
  665. nserv = &nserv[*num_server];
  666. (*num_server)++;
  667. (*curr_serv) = nserv;
  668. os_memset(nserv, 0, sizeof(*nserv));
  669. nserv->port = def_port;
  670. ret = hostapd_parse_ip_addr(val, &nserv->addr);
  671. nserv->index = server_index++;
  672. return ret;
  673. }
  674. static int hostapd_config_parse_key_mgmt(int line, const char *value)
  675. {
  676. int val = 0, last;
  677. char *start, *end, *buf;
  678. buf = os_strdup(value);
  679. if (buf == NULL)
  680. return -1;
  681. start = buf;
  682. while (start != '\0') {
  683. while (*start == ' ' || *start == '\t')
  684. start++;
  685. if (*start == '\0')
  686. break;
  687. end = start;
  688. while (*end != ' ' && *end != '\t' && *end != '\0')
  689. end++;
  690. last = *end == '\0';
  691. *end = '\0';
  692. if (os_strcmp(start, "WPA-PSK") == 0)
  693. val |= WPA_KEY_MGMT_PSK;
  694. else if (os_strcmp(start, "WPA-EAP") == 0)
  695. val |= WPA_KEY_MGMT_IEEE8021X;
  696. #ifdef CONFIG_IEEE80211R
  697. else if (os_strcmp(start, "FT-PSK") == 0)
  698. val |= WPA_KEY_MGMT_FT_PSK;
  699. else if (os_strcmp(start, "FT-EAP") == 0)
  700. val |= WPA_KEY_MGMT_FT_IEEE8021X;
  701. #endif /* CONFIG_IEEE80211R */
  702. #ifdef CONFIG_IEEE80211W
  703. else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
  704. val |= WPA_KEY_MGMT_PSK_SHA256;
  705. else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
  706. val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
  707. #endif /* CONFIG_IEEE80211W */
  708. else {
  709. printf("Line %d: invalid key_mgmt '%s'\n",
  710. line, start);
  711. os_free(buf);
  712. return -1;
  713. }
  714. if (last)
  715. break;
  716. start = end + 1;
  717. }
  718. os_free(buf);
  719. if (val == 0) {
  720. printf("Line %d: no key_mgmt values configured.\n", line);
  721. return -1;
  722. }
  723. return val;
  724. }
  725. static int hostapd_config_parse_cipher(int line, const char *value)
  726. {
  727. int val = 0, last;
  728. char *start, *end, *buf;
  729. buf = os_strdup(value);
  730. if (buf == NULL)
  731. return -1;
  732. start = buf;
  733. while (start != '\0') {
  734. while (*start == ' ' || *start == '\t')
  735. start++;
  736. if (*start == '\0')
  737. break;
  738. end = start;
  739. while (*end != ' ' && *end != '\t' && *end != '\0')
  740. end++;
  741. last = *end == '\0';
  742. *end = '\0';
  743. if (os_strcmp(start, "CCMP") == 0)
  744. val |= WPA_CIPHER_CCMP;
  745. else if (os_strcmp(start, "TKIP") == 0)
  746. val |= WPA_CIPHER_TKIP;
  747. else if (os_strcmp(start, "WEP104") == 0)
  748. val |= WPA_CIPHER_WEP104;
  749. else if (os_strcmp(start, "WEP40") == 0)
  750. val |= WPA_CIPHER_WEP40;
  751. else if (os_strcmp(start, "NONE") == 0)
  752. val |= WPA_CIPHER_NONE;
  753. else {
  754. printf("Line %d: invalid cipher '%s'.", line, start);
  755. os_free(buf);
  756. return -1;
  757. }
  758. if (last)
  759. break;
  760. start = end + 1;
  761. }
  762. os_free(buf);
  763. if (val == 0) {
  764. printf("Line %d: no cipher values configured.", line);
  765. return -1;
  766. }
  767. return val;
  768. }
  769. static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
  770. struct hostapd_config *conf)
  771. {
  772. if (bss->ieee802_1x && !bss->eap_server &&
  773. !bss->radius->auth_servers) {
  774. printf("Invalid IEEE 802.1X configuration (no EAP "
  775. "authenticator configured).\n");
  776. return -1;
  777. }
  778. if (bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
  779. bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
  780. bss->ssid.wpa_psk_file == NULL) {
  781. printf("WPA-PSK enabled, but PSK or passphrase is not "
  782. "configured.\n");
  783. return -1;
  784. }
  785. if (hostapd_mac_comp_empty(bss->bssid) != 0) {
  786. size_t i;
  787. for (i = 0; i < conf->num_bss; i++) {
  788. if ((&conf->bss[i] != bss) &&
  789. (hostapd_mac_comp(conf->bss[i].bssid,
  790. bss->bssid) == 0)) {
  791. printf("Duplicate BSSID " MACSTR
  792. " on interface '%s' and '%s'.\n",
  793. MAC2STR(bss->bssid),
  794. conf->bss[i].iface, bss->iface);
  795. return -1;
  796. }
  797. }
  798. }
  799. #ifdef CONFIG_IEEE80211R
  800. if ((bss->wpa_key_mgmt &
  801. (WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X)) &&
  802. (bss->nas_identifier == NULL ||
  803. os_strlen(bss->nas_identifier) < 1 ||
  804. os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
  805. printf("FT (IEEE 802.11r) requires nas_identifier to be "
  806. "configured as a 1..48 octet string\n");
  807. return -1;
  808. }
  809. #endif /* CONFIG_IEEE80211R */
  810. return 0;
  811. }
  812. static int hostapd_config_check(struct hostapd_config *conf)
  813. {
  814. size_t i;
  815. for (i = 0; i < conf->num_bss; i++) {
  816. if (hostapd_config_check_bss(&conf->bss[i], conf))
  817. return -1;
  818. }
  819. return 0;
  820. }
  821. static int hostapd_config_read_wep(struct hostapd_wep_keys *wep, int keyidx,
  822. char *val)
  823. {
  824. size_t len = os_strlen(val);
  825. if (keyidx < 0 || keyidx > 3 || wep->key[keyidx] != NULL)
  826. return -1;
  827. if (val[0] == '"') {
  828. if (len < 2 || val[len - 1] != '"')
  829. return -1;
  830. len -= 2;
  831. wep->key[keyidx] = os_malloc(len);
  832. if (wep->key[keyidx] == NULL)
  833. return -1;
  834. os_memcpy(wep->key[keyidx], val + 1, len);
  835. wep->len[keyidx] = len;
  836. } else {
  837. if (len & 1)
  838. return -1;
  839. len /= 2;
  840. wep->key[keyidx] = os_malloc(len);
  841. if (wep->key[keyidx] == NULL)
  842. return -1;
  843. wep->len[keyidx] = len;
  844. if (hexstr2bin(val, wep->key[keyidx], len) < 0)
  845. return -1;
  846. }
  847. wep->keys_set++;
  848. return 0;
  849. }
  850. static int hostapd_parse_rates(int **rate_list, char *val)
  851. {
  852. int *list;
  853. int count;
  854. char *pos, *end;
  855. os_free(*rate_list);
  856. *rate_list = NULL;
  857. pos = val;
  858. count = 0;
  859. while (*pos != '\0') {
  860. if (*pos == ' ')
  861. count++;
  862. pos++;
  863. }
  864. list = os_malloc(sizeof(int) * (count + 2));
  865. if (list == NULL)
  866. return -1;
  867. pos = val;
  868. count = 0;
  869. while (*pos != '\0') {
  870. end = os_strchr(pos, ' ');
  871. if (end)
  872. *end = '\0';
  873. list[count++] = atoi(pos);
  874. if (!end)
  875. break;
  876. pos = end + 1;
  877. }
  878. list[count] = -1;
  879. *rate_list = list;
  880. return 0;
  881. }
  882. static int hostapd_config_bss(struct hostapd_config *conf, const char *ifname)
  883. {
  884. struct hostapd_bss_config *bss;
  885. if (*ifname == '\0')
  886. return -1;
  887. bss = os_realloc(conf->bss, (conf->num_bss + 1) *
  888. sizeof(struct hostapd_bss_config));
  889. if (bss == NULL) {
  890. printf("Failed to allocate memory for multi-BSS entry\n");
  891. return -1;
  892. }
  893. conf->bss = bss;
  894. bss = &(conf->bss[conf->num_bss]);
  895. os_memset(bss, 0, sizeof(*bss));
  896. bss->radius = os_zalloc(sizeof(*bss->radius));
  897. if (bss->radius == NULL) {
  898. printf("Failed to allocate memory for multi-BSS RADIUS "
  899. "data\n");
  900. return -1;
  901. }
  902. conf->num_bss++;
  903. conf->last_bss = bss;
  904. hostapd_config_defaults_bss(bss);
  905. os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
  906. os_memcpy(bss->ssid.vlan, bss->iface, IFNAMSIZ + 1);
  907. return 0;
  908. }
  909. static int valid_cw(int cw)
  910. {
  911. return (cw == 1 || cw == 3 || cw == 7 || cw == 15 || cw == 31 ||
  912. cw == 63 || cw == 127 || cw == 255 || cw == 511 || cw == 1023);
  913. }
  914. enum {
  915. IEEE80211_TX_QUEUE_DATA0 = 0, /* used for EDCA AC_VO data */
  916. IEEE80211_TX_QUEUE_DATA1 = 1, /* used for EDCA AC_VI data */
  917. IEEE80211_TX_QUEUE_DATA2 = 2, /* used for EDCA AC_BE data */
  918. IEEE80211_TX_QUEUE_DATA3 = 3, /* used for EDCA AC_BK data */
  919. IEEE80211_TX_QUEUE_DATA4 = 4,
  920. IEEE80211_TX_QUEUE_AFTER_BEACON = 6,
  921. IEEE80211_TX_QUEUE_BEACON = 7
  922. };
  923. static int hostapd_config_tx_queue(struct hostapd_config *conf, char *name,
  924. char *val)
  925. {
  926. int num;
  927. char *pos;
  928. struct hostapd_tx_queue_params *queue;
  929. /* skip 'tx_queue_' prefix */
  930. pos = name + 9;
  931. if (os_strncmp(pos, "data", 4) == 0 &&
  932. pos[4] >= '0' && pos[4] <= '9' && pos[5] == '_') {
  933. num = pos[4] - '0';
  934. pos += 6;
  935. } else if (os_strncmp(pos, "after_beacon_", 13) == 0) {
  936. num = IEEE80211_TX_QUEUE_AFTER_BEACON;
  937. pos += 13;
  938. } else if (os_strncmp(pos, "beacon_", 7) == 0) {
  939. num = IEEE80211_TX_QUEUE_BEACON;
  940. pos += 7;
  941. } else {
  942. printf("Unknown tx_queue name '%s'\n", pos);
  943. return -1;
  944. }
  945. queue = &conf->tx_queue[num];
  946. if (os_strcmp(pos, "aifs") == 0) {
  947. queue->aifs = atoi(val);
  948. if (queue->aifs < 0 || queue->aifs > 255) {
  949. printf("Invalid AIFS value %d\n", queue->aifs);
  950. return -1;
  951. }
  952. } else if (os_strcmp(pos, "cwmin") == 0) {
  953. queue->cwmin = atoi(val);
  954. if (!valid_cw(queue->cwmin)) {
  955. printf("Invalid cwMin value %d\n", queue->cwmin);
  956. return -1;
  957. }
  958. } else if (os_strcmp(pos, "cwmax") == 0) {
  959. queue->cwmax = atoi(val);
  960. if (!valid_cw(queue->cwmax)) {
  961. printf("Invalid cwMax value %d\n", queue->cwmax);
  962. return -1;
  963. }
  964. } else if (os_strcmp(pos, "burst") == 0) {
  965. queue->burst = hostapd_config_read_int10(val);
  966. } else {
  967. printf("Unknown tx_queue field '%s'\n", pos);
  968. return -1;
  969. }
  970. queue->configured = 1;
  971. return 0;
  972. }
  973. static int hostapd_config_wme_ac(struct hostapd_config *conf, char *name,
  974. char *val)
  975. {
  976. int num, v;
  977. char *pos;
  978. struct hostapd_wme_ac_params *ac;
  979. /* skip 'wme_ac_' prefix */
  980. pos = name + 7;
  981. if (os_strncmp(pos, "be_", 3) == 0) {
  982. num = 0;
  983. pos += 3;
  984. } else if (os_strncmp(pos, "bk_", 3) == 0) {
  985. num = 1;
  986. pos += 3;
  987. } else if (os_strncmp(pos, "vi_", 3) == 0) {
  988. num = 2;
  989. pos += 3;
  990. } else if (os_strncmp(pos, "vo_", 3) == 0) {
  991. num = 3;
  992. pos += 3;
  993. } else {
  994. printf("Unknown wme name '%s'\n", pos);
  995. return -1;
  996. }
  997. ac = &conf->wme_ac_params[num];
  998. if (os_strcmp(pos, "aifs") == 0) {
  999. v = atoi(val);
  1000. if (v < 1 || v > 255) {
  1001. printf("Invalid AIFS value %d\n", v);
  1002. return -1;
  1003. }
  1004. ac->aifs = v;
  1005. } else if (os_strcmp(pos, "cwmin") == 0) {
  1006. v = atoi(val);
  1007. if (v < 0 || v > 12) {
  1008. printf("Invalid cwMin value %d\n", v);
  1009. return -1;
  1010. }
  1011. ac->cwmin = v;
  1012. } else if (os_strcmp(pos, "cwmax") == 0) {
  1013. v = atoi(val);
  1014. if (v < 0 || v > 12) {
  1015. printf("Invalid cwMax value %d\n", v);
  1016. return -1;
  1017. }
  1018. ac->cwmax = v;
  1019. } else if (os_strcmp(pos, "txop_limit") == 0) {
  1020. v = atoi(val);
  1021. if (v < 0 || v > 0xffff) {
  1022. printf("Invalid txop value %d\n", v);
  1023. return -1;
  1024. }
  1025. ac->txopLimit = v;
  1026. } else if (os_strcmp(pos, "acm") == 0) {
  1027. v = atoi(val);
  1028. if (v < 0 || v > 1) {
  1029. printf("Invalid acm value %d\n", v);
  1030. return -1;
  1031. }
  1032. ac->admission_control_mandatory = v;
  1033. } else {
  1034. printf("Unknown wme_ac_ field '%s'\n", pos);
  1035. return -1;
  1036. }
  1037. return 0;
  1038. }
  1039. #ifdef CONFIG_IEEE80211R
  1040. static int add_r0kh(struct hostapd_bss_config *bss, char *value)
  1041. {
  1042. struct ft_remote_r0kh *r0kh;
  1043. char *pos, *next;
  1044. r0kh = os_zalloc(sizeof(*r0kh));
  1045. if (r0kh == NULL)
  1046. return -1;
  1047. /* 02:01:02:03:04:05 a.example.com 000102030405060708090a0b0c0d0e0f */
  1048. pos = value;
  1049. next = os_strchr(pos, ' ');
  1050. if (next)
  1051. *next++ = '\0';
  1052. if (next == NULL || hwaddr_aton(pos, r0kh->addr)) {
  1053. printf("Invalid R0KH MAC address: '%s'\n", pos);
  1054. os_free(r0kh);
  1055. return -1;
  1056. }
  1057. pos = next;
  1058. next = os_strchr(pos, ' ');
  1059. if (next)
  1060. *next++ = '\0';
  1061. if (next == NULL || next - pos > FT_R0KH_ID_MAX_LEN) {
  1062. printf("Invalid R0KH-ID: '%s'\n", pos);
  1063. os_free(r0kh);
  1064. return -1;
  1065. }
  1066. r0kh->id_len = next - pos - 1;
  1067. os_memcpy(r0kh->id, pos, r0kh->id_len);
  1068. pos = next;
  1069. if (hexstr2bin(pos, r0kh->key, sizeof(r0kh->key))) {
  1070. printf("Invalid R0KH key: '%s'\n", pos);
  1071. os_free(r0kh);
  1072. return -1;
  1073. }
  1074. r0kh->next = bss->r0kh_list;
  1075. bss->r0kh_list = r0kh;
  1076. return 0;
  1077. }
  1078. static int add_r1kh(struct hostapd_bss_config *bss, char *value)
  1079. {
  1080. struct ft_remote_r1kh *r1kh;
  1081. char *pos, *next;
  1082. r1kh = os_zalloc(sizeof(*r1kh));
  1083. if (r1kh == NULL)
  1084. return -1;
  1085. /* 02:01:02:03:04:05 02:01:02:03:04:05
  1086. * 000102030405060708090a0b0c0d0e0f */
  1087. pos = value;
  1088. next = os_strchr(pos, ' ');
  1089. if (next)
  1090. *next++ = '\0';
  1091. if (next == NULL || hwaddr_aton(pos, r1kh->addr)) {
  1092. printf("Invalid R1KH MAC address: '%s'\n", pos);
  1093. os_free(r1kh);
  1094. return -1;
  1095. }
  1096. pos = next;
  1097. next = os_strchr(pos, ' ');
  1098. if (next)
  1099. *next++ = '\0';
  1100. if (next == NULL || hwaddr_aton(pos, r1kh->id)) {
  1101. printf("Invalid R1KH-ID: '%s'\n", pos);
  1102. os_free(r1kh);
  1103. return -1;
  1104. }
  1105. pos = next;
  1106. if (hexstr2bin(pos, r1kh->key, sizeof(r1kh->key))) {
  1107. printf("Invalid R1KH key: '%s'\n", pos);
  1108. os_free(r1kh);
  1109. return -1;
  1110. }
  1111. r1kh->next = bss->r1kh_list;
  1112. bss->r1kh_list = r1kh;
  1113. return 0;
  1114. }
  1115. #endif /* CONFIG_IEEE80211R */
  1116. struct hostapd_config * hostapd_config_read(const char *fname)
  1117. {
  1118. struct hostapd_config *conf;
  1119. struct hostapd_bss_config *bss;
  1120. FILE *f;
  1121. char buf[256], *pos;
  1122. int line = 0;
  1123. int errors = 0;
  1124. int pairwise;
  1125. size_t i;
  1126. f = fopen(fname, "r");
  1127. if (f == NULL) {
  1128. printf("Could not open configuration file '%s' for reading.\n",
  1129. fname);
  1130. return NULL;
  1131. }
  1132. conf = hostapd_config_defaults();
  1133. if (conf == NULL) {
  1134. fclose(f);
  1135. return NULL;
  1136. }
  1137. bss = conf->last_bss = conf->bss;
  1138. while (fgets(buf, sizeof(buf), f)) {
  1139. bss = conf->last_bss;
  1140. line++;
  1141. if (buf[0] == '#')
  1142. continue;
  1143. pos = buf;
  1144. while (*pos != '\0') {
  1145. if (*pos == '\n') {
  1146. *pos = '\0';
  1147. break;
  1148. }
  1149. pos++;
  1150. }
  1151. if (buf[0] == '\0')
  1152. continue;
  1153. pos = os_strchr(buf, '=');
  1154. if (pos == NULL) {
  1155. printf("Line %d: invalid line '%s'\n", line, buf);
  1156. errors++;
  1157. continue;
  1158. }
  1159. *pos = '\0';
  1160. pos++;
  1161. if (os_strcmp(buf, "interface") == 0) {
  1162. os_strlcpy(conf->bss[0].iface, pos,
  1163. sizeof(conf->bss[0].iface));
  1164. } else if (os_strcmp(buf, "bridge") == 0) {
  1165. os_strlcpy(bss->bridge, pos, sizeof(bss->bridge));
  1166. } else if (os_strcmp(buf, "driver") == 0) {
  1167. int i;
  1168. /* clear to get error below if setting is invalid */
  1169. conf->driver = NULL;
  1170. for (i = 0; hostapd_drivers[i]; i++) {
  1171. if (os_strcmp(pos, hostapd_drivers[i]->name) ==
  1172. 0) {
  1173. conf->driver = hostapd_drivers[i];
  1174. break;
  1175. }
  1176. }
  1177. if (conf->driver == NULL) {
  1178. printf("Line %d: invalid/unknown driver "
  1179. "'%s'\n", line, pos);
  1180. errors++;
  1181. }
  1182. } else if (os_strcmp(buf, "debug") == 0) {
  1183. wpa_printf(MSG_DEBUG, "Line %d: DEPRECATED: 'debug' "
  1184. "configuration variable is not used "
  1185. "anymore", line);
  1186. } else if (os_strcmp(buf, "logger_syslog_level") == 0) {
  1187. bss->logger_syslog_level = atoi(pos);
  1188. } else if (os_strcmp(buf, "logger_stdout_level") == 0) {
  1189. bss->logger_stdout_level = atoi(pos);
  1190. } else if (os_strcmp(buf, "logger_syslog") == 0) {
  1191. bss->logger_syslog = atoi(pos);
  1192. } else if (os_strcmp(buf, "logger_stdout") == 0) {
  1193. bss->logger_stdout = atoi(pos);
  1194. } else if (os_strcmp(buf, "dump_file") == 0) {
  1195. bss->dump_log_name = os_strdup(pos);
  1196. } else if (os_strcmp(buf, "ssid") == 0) {
  1197. bss->ssid.ssid_len = os_strlen(pos);
  1198. if (bss->ssid.ssid_len > HOSTAPD_MAX_SSID_LEN ||
  1199. bss->ssid.ssid_len < 1) {
  1200. printf("Line %d: invalid SSID '%s'\n", line,
  1201. pos);
  1202. errors++;
  1203. } else {
  1204. os_memcpy(bss->ssid.ssid, pos,
  1205. bss->ssid.ssid_len);
  1206. bss->ssid.ssid[bss->ssid.ssid_len] = '\0';
  1207. bss->ssid.ssid_set = 1;
  1208. }
  1209. } else if (os_strcmp(buf, "macaddr_acl") == 0) {
  1210. bss->macaddr_acl = atoi(pos);
  1211. if (bss->macaddr_acl != ACCEPT_UNLESS_DENIED &&
  1212. bss->macaddr_acl != DENY_UNLESS_ACCEPTED &&
  1213. bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
  1214. printf("Line %d: unknown macaddr_acl %d\n",
  1215. line, bss->macaddr_acl);
  1216. }
  1217. } else if (os_strcmp(buf, "accept_mac_file") == 0) {
  1218. if (hostapd_config_read_maclist(pos, &bss->accept_mac,
  1219. &bss->num_accept_mac))
  1220. {
  1221. printf("Line %d: Failed to read "
  1222. "accept_mac_file '%s'\n",
  1223. line, pos);
  1224. errors++;
  1225. }
  1226. } else if (os_strcmp(buf, "deny_mac_file") == 0) {
  1227. if (hostapd_config_read_maclist(pos, &bss->deny_mac,
  1228. &bss->num_deny_mac))
  1229. {
  1230. printf("Line %d: Failed to read "
  1231. "deny_mac_file '%s'\n",
  1232. line, pos);
  1233. errors++;
  1234. }
  1235. } else if (os_strcmp(buf, "ap_max_inactivity") == 0) {
  1236. bss->ap_max_inactivity = atoi(pos);
  1237. } else if (os_strcmp(buf, "country_code") == 0) {
  1238. os_memcpy(conf->country, pos, 2);
  1239. /* FIX: make this configurable */
  1240. conf->country[2] = ' ';
  1241. } else if (os_strcmp(buf, "ieee80211d") == 0) {
  1242. conf->ieee80211d = atoi(pos);
  1243. } else if (os_strcmp(buf, "assoc_ap_addr") == 0) {
  1244. if (hwaddr_aton(pos, bss->assoc_ap_addr)) {
  1245. printf("Line %d: invalid MAC address '%s'\n",
  1246. line, pos);
  1247. errors++;
  1248. }
  1249. bss->assoc_ap = 1;
  1250. } else if (os_strcmp(buf, "ieee8021x") == 0) {
  1251. bss->ieee802_1x = atoi(pos);
  1252. } else if (os_strcmp(buf, "eapol_version") == 0) {
  1253. bss->eapol_version = atoi(pos);
  1254. if (bss->eapol_version < 1 ||
  1255. bss->eapol_version > 2) {
  1256. printf("Line %d: invalid EAPOL "
  1257. "version (%d): '%s'.\n",
  1258. line, bss->eapol_version, pos);
  1259. errors++;
  1260. } else
  1261. wpa_printf(MSG_DEBUG, "eapol_version=%d",
  1262. bss->eapol_version);
  1263. #ifdef EAP_SERVER
  1264. } else if (os_strcmp(buf, "eap_authenticator") == 0) {
  1265. bss->eap_server = atoi(pos);
  1266. printf("Line %d: obsolete eap_authenticator used; "
  1267. "this has been renamed to eap_server\n", line);
  1268. } else if (os_strcmp(buf, "eap_server") == 0) {
  1269. bss->eap_server = atoi(pos);
  1270. } else if (os_strcmp(buf, "eap_user_file") == 0) {
  1271. if (hostapd_config_read_eap_user(pos, bss))
  1272. errors++;
  1273. } else if (os_strcmp(buf, "ca_cert") == 0) {
  1274. os_free(bss->ca_cert);
  1275. bss->ca_cert = os_strdup(pos);
  1276. } else if (os_strcmp(buf, "server_cert") == 0) {
  1277. os_free(bss->server_cert);
  1278. bss->server_cert = os_strdup(pos);
  1279. } else if (os_strcmp(buf, "private_key") == 0) {
  1280. os_free(bss->private_key);
  1281. bss->private_key = os_strdup(pos);
  1282. } else if (os_strcmp(buf, "private_key_passwd") == 0) {
  1283. os_free(bss->private_key_passwd);
  1284. bss->private_key_passwd = os_strdup(pos);
  1285. } else if (os_strcmp(buf, "check_crl") == 0) {
  1286. bss->check_crl = atoi(pos);
  1287. } else if (os_strcmp(buf, "dh_file") == 0) {
  1288. os_free(bss->dh_file);
  1289. bss->dh_file = os_strdup(pos);
  1290. #ifdef EAP_FAST
  1291. } else if (os_strcmp(buf, "pac_opaque_encr_key") == 0) {
  1292. os_free(bss->pac_opaque_encr_key);
  1293. bss->pac_opaque_encr_key = os_malloc(16);
  1294. if (bss->pac_opaque_encr_key == NULL) {
  1295. printf("Line %d: No memory for "
  1296. "pac_opque_encr_key\n", line);
  1297. errors++;
  1298. } else if (hexstr2bin(pos, bss->pac_opaque_encr_key,
  1299. 16)) {
  1300. printf("Line %d: Invalid pac_opque_encr_key\n",
  1301. line);
  1302. errors++;
  1303. }
  1304. } else if (os_strcmp(buf, "eap_fast_a_id") == 0) {
  1305. size_t idlen = os_strlen(pos);
  1306. if (idlen & 1) {
  1307. printf("Line %d: Invalid eap_fast_a_id\n",
  1308. line);
  1309. errors++;
  1310. } else {
  1311. os_free(bss->eap_fast_a_id);
  1312. bss->eap_fast_a_id = os_malloc(idlen / 2);
  1313. if (bss->eap_fast_a_id == NULL ||
  1314. hexstr2bin(pos, bss->eap_fast_a_id,
  1315. idlen / 2)) {
  1316. printf("Line %d: Failed to parse "
  1317. "eap_fast_a_id\n", line);
  1318. errors++;
  1319. } else
  1320. bss->eap_fast_a_id_len = idlen / 2;
  1321. }
  1322. } else if (os_strcmp(buf, "eap_fast_a_id_info") == 0) {
  1323. os_free(bss->eap_fast_a_id_info);
  1324. bss->eap_fast_a_id_info = os_strdup(pos);
  1325. } else if (os_strcmp(buf, "eap_fast_prov") == 0) {
  1326. bss->eap_fast_prov = atoi(pos);
  1327. } else if (os_strcmp(buf, "pac_key_lifetime") == 0) {
  1328. bss->pac_key_lifetime = atoi(pos);
  1329. } else if (os_strcmp(buf, "pac_key_refresh_time") == 0) {
  1330. bss->pac_key_refresh_time = atoi(pos);
  1331. #endif /* EAP_FAST */
  1332. #ifdef EAP_SIM
  1333. } else if (os_strcmp(buf, "eap_sim_db") == 0) {
  1334. os_free(bss->eap_sim_db);
  1335. bss->eap_sim_db = os_strdup(pos);
  1336. } else if (os_strcmp(buf, "eap_sim_aka_result_ind") == 0) {
  1337. bss->eap_sim_aka_result_ind = atoi(pos);
  1338. #endif /* EAP_SIM */
  1339. #ifdef EAP_TNC
  1340. } else if (os_strcmp(buf, "tnc") == 0) {
  1341. bss->tnc = atoi(pos);
  1342. #endif /* EAP_TNC */
  1343. #endif /* EAP_SERVER */
  1344. } else if (os_strcmp(buf, "eap_message") == 0) {
  1345. char *term;
  1346. bss->eap_req_id_text = os_strdup(pos);
  1347. if (bss->eap_req_id_text == NULL) {
  1348. printf("Line %d: Failed to allocate memory "
  1349. "for eap_req_id_text\n", line);
  1350. errors++;
  1351. continue;
  1352. }
  1353. bss->eap_req_id_text_len =
  1354. os_strlen(bss->eap_req_id_text);
  1355. term = os_strstr(bss->eap_req_id_text, "\\0");
  1356. if (term) {
  1357. *term++ = '\0';
  1358. os_memmove(term, term + 1,
  1359. bss->eap_req_id_text_len -
  1360. (term - bss->eap_req_id_text) - 1);
  1361. bss->eap_req_id_text_len--;
  1362. }
  1363. } else if (os_strcmp(buf, "wep_key_len_broadcast") == 0) {
  1364. bss->default_wep_key_len = atoi(pos);
  1365. if (bss->default_wep_key_len > 13) {
  1366. printf("Line %d: invalid WEP key len %lu "
  1367. "(= %lu bits)\n", line,
  1368. (unsigned long)
  1369. bss->default_wep_key_len,
  1370. (unsigned long)
  1371. bss->default_wep_key_len * 8);
  1372. errors++;
  1373. }
  1374. } else if (os_strcmp(buf, "wep_key_len_unicast") == 0) {
  1375. bss->individual_wep_key_len = atoi(pos);
  1376. if (bss->individual_wep_key_len < 0 ||
  1377. bss->individual_wep_key_len > 13) {
  1378. printf("Line %d: invalid WEP key len %d "
  1379. "(= %d bits)\n", line,
  1380. bss->individual_wep_key_len,
  1381. bss->individual_wep_key_len * 8);
  1382. errors++;
  1383. }
  1384. } else if (os_strcmp(buf, "wep_rekey_period") == 0) {
  1385. bss->wep_rekeying_period = atoi(pos);
  1386. if (bss->wep_rekeying_period < 0) {
  1387. printf("Line %d: invalid period %d\n",
  1388. line, bss->wep_rekeying_period);
  1389. errors++;
  1390. }
  1391. } else if (os_strcmp(buf, "eap_reauth_period") == 0) {
  1392. bss->eap_reauth_period = atoi(pos);
  1393. if (bss->eap_reauth_period < 0) {
  1394. printf("Line %d: invalid period %d\n",
  1395. line, bss->eap_reauth_period);
  1396. errors++;
  1397. }
  1398. } else if (os_strcmp(buf, "eapol_key_index_workaround") == 0) {
  1399. bss->eapol_key_index_workaround = atoi(pos);
  1400. #ifdef CONFIG_IAPP
  1401. } else if (os_strcmp(buf, "iapp_interface") == 0) {
  1402. bss->ieee802_11f = 1;
  1403. os_strlcpy(bss->iapp_iface, pos,
  1404. sizeof(bss->iapp_iface));
  1405. #endif /* CONFIG_IAPP */
  1406. } else if (os_strcmp(buf, "own_ip_addr") == 0) {
  1407. if (hostapd_parse_ip_addr(pos, &bss->own_ip_addr)) {
  1408. printf("Line %d: invalid IP address '%s'\n",
  1409. line, pos);
  1410. errors++;
  1411. }
  1412. } else if (os_strcmp(buf, "nas_identifier") == 0) {
  1413. bss->nas_identifier = os_strdup(pos);
  1414. } else if (os_strcmp(buf, "auth_server_addr") == 0) {
  1415. if (hostapd_config_read_radius_addr(
  1416. &bss->radius->auth_servers,
  1417. &bss->radius->num_auth_servers, pos, 1812,
  1418. &bss->radius->auth_server)) {
  1419. printf("Line %d: invalid IP address '%s'\n",
  1420. line, pos);
  1421. errors++;
  1422. }
  1423. } else if (bss->radius->auth_server &&
  1424. os_strcmp(buf, "auth_server_port") == 0) {
  1425. bss->radius->auth_server->port = atoi(pos);
  1426. } else if (bss->radius->auth_server &&
  1427. os_strcmp(buf, "auth_server_shared_secret") == 0) {
  1428. int len = os_strlen(pos);
  1429. if (len == 0) {
  1430. /* RFC 2865, Ch. 3 */
  1431. printf("Line %d: empty shared secret is not "
  1432. "allowed.\n", line);
  1433. errors++;
  1434. }
  1435. bss->radius->auth_server->shared_secret =
  1436. (u8 *) os_strdup(pos);
  1437. bss->radius->auth_server->shared_secret_len = len;
  1438. } else if (os_strcmp(buf, "acct_server_addr") == 0) {
  1439. if (hostapd_config_read_radius_addr(
  1440. &bss->radius->acct_servers,
  1441. &bss->radius->num_acct_servers, pos, 1813,
  1442. &bss->radius->acct_server)) {
  1443. printf("Line %d: invalid IP address '%s'\n",
  1444. line, pos);
  1445. errors++;
  1446. }
  1447. } else if (bss->radius->acct_server &&
  1448. os_strcmp(buf, "acct_server_port") == 0) {
  1449. bss->radius->acct_server->port = atoi(pos);
  1450. } else if (bss->radius->acct_server &&
  1451. os_strcmp(buf, "acct_server_shared_secret") == 0) {
  1452. int len = os_strlen(pos);
  1453. if (len == 0) {
  1454. /* RFC 2865, Ch. 3 */
  1455. printf("Line %d: empty shared secret is not "
  1456. "allowed.\n", line);
  1457. errors++;
  1458. }
  1459. bss->radius->acct_server->shared_secret =
  1460. (u8 *) os_strdup(pos);
  1461. bss->radius->acct_server->shared_secret_len = len;
  1462. } else if (os_strcmp(buf, "radius_retry_primary_interval") ==
  1463. 0) {
  1464. bss->radius->retry_primary_interval = atoi(pos);
  1465. } else if (os_strcmp(buf, "radius_acct_interim_interval") == 0)
  1466. {
  1467. bss->radius->acct_interim_interval = atoi(pos);
  1468. } else if (os_strcmp(buf, "auth_algs") == 0) {
  1469. bss->auth_algs = atoi(pos);
  1470. if (bss->auth_algs == 0) {
  1471. printf("Line %d: no authentication algorithms "
  1472. "allowed\n",
  1473. line);
  1474. errors++;
  1475. }
  1476. } else if (os_strcmp(buf, "max_num_sta") == 0) {
  1477. bss->max_num_sta = atoi(pos);
  1478. if (bss->max_num_sta < 0 ||
  1479. bss->max_num_sta > MAX_STA_COUNT) {
  1480. printf("Line %d: Invalid max_num_sta=%d; "
  1481. "allowed range 0..%d\n", line,
  1482. bss->max_num_sta, MAX_STA_COUNT);
  1483. errors++;
  1484. }
  1485. } else if (os_strcmp(buf, "wpa") == 0) {
  1486. bss->wpa = atoi(pos);
  1487. } else if (os_strcmp(buf, "wpa_group_rekey") == 0) {
  1488. bss->wpa_group_rekey = atoi(pos);
  1489. } else if (os_strcmp(buf, "wpa_strict_rekey") == 0) {
  1490. bss->wpa_strict_rekey = atoi(pos);
  1491. } else if (os_strcmp(buf, "wpa_gmk_rekey") == 0) {
  1492. bss->wpa_gmk_rekey = atoi(pos);
  1493. } else if (os_strcmp(buf, "wpa_ptk_rekey") == 0) {
  1494. bss->wpa_ptk_rekey = atoi(pos);
  1495. } else if (os_strcmp(buf, "wpa_passphrase") == 0) {
  1496. int len = os_strlen(pos);
  1497. if (len < 8 || len > 63) {
  1498. printf("Line %d: invalid WPA passphrase length"
  1499. " %d (expected 8..63)\n", line, len);
  1500. errors++;
  1501. } else {
  1502. os_free(bss->ssid.wpa_passphrase);
  1503. bss->ssid.wpa_passphrase = os_strdup(pos);
  1504. }
  1505. } else if (os_strcmp(buf, "wpa_psk") == 0) {
  1506. os_free(bss->ssid.wpa_psk);
  1507. bss->ssid.wpa_psk =
  1508. os_zalloc(sizeof(struct hostapd_wpa_psk));
  1509. if (bss->ssid.wpa_psk == NULL)
  1510. errors++;
  1511. else if (hexstr2bin(pos, bss->ssid.wpa_psk->psk,
  1512. PMK_LEN) ||
  1513. pos[PMK_LEN * 2] != '\0') {
  1514. printf("Line %d: Invalid PSK '%s'.\n", line,
  1515. pos);
  1516. errors++;
  1517. } else {
  1518. bss->ssid.wpa_psk->group = 1;
  1519. }
  1520. } else if (os_strcmp(buf, "wpa_psk_file") == 0) {
  1521. os_free(bss->ssid.wpa_psk_file);
  1522. bss->ssid.wpa_psk_file = os_strdup(pos);
  1523. if (!bss->ssid.wpa_psk_file) {
  1524. printf("Line %d: allocation failed\n", line);
  1525. errors++;
  1526. }
  1527. } else if (os_strcmp(buf, "wpa_key_mgmt") == 0) {
  1528. bss->wpa_key_mgmt =
  1529. hostapd_config_parse_key_mgmt(line, pos);
  1530. if (bss->wpa_key_mgmt == -1)
  1531. errors++;
  1532. } else if (os_strcmp(buf, "wpa_pairwise") == 0) {
  1533. bss->wpa_pairwise =
  1534. hostapd_config_parse_cipher(line, pos);
  1535. if (bss->wpa_pairwise == -1 ||
  1536. bss->wpa_pairwise == 0)
  1537. errors++;
  1538. else if (bss->wpa_pairwise &
  1539. (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 |
  1540. WPA_CIPHER_WEP104)) {
  1541. printf("Line %d: unsupported pairwise "
  1542. "cipher suite '%s'\n",
  1543. bss->wpa_pairwise, pos);
  1544. errors++;
  1545. }
  1546. } else if (os_strcmp(buf, "rsn_pairwise") == 0) {
  1547. bss->rsn_pairwise =
  1548. hostapd_config_parse_cipher(line, pos);
  1549. if (bss->rsn_pairwise == -1 ||
  1550. bss->rsn_pairwise == 0)
  1551. errors++;
  1552. else if (bss->rsn_pairwise &
  1553. (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 |
  1554. WPA_CIPHER_WEP104)) {
  1555. printf("Line %d: unsupported pairwise "
  1556. "cipher suite '%s'\n",
  1557. bss->rsn_pairwise, pos);
  1558. errors++;
  1559. }
  1560. #ifdef CONFIG_RSN_PREAUTH
  1561. } else if (os_strcmp(buf, "rsn_preauth") == 0) {
  1562. bss->rsn_preauth = atoi(pos);
  1563. } else if (os_strcmp(buf, "rsn_preauth_interfaces") == 0) {
  1564. bss->rsn_preauth_interfaces = os_strdup(pos);
  1565. #endif /* CONFIG_RSN_PREAUTH */
  1566. #ifdef CONFIG_PEERKEY
  1567. } else if (os_strcmp(buf, "peerkey") == 0) {
  1568. bss->peerkey = atoi(pos);
  1569. #endif /* CONFIG_PEERKEY */
  1570. #ifdef CONFIG_IEEE80211R
  1571. } else if (os_strcmp(buf, "mobility_domain") == 0) {
  1572. if (os_strlen(pos) != 2 * MOBILITY_DOMAIN_ID_LEN ||
  1573. hexstr2bin(pos, bss->mobility_domain,
  1574. MOBILITY_DOMAIN_ID_LEN) != 0) {
  1575. wpa_printf(MSG_DEBUG, "Line %d: Invalid "
  1576. "mobility_domain '%s'", line, pos);
  1577. errors++;
  1578. continue;
  1579. }
  1580. } else if (os_strcmp(buf, "r1_key_holder") == 0) {
  1581. if (os_strlen(pos) != 2 * FT_R1KH_ID_LEN ||
  1582. hexstr2bin(pos, bss->r1_key_holder,
  1583. FT_R1KH_ID_LEN) != 0) {
  1584. wpa_printf(MSG_DEBUG, "Line %d: Invalid "
  1585. "r1_key_holder '%s'", line, pos);
  1586. errors++;
  1587. continue;
  1588. }
  1589. } else if (os_strcmp(buf, "r0_key_lifetime") == 0) {
  1590. bss->r0_key_lifetime = atoi(pos);
  1591. } else if (os_strcmp(buf, "reassociation_deadline") == 0) {
  1592. bss->reassociation_deadline = atoi(pos);
  1593. } else if (os_strcmp(buf, "r0kh") == 0) {
  1594. if (add_r0kh(bss, pos) < 0) {
  1595. wpa_printf(MSG_DEBUG, "Line %d: Invalid "
  1596. "r0kh '%s'", line, pos);
  1597. errors++;
  1598. continue;
  1599. }
  1600. } else if (os_strcmp(buf, "r1kh") == 0) {
  1601. if (add_r1kh(bss, pos) < 0) {
  1602. wpa_printf(MSG_DEBUG, "Line %d: Invalid "
  1603. "r1kh '%s'", line, pos);
  1604. errors++;
  1605. continue;
  1606. }
  1607. } else if (os_strcmp(buf, "pmk_r1_push") == 0) {
  1608. bss->pmk_r1_push = atoi(pos);
  1609. #endif /* CONFIG_IEEE80211R */
  1610. } else if (os_strcmp(buf, "ctrl_interface") == 0) {
  1611. os_free(bss->ctrl_interface);
  1612. bss->ctrl_interface = os_strdup(pos);
  1613. } else if (os_strcmp(buf, "ctrl_interface_group") == 0) {
  1614. #ifndef CONFIG_NATIVE_WINDOWS
  1615. struct group *grp;
  1616. char *endp;
  1617. const char *group = pos;
  1618. grp = getgrnam(group);
  1619. if (grp) {
  1620. bss->ctrl_interface_gid = grp->gr_gid;
  1621. bss->ctrl_interface_gid_set = 1;
  1622. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  1623. " (from group name '%s')",
  1624. bss->ctrl_interface_gid, group);
  1625. continue;
  1626. }
  1627. /* Group name not found - try to parse this as gid */
  1628. bss->ctrl_interface_gid = strtol(group, &endp, 10);
  1629. if (*group == '\0' || *endp != '\0') {
  1630. wpa_printf(MSG_DEBUG, "Line %d: Invalid group "
  1631. "'%s'", line, group);
  1632. errors++;
  1633. continue;
  1634. }
  1635. bss->ctrl_interface_gid_set = 1;
  1636. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  1637. bss->ctrl_interface_gid);
  1638. #endif /* CONFIG_NATIVE_WINDOWS */
  1639. #ifdef RADIUS_SERVER
  1640. } else if (os_strcmp(buf, "radius_server_clients") == 0) {
  1641. os_free(bss->radius_server_clients);
  1642. bss->radius_server_clients = os_strdup(pos);
  1643. } else if (os_strcmp(buf, "radius_server_auth_port") == 0) {
  1644. bss->radius_server_auth_port = atoi(pos);
  1645. } else if (os_strcmp(buf, "radius_server_ipv6") == 0) {
  1646. bss->radius_server_ipv6 = atoi(pos);
  1647. #endif /* RADIUS_SERVER */
  1648. } else if (os_strcmp(buf, "test_socket") == 0) {
  1649. os_free(bss->test_socket);
  1650. bss->test_socket = os_strdup(pos);
  1651. } else if (os_strcmp(buf, "use_pae_group_addr") == 0) {
  1652. bss->use_pae_group_addr = atoi(pos);
  1653. } else if (os_strcmp(buf, "hw_mode") == 0) {
  1654. if (os_strcmp(pos, "a") == 0)
  1655. conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
  1656. else if (os_strcmp(pos, "b") == 0)
  1657. conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
  1658. else if (os_strcmp(pos, "g") == 0)
  1659. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  1660. else {
  1661. printf("Line %d: unknown hw_mode '%s'\n",
  1662. line, pos);
  1663. errors++;
  1664. }
  1665. } else if (os_strcmp(buf, "channel") == 0) {
  1666. conf->channel = atoi(pos);
  1667. } else if (os_strcmp(buf, "beacon_int") == 0) {
  1668. int val = atoi(pos);
  1669. /* MIB defines range as 1..65535, but very small values
  1670. * cause problems with the current implementation.
  1671. * Since it is unlikely that this small numbers are
  1672. * useful in real life scenarios, do not allow beacon
  1673. * period to be set below 15 TU. */
  1674. if (val < 15 || val > 65535) {
  1675. printf("Line %d: invalid beacon_int %d "
  1676. "(expected 15..65535)\n",
  1677. line, val);
  1678. errors++;
  1679. } else
  1680. conf->beacon_int = val;
  1681. } else if (os_strcmp(buf, "dtim_period") == 0) {
  1682. bss->dtim_period = atoi(pos);
  1683. if (bss->dtim_period < 1 || bss->dtim_period > 255) {
  1684. printf("Line %d: invalid dtim_period %d\n",
  1685. line, bss->dtim_period);
  1686. errors++;
  1687. }
  1688. } else if (os_strcmp(buf, "rts_threshold") == 0) {
  1689. conf->rts_threshold = atoi(pos);
  1690. if (conf->rts_threshold < 0 ||
  1691. conf->rts_threshold > 2347) {
  1692. printf("Line %d: invalid rts_threshold %d\n",
  1693. line, conf->rts_threshold);
  1694. errors++;
  1695. }
  1696. } else if (os_strcmp(buf, "fragm_threshold") == 0) {
  1697. conf->fragm_threshold = atoi(pos);
  1698. if (conf->fragm_threshold < 256 ||
  1699. conf->fragm_threshold > 2346) {
  1700. printf("Line %d: invalid fragm_threshold %d\n",
  1701. line, conf->fragm_threshold);
  1702. errors++;
  1703. }
  1704. } else if (os_strcmp(buf, "send_probe_response") == 0) {
  1705. int val = atoi(pos);
  1706. if (val != 0 && val != 1) {
  1707. printf("Line %d: invalid send_probe_response "
  1708. "%d (expected 0 or 1)\n", line, val);
  1709. } else
  1710. conf->send_probe_response = val;
  1711. } else if (os_strcmp(buf, "supported_rates") == 0) {
  1712. if (hostapd_parse_rates(&conf->supported_rates, pos)) {
  1713. printf("Line %d: invalid rate list\n", line);
  1714. errors++;
  1715. }
  1716. } else if (os_strcmp(buf, "basic_rates") == 0) {
  1717. if (hostapd_parse_rates(&conf->basic_rates, pos)) {
  1718. printf("Line %d: invalid rate list\n", line);
  1719. errors++;
  1720. }
  1721. } else if (os_strcmp(buf, "preamble") == 0) {
  1722. if (atoi(pos))
  1723. conf->preamble = SHORT_PREAMBLE;
  1724. else
  1725. conf->preamble = LONG_PREAMBLE;
  1726. } else if (os_strcmp(buf, "ignore_broadcast_ssid") == 0) {
  1727. bss->ignore_broadcast_ssid = atoi(pos);
  1728. } else if (os_strcmp(buf, "bridge_packets") == 0) {
  1729. conf->bridge_packets = atoi(pos);
  1730. } else if (os_strcmp(buf, "wep_default_key") == 0) {
  1731. bss->ssid.wep.idx = atoi(pos);
  1732. if (bss->ssid.wep.idx > 3) {
  1733. printf("Invalid wep_default_key index %d\n",
  1734. bss->ssid.wep.idx);
  1735. errors++;
  1736. }
  1737. } else if (os_strcmp(buf, "wep_key0") == 0 ||
  1738. os_strcmp(buf, "wep_key1") == 0 ||
  1739. os_strcmp(buf, "wep_key2") == 0 ||
  1740. os_strcmp(buf, "wep_key3") == 0) {
  1741. if (hostapd_config_read_wep(&bss->ssid.wep,
  1742. buf[7] - '0', pos)) {
  1743. printf("Line %d: invalid WEP key '%s'\n",
  1744. line, buf);
  1745. errors++;
  1746. }
  1747. } else if (os_strcmp(buf, "dynamic_vlan") == 0) {
  1748. bss->ssid.dynamic_vlan = atoi(pos);
  1749. } else if (os_strcmp(buf, "vlan_file") == 0) {
  1750. if (hostapd_config_read_vlan_file(bss, pos)) {
  1751. printf("Line %d: failed to read VLAN file "
  1752. "'%s'\n", line, pos);
  1753. errors++;
  1754. }
  1755. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  1756. } else if (os_strcmp(buf, "vlan_tagged_interface") == 0) {
  1757. bss->ssid.vlan_tagged_interface = os_strdup(pos);
  1758. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  1759. } else if (os_strcmp(buf, "passive_scan_interval") == 0) {
  1760. conf->passive_scan_interval = atoi(pos);
  1761. } else if (os_strcmp(buf, "passive_scan_listen") == 0) {
  1762. conf->passive_scan_listen = atoi(pos);
  1763. } else if (os_strcmp(buf, "passive_scan_mode") == 0) {
  1764. conf->passive_scan_mode = atoi(pos);
  1765. } else if (os_strcmp(buf, "ap_table_max_size") == 0) {
  1766. conf->ap_table_max_size = atoi(pos);
  1767. } else if (os_strcmp(buf, "ap_table_expiration_time") == 0) {
  1768. conf->ap_table_expiration_time = atoi(pos);
  1769. } else if (os_strncmp(buf, "tx_queue_", 9) == 0) {
  1770. if (hostapd_config_tx_queue(conf, buf, pos)) {
  1771. printf("Line %d: invalid TX queue item\n",
  1772. line);
  1773. errors++;
  1774. }
  1775. } else if (os_strcmp(buf, "wme_enabled") == 0) {
  1776. bss->wme_enabled = atoi(pos);
  1777. } else if (os_strncmp(buf, "wme_ac_", 7) == 0) {
  1778. if (hostapd_config_wme_ac(conf, buf, pos)) {
  1779. printf("Line %d: invalid wme ac item\n",
  1780. line);
  1781. errors++;
  1782. }
  1783. } else if (os_strcmp(buf, "bss") == 0) {
  1784. if (hostapd_config_bss(conf, pos)) {
  1785. printf("Line %d: invalid bss item\n", line);
  1786. errors++;
  1787. }
  1788. } else if (os_strcmp(buf, "bssid") == 0) {
  1789. if (bss == conf->bss &&
  1790. (!conf->driver || !conf->driver->init_bssid)) {
  1791. printf("Line %d: bssid item not allowed "
  1792. "for the default interface and this "
  1793. "driver\n", line);
  1794. errors++;
  1795. } else if (hwaddr_aton(pos, bss->bssid)) {
  1796. printf("Line %d: invalid bssid item\n", line);
  1797. errors++;
  1798. }
  1799. #ifdef CONFIG_IEEE80211W
  1800. } else if (os_strcmp(buf, "ieee80211w") == 0) {
  1801. bss->ieee80211w = atoi(pos);
  1802. } else if (os_strcmp(buf, "assoc_ping_timeout") == 0) {
  1803. bss->assoc_ping_timeout = atoi(pos);
  1804. if (bss->assoc_ping_timeout == 0) {
  1805. printf("Line %d: invalid assoc_ping_timeout\n",
  1806. line);
  1807. errors++;
  1808. }
  1809. } else if (os_strcmp(buf, "assoc_ping_attempts") == 0) {
  1810. bss->assoc_ping_timeout = atoi(pos);
  1811. if (bss->assoc_ping_timeout == 0) {
  1812. printf("Line %d: invalid assoc_ping_attempts "
  1813. "(valid range: 1..255)\n",
  1814. line);
  1815. errors++;
  1816. }
  1817. #endif /* CONFIG_IEEE80211W */
  1818. #ifdef CONFIG_IEEE80211N
  1819. } else if (os_strcmp(buf, "ieee80211n") == 0) {
  1820. conf->ieee80211n = atoi(pos);
  1821. #endif /* CONFIG_IEEE80211N */
  1822. } else if (os_strcmp(buf, "max_listen_interval") == 0) {
  1823. bss->max_listen_interval = atoi(pos);
  1824. } else if (os_strcmp(buf, "okc") == 0) {
  1825. bss->okc = atoi(pos);
  1826. } else {
  1827. printf("Line %d: unknown configuration item '%s'\n",
  1828. line, buf);
  1829. errors++;
  1830. }
  1831. }
  1832. fclose(f);
  1833. if (bss->individual_wep_key_len == 0) {
  1834. /* individual keys are not use; can use key idx0 for broadcast
  1835. * keys */
  1836. bss->broadcast_key_idx_min = 0;
  1837. }
  1838. /* Select group cipher based on the enabled pairwise cipher suites */
  1839. pairwise = 0;
  1840. if (bss->wpa & 1)
  1841. pairwise |= bss->wpa_pairwise;
  1842. if (bss->wpa & 2) {
  1843. if (bss->rsn_pairwise == 0)
  1844. bss->rsn_pairwise = bss->wpa_pairwise;
  1845. pairwise |= bss->rsn_pairwise;
  1846. }
  1847. if (pairwise & WPA_CIPHER_TKIP)
  1848. bss->wpa_group = WPA_CIPHER_TKIP;
  1849. else
  1850. bss->wpa_group = WPA_CIPHER_CCMP;
  1851. for (i = 0; i < conf->num_bss; i++) {
  1852. bss = &conf->bss[i];
  1853. bss->radius->auth_server = bss->radius->auth_servers;
  1854. bss->radius->acct_server = bss->radius->acct_servers;
  1855. if (bss->wpa && bss->ieee802_1x) {
  1856. bss->ssid.security_policy = SECURITY_WPA;
  1857. } else if (bss->wpa) {
  1858. bss->ssid.security_policy = SECURITY_WPA_PSK;
  1859. } else if (bss->ieee802_1x) {
  1860. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  1861. bss->ssid.wep.default_len = bss->default_wep_key_len;
  1862. } else if (bss->ssid.wep.keys_set)
  1863. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  1864. else
  1865. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  1866. }
  1867. if (hostapd_config_check(conf))
  1868. errors++;
  1869. if (errors) {
  1870. printf("%d errors found in configuration file '%s'\n",
  1871. errors, fname);
  1872. hostapd_config_free(conf);
  1873. conf = NULL;
  1874. }
  1875. return conf;
  1876. }
  1877. int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
  1878. {
  1879. int i;
  1880. if (a->idx != b->idx || a->default_len != b->default_len)
  1881. return 1;
  1882. for (i = 0; i < NUM_WEP_KEYS; i++)
  1883. if (a->len[i] != b->len[i] ||
  1884. os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
  1885. return 1;
  1886. return 0;
  1887. }
  1888. static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
  1889. int num_servers)
  1890. {
  1891. int i;
  1892. for (i = 0; i < num_servers; i++) {
  1893. os_free(servers[i].shared_secret);
  1894. }
  1895. os_free(servers);
  1896. }
  1897. static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
  1898. {
  1899. os_free(user->identity);
  1900. os_free(user->password);
  1901. os_free(user);
  1902. }
  1903. static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
  1904. {
  1905. int i;
  1906. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1907. os_free(keys->key[i]);
  1908. keys->key[i] = NULL;
  1909. }
  1910. }
  1911. static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
  1912. {
  1913. struct hostapd_wpa_psk *psk, *prev;
  1914. struct hostapd_eap_user *user, *prev_user;
  1915. if (conf == NULL)
  1916. return;
  1917. psk = conf->ssid.wpa_psk;
  1918. while (psk) {
  1919. prev = psk;
  1920. psk = psk->next;
  1921. os_free(prev);
  1922. }
  1923. os_free(conf->ssid.wpa_passphrase);
  1924. os_free(conf->ssid.wpa_psk_file);
  1925. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  1926. os_free(conf->ssid.vlan_tagged_interface);
  1927. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  1928. user = conf->eap_user;
  1929. while (user) {
  1930. prev_user = user;
  1931. user = user->next;
  1932. hostapd_config_free_eap_user(prev_user);
  1933. }
  1934. os_free(conf->dump_log_name);
  1935. os_free(conf->eap_req_id_text);
  1936. os_free(conf->accept_mac);
  1937. os_free(conf->deny_mac);
  1938. os_free(conf->nas_identifier);
  1939. hostapd_config_free_radius(conf->radius->auth_servers,
  1940. conf->radius->num_auth_servers);
  1941. hostapd_config_free_radius(conf->radius->acct_servers,
  1942. conf->radius->num_acct_servers);
  1943. os_free(conf->rsn_preauth_interfaces);
  1944. os_free(conf->ctrl_interface);
  1945. os_free(conf->ca_cert);
  1946. os_free(conf->server_cert);
  1947. os_free(conf->private_key);
  1948. os_free(conf->private_key_passwd);
  1949. os_free(conf->dh_file);
  1950. os_free(conf->pac_opaque_encr_key);
  1951. os_free(conf->eap_fast_a_id);
  1952. os_free(conf->eap_fast_a_id_info);
  1953. os_free(conf->eap_sim_db);
  1954. os_free(conf->radius_server_clients);
  1955. os_free(conf->test_socket);
  1956. os_free(conf->radius);
  1957. hostapd_config_free_vlan(conf);
  1958. if (conf->ssid.dyn_vlan_keys) {
  1959. struct hostapd_ssid *ssid = &conf->ssid;
  1960. size_t i;
  1961. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  1962. if (ssid->dyn_vlan_keys[i] == NULL)
  1963. continue;
  1964. hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
  1965. os_free(ssid->dyn_vlan_keys[i]);
  1966. }
  1967. os_free(ssid->dyn_vlan_keys);
  1968. ssid->dyn_vlan_keys = NULL;
  1969. }
  1970. #ifdef CONFIG_IEEE80211R
  1971. {
  1972. struct ft_remote_r0kh *r0kh, *r0kh_prev;
  1973. struct ft_remote_r1kh *r1kh, *r1kh_prev;
  1974. r0kh = conf->r0kh_list;
  1975. conf->r0kh_list = NULL;
  1976. while (r0kh) {
  1977. r0kh_prev = r0kh;
  1978. r0kh = r0kh->next;
  1979. os_free(r0kh_prev);
  1980. }
  1981. r1kh = conf->r1kh_list;
  1982. conf->r1kh_list = NULL;
  1983. while (r1kh) {
  1984. r1kh_prev = r1kh;
  1985. r1kh = r1kh->next;
  1986. os_free(r1kh_prev);
  1987. }
  1988. }
  1989. #endif /* CONFIG_IEEE80211R */
  1990. }
  1991. void hostapd_config_free(struct hostapd_config *conf)
  1992. {
  1993. size_t i;
  1994. if (conf == NULL)
  1995. return;
  1996. for (i = 0; i < conf->num_bss; i++)
  1997. hostapd_config_free_bss(&conf->bss[i]);
  1998. os_free(conf->bss);
  1999. os_free(conf);
  2000. }
  2001. /* Perform a binary search for given MAC address from a pre-sorted list.
  2002. * Returns 1 if address is in the list or 0 if not. */
  2003. int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
  2004. const u8 *addr, int *vlan_id)
  2005. {
  2006. int start, end, middle, res;
  2007. start = 0;
  2008. end = num_entries - 1;
  2009. while (start <= end) {
  2010. middle = (start + end) / 2;
  2011. res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
  2012. if (res == 0) {
  2013. if (vlan_id)
  2014. *vlan_id = list[middle].vlan_id;
  2015. return 1;
  2016. }
  2017. if (res < 0)
  2018. start = middle + 1;
  2019. else
  2020. end = middle - 1;
  2021. }
  2022. return 0;
  2023. }
  2024. int hostapd_rate_found(int *list, int rate)
  2025. {
  2026. int i;
  2027. if (list == NULL)
  2028. return 0;
  2029. for (i = 0; list[i] >= 0; i++)
  2030. if (list[i] == rate)
  2031. return 1;
  2032. return 0;
  2033. }
  2034. const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
  2035. {
  2036. struct hostapd_vlan *v = vlan;
  2037. while (v) {
  2038. if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
  2039. return v->ifname;
  2040. v = v->next;
  2041. }
  2042. return NULL;
  2043. }
  2044. const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
  2045. const u8 *addr, const u8 *prev_psk)
  2046. {
  2047. struct hostapd_wpa_psk *psk;
  2048. int next_ok = prev_psk == NULL;
  2049. for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
  2050. if (next_ok &&
  2051. (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
  2052. return psk->psk;
  2053. if (psk->psk == prev_psk)
  2054. next_ok = 1;
  2055. }
  2056. return NULL;
  2057. }
  2058. const struct hostapd_eap_user *
  2059. hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
  2060. size_t identity_len, int phase2)
  2061. {
  2062. struct hostapd_eap_user *user = conf->eap_user;
  2063. while (user) {
  2064. if (!phase2 && user->identity == NULL) {
  2065. /* Wildcard match */
  2066. break;
  2067. }
  2068. if (user->phase2 == !!phase2 && user->wildcard_prefix &&
  2069. identity_len >= user->identity_len &&
  2070. os_memcmp(user->identity, identity, user->identity_len) ==
  2071. 0) {
  2072. /* Wildcard prefix match */
  2073. break;
  2074. }
  2075. if (user->phase2 == !!phase2 &&
  2076. user->identity_len == identity_len &&
  2077. os_memcmp(user->identity, identity, identity_len) == 0)
  2078. break;
  2079. user = user->next;
  2080. }
  2081. return user;
  2082. }