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