2020-05-08 15:39:37 +00:00
|
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
|
|
import (
|
2021-12-03 14:56:07 +00:00
|
|
|
|
"encoding/binary"
|
2020-08-18 09:36:52 +00:00
|
|
|
|
"net"
|
2020-06-23 09:13:13 +00:00
|
|
|
|
"strings"
|
2020-05-08 15:39:37 +00:00
|
|
|
|
"time"
|
|
|
|
|
|
2020-10-30 10:32:02 +00:00
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
2021-05-21 13:15:47 +00:00
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-08 15:39:37 +00:00
|
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 13:03:37 +00:00
|
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-07-29 14:40:31 +00:00
|
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2020-05-08 15:39:37 +00:00
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// To transfer information between modules
|
|
|
|
|
type dnsContext struct {
|
2021-01-27 15:32:13 +00:00
|
|
|
|
proxyCtx *proxy.DNSContext
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
// setts are the filtering settings for the client.
|
2021-12-13 15:06:01 +00:00
|
|
|
|
setts *filtering.Settings
|
|
|
|
|
|
|
|
|
|
result *filtering.Result
|
2021-01-27 15:32:13 +00:00
|
|
|
|
// origResp is the response received from upstream. It is set when the
|
|
|
|
|
// response is modified by filters.
|
|
|
|
|
origResp *dns.Msg
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
|
// unreversedReqIP stores an IP address obtained from PTR request if it
|
2021-11-15 14:42:10 +00:00
|
|
|
|
// parsed successfully and belongs to one of locally-served IP ranges as per
|
|
|
|
|
// RFC 6303.
|
2021-03-31 12:00:47 +00:00
|
|
|
|
unreversedReqIP net.IP
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
// err is the error returned from a processing function.
|
|
|
|
|
err error
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
2022-02-10 12:42:59 +00:00
|
|
|
|
// clientID is the ClientID from DoH, DoQ, or DoT, if provided.
|
2021-01-27 15:32:13 +00:00
|
|
|
|
clientID string
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
// origQuestion is the question received from the client. It is set
|
|
|
|
|
// when the request is modified by rewrites.
|
|
|
|
|
origQuestion dns.Question
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
|
|
|
|
// startTime is the time at which the processing of the request has started.
|
|
|
|
|
startTime time.Time
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
// protectionEnabled shows if the filtering is enabled, and if the
|
|
|
|
|
// server's DNS filter is ready.
|
|
|
|
|
protectionEnabled bool
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
// responseFromUpstream shows if the response is received from the
|
|
|
|
|
// upstream servers.
|
|
|
|
|
responseFromUpstream bool
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
|
|
|
|
// responseAD shows if the response had the AD bit set.
|
|
|
|
|
responseAD bool
|
|
|
|
|
|
2021-04-08 15:07:29 +00:00
|
|
|
|
// isLocalClient shows if client's IP address is from locally-served
|
|
|
|
|
// network.
|
|
|
|
|
isLocalClient bool
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
// resultCode is the result of a request processing function.
|
|
|
|
|
type resultCode int
|
|
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
|
const (
|
2021-01-27 15:32:13 +00:00
|
|
|
|
// resultCodeSuccess is returned when a handler performed successfully,
|
|
|
|
|
// and the next handler must be called.
|
|
|
|
|
resultCodeSuccess resultCode = iota
|
|
|
|
|
// resultCodeFinish is returned when a handler performed successfully,
|
|
|
|
|
// and the processing of the request must be stopped.
|
|
|
|
|
resultCodeFinish
|
|
|
|
|
// resultCodeError is returned when a handler failed, and the processing
|
|
|
|
|
// of the request must be stopped.
|
|
|
|
|
resultCodeError
|
2020-05-08 15:39:37 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-04-27 08:39:48 +00:00
|
|
|
|
// ddrHostFQDN is the FQDN used in Discovery of Designated Resolvers (DDR) requests.
|
|
|
|
|
// See https://www.ietf.org/archive/id/draft-ietf-add-ddr-06.html.
|
|
|
|
|
const ddrHostFQDN = "_dns.resolver.arpa."
|
|
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
|
// handleDNSRequest filters the incoming DNS requests and writes them to the query log
|
2020-05-13 17:31:43 +00:00
|
|
|
|
func (s *Server) handleDNSRequest(_ *proxy.Proxy, d *proxy.DNSContext) error {
|
2021-01-27 15:32:13 +00:00
|
|
|
|
ctx := &dnsContext{
|
|
|
|
|
proxyCtx: d,
|
2021-05-21 13:15:47 +00:00
|
|
|
|
result: &filtering.Result{},
|
2021-01-27 15:32:13 +00:00
|
|
|
|
startTime: time.Now(),
|
|
|
|
|
}
|
2020-05-08 15:39:37 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
type modProcessFunc func(ctx *dnsContext) (rc resultCode)
|
2020-12-28 16:26:37 +00:00
|
|
|
|
|
|
|
|
|
// Since (*dnsforward.Server).handleDNSRequest(...) is used as
|
|
|
|
|
// proxy.(Config).RequestHandler, there is no need for additional index
|
|
|
|
|
// out of range checking in any of the following functions, because the
|
|
|
|
|
// (*proxy.Proxy).handleDNSRequest method performs it before calling the
|
|
|
|
|
// appropriate handler.
|
2020-05-08 15:39:37 +00:00
|
|
|
|
mods := []modProcessFunc{
|
2021-05-27 16:19:19 +00:00
|
|
|
|
s.processRecursion,
|
2021-11-15 14:42:10 +00:00
|
|
|
|
s.processInitial,
|
2022-04-27 08:39:48 +00:00
|
|
|
|
s.processDDRQuery,
|
2021-04-08 15:07:29 +00:00
|
|
|
|
s.processDetermineLocal,
|
2021-03-25 13:00:27 +00:00
|
|
|
|
s.processInternalHosts,
|
2021-03-31 12:00:47 +00:00
|
|
|
|
s.processRestrictLocal,
|
|
|
|
|
s.processInternalIPAddrs,
|
2021-10-20 16:52:13 +00:00
|
|
|
|
s.processFilteringBeforeRequest,
|
2021-03-31 12:00:47 +00:00
|
|
|
|
s.processLocalPTR,
|
2021-05-27 16:19:19 +00:00
|
|
|
|
s.processUpstream,
|
2021-11-15 14:42:10 +00:00
|
|
|
|
s.processFilteringAfterResponse,
|
2020-09-02 11:13:45 +00:00
|
|
|
|
s.ipset.process,
|
2021-11-15 14:42:10 +00:00
|
|
|
|
s.processQueryLogsAndStats,
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
for _, process := range mods {
|
|
|
|
|
r := process(ctx)
|
|
|
|
|
switch r {
|
2021-01-27 15:32:13 +00:00
|
|
|
|
case resultCodeSuccess:
|
2020-05-08 15:39:37 +00:00
|
|
|
|
// continue: call the next filter
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
case resultCodeFinish:
|
2020-05-08 15:39:37 +00:00
|
|
|
|
return nil
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
case resultCodeError:
|
2020-05-08 15:39:37 +00:00
|
|
|
|
return ctx.err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if d.Res != nil {
|
|
|
|
|
d.Res.Compress = true // some devices require DNS message compression
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 16:19:19 +00:00
|
|
|
|
// processRecursion checks the incoming request and halts it's handling if s
|
|
|
|
|
// have tried to resolve it recently.
|
|
|
|
|
func (s *Server) processRecursion(dctx *dnsContext) (rc resultCode) {
|
|
|
|
|
pctx := dctx.proxyCtx
|
|
|
|
|
|
|
|
|
|
if msg := pctx.Req; msg != nil && s.recDetector.check(*msg) {
|
|
|
|
|
log.Debug("recursion detected resolving %q", msg.Question[0].Name)
|
|
|
|
|
pctx.Res = s.genNXDomain(pctx.Req)
|
|
|
|
|
|
|
|
|
|
return resultCodeFinish
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 14:56:07 +00:00
|
|
|
|
// processInitial terminates the following processing for some requests if
|
|
|
|
|
// needed and enriches the ctx with some client-specific information.
|
|
|
|
|
//
|
|
|
|
|
// TODO(e.burkov): Decompose into less general processors.
|
2021-11-15 14:42:10 +00:00
|
|
|
|
func (s *Server) processInitial(ctx *dnsContext) (rc resultCode) {
|
2020-05-08 15:39:37 +00:00
|
|
|
|
d := ctx.proxyCtx
|
|
|
|
|
if s.conf.AAAADisabled && d.Req.Question[0].Qtype == dns.TypeAAAA {
|
|
|
|
|
_ = proxy.CheckDisabledAAAARequest(d, true)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeFinish
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.conf.OnDNSRequest != nil {
|
|
|
|
|
s.conf.OnDNSRequest(d)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// disable Mozilla DoH
|
2020-10-02 09:51:55 +00:00
|
|
|
|
// https://support.mozilla.org/en-US/kb/canary-domain-use-application-dnsnet
|
2020-05-08 15:39:37 +00:00
|
|
|
|
if (d.Req.Question[0].Qtype == dns.TypeA || d.Req.Question[0].Qtype == dns.TypeAAAA) &&
|
|
|
|
|
d.Req.Question[0].Name == "use-application-dns.net." {
|
2020-10-02 09:51:55 +00:00
|
|
|
|
d.Res = s.genNXDomain(d.Req)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeFinish
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 14:56:07 +00:00
|
|
|
|
// Get the client's ID if any. It should be performed before getting
|
|
|
|
|
// client-specific filtering settings.
|
|
|
|
|
var key [8]byte
|
|
|
|
|
binary.BigEndian.PutUint64(key[:], d.RequestID)
|
|
|
|
|
ctx.clientID = string(s.clientIDCache.Get(key[:]))
|
|
|
|
|
|
|
|
|
|
// Get the client-specific filtering settings.
|
2021-11-15 14:42:10 +00:00
|
|
|
|
ctx.protectionEnabled = s.conf.ProtectionEnabled
|
|
|
|
|
ctx.setts = s.getClientRequestFilteringSettings(ctx)
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
|
func (s *Server) setTableHostToIP(t hostToIPTable) {
|
|
|
|
|
s.tableHostToIPLock.Lock()
|
|
|
|
|
defer s.tableHostToIPLock.Unlock()
|
|
|
|
|
|
|
|
|
|
s.tableHostToIP = t
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 13:03:37 +00:00
|
|
|
|
func (s *Server) setTableIPToHost(t *netutil.IPMap) {
|
2021-04-15 14:52:53 +00:00
|
|
|
|
s.tableIPToHostLock.Lock()
|
|
|
|
|
defer s.tableIPToHostLock.Unlock()
|
|
|
|
|
|
|
|
|
|
s.tableIPToHost = t
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 09:13:13 +00:00
|
|
|
|
func (s *Server) onDHCPLeaseChanged(flags int) {
|
2021-04-19 13:04:40 +00:00
|
|
|
|
var err error
|
|
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
|
add := true
|
2020-06-23 09:13:13 +00:00
|
|
|
|
switch flags {
|
|
|
|
|
case dhcpd.LeaseChangedAdded,
|
|
|
|
|
dhcpd.LeaseChangedAddedStatic,
|
|
|
|
|
dhcpd.LeaseChangedRemovedStatic:
|
2021-04-15 14:52:53 +00:00
|
|
|
|
// Go on.
|
|
|
|
|
case dhcpd.LeaseChangedRemovedAll:
|
|
|
|
|
add = false
|
2020-06-23 09:13:13 +00:00
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
|
var hostToIP hostToIPTable
|
2021-08-09 13:03:37 +00:00
|
|
|
|
var ipToHost *netutil.IPMap
|
2021-04-15 14:52:53 +00:00
|
|
|
|
if add {
|
|
|
|
|
ll := s.dhcpServer.Leases(dhcpd.LeasesAll)
|
2020-08-18 09:36:52 +00:00
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
|
hostToIP = make(hostToIPTable, len(ll))
|
2021-08-09 13:03:37 +00:00
|
|
|
|
ipToHost = netutil.NewIPMap(len(ll))
|
2021-06-29 12:53:28 +00:00
|
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
|
for _, l := range ll {
|
2022-01-25 16:47:02 +00:00
|
|
|
|
// TODO(a.garipov): Remove this after we're finished with the client
|
|
|
|
|
// hostname validations in the DHCP server code.
|
2021-08-09 13:03:37 +00:00
|
|
|
|
err = netutil.ValidateDomainName(l.Hostname)
|
2021-04-19 13:04:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Debug(
|
|
|
|
|
"dns: skipping invalid hostname %q from dhcp: %s",
|
|
|
|
|
l.Hostname,
|
|
|
|
|
err,
|
|
|
|
|
)
|
2021-04-15 14:52:53 +00:00
|
|
|
|
}
|
2020-08-18 14:40:36 +00:00
|
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
|
lowhost := strings.ToLower(l.Hostname)
|
2020-08-18 09:36:52 +00:00
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
|
ipToHost.Set(l.IP, lowhost)
|
2020-08-18 09:36:52 +00:00
|
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
|
ip := make(net.IP, 4)
|
|
|
|
|
copy(ip, l.IP.To4())
|
|
|
|
|
hostToIP[lowhost] = ip
|
|
|
|
|
}
|
2020-08-18 09:36:52 +00:00
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
|
log.Debug("dns: added %d A/PTR entries from DHCP", ipToHost.Len())
|
2021-04-15 14:52:53 +00:00
|
|
|
|
}
|
2020-08-18 09:36:52 +00:00
|
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
|
s.setTableHostToIP(hostToIP)
|
|
|
|
|
s.setTableIPToHost(ipToHost)
|
2020-06-23 09:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-27 08:39:48 +00:00
|
|
|
|
// processDDRQuery responds to SVCB query for a special use domain name
|
|
|
|
|
// ‘_dns.resolver.arpa’. The result contains different types of encryption
|
|
|
|
|
// supported by current user configuration.
|
|
|
|
|
//
|
|
|
|
|
// See https://www.ietf.org/archive/id/draft-ietf-add-ddr-06.html.
|
|
|
|
|
func (s *Server) processDDRQuery(ctx *dnsContext) (rc resultCode) {
|
|
|
|
|
d := ctx.proxyCtx
|
|
|
|
|
question := d.Req.Question[0]
|
|
|
|
|
|
|
|
|
|
if !s.conf.HandleDDR {
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if question.Name == ddrHostFQDN {
|
|
|
|
|
// TODO(a.garipov): Check DoQ support in next RFC drafts.
|
|
|
|
|
if s.dnsProxy.TLSListenAddr == nil && s.dnsProxy.HTTPSListenAddr == nil ||
|
|
|
|
|
question.Qtype != dns.TypeSVCB {
|
|
|
|
|
d.Res = s.makeResponse(d.Req)
|
|
|
|
|
|
|
|
|
|
return resultCodeFinish
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.Res = s.makeDDRResponse(d.Req)
|
|
|
|
|
|
|
|
|
|
return resultCodeFinish
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// makeDDRResponse creates DDR answer according to server configuration.
|
|
|
|
|
func (s *Server) makeDDRResponse(req *dns.Msg) (resp *dns.Msg) {
|
|
|
|
|
resp = s.makeResponse(req)
|
|
|
|
|
domainName := s.conf.ServerName
|
|
|
|
|
|
|
|
|
|
for _, addr := range s.dnsProxy.HTTPSListenAddr {
|
|
|
|
|
values := []dns.SVCBKeyValue{
|
|
|
|
|
&dns.SVCBAlpn{Alpn: []string{"h2"}},
|
|
|
|
|
&dns.SVCBPort{Port: uint16(addr.Port)},
|
|
|
|
|
&dns.SVCBDoHPath{Template: "/dns-query?dns"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ans := &dns.SVCB{
|
|
|
|
|
Hdr: s.hdr(req, dns.TypeSVCB),
|
|
|
|
|
Priority: 1,
|
|
|
|
|
Target: domainName,
|
|
|
|
|
Value: values,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, addr := range s.dnsProxy.TLSListenAddr {
|
|
|
|
|
values := []dns.SVCBKeyValue{
|
|
|
|
|
&dns.SVCBAlpn{Alpn: []string{"dot"}},
|
|
|
|
|
&dns.SVCBPort{Port: uint16(addr.Port)},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ans := &dns.SVCB{
|
|
|
|
|
Hdr: s.hdr(req, dns.TypeSVCB),
|
|
|
|
|
Priority: 2,
|
|
|
|
|
Target: domainName,
|
|
|
|
|
Value: values,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 15:07:29 +00:00
|
|
|
|
// processDetermineLocal determines if the client's IP address is from
|
|
|
|
|
// locally-served network and saves the result into the context.
|
|
|
|
|
func (s *Server) processDetermineLocal(dctx *dnsContext) (rc resultCode) {
|
|
|
|
|
rc = resultCodeSuccess
|
|
|
|
|
|
|
|
|
|
var ip net.IP
|
2021-08-09 13:03:37 +00:00
|
|
|
|
if ip, _ = netutil.IPAndPortFromAddr(dctx.proxyCtx.Addr); ip == nil {
|
2021-04-08 15:07:29 +00:00
|
|
|
|
return rc
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 10:37:27 +00:00
|
|
|
|
dctx.isLocalClient = s.privateNets.Contains(ip)
|
2021-04-08 15:07:29 +00:00
|
|
|
|
|
|
|
|
|
return rc
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
|
// hostToIP tries to get an IP leased by DHCP and returns the copy of address
|
|
|
|
|
// since the data inside the internal table may be changed while request
|
|
|
|
|
// processing. It's safe for concurrent use.
|
|
|
|
|
func (s *Server) hostToIP(host string) (ip net.IP, ok bool) {
|
|
|
|
|
s.tableHostToIPLock.Lock()
|
|
|
|
|
defer s.tableHostToIPLock.Unlock()
|
|
|
|
|
|
|
|
|
|
if s.tableHostToIP == nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ipFromTable net.IP
|
|
|
|
|
ipFromTable, ok = s.tableHostToIP[host]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ip = make(net.IP, len(ipFromTable))
|
|
|
|
|
copy(ip, ipFromTable)
|
|
|
|
|
|
|
|
|
|
return ip, true
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 13:00:27 +00:00
|
|
|
|
// processInternalHosts respond to A requests if the target hostname is known to
|
|
|
|
|
// the server.
|
|
|
|
|
//
|
|
|
|
|
// TODO(a.garipov): Adapt to AAAA as well.
|
|
|
|
|
func (s *Server) processInternalHosts(dctx *dnsContext) (rc resultCode) {
|
2021-04-29 13:00:07 +00:00
|
|
|
|
if !s.dhcpServer.Enabled() {
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 13:00:27 +00:00
|
|
|
|
req := dctx.proxyCtx.Req
|
|
|
|
|
q := req.Question[0]
|
2021-03-25 14:21:00 +00:00
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
|
// Go on processing the AAAA request despite the fact that we don't support
|
|
|
|
|
// it yet. The expected behavior here is to respond with an empty answer
|
|
|
|
|
// and not NXDOMAIN.
|
2021-03-25 14:21:00 +00:00
|
|
|
|
if q.Qtype != dns.TypeA && q.Qtype != dns.TypeAAAA {
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-08-18 09:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 13:00:27 +00:00
|
|
|
|
reqHost := strings.ToLower(q.Name)
|
2022-01-25 16:47:02 +00:00
|
|
|
|
// TODO(a.garipov): Move everything related to DHCP local domain to the DHCP
|
|
|
|
|
// server.
|
2021-04-15 16:00:31 +00:00
|
|
|
|
host := strings.TrimSuffix(reqHost, s.localDomainSuffix)
|
2021-03-25 13:00:27 +00:00
|
|
|
|
if host == reqHost {
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-08-18 09:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 15:07:29 +00:00
|
|
|
|
d := dctx.proxyCtx
|
|
|
|
|
if !dctx.isLocalClient {
|
|
|
|
|
log.Debug("dns: %q requests for internal host", d.Addr)
|
|
|
|
|
d.Res = s.genNXDomain(req)
|
|
|
|
|
|
|
|
|
|
// Do not even put into query log.
|
|
|
|
|
return resultCodeFinish
|
|
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
|
|
|
|
ip, ok := s.hostToIP(host)
|
2020-08-18 09:36:52 +00:00
|
|
|
|
if !ok {
|
2021-12-06 14:26:43 +00:00
|
|
|
|
// TODO(e.burkov): Inspect special cases when user want to apply some
|
|
|
|
|
// rules handled by other processors to the hosts with TLD.
|
2021-04-08 15:07:29 +00:00
|
|
|
|
d.Res = s.genNXDomain(req)
|
|
|
|
|
|
|
|
|
|
return resultCodeFinish
|
2020-08-18 09:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 13:00:27 +00:00
|
|
|
|
log.Debug("dns: internal record: %s -> %s", q.Name, ip)
|
2020-08-18 09:36:52 +00:00
|
|
|
|
|
|
|
|
|
resp := s.makeResponse(req)
|
2021-03-25 13:00:27 +00:00
|
|
|
|
if q.Qtype == dns.TypeA {
|
|
|
|
|
a := &dns.A{
|
|
|
|
|
Hdr: s.hdr(req, dns.TypeA),
|
2021-03-31 12:00:47 +00:00
|
|
|
|
A: ip,
|
2020-08-18 09:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
resp.Answer = append(resp.Answer, a)
|
|
|
|
|
}
|
2021-03-25 13:00:27 +00:00
|
|
|
|
dctx.proxyCtx.Res = resp
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 15:07:29 +00:00
|
|
|
|
// processRestrictLocal responds with NXDOMAIN to PTR requests for IP addresses
|
|
|
|
|
// in locally-served network from external clients.
|
2021-03-31 12:00:47 +00:00
|
|
|
|
func (s *Server) processRestrictLocal(ctx *dnsContext) (rc resultCode) {
|
|
|
|
|
d := ctx.proxyCtx
|
|
|
|
|
req := d.Req
|
|
|
|
|
q := req.Question[0]
|
|
|
|
|
if q.Qtype != dns.TypePTR {
|
|
|
|
|
// No need for restriction.
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-06-23 09:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 13:03:37 +00:00
|
|
|
|
ip, err := netutil.IPFromReversedAddr(q.Name)
|
|
|
|
|
if err != nil {
|
2022-01-18 11:20:25 +00:00
|
|
|
|
log.Debug("dns: parsing reversed addr: %s", err)
|
2021-08-09 13:03:37 +00:00
|
|
|
|
|
2022-01-18 11:20:25 +00:00
|
|
|
|
// DNS-Based Service Discovery uses PTR records having not an ARPA
|
|
|
|
|
// format of the domain name in question. Those shouldn't be
|
|
|
|
|
// invalidated. See http://www.dns-sd.org/ServerStaticSetup.html and
|
|
|
|
|
// RFC 2782.
|
|
|
|
|
name := strings.TrimSuffix(q.Name, ".")
|
|
|
|
|
if err = netutil.ValidateSRVDomainName(name); err != nil {
|
|
|
|
|
log.Debug("dns: validating service domain: %s", err)
|
|
|
|
|
|
|
|
|
|
return resultCodeError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Debug("dns: request is for a service domain")
|
|
|
|
|
|
|
|
|
|
return resultCodeSuccess
|
2020-06-23 09:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
|
// Restrict an access to local addresses for external clients. We also
|
2021-11-15 14:42:10 +00:00
|
|
|
|
// assume that all the DHCP leases we give are locally-served or at least
|
|
|
|
|
// don't need to be inaccessible externally.
|
2022-03-18 10:37:27 +00:00
|
|
|
|
if !s.privateNets.Contains(ip) {
|
2021-11-15 14:42:10 +00:00
|
|
|
|
log.Debug("dns: addr %s is not from locally-served network", ip)
|
|
|
|
|
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !ctx.isLocalClient {
|
|
|
|
|
log.Debug("dns: %q requests an internal ip", d.Addr)
|
2021-11-11 13:19:33 +00:00
|
|
|
|
d.Res = s.genNXDomain(req)
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
2021-11-11 13:19:33 +00:00
|
|
|
|
// Do not even put into query log.
|
|
|
|
|
return resultCodeFinish
|
2021-03-31 12:00:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do not perform unreversing ever again.
|
|
|
|
|
ctx.unreversedReqIP = ip
|
|
|
|
|
|
2021-11-15 14:42:10 +00:00
|
|
|
|
// There is no need to filter request from external addresses since this
|
|
|
|
|
// code is only executed when the request is for locally-served ARPA
|
|
|
|
|
// hostname so disable redundant filters.
|
|
|
|
|
ctx.setts.ParentalEnabled = false
|
|
|
|
|
ctx.setts.SafeBrowsingEnabled = false
|
|
|
|
|
ctx.setts.SafeSearchEnabled = false
|
|
|
|
|
ctx.setts.ServicesRules = nil
|
2021-04-07 17:16:06 +00:00
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
|
// Nothing to restrict.
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ipToHost tries to get a hostname leased by DHCP. It's safe for concurrent
|
|
|
|
|
// use.
|
|
|
|
|
func (s *Server) ipToHost(ip net.IP) (host string, ok bool) {
|
2021-04-15 14:52:53 +00:00
|
|
|
|
s.tableIPToHostLock.Lock()
|
|
|
|
|
defer s.tableIPToHostLock.Unlock()
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
|
if s.tableIPToHost == nil {
|
2021-03-31 12:00:47 +00:00
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
|
var v interface{}
|
|
|
|
|
v, ok = s.tableIPToHost.Get(ip)
|
2021-07-06 12:08:55 +00:00
|
|
|
|
if !ok {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
2021-06-29 12:53:28 +00:00
|
|
|
|
|
2021-07-06 12:08:55 +00:00
|
|
|
|
if host, ok = v.(string); !ok {
|
2021-06-29 12:53:28 +00:00
|
|
|
|
log.Error("dns: bad type %T in tableIPToHost for %s", v, ip)
|
|
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
2021-07-06 12:08:55 +00:00
|
|
|
|
return host, true
|
2021-03-31 12:00:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Respond to PTR requests if the target IP is leased by our DHCP server and the
|
|
|
|
|
// requestor is inside the local network.
|
|
|
|
|
func (s *Server) processInternalIPAddrs(ctx *dnsContext) (rc resultCode) {
|
|
|
|
|
d := ctx.proxyCtx
|
|
|
|
|
if d.Res != nil {
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-06-23 09:13:13 +00:00
|
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
|
|
|
|
ip := ctx.unreversedReqIP
|
|
|
|
|
if ip == nil {
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host, ok := s.ipToHost(ip)
|
2020-06-23 09:13:13 +00:00
|
|
|
|
if !ok {
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-06-23 09:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
|
log.Debug("dns: reverse-lookup: %s -> %s", ip, host)
|
2020-06-23 09:13:13 +00:00
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
|
req := d.Req
|
2020-06-23 09:13:13 +00:00
|
|
|
|
resp := s.makeResponse(req)
|
2021-03-31 12:00:47 +00:00
|
|
|
|
ptr := &dns.PTR{
|
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
|
Name: req.Question[0].Name,
|
|
|
|
|
Rrtype: dns.TypePTR,
|
|
|
|
|
Ttl: s.conf.BlockedResponseTTL,
|
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
|
},
|
|
|
|
|
Ptr: dns.Fqdn(host),
|
2020-06-23 09:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
resp.Answer = append(resp.Answer, ptr)
|
2021-03-31 12:00:47 +00:00
|
|
|
|
d.Res = resp
|
|
|
|
|
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// processLocalPTR responds to PTR requests if the target IP is detected to be
|
|
|
|
|
// inside the local network and the query was not answered from DHCP.
|
|
|
|
|
func (s *Server) processLocalPTR(ctx *dnsContext) (rc resultCode) {
|
|
|
|
|
d := ctx.proxyCtx
|
|
|
|
|
if d.Res != nil {
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ip := ctx.unreversedReqIP
|
|
|
|
|
if ip == nil {
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
|
s.serverLock.RLock()
|
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
|
2022-03-18 10:37:27 +00:00
|
|
|
|
if !s.privateNets.Contains(ip) {
|
2021-03-31 12:00:47 +00:00
|
|
|
|
return resultCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
|
if s.conf.UsePrivateRDNS {
|
2021-05-27 16:19:19 +00:00
|
|
|
|
s.recDetector.add(*d.Req)
|
2021-05-26 14:55:19 +00:00
|
|
|
|
if err := s.localResolvers.Resolve(d); err != nil {
|
|
|
|
|
ctx.err = err
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
|
return resultCodeError
|
|
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 18:01:21 +00:00
|
|
|
|
if d.Res == nil {
|
|
|
|
|
d.Res = s.genNXDomain(d.Req)
|
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
|
// Do not even put into query log.
|
2021-04-09 18:01:21 +00:00
|
|
|
|
return resultCodeFinish
|
|
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-06-23 09:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
|
// Apply filtering logic
|
2021-10-20 16:52:13 +00:00
|
|
|
|
func (s *Server) processFilteringBeforeRequest(ctx *dnsContext) (rc resultCode) {
|
|
|
|
|
if ctx.proxyCtx.Res != nil {
|
|
|
|
|
// Go on since the response is already set.
|
|
|
|
|
return resultCodeSuccess
|
2020-06-23 09:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
|
s.serverLock.RLock()
|
|
|
|
|
defer s.serverLock.RUnlock()
|
2020-05-08 15:39:37 +00:00
|
|
|
|
|
2021-10-20 16:52:13 +00:00
|
|
|
|
if s.dnsFilter == nil {
|
2021-05-26 14:55:19 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
|
var err error
|
2021-10-20 16:52:13 +00:00
|
|
|
|
if ctx.result, err = s.filterDNSRequest(ctx); err != nil {
|
2020-05-08 15:39:37 +00:00
|
|
|
|
ctx.err = err
|
2021-04-07 17:16:06 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeError
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
2021-04-07 17:16:06 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 10:02:59 +00:00
|
|
|
|
// ipStringFromAddr extracts an IP address string from net.Addr.
|
|
|
|
|
func ipStringFromAddr(addr net.Addr) (ipStr string) {
|
2021-08-09 13:03:37 +00:00
|
|
|
|
if ip, _ := netutil.IPAndPortFromAddr(addr); ip != nil {
|
2021-05-28 10:02:59 +00:00
|
|
|
|
return ip.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 13:56:05 +00:00
|
|
|
|
// processUpstream passes request to upstream servers and handles the response.
|
2021-12-13 15:06:01 +00:00
|
|
|
|
func (s *Server) processUpstream(dctx *dnsContext) (rc resultCode) {
|
|
|
|
|
pctx := dctx.proxyCtx
|
|
|
|
|
if pctx.Res != nil {
|
|
|
|
|
// The response has already been set.
|
|
|
|
|
return resultCodeSuccess
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
|
if pctx.Addr != nil && s.conf.GetCustomUpstreamByClient != nil {
|
2022-02-10 12:42:59 +00:00
|
|
|
|
// Use the ClientID first, since it has a higher priority.
|
2021-12-13 15:06:01 +00:00
|
|
|
|
id := stringutil.Coalesce(dctx.clientID, ipStringFromAddr(pctx.Addr))
|
2021-05-28 10:02:59 +00:00
|
|
|
|
upsConf, err := s.conf.GetCustomUpstreamByClient(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("dns: getting custom upstreams for client %s: %s", id, err)
|
|
|
|
|
} else if upsConf != nil {
|
|
|
|
|
log.Debug("dns: using custom upstreams for client %s", id)
|
2021-12-13 15:06:01 +00:00
|
|
|
|
pctx.CustomUpstreamConfig = upsConf
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
|
req := pctx.Req
|
|
|
|
|
origReqAD := false
|
2020-05-08 15:39:37 +00:00
|
|
|
|
if s.conf.EnableDNSSEC {
|
2021-12-13 15:06:01 +00:00
|
|
|
|
if req.AuthenticatedData {
|
|
|
|
|
origReqAD = true
|
2020-05-08 15:39:37 +00:00
|
|
|
|
} else {
|
2021-12-13 15:06:01 +00:00
|
|
|
|
req.AuthenticatedData = true
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 10:14:41 +00:00
|
|
|
|
// Process the request further since it wasn't filtered.
|
|
|
|
|
prx := s.proxy()
|
|
|
|
|
if prx == nil {
|
2021-12-13 15:06:01 +00:00
|
|
|
|
dctx.err = srvClosedErr
|
2021-10-06 10:14:41 +00:00
|
|
|
|
|
|
|
|
|
return resultCodeError
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
|
if dctx.err = prx.Resolve(pctx); dctx.err != nil {
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeError
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
|
dctx.responseFromUpstream = true
|
|
|
|
|
dctx.responseAD = pctx.Res.AuthenticatedData
|
2020-05-08 15:39:37 +00:00
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
|
if s.conf.EnableDNSSEC && !origReqAD {
|
|
|
|
|
pctx.Req.AuthenticatedData = false
|
|
|
|
|
pctx.Res.AuthenticatedData = false
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply filtering logic after we have received response from upstream servers
|
2021-11-15 14:42:10 +00:00
|
|
|
|
func (s *Server) processFilteringAfterResponse(ctx *dnsContext) (rc resultCode) {
|
2020-05-08 15:39:37 +00:00
|
|
|
|
d := ctx.proxyCtx
|
|
|
|
|
|
2021-10-20 16:52:13 +00:00
|
|
|
|
switch res := ctx.result; res.Reason {
|
|
|
|
|
case filtering.NotFilteredAllowList:
|
|
|
|
|
// Go on.
|
|
|
|
|
case
|
|
|
|
|
filtering.Rewritten,
|
2021-05-21 13:15:47 +00:00
|
|
|
|
filtering.RewrittenRule:
|
2020-12-21 14:48:07 +00:00
|
|
|
|
|
2020-06-01 12:23:08 +00:00
|
|
|
|
if len(ctx.origQuestion.Name) == 0 {
|
2021-10-20 16:52:13 +00:00
|
|
|
|
// origQuestion is set in case we get only CNAME without IP from
|
|
|
|
|
// rewrites table.
|
2020-05-08 15:39:37 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2020-06-01 12:23:08 +00:00
|
|
|
|
|
2021-10-20 16:52:13 +00:00
|
|
|
|
d.Req.Question[0], d.Res.Question[0] = ctx.origQuestion, ctx.origQuestion
|
|
|
|
|
if len(d.Res.Answer) > 0 {
|
|
|
|
|
answer := append([]dns.RR{s.genAnswerCNAME(d.Req, res.CanonName)}, d.Res.Answer...)
|
2020-05-08 15:39:37 +00:00
|
|
|
|
d.Res.Answer = answer
|
|
|
|
|
}
|
|
|
|
|
default:
|
2022-02-03 18:19:32 +00:00
|
|
|
|
// Check the response only if it's from an upstream. Don't check the
|
|
|
|
|
// response if the protection is disabled since dnsrewrite rules aren't
|
|
|
|
|
// applied to it anyway.
|
2021-10-20 16:52:13 +00:00
|
|
|
|
if !ctx.protectionEnabled || !ctx.responseFromUpstream || s.dnsFilter == nil {
|
2020-05-08 15:39:37 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2021-10-20 16:52:13 +00:00
|
|
|
|
|
|
|
|
|
origResp := d.Res
|
|
|
|
|
result, err := s.filterDNSResponse(ctx)
|
2020-05-08 15:39:37 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.err = err
|
2021-10-20 16:52:13 +00:00
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeError
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
2021-10-20 16:52:13 +00:00
|
|
|
|
|
|
|
|
|
if result != nil {
|
|
|
|
|
ctx.result = result
|
|
|
|
|
ctx.origResp = origResp
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 16:52:13 +00:00
|
|
|
|
if ctx.result == nil {
|
|
|
|
|
ctx.result = &filtering.Result{}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
|
return resultCodeSuccess
|
2020-05-08 15:39:37 +00:00
|
|
|
|
}
|