2 # Copyright (c) 2016 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 ICMP packet
16 from one interface and expects ARP on the other one.
22 from scapy.all import Ether
23 from scapy.layers.inet import ICMP, IP
25 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
26 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
30 """Check if IP address has the correct IPv4 address format.
32 :param ip: IP address.
34 :return: True in case of correct IPv4 address format,
35 otherwise return False.
39 ipaddress.IPv4Address(unicode(ip))
41 except (AttributeError, ipaddress.AddressValueError):
46 """Send IP ICMP packet from one traffic generator interface and expects
48 args = TrafficScriptArg(
49 ['tx_dst_mac', 'rx_dst_mac', 'tx_src_ip', 'tx_dst_ip', 'rx_arp_src_ip',
52 tx_dst_mac = args.get_arg('tx_dst_mac')
53 rx_dst_mac = args.get_arg('rx_dst_mac')
54 src_ip = args.get_arg('tx_src_ip')
55 dst_ip = args.get_arg('tx_dst_ip')
56 tx_if = args.get_arg('tx_if')
57 rx_if = args.get_arg('rx_if')
58 rx_arp_src_ip = args.get_arg('rx_arp_src_ip')
59 rx_arp_dst_ip = args.get_arg('rx_arp_dst_ip')
64 # Create empty IP ICMP packet
65 if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
66 pkt_raw = Ether(dst=tx_dst_mac) / IP(src=src_ip, dst=dst_ip) / ICMP()
68 # Send created packet on one interface and receive on the other
74 raise RuntimeError("Ethernet frame Rx timeout")
77 if ether['ARP'] is not None:
78 print("ARP packet received.")
80 raise RuntimeError("Not an ARP packet received {0}"
81 .format(ether.__repr__()))
83 # Compare data from packets
84 if ether['ARP'].op == 1: # 1 - who-has request
85 print("ARP request matched.")
87 raise RuntimeError("Matching ARP request unsuccessful: {0}"
88 .format(ether.__repr__()))
90 if ether['ARP'].hwsrc == rx_dst_mac:
91 print("Source MAC matched.")
93 raise RuntimeError("Matching Source MAC unsuccessful: {0}"
94 .format(ether.__repr__()))
96 if ether['ARP'].hwdst == "00:00:00:00:00:00":
97 print("Destination MAC matched.")
99 raise RuntimeError("Matching Destination MAC unsuccessful: {0}"
100 .format(ether.__repr__()))
102 if ether['ARP'].psrc == rx_arp_src_ip:
103 print("Source ARP IP address matched.")
105 raise RuntimeError("Matching Source ARP IP address unsuccessful: {0}"
106 .format(ether.__repr__()))
108 if ether['ARP'].pdst == rx_arp_dst_ip:
109 print("Destination ARP IP address matched.")
111 raise RuntimeError("Matching Destination ARP IP address unsuccessful: "
112 "{0}".format(ether.__repr__()))
117 if __name__ == "__main__":