Report: Add data
[csit.git] / resources / traffic_scripts / icmpv6_echo.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 ICMPv6 echo test."""
17
18 import sys
19 import logging
20
21 # pylint: disable=no-name-in-module
22 # pylint: disable=import-error
23 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
24
25 from scapy.all import Ether
26 from scapy.layers.inet6 import IPv6, ICMPv6ND_NA, ICMPv6ND_NS
27 from scapy.layers.inet6 import ICMPv6NDOptDstLLAddr
28 from scapy.layers.inet6 import ICMPv6EchoRequest, ICMPv6EchoReply
29
30 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
31 from resources.libraries.python.PacketVerifier import checksum_equal
32 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
33
34
35 def main():
36     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip'])
37
38     rxq = RxQueue(args.get_arg('rx_if'))
39     txq = TxQueue(args.get_arg('tx_if'))
40
41     src_mac = args.get_arg('src_mac')
42     dst_mac = args.get_arg('dst_mac')
43     src_ip = args.get_arg('src_ip')
44     dst_ip = args.get_arg('dst_ip')
45     echo_id = 0xa
46     echo_seq = 0x1
47
48     sent_packets = []
49
50     # send ICMPv6 neighbor advertisement message
51     pkt_send = (Ether(src=src_mac, dst='ff:ff:ff:ff:ff:ff') /
52                 IPv6(src=src_ip, dst='ff02::1:ff00:2') /
53                 ICMPv6ND_NA(tgt=src_ip, R=0) /
54                 ICMPv6NDOptDstLLAddr(lladdr=src_mac))
55     sent_packets.append(pkt_send)
56     txq.send(pkt_send)
57
58     # send ICMPv6 echo request
59     pkt_send = (Ether(src=src_mac, dst=dst_mac) /
60                 IPv6(src=src_ip, dst=dst_ip) /
61                 ICMPv6EchoRequest(id=echo_id, seq=echo_seq))
62     sent_packets.append(pkt_send)
63     txq.send(pkt_send)
64
65     # receive ICMPv6 echo reply
66     while True:
67         ether = rxq.recv(2, sent_packets)
68         if ether is None:
69             raise RuntimeError('ICMPv6 echo reply Rx timeout')
70
71         if ether.haslayer(ICMPv6ND_NS):
72             # read another packet in the queue if the current one is ICMPv6ND_NS
73             continue
74         else:
75             # otherwise process the current packet
76             break
77
78     if not ether.haslayer(IPv6):
79         raise RuntimeError('Unexpected packet with no IPv6 received {0}'.
80                            format(ether.__repr__()))
81
82     ipv6 = ether[IPv6]
83
84     if not ipv6.haslayer(ICMPv6EchoReply):
85         raise RuntimeError('Unexpected packet with no ICMPv6 echo reply '
86                            'received {0}'.format(ipv6.__repr__()))
87
88     icmpv6 = ipv6[ICMPv6EchoReply]
89
90     # check identifier and sequence number
91     if icmpv6.id != echo_id or icmpv6.seq != echo_seq:
92         raise RuntimeError('Invalid ICMPv6 echo reply received ID {0} seq {1} '
93                            'should be ID {2} seq {3}'.
94                            format(icmpv6.id, icmpv6.seq, echo_id, echo_seq))
95
96     # verify checksum
97     cksum = icmpv6.cksum
98     del icmpv6.cksum
99     tmp = ICMPv6EchoReply(str(icmpv6))
100     if not checksum_equal(tmp.cksum, cksum):
101         raise RuntimeError('Invalid checksum {0} should be {1}'.
102                            format(cksum, tmp.cksum))
103
104     sys.exit(0)
105
106
107 if __name__ == "__main__":
108     main()