Home | History | Annotate | Line # | Download | only in netinet6
in6_cksum.c revision 1.24
      1 /*	$NetBSD: in6_cksum.c,v 1.24 2008/02/12 13:05:55 joerg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 Joerg Sonnenberger <joerg (at) NetBSD.org>.
      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  *
     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
     15  *    the documentation and/or other materials provided with the
     16  *    distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: in6_cksum.c,v 1.24 2008/02/12 13:05:55 joerg Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/mbuf.h>
     37 #include <netinet/in.h>
     38 #include <netinet/ip6.h>
     39 
     40 /*
     41  * Checksum of the IPv6 pseudo header.
     42  *
     43  * off is supposed to be the skipped IPv6 header, len is the payload size.
     44  */
     45 
     46 int
     47 in6_cksum(struct mbuf *m, u_int8_t nxt, uint32_t off, uint32_t len)
     48 {
     49 	union {
     50 		uint16_t words[16];
     51 		struct {
     52 			struct in6_addr ip6_src;
     53 			struct in6_addr ip6_dst;
     54 		} addrs;
     55 	} u;
     56 	struct in6_addr *in6_src, *in6_dst;
     57 	uint32_t sum;
     58 	uint16_t *w;
     59 
     60 	if (__predict_false(off < sizeof(struct ip6_hdr)))
     61 		panic("in6_cksum: offset too short for IPv6 header");
     62 	if (__predict_false(m->m_len < sizeof(struct ip6_hdr)))
     63 		panic("in6_cksum: mbuf too short for IPv6 header");
     64 
     65 	if (nxt == 0)
     66 		return cpu_in_cksum(m, len, off, 0);
     67 
     68 	/*
     69 	 * Compute the equivalent of:
     70 	 * struct ip6_hdr_pseudo ip6;
     71 	 *
     72 	 * bzero(sizeof(*ip6));
     73 	 * ip6.ip6ph_nxt = nxt;
     74 	 * ip6.ip6ph_len = htonl(len);
     75 	 * ipv6.ip6ph_src = mtod(m, struct ip6_hdr *)->ip6_src;
     76 	 * in6_clearscope(&ip6->ip6ph_src);
     77 	 * ipv6.ip6ph_dst = mtod(m, struct ip6_hdr *)->ip6_dst;
     78 	 * in6_clearscope(&ip6->ip6ph_dst);
     79 	 * sum = one_add(&ip6);
     80 	 */
     81 
     82 #if BYTE_ORDER == LITTLE_ENDIAN
     83 	sum = ((len & 0xffff) + ((len >> 16) & 0xffff) + nxt) << 8;
     84 #else
     85 	sum = (len & 0xffff) + ((len >> 16) & 0xffff) + nxt;
     86 #endif
     87 	w = (uint16_t *)(mtod(m, char  *) + offsetof(struct ip6_hdr, ip6_src));
     88 	if (__predict_true((uintptr_t)w % 2 == 0)) {
     89 		in6_src = &mtod(m, struct ip6_hdr *)->ip6_src;
     90 		in6_dst = &mtod(m, struct ip6_hdr *)->ip6_dst;
     91 	} else {
     92 		memcpy(&u, &mtod(m, struct ip6_hdr *)->ip6_src, 32);
     93 		w = u.words;
     94 		in6_src = &u.addrs.ip6_src;
     95 		in6_dst = &u.addrs.ip6_dst;
     96 	}
     97 
     98 	sum += w[0];
     99 	if (!IN6_IS_SCOPE_LINKLOCAL(in6_src) &&
    100 	    !IN6_IS_ADDR_MC_INTFACELOCAL(in6_src))
    101 		sum += w[1];
    102 	sum += w[2];
    103 	sum += w[3];
    104 	sum += w[4];
    105 	sum += w[5];
    106 	sum += w[6];
    107 	sum += w[7];
    108 	w += 8;
    109 	sum += w[0];
    110 	if (!IN6_IS_SCOPE_LINKLOCAL(in6_dst) &&
    111 	    !IN6_IS_ADDR_MC_INTFACELOCAL(in6_dst))
    112 		sum += w[1];
    113 	sum += w[2];
    114 	sum += w[3];
    115 	sum += w[4];
    116 	sum += w[5];
    117 	sum += w[6];
    118 	sum += w[7];
    119 
    120 	return cpu_in_cksum(m, len, off, sum);
    121 }
    122