Home | History | Annotate | Line # | Download | only in netinet
in4_cksum.c revision 1.10
      1 /*	$NetBSD: in4_cksum.c,v 1.10 2003/08/07 16:33:10 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1999 WIDE Project.
      5  * 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. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1988, 1992, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.10 2003/08/07 16:33:10 agc Exp $");
     65 
     66 #include <sys/param.h>
     67 #include <sys/mbuf.h>
     68 #include <sys/systm.h>
     69 #include <sys/socket.h>
     70 #include <net/route.h>
     71 #include <netinet/in.h>
     72 #include <netinet/in_systm.h>
     73 #include <netinet/ip.h>
     74 #include <netinet/ip_var.h>
     75 
     76 /*
     77  * Checksum routine for Internet Protocol family headers (Portable Version).
     78  * This is only for IPv4 pseudo header checksum.
     79  * No need to clear non-pseudo-header fields in IPv4 header.
     80  * len is for actual payload size, and does not include IPv4 header and
     81  * skipped header chain (off + len should be equal to the whole packet).
     82  *
     83  * This routine is very heavily used in the network
     84  * code and should be modified for each CPU to be as fast as possible.
     85  */
     86 
     87 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
     88 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
     89 
     90 int
     91 in4_cksum(m, nxt, off, len)
     92 	struct mbuf *m;
     93 	u_int8_t nxt;
     94 	int off, len;
     95 {
     96 	u_int16_t *w;
     97 	int sum = 0;
     98 	int mlen = 0;
     99 	int byte_swapped = 0;
    100 	union {
    101 		struct ipovly ipov;
    102 		u_int16_t w[10];
    103 	} u;
    104 	union {
    105 		u_int8_t  c[2];
    106 		u_int16_t s;
    107 	} s_util;
    108 	union {
    109 		u_int16_t s[2];
    110 		u_int32_t l;
    111 	} l_util;
    112 
    113 	if (nxt != 0) {
    114 		/* pseudo header */
    115 		if (off < sizeof(struct ipovly))
    116 			panic("in4_cksum: offset too short");
    117 		if (m->m_len < sizeof(struct ip))
    118 			panic("in4_cksum: bad mbuf chain");
    119 		bzero(&u.ipov, sizeof(u.ipov));
    120 		u.ipov.ih_len = htons(len);
    121 		u.ipov.ih_pr = nxt;
    122 		u.ipov.ih_src = mtod(m, struct ip *)->ip_src;
    123 		u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
    124 		w = u.w;
    125 		/* assumes sizeof(ipov) == 20 */
    126 		sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];
    127 		sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];
    128 	}
    129 
    130 	/* skip unnecessary part */
    131 	while (m && off > 0) {
    132 		if (m->m_len > off)
    133 			break;
    134 		off -= m->m_len;
    135 		m = m->m_next;
    136 	}
    137 
    138 	for (;m && len; m = m->m_next) {
    139 		if (m->m_len == 0)
    140 			continue;
    141 		w = (u_int16_t *)(mtod(m, caddr_t) + off);
    142 		if (mlen == -1) {
    143 			/*
    144 			 * The first byte of this mbuf is the continuation
    145 			 * of a word spanning between this mbuf and the
    146 			 * last mbuf.
    147 			 *
    148 			 * s_util.c[0] is already saved when scanning previous
    149 			 * mbuf.
    150 			 */
    151 			s_util.c[1] = *(u_int8_t *)w;
    152 			sum += s_util.s;
    153 			w = (u_int16_t *)((u_int8_t *)w + 1);
    154 			mlen = m->m_len - off - 1;
    155 			len--;
    156 		} else
    157 			mlen = m->m_len - off;
    158 		off = 0;
    159 		if (len < mlen)
    160 			mlen = len;
    161 		len -= mlen;
    162 		/*
    163 		 * Force to even boundary.
    164 		 */
    165 		if ((1 & (long) w) && (mlen > 0)) {
    166 			REDUCE;
    167 			sum <<= 8;
    168 			s_util.c[0] = *(u_int8_t *)w;
    169 			w = (u_int16_t *)((int8_t *)w + 1);
    170 			mlen--;
    171 			byte_swapped = 1;
    172 		}
    173 		/*
    174 		 * Unroll the loop to make overhead from
    175 		 * branches &c small.
    176 		 */
    177 		while ((mlen -= 32) >= 0) {
    178 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    179 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
    180 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
    181 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
    182 			w += 16;
    183 		}
    184 		mlen += 32;
    185 		while ((mlen -= 8) >= 0) {
    186 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    187 			w += 4;
    188 		}
    189 		mlen += 8;
    190 		if (mlen == 0 && byte_swapped == 0)
    191 			continue;
    192 		REDUCE;
    193 		while ((mlen -= 2) >= 0) {
    194 			sum += *w++;
    195 		}
    196 		if (byte_swapped) {
    197 			REDUCE;
    198 			sum <<= 8;
    199 			byte_swapped = 0;
    200 			if (mlen == -1) {
    201 				s_util.c[1] = *(u_int8_t *)w;
    202 				sum += s_util.s;
    203 				mlen = 0;
    204 			} else
    205 				mlen = -1;
    206 		} else if (mlen == -1)
    207 			s_util.c[0] = *(u_int8_t *)w;
    208 	}
    209 	if (len)
    210 		printf("cksum4: out of data\n");
    211 	if (mlen == -1) {
    212 		/* The last mbuf has odd # of bytes. Follow the
    213 		   standard (the odd byte may be shifted left by 8 bits
    214 		   or not as determined by endian-ness of the machine) */
    215 		s_util.c[1] = 0;
    216 		sum += s_util.s;
    217 	}
    218 	REDUCE;
    219 	return (~sum & 0xffff);
    220 }
    221