config.c 99 KB

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