3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2017 Kristof Provost <kp@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 import scapy.all as sp
32 from sniffer import Sniffer
34 PAYLOAD_MAGIC = bytes.fromhex('42c0ffee')
36 def check_ping_request(args, packet):
38 return check_ping6_request(args, packet)
40 return check_ping4_request(args, packet)
42 def check_ping4_request(args, packet):
44 Verify that the packet matches what we'd have sent
48 ip = packet.getlayer(sp.IP)
54 icmp = packet.getlayer(sp.ICMP)
57 if sp.icmptypes[icmp.type] != 'echo-request':
60 raw = packet.getlayer(sp.Raw)
63 if raw.load != PAYLOAD_MAGIC:
66 # Wait to check expectations until we've established this is the packet we
69 if ip.tos != int(args.expect_tos[0]):
70 print("Unexpected ToS value %d, expected %d" \
71 % (ip.tos, int(args.expect_tos[0])))
76 def check_ping6_request(args, packet):
78 Verify that the packet matches what we'd have sent
82 ip = packet.getlayer(sp.IPv6)
88 icmp = packet.getlayer(sp.ICMPv6EchoRequest)
91 if icmp.data != PAYLOAD_MAGIC:
96 def ping(send_if, dst_ip, args):
98 ip = sp.IP(dst=dst_ip)
99 icmp = sp.ICMP(type='echo-request')
100 raw = sp.raw(PAYLOAD_MAGIC)
103 ip.tos = int(args.send_tos[0])
105 req = ether / ip / icmp / raw
106 sp.sendp(req, iface=send_if, verbose=False)
108 def ping6(send_if, dst_ip, args):
110 ip6 = sp.IPv6(dst=dst_ip)
111 icmp = sp.ICMPv6EchoRequest(data=sp.raw(PAYLOAD_MAGIC))
113 req = ether / ip6 / icmp
114 sp.sendp(req, iface=send_if, verbose=False)
117 parser = argparse.ArgumentParser("pft_ping.py",
118 description="Ping test tool")
119 parser.add_argument('--sendif', nargs=1,
121 help='The interface through which the packet(s) will be sent')
122 parser.add_argument('--recvif', nargs=1,
123 help='The interface on which to expect the ICMP echo response')
124 parser.add_argument('--ip6', action='store_true',
126 parser.add_argument('--to', nargs=1,
128 help='The destination IP address for the ICMP echo request')
131 parser.add_argument('--send-tos', nargs=1,
132 help='Set the ToS value for the transmitted packet')
135 parser.add_argument('--expect-tos', nargs=1,
136 help='The expected ToS value in the received packet')
138 args = parser.parse_args()
140 # We may not have a default route. Tell scapy where to start looking for routes
141 sp.conf.iface6 = args.sendif[0]
144 if not args.recvif is None:
145 sniffer = Sniffer(args, check_ping_request)
148 ping6(args.sendif[0], args.to[0], args)
150 ping(args.sendif[0], args.to[0], args)
155 if sniffer.foundCorrectPacket:
160 if __name__ == '__main__':