Skip to content

Commit

Permalink
Merge pull request #2233 from Luap99/resolv-ipv6-link-local
Browse files Browse the repository at this point in the history
libnetwork/resolvconf: filter out ipv6 link local
  • Loading branch information
openshift-merge-bot[bot] authored Nov 8, 2024
2 parents 13a63dc + d830bf8 commit 9819e05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 12 additions & 0 deletions libnetwork/resolvconf/resolv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ func TestNew(t *testing.T) {
ipv6: true,
want: "nameserver 1.1.1.1\nnameserver fd::1\n",
},
{
name: "ipv6 link local must always be filtered when netns is private",
baseContent: "nameserver 1.1.1.1\nnameserver fe80::1%eth1\nnameserver fd::1\n",
ipv6: true,
want: "nameserver 1.1.1.1\nnameserver fd::1\n",
},
{
name: "ipv6 link local must not be filtered when netns is host",
baseContent: "nameserver 1.1.1.1\nnameserver fe80::1%eth1\nnameserver fd::1\n",
hostns: true,
want: "nameserver 1.1.1.1\nnameserver fe80::1%eth1\nnameserver fd::1\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
15 changes: 10 additions & 5 deletions libnetwork/resolvconf/resolvconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ var (
// ipLocalhost is a regex pattern for IPv4 or IPv6 loopback range.
ipLocalhost = `((127\.([0-9]{1,3}\.){2}[0-9]{1,3})|(::1)$)`

localhostNSRegexp = regexp.Delayed(`(?m)^nameserver\s+` + ipLocalhost + `\s*\n*`)
nsIPv6Regexp = regexp.Delayed(`(?m)^nameserver\s+` + ipv6Address + `\s*\n*`)
nsRegexp = regexp.Delayed(`^\s*nameserver\s*((` + ipv4Address + `)|(` + ipv6Address + `))\s*$`)
searchRegexp = regexp.Delayed(`^\s*search\s*(([^\s]+\s*)*)$`)
optionsRegexp = regexp.Delayed(`^\s*options\s*(([^\s]+\s*)*)$`)
localhostNSRegexp = regexp.Delayed(`(?m)^nameserver\s+` + ipLocalhost + `\s*\n*`)
nsIPv6Regexp = regexp.Delayed(`(?m)^nameserver\s+` + ipv6Address + `\s*\n*`)
nsIPv6LinkLocalRegexp = regexp.Delayed(`(?m)^nameserver\s+` + ipv6Address + `%.*\s*\n*`)
nsRegexp = regexp.Delayed(`^\s*nameserver\s*((` + ipv4Address + `)|(` + ipv6Address + `))\s*$`)
searchRegexp = regexp.Delayed(`^\s*search\s*(([^\s]+\s*)*)$`)
optionsRegexp = regexp.Delayed(`^\s*options\s*(([^\s]+\s*)*)$`)
)

// filterResolvDNS cleans up the config in resolvConf. It has two main jobs:
Expand All @@ -54,6 +55,10 @@ func filterResolvDNS(resolvConf []byte, ipv6Enabled bool, netnsEnabled bool) []b
// if IPv6 is not enabled, also clean out any IPv6 address nameserver
if !ipv6Enabled {
cleanedResolvConf = nsIPv6Regexp.ReplaceAll(cleanedResolvConf, []byte{})
} else {
// If ipv6 is we still must remove any ipv6 link-local addresses as
// the zone will never match the interface name or index in the container.
cleanedResolvConf = nsIPv6LinkLocalRegexp.ReplaceAll(cleanedResolvConf, []byte{})
}
// if the resulting resolvConf has no more nameservers defined, add appropriate
// default DNS servers for IPv4 and (optionally) IPv6
Expand Down

0 comments on commit 9819e05

Please sign in to comment.