config.c 98 KB

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