xref: /freebsd/sys/i386/include/in_cksum.h (revision 29363fb446372cb3f10bc98664e9767c53fbb457)
197f588a7SGarrett Wollman /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
497f588a7SGarrett Wollman  * Copyright (c) 1990 The Regents of the University of California.
597f588a7SGarrett Wollman  * All rights reserved.
697f588a7SGarrett Wollman  *
797f588a7SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
897f588a7SGarrett Wollman  * modification, are permitted provided that the following conditions
997f588a7SGarrett Wollman  * are met:
1097f588a7SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
1197f588a7SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
1297f588a7SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
1397f588a7SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
1497f588a7SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
15fc8c8560SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1697f588a7SGarrett Wollman  *    may be used to endorse or promote products derived from this software
1797f588a7SGarrett Wollman  *    without specific prior written permission.
1897f588a7SGarrett Wollman  *
1997f588a7SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2097f588a7SGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2197f588a7SGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2297f588a7SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2397f588a7SGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2497f588a7SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2597f588a7SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2697f588a7SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2797f588a7SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2897f588a7SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2997f588a7SGarrett Wollman  * SUCH DAMAGE.
3097f588a7SGarrett Wollman  *
3197f588a7SGarrett Wollman  *	from tahoe:	in_cksum.c	1.2	86/01/05
3297f588a7SGarrett Wollman  *	from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp
3397f588a7SGarrett Wollman  */
3497f588a7SGarrett Wollman 
3597f588a7SGarrett Wollman #ifndef _MACHINE_IN_CKSUM_H_
3697f588a7SGarrett Wollman #define	_MACHINE_IN_CKSUM_H_	1
3797f588a7SGarrett Wollman 
3897f588a7SGarrett Wollman #include <sys/cdefs.h>
3997f588a7SGarrett Wollman 
40cb1a6557SJonathan Lemon #define in_cksum(m, len)	in_cksum_skip(m, len, 0)
41cb1a6557SJonathan Lemon 
4297f588a7SGarrett Wollman /*
4397f588a7SGarrett Wollman  * It it useful to have an Internet checksum routine which is inlineable
4497f588a7SGarrett Wollman  * and optimized specifically for the task of computing IP header checksums
4597f588a7SGarrett Wollman  * in the normal case (where there are no options and the header length is
4697f588a7SGarrett Wollman  * therefore always exactly five 32-bit words.
4797f588a7SGarrett Wollman  */
48920b9658SBjoern A. Zeeb #if defined(IPVERSION) && (IPVERSION == 4)
4997f588a7SGarrett Wollman static __inline u_int
in_cksum_hdr(const struct ip * ip)5097f588a7SGarrett Wollman in_cksum_hdr(const struct ip *ip)
5197f588a7SGarrett Wollman {
524ec1748cSEd Schouten 	u_int sum = 0;
5397f588a7SGarrett Wollman 
544ec1748cSEd Schouten 	__asm(
552e262ac3SDavid E. O'Brien 		"addl %1, %0\n"
562e262ac3SDavid E. O'Brien 		"adcl %2, %0\n"
572e262ac3SDavid E. O'Brien 		"adcl %3, %0\n"
582e262ac3SDavid E. O'Brien 		"adcl %4, %0\n"
592e262ac3SDavid E. O'Brien 		"adcl %5, %0\n"
602e262ac3SDavid E. O'Brien 		"adcl $0, %0"
612e262ac3SDavid E. O'Brien 		: "+r" (sum)
622e262ac3SDavid E. O'Brien 		: "g" (((const u_int32_t *)ip)[0]),
632e262ac3SDavid E. O'Brien 		  "g" (((const u_int32_t *)ip)[1]),
642e262ac3SDavid E. O'Brien 		  "g" (((const u_int32_t *)ip)[2]),
652e262ac3SDavid E. O'Brien 		  "g" (((const u_int32_t *)ip)[3]),
662e262ac3SDavid E. O'Brien 		  "g" (((const u_int32_t *)ip)[4])
674ec1748cSEd Schouten 		: "cc"
682e262ac3SDavid E. O'Brien 	);
6997f588a7SGarrett Wollman 	sum = (sum & 0xffff) + (sum >> 16);
7097f588a7SGarrett Wollman 	if (sum > 0xffff)
7197f588a7SGarrett Wollman 		sum -= 0xffff;
7297f588a7SGarrett Wollman 
7397f588a7SGarrett Wollman 	return ~sum & 0xffff;
7497f588a7SGarrett Wollman }
75920b9658SBjoern A. Zeeb #endif
76f66e235aSGarrett Wollman 
77db4f9cc7SJonathan Lemon static __inline u_short
in_addword(u_short sum,u_short b)78db4f9cc7SJonathan Lemon in_addword(u_short sum, u_short b)
79db4f9cc7SJonathan Lemon {
804ec1748cSEd Schouten 	__asm(
812b3e7485SBjoern A. Zeeb 		"addw %1, %0\n"
822b3e7485SBjoern A. Zeeb 		"adcw $0, %0"
832b3e7485SBjoern A. Zeeb 		: "+r" (sum)
844ec1748cSEd Schouten 		: "g" (b)
854ec1748cSEd Schouten 		: "cc"
862b3e7485SBjoern A. Zeeb 	);
87db4f9cc7SJonathan Lemon 	return (sum);
88db4f9cc7SJonathan Lemon }
89db4f9cc7SJonathan Lemon 
90db4f9cc7SJonathan Lemon static __inline u_short
in_pseudo(u_int sum,u_int b,u_int c)91db4f9cc7SJonathan Lemon in_pseudo(u_int sum, u_int b, u_int c)
92db4f9cc7SJonathan Lemon {
934ec1748cSEd Schouten 	__asm(
942b3e7485SBjoern A. Zeeb 		"addl %1, %0\n"
952b3e7485SBjoern A. Zeeb 		"adcl %2, %0\n"
962b3e7485SBjoern A. Zeeb 		"adcl $0, %0"
972b3e7485SBjoern A. Zeeb 		: "+r" (sum)
982b3e7485SBjoern A. Zeeb 		: "g" (b),
992b3e7485SBjoern A. Zeeb 		  "g" (c)
1004ec1748cSEd Schouten 		: "cc"
1012b3e7485SBjoern A. Zeeb 	);
102db4f9cc7SJonathan Lemon 	sum = (sum & 0xffff) + (sum >> 16);
103db4f9cc7SJonathan Lemon 	if (sum > 0xffff)
104db4f9cc7SJonathan Lemon 		sum -= 0xffff;
105db4f9cc7SJonathan Lemon 	return (sum);
106db4f9cc7SJonathan Lemon }
10797f588a7SGarrett Wollman 
108664a31e4SPeter Wemm #ifdef _KERNEL
109*ecbbe831SMark Johnston 
110*ecbbe831SMark Johnston #define	HAVE_MD_IN_CKSUM
111*ecbbe831SMark Johnston 
112db4f9cc7SJonathan Lemon u_short in_cksum_skip(struct mbuf *m, int len, int skip);
113664a31e4SPeter Wemm #endif /* _KERNEL */
11457bf258eSGarrett Wollman 
11597f588a7SGarrett Wollman #endif /* _MACHINE_IN_CKSUM_H_ */
116