config.c 114 KB

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