config.c 102 KB

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