config.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339
  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, "ieee8021x") == 0) {
  1244. bss->ieee802_1x = atoi(pos);
  1245. } else if (os_strcmp(buf, "eapol_version") == 0) {
  1246. bss->eapol_version = atoi(pos);
  1247. if (bss->eapol_version < 1 ||
  1248. bss->eapol_version > 2) {
  1249. printf("Line %d: invalid EAPOL "
  1250. "version (%d): '%s'.\n",
  1251. line, bss->eapol_version, pos);
  1252. errors++;
  1253. } else
  1254. wpa_printf(MSG_DEBUG, "eapol_version=%d",
  1255. bss->eapol_version);
  1256. #ifdef EAP_SERVER
  1257. } else if (os_strcmp(buf, "eap_authenticator") == 0) {
  1258. bss->eap_server = atoi(pos);
  1259. printf("Line %d: obsolete eap_authenticator used; "
  1260. "this has been renamed to eap_server\n", line);
  1261. } else if (os_strcmp(buf, "eap_server") == 0) {
  1262. bss->eap_server = atoi(pos);
  1263. } else if (os_strcmp(buf, "eap_user_file") == 0) {
  1264. if (hostapd_config_read_eap_user(pos, bss))
  1265. errors++;
  1266. } else if (os_strcmp(buf, "ca_cert") == 0) {
  1267. os_free(bss->ca_cert);
  1268. bss->ca_cert = os_strdup(pos);
  1269. } else if (os_strcmp(buf, "server_cert") == 0) {
  1270. os_free(bss->server_cert);
  1271. bss->server_cert = os_strdup(pos);
  1272. } else if (os_strcmp(buf, "private_key") == 0) {
  1273. os_free(bss->private_key);
  1274. bss->private_key = os_strdup(pos);
  1275. } else if (os_strcmp(buf, "private_key_passwd") == 0) {
  1276. os_free(bss->private_key_passwd);
  1277. bss->private_key_passwd = os_strdup(pos);
  1278. } else if (os_strcmp(buf, "check_crl") == 0) {
  1279. bss->check_crl = atoi(pos);
  1280. } else if (os_strcmp(buf, "dh_file") == 0) {
  1281. os_free(bss->dh_file);
  1282. bss->dh_file = os_strdup(pos);
  1283. #ifdef EAP_FAST
  1284. } else if (os_strcmp(buf, "pac_opaque_encr_key") == 0) {
  1285. os_free(bss->pac_opaque_encr_key);
  1286. bss->pac_opaque_encr_key = os_malloc(16);
  1287. if (bss->pac_opaque_encr_key == NULL) {
  1288. printf("Line %d: No memory for "
  1289. "pac_opque_encr_key\n", line);
  1290. errors++;
  1291. } else if (hexstr2bin(pos, bss->pac_opaque_encr_key,
  1292. 16)) {
  1293. printf("Line %d: Invalid pac_opque_encr_key\n",
  1294. line);
  1295. errors++;
  1296. }
  1297. } else if (os_strcmp(buf, "eap_fast_a_id") == 0) {
  1298. size_t idlen = os_strlen(pos);
  1299. if (idlen & 1) {
  1300. printf("Line %d: Invalid eap_fast_a_id\n",
  1301. line);
  1302. errors++;
  1303. } else {
  1304. os_free(bss->eap_fast_a_id);
  1305. bss->eap_fast_a_id = os_malloc(idlen / 2);
  1306. if (bss->eap_fast_a_id == NULL ||
  1307. hexstr2bin(pos, bss->eap_fast_a_id,
  1308. idlen / 2)) {
  1309. printf("Line %d: Failed to parse "
  1310. "eap_fast_a_id\n", line);
  1311. errors++;
  1312. } else
  1313. bss->eap_fast_a_id_len = idlen / 2;
  1314. }
  1315. } else if (os_strcmp(buf, "eap_fast_a_id_info") == 0) {
  1316. os_free(bss->eap_fast_a_id_info);
  1317. bss->eap_fast_a_id_info = os_strdup(pos);
  1318. } else if (os_strcmp(buf, "eap_fast_prov") == 0) {
  1319. bss->eap_fast_prov = atoi(pos);
  1320. } else if (os_strcmp(buf, "pac_key_lifetime") == 0) {
  1321. bss->pac_key_lifetime = atoi(pos);
  1322. } else if (os_strcmp(buf, "pac_key_refresh_time") == 0) {
  1323. bss->pac_key_refresh_time = atoi(pos);
  1324. #endif /* EAP_FAST */
  1325. #ifdef EAP_SIM
  1326. } else if (os_strcmp(buf, "eap_sim_db") == 0) {
  1327. os_free(bss->eap_sim_db);
  1328. bss->eap_sim_db = os_strdup(pos);
  1329. } else if (os_strcmp(buf, "eap_sim_aka_result_ind") == 0) {
  1330. bss->eap_sim_aka_result_ind = atoi(pos);
  1331. #endif /* EAP_SIM */
  1332. #ifdef EAP_TNC
  1333. } else if (os_strcmp(buf, "tnc") == 0) {
  1334. bss->tnc = atoi(pos);
  1335. #endif /* EAP_TNC */
  1336. #endif /* EAP_SERVER */
  1337. } else if (os_strcmp(buf, "eap_message") == 0) {
  1338. char *term;
  1339. bss->eap_req_id_text = os_strdup(pos);
  1340. if (bss->eap_req_id_text == NULL) {
  1341. printf("Line %d: Failed to allocate memory "
  1342. "for eap_req_id_text\n", line);
  1343. errors++;
  1344. continue;
  1345. }
  1346. bss->eap_req_id_text_len =
  1347. os_strlen(bss->eap_req_id_text);
  1348. term = os_strstr(bss->eap_req_id_text, "\\0");
  1349. if (term) {
  1350. *term++ = '\0';
  1351. os_memmove(term, term + 1,
  1352. bss->eap_req_id_text_len -
  1353. (term - bss->eap_req_id_text) - 1);
  1354. bss->eap_req_id_text_len--;
  1355. }
  1356. } else if (os_strcmp(buf, "wep_key_len_broadcast") == 0) {
  1357. bss->default_wep_key_len = atoi(pos);
  1358. if (bss->default_wep_key_len > 13) {
  1359. printf("Line %d: invalid WEP key len %lu "
  1360. "(= %lu bits)\n", line,
  1361. (unsigned long)
  1362. bss->default_wep_key_len,
  1363. (unsigned long)
  1364. bss->default_wep_key_len * 8);
  1365. errors++;
  1366. }
  1367. } else if (os_strcmp(buf, "wep_key_len_unicast") == 0) {
  1368. bss->individual_wep_key_len = atoi(pos);
  1369. if (bss->individual_wep_key_len < 0 ||
  1370. bss->individual_wep_key_len > 13) {
  1371. printf("Line %d: invalid WEP key len %d "
  1372. "(= %d bits)\n", line,
  1373. bss->individual_wep_key_len,
  1374. bss->individual_wep_key_len * 8);
  1375. errors++;
  1376. }
  1377. } else if (os_strcmp(buf, "wep_rekey_period") == 0) {
  1378. bss->wep_rekeying_period = atoi(pos);
  1379. if (bss->wep_rekeying_period < 0) {
  1380. printf("Line %d: invalid period %d\n",
  1381. line, bss->wep_rekeying_period);
  1382. errors++;
  1383. }
  1384. } else if (os_strcmp(buf, "eap_reauth_period") == 0) {
  1385. bss->eap_reauth_period = atoi(pos);
  1386. if (bss->eap_reauth_period < 0) {
  1387. printf("Line %d: invalid period %d\n",
  1388. line, bss->eap_reauth_period);
  1389. errors++;
  1390. }
  1391. } else if (os_strcmp(buf, "eapol_key_index_workaround") == 0) {
  1392. bss->eapol_key_index_workaround = atoi(pos);
  1393. #ifdef CONFIG_IAPP
  1394. } else if (os_strcmp(buf, "iapp_interface") == 0) {
  1395. bss->ieee802_11f = 1;
  1396. os_strlcpy(bss->iapp_iface, pos,
  1397. sizeof(bss->iapp_iface));
  1398. #endif /* CONFIG_IAPP */
  1399. } else if (os_strcmp(buf, "own_ip_addr") == 0) {
  1400. if (hostapd_parse_ip_addr(pos, &bss->own_ip_addr)) {
  1401. printf("Line %d: invalid IP address '%s'\n",
  1402. line, pos);
  1403. errors++;
  1404. }
  1405. } else if (os_strcmp(buf, "nas_identifier") == 0) {
  1406. bss->nas_identifier = os_strdup(pos);
  1407. } else if (os_strcmp(buf, "auth_server_addr") == 0) {
  1408. if (hostapd_config_read_radius_addr(
  1409. &bss->radius->auth_servers,
  1410. &bss->radius->num_auth_servers, pos, 1812,
  1411. &bss->radius->auth_server)) {
  1412. printf("Line %d: invalid IP address '%s'\n",
  1413. line, pos);
  1414. errors++;
  1415. }
  1416. } else if (bss->radius->auth_server &&
  1417. os_strcmp(buf, "auth_server_port") == 0) {
  1418. bss->radius->auth_server->port = atoi(pos);
  1419. } else if (bss->radius->auth_server &&
  1420. os_strcmp(buf, "auth_server_shared_secret") == 0) {
  1421. int len = os_strlen(pos);
  1422. if (len == 0) {
  1423. /* RFC 2865, Ch. 3 */
  1424. printf("Line %d: empty shared secret is not "
  1425. "allowed.\n", line);
  1426. errors++;
  1427. }
  1428. bss->radius->auth_server->shared_secret =
  1429. (u8 *) os_strdup(pos);
  1430. bss->radius->auth_server->shared_secret_len = len;
  1431. } else if (os_strcmp(buf, "acct_server_addr") == 0) {
  1432. if (hostapd_config_read_radius_addr(
  1433. &bss->radius->acct_servers,
  1434. &bss->radius->num_acct_servers, pos, 1813,
  1435. &bss->radius->acct_server)) {
  1436. printf("Line %d: invalid IP address '%s'\n",
  1437. line, pos);
  1438. errors++;
  1439. }
  1440. } else if (bss->radius->acct_server &&
  1441. os_strcmp(buf, "acct_server_port") == 0) {
  1442. bss->radius->acct_server->port = atoi(pos);
  1443. } else if (bss->radius->acct_server &&
  1444. os_strcmp(buf, "acct_server_shared_secret") == 0) {
  1445. int len = os_strlen(pos);
  1446. if (len == 0) {
  1447. /* RFC 2865, Ch. 3 */
  1448. printf("Line %d: empty shared secret is not "
  1449. "allowed.\n", line);
  1450. errors++;
  1451. }
  1452. bss->radius->acct_server->shared_secret =
  1453. (u8 *) os_strdup(pos);
  1454. bss->radius->acct_server->shared_secret_len = len;
  1455. } else if (os_strcmp(buf, "radius_retry_primary_interval") ==
  1456. 0) {
  1457. bss->radius->retry_primary_interval = atoi(pos);
  1458. } else if (os_strcmp(buf, "radius_acct_interim_interval") == 0)
  1459. {
  1460. bss->radius->acct_interim_interval = atoi(pos);
  1461. } else if (os_strcmp(buf, "auth_algs") == 0) {
  1462. bss->auth_algs = atoi(pos);
  1463. if (bss->auth_algs == 0) {
  1464. printf("Line %d: no authentication algorithms "
  1465. "allowed\n",
  1466. line);
  1467. errors++;
  1468. }
  1469. } else if (os_strcmp(buf, "max_num_sta") == 0) {
  1470. bss->max_num_sta = atoi(pos);
  1471. if (bss->max_num_sta < 0 ||
  1472. bss->max_num_sta > MAX_STA_COUNT) {
  1473. printf("Line %d: Invalid max_num_sta=%d; "
  1474. "allowed range 0..%d\n", line,
  1475. bss->max_num_sta, MAX_STA_COUNT);
  1476. errors++;
  1477. }
  1478. } else if (os_strcmp(buf, "wpa") == 0) {
  1479. bss->wpa = atoi(pos);
  1480. } else if (os_strcmp(buf, "wpa_group_rekey") == 0) {
  1481. bss->wpa_group_rekey = atoi(pos);
  1482. } else if (os_strcmp(buf, "wpa_strict_rekey") == 0) {
  1483. bss->wpa_strict_rekey = atoi(pos);
  1484. } else if (os_strcmp(buf, "wpa_gmk_rekey") == 0) {
  1485. bss->wpa_gmk_rekey = atoi(pos);
  1486. } else if (os_strcmp(buf, "wpa_ptk_rekey") == 0) {
  1487. bss->wpa_ptk_rekey = atoi(pos);
  1488. } else if (os_strcmp(buf, "wpa_passphrase") == 0) {
  1489. int len = os_strlen(pos);
  1490. if (len < 8 || len > 63) {
  1491. printf("Line %d: invalid WPA passphrase length"
  1492. " %d (expected 8..63)\n", line, len);
  1493. errors++;
  1494. } else {
  1495. os_free(bss->ssid.wpa_passphrase);
  1496. bss->ssid.wpa_passphrase = os_strdup(pos);
  1497. }
  1498. } else if (os_strcmp(buf, "wpa_psk") == 0) {
  1499. os_free(bss->ssid.wpa_psk);
  1500. bss->ssid.wpa_psk =
  1501. os_zalloc(sizeof(struct hostapd_wpa_psk));
  1502. if (bss->ssid.wpa_psk == NULL)
  1503. errors++;
  1504. else if (hexstr2bin(pos, bss->ssid.wpa_psk->psk,
  1505. PMK_LEN) ||
  1506. pos[PMK_LEN * 2] != '\0') {
  1507. printf("Line %d: Invalid PSK '%s'.\n", line,
  1508. pos);
  1509. errors++;
  1510. } else {
  1511. bss->ssid.wpa_psk->group = 1;
  1512. }
  1513. } else if (os_strcmp(buf, "wpa_psk_file") == 0) {
  1514. os_free(bss->ssid.wpa_psk_file);
  1515. bss->ssid.wpa_psk_file = os_strdup(pos);
  1516. if (!bss->ssid.wpa_psk_file) {
  1517. printf("Line %d: allocation failed\n", line);
  1518. errors++;
  1519. }
  1520. } else if (os_strcmp(buf, "wpa_key_mgmt") == 0) {
  1521. bss->wpa_key_mgmt =
  1522. hostapd_config_parse_key_mgmt(line, pos);
  1523. if (bss->wpa_key_mgmt == -1)
  1524. errors++;
  1525. } else if (os_strcmp(buf, "wpa_pairwise") == 0) {
  1526. bss->wpa_pairwise =
  1527. hostapd_config_parse_cipher(line, pos);
  1528. if (bss->wpa_pairwise == -1 ||
  1529. bss->wpa_pairwise == 0)
  1530. errors++;
  1531. else if (bss->wpa_pairwise &
  1532. (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 |
  1533. WPA_CIPHER_WEP104)) {
  1534. printf("Line %d: unsupported pairwise "
  1535. "cipher suite '%s'\n",
  1536. bss->wpa_pairwise, pos);
  1537. errors++;
  1538. }
  1539. } else if (os_strcmp(buf, "rsn_pairwise") == 0) {
  1540. bss->rsn_pairwise =
  1541. hostapd_config_parse_cipher(line, pos);
  1542. if (bss->rsn_pairwise == -1 ||
  1543. bss->rsn_pairwise == 0)
  1544. errors++;
  1545. else if (bss->rsn_pairwise &
  1546. (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 |
  1547. WPA_CIPHER_WEP104)) {
  1548. printf("Line %d: unsupported pairwise "
  1549. "cipher suite '%s'\n",
  1550. bss->rsn_pairwise, pos);
  1551. errors++;
  1552. }
  1553. #ifdef CONFIG_RSN_PREAUTH
  1554. } else if (os_strcmp(buf, "rsn_preauth") == 0) {
  1555. bss->rsn_preauth = atoi(pos);
  1556. } else if (os_strcmp(buf, "rsn_preauth_interfaces") == 0) {
  1557. bss->rsn_preauth_interfaces = os_strdup(pos);
  1558. #endif /* CONFIG_RSN_PREAUTH */
  1559. #ifdef CONFIG_PEERKEY
  1560. } else if (os_strcmp(buf, "peerkey") == 0) {
  1561. bss->peerkey = atoi(pos);
  1562. #endif /* CONFIG_PEERKEY */
  1563. #ifdef CONFIG_IEEE80211R
  1564. } else if (os_strcmp(buf, "mobility_domain") == 0) {
  1565. if (os_strlen(pos) != 2 * MOBILITY_DOMAIN_ID_LEN ||
  1566. hexstr2bin(pos, bss->mobility_domain,
  1567. MOBILITY_DOMAIN_ID_LEN) != 0) {
  1568. wpa_printf(MSG_DEBUG, "Line %d: Invalid "
  1569. "mobility_domain '%s'", line, pos);
  1570. errors++;
  1571. continue;
  1572. }
  1573. } else if (os_strcmp(buf, "r1_key_holder") == 0) {
  1574. if (os_strlen(pos) != 2 * FT_R1KH_ID_LEN ||
  1575. hexstr2bin(pos, bss->r1_key_holder,
  1576. FT_R1KH_ID_LEN) != 0) {
  1577. wpa_printf(MSG_DEBUG, "Line %d: Invalid "
  1578. "r1_key_holder '%s'", line, pos);
  1579. errors++;
  1580. continue;
  1581. }
  1582. } else if (os_strcmp(buf, "r0_key_lifetime") == 0) {
  1583. bss->r0_key_lifetime = atoi(pos);
  1584. } else if (os_strcmp(buf, "reassociation_deadline") == 0) {
  1585. bss->reassociation_deadline = atoi(pos);
  1586. } else if (os_strcmp(buf, "r0kh") == 0) {
  1587. if (add_r0kh(bss, pos) < 0) {
  1588. wpa_printf(MSG_DEBUG, "Line %d: Invalid "
  1589. "r0kh '%s'", line, pos);
  1590. errors++;
  1591. continue;
  1592. }
  1593. } else if (os_strcmp(buf, "r1kh") == 0) {
  1594. if (add_r1kh(bss, pos) < 0) {
  1595. wpa_printf(MSG_DEBUG, "Line %d: Invalid "
  1596. "r1kh '%s'", line, pos);
  1597. errors++;
  1598. continue;
  1599. }
  1600. } else if (os_strcmp(buf, "pmk_r1_push") == 0) {
  1601. bss->pmk_r1_push = atoi(pos);
  1602. #endif /* CONFIG_IEEE80211R */
  1603. } else if (os_strcmp(buf, "ctrl_interface") == 0) {
  1604. os_free(bss->ctrl_interface);
  1605. bss->ctrl_interface = os_strdup(pos);
  1606. } else if (os_strcmp(buf, "ctrl_interface_group") == 0) {
  1607. #ifndef CONFIG_NATIVE_WINDOWS
  1608. struct group *grp;
  1609. char *endp;
  1610. const char *group = pos;
  1611. grp = getgrnam(group);
  1612. if (grp) {
  1613. bss->ctrl_interface_gid = grp->gr_gid;
  1614. bss->ctrl_interface_gid_set = 1;
  1615. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
  1616. " (from group name '%s')",
  1617. bss->ctrl_interface_gid, group);
  1618. continue;
  1619. }
  1620. /* Group name not found - try to parse this as gid */
  1621. bss->ctrl_interface_gid = strtol(group, &endp, 10);
  1622. if (*group == '\0' || *endp != '\0') {
  1623. wpa_printf(MSG_DEBUG, "Line %d: Invalid group "
  1624. "'%s'", line, group);
  1625. errors++;
  1626. continue;
  1627. }
  1628. bss->ctrl_interface_gid_set = 1;
  1629. wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
  1630. bss->ctrl_interface_gid);
  1631. #endif /* CONFIG_NATIVE_WINDOWS */
  1632. #ifdef RADIUS_SERVER
  1633. } else if (os_strcmp(buf, "radius_server_clients") == 0) {
  1634. os_free(bss->radius_server_clients);
  1635. bss->radius_server_clients = os_strdup(pos);
  1636. } else if (os_strcmp(buf, "radius_server_auth_port") == 0) {
  1637. bss->radius_server_auth_port = atoi(pos);
  1638. } else if (os_strcmp(buf, "radius_server_ipv6") == 0) {
  1639. bss->radius_server_ipv6 = atoi(pos);
  1640. #endif /* RADIUS_SERVER */
  1641. } else if (os_strcmp(buf, "test_socket") == 0) {
  1642. os_free(bss->test_socket);
  1643. bss->test_socket = os_strdup(pos);
  1644. } else if (os_strcmp(buf, "use_pae_group_addr") == 0) {
  1645. bss->use_pae_group_addr = atoi(pos);
  1646. } else if (os_strcmp(buf, "hw_mode") == 0) {
  1647. if (os_strcmp(pos, "a") == 0)
  1648. conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
  1649. else if (os_strcmp(pos, "b") == 0)
  1650. conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
  1651. else if (os_strcmp(pos, "g") == 0)
  1652. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  1653. else {
  1654. printf("Line %d: unknown hw_mode '%s'\n",
  1655. line, pos);
  1656. errors++;
  1657. }
  1658. } else if (os_strcmp(buf, "channel") == 0) {
  1659. conf->channel = atoi(pos);
  1660. } else if (os_strcmp(buf, "beacon_int") == 0) {
  1661. int val = atoi(pos);
  1662. /* MIB defines range as 1..65535, but very small values
  1663. * cause problems with the current implementation.
  1664. * Since it is unlikely that this small numbers are
  1665. * useful in real life scenarios, do not allow beacon
  1666. * period to be set below 15 TU. */
  1667. if (val < 15 || val > 65535) {
  1668. printf("Line %d: invalid beacon_int %d "
  1669. "(expected 15..65535)\n",
  1670. line, val);
  1671. errors++;
  1672. } else
  1673. conf->beacon_int = val;
  1674. } else if (os_strcmp(buf, "dtim_period") == 0) {
  1675. bss->dtim_period = atoi(pos);
  1676. if (bss->dtim_period < 1 || bss->dtim_period > 255) {
  1677. printf("Line %d: invalid dtim_period %d\n",
  1678. line, bss->dtim_period);
  1679. errors++;
  1680. }
  1681. } else if (os_strcmp(buf, "rts_threshold") == 0) {
  1682. conf->rts_threshold = atoi(pos);
  1683. if (conf->rts_threshold < 0 ||
  1684. conf->rts_threshold > 2347) {
  1685. printf("Line %d: invalid rts_threshold %d\n",
  1686. line, conf->rts_threshold);
  1687. errors++;
  1688. }
  1689. } else if (os_strcmp(buf, "fragm_threshold") == 0) {
  1690. conf->fragm_threshold = atoi(pos);
  1691. if (conf->fragm_threshold < 256 ||
  1692. conf->fragm_threshold > 2346) {
  1693. printf("Line %d: invalid fragm_threshold %d\n",
  1694. line, conf->fragm_threshold);
  1695. errors++;
  1696. }
  1697. } else if (os_strcmp(buf, "send_probe_response") == 0) {
  1698. int val = atoi(pos);
  1699. if (val != 0 && val != 1) {
  1700. printf("Line %d: invalid send_probe_response "
  1701. "%d (expected 0 or 1)\n", line, val);
  1702. } else
  1703. conf->send_probe_response = val;
  1704. } else if (os_strcmp(buf, "supported_rates") == 0) {
  1705. if (hostapd_parse_rates(&conf->supported_rates, pos)) {
  1706. printf("Line %d: invalid rate list\n", line);
  1707. errors++;
  1708. }
  1709. } else if (os_strcmp(buf, "basic_rates") == 0) {
  1710. if (hostapd_parse_rates(&conf->basic_rates, pos)) {
  1711. printf("Line %d: invalid rate list\n", line);
  1712. errors++;
  1713. }
  1714. } else if (os_strcmp(buf, "preamble") == 0) {
  1715. if (atoi(pos))
  1716. conf->preamble = SHORT_PREAMBLE;
  1717. else
  1718. conf->preamble = LONG_PREAMBLE;
  1719. } else if (os_strcmp(buf, "ignore_broadcast_ssid") == 0) {
  1720. bss->ignore_broadcast_ssid = atoi(pos);
  1721. } else if (os_strcmp(buf, "bridge_packets") == 0) {
  1722. conf->bridge_packets = atoi(pos);
  1723. } else if (os_strcmp(buf, "wep_default_key") == 0) {
  1724. bss->ssid.wep.idx = atoi(pos);
  1725. if (bss->ssid.wep.idx > 3) {
  1726. printf("Invalid wep_default_key index %d\n",
  1727. bss->ssid.wep.idx);
  1728. errors++;
  1729. }
  1730. } else if (os_strcmp(buf, "wep_key0") == 0 ||
  1731. os_strcmp(buf, "wep_key1") == 0 ||
  1732. os_strcmp(buf, "wep_key2") == 0 ||
  1733. os_strcmp(buf, "wep_key3") == 0) {
  1734. if (hostapd_config_read_wep(&bss->ssid.wep,
  1735. buf[7] - '0', pos)) {
  1736. printf("Line %d: invalid WEP key '%s'\n",
  1737. line, buf);
  1738. errors++;
  1739. }
  1740. } else if (os_strcmp(buf, "dynamic_vlan") == 0) {
  1741. bss->ssid.dynamic_vlan = atoi(pos);
  1742. } else if (os_strcmp(buf, "vlan_file") == 0) {
  1743. if (hostapd_config_read_vlan_file(bss, pos)) {
  1744. printf("Line %d: failed to read VLAN file "
  1745. "'%s'\n", line, pos);
  1746. errors++;
  1747. }
  1748. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  1749. } else if (os_strcmp(buf, "vlan_tagged_interface") == 0) {
  1750. bss->ssid.vlan_tagged_interface = os_strdup(pos);
  1751. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  1752. } else if (os_strcmp(buf, "passive_scan_interval") == 0) {
  1753. conf->passive_scan_interval = atoi(pos);
  1754. } else if (os_strcmp(buf, "passive_scan_listen") == 0) {
  1755. conf->passive_scan_listen = atoi(pos);
  1756. } else if (os_strcmp(buf, "passive_scan_mode") == 0) {
  1757. conf->passive_scan_mode = atoi(pos);
  1758. } else if (os_strcmp(buf, "ap_table_max_size") == 0) {
  1759. conf->ap_table_max_size = atoi(pos);
  1760. } else if (os_strcmp(buf, "ap_table_expiration_time") == 0) {
  1761. conf->ap_table_expiration_time = atoi(pos);
  1762. } else if (os_strncmp(buf, "tx_queue_", 9) == 0) {
  1763. if (hostapd_config_tx_queue(conf, buf, pos)) {
  1764. printf("Line %d: invalid TX queue item\n",
  1765. line);
  1766. errors++;
  1767. }
  1768. } else if (os_strcmp(buf, "wme_enabled") == 0) {
  1769. bss->wme_enabled = atoi(pos);
  1770. } else if (os_strncmp(buf, "wme_ac_", 7) == 0) {
  1771. if (hostapd_config_wme_ac(conf, buf, pos)) {
  1772. printf("Line %d: invalid wme ac item\n",
  1773. line);
  1774. errors++;
  1775. }
  1776. } else if (os_strcmp(buf, "bss") == 0) {
  1777. if (hostapd_config_bss(conf, pos)) {
  1778. printf("Line %d: invalid bss item\n", line);
  1779. errors++;
  1780. }
  1781. } else if (os_strcmp(buf, "bssid") == 0) {
  1782. if (bss == conf->bss &&
  1783. (!conf->driver || !conf->driver->init_bssid)) {
  1784. printf("Line %d: bssid item not allowed "
  1785. "for the default interface and this "
  1786. "driver\n", line);
  1787. errors++;
  1788. } else if (hwaddr_aton(pos, bss->bssid)) {
  1789. printf("Line %d: invalid bssid item\n", line);
  1790. errors++;
  1791. }
  1792. #ifdef CONFIG_IEEE80211W
  1793. } else if (os_strcmp(buf, "ieee80211w") == 0) {
  1794. bss->ieee80211w = atoi(pos);
  1795. } else if (os_strcmp(buf, "assoc_ping_timeout") == 0) {
  1796. bss->assoc_ping_timeout = atoi(pos);
  1797. if (bss->assoc_ping_timeout == 0) {
  1798. printf("Line %d: invalid assoc_ping_timeout\n",
  1799. line);
  1800. errors++;
  1801. }
  1802. } else if (os_strcmp(buf, "assoc_ping_attempts") == 0) {
  1803. bss->assoc_ping_timeout = atoi(pos);
  1804. if (bss->assoc_ping_timeout == 0) {
  1805. printf("Line %d: invalid assoc_ping_attempts "
  1806. "(valid range: 1..255)\n",
  1807. line);
  1808. errors++;
  1809. }
  1810. #endif /* CONFIG_IEEE80211W */
  1811. #ifdef CONFIG_IEEE80211N
  1812. } else if (os_strcmp(buf, "ieee80211n") == 0) {
  1813. conf->ieee80211n = atoi(pos);
  1814. #endif /* CONFIG_IEEE80211N */
  1815. } else if (os_strcmp(buf, "max_listen_interval") == 0) {
  1816. bss->max_listen_interval = atoi(pos);
  1817. } else if (os_strcmp(buf, "okc") == 0) {
  1818. bss->okc = atoi(pos);
  1819. } else {
  1820. printf("Line %d: unknown configuration item '%s'\n",
  1821. line, buf);
  1822. errors++;
  1823. }
  1824. }
  1825. fclose(f);
  1826. if (bss->individual_wep_key_len == 0) {
  1827. /* individual keys are not use; can use key idx0 for broadcast
  1828. * keys */
  1829. bss->broadcast_key_idx_min = 0;
  1830. }
  1831. /* Select group cipher based on the enabled pairwise cipher suites */
  1832. pairwise = 0;
  1833. if (bss->wpa & 1)
  1834. pairwise |= bss->wpa_pairwise;
  1835. if (bss->wpa & 2) {
  1836. if (bss->rsn_pairwise == 0)
  1837. bss->rsn_pairwise = bss->wpa_pairwise;
  1838. pairwise |= bss->rsn_pairwise;
  1839. }
  1840. if (pairwise & WPA_CIPHER_TKIP)
  1841. bss->wpa_group = WPA_CIPHER_TKIP;
  1842. else
  1843. bss->wpa_group = WPA_CIPHER_CCMP;
  1844. for (i = 0; i < conf->num_bss; i++) {
  1845. bss = &conf->bss[i];
  1846. bss->radius->auth_server = bss->radius->auth_servers;
  1847. bss->radius->acct_server = bss->radius->acct_servers;
  1848. if (bss->wpa && bss->ieee802_1x) {
  1849. bss->ssid.security_policy = SECURITY_WPA;
  1850. } else if (bss->wpa) {
  1851. bss->ssid.security_policy = SECURITY_WPA_PSK;
  1852. } else if (bss->ieee802_1x) {
  1853. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  1854. bss->ssid.wep.default_len = bss->default_wep_key_len;
  1855. } else if (bss->ssid.wep.keys_set)
  1856. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  1857. else
  1858. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  1859. }
  1860. if (hostapd_config_check(conf))
  1861. errors++;
  1862. if (errors) {
  1863. printf("%d errors found in configuration file '%s'\n",
  1864. errors, fname);
  1865. hostapd_config_free(conf);
  1866. conf = NULL;
  1867. }
  1868. return conf;
  1869. }
  1870. int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
  1871. {
  1872. int i;
  1873. if (a->idx != b->idx || a->default_len != b->default_len)
  1874. return 1;
  1875. for (i = 0; i < NUM_WEP_KEYS; i++)
  1876. if (a->len[i] != b->len[i] ||
  1877. os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
  1878. return 1;
  1879. return 0;
  1880. }
  1881. static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
  1882. int num_servers)
  1883. {
  1884. int i;
  1885. for (i = 0; i < num_servers; i++) {
  1886. os_free(servers[i].shared_secret);
  1887. }
  1888. os_free(servers);
  1889. }
  1890. static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
  1891. {
  1892. os_free(user->identity);
  1893. os_free(user->password);
  1894. os_free(user);
  1895. }
  1896. static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
  1897. {
  1898. int i;
  1899. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1900. os_free(keys->key[i]);
  1901. keys->key[i] = NULL;
  1902. }
  1903. }
  1904. static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
  1905. {
  1906. struct hostapd_wpa_psk *psk, *prev;
  1907. struct hostapd_eap_user *user, *prev_user;
  1908. if (conf == NULL)
  1909. return;
  1910. psk = conf->ssid.wpa_psk;
  1911. while (psk) {
  1912. prev = psk;
  1913. psk = psk->next;
  1914. os_free(prev);
  1915. }
  1916. os_free(conf->ssid.wpa_passphrase);
  1917. os_free(conf->ssid.wpa_psk_file);
  1918. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  1919. os_free(conf->ssid.vlan_tagged_interface);
  1920. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  1921. user = conf->eap_user;
  1922. while (user) {
  1923. prev_user = user;
  1924. user = user->next;
  1925. hostapd_config_free_eap_user(prev_user);
  1926. }
  1927. os_free(conf->dump_log_name);
  1928. os_free(conf->eap_req_id_text);
  1929. os_free(conf->accept_mac);
  1930. os_free(conf->deny_mac);
  1931. os_free(conf->nas_identifier);
  1932. hostapd_config_free_radius(conf->radius->auth_servers,
  1933. conf->radius->num_auth_servers);
  1934. hostapd_config_free_radius(conf->radius->acct_servers,
  1935. conf->radius->num_acct_servers);
  1936. os_free(conf->rsn_preauth_interfaces);
  1937. os_free(conf->ctrl_interface);
  1938. os_free(conf->ca_cert);
  1939. os_free(conf->server_cert);
  1940. os_free(conf->private_key);
  1941. os_free(conf->private_key_passwd);
  1942. os_free(conf->dh_file);
  1943. os_free(conf->pac_opaque_encr_key);
  1944. os_free(conf->eap_fast_a_id);
  1945. os_free(conf->eap_fast_a_id_info);
  1946. os_free(conf->eap_sim_db);
  1947. os_free(conf->radius_server_clients);
  1948. os_free(conf->test_socket);
  1949. os_free(conf->radius);
  1950. hostapd_config_free_vlan(conf);
  1951. if (conf->ssid.dyn_vlan_keys) {
  1952. struct hostapd_ssid *ssid = &conf->ssid;
  1953. size_t i;
  1954. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  1955. if (ssid->dyn_vlan_keys[i] == NULL)
  1956. continue;
  1957. hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
  1958. os_free(ssid->dyn_vlan_keys[i]);
  1959. }
  1960. os_free(ssid->dyn_vlan_keys);
  1961. ssid->dyn_vlan_keys = NULL;
  1962. }
  1963. #ifdef CONFIG_IEEE80211R
  1964. {
  1965. struct ft_remote_r0kh *r0kh, *r0kh_prev;
  1966. struct ft_remote_r1kh *r1kh, *r1kh_prev;
  1967. r0kh = conf->r0kh_list;
  1968. conf->r0kh_list = NULL;
  1969. while (r0kh) {
  1970. r0kh_prev = r0kh;
  1971. r0kh = r0kh->next;
  1972. os_free(r0kh_prev);
  1973. }
  1974. r1kh = conf->r1kh_list;
  1975. conf->r1kh_list = NULL;
  1976. while (r1kh) {
  1977. r1kh_prev = r1kh;
  1978. r1kh = r1kh->next;
  1979. os_free(r1kh_prev);
  1980. }
  1981. }
  1982. #endif /* CONFIG_IEEE80211R */
  1983. }
  1984. void hostapd_config_free(struct hostapd_config *conf)
  1985. {
  1986. size_t i;
  1987. if (conf == NULL)
  1988. return;
  1989. for (i = 0; i < conf->num_bss; i++)
  1990. hostapd_config_free_bss(&conf->bss[i]);
  1991. os_free(conf->bss);
  1992. os_free(conf);
  1993. }
  1994. /* Perform a binary search for given MAC address from a pre-sorted list.
  1995. * Returns 1 if address is in the list or 0 if not. */
  1996. int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
  1997. const u8 *addr, int *vlan_id)
  1998. {
  1999. int start, end, middle, res;
  2000. start = 0;
  2001. end = num_entries - 1;
  2002. while (start <= end) {
  2003. middle = (start + end) / 2;
  2004. res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
  2005. if (res == 0) {
  2006. if (vlan_id)
  2007. *vlan_id = list[middle].vlan_id;
  2008. return 1;
  2009. }
  2010. if (res < 0)
  2011. start = middle + 1;
  2012. else
  2013. end = middle - 1;
  2014. }
  2015. return 0;
  2016. }
  2017. int hostapd_rate_found(int *list, int rate)
  2018. {
  2019. int i;
  2020. if (list == NULL)
  2021. return 0;
  2022. for (i = 0; list[i] >= 0; i++)
  2023. if (list[i] == rate)
  2024. return 1;
  2025. return 0;
  2026. }
  2027. const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
  2028. {
  2029. struct hostapd_vlan *v = vlan;
  2030. while (v) {
  2031. if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
  2032. return v->ifname;
  2033. v = v->next;
  2034. }
  2035. return NULL;
  2036. }
  2037. const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
  2038. const u8 *addr, const u8 *prev_psk)
  2039. {
  2040. struct hostapd_wpa_psk *psk;
  2041. int next_ok = prev_psk == NULL;
  2042. for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
  2043. if (next_ok &&
  2044. (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
  2045. return psk->psk;
  2046. if (psk->psk == prev_psk)
  2047. next_ok = 1;
  2048. }
  2049. return NULL;
  2050. }
  2051. const struct hostapd_eap_user *
  2052. hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
  2053. size_t identity_len, int phase2)
  2054. {
  2055. struct hostapd_eap_user *user = conf->eap_user;
  2056. while (user) {
  2057. if (!phase2 && user->identity == NULL) {
  2058. /* Wildcard match */
  2059. break;
  2060. }
  2061. if (user->phase2 == !!phase2 && user->wildcard_prefix &&
  2062. identity_len >= user->identity_len &&
  2063. os_memcmp(user->identity, identity, user->identity_len) ==
  2064. 0) {
  2065. /* Wildcard prefix match */
  2066. break;
  2067. }
  2068. if (user->phase2 == !!phase2 &&
  2069. user->identity_len == identity_len &&
  2070. os_memcmp(user->identity, identity, identity_len) == 0)
  2071. break;
  2072. user = user->next;
  2073. }
  2074. return user;
  2075. }