Use a normal malloc() for PCB allocations instead of the really aweful mbuf
[dragonfly.git] / sys / netproto / ns / ns_cksum.c
1 /*
2  * Copyright (c) 1982, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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  *      @(#)ns_cksum.c  8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/netns/ns_cksum.c,v 1.7 1999/08/28 00:49:49 peter Exp $
35  * $DragonFly: src/sys/netproto/ns/ns_cksum.c,v 1.4 2004/06/02 14:19:11 joerg Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/mbuf.h>
40 #include <sys/socketvar.h>
41 #include "ns.h"
42
43 /*
44  * Checksum routine for Network Systems Protocol Packets (Big-Endian).
45  *
46  * This routine is very heavily used in the network
47  * code and should be modified for each CPU to be as fast as possible.
48  */
49
50 #define ADDCARRY(x)  { if ((x) > 65535) (x) -= 65535; }
51 #define FOLD(x) {l_util.l = (x); (x) = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
52
53 u_short
54 ns_cksum(m, len)
55         struct mbuf *m;
56         int len;
57 {
58         u_short *w;
59         int sum = 0;
60         int mlen = 0;
61         int sum2;
62
63         union {
64                 u_short s[2];
65                 long    l;
66         } l_util;
67
68         for (;m && len; m = m->m_next) {
69                 if (m->m_len == 0)
70                         continue;
71                 /*
72                  * Each trip around loop adds in
73                  * word from one mbuf segment.
74                  */
75                 w = mtod(m, u_short *);
76                 if (mlen == -1) {
77                         /*
78                          * There is a byte left from the last segment;
79                          * ones-complement add it into the checksum.
80                          */
81 #if BYTE_ORDER == BIG_ENDIAN
82                         sum  += *(u_char *)w;
83 #else
84                         sum  += *(u_char *)w << 8;
85 #endif
86                         sum += sum;
87                         w = (u_short *)(1 + (char *)w);
88                         mlen = m->m_len - 1;
89                         len--;
90                         FOLD(sum);
91                 } else
92                         mlen = m->m_len;
93                 if (len < mlen)
94                         mlen = len;
95                 len -= mlen;
96                 /*
97                  * We can do a 16 bit ones complement sum using
98                  * 32 bit arithmetic registers for adding,
99                  * with carries from the low added
100                  * into the high (by normal carry-chaining)
101                  * so long as we fold back before 16 carries have occured.
102                  */
103                 if (1 & (int) w)
104                         goto uuuuglyy;
105 #ifndef TINY
106 /* -DTINY reduces the size from 1250 to 550, but slows it down by 22% */
107                 while ((mlen -= 32) >= 0) {
108                         sum += w[0]; sum += sum; sum += w[1]; sum += sum;
109                         sum += w[2]; sum += sum; sum += w[3]; sum += sum;
110                         sum += w[4]; sum += sum; sum += w[5]; sum += sum;
111                         sum += w[6]; sum += sum; sum += w[7]; sum += sum;
112                         FOLD(sum);
113                         sum += w[8]; sum += sum; sum += w[9]; sum += sum;
114                         sum += w[10]; sum += sum; sum += w[11]; sum += sum;
115                         sum += w[12]; sum += sum; sum += w[13]; sum += sum;
116                         sum += w[14]; sum += sum; sum += w[15]; sum += sum;
117                         FOLD(sum);
118                         w += 16;
119                 }
120                 mlen += 32;
121 #endif
122                 while ((mlen -= 8) >= 0) {
123                         sum += w[0]; sum += sum; sum += w[1]; sum += sum;
124                         sum += w[2]; sum += sum; sum += w[3]; sum += sum;
125                         FOLD(sum);
126                         w += 4;
127                 }
128                 mlen += 8;
129                 while ((mlen -= 2) >= 0) {
130                         sum += *w++; sum += sum;
131                 }
132                 goto commoncase;
133 uuuuglyy:
134 #if BYTE_ORDER == BIG_ENDIAN
135 #define ww(n) (((u_char *)w)[n + n + 1])
136 #define vv(n) (((u_char *)w)[n + n])
137 #else
138 #if BYTE_ORDER == LITTLE_ENDIAN
139 #define vv(n) (((u_char *)w)[n + n + 1])
140 #define ww(n) (((u_char *)w)[n + n])
141 #endif
142 #endif
143                 sum2 = 0;
144 #ifndef TINY
145                 while ((mlen -= 32) >= 0) {
146                     sum += ww(0); sum += sum; sum += ww(1); sum += sum;
147                     sum += ww(2); sum += sum; sum += ww(3); sum += sum;
148                     sum += ww(4); sum += sum; sum += ww(5); sum += sum;
149                     sum += ww(6); sum += sum; sum += ww(7); sum += sum;
150                     FOLD(sum);
151                     sum += ww(8); sum += sum; sum += ww(9); sum += sum;
152                     sum += ww(10); sum += sum; sum += ww(11); sum += sum;
153                     sum += ww(12); sum += sum; sum += ww(13); sum += sum;
154                     sum += ww(14); sum += sum; sum += ww(15); sum += sum;
155                     FOLD(sum);
156                     sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
157                     sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
158                     sum2 += vv(4); sum2 += sum2; sum2 += vv(5); sum2 += sum2;
159                     sum2 += vv(6); sum2 += sum2; sum2 += vv(7); sum2 += sum2;
160                     FOLD(sum2);
161                     sum2 += vv(8); sum2 += sum2; sum2 += vv(9); sum2 += sum2;
162                     sum2 += vv(10); sum2 += sum2; sum2 += vv(11); sum2 += sum2;
163                     sum2 += vv(12); sum2 += sum2; sum2 += vv(13); sum2 += sum2;
164                     sum2 += vv(14); sum2 += sum2; sum2 += vv(15); sum2 += sum2;
165                     FOLD(sum2);
166                     w += 16;
167                 }
168                 mlen += 32;
169 #endif
170                 while ((mlen -= 8) >= 0) {
171                     sum += ww(0); sum += sum; sum += ww(1); sum += sum;
172                     sum += ww(2); sum += sum; sum += ww(3); sum += sum;
173                     FOLD(sum);
174                     sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
175                     sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
176                     FOLD(sum2);
177                     w += 4;
178                 }
179                 mlen += 8;
180                 while ((mlen -= 2) >= 0) {
181                         sum += ww(0); sum += sum;
182                         sum2 += vv(0); sum2 += sum2;
183                         w++;
184                 }
185                 sum += (sum2 << 8);
186 commoncase:
187                 if (mlen == -1) {
188 #if BYTE_ORDER == BIG_ENDIAN
189                         sum += *(u_char *)w << 8;
190 #else
191                         sum += *(u_char *)w;
192 #endif
193                 }
194                 FOLD(sum);
195         }
196         if (mlen == -1) {
197                 /* We had an odd number of bytes to sum; assume a garbage
198                    byte of zero and clean up */
199                 sum += sum;
200                 FOLD(sum);
201         }
202         /*
203          * sum has already been kept to low sixteen bits.
204          * just examine result and exit.
205          */
206         if(sum==0xffff) sum = 0;
207         return (sum);
208 }