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