config.c 92 KB

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