1*ecbbe831SMark Johnston /* $NetBSD: in_cksum.c,v 1.7 1997/09/02 13:18:15 thorpej Exp $ */
2*ecbbe831SMark Johnston
3*ecbbe831SMark Johnston /*-
4*ecbbe831SMark Johnston * SPDX-License-Identifier: BSD-4-Clause
5*ecbbe831SMark Johnston *
6*ecbbe831SMark Johnston * Copyright (c) 1988, 1992, 1993
7*ecbbe831SMark Johnston * The Regents of the University of California. All rights reserved.
8*ecbbe831SMark Johnston * Copyright (c) 1996
9*ecbbe831SMark Johnston * Matt Thomas <matt@3am-software.com>
10*ecbbe831SMark Johnston *
11*ecbbe831SMark Johnston * Redistribution and use in source and binary forms, with or without
12*ecbbe831SMark Johnston * modification, are permitted provided that the following conditions
13*ecbbe831SMark Johnston * are met:
14*ecbbe831SMark Johnston * 1. Redistributions of source code must retain the above copyright
15*ecbbe831SMark Johnston * notice, this list of conditions and the following disclaimer.
16*ecbbe831SMark Johnston * 2. Redistributions in binary form must reproduce the above copyright
17*ecbbe831SMark Johnston * notice, this list of conditions and the following disclaimer in the
18*ecbbe831SMark Johnston * documentation and/or other materials provided with the distribution.
19*ecbbe831SMark Johnston * 3. All advertising materials mentioning features or use of this software
20*ecbbe831SMark Johnston * must display the following acknowledgement:
21*ecbbe831SMark Johnston * This product includes software developed by the University of
22*ecbbe831SMark Johnston * California, Berkeley and its contributors.
23*ecbbe831SMark Johnston * 4. Neither the name of the University nor the names of its contributors
24*ecbbe831SMark Johnston * may be used to endorse or promote products derived from this software
25*ecbbe831SMark Johnston * without specific prior written permission.
26*ecbbe831SMark Johnston *
27*ecbbe831SMark Johnston * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28*ecbbe831SMark Johnston * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29*ecbbe831SMark Johnston * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30*ecbbe831SMark Johnston * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31*ecbbe831SMark Johnston * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32*ecbbe831SMark Johnston * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33*ecbbe831SMark Johnston * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34*ecbbe831SMark Johnston * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35*ecbbe831SMark Johnston * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36*ecbbe831SMark Johnston * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37*ecbbe831SMark Johnston * SUCH DAMAGE.
38*ecbbe831SMark Johnston */
39*ecbbe831SMark Johnston
40*ecbbe831SMark Johnston #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
41*ecbbe831SMark Johnston #include <sys/param.h>
42*ecbbe831SMark Johnston #include <sys/mbuf.h>
43*ecbbe831SMark Johnston #include <sys/systm.h>
44*ecbbe831SMark Johnston #include <netinet/in_systm.h>
45*ecbbe831SMark Johnston #include <netinet/in.h>
46*ecbbe831SMark Johnston #include <netinet/ip.h>
47*ecbbe831SMark Johnston #include <machine/in_cksum.h>
48*ecbbe831SMark Johnston
49*ecbbe831SMark Johnston /*
50*ecbbe831SMark Johnston * Checksum routine for Internet Protocol family headers
51*ecbbe831SMark Johnston * (Portable Alpha version).
52*ecbbe831SMark Johnston *
53*ecbbe831SMark Johnston * This routine is very heavily used in the network
54*ecbbe831SMark Johnston * code and should be modified for each CPU to be as fast as possible.
55*ecbbe831SMark Johnston */
56*ecbbe831SMark Johnston
57*ecbbe831SMark Johnston #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
58*ecbbe831SMark Johnston #define REDUCE32 \
59*ecbbe831SMark Johnston { \
60*ecbbe831SMark Johnston q_util.q = sum; \
61*ecbbe831SMark Johnston sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
62*ecbbe831SMark Johnston }
63*ecbbe831SMark Johnston #define REDUCE16 \
64*ecbbe831SMark Johnston { \
65*ecbbe831SMark Johnston q_util.q = sum; \
66*ecbbe831SMark Johnston l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
67*ecbbe831SMark Johnston sum = l_util.s[0] + l_util.s[1]; \
68*ecbbe831SMark Johnston ADDCARRY(sum); \
69*ecbbe831SMark Johnston }
70*ecbbe831SMark Johnston
71*ecbbe831SMark Johnston union l_util {
72*ecbbe831SMark Johnston u_int16_t s[2];
73*ecbbe831SMark Johnston u_int32_t l;
74*ecbbe831SMark Johnston };
75*ecbbe831SMark Johnston union q_util {
76*ecbbe831SMark Johnston u_int16_t s[4];
77*ecbbe831SMark Johnston u_int32_t l[2];
78*ecbbe831SMark Johnston u_int64_t q;
79*ecbbe831SMark Johnston };
80*ecbbe831SMark Johnston
81*ecbbe831SMark Johnston u_short
in_addword(u_short a,u_short b)82*ecbbe831SMark Johnston in_addword(u_short a, u_short b)
83*ecbbe831SMark Johnston {
84*ecbbe831SMark Johnston u_int64_t sum = a + b;
85*ecbbe831SMark Johnston
86*ecbbe831SMark Johnston ADDCARRY(sum);
87*ecbbe831SMark Johnston return (sum);
88*ecbbe831SMark Johnston }
89*ecbbe831SMark Johnston
90*ecbbe831SMark Johnston static
_do_cksum(void * addr,int len)91*ecbbe831SMark Johnston uint64_t _do_cksum(void *addr, int len)
92*ecbbe831SMark Johnston {
93*ecbbe831SMark Johnston uint64_t sum;
94*ecbbe831SMark Johnston union q_util q_util;
95*ecbbe831SMark Johnston
96*ecbbe831SMark Johnston sum = do_cksum(addr, len);
97*ecbbe831SMark Johnston REDUCE32;
98*ecbbe831SMark Johnston return (sum);
99*ecbbe831SMark Johnston }
100*ecbbe831SMark Johnston
101*ecbbe831SMark Johnston u_short
in_cksum_skip(struct mbuf * m,int len,int skip)102*ecbbe831SMark Johnston in_cksum_skip(struct mbuf *m, int len, int skip)
103*ecbbe831SMark Johnston {
104*ecbbe831SMark Johnston u_int64_t sum = 0;
105*ecbbe831SMark Johnston int mlen = 0;
106*ecbbe831SMark Johnston int clen = 0;
107*ecbbe831SMark Johnston caddr_t addr;
108*ecbbe831SMark Johnston union q_util q_util;
109*ecbbe831SMark Johnston union l_util l_util;
110*ecbbe831SMark Johnston
111*ecbbe831SMark Johnston len -= skip;
112*ecbbe831SMark Johnston for (; skip && m; m = m->m_next) {
113*ecbbe831SMark Johnston if (m->m_len > skip) {
114*ecbbe831SMark Johnston mlen = m->m_len - skip;
115*ecbbe831SMark Johnston addr = mtod(m, caddr_t) + skip;
116*ecbbe831SMark Johnston goto skip_start;
117*ecbbe831SMark Johnston } else {
118*ecbbe831SMark Johnston skip -= m->m_len;
119*ecbbe831SMark Johnston }
120*ecbbe831SMark Johnston }
121*ecbbe831SMark Johnston
122*ecbbe831SMark Johnston for (; m && len; m = m->m_next) {
123*ecbbe831SMark Johnston if (m->m_len == 0)
124*ecbbe831SMark Johnston continue;
125*ecbbe831SMark Johnston mlen = m->m_len;
126*ecbbe831SMark Johnston addr = mtod(m, caddr_t);
127*ecbbe831SMark Johnston skip_start:
128*ecbbe831SMark Johnston if (len < mlen)
129*ecbbe831SMark Johnston mlen = len;
130*ecbbe831SMark Johnston
131*ecbbe831SMark Johnston if ((clen ^ (int) addr) & 1)
132*ecbbe831SMark Johnston sum += _do_cksum(addr, mlen) << 8;
133*ecbbe831SMark Johnston else
134*ecbbe831SMark Johnston sum += _do_cksum(addr, mlen);
135*ecbbe831SMark Johnston
136*ecbbe831SMark Johnston clen += mlen;
137*ecbbe831SMark Johnston len -= mlen;
138*ecbbe831SMark Johnston }
139*ecbbe831SMark Johnston REDUCE16;
140*ecbbe831SMark Johnston return (~sum & 0xffff);
141*ecbbe831SMark Johnston }
142*ecbbe831SMark Johnston
in_cksum_hdr(const struct ip * ip)143*ecbbe831SMark Johnston u_int in_cksum_hdr(const struct ip *ip)
144*ecbbe831SMark Johnston {
145*ecbbe831SMark Johnston u_int64_t sum = do_cksum(ip, sizeof(struct ip));
146*ecbbe831SMark Johnston union q_util q_util;
147*ecbbe831SMark Johnston union l_util l_util;
148*ecbbe831SMark Johnston REDUCE16;
149*ecbbe831SMark Johnston return (~sum & 0xffff);
150*ecbbe831SMark Johnston }
151