3 # validate the IPv4 header checksum.
4 # $bytes[] is an array of 16bit values, with $cnt elements in the array.
8 for ($i = 0; $i < $#bytes; $i++) {
9 printf "%04x ", $bytes[$i];
16 local($start) = $_[1];
21 for ($idx = $start, $lsum = $seed; $idx < $max; $idx++) {
22 $lsum += $bytes[$idx];
24 $lsum = ($lsum & 0xffff) + ($lsum >> 16);
25 $lsum = ~$lsum & 0xffff;
31 $hl = $bytes[$base] / 256;
32 return if (($hl >> 4) != 4); # IPv4 ?
34 $hl <<= 1; # get the header length in 16bit words
36 $hs = &dosum
(0, $base, $base + $hl);
37 $osum = $bytes[$base + 5];
40 $bytes[$base + 5] = 0;
41 $hs2 = &dosum
(0, $base, $base + $hl);
42 $bytes[$base + 5] = $osum;
43 printf " IP: ($hl,%x) %x != %x", $hs, $osum, $hs2;
45 print " IP($base): ok ";
49 # Recognise TCP & UDP and calculate checksums for each of these.
51 if (($bytes[$base + 4] & 0xff) == 6) {
55 if (($bytes[$base + 4] & 0xff) == 17) {
59 if (($bytes[$base + 4] & 0xff) == 1) {
69 local($hl) = $bytes[$base] / 256;
70 return if (($hl >> 4) != 4);
71 return if ($bytes[$base + 3] & 0x1fff);
77 local($len) = $bytes[$base + 1] - ($hl << 1);
79 $hs += $bytes[$base + 6]; # source address
80 $hs += $bytes[$base + 7];
81 $hs += $bytes[$base + 8]; # destination address
82 $hs += $bytes[$base + 9];
85 local($thl) = $bytes[$base + $hl + 6] >> 8;
89 $x = $bytes[$base + 1];
90 $y = ($cnt - $base) * 2;
92 if ($bytes[$base + 1] > ($cnt - $base) * 2) {
93 print "[cnt=$cnt base=$base]";
94 $x = $bytes[$base + 1];
95 $y = ($cnt - $base) * 2;
97 } elsif (($cnt - $base) * 2 < $hl + 20) {
98 $x = ($cnt - $base) * 2;
101 } elsif (($cnt - $base) * 2 < $hl + $thl) {
102 $x = ($cnt - $base) * 2;
105 } elsif ($len < $thl) {
106 $x = ($cnt - $base) * 2;
112 print " TCP: missing data($x $y $z) $hl";
117 local($tcpat) = $base + $hl;
118 $hs = &dosum
($tcpsum, $tcpat, $cnt);
120 local($osum) = $bytes[$tcpat + 8];
121 $bytes[$base + $hl + 8] = 0;
122 $hs2 = &dosum
($tcpsum, $tcpat, $cnt);
123 $bytes[$tcpat + 8] = $osum;
124 printf " TCP: (%x) %x != %x", $hs, $osum, $hs2;
126 print " TCP: ok ($x $y)";
131 local($base) = $_[0];
132 local($hl) = $bytes[0] / 256;
133 return if (($hl >> 4) != 4);
134 return if ($bytes[3] & 0x1fff);
139 local($hs) = 17; # UDP
140 local($len) = $bytes[$base + 1] - ($hl << 1);
142 $hs += $bytes[$base + 6]; # source address
143 $hs += $bytes[$base + 7];
144 $hs += $bytes[$base + 8]; # destination address
145 $hs += $bytes[$base + 9];
146 local($udpsum) = $hs;
148 if ($bytes[$base + 1] > ($cnt - $base) * 2) {
149 print " UDP: missing data(1)";
151 } elsif ($bytes[$base + 1] < ($hl << 1) + 8) {
152 print " UDP: missing data(2)";
154 } elsif (($cnt - $base) * 2 < ($hl << 1) + 8) {
155 print " UDP: missing data(3)";
159 local($udpat) = $base + $hl;
160 $hs = &dosum
($udpsum, $udpat, $cnt);
161 local($osum) = $bytes[$udpat + 3];
164 # It is valid for UDP packets to have a 0 checksum field.
165 # If it is 0, then display what it would otherwise be.
168 printf " UDP: => %x", $hs;
170 $bytes[$udpat + 3] = 0;
171 $hs2 = &dosum
($udpsum, $udpat, $cnt);
172 $bytes[$udpat + 3] = $osum;
173 printf " UDP: (%x) %x != %x", $hs, $osum, $hs2;
180 local($base) = $_[0];
181 local($hl) = $bytes[$base + 0] / 256;
182 return if (($hl >> 4) != 4);
183 return if ($bytes[3] & 0x1fff);
190 local($len) = $bytes[$base + 1] - ($hl << 1);
192 if ($bytes[$base + 1] > ($cnt - $base) * 2) {
193 print " ICMP: missing data(1)";
195 } elsif ($bytes[$base + 1] < ($hl << 1) + 8) {
196 print " ICMP: missing data(2)";
198 } elsif (($cnt - $base) * 2 < ($hl << 1) + 8) {
199 print " ICMP: missing data(3)";
203 local($osum) = $bytes[$base + $hl + 1];
204 $bytes[$base + $hl + 1] = 0;
205 $hs2 = &dosum
(0, $base + $hl, $cnt);
206 $bytes[$base + $hl + 1] = $osum;
209 printf " ICMP: (%x) %x != %x", $hs, $osum, $hs2;
214 $type = $bytes[$hl] >> 8;
215 if ($type == 3 || $type == 4 || $type == 5 ||
216 $type == 11 || $type == 12) {
222 while ($#ARGV >= 0) {
223 open(I
, "$ARGV[0]") || die $!;
224 print "--- $ARGV[0] ---\n";
231 # If the first non-comment, non-empty line of input starts
232 # with a '[', then allow the input to be a multi-line hex
233 # string, otherwise it has to be all on one line.
250 # look for 16 bits, represented with leading 0's as required,
254 while (/^[0-9a-fA-F][0-9a-fA-F] [0-9a-fA-F][0-9a-fA-F] .*/) {
255 s/^([0-9a-fA-F][0-9a-fA-F]) ([0-9a-fA-F][0-9a-fA-F]) (.*)/$1$2 $3/;
257 while (/.* [0-9a-fA-F][0-9a-fA-F] [0-9a-fA-F][0-9a-fA-F] .*/) {
259 s/(.*?) ([0-9a-fA-F][0-9a-fA-F]) ([0-9a-fA-F][0-9a-fA-F]) (.*)/$1 $2$3 $4/g;
261 if (/.* [0-9a-fA-F][0-9a-fA-F] [0-9a-fA-F][0-9a-fA-F]/) {
263 s/(.*?) ([0-9a-fA-F][0-9a-fA-F]) ([0-9a-fA-F][0-9a-fA-F])/$1 $2$3/g;
265 while (/^[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F].*/) {
267 $x =~ s/([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]).*/$1/;
270 s/[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F] *(.*)/$1/;
272 #print "bytes[$cnt] = $x\n";
277 # Pick up stragler bytes.
279 if (/^[0-9a-fA-F][0-9a-fA-F]/) {
281 $bytes[$cnt++] = $y * 256;
283 if ($multi == 0 && $cnt > 0) {