forked from florianl/go-nfqueue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nfqueue.go
229 lines (198 loc) · 6.5 KB
/
nfqueue.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
package nfqueue
import (
"context"
"encoding/binary"
"log"
"github.com/twistlock/go-nfqueue/internal/unix"
"github.com/twistlock/netlink"
"github.com/pkg/errors"
)
// devNull satisfies io.Writer, in case *log.Logger is not provided
type devNull struct{}
func (devNull) Write(p []byte) (int, error) {
return 0, nil
}
// Close the connection to the netfilter queue subsystem
func (nfqueue *Nfqueue) Close() error {
return nfqueue.Con.Close()
}
// SetVerdictWithMark signals the kernel the next action and the mark for a specified package id
func (nfqueue *Nfqueue) SetVerdictWithMark(id uint32, verdict, mark int) error {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(mark))
attributes, err := netlink.MarshalAttributes([]netlink.Attribute{{
Type: nfQaMark,
Data: buf,
}})
if err != nil {
return err
}
return nfqueue.setVerdict(id, verdict, false, attributes)
}
// SetVerdictModPacket signals the kernel the next action for an altered packet
func (nfqueue *Nfqueue) SetVerdictModPacket(id uint32, verdict int, packet []byte) error {
data, err := netlink.MarshalAttributes([]netlink.Attribute{{
Type: nfQaPayload,
Data: packet,
}})
if err != nil {
return err
}
return nfqueue.setVerdict(id, verdict, false, data)
}
// SetVerdictModPacketWithMark signals the kernel the next action and mark for an altered packet
func (nfqueue *Nfqueue) SetVerdictModPacketWithMark(id uint32, verdict, mark int, packet []byte) error {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(mark))
data, err := netlink.MarshalAttributes([]netlink.Attribute{{
Type: nfQaPayload,
Data: packet,
},
{Type: nfQaMark,
Data: buf}})
if err != nil {
return err
}
return nfqueue.setVerdict(id, verdict, false, data)
}
// SetVerdict signals the kernel the next action for a specified package id
func (nfqueue *Nfqueue) SetVerdict(id uint32, verdict int) error {
return nfqueue.setVerdict(id, verdict, false, []byte{})
}
// SetVerdictBatch signals the kernel the next action for a batch of packages till id
func (nfqueue *Nfqueue) SetVerdictBatch(id uint32, verdict int) error {
return nfqueue.setVerdict(id, verdict, true, []byte{})
}
func (nfqueue *Nfqueue) Unbind() error {
// unbinding existing handler (if any)
seq, err := nfqueue.setConfig(unix.AF_UNSPEC, 0, 0, []netlink.Attribute{
{Type: nfQaCfgCmd, Data: []byte{nfUlnlCfgCmdPfUnbind, 0x0, 0x0, byte(nfqueue.family)}},
})
if err != nil {
return errors.Wrapf(err, "Could not unbind existing handlers (if any)")
}
_, err = nfqueue.setConfig(uint8(unix.AF_UNSPEC), seq, nfqueue.queue, []netlink.Attribute{
{Type: nfQaCfgCmd, Data: []byte{nfUlnlCfgCmdUnbind, 0x0, 0x0, byte(nfqueue.family)}},
})
return err
}
func (nfqueue *Nfqueue) Bind() (seq uint32, err error) {
// unbinding existing handler (if any)
seq, err = nfqueue.setConfig(unix.AF_UNSPEC, 0, 0, []netlink.Attribute{
{Type: nfQaCfgCmd, Data: []byte{nfUlnlCfgCmdPfUnbind, 0x0, 0x0, byte(nfqueue.family)}},
})
if err != nil {
return 0, errors.Wrapf(err, "Could not unbind existing handlers (if any)")
}
// binding to family
_, err = nfqueue.setConfig(unix.AF_UNSPEC, seq, 0, []netlink.Attribute{
{Type: nfQaCfgCmd, Data: []byte{nfUlnlCfgCmdPfBind, 0x0, 0x0, byte(nfqueue.family)}},
})
if err != nil {
return 0, errors.Wrapf(err, "Could not bind to family %d", nfqueue.family)
}
// binding to the requested queue
_, err = nfqueue.setConfig(uint8(unix.AF_UNSPEC), seq, nfqueue.queue, []netlink.Attribute{
{Type: nfQaCfgCmd, Data: []byte{nfUlnlCfgCmdBind, 0x0, 0x0, byte(nfqueue.family)}},
})
if err != nil {
return 0, errors.Wrapf(err, "Could not bind to requested queue %d", nfqueue.queue)
}
// set copy mode and buffer size
data := append(nfqueue.maxPacketLen, nfqueue.copymode)
_, err = nfqueue.setConfig(uint8(unix.AF_UNSPEC), seq, nfqueue.queue, []netlink.Attribute{
{Type: nfQaCfgParams, Data: data},
})
if err != nil {
return 0, err
}
var attrs []netlink.Attribute
if nfqueue.flags[0] != 0 || nfqueue.flags[1] != 0 || nfqueue.flags[2] != 0 || nfqueue.flags[3] != 0 {
// set flags
attrs = append(attrs, netlink.Attribute{Type: nfQaCfgFlags, Data: nfqueue.flags})
attrs = append(attrs, netlink.Attribute{Type: nfQaCfgMask, Data: nfqueue.flags})
}
attrs = append(attrs, netlink.Attribute{Type: nfQaCfgQueueMaxLen, Data: nfqueue.maxQueueLen})
_, err = nfqueue.setConfig(uint8(unix.AF_UNSPEC), seq, nfqueue.queue, attrs)
if err != nil {
return 0, err
}
return seq, nil
}
// Register your own function as callback for a netfilter queue
func (nfqueue *Nfqueue) Register(ctx context.Context, fn HookFunc) error {
seq, err := nfqueue.Bind()
if err != nil {
return err
}
go nfqueue.socketCallback(ctx, fn, seq)
return nil
}
func (nfqueue *Nfqueue) Recv() ([]Attribute, error) {
msgs, err := nfqueue.Con.Receive()
if err != nil {
return nil, err
}
var attrs []Attribute
for _, msg := range msgs {
if msg.Header.Type == netlink.Done {
// this is the last message of a batch
// continue to receive messages
break
}
m, err := parseMsg(nfqueue.logger, msg)
if err != nil {
nfqueue.logger.Printf("Could not parse message: %v", err)
continue
}
attrs = append(attrs, m)
}
return attrs, nil
}
// /include/uapi/linux/netfilter/nfnetlink.h:struct nfgenmsg{} res_id is Big Endian
func putExtraHeader(familiy, version uint8, resid uint16) []byte {
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, resid)
return append([]byte{familiy, version}, buf...)
}
func (nfqueue *Nfqueue) setConfig(afFamily uint8, oseq uint32, resid uint16, attrs []netlink.Attribute) (uint32, error) {
cmd, err := netlink.MarshalAttributes(attrs)
if err != nil {
return 0, err
}
data := putExtraHeader(afFamily, unix.NFNETLINK_V0, resid)
data = append(data, cmd...)
req := netlink.Message{
Header: netlink.Header{
Type: netlink.HeaderType((nfnlSubSysQueue << 8) | nfQnlMsgConfig),
Flags: netlink.Request | netlink.Acknowledge,
Sequence: oseq,
},
Data: data,
}
return nfqueue.execute(req)
}
func (nfqueue *Nfqueue) execute(req netlink.Message) (uint32, error) {
var seq uint32
reply, e := nfqueue.Con.Execute(req)
if e != nil {
return 0, e
}
if e := netlink.Validate(req, reply); e != nil {
return 0, e
}
for _, msg := range reply {
if seq != 0 {
return 0, errors.Wrapf(ErrUnexpMsg, "Number of received messages: %d", len(reply))
}
seq = msg.Header.Sequence
}
return seq, nil
}
func parseMsg(log *log.Logger, msg netlink.Message) (Attribute, error) {
a, err := extractAttributes(log, msg.Data)
if err != nil {
return a, err
}
return a, nil
}