Skip to content

Commit f12dcbc

Browse files
committed
portmap: fix CHECK for nftables backend
Fixes 01a94e1 Signed-off-by: Etienne Champetier <[email protected]>
1 parent 372953d commit f12dcbc

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

plugins/meta/portmap/portmap_nftables.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,24 @@ func (pmNFT *portMapperNFTables) checkPorts(config *PortMapConf, containerNet ne
246246
var hostPorts, hostIPHostPorts, masqueradings int
247247
for _, e := range config.RuntimeConfig.PortMaps {
248248
if e.HostIP != "" {
249-
hostIPHostPorts++
249+
hostIP := net.ParseIP(e.HostIP)
250+
isHostV6 := (hostIP.To4() == nil)
251+
// Ignore wrong-IP-family HostIPs
252+
if isV6 != isHostV6 {
253+
continue
254+
}
255+
if hostIP.IsUnspecified() {
256+
hostPorts++
257+
} else {
258+
hostIPHostPorts++
259+
}
250260
} else {
251261
hostPorts++
252262
}
253263
}
254264
if *config.SNAT {
255-
masqueradings = len(config.RuntimeConfig.PortMaps)
256-
if isV6 {
265+
masqueradings = 1
266+
if !isV6 {
257267
masqueradings *= 2
258268
}
259269
}

plugins/meta/portmap/portmap_nftables_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var _ = Describe("portmapping configuration (nftables)", func() {
4545
}
4646
})
4747

48-
It(fmt.Sprintf("[%s] generates correct rules on ADD", ver), func() {
48+
It(fmt.Sprintf("[%s] generates correct rules", ver), func() {
4949
configBytes := []byte(fmt.Sprintf(`{
5050
"name": "test",
5151
"type": "portmap",
@@ -72,10 +72,13 @@ var _ = Describe("portmapping configuration (nftables)", func() {
7272
Expect(err).NotTo(HaveOccurred())
7373
conf.ContainerID = containerID
7474

75-
containerNet, err := types.ParseCIDR("10.0.0.2/24")
75+
containerNet4, err := types.ParseCIDR("10.0.0.2/24")
7676
Expect(err).NotTo(HaveOccurred())
7777

78-
err = pmNFT.forwardPorts(conf, *containerNet)
78+
err = pmNFT.forwardPorts(conf, *containerNet4)
79+
Expect(err).NotTo(HaveOccurred())
80+
81+
err = pmNFT.checkPorts(conf, *containerNet4)
7982
Expect(err).NotTo(HaveOccurred())
8083

8184
expectedRules := strings.TrimSpace(`
@@ -103,10 +106,13 @@ add rule ip cni_hostport prerouting a b jump hostports
103106

104107
// Disable snat, generate IPv6 rules
105108
*conf.SNAT = false
106-
containerNet, err = types.ParseCIDR("2001:db8::2/64")
109+
containerNet6, err := types.ParseCIDR("2001:db8::2/64")
110+
Expect(err).NotTo(HaveOccurred())
111+
112+
err = pmNFT.forwardPorts(conf, *containerNet6)
107113
Expect(err).NotTo(HaveOccurred())
108114

109-
err = pmNFT.forwardPorts(conf, *containerNet)
115+
err = pmNFT.checkPorts(conf, *containerNet6)
110116
Expect(err).NotTo(HaveOccurred())
111117

112118
expectedRules = strings.TrimSpace(`

0 commit comments

Comments
 (0)