xref: /freebsd/sys/amd64/include/in_cksum.h (revision 920b965865cd72bbb131f4461feb176074ce164a)
11ebcad57SDoug Rabson /*-
21ebcad57SDoug Rabson  * Copyright (c) 1990 The Regents of the University of California.
31ebcad57SDoug Rabson  * All rights reserved.
41ebcad57SDoug Rabson  *
51ebcad57SDoug Rabson  * Redistribution and use in source and binary forms, with or without
61ebcad57SDoug Rabson  * modification, are permitted provided that the following conditions
71ebcad57SDoug Rabson  * are met:
81ebcad57SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
91ebcad57SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
101ebcad57SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
111ebcad57SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
121ebcad57SDoug Rabson  *    documentation and/or other materials provided with the distribution.
131ebcad57SDoug Rabson  * 4. Neither the name of the University nor the names of its contributors
141ebcad57SDoug Rabson  *    may be used to endorse or promote products derived from this software
151ebcad57SDoug Rabson  *    without specific prior written permission.
161ebcad57SDoug Rabson  *
171ebcad57SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181ebcad57SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191ebcad57SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201ebcad57SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211ebcad57SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221ebcad57SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231ebcad57SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241ebcad57SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251ebcad57SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261ebcad57SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271ebcad57SDoug Rabson  * SUCH DAMAGE.
281ebcad57SDoug Rabson  *
291ebcad57SDoug Rabson  *	from tahoe:	in_cksum.c	1.2	86/01/05
301ebcad57SDoug Rabson  *	from:		@(#)in_cksum.c	1.3 (Berkeley) 1/19/91
311ebcad57SDoug Rabson  *	from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp
321ebcad57SDoug Rabson  * $FreeBSD$
331ebcad57SDoug Rabson  */
341ebcad57SDoug Rabson 
351ebcad57SDoug Rabson #ifndef _MACHINE_IN_CKSUM_H_
361ebcad57SDoug Rabson #define	_MACHINE_IN_CKSUM_H_	1
371ebcad57SDoug Rabson 
38cf4e1c46SPeter Wemm #ifndef _SYS_CDEFS_H_
39cf4e1c46SPeter Wemm #error this file needs sys/cdefs.h as a prerequisite
40cf4e1c46SPeter Wemm #endif
41cf4e1c46SPeter Wemm 
421ebcad57SDoug Rabson #include <sys/cdefs.h>
431ebcad57SDoug Rabson 
441ebcad57SDoug Rabson #define in_cksum(m, len)	in_cksum_skip(m, len, 0)
451ebcad57SDoug Rabson 
46*920b9658SBjoern A. Zeeb #if defined(IPVERSION) && (IPVERSION == 4)
471ebcad57SDoug Rabson /*
481ebcad57SDoug Rabson  * It it useful to have an Internet checksum routine which is inlineable
491ebcad57SDoug Rabson  * and optimized specifically for the task of computing IP header checksums
501ebcad57SDoug Rabson  * in the normal case (where there are no options and the header length is
511ebcad57SDoug Rabson  * therefore always exactly five 32-bit words.
521ebcad57SDoug Rabson  */
53a5f50ef9SJoerg Wunsch #ifdef __CC_SUPPORTS___INLINE
541ebcad57SDoug Rabson 
551ebcad57SDoug Rabson static __inline void
561ebcad57SDoug Rabson in_cksum_update(struct ip *ip)
571ebcad57SDoug Rabson {
581ebcad57SDoug Rabson 	int __tmpsum;
591ebcad57SDoug Rabson 	__tmpsum = (int)ntohs(ip->ip_sum) + 256;
601ebcad57SDoug Rabson 	ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16));
611ebcad57SDoug Rabson }
621ebcad57SDoug Rabson 
631ebcad57SDoug Rabson #else
641ebcad57SDoug Rabson 
651ebcad57SDoug Rabson #define	in_cksum_update(ip) \
661ebcad57SDoug Rabson 	do { \
671ebcad57SDoug Rabson 		int __tmpsum; \
681ebcad57SDoug Rabson 		__tmpsum = (int)ntohs(ip->ip_sum) + 256; \
691ebcad57SDoug Rabson 		ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); \
701ebcad57SDoug Rabson 	} while(0)
711ebcad57SDoug Rabson 
721ebcad57SDoug Rabson #endif
73*920b9658SBjoern A. Zeeb #endif
741ebcad57SDoug Rabson 
751ebcad57SDoug Rabson #ifdef _KERNEL
76*920b9658SBjoern A. Zeeb #if defined(IPVERSION) && (IPVERSION == 4)
771ebcad57SDoug Rabson u_int in_cksum_hdr(const struct ip *ip);
78*920b9658SBjoern A. Zeeb #endif
791ebcad57SDoug Rabson u_short	in_addword(u_short sum, u_short b);
801ebcad57SDoug Rabson u_short	in_pseudo(u_int sum, u_int b, u_int c);
811ebcad57SDoug Rabson u_short	in_cksum_skip(struct mbuf *m, int len, int skip);
821ebcad57SDoug Rabson #endif
831ebcad57SDoug Rabson 
841ebcad57SDoug Rabson #endif /* _MACHINE_IN_CKSUM_H_ */
85