in6_cksum.c revision 1.16 1 1.16 agc /* $NetBSD: in6_cksum.c,v 1.16 2003/08/07 16:33:24 agc Exp $ */
2 1.9 itojun /* $KAME: in6_cksum.c,v 1.9 2000/09/09 15:33:31 itojun Exp $ */
3 1.3 thorpej
4 1.2 itojun /*
5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.2 itojun * All rights reserved.
7 1.8 itojun *
8 1.2 itojun * Redistribution and use in source and binary forms, with or without
9 1.2 itojun * modification, are permitted provided that the following conditions
10 1.2 itojun * are met:
11 1.2 itojun * 1. Redistributions of source code must retain the above copyright
12 1.2 itojun * notice, this list of conditions and the following disclaimer.
13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 itojun * notice, this list of conditions and the following disclaimer in the
15 1.2 itojun * documentation and/or other materials provided with the distribution.
16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.2 itojun * may be used to endorse or promote products derived from this software
18 1.2 itojun * without specific prior written permission.
19 1.8 itojun *
20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2 itojun * SUCH DAMAGE.
31 1.2 itojun */
32 1.2 itojun
33 1.2 itojun /*
34 1.2 itojun * Copyright (c) 1988, 1992, 1993
35 1.2 itojun * The Regents of the University of California. All rights reserved.
36 1.2 itojun *
37 1.2 itojun * Redistribution and use in source and binary forms, with or without
38 1.2 itojun * modification, are permitted provided that the following conditions
39 1.2 itojun * are met:
40 1.2 itojun * 1. Redistributions of source code must retain the above copyright
41 1.2 itojun * notice, this list of conditions and the following disclaimer.
42 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
43 1.2 itojun * notice, this list of conditions and the following disclaimer in the
44 1.2 itojun * documentation and/or other materials provided with the distribution.
45 1.16 agc * 3. Neither the name of the University nor the names of its contributors
46 1.2 itojun * may be used to endorse or promote products derived from this software
47 1.2 itojun * without specific prior written permission.
48 1.2 itojun *
49 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.2 itojun * SUCH DAMAGE.
60 1.2 itojun *
61 1.2 itojun * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
62 1.2 itojun */
63 1.12 lukem
64 1.12 lukem #include <sys/cdefs.h>
65 1.16 agc __KERNEL_RCSID(0, "$NetBSD: in6_cksum.c,v 1.16 2003/08/07 16:33:24 agc Exp $");
66 1.2 itojun
67 1.2 itojun #include <sys/param.h>
68 1.2 itojun #include <sys/mbuf.h>
69 1.2 itojun #include <sys/systm.h>
70 1.2 itojun #include <netinet/in.h>
71 1.7 itojun #include <netinet/ip6.h>
72 1.2 itojun
73 1.6 itojun #include <net/net_osdep.h>
74 1.6 itojun
75 1.2 itojun /*
76 1.2 itojun * Checksum routine for Internet Protocol family headers (Portable Version).
77 1.2 itojun *
78 1.2 itojun * This routine is very heavily used in the network
79 1.2 itojun * code and should be modified for each CPU to be as fast as possible.
80 1.2 itojun */
81 1.2 itojun
82 1.2 itojun #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
83 1.2 itojun #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
84 1.2 itojun
85 1.2 itojun /*
86 1.2 itojun * m MUST contain a continuous IP6 header.
87 1.2 itojun * off is a offset where TCP/UDP/ICMP6 header starts.
88 1.2 itojun * len is a total length of a transport segment.
89 1.2 itojun * (e.g. TCP header + TCP payload)
90 1.2 itojun */
91 1.2 itojun
92 1.2 itojun int
93 1.2 itojun in6_cksum(m, nxt, off, len)
94 1.10 itojun struct mbuf *m;
95 1.2 itojun u_int8_t nxt;
96 1.6 itojun u_int32_t off, len;
97 1.2 itojun {
98 1.10 itojun u_int16_t *w;
99 1.10 itojun int sum = 0;
100 1.10 itojun int mlen = 0;
101 1.2 itojun int byte_swapped = 0;
102 1.13 itojun struct ip6_hdr *ip6;
103 1.9 itojun union {
104 1.9 itojun u_int16_t phs[4];
105 1.9 itojun struct {
106 1.9 itojun u_int32_t ph_len;
107 1.9 itojun u_int8_t ph_zero[3];
108 1.9 itojun u_int8_t ph_nxt;
109 1.9 itojun } ph __attribute__((__packed__));
110 1.9 itojun } uph;
111 1.2 itojun union {
112 1.5 itojun u_int8_t c[2];
113 1.5 itojun u_int16_t s;
114 1.2 itojun } s_util;
115 1.2 itojun union {
116 1.5 itojun u_int16_t s[2];
117 1.5 itojun u_int32_t l;
118 1.2 itojun } l_util;
119 1.2 itojun
120 1.2 itojun /* sanity check */
121 1.2 itojun if (m->m_pkthdr.len < off + len) {
122 1.14 provos panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
123 1.2 itojun m->m_pkthdr.len, off, len);
124 1.2 itojun }
125 1.9 itojun
126 1.11 thorpej /* Skip pseudo-header if nxt == 0. */
127 1.11 thorpej if (nxt == 0)
128 1.11 thorpej goto skip_phdr;
129 1.11 thorpej
130 1.9 itojun bzero(&uph, sizeof(uph));
131 1.2 itojun
132 1.2 itojun /*
133 1.2 itojun * First create IP6 pseudo header and calculate a summary.
134 1.2 itojun */
135 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
136 1.5 itojun w = (u_int16_t *)&ip6->ip6_src;
137 1.2 itojun uph.ph.ph_len = htonl(len);
138 1.2 itojun uph.ph.ph_nxt = nxt;
139 1.2 itojun
140 1.2 itojun /* IPv6 source address */
141 1.2 itojun sum += w[0];
142 1.2 itojun if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
143 1.2 itojun sum += w[1];
144 1.8 itojun sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
145 1.2 itojun sum += w[6]; sum += w[7];
146 1.2 itojun /* IPv6 destination address */
147 1.2 itojun sum += w[8];
148 1.2 itojun if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
149 1.2 itojun sum += w[9];
150 1.2 itojun sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13];
151 1.2 itojun sum += w[14]; sum += w[15];
152 1.2 itojun /* Payload length and upper layer identifier */
153 1.2 itojun sum += uph.phs[0]; sum += uph.phs[1];
154 1.2 itojun sum += uph.phs[2]; sum += uph.phs[3];
155 1.2 itojun
156 1.11 thorpej skip_phdr:
157 1.2 itojun /*
158 1.2 itojun * Secondly calculate a summary of the first mbuf excluding offset.
159 1.2 itojun */
160 1.2 itojun while (m != NULL && off > 0) {
161 1.2 itojun if (m->m_len <= off)
162 1.2 itojun off -= m->m_len;
163 1.2 itojun else
164 1.2 itojun break;
165 1.2 itojun m = m->m_next;
166 1.2 itojun }
167 1.5 itojun w = (u_int16_t *)(mtod(m, u_char *) + off);
168 1.2 itojun mlen = m->m_len - off;
169 1.2 itojun if (len < mlen)
170 1.2 itojun mlen = len;
171 1.2 itojun len -= mlen;
172 1.2 itojun /*
173 1.2 itojun * Force to even boundary.
174 1.2 itojun */
175 1.4 itojun if ((1 & (long) w) && (mlen > 0)) {
176 1.2 itojun REDUCE;
177 1.2 itojun sum <<= 8;
178 1.2 itojun s_util.c[0] = *(u_char *)w;
179 1.5 itojun w = (u_int16_t *)((char *)w + 1);
180 1.2 itojun mlen--;
181 1.2 itojun byte_swapped = 1;
182 1.2 itojun }
183 1.2 itojun /*
184 1.2 itojun * Unroll the loop to make overhead from
185 1.2 itojun * branches &c small.
186 1.2 itojun */
187 1.2 itojun while ((mlen -= 32) >= 0) {
188 1.2 itojun sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
189 1.2 itojun sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
190 1.2 itojun sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
191 1.2 itojun sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
192 1.2 itojun w += 16;
193 1.2 itojun }
194 1.2 itojun mlen += 32;
195 1.2 itojun while ((mlen -= 8) >= 0) {
196 1.2 itojun sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
197 1.2 itojun w += 4;
198 1.2 itojun }
199 1.2 itojun mlen += 8;
200 1.2 itojun if (mlen == 0 && byte_swapped == 0)
201 1.2 itojun goto next;
202 1.2 itojun REDUCE;
203 1.2 itojun while ((mlen -= 2) >= 0) {
204 1.2 itojun sum += *w++;
205 1.2 itojun }
206 1.2 itojun if (byte_swapped) {
207 1.2 itojun REDUCE;
208 1.2 itojun sum <<= 8;
209 1.2 itojun byte_swapped = 0;
210 1.2 itojun if (mlen == -1) {
211 1.2 itojun s_util.c[1] = *(char *)w;
212 1.2 itojun sum += s_util.s;
213 1.2 itojun mlen = 0;
214 1.2 itojun } else
215 1.2 itojun mlen = -1;
216 1.2 itojun } else if (mlen == -1)
217 1.2 itojun s_util.c[0] = *(char *)w;
218 1.2 itojun next:
219 1.2 itojun m = m->m_next;
220 1.2 itojun
221 1.2 itojun /*
222 1.2 itojun * Lastly calculate a summary of the rest of mbufs.
223 1.2 itojun */
224 1.13 itojun
225 1.2 itojun for (;m && len; m = m->m_next) {
226 1.2 itojun if (m->m_len == 0)
227 1.2 itojun continue;
228 1.5 itojun w = mtod(m, u_int16_t *);
229 1.2 itojun if (mlen == -1) {
230 1.2 itojun /*
231 1.2 itojun * The first byte of this mbuf is the continuation
232 1.2 itojun * of a word spanning between this mbuf and the
233 1.2 itojun * last mbuf.
234 1.2 itojun *
235 1.8 itojun * s_util.c[0] is already saved when scanning previous
236 1.2 itojun * mbuf.
237 1.2 itojun */
238 1.2 itojun s_util.c[1] = *(char *)w;
239 1.2 itojun sum += s_util.s;
240 1.5 itojun w = (u_int16_t *)((char *)w + 1);
241 1.2 itojun mlen = m->m_len - 1;
242 1.2 itojun len--;
243 1.2 itojun } else
244 1.2 itojun mlen = m->m_len;
245 1.2 itojun if (len < mlen)
246 1.2 itojun mlen = len;
247 1.2 itojun len -= mlen;
248 1.2 itojun /*
249 1.2 itojun * Force to even boundary.
250 1.2 itojun */
251 1.4 itojun if ((1 & (long) w) && (mlen > 0)) {
252 1.2 itojun REDUCE;
253 1.2 itojun sum <<= 8;
254 1.2 itojun s_util.c[0] = *(u_char *)w;
255 1.5 itojun w = (u_int16_t *)((char *)w + 1);
256 1.2 itojun mlen--;
257 1.2 itojun byte_swapped = 1;
258 1.2 itojun }
259 1.2 itojun /*
260 1.2 itojun * Unroll the loop to make overhead from
261 1.2 itojun * branches &c small.
262 1.2 itojun */
263 1.2 itojun while ((mlen -= 32) >= 0) {
264 1.2 itojun sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
265 1.2 itojun sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
266 1.2 itojun sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
267 1.2 itojun sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
268 1.2 itojun w += 16;
269 1.2 itojun }
270 1.2 itojun mlen += 32;
271 1.2 itojun while ((mlen -= 8) >= 0) {
272 1.2 itojun sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
273 1.2 itojun w += 4;
274 1.2 itojun }
275 1.2 itojun mlen += 8;
276 1.2 itojun if (mlen == 0 && byte_swapped == 0)
277 1.2 itojun continue;
278 1.2 itojun REDUCE;
279 1.2 itojun while ((mlen -= 2) >= 0) {
280 1.2 itojun sum += *w++;
281 1.2 itojun }
282 1.2 itojun if (byte_swapped) {
283 1.2 itojun REDUCE;
284 1.2 itojun sum <<= 8;
285 1.2 itojun byte_swapped = 0;
286 1.2 itojun if (mlen == -1) {
287 1.2 itojun s_util.c[1] = *(char *)w;
288 1.2 itojun sum += s_util.s;
289 1.2 itojun mlen = 0;
290 1.2 itojun } else
291 1.2 itojun mlen = -1;
292 1.2 itojun } else if (mlen == -1)
293 1.2 itojun s_util.c[0] = *(char *)w;
294 1.2 itojun }
295 1.2 itojun if (len)
296 1.14 provos panic("in6_cksum: out of data");
297 1.2 itojun if (mlen == -1) {
298 1.2 itojun /* The last mbuf has odd # of bytes. Follow the
299 1.2 itojun standard (the odd byte may be shifted left by 8 bits
300 1.2 itojun or not as determined by endian-ness of the machine) */
301 1.2 itojun s_util.c[1] = 0;
302 1.2 itojun sum += s_util.s;
303 1.2 itojun }
304 1.2 itojun REDUCE;
305 1.2 itojun return (~sum & 0xffff);
306 1.2 itojun }
307