aboutsummaryrefslogtreecommitdiffstats
path: root/GPL/traffic_scripts/srv6_encap.py
blob: ce62d79974e7c99c5d35eae890f13841975b2f75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3

# Copyright (c) 2021 Cisco and/or its affiliates.
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
#
# Licensed under the Apache License 2.0 or
# GNU General Public License v2.0 or later;  you may not use this file
# except in compliance with one of these Licenses. You
# may obtain a copy of the Licenses at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
#
# Note: If this file is linked with Scapy, which is GPLv2+, your use of it
# must be under GPLv2+.  If at any point in the future it is no longer linked
# with Scapy (or other GPLv2+ licensed software), you are free to choose
# Apache 2.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Traffic script for SRv6 verification."""

import sys

from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, IPv6ExtHdrSegmentRouting,\
    ipv6nh, ICMPv6MLReport2
from scapy.layers.l2 import Ether
from scapy.packet import Raw

from .PacketVerifier import RxQueue, TxQueue
from .TrafficScriptArg import TrafficScriptArg


def check_srv6(
        pkt_recv, src_mac, dst_mac, src_ip, dst_ip, dir_srcsid, dir_dstsid1,
        dir_dstsid2, dir_dstsid3, segleft_corr, static_proxy=False):
    """Check received SRv6 packet.

    :param pkt_recv: Received packet to verify.
    :param src_mac: Source MAC address.
    :param dst_mac: Destination MAC address.
    :param src_ip: Source IP/IPv6 address of original IP/IPv6 packet.
    :param dst_ip: Destination IP/IPv6 address of original IP/IPv6 packet.
    :param dir_srcsid: Source SID for SR in desired direction.
    :param dir_dstsid1: Destination SID1 in desired direction.
    :param dir_dstsid2: Destination SID2 in desired direction.
    :param dir_dstsid3: Destination SID3 in desired direction.
    :param segleft_corr: Correction for expected segleft value of SRH.
    :type pkt_recv: scapy.Ether
    :type src_mac: str
    :type dst_mac: str
    :type src_ip: str
    :type dst_ip: str
    :type dir_srcsid: str
    :type dir_dstsid1: str
    :type dir_dstsid2: str
    :type dir_dstsid3: str
    :type segleft_corr: int
    :type static_proxy; bool
    :raises RuntimeError: If received packet is invalid.
    """
    if pkt_recv[Ether].src != src_mac:
        raise RuntimeError(
            f"Received frame has invalid source MAC address: "
            f"{pkt_recv[Ether].src} should be: {src_mac}"
        )
    if pkt_recv[Ether].dst != dst_mac:
        raise RuntimeError(
            f"Received frame has invalid destination MAC address: "
            f"{pkt_recv[Ether].dst} should be: {dst_mac}"
        )
    if not pkt_recv.haslayer(IPv6):
        raise RuntimeError(
            f"Not an IPv6 packet received: {pkt_recv!r}"
        )
    ip6_pkt = pkt_recv[IPv6]
    if ip6_pkt.src != dir_srcsid:
        raise RuntimeError(
            f"Outer IPv6 packet has invalid source address: "
            f"{ip6_pkt.src} should be: {dir_srcsid}"
        )
    dir_dstsids = [dir_dstsid2, dir_dstsid1] if dir_dstsid3 == u"None" \
        else [dir_dstsid3, dir_dstsid2, dir_dstsid1] if not static_proxy \
        else [dir_dstsid3, dir_dstsid2]
    segleft = len(dir_dstsids) - segleft_corr if not static_proxy \
        else len(dir_dstsids) - segleft_corr + 1
    if ip6_pkt.dst != dir_dstsids[segleft]:
        raise RuntimeError(
            f"Outer IPv6 packet has invalid destination address: "
            f"{ip6_pkt.dst} should be: {dir_dstsids[segleft]}"
        )
    if dir_dstsid2 == u"None":
        if ip6_pkt.haslayer(IPv6ExtHdrSegmentRouting):
            raise RuntimeError(
                f"Segment Routing Header in received packet: {pkt_recv!r}"
            )
        if ip6_pkt.nh != 41:  # ipv6nh[41] == IPv6
            raise RuntimeError(
                f"Outer IPv6 packet has invalid next header: "
                f"{ip6_pkt.nh} should be: 41 ({ipv6nh[41]})"
            )
        ip6_pkt = ip6_pkt[IPv6][1]
    else:
        if not pkt_recv.haslayer(IPv6ExtHdrSegmentRouting):
            raise RuntimeError(
                f"No Segment Routing Header in received packet: {pkt_recv!r}"
            )
        if ip6_pkt.nh != 43:  # ipv6nh[43] == Routing Header
            raise RuntimeError(
                f"Outer IPv6 packet has invalid next header: "
                f"{pkt_recv[IPv6][0].nh} should be: 43 ({ipv6nh[43]})"
            )
        ip6_pkt = ip6_pkt[IPv6ExtHdrSegmentRouting]
        if ip6_pkt.addresses != dir_dstsids:
            raise RuntimeError(
                f"Segment Routing Header has invalid addresses: "
                f"{ip6_pkt.addresses} should be: {dir_dstsids}"
            )
        if ip6_pkt.segleft != segleft:
            raise RuntimeError(
                f"Segment Routing Header has invalid segleft value: "
                f"{ip6_pkt.segleft} should be: {segleft}"
            )
        if ip6_pkt.nh != 41:  # ipv6nh[41] == IPv6
            raise RuntimeError(
                f"Segment Routing Header has invalid next header: "
                f"{ip6_pkt.nh} should be: 41 ({ipv6nh[41]})"
            )
        ip6_pkt = ip6_pkt[IPv6]
    if ip6_pkt.src != src_ip:
        raise RuntimeError(
            f"Inner IPv6 packet has invalid source address: "
            f"{ip6_pkt.src} should be: {src_ip}"
        )
    if ip6_pkt.dst != dst_ip:
        raise RuntimeError(
            f"Inner IPv6 packet has invalid destination address: "
            f"{ip6_pkt.dst} should be: {dst_ip}"
        )
    if ip6_pkt.nh != 59:  # ipv6nh[59] == No Next Header
        raise RuntimeError(
            f"Inner IPv6 packet has invalid next header: "
            f"{ip6_pkt.nh} should be: 59 ({ipv6nh[59]})"
        )


