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