revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / netinet / in_cksum.c
blob96e97f8b0f5cc0d08723aa4c19b30614baad89c9
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
5 * Copyright (C) 2005 Neil Cafferkey
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
24 * Copyright (c) 1988 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
55 * @(#)in_cksum.c 7.3 (Berkeley) 6/28/90
58 #include <conf.h>
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
65 #include <netinet/in_cksum_protos.h>
68 * Checksum routine for Internet Protocol family headers (Portable Version).
70 * This routine is very heavily used in the network
71 * code and should be modified for each CPU to be as fast as possible.
74 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
75 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
77 int
78 in_cksum(m, len)
79 register struct mbuf *m;
80 register int len;
82 register u_short *w;
83 register int sum = 0;
84 register int mlen = 0;
85 int byte_swapped = 0;
87 union {
88 char c[2];
89 u_short s;
90 } s_util;
91 union {
92 u_short s[2];
93 long l;
94 } l_util;
96 for (;m && len; m = m->m_next) {
97 if (m->m_len == 0)
98 continue;
99 w = mtod(m, u_short *);
100 if (mlen == -1) {
102 * The first byte of this mbuf is the continuation
103 * of a word spanning between this mbuf and the
104 * last mbuf.
106 * s_util.c[0] is already saved when scanning previous
107 * mbuf.
109 s_util.c[1] = *(char *)w;
110 sum += s_util.s;
111 w = (u_short *)((char *)w + 1);
112 mlen = m->m_len - 1;
113 len--;
114 } else
115 mlen = m->m_len;
116 if (len < mlen)
117 mlen = len;
118 len -= mlen;
120 * Force to even boundary.
122 if ((1 & (long) w) && (mlen > 0)) {
123 REDUCE;
124 sum <<= 8;
125 s_util.c[0] = *(u_char *)w;
126 w = (u_short *)((char *)w + 1);
127 mlen--;
128 byte_swapped = 1;
131 * Unroll the loop to make overhead from
132 * branches &c small.
134 while ((mlen -= 32) >= 0) {
135 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
136 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
137 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
138 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
139 w += 16;
141 mlen += 32;
142 while ((mlen -= 8) >= 0) {
143 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
144 w += 4;
146 mlen += 8;
147 if (mlen == 0 && byte_swapped == 0)
148 continue;
149 REDUCE;
150 while ((mlen -= 2) >= 0) {
151 sum += *w++;
153 if (byte_swapped) {
154 REDUCE;
155 sum <<= 8;
156 byte_swapped = 0;
157 if (mlen == -1) {
158 s_util.c[1] = *(char *)w;
159 sum += s_util.s;
160 mlen = 0;
161 } else
162 mlen = -1;
163 } else if (mlen == -1)
164 s_util.c[0] = *(char *)w;
166 if (len)
167 printf("cksum: out of data\n");
168 if (mlen == -1) {
169 /* The last mbuf has odd # of bytes. Follow the
170 standard (the odd byte may be shifted left by 8 bits
171 or not as determined by endian-ness of the machine) */
172 s_util.c[1] = 0;
173 sum += s_util.s;
175 REDUCE;
176 return (~sum & 0xffff);