FIX: Add ICMPv6MLReport2 masking
[csit.git] / GPL / traffic_scripts / policer.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 IPsec verification."""
27
28 import sys
29 import logging
30
31 from ipaddress import ip_address
32 from scapy.layers.l2 import Ether
33 from scapy.layers.inet import IP, TCP
34 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6MLReport2
35 from scapy.packet import Raw
36
37 from .TrafficScriptArg import TrafficScriptArg
38 from .PacketVerifier import RxQueue, TxQueue
39
40
41 def check_ipv4(pkt_recv, dscp):
42     """Check received IPv4 IPsec packet.
43
44     :param pkt_recv: Received packet to verify.
45     :param dscp: DSCP value to check.
46     :type pkt_recv: scapy.Ether
47     :type dscp: int
48     :raises RuntimeError: If received packet is invalid.
49     """
50     if not pkt_recv.haslayer(IP):
51         raise RuntimeError(f"Not an IPv4 packet received: {pkt_recv!r}")
52
53     rx_dscp = pkt_recv[IP].tos >> 2
54     if rx_dscp != dscp:
55         raise RuntimeError(f"Invalid DSCP {rx_dscp} should be {dscp}")
56
57     if not pkt_recv.haslayer(TCP):
58         raise RuntimeError(f"Not a TCP packet received: {pkt_recv!r}")
59
60
61 def check_ipv6(pkt_recv, dscp):
62     """Check received IPv6 IPsec packet.
63
64     :param pkt_recv: Received packet to verify.
65     :param dscp: DSCP value to check.
66     :type pkt_recv: scapy.Ether
67     :type dscp: int
68     :raises RuntimeError: If received packet is invalid.
69     """
70     if not pkt_recv.haslayer(IPv6):
71         raise RuntimeError(f"Not an IPv6 packet received: {pkt_recv!r}")
72
73     rx_dscp = pkt_recv[IPv6].tc >> 2
74     if rx_dscp != dscp:
75         raise RuntimeError(f"Invalid DSCP {rx_dscp} should be {dscp}")
76
77     if not pkt_recv.haslayer(TCP):
78         raise RuntimeError(f"Not a TCP packet received: {pkt_recv!r}")
79
80
81 # TODO: Pylint says too-many-locals and too-many-statements. Refactor!
82 def main():
83     """Send and receive TCP packet."""
84     args = TrafficScriptArg(
85         [u"src_mac", u"dst_mac", u"src_ip", u"dst_ip", u"dscp"]
86     )
87
88     rxq = RxQueue(args.get_arg(u"rx_if"))
89     txq = TxQueue(args.get_arg(u"tx_if"))
90
91     src_mac = args.get_arg(u"src_mac")
92     dst_mac = args.get_arg(u"dst_mac")
93     src_ip = args.get_arg(u"src_ip")
94     dst_ip = args.get_arg(u"dst_ip")
95     dscp = int(args.get_arg(u"dscp"))
96
97     ip_layer = IPv6 if ip_address(src_ip).version == 6 else IP
98
99     sent_packets = list()
100     pkt_send = (Ether(src=src_mac, dst=dst_mac) /
101                 ip_layer(src=src_ip, dst=dst_ip) /
102                 TCP())
103
104     pkt_send /= Raw()
105     sent_packets.append(pkt_send)
106     txq.send(pkt_send)
107
108     while True:
109         pkt_recv = rxq.recv(2, sent_packets)
110         if pkt_recv is None:
111             raise RuntimeError(u"ICMPv6 echo reply Rx timeout")
112
113         if pkt_recv.haslayer(ICMPv6ND_NS):
114             # read another packet in the queue if the current one is ICMPv6ND_NS
115             continue
116         elif pkt_recv.haslayer(ICMPv6MLReport2):
117             # read another packet in the queue if the current one is
118             # ICMPv6MLReport2
119             continue
120         else:
121             # otherwise process the current packet
122             break
123
124     if pkt_recv is None:
125         raise RuntimeError(u"Rx timeout")
126
127     if ip_layer == IP:
128         check_ipv4(pkt_recv, dscp)
129     else:
130         check_ipv6(pkt_recv, dscp)
131
132     sys.exit(0)
133
134
135 if __name__ == u"__main__":
136     main()