config.c 109 KB

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