1 /* $NetBSD: in4_cksum.c,v 1.11 2006/12/31 10:52:52 ragge Exp $ */
4 * Copyright (C) 1999 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Copyright (c) 1988, 1992, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.11 2006/12/31 10:52:52 ragge Exp $");
66 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/socket.h>
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
77 int in4_cksum_md_debug(struct mbuf
*m
, uint8_t nxt
, int off
, int len
);
78 #define in4_cksum in4_cksum_md_debug
79 #include <netinet/in4_cksum.c>
86 * Checksum routine for Internet Protocol family headers.
87 * This is only for IPv4 pseudo header checksum.
88 * No need to clear non-pseudo-header fields in IPv4 header.
89 * len is for actual payload size, and does not include IPv4 header and
90 * skipped header chain (off + len should be equal to the whole packet).
92 * This implementation is VAX version.
96 #define REDUCE {sum = (sum & 0xffff) + (sum >> 16);}
97 #define ADDCARRY {if (sum > 0xffff) sum -= 0xffff;}
98 #define ADVANCE(n) {w += n; mlen -= n;}
99 #define SWAP {sum <<= 8;} /* depends on recent REDUCE */
101 #define Asm __asm volatile
102 #define ADDL Asm("addl2 (%0)+,%1": "=r" (w), "=r" (sum): "0" (w), "1" (sum))
103 #define ADWC Asm("adwc (%0)+,%1": "=r" (w), "=r" (sum): "0" (w), "1" (sum))
104 #define ADDC Asm("adwc $0,%0" : "=r" (sum) : "0" (sum))
105 #define UNSWAP Asm("rotl $8,%0,%0" : "=r" (sum) : "0" (sum))
106 #define ADDBYTE {sum += *w; SWAP; byte_swapped ^= 1;}
107 #define ADDWORD {sum += *(uint16_t *)w;}
110 in4_cksum(struct mbuf
*m
, uint8_t nxt
, int off
, int len
)
115 int byte_swapped
= 0;
117 int debugrv
= in4_cksum_md_debug(m
, nxt
, off
, len
);
122 if (off
< sizeof(struct ipovly
))
123 panic("in4_cksum: offset too short");
124 if (m
->m_len
< sizeof(struct ip
))
125 panic("in4_cksum: bad mbuf chain");
129 "movzwl 16(%%ap),%0;" /* mov len to sum */
130 "addb2 8(%%ap),%0;" /* add proto to sum */
131 "rotl $8,%0,%0;" /* htons, carry is preserved */
132 "adwc 12(%2),%0;" /* add src ip */
133 "adwc 16(%2),%0;" /* add dst ip */
134 "adwc $0,%0;" /* clean up carry */
136 : "0" (sum
), "r" (mtod(m
, void *)));
139 /* skip unnecessary part */
140 while (m
&& off
> 0) {
147 for (;m
&& len
; m
= m
->m_next
) {
148 if ((mlen
= m
->m_len
) == 0)
150 w
= mtod(m
, uint8_t *);
162 * Ensure that we're aligned on a word boundary here so
163 * that we can do 32 bit operations below.
165 if ((3 & (intptr_t) w
) != 0) {
167 if ((1 & (intptr_t) w
) != 0) {
171 if ((2 & (intptr_t) w
) != 0) {
177 * Do as much of the checksum as possible 32 bits at at time.
178 * In fact, this loop is unrolled to make overhead from
181 while ((mlen
-= 32) >= 0) {
183 * Add with carry 16 words and fold in the last carry
184 * by adding a 0 with carry.
186 ADDL
; ADWC
; ADWC
; ADWC
;
187 ADWC
; ADWC
; ADWC
; ADWC
;
192 ADDL
; ADWC
; ADWC
; ADWC
;
220 printf("cksum4: out of data\n");
227 if ((sum
^ 0xffff) != debugrv
)
228 printf("in4_cksum: rv != debugrv (rv %x debugrv %x)\n",
229 (sum
^ 0xffff), debugrv
);
231 return (sum
^ 0xffff);