This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_iptables.go
187 lines (166 loc) · 5.91 KB
/
node_iptables.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package plugin
import (
"fmt"
"sync"
"time"
"github.com/golang/glog"
utilwait "k8s.io/apimachinery/pkg/util/wait"
utildbus "k8s.io/kubernetes/pkg/util/dbus"
kexec "k8s.io/kubernetes/pkg/util/exec"
"k8s.io/kubernetes/pkg/util/iptables"
)
type NodeIPTables struct {
ipt iptables.Interface
clusterNetworkCIDR string
syncPeriod time.Duration
masqueradeServices bool
mu sync.Mutex // Protects concurrent access to syncIPTableRules()
}
func newNodeIPTables(clusterNetworkCIDR string, syncPeriod time.Duration, masqueradeServices bool) *NodeIPTables {
return &NodeIPTables{
ipt: iptables.New(kexec.New(), utildbus.New(), iptables.ProtocolIpv4),
clusterNetworkCIDR: clusterNetworkCIDR,
syncPeriod: syncPeriod,
masqueradeServices: masqueradeServices,
}
}
func (n *NodeIPTables) Setup() error {
if err := n.syncIPTableRules(); err != nil {
return err
}
// If firewalld is running, reload will call this method
n.ipt.AddReloadFunc(func() {
if err := n.syncIPTableRules(); err != nil {
glog.Errorf("Reloading openshift iptables failed: %v", err)
}
})
go utilwait.Forever(n.syncLoop, 0)
return nil
}
// syncLoop periodically calls syncIPTableRules().
// This is expected to run as a go routine or as the main loop. It does not return.
func (n *NodeIPTables) syncLoop() {
t := time.NewTicker(n.syncPeriod)
defer t.Stop()
for {
<-t.C
glog.V(6).Infof("Periodic openshift iptables sync")
err := n.syncIPTableRules()
if err != nil {
glog.Errorf("Syncing openshift iptables failed: %v", err)
}
}
}
type Chain struct {
table string
name string
srcChain string
srcRule []string
rules [][]string
}
// Adds all the rules in chain, returning true if they were all already present
func (n *NodeIPTables) addChainRules(chain Chain) (bool, error) {
allExisted := true
for _, rule := range chain.rules {
existed, err := n.ipt.EnsureRule(iptables.Append, iptables.Table(chain.table), iptables.Chain(chain.name), rule...)
if err != nil {
return false, fmt.Errorf("failed to ensure rule %v exists: %v", rule, err)
}
if !existed {
allExisted = false
}
}
return allExisted, nil
}
// syncIPTableRules syncs the cluster network cidr iptables rules.
// Called from SyncLoop() or firewalld reload()
func (n *NodeIPTables) syncIPTableRules() error {
n.mu.Lock()
defer n.mu.Unlock()
start := time.Now()
defer func() {
glog.V(4).Infof("syncIPTableRules took %v", time.Since(start))
}()
glog.V(3).Infof("Syncing openshift iptables rules")
for _, chain := range n.getNodeIPTablesChains() {
// Create chain if it does not already exist
chainExisted, err := n.ipt.EnsureChain(iptables.Table(chain.table), iptables.Chain(chain.name))
if err != nil {
return fmt.Errorf("failed to ensure chain %s exists: %v", chain.name, err)
}
// Create the rule pointing to it from its parent chain. Note that since we
// use iptables.Prepend each time, chains with the same table and srcChain
// (ie, OPENSHIFT-FIREWALL-FORWARD and OPENSHIFT-ADMIN-OUTPUT-RULES) will
// run in *reverse* order of how they are listed in getNodeIPTablesChains().
_, err = n.ipt.EnsureRule(iptables.Prepend, iptables.Table(chain.table), iptables.Chain(chain.srcChain), append(chain.srcRule, "-j", chain.name)...)
if err != nil {
return fmt.Errorf("failed to ensure rule from %s to %s exists: %v", chain.srcChain, chain.name, err)
}
// Add/sync the rules
rulesExisted, err := n.addChainRules(chain)
if err != nil {
return err
}
if chainExisted && !rulesExisted {
// Chain existed but not with the expected rules; this probably means
// it contained rules referring to a *different* subnet; flush them
// and try again.
if err = n.ipt.FlushChain(iptables.Table(chain.table), iptables.Chain(chain.name)); err != nil {
return fmt.Errorf("failed to flush chain %s: %v", chain.name, err)
}
if _, err = n.addChainRules(chain); err != nil {
return err
}
}
}
return nil
}
const VXLAN_PORT = "4789"
func (n *NodeIPTables) getNodeIPTablesChains() []Chain {
var masqRule []string
if n.masqueradeServices {
masqRule = []string{"-s", n.clusterNetworkCIDR, "-m", "comment", "--comment", "masquerade pod-to-service and pod-to-external traffic", "-j", "MASQUERADE"}
} else {
masqRule = []string{"-s", n.clusterNetworkCIDR, "!", "-d", n.clusterNetworkCIDR, "-m", "comment", "--comment", "masquerade pod-to-external traffic", "-j", "MASQUERADE"}
}
return []Chain{
{
table: "nat",
name: "OPENSHIFT-MASQUERADE",
srcChain: "POSTROUTING",
srcRule: []string{"-m", "comment", "--comment", "rules for masquerading OpenShift traffic"},
rules: [][]string{
masqRule,
},
},
{
table: "filter",
name: "OPENSHIFT-FIREWALL-ALLOW",
srcChain: "INPUT",
srcRule: []string{"-m", "comment", "--comment", "firewall overrides"},
rules: [][]string{
{"-p", "udp", "--dport", VXLAN_PORT, "-m", "comment", "--comment", "VXLAN incoming", "-j", "ACCEPT"},
{"-i", TUN, "-m", "comment", "--comment", "from SDN to localhost", "-j", "ACCEPT"},
{"-i", "docker0", "-m", "comment", "--comment", "from docker to localhost", "-j", "ACCEPT"},
},
},
{
table: "filter",
name: "OPENSHIFT-FIREWALL-FORWARD",
srcChain: "FORWARD",
srcRule: []string{"-m", "comment", "--comment", "firewall overrides"},
rules: [][]string{
{"-s", n.clusterNetworkCIDR, "-m", "comment", "--comment", "attempted resend after connection close", "-m", "conntrack", "--ctstate", "INVALID", "-j", "DROP"},
{"-d", n.clusterNetworkCIDR, "-m", "comment", "--comment", "forward traffic from SDN", "-j", "ACCEPT"},
{"-s", n.clusterNetworkCIDR, "-m", "comment", "--comment", "forward traffic to SDN", "-j", "ACCEPT"},
},
},
{
table: "filter",
name: "OPENSHIFT-ADMIN-OUTPUT-RULES",
srcChain: "FORWARD",
srcRule: []string{"-i", TUN, "!", "-o", TUN, "-m", "comment", "--comment", "administrator overrides"},
rules: nil,
},
}
}