]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
b0d623f7 | 2 | * Copyright (c) 2000-2009 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1988, 1992, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
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/param.h> | |
64 | #include <sys/mbuf.h> | |
65 | #include <sys/kdebug.h> | |
2d21ac55 A |
66 | #include <kern/debug.h> |
67 | #include <netinet/in.h> | |
68 | #include <netinet/ip.h> | |
b0d623f7 | 69 | #include <libkern/libkern.h> |
1c79356b A |
70 | |
71 | #define DBG_FNC_IN_CKSUM NETDBG_CODE(DBG_NETIP, (3 << 8)) | |
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 | ||
0b4e3aa0 A |
80 | union s_util { |
81 | char c[2]; | |
82 | u_short s; | |
83 | }; | |
1c79356b | 84 | |
0b4e3aa0 A |
85 | union l_util { |
86 | u_int16_t s[2]; | |
2d21ac55 | 87 | u_int32_t l; |
0b4e3aa0 | 88 | }; |
1c79356b | 89 | |
0b4e3aa0 A |
90 | union q_util { |
91 | u_int16_t s[4]; | |
92 | u_int32_t l[2]; | |
93 | u_int64_t q; | |
2d21ac55 | 94 | }; |
0b4e3aa0 A |
95 | |
96 | #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x) | |
97 | ||
98 | #define REDUCE32 \ | |
99 | { \ | |
100 | q_util.q = sum; \ | |
101 | sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \ | |
102 | } | |
103 | #define REDUCE16 \ | |
104 | { \ | |
105 | q_util.q = sum; \ | |
106 | l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \ | |
107 | sum = l_util.s[0] + l_util.s[1]; \ | |
108 | ADDCARRY(sum); \ | |
109 | } | |
110 | ||
111 | #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);} | |
112 | ||
2d21ac55 A |
113 | u_int16_t inet_cksum_simple(struct mbuf *, int); |
114 | ||
115 | u_int16_t | |
116 | inet_cksum_simple(struct mbuf *m, int len) | |
117 | { | |
118 | return (inet_cksum(m, 0, 0, len)); | |
119 | } | |
1c79356b | 120 | |
2d21ac55 | 121 | inline u_short |
0b4e3aa0 A |
122 | in_addword(u_short a, u_short b) |
123 | { | |
2d21ac55 | 124 | union l_util l_util; |
0b4e3aa0 | 125 | u_int32_t sum = a + b; |
2d21ac55 | 126 | |
0b4e3aa0 A |
127 | REDUCE; |
128 | return (sum); | |
129 | } | |
130 | ||
2d21ac55 | 131 | inline u_short |
0b4e3aa0 A |
132 | in_pseudo(u_int a, u_int b, u_int c) |
133 | { | |
134 | u_int64_t sum; | |
135 | union q_util q_util; | |
2d21ac55 | 136 | union l_util l_util; |
0b4e3aa0 A |
137 | |
138 | sum = (u_int64_t) a + b + c; | |
139 | REDUCE16; | |
140 | return (sum); | |
141 | ||
142 | } | |
143 | ||
b0d623f7 A |
144 | #if defined(__ppc__) |
145 | ||
146 | extern u_short xsum_assym(u_short *p, int len, u_short xsum, int odd); | |
147 | ||
2d21ac55 A |
148 | u_int16_t |
149 | inet_cksum(struct mbuf *m, unsigned int nxt, unsigned int skip, | |
150 | unsigned int len) | |
1c79356b | 151 | { |
2d21ac55 A |
152 | u_short *w; |
153 | u_int32_t sum = 0; | |
154 | int mlen = 0; | |
1c79356b A |
155 | int starting_on_odd = 0; |
156 | ||
1c79356b A |
157 | KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_START, len,0,0,0,0); |
158 | ||
2d21ac55 | 159 | /* sanity check */ |
593a1d5f | 160 | if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.len < skip + len) { |
2d21ac55 A |
161 | panic("inet_cksum: mbuf len (%d) < off+len (%d+%d)\n", |
162 | m->m_pkthdr.len, skip, len); | |
163 | } | |
1c79356b | 164 | |
2d21ac55 A |
165 | /* include pseudo header checksum? */ |
166 | if (nxt != 0) { | |
167 | struct ip *iph; | |
1c79356b | 168 | |
2d21ac55 A |
169 | if (m->m_len < sizeof (struct ip)) |
170 | panic("inet_cksum: bad mbuf chain"); | |
1c79356b | 171 | |
2d21ac55 A |
172 | iph = mtod(m, struct ip *); |
173 | sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr, | |
174 | htonl(len + nxt)); | |
175 | } | |
1c79356b | 176 | |
2d21ac55 A |
177 | if (skip != 0) { |
178 | for (; skip && m; m = m->m_next) { | |
179 | if (m->m_len > skip) { | |
180 | mlen = m->m_len - skip; | |
181 | w = (u_short *)(m->m_data+skip); | |
182 | goto skip_start; | |
183 | } else { | |
184 | skip -= m->m_len; | |
185 | } | |
186 | } | |
187 | } | |
0b4e3aa0 | 188 | |
0b4e3aa0 A |
189 | for (;m && len; m = m->m_next) { |
190 | if (m->m_len == 0) | |
191 | continue; | |
192 | mlen = m->m_len; | |
193 | w = mtod(m, u_short *); | |
194 | ||
765c9de3 | 195 | skip_start: |
0b4e3aa0 A |
196 | if (len < mlen) |
197 | mlen = len; | |
0b4e3aa0 A |
198 | sum = xsum_assym(w, mlen, sum, starting_on_odd); |
199 | len -= mlen; | |
200 | if (mlen & 0x1) | |
201 | { | |
202 | if (starting_on_odd) | |
203 | starting_on_odd = 0; | |
204 | else | |
205 | starting_on_odd = 1; | |
206 | } | |
207 | } | |
208 | ||
2d21ac55 A |
209 | KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_END, 0,0,0,0,0); |
210 | ||
0b4e3aa0 A |
211 | return (~sum & 0xffff); |
212 | } | |
2d21ac55 | 213 | |
b0d623f7 | 214 | #elif defined(__arm__) && __ARM_ARCH__ >= 6 |
1c79356b | 215 | |
b0d623f7 A |
216 | extern int cpu_in_cksum(struct mbuf *m, int len, int off, uint32_t initial_sum); |
217 | ||
218 | u_int16_t | |
219 | inet_cksum(struct mbuf *m, unsigned int nxt, unsigned int skip, | |
220 | unsigned int len) | |
2d21ac55 | 221 | { |
b0d623f7 | 222 | u_int32_t sum = 0; |
2d21ac55 | 223 | |
b0d623f7 A |
224 | /* sanity check */ |
225 | if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.len < skip + len) { | |
226 | panic("inet_cksum: mbuf len (%d) < off+len (%d+%d)\n", | |
227 | m->m_pkthdr.len, skip, len); | |
228 | } | |
1c79356b | 229 | |
b0d623f7 A |
230 | /* include pseudo header checksum? */ |
231 | if (nxt != 0) { | |
232 | struct ip *iph; | |
1c79356b | 233 | |
b0d623f7 A |
234 | if (m->m_len < sizeof (struct ip)) |
235 | panic("inet_cksum: bad mbuf chain"); | |
236 | ||
237 | iph = mtod(m, struct ip *); | |
238 | sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr, | |
239 | htonl(len + nxt)); | |
240 | } | |
241 | ||
242 | return (cpu_in_cksum(m, len, skip, sum)); | |
0b4e3aa0 | 243 | } |
1c79356b | 244 | |
b0d623f7 A |
245 | #else |
246 | ||
2d21ac55 A |
247 | u_int16_t |
248 | inet_cksum(struct mbuf *m, unsigned int nxt, unsigned int skip, | |
249 | unsigned int len) | |
1c79356b | 250 | { |
2d21ac55 A |
251 | u_short *w; |
252 | u_int32_t sum = 0; | |
253 | int mlen = 0; | |
1c79356b | 254 | int byte_swapped = 0; |
0b4e3aa0 | 255 | union s_util s_util; |
2d21ac55 | 256 | union l_util l_util; |
1c79356b A |
257 | |
258 | KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_START, len,0,0,0,0); | |
259 | ||
2d21ac55 | 260 | /* sanity check */ |
593a1d5f | 261 | if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.len < skip + len) { |
2d21ac55 A |
262 | panic("inet_cksum: mbuf len (%d) < off+len (%d+%d)\n", |
263 | m->m_pkthdr.len, skip, len); | |
0b4e3aa0 | 264 | } |
0b4e3aa0 | 265 | |
2d21ac55 A |
266 | /* include pseudo header checksum? */ |
267 | if (nxt != 0) { | |
268 | struct ip *iph; | |
0b4e3aa0 | 269 | |
2d21ac55 A |
270 | if (m->m_len < sizeof (struct ip)) |
271 | panic("inet_cksum: bad mbuf chain"); | |
272 | ||
273 | iph = mtod(m, struct ip *); | |
274 | sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr, | |
275 | htonl(len + nxt)); | |
276 | } | |
0b4e3aa0 | 277 | |
2d21ac55 A |
278 | if (skip != 0) { |
279 | for (; skip && m; m = m->m_next) { | |
280 | if (m->m_len > skip) { | |
281 | mlen = m->m_len - skip; | |
282 | w = (u_short *)(m->m_data+skip); | |
283 | goto skip_start; | |
284 | } else { | |
285 | skip -= m->m_len; | |
286 | } | |
287 | } | |
288 | } | |
0b4e3aa0 A |
289 | for (;m && len; m = m->m_next) { |
290 | if (m->m_len == 0) | |
291 | continue; | |
292 | w = mtod(m, u_short *); | |
293 | ||
294 | if (mlen == -1) { | |
295 | /* | |
296 | * The first byte of this mbuf is the continuation | |
297 | * of a word spanning between this mbuf and the | |
298 | * last mbuf. | |
299 | * | |
300 | * s_util.c[0] is already saved when scanning previous | |
301 | * mbuf. | |
302 | */ | |
303 | s_util.c[1] = *(char *)w; | |
304 | sum += s_util.s; | |
305 | w = (u_short *)((char *)w + 1); | |
306 | mlen = m->m_len - 1; | |
307 | len--; | |
308 | } else { | |
2d21ac55 | 309 | mlen = m->m_len; |
0b4e3aa0 | 310 | } |
0b4e3aa0 | 311 | skip_start: |
765c9de3 | 312 | if (len < mlen) |
2d21ac55 | 313 | mlen = len; |
0b4e3aa0 A |
314 | |
315 | len -= mlen; | |
316 | /* | |
317 | * Force to even boundary. | |
318 | */ | |
b0d623f7 | 319 | if ((1 & (uintptr_t) w) && (mlen > 0)) { |
0b4e3aa0 A |
320 | REDUCE; |
321 | sum <<= 8; | |
322 | s_util.c[0] = *(u_char *)w; | |
323 | w = (u_short *)((char *)w + 1); | |
324 | mlen--; | |
325 | byte_swapped = 1; | |
326 | } | |
327 | /* | |
328 | * Unroll the loop to make overhead from | |
329 | * branches &c small. | |
330 | */ | |
331 | while ((mlen -= 32) >= 0) { | |
332 | sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; | |
333 | sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; | |
1c79356b A |
334 | sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; |
335 | sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; | |
336 | w += 16; | |
337 | } | |
338 | mlen += 32; | |
339 | while ((mlen -= 8) >= 0) { | |
340 | sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; | |
341 | w += 4; | |
342 | } | |
343 | mlen += 8; | |
344 | if (mlen == 0 && byte_swapped == 0) | |
345 | continue; | |
346 | REDUCE; | |
347 | while ((mlen -= 2) >= 0) { | |
348 | sum += *w++; | |
349 | } | |
350 | if (byte_swapped) { | |
351 | REDUCE; | |
352 | sum <<= 8; | |
353 | byte_swapped = 0; | |
354 | if (mlen == -1) { | |
355 | s_util.c[1] = *(char *)w; | |
356 | sum += s_util.s; | |
357 | mlen = 0; | |
358 | } else | |
359 | mlen = -1; | |
360 | } else if (mlen == -1) | |
361 | s_util.c[0] = *(char *)w; | |
362 | } | |
363 | if (len) | |
2d21ac55 | 364 | printf("cksum: out of data by %d\n", len); |
1c79356b A |
365 | if (mlen == -1) { |
366 | /* The last mbuf has odd # of bytes. Follow the | |
367 | standard (the odd byte may be shifted left by 8 bits | |
368 | or not as determined by endian-ness of the machine) */ | |
369 | s_util.c[1] = 0; | |
370 | sum += s_util.s; | |
371 | } | |
372 | REDUCE; | |
373 | KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_END, 0,0,0,0,0); | |
374 | return (~sum & 0xffff); | |
375 | } | |
376 | ||
377 | #endif |