3 # core packet generator for vinegar-ip
5 # This file is part of vinegar-ip, tools for IP transparency testing.
6 # vinegar-ip is Copyright (C) 2002 Ian Jackson
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software Foundation,
20 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 proc debug {level str} {
26 if {$level <= $debug_level} { puts stderr "debug\[$level] $str" }
29 proc manyset {list args} {
30 foreach val $list var $args {
37 proc start_gen {seed} {
39 random-bytes-init $seed
43 proc packet-len {p} { expr {[string length $p]/2} }
45 proc packet-csum-ip {packet} {
48 while {[regexp {^([0-9a-f]{4})(.*)$} $packet dummy this packet]} {
49 set cs [expr "\$cs + 0x$this"]
50 debug 7 [format "0x%s 0x%08x" $this $cs]
52 return [expr {(($cs & 0xffff) + (($cs >> 16) & 0xffff)) ^ 0xffff}]
55 proc packet-fromstring {s} {
60 namespace eval Random-Bytes {
61 namespace export random-bytes random-bytes-init
63 proc random-bytes-init {seed} {
65 catch { set h $fh; unset fh; close $h }
66 set fh [open |[list openssl bf-ofb < /dev/zero -e -k " $seed"] r]
67 fconfigure $fh -translation binary
69 proc random-bytes {n} {
72 if {[string length $x] != $n} {
73 set h $fh; unset fh; close $h
74 error "openssl bf-ofb exited unexpectedly"
76 set y [packet-fromstring $x]
81 namespace import Random-Bytes::*
83 proc choice-int {min max} {
84 set rv 0x[random-bytes 3]
86 int( double($rv) / double(0x1000000) * double($max+1-$min) )
91 proc choice-prob {cv def} {
92 set prob [config $cv $def]
93 set rv 0x[random-bytes 3]
94 return [expr {$rv < double($prob)*0x1000000}]
97 proc choice-mult {args} {
98 if {!([llength $args] % 2)} { error "choice-mult must have default" }
99 set h 0x[random-bytes 3]
100 set x [expr { double($h) / double(0x1000000) }]
102 set def [lindex $args end]
103 set args [lreplace $args end end]
104 foreach {val p} $args {
105 set cump [expr {$cump + double($p)}]
106 if {$x < $cump} { return $val }
112 upvar #0 getlog_log log
114 debug 2 "getlog $msg"
117 proc config {cv def} {
118 upvar #0 config/$cv v
119 if {[info exists v]} { return $v }
124 proc define {enum val name argnames body} {
125 upvar #0 enum/val2name/$enum v2n
126 upvar #0 enum/name2val/$enum n2v
129 proc enum/val/$enum/$val $argnames $body
132 proc depending-on {scope enum_and_var mtu mtuadjust args} {
133 upvar 1 $enum_and_var val
134 set mtu [expr {$mtu + $mtuadjust}]
135 set procname enum/val/$scope-$enum_and_var/[format %d $val]
136 if {[choice-prob $enum_and_var-unstruct 0.1] ||
137 [catch { info body $procname }]} {
140 get data rand 0 $mtu 1
143 uplevel 1 [list $procname] $mtu $args
148 proc get-for {scope} {
153 proc get {variable kind args} {
154 upvar 1 get/scope scope
155 upvar 1 $variable var
156 set var [eval [list get/$kind $scope $variable] $args]
159 proc get-config/number {val min max} { return $val }
160 proc get-config/v4addr {val} {
161 if {![regexp {^(\d+)\.(\d+)\.(\d+)\.(\d+)$} $val dummy a b c d]} {
162 error "bad v4addr ?$val?"
164 return [format 0x%02x%02x%02x%02x $a $b $c $d]
167 proc get-config {variable def kind args} {
168 # args currently ignored
169 upvar 1 get/scope scope
170 upvar 1 $variable var
171 set val [config $scope-$variable $def]
172 set var [eval [list get-config/$kind $val] $args]
175 proc get-enum-got {s v rv} {
176 upvar #0 enum/val2name/$s-$v v2n
177 if {[info exists v2n($rv)]} {
178 getlog "$v=$v2n($rv)\[$rv]"
185 proc get/enum-rand {s v min max} {
186 set rv [choice-int $min $max]
187 return [get-enum-got $s $v $rv]
190 proc get/enum-def {s v} {
191 upvar #0 enum/val2name/$s-$v v2n
192 set rv [choice-int 1 [array size v2n]]
193 set rv [lindex [lsort [array names v2n]] [expr {$rv-1}]]
194 return [get-enum-got $s $v $rv]
197 proc get/enum {s v min max prand} {
199 get any choice $prand
201 return [get/enum-rand $s $v $min $max]
203 return [get/enum-def $s $v]
207 proc get/number {s v min max} {
208 set rv [choice-int $min $max]
213 proc get/hex {s v min max} {
214 set rv [choice-int $min $max]
215 getlog [format %s=0x%x $v $rv]
219 proc get/hex32 {s v} {
220 set rv [random-bytes 4]
225 proc get/flag {s v defprob} {
226 set rv [choice-prob $s-$v $defprob]
227 if {$rv} { getlog "$v" } else { getlog "!$v" }
231 proc get/choice {s v defprob} {
232 set rv [choice-prob $s-$v $defprob]
233 if {$rv} { getlog "($v)" } else { getlog "(!$v)" }
237 proc get/rand {s v minlen maxlen blockbytes} {
239 if {$maxlen<0} { getlog (full!); return {} }
240 get l number [expr {$minlen/$blockbytes}] [expr {$maxlen/$blockbytes}]
241 return [random-bytes [expr {$l*$blockbytes}]]
244 proc get/ip-timestamp {s v} {
245 set rv [expr {[clock seconds] | 0x80000000}]
246 getlog "$v=[format %x $rv]"
250 proc get/v4addr {s v} {
254 for {set i 0} {$i<4} {incr i} {
255 set b [random-bytes 1]
257 append p $d [format %d 0x$b]
264 proc get/choice-mult {s v args} {
265 set rv [eval choice-mult $args]
270 proc get/string {s v minlen maxlen first rest} {
273 for {set l [choice-int $minlen $maxlen]} {$l>0} {incr l -1} {
274 set cn [choice-int 0 [expr {[string length $now]-1}]]
275 append o [string index $now $cn]
279 return [packet-fromstring $o]
282 proc get/ntstring {s v minlen maxlen first rest} {
283 set s [get/string $s $v $minlen $maxlen $first $rest]
285 append s [random-bytes $maxlen]
286 return [string range $s 0 [expr {$maxlen*2-1}]]
289 namespace eval Assembler {
290 namespace export assemble assembly-overwrite
292 proc assemble {outvarname format} {
293 # format should look like those RFC diagrams. +-+-+ stuff and
294 # good formatting is mandatory. You can have a single data
295 # item at the end ending in ..., which means append that data
298 # Field names are converted to lowercase; internal spaces
299 # are replaced with _. They are then assumed to be
300 # variable names in the caller's scope. The packet is
301 # assembled from those values (which must all be set)
302 # and stored in $varname in the caller's scope.
304 # Variables ?_whatever will be *set* with the location of the
305 # field in the string (in internal format); the corresponding
306 # `whatever' (with the ?_ stripped) will be read when assembling.
308 # Field names starting with digits are literal values instead.
311 upvar 1 $outvarname out
312 if {[catch { set parsed $cache($format) }]} {
313 set parsed [parse $format]
314 set cache($format) $parsed
317 manyset $parsed outbytes lout
318 set out [string repeat 00 $outbytes]
319 foreach {?_location varname locvarname} $lout {
320 if {[regexp {^[0-9]} $varname]} {
323 set value [uplevel 1 [list set $varname]]
325 if {[string length $locvarname]} {
326 upvar 1 $locvarname lv
330 assembly-overwrite out location $value
332 global errorInfo errorCode
334 "$errorInfo\n setting\n$varname at ${?_location}" \
340 proc parse {format} {
344 debug 7 "ASSEMBLY $format"
345 set format [exec expand << $format]
346 foreach l [split $format "\n"] {
348 if {[regexp -nocase {^ *\| +\| *$} $l]} {
349 if {![info exists wordbits]} {
350 error "vspace not in data @$lno\n?$l?"
353 } elseif {[regexp -nocase {^ *[|? a-z0-9.]+$} $l]} {
354 if {[info exists words]} {
355 error "data without delimline @$lno\n?$l?"
359 } elseif {[regexp {^ *[-+]+ *$} $l]} {
362 while {[regexp {^([-= ]+)\+(.*)$} $l dummy before after]} {
363 set atpos([string length $before]) $wordbits
365 set l "$before=$after"
366 append newlineform "@[string length $before]:$wordbits "
369 append newlineform $wordbits
370 if {[info exists lineform]} {
371 if {"$newlineform" != "$lineform"} {
372 error "format change @$lno\n?$l?\n$wordformat != $oldwordformat"
374 if {![info exists words] || $words<0} {
375 error "consecutive delimlines @$lno\n?$l?"
377 incr outbytes [expr {$words*$wordbits/8}]
379 while {[regexp -nocase \
380 {^([ =]*)\|( *[? a-z0-9]*[a-z0-9] *\.* *)\|(.*)$} \
381 $l dummy before midpart after]} {
382 debug 7 "RWORKG ?$l?"
384 error "two things at end @$lno\n?$l?"
386 set varname [string tolower [string trim $midpart]]
387 if {[regexp {(.*[a-z0-9])\s*\.\.\.$} $varname \
388 dummy realvarname]} {
389 set varname $realvarname
392 set varname [string map {{ } _} $varname]
393 set p1 [string length $before]
395 [string length $before] +
396 [string length $midpart] + 1
398 if {![info exists atpos($p1)] ||
399 ![info exists atpos($p2)]} {
400 error "vdelims not found @$lno $varname $p1 $p2 -- $lineform ?$cue?"
408 $atpos($p2) - $atpos($p1) + ($words-1)*$wordbits
412 error "atend not at byte @$lno\n?$l?"
414 set outbytes [expr {$bit1/8}]
415 set location [list $bit1 0 $varname]
417 set location [list $bit1 $bitlen $varname]
419 if {[regexp {^\?_(.*)} $varname dummy realvarname]} {
420 debug 7 "LOCATING $varname $location"
421 set locvarname $varname
422 set varname $realvarname
426 lappend lout $location $varname $locvarname
428 append l [string repeat = [string length $midpart]]
431 debug 7 "REMAIN ?$l?"
432 if {![regexp {^[ =]*\|? *$} $l]} {
433 error "unclear @$lno\n?$l?"
436 if {$wordbits % 8 || $wordbits >32} {
437 error "bad wordbits $wordbits @$lno ?$l? $newlineform"
439 set lineform $newlineform
441 catch { unset words }
442 } elseif {[regexp {^ *$} $l]} {
444 error "huh? @$lno ?$l?"
447 debug 7 "ASSEMBLY\n$outbytes\n$lout\n"
448 return [list $outbytes $lout]
451 proc assembly-overwrite {outvarname locvarnameex value} {
452 upvar 1 $outvarname out
453 upvar 1 ?_$locvarnameex location
454 manyset $location bit1 bitlen diag
455 if {$bitlen > 0 && $bitlen != 32 && $value >= (1<<$bitlen)} {
456 error "$diag $value >= 2**$bitlen"
458 if {!($bit1 % 8) && !($bitlen % 8)} {
459 set char0no [expr {$bit1/4}]
460 set charlen [expr {$bitlen/4}]
461 set chareno [expr {$char0no + $charlen -1}]
463 debug 8 "ASSEMBLY $diag == $value == 0x[format %x $value] (@$bit1+$bitlen)"
464 set repl [format %0${charlen}x $value]
465 set out [string replace $out $char0no $chareno $repl]
467 debug 8 "ASSEMBLY-ADD $diag (@$bit1)"
468 # bitlen==0 => append
469 set out [string range $out 0 $chareno]
473 while {$bitlen > 0} {
474 set byteno [expr {$bit1 / 8}]
475 set char0no [expr {$byteno*2}]
476 set char1no [expr {$char0no+1}]
477 set bytebit [expr {128>>($bit1 % 8)}]
478 set byte 0x[string range $out $char0no $char1no]
479 debug 7 "< $bit1+$bitlen $byteno $char0no $bytebit $byte $out"
481 ($value & (1<<($bitlen-1)))
483 : ($byte & ~$bytebit)
485 set out [string replace $out $char0no $char1no \
487 debug 8 "> $bit1+$bitlen $byteno $char0no $bytebit 0x[format %02x $byte] $out"
494 namespace import Assembler::*
496 proc gen_1_ip {mtu source_spec dest_spec} {
498 upvar #0 ip_proto proto
499 upvar #0 ip_source source
500 upvar #0 ip_dest dest
502 set source $source_spec
507 get tos hex 0x00 0xff
508 get id number 0x0000 0xffff
510 if {$df || ![choice-prob ip-midfrag 0.05]} {
515 get frag number 0 0x1fff
517 get-config ttl 255 number 0 255
518 get proto enum 1 255 0.05
519 set flags [expr {$df*2 + $mf}]
521 set header_checksum 0
525 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
526 |Version| ? IHL |TOS | ? Total Length |
527 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
529 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
530 | TTL | Proto | ? Header Checksum |
531 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
533 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
535 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
537 # we don't do any IP options
539 set ihl [packet-len $ip]
540 if {$ihl % 4} { error "ihl not mult of 4 bytes" }
541 assembly-overwrite ip ihl [expr {$ihl / 4}]
543 set body [depending-on ip proto $mtu -$ihl]
544 set total_length [expr {[packet-len $ip] + [packet-len $body]}]
546 assembly-overwrite ip total_length $total_length
547 assembly-overwrite ip header_checksum [packet-csum-ip $ip]
553 define ip-proto 1 icmp {mtu} {
556 get type enum 0 255 0.2
557 manyset [depending-on icmp type $mtu -4] body code
558 if {![string length $code]} { get code number 0 255 }
561 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
562 | Type | Code | ? Checksum |
563 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
565 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
567 assembly-overwrite icmp checksum [packet-csum-ip $icmp]
571 proc define-icmp-type-vanilla {num name} {
572 define icmp-type $num $name {mbl} "icmp-vanilla \$mbl [list $name]"
574 proc icmp-vanilla {mbl typename} {
575 get-for icmp-$typename
576 get code enum 0 255 0.4
577 get body rand 0 $mbl 1
578 return [list $body $code]
581 define-icmp-type-vanilla 3 unreach
582 define icmp-unreach-code 0 net {} {}
583 define icmp-unreach-code 1 host {} {}
584 define icmp-unreach-code 2 proto {} {}
585 define icmp-unreach-code 3 port {} {}
586 define icmp-unreach-code 4 fragneeded {} {}
587 define icmp-unreach-code 5 sourceroutefail {} {}
589 define-icmp-type-vanilla 11 timeout
590 define icmp-timeout-code 0 intransit {} {}
591 define icmp-timeout-code 1 fragment {} {}
593 define-icmp-type-vanilla 12 parameters
594 define icmp-parameters-code 0 seepointer {} {}
596 define-icmp-type-vanilla 4 sourcequench
597 define icmp-sourcequench-code 0 quench {} {}
599 define icmp-type 5 redirect {mbl} {
600 get-for icmp-redirect
601 get code enum 0 255 0.4
603 get data rand 0 [expr {$mbl-4}] 1
605 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
607 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 return [list $body $code]
614 define icmp-redirect-code 0 net {} {}
615 define icmp-redirect-code 1 host {} {}
616 define icmp-redirect-code 2 net+tos {} {}
617 define icmp-redirect-code 3 host+tos {} {}
619 define icmp-type 8 ping {mbl} { icmp-echo $mbl }
620 define icmp-type 0 pong {mbl} { icmp-echo $mbl }
621 proc icmp-echo {mbl} {
623 get code enum 0 255 0.4
626 get data rand 0 [expr {$mbl-8}] 1
628 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 return [list $body $code]
636 define icmp-echo-code 0 echo {} {}
638 define icmp-type 13 timestamp {mbl} { icmp-timestamp }
639 define icmp-type 14 timestampreply {mbl} { icmp-timestamp }
640 proc icmp-timestamp {} {
641 get-for icmp-timestamp
642 get code enum 0 255 0.4
645 get originate ip-timestamp
646 get receive ip-timestamp
647 get transmit ip-timestamp
649 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
651 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
653 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
655 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659 return [list $body $code]
661 define icmp-timestamp-code 0 timestamp {} {}
663 define icmp-type 15 inforequest {mbl} { icmp-inforeq }
664 define icmp-type 16 inforeply {mbl} { icmp-inforeq }
665 proc icmp-inforeq {} {
667 get code enum 0 255 0.4
671 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 return [list $body $code]
677 define icmp-inforeq-code 0 timestamp {} {}
679 # MAYADD ICMP traceroute RFC1393
680 # MAYADD ICMP router discovery RFC1256
683 define ip-proto 4 ip {mtu} {
688 gen_1_ip $mtu $source $dest
692 define ip-proto 2 igmp {mtu} {
694 get type enum 0 255 0.5
695 get timeout number 0 255
700 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
701 | Type | Timeout | ? Checksum |
702 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
704 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
706 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
709 if {[choice-prob igmp-extra 0.3]} {
710 get extra rand 1 [expr {$mtu - [packet-len $igmp]}] 1
711 assembly-overwrite igmp extra $extra
714 assembly-overwrite igmp checksum [packet-csum-ip $igmp]
718 define igmp-type 17 membquery {} {}
719 define igmp-type 16 membreport {} {}
720 define igmp-type 23 leavegroup {} {}
721 define igmp-type 18 membreport {} {}
724 define ip-proto 51 ah {mtu} {
727 get next number 0 255
728 get reserved hex 0 0xffff
730 get auth_data rand 0 [expr {$mtu-8}] 4
731 set length [packet-len $auth_data]
733 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
734 | Next | Length | RESERVED |
735 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
737 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
739 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
741 get payload rand 0 [expr {$mtu - [packet-len $ah]}] 1
748 get port enum-rand 0 0xffff
752 define ip-proto 17 udp {mtu} {
756 set csum_mode [choice-mult \
761 get style choice-mult \
767 if {"$style" != "random"} {
773 switch -exact $style {
774 random { set source_port [udp-rport]; set dest_port [udp-rport] }
775 request { set source_port [udp-rport]; set dest_port $def_port }
776 reply { set source_port $def_port; set dest_port [udp-rport] }
777 servers { set source_port $def_port; set dest_port $def_port }
780 if {"$style" != "random"} {
782 set data [depending-on udp port $mtu -8 $style]
784 get data rand 0 [expr {$mtu-8}] 1
790 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
791 | Source Port | Dest Port |
792 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
793 | ? Length | ? Checksum |
794 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
796 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
798 set udp_length [packet-len $udp]
799 assembly-overwrite udp length $udp_length
801 if {"$csum_mode" == "checksum_none"} {
805 global ip_source ip_dest ip_proto
807 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
809 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
811 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
812 | 0 | IP Proto | UDP length |
813 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
815 set checksum [packet-csum-ip "$pseudo$udp"]
816 if {!$checksum} { set checksum 0xffff }
817 if {"$csum_mode" == "checksum_bad"} {
818 get csumerror hex 1 0xffff
819 set checksum [expr {$checksum ^ $csumerror}]
822 assembly-overwrite udp checksum $checksum
826 define udp-port 50 remailck {mtu style} {
829 if {"$style" == "request"} {
830 get what choice-mult \
837 get what choice-mult \
844 switch -exact $what {
847 get user string 1 8 \
848 abcdefghijklmnopqrustuvwxyz \
849 abcdefghijklmnopqrustuvwxyz-0123456789_
853 get user rand 0 [expr {$mtu - 4}] 1
856 get auth enum 0 31 0.5
857 set user [depending-on remailck auth $mtu -4]
860 get auth hex 0 0xffff
865 get mail choice-mult \
871 switch -exact $mail {
885 get modified number 1 600
886 get read number 1 600
888 default { error "mail? $mail" }
891 default { error "what? $what" }
896 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
898 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
900 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
905 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
907 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
909 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
911 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
914 default { error "what?? $what" }
919 define remailck-auth 31 passwd {mtu} {
920 get-for remailck-passwd
921 get passwd string 6 8 \
922 0123456789abcdefghijklmnopqrstuvxwyz \
923 0123456789abcdefghijklmnopqrstuvxwyz
927 define udp-port 67 dhcpserv {mtu style} { return [dhcp $mtu] }
928 define udp-port 68 dhcpclient {mtu style} { return [dhcp $mtu] }
931 get op enum 0 255 0.2
932 get htype enum 0 255 0.2
934 get hops number 0 255
936 get secs number 0 300
942 set chaddr [random-bytes 16]
943 get sname ntstring 0 64 \
944 0123456789abcdefghijklmnopqrstuvwxyz \
945 0123456789abcdefghijklmnopqrstuvwxyz.-+
946 get file ntstring 0 128 / \
947 0123456789abcdefghijklmnopqrstuvwxyz.-+/_
950 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
951 | op | htype | hlen | hops |
952 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
954 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
956 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
958 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
960 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
962 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
964 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
966 append dhcp $chaddr $sname $file
970 define dhcp-op 1 request {} {}
971 define dhcp-op 2 reply {} {}
972 define dhcp-htype 1 ethernet {} {}
975 define ip-proto 6 tcp {mtu} {
979 get source_port number 0 65535
980 get dest_port number 0 65535
981 get event choice-mult \
992 switch -exact $event {
993 connect { set s 1; set a 0 }
996 reset { set a 0; set r 1 }
1004 default { error "event? $event" }
1008 if {[choice-prob tcp-smallwindow 0.7]} {
1009 get window number 0 1
1011 get window hex 0 0xffff
1015 get urg hex 0 0xffff
1018 get optmode choice-mult badopt 0.3 opt 0.3 noopt
1019 switch -exact $optmode {
1022 get options rand 1 60 1
1026 while {$nooi || [choice-prob tcp-opts-more 0.4]} {
1028 get opt enum 1 255 0.5
1036 set data [depending-on tcp opt 6 0]
1039 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1040 | Opt | ? Option Len |
1041 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1043 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1045 assembly-overwrite option option_len [packet-len $option]
1047 append options $option
1052 if {[choice-prob reserved-nonzero 0.25]} {
1053 get reserved hex 0 0x3f
1058 if {[packet-len $options] % 4 || [get optxpad choice 0.15]} {
1059 if {"$optmode" != "badopt"} { append options 00 }
1060 set padlen [expr {3 - ([packet-len $options] + 3) % 4}]
1061 append options [random-bytes $padlen]
1067 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1068 | Source Port | Dest Port |
1069 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1071 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1073 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1074 |? D Off| Reserved |U|A|P|R|S|F| Window |
1075 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1076 | ? Checksum | Urg |
1077 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1079 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1082 set d_off [expr {([packet-len $packet]/4) & 0x0f}]
1083 assembly-overwrite packet d_off $d_off
1085 if {!($s || $r) || [get unexpdata flag 0.2]} {
1086 get data rand 0 [expr {$mtu - [packet-len $packet]}] 1
1089 set tcp_length [packet-len $packet]
1091 global ip_source ip_dest ip_proto
1093 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1095 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1097 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1098 | 0 | IP Proto | TCP length |
1099 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1102 set csum [packet-csum-ip "$pseudo$packet"]
1103 if {[choice-prob tcp-badcsum 0.1]} {
1104 get csumerror hex 1 0xffff
1105 set csum [expr {$csum ^ $csumerror}]
1107 assembly-overwrite packet checksum $csum
1111 define tcp-opt 2 mss {mdl} {
1113 get mss hex 0 0xffff
1115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1123 namespace eval PCap {
1124 namespace export pcap_open pcap_write pcap_write_raw pcap_close
1126 proc pcap_open {fn} {
1130 fconfigure $fh -translation binary
1133 proc pcap_close {} {
1135 if {![info exists fh]} return
1140 proc pcap_write_raw {packet} {
1142 if {![info exists fh]} return
1143 puts -nonewline $fh [binary format H* $packet]
1146 proc pcap_write {valdeflist} {
1147 foreach {kind valvar} $valdeflist {
1148 if {![regexp {^([usx])([0-9]+)} $kind dummy mode bits]} {
1149 error "unknown kind $kind for $valvar"
1151 set value [uplevel 1 [list set $valvar]]
1152 if {$bits % 8} { error "bits must be mult 8 in $kind for $valvar" }
1153 if {"$mode" != "x"} {
1156 for {set i 0} {$i<$bits/8} {incr i} {
1157 append v [format %02x [expr {$value & 0xff}]]
1158 set value [expr {$value >> 8}]
1160 if {$value != 0 && $value != -1} {
1161 error "value $ov more than $bits bits (residue=$value)"
1165 if {[string length $value] != $bits/4} {
1166 error "$valvar value $value wrong length, not $bits bits"
1168 pcap_write_raw $value
1172 namespace import PCap::*
1175 global getlog_log errorInfo mtu fake_time_t
1179 get-config source 127.0.0.1 v4addr
1180 get-config dest 127.0.0.1 v4addr
1184 set packet [gen_1_ip $mtu $source $dest]
1185 puts stdout "[format %6s $seed] $getlog_log\n $packet"
1187 puts stderr "\nERROR\n$seed\n\n$emsg\n\n$errorInfo\n\n"
1188 puts stdout "[format %6s $seed] error"
1190 set ts_sec [incr fake_time_t]
1193 set l [packet-len $packet]
1194 if {$l < $minframelen} {
1195 append packet [string repeat 00 [expr {$minframelen - $l}]]
1199 set llpkt [random-bytes 12]
1201 append llpkt $packet
1203 set len [packet-len "$llpkt"]
1210 pcap_write_raw $llpkt
1217 if {![llength $argv]} { error "need another arg" }
1218 set a [lindex $argv 0]
1219 set argv [lrange $argv 1 end]
1223 proc nextarg_num {} { return [expr {[nextarg] + 0}] }
1229 while {[regexp {^\-\-} [lindex $argv 0]]} {
1231 switch -exact -- $o {
1232 --infinite { set upto -1 }
1233 --debug { set debug_level [nextarg_num] }
1234 --upto { set upto [nextarg_num] }
1235 --write { pcap_open [nextarg] }
1236 --mtu { set mtu [nextarg_num] }
1237 --xseed { set xseed [nextarg] }
1238 --source { set config/ip-source [nextarg] }
1239 --dest { set config/ip-dest [nextarg] }
1240 default { error "bad option $o" }
1265 set fake_time_t [clock seconds]
1267 if {[llength $argv]} {
1268 foreach count $argv { emit "$xseed$count" }
1270 if {![string length $upto]} { set upto 100 }
1271 for {set count 1} {$upto<0 || $count<=$upto} {incr count} {