1 /*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1988, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1996
7 * Matt Thomas <matt@3am-software.com>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
38 */
39
40 #include <sys/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/mbuf.h>
43 #include <sys/systm.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <machine/in_cksum.h>
48
49 /*
50 * These implementations may be overridden on a per-platform basis. On
51 * platforms with a direct map, the implementation of in_cksum() must handle
52 * unmapped mbufs.
53 */
54 #ifndef HAVE_MD_IN_CKSUM
55
56 /*
57 * Checksum routine for Internet Protocol family headers
58 * (Portable Alpha version).
59 *
60 * This routine is very heavily used in the network
61 * code and should be modified for each CPU to be as fast as possible.
62 */
63
64 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
65 #define REDUCE32 \
66 { \
67 q_util.q = sum; \
68 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
69 }
70 #define REDUCE16 \
71 { \
72 q_util.q = sum; \
73 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
74 sum = l_util.s[0] + l_util.s[1]; \
75 ADDCARRY(sum); \
76 }
77
78 static const u_int32_t in_masks[] = {
79 #if _BYTE_ORDER == _LITTLE_ENDIAN
80 /*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/
81 0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
82 0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
83 0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
84 0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
85 #else
86 /*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/
87 0x00000000, 0xFF000000, 0xFFFF0000, 0xFFFFFF00, /* offset 0 */
88 0x00000000, 0x00FF0000, 0x00FFFF00, 0x00FFFFFF, /* offset 1 */
89 0x00000000, 0x0000FF00, 0x0000FFFF, 0x0000FFFF, /* offset 2 */
90 0x00000000, 0x000000FF, 0x000000FF, 0x000000FF, /* offset 3 */
91 #endif
92 };
93
94 union l_util {
95 u_int16_t s[2];
96 u_int32_t l;
97 };
98 union q_util {
99 u_int16_t s[4];
100 u_int32_t l[2];
101 u_int64_t q;
102 };
103
104 static u_int64_t
in_cksumdata(const void * buf,int len)105 in_cksumdata(const void *buf, int len)
106 {
107 const u_int32_t *lw = (const u_int32_t *) buf;
108 u_int64_t sum = 0;
109 u_int64_t prefilled;
110 int offset;
111 union q_util q_util;
112
113 if ((3 & (long) lw) == 0 && len == 20) {
114 sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
115 REDUCE32;
116 return sum;
117 }
118
119 if ((offset = 3 & (long) lw) != 0) {
120 const u_int32_t *masks = in_masks + (offset << 2);
121 lw = (u_int32_t *) (((long) lw) - offset);
122 sum = *lw++ & masks[len >= 3 ? 3 : len];
123 len -= 4 - offset;
124 if (len <= 0) {
125 REDUCE32;
126 return sum;
127 }
128 }
129 #if 0
130 /*
131 * Force to cache line boundary.
132 */
133 offset = 32 - (0x1f & (long) lw);
134 if (offset < 32 && len > offset) {
135 len -= offset;
136 if (4 & offset) {
137 sum += (u_int64_t) lw[0];
138 lw += 1;
139 }
140 if (8 & offset) {
141 sum += (u_int64_t) lw[0] + lw[1];
142 lw += 2;
143 }
144 if (16 & offset) {
145 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
146 lw += 4;
147 }
148 }
149 #endif
150 /*
151 * access prefilling to start load of next cache line.
152 * then add current cache line
153 * save result of prefilling for loop iteration.
154 */
155 prefilled = lw[0];
156 while ((len -= 32) >= 4) {
157 u_int64_t prefilling = lw[8];
158 sum += prefilled + lw[1] + lw[2] + lw[3]
159 + lw[4] + lw[5] + lw[6] + lw[7];
160 lw += 8;
161 prefilled = prefilling;
162 }
163 if (len >= 0) {
164 sum += prefilled + lw[1] + lw[2] + lw[3]
165 + lw[4] + lw[5] + lw[6] + lw[7];
166 lw += 8;
167 } else {
168 len += 32;
169 }
170 while ((len -= 16) >= 0) {
171 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
172 lw += 4;
173 }
174 len += 16;
175 while ((len -= 4) >= 0) {
176 sum += (u_int64_t) *lw++;
177 }
178 len += 4;
179 if (len > 0)
180 sum += (u_int64_t) (in_masks[len] & *lw);
181 REDUCE32;
182 return sum;
183 }
184
185 u_short
in_addword(u_short a,u_short b)186 in_addword(u_short a, u_short b)
187 {
188 u_int64_t sum = a + b;
189
190 ADDCARRY(sum);
191 return (sum);
192 }
193
194 u_short
in_pseudo(u_int32_t a,u_int32_t b,u_int32_t c)195 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
196 {
197 u_int64_t sum;
198 union q_util q_util;
199 union l_util l_util;
200
201 sum = (u_int64_t) a + b + c;
202 REDUCE16;
203 return (sum);
204 }
205
206 struct cksum_skip_partial_args {
207 uint64_t csum;
208 int clen;
209 };
210
211 static int
in_cksum_skip_partial(void * arg,void * data,u_int len)212 in_cksum_skip_partial(void *arg, void *data, u_int len)
213 {
214 struct cksum_skip_partial_args *a;
215
216 a = arg;
217 if (((uintptr_t)data ^ a->clen) & 1)
218 a->csum += in_cksumdata(data, len) << 8;
219 else
220 a->csum += in_cksumdata(data, len);
221 a->clen += len;
222 return (0);
223 }
224
225 u_short
in_cksum_skip(struct mbuf * m,int len,int skip)226 in_cksum_skip(struct mbuf *m, int len, int skip)
227 {
228 struct cksum_skip_partial_args a;
229 union q_util q_util;
230 union l_util l_util;
231 uint64_t sum;
232
233 len -= skip;
234
235 /*
236 * The use of m_apply() allows this routine to operate on unmapped
237 * mbufs.
238 */
239 a.csum = 0;
240 a.clen = 0;
241 (void)m_apply(m, skip, len, in_cksum_skip_partial, &a);
242 sum = a.csum;
243 REDUCE16;
244 return (~sum & 0xffff);
245 }
246
in_cksum_hdr(const struct ip * ip)247 u_int in_cksum_hdr(const struct ip *ip)
248 {
249 u_int64_t sum = in_cksumdata(ip, sizeof(struct ip));
250 union q_util q_util;
251 union l_util l_util;
252 REDUCE16;
253 return (~sum & 0xffff);
254 }
255
256 #endif /* !HAVE_MD_IN_CKSUM */
257