-
Notifications
You must be signed in to change notification settings - Fork 70
/
rules.go
294 lines (260 loc) · 7.86 KB
/
rules.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package rules
import (
"fmt"
"strconv"
"strings"
)
type IPTablesRule []string
func AppendComment(rule IPTablesRule, comment string) IPTablesRule {
comment = strings.Replace(comment, " ", "_", -1)
return IPTablesRule(
append(rule, "-m", "comment", "--comment", comment),
)
}
func NewPortForwardingRule(hostPort, containerPort int, hostIP, containerIP string) IPTablesRule {
return IPTablesRule{
"-d", hostIP, "-p", "tcp",
"-m", "tcp", "--dport", fmt.Sprintf("%d", hostPort),
"--jump", "DNAT",
"--to-destination", fmt.Sprintf("%s:%d", containerIP, containerPort),
}
}
func NewIngressMarkRule(hostInterface string, hostPort int, hostIP, tag string) IPTablesRule {
return IPTablesRule{
"-i", hostInterface, "-d", hostIP, "-p", "tcp",
"-m", "tcp", "--dport", fmt.Sprintf("%d", hostPort),
"--jump", "MARK",
"--set-mark", fmt.Sprintf("0x%s", tag),
}
}
func NewMarkAllowRule(destinationIP, protocol string, startPort, endPort int, tag string, sourceAppGUID, destinationAppGUID string) IPTablesRule {
return AppendComment(IPTablesRule{
"-d", destinationIP,
"-p", protocol,
"--dport", fmt.Sprintf("%d:%d", startPort, endPort),
"-m", "mark", "--mark", fmt.Sprintf("0x%s", tag),
"--jump", "ACCEPT",
}, fmt.Sprintf("src:%s_dst:%s", sourceAppGUID, destinationAppGUID))
}
func NewMarkAllowLogRule(destinationIP, protocol string, startPort, endPort int, tag string, destinationAppGUID string, acceptedUDPLogsPerSec int) IPTablesRule {
if protocol != "udp" {
return IPTablesRule{
"-d", destinationIP,
"-p", protocol,
"--dport", fmt.Sprintf("%d:%d", startPort, endPort),
"-m", "mark", "--mark", fmt.Sprintf("0x%s", tag),
"-m", "conntrack", "--ctstate", "INVALID,NEW,UNTRACKED",
"--jump", "LOG", "--log-prefix",
trimAndPad(fmt.Sprintf("OK_%s_%s", tag, destinationAppGUID))}
} else {
return IPTablesRule{
"-d", destinationIP,
"-p", protocol,
"--dport", fmt.Sprintf("%d:%d", startPort, endPort),
"-m", "mark", "--mark", fmt.Sprintf("0x%s", tag),
"-m", "limit",
"--limit", fmt.Sprintf("%d/s", acceptedUDPLogsPerSec),
"--limit-burst", strconv.Itoa(acceptedUDPLogsPerSec),
"--jump", "LOG", "--log-prefix",
trimAndPad(fmt.Sprintf("OK_%s_%s", tag, destinationAppGUID))}
}
}
func NewMarkSetRule(sourceIP, tag, appGUID string) IPTablesRule {
return AppendComment(IPTablesRule{
"--source", sourceIP,
"--jump", "MARK", "--set-xmark", fmt.Sprintf("0x%s", tag),
}, fmt.Sprintf("src:%s", appGUID))
}
func NewDefaultEgressRule(localSubnet, deviceName string) IPTablesRule {
return IPTablesRule{
"--source", localSubnet,
"!", "-o", deviceName,
"--jump", "MASQUERADE",
}
}
func NewLogRule(rule IPTablesRule, name string) IPTablesRule {
return IPTablesRule(append(
rule, "-m", "limit", "--limit", "2/min",
"--jump", "LOG",
"--log-prefix", trimAndPad(name),
))
}
func NewAcceptExistingLocalRule() IPTablesRule {
return IPTablesRule{
"-m", "state", "--state", "ESTABLISHED,RELATED",
"--jump", "ACCEPT",
}
}
func NewLogLocalRejectRule(localSubnet string) IPTablesRule {
return NewLogRule(
IPTablesRule{
"-s", localSubnet,
"-d", localSubnet,
},
"REJECT_LOCAL: ",
)
}
func NewDefaultDenyLocalRule(localSubnet string) IPTablesRule {
return IPTablesRule{
"--source", localSubnet,
"-d", localSubnet,
"--jump", "REJECT",
}
}
func NewNetOutRule(startIP, endIP string) IPTablesRule {
return IPTablesRule{
"-m", "iprange",
"--dst-range", fmt.Sprintf("%s-%s", startIP, endIP),
"--jump", "ACCEPT",
}
}
func NewNetOutWithPortsRule(startIP, endIP string, startPort, endPort int, protocol string) IPTablesRule {
return IPTablesRule{
"-m", "iprange",
"-p", protocol,
"--dst-range", fmt.Sprintf("%s-%s", startIP, endIP),
"-m", protocol,
"--destination-port", fmt.Sprintf("%d:%d", startPort, endPort),
"--jump", "ACCEPT",
}
}
func NewNetOutICMPRule(startIP, endIP string, icmpType, icmpCode int) IPTablesRule {
return IPTablesRule{
"-m", "iprange",
"-p", "icmp",
"--dst-range", fmt.Sprintf("%s-%s", startIP, endIP),
"-m", "icmp",
"--icmp-type", fmt.Sprintf("%d/%d", icmpType, icmpCode),
"--jump", "ACCEPT",
}
}
func NewNetOutICMPLogRule(startIP, endIP string, icmpType, icmpCode int, chain string) IPTablesRule {
return IPTablesRule{
"-m", "iprange",
"-p", "icmp",
"--dst-range", fmt.Sprintf("%s-%s", startIP, endIP),
"-m", "icmp",
"--icmp-type", fmt.Sprintf("%d/%d", icmpType, icmpCode),
"-g", chain,
}
}
func NewNetOutLogRule(startIP, endIP, chain string) IPTablesRule {
return IPTablesRule{
"-m", "iprange",
"--dst-range", fmt.Sprintf("%s-%s", startIP, endIP),
"-g", chain,
}
}
func NewNetOutWithPortsLogRule(startIP, endIP string, startPort, endPort int, protocol, chain string) IPTablesRule {
return IPTablesRule{
"-m", "iprange",
"-p", protocol,
"--dst-range", fmt.Sprintf("%s-%s", startIP, endIP),
"-m", protocol,
"--destination-port", fmt.Sprintf("%d:%d", startPort, endPort),
"-g", chain,
}
}
func NewNetOutDefaultNonUDPLogRule(prefix string) IPTablesRule {
return IPTablesRule{
"!", "-p", "udp",
"-m", "conntrack", "--ctstate", "INVALID,NEW,UNTRACKED",
"-j", "LOG", "--log-prefix", trimAndPad(fmt.Sprintf("OK_%s", prefix)),
}
}
func NewNetOutDefaultUDPLogRule(prefix string, acceptedUDPLogsPerSec int) IPTablesRule {
return IPTablesRule{
"-p", "udp",
"-m", "limit", "--limit", fmt.Sprintf("%d/s", acceptedUDPLogsPerSec),
"--limit-burst", strconv.Itoa(acceptedUDPLogsPerSec),
"-j", "LOG", "--log-prefix", trimAndPad(fmt.Sprintf("OK_%s", prefix)),
}
}
func NewAcceptRule() IPTablesRule {
return IPTablesRule{
"--jump", "ACCEPT",
}
}
func NewInputRelatedEstablishedRule() IPTablesRule {
return IPTablesRule{
"-m", "state", "--state", "RELATED,ESTABLISHED",
"--jump", "ACCEPT",
}
}
func NewInputAllowRule(protocol, destination string, destPort int) IPTablesRule {
return IPTablesRule{
"-p", protocol,
"-d", destination, "--destination-port", strconv.Itoa(destPort),
"--jump", "ACCEPT",
}
}
func NewInputDefaultRejectRule() IPTablesRule {
return IPTablesRule{
"--jump", "REJECT",
"--reject-with", "icmp-port-unreachable",
}
}
func NewNetOutRelatedEstablishedRule() IPTablesRule {
return IPTablesRule{
"-m", "state", "--state", "RELATED,ESTABLISHED",
"--jump", "ACCEPT",
}
}
func NewOverlayTagAcceptRule(containerIP, tag string) IPTablesRule {
return IPTablesRule{
"-d", containerIP,
"-m", "mark", "--mark", fmt.Sprintf("0x%s", tag),
"--jump", "ACCEPT",
}
}
func NewOverlayDefaultRejectRule(containerIP string) IPTablesRule {
return IPTablesRule{
"-d", containerIP,
"--jump", "REJECT",
"--reject-with", "icmp-port-unreachable",
}
}
func NewOverlayDefaultRejectLogRule(containerHandle, containerIP string, deniedLogsPerSec int) IPTablesRule {
return IPTablesRule{
"-d", containerIP,
"-m", "limit", "--limit", fmt.Sprintf("%d/s", deniedLogsPerSec),
"--limit-burst", strconv.Itoa(deniedLogsPerSec),
"--jump", "LOG",
"--log-prefix", trimAndPad(fmt.Sprintf("DENY_C2C_%s", containerHandle)),
}
}
func NewOverlayAllowEgress(deviceName, containerIP string) IPTablesRule {
return IPTablesRule{
"-s", containerIP,
"-o", deviceName,
"-m", "mark", "!", "--mark", "0x0",
"--jump", "ACCEPT",
}
}
func NewOverlayRelatedEstablishedRule(containerIP string) IPTablesRule {
return IPTablesRule{
"-d", containerIP,
"-m", "state", "--state", "RELATED,ESTABLISHED",
"--jump", "ACCEPT",
}
}
func NewNetOutDefaultRejectLogRule(containerHandle string, deniedLogsPerSec int) IPTablesRule {
return IPTablesRule{
"-m", "limit", "--limit", fmt.Sprintf("%d/s", deniedLogsPerSec),
"--limit-burst", strconv.Itoa(deniedLogsPerSec),
"--jump", "LOG",
"--log-prefix", trimAndPad(fmt.Sprintf("DENY_%s", containerHandle)),
}
}
func NewNetOutDefaultRejectRule() IPTablesRule {
return IPTablesRule{
"--jump", "REJECT",
"--reject-with", "icmp-port-unreachable",
}
}
func trimAndPad(name string) string {
if len(name) > 28 {
name = name[:28]
}
return fmt.Sprintf(`"%s "`, name)
}