3 # Copyright (c) 2021 Cisco and/or its affiliates.
5 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 # Licensed under the Apache License 2.0 or
8 # GNU General Public License v2.0 or later; you may not use this file
9 # except in compliance with one of these Licenses. You
10 # may obtain a copy of the Licenses at:
12 # http://www.apache.org/licenses/LICENSE-2.0
13 # https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
15 # Note: If this file is linked with Scapy, which is GPLv2+, your use of it
16 # must be under GPLv2+. If at any point in the future it is no longer linked
17 # with Scapy (or other GPLv2+ licensed software), you are free to choose
20 # Unless required by applicable law or agreed to in writing, software
21 # distributed under the License is distributed on an "AS IS" BASIS,
22 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 # See the License for the specific language governing permissions and
24 # limitations under the License.
26 """Traffic script that sends an IP ICMPv4 or ICMPv6."""
31 from scapy.layers.inet import ICMP, IP
32 from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, ICMPv6EchoReply,\
34 from scapy.layers.l2 import Ether
35 from scapy.packet import Raw
37 from .PacketVerifier import RxQueue, TxQueue
38 from .TrafficScriptArg import TrafficScriptArg
42 """Check if IP address has the correct IPv4 address format.
44 :param ip: IP address.
46 :return: True in case of correct IPv4 address format,
47 otherwise return False.
51 ipaddress.IPv4Address(ip)
53 except (AttributeError, ipaddress.AddressValueError):
58 """Check if IP address has the correct IPv6 address format.
60 :param ip: IP address.
62 :return: True in case of correct IPv6 address format,
63 otherwise return False.
67 ipaddress.IPv6Address(ip)
69 except (AttributeError, ipaddress.AddressValueError):
74 """Send ICMP echo request and wait for ICMP echo reply. It ignores all other
76 args = TrafficScriptArg(
77 [u"dst_mac", u"src_mac", u"dst_ip", u"src_ip", u"timeout"]
80 dst_mac = args.get_arg(u"dst_mac")
81 src_mac = args.get_arg(u"src_mac")
82 dst_ip = args.get_arg(u"dst_ip")
83 src_ip = args.get_arg(u"src_ip")
84 tx_if = args.get_arg(u"tx_if")
85 rx_if = args.get_arg(u"rx_if")
86 timeout = int(args.get_arg(u"timeout"))
93 # Create empty ip ICMP packet
94 if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
98 icmp_type = 0 # echo-reply
99 elif valid_ipv6(src_ip) and valid_ipv6(dst_ip):
101 icmp_req = ICMPv6EchoRequest
102 icmp_resp = ICMPv6EchoReply
103 icmp_type = 0 # Echo Reply
105 raise ValueError(u"IP not in correct format")
108 Ether(src=src_mac, dst=dst_mac) /
109 ip_layer(src=src_ip, dst=dst_ip) /
113 # Send created packet on the interface
114 icmp_request /= Raw()
115 sent_packets.append(icmp_request)
116 txq.send(icmp_request)
118 for _ in range(1000):
120 icmp_reply = rxq.recv(wait_step, ignore=sent_packets)
121 if icmp_reply is None:
124 raise RuntimeError(u"ICMP echo Rx timeout")
126 elif icmp_reply.haslayer(ICMPv6ND_NS):
127 # read another packet in the queue in case of ICMPv6ND_NS packet
130 # otherwise process the current packet
133 if icmp_reply[ip_layer][icmp_resp].type == icmp_type:
134 if icmp_reply[ip_layer].src == dst_ip and \
135 icmp_reply[ip_layer].dst == src_ip:
138 raise RuntimeError(u"Max packet count limit reached")
140 print(u"ICMP echo reply received.")
145 if __name__ == u"__main__":