in6_cksum.c revision 1.14 1 /* $NetBSD: in6_cksum.c,v 1.14 2002/09/27 15:37:53 provos Exp $ */
2 /* $KAME: in6_cksum.c,v 1.9 2000/09/09 15:33:31 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1988, 1992, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
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.
52 *
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
63 * SUCH DAMAGE.
64 *
65 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: in6_cksum.c,v 1.14 2002/09/27 15:37:53 provos Exp $");
70
71 #include <sys/param.h>
72 #include <sys/mbuf.h>
73 #include <sys/systm.h>
74 #include <netinet/in.h>
75 #include <netinet/ip6.h>
76
77 #include <net/net_osdep.h>
78
79 /*
80 * Checksum routine for Internet Protocol family headers (Portable Version).
81 *
82 * This routine is very heavily used in the network
83 * code and should be modified for each CPU to be as fast as possible.
84 */
85
86 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
87 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
88
89 /*
90 * m MUST contain a continuous IP6 header.
91 * off is a offset where TCP/UDP/ICMP6 header starts.
92 * len is a total length of a transport segment.
93 * (e.g. TCP header + TCP payload)
94 */
95
96 int
97 in6_cksum(m, nxt, off, len)
98 struct mbuf *m;
99 u_int8_t nxt;
100 u_int32_t off, len;
101 {
102 u_int16_t *w;
103 int sum = 0;
104 int mlen = 0;
105 int byte_swapped = 0;
106 #if 0
107 int srcifid = 0, dstifid = 0;
108 #endif
109 struct ip6_hdr *ip6;
110 union {
111 u_int16_t phs[4];
112 struct {
113 u_int32_t ph_len;
114 u_int8_t ph_zero[3];
115 u_int8_t ph_nxt;
116 } ph __attribute__((__packed__));
117 } uph;
118 union {
119 u_int8_t c[2];
120 u_int16_t s;
121 } s_util;
122 union {
123 u_int16_t s[2];
124 u_int32_t l;
125 } l_util;
126
127 /* sanity check */
128 if (m->m_pkthdr.len < off + len) {
129 panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
130 m->m_pkthdr.len, off, len);
131 }
132
133 /* Skip pseudo-header if nxt == 0. */
134 if (nxt == 0)
135 goto skip_phdr;
136
137 bzero(&uph, sizeof(uph));
138
139 /*
140 * First create IP6 pseudo header and calculate a summary.
141 */
142 ip6 = mtod(m, struct ip6_hdr *);
143 #if 0
144 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
145 srcifid = ip6->ip6_src.s6_addr16[1];
146 ip6->ip6_src.s6_addr16[1] = 0;
147 }
148 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
149 dstifid = ip6->ip6_dst.s6_addr16[1];
150 ip6->ip6_dst.s6_addr16[1] = 0;
151 }
152 #endif
153 w = (u_int16_t *)&ip6->ip6_src;
154 uph.ph.ph_len = htonl(len);
155 uph.ph.ph_nxt = nxt;
156
157 /* IPv6 source address */
158 sum += w[0];
159 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
160 sum += w[1];
161 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
162 sum += w[6]; sum += w[7];
163 /* IPv6 destination address */
164 sum += w[8];
165 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
166 sum += w[9];
167 sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13];
168 sum += w[14]; sum += w[15];
169 /* Payload length and upper layer identifier */
170 sum += uph.phs[0]; sum += uph.phs[1];
171 sum += uph.phs[2]; sum += uph.phs[3];
172
173 #if 0
174 if (srcifid)
175 ip6->ip6_src.s6_addr16[1] = srcifid;
176 if (dstifid)
177 ip6->ip6_dst.s6_addr16[1] = dstifid;
178 #endif
179 skip_phdr:
180 /*
181 * Secondly calculate a summary of the first mbuf excluding offset.
182 */
183 while (m != NULL && off > 0) {
184 if (m->m_len <= off)
185 off -= m->m_len;
186 else
187 break;
188 m = m->m_next;
189 }
190 w = (u_int16_t *)(mtod(m, u_char *) + off);
191 mlen = m->m_len - off;
192 if (len < mlen)
193 mlen = len;
194 len -= mlen;
195 /*
196 * Force to even boundary.
197 */
198 if ((1 & (long) w) && (mlen > 0)) {
199 REDUCE;
200 sum <<= 8;
201 s_util.c[0] = *(u_char *)w;
202 w = (u_int16_t *)((char *)w + 1);
203 mlen--;
204 byte_swapped = 1;
205 }
206 /*
207 * Unroll the loop to make overhead from
208 * branches &c small.
209 */
210 while ((mlen -= 32) >= 0) {
211 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
212 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
213 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
214 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
215 w += 16;
216 }
217 mlen += 32;
218 while ((mlen -= 8) >= 0) {
219 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
220 w += 4;
221 }
222 mlen += 8;
223 if (mlen == 0 && byte_swapped == 0)
224 goto next;
225 REDUCE;
226 while ((mlen -= 2) >= 0) {
227 sum += *w++;
228 }
229 if (byte_swapped) {
230 REDUCE;
231 sum <<= 8;
232 byte_swapped = 0;
233 if (mlen == -1) {
234 s_util.c[1] = *(char *)w;
235 sum += s_util.s;
236 mlen = 0;
237 } else
238 mlen = -1;
239 } else if (mlen == -1)
240 s_util.c[0] = *(char *)w;
241 next:
242 m = m->m_next;
243
244 /*
245 * Lastly calculate a summary of the rest of mbufs.
246 */
247
248 for (;m && len; m = m->m_next) {
249 if (m->m_len == 0)
250 continue;
251 w = mtod(m, u_int16_t *);
252 if (mlen == -1) {
253 /*
254 * The first byte of this mbuf is the continuation
255 * of a word spanning between this mbuf and the
256 * last mbuf.
257 *
258 * s_util.c[0] is already saved when scanning previous
259 * mbuf.
260 */
261 s_util.c[1] = *(char *)w;
262 sum += s_util.s;
263 w = (u_int16_t *)((char *)w + 1);
264 mlen = m->m_len - 1;
265 len--;
266 } else
267 mlen = m->m_len;
268 if (len < mlen)
269 mlen = len;
270 len -= mlen;
271 /*
272 * Force to even boundary.
273 */
274 if ((1 & (long) w) && (mlen > 0)) {
275 REDUCE;
276 sum <<= 8;
277 s_util.c[0] = *(u_char *)w;
278 w = (u_int16_t *)((char *)w + 1);
279 mlen--;
280 byte_swapped = 1;
281 }
282 /*
283 * Unroll the loop to make overhead from
284 * branches &c small.
285 */
286 while ((mlen -= 32) >= 0) {
287 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
288 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
289 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
290 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
291 w += 16;
292 }
293 mlen += 32;
294 while ((mlen -= 8) >= 0) {
295 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
296 w += 4;
297 }
298 mlen += 8;
299 if (mlen == 0 && byte_swapped == 0)
300 continue;
301 REDUCE;
302 while ((mlen -= 2) >= 0) {
303 sum += *w++;
304 }
305 if (byte_swapped) {
306 REDUCE;
307 sum <<= 8;
308 byte_swapped = 0;
309 if (mlen == -1) {
310 s_util.c[1] = *(char *)w;
311 sum += s_util.s;
312 mlen = 0;
313 } else
314 mlen = -1;
315 } else if (mlen == -1)
316 s_util.c[0] = *(char *)w;
317 }
318 if (len)
319 panic("in6_cksum: out of data");
320 if (mlen == -1) {
321 /* The last mbuf has odd # of bytes. Follow the
322 standard (the odd byte may be shifted left by 8 bits
323 or not as determined by endian-ness of the machine) */
324 s_util.c[1] = 0;
325 sum += s_util.s;
326 }
327 REDUCE;
328 return (~sum & 0xffff);
329 }
330