config.c 104 KB

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