]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in_cksum.c
xnu-1228.5.18.tar.gz
[apple/xnu.git] / bsd / netinet / in_cksum.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1988, 1992, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
61 */
62
63#include <sys/param.h>
64#include <sys/mbuf.h>
65#include <sys/kdebug.h>
2d21ac55
A
66#include <kern/debug.h>
67#include <netinet/in.h>
68#include <netinet/ip.h>
1c79356b
A
69
70#define DBG_FNC_IN_CKSUM NETDBG_CODE(DBG_NETIP, (3 << 8))
71
72/*
73 * Checksum routine for Internet Protocol family headers (Portable Version).
74 *
75 * This routine is very heavily used in the network
76 * code and should be modified for each CPU to be as fast as possible.
77 */
78
0b4e3aa0
A
79union s_util {
80 char c[2];
81 u_short s;
82};
1c79356b 83
0b4e3aa0
A
84union l_util {
85 u_int16_t s[2];
2d21ac55 86 u_int32_t l;
0b4e3aa0 87};
1c79356b 88
0b4e3aa0
A
89union q_util {
90 u_int16_t s[4];
91 u_int32_t l[2];
92 u_int64_t q;
2d21ac55 93};
0b4e3aa0
A
94
95#define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
96
97#define REDUCE32 \
98 { \
99 q_util.q = sum; \
100 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
101 }
102#define REDUCE16 \
103 { \
104 q_util.q = sum; \
105 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
106 sum = l_util.s[0] + l_util.s[1]; \
107 ADDCARRY(sum); \
108 }
109
110#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
111
2d21ac55
A
112u_int16_t inet_cksum_simple(struct mbuf *, int);
113
114u_int16_t
115inet_cksum_simple(struct mbuf *m, int len)
116{
117 return (inet_cksum(m, 0, 0, len));
118}
1c79356b 119
2d21ac55
A
120#if defined(__ppc__)
121
122extern u_short xsum_assym(u_short *p, int len, u_short xsum, int odd);
123
124inline u_short
0b4e3aa0
A
125in_addword(u_short a, u_short b)
126{
2d21ac55 127 union l_util l_util;
0b4e3aa0 128 u_int32_t sum = a + b;
2d21ac55 129
0b4e3aa0
A
130 REDUCE;
131 return (sum);
132}
133
2d21ac55 134inline u_short
0b4e3aa0
A
135in_pseudo(u_int a, u_int b, u_int c)
136{
137 u_int64_t sum;
138 union q_util q_util;
2d21ac55 139 union l_util l_util;
0b4e3aa0
A
140
141 sum = (u_int64_t) a + b + c;
142 REDUCE16;
143 return (sum);
144
145}
146
2d21ac55
A
147u_int16_t
148inet_cksum(struct mbuf *m, unsigned int nxt, unsigned int skip,
149 unsigned int len)
1c79356b 150{
2d21ac55
A
151 u_short *w;
152 u_int32_t sum = 0;
153 int mlen = 0;
1c79356b
A
154 int starting_on_odd = 0;
155
1c79356b
A
156 KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_START, len,0,0,0,0);
157
2d21ac55
A
158 /* sanity check */
159 if (m->m_pkthdr.len < skip + len) {
160 panic("inet_cksum: mbuf len (%d) < off+len (%d+%d)\n",
161 m->m_pkthdr.len, skip, len);
162 }
1c79356b 163
2d21ac55
A
164 /* include pseudo header checksum? */
165 if (nxt != 0) {
166 struct ip *iph;
1c79356b 167
2d21ac55
A
168 if (m->m_len < sizeof (struct ip))
169 panic("inet_cksum: bad mbuf chain");
1c79356b 170
2d21ac55
A
171 iph = mtod(m, struct ip *);
172 sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr,
173 htonl(len + nxt));
174 }
1c79356b 175
2d21ac55
A
176 if (skip != 0) {
177 for (; skip && m; m = m->m_next) {
178 if (m->m_len > skip) {
179 mlen = m->m_len - skip;
180 w = (u_short *)(m->m_data+skip);
181 goto skip_start;
182 } else {
183 skip -= m->m_len;
184 }
185 }
186 }
0b4e3aa0 187
0b4e3aa0
A
188 for (;m && len; m = m->m_next) {
189 if (m->m_len == 0)
190 continue;
191 mlen = m->m_len;
192 w = mtod(m, u_short *);
193
765c9de3 194skip_start:
0b4e3aa0
A
195 if (len < mlen)
196 mlen = len;
0b4e3aa0
A
197 sum = xsum_assym(w, mlen, sum, starting_on_odd);
198 len -= mlen;
199 if (mlen & 0x1)
200 {
201 if (starting_on_odd)
202 starting_on_odd = 0;
203 else
204 starting_on_odd = 1;
205 }
206 }
207
2d21ac55
A
208 KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_END, 0,0,0,0,0);
209
0b4e3aa0
A
210 return (~sum & 0xffff);
211}
2d21ac55 212
1c79356b
A
213#else
214
2d21ac55 215inline u_short
0b4e3aa0 216in_addword(u_short a, u_short b)
2d21ac55
A
217{
218 union l_util l_util;
0b4e3aa0 219 u_int32_t sum = a + b;
2d21ac55 220
0b4e3aa0
A
221 REDUCE(sum);
222 return (sum);
2d21ac55 223}
1c79356b 224
2d21ac55 225inline u_short
0b4e3aa0
A
226in_pseudo(u_int a, u_int b, u_int c)
227{
2d21ac55 228 u_int64_t sum;
0b4e3aa0 229 union q_util q_util;
2d21ac55 230 union l_util l_util;
1c79356b 231
0b4e3aa0
A
232 sum = (u_int64_t) a + b + c;
233 REDUCE16;
234 return (sum);
235}
1c79356b 236
2d21ac55
A
237u_int16_t
238inet_cksum(struct mbuf *m, unsigned int nxt, unsigned int skip,
239 unsigned int len)
1c79356b 240{
2d21ac55
A
241 u_short *w;
242 u_int32_t sum = 0;
243 int mlen = 0;
1c79356b 244 int byte_swapped = 0;
0b4e3aa0 245 union s_util s_util;
2d21ac55 246 union l_util l_util;
1c79356b
A
247
248 KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_START, len,0,0,0,0);
249
2d21ac55
A
250 /* sanity check */
251 if (m->m_pkthdr.len < skip + len) {
252 panic("inet_cksum: mbuf len (%d) < off+len (%d+%d)\n",
253 m->m_pkthdr.len, skip, len);
0b4e3aa0 254 }
0b4e3aa0 255
2d21ac55
A
256 /* include pseudo header checksum? */
257 if (nxt != 0) {
258 struct ip *iph;
0b4e3aa0 259
2d21ac55
A
260 if (m->m_len < sizeof (struct ip))
261 panic("inet_cksum: bad mbuf chain");
262
263 iph = mtod(m, struct ip *);
264 sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr,
265 htonl(len + nxt));
266 }
0b4e3aa0 267
2d21ac55
A
268 if (skip != 0) {
269 for (; skip && m; m = m->m_next) {
270 if (m->m_len > skip) {
271 mlen = m->m_len - skip;
272 w = (u_short *)(m->m_data+skip);
273 goto skip_start;
274 } else {
275 skip -= m->m_len;
276 }
277 }
278 }
0b4e3aa0
A
279 for (;m && len; m = m->m_next) {
280 if (m->m_len == 0)
281 continue;
282 w = mtod(m, u_short *);
283
284 if (mlen == -1) {
285 /*
286 * The first byte of this mbuf is the continuation
287 * of a word spanning between this mbuf and the
288 * last mbuf.
289 *
290 * s_util.c[0] is already saved when scanning previous
291 * mbuf.
292 */
293 s_util.c[1] = *(char *)w;
294 sum += s_util.s;
295 w = (u_short *)((char *)w + 1);
296 mlen = m->m_len - 1;
297 len--;
298 } else {
2d21ac55 299 mlen = m->m_len;
0b4e3aa0 300 }
0b4e3aa0 301skip_start:
765c9de3 302 if (len < mlen)
2d21ac55 303 mlen = len;
0b4e3aa0
A
304
305 len -= mlen;
306 /*
307 * Force to even boundary.
308 */
309 if ((1 & (int) w) && (mlen > 0)) {
310 REDUCE;
311 sum <<= 8;
312 s_util.c[0] = *(u_char *)w;
313 w = (u_short *)((char *)w + 1);
314 mlen--;
315 byte_swapped = 1;
316 }
317 /*
318 * Unroll the loop to make overhead from
319 * branches &c small.
320 */
321 while ((mlen -= 32) >= 0) {
322 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
323 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
1c79356b
A
324 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
325 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
326 w += 16;
327 }
328 mlen += 32;
329 while ((mlen -= 8) >= 0) {
330 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
331 w += 4;
332 }
333 mlen += 8;
334 if (mlen == 0 && byte_swapped == 0)
335 continue;
336 REDUCE;
337 while ((mlen -= 2) >= 0) {
338 sum += *w++;
339 }
340 if (byte_swapped) {
341 REDUCE;
342 sum <<= 8;
343 byte_swapped = 0;
344 if (mlen == -1) {
345 s_util.c[1] = *(char *)w;
346 sum += s_util.s;
347 mlen = 0;
348 } else
349 mlen = -1;
350 } else if (mlen == -1)
351 s_util.c[0] = *(char *)w;
352 }
353 if (len)
2d21ac55 354 printf("cksum: out of data by %d\n", len);
1c79356b
A
355 if (mlen == -1) {
356 /* The last mbuf has odd # of bytes. Follow the
357 standard (the odd byte may be shifted left by 8 bits
358 or not as determined by endian-ness of the machine) */
359 s_util.c[1] = 0;
360 sum += s_util.s;
361 }
362 REDUCE;
363 KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_END, 0,0,0,0,0);
364 return (~sum & 0xffff);
365}
366
367#endif