config.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  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. };
  1357. #undef OFFSET
  1358. #undef _STR
  1359. #undef STR
  1360. #undef STR_KEY
  1361. #undef _STR_LEN
  1362. #undef STR_LEN
  1363. #undef STR_LEN_KEY
  1364. #undef _STR_RANGE
  1365. #undef STR_RANGE
  1366. #undef STR_RANGE_KEY
  1367. #undef _INT
  1368. #undef INT
  1369. #undef INT_RANGE
  1370. #undef _FUNC
  1371. #undef FUNC
  1372. #undef FUNC_KEY
  1373. #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
  1374. /**
  1375. * wpa_config_add_prio_network - Add a network to priority lists
  1376. * @config: Configuration data from wpa_config_read()
  1377. * @ssid: Pointer to the network configuration to be added to the list
  1378. * Returns: 0 on success, -1 on failure
  1379. *
  1380. * This function is used to add a network block to the priority list of
  1381. * networks. This must be called for each network when reading in the full
  1382. * configuration. In addition, this can be used indirectly when updating
  1383. * priorities by calling wpa_config_update_prio_list().
  1384. */
  1385. int wpa_config_add_prio_network(struct wpa_config *config,
  1386. struct wpa_ssid *ssid)
  1387. {
  1388. int prio;
  1389. struct wpa_ssid *prev, **nlist;
  1390. /*
  1391. * Add to an existing priority list if one is available for the
  1392. * configured priority level for this network.
  1393. */
  1394. for (prio = 0; prio < config->num_prio; prio++) {
  1395. prev = config->pssid[prio];
  1396. if (prev->priority == ssid->priority) {
  1397. while (prev->pnext)
  1398. prev = prev->pnext;
  1399. prev->pnext = ssid;
  1400. return 0;
  1401. }
  1402. }
  1403. /* First network for this priority - add a new priority list */
  1404. nlist = os_realloc(config->pssid,
  1405. (config->num_prio + 1) * sizeof(struct wpa_ssid *));
  1406. if (nlist == NULL)
  1407. return -1;
  1408. for (prio = 0; prio < config->num_prio; prio++) {
  1409. if (nlist[prio]->priority < ssid->priority)
  1410. break;
  1411. }
  1412. os_memmove(&nlist[prio + 1], &nlist[prio],
  1413. (config->num_prio - prio) * sizeof(struct wpa_ssid *));
  1414. nlist[prio] = ssid;
  1415. config->num_prio++;
  1416. config->pssid = nlist;
  1417. return 0;
  1418. }
  1419. /**
  1420. * wpa_config_update_prio_list - Update network priority list
  1421. * @config: Configuration data from wpa_config_read()
  1422. * Returns: 0 on success, -1 on failure
  1423. *
  1424. * This function is called to update the priority list of networks in the
  1425. * configuration when a network is being added or removed. This is also called
  1426. * if a priority for a network is changed.
  1427. */
  1428. int wpa_config_update_prio_list(struct wpa_config *config)
  1429. {
  1430. struct wpa_ssid *ssid;
  1431. int ret = 0;
  1432. os_free(config->pssid);
  1433. config->pssid = NULL;
  1434. config->num_prio = 0;
  1435. ssid = config->ssid;
  1436. while (ssid) {
  1437. ssid->pnext = NULL;
  1438. if (wpa_config_add_prio_network(config, ssid) < 0)
  1439. ret = -1;
  1440. ssid = ssid->next;
  1441. }
  1442. return ret;
  1443. }
  1444. #ifdef IEEE8021X_EAPOL
  1445. static void eap_peer_config_free(struct eap_peer_config *eap)
  1446. {
  1447. os_free(eap->eap_methods);
  1448. os_free(eap->identity);
  1449. os_free(eap->anonymous_identity);
  1450. os_free(eap->password);
  1451. os_free(eap->ca_cert);
  1452. os_free(eap->ca_path);
  1453. os_free(eap->client_cert);
  1454. os_free(eap->private_key);
  1455. os_free(eap->private_key_passwd);
  1456. os_free(eap->dh_file);
  1457. os_free(eap->subject_match);
  1458. os_free(eap->altsubject_match);
  1459. os_free(eap->ca_cert2);
  1460. os_free(eap->ca_path2);
  1461. os_free(eap->client_cert2);
  1462. os_free(eap->private_key2);
  1463. os_free(eap->private_key2_passwd);
  1464. os_free(eap->dh_file2);
  1465. os_free(eap->subject_match2);
  1466. os_free(eap->altsubject_match2);
  1467. os_free(eap->phase1);
  1468. os_free(eap->phase2);
  1469. os_free(eap->pcsc);
  1470. os_free(eap->pin);
  1471. os_free(eap->engine_id);
  1472. os_free(eap->key_id);
  1473. os_free(eap->cert_id);
  1474. os_free(eap->ca_cert_id);
  1475. os_free(eap->key2_id);
  1476. os_free(eap->cert2_id);
  1477. os_free(eap->ca_cert2_id);
  1478. os_free(eap->pin2);
  1479. os_free(eap->engine2_id);
  1480. os_free(eap->otp);
  1481. os_free(eap->pending_req_otp);
  1482. os_free(eap->pac_file);
  1483. os_free(eap->new_password);
  1484. }
  1485. #endif /* IEEE8021X_EAPOL */
  1486. /**
  1487. * wpa_config_free_ssid - Free network/ssid configuration data
  1488. * @ssid: Configuration data for the network
  1489. *
  1490. * This function frees all resources allocated for the network configuration
  1491. * data.
  1492. */
  1493. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1494. {
  1495. os_free(ssid->ssid);
  1496. os_free(ssid->passphrase);
  1497. #ifdef IEEE8021X_EAPOL
  1498. eap_peer_config_free(&ssid->eap);
  1499. #endif /* IEEE8021X_EAPOL */
  1500. os_free(ssid->id_str);
  1501. os_free(ssid->scan_freq);
  1502. os_free(ssid->freq_list);
  1503. os_free(ssid->bgscan);
  1504. os_free(ssid->p2p_client_list);
  1505. os_free(ssid);
  1506. }
  1507. /**
  1508. * wpa_config_free - Free configuration data
  1509. * @config: Configuration data from wpa_config_read()
  1510. *
  1511. * This function frees all resources allocated for the configuration data by
  1512. * wpa_config_read().
  1513. */
  1514. void wpa_config_free(struct wpa_config *config)
  1515. {
  1516. #ifndef CONFIG_NO_CONFIG_BLOBS
  1517. struct wpa_config_blob *blob, *prevblob;
  1518. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1519. struct wpa_ssid *ssid, *prev = NULL;
  1520. ssid = config->ssid;
  1521. while (ssid) {
  1522. prev = ssid;
  1523. ssid = ssid->next;
  1524. wpa_config_free_ssid(prev);
  1525. }
  1526. #ifndef CONFIG_NO_CONFIG_BLOBS
  1527. blob = config->blobs;
  1528. prevblob = NULL;
  1529. while (blob) {
  1530. prevblob = blob;
  1531. blob = blob->next;
  1532. wpa_config_free_blob(prevblob);
  1533. }
  1534. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1535. os_free(config->ctrl_interface);
  1536. os_free(config->ctrl_interface_group);
  1537. os_free(config->opensc_engine_path);
  1538. os_free(config->pkcs11_engine_path);
  1539. os_free(config->pkcs11_module_path);
  1540. os_free(config->driver_param);
  1541. os_free(config->device_name);
  1542. os_free(config->manufacturer);
  1543. os_free(config->model_name);
  1544. os_free(config->model_number);
  1545. os_free(config->serial_number);
  1546. os_free(config->config_methods);
  1547. os_free(config->p2p_ssid_postfix);
  1548. os_free(config->pssid);
  1549. os_free(config->home_realm);
  1550. os_free(config->home_username);
  1551. os_free(config->home_password);
  1552. os_free(config->home_ca_cert);
  1553. os_free(config->home_imsi);
  1554. os_free(config->home_milenage);
  1555. os_free(config);
  1556. }
  1557. /**
  1558. * wpa_config_foreach_network - Iterate over each configured network
  1559. * @config: Configuration data from wpa_config_read()
  1560. * @func: Callback function to process each network
  1561. * @arg: Opaque argument to pass to callback function
  1562. *
  1563. * Iterate over the set of configured networks calling the specified
  1564. * function for each item. We guard against callbacks removing the
  1565. * supplied network.
  1566. */
  1567. void wpa_config_foreach_network(struct wpa_config *config,
  1568. void (*func)(void *, struct wpa_ssid *),
  1569. void *arg)
  1570. {
  1571. struct wpa_ssid *ssid, *next;
  1572. ssid = config->ssid;
  1573. while (ssid) {
  1574. next = ssid->next;
  1575. func(arg, ssid);
  1576. ssid = next;
  1577. }
  1578. }
  1579. /**
  1580. * wpa_config_get_network - Get configured network based on id
  1581. * @config: Configuration data from wpa_config_read()
  1582. * @id: Unique network id to search for
  1583. * Returns: Network configuration or %NULL if not found
  1584. */
  1585. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1586. {
  1587. struct wpa_ssid *ssid;
  1588. ssid = config->ssid;
  1589. while (ssid) {
  1590. if (id == ssid->id)
  1591. break;
  1592. ssid = ssid->next;
  1593. }
  1594. return ssid;
  1595. }
  1596. /**
  1597. * wpa_config_add_network - Add a new network with empty configuration
  1598. * @config: Configuration data from wpa_config_read()
  1599. * Returns: The new network configuration or %NULL if operation failed
  1600. */
  1601. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1602. {
  1603. int id;
  1604. struct wpa_ssid *ssid, *last = NULL;
  1605. id = -1;
  1606. ssid = config->ssid;
  1607. while (ssid) {
  1608. if (ssid->id > id)
  1609. id = ssid->id;
  1610. last = ssid;
  1611. ssid = ssid->next;
  1612. }
  1613. id++;
  1614. ssid = os_zalloc(sizeof(*ssid));
  1615. if (ssid == NULL)
  1616. return NULL;
  1617. ssid->id = id;
  1618. if (last)
  1619. last->next = ssid;
  1620. else
  1621. config->ssid = ssid;
  1622. wpa_config_update_prio_list(config);
  1623. return ssid;
  1624. }
  1625. /**
  1626. * wpa_config_remove_network - Remove a configured network based on id
  1627. * @config: Configuration data from wpa_config_read()
  1628. * @id: Unique network id to search for
  1629. * Returns: 0 on success, or -1 if the network was not found
  1630. */
  1631. int wpa_config_remove_network(struct wpa_config *config, int id)
  1632. {
  1633. struct wpa_ssid *ssid, *prev = NULL;
  1634. ssid = config->ssid;
  1635. while (ssid) {
  1636. if (id == ssid->id)
  1637. break;
  1638. prev = ssid;
  1639. ssid = ssid->next;
  1640. }
  1641. if (ssid == NULL)
  1642. return -1;
  1643. if (prev)
  1644. prev->next = ssid->next;
  1645. else
  1646. config->ssid = ssid->next;
  1647. wpa_config_update_prio_list(config);
  1648. wpa_config_free_ssid(ssid);
  1649. return 0;
  1650. }
  1651. /**
  1652. * wpa_config_set_network_defaults - Set network default values
  1653. * @ssid: Pointer to network configuration data
  1654. */
  1655. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1656. {
  1657. ssid->proto = DEFAULT_PROTO;
  1658. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1659. ssid->group_cipher = DEFAULT_GROUP;
  1660. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1661. #ifdef IEEE8021X_EAPOL
  1662. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1663. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1664. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1665. #endif /* IEEE8021X_EAPOL */
  1666. }
  1667. /**
  1668. * wpa_config_set - Set a variable in network configuration
  1669. * @ssid: Pointer to network configuration data
  1670. * @var: Variable name, e.g., "ssid"
  1671. * @value: Variable value
  1672. * @line: Line number in configuration file or 0 if not used
  1673. * Returns: 0 on success, -1 on failure
  1674. *
  1675. * This function can be used to set network configuration variables based on
  1676. * both the configuration file and management interface input. The value
  1677. * parameter must be in the same format as the text-based configuration file is
  1678. * using. For example, strings are using double quotation marks.
  1679. */
  1680. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1681. int line)
  1682. {
  1683. size_t i;
  1684. int ret = 0;
  1685. if (ssid == NULL || var == NULL || value == NULL)
  1686. return -1;
  1687. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1688. const struct parse_data *field = &ssid_fields[i];
  1689. if (os_strcmp(var, field->name) != 0)
  1690. continue;
  1691. if (field->parser(field, ssid, line, value)) {
  1692. if (line) {
  1693. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1694. "parse %s '%s'.", line, var, value);
  1695. }
  1696. ret = -1;
  1697. }
  1698. break;
  1699. }
  1700. if (i == NUM_SSID_FIELDS) {
  1701. if (line) {
  1702. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1703. "'%s'.", line, var);
  1704. }
  1705. ret = -1;
  1706. }
  1707. return ret;
  1708. }
  1709. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  1710. const char *value)
  1711. {
  1712. size_t len;
  1713. char *buf;
  1714. int ret;
  1715. len = os_strlen(value);
  1716. buf = os_malloc(len + 3);
  1717. if (buf == NULL)
  1718. return -1;
  1719. buf[0] = '"';
  1720. os_memcpy(buf + 1, value, len);
  1721. buf[len + 1] = '"';
  1722. buf[len + 2] = '\0';
  1723. ret = wpa_config_set(ssid, var, buf, 0);
  1724. os_free(buf);
  1725. return ret;
  1726. }
  1727. /**
  1728. * wpa_config_get_all - Get all options from network configuration
  1729. * @ssid: Pointer to network configuration data
  1730. * @get_keys: Determines if keys/passwords will be included in returned list
  1731. * (if they may be exported)
  1732. * Returns: %NULL terminated list of all set keys and their values in the form
  1733. * of [key1, val1, key2, val2, ... , NULL]
  1734. *
  1735. * This function can be used to get list of all configured network properties.
  1736. * The caller is responsible for freeing the returned list and all its
  1737. * elements.
  1738. */
  1739. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  1740. {
  1741. const struct parse_data *field;
  1742. char *key, *value;
  1743. size_t i;
  1744. char **props;
  1745. int fields_num;
  1746. get_keys = get_keys && ssid->export_keys;
  1747. props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
  1748. if (!props)
  1749. return NULL;
  1750. fields_num = 0;
  1751. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1752. field = &ssid_fields[i];
  1753. if (field->key_data && !get_keys)
  1754. continue;
  1755. value = field->writer(field, ssid);
  1756. if (value == NULL)
  1757. continue;
  1758. if (os_strlen(value) == 0) {
  1759. os_free(value);
  1760. continue;
  1761. }
  1762. key = os_strdup(field->name);
  1763. if (key == NULL) {
  1764. os_free(value);
  1765. goto err;
  1766. }
  1767. props[fields_num * 2] = key;
  1768. props[fields_num * 2 + 1] = value;
  1769. fields_num++;
  1770. }
  1771. return props;
  1772. err:
  1773. value = *props;
  1774. while (value)
  1775. os_free(value++);
  1776. os_free(props);
  1777. return NULL;
  1778. }
  1779. #ifndef NO_CONFIG_WRITE
  1780. /**
  1781. * wpa_config_get - Get a variable in network configuration
  1782. * @ssid: Pointer to network configuration data
  1783. * @var: Variable name, e.g., "ssid"
  1784. * Returns: Value of the variable or %NULL on failure
  1785. *
  1786. * This function can be used to get network configuration variables. The
  1787. * returned value is a copy of the configuration variable in text format, i.e,.
  1788. * the same format that the text-based configuration file and wpa_config_set()
  1789. * are using for the value. The caller is responsible for freeing the returned
  1790. * value.
  1791. */
  1792. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1793. {
  1794. size_t i;
  1795. if (ssid == NULL || var == NULL)
  1796. return NULL;
  1797. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1798. const struct parse_data *field = &ssid_fields[i];
  1799. if (os_strcmp(var, field->name) == 0)
  1800. return field->writer(field, ssid);
  1801. }
  1802. return NULL;
  1803. }
  1804. /**
  1805. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1806. * @ssid: Pointer to network configuration data
  1807. * @var: Variable name, e.g., "ssid"
  1808. * Returns: Value of the variable or %NULL on failure
  1809. *
  1810. * This function can be used to get network configuration variable like
  1811. * wpa_config_get(). The only difference is that this functions does not expose
  1812. * key/password material from the configuration. In case a key/password field
  1813. * is requested, the returned value is an empty string or %NULL if the variable
  1814. * is not set or "*" if the variable is set (regardless of its value). The
  1815. * returned value is a copy of the configuration variable in text format, i.e,.
  1816. * the same format that the text-based configuration file and wpa_config_set()
  1817. * are using for the value. The caller is responsible for freeing the returned
  1818. * value.
  1819. */
  1820. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1821. {
  1822. size_t i;
  1823. if (ssid == NULL || var == NULL)
  1824. return NULL;
  1825. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1826. const struct parse_data *field = &ssid_fields[i];
  1827. if (os_strcmp(var, field->name) == 0) {
  1828. char *res = field->writer(field, ssid);
  1829. if (field->key_data) {
  1830. if (res && res[0]) {
  1831. wpa_printf(MSG_DEBUG, "Do not allow "
  1832. "key_data field to be "
  1833. "exposed");
  1834. os_free(res);
  1835. return os_strdup("*");
  1836. }
  1837. os_free(res);
  1838. return NULL;
  1839. }
  1840. return res;
  1841. }
  1842. }
  1843. return NULL;
  1844. }
  1845. #endif /* NO_CONFIG_WRITE */
  1846. /**
  1847. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  1848. * @ssid: Pointer to network configuration data
  1849. *
  1850. * This function must be called to update WPA PSK when either SSID or the
  1851. * passphrase has changed for the network configuration.
  1852. */
  1853. void wpa_config_update_psk(struct wpa_ssid *ssid)
  1854. {
  1855. #ifndef CONFIG_NO_PBKDF2
  1856. pbkdf2_sha1(ssid->passphrase,
  1857. (char *) ssid->ssid, ssid->ssid_len, 4096,
  1858. ssid->psk, PMK_LEN);
  1859. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1860. ssid->psk, PMK_LEN);
  1861. ssid->psk_set = 1;
  1862. #endif /* CONFIG_NO_PBKDF2 */
  1863. }
  1864. #ifndef CONFIG_NO_CONFIG_BLOBS
  1865. /**
  1866. * wpa_config_get_blob - Get a named configuration blob
  1867. * @config: Configuration data from wpa_config_read()
  1868. * @name: Name of the blob
  1869. * Returns: Pointer to blob data or %NULL if not found
  1870. */
  1871. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  1872. const char *name)
  1873. {
  1874. struct wpa_config_blob *blob = config->blobs;
  1875. while (blob) {
  1876. if (os_strcmp(blob->name, name) == 0)
  1877. return blob;
  1878. blob = blob->next;
  1879. }
  1880. return NULL;
  1881. }
  1882. /**
  1883. * wpa_config_set_blob - Set or add a named configuration blob
  1884. * @config: Configuration data from wpa_config_read()
  1885. * @blob: New value for the blob
  1886. *
  1887. * Adds a new configuration blob or replaces the current value of an existing
  1888. * blob.
  1889. */
  1890. void wpa_config_set_blob(struct wpa_config *config,
  1891. struct wpa_config_blob *blob)
  1892. {
  1893. wpa_config_remove_blob(config, blob->name);
  1894. blob->next = config->blobs;
  1895. config->blobs = blob;
  1896. }
  1897. /**
  1898. * wpa_config_free_blob - Free blob data
  1899. * @blob: Pointer to blob to be freed
  1900. */
  1901. void wpa_config_free_blob(struct wpa_config_blob *blob)
  1902. {
  1903. if (blob) {
  1904. os_free(blob->name);
  1905. os_free(blob->data);
  1906. os_free(blob);
  1907. }
  1908. }
  1909. /**
  1910. * wpa_config_remove_blob - Remove a named configuration blob
  1911. * @config: Configuration data from wpa_config_read()
  1912. * @name: Name of the blob to remove
  1913. * Returns: 0 if blob was removed or -1 if blob was not found
  1914. */
  1915. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  1916. {
  1917. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  1918. while (pos) {
  1919. if (os_strcmp(pos->name, name) == 0) {
  1920. if (prev)
  1921. prev->next = pos->next;
  1922. else
  1923. config->blobs = pos->next;
  1924. wpa_config_free_blob(pos);
  1925. return 0;
  1926. }
  1927. prev = pos;
  1928. pos = pos->next;
  1929. }
  1930. return -1;
  1931. }
  1932. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1933. /**
  1934. * wpa_config_alloc_empty - Allocate an empty configuration
  1935. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  1936. * socket
  1937. * @driver_param: Driver parameters
  1938. * Returns: Pointer to allocated configuration data or %NULL on failure
  1939. */
  1940. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  1941. const char *driver_param)
  1942. {
  1943. struct wpa_config *config;
  1944. config = os_zalloc(sizeof(*config));
  1945. if (config == NULL)
  1946. return NULL;
  1947. config->eapol_version = DEFAULT_EAPOL_VERSION;
  1948. config->ap_scan = DEFAULT_AP_SCAN;
  1949. config->fast_reauth = DEFAULT_FAST_REAUTH;
  1950. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  1951. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  1952. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  1953. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  1954. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  1955. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  1956. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  1957. if (ctrl_interface)
  1958. config->ctrl_interface = os_strdup(ctrl_interface);
  1959. if (driver_param)
  1960. config->driver_param = os_strdup(driver_param);
  1961. return config;
  1962. }
  1963. #ifndef CONFIG_NO_STDOUT_DEBUG
  1964. /**
  1965. * wpa_config_debug_dump_networks - Debug dump of configured networks
  1966. * @config: Configuration data from wpa_config_read()
  1967. */
  1968. void wpa_config_debug_dump_networks(struct wpa_config *config)
  1969. {
  1970. int prio;
  1971. struct wpa_ssid *ssid;
  1972. for (prio = 0; prio < config->num_prio; prio++) {
  1973. ssid = config->pssid[prio];
  1974. wpa_printf(MSG_DEBUG, "Priority group %d",
  1975. ssid->priority);
  1976. while (ssid) {
  1977. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  1978. ssid->id,
  1979. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1980. ssid = ssid->pnext;
  1981. }
  1982. }
  1983. }
  1984. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1985. struct global_parse_data {
  1986. char *name;
  1987. int (*parser)(const struct global_parse_data *data,
  1988. struct wpa_config *config, int line, const char *value);
  1989. void *param1, *param2, *param3;
  1990. unsigned int changed_flag;
  1991. };
  1992. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  1993. struct wpa_config *config, int line,
  1994. const char *pos)
  1995. {
  1996. int *dst;
  1997. dst = (int *) (((u8 *) config) + (long) data->param1);
  1998. *dst = atoi(pos);
  1999. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  2000. if (data->param2 && *dst < (long) data->param2) {
  2001. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  2002. "min_value=%ld)", line, data->name, *dst,
  2003. (long) data->param2);
  2004. *dst = (long) data->param2;
  2005. return -1;
  2006. }
  2007. if (data->param3 && *dst > (long) data->param3) {
  2008. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  2009. "max_value=%ld)", line, data->name, *dst,
  2010. (long) data->param3);
  2011. *dst = (long) data->param3;
  2012. return -1;
  2013. }
  2014. return 0;
  2015. }
  2016. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  2017. struct wpa_config *config, int line,
  2018. const char *pos)
  2019. {
  2020. size_t len;
  2021. char **dst, *tmp;
  2022. len = os_strlen(pos);
  2023. if (data->param2 && len < (size_t) data->param2) {
  2024. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  2025. "min_len=%ld)", line, data->name,
  2026. (unsigned long) len, (long) data->param2);
  2027. return -1;
  2028. }
  2029. if (data->param3 && len > (size_t) data->param3) {
  2030. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  2031. "max_len=%ld)", line, data->name,
  2032. (unsigned long) len, (long) data->param3);
  2033. return -1;
  2034. }
  2035. tmp = os_strdup(pos);
  2036. if (tmp == NULL)
  2037. return -1;
  2038. dst = (char **) (((u8 *) config) + (long) data->param1);
  2039. os_free(*dst);
  2040. *dst = tmp;
  2041. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  2042. return 0;
  2043. }
  2044. static int wpa_config_process_country(const struct global_parse_data *data,
  2045. struct wpa_config *config, int line,
  2046. const char *pos)
  2047. {
  2048. if (!pos[0] || !pos[1]) {
  2049. wpa_printf(MSG_DEBUG, "Invalid country set");
  2050. return -1;
  2051. }
  2052. config->country[0] = pos[0];
  2053. config->country[1] = pos[1];
  2054. wpa_printf(MSG_DEBUG, "country='%c%c'",
  2055. config->country[0], config->country[1]);
  2056. return 0;
  2057. }
  2058. static int wpa_config_process_load_dynamic_eap(
  2059. const struct global_parse_data *data, struct wpa_config *config,
  2060. int line, const char *so)
  2061. {
  2062. int ret;
  2063. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  2064. ret = eap_peer_method_load(so);
  2065. if (ret == -2) {
  2066. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  2067. "reloading.");
  2068. } else if (ret) {
  2069. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  2070. "method '%s'.", line, so);
  2071. return -1;
  2072. }
  2073. return 0;
  2074. }
  2075. #ifdef CONFIG_WPS
  2076. static int wpa_config_process_uuid(const struct global_parse_data *data,
  2077. struct wpa_config *config, int line,
  2078. const char *pos)
  2079. {
  2080. char buf[40];
  2081. if (uuid_str2bin(pos, config->uuid)) {
  2082. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  2083. return -1;
  2084. }
  2085. uuid_bin2str(config->uuid, buf, sizeof(buf));
  2086. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  2087. return 0;
  2088. }
  2089. static int wpa_config_process_device_type(
  2090. const struct global_parse_data *data,
  2091. struct wpa_config *config, int line, const char *pos)
  2092. {
  2093. return wps_dev_type_str2bin(pos, config->device_type);
  2094. }
  2095. static int wpa_config_process_os_version(const struct global_parse_data *data,
  2096. struct wpa_config *config, int line,
  2097. const char *pos)
  2098. {
  2099. if (hexstr2bin(pos, config->os_version, 4)) {
  2100. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  2101. return -1;
  2102. }
  2103. wpa_printf(MSG_DEBUG, "os_version=%08x",
  2104. WPA_GET_BE32(config->os_version));
  2105. return 0;
  2106. }
  2107. #endif /* CONFIG_WPS */
  2108. #ifdef CONFIG_P2P
  2109. static int wpa_config_process_sec_device_type(
  2110. const struct global_parse_data *data,
  2111. struct wpa_config *config, int line, const char *pos)
  2112. {
  2113. int idx;
  2114. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  2115. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  2116. "items", line);
  2117. return -1;
  2118. }
  2119. idx = config->num_sec_device_types;
  2120. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  2121. return -1;
  2122. config->num_sec_device_types++;
  2123. return 0;
  2124. }
  2125. #endif /* CONFIG_P2P */
  2126. static int wpa_config_process_hessid(
  2127. const struct global_parse_data *data,
  2128. struct wpa_config *config, int line, const char *pos)
  2129. {
  2130. if (hwaddr_aton2(pos, config->hessid) < 0) {
  2131. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  2132. line, pos);
  2133. return -1;
  2134. }
  2135. return 0;
  2136. }
  2137. #ifdef OFFSET
  2138. #undef OFFSET
  2139. #endif /* OFFSET */
  2140. /* OFFSET: Get offset of a variable within the wpa_config structure */
  2141. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  2142. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  2143. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  2144. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  2145. #define INT(f) _INT(f), NULL, NULL
  2146. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  2147. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  2148. #define STR(f) _STR(f), NULL, NULL
  2149. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  2150. static const struct global_parse_data global_fields[] = {
  2151. #ifdef CONFIG_CTRL_IFACE
  2152. { STR(ctrl_interface), 0 },
  2153. { STR(ctrl_interface_group), 0 } /* deprecated */,
  2154. #endif /* CONFIG_CTRL_IFACE */
  2155. { INT_RANGE(eapol_version, 1, 2), 0 },
  2156. { INT(ap_scan), 0 },
  2157. { INT(fast_reauth), 0 },
  2158. { STR(opensc_engine_path), 0 },
  2159. { STR(pkcs11_engine_path), 0 },
  2160. { STR(pkcs11_module_path), 0 },
  2161. { STR(driver_param), 0 },
  2162. { INT(dot11RSNAConfigPMKLifetime), 0 },
  2163. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  2164. { INT(dot11RSNAConfigSATimeout), 0 },
  2165. #ifndef CONFIG_NO_CONFIG_WRITE
  2166. { INT(update_config), 0 },
  2167. #endif /* CONFIG_NO_CONFIG_WRITE */
  2168. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  2169. #ifdef CONFIG_WPS
  2170. { FUNC(uuid), CFG_CHANGED_UUID },
  2171. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  2172. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  2173. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  2174. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2175. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2176. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  2177. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  2178. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  2179. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  2180. #endif /* CONFIG_WPS */
  2181. #ifdef CONFIG_P2P
  2182. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  2183. { INT(p2p_listen_reg_class), 0 },
  2184. { INT(p2p_listen_channel), 0 },
  2185. { INT(p2p_oper_reg_class), 0 },
  2186. { INT(p2p_oper_channel), 0 },
  2187. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  2188. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  2189. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  2190. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  2191. { INT(p2p_group_idle), 0 },
  2192. #endif /* CONFIG_P2P */
  2193. { FUNC(country), CFG_CHANGED_COUNTRY },
  2194. { INT(bss_max_count), 0 },
  2195. { INT(bss_expiration_age), 0 },
  2196. { INT(bss_expiration_scan_count), 0 },
  2197. { INT_RANGE(filter_ssids, 0, 1), 0 },
  2198. { INT(max_num_sta), 0 },
  2199. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  2200. { STR(home_realm), 0 },
  2201. { STR(home_username), 0 },
  2202. { STR(home_password), 0 },
  2203. { STR(home_ca_cert), 0 },
  2204. { STR(home_imsi), 0 },
  2205. { STR(home_milenage), 0 },
  2206. { INT_RANGE(interworking, 0, 1), 0 },
  2207. { FUNC(hessid), 0 },
  2208. { INT_RANGE(access_network_type, 0, 15), 0 }
  2209. };
  2210. #undef FUNC
  2211. #undef _INT
  2212. #undef INT
  2213. #undef INT_RANGE
  2214. #undef _STR
  2215. #undef STR
  2216. #undef STR_RANGE
  2217. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  2218. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  2219. {
  2220. size_t i;
  2221. int ret = 0;
  2222. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  2223. const struct global_parse_data *field = &global_fields[i];
  2224. size_t flen = os_strlen(field->name);
  2225. if (os_strncmp(pos, field->name, flen) != 0 ||
  2226. pos[flen] != '=')
  2227. continue;
  2228. if (field->parser(field, config, line, pos + flen + 1)) {
  2229. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2230. "parse '%s'.", line, pos);
  2231. ret = -1;
  2232. }
  2233. config->changed_parameters |= field->changed_flag;
  2234. break;
  2235. }
  2236. if (i == NUM_GLOBAL_FIELDS) {
  2237. if (line < 0)
  2238. return -1;
  2239. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  2240. line, pos);
  2241. ret = -1;
  2242. }
  2243. return ret;
  2244. }