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