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