config.c 109 KB

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