config.c 65 KB

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