3 # Copyright (c) 2016 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 # pylint: disable=no-name-in-module
22 # pylint: disable=import-error
23 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
25 from scapy.all import Ether
26 from scapy.layers.inet import IP, TCP
27 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS
28 from ipaddress import ip_address
30 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
31 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
34 def check_ipv4(pkt_recv, dscp):
35 """Check received IPv4 IPsec packet.
37 :param pkt_recv: Received packet to verify.
38 :param dscp: DSCP value to check.
39 :type pkt_recv: scapy.Ether
41 :raises RuntimeError: If received packet is invalid.
43 if not pkt_recv.haslayer(IP):
44 raise RuntimeError('Not an IPv4 packet received: {0}'.
45 format(pkt_recv.__repr__()))
47 rx_dscp = pkt_recv[IP].tos >> 2
49 raise RuntimeError('Invalid DSCP {0} should be {1}'.
50 format(rx_dscp, dscp))
52 if not pkt_recv.haslayer(TCP):
53 raise RuntimeError('Not a TCP packet received: {0}'.
54 format(pkt_recv.__repr__()))
57 def check_ipv6(pkt_recv, dscp):
58 """Check received IPv6 IPsec packet.
60 :param pkt_recv: Received packet to verify.
61 :param dscp: DSCP value to check.
62 :type pkt_recv: scapy.Ether
64 :raises RuntimeError: If received packet is invalid.
66 if not pkt_recv.haslayer(IPv6):
67 raise RuntimeError('Not an IPv6 packet received: {0}'.
68 format(pkt_recv.__repr__()))
70 rx_dscp = pkt_recv[IPv6].tc >> 2
72 raise RuntimeError('Invalid DSCP {0} should be {1}'.
73 format(rx_dscp, dscp))
75 if not pkt_recv.haslayer(TCP):
76 raise RuntimeError('Not a TCP packet received: {0}'.
77 format(pkt_recv.__repr__()))
80 # pylint: disable=too-many-locals
81 # pylint: disable=too-many-statements
83 """Send and receive TCP packet."""
84 args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip', 'dscp'])
86 rxq = RxQueue(args.get_arg('rx_if'))
87 txq = TxQueue(args.get_arg('tx_if'))
89 src_mac = args.get_arg('src_mac')
90 dst_mac = args.get_arg('dst_mac')
91 src_ip = args.get_arg('src_ip')
92 dst_ip = args.get_arg('dst_ip')
93 dscp = int(args.get_arg('dscp'))
95 if 6 == ip_address(unicode(src_ip)).version:
103 ip_pkt = (IP(src=src_ip, dst=dst_ip) /
106 ip_pkt = (IPv6(src=src_ip, dst=dst_ip) /
109 pkt_send = (Ether(src=src_mac, dst=dst_mac) /
112 sent_packets.append(pkt_send)
116 pkt_recv = rxq.recv(2, sent_packets)
118 raise RuntimeError('ICMPv6 echo reply Rx timeout')
120 if pkt_recv.haslayer(ICMPv6ND_NS):
121 # read another packet in the queue if the current one is ICMPv6ND_NS
124 # otherwise process the current packet
128 raise RuntimeError('Rx timeout')
131 check_ipv4(pkt_recv, dscp)
133 check_ipv6(pkt_recv, dscp)
138 if __name__ == "__main__":