xref: /freebsd/sys/netinet6/in6_cksum.c (revision 7bd6fde3)
1 /*	$FreeBSD$	*/
2 /*	$KAME: in6_cksum.c,v 1.10 2000/12/03 00:53:59 itojun Exp $	*/
3 
4 /*-
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1988, 1992, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 4. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
62  */
63 
64 #include <sys/param.h>
65 #include <sys/mbuf.h>
66 #include <sys/systm.h>
67 #include <netinet/in.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/scope6_var.h>
70 
71 /*
72  * Checksum routine for Internet Protocol family headers (Portable Version).
73  *
74  * This routine is very heavily used in the network
75  * code and should be modified for each CPU to be as fast as possible.
76  */
77 
78 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
79 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
80 
81 /*
82  * m MUST contain a continuous IP6 header.
83  * off is an offset where TCP/UDP/ICMP6 header starts.
84  * len is a total length of a transport segment.
85  * (e.g. TCP header + TCP payload)
86  */
87 
88 int
89 in6_cksum(struct mbuf *m, u_int8_t nxt, u_int32_t off, u_int32_t len)
90 {
91 	u_int16_t *w;
92 	int sum = 0;
93 	int mlen = 0;
94 	int byte_swapped = 0;
95 	struct ip6_hdr *ip6;
96 	struct in6_addr in6;
97 	union {
98 		u_int16_t phs[4];
99 		struct {
100 			u_int32_t	ph_len;
101 			u_int8_t	ph_zero[3];
102 			u_int8_t	ph_nxt;
103 		} __packed ph;
104 	} uph;
105 	union {
106 		u_int8_t	c[2];
107 		u_int16_t	s;
108 	} s_util;
109 	union {
110 		u_int16_t s[2];
111 		u_int32_t l;
112 	} l_util;
113 
114 	/* sanity check */
115 	if (m->m_pkthdr.len < off + len) {
116 		panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
117 			m->m_pkthdr.len, off, len);
118 	}
119 
120 	bzero(&uph, sizeof(uph));
121 
122 	/*
123 	 * First create IP6 pseudo header and calculate a summary.
124 	 */
125 	ip6 = mtod(m, struct ip6_hdr *);
126 	uph.ph.ph_len = htonl(len);
127 	uph.ph.ph_nxt = nxt;
128 
129 	/*
130 	 * IPv6 source address.
131 	 * XXX: we'd like to avoid copying the address, but we can't due to
132 	 * the possibly embedded scope zone ID.
133 	 */
134 	in6 = ip6->ip6_src;
135 	in6_clearscope(&in6);
136 	w = (u_int16_t *)&in6;
137 	sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
138 	sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
139 
140 	/* IPv6 destination address */
141 	in6 = ip6->ip6_dst;
142 	in6_clearscope(&in6);
143 	w = (u_int16_t *)&in6;
144 	sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
145 	sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
146 
147 	/* Payload length and upper layer identifier */
148 	sum += uph.phs[0];  sum += uph.phs[1];
149 	sum += uph.phs[2];  sum += uph.phs[3];
150 
151 	/*
152 	 * Secondly calculate a summary of the first mbuf excluding offset.
153 	 */
154 	while (off > 0) {
155 		if (m->m_len <= off)
156 			off -= m->m_len;
157 		else
158 			break;
159 		m = m->m_next;
160 	}
161 	w = (u_int16_t *)(mtod(m, u_char *) + off);
162 	mlen = m->m_len - off;
163 	if (len < mlen)
164 		mlen = len;
165 	len -= mlen;
166 	/*
167 	 * Force to even boundary.
168 	 */
169 	if ((1 & (long) w) && (mlen > 0)) {
170 		REDUCE;
171 		sum <<= 8;
172 		s_util.c[0] = *(u_char *)w;
173 		w = (u_int16_t *)((char *)w + 1);
174 		mlen--;
175 		byte_swapped = 1;
176 	}
177 	/*
178 	 * Unroll the loop to make overhead from
179 	 * branches &c small.
180 	 */
181 	while ((mlen -= 32) >= 0) {
182 		sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
183 		sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
184 		sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
185 		sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
186 		w += 16;
187 	}
188 	mlen += 32;
189 	while ((mlen -= 8) >= 0) {
190 		sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
191 		w += 4;
192 	}
193 	mlen += 8;
194 	if (mlen == 0 && byte_swapped == 0)
195 		goto next;
196 	REDUCE;
197 	while ((mlen -= 2) >= 0) {
198 		sum += *w++;
199 	}
200 	if (byte_swapped) {
201 		REDUCE;
202 		sum <<= 8;
203 		byte_swapped = 0;
204 		if (mlen == -1) {
205 			s_util.c[1] = *(char *)w;
206 			sum += s_util.s;
207 			mlen = 0;
208 		} else
209 			mlen = -1;
210 	} else if (mlen == -1)
211 		s_util.c[0] = *(char *)w;
212  next:
213 	m = m->m_next;
214 
215 	/*
216 	 * Lastly calculate a summary of the rest of mbufs.
217 	 */
218 
219 	for (;m && len; m = m->m_next) {
220 		if (m->m_len == 0)
221 			continue;
222 		w = mtod(m, u_int16_t *);
223 		if (mlen == -1) {
224 			/*
225 			 * The first byte of this mbuf is the continuation
226 			 * of a word spanning between this mbuf and the
227 			 * last mbuf.
228 			 *
229 			 * s_util.c[0] is already saved when scanning previous
230 			 * mbuf.
231 			 */
232 			s_util.c[1] = *(char *)w;
233 			sum += s_util.s;
234 			w = (u_int16_t *)((char *)w + 1);
235 			mlen = m->m_len - 1;
236 			len--;
237 		} else
238 			mlen = m->m_len;
239 		if (len < mlen)
240 			mlen = len;
241 		len -= mlen;
242 		/*
243 		 * Force to even boundary.
244 		 */
245 		if ((1 & (long) w) && (mlen > 0)) {
246 			REDUCE;
247 			sum <<= 8;
248 			s_util.c[0] = *(u_char *)w;
249 			w = (u_int16_t *)((char *)w + 1);
250 			mlen--;
251 			byte_swapped = 1;
252 		}
253 		/*
254 		 * Unroll the loop to make overhead from
255 		 * branches &c small.
256 		 */
257 		while ((mlen -= 32) >= 0) {
258 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
259 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
260 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
261 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
262 			w += 16;
263 		}
264 		mlen += 32;
265 		while ((mlen -= 8) >= 0) {
266 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
267 			w += 4;
268 		}
269 		mlen += 8;
270 		if (mlen == 0 && byte_swapped == 0)
271 			continue;
272 		REDUCE;
273 		while ((mlen -= 2) >= 0) {
274 			sum += *w++;
275 		}
276 		if (byte_swapped) {
277 			REDUCE;
278 			sum <<= 8;
279 			byte_swapped = 0;
280 			if (mlen == -1) {
281 				s_util.c[1] = *(char *)w;
282 				sum += s_util.s;
283 				mlen = 0;
284 			} else
285 				mlen = -1;
286 		} else if (mlen == -1)
287 			s_util.c[0] = *(char *)w;
288 	}
289 	if (len)
290 		panic("in6_cksum: out of data");
291 	if (mlen == -1) {
292 		/* The last mbuf has odd # of bytes. Follow the
293 		   standard (the odd byte may be shifted left by 8 bits
294 		   or not as determined by endian-ness of the machine) */
295 		s_util.c[1] = 0;
296 		sum += s_util.s;
297 	}
298 	REDUCE;
299 	return (~sum & 0xffff);
300 }
301