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 LISPGPE-encapsulated packet on the other interface and verifies received
33 from scapy.all import bind_layers, Packet
34 from scapy.fields import FlagsField, BitField, XBitField, IntField
35 from scapy.layers.inet import ICMP, IP, UDP
36 from scapy.layers.inet6 import ICMPv6EchoRequest
37 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6MLReport2, ICMPv6ND_RA
38 from scapy.layers.l2 import Ether
39 from scapy.packet import Raw
41 from ..PacketVerifier import RxQueue, TxQueue
42 from ..TrafficScriptArg import TrafficScriptArg
43 from ..ValidIp import valid_ipv4, valid_ipv6
46 class LispGPEHeader(Packet):
47 """Scapy header for the Lisp GPE Layer."""
49 name = "Lisp GPE Header"
52 u"flags", None, 8, [u"N", u"L", u"E", u"V", u"I", u"P", u"R", u"O"]
54 BitField(u"version", 0, size=2),
55 BitField(u"reserved", 0, size=14),
56 XBitField(u"next_protocol", 0, size=8),
57 IntField(u"instance_id/locator_status_bits", 0)
60 def guess_payload_class(self, payload):
63 0x2: LispGPEInnerIPv6,
64 0x3: LispGPEInnerEther,
67 return protocol[self.next_protocol]
70 class LispGPEInnerIP(IP):
71 """Scapy inner LISP GPE layer for IPv4-in-IPv4."""
73 name = u"Lisp GPE Inner Layer - IPv4"
76 class LispGPEInnerIPv6(IPv6):
77 """Scapy inner LISP GPE layer for IPv6-in-IPv6."""
79 name = u"Lisp GPE Inner Layer - IPv6"
82 class LispGPEInnerEther(Ether):
83 """Scapy inner LISP GPE layer for Lisp-L2."""
85 name = u"Lisp GPE Inner Layer - Ethernet"
88 class LispGPEInnerNSH(Packet):
89 """Scapy inner LISP GPE layer for Lisp-NSH.
91 Parsing not implemented.
96 """Send IP ICMP packet from one traffic generator interface to the other.
98 :raises RuntimeError: If the received packet is not correct."""
100 args = TrafficScriptArg(
102 u"tg_src_mac", u"tg_dst_mac", u"src_ip", u"dst_ip", u"dut_if1_mac",
103 u"dut_if2_mac", u"src_rloc", u"dst_rloc"
108 tx_src_mac = args.get_arg(u"tg_src_mac")
109 tx_dst_mac = args.get_arg(u"dut_if1_mac")
110 rx_dst_mac = args.get_arg(u"tg_dst_mac")
111 rx_src_mac = args.get_arg(u"dut_if2_mac")
112 src_ip = args.get_arg(u"src_ip")
113 dst_ip = args.get_arg(u"dst_ip")
114 src_rloc = args.get_arg(u"src_rloc")
115 dst_rloc = args.get_arg(u"dst_rloc")
116 tx_if = args.get_arg(u"tx_if")
117 rx_if = args.get_arg(u"rx_if")
118 ot_mode = args.get_arg(u"ot_mode")
123 pkt_raw = Ether(src=tx_src_mac, dst=tx_dst_mac)
125 if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
126 pkt_raw /= IP(src=src_ip, dst=dst_ip)
129 elif valid_ipv6(src_ip) and valid_ipv6(dst_ip):
130 pkt_raw /= IPv6(src=src_ip, dst=dst_ip)
131 pkt_raw /= ICMPv6EchoRequest()
134 raise ValueError(u"IP not in correct format")
136 bind_layers(UDP, LispGPEHeader, dport=4341)
139 sent_packets = list()
140 sent_packets.append(pkt_raw)
145 ether = rxq.recv(2, ignore=sent_packets)
150 raise RuntimeError(u"ICMP echo Rx timeout")
152 if ether.haslayer(ICMPv6ND_NS):
153 # read another packet in the queue if the current one is ICMPv6ND_NS
155 if ether.haslayer(ICMPv6ND_RA):
156 # read another packet in the queue if the current one is ICMPv6ND_RA
158 elif ether.haslayer(ICMPv6MLReport2):
159 # read another packet in the queue if the current one is
163 # otherwise process the current packet
166 if rx_dst_mac == ether[Ether].dst and rx_src_mac == ether[Ether].src:
167 print(u"MAC addresses match.")
169 raise RuntimeError(f"Matching packet unsuccessful: {ether!r}")
173 if ot_mode == u"6to4":
174 if not isinstance(ip, IP):
175 raise RuntimeError(f"Not an IP packet received {ip!r}")
176 elif ot_mode == u"4to6":
177 if not isinstance(ip, IPv6):
178 raise RuntimeError(f"Not an IP packet received {ip!r}")
179 elif not isinstance(ip, ip_format):
180 raise RuntimeError(f"Not an IP packet received {ip!r}")
182 lisp = ether.getlayer(LispGPEHeader).underlayer
184 raise RuntimeError(u"Lisp layer not present or parsing failed.")
186 # Compare data from packets
187 if src_ip == lisp.src:
188 print(u"Source IP matches source EID.")
191 f"Matching Src IP unsuccessful: {src_ip} != {lisp.src}"
194 if dst_ip == lisp.dst:
195 print(u"Destination IP matches destination EID.")
198 f"Matching Dst IP unsuccessful: {dst_ip} != {lisp.dst}"
201 if src_rloc == ip.src:
202 print(u"Source RLOC matches configuration.")
205 f"Matching Src RLOC unsuccessful: {src_rloc} != {ip.src}"
208 if dst_rloc == ip.dst:
209 print(u"Destination RLOC matches configuration.")
212 f"Matching dst RLOC unsuccessful: {dst_rloc} != {ip.dst}"
218 if __name__ == u"__main__":