
# NOTE: start and len are the payload bytes, and do NOT include the IP header

proc make_frag { orig frag start len } {
	if {[pproto $orig 0] != "ip"} {
		puts "Packet must begin with IP header"
		return error
	}
	if {($start & 0xfff8) != $start} {
		puts "Packet frag boundaries must be mod 8"
		return error
	}
	if {($len&0xfff8) != $len && $len != [plen $orig 1]} {
		puts "Packet len boundaries must be mod 8"
		return error
	}
	pinit ip $frag
	pcopy $frag $orig		;# start with a copy
	pcopy $frag/end $orig/data	;# XXX icmp
	plen $frag 1 $len	;# make it the right size

	if {$start + $len < [plen $orig 1]} {
		;# must set MF bit - more frags in this packet
		pset $frag ip off [expr ($start/8) + 0x2000]
	} else {
		pset $frag ip off [expr $start/8]
	}
	pset $frag ip len [expr $len + [plen $orig 0]]

	;# now copy the correct data bytes to final resting spots
	set rstart [expr [plen $orig 0] + $start]
	set wstart [plen $orig 0]
	while {$len > 0} {
		pwrite $frag b $wstart 0x[pread $orig b $rstart]
		incr rstart; incr wstart; incr len -1
	}

}
