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 IPv4 sweep ping."""
22 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
23 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue, \
24 create_gratuitous_arp_request, checksum_equal
25 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
26 from scapy.layers.inet import IP, ICMP
27 from scapy.all import Ether, Raw
31 # start_size - start size of the ICMPv4 echo data
32 # end_size - end size of the ICMPv4 echo data
33 # step - increment step
34 args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
35 'start_size', 'end_size', 'step'])
37 rxq = RxQueue(args.get_arg('rx_if'))
38 txq = TxQueue(args.get_arg('tx_if'))
40 src_mac = args.get_arg('src_mac')
41 dst_mac = args.get_arg('dst_mac')
42 src_ip = args.get_arg('src_ip')
43 dst_ip = args.get_arg('dst_ip')
44 start_size = int(args.get_arg('start_size'))
45 end_size = int(args.get_arg('end_size'))
46 step = int(args.get_arg('step'))
48 # generate some random data buffer
49 data = bytearray(os.urandom(end_size))
52 pkt_send = create_gratuitous_arp_request(src_mac, src_ip)
53 sent_packets.append(pkt_send)
56 # send ICMP echo request with incremented data length and receive ICMP
58 for echo_seq in range(start_size, end_size + 1, step):
59 pkt_send = (Ether(src=src_mac, dst=dst_mac) /
60 IP(src=src_ip, dst=dst_ip) /
61 ICMP(id=echo_id, seq=echo_seq) /
62 Raw(load=data[0:echo_seq]))
63 sent_packets.append(pkt_send)
66 ether = rxq.recv(ignore=sent_packets)
69 'ICMP echo reply seq {0} Rx timeout'.format(echo_seq))
71 if not ether.haslayer(IP):
73 'Unexpected packet with no IPv4 received {0}'.format(
78 if not ipv4.haslayer(ICMP):
80 'Unexpected packet with no ICMP received {0}'.format(
85 if icmpv4.id != echo_id or icmpv4.seq != echo_seq:
87 'Invalid ICMP echo reply received ID {0} seq {1} should be '
88 'ID {2} seq {3}, {0}'.format(icmpv4.id, icmpv4.seq, echo_id,
91 chksum = icmpv4.chksum
93 tmp = ICMP(str(icmpv4))
94 if not checksum_equal(tmp.chksum, chksum):
96 'Invalid checksum {0} should be {1}'.format(chksum, tmp.chksum))
99 load = icmpv4['Raw'].load
102 if load != data[0:echo_seq]:
104 'Received ICMP payload does not match sent payload')
106 sent_packets.remove(pkt_send)
111 if __name__ == "__main__":