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