def check_ip(pkt_recv, src_mac, dst_mac, src_ip, dst_ip):
    """Check received IPv6 packet.

    :param pkt_recv: Received packet to verify.
    :param src_mac: Source MAC address.
    :param dst_mac: Destination MAC address.
    :param src_ip: Source IP/IPv6 address.
    :param dst_ip: Destination IP/IPv6 address.
    :type pkt_recv: scapy.Ether
    :type src_mac: str
    :type dst_mac: str
    :type src_ip: str
    :type dst_ip: str
    :raises RuntimeError: If received packet is invalid.
    """
    if pkt_recv[Ether].src != src_mac:
        raise RuntimeError(
            f"Received frame has invalid source MAC address: "
            f"{pkt_recv[Ether].src} should be: {src_mac}"
        )

    if pkt_recv[Ether].dst != dst_mac:
        raise RuntimeError(
            f"Received frame has invalid destination MAC address: "
            f"{pkt_recv[Ether].dst} should be: {dst_mac}"
        )

    if not pkt_recv.haslayer(IPv6):
        raise RuntimeError(
            f"Not an {IPv6.name} packet received: {pkt_recv!r}"
        )

    if pkt_recv[IPv6].dst != dst_ip:
        raise RuntimeError(
            f"Received packet has invalid destination address: "
            f"{pkt_recv[IPv6.name].dst} should be: {dst_ip}"
        )

    if pkt_recv[IPv6].src != src_ip:
        raise RuntimeError(
            f"Received packet has invalid destination address: "
            f"{pkt_recv[IPv6.name].dst} should be: {src_ip}"
        )

    if pkt_recv[IPv6].nh != 59:  # ipv6nh[59] == No Next Header
        raise RuntimeError(
            f"Received IPv6 packet has invalid next header: "
            f"{IPv6.nh} should be: 59 ({ipv6nh[59]})"
        )


