Skip to content

Commit 1fb9793

Browse files
authored
Merge pull request #487 from aojea/pmapHostIp
portmap: don't use unspecified address as iptables rule destination
2 parents b76fdd7 + 5cb3a5e commit 1fb9793

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

plugins/meta/portmap/portmap.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,26 @@ func fillDnatRules(c *chain, config *PortMapConf, containerNet net.IPNet) {
225225
c.rules = make([][]string, 0, 3*len(entries))
226226
for _, entry := range entries {
227227
// If a HostIP is given, only process the entry if host and container address families match
228+
// and append it to the iptables rules
229+
addRuleBaseDst := false
228230
if entry.HostIP != "" {
229231
hostIP := net.ParseIP(entry.HostIP)
230232
isHostV6 := (hostIP.To4() == nil)
231233

232234
if isV6 != isHostV6 {
233235
continue
234236
}
237+
238+
// Unspecified addresses can not be used as destination
239+
if !hostIP.IsUnspecified() {
240+
addRuleBaseDst = true
241+
}
235242
}
236243

237244
ruleBase := []string{
238245
"-p", entry.Protocol,
239246
"--dport", strconv.Itoa(entry.HostPort)}
240-
if entry.HostIP != "" {
247+
if addRuleBaseDst {
241248
ruleBase = append(ruleBase,
242249
"-d", entry.HostIP)
243250
}

plugins/meta/portmap/portmap_test.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ var _ = Describe("portmapping configuration", func() {
170170
{ "hostPort": 8080, "containerPort": 80, "protocol": "tcp"},
171171
{ "hostPort": 8081, "containerPort": 80, "protocol": "tcp"},
172172
{ "hostPort": 8080, "containerPort": 81, "protocol": "udp"},
173-
{ "hostPort": 8082, "containerPort": 82, "protocol": "udp"}
173+
{ "hostPort": 8082, "containerPort": 82, "protocol": "udp"},
174+
{ "hostPort": 8083, "containerPort": 83, "protocol": "tcp", "hostIP": "192.168.0.2"},
175+
{ "hostPort": 8084, "containerPort": 84, "protocol": "tcp", "hostIP": "0.0.0.0"},
176+
{ "hostPort": 8085, "containerPort": 85, "protocol": "tcp", "hostIP": "2001:db8:a::1"},
177+
{ "hostPort": 8086, "containerPort": 86, "protocol": "tcp", "hostIP": "::"}
174178
]
175179
},
176180
"snat": true,
@@ -197,7 +201,7 @@ var _ = Describe("portmapping configuration", func() {
197201
fmt.Sprintf("dnat name: \"test\" id: \"%s\"", containerID),
198202
"-m", "multiport",
199203
"-p", "tcp",
200-
"--destination-ports", "8080,8081",
204+
"--destination-ports", "8080,8081,8083,8084,8085,8086",
201205
"a", "b"},
202206
{"-m", "comment", "--comment",
203207
fmt.Sprintf("dnat name: \"test\" id: \"%s\"", containerID),
@@ -208,18 +212,28 @@ var _ = Describe("portmapping configuration", func() {
208212
}))
209213

210214
Expect(ch.rules).To(Equal([][]string{
215+
// tcp rules and not hostIP
211216
{"-p", "tcp", "--dport", "8080", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"},
212217
{"-p", "tcp", "--dport", "8080", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"},
213218
{"-p", "tcp", "--dport", "8080", "-j", "DNAT", "--to-destination", "10.0.0.2:80"},
214219
{"-p", "tcp", "--dport", "8081", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"},
215220
{"-p", "tcp", "--dport", "8081", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"},
216221
{"-p", "tcp", "--dport", "8081", "-j", "DNAT", "--to-destination", "10.0.0.2:80"},
222+
// udp rules and not hostIP
217223
{"-p", "udp", "--dport", "8080", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"},
218224
{"-p", "udp", "--dport", "8080", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"},
219225
{"-p", "udp", "--dport", "8080", "-j", "DNAT", "--to-destination", "10.0.0.2:81"},
220226
{"-p", "udp", "--dport", "8082", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"},
221227
{"-p", "udp", "--dport", "8082", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"},
222228
{"-p", "udp", "--dport", "8082", "-j", "DNAT", "--to-destination", "10.0.0.2:82"},
229+
// tcp rules and hostIP
230+
{"-p", "tcp", "--dport", "8083", "-d", "192.168.0.2", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"},
231+
{"-p", "tcp", "--dport", "8083", "-d", "192.168.0.2", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"},
232+
{"-p", "tcp", "--dport", "8083", "-d", "192.168.0.2", "-j", "DNAT", "--to-destination", "10.0.0.2:83"},
233+
// tcp rules and hostIP = "0.0.0.0"
234+
{"-p", "tcp", "--dport", "8084", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"},
235+
{"-p", "tcp", "--dport", "8084", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"},
236+
{"-p", "tcp", "--dport", "8084", "-j", "DNAT", "--to-destination", "10.0.0.2:84"},
223237
}))
224238

225239
ch.rules = nil
@@ -229,14 +243,22 @@ var _ = Describe("portmapping configuration", func() {
229243
fillDnatRules(&ch, conf, *n)
230244

231245
Expect(ch.rules).To(Equal([][]string{
246+
// tcp rules and not hostIP
232247
{"-p", "tcp", "--dport", "8080", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"},
233248
{"-p", "tcp", "--dport", "8080", "-j", "DNAT", "--to-destination", "[2001:db8::2]:80"},
234249
{"-p", "tcp", "--dport", "8081", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"},
235250
{"-p", "tcp", "--dport", "8081", "-j", "DNAT", "--to-destination", "[2001:db8::2]:80"},
251+
// udp rules and not hostIP
236252
{"-p", "udp", "--dport", "8080", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"},
237253
{"-p", "udp", "--dport", "8080", "-j", "DNAT", "--to-destination", "[2001:db8::2]:81"},
238254
{"-p", "udp", "--dport", "8082", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"},
239255
{"-p", "udp", "--dport", "8082", "-j", "DNAT", "--to-destination", "[2001:db8::2]:82"},
256+
// tcp rules and hostIP
257+
{"-p", "tcp", "--dport", "8085", "-d", "2001:db8:a::1", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"},
258+
{"-p", "tcp", "--dport", "8085", "-d", "2001:db8:a::1", "-j", "DNAT", "--to-destination", "[2001:db8::2]:85"},
259+
// tcp rules and hostIP = "::"
260+
{"-p", "tcp", "--dport", "8086", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"},
261+
{"-p", "tcp", "--dport", "8086", "-j", "DNAT", "--to-destination", "[2001:db8::2]:86"},
240262
}))
241263

242264
// Disable snat, generate rules
@@ -252,6 +274,8 @@ var _ = Describe("portmapping configuration", func() {
252274
{"-p", "tcp", "--dport", "8081", "-j", "DNAT", "--to-destination", "10.0.0.2:80"},
253275
{"-p", "udp", "--dport", "8080", "-j", "DNAT", "--to-destination", "10.0.0.2:81"},
254276
{"-p", "udp", "--dport", "8082", "-j", "DNAT", "--to-destination", "10.0.0.2:82"},
277+
{"-p", "tcp", "--dport", "8083", "-d", "192.168.0.2", "-j", "DNAT", "--to-destination", "10.0.0.2:83"},
278+
{"-p", "tcp", "--dport", "8084", "-j", "DNAT", "--to-destination", "10.0.0.2:84"},
255279
}))
256280
})
257281

0 commit comments

Comments
 (0)