Path: blob/main/tests/sys/netpfil/pf/frag-overlimit.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 *19from itertools import chain2021# index boundary 4096 |22# |--------------|23# ....24# |--------------|25# |--------------|26# ....----|27# |XXXX-----|28# |--------------|2930# this should trigger "fragment requeue limit exceeded" log in kernel3132def send(src, dst, send_if, recv_if):33pid = os.getpid()34eid = pid & 0xffff35payload = b"ABCDEFGHIJKLMNOP"36dummy = b"01234567"37fragsize = 6438boundary = 409639fragnum= int(boundary / fragsize)40packet = sp.IP(src=src, dst=dst)/ \41sp.ICMP(type='echo-request', id=eid)/ \42(int((boundary + boundary) / len(payload)) * payload)43frag = []44fid = pid & 0xffff45for i in chain(range(fragnum - 1), range(fragnum, fragnum + fragnum - 1)):46frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,47frag=(i * fragsize) >> 3, flags='MF') /48bytes(packet)[20 + i * fragsize:20 + (i + 1) * fragsize])49frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,50frag=(boundary + boundary - fragsize) >> 3) /51bytes(packet)[20 + boundary + boundary - fragsize:])52frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,53frag=(boundary - 8) >> 3, flags='MF')/54(dummy + bytes(packet)[20 + boundary:20 + boundary + 8]))55frag.append(sp.IP(src=src, dst=dst, proto=1, id=fid,56frag=(boundary - fragsize) >> 3, flags='MF') /57bytes(packet)[20 + boundary - fragsize:20 + boundary])58eth = []59for f in frag:60eth.append(sp.Ether() / f)6162if os.fork() == 0:63time.sleep(1)64for e in eth:65sp.sendp(e, iface=send_if)66time.sleep(0.001)67os._exit(0)6869ans = sp.sniff(iface=recv_if, timeout=10, filter=70"ip and src " + dst + " and dst " + src + " and icmp")71for a in ans:72if a and a.type == ETH_P_IP and \73a.payload.proto == 1 and \74a.payload.frag == 0 and \75sp.icmptypes[a.payload.payload.type] == 'echo-reply':76id = a.payload.payload.id77print("id=%#x" % (id))78if id != eid:79print("WRONG ECHO REPLY ID")80sys.exit(2)81print("ECHO REPLY")82sys.exit(1)83sys.exit(0)8485if __name__ == '__main__':86main(send)878889