config.c 67 KB

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