xref: /openbsd-src/sys/arch/sparc64/sparc64/in4_cksum.c (revision fe1ad185fc0ddcddc04025602e8ef7ac6d75d4cc)
1 /*	$OpenBSD: in4_cksum.c,v 1.8 2022/02/01 15:30:10 miod Exp $	*/
2 /*	$NetBSD: in4_cksum.c,v 1.5 2003/10/13 14:22:20 agc Exp $ */
3 
4 /*
5  * Copyright (c) 1995 Matthew R. Green.
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * All advertising materials mentioning features or use of this software
10  * must display the following acknowledgement:
11  *	This product includes software developed by the University of
12  *	California, and its contributors.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)in_cksum.c	8.1 (Berkeley) 6/11/93
39  */
40 
41 /*
42  * Copyright (c) 2001 Eduardo Horvath.
43  * Copyright (c) 1995 Zubin Dittia.
44  * Copyright (c) 1994, 1998 Charles M. Hannum.
45  *
46  * All advertising materials mentioning features or use of this software
47  * must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, and its contributors.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
54  * 1. Redistributions of source code must retain the above copyright
55  *    notice, this list of conditions and the following disclaimer.
56  * 2. Redistributions in binary form must reproduce the above copyright
57  *    notice, this list of conditions and the following disclaimer in the
58  *    documentation and/or other materials provided with the distribution.
59  * 3. All advertising materials mentioning features or use of this software
60  *    must display the following acknowledgement:
61  *	This product includes software developed by the University of
62  *	California, Berkeley and its contributors.
63  * 4. Neither the name of the University nor the names of its contributors
64  *    may be used to endorse or promote products derived from this software
65  *    without specific prior written permission.
66  *
67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77  * SUCH DAMAGE.
78  *
79  *	@(#)in_cksum.c	8.1 (Berkeley) 6/11/93
80  */
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/mbuf.h>
85 #include <sys/socketvar.h>
86 
87 #include <netinet/in.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_var.h>
90 
91 extern int in_cksum_internal(struct mbuf *, int len, int offset, int sum);
92 
93 int
in4_cksum(struct mbuf * m,u_int8_t nxt,int off,int len)94 in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
95 {
96 	u_int sum = 0;
97 	struct ipovly ipov;
98 
99 	/*
100 	 * Declare three temporary registers for use by the asm code.  We
101 	 * allow the compiler to pick which specific machine registers to
102 	 * use, instead of hard-coding this in the asm code.
103 	 */
104 	u_int tmp1, tmp2;
105 
106 	if (nxt != 0) {
107 		/* pseudo header */
108 		ipov.ih_src = mtod(m, struct ip *)->ip_src;
109 		ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
110 		sum = ((u_int)nxt << 16) | htons(len);
111 		/* assumes sizeof(ipov) == 20 */
112 		__asm volatile(
113 			" lduw [%4 + 12], %1; "
114 			" lduw [%4 + 16], %2; "
115 			" add %0, %1, %0; "
116 			" add %0, %2, %0; "
117 			" srlx %0, 32, %2; "
118 			" add %0, %2, %0; "
119 			: "=r" (sum), "=&r" (tmp1), "=&r" (tmp2)
120 			: "0" (sum), "r" (&ipov));
121 	}
122 
123 	/* skip unnecessary part */
124 	while (m && off > 0) {
125 		if (m->m_len > off)
126 			break;
127 		off -= m->m_len;
128 		m = m->m_next;
129 	}
130 	return (in_cksum_internal(m, len, off, sum));
131 }
132