forked from kraman/go-netfilter-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetfilter.go
193 lines (160 loc) · 4.89 KB
/
netfilter.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
/*
Copyright 2014 Krishna Raman <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Go bindings for libnetfilter_queue
This library provides access to packets in the IPTables netfilter queue (NFQUEUE).
The libnetfilter_queue library is part of the http://netfilter.org/projects/libnetfilter_queue/ project.
*/
package netfilter
/*
#cgo pkg-config: libnetfilter_queue
#cgo CFLAGS: -Wall -Werror -I/usr/include
#cgo LDFLAGS: -L/usr/lib64/
#include "netfilter.h"
*/
import "C"
import (
"code.google.com/p/gopacket"
"code.google.com/p/gopacket/layers"
"fmt"
"unsafe"
)
//Verdict for a packet
type Verdict C.uint
//Verdict + packet for injection
type VerdictPacket struct {
Verdict Verdict
Packet []byte
}
type NFPacket struct {
Packet gopacket.Packet
verdictChannel chan Verdict
verdictModifiedChannel chan VerdictPacket
}
//Set the verdict for the packet AND new packet content for injection
func (p *NFPacket) SetModifiedVerdict(v Verdict, packet []byte) {
p.verdictModifiedChannel <- VerdictPacket{
Verdict: v,
Packet: packet,
}
}
//Set the verdict for the packet
func (p *NFPacket) SetVerdict(v Verdict) {
p.verdictChannel <- v
}
//Set the verdict for the packet
func (p *NFPacket) SetRequeueVerdict(newQueueId uint16) {
v := uint(NF_QUEUE)
q := (uint(newQueueId) << 16)
v = v | q
p.verdictChannel <- Verdict(v)
}
type NFQueue struct {
h *C.struct_nfq_handle
qh *C.struct_nfq_q_handle
fd C.int
packets chan NFPacket
}
const (
AF_INET = 2
NF_DROP Verdict = 0
NF_ACCEPT Verdict = 1
NF_STOLEN Verdict = 2
NF_QUEUE Verdict = 3
NF_REPEAT Verdict = 4
NF_STOP Verdict = 5
NF_DEFAULT_PACKET_SIZE uint32 = 0xffff
)
//Create and bind to queue specified by queueId
func NewNFQueue(queueId uint16, maxPacketsInQueue uint32, packetSize uint32) (*NFQueue, error) {
var nfq = NFQueue{}
var err error
var ret C.int
if nfq.h, err = C.nfq_open(); err != nil {
return nil, fmt.Errorf("Error opening NFQueue handle: %v\n", err)
}
if ret, err = C.nfq_unbind_pf(nfq.h, AF_INET); err != nil || ret < 0 {
return nil, fmt.Errorf("Error unbinding existing NFQ handler from AF_INET protocol family: %v\n", err)
}
if ret, err := C.nfq_bind_pf(nfq.h, AF_INET); err != nil || ret < 0 {
return nil, fmt.Errorf("Error binding to AF_INET protocol family: %v\n", err)
}
nfq.packets = make(chan NFPacket)
if nfq.qh, err = C.CreateQueue(nfq.h, C.u_int16_t(queueId), unsafe.Pointer(&nfq.packets)); err != nil || nfq.qh == nil {
C.nfq_close(nfq.h)
return nil, fmt.Errorf("Error binding to queue: %v\n", err)
}
if ret, err = C.nfq_set_queue_maxlen(nfq.qh, C.u_int32_t(maxPacketsInQueue)); err != nil || ret < 0 {
C.nfq_destroy_queue(nfq.qh)
C.nfq_close(nfq.h)
return nil, fmt.Errorf("Unable to set max packets in queue: %v\n", err)
}
if C.nfq_set_mode(nfq.qh, C.u_int8_t(2), C.uint(packetSize)) < 0 {
C.nfq_destroy_queue(nfq.qh)
C.nfq_close(nfq.h)
return nil, fmt.Errorf("Unable to set packets copy mode: %v\n", err)
}
if nfq.fd, err = C.nfq_fd(nfq.h); err != nil {
C.nfq_destroy_queue(nfq.qh)
C.nfq_close(nfq.h)
return nil, fmt.Errorf("Unable to get queue file-descriptor. %v", err)
}
go nfq.run()
return &nfq, nil
}
//Unbind and close the queue
func (nfq *NFQueue) Close() {
C.nfq_destroy_queue(nfq.qh)
C.nfq_close(nfq.h)
}
//Get the channel for packets
func (nfq *NFQueue) GetPackets() <-chan NFPacket {
return nfq.packets
}
func (nfq *NFQueue) run() {
C.Run(nfq.h, nfq.fd)
}
type VerdictModified C.verdictModified
//export go_callback
func go_callback(queueId C.int, data *C.uchar, length C.int, cb *chan NFPacket) VerdictModified {
xdata := C.GoBytes(unsafe.Pointer(data), length)
packet := gopacket.NewPacket(xdata, layers.LayerTypeIPv4, gopacket.DecodeOptions{
Lazy: true,
NoCopy: true,
SkipDecodeRecovery: false,
})
p := NFPacket{
verdictChannel: make(chan Verdict),
verdictModifiedChannel: make(chan VerdictPacket),
Packet: packet,
}
select {
case (*cb) <- p:
select {
case v := <-p.verdictModifiedChannel:
return VerdictModified{
verdict: C.uint(v.Verdict),
data: (*C.uchar)(unsafe.Pointer(&v.Packet[0])),
length: C.uint(len(v.Packet)),
}
case v := <-p.verdictChannel:
return VerdictModified{
verdict: C.uint(v),
data: nil,
length: 0,
}
}
default:
return VerdictModified{verdict: C.uint(NF_DROP), data: nil, length: 0}
}
}