3 # Copyright (c) 2019 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 """Traffic script for IPsec verification."""
21 from ipaddress import ip_address
22 # pylint: disable=no-name-in-module
23 # pylint: disable=import-error
24 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
26 from scapy.layers.l2 import Ether
27 from scapy.layers.inet import IP, TCP
28 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS
29 from scapy.packet import Raw
31 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
32 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
35 def check_ipv4(pkt_recv, dscp):
36 """Check received IPv4 IPsec packet.
38 :param pkt_recv: Received packet to verify.
39 :param dscp: DSCP value to check.
40 :type pkt_recv: scapy.Ether
42 :raises RuntimeError: If received packet is invalid.
44 if not pkt_recv.haslayer(IP):
45 raise RuntimeError(f"Not an IPv4 packet received: {pkt_recv!r}")
47 rx_dscp = pkt_recv[IP].tos >> 2
49 raise RuntimeError(f"Invalid DSCP {rx_dscp} should be {dscp}")
51 if not pkt_recv.haslayer(TCP):
52 raise RuntimeError(f"Not a TCP packet received: {pkt_recv!r}")
55 def check_ipv6(pkt_recv, dscp):
56 """Check received IPv6 IPsec packet.
58 :param pkt_recv: Received packet to verify.
59 :param dscp: DSCP value to check.
60 :type pkt_recv: scapy.Ether
62 :raises RuntimeError: If received packet is invalid.
64 if not pkt_recv.haslayer(IPv6):
65 raise RuntimeError(f"Not an IPv6 packet received: {pkt_recv!r}")
67 rx_dscp = pkt_recv[IPv6].tc >> 2
69 raise RuntimeError(f"Invalid DSCP {rx_dscp} should be {dscp}")
71 if not pkt_recv.haslayer(TCP):
72 raise RuntimeError(f"Not a TCP packet received: {pkt_recv!r}")
75 # pylint: disable=too-many-locals
76 # pylint: disable=too-many-statements
78 """Send and receive TCP packet."""
79 args = TrafficScriptArg(
80 [u"src_mac", u"dst_mac", u"src_ip", u"dst_ip", u"dscp"]
83 rxq = RxQueue(args.get_arg(u"rx_if"))
84 txq = TxQueue(args.get_arg(u"tx_if"))
86 src_mac = args.get_arg(u"src_mac")
87 dst_mac = args.get_arg(u"dst_mac")
88 src_ip = args.get_arg(u"src_ip")
89 dst_ip = args.get_arg(u"dst_ip")
90 dscp = int(args.get_arg(u"dscp"))
92 ip_layer = IPv6 if ip_address(src_ip).version == 6 else IP
95 pkt_send = (Ether(src=src_mac, dst=dst_mac) /
96 ip_layer(src=src_ip, dst=dst_ip) /
100 sent_packets.append(pkt_send)
104 pkt_recv = rxq.recv(2, sent_packets)
106 raise RuntimeError(u"ICMPv6 echo reply Rx timeout")
108 if pkt_recv.haslayer(ICMPv6ND_NS):
109 # read another packet in the queue if the current one is ICMPv6ND_NS
112 # otherwise process the current packet
116 raise RuntimeError(u"Rx timeout")
119 check_ipv4(pkt_recv, dscp)
121 check_ipv6(pkt_recv, dscp)
126 if __name__ == u"__main__":