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