config.c 93 KB

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