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
40 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
41 #include <sys/param.h>
42 #include <sys/mbuf.h>
43 #include <sys/systm.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <machine/in_cksum.h>
48
49 /*
50 * Checksum routine for Internet Protocol family headers
51 * (Portable Alpha version).
52 *
53 * This routine is very heavily used in the network
54 * code and should be modified for each CPU to be as fast as possible.
55 */
56
57 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
58 #define REDUCE32 \
59 { \
60 q_util.q = sum; \
61 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
62 }
63 #define REDUCE16 \
64 { \
65 q_util.q = sum; \
66 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
67 sum = l_util.s[0] + l_util.s[1]; \
68 ADDCARRY(sum); \
69 }
70
71 union l_util {
72 u_int16_t s[2];
73 u_int32_t l;
74 };
75 union q_util {
76 u_int16_t s[4];
77 u_int32_t l[2];
78 u_int64_t q;
79 };
80
81 u_short
in_addword(u_short a,u_short b)82 in_addword(u_short a, u_short b)
83 {
84 u_int64_t sum = a + b;
85
86 ADDCARRY(sum);
87 return (sum);
88 }
89
90 static
_do_cksum(void * addr,int len)91 uint64_t _do_cksum(void *addr, int len)
92 {
93 uint64_t sum;
94 union q_util q_util;
95
96 sum = do_cksum(addr, len);
97 REDUCE32;
98 return (sum);
99 }
100
101 u_short
in_cksum_skip(struct mbuf * m,int len,int skip)102 in_cksum_skip(struct mbuf *m, int len, int skip)
103 {
104 u_int64_t sum = 0;
105 int mlen = 0;
106 int clen = 0;
107 caddr_t addr;
108 union q_util q_util;
109 union l_util l_util;
110
111 len -= skip;
112 for (; skip && m; m = m->m_next) {
113 if (m->m_len > skip) {
114 mlen = m->m_len - skip;
115 addr = mtod(m, caddr_t) + skip;
116 goto skip_start;
117 } else {
118 skip -= m->m_len;
119 }
120 }
121
122 for (; m && len; m = m->m_next) {
123 if (m->m_len == 0)
124 continue;
125 mlen = m->m_len;
126 addr = mtod(m, caddr_t);
127 skip_start:
128 if (len < mlen)
129 mlen = len;
130
131 if ((clen ^ (int) addr) & 1)
132 sum += _do_cksum(addr, mlen) << 8;
133 else
134 sum += _do_cksum(addr, mlen);
135
136 clen += mlen;
137 len -= mlen;
138 }
139 REDUCE16;
140 return (~sum & 0xffff);
141 }
142
in_cksum_hdr(const struct ip * ip)143 u_int in_cksum_hdr(const struct ip *ip)
144 {
145 u_int64_t sum = do_cksum(ip, sizeof(struct ip));
146 union q_util q_util;
147 union l_util l_util;
148 REDUCE16;
149 return (~sum & 0xffff);
150 }
151