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