xref: /freebsd-src/tests/sys/netpfil/pf/pft_ether.py (revision 6d1471fda81adcab5c7098f262c9e07b2d6a9fbb)
107ffa50bSKristof Provost#!/usr/bin/env python3
207ffa50bSKristof Provost#
307ffa50bSKristof Provost# SPDX-License-Identifier: BSD-2-Clause
407ffa50bSKristof Provost#
507ffa50bSKristof Provost# Copyright © 2022. Rubicon Communications, LLC (Netgate). All Rights Reserved.
607ffa50bSKristof Provost#
707ffa50bSKristof Provost# Redistribution and use in source and binary forms, with or without
807ffa50bSKristof Provost# modification, are permitted provided that the following conditions
907ffa50bSKristof Provost# are met:
1007ffa50bSKristof Provost# 1. Redistributions of source code must retain the above copyright
1107ffa50bSKristof Provost#    notice, this list of conditions and the following disclaimer.
1207ffa50bSKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
1307ffa50bSKristof Provost#    notice, this list of conditions and the following disclaimer in the
1407ffa50bSKristof Provost#    documentation and/or other materials provided with the distribution.
1507ffa50bSKristof Provost#
1607ffa50bSKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1707ffa50bSKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1807ffa50bSKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1907ffa50bSKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2007ffa50bSKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2107ffa50bSKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2207ffa50bSKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2307ffa50bSKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2407ffa50bSKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2507ffa50bSKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2607ffa50bSKristof Provost# SUCH DAMAGE.
2707ffa50bSKristof Provost#
2807ffa50bSKristof Provost
2907ffa50bSKristof Provostimport argparse
3007ffa50bSKristof Provostimport logging
3107ffa50bSKristof Provostlogging.getLogger("scapy").setLevel(logging.CRITICAL)
3207ffa50bSKristof Provostimport scapy.all as sp
3307ffa50bSKristof Provostimport socket
3407ffa50bSKristof Provostimport sys
3507ffa50bSKristof Provost
3607ffa50bSKristof ProvostPAYLOAD_MAGIC = bytes.fromhex('42c0ffee')
3707ffa50bSKristof Provost
3807ffa50bSKristof Provostdef ping(send_if, dst_ip, length):
3907ffa50bSKristof Provost	ether = sp.Ether()
4007ffa50bSKristof Provost	ip = sp.IP(dst=dst_ip)
4107ffa50bSKristof Provost	icmp = sp.ICMP(type='echo-request')
4207ffa50bSKristof Provost	raw = sp.raw(PAYLOAD_MAGIC)
4307ffa50bSKristof Provost
4407ffa50bSKristof Provost	req = ether / ip / icmp / raw
4507ffa50bSKristof Provost	req = req.build()[0:length]
4607ffa50bSKristof Provost
4707ffa50bSKristof Provost	sp.sendp(req, iface=send_if, verbose=False)
4807ffa50bSKristof Provost
4907ffa50bSKristof Provostdef main():
5007ffa50bSKristof Provost	parser = argparse.ArgumentParser("pft_ether.py",
5107ffa50bSKristof Provost		description="Ethernet test tool")
5207ffa50bSKristof Provost	parser.add_argument('--sendif', nargs=1,
5307ffa50bSKristof Provost		required=True,
5407ffa50bSKristof Provost		help='The interface through which the packet(s) will be sent')
5507ffa50bSKristof Provost	parser.add_argument('--to', nargs=1,
5607ffa50bSKristof Provost		required=True,
5707ffa50bSKristof Provost		help='The destination IP address for the ICMP echo request')
5807ffa50bSKristof Provost	parser.add_argument('--len', nargs=1,
5907ffa50bSKristof Provost		required=True,
6007ffa50bSKristof Provost		help='The length of the packet')
6107ffa50bSKristof Provost
6207ffa50bSKristof Provost	args = parser.parse_args()
6307ffa50bSKristof Provost
64*6d1471fdSKristof Provost	if '-' in args.len[0]:
65*6d1471fdSKristof Provost		s=args.len[0].split('-')
66*6d1471fdSKristof Provost		for i in range(int(s[0]), int(s[1]) + 1):
67*6d1471fdSKristof Provost			ping(args.sendif[0], args.to[0], i)
68*6d1471fdSKristof Provost	else:
6907ffa50bSKristof Provost		ping(args.sendif[0], args.to[0], int(args.len[0]))
7007ffa50bSKristof Provost
7107ffa50bSKristof Provostif __name__ == '__main__':
7207ffa50bSKristof Provost	main()
73