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, checksum_equal
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:
25 "TTL changed from {} to {} but decrease by {} expected"
26 .format(ttl_begin, ttl_end, ttl_diff))
29 def ckeck_packets_equal(pkt_send, pkt_recv):
30 pkt_send_raw = auto_pad(pkt_send)
31 pkt_recv_raw = auto_pad(pkt_recv)
32 if pkt_send_raw != pkt_recv_raw:
33 print "Sent: {}".format(pkt_send_raw.encode('hex'))
34 print "Received: {}".format(pkt_recv_raw.encode('hex'))
39 raise RuntimeError("Sent packet doesn't match received packet")
43 args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
44 'hops', 'first_hop_mac', 'is_dst_tg'])
46 src_if_name = args.get_arg('tx_if')
47 dst_if_name = args.get_arg('rx_if')
48 is_dst_tg = True if args.get_arg('is_dst_tg') == 'True' else False
50 src_mac = args.get_arg('src_mac')
51 first_hop_mac = args.get_arg('first_hop_mac')
52 dst_mac = args.get_arg('dst_mac')
53 src_ip = args.get_arg('src_ip')
54 dst_ip = args.get_arg('dst_ip')
55 hops = int(args.get_arg('hops'))
57 if is_dst_tg and (src_if_name == dst_if_name):
59 "Source interface name equals destination interface name")
61 src_if = Interface(src_if_name)
62 src_if.send_pkt(str(create_gratuitous_arp_request(src_mac, src_ip)))
64 dst_if = Interface(dst_if_name)
65 dst_if.send_pkt(str(create_gratuitous_arp_request(dst_mac, dst_ip)))
67 pkt_req_send = (Ether(src=src_mac, dst=first_hop_mac) /
68 IP(src=src_ip, dst=dst_ip) /
70 src_if.send_pkt(pkt_req_send)
73 pkt_req_recv = dst_if.recv_pkt()
74 if pkt_req_recv is None:
75 raise RuntimeError('Timeout waiting for packet')
77 check_ttl(pkt_req_send[IP].ttl, pkt_req_recv[IP].ttl, hops)
78 pkt_req_send_mod = pkt_req_send.copy()
79 pkt_req_send_mod[IP].ttl = pkt_req_recv[IP].ttl
80 del pkt_req_send_mod[IP].chksum # update checksum
81 ckeck_packets_equal(pkt_req_send_mod[IP], pkt_req_recv[IP])
83 pkt_resp_send = (Ether(src=dst_mac, dst=pkt_req_recv.src) /
84 IP(src=dst_ip, dst=src_ip) /
85 ICMP(type=0)) # echo-reply
86 dst_if.send_pkt(pkt_resp_send)
88 pkt_resp_recv = src_if.recv_pkt()
89 if pkt_resp_recv is None:
90 raise RuntimeError('Timeout waiting for packet')
93 check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
94 pkt_resp_send_mod = pkt_resp_send.copy()
95 pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
96 del pkt_resp_send_mod[IP].chksum # update checksum
97 ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])
99 if not pkt_resp_recv.haslayer(IP):
100 raise RuntimeError('Received packet does not contain IPv4 header: {}'.
101 format(pkt_resp_recv.__repr__()))
103 if pkt_resp_recv[IP].src != pkt_req_send[IP].dst:
105 'Received IPv4 packet contains wrong src IP address, '
106 '{} instead of {}'.format(pkt_resp_recv[IP].src,
107 pkt_req_send[IP].dst))
109 if pkt_resp_recv[IP].dst != pkt_req_send[IP].src:
111 'Received IPv4 packet contains wrong dst IP address, '
112 '{} instead of {}'.format(pkt_resp_recv[IP].dst,
113 pkt_req_send[IP].src))
115 # verify IPv4 checksum
116 copy = pkt_resp_recv.copy()
117 chksum = copy[IP].chksum
119 tmp = IP(str(copy[IP]))
120 if not checksum_equal(tmp.chksum, chksum):
121 raise RuntimeError('Received IPv4 packet contains invalid checksum, '
122 '{} instead of {}'.format(chksum, tmp.chksum))
124 if not pkt_resp_recv[IP].haslayer(ICMP):
126 'Received IPv4 packet does not contain ICMP header: {}'.
127 format(pkt_resp_recv[IP].__repr__()))
129 # verify ICMP checksum
130 copy = pkt_resp_recv.copy()
131 chksum = copy[IP][ICMP].chksum
132 del copy[IP][ICMP].chksum
133 tmp = ICMP(str(copy[IP][ICMP]))
134 if not checksum_equal(tmp.chksum, chksum):
135 raise RuntimeError('Received ICMP packet contains invalid checksum, '
136 '{} instead of {}'.format(chksum, tmp.chksum))
138 pkt_req_send_mod = pkt_req_send.copy()
139 pkt_req_send_mod[IP][ICMP].type = pkt_resp_recv[IP][ICMP].type
140 del pkt_req_send_mod[IP][ICMP].chksum # update checksum
141 ckeck_packets_equal(pkt_req_send_mod[IP][ICMP], pkt_resp_recv[IP][ICMP])
144 if __name__ == "__main__":