/
nfct-create-batch.go
178 lines (152 loc) · 4.48 KB
/
nfct-create-batch.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
package main
/*
#include <stdlib.h>
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nfnetlink_conntrack.h>
#include <linux/netfilter/nf_conntrack_common.h>
#include <linux/netfilter/nf_conntrack_tcp.h>
*/
import "C"
import (
"fmt"
mnl "github.com/chamaken/cgolmnl"
inet "github.com/chamaken/cgolmnl/inet"
"os"
"syscall"
"time"
)
func put_msg(nlh *mnl.Nlmsg, i uint16, seq uint32) {
nlh.PutHeader()
nlh.Type = (C.NFNL_SUBSYS_CTNETLINK << 8) | C.IPCTNL_MSG_CT_NEW
nlh.Flags = C.NLM_F_REQUEST | C.NLM_F_CREATE | C.NLM_F_EXCL | C.NLM_F_ACK
nlh.Seq = seq
nfh := (*Nfgenmsg)(nlh.PutExtraHeader(SizeofNfgenmsg))
nfh.Nfgen_family = C.AF_INET
nfh.Version = C.NFNETLINK_V0
nfh.Res_id = 0
nest1 := nlh.NestStart(C.CTA_TUPLE_ORIG)
nest2 := nlh.NestStart(C.CTA_TUPLE_IP)
nlh.PutU32(C.CTA_IP_V4_SRC, inet.InetAddr("1.1.1.1"))
nlh.PutU32(C.CTA_IP_V4_DST, inet.InetAddr("2.2.2.2"))
nlh.NestEnd(nest2)
nest2 = nlh.NestStart(C.CTA_TUPLE_PROTO)
nlh.PutU8(C.CTA_PROTO_NUM, C.IPPROTO_TCP)
nlh.PutU16(C.CTA_PROTO_SRC_PORT, inet.Htons(i))
nlh.PutU16(C.CTA_PROTO_DST_PORT, inet.Htons(1025))
nlh.NestEnd(nest2)
nlh.NestEnd(nest1)
nest1 = nlh.NestStart(C.CTA_TUPLE_REPLY)
nest2 = nlh.NestStart(C.CTA_TUPLE_IP)
nlh.PutU32(C.CTA_IP_V4_SRC, inet.InetAddr("2.2.2.2"))
nlh.PutU32(C.CTA_IP_V4_DST, inet.InetAddr("1.1.1.1"))
nlh.NestEnd(nest2)
nest2 = nlh.NestStart(C.CTA_TUPLE_PROTO)
nlh.PutU8(C.CTA_PROTO_NUM, C.IPPROTO_TCP)
nlh.PutU16(C.CTA_PROTO_SRC_PORT, inet.Htons(1025))
nlh.PutU16(C.CTA_PROTO_DST_PORT, inet.Htons(i))
nlh.NestEnd(nest2)
nlh.NestEnd(nest1)
nest1 = nlh.NestStart(C.CTA_PROTOINFO)
nest2 = nlh.NestStart(C.CTA_PROTOINFO_TCP)
nlh.PutU8(C.CTA_PROTOINFO_TCP_STATE, C.TCP_CONNTRACK_SYN_SENT)
nlh.NestEnd(nest2)
nlh.NestEnd(nest1)
nlh.PutU32(C.CTA_STATUS, inet.Htonl(C.IPS_CONFIRMED))
nlh.PutU32(C.CTA_TIMEOUT, inet.Htonl(1000))
}
func cb_ctl(nlh *mnl.Nlmsg, data interface{}) (int, syscall.Errno) {
switch nlh.Type {
case C.NLMSG_NOOP:
case C.NLMSG_OVERRUN:
return mnl.MNL_CB_OK, 0
case C.NLMSG_ERROR:
err := (*mnl.Nlmsgerr)(nlh.Payload())
if err.Error != 0 {
errno := -err.Error
fmt.Printf("mssage with seq %d has failed: %s\n", nlh.Seq, syscall.Errno(errno))
}
case C.NLMSG_DONE:
return mnl.MNL_CB_STOP, 0
}
return mnl.MNL_CB_OK, 0
}
func send_batch(nl *mnl.Socket, b *mnl.NlmsgBatch, portid uint32) {
var err error
var epfd int
var event syscall.EpollEvent
var events [1]syscall.EpollEvent
rcv_buf := make([]byte, mnl.MNL_SOCKET_BUFFER_SIZE)
if _, err := nl.Sendto(b.HeadBytes()); err != nil {
fmt.Fprintf(os.Stderr, "mnl_socket_sendto: %s", err)
os.Exit(C.EXIT_FAILURE)
}
if epfd, err = syscall.EpollCreate1(0); err != nil {
fmt.Fprintf(os.Stderr, "EpollCreate1: %s", err)
os.Exit(C.EXIT_FAILURE)
}
defer syscall.Close(epfd)
event.Events = syscall.EPOLLIN
event.Fd = int32(nl.Fd())
if err := syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, int(event.Fd), &event); err != nil {
fmt.Fprintf(os.Stderr, "EpollCtl: %s", err)
os.Exit(C.EXIT_FAILURE)
}
ctl_types := []uint16{C.NLMSG_ERROR}
ret := mnl.MNL_CB_OK
for ret > 0 {
var n int
nevents, err := syscall.EpollWait(epfd, events[:], 0)
if nevents == 0 {
break
}
for n = 0; n < nevents; n++ {
if events[n].Fd == event.Fd {
break
}
}
if n >= nevents {
continue
}
nrecv, err := nl.Recvfrom(rcv_buf)
if err != nil {
fmt.Fprintf(os.Stderr, "mnl_socket_recvfrom: %s\n", err)
os.Exit(C.EXIT_FAILURE)
}
_, err = mnl.CbRun2(rcv_buf[:nrecv], 0, portid, nil, nil, cb_ctl, ctl_types)
}
}
func main() {
var err error
var nl *mnl.Socket
var b *mnl.NlmsgBatch
if nl, err = mnl.NewSocket(C.NETLINK_NETFILTER); err != nil {
fmt.Fprintf(os.Stderr, "mnl_socket_open: %s\n", err)
os.Exit(C.EXIT_FAILURE)
}
defer nl.Close()
if err = nl.Bind(0, mnl.MNL_SOCKET_AUTOPID); err != nil {
fmt.Fprintf(os.Stderr, "mnl_socket_bind: %s\n", err)
os.Exit(C.EXIT_FAILURE)
}
portid := nl.Portid()
snd_buf := make([]byte, mnl.MNL_SOCKET_BUFFER_SIZE*2, mnl.MNL_SOCKET_BUFFER_SIZE*2)
if b, err = mnl.NewNlmsgBatch(snd_buf, mnl.Size_t(mnl.MNL_SOCKET_BUFFER_SIZE)); err != nil {
fmt.Fprintf(os.Stderr, "mnl_nlmsg_batch_start: %s\n", err)
os.Exit(C.EXIT_FAILURE)
}
defer b.Stop()
seq := uint32(time.Now().Unix())
for i := 1024; i < 65535; i++ {
put_msg(b.CurrentNlmsg(), uint16(i), seq+uint32(i)-1024)
if b.Next() {
continue
}
send_batch(nl, b, portid)
b.Reset()
}
if !b.IsEmpty() {
send_batch(nl, b, portid)
}
}