Fix a netmsg memory leak in the ARP code. Adjust all ms_cmd function
[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.2 2003/06/17 04:28:52 dillon 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(m, nxt, off, len)
96         struct mbuf *m;
97         u_int8_t nxt;
98         u_int32_t off, len;
99 {
100         u_int16_t *w;
101         int sum = 0;
102         int mlen = 0;
103         int byte_swapped = 0;
104 #if 0
105         int srcifid = 0, dstifid = 0;
106 #endif
107         struct ip6_hdr *ip6;
108         union {
109                 u_int16_t phs[4];
110                 struct {
111                         u_int32_t       ph_len;
112                         u_int8_t        ph_zero[3];
113                         u_int8_t        ph_nxt;
114                 } ph __attribute__((__packed__));
115         } uph;
116         union {
117                 u_int8_t        c[2];
118                 u_int16_t       s;
119         } s_util;
120         union {
121                 u_int16_t s[2];
122                 u_int32_t l;
123         } l_util;
124
125         /* sanity check */
126         if (m->m_pkthdr.len < off + len) {
127                 panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
128                         m->m_pkthdr.len, off, len);
129         }
130
131         bzero(&uph, sizeof(uph));
132
133         /*
134          * First create IP6 pseudo header and calculate a summary.
135          */
136         ip6 = mtod(m, struct ip6_hdr *);
137 #if 0
138         if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
139                 srcifid = ip6->ip6_src.s6_addr16[1];
140                 ip6->ip6_src.s6_addr16[1] = 0;
141         }
142         if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
143                 dstifid = ip6->ip6_dst.s6_addr16[1];
144                 ip6->ip6_dst.s6_addr16[1] = 0;
145         }
146 #endif
147         w = (u_int16_t *)&ip6->ip6_src;
148         uph.ph.ph_len = htonl(len);
149         uph.ph.ph_nxt = nxt;
150
151         /* IPv6 source address */
152         sum += w[0];
153         if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
154                 sum += w[1];
155         sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
156         sum += w[6]; sum += w[7];
157         /* IPv6 destination address */
158         sum += w[8];
159         if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
160                 sum += w[9];
161         sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13];
162         sum += w[14]; sum += w[15];
163         /* Payload length and upper layer identifier */
164         sum += uph.phs[0];  sum += uph.phs[1];
165         sum += uph.phs[2];  sum += uph.phs[3];
166
167 #if 0
168         if (srcifid)
169                 ip6->ip6_src.s6_addr16[1] = srcifid;
170         if (dstifid)
171                 ip6->ip6_dst.s6_addr16[1] = dstifid;
172 #endif
173         /*
174          * Secondly calculate a summary of the first mbuf excluding offset.
175          */
176         while (m != NULL && off > 0) {
177                 if (m->m_len <= off)
178                         off -= m->m_len;
179                 else
180                         break;
181                 m = m->m_next;
182         }
183         w = (u_int16_t *)(mtod(m, u_char *) + off);
184         mlen = m->m_len - off;
185         if (len < mlen)
186                 mlen = len;
187         len -= mlen;
188         /*
189          * Force to even boundary.
190          */
191         if ((1 & (long) w) && (mlen > 0)) {
192                 REDUCE;
193                 sum <<= 8;
194                 s_util.c[0] = *(u_char *)w;
195                 w = (u_int16_t *)((char *)w + 1);
196                 mlen--;
197                 byte_swapped = 1;
198         }
199         /*
200          * Unroll the loop to make overhead from
201          * branches &c small.
202          */
203         while ((mlen -= 32) >= 0) {
204                 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
205                 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
206                 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
207                 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
208                 w += 16;
209         }
210         mlen += 32;
211         while ((mlen -= 8) >= 0) {
212                 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
213                 w += 4;
214         }
215         mlen += 8;
216         if (mlen == 0 && byte_swapped == 0)
217                 goto next;
218         REDUCE;
219         while ((mlen -= 2) >= 0) {
220                 sum += *w++;
221         }
222         if (byte_swapped) {
223                 REDUCE;
224                 sum <<= 8;
225                 byte_swapped = 0;
226                 if (mlen == -1) {
227                         s_util.c[1] = *(char *)w;
228                         sum += s_util.s;
229                         mlen = 0;
230                 } else
231                         mlen = -1;
232         } else if (mlen == -1)
233                 s_util.c[0] = *(char *)w;
234  next:
235         m = m->m_next;
236
237         /*
238          * Lastly calculate a summary of the rest of mbufs.
239          */
240
241         for (;m && len; m = m->m_next) {
242                 if (m->m_len == 0)
243                         continue;
244                 w = mtod(m, u_int16_t *);
245                 if (mlen == -1) {
246                         /*
247                          * The first byte of this mbuf is the continuation
248                          * of a word spanning between this mbuf and the
249                          * last mbuf.
250                          *
251                          * s_util.c[0] is already saved when scanning previous
252                          * mbuf.
253                          */
254                         s_util.c[1] = *(char *)w;
255                         sum += s_util.s;
256                         w = (u_int16_t *)((char *)w + 1);
257                         mlen = m->m_len - 1;
258                         len--;
259                 } else
260                         mlen = m->m_len;
261                 if (len < mlen)
262                         mlen = len;
263                 len -= mlen;
264                 /*
265                  * Force to even boundary.
266                  */
267                 if ((1 & (long) w) && (mlen > 0)) {
268                         REDUCE;
269                         sum <<= 8;
270                         s_util.c[0] = *(u_char *)w;
271                         w = (u_int16_t *)((char *)w + 1);
272                         mlen--;
273                         byte_swapped = 1;
274                 }
275                 /*
276                  * Unroll the loop to make overhead from
277                  * branches &c small.
278                  */
279                 while ((mlen -= 32) >= 0) {
280                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
281                         sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
282                         sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
283                         sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
284                         w += 16;
285                 }
286                 mlen += 32;
287                 while ((mlen -= 8) >= 0) {
288                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
289                         w += 4;
290                 }
291                 mlen += 8;
292                 if (mlen == 0 && byte_swapped == 0)
293                         continue;
294                 REDUCE;
295                 while ((mlen -= 2) >= 0) {
296                         sum += *w++;
297                 }
298                 if (byte_swapped) {
299                         REDUCE;
300                         sum <<= 8;
301                         byte_swapped = 0;
302                         if (mlen == -1) {
303                                 s_util.c[1] = *(char *)w;
304                                 sum += s_util.s;
305                                 mlen = 0;
306                         } else
307                                 mlen = -1;
308                 } else if (mlen == -1)
309                         s_util.c[0] = *(char *)w;
310         }
311         if (len)
312                 panic("in6_cksum: out of data");
313         if (mlen == -1) {
314                 /* The last mbuf has odd # of bytes. Follow the
315                    standard (the odd byte may be shifted left by 8 bits
316                    or not as determined by endian-ness of the machine) */
317                 s_util.c[1] = 0;
318                 sum += s_util.s;
319         }
320         REDUCE;
321         return (~sum & 0xffff);
322 }