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