forked from bkartel1/packetd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacketd.go
346 lines (281 loc) · 9.45 KB
/
packetd.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
package main
//#include "common.h"
//#include "netfilter.h"
//#include "conntrack.h"
//#include "netlogger.h"
//#cgo LDFLAGS: -lnetfilter_queue -lnfnetlink -lnetfilter_conntrack -lnetfilter_log
import "C"
import "os"
import "net"
import "time"
import "sync"
import "bufio"
import "unsafe"
import "encoding/binary"
import "github.com/google/gopacket"
import "github.com/google/gopacket/layers"
import "github.com/untangle/packetd/support"
import "github.com/untangle/packetd/example"
import "github.com/untangle/packetd/classify"
import "github.com/untangle/packetd/geoip"
import "github.com/untangle/packetd/certcache"
import "github.com/untangle/packetd/restd"
/*---------------------------------------------------------------------------*/
/*
* The childsync is used to give the main process something to watch while
* waiting for all of the goroutine children to finish execution and cleanup.
* To give C child functions access we export go_child_startup and goodbye
* functions. For children in normal go packages, we pass the WaitGroup
* directly to the goroutine.
*/
var childsync sync.WaitGroup
/*---------------------------------------------------------------------------*/
func main() {
var lastmin int
var counter int
support.Startup()
C.common_startup()
support.LogMessage("Untangle Packet Daemon Version %s\n", "1.00")
go C.netfilter_thread()
go C.conntrack_thread()
go C.netlogger_thread()
// ********** Call all plugin startup functions here
go example.Plugin_Startup(&childsync)
go classify.Plugin_Startup(&childsync)
go geoip.Plugin_Startup(&childsync)
go certcache.Plugin_Startup(&childsync)
// Start REST HTTP daemon
go restd.StartRestDaemon()
// ********** End of plugin startup functions
ch := make(chan string)
go func(ch chan string) {
reader := bufio.NewReader(os.Stdin)
for {
s, err := reader.ReadString('\n')
if err != nil {
close(ch)
return
}
ch <- s
}
close(ch)
}(ch)
support.LogMessage("RUNNING ON CONSOLE - HIT ENTER TO EXIT\n")
stdinloop:
for {
shutdown := C.get_shutdown_flag()
if shutdown != 0 {
break
}
select {
case stdin, ok := <-ch:
if !ok {
break stdinloop
} else {
support.LogMessage("Console input detected - Application shutting down\n")
_ = stdin
break stdinloop
}
case <-time.After(1 * time.Second):
current := time.Now()
if current.Minute() != lastmin {
lastmin = current.Minute()
counter++
support.LogMessage("Calling perodic conntrack dump %d\n", counter)
C.conntrack_dump()
support.CleanConntrackTable()
support.CleanCertificateTable()
}
}
}
// ********** Call all plugin goodbye functions here
go example.Plugin_Goodbye(&childsync)
go classify.Plugin_Goodbye(&childsync)
go geoip.Plugin_Goodbye(&childsync)
go certcache.Plugin_Goodbye(&childsync)
// ********** End of plugin goodbye functions
C.netfilter_goodbye()
C.conntrack_goodbye()
C.netlogger_goodbye()
childsync.Wait()
}
/*---------------------------------------------------------------------------*/
//export go_netfilter_callback
func go_netfilter_callback(mark C.int, data *C.uchar, size C.int) int32 {
// ***** this version creates a Go copy of the buffer = SLOWER
// buffer := C.GoBytes(unsafe.Pointer(data),size)
// ***** this version creates a Go pointer to the buffer = FASTER
buffer := (*[0xFFFF]byte)(unsafe.Pointer(data))[:int(size):int(size)]
// convert the C integer to a Go integer
length := int(size)
// get the existing mark on the packet
var pmark int32 = int32(C.int(mark))
// make a gopacket from the raw packet data
packet := gopacket.NewPacket(buffer, layers.LayerTypeIPv4, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
// get the IPv4 layer
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if ipLayer == nil {
return (pmark)
}
ip := ipLayer.(*layers.IPv4)
var tuple support.Tuple
tuple.Protocol = uint8(ip.Protocol)
tuple.ClientAddr = ip.SrcIP
tuple.ServerAddr = ip.DstIP
// get the TCP layer
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
tcp := tcpLayer.(*layers.TCP)
tuple.ClientPort = uint16(tcp.SrcPort)
tuple.ServerPort = uint16(tcp.DstPort)
}
// get the UDP layer
udpLayer := packet.Layer(layers.LayerTypeUDP)
if udpLayer != nil {
udp := udpLayer.(*layers.UDP)
tuple.ClientPort = uint16(udp.SrcPort)
tuple.ServerPort = uint16(udp.DstPort)
}
// right now we only care about TCP and UDP
if (tcpLayer == nil) && (udpLayer == nil) {
return (pmark)
}
var entry support.SessionEntry
var ok bool
finder := support.Tuple2String(tuple)
/*
* If we already have a session entry update the existing, otherwise
* create a new entry for the table.
*/
if entry, ok = support.FindSessionEntry(finder); ok {
support.LogMessage("SESSION Found %s in table\n", finder)
entry.UpdateCount++
} else {
support.LogMessage("SESSION Adding %s to table\n", finder)
entry.SessionId = support.NextSessionId()
entry.SessionCreation = time.Now()
entry.UpdateCount = 1
}
// TODO - pass the gopacket to the handlers instead of the raw buffer
// ********** Call all plugin netfilter handler functions here
c1 := make(chan int32)
go example.Plugin_netfilter_handler(c1, buffer, length)
c2 := make(chan int32)
go classify.Plugin_netfilter_handler(c2, buffer, length)
c3 := make(chan int32)
go geoip.Plugin_netfilter_handler(c3, buffer, length)
c4 := make(chan int32)
go certcache.Plugin_netfilter_handler(c4, tuple)
// ********** End of plugin netfilter callback functions
// add the mark bits returned from each package handler
for i := 0; i < 2; i++ {
select {
case mark1 := <-c1:
pmark |= mark1
case mark2 := <-c2:
pmark |= mark2
case mark3 := <-c3:
pmark |= mark3
}
}
// return the updated mark to be set on the packet
return (pmark)
}
/*---------------------------------------------------------------------------*/
//export go_conntrack_callback
func go_conntrack_callback(info *C.struct_conntrack_info) {
var tuple support.Tuple
var entry support.ConntrackEntry
var ok bool
tuple.Protocol = uint8(info.orig_proto)
tuple.ClientAddr = make(net.IP, 4)
binary.LittleEndian.PutUint32(tuple.ClientAddr, uint32(info.orig_saddr))
tuple.ClientPort = uint16(info.orig_sport)
tuple.ServerAddr = make(net.IP, 4)
binary.LittleEndian.PutUint32(tuple.ServerAddr, uint32(info.orig_daddr))
tuple.ServerPort = uint16(info.orig_dport)
finder := support.Tuple2String(tuple)
/*
* If we already have a conntrack entry update the existing, otherwise
* create a new entry for the table.
*/
if entry, ok = support.FindConntrackEntry(finder); ok {
support.LogMessage("CONNTRACK Found %s in table\n", finder)
entry.UpdateCount++
} else {
support.LogMessage("CONNTRACK Adding %s to table\n", finder)
entry.SessionId = support.NextSessionId()
entry.SessionCreation = time.Now()
entry.SessionTuple = tuple
entry.UpdateCount = 1
}
oldC2sBytes := entry.C2Sbytes
oldS2cBytes := entry.S2Cbytes
oldTotalBytes := entry.TotalBytes
newC2sBytes := uint64(info.orig_bytes)
newS2cBytes := uint64(info.repl_bytes)
newTotalBytes := (newC2sBytes + newS2cBytes)
diffC2sBytes := (newC2sBytes - oldC2sBytes)
diffS2cBytes := (newS2cBytes - oldS2cBytes)
diffTotalBytes := (newTotalBytes - oldTotalBytes)
// In some cases, specifically UDP, a new session takes the place of an old session with the same tuple.
// In this case the counts go down because its actually a new session. If the total bytes is low, this
// is probably the case so treat it as a new entry.
if (diffC2sBytes < 0) || (diffS2cBytes < 0) {
newC2sBytes = uint64(info.orig_bytes)
diffC2sBytes = newC2sBytes
newS2cBytes = uint64(info.repl_bytes)
diffS2cBytes = newS2cBytes
newTotalBytes = (newC2sBytes + newS2cBytes)
diffTotalBytes = newTotalBytes
return
}
c2sRate := float32(diffC2sBytes / 60)
s2cRate := float32(diffS2cBytes / 60)
totalRate := float32(diffTotalBytes / 60)
entry.C2Sbytes = newC2sBytes
entry.S2Cbytes = newS2cBytes
entry.TotalBytes = newTotalBytes
entry.C2Srate = c2sRate
entry.S2Crate = s2cRate
entry.TotalRate = totalRate
entry.SessionActivity = time.Now()
if info.msg_type == 'D' {
entry.PurgeFlag = true
} else {
entry.PurgeFlag = false
}
support.InsertConntrackEntry(finder, entry)
// ********** Call all plugin conntrack handler functions here
go example.Plugin_conntrack_handler(int(info.msg_type), &entry)
// ********** End of plugin netfilter callback functions
}
/*---------------------------------------------------------------------------*/
//export go_netlogger_callback
func go_netlogger_callback(info *C.struct_netlogger_info) {
var logger support.Logger
logger.Protocol = uint8(info.protocol)
logger.IcmpType = uint16(info.icmp_type)
logger.SrcIntf = uint8(info.src_intf)
logger.DstIntf = uint8(info.dst_intf)
logger.SrcAddr = uint32(info.src_addr)
logger.DstAddr = uint32(info.dst_addr)
logger.SrcPort = uint16(info.src_port)
logger.DstPort = uint16(info.dst_port)
logger.Mark = uint32(info.mark)
logger.Prefix = C.GoString(info.prefix)
// ********** Call all plugin netlogger handler functions here
go example.Plugin_netlogger_handler(&logger)
// ********** End of plugin netlogger callback functions
}
/*---------------------------------------------------------------------------*/
//export go_child_startup
func go_child_startup() {
childsync.Add(1)
}
/*---------------------------------------------------------------------------*/
//export go_child_goodbye
func go_child_goodbye() {
childsync.Done()
}
/*---------------------------------------------------------------------------*/