Home | History | Annotate | Line # | Download | only in netinet6
in6_cksum.c revision 1.16
      1 /*	$NetBSD: in6_cksum.c,v 1.16 2003/08/07 16:33:24 agc Exp $	*/
      2 /*	$KAME: in6_cksum.c,v 1.9 2000/09/09 15:33:31 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1988, 1992, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: in6_cksum.c,v 1.16 2003/08/07 16:33:24 agc Exp $");
     66 
     67 #include <sys/param.h>
     68 #include <sys/mbuf.h>
     69 #include <sys/systm.h>
     70 #include <netinet/in.h>
     71 #include <netinet/ip6.h>
     72 
     73 #include <net/net_osdep.h>
     74 
     75 /*
     76  * Checksum routine for Internet Protocol family headers (Portable Version).
     77  *
     78  * This routine is very heavily used in the network
     79  * code and should be modified for each CPU to be as fast as possible.
     80  */
     81 
     82 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
     83 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
     84 
     85 /*
     86  * m MUST contain a continuous IP6 header.
     87  * off is a offset where TCP/UDP/ICMP6 header starts.
     88  * len is a total length of a transport segment.
     89  * (e.g. TCP header + TCP payload)
     90  */
     91 
     92 int
     93 in6_cksum(m, nxt, off, len)
     94 	struct mbuf *m;
     95 	u_int8_t nxt;
     96 	u_int32_t off, len;
     97 {
     98 	u_int16_t *w;
     99 	int sum = 0;
    100 	int mlen = 0;
    101 	int byte_swapped = 0;
    102 	struct ip6_hdr *ip6;
    103 	union {
    104 		u_int16_t phs[4];
    105 		struct {
    106 			u_int32_t	ph_len;
    107 			u_int8_t	ph_zero[3];
    108 			u_int8_t	ph_nxt;
    109 		} ph __attribute__((__packed__));
    110 	} uph;
    111 	union {
    112 		u_int8_t	c[2];
    113 		u_int16_t	s;
    114 	} s_util;
    115 	union {
    116 		u_int16_t s[2];
    117 		u_int32_t l;
    118 	} l_util;
    119 
    120 	/* sanity check */
    121 	if (m->m_pkthdr.len < off + len) {
    122 		panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
    123 			m->m_pkthdr.len, off, len);
    124 	}
    125 
    126 	/* Skip pseudo-header if nxt == 0. */
    127 	if (nxt == 0)
    128 		goto skip_phdr;
    129 
    130 	bzero(&uph, sizeof(uph));
    131 
    132 	/*
    133 	 * First create IP6 pseudo header and calculate a summary.
    134 	 */
    135 	ip6 = mtod(m, struct ip6_hdr *);
    136 	w = (u_int16_t *)&ip6->ip6_src;
    137 	uph.ph.ph_len = htonl(len);
    138 	uph.ph.ph_nxt = nxt;
    139 
    140 	/* IPv6 source address */
    141 	sum += w[0];
    142 	if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    143 		sum += w[1];
    144 	sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
    145 	sum += w[6]; sum += w[7];
    146 	/* IPv6 destination address */
    147 	sum += w[8];
    148 	if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    149 		sum += w[9];
    150 	sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13];
    151 	sum += w[14]; sum += w[15];
    152 	/* Payload length and upper layer identifier */
    153 	sum += uph.phs[0];  sum += uph.phs[1];
    154 	sum += uph.phs[2];  sum += uph.phs[3];
    155 
    156  skip_phdr:
    157 	/*
    158 	 * Secondly calculate a summary of the first mbuf excluding offset.
    159 	 */
    160 	while (m != NULL && off > 0) {
    161 		if (m->m_len <= off)
    162 			off -= m->m_len;
    163 		else
    164 			break;
    165 		m = m->m_next;
    166 	}
    167 	w = (u_int16_t *)(mtod(m, u_char *) + off);
    168 	mlen = m->m_len - off;
    169 	if (len < mlen)
    170 		mlen = len;
    171 	len -= mlen;
    172 	/*
    173 	 * Force to even boundary.
    174 	 */
    175 	if ((1 & (long) w) && (mlen > 0)) {
    176 		REDUCE;
    177 		sum <<= 8;
    178 		s_util.c[0] = *(u_char *)w;
    179 		w = (u_int16_t *)((char *)w + 1);
    180 		mlen--;
    181 		byte_swapped = 1;
    182 	}
    183 	/*
    184 	 * Unroll the loop to make overhead from
    185 	 * branches &c small.
    186 	 */
    187 	while ((mlen -= 32) >= 0) {
    188 		sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    189 		sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
    190 		sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
    191 		sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
    192 		w += 16;
    193 	}
    194 	mlen += 32;
    195 	while ((mlen -= 8) >= 0) {
    196 		sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    197 		w += 4;
    198 	}
    199 	mlen += 8;
    200 	if (mlen == 0 && byte_swapped == 0)
    201 		goto next;
    202 	REDUCE;
    203 	while ((mlen -= 2) >= 0) {
    204 		sum += *w++;
    205 	}
    206 	if (byte_swapped) {
    207 		REDUCE;
    208 		sum <<= 8;
    209 		byte_swapped = 0;
    210 		if (mlen == -1) {
    211 			s_util.c[1] = *(char *)w;
    212 			sum += s_util.s;
    213 			mlen = 0;
    214 		} else
    215 			mlen = -1;
    216 	} else if (mlen == -1)
    217 		s_util.c[0] = *(char *)w;
    218  next:
    219 	m = m->m_next;
    220 
    221 	/*
    222 	 * Lastly calculate a summary of the rest of mbufs.
    223 	 */
    224 
    225 	for (;m && len; m = m->m_next) {
    226 		if (m->m_len == 0)
    227 			continue;
    228 		w = mtod(m, u_int16_t *);
    229 		if (mlen == -1) {
    230 			/*
    231 			 * The first byte of this mbuf is the continuation
    232 			 * of a word spanning between this mbuf and the
    233 			 * last mbuf.
    234 			 *
    235 			 * s_util.c[0] is already saved when scanning previous
    236 			 * mbuf.
    237 			 */
    238 			s_util.c[1] = *(char *)w;
    239 			sum += s_util.s;
    240 			w = (u_int16_t *)((char *)w + 1);
    241 			mlen = m->m_len - 1;
    242 			len--;
    243 		} else
    244 			mlen = m->m_len;
    245 		if (len < mlen)
    246 			mlen = len;
    247 		len -= mlen;
    248 		/*
    249 		 * Force to even boundary.
    250 		 */
    251 		if ((1 & (long) w) && (mlen > 0)) {
    252 			REDUCE;
    253 			sum <<= 8;
    254 			s_util.c[0] = *(u_char *)w;
    255 			w = (u_int16_t *)((char *)w + 1);
    256 			mlen--;
    257 			byte_swapped = 1;
    258 		}
    259 		/*
    260 		 * Unroll the loop to make overhead from
    261 		 * branches &c small.
    262 		 */
    263 		while ((mlen -= 32) >= 0) {
    264 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    265 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
    266 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
    267 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
    268 			w += 16;
    269 		}
    270 		mlen += 32;
    271 		while ((mlen -= 8) >= 0) {
    272 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    273 			w += 4;
    274 		}
    275 		mlen += 8;
    276 		if (mlen == 0 && byte_swapped == 0)
    277 			continue;
    278 		REDUCE;
    279 		while ((mlen -= 2) >= 0) {
    280 			sum += *w++;
    281 		}
    282 		if (byte_swapped) {
    283 			REDUCE;
    284 			sum <<= 8;
    285 			byte_swapped = 0;
    286 			if (mlen == -1) {
    287 				s_util.c[1] = *(char *)w;
    288 				sum += s_util.s;
    289 				mlen = 0;
    290 			} else
    291 				mlen = -1;
    292 		} else if (mlen == -1)
    293 			s_util.c[0] = *(char *)w;
    294 	}
    295 	if (len)
    296 		panic("in6_cksum: out of data");
    297 	if (mlen == -1) {
    298 		/* The last mbuf has odd # of bytes. Follow the
    299 		   standard (the odd byte may be shifted left by 8 bits
    300 		   or not as determined by endian-ness of the machine) */
    301 		s_util.c[1] = 0;
    302 		sum += s_util.s;
    303 	}
    304 	REDUCE;
    305 	return (~sum & 0xffff);
    306 }
    307