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)
24 from scapy.all import Ether, IP, IPv6, TCP
25 from ipaddress import ip_address
27 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
28 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
31 def check_ipv4(pkt_recv, dscp):
32 """Check received IPv4 IPsec packet.
34 :param pkt_recv: Received packet to verify.
35 :param dscp: DSCP value to check.
36 :type pkt_recv: scapy.Ether
38 :raises RuntimeError: If received packet is invalid.
40 if not pkt_recv.haslayer(IP):
42 'Not an IPv4 packet received: {0}'.format(pkt_recv.__repr__()))
44 rx_dscp = pkt_recv['IP'].tos >> 2
47 'Invalid DSCP {0} should be {1}'.format(rx_dscp, dscp))
49 if not pkt_recv.haslayer(TCP):
51 'Not a TCP packet received: {0}'.format(pkt_recv.__repr__()))
54 def check_ipv6(pkt_recv, dscp):
55 """Check received IPv6 IPsec packet.
57 :param pkt_recv: Received packet to verify.
58 :param dscp: DSCP value to check.
59 :type pkt_recv: scapy.Ether
61 :raises RuntimeError: If received packet is invalid.
63 if not pkt_recv.haslayer(IPv6):
65 'Not an IPv6 packet received: {0}'.format(pkt_recv.__repr__()))
67 rx_dscp = pkt_recv['IPv6'].tc >> 2
70 'Invalid DSCP {0} should be {1}'.format(rx_dscp, dscp))
72 if not pkt_recv.haslayer(TCP):
74 'Not a TCP packet received: {0}'.format(pkt_recv.__repr__()))
77 # pylint: disable=too-many-locals
78 # pylint: disable=too-many-statements
80 """Send and receive TCP packet."""
81 args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip', 'dscp'])
83 rxq = RxQueue(args.get_arg('rx_if'))
84 txq = TxQueue(args.get_arg('tx_if'))
86 src_mac = args.get_arg('src_mac')
87 dst_mac = args.get_arg('dst_mac')
88 src_ip = args.get_arg('src_ip')
89 dst_ip = args.get_arg('dst_ip')
90 dscp = int(args.get_arg('dscp'))
92 if 6 == ip_address(unicode(src_ip)).version:
100 ip_pkt = IP(src=src_ip, dst=dst_ip) / \
103 ip_pkt = IPv6(src=src_ip, dst=dst_ip) / \
106 pkt_send = Ether(src=src_mac, dst=dst_mac) / \
109 sent_packets.append(pkt_send)
112 pkt_recv = rxq.recv(2, sent_packets)
115 raise RuntimeError('Rx timeout')
118 check_ipv4(pkt_recv, dscp)
120 check_ipv6(pkt_recv, dscp)
124 if __name__ == "__main__":