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
31 logging.getLogger("scapy").setLevel(logging.CRITICAL)
32 import scapy.all as sp
35 from sniffer import Sniffer
37 PAYLOAD_MAGIC = bytes.fromhex('42c0ffee')
39 def check_ping_request(args, packet):
41 return check_ping6_request(args, packet)
43 return check_ping4_request(args, packet)
45 def check_ping4_request(args, packet):
47 Verify that the packet matches what we'd have sent
51 ip = packet.getlayer(sp.IP)
57 icmp = packet.getlayer(sp.ICMP)
60 if sp.icmptypes[icmp.type] != 'echo-request':
63 raw = packet.getlayer(sp.Raw)
66 if raw.load != PAYLOAD_MAGIC:
69 # Wait to check expectations until we've established this is the packet we
72 if ip.tos != int(args.expect_tos[0]):
73 print("Unexpected ToS value %d, expected %d" \
74 % (ip.tos, int(args.expect_tos[0])))
79 def check_ping6_request(args, packet):
81 Verify that the packet matches what we'd have sent
85 ip = packet.getlayer(sp.IPv6)
91 icmp = packet.getlayer(sp.ICMPv6EchoRequest)
94 if icmp.data != PAYLOAD_MAGIC:
99 def ping(send_if, dst_ip, args):
101 ip = sp.IP(dst=dst_ip)
102 icmp = sp.ICMP(type='echo-request')
103 raw = sp.raw(PAYLOAD_MAGIC)
106 ip.tos = int(args.send_tos[0])
108 req = ether / ip / icmp / raw
109 sp.sendp(req, iface=send_if, verbose=False)
111 def ping6(send_if, dst_ip, args):
113 ip6 = sp.IPv6(dst=dst_ip)
114 icmp = sp.ICMPv6EchoRequest(data=sp.raw(PAYLOAD_MAGIC))
116 req = ether / ip6 / icmp
117 sp.sendp(req, iface=send_if, verbose=False)
119 def check_tcpsyn(args, packet):
122 ip = packet.getlayer(sp.IP)
128 tcp = packet.getlayer(sp.TCP)
135 new_chksum = sp.IP(sp.raw(ip)).chksum
136 if chksum != new_chksum:
137 print("Expected IP checksum %x but found %x\n" % (new_cshkum, chksum))
140 # Verify TCP checksum
142 packet_raw = sp.raw(packet)
144 newpacket = sp.Ether(sp.raw(packet[sp.Ether]))
145 new_chksum = newpacket[sp.TCP].chksum
146 if chksum != new_chksum:
147 print("Expected TCP checksum %x but found %x\n" % (new_chksum, chksum))
152 def tcpsyn(send_if, dst_ip, args):
153 opts=[('Timestamp', (1, 1)), ('MSS', 1280)]
155 if args.tcpopt_unaligned:
156 opts = [('NOP', 0 )] + opts
159 ip = sp.IP(dst=dst_ip)
160 tcp = sp.TCP(dport=666, flags='S', options=opts)
162 req = ether / ip / tcp
163 sp.sendp(req, iface=send_if, verbose=False)
167 parser = argparse.ArgumentParser("pft_ping.py",
168 description="Ping test tool")
169 parser.add_argument('--sendif', nargs=1,
171 help='The interface through which the packet(s) will be sent')
172 parser.add_argument('--recvif', nargs=1,
173 help='The interface on which to expect the ICMP echo response')
174 parser.add_argument('--ip6', action='store_true',
176 parser.add_argument('--to', nargs=1,
178 help='The destination IP address for the ICMP echo request')
181 parser.add_argument('--tcpsyn', action='store_true',
182 help='Send a TCP SYN packet')
183 parser.add_argument('--tcpopt_unaligned', action='store_true',
184 help='Include unaligned TCP options')
187 parser.add_argument('--send-tos', nargs=1,
188 help='Set the ToS value for the transmitted packet')
191 parser.add_argument('--expect-tos', nargs=1,
192 help='The expected ToS value in the received packet')
194 args = parser.parse_args()
196 # We may not have a default route. Tell scapy where to start looking for routes
197 sp.conf.iface6 = args.sendif[0]
200 if not args.recvif is None:
201 checkfn=check_ping_request
205 sniffer = Sniffer(args, checkfn)
208 tcpsyn(args.sendif[0], args.to[0], args)
211 ping6(args.sendif[0], args.to[0], args)
213 ping(args.sendif[0], args.to[0], args)
218 if sniffer.foundCorrectPacket:
223 if __name__ == '__main__':