config.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822
  1. /*
  2. * WPA Supplicant / Configuration parser and common functions
  3. * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "utils/uuid.h"
  11. #include "crypto/sha1.h"
  12. #include "rsn_supp/wpa.h"
  13. #include "eap_peer/eap.h"
  14. #include "config.h"
  15. #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
  16. #define NO_CONFIG_WRITE
  17. #endif
  18. /*
  19. * Structure for network configuration parsing. This data is used to implement
  20. * a generic parser for each network block variable. The table of configuration
  21. * variables is defined below in this file (ssid_fields[]).
  22. */
  23. struct parse_data {
  24. /* Configuration variable name */
  25. char *name;
  26. /* Parser function for this variable */
  27. int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
  28. int line, const char *value);
  29. #ifndef NO_CONFIG_WRITE
  30. /* Writer function (i.e., to get the variable in text format from
  31. * internal presentation). */
  32. char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
  33. #endif /* NO_CONFIG_WRITE */
  34. /* Variable specific parameters for the parser. */
  35. void *param1, *param2, *param3, *param4;
  36. /* 0 = this variable can be included in debug output and ctrl_iface
  37. * 1 = this variable contains key/private data and it must not be
  38. * included in debug output unless explicitly requested. In
  39. * addition, this variable will not be readable through the
  40. * ctrl_iface.
  41. */
  42. int key_data;
  43. };
  44. static char * wpa_config_parse_string(const char *value, size_t *len)
  45. {
  46. if (*value == '"') {
  47. const char *pos;
  48. char *str;
  49. value++;
  50. pos = os_strrchr(value, '"');
  51. if (pos == NULL || pos[1] != '\0')
  52. return NULL;
  53. *len = pos - value;
  54. str = os_malloc(*len + 1);
  55. if (str == NULL)
  56. return NULL;
  57. os_memcpy(str, value, *len);
  58. str[*len] = '\0';
  59. return str;
  60. } else {
  61. u8 *str;
  62. size_t tlen, hlen = os_strlen(value);
  63. if (hlen & 1)
  64. return NULL;
  65. tlen = hlen / 2;
  66. str = os_malloc(tlen + 1);
  67. if (str == NULL)
  68. return NULL;
  69. if (hexstr2bin(value, str, tlen)) {
  70. os_free(str);
  71. return NULL;
  72. }
  73. str[tlen] = '\0';
  74. *len = tlen;
  75. return (char *) str;
  76. }
  77. }
  78. static int wpa_config_parse_str(const struct parse_data *data,
  79. struct wpa_ssid *ssid,
  80. int line, const char *value)
  81. {
  82. size_t res_len, *dst_len;
  83. char **dst, *tmp;
  84. if (os_strcmp(value, "NULL") == 0) {
  85. wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
  86. data->name);
  87. tmp = NULL;
  88. res_len = 0;
  89. goto set;
  90. }
  91. tmp = wpa_config_parse_string(value, &res_len);
  92. if (tmp == NULL) {
  93. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
  94. line, data->name,
  95. data->key_data ? "[KEY DATA REMOVED]" : value);
  96. return -1;
  97. }
  98. if (data->key_data) {
  99. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  100. (u8 *) tmp, res_len);
  101. } else {
  102. wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
  103. (u8 *) tmp, res_len);
  104. }
  105. if (data->param3 && res_len < (size_t) data->param3) {
  106. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  107. "min_len=%ld)", line, data->name,
  108. (unsigned long) res_len, (long) data->param3);
  109. os_free(tmp);
  110. return -1;
  111. }
  112. if (data->param4 && res_len > (size_t) data->param4) {
  113. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  114. "max_len=%ld)", line, data->name,
  115. (unsigned long) res_len, (long) data->param4);
  116. os_free(tmp);
  117. return -1;
  118. }
  119. set:
  120. dst = (char **) (((u8 *) ssid) + (long) data->param1);
  121. dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
  122. os_free(*dst);
  123. *dst = tmp;
  124. if (data->param2)
  125. *dst_len = res_len;
  126. return 0;
  127. }
  128. #ifndef NO_CONFIG_WRITE
  129. static int is_hex(const u8 *data, size_t len)
  130. {
  131. size_t i;
  132. for (i = 0; i < len; i++) {
  133. if (data[i] < 32 || data[i] >= 127)
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
  139. {
  140. char *buf;
  141. buf = os_malloc(len + 3);
  142. if (buf == NULL)
  143. return NULL;
  144. buf[0] = '"';
  145. os_memcpy(buf + 1, value, len);
  146. buf[len + 1] = '"';
  147. buf[len + 2] = '\0';
  148. return buf;
  149. }
  150. static char * wpa_config_write_string_hex(const u8 *value, size_t len)
  151. {
  152. char *buf;
  153. buf = os_zalloc(2 * len + 1);
  154. if (buf == NULL)
  155. return NULL;
  156. wpa_snprintf_hex(buf, 2 * len + 1, value, len);
  157. return buf;
  158. }
  159. static char * wpa_config_write_string(const u8 *value, size_t len)
  160. {
  161. if (value == NULL)
  162. return NULL;
  163. if (is_hex(value, len))
  164. return wpa_config_write_string_hex(value, len);
  165. else
  166. return wpa_config_write_string_ascii(value, len);
  167. }
  168. static char * wpa_config_write_str(const struct parse_data *data,
  169. struct wpa_ssid *ssid)
  170. {
  171. size_t len;
  172. char **src;
  173. src = (char **) (((u8 *) ssid) + (long) data->param1);
  174. if (*src == NULL)
  175. return NULL;
  176. if (data->param2)
  177. len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
  178. else
  179. len = os_strlen(*src);
  180. return wpa_config_write_string((const u8 *) *src, len);
  181. }
  182. #endif /* NO_CONFIG_WRITE */
  183. static int wpa_config_parse_int(const struct parse_data *data,
  184. struct wpa_ssid *ssid,
  185. int line, const char *value)
  186. {
  187. int *dst;
  188. dst = (int *) (((u8 *) ssid) + (long) data->param1);
  189. *dst = atoi(value);
  190. wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
  191. if (data->param3 && *dst < (long) data->param3) {
  192. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  193. "min_value=%ld)", line, data->name, *dst,
  194. (long) data->param3);
  195. *dst = (long) data->param3;
  196. return -1;
  197. }
  198. if (data->param4 && *dst > (long) data->param4) {
  199. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  200. "max_value=%ld)", line, data->name, *dst,
  201. (long) data->param4);
  202. *dst = (long) data->param4;
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. #ifndef NO_CONFIG_WRITE
  208. static char * wpa_config_write_int(const struct parse_data *data,
  209. struct wpa_ssid *ssid)
  210. {
  211. int *src, res;
  212. char *value;
  213. src = (int *) (((u8 *) ssid) + (long) data->param1);
  214. value = os_malloc(20);
  215. if (value == NULL)
  216. return NULL;
  217. res = os_snprintf(value, 20, "%d", *src);
  218. if (res < 0 || res >= 20) {
  219. os_free(value);
  220. return NULL;
  221. }
  222. value[20 - 1] = '\0';
  223. return value;
  224. }
  225. #endif /* NO_CONFIG_WRITE */
  226. static int wpa_config_parse_bssid(const struct parse_data *data,
  227. struct wpa_ssid *ssid, int line,
  228. const char *value)
  229. {
  230. if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
  231. os_strcmp(value, "any") == 0) {
  232. ssid->bssid_set = 0;
  233. wpa_printf(MSG_MSGDUMP, "BSSID any");
  234. return 0;
  235. }
  236. if (hwaddr_aton(value, ssid->bssid)) {
  237. wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
  238. line, value);
  239. return -1;
  240. }
  241. ssid->bssid_set = 1;
  242. wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
  243. return 0;
  244. }
  245. #ifndef NO_CONFIG_WRITE
  246. static char * wpa_config_write_bssid(const struct parse_data *data,
  247. struct wpa_ssid *ssid)
  248. {
  249. char *value;
  250. int res;
  251. if (!ssid->bssid_set)
  252. return NULL;
  253. value = os_malloc(20);
  254. if (value == NULL)
  255. return NULL;
  256. res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
  257. if (res < 0 || res >= 20) {
  258. os_free(value);
  259. return NULL;
  260. }
  261. value[20 - 1] = '\0';
  262. return value;
  263. }
  264. #endif /* NO_CONFIG_WRITE */
  265. static int wpa_config_parse_psk(const struct parse_data *data,
  266. struct wpa_ssid *ssid, int line,
  267. const char *value)
  268. {
  269. if (*value == '"') {
  270. #ifndef CONFIG_NO_PBKDF2
  271. const char *pos;
  272. size_t len;
  273. value++;
  274. pos = os_strrchr(value, '"');
  275. if (pos)
  276. len = pos - value;
  277. else
  278. len = os_strlen(value);
  279. if (len < 8 || len > 63) {
  280. wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
  281. "length %lu (expected: 8..63) '%s'.",
  282. line, (unsigned long) len, value);
  283. return -1;
  284. }
  285. wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
  286. (u8 *) value, len);
  287. if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
  288. os_memcmp(ssid->passphrase, value, len) == 0)
  289. return 0;
  290. ssid->psk_set = 0;
  291. os_free(ssid->passphrase);
  292. ssid->passphrase = os_malloc(len + 1);
  293. if (ssid->passphrase == NULL)
  294. return -1;
  295. os_memcpy(ssid->passphrase, value, len);
  296. ssid->passphrase[len] = '\0';
  297. return 0;
  298. #else /* CONFIG_NO_PBKDF2 */
  299. wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
  300. "supported.", line);
  301. return -1;
  302. #endif /* CONFIG_NO_PBKDF2 */
  303. }
  304. if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
  305. value[PMK_LEN * 2] != '\0') {
  306. wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
  307. line, value);
  308. return -1;
  309. }
  310. os_free(ssid->passphrase);
  311. ssid->passphrase = NULL;
  312. ssid->psk_set = 1;
  313. wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
  314. return 0;
  315. }
  316. #ifndef NO_CONFIG_WRITE
  317. static char * wpa_config_write_psk(const struct parse_data *data,
  318. struct wpa_ssid *ssid)
  319. {
  320. if (ssid->passphrase)
  321. return wpa_config_write_string_ascii(
  322. (const u8 *) ssid->passphrase,
  323. os_strlen(ssid->passphrase));
  324. if (ssid->psk_set)
  325. return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
  326. return NULL;
  327. }
  328. #endif /* NO_CONFIG_WRITE */
  329. static int wpa_config_parse_proto(const struct parse_data *data,
  330. struct wpa_ssid *ssid, int line,
  331. const char *value)
  332. {
  333. int val = 0, last, errors = 0;
  334. char *start, *end, *buf;
  335. buf = os_strdup(value);
  336. if (buf == NULL)
  337. return -1;
  338. start = buf;
  339. while (*start != '\0') {
  340. while (*start == ' ' || *start == '\t')
  341. start++;
  342. if (*start == '\0')
  343. break;
  344. end = start;
  345. while (*end != ' ' && *end != '\t' && *end != '\0')
  346. end++;
  347. last = *end == '\0';
  348. *end = '\0';
  349. if (os_strcmp(start, "WPA") == 0)
  350. val |= WPA_PROTO_WPA;
  351. else if (os_strcmp(start, "RSN") == 0 ||
  352. os_strcmp(start, "WPA2") == 0)
  353. val |= WPA_PROTO_RSN;
  354. else {
  355. wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
  356. line, start);
  357. errors++;
  358. }
  359. if (last)
  360. break;
  361. start = end + 1;
  362. }
  363. os_free(buf);
  364. if (val == 0) {
  365. wpa_printf(MSG_ERROR,
  366. "Line %d: no proto values configured.", line);
  367. errors++;
  368. }
  369. wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
  370. ssid->proto = val;
  371. return errors ? -1 : 0;
  372. }
  373. #ifndef NO_CONFIG_WRITE
  374. static char * wpa_config_write_proto(const struct parse_data *data,
  375. struct wpa_ssid *ssid)
  376. {
  377. int first = 1, ret;
  378. char *buf, *pos, *end;
  379. pos = buf = os_zalloc(10);
  380. if (buf == NULL)
  381. return NULL;
  382. end = buf + 10;
  383. if (ssid->proto & WPA_PROTO_WPA) {
  384. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  385. if (ret < 0 || ret >= end - pos)
  386. return buf;
  387. pos += ret;
  388. first = 0;
  389. }
  390. if (ssid->proto & WPA_PROTO_RSN) {
  391. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  392. if (ret < 0 || ret >= end - pos)
  393. return buf;
  394. pos += ret;
  395. first = 0;
  396. }
  397. return buf;
  398. }
  399. #endif /* NO_CONFIG_WRITE */
  400. static int wpa_config_parse_key_mgmt(const struct parse_data *data,
  401. struct wpa_ssid *ssid, int line,
  402. const char *value)
  403. {
  404. int val = 0, last, errors = 0;
  405. char *start, *end, *buf;
  406. buf = os_strdup(value);
  407. if (buf == NULL)
  408. return -1;
  409. start = buf;
  410. while (*start != '\0') {
  411. while (*start == ' ' || *start == '\t')
  412. start++;
  413. if (*start == '\0')
  414. break;
  415. end = start;
  416. while (*end != ' ' && *end != '\t' && *end != '\0')
  417. end++;
  418. last = *end == '\0';
  419. *end = '\0';
  420. if (os_strcmp(start, "WPA-PSK") == 0)
  421. val |= WPA_KEY_MGMT_PSK;
  422. else if (os_strcmp(start, "WPA-EAP") == 0)
  423. val |= WPA_KEY_MGMT_IEEE8021X;
  424. else if (os_strcmp(start, "IEEE8021X") == 0)
  425. val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  426. else if (os_strcmp(start, "NONE") == 0)
  427. val |= WPA_KEY_MGMT_NONE;
  428. else if (os_strcmp(start, "WPA-NONE") == 0)
  429. val |= WPA_KEY_MGMT_WPA_NONE;
  430. #ifdef CONFIG_IEEE80211R
  431. else if (os_strcmp(start, "FT-PSK") == 0)
  432. val |= WPA_KEY_MGMT_FT_PSK;
  433. else if (os_strcmp(start, "FT-EAP") == 0)
  434. val |= WPA_KEY_MGMT_FT_IEEE8021X;
  435. #endif /* CONFIG_IEEE80211R */
  436. #ifdef CONFIG_IEEE80211W
  437. else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
  438. val |= WPA_KEY_MGMT_PSK_SHA256;
  439. else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
  440. val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
  441. #endif /* CONFIG_IEEE80211W */
  442. #ifdef CONFIG_WPS
  443. else if (os_strcmp(start, "WPS") == 0)
  444. val |= WPA_KEY_MGMT_WPS;
  445. #endif /* CONFIG_WPS */
  446. else {
  447. wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
  448. line, start);
  449. errors++;
  450. }
  451. if (last)
  452. break;
  453. start = end + 1;
  454. }
  455. os_free(buf);
  456. if (val == 0) {
  457. wpa_printf(MSG_ERROR,
  458. "Line %d: no key_mgmt values configured.", line);
  459. errors++;
  460. }
  461. wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
  462. ssid->key_mgmt = val;
  463. return errors ? -1 : 0;
  464. }
  465. #ifndef NO_CONFIG_WRITE
  466. static char * wpa_config_write_key_mgmt(const struct parse_data *data,
  467. struct wpa_ssid *ssid)
  468. {
  469. char *buf, *pos, *end;
  470. int ret;
  471. pos = buf = os_zalloc(50);
  472. if (buf == NULL)
  473. return NULL;
  474. end = buf + 50;
  475. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  476. ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
  477. pos == buf ? "" : " ");
  478. if (ret < 0 || ret >= end - pos) {
  479. end[-1] = '\0';
  480. return buf;
  481. }
  482. pos += ret;
  483. }
  484. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  485. ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
  486. pos == buf ? "" : " ");
  487. if (ret < 0 || ret >= end - pos) {
  488. end[-1] = '\0';
  489. return buf;
  490. }
  491. pos += ret;
  492. }
  493. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  494. ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
  495. pos == buf ? "" : " ");
  496. if (ret < 0 || ret >= end - pos) {
  497. end[-1] = '\0';
  498. return buf;
  499. }
  500. pos += ret;
  501. }
  502. if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
  503. ret = os_snprintf(pos, end - pos, "%sNONE",
  504. pos == buf ? "" : " ");
  505. if (ret < 0 || ret >= end - pos) {
  506. end[-1] = '\0';
  507. return buf;
  508. }
  509. pos += ret;
  510. }
  511. if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  512. ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
  513. pos == buf ? "" : " ");
  514. if (ret < 0 || ret >= end - pos) {
  515. end[-1] = '\0';
  516. return buf;
  517. }
  518. pos += ret;
  519. }
  520. #ifdef CONFIG_IEEE80211R
  521. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  522. pos += os_snprintf(pos, end - pos, "%sFT-PSK",
  523. pos == buf ? "" : " ");
  524. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  525. pos += os_snprintf(pos, end - pos, "%sFT-EAP",
  526. pos == buf ? "" : " ");
  527. #endif /* CONFIG_IEEE80211R */
  528. #ifdef CONFIG_IEEE80211W
  529. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  530. pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
  531. pos == buf ? "" : " ");
  532. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  533. pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
  534. pos == buf ? "" : " ");
  535. #endif /* CONFIG_IEEE80211W */
  536. #ifdef CONFIG_WPS
  537. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  538. pos += os_snprintf(pos, end - pos, "%sWPS",
  539. pos == buf ? "" : " ");
  540. #endif /* CONFIG_WPS */
  541. return buf;
  542. }
  543. #endif /* NO_CONFIG_WRITE */
  544. static int wpa_config_parse_cipher(int line, const char *value)
  545. {
  546. int val = 0, last;
  547. char *start, *end, *buf;
  548. buf = os_strdup(value);
  549. if (buf == NULL)
  550. return -1;
  551. start = buf;
  552. while (*start != '\0') {
  553. while (*start == ' ' || *start == '\t')
  554. start++;
  555. if (*start == '\0')
  556. break;
  557. end = start;
  558. while (*end != ' ' && *end != '\t' && *end != '\0')
  559. end++;
  560. last = *end == '\0';
  561. *end = '\0';
  562. if (os_strcmp(start, "CCMP") == 0)
  563. val |= WPA_CIPHER_CCMP;
  564. else if (os_strcmp(start, "TKIP") == 0)
  565. val |= WPA_CIPHER_TKIP;
  566. else if (os_strcmp(start, "WEP104") == 0)
  567. val |= WPA_CIPHER_WEP104;
  568. else if (os_strcmp(start, "WEP40") == 0)
  569. val |= WPA_CIPHER_WEP40;
  570. else if (os_strcmp(start, "NONE") == 0)
  571. val |= WPA_CIPHER_NONE;
  572. else {
  573. wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
  574. line, start);
  575. os_free(buf);
  576. return -1;
  577. }
  578. if (last)
  579. break;
  580. start = end + 1;
  581. }
  582. os_free(buf);
  583. if (val == 0) {
  584. wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
  585. line);
  586. return -1;
  587. }
  588. return val;
  589. }
  590. #ifndef NO_CONFIG_WRITE
  591. static char * wpa_config_write_cipher(int cipher)
  592. {
  593. char *buf, *pos, *end;
  594. int ret;
  595. pos = buf = os_zalloc(50);
  596. if (buf == NULL)
  597. return NULL;
  598. end = buf + 50;
  599. if (cipher & WPA_CIPHER_CCMP) {
  600. ret = os_snprintf(pos, end - pos, "%sCCMP",
  601. pos == buf ? "" : " ");
  602. if (ret < 0 || ret >= end - pos) {
  603. end[-1] = '\0';
  604. return buf;
  605. }
  606. pos += ret;
  607. }
  608. if (cipher & WPA_CIPHER_TKIP) {
  609. ret = os_snprintf(pos, end - pos, "%sTKIP",
  610. pos == buf ? "" : " ");
  611. if (ret < 0 || ret >= end - pos) {
  612. end[-1] = '\0';
  613. return buf;
  614. }
  615. pos += ret;
  616. }
  617. if (cipher & WPA_CIPHER_WEP104) {
  618. ret = os_snprintf(pos, end - pos, "%sWEP104",
  619. pos == buf ? "" : " ");
  620. if (ret < 0 || ret >= end - pos) {
  621. end[-1] = '\0';
  622. return buf;
  623. }
  624. pos += ret;
  625. }
  626. if (cipher & WPA_CIPHER_WEP40) {
  627. ret = os_snprintf(pos, end - pos, "%sWEP40",
  628. pos == buf ? "" : " ");
  629. if (ret < 0 || ret >= end - pos) {
  630. end[-1] = '\0';
  631. return buf;
  632. }
  633. pos += ret;
  634. }
  635. if (cipher & WPA_CIPHER_NONE) {
  636. ret = os_snprintf(pos, end - pos, "%sNONE",
  637. pos == buf ? "" : " ");
  638. if (ret < 0 || ret >= end - pos) {
  639. end[-1] = '\0';
  640. return buf;
  641. }
  642. pos += ret;
  643. }
  644. return buf;
  645. }
  646. #endif /* NO_CONFIG_WRITE */
  647. static int wpa_config_parse_pairwise(const struct parse_data *data,
  648. struct wpa_ssid *ssid, int line,
  649. const char *value)
  650. {
  651. int val;
  652. val = wpa_config_parse_cipher(line, value);
  653. if (val == -1)
  654. return -1;
  655. if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
  656. wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
  657. "(0x%x).", line, val);
  658. return -1;
  659. }
  660. wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
  661. ssid->pairwise_cipher = val;
  662. return 0;
  663. }
  664. #ifndef NO_CONFIG_WRITE
  665. static char * wpa_config_write_pairwise(const struct parse_data *data,
  666. struct wpa_ssid *ssid)
  667. {
  668. return wpa_config_write_cipher(ssid->pairwise_cipher);
  669. }
  670. #endif /* NO_CONFIG_WRITE */
  671. static int wpa_config_parse_group(const struct parse_data *data,
  672. struct wpa_ssid *ssid, int line,
  673. const char *value)
  674. {
  675. int val;
  676. val = wpa_config_parse_cipher(line, value);
  677. if (val == -1)
  678. return -1;
  679. if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
  680. WPA_CIPHER_WEP40)) {
  681. wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
  682. "(0x%x).", line, val);
  683. return -1;
  684. }
  685. wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
  686. ssid->group_cipher = val;
  687. return 0;
  688. }
  689. #ifndef NO_CONFIG_WRITE
  690. static char * wpa_config_write_group(const struct parse_data *data,
  691. struct wpa_ssid *ssid)
  692. {
  693. return wpa_config_write_cipher(ssid->group_cipher);
  694. }
  695. #endif /* NO_CONFIG_WRITE */
  696. static int wpa_config_parse_auth_alg(const struct parse_data *data,
  697. struct wpa_ssid *ssid, int line,
  698. const char *value)
  699. {
  700. int val = 0, last, errors = 0;
  701. char *start, *end, *buf;
  702. buf = os_strdup(value);
  703. if (buf == NULL)
  704. return -1;
  705. start = buf;
  706. while (*start != '\0') {
  707. while (*start == ' ' || *start == '\t')
  708. start++;
  709. if (*start == '\0')
  710. break;
  711. end = start;
  712. while (*end != ' ' && *end != '\t' && *end != '\0')
  713. end++;
  714. last = *end == '\0';
  715. *end = '\0';
  716. if (os_strcmp(start, "OPEN") == 0)
  717. val |= WPA_AUTH_ALG_OPEN;
  718. else if (os_strcmp(start, "SHARED") == 0)
  719. val |= WPA_AUTH_ALG_SHARED;
  720. else if (os_strcmp(start, "LEAP") == 0)
  721. val |= WPA_AUTH_ALG_LEAP;
  722. else {
  723. wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
  724. line, start);
  725. errors++;
  726. }
  727. if (last)
  728. break;
  729. start = end + 1;
  730. }
  731. os_free(buf);
  732. if (val == 0) {
  733. wpa_printf(MSG_ERROR,
  734. "Line %d: no auth_alg values configured.", line);
  735. errors++;
  736. }
  737. wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
  738. ssid->auth_alg = val;
  739. return errors ? -1 : 0;
  740. }
  741. #ifndef NO_CONFIG_WRITE
  742. static char * wpa_config_write_auth_alg(const struct parse_data *data,
  743. struct wpa_ssid *ssid)
  744. {
  745. char *buf, *pos, *end;
  746. int ret;
  747. pos = buf = os_zalloc(30);
  748. if (buf == NULL)
  749. return NULL;
  750. end = buf + 30;
  751. if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
  752. ret = os_snprintf(pos, end - pos, "%sOPEN",
  753. pos == buf ? "" : " ");
  754. if (ret < 0 || ret >= end - pos) {
  755. end[-1] = '\0';
  756. return buf;
  757. }
  758. pos += ret;
  759. }
  760. if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
  761. ret = os_snprintf(pos, end - pos, "%sSHARED",
  762. pos == buf ? "" : " ");
  763. if (ret < 0 || ret >= end - pos) {
  764. end[-1] = '\0';
  765. return buf;
  766. }
  767. pos += ret;
  768. }
  769. if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
  770. ret = os_snprintf(pos, end - pos, "%sLEAP",
  771. pos == buf ? "" : " ");
  772. if (ret < 0 || ret >= end - pos) {
  773. end[-1] = '\0';
  774. return buf;
  775. }
  776. pos += ret;
  777. }
  778. return buf;
  779. }
  780. #endif /* NO_CONFIG_WRITE */
  781. static int * wpa_config_parse_freqs(const struct parse_data *data,
  782. struct wpa_ssid *ssid, int line,
  783. const char *value)
  784. {
  785. int *freqs;
  786. size_t used, len;
  787. const char *pos;
  788. used = 0;
  789. len = 10;
  790. freqs = os_zalloc((len + 1) * sizeof(int));
  791. if (freqs == NULL)
  792. return NULL;
  793. pos = value;
  794. while (pos) {
  795. while (*pos == ' ')
  796. pos++;
  797. if (used == len) {
  798. int *n;
  799. size_t i;
  800. n = os_realloc(freqs, (len * 2 + 1) * sizeof(int));
  801. if (n == NULL) {
  802. os_free(freqs);
  803. return NULL;
  804. }
  805. for (i = len; i <= len * 2; i++)
  806. n[i] = 0;
  807. freqs = n;
  808. len *= 2;
  809. }
  810. freqs[used] = atoi(pos);
  811. if (freqs[used] == 0)
  812. break;
  813. used++;
  814. pos = os_strchr(pos + 1, ' ');
  815. }
  816. return freqs;
  817. }
  818. static int wpa_config_parse_scan_freq(const struct parse_data *data,
  819. struct wpa_ssid *ssid, int line,
  820. const char *value)
  821. {
  822. int *freqs;
  823. freqs = wpa_config_parse_freqs(data, ssid, line, value);
  824. if (freqs == NULL)
  825. return -1;
  826. os_free(ssid->scan_freq);
  827. ssid->scan_freq = freqs;
  828. return 0;
  829. }
  830. static int wpa_config_parse_freq_list(const struct parse_data *data,
  831. struct wpa_ssid *ssid, int line,
  832. const char *value)
  833. {
  834. int *freqs;
  835. freqs = wpa_config_parse_freqs(data, ssid, line, value);
  836. if (freqs == NULL)
  837. return -1;
  838. os_free(ssid->freq_list);
  839. ssid->freq_list = freqs;
  840. return 0;
  841. }
  842. #ifndef NO_CONFIG_WRITE
  843. static char * wpa_config_write_freqs(const struct parse_data *data,
  844. const int *freqs)
  845. {
  846. char *buf, *pos, *end;
  847. int i, ret;
  848. size_t count;
  849. if (freqs == NULL)
  850. return NULL;
  851. count = 0;
  852. for (i = 0; freqs[i]; i++)
  853. count++;
  854. pos = buf = os_zalloc(10 * count + 1);
  855. if (buf == NULL)
  856. return NULL;
  857. end = buf + 10 * count + 1;
  858. for (i = 0; freqs[i]; i++) {
  859. ret = os_snprintf(pos, end - pos, "%s%u",
  860. i == 0 ? "" : " ", freqs[i]);
  861. if (ret < 0 || ret >= end - pos) {
  862. end[-1] = '\0';
  863. return buf;
  864. }
  865. pos += ret;
  866. }
  867. return buf;
  868. }
  869. static char * wpa_config_write_scan_freq(const struct parse_data *data,
  870. struct wpa_ssid *ssid)
  871. {
  872. return wpa_config_write_freqs(data, ssid->scan_freq);
  873. }
  874. static char * wpa_config_write_freq_list(const struct parse_data *data,
  875. struct wpa_ssid *ssid)
  876. {
  877. return wpa_config_write_freqs(data, ssid->freq_list);
  878. }
  879. #endif /* NO_CONFIG_WRITE */
  880. #ifdef IEEE8021X_EAPOL
  881. static int wpa_config_parse_eap(const struct parse_data *data,
  882. struct wpa_ssid *ssid, int line,
  883. const char *value)
  884. {
  885. int last, errors = 0;
  886. char *start, *end, *buf;
  887. struct eap_method_type *methods = NULL, *tmp;
  888. size_t num_methods = 0;
  889. buf = os_strdup(value);
  890. if (buf == NULL)
  891. return -1;
  892. start = buf;
  893. while (*start != '\0') {
  894. while (*start == ' ' || *start == '\t')
  895. start++;
  896. if (*start == '\0')
  897. break;
  898. end = start;
  899. while (*end != ' ' && *end != '\t' && *end != '\0')
  900. end++;
  901. last = *end == '\0';
  902. *end = '\0';
  903. tmp = methods;
  904. methods = os_realloc(methods,
  905. (num_methods + 1) * sizeof(*methods));
  906. if (methods == NULL) {
  907. os_free(tmp);
  908. os_free(buf);
  909. return -1;
  910. }
  911. methods[num_methods].method = eap_peer_get_type(
  912. start, &methods[num_methods].vendor);
  913. if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  914. methods[num_methods].method == EAP_TYPE_NONE) {
  915. wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
  916. "'%s'", line, start);
  917. wpa_printf(MSG_ERROR, "You may need to add support for"
  918. " this EAP method during wpa_supplicant\n"
  919. "build time configuration.\n"
  920. "See README for more information.");
  921. errors++;
  922. } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  923. methods[num_methods].method == EAP_TYPE_LEAP)
  924. ssid->leap++;
  925. else
  926. ssid->non_leap++;
  927. num_methods++;
  928. if (last)
  929. break;
  930. start = end + 1;
  931. }
  932. os_free(buf);
  933. tmp = methods;
  934. methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
  935. if (methods == NULL) {
  936. os_free(tmp);
  937. return -1;
  938. }
  939. methods[num_methods].vendor = EAP_VENDOR_IETF;
  940. methods[num_methods].method = EAP_TYPE_NONE;
  941. num_methods++;
  942. wpa_hexdump(MSG_MSGDUMP, "eap methods",
  943. (u8 *) methods, num_methods * sizeof(*methods));
  944. os_free(ssid->eap.eap_methods);
  945. ssid->eap.eap_methods = methods;
  946. return errors ? -1 : 0;
  947. }
  948. static char * wpa_config_write_eap(const struct parse_data *data,
  949. struct wpa_ssid *ssid)
  950. {
  951. int i, ret;
  952. char *buf, *pos, *end;
  953. const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
  954. const char *name;
  955. if (eap_methods == NULL)
  956. return NULL;
  957. pos = buf = os_zalloc(100);
  958. if (buf == NULL)
  959. return NULL;
  960. end = buf + 100;
  961. for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
  962. eap_methods[i].method != EAP_TYPE_NONE; i++) {
  963. name = eap_get_name(eap_methods[i].vendor,
  964. eap_methods[i].method);
  965. if (name) {
  966. ret = os_snprintf(pos, end - pos, "%s%s",
  967. pos == buf ? "" : " ", name);
  968. if (ret < 0 || ret >= end - pos)
  969. break;
  970. pos += ret;
  971. }
  972. }
  973. end[-1] = '\0';
  974. return buf;
  975. }
  976. static int wpa_config_parse_password(const struct parse_data *data,
  977. struct wpa_ssid *ssid, int line,
  978. const char *value)
  979. {
  980. u8 *hash;
  981. if (os_strcmp(value, "NULL") == 0) {
  982. wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
  983. os_free(ssid->eap.password);
  984. ssid->eap.password = NULL;
  985. ssid->eap.password_len = 0;
  986. return 0;
  987. }
  988. if (os_strncmp(value, "hash:", 5) != 0) {
  989. char *tmp;
  990. size_t res_len;
  991. tmp = wpa_config_parse_string(value, &res_len);
  992. if (tmp == NULL) {
  993. wpa_printf(MSG_ERROR, "Line %d: failed to parse "
  994. "password.", line);
  995. return -1;
  996. }
  997. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  998. (u8 *) tmp, res_len);
  999. os_free(ssid->eap.password);
  1000. ssid->eap.password = (u8 *) tmp;
  1001. ssid->eap.password_len = res_len;
  1002. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  1003. return 0;
  1004. }
  1005. /* NtPasswordHash: hash:<32 hex digits> */
  1006. if (os_strlen(value + 5) != 2 * 16) {
  1007. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
  1008. "(expected 32 hex digits)", line);
  1009. return -1;
  1010. }
  1011. hash = os_malloc(16);
  1012. if (hash == NULL)
  1013. return -1;
  1014. if (hexstr2bin(value + 5, hash, 16)) {
  1015. os_free(hash);
  1016. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
  1017. return -1;
  1018. }
  1019. wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
  1020. os_free(ssid->eap.password);
  1021. ssid->eap.password = hash;
  1022. ssid->eap.password_len = 16;
  1023. ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  1024. return 0;
  1025. }
  1026. static char * wpa_config_write_password(const struct parse_data *data,
  1027. struct wpa_ssid *ssid)
  1028. {
  1029. char *buf;
  1030. if (ssid->eap.password == NULL)
  1031. return NULL;
  1032. if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
  1033. return wpa_config_write_string(
  1034. ssid->eap.password, ssid->eap.password_len);
  1035. }
  1036. buf = os_malloc(5 + 32 + 1);
  1037. if (buf == NULL)
  1038. return NULL;
  1039. os_memcpy(buf, "hash:", 5);
  1040. wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
  1041. return buf;
  1042. }
  1043. #endif /* IEEE8021X_EAPOL */
  1044. static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
  1045. const char *value, int idx)
  1046. {
  1047. char *buf, title[20];
  1048. int res;
  1049. buf = wpa_config_parse_string(value, len);
  1050. if (buf == NULL) {
  1051. wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
  1052. line, idx, value);
  1053. return -1;
  1054. }
  1055. if (*len > MAX_WEP_KEY_LEN) {
  1056. wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
  1057. line, idx, value);
  1058. os_free(buf);
  1059. return -1;
  1060. }
  1061. os_memcpy(key, buf, *len);
  1062. os_free(buf);
  1063. res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
  1064. if (res >= 0 && (size_t) res < sizeof(title))
  1065. wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
  1066. return 0;
  1067. }
  1068. static int wpa_config_parse_wep_key0(const struct parse_data *data,
  1069. struct wpa_ssid *ssid, int line,
  1070. const char *value)
  1071. {
  1072. return wpa_config_parse_wep_key(ssid->wep_key[0],
  1073. &ssid->wep_key_len[0], line,
  1074. value, 0);
  1075. }
  1076. static int wpa_config_parse_wep_key1(const struct parse_data *data,
  1077. struct wpa_ssid *ssid, int line,
  1078. const char *value)
  1079. {
  1080. return wpa_config_parse_wep_key(ssid->wep_key[1],
  1081. &ssid->wep_key_len[1], line,
  1082. value, 1);
  1083. }
  1084. static int wpa_config_parse_wep_key2(const struct parse_data *data,
  1085. struct wpa_ssid *ssid, int line,
  1086. const char *value)
  1087. {
  1088. return wpa_config_parse_wep_key(ssid->wep_key[2],
  1089. &ssid->wep_key_len[2], line,
  1090. value, 2);
  1091. }
  1092. static int wpa_config_parse_wep_key3(const struct parse_data *data,
  1093. struct wpa_ssid *ssid, int line,
  1094. const char *value)
  1095. {
  1096. return wpa_config_parse_wep_key(ssid->wep_key[3],
  1097. &ssid->wep_key_len[3], line,
  1098. value, 3);
  1099. }
  1100. #ifndef NO_CONFIG_WRITE
  1101. static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
  1102. {
  1103. if (ssid->wep_key_len[idx] == 0)
  1104. return NULL;
  1105. return wpa_config_write_string(ssid->wep_key[idx],
  1106. ssid->wep_key_len[idx]);
  1107. }
  1108. static char * wpa_config_write_wep_key0(const struct parse_data *data,
  1109. struct wpa_ssid *ssid)
  1110. {
  1111. return wpa_config_write_wep_key(ssid, 0);
  1112. }
  1113. static char * wpa_config_write_wep_key1(const struct parse_data *data,
  1114. struct wpa_ssid *ssid)
  1115. {
  1116. return wpa_config_write_wep_key(ssid, 1);
  1117. }
  1118. static char * wpa_config_write_wep_key2(const struct parse_data *data,
  1119. struct wpa_ssid *ssid)
  1120. {
  1121. return wpa_config_write_wep_key(ssid, 2);
  1122. }
  1123. static char * wpa_config_write_wep_key3(const struct parse_data *data,
  1124. struct wpa_ssid *ssid)
  1125. {
  1126. return wpa_config_write_wep_key(ssid, 3);
  1127. }
  1128. #endif /* NO_CONFIG_WRITE */
  1129. #ifdef CONFIG_P2P
  1130. static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
  1131. struct wpa_ssid *ssid, int line,
  1132. const char *value)
  1133. {
  1134. const char *pos;
  1135. u8 *buf, *n, addr[ETH_ALEN];
  1136. size_t count;
  1137. buf = NULL;
  1138. count = 0;
  1139. pos = value;
  1140. while (pos && *pos) {
  1141. while (*pos == ' ')
  1142. pos++;
  1143. if (hwaddr_aton(pos, addr)) {
  1144. wpa_printf(MSG_ERROR, "Line %d: Invalid "
  1145. "p2p_client_list address '%s'.",
  1146. line, value);
  1147. /* continue anyway */
  1148. } else {
  1149. n = os_realloc(buf, (count + 1) * ETH_ALEN);
  1150. if (n == NULL) {
  1151. os_free(buf);
  1152. return -1;
  1153. }
  1154. buf = n;
  1155. os_memcpy(buf + count * ETH_ALEN, addr, ETH_ALEN);
  1156. count++;
  1157. wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
  1158. addr, ETH_ALEN);
  1159. }
  1160. pos = os_strchr(pos, ' ');
  1161. }
  1162. os_free(ssid->p2p_client_list);
  1163. ssid->p2p_client_list = buf;
  1164. ssid->num_p2p_clients = count;
  1165. return 0;
  1166. }
  1167. #ifndef NO_CONFIG_WRITE
  1168. static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
  1169. struct wpa_ssid *ssid)
  1170. {
  1171. char *value, *end, *pos;
  1172. int res;
  1173. size_t i;
  1174. if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
  1175. return NULL;
  1176. value = os_malloc(20 * ssid->num_p2p_clients);
  1177. if (value == NULL)
  1178. return NULL;
  1179. pos = value;
  1180. end = value + 20 * ssid->num_p2p_clients;
  1181. for (i = 0; i < ssid->num_p2p_clients; i++) {
  1182. res = os_snprintf(pos, end - pos, MACSTR " ",
  1183. MAC2STR(ssid->p2p_client_list +
  1184. i * ETH_ALEN));
  1185. if (res < 0 || res >= end - pos) {
  1186. os_free(value);
  1187. return NULL;
  1188. }
  1189. pos += res;
  1190. }
  1191. if (pos > value)
  1192. pos[-1] = '\0';
  1193. return value;
  1194. }
  1195. #endif /* NO_CONFIG_WRITE */
  1196. #endif /* CONFIG_P2P */
  1197. /* Helper macros for network block parser */
  1198. #ifdef OFFSET
  1199. #undef OFFSET
  1200. #endif /* OFFSET */
  1201. /* OFFSET: Get offset of a variable within the wpa_ssid structure */
  1202. #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
  1203. /* STR: Define a string variable for an ASCII string; f = field name */
  1204. #ifdef NO_CONFIG_WRITE
  1205. #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
  1206. #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
  1207. #else /* NO_CONFIG_WRITE */
  1208. #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
  1209. #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
  1210. #endif /* NO_CONFIG_WRITE */
  1211. #define STR(f) _STR(f), NULL, NULL, NULL, 0
  1212. #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
  1213. #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
  1214. #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
  1215. /* STR_LEN: Define a string variable with a separate variable for storing the
  1216. * data length. Unlike STR(), this can be used to store arbitrary binary data
  1217. * (i.e., even nul termination character). */
  1218. #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
  1219. #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
  1220. #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
  1221. #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
  1222. #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
  1223. /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
  1224. * explicitly specified. */
  1225. #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
  1226. #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
  1227. #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
  1228. #ifdef NO_CONFIG_WRITE
  1229. #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
  1230. #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
  1231. #else /* NO_CONFIG_WRITE */
  1232. #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1233. OFFSET(f), (void *) 0
  1234. #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1235. OFFSET(eap.f), (void *) 0
  1236. #endif /* NO_CONFIG_WRITE */
  1237. /* INT: Define an integer variable */
  1238. #define INT(f) _INT(f), NULL, NULL, 0
  1239. #define INTe(f) _INTe(f), NULL, NULL, 0
  1240. /* INT_RANGE: Define an integer variable with allowed value range */
  1241. #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
  1242. /* FUNC: Define a configuration variable that uses a custom function for
  1243. * parsing and writing the value. */
  1244. #ifdef NO_CONFIG_WRITE
  1245. #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
  1246. #else /* NO_CONFIG_WRITE */
  1247. #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
  1248. NULL, NULL, NULL, NULL
  1249. #endif /* NO_CONFIG_WRITE */
  1250. #define FUNC(f) _FUNC(f), 0
  1251. #define FUNC_KEY(f) _FUNC(f), 1
  1252. /*
  1253. * Table of network configuration variables. This table is used to parse each
  1254. * network configuration variable, e.g., each line in wpa_supplicant.conf file
  1255. * that is inside a network block.
  1256. *
  1257. * This table is generated using the helper macros defined above and with
  1258. * generous help from the C pre-processor. The field name is stored as a string
  1259. * into .name and for STR and INT types, the offset of the target buffer within
  1260. * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
  1261. * offset to the field containing the length of the configuration variable.
  1262. * .param3 and .param4 can be used to mark the allowed range (length for STR
  1263. * and value for INT).
  1264. *
  1265. * For each configuration line in wpa_supplicant.conf, the parser goes through
  1266. * this table and select the entry that matches with the field name. The parser
  1267. * function (.parser) is then called to parse the actual value of the field.
  1268. *
  1269. * This kind of mechanism makes it easy to add new configuration parameters,
  1270. * since only one line needs to be added into this table and into the
  1271. * struct wpa_ssid definition if the new variable is either a string or
  1272. * integer. More complex types will need to use their own parser and writer
  1273. * functions.
  1274. */
  1275. static const struct parse_data ssid_fields[] = {
  1276. { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
  1277. { INT_RANGE(scan_ssid, 0, 1) },
  1278. { FUNC(bssid) },
  1279. { FUNC_KEY(psk) },
  1280. { FUNC(proto) },
  1281. { FUNC(key_mgmt) },
  1282. { FUNC(pairwise) },
  1283. { FUNC(group) },
  1284. { FUNC(auth_alg) },
  1285. { FUNC(scan_freq) },
  1286. { FUNC(freq_list) },
  1287. #ifdef IEEE8021X_EAPOL
  1288. { FUNC(eap) },
  1289. { STR_LENe(identity) },
  1290. { STR_LENe(anonymous_identity) },
  1291. { FUNC_KEY(password) },
  1292. { STRe(ca_cert) },
  1293. { STRe(ca_path) },
  1294. { STRe(client_cert) },
  1295. { STRe(private_key) },
  1296. { STR_KEYe(private_key_passwd) },
  1297. { STRe(dh_file) },
  1298. { STRe(subject_match) },
  1299. { STRe(altsubject_match) },
  1300. { STRe(ca_cert2) },
  1301. { STRe(ca_path2) },
  1302. { STRe(client_cert2) },
  1303. { STRe(private_key2) },
  1304. { STR_KEYe(private_key2_passwd) },
  1305. { STRe(dh_file2) },
  1306. { STRe(subject_match2) },
  1307. { STRe(altsubject_match2) },
  1308. { STRe(phase1) },
  1309. { STRe(phase2) },
  1310. { STRe(pcsc) },
  1311. { STR_KEYe(pin) },
  1312. { STRe(engine_id) },
  1313. { STRe(key_id) },
  1314. { STRe(cert_id) },
  1315. { STRe(ca_cert_id) },
  1316. { STR_KEYe(pin2) },
  1317. { STRe(engine2_id) },
  1318. { STRe(key2_id) },
  1319. { STRe(cert2_id) },
  1320. { STRe(ca_cert2_id) },
  1321. { INTe(engine) },
  1322. { INTe(engine2) },
  1323. { INT(eapol_flags) },
  1324. #endif /* IEEE8021X_EAPOL */
  1325. { FUNC_KEY(wep_key0) },
  1326. { FUNC_KEY(wep_key1) },
  1327. { FUNC_KEY(wep_key2) },
  1328. { FUNC_KEY(wep_key3) },
  1329. { INT(wep_tx_keyidx) },
  1330. { INT(priority) },
  1331. #ifdef IEEE8021X_EAPOL
  1332. { INT(eap_workaround) },
  1333. { STRe(pac_file) },
  1334. { INTe(fragment_size) },
  1335. #endif /* IEEE8021X_EAPOL */
  1336. { INT_RANGE(mode, 0, 4) },
  1337. { INT_RANGE(proactive_key_caching, 0, 1) },
  1338. { INT_RANGE(disabled, 0, 2) },
  1339. { STR(id_str) },
  1340. #ifdef CONFIG_IEEE80211W
  1341. { INT_RANGE(ieee80211w, 0, 2) },
  1342. #endif /* CONFIG_IEEE80211W */
  1343. { INT_RANGE(peerkey, 0, 1) },
  1344. { INT_RANGE(mixed_cell, 0, 1) },
  1345. { INT_RANGE(frequency, 0, 10000) },
  1346. { INT(wpa_ptk_rekey) },
  1347. { STR(bgscan) },
  1348. { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
  1349. #ifdef CONFIG_P2P
  1350. { FUNC(p2p_client_list) },
  1351. #endif /* CONFIG_P2P */
  1352. #ifdef CONFIG_HT_OVERRIDES
  1353. { INT_RANGE(disable_ht, 0, 1) },
  1354. { INT_RANGE(disable_ht40, -1, 1) },
  1355. { INT_RANGE(disable_max_amsdu, -1, 1) },
  1356. { INT_RANGE(ampdu_factor, -1, 3) },
  1357. { INT_RANGE(ampdu_density, -1, 7) },
  1358. { STR(ht_mcs) },
  1359. #endif /* CONFIG_HT_OVERRIDES */
  1360. };
  1361. #undef OFFSET
  1362. #undef _STR
  1363. #undef STR
  1364. #undef STR_KEY
  1365. #undef _STR_LEN
  1366. #undef STR_LEN
  1367. #undef STR_LEN_KEY
  1368. #undef _STR_RANGE
  1369. #undef STR_RANGE
  1370. #undef STR_RANGE_KEY
  1371. #undef _INT
  1372. #undef INT
  1373. #undef INT_RANGE
  1374. #undef _FUNC
  1375. #undef FUNC
  1376. #undef FUNC_KEY
  1377. #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
  1378. /**
  1379. * wpa_config_add_prio_network - Add a network to priority lists
  1380. * @config: Configuration data from wpa_config_read()
  1381. * @ssid: Pointer to the network configuration to be added to the list
  1382. * Returns: 0 on success, -1 on failure
  1383. *
  1384. * This function is used to add a network block to the priority list of
  1385. * networks. This must be called for each network when reading in the full
  1386. * configuration. In addition, this can be used indirectly when updating
  1387. * priorities by calling wpa_config_update_prio_list().
  1388. */
  1389. int wpa_config_add_prio_network(struct wpa_config *config,
  1390. struct wpa_ssid *ssid)
  1391. {
  1392. int prio;
  1393. struct wpa_ssid *prev, **nlist;
  1394. /*
  1395. * Add to an existing priority list if one is available for the
  1396. * configured priority level for this network.
  1397. */
  1398. for (prio = 0; prio < config->num_prio; prio++) {
  1399. prev = config->pssid[prio];
  1400. if (prev->priority == ssid->priority) {
  1401. while (prev->pnext)
  1402. prev = prev->pnext;
  1403. prev->pnext = ssid;
  1404. return 0;
  1405. }
  1406. }
  1407. /* First network for this priority - add a new priority list */
  1408. nlist = os_realloc(config->pssid,
  1409. (config->num_prio + 1) * sizeof(struct wpa_ssid *));
  1410. if (nlist == NULL)
  1411. return -1;
  1412. for (prio = 0; prio < config->num_prio; prio++) {
  1413. if (nlist[prio]->priority < ssid->priority) {
  1414. os_memmove(&nlist[prio + 1], &nlist[prio],
  1415. (config->num_prio - prio) *
  1416. sizeof(struct wpa_ssid *));
  1417. break;
  1418. }
  1419. }
  1420. nlist[prio] = ssid;
  1421. config->num_prio++;
  1422. config->pssid = nlist;
  1423. return 0;
  1424. }
  1425. /**
  1426. * wpa_config_update_prio_list - Update network priority list
  1427. * @config: Configuration data from wpa_config_read()
  1428. * Returns: 0 on success, -1 on failure
  1429. *
  1430. * This function is called to update the priority list of networks in the
  1431. * configuration when a network is being added or removed. This is also called
  1432. * if a priority for a network is changed.
  1433. */
  1434. int wpa_config_update_prio_list(struct wpa_config *config)
  1435. {
  1436. struct wpa_ssid *ssid;
  1437. int ret = 0;
  1438. os_free(config->pssid);
  1439. config->pssid = NULL;
  1440. config->num_prio = 0;
  1441. ssid = config->ssid;
  1442. while (ssid) {
  1443. ssid->pnext = NULL;
  1444. if (wpa_config_add_prio_network(config, ssid) < 0)
  1445. ret = -1;
  1446. ssid = ssid->next;
  1447. }
  1448. return ret;
  1449. }
  1450. #ifdef IEEE8021X_EAPOL
  1451. static void eap_peer_config_free(struct eap_peer_config *eap)
  1452. {
  1453. os_free(eap->eap_methods);
  1454. os_free(eap->identity);
  1455. os_free(eap->anonymous_identity);
  1456. os_free(eap->password);
  1457. os_free(eap->ca_cert);
  1458. os_free(eap->ca_path);
  1459. os_free(eap->client_cert);
  1460. os_free(eap->private_key);
  1461. os_free(eap->private_key_passwd);
  1462. os_free(eap->dh_file);
  1463. os_free(eap->subject_match);
  1464. os_free(eap->altsubject_match);
  1465. os_free(eap->ca_cert2);
  1466. os_free(eap->ca_path2);
  1467. os_free(eap->client_cert2);
  1468. os_free(eap->private_key2);
  1469. os_free(eap->private_key2_passwd);
  1470. os_free(eap->dh_file2);
  1471. os_free(eap->subject_match2);
  1472. os_free(eap->altsubject_match2);
  1473. os_free(eap->phase1);
  1474. os_free(eap->phase2);
  1475. os_free(eap->pcsc);
  1476. os_free(eap->pin);
  1477. os_free(eap->engine_id);
  1478. os_free(eap->key_id);
  1479. os_free(eap->cert_id);
  1480. os_free(eap->ca_cert_id);
  1481. os_free(eap->key2_id);
  1482. os_free(eap->cert2_id);
  1483. os_free(eap->ca_cert2_id);
  1484. os_free(eap->pin2);
  1485. os_free(eap->engine2_id);
  1486. os_free(eap->otp);
  1487. os_free(eap->pending_req_otp);
  1488. os_free(eap->pac_file);
  1489. os_free(eap->new_password);
  1490. }
  1491. #endif /* IEEE8021X_EAPOL */
  1492. /**
  1493. * wpa_config_free_ssid - Free network/ssid configuration data
  1494. * @ssid: Configuration data for the network
  1495. *
  1496. * This function frees all resources allocated for the network configuration
  1497. * data.
  1498. */
  1499. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1500. {
  1501. os_free(ssid->ssid);
  1502. os_free(ssid->passphrase);
  1503. #ifdef IEEE8021X_EAPOL
  1504. eap_peer_config_free(&ssid->eap);
  1505. #endif /* IEEE8021X_EAPOL */
  1506. os_free(ssid->id_str);
  1507. os_free(ssid->scan_freq);
  1508. os_free(ssid->freq_list);
  1509. os_free(ssid->bgscan);
  1510. os_free(ssid->p2p_client_list);
  1511. #ifdef CONFIG_HT_OVERRIDES
  1512. os_free(ssid->ht_mcs);
  1513. #endif /* CONFIG_HT_OVERRIDES */
  1514. os_free(ssid);
  1515. }
  1516. void wpa_config_free_cred(struct wpa_cred *cred)
  1517. {
  1518. os_free(cred->realm);
  1519. os_free(cred->username);
  1520. os_free(cred->password);
  1521. os_free(cred->ca_cert);
  1522. os_free(cred->client_cert);
  1523. os_free(cred->private_key);
  1524. os_free(cred->private_key_passwd);
  1525. os_free(cred->imsi);
  1526. os_free(cred->milenage);
  1527. os_free(cred->domain);
  1528. os_free(cred);
  1529. }
  1530. /**
  1531. * wpa_config_free - Free configuration data
  1532. * @config: Configuration data from wpa_config_read()
  1533. *
  1534. * This function frees all resources allocated for the configuration data by
  1535. * wpa_config_read().
  1536. */
  1537. void wpa_config_free(struct wpa_config *config)
  1538. {
  1539. #ifndef CONFIG_NO_CONFIG_BLOBS
  1540. struct wpa_config_blob *blob, *prevblob;
  1541. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1542. struct wpa_ssid *ssid, *prev = NULL;
  1543. struct wpa_cred *cred, *cprev;
  1544. ssid = config->ssid;
  1545. while (ssid) {
  1546. prev = ssid;
  1547. ssid = ssid->next;
  1548. wpa_config_free_ssid(prev);
  1549. }
  1550. cred = config->cred;
  1551. while (cred) {
  1552. cprev = cred;
  1553. cred = cred->next;
  1554. wpa_config_free_cred(cprev);
  1555. }
  1556. #ifndef CONFIG_NO_CONFIG_BLOBS
  1557. blob = config->blobs;
  1558. prevblob = NULL;
  1559. while (blob) {
  1560. prevblob = blob;
  1561. blob = blob->next;
  1562. wpa_config_free_blob(prevblob);
  1563. }
  1564. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1565. os_free(config->ctrl_interface);
  1566. os_free(config->ctrl_interface_group);
  1567. os_free(config->opensc_engine_path);
  1568. os_free(config->pkcs11_engine_path);
  1569. os_free(config->pkcs11_module_path);
  1570. os_free(config->pcsc_reader);
  1571. os_free(config->pcsc_pin);
  1572. os_free(config->driver_param);
  1573. os_free(config->device_name);
  1574. os_free(config->manufacturer);
  1575. os_free(config->model_name);
  1576. os_free(config->model_number);
  1577. os_free(config->serial_number);
  1578. os_free(config->config_methods);
  1579. os_free(config->p2p_ssid_postfix);
  1580. os_free(config->pssid);
  1581. os_free(config);
  1582. }
  1583. /**
  1584. * wpa_config_foreach_network - Iterate over each configured network
  1585. * @config: Configuration data from wpa_config_read()
  1586. * @func: Callback function to process each network
  1587. * @arg: Opaque argument to pass to callback function
  1588. *
  1589. * Iterate over the set of configured networks calling the specified
  1590. * function for each item. We guard against callbacks removing the
  1591. * supplied network.
  1592. */
  1593. void wpa_config_foreach_network(struct wpa_config *config,
  1594. void (*func)(void *, struct wpa_ssid *),
  1595. void *arg)
  1596. {
  1597. struct wpa_ssid *ssid, *next;
  1598. ssid = config->ssid;
  1599. while (ssid) {
  1600. next = ssid->next;
  1601. func(arg, ssid);
  1602. ssid = next;
  1603. }
  1604. }
  1605. /**
  1606. * wpa_config_get_network - Get configured network based on id
  1607. * @config: Configuration data from wpa_config_read()
  1608. * @id: Unique network id to search for
  1609. * Returns: Network configuration or %NULL if not found
  1610. */
  1611. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1612. {
  1613. struct wpa_ssid *ssid;
  1614. ssid = config->ssid;
  1615. while (ssid) {
  1616. if (id == ssid->id)
  1617. break;
  1618. ssid = ssid->next;
  1619. }
  1620. return ssid;
  1621. }
  1622. /**
  1623. * wpa_config_add_network - Add a new network with empty configuration
  1624. * @config: Configuration data from wpa_config_read()
  1625. * Returns: The new network configuration or %NULL if operation failed
  1626. */
  1627. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1628. {
  1629. int id;
  1630. struct wpa_ssid *ssid, *last = NULL;
  1631. id = -1;
  1632. ssid = config->ssid;
  1633. while (ssid) {
  1634. if (ssid->id > id)
  1635. id = ssid->id;
  1636. last = ssid;
  1637. ssid = ssid->next;
  1638. }
  1639. id++;
  1640. ssid = os_zalloc(sizeof(*ssid));
  1641. if (ssid == NULL)
  1642. return NULL;
  1643. ssid->id = id;
  1644. if (last)
  1645. last->next = ssid;
  1646. else
  1647. config->ssid = ssid;
  1648. wpa_config_update_prio_list(config);
  1649. return ssid;
  1650. }
  1651. /**
  1652. * wpa_config_remove_network - Remove a configured network based on id
  1653. * @config: Configuration data from wpa_config_read()
  1654. * @id: Unique network id to search for
  1655. * Returns: 0 on success, or -1 if the network was not found
  1656. */
  1657. int wpa_config_remove_network(struct wpa_config *config, int id)
  1658. {
  1659. struct wpa_ssid *ssid, *prev = NULL;
  1660. ssid = config->ssid;
  1661. while (ssid) {
  1662. if (id == ssid->id)
  1663. break;
  1664. prev = ssid;
  1665. ssid = ssid->next;
  1666. }
  1667. if (ssid == NULL)
  1668. return -1;
  1669. if (prev)
  1670. prev->next = ssid->next;
  1671. else
  1672. config->ssid = ssid->next;
  1673. wpa_config_update_prio_list(config);
  1674. wpa_config_free_ssid(ssid);
  1675. return 0;
  1676. }
  1677. /**
  1678. * wpa_config_set_network_defaults - Set network default values
  1679. * @ssid: Pointer to network configuration data
  1680. */
  1681. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1682. {
  1683. ssid->proto = DEFAULT_PROTO;
  1684. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1685. ssid->group_cipher = DEFAULT_GROUP;
  1686. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1687. #ifdef IEEE8021X_EAPOL
  1688. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1689. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1690. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1691. #endif /* IEEE8021X_EAPOL */
  1692. #ifdef CONFIG_HT_OVERRIDES
  1693. ssid->disable_ht = DEFAULT_DISABLE_HT;
  1694. ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
  1695. ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
  1696. ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
  1697. ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
  1698. #endif /* CONFIG_HT_OVERRIDES */
  1699. }
  1700. /**
  1701. * wpa_config_set - Set a variable in network configuration
  1702. * @ssid: Pointer to network configuration data
  1703. * @var: Variable name, e.g., "ssid"
  1704. * @value: Variable value
  1705. * @line: Line number in configuration file or 0 if not used
  1706. * Returns: 0 on success, -1 on failure
  1707. *
  1708. * This function can be used to set network configuration variables based on
  1709. * both the configuration file and management interface input. The value
  1710. * parameter must be in the same format as the text-based configuration file is
  1711. * using. For example, strings are using double quotation marks.
  1712. */
  1713. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1714. int line)
  1715. {
  1716. size_t i;
  1717. int ret = 0;
  1718. if (ssid == NULL || var == NULL || value == NULL)
  1719. return -1;
  1720. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1721. const struct parse_data *field = &ssid_fields[i];
  1722. if (os_strcmp(var, field->name) != 0)
  1723. continue;
  1724. if (field->parser(field, ssid, line, value)) {
  1725. if (line) {
  1726. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1727. "parse %s '%s'.", line, var, value);
  1728. }
  1729. ret = -1;
  1730. }
  1731. break;
  1732. }
  1733. if (i == NUM_SSID_FIELDS) {
  1734. if (line) {
  1735. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1736. "'%s'.", line, var);
  1737. }
  1738. ret = -1;
  1739. }
  1740. return ret;
  1741. }
  1742. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  1743. const char *value)
  1744. {
  1745. size_t len;
  1746. char *buf;
  1747. int ret;
  1748. len = os_strlen(value);
  1749. buf = os_malloc(len + 3);
  1750. if (buf == NULL)
  1751. return -1;
  1752. buf[0] = '"';
  1753. os_memcpy(buf + 1, value, len);
  1754. buf[len + 1] = '"';
  1755. buf[len + 2] = '\0';
  1756. ret = wpa_config_set(ssid, var, buf, 0);
  1757. os_free(buf);
  1758. return ret;
  1759. }
  1760. /**
  1761. * wpa_config_get_all - Get all options from network configuration
  1762. * @ssid: Pointer to network configuration data
  1763. * @get_keys: Determines if keys/passwords will be included in returned list
  1764. * (if they may be exported)
  1765. * Returns: %NULL terminated list of all set keys and their values in the form
  1766. * of [key1, val1, key2, val2, ... , NULL]
  1767. *
  1768. * This function can be used to get list of all configured network properties.
  1769. * The caller is responsible for freeing the returned list and all its
  1770. * elements.
  1771. */
  1772. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  1773. {
  1774. const struct parse_data *field;
  1775. char *key, *value;
  1776. size_t i;
  1777. char **props;
  1778. int fields_num;
  1779. get_keys = get_keys && ssid->export_keys;
  1780. props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
  1781. if (!props)
  1782. return NULL;
  1783. fields_num = 0;
  1784. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1785. field = &ssid_fields[i];
  1786. if (field->key_data && !get_keys)
  1787. continue;
  1788. value = field->writer(field, ssid);
  1789. if (value == NULL)
  1790. continue;
  1791. if (os_strlen(value) == 0) {
  1792. os_free(value);
  1793. continue;
  1794. }
  1795. key = os_strdup(field->name);
  1796. if (key == NULL) {
  1797. os_free(value);
  1798. goto err;
  1799. }
  1800. props[fields_num * 2] = key;
  1801. props[fields_num * 2 + 1] = value;
  1802. fields_num++;
  1803. }
  1804. return props;
  1805. err:
  1806. value = *props;
  1807. while (value)
  1808. os_free(value++);
  1809. os_free(props);
  1810. return NULL;
  1811. }
  1812. #ifndef NO_CONFIG_WRITE
  1813. /**
  1814. * wpa_config_get - Get a variable in network configuration
  1815. * @ssid: Pointer to network configuration data
  1816. * @var: Variable name, e.g., "ssid"
  1817. * Returns: Value of the variable or %NULL on failure
  1818. *
  1819. * This function can be used to get network configuration variables. The
  1820. * returned value is a copy of the configuration variable in text format, i.e,.
  1821. * the same format that the text-based configuration file and wpa_config_set()
  1822. * are using for the value. The caller is responsible for freeing the returned
  1823. * value.
  1824. */
  1825. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1826. {
  1827. size_t i;
  1828. if (ssid == NULL || var == NULL)
  1829. return NULL;
  1830. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1831. const struct parse_data *field = &ssid_fields[i];
  1832. if (os_strcmp(var, field->name) == 0)
  1833. return field->writer(field, ssid);
  1834. }
  1835. return NULL;
  1836. }
  1837. /**
  1838. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1839. * @ssid: Pointer to network configuration data
  1840. * @var: Variable name, e.g., "ssid"
  1841. * Returns: Value of the variable or %NULL on failure
  1842. *
  1843. * This function can be used to get network configuration variable like
  1844. * wpa_config_get(). The only difference is that this functions does not expose
  1845. * key/password material from the configuration. In case a key/password field
  1846. * is requested, the returned value is an empty string or %NULL if the variable
  1847. * is not set or "*" if the variable is set (regardless of its value). The
  1848. * returned value is a copy of the configuration variable in text format, i.e,.
  1849. * the same format that the text-based configuration file and wpa_config_set()
  1850. * are using for the value. The caller is responsible for freeing the returned
  1851. * value.
  1852. */
  1853. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1854. {
  1855. size_t i;
  1856. if (ssid == NULL || var == NULL)
  1857. return NULL;
  1858. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1859. const struct parse_data *field = &ssid_fields[i];
  1860. if (os_strcmp(var, field->name) == 0) {
  1861. char *res = field->writer(field, ssid);
  1862. if (field->key_data) {
  1863. if (res && res[0]) {
  1864. wpa_printf(MSG_DEBUG, "Do not allow "
  1865. "key_data field to be "
  1866. "exposed");
  1867. os_free(res);
  1868. return os_strdup("*");
  1869. }
  1870. os_free(res);
  1871. return NULL;
  1872. }
  1873. return res;
  1874. }
  1875. }
  1876. return NULL;
  1877. }
  1878. #endif /* NO_CONFIG_WRITE */
  1879. /**
  1880. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  1881. * @ssid: Pointer to network configuration data
  1882. *
  1883. * This function must be called to update WPA PSK when either SSID or the
  1884. * passphrase has changed for the network configuration.
  1885. */
  1886. void wpa_config_update_psk(struct wpa_ssid *ssid)
  1887. {
  1888. #ifndef CONFIG_NO_PBKDF2
  1889. pbkdf2_sha1(ssid->passphrase,
  1890. (char *) ssid->ssid, ssid->ssid_len, 4096,
  1891. ssid->psk, PMK_LEN);
  1892. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1893. ssid->psk, PMK_LEN);
  1894. ssid->psk_set = 1;
  1895. #endif /* CONFIG_NO_PBKDF2 */
  1896. }
  1897. int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
  1898. const char *value, int line)
  1899. {
  1900. char *val;
  1901. size_t len;
  1902. if (os_strcmp(var, "priority") == 0) {
  1903. cred->priority = atoi(value);
  1904. return 0;
  1905. }
  1906. if (os_strcmp(var, "pcsc") == 0) {
  1907. cred->pcsc = atoi(value);
  1908. return 0;
  1909. }
  1910. val = wpa_config_parse_string(value, &len);
  1911. if (val == NULL) {
  1912. wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
  1913. "value '%s'.", line, var, value);
  1914. return -1;
  1915. }
  1916. if (os_strcmp(var, "realm") == 0) {
  1917. os_free(cred->realm);
  1918. cred->realm = val;
  1919. return 0;
  1920. }
  1921. if (os_strcmp(var, "username") == 0) {
  1922. os_free(cred->username);
  1923. cred->username = val;
  1924. return 0;
  1925. }
  1926. if (os_strcmp(var, "password") == 0) {
  1927. os_free(cred->password);
  1928. cred->password = val;
  1929. return 0;
  1930. }
  1931. if (os_strcmp(var, "ca_cert") == 0) {
  1932. os_free(cred->ca_cert);
  1933. cred->ca_cert = val;
  1934. return 0;
  1935. }
  1936. if (os_strcmp(var, "client_cert") == 0) {
  1937. os_free(cred->client_cert);
  1938. cred->client_cert = val;
  1939. return 0;
  1940. }
  1941. if (os_strcmp(var, "private_key") == 0) {
  1942. os_free(cred->private_key);
  1943. cred->private_key = val;
  1944. return 0;
  1945. }
  1946. if (os_strcmp(var, "private_key_passwd") == 0) {
  1947. os_free(cred->private_key_passwd);
  1948. cred->private_key_passwd = val;
  1949. return 0;
  1950. }
  1951. if (os_strcmp(var, "imsi") == 0) {
  1952. os_free(cred->imsi);
  1953. cred->imsi = val;
  1954. return 0;
  1955. }
  1956. if (os_strcmp(var, "milenage") == 0) {
  1957. os_free(cred->milenage);
  1958. cred->milenage = val;
  1959. return 0;
  1960. }
  1961. if (os_strcmp(var, "domain") == 0) {
  1962. os_free(cred->domain);
  1963. cred->domain = val;
  1964. return 0;
  1965. }
  1966. if (line) {
  1967. wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
  1968. line, var);
  1969. }
  1970. os_free(val);
  1971. return -1;
  1972. }
  1973. struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
  1974. {
  1975. struct wpa_cred *cred;
  1976. cred = config->cred;
  1977. while (cred) {
  1978. if (id == cred->id)
  1979. break;
  1980. cred = cred->next;
  1981. }
  1982. return cred;
  1983. }
  1984. struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
  1985. {
  1986. int id;
  1987. struct wpa_cred *cred, *last = NULL;
  1988. id = -1;
  1989. cred = config->cred;
  1990. while (cred) {
  1991. if (cred->id > id)
  1992. id = cred->id;
  1993. last = cred;
  1994. cred = cred->next;
  1995. }
  1996. id++;
  1997. cred = os_zalloc(sizeof(*cred));
  1998. if (cred == NULL)
  1999. return NULL;
  2000. cred->id = id;
  2001. if (last)
  2002. last->next = cred;
  2003. else
  2004. config->cred = cred;
  2005. return cred;
  2006. }
  2007. int wpa_config_remove_cred(struct wpa_config *config, int id)
  2008. {
  2009. struct wpa_cred *cred, *prev = NULL;
  2010. cred = config->cred;
  2011. while (cred) {
  2012. if (id == cred->id)
  2013. break;
  2014. prev = cred;
  2015. cred = cred->next;
  2016. }
  2017. if (cred == NULL)
  2018. return -1;
  2019. if (prev)
  2020. prev->next = cred->next;
  2021. else
  2022. config->cred = cred->next;
  2023. wpa_config_free_cred(cred);
  2024. return 0;
  2025. }
  2026. #ifndef CONFIG_NO_CONFIG_BLOBS
  2027. /**
  2028. * wpa_config_get_blob - Get a named configuration blob
  2029. * @config: Configuration data from wpa_config_read()
  2030. * @name: Name of the blob
  2031. * Returns: Pointer to blob data or %NULL if not found
  2032. */
  2033. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  2034. const char *name)
  2035. {
  2036. struct wpa_config_blob *blob = config->blobs;
  2037. while (blob) {
  2038. if (os_strcmp(blob->name, name) == 0)
  2039. return blob;
  2040. blob = blob->next;
  2041. }
  2042. return NULL;
  2043. }
  2044. /**
  2045. * wpa_config_set_blob - Set or add a named configuration blob
  2046. * @config: Configuration data from wpa_config_read()
  2047. * @blob: New value for the blob
  2048. *
  2049. * Adds a new configuration blob or replaces the current value of an existing
  2050. * blob.
  2051. */
  2052. void wpa_config_set_blob(struct wpa_config *config,
  2053. struct wpa_config_blob *blob)
  2054. {
  2055. wpa_config_remove_blob(config, blob->name);
  2056. blob->next = config->blobs;
  2057. config->blobs = blob;
  2058. }
  2059. /**
  2060. * wpa_config_free_blob - Free blob data
  2061. * @blob: Pointer to blob to be freed
  2062. */
  2063. void wpa_config_free_blob(struct wpa_config_blob *blob)
  2064. {
  2065. if (blob) {
  2066. os_free(blob->name);
  2067. os_free(blob->data);
  2068. os_free(blob);
  2069. }
  2070. }
  2071. /**
  2072. * wpa_config_remove_blob - Remove a named configuration blob
  2073. * @config: Configuration data from wpa_config_read()
  2074. * @name: Name of the blob to remove
  2075. * Returns: 0 if blob was removed or -1 if blob was not found
  2076. */
  2077. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  2078. {
  2079. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  2080. while (pos) {
  2081. if (os_strcmp(pos->name, name) == 0) {
  2082. if (prev)
  2083. prev->next = pos->next;
  2084. else
  2085. config->blobs = pos->next;
  2086. wpa_config_free_blob(pos);
  2087. return 0;
  2088. }
  2089. prev = pos;
  2090. pos = pos->next;
  2091. }
  2092. return -1;
  2093. }
  2094. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2095. /**
  2096. * wpa_config_alloc_empty - Allocate an empty configuration
  2097. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  2098. * socket
  2099. * @driver_param: Driver parameters
  2100. * Returns: Pointer to allocated configuration data or %NULL on failure
  2101. */
  2102. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  2103. const char *driver_param)
  2104. {
  2105. struct wpa_config *config;
  2106. config = os_zalloc(sizeof(*config));
  2107. if (config == NULL)
  2108. return NULL;
  2109. config->eapol_version = DEFAULT_EAPOL_VERSION;
  2110. config->ap_scan = DEFAULT_AP_SCAN;
  2111. config->fast_reauth = DEFAULT_FAST_REAUTH;
  2112. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  2113. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  2114. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  2115. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  2116. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  2117. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  2118. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  2119. if (ctrl_interface)
  2120. config->ctrl_interface = os_strdup(ctrl_interface);
  2121. if (driver_param)
  2122. config->driver_param = os_strdup(driver_param);
  2123. return config;
  2124. }
  2125. #ifndef CONFIG_NO_STDOUT_DEBUG
  2126. /**
  2127. * wpa_config_debug_dump_networks - Debug dump of configured networks
  2128. * @config: Configuration data from wpa_config_read()
  2129. */
  2130. void wpa_config_debug_dump_networks(struct wpa_config *config)
  2131. {
  2132. int prio;
  2133. struct wpa_ssid *ssid;
  2134. for (prio = 0; prio < config->num_prio; prio++) {
  2135. ssid = config->pssid[prio];
  2136. wpa_printf(MSG_DEBUG, "Priority group %d",
  2137. ssid->priority);
  2138. while (ssid) {
  2139. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  2140. ssid->id,
  2141. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2142. ssid = ssid->pnext;
  2143. }
  2144. }
  2145. }
  2146. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2147. struct global_parse_data {
  2148. char *name;
  2149. int (*parser)(const struct global_parse_data *data,
  2150. struct wpa_config *config, int line, const char *value);
  2151. void *param1, *param2, *param3;
  2152. unsigned int changed_flag;
  2153. };
  2154. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  2155. struct wpa_config *config, int line,
  2156. const char *pos)
  2157. {
  2158. int *dst;
  2159. dst = (int *) (((u8 *) config) + (long) data->param1);
  2160. *dst = atoi(pos);
  2161. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  2162. if (data->param2 && *dst < (long) data->param2) {
  2163. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  2164. "min_value=%ld)", line, data->name, *dst,
  2165. (long) data->param2);
  2166. *dst = (long) data->param2;
  2167. return -1;
  2168. }
  2169. if (data->param3 && *dst > (long) data->param3) {
  2170. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  2171. "max_value=%ld)", line, data->name, *dst,
  2172. (long) data->param3);
  2173. *dst = (long) data->param3;
  2174. return -1;
  2175. }
  2176. return 0;
  2177. }
  2178. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  2179. struct wpa_config *config, int line,
  2180. const char *pos)
  2181. {
  2182. size_t len;
  2183. char **dst, *tmp;
  2184. len = os_strlen(pos);
  2185. if (data->param2 && len < (size_t) data->param2) {
  2186. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  2187. "min_len=%ld)", line, data->name,
  2188. (unsigned long) len, (long) data->param2);
  2189. return -1;
  2190. }
  2191. if (data->param3 && len > (size_t) data->param3) {
  2192. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  2193. "max_len=%ld)", line, data->name,
  2194. (unsigned long) len, (long) data->param3);
  2195. return -1;
  2196. }
  2197. tmp = os_strdup(pos);
  2198. if (tmp == NULL)
  2199. return -1;
  2200. dst = (char **) (((u8 *) config) + (long) data->param1);
  2201. os_free(*dst);
  2202. *dst = tmp;
  2203. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  2204. return 0;
  2205. }
  2206. static int wpa_config_process_country(const struct global_parse_data *data,
  2207. struct wpa_config *config, int line,
  2208. const char *pos)
  2209. {
  2210. if (!pos[0] || !pos[1]) {
  2211. wpa_printf(MSG_DEBUG, "Invalid country set");
  2212. return -1;
  2213. }
  2214. config->country[0] = pos[0];
  2215. config->country[1] = pos[1];
  2216. wpa_printf(MSG_DEBUG, "country='%c%c'",
  2217. config->country[0], config->country[1]);
  2218. return 0;
  2219. }
  2220. static int wpa_config_process_load_dynamic_eap(
  2221. const struct global_parse_data *data, struct wpa_config *config,
  2222. int line, const char *so)
  2223. {
  2224. int ret;
  2225. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  2226. ret = eap_peer_method_load(so);
  2227. if (ret == -2) {
  2228. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  2229. "reloading.");
  2230. } else if (ret) {
  2231. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  2232. "method '%s'.", line, so);
  2233. return -1;
  2234. }
  2235. return 0;
  2236. }
  2237. #ifdef CONFIG_WPS
  2238. static int wpa_config_process_uuid(const struct global_parse_data *data,
  2239. struct wpa_config *config, int line,
  2240. const char *pos)
  2241. {
  2242. char buf[40];
  2243. if (uuid_str2bin(pos, config->uuid)) {
  2244. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  2245. return -1;
  2246. }
  2247. uuid_bin2str(config->uuid, buf, sizeof(buf));
  2248. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  2249. return 0;
  2250. }
  2251. static int wpa_config_process_device_type(
  2252. const struct global_parse_data *data,
  2253. struct wpa_config *config, int line, const char *pos)
  2254. {
  2255. return wps_dev_type_str2bin(pos, config->device_type);
  2256. }
  2257. static int wpa_config_process_os_version(const struct global_parse_data *data,
  2258. struct wpa_config *config, int line,
  2259. const char *pos)
  2260. {
  2261. if (hexstr2bin(pos, config->os_version, 4)) {
  2262. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  2263. return -1;
  2264. }
  2265. wpa_printf(MSG_DEBUG, "os_version=%08x",
  2266. WPA_GET_BE32(config->os_version));
  2267. return 0;
  2268. }
  2269. #endif /* CONFIG_WPS */
  2270. #ifdef CONFIG_P2P
  2271. static int wpa_config_process_sec_device_type(
  2272. const struct global_parse_data *data,
  2273. struct wpa_config *config, int line, const char *pos)
  2274. {
  2275. int idx;
  2276. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  2277. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  2278. "items", line);
  2279. return -1;
  2280. }
  2281. idx = config->num_sec_device_types;
  2282. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  2283. return -1;
  2284. config->num_sec_device_types++;
  2285. return 0;
  2286. }
  2287. #endif /* CONFIG_P2P */
  2288. static int wpa_config_process_hessid(
  2289. const struct global_parse_data *data,
  2290. struct wpa_config *config, int line, const char *pos)
  2291. {
  2292. if (hwaddr_aton2(pos, config->hessid) < 0) {
  2293. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  2294. line, pos);
  2295. return -1;
  2296. }
  2297. return 0;
  2298. }
  2299. #ifdef OFFSET
  2300. #undef OFFSET
  2301. #endif /* OFFSET */
  2302. /* OFFSET: Get offset of a variable within the wpa_config structure */
  2303. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  2304. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  2305. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  2306. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  2307. #define INT(f) _INT(f), NULL, NULL
  2308. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  2309. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  2310. #define STR(f) _STR(f), NULL, NULL
  2311. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  2312. static const struct global_parse_data global_fields[] = {
  2313. #ifdef CONFIG_CTRL_IFACE
  2314. { STR(ctrl_interface), 0 },
  2315. { STR(ctrl_interface_group), 0 } /* deprecated */,
  2316. #endif /* CONFIG_CTRL_IFACE */
  2317. { INT_RANGE(eapol_version, 1, 2), 0 },
  2318. { INT(ap_scan), 0 },
  2319. { INT(fast_reauth), 0 },
  2320. { STR(opensc_engine_path), 0 },
  2321. { STR(pkcs11_engine_path), 0 },
  2322. { STR(pkcs11_module_path), 0 },
  2323. { STR(pcsc_reader), 0 },
  2324. { STR(pcsc_pin), 0 },
  2325. { STR(driver_param), 0 },
  2326. { INT(dot11RSNAConfigPMKLifetime), 0 },
  2327. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  2328. { INT(dot11RSNAConfigSATimeout), 0 },
  2329. #ifndef CONFIG_NO_CONFIG_WRITE
  2330. { INT(update_config), 0 },
  2331. #endif /* CONFIG_NO_CONFIG_WRITE */
  2332. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  2333. #ifdef CONFIG_WPS
  2334. { FUNC(uuid), CFG_CHANGED_UUID },
  2335. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  2336. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  2337. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  2338. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2339. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2340. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  2341. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  2342. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  2343. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  2344. #endif /* CONFIG_WPS */
  2345. #ifdef CONFIG_P2P
  2346. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  2347. { INT(p2p_listen_reg_class), 0 },
  2348. { INT(p2p_listen_channel), 0 },
  2349. { INT(p2p_oper_reg_class), 0 },
  2350. { INT(p2p_oper_channel), 0 },
  2351. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  2352. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  2353. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  2354. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  2355. { INT(p2p_group_idle), 0 },
  2356. #endif /* CONFIG_P2P */
  2357. { FUNC(country), CFG_CHANGED_COUNTRY },
  2358. { INT(bss_max_count), 0 },
  2359. { INT(bss_expiration_age), 0 },
  2360. { INT(bss_expiration_scan_count), 0 },
  2361. { INT_RANGE(filter_ssids, 0, 1), 0 },
  2362. { INT(max_num_sta), 0 },
  2363. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  2364. { INT_RANGE(interworking, 0, 1), 0 },
  2365. { FUNC(hessid), 0 },
  2366. { INT_RANGE(access_network_type, 0, 15), 0 }
  2367. };
  2368. #undef FUNC
  2369. #undef _INT
  2370. #undef INT
  2371. #undef INT_RANGE
  2372. #undef _STR
  2373. #undef STR
  2374. #undef STR_RANGE
  2375. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  2376. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  2377. {
  2378. size_t i;
  2379. int ret = 0;
  2380. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  2381. const struct global_parse_data *field = &global_fields[i];
  2382. size_t flen = os_strlen(field->name);
  2383. if (os_strncmp(pos, field->name, flen) != 0 ||
  2384. pos[flen] != '=')
  2385. continue;
  2386. if (field->parser(field, config, line, pos + flen + 1)) {
  2387. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2388. "parse '%s'.", line, pos);
  2389. ret = -1;
  2390. }
  2391. config->changed_parameters |= field->changed_flag;
  2392. break;
  2393. }
  2394. if (i == NUM_GLOBAL_FIELDS) {
  2395. if (line < 0)
  2396. return -1;
  2397. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  2398. line, pos);
  2399. ret = -1;
  2400. }
  2401. return ret;
  2402. }