]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_cksum.c
1 /* $FreeBSD: src/sys/netinet6/in6_cksum.c,v 1.1.2.3 2001/07/03 11:01:52 ume Exp $ */
2 /* $KAME: in6_cksum.c,v 1.10 2000/12/03 00:53:59 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1988, 1992, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
68 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <kern/debug.h>
72 #include <netinet/in.h>
73 #include <netinet/ip6.h>
75 #include <net/net_osdep.h>
78 * Checksum routine for Internet Protocol family headers (Portable Version).
80 * This routine is very heavily used in the network
81 * code and should be modified for each CPU to be as fast as possible.
84 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
85 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
88 * m MUST contain a continuous IP6 header.
89 * off is a offset where TCP/UDP/ICMP6 header starts.
90 * len is a total length of a transport segment.
91 * (e.g. TCP header + TCP payload)
95 inet6_cksum(struct mbuf
*m
, unsigned int nxt
, unsigned int off
,
101 int byte_swapped
= 0;
109 } ph
__attribute__((__packed__
));
121 if (m
->m_pkthdr
.len
< off
+ len
) {
122 panic("inet6_cksum: mbuf len (%d) < off+len (%d+%d)\n",
123 m
->m_pkthdr
.len
, off
, len
);
127 bzero(&uph
, sizeof (uph
));
130 * First create IP6 pseudo header and calculate a summary.
132 ip6
= mtod(m
, struct ip6_hdr
*);
133 w
= (u_int16_t
*)&ip6
->ip6_src
;
134 uph
.ph
.ph_len
= htonl(len
);
137 /* IPv6 source address */
139 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
141 sum
+= w
[2]; sum
+= w
[3]; sum
+= w
[4]; sum
+= w
[5];
142 sum
+= w
[6]; sum
+= w
[7];
143 /* IPv6 destination address */
145 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
147 sum
+= w
[10]; sum
+= w
[11]; sum
+= w
[12]; sum
+= w
[13];
148 sum
+= w
[14]; sum
+= w
[15];
149 /* Payload length and upper layer identifier */
150 sum
+= uph
.phs
[0]; sum
+= uph
.phs
[1];
151 sum
+= uph
.phs
[2]; sum
+= uph
.phs
[3];
155 * Secondly calculate a summary of the first mbuf excluding offset.
157 while (m
!= NULL
&& off
> 0) {
164 w
= (u_int16_t
*)(mtod(m
, u_char
*) + off
);
165 mlen
= m
->m_len
- off
;
170 * Force to even boundary.
172 if ((1 & (long) w
) && (mlen
> 0)) {
175 s_util
.c
[0] = *(u_char
*)w
;
176 w
= (u_int16_t
*)((char *)w
+ 1);
181 * Unroll the loop to make overhead from
184 while ((mlen
-= 32) >= 0) {
185 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
186 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
187 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
188 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
192 while ((mlen
-= 8) >= 0) {
193 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
197 if (mlen
== 0 && byte_swapped
== 0)
200 while ((mlen
-= 2) >= 0) {
208 s_util
.c
[1] = *(char *)w
;
213 } else if (mlen
== -1)
214 s_util
.c
[0] = *(char *)w
;
219 * Lastly calculate a summary of the rest of mbufs.
222 for (;m
&& len
; m
= m
->m_next
) {
225 w
= mtod(m
, u_int16_t
*);
228 * The first byte of this mbuf is the continuation
229 * of a word spanning between this mbuf and the
232 * s_util.c[0] is already saved when scanning previous
235 s_util
.c
[1] = *(char *)w
;
237 w
= (u_int16_t
*)((char *)w
+ 1);
246 * Force to even boundary.
248 if ((1 & (long) w
) && (mlen
> 0)) {
251 s_util
.c
[0] = *(u_char
*)w
;
252 w
= (u_int16_t
*)((char *)w
+ 1);
257 * Unroll the loop to make overhead from
260 while ((mlen
-= 32) >= 0) {
261 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
262 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
263 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
264 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
268 while ((mlen
-= 8) >= 0) {
269 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
273 if (mlen
== 0 && byte_swapped
== 0)
276 while ((mlen
-= 2) >= 0) {
284 s_util
.c
[1] = *(char *)w
;
289 } else if (mlen
== -1)
290 s_util
.c
[0] = *(char *)w
;
293 printf("inet6_cksum: out of data by %d\n", len
);
295 /* The last mbuf has odd # of bytes. Follow the
296 standard (the odd byte may be shifted left by 8 bits
297 or not as determined by endian-ness of the machine) */
302 return (~sum
& 0xffff);