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