1*05b00f60SXin Li /* in_cksum.c
2*05b00f60SXin Li * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
3*05b00f60SXin Li * pointers/lengths giving the pieces to be checksummed. Also using
4*05b00f60SXin Li * Tahoe/CGI version of ADDCARRY(x) macro instead of from portable version.
5*05b00f60SXin Li */
6*05b00f60SXin Li
7*05b00f60SXin Li /*
8*05b00f60SXin Li * Copyright (c) 1988, 1992, 1993
9*05b00f60SXin Li * The Regents of the University of California. All rights reserved.
10*05b00f60SXin Li *
11*05b00f60SXin Li * Redistribution and use in source and binary forms, with or without
12*05b00f60SXin Li * modification, are permitted provided that the following conditions
13*05b00f60SXin Li * are met:
14*05b00f60SXin Li * 1. Redistributions of source code must retain the above copyright
15*05b00f60SXin Li * notice, this list of conditions and the following disclaimer.
16*05b00f60SXin Li * 2. Redistributions in binary form must reproduce the above copyright
17*05b00f60SXin Li * notice, this list of conditions and the following disclaimer in the
18*05b00f60SXin Li * documentation and/or other materials provided with the distribution.
19*05b00f60SXin Li * 3. Neither the name of the University nor the names of its contributors
20*05b00f60SXin Li * may be used to endorse or promote products derived from this software
21*05b00f60SXin Li * without specific prior written permission.
22*05b00f60SXin Li *
23*05b00f60SXin Li * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*05b00f60SXin Li * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*05b00f60SXin Li * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*05b00f60SXin Li * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*05b00f60SXin Li * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*05b00f60SXin Li * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*05b00f60SXin Li * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*05b00f60SXin Li * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*05b00f60SXin Li * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*05b00f60SXin Li * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*05b00f60SXin Li * SUCH DAMAGE.
34*05b00f60SXin Li *
35*05b00f60SXin Li * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
36*05b00f60SXin Li */
37*05b00f60SXin Li
38*05b00f60SXin Li #ifdef HAVE_CONFIG_H
39*05b00f60SXin Li # include <config.h>
40*05b00f60SXin Li #endif
41*05b00f60SXin Li
42*05b00f60SXin Li #include "netdissect-stdinc.h"
43*05b00f60SXin Li
44*05b00f60SXin Li #include "netdissect.h"
45*05b00f60SXin Li
46*05b00f60SXin Li /*
47*05b00f60SXin Li * Checksum routine for Internet Protocol family headers (Portable Version).
48*05b00f60SXin Li *
49*05b00f60SXin Li * This routine is very heavily used in the network
50*05b00f60SXin Li * code and should be modified for each CPU to be as fast as possible.
51*05b00f60SXin Li */
52*05b00f60SXin Li
53*05b00f60SXin Li #define ADDCARRY(x) {if ((x) > 65535) (x) -= 65535;}
54*05b00f60SXin Li #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
55*05b00f60SXin Li
56*05b00f60SXin Li uint16_t
in_cksum(const struct cksum_vec * vec,int veclen)57*05b00f60SXin Li in_cksum(const struct cksum_vec *vec, int veclen)
58*05b00f60SXin Li {
59*05b00f60SXin Li const uint16_t *w;
60*05b00f60SXin Li int sum = 0;
61*05b00f60SXin Li int mlen = 0;
62*05b00f60SXin Li int byte_swapped = 0;
63*05b00f60SXin Li
64*05b00f60SXin Li union {
65*05b00f60SXin Li uint8_t c[2];
66*05b00f60SXin Li uint16_t s;
67*05b00f60SXin Li } s_util;
68*05b00f60SXin Li union {
69*05b00f60SXin Li uint16_t s[2];
70*05b00f60SXin Li uint32_t l;
71*05b00f60SXin Li } l_util;
72*05b00f60SXin Li
73*05b00f60SXin Li for (; veclen != 0; vec++, veclen--) {
74*05b00f60SXin Li if (vec->len == 0)
75*05b00f60SXin Li continue;
76*05b00f60SXin Li w = (const uint16_t *)(const void *)vec->ptr;
77*05b00f60SXin Li if (mlen == -1) {
78*05b00f60SXin Li /*
79*05b00f60SXin Li * The first byte of this chunk is the continuation
80*05b00f60SXin Li * of a word spanning between this chunk and the
81*05b00f60SXin Li * last chunk.
82*05b00f60SXin Li *
83*05b00f60SXin Li * s_util.c[0] is already saved when scanning previous
84*05b00f60SXin Li * chunk.
85*05b00f60SXin Li */
86*05b00f60SXin Li s_util.c[1] = *(const uint8_t *)w;
87*05b00f60SXin Li sum += s_util.s;
88*05b00f60SXin Li w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
89*05b00f60SXin Li mlen = vec->len - 1;
90*05b00f60SXin Li } else
91*05b00f60SXin Li mlen = vec->len;
92*05b00f60SXin Li /*
93*05b00f60SXin Li * Force to even boundary.
94*05b00f60SXin Li */
95*05b00f60SXin Li if ((1 & (uintptr_t) w) && (mlen > 0)) {
96*05b00f60SXin Li REDUCE;
97*05b00f60SXin Li sum <<= 8;
98*05b00f60SXin Li s_util.c[0] = *(const uint8_t *)w;
99*05b00f60SXin Li w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
100*05b00f60SXin Li mlen--;
101*05b00f60SXin Li byte_swapped = 1;
102*05b00f60SXin Li }
103*05b00f60SXin Li /*
104*05b00f60SXin Li * Unroll the loop to make overhead from
105*05b00f60SXin Li * branches &c small.
106*05b00f60SXin Li */
107*05b00f60SXin Li while ((mlen -= 32) >= 0) {
108*05b00f60SXin Li sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
109*05b00f60SXin Li sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
110*05b00f60SXin Li sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
111*05b00f60SXin Li sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
112*05b00f60SXin Li w += 16;
113*05b00f60SXin Li }
114*05b00f60SXin Li mlen += 32;
115*05b00f60SXin Li while ((mlen -= 8) >= 0) {
116*05b00f60SXin Li sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
117*05b00f60SXin Li w += 4;
118*05b00f60SXin Li }
119*05b00f60SXin Li mlen += 8;
120*05b00f60SXin Li if (mlen == 0 && byte_swapped == 0)
121*05b00f60SXin Li continue;
122*05b00f60SXin Li REDUCE;
123*05b00f60SXin Li while ((mlen -= 2) >= 0) {
124*05b00f60SXin Li sum += *w++;
125*05b00f60SXin Li }
126*05b00f60SXin Li if (byte_swapped) {
127*05b00f60SXin Li REDUCE;
128*05b00f60SXin Li sum <<= 8;
129*05b00f60SXin Li byte_swapped = 0;
130*05b00f60SXin Li if (mlen == -1) {
131*05b00f60SXin Li s_util.c[1] = *(const uint8_t *)w;
132*05b00f60SXin Li sum += s_util.s;
133*05b00f60SXin Li mlen = 0;
134*05b00f60SXin Li } else
135*05b00f60SXin Li mlen = -1;
136*05b00f60SXin Li } else if (mlen == -1)
137*05b00f60SXin Li s_util.c[0] = *(const uint8_t *)w;
138*05b00f60SXin Li }
139*05b00f60SXin Li if (mlen == -1) {
140*05b00f60SXin Li /* The last mbuf has odd # of bytes. Follow the
141*05b00f60SXin Li standard (the odd byte may be shifted left by 8 bits
142*05b00f60SXin Li or not as determined by endian-ness of the machine) */
143*05b00f60SXin Li s_util.c[1] = 0;
144*05b00f60SXin Li sum += s_util.s;
145*05b00f60SXin Li }
146*05b00f60SXin Li REDUCE;
147*05b00f60SXin Li return (~sum & 0xffff);
148*05b00f60SXin Li }
149*05b00f60SXin Li
150*05b00f60SXin Li /*
151*05b00f60SXin Li * Given the host-byte-order value of the checksum field in a packet
152*05b00f60SXin Li * header, and the network-byte-order computed checksum of the data
153*05b00f60SXin Li * that the checksum covers (including the checksum itself), compute
154*05b00f60SXin Li * what the checksum field *should* have been.
155*05b00f60SXin Li */
156*05b00f60SXin Li uint16_t
in_cksum_shouldbe(uint16_t sum,uint16_t computed_sum)157*05b00f60SXin Li in_cksum_shouldbe(uint16_t sum, uint16_t computed_sum)
158*05b00f60SXin Li {
159*05b00f60SXin Li uint32_t shouldbe;
160*05b00f60SXin Li
161*05b00f60SXin Li /*
162*05b00f60SXin Li * The value that should have gone into the checksum field
163*05b00f60SXin Li * is the negative of the value gotten by summing up everything
164*05b00f60SXin Li * *but* the checksum field.
165*05b00f60SXin Li *
166*05b00f60SXin Li * We can compute that by subtracting the value of the checksum
167*05b00f60SXin Li * field from the sum of all the data in the packet, and then
168*05b00f60SXin Li * computing the negative of that value.
169*05b00f60SXin Li *
170*05b00f60SXin Li * "sum" is the value of the checksum field, and "computed_sum"
171*05b00f60SXin Li * is the negative of the sum of all the data in the packets,
172*05b00f60SXin Li * so that's -(-computed_sum - sum), or (sum + computed_sum).
173*05b00f60SXin Li *
174*05b00f60SXin Li * All the arithmetic in question is one's complement, so the
175*05b00f60SXin Li * addition must include an end-around carry; we do this by
176*05b00f60SXin Li * doing the arithmetic in 32 bits (with no sign-extension),
177*05b00f60SXin Li * and then adding the upper 16 bits of the sum, which contain
178*05b00f60SXin Li * the carry, to the lower 16 bits of the sum, and then do it
179*05b00f60SXin Li * again in case *that* sum produced a carry.
180*05b00f60SXin Li *
181*05b00f60SXin Li * As RFC 1071 notes, the checksum can be computed without
182*05b00f60SXin Li * byte-swapping the 16-bit words; summing 16-bit words
183*05b00f60SXin Li * on a big-endian machine gives a big-endian checksum, which
184*05b00f60SXin Li * can be directly stuffed into the big-endian checksum fields
185*05b00f60SXin Li * in protocol headers, and summing words on a little-endian
186*05b00f60SXin Li * machine gives a little-endian checksum, which must be
187*05b00f60SXin Li * byte-swapped before being stuffed into a big-endian checksum
188*05b00f60SXin Li * field.
189*05b00f60SXin Li *
190*05b00f60SXin Li * "computed_sum" is a network-byte-order value, so we must put
191*05b00f60SXin Li * it in host byte order before subtracting it from the
192*05b00f60SXin Li * host-byte-order value from the header; the adjusted checksum
193*05b00f60SXin Li * will be in host byte order, which is what we'll return.
194*05b00f60SXin Li */
195*05b00f60SXin Li shouldbe = sum;
196*05b00f60SXin Li shouldbe += ntohs(computed_sum);
197*05b00f60SXin Li shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
198*05b00f60SXin Li shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
199*05b00f60SXin Li return (uint16_t)shouldbe;
200*05b00f60SXin Li }
201