-
Notifications
You must be signed in to change notification settings - Fork 51
/
acls.go
363 lines (305 loc) · 10.1 KB
/
acls.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package ipsetctrl
import (
"errors"
"fmt"
"go.uber.org/zap"
"github.com/aporeto-inc/trireme-lib/policy"
"github.com/bvandewalle/go-ipset/ipset"
)
const (
appChainPrefix = "TRIREME-App-"
netChainPrefix = "TRIREME-Net-"
allowPrefix = "A-"
rejectPrefix = "R-"
)
// createACLSets creates the sets for a given PU
func (i *Instance) createACLSets(version string, set string, rules policy.IPRuleList) error {
allowSet, err := i.ips.NewIpset(set+allowPrefix+version, "hash:net,port", &ipset.Params{})
if err != nil {
return fmt.Errorf("unable to create ipset for trireme: %s", err)
}
rejectSet, err := i.ips.NewIpset(set+rejectPrefix+version, "hash:net,port", &ipset.Params{})
if err != nil {
return fmt.Errorf("unable to create ipset for trireme: %s", err)
}
for _, rule := range rules {
var err error
switch rule.Policy.Action {
case policy.Accept:
err = allowSet.Add(rule.Address+","+rule.Port, 0)
case policy.Reject:
err = rejectSet.Add(rule.Address+","+rule.Port, 0)
default:
continue
}
if err != nil {
return fmt.Errorf("unable to create ipset for trireme: %s", err)
}
}
return nil
}
// AddAppSetRule adds an ACL rule to the Set
func (i *Instance) addAppSetRules(version, setPrefix, ip string) error {
if err := i.ipt.Insert(
i.appAckPacketIPTableContext, i.appPacketIPTableSection, 3,
"-m", "state", "--state", "NEW",
"-m", "set", "--match-set", setPrefix+rejectPrefix+version, "dst",
"-s", ip,
"-j", "DROP",
); err != nil {
zap.L().Debug("Error when adding app acl rule",
zap.String("appAckPacketIPTableContext", i.appAckPacketIPTableContext),
zap.Error(err),
)
return fmt.Errorf("unable to add app acl rule: %s", err)
}
if err := i.ipt.Insert(
i.appAckPacketIPTableContext, i.appPacketIPTableSection, 3,
"-m", "state", "--state", "NEW",
"-m", "set", "--match-set", setPrefix+allowPrefix+version, "dst",
"-s", ip,
"-j", "ACCEPT",
); err != nil {
zap.L().Debug("Error when adding app acl rule",
zap.String("appAckPacketIPTableContext", i.appAckPacketIPTableContext),
zap.Error(err),
)
return fmt.Errorf("unable to add app acl rule: %s", err)
}
return nil
}
// addNetSetRule
func (i *Instance) addNetSetRules(version, setPrefix, ip string) error {
if err := i.ipt.Insert(
i.netPacketIPTableContext, i.netPacketIPTableSection, 2,
"-m", "state", "--state", "NEW",
"-m", "set", "--match-set", setPrefix+rejectPrefix+version, "src",
"-d", ip,
"-j", "DROP",
); err != nil {
zap.L().Debug("Error when adding app acl rule",
zap.String("netPacketIPTableContext", i.netPacketIPTableContext),
zap.Error(err),
)
return fmt.Errorf("unable to add net acl rule: %s", err)
}
if err := i.ipt.Insert(
i.netPacketIPTableContext, i.netPacketIPTableSection, 2,
"-m", "state", "--state", "NEW",
"-m", "set", "--match-set", setPrefix+allowPrefix+version, "src",
"-d", ip,
"-j", "ACCEPT",
); err != nil {
zap.L().Debug("Error when adding app acl rule",
zap.String("netPacketIPTableContext", i.netPacketIPTableContext),
zap.Error(err),
)
return fmt.Errorf("unable to add net acl rule: %s", err)
}
return nil
}
// deleteAppSetRule
func (i *Instance) deleteAppSetRules(version, setPrefix, ip string) error {
if err := i.ipt.Delete(
i.appAckPacketIPTableContext, i.appPacketIPTableSection,
"-m", "state", "--state", "NEW",
"-m", "set", "--match-set", setPrefix+rejectPrefix+version, "dst",
"-s", ip,
"-j", "DROP",
); err != nil {
zap.L().Debug("Error when removing app acl rule",
zap.String("appAckPacketIPTableContext", i.appAckPacketIPTableContext),
zap.Error(err),
)
}
if err := i.ipt.Delete(
i.appAckPacketIPTableContext, i.appPacketIPTableSection,
"-m", "state", "--state", "NEW",
"-m", "set", "--match-set", setPrefix+allowPrefix+version, "dst",
"-s", ip,
"-j", "ACCEPT",
); err != nil {
zap.L().Debug("Error when removing ingress app acl rule",
zap.String("netPacketIPTableContext", i.netPacketIPTableContext),
zap.String("chaim", i.appPacketIPTableSection),
zap.Error(err),
)
}
return nil
}
// deleteNetSetRule
func (i *Instance) deleteNetSetRules(version, setPrefix, ip string) error {
if err := i.ipt.Delete(
i.netPacketIPTableContext, i.netPacketIPTableSection,
"-m", "state", "--state", "NEW",
"-m", "set", "--match-set", setPrefix+rejectPrefix+version, "src",
"-d", ip,
"-j", "DROP",
); err != nil {
zap.L().Debug("Error when removing ingress net acl rule",
zap.String("netPacketIPTableContext", i.netPacketIPTableContext),
zap.String("chaim", i.appPacketIPTableSection),
zap.Error(err),
)
}
if err := i.ipt.Delete(
i.netPacketIPTableContext, i.netPacketIPTableSection,
"-m", "state", "--state", "NEW",
"-m", "set", "--match-set", setPrefix+allowPrefix+version, "src",
"-d", ip,
"-j", "ACCEPT",
); err != nil {
zap.L().Debug("ErrorError when removing ingress app acl rule",
zap.String("netPacketIPTableContext", i.netPacketIPTableContext),
zap.String("chaim", i.appPacketIPTableSection),
zap.Error(err),
)
}
return nil
}
//deleteSet deletes the ipset
func (i *Instance) deleteSet(set string) error {
ipSet, err := i.ips.NewIpset(set, "hash:net,port", &ipset.Params{})
if err != nil {
return fmt.Errorf("unable to create ipset for trireme: %s", err)
}
return ipSet.Destroy()
}
// setupIpset sets up an ipset
func (i *Instance) setupIpset(target, container string) error {
ips, err := i.ips.NewIpset(target, "hash:net", &ipset.Params{})
if err != nil {
return fmt.Errorf("unable to create ipset for %s: %s", target, err)
}
i.targetSet = ips
cSet, err := i.ips.NewIpset(container, "hash:ip", &ipset.Params{})
if err != nil {
return fmt.Errorf("unable to create container ipset: %s", err)
}
i.containerSet = cSet
return nil
}
// addTargetNets adds the target networks to the IPset
func (i *Instance) addTargetNets(networks []string) error {
if i.targetSet == nil {
return errors.New("target net set not configured")
}
for _, net := range networks {
if err := i.targetSet.Add(net, 0); err != nil {
return fmt.Errorf("unable to add ip %s to target networks ipset: %s", net, err)
}
}
return nil
}
func (i *Instance) addContainerToSet(ip string) error {
if i.containerSet == nil {
return errors.New("invalid operation: container set is nil")
}
if err := i.containerSet.Add(ip, 0); err != nil {
return fmt.Errorf("unable to add ip %s to container ipset : %s", ip, err)
}
return nil
}
func (i *Instance) delContainerFromSet(ip string) error {
if i.containerSet == nil {
return errors.New("invalid operation: container set is nil")
}
if err := i.containerSet.Del(ip); err != nil {
return fmt.Errorf("unable to add ip %s to container ipset: %s", ip, err)
}
return nil
}
// addIpsetOption
func (i *Instance) addIpsetOption(ip string) error {
if i.targetSet == nil {
return errors.New("cannot add option: target set is nil")
}
return i.targetSet.AddOption(ip, "nomatch", 0)
}
// deleteIpsetOption
func (i *Instance) deleteIpsetOption(ip string) error {
if i.targetSet == nil {
return errors.New("cannot remove option: target set is nil")
}
return i.targetSet.Del(ip)
}
// setupTrapRules
func (i *Instance) setupTrapRules(set string) error {
rules := [][]string{
// Application Syn
{
i.appPacketIPTableContext, i.appPacketIPTableSection,
"-m", "set", "--match-set", set, "dst",
"-m", "set", "--match-set", containerSet, "src",
"-p", "tcp", "--tcp-flags", "SYN,ACK", "SYN",
"-j", "NFQUEUE", "--queue-balance", i.fqc.GetApplicationQueueSynStr(),
},
// Application Syn and Syn/Ack accepted
{
i.appAckPacketIPTableContext, i.appPacketIPTableSection,
"-m", "set", "--match-set", set, "dst",
"-m", "set", "--match-set", containerSet, "src",
"-p", "tcp", "--tcp-flags", "SYN,ACK", "SYN",
"-j", "ACCEPT",
},
// Application Matching Trireme SRC and DST. everything but SYN, first 4 packets
{
i.appAckPacketIPTableContext, i.appPacketIPTableSection,
"-m", "set", "--match-set", containerSet, "src",
"-m", "set", "--match-set", set, "dst",
"-p", "tcp",
"-m", "connbytes", "--connbytes", ":3", "--connbytes-dir", "original", "--connbytes-mode", "packets",
"-j", "NFQUEUE", "--queue-balance", i.fqc.GetApplicationQueueAckStr(),
},
// Default Drop from Trireme to Network
{
i.appAckPacketIPTableContext, i.appPacketIPTableSection,
"-m", "set", "--match-set", containerSet, "src",
"-p", "tcp", "-m", "state", "--state", "NEW",
"-j", "DROP",
},
// Network Matching Trireme SRC and DST.
{
i.netPacketIPTableContext, i.netPacketIPTableSection,
"-m", "set", "--match-set", set, "src",
"-m", "set", "--match-set", containerSet, "dst",
"-p", "tcp", "--tcp-flags", "SYN,ACK", "SYN",
"-j", "NFQUEUE", "--queue-balance", i.fqc.GetNetworkQueueSynStr(),
},
// Network Matching Trireme SRC and DST. Everything ut SYN, first 4 packets
{
i.netPacketIPTableContext, i.netPacketIPTableSection,
"-m", "set", "--match-set", set, "src",
"-m", "set", "--match-set", containerSet, "dst",
"-p", "tcp",
"-m", "connbytes", "--connbytes", ":3", "--connbytes-dir", "original", "--connbytes-mode", "packets",
"-j", "NFQUEUE", "--queue-balance", i.fqc.GetNetworkQueueAckStr(),
},
// Default Drop from Network to Trireme.
{
i.netPacketIPTableContext, i.netPacketIPTableSection,
"-m", "set", "--match-set", containerSet, "dst",
"-p", "tcp", "-m", "state", "--state", "NEW",
"-j", "DROP",
},
}
for _, tr := range rules {
if err := i.ipt.Append(tr[0], tr[1], tr[2:]...); err != nil {
return fmt.Errorf("unable to add initial rules for triremenet ipset: %s", err)
}
}
return nil
}
// cleanIPSets cleans all the ipsets
func (i *Instance) cleanIPSets() error {
if err := i.ipt.ClearChain(i.appPacketIPTableContext, i.appPacketIPTableSection); err != nil {
zap.L().Warn("Failed to cleanup app packet chain", zap.Error(err))
}
if err := i.ipt.ClearChain(i.appAckPacketIPTableContext, i.appPacketIPTableSection); err != nil {
zap.L().Warn("Failed to cleanup ack packet chain", zap.Error(err))
}
if err := i.ipt.ClearChain(i.netPacketIPTableContext, i.netPacketIPTableSection); err != nil {
zap.L().Warn("Failed to cleanup net packet chain", zap.Error(err))
}
return i.ips.DestroyAll()
}