summaryrefslogtreecommitdiffstats
path: root/scripts/external_libs/scapy-2.3.1/python3/scapy/contrib/cdp.py
blob: ed3361629b3ebc8de5232453dc13b4731069f2da (plain)
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
#! /usr/bin/env python

# scapy.contrib.description = Cisco Discovery Protocol
# scapy.contrib.status = loads

#############################################################################
##                                                                         ##
## cdp.py --- Cisco Discovery Protocol (CDP) extension for Scapy           ##
##                                                                         ##
## Copyright (C) 2006    Nicolas Bareil  <nicolas.bareil AT eads DOT net>  ##
##                       Arnaud Ebalard  <arnaud.ebalard AT eads DOT net>  ##
##                       EADS/CRC security team                            ##
##                                                                         ##
## This program is free software; you can redistribute it and/or modify it ##
## under the terms of the GNU General Public License version 2 as          ##
## published by the Free Software Foundation; version 2.                   ##
##                                                                         ##
## This program is distributed in the hope that it will be useful, but     ##
## WITHOUT ANY WARRANTY; without even the implied warranty of              ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       ##
## General Public License for more details.                                ##
##                                                                         ##
#############################################################################

from scapy.packet import *
from scapy.fields import *
from scapy.layers.inet6 import *


#####################################################################
# Helpers and constants
#####################################################################

# CDP TLV classes keyed by type
_cdp_tlv_cls = { 0x0001: "CDPMsgDeviceID",
                 0x0002: "CDPMsgAddr",
                 0x0003: "CDPMsgPortID",
                 0x0004: "CDPMsgCapabilities",
                 0x0005: "CDPMsgSoftwareVersion",
                 0x0006: "CDPMsgPlatform",
                 0x0007: "CDPMsgIPPrefix",
                 0x0008: "CDPMsgProtoHello",
                 0x0009: "CDPMsgVTPMgmtDomain", # CDPv2
                 0x000a: "CDPMsgNativeVLAN",    # CDPv2
                 0x000b: "CDPMsgDuplex",        # 
#                 0x000c: "CDPMsgGeneric",
#                 0x000d: "CDPMsgGeneric",
                 0x000e: "CDPMsgVoIPVLANReply",
                 0x000f: "CDPMsgVoIPVLANQuery",
                 0x0010: "CDPMsgPower",
                 0x0011: "CDPMsgMTU",
#                 0x0012: "CDPMsgTrustBitmap",
#                 0x0013: "CDPMsgUntrustedPortCoS",
#                 0x0014: "CDPMsgSystemName",
#                 0x0015: "CDPMsgSystemOID",
                 0x0016: "CDPMsgMgmtAddr",
#                 0x0017: "CDPMsgLocation",
                 0x0019: "CDPMsgUnknown19",
#                 0x001a: "CDPPowerAvailable"
                 }

_cdp_tlv_types = { 0x0001: "Device ID",
                   0x0002: "Addresses",
                   0x0003: "Port ID",
                   0x0004: "Capabilities",
                   0x0005: "Software Version",
                   0x0006: "Platform",
                   0x0007: "IP Prefix",
                   0x0008: "Protocol Hello",
                   0x0009: "VTP Mangement Domain", # CDPv2
                   0x000a: "Native VLAN",    # CDPv2
                   0x000b: "Duplex",        # 
                   0x000c: "CDP Unknown command (send us a pcap file)",
                   0x000d: "CDP Unknown command (send us a pcap file)",
                   0x000e: "VoIP VLAN Reply",
                   0x000f: "VoIP VLAN Query",
                   0x0010: "Power",
                   0x0011: "MTU",
                   0x0012: "Trust Bitmap",
                   0x0013: "Untrusted Port CoS",
                   0x0014: "System Name",
                   0x0015: "System OID",
                   0x0016: "Management Address",
                   0x0017: "Location",
                   0x0018: "CDP Unknown command (send us a pcap file)",
                   0x0019: "CDP Unknown command (send us a pcap file)",
                   0x001a: "Power Available"}

def _CDPGuessPayloadClass(p, **kargs):
    cls = conf.raw_layer
    if len(p) >= 2:
        t = struct.unpack("!H", p[:2])[0]
        clsname = _cdp_tlv_cls.get(t, "CDPMsgGeneric")
        cls = globals()[clsname]

    return cls(p, **kargs)

class CDPMsgGeneric(Packet):
    name = "CDP Generic Message"
    fields_desc = [ XShortEnumField("type", None, _cdp_tlv_types),
                    FieldLenField("len", None, "val", "!H"),
                    StrLenField("val", "", length_from=lambda x:x.len - 4) ]


    def guess_payload_class(self, p):
        return conf.padding_layer # _CDPGuessPayloadClass

class CDPMsgDeviceID(CDPMsgGeneric):
    name = "Device ID"
    type = 0x0001

_cdp_addr_record_ptype = {0x01: "NLPID", 0x02: "802.2"}
_cdp_addrrecord_proto_ip = "\xcc"
_cdp_addrrecord_proto_ipv6 = "\xaa\xaa\x03\x00\x00\x00\x86\xdd"

class CDPAddrRecord(Packet):
    name = "CDP Address"
    fields_desc = [ ByteEnumField("ptype", 0x01, _cdp_addr_record_ptype),
                    FieldLenField("plen", None, "proto", "B"),
                    StrLenField("proto", None, length_from=lambda x:x.plen),
                    FieldLenField("addrlen", None, length_of=lambda x:x.addr),
                    StrLenField("addr", None, length_from=lambda x:x.addrlen)]

    def guess_payload_class(self, p):
        return conf.padding_layer

class CDPAddrRecordIPv4(CDPAddrRecord):
    name = "CDP Address IPv4"
    fields_desc = [ ByteEnumField("ptype", 0x01, _cdp_addr_record_ptype),
                    FieldLenField("plen", 1, "proto", "B"),
                    StrLenField("proto", _cdp_addrrecord_proto_ip, length_from=lambda x:x.plen),
                    ShortField("addrlen", 4),
                    IPField("addr", "0.0.0.0")]

class CDPAddrRecordIPv6(CDPAddrRecord):
    name = "CDP Address IPv6"
    fields_desc = [ ByteEnumField("ptype", 0x02, _cdp_addr_record_ptype),
                    FieldLenField("plen", 8, "proto", "B"),
                    StrLenField("proto", _cdp_addrrecord_proto_ipv6, length_from=lambda x:x.plen),
                    ShortField("addrlen", 16),
                    IP6Field("addr", "::1")]

def _CDPGuessAddrRecord(p, **kargs):
    cls = conf.raw_layer
    if len(p) >= 2:
        plen = struct.unpack("B", p[1])[0]
        proto = ''.join(struct.unpack("s" * plen, p[2:plen + 2])[0:plen])

        if proto == _cdp_addrrecord_proto_ip:
            clsname = "CDPAddrRecordIPv4"
        elif proto == _cdp_addrrecord_proto_ipv6:
            clsname = "CDPAddrRecordIPv6"
        else:
            clsname = "CDPAddrRecord"

        cls = globals()[clsname]

    return cls(p, **kargs)

class CDPMsgAddr(CDPMsgGeneric):
    name = "Addresses"
    fields_desc = [ XShortEnumField("type", 0x0002, _cdp_tlv_types),
                    ShortField("len", None),
                    FieldLenField("naddr", None, "addr", "!I"),
                    PacketListField("addr", [], _CDPGuessAddrRecord, count_from=lambda x:x.naddr) ]

    def post_build(self, pkt, pay):
        if self.len is None:
            l = 8 + len(self.addr) * 9
            pkt = pkt[:2] + struct.pack("!H", l) + pkt[4:]
        p = pkt + pay
        return p

class CDPMsgPortID(CDPMsgGeneric):
    name = "Port ID"
    fields_desc = [ XShortEnumField("type", 0x0003, _cdp_tlv_types),
                    FieldLenField("len", None, "iface", "!H"),
                    StrLenField("iface", "Port 1", length_from=lambda x:x.len - 4) ]


_cdp_capabilities = [ "Router",
                      "TransparentBridge",
                      "SourceRouteBridge",
                      "Switch",
                      "Host",
                      "IGMPCapable",
                      "Repeater"] + ["Bit%d" % x for x in range(25,0,-1)]


