1## This file is (hopefully) part of Scapy
2## See http://www.secdev.org/projects/scapy for more informations
3## <jellch@harris.com>
4## This program is published under a GPLv2 license
5
6# scapy.contrib.description = PPI
7# scapy.contrib.status = loads
8
9
10"""
11PPI (Per-Packet Information).
12"""
13import logging,struct
14from scapy.config import conf
15from scapy.packet import *
16from scapy.fields import *
17from scapy.layers.l2 import Ether
18from scapy.layers.dot11 import Dot11
19
20# Dictionary to map the TLV type to the class name of a sub-packet
21_ppi_types = {}
22def addPPIType(id, value):
23    _ppi_types[id] = value
24def getPPIType(id, default="default"):
25    return _ppi_types.get(id, _ppi_types.get(default, None))
26
27
28# Default PPI Field Header
29class PPIGenericFldHdr(Packet):
30    name = "PPI Field Header"
31    fields_desc = [ LEShortField('pfh_type', 0),
32                    FieldLenField('pfh_length', None, length_of="value", fmt='<H', adjust=lambda p,x:x+4),
33                    StrLenField("value", "", length_from=lambda p:p.pfh_length) ]
34
35    def extract_padding(self, p):
36        return "",p
37
38def _PPIGuessPayloadClass(p, **kargs):
39    """ This function tells the PacketListField how it should extract the
40        TLVs from the payload.  We pass cls only the length string
41        pfh_len says it needs.  If a payload is returned, that means
42        part of the sting was unused.  This converts to a Raw layer, and
43        the remainder of p is added as Raw's payload.  If there is no
44        payload, the remainder of p is added as out's payload.
45    """
46    if len(p) >= 4:
47        t,pfh_len = struct.unpack("<HH", p[:4])
48        # Find out if the value t is in the dict _ppi_types.
49        # If not, return the default TLV class
50        cls = getPPIType(t, "default")
51        pfh_len += 4
52        out = cls(p[:pfh_len], **kargs)
53        if (out.payload):
54            out.payload = conf.raw_layer(out.payload.load)
55            if (len(p) > pfh_len):
56                out.payload.payload = conf.padding_layer(p[pfh_len:])
57        elif (len(p) > pfh_len):
58            out.payload = conf.padding_layer(p[pfh_len:])
59
60    else:
61        out = conf.raw_layer(p, **kargs)
62    return out
63
64
65
66
67class PPI(Packet):
68    name = "PPI Packet Header"
69    fields_desc = [ ByteField('pph_version', 0),
70                    ByteField('pph_flags', 0),
71                    FieldLenField('pph_len', None, length_of="PPIFieldHeaders", fmt="<H", adjust=lambda p,x:x+8 ),
72                    LEIntField('dlt', None),
73                    PacketListField("PPIFieldHeaders", [],  _PPIGuessPayloadClass, length_from=lambda p:p.pph_len-8,) ]
74    def guess_payload_class(self,payload):
75        return conf.l2types.get(self.dlt, Packet.guess_payload_class(self, payload))
76
77#Register PPI
78addPPIType("default", PPIGenericFldHdr)
79
80conf.l2types.register(192, PPI)
81conf.l2types.register_num2layer(192, PPI)
82
83bind_layers(PPI, Dot11, dlt=conf.l2types.get(Dot11))
84bind_layers(Dot11, PPI)
85bind_layers(PPI, Ether, dlt=conf.l2types.get(Ether))
86bind_layers(Dot11, Ether)
87