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 * Linux and Windows, at least, when performing Local Checksum Offload
33 * store the one's complement sum (not inverted to its bitwise complement)
34 * of the pseudo header in the checksum field (instead of intializing
35 * to zero), allowing the device driver to calculate the real checksum
36 * later without needing knowledge of the pseudoheader itself.
37 * (This is presumably why GSO requires equal length buffers - so that the
38 * pseudo header contribution to the checksum, which includes the payload
39 * length, is the same.)
41 * We can output this partial checksum as an intermediate result,
42 * assuming that the pseudo header is all but the last chunk in the vector.
43 * Note that unlike the final output it is not inverted, and that it
44 * (like the final computed checksum) is is network byte order.
47 in_cksum_ret_partial(const vec_t
*vec
, int veclen
, uint16_t *partial
)
49 register const uint16_t *w
;
51 register int mlen
= 0;
63 for (; veclen
!= 0; vec
++, veclen
--) {
64 if (veclen
== 1 && partial
) {
70 w
= (const uint16_t *)(const void *)vec
->ptr
;
73 * The first byte of this chunk is the continuation
74 * of a word spanning between this chunk and the
77 * s_util.c[0] is already saved when scanning previous
80 s_util
.c
[1] = *(const uint8_t *)w
;
82 w
= (const uint16_t *)(const void *)((const uint8_t *)w
+ 1);
87 * Force to even boundary.
89 if ((1 & (intptr_t)w
) && (mlen
> 0)) {
92 s_util
.c
[0] = *(const uint8_t *)w
;
93 w
= (const uint16_t *)(const void *)((const uint8_t *)w
+ 1);
98 * Unroll the loop to make overhead from
101 while ((mlen
-= 32) >= 0) {
102 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
103 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
104 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
105 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
109 while ((mlen
-= 8) >= 0) {
110 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
114 if (mlen
== 0 && byte_swapped
== 0)
117 while ((mlen
-= 2) >= 0) {
125 s_util
.c
[1] = *(const uint8_t *)w
;
130 } else if (mlen
== -1)
131 s_util
.c
[0] = *(const uint8_t *)w
;
134 /* The last mbuf has odd # of bytes. Follow the
135 standard (the odd byte may be shifted left by 8 bits
136 or not as determined by endian-ness of the machine) */
141 return (~sum
& 0xffff);
145 in_cksum(const vec_t
*vec
, int veclen
)
147 return in_cksum_ret_partial(vec
, veclen
, NULL
);
151 ip_checksum(const uint8_t *ptr
, int len
)
155 SET_CKSUM_VEC_PTR(cksum_vec
[0], ptr
, len
);
156 return in_cksum_ret_partial(&cksum_vec
[0], 1, NULL
);
160 ip_checksum_tvb(tvbuff_t
*tvb
, int offset
, int len
)
164 SET_CKSUM_VEC_TVB(cksum_vec
[0], tvb
, offset
, len
);
165 return in_cksum_ret_partial(&cksum_vec
[0], 1, NULL
);
169 * Given the host-byte-order value of the checksum field in a packet
170 * header, and the network-byte-order computed checksum of the data
171 * that the checksum covers (including the checksum itself), compute
172 * what the checksum field *should* have been.
175 in_cksum_shouldbe(uint16_t sum
, uint16_t computed_sum
)
180 * The value that should have gone into the checksum field
181 * is the negative of the value gotten by summing up everything
182 * *but* the checksum field.
184 * We can compute that by subtracting the value of the checksum
185 * field from the sum of all the data in the packet, and then
186 * computing the negative of that value.
188 * "sum" is the value of the checksum field, and "computed_sum"
189 * is the negative of the sum of all the data in the packets,
190 * so that's -(-computed_sum - sum), or (sum + computed_sum).
192 * All the arithmetic in question is one's complement, so the
193 * addition must include an end-around carry; we do this by
194 * doing the arithmetic in 32 bits (with no sign-extension),
195 * and then adding the upper 16 bits of the sum, which contain
196 * the carry, to the lower 16 bits of the sum, and then do it
197 * again in case *that* sum produced a carry.
199 * As RFC 1071 notes, the checksum can be computed without
200 * byte-swapping the 16-bit words; summing 16-bit words
201 * on a big-endian machine gives a big-endian checksum, which
202 * can be directly stuffed into the big-endian checksum fields
203 * in protocol headers, and summing words on a little-endian
204 * machine gives a little-endian checksum, which must be
205 * byte-swapped before being stuffed into a big-endian checksum
208 * "computed_sum" is a network-byte-order value, so we must put
209 * it in host byte order before subtracting it from the
210 * host-byte-order value from the header; the adjusted checksum
211 * will be in host byte order, which is what we'll return.
214 shouldbe
+= g_ntohs(computed_sum
);
215 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
216 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
221 * Editor modelines - https://www.wireshark.org/tools/modelines.html
226 * indent-tabs-mode: t
229 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
230 * :indentSize=8:tabSize=8:noTabs=false: