Package dpkt :: Module ethernet
[hide private]
[frames] | no frames]

Source Code for Module dpkt.ethernet

  1  # $Id: ethernet.py 365 2006-05-08 19:34:00Z dugsong $ 
  2   
  3  """Ethernet II, LLC (802.3+802.2), LLC/SNAP, and Novell raw 802.3, 
  4  with automatic 802.1q, MPLS, PPPoE, and Cisco ISL decapsulation.""" 
  5   
  6  import struct 
  7  import dpkt, stp 
  8   
  9  ETH_CRC_LEN     = 4 
 10  ETH_HDR_LEN     = 14 
 11   
 12  ETH_LEN_MIN     = 64            # minimum frame length with CRC 
 13  ETH_LEN_MAX     = 1518          # maximum frame length with CRC 
 14   
 15  ETH_MTU         = (ETH_LEN_MAX - ETH_HDR_LEN - ETH_CRC_LEN) 
 16  ETH_MIN         = (ETH_LEN_MIN - ETH_HDR_LEN - ETH_CRC_LEN) 
 17   
 18  # Ethernet payload types - http://standards.ieee.org/regauth/ethertype 
 19  ETH_TYPE_PUP    = 0x0200                # PUP protocol 
 20  ETH_TYPE_IP     = 0x0800                # IP protocol 
 21  ETH_TYPE_ARP    = 0x0806                # address resolution protocol 
 22  ETH_TYPE_CDP    = 0x2000                # Cisco Discovery Protocol 
 23  ETH_TYPE_DTP    = 0x2004                # Cisco Dynamic Trunking Protocol 
 24  ETH_TYPE_REVARP = 0x8035                # reverse addr resolution protocol 
 25  ETH_TYPE_8021Q  = 0x8100                # IEEE 802.1Q VLAN tagging 
 26  ETH_TYPE_IPX    = 0x8137                # Internetwork Packet Exchange 
 27  ETH_TYPE_IP6    = 0x86DD                # IPv6 protocol 
 28  ETH_TYPE_PPP    = 0x880B                # PPP 
 29  ETH_TYPE_MPLS   = 0x8847                # MPLS 
 30  ETH_TYPE_MPLS_MCAST     = 0x8848        # MPLS Multicast 
 31  ETH_TYPE_PPPoE_DISC     = 0x8863        # PPP Over Ethernet Discovery Stage 
 32  ETH_TYPE_PPPoE          = 0x8864        # PPP Over Ethernet Session Stage 
 33   
34 -class Ethernet(dpkt.Packet):
35 __hdr__ = ( 36 ('dst', '6s', ''), 37 ('src', '6s', ''), 38 ('type', 'H', ETH_TYPE_IP) 39 ) 40 _typesw = {} 41
42 - def _unpack_data(self, buf):
43 if self.type == ETH_TYPE_8021Q: 44 self.tag, self.type = struct.unpack('>HH', buf[:4]) 45 buf = buf[4:] 46 elif self.type == ETH_TYPE_MPLS or \ 47 self.type == ETH_TYPE_MPLS_MCAST: 48 # XXX - skip labels 49 for i in range(24): 50 if struct.unpack('>I', buf[i:i+4])[0] & 0x0100: # MPLS_STACK_BOTTOM 51 break 52 self.type = ETH_TYPE_IP 53 buf = buf[(i + 1) * 4:] 54 try: 55 self.data = self._typesw[self.type](buf) 56 setattr(self, self.data.__class__.__name__.lower(), self.data) 57 except (KeyError, dpkt.UnpackError): 58 self.data = buf
59
60 - def unpack(self, buf):
61 dpkt.Packet.unpack(self, buf) 62 if self.type > 1500: 63 # Ethernet II 64 self._unpack_data(self.data) 65 elif self.dst.startswith('\x01\x00\x0c\x00\x00') or \ 66 self.dst.startswith('\x03\x00\x0c\x00\x00'): 67 # Cisco ISL 68 #self.vlan = struct.unpack('>H', self.data[6:8])[0] 69 self.unpack(self.data[12:]) 70 elif self.data.startswith('\xff\xff'): 71 # Novell "raw" 802.3 72 self.type = ETH_TYPE_IPX 73 self.data = self.ipx = self._typesw[ETH_TYPE_IPX](self.data[2:]) 74 else: 75 # 802.2 LLC 76 #self.dsap, self.ssap, self.ctl = struct.unpack('BBB', self.data[:3]) 77 if self.data.startswith('\xaa\xaa'): 78 # SNAP 79 self.type = struct.unpack('>H', self.data[6:8])[0] 80 self._unpack_data(self.data[8:]) 81 else: 82 # non-SNAP 83 dsap = ord(self.data[0]) 84 if dsap == 0x06: # SAP_IP 85 self.data = self.ip = self._typesw[ETH_TYPE_IP](self.data[3:]) 86 elif dsap == 0x10 or dsap == 0xe0: # SAP_NETWARE{1,2} 87 self.data = self.ipx = self._typesw[ETH_TYPE_IPX](self.data[3:]) 88 elif dsap == 0x42: # SAP_STP 89 self.data = self.stp = stp.STP(self.data[3:])
90
91 - def set_type(cls, t, pktclass):
92 cls._typesw[t] = pktclass
93 set_type = classmethod(set_type) 94
95 - def get_type(cls, t):
96 return cls._typesw[t]
97 get_type = classmethod(get_type)
98 99 # XXX - auto-load Ethernet dispatch table from ETH_TYPE_* definitions
100 -def __load_types():
101 import os 102 d = dict.fromkeys([ x[:-3] for x in os.listdir(os.path.dirname(__file__) or '.') if x.endswith('.py') ]) 103 g = globals() 104 for k, v in g.iteritems(): 105 if k.startswith('ETH_TYPE_'): 106 name = k[9:] 107 modname = name.lower() 108 if modname in d: 109 mod = __import__(modname, g) 110 Ethernet.set_type(v, getattr(mod, name))
111 112 if not Ethernet._typesw: 113 __load_types() 114 115 if __name__ == '__main__': 116 import unittest 117
118 - class EthTestCase(unittest.TestCase):
119 - def test_eth(self):
120 s = '\x00\xb0\xd0\xe1\x80r\x00\x11$\x8c\x11\xde\x86\xdd`\x00\x00\x00\x00(\x06@\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x11$\xff\xfe\x8c\x11\xde\xfe\x80\x00\x00\x00\x00\x00\x00\x02\xb0\xd0\xff\xfe\xe1\x80r\xcd\xd3\x00\x16\xffP\xd7\x13\x00\x00\x00\x00\xa0\x02\xff\xffg\xd3\x00\x00\x02\x04\x05\xa0\x01\x03\x03\x00\x01\x01\x08\n}\x18:a\x00\x00\x00\x00' 121 eth = Ethernet(s)
122 123 unittest.main() 124