c79b74d76067f6a9ba5de39852b459a5f2a06f9f
[csit.git] / resources / traffic_scripts / ipv6_sweep_ping.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """Traffic script for IPv6 sweep ping."""
17
18 import sys
19 import logging
20 import os
21 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
22 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
23 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
24 from scapy.layers.inet6 import IPv6, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr
25 from scapy.layers.inet6 import ICMPv6EchoRequest, ICMPv6EchoReply
26 from scapy.all import Ether
27
28
29 def main():
30     # start_size - start size of the ICMPv6 echo data
31     # end_size - end size of the ICMPv6 echo data
32     # step - increment step
33     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
34                              'start_size', 'end_size', 'step'])
35
36     rxq = RxQueue(args.get_arg('rx_if'))
37     txq = TxQueue(args.get_arg('tx_if'))
38
39     src_mac = args.get_arg('src_mac')
40     dst_mac = args.get_arg('dst_mac')
41     src_ip = args.get_arg('src_ip')
42     dst_ip = args.get_arg('dst_ip')
43     start_size = int(args.get_arg('start_size'))
44     end_size = int(args.get_arg('end_size'))
45     step = int(args.get_arg('step'))
46     echo_id = 0xa
47     # generate some random data buffer
48     data = bytearray(os.urandom(end_size))
49
50     # send ICMPv6 neighbor advertisement message
51     sent_packets = []
52     pkt_send = (Ether(src=src_mac, dst=dst_mac) /
53                       IPv6(src=src_ip, dst=dst_ip) /
54                       ICMPv6ND_NA(tgt=src_ip, R=0) /
55                       ICMPv6NDOptDstLLAddr(lladdr=src_mac))
56     sent_packets.append(pkt_send)
57     txq.send(pkt_send)
58
59     # send ICMPv6 echo request with incremented data length and receive ICMPv6
60     # echo reply
61     for echo_seq in range(start_size, end_size+1, step):
62         pkt_send = (Ether(src=src_mac, dst=dst_mac) /
63                           IPv6(src=src_ip, dst=dst_ip) /
64                           ICMPv6EchoRequest(id=echo_id, seq=echo_seq,
65                                             data=data[0:echo_seq]))
66         sent_packets.append(pkt_send)
67         txq.send(pkt_send)
68
69         ether = rxq.recv(ignore=sent_packets)
70         if ether is None:
71             rxq._proc.terminate()
72             raise RuntimeError(
73                 'ICMPv6 echo reply seq {0} Rx timeout'.format(echo_seq))
74
75         if not ether.haslayer(IPv6):
76             rxq._proc.terminate()
77             raise RuntimeError(
78                 'Unexpected packet with no IPv6 received {0}'.format(
79                     ether.__repr__()))
80
81         ipv6 = ether['IPv6']
82
83         if not ipv6.haslayer(ICMPv6EchoReply):
84             rxq._proc.terminate()
85             raise RuntimeError(
86                 'Unexpected packet with no IPv6 ICMP received {0}'.format(
87                     ipv6.__repr__()))
88
89         icmpv6 = ipv6['ICMPv6 Echo Reply']
90
91         if icmpv6.id != echo_id or icmpv6.seq != echo_seq:
92             rxq._proc.terminate()
93             raise RuntimeError(
94                 'Invalid ICMPv6 echo reply received ID {0} seq {1} should be ' +
95                 'ID {2} seq {3}, {0}'.format(icmpv6.id, icmpv6.seq, echo_id,
96                                              echo_seq))
97
98         cksum = icmpv6.cksum
99         del icmpv6.cksum
100         tmp = ICMPv6EchoReply(str(icmpv6))
101         if tmp.cksum != cksum:
102             rxq._proc.terminate()
103             raise RuntimeError(
104                 'Invalid checksum {0} should be {1}'.format(cksum, tmp.cksum))
105
106     rxq._proc.terminate()
107     sys.exit(0)
108
109 if __name__ == "__main__":
110     main()