xref: /openbsd-src/sys/netinet/in4_cksum.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: in4_cksum.c,v 1.4 2001/06/24 23:45:16 angelos Exp $	*/
2 
3 /*	$NetBSD: in_cksum.c,v 1.13 1996/10/13 02:03:03 christos Exp $	*/
4 
5 /*
6  * Copyright (C) 1999 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1988, 1992, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
67  */
68 
69 #include <sys/param.h>
70 #include <sys/mbuf.h>
71 #include <sys/systm.h>
72 #include <sys/socket.h>
73 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_var.h>
78 
79 /*
80  * Checksum routine for Internet Protocol family headers (Portable Version).
81  * This is only for IPv4 pseudo header checksum.
82  * No need to clear non-pseudo-header fields in IPv4 header.
83  * len is for actual payload size, and does not include IPv4 header and
84  * skipped header chain (off + len should be equal to the whole packet).
85  *
86  * This routine is very heavily used in the network
87  * code and should be modified for each CPU to be as fast as possible.
88  */
89 
90 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
91 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
92 
93 int
94 in4_cksum(m, nxt, off, len)
95 	register struct mbuf *m;
96 	u_int8_t nxt;
97 	register int off, len;
98 {
99 	register u_int16_t *w;
100 	register int sum = 0;
101 	register int mlen = 0;
102 	int byte_swapped = 0;
103 	struct ipovly ipov;
104 
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 	if (nxt != 0) {
115 		/* pseudo header */
116 		if (off < sizeof(struct ipovly))
117 			panic("in4_cksum: offset too short");
118 		if (m->m_len < sizeof(struct ip))
119 			panic("in4_cksum: bad mbuf chain");
120 		bzero(&ipov, sizeof(ipov));
121 		ipov.ih_len = htons(len);
122 		ipov.ih_pr = nxt;
123 		ipov.ih_src = mtod(m, struct ip *)->ip_src;
124 		ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
125 		w = (u_int16_t *)&ipov;
126 		/* assumes sizeof(ipov) == 20 */
127 		sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];
128 		sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];
129 	}
130 
131 	/* skip unnecessary part */
132 	while (m && off > 0) {
133 		if (m->m_len > off)
134 			break;
135 		off -= m->m_len;
136 		m = m->m_next;
137 	}
138 
139 	for (;m && len; m = m->m_next) {
140 		if (m->m_len == 0)
141 			continue;
142 		w = (u_int16_t *)(mtod(m, caddr_t) + off);
143 		if (mlen == -1) {
144 			/*
145 			 * The first byte of this mbuf is the continuation
146 			 * of a word spanning between this mbuf and the
147 			 * last mbuf.
148 			 *
149 			 * s_util.c[0] is already saved when scanning previous
150 			 * mbuf.
151 			 */
152 			s_util.c[1] = *(u_int8_t *)w;
153 			sum += s_util.s;
154 			w = (u_int16_t *)((u_int8_t *)w + 1);
155 			mlen = m->m_len - off - 1;
156 			len--;
157 		} else
158 			mlen = m->m_len - off;
159 		off = 0;
160 		if (len < mlen)
161 			mlen = len;
162 		len -= mlen;
163 		/*
164 		 * Force to even boundary.
165 		 */
166 		if ((1 & (long) w) && (mlen > 0)) {
167 			REDUCE;
168 			sum <<= 8;
169 			s_util.c[0] = *(u_int8_t *)w;
170 			w = (u_int16_t *)((int8_t *)w + 1);
171 			mlen--;
172 			byte_swapped = 1;
173 		}
174 		/*
175 		 * Unroll the loop to make overhead from
176 		 * branches &c small.
177 		 */
178 		while ((mlen -= 32) >= 0) {
179 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
180 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
181 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
182 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
183 			w += 16;
184 		}
185 		mlen += 32;
186 		while ((mlen -= 8) >= 0) {
187 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
188 			w += 4;
189 		}
190 		mlen += 8;
191 		if (mlen == 0 && byte_swapped == 0)
192 			continue;
193 		REDUCE;
194 		while ((mlen -= 2) >= 0) {
195 			sum += *w++;
196 		}
197 		if (byte_swapped) {
198 			REDUCE;
199 			sum <<= 8;
200 			byte_swapped = 0;
201 			if (mlen == -1) {
202 				s_util.c[1] = *(u_int8_t *)w;
203 				sum += s_util.s;
204 				mlen = 0;
205 			} else
206 				mlen = -1;
207 		} else if (mlen == -1)
208 			s_util.c[0] = *(u_int8_t *)w;
209 	}
210 	if (len)
211 		printf("cksum4: out of data\n");
212 	if (mlen == -1) {
213 		/* The last mbuf has odd # of bytes. Follow the
214 		   standard (the odd byte may be shifted left by 8 bits
215 		   or not as determined by endian-ness of the machine) */
216 		s_util.c[1] = 0;
217 		sum += s_util.s;
218 	}
219 	REDUCE;
220 	return (~sum & 0xffff);
221 }
222