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