def main():
    """Send, receive and check IPv6 and IPv6ExtHdrSegmentRouting packets."""

    args = TrafficScriptArg(
        [
            u"tx_src_mac", u"tx_dst_mac", u"rx_src_mac", u"rx_dst_mac",
            u"src_ip", u"dst_ip", u"dir0_srcsid", u"dir0_dstsid1",
            u"dir0_dstsid2", u"dir1_srcsid", u"dir1_dstsid1", u"dir1_dstsid2",
            u"decap", u"dir0_dstsid3", u"dir1_dstsid3", u"static_proxy"
        ]
    )

    tx_txq = TxQueue(args.get_arg(u"tx_if"))
    tx_rxq = RxQueue(args.get_arg(u"tx_if"))
    rx_txq = TxQueue(args.get_arg(u"rx_if"))
    rx_rxq = RxQueue(args.get_arg(u"rx_if"))

    tx_src_mac = args.get_arg(u"tx_src_mac")
    tx_dst_mac = args.get_arg(u"tx_dst_mac")
    rx_src_mac = args.get_arg(u"rx_src_mac")
    rx_dst_mac = args.get_arg(u"rx_dst_mac")
    src_ip = args.get_arg(u"src_ip")
    dst_ip = args.get_arg(u"dst_ip")

    dir0_srcsid = args.get_arg(u"dir0_srcsid")
    dir0_dstsid1 = args.get_arg(u"dir0_dstsid1")
    dir0_dstsid2 = args.get_arg(u"dir0_dstsid2")
    dir1_srcsid = args.get_arg(u"dir1_srcsid")
    dir1_dstsid1 = args.get_arg(u"dir1_dstsid1")
    dir1_dstsid2 = args.get_arg(u"dir1_dstsid2")
    decap = args.get_arg(u"decap")
    dir0_dstsid3 = args.get_arg(u"dir0_dstsid3")
    dir1_dstsid3 = args.get_arg(u"dir1_dstsid3")
    static_proxy = args.get_arg(u"static_proxy")

    ip_pkt = IPv6(src=src_ip, dst=dst_ip)

    sent_packets = list()
    tx_pkt_send = (Ether(src=tx_src_mac, dst=tx_dst_mac) / ip_pkt)
    tx_pkt_send /= Raw()
    size_limit = 78
    if len(tx_pkt_send) < size_limit:
        tx_pkt_send[Raw].load += (b"\0" * (size_limit - len(tx_pkt_send)))
    sent_packets.append(tx_pkt_send)
    tx_txq.send(tx_pkt_send)

    while True:
        rx_pkt_recv = rx_rxq.recv(2)

        if rx_pkt_recv is None:
            raise RuntimeError(f"{IPv6.name} packet Rx timeout")

        if rx_pkt_recv.haslayer(ICMPv6ND_NS):
            # read another packet in the queue if the current one is ICMPv6ND_NS
            continue

        # otherwise process the current packet
        break

    check_srv6(
        rx_pkt_recv, rx_src_mac, rx_dst_mac, src_ip, dst_ip, dir0_srcsid,
        dir0_dstsid1, dir0_dstsid2, dir0_dstsid3, 1
    )

    ip_pkt = IPv6(src=dst_ip, dst=src_ip)
    ip_pkt /= Raw()
    if len(ip_pkt) < (size_limit - 14):
        ip_pkt[Raw].load += (b"\0" * (size_limit - 14 - len(ip_pkt)))

    rx_pkt_send = (
        Ether(src=rx_dst_mac, dst=rx_src_mac) /
        IPv6(src=dir1_srcsid, dst=dir1_dstsid1) /
        IPv6ExtHdrSegmentRouting(
            segleft=1 if dir1_dstsid3 == u"None" else 2,
            lastentry=1 if dir1_dstsid3 == u"None" else 2,
            addresses=[dir1_dstsid2, dir1_dstsid1]
            if dir1_dstsid3 == u"None"
            else [dir1_dstsid3, dir1_dstsid2, dir1_dstsid1]
        ) /
        ip_pkt
    ) if dir1_dstsid2 != u"None" else (
        Ether(src=rx_dst_mac, dst=rx_src_mac) /
        IPv6(src=dir1_srcsid, dst=dir1_dstsid1) /
        ip_pkt
    )
    rx_txq.send(rx_pkt_send)

    while True:
        tx_pkt_recv = tx_rxq.recv(2, ignore=sent_packets)

        if tx_pkt_recv is None:
            raise RuntimeError(f"{IPv6.name} packet Rx timeout")

        if tx_pkt_recv.haslayer(ICMPv6ND_NS):
            # read another packet in the queue if the current one is ICMPv6ND_NS
            continue
        elif tx_pkt_recv.haslayer(ICMPv6MLReport2):
            # read another packet in the queue if the current one is
            # ICMPv6MLReport2
            continue

        # otherwise process the current packet
        break

    if decap == u"True":
        check_ip(tx_pkt_recv, tx_dst_mac, tx_src_mac, dst_ip, src_ip)
    else:
        check_srv6(
            tx_pkt_recv, tx_dst_mac, tx_src_mac, dst_ip, src_ip, dir1_srcsid,
            dir1_dstsid1, dir1_dstsid2, dir1_dstsid3, 2,
            bool(static_proxy == u"True")
        )

    sys.exit(0)


if __name__ == u"__main__":
    main()