-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimplemux.lua
480 lines (367 loc) · 18.7 KB
/
simplemux.lua
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
-- lua dissector for Simplemux
-- Pending: get the length from the previous header, instead of getting it from the length
-- of the buffer. This would avoid problems with e.g. PRP (it is a trailer at the end of the frame)
local debug = 0 -- set this to 1 if you want debug information
local simplemux = Proto("simplemux", "Simplemux packet/frame multiplexer");
-- declare the value strings for the field 'Protocol'
local protocol_types = {[4] = "IPv4",
[143] = "Ethernet",
[142] = "ROHC" }
-- declare the field 'protocol type'
local protocol_type = ProtoField.uint8("simplemux.Protocol", "Protocol", base.DEC, protocol_types)
-- define the field structure of the Simplemux header
-- as it is variable, we only include the fixed part now ('Protocol' field)
simplemux.fields = { protocol_type }
-- add more fields: SPB, LXT and LEN
-- based on https://stackoverflow.com/questions/51248914/how-to-handle-bit-fields-in-wireshark-lua-dissector
-- SPB
local singleProtocolOptions = {
[0] = "Each multiplexed packet/frame has its protocol ID",
[1] = "All the multiplexed packets/frames belong to the same protocol"
}
-- this prints '1... ....' or '0... ....' depending on the value of the first bit
simplemux.fields.first_header_first_bit = ProtoField.uint8("Single_Protocol_Bit", "Single Protocol Bit", base.DEC, singleProtocolOptions, 0x80)
-- this prints '1... .... .... ....' or '0... .... .... ....' depending on the value of the first bit
simplemux.fields.first_header_first_bit_16 = ProtoField.uint16("Single_Protocol_Bit", "Single Protocol Bit", base.DEC, singleProtocolOptions, 0x8000)
-- LXT
local lengthExtensionOptionsFirstHeader = {
[0] = "Packet/frame length in 6 bits",
[1] = "Packet/frame length in 13 bits"
}
-- this prints '.1.. .....' or '.0.. ....' depending on the value of the second bit
simplemux.fields.first_header_second_bit = ProtoField.uint8("Length_Extenxsion_First_Header", "Length extension", base.DEC, lengthExtensionOptionsFirstHeader, 0x40)
-- this prints '.1.. ..... .... ....' or '.0.. .... .... ....' depending on the value of the second bit
simplemux.fields.first_header_second_bit_16 = ProtoField.uint16("Length_Extenxsion_First_Header", "Length extension", base.DEC, lengthExtensionOptionsFirstHeader, 0x4000)
local lengthExtensionOptionsNonFirstHeader = {
[0] = "Packet/frame length in 7 bits",
[1] = "Packet/frame length in 14 bits"
}
-- this prints '1... ....' or '0... ....' depending on the value of the first bit
simplemux.fields.non_first_header_first_bit = ProtoField.uint8("Length_Extension_non-First_header", "Length extension", base.DEC, lengthExtensionOptionsNonFirstHeader, 0x80)
-- this prints '1... .... .... ....' or '0... .... .... ....' depending on the value of the first bit
simplemux.fields.non_first_header_first_bit_16 = ProtoField.uint16("Length_Extension_non-First_header", "Length extension", base.DEC, lengthExtensionOptionsNonFirstHeader, 0x8000)
local lengthExtensionOptionsSecondByte = {
[0] = "No more bytes for length",
[1] = "Third byte for length (not implemented)"
}
-- this prints '.... .... 0... ....' or '.... .... 1... ....' depending on the value of the first bit
simplemux.fields.first_header_nineth_bit = ProtoField.uint16("Second_Length_Extenxsion", "Second length extension", base.DEC, lengthExtensionOptionsSecondByte, 0x0080)
-- Length
-- this prints '..101010', i.e. the value of the six less-significant bits
simplemux.fields.first_header_third_to_eighth_bits = ProtoField.uint8("Length_6bits", "Length", base.DEC, null, 0x3F)
-- this prints '..101010 .0101010', i.e. the value of the seven less-significant bits
simplemux.fields.first_header_third_to_sixteenth_bits = ProtoField.uint16("Length_13bits", "Length", base.DEC, null, 0x3F7F)
-- this prints '.0101010', i.e. the value of the seven less-significant bits
simplemux.fields.non_first_header_second_to_eighth_bits = ProtoField.uint8("Length_7bits", "Length", base.DEC, null, 0x7F)
-- this prints '.0101010 .0101010', i.e. the bits where the length is expressed
simplemux.fields.non_first_header_second_to_sixteenth_bits = ProtoField.uint16("Length_14bits", "Length", base.DEC, null, 0x7F7F)
-- define this in order to show the bytes of the Simplemux payload
simplemux.fields.bytes = ProtoField.bytes("simplemux.bytes", "Simplemux payload")
local data_dis = Dissector.get("data")
-- dissector function
function simplemux.dissector(buf, pkt, tree)
if (debug == 1 ) then
print()
end
-- I check if there is a single protocol, i.e. the Protocol field is only present
-- in the first simplemux separator
local singleProtocol = 0
-- first byte of the first simplemux separator
-- Most significant bit: SPB
-- second bit: LXT
-- six bits: part of the LENGTH
-- this is a way to get the most significant bit
--singleProtocol = ( buf(0,1):uint() - ( buf(0,1):uint() % 128 ) ) / 128
-- this is another way (see http://lua-users.org/wiki/BitwiseOperators)
-- bit.band() makes a bitwise AND, in this case with 0x80 (= 1000 0000)
-- bit.rshift() moves the bits to the right n positions
singleProtocol = bit.rshift(bit.band( buf(0,1):uint(), 0x80 ), 7)
-- variable to store the offset: positions I have advanced
local offset = 0
local packetNumber = 0
while (offset < buf:len()) do
-- ************************ first separator ********************
if (packetNumber == 0) then
-- first byte of the first simplemux separator
-- Most significant bit: SPB
-- second bit: LXT
-- six bits: part of the LENGTH
-- this is a way to get the most significant bit
-- SPB = ( buf(offset,1):uint() - ( buf(offset,1):uint() % 128 ) ) / 128
SPB = singleProtocol
-- this is a way to get the second bit
-- local value = buf(0,1):uint()
-- remove the most significant bit
-- if SPB == 1 then
-- value = value - 128
-- end
--LXT = ( value - (value % 64 ) ) / 64
LXT = bit.rshift(bit.band( buf(0,1):uint(), 0x40 ), 6)
-- check if the length has 1 or 2 bytes (depending on LXT, second bit)
if LXT == 0 then
-- the simplemux separator is 2 bytes long:
-- first byte: SPB-LXT-LEN (6 bits)
-- second byte: protocol
-- create the Simplemux protocol tree item
-- the second argument of 'buf' means the number of bytes that are considered
--a part of simplemux
local subtree = tree:add(simplemux, buf(offset,2))
-- the length field is 6 bits long
-- LEN = buf(offset,1):uint() % 64
-- make a bitwise AND with 0x3F ( = 0011 1111)
LEN = bit.band( buf(offset,1):uint(), 0x3F)
if (debug == 1 ) then
print(" offset: " .. offset .. " LEN: " .. LEN)
print(" packet " .. packetNumber .. " the length field is 6 bits long")
end
-- first byte (including SPB, LXT and LEN)
-- add SPB
subtree:add(simplemux.fields.first_header_first_bit, buf(offset,1))
-- add LXT
subtree:add(simplemux.fields.first_header_second_bit, buf(offset,1))
-- add length
subtree:add(simplemux.fields.first_header_third_to_eighth_bits, buf(offset,1))
offset = offset + 1
-- second byte: 'Protocol' field
Protocol = buf(offset,1):uint()
subtree:add(protocol_type, buf(offset,1))
-- add the Protocol length to the offset
offset = offset + 1
-- add the content
simplemux_payload = buf(offset,LEN)
subtree:add(simplemux.fields.bytes, simplemux_payload)
if Protocol == 4 then
Dissector.get("ip"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 143, there is an Eth frame inside
if Protocol == 143 then
Dissector.get("eth_withoutfcs"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 142, there is a ROHC compressed packet inside
if Protocol == 142 then
Dissector.get("rohc"):call(buf(offset):tvb(), pkt, subtree)
end
offset = offset + LEN
else -- LXT == 1
-- the simplemux separator is 3 bytes long:
-- first byte: SPB-LXT-LEN1
-- second byte: LEN2
-- third byte: protocol
if (debug == 1 ) then
print(" packet " .. packetNumber .. " the length field is 13 bits long")
end
-- the length field is 14 bits long
-- create the Simplemux protocol tree item
-- the second argument of 'buf' means the number of bytes that are considered
-- a part of simplemux
local subtree = tree:add(simplemux, buf(offset,3))
-- first byte (including SPB, LXT and part of LEN)
-- second byte (including LXT and the rest of LEN)
-- the length is between the first and the second bytes
-- 6 bits come from the first byte (I remove the two most significant ones)
-- 7 bits come from the second byte (I remove the most significant one)
-- LEN = ((buf(offset,1):uint() % 64 ) * 128 ) + (buf(offset + 1,1):uint() % 128)
LEN = (bit.band( buf(offset,1):uint(), 0x3F) * 128 ) + (bit.band( buf(offset + 1,1):uint(), 0x7F))
if (debug == 1 ) then
print(" offset: " .. offset .. " LEN: " .. LEN)
end
-- first byte (including SPB, LXT and part of LEN)
-- second byte (including LXT and the rest of LEN)
-- add SPB
subtree:add(simplemux.fields.first_header_first_bit_16, buf(offset,2))
-- add LXT
subtree:add(simplemux.fields.first_header_second_bit_16, buf(offset,2))
-- add Length
subtree:add(simplemux.fields.first_header_third_to_sixteenth_bits, buf(offset,2))
-- add second LXT
subtree:add(simplemux.fields.first_header_nineth_bit, buf(offset,2))
offset = offset + 2
-- last byte: Protocol field
Protocol = buf(offset,1):uint()
subtree:add(protocol_type, buf(offset,1))
offset = offset + 1
-- add the content
simplemux_payload = buf(offset,LEN)
subtree:add(simplemux.fields.bytes, simplemux_payload)
if Protocol == 4 then
Dissector.get("ip"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 143, there is an Eth frame inside
if Protocol == 143 then
Dissector.get("eth_withoutfcs"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 142, there is a ROHC compressed packet inside
if Protocol == 142 then
Dissector.get("rohc"):call(buf(offset):tvb(), pkt, subtree)
end
offset = offset + LEN
end
else
-- ************************ non-first separator ********************
-- first byte of a non-first simplemux separator
-- Most significant bit: LXT
-- seven bits: part of the LENGTH
-- this is a way to get the most significant bit
-- LXT = ( buf(offset,1):uint() - ( buf(offset,1):uint() % 128 ) ) / 128
LXT = bit.rshift(bit.band( buf(offset,1):uint(), 0x80 ), 7)
-- check if the length has 1 or 2 bytes (depending on LXT)
if LXT == 0 then
-- the simplemux separator has this structure:
-- first byte: LXT-LEN
-- OPTIONAL: second byte: protocol
-- the length field is 7 bits long
-- LEN = buf(offset,1):uint() % 128
-- make a bitwise AND with 0x7F ( = 0111 1111)
LEN = bit.band( buf(offset,1):uint(), 0x7F)
if (debug == 1 ) then
print(" packet " .. packetNumber .. " the length field is 7 bits long")
print(" offset: " .. offset .. " LEN: " .. LEN)
end
if (singleProtocol == 0) then
-- the simplemux separator DOES have a 'Protocol' field
-- create the Simplemux protocol tree item
-- the second argument of 'buf' means the number of bytes that are considered
-- a part of simplemux
local subtree = tree:add(simplemux, buf(offset,2))
-- first byte (including LXT and LEN)
-- add LXT
subtree:add(simplemux.fields.non_first_header_first_bit, buf(offset,1))
-- add length
subtree:add(simplemux.fields.non_first_header_second_to_eighth_bits, buf(offset,1))
offset = offset + 1
-- last byte: 'Protocol' field
Protocol = buf(offset,1):uint()
subtree:add(protocol_type, buf(offset,1))
-- add the Protocol length to the offset
offset = offset + 1
-- add the content
simplemux_payload = buf(offset,LEN)
subtree:add(simplemux.fields.bytes, simplemux_payload)
if Protocol == 4 then
Dissector.get("ip"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 143, there is an Eth frame inside
if Protocol == 143 then
Dissector.get("eth_withoutfcs"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 142, there is a ROHC compressed packet inside
if Protocol == 142 then
Dissector.get("rohc"):call(buf(offset):tvb(), pkt, subtree)
end
offset = offset + LEN
else
-- the simplemux separator does NOT have a 'Protocol' field
-- create the Simplemux protocol tree item
-- the second argument of 'buf' means the number of bytes that are considered
-- a part of simplemux
local subtree = tree:add(simplemux, buf(offset,1))
-- first byte (including LXT and LEN)
-- add LXT
subtree:add(simplemux.fields.non_first_header_first_bit, buf(offset,1))
-- add length
subtree:add(simplemux.fields.non_first_header_second_to_eighth_bits, buf(offset,1))
offset = offset + 1
-- add the content
simplemux_payload = buf(offset,LEN)
subtree:add(simplemux.fields.bytes, simplemux_payload)
if Protocol == 4 then
Dissector.get("ip"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 143, there is an Eth frame inside
if Protocol == 143 then
Dissector.get("eth_withoutfcs"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 142, there is a ROHC compressed packet inside
if Protocol == 142 then
Dissector.get("rohc"):call(buf(offset):tvb(), pkt, subtree)
end
offset = offset + LEN
end
else -- LXT == 1
-- the simplemux separator has this structure:
-- first byte: LXT-LEN1
-- second byte: LEN2
-- OPTIONAL: second byte: protocol
if (debug == 1 ) then
print(" packet " .. packetNumber .. " the length field is 14 bits long")
end
-- the length field is 15 bits long
-- second byte (including LXT and the rest of LEN)
-- the length is between the first and the second bytes
-- 7 bits come from the first byte (I remove the two most significant ones)
-- 7 bits come from the second byte (I remove the most significant one)
--LEN = ((buf(offset,1):uint() % 128 ) * 128 )+ (buf(offset + 1,1):uint() % 128)
LEN = (bit.band( buf(offset,1):uint(), 0x7F) * 128 ) + (bit.band( buf(offset + 1,1):uint(), 0x7F))
if (debug == 1 ) then
print(" offset: " .. offset .. " LEN: " .. LEN)
end
if (singleProtocol == 0) then
-- the simplemux separator DOES have a 'Protocol' field
-- create the Simplemux protocol tree item
-- the second argument of 'buf' means the number of bytes that are considered
-- a part of simplemux
local subtree = tree:add(simplemux, buf(offset,3))
-- first and second bytes (including LXT and LEN)
-- add LXT
subtree:add(simplemux.fields.non_first_header_first_bit_16, buf(offset,2))
-- add length
subtree:add(simplemux.fields.non_first_header_second_to_sixteenth_bits, buf(offset,2))
-- add second LXT
subtree:add(simplemux.fields.first_header_nineth_bit, buf(offset,2))
offset = offset + 2
-- last byte: 'Protocol' field
Protocol = buf(offset,1):uint()
subtree:add(protocol_type, buf(offset,1))
-- add the Protocol length to the offset
offset = offset + 1
-- add the content
simplemux_payload = buf(offset,LEN)
subtree:add(simplemux.fields.bytes, simplemux_payload)
if Protocol == 4 then
Dissector.get("ip"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 143, there is an Eth frame inside
if Protocol == 143 then
Dissector.get("eth_withoutfcs"):call(buf(offset):tvb(), pkt, subtree)
end
-- if Protocol is 142, there is a ROHC compressed packet inside
if Protocol == 142 then
Dissector.get("rohc"):call(buf(offset):tvb(), pkt, subtree)
end
offset = offset + LEN
else
-- the simplemux separator does NOT have a 'Protocol' field
-- create the Simplemux protocol tree item
-- the second argument of 'buf' means the number of bytes that are considered
-- a part of simplemux
local subtree = tree:add(simplemux, buf(offset,2))
-- first and second bytes (including LXT and LEN)
-- add LXT
subtree:add(simplemux.fields.non_first_header_first_bit_16, buf(offset,2))
-- add length
subtree:add(simplemux.fields.non_first_header_second_to_sixteenth_bits, buf(offset,2))
offset = offset + 2
-- add the content
simplemux_payload = buf(offset,LEN)
subtree:add(simplemux.fields.bytes, simplemux_payload)
offset = offset + LEN
end
end
end
packetNumber = packetNumber + 1
if (debug == 1 ) then
print (" total bufLen: " .. buf:len() .. ". LEN: " .. LEN .. ". offset: " .. offset .. ". packets: " .. packetNumber)
end
end -- end while
end -- end of function simplemux.dissector()
-- load the UDP port table
local udp_encap_table = DissectorTable.get("udp.port")
local tcp_encap_table = DissectorTable.get("tcp.port")
local ip_encap_table = DissectorTable.get("ip.proto")
-- register the protocol to port 55555
-- this is needed in Transport mode
udp_encap_table:add(55555, simplemux)
tcp_encap_table:add(55555, simplemux)
-- register IANA protocol 253 as Simplemux
-- this is needed in Network mode
ip_encap_table:add(253, simplemux)