config.c 72 KB

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