-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
313 lines (283 loc) · 7.92 KB
/
reader.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
// Package ndntdump processes captured NDN traffic.
package ndntdump
import (
"net"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"github.com/usnistgov/ndn-dpdk/core/macaddr"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndn/an"
"github.com/usnistgov/ndn-dpdk/ndn/ndnlayer"
"github.com/usnistgov/ndn-dpdk/ndn/tlv"
"github.com/usnistgov/ndntdump/websocket"
)
var lotsOfZeros [65536]byte
func zeroizeInterestPayload(interest *ndn.Interest) {
copy(interest.AppParameters, lotsOfZeros[:])
copy(interest.SigValue, lotsOfZeros[:])
}
func zeroizeDataPayload(data *ndn.Data) {
copy(data.Content, lotsOfZeros[:])
copy(data.SigValue, lotsOfZeros[:])
}
func saveFlowAddrs[A ~[]byte](flow []byte, dir Direction, src, dst A) []byte {
local, remote := src, dst
if dir == DirectionRX {
local, remote = dst, src
}
flow = append(flow, local...)
flow = append(flow, remote...)
return flow
}
func saveFlowPorts[P ~uint8, N ~uint16](flow []byte, dir Direction, proto P, src, dst N) []byte {
local, remote := src, dst
if dir == DirectionRX {
local, remote = dst, src
}
return append(flow, uint8(proto), uint8(local>>8), uint8(local), uint8(remote>>8), uint8(remote))
}
// Reader reads NDN packets from ZeroCopyPacketDataSource.
type Reader struct {
src gopacket.ZeroCopyPacketDataSource
isLocal func(net.HardwareAddr) bool
tcpPort layers.TCPPort
wssPort layers.TCPPort
anon *Anonymizer
zeroizePayload bool
dlp *gopacket.DecodingLayerParser
dlpTLV *gopacket.DecodingLayerParser
decoded []gopacket.LayerType
eth layers.Ethernet
ip4 layers.IPv4
ip6 layers.IPv6
udp layers.UDP
tcp layers.TCP
tlv ndnlayer.TLV
ndn ndnlayer.NDN
dir Direction
unread []Record
}
// Read reads an NDN packet.
//
// []byte fields within returned Record are valid until next call to this function.
func (r *Reader) Read() (rec Record, e error) {
if len(r.unread) > 0 {
rec = r.unread[0]
r.unread = r.unread[1:]
return
}
RETRY:
rec = Record{}
if rec.Wire, rec.CaptureInfo, e = r.src.ZeroCopyReadPacketData(); e != nil {
return
}
if e = r.dlp.DecodeLayers(rec.Wire, &r.decoded); e != nil {
goto RETRY
}
for _, layerType := range r.decoded {
switch layerType {
case layers.LayerTypeEthernet:
switch {
case macaddr.Equal(r.eth.SrcMAC, r.eth.DstMAC):
if len(r.decoded) >= 3 && r.decoded[2] == layers.LayerTypeTCP {
switch {
case r.tcp.SrcPort == r.tcpPort, r.tcp.SrcPort == r.wssPort:
r.dir = DirectionTX
case r.tcp.DstPort == r.tcpPort, r.tcp.DstPort == r.wssPort:
r.dir = DirectionRX
default:
goto RETRY
}
}
case r.isLocal(r.eth.SrcMAC):
r.dir = DirectionTX
case r.isLocal(r.eth.DstMAC):
r.dir = DirectionRX
default:
goto RETRY
}
r.anon.AnonymizeMAC(r.eth.SrcMAC)
r.anon.AnonymizeMAC(r.eth.DstMAC)
rec.Flow = saveFlowAddrs(make([]byte, 0, 12), r.dir, r.eth.SrcMAC, r.eth.DstMAC)
case layers.LayerTypeIPv4:
r.anon.AnonymizeIP(r.ip4.SrcIP)
r.anon.AnonymizeIP(r.ip4.DstIP)
rec.Flow = saveFlowAddrs(make([]byte, 0, 13), r.dir, r.ip4.SrcIP, r.ip4.DstIP)
case layers.LayerTypeIPv6:
r.anon.AnonymizeIP(r.ip6.SrcIP)
r.anon.AnonymizeIP(r.ip6.DstIP)
rec.Flow = saveFlowAddrs(make([]byte, 0, 37), r.dir, r.ip6.SrcIP, r.ip6.DstIP)
case layers.LayerTypeUDP:
rec.Flow = saveFlowPorts(rec.Flow, r.dir, layers.IPProtocolUDP, r.udp.SrcPort, r.udp.DstPort)
case layers.LayerTypeTCP:
rec.Flow = saveFlowPorts(rec.Flow, r.dir, layers.IPProtocolTCP, r.tcp.SrcPort, r.tcp.DstPort)
switch {
case r.tcp.SrcPort == r.wssPort, r.tcp.DstPort == r.wssPort:
r.readWebSocket(rec.CaptureInfo, rec.Flow)
case r.tcp.SrcPort == r.tcpPort, r.tcp.DstPort == r.tcpPort:
default:
goto RETRY
}
return rec, nil
case ndnlayer.LayerTypeTLV:
rec.Size2 = len(r.tlv.LayerContents())
case ndnlayer.LayerTypeNDN:
if r.readPacket(&rec) {
return rec, nil
}
}
}
goto RETRY
}
func (r *Reader) readWebSocket(ci gopacket.CaptureInfo, flow []byte) {
if len(r.tcp.Payload) == 0 {
return
}
frames, _ := websocket.ExtractBinaryFrames(r.tcp.Payload)
if len(frames) == 0 {
websocket.AnonymizeXForwardedFor(r.tcp.Payload)
return
}
r.unread = make([]Record, 0, len(frames))
for _, f := range frames {
if e := r.dlpTLV.DecodeLayers(f.Payload, &r.decoded); e != nil {
continue
}
rec := Record{CaptureInfo: ci, Flow: flow}
for _, layerType := range r.decoded {
switch layerType {
case ndnlayer.LayerTypeTLV:
rec.Size2 = len(r.tlv.LayerContents())
case ndnlayer.LayerTypeNDN:
if r.readPacket(&rec) {
r.unread = append(r.unread, rec)
}
}
}
}
}
func (r *Reader) readPacket(rec *Record) bool {
pkt := r.ndn.Packet
var pktType PktType
switch {
case pkt == nil:
return false
case pkt.Fragment != nil:
pktType = PktTypeFragment
case pkt.Interest != nil:
pktType = PktTypeInterest
rec.SaveInterest(*pkt.Interest, an.NackNone)
if r.zeroizePayload {
zeroizeInterestPayload(pkt.Interest)
}
case pkt.Data != nil:
pktType = PktTypeData
rec.SaveData(*pkt.Data)
if r.zeroizePayload {
zeroizeDataPayload(pkt.Data)
}
case pkt.Nack != nil:
pktType = PktTypeNack
rec.SaveInterest(pkt.Nack.Interest, pkt.Nack.Reason)
if r.zeroizePayload {
zeroizeInterestPayload(&pkt.Nack.Interest)
}
default:
return false
}
rec.DirType = string(r.dir) + string(pktType)
rec.Timestamp = rec.CaptureInfo.Timestamp.UnixNano()
if frag := pkt.Fragment; frag != nil {
if frag.FragIndex == 0 {
r.readFragment(pkt.Lp, *frag, rec)
}
} else {
switch r.tlv.Element.Type {
case an.TtInterest, an.TtData:
rec.Size3 = r.tlv.Element.Size()
case an.TtLpPacket:
d := tlv.DecodingBuffer(r.tlv.Element.Value)
for _, child := range d.Elements() {
if child.Type == an.TtLpPayload {
rec.Size3 = child.Length()
}
}
}
}
return true
}
func (Reader) readFragment(lpl3 ndn.LpL3, frag ndn.LpFragment, rec *Record) {
var payload incompleteTLV
if _, e := payload.Decode(frag.Payload); e != nil {
return
}
switch payload.Type {
case an.TtInterest:
var interest ndn.Interest
interest.UnmarshalBinary(payload.Value) // ignore error
if lpl3.NackReason == an.NackNone {
rec.DirType += string(PktTypeInterest)
} else {
rec.DirType += string(PktTypeNack)
}
rec.SaveInterest(interest, lpl3.NackReason)
case an.TtData:
var data ndn.Data
data.UnmarshalBinary(payload.Value) // ignore error
rec.DirType += string(PktTypeData)
rec.SaveData(data)
}
rec.Size3 = payload.Size
}
// NewReader creates Reader.
func NewReader(src gopacket.ZeroCopyPacketDataSource, opts ReaderOptions) (r *Reader) {
r = &Reader{
src: src,
isLocal: opts.IsLocal,
tcpPort: layers.TCPPort(opts.TCPPort),
wssPort: layers.TCPPort(opts.WebSocketPort),
anon: opts.Anonymizer,
zeroizePayload: !opts.KeepPayload,
}
if r.wssPort == 0 {
r.wssPort = 9696
}
r.dlp = gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, &r.eth, &r.ip4, &r.ip6, &r.udp, &r.tcp, &r.tlv, &r.ndn)
r.dlp.IgnoreUnsupported = true
r.dlpTLV = gopacket.NewDecodingLayerParser(ndnlayer.LayerTypeTLV, &r.tlv, &r.ndn)
r.dlpTLV.IgnoreUnsupported = true
return r
}
// ReaderOptions passes options to NewReader.
type ReaderOptions struct {
IsLocal func(net.HardwareAddr) bool
TCPPort int
WebSocketPort int
Anonymizer *Anonymizer
KeepPayload bool
}
type incompleteTLV struct {
Size int
Type uint32
Length int
Value []byte
}
func (ele *incompleteTLV) Decode(wire []byte) (rest []byte, e error) {
var typ, length tlv.VarNum
rest, e = typ.Decode(wire)
if e != nil {
return nil, e
}
rest, e = length.Decode(rest)
if e != nil {
return nil, e
}
ele.Type, ele.Length = uint32(typ), int(length)
ele.Size = len(wire) - len(rest) + ele.Length
if len(rest) >= ele.Length {
ele.Value, rest = rest[:ele.Length], rest[ele.Length:]
} else {
ele.Value, rest = rest, nil
}
return rest, nil
}