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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 7a0d08c5eed9e298a1c5ab10e4cd6348


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