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