Path: blob/main/tests/sys/netpfil/pf/frag-overindex.py
39536 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#27# this should trigger "frag index %d, new %d" log in kernel2829def send(src, dst, send_if, recv_if):30pid = os.getpid()31eid = pid & 0xffff32payload = b"ABCDEFGHIJKLMNOP"33dummy = b"01234567"34fragsize = 6435boundary = 409636fragnum = int(boundary / fragsize)37packet = sp.IP(src=src, dst=dst)/ \38sp.ICMP(type='echo-request', id=eid)/ \39(int((boundary + 8) / len(payload)) * payload)40frag = []41fid = pid & 0xffff42for i in range(fragnum - 1):43frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,44frag=(i * fragsize) >> 3, flags='MF') /45bytes(packet)[20 + i * fragsize:20 + (i + 1) * fragsize])46frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,47frag=(boundary - 8) >> 3) /48(dummy + bytes(packet)[20 + boundary:20 + boundary + 8]))49frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,50frag=(boundary - fragsize) >> 3, flags='MF') /51bytes(packet)[20 + boundary - fragsize:20 + boundary])52eth = []53for f in frag:54eth.append(sp.Ether() / f)5556if os.fork() == 0:57time.sleep(1)58for e in eth:59sp.sendp(e, iface=send_if)60time.sleep(0.001)61os._exit(0)6263ans = sp.sniff(iface=recv_if, timeout=5)64print(ans)65for a in ans:66a.show()67if a and a.type == sp.ETH_P_IP and \68a.payload.proto == 1 and \69a.payload.frag == 0 and \70sp.icmptypes[a.payload.payload.type] == 'echo-reply':71id = a.payload.payload.id72print("id=%#x" % (id))73if id != eid:74print("WRONG ECHO REPLY ID")75sys.exit(2)76sys.exit(0)77print("NO ECHO REPLY")78exit(1)7980if __name__ == '__main__':81main(send)828384