-
Notifications
You must be signed in to change notification settings - Fork 51
/
nfq_linux.go
360 lines (313 loc) · 11.4 KB
/
nfq_linux.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
355
356
357
358
359
360
// +build linux
package nfqdatapath
// Go libraries
import (
"context"
"fmt"
"strconv"
"time"
nfqueue "go.aporeto.io/netlink-go/nfqueue"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/controller/pkg/connection"
"go.aporeto.io/trireme-lib/controller/pkg/packet"
"go.aporeto.io/trireme-lib/controller/pkg/packettracing"
"go.uber.org/zap"
)
func errorCallback(err error, _ interface{}) {
zap.L().Error("Error while processing packets on queue", zap.Error(err))
}
func networkCallback(packet *nfqueue.NFPacket, d interface{}) {
d.(*Datapath).processNetworkPacketsFromNFQ(packet)
}
func appCallBack(packet *nfqueue.NFPacket, d interface{}) {
d.(*Datapath).processApplicationPacketsFromNFQ(packet)
}
// startNetworkInterceptor will the process that processes packets from the network
// Still has one more copy than needed. Can be improved.
func (d *Datapath) startNetworkInterceptor(ctx context.Context) {
var err error
nfq := make([]nfqueue.Verdict, d.filterQueue.GetNumNetworkQueues())
for i := uint16(0); i < d.filterQueue.GetNumNetworkQueues(); i++ {
// Initialize all the queues
nfq[i], err = nfqueue.CreateAndStartNfQueue(ctx, d.filterQueue.GetNetworkQueueStart()+i, d.filterQueue.GetNetworkQueueSize(), nfqueue.NfDefaultPacketSize, networkCallback, errorCallback, d)
if err != nil {
for retry := 0; retry < 5 && err != nil; retry++ {
nfq[i], err = nfqueue.CreateAndStartNfQueue(ctx, d.filterQueue.GetNetworkQueueStart()+i, d.filterQueue.GetNetworkQueueSize(), nfqueue.NfDefaultPacketSize, networkCallback, errorCallback, d)
<-time.After(3 * time.Second)
}
if err != nil {
zap.L().Fatal("Unable to initialize netfilter queue", zap.Error(err))
}
// Queue started lets start debug loops for capturing packets
}
}
}
// startApplicationInterceptor will create a interceptor that processes
// packets originated from a local application
func (d *Datapath) startApplicationInterceptor(ctx context.Context) {
var err error
nfq := make([]nfqueue.Verdict, d.filterQueue.GetNumApplicationQueues())
for i := uint16(0); i < d.filterQueue.GetNumApplicationQueues(); i++ {
nfq[i], err = nfqueue.CreateAndStartNfQueue(ctx, d.filterQueue.GetApplicationQueueStart()+i, d.filterQueue.GetApplicationQueueSize(), nfqueue.NfDefaultPacketSize, appCallBack, errorCallback, d)
if err != nil {
for retry := 0; retry < 5 && err != nil; retry++ {
nfq[i], err = nfqueue.CreateAndStartNfQueue(ctx, d.filterQueue.GetApplicationQueueStart()+i, d.filterQueue.GetApplicationQueueSize(), nfqueue.NfDefaultPacketSize, appCallBack, errorCallback, d)
<-time.After(3 * time.Second)
}
if err != nil {
zap.L().Fatal("Unable to initialize netfilter queue", zap.Int("QueueNum", int(d.filterQueue.GetNetworkQueueStart()+i)), zap.Error(err))
}
}
}
}
// processNetworkPacketsFromNFQ processes packets arriving from the network in an NF queue
func (d *Datapath) processNetworkPacketsFromNFQ(p *nfqueue.NFPacket) {
// Parse the packet - drop if parsing fails
netPacket, err := packet.New(packet.PacketTypeNetwork, p.Buffer, strconv.Itoa(p.Mark), true)
var processError error
var tcpConn *connection.TCPConnection
var udpConn *connection.UDPConnection
if err != nil {
netPacket.Print(packet.PacketFailureCreate)
} else if netPacket.IPProto == packet.IPProtocolTCP {
tcpConn, processError = d.processNetworkTCPPackets(netPacket)
} else if netPacket.IPProto == packet.IPProtocolUDP {
udpConn, processError = d.ProcessNetworkUDPPacket(netPacket)
} else {
processError = fmt.Errorf("invalid ip protocol: %d", netPacket.IPProto)
}
if processError != nil {
length := uint32(len(p.Buffer))
buffer := p.Buffer
p.QueueHandle.SetVerdict2(uint32(p.QueueHandle.QueueNum), 0, uint32(p.Mark), length, uint32(p.ID), buffer)
if netPacket.IPProto == packet.IPProtocolTCP {
d.collectTCPPacket(&debugpacketmessage{
Mark: p.Mark,
p: netPacket,
tcpConn: tcpConn,
udpConn: nil,
err: processError,
network: true,
})
} else if netPacket.IPProto == packet.IPProtocolUDP {
d.collectUDPPacket(&debugpacketmessage{
Mark: p.Mark,
p: netPacket,
tcpConn: nil,
udpConn: udpConn,
err: processError,
network: true,
})
}
return
}
if netPacket.IPProto == packet.IPProtocolTCP {
// // Accept the packet
buffer := make([]byte, len(netPacket.Buffer)+netPacket.TCPOptionLength()+netPacket.TCPDataLength())
copyIndex := copy(buffer, netPacket.Buffer)
copyIndex += copy(buffer[copyIndex:], netPacket.GetTCPOptions())
copyIndex += copy(buffer[copyIndex:], netPacket.GetTCPData())
p.QueueHandle.SetVerdict2(uint32(p.QueueHandle.QueueNum), 1, uint32(p.Mark), uint32(copyIndex), uint32(p.ID), buffer)
} else {
// Buffer is already modified.
buffer := make([]byte, len(netPacket.Buffer))
copyIndex := copy(buffer, netPacket.Buffer)
p.QueueHandle.SetVerdict2(uint32(p.QueueHandle.QueueNum), 1, uint32(p.Mark), uint32(copyIndex), uint32(p.ID), buffer)
}
if netPacket.IPProto == packet.IPProtocolTCP {
d.collectTCPPacket(&debugpacketmessage{
Mark: p.Mark,
p: netPacket,
tcpConn: tcpConn,
udpConn: nil,
err: nil,
network: true,
})
} else if netPacket.IPProto == packet.IPProtocolUDP {
d.collectUDPPacket(&debugpacketmessage{
Mark: p.Mark,
p: netPacket,
tcpConn: nil,
udpConn: udpConn,
err: nil,
network: true,
})
}
}
// processApplicationPackets processes packets arriving from an application and are destined to the network
func (d *Datapath) processApplicationPacketsFromNFQ(p *nfqueue.NFPacket) {
// Being liberal on what we transmit - malformed TCP packets are let go
// We are strict on what we accept on the other side, but we don't block
// lots of things at the ingress to the network
appPacket, err := packet.New(packet.PacketTypeApplication, p.Buffer, strconv.Itoa(p.Mark), true)
var processError error
var tcpConn *connection.TCPConnection
var udpConn *connection.UDPConnection
if err != nil {
appPacket.Print(packet.PacketFailureCreate)
} else if appPacket.IPProto == packet.IPProtocolTCP {
tcpConn, processError = d.processApplicationTCPPackets(appPacket)
} else if appPacket.IPProto == packet.IPProtocolUDP {
udpConn, processError = d.ProcessApplicationUDPPacket(appPacket)
} else {
processError = fmt.Errorf("invalid ip protocol: %d", appPacket.IPProto)
}
if processError != nil {
length := uint32(len(p.Buffer))
buffer := p.Buffer
p.QueueHandle.SetVerdict2(uint32(p.QueueHandle.QueueNum), 0, uint32(p.Mark), length, uint32(p.ID), buffer)
if appPacket.IPProto == packet.IPProtocolTCP {
d.collectTCPPacket(&debugpacketmessage{
Mark: p.Mark,
p: appPacket,
tcpConn: tcpConn,
udpConn: nil,
err: processError,
network: false,
})
} else if appPacket.IPProto == packet.IPProtocolUDP {
d.collectUDPPacket(&debugpacketmessage{
Mark: p.Mark,
p: appPacket,
tcpConn: nil,
udpConn: udpConn,
err: processError,
network: false,
})
}
return
}
if appPacket.IPProto == packet.IPProtocolTCP {
// Accept the packet
buffer := make([]byte, len(appPacket.Buffer)+appPacket.TCPOptionLength()+appPacket.TCPDataLength())
copyIndex := copy(buffer, appPacket.Buffer)
copyIndex += copy(buffer[copyIndex:], appPacket.GetTCPOptions())
copyIndex += copy(buffer[copyIndex:], appPacket.GetTCPData())
p.QueueHandle.SetVerdict2(uint32(p.QueueHandle.QueueNum), 1, uint32(p.Mark), uint32(copyIndex), uint32(p.ID), buffer)
} else {
buffer := make([]byte, len(appPacket.Buffer))
copyIndex := copy(buffer, appPacket.Buffer)
p.QueueHandle.SetVerdict2(uint32(p.QueueHandle.QueueNum), 1, uint32(p.Mark), uint32(copyIndex), uint32(p.ID), buffer)
}
if appPacket.IPProto == packet.IPProtocolTCP {
d.collectTCPPacket(&debugpacketmessage{
Mark: p.Mark,
p: appPacket,
tcpConn: tcpConn,
udpConn: nil,
err: nil,
network: false,
})
} else if appPacket.IPProto == packet.IPProtocolUDP {
d.collectUDPPacket(&debugpacketmessage{
Mark: p.Mark,
p: appPacket,
tcpConn: nil,
udpConn: udpConn,
err: nil,
network: false,
})
}
}
func (d *Datapath) collectUDPPacket(msg *debugpacketmessage) {
var value interface{}
var err error
report := &collector.PacketReport{}
if msg.udpConn == nil {
if d.puFromIP == nil {
return
}
if value, err = d.packetTracingCache.Get(d.puFromIP.ID()); err != nil {
//not being traced return
return
}
report.Claims = d.puFromIP.Identity().GetSlice()
report.PUID = d.puFromIP.ManagementID()
report.Encrypt = false
} else {
//udpConn is not nil
if value, err = d.packetTracingCache.Get(msg.udpConn.Context.ID()); err != nil {
return
}
report.Encrypt = msg.udpConn.ServiceConnection
report.Claims = msg.udpConn.Context.Identity().GetSlice()
report.PUID = msg.udpConn.Context.ManagementID()
}
if msg.network && !packettracing.IsNetworkPacketTraced(value.(*tracingCacheEntry).direction) {
return
} else if !msg.network && !packettracing.IsApplicationPacketTraced(value.(*tracingCacheEntry).direction) {
return
}
report.Protocol = int(packet.IPProtocolUDP)
report.DestinationIP = msg.p.DestinationAddress.String()
report.SourceIP = msg.p.SourceAddress.String()
report.DestinationPort = int(msg.p.DestinationPort)
report.SourcePort = int(msg.p.SourcePort)
if msg.err != nil {
report.DropReason = msg.err.Error()
report.Event = packettracing.PacketDropped
} else {
report.DropReason = ""
report.Event = packettracing.PacketReceived
}
report.Length = int(msg.p.IPTotalLength)
report.Mark = msg.Mark
report.PacketID, _ = strconv.Atoi(msg.p.ID())
report.TriremePacket = true
d.collector.CollectPacketEvent(report)
}
func (d *Datapath) collectTCPPacket(msg *debugpacketmessage) {
var value interface{}
var err error
report := &collector.PacketReport{}
if msg.tcpConn == nil {
if d.puFromIP == nil {
return
}
if value, err = d.packetTracingCache.Get(d.puFromIP.ID()); err != nil {
//not being traced return
return
}
report.Claims = d.puFromIP.Identity().GetSlice()
report.PUID = d.puFromIP.ManagementID()
report.Encrypt = false
} else {
if value, err = d.packetTracingCache.Get(msg.tcpConn.Context.ID()); err != nil {
//not being traced return
return
}
//tcpConn is not nil
report.Encrypt = msg.tcpConn.ServiceConnection
report.Claims = msg.tcpConn.Context.Identity().GetSlice()
report.PUID = msg.tcpConn.Context.ManagementID()
}
if msg.network && !packettracing.IsNetworkPacketTraced(value.(*tracingCacheEntry).direction) {
return
} else if !msg.network && !packettracing.IsApplicationPacketTraced(value.(*tracingCacheEntry).direction) {
return
}
report.TCPFlags = int(msg.p.TCPFlags)
report.Protocol = int(packet.IPProtocolTCP)
report.DestinationIP = msg.p.DestinationAddress.String()
report.SourceIP = msg.p.SourceAddress.String()
report.DestinationPort = int(msg.p.DestinationPort)
report.SourcePort = int(msg.p.SourcePort)
if msg.err != nil {
report.DropReason = msg.err.Error()
report.Event = packettracing.PacketDropped
} else {
report.DropReason = ""
report.Event = packettracing.PacketReceived
}
report.Length = int(msg.p.IPTotalLength)
report.Mark = msg.Mark
report.PacketID, _ = strconv.Atoi(msg.p.ID())
report.TriremePacket = true
// if tcpConn != nil {
// packetClaims := d.tokenAccessor.ParsePacketToken(tcpConn.AuthInfo, packet.ReadTCPData())
// if packetClaims != nil {
// report.PacketClaims := packetClaims.T.GetSlice()
// }
// }
d.collector.CollectPacketEvent(report)
}