]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_cksum.c
xnu-2782.30.5.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_cksum.c
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 */
141 uint16_t
142 in6_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 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
155 sum += w[6]; sum += w[7];
156
157 /*
158 * IPv6 destination address
159 */
160 w = (const uint16_t *)dst;
161 sum += w[0];
162 if (!IN6_IS_SCOPE_EMBED(dst))
163 sum += w[1];
164 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
165 sum += w[6]; sum += w[7];
166
167 /*
168 * Caller-supplied value; 'x' could be one of:
169 *
170 * htonl(proto + length), or
171 * htonl(proto + length + sum)
172 **/
173 sum += x;
174
175 /* fold in carry bits */
176 ADDCARRY(sum);
177
178 return (sum);
179 }
180
181 /*
182 * m MUST contain at least an IPv6 header, if nxt is specified;
183 * nxt is the upper layer protocol number;
184 * off is an offset where TCP/UDP/ICMP6 header starts;
185 * len is a total length of a transport segment (e.g. TCP header + TCP payload)
186 */
187 u_int16_t
188 inet6_cksum(struct mbuf *m, uint32_t nxt, uint32_t off, uint32_t len)
189 {
190 uint32_t sum;
191
192 sum = m_sum16(m, off, len);
193
194 if (nxt != 0) {
195 struct ip6_hdr *ip6;
196 unsigned char buf[sizeof (*ip6)] __attribute__((aligned(8)));
197 uint32_t mlen;
198
199 /*
200 * Sanity check
201 *
202 * Use m_length2() instead of m_length(), as we cannot rely on
203 * the caller setting m_pkthdr.len correctly, if the mbuf is
204 * a M_PKTHDR one.
205 */
206 if ((mlen = m_length2(m, NULL)) < sizeof (*ip6)) {
207 panic("%s: mbuf %p pkt too short (%d) for IPv6 header",
208 __func__, m, mlen);
209 /* NOTREACHED */
210 }
211
212 /*
213 * In case the IPv6 header is not contiguous, or not 32-bit
214 * aligned, copy it to a local buffer. Note here that we
215 * expect the data pointer to point to the IPv6 header.
216 */
217 if ((sizeof (*ip6) > m->m_len) ||
218 !IP6_HDR_ALIGNED_P(mtod(m, caddr_t))) {
219 m_copydata(m, 0, sizeof (*ip6), (caddr_t)buf);
220 ip6 = (struct ip6_hdr *)(void *)buf;
221 } else {
222 ip6 = (struct ip6_hdr *)(void *)(m->m_data);
223 }
224
225 /* add pseudo header checksum */
226 sum += in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst,
227 htonl(nxt + len));
228
229 /* fold in carry bits */
230 ADDCARRY(sum);
231 }
232
233 return (~sum & 0xffff);
234 }