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.3/sys/netinet6/in6_cksum.c 136588 2004-10-16 08:43:07Z cvs2svn $     */
    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  * 4. 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/param.h>
   65 #include <sys/mbuf.h>
   66 #include <sys/systm.h>
   67 #include <netinet/in.h>
   68 #include <netinet/ip6.h>
   69 
   70 #include <net/net_osdep.h>
   71 
   72 /*
   73  * Checksum routine for Internet Protocol family headers (Portable Version).
   74  *
   75  * This routine is very heavily used in the network
   76  * code and should be modified for each CPU to be as fast as possible.
   77  */
   78 
   79 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
   80 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
   81 
   82 /*
   83  * m MUST contain a continuous IP6 header.
   84  * off is an offset where TCP/UDP/ICMP6 header starts.
   85  * len is a total length of a transport segment.
   86  * (e.g. TCP header + TCP payload)
   87  */
   88 
   89 int
   90 in6_cksum(m, nxt, off, len)
   91         struct mbuf *m;
   92         u_int8_t nxt;
   93         u_int32_t off, len;
   94 {
   95         u_int16_t *w;
   96         int sum = 0;
   97         int mlen = 0;
   98         int byte_swapped = 0;
   99         struct ip6_hdr *ip6;
  100         union {
  101                 u_int16_t phs[4];
  102                 struct {
  103                         u_int32_t       ph_len;
  104                         u_int8_t        ph_zero[3];
  105                         u_int8_t        ph_nxt;
  106                 } ph __packed;
  107         } uph;
  108         union {
  109                 u_int8_t        c[2];
  110                 u_int16_t       s;
  111         } s_util;
  112         union {
  113                 u_int16_t s[2];
  114                 u_int32_t l;
  115         } l_util;
  116 
  117         /* sanity check */
  118         if (m->m_pkthdr.len < off + len) {
  119                 panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)",
  120                         m->m_pkthdr.len, off, len);
  121         }
  122 
  123         bzero(&uph, sizeof(uph));
  124 
  125         /*
  126          * First create IP6 pseudo header and calculate a summary.
  127          */
  128         ip6 = mtod(m, struct ip6_hdr *);
  129         w = (u_int16_t *)&ip6->ip6_src;
  130         uph.ph.ph_len = htonl(len);
  131         uph.ph.ph_nxt = nxt;
  132 
  133         /* IPv6 source address */
  134         sum += w[0];
  135         if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
  136                 sum += w[1];
  137         sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
  138         sum += w[6]; sum += w[7];
  139         /* IPv6 destination address */
  140         sum += w[8];
  141         if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
  142                 sum += w[9];
  143         sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13];
  144         sum += w[14]; sum += w[15];
  145         /* Payload length and upper layer identifier */
  146         sum += uph.phs[0];  sum += uph.phs[1];
  147         sum += uph.phs[2];  sum += uph.phs[3];
  148 
  149         /*
  150          * Secondly calculate a summary of the first mbuf excluding offset.
  151          */
  152         while (m != NULL && off > 0) {
  153                 if (m->m_len <= off)
  154                         off -= m->m_len;
  155                 else
  156                         break;
  157                 m = m->m_next;
  158         }
  159         w = (u_int16_t *)(mtod(m, u_char *) + off);
  160         mlen = m->m_len - off;
  161         if (len < mlen)
  162                 mlen = len;
  163         len -= mlen;
  164         /*
  165          * Force to even boundary.
  166          */
  167         if ((1 & (long) w) && (mlen > 0)) {
  168                 REDUCE;
  169                 sum <<= 8;
  170                 s_util.c[0] = *(u_char *)w;
  171                 w = (u_int16_t *)((char *)w + 1);
  172                 mlen--;
  173                 byte_swapped = 1;
  174         }
  175         /*
  176          * Unroll the loop to make overhead from
  177          * branches &c small.
  178          */
  179         while ((mlen -= 32) >= 0) {
  180                 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  181                 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
  182                 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
  183                 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
  184                 w += 16;
  185         }
  186         mlen += 32;
  187         while ((mlen -= 8) >= 0) {
  188                 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  189                 w += 4;
  190         }
  191         mlen += 8;
  192         if (mlen == 0 && byte_swapped == 0)
  193                 goto next;
  194         REDUCE;
  195         while ((mlen -= 2) >= 0) {
  196                 sum += *w++;
  197         }
  198         if (byte_swapped) {
  199                 REDUCE;
  200                 sum <<= 8;
  201                 byte_swapped = 0;
  202                 if (mlen == -1) {
  203                         s_util.c[1] = *(char *)w;
  204                         sum += s_util.s;
  205                         mlen = 0;
  206                 } else
  207                         mlen = -1;
  208         } else if (mlen == -1)
  209                 s_util.c[0] = *(char *)w;
  210  next:
  211         m = m->m_next;
  212 
  213         /*
  214          * Lastly calculate a summary of the rest of mbufs.
  215          */
  216 
  217         for (;m && len; m = m->m_next) {
  218                 if (m->m_len == 0)
  219                         continue;
  220                 w = mtod(m, u_int16_t *);
  221                 if (mlen == -1) {
  222                         /*
  223                          * The first byte of this mbuf is the continuation
  224                          * of a word spanning between this mbuf and the
  225                          * last mbuf.
  226                          *
  227                          * s_util.c[0] is already saved when scanning previous
  228                          * mbuf.
  229                          */
  230                         s_util.c[1] = *(char *)w;
  231                         sum += s_util.s;
  232                         w = (u_int16_t *)((char *)w + 1);
  233                         mlen = m->m_len - 1;
  234                         len--;
  235                 } else
  236                         mlen = m->m_len;
  237                 if (len < mlen)
  238                         mlen = len;
  239                 len -= mlen;
  240                 /*
  241                  * Force to even boundary.
  242                  */
  243                 if ((1 & (long) w) && (mlen > 0)) {
  244                         REDUCE;
  245                         sum <<= 8;
  246                         s_util.c[0] = *(u_char *)w;
  247                         w = (u_int16_t *)((char *)w + 1);
  248                         mlen--;
  249                         byte_swapped = 1;
  250                 }
  251                 /*
  252                  * Unroll the loop to make overhead from
  253                  * branches &c small.
  254                  */
  255                 while ((mlen -= 32) >= 0) {
  256                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  257                         sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
  258                         sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
  259                         sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
  260                         w += 16;
  261                 }
  262                 mlen += 32;
  263                 while ((mlen -= 8) >= 0) {
  264                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  265                         w += 4;
  266                 }
  267                 mlen += 8;
  268                 if (mlen == 0 && byte_swapped == 0)
  269                         continue;
  270                 REDUCE;
  271                 while ((mlen -= 2) >= 0) {
  272                         sum += *w++;
  273                 }
  274                 if (byte_swapped) {
  275                         REDUCE;
  276                         sum <<= 8;
  277                         byte_swapped = 0;
  278                         if (mlen == -1) {
  279                                 s_util.c[1] = *(char *)w;
  280                                 sum += s_util.s;
  281                                 mlen = 0;
  282                         } else
  283                                 mlen = -1;
  284                 } else if (mlen == -1)
  285                         s_util.c[0] = *(char *)w;
  286         }
  287         if (len)
  288                 panic("in6_cksum: out of data");
  289         if (mlen == -1) {
  290                 /* The last mbuf has odd # of bytes. Follow the
  291                    standard (the odd byte may be shifted left by 8 bits
  292                    or not as determined by endian-ness of the machine) */
  293                 s_util.c[1] = 0;
  294                 sum += s_util.s;
  295         }
  296         REDUCE;
  297         return (~sum & 0xffff);
  298 }

Cache object: 2325f7bbcc780e2b44b2d13a8bd0fbaf


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