2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1988, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Matt Thomas <matt@3am-software.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
37 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
40 #include <sys/cdefs.h>
41 #include <sys/param.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>
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
54 #ifndef HAVE_MD_IN_CKSUM
57 * Checksum routine for Internet Protocol family headers
58 * (Portable Alpha version).
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.
64 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
68 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
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]; \
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 */
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 */
105 in_cksumdata(const void *buf, int len)
107 const u_int32_t *lw = (const u_int32_t *) buf;
113 if ((3 & (long) lw) == 0 && len == 20) {
114 sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
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];
131 * Force to cache line boundary.
133 offset = 32 - (0x1f & (long) lw);
134 if (offset < 32 && len > offset) {
137 sum += (u_int64_t) lw[0];
141 sum += (u_int64_t) lw[0] + lw[1];
145 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
151 * access prefilling to start load of next cache line.
152 * then add current cache line
153 * save result of prefilling for loop iteration.
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];
161 prefilled = prefilling;
164 sum += prefilled + lw[1] + lw[2] + lw[3]
165 + lw[4] + lw[5] + lw[6] + lw[7];
170 while ((len -= 16) >= 0) {
171 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
175 while ((len -= 4) >= 0) {
176 sum += (u_int64_t) *lw++;
180 sum += (u_int64_t) (in_masks[len] & *lw);
186 in_addword(u_short a, u_short b)
188 u_int64_t sum = a + b;
195 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
201 sum = (u_int64_t) a + b + c;
206 struct cksum_skip_partial_args {
212 in_cksum_skip_partial(void *arg, void *data, u_int len)
214 struct cksum_skip_partial_args *a;
217 if (((uintptr_t)data ^ a->clen) & 1)
218 a->csum += in_cksumdata(data, len) << 8;
220 a->csum += in_cksumdata(data, len);
226 in_cksum_skip(struct mbuf *m, int len, int skip)
228 struct cksum_skip_partial_args a;
236 * The use of m_apply() allows this routine to operate on unmapped
241 (void)m_apply(m, skip, len, in_cksum_skip_partial, &a);
244 return (~sum & 0xffff);
247 u_int in_cksum_hdr(const struct ip *ip)
249 u_int64_t sum = in_cksumdata(ip, sizeof(struct ip));
253 return (~sum & 0xffff);
256 #endif /* !HAVE_MD_IN_CKSUM */