2 /* $NetBSD: in_cksum.c,v 1.7 1997/09/02 13:18:15 thorpej Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (c) 1988, 1992, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Matt Thomas <matt@3am-software.com>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
43 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
45 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/in.h>
50 #include <netinet/ip.h>
51 #include <machine/in_cksum.h>
54 * Checksum routine for Internet Protocol family headers
55 * (Portable Alpha version).
57 * This routine is very heavily used in the network
58 * code and should be modified for each CPU to be as fast as possible.
61 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
65 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
70 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
71 sum = l_util.s[0] + l_util.s[1]; \
75 static const u_int32_t in_masks[] = {
77 /*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/
78 0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
79 0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
80 0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
81 0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
83 /*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/
84 0x00000000, 0xFF000000, 0xFFFF0000, 0xFFFFFF00, /* offset 0 */
85 0x00000000, 0x00FF0000, 0x00FFFF00, 0x00FFFFFF, /* offset 1 */
86 0x00000000, 0x0000FF00, 0x0000FFFF, 0x0000FFFF, /* offset 2 */
87 0x00000000, 0x000000FF, 0x000000FF, 0x000000FF, /* offset 3 */
102 in_cksumdata(const void *buf, int len)
104 const u_int32_t *lw = (const u_int32_t *) buf;
110 if ((3 & (long) lw) == 0 && len == 20) {
111 sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
116 if ((offset = 3 & (long) lw) != 0) {
117 const u_int32_t *masks = in_masks + (offset << 2);
118 lw = (u_int32_t *) (((long) lw) - offset);
119 sum = *lw++ & masks[len >= 3 ? 3 : len];
128 * Force to cache line boundary.
130 offset = 32 - (0x1f & (long) lw);
131 if (offset < 32 && len > offset) {
134 sum += (u_int64_t) lw[0];
138 sum += (u_int64_t) lw[0] + lw[1];
142 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
148 * access prefilling to start load of next cache line.
149 * then add current cache line
150 * save result of prefilling for loop iteration.
153 while ((len -= 32) >= 4) {
154 u_int64_t prefilling = lw[8];
155 sum += prefilled + lw[1] + lw[2] + lw[3]
156 + lw[4] + lw[5] + lw[6] + lw[7];
158 prefilled = prefilling;
161 sum += prefilled + lw[1] + lw[2] + lw[3]
162 + lw[4] + lw[5] + lw[6] + lw[7];
167 while ((len -= 16) >= 0) {
168 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
172 while ((len -= 4) >= 0) {
173 sum += (u_int64_t) *lw++;
177 sum += (u_int64_t) (in_masks[len] & *lw);
183 in_addword(u_short a, u_short b)
185 u_int64_t sum = a + b;
192 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
198 sum = (u_int64_t) a + b + c;
204 in_cksum_skip(struct mbuf *m, int len, int skip)
214 for (; skip && m; m = m->m_next) {
215 if (m->m_len > skip) {
216 mlen = m->m_len - skip;
217 addr = mtod(m, caddr_t) + skip;
224 for (; m && len; m = m->m_next) {
228 addr = mtod(m, caddr_t);
233 if ((clen ^ (long) addr) & 1)
234 sum += in_cksumdata(addr, mlen) << 8;
236 sum += in_cksumdata(addr, mlen);
242 return (~sum & 0xffff);
245 u_int in_cksum_hdr(const struct ip *ip)
247 u_int64_t sum = in_cksumdata(ip, sizeof(struct ip));
251 return (~sum & 0xffff);