config.c 68 KB

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