1/*
2 * Copyright (c) 2009-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
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
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
122#include <sys/param.h>
123#include <machine/endian.h>
124#include <sys/mbuf.h>
125#include <sys/systm.h>
126#include <kern/debug.h>
127#include <netinet/in.h>
128#include <netinet/ip6.h>
129#include <netinet6/ip6_var.h>
130
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
138/*
139 * Compute IPv6 pseudo-header checksum; assumes 16-bit aligned pointers.
140 */
141uint16_t
142in6_pseudo(const struct in6_addr *src, const struct in6_addr *dst, uint32_t x)
143{
144 uint32_t sum = 0;
145 const uint16_t *w;
146
147 /*
148 * IPv6 source address
149 */
150 w = (const uint16_t *)src;
151 sum += w[0];
152 if (!IN6_IS_SCOPE_EMBED(src)) {
153 sum += w[1];
154 }
155 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
156 sum += w[6]; sum += w[7];
157
158 /*
159 * IPv6 destination address
160 */
161 w = (const uint16_t *)dst;
162 sum += w[0];
163 if (!IN6_IS_SCOPE_EMBED(dst)) {
164 sum += w[1];
165 }
166 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
167 sum += w[6]; sum += w[7];
168
169 /*
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
180 return (uint16_t)sum;
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;
198 unsigned char buf[sizeof(*ip6)] __attribute__((aligned(8)));
199 uint32_t mlen;
200
201 /*
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.
207 */
208 if ((mlen = m_length2(m, NULL)) < sizeof(*ip6)) {
209 panic("%s: mbuf %p pkt too short (%d) for IPv6 header",
210 __func__, m, mlen);
211 /* NOTREACHED */
212 }
213
214 /*
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.
218 */
219 if ((sizeof(*ip6) > m->m_len) ||
220 !IP6_HDR_ALIGNED_P(mtod(m, caddr_t))) {
221 m_copydata(m, 0, sizeof(*ip6), (caddr_t)buf);
222 ip6 = (struct ip6_hdr *)(void *)buf;
223 } else {
224 ip6 = (struct ip6_hdr *)(void *)(m->m_data);
225 }
226
227 /* add pseudo header checksum */
228 sum += in6_pseudo(src: &ip6->ip6_src, dst: &ip6->ip6_dst,
229 htonl(nxt + len));
230
231 /* fold in carry bits */
232 ADDCARRY(sum);
233 }
234
235 return ~sum & 0xffff;
236}
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
250 if (off >= len) {
251 panic("%s: off (%d) >= len (%d)", __func__, off, len);
252 }
253
254 sum = b_sum16(buf: &((const uint8_t *)buffer)[off], len);
255
256 if (nxt != 0) {
257 const struct ip6_hdr *ip6;
258 unsigned char buf[sizeof(*ip6)] __attribute__((aligned(8)));
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)) {
266 memcpy(dst: buf, src: buffer, n: sizeof(*ip6));
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(src: &ip6->ip6_src, dst: &ip6->ip6_dst,
274 htonl(nxt + len));
275
276 /* fold in carry bits */
277 ADDCARRY(sum);
278 }
279
280 return ~sum & 0xffff;
281}
282