in_cksum.c revision 1.17 1 /* $NetBSD: in_cksum.c,v 1.17 2003/08/07 16:33:10 agc Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1992, 1993
5 * The Regents of the University of California. 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 University 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 REGENTS 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 REGENTS 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 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: in_cksum.c,v 1.17 2003/08/07 16:33:10 agc Exp $");
36
37 #include <sys/param.h>
38 #include <sys/mbuf.h>
39 #include <sys/systm.h>
40 #include <netinet/in.h>
41
42 /*
43 * Checksum routine for Internet Protocol family headers (Portable Version).
44 *
45 * This routine is very heavily used in the network
46 * code and should be modified for each CPU to be as fast as possible.
47 */
48
49 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
50 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
51
52 int
53 in_cksum(m, len)
54 struct mbuf *m;
55 int len;
56 {
57 u_int16_t *w;
58 int sum = 0;
59 int mlen = 0;
60 int byte_swapped = 0;
61
62 union {
63 u_int8_t c[2];
64 u_int16_t s;
65 } s_util;
66 union {
67 u_int16_t s[2];
68 u_int32_t l;
69 } l_util;
70
71 for (;m && len; m = m->m_next) {
72 if (m->m_len == 0)
73 continue;
74 w = mtod(m, u_int16_t *);
75 if (mlen == -1) {
76 /*
77 * The first byte of this mbuf is the continuation
78 * of a word spanning between this mbuf and the
79 * last mbuf.
80 *
81 * s_util.c[0] is already saved when scanning previous
82 * mbuf.
83 */
84 s_util.c[1] = *(u_int8_t *)w;
85 sum += s_util.s;
86 w = (u_int16_t *)((u_int8_t *)w + 1);
87 mlen = m->m_len - 1;
88 len--;
89 } else
90 mlen = m->m_len;
91 if (len < mlen)
92 mlen = len;
93 len -= mlen;
94 /*
95 * Force to even boundary.
96 */
97 if ((1 & (long) w) && (mlen > 0)) {
98 REDUCE;
99 sum <<= 8;
100 s_util.c[0] = *(u_int8_t *)w;
101 w = (u_int16_t *)((int8_t *)w + 1);
102 mlen--;
103 byte_swapped = 1;
104 }
105 /*
106 * Unroll the loop to make overhead from
107 * branches &c small.
108 */
109 while ((mlen -= 32) >= 0) {
110 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
111 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
112 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
113 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
114 w += 16;
115 }
116 mlen += 32;
117 while ((mlen -= 8) >= 0) {
118 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
119 w += 4;
120 }
121 mlen += 8;
122 if (mlen == 0 && byte_swapped == 0)
123 continue;
124 REDUCE;
125 while ((mlen -= 2) >= 0) {
126 sum += *w++;
127 }
128 if (byte_swapped) {
129 REDUCE;
130 sum <<= 8;
131 byte_swapped = 0;
132 if (mlen == -1) {
133 s_util.c[1] = *(u_int8_t *)w;
134 sum += s_util.s;
135 mlen = 0;
136 } else
137 mlen = -1;
138 } else if (mlen == -1)
139 s_util.c[0] = *(u_int8_t *)w;
140 }
141 if (len)
142 printf("cksum: out of data\n");
143 if (mlen == -1) {
144 /* The last mbuf has odd # of bytes. Follow the
145 standard (the odd byte may be shifted left by 8 bits
146 or not as determined by endian-ness of the machine) */
147 s_util.c[1] = 0;
148 sum += s_util.s;
149 }
150 REDUCE;
151 return (~sum & 0xffff);
152 }
153