summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/test/vfycksum.pl
blob: b3a20be0cf24f3093ca6fa4d2d8547c716f7cc6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

#
# validate the IPv4 header checksum.
# $bytes[] is an array of 16bit values, with $cnt elements in the array.
#
sub dump {
	print "\n";
	for ($i = 0; $i < $#bytes; $i++) {
		printf "%04x ", $bytes[$i];
	}
	print "\n";
}

sub dosum {
	local($seed) = $_[0];
	local($start) = $_[1];
	local($max) = $_[2];
	local($idx) = $start;
	local($lsum) = $seed;

	for ($idx = $start, $lsum = $seed; $idx < $max; $idx++) {
		$lsum += $bytes[$idx];
	}
	$lsum = ($lsum & 0xffff) + ($lsum >> 16);
	$lsum = ~$lsum & 0xffff;
	return $lsum;
}

sub ipv4check {
	local($base) = $_[0];
	$hl = $bytes[$base] / 256;
	return if (($hl >> 4) != 4);	# IPv4 ?
	$hl &= 0xf;
	$hl <<= 1;			# get the header length in 16bit words

	$hs = &dosum(0, $base, $base + $hl);
	$osum = $bytes[$base + 5];

	if ($hs != 0) {
		$bytes[$base + 5] = 0;
		$hs2 = &dosum(0, $base, $base + $hl);
		$bytes[$base + 5] = $osum;
		printf " IP: ($hl,%x) %x != %x", $hs, $osum, $hs2;
	} else {
		print " IP($base): ok ";
	}

	#
	# Recognise TCP & UDP and calculate checksums for each of these.
	#
	if (($bytes[$base + 4] & 0xff) == 6) {
		&tcpcheck($base);
	}

	if (($bytes[$base + 4] & 0xff) == 17) {
		&udpcheck($base);
	}

	if (($bytes[$base + 4] & 0xff) == 1) {
		&icmpcheck($base);
	}
	if ($base == 0) {
		print "\n";
	}
}

sub tcpcheck {
	local($base) = $_[0];
	local($hl) = $bytes[$base] / 256;
	return if (($hl >> 4) != 4);
	return if ($bytes[$base + 3] & 0x1fff);
	$hl &= 0xf;
	$hl <<= 1;

	local($hs2);
	local($hs) = 6;	# TCP
	local($len) = $bytes[$base + 1] - ($hl << 1);
	$hs += $len;
	$hs += $bytes[$base + 6];	# source address
	$hs += $bytes[$base + 7];
	$hs += $bytes[$base + 8];	# destination address
	$hs += $bytes[$base + 9];
	local($tcpsum) = $hs;

	local($thl) = $bytes[$base + $hl + 6] >> 8;
	$thl &= 0xf0;
	$thl >>= 2;

	$x = $bytes[$base + 1];
	$y = ($cnt - $base) * 2;
	$z = 0;
	if ($bytes[$base + 1] > ($cnt - $base) * 2) {
		print "[cnt=$cnt base=$base]";
		$x = $bytes[$base + 1];
		$y = ($cnt - $base) * 2;
		$z = 1;
	} elsif (($cnt - $base) * 2 < $hl + 20) {
		$x = ($cnt - $base) * 2;
		$y = $hl + 20;
		$z = 2;
	} elsif (($cnt - $base) * 2 < $hl + $thl) {
		$x = ($cnt - $base) * 2;
		$y = $hl + $thl;
		$z = 3;
	} elsif ($len < $thl) {
		$x = ($cnt - $base) * 2;
		$y = $len;
		$z = 4;
	}

	if ($z) {
		print " TCP: missing data($x $y $z) $hl";
#		&dump();
		return;
	}

	local($tcpat) = $base + $hl;
	$hs = &dosum($tcpsum, $tcpat, $cnt);
	if ($hs != 0) {
		local($osum) = $bytes[$tcpat + 8];
		$bytes[$base + $hl + 8] = 0;
		$hs2 = &dosum($tcpsum, $tcpat, $cnt);
		$bytes[$tcpat + 8] = $osum;
		printf " TCP: (%x) %x != %x", $hs, $osum, $hs2;
	} else {
		print " TCP: ok ($x $y)";
	}
}

