config.c 66 KB

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