AdGuardHome/internal/dnsforward/dns64.go

48 lines
1.5 KiB
Go
Raw Normal View History

2023-02-01 12:41:34 +00:00
package dnsforward
import (
"net"
"net/netip"
"github.com/AdguardTeam/dnsproxy/proxy"
)
// setupDNS64 initializes DNS64 settings, the NAT64 prefixes in particular. If
// the DNS64 feature is enabled and no prefixes are configured, the default
// Well-Known Prefix is used, just like Section 5.2 of RFC 6147 prescribes. Any
// configured set of prefixes discards the default Well-Known prefix unless it
// is specified explicitly. Each prefix also validated to be a valid IPv6
// CIDR with a maximum length of 96 bits. The first specified prefix is then
// used to synthesize AAAA records.
2023-02-15 13:53:29 +00:00
func (s *Server) setupDNS64() {
2023-02-01 12:41:34 +00:00
if !s.conf.UseDNS64 {
2023-02-15 13:53:29 +00:00
return
2023-02-01 12:41:34 +00:00
}
2023-02-15 13:53:29 +00:00
if len(s.conf.DNS64Prefixes) == 0 {
// dns64WellKnownPref is the default prefix to use in an algorithmic
// mapping for DNS64.
//
// See https://datatracker.ietf.org/doc/html/rfc6052#section-2.1.
dns64WellKnownPref := netip.MustParsePrefix("64:ff9b::/96")
2023-02-01 12:41:34 +00:00
2023-02-15 13:53:29 +00:00
s.dns64Pref = dns64WellKnownPref
} else {
s.dns64Pref = s.conf.DNS64Prefixes[0]
2023-02-01 12:41:34 +00:00
}
}
// mapDNS64 maps ip to IPv6 address using configured DNS64 prefix. ip must be a
// valid IPv4. It panics, if there are no configured DNS64 prefixes, because
// synthesis should not be performed unless DNS64 function enabled.
func (s *Server) mapDNS64(ip netip.Addr) (mapped net.IP) {
2023-02-15 13:53:29 +00:00
pref := s.dns64Pref.Masked().Addr().As16()
2023-02-01 12:41:34 +00:00
ipData := ip.As4()
mapped = make(net.IP, net.IPv6len)
2023-02-15 13:53:29 +00:00
copy(mapped[:proxy.NAT64PrefixLength], pref[:])
copy(mapped[proxy.NAT64PrefixLength:], ipData[:])
2023-02-01 12:41:34 +00:00
return mapped
}