config.c 75 KB

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