config.c 102 KB

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