Revert "LATER... ei_kerberos_kdc_session_key ..."
[wireshark-sm.git] / epan / in_cksum.c
blobcbd328353a91d91b9f1e221a22020025b1738b14
1 /* in_cksum.c
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
14 #include "config.h"
16 #include <glib.h>
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.
46 int
47 in_cksum_ret_partial(const vec_t *vec, int veclen, uint16_t *partial)
49 register const uint16_t *w;
50 register int sum = 0;
51 register int mlen = 0;
52 int byte_swapped = 0;
54 union {
55 uint8_t c[2];
56 uint16_t s;
57 } s_util;
58 union {
59 uint16_t s[2];
60 uint32_t l;
61 } l_util;
63 for (; veclen != 0; vec++, veclen--) {
64 if (veclen == 1 && partial) {
65 REDUCE;
66 *partial = sum;
68 if (vec->len == 0)
69 continue;
70 w = (const uint16_t *)(const void *)vec->ptr;
71 if (mlen == -1) {
73 * The first byte of this chunk is the continuation
74 * of a word spanning between this chunk and the
75 * last chunk.
77 * s_util.c[0] is already saved when scanning previous
78 * chunk.
80 s_util.c[1] = *(const uint8_t *)w;
81 sum += s_util.s;
82 w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
83 mlen = vec->len - 1;
84 } else
85 mlen = vec->len;
87 * Force to even boundary.
89 if ((1 & (intptr_t)w) && (mlen > 0)) {
90 REDUCE;
91 sum <<= 8;
92 s_util.c[0] = *(const uint8_t *)w;
93 w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
94 mlen--;
95 byte_swapped = 1;
98 * Unroll the loop to make overhead from
99 * branches &c small.
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];
106 w += 16;
108 mlen += 32;
109 while ((mlen -= 8) >= 0) {
110 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
111 w += 4;
113 mlen += 8;
114 if (mlen == 0 && byte_swapped == 0)
115 continue;
116 REDUCE;
117 while ((mlen -= 2) >= 0) {
118 sum += *w++;
120 if (byte_swapped) {
121 REDUCE;
122 sum <<= 8;
123 byte_swapped = 0;
124 if (mlen == -1) {
125 s_util.c[1] = *(const uint8_t *)w;
126 sum += s_util.s;
127 mlen = 0;
128 } else
129 mlen = -1;
130 } else if (mlen == -1)
131 s_util.c[0] = *(const uint8_t *)w;
133 if (mlen == -1) {
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) */
137 s_util.c[1] = 0;
138 sum += s_util.s;
140 REDUCE;
141 return (~sum & 0xffff);
145 in_cksum(const vec_t *vec, int veclen)
147 return in_cksum_ret_partial(vec, veclen, NULL);
150 uint16_t
151 ip_checksum(const uint8_t *ptr, int len)
153 vec_t cksum_vec[1];
155 SET_CKSUM_VEC_PTR(cksum_vec[0], ptr, len);
156 return in_cksum_ret_partial(&cksum_vec[0], 1, NULL);
159 uint16_t
160 ip_checksum_tvb(tvbuff_t *tvb, int offset, int len)
162 vec_t cksum_vec[1];
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.
174 uint16_t
175 in_cksum_shouldbe(uint16_t sum, uint16_t computed_sum)
177 uint32_t shouldbe;
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
206 * field.
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.
213 shouldbe = sum;
214 shouldbe += g_ntohs(computed_sum);
215 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
216 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
217 return shouldbe;
221 * Editor modelines - https://www.wireshark.org/tools/modelines.html
223 * Local variables:
224 * c-basic-offset: 8
225 * tab-width: 8
226 * indent-tabs-mode: t
227 * End:
229 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
230 * :indentSize=8:tabSize=8:noTabs=false: