/
notrack.go
136 lines (115 loc) · 3.99 KB
/
notrack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package notrack
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/coreos/go-iptables/iptables"
"github.com/telekom/das-schiff-network-operator/pkg/nl"
"github.com/vishvananda/netlink"
"k8s.io/utils/strings/slices"
ctrl "sigs.k8s.io/controller-runtime"
)
const (
iptablesTable = "raw"
iptablesPrerouting = "PREROUTING"
iptablesOutput = "OUTPUT"
iptablesTimeout = 5
syncInterval = 20 * time.Second
ruleMatchesCount = 2
)
var (
notrackLog = ctrl.Log.WithName("notrack")
rulesRegex = regexp.MustCompile(`--comment "?nwop:notrack"?`)
inputInterfaceRegex = regexp.MustCompile(`-i "?([a-zA-Z0-9._-]*)"?`)
notrackLinkPrefixes = []string{"vr."}
)
func parseRuleInterface(rule string) (string, error) {
matches := inputInterfaceRegex.FindStringSubmatch(rule)
if len(matches) != ruleMatchesCount {
return "", fmt.Errorf("illegal matchcount %d (should be %d)", len(matches), ruleMatchesCount)
}
return matches[1], nil
}
func buildRule(link string) []string {
return []string{"-i", link, "-m", "comment", "--comment", "nwop:notrack", "-j", "NOTRACK"}
}
func reconcileIPTables(notrackLinks []string, ipt *iptables.IPTables) error {
rules, err := ipt.List(iptablesTable, iptablesPrerouting)
if err != nil {
return fmt.Errorf("error listing IPTables rules: %w", err)
}
existingLinks := []string{}
for _, rule := range rules {
if !rulesRegex.MatchString(rule) {
continue
}
link, err := parseRuleInterface(rule)
if err != nil {
notrackLog.Error(err, "error parsing rule", "rule", rule)
}
if !slices.Contains(notrackLinks, link) {
if err := ipt.Delete(iptablesTable, iptablesPrerouting, buildRule(link)...); err != nil {
return fmt.Errorf("error deleting IPTables rule: %w", err)
}
}
existingLinks = append(existingLinks, link)
}
for _, notrackLink := range notrackLinks {
if slices.Contains(existingLinks, notrackLink) {
continue
}
if err := ipt.Append(iptablesTable, iptablesPrerouting, buildRule(notrackLink)...); err != nil {
return fmt.Errorf("error appending IPTables rule: %w", err)
}
}
return nil
}
func RunIPTablesSync() error {
ipt4, err := iptables.New(iptables.IPFamily(iptables.ProtocolIPv4), iptables.Timeout(iptablesTimeout))
if err != nil {
return fmt.Errorf("error connecting to ip4tables for notrack: %w", err)
}
ipt6, err := iptables.New(iptables.IPFamily(iptables.ProtocolIPv6), iptables.Timeout(iptablesTimeout))
if err != nil {
return fmt.Errorf("error connecting to ip6tables for notrack: %w", err)
}
netlinkManager := &nl.NetlinkManager{}
go syncIPTTables(netlinkManager, ipt4, ipt6)
return nil
}
func syncIPTTables(netlinkManager *nl.NetlinkManager, ipt4, ipt6 *iptables.IPTables) {
for {
links, err := netlink.LinkList()
if err != nil {
notrackLog.Error(err, "error getting link list for notrack check")
continue
}
var notrackLinks []string
for _, link := range links {
for _, notrackPrefix := range notrackLinkPrefixes {
if strings.HasPrefix(link.Attrs().Name, notrackPrefix) {
notrackLinks = append(notrackLinks, link.Attrs().Name)
break
}
}
}
if err := reconcileIPTables(notrackLinks, ipt4); err != nil {
notrackLog.Error(err, "error reconciling notrack in IPv4 iptables")
}
if err := reconcileIPTables(notrackLinks, ipt6); err != nil {
notrackLog.Error(err, "error reconciling notrack in IPv6 iptables")
}
if underlayIP, err := netlinkManager.GetUnderlayIP(); err == nil {
if err := ipt4.AppendUnique(iptablesTable, iptablesPrerouting, "-d", underlayIP.String(), "-p", "udp", "--dport", "4789", "-j", "NOTRACK"); err != nil {
notrackLog.Error(err, "error reconciling VXLAN notrack in IPv4 iptables")
}
if err := ipt4.AppendUnique(iptablesTable, iptablesOutput, "-s", underlayIP.String(), "-p", "udp", "--dport", "4789", "-j", "NOTRACK"); err != nil {
notrackLog.Error(err, "error reconciling VXLAN notrack in IPv4 iptables")
}
} else {
notrackLog.Error(err, "error reconciling VXLAN notrack in IPv4 iptables")
}
time.Sleep(syncInterval)
}
}