config.c 68 KB

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