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