config.c 109 KB

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