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