config.c 110 KB

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