xref: /freebsd/contrib/tcpdump/in_cksum.c (revision 7afb8adff33d47f10a11368ff54bb2eec5b30165)
1 /* in_cksum.c
2  * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
3  * pointers/lengths giving the pieces to be checksummed.  Also using
4  * Tahoe/CGI version of ADDCARRY(x) macro instead of from portable version.
5  */
6 
7 /*
8  * Copyright (c) 1988, 1992, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
36  */
37 
38 #define NETDISSECT_REWORKED
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42 
43 #include <tcpdump-stdinc.h>
44 
45 #include "interface.h"
46 
47 /*
48  * Checksum routine for Internet Protocol family headers (Portable Version).
49  *
50  * This routine is very heavily used in the network
51  * code and should be modified for each CPU to be as fast as possible.
52  */
53 
54 #define ADDCARRY(x)  {if ((x) > 65535) (x) -= 65535;}
55 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
56 
57 uint16_t
58 in_cksum(const struct cksum_vec *vec, int veclen)
59 {
60 	register const uint16_t *w;
61 	register int sum = 0;
62 	register int mlen = 0;
63 	int byte_swapped = 0;
64 
65 	union {
66 		uint8_t		c[2];
67 		uint16_t	s;
68 	} s_util;
69 	union {
70 		uint16_t	s[2];
71 		uint32_t	l;
72 	} l_util;
73 
74 	for (; veclen != 0; vec++, veclen--) {
75 		if (vec->len == 0)
76 			continue;
77 		w = (const uint16_t *)(void *)vec->ptr;
78 		if (mlen == -1) {
79 			/*
80 			 * The first byte of this chunk is the continuation
81 			 * of a word spanning between this chunk and the
82 			 * last chunk.
83 			 *
84 			 * s_util.c[0] is already saved when scanning previous
85 			 * chunk.
86 			 */
87 			s_util.c[1] = *(const uint8_t *)w;
88 			sum += s_util.s;
89 			w = (const uint16_t *)(void *)((const uint8_t *)w + 1);
90 			mlen = vec->len - 1;
91 		} else
92 			mlen = vec->len;
93 		/*
94 		 * Force to even boundary.
95 		 */
96 		if ((1 & (unsigned long) w) && (mlen > 0)) {
97 			REDUCE;
98 			sum <<= 8;
99 			s_util.c[0] = *(const uint8_t *)w;
100 			w = (const uint16_t *)(void *)((const uint8_t *)w + 1);
101 			mlen--;
102 			byte_swapped = 1;
103 		}
104 		/*
105 		 * Unroll the loop to make overhead from
106 		 * branches &c small.
107 		 */
108 		while ((mlen -= 32) >= 0) {
109 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
110 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
111 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
112 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
113 			w += 16;
114 		}
115 		mlen += 32;
116 		while ((mlen -= 8) >= 0) {
117 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
118 			w += 4;
119 		}
120 		mlen += 8;
121 		if (mlen == 0 && byte_swapped == 0)
122 			continue;
123 		REDUCE;
124 		while ((mlen -= 2) >= 0) {
125 			sum += *w++;
126 		}
127 		if (byte_swapped) {
128 			REDUCE;
129 			sum <<= 8;
130 			byte_swapped = 0;
131 			if (mlen == -1) {
132 				s_util.c[1] = *(const uint8_t *)w;
133 				sum += s_util.s;
134 				mlen = 0;
135 			} else
136 				mlen = -1;
137 		} else if (mlen == -1)
138 			s_util.c[0] = *(const uint8_t *)w;
139 	}
140 	if (mlen == -1) {
141 		/* The last mbuf has odd # of bytes. Follow the
142 		   standard (the odd byte may be shifted left by 8 bits
143 		   or not as determined by endian-ness of the machine) */
144 		s_util.c[1] = 0;
145 		sum += s_util.s;
146 	}
147 	REDUCE;
148 	return (~sum & 0xffff);
149 }
150 
151 /*
152  * Given the host-byte-order value of the checksum field in a packet
153  * header, and the network-byte-order computed checksum of the data
154  * that the checksum covers (including the checksum itself), compute
155  * what the checksum field *should* have been.
156  */
157 uint16_t
158 in_cksum_shouldbe(uint16_t sum, uint16_t computed_sum)
159 {
160 	uint32_t shouldbe;
161 
162 	/*
163 	 * The value that should have gone into the checksum field
164 	 * is the negative of the value gotten by summing up everything
165 	 * *but* the checksum field.
166 	 *
167 	 * We can compute that by subtracting the value of the checksum
168 	 * field from the sum of all the data in the packet, and then
169 	 * computing the negative of that value.
170 	 *
171 	 * "sum" is the value of the checksum field, and "computed_sum"
172 	 * is the negative of the sum of all the data in the packets,
173 	 * so that's -(-computed_sum - sum), or (sum + computed_sum).
174 	 *
175 	 * All the arithmetic in question is one's complement, so the
176 	 * addition must include an end-around carry; we do this by
177 	 * doing the arithmetic in 32 bits (with no sign-extension),
178 	 * and then adding the upper 16 bits of the sum, which contain
179 	 * the carry, to the lower 16 bits of the sum, and then do it
180 	 * again in case *that* sum produced a carry.
181 	 *
182 	 * As RFC 1071 notes, the checksum can be computed without
183 	 * byte-swapping the 16-bit words; summing 16-bit words
184 	 * on a big-endian machine gives a big-endian checksum, which
185 	 * can be directly stuffed into the big-endian checksum fields
186 	 * in protocol headers, and summing words on a little-endian
187 	 * machine gives a little-endian checksum, which must be
188 	 * byte-swapped before being stuffed into a big-endian checksum
189 	 * field.
190 	 *
191 	 * "computed_sum" is a network-byte-order value, so we must put
192 	 * it in host byte order before subtracting it from the
193 	 * host-byte-order value from the header; the adjusted checksum
194 	 * will be in host byte order, which is what we'll return.
195 	 */
196 	shouldbe = sum;
197 	shouldbe += ntohs(computed_sum);
198 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
199 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
200 	return shouldbe;
201 }
202