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 IPsec verification."""
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
35 from scapy.packet import Raw
37 from .TrafficScriptArg import TrafficScriptArg
38 from .PacketVerifier import RxQueue, TxQueue
41 def check_ipv4(pkt_recv, dscp):
42 """Check received IPv4 IPsec packet.
44 :param pkt_recv: Received packet to verify.
45 :param dscp: DSCP value to check.
46 :type pkt_recv: scapy.Ether
48 :raises RuntimeError: If received packet is invalid.
50 if not pkt_recv.haslayer(IP):
51 raise RuntimeError(f"Not an IPv4 packet received: {pkt_recv!r}")
53 rx_dscp = pkt_recv[IP].tos >> 2
55 raise RuntimeError(f"Invalid DSCP {rx_dscp} should be {dscp}")
57 if not pkt_recv.haslayer(TCP):
58 raise RuntimeError(f"Not a TCP packet received: {pkt_recv!r}")
61 def check_ipv6(pkt_recv, dscp):
62 """Check received IPv6 IPsec packet.
64 :param pkt_recv: Received packet to verify.
65 :param dscp: DSCP value to check.
66 :type pkt_recv: scapy.Ether
68 :raises RuntimeError: If received packet is invalid.
70 if not pkt_recv.haslayer(IPv6):
71 raise RuntimeError(f"Not an IPv6 packet received: {pkt_recv!r}")
73 rx_dscp = pkt_recv[IPv6].tc >> 2
75 raise RuntimeError(f"Invalid DSCP {rx_dscp} should be {dscp}")
77 if not pkt_recv.haslayer(TCP):
78 raise RuntimeError(f"Not a TCP packet received: {pkt_recv!r}")
81 # TODO: Pylint says too-many-locals and too-many-statements. Refactor!
83 """Send and receive TCP packet."""
84 args = TrafficScriptArg(
85 [u"src_mac", u"dst_mac", u"src_ip", u"dst_ip", u"dscp"]
88 rxq = RxQueue(args.get_arg(u"rx_if"))
89 txq = TxQueue(args.get_arg(u"tx_if"))
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"))
97 ip_layer = IPv6 if ip_address(src_ip).version == 6 else IP
100 pkt_send = (Ether(src=src_mac, dst=dst_mac) /
101 ip_layer(src=src_ip, dst=dst_ip) /
105 sent_packets.append(pkt_send)
109 pkt_recv = rxq.recv(2, sent_packets)
111 raise RuntimeError(u"ICMPv6 echo reply Rx timeout")
113 if pkt_recv.haslayer(ICMPv6ND_NS):
114 # read another packet in the queue if the current one is ICMPv6ND_NS
117 # otherwise process the current packet
121 raise RuntimeError(u"Rx timeout")
124 check_ipv4(pkt_recv, dscp)
126 check_ipv6(pkt_recv, dscp)
131 if __name__ == u"__main__":