CSIT-563: HC Test: improved Lisp test coverage
[csit.git] / resources / traffic_scripts / send_icmp_type_code.py
1 #!/usr/bin/env python
2 # Copyright (c) 2017 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Traffic script that sends an IP ICMPv4/ICMPv6 packet from one interface to
16 the other one. Dot1q or Dot1ad tagging of the ethernet frame can be set.
17 """
18
19 import sys
20 import ipaddress
21
22 from scapy.layers.inet import ICMP, IP
23 from scapy.layers.l2 import Ether
24 from scapy.layers.inet6 import ICMPv6EchoRequest
25 from scapy.layers.inet6 import IPv6
26
27 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
28 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
29
30
31 def valid_ipv4(ip):
32     """Check if IP address has the correct IPv4 address format.
33
34     :param ip: IP address.
35     :type ip: str
36     :returns: True in case of correct IPv4 address format,
37     otherwise return false.
38     :rtype: bool
39     """
40     try:
41         ipaddress.IPv4Address(unicode(ip))
42         return True
43     except (AttributeError, ipaddress.AddressValueError):
44         return False
45
46
47 def valid_ipv6(ip):
48     """Check if IP address has the correct IPv6 address format.
49
50     :param ip: IP address.
51     :type ip: str
52     :returns: True in case of correct IPv6 address format,
53     otherwise return false.
54     :rtype: bool
55     """
56     try:
57         ipaddress.IPv6Address(unicode(ip))
58         return True
59     except (AttributeError, ipaddress.AddressValueError):
60         return False
61
62
63 def main():
64     """Send IP ICMPv4/ICMPv6 packet from one traffic generator interface to
65     the other one."""
66
67     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
68                             'icmp_type', 'icmp_code'])
69
70     src_mac = args.get_arg('src_mac')
71     dst_mac = args.get_arg('dst_mac')
72     src_ip = args.get_arg('src_ip')
73     dst_ip = args.get_arg('dst_ip')
74
75     tx_if = args.get_arg('tx_if')
76     rx_if = args.get_arg('rx_if')
77
78     icmp_type = int(args.get_arg('icmp_type'))
79     icmp_code = int(args.get_arg('icmp_code'))
80
81     rxq = RxQueue(rx_if)
82     txq = TxQueue(tx_if)
83
84     sent_packets = []
85
86     # Create empty ip ICMP packet and add padding before sending
87     if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
88         pkt_raw = (Ether(src=src_mac, dst=dst_mac) /
89                    IP(src=src_ip, dst=dst_ip) /
90                    ICMP(code=icmp_code, type=icmp_type))
91         ip_format = 'IP'
92         icmp_format = 'ICMP'
93     elif valid_ipv6(src_ip) and valid_ipv6(dst_ip):
94         pkt_raw = (Ether(src=src_mac, dst=dst_mac) /
95                    IPv6(src=src_ip, dst=dst_ip) /
96                    ICMPv6EchoRequest(code=icmp_code, type=icmp_type))
97         ip_format = 'IPv6'
98         icmp_format = 'ICMPv6'
99     else:
100         raise ValueError("IP(s) not in correct format")
101
102     # Send created packet on one interface and receive on the other
103     sent_packets.append(pkt_raw)
104     txq.send(pkt_raw)
105
106     ether = rxq.recv(2)
107
108     # Check whether received packet contains layers Ether, IP and ICMP
109     if ether is None:
110         raise RuntimeError('ICMP echo Rx timeout')
111
112     if not ether.haslayer(ip_format):
113         raise RuntimeError('Not an IP packet received {0}'
114                            .format(ether.__repr__()))
115
116     # Cannot use haslayer for ICMPv6, every type of ICMPv6 is a separate layer
117     # Next header value of 58 means the next header is ICMPv6
118     if not ether.haslayer(icmp_format) and ether[ip_format].nh != 58:
119         raise RuntimeError('Not an ICMP packet received {0}'
120                            .format(ether.__repr__()))
121
122     sys.exit(0)
123
124 if __name__ == "__main__":
125     main()