Home | History | Annotate | Line # | Download | only in netinet6
in6_cksum.c revision 1.22
      1  1.22     joerg /*	$NetBSD: in6_cksum.c,v 1.22 2008/01/25 21:12:15 joerg Exp $	*/
      2   1.9    itojun /*	$KAME: in6_cksum.c,v 1.9 2000/09/09 15:33:31 itojun Exp $	*/
      3   1.3   thorpej 
      4   1.2    itojun /*
      5   1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.2    itojun  * All rights reserved.
      7   1.8    itojun  *
      8   1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.2    itojun  * modification, are permitted provided that the following conditions
     10   1.2    itojun  * are met:
     11   1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.2    itojun  *    may be used to endorse or promote products derived from this software
     18   1.2    itojun  *    without specific prior written permission.
     19   1.8    itojun  *
     20   1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.2    itojun  * SUCH DAMAGE.
     31   1.2    itojun  */
     32   1.2    itojun 
     33   1.2    itojun /*
     34   1.2    itojun  * Copyright (c) 1988, 1992, 1993
     35   1.2    itojun  *	The Regents of the University of California.  All rights reserved.
     36   1.2    itojun  *
     37   1.2    itojun  * Redistribution and use in source and binary forms, with or without
     38   1.2    itojun  * modification, are permitted provided that the following conditions
     39   1.2    itojun  * are met:
     40   1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     41   1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     42   1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     44   1.2    itojun  *    documentation and/or other materials provided with the distribution.
     45  1.16       agc  * 3. Neither the name of the University nor the names of its contributors
     46   1.2    itojun  *    may be used to endorse or promote products derived from this software
     47   1.2    itojun  *    without specific prior written permission.
     48   1.2    itojun  *
     49   1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50   1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51   1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52   1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53   1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54   1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55   1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56   1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57   1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58   1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59   1.2    itojun  * SUCH DAMAGE.
     60   1.2    itojun  *
     61   1.2    itojun  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
     62   1.2    itojun  */
     63  1.12     lukem 
     64  1.12     lukem #include <sys/cdefs.h>
     65  1.22     joerg __KERNEL_RCSID(0, "$NetBSD: in6_cksum.c,v 1.22 2008/01/25 21:12:15 joerg Exp $");
     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.7    itojun #include <netinet/ip6.h>
     72  1.18    rpaulo #include <netinet6/scope6_var.h>
     73   1.2    itojun 
     74   1.6    itojun #include <net/net_osdep.h>
     75   1.6    itojun 
     76   1.2    itojun /*
     77   1.2    itojun  * Checksum routine for Internet Protocol family headers (Portable Version).
     78   1.2    itojun  *
     79   1.2    itojun  * This routine is very heavily used in the network
     80   1.2    itojun  * code and should be modified for each CPU to be as fast as possible.
     81   1.2    itojun  */
     82   1.2    itojun 
     83   1.2    itojun #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
     84   1.2    itojun #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
     85   1.2    itojun 
     86   1.2    itojun /*
     87   1.2    itojun  * m MUST contain a continuous IP6 header.
     88   1.2    itojun  * off is a offset where TCP/UDP/ICMP6 header starts.
     89   1.2    itojun  * len is a total length of a transport segment.
     90   1.2    itojun  * (e.g. TCP header + TCP payload)
     91   1.2    itojun  */
     92   1.2    itojun 
     93   1.2    itojun int
     94  1.20  christos in6_cksum(struct mbuf *m, u_int8_t nxt, u_int32_t off, u_int32_t len)
     95   1.2    itojun {
     96  1.10    itojun 	u_int16_t *w;
     97  1.22     joerg 	uint32_t sum = 0;
     98  1.13    itojun 	struct ip6_hdr *ip6;
     99  1.18    rpaulo 	struct in6_addr in6;
    100   1.9    itojun 	union {
    101   1.9    itojun 		u_int16_t phs[4];
    102   1.9    itojun 		struct {
    103   1.9    itojun 			u_int32_t	ph_len;
    104   1.9    itojun 			u_int8_t	ph_zero[3];
    105   1.9    itojun 			u_int8_t	ph_nxt;
    106  1.21     perry 		} ph __packed;
    107   1.9    itojun 	} uph;
    108   1.2    itojun 
    109   1.2    itojun 	/* sanity check */
    110   1.2    itojun 	if (m->m_pkthdr.len < off + len) {
    111  1.14    provos 		panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
    112   1.2    itojun 			m->m_pkthdr.len, off, len);
    113   1.2    itojun 	}
    114   1.9    itojun 
    115  1.11   thorpej 	/* Skip pseudo-header if nxt == 0. */
    116  1.11   thorpej 	if (nxt == 0)
    117  1.11   thorpej 		goto skip_phdr;
    118  1.11   thorpej 
    119   1.9    itojun 	bzero(&uph, sizeof(uph));
    120   1.2    itojun 
    121   1.2    itojun 	/*
    122   1.2    itojun 	 * First create IP6 pseudo header and calculate a summary.
    123   1.2    itojun 	 */
    124   1.2    itojun 	ip6 = mtod(m, struct ip6_hdr *);
    125   1.5    itojun 	w = (u_int16_t *)&ip6->ip6_src;
    126   1.2    itojun 	uph.ph.ph_len = htonl(len);
    127   1.2    itojun 	uph.ph.ph_nxt = nxt;
    128   1.2    itojun 
    129  1.18    rpaulo 	/*
    130  1.18    rpaulo 	 * IPv6 source address
    131  1.18    rpaulo 	 * XXX: we'd like to avoid copying the address, but we can't due to
    132  1.18    rpaulo 	 * the possibly embedded scope zone ID.
    133  1.18    rpaulo 	 */
    134  1.18    rpaulo 	in6 = ip6->ip6_src;
    135  1.18    rpaulo 	in6_clearscope(&in6);
    136  1.18    rpaulo 	w = (u_int16_t *)&in6;
    137  1.18    rpaulo 	sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    138  1.18    rpaulo 	sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
    139  1.18    rpaulo 
    140   1.2    itojun 	/* IPv6 destination address */
    141  1.18    rpaulo 	in6 = ip6->ip6_dst;
    142  1.18    rpaulo 	in6_clearscope(&in6);
    143  1.18    rpaulo 	w = (u_int16_t *)&in6;
    144  1.18    rpaulo 	sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    145  1.18    rpaulo 	sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
    146  1.18    rpaulo 
    147   1.2    itojun 	/* Payload length and upper layer identifier */
    148   1.2    itojun 	sum += uph.phs[0];  sum += uph.phs[1];
    149   1.2    itojun 	sum += uph.phs[2];  sum += uph.phs[3];
    150   1.2    itojun 
    151  1.11   thorpej  skip_phdr:
    152  1.22     joerg 	return cpu_in_cksum(m, len, off, sum);
    153   1.2    itojun }
    154