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 *
17 from resources.libraries.python.PacketVerifier \
18 import Interface, create_gratuitous_arp_request, auto_pad
19 from optparse import OptionParser
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 parser = OptionParser()
49 parser.add_option("--src_if", dest="src_if")
50 parser.add_option("--dst_if", dest="dst_if") # optional
51 parser.add_option("--src_mac", dest="src_mac")
52 parser.add_option("--first_hop_mac", dest="first_hop_mac")
53 parser.add_option("--dst_mac", dest="dst_mac") # optional
54 parser.add_option("--src_ip", dest="src_ip")
55 parser.add_option("--dst_ip", dest="dst_ip")
56 parser.add_option("--hops", dest="hops") # optional
57 # If one of 'dst_if', 'dst_mac' and 'hops' is specified all must be specified.
58 (opts, args) = parser.parse_args()
59 src_if_name = opts.src_if
60 dst_if_name = opts.dst_if
62 if dst_if_name is None:
63 dst_if_defined = False
64 src_mac = opts.src_mac
65 first_hop_mac = opts.first_hop_mac
66 dst_mac = opts.dst_mac
71 if dst_if_defined and (src_if_name == dst_if_name):
72 raise Exception("Source interface name equals destination interface name")
74 src_if = Interface(src_if_name)
75 src_if.send_pkt(create_gratuitous_arp_request(src_mac, src_ip))
77 dst_if = Interface(dst_if_name)
78 dst_if.send_pkt(create_gratuitous_arp_request(dst_mac, dst_ip))
80 pkt_req_send = auto_pad(Ether(src=src_mac, dst=first_hop_mac) /
81 IP(src=src_ip, dst=dst_ip) /
83 pkt_req_send = Ether(pkt_req_send)
84 src_if.send_pkt(pkt_req_send)
88 pkt_req_recv = dst_if.recv_pkt()
95 check_ttl(pkt_req_send[IP].ttl, pkt_req_recv[IP].ttl, hops)
96 pkt_req_send_mod = pkt_req_send.copy()
97 pkt_req_send_mod[IP].ttl = pkt_req_recv[IP].ttl
98 del pkt_req_send_mod[IP].chksum # update checksum
99 ckeck_packets_equal(pkt_req_send_mod[IP], pkt_req_recv[IP])
101 pkt_resp_send = auto_pad(Ether(src=dst_mac, dst=pkt_req_recv.src) /
102 IP(src=dst_ip, dst=src_ip) /
103 ICMP(type=0)) # echo-reply
104 pkt_resp_send = Ether(pkt_resp_send)
105 dst_if.send_pkt(pkt_resp_send)
108 pkt_resp_recv = src_if.recv_pkt()
116 check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
117 pkt_resp_send_mod = pkt_resp_send.copy()
118 pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
119 del pkt_resp_send_mod[IP].chksum # update checksum
120 ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])