/
nflog_linux.go
129 lines (103 loc) · 3.09 KB
/
nflog_linux.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
// +build linux
package nflog
import (
"fmt"
"strings"
"sync"
"github.com/aporeto-inc/netlink-go/nflog"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/policy"
"go.uber.org/zap"
)
type nfLog struct {
getPUInfo GetPUInfoFunc
ipv4groupSource uint16
ipv4groupDest uint16
collector collector.EventCollector
srcNflogHandle nflog.NFLog
dstNflogHandle nflog.NFLog
sync.Mutex
}
// NewNFLogger provides an NFLog instance
func NewNFLogger(ipv4groupSource, ipv4groupDest uint16, getPUInfo GetPUInfoFunc, collector collector.EventCollector) NFLogger {
return &nfLog{
ipv4groupSource: ipv4groupSource,
ipv4groupDest: ipv4groupDest,
collector: collector,
getPUInfo: getPUInfo,
}
}
func (a *nfLog) Start() {
a.Lock()
a.srcNflogHandle, _ = nflog.BindAndListenForLogs([]uint16{a.ipv4groupSource}, 64, a.sourceNFLogsHanlder, a.nflogErrorHandler)
a.dstNflogHandle, _ = nflog.BindAndListenForLogs([]uint16{a.ipv4groupDest}, 64, a.destNFLogsHandler, a.nflogErrorHandler)
a.Unlock()
}
func (a *nfLog) Stop() {
a.Lock()
a.srcNflogHandle.NFlogClose()
a.dstNflogHandle.NFlogClose()
a.Unlock()
}
func (a *nfLog) sourceNFLogsHanlder(buf *nflog.NfPacket, data interface{}) {
record, err := a.recordFromNFLogBuffer(buf, false)
if err != nil {
zap.L().Error("sourceNFLogsHanlder: create flow record", zap.Error(err))
return
}
a.collector.CollectFlowEvent(record)
}
func (a *nfLog) destNFLogsHandler(buf *nflog.NfPacket, data interface{}) {
record, err := a.recordFromNFLogBuffer(buf, true)
if err != nil {
zap.L().Error("destNFLogsHandler: create flow record", zap.Error(err))
return
}
a.collector.CollectFlowEvent(record)
}
func (a *nfLog) nflogErrorHandler(err error) {
zap.L().Error("Error while processing nflog packet", zap.Error(err))
}
func (a *nfLog) recordFromNFLogBuffer(buf *nflog.NfPacket, puIsSource bool) (*collector.FlowRecord, error) {
parts := strings.SplitN(buf.Prefix[:len(buf.Prefix)-1], ":", 3)
if len(parts) != 3 {
return nil, fmt.Errorf("nflog: prefix doesn't contain sufficient information: %s", buf.Prefix)
}
contextID, policyID, extSrvID := parts[0], parts[1], parts[2]
shortAction := string(buf.Prefix[len(buf.Prefix)-1])
puID, tags := a.getPUInfo(contextID)
if puID == "" {
return nil, fmt.Errorf("nflog: unable to find pu ID associated given contexID: %s", contextID)
}
var action policy.ActionType
if shortAction == "a" {
action = policy.Accept
} else {
action = policy.Reject
}
record := &collector.FlowRecord{
ContextID: contextID,
Source: &collector.EndPoint{
IP: buf.SrcIP.String(),
},
Destination: &collector.EndPoint{
IP: buf.DstIP.String(),
Port: uint16(buf.DstPort),
},
PolicyID: policyID,
Tags: tags,
Action: action,
}
if puIsSource {
record.Source.Type = collector.PU
record.Source.ID = puID
record.Destination.Type = collector.Address
record.Destination.ID = extSrvID
} else {
record.Source.Type = collector.Address
record.Source.ID = extSrvID
record.Destination.Type = collector.PU
record.Destination.ID = puID
}
return record, nil
}