config.c 113 KB

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