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_src_mac', 'tx_src_ip', 'tx_dst_ip', 'rx_arp_src_ip',
52 tx_dst_mac = args.get_arg('tx_dst_mac')
53 rx_src_mac = args.get_arg('rx_src_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_dst_mac = 'ff:ff:ff:ff:ff:ff'
59 rx_arp_src_ip = args.get_arg('rx_arp_src_ip')
60 rx_arp_dst_ip = args.get_arg('rx_arp_dst_ip')
65 # Create empty IP ICMP packet
66 if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
67 pkt_raw = Ether(dst=tx_dst_mac) / IP(src=src_ip, dst=dst_ip) / ICMP()
69 # Send created packet on one interface and receive on the other
75 raise RuntimeError("Ethernet frame Rx timeout")
77 if ether.dst == rx_dst_mac:
78 print("Ethernet destination address matched.")
81 "Matching ethernet destination address unsuccessful: {0} != {1}".
82 format(ether.dst, rx_dst_mac))
84 if ether.src == rx_src_mac:
85 print("Ethernet source address matched.")
88 "Matching ethernet source address unsuccessful: {0} != {1}"
89 .format(ether.src, rx_src_mac))
92 if ether['ARP'] is not None:
93 print("ARP packet received.")
95 raise RuntimeError("Not an ARP packet received {0}"
96 .format(ether.__repr__()))
98 # Compare data from packets
99 if ether['ARP'].op == 1: # 1 - who-has request
100 print("ARP request matched.")
102 raise RuntimeError("Matching ARP request unsuccessful: {0} != {1}"
103 .format(ether['ARP'].op, 1))
105 if ether['ARP'].hwsrc == rx_src_mac:
106 print("Source MAC matched.")
108 raise RuntimeError("Matching Source MAC unsuccessful: {0} != {1}"
109 .format(ether['ARP'].hwsrc, rx_src_mac))
111 if ether['ARP'].hwdst == "00:00:00:00:00:00":
112 print("Destination MAC matched.")
114 raise RuntimeError("Matching Destination MAC unsuccessful: {0} != {1}"
115 .format(ether['ARP'].hwdst, "00:00:00:00:00:00"))
117 if ether['ARP'].psrc == rx_arp_src_ip:
118 print("Source ARP IP address matched.")
121 "Matching Source ARP IP address unsuccessful: {0} != {1}"
122 .format(ether['ARP'].psrc, rx_arp_src_ip))
124 if ether['ARP'].pdst == rx_arp_dst_ip:
125 print("Destination ARP IP address matched.")
128 "Matching Destination ARP IP address unsuccessful: {0} != {1}"
129 .format(ether['ARP'].pdst, rx_arp_dst_ip))
134 if __name__ == "__main__":