config.c 94 KB

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