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 from scapy.all import Ether, IP, ICMP
17 from resources.libraries.python.PacketVerifier \
18 import Interface, create_gratuitous_arp_request, auto_pad
19 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
22 def check_ttl(ttl_begin, ttl_end, ttl_diff):
23 if ttl_begin != ttl_end + ttl_diff:
28 "TTL changed from {} to {} but decrease by {} expected"
29 .format(ttl_begin, ttl_end, hops))
32 def ckeck_packets_equal(pkt_send, pkt_recv):
33 pkt_send_raw = str(pkt_send)
34 pkt_recv_raw = str(pkt_recv)
35 if pkt_send_raw != pkt_recv_raw:
36 print "Sent: {}".format(pkt_send_raw.encode('hex'))
37 print "Received: {}".format(pkt_recv_raw.encode('hex'))
39 Ether(pkt_send_raw).show2()
41 Ether(pkt_recv_raw).show2()
45 raise Exception("Sent packet doesn't match received packet")
48 args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
49 'hops', 'first_hop_mac', 'is_dst_defined'])
51 src_if_name = args.get_arg('tx_if')
52 dst_if_name = args.get_arg('rx_if')
53 dst_if_defined = True if args.get_arg('is_dst_defined') == 'True' else False
55 src_mac = args.get_arg('src_mac')
56 first_hop_mac = args.get_arg('first_hop_mac')
57 dst_mac = args.get_arg('dst_mac')
58 src_ip = args.get_arg('src_ip')
59 dst_ip = args.get_arg('dst_ip')
60 hops = int(args.get_arg('hops'))
62 if dst_if_defined and (src_if_name == dst_if_name):
63 raise Exception("Source interface name equals destination interface name")
65 src_if = Interface(src_if_name)
66 src_if.send_pkt(create_gratuitous_arp_request(src_mac, src_ip))
68 dst_if = Interface(dst_if_name)
69 dst_if.send_pkt(create_gratuitous_arp_request(dst_mac, dst_ip))
71 pkt_req_send = auto_pad(Ether(src=src_mac, dst=first_hop_mac) /
72 IP(src=src_ip, dst=dst_ip) /
74 pkt_req_send = Ether(pkt_req_send)
75 src_if.send_pkt(pkt_req_send)
79 pkt_req_recv = dst_if.recv_pkt()
86 check_ttl(pkt_req_send[IP].ttl, pkt_req_recv[IP].ttl, hops)
87 pkt_req_send_mod = pkt_req_send.copy()
88 pkt_req_send_mod[IP].ttl = pkt_req_recv[IP].ttl
89 del pkt_req_send_mod[IP].chksum # update checksum
90 ckeck_packets_equal(pkt_req_send_mod[IP], pkt_req_recv[IP])
92 pkt_resp_send = auto_pad(Ether(src=dst_mac, dst=pkt_req_recv.src) /
93 IP(src=dst_ip, dst=src_ip) /
94 ICMP(type=0)) # echo-reply
95 pkt_resp_send = Ether(pkt_resp_send)
96 dst_if.send_pkt(pkt_resp_send)
99 pkt_resp_recv = src_if.recv_pkt()
107 check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
108 pkt_resp_send_mod = pkt_resp_send.copy()
109 pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
110 del pkt_resp_send_mod[IP].chksum # update checksum
111 ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])