From: Y7n05h Date: Wed, 20 Apr 2022 18:44:58 +0000 (+0800) Subject: add blockRange and unblockRange X-Git-Tag: auth-4.8.0-alpha0~66^2~26 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=eb8e20c3cb094addcc75bd08328fcb12ff7e3011;p=thirdparty%2Fpdns.git add blockRange and unblockRange --- diff --git a/pdns/bpf-filter.cc b/pdns/bpf-filter.cc index c24155c8ed..cc59b8eac3 100644 --- a/pdns/bpf-filter.cc +++ b/pdns/bpf-filter.cc @@ -359,6 +359,8 @@ BPFFilter::BPFFilter(std::unordered_map& configs, maps->d_v4 = BPFFilter::Map(configs["ipv4"], d_mapFormat); maps->d_v6 = BPFFilter::Map(configs["ipv6"], d_mapFormat); maps->d_qnames = BPFFilter::Map(configs["qnames"], d_mapFormat); + maps->d_cidr4 = BPFFilter::Map(configs["cidr4"], d_mapFormat); + maps->d_cidr6 = BPFFilter::Map(configs["cidr6"], d_mapFormat); if (!external) { BPFFilter::MapConfiguration filters; filters.d_maxItems = 1; diff --git a/pdns/dnsdist-lua-bindings.cc b/pdns/dnsdist-lua-bindings.cc index 2ecae9d9ab..f09b7a22ac 100644 --- a/pdns/dnsdist-lua-bindings.cc +++ b/pdns/dnsdist-lua-bindings.cc @@ -457,6 +457,8 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) convertParamsToConfig("ipv4", BPFFilter::MapType::IPv4); convertParamsToConfig("ipv6", BPFFilter::MapType::IPv6); convertParamsToConfig("qnames", BPFFilter::MapType::QNames); + convertParamsToConfig("cidr4", BPFFilter::MapType::CIDR4); + convertParamsToConfig("cidr6", BPFFilter::MapType::CIDR6); BPFFilter::MapFormat format = BPFFilter::MapFormat::Legacy; bool external = false; @@ -498,7 +500,30 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) } } }); + luaCtx.registerFunction::*)(const string& range, boost::optional action)>("blockRange", [](std::shared_ptr bpf, const string& range, boost::optional action) { + if (!bpf) { + return; + } + if (!action) { + return bpf->block(Netmask(range), BPFFilter::MatchAction::Drop); + } + BPFFilter::MatchAction match; + switch (*action) { + case 0: + match = BPFFilter::MatchAction::Pass; + break; + case 1: + match = BPFFilter::MatchAction::Drop; + break; + case 2: + match = BPFFilter::MatchAction::Truncate; + break; + default: + throw std::runtime_error("Unsupported action for BPFFilter::block"); + } + return bpf->block(Netmask(range), match); + }); luaCtx.registerFunction::*)(const DNSName& qname, boost::optional qtype, boost::optional action)>("blockQName", [](std::shared_ptr bpf, const DNSName& qname, boost::optional qtype, boost::optional action) { if (bpf) { if (!action) { @@ -530,7 +555,12 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) return bpf->unblock(ca); } }); - + luaCtx.registerFunction::*)(const string& range)>("unblockRange", [](std::shared_ptr bpf, const string& range) { + if (!bpf) { + return; + } + bpf->unblock(Netmask(range)); + }); luaCtx.registerFunction::*)(const DNSName& qname, boost::optional qtype)>("unblockQName", [](std::shared_ptr bpf, const DNSName& qname, boost::optional qtype) { if (bpf) { return bpf->unblock(qname, qtype ? *qtype : 255);