xref: /freebsd/sys/netinet/in_cksum.c (revision 1323ec57)
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 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/mbuf.h>
45 #include <sys/systm.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/in.h>
48 #include <netinet/ip.h>
49 #include <machine/in_cksum.h>
50 
51 /*
52  * These implementations may be overridden on a per-platform basis.  On
53  * platforms with a direct map, the implementation of in_cksum() must handle
54  * unmapped mbufs.
55  */
56 #ifndef HAVE_MD_IN_CKSUM
57 
58 /*
59  * Checksum routine for Internet Protocol family headers
60  *    (Portable Alpha version).
61  *
62  * This routine is very heavily used in the network
63  * code and should be modified for each CPU to be as fast as possible.
64  */
65 
66 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
67 #define REDUCE32							  \
68     {									  \
69 	q_util.q = sum;							  \
70 	sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3];	  \
71     }
72 #define REDUCE16							  \
73     {									  \
74 	q_util.q = sum;							  \
75 	l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
76 	sum = l_util.s[0] + l_util.s[1];				  \
77 	ADDCARRY(sum);							  \
78     }
79 
80 static const u_int32_t in_masks[] = {
81 #if _BYTE_ORDER == _LITTLE_ENDIAN
82 	/*0 bytes*/ /*1 byte*/	/*2 bytes*/ /*3 bytes*/
83 	0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF,	/* offset 0 */
84 	0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00,	/* offset 1 */
85 	0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000,	/* offset 2 */
86 	0x00000000, 0xFF000000, 0xFF000000, 0xFF000000,	/* offset 3 */
87 #else
88 	/*0 bytes*/ /*1 byte*/	/*2 bytes*/ /*3 bytes*/
89 	0x00000000, 0xFF000000, 0xFFFF0000, 0xFFFFFF00,	/* offset 0 */
90 	0x00000000, 0x00FF0000, 0x00FFFF00, 0x00FFFFFF,	/* offset 1 */
91 	0x00000000, 0x0000FF00, 0x0000FFFF, 0x0000FFFF,	/* offset 2 */
92 	0x00000000, 0x000000FF, 0x000000FF, 0x000000FF,	/* offset 3 */
93 #endif
94 };
95 
96 union l_util {
97 	u_int16_t s[2];
98 	u_int32_t l;
99 };
100 union q_util {
101 	u_int16_t s[4];
102 	u_int32_t l[2];
103 	u_int64_t q;
104 };
105 
106 static u_int64_t
107 in_cksumdata(const void *buf, int len)
108 {
109 	const u_int32_t *lw = (const u_int32_t *) buf;
110 	u_int64_t sum = 0;
111 	u_int64_t prefilled;
112 	int offset;
113 	union q_util q_util;
114 
115 	if ((3 & (long) lw) == 0 && len == 20) {
116 		sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
117 		REDUCE32;
118 		return sum;
119 	}
120 
121 	if ((offset = 3 & (long) lw) != 0) {
122 		const u_int32_t *masks = in_masks + (offset << 2);
123 		lw = (u_int32_t *) (((long) lw) - offset);
124 		sum = *lw++ & masks[len >= 3 ? 3 : len];
125 		len -= 4 - offset;
126 		if (len <= 0) {
127 			REDUCE32;
128 			return sum;
129 		}
130 	}
131 #if 0
132 	/*
133 	 * Force to cache line boundary.
134 	 */
135 	offset = 32 - (0x1f & (long) lw);
136 	if (offset < 32 && len > offset) {
137 		len -= offset;
138 		if (4 & offset) {
139 			sum += (u_int64_t) lw[0];
140 			lw += 1;
141 		}
142 		if (8 & offset) {
143 			sum += (u_int64_t) lw[0] + lw[1];
144 			lw += 2;
145 		}
146 		if (16 & offset) {
147 			sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
148 			lw += 4;
149 		}
150 	}
151 #endif
152 	/*
153 	 * access prefilling to start load of next cache line.
154 	 * then add current cache line
155 	 * save result of prefilling for loop iteration.
156 	 */
157 	prefilled = lw[0];
158 	while ((len -= 32) >= 4) {
159 		u_int64_t prefilling = lw[8];
160 		sum += prefilled + lw[1] + lw[2] + lw[3]
161 			+ lw[4] + lw[5] + lw[6] + lw[7];
162 		lw += 8;
163 		prefilled = prefilling;
164 	}
165 	if (len >= 0) {
166 		sum += prefilled + lw[1] + lw[2] + lw[3]
167 			+ lw[4] + lw[5] + lw[6] + lw[7];
168 		lw += 8;
169 	} else {
170 		len += 32;
171 	}
172 	while ((len -= 16) >= 0) {
173 		sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
174 		lw += 4;
175 	}
176 	len += 16;
177 	while ((len -= 4) >= 0) {
178 		sum += (u_int64_t) *lw++;
179 	}
180 	len += 4;
181 	if (len > 0)
182 		sum += (u_int64_t) (in_masks[len] & *lw);
183 	REDUCE32;
184 	return sum;
185 }
186 
187 u_short
188 in_addword(u_short a, u_short b)
189 {
190 	u_int64_t sum = a + b;
191 
192 	ADDCARRY(sum);
193 	return (sum);
194 }
195 
196 u_short
197 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
198 {
199 	u_int64_t sum;
200 	union q_util q_util;
201 	union l_util l_util;
202 
203 	sum = (u_int64_t) a + b + c;
204 	REDUCE16;
205 	return (sum);
206 }
207 
208 struct cksum_skip_partial_args {
209 	uint64_t csum;
210 	int clen;
211 };
212 
213 static int
214 in_cksum_skip_partial(void *arg, void *data, u_int len)
215 {
216 	struct cksum_skip_partial_args *a;
217 
218 	a = arg;
219         if (((uintptr_t)data ^ a->clen) & 1)
220                 a->csum += in_cksumdata(data, len) << 8;
221         else
222                 a->csum += in_cksumdata(data, len);
223 	a->clen += len;
224 	return (0);
225 }
226 
227 u_short
228 in_cksum_skip(struct mbuf *m, int len, int skip)
229 {
230 	struct cksum_skip_partial_args a;
231 	union q_util q_util;
232 	union l_util l_util;
233 	uint64_t sum;
234 
235 	len -= skip;
236 
237 	/*
238 	 * The use of m_apply() allows this routine to operate on unmapped
239 	 * mbufs.
240 	 */
241 	a.csum = 0;
242 	a.clen = 0;
243 	(void)m_apply(m, skip, len, in_cksum_skip_partial, &a);
244 	sum = a.csum;
245 	REDUCE16;
246 	return (~sum & 0xffff);
247 }
248 
249 u_int in_cksum_hdr(const struct ip *ip)
250 {
251 	u_int64_t sum = in_cksumdata(ip, sizeof(struct ip));
252 	union q_util q_util;
253 	union l_util l_util;
254 	REDUCE16;
255 	return (~sum & 0xffff);
256 }
257 
258 #endif /* !HAVE_MD_IN_CKSUM */
259