Path: blob/main/tests/sys/netpfil/common/pft_icmp_check.py
39534 views
#!/usr/bin/env python31#2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2021 Rubicon Communications, LLC (Netgate)5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728import argparse29import logging30logging.getLogger("scapy").setLevel(logging.CRITICAL)31import random32import scapy.all as sp33import socket34import sys35from sniffer import Sniffer3637PAYLOAD_MAGIC = bytes.fromhex('42c0ffee')3839def ping(send_if, dst_ip, args):40ether = sp.Ether()41ip = sp.IP(dst=dst_ip, src=args.fromaddr[0])42icmp = sp.ICMP(type='echo-request')43raw = sp.raw(PAYLOAD_MAGIC * 250) # We want 1000 bytes payload, -ish4445ip.flags = 2 # Don't fragment46icmp.seq = random.randint(0, 65535)47args.icmp_seq = icmp.seq4849req = ether / ip / icmp / raw50sp.sendp(req, iface=send_if, verbose=False)5152def check_icmp_too_big(args, packet):53"""54Verify that this is an ICMP packet too big error, and that the IP addresses55in the payload packet match expectations.56"""57icmp = packet.getlayer(sp.ICMP)58if not icmp:59return False6061if not icmp.type == 3:62return False63ip = packet.getlayer(sp.IPerror)64if not ip:65return False6667if ip.src != args.fromaddr[0]:68print("Incorrect src addr %s" % ip.src)69return False70if ip.dst != args.to[0]:71print("Incorrect dst addr %s" % ip.dst)72return False7374icmp2 = packet.getlayer(sp.ICMPerror)75if not icmp2:76print("IPerror doesn't contain ICMP")77return False78if icmp2.seq != args.icmp_seq:79print("Incorrect icmp seq %d != %d" % (icmp2.seq, args.icmp_seq))80return False81return True8283def main():84parser = argparse.ArgumentParser("pft_icmp_check.py",85description="ICMP error validation tool")86parser.add_argument('--to', nargs=1, required=True,87help='The destination IP address')88parser.add_argument('--fromaddr', nargs=1, required=True,89help='The source IP address')90parser.add_argument('--sendif', nargs=1, required=True,91help='The interface through which the packet(s) will be sent')92parser.add_argument('--recvif', nargs=1,93help='The interface on which to expect the ICMP error')9495args = parser.parse_args()96sniffer = None97if not args.recvif is None:98sniffer = Sniffer(args, check_icmp_too_big, args.recvif[0])99100ping(args.sendif[0], args.to[0], args)101102if sniffer:103sniffer.join()104105if sniffer.correctPackets:106sys.exit(0)107else:108sys.exit(1)109110if __name__ == '__main__':111main()112113114