1 /* $OpenBSD: in_cksum.c,v 1.3 1997/02/24 14:06:35 niklas Exp $ */
2 /* $NetBSD: in_cksum.c,v 1.11 1996/04/08 19:55:37 jonathan Exp $ */
5 * Copyright (c) 1988, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
39 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <netinet/in.h>
45 * Checksum routine for Internet Protocol family headers (Portable Version).
47 * This routine is very heavily used in the network
48 * code and should be modified for each CPU to be as fast as possible.
51 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
52 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
56 register struct mbuf
*m
;
59 register u_int16_t
*w
;
61 register int mlen
= 0;
73 for (;m
&& len
; m
= m
->m_next
) {
76 w
= mtod(m
, u_int16_t
*);
79 * The first byte of this mbuf is the continuation
80 * of a word spanning between this mbuf and the
83 * s_util.c[0] is already saved when scanning previous
86 s_util
.c
[1] = *(u_int8_t
*)w
;
88 w
= (u_int16_t
*)((u_int8_t
*)w
+ 1);
97 * Force to even boundary.
99 if ((1 & (long) w
) && (mlen
> 0)) {
102 s_util
.c
[0] = *(u_int8_t
*)w
;
103 w
= (u_int16_t
*)((int8_t *)w
+ 1);
108 * Unroll the loop to make overhead from
111 while ((mlen
-= 32) >= 0) {
112 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
113 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
114 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
115 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
119 while ((mlen
-= 8) >= 0) {
120 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
124 if (mlen
== 0 && byte_swapped
== 0)
127 while ((mlen
-= 2) >= 0) {
135 s_util
.c
[1] = *(u_int8_t
*)w
;
140 } else if (mlen
== -1)
141 s_util
.c
[0] = *(u_int8_t
*)w
;
144 printf("cksum: out of data\n");
146 /* The last mbuf has odd # of bytes. Follow the
147 standard (the odd byte may be shifted left by 8 bits
148 or not as determined by endian-ness of the machine) */
153 return (~sum
& 0xffff);