1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Copyright (c) 2004 Matthew Dillon, All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * from tahoe: in_cksum.c 1.2 86/01/05 36 * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91 37 * from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp 38 * $FreeBSD: src/sys/i386/include/in_cksum.h,v 1.7.2.2 2002/07/02 04:03:04 jdp Exp $ 39 * $DragonFly: src/sys/sys/in_cksum.h,v 1.1 2004/02/14 21:15:34 dillon Exp $ 40 */ 41 42 #ifndef _SYS_IN_CKSUM_H_ 43 #define _SYS_IN_CKSUM_H_ 44 45 #ifdef _KERNEL 46 47 struct ip; 48 struct mbuf; 49 50 __uint32_t in_cksum_range(struct mbuf *m, int offset, int bytes); 51 __uint32_t asm_ones32(const void *buf, int count); /* in 32 bit words */ 52 53 static __inline u_int 54 in_cksum(struct mbuf *m, int len) 55 { 56 return(in_cksum_range(m, 0, len)); 57 } 58 59 static __inline u_int 60 in_cksum_skip(struct mbuf *m, int len, int skip) 61 { 62 return(in_cksum_range(m, skip, len - skip)); 63 } 64 65 static __inline u_int 66 in_cksum_hdr(const struct ip *ip) 67 { 68 __uint32_t sum; 69 70 sum = asm_ones32((const void *)ip, 5); /* 5x4 = 20 bytes */ 71 sum = (sum >> 16) + (sum & 0xFFFF); 72 if (sum > 0xFFFF) 73 ++sum; 74 return(~sum & 0xFFFF); 75 } 76 77 #endif 78 79 static __inline u_short 80 in_addword(u_short sum, u_short b) 81 { 82 /* __volatile is necessary because the condition codes are used. */ 83 __asm __volatile ("addw %1, %0; adcw $0,%0" : "+r" (sum) : "r" (b)); 84 85 return (sum); 86 } 87 88 static __inline u_short 89 in_pseudo(u_int sum, u_int b, u_int c) 90 { 91 /* __volatile is necessary because the condition codes are used. */ 92 __asm __volatile ("addl %1,%0; adcl %2,%0; adcl $0,%0" 93 : "+r" (sum) 94 : "g" (b), "g" (c)); 95 sum = (sum & 0xffff) + (sum >> 16); 96 if (sum > 0xffff) 97 sum -= 0xffff; 98 return (sum); 99 } 100 101 #endif /* _MACHINE_IN_CKSUM_H_ */ 102