config.c 63 KB

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