]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/in6_cksum.c
xnu-6153.11.26.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_cksum.c
CommitLineData
b0d623f7 1/*
39236c6e 2 * Copyright (c) 2009-2012 Apple Inc. All rights reserved.
b0d623f7
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
b0d623f7
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
b0d623f7
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
b0d623f7
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
b0d623f7
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
1c79356b
A
28
29/*
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58/*
59 * Copyright (c) 1988, 1992, 1993
60 * The Regents of the University of California. All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
64 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Berkeley and its contributors.
74 * 4. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
91 */
92
b0d623f7
A
93/*-
94 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
95 * All rights reserved.
96 *
97 * Redistribution and use in source and binary forms, with or without
98 * modification, are permitted provided that the following conditions
99 * are met:
100 *
101 * 1. Redistributions of source code must retain the above copyright
102 * notice, this list of conditions and the following disclaimer.
103 * 2. Redistributions in binary form must reproduce the above copyright
104 * notice, this list of conditions and the following disclaimer in
105 * the documentation and/or other materials provided with the
106 * distribution.
107 *
108 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
109 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
110 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
111 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
112 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
113 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
114 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
115 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
116 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
117 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
118 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
119 * SUCH DAMAGE.
120 */
121
1c79356b 122#include <sys/param.h>
39236c6e 123#include <machine/endian.h>
1c79356b
A
124#include <sys/mbuf.h>
125#include <sys/systm.h>
2d21ac55 126#include <kern/debug.h>
1c79356b
A
127#include <netinet/in.h>
128#include <netinet/ip6.h>
39236c6e 129#include <netinet6/ip6_var.h>
b0d623f7 130
1c79356b
A
131/*
132 * Checksum routine for Internet Protocol family headers (Portable Version).
133 *
134 * This routine is very heavily used in the network
135 * code and should be modified for each CPU to be as fast as possible.
136 */
137
1c79356b 138/*
39236c6e 139 * Compute IPv6 pseudo-header checksum; assumes 16-bit aligned pointers.
1c79356b 140 */
39236c6e
A
141uint16_t
142in6_pseudo(const struct in6_addr *src, const struct in6_addr *dst, uint32_t x)
1c79356b 143{
39236c6e
A
144 uint32_t sum = 0;
145 const uint16_t *w;
1c79356b 146
1c79356b 147 /*
39236c6e 148 * IPv6 source address
1c79356b 149 */
39236c6e
A
150 w = (const uint16_t *)src;
151 sum += w[0];
0a7de745 152 if (!IN6_IS_SCOPE_EMBED(src)) {
39236c6e 153 sum += w[1];
0a7de745 154 }
39236c6e
A
155 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
156 sum += w[6]; sum += w[7];
157
1c79356b 158 /*
39236c6e 159 * IPv6 destination address
1c79356b 160 */
39236c6e
A
161 w = (const uint16_t *)dst;
162 sum += w[0];
0a7de745 163 if (!IN6_IS_SCOPE_EMBED(dst)) {
39236c6e 164 sum += w[1];
0a7de745 165 }
39236c6e
A
166 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
167 sum += w[6]; sum += w[7];
1c79356b
A
168
169 /*
39236c6e
A
170 * Caller-supplied value; 'x' could be one of:
171 *
172 * htonl(proto + length), or
173 * htonl(proto + length + sum)
174 **/
175 sum += x;
176
177 /* fold in carry bits */
178 ADDCARRY(sum);
179
0a7de745 180 return sum;
39236c6e
A
181}
182
183/*
184 * m MUST contain at least an IPv6 header, if nxt is specified;
185 * nxt is the upper layer protocol number;
186 * off is an offset where TCP/UDP/ICMP6 header starts;
187 * len is a total length of a transport segment (e.g. TCP header + TCP payload)
188 */
189u_int16_t
190inet6_cksum(struct mbuf *m, uint32_t nxt, uint32_t off, uint32_t len)
191{
192 uint32_t sum;
193
194 sum = m_sum16(m, off, len);
195
196 if (nxt != 0) {
197 struct ip6_hdr *ip6;
0a7de745 198 unsigned char buf[sizeof(*ip6)] __attribute__((aligned(8)));
39236c6e 199 uint32_t mlen;
2d21ac55 200
1c79356b 201 /*
39236c6e
A
202 * Sanity check
203 *
204 * Use m_length2() instead of m_length(), as we cannot rely on
205 * the caller setting m_pkthdr.len correctly, if the mbuf is
206 * a M_PKTHDR one.
1c79356b 207 */
0a7de745 208 if ((mlen = m_length2(m, NULL)) < sizeof(*ip6)) {
39236c6e
A
209 panic("%s: mbuf %p pkt too short (%d) for IPv6 header",
210 __func__, m, mlen);
211 /* NOTREACHED */
1c79356b 212 }
39236c6e 213
1c79356b 214 /*
39236c6e
A
215 * In case the IPv6 header is not contiguous, or not 32-bit
216 * aligned, copy it to a local buffer. Note here that we
217 * expect the data pointer to point to the IPv6 header.
1c79356b 218 */
0a7de745 219 if ((sizeof(*ip6) > m->m_len) ||
39236c6e 220 !IP6_HDR_ALIGNED_P(mtod(m, caddr_t))) {
0a7de745 221 m_copydata(m, 0, sizeof(*ip6), (caddr_t)buf);
39236c6e
A
222 ip6 = (struct ip6_hdr *)(void *)buf;
223 } else {
224 ip6 = (struct ip6_hdr *)(void *)(m->m_data);
1c79356b 225 }
39236c6e
A
226
227 /* add pseudo header checksum */
228 sum += in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst,
229 htonl(nxt + len));
230
231 /* fold in carry bits */
232 ADDCARRY(sum);
1c79356b 233 }
39236c6e 234
0a7de745 235 return ~sum & 0xffff;
1c79356b 236}
5ba3f43e
A
237
238/*
239 * buffer MUST contain at least an IPv6 header, if nxt is specified;
240 * nxt is the upper layer protocol number;
241 * off is an offset where TCP/UDP/ICMP6 header starts;
242 * len is a total length of a transport segment (e.g. TCP header + TCP payload)
243 */
244u_int16_t
245inet6_cksum_buffer(const uint8_t *buffer, uint32_t nxt, uint32_t off,
246 uint32_t len)
247{
248 uint32_t sum;
249
0a7de745 250 if (off >= len) {
5ba3f43e 251 panic("%s: off (%d) >= len (%d)", __func__, off, len);
0a7de745 252 }
5ba3f43e
A
253
254 sum = b_sum16(&((const uint8_t *)buffer)[off], len);
255
256 if (nxt != 0) {
257 const struct ip6_hdr *ip6;
0a7de745 258 unsigned char buf[sizeof(*ip6)] __attribute__((aligned(8)));
5ba3f43e
A
259
260 /*
261 * In case the IPv6 header is not contiguous, or not 32-bit
262 * aligned, copy it to a local buffer. Note here that we
263 * expect the data pointer to point to the IPv6 header.
264 */
265 if (!IP6_HDR_ALIGNED_P(buffer)) {
0a7de745 266 memcpy(buf, buffer, sizeof(*ip6));
5ba3f43e
A
267 ip6 = (const struct ip6_hdr *)(const void *)buf;
268 } else {
269 ip6 = (const struct ip6_hdr *)buffer;
270 }
271
272 /* add pseudo header checksum */
273 sum += in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst,
274 htonl(nxt + len));
275
276 /* fold in carry bits */
277 ADDCARRY(sum);
278 }
279
0a7de745 280 return ~sum & 0xffff;
5ba3f43e 281}