Skip to content

Commit 44cf27b

Browse files
committed
libnet/d/overlay: extract VNI match rule builder
The iptables rule clause used to match on the VNI of VXLAN datagrams looks like line noise to the uninitiated. It doesn't help that the expression is repeated twice and neither copy has any commentary. DRY out the rule builder to a common function, and document what the rule does and how it works. Signed-off-by: Cory Snider <[email protected]>
1 parent 142f46c commit 44cf27b

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

libnetwork/drivers/overlay/encryption.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,9 @@ func removeEncryption(localIP, remoteIP net.IP, em *encrMap) error {
227227

228228
func programMangle(vni uint32, add bool) error {
229229
var (
230-
p = strconv.FormatUint(uint64(overlayutils.VXLANUDPPort()), 10)
231-
c = fmt.Sprintf("0>>22&0x3C@12&0xFFFFFF00=%d", int(vni)<<8)
232230
m = strconv.FormatUint(mark, 10)
233231
chain = "OUTPUT"
234-
rule = []string{"-p", "udp", "--dport", p, "-m", "u32", "--u32", c, "-j", "MARK", "--set-mark", m}
232+
rule = append(matchVXLAN(overlayutils.VXLANUDPPort(), vni), "-j", "MARK", "--set-mark", m)
235233
a = iptables.Append
236234
action = "install"
237235
)
@@ -253,12 +251,10 @@ func programMangle(vni uint32, add bool) error {
253251

254252
func programInput(vni uint32, add bool) error {
255253
var (
256-
port = strconv.FormatUint(uint64(overlayutils.VXLANUDPPort()), 10)
257-
vniMatch = fmt.Sprintf("0>>22&0x3C@12&0xFFFFFF00=%d", int(vni)<<8)
258-
plainVxlan = []string{"-p", "udp", "--dport", port, "-m", "u32", "--u32", vniMatch, "-j"}
254+
plainVxlan = matchVXLAN(overlayutils.VXLANUDPPort(), vni)
259255
ipsecVxlan = append([]string{"-m", "policy", "--dir", "in", "--pol", "ipsec"}, plainVxlan...)
260-
block = append(plainVxlan, "DROP")
261-
accept = append(ipsecVxlan, "ACCEPT")
256+
block = append(plainVxlan, "-j", "DROP")
257+
accept = append(ipsecVxlan, "-j", "ACCEPT")
262258
chain = "INPUT"
263259
action = iptables.Append
264260
msg = "add"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package overlay
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
// matchVXLAN returns an iptables rule fragment which matches VXLAN datagrams
9+
// with the given destination port and VXLAN Network ID utilizing the xt_u32
10+
// netfilter kernel module. The returned slice's backing array is guaranteed not
11+
// to alias any other slice's.
12+
func matchVXLAN(port, vni uint32) []string {
13+
dport := strconv.FormatUint(uint64(port), 10)
14+
15+
// The u32 expression language is documented in iptables-extensions(8).
16+
// https://ipset.netfilter.org/iptables-extensions.man.html#lbCK
17+
//
18+
// 0>>22&0x3C ; Compute number of octets in IPv4 header
19+
// @ ; Make this the new offset into the packet
20+
// ; (jump to start of UDP header)
21+
// 12&0xFFFFFF00 ; Read 32-bit value at offset 12 and mask off the bottom octet
22+
// = ; Test whether the value is equal to a constant
23+
//
24+
// A UDP header is eight octets long so offset 12 from the start of the
25+
// UDP header is four octets into the payload: the VNI field of the
26+
// VXLAN header.
27+
vniMatch := fmt.Sprintf("0>>22&0x3C@12&0xFFFFFF00=%d", int(vni)<<8)
28+
29+
return []string{"-p", "udp", "--dport", dport, "-m", "u32", "--u32", vniMatch}
30+
}

0 commit comments

Comments
 (0)