config.c 73 KB

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