diff options
Diffstat (limited to 'resources/traffic_scripts')
-rwxr-xr-x | resources/traffic_scripts/arp_request.py | 118 | ||||
-rwxr-xr-x | resources/traffic_scripts/ipv4_ping_ttl_check.py | 6 | ||||
-rwxr-xr-x | resources/traffic_scripts/ipv4_sweep_ping.py | 112 | ||||
-rwxr-xr-x | resources/traffic_scripts/ipv6_sweep_ping.py | 2 |
4 files changed, 234 insertions, 4 deletions
diff --git a/resources/traffic_scripts/arp_request.py b/resources/traffic_scripts/arp_request.py new file mode 100755 index 0000000000..86a4c015cd --- /dev/null +++ b/resources/traffic_scripts/arp_request.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python + +# Copyright (c) 2016 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Send an ARP request and verify the reply""" + +import sys + +from scapy.all import Ether, ARP + +from resources.libraries.python.PacketVerifier import Interface +from resources.libraries.python.TrafficScriptArg import TrafficScriptArg + + +def parse_arguments(): + """Parse arguments of the script passed through command line + + :return: tuple of parsed arguments + """ + args = TrafficScriptArg(['src_if', 'src_mac', 'dst_mac', + 'src_ip', 'dst_ip']) + + # check for mandatory parameters + params = (args.get_arg('tx_if'), + args.get_arg('src_mac'), + args.get_arg('dst_mac'), + args.get_arg('src_ip'), + args.get_arg('dst_ip')) + if None in params: + raise Exception('Missing mandatory parameter(s)!') + + return params + + +def arp_request_test(): + """Send ARP request, expect a reply and verify its fields. + + returns: test status + """ + test_passed = False + (src_if, src_mac, dst_mac, src_ip, dst_ip) = parse_arguments() + + interface = Interface(src_if) + + # build an ARP request + arp_request = (Ether(src=src_mac, dst='ff:ff:ff:ff:ff:ff') / + ARP(psrc=src_ip, hwsrc=src_mac, pdst=dst_ip, + hwdst='ff:ff:ff:ff:ff:ff')) + + # send the request + interface.send_pkt(arp_request) + + try: + # wait for APR reply + ether = interface.recv_pkt() + + # verify received packet + + if not ether.haslayer(ARP): + raise RuntimeError('Unexpected packet: does not contain ARP ' + + 'header "{}"'.format(ether.__repr__())) + + arp = ether['ARP'] + arp_reply = 2 + + if arp.op != arp_reply: + raise RuntimeError('expected op={}, received {}'.format(arp_reply, + arp.op)) + if arp.ptype != 0x800: + raise RuntimeError('expected ptype=0x800, received {}'. + format(arp.ptype)) + if arp.hwlen != 6: + raise RuntimeError('expected hwlen=6, received {}'. + format(arp.hwlen)) + if arp.plen != 4: + raise RuntimeError('expected plen=4, received {}'.format(arp.plen)) + if arp.hwsrc != dst_mac: + raise RuntimeError('expected hwsrc={}, received {}'. + format(dst_mac, arp.hwsrc)) + if arp.psrc != dst_ip: + raise RuntimeError('expected psrc={}, received {}'. + format(dst_ip, arp.psrc)) + if arp.hwdst != src_mac: + raise RuntimeError('expected hwdst={}, received {}'. + format(src_mac, arp.hwdst)) + if arp.pdst != src_ip: + raise RuntimeError('expected pdst={}, received {}'. + format(src_ip, arp.pdst)) + test_passed = True + + except RuntimeError as ex: + print 'Error occurred: {}'.format(ex) + finally: + interface.close() + + return test_passed + + +def main(): + """Run the test and collect result""" + if arp_request_test(): + sys.exit(0) + else: + sys.exit(1) + +if __name__ == '__main__': + main() diff --git a/resources/traffic_scripts/ipv4_ping_ttl_check.py b/resources/traffic_scripts/ipv4_ping_ttl_check.py index 050a1d7b29..1286b46876 100755 --- a/resources/traffic_scripts/ipv4_ping_ttl_check.py +++ b/resources/traffic_scripts/ipv4_ping_ttl_check.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from scapy.all import * +from scapy.all import Ether, IP, ICMP from resources.libraries.python.PacketVerifier \ import Interface, create_gratuitous_arp_request, auto_pad from optparse import OptionParser @@ -25,8 +25,8 @@ def check_ttl(ttl_begin, ttl_end, ttl_diff): if dst_if_defined: dst_if.close() raise Exception( - "TTL changed from {} to {} but decrease by {} expected")\ - .format(ttl_begin, ttl_end, hops) + "TTL changed from {} to {} but decrease by {} expected" + .format(ttl_begin, ttl_end, hops)) def ckeck_packets_equal(pkt_send, pkt_recv): diff --git a/resources/traffic_scripts/ipv4_sweep_ping.py b/resources/traffic_scripts/ipv4_sweep_ping.py new file mode 100755 index 0000000000..4b82a9b03e --- /dev/null +++ b/resources/traffic_scripts/ipv4_sweep_ping.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python + +# Copyright (c) 2016 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Traffic script for IPv4 sweep ping.""" + +import sys +import logging +import os +logging.getLogger("scapy.runtime").setLevel(logging.ERROR) +from resources.libraries.python.PacketVerifier import RxQueue, TxQueue,\ + auto_pad, create_gratuitous_arp_request +from resources.libraries.python.TrafficScriptArg import TrafficScriptArg +from scapy.layers.inet import IP, ICMP +from scapy.all import Ether, Raw + + +def main(): + # start_size - start size of the ICMPv4 echo data + # end_size - end size of the ICMPv4 echo data + # step - increment step + args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip', + 'start_size', 'end_size', 'step']) + + rxq = RxQueue(args.get_arg('rx_if')) + txq = TxQueue(args.get_arg('tx_if')) + + src_mac = args.get_arg('src_mac') + dst_mac = args.get_arg('dst_mac') + src_ip = args.get_arg('src_ip') + dst_ip = args.get_arg('dst_ip') + start_size = int(args.get_arg('start_size')) + end_size = int(args.get_arg('end_size')) + step = int(args.get_arg('step')) + echo_id = 0xa + # generate some random data buffer + data = bytearray(os.urandom(end_size)) + + sent_packets = [] + pkt_send = create_gratuitous_arp_request(src_mac, src_ip) + sent_packets.append(pkt_send) + txq.send(pkt_send) + + # send ICMP echo request with incremented data length and receive ICMP + # echo reply + for echo_seq in range(start_size, end_size+1, step): + pkt_send = auto_pad(Ether(src=src_mac, dst=dst_mac) / + IP(src=src_ip, dst=dst_ip) / + ICMP(id=echo_id, seq=echo_seq) / + Raw(load=data[0:echo_seq])) + sent_packets.append(pkt_send) + txq.send(pkt_send) + + ether = rxq.recv(ignore=sent_packets) + if ether is None: + rxq._proc.terminate() + raise RuntimeError( + 'ICMP echo reply seq {0} Rx timeout'.format(echo_seq)) + + if not ether.haslayer(IP): + rxq._proc.terminate() + raise RuntimeError( + 'Unexpected packet with no IPv4 received {0}'.format( + ether.__repr__())) + + ipv4 = ether['IP'] + + if not ipv4.haslayer(ICMP): + rxq._proc.terminate() + raise RuntimeError( + 'Unexpected packet with no ICMP received {0}'.format( + ipv4.__repr__())) + + icmpv4 = ipv4['ICMP'] + + if icmpv4.id != echo_id or icmpv4.seq != echo_seq: + rxq._proc.terminate() + raise RuntimeError( + 'Invalid ICMP echo reply received ID {0} seq {1} should be ' + + 'ID {2} seq {3}, {0}'.format(icmpv4.id, icmpv4.seq, echo_id, + echo_seq)) + + chksum = icmpv4.chksum + del icmpv4.chksum + tmp = ICMP(str(icmpv4)) + if tmp.chksum != chksum: + rxq._proc.terminate() + raise RuntimeError( + 'Invalid checksum {0} should be {1}'.format(chksum, tmp.chksum)) + recv_payload_len = ipv4.len - 20 - 8 + load = tmp['Raw'].load[0:recv_payload_len] + if load != data[0:echo_seq]: + rxq._proc.terminate() + raise RuntimeError( + 'Received ICMP payload does not match sent payload') + + rxq._proc.terminate() + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/resources/traffic_scripts/ipv6_sweep_ping.py b/resources/traffic_scripts/ipv6_sweep_ping.py index 2282f40f78..c79b74d760 100755 --- a/resources/traffic_scripts/ipv6_sweep_ping.py +++ b/resources/traffic_scripts/ipv6_sweep_ping.py @@ -58,7 +58,7 @@ def main(): # send ICMPv6 echo request with incremented data length and receive ICMPv6 # echo reply - for echo_seq in range(start_size, end_size, step): + for echo_seq in range(start_size, end_size+1, step): pkt_send = (Ether(src=src_mac, dst=dst_mac) / IPv6(src=src_ip, dst=dst_ip) / ICMPv6EchoRequest(id=echo_id, seq=echo_seq, |