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 * SPDX-License-Identifier: BSD-3-Clause
11 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
18 #include <epan/tvbuff.h>
19 #include <epan/in_cksum.h>
22 * Checksum routine for Internet Protocol family headers (Portable Version).
24 * This routine is very heavily used in the network
25 * code and should be modified for each CPU to be as fast as possible.
28 #define ADDCARRY(x) {if ((x) > 65535) (x) -= 65535;}
29 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
32 in_cksum(const vec_t *vec, int veclen)
34 register const guint16 *w;
36 register int mlen = 0;
48 for (; veclen != 0; vec++, veclen--) {
51 w = (const guint16 *)(const void *)vec->ptr;
54 * The first byte of this chunk is the continuation
55 * of a word spanning between this chunk and the
58 * s_util.c[0] is already saved when scanning previous
61 s_util.c[1] = *(const guint8 *)w;
63 w = (const guint16 *)(const void *)((const guint8 *)w + 1);
68 * Force to even boundary.
70 if ((1 & (gintptr)w) && (mlen > 0)) {
73 s_util.c[0] = *(const guint8 *)w;
74 w = (const guint16 *)(const void *)((const guint8 *)w + 1);
79 * Unroll the loop to make overhead from
82 while ((mlen -= 32) >= 0) {
83 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
84 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
85 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
86 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
90 while ((mlen -= 8) >= 0) {
91 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
95 if (mlen == 0 && byte_swapped == 0)
98 while ((mlen -= 2) >= 0) {
106 s_util.c[1] = *(const guint8 *)w;
111 } else if (mlen == -1)
112 s_util.c[0] = *(const guint8 *)w;
115 /* The last mbuf has odd # of bytes. Follow the
116 standard (the odd byte may be shifted left by 8 bits
117 or not as determined by endian-ness of the machine) */
122 return (~sum & 0xffff);
126 ip_checksum(const guint8 *ptr, int len)
130 SET_CKSUM_VEC_PTR(cksum_vec[0], ptr, len);
131 return in_cksum(&cksum_vec[0], 1);
135 ip_checksum_tvb(tvbuff_t *tvb, int offset, int len)
139 SET_CKSUM_VEC_TVB(cksum_vec[0], tvb, offset, len);
140 return in_cksum(&cksum_vec[0], 1);
144 * Given the host-byte-order value of the checksum field in a packet
145 * header, and the network-byte-order computed checksum of the data
146 * that the checksum covers (including the checksum itself), compute
147 * what the checksum field *should* have been.
150 in_cksum_shouldbe(guint16 sum, guint16 computed_sum)
155 * The value that should have gone into the checksum field
156 * is the negative of the value gotten by summing up everything
157 * *but* the checksum field.
159 * We can compute that by subtracting the value of the checksum
160 * field from the sum of all the data in the packet, and then
161 * computing the negative of that value.
163 * "sum" is the value of the checksum field, and "computed_sum"
164 * is the negative of the sum of all the data in the packets,
165 * so that's -(-computed_sum - sum), or (sum + computed_sum).
167 * All the arithmetic in question is one's complement, so the
168 * addition must include an end-around carry; we do this by
169 * doing the arithmetic in 32 bits (with no sign-extension),
170 * and then adding the upper 16 bits of the sum, which contain
171 * the carry, to the lower 16 bits of the sum, and then do it
172 * again in case *that* sum produced a carry.
174 * As RFC 1071 notes, the checksum can be computed without
175 * byte-swapping the 16-bit words; summing 16-bit words
176 * on a big-endian machine gives a big-endian checksum, which
177 * can be directly stuffed into the big-endian checksum fields
178 * in protocol headers, and summing words on a little-endian
179 * machine gives a little-endian checksum, which must be
180 * byte-swapped before being stuffed into a big-endian checksum
183 * "computed_sum" is a network-byte-order value, so we must put
184 * it in host byte order before subtracting it from the
185 * host-byte-order value from the header; the adjusted checksum
186 * will be in host byte order, which is what we'll return.
189 shouldbe += g_ntohs(computed_sum);
190 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
191 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
196 * Editor modelines - http://www.wireshark.org/tools/modelines.html
201 * indent-tabs-mode: t
204 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
205 * :indentSize=8:tabSize=8:noTabs=false: