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 for SRv6 verification."""
30 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, IPv6ExtHdrSegmentRouting,\
31 ipv6nh, ICMPv6MLReport2, ICMPv6ND_RA
32 from scapy.layers.l2 import Ether
33 from scapy.packet import Raw
35 from .PacketVerifier import RxQueue, TxQueue
36 from .TrafficScriptArg import TrafficScriptArg
40 pkt_recv, src_mac, dst_mac, src_ip, dst_ip, dir_srcsid, dir_dstsid1,
41 dir_dstsid2, dir_dstsid3, segleft_corr, static_proxy=False):
42 """Check received SRv6 packet.
44 :param pkt_recv: Received packet to verify.
45 :param src_mac: Source MAC address.
46 :param dst_mac: Destination MAC address.
47 :param src_ip: Source IP/IPv6 address of original IP/IPv6 packet.
48 :param dst_ip: Destination IP/IPv6 address of original IP/IPv6 packet.
49 :param dir_srcsid: Source SID for SR in desired direction.
50 :param dir_dstsid1: Destination SID1 in desired direction.
51 :param dir_dstsid2: Destination SID2 in desired direction.
52 :param dir_dstsid3: Destination SID3 in desired direction.
53 :param segleft_corr: Correction for expected segleft value of SRH.
54 :type pkt_recv: scapy.Ether
60 :type dir_dstsid1: str
61 :type dir_dstsid2: str
62 :type dir_dstsid3: str
63 :type segleft_corr: int
64 :type static_proxy; bool
65 :raises RuntimeError: If received packet is invalid.
67 if pkt_recv[Ether].src != src_mac:
69 f"Received frame has invalid source MAC address: "
70 f"{pkt_recv[Ether].src} should be: {src_mac}"
72 if pkt_recv[Ether].dst != dst_mac:
74 f"Received frame has invalid destination MAC address: "
75 f"{pkt_recv[Ether].dst} should be: {dst_mac}"
77 if not pkt_recv.haslayer(IPv6):
79 f"Not an IPv6 packet received: {pkt_recv!r}"
81 ip6_pkt = pkt_recv[IPv6]
82 if ip6_pkt.src != dir_srcsid:
84 f"Outer IPv6 packet has invalid source address: "
85 f"{ip6_pkt.src} should be: {dir_srcsid}"
87 dir_dstsids = [dir_dstsid2, dir_dstsid1] if dir_dstsid3 == u"None" \
88 else [dir_dstsid3, dir_dstsid2, dir_dstsid1] if not static_proxy \
89 else [dir_dstsid3, dir_dstsid2]
90 segleft = len(dir_dstsids) - segleft_corr if not static_proxy \
91 else len(dir_dstsids) - segleft_corr + 1
92 if ip6_pkt.dst != dir_dstsids[segleft]:
94 f"Outer IPv6 packet has invalid destination address: "
95 f"{ip6_pkt.dst} should be: {dir_dstsids[segleft]}"
97 if dir_dstsid2 == u"None":
98 if ip6_pkt.haslayer(IPv6ExtHdrSegmentRouting):
100 f"Segment Routing Header in received packet: {pkt_recv!r}"
102 if ip6_pkt.nh != 41: # ipv6nh[41] == IPv6
104 f"Outer IPv6 packet has invalid next header: "
105 f"{ip6_pkt.nh} should be: 41 ({ipv6nh[41]})"
107 ip6_pkt = ip6_pkt[IPv6][1]
109 if not pkt_recv.haslayer(IPv6ExtHdrSegmentRouting):
111 f"No Segment Routing Header in received packet: {pkt_recv!r}"
113 if ip6_pkt.nh != 43: # ipv6nh[43] == Routing Header
115 f"Outer IPv6 packet has invalid next header: "
116 f"{pkt_recv[IPv6][0].nh} should be: 43 ({ipv6nh[43]})"
118 ip6_pkt = ip6_pkt[IPv6ExtHdrSegmentRouting]
119 if ip6_pkt.addresses != dir_dstsids:
121 f"Segment Routing Header has invalid addresses: "
122 f"{ip6_pkt.addresses} should be: {dir_dstsids}"
124 if ip6_pkt.segleft != segleft:
126 f"Segment Routing Header has invalid segleft value: "
127 f"{ip6_pkt.segleft} should be: {segleft}"
129 if ip6_pkt.nh != 41: # ipv6nh[41] == IPv6
131 f"Segment Routing Header has invalid next header: "
132 f"{ip6_pkt.nh} should be: 41 ({ipv6nh[41]})"
134 ip6_pkt = ip6_pkt[IPv6]
135 if ip6_pkt.src != src_ip:
137 f"Inner IPv6 packet has invalid source address: "
138 f"{ip6_pkt.src} should be: {src_ip}"
140 if ip6_pkt.dst != dst_ip:
142 f"Inner IPv6 packet has invalid destination address: "
143 f"{ip6_pkt.dst} should be: {dst_ip}"
145 if ip6_pkt.nh != 59: # ipv6nh[59] == No Next Header
147 f"Inner IPv6 packet has invalid next header: "
148 f"{ip6_pkt.nh} should be: 59 ({ipv6nh[59]})"
152 def check_ip(pkt_recv, src_mac, dst_mac, src_ip, dst_ip):
153 """Check received IPv6 packet.
155 :param pkt_recv: Received packet to verify.
156 :param src_mac: Source MAC address.
157 :param dst_mac: Destination MAC address.
158 :param src_ip: Source IP/IPv6 address.
159 :param dst_ip: Destination IP/IPv6 address.
160 :type pkt_recv: scapy.Ether
165 :raises RuntimeError: If received packet is invalid.
167 if pkt_recv[Ether].src != src_mac:
169 f"Received frame has invalid source MAC address: "
170 f"{pkt_recv[Ether].src} should be: {src_mac}"
173 if pkt_recv[Ether].dst != dst_mac:
175 f"Received frame has invalid destination MAC address: "
176 f"{pkt_recv[Ether].dst} should be: {dst_mac}"
179 if not pkt_recv.haslayer(IPv6):
181 f"Not an {IPv6.name} packet received: {pkt_recv!r}"
184 if pkt_recv[IPv6].dst != dst_ip:
186 f"Received packet has invalid destination address: "
187 f"{pkt_recv[IPv6.name].dst} should be: {dst_ip}"
190 if pkt_recv[IPv6].src != src_ip:
192 f"Received packet has invalid destination address: "
193 f"{pkt_recv[IPv6.name].dst} should be: {src_ip}"
196 if pkt_recv[IPv6].nh != 59: # ipv6nh[59] == No Next Header
198 f"Received IPv6 packet has invalid next header: "
199 f"{IPv6.nh} should be: 59 ({ipv6nh[59]})"
204 """Send, receive and check IPv6 and IPv6ExtHdrSegmentRouting packets."""
206 args = TrafficScriptArg(
208 u"tx_src_mac", u"tx_dst_mac", u"rx_src_mac", u"rx_dst_mac",
209 u"src_ip", u"dst_ip", u"dir0_srcsid", u"dir0_dstsid1",
210 u"dir0_dstsid2", u"dir1_srcsid", u"dir1_dstsid1", u"dir1_dstsid2",
211 u"decap", u"dir0_dstsid3", u"dir1_dstsid3", u"static_proxy"
215 tx_txq = TxQueue(args.get_arg(u"tx_if"))
216 tx_rxq = RxQueue(args.get_arg(u"tx_if"))
217 rx_txq = TxQueue(args.get_arg(u"rx_if"))
218 rx_rxq = RxQueue(args.get_arg(u"rx_if"))
220 tx_src_mac = args.get_arg(u"tx_src_mac")
221 tx_dst_mac = args.get_arg(u"tx_dst_mac")
222 rx_src_mac = args.get_arg(u"rx_src_mac")
223 rx_dst_mac = args.get_arg(u"rx_dst_mac")
224 src_ip = args.get_arg(u"src_ip")
225 dst_ip = args.get_arg(u"dst_ip")
227 dir0_srcsid = args.get_arg(u"dir0_srcsid")
228 dir0_dstsid1 = args.get_arg(u"dir0_dstsid1")
229 dir0_dstsid2 = args.get_arg(u"dir0_dstsid2")
230 dir1_srcsid = args.get_arg(u"dir1_srcsid")
231 dir1_dstsid1 = args.get_arg(u"dir1_dstsid1")
232 dir1_dstsid2 = args.get_arg(u"dir1_dstsid2")
233 decap = args.get_arg(u"decap")
234 dir0_dstsid3 = args.get_arg(u"dir0_dstsid3")
235 dir1_dstsid3 = args.get_arg(u"dir1_dstsid3")
236 static_proxy = args.get_arg(u"static_proxy")
238 ip_pkt = IPv6(src=src_ip, dst=dst_ip)
240 sent_packets = list()
241 tx_pkt_send = (Ether(src=tx_src_mac, dst=tx_dst_mac) / ip_pkt)
244 if len(tx_pkt_send) < size_limit:
245 tx_pkt_send[Raw].load += (b"\0" * (size_limit - len(tx_pkt_send)))
246 sent_packets.append(tx_pkt_send)
247 tx_txq.send(tx_pkt_send)
250 rx_pkt_recv = rx_rxq.recv(2)
252 if rx_pkt_recv is None:
253 raise RuntimeError(f"{IPv6.name} packet Rx timeout")
255 if rx_pkt_recv.haslayer(ICMPv6ND_NS):
256 # read another packet in the queue if the current one is ICMPv6ND_NS
258 elif rx_pkt_recv.haslayer(ICMPv6MLReport2):
259 # read another packet in the queue if the current one is
262 elif rx_pkt_recv.haslayer(ICMPv6ND_RA):
263 # read another packet in the queue if the current one is
267 # otherwise process the current packet
271 rx_pkt_recv, rx_src_mac, rx_dst_mac, src_ip, dst_ip, dir0_srcsid,
272 dir0_dstsid1, dir0_dstsid2, dir0_dstsid3, 1
275 ip_pkt = IPv6(src=dst_ip, dst=src_ip)
277 if len(ip_pkt) < (size_limit - 14):
278 ip_pkt[Raw].load += (b"\0" * (size_limit - 14 - len(ip_pkt)))
281 Ether(src=rx_dst_mac, dst=rx_src_mac) /
282 IPv6(src=dir1_srcsid, dst=dir1_dstsid1) /
283 IPv6ExtHdrSegmentRouting(
284 segleft=1 if dir1_dstsid3 == u"None" else 2,
285 lastentry=1 if dir1_dstsid3 == u"None" else 2,
286 addresses=[dir1_dstsid2, dir1_dstsid1]
287 if dir1_dstsid3 == u"None"
288 else [dir1_dstsid3, dir1_dstsid2, dir1_dstsid1]
291 ) if dir1_dstsid2 != u"None" else (
292 Ether(src=rx_dst_mac, dst=rx_src_mac) /
293 IPv6(src=dir1_srcsid, dst=dir1_dstsid1) /
296 rx_txq.send(rx_pkt_send)
299 tx_pkt_recv = tx_rxq.recv(2, ignore=sent_packets)
301 if tx_pkt_recv is None:
302 raise RuntimeError(f"{IPv6.name} packet Rx timeout")
304 if tx_pkt_recv.haslayer(ICMPv6ND_NS):
305 # read another packet in the queue if the current one is ICMPv6ND_NS
307 elif tx_pkt_recv.haslayer(ICMPv6MLReport2):
308 # read another packet in the queue if the current one is
311 elif tx_pkt_recv.haslayer(ICMPv6ND_RA):
312 # read another packet in the queue if the current one is
316 # otherwise process the current packet
320 check_ip(tx_pkt_recv, tx_dst_mac, tx_src_mac, dst_ip, src_ip)
323 tx_pkt_recv, tx_dst_mac, tx_src_mac, dst_ip, src_ip, dir1_srcsid,
324 dir1_dstsid1, dir1_dstsid2, dir1_dstsid3, 2,
325 bool(static_proxy == u"True")
331 if __name__ == u"__main__":