config.c 74 KB

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