Home | History | Annotate | Download | only in contrib
      1 # This file is part of Scapy
      2 # Scapy is free software: you can redistribute it and/or modify
      3 # it under the terms of the GNU General Public License as published by
      4 # the Free Software Foundation, either version 2 of the License, or
      5 # any later version.
      6 #
      7 # Scapy is distributed in the hope that it will be useful,
      8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     10 # GNU General Public License for more details.
     11 #
     12 # You should have received a copy of the GNU General Public License
     13 # along with Scapy. If not, see <http://www.gnu.org/licenses/>.
     14 
     15 # scapy.contrib.description = MPLS
     16 # scapy.contrib.status = loads
     17 
     18 from scapy.packet import Packet, bind_layers, Padding
     19 from scapy.fields import BitField,ByteField
     20 from scapy.layers.inet import IP
     21 from scapy.layers.inet6 import IPv6
     22 from scapy.layers.l2 import Ether, GRE
     23 from scapy.compat import orb
     24 
     25 class MPLS(Packet):
     26    name = "MPLS"
     27    fields_desc =  [ BitField("label", 3, 20),
     28                     BitField("cos", 0, 3),
     29                     BitField("s", 1, 1),
     30                     ByteField("ttl", 0)  ]
     31 
     32    def guess_payload_class(self, payload):
     33        if len(payload) >= 1:
     34            if not self.s:
     35               return MPLS
     36            ip_version = (orb(payload[0]) >> 4) & 0xF
     37            if ip_version == 4:
     38                return IP
     39            elif ip_version == 6:
     40                return IPv6
     41        return Padding
     42 
     43 bind_layers(Ether, MPLS, type=0x8847)
     44 bind_layers(GRE, MPLS, proto=0x8847)
     45 bind_layers(MPLS, MPLS, s=0)
     46