config.c 81 KB

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