-
Notifications
You must be signed in to change notification settings - Fork 12
/
nfnl_sock.go
354 lines (312 loc) · 8.84 KB
/
nfnl_sock.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package nfnetlink
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"log"
"sync"
"syscall"
"time"
)
var ErrShortResponse = errors.New("Got short response from netlink")
const recvBufferSize = 8192
var readResponseTimeout = 250 * time.Millisecond
type SockFlags int
const (
FlagDebug SockFlags = 1 << iota
FlagAckRequests
FlagLogWarnings
)
func (sf SockFlags) isSet(f SockFlags) bool {
return sf&f == f
}
func (sf *SockFlags) set(f SockFlags) {
*sf |= f
}
func (sf *SockFlags) clear(f SockFlags) {
*sf &= ^f
}
type nlResponseType int
const (
responseAck nlResponseType = iota
responseErr
responseMsg
)
type netlinkResponse struct {
rtype nlResponseType
errno uint32
msg *NfNlMessage
}
type NetlinkSocket struct {
fd int // Socket file descriptor
peer *syscall.SockaddrNetlink // Destination address for sendto()
recvChan chan *NfNlMessage // Channel for transmitting received messages
recvError error // If an error interrupts reception of messages store it here
recvBuffer []byte // Buffer for storing bytes read from socket
seq uint32 // next sequence number to use
flags SockFlags
responseMap map[uint32]chan *netlinkResponse // maps sequence numbers to channels to deliver response message on
lock sync.Mutex // protects responseMap and recvChan
}
// NewNetlinkSocket creates a new NetlinkSocket
func NewNetlinkSocket(bus int) (*NetlinkSocket, error) {
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, bus)
if err != nil {
return nil, err
}
lsa := &syscall.SockaddrNetlink{Family: syscall.AF_NETLINK}
if err := syscall.Bind(fd, lsa); err != nil {
syscall.Close(fd)
return nil, err
}
s := &NetlinkSocket{
fd: fd,
flags: FlagAckRequests,
peer: &syscall.SockaddrNetlink{Family: syscall.AF_NETLINK},
recvBuffer: make([]byte, recvBufferSize),
responseMap: make(map[uint32]chan *netlinkResponse),
}
go s.runReceiveLoop()
return s, nil
}
// SetFlag adds the flag f to the set of enabled flags for this socket
func (s *NetlinkSocket) SetFlag(f SockFlags) {
s.flags.set(f)
}
// ClearFlag removes the flag f from the set of enabled flags for this socket
func (s *NetlinkSocket) ClearFlag(f SockFlags) {
s.flags.clear(f)
}
// Close the socket
func (s *NetlinkSocket) Close() {
syscall.Close(s.fd)
}
// nextSeq returns a new sequence number to use when building a message
func (s *NetlinkSocket) nextSeq() uint32 {
s.seq += 1
if s.seq == 0 {
s.seq = 1
}
return s.seq
}
// Send serializes msg and transmits in on the socket.
func (s *NetlinkSocket) Send(msg *NfNlMessage) error {
msg.Seq = s.nextSeq()
if s.flags.isSet(FlagAckRequests) {
return s.sendWithAck(msg)
}
return s.sendMessage(msg)
}
// sendWithAck is called to send messages when FlagAckRequests is set to handle delivery
// and processing of reponse messages.
func (s *NetlinkSocket) sendWithAck(msg *NfNlMessage) error {
msg.Flags |= syscall.NLM_F_ACK
ch := s.createResponseChannel(msg.Seq)
if err := s.sendMessage(msg); err != nil {
s.removeResponseChannel(msg.Seq, true)
return err
}
return s.readResponse(ch, msg)
}
// readResponse message from the provided channel and convert it into an error return value or
// return nil if the response was an ack
func (s *NetlinkSocket) readResponse(ch chan *netlinkResponse, msg *NfNlMessage) error {
select {
case resp := <-ch:
switch resp.rtype {
case responseAck:
return nil
case responseErr:
return syscall.Errno(resp.errno)
default:
return fmt.Errorf("unexpected response type: %v to message (seq=%d)", resp.rtype, msg.Seq)
}
case <-time.After(readResponseTimeout):
s.removeResponseChannel(msg.Seq, true)
return fmt.Errorf("timeout waiting for expected response to message (seq=%d)", msg.Seq)
}
}
func (s *NetlinkSocket) sendMessage(msg *NfNlMessage) error {
bs := msg.Serialize()
if s.flags.isSet(FlagDebug) {
log.Printf("Send: %v '%s'", msg, hex.EncodeToString(bs))
}
return syscall.Sendto(s.fd, bs, 0, s.peer)
}
// Receive returns a channel to read incoming event messages from.
func (s *NetlinkSocket) Receive() <-chan *NfNlMessage {
s.lock.Lock()
defer s.lock.Unlock()
if s.recvChan != nil {
return s.recvChan
}
s.recvChan = make(chan *NfNlMessage)
return s.recvChan
}
func (s *NetlinkSocket) runReceiveLoop() {
if err := s.receive(); err != nil {
s.recvError = err
if s.recvChan != nil {
close(s.recvChan)
}
}
}
// RecvErr returns an error value if reception of messages ended with
// an error. When the channel returned by Receive() is closed this
// function should be called to determine the error, if any, that occurred.
func (s *NetlinkSocket) RecvErr() error {
return s.recvError
}
// receive reads from the socket, parses messages, and writes each parsed message
// to the recvChan channel. The received buffer is aligned to NLMSG_ALIGNTO before
// parsing.
// It will loop reading and processing messages until an
// error occurs and then return the error.
func (s *NetlinkSocket) receive() error {
for {
n, err := s.fillRecvBuffer()
if err != nil {
return err
}
msgs, err := syscall.ParseNetlinkMessage(s.recvBuffer[:nlmAlignOf(n)])
if err != nil {
return err
}
for _, msg := range msgs {
if err := s.processMessage(msg); err != nil {
return err
}
}
}
}
func (s *NetlinkSocket) processMessage(msg syscall.NetlinkMessage) error {
if msg.Header.Type == syscall.NLMSG_ERROR {
s.processErrorMsg(msg)
return nil
}
nlm, err := s.parseMessage(msg)
if err != nil {
return err
}
s.deliverMessage(nlm)
return nil
}
// deliverMessage sends the message out on the recvChan channel. If the user
// has not called Receive() this channel will not have been created yet and
// the message will be dropped to avoid deadlocking.
func (s *NetlinkSocket) deliverMessage(nlm *NfNlMessage) {
s.lock.Lock()
if s.recvChan == nil {
s.warn("Dropping message because nobody is listening for received messages")
return
}
s.lock.Unlock()
s.recvChan <- nlm
}
func (s *NetlinkSocket) warn(format string, v ...interface{}) {
if s.flags.isSet(FlagLogWarnings) {
log.Println(fmt.Sprintf(format, v...))
}
}
func (s *NetlinkSocket) processErrorMsg(msg syscall.NetlinkMessage) {
if len(msg.Data) < 4 {
s.warn("Netlink error message received with short (%d byte) body", len(msg.Data))
return
}
errno := readErrno(msg.Data)
rtype := responseAck
if errno != 0 {
rtype = responseErr
}
m := s.parseMessageFromBytes(msg.Data[4:])
s.sendResponse(msg.Header.Seq, rtype, errno, m)
}
func (s *NetlinkSocket) parseMessageFromBytes(data []byte) *NfNlMessage {
if len(data) < syscall.NLMSG_HDRLEN+NFGEN_HDRLEN {
return nil
}
msgs, err := syscall.ParseNetlinkMessage(data)
if err != nil {
s.warn("Error parsing netlink message inside error message: %v", err)
return nil
}
if len(msgs) != 1 {
s.warn("Expected 1 message got %d", len(msgs))
return nil
}
m := s.NewNfNlMsg()
if err := m.parse(bytes.NewReader(msgs[0].Data), msgs[0].Header); err != nil {
s.warn("Error parsing message %v", err)
return nil
}
return m
}
func (s *NetlinkSocket) sendResponse(seq uint32, rtype nlResponseType, errno uint32, msg *NfNlMessage) {
ch := s.removeResponseChannel(seq, false)
if ch == nil {
s.warn("No response channel found for seq %d", seq)
return
}
ch <- &netlinkResponse{
rtype: rtype,
errno: errno,
msg: msg,
}
close(ch)
}
func (s *NetlinkSocket) removeResponseChannel(seq uint32, closeChan bool) chan *netlinkResponse {
s.lock.Lock()
defer s.lock.Unlock()
ch, ok := s.responseMap[seq]
if !ok {
return nil
}
delete(s.responseMap, seq)
if closeChan {
close(ch)
return nil
}
return ch
}
func (s *NetlinkSocket) createResponseChannel(seq uint32) chan *netlinkResponse {
ch := make(chan *netlinkResponse)
s.lock.Lock()
defer s.lock.Unlock()
if s.responseMap[seq] != nil {
close(s.responseMap[seq])
}
s.responseMap[seq] = ch
return ch
}
func readErrno(data []byte) uint32 {
errno := int32(native.Uint32(data))
return uint32(-errno)
}
// fillRecvBuffer reads from the socket into recvBuffer and returns the number
// of bytes read. If less than NLMSG_HDRLEN bytes are read, ErrShortResponse
// is returned as an error.
func (s *NetlinkSocket) fillRecvBuffer() (int, error) {
n, _, err := syscall.Recvfrom(s.fd, s.recvBuffer, 0)
if err != nil {
return 0, err
}
if n < syscall.NLMSG_HDRLEN {
return 0, ErrShortResponse
}
return n, nil
}
// parseMessage converts a syscall.NetlinkMessage into a NfNlMessage by
// parsing the Data byte slice into a NfGenHdr and zero or more attribute
// instances.
func (s *NetlinkSocket) parseMessage(msg syscall.NetlinkMessage) (*NfNlMessage, error) {
m := s.NewNfNlMsg()
r := bytes.NewReader(msg.Data)
if err := m.parse(r, msg.Header); err != nil {
return nil, err
}
if s.flags.isSet(FlagDebug) {
log.Printf("Recv: %v\n", m)
}
return m, nil
}