1/*
2 * Copyright (c) 2000-2009 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 * 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>
66#include <kern/debug.h>
67#include <netinet/in.h>
68#include <netinet/ip.h>
69#include <libkern/libkern.h>
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
80union s_util {
81        char    c[2];
82        u_short s;
83};
84
85union l_util {
86        u_int16_t s[2];
87        u_int32_t l;
88};
89
90union q_util {
91        u_int16_t s[4];
92        u_int32_t l[2];
93        u_int64_t q;
94};
95
96#define ADDCARRY(x)  do { if (x > 65535) { x -= 65535; } } while (0)
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
113u_int16_t inet_cksum_simple(struct mbuf *, int);
114
115u_int16_t
116inet_cksum_simple(struct mbuf *m, int len)
117{
118	return (inet_cksum(m, 0, 0, len));
119}
120
121u_short
122in_addword(u_short a, u_short b)
123{
124        union l_util l_util;
125	u_int32_t sum = a + b;
126
127	REDUCE;
128	return (sum);
129}
130
131u_short
132in_pseudo(u_int a, u_int b, u_int c)
133{
134        u_int64_t sum;
135        union q_util q_util;
136        union l_util l_util;
137
138        sum = (u_int64_t) a + b + c;
139        REDUCE16;
140        return (sum);
141
142}
143
144#if defined(__arm__)
145
146extern int cpu_in_cksum(struct mbuf *m, int len, int off, uint32_t initial_sum);
147
148u_int16_t
149inet_cksum(struct mbuf *m, unsigned int nxt, unsigned int skip,
150    unsigned int len)
151{
152	u_int32_t sum = 0;
153
154	/* sanity check */
155	if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.len < skip + len) {
156		panic("inet_cksum: mbuf len (%d) < off+len (%d+%d)\n",
157		    m->m_pkthdr.len, skip, len);
158	}
159
160	/* include pseudo header checksum? */
161	if (nxt != 0) {
162		struct ip *iph;
163
164		if (m->m_len < sizeof (struct ip))
165			panic("inet_cksum: bad mbuf chain");
166
167		iph = mtod(m, struct ip *);
168		sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr,
169		    htonl(len + nxt));
170	}
171
172	return (cpu_in_cksum(m, len, skip, sum));
173}
174
175#else
176
177u_int16_t
178inet_cksum(struct mbuf *m, unsigned int nxt, unsigned int skip,
179    unsigned int len)
180{
181	u_short *w;
182	u_int32_t sum = 0;
183	int mlen = 0;
184	int byte_swapped = 0;
185	union s_util s_util;
186	union l_util l_util;
187
188	KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_START, len,0,0,0,0);
189
190	/* sanity check */
191	if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.len < skip + len) {
192		panic("inet_cksum: mbuf len (%d) < off+len (%d+%d)\n",
193		    m->m_pkthdr.len, skip, len);
194	}
195
196	/* include pseudo header checksum? */
197	if (nxt != 0) {
198		struct ip *iph;
199
200		if (m->m_len < sizeof (struct ip))
201			panic("inet_cksum: bad mbuf chain");
202
203		iph = mtod(m, struct ip *);
204		sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr,
205		    htonl(len + nxt));
206	}
207
208	if (skip != 0) {
209		for (; skip && m; m = m->m_next) {
210			if (m->m_len > skip) {
211				mlen = m->m_len - skip;
212				w = (u_short *)(void *)(m->m_data+skip);
213				goto skip_start;
214			} else {
215				skip -= m->m_len;
216			}
217		}
218	}
219	for (;m && len; m = m->m_next) {
220		if (m->m_len == 0)
221			continue;
222		w = mtod(m, u_short *);
223
224		if (mlen == -1) {
225			/*
226			 * The first byte of this mbuf is the continuation
227			 * of a word spanning between this mbuf and the
228			 * last mbuf.
229			 *
230			 * s_util.c[0] is already saved when scanning previous
231			 * mbuf.
232			 */
233			s_util.c[1] = *(char *)w;
234			sum += s_util.s;
235			w = (u_short *)(void *)((char *)w + 1);
236			mlen = m->m_len - 1;
237			len--;
238		} else {
239			mlen = m->m_len;
240		}
241skip_start:
242		if (len < mlen)
243			mlen = len;
244
245		len -= mlen;
246		/*
247		 * Force to even boundary.
248		 */
249		if ((1 & (uintptr_t) w) && (mlen > 0)) {
250			REDUCE;
251			sum <<= 8;
252			s_util.c[0] = *(u_char *)w;
253			w = (u_short *)(void *)((char *)w + 1);
254			mlen--;
255			byte_swapped = 1;
256		}
257		/*
258		 * Unroll the loop to make overhead from
259		 * branches &c small.
260		 */
261		while ((mlen -= 32) >= 0) {
262			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
263			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
264			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
265			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
266			w += 16;
267		}
268		mlen += 32;
269		while ((mlen -= 8) >= 0) {
270			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
271			w += 4;
272		}
273		mlen += 8;
274		if (mlen == 0 && byte_swapped == 0)
275			continue;
276		REDUCE;
277		while ((mlen -= 2) >= 0) {
278			sum += *w++;
279		}
280		if (byte_swapped) {
281			REDUCE;
282			sum <<= 8;
283			byte_swapped = 0;
284			if (mlen == -1) {
285				s_util.c[1] = *(char *)w;
286				sum += s_util.s;
287				mlen = 0;
288			} else
289				mlen = -1;
290		} else if (mlen == -1)
291			s_util.c[0] = *(char *)w;
292	}
293	if (len)
294		printf("cksum: out of data by %d\n", len);
295	if (mlen == -1) {
296		/* The last mbuf has odd # of bytes. Follow the
297		   standard (the odd byte may be shifted left by 8 bits
298		   or not as determined by endian-ness of the machine) */
299		s_util.c[1] = 0;
300		sum += s_util.s;
301	}
302	REDUCE;
303	KERNEL_DEBUG(DBG_FNC_IN_CKSUM | DBG_FUNC_END, 0,0,0,0,0);
304	return (~sum & 0xffff);
305}
306
307#endif
308