Home | History | Annotate | Line # | Download | only in netinet
in_cksum.c revision 1.16
      1 /*	$NetBSD: in_cksum.c,v 1.16 2002/06/09 16:33:38 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1992, 1993
      5  *	The Regents of the University of California.  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  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: in_cksum.c,v 1.16 2002/06/09 16:33:38 itojun Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/systm.h>
     44 #include <netinet/in.h>
     45 
     46 /*
     47  * Checksum routine for Internet Protocol family headers (Portable Version).
     48  *
     49  * This routine is very heavily used in the network
     50  * code and should be modified for each CPU to be as fast as possible.
     51  */
     52 
     53 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
     54 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
     55 
     56 int
     57 in_cksum(m, len)
     58 	struct mbuf *m;
     59 	int len;
     60 {
     61 	u_int16_t *w;
     62 	int sum = 0;
     63 	int mlen = 0;
     64 	int byte_swapped = 0;
     65 
     66 	union {
     67 		u_int8_t  c[2];
     68 		u_int16_t s;
     69 	} s_util;
     70 	union {
     71 		u_int16_t s[2];
     72 		u_int32_t l;
     73 	} l_util;
     74 
     75 	for (;m && len; m = m->m_next) {
     76 		if (m->m_len == 0)
     77 			continue;
     78 		w = mtod(m, u_int16_t *);
     79 		if (mlen == -1) {
     80 			/*
     81 			 * The first byte of this mbuf is the continuation
     82 			 * of a word spanning between this mbuf and the
     83 			 * last mbuf.
     84 			 *
     85 			 * s_util.c[0] is already saved when scanning previous
     86 			 * mbuf.
     87 			 */
     88 			s_util.c[1] = *(u_int8_t *)w;
     89 			sum += s_util.s;
     90 			w = (u_int16_t *)((u_int8_t *)w + 1);
     91 			mlen = m->m_len - 1;
     92 			len--;
     93 		} else
     94 			mlen = m->m_len;
     95 		if (len < mlen)
     96 			mlen = len;
     97 		len -= mlen;
     98 		/*
     99 		 * Force to even boundary.
    100 		 */
    101 		if ((1 & (long) w) && (mlen > 0)) {
    102 			REDUCE;
    103 			sum <<= 8;
    104 			s_util.c[0] = *(u_int8_t *)w;
    105 			w = (u_int16_t *)((int8_t *)w + 1);
    106 			mlen--;
    107 			byte_swapped = 1;
    108 		}
    109 		/*
    110 		 * Unroll the loop to make overhead from
    111 		 * branches &c small.
    112 		 */
    113 		while ((mlen -= 32) >= 0) {
    114 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    115 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
    116 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
    117 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
    118 			w += 16;
    119 		}
    120 		mlen += 32;
    121 		while ((mlen -= 8) >= 0) {
    122 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    123 			w += 4;
    124 		}
    125 		mlen += 8;
    126 		if (mlen == 0 && byte_swapped == 0)
    127 			continue;
    128 		REDUCE;
    129 		while ((mlen -= 2) >= 0) {
    130 			sum += *w++;
    131 		}
    132 		if (byte_swapped) {
    133 			REDUCE;
    134 			sum <<= 8;
    135 			byte_swapped = 0;
    136 			if (mlen == -1) {
    137 				s_util.c[1] = *(u_int8_t *)w;
    138 				sum += s_util.s;
    139 				mlen = 0;
    140 			} else
    141 				mlen = -1;
    142 		} else if (mlen == -1)
    143 			s_util.c[0] = *(u_int8_t *)w;
    144 	}
    145 	if (len)
    146 		printf("cksum: out of data\n");
    147 	if (mlen == -1) {
    148 		/* The last mbuf has odd # of bytes. Follow the
    149 		   standard (the odd byte may be shifted left by 8 bits
    150 		   or not as determined by endian-ness of the machine) */
    151 		s_util.c[1] = 0;
    152 		sum += s_util.s;
    153 	}
    154 	REDUCE;
    155 	return (~sum & 0xffff);
    156 }
    157