-
Notifications
You must be signed in to change notification settings - Fork 51
/
nflog_common.go
181 lines (155 loc) · 5.05 KB
/
nflog_common.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
package nflog
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/controller/pkg/counters"
"go.aporeto.io/trireme-lib/controller/pkg/packet"
"go.aporeto.io/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/trireme-lib/policy"
"go.uber.org/zap"
)
// NFLogger provides an interface for NFLog
type NFLogger interface {
Run(ctx context.Context)
}
// GetPUContextFunc provides PU information given the id
type GetPUContextFunc func(hash string) (*pucontext.PUContext, error)
func recordCounters(protocol uint8, dstport uint16, srcport uint16, pu *pucontext.PUContext, puIsSource bool) {
switch protocol {
case packet.IPProtocolTCP:
pu.Counters().IncrementCounter(counters.ErrDroppedTCPPackets)
case packet.IPProtocolUDP:
pu.Counters().IncrementCounter(counters.ErrDroppedUDPPackets)
if puIsSource {
switch dstport {
case 53:
pu.Counters().IncrementCounter(counters.ErrDroppedDNSPackets)
case 67, 68:
pu.Counters().IncrementCounter(counters.ErrDroppedDHCPPackets)
case 123:
pu.Counters().IncrementCounter(counters.ErrDroppedNTPPackets)
}
} else {
switch srcport {
case 53:
pu.Counters().IncrementCounter(counters.ErrDroppedDNSPackets)
case 67, 68:
pu.Counters().IncrementCounter(counters.ErrDroppedDHCPPackets)
case 123:
pu.Counters().IncrementCounter(counters.ErrDroppedNTPPackets)
}
}
case packet.IPProtocolICMP:
pu.Counters().IncrementCounter(counters.ErrDroppedICMPPackets)
}
}
func recordDroppedPacket(payload []byte, protocol uint8, srcIP, dstIP net.IP, srcPort, dstPort uint16, pu *pucontext.PUContext, puIsSource bool) (*collector.PacketReport, error) {
report := &collector.PacketReport{}
report.PUID = pu.ManagementID()
report.Namespace = pu.ManagementNamespace()
ipPacket, err := packet.New(packet.PacketTypeNetwork, payload, "", false)
if err == nil {
report.Length = int(ipPacket.GetIPLength())
report.PacketID, _ = strconv.Atoi(ipPacket.ID())
} else {
zap.L().Debug("payload not valid", zap.Error(err))
return nil, err
}
recordCounters(protocol, dstPort, srcPort, pu, puIsSource)
if protocol == packet.IPProtocolTCP || protocol == packet.IPProtocolUDP {
report.SourcePort = int(srcPort)
report.DestinationPort = int(dstPort)
}
if protocol == packet.IPProtocolTCP {
report.TCPFlags = int(ipPacket.GetTCPFlags())
}
report.Protocol = int(protocol)
report.DestinationIP = dstIP.String()
report.SourceIP = srcIP.String()
report.TriremePacket = false
report.DropReason = collector.PacketDrop
if payload == nil {
report.Payload = []byte{}
return report, nil
}
if len(payload) <= 64 {
report.Payload = make([]byte, len(payload))
copy(report.Payload, payload)
} else {
report.Payload = make([]byte, 64)
copy(report.Payload, payload[0:64])
}
return report, nil
}
func recordFromNFLogData(payload []byte, prefix string, protocol uint8, srcIP, dstIP net.IP, srcPort, dstPort uint16, getPUContext GetPUContextFunc, puIsSource bool) (*collector.FlowRecord, *collector.PacketReport, error) {
var packetReport *collector.PacketReport
var err error
// `hashID:policyID:extServiceID:action`
parts := strings.SplitN(prefix, ":", 4)
if len(parts) != 4 {
return nil, nil, fmt.Errorf("nflog: prefix doesn't contain sufficient information: %s", prefix)
}
hashID, policyID, extServiceID, encodedAction := parts[0], parts[1], parts[2], parts[3]
pu, err := getPUContext(hashID)
if err != nil {
return nil, nil, err
}
if encodedAction == "10" {
packetReport, err = recordDroppedPacket(payload, protocol, srcIP, dstIP, srcPort, dstPort, pu, puIsSource)
return nil, packetReport, err
}
action, _, err := policy.EncodedStringToAction(encodedAction)
if err != nil {
return nil, packetReport, fmt.Errorf("nflog: unable to decode action for context id: %s (%s)", pu.ID(), encodedAction)
}
dropReason := ""
if action.Rejected() {
dropReason = collector.PolicyDrop
}
// point fix for now.
var destination *collector.EndPoint
if protocol == packet.IPProtocolUDP || protocol == packet.IPProtocolTCP {
destination = &collector.EndPoint{
IP: dstIP.String(),
Port: dstPort,
}
} else {
destination = &collector.EndPoint{
IP: dstIP.String(),
}
}
record := &collector.FlowRecord{
ContextID: pu.ID(),
Source: &collector.EndPoint{
IP: srcIP.String(),
},
Destination: destination,
DropReason: dropReason,
PolicyID: policyID,
Tags: pu.Annotations().Copy(),
Action: action,
L4Protocol: protocol,
Namespace: pu.ManagementNamespace(),
Count: 1,
}
if action.Observed() {
record.ObservedAction = action
record.ObservedPolicyID = policyID
}
if puIsSource {
record.Source.Type = collector.EnpointTypePU
record.Source.ID = pu.ManagementID()
record.Destination.Type = collector.EndPointTypeExternalIP
record.Destination.ID = extServiceID
} else {
record.Source.Type = collector.EndPointTypeExternalIP
record.Source.ID = extServiceID
record.Destination.Type = collector.EnpointTypePU
record.Destination.ID = pu.ManagementID()
}
return record, packetReport, nil
}