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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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.
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
27 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
28 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
32 """Check if IP address has the correct IPv4 address format.
34 :param ip: IP address.
36 :returns: True in case of correct IPv4 address format,
37 otherwise return false.
41 ipaddress.IPv4Address(unicode(ip))
43 except (AttributeError, ipaddress.AddressValueError):
48 """Check if IP address has the correct IPv6 address format.
50 :param ip: IP address.
52 :returns: True in case of correct IPv6 address format,
53 otherwise return false.
57 ipaddress.IPv6Address(unicode(ip))
59 except (AttributeError, ipaddress.AddressValueError):
64 """Send IP ICMPv4/ICMPv6 packet from one traffic generator interface to
67 args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
68 'icmp_type', 'icmp_code'])
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')
75 tx_if = args.get_arg('tx_if')
76 rx_if = args.get_arg('rx_if')
78 icmp_type = int(args.get_arg('icmp_type'))
79 icmp_code = int(args.get_arg('icmp_code'))
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))
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))
98 icmp_format = 'ICMPv6'
100 raise ValueError("IP(s) not in correct format")
102 # Send created packet on one interface and receive on the other
103 sent_packets.append(pkt_raw)
108 # Check whether received packet contains layers Ether, IP and ICMP
110 raise RuntimeError('ICMP echo Rx timeout')
112 if not ether.haslayer(ip_format):
113 raise RuntimeError('Not an IP packet received {0}'
114 .format(ether.__repr__()))
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__()))
124 if __name__ == "__main__":