config.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057
  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 "crypto/sha1.h"
  12. #include "rsn_supp/wpa.h"
  13. #include "eap_peer/eap.h"
  14. #include "p2p/p2p.h"
  15. #include "config.h"
  16. #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
  17. #define NO_CONFIG_WRITE
  18. #endif
  19. /*
  20. * Structure for network configuration parsing. This data is used to implement
  21. * a generic parser for each network block variable. The table of configuration
  22. * variables is defined below in this file (ssid_fields[]).
  23. */
  24. struct parse_data {
  25. /* Configuration variable name */
  26. char *name;
  27. /* Parser function for this variable */
  28. int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
  29. int line, const char *value);
  30. #ifndef NO_CONFIG_WRITE
  31. /* Writer function (i.e., to get the variable in text format from
  32. * internal presentation). */
  33. char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
  34. #endif /* NO_CONFIG_WRITE */
  35. /* Variable specific parameters for the parser. */
  36. void *param1, *param2, *param3, *param4;
  37. /* 0 = this variable can be included in debug output and ctrl_iface
  38. * 1 = this variable contains key/private data and it must not be
  39. * included in debug output unless explicitly requested. In
  40. * addition, this variable will not be readable through the
  41. * ctrl_iface.
  42. */
  43. int key_data;
  44. };
  45. static int wpa_config_parse_str(const struct parse_data *data,
  46. struct wpa_ssid *ssid,
  47. int line, const char *value)
  48. {
  49. size_t res_len, *dst_len;
  50. char **dst, *tmp;
  51. if (os_strcmp(value, "NULL") == 0) {
  52. wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
  53. data->name);
  54. tmp = NULL;
  55. res_len = 0;
  56. goto set;
  57. }
  58. tmp = wpa_config_parse_string(value, &res_len);
  59. if (tmp == NULL) {
  60. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
  61. line, data->name,
  62. data->key_data ? "[KEY DATA REMOVED]" : value);
  63. return -1;
  64. }
  65. if (data->key_data) {
  66. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  67. (u8 *) tmp, res_len);
  68. } else {
  69. wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
  70. (u8 *) tmp, res_len);
  71. }
  72. if (data->param3 && res_len < (size_t) data->param3) {
  73. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  74. "min_len=%ld)", line, data->name,
  75. (unsigned long) res_len, (long) data->param3);
  76. os_free(tmp);
  77. return -1;
  78. }
  79. if (data->param4 && res_len > (size_t) data->param4) {
  80. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  81. "max_len=%ld)", line, data->name,
  82. (unsigned long) res_len, (long) data->param4);
  83. os_free(tmp);
  84. return -1;
  85. }
  86. set:
  87. dst = (char **) (((u8 *) ssid) + (long) data->param1);
  88. dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
  89. os_free(*dst);
  90. *dst = tmp;
  91. if (data->param2)
  92. *dst_len = res_len;
  93. return 0;
  94. }
  95. #ifndef NO_CONFIG_WRITE
  96. static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
  97. {
  98. char *buf;
  99. buf = os_malloc(len + 3);
  100. if (buf == NULL)
  101. return NULL;
  102. buf[0] = '"';
  103. os_memcpy(buf + 1, value, len);
  104. buf[len + 1] = '"';
  105. buf[len + 2] = '\0';
  106. return buf;
  107. }
  108. static char * wpa_config_write_string_hex(const u8 *value, size_t len)
  109. {
  110. char *buf;
  111. buf = os_zalloc(2 * len + 1);
  112. if (buf == NULL)
  113. return NULL;
  114. wpa_snprintf_hex(buf, 2 * len + 1, value, len);
  115. return buf;
  116. }
  117. static char * wpa_config_write_string(const u8 *value, size_t len)
  118. {
  119. if (value == NULL)
  120. return NULL;
  121. if (is_hex(value, len))
  122. return wpa_config_write_string_hex(value, len);
  123. else
  124. return wpa_config_write_string_ascii(value, len);
  125. }
  126. static char * wpa_config_write_str(const struct parse_data *data,
  127. struct wpa_ssid *ssid)
  128. {
  129. size_t len;
  130. char **src;
  131. src = (char **) (((u8 *) ssid) + (long) data->param1);
  132. if (*src == NULL)
  133. return NULL;
  134. if (data->param2)
  135. len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
  136. else
  137. len = os_strlen(*src);
  138. return wpa_config_write_string((const u8 *) *src, len);
  139. }
  140. #endif /* NO_CONFIG_WRITE */
  141. static int wpa_config_parse_int(const struct parse_data *data,
  142. struct wpa_ssid *ssid,
  143. int line, const char *value)
  144. {
  145. int *dst;
  146. dst = (int *) (((u8 *) ssid) + (long) data->param1);
  147. *dst = atoi(value);
  148. wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
  149. if (data->param3 && *dst < (long) data->param3) {
  150. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  151. "min_value=%ld)", line, data->name, *dst,
  152. (long) data->param3);
  153. *dst = (long) data->param3;
  154. return -1;
  155. }
  156. if (data->param4 && *dst > (long) data->param4) {
  157. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  158. "max_value=%ld)", line, data->name, *dst,
  159. (long) data->param4);
  160. *dst = (long) data->param4;
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. #ifndef NO_CONFIG_WRITE
  166. static char * wpa_config_write_int(const struct parse_data *data,
  167. struct wpa_ssid *ssid)
  168. {
  169. int *src, res;
  170. char *value;
  171. src = (int *) (((u8 *) ssid) + (long) data->param1);
  172. value = os_malloc(20);
  173. if (value == NULL)
  174. return NULL;
  175. res = os_snprintf(value, 20, "%d", *src);
  176. if (res < 0 || res >= 20) {
  177. os_free(value);
  178. return NULL;
  179. }
  180. value[20 - 1] = '\0';
  181. return value;
  182. }
  183. #endif /* NO_CONFIG_WRITE */
  184. static int wpa_config_parse_bssid(const struct parse_data *data,
  185. struct wpa_ssid *ssid, int line,
  186. const char *value)
  187. {
  188. if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
  189. os_strcmp(value, "any") == 0) {
  190. ssid->bssid_set = 0;
  191. wpa_printf(MSG_MSGDUMP, "BSSID any");
  192. return 0;
  193. }
  194. if (hwaddr_aton(value, ssid->bssid)) {
  195. wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
  196. line, value);
  197. return -1;
  198. }
  199. ssid->bssid_set = 1;
  200. wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
  201. return 0;
  202. }
  203. #ifndef NO_CONFIG_WRITE
  204. static char * wpa_config_write_bssid(const struct parse_data *data,
  205. struct wpa_ssid *ssid)
  206. {
  207. char *value;
  208. int res;
  209. if (!ssid->bssid_set)
  210. return NULL;
  211. value = os_malloc(20);
  212. if (value == NULL)
  213. return NULL;
  214. res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
  215. if (res < 0 || res >= 20) {
  216. os_free(value);
  217. return NULL;
  218. }
  219. value[20 - 1] = '\0';
  220. return value;
  221. }
  222. #endif /* NO_CONFIG_WRITE */
  223. static int wpa_config_parse_psk(const struct parse_data *data,
  224. struct wpa_ssid *ssid, int line,
  225. const char *value)
  226. {
  227. #ifdef CONFIG_EXT_PASSWORD
  228. if (os_strncmp(value, "ext:", 4) == 0) {
  229. os_free(ssid->passphrase);
  230. ssid->passphrase = NULL;
  231. ssid->psk_set = 0;
  232. os_free(ssid->ext_psk);
  233. ssid->ext_psk = os_strdup(value + 4);
  234. if (ssid->ext_psk == NULL)
  235. return -1;
  236. wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
  237. ssid->ext_psk);
  238. return 0;
  239. }
  240. #endif /* CONFIG_EXT_PASSWORD */
  241. if (*value == '"') {
  242. #ifndef CONFIG_NO_PBKDF2
  243. const char *pos;
  244. size_t len;
  245. value++;
  246. pos = os_strrchr(value, '"');
  247. if (pos)
  248. len = pos - value;
  249. else
  250. len = os_strlen(value);
  251. if (len < 8 || len > 63) {
  252. wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
  253. "length %lu (expected: 8..63) '%s'.",
  254. line, (unsigned long) len, value);
  255. return -1;
  256. }
  257. wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
  258. (u8 *) value, len);
  259. if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
  260. os_memcmp(ssid->passphrase, value, len) == 0)
  261. return 0;
  262. ssid->psk_set = 0;
  263. os_free(ssid->passphrase);
  264. ssid->passphrase = os_malloc(len + 1);
  265. if (ssid->passphrase == NULL)
  266. return -1;
  267. os_memcpy(ssid->passphrase, value, len);
  268. ssid->passphrase[len] = '\0';
  269. return 0;
  270. #else /* CONFIG_NO_PBKDF2 */
  271. wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
  272. "supported.", line);
  273. return -1;
  274. #endif /* CONFIG_NO_PBKDF2 */
  275. }
  276. if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
  277. value[PMK_LEN * 2] != '\0') {
  278. wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
  279. line, value);
  280. return -1;
  281. }
  282. os_free(ssid->passphrase);
  283. ssid->passphrase = NULL;
  284. ssid->psk_set = 1;
  285. wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
  286. return 0;
  287. }
  288. #ifndef NO_CONFIG_WRITE
  289. static char * wpa_config_write_psk(const struct parse_data *data,
  290. struct wpa_ssid *ssid)
  291. {
  292. #ifdef CONFIG_EXT_PASSWORD
  293. if (ssid->ext_psk) {
  294. size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
  295. char *buf = os_malloc(len);
  296. if (buf == NULL)
  297. return NULL;
  298. os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
  299. return buf;
  300. }
  301. #endif /* CONFIG_EXT_PASSWORD */
  302. if (ssid->passphrase)
  303. return wpa_config_write_string_ascii(
  304. (const u8 *) ssid->passphrase,
  305. os_strlen(ssid->passphrase));
  306. if (ssid->psk_set)
  307. return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
  308. return NULL;
  309. }
  310. #endif /* NO_CONFIG_WRITE */
  311. static int wpa_config_parse_proto(const struct parse_data *data,
  312. struct wpa_ssid *ssid, int line,
  313. const char *value)
  314. {
  315. int val = 0, last, errors = 0;
  316. char *start, *end, *buf;
  317. buf = os_strdup(value);
  318. if (buf == NULL)
  319. return -1;
  320. start = buf;
  321. while (*start != '\0') {
  322. while (*start == ' ' || *start == '\t')
  323. start++;
  324. if (*start == '\0')
  325. break;
  326. end = start;
  327. while (*end != ' ' && *end != '\t' && *end != '\0')
  328. end++;
  329. last = *end == '\0';
  330. *end = '\0';
  331. if (os_strcmp(start, "WPA") == 0)
  332. val |= WPA_PROTO_WPA;
  333. else if (os_strcmp(start, "RSN") == 0 ||
  334. os_strcmp(start, "WPA2") == 0)
  335. val |= WPA_PROTO_RSN;
  336. else {
  337. wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
  338. line, start);
  339. errors++;
  340. }
  341. if (last)
  342. break;
  343. start = end + 1;
  344. }
  345. os_free(buf);
  346. if (val == 0) {
  347. wpa_printf(MSG_ERROR,
  348. "Line %d: no proto values configured.", line);
  349. errors++;
  350. }
  351. wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
  352. ssid->proto = val;
  353. return errors ? -1 : 0;
  354. }
  355. #ifndef NO_CONFIG_WRITE
  356. static char * wpa_config_write_proto(const struct parse_data *data,
  357. struct wpa_ssid *ssid)
  358. {
  359. int first = 1, ret;
  360. char *buf, *pos, *end;
  361. pos = buf = os_zalloc(10);
  362. if (buf == NULL)
  363. return NULL;
  364. end = buf + 10;
  365. if (ssid->proto & WPA_PROTO_WPA) {
  366. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  367. if (ret < 0 || ret >= end - pos)
  368. return buf;
  369. pos += ret;
  370. first = 0;
  371. }
  372. if (ssid->proto & WPA_PROTO_RSN) {
  373. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  374. if (ret < 0 || ret >= end - pos)
  375. return buf;
  376. pos += ret;
  377. first = 0;
  378. }
  379. return buf;
  380. }
  381. #endif /* NO_CONFIG_WRITE */
  382. static int wpa_config_parse_key_mgmt(const struct parse_data *data,
  383. struct wpa_ssid *ssid, int line,
  384. const char *value)
  385. {
  386. int val = 0, last, errors = 0;
  387. char *start, *end, *buf;
  388. buf = os_strdup(value);
  389. if (buf == NULL)
  390. return -1;
  391. start = buf;
  392. while (*start != '\0') {
  393. while (*start == ' ' || *start == '\t')
  394. start++;
  395. if (*start == '\0')
  396. break;
  397. end = start;
  398. while (*end != ' ' && *end != '\t' && *end != '\0')
  399. end++;
  400. last = *end == '\0';
  401. *end = '\0';
  402. if (os_strcmp(start, "WPA-PSK") == 0)
  403. val |= WPA_KEY_MGMT_PSK;
  404. else if (os_strcmp(start, "WPA-EAP") == 0)
  405. val |= WPA_KEY_MGMT_IEEE8021X;
  406. else if (os_strcmp(start, "IEEE8021X") == 0)
  407. val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  408. else if (os_strcmp(start, "NONE") == 0)
  409. val |= WPA_KEY_MGMT_NONE;
  410. else if (os_strcmp(start, "WPA-NONE") == 0)
  411. val |= WPA_KEY_MGMT_WPA_NONE;
  412. #ifdef CONFIG_IEEE80211R
  413. else if (os_strcmp(start, "FT-PSK") == 0)
  414. val |= WPA_KEY_MGMT_FT_PSK;
  415. else if (os_strcmp(start, "FT-EAP") == 0)
  416. val |= WPA_KEY_MGMT_FT_IEEE8021X;
  417. #endif /* CONFIG_IEEE80211R */
  418. #ifdef CONFIG_IEEE80211W
  419. else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
  420. val |= WPA_KEY_MGMT_PSK_SHA256;
  421. else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
  422. val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
  423. #endif /* CONFIG_IEEE80211W */
  424. #ifdef CONFIG_WPS
  425. else if (os_strcmp(start, "WPS") == 0)
  426. val |= WPA_KEY_MGMT_WPS;
  427. #endif /* CONFIG_WPS */
  428. #ifdef CONFIG_SAE
  429. else if (os_strcmp(start, "SAE") == 0)
  430. val |= WPA_KEY_MGMT_SAE;
  431. else if (os_strcmp(start, "FT-SAE") == 0)
  432. val |= WPA_KEY_MGMT_FT_SAE;
  433. #endif /* CONFIG_SAE */
  434. else {
  435. wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
  436. line, start);
  437. errors++;
  438. }
  439. if (last)
  440. break;
  441. start = end + 1;
  442. }
  443. os_free(buf);
  444. if (val == 0) {
  445. wpa_printf(MSG_ERROR,
  446. "Line %d: no key_mgmt values configured.", line);
  447. errors++;
  448. }
  449. wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
  450. ssid->key_mgmt = val;
  451. return errors ? -1 : 0;
  452. }
  453. #ifndef NO_CONFIG_WRITE
  454. static char * wpa_config_write_key_mgmt(const struct parse_data *data,
  455. struct wpa_ssid *ssid)
  456. {
  457. char *buf, *pos, *end;
  458. int ret;
  459. pos = buf = os_zalloc(50);
  460. if (buf == NULL)
  461. return NULL;
  462. end = buf + 50;
  463. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  464. ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
  465. pos == buf ? "" : " ");
  466. if (ret < 0 || ret >= end - pos) {
  467. end[-1] = '\0';
  468. return buf;
  469. }
  470. pos += ret;
  471. }
  472. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  473. ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
  474. pos == buf ? "" : " ");
  475. if (ret < 0 || ret >= end - pos) {
  476. end[-1] = '\0';
  477. return buf;
  478. }
  479. pos += ret;
  480. }
  481. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  482. ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
  483. pos == buf ? "" : " ");
  484. if (ret < 0 || ret >= end - pos) {
  485. end[-1] = '\0';
  486. return buf;
  487. }
  488. pos += ret;
  489. }
  490. if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
  491. ret = os_snprintf(pos, end - pos, "%sNONE",
  492. pos == buf ? "" : " ");
  493. if (ret < 0 || ret >= end - pos) {
  494. end[-1] = '\0';
  495. return buf;
  496. }
  497. pos += ret;
  498. }
  499. if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  500. ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
  501. pos == buf ? "" : " ");
  502. if (ret < 0 || ret >= end - pos) {
  503. end[-1] = '\0';
  504. return buf;
  505. }
  506. pos += ret;
  507. }
  508. #ifdef CONFIG_IEEE80211R
  509. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  510. pos += os_snprintf(pos, end - pos, "%sFT-PSK",
  511. pos == buf ? "" : " ");
  512. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  513. pos += os_snprintf(pos, end - pos, "%sFT-EAP",
  514. pos == buf ? "" : " ");
  515. #endif /* CONFIG_IEEE80211R */
  516. #ifdef CONFIG_IEEE80211W
  517. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  518. pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
  519. pos == buf ? "" : " ");
  520. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  521. pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
  522. pos == buf ? "" : " ");
  523. #endif /* CONFIG_IEEE80211W */
  524. #ifdef CONFIG_WPS
  525. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  526. pos += os_snprintf(pos, end - pos, "%sWPS",
  527. pos == buf ? "" : " ");
  528. #endif /* CONFIG_WPS */
  529. return buf;
  530. }
  531. #endif /* NO_CONFIG_WRITE */
  532. static int wpa_config_parse_cipher(int line, const char *value)
  533. {
  534. int val = wpa_parse_cipher(value);
  535. if (val < 0) {
  536. wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
  537. line, value);
  538. return -1;
  539. }
  540. if (val == 0) {
  541. wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
  542. line);
  543. return -1;
  544. }
  545. return val;
  546. }
  547. #ifndef NO_CONFIG_WRITE
  548. static char * wpa_config_write_cipher(int cipher)
  549. {
  550. char *buf = os_zalloc(50);
  551. if (buf == NULL)
  552. return NULL;
  553. if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
  554. os_free(buf);
  555. return NULL;
  556. }
  557. return buf;
  558. }
  559. #endif /* NO_CONFIG_WRITE */
  560. static int wpa_config_parse_pairwise(const struct parse_data *data,
  561. struct wpa_ssid *ssid, int line,
  562. const char *value)
  563. {
  564. int val;
  565. val = wpa_config_parse_cipher(line, value);
  566. if (val == -1)
  567. return -1;
  568. if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
  569. wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
  570. "(0x%x).", line, val);
  571. return -1;
  572. }
  573. wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
  574. ssid->pairwise_cipher = val;
  575. return 0;
  576. }
  577. #ifndef NO_CONFIG_WRITE
  578. static char * wpa_config_write_pairwise(const struct parse_data *data,
  579. struct wpa_ssid *ssid)
  580. {
  581. return wpa_config_write_cipher(ssid->pairwise_cipher);
  582. }
  583. #endif /* NO_CONFIG_WRITE */
  584. static int wpa_config_parse_group(const struct parse_data *data,
  585. struct wpa_ssid *ssid, int line,
  586. const char *value)
  587. {
  588. int val;
  589. val = wpa_config_parse_cipher(line, value);
  590. if (val == -1)
  591. return -1;
  592. if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
  593. wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
  594. "(0x%x).", line, val);
  595. return -1;
  596. }
  597. wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
  598. ssid->group_cipher = val;
  599. return 0;
  600. }
  601. #ifndef NO_CONFIG_WRITE
  602. static char * wpa_config_write_group(const struct parse_data *data,
  603. struct wpa_ssid *ssid)
  604. {
  605. return wpa_config_write_cipher(ssid->group_cipher);
  606. }
  607. #endif /* NO_CONFIG_WRITE */
  608. static int wpa_config_parse_auth_alg(const struct parse_data *data,
  609. struct wpa_ssid *ssid, int line,
  610. const char *value)
  611. {
  612. int val = 0, last, errors = 0;
  613. char *start, *end, *buf;
  614. buf = os_strdup(value);
  615. if (buf == NULL)
  616. return -1;
  617. start = buf;
  618. while (*start != '\0') {
  619. while (*start == ' ' || *start == '\t')
  620. start++;
  621. if (*start == '\0')
  622. break;
  623. end = start;
  624. while (*end != ' ' && *end != '\t' && *end != '\0')
  625. end++;
  626. last = *end == '\0';
  627. *end = '\0';
  628. if (os_strcmp(start, "OPEN") == 0)
  629. val |= WPA_AUTH_ALG_OPEN;
  630. else if (os_strcmp(start, "SHARED") == 0)
  631. val |= WPA_AUTH_ALG_SHARED;
  632. else if (os_strcmp(start, "LEAP") == 0)
  633. val |= WPA_AUTH_ALG_LEAP;
  634. else {
  635. wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
  636. line, start);
  637. errors++;
  638. }
  639. if (last)
  640. break;
  641. start = end + 1;
  642. }
  643. os_free(buf);
  644. if (val == 0) {
  645. wpa_printf(MSG_ERROR,
  646. "Line %d: no auth_alg values configured.", line);
  647. errors++;
  648. }
  649. wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
  650. ssid->auth_alg = val;
  651. return errors ? -1 : 0;
  652. }
  653. #ifndef NO_CONFIG_WRITE
  654. static char * wpa_config_write_auth_alg(const struct parse_data *data,
  655. struct wpa_ssid *ssid)
  656. {
  657. char *buf, *pos, *end;
  658. int ret;
  659. pos = buf = os_zalloc(30);
  660. if (buf == NULL)
  661. return NULL;
  662. end = buf + 30;
  663. if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
  664. ret = os_snprintf(pos, end - pos, "%sOPEN",
  665. pos == buf ? "" : " ");
  666. if (ret < 0 || ret >= end - pos) {
  667. end[-1] = '\0';
  668. return buf;
  669. }
  670. pos += ret;
  671. }
  672. if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
  673. ret = os_snprintf(pos, end - pos, "%sSHARED",
  674. pos == buf ? "" : " ");
  675. if (ret < 0 || ret >= end - pos) {
  676. end[-1] = '\0';
  677. return buf;
  678. }
  679. pos += ret;
  680. }
  681. if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
  682. ret = os_snprintf(pos, end - pos, "%sLEAP",
  683. pos == buf ? "" : " ");
  684. if (ret < 0 || ret >= end - pos) {
  685. end[-1] = '\0';
  686. return buf;
  687. }
  688. pos += ret;
  689. }
  690. return buf;
  691. }
  692. #endif /* NO_CONFIG_WRITE */
  693. static int * wpa_config_parse_int_array(const char *value)
  694. {
  695. int *freqs;
  696. size_t used, len;
  697. const char *pos;
  698. used = 0;
  699. len = 10;
  700. freqs = os_calloc(len + 1, sizeof(int));
  701. if (freqs == NULL)
  702. return NULL;
  703. pos = value;
  704. while (pos) {
  705. while (*pos == ' ')
  706. pos++;
  707. if (used == len) {
  708. int *n;
  709. size_t i;
  710. n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
  711. if (n == NULL) {
  712. os_free(freqs);
  713. return NULL;
  714. }
  715. for (i = len; i <= len * 2; i++)
  716. n[i] = 0;
  717. freqs = n;
  718. len *= 2;
  719. }
  720. freqs[used] = atoi(pos);
  721. if (freqs[used] == 0)
  722. break;
  723. used++;
  724. pos = os_strchr(pos + 1, ' ');
  725. }
  726. return freqs;
  727. }
  728. static int wpa_config_parse_scan_freq(const struct parse_data *data,
  729. struct wpa_ssid *ssid, int line,
  730. const char *value)
  731. {
  732. int *freqs;
  733. freqs = wpa_config_parse_int_array(value);
  734. if (freqs == NULL)
  735. return -1;
  736. os_free(ssid->scan_freq);
  737. ssid->scan_freq = freqs;
  738. return 0;
  739. }
  740. static int wpa_config_parse_freq_list(const struct parse_data *data,
  741. struct wpa_ssid *ssid, int line,
  742. const char *value)
  743. {
  744. int *freqs;
  745. freqs = wpa_config_parse_int_array(value);
  746. if (freqs == NULL)
  747. return -1;
  748. os_free(ssid->freq_list);
  749. ssid->freq_list = freqs;
  750. return 0;
  751. }
  752. #ifndef NO_CONFIG_WRITE
  753. static char * wpa_config_write_freqs(const struct parse_data *data,
  754. const int *freqs)
  755. {
  756. char *buf, *pos, *end;
  757. int i, ret;
  758. size_t count;
  759. if (freqs == NULL)
  760. return NULL;
  761. count = 0;
  762. for (i = 0; freqs[i]; i++)
  763. count++;
  764. pos = buf = os_zalloc(10 * count + 1);
  765. if (buf == NULL)
  766. return NULL;
  767. end = buf + 10 * count + 1;
  768. for (i = 0; freqs[i]; i++) {
  769. ret = os_snprintf(pos, end - pos, "%s%u",
  770. i == 0 ? "" : " ", freqs[i]);
  771. if (ret < 0 || ret >= end - pos) {
  772. end[-1] = '\0';
  773. return buf;
  774. }
  775. pos += ret;
  776. }
  777. return buf;
  778. }
  779. static char * wpa_config_write_scan_freq(const struct parse_data *data,
  780. struct wpa_ssid *ssid)
  781. {
  782. return wpa_config_write_freqs(data, ssid->scan_freq);
  783. }
  784. static char * wpa_config_write_freq_list(const struct parse_data *data,
  785. struct wpa_ssid *ssid)
  786. {
  787. return wpa_config_write_freqs(data, ssid->freq_list);
  788. }
  789. #endif /* NO_CONFIG_WRITE */
  790. #ifdef IEEE8021X_EAPOL
  791. static int wpa_config_parse_eap(const struct parse_data *data,
  792. struct wpa_ssid *ssid, int line,
  793. const char *value)
  794. {
  795. int last, errors = 0;
  796. char *start, *end, *buf;
  797. struct eap_method_type *methods = NULL, *tmp;
  798. size_t num_methods = 0;
  799. buf = os_strdup(value);
  800. if (buf == NULL)
  801. return -1;
  802. start = buf;
  803. while (*start != '\0') {
  804. while (*start == ' ' || *start == '\t')
  805. start++;
  806. if (*start == '\0')
  807. break;
  808. end = start;
  809. while (*end != ' ' && *end != '\t' && *end != '\0')
  810. end++;
  811. last = *end == '\0';
  812. *end = '\0';
  813. tmp = methods;
  814. methods = os_realloc_array(methods, num_methods + 1,
  815. sizeof(*methods));
  816. if (methods == NULL) {
  817. os_free(tmp);
  818. os_free(buf);
  819. return -1;
  820. }
  821. methods[num_methods].method = eap_peer_get_type(
  822. start, &methods[num_methods].vendor);
  823. if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  824. methods[num_methods].method == EAP_TYPE_NONE) {
  825. wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
  826. "'%s'", line, start);
  827. wpa_printf(MSG_ERROR, "You may need to add support for"
  828. " this EAP method during wpa_supplicant\n"
  829. "build time configuration.\n"
  830. "See README for more information.");
  831. errors++;
  832. } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  833. methods[num_methods].method == EAP_TYPE_LEAP)
  834. ssid->leap++;
  835. else
  836. ssid->non_leap++;
  837. num_methods++;
  838. if (last)
  839. break;
  840. start = end + 1;
  841. }
  842. os_free(buf);
  843. tmp = methods;
  844. methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
  845. if (methods == NULL) {
  846. os_free(tmp);
  847. return -1;
  848. }
  849. methods[num_methods].vendor = EAP_VENDOR_IETF;
  850. methods[num_methods].method = EAP_TYPE_NONE;
  851. num_methods++;
  852. wpa_hexdump(MSG_MSGDUMP, "eap methods",
  853. (u8 *) methods, num_methods * sizeof(*methods));
  854. os_free(ssid->eap.eap_methods);
  855. ssid->eap.eap_methods = methods;
  856. return errors ? -1 : 0;
  857. }
  858. static char * wpa_config_write_eap(const struct parse_data *data,
  859. struct wpa_ssid *ssid)
  860. {
  861. int i, ret;
  862. char *buf, *pos, *end;
  863. const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
  864. const char *name;
  865. if (eap_methods == NULL)
  866. return NULL;
  867. pos = buf = os_zalloc(100);
  868. if (buf == NULL)
  869. return NULL;
  870. end = buf + 100;
  871. for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
  872. eap_methods[i].method != EAP_TYPE_NONE; i++) {
  873. name = eap_get_name(eap_methods[i].vendor,
  874. eap_methods[i].method);
  875. if (name) {
  876. ret = os_snprintf(pos, end - pos, "%s%s",
  877. pos == buf ? "" : " ", name);
  878. if (ret < 0 || ret >= end - pos)
  879. break;
  880. pos += ret;
  881. }
  882. }
  883. end[-1] = '\0';
  884. return buf;
  885. }
  886. static int wpa_config_parse_password(const struct parse_data *data,
  887. struct wpa_ssid *ssid, int line,
  888. const char *value)
  889. {
  890. u8 *hash;
  891. if (os_strcmp(value, "NULL") == 0) {
  892. wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
  893. os_free(ssid->eap.password);
  894. ssid->eap.password = NULL;
  895. ssid->eap.password_len = 0;
  896. return 0;
  897. }
  898. #ifdef CONFIG_EXT_PASSWORD
  899. if (os_strncmp(value, "ext:", 4) == 0) {
  900. char *name = os_strdup(value + 4);
  901. if (name == NULL)
  902. return -1;
  903. os_free(ssid->eap.password);
  904. ssid->eap.password = (u8 *) name;
  905. ssid->eap.password_len = os_strlen(name);
  906. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  907. ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
  908. return 0;
  909. }
  910. #endif /* CONFIG_EXT_PASSWORD */
  911. if (os_strncmp(value, "hash:", 5) != 0) {
  912. char *tmp;
  913. size_t res_len;
  914. tmp = wpa_config_parse_string(value, &res_len);
  915. if (tmp == NULL) {
  916. wpa_printf(MSG_ERROR, "Line %d: failed to parse "
  917. "password.", line);
  918. return -1;
  919. }
  920. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  921. (u8 *) tmp, res_len);
  922. os_free(ssid->eap.password);
  923. ssid->eap.password = (u8 *) tmp;
  924. ssid->eap.password_len = res_len;
  925. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  926. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
  927. return 0;
  928. }
  929. /* NtPasswordHash: hash:<32 hex digits> */
  930. if (os_strlen(value + 5) != 2 * 16) {
  931. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
  932. "(expected 32 hex digits)", line);
  933. return -1;
  934. }
  935. hash = os_malloc(16);
  936. if (hash == NULL)
  937. return -1;
  938. if (hexstr2bin(value + 5, hash, 16)) {
  939. os_free(hash);
  940. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
  941. return -1;
  942. }
  943. wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
  944. os_free(ssid->eap.password);
  945. ssid->eap.password = hash;
  946. ssid->eap.password_len = 16;
  947. ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  948. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
  949. return 0;
  950. }
  951. static char * wpa_config_write_password(const struct parse_data *data,
  952. struct wpa_ssid *ssid)
  953. {
  954. char *buf;
  955. if (ssid->eap.password == NULL)
  956. return NULL;
  957. #ifdef CONFIG_EXT_PASSWORD
  958. if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
  959. buf = os_zalloc(4 + ssid->eap.password_len + 1);
  960. if (buf == NULL)
  961. return NULL;
  962. os_memcpy(buf, "ext:", 4);
  963. os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
  964. return buf;
  965. }
  966. #endif /* CONFIG_EXT_PASSWORD */
  967. if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
  968. return wpa_config_write_string(
  969. ssid->eap.password, ssid->eap.password_len);
  970. }
  971. buf = os_malloc(5 + 32 + 1);
  972. if (buf == NULL)
  973. return NULL;
  974. os_memcpy(buf, "hash:", 5);
  975. wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
  976. return buf;
  977. }
  978. #endif /* IEEE8021X_EAPOL */
  979. static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
  980. const char *value, int idx)
  981. {
  982. char *buf, title[20];
  983. int res;
  984. buf = wpa_config_parse_string(value, len);
  985. if (buf == NULL) {
  986. wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
  987. line, idx, value);
  988. return -1;
  989. }
  990. if (*len > MAX_WEP_KEY_LEN) {
  991. wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
  992. line, idx, value);
  993. os_free(buf);
  994. return -1;
  995. }
  996. if (*len && *len != 5 && *len != 13 && *len != 16) {
  997. wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
  998. "this network block will be ignored",
  999. line, (unsigned int) *len);
  1000. }
  1001. os_memcpy(key, buf, *len);
  1002. os_free(buf);
  1003. res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
  1004. if (res >= 0 && (size_t) res < sizeof(title))
  1005. wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
  1006. return 0;
  1007. }
  1008. static int wpa_config_parse_wep_key0(const struct parse_data *data,
  1009. struct wpa_ssid *ssid, int line,
  1010. const char *value)
  1011. {
  1012. return wpa_config_parse_wep_key(ssid->wep_key[0],
  1013. &ssid->wep_key_len[0], line,
  1014. value, 0);
  1015. }
  1016. static int wpa_config_parse_wep_key1(const struct parse_data *data,
  1017. struct wpa_ssid *ssid, int line,
  1018. const char *value)
  1019. {
  1020. return wpa_config_parse_wep_key(ssid->wep_key[1],
  1021. &ssid->wep_key_len[1], line,
  1022. value, 1);
  1023. }
  1024. static int wpa_config_parse_wep_key2(const struct parse_data *data,
  1025. struct wpa_ssid *ssid, int line,
  1026. const char *value)
  1027. {
  1028. return wpa_config_parse_wep_key(ssid->wep_key[2],
  1029. &ssid->wep_key_len[2], line,
  1030. value, 2);
  1031. }
  1032. static int wpa_config_parse_wep_key3(const struct parse_data *data,
  1033. struct wpa_ssid *ssid, int line,
  1034. const char *value)
  1035. {
  1036. return wpa_config_parse_wep_key(ssid->wep_key[3],
  1037. &ssid->wep_key_len[3], line,
  1038. value, 3);
  1039. }
  1040. #ifndef NO_CONFIG_WRITE
  1041. static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
  1042. {
  1043. if (ssid->wep_key_len[idx] == 0)
  1044. return NULL;
  1045. return wpa_config_write_string(ssid->wep_key[idx],
  1046. ssid->wep_key_len[idx]);
  1047. }
  1048. static char * wpa_config_write_wep_key0(const struct parse_data *data,
  1049. struct wpa_ssid *ssid)
  1050. {
  1051. return wpa_config_write_wep_key(ssid, 0);
  1052. }
  1053. static char * wpa_config_write_wep_key1(const struct parse_data *data,
  1054. struct wpa_ssid *ssid)
  1055. {
  1056. return wpa_config_write_wep_key(ssid, 1);
  1057. }
  1058. static char * wpa_config_write_wep_key2(const struct parse_data *data,
  1059. struct wpa_ssid *ssid)
  1060. {
  1061. return wpa_config_write_wep_key(ssid, 2);
  1062. }
  1063. static char * wpa_config_write_wep_key3(const struct parse_data *data,
  1064. struct wpa_ssid *ssid)
  1065. {
  1066. return wpa_config_write_wep_key(ssid, 3);
  1067. }
  1068. #endif /* NO_CONFIG_WRITE */
  1069. #ifdef CONFIG_P2P
  1070. static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
  1071. struct wpa_ssid *ssid, int line,
  1072. const char *value)
  1073. {
  1074. const char *pos;
  1075. u8 *buf, *n, addr[ETH_ALEN];
  1076. size_t count;
  1077. buf = NULL;
  1078. count = 0;
  1079. pos = value;
  1080. while (pos && *pos) {
  1081. while (*pos == ' ')
  1082. pos++;
  1083. if (hwaddr_aton(pos, addr)) {
  1084. if (count == 0) {
  1085. wpa_printf(MSG_ERROR, "Line %d: Invalid "
  1086. "p2p_client_list address '%s'.",
  1087. line, value);
  1088. os_free(buf);
  1089. return -1;
  1090. }
  1091. /* continue anyway since this could have been from a
  1092. * truncated configuration file line */
  1093. wpa_printf(MSG_INFO, "Line %d: Ignore likely "
  1094. "truncated p2p_client_list address '%s'",
  1095. line, pos);
  1096. } else {
  1097. n = os_realloc_array(buf, count + 1, ETH_ALEN);
  1098. if (n == NULL) {
  1099. os_free(buf);
  1100. return -1;
  1101. }
  1102. buf = n;
  1103. os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
  1104. os_memcpy(buf, addr, ETH_ALEN);
  1105. count++;
  1106. wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
  1107. addr, ETH_ALEN);
  1108. }
  1109. pos = os_strchr(pos, ' ');
  1110. }
  1111. os_free(ssid->p2p_client_list);
  1112. ssid->p2p_client_list = buf;
  1113. ssid->num_p2p_clients = count;
  1114. return 0;
  1115. }
  1116. #ifndef NO_CONFIG_WRITE
  1117. static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
  1118. struct wpa_ssid *ssid)
  1119. {
  1120. char *value, *end, *pos;
  1121. int res;
  1122. size_t i;
  1123. if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
  1124. return NULL;
  1125. value = os_malloc(20 * ssid->num_p2p_clients);
  1126. if (value == NULL)
  1127. return NULL;
  1128. pos = value;
  1129. end = value + 20 * ssid->num_p2p_clients;
  1130. for (i = ssid->num_p2p_clients; i > 0; i--) {
  1131. res = os_snprintf(pos, end - pos, MACSTR " ",
  1132. MAC2STR(ssid->p2p_client_list +
  1133. (i - 1) * ETH_ALEN));
  1134. if (res < 0 || res >= end - pos) {
  1135. os_free(value);
  1136. return NULL;
  1137. }
  1138. pos += res;
  1139. }
  1140. if (pos > value)
  1141. pos[-1] = '\0';
  1142. return value;
  1143. }
  1144. #endif /* NO_CONFIG_WRITE */
  1145. #endif /* CONFIG_P2P */
  1146. /* Helper macros for network block parser */
  1147. #ifdef OFFSET
  1148. #undef OFFSET
  1149. #endif /* OFFSET */
  1150. /* OFFSET: Get offset of a variable within the wpa_ssid structure */
  1151. #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
  1152. /* STR: Define a string variable for an ASCII string; f = field name */
  1153. #ifdef NO_CONFIG_WRITE
  1154. #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
  1155. #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
  1156. #else /* NO_CONFIG_WRITE */
  1157. #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
  1158. #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
  1159. #endif /* NO_CONFIG_WRITE */
  1160. #define STR(f) _STR(f), NULL, NULL, NULL, 0
  1161. #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
  1162. #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
  1163. #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
  1164. /* STR_LEN: Define a string variable with a separate variable for storing the
  1165. * data length. Unlike STR(), this can be used to store arbitrary binary data
  1166. * (i.e., even nul termination character). */
  1167. #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
  1168. #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
  1169. #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
  1170. #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
  1171. #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
  1172. /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
  1173. * explicitly specified. */
  1174. #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
  1175. #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
  1176. #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
  1177. #ifdef NO_CONFIG_WRITE
  1178. #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
  1179. #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
  1180. #else /* NO_CONFIG_WRITE */
  1181. #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1182. OFFSET(f), (void *) 0
  1183. #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1184. OFFSET(eap.f), (void *) 0
  1185. #endif /* NO_CONFIG_WRITE */
  1186. /* INT: Define an integer variable */
  1187. #define INT(f) _INT(f), NULL, NULL, 0
  1188. #define INTe(f) _INTe(f), NULL, NULL, 0
  1189. /* INT_RANGE: Define an integer variable with allowed value range */
  1190. #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
  1191. /* FUNC: Define a configuration variable that uses a custom function for
  1192. * parsing and writing the value. */
  1193. #ifdef NO_CONFIG_WRITE
  1194. #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
  1195. #else /* NO_CONFIG_WRITE */
  1196. #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
  1197. NULL, NULL, NULL, NULL
  1198. #endif /* NO_CONFIG_WRITE */
  1199. #define FUNC(f) _FUNC(f), 0
  1200. #define FUNC_KEY(f) _FUNC(f), 1
  1201. /*
  1202. * Table of network configuration variables. This table is used to parse each
  1203. * network configuration variable, e.g., each line in wpa_supplicant.conf file
  1204. * that is inside a network block.
  1205. *
  1206. * This table is generated using the helper macros defined above and with
  1207. * generous help from the C pre-processor. The field name is stored as a string
  1208. * into .name and for STR and INT types, the offset of the target buffer within
  1209. * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
  1210. * offset to the field containing the length of the configuration variable.
  1211. * .param3 and .param4 can be used to mark the allowed range (length for STR
  1212. * and value for INT).
  1213. *
  1214. * For each configuration line in wpa_supplicant.conf, the parser goes through
  1215. * this table and select the entry that matches with the field name. The parser
  1216. * function (.parser) is then called to parse the actual value of the field.
  1217. *
  1218. * This kind of mechanism makes it easy to add new configuration parameters,
  1219. * since only one line needs to be added into this table and into the
  1220. * struct wpa_ssid definition if the new variable is either a string or
  1221. * integer. More complex types will need to use their own parser and writer
  1222. * functions.
  1223. */
  1224. static const struct parse_data ssid_fields[] = {
  1225. { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
  1226. { INT_RANGE(scan_ssid, 0, 1) },
  1227. { FUNC(bssid) },
  1228. { FUNC_KEY(psk) },
  1229. { FUNC(proto) },
  1230. { FUNC(key_mgmt) },
  1231. { INT(bg_scan_period) },
  1232. { FUNC(pairwise) },
  1233. { FUNC(group) },
  1234. { FUNC(auth_alg) },
  1235. { FUNC(scan_freq) },
  1236. { FUNC(freq_list) },
  1237. #ifdef IEEE8021X_EAPOL
  1238. { FUNC(eap) },
  1239. { STR_LENe(identity) },
  1240. { STR_LENe(anonymous_identity) },
  1241. { FUNC_KEY(password) },
  1242. { STRe(ca_cert) },
  1243. { STRe(ca_path) },
  1244. { STRe(client_cert) },
  1245. { STRe(private_key) },
  1246. { STR_KEYe(private_key_passwd) },
  1247. { STRe(dh_file) },
  1248. { STRe(subject_match) },
  1249. { STRe(altsubject_match) },
  1250. { STRe(ca_cert2) },
  1251. { STRe(ca_path2) },
  1252. { STRe(client_cert2) },
  1253. { STRe(private_key2) },
  1254. { STR_KEYe(private_key2_passwd) },
  1255. { STRe(dh_file2) },
  1256. { STRe(subject_match2) },
  1257. { STRe(altsubject_match2) },
  1258. { STRe(phase1) },
  1259. { STRe(phase2) },
  1260. { STRe(pcsc) },
  1261. { STR_KEYe(pin) },
  1262. { STRe(engine_id) },
  1263. { STRe(key_id) },
  1264. { STRe(cert_id) },
  1265. { STRe(ca_cert_id) },
  1266. { STR_KEYe(pin2) },
  1267. { STRe(engine2_id) },
  1268. { STRe(key2_id) },
  1269. { STRe(cert2_id) },
  1270. { STRe(ca_cert2_id) },
  1271. { INTe(engine) },
  1272. { INTe(engine2) },
  1273. { INT(eapol_flags) },
  1274. #endif /* IEEE8021X_EAPOL */
  1275. { FUNC_KEY(wep_key0) },
  1276. { FUNC_KEY(wep_key1) },
  1277. { FUNC_KEY(wep_key2) },
  1278. { FUNC_KEY(wep_key3) },
  1279. { INT(wep_tx_keyidx) },
  1280. { INT(priority) },
  1281. #ifdef IEEE8021X_EAPOL
  1282. { INT(eap_workaround) },
  1283. { STRe(pac_file) },
  1284. { INTe(fragment_size) },
  1285. #endif /* IEEE8021X_EAPOL */
  1286. { INT_RANGE(mode, 0, 4) },
  1287. { INT_RANGE(proactive_key_caching, 0, 1) },
  1288. { INT_RANGE(disabled, 0, 2) },
  1289. { STR(id_str) },
  1290. #ifdef CONFIG_IEEE80211W
  1291. { INT_RANGE(ieee80211w, 0, 2) },
  1292. #endif /* CONFIG_IEEE80211W */
  1293. { INT_RANGE(peerkey, 0, 1) },
  1294. { INT_RANGE(mixed_cell, 0, 1) },
  1295. { INT_RANGE(frequency, 0, 65000) },
  1296. { INT(wpa_ptk_rekey) },
  1297. { STR(bgscan) },
  1298. { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
  1299. #ifdef CONFIG_P2P
  1300. { FUNC(p2p_client_list) },
  1301. #endif /* CONFIG_P2P */
  1302. #ifdef CONFIG_HT_OVERRIDES
  1303. { INT_RANGE(disable_ht, 0, 1) },
  1304. { INT_RANGE(disable_ht40, -1, 1) },
  1305. { INT_RANGE(disable_sgi, 0, 1) },
  1306. { INT_RANGE(disable_max_amsdu, -1, 1) },
  1307. { INT_RANGE(ampdu_factor, -1, 3) },
  1308. { INT_RANGE(ampdu_density, -1, 7) },
  1309. { STR(ht_mcs) },
  1310. #endif /* CONFIG_HT_OVERRIDES */
  1311. { INT(ap_max_inactivity) },
  1312. { INT(dtim_period) },
  1313. };
  1314. #undef OFFSET
  1315. #undef _STR
  1316. #undef STR
  1317. #undef STR_KEY
  1318. #undef _STR_LEN
  1319. #undef STR_LEN
  1320. #undef STR_LEN_KEY
  1321. #undef _STR_RANGE
  1322. #undef STR_RANGE
  1323. #undef STR_RANGE_KEY
  1324. #undef _INT
  1325. #undef INT
  1326. #undef INT_RANGE
  1327. #undef _FUNC
  1328. #undef FUNC
  1329. #undef FUNC_KEY
  1330. #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
  1331. /**
  1332. * wpa_config_add_prio_network - Add a network to priority lists
  1333. * @config: Configuration data from wpa_config_read()
  1334. * @ssid: Pointer to the network configuration to be added to the list
  1335. * Returns: 0 on success, -1 on failure
  1336. *
  1337. * This function is used to add a network block to the priority list of
  1338. * networks. This must be called for each network when reading in the full
  1339. * configuration. In addition, this can be used indirectly when updating
  1340. * priorities by calling wpa_config_update_prio_list().
  1341. */
  1342. int wpa_config_add_prio_network(struct wpa_config *config,
  1343. struct wpa_ssid *ssid)
  1344. {
  1345. int prio;
  1346. struct wpa_ssid *prev, **nlist;
  1347. /*
  1348. * Add to an existing priority list if one is available for the
  1349. * configured priority level for this network.
  1350. */
  1351. for (prio = 0; prio < config->num_prio; prio++) {
  1352. prev = config->pssid[prio];
  1353. if (prev->priority == ssid->priority) {
  1354. while (prev->pnext)
  1355. prev = prev->pnext;
  1356. prev->pnext = ssid;
  1357. return 0;
  1358. }
  1359. }
  1360. /* First network for this priority - add a new priority list */
  1361. nlist = os_realloc_array(config->pssid, config->num_prio + 1,
  1362. sizeof(struct wpa_ssid *));
  1363. if (nlist == NULL)
  1364. return -1;
  1365. for (prio = 0; prio < config->num_prio; prio++) {
  1366. if (nlist[prio]->priority < ssid->priority) {
  1367. os_memmove(&nlist[prio + 1], &nlist[prio],
  1368. (config->num_prio - prio) *
  1369. sizeof(struct wpa_ssid *));
  1370. break;
  1371. }
  1372. }
  1373. nlist[prio] = ssid;
  1374. config->num_prio++;
  1375. config->pssid = nlist;
  1376. return 0;
  1377. }
  1378. /**
  1379. * wpa_config_update_prio_list - Update network priority list
  1380. * @config: Configuration data from wpa_config_read()
  1381. * Returns: 0 on success, -1 on failure
  1382. *
  1383. * This function is called to update the priority list of networks in the
  1384. * configuration when a network is being added or removed. This is also called
  1385. * if a priority for a network is changed.
  1386. */
  1387. int wpa_config_update_prio_list(struct wpa_config *config)
  1388. {
  1389. struct wpa_ssid *ssid;
  1390. int ret = 0;
  1391. os_free(config->pssid);
  1392. config->pssid = NULL;
  1393. config->num_prio = 0;
  1394. ssid = config->ssid;
  1395. while (ssid) {
  1396. ssid->pnext = NULL;
  1397. if (wpa_config_add_prio_network(config, ssid) < 0)
  1398. ret = -1;
  1399. ssid = ssid->next;
  1400. }
  1401. return ret;
  1402. }
  1403. #ifdef IEEE8021X_EAPOL
  1404. static void eap_peer_config_free(struct eap_peer_config *eap)
  1405. {
  1406. os_free(eap->eap_methods);
  1407. os_free(eap->identity);
  1408. os_free(eap->anonymous_identity);
  1409. os_free(eap->password);
  1410. os_free(eap->ca_cert);
  1411. os_free(eap->ca_path);
  1412. os_free(eap->client_cert);
  1413. os_free(eap->private_key);
  1414. os_free(eap->private_key_passwd);
  1415. os_free(eap->dh_file);
  1416. os_free(eap->subject_match);
  1417. os_free(eap->altsubject_match);
  1418. os_free(eap->ca_cert2);
  1419. os_free(eap->ca_path2);
  1420. os_free(eap->client_cert2);
  1421. os_free(eap->private_key2);
  1422. os_free(eap->private_key2_passwd);
  1423. os_free(eap->dh_file2);
  1424. os_free(eap->subject_match2);
  1425. os_free(eap->altsubject_match2);
  1426. os_free(eap->phase1);
  1427. os_free(eap->phase2);
  1428. os_free(eap->pcsc);
  1429. os_free(eap->pin);
  1430. os_free(eap->engine_id);
  1431. os_free(eap->key_id);
  1432. os_free(eap->cert_id);
  1433. os_free(eap->ca_cert_id);
  1434. os_free(eap->key2_id);
  1435. os_free(eap->cert2_id);
  1436. os_free(eap->ca_cert2_id);
  1437. os_free(eap->pin2);
  1438. os_free(eap->engine2_id);
  1439. os_free(eap->otp);
  1440. os_free(eap->pending_req_otp);
  1441. os_free(eap->pac_file);
  1442. os_free(eap->new_password);
  1443. }
  1444. #endif /* IEEE8021X_EAPOL */
  1445. /**
  1446. * wpa_config_free_ssid - Free network/ssid configuration data
  1447. * @ssid: Configuration data for the network
  1448. *
  1449. * This function frees all resources allocated for the network configuration
  1450. * data.
  1451. */
  1452. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1453. {
  1454. os_free(ssid->ssid);
  1455. os_free(ssid->passphrase);
  1456. os_free(ssid->ext_psk);
  1457. #ifdef IEEE8021X_EAPOL
  1458. eap_peer_config_free(&ssid->eap);
  1459. #endif /* IEEE8021X_EAPOL */
  1460. os_free(ssid->id_str);
  1461. os_free(ssid->scan_freq);
  1462. os_free(ssid->freq_list);
  1463. os_free(ssid->bgscan);
  1464. os_free(ssid->p2p_client_list);
  1465. #ifdef CONFIG_HT_OVERRIDES
  1466. os_free(ssid->ht_mcs);
  1467. #endif /* CONFIG_HT_OVERRIDES */
  1468. os_free(ssid);
  1469. }
  1470. void wpa_config_free_cred(struct wpa_cred *cred)
  1471. {
  1472. os_free(cred->realm);
  1473. os_free(cred->username);
  1474. os_free(cred->password);
  1475. os_free(cred->ca_cert);
  1476. os_free(cred->client_cert);
  1477. os_free(cred->private_key);
  1478. os_free(cred->private_key_passwd);
  1479. os_free(cred->imsi);
  1480. os_free(cred->milenage);
  1481. os_free(cred->domain);
  1482. os_free(cred->eap_method);
  1483. os_free(cred->phase1);
  1484. os_free(cred->phase2);
  1485. os_free(cred->excluded_ssid);
  1486. os_free(cred);
  1487. }
  1488. /**
  1489. * wpa_config_free - Free configuration data
  1490. * @config: Configuration data from wpa_config_read()
  1491. *
  1492. * This function frees all resources allocated for the configuration data by
  1493. * wpa_config_read().
  1494. */
  1495. void wpa_config_free(struct wpa_config *config)
  1496. {
  1497. #ifndef CONFIG_NO_CONFIG_BLOBS
  1498. struct wpa_config_blob *blob, *prevblob;
  1499. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1500. struct wpa_ssid *ssid, *prev = NULL;
  1501. struct wpa_cred *cred, *cprev;
  1502. ssid = config->ssid;
  1503. while (ssid) {
  1504. prev = ssid;
  1505. ssid = ssid->next;
  1506. wpa_config_free_ssid(prev);
  1507. }
  1508. cred = config->cred;
  1509. while (cred) {
  1510. cprev = cred;
  1511. cred = cred->next;
  1512. wpa_config_free_cred(cprev);
  1513. }
  1514. #ifndef CONFIG_NO_CONFIG_BLOBS
  1515. blob = config->blobs;
  1516. prevblob = NULL;
  1517. while (blob) {
  1518. prevblob = blob;
  1519. blob = blob->next;
  1520. wpa_config_free_blob(prevblob);
  1521. }
  1522. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1523. wpabuf_free(config->wps_vendor_ext_m1);
  1524. os_free(config->ctrl_interface);
  1525. os_free(config->ctrl_interface_group);
  1526. os_free(config->opensc_engine_path);
  1527. os_free(config->pkcs11_engine_path);
  1528. os_free(config->pkcs11_module_path);
  1529. os_free(config->pcsc_reader);
  1530. os_free(config->pcsc_pin);
  1531. os_free(config->driver_param);
  1532. os_free(config->device_name);
  1533. os_free(config->manufacturer);
  1534. os_free(config->model_name);
  1535. os_free(config->model_number);
  1536. os_free(config->serial_number);
  1537. os_free(config->config_methods);
  1538. os_free(config->p2p_ssid_postfix);
  1539. os_free(config->pssid);
  1540. os_free(config->p2p_pref_chan);
  1541. os_free(config->autoscan);
  1542. wpabuf_free(config->wps_nfc_dh_pubkey);
  1543. wpabuf_free(config->wps_nfc_dh_privkey);
  1544. wpabuf_free(config->wps_nfc_dev_pw);
  1545. os_free(config->ext_password_backend);
  1546. os_free(config->sae_groups);
  1547. os_free(config);
  1548. }
  1549. /**
  1550. * wpa_config_foreach_network - Iterate over each configured network
  1551. * @config: Configuration data from wpa_config_read()
  1552. * @func: Callback function to process each network
  1553. * @arg: Opaque argument to pass to callback function
  1554. *
  1555. * Iterate over the set of configured networks calling the specified
  1556. * function for each item. We guard against callbacks removing the
  1557. * supplied network.
  1558. */
  1559. void wpa_config_foreach_network(struct wpa_config *config,
  1560. void (*func)(void *, struct wpa_ssid *),
  1561. void *arg)
  1562. {
  1563. struct wpa_ssid *ssid, *next;
  1564. ssid = config->ssid;
  1565. while (ssid) {
  1566. next = ssid->next;
  1567. func(arg, ssid);
  1568. ssid = next;
  1569. }
  1570. }
  1571. /**
  1572. * wpa_config_get_network - Get configured network based on id
  1573. * @config: Configuration data from wpa_config_read()
  1574. * @id: Unique network id to search for
  1575. * Returns: Network configuration or %NULL if not found
  1576. */
  1577. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1578. {
  1579. struct wpa_ssid *ssid;
  1580. ssid = config->ssid;
  1581. while (ssid) {
  1582. if (id == ssid->id)
  1583. break;
  1584. ssid = ssid->next;
  1585. }
  1586. return ssid;
  1587. }
  1588. /**
  1589. * wpa_config_add_network - Add a new network with empty configuration
  1590. * @config: Configuration data from wpa_config_read()
  1591. * Returns: The new network configuration or %NULL if operation failed
  1592. */
  1593. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1594. {
  1595. int id;
  1596. struct wpa_ssid *ssid, *last = NULL;
  1597. id = -1;
  1598. ssid = config->ssid;
  1599. while (ssid) {
  1600. if (ssid->id > id)
  1601. id = ssid->id;
  1602. last = ssid;
  1603. ssid = ssid->next;
  1604. }
  1605. id++;
  1606. ssid = os_zalloc(sizeof(*ssid));
  1607. if (ssid == NULL)
  1608. return NULL;
  1609. ssid->id = id;
  1610. if (last)
  1611. last->next = ssid;
  1612. else
  1613. config->ssid = ssid;
  1614. wpa_config_update_prio_list(config);
  1615. return ssid;
  1616. }
  1617. /**
  1618. * wpa_config_remove_network - Remove a configured network based on id
  1619. * @config: Configuration data from wpa_config_read()
  1620. * @id: Unique network id to search for
  1621. * Returns: 0 on success, or -1 if the network was not found
  1622. */
  1623. int wpa_config_remove_network(struct wpa_config *config, int id)
  1624. {
  1625. struct wpa_ssid *ssid, *prev = NULL;
  1626. ssid = config->ssid;
  1627. while (ssid) {
  1628. if (id == ssid->id)
  1629. break;
  1630. prev = ssid;
  1631. ssid = ssid->next;
  1632. }
  1633. if (ssid == NULL)
  1634. return -1;
  1635. if (prev)
  1636. prev->next = ssid->next;
  1637. else
  1638. config->ssid = ssid->next;
  1639. wpa_config_update_prio_list(config);
  1640. wpa_config_free_ssid(ssid);
  1641. return 0;
  1642. }
  1643. /**
  1644. * wpa_config_set_network_defaults - Set network default values
  1645. * @ssid: Pointer to network configuration data
  1646. */
  1647. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1648. {
  1649. ssid->proto = DEFAULT_PROTO;
  1650. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1651. ssid->group_cipher = DEFAULT_GROUP;
  1652. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1653. ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
  1654. #ifdef IEEE8021X_EAPOL
  1655. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1656. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1657. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1658. #endif /* IEEE8021X_EAPOL */
  1659. #ifdef CONFIG_HT_OVERRIDES
  1660. ssid->disable_ht = DEFAULT_DISABLE_HT;
  1661. ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
  1662. ssid->disable_sgi = DEFAULT_DISABLE_SGI;
  1663. ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
  1664. ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
  1665. ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
  1666. #endif /* CONFIG_HT_OVERRIDES */
  1667. ssid->proactive_key_caching = -1;
  1668. #ifdef CONFIG_IEEE80211W
  1669. ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
  1670. #endif /* CONFIG_IEEE80211W */
  1671. }
  1672. /**
  1673. * wpa_config_set - Set a variable in network configuration
  1674. * @ssid: Pointer to network configuration data
  1675. * @var: Variable name, e.g., "ssid"
  1676. * @value: Variable value
  1677. * @line: Line number in configuration file or 0 if not used
  1678. * Returns: 0 on success, -1 on failure
  1679. *
  1680. * This function can be used to set network configuration variables based on
  1681. * both the configuration file and management interface input. The value
  1682. * parameter must be in the same format as the text-based configuration file is
  1683. * using. For example, strings are using double quotation marks.
  1684. */
  1685. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1686. int line)
  1687. {
  1688. size_t i;
  1689. int ret = 0;
  1690. if (ssid == NULL || var == NULL || value == NULL)
  1691. return -1;
  1692. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1693. const struct parse_data *field = &ssid_fields[i];
  1694. if (os_strcmp(var, field->name) != 0)
  1695. continue;
  1696. if (field->parser(field, ssid, line, value)) {
  1697. if (line) {
  1698. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1699. "parse %s '%s'.", line, var, value);
  1700. }
  1701. ret = -1;
  1702. }
  1703. break;
  1704. }
  1705. if (i == NUM_SSID_FIELDS) {
  1706. if (line) {
  1707. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1708. "'%s'.", line, var);
  1709. }
  1710. ret = -1;
  1711. }
  1712. return ret;
  1713. }
  1714. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  1715. const char *value)
  1716. {
  1717. size_t len;
  1718. char *buf;
  1719. int ret;
  1720. len = os_strlen(value);
  1721. buf = os_malloc(len + 3);
  1722. if (buf == NULL)
  1723. return -1;
  1724. buf[0] = '"';
  1725. os_memcpy(buf + 1, value, len);
  1726. buf[len + 1] = '"';
  1727. buf[len + 2] = '\0';
  1728. ret = wpa_config_set(ssid, var, buf, 0);
  1729. os_free(buf);
  1730. return ret;
  1731. }
  1732. /**
  1733. * wpa_config_get_all - Get all options from network configuration
  1734. * @ssid: Pointer to network configuration data
  1735. * @get_keys: Determines if keys/passwords will be included in returned list
  1736. * (if they may be exported)
  1737. * Returns: %NULL terminated list of all set keys and their values in the form
  1738. * of [key1, val1, key2, val2, ... , NULL]
  1739. *
  1740. * This function can be used to get list of all configured network properties.
  1741. * The caller is responsible for freeing the returned list and all its
  1742. * elements.
  1743. */
  1744. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  1745. {
  1746. const struct parse_data *field;
  1747. char *key, *value;
  1748. size_t i;
  1749. char **props;
  1750. int fields_num;
  1751. get_keys = get_keys && ssid->export_keys;
  1752. props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
  1753. if (!props)
  1754. return NULL;
  1755. fields_num = 0;
  1756. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1757. field = &ssid_fields[i];
  1758. if (field->key_data && !get_keys)
  1759. continue;
  1760. value = field->writer(field, ssid);
  1761. if (value == NULL)
  1762. continue;
  1763. if (os_strlen(value) == 0) {
  1764. os_free(value);
  1765. continue;
  1766. }
  1767. key = os_strdup(field->name);
  1768. if (key == NULL) {
  1769. os_free(value);
  1770. goto err;
  1771. }
  1772. props[fields_num * 2] = key;
  1773. props[fields_num * 2 + 1] = value;
  1774. fields_num++;
  1775. }
  1776. return props;
  1777. err:
  1778. value = *props;
  1779. while (value)
  1780. os_free(value++);
  1781. os_free(props);
  1782. return NULL;
  1783. }
  1784. #ifndef NO_CONFIG_WRITE
  1785. /**
  1786. * wpa_config_get - Get a variable in network configuration
  1787. * @ssid: Pointer to network configuration data
  1788. * @var: Variable name, e.g., "ssid"
  1789. * Returns: Value of the variable or %NULL on failure
  1790. *
  1791. * This function can be used to get network configuration variables. The
  1792. * returned value is a copy of the configuration variable in text format, i.e,.
  1793. * the same format that the text-based configuration file and wpa_config_set()
  1794. * are using for the value. The caller is responsible for freeing the returned
  1795. * value.
  1796. */
  1797. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1798. {
  1799. size_t i;
  1800. if (ssid == NULL || var == NULL)
  1801. return NULL;
  1802. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1803. const struct parse_data *field = &ssid_fields[i];
  1804. if (os_strcmp(var, field->name) == 0)
  1805. return field->writer(field, ssid);
  1806. }
  1807. return NULL;
  1808. }
  1809. /**
  1810. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1811. * @ssid: Pointer to network configuration data
  1812. * @var: Variable name, e.g., "ssid"
  1813. * Returns: Value of the variable or %NULL on failure
  1814. *
  1815. * This function can be used to get network configuration variable like
  1816. * wpa_config_get(). The only difference is that this functions does not expose
  1817. * key/password material from the configuration. In case a key/password field
  1818. * is requested, the returned value is an empty string or %NULL if the variable
  1819. * is not set or "*" if the variable is set (regardless of its value). The
  1820. * returned value is a copy of the configuration variable in text format, i.e,.
  1821. * the same format that the text-based configuration file and wpa_config_set()
  1822. * are using for the value. The caller is responsible for freeing the returned
  1823. * value.
  1824. */
  1825. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1826. {
  1827. size_t i;
  1828. if (ssid == NULL || var == NULL)
  1829. return NULL;
  1830. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1831. const struct parse_data *field = &ssid_fields[i];
  1832. if (os_strcmp(var, field->name) == 0) {
  1833. char *res = field->writer(field, ssid);
  1834. if (field->key_data) {
  1835. if (res && res[0]) {
  1836. wpa_printf(MSG_DEBUG, "Do not allow "
  1837. "key_data field to be "
  1838. "exposed");
  1839. os_free(res);
  1840. return os_strdup("*");
  1841. }
  1842. os_free(res);
  1843. return NULL;
  1844. }
  1845. return res;
  1846. }
  1847. }
  1848. return NULL;
  1849. }
  1850. #endif /* NO_CONFIG_WRITE */
  1851. /**
  1852. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  1853. * @ssid: Pointer to network configuration data
  1854. *
  1855. * This function must be called to update WPA PSK when either SSID or the
  1856. * passphrase has changed for the network configuration.
  1857. */
  1858. void wpa_config_update_psk(struct wpa_ssid *ssid)
  1859. {
  1860. #ifndef CONFIG_NO_PBKDF2
  1861. pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
  1862. ssid->psk, PMK_LEN);
  1863. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1864. ssid->psk, PMK_LEN);
  1865. ssid->psk_set = 1;
  1866. #endif /* CONFIG_NO_PBKDF2 */
  1867. }
  1868. int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
  1869. const char *value, int line)
  1870. {
  1871. char *val;
  1872. size_t len;
  1873. if (os_strcmp(var, "priority") == 0) {
  1874. cred->priority = atoi(value);
  1875. return 0;
  1876. }
  1877. if (os_strcmp(var, "pcsc") == 0) {
  1878. cred->pcsc = atoi(value);
  1879. return 0;
  1880. }
  1881. if (os_strcmp(var, "eap") == 0) {
  1882. struct eap_method_type method;
  1883. method.method = eap_peer_get_type(value, &method.vendor);
  1884. if (method.vendor == EAP_VENDOR_IETF &&
  1885. method.method == EAP_TYPE_NONE) {
  1886. wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
  1887. "for a credential", line, value);
  1888. return -1;
  1889. }
  1890. os_free(cred->eap_method);
  1891. cred->eap_method = os_malloc(sizeof(*cred->eap_method));
  1892. if (cred->eap_method == NULL)
  1893. return -1;
  1894. os_memcpy(cred->eap_method, &method, sizeof(method));
  1895. return 0;
  1896. }
  1897. if (os_strcmp(var, "password") == 0 &&
  1898. os_strncmp(value, "ext:", 4) == 0) {
  1899. os_free(cred->password);
  1900. cred->password = os_strdup(value);
  1901. cred->ext_password = 1;
  1902. return 0;
  1903. }
  1904. val = wpa_config_parse_string(value, &len);
  1905. if (val == NULL) {
  1906. wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
  1907. "value '%s'.", line, var, value);
  1908. return -1;
  1909. }
  1910. if (os_strcmp(var, "realm") == 0) {
  1911. os_free(cred->realm);
  1912. cred->realm = val;
  1913. return 0;
  1914. }
  1915. if (os_strcmp(var, "username") == 0) {
  1916. os_free(cred->username);
  1917. cred->username = val;
  1918. return 0;
  1919. }
  1920. if (os_strcmp(var, "password") == 0) {
  1921. os_free(cred->password);
  1922. cred->password = val;
  1923. cred->ext_password = 0;
  1924. return 0;
  1925. }
  1926. if (os_strcmp(var, "ca_cert") == 0) {
  1927. os_free(cred->ca_cert);
  1928. cred->ca_cert = val;
  1929. return 0;
  1930. }
  1931. if (os_strcmp(var, "client_cert") == 0) {
  1932. os_free(cred->client_cert);
  1933. cred->client_cert = val;
  1934. return 0;
  1935. }
  1936. if (os_strcmp(var, "private_key") == 0) {
  1937. os_free(cred->private_key);
  1938. cred->private_key = val;
  1939. return 0;
  1940. }
  1941. if (os_strcmp(var, "private_key_passwd") == 0) {
  1942. os_free(cred->private_key_passwd);
  1943. cred->private_key_passwd = val;
  1944. return 0;
  1945. }
  1946. if (os_strcmp(var, "imsi") == 0) {
  1947. os_free(cred->imsi);
  1948. cred->imsi = val;
  1949. return 0;
  1950. }
  1951. if (os_strcmp(var, "milenage") == 0) {
  1952. os_free(cred->milenage);
  1953. cred->milenage = val;
  1954. return 0;
  1955. }
  1956. if (os_strcmp(var, "domain") == 0) {
  1957. os_free(cred->domain);
  1958. cred->domain = val;
  1959. return 0;
  1960. }
  1961. if (os_strcmp(var, "phase1") == 0) {
  1962. os_free(cred->phase1);
  1963. cred->phase1 = val;
  1964. return 0;
  1965. }
  1966. if (os_strcmp(var, "phase2") == 0) {
  1967. os_free(cred->phase2);
  1968. cred->phase2 = val;
  1969. return 0;
  1970. }
  1971. if (os_strcmp(var, "roaming_consortium") == 0) {
  1972. if (len < 3 || len > sizeof(cred->roaming_consortium)) {
  1973. wpa_printf(MSG_ERROR, "Line %d: invalid "
  1974. "roaming_consortium length %d (3..15 "
  1975. "expected)", line, (int) len);
  1976. os_free(val);
  1977. return -1;
  1978. }
  1979. os_memcpy(cred->roaming_consortium, val, len);
  1980. cred->roaming_consortium_len = len;
  1981. os_free(val);
  1982. return 0;
  1983. }
  1984. if (os_strcmp(var, "excluded_ssid") == 0) {
  1985. struct excluded_ssid *e;
  1986. if (len > MAX_SSID_LEN) {
  1987. wpa_printf(MSG_ERROR, "Line %d: invalid "
  1988. "excluded_ssid length %d", line, (int) len);
  1989. os_free(val);
  1990. return -1;
  1991. }
  1992. e = os_realloc_array(cred->excluded_ssid,
  1993. cred->num_excluded_ssid + 1,
  1994. sizeof(struct excluded_ssid));
  1995. if (e == NULL) {
  1996. os_free(val);
  1997. return -1;
  1998. }
  1999. cred->excluded_ssid = e;
  2000. e = &cred->excluded_ssid[cred->num_excluded_ssid++];
  2001. os_memcpy(e->ssid, val, len);
  2002. e->ssid_len = len;
  2003. os_free(val);
  2004. return 0;
  2005. }
  2006. if (line) {
  2007. wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
  2008. line, var);
  2009. }
  2010. os_free(val);
  2011. return -1;
  2012. }
  2013. struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
  2014. {
  2015. struct wpa_cred *cred;
  2016. cred = config->cred;
  2017. while (cred) {
  2018. if (id == cred->id)
  2019. break;
  2020. cred = cred->next;
  2021. }
  2022. return cred;
  2023. }
  2024. struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
  2025. {
  2026. int id;
  2027. struct wpa_cred *cred, *last = NULL;
  2028. id = -1;
  2029. cred = config->cred;
  2030. while (cred) {
  2031. if (cred->id > id)
  2032. id = cred->id;
  2033. last = cred;
  2034. cred = cred->next;
  2035. }
  2036. id++;
  2037. cred = os_zalloc(sizeof(*cred));
  2038. if (cred == NULL)
  2039. return NULL;
  2040. cred->id = id;
  2041. if (last)
  2042. last->next = cred;
  2043. else
  2044. config->cred = cred;
  2045. return cred;
  2046. }
  2047. int wpa_config_remove_cred(struct wpa_config *config, int id)
  2048. {
  2049. struct wpa_cred *cred, *prev = NULL;
  2050. cred = config->cred;
  2051. while (cred) {
  2052. if (id == cred->id)
  2053. break;
  2054. prev = cred;
  2055. cred = cred->next;
  2056. }
  2057. if (cred == NULL)
  2058. return -1;
  2059. if (prev)
  2060. prev->next = cred->next;
  2061. else
  2062. config->cred = cred->next;
  2063. wpa_config_free_cred(cred);
  2064. return 0;
  2065. }
  2066. #ifndef CONFIG_NO_CONFIG_BLOBS
  2067. /**
  2068. * wpa_config_get_blob - Get a named configuration blob
  2069. * @config: Configuration data from wpa_config_read()
  2070. * @name: Name of the blob
  2071. * Returns: Pointer to blob data or %NULL if not found
  2072. */
  2073. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  2074. const char *name)
  2075. {
  2076. struct wpa_config_blob *blob = config->blobs;
  2077. while (blob) {
  2078. if (os_strcmp(blob->name, name) == 0)
  2079. return blob;
  2080. blob = blob->next;
  2081. }
  2082. return NULL;
  2083. }
  2084. /**
  2085. * wpa_config_set_blob - Set or add a named configuration blob
  2086. * @config: Configuration data from wpa_config_read()
  2087. * @blob: New value for the blob
  2088. *
  2089. * Adds a new configuration blob or replaces the current value of an existing
  2090. * blob.
  2091. */
  2092. void wpa_config_set_blob(struct wpa_config *config,
  2093. struct wpa_config_blob *blob)
  2094. {
  2095. wpa_config_remove_blob(config, blob->name);
  2096. blob->next = config->blobs;
  2097. config->blobs = blob;
  2098. }
  2099. /**
  2100. * wpa_config_free_blob - Free blob data
  2101. * @blob: Pointer to blob to be freed
  2102. */
  2103. void wpa_config_free_blob(struct wpa_config_blob *blob)
  2104. {
  2105. if (blob) {
  2106. os_free(blob->name);
  2107. os_free(blob->data);
  2108. os_free(blob);
  2109. }
  2110. }
  2111. /**
  2112. * wpa_config_remove_blob - Remove a named configuration blob
  2113. * @config: Configuration data from wpa_config_read()
  2114. * @name: Name of the blob to remove
  2115. * Returns: 0 if blob was removed or -1 if blob was not found
  2116. */
  2117. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  2118. {
  2119. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  2120. while (pos) {
  2121. if (os_strcmp(pos->name, name) == 0) {
  2122. if (prev)
  2123. prev->next = pos->next;
  2124. else
  2125. config->blobs = pos->next;
  2126. wpa_config_free_blob(pos);
  2127. return 0;
  2128. }
  2129. prev = pos;
  2130. pos = pos->next;
  2131. }
  2132. return -1;
  2133. }
  2134. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2135. /**
  2136. * wpa_config_alloc_empty - Allocate an empty configuration
  2137. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  2138. * socket
  2139. * @driver_param: Driver parameters
  2140. * Returns: Pointer to allocated configuration data or %NULL on failure
  2141. */
  2142. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  2143. const char *driver_param)
  2144. {
  2145. struct wpa_config *config;
  2146. const int aCWmin = 4, aCWmax = 10;
  2147. const struct hostapd_wmm_ac_params ac_bk =
  2148. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  2149. const struct hostapd_wmm_ac_params ac_be =
  2150. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  2151. const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
  2152. { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
  2153. const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
  2154. { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
  2155. config = os_zalloc(sizeof(*config));
  2156. if (config == NULL)
  2157. return NULL;
  2158. config->eapol_version = DEFAULT_EAPOL_VERSION;
  2159. config->ap_scan = DEFAULT_AP_SCAN;
  2160. config->fast_reauth = DEFAULT_FAST_REAUTH;
  2161. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  2162. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  2163. config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
  2164. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  2165. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  2166. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  2167. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  2168. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  2169. config->wmm_ac_params[0] = ac_be;
  2170. config->wmm_ac_params[1] = ac_bk;
  2171. config->wmm_ac_params[2] = ac_vi;
  2172. config->wmm_ac_params[3] = ac_vo;
  2173. if (ctrl_interface)
  2174. config->ctrl_interface = os_strdup(ctrl_interface);
  2175. if (driver_param)
  2176. config->driver_param = os_strdup(driver_param);
  2177. return config;
  2178. }
  2179. #ifndef CONFIG_NO_STDOUT_DEBUG
  2180. /**
  2181. * wpa_config_debug_dump_networks - Debug dump of configured networks
  2182. * @config: Configuration data from wpa_config_read()
  2183. */
  2184. void wpa_config_debug_dump_networks(struct wpa_config *config)
  2185. {
  2186. int prio;
  2187. struct wpa_ssid *ssid;
  2188. for (prio = 0; prio < config->num_prio; prio++) {
  2189. ssid = config->pssid[prio];
  2190. wpa_printf(MSG_DEBUG, "Priority group %d",
  2191. ssid->priority);
  2192. while (ssid) {
  2193. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  2194. ssid->id,
  2195. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2196. ssid = ssid->pnext;
  2197. }
  2198. }
  2199. }
  2200. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2201. struct global_parse_data {
  2202. char *name;
  2203. int (*parser)(const struct global_parse_data *data,
  2204. struct wpa_config *config, int line, const char *value);
  2205. void *param1, *param2, *param3;
  2206. unsigned int changed_flag;
  2207. };
  2208. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  2209. struct wpa_config *config, int line,
  2210. const char *pos)
  2211. {
  2212. int *dst;
  2213. dst = (int *) (((u8 *) config) + (long) data->param1);
  2214. *dst = atoi(pos);
  2215. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  2216. if (data->param2 && *dst < (long) data->param2) {
  2217. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  2218. "min_value=%ld)", line, data->name, *dst,
  2219. (long) data->param2);
  2220. *dst = (long) data->param2;
  2221. return -1;
  2222. }
  2223. if (data->param3 && *dst > (long) data->param3) {
  2224. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  2225. "max_value=%ld)", line, data->name, *dst,
  2226. (long) data->param3);
  2227. *dst = (long) data->param3;
  2228. return -1;
  2229. }
  2230. return 0;
  2231. }
  2232. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  2233. struct wpa_config *config, int line,
  2234. const char *pos)
  2235. {
  2236. size_t len;
  2237. char **dst, *tmp;
  2238. len = os_strlen(pos);
  2239. if (data->param2 && len < (size_t) data->param2) {
  2240. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  2241. "min_len=%ld)", line, data->name,
  2242. (unsigned long) len, (long) data->param2);
  2243. return -1;
  2244. }
  2245. if (data->param3 && len > (size_t) data->param3) {
  2246. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  2247. "max_len=%ld)", line, data->name,
  2248. (unsigned long) len, (long) data->param3);
  2249. return -1;
  2250. }
  2251. tmp = os_strdup(pos);
  2252. if (tmp == NULL)
  2253. return -1;
  2254. dst = (char **) (((u8 *) config) + (long) data->param1);
  2255. os_free(*dst);
  2256. *dst = tmp;
  2257. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  2258. return 0;
  2259. }
  2260. static int wpa_global_config_parse_bin(const struct global_parse_data *data,
  2261. struct wpa_config *config, int line,
  2262. const char *pos)
  2263. {
  2264. size_t len;
  2265. struct wpabuf **dst, *tmp;
  2266. len = os_strlen(pos);
  2267. if (len & 0x01)
  2268. return -1;
  2269. tmp = wpabuf_alloc(len / 2);
  2270. if (tmp == NULL)
  2271. return -1;
  2272. if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
  2273. wpabuf_free(tmp);
  2274. return -1;
  2275. }
  2276. dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
  2277. wpabuf_free(*dst);
  2278. *dst = tmp;
  2279. wpa_printf(MSG_DEBUG, "%s", data->name);
  2280. return 0;
  2281. }
  2282. static int wpa_config_process_country(const struct global_parse_data *data,
  2283. struct wpa_config *config, int line,
  2284. const char *pos)
  2285. {
  2286. if (!pos[0] || !pos[1]) {
  2287. wpa_printf(MSG_DEBUG, "Invalid country set");
  2288. return -1;
  2289. }
  2290. config->country[0] = pos[0];
  2291. config->country[1] = pos[1];
  2292. wpa_printf(MSG_DEBUG, "country='%c%c'",
  2293. config->country[0], config->country[1]);
  2294. return 0;
  2295. }
  2296. static int wpa_config_process_load_dynamic_eap(
  2297. const struct global_parse_data *data, struct wpa_config *config,
  2298. int line, const char *so)
  2299. {
  2300. int ret;
  2301. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  2302. ret = eap_peer_method_load(so);
  2303. if (ret == -2) {
  2304. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  2305. "reloading.");
  2306. } else if (ret) {
  2307. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  2308. "method '%s'.", line, so);
  2309. return -1;
  2310. }
  2311. return 0;
  2312. }
  2313. #ifdef CONFIG_WPS
  2314. static int wpa_config_process_uuid(const struct global_parse_data *data,
  2315. struct wpa_config *config, int line,
  2316. const char *pos)
  2317. {
  2318. char buf[40];
  2319. if (uuid_str2bin(pos, config->uuid)) {
  2320. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  2321. return -1;
  2322. }
  2323. uuid_bin2str(config->uuid, buf, sizeof(buf));
  2324. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  2325. return 0;
  2326. }
  2327. static int wpa_config_process_device_type(
  2328. const struct global_parse_data *data,
  2329. struct wpa_config *config, int line, const char *pos)
  2330. {
  2331. return wps_dev_type_str2bin(pos, config->device_type);
  2332. }
  2333. static int wpa_config_process_os_version(const struct global_parse_data *data,
  2334. struct wpa_config *config, int line,
  2335. const char *pos)
  2336. {
  2337. if (hexstr2bin(pos, config->os_version, 4)) {
  2338. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  2339. return -1;
  2340. }
  2341. wpa_printf(MSG_DEBUG, "os_version=%08x",
  2342. WPA_GET_BE32(config->os_version));
  2343. return 0;
  2344. }
  2345. static int wpa_config_process_wps_vendor_ext_m1(
  2346. const struct global_parse_data *data,
  2347. struct wpa_config *config, int line, const char *pos)
  2348. {
  2349. struct wpabuf *tmp;
  2350. int len = os_strlen(pos) / 2;
  2351. u8 *p;
  2352. if (!len) {
  2353. wpa_printf(MSG_ERROR, "Line %d: "
  2354. "invalid wps_vendor_ext_m1", line);
  2355. return -1;
  2356. }
  2357. tmp = wpabuf_alloc(len);
  2358. if (tmp) {
  2359. p = wpabuf_put(tmp, len);
  2360. if (hexstr2bin(pos, p, len)) {
  2361. wpa_printf(MSG_ERROR, "Line %d: "
  2362. "invalid wps_vendor_ext_m1", line);
  2363. wpabuf_free(tmp);
  2364. return -1;
  2365. }
  2366. wpabuf_free(config->wps_vendor_ext_m1);
  2367. config->wps_vendor_ext_m1 = tmp;
  2368. } else {
  2369. wpa_printf(MSG_ERROR, "Can not allocate "
  2370. "memory for wps_vendor_ext_m1");
  2371. return -1;
  2372. }
  2373. return 0;
  2374. }
  2375. #endif /* CONFIG_WPS */
  2376. #ifdef CONFIG_P2P
  2377. static int wpa_config_process_sec_device_type(
  2378. const struct global_parse_data *data,
  2379. struct wpa_config *config, int line, const char *pos)
  2380. {
  2381. int idx;
  2382. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  2383. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  2384. "items", line);
  2385. return -1;
  2386. }
  2387. idx = config->num_sec_device_types;
  2388. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  2389. return -1;
  2390. config->num_sec_device_types++;
  2391. return 0;
  2392. }
  2393. static int wpa_config_process_p2p_pref_chan(
  2394. const struct global_parse_data *data,
  2395. struct wpa_config *config, int line, const char *pos)
  2396. {
  2397. struct p2p_channel *pref = NULL, *n;
  2398. unsigned int num = 0;
  2399. const char *pos2;
  2400. u8 op_class, chan;
  2401. /* format: class:chan,class:chan,... */
  2402. while (*pos) {
  2403. op_class = atoi(pos);
  2404. pos2 = os_strchr(pos, ':');
  2405. if (pos2 == NULL)
  2406. goto fail;
  2407. pos2++;
  2408. chan = atoi(pos2);
  2409. n = os_realloc_array(pref, num + 1,
  2410. sizeof(struct p2p_channel));
  2411. if (n == NULL)
  2412. goto fail;
  2413. pref = n;
  2414. pref[num].op_class = op_class;
  2415. pref[num].chan = chan;
  2416. num++;
  2417. pos = os_strchr(pos2, ',');
  2418. if (pos == NULL)
  2419. break;
  2420. pos++;
  2421. }
  2422. os_free(config->p2p_pref_chan);
  2423. config->p2p_pref_chan = pref;
  2424. config->num_p2p_pref_chan = num;
  2425. wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
  2426. (u8 *) config->p2p_pref_chan,
  2427. config->num_p2p_pref_chan * sizeof(struct p2p_channel));
  2428. return 0;
  2429. fail:
  2430. os_free(pref);
  2431. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
  2432. return -1;
  2433. }
  2434. #endif /* CONFIG_P2P */
  2435. static int wpa_config_process_hessid(
  2436. const struct global_parse_data *data,
  2437. struct wpa_config *config, int line, const char *pos)
  2438. {
  2439. if (hwaddr_aton2(pos, config->hessid) < 0) {
  2440. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  2441. line, pos);
  2442. return -1;
  2443. }
  2444. return 0;
  2445. }
  2446. static int wpa_config_process_sae_groups(
  2447. const struct global_parse_data *data,
  2448. struct wpa_config *config, int line, const char *pos)
  2449. {
  2450. int *groups = wpa_config_parse_int_array(pos);
  2451. if (groups == NULL) {
  2452. wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
  2453. line, pos);
  2454. return -1;
  2455. }
  2456. os_free(config->sae_groups);
  2457. config->sae_groups = groups;
  2458. return 0;
  2459. }
  2460. #ifdef OFFSET
  2461. #undef OFFSET
  2462. #endif /* OFFSET */
  2463. /* OFFSET: Get offset of a variable within the wpa_config structure */
  2464. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  2465. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  2466. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  2467. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  2468. #define INT(f) _INT(f), NULL, NULL
  2469. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  2470. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  2471. #define STR(f) _STR(f), NULL, NULL
  2472. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  2473. #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
  2474. static const struct global_parse_data global_fields[] = {
  2475. #ifdef CONFIG_CTRL_IFACE
  2476. { STR(ctrl_interface), 0 },
  2477. { STR(ctrl_interface_group), 0 } /* deprecated */,
  2478. #endif /* CONFIG_CTRL_IFACE */
  2479. { INT_RANGE(eapol_version, 1, 2), 0 },
  2480. { INT(ap_scan), 0 },
  2481. { INT(disable_scan_offload), 0 },
  2482. { INT(fast_reauth), 0 },
  2483. { STR(opensc_engine_path), 0 },
  2484. { STR(pkcs11_engine_path), 0 },
  2485. { STR(pkcs11_module_path), 0 },
  2486. { STR(pcsc_reader), 0 },
  2487. { STR(pcsc_pin), 0 },
  2488. { STR(driver_param), 0 },
  2489. { INT(dot11RSNAConfigPMKLifetime), 0 },
  2490. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  2491. { INT(dot11RSNAConfigSATimeout), 0 },
  2492. #ifndef CONFIG_NO_CONFIG_WRITE
  2493. { INT(update_config), 0 },
  2494. #endif /* CONFIG_NO_CONFIG_WRITE */
  2495. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  2496. #ifdef CONFIG_WPS
  2497. { FUNC(uuid), CFG_CHANGED_UUID },
  2498. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  2499. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  2500. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  2501. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2502. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2503. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  2504. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  2505. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  2506. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  2507. { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
  2508. #endif /* CONFIG_WPS */
  2509. #ifdef CONFIG_P2P
  2510. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  2511. { INT(p2p_listen_reg_class), 0 },
  2512. { INT(p2p_listen_channel), 0 },
  2513. { INT(p2p_oper_reg_class), 0 },
  2514. { INT(p2p_oper_channel), 0 },
  2515. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  2516. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  2517. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  2518. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  2519. { INT(p2p_group_idle), 0 },
  2520. { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
  2521. { INT(p2p_go_ht40), 0 },
  2522. { INT(p2p_disabled), 0 },
  2523. { INT(p2p_no_group_iface), 0 },
  2524. #endif /* CONFIG_P2P */
  2525. { FUNC(country), CFG_CHANGED_COUNTRY },
  2526. { INT(bss_max_count), 0 },
  2527. { INT(bss_expiration_age), 0 },
  2528. { INT(bss_expiration_scan_count), 0 },
  2529. { INT_RANGE(filter_ssids, 0, 1), 0 },
  2530. { INT_RANGE(filter_rssi, -100, 0), 0 },
  2531. { INT(max_num_sta), 0 },
  2532. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  2533. #ifdef CONFIG_HS20
  2534. { INT_RANGE(hs20, 0, 1), 0 },
  2535. #endif /* CONFIG_HS20 */
  2536. { INT_RANGE(interworking, 0, 1), 0 },
  2537. { FUNC(hessid), 0 },
  2538. { INT_RANGE(access_network_type, 0, 15), 0 },
  2539. { INT_RANGE(pbc_in_m1, 0, 1), 0 },
  2540. { STR(autoscan), 0 },
  2541. { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
  2542. CFG_CHANGED_NFC_PASSWORD_TOKEN },
  2543. { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  2544. { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  2545. { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  2546. { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
  2547. { INT(p2p_go_max_inactivity), 0 },
  2548. { INT_RANGE(auto_interworking, 0, 1), 0 },
  2549. { INT(okc), 0 },
  2550. { INT(pmf), 0 },
  2551. { FUNC(sae_groups), 0 },
  2552. };
  2553. #undef FUNC
  2554. #undef _INT
  2555. #undef INT
  2556. #undef INT_RANGE
  2557. #undef _STR
  2558. #undef STR
  2559. #undef STR_RANGE
  2560. #undef BIN
  2561. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  2562. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  2563. {
  2564. size_t i;
  2565. int ret = 0;
  2566. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  2567. const struct global_parse_data *field = &global_fields[i];
  2568. size_t flen = os_strlen(field->name);
  2569. if (os_strncmp(pos, field->name, flen) != 0 ||
  2570. pos[flen] != '=')
  2571. continue;
  2572. if (field->parser(field, config, line, pos + flen + 1)) {
  2573. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2574. "parse '%s'.", line, pos);
  2575. ret = -1;
  2576. }
  2577. if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
  2578. config->wps_nfc_pw_from_config = 1;
  2579. config->changed_parameters |= field->changed_flag;
  2580. break;
  2581. }
  2582. if (i == NUM_GLOBAL_FIELDS) {
  2583. #ifdef CONFIG_AP
  2584. if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
  2585. char *tmp = os_strchr(pos, '=');
  2586. if (tmp == NULL) {
  2587. if (line < 0)
  2588. return -1;
  2589. wpa_printf(MSG_ERROR, "Line %d: invalid line "
  2590. "'%s'", line, pos);
  2591. return -1;
  2592. }
  2593. *tmp++ = '\0';
  2594. if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
  2595. tmp)) {
  2596. wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
  2597. "AC item", line);
  2598. return -1;
  2599. }
  2600. }
  2601. #endif /* CONFIG_AP */
  2602. if (line < 0)
  2603. return -1;
  2604. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  2605. line, pos);
  2606. ret = -1;
  2607. }
  2608. return ret;
  2609. }