]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_cksum.c
efaaea5b43ef3d13e14345a405621e62d544daed
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1988, 1992, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
65 #include <sys/param.h>
67 #include <sys/kdebug.h>
69 #define DBG_FNC_IN_CKSUM NETDBG_CODE(DBG_NETIP, (3 << 8))
72 * Checksum routine for Internet Protocol family headers (Portable Version).
74 * This routine is very heavily used in the network
75 * code and should be modified for each CPU to be as fast as possible.
94 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
99 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
104 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
105 sum = l_util.s[0] + l_util.s[1]; \
109 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
114 __inline
unsigned short
115 in_addword(u_short a
, u_short b
)
118 u_int32_t sum
= a
+ b
;
123 __inline
unsigned short
124 in_pseudo(u_int a
, u_int b
, u_int c
)
130 sum
= (u_int64_t
) a
+ b
+ c
;
138 register struct mbuf
*m
;
142 register int sum
= 0;
143 register int mlen
= 0;
144 int starting_on_odd
= 0;
147 KERNEL_DEBUG(DBG_FNC_IN_CKSUM
| DBG_FUNC_START
, len
,0,0,0,0);
149 for (;m
&& len
; m
= m
->m_next
) {
153 w
= mtod(m
, u_short
*);
158 sum
= xsum_assym(w
, mlen
, sum
, starting_on_odd
);
169 KERNEL_DEBUG(DBG_FNC_IN_CKSUM
| DBG_FUNC_END
, 0,0,0,0,0);
170 return (~sum
& 0xffff);
174 in_cksum_skip(m
, len
, skip
)
175 register struct mbuf
*m
;
180 register int sum
= 0;
181 register int mlen
= 0;
182 int starting_on_odd
= 0;
185 for (; skip
&& m
; m
= m
->m_next
) {
186 if (m
->m_len
> skip
) {
187 mlen
= m
->m_len
- skip
;
188 w
= (u_short
*)(m
->m_data
+skip
);
194 for (;m
&& len
; m
= m
->m_next
) {
198 w
= mtod(m
, u_short
*);
203 sum
= xsum_assym(w
, mlen
, sum
, starting_on_odd
);
214 return (~sum
& 0xffff);
219 in_addword(u_short a
, u_short b
)
222 u_int32_t sum
= a
+ b
;
228 in_pseudo(u_int a
, u_int b
, u_int c
)
234 sum
= (u_int64_t
) a
+ b
+ c
;
242 register struct mbuf
*m
;
246 register int sum
= 0;
247 register int mlen
= 0;
248 int byte_swapped
= 0;
252 KERNEL_DEBUG(DBG_FNC_IN_CKSUM
| DBG_FUNC_START
, len
,0,0,0,0);
254 for (;m
&& len
; m
= m
->m_next
) {
257 w
= mtod(m
, u_short
*);
260 * The first byte of this mbuf is the continuation
261 * of a word spanning between this mbuf and the
264 * s_util.c[0] is already saved when scanning previous
267 s_util
.c
[1] = *(char *)w
;
269 w
= (u_short
*)((char *)w
+ 1);
278 * Force to even boundary.
280 if ((1 & (int) w
) && (mlen
> 0)) {
283 s_util
.c
[0] = *(u_char
*)w
;
284 w
= (u_short
*)((char *)w
+ 1);
289 * Unroll the loop to make overhead from
292 while ((mlen
-= 32) >= 0) {
293 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
294 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
295 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
296 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
300 while ((mlen
-= 8) >= 0) {
301 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
305 if (mlen
== 0 && byte_swapped
== 0)
308 while ((mlen
-= 2) >= 0) {
316 s_util
.c
[1] = *(char *)w
;
321 } else if (mlen
== -1)
322 s_util
.c
[0] = *(char *)w
;
325 printf("cksum: out of data\n");
327 /* The last mbuf has odd # of bytes. Follow the
328 standard (the odd byte may be shifted left by 8 bits
329 or not as determined by endian-ness of the machine) */
334 KERNEL_DEBUG(DBG_FNC_IN_CKSUM
| DBG_FUNC_END
, 0,0,0,0,0);
335 return (~sum
& 0xffff);
339 in_cksum_skip(m
, len
, skip
)
340 register struct mbuf
*m
;
341 register u_short len
;
342 register u_short skip
;
345 register int sum
= 0;
346 register int mlen
= 0;
347 int byte_swapped
= 0;
351 KERNEL_DEBUG(DBG_FNC_IN_CKSUM
| DBG_FUNC_START
, len
,0,0,0,0);
354 for (; skip
&& m
; m
= m
->m_next
) {
355 if (m
->m_len
> skip
) {
356 mlen
= m
->m_len
- skip
;
357 w
= (u_short
*)(m
->m_data
+skip
);
363 for (;m
&& len
; m
= m
->m_next
) {
366 w
= mtod(m
, u_short
*);
370 * The first byte of this mbuf is the continuation
371 * of a word spanning between this mbuf and the
374 * s_util.c[0] is already saved when scanning previous
377 s_util
.c
[1] = *(char *)w
;
379 w
= (u_short
*)((char *)w
+ 1);
391 * Force to even boundary.
393 if ((1 & (int) w
) && (mlen
> 0)) {
396 s_util
.c
[0] = *(u_char
*)w
;
397 w
= (u_short
*)((char *)w
+ 1);
402 * Unroll the loop to make overhead from
405 while ((mlen
-= 32) >= 0) {
406 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
407 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
408 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
409 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
413 while ((mlen
-= 8) >= 0) {
414 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
418 if (mlen
== 0 && byte_swapped
== 0)
421 while ((mlen
-= 2) >= 0) {
429 s_util
.c
[1] = *(char *)w
;
434 } else if (mlen
== -1)
435 s_util
.c
[0] = *(char *)w
;
438 printf("cksum: out of data\n");
440 /* The last mbuf has odd # of bytes. Follow the
441 standard (the odd byte may be shifted left by 8 bits
442 or not as determined by endian-ness of the machine) */
447 KERNEL_DEBUG(DBG_FNC_IN_CKSUM
| DBG_FUNC_END
, 0,0,0,0,0);
448 return (~sum
& 0xffff);