1*ca987d46SWarner Losh /* $NetBSD: in_cksum.c,v 1.6 2000/03/31 19:55:09 castor Exp $ */ 2*ca987d46SWarner Losh 3*ca987d46SWarner Losh /* 4*ca987d46SWarner Losh * Copyright (c) 1992 Regents of the University of California. 5*ca987d46SWarner Losh * All rights reserved. 6*ca987d46SWarner Losh * 7*ca987d46SWarner Losh * This software was developed by the Computer Systems Engineering group 8*ca987d46SWarner Losh * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9*ca987d46SWarner Losh * contributed to Berkeley. 10*ca987d46SWarner Losh * 11*ca987d46SWarner Losh * Redistribution and use in source and binary forms, with or without 12*ca987d46SWarner Losh * modification, are permitted provided that the following conditions 13*ca987d46SWarner Losh * are met: 14*ca987d46SWarner Losh * 1. Redistributions of source code must retain the above copyright 15*ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer. 16*ca987d46SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 17*ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer in the 18*ca987d46SWarner Losh * documentation and/or other materials provided with the distribution. 19*ca987d46SWarner Losh * 3. Neither the name of the University nor the names of its contributors 20*ca987d46SWarner Losh * may be used to endorse or promote products derived from this software 21*ca987d46SWarner Losh * without specific prior written permission. 22*ca987d46SWarner Losh * 23*ca987d46SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24*ca987d46SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25*ca987d46SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26*ca987d46SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27*ca987d46SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28*ca987d46SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29*ca987d46SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30*ca987d46SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31*ca987d46SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32*ca987d46SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33*ca987d46SWarner Losh * SUCH DAMAGE. 34*ca987d46SWarner Losh * 35*ca987d46SWarner Losh * @(#) Header: in_cksum.c,v 1.1 92/09/11 01:15:55 leres Exp (LBL) 36*ca987d46SWarner Losh */ 37*ca987d46SWarner Losh 38*ca987d46SWarner Losh #include <sys/cdefs.h> 39*ca987d46SWarner Losh __FBSDID("$FreeBSD$"); 40*ca987d46SWarner Losh 41*ca987d46SWarner Losh #include <sys/types.h> 42*ca987d46SWarner Losh #include <machine/endian.h> 43*ca987d46SWarner Losh 44*ca987d46SWarner Losh #include "stand.h" 45*ca987d46SWarner Losh 46*ca987d46SWarner Losh /* 47*ca987d46SWarner Losh * Checksum routine for Internet Protocol family headers. 48*ca987d46SWarner Losh * This routine is very heavily used in the network 49*ca987d46SWarner Losh * code and should be modified for each CPU to be as fast as possible. 50*ca987d46SWarner Losh * In particular, it should not be this one. 51*ca987d46SWarner Losh */ 52*ca987d46SWarner Losh int 53*ca987d46SWarner Losh in_cksum(p, len) 54*ca987d46SWarner Losh void *p; 55*ca987d46SWarner Losh int len; 56*ca987d46SWarner Losh { 57*ca987d46SWarner Losh int sum = 0, oddbyte = 0, v = 0; 58*ca987d46SWarner Losh u_char *cp = p; 59*ca987d46SWarner Losh 60*ca987d46SWarner Losh /* we assume < 2^16 bytes being summed */ 61*ca987d46SWarner Losh while (len > 0) { 62*ca987d46SWarner Losh if (oddbyte) { 63*ca987d46SWarner Losh sum += v + *cp++; 64*ca987d46SWarner Losh len--; 65*ca987d46SWarner Losh } 66*ca987d46SWarner Losh if (((long)cp & 1) == 0) { 67*ca987d46SWarner Losh while ((len -= 2) >= 0) { 68*ca987d46SWarner Losh sum += *(u_short *)cp; 69*ca987d46SWarner Losh cp += 2; 70*ca987d46SWarner Losh } 71*ca987d46SWarner Losh } else { 72*ca987d46SWarner Losh while ((len -= 2) >= 0) { 73*ca987d46SWarner Losh #if BYTE_ORDER == BIG_ENDIAN 74*ca987d46SWarner Losh sum += *cp++ << 8; 75*ca987d46SWarner Losh sum += *cp++; 76*ca987d46SWarner Losh #else 77*ca987d46SWarner Losh sum += *cp++; 78*ca987d46SWarner Losh sum += *cp++ << 8; 79*ca987d46SWarner Losh #endif 80*ca987d46SWarner Losh } 81*ca987d46SWarner Losh } 82*ca987d46SWarner Losh if ((oddbyte = len & 1) != 0) 83*ca987d46SWarner Losh #if BYTE_ORDER == BIG_ENDIAN 84*ca987d46SWarner Losh v = *cp << 8; 85*ca987d46SWarner Losh #else 86*ca987d46SWarner Losh v = *cp; 87*ca987d46SWarner Losh #endif 88*ca987d46SWarner Losh } 89*ca987d46SWarner Losh if (oddbyte) 90*ca987d46SWarner Losh sum += v; 91*ca987d46SWarner Losh sum = (sum >> 16) + (sum & 0xffff); /* add in accumulated carries */ 92*ca987d46SWarner Losh sum += sum >> 16; /* add potential last carry */ 93*ca987d46SWarner Losh return (0xffff & ~sum); 94*ca987d46SWarner Losh } 95