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