config.c 57 KB

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