config.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000
  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->eap_method);
  1538. os_free(cred->phase1);
  1539. os_free(cred->phase2);
  1540. os_free(cred);
  1541. }
  1542. /**
  1543. * wpa_config_free - Free configuration data
  1544. * @config: Configuration data from wpa_config_read()
  1545. *
  1546. * This function frees all resources allocated for the configuration data by
  1547. * wpa_config_read().
  1548. */
  1549. void wpa_config_free(struct wpa_config *config)
  1550. {
  1551. #ifndef CONFIG_NO_CONFIG_BLOBS
  1552. struct wpa_config_blob *blob, *prevblob;
  1553. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1554. struct wpa_ssid *ssid, *prev = NULL;
  1555. struct wpa_cred *cred, *cprev;
  1556. ssid = config->ssid;
  1557. while (ssid) {
  1558. prev = ssid;
  1559. ssid = ssid->next;
  1560. wpa_config_free_ssid(prev);
  1561. }
  1562. cred = config->cred;
  1563. while (cred) {
  1564. cprev = cred;
  1565. cred = cred->next;
  1566. wpa_config_free_cred(cprev);
  1567. }
  1568. #ifndef CONFIG_NO_CONFIG_BLOBS
  1569. blob = config->blobs;
  1570. prevblob = NULL;
  1571. while (blob) {
  1572. prevblob = blob;
  1573. blob = blob->next;
  1574. wpa_config_free_blob(prevblob);
  1575. }
  1576. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1577. wpabuf_free(config->wps_vendor_ext_m1);
  1578. os_free(config->ctrl_interface);
  1579. os_free(config->ctrl_interface_group);
  1580. os_free(config->opensc_engine_path);
  1581. os_free(config->pkcs11_engine_path);
  1582. os_free(config->pkcs11_module_path);
  1583. os_free(config->pcsc_reader);
  1584. os_free(config->pcsc_pin);
  1585. os_free(config->driver_param);
  1586. os_free(config->device_name);
  1587. os_free(config->manufacturer);
  1588. os_free(config->model_name);
  1589. os_free(config->model_number);
  1590. os_free(config->serial_number);
  1591. os_free(config->config_methods);
  1592. os_free(config->p2p_ssid_postfix);
  1593. os_free(config->pssid);
  1594. os_free(config->p2p_pref_chan);
  1595. os_free(config->autoscan);
  1596. wpabuf_free(config->wps_nfc_dh_pubkey);
  1597. wpabuf_free(config->wps_nfc_dh_privkey);
  1598. wpabuf_free(config->wps_nfc_dev_pw);
  1599. os_free(config);
  1600. }
  1601. /**
  1602. * wpa_config_foreach_network - Iterate over each configured network
  1603. * @config: Configuration data from wpa_config_read()
  1604. * @func: Callback function to process each network
  1605. * @arg: Opaque argument to pass to callback function
  1606. *
  1607. * Iterate over the set of configured networks calling the specified
  1608. * function for each item. We guard against callbacks removing the
  1609. * supplied network.
  1610. */
  1611. void wpa_config_foreach_network(struct wpa_config *config,
  1612. void (*func)(void *, struct wpa_ssid *),
  1613. void *arg)
  1614. {
  1615. struct wpa_ssid *ssid, *next;
  1616. ssid = config->ssid;
  1617. while (ssid) {
  1618. next = ssid->next;
  1619. func(arg, ssid);
  1620. ssid = next;
  1621. }
  1622. }
  1623. /**
  1624. * wpa_config_get_network - Get configured network based on id
  1625. * @config: Configuration data from wpa_config_read()
  1626. * @id: Unique network id to search for
  1627. * Returns: Network configuration or %NULL if not found
  1628. */
  1629. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1630. {
  1631. struct wpa_ssid *ssid;
  1632. ssid = config->ssid;
  1633. while (ssid) {
  1634. if (id == ssid->id)
  1635. break;
  1636. ssid = ssid->next;
  1637. }
  1638. return ssid;
  1639. }
  1640. /**
  1641. * wpa_config_add_network - Add a new network with empty configuration
  1642. * @config: Configuration data from wpa_config_read()
  1643. * Returns: The new network configuration or %NULL if operation failed
  1644. */
  1645. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1646. {
  1647. int id;
  1648. struct wpa_ssid *ssid, *last = NULL;
  1649. id = -1;
  1650. ssid = config->ssid;
  1651. while (ssid) {
  1652. if (ssid->id > id)
  1653. id = ssid->id;
  1654. last = ssid;
  1655. ssid = ssid->next;
  1656. }
  1657. id++;
  1658. ssid = os_zalloc(sizeof(*ssid));
  1659. if (ssid == NULL)
  1660. return NULL;
  1661. ssid->id = id;
  1662. if (last)
  1663. last->next = ssid;
  1664. else
  1665. config->ssid = ssid;
  1666. wpa_config_update_prio_list(config);
  1667. return ssid;
  1668. }
  1669. /**
  1670. * wpa_config_remove_network - Remove a configured network based on id
  1671. * @config: Configuration data from wpa_config_read()
  1672. * @id: Unique network id to search for
  1673. * Returns: 0 on success, or -1 if the network was not found
  1674. */
  1675. int wpa_config_remove_network(struct wpa_config *config, int id)
  1676. {
  1677. struct wpa_ssid *ssid, *prev = NULL;
  1678. ssid = config->ssid;
  1679. while (ssid) {
  1680. if (id == ssid->id)
  1681. break;
  1682. prev = ssid;
  1683. ssid = ssid->next;
  1684. }
  1685. if (ssid == NULL)
  1686. return -1;
  1687. if (prev)
  1688. prev->next = ssid->next;
  1689. else
  1690. config->ssid = ssid->next;
  1691. wpa_config_update_prio_list(config);
  1692. wpa_config_free_ssid(ssid);
  1693. return 0;
  1694. }
  1695. /**
  1696. * wpa_config_set_network_defaults - Set network default values
  1697. * @ssid: Pointer to network configuration data
  1698. */
  1699. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1700. {
  1701. ssid->proto = DEFAULT_PROTO;
  1702. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1703. ssid->group_cipher = DEFAULT_GROUP;
  1704. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1705. ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
  1706. #ifdef IEEE8021X_EAPOL
  1707. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1708. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1709. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1710. #endif /* IEEE8021X_EAPOL */
  1711. #ifdef CONFIG_HT_OVERRIDES
  1712. ssid->disable_ht = DEFAULT_DISABLE_HT;
  1713. ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
  1714. ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
  1715. ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
  1716. ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
  1717. #endif /* CONFIG_HT_OVERRIDES */
  1718. }
  1719. /**
  1720. * wpa_config_set - Set a variable in network configuration
  1721. * @ssid: Pointer to network configuration data
  1722. * @var: Variable name, e.g., "ssid"
  1723. * @value: Variable value
  1724. * @line: Line number in configuration file or 0 if not used
  1725. * Returns: 0 on success, -1 on failure
  1726. *
  1727. * This function can be used to set network configuration variables based on
  1728. * both the configuration file and management interface input. The value
  1729. * parameter must be in the same format as the text-based configuration file is
  1730. * using. For example, strings are using double quotation marks.
  1731. */
  1732. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1733. int line)
  1734. {
  1735. size_t i;
  1736. int ret = 0;
  1737. if (ssid == NULL || var == NULL || value == NULL)
  1738. return -1;
  1739. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1740. const struct parse_data *field = &ssid_fields[i];
  1741. if (os_strcmp(var, field->name) != 0)
  1742. continue;
  1743. if (field->parser(field, ssid, line, value)) {
  1744. if (line) {
  1745. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1746. "parse %s '%s'.", line, var, value);
  1747. }
  1748. ret = -1;
  1749. }
  1750. break;
  1751. }
  1752. if (i == NUM_SSID_FIELDS) {
  1753. if (line) {
  1754. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1755. "'%s'.", line, var);
  1756. }
  1757. ret = -1;
  1758. }
  1759. return ret;
  1760. }
  1761. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  1762. const char *value)
  1763. {
  1764. size_t len;
  1765. char *buf;
  1766. int ret;
  1767. len = os_strlen(value);
  1768. buf = os_malloc(len + 3);
  1769. if (buf == NULL)
  1770. return -1;
  1771. buf[0] = '"';
  1772. os_memcpy(buf + 1, value, len);
  1773. buf[len + 1] = '"';
  1774. buf[len + 2] = '\0';
  1775. ret = wpa_config_set(ssid, var, buf, 0);
  1776. os_free(buf);
  1777. return ret;
  1778. }
  1779. /**
  1780. * wpa_config_get_all - Get all options from network configuration
  1781. * @ssid: Pointer to network configuration data
  1782. * @get_keys: Determines if keys/passwords will be included in returned list
  1783. * (if they may be exported)
  1784. * Returns: %NULL terminated list of all set keys and their values in the form
  1785. * of [key1, val1, key2, val2, ... , NULL]
  1786. *
  1787. * This function can be used to get list of all configured network properties.
  1788. * The caller is responsible for freeing the returned list and all its
  1789. * elements.
  1790. */
  1791. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  1792. {
  1793. const struct parse_data *field;
  1794. char *key, *value;
  1795. size_t i;
  1796. char **props;
  1797. int fields_num;
  1798. get_keys = get_keys && ssid->export_keys;
  1799. props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
  1800. if (!props)
  1801. return NULL;
  1802. fields_num = 0;
  1803. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1804. field = &ssid_fields[i];
  1805. if (field->key_data && !get_keys)
  1806. continue;
  1807. value = field->writer(field, ssid);
  1808. if (value == NULL)
  1809. continue;
  1810. if (os_strlen(value) == 0) {
  1811. os_free(value);
  1812. continue;
  1813. }
  1814. key = os_strdup(field->name);
  1815. if (key == NULL) {
  1816. os_free(value);
  1817. goto err;
  1818. }
  1819. props[fields_num * 2] = key;
  1820. props[fields_num * 2 + 1] = value;
  1821. fields_num++;
  1822. }
  1823. return props;
  1824. err:
  1825. value = *props;
  1826. while (value)
  1827. os_free(value++);
  1828. os_free(props);
  1829. return NULL;
  1830. }
  1831. #ifndef NO_CONFIG_WRITE
  1832. /**
  1833. * wpa_config_get - Get a variable in network configuration
  1834. * @ssid: Pointer to network configuration data
  1835. * @var: Variable name, e.g., "ssid"
  1836. * Returns: Value of the variable or %NULL on failure
  1837. *
  1838. * This function can be used to get network configuration variables. The
  1839. * returned value is a copy of the configuration variable in text format, i.e,.
  1840. * the same format that the text-based configuration file and wpa_config_set()
  1841. * are using for the value. The caller is responsible for freeing the returned
  1842. * value.
  1843. */
  1844. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1845. {
  1846. size_t i;
  1847. if (ssid == NULL || var == NULL)
  1848. return NULL;
  1849. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1850. const struct parse_data *field = &ssid_fields[i];
  1851. if (os_strcmp(var, field->name) == 0)
  1852. return field->writer(field, ssid);
  1853. }
  1854. return NULL;
  1855. }
  1856. /**
  1857. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1858. * @ssid: Pointer to network configuration data
  1859. * @var: Variable name, e.g., "ssid"
  1860. * Returns: Value of the variable or %NULL on failure
  1861. *
  1862. * This function can be used to get network configuration variable like
  1863. * wpa_config_get(). The only difference is that this functions does not expose
  1864. * key/password material from the configuration. In case a key/password field
  1865. * is requested, the returned value is an empty string or %NULL if the variable
  1866. * is not set or "*" if the variable is set (regardless of its value). The
  1867. * returned value is a copy of the configuration variable in text format, i.e,.
  1868. * the same format that the text-based configuration file and wpa_config_set()
  1869. * are using for the value. The caller is responsible for freeing the returned
  1870. * value.
  1871. */
  1872. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1873. {
  1874. size_t i;
  1875. if (ssid == NULL || var == NULL)
  1876. return NULL;
  1877. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1878. const struct parse_data *field = &ssid_fields[i];
  1879. if (os_strcmp(var, field->name) == 0) {
  1880. char *res = field->writer(field, ssid);
  1881. if (field->key_data) {
  1882. if (res && res[0]) {
  1883. wpa_printf(MSG_DEBUG, "Do not allow "
  1884. "key_data field to be "
  1885. "exposed");
  1886. os_free(res);
  1887. return os_strdup("*");
  1888. }
  1889. os_free(res);
  1890. return NULL;
  1891. }
  1892. return res;
  1893. }
  1894. }
  1895. return NULL;
  1896. }
  1897. #endif /* NO_CONFIG_WRITE */
  1898. /**
  1899. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  1900. * @ssid: Pointer to network configuration data
  1901. *
  1902. * This function must be called to update WPA PSK when either SSID or the
  1903. * passphrase has changed for the network configuration.
  1904. */
  1905. void wpa_config_update_psk(struct wpa_ssid *ssid)
  1906. {
  1907. #ifndef CONFIG_NO_PBKDF2
  1908. pbkdf2_sha1(ssid->passphrase,
  1909. (char *) ssid->ssid, ssid->ssid_len, 4096,
  1910. ssid->psk, PMK_LEN);
  1911. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1912. ssid->psk, PMK_LEN);
  1913. ssid->psk_set = 1;
  1914. #endif /* CONFIG_NO_PBKDF2 */
  1915. }
  1916. int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
  1917. const char *value, int line)
  1918. {
  1919. char *val;
  1920. size_t len;
  1921. if (os_strcmp(var, "priority") == 0) {
  1922. cred->priority = atoi(value);
  1923. return 0;
  1924. }
  1925. if (os_strcmp(var, "pcsc") == 0) {
  1926. cred->pcsc = atoi(value);
  1927. return 0;
  1928. }
  1929. if (os_strcmp(var, "eap") == 0) {
  1930. struct eap_method_type method;
  1931. method.method = eap_peer_get_type(value, &method.vendor);
  1932. if (method.vendor == EAP_VENDOR_IETF &&
  1933. method.method == EAP_TYPE_NONE) {
  1934. wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
  1935. "for a credential", line, value);
  1936. return -1;
  1937. }
  1938. os_free(cred->eap_method);
  1939. cred->eap_method = os_malloc(sizeof(*cred->eap_method));
  1940. if (cred->eap_method == NULL)
  1941. return -1;
  1942. os_memcpy(cred->eap_method, &method, sizeof(method));
  1943. return 0;
  1944. }
  1945. val = wpa_config_parse_string(value, &len);
  1946. if (val == NULL) {
  1947. wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
  1948. "value '%s'.", line, var, value);
  1949. return -1;
  1950. }
  1951. if (os_strcmp(var, "realm") == 0) {
  1952. os_free(cred->realm);
  1953. cred->realm = val;
  1954. return 0;
  1955. }
  1956. if (os_strcmp(var, "username") == 0) {
  1957. os_free(cred->username);
  1958. cred->username = val;
  1959. return 0;
  1960. }
  1961. if (os_strcmp(var, "password") == 0) {
  1962. os_free(cred->password);
  1963. cred->password = val;
  1964. return 0;
  1965. }
  1966. if (os_strcmp(var, "ca_cert") == 0) {
  1967. os_free(cred->ca_cert);
  1968. cred->ca_cert = val;
  1969. return 0;
  1970. }
  1971. if (os_strcmp(var, "client_cert") == 0) {
  1972. os_free(cred->client_cert);
  1973. cred->client_cert = val;
  1974. return 0;
  1975. }
  1976. if (os_strcmp(var, "private_key") == 0) {
  1977. os_free(cred->private_key);
  1978. cred->private_key = val;
  1979. return 0;
  1980. }
  1981. if (os_strcmp(var, "private_key_passwd") == 0) {
  1982. os_free(cred->private_key_passwd);
  1983. cred->private_key_passwd = val;
  1984. return 0;
  1985. }
  1986. if (os_strcmp(var, "imsi") == 0) {
  1987. os_free(cred->imsi);
  1988. cred->imsi = val;
  1989. return 0;
  1990. }
  1991. if (os_strcmp(var, "milenage") == 0) {
  1992. os_free(cred->milenage);
  1993. cred->milenage = val;
  1994. return 0;
  1995. }
  1996. if (os_strcmp(var, "domain") == 0) {
  1997. os_free(cred->domain);
  1998. cred->domain = val;
  1999. return 0;
  2000. }
  2001. if (os_strcmp(var, "phase1") == 0) {
  2002. os_free(cred->phase1);
  2003. cred->phase1 = val;
  2004. return 0;
  2005. }
  2006. if (os_strcmp(var, "phase2") == 0) {
  2007. os_free(cred->phase2);
  2008. cred->phase2 = val;
  2009. return 0;
  2010. }
  2011. if (line) {
  2012. wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
  2013. line, var);
  2014. }
  2015. os_free(val);
  2016. return -1;
  2017. }
  2018. struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
  2019. {
  2020. struct wpa_cred *cred;
  2021. cred = config->cred;
  2022. while (cred) {
  2023. if (id == cred->id)
  2024. break;
  2025. cred = cred->next;
  2026. }
  2027. return cred;
  2028. }
  2029. struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
  2030. {
  2031. int id;
  2032. struct wpa_cred *cred, *last = NULL;
  2033. id = -1;
  2034. cred = config->cred;
  2035. while (cred) {
  2036. if (cred->id > id)
  2037. id = cred->id;
  2038. last = cred;
  2039. cred = cred->next;
  2040. }
  2041. id++;
  2042. cred = os_zalloc(sizeof(*cred));
  2043. if (cred == NULL)
  2044. return NULL;
  2045. cred->id = id;
  2046. if (last)
  2047. last->next = cred;
  2048. else
  2049. config->cred = cred;
  2050. return cred;
  2051. }
  2052. int wpa_config_remove_cred(struct wpa_config *config, int id)
  2053. {
  2054. struct wpa_cred *cred, *prev = NULL;
  2055. cred = config->cred;
  2056. while (cred) {
  2057. if (id == cred->id)
  2058. break;
  2059. prev = cred;
  2060. cred = cred->next;
  2061. }
  2062. if (cred == NULL)
  2063. return -1;
  2064. if (prev)
  2065. prev->next = cred->next;
  2066. else
  2067. config->cred = cred->next;
  2068. wpa_config_free_cred(cred);
  2069. return 0;
  2070. }
  2071. #ifndef CONFIG_NO_CONFIG_BLOBS
  2072. /**
  2073. * wpa_config_get_blob - Get a named configuration blob
  2074. * @config: Configuration data from wpa_config_read()
  2075. * @name: Name of the blob
  2076. * Returns: Pointer to blob data or %NULL if not found
  2077. */
  2078. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  2079. const char *name)
  2080. {
  2081. struct wpa_config_blob *blob = config->blobs;
  2082. while (blob) {
  2083. if (os_strcmp(blob->name, name) == 0)
  2084. return blob;
  2085. blob = blob->next;
  2086. }
  2087. return NULL;
  2088. }
  2089. /**
  2090. * wpa_config_set_blob - Set or add a named configuration blob
  2091. * @config: Configuration data from wpa_config_read()
  2092. * @blob: New value for the blob
  2093. *
  2094. * Adds a new configuration blob or replaces the current value of an existing
  2095. * blob.
  2096. */
  2097. void wpa_config_set_blob(struct wpa_config *config,
  2098. struct wpa_config_blob *blob)
  2099. {
  2100. wpa_config_remove_blob(config, blob->name);
  2101. blob->next = config->blobs;
  2102. config->blobs = blob;
  2103. }
  2104. /**
  2105. * wpa_config_free_blob - Free blob data
  2106. * @blob: Pointer to blob to be freed
  2107. */
  2108. void wpa_config_free_blob(struct wpa_config_blob *blob)
  2109. {
  2110. if (blob) {
  2111. os_free(blob->name);
  2112. os_free(blob->data);
  2113. os_free(blob);
  2114. }
  2115. }
  2116. /**
  2117. * wpa_config_remove_blob - Remove a named configuration blob
  2118. * @config: Configuration data from wpa_config_read()
  2119. * @name: Name of the blob to remove
  2120. * Returns: 0 if blob was removed or -1 if blob was not found
  2121. */
  2122. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  2123. {
  2124. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  2125. while (pos) {
  2126. if (os_strcmp(pos->name, name) == 0) {
  2127. if (prev)
  2128. prev->next = pos->next;
  2129. else
  2130. config->blobs = pos->next;
  2131. wpa_config_free_blob(pos);
  2132. return 0;
  2133. }
  2134. prev = pos;
  2135. pos = pos->next;
  2136. }
  2137. return -1;
  2138. }
  2139. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2140. /**
  2141. * wpa_config_alloc_empty - Allocate an empty configuration
  2142. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  2143. * socket
  2144. * @driver_param: Driver parameters
  2145. * Returns: Pointer to allocated configuration data or %NULL on failure
  2146. */
  2147. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  2148. const char *driver_param)
  2149. {
  2150. struct wpa_config *config;
  2151. config = os_zalloc(sizeof(*config));
  2152. if (config == NULL)
  2153. return NULL;
  2154. config->eapol_version = DEFAULT_EAPOL_VERSION;
  2155. config->ap_scan = DEFAULT_AP_SCAN;
  2156. config->fast_reauth = DEFAULT_FAST_REAUTH;
  2157. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  2158. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  2159. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  2160. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  2161. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  2162. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  2163. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  2164. if (ctrl_interface)
  2165. config->ctrl_interface = os_strdup(ctrl_interface);
  2166. if (driver_param)
  2167. config->driver_param = os_strdup(driver_param);
  2168. return config;
  2169. }
  2170. #ifndef CONFIG_NO_STDOUT_DEBUG
  2171. /**
  2172. * wpa_config_debug_dump_networks - Debug dump of configured networks
  2173. * @config: Configuration data from wpa_config_read()
  2174. */
  2175. void wpa_config_debug_dump_networks(struct wpa_config *config)
  2176. {
  2177. int prio;
  2178. struct wpa_ssid *ssid;
  2179. for (prio = 0; prio < config->num_prio; prio++) {
  2180. ssid = config->pssid[prio];
  2181. wpa_printf(MSG_DEBUG, "Priority group %d",
  2182. ssid->priority);
  2183. while (ssid) {
  2184. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  2185. ssid->id,
  2186. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2187. ssid = ssid->pnext;
  2188. }
  2189. }
  2190. }
  2191. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2192. struct global_parse_data {
  2193. char *name;
  2194. int (*parser)(const struct global_parse_data *data,
  2195. struct wpa_config *config, int line, const char *value);
  2196. void *param1, *param2, *param3;
  2197. unsigned int changed_flag;
  2198. };
  2199. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  2200. struct wpa_config *config, int line,
  2201. const char *pos)
  2202. {
  2203. int *dst;
  2204. dst = (int *) (((u8 *) config) + (long) data->param1);
  2205. *dst = atoi(pos);
  2206. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  2207. if (data->param2 && *dst < (long) data->param2) {
  2208. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  2209. "min_value=%ld)", line, data->name, *dst,
  2210. (long) data->param2);
  2211. *dst = (long) data->param2;
  2212. return -1;
  2213. }
  2214. if (data->param3 && *dst > (long) data->param3) {
  2215. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  2216. "max_value=%ld)", line, data->name, *dst,
  2217. (long) data->param3);
  2218. *dst = (long) data->param3;
  2219. return -1;
  2220. }
  2221. return 0;
  2222. }
  2223. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  2224. struct wpa_config *config, int line,
  2225. const char *pos)
  2226. {
  2227. size_t len;
  2228. char **dst, *tmp;
  2229. len = os_strlen(pos);
  2230. if (data->param2 && len < (size_t) data->param2) {
  2231. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  2232. "min_len=%ld)", line, data->name,
  2233. (unsigned long) len, (long) data->param2);
  2234. return -1;
  2235. }
  2236. if (data->param3 && len > (size_t) data->param3) {
  2237. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  2238. "max_len=%ld)", line, data->name,
  2239. (unsigned long) len, (long) data->param3);
  2240. return -1;
  2241. }
  2242. tmp = os_strdup(pos);
  2243. if (tmp == NULL)
  2244. return -1;
  2245. dst = (char **) (((u8 *) config) + (long) data->param1);
  2246. os_free(*dst);
  2247. *dst = tmp;
  2248. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  2249. return 0;
  2250. }
  2251. static int wpa_global_config_parse_bin(const struct global_parse_data *data,
  2252. struct wpa_config *config, int line,
  2253. const char *pos)
  2254. {
  2255. size_t len;
  2256. struct wpabuf **dst, *tmp;
  2257. len = os_strlen(pos);
  2258. if (len & 0x01)
  2259. return -1;
  2260. tmp = wpabuf_alloc(len / 2);
  2261. if (tmp == NULL)
  2262. return -1;
  2263. if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
  2264. wpabuf_free(tmp);
  2265. return -1;
  2266. }
  2267. dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
  2268. wpabuf_free(*dst);
  2269. *dst = tmp;
  2270. wpa_printf(MSG_DEBUG, "%s", data->name);
  2271. return 0;
  2272. }
  2273. static int wpa_config_process_country(const struct global_parse_data *data,
  2274. struct wpa_config *config, int line,
  2275. const char *pos)
  2276. {
  2277. if (!pos[0] || !pos[1]) {
  2278. wpa_printf(MSG_DEBUG, "Invalid country set");
  2279. return -1;
  2280. }
  2281. config->country[0] = pos[0];
  2282. config->country[1] = pos[1];
  2283. wpa_printf(MSG_DEBUG, "country='%c%c'",
  2284. config->country[0], config->country[1]);
  2285. return 0;
  2286. }
  2287. static int wpa_config_process_load_dynamic_eap(
  2288. const struct global_parse_data *data, struct wpa_config *config,
  2289. int line, const char *so)
  2290. {
  2291. int ret;
  2292. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  2293. ret = eap_peer_method_load(so);
  2294. if (ret == -2) {
  2295. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  2296. "reloading.");
  2297. } else if (ret) {
  2298. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  2299. "method '%s'.", line, so);
  2300. return -1;
  2301. }
  2302. return 0;
  2303. }
  2304. #ifdef CONFIG_WPS
  2305. static int wpa_config_process_uuid(const struct global_parse_data *data,
  2306. struct wpa_config *config, int line,
  2307. const char *pos)
  2308. {
  2309. char buf[40];
  2310. if (uuid_str2bin(pos, config->uuid)) {
  2311. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  2312. return -1;
  2313. }
  2314. uuid_bin2str(config->uuid, buf, sizeof(buf));
  2315. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  2316. return 0;
  2317. }
  2318. static int wpa_config_process_device_type(
  2319. const struct global_parse_data *data,
  2320. struct wpa_config *config, int line, const char *pos)
  2321. {
  2322. return wps_dev_type_str2bin(pos, config->device_type);
  2323. }
  2324. static int wpa_config_process_os_version(const struct global_parse_data *data,
  2325. struct wpa_config *config, int line,
  2326. const char *pos)
  2327. {
  2328. if (hexstr2bin(pos, config->os_version, 4)) {
  2329. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  2330. return -1;
  2331. }
  2332. wpa_printf(MSG_DEBUG, "os_version=%08x",
  2333. WPA_GET_BE32(config->os_version));
  2334. return 0;
  2335. }
  2336. static int wpa_config_process_wps_vendor_ext_m1(
  2337. const struct global_parse_data *data,
  2338. struct wpa_config *config, int line, const char *pos)
  2339. {
  2340. struct wpabuf *tmp;
  2341. int len = os_strlen(pos) / 2;
  2342. u8 *p;
  2343. if (!len) {
  2344. wpa_printf(MSG_ERROR, "Line %d: "
  2345. "invalid wps_vendor_ext_m1", line);
  2346. return -1;
  2347. }
  2348. tmp = wpabuf_alloc(len);
  2349. if (tmp) {
  2350. p = wpabuf_put(tmp, len);
  2351. if (hexstr2bin(pos, p, len)) {
  2352. wpa_printf(MSG_ERROR, "Line %d: "
  2353. "invalid wps_vendor_ext_m1", line);
  2354. wpabuf_free(tmp);
  2355. return -1;
  2356. }
  2357. wpabuf_free(config->wps_vendor_ext_m1);
  2358. config->wps_vendor_ext_m1 = tmp;
  2359. } else {
  2360. wpa_printf(MSG_ERROR, "Can not allocate "
  2361. "memory for wps_vendor_ext_m1");
  2362. return -1;
  2363. }
  2364. return 0;
  2365. }
  2366. #endif /* CONFIG_WPS */
  2367. #ifdef CONFIG_P2P
  2368. static int wpa_config_process_sec_device_type(
  2369. const struct global_parse_data *data,
  2370. struct wpa_config *config, int line, const char *pos)
  2371. {
  2372. int idx;
  2373. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  2374. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  2375. "items", line);
  2376. return -1;
  2377. }
  2378. idx = config->num_sec_device_types;
  2379. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  2380. return -1;
  2381. config->num_sec_device_types++;
  2382. return 0;
  2383. }
  2384. static int wpa_config_process_p2p_pref_chan(
  2385. const struct global_parse_data *data,
  2386. struct wpa_config *config, int line, const char *pos)
  2387. {
  2388. struct p2p_channel *pref = NULL, *n;
  2389. unsigned int num = 0;
  2390. const char *pos2;
  2391. u8 op_class, chan;
  2392. /* format: class:chan,class:chan,... */
  2393. while (*pos) {
  2394. op_class = atoi(pos);
  2395. pos2 = os_strchr(pos, ':');
  2396. if (pos2 == NULL)
  2397. goto fail;
  2398. pos2++;
  2399. chan = atoi(pos2);
  2400. n = os_realloc(pref, (num + 1) * sizeof(struct p2p_channel));
  2401. if (n == NULL)
  2402. goto fail;
  2403. pref = n;
  2404. pref[num].op_class = op_class;
  2405. pref[num].chan = chan;
  2406. num++;
  2407. pos = os_strchr(pos2, ',');
  2408. if (pos == NULL)
  2409. break;
  2410. pos++;
  2411. }
  2412. os_free(config->p2p_pref_chan);
  2413. config->p2p_pref_chan = pref;
  2414. config->num_p2p_pref_chan = num;
  2415. wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
  2416. (u8 *) config->p2p_pref_chan,
  2417. config->num_p2p_pref_chan * sizeof(struct p2p_channel));
  2418. return 0;
  2419. fail:
  2420. os_free(pref);
  2421. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
  2422. return -1;
  2423. }
  2424. #endif /* CONFIG_P2P */
  2425. static int wpa_config_process_hessid(
  2426. const struct global_parse_data *data,
  2427. struct wpa_config *config, int line, const char *pos)
  2428. {
  2429. if (hwaddr_aton2(pos, config->hessid) < 0) {
  2430. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  2431. line, pos);
  2432. return -1;
  2433. }
  2434. return 0;
  2435. }
  2436. #ifdef OFFSET
  2437. #undef OFFSET
  2438. #endif /* OFFSET */
  2439. /* OFFSET: Get offset of a variable within the wpa_config structure */
  2440. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  2441. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  2442. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  2443. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  2444. #define INT(f) _INT(f), NULL, NULL
  2445. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  2446. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  2447. #define STR(f) _STR(f), NULL, NULL
  2448. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  2449. #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
  2450. static const struct global_parse_data global_fields[] = {
  2451. #ifdef CONFIG_CTRL_IFACE
  2452. { STR(ctrl_interface), 0 },
  2453. { STR(ctrl_interface_group), 0 } /* deprecated */,
  2454. #endif /* CONFIG_CTRL_IFACE */
  2455. { INT_RANGE(eapol_version, 1, 2), 0 },
  2456. { INT(ap_scan), 0 },
  2457. { INT(disable_scan_offload), 0 },
  2458. { INT(fast_reauth), 0 },
  2459. { STR(opensc_engine_path), 0 },
  2460. { STR(pkcs11_engine_path), 0 },
  2461. { STR(pkcs11_module_path), 0 },
  2462. { STR(pcsc_reader), 0 },
  2463. { STR(pcsc_pin), 0 },
  2464. { STR(driver_param), 0 },
  2465. { INT(dot11RSNAConfigPMKLifetime), 0 },
  2466. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  2467. { INT(dot11RSNAConfigSATimeout), 0 },
  2468. #ifndef CONFIG_NO_CONFIG_WRITE
  2469. { INT(update_config), 0 },
  2470. #endif /* CONFIG_NO_CONFIG_WRITE */
  2471. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  2472. #ifdef CONFIG_WPS
  2473. { FUNC(uuid), CFG_CHANGED_UUID },
  2474. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  2475. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  2476. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  2477. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2478. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2479. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  2480. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  2481. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  2482. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  2483. { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
  2484. #endif /* CONFIG_WPS */
  2485. #ifdef CONFIG_P2P
  2486. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  2487. { INT(p2p_listen_reg_class), 0 },
  2488. { INT(p2p_listen_channel), 0 },
  2489. { INT(p2p_oper_reg_class), 0 },
  2490. { INT(p2p_oper_channel), 0 },
  2491. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  2492. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  2493. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  2494. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  2495. { INT(p2p_group_idle), 0 },
  2496. { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
  2497. #endif /* CONFIG_P2P */
  2498. { FUNC(country), CFG_CHANGED_COUNTRY },
  2499. { INT(bss_max_count), 0 },
  2500. { INT(bss_expiration_age), 0 },
  2501. { INT(bss_expiration_scan_count), 0 },
  2502. { INT_RANGE(filter_ssids, 0, 1), 0 },
  2503. { INT_RANGE(filter_rssi, -100, 0), 0 },
  2504. { INT(max_num_sta), 0 },
  2505. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  2506. #ifdef CONFIG_HS20
  2507. { INT_RANGE(hs20, 0, 1), 0 },
  2508. #endif /* CONFIG_HS20 */
  2509. { INT_RANGE(interworking, 0, 1), 0 },
  2510. { FUNC(hessid), 0 },
  2511. { INT_RANGE(access_network_type, 0, 15), 0 },
  2512. { INT_RANGE(pbc_in_m1, 0, 1), 0 },
  2513. { STR(autoscan), 0 },
  2514. { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff), 0 },
  2515. { BIN(wps_nfc_dh_pubkey), 0 },
  2516. { BIN(wps_nfc_dh_privkey), 0 },
  2517. { BIN(wps_nfc_dev_pw), 0 }
  2518. };
  2519. #undef FUNC
  2520. #undef _INT
  2521. #undef INT
  2522. #undef INT_RANGE
  2523. #undef _STR
  2524. #undef STR
  2525. #undef STR_RANGE
  2526. #undef BIN
  2527. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  2528. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  2529. {
  2530. size_t i;
  2531. int ret = 0;
  2532. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  2533. const struct global_parse_data *field = &global_fields[i];
  2534. size_t flen = os_strlen(field->name);
  2535. if (os_strncmp(pos, field->name, flen) != 0 ||
  2536. pos[flen] != '=')
  2537. continue;
  2538. if (field->parser(field, config, line, pos + flen + 1)) {
  2539. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2540. "parse '%s'.", line, pos);
  2541. ret = -1;
  2542. }
  2543. config->changed_parameters |= field->changed_flag;
  2544. break;
  2545. }
  2546. if (i == NUM_GLOBAL_FIELDS) {
  2547. if (line < 0)
  2548. return -1;
  2549. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  2550. line, pos);
  2551. ret = -1;
  2552. }
  2553. return ret;
  2554. }