2 * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
3 * pointers/lengths giving the pieces to be checksummed.
5 * $Id: in_cksum.c,v 1.4 2001/06/09 01:02:29 guy Exp $
9 * Copyright (c) 1988, 1992, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
47 #ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
64 * Checksum routine for Internet Protocol family headers (Portable Version).
66 * This routine is very heavily used in the network
67 * code and should be modified for each CPU to be as fast as possible.
70 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
71 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
74 in_cksum(const vec_t *vec, int veclen)
76 register const guint16 *w;
78 register int mlen = 0;
90 for (; veclen != 0; vec++, veclen--) {
93 w = (const guint16 *)vec->ptr;
96 * The first byte of this chunk is the continuation
97 * of a word spanning between this chunk and the
100 * s_util.c[0] is already saved when scanning previous
103 s_util.c[1] = *(const guint8 *)w;
105 w = (const guint16 *)((const guint8 *)w + 1);
110 * Force to even boundary.
112 if ((1 & (unsigned long) w) && (mlen > 0)) {
115 s_util.c[0] = *(const guint8 *)w;
116 w = (const guint16 *)((const guint8 *)w + 1);
121 * Unroll the loop to make overhead from
124 while ((mlen -= 32) >= 0) {
125 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
126 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
127 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
128 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
132 while ((mlen -= 8) >= 0) {
133 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
137 if (mlen == 0 && byte_swapped == 0)
140 while ((mlen -= 2) >= 0) {
148 s_util.c[1] = *(const guint8 *)w;
153 } else if (mlen == -1)
154 s_util.c[0] = *(const guint8 *)w;
157 /* The last mbuf has odd # of bytes. Follow the
158 standard (the odd byte may be shifted left by 8 bits
159 or not as determined by endian-ness of the machine) */
164 return (~sum & 0xffff);
168 * Given the host-byte-order value of the checksum field in a packet
169 * header, and the one's complement negation of the host-byte-order
170 * checksum of the packet, compute what the checksum field *should*
174 in_cksum_shouldbe(guint16 sum, guint16 computed_sum)
179 * The value that should have gone into the checksum field
180 * is the negative of the value gotten by summing up everything
181 * *but* the checksum field.
183 * We can compute that by subtracting the value of the checksum
184 * field from the sum of all the data in the packet, and then
185 * computing the negative of that value.
187 * "sum" is the value of the checksum field, and "computed_sum"
188 * is the negative of the sum of all the data in the packets,
189 * so that's -(-computed_sum - sum), or (sum + computed_sum).
191 * All the arithmetic in question is one's complement, so the
192 * addition must include an end-around carry; we do this by
193 * doing the arithmetic in 32 bits (with no sign-extension),
194 * and then adding the upper 16 bits of the sum, which contain
195 * the carry, to the lower 16 bits of the sum, and then do it
196 * again in case *that* sum produced a carry.
198 * As RFC 1071 notes, the checksum can be computed without
199 * byte-swapping the 16-bit words; summing 16-bit words
200 * on a big-endian machine gives a big-endian checksum, which
201 * can be directly stuffed into the big-endian checksum fields
202 * in protocol headers, and summing words on a little-endian
203 * machine gives a little-endian checksum, which must be
204 * byte-swapped before being stuffed into a big-endian checksum
207 * "computed_sum" is a host-byte-order value, so we must put
208 * it in network byte order before subtracting it from the
209 * network-byte-order value from the header; the adjusted
210 * checksum will be in network byte order, which is what
214 shouldbe += htons(computed_sum);
215 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
216 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);