config.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773
  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. static int wpa_config_parse_peerkey(const struct parse_data *data,
  1710. struct wpa_ssid *ssid, int line,
  1711. const char *value)
  1712. {
  1713. wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
  1714. return 0;
  1715. }
  1716. #ifndef NO_CONFIG_WRITE
  1717. static char * wpa_config_write_peerkey(const struct parse_data *data,
  1718. struct wpa_ssid *ssid)
  1719. {
  1720. return NULL;
  1721. }
  1722. #endif /* NO_CONFIG_WRITE */
  1723. /* Helper macros for network block parser */
  1724. #ifdef OFFSET
  1725. #undef OFFSET
  1726. #endif /* OFFSET */
  1727. /* OFFSET: Get offset of a variable within the wpa_ssid structure */
  1728. #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
  1729. /* STR: Define a string variable for an ASCII string; f = field name */
  1730. #ifdef NO_CONFIG_WRITE
  1731. #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
  1732. #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
  1733. #else /* NO_CONFIG_WRITE */
  1734. #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
  1735. #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
  1736. #endif /* NO_CONFIG_WRITE */
  1737. #define STR(f) _STR(f), NULL, NULL, NULL, 0
  1738. #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
  1739. #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
  1740. #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
  1741. /* STR_LEN: Define a string variable with a separate variable for storing the
  1742. * data length. Unlike STR(), this can be used to store arbitrary binary data
  1743. * (i.e., even nul termination character). */
  1744. #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
  1745. #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
  1746. #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
  1747. #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
  1748. #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
  1749. /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
  1750. * explicitly specified. */
  1751. #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
  1752. #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
  1753. #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
  1754. #ifdef NO_CONFIG_WRITE
  1755. #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
  1756. #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
  1757. #else /* NO_CONFIG_WRITE */
  1758. #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1759. OFFSET(f), (void *) 0
  1760. #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1761. OFFSET(eap.f), (void *) 0
  1762. #endif /* NO_CONFIG_WRITE */
  1763. /* INT: Define an integer variable */
  1764. #define INT(f) _INT(f), NULL, NULL, 0
  1765. #define INTe(f) _INTe(f), NULL, NULL, 0
  1766. /* INT_RANGE: Define an integer variable with allowed value range */
  1767. #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
  1768. /* FUNC: Define a configuration variable that uses a custom function for
  1769. * parsing and writing the value. */
  1770. #ifdef NO_CONFIG_WRITE
  1771. #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
  1772. #else /* NO_CONFIG_WRITE */
  1773. #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
  1774. NULL, NULL, NULL, NULL
  1775. #endif /* NO_CONFIG_WRITE */
  1776. #define FUNC(f) _FUNC(f), 0
  1777. #define FUNC_KEY(f) _FUNC(f), 1
  1778. /*
  1779. * Table of network configuration variables. This table is used to parse each
  1780. * network configuration variable, e.g., each line in wpa_supplicant.conf file
  1781. * that is inside a network block.
  1782. *
  1783. * This table is generated using the helper macros defined above and with
  1784. * generous help from the C pre-processor. The field name is stored as a string
  1785. * into .name and for STR and INT types, the offset of the target buffer within
  1786. * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
  1787. * offset to the field containing the length of the configuration variable.
  1788. * .param3 and .param4 can be used to mark the allowed range (length for STR
  1789. * and value for INT).
  1790. *
  1791. * For each configuration line in wpa_supplicant.conf, the parser goes through
  1792. * this table and select the entry that matches with the field name. The parser
  1793. * function (.parser) is then called to parse the actual value of the field.
  1794. *
  1795. * This kind of mechanism makes it easy to add new configuration parameters,
  1796. * since only one line needs to be added into this table and into the
  1797. * struct wpa_ssid definition if the new variable is either a string or
  1798. * integer. More complex types will need to use their own parser and writer
  1799. * functions.
  1800. */
  1801. static const struct parse_data ssid_fields[] = {
  1802. { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
  1803. { INT_RANGE(scan_ssid, 0, 1) },
  1804. { FUNC(bssid) },
  1805. { FUNC(bssid_hint) },
  1806. { FUNC(bssid_blacklist) },
  1807. { FUNC(bssid_whitelist) },
  1808. { FUNC_KEY(psk) },
  1809. { INT(mem_only_psk) },
  1810. { STR_KEY(sae_password) },
  1811. { FUNC(proto) },
  1812. { FUNC(key_mgmt) },
  1813. { INT(bg_scan_period) },
  1814. { FUNC(pairwise) },
  1815. { FUNC(group) },
  1816. { FUNC(group_mgmt) },
  1817. { FUNC(auth_alg) },
  1818. { FUNC(scan_freq) },
  1819. { FUNC(freq_list) },
  1820. { INT_RANGE(ht, 0, 1) },
  1821. { INT_RANGE(vht, 0, 1) },
  1822. { INT_RANGE(ht40, -1, 1) },
  1823. { INT_RANGE(max_oper_chwidth, VHT_CHANWIDTH_USE_HT,
  1824. VHT_CHANWIDTH_80P80MHZ) },
  1825. { INT(vht_center_freq1) },
  1826. { INT(vht_center_freq2) },
  1827. #ifdef IEEE8021X_EAPOL
  1828. { FUNC(eap) },
  1829. { STR_LENe(identity) },
  1830. { STR_LENe(anonymous_identity) },
  1831. { STR_LENe(imsi_identity) },
  1832. { FUNC_KEY(password) },
  1833. { STRe(ca_cert) },
  1834. { STRe(ca_path) },
  1835. { STRe(client_cert) },
  1836. { STRe(private_key) },
  1837. { STR_KEYe(private_key_passwd) },
  1838. { STRe(dh_file) },
  1839. { STRe(subject_match) },
  1840. { STRe(altsubject_match) },
  1841. { STRe(domain_suffix_match) },
  1842. { STRe(domain_match) },
  1843. { STRe(ca_cert2) },
  1844. { STRe(ca_path2) },
  1845. { STRe(client_cert2) },
  1846. { STRe(private_key2) },
  1847. { STR_KEYe(private_key2_passwd) },
  1848. { STRe(dh_file2) },
  1849. { STRe(subject_match2) },
  1850. { STRe(altsubject_match2) },
  1851. { STRe(domain_suffix_match2) },
  1852. { STRe(domain_match2) },
  1853. { STRe(phase1) },
  1854. { STRe(phase2) },
  1855. { STRe(pcsc) },
  1856. { STR_KEYe(pin) },
  1857. { STRe(engine_id) },
  1858. { STRe(key_id) },
  1859. { STRe(cert_id) },
  1860. { STRe(ca_cert_id) },
  1861. { STR_KEYe(pin2) },
  1862. { STRe(engine2_id) },
  1863. { STRe(key2_id) },
  1864. { STRe(cert2_id) },
  1865. { STRe(ca_cert2_id) },
  1866. { INTe(engine) },
  1867. { INTe(engine2) },
  1868. { INT(eapol_flags) },
  1869. { INTe(sim_num) },
  1870. { STRe(openssl_ciphers) },
  1871. { INTe(erp) },
  1872. #endif /* IEEE8021X_EAPOL */
  1873. { FUNC_KEY(wep_key0) },
  1874. { FUNC_KEY(wep_key1) },
  1875. { FUNC_KEY(wep_key2) },
  1876. { FUNC_KEY(wep_key3) },
  1877. { INT(wep_tx_keyidx) },
  1878. { INT(priority) },
  1879. #ifdef IEEE8021X_EAPOL
  1880. { INT(eap_workaround) },
  1881. { STRe(pac_file) },
  1882. { INTe(fragment_size) },
  1883. { INTe(ocsp) },
  1884. #endif /* IEEE8021X_EAPOL */
  1885. #ifdef CONFIG_MESH
  1886. { INT_RANGE(mode, 0, 5) },
  1887. { INT_RANGE(no_auto_peer, 0, 1) },
  1888. { INT_RANGE(mesh_rssi_threshold, -255, 1) },
  1889. #else /* CONFIG_MESH */
  1890. { INT_RANGE(mode, 0, 4) },
  1891. #endif /* CONFIG_MESH */
  1892. { INT_RANGE(proactive_key_caching, 0, 1) },
  1893. { INT_RANGE(disabled, 0, 2) },
  1894. { STR(id_str) },
  1895. #ifdef CONFIG_IEEE80211W
  1896. { INT_RANGE(ieee80211w, 0, 2) },
  1897. #endif /* CONFIG_IEEE80211W */
  1898. { FUNC(peerkey) /* obsolete - removed */ },
  1899. { INT_RANGE(mixed_cell, 0, 1) },
  1900. { INT_RANGE(frequency, 0, 65000) },
  1901. { INT_RANGE(fixed_freq, 0, 1) },
  1902. #ifdef CONFIG_ACS
  1903. { INT_RANGE(acs, 0, 1) },
  1904. #endif /* CONFIG_ACS */
  1905. #ifdef CONFIG_MESH
  1906. { FUNC(mesh_basic_rates) },
  1907. { INT(dot11MeshMaxRetries) },
  1908. { INT(dot11MeshRetryTimeout) },
  1909. { INT(dot11MeshConfirmTimeout) },
  1910. { INT(dot11MeshHoldingTimeout) },
  1911. #endif /* CONFIG_MESH */
  1912. { INT(wpa_ptk_rekey) },
  1913. { INT(group_rekey) },
  1914. { STR(bgscan) },
  1915. { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
  1916. #ifdef CONFIG_P2P
  1917. { FUNC(go_p2p_dev_addr) },
  1918. { FUNC(p2p_client_list) },
  1919. { FUNC(psk_list) },
  1920. #endif /* CONFIG_P2P */
  1921. #ifdef CONFIG_HT_OVERRIDES
  1922. { INT_RANGE(disable_ht, 0, 1) },
  1923. { INT_RANGE(disable_ht40, -1, 1) },
  1924. { INT_RANGE(disable_sgi, 0, 1) },
  1925. { INT_RANGE(disable_ldpc, 0, 1) },
  1926. { INT_RANGE(ht40_intolerant, 0, 1) },
  1927. { INT_RANGE(disable_max_amsdu, -1, 1) },
  1928. { INT_RANGE(ampdu_factor, -1, 3) },
  1929. { INT_RANGE(ampdu_density, -1, 7) },
  1930. { STR(ht_mcs) },
  1931. #endif /* CONFIG_HT_OVERRIDES */
  1932. #ifdef CONFIG_VHT_OVERRIDES
  1933. { INT_RANGE(disable_vht, 0, 1) },
  1934. { INT(vht_capa) },
  1935. { INT(vht_capa_mask) },
  1936. { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
  1937. { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
  1938. { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
  1939. { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
  1940. { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
  1941. { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
  1942. { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
  1943. { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
  1944. { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
  1945. { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
  1946. { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
  1947. { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
  1948. { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
  1949. { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
  1950. { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
  1951. { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
  1952. #endif /* CONFIG_VHT_OVERRIDES */
  1953. { INT(ap_max_inactivity) },
  1954. { INT(dtim_period) },
  1955. { INT(beacon_int) },
  1956. #ifdef CONFIG_MACSEC
  1957. { INT_RANGE(macsec_policy, 0, 1) },
  1958. { INT_RANGE(macsec_integ_only, 0, 1) },
  1959. { INT_RANGE(macsec_port, 1, 65534) },
  1960. { INT_RANGE(mka_priority, 0, 255) },
  1961. { FUNC_KEY(mka_cak) },
  1962. { FUNC_KEY(mka_ckn) },
  1963. #endif /* CONFIG_MACSEC */
  1964. #ifdef CONFIG_HS20
  1965. { INT(update_identifier) },
  1966. #endif /* CONFIG_HS20 */
  1967. { INT_RANGE(mac_addr, 0, 2) },
  1968. { INT_RANGE(pbss, 0, 2) },
  1969. { INT_RANGE(wps_disabled, 0, 1) },
  1970. { INT_RANGE(fils_dh_group, 0, 65535) },
  1971. #ifdef CONFIG_DPP
  1972. { STR(dpp_connector) },
  1973. { STR_LEN(dpp_netaccesskey) },
  1974. { INT(dpp_netaccesskey_expiry) },
  1975. { STR_LEN(dpp_csign) },
  1976. #endif /* CONFIG_DPP */
  1977. { INT_RANGE(owe_group, 0, 65535) },
  1978. { INT_RANGE(owe_only, 0, 1) },
  1979. };
  1980. #undef OFFSET
  1981. #undef _STR
  1982. #undef STR
  1983. #undef STR_KEY
  1984. #undef _STR_LEN
  1985. #undef STR_LEN
  1986. #undef STR_LEN_KEY
  1987. #undef _STR_RANGE
  1988. #undef STR_RANGE
  1989. #undef STR_RANGE_KEY
  1990. #undef _INT
  1991. #undef INT
  1992. #undef INT_RANGE
  1993. #undef _FUNC
  1994. #undef FUNC
  1995. #undef FUNC_KEY
  1996. #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
  1997. /**
  1998. * wpa_config_add_prio_network - Add a network to priority lists
  1999. * @config: Configuration data from wpa_config_read()
  2000. * @ssid: Pointer to the network configuration to be added to the list
  2001. * Returns: 0 on success, -1 on failure
  2002. *
  2003. * This function is used to add a network block to the priority list of
  2004. * networks. This must be called for each network when reading in the full
  2005. * configuration. In addition, this can be used indirectly when updating
  2006. * priorities by calling wpa_config_update_prio_list().
  2007. */
  2008. int wpa_config_add_prio_network(struct wpa_config *config,
  2009. struct wpa_ssid *ssid)
  2010. {
  2011. int prio;
  2012. struct wpa_ssid *prev, **nlist;
  2013. /*
  2014. * Add to an existing priority list if one is available for the
  2015. * configured priority level for this network.
  2016. */
  2017. for (prio = 0; prio < config->num_prio; prio++) {
  2018. prev = config->pssid[prio];
  2019. if (prev->priority == ssid->priority) {
  2020. while (prev->pnext)
  2021. prev = prev->pnext;
  2022. prev->pnext = ssid;
  2023. return 0;
  2024. }
  2025. }
  2026. /* First network for this priority - add a new priority list */
  2027. nlist = os_realloc_array(config->pssid, config->num_prio + 1,
  2028. sizeof(struct wpa_ssid *));
  2029. if (nlist == NULL)
  2030. return -1;
  2031. for (prio = 0; prio < config->num_prio; prio++) {
  2032. if (nlist[prio]->priority < ssid->priority) {
  2033. os_memmove(&nlist[prio + 1], &nlist[prio],
  2034. (config->num_prio - prio) *
  2035. sizeof(struct wpa_ssid *));
  2036. break;
  2037. }
  2038. }
  2039. nlist[prio] = ssid;
  2040. config->num_prio++;
  2041. config->pssid = nlist;
  2042. return 0;
  2043. }
  2044. /**
  2045. * wpa_config_update_prio_list - Update network priority list
  2046. * @config: Configuration data from wpa_config_read()
  2047. * Returns: 0 on success, -1 on failure
  2048. *
  2049. * This function is called to update the priority list of networks in the
  2050. * configuration when a network is being added or removed. This is also called
  2051. * if a priority for a network is changed.
  2052. */
  2053. int wpa_config_update_prio_list(struct wpa_config *config)
  2054. {
  2055. struct wpa_ssid *ssid;
  2056. int ret = 0;
  2057. os_free(config->pssid);
  2058. config->pssid = NULL;
  2059. config->num_prio = 0;
  2060. ssid = config->ssid;
  2061. while (ssid) {
  2062. ssid->pnext = NULL;
  2063. if (wpa_config_add_prio_network(config, ssid) < 0)
  2064. ret = -1;
  2065. ssid = ssid->next;
  2066. }
  2067. return ret;
  2068. }
  2069. #ifdef IEEE8021X_EAPOL
  2070. static void eap_peer_config_free(struct eap_peer_config *eap)
  2071. {
  2072. os_free(eap->eap_methods);
  2073. bin_clear_free(eap->identity, eap->identity_len);
  2074. os_free(eap->anonymous_identity);
  2075. os_free(eap->imsi_identity);
  2076. bin_clear_free(eap->password, eap->password_len);
  2077. os_free(eap->ca_cert);
  2078. os_free(eap->ca_path);
  2079. os_free(eap->client_cert);
  2080. os_free(eap->private_key);
  2081. str_clear_free(eap->private_key_passwd);
  2082. os_free(eap->dh_file);
  2083. os_free(eap->subject_match);
  2084. os_free(eap->altsubject_match);
  2085. os_free(eap->domain_suffix_match);
  2086. os_free(eap->domain_match);
  2087. os_free(eap->ca_cert2);
  2088. os_free(eap->ca_path2);
  2089. os_free(eap->client_cert2);
  2090. os_free(eap->private_key2);
  2091. str_clear_free(eap->private_key2_passwd);
  2092. os_free(eap->dh_file2);
  2093. os_free(eap->subject_match2);
  2094. os_free(eap->altsubject_match2);
  2095. os_free(eap->domain_suffix_match2);
  2096. os_free(eap->domain_match2);
  2097. os_free(eap->phase1);
  2098. os_free(eap->phase2);
  2099. os_free(eap->pcsc);
  2100. str_clear_free(eap->pin);
  2101. os_free(eap->engine_id);
  2102. os_free(eap->key_id);
  2103. os_free(eap->cert_id);
  2104. os_free(eap->ca_cert_id);
  2105. os_free(eap->key2_id);
  2106. os_free(eap->cert2_id);
  2107. os_free(eap->ca_cert2_id);
  2108. str_clear_free(eap->pin2);
  2109. os_free(eap->engine2_id);
  2110. os_free(eap->otp);
  2111. os_free(eap->pending_req_otp);
  2112. os_free(eap->pac_file);
  2113. bin_clear_free(eap->new_password, eap->new_password_len);
  2114. str_clear_free(eap->external_sim_resp);
  2115. os_free(eap->openssl_ciphers);
  2116. }
  2117. #endif /* IEEE8021X_EAPOL */
  2118. /**
  2119. * wpa_config_free_ssid - Free network/ssid configuration data
  2120. * @ssid: Configuration data for the network
  2121. *
  2122. * This function frees all resources allocated for the network configuration
  2123. * data.
  2124. */
  2125. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  2126. {
  2127. struct psk_list_entry *psk;
  2128. os_free(ssid->ssid);
  2129. str_clear_free(ssid->passphrase);
  2130. os_free(ssid->ext_psk);
  2131. str_clear_free(ssid->sae_password);
  2132. #ifdef IEEE8021X_EAPOL
  2133. eap_peer_config_free(&ssid->eap);
  2134. #endif /* IEEE8021X_EAPOL */
  2135. os_free(ssid->id_str);
  2136. os_free(ssid->scan_freq);
  2137. os_free(ssid->freq_list);
  2138. os_free(ssid->bgscan);
  2139. os_free(ssid->p2p_client_list);
  2140. os_free(ssid->bssid_blacklist);
  2141. os_free(ssid->bssid_whitelist);
  2142. #ifdef CONFIG_HT_OVERRIDES
  2143. os_free(ssid->ht_mcs);
  2144. #endif /* CONFIG_HT_OVERRIDES */
  2145. #ifdef CONFIG_MESH
  2146. os_free(ssid->mesh_basic_rates);
  2147. #endif /* CONFIG_MESH */
  2148. os_free(ssid->dpp_connector);
  2149. bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
  2150. os_free(ssid->dpp_csign);
  2151. while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
  2152. list))) {
  2153. dl_list_del(&psk->list);
  2154. bin_clear_free(psk, sizeof(*psk));
  2155. }
  2156. bin_clear_free(ssid, sizeof(*ssid));
  2157. }
  2158. void wpa_config_free_cred(struct wpa_cred *cred)
  2159. {
  2160. size_t i;
  2161. os_free(cred->realm);
  2162. str_clear_free(cred->username);
  2163. str_clear_free(cred->password);
  2164. os_free(cred->ca_cert);
  2165. os_free(cred->client_cert);
  2166. os_free(cred->private_key);
  2167. str_clear_free(cred->private_key_passwd);
  2168. os_free(cred->imsi);
  2169. str_clear_free(cred->milenage);
  2170. for (i = 0; i < cred->num_domain; i++)
  2171. os_free(cred->domain[i]);
  2172. os_free(cred->domain);
  2173. os_free(cred->domain_suffix_match);
  2174. os_free(cred->eap_method);
  2175. os_free(cred->phase1);
  2176. os_free(cred->phase2);
  2177. os_free(cred->excluded_ssid);
  2178. os_free(cred->roaming_partner);
  2179. os_free(cred->provisioning_sp);
  2180. for (i = 0; i < cred->num_req_conn_capab; i++)
  2181. os_free(cred->req_conn_capab_port[i]);
  2182. os_free(cred->req_conn_capab_port);
  2183. os_free(cred->req_conn_capab_proto);
  2184. os_free(cred);
  2185. }
  2186. void wpa_config_flush_blobs(struct wpa_config *config)
  2187. {
  2188. #ifndef CONFIG_NO_CONFIG_BLOBS
  2189. struct wpa_config_blob *blob, *prev;
  2190. blob = config->blobs;
  2191. config->blobs = NULL;
  2192. while (blob) {
  2193. prev = blob;
  2194. blob = blob->next;
  2195. wpa_config_free_blob(prev);
  2196. }
  2197. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2198. }
  2199. /**
  2200. * wpa_config_free - Free configuration data
  2201. * @config: Configuration data from wpa_config_read()
  2202. *
  2203. * This function frees all resources allocated for the configuration data by
  2204. * wpa_config_read().
  2205. */
  2206. void wpa_config_free(struct wpa_config *config)
  2207. {
  2208. struct wpa_ssid *ssid, *prev = NULL;
  2209. struct wpa_cred *cred, *cprev;
  2210. int i;
  2211. ssid = config->ssid;
  2212. while (ssid) {
  2213. prev = ssid;
  2214. ssid = ssid->next;
  2215. wpa_config_free_ssid(prev);
  2216. }
  2217. cred = config->cred;
  2218. while (cred) {
  2219. cprev = cred;
  2220. cred = cred->next;
  2221. wpa_config_free_cred(cprev);
  2222. }
  2223. wpa_config_flush_blobs(config);
  2224. wpabuf_free(config->wps_vendor_ext_m1);
  2225. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
  2226. wpabuf_free(config->wps_vendor_ext[i]);
  2227. os_free(config->ctrl_interface);
  2228. os_free(config->ctrl_interface_group);
  2229. os_free(config->opensc_engine_path);
  2230. os_free(config->pkcs11_engine_path);
  2231. os_free(config->pkcs11_module_path);
  2232. os_free(config->openssl_ciphers);
  2233. os_free(config->pcsc_reader);
  2234. str_clear_free(config->pcsc_pin);
  2235. os_free(config->driver_param);
  2236. os_free(config->device_name);
  2237. os_free(config->manufacturer);
  2238. os_free(config->model_name);
  2239. os_free(config->model_number);
  2240. os_free(config->serial_number);
  2241. os_free(config->config_methods);
  2242. os_free(config->p2p_ssid_postfix);
  2243. os_free(config->pssid);
  2244. os_free(config->p2p_pref_chan);
  2245. os_free(config->p2p_no_go_freq.range);
  2246. os_free(config->autoscan);
  2247. os_free(config->freq_list);
  2248. wpabuf_free(config->wps_nfc_dh_pubkey);
  2249. wpabuf_free(config->wps_nfc_dh_privkey);
  2250. wpabuf_free(config->wps_nfc_dev_pw);
  2251. os_free(config->ext_password_backend);
  2252. os_free(config->sae_groups);
  2253. wpabuf_free(config->ap_vendor_elements);
  2254. os_free(config->osu_dir);
  2255. os_free(config->bgscan);
  2256. os_free(config->wowlan_triggers);
  2257. os_free(config->fst_group_id);
  2258. os_free(config->sched_scan_plans);
  2259. #ifdef CONFIG_MBO
  2260. os_free(config->non_pref_chan);
  2261. #endif /* CONFIG_MBO */
  2262. os_free(config);
  2263. }
  2264. /**
  2265. * wpa_config_foreach_network - Iterate over each configured network
  2266. * @config: Configuration data from wpa_config_read()
  2267. * @func: Callback function to process each network
  2268. * @arg: Opaque argument to pass to callback function
  2269. *
  2270. * Iterate over the set of configured networks calling the specified
  2271. * function for each item. We guard against callbacks removing the
  2272. * supplied network.
  2273. */
  2274. void wpa_config_foreach_network(struct wpa_config *config,
  2275. void (*func)(void *, struct wpa_ssid *),
  2276. void *arg)
  2277. {
  2278. struct wpa_ssid *ssid, *next;
  2279. ssid = config->ssid;
  2280. while (ssid) {
  2281. next = ssid->next;
  2282. func(arg, ssid);
  2283. ssid = next;
  2284. }
  2285. }
  2286. /**
  2287. * wpa_config_get_network - Get configured network based on id
  2288. * @config: Configuration data from wpa_config_read()
  2289. * @id: Unique network id to search for
  2290. * Returns: Network configuration or %NULL if not found
  2291. */
  2292. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  2293. {
  2294. struct wpa_ssid *ssid;
  2295. ssid = config->ssid;
  2296. while (ssid) {
  2297. if (id == ssid->id)
  2298. break;
  2299. ssid = ssid->next;
  2300. }
  2301. return ssid;
  2302. }
  2303. /**
  2304. * wpa_config_add_network - Add a new network with empty configuration
  2305. * @config: Configuration data from wpa_config_read()
  2306. * Returns: The new network configuration or %NULL if operation failed
  2307. */
  2308. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  2309. {
  2310. int id;
  2311. struct wpa_ssid *ssid, *last = NULL;
  2312. id = -1;
  2313. ssid = config->ssid;
  2314. while (ssid) {
  2315. if (ssid->id > id)
  2316. id = ssid->id;
  2317. last = ssid;
  2318. ssid = ssid->next;
  2319. }
  2320. id++;
  2321. ssid = os_zalloc(sizeof(*ssid));
  2322. if (ssid == NULL)
  2323. return NULL;
  2324. ssid->id = id;
  2325. dl_list_init(&ssid->psk_list);
  2326. if (last)
  2327. last->next = ssid;
  2328. else
  2329. config->ssid = ssid;
  2330. wpa_config_update_prio_list(config);
  2331. return ssid;
  2332. }
  2333. /**
  2334. * wpa_config_remove_network - Remove a configured network based on id
  2335. * @config: Configuration data from wpa_config_read()
  2336. * @id: Unique network id to search for
  2337. * Returns: 0 on success, or -1 if the network was not found
  2338. */
  2339. int wpa_config_remove_network(struct wpa_config *config, int id)
  2340. {
  2341. struct wpa_ssid *ssid, *prev = NULL;
  2342. ssid = config->ssid;
  2343. while (ssid) {
  2344. if (id == ssid->id)
  2345. break;
  2346. prev = ssid;
  2347. ssid = ssid->next;
  2348. }
  2349. if (ssid == NULL)
  2350. return -1;
  2351. if (prev)
  2352. prev->next = ssid->next;
  2353. else
  2354. config->ssid = ssid->next;
  2355. wpa_config_update_prio_list(config);
  2356. wpa_config_free_ssid(ssid);
  2357. return 0;
  2358. }
  2359. /**
  2360. * wpa_config_set_network_defaults - Set network default values
  2361. * @ssid: Pointer to network configuration data
  2362. */
  2363. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  2364. {
  2365. ssid->proto = DEFAULT_PROTO;
  2366. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  2367. ssid->group_cipher = DEFAULT_GROUP;
  2368. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  2369. ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
  2370. ssid->ht = 1;
  2371. #ifdef IEEE8021X_EAPOL
  2372. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  2373. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  2374. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  2375. ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
  2376. #endif /* IEEE8021X_EAPOL */
  2377. #ifdef CONFIG_MESH
  2378. ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
  2379. ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
  2380. ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
  2381. ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
  2382. ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
  2383. #endif /* CONFIG_MESH */
  2384. #ifdef CONFIG_HT_OVERRIDES
  2385. ssid->disable_ht = DEFAULT_DISABLE_HT;
  2386. ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
  2387. ssid->disable_sgi = DEFAULT_DISABLE_SGI;
  2388. ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
  2389. ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
  2390. ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
  2391. ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
  2392. #endif /* CONFIG_HT_OVERRIDES */
  2393. #ifdef CONFIG_VHT_OVERRIDES
  2394. ssid->vht_rx_mcs_nss_1 = -1;
  2395. ssid->vht_rx_mcs_nss_2 = -1;
  2396. ssid->vht_rx_mcs_nss_3 = -1;
  2397. ssid->vht_rx_mcs_nss_4 = -1;
  2398. ssid->vht_rx_mcs_nss_5 = -1;
  2399. ssid->vht_rx_mcs_nss_6 = -1;
  2400. ssid->vht_rx_mcs_nss_7 = -1;
  2401. ssid->vht_rx_mcs_nss_8 = -1;
  2402. ssid->vht_tx_mcs_nss_1 = -1;
  2403. ssid->vht_tx_mcs_nss_2 = -1;
  2404. ssid->vht_tx_mcs_nss_3 = -1;
  2405. ssid->vht_tx_mcs_nss_4 = -1;
  2406. ssid->vht_tx_mcs_nss_5 = -1;
  2407. ssid->vht_tx_mcs_nss_6 = -1;
  2408. ssid->vht_tx_mcs_nss_7 = -1;
  2409. ssid->vht_tx_mcs_nss_8 = -1;
  2410. #endif /* CONFIG_VHT_OVERRIDES */
  2411. ssid->proactive_key_caching = -1;
  2412. #ifdef CONFIG_IEEE80211W
  2413. ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
  2414. #endif /* CONFIG_IEEE80211W */
  2415. #ifdef CONFIG_MACSEC
  2416. ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
  2417. #endif /* CONFIG_MACSEC */
  2418. ssid->mac_addr = -1;
  2419. }
  2420. /**
  2421. * wpa_config_set - Set a variable in network configuration
  2422. * @ssid: Pointer to network configuration data
  2423. * @var: Variable name, e.g., "ssid"
  2424. * @value: Variable value
  2425. * @line: Line number in configuration file or 0 if not used
  2426. * Returns: 0 on success with possible change in the value, 1 on success with
  2427. * no change to previously configured value, or -1 on failure
  2428. *
  2429. * This function can be used to set network configuration variables based on
  2430. * both the configuration file and management interface input. The value
  2431. * parameter must be in the same format as the text-based configuration file is
  2432. * using. For example, strings are using double quotation marks.
  2433. */
  2434. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  2435. int line)
  2436. {
  2437. size_t i;
  2438. int ret = 0;
  2439. if (ssid == NULL || var == NULL || value == NULL)
  2440. return -1;
  2441. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2442. const struct parse_data *field = &ssid_fields[i];
  2443. if (os_strcmp(var, field->name) != 0)
  2444. continue;
  2445. ret = field->parser(field, ssid, line, value);
  2446. if (ret < 0) {
  2447. if (line) {
  2448. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2449. "parse %s '%s'.", line, var, value);
  2450. }
  2451. ret = -1;
  2452. }
  2453. break;
  2454. }
  2455. if (i == NUM_SSID_FIELDS) {
  2456. if (line) {
  2457. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  2458. "'%s'.", line, var);
  2459. }
  2460. ret = -1;
  2461. }
  2462. return ret;
  2463. }
  2464. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  2465. const char *value)
  2466. {
  2467. size_t len;
  2468. char *buf;
  2469. int ret;
  2470. len = os_strlen(value);
  2471. buf = os_malloc(len + 3);
  2472. if (buf == NULL)
  2473. return -1;
  2474. buf[0] = '"';
  2475. os_memcpy(buf + 1, value, len);
  2476. buf[len + 1] = '"';
  2477. buf[len + 2] = '\0';
  2478. ret = wpa_config_set(ssid, var, buf, 0);
  2479. os_free(buf);
  2480. return ret;
  2481. }
  2482. /**
  2483. * wpa_config_get_all - Get all options from network configuration
  2484. * @ssid: Pointer to network configuration data
  2485. * @get_keys: Determines if keys/passwords will be included in returned list
  2486. * (if they may be exported)
  2487. * Returns: %NULL terminated list of all set keys and their values in the form
  2488. * of [key1, val1, key2, val2, ... , NULL]
  2489. *
  2490. * This function can be used to get list of all configured network properties.
  2491. * The caller is responsible for freeing the returned list and all its
  2492. * elements.
  2493. */
  2494. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  2495. {
  2496. #ifdef NO_CONFIG_WRITE
  2497. return NULL;
  2498. #else /* NO_CONFIG_WRITE */
  2499. const struct parse_data *field;
  2500. char *key, *value;
  2501. size_t i;
  2502. char **props;
  2503. int fields_num;
  2504. get_keys = get_keys && ssid->export_keys;
  2505. props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
  2506. if (!props)
  2507. return NULL;
  2508. fields_num = 0;
  2509. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2510. field = &ssid_fields[i];
  2511. if (field->key_data && !get_keys)
  2512. continue;
  2513. value = field->writer(field, ssid);
  2514. if (value == NULL)
  2515. continue;
  2516. if (os_strlen(value) == 0) {
  2517. os_free(value);
  2518. continue;
  2519. }
  2520. key = os_strdup(field->name);
  2521. if (key == NULL) {
  2522. os_free(value);
  2523. goto err;
  2524. }
  2525. props[fields_num * 2] = key;
  2526. props[fields_num * 2 + 1] = value;
  2527. fields_num++;
  2528. }
  2529. return props;
  2530. err:
  2531. for (i = 0; props[i]; i++)
  2532. os_free(props[i]);
  2533. os_free(props);
  2534. return NULL;
  2535. #endif /* NO_CONFIG_WRITE */
  2536. }
  2537. #ifndef NO_CONFIG_WRITE
  2538. /**
  2539. * wpa_config_get - Get a variable in network configuration
  2540. * @ssid: Pointer to network configuration data
  2541. * @var: Variable name, e.g., "ssid"
  2542. * Returns: Value of the variable or %NULL on failure
  2543. *
  2544. * This function can be used to get network configuration variables. The
  2545. * returned value is a copy of the configuration variable in text format, i.e,.
  2546. * the same format that the text-based configuration file and wpa_config_set()
  2547. * are using for the value. The caller is responsible for freeing the returned
  2548. * value.
  2549. */
  2550. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  2551. {
  2552. size_t i;
  2553. if (ssid == NULL || var == NULL)
  2554. return NULL;
  2555. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2556. const struct parse_data *field = &ssid_fields[i];
  2557. if (os_strcmp(var, field->name) == 0) {
  2558. char *ret = field->writer(field, ssid);
  2559. if (ret && has_newline(ret)) {
  2560. wpa_printf(MSG_ERROR,
  2561. "Found newline in value for %s; not returning it",
  2562. var);
  2563. os_free(ret);
  2564. ret = NULL;
  2565. }
  2566. return ret;
  2567. }
  2568. }
  2569. return NULL;
  2570. }
  2571. /**
  2572. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  2573. * @ssid: Pointer to network configuration data
  2574. * @var: Variable name, e.g., "ssid"
  2575. * Returns: Value of the variable or %NULL on failure
  2576. *
  2577. * This function can be used to get network configuration variable like
  2578. * wpa_config_get(). The only difference is that this functions does not expose
  2579. * key/password material from the configuration. In case a key/password field
  2580. * is requested, the returned value is an empty string or %NULL if the variable
  2581. * is not set or "*" if the variable is set (regardless of its value). The
  2582. * returned value is a copy of the configuration variable in text format, i.e,.
  2583. * the same format that the text-based configuration file and wpa_config_set()
  2584. * are using for the value. The caller is responsible for freeing the returned
  2585. * value.
  2586. */
  2587. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  2588. {
  2589. size_t i;
  2590. if (ssid == NULL || var == NULL)
  2591. return NULL;
  2592. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2593. const struct parse_data *field = &ssid_fields[i];
  2594. if (os_strcmp(var, field->name) == 0) {
  2595. char *res = field->writer(field, ssid);
  2596. if (field->key_data) {
  2597. if (res && res[0]) {
  2598. wpa_printf(MSG_DEBUG, "Do not allow "
  2599. "key_data field to be "
  2600. "exposed");
  2601. str_clear_free(res);
  2602. return os_strdup("*");
  2603. }
  2604. os_free(res);
  2605. return NULL;
  2606. }
  2607. return res;
  2608. }
  2609. }
  2610. return NULL;
  2611. }
  2612. #endif /* NO_CONFIG_WRITE */
  2613. /**
  2614. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  2615. * @ssid: Pointer to network configuration data
  2616. *
  2617. * This function must be called to update WPA PSK when either SSID or the
  2618. * passphrase has changed for the network configuration.
  2619. */
  2620. void wpa_config_update_psk(struct wpa_ssid *ssid)
  2621. {
  2622. #ifndef CONFIG_NO_PBKDF2
  2623. pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
  2624. ssid->psk, PMK_LEN);
  2625. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  2626. ssid->psk, PMK_LEN);
  2627. ssid->psk_set = 1;
  2628. #endif /* CONFIG_NO_PBKDF2 */
  2629. }
  2630. static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
  2631. const char *value)
  2632. {
  2633. u8 *proto;
  2634. int **port;
  2635. int *ports, *nports;
  2636. const char *pos;
  2637. unsigned int num_ports;
  2638. proto = os_realloc_array(cred->req_conn_capab_proto,
  2639. cred->num_req_conn_capab + 1, sizeof(u8));
  2640. if (proto == NULL)
  2641. return -1;
  2642. cred->req_conn_capab_proto = proto;
  2643. port = os_realloc_array(cred->req_conn_capab_port,
  2644. cred->num_req_conn_capab + 1, sizeof(int *));
  2645. if (port == NULL)
  2646. return -1;
  2647. cred->req_conn_capab_port = port;
  2648. proto[cred->num_req_conn_capab] = atoi(value);
  2649. pos = os_strchr(value, ':');
  2650. if (pos == NULL) {
  2651. port[cred->num_req_conn_capab] = NULL;
  2652. cred->num_req_conn_capab++;
  2653. return 0;
  2654. }
  2655. pos++;
  2656. ports = NULL;
  2657. num_ports = 0;
  2658. while (*pos) {
  2659. nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
  2660. if (nports == NULL) {
  2661. os_free(ports);
  2662. return -1;
  2663. }
  2664. ports = nports;
  2665. ports[num_ports++] = atoi(pos);
  2666. pos = os_strchr(pos, ',');
  2667. if (pos == NULL)
  2668. break;
  2669. pos++;
  2670. }
  2671. nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
  2672. if (nports == NULL) {
  2673. os_free(ports);
  2674. return -1;
  2675. }
  2676. ports = nports;
  2677. ports[num_ports] = -1;
  2678. port[cred->num_req_conn_capab] = ports;
  2679. cred->num_req_conn_capab++;
  2680. return 0;
  2681. }
  2682. int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
  2683. const char *value, int line)
  2684. {
  2685. char *val;
  2686. size_t len;
  2687. if (os_strcmp(var, "temporary") == 0) {
  2688. cred->temporary = atoi(value);
  2689. return 0;
  2690. }
  2691. if (os_strcmp(var, "priority") == 0) {
  2692. cred->priority = atoi(value);
  2693. return 0;
  2694. }
  2695. if (os_strcmp(var, "sp_priority") == 0) {
  2696. int prio = atoi(value);
  2697. if (prio < 0 || prio > 255)
  2698. return -1;
  2699. cred->sp_priority = prio;
  2700. return 0;
  2701. }
  2702. if (os_strcmp(var, "pcsc") == 0) {
  2703. cred->pcsc = atoi(value);
  2704. return 0;
  2705. }
  2706. if (os_strcmp(var, "eap") == 0) {
  2707. struct eap_method_type method;
  2708. method.method = eap_peer_get_type(value, &method.vendor);
  2709. if (method.vendor == EAP_VENDOR_IETF &&
  2710. method.method == EAP_TYPE_NONE) {
  2711. wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
  2712. "for a credential", line, value);
  2713. return -1;
  2714. }
  2715. os_free(cred->eap_method);
  2716. cred->eap_method = os_malloc(sizeof(*cred->eap_method));
  2717. if (cred->eap_method == NULL)
  2718. return -1;
  2719. os_memcpy(cred->eap_method, &method, sizeof(method));
  2720. return 0;
  2721. }
  2722. if (os_strcmp(var, "password") == 0 &&
  2723. os_strncmp(value, "ext:", 4) == 0) {
  2724. if (has_newline(value))
  2725. return -1;
  2726. str_clear_free(cred->password);
  2727. cred->password = os_strdup(value);
  2728. cred->ext_password = 1;
  2729. return 0;
  2730. }
  2731. if (os_strcmp(var, "update_identifier") == 0) {
  2732. cred->update_identifier = atoi(value);
  2733. return 0;
  2734. }
  2735. if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
  2736. cred->min_dl_bandwidth_home = atoi(value);
  2737. return 0;
  2738. }
  2739. if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
  2740. cred->min_ul_bandwidth_home = atoi(value);
  2741. return 0;
  2742. }
  2743. if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
  2744. cred->min_dl_bandwidth_roaming = atoi(value);
  2745. return 0;
  2746. }
  2747. if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
  2748. cred->min_ul_bandwidth_roaming = atoi(value);
  2749. return 0;
  2750. }
  2751. if (os_strcmp(var, "max_bss_load") == 0) {
  2752. cred->max_bss_load = atoi(value);
  2753. return 0;
  2754. }
  2755. if (os_strcmp(var, "req_conn_capab") == 0)
  2756. return wpa_config_set_cred_req_conn_capab(cred, value);
  2757. if (os_strcmp(var, "ocsp") == 0) {
  2758. cred->ocsp = atoi(value);
  2759. return 0;
  2760. }
  2761. if (os_strcmp(var, "sim_num") == 0) {
  2762. cred->sim_num = atoi(value);
  2763. return 0;
  2764. }
  2765. val = wpa_config_parse_string(value, &len);
  2766. if (val == NULL ||
  2767. (os_strcmp(var, "excluded_ssid") != 0 &&
  2768. os_strcmp(var, "roaming_consortium") != 0 &&
  2769. os_strcmp(var, "required_roaming_consortium") != 0 &&
  2770. has_newline(val))) {
  2771. wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
  2772. "value '%s'.", line, var, value);
  2773. os_free(val);
  2774. return -1;
  2775. }
  2776. if (os_strcmp(var, "realm") == 0) {
  2777. os_free(cred->realm);
  2778. cred->realm = val;
  2779. return 0;
  2780. }
  2781. if (os_strcmp(var, "username") == 0) {
  2782. str_clear_free(cred->username);
  2783. cred->username = val;
  2784. return 0;
  2785. }
  2786. if (os_strcmp(var, "password") == 0) {
  2787. str_clear_free(cred->password);
  2788. cred->password = val;
  2789. cred->ext_password = 0;
  2790. return 0;
  2791. }
  2792. if (os_strcmp(var, "ca_cert") == 0) {
  2793. os_free(cred->ca_cert);
  2794. cred->ca_cert = val;
  2795. return 0;
  2796. }
  2797. if (os_strcmp(var, "client_cert") == 0) {
  2798. os_free(cred->client_cert);
  2799. cred->client_cert = val;
  2800. return 0;
  2801. }
  2802. if (os_strcmp(var, "private_key") == 0) {
  2803. os_free(cred->private_key);
  2804. cred->private_key = val;
  2805. return 0;
  2806. }
  2807. if (os_strcmp(var, "private_key_passwd") == 0) {
  2808. str_clear_free(cred->private_key_passwd);
  2809. cred->private_key_passwd = val;
  2810. return 0;
  2811. }
  2812. if (os_strcmp(var, "imsi") == 0) {
  2813. os_free(cred->imsi);
  2814. cred->imsi = val;
  2815. return 0;
  2816. }
  2817. if (os_strcmp(var, "milenage") == 0) {
  2818. str_clear_free(cred->milenage);
  2819. cred->milenage = val;
  2820. return 0;
  2821. }
  2822. if (os_strcmp(var, "domain_suffix_match") == 0) {
  2823. os_free(cred->domain_suffix_match);
  2824. cred->domain_suffix_match = val;
  2825. return 0;
  2826. }
  2827. if (os_strcmp(var, "domain") == 0) {
  2828. char **new_domain;
  2829. new_domain = os_realloc_array(cred->domain,
  2830. cred->num_domain + 1,
  2831. sizeof(char *));
  2832. if (new_domain == NULL) {
  2833. os_free(val);
  2834. return -1;
  2835. }
  2836. new_domain[cred->num_domain++] = val;
  2837. cred->domain = new_domain;
  2838. return 0;
  2839. }
  2840. if (os_strcmp(var, "phase1") == 0) {
  2841. os_free(cred->phase1);
  2842. cred->phase1 = val;
  2843. return 0;
  2844. }
  2845. if (os_strcmp(var, "phase2") == 0) {
  2846. os_free(cred->phase2);
  2847. cred->phase2 = val;
  2848. return 0;
  2849. }
  2850. if (os_strcmp(var, "roaming_consortium") == 0) {
  2851. if (len < 3 || len > sizeof(cred->roaming_consortium)) {
  2852. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2853. "roaming_consortium length %d (3..15 "
  2854. "expected)", line, (int) len);
  2855. os_free(val);
  2856. return -1;
  2857. }
  2858. os_memcpy(cred->roaming_consortium, val, len);
  2859. cred->roaming_consortium_len = len;
  2860. os_free(val);
  2861. return 0;
  2862. }
  2863. if (os_strcmp(var, "required_roaming_consortium") == 0) {
  2864. if (len < 3 || len > sizeof(cred->required_roaming_consortium))
  2865. {
  2866. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2867. "required_roaming_consortium length %d "
  2868. "(3..15 expected)", line, (int) len);
  2869. os_free(val);
  2870. return -1;
  2871. }
  2872. os_memcpy(cred->required_roaming_consortium, val, len);
  2873. cred->required_roaming_consortium_len = len;
  2874. os_free(val);
  2875. return 0;
  2876. }
  2877. if (os_strcmp(var, "excluded_ssid") == 0) {
  2878. struct excluded_ssid *e;
  2879. if (len > SSID_MAX_LEN) {
  2880. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2881. "excluded_ssid length %d", line, (int) len);
  2882. os_free(val);
  2883. return -1;
  2884. }
  2885. e = os_realloc_array(cred->excluded_ssid,
  2886. cred->num_excluded_ssid + 1,
  2887. sizeof(struct excluded_ssid));
  2888. if (e == NULL) {
  2889. os_free(val);
  2890. return -1;
  2891. }
  2892. cred->excluded_ssid = e;
  2893. e = &cred->excluded_ssid[cred->num_excluded_ssid++];
  2894. os_memcpy(e->ssid, val, len);
  2895. e->ssid_len = len;
  2896. os_free(val);
  2897. return 0;
  2898. }
  2899. if (os_strcmp(var, "roaming_partner") == 0) {
  2900. struct roaming_partner *p;
  2901. char *pos;
  2902. p = os_realloc_array(cred->roaming_partner,
  2903. cred->num_roaming_partner + 1,
  2904. sizeof(struct roaming_partner));
  2905. if (p == NULL) {
  2906. os_free(val);
  2907. return -1;
  2908. }
  2909. cred->roaming_partner = p;
  2910. p = &cred->roaming_partner[cred->num_roaming_partner];
  2911. pos = os_strchr(val, ',');
  2912. if (pos == NULL) {
  2913. os_free(val);
  2914. return -1;
  2915. }
  2916. *pos++ = '\0';
  2917. if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
  2918. os_free(val);
  2919. return -1;
  2920. }
  2921. os_memcpy(p->fqdn, val, pos - val);
  2922. p->exact_match = atoi(pos);
  2923. pos = os_strchr(pos, ',');
  2924. if (pos == NULL) {
  2925. os_free(val);
  2926. return -1;
  2927. }
  2928. *pos++ = '\0';
  2929. p->priority = atoi(pos);
  2930. pos = os_strchr(pos, ',');
  2931. if (pos == NULL) {
  2932. os_free(val);
  2933. return -1;
  2934. }
  2935. *pos++ = '\0';
  2936. if (os_strlen(pos) >= sizeof(p->country)) {
  2937. os_free(val);
  2938. return -1;
  2939. }
  2940. os_memcpy(p->country, pos, os_strlen(pos) + 1);
  2941. cred->num_roaming_partner++;
  2942. os_free(val);
  2943. return 0;
  2944. }
  2945. if (os_strcmp(var, "provisioning_sp") == 0) {
  2946. os_free(cred->provisioning_sp);
  2947. cred->provisioning_sp = val;
  2948. return 0;
  2949. }
  2950. if (line) {
  2951. wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
  2952. line, var);
  2953. }
  2954. os_free(val);
  2955. return -1;
  2956. }
  2957. static char * alloc_int_str(int val)
  2958. {
  2959. const unsigned int bufsize = 20;
  2960. char *buf;
  2961. int res;
  2962. buf = os_malloc(bufsize);
  2963. if (buf == NULL)
  2964. return NULL;
  2965. res = os_snprintf(buf, bufsize, "%d", val);
  2966. if (os_snprintf_error(bufsize, res)) {
  2967. os_free(buf);
  2968. buf = NULL;
  2969. }
  2970. return buf;
  2971. }
  2972. static char * alloc_strdup(const char *str)
  2973. {
  2974. if (str == NULL)
  2975. return NULL;
  2976. return os_strdup(str);
  2977. }
  2978. char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
  2979. {
  2980. if (os_strcmp(var, "temporary") == 0)
  2981. return alloc_int_str(cred->temporary);
  2982. if (os_strcmp(var, "priority") == 0)
  2983. return alloc_int_str(cred->priority);
  2984. if (os_strcmp(var, "sp_priority") == 0)
  2985. return alloc_int_str(cred->sp_priority);
  2986. if (os_strcmp(var, "pcsc") == 0)
  2987. return alloc_int_str(cred->pcsc);
  2988. if (os_strcmp(var, "eap") == 0) {
  2989. if (!cred->eap_method)
  2990. return NULL;
  2991. return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
  2992. cred->eap_method[0].method));
  2993. }
  2994. if (os_strcmp(var, "update_identifier") == 0)
  2995. return alloc_int_str(cred->update_identifier);
  2996. if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
  2997. return alloc_int_str(cred->min_dl_bandwidth_home);
  2998. if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
  2999. return alloc_int_str(cred->min_ul_bandwidth_home);
  3000. if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
  3001. return alloc_int_str(cred->min_dl_bandwidth_roaming);
  3002. if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
  3003. return alloc_int_str(cred->min_ul_bandwidth_roaming);
  3004. if (os_strcmp(var, "max_bss_load") == 0)
  3005. return alloc_int_str(cred->max_bss_load);
  3006. if (os_strcmp(var, "req_conn_capab") == 0) {
  3007. unsigned int i;
  3008. char *buf, *end, *pos;
  3009. int ret;
  3010. if (!cred->num_req_conn_capab)
  3011. return NULL;
  3012. buf = os_malloc(4000);
  3013. if (buf == NULL)
  3014. return NULL;
  3015. pos = buf;
  3016. end = pos + 4000;
  3017. for (i = 0; i < cred->num_req_conn_capab; i++) {
  3018. int *ports;
  3019. ret = os_snprintf(pos, end - pos, "%s%u",
  3020. i > 0 ? "\n" : "",
  3021. cred->req_conn_capab_proto[i]);
  3022. if (os_snprintf_error(end - pos, ret))
  3023. return buf;
  3024. pos += ret;
  3025. ports = cred->req_conn_capab_port[i];
  3026. if (ports) {
  3027. int j;
  3028. for (j = 0; ports[j] != -1; j++) {
  3029. ret = os_snprintf(pos, end - pos,
  3030. "%s%d",
  3031. j > 0 ? "," : ":",
  3032. ports[j]);
  3033. if (os_snprintf_error(end - pos, ret))
  3034. return buf;
  3035. pos += ret;
  3036. }
  3037. }
  3038. }
  3039. return buf;
  3040. }
  3041. if (os_strcmp(var, "ocsp") == 0)
  3042. return alloc_int_str(cred->ocsp);
  3043. if (os_strcmp(var, "realm") == 0)
  3044. return alloc_strdup(cred->realm);
  3045. if (os_strcmp(var, "username") == 0)
  3046. return alloc_strdup(cred->username);
  3047. if (os_strcmp(var, "password") == 0) {
  3048. if (!cred->password)
  3049. return NULL;
  3050. return alloc_strdup("*");
  3051. }
  3052. if (os_strcmp(var, "ca_cert") == 0)
  3053. return alloc_strdup(cred->ca_cert);
  3054. if (os_strcmp(var, "client_cert") == 0)
  3055. return alloc_strdup(cred->client_cert);
  3056. if (os_strcmp(var, "private_key") == 0)
  3057. return alloc_strdup(cred->private_key);
  3058. if (os_strcmp(var, "private_key_passwd") == 0) {
  3059. if (!cred->private_key_passwd)
  3060. return NULL;
  3061. return alloc_strdup("*");
  3062. }
  3063. if (os_strcmp(var, "imsi") == 0)
  3064. return alloc_strdup(cred->imsi);
  3065. if (os_strcmp(var, "milenage") == 0) {
  3066. if (!(cred->milenage))
  3067. return NULL;
  3068. return alloc_strdup("*");
  3069. }
  3070. if (os_strcmp(var, "domain_suffix_match") == 0)
  3071. return alloc_strdup(cred->domain_suffix_match);
  3072. if (os_strcmp(var, "domain") == 0) {
  3073. unsigned int i;
  3074. char *buf, *end, *pos;
  3075. int ret;
  3076. if (!cred->num_domain)
  3077. return NULL;
  3078. buf = os_malloc(4000);
  3079. if (buf == NULL)
  3080. return NULL;
  3081. pos = buf;
  3082. end = pos + 4000;
  3083. for (i = 0; i < cred->num_domain; i++) {
  3084. ret = os_snprintf(pos, end - pos, "%s%s",
  3085. i > 0 ? "\n" : "", cred->domain[i]);
  3086. if (os_snprintf_error(end - pos, ret))
  3087. return buf;
  3088. pos += ret;
  3089. }
  3090. return buf;
  3091. }
  3092. if (os_strcmp(var, "phase1") == 0)
  3093. return alloc_strdup(cred->phase1);
  3094. if (os_strcmp(var, "phase2") == 0)
  3095. return alloc_strdup(cred->phase2);
  3096. if (os_strcmp(var, "roaming_consortium") == 0) {
  3097. size_t buflen;
  3098. char *buf;
  3099. if (!cred->roaming_consortium_len)
  3100. return NULL;
  3101. buflen = cred->roaming_consortium_len * 2 + 1;
  3102. buf = os_malloc(buflen);
  3103. if (buf == NULL)
  3104. return NULL;
  3105. wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
  3106. cred->roaming_consortium_len);
  3107. return buf;
  3108. }
  3109. if (os_strcmp(var, "required_roaming_consortium") == 0) {
  3110. size_t buflen;
  3111. char *buf;
  3112. if (!cred->required_roaming_consortium_len)
  3113. return NULL;
  3114. buflen = cred->required_roaming_consortium_len * 2 + 1;
  3115. buf = os_malloc(buflen);
  3116. if (buf == NULL)
  3117. return NULL;
  3118. wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
  3119. cred->required_roaming_consortium_len);
  3120. return buf;
  3121. }
  3122. if (os_strcmp(var, "excluded_ssid") == 0) {
  3123. unsigned int i;
  3124. char *buf, *end, *pos;
  3125. if (!cred->num_excluded_ssid)
  3126. return NULL;
  3127. buf = os_malloc(4000);
  3128. if (buf == NULL)
  3129. return NULL;
  3130. pos = buf;
  3131. end = pos + 4000;
  3132. for (i = 0; i < cred->num_excluded_ssid; i++) {
  3133. struct excluded_ssid *e;
  3134. int ret;
  3135. e = &cred->excluded_ssid[i];
  3136. ret = os_snprintf(pos, end - pos, "%s%s",
  3137. i > 0 ? "\n" : "",
  3138. wpa_ssid_txt(e->ssid, e->ssid_len));
  3139. if (os_snprintf_error(end - pos, ret))
  3140. return buf;
  3141. pos += ret;
  3142. }
  3143. return buf;
  3144. }
  3145. if (os_strcmp(var, "roaming_partner") == 0) {
  3146. unsigned int i;
  3147. char *buf, *end, *pos;
  3148. if (!cred->num_roaming_partner)
  3149. return NULL;
  3150. buf = os_malloc(4000);
  3151. if (buf == NULL)
  3152. return NULL;
  3153. pos = buf;
  3154. end = pos + 4000;
  3155. for (i = 0; i < cred->num_roaming_partner; i++) {
  3156. struct roaming_partner *p;
  3157. int ret;
  3158. p = &cred->roaming_partner[i];
  3159. ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
  3160. i > 0 ? "\n" : "",
  3161. p->fqdn, p->exact_match, p->priority,
  3162. p->country);
  3163. if (os_snprintf_error(end - pos, ret))
  3164. return buf;
  3165. pos += ret;
  3166. }
  3167. return buf;
  3168. }
  3169. if (os_strcmp(var, "provisioning_sp") == 0)
  3170. return alloc_strdup(cred->provisioning_sp);
  3171. return NULL;
  3172. }
  3173. struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
  3174. {
  3175. struct wpa_cred *cred;
  3176. cred = config->cred;
  3177. while (cred) {
  3178. if (id == cred->id)
  3179. break;
  3180. cred = cred->next;
  3181. }
  3182. return cred;
  3183. }
  3184. struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
  3185. {
  3186. int id;
  3187. struct wpa_cred *cred, *last = NULL;
  3188. id = -1;
  3189. cred = config->cred;
  3190. while (cred) {
  3191. if (cred->id > id)
  3192. id = cred->id;
  3193. last = cred;
  3194. cred = cred->next;
  3195. }
  3196. id++;
  3197. cred = os_zalloc(sizeof(*cred));
  3198. if (cred == NULL)
  3199. return NULL;
  3200. cred->id = id;
  3201. cred->sim_num = DEFAULT_USER_SELECTED_SIM;
  3202. if (last)
  3203. last->next = cred;
  3204. else
  3205. config->cred = cred;
  3206. return cred;
  3207. }
  3208. int wpa_config_remove_cred(struct wpa_config *config, int id)
  3209. {
  3210. struct wpa_cred *cred, *prev = NULL;
  3211. cred = config->cred;
  3212. while (cred) {
  3213. if (id == cred->id)
  3214. break;
  3215. prev = cred;
  3216. cred = cred->next;
  3217. }
  3218. if (cred == NULL)
  3219. return -1;
  3220. if (prev)
  3221. prev->next = cred->next;
  3222. else
  3223. config->cred = cred->next;
  3224. wpa_config_free_cred(cred);
  3225. return 0;
  3226. }
  3227. #ifndef CONFIG_NO_CONFIG_BLOBS
  3228. /**
  3229. * wpa_config_get_blob - Get a named configuration blob
  3230. * @config: Configuration data from wpa_config_read()
  3231. * @name: Name of the blob
  3232. * Returns: Pointer to blob data or %NULL if not found
  3233. */
  3234. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  3235. const char *name)
  3236. {
  3237. struct wpa_config_blob *blob = config->blobs;
  3238. while (blob) {
  3239. if (os_strcmp(blob->name, name) == 0)
  3240. return blob;
  3241. blob = blob->next;
  3242. }
  3243. return NULL;
  3244. }
  3245. /**
  3246. * wpa_config_set_blob - Set or add a named configuration blob
  3247. * @config: Configuration data from wpa_config_read()
  3248. * @blob: New value for the blob
  3249. *
  3250. * Adds a new configuration blob or replaces the current value of an existing
  3251. * blob.
  3252. */
  3253. void wpa_config_set_blob(struct wpa_config *config,
  3254. struct wpa_config_blob *blob)
  3255. {
  3256. wpa_config_remove_blob(config, blob->name);
  3257. blob->next = config->blobs;
  3258. config->blobs = blob;
  3259. }
  3260. /**
  3261. * wpa_config_free_blob - Free blob data
  3262. * @blob: Pointer to blob to be freed
  3263. */
  3264. void wpa_config_free_blob(struct wpa_config_blob *blob)
  3265. {
  3266. if (blob) {
  3267. os_free(blob->name);
  3268. bin_clear_free(blob->data, blob->len);
  3269. os_free(blob);
  3270. }
  3271. }
  3272. /**
  3273. * wpa_config_remove_blob - Remove a named configuration blob
  3274. * @config: Configuration data from wpa_config_read()
  3275. * @name: Name of the blob to remove
  3276. * Returns: 0 if blob was removed or -1 if blob was not found
  3277. */
  3278. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  3279. {
  3280. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  3281. while (pos) {
  3282. if (os_strcmp(pos->name, name) == 0) {
  3283. if (prev)
  3284. prev->next = pos->next;
  3285. else
  3286. config->blobs = pos->next;
  3287. wpa_config_free_blob(pos);
  3288. return 0;
  3289. }
  3290. prev = pos;
  3291. pos = pos->next;
  3292. }
  3293. return -1;
  3294. }
  3295. #endif /* CONFIG_NO_CONFIG_BLOBS */
  3296. /**
  3297. * wpa_config_alloc_empty - Allocate an empty configuration
  3298. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  3299. * socket
  3300. * @driver_param: Driver parameters
  3301. * Returns: Pointer to allocated configuration data or %NULL on failure
  3302. */
  3303. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  3304. const char *driver_param)
  3305. {
  3306. struct wpa_config *config;
  3307. const int aCWmin = 4, aCWmax = 10;
  3308. const struct hostapd_wmm_ac_params ac_bk =
  3309. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  3310. const struct hostapd_wmm_ac_params ac_be =
  3311. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  3312. const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
  3313. { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
  3314. const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
  3315. { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
  3316. config = os_zalloc(sizeof(*config));
  3317. if (config == NULL)
  3318. return NULL;
  3319. config->eapol_version = DEFAULT_EAPOL_VERSION;
  3320. config->ap_scan = DEFAULT_AP_SCAN;
  3321. config->user_mpm = DEFAULT_USER_MPM;
  3322. config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
  3323. config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
  3324. config->dot11RSNASAERetransPeriod =
  3325. DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
  3326. config->fast_reauth = DEFAULT_FAST_REAUTH;
  3327. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  3328. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  3329. config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
  3330. config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
  3331. config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
  3332. config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
  3333. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  3334. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  3335. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  3336. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  3337. config->ap_isolate = DEFAULT_AP_ISOLATE;
  3338. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  3339. config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
  3340. config->wmm_ac_params[0] = ac_be;
  3341. config->wmm_ac_params[1] = ac_bk;
  3342. config->wmm_ac_params[2] = ac_vi;
  3343. config->wmm_ac_params[3] = ac_vo;
  3344. config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
  3345. config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
  3346. config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
  3347. config->cert_in_cb = DEFAULT_CERT_IN_CB;
  3348. config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
  3349. #ifdef CONFIG_MBO
  3350. config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
  3351. config->disassoc_imminent_rssi_threshold =
  3352. DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
  3353. config->oce = DEFAULT_OCE_SUPPORT;
  3354. #endif /* CONFIG_MBO */
  3355. if (ctrl_interface)
  3356. config->ctrl_interface = os_strdup(ctrl_interface);
  3357. if (driver_param)
  3358. config->driver_param = os_strdup(driver_param);
  3359. config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
  3360. return config;
  3361. }
  3362. #ifndef CONFIG_NO_STDOUT_DEBUG
  3363. /**
  3364. * wpa_config_debug_dump_networks - Debug dump of configured networks
  3365. * @config: Configuration data from wpa_config_read()
  3366. */
  3367. void wpa_config_debug_dump_networks(struct wpa_config *config)
  3368. {
  3369. int prio;
  3370. struct wpa_ssid *ssid;
  3371. for (prio = 0; prio < config->num_prio; prio++) {
  3372. ssid = config->pssid[prio];
  3373. wpa_printf(MSG_DEBUG, "Priority group %d",
  3374. ssid->priority);
  3375. while (ssid) {
  3376. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  3377. ssid->id,
  3378. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  3379. ssid = ssid->pnext;
  3380. }
  3381. }
  3382. }
  3383. #endif /* CONFIG_NO_STDOUT_DEBUG */
  3384. struct global_parse_data {
  3385. char *name;
  3386. int (*parser)(const struct global_parse_data *data,
  3387. struct wpa_config *config, int line, const char *value);
  3388. int (*get)(const char *name, struct wpa_config *config, long offset,
  3389. char *buf, size_t buflen, int pretty_print);
  3390. void *param1, *param2, *param3;
  3391. unsigned int changed_flag;
  3392. };
  3393. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  3394. struct wpa_config *config, int line,
  3395. const char *pos)
  3396. {
  3397. int val, *dst;
  3398. char *end;
  3399. dst = (int *) (((u8 *) config) + (long) data->param1);
  3400. val = strtol(pos, &end, 0);
  3401. if (*end) {
  3402. wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
  3403. line, pos);
  3404. return -1;
  3405. }
  3406. *dst = val;
  3407. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  3408. if (data->param2 && *dst < (long) data->param2) {
  3409. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  3410. "min_value=%ld)", line, data->name, *dst,
  3411. (long) data->param2);
  3412. *dst = (long) data->param2;
  3413. return -1;
  3414. }
  3415. if (data->param3 && *dst > (long) data->param3) {
  3416. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  3417. "max_value=%ld)", line, data->name, *dst,
  3418. (long) data->param3);
  3419. *dst = (long) data->param3;
  3420. return -1;
  3421. }
  3422. return 0;
  3423. }
  3424. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  3425. struct wpa_config *config, int line,
  3426. const char *pos)
  3427. {
  3428. size_t len;
  3429. char **dst, *tmp;
  3430. len = os_strlen(pos);
  3431. if (data->param2 && len < (size_t) data->param2) {
  3432. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  3433. "min_len=%ld)", line, data->name,
  3434. (unsigned long) len, (long) data->param2);
  3435. return -1;
  3436. }
  3437. if (data->param3 && len > (size_t) data->param3) {
  3438. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  3439. "max_len=%ld)", line, data->name,
  3440. (unsigned long) len, (long) data->param3);
  3441. return -1;
  3442. }
  3443. if (has_newline(pos)) {
  3444. wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
  3445. line, data->name);
  3446. return -1;
  3447. }
  3448. tmp = os_strdup(pos);
  3449. if (tmp == NULL)
  3450. return -1;
  3451. dst = (char **) (((u8 *) config) + (long) data->param1);
  3452. os_free(*dst);
  3453. *dst = tmp;
  3454. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  3455. return 0;
  3456. }
  3457. static int wpa_config_process_bgscan(const struct global_parse_data *data,
  3458. struct wpa_config *config, int line,
  3459. const char *pos)
  3460. {
  3461. size_t len;
  3462. char *tmp;
  3463. int res;
  3464. tmp = wpa_config_parse_string(pos, &len);
  3465. if (tmp == NULL) {
  3466. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
  3467. line, data->name);
  3468. return -1;
  3469. }
  3470. res = wpa_global_config_parse_str(data, config, line, tmp);
  3471. os_free(tmp);
  3472. return res;
  3473. }
  3474. static int wpa_global_config_parse_bin(const struct global_parse_data *data,
  3475. struct wpa_config *config, int line,
  3476. const char *pos)
  3477. {
  3478. struct wpabuf **dst, *tmp;
  3479. tmp = wpabuf_parse_bin(pos);
  3480. if (!tmp)
  3481. return -1;
  3482. dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
  3483. wpabuf_free(*dst);
  3484. *dst = tmp;
  3485. wpa_printf(MSG_DEBUG, "%s", data->name);
  3486. return 0;
  3487. }
  3488. static int wpa_config_process_freq_list(const struct global_parse_data *data,
  3489. struct wpa_config *config, int line,
  3490. const char *value)
  3491. {
  3492. int *freqs;
  3493. freqs = wpa_config_parse_int_array(value);
  3494. if (freqs == NULL)
  3495. return -1;
  3496. if (freqs[0] == 0) {
  3497. os_free(freqs);
  3498. freqs = NULL;
  3499. }
  3500. os_free(config->freq_list);
  3501. config->freq_list = freqs;
  3502. return 0;
  3503. }
  3504. #ifdef CONFIG_P2P
  3505. static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
  3506. struct wpa_config *config, int line,
  3507. const char *pos)
  3508. {
  3509. u32 *dst;
  3510. struct hostapd_ip_addr addr;
  3511. if (hostapd_parse_ip_addr(pos, &addr) < 0)
  3512. return -1;
  3513. if (addr.af != AF_INET)
  3514. return -1;
  3515. dst = (u32 *) (((u8 *) config) + (long) data->param1);
  3516. os_memcpy(dst, &addr.u.v4.s_addr, 4);
  3517. wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
  3518. WPA_GET_BE32((u8 *) dst));
  3519. return 0;
  3520. }
  3521. #endif /* CONFIG_P2P */
  3522. static int wpa_config_process_country(const struct global_parse_data *data,
  3523. struct wpa_config *config, int line,
  3524. const char *pos)
  3525. {
  3526. if (!pos[0] || !pos[1]) {
  3527. wpa_printf(MSG_DEBUG, "Invalid country set");
  3528. return -1;
  3529. }
  3530. config->country[0] = pos[0];
  3531. config->country[1] = pos[1];
  3532. wpa_printf(MSG_DEBUG, "country='%c%c'",
  3533. config->country[0], config->country[1]);
  3534. return 0;
  3535. }
  3536. static int wpa_config_process_load_dynamic_eap(
  3537. const struct global_parse_data *data, struct wpa_config *config,
  3538. int line, const char *so)
  3539. {
  3540. int ret;
  3541. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  3542. ret = eap_peer_method_load(so);
  3543. if (ret == -2) {
  3544. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  3545. "reloading.");
  3546. } else if (ret) {
  3547. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  3548. "method '%s'.", line, so);
  3549. return -1;
  3550. }
  3551. return 0;
  3552. }
  3553. #ifdef CONFIG_WPS
  3554. static int wpa_config_process_uuid(const struct global_parse_data *data,
  3555. struct wpa_config *config, int line,
  3556. const char *pos)
  3557. {
  3558. char buf[40];
  3559. if (uuid_str2bin(pos, config->uuid)) {
  3560. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  3561. return -1;
  3562. }
  3563. uuid_bin2str(config->uuid, buf, sizeof(buf));
  3564. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  3565. return 0;
  3566. }
  3567. static int wpa_config_process_device_type(
  3568. const struct global_parse_data *data,
  3569. struct wpa_config *config, int line, const char *pos)
  3570. {
  3571. return wps_dev_type_str2bin(pos, config->device_type);
  3572. }
  3573. static int wpa_config_process_os_version(const struct global_parse_data *data,
  3574. struct wpa_config *config, int line,
  3575. const char *pos)
  3576. {
  3577. if (hexstr2bin(pos, config->os_version, 4)) {
  3578. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  3579. return -1;
  3580. }
  3581. wpa_printf(MSG_DEBUG, "os_version=%08x",
  3582. WPA_GET_BE32(config->os_version));
  3583. return 0;
  3584. }
  3585. static int wpa_config_process_wps_vendor_ext_m1(
  3586. const struct global_parse_data *data,
  3587. struct wpa_config *config, int line, const char *pos)
  3588. {
  3589. struct wpabuf *tmp;
  3590. int len = os_strlen(pos) / 2;
  3591. u8 *p;
  3592. if (!len) {
  3593. wpa_printf(MSG_ERROR, "Line %d: "
  3594. "invalid wps_vendor_ext_m1", line);
  3595. return -1;
  3596. }
  3597. tmp = wpabuf_alloc(len);
  3598. if (tmp) {
  3599. p = wpabuf_put(tmp, len);
  3600. if (hexstr2bin(pos, p, len)) {
  3601. wpa_printf(MSG_ERROR, "Line %d: "
  3602. "invalid wps_vendor_ext_m1", line);
  3603. wpabuf_free(tmp);
  3604. return -1;
  3605. }
  3606. wpabuf_free(config->wps_vendor_ext_m1);
  3607. config->wps_vendor_ext_m1 = tmp;
  3608. } else {
  3609. wpa_printf(MSG_ERROR, "Can not allocate "
  3610. "memory for wps_vendor_ext_m1");
  3611. return -1;
  3612. }
  3613. return 0;
  3614. }
  3615. #endif /* CONFIG_WPS */
  3616. #ifdef CONFIG_P2P
  3617. static int wpa_config_process_sec_device_type(
  3618. const struct global_parse_data *data,
  3619. struct wpa_config *config, int line, const char *pos)
  3620. {
  3621. int idx;
  3622. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  3623. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  3624. "items", line);
  3625. return -1;
  3626. }
  3627. idx = config->num_sec_device_types;
  3628. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  3629. return -1;
  3630. config->num_sec_device_types++;
  3631. return 0;
  3632. }
  3633. static int wpa_config_process_p2p_pref_chan(
  3634. const struct global_parse_data *data,
  3635. struct wpa_config *config, int line, const char *pos)
  3636. {
  3637. struct p2p_channel *pref = NULL, *n;
  3638. unsigned int num = 0;
  3639. const char *pos2;
  3640. u8 op_class, chan;
  3641. /* format: class:chan,class:chan,... */
  3642. while (*pos) {
  3643. op_class = atoi(pos);
  3644. pos2 = os_strchr(pos, ':');
  3645. if (pos2 == NULL)
  3646. goto fail;
  3647. pos2++;
  3648. chan = atoi(pos2);
  3649. n = os_realloc_array(pref, num + 1,
  3650. sizeof(struct p2p_channel));
  3651. if (n == NULL)
  3652. goto fail;
  3653. pref = n;
  3654. pref[num].op_class = op_class;
  3655. pref[num].chan = chan;
  3656. num++;
  3657. pos = os_strchr(pos2, ',');
  3658. if (pos == NULL)
  3659. break;
  3660. pos++;
  3661. }
  3662. os_free(config->p2p_pref_chan);
  3663. config->p2p_pref_chan = pref;
  3664. config->num_p2p_pref_chan = num;
  3665. wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
  3666. (u8 *) config->p2p_pref_chan,
  3667. config->num_p2p_pref_chan * sizeof(struct p2p_channel));
  3668. return 0;
  3669. fail:
  3670. os_free(pref);
  3671. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
  3672. return -1;
  3673. }
  3674. static int wpa_config_process_p2p_no_go_freq(
  3675. const struct global_parse_data *data,
  3676. struct wpa_config *config, int line, const char *pos)
  3677. {
  3678. int ret;
  3679. ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
  3680. if (ret < 0) {
  3681. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
  3682. return -1;
  3683. }
  3684. wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
  3685. config->p2p_no_go_freq.num);
  3686. return 0;
  3687. }
  3688. #endif /* CONFIG_P2P */
  3689. static int wpa_config_process_hessid(
  3690. const struct global_parse_data *data,
  3691. struct wpa_config *config, int line, const char *pos)
  3692. {
  3693. if (hwaddr_aton2(pos, config->hessid) < 0) {
  3694. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  3695. line, pos);
  3696. return -1;
  3697. }
  3698. return 0;
  3699. }
  3700. static int wpa_config_process_sae_groups(
  3701. const struct global_parse_data *data,
  3702. struct wpa_config *config, int line, const char *pos)
  3703. {
  3704. int *groups = wpa_config_parse_int_array(pos);
  3705. if (groups == NULL) {
  3706. wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
  3707. line, pos);
  3708. return -1;
  3709. }
  3710. os_free(config->sae_groups);
  3711. config->sae_groups = groups;
  3712. return 0;
  3713. }
  3714. static int wpa_config_process_ap_vendor_elements(
  3715. const struct global_parse_data *data,
  3716. struct wpa_config *config, int line, const char *pos)
  3717. {
  3718. struct wpabuf *tmp;
  3719. int len = os_strlen(pos) / 2;
  3720. u8 *p;
  3721. if (!len) {
  3722. wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
  3723. line);
  3724. return -1;
  3725. }
  3726. tmp = wpabuf_alloc(len);
  3727. if (tmp) {
  3728. p = wpabuf_put(tmp, len);
  3729. if (hexstr2bin(pos, p, len)) {
  3730. wpa_printf(MSG_ERROR, "Line %d: invalid "
  3731. "ap_vendor_elements", line);
  3732. wpabuf_free(tmp);
  3733. return -1;
  3734. }
  3735. wpabuf_free(config->ap_vendor_elements);
  3736. config->ap_vendor_elements = tmp;
  3737. } else {
  3738. wpa_printf(MSG_ERROR, "Cannot allocate memory for "
  3739. "ap_vendor_elements");
  3740. return -1;
  3741. }
  3742. return 0;
  3743. }
  3744. #ifdef CONFIG_CTRL_IFACE
  3745. static int wpa_config_process_no_ctrl_interface(
  3746. const struct global_parse_data *data,
  3747. struct wpa_config *config, int line, const char *pos)
  3748. {
  3749. wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
  3750. os_free(config->ctrl_interface);
  3751. config->ctrl_interface = NULL;
  3752. return 0;
  3753. }
  3754. #endif /* CONFIG_CTRL_IFACE */
  3755. static int wpa_config_get_int(const char *name, struct wpa_config *config,
  3756. long offset, char *buf, size_t buflen,
  3757. int pretty_print)
  3758. {
  3759. int *val = (int *) (((u8 *) config) + (long) offset);
  3760. if (pretty_print)
  3761. return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
  3762. return os_snprintf(buf, buflen, "%d", *val);
  3763. }
  3764. static int wpa_config_get_str(const char *name, struct wpa_config *config,
  3765. long offset, char *buf, size_t buflen,
  3766. int pretty_print)
  3767. {
  3768. char **val = (char **) (((u8 *) config) + (long) offset);
  3769. int res;
  3770. if (pretty_print)
  3771. res = os_snprintf(buf, buflen, "%s=%s\n", name,
  3772. *val ? *val : "null");
  3773. else if (!*val)
  3774. return -1;
  3775. else
  3776. res = os_snprintf(buf, buflen, "%s", *val);
  3777. if (os_snprintf_error(buflen, res))
  3778. res = -1;
  3779. return res;
  3780. }
  3781. #ifdef CONFIG_P2P
  3782. static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
  3783. long offset, char *buf, size_t buflen,
  3784. int pretty_print)
  3785. {
  3786. void *val = ((u8 *) config) + (long) offset;
  3787. int res;
  3788. char addr[INET_ADDRSTRLEN];
  3789. if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
  3790. return -1;
  3791. if (pretty_print)
  3792. res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
  3793. else
  3794. res = os_snprintf(buf, buflen, "%s", addr);
  3795. if (os_snprintf_error(buflen, res))
  3796. res = -1;
  3797. return res;
  3798. }
  3799. #endif /* CONFIG_P2P */
  3800. #ifdef OFFSET
  3801. #undef OFFSET
  3802. #endif /* OFFSET */
  3803. /* OFFSET: Get offset of a variable within the wpa_config structure */
  3804. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  3805. #define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
  3806. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
  3807. #define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
  3808. #define INT(f) _INT(f), NULL, NULL
  3809. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  3810. #define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
  3811. #define STR(f) _STR(f), NULL, NULL
  3812. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  3813. #define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
  3814. #define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
  3815. OFFSET(f), NULL, NULL
  3816. static const struct global_parse_data global_fields[] = {
  3817. #ifdef CONFIG_CTRL_IFACE
  3818. { STR(ctrl_interface), 0 },
  3819. { FUNC_NO_VAR(no_ctrl_interface), 0 },
  3820. { STR(ctrl_interface_group), 0 } /* deprecated */,
  3821. #endif /* CONFIG_CTRL_IFACE */
  3822. #ifdef CONFIG_MACSEC
  3823. { INT_RANGE(eapol_version, 1, 3), 0 },
  3824. #else /* CONFIG_MACSEC */
  3825. { INT_RANGE(eapol_version, 1, 2), 0 },
  3826. #endif /* CONFIG_MACSEC */
  3827. { INT(ap_scan), 0 },
  3828. { FUNC(bgscan), 0 },
  3829. #ifdef CONFIG_MESH
  3830. { INT(user_mpm), 0 },
  3831. { INT_RANGE(max_peer_links, 0, 255), 0 },
  3832. { INT(mesh_max_inactivity), 0 },
  3833. { INT(dot11RSNASAERetransPeriod), 0 },
  3834. #endif /* CONFIG_MESH */
  3835. { INT(disable_scan_offload), 0 },
  3836. { INT(fast_reauth), 0 },
  3837. { STR(opensc_engine_path), 0 },
  3838. { STR(pkcs11_engine_path), 0 },
  3839. { STR(pkcs11_module_path), 0 },
  3840. { STR(openssl_ciphers), 0 },
  3841. { STR(pcsc_reader), 0 },
  3842. { STR(pcsc_pin), 0 },
  3843. { INT(external_sim), 0 },
  3844. { STR(driver_param), 0 },
  3845. { INT(dot11RSNAConfigPMKLifetime), 0 },
  3846. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  3847. { INT(dot11RSNAConfigSATimeout), 0 },
  3848. #ifndef CONFIG_NO_CONFIG_WRITE
  3849. { INT(update_config), 0 },
  3850. #endif /* CONFIG_NO_CONFIG_WRITE */
  3851. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  3852. #ifdef CONFIG_WPS
  3853. { FUNC(uuid), CFG_CHANGED_UUID },
  3854. { INT_RANGE(auto_uuid, 0, 1), 0 },
  3855. { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
  3856. CFG_CHANGED_DEVICE_NAME },
  3857. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  3858. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  3859. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  3860. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  3861. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  3862. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  3863. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  3864. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  3865. { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
  3866. #endif /* CONFIG_WPS */
  3867. #ifdef CONFIG_P2P
  3868. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  3869. { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
  3870. { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
  3871. { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
  3872. { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
  3873. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  3874. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  3875. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  3876. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  3877. { INT(p2p_group_idle), 0 },
  3878. { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
  3879. { INT_RANGE(p2p_passphrase_len, 8, 63),
  3880. CFG_CHANGED_P2P_PASSPHRASE_LEN },
  3881. { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
  3882. { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
  3883. { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
  3884. { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
  3885. { INT(p2p_go_ht40), 0 },
  3886. { INT(p2p_go_vht), 0 },
  3887. { INT(p2p_disabled), 0 },
  3888. { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
  3889. { INT(p2p_no_group_iface), 0 },
  3890. { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
  3891. { IPV4(ip_addr_go), 0 },
  3892. { IPV4(ip_addr_mask), 0 },
  3893. { IPV4(ip_addr_start), 0 },
  3894. { IPV4(ip_addr_end), 0 },
  3895. { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
  3896. #endif /* CONFIG_P2P */
  3897. { FUNC(country), CFG_CHANGED_COUNTRY },
  3898. { INT(bss_max_count), 0 },
  3899. { INT(bss_expiration_age), 0 },
  3900. { INT(bss_expiration_scan_count), 0 },
  3901. { INT_RANGE(filter_ssids, 0, 1), 0 },
  3902. { INT_RANGE(filter_rssi, -100, 0), 0 },
  3903. { INT(max_num_sta), 0 },
  3904. { INT_RANGE(ap_isolate, 0, 1), 0 },
  3905. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  3906. #ifdef CONFIG_HS20
  3907. { INT_RANGE(hs20, 0, 1), 0 },
  3908. #endif /* CONFIG_HS20 */
  3909. { INT_RANGE(interworking, 0, 1), 0 },
  3910. { FUNC(hessid), 0 },
  3911. { INT_RANGE(access_network_type, 0, 15), 0 },
  3912. { INT_RANGE(go_interworking, 0, 1), 0 },
  3913. { INT_RANGE(go_access_network_type, 0, 15), 0 },
  3914. { INT_RANGE(go_internet, 0, 1), 0 },
  3915. { INT_RANGE(go_venue_group, 0, 255), 0 },
  3916. { INT_RANGE(go_venue_type, 0, 255), 0 },
  3917. { INT_RANGE(pbc_in_m1, 0, 1), 0 },
  3918. { STR(autoscan), 0 },
  3919. { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
  3920. CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3921. { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3922. { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3923. { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3924. { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
  3925. { INT(p2p_go_max_inactivity), 0 },
  3926. { INT_RANGE(auto_interworking, 0, 1), 0 },
  3927. { INT(okc), 0 },
  3928. { INT(pmf), 0 },
  3929. { FUNC(sae_groups), 0 },
  3930. { INT(dtim_period), 0 },
  3931. { INT(beacon_int), 0 },
  3932. { FUNC(ap_vendor_elements), 0 },
  3933. { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
  3934. { FUNC(freq_list), 0 },
  3935. { INT(scan_cur_freq), 0 },
  3936. { INT(sched_scan_interval), 0 },
  3937. { INT(sched_scan_start_delay), 0 },
  3938. { INT(tdls_external_control), 0},
  3939. { STR(osu_dir), 0 },
  3940. { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
  3941. { INT(p2p_search_delay), 0},
  3942. { INT(mac_addr), 0 },
  3943. { INT(rand_addr_lifetime), 0 },
  3944. { INT(preassoc_mac_addr), 0 },
  3945. { INT(key_mgmt_offload), 0},
  3946. { INT(passive_scan), 0 },
  3947. { INT(reassoc_same_bss_optim), 0 },
  3948. { INT(wps_priority), 0},
  3949. #ifdef CONFIG_FST
  3950. { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
  3951. { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
  3952. { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
  3953. #endif /* CONFIG_FST */
  3954. { INT_RANGE(cert_in_cb, 0, 1), 0 },
  3955. { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
  3956. { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
  3957. #ifdef CONFIG_MBO
  3958. { STR(non_pref_chan), 0 },
  3959. { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
  3960. MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
  3961. { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
  3962. { INT_RANGE(oce, 0, 3), 0 },
  3963. #endif /* CONFIG_MBO */
  3964. { INT(gas_address3), 0 },
  3965. { INT_RANGE(ftm_responder, 0, 1), 0 },
  3966. { INT_RANGE(ftm_initiator, 0, 1), 0 },
  3967. { INT(gas_rand_addr_lifetime), 0 },
  3968. { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
  3969. { INT_RANGE(dpp_config_processing, 0, 2), 0 },
  3970. };
  3971. #undef FUNC
  3972. #undef _INT
  3973. #undef INT
  3974. #undef INT_RANGE
  3975. #undef _STR
  3976. #undef STR
  3977. #undef STR_RANGE
  3978. #undef BIN
  3979. #undef IPV4
  3980. #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
  3981. int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
  3982. {
  3983. int result = 0;
  3984. size_t i;
  3985. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  3986. const struct global_parse_data *field = &global_fields[i];
  3987. int tmp;
  3988. if (!field->get)
  3989. continue;
  3990. tmp = field->get(field->name, config, (long) field->param1,
  3991. buf, buflen, 1);
  3992. if (tmp < 0)
  3993. return -1;
  3994. buf += tmp;
  3995. buflen -= tmp;
  3996. result += tmp;
  3997. }
  3998. return result;
  3999. }
  4000. int wpa_config_get_value(const char *name, struct wpa_config *config,
  4001. char *buf, size_t buflen)
  4002. {
  4003. size_t i;
  4004. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  4005. const struct global_parse_data *field = &global_fields[i];
  4006. if (os_strcmp(name, field->name) != 0)
  4007. continue;
  4008. if (!field->get)
  4009. break;
  4010. return field->get(name, config, (long) field->param1,
  4011. buf, buflen, 0);
  4012. }
  4013. return -1;
  4014. }
  4015. int wpa_config_get_num_global_field_names(void)
  4016. {
  4017. return NUM_GLOBAL_FIELDS;
  4018. }
  4019. const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
  4020. {
  4021. if (i >= NUM_GLOBAL_FIELDS)
  4022. return NULL;
  4023. if (no_var)
  4024. *no_var = !global_fields[i].param1;
  4025. return global_fields[i].name;
  4026. }
  4027. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  4028. {
  4029. size_t i;
  4030. int ret = 0;
  4031. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  4032. const struct global_parse_data *field = &global_fields[i];
  4033. size_t flen = os_strlen(field->name);
  4034. if (os_strncmp(pos, field->name, flen) != 0 ||
  4035. pos[flen] != '=')
  4036. continue;
  4037. if (field->parser(field, config, line, pos + flen + 1)) {
  4038. wpa_printf(MSG_ERROR, "Line %d: failed to "
  4039. "parse '%s'.", line, pos);
  4040. ret = -1;
  4041. }
  4042. if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
  4043. config->wps_nfc_pw_from_config = 1;
  4044. config->changed_parameters |= field->changed_flag;
  4045. break;
  4046. }
  4047. if (i == NUM_GLOBAL_FIELDS) {
  4048. #ifdef CONFIG_AP
  4049. if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
  4050. char *tmp = os_strchr(pos, '=');
  4051. if (tmp == NULL) {
  4052. if (line < 0)
  4053. return -1;
  4054. wpa_printf(MSG_ERROR, "Line %d: invalid line "
  4055. "'%s'", line, pos);
  4056. return -1;
  4057. }
  4058. *tmp++ = '\0';
  4059. if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
  4060. tmp)) {
  4061. wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
  4062. "AC item", line);
  4063. return -1;
  4064. }
  4065. }
  4066. #endif /* CONFIG_AP */
  4067. if (line < 0)
  4068. return -1;
  4069. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  4070. line, pos);
  4071. ret = -1;
  4072. }
  4073. return ret;
  4074. }