Skip to content

Commit af7236f

Browse files
committed
Check ipt options before looking for ip6t
iptables package has a function `detectIptables()` called to initialize some local variables. Since v20.10.0, it first looks for iptables bin, then ip6tables and finally it checks what iptables flags are available (including -C). It early exits when ip6tables isn't available, and doesn't execute the last check. To remove port mappings (eg. when a container stops/dies), Docker first checks if those NAT rules exist and then deletes them. However, in the particular case where there's no ip6tables bin available, iptables `-C` flag is considered unavailable and thus it looks for NAT rules by using some substring matching. This substring matching then fails because `iptables -t nat -S POSTROUTING` dumps rules in a slighly format than what's expected. For instance, here's what `iptables -t nat -S POSTROUTING` dumps: ``` -A POSTROUTING -s 172.18.0.2/32 -d 172.18.0.2/32 -p tcp -m tcp --dport 9999 -j MASQUERADE ``` And here's what Docker looks for: ``` POSTROUTING -p tcp -s 172.18.0.2 -d 172.18.0.2 --dport 9999 -j MASQUERADE ``` Because of that, those rules are considered non-existant by Docker and thus never deleted. To fix that, this change reorders the code in `detectIptables()`. Fixes #42127. Signed-off-by: Albin Kerouanton <[email protected]>
1 parent f6848ae commit af7236f

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

libnetwork/iptables/iptables.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,21 @@ func detectIptables() {
116116
return
117117
}
118118
iptablesPath = path
119-
path, err = exec.LookPath("ip6tables")
120-
if err != nil {
121-
return
122-
}
123-
ip6tablesPath = path
119+
124120
supportsXlock = exec.Command(iptablesPath, "--wait", "-L", "-n").Run() == nil
125121
mj, mn, mc, err := GetVersion()
126122
if err != nil {
127123
logrus.Warnf("Failed to read iptables version: %v", err)
128124
return
129125
}
130126
supportsCOpt = supportsCOption(mj, mn, mc)
127+
128+
path, err = exec.LookPath("ip6tables")
129+
if err != nil {
130+
return
131+
} else {
132+
ip6tablesPath = path
133+
}
131134
}
132135

133136
func initDependencies() {

0 commit comments

Comments
 (0)