-
Notifications
You must be signed in to change notification settings - Fork 51
/
datapath.go
520 lines (426 loc) · 16 KB
/
datapath.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package nfqdatapath
// Go libraries
import (
"context"
"fmt"
"os/exec"
"time"
"go.uber.org/zap"
"go.aporeto.io/netlink-go/conntrack"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/controller/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/acls"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/nfqdatapath/afinetrawsocket"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/nfqdatapath/nflog"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/nfqdatapath/tokenaccessor"
"go.aporeto.io/trireme-lib/controller/internal/portset"
"go.aporeto.io/trireme-lib/controller/pkg/fqconfig"
"go.aporeto.io/trireme-lib/controller/pkg/packet"
"go.aporeto.io/trireme-lib/controller/pkg/packetprocessor"
"go.aporeto.io/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/trireme-lib/controller/pkg/secrets"
"go.aporeto.io/trireme-lib/policy"
"go.aporeto.io/trireme-lib/utils/cache"
"go.aporeto.io/trireme-lib/utils/portcache"
"go.aporeto.io/trireme-lib/utils/portspec"
)
// DefaultExternalIPTimeout is the default used for the cache for External IPTimeout.
const DefaultExternalIPTimeout = "500ms"
// GetUDPRawSocket is placeholder for createSocket function. It is useful to mock tcp unit tests.
var GetUDPRawSocket = afinetrawsocket.CreateSocket
// Datapath is the structure holding all information about a connection filter
type Datapath struct {
// Configuration parameters
filterQueue *fqconfig.FilterQueue
collector collector.EventCollector
tokenAccessor tokenaccessor.TokenAccessor
service packetprocessor.PacketProcessor
secrets secrets.Secrets
nflogger nflog.NFLogger
procMountPoint string
targetNetworks *acls.ACLCache
// Internal structures and caches
// Key=ContextId Value=puContext
puFromContextID cache.DataStore
puFromMark cache.DataStore
contextIDFromTCPPort *portcache.PortCache
contextIDFromUDPPort *portcache.PortCache
// For remotes this is a reverse link to the context
puFromIP *pucontext.PUContext
// Hash based on source IP/Port to capture SynAck packets with possible NAT.
// When a new connection is created, we has the source IP/port. A return
// poacket might come with a different source IP NAT is done later.
// If we don't receife a return SynAck in 20 seconds, it expires
sourcePortConnectionCache cache.DataStore
// Hash on full five-tuple and return the connection
// These are auto-expired connections after 60 seconds of inactivity.
appOrigConnectionTracker cache.DataStore
appReplyConnectionTracker cache.DataStore
netOrigConnectionTracker cache.DataStore
netReplyConnectionTracker cache.DataStore
unknownSynConnectionTracker cache.DataStore
udpSourcePortConnectionCache cache.DataStore
// Hash on full five-tuple and return the connection
// These are auto-expired connections after 60 seconds of inactivity.
udpAppOrigConnectionTracker cache.DataStore
udpAppReplyConnectionTracker cache.DataStore
udpNetOrigConnectionTracker cache.DataStore
udpNetReplyConnectionTracker cache.DataStore
udpNatConnectionTracker cache.DataStore
// CacheTimeout used for Trireme auto-detecion
ExternalIPCacheTimeout time.Duration
// connctrack handle
conntrackHdl conntrack.Conntrack
// mode captures the mode of the enforcer
mode constants.ModeType
// ack size
ackSize uint32
mutualAuthorization bool
packetLogs bool
portSetInstance portset.PortSet
// udp socket fd for application.
udpSocketWriter afinetrawsocket.SocketWriter
}
func createPolicy(networks []string) policy.IPRuleList {
var rules policy.IPRuleList
f := policy.FlowPolicy{
Action: policy.Accept,
}
for _, network := range networks {
iprule := policy.IPRule{
Address: network,
Port: "0:65535",
Protocol: "tcp",
Policy: &f,
}
rules = append(rules, iprule)
}
return rules
}
// New will create a new data path structure. It instantiates the data stores
// needed to track sessions. The data path is started with a different call.
// Only required parameters must be provided. Rest a pre-populated with defaults.
func New(
mutualAuth bool,
filterQueue *fqconfig.FilterQueue,
collector collector.EventCollector,
serverID string,
validity time.Duration,
service packetprocessor.PacketProcessor,
secrets secrets.Secrets,
mode constants.ModeType,
procMountPoint string,
ExternalIPCacheTimeout time.Duration,
packetLogs bool,
tokenaccessor tokenaccessor.TokenAccessor,
puFromContextID cache.DataStore,
targetNetworks []string,
) *Datapath {
if ExternalIPCacheTimeout <= 0 {
var err error
ExternalIPCacheTimeout, err = time.ParseDuration(enforcerconstants.DefaultExternalIPTimeout)
if err != nil {
ExternalIPCacheTimeout = time.Second
}
}
if mode == constants.RemoteContainer || mode == constants.LocalServer {
// Make conntrack liberal for TCP
sysctlCmd, err := exec.LookPath("sysctl")
if err != nil {
zap.L().Fatal("sysctl command must be installed", zap.Error(err))
}
cmd := exec.Command(sysctlCmd, "-w", "net.netfilter.nf_conntrack_tcp_be_liberal=1")
if err := cmd.Run(); err != nil {
zap.L().Fatal("Failed to set conntrack options", zap.Error(err))
}
if mode == constants.LocalServer {
cmd = exec.Command(sysctlCmd, "-w", "net.ipv4.ip_early_demux=0")
if err := cmd.Run(); err != nil {
zap.L().Fatal("Failed to set early demux options", zap.Error(err))
}
}
}
// This cache is shared with portSetInstance. The portSetInstance
// cleans up the entry corresponding to port when port is no longer
// part of ipset portset.
contextIDFromTCPPort := portcache.NewPortCache("contextIDFromTCPPort")
contextIDFromUDPPort := portcache.NewPortCache("contextIDFromUDPPort")
var portSetInstance portset.PortSet
if mode != constants.RemoteContainer {
portSetInstance = portset.New(contextIDFromTCPPort)
}
udpSocketWriter, err := GetUDPRawSocket(afinetrawsocket.ApplicationRawSocketMark, "udp")
if err != nil {
zap.L().Fatal("Unable to create raw socket for udp packet transmission", zap.Error(err))
}
d := &Datapath{
puFromMark: cache.NewCache("puFromMark"),
contextIDFromTCPPort: contextIDFromTCPPort,
contextIDFromUDPPort: contextIDFromUDPPort,
puFromContextID: puFromContextID,
sourcePortConnectionCache: cache.NewCacheWithExpiration("sourcePortConnectionCache", time.Second*24),
appOrigConnectionTracker: cache.NewCacheWithExpiration("appOrigConnectionTracker", time.Second*24),
appReplyConnectionTracker: cache.NewCacheWithExpiration("appReplyConnectionTracker", time.Second*24),
netOrigConnectionTracker: cache.NewCacheWithExpiration("netOrigConnectionTracker", time.Second*24),
netReplyConnectionTracker: cache.NewCacheWithExpiration("netReplyConnectionTracker", time.Second*24),
unknownSynConnectionTracker: cache.NewCacheWithExpiration("unknownSynConnectionTracker", time.Second*2),
udpSourcePortConnectionCache: cache.NewCacheWithExpiration("udpSourcePortConnectionCache", time.Second*60),
udpAppOrigConnectionTracker: cache.NewCacheWithExpiration("udpAppOrigConnectionTracker", time.Second*60),
udpAppReplyConnectionTracker: cache.NewCacheWithExpiration("udpAppReplyConnectionTracker", time.Second*60),
udpNetOrigConnectionTracker: cache.NewCacheWithExpiration("udpNetOrigConnectionTracker", time.Second*60),
udpNetReplyConnectionTracker: cache.NewCacheWithExpiration("udpNetReplyConnectionTracker", time.Second*60),
udpNatConnectionTracker: cache.NewCacheWithExpiration("udpNatConnectionTracker", time.Second*60),
targetNetworks: acls.NewACLCache(),
ExternalIPCacheTimeout: ExternalIPCacheTimeout,
filterQueue: filterQueue,
mutualAuthorization: mutualAuth,
service: service,
collector: collector,
tokenAccessor: tokenaccessor,
secrets: secrets,
ackSize: secrets.AckSize(),
mode: mode,
procMountPoint: procMountPoint,
conntrackHdl: conntrack.NewHandle(),
portSetInstance: portSetInstance,
packetLogs: packetLogs,
udpSocketWriter: udpSocketWriter,
}
if err = d.SetTargetNetworks(targetNetworks); err != nil {
zap.L().Error("Error adding target networks to the ACLs")
}
packet.PacketLogLevel = packetLogs
d.nflogger = nflog.NewNFLogger(11, 10, d.puInfoDelegate, collector)
return d
}
// NewWithDefaults create a new data path with most things used by default
func NewWithDefaults(
serverID string,
collector collector.EventCollector,
service packetprocessor.PacketProcessor,
secrets secrets.Secrets,
mode constants.ModeType,
procMountPoint string,
targetNetworks []string,
) *Datapath {
if collector == nil {
zap.L().Fatal("Collector must be given to NewDefaultDatapathEnforcer")
}
defaultMutualAuthorization := false
defaultFQConfig := fqconfig.NewFilterQueueWithDefaults()
defaultValidity := time.Hour * 8760
defaultExternalIPCacheTimeout, err := time.ParseDuration(enforcerconstants.DefaultExternalIPTimeout)
if err != nil {
defaultExternalIPCacheTimeout = time.Second
}
defaultPacketLogs := false
tokenAccessor, err := tokenaccessor.New(serverID, defaultValidity, secrets)
if err != nil {
zap.L().Fatal("Cannot create a token engine")
}
puFromContextID := cache.NewCache("puFromContextID")
return New(
defaultMutualAuthorization,
defaultFQConfig,
collector,
serverID,
defaultValidity,
service,
secrets,
mode,
procMountPoint,
defaultExternalIPCacheTimeout,
defaultPacketLogs,
tokenAccessor,
puFromContextID,
targetNetworks,
)
}
// Enforce implements the Enforce interface method and configures the data path for a new PU
func (d *Datapath) Enforce(contextID string, puInfo *policy.PUInfo) error {
// Always create a new PU context
pu, err := pucontext.NewPU(contextID, puInfo, d.ExternalIPCacheTimeout)
if err != nil {
return fmt.Errorf("error creating new pu: %s", err)
}
// Cache PUs for retrieval based on packet information
if pu.Type() == common.LinuxProcessPU || pu.Type() == common.UIDLoginPU {
mark, tcpPorts, udpPorts := pu.GetProcessKeys()
d.puFromMark.AddOrUpdate(mark, pu)
for _, port := range tcpPorts {
portSpec, err := portspec.NewPortSpecFromString(port, contextID)
if err != nil {
continue
}
d.contextIDFromTCPPort.AddPortSpec(portSpec)
}
for _, port := range udpPorts {
portSpec, err := portspec.NewPortSpecFromString(port, contextID)
if err != nil {
continue
}
d.contextIDFromUDPPort.AddPortSpec(portSpec)
}
} else {
d.puFromIP = pu
}
// pucontext launches a go routine to periodically
// lookup dns names. ctx cancel signals the go routine to exit
if prevPU, _ := d.puFromContextID.Get(contextID); prevPU != nil {
prevPU.(*pucontext.PUContext).CancelFunc()
}
// Cache PU from contextID for management and policy updates
d.puFromContextID.AddOrUpdate(contextID, pu)
return nil
}
// Unenforce removes the configuration for the given PU
func (d *Datapath) Unenforce(contextID string) error {
puContext, err := d.puFromContextID.Get(contextID)
if err != nil {
return fmt.Errorf("contextid not found in enforcer: %s", err)
}
// Cleanup the IP based lookup
pu := puContext.(*pucontext.PUContext)
// Cleanup the mark information
if err := d.puFromMark.Remove(pu.Mark()); err != nil {
zap.L().Debug("Unable to remove cache entry during unenforcement",
zap.String("Mark", pu.Mark()),
zap.Error(err),
)
}
// Cleanup the port cache
for _, port := range pu.TCPPorts() {
if err := d.contextIDFromTCPPort.RemoveStringPorts(port); err != nil {
zap.L().Debug("Unable to remove cache entry during unenforcement",
zap.String("TCPPort", port),
zap.Error(err),
)
}
}
for _, port := range pu.UDPPorts() {
if err := d.contextIDFromUDPPort.RemoveStringPorts(port); err != nil {
zap.L().Debug("Unable to remove cache entry during unenforcement",
zap.String("UDPPort", port),
zap.Error(err),
)
}
}
// Cleanup the contextID cache
if err := d.puFromContextID.RemoveWithDelay(contextID, 10*time.Second); err != nil {
zap.L().Warn("Unable to remove context from cache",
zap.String("contextID", contextID),
zap.Error(err),
)
}
return nil
}
// SetTargetNetworks sets new target networks used by datapath
func (d *Datapath) SetTargetNetworks(networks []string) error {
if len(networks) == 0 {
networks = []string{"0.0.0.0/1", "128.0.0.0/1"}
}
d.targetNetworks = acls.NewACLCache()
targetacl := createPolicy(networks)
return d.targetNetworks.AddRuleList(targetacl)
}
// GetFilterQueue returns the filter queues used by the data path
func (d *Datapath) GetFilterQueue() *fqconfig.FilterQueue {
return d.filterQueue
}
// GetPortSetInstance returns the portset instance used by data path
func (d *Datapath) GetPortSetInstance() portset.PortSet {
return d.portSetInstance
}
// Run starts the application and network interceptors
func (d *Datapath) Run(ctx context.Context) error {
zap.L().Debug("Start enforcer", zap.Int("mode", int(d.mode)))
d.startApplicationInterceptor(ctx)
d.startNetworkInterceptor(ctx)
go d.nflogger.Run(ctx)
return nil
}
// UpdateSecrets updates the secrets used for signing communication between trireme instances
func (d *Datapath) UpdateSecrets(token secrets.Secrets) error {
d.secrets = token
return d.tokenAccessor.SetToken(d.tokenAccessor.GetTokenServerID(), d.tokenAccessor.GetTokenValidity(), token)
}
func (d *Datapath) puInfoDelegate(contextID string) (ID string, tags *policy.TagStore) {
item, err := d.puFromContextID.Get(contextID)
if err != nil {
return
}
ctx := item.(*pucontext.PUContext)
ID = ctx.ManagementID()
tags = ctx.Annotations().Copy()
return
}
func (d *Datapath) reportFlow(p *packet.Packet, sourceID string, destID string, context *pucontext.PUContext, mode string, report *policy.FlowPolicy, packet *policy.FlowPolicy) {
c := &collector.FlowRecord{
ContextID: context.ID(),
Source: &collector.EndPoint{
ID: sourceID,
IP: p.SourceAddress.String(),
Port: p.SourcePort,
Type: collector.EnpointTypePU,
},
Destination: &collector.EndPoint{
ID: destID,
IP: p.DestinationAddress.String(),
Port: p.DestinationPort,
Type: collector.EnpointTypePU,
},
Tags: context.Annotations(),
Action: report.Action,
DropReason: mode,
PolicyID: report.PolicyID,
L4Protocol: p.IPProto,
Count: 1,
}
if report.ObserveAction.Observed() {
c.ObservedAction = packet.Action
c.ObservedPolicyID = packet.PolicyID
}
d.collector.CollectFlowEvent(c)
}
// contextFromIP returns the PU context from the default IP if remote. Otherwise
// it returns the context from the port or mark values of the packet. Synack
// packets are again special and the flow is reversed. If a container doesn't supply
// its IP information, we use the default IP. This will only work with remotes
// and Linux processes.
func (d *Datapath) contextFromIP(app bool, packetIP string, mark string, port uint16, protocol uint8) (*pucontext.PUContext, error) {
if d.puFromIP != nil {
return d.puFromIP, nil
}
if app {
pu, err := d.puFromMark.Get(mark)
if err != nil {
return nil, fmt.Errorf("pu context cannot be found using mark %s: %s", mark, err)
}
return pu.(*pucontext.PUContext), nil
}
if protocol == packet.IPProtocolTCP {
contextID, err := d.contextIDFromTCPPort.GetSpecValueFromPort(port)
if err != nil {
return nil, fmt.Errorf("pu contextID cannot be found using port %d: %s", port, err)
}
pu, err := d.puFromContextID.Get(contextID)
if err != nil {
return nil, fmt.Errorf("unable to find contextID: %s", contextID)
}
return pu.(*pucontext.PUContext), nil
} else if protocol == packet.IPProtocolUDP {
contextID, err := d.contextIDFromUDPPort.GetSpecValueFromPort(port)
if err != nil {
return nil, fmt.Errorf("pu contextID cannot be found using port %d: %s", port, err)
}
pu, err := d.puFromContextID.Get(contextID)
if err != nil {
return nil, fmt.Errorf("unable to find contextID: %s", contextID)
}
return pu.(*pucontext.PUContext), nil
} else {
return nil, fmt.Errorf("Invalid protocol:%d", protocol)
}
}