License: Wrap GPL block to 80 characters
[csit.git] / GPL / traffic_scripts / srv6_encap.py
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2021 Cisco and/or its affiliates.
4 #
5 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 #
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:
11 #
12 #     http://www.apache.org/licenses/LICENSE-2.0
13 #     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14 #
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
18 # Apache 2.
19 #
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.
25
26 """Traffic script for SRv6 verification."""
27
28 import sys
29
30 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, IPv6ExtHdrSegmentRouting,\
31     ipv6nh
32 from scapy.layers.l2 import Ether
33 from scapy.packet import Raw
34
35 from .PacketVerifier import RxQueue, TxQueue
36 from .TrafficScriptArg import TrafficScriptArg
37
38
39 def check_srv6(
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.
43
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
55     :type src_mac: str
56     :type dst_mac: str
57     :type src_ip: str
58     :type dst_ip: str
59     :type dir_srcsid: str
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.
66     """
67     if pkt_recv[Ether].src != src_mac:
68         raise RuntimeError(
69             f"Received frame has invalid source MAC address: "
70             f"{pkt_recv[Ether].src} should be: {src_mac}"
71         )
72     if pkt_recv[Ether].dst != dst_mac:
73         raise RuntimeError(
74             f"Received frame has invalid destination MAC address: "
75             f"{pkt_recv[Ether].dst} should be: {dst_mac}"
76         )
77     if not pkt_recv.haslayer(IPv6):
78         raise RuntimeError(
79             f"Not an IPv6 packet received: {pkt_recv!r}"
80         )
81     ip6_pkt = pkt_recv[IPv6]
82     if ip6_pkt.src != dir_srcsid:
83         raise RuntimeError(
84             f"Outer IPv6 packet has invalid source address: "
85             f"{ip6_pkt.src} should be: {dir_srcsid}"
86         )
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]:
93         raise RuntimeError(
94             f"Outer IPv6 packet has invalid destination address: "
95             f"{ip6_pkt.dst} should be: {dir_dstsids[segleft]}"
96         )
97     if dir_dstsid2 == u"None":
98         if ip6_pkt.haslayer(IPv6ExtHdrSegmentRouting):
99             raise RuntimeError(
100                 f"Segment Routing Header in received packet: {pkt_recv!r}"
101             )
102         if ip6_pkt.nh != 41:  # ipv6nh[41] == IPv6
103             raise RuntimeError(
104                 f"Outer IPv6 packet has invalid next header: "
105                 f"{ip6_pkt.nh} should be: 41 ({ipv6nh[41]})"
106             )
107         ip6_pkt = ip6_pkt[IPv6][1]
108     else:
109         if not pkt_recv.haslayer(IPv6ExtHdrSegmentRouting):
110             raise RuntimeError(
111                 f"No Segment Routing Header in received packet: {pkt_recv!r}"
112             )
113         if ip6_pkt.nh != 43:  # ipv6nh[43] == Routing Header
114             raise RuntimeError(
115                 f"Outer IPv6 packet has invalid next header: "
116                 f"{pkt_recv[IPv6][0].nh} should be: 43 ({ipv6nh[43]})"
117             )
118         ip6_pkt = ip6_pkt[IPv6ExtHdrSegmentRouting]
119         if ip6_pkt.addresses != dir_dstsids:
120             raise RuntimeError(
121                 f"Segment Routing Header has invalid addresses: "
122                 f"{ip6_pkt.addresses} should be: {dir_dstsids}"
123             )
124         if ip6_pkt.segleft != segleft:
125             raise RuntimeError(
126                 f"Segment Routing Header has invalid segleft value: "
127                 f"{ip6_pkt.segleft} should be: {segleft}"
128             )
129         if ip6_pkt.nh != 41:  # ipv6nh[41] == IPv6
130             raise RuntimeError(
131                 f"Segment Routing Header has invalid next header: "
132                 f"{ip6_pkt.nh} should be: 41 ({ipv6nh[41]})"
133             )
134         ip6_pkt = ip6_pkt[IPv6]
135     if ip6_pkt.src != src_ip:
136         raise RuntimeError(
137             f"Inner IPv6 packet has invalid source address: "
138             f"{ip6_pkt.src} should be: {src_ip}"
139         )
140     if ip6_pkt.dst != dst_ip:
141         raise RuntimeError(
142             f"Inner IPv6 packet has invalid destination address: "
143             f"{ip6_pkt.dst} should be: {dst_ip}"
144         )
145     if ip6_pkt.nh != 59:  # ipv6nh[59] == No Next Header
146         raise RuntimeError(
147             f"Inner IPv6 packet has invalid next header: "
148             f"{ip6_pkt.nh} should be: 59 ({ipv6nh[59]})"
149         )
150
151
152 def check_ip(pkt_recv, src_mac, dst_mac, src_ip, dst_ip):
153     """Check received IPv6 packet.
154
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
161     :type src_mac: str
162     :type dst_mac: str
163     :type src_ip: str
164     :type dst_ip: str
165     :raises RuntimeError: If received packet is invalid.
166     """
167     if pkt_recv[Ether].src != src_mac:
168         raise RuntimeError(
169             f"Received frame has invalid source MAC address: "
170             f"{pkt_recv[Ether].src} should be: {src_mac}"
171         )
172
173     if pkt_recv[Ether].dst != dst_mac:
174         raise RuntimeError(
175             f"Received frame has invalid destination MAC address: "
176             f"{pkt_recv[Ether].dst} should be: {dst_mac}"
177         )
178
179     if not pkt_recv.haslayer(IPv6):
180         raise RuntimeError(
181             f"Not an {IPv6.name} packet received: {pkt_recv!r}"
182         )
183
184     if pkt_recv[IPv6].dst != dst_ip:
185         raise RuntimeError(
186             f"Received packet has invalid destination address: "
187             f"{pkt_recv[IPv6.name].dst} should be: {dst_ip}"
188         )
189
190     if pkt_recv[IPv6].src != src_ip:
191         raise RuntimeError(
192             f"Received packet has invalid destination address: "
193             f"{pkt_recv[IPv6.name].dst} should be: {src_ip}"
194         )
195
196     if pkt_recv[IPv6].nh != 59:  # ipv6nh[59] == No Next Header
197         raise RuntimeError(
198             f"Received IPv6 packet has invalid next header: "
199             f"{IPv6.nh} should be: 59 ({ipv6nh[59]})"
200         )
201
202
203 def main():
204     """Send, receive and check IPv6 and IPv6ExtHdrSegmentRouting packets."""
205
206     args = TrafficScriptArg(
207         [
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"
212         ]
213     )
214
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"))
219
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")
226
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")
237
238     ip_pkt = IPv6(src=src_ip, dst=dst_ip)
239
240     sent_packets = list()
241     tx_pkt_send = (Ether(src=tx_src_mac, dst=tx_dst_mac) / ip_pkt)
242     tx_pkt_send /= Raw()
243     size_limit = 78
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)
248
249     while True:
250         rx_pkt_recv = rx_rxq.recv(2)
251
252         if rx_pkt_recv is None:
253             raise RuntimeError(f"{IPv6.name} packet Rx timeout")
254
255         if rx_pkt_recv.haslayer(ICMPv6ND_NS):
256             # read another packet in the queue if the current one is ICMPv6ND_NS
257             continue
258         else:
259             # otherwise process the current packet
260             break
261
262     check_srv6(
263         rx_pkt_recv, rx_src_mac, rx_dst_mac, src_ip, dst_ip, dir0_srcsid,
264         dir0_dstsid1, dir0_dstsid2, dir0_dstsid3, 1
265     )
266
267     ip_pkt = IPv6(src=dst_ip, dst=src_ip)
268     ip_pkt /= Raw()
269     if len(ip_pkt) < (size_limit - 14):
270         ip_pkt[Raw].load += (b"\0" * (size_limit - 14 - len(ip_pkt)))
271
272     rx_pkt_send = (
273             Ether(src=rx_dst_mac, dst=rx_src_mac) /
274             IPv6(src=dir1_srcsid, dst=dir1_dstsid1) /
275             IPv6ExtHdrSegmentRouting(
276                 segleft=1 if dir1_dstsid3 == u"None" else 2,
277                 lastentry=1 if dir1_dstsid3 == u"None" else 2,
278                 addresses=[dir1_dstsid2, dir1_dstsid1]
279                 if dir1_dstsid3 == u"None"
280                 else [dir1_dstsid3, dir1_dstsid2, dir1_dstsid1]
281             ) /
282             ip_pkt
283     ) if dir1_dstsid2 != u"None" else (
284             Ether(src=rx_dst_mac, dst=rx_src_mac) /
285             IPv6(src=dir1_srcsid, dst=dir1_dstsid1) /
286             ip_pkt
287     )
288     rx_txq.send(rx_pkt_send)
289
290     while True:
291         tx_pkt_recv = tx_rxq.recv(2, ignore=sent_packets)
292
293         if tx_pkt_recv is None:
294             raise RuntimeError(f"{IPv6.name} packet Rx timeout")
295
296         if tx_pkt_recv.haslayer(ICMPv6ND_NS):
297             # read another packet in the queue if the current one is ICMPv6ND_NS
298             continue
299         else:
300             # otherwise process the current packet
301             break
302
303     if decap == u"True":
304         check_ip(tx_pkt_recv, tx_dst_mac, tx_src_mac, dst_ip, src_ip)
305     else:
306         check_srv6(
307             tx_pkt_recv, tx_dst_mac, tx_src_mac, dst_ip, src_ip, dir1_srcsid,
308             dir1_dstsid1, dir1_dstsid2, dir1_dstsid3, 2,
309             bool(static_proxy == u"True")
310         )
311
312     sys.exit(0)
313
314
315 if __name__ == u"__main__":
316     main()