197f588a7SGarrett Wollman /*- 297f588a7SGarrett Wollman * Copyright (c) 1990 The Regents of the University of California. 397f588a7SGarrett Wollman * All rights reserved. 497f588a7SGarrett Wollman * 597f588a7SGarrett Wollman * Redistribution and use in source and binary forms, with or without 697f588a7SGarrett Wollman * modification, are permitted provided that the following conditions 797f588a7SGarrett Wollman * are met: 897f588a7SGarrett Wollman * 1. Redistributions of source code must retain the above copyright 997f588a7SGarrett Wollman * notice, this list of conditions and the following disclaimer. 1097f588a7SGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright 1197f588a7SGarrett Wollman * notice, this list of conditions and the following disclaimer in the 1297f588a7SGarrett Wollman * documentation and/or other materials provided with the distribution. 1397f588a7SGarrett Wollman * 3. All advertising materials mentioning features or use of this software 1497f588a7SGarrett Wollman * must display the following acknowledgement: 1597f588a7SGarrett Wollman * This product includes software developed by the University of 1697f588a7SGarrett Wollman * California, Berkeley and its contributors. 1797f588a7SGarrett Wollman * 4. Neither the name of the University nor the names of its contributors 1897f588a7SGarrett Wollman * may be used to endorse or promote products derived from this software 1997f588a7SGarrett Wollman * without specific prior written permission. 2097f588a7SGarrett Wollman * 2197f588a7SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2297f588a7SGarrett Wollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2397f588a7SGarrett Wollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2497f588a7SGarrett Wollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2597f588a7SGarrett Wollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2697f588a7SGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2797f588a7SGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2897f588a7SGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2997f588a7SGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3097f588a7SGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3197f588a7SGarrett Wollman * SUCH DAMAGE. 3297f588a7SGarrett Wollman * 3397f588a7SGarrett Wollman * from tahoe: in_cksum.c 1.2 86/01/05 3497f588a7SGarrett Wollman * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91 3597f588a7SGarrett Wollman * from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp 3657bf258eSGarrett Wollman * $Id: in_cksum.h,v 1.4 1997/02/22 09:34:42 peter Exp $ 3797f588a7SGarrett Wollman */ 3897f588a7SGarrett Wollman 3997f588a7SGarrett Wollman #ifndef _MACHINE_IN_CKSUM_H_ 4097f588a7SGarrett Wollman #define _MACHINE_IN_CKSUM_H_ 1 4197f588a7SGarrett Wollman 4297f588a7SGarrett Wollman #include <sys/cdefs.h> 4397f588a7SGarrett Wollman 4497f588a7SGarrett Wollman /* 4597f588a7SGarrett Wollman * It it useful to have an Internet checksum routine which is inlineable 4697f588a7SGarrett Wollman * and optimized specifically for the task of computing IP header checksums 4797f588a7SGarrett Wollman * in the normal case (where there are no options and the header length is 4897f588a7SGarrett Wollman * therefore always exactly five 32-bit words. 4997f588a7SGarrett Wollman */ 5097f588a7SGarrett Wollman #ifdef __GNUC__ 5197f588a7SGarrett Wollman static __inline u_int 5297f588a7SGarrett Wollman in_cksum_hdr(const struct ip *ip) 5397f588a7SGarrett Wollman { 5497f588a7SGarrett Wollman register u_int sum = 0; 5597f588a7SGarrett Wollman 5697f588a7SGarrett Wollman #define ADD(n) __asm("addl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (ip)) 5797f588a7SGarrett Wollman #define ADDC(n) __asm("adcl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (ip)) 5897f588a7SGarrett Wollman #define MOP __asm("adcl $0, %0" : "=r" (sum) : "0" (sum)) 5997f588a7SGarrett Wollman 6097f588a7SGarrett Wollman ADD(0); 6197f588a7SGarrett Wollman ADDC(4); 6297f588a7SGarrett Wollman ADDC(8); 6397f588a7SGarrett Wollman ADDC(12); 6497f588a7SGarrett Wollman ADDC(16); 6597f588a7SGarrett Wollman MOP; 6657bf258eSGarrett Wollman #undef ADD 6757bf258eSGarrett Wollman #undef ADDC 6857bf258eSGarrett Wollman #undef MOP 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 } 75f66e235aSGarrett Wollman 76f66e235aSGarrett Wollman static __inline void 77f66e235aSGarrett Wollman in_cksum_update(struct ip *ip) 78f66e235aSGarrett Wollman { 79f66e235aSGarrett Wollman int __tmpsum; 80f66e235aSGarrett Wollman __tmpsum = (int)ntohs(ip->ip_sum) + 256; 81f66e235aSGarrett Wollman ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); 82f66e235aSGarrett Wollman } 83f66e235aSGarrett Wollman 8497f588a7SGarrett Wollman #else 8597f588a7SGarrett Wollman u_int in_cksum_hdr __P((const struct ip *)); 86f66e235aSGarrett Wollman #define in_cksum_update(ip) \ 87f66e235aSGarrett Wollman do { \ 88f66e235aSGarrett Wollman int __tmpsum; \ 89f66e235aSGarrett Wollman __tmpsum = (int)ntohs(ip->ip_sum) + 256; \ 90f66e235aSGarrett Wollman ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); \ 91f66e235aSGarrett Wollman } while(0) 92f66e235aSGarrett Wollman 9397f588a7SGarrett Wollman #endif 9497f588a7SGarrett Wollman 9557bf258eSGarrett Wollman typedef unsigned in_psum_t; 9657bf258eSGarrett Wollman #ifdef KERNEL 9757bf258eSGarrett Wollman in_psum_t in_cksum_partial(in_psum_t psum, const u_short *w, int len); 9857bf258eSGarrett Wollman int in_cksum_finalize(in_psum_t psum); 9957bf258eSGarrett Wollman #endif /* KERNEL */ 10057bf258eSGarrett Wollman 10197f588a7SGarrett Wollman #endif /* _MACHINE_IN_CKSUM_H_ */ 102