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 /*      $FreeBSD: releng/5.2/sys/netinet6/in6_cksum.c 120913 2003-10-08 18:26:08Z ume $ */
    2 /*      $KAME: in6_cksum.c,v 1.10 2000/12/03 00:53:59 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. All advertising materials mentioning features or use of this software
   46  *    must display the following acknowledgement:
   47  *      This product includes software developed by the University of
   48  *      California, Berkeley and its contributors.
   49  * 4. Neither the name of the University nor the names of its contributors
   50  *    may be used to endorse or promote products derived from this software
   51  *    without specific prior written permission.
   52  *
   53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   63  * SUCH DAMAGE.
   64  *
   65  *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
   66  */
   67 
   68 #include <sys/param.h>
   69 #include <sys/mbuf.h>
   70 #include <sys/systm.h>
   71 #include <netinet/in.h>
   72 #include <netinet/ip6.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 an 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         union {
  105                 u_int16_t phs[4];
  106                 struct {
  107                         u_int32_t       ph_len;
  108                         u_int8_t        ph_zero[3];
  109                         u_int8_t        ph_nxt;
  110                 } ph __packed;
  111         } uph;
  112         union {
  113                 u_int8_t        c[2];
  114                 u_int16_t       s;
  115         } s_util;
  116         union {
  117                 u_int16_t s[2];
  118                 u_int32_t l;
  119         } l_util;
  120 
  121         /* sanity check */
  122         if (m->m_pkthdr.len < off + len) {
  123                 panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
  124                         m->m_pkthdr.len, off, len);
  125         }
  126 
  127         bzero(&uph, sizeof(uph));
  128 
  129         /*
  130          * First create IP6 pseudo header and calculate a summary.
  131          */
  132         ip6 = mtod(m, struct ip6_hdr *);
  133         w = (u_int16_t *)&ip6->ip6_src;
  134         uph.ph.ph_len = htonl(len);
  135         uph.ph.ph_nxt = nxt;
  136 
  137         /* IPv6 source address */
  138         sum += w[0];
  139         if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
  140                 sum += w[1];
  141         sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
  142         sum += w[6]; sum += w[7];
  143         /* IPv6 destination address */
  144         sum += w[8];
  145         if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
  146                 sum += w[9];
  147         sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13];
  148         sum += w[14]; sum += w[15];
  149         /* Payload length and upper layer identifier */
  150         sum += uph.phs[0];  sum += uph.phs[1];
  151         sum += uph.phs[2];  sum += uph.phs[3];
  152 
  153         /*
  154          * Secondly calculate a summary of the first mbuf excluding offset.
  155          */
  156         while (m != NULL && off > 0) {
  157                 if (m->m_len <= off)
  158                         off -= m->m_len;
  159                 else
  160                         break;
  161                 m = m->m_next;
  162         }
  163         w = (u_int16_t *)(mtod(m, u_char *) + off);
  164         mlen = m->m_len - off;
  165         if (len < mlen)
  166                 mlen = len;
  167         len -= mlen;
  168         /*
  169          * Force to even boundary.
  170          */
  171         if ((1 & (long) w) && (mlen > 0)) {
  172                 REDUCE;
  173                 sum <<= 8;
  174                 s_util.c[0] = *(u_char *)w;
  175                 w = (u_int16_t *)((char *)w + 1);
  176                 mlen--;
  177                 byte_swapped = 1;
  178         }
  179         /*
  180          * Unroll the loop to make overhead from
  181          * branches &c small.
  182          */
  183         while ((mlen -= 32) >= 0) {
  184                 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  185                 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
  186                 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
  187                 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
  188                 w += 16;
  189         }
  190         mlen += 32;
  191         while ((mlen -= 8) >= 0) {
  192                 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  193                 w += 4;
  194         }
  195         mlen += 8;
  196         if (mlen == 0 && byte_swapped == 0)
  197                 goto next;
  198         REDUCE;
  199         while ((mlen -= 2) >= 0) {
  200                 sum += *w++;
  201         }
  202         if (byte_swapped) {
  203                 REDUCE;
  204                 sum <<= 8;
  205                 byte_swapped = 0;
  206                 if (mlen == -1) {
  207                         s_util.c[1] = *(char *)w;
  208                         sum += s_util.s;
  209                         mlen = 0;
  210                 } else
  211                         mlen = -1;
  212         } else if (mlen == -1)
  213                 s_util.c[0] = *(char *)w;
  214  next:
  215         m = m->m_next;
  216 
  217         /*
  218          * Lastly calculate a summary of the rest of mbufs.
  219          */
  220 
  221         for (;m && len; m = m->m_next) {
  222                 if (m->m_len == 0)
  223                         continue;
  224                 w = mtod(m, u_int16_t *);
  225                 if (mlen == -1) {
  226                         /*
  227                          * The first byte of this mbuf is the continuation
  228                          * of a word spanning between this mbuf and the
  229                          * last mbuf.
  230                          *
  231                          * s_util.c[0] is already saved when scanning previous
  232                          * mbuf.
  233                          */
  234                         s_util.c[1] = *(char *)w;
  235                         sum += s_util.s;
  236                         w = (u_int16_t *)((char *)w + 1);
  237                         mlen = m->m_len - 1;
  238                         len--;
  239                 } else
  240                         mlen = m->m_len;
  241                 if (len < mlen)
  242                         mlen = len;
  243                 len -= mlen;
  244                 /*
  245                  * Force to even boundary.
  246                  */
  247                 if ((1 & (long) w) && (mlen > 0)) {
  248                         REDUCE;
  249                         sum <<= 8;
  250                         s_util.c[0] = *(u_char *)w;
  251                         w = (u_int16_t *)((char *)w + 1);
  252                         mlen--;
  253                         byte_swapped = 1;
  254                 }
  255                 /*
  256                  * Unroll the loop to make overhead from
  257                  * branches &c small.
  258                  */
  259                 while ((mlen -= 32) >= 0) {
  260                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  261                         sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
  262                         sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
  263                         sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
  264                         w += 16;
  265                 }
  266                 mlen += 32;
  267                 while ((mlen -= 8) >= 0) {
  268                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  269                         w += 4;
  270                 }
  271                 mlen += 8;
  272                 if (mlen == 0 && byte_swapped == 0)
  273                         continue;
  274                 REDUCE;
  275                 while ((mlen -= 2) >= 0) {
  276                         sum += *w++;
  277                 }
  278                 if (byte_swapped) {
  279                         REDUCE;
  280                         sum <<= 8;
  281                         byte_swapped = 0;
  282                         if (mlen == -1) {
  283                                 s_util.c[1] = *(char *)w;
  284                                 sum += s_util.s;
  285                                 mlen = 0;
  286                         } else
  287                                 mlen = -1;
  288                 } else if (mlen == -1)
  289                         s_util.c[0] = *(char *)w;
  290         }
  291         if (len)
  292                 panic("in6_cksum: out of data");
  293         if (mlen == -1) {
  294                 /* The last mbuf has odd # of bytes. Follow the
  295                    standard (the odd byte may be shifted left by 8 bits
  296                    or not as determined by endian-ness of the machine) */
  297                 s_util.c[1] = 0;
  298                 sum += s_util.s;
  299         }
  300         REDUCE;
  301         return (~sum & 0xffff);
  302 }

Cache object: c18e01e643cab188fcce1b3763820b98


[ 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.