xref: /freebsd/tests/sys/netpfil/pf/frag-overindex.py (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1#!/usr/bin/env python3
2#
3# SPDX-License-Identifier: ISC
4#
5# Copyright (c) 2012-2021 Alexander Bluhm <bluhm@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19from fragcommon import *
20
21#                               index boundary 4096 |
22# |--------------|
23#                 ....
24#                     |--------------|
25#                                              |XXXX-----|
26#                                    |--------------|
27#
28# this should trigger "frag index %d, new %d" log in kernel
29
30def send(src, dst, send_if, recv_if):
31	pid = os.getpid()
32	eid = pid & 0xffff
33	payload = b"ABCDEFGHIJKLMNOP"
34	dummy = b"01234567"
35	fragsize = 64
36	boundary = 4096
37	fragnum = int(boundary / fragsize)
38	packet = sp.IP(src=src, dst=dst)/ \
39			sp.ICMP(type='echo-request', id=eid)/ \
40			(int((boundary + 8) / len(payload)) * payload)
41	frag = []
42	fid = pid & 0xffff
43	for i in range(fragnum - 1):
44		frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,
45			frag=(i * fragsize) >> 3, flags='MF') /
46			bytes(packet)[20 + i * fragsize:20 + (i + 1) * fragsize])
47	frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,
48		frag=(boundary - 8) >> 3) /
49		(dummy + bytes(packet)[20 + boundary:20 + boundary + 8]))
50	frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,
51		frag=(boundary - fragsize) >> 3, flags='MF') /
52		bytes(packet)[20 + boundary - fragsize:20 + boundary])
53	eth = []
54	for f in frag:
55		eth.append(sp.Ether() / f)
56
57	if os.fork() == 0:
58		time.sleep(1)
59		for e in eth:
60			sp.sendp(e, iface=send_if)
61			time.sleep(0.001)
62		os._exit(0)
63
64	ans = sp.sniff(iface=recv_if, timeout=5)
65	print(ans)
66	for a in ans:
67		a.show()
68		if a and a.type == sp.ETH_P_IP and \
69				a.payload.proto == 1 and \
70				a.payload.frag == 0 and \
71				sp.icmptypes[a.payload.payload.type] == 'echo-reply':
72			id = a.payload.payload.id
73			print("id=%#x" % (id))
74			if id != eid:
75				print("WRONG ECHO REPLY ID")
76				sys.exit(2)
77			sys.exit(0)
78	print("NO ECHO REPLY")
79	exit(1)
80
81if __name__ == '__main__':
82	main(send)
83