CSIT-265: Switched Port Analyzer Mirroring (SPAN) - L2
[csit.git] / resources / traffic_scripts / send_icmp_check_arp.py
1 #!/usr/bin/env python
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:
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 ICMP packet
16 from one interface and expects ARP on the other one.
17 """
18
19 import sys
20 import ipaddress
21
22 from scapy.all import Ether
23 from scapy.layers.inet import ICMP, IP
24
25 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
26 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
27
28
29 def valid_ipv4(ip):
30     """Check if IP address has the correct IPv4 address format.
31
32     :param ip: IP address.
33     :type ip: str
34     :return: True in case of correct IPv4 address format,
35              otherwise return False.
36     :rtype: bool
37     """
38     try:
39         ipaddress.IPv4Address(unicode(ip))
40         return True
41     except (AttributeError, ipaddress.AddressValueError):
42         return False
43
44
45 def main():
46     """Send IP ICMP packet from one traffic generator interface and expects
47      ARP on the other."""
48     args = TrafficScriptArg(
49         ['tx_dst_mac', 'rx_src_mac', 'tx_src_ip', 'tx_dst_ip', 'rx_arp_src_ip',
50          'rx_arp_dst_ip'])
51
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')
61
62     rxq = RxQueue(rx_if)
63     txq = TxQueue(tx_if)
64
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()
68
69     # Send created packet on one interface and receive on the other
70     txq.send(pkt_raw)
71
72     ether = rxq.recv(2)
73
74     if ether is None:
75         raise RuntimeError("Ethernet frame Rx timeout")
76
77     if ether.dst == rx_dst_mac:
78         print("Ethernet destination address matched.")
79     else:
80         raise RuntimeError(
81             "Matching ethernet destination address unsuccessful: {0} != {1}".
82                 format(ether.dst, rx_dst_mac))
83
84     if ether.src == rx_src_mac:
85         print("Ethernet source address matched.")
86     else:
87         raise RuntimeError(
88             "Matching ethernet source address unsuccessful: {0} != {1}"
89             .format(ether.src, rx_src_mac))
90
91     # ARP check
92     if ether['ARP'] is not None:
93         print("ARP packet received.")
94     else:
95         raise RuntimeError("Not an ARP packet received {0}"
96                            .format(ether.__repr__()))
97
98     # Compare data from packets
99     if ether['ARP'].op == 1:  # 1 - who-has request
100         print("ARP request matched.")
101     else:
102         raise RuntimeError("Matching ARP request unsuccessful: {0} != {1}"
103                            .format(ether['ARP'].op, 1))
104
105     if ether['ARP'].hwsrc == rx_src_mac:
106         print("Source MAC matched.")
107     else:
108         raise RuntimeError("Matching Source MAC unsuccessful: {0} != {1}"
109                            .format(ether['ARP'].hwsrc, rx_src_mac))
110
111     if ether['ARP'].hwdst == "00:00:00:00:00:00":
112         print("Destination MAC matched.")
113     else:
114         raise RuntimeError("Matching Destination MAC unsuccessful: {0} != {1}"
115                            .format(ether['ARP'].hwdst, "00:00:00:00:00:00"))
116
117     if ether['ARP'].psrc == rx_arp_src_ip:
118         print("Source ARP IP address matched.")
119     else:
120         raise RuntimeError(
121             "Matching Source ARP IP address unsuccessful: {0} != {1}"
122             .format(ether['ARP'].psrc, rx_arp_src_ip))
123
124     if ether['ARP'].pdst == rx_arp_dst_ip:
125         print("Destination ARP IP address matched.")
126     else:
127         raise RuntimeError(
128             "Matching Destination ARP IP address unsuccessful: {0} != {1}"
129             .format(ether['ARP'].pdst, rx_arp_dst_ip))
130
131     sys.exit(0)
132
133
134 if __name__ == "__main__":
135     main()