class CDPMsgCapabilities(CDPMsgGeneric):
    name = "Capabilities"
    fields_desc = [ XShortEnumField("type", 0x0004, _cdp_tlv_types),
                    ShortField("len", 8),
                    FlagsField("cap", 0, 32,  _cdp_capabilities) ]


class CDPMsgSoftwareVersion(CDPMsgGeneric):
    name = "Software Version"
    type = 0x0005


class CDPMsgPlatform(CDPMsgGeneric):
    name = "Platform"
    type = 0x0006

_cdp_duplex = { 0x00: "Half", 0x01: "Full" }

# ODR Routing
class CDPMsgIPPrefix(CDPMsgGeneric):
    name = "IP Prefix"
    type = 0x0007
    fields_desc = [ XShortEnumField("type", 0x0007, _cdp_tlv_types),
                    ShortField("len", 8),
                    IPField("defaultgw", "192.168.0.1") ]

# TODO : Do me !!!!!! 0x0008
class CDPMsgProtoHello(CDPMsgGeneric):
    name = "Protocol Hello"
    type = 0x0008

class CDPMsgVTPMgmtDomain(CDPMsgGeneric):
    name = "VTP Management Domain"
    type = 0x0009

class CDPMsgNativeVLAN(CDPMsgGeneric):
    name = "Native VLAN"
    fields_desc = [ XShortEnumField("type", 0x000a, _cdp_tlv_types),
                    ShortField("len", 6),
                    ShortField("vlan", 1) ]

class CDPMsgDuplex(CDPMsgGeneric):
    name = "Duplex"
    fields_desc = [ XShortEnumField("type", 0x000b, _cdp_tlv_types),
                    ShortField("len", 5),
                    ByteEnumField("duplex", 0x00, _cdp_duplex) ]

class CDPMsgVoIPVLANReply(CDPMsgGeneric):
    name = "VoIP VLAN Reply"
    fields_desc = [ XShortEnumField("type", 0x000e, _cdp_tlv_types),
                    ShortField("len", 7),
                    ByteField("status?", 1),
                    ShortField("vlan", 1)]


# TODO : Do me !!! 0x000F
class CDPMsgVoIPVLANQuery(CDPMsgGeneric):
    name = "VoIP VLAN Query"
    type = 0x000f

#    fields_desc = [XShortEnumField("type", 0x000f, _cdp_tlv_types),
#		   FieldLenField("len", None, "val", "!H") ]


class _CDPPowerField(ShortField):
    def i2repr(self, pkt, x):
        if x is None:
            x = 0
        return "%d mW" % x


class CDPMsgPower(CDPMsgGeneric):
    name = "Power"
    # Check if field length is fixed (2 bytes)
    fields_desc = [ XShortEnumField("type", 0x0010, _cdp_tlv_types),
                    ShortField("len", 6),
                    _CDPPowerField("power", 1337)]


class CDPMsgMTU(CDPMsgGeneric):
    name = "MTU"
    # Check if field length is fixed (2 bytes)
    fields_desc = [ XShortEnumField("type", 0x0011, _cdp_tlv_types),
                    ShortField("len", 6),
                    ShortField("mtu", 1500)]

class CDPMsgMgmtAddr(CDPMsgAddr):
    name = "Management Address"
    type = 0x0016

class CDPMsgUnknown19(CDPMsgGeneric):
    name = "Unknown CDP Message"
    type = 0x0019

class CDPMsg(CDPMsgGeneric):
    name = "CDP "
    fields_desc = [ XShortEnumField("type", None, _cdp_tlv_types),
                    FieldLenField("len", None, "val", "!H"),
                    StrLenField("val", "", length_from=lambda x:x.len - 4) ]

class _CDPChecksum:
    def post_build(self, pkt, pay):
        p = pkt + pay
        if self.cksum is None:
            cksum = checksum(p)
            p = p[:2] + struct.pack("!H", cksum) + p[4:]
        return p

class CDPv2_HDR(_CDPChecksum, CDPMsgGeneric):
    name = "Cisco Discovery Protocol version 2"
    fields_desc = [ ByteField("vers", 2),
                    ByteField("ttl", 180),
                    XShortField("cksum", None),
                    PacketListField("msg", [], _CDPGuessPayloadClass) ]

bind_layers(SNAP, CDPv2_HDR, {"code": 0x2000, "OUI": 0xC})