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