-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
pnio_dcp.py
670 lines (564 loc) · 21.1 KB
/
pnio_dcp.py
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# SPDX-License-Identifier: GPL-2.0-or-later
# This file is part of Scapy
# See https://scapy.net/ for more information
# Copyright (C) 2019 Stefan Mehner (stefan.mehner@b-tu.de)
# scapy.contrib.description = Profinet DCP layer
# scapy.contrib.status = loads
from scapy.compat import orb
from scapy.all import Packet, bind_layers, Padding
from scapy.fields import (
ByteEnumField,
ConditionalField,
FieldLenField,
FieldListField,
IPField,
LenField,
MACField,
MultiEnumField,
MultipleTypeField,
PacketListField,
PadField,
ShortEnumField,
ShortField,
StrLenField,
XByteField,
XIntField,
XShortField,
)
# minimum packet is 60 bytes.. 14 bytes are Ether()
MIN_PACKET_LENGTH = 44
#####################################################
# Constants #
#####################################################
DCP_GET_SET_FRAME_ID = 0xFEFD
DCP_IDENTIFY_REQUEST_FRAME_ID = 0xFEFE
DCP_IDENTIFY_RESPONSE_FRAME_ID = 0xFEFF
DCP_REQUEST = 0x00
DCP_RESPONSE = 0x01
DCP_SERVICE_ID_GET = 0x03
DCP_SERVICE_ID_SET = 0x04
DCP_SERVICE_ID_IDENTIFY = 0x05
DCP_SERVICE_ID = {
0x00: "reserved",
0x01: "Manufacturer specific",
0x02: "Manufacturer specific",
0x03: "Get",
0x04: "Set",
0x05: "Identify",
0x06: "Hello",
}
DCP_SERVICE_TYPE = {
0x00: "Request",
0x01: "Response Success",
0x05: "Response - Request not supported",
}
DCP_DEVICE_ROLES = {
0x00: "IO Supervisor",
0x01: "IO Device",
0x02: "IO Controller",
}
DCP_OPTIONS = {
0x00: "reserved",
0x01: "IP",
0x02: "Device properties",
0x03: "DHCP",
0x04: "Reserved",
0x05: "Control",
0x06: "Device Initiative",
0xff: "All Selector"
}
DCP_OPTIONS.update({i: "reserved" for i in range(0x07, 0x7f)})
DCP_OPTIONS.update({i: "Manufacturer specific" for i in range(0x80, 0xfe)})
DCP_SUBOPTIONS = {
# ip
0x01: {
0x00: "Reserved",
0x01: "MAC Address",
0x02: "IP Parameter",
0x03: "Full IP Suite",
},
# device properties
0x02: {
0x00: "Reserved",
0x01: "Manufacturer specific (Type of Station)",
0x02: "Name of Station",
0x03: "Device ID",
0x04: "Device Role",
0x05: "Device Options",
0x06: "Alias Name",
0x07: "Device Instance",
0x08: "OEM Device ID",
},
# dhcp
0x03: {
0x0c: "Host name",
0x2b: "Vendor specific",
0x36: "Server identifier",
0x37: "Parameter request list",
0x3c: "Class identifier",
0x3d: "DHCP client identifier",
0x51: "FQDN, Fully Qualified Domain Name",
0x61: "UUID/GUID-based Client",
0xff: "Control DHCP for address resolution"
},
# control
0x05: {
0x00: "Reserved",
0x01: "Start Transaction",
0x02: "End Transaction",
0x03: "Signal",
0x04: "Response",
0x05: "Reset Factory Settings",
0x06: "Reset to Factory"
},
# device initiative
0x06: {
0x00: "Reserved",
0x01: "Device Initiative"
},
0xff: {
0xff: "ALL Selector"
}
}
BLOCK_INFOS = {
0x00: "Reserved",
}
BLOCK_INFOS.update({i: "reserved" for i in range(0x01, 0xff)})
IP_BLOCK_INFOS = {
0x0000: "IP not set",
0x0001: "IP set",
0x0002: "IP set by DHCP",
0x0080: "IP not set (address conflict detected)",
0x0081: "IP set (address conflict detected)",
0x0082: "IP set by DHCP (address conflict detected)",
}
IP_BLOCK_INFOS.update({i: "reserved" for i in range(0x0003, 0x007f)})
BLOCK_ERRORS = {
0x00: "Ok",
0x01: "Option unsupp.",
0x02: "Suboption unsupp. or no DataSet avail.",
0x03: "Suboption not set",
0x04: "Resource Error",
0x05: "SET not possible by local reasons",
0x06: "In operation, SET not possible",
}
BLOCK_QUALIFIERS = {
0x0000: "Use the value temporary",
0x0001: "Save the value permanent",
}
BLOCK_QUALIFIERS.update({i: "reserved" for i in range(0x0002, 0x00ff)})
#####################################################
# DCP Blocks #
#####################################################
# GENERIC DCP BLOCK
# DCP RESPONSE BLOCKS
class DCPBaseBlock(Packet):
"""
base class for all DCP Blocks
"""
fields_desc = [
ByteEnumField("option", 1, DCP_OPTIONS),
MultiEnumField("sub_option", 2, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
FieldLenField("dcp_block_length", None, length_of="data"),
ShortEnumField("block_info", 0, BLOCK_INFOS),
StrLenField("data", "", length_from=lambda x: x.dcp_block_length),
]
def extract_padding(self, s):
return '', s
# OPTION: IP
class DCPIPBlock(Packet):
fields_desc = [
ByteEnumField("option", 1, DCP_OPTIONS),
MultiEnumField("sub_option", 2, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", None),
ShortEnumField("block_info", 1, IP_BLOCK_INFOS),
IPField("ip", "192.168.0.2"),
IPField("netmask", "255.255.255.0"),
IPField("gateway", "192.168.0.1"),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPFullIPBlock(Packet):
fields_desc = [
ByteEnumField("option", 1, DCP_OPTIONS),
MultiEnumField("sub_option", 3, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", None),
ShortEnumField("block_info", 1, IP_BLOCK_INFOS),
IPField("ip", "192.168.0.2"),
IPField("netmask", "255.255.255.0"),
IPField("gateway", "192.168.0.1"),
FieldListField("dnsaddr", [], IPField("", "0.0.0.0"),
count_from=lambda x: 4),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPMACBlock(Packet):
fields_desc = [
ByteEnumField("option", 1, DCP_OPTIONS),
MultiEnumField("sub_option", 1, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
FieldLenField("dcp_block_length", None),
ShortEnumField("block_info", 0, BLOCK_INFOS),
MACField("mac", "00:00:00:00:00:00"),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
# OPTION: Device Properties
class DCPManufacturerSpecificBlock(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 1, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
FieldLenField("dcp_block_length", None),
ShortEnumField("block_info", 0, BLOCK_INFOS),
StrLenField("device_vendor_value", "et200sp",
length_from=lambda x: x.dcp_block_length - 2),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPNameOfStationBlock(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 2, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
FieldLenField("dcp_block_length", None, length_of="name_of_station",
adjust=lambda p, x: x + 2),
ShortEnumField("block_info", 0, BLOCK_INFOS),
StrLenField("name_of_station", "et200sp",
length_from=lambda x: x.dcp_block_length - 2),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPDeviceIDBlock(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 3, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", None),
ShortEnumField("block_info", 0, BLOCK_INFOS),
XShortField("vendor_id", 0x002a),
XShortField("device_id", 0x0313),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPDeviceRoleBlock(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 4, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", 4),
ShortEnumField("block_info", 0, BLOCK_INFOS),
ByteEnumField("device_role_details", 1, DCP_DEVICE_ROLES),
XByteField("reserved", 0x00),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
# one DeviceOptionsBlock can contain 1..n different options
class DeviceOption(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 5, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
]
def extract_padding(self, s):
return '', s
class DCPDeviceOptionsBlock(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 5, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", None),
ShortEnumField("block_info", 0, BLOCK_INFOS),
PacketListField("device_options", [], DeviceOption,
length_from=lambda p: p.dcp_block_length - 2),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPAliasNameBlock(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 6, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
FieldLenField("dcp_block_length", None, length_of="alias_name",
adjust=lambda p, x: x + 2),
ShortEnumField("block_info", 0, BLOCK_INFOS),
StrLenField("alias_name", "et200sp",
length_from=lambda x: x.dcp_block_length - 2),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPDeviceInstanceBlock(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 7, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", 4),
ShortEnumField("block_info", 0, BLOCK_INFOS),
XByteField("device_instance_high", 0x00),
XByteField("device_instance_low", 0x01),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPOEMIDBlock(Packet):
fields_desc = [
ByteEnumField("option", 2, DCP_OPTIONS),
MultiEnumField("sub_option", 8, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", None),
ShortEnumField("block_info", 0, BLOCK_INFOS),
XShortField("vendor_id", 0x002a),
XShortField("device_id", 0x0313),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPControlBlock(Packet):
fields_desc = [
ByteEnumField("option", 5, DCP_OPTIONS),
MultiEnumField("sub_option", 4, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", 3),
ByteEnumField("response", 2, DCP_OPTIONS),
MultiEnumField("response_sub_option", 2, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
ByteEnumField("block_error", 0, BLOCK_ERRORS),
PadField(StrLenField("padding", b"\x00",
length_from=lambda p: p.dcp_block_length % 2), 1,
padwith=b"\x00")
]
def extract_padding(self, s):
return '', s
class DCPDeviceInitiativeBlock(Packet):
"""
device initiative DCP block
"""
fields_desc = [
ByteEnumField("option", 6, DCP_OPTIONS),
MultiEnumField("sub_option", 1, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
FieldLenField("dcp_block_length", None, length_of="device_initiative"),
ShortEnumField("block_info", 0, BLOCK_INFOS),
ShortField("device_initiative", 1),
]
def extract_padding(self, s):
return '', s
def guess_dcp_block_class(packet, **kargs):
"""
returns the correct dcp block class needed to dissect the current tag
if nothing can be found -> dcp base block will be used
:param packet: the current packet
:return: dcp block class
"""
# packet = unicode(packet, "utf-8")
option = orb(packet[0])
suboption = orb(packet[1])
# NOTE implement the other functions if needed
class_switch_case = {
# IP
0x01:
{
0x01: "DCPMACBlock",
0x02: "DCPIPBlock"
},
# Device Properties
0x02:
{
0x01: "DCPManufacturerSpecificBlock",
0x02: "DCPNameOfStationBlock",
0x03: "DCPDeviceIDBlock",
0x04: "DCPDeviceRoleBlock",
0x05: "DCPDeviceOptionsBlock",
0x06: "DCPAliasNameBlock",
0x07: "DCPDeviceInstanceBlock",
0x08: "DCPOEMIDBlock"
},
# DHCP
0x03:
{
0x0c: "Host name",
0x2b: "Vendor specific",
0x36: "Server identifier",
0x37: "Parameter request list",
0x3c: "Class identifier",
0x3d: "DHCP client identifier",
0x51: "FQDN, Fully Qualified Domain Name",
0x61: "UUID/GUID-based Client",
0xff: "Control DHCP for address resolution"
},
# Control
0x05:
{
0x00: "Reserved (0x00)",
0x01: "Start Transaction (0x01)",
0x02: "End Transaction (0x02)",
0x03: "Signal (0x03)",
0x04: "DCPControlBlock",
0x05: "Reset Factory Settings (0x05)",
0x06: "Reset to Factory (0x06)"
},
# Device Inactive
0x06:
{
0x00: "Reserved (0x00)",
0x01: "DCPDeviceInitiativeBlock"
},
# ALL Selector
0xff:
{
0xff: "ALL Selector (0xff)"
}
}
try:
c = class_switch_case[option][suboption]
except KeyError:
c = "DCPBaseBlock"
cls = globals()[c]
return cls(packet, **kargs)
# GENERIC DCP PACKET
class ProfinetDCP(Packet):
"""
Profinet DCP Packet
Requests are handled via ConditionalField because here only 1 Block is used
every time.
Response can contain 1..n Blocks, for that you have to use one ProfinetDCP
Layer with one or multiple DCP*Block Layers::
ProfinetDCP / DCPNameOfStationBlock / DCPDeviceIDBlock ...
Example for a DCP Identify All Request::
Ether(dst="01:0e:cf:00:00:00") /
ProfinetIO(frameID=DCP_IDENTIFY_REQUEST_FRAME_ID) /
ProfinetDCP(service_id=DCP_SERVICE_ID_IDENTIFY,
service_type=DCP_REQUEST, option=255, sub_option=255,
dcp_data_length=4)
Example for a DCP Identify Response::
Ether(dst=dst_mac) /
ProfinetIO(frameID=DCP_IDENTIFY_RESPONSE_FRAME_ID) /
ProfinetDCP(
service_id=DCP_SERVICE_ID_IDENTIFY,
service_type=DCP_RESPONSE) /
DCPNameOfStationBlock(name_of_station="device1")
Example for a DCP Set Request::
Ether(dst=mac) /
ProfinetIO(frameID=DCP_GET_SET_FRAME_ID) /
ProfinetDCP(service_id=DCP_SERVICE_ID_SET, service_type=DCP_REQUEST,
option=2, sub_option=2, dcp_data_length=14, dcp_block_length=10,
name_of_station=name, reserved=0)
"""
name = "Profinet DCP"
# a DCP PDU consists of some fields and 1..n DCP Blocks
fields_desc = [
ByteEnumField("service_id", 5, DCP_SERVICE_ID),
ByteEnumField("service_type", 0, DCP_SERVICE_TYPE),
XIntField("xid", 0x01000001),
# XShortField('reserved', 0),
ShortField('reserved', 0),
LenField("dcp_data_length", None),
# DCP REQUEST specific
ConditionalField(ByteEnumField("option", 2, DCP_OPTIONS),
lambda pkt: pkt.service_type == 0),
ConditionalField(
MultiEnumField("sub_option", 3, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
lambda pkt: pkt.service_type == 0),
# calculate the len fields - workaround
ConditionalField(LenField("dcp_block_length", 0),
lambda pkt: pkt.service_type == 0),
# DCP SET REQUEST #
ConditionalField(ShortEnumField("block_qualifier", 1,
BLOCK_QUALIFIERS),
lambda pkt: pkt.service_id == 4 and
pkt.service_type == 0),
# (Common) Name Of Station
ConditionalField(
MultipleTypeField(
[
(StrLenField("name_of_station", "et200sp",
length_from=lambda x: x.dcp_block_length - 2),
lambda pkt: pkt.service_id == 4),
],
StrLenField("name_of_station", "et200sp",
length_from=lambda x: x.dcp_block_length),
),
lambda pkt: pkt.service_type == 0 and pkt.option == 2 and
pkt.sub_option == 2
),
# DCP SET REQUEST #
# MAC
ConditionalField(MACField("mac", "00:00:00:00:00:00"),
lambda pkt: pkt.service_id == 4 and
pkt.service_type == 0 and pkt.option == 1 and
pkt.sub_option == 1),
# IP
ConditionalField(IPField("ip", "192.168.0.2"),
lambda pkt: pkt.service_id == 4 and
pkt.service_type == 0 and pkt.option == 1 and
pkt.sub_option in [2, 3]),
ConditionalField(IPField("netmask", "255.255.255.0"),
lambda pkt: pkt.service_id == 4 and
pkt.service_type == 0 and pkt.option == 1 and
pkt.sub_option in [2, 3]),
ConditionalField(IPField("gateway", "192.168.0.1"),
lambda pkt: pkt.service_id == 4 and
pkt.service_type == 0 and pkt.option == 1 and
pkt.sub_option in [2, 3]),
# Full IP
ConditionalField(FieldListField("dnsaddr", [], IPField("", "0.0.0.0"),
count_from=lambda x: 4),
lambda pkt: pkt.service_id == 4 and
pkt.service_type == 0 and pkt.option == 1 and
pkt.sub_option == 3),
# DCP IDENTIFY REQUEST #
# Name of station (handled above)
# Alias name
ConditionalField(StrLenField("alias_name", "et200sp",
length_from=lambda x: x.dcp_block_length),
lambda pkt: pkt.service_id == 5 and
pkt.service_type == 0 and pkt.option == 2 and
pkt.sub_option == 6),
# implement further REQUEST fields if needed ....
# DCP RESPONSE BLOCKS #
ConditionalField(
PacketListField("dcp_blocks", [], guess_dcp_block_class,
length_from=lambda p: p.dcp_data_length),
lambda pkt: pkt.service_type == 1),
]
def post_build(self, pkt, pay):
# add padding to ensure min packet length
padding = MIN_PACKET_LENGTH - (len(pkt + pay))
pay += b"\0" * padding
return Packet.post_build(self, pkt, pay)
bind_layers(ProfinetDCP, Padding)