1#! /usr/bin/env python
2
3# This file is part of Scapy
4# Scapy is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 2 of the License, or
7# any later version.
8#
9# Scapy is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with Scapy. If not, see <http://www.gnu.org/licenses/>.
16
17## Copyright (C) 2009 Adline Stephane <adline.stephane@gmail.com>
18##
19
20# Partial support of RFC3971
21# scapy.contrib.description = SEND (ICMPv6)
22# scapy.contrib.status = loads
23
24from __future__ import absolute_import
25import socket
26
27from scapy.packet import *
28from scapy.fields import *
29from scapy.layers.inet6 import icmp6typescls, _ICMPv6NDGuessPayload, Net6
30
31send_icmp6typescls = { 11: "ICMPv6NDOptCGA",
32                       12: "ICMPv6NDOptRsaSig",
33                       13: "ICMPv6NDOptTmstp",
34                       14: "ICMPv6NDOptNonce"
35                     }
36icmp6typescls.update(send_icmp6typescls)
37
38class HashField(Field):
39    def __init__(self, name, default):
40        Field.__init__(self, name, default, "16s")
41    def h2i(self, pkt, x):
42        if isinstance(x, str):
43            try:
44                x = in6_ptop(x)
45            except socket.error:
46                x = Net6(x)
47        elif isinstance(x, list):
48            x = [Net6(e) for e in x]
49        return x
50    def i2m(self, pkt, x):
51        return inet_pton(socket.AF_INET6, x)
52    def m2i(self, pkt, x):
53        return inet_ntop(socket.AF_INET6, x)
54    def any2i(self, pkt, x):
55        return self.h2i(pkt,x)
56    def i2repr(self, pkt, x):
57        return self.i2h(pkt, x)    # No specific information to return
58
59class ICMPv6NDOptNonce(_ICMPv6NDGuessPayload, Packet):
60    name = "ICMPv6NDOptNonce"
61    fields_desc = [ ByteField("type",14),
62                    FieldLenField("len",None,length_of="data",fmt="B", adjust = lambda pkt,x: (x)/8),
63                    StrLenField("nonce","", length_from = lambda pkt: pkt.len*8-2) ]
64
65class ICMPv6NDOptTmstp(_ICMPv6NDGuessPayload, Packet):
66    name = "ICMPv6NDOptTmstp"
67    fields_desc = [ ByteField("type",13),
68                    ByteField("len",2),
69                    BitField("reserved",0, 48),
70                    LongField("timestamp", None) ]
71
72class ICMPv6NDOptRsaSig(_ICMPv6NDGuessPayload, Packet):
73    name = "ICMPv6NDOptRsaSig"
74    fields_desc = [ ByteField("type",12),
75                    FieldLenField("len",None,length_of="data",fmt="B", adjust = lambda pkt,x: (x)/8),
76                    ShortField("reserved",0),
77                    HashField("key_hash",None),
78                    StrLenField("signature_pad", "", length_from = lambda pkt: pkt.len*8-20) ]
79
80class ICMPv6NDOptCGA(_ICMPv6NDGuessPayload, Packet):
81    name = "ICMPv6NDOptCGA"
82    fields_desc = [ ByteField("type",11),
83                    FieldLenField("len",None,length_of="data",fmt="B", adjust = lambda pkt,x: (x)/8),
84                    ByteField("padlength",0),
85                    ByteField("reserved",0),
86                    StrLenField("CGA_PARAMS", "", length_from = lambda pkt: pkt.len*8 - pkt.padlength - 4),
87                    StrLenField("padding", None, length_from = lambda pkt: pkt.padlength) ]
88
89if __name__ == "__main__":
90    from scapy.all import *
91    interact(mydict=globals(), mybanner="SEND add-on")
92