config.c 77 KB

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