Path: blob/main/tests/sys/netpfil/pf/frag-overreplace.py
39507 views
#!/usr/bin/env python31#2# SPDX-License-Identifier: ISC3#4# Copyright (c) 2012-2021 Alexander Bluhm <[email protected]>5#6# Permission to use, copy, modify, and distribute this software for any7# purpose with or without fee is hereby granted, provided that the above8# copyright notice and this permission notice appear in all copies.9#10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.1718from fragcommon import *1920# index boundary 4096 |21# |--------------|22# ....23# |--------------|24# |XXXX-----|25# |--------------|26# |--------------|2728# this should trigger "frag tail overlap %d" and "frag head overlap %d"2930def send(src, dst, send_if, recv_if):31pid = os.getpid()32eid = pid & 0xffff33payload = b"ABCDEFGHIJKLMNOP"34dummy = b"01234567"35fragsize = 102436boundary = 409637fragnum = int(boundary / fragsize)38packet = sp.IP(src=src, dst=dst)/ \39sp.ICMP(type='echo-request', id=eid)/ \40(int((boundary + fragsize) / len(payload)) * payload)41frag = []42fid = pid & 0xffff4344for i in range(fragnum - 1):45frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,46frag=(i * fragsize) >> 3, flags='MF') /47bytes(packet)[20 + i * fragsize:20 + (i + 1) * fragsize])48frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,49frag=(boundary - 8) >> 3, flags='MF') /50(dummy + bytes(packet)[20 + boundary:20 + boundary + 8]))51frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,52frag=(boundary - fragsize) >> 3, flags='MF') /53bytes(packet)[20 + boundary - fragsize:20 + boundary])54frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,55frag=(boundary) >> 3)/bytes(packet)[20 + boundary:])5657eth=[]58for f in frag:59eth.append(sp.Ether() / f)6061if os.fork() == 0:62time.sleep(1)63for e in eth:64sp.sendp(e, iface=send_if)65time.sleep(0.001)66os._exit(0)6768ans = sp.sniff(iface=recv_if, timeout=3, filter="")69for a in ans:70if a and a.type == sp.ETH_P_IP and \71a.payload.proto == 1 and \72a.payload.frag == 0 and \73sp.icmptypes[a.payload.payload.type] == 'echo-reply':74id=a.payload.payload.id75if id != eid:76print("WRONG ECHO REPLY ID")77sys.exit(2)78sys.exit(0)79print("NO ECHO REPLY")80sys.exit(1)8182if __name__ == '__main__':83main(send)848586