config.c 109 KB

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