]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_cksum.c
21aea718be199207f3e03cff0934b5c4ac602409
1 /* $KAME: in6_cksum.c,v 1.5 2000/02/22 14:04:17 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1988, 1992, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
67 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <netinet/in.h>
71 #include <netinet/ip6.h>
73 #include <net/net_osdep.h>
76 * Checksum routine for Internet Protocol family headers (Portable Version).
78 * This routine is very heavily used in the network
79 * code and should be modified for each CPU to be as fast as possible.
82 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
83 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
95 * m MUST contain a continuous IP6 header.
96 * off is a offset where TCP/UDP/ICMP6 header starts.
97 * len is a total length of a transport segment.
98 * (e.g. TCP header + TCP payload)
102 in6_cksum(m
, nxt
, off
, len
)
103 register struct mbuf
*m
;
107 register u_int16_t
*w
;
108 register int sum
= 0;
109 register int mlen
= 0;
110 int byte_swapped
= 0;
112 int srcifid
= 0, dstifid
= 0;
126 if (m
->m_pkthdr
.len
< off
+ len
) {
127 panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)\n",
128 m
->m_pkthdr
.len
, off
, len
);
132 * First create IP6 pseudo header and calculate a summary.
134 ip6
= mtod(m
, struct ip6_hdr
*);
136 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
137 srcifid
= ip6
->ip6_src
.s6_addr16
[1];
138 ip6
->ip6_src
.s6_addr16
[1] = 0;
140 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
141 dstifid
= ip6
->ip6_dst
.s6_addr16
[1];
142 ip6
->ip6_dst
.s6_addr16
[1] = 0;
145 w
= (u_int16_t
*)&ip6
->ip6_src
;
146 uph
.ph
.ph_len
= htonl(len
);
149 /* IPv6 source address */
151 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
153 sum
+= w
[2]; sum
+= w
[3]; sum
+= w
[4]; sum
+= w
[5];
154 sum
+= w
[6]; sum
+= w
[7];
155 /* IPv6 destination address */
157 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
159 sum
+= w
[10]; sum
+= w
[11]; sum
+= w
[12]; sum
+= w
[13];
160 sum
+= w
[14]; sum
+= w
[15];
161 /* Payload length and upper layer identifier */
162 sum
+= uph
.phs
[0]; sum
+= uph
.phs
[1];
163 sum
+= uph
.phs
[2]; sum
+= uph
.phs
[3];
167 ip6
->ip6_src
.s6_addr16
[1] = srcifid
;
169 ip6
->ip6_dst
.s6_addr16
[1] = dstifid
;
172 * Secondly calculate a summary of the first mbuf excluding offset.
174 while (m
!= NULL
&& off
> 0) {
181 w
= (u_int16_t
*)(mtod(m
, u_char
*) + off
);
182 mlen
= m
->m_len
- off
;
187 * Force to even boundary.
189 if ((1 & (long) w
) && (mlen
> 0)) {
192 s_util
.c
[0] = *(u_char
*)w
;
193 w
= (u_int16_t
*)((char *)w
+ 1);
198 * Unroll the loop to make overhead from
201 while ((mlen
-= 32) >= 0) {
202 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
203 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
204 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
205 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
209 while ((mlen
-= 8) >= 0) {
210 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
214 if (mlen
== 0 && byte_swapped
== 0)
217 while ((mlen
-= 2) >= 0) {
225 s_util
.c
[1] = *(char *)w
;
230 } else if (mlen
== -1)
231 s_util
.c
[0] = *(char *)w
;
236 * Lastly calculate a summary of the rest of mbufs.
239 for (;m
&& len
; m
= m
->m_next
) {
242 w
= mtod(m
, u_int16_t
*);
245 * The first byte of this mbuf is the continuation
246 * of a word spanning between this mbuf and the
249 * s_util.c[0] is already saved when scanning previous
252 s_util
.c
[1] = *(char *)w
;
254 w
= (u_int16_t
*)((char *)w
+ 1);
263 * Force to even boundary.
265 if ((1 & (long) w
) && (mlen
> 0)) {
268 s_util
.c
[0] = *(u_char
*)w
;
269 w
= (u_int16_t
*)((char *)w
+ 1);
274 * Unroll the loop to make overhead from
277 while ((mlen
-= 32) >= 0) {
278 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
279 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
280 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
281 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
285 while ((mlen
-= 8) >= 0) {
286 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
290 if (mlen
== 0 && byte_swapped
== 0)
293 while ((mlen
-= 2) >= 0) {
301 s_util
.c
[1] = *(char *)w
;
306 } else if (mlen
== -1)
307 s_util
.c
[0] = *(char *)w
;
310 panic("in6_cksum: out of data\n");
312 /* The last mbuf has odd # of bytes. Follow the
313 standard (the odd byte may be shifted left by 8 bits
314 or not as determined by endian-ness of the machine) */
319 return (~sum
& 0xffff);