sub udpcheck {
	local($base) = $_[0];
	local($hl) = $bytes[0] / 256;
	return if (($hl >> 4) != 4);
	return if ($bytes[3] & 0x1fff);
	$hl &= 0xf;
	$hl <<= 1;

	local($hs2);
	local($hs) = 17;	# UDP
	local($len) = $bytes[$base + 1] - ($hl << 1);
	$hs += $len;
	$hs += $bytes[$base + 6];	# source address
	$hs += $bytes[$base + 7];
	$hs += $bytes[$base + 8];	# destination address
	$hs += $bytes[$base + 9];
	local($udpsum) = $hs;

	if ($bytes[$base + 1] > ($cnt - $base) * 2) {
		print " UDP: missing data(1)";
		return;
	} elsif ($bytes[$base + 1] < ($hl << 1) + 8) {
		print " UDP: missing data(2)";
		return;
	} elsif (($cnt - $base) * 2 < ($hl << 1) + 8) {
		print " UDP: missing data(3)";
		return;
	}

	local($udpat) = $base + $hl;
	$hs = &dosum($udpsum, $udpat, $cnt);
	local($osum) = $bytes[$udpat + 3];

	#
	# It is valid for UDP packets to have a 0 checksum field.
	# If it is 0, then display what it would otherwise be.
	#
	if ($osum == 0) {
		printf " UDP: => %x", $hs;
	} elsif ($hs != 0) {
		$bytes[$udpat + 3] = 0;
		$hs2 = &dosum($udpsum, $udpat, $cnt);
		$bytes[$udpat + 3] = $osum;
		printf " UDP: (%x) %x != %x", $hs, $osum, $hs2;
	} else {
		print " UDP: ok";
	}
}

sub icmpcheck {
	local($base) = $_[0];
	local($hl) = $bytes[$base + 0] / 256;
	return if (($hl >> 4) != 4);
	return if ($bytes[3] & 0x1fff);
	$hl &= 0xf;
	$hl <<= 1;

	local($hs);
	local($hs2);

	local($len) = $bytes[$base + 1] - ($hl << 1);

	if ($bytes[$base + 1] > ($cnt - $base) * 2) {
		print " ICMP: missing data(1)";
		return;
	} elsif ($bytes[$base + 1] < ($hl << 1) + 8) {
		print " ICMP: missing data(2)";
		return;
	} elsif (($cnt - $base) * 2 < ($hl << 1) + 8) {
		print " ICMP: missing data(3)";
		return;
	}

	local($osum) = $bytes[$base + $hl + 1];
	$bytes[$base + $hl + 1] = 0;
	$hs2 = &dosum(0, $base + $hl, $cnt);
	$bytes[$base + $hl + 1] = $osum;

	if ($osum != $hs2) {
		printf " ICMP: (%x) %x != %x", $hs, $osum, $hs2;
	} else {
		print " ICMP: ok";
	}
	if ($base == 0) {
		$type = $bytes[$hl] >> 8;
		if ($type == 3 || $type == 4 || $type == 5 ||
		    $type == 11 || $type == 12) {
			&ipv4check($hl + 4);
		}
	}
}

while ($#ARGV >= 0) {
	open(I, "$ARGV[0]") || die $!;
	print "--- $ARGV[0] ---\n";
	$multi = 0;
	while (<I>) {
		chop;
		s/#.*//g;

		#
		# If the first non-comment, non-empty line of input starts
		# with a '[', then allow the input to be a multi-line hex
		# string, otherwise it has to be all on one line.
		#
		if (/^\[/) {
			$multi=1;
			s/^\[[^]]*\]//g;

		}
		s/^ *//g;
		if (length == 0) {
			next if ($cnt == 0);
			&ipv4check(0);
			$cnt = 0;
			$multi = 0;
			next;
		}

		#
		# look for 16 bits, represented with leading 0's as required,
		# in hex.
		#
		s/\t/ /g;
		while (/^[0-9a-fA-F][0-9a-fA-F] [0-9a-fA-F][0-9a-fA-F] .*/) {
			s/^([0-9a-fA-F][0-9a-fA-F]) ([0-9a-fA-F][0-9a-fA-F]) (.*)/$1$2 $3/;
		}
		while (/.* [0-9a-fA-F][0-9a-fA-F] [0-9a-fA-F][0-9a-fA-F] .*/) {
$b=$_;
			s/(.*?) ([0-9a-fA-F][0-9a-fA-F]) ([0-9a-fA-F][0-9a-fA-F]) (.*)/$1 $2$3 $4/g;
		}
		if (/.* [0-9a-fA-F][0-9a-fA-F] [0-9a-fA-F][0-9a-fA-F]/) {
$b=$_;
			s/(.*?) ([0-9a-fA-F][0-9a-fA-F]) ([0-9a-fA-F][0-9a-fA-F])/$1 $2$3/g;
		}
		while (/^[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F].*/) {
			$x = $_;
			$x =~ s/([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]).*/$1/;
			$x =~ s/ *//g;
			$y = hex $x;
			s/[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F] *(.*)/$1/;
			$bytes[$cnt] = $y;
#print "bytes[$cnt] = $x\n";
			$cnt++;
		}

		#
		# Pick up stragler bytes.
		#
		if (/^[0-9a-fA-F][0-9a-fA-F]/) {
			$y = hex $_;
			$bytes[$cnt++] = $y * 256;
		}
		if ($multi == 0 && $cnt > 0) {
			&ipv4check(0);
			$cnt = 0;
		}
	}

	if ($cnt > 0) {
		&ipv4check(0);
	}
	close(I);
	shift(@ARGV);
}
OpenPOWER on IntegriCloud