xref: /freebsd/tests/sys/netpfil/pf/frag-overreplace.py (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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
29# this should trigger "frag tail overlap %d" and "frag head overlap %d"
30
31def send(src, dst, send_if, recv_if):
32	pid = os.getpid()
33	eid = pid & 0xffff
34	payload = b"ABCDEFGHIJKLMNOP"
35	dummy = b"01234567"
36	fragsize = 1024
37	boundary = 4096
38	fragnum = int(boundary / fragsize)
39	packet = sp.IP(src=src, dst=dst)/ \
40			sp.ICMP(type='echo-request', id=eid)/ \
41			(int((boundary + fragsize) / len(payload)) * payload)
42	frag = []
43	fid = pid & 0xffff
44
45	for i in range(fragnum - 1):
46		frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,
47			frag=(i * fragsize) >> 3, flags='MF') /
48			bytes(packet)[20 + i * fragsize:20 + (i + 1) * fragsize])
49	frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,
50		frag=(boundary - 8) >> 3, flags='MF') /
51		(dummy + bytes(packet)[20 + boundary:20 + boundary + 8]))
52	frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,
53		frag=(boundary - fragsize) >> 3, flags='MF') /
54		bytes(packet)[20 + boundary - fragsize:20 + boundary])
55	frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,
56		frag=(boundary) >> 3)/bytes(packet)[20 + boundary:])
57
58	eth=[]
59	for f in frag:
60		eth.append(sp.Ether() / f)
61
62	if os.fork() == 0:
63		time.sleep(1)
64		for e in eth:
65			sp.sendp(e, iface=send_if)
66			time.sleep(0.001)
67		os._exit(0)
68
69	ans = sp.sniff(iface=recv_if, timeout=3, filter="")
70	for a in ans:
71		if a and a.type == sp.ETH_P_IP and \
72				a.payload.proto == 1 and \
73				a.payload.frag == 0 and \
74				sp.icmptypes[a.payload.payload.type] == 'echo-reply':
75			id=a.payload.payload.id
76			if id != eid:
77				print("WRONG ECHO REPLY ID")
78				sys.exit(2)
79			sys.exit(0)
80	print("NO ECHO REPLY")
81	sys.exit(1)
82
83if __name__ == '__main__':
84	main(send)
85