in_cksum.h revision 28270
115291Swollman/*- 215291Swollman * Copyright (c) 1990 The Regents of the University of California. 315291Swollman * All rights reserved. 415291Swollman * 515291Swollman * Redistribution and use in source and binary forms, with or without 615291Swollman * modification, are permitted provided that the following conditions 715291Swollman * are met: 815291Swollman * 1. Redistributions of source code must retain the above copyright 915291Swollman * notice, this list of conditions and the following disclaimer. 1015291Swollman * 2. Redistributions in binary form must reproduce the above copyright 1115291Swollman * notice, this list of conditions and the following disclaimer in the 1215291Swollman * documentation and/or other materials provided with the distribution. 1315291Swollman * 3. All advertising materials mentioning features or use of this software 1415291Swollman * must display the following acknowledgement: 1515291Swollman * This product includes software developed by the University of 1615291Swollman * California, Berkeley and its contributors. 1715291Swollman * 4. Neither the name of the University nor the names of its contributors 1815291Swollman * may be used to endorse or promote products derived from this software 1915291Swollman * without specific prior written permission. 2015291Swollman * 2115291Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2215291Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2315291Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2415291Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2515291Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2615291Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2715291Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2815291Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2915291Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3015291Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3115291Swollman * SUCH DAMAGE. 3215291Swollman * 3315291Swollman * from tahoe: in_cksum.c 1.2 86/01/05 3415291Swollman * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91 3515291Swollman * from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp 3628270Swollman * $Id: in_cksum.h,v 1.4 1997/02/22 09:34:42 peter Exp $ 3715291Swollman */ 3815291Swollman 3915291Swollman#ifndef _MACHINE_IN_CKSUM_H_ 4015291Swollman#define _MACHINE_IN_CKSUM_H_ 1 4115291Swollman 4215291Swollman#include <sys/cdefs.h> 4315291Swollman 4415291Swollman/* 4515291Swollman * It it useful to have an Internet checksum routine which is inlineable 4615291Swollman * and optimized specifically for the task of computing IP header checksums 4715291Swollman * in the normal case (where there are no options and the header length is 4815291Swollman * therefore always exactly five 32-bit words. 4915291Swollman */ 5015291Swollman#ifdef __GNUC__ 5115291Swollmanstatic __inline u_int 5215291Swollmanin_cksum_hdr(const struct ip *ip) 5315291Swollman{ 5415291Swollman register u_int sum = 0; 5515291Swollman 5615291Swollman#define ADD(n) __asm("addl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (ip)) 5715291Swollman#define ADDC(n) __asm("adcl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (ip)) 5815291Swollman#define MOP __asm("adcl $0, %0" : "=r" (sum) : "0" (sum)) 5915291Swollman 6015291Swollman ADD(0); 6115291Swollman ADDC(4); 6215291Swollman ADDC(8); 6315291Swollman ADDC(12); 6415291Swollman ADDC(16); 6515291Swollman MOP; 6628270Swollman#undef ADD 6728270Swollman#undef ADDC 6828270Swollman#undef MOP 6915291Swollman sum = (sum & 0xffff) + (sum >> 16); 7015291Swollman if (sum > 0xffff) 7115291Swollman sum -= 0xffff; 7215291Swollman 7315291Swollman return ~sum & 0xffff; 7415291Swollman} 7515884Swollman 7615884Swollmanstatic __inline void 7715884Swollmanin_cksum_update(struct ip *ip) 7815884Swollman{ 7915884Swollman int __tmpsum; 8015884Swollman __tmpsum = (int)ntohs(ip->ip_sum) + 256; 8115884Swollman ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); 8215884Swollman} 8315884Swollman 8415291Swollman#else 8515291Swollmanu_int in_cksum_hdr __P((const struct ip *)); 8615884Swollman#define in_cksum_update(ip) \ 8715884Swollman do { \ 8815884Swollman int __tmpsum; \ 8915884Swollman __tmpsum = (int)ntohs(ip->ip_sum) + 256; \ 9015884Swollman ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); \ 9115884Swollman } while(0) 9215884Swollman 9315291Swollman#endif 9415291Swollman 9528270Swollmantypedef unsigned in_psum_t; 9628270Swollman#ifdef KERNEL 9728270Swollmanin_psum_t in_cksum_partial(in_psum_t psum, const u_short *w, int len); 9828270Swollmanint in_cksum_finalize(in_psum_t psum); 9928270Swollman#endif /* KERNEL */ 10028270Swollman 10115291Swollman#endif /* _MACHINE_IN_CKSUM_H_ */ 102