1 /* $FreeBSD: src/sys/netinet6/in6_cksum.c,v 1.1.2.4 2003/05/06 06:46:58 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6_cksum.c,v 1.4 2006/10/24 06:18:42 hsu Exp $ */
3 /* $KAME: in6_cksum.c,v 1.10 2000/12/03 00:53:59 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1988, 1992, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
69 #include <sys/param.h>
71 #include <sys/systm.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 in6_cksum(struct mbuf
*m
, u_int8_t nxt
, u_int32_t off
, u_int32_t len
)
100 int byte_swapped
= 0;
102 int srcifid
= 0, dstifid
= 0;
111 } ph
__attribute__((__packed__
));
123 if (m
->m_pkthdr
.len
< off
+ len
) {
124 panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
125 m
->m_pkthdr
.len
, off
, len
);
128 bzero(&uph
, sizeof(uph
));
131 * First create IP6 pseudo header and calculate a summary.
133 ip6
= mtod(m
, struct ip6_hdr
*);
135 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
136 srcifid
= ip6
->ip6_src
.s6_addr16
[1];
137 ip6
->ip6_src
.s6_addr16
[1] = 0;
139 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
140 dstifid
= ip6
->ip6_dst
.s6_addr16
[1];
141 ip6
->ip6_dst
.s6_addr16
[1] = 0;
144 w
= (u_int16_t
*)&ip6
->ip6_src
;
145 uph
.ph
.ph_len
= htonl(len
);
148 /* IPv6 source address */
150 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
152 sum
+= w
[2]; sum
+= w
[3]; sum
+= w
[4]; sum
+= w
[5];
153 sum
+= w
[6]; sum
+= w
[7];
154 /* IPv6 destination address */
156 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
158 sum
+= w
[10]; sum
+= w
[11]; sum
+= w
[12]; sum
+= w
[13];
159 sum
+= w
[14]; sum
+= w
[15];
160 /* Payload length and upper layer identifier */
161 sum
+= uph
.phs
[0]; sum
+= uph
.phs
[1];
162 sum
+= uph
.phs
[2]; sum
+= uph
.phs
[3];
166 ip6
->ip6_src
.s6_addr16
[1] = srcifid
;
168 ip6
->ip6_dst
.s6_addr16
[1] = dstifid
;
171 * Secondly calculate a summary of the first mbuf excluding offset.
173 while (m
!= NULL
&& off
> 0) {
180 w
= (u_int16_t
*)(mtod(m
, u_char
*) + off
);
181 mlen
= m
->m_len
- off
;
186 * Force to even boundary.
188 if ((1 & (long) w
) && (mlen
> 0)) {
191 s_util
.c
[0] = *(u_char
*)w
;
192 w
= (u_int16_t
*)((char *)w
+ 1);
197 * Unroll the loop to make overhead from
200 while ((mlen
-= 32) >= 0) {
201 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
202 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
203 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
204 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
208 while ((mlen
-= 8) >= 0) {
209 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
213 if (mlen
== 0 && byte_swapped
== 0)
216 while ((mlen
-= 2) >= 0) {
224 s_util
.c
[1] = *(char *)w
;
229 } else if (mlen
== -1)
230 s_util
.c
[0] = *(char *)w
;
235 * Lastly calculate a summary of the rest of mbufs.
238 for (;m
&& len
; m
= m
->m_next
) {
241 w
= mtod(m
, u_int16_t
*);
244 * The first byte of this mbuf is the continuation
245 * of a word spanning between this mbuf and the
248 * s_util.c[0] is already saved when scanning previous
251 s_util
.c
[1] = *(char *)w
;
253 w
= (u_int16_t
*)((char *)w
+ 1);
262 * Force to even boundary.
264 if ((1 & (long) w
) && (mlen
> 0)) {
267 s_util
.c
[0] = *(u_char
*)w
;
268 w
= (u_int16_t
*)((char *)w
+ 1);
273 * Unroll the loop to make overhead from
276 while ((mlen
-= 32) >= 0) {
277 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
278 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
279 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
280 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
284 while ((mlen
-= 8) >= 0) {
285 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
289 if (mlen
== 0 && byte_swapped
== 0)
292 while ((mlen
-= 2) >= 0) {
300 s_util
.c
[1] = *(char *)w
;
305 } else if (mlen
== -1)
306 s_util
.c
[0] = *(char *)w
;
309 panic("in6_cksum: out of data");
311 /* The last mbuf has odd # of bytes. Follow the
312 standard (the odd byte may be shifted left by 8 bits
313 or not as determined by endian-ness of the machine) */
318 return (~sum
& 0xffff);