config.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916
  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. os_memcpy(key, buf, *len);
  1063. os_free(buf);
  1064. res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
  1065. if (res >= 0 && (size_t) res < sizeof(title))
  1066. wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
  1067. return 0;
  1068. }
  1069. static int wpa_config_parse_wep_key0(const struct parse_data *data,
  1070. struct wpa_ssid *ssid, int line,
  1071. const char *value)
  1072. {
  1073. return wpa_config_parse_wep_key(ssid->wep_key[0],
  1074. &ssid->wep_key_len[0], line,
  1075. value, 0);
  1076. }
  1077. static int wpa_config_parse_wep_key1(const struct parse_data *data,
  1078. struct wpa_ssid *ssid, int line,
  1079. const char *value)
  1080. {
  1081. return wpa_config_parse_wep_key(ssid->wep_key[1],
  1082. &ssid->wep_key_len[1], line,
  1083. value, 1);
  1084. }
  1085. static int wpa_config_parse_wep_key2(const struct parse_data *data,
  1086. struct wpa_ssid *ssid, int line,
  1087. const char *value)
  1088. {
  1089. return wpa_config_parse_wep_key(ssid->wep_key[2],
  1090. &ssid->wep_key_len[2], line,
  1091. value, 2);
  1092. }
  1093. static int wpa_config_parse_wep_key3(const struct parse_data *data,
  1094. struct wpa_ssid *ssid, int line,
  1095. const char *value)
  1096. {
  1097. return wpa_config_parse_wep_key(ssid->wep_key[3],
  1098. &ssid->wep_key_len[3], line,
  1099. value, 3);
  1100. }
  1101. #ifndef NO_CONFIG_WRITE
  1102. static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
  1103. {
  1104. if (ssid->wep_key_len[idx] == 0)
  1105. return NULL;
  1106. return wpa_config_write_string(ssid->wep_key[idx],
  1107. ssid->wep_key_len[idx]);
  1108. }
  1109. static char * wpa_config_write_wep_key0(const struct parse_data *data,
  1110. struct wpa_ssid *ssid)
  1111. {
  1112. return wpa_config_write_wep_key(ssid, 0);
  1113. }
  1114. static char * wpa_config_write_wep_key1(const struct parse_data *data,
  1115. struct wpa_ssid *ssid)
  1116. {
  1117. return wpa_config_write_wep_key(ssid, 1);
  1118. }
  1119. static char * wpa_config_write_wep_key2(const struct parse_data *data,
  1120. struct wpa_ssid *ssid)
  1121. {
  1122. return wpa_config_write_wep_key(ssid, 2);
  1123. }
  1124. static char * wpa_config_write_wep_key3(const struct parse_data *data,
  1125. struct wpa_ssid *ssid)
  1126. {
  1127. return wpa_config_write_wep_key(ssid, 3);
  1128. }
  1129. #endif /* NO_CONFIG_WRITE */
  1130. #ifdef CONFIG_P2P
  1131. static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
  1132. struct wpa_ssid *ssid, int line,
  1133. const char *value)
  1134. {
  1135. const char *pos;
  1136. u8 *buf, *n, addr[ETH_ALEN];
  1137. size_t count;
  1138. buf = NULL;
  1139. count = 0;
  1140. pos = value;
  1141. while (pos && *pos) {
  1142. while (*pos == ' ')
  1143. pos++;
  1144. if (hwaddr_aton(pos, addr)) {
  1145. wpa_printf(MSG_ERROR, "Line %d: Invalid "
  1146. "p2p_client_list address '%s'.",
  1147. line, value);
  1148. /* continue anyway */
  1149. } else {
  1150. n = os_realloc(buf, (count + 1) * ETH_ALEN);
  1151. if (n == NULL) {
  1152. os_free(buf);
  1153. return -1;
  1154. }
  1155. buf = n;
  1156. os_memcpy(buf + count * ETH_ALEN, addr, ETH_ALEN);
  1157. count++;
  1158. wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
  1159. addr, ETH_ALEN);
  1160. }
  1161. pos = os_strchr(pos, ' ');
  1162. }
  1163. os_free(ssid->p2p_client_list);
  1164. ssid->p2p_client_list = buf;
  1165. ssid->num_p2p_clients = count;
  1166. return 0;
  1167. }
  1168. #ifndef NO_CONFIG_WRITE
  1169. static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
  1170. struct wpa_ssid *ssid)
  1171. {
  1172. char *value, *end, *pos;
  1173. int res;
  1174. size_t i;
  1175. if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
  1176. return NULL;
  1177. value = os_malloc(20 * ssid->num_p2p_clients);
  1178. if (value == NULL)
  1179. return NULL;
  1180. pos = value;
  1181. end = value + 20 * ssid->num_p2p_clients;
  1182. for (i = 0; i < ssid->num_p2p_clients; i++) {
  1183. res = os_snprintf(pos, end - pos, MACSTR " ",
  1184. MAC2STR(ssid->p2p_client_list +
  1185. i * ETH_ALEN));
  1186. if (res < 0 || res >= end - pos) {
  1187. os_free(value);
  1188. return NULL;
  1189. }
  1190. pos += res;
  1191. }
  1192. if (pos > value)
  1193. pos[-1] = '\0';
  1194. return value;
  1195. }
  1196. #endif /* NO_CONFIG_WRITE */
  1197. #endif /* CONFIG_P2P */
  1198. /* Helper macros for network block parser */
  1199. #ifdef OFFSET
  1200. #undef OFFSET
  1201. #endif /* OFFSET */
  1202. /* OFFSET: Get offset of a variable within the wpa_ssid structure */
  1203. #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
  1204. /* STR: Define a string variable for an ASCII string; f = field name */
  1205. #ifdef NO_CONFIG_WRITE
  1206. #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
  1207. #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
  1208. #else /* NO_CONFIG_WRITE */
  1209. #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
  1210. #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
  1211. #endif /* NO_CONFIG_WRITE */
  1212. #define STR(f) _STR(f), NULL, NULL, NULL, 0
  1213. #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
  1214. #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
  1215. #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
  1216. /* STR_LEN: Define a string variable with a separate variable for storing the
  1217. * data length. Unlike STR(), this can be used to store arbitrary binary data
  1218. * (i.e., even nul termination character). */
  1219. #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
  1220. #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
  1221. #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
  1222. #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
  1223. #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
  1224. /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
  1225. * explicitly specified. */
  1226. #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
  1227. #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
  1228. #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
  1229. #ifdef NO_CONFIG_WRITE
  1230. #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
  1231. #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
  1232. #else /* NO_CONFIG_WRITE */
  1233. #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1234. OFFSET(f), (void *) 0
  1235. #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1236. OFFSET(eap.f), (void *) 0
  1237. #endif /* NO_CONFIG_WRITE */
  1238. /* INT: Define an integer variable */
  1239. #define INT(f) _INT(f), NULL, NULL, 0
  1240. #define INTe(f) _INTe(f), NULL, NULL, 0
  1241. /* INT_RANGE: Define an integer variable with allowed value range */
  1242. #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
  1243. /* FUNC: Define a configuration variable that uses a custom function for
  1244. * parsing and writing the value. */
  1245. #ifdef NO_CONFIG_WRITE
  1246. #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
  1247. #else /* NO_CONFIG_WRITE */
  1248. #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
  1249. NULL, NULL, NULL, NULL
  1250. #endif /* NO_CONFIG_WRITE */
  1251. #define FUNC(f) _FUNC(f), 0
  1252. #define FUNC_KEY(f) _FUNC(f), 1
  1253. /*
  1254. * Table of network configuration variables. This table is used to parse each
  1255. * network configuration variable, e.g., each line in wpa_supplicant.conf file
  1256. * that is inside a network block.
  1257. *
  1258. * This table is generated using the helper macros defined above and with
  1259. * generous help from the C pre-processor. The field name is stored as a string
  1260. * into .name and for STR and INT types, the offset of the target buffer within
  1261. * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
  1262. * offset to the field containing the length of the configuration variable.
  1263. * .param3 and .param4 can be used to mark the allowed range (length for STR
  1264. * and value for INT).
  1265. *
  1266. * For each configuration line in wpa_supplicant.conf, the parser goes through
  1267. * this table and select the entry that matches with the field name. The parser
  1268. * function (.parser) is then called to parse the actual value of the field.
  1269. *
  1270. * This kind of mechanism makes it easy to add new configuration parameters,
  1271. * since only one line needs to be added into this table and into the
  1272. * struct wpa_ssid definition if the new variable is either a string or
  1273. * integer. More complex types will need to use their own parser and writer
  1274. * functions.
  1275. */
  1276. static const struct parse_data ssid_fields[] = {
  1277. { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
  1278. { INT_RANGE(scan_ssid, 0, 1) },
  1279. { FUNC(bssid) },
  1280. { FUNC_KEY(psk) },
  1281. { FUNC(proto) },
  1282. { FUNC(key_mgmt) },
  1283. { INT(bg_scan_period) },
  1284. { FUNC(pairwise) },
  1285. { FUNC(group) },
  1286. { FUNC(auth_alg) },
  1287. { FUNC(scan_freq) },
  1288. { FUNC(freq_list) },
  1289. #ifdef IEEE8021X_EAPOL
  1290. { FUNC(eap) },
  1291. { STR_LENe(identity) },
  1292. { STR_LENe(anonymous_identity) },
  1293. { FUNC_KEY(password) },
  1294. { STRe(ca_cert) },
  1295. { STRe(ca_path) },
  1296. { STRe(client_cert) },
  1297. { STRe(private_key) },
  1298. { STR_KEYe(private_key_passwd) },
  1299. { STRe(dh_file) },
  1300. { STRe(subject_match) },
  1301. { STRe(altsubject_match) },
  1302. { STRe(ca_cert2) },
  1303. { STRe(ca_path2) },
  1304. { STRe(client_cert2) },
  1305. { STRe(private_key2) },
  1306. { STR_KEYe(private_key2_passwd) },
  1307. { STRe(dh_file2) },
  1308. { STRe(subject_match2) },
  1309. { STRe(altsubject_match2) },
  1310. { STRe(phase1) },
  1311. { STRe(phase2) },
  1312. { STRe(pcsc) },
  1313. { STR_KEYe(pin) },
  1314. { STRe(engine_id) },
  1315. { STRe(key_id) },
  1316. { STRe(cert_id) },
  1317. { STRe(ca_cert_id) },
  1318. { STR_KEYe(pin2) },
  1319. { STRe(engine2_id) },
  1320. { STRe(key2_id) },
  1321. { STRe(cert2_id) },
  1322. { STRe(ca_cert2_id) },
  1323. { INTe(engine) },
  1324. { INTe(engine2) },
  1325. { INT(eapol_flags) },
  1326. #endif /* IEEE8021X_EAPOL */
  1327. { FUNC_KEY(wep_key0) },
  1328. { FUNC_KEY(wep_key1) },
  1329. { FUNC_KEY(wep_key2) },
  1330. { FUNC_KEY(wep_key3) },
  1331. { INT(wep_tx_keyidx) },
  1332. { INT(priority) },
  1333. #ifdef IEEE8021X_EAPOL
  1334. { INT(eap_workaround) },
  1335. { STRe(pac_file) },
  1336. { INTe(fragment_size) },
  1337. #endif /* IEEE8021X_EAPOL */
  1338. { INT_RANGE(mode, 0, 4) },
  1339. { INT_RANGE(proactive_key_caching, 0, 1) },
  1340. { INT_RANGE(disabled, 0, 2) },
  1341. { STR(id_str) },
  1342. #ifdef CONFIG_IEEE80211W
  1343. { INT_RANGE(ieee80211w, 0, 2) },
  1344. #endif /* CONFIG_IEEE80211W */
  1345. { INT_RANGE(peerkey, 0, 1) },
  1346. { INT_RANGE(mixed_cell, 0, 1) },
  1347. { INT_RANGE(frequency, 0, 10000) },
  1348. { INT(wpa_ptk_rekey) },
  1349. { STR(bgscan) },
  1350. { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
  1351. #ifdef CONFIG_P2P
  1352. { FUNC(p2p_client_list) },
  1353. #endif /* CONFIG_P2P */
  1354. #ifdef CONFIG_HT_OVERRIDES
  1355. { INT_RANGE(disable_ht, 0, 1) },
  1356. { INT_RANGE(disable_ht40, -1, 1) },
  1357. { INT_RANGE(disable_max_amsdu, -1, 1) },
  1358. { INT_RANGE(ampdu_factor, -1, 3) },
  1359. { INT_RANGE(ampdu_density, -1, 7) },
  1360. { STR(ht_mcs) },
  1361. #endif /* CONFIG_HT_OVERRIDES */
  1362. { INT(ap_max_inactivity) },
  1363. };
  1364. #undef OFFSET
  1365. #undef _STR
  1366. #undef STR
  1367. #undef STR_KEY
  1368. #undef _STR_LEN
  1369. #undef STR_LEN
  1370. #undef STR_LEN_KEY
  1371. #undef _STR_RANGE
  1372. #undef STR_RANGE
  1373. #undef STR_RANGE_KEY
  1374. #undef _INT
  1375. #undef INT
  1376. #undef INT_RANGE
  1377. #undef _FUNC
  1378. #undef FUNC
  1379. #undef FUNC_KEY
  1380. #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
  1381. /**
  1382. * wpa_config_add_prio_network - Add a network to priority lists
  1383. * @config: Configuration data from wpa_config_read()
  1384. * @ssid: Pointer to the network configuration to be added to the list
  1385. * Returns: 0 on success, -1 on failure
  1386. *
  1387. * This function is used to add a network block to the priority list of
  1388. * networks. This must be called for each network when reading in the full
  1389. * configuration. In addition, this can be used indirectly when updating
  1390. * priorities by calling wpa_config_update_prio_list().
  1391. */
  1392. int wpa_config_add_prio_network(struct wpa_config *config,
  1393. struct wpa_ssid *ssid)
  1394. {
  1395. int prio;
  1396. struct wpa_ssid *prev, **nlist;
  1397. /*
  1398. * Add to an existing priority list if one is available for the
  1399. * configured priority level for this network.
  1400. */
  1401. for (prio = 0; prio < config->num_prio; prio++) {
  1402. prev = config->pssid[prio];
  1403. if (prev->priority == ssid->priority) {
  1404. while (prev->pnext)
  1405. prev = prev->pnext;
  1406. prev->pnext = ssid;
  1407. return 0;
  1408. }
  1409. }
  1410. /* First network for this priority - add a new priority list */
  1411. nlist = os_realloc(config->pssid,
  1412. (config->num_prio + 1) * sizeof(struct wpa_ssid *));
  1413. if (nlist == NULL)
  1414. return -1;
  1415. for (prio = 0; prio < config->num_prio; prio++) {
  1416. if (nlist[prio]->priority < ssid->priority) {
  1417. os_memmove(&nlist[prio + 1], &nlist[prio],
  1418. (config->num_prio - prio) *
  1419. sizeof(struct wpa_ssid *));
  1420. break;
  1421. }
  1422. }
  1423. nlist[prio] = ssid;
  1424. config->num_prio++;
  1425. config->pssid = nlist;
  1426. return 0;
  1427. }
  1428. /**
  1429. * wpa_config_update_prio_list - Update network priority list
  1430. * @config: Configuration data from wpa_config_read()
  1431. * Returns: 0 on success, -1 on failure
  1432. *
  1433. * This function is called to update the priority list of networks in the
  1434. * configuration when a network is being added or removed. This is also called
  1435. * if a priority for a network is changed.
  1436. */
  1437. int wpa_config_update_prio_list(struct wpa_config *config)
  1438. {
  1439. struct wpa_ssid *ssid;
  1440. int ret = 0;
  1441. os_free(config->pssid);
  1442. config->pssid = NULL;
  1443. config->num_prio = 0;
  1444. ssid = config->ssid;
  1445. while (ssid) {
  1446. ssid->pnext = NULL;
  1447. if (wpa_config_add_prio_network(config, ssid) < 0)
  1448. ret = -1;
  1449. ssid = ssid->next;
  1450. }
  1451. return ret;
  1452. }
  1453. #ifdef IEEE8021X_EAPOL
  1454. static void eap_peer_config_free(struct eap_peer_config *eap)
  1455. {
  1456. os_free(eap->eap_methods);
  1457. os_free(eap->identity);
  1458. os_free(eap->anonymous_identity);
  1459. os_free(eap->password);
  1460. os_free(eap->ca_cert);
  1461. os_free(eap->ca_path);
  1462. os_free(eap->client_cert);
  1463. os_free(eap->private_key);
  1464. os_free(eap->private_key_passwd);
  1465. os_free(eap->dh_file);
  1466. os_free(eap->subject_match);
  1467. os_free(eap->altsubject_match);
  1468. os_free(eap->ca_cert2);
  1469. os_free(eap->ca_path2);
  1470. os_free(eap->client_cert2);
  1471. os_free(eap->private_key2);
  1472. os_free(eap->private_key2_passwd);
  1473. os_free(eap->dh_file2);
  1474. os_free(eap->subject_match2);
  1475. os_free(eap->altsubject_match2);
  1476. os_free(eap->phase1);
  1477. os_free(eap->phase2);
  1478. os_free(eap->pcsc);
  1479. os_free(eap->pin);
  1480. os_free(eap->engine_id);
  1481. os_free(eap->key_id);
  1482. os_free(eap->cert_id);
  1483. os_free(eap->ca_cert_id);
  1484. os_free(eap->key2_id);
  1485. os_free(eap->cert2_id);
  1486. os_free(eap->ca_cert2_id);
  1487. os_free(eap->pin2);
  1488. os_free(eap->engine2_id);
  1489. os_free(eap->otp);
  1490. os_free(eap->pending_req_otp);
  1491. os_free(eap->pac_file);
  1492. os_free(eap->new_password);
  1493. }
  1494. #endif /* IEEE8021X_EAPOL */
  1495. /**
  1496. * wpa_config_free_ssid - Free network/ssid configuration data
  1497. * @ssid: Configuration data for the network
  1498. *
  1499. * This function frees all resources allocated for the network configuration
  1500. * data.
  1501. */
  1502. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1503. {
  1504. os_free(ssid->ssid);
  1505. os_free(ssid->passphrase);
  1506. #ifdef IEEE8021X_EAPOL
  1507. eap_peer_config_free(&ssid->eap);
  1508. #endif /* IEEE8021X_EAPOL */
  1509. os_free(ssid->id_str);
  1510. os_free(ssid->scan_freq);
  1511. os_free(ssid->freq_list);
  1512. os_free(ssid->bgscan);
  1513. os_free(ssid->p2p_client_list);
  1514. #ifdef CONFIG_HT_OVERRIDES
  1515. os_free(ssid->ht_mcs);
  1516. #endif /* CONFIG_HT_OVERRIDES */
  1517. os_free(ssid);
  1518. }
  1519. void wpa_config_free_cred(struct wpa_cred *cred)
  1520. {
  1521. os_free(cred->realm);
  1522. os_free(cred->username);
  1523. os_free(cred->password);
  1524. os_free(cred->ca_cert);
  1525. os_free(cred->client_cert);
  1526. os_free(cred->private_key);
  1527. os_free(cred->private_key_passwd);
  1528. os_free(cred->imsi);
  1529. os_free(cred->milenage);
  1530. os_free(cred->domain);
  1531. os_free(cred);
  1532. }
  1533. /**
  1534. * wpa_config_free - Free configuration data
  1535. * @config: Configuration data from wpa_config_read()
  1536. *
  1537. * This function frees all resources allocated for the configuration data by
  1538. * wpa_config_read().
  1539. */
  1540. void wpa_config_free(struct wpa_config *config)
  1541. {
  1542. #ifndef CONFIG_NO_CONFIG_BLOBS
  1543. struct wpa_config_blob *blob, *prevblob;
  1544. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1545. struct wpa_ssid *ssid, *prev = NULL;
  1546. struct wpa_cred *cred, *cprev;
  1547. ssid = config->ssid;
  1548. while (ssid) {
  1549. prev = ssid;
  1550. ssid = ssid->next;
  1551. wpa_config_free_ssid(prev);
  1552. }
  1553. cred = config->cred;
  1554. while (cred) {
  1555. cprev = cred;
  1556. cred = cred->next;
  1557. wpa_config_free_cred(cprev);
  1558. }
  1559. #ifndef CONFIG_NO_CONFIG_BLOBS
  1560. blob = config->blobs;
  1561. prevblob = NULL;
  1562. while (blob) {
  1563. prevblob = blob;
  1564. blob = blob->next;
  1565. wpa_config_free_blob(prevblob);
  1566. }
  1567. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1568. wpabuf_free(config->wps_vendor_ext_m1);
  1569. os_free(config->ctrl_interface);
  1570. os_free(config->ctrl_interface_group);
  1571. os_free(config->opensc_engine_path);
  1572. os_free(config->pkcs11_engine_path);
  1573. os_free(config->pkcs11_module_path);
  1574. os_free(config->pcsc_reader);
  1575. os_free(config->pcsc_pin);
  1576. os_free(config->driver_param);
  1577. os_free(config->device_name);
  1578. os_free(config->manufacturer);
  1579. os_free(config->model_name);
  1580. os_free(config->model_number);
  1581. os_free(config->serial_number);
  1582. os_free(config->config_methods);
  1583. os_free(config->p2p_ssid_postfix);
  1584. os_free(config->pssid);
  1585. os_free(config->p2p_pref_chan);
  1586. os_free(config);
  1587. }
  1588. /**
  1589. * wpa_config_foreach_network - Iterate over each configured network
  1590. * @config: Configuration data from wpa_config_read()
  1591. * @func: Callback function to process each network
  1592. * @arg: Opaque argument to pass to callback function
  1593. *
  1594. * Iterate over the set of configured networks calling the specified
  1595. * function for each item. We guard against callbacks removing the
  1596. * supplied network.
  1597. */
  1598. void wpa_config_foreach_network(struct wpa_config *config,
  1599. void (*func)(void *, struct wpa_ssid *),
  1600. void *arg)
  1601. {
  1602. struct wpa_ssid *ssid, *next;
  1603. ssid = config->ssid;
  1604. while (ssid) {
  1605. next = ssid->next;
  1606. func(arg, ssid);
  1607. ssid = next;
  1608. }
  1609. }
  1610. /**
  1611. * wpa_config_get_network - Get configured network based on id
  1612. * @config: Configuration data from wpa_config_read()
  1613. * @id: Unique network id to search for
  1614. * Returns: Network configuration or %NULL if not found
  1615. */
  1616. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1617. {
  1618. struct wpa_ssid *ssid;
  1619. ssid = config->ssid;
  1620. while (ssid) {
  1621. if (id == ssid->id)
  1622. break;
  1623. ssid = ssid->next;
  1624. }
  1625. return ssid;
  1626. }
  1627. /**
  1628. * wpa_config_add_network - Add a new network with empty configuration
  1629. * @config: Configuration data from wpa_config_read()
  1630. * Returns: The new network configuration or %NULL if operation failed
  1631. */
  1632. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1633. {
  1634. int id;
  1635. struct wpa_ssid *ssid, *last = NULL;
  1636. id = -1;
  1637. ssid = config->ssid;
  1638. while (ssid) {
  1639. if (ssid->id > id)
  1640. id = ssid->id;
  1641. last = ssid;
  1642. ssid = ssid->next;
  1643. }
  1644. id++;
  1645. ssid = os_zalloc(sizeof(*ssid));
  1646. if (ssid == NULL)
  1647. return NULL;
  1648. ssid->id = id;
  1649. if (last)
  1650. last->next = ssid;
  1651. else
  1652. config->ssid = ssid;
  1653. wpa_config_update_prio_list(config);
  1654. return ssid;
  1655. }
  1656. /**
  1657. * wpa_config_remove_network - Remove a configured network based on id
  1658. * @config: Configuration data from wpa_config_read()
  1659. * @id: Unique network id to search for
  1660. * Returns: 0 on success, or -1 if the network was not found
  1661. */
  1662. int wpa_config_remove_network(struct wpa_config *config, int id)
  1663. {
  1664. struct wpa_ssid *ssid, *prev = NULL;
  1665. ssid = config->ssid;
  1666. while (ssid) {
  1667. if (id == ssid->id)
  1668. break;
  1669. prev = ssid;
  1670. ssid = ssid->next;
  1671. }
  1672. if (ssid == NULL)
  1673. return -1;
  1674. if (prev)
  1675. prev->next = ssid->next;
  1676. else
  1677. config->ssid = ssid->next;
  1678. wpa_config_update_prio_list(config);
  1679. wpa_config_free_ssid(ssid);
  1680. return 0;
  1681. }
  1682. /**
  1683. * wpa_config_set_network_defaults - Set network default values
  1684. * @ssid: Pointer to network configuration data
  1685. */
  1686. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1687. {
  1688. ssid->proto = DEFAULT_PROTO;
  1689. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1690. ssid->group_cipher = DEFAULT_GROUP;
  1691. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1692. ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
  1693. #ifdef IEEE8021X_EAPOL
  1694. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1695. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1696. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1697. #endif /* IEEE8021X_EAPOL */
  1698. #ifdef CONFIG_HT_OVERRIDES
  1699. ssid->disable_ht = DEFAULT_DISABLE_HT;
  1700. ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
  1701. ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
  1702. ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
  1703. ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
  1704. #endif /* CONFIG_HT_OVERRIDES */
  1705. }
  1706. /**
  1707. * wpa_config_set - Set a variable in network configuration
  1708. * @ssid: Pointer to network configuration data
  1709. * @var: Variable name, e.g., "ssid"
  1710. * @value: Variable value
  1711. * @line: Line number in configuration file or 0 if not used
  1712. * Returns: 0 on success, -1 on failure
  1713. *
  1714. * This function can be used to set network configuration variables based on
  1715. * both the configuration file and management interface input. The value
  1716. * parameter must be in the same format as the text-based configuration file is
  1717. * using. For example, strings are using double quotation marks.
  1718. */
  1719. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1720. int line)
  1721. {
  1722. size_t i;
  1723. int ret = 0;
  1724. if (ssid == NULL || var == NULL || value == NULL)
  1725. return -1;
  1726. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1727. const struct parse_data *field = &ssid_fields[i];
  1728. if (os_strcmp(var, field->name) != 0)
  1729. continue;
  1730. if (field->parser(field, ssid, line, value)) {
  1731. if (line) {
  1732. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1733. "parse %s '%s'.", line, var, value);
  1734. }
  1735. ret = -1;
  1736. }
  1737. break;
  1738. }
  1739. if (i == NUM_SSID_FIELDS) {
  1740. if (line) {
  1741. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1742. "'%s'.", line, var);
  1743. }
  1744. ret = -1;
  1745. }
  1746. return ret;
  1747. }
  1748. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  1749. const char *value)
  1750. {
  1751. size_t len;
  1752. char *buf;
  1753. int ret;
  1754. len = os_strlen(value);
  1755. buf = os_malloc(len + 3);
  1756. if (buf == NULL)
  1757. return -1;
  1758. buf[0] = '"';
  1759. os_memcpy(buf + 1, value, len);
  1760. buf[len + 1] = '"';
  1761. buf[len + 2] = '\0';
  1762. ret = wpa_config_set(ssid, var, buf, 0);
  1763. os_free(buf);
  1764. return ret;
  1765. }
  1766. /**
  1767. * wpa_config_get_all - Get all options from network configuration
  1768. * @ssid: Pointer to network configuration data
  1769. * @get_keys: Determines if keys/passwords will be included in returned list
  1770. * (if they may be exported)
  1771. * Returns: %NULL terminated list of all set keys and their values in the form
  1772. * of [key1, val1, key2, val2, ... , NULL]
  1773. *
  1774. * This function can be used to get list of all configured network properties.
  1775. * The caller is responsible for freeing the returned list and all its
  1776. * elements.
  1777. */
  1778. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  1779. {
  1780. const struct parse_data *field;
  1781. char *key, *value;
  1782. size_t i;
  1783. char **props;
  1784. int fields_num;
  1785. get_keys = get_keys && ssid->export_keys;
  1786. props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
  1787. if (!props)
  1788. return NULL;
  1789. fields_num = 0;
  1790. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1791. field = &ssid_fields[i];
  1792. if (field->key_data && !get_keys)
  1793. continue;
  1794. value = field->writer(field, ssid);
  1795. if (value == NULL)
  1796. continue;
  1797. if (os_strlen(value) == 0) {
  1798. os_free(value);
  1799. continue;
  1800. }
  1801. key = os_strdup(field->name);
  1802. if (key == NULL) {
  1803. os_free(value);
  1804. goto err;
  1805. }
  1806. props[fields_num * 2] = key;
  1807. props[fields_num * 2 + 1] = value;
  1808. fields_num++;
  1809. }
  1810. return props;
  1811. err:
  1812. value = *props;
  1813. while (value)
  1814. os_free(value++);
  1815. os_free(props);
  1816. return NULL;
  1817. }
  1818. #ifndef NO_CONFIG_WRITE
  1819. /**
  1820. * wpa_config_get - Get a variable in network configuration
  1821. * @ssid: Pointer to network configuration data
  1822. * @var: Variable name, e.g., "ssid"
  1823. * Returns: Value of the variable or %NULL on failure
  1824. *
  1825. * This function can be used to get network configuration variables. The
  1826. * returned value is a copy of the configuration variable in text format, i.e,.
  1827. * the same format that the text-based configuration file and wpa_config_set()
  1828. * are using for the value. The caller is responsible for freeing the returned
  1829. * value.
  1830. */
  1831. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1832. {
  1833. size_t i;
  1834. if (ssid == NULL || var == NULL)
  1835. return NULL;
  1836. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1837. const struct parse_data *field = &ssid_fields[i];
  1838. if (os_strcmp(var, field->name) == 0)
  1839. return field->writer(field, ssid);
  1840. }
  1841. return NULL;
  1842. }
  1843. /**
  1844. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1845. * @ssid: Pointer to network configuration data
  1846. * @var: Variable name, e.g., "ssid"
  1847. * Returns: Value of the variable or %NULL on failure
  1848. *
  1849. * This function can be used to get network configuration variable like
  1850. * wpa_config_get(). The only difference is that this functions does not expose
  1851. * key/password material from the configuration. In case a key/password field
  1852. * is requested, the returned value is an empty string or %NULL if the variable
  1853. * is not set or "*" if the variable is set (regardless of its value). The
  1854. * returned value is a copy of the configuration variable in text format, i.e,.
  1855. * the same format that the text-based configuration file and wpa_config_set()
  1856. * are using for the value. The caller is responsible for freeing the returned
  1857. * value.
  1858. */
  1859. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1860. {
  1861. size_t i;
  1862. if (ssid == NULL || var == NULL)
  1863. return NULL;
  1864. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1865. const struct parse_data *field = &ssid_fields[i];
  1866. if (os_strcmp(var, field->name) == 0) {
  1867. char *res = field->writer(field, ssid);
  1868. if (field->key_data) {
  1869. if (res && res[0]) {
  1870. wpa_printf(MSG_DEBUG, "Do not allow "
  1871. "key_data field to be "
  1872. "exposed");
  1873. os_free(res);
  1874. return os_strdup("*");
  1875. }
  1876. os_free(res);
  1877. return NULL;
  1878. }
  1879. return res;
  1880. }
  1881. }
  1882. return NULL;
  1883. }
  1884. #endif /* NO_CONFIG_WRITE */
  1885. /**
  1886. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  1887. * @ssid: Pointer to network configuration data
  1888. *
  1889. * This function must be called to update WPA PSK when either SSID or the
  1890. * passphrase has changed for the network configuration.
  1891. */
  1892. void wpa_config_update_psk(struct wpa_ssid *ssid)
  1893. {
  1894. #ifndef CONFIG_NO_PBKDF2
  1895. pbkdf2_sha1(ssid->passphrase,
  1896. (char *) ssid->ssid, ssid->ssid_len, 4096,
  1897. ssid->psk, PMK_LEN);
  1898. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1899. ssid->psk, PMK_LEN);
  1900. ssid->psk_set = 1;
  1901. #endif /* CONFIG_NO_PBKDF2 */
  1902. }
  1903. int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
  1904. const char *value, int line)
  1905. {
  1906. char *val;
  1907. size_t len;
  1908. if (os_strcmp(var, "priority") == 0) {
  1909. cred->priority = atoi(value);
  1910. return 0;
  1911. }
  1912. if (os_strcmp(var, "pcsc") == 0) {
  1913. cred->pcsc = atoi(value);
  1914. return 0;
  1915. }
  1916. val = wpa_config_parse_string(value, &len);
  1917. if (val == NULL) {
  1918. wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
  1919. "value '%s'.", line, var, value);
  1920. return -1;
  1921. }
  1922. if (os_strcmp(var, "realm") == 0) {
  1923. os_free(cred->realm);
  1924. cred->realm = val;
  1925. return 0;
  1926. }
  1927. if (os_strcmp(var, "username") == 0) {
  1928. os_free(cred->username);
  1929. cred->username = val;
  1930. return 0;
  1931. }
  1932. if (os_strcmp(var, "password") == 0) {
  1933. os_free(cred->password);
  1934. cred->password = val;
  1935. return 0;
  1936. }
  1937. if (os_strcmp(var, "ca_cert") == 0) {
  1938. os_free(cred->ca_cert);
  1939. cred->ca_cert = val;
  1940. return 0;
  1941. }
  1942. if (os_strcmp(var, "client_cert") == 0) {
  1943. os_free(cred->client_cert);
  1944. cred->client_cert = val;
  1945. return 0;
  1946. }
  1947. if (os_strcmp(var, "private_key") == 0) {
  1948. os_free(cred->private_key);
  1949. cred->private_key = val;
  1950. return 0;
  1951. }
  1952. if (os_strcmp(var, "private_key_passwd") == 0) {
  1953. os_free(cred->private_key_passwd);
  1954. cred->private_key_passwd = val;
  1955. return 0;
  1956. }
  1957. if (os_strcmp(var, "imsi") == 0) {
  1958. os_free(cred->imsi);
  1959. cred->imsi = val;
  1960. return 0;
  1961. }
  1962. if (os_strcmp(var, "milenage") == 0) {
  1963. os_free(cred->milenage);
  1964. cred->milenage = val;
  1965. return 0;
  1966. }
  1967. if (os_strcmp(var, "domain") == 0) {
  1968. os_free(cred->domain);
  1969. cred->domain = val;
  1970. return 0;
  1971. }
  1972. if (line) {
  1973. wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
  1974. line, var);
  1975. }
  1976. os_free(val);
  1977. return -1;
  1978. }
  1979. struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
  1980. {
  1981. struct wpa_cred *cred;
  1982. cred = config->cred;
  1983. while (cred) {
  1984. if (id == cred->id)
  1985. break;
  1986. cred = cred->next;
  1987. }
  1988. return cred;
  1989. }
  1990. struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
  1991. {
  1992. int id;
  1993. struct wpa_cred *cred, *last = NULL;
  1994. id = -1;
  1995. cred = config->cred;
  1996. while (cred) {
  1997. if (cred->id > id)
  1998. id = cred->id;
  1999. last = cred;
  2000. cred = cred->next;
  2001. }
  2002. id++;
  2003. cred = os_zalloc(sizeof(*cred));
  2004. if (cred == NULL)
  2005. return NULL;
  2006. cred->id = id;
  2007. if (last)
  2008. last->next = cred;
  2009. else
  2010. config->cred = cred;
  2011. return cred;
  2012. }
  2013. int wpa_config_remove_cred(struct wpa_config *config, int id)
  2014. {
  2015. struct wpa_cred *cred, *prev = NULL;
  2016. cred = config->cred;
  2017. while (cred) {
  2018. if (id == cred->id)
  2019. break;
  2020. prev = cred;
  2021. cred = cred->next;
  2022. }
  2023. if (cred == NULL)
  2024. return -1;
  2025. if (prev)
  2026. prev->next = cred->next;
  2027. else
  2028. config->cred = cred->next;
  2029. wpa_config_free_cred(cred);
  2030. return 0;
  2031. }
  2032. #ifndef CONFIG_NO_CONFIG_BLOBS
  2033. /**
  2034. * wpa_config_get_blob - Get a named configuration blob
  2035. * @config: Configuration data from wpa_config_read()
  2036. * @name: Name of the blob
  2037. * Returns: Pointer to blob data or %NULL if not found
  2038. */
  2039. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  2040. const char *name)
  2041. {
  2042. struct wpa_config_blob *blob = config->blobs;
  2043. while (blob) {
  2044. if (os_strcmp(blob->name, name) == 0)
  2045. return blob;
  2046. blob = blob->next;
  2047. }
  2048. return NULL;
  2049. }
  2050. /**
  2051. * wpa_config_set_blob - Set or add a named configuration blob
  2052. * @config: Configuration data from wpa_config_read()
  2053. * @blob: New value for the blob
  2054. *
  2055. * Adds a new configuration blob or replaces the current value of an existing
  2056. * blob.
  2057. */
  2058. void wpa_config_set_blob(struct wpa_config *config,
  2059. struct wpa_config_blob *blob)
  2060. {
  2061. wpa_config_remove_blob(config, blob->name);
  2062. blob->next = config->blobs;
  2063. config->blobs = blob;
  2064. }
  2065. /**
  2066. * wpa_config_free_blob - Free blob data
  2067. * @blob: Pointer to blob to be freed
  2068. */
  2069. void wpa_config_free_blob(struct wpa_config_blob *blob)
  2070. {
  2071. if (blob) {
  2072. os_free(blob->name);
  2073. os_free(blob->data);
  2074. os_free(blob);
  2075. }
  2076. }
  2077. /**
  2078. * wpa_config_remove_blob - Remove a named configuration blob
  2079. * @config: Configuration data from wpa_config_read()
  2080. * @name: Name of the blob to remove
  2081. * Returns: 0 if blob was removed or -1 if blob was not found
  2082. */
  2083. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  2084. {
  2085. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  2086. while (pos) {
  2087. if (os_strcmp(pos->name, name) == 0) {
  2088. if (prev)
  2089. prev->next = pos->next;
  2090. else
  2091. config->blobs = pos->next;
  2092. wpa_config_free_blob(pos);
  2093. return 0;
  2094. }
  2095. prev = pos;
  2096. pos = pos->next;
  2097. }
  2098. return -1;
  2099. }
  2100. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2101. /**
  2102. * wpa_config_alloc_empty - Allocate an empty configuration
  2103. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  2104. * socket
  2105. * @driver_param: Driver parameters
  2106. * Returns: Pointer to allocated configuration data or %NULL on failure
  2107. */
  2108. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  2109. const char *driver_param)
  2110. {
  2111. struct wpa_config *config;
  2112. config = os_zalloc(sizeof(*config));
  2113. if (config == NULL)
  2114. return NULL;
  2115. config->eapol_version = DEFAULT_EAPOL_VERSION;
  2116. config->ap_scan = DEFAULT_AP_SCAN;
  2117. config->fast_reauth = DEFAULT_FAST_REAUTH;
  2118. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  2119. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  2120. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  2121. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  2122. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  2123. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  2124. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  2125. if (ctrl_interface)
  2126. config->ctrl_interface = os_strdup(ctrl_interface);
  2127. if (driver_param)
  2128. config->driver_param = os_strdup(driver_param);
  2129. return config;
  2130. }
  2131. #ifndef CONFIG_NO_STDOUT_DEBUG
  2132. /**
  2133. * wpa_config_debug_dump_networks - Debug dump of configured networks
  2134. * @config: Configuration data from wpa_config_read()
  2135. */
  2136. void wpa_config_debug_dump_networks(struct wpa_config *config)
  2137. {
  2138. int prio;
  2139. struct wpa_ssid *ssid;
  2140. for (prio = 0; prio < config->num_prio; prio++) {
  2141. ssid = config->pssid[prio];
  2142. wpa_printf(MSG_DEBUG, "Priority group %d",
  2143. ssid->priority);
  2144. while (ssid) {
  2145. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  2146. ssid->id,
  2147. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2148. ssid = ssid->pnext;
  2149. }
  2150. }
  2151. }
  2152. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2153. struct global_parse_data {
  2154. char *name;
  2155. int (*parser)(const struct global_parse_data *data,
  2156. struct wpa_config *config, int line, const char *value);
  2157. void *param1, *param2, *param3;
  2158. unsigned int changed_flag;
  2159. };
  2160. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  2161. struct wpa_config *config, int line,
  2162. const char *pos)
  2163. {
  2164. int *dst;
  2165. dst = (int *) (((u8 *) config) + (long) data->param1);
  2166. *dst = atoi(pos);
  2167. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  2168. if (data->param2 && *dst < (long) data->param2) {
  2169. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  2170. "min_value=%ld)", line, data->name, *dst,
  2171. (long) data->param2);
  2172. *dst = (long) data->param2;
  2173. return -1;
  2174. }
  2175. if (data->param3 && *dst > (long) data->param3) {
  2176. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  2177. "max_value=%ld)", line, data->name, *dst,
  2178. (long) data->param3);
  2179. *dst = (long) data->param3;
  2180. return -1;
  2181. }
  2182. return 0;
  2183. }
  2184. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  2185. struct wpa_config *config, int line,
  2186. const char *pos)
  2187. {
  2188. size_t len;
  2189. char **dst, *tmp;
  2190. len = os_strlen(pos);
  2191. if (data->param2 && len < (size_t) data->param2) {
  2192. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  2193. "min_len=%ld)", line, data->name,
  2194. (unsigned long) len, (long) data->param2);
  2195. return -1;
  2196. }
  2197. if (data->param3 && len > (size_t) data->param3) {
  2198. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  2199. "max_len=%ld)", line, data->name,
  2200. (unsigned long) len, (long) data->param3);
  2201. return -1;
  2202. }
  2203. tmp = os_strdup(pos);
  2204. if (tmp == NULL)
  2205. return -1;
  2206. dst = (char **) (((u8 *) config) + (long) data->param1);
  2207. os_free(*dst);
  2208. *dst = tmp;
  2209. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  2210. return 0;
  2211. }
  2212. static int wpa_config_process_country(const struct global_parse_data *data,
  2213. struct wpa_config *config, int line,
  2214. const char *pos)
  2215. {
  2216. if (!pos[0] || !pos[1]) {
  2217. wpa_printf(MSG_DEBUG, "Invalid country set");
  2218. return -1;
  2219. }
  2220. config->country[0] = pos[0];
  2221. config->country[1] = pos[1];
  2222. wpa_printf(MSG_DEBUG, "country='%c%c'",
  2223. config->country[0], config->country[1]);
  2224. return 0;
  2225. }
  2226. static int wpa_config_process_load_dynamic_eap(
  2227. const struct global_parse_data *data, struct wpa_config *config,
  2228. int line, const char *so)
  2229. {
  2230. int ret;
  2231. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  2232. ret = eap_peer_method_load(so);
  2233. if (ret == -2) {
  2234. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  2235. "reloading.");
  2236. } else if (ret) {
  2237. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  2238. "method '%s'.", line, so);
  2239. return -1;
  2240. }
  2241. return 0;
  2242. }
  2243. #ifdef CONFIG_WPS
  2244. static int wpa_config_process_uuid(const struct global_parse_data *data,
  2245. struct wpa_config *config, int line,
  2246. const char *pos)
  2247. {
  2248. char buf[40];
  2249. if (uuid_str2bin(pos, config->uuid)) {
  2250. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  2251. return -1;
  2252. }
  2253. uuid_bin2str(config->uuid, buf, sizeof(buf));
  2254. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  2255. return 0;
  2256. }
  2257. static int wpa_config_process_device_type(
  2258. const struct global_parse_data *data,
  2259. struct wpa_config *config, int line, const char *pos)
  2260. {
  2261. return wps_dev_type_str2bin(pos, config->device_type);
  2262. }
  2263. static int wpa_config_process_os_version(const struct global_parse_data *data,
  2264. struct wpa_config *config, int line,
  2265. const char *pos)
  2266. {
  2267. if (hexstr2bin(pos, config->os_version, 4)) {
  2268. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  2269. return -1;
  2270. }
  2271. wpa_printf(MSG_DEBUG, "os_version=%08x",
  2272. WPA_GET_BE32(config->os_version));
  2273. return 0;
  2274. }
  2275. static int wpa_config_process_wps_vendor_ext_m1(
  2276. const struct global_parse_data *data,
  2277. struct wpa_config *config, int line, const char *pos)
  2278. {
  2279. struct wpabuf *tmp;
  2280. int len = os_strlen(pos) / 2;
  2281. u8 *p;
  2282. if (!len) {
  2283. wpa_printf(MSG_ERROR, "Line %d: "
  2284. "invalid wps_vendor_ext_m1", line);
  2285. return -1;
  2286. }
  2287. tmp = wpabuf_alloc(len);
  2288. if (tmp) {
  2289. p = wpabuf_put(tmp, len);
  2290. if (hexstr2bin(pos, p, len)) {
  2291. wpa_printf(MSG_ERROR, "Line %d: "
  2292. "invalid wps_vendor_ext_m1", line);
  2293. wpabuf_free(tmp);
  2294. return -1;
  2295. }
  2296. wpabuf_free(config->wps_vendor_ext_m1);
  2297. config->wps_vendor_ext_m1 = tmp;
  2298. } else {
  2299. wpa_printf(MSG_ERROR, "Can not allocate "
  2300. "memory for wps_vendor_ext_m1");
  2301. return -1;
  2302. }
  2303. return 0;
  2304. }
  2305. #endif /* CONFIG_WPS */
  2306. #ifdef CONFIG_P2P
  2307. static int wpa_config_process_sec_device_type(
  2308. const struct global_parse_data *data,
  2309. struct wpa_config *config, int line, const char *pos)
  2310. {
  2311. int idx;
  2312. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  2313. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  2314. "items", line);
  2315. return -1;
  2316. }
  2317. idx = config->num_sec_device_types;
  2318. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  2319. return -1;
  2320. config->num_sec_device_types++;
  2321. return 0;
  2322. }
  2323. static int wpa_config_process_p2p_pref_chan(
  2324. const struct global_parse_data *data,
  2325. struct wpa_config *config, int line, const char *pos)
  2326. {
  2327. struct p2p_channel *pref = NULL, *n;
  2328. unsigned int num = 0;
  2329. const char *pos2;
  2330. u8 op_class, chan;
  2331. /* format: class:chan,class:chan,... */
  2332. while (*pos) {
  2333. op_class = atoi(pos);
  2334. pos2 = os_strchr(pos, ':');
  2335. if (pos2 == NULL)
  2336. goto fail;
  2337. pos2++;
  2338. chan = atoi(pos2);
  2339. n = os_realloc(pref, (num + 1) * sizeof(struct p2p_channel));
  2340. if (n == NULL)
  2341. goto fail;
  2342. pref = n;
  2343. pref[num].op_class = op_class;
  2344. pref[num].chan = chan;
  2345. num++;
  2346. pos = os_strchr(pos2, ',');
  2347. if (pos == NULL)
  2348. break;
  2349. pos++;
  2350. }
  2351. os_free(config->p2p_pref_chan);
  2352. config->p2p_pref_chan = pref;
  2353. config->num_p2p_pref_chan = num;
  2354. wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
  2355. (u8 *) config->p2p_pref_chan,
  2356. config->num_p2p_pref_chan * sizeof(struct p2p_channel));
  2357. return 0;
  2358. fail:
  2359. os_free(pref);
  2360. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
  2361. return -1;
  2362. }
  2363. #endif /* CONFIG_P2P */
  2364. static int wpa_config_process_hessid(
  2365. const struct global_parse_data *data,
  2366. struct wpa_config *config, int line, const char *pos)
  2367. {
  2368. if (hwaddr_aton2(pos, config->hessid) < 0) {
  2369. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  2370. line, pos);
  2371. return -1;
  2372. }
  2373. return 0;
  2374. }
  2375. #ifdef OFFSET
  2376. #undef OFFSET
  2377. #endif /* OFFSET */
  2378. /* OFFSET: Get offset of a variable within the wpa_config structure */
  2379. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  2380. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  2381. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  2382. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  2383. #define INT(f) _INT(f), NULL, NULL
  2384. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  2385. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  2386. #define STR(f) _STR(f), NULL, NULL
  2387. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  2388. static const struct global_parse_data global_fields[] = {
  2389. #ifdef CONFIG_CTRL_IFACE
  2390. { STR(ctrl_interface), 0 },
  2391. { STR(ctrl_interface_group), 0 } /* deprecated */,
  2392. #endif /* CONFIG_CTRL_IFACE */
  2393. { INT_RANGE(eapol_version, 1, 2), 0 },
  2394. { INT(ap_scan), 0 },
  2395. { INT(fast_reauth), 0 },
  2396. { STR(opensc_engine_path), 0 },
  2397. { STR(pkcs11_engine_path), 0 },
  2398. { STR(pkcs11_module_path), 0 },
  2399. { STR(pcsc_reader), 0 },
  2400. { STR(pcsc_pin), 0 },
  2401. { STR(driver_param), 0 },
  2402. { INT(dot11RSNAConfigPMKLifetime), 0 },
  2403. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  2404. { INT(dot11RSNAConfigSATimeout), 0 },
  2405. #ifndef CONFIG_NO_CONFIG_WRITE
  2406. { INT(update_config), 0 },
  2407. #endif /* CONFIG_NO_CONFIG_WRITE */
  2408. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  2409. #ifdef CONFIG_WPS
  2410. { FUNC(uuid), CFG_CHANGED_UUID },
  2411. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  2412. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  2413. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  2414. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2415. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2416. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  2417. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  2418. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  2419. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  2420. { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
  2421. #endif /* CONFIG_WPS */
  2422. #ifdef CONFIG_P2P
  2423. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  2424. { INT(p2p_listen_reg_class), 0 },
  2425. { INT(p2p_listen_channel), 0 },
  2426. { INT(p2p_oper_reg_class), 0 },
  2427. { INT(p2p_oper_channel), 0 },
  2428. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  2429. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  2430. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  2431. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  2432. { INT(p2p_group_idle), 0 },
  2433. { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
  2434. #endif /* CONFIG_P2P */
  2435. { FUNC(country), CFG_CHANGED_COUNTRY },
  2436. { INT(bss_max_count), 0 },
  2437. { INT(bss_expiration_age), 0 },
  2438. { INT(bss_expiration_scan_count), 0 },
  2439. { INT_RANGE(filter_ssids, 0, 1), 0 },
  2440. { INT(max_num_sta), 0 },
  2441. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  2442. { INT_RANGE(interworking, 0, 1), 0 },
  2443. { FUNC(hessid), 0 },
  2444. { INT_RANGE(access_network_type, 0, 15), 0 }
  2445. };
  2446. #undef FUNC
  2447. #undef _INT
  2448. #undef INT
  2449. #undef INT_RANGE
  2450. #undef _STR
  2451. #undef STR
  2452. #undef STR_RANGE
  2453. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  2454. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  2455. {
  2456. size_t i;
  2457. int ret = 0;
  2458. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  2459. const struct global_parse_data *field = &global_fields[i];
  2460. size_t flen = os_strlen(field->name);
  2461. if (os_strncmp(pos, field->name, flen) != 0 ||
  2462. pos[flen] != '=')
  2463. continue;
  2464. if (field->parser(field, config, line, pos + flen + 1)) {
  2465. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2466. "parse '%s'.", line, pos);
  2467. ret = -1;
  2468. }
  2469. config->changed_parameters |= field->changed_flag;
  2470. break;
  2471. }
  2472. if (i == NUM_GLOBAL_FIELDS) {
  2473. if (line < 0)
  2474. return -1;
  2475. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  2476. line, pos);
  2477. ret = -1;
  2478. }
  2479. return ret;
  2480. }