-
Notifications
You must be signed in to change notification settings - Fork 51
/
utils.go
155 lines (128 loc) · 4.45 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
package nfqdatapath
import (
"net"
"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) {
if conn != nil {
conn.SetReported(connection.AcceptReported)
}
d.reportFlow(p, sourceID, destID, context, "", report, packet)
}
func (d *Datapath) reportUDPAcceptedFlow(p *packet.Packet, conn *connection.UDPConnection, sourceID string, destID string, context *pucontext.PUContext, report *policy.FlowPolicy, packet *policy.FlowPolicy) {
if conn != nil {
conn.SetReported(connection.AcceptReported)
}
d.reportFlow(p, sourceID, destID, context, "", report, packet)
}
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) {
if conn != nil && mode == collector.PolicyDrop {
conn.SetReported(connection.RejectReported)
}
if report == nil {
report = &policy.FlowPolicy{
Action: policy.Reject,
PolicyID: "default",
}
}
if packet == nil {
packet = report
}
d.reportFlow(p, sourceID, destID, context, mode, report, packet)
}
func (d *Datapath) reportUDPExternalFlow(p *packet.Packet, context *pucontext.PUContext, app bool, report *policy.FlowPolicy, packet *policy.FlowPolicy) {
if report == nil {
report = &policy.FlowPolicy{
Action: policy.Reject,
PolicyID: "default",
ServiceID: "default",
}
}
if packet == nil {
packet = report
}
d.reportExternalServiceFlow(context, report, packet, app, p)
}
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) {
if conn != nil && mode == collector.PolicyDrop {
conn.SetReported(connection.RejectReported)
}
if report == nil {
report = &policy.FlowPolicy{
Action: policy.Reject,
PolicyID: "default",
}
}
if packet == nil {
packet = report
}
d.reportFlow(p, sourceID, destID, context, mode, report, packet)
}
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,
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.DestinationPort,
}
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.DestinationPort,
}
dst := &collector.EndPoint{
IP: p.SourceAddress.String(),
Port: p.SourcePort,
}
d.reportExternalServiceFlowCommon(context, report, packet, app, p, src, dst)
}
func addressMatch(ip net.IP, targets []*net.IPNet) bool {
for _, t := range targets {
if t.Contains(ip) {
return true
}
}
return false
}