2020-05-08 18:39:37 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2024-04-11 14:03:37 +03:00
|
|
|
"cmp"
|
2021-12-03 17:56:07 +03:00
|
|
|
"encoding/binary"
|
2020-08-18 12:36:52 +03:00
|
|
|
"net"
|
2022-10-26 21:12:51 +03:00
|
|
|
"net/netip"
|
2020-06-23 12:13:13 +03:00
|
|
|
"strings"
|
2020-05-08 18:39:37 +03:00
|
|
|
"time"
|
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-08 18:39:37 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 16:03:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2020-05-08 18:39:37 +03:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// To transfer information between modules
|
2023-02-07 17:50:39 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Add lowercased, non-FQDN version of the hostname from the
|
2023-04-07 13:17:40 +03:00
|
|
|
// question of the request. Add persistent client.
|
2020-05-08 18:39:37 +03:00
|
|
|
type dnsContext struct {
|
2021-01-27 18:32:13 +03:00
|
|
|
proxyCtx *proxy.DNSContext
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// setts are the filtering settings for the client.
|
2021-12-13 18:06:01 +03:00
|
|
|
setts *filtering.Settings
|
|
|
|
|
|
|
|
result *filtering.Result
|
2023-07-18 17:02:07 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// origResp is the response received from upstream. It is set when the
|
|
|
|
// response is modified by filters.
|
|
|
|
origResp *dns.Msg
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// err is the error returned from a processing function.
|
|
|
|
err error
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2022-02-10 15:42:59 +03:00
|
|
|
// clientID is the ClientID from DoH, DoQ, or DoT, if provided.
|
2021-01-27 18:32:13 +03:00
|
|
|
clientID string
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
// startTime is the time at which the processing of the request has started.
|
|
|
|
startTime time.Time
|
|
|
|
|
2021-01-27 18:32:13 +03: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 18:06:01 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// protectionEnabled shows if the filtering is enabled, and if the
|
|
|
|
// server's DNS filter is ready.
|
|
|
|
protectionEnabled bool
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// responseFromUpstream shows if the response is received from the
|
|
|
|
// upstream servers.
|
|
|
|
responseFromUpstream bool
|
2021-12-13 18:06:01 +03:00
|
|
|
|
|
|
|
// responseAD shows if the response had the AD bit set.
|
|
|
|
responseAD bool
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// isDHCPHost is true if the request for a local domain name and the DHCP is
|
|
|
|
// available for this request.
|
|
|
|
isDHCPHost bool
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// resultCode is the result of a request processing function.
|
|
|
|
type resultCode int
|
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
const (
|
2023-08-30 14:02:12 +03:00
|
|
|
// resultCodeSuccess is returned when a handler performed successfully, and
|
|
|
|
// the next handler must be called.
|
2021-01-27 18:32:13 +03:00
|
|
|
resultCodeSuccess resultCode = iota
|
2023-08-30 14:02:12 +03:00
|
|
|
|
|
|
|
// resultCodeFinish is returned when a handler performed successfully, and
|
|
|
|
// the processing of the request must be stopped.
|
2021-01-27 18:32:13 +03:00
|
|
|
resultCodeFinish
|
2023-08-30 14:02:12 +03:00
|
|
|
|
|
|
|
// resultCodeError is returned when a handler failed, and the processing of
|
|
|
|
// the request must be stopped.
|
2021-01-27 18:32:13 +03:00
|
|
|
resultCodeError
|
2020-05-08 18:39:37 +03:00
|
|
|
)
|
|
|
|
|
2022-04-27 11:39:48 +03: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 18:39:37 +03:00
|
|
|
// handleDNSRequest filters the incoming DNS requests and writes them to the query log
|
2022-09-02 14:52:19 +03:00
|
|
|
func (s *Server) handleDNSRequest(_ *proxy.Proxy, pctx *proxy.DNSContext) error {
|
|
|
|
dctx := &dnsContext{
|
|
|
|
proxyCtx: pctx,
|
2021-05-21 16:15:47 +03:00
|
|
|
result: &filtering.Result{},
|
2021-01-27 18:32:13 +03:00
|
|
|
startTime: time.Now(),
|
|
|
|
}
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
type modProcessFunc func(ctx *dnsContext) (rc resultCode)
|
2020-12-28 19:26:37 +03: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 18:39:37 +03:00
|
|
|
mods := []modProcessFunc{
|
2021-11-15 17:42:10 +03:00
|
|
|
s.processInitial,
|
2022-04-27 11:39:48 +03:00
|
|
|
s.processDDRQuery,
|
2022-06-28 19:09:26 +03:00
|
|
|
s.processDHCPHosts,
|
|
|
|
s.processDHCPAddrs,
|
2021-10-20 19:52:13 +03:00
|
|
|
s.processFilteringBeforeRequest,
|
2021-05-27 19:19:19 +03:00
|
|
|
s.processUpstream,
|
2021-11-15 17:42:10 +03:00
|
|
|
s.processFilteringAfterResponse,
|
2020-09-02 14:13:45 +03:00
|
|
|
s.ipset.process,
|
2021-11-15 17:42:10 +03:00
|
|
|
s.processQueryLogsAndStats,
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
for _, process := range mods {
|
2022-09-02 14:52:19 +03:00
|
|
|
r := process(dctx)
|
2020-05-08 18:39:37 +03:00
|
|
|
switch r {
|
2021-01-27 18:32:13 +03:00
|
|
|
case resultCodeSuccess:
|
2020-05-08 18:39:37 +03:00
|
|
|
// continue: call the next filter
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
case resultCodeFinish:
|
2020-05-08 18:39:37 +03:00
|
|
|
return nil
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
case resultCodeError:
|
2022-09-02 14:52:19 +03:00
|
|
|
return dctx.err
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
if pctx.Res != nil {
|
|
|
|
// Some devices require DNS message compression.
|
|
|
|
pctx.Res.Compress = true
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2022-09-02 14:52:19 +03:00
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
// mozillaFQDN is the domain used to signal the Firefox browser to not use its
|
|
|
|
// own DoH server.
|
|
|
|
//
|
|
|
|
// See https://support.mozilla.org/en-US/kb/canary-domain-use-application-dnsnet.
|
|
|
|
const mozillaFQDN = "use-application-dns.net."
|
|
|
|
|
|
|
|
// healthcheckFQDN is a reserved domain-name used for healthchecking.
|
|
|
|
//
|
|
|
|
// [Section 6.2 of RFC 6761] states that DNS Registries/Registrars must not
|
|
|
|
// grant requests to register test names in the normal way to any person or
|
|
|
|
// entity, making domain names under the .test TLD free to use in internal
|
|
|
|
// purposes.
|
|
|
|
//
|
|
|
|
// [Section 6.2 of RFC 6761]: https://www.rfc-editor.org/rfc/rfc6761.html#section-6.2
|
|
|
|
const healthcheckFQDN = "healthcheck.adguardhome.test."
|
|
|
|
|
2021-12-03 17:56:07 +03:00
|
|
|
// processInitial terminates the following processing for some requests if
|
2023-06-28 13:46:04 +03:00
|
|
|
// needed and enriches dctx with some client-specific information.
|
2021-12-03 17:56:07 +03:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Decompose into less general processors.
|
2022-09-02 14:52:19 +03:00
|
|
|
func (s *Server) processInitial(dctx *dnsContext) (rc resultCode) {
|
2023-06-28 13:46:04 +03:00
|
|
|
log.Debug("dnsforward: started processing initial")
|
|
|
|
defer log.Debug("dnsforward: finished processing initial")
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
pctx := dctx.proxyCtx
|
2023-12-05 17:43:50 +03:00
|
|
|
s.processClientIP(pctx.Addr.Addr())
|
2023-07-18 17:02:07 +03:00
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
q := pctx.Req.Question[0]
|
|
|
|
qt := q.Qtype
|
|
|
|
if s.conf.AAAADisabled && qt == dns.TypeAAAA {
|
|
|
|
_ = proxy.CheckDisabledAAAARequest(pctx, true)
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeFinish
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
if (qt == dns.TypeA || qt == dns.TypeAAAA) && q.Name == mozillaFQDN {
|
2024-04-11 14:03:37 +03:00
|
|
|
pctx.Res = s.NewMsgNXDOMAIN(pctx.Req)
|
2022-09-02 14:52:19 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeFinish
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
if q.Name == healthcheckFQDN {
|
2023-03-26 23:12:24 +03:00
|
|
|
// Generate a NODATA negative response to make nslookup exit with 0.
|
2024-04-11 14:03:37 +03:00
|
|
|
pctx.Res = s.replyCompressed(pctx.Req)
|
2023-03-26 23:12:24 +03:00
|
|
|
|
|
|
|
return resultCodeFinish
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
// Get the ClientID, if any, before getting client-specific filtering
|
|
|
|
// settings.
|
2021-12-03 17:56:07 +03:00
|
|
|
var key [8]byte
|
2022-09-02 14:52:19 +03:00
|
|
|
binary.BigEndian.PutUint64(key[:], pctx.RequestID)
|
|
|
|
dctx.clientID = string(s.clientIDCache.Get(key[:]))
|
2021-12-03 17:56:07 +03:00
|
|
|
|
|
|
|
// Get the client-specific filtering settings.
|
2023-04-11 15:02:29 +03:00
|
|
|
dctx.protectionEnabled, _ = s.UpdatedProtectionStatus()
|
2023-07-18 17:02:07 +03:00
|
|
|
dctx.setts = s.clientRequestFilteringSettings(dctx)
|
2021-11-15 17:42:10 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeSuccess
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
// processClientIP sends the client IP address to s.addrProc, if needed.
|
2023-12-05 17:43:50 +03:00
|
|
|
func (s *Server) processClientIP(addr netip.Addr) {
|
|
|
|
if !addr.IsValid() {
|
2023-07-18 17:02:07 +03:00
|
|
|
log.Info("dnsforward: warning: bad client addr %q", addr)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not assign s.addrProc to a local variable to then use, since this lock
|
|
|
|
// also serializes the closure of s.addrProc.
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2023-12-05 17:43:50 +03:00
|
|
|
s.addrProc.Process(addr)
|
2023-07-18 17:02:07 +03:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:56:59 +03:00
|
|
|
// processDDRQuery responds to Discovery of Designated Resolvers (DDR) SVCB
|
|
|
|
// queries. The response contains different types of encryption supported by
|
|
|
|
// current user configuration.
|
2022-04-27 11:39:48 +03:00
|
|
|
//
|
2022-09-02 16:56:59 +03:00
|
|
|
// See https://www.ietf.org/archive/id/draft-ietf-add-ddr-10.html.
|
2022-09-02 14:52:19 +03:00
|
|
|
func (s *Server) processDDRQuery(dctx *dnsContext) (rc resultCode) {
|
2023-06-28 13:46:04 +03:00
|
|
|
log.Debug("dnsforward: started processing ddr")
|
|
|
|
defer log.Debug("dnsforward: finished processing ddr")
|
|
|
|
|
2022-04-27 11:39:48 +03:00
|
|
|
if !s.conf.HandleDDR {
|
|
|
|
return resultCodeSuccess
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:37:41 +03:00
|
|
|
pctx := dctx.proxyCtx
|
|
|
|
q := pctx.Req.Question[0]
|
2022-09-02 14:52:19 +03:00
|
|
|
if q.Name == ddrHostFQDN {
|
|
|
|
pctx.Res = s.makeDDRResponse(pctx.Req)
|
2022-04-27 11:39:48 +03:00
|
|
|
|
|
|
|
return resultCodeFinish
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:56:59 +03:00
|
|
|
// makeDDRResponse creates a DDR answer based on the server configuration. The
|
|
|
|
// constructed SVCB resource records have the priority of 1 for each entry,
|
|
|
|
// similar to examples provided by the [draft standard].
|
2022-06-20 17:48:56 +03:00
|
|
|
//
|
|
|
|
// TODO(a.meshkov): Consider setting the priority values based on the protocol.
|
2022-09-02 16:56:59 +03:00
|
|
|
//
|
|
|
|
// [draft standard]: https://www.ietf.org/archive/id/draft-ietf-add-ddr-10.html.
|
2022-04-27 11:39:48 +03:00
|
|
|
func (s *Server) makeDDRResponse(req *dns.Msg) (resp *dns.Msg) {
|
2024-04-11 14:03:37 +03:00
|
|
|
resp = s.replyCompressed(req)
|
2022-11-02 14:37:41 +03:00
|
|
|
if req.Question[0].Qtype != dns.TypeSVCB {
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:28:50 +03:00
|
|
|
// TODO(e.burkov): Think about storing the FQDN version of the server's
|
2022-05-30 16:04:28 +03:00
|
|
|
// name somewhere.
|
|
|
|
domainName := dns.Fqdn(s.conf.ServerName)
|
2022-04-27 11:39:48 +03:00
|
|
|
|
2022-05-31 17:28:50 +03:00
|
|
|
for _, addr := range s.conf.HTTPSListenAddrs {
|
2022-04-27 11:39:48 +03:00
|
|
|
values := []dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBAlpn{Alpn: []string{"h2"}},
|
|
|
|
&dns.SVCBPort{Port: uint16(addr.Port)},
|
2022-09-20 15:26:10 +03:00
|
|
|
&dns.SVCBDoHPath{Template: "/dns-query{?dns}"},
|
2022-04-27 11:39:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ans := &dns.SVCB{
|
|
|
|
Hdr: s.hdr(req, dns.TypeSVCB),
|
|
|
|
Priority: 1,
|
|
|
|
Target: domainName,
|
|
|
|
Value: values,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:37:41 +03:00
|
|
|
if s.conf.hasIPAddrs {
|
|
|
|
// Only add DNS-over-TLS resolvers in case the certificate contains IP
|
|
|
|
// addresses.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/4927.
|
|
|
|
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: 1,
|
|
|
|
Target: domainName,
|
|
|
|
Value: values,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
2022-04-27 11:39:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:53:11 +03:00
|
|
|
for _, addr := range s.dnsProxy.QUICListenAddr {
|
|
|
|
values := []dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBAlpn{Alpn: []string{"doq"}},
|
|
|
|
&dns.SVCBPort{Port: uint16(addr.Port)},
|
|
|
|
}
|
|
|
|
|
|
|
|
ans := &dns.SVCB{
|
|
|
|
Hdr: s.hdr(req, dns.TypeSVCB),
|
2022-06-20 17:48:56 +03:00
|
|
|
Priority: 1,
|
2022-05-26 17:53:11 +03:00
|
|
|
Target: domainName,
|
|
|
|
Value: values,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
|
|
|
}
|
|
|
|
|
2022-04-27 11:39:48 +03:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
// processDHCPHosts respond to A requests if the target hostname is known to
|
2023-01-23 19:10:56 +03:00
|
|
|
// the server. It responds with a mapped IP address if the DNS64 is enabled and
|
|
|
|
// the request is for AAAA.
|
2021-03-25 16:00:27 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Adapt to AAAA as well.
|
2022-06-28 19:09:26 +03:00
|
|
|
func (s *Server) processDHCPHosts(dctx *dnsContext) (rc resultCode) {
|
2023-06-28 13:46:04 +03:00
|
|
|
log.Debug("dnsforward: started processing dhcp hosts")
|
|
|
|
defer log.Debug("dnsforward: finished processing dhcp hosts")
|
|
|
|
|
2022-09-02 16:56:59 +03:00
|
|
|
pctx := dctx.proxyCtx
|
|
|
|
req := pctx.Req
|
2023-08-30 14:02:12 +03:00
|
|
|
|
|
|
|
q := &req.Question[0]
|
|
|
|
dhcpHost := s.dhcpHostFromRequest(q)
|
|
|
|
if dctx.isDHCPHost = dhcpHost != ""; !dctx.isDHCPHost {
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeSuccess
|
2020-08-18 12:36:52 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
if !pctx.IsPrivateClient {
|
2023-08-30 14:02:12 +03:00
|
|
|
log.Debug("dnsforward: %q requests for dhcp host %q", pctx.Addr, dhcpHost)
|
2024-04-11 14:03:37 +03:00
|
|
|
pctx.Res = s.NewMsgNXDOMAIN(req)
|
2021-04-08 18:07:29 +03:00
|
|
|
|
|
|
|
// Do not even put into query log.
|
|
|
|
return resultCodeFinish
|
|
|
|
}
|
2021-03-31 15:00:47 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
ip := s.dhcpServer.IPByHost(dhcpHost)
|
|
|
|
if ip == (netip.Addr{}) {
|
2022-09-02 16:56:59 +03:00
|
|
|
// Go on and process them with filters, including dnsrewrite ones, and
|
|
|
|
// possibly route them to a domain-specific upstream.
|
2023-08-30 14:02:12 +03:00
|
|
|
log.Debug("dnsforward: no dhcp record for %q", dhcpHost)
|
2021-04-08 18:07:29 +03:00
|
|
|
|
2022-09-02 16:56:59 +03:00
|
|
|
return resultCodeSuccess
|
2020-08-18 12:36:52 +03:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
log.Debug("dnsforward: dhcp record for %q is %s", dhcpHost, ip)
|
2020-08-18 12:36:52 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
resp := s.replyCompressed(req)
|
2023-01-23 19:10:56 +03:00
|
|
|
switch q.Qtype {
|
|
|
|
case dns.TypeA:
|
2021-03-25 16:00:27 +03:00
|
|
|
a := &dns.A{
|
|
|
|
Hdr: s.hdr(req, dns.TypeA),
|
2022-10-26 21:12:51 +03:00
|
|
|
A: ip.AsSlice(),
|
2020-08-18 12:36:52 +03:00
|
|
|
}
|
|
|
|
resp.Answer = append(resp.Answer, a)
|
2023-01-23 19:10:56 +03:00
|
|
|
case dns.TypeAAAA:
|
2023-02-06 17:17:51 +03:00
|
|
|
if s.dns64Pref != (netip.Prefix{}) {
|
2023-01-23 19:10:56 +03:00
|
|
|
// Respond with DNS64-mapped address for IPv4 host if DNS64 is
|
|
|
|
// enabled.
|
|
|
|
aaaa := &dns.AAAA{
|
|
|
|
Hdr: s.hdr(req, dns.TypeAAAA),
|
|
|
|
AAAA: s.mapDNS64(ip),
|
|
|
|
}
|
|
|
|
resp.Answer = append(resp.Answer, aaaa)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Go on.
|
2020-08-18 12:36:52 +03:00
|
|
|
}
|
2023-01-23 19:10:56 +03:00
|
|
|
|
2021-03-25 16:00:27 +03:00
|
|
|
dctx.proxyCtx.Res = resp
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeSuccess
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:56:59 +03:00
|
|
|
// processDHCPAddrs responds to PTR requests if the target IP is leased by the
|
|
|
|
// DHCP server.
|
2022-09-02 14:52:19 +03:00
|
|
|
func (s *Server) processDHCPAddrs(dctx *dnsContext) (rc resultCode) {
|
2023-06-28 13:46:04 +03:00
|
|
|
log.Debug("dnsforward: started processing dhcp addrs")
|
|
|
|
defer log.Debug("dnsforward: finished processing dhcp addrs")
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
pctx := dctx.proxyCtx
|
|
|
|
if pctx.Res != nil {
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeSuccess
|
2020-06-23 12:13:13 +03:00
|
|
|
}
|
2021-03-31 15:00:47 +03:00
|
|
|
|
2024-04-22 19:17:30 +03:00
|
|
|
req := pctx.Req
|
|
|
|
q := req.Question[0]
|
2024-04-11 14:03:37 +03:00
|
|
|
pref := pctx.RequestedPrivateRDNS
|
2024-04-22 19:17:30 +03:00
|
|
|
// TODO(e.burkov): Consider answering authoritatively for SOA and NS
|
|
|
|
// queries.
|
|
|
|
if pref == (netip.Prefix{}) || q.Qtype != dns.TypePTR {
|
2022-10-26 21:12:51 +03:00
|
|
|
return resultCodeSuccess
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
addr := pref.Addr()
|
|
|
|
host := s.dhcpServer.HostByIP(addr)
|
2023-08-30 14:02:12 +03:00
|
|
|
if host == "" {
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeSuccess
|
2020-06-23 12:13:13 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
log.Debug("dnsforward: dhcp client %s is %q", addr, host)
|
2020-06-23 12:13:13 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
resp := s.replyCompressed(req)
|
2021-03-31 15:00:47 +03:00
|
|
|
ptr := &dns.PTR{
|
|
|
|
Hdr: dns.RR_Header{
|
2024-04-22 19:17:30 +03:00
|
|
|
Name: q.Name,
|
2021-03-31 15:00:47 +03:00
|
|
|
Rrtype: dns.TypePTR,
|
2023-08-30 14:02:12 +03:00
|
|
|
// TODO(e.burkov): Use [dhcpsvc.Lease.Expiry]. See
|
|
|
|
// https://github.com/AdguardTeam/AdGuardHome/issues/3932.
|
2023-09-04 17:18:43 +03:00
|
|
|
Ttl: s.dnsFilter.BlockedResponseTTL(),
|
2023-08-30 14:02:12 +03:00
|
|
|
Class: dns.ClassINET,
|
2021-03-31 15:00:47 +03:00
|
|
|
},
|
2023-08-30 14:02:12 +03:00
|
|
|
Ptr: dns.Fqdn(strings.Join([]string{host, s.localDomainSuffix}, ".")),
|
2020-06-23 12:13:13 +03:00
|
|
|
}
|
|
|
|
resp.Answer = append(resp.Answer, ptr)
|
2022-09-02 14:52:19 +03:00
|
|
|
pctx.Res = resp
|
2021-03-31 15:00:47 +03:00
|
|
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
// Apply filtering logic
|
2023-09-08 17:55:13 +03:00
|
|
|
func (s *Server) processFilteringBeforeRequest(dctx *dnsContext) (rc resultCode) {
|
2023-06-28 13:46:04 +03:00
|
|
|
log.Debug("dnsforward: started processing filtering before req")
|
|
|
|
defer log.Debug("dnsforward: finished processing filtering before req")
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
if dctx.proxyCtx.RequestedPrivateRDNS != (netip.Prefix{}) {
|
|
|
|
// There is no need to filter request for locally served ARPA hostname
|
|
|
|
// so disable redundant filters.
|
|
|
|
dctx.setts.ParentalEnabled = false
|
|
|
|
dctx.setts.SafeBrowsingEnabled = false
|
|
|
|
dctx.setts.SafeSearchEnabled = false
|
|
|
|
dctx.setts.ServicesRules = nil
|
|
|
|
}
|
|
|
|
|
2023-09-08 17:55:13 +03:00
|
|
|
if dctx.proxyCtx.Res != nil {
|
2021-10-20 19:52:13 +03:00
|
|
|
// Go on since the response is already set.
|
|
|
|
return resultCodeSuccess
|
2020-06-23 12:13:13 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
var err error
|
2023-09-08 17:55:13 +03:00
|
|
|
if dctx.result, err = s.filterDNSRequest(dctx); err != nil {
|
|
|
|
dctx.err = err
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeError
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeSuccess
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2021-05-28 13:02:59 +03:00
|
|
|
// ipStringFromAddr extracts an IP address string from net.Addr.
|
|
|
|
func ipStringFromAddr(addr net.Addr) (ipStr string) {
|
2021-08-09 16:03:37 +03:00
|
|
|
if ip, _ := netutil.IPAndPortFromAddr(addr); ip != nil {
|
2021-05-28 13:02:59 +03:00
|
|
|
return ip.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-01-13 16:56:05 +03:00
|
|
|
// processUpstream passes request to upstream servers and handles the response.
|
2021-12-13 18:06:01 +03:00
|
|
|
func (s *Server) processUpstream(dctx *dnsContext) (rc resultCode) {
|
2023-06-28 13:46:04 +03:00
|
|
|
log.Debug("dnsforward: started processing upstream")
|
|
|
|
defer log.Debug("dnsforward: finished processing upstream")
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
pctx := dctx.proxyCtx
|
2022-09-02 16:56:59 +03:00
|
|
|
req := pctx.Req
|
2023-08-30 14:02:12 +03:00
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
if pctx.Res != nil {
|
|
|
|
// The response has already been set.
|
|
|
|
return resultCodeSuccess
|
2023-08-30 14:02:12 +03:00
|
|
|
} else if dctx.isDHCPHost {
|
2022-09-02 16:56:59 +03:00
|
|
|
// A DHCP client hostname query that hasn't been handled or filtered.
|
|
|
|
// Respond with an NXDOMAIN.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Route such queries to a custom upstream for the
|
|
|
|
// local domain name if there is one.
|
2023-08-30 14:02:12 +03:00
|
|
|
name := req.Question[0].Name
|
|
|
|
log.Debug("dnsforward: dhcp client hostname %q was not filtered", name[:len(name)-1])
|
2024-04-11 14:03:37 +03:00
|
|
|
pctx.Res = s.NewMsgNXDOMAIN(req)
|
2022-09-02 16:56:59 +03:00
|
|
|
|
|
|
|
return resultCodeFinish
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
s.setCustomUpstream(pctx, dctx.clientID)
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2023-02-13 19:07:10 +03:00
|
|
|
reqWantsDNSSEC := s.setReqAD(req)
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2021-10-06 13:14:41 +03:00
|
|
|
// Process the request further since it wasn't filtered.
|
|
|
|
prx := s.proxy()
|
|
|
|
if prx == nil {
|
2021-12-13 18:06:01 +03:00
|
|
|
dctx.err = srvClosedErr
|
2021-10-06 13:14:41 +03:00
|
|
|
|
|
|
|
return resultCodeError
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
if dctx.err = prx.Resolve(pctx); dctx.err != nil {
|
2023-01-23 19:10:56 +03:00
|
|
|
return resultCodeError
|
|
|
|
}
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
dctx.responseFromUpstream = true
|
|
|
|
dctx.responseAD = pctx.Res.AuthenticatedData
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2023-02-13 19:07:10 +03:00
|
|
|
s.setRespAD(pctx, reqWantsDNSSEC)
|
|
|
|
|
|
|
|
return resultCodeSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
// setReqAD changes the request based on the server settings. wantsDNSSEC is
|
|
|
|
// false if the response should be cleared of the AD bit.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov, e.burkov): This should probably be done in module dnsproxy.
|
|
|
|
func (s *Server) setReqAD(req *dns.Msg) (wantsDNSSEC bool) {
|
|
|
|
if !s.conf.EnableDNSSEC {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
origReqAD := req.AuthenticatedData
|
|
|
|
req.AuthenticatedData = true
|
|
|
|
|
|
|
|
// Per [RFC 6840] says, validating resolvers should only set the AD bit when
|
|
|
|
// the response has the AD bit set and the request contained either a set DO
|
|
|
|
// bit or a set AD bit. So, if neither of these is true, clear the AD bits
|
|
|
|
// in [Server.setRespAD].
|
|
|
|
//
|
|
|
|
// [RFC 6840]: https://datatracker.ietf.org/doc/html/rfc6840#section-5.8
|
|
|
|
return origReqAD || hasDO(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasDO returns true if msg has EDNS(0) options and the DNSSEC OK flag is set
|
|
|
|
// in there.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Move to golibs/dnsmsg when it's there.
|
|
|
|
func hasDO(msg *dns.Msg) (do bool) {
|
|
|
|
o := msg.IsEdns0()
|
|
|
|
if o == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.Do()
|
|
|
|
}
|
|
|
|
|
|
|
|
// setRespAD changes the request and response based on the server settings and
|
|
|
|
// the original request data.
|
|
|
|
func (s *Server) setRespAD(pctx *proxy.DNSContext, reqWantsDNSSEC bool) {
|
|
|
|
if s.conf.EnableDNSSEC && !reqWantsDNSSEC {
|
2021-12-13 18:06:01 +03:00
|
|
|
pctx.Req.AuthenticatedData = false
|
|
|
|
pctx.Res.AuthenticatedData = false
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// dhcpHostFromRequest returns a hostname from question, if the request is for a
|
|
|
|
// DHCP client's hostname when DHCP is enabled, and an empty string otherwise.
|
|
|
|
func (s *Server) dhcpHostFromRequest(q *dns.Question) (reqHost string) {
|
2022-09-02 16:56:59 +03:00
|
|
|
if !s.dhcpServer.Enabled() {
|
2023-08-30 14:02:12 +03:00
|
|
|
return ""
|
2022-09-02 16:56:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include AAAA here, because despite the fact that we don't support it yet,
|
|
|
|
// the expected behavior here is to respond with an empty answer and not
|
|
|
|
// NXDOMAIN.
|
|
|
|
if qt := q.Qtype; qt != dns.TypeA && qt != dns.TypeAAAA {
|
2023-08-30 14:02:12 +03:00
|
|
|
return ""
|
2022-09-02 16:56:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reqHost = strings.ToLower(q.Name[:len(q.Name)-1])
|
2023-08-30 14:02:12 +03:00
|
|
|
if !netutil.IsImmediateSubdomain(reqHost, s.localDomainSuffix) {
|
|
|
|
return ""
|
2022-09-02 16:56:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
return reqHost[:len(reqHost)-len(s.localDomainSuffix)-1]
|
2022-09-02 16:56:59 +03:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
// setCustomUpstream sets custom upstream settings in pctx, if necessary.
|
|
|
|
func (s *Server) setCustomUpstream(pctx *proxy.DNSContext, clientID string) {
|
2023-12-05 17:43:50 +03:00
|
|
|
if !pctx.Addr.IsValid() || s.conf.ClientsContainer == nil {
|
2022-09-02 14:52:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the ClientID first, since it has a higher priority.
|
2024-04-11 14:03:37 +03:00
|
|
|
id := cmp.Or(clientID, pctx.Addr.Addr().String())
|
2023-11-16 14:05:10 +03:00
|
|
|
upsConf, err := s.conf.ClientsContainer.UpstreamConfigByID(id, s.bootstrap)
|
2022-09-02 14:52:19 +03:00
|
|
|
if err != nil {
|
2022-10-26 21:12:51 +03:00
|
|
|
log.Error("dnsforward: getting custom upstreams for client %s: %s", id, err)
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if upsConf != nil {
|
2022-10-26 21:12:51 +03:00
|
|
|
log.Debug("dnsforward: using custom upstreams for client %s", id)
|
2022-09-02 14:52:19 +03:00
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
pctx.CustomUpstreamConfig = upsConf
|
|
|
|
}
|
2022-09-02 14:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply filtering logic after we have received response from upstream servers
|
|
|
|
func (s *Server) processFilteringAfterResponse(dctx *dnsContext) (rc resultCode) {
|
2023-06-28 15:29:38 +03:00
|
|
|
log.Debug("dnsforward: started processing filtering after resp")
|
|
|
|
defer log.Debug("dnsforward: finished processing filtering after resp")
|
2023-06-28 13:46:04 +03:00
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
switch res := dctx.result; res.Reason {
|
2021-10-20 19:52:13 +03:00
|
|
|
case filtering.NotFilteredAllowList:
|
2022-09-02 14:52:19 +03:00
|
|
|
return resultCodeSuccess
|
2021-10-20 19:52:13 +03:00
|
|
|
case
|
|
|
|
filtering.Rewritten,
|
2024-04-22 10:48:26 +03:00
|
|
|
filtering.RewrittenRule,
|
|
|
|
filtering.FilteredSafeSearch:
|
2020-12-21 17:48:07 +03:00
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
if dctx.origQuestion.Name == "" {
|
2021-10-20 19:52:13 +03:00
|
|
|
// origQuestion is set in case we get only CNAME without IP from
|
|
|
|
// rewrites table.
|
2022-09-02 14:52:19 +03:00
|
|
|
return resultCodeSuccess
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2020-06-01 15:23:08 +03:00
|
|
|
|
2023-09-08 17:55:13 +03:00
|
|
|
pctx := dctx.proxyCtx
|
2022-09-02 14:52:19 +03:00
|
|
|
pctx.Req.Question[0], pctx.Res.Question[0] = dctx.origQuestion, dctx.origQuestion
|
2024-04-22 10:48:26 +03:00
|
|
|
|
|
|
|
rr := s.genAnswerCNAME(pctx.Req, res.CanonName)
|
|
|
|
answer := append([]dns.RR{rr}, pctx.Res.Answer...)
|
|
|
|
pctx.Res.Answer = answer
|
2022-09-02 14:52:19 +03:00
|
|
|
|
|
|
|
return resultCodeSuccess
|
2020-05-08 18:39:37 +03:00
|
|
|
default:
|
2023-09-08 17:55:13 +03:00
|
|
|
return s.filterAfterResponse(dctx)
|
2022-09-02 14:52:19 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-20 19:52:13 +03:00
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
// filterAfterResponse returns the result of filtering the response that wasn't
|
|
|
|
// explicitly allowed or rewritten.
|
2023-09-08 17:55:13 +03:00
|
|
|
func (s *Server) filterAfterResponse(dctx *dnsContext) (res resultCode) {
|
2022-09-02 14:52:19 +03: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.
|
2023-08-07 16:07:21 +03:00
|
|
|
if !dctx.protectionEnabled || !dctx.responseFromUpstream {
|
2022-09-02 14:52:19 +03:00
|
|
|
return resultCodeSuccess
|
|
|
|
}
|
2021-10-20 19:52:13 +03:00
|
|
|
|
2023-09-08 17:55:13 +03:00
|
|
|
err := s.filterDNSResponse(dctx)
|
2022-09-02 14:52:19 +03:00
|
|
|
if err != nil {
|
|
|
|
dctx.err = err
|
2021-10-20 19:52:13 +03:00
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
return resultCodeError
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeSuccess
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|