-
Notifications
You must be signed in to change notification settings - Fork 6
/
nflog.go
283 lines (233 loc) · 5.65 KB
/
nflog.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Package nflog is a pure Go client library for capturing and parsing NFLog messages
*/
package nflog
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"syscall"
)
type NFLog struct {
fd int
seq uint32
c chan Msg
errors chan error
conf *Config
}
func New(conf *Config) (*NFLog, error) {
var err error
err = conf.Validate()
if err != nil {
return nil, err
}
n := &NFLog{c: make(chan Msg), conf: conf}
if conf.Return.Errors {
n.errors = make(chan error)
}
n.fd, err = syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_NETFILTER)
if err != nil {
return nil, err
}
// Send Unbind
err = n.sendNFConfigCmd(NFULNL_CFG_CMD_PF_UNBIND, syscall.AF_INET, 0)
if err != nil {
syscall.Close(n.fd)
return nil, err
}
// Send Bind
err = n.sendNFConfigCmd(NFULNL_CFG_CMD_PF_BIND, syscall.AF_INET, 0)
if err != nil {
syscall.Close(n.fd)
return nil, err
}
for _, g := range conf.Groups {
// Bind to groups
err = n.sendNFConfigCmd(NFULNL_CFG_CMD_BIND, syscall.AF_INET, g)
if err != nil {
syscall.Close(n.fd)
return nil, err
}
// Set CopyMeta only
err = n.sendNFConfigMode(g, conf.CopyRange)
if err != nil {
syscall.Close(n.fd)
return nil, err
}
}
go n.readNFMsg()
return n, nil
}
func (n NFLog) Messages() <-chan Msg {
return n.c
}
func (n NFLog) Errors() <-chan error {
return n.errors
}
func (n *NFLog) sendNFConfigCmd(cmd uint8, family uint8, resId uint16) error {
c := newNFConfigCmd(cmd, family, resId)
c.Header.Seq = n.seq
n.seq++
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, c)
return n.sendNLMsg(buf.Bytes())
}
func (n *NFLog) sendNFConfigMode(resId uint16, copyLen uint16) error {
c := newNFConfigMode(resId, copyLen)
c.Header.Seq = n.seq
n.seq++
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, c)
return n.sendNLMsg(buf.Bytes())
}
func (n *NFLog) sendNLMsg(buf []byte) error {
err := syscall.Sendto(n.fd, buf, 0, &syscall.SockaddrNetlink{Family: syscall.AF_NETLINK})
if err != nil {
return err
}
// Wait reply
reply := make([]byte, 2048)
size, _, err := syscall.Recvfrom(n.fd, reply, 0)
if err != nil {
return err
}
// Parse Message
msg, err := syscall.ParseNetlinkMessage(reply[:size])
if err != nil {
return err
}
if len(msg) != 1 {
return fmt.Errorf("Invalid len: %d\n", len(msg))
}
if msg[0].Header.Type != syscall.NLMSG_ERROR {
return fmt.Errorf("Invalid Msg Type: %d", msg[0].Header.Type)
}
// Read error
var nlerror nlmsgerr
err = binary.Read(bytes.NewReader(msg[0].Data), binary.LittleEndian, &nlerror)
if err != nil {
return err
}
if nlerror.Error != 0 {
return fmt.Errorf("Invalid Error code: %d", nlerror.Error)
}
return nil
}
func (n *NFLog) readNFMsg() {
defer syscall.Close(n.fd)
buffer := make([]byte, 65536)
for {
s, _, err := syscall.Recvfrom(n.fd, buffer, 0)
if err != nil {
if n.conf.Return.Errors {
n.errors <- ReaderError(err.Error())
}
if err == syscall.ENOBUFS {
// Non critical error, continue
continue
}
return
}
err = n.parseNFMsg(buffer[:s])
if err != nil {
if n.conf.Return.Errors {
n.errors <- ParserError(err.Error())
}
}
}
}
func (n *NFLog) parseNFMsg(buffer []byte) error {
for len(buffer) > 0 {
reader := bytes.NewReader(buffer)
// Read header
var header nlmsghdr
binary.Read(reader, binary.LittleEndian, &header)
msgLen := header.Len
if msgLen > uint32(len(buffer)) {
return errors.New("message was truncated")
}
// Check only packets
if header.Type == ((NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_PACKET) {
err := n.parseNFPacket(buffer[16:msgLen])
if err != nil {
return errors.New("failed to parse NFPacket: " + err.Error())
}
}
buffer = buffer[msgLen:]
}
return nil
}
func (n *NFLog) parseNFPacket(buffer []byte) error {
reader := bytes.NewReader(buffer)
// Read Header
var header nflogHeader
binary.Read(reader, binary.LittleEndian, &header)
var m Msg
m.Family = header.Family
m.Group = htons(header.ResId)
var tlvHeader nflogTlv
for reader.Len() != 0 {
err := binary.Read(reader, binary.LittleEndian, &tlvHeader)
if err != nil {
return err
}
payloadLen := tlvHeader.Len - 4
switch tlvHeader.Type {
case NFULA_PREFIX:
payload := make([]byte, align4_16(payloadLen))
reader.Read(payload)
m.Prefix = string(payload[:payloadLen-1]) // Removes NUL Byte
case NFULA_PAYLOAD:
payload := make([]byte, align4_16(payloadLen))
reader.Read(payload)
m.Payload = payload[:payloadLen]
case NFULA_UID:
var uid uint32
binary.Read(reader, binary.BigEndian, &uid)
m.UID = &uid
case NFULA_GID:
var gid uint32
binary.Read(reader, binary.BigEndian, &gid)
m.GID = &gid
case NFULA_IFINDEX_INDEV:
var i uint32
binary.Read(reader, binary.BigEndian, &i)
m.InDev = &i
case NFULA_IFINDEX_OUTDEV:
var o uint32
binary.Read(reader, binary.BigEndian, &o)
m.OutDev = &o
case NFULA_HWADDR:
var addr HwAddr
binary.Read(reader, binary.BigEndian, &addr)
m.HwAddr = &addr
case NFULA_HWTYPE:
var t uint16
binary.Read(reader, binary.BigEndian, &t)
m.MacLayerType = &t
reader.Seek(2, io.SeekCurrent) // Padding
case NFULA_HWHEADER:
payload := make([]byte, align4_16(payloadLen))
reader.Read(payload)
m.MacLayer = payload[:payloadLen]
case NFULA_MARK:
var mark uint32
binary.Read(reader, binary.BigEndian, &mark)
m.Mark = &mark
case NFULA_TIMESTAMP:
var sec int64
var usec int64
binary.Read(reader, binary.BigEndian, &sec)
binary.Read(reader, binary.BigEndian, &usec)
m.Sec = &sec
m.Usec = &usec
default:
reader.Seek(int64(align4_16(payloadLen)), io.SeekCurrent)
}
}
// Send Msg to socket
n.c <- m
return nil
}