xref: /netbsd-src/external/bsd/tcpdump/dist/in_cksum.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
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 #include <sys/cdefs.h>
39 #ifndef lint
40 __RCSID("$NetBSD: in_cksum.c,v 1.2 2017/01/24 23:29:13 christos Exp $");
41 #endif
42 
43 #ifdef HAVE_CONFIG_H
44 # include "config.h"
45 #endif
46 
47 #include <netdissect-stdinc.h>
48 
49 #include "netdissect.h"
50 
51 /*
52  * Checksum routine for Internet Protocol family headers (Portable Version).
53  *
54  * This routine is very heavily used in the network
55  * code and should be modified for each CPU to be as fast as possible.
56  */
57 
58 #define ADDCARRY(x)  {if ((x) > 65535) (x) -= 65535;}
59 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
60 
61 uint16_t
62 in_cksum(const struct cksum_vec *vec, int veclen)
63 {
64 	register const uint16_t *w;
65 	register int sum = 0;
66 	register int mlen = 0;
67 	int byte_swapped = 0;
68 
69 	union {
70 		uint8_t		c[2];
71 		uint16_t	s;
72 	} s_util;
73 	union {
74 		uint16_t	s[2];
75 		uint32_t	l;
76 	} l_util;
77 
78 	for (; veclen != 0; vec++, veclen--) {
79 		if (vec->len == 0)
80 			continue;
81 		w = (const uint16_t *)(const void *)vec->ptr;
82 		if (mlen == -1) {
83 			/*
84 			 * The first byte of this chunk is the continuation
85 			 * of a word spanning between this chunk and the
86 			 * last chunk.
87 			 *
88 			 * s_util.c[0] is already saved when scanning previous
89 			 * chunk.
90 			 */
91 			s_util.c[1] = *(const uint8_t *)w;
92 			sum += s_util.s;
93 			w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
94 			mlen = vec->len - 1;
95 		} else
96 			mlen = vec->len;
97 		/*
98 		 * Force to even boundary.
99 		 */
100 		if ((1 & (uintptr_t) w) && (mlen > 0)) {
101 			REDUCE;
102 			sum <<= 8;
103 			s_util.c[0] = *(const uint8_t *)w;
104 			w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
105 			mlen--;
106 			byte_swapped = 1;
107 		}
108 		/*
109 		 * Unroll the loop to make overhead from
110 		 * branches &c small.
111 		 */
112 		while ((mlen -= 32) >= 0) {
113 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
114 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
115 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
116 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
117 			w += 16;
118 		}
119 		mlen += 32;
120 		while ((mlen -= 8) >= 0) {
121 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
122 			w += 4;
123 		}
124 		mlen += 8;
125 		if (mlen == 0 && byte_swapped == 0)
126 			continue;
127 		REDUCE;
128 		while ((mlen -= 2) >= 0) {
129 			sum += *w++;
130 		}
131 		if (byte_swapped) {
132 			REDUCE;
133 			sum <<= 8;
134 			byte_swapped = 0;
135 			if (mlen == -1) {
136 				s_util.c[1] = *(const uint8_t *)w;
137 				sum += s_util.s;
138 				mlen = 0;
139 			} else
140 				mlen = -1;
141 		} else if (mlen == -1)
142 			s_util.c[0] = *(const uint8_t *)w;
143 	}
144 	if (mlen == -1) {
145 		/* The last mbuf has odd # of bytes. Follow the
146 		   standard (the odd byte may be shifted left by 8 bits
147 		   or not as determined by endian-ness of the machine) */
148 		s_util.c[1] = 0;
149 		sum += s_util.s;
150 	}
151 	REDUCE;
152 	return (~sum & 0xffff);
153 }
154 
155 /*
156  * Given the host-byte-order value of the checksum field in a packet
157  * header, and the network-byte-order computed checksum of the data
158  * that the checksum covers (including the checksum itself), compute
159  * what the checksum field *should* have been.
160  */
161 uint16_t
162 in_cksum_shouldbe(uint16_t sum, uint16_t computed_sum)
163 {
164 	uint32_t shouldbe;
165 
166 	/*
167 	 * The value that should have gone into the checksum field
168 	 * is the negative of the value gotten by summing up everything
169 	 * *but* the checksum field.
170 	 *
171 	 * We can compute that by subtracting the value of the checksum
172 	 * field from the sum of all the data in the packet, and then
173 	 * computing the negative of that value.
174 	 *
175 	 * "sum" is the value of the checksum field, and "computed_sum"
176 	 * is the negative of the sum of all the data in the packets,
177 	 * so that's -(-computed_sum - sum), or (sum + computed_sum).
178 	 *
179 	 * All the arithmetic in question is one's complement, so the
180 	 * addition must include an end-around carry; we do this by
181 	 * doing the arithmetic in 32 bits (with no sign-extension),
182 	 * and then adding the upper 16 bits of the sum, which contain
183 	 * the carry, to the lower 16 bits of the sum, and then do it
184 	 * again in case *that* sum produced a carry.
185 	 *
186 	 * As RFC 1071 notes, the checksum can be computed without
187 	 * byte-swapping the 16-bit words; summing 16-bit words
188 	 * on a big-endian machine gives a big-endian checksum, which
189 	 * can be directly stuffed into the big-endian checksum fields
190 	 * in protocol headers, and summing words on a little-endian
191 	 * machine gives a little-endian checksum, which must be
192 	 * byte-swapped before being stuffed into a big-endian checksum
193 	 * field.
194 	 *
195 	 * "computed_sum" is a network-byte-order value, so we must put
196 	 * it in host byte order before subtracting it from the
197 	 * host-byte-order value from the header; the adjusted checksum
198 	 * will be in host byte order, which is what we'll return.
199 	 */
200 	shouldbe = sum;
201 	shouldbe += ntohs(computed_sum);
202 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
203 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
204 	return shouldbe;
205 }
206