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