config.c 76 KB

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