Merge from vendor branch GCC:
[dragonfly.git] / sys / netinet6 / in6_cksum.c
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.3 2004/05/20 18:30:36 cpressey Exp $       */
3 /*      $KAME: in6_cksum.c,v 1.10 2000/12/03 00:53:59 itojun Exp $      */
4
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 1988, 1992, 1993
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
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.
53  *
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
64  * SUCH DAMAGE.
65  *
66  *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
67  */
68
69 #include <sys/param.h>
70 #include <sys/mbuf.h>
71 #include <sys/systm.h>
72 #include <netinet/in.h>
73 #include <netinet/ip6.h>
74
75 #include <net/net_osdep.h>
76
77 /*
78  * Checksum routine for Internet Protocol family headers (Portable Version).
79  *
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.
82  */
83
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);}
86
87 /*
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)
92  */
93
94 int
95 in6_cksum(struct mbuf *m, u_int8_t nxt, u_int32_t off, u_int32_t len)
96 {
97         u_int16_t *w;
98         int sum = 0;
99         int mlen = 0;
100         int byte_swapped = 0;
101 #if 0
102         int srcifid = 0, dstifid = 0;
103 #endif
104         struct ip6_hdr *ip6;
105         union {
106                 u_int16_t phs[4];
107                 struct {
108                         u_int32_t       ph_len;
109                         u_int8_t        ph_zero[3];
110                         u_int8_t        ph_nxt;
111                 } ph __attribute__((__packed__));
112         } uph;
113         union {
114                 u_int8_t        c[2];
115                 u_int16_t       s;
116         } s_util;
117         union {
118                 u_int16_t s[2];
119                 u_int32_t l;
120         } l_util;
121
122         /* sanity check */
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);
126         }
127
128         bzero(&uph, sizeof(uph));
129
130         /*
131          * First create IP6 pseudo header and calculate a summary.
132          */
133         ip6 = mtod(m, struct ip6_hdr *);
134 #if 0
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;
138         }
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;
142         }
143 #endif
144         w = (u_int16_t *)&ip6->ip6_src;
145         uph.ph.ph_len = htonl(len);
146         uph.ph.ph_nxt = nxt;
147
148         /* IPv6 source address */
149         sum += w[0];
150         if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
151                 sum += w[1];
152         sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
153         sum += w[6]; sum += w[7];
154         /* IPv6 destination address */
155         sum += w[8];
156         if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
157                 sum += w[9];
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];
163
164 #if 0
165         if (srcifid)
166                 ip6->ip6_src.s6_addr16[1] = srcifid;
167         if (dstifid)
168                 ip6->ip6_dst.s6_addr16[1] = dstifid;
169 #endif
170         /*
171          * Secondly calculate a summary of the first mbuf excluding offset.
172          */
173         while (m != NULL && off > 0) {
174                 if (m->m_len <= off)
175                         off -= m->m_len;
176                 else
177                         break;
178                 m = m->m_next;
179         }
180         w = (u_int16_t *)(mtod(m, u_char *) + off);
181         mlen = m->m_len - off;
182         if (len < mlen)
183                 mlen = len;
184         len -= mlen;
185         /*
186          * Force to even boundary.
187          */
188         if ((1 & (long) w) && (mlen > 0)) {
189                 REDUCE;
190                 sum <<= 8;
191                 s_util.c[0] = *(u_char *)w;
192                 w = (u_int16_t *)((char *)w + 1);
193                 mlen--;
194                 byte_swapped = 1;
195         }
196         /*
197          * Unroll the loop to make overhead from
198          * branches &c small.
199          */
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];
205                 w += 16;
206         }
207         mlen += 32;
208         while ((mlen -= 8) >= 0) {
209                 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
210                 w += 4;
211         }
212         mlen += 8;
213         if (mlen == 0 && byte_swapped == 0)
214                 goto next;
215         REDUCE;
216         while ((mlen -= 2) >= 0) {
217                 sum += *w++;
218         }
219         if (byte_swapped) {
220                 REDUCE;
221                 sum <<= 8;
222                 byte_swapped = 0;
223                 if (mlen == -1) {
224                         s_util.c[1] = *(char *)w;
225                         sum += s_util.s;
226                         mlen = 0;
227                 } else
228                         mlen = -1;
229         } else if (mlen == -1)
230                 s_util.c[0] = *(char *)w;
231  next:
232         m = m->m_next;
233
234         /*
235          * Lastly calculate a summary of the rest of mbufs.
236          */
237
238         for (;m && len; m = m->m_next) {
239                 if (m->m_len == 0)
240                         continue;
241                 w = mtod(m, u_int16_t *);
242                 if (mlen == -1) {
243                         /*
244                          * The first byte of this mbuf is the continuation
245                          * of a word spanning between this mbuf and the
246                          * last mbuf.
247                          *
248                          * s_util.c[0] is already saved when scanning previous
249                          * mbuf.
250                          */
251                         s_util.c[1] = *(char *)w;
252                         sum += s_util.s;
253                         w = (u_int16_t *)((char *)w + 1);
254                         mlen = m->m_len - 1;
255                         len--;
256                 } else
257                         mlen = m->m_len;
258                 if (len < mlen)
259                         mlen = len;
260                 len -= mlen;
261                 /*
262                  * Force to even boundary.
263                  */
264                 if ((1 & (long) w) && (mlen > 0)) {
265                         REDUCE;
266                         sum <<= 8;
267                         s_util.c[0] = *(u_char *)w;
268                         w = (u_int16_t *)((char *)w + 1);
269                         mlen--;
270                         byte_swapped = 1;
271                 }
272                 /*
273                  * Unroll the loop to make overhead from
274                  * branches &c small.
275                  */
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];
281                         w += 16;
282                 }
283                 mlen += 32;
284                 while ((mlen -= 8) >= 0) {
285                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
286                         w += 4;
287                 }
288                 mlen += 8;
289                 if (mlen == 0 && byte_swapped == 0)
290                         continue;
291                 REDUCE;
292                 while ((mlen -= 2) >= 0) {
293                         sum += *w++;
294                 }
295                 if (byte_swapped) {
296                         REDUCE;
297                         sum <<= 8;
298                         byte_swapped = 0;
299                         if (mlen == -1) {
300                                 s_util.c[1] = *(char *)w;
301                                 sum += s_util.s;
302                                 mlen = 0;
303                         } else
304                                 mlen = -1;
305                 } else if (mlen == -1)
306                         s_util.c[0] = *(char *)w;
307         }
308         if (len)
309                 panic("in6_cksum: out of data");
310         if (mlen == -1) {
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) */
314                 s_util.c[1] = 0;
315                 sum += s_util.s;
316         }
317         REDUCE;
318         return (~sum & 0xffff);
319 }