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.
6 * Copyright (c) 1988, 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
40 #include <epan/tvbuff.h>
41 #include <epan/in_cksum.h>
44 * Checksum routine for Internet Protocol family headers (Portable Version).
46 * This routine is very heavily used in the network
47 * code and should be modified for each CPU to be as fast as possible.
50 #define ADDCARRY(x) {if ((x) > 65535) (x) -= 65535;}
51 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
54 in_cksum(const vec_t *vec, int veclen)
56 register const guint16 *w;
58 register int mlen = 0;
70 for (; veclen != 0; vec++, veclen--) {
73 w = (const guint16 *)(const void *)vec->ptr;
76 * The first byte of this chunk is the continuation
77 * of a word spanning between this chunk and the
80 * s_util.c[0] is already saved when scanning previous
83 s_util.c[1] = *(const guint8 *)w;
85 w = (const guint16 *)(const void *)((const guint8 *)w + 1);
90 * Force to even boundary.
92 #if GLIB_CHECK_VERSION(2,18,0)
93 if ((1 & (gintptr)w) && (mlen > 0)) {
95 if ((1 & (unsigned long) w) && (mlen > 0)) {
96 #endif /* GLIB_CHECK_VERSION(2,18,0) */
99 s_util.c[0] = *(const guint8 *)w;
100 w = (const guint16 *)(const void *)((const guint8 *)w + 1);
105 * Unroll the loop to make overhead from
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];
116 while ((mlen -= 8) >= 0) {
117 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
121 if (mlen == 0 && byte_swapped == 0)
124 while ((mlen -= 2) >= 0) {
132 s_util.c[1] = *(const guint8 *)w;
137 } else if (mlen == -1)
138 s_util.c[0] = *(const guint8 *)w;
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) */
148 return (~sum & 0xffff);
152 ip_checksum(const guint8 *ptr, int len)
156 SET_CKSUM_VEC_PTR(cksum_vec[0], ptr, len);
157 return in_cksum(&cksum_vec[0], 1);
161 ip_checksum_tvb(tvbuff_t *tvb, int offset, int len)
165 SET_CKSUM_VEC_TVB(cksum_vec[0], tvb, offset, len);
166 return in_cksum(&cksum_vec[0], 1);
170 * Given the host-byte-order value of the checksum field in a packet
171 * header, and the network-byte-order computed checksum of the data
172 * that the checksum covers (including the checksum itself), compute
173 * what the checksum field *should* have been.
176 in_cksum_shouldbe(guint16 sum, guint16 computed_sum)
181 * The value that should have gone into the checksum field
182 * is the negative of the value gotten by summing up everything
183 * *but* the checksum field.
185 * We can compute that by subtracting the value of the checksum
186 * field from the sum of all the data in the packet, and then
187 * computing the negative of that value.
189 * "sum" is the value of the checksum field, and "computed_sum"
190 * is the negative of the sum of all the data in the packets,
191 * so that's -(-computed_sum - sum), or (sum + computed_sum).
193 * All the arithmetic in question is one's complement, so the
194 * addition must include an end-around carry; we do this by
195 * doing the arithmetic in 32 bits (with no sign-extension),
196 * and then adding the upper 16 bits of the sum, which contain
197 * the carry, to the lower 16 bits of the sum, and then do it
198 * again in case *that* sum produced a carry.
200 * As RFC 1071 notes, the checksum can be computed without
201 * byte-swapping the 16-bit words; summing 16-bit words
202 * on a big-endian machine gives a big-endian checksum, which
203 * can be directly stuffed into the big-endian checksum fields
204 * in protocol headers, and summing words on a little-endian
205 * machine gives a little-endian checksum, which must be
206 * byte-swapped before being stuffed into a big-endian checksum
209 * "computed_sum" is a network-byte-order value, so we must put
210 * it in host byte order before subtracting it from the
211 * host-byte-order value from the header; the adjusted checksum
212 * will be in host byte order, which is what we'll return.
215 shouldbe += g_ntohs(computed_sum);
216 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
217 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
222 * Editor modelines - http://www.wireshark.org/tools/modelines.html
227 * indent-tabs-mode: t
230 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
231 * :indentSize=8:tabSize=8:noTabs=false: