config.c 104 KB

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