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