config.c 94 KB

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