-
Notifications
You must be signed in to change notification settings - Fork 51
/
utils.go
193 lines (153 loc) · 6.14 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package nfqdatapath
import (
"go.aporeto.io/enforcerd/trireme-lib/collector"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/connection"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/packet"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/enforcerd/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 | policy.Log,
PolicyID: "local",
}
packet = report
}
sourceController, destinationController := getTCPConnectionInfo(conn)
src, dst := d.generateEndpoints(p, sourceID, destID, reverse)
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 | policy.Log,
PolicyID: "default",
}
}
if packet == nil {
packet = report
}
sourceController, destinationController := getTCPConnectionInfo(conn)
src, dst := d.generateEndpoints(p, sourceID, destID, reverse)
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
sourceController, destinationController := getUDPConnectionInfo(conn)
src, dst := d.generateEndpoints(p, sourceID, destID, reverse)
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 | policy.Log,
PolicyID: "default",
}
}
if packet == nil {
packet = report
}
sourceController, destinationController := getUDPConnectionInfo(conn)
src, dst := d.generateEndpoints(p, sourceID, destID, reverse)
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 {
// If you have an observe policy then its external network gets reported as the dest or src ID.
// Really we should has an oSrc and oDest ID but currently we don't.
src.ID = context.ManagementID()
src.Type = collector.EndPointTypePU
dst.ID = report.ServiceID
dst.Type = collector.EndPointTypeExternalIP
} else {
src.ID = report.ServiceID
src.Type = collector.EndPointTypeExternalIP
dst.ID = context.ManagementID()
dst.Type = collector.EndPointTypePU
}
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,
PolicyID: actual.PolicyID,
L4Protocol: p.IPProto(),
Namespace: context.ManagementNamespace(),
Count: 1,
RuleName: actual.RuleName,
}
if context.Annotations() != nil {
record.Tags = context.Annotations().GetSlice()
}
if report.ObserveAction.Observed() {
record.ObservedAction = report.Action
record.ObservedPolicyID = report.PolicyID
record.ObservedActionType = report.ObserveAction
}
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.EndPointTypePU,
}
dst := &collector.EndPoint{
ID: destID,
IP: p.DestinationAddress().String(),
Port: p.DestPort(),
Type: collector.EndPointTypePU,
}
if src.ID == collector.DefaultEndPoint {
src.Type = collector.EndPointTypeExternalIP
}
if dst.ID == collector.DefaultEndPoint {
dst.Type = collector.EndPointTypeExternalIP
}
if reverse {
return dst, src
}
return src, dst
}
func getTCPConnectionInfo(conn *connection.TCPConnection) (string, string) {
sourceController, destinationController := "", ""
if conn != nil {
sourceController, destinationController = conn.SourceController, conn.DestinationController
}
return sourceController, destinationController
}
func getUDPConnectionInfo(conn *connection.UDPConnection) (string, string) {
sourceController, destinationController := "", ""
if conn != nil {
sourceController, destinationController = conn.SourceController, conn.DestinationController
}
return sourceController, destinationController
}