2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
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,
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
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
55 * @(#)in_cksum.c 7.3 (Berkeley) 6/28/90
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/malloc.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);}
79 register struct mbuf
*m
;
84 register int mlen
= 0;
96 for (;m
&& len
; m
= m
->m_next
) {
99 w
= mtod(m
, u_short
*);
102 * The first byte of this mbuf is the continuation
103 * of a word spanning between this mbuf and the
106 * s_util.c[0] is already saved when scanning previous
109 s_util
.c
[1] = *(char *)w
;
111 w
= (u_short
*)((char *)w
+ 1);
120 * Force to even boundary.
122 if ((1 & (long) w
) && (mlen
> 0)) {
125 s_util
.c
[0] = *(u_char
*)w
;
126 w
= (u_short
*)((char *)w
+ 1);
131 * Unroll the loop to make overhead from
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];
142 while ((mlen
-= 8) >= 0) {
143 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
147 if (mlen
== 0 && byte_swapped
== 0)
150 while ((mlen
-= 2) >= 0) {
158 s_util
.c
[1] = *(char *)w
;
163 } else if (mlen
== -1)
164 s_util
.c
[0] = *(char *)w
;
167 printf("cksum: out of data\n");
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) */
176 return (~sum
& 0xffff);