-
Notifications
You must be signed in to change notification settings - Fork 51
/
utils.go
178 lines (140 loc) · 5.4 KB
/
utils.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
package nfqdatapath
import (
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/controller/pkg/connection"
"go.aporeto.io/trireme-lib/controller/pkg/packet"
"go.aporeto.io/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/trireme-lib/policy"
)
func (d *Datapath) reportAcceptedFlow(p *packet.Packet, conn *connection.TCPConnection, sourceID string, destID string, context *pucontext.PUContext, report *policy.FlowPolicy, packet *policy.FlowPolicy, reverse bool) { // nolint:unparam
if sourceID == destID {
report = &policy.FlowPolicy{
Action: policy.Accept,
PolicyID: "local",
}
packet = report
}
src, dst := d.generateEndpoints(p, sourceID, destID, reverse)
sourceController, destinationController := "", ""
if conn != nil {
sourceController, destinationController = conn.SourceController, conn.DestinationController
}
d.reportFlow(p, src, dst, context, "", report, packet, sourceController, destinationController)
}
func (d *Datapath) reportRejectedFlow(p *packet.Packet, conn *connection.TCPConnection, sourceID string, destID string, context *pucontext.PUContext, mode string, report *policy.FlowPolicy, packet *policy.FlowPolicy, reverse bool) { // nolint:unparam
if report == nil {
report = &policy.FlowPolicy{
Action: policy.Reject,
PolicyID: "default",
}
}
if packet == nil {
packet = report
}
src, dst := d.generateEndpoints(p, sourceID, destID, reverse)
sourceController, destinationController := "", ""
if conn != nil {
sourceController, destinationController = conn.SourceController, conn.DestinationController
}
d.reportFlow(p, src, dst, context, mode, report, packet, sourceController, destinationController)
}
func (d *Datapath) reportUDPAcceptedFlow(p *packet.Packet, conn *connection.UDPConnection, sourceID string, destID string, context *pucontext.PUContext, report *policy.FlowPolicy, packet *policy.FlowPolicy, reverse bool) { // nolint:unparam
src, dst := d.generateEndpoints(p, sourceID, destID, reverse)
sourceController, destinationController := "", ""
if conn != nil {
sourceController, destinationController = conn.SourceController, conn.DestinationController
}
d.reportFlow(p, src, dst, context, "", report, packet, sourceController, destinationController)
}
func (d *Datapath) reportUDPRejectedFlow(p *packet.Packet, conn *connection.UDPConnection, sourceID string, destID string, context *pucontext.PUContext, mode string, report *policy.FlowPolicy, packet *policy.FlowPolicy, reverse bool) { // nolint:unparam
if report == nil {
report = &policy.FlowPolicy{
Action: policy.Reject,
PolicyID: "default",
}
}
if packet == nil {
packet = report
}
src, dst := d.generateEndpoints(p, sourceID, destID, reverse)
sourceController, destinationController := "", ""
if conn != nil {
sourceController, destinationController = conn.SourceController, conn.DestinationController
}
d.reportFlow(p, src, dst, context, mode, report, packet, sourceController, destinationController)
}
func (d *Datapath) reportExternalServiceFlowCommon(context *pucontext.PUContext, report *policy.FlowPolicy, actual *policy.FlowPolicy, app bool, p *packet.Packet, src, dst *collector.EndPoint) {
if app {
// TODO: report.ServiceID ????
src.ID = context.ManagementID()
src.Type = collector.EnpointTypePU
dst.ID = report.ServiceID
dst.Type = collector.EndPointTypeExternalIP
} else {
src.ID = report.ServiceID
src.Type = collector.EndPointTypeExternalIP
dst.ID = context.ManagementID()
dst.Type = collector.EnpointTypePU
}
dropReason := ""
if report.Action.Rejected() || actual.Action.Rejected() {
dropReason = collector.PolicyDrop
}
record := &collector.FlowRecord{
ContextID: context.ID(),
Source: src,
Destination: dst,
DropReason: dropReason,
Action: actual.Action,
Tags: context.Annotations(),
PolicyID: actual.PolicyID,
L4Protocol: p.IPProto(),
Namespace: context.ManagementNamespace(),
Count: 1,
}
if report.ObserveAction.Observed() {
record.ObservedAction = report.Action
record.ObservedPolicyID = report.PolicyID
}
d.collector.CollectFlowEvent(record)
}
func (d *Datapath) reportExternalServiceFlow(context *pucontext.PUContext, report *policy.FlowPolicy, packet *policy.FlowPolicy, app bool, p *packet.Packet) {
src := &collector.EndPoint{
IP: p.SourceAddress().String(),
Port: p.SourcePort(),
}
dst := &collector.EndPoint{
IP: p.DestinationAddress().String(),
Port: p.DestPort(),
}
d.reportExternalServiceFlowCommon(context, report, packet, app, p, src, dst)
}
func (d *Datapath) reportReverseExternalServiceFlow(context *pucontext.PUContext, report *policy.FlowPolicy, packet *policy.FlowPolicy, app bool, p *packet.Packet) {
src := &collector.EndPoint{
IP: p.DestinationAddress().String(),
Port: p.DestPort(),
}
dst := &collector.EndPoint{
IP: p.SourceAddress().String(),
Port: p.SourcePort(),
}
d.reportExternalServiceFlowCommon(context, report, packet, app, p, src, dst)
}
func (d *Datapath) generateEndpoints(p *packet.Packet, sourceID string, destID string, reverse bool) (*collector.EndPoint, *collector.EndPoint) {
src := &collector.EndPoint{
ID: sourceID,
IP: p.SourceAddress().String(),
Port: p.SourcePort(),
Type: collector.EnpointTypePU,
}
dst := &collector.EndPoint{
ID: destID,
IP: p.DestinationAddress().String(),
Port: p.DestPort(),
Type: collector.EnpointTypePU,
}
if reverse {
return dst, src
}
return src, dst
}