]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netns/ns_cksum.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1992, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)ns_cksum.c 8.1 (Berkeley) 6/10/93
57 #include <sys/param.h>
61 * Checksum routine for Network Systems Protocol Packets (Big-Endian).
63 * This routine is very heavily used in the network
64 * code and should be modified for each CPU to be as fast as possible.
67 #define ADDCARRY(x) { if ((x) > 65535) (x) -= 65535; }
68 #define FOLD(x) {l_util.l = (x); (x) = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
72 register struct mbuf
*m
;
77 register int mlen
= 0;
85 for (;m
&& len
; m
= m
->m_next
) {
89 * Each trip around loop adds in
90 * word from one mbuf segment.
92 w
= mtod(m
, u_short
*);
95 * There is a byte left from the last segment;
96 * ones-complement add it into the checksum.
98 #if BYTE_ORDER == BIG_ENDIAN
101 sum
+= *(u_char
*)w
<< 8;
104 w
= (u_short
*)(1 + (char *)w
);
114 * We can do a 16 bit ones complement sum using
115 * 32 bit arithmetic registers for adding,
116 * with carries from the low added
117 * into the high (by normal carry-chaining)
118 * so long as we fold back before 16 carries have occured.
123 /* -DTINY reduces the size from 1250 to 550, but slows it down by 22% */
124 while ((mlen
-= 32) >= 0) {
125 sum
+= w
[0]; sum
+= sum
; sum
+= w
[1]; sum
+= sum
;
126 sum
+= w
[2]; sum
+= sum
; sum
+= w
[3]; sum
+= sum
;
127 sum
+= w
[4]; sum
+= sum
; sum
+= w
[5]; sum
+= sum
;
128 sum
+= w
[6]; sum
+= sum
; sum
+= w
[7]; sum
+= sum
;
130 sum
+= w
[8]; sum
+= sum
; sum
+= w
[9]; sum
+= sum
;
131 sum
+= w
[10]; sum
+= sum
; sum
+= w
[11]; sum
+= sum
;
132 sum
+= w
[12]; sum
+= sum
; sum
+= w
[13]; sum
+= sum
;
133 sum
+= w
[14]; sum
+= sum
; sum
+= w
[15]; sum
+= sum
;
139 while ((mlen
-= 8) >= 0) {
140 sum
+= w
[0]; sum
+= sum
; sum
+= w
[1]; sum
+= sum
;
141 sum
+= w
[2]; sum
+= sum
; sum
+= w
[3]; sum
+= sum
;
146 while ((mlen
-= 2) >= 0) {
147 sum
+= *w
++; sum
+= sum
;
151 #if BYTE_ORDER == BIG_ENDIAN
152 #define ww(n) (((u_char *)w)[n + n + 1])
153 #define vv(n) (((u_char *)w)[n + n])
155 #if BYTE_ORDER == LITTLE_ENDIAN
156 #define vv(n) (((u_char *)w)[n + n + 1])
157 #define ww(n) (((u_char *)w)[n + n])
162 while ((mlen
-= 32) >= 0) {
163 sum
+= ww(0); sum
+= sum
; sum
+= ww(1); sum
+= sum
;
164 sum
+= ww(2); sum
+= sum
; sum
+= ww(3); sum
+= sum
;
165 sum
+= ww(4); sum
+= sum
; sum
+= ww(5); sum
+= sum
;
166 sum
+= ww(6); sum
+= sum
; sum
+= ww(7); sum
+= sum
;
168 sum
+= ww(8); sum
+= sum
; sum
+= ww(9); sum
+= sum
;
169 sum
+= ww(10); sum
+= sum
; sum
+= ww(11); sum
+= sum
;
170 sum
+= ww(12); sum
+= sum
; sum
+= ww(13); sum
+= sum
;
171 sum
+= ww(14); sum
+= sum
; sum
+= ww(15); sum
+= sum
;
173 sum2
+= vv(0); sum2
+= sum2
; sum2
+= vv(1); sum2
+= sum2
;
174 sum2
+= vv(2); sum2
+= sum2
; sum2
+= vv(3); sum2
+= sum2
;
175 sum2
+= vv(4); sum2
+= sum2
; sum2
+= vv(5); sum2
+= sum2
;
176 sum2
+= vv(6); sum2
+= sum2
; sum2
+= vv(7); sum2
+= sum2
;
178 sum2
+= vv(8); sum2
+= sum2
; sum2
+= vv(9); sum2
+= sum2
;
179 sum2
+= vv(10); sum2
+= sum2
; sum2
+= vv(11); sum2
+= sum2
;
180 sum2
+= vv(12); sum2
+= sum2
; sum2
+= vv(13); sum2
+= sum2
;
181 sum2
+= vv(14); sum2
+= sum2
; sum2
+= vv(15); sum2
+= sum2
;
187 while ((mlen
-= 8) >= 0) {
188 sum
+= ww(0); sum
+= sum
; sum
+= ww(1); sum
+= sum
;
189 sum
+= ww(2); sum
+= sum
; sum
+= ww(3); sum
+= sum
;
191 sum2
+= vv(0); sum2
+= sum2
; sum2
+= vv(1); sum2
+= sum2
;
192 sum2
+= vv(2); sum2
+= sum2
; sum2
+= vv(3); sum2
+= sum2
;
197 while ((mlen
-= 2) >= 0) {
198 sum
+= ww(0); sum
+= sum
;
199 sum2
+= vv(0); sum2
+= sum2
;
205 #if BYTE_ORDER == BIG_ENDIAN
206 sum
+= *(u_char
*)w
<< 8;
214 /* We had an odd number of bytes to sum; assume a garbage
215 byte of zero and clean up */
220 * sum has already been kept to low sixteen bits.
221 * just examine result and exit.
223 if(sum
==0xffff) sum
= 0;