config.c 106 KB

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