The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/in6_cksum.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 25cad6c10f4a45cdfd5088029e926115


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.