/
iptables.go
492 lines (402 loc) · 16 KB
/
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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package iptablesctrl
import (
"crypto/md5"
"encoding/base64"
"errors"
"fmt"
"io"
"strconv"
"go.uber.org/zap"
"github.com/aporeto-inc/trireme-lib/constants"
"github.com/aporeto-inc/trireme-lib/enforcer/utils/fqconfig"
"github.com/aporeto-inc/trireme-lib/internal/portset"
"github.com/aporeto-inc/trireme-lib/policy"
"github.com/bvandewalle/go-ipset/ipset"
"github.com/aporeto-inc/trireme-lib/internal/supervisor/provider"
)
const (
uidchain = "UIDCHAIN"
chainPrefix = "TRIREME-"
appChainPrefix = chainPrefix + "App-"
netChainPrefix = chainPrefix + "Net-"
targetNetworkSet = "TargetNetSet"
// PuPortSet The prefix for portset names
PuPortSet = "PUPort-"
proxyPortSet = "Proxy-"
ipTableSectionOutput = "OUTPUT"
ipTableSectionInput = "INPUT"
ipTableSectionPreRouting = "PREROUTING"
natProxyOutputChain = "RedirProxy-App"
natProxyInputChain = "RedirProxy-Net"
proxyOutputChain = "Proxy-App"
proxyInputChain = "Proxy-Net"
proxyMark = "0x40"
// ProxyPort DefaultProxyPort
ProxyPort = "5000"
)
// Instance is the structure holding all information about a implementation
type Instance struct {
fqc *fqconfig.FilterQueue
ipt provider.IptablesProvider
ipset provider.IpsetProvider
vipTargetSet provider.Ipset
pipTargetSet provider.Ipset
targetSet provider.Ipset
appPacketIPTableContext string
appProxyIPTableContext string
appPacketIPTableSection string
netPacketIPTableContext string
netPacketIPTableSection string
appCgroupIPTableSection string
appSynAckIPTableSection string
mode constants.ModeType
portSetInstance portset.PortSet
}
// NewInstance creates a new iptables controller instance
func NewInstance(fqc *fqconfig.FilterQueue, mode constants.ModeType, portset portset.PortSet) (*Instance, error) {
ipt, err := provider.NewGoIPTablesProvider()
if err != nil {
return nil, fmt.Errorf("unable to initialize iptables provider: %s", err)
}
ips := provider.NewGoIPsetProvider()
if err != nil {
return nil, fmt.Errorf("unable to initialize ipsets: %s", err)
}
i := &Instance{
fqc: fqc,
ipt: ipt,
ipset: ips,
appPacketIPTableContext: "mangle",
netPacketIPTableContext: "mangle",
appProxyIPTableContext: "nat",
mode: mode,
portSetInstance: portset,
appPacketIPTableSection: ipTableSectionOutput,
appCgroupIPTableSection: ipTableSectionOutput,
netPacketIPTableSection: ipTableSectionInput,
appSynAckIPTableSection: ipTableSectionOutput,
}
return i, nil
}
// chainPrefix returns the chain name for the specific PU
func (i *Instance) chainName(contextID string, version int) (app, net string, err error) {
hash := md5.New()
if _, err := io.WriteString(hash, contextID); err != nil {
return "", "", err
}
output := base64.URLEncoding.EncodeToString(hash.Sum(nil))
if len(contextID) > 4 {
contextID = contextID[:4] + string(output[:6])
} else {
contextID = contextID + string(output[:6])
}
app = appChainPrefix + contextID + "-" + strconv.Itoa(version)
net = netChainPrefix + contextID + "-" + strconv.Itoa(version)
return app, net, nil
}
// PuPortSetName returns the name of the pu portset
func PuPortSetName(contextID string, mark string, prefix string) string {
hash := md5.New()
if _, err := io.WriteString(hash, contextID); err != nil {
return ""
}
output := base64.URLEncoding.EncodeToString(hash.Sum(nil))
if len(contextID) > 4 {
contextID = contextID[:4] + string(output[:4])
} else {
contextID = contextID + string(output[:4])
}
return (prefix + contextID + mark)
}
// DefaultIPAddress returns the default IP address for the processing unit
func (i *Instance) defaultIP(addresslist map[string]string) (string, bool) {
if ip, ok := addresslist[policy.DefaultNamespace]; ok && len(ip) > 0 {
return ip, true
}
return "0.0.0.0/0", true
}
// ConfigureRules implmenets the ConfigureRules interface
func (i *Instance) ConfigureRules(version int, contextID string, containerInfo *policy.PUInfo) error {
policyrules := containerInfo.Policy
appChain, netChain, err := i.chainName(contextID, version)
if err != nil {
return err
}
// Supporting only one ip
ipAddress, ok := i.defaultIP(policyrules.IPAddresses())
if !ok {
return errors.New("no ip address found")
}
proxyPort := containerInfo.Runtime.Options().ProxyPort
zap.L().Debug("Configure rules", zap.String("proxyPort", proxyPort))
proxiedServices := containerInfo.Policy.ProxiedServices()
// Configure all the ACLs
if err = i.addContainerChain(appChain, netChain); err != nil {
return err
}
if i.mode != constants.LocalServer {
proxyPortSetName := PuPortSetName(contextID, "", proxyPortSet)
if err = i.createProxySets(proxiedServices.PublicIPPortPair, proxiedServices.PrivateIPPortPair, proxyPortSetName); err != nil {
zap.L().Debug("Failed to create ProxySets", zap.Error(err))
return fmt.Errorf("Failed to create ProxySet %s : %s", proxyPortSetName, err)
}
if err = i.addChainRules("", appChain, netChain, ipAddress, "", "", "", proxyPort, proxyPortSetName); err != nil {
return err
}
} else {
mark := containerInfo.Runtime.Options().CgroupMark
if mark == "" {
return errors.New("no mark value found")
}
port := policy.ConvertServicesToPortList(containerInfo.Runtime.Options().Services)
uid := containerInfo.Runtime.Options().UserID
if uid != "" {
// We are about to create a uid login pu
// This set will be empty and we will only fill it when we find a port for it
// The reason to use contextID here is to ensure that we don't need to talk between supervisor and enforcer to share names the id is derivable from information available in the enforcer
portSetName := PuPortSetName(contextID, mark, PuPortSet)
if puseterr := i.createPUPortSet(portSetName); puseterr != nil {
return puseterr
}
// update the portset cache, so that it can program the portset
if i.portSetInstance == nil {
return errors.New("enforcer portset instance cannot be nil for host")
}
if err = i.portSetInstance.AddUserPortSet(uid, portSetName, mark); err != nil {
return err
}
}
portSetName := PuPortSetName(contextID, mark, PuPortSet)
proxyPortSetName := PuPortSetName(contextID, mark, proxyPortSet)
if err = i.createProxySets(proxiedServices.PublicIPPortPair, proxiedServices.PrivateIPPortPair, proxyPortSetName); err != nil {
zap.L().Debug("Failed to create ProxySets", zap.Error(err))
return fmt.Errorf("Failed to create ProxySet %s : %s", proxyPortSetName, err)
}
if err := i.addChainRules(portSetName, appChain, netChain, ipAddress, port, mark, uid, proxyPort, proxyPortSetName); err != nil {
return err
}
}
if err := i.addPacketTrap(appChain, netChain, ipAddress, containerInfo.Policy.TriremeNetworks()); err != nil {
return err
}
if err := i.addAppACLs(contextID, appChain, ipAddress, policyrules.ApplicationACLs()); err != nil {
return err
}
if err := i.addNetACLs(contextID, netChain, ipAddress, policyrules.NetworkACLs()); err != nil {
return err
}
return i.addExclusionACLs(appChain, netChain, ipAddress, policyrules.ExcludedNetworks())
}
// DeleteRules implements the DeleteRules interface
func (i *Instance) DeleteRules(version int, contextID string, ipAddresses policy.ExtendedMap, port string, mark string, uid string, proxyPort string, proxyPortSetName string) error {
var ipAddress string
var ok bool
// Supporting only one ip
if i.mode != constants.LocalServer {
if ipAddresses == nil {
return errors.New("provided map of ip addresses is nil")
}
ipAddress, ok = i.defaultIP(ipAddresses)
if !ok {
return errors.New("no ip address found")
}
}
appChain, netChain, err := i.chainName(contextID, version)
if err != nil {
// Don't return here we can still try and reclaims portset and targetnetwork sets
zap.L().Error("Count not generate chain name", zap.Error(err))
}
portSetName := PuPortSetName(contextID, mark, PuPortSet)
if derr := i.deleteChainRules(portSetName, appChain, netChain, ipAddress, port, mark, uid, proxyPort, proxyPortSetName); derr != nil {
zap.L().Warn("Failed to clean rules", zap.Error(derr))
}
if err = i.deleteAllContainerChains(appChain, netChain); err != nil {
zap.L().Warn("Failed to clean container chains while deleting the rules", zap.Error(err))
}
if uid != "" {
portSetName := PuPortSetName(contextID, mark, PuPortSet)
ips := ipset.IPSet{
Name: portSetName,
}
if err = ips.Destroy(); err != nil {
zap.L().Warn("Failed to clear puport set", zap.Error(err))
}
// delete the entry in the portset cache
if i.portSetInstance == nil {
return errors.New("enforcer portset instance cannot be nil for host")
}
if err = i.portSetInstance.DelUserPortSet(uid, mark); err != nil {
return err
}
}
dstPortSetName, srcPortSetName := i.getSetNamePair(proxyPortSetName)
ips := ipset.IPSet{
Name: dstPortSetName,
}
if err := ips.Destroy(); err != nil {
zap.L().Warn("Failed to destroy proxyPortSet", zap.String("SetName", proxyPortSetName), zap.Error(err))
}
ips = ipset.IPSet{
Name: srcPortSetName,
}
if err := ips.Destroy(); err != nil {
zap.L().Warn("Failed to destroy proxyPortSet", zap.String("SetName", proxyPortSetName), zap.Error(err))
}
return nil
}
// UpdateRules implements the update part of the interface
func (i *Instance) UpdateRules(version int, contextID string, containerInfo *policy.PUInfo, oldContainerInfo *policy.PUInfo) error {
if containerInfo == nil {
return errors.New("container info cannot be nil")
}
policyrules := containerInfo.Policy
if policyrules == nil {
return errors.New("policy rules cannot be nil")
}
// Supporting only one ip
ipAddress, ok := i.defaultIP(policyrules.IPAddresses())
if !ok {
return errors.New("no ip address found")
}
proxyPort := containerInfo.Runtime.Options().ProxyPort
appChain, netChain, err := i.chainName(contextID, version)
if err != nil {
return err
}
oldAppChain, oldNetChain, err := i.chainName(contextID, version^1)
if err != nil {
return err
}
// Add a new chain for this update and map all rules there
if err := i.addContainerChain(appChain, netChain); err != nil {
return err
}
if err := i.addPacketTrap(appChain, netChain, ipAddress, containerInfo.Policy.TriremeNetworks()); err != nil {
return err
}
if err := i.addAppACLs(contextID, appChain, ipAddress, policyrules.ApplicationACLs()); err != nil {
return err
}
if err := i.addNetACLs(contextID, netChain, ipAddress, policyrules.NetworkACLs()); err != nil {
return err
}
if err := i.addExclusionACLs(appChain, netChain, ipAddress, policyrules.ExcludedNetworks()); err != nil {
return err
}
// Add mapping to new chain
if i.mode != constants.LocalServer {
proxyPortSetName := PuPortSetName(contextID, "", proxyPortSet)
if err := i.addChainRules("", appChain, netChain, ipAddress, "", "", "", proxyPort, proxyPortSetName); err != nil {
return err
}
} else {
mark := containerInfo.Runtime.Options().CgroupMark
if mark == "" {
return errors.New("no mark value found")
}
portlist := policy.ConvertServicesToPortList(containerInfo.Runtime.Options().Services)
uid := containerInfo.Runtime.Options().UserID
portSetName := PuPortSetName(contextID, mark, PuPortSet)
proxyPortSetName := PuPortSetName(contextID, mark, proxyPortSet)
if err := i.addChainRules(portSetName, appChain, netChain, ipAddress, portlist, mark, uid, proxyPort, proxyPortSetName); err != nil {
return err
}
}
// Remove mapping from old chain
if i.mode != constants.LocalServer {
proxyPortSetName := PuPortSetName(contextID, "", proxyPortSet)
if err := i.deleteChainRules("", oldAppChain, oldNetChain, ipAddress, "", "", "", proxyPort, proxyPortSetName); err != nil {
return err
}
} else {
mark := containerInfo.Runtime.Options().CgroupMark
port := policy.ConvertServicesToPortList(containerInfo.Runtime.Options().Services)
uid := containerInfo.Runtime.Options().UserID
portSetName := PuPortSetName(contextID, mark, PuPortSet)
proxyPortSetName := PuPortSetName(contextID, mark, proxyPortSet)
if err := i.deleteChainRules(portSetName, oldAppChain, oldNetChain, ipAddress, port, mark, uid, proxyPort, proxyPortSetName); err != nil {
return err
}
}
// Update Proxy Ports
mark := ""
if i.mode == constants.LocalServer {
mark = containerInfo.Runtime.Options().CgroupMark
}
proxyPortSetName := PuPortSetName(contextID, mark, proxyPortSet)
proxiedServiceList := containerInfo.Policy.ProxiedServices()
if err := i.updateProxySet(proxiedServiceList.PublicIPPortPair, proxiedServiceList.PrivateIPPortPair, proxyPortSetName); err != nil {
zap.L().Debug("Failed to update Proxy Set", zap.Error(err),
zap.Strings("Public ProxiedService List", proxiedServiceList.PublicIPPortPair),
zap.Strings("Private ProxiedService List", proxiedServiceList.PrivateIPPortPair),
)
return fmt.Errorf("Failed to update proxySet %s : %s", proxyPortSetName, err)
}
// Delete the old chain to clean up
return i.deleteAllContainerChains(oldAppChain, oldNetChain)
}
// Start starts the iptables controller
func (i *Instance) Start() error {
// Clean any previous ACLs
if err := i.cleanACLs(); err != nil {
zap.L().Warn("Unable to clean previous acls while starting the supervisor", zap.Error(err))
}
zap.L().Debug("Started the iptables controller")
return nil
}
// SetTargetNetworks updates ths target networks for SynAck packets
func (i *Instance) SetTargetNetworks(current, networks []string) error {
if len(networks) == 0 {
networks = []string{"0.0.0.0/1", "128.0.0.0/1"}
}
// Cleanup old ACLs
if len(current) > 0 {
return i.updateTargetNetworks(current, networks)
}
// Create the target network set
if err := i.createTargetSet(networks); err != nil {
return err
}
if i.mode == constants.LocalServer {
if err := i.ipt.NewChain(i.appPacketIPTableContext, uidchain); err != nil {
zap.L().Error("Unable to create new chain", zap.String("TableContext", i.appPacketIPTableContext), zap.String("ChainName", uidchain))
return err
}
}
if err := i.ipt.NewChain(i.appProxyIPTableContext, natProxyInputChain); err != nil {
zap.L().Info("Unable to create New Chain", zap.String("TableContext", i.appProxyIPTableContext), zap.String("ChainName", natProxyInputChain))
}
zap.L().Debug("Created NewChain ", zap.String("TableContext", i.appProxyIPTableContext), zap.String("ChainName", natProxyOutputChain))
if err := i.ipt.NewChain(i.appProxyIPTableContext, natProxyOutputChain); err != nil {
zap.L().Info("Unable to create New Chain", zap.String("TableContext", i.appProxyIPTableContext), zap.String("ChainName", natProxyOutputChain))
}
zap.L().Debug("Created NewChain ", zap.String("TableContext", i.appProxyIPTableContext), zap.String("ChainName", natProxyOutputChain))
if err := i.ipt.NewChain(i.appPacketIPTableContext, proxyOutputChain); err != nil {
zap.L().Error("Unable to create New Chain", zap.String("TableContext", i.appPacketIPTableContext), zap.String("ChainName", proxyOutputChain))
}
if err := i.ipt.NewChain(i.appPacketIPTableContext, proxyInputChain); err != nil {
zap.L().Error("Unable to create New Chain", zap.String("TableContext", i.appPacketIPTableContext), zap.String("ChainName", proxyInputChain))
}
if i.mode == constants.LocalServer {
if err := i.ipt.Insert(i.appPacketIPTableContext, i.appPacketIPTableSection, 1, "-j", uidchain); err != nil {
zap.L().Error("Unable to Insert", zap.String("TableContext", i.appPacketIPTableContext), zap.String("ChainName", uidchain))
}
}
// Insert the ACLS that point to the target networks
if err := i.setGlobalRules(i.appPacketIPTableSection, i.netPacketIPTableSection); err != nil {
return fmt.Errorf("failed to update synack networks: %s", err)
}
return nil
}
// Stop stops the supervisor
func (i *Instance) Stop() error {
zap.L().Debug("Stop the supervisor")
// Clean any previous ACLs that we have installed
if err := i.cleanACLs(); err != nil {
zap.L().Error("Failed to clean acls while stopping the supervisor", zap.Error(err))
}
if err := i.ipset.DestroyAll(); err != nil {
zap.L().Error("Failed to clean up ipsets", zap.Error(err))
}
return nil
}