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 ICMP/ICMPv6 packet out one interface, receives
27 a LISP-encapsulated packet on the other interface and verifies received packet.
32 from scapy.all import bind_layers, Packet
33 from scapy.fields import FlagsField, BitField, IntField
34 from scapy.layers.inet import ICMP, IP, UDP
35 from scapy.layers.inet6 import ICMPv6EchoRequest
36 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6MLReport2, ICMPv6ND_RA
37 from scapy.layers.l2 import Ether
38 from scapy.packet import Raw
40 from ..PacketVerifier import RxQueue, TxQueue
41 from ..TrafficScriptArg import TrafficScriptArg
42 from ..ValidIp import valid_ipv4, valid_ipv6
45 class LispHeader(Packet):
46 """Scapy header for the LISP Layer."""
51 u"flags", None, 8, [u"N", u"L", u"E", u"V", u"I", u"", u"", u""]
53 BitField(u"nonce/map_version", 0, size=24),
54 IntField(u"instance_id/locator_status_bits", 0)]
57 class LispInnerIP(IP):
58 """Scapy inner LISP layer for IPv4-in-IPv4."""
60 name = u"Lisp Inner Layer - IPv4"
63 class LispInnerIPv6(IPv6):
64 """Scapy inner LISP layer for IPv6-in-IPv6."""
66 name = u"Lisp Inner Layer - IPv6"
70 """Send IP ICMP packet from one traffic generator interface to the other.
72 :raises RuntimeError: If the received packet is not correct.
75 args = TrafficScriptArg(
77 u"tg_src_mac", u"tg_dst_mac", u"src_ip", u"dst_ip", u"dut_if1_mac",
78 u"dut_if2_mac", u"src_rloc", u"dst_rloc"
83 tx_src_mac = args.get_arg(u"tg_src_mac")
84 tx_dst_mac = args.get_arg(u"dut_if1_mac")
85 rx_dst_mac = args.get_arg(u"tg_dst_mac")
86 rx_src_mac = args.get_arg(u"dut_if2_mac")
87 src_ip = args.get_arg(u"src_ip")
88 dst_ip = args.get_arg(u"dst_ip")
89 src_rloc = args.get_arg(u"src_rloc")
90 dst_rloc = args.get_arg(u"dst_rloc")
91 tx_if = args.get_arg(u"tx_if")
92 rx_if = args.get_arg(u"rx_if")
93 ot_mode = args.get_arg(u"ot_mode")
98 pkt_raw = Ether(src=tx_src_mac, dst=tx_dst_mac)
100 if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
101 pkt_raw /= IP(src=src_ip, dst=dst_ip)
104 lisp_layer = LispInnerIP
105 elif valid_ipv6(src_ip) and valid_ipv6(dst_ip):
106 pkt_raw /= IPv6(src=src_ip, dst=dst_ip)
107 pkt_raw /= ICMPv6EchoRequest()
109 lisp_layer = LispInnerIPv6
111 raise ValueError(u"IP not in correct format")
113 bind_layers(UDP, LispHeader, dport=4341)
114 bind_layers(LispHeader, lisp_layer)
117 sent_packets = list()
118 sent_packets.append(pkt_raw)
123 ether = rxq.recv(2, ignore=sent_packets)
128 raise RuntimeError(u"ICMP echo Rx timeout")
130 if ether.haslayer(ICMPv6ND_NS):
131 # read another packet in the queue if the current one is ICMPv6ND_NS
133 elif ether.haslayer(ICMPv6MLReport2):
134 # read another packet in the queue if the current one is
137 elif ether.haslayer(ICMPv6ND_RA):
138 # read another packet in the queue if the current one is
142 # otherwise process the current packet
145 if rx_dst_mac == ether[Ether].dst and rx_src_mac == ether[Ether].src:
146 print(u"MAC addresses match.")
148 raise RuntimeError(f"Matching packet unsuccessful: {ether!r}")
152 if ot_mode == u"6to4":
153 if not isinstance(ip, IP):
154 raise RuntimeError(f"Not an IP packet received {ip!r}")
155 elif ot_mode == u"4to6":
156 if not isinstance(ip, IPv6):
157 raise RuntimeError(f"Not an IP packet received {ip!r}")
158 elif not isinstance(ip, ip_format):
159 raise RuntimeError(f"Not an IP packet received {ip!r}")
161 lisp = ether.getlayer(lisp_layer)
163 raise RuntimeError("Lisp layer not present or parsing failed.")
165 # Compare data from packets
166 if src_ip == lisp.src:
167 print(u"Source IP matches source EID.")
170 f"Matching Src IP unsuccessful: {src_ip} != {lisp.src}"
173 if dst_ip == lisp.dst:
174 print(u"Destination IP matches destination EID.")
177 f"Matching Dst IP unsuccessful: {dst_ip} != {lisp.dst}"
180 if src_rloc == ip.src:
181 print(u"Source RLOC matches configuration.")
184 f"Matching Src RLOC unsuccessful: {src_rloc} != {ip.src}"
187 if dst_rloc == ip.dst:
188 print(u"Destination RLOC matches configuration.")
191 f"Matching dst RLOC unsuccessful: {dst_rloc} != {ip.dst}"
197 if __name__ == u"__main__":