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/netiso/iso_chksum.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 /*      $NetBSD: iso_chksum.c,v 1.23 2007/03/04 06:03:32 christos Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1991, 1993
    5  *      The Regents of the University of California.  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  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)iso_chksum.c        8.1 (Berkeley) 6/10/93
   32  */
   33 
   34 /***********************************************************
   35                 Copyright IBM Corporation 1987
   36 
   37                       All Rights Reserved
   38 
   39 Permission to use, copy, modify, and distribute this software and its
   40 documentation for any purpose and without fee is hereby granted,
   41 provided that the above copyright notice appear in all copies and that
   42 both that copyright notice and this permission notice appear in
   43 supporting documentation, and that the name of IBM not be
   44 used in advertising or publicity pertaining to distribution of the
   45 software without specific, written prior permission.
   46 
   47 IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
   48 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
   49 IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
   50 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
   51 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
   52 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
   53 SOFTWARE.
   54 
   55 ******************************************************************/
   56 
   57 /*
   58  * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
   59  */
   60 /*
   61  * ISO CHECKSUM
   62  *
   63  * The checksum generation and check routines are here. The checksum is 2 bytes
   64  * such that the sum of all the bytes b(i) == 0 and the sum of i * b(i) == 0.
   65  * The whole thing is complicated by the fact that the data are in mbuf
   66  * chains. Furthermore, there is the possibility of wraparound in the running
   67  * sums after adding up 4102 octets.  In order to avoid doing a mod operation
   68  * after EACH add, we have restricted this implementation to negotiating a
   69  * maximum of 4096-octets per TPDU (for the transport layer). The routine
   70  * iso_check_csum doesn't need to know where the checksum octets are. The
   71  * routine iso_gen_csum takes a pointer to an mbuf chain (logically a chunk
   72  * of data), an offset into the chunk at which the 2 octets are to be
   73  * stuffed, and the length of the chunk.  The 2 octets have to be logically
   74  * adjacent, but may be physically located in separate mbufs.
   75  */
   76 
   77 #include <sys/cdefs.h>
   78 __KERNEL_RCSID(1, "$NetBSD: iso_chksum.c,v 1.23 2007/03/04 06:03:32 christos Exp $");
   79 
   80 #include "opt_iso.h"
   81 
   82 #ifdef ISO
   83 #include <sys/param.h>
   84 #include <sys/systm.h>
   85 #include <sys/mbuf.h>
   86 #include <sys/socket.h>
   87 
   88 #include <uvm/uvm_extern.h>
   89 
   90 #include <net/if.h>
   91 #include <netiso/argo_debug.h>
   92 #include <netiso/iso.h>
   93 #include <netiso/iso_var.h>
   94 #endif /* ISO */
   95 
   96 /*
   97  * FUNCTION:    iso_check_csum
   98  *
   99  * PURPOSE:     To check the checksum of the packet in the mbuf chain (m).
  100  *                      The total length of the packet is (len).
  101  *                      Called from tp_input() and clnp_intr()
  102  *
  103  * RETURNS:      TRUE (something non-zero) if there is a checksum error,
  104  *               FALSE if there was NO checksum error.
  105  *
  106  * SIDE EFFECTS:  none
  107  *
  108  * NOTES:        It might be possible to gain something by optimizing
  109  *               this routine (unrolling loops, etc). But it is such
  110  *               a horrible thing to fiddle with anyway, it probably
  111  *               isn't worth it.
  112  */
  113 int
  114 iso_check_csum(struct mbuf *m, int len)
  115 {
  116         u_char *p = mtod(m, u_char *);
  117         u_long c0 = 0, c1 = 0;
  118         int    i = 0;
  119         int             cum = 0;/* cumulative length */
  120         int             l;
  121 
  122         l = len;
  123         len = min(m->m_len, len);
  124         i = 0;
  125 
  126 #ifdef ARGO_DEBUG
  127         if (argo_debug[D_CHKSUM]) {
  128                 printf("iso_check_csum: m %p, l x%x, m->m_len x%x\n",
  129                     m, l, m->m_len);
  130         }
  131 #endif
  132 
  133         while (i < l) {
  134                 cum += len;
  135                 while (i < cum) {
  136                         c0 = c0 + *(p++);
  137                         c1 += c0;
  138                         i++;
  139                 }
  140                 if (i < l) {
  141                         m = m->m_next;
  142 #ifdef ARGO_DEBUG
  143                         if (argo_debug[D_CHKSUM]) {
  144                                 printf("iso_check_csum: new mbuf\n");
  145                                 if (l - i < m->m_len)
  146                                         printf(
  147                        "bad mbuf chain in check csum l 0x%x i 0x%x m_data %p",
  148                                                l, i, m->m_data);
  149                         }
  150 #endif
  151                         ASSERT(m != NULL);
  152                         len = min(m->m_len, l - i);
  153                         p = mtod(m, u_char *);
  154                 }
  155         }
  156         if (((int) c0 % 255) || ((int) c1 % 255)) {
  157 #ifdef ARGO_DEBUG
  158                 if (argo_debug[D_CHKSUM]) {
  159                         printf("BAD iso_check_csum l 0x%x cum 0x%x len 0x%x, i 0x%x",
  160                             l, cum, len, i);
  161                 }
  162 #endif
  163                 return ((int) c0 % 255) << 8 | ((int) c1 % 255);
  164         }
  165         return 0;
  166 }
  167 
  168 /*
  169  * FUNCTION:    iso_gen_csum
  170  *
  171  * PURPOSE:     To generate the checksum of the packet in the mbuf chain (m).
  172  *              The first of the 2 (logically) adjacent checksum bytes
  173  *              (x and y) go at offset (n).
  174  *              (n) is an offset relative to the beginning of the data,
  175  *              not the beginning of the mbuf.
  176  *              (l) is the length of the total mbuf chain's data.
  177  *              Called from tp_emit(), tp_error_emit()
  178  *              clnp_emit_er(), clnp_forward(), clnp_output().
  179  *
  180  * RETURNS:     Rien
  181  *
  182  * SIDE EFFECTS: Puts the 2 checksum bytes into the packet.
  183  *
  184  * NOTES:       Ditto the note for iso_check_csum().
  185  */
  186 
  187 void
  188 iso_gen_csum(
  189         struct mbuf    *m,
  190         int             n,      /* offset of 2 checksum bytes */
  191         int             l)
  192 {
  193         u_char *p = mtod(m, u_char *);
  194         int    c0 = 0, c1 = 0;
  195         int    i = 0;
  196         int             loc = n++, len = 0;     /* n is position, loc is
  197                                                  * offset */
  198         u_char         *xloc = NULL;
  199         u_char         *yloc = NULL;
  200         int             cum = 0;/* cum == cumulative length */
  201 
  202 #ifdef ARGO_DEBUG
  203         if (argo_debug[D_CHKSUM]) {
  204                 printf("enter gen csum m %p n 0x%x l 0x%x\n",
  205                     m, n - 1, l);
  206         }
  207 #endif
  208 
  209         while (i < l) {
  210                 len = min(m->m_len, PAGE_SIZE);
  211                 /* RAH: don't cksum more than l bytes */
  212                 len = min(len, l - i);
  213 
  214                 cum += len;
  215                 p = mtod(m, u_char *);
  216 
  217                 if (loc >= 0) {
  218                         if (loc < len) {
  219                                 xloc = loc + mtod(m, u_char *);
  220 #ifdef ARGO_DEBUG
  221                                 if (argo_debug[D_CHKSUM]) {
  222                                         printf("1: zeroing xloc %p loc 0x%x\n",
  223                                             xloc, loc);
  224                                 }
  225 #endif
  226                                 *xloc = (u_char) 0;
  227                                 if (loc + 1 < len) {
  228                                         /*
  229                                          * both xloc and yloc are in same
  230                                          * mbuf
  231                                          */
  232                                         yloc = 1 + xloc;
  233 #ifdef ARGO_DEBUG
  234                                         if (argo_debug[D_CHKSUM]) {
  235                                                 printf(
  236                                         "2: zeroing yloc %p loc 0x%x\n",
  237                                         yloc, loc);
  238                                         }
  239 #endif
  240                                         *yloc = (u_char) 0;
  241                                 } else {
  242                                         /* crosses boundary of mbufs */
  243                                         yloc = mtod(m->m_next, u_char *);
  244 #ifdef ARGO_DEBUG
  245                                         if (argo_debug[D_CHKSUM]) {
  246                                                 printf(
  247                                             "3: zeroing yloc %p \n", yloc);
  248                                         }
  249 #endif
  250                                         *yloc = (u_char) 0;
  251                                 }
  252                         }
  253                         loc -= len;
  254                 }
  255                 while (i < cum) {
  256                         c0 = (c0 + *p);
  257                         c1 += c0;
  258                         i++;
  259                         p++;
  260                 }
  261                 m = m->m_next;
  262         }
  263 #ifdef ARGO_DEBUG
  264         if (argo_debug[D_CHKSUM]) {
  265                 printf("gen csum final xloc %p yloc %p\n", xloc, yloc);
  266         }
  267 #endif
  268 
  269         c1 = (((c0 * (l - n)) - c1) % 255);
  270         if (xloc)
  271                 *xloc = (u_char) ((c1 < 0) ? c1 + 255 : c1);
  272 
  273         c1 = (-(int) (c1 + c0)) % 255;
  274         if (yloc)
  275                 *yloc = (u_char) (c1 < 0 ? c1 + 255 : c1);
  276 
  277 #ifdef ARGO_DEBUG
  278         if (argo_debug[D_CHKSUM]) {
  279                 printf("gen csum end \n");
  280         }
  281 #endif
  282 }
  283 
  284 /*
  285  * FUNCTION:    m_datalen
  286  *
  287  * PURPOSE:     returns length of the mbuf chain.
  288  *              used all over the iso code.
  289  *
  290  * RETURNS:     integer
  291  *
  292  * SIDE EFFECTS: none
  293  *
  294  * NOTES:
  295  */
  296 
  297 int
  298 m_datalen(struct mbuf *m)
  299 {
  300         int    datalen;
  301 
  302         for (datalen = 0; m; m = m->m_next)
  303                 datalen += m->m_len;
  304         return datalen;
  305 }
  306 
  307 int
  308 m_compress(struct mbuf *in, struct mbuf **out)
  309 {
  310         int    datalen = 0;
  311         int             s = splnet();
  312 
  313         if (in->m_next == NULL) {
  314                 *out = in;
  315 #ifdef ARGO_DEBUG
  316                 if (argo_debug[D_REQUEST]) {
  317                         printf("m_compress returning 0x%x: A\n", in->m_len);
  318                 }
  319 #endif
  320                 splx(s);
  321                 return in->m_len;
  322         }
  323         MGET((*out), M_DONTWAIT, MT_DATA);
  324         if ((*out) == NULL) {
  325                 *out = in;
  326 #ifdef ARGO_DEBUG
  327                 if (argo_debug[D_REQUEST]) {
  328                         printf("m_compress returning -1: B\n");
  329                 }
  330 #endif
  331                 splx(s);
  332                 return -1;
  333         }
  334         (*out)->m_len = 0;
  335         (*out)->m_nextpkt = NULL;
  336 
  337         while (in) {
  338 #ifdef ARGO_DEBUG
  339                 if (argo_debug[D_REQUEST]) {
  340                         printf("m_compress in %p *out %p\n", in, *out);
  341                         printf("m_compress in: len 0x%x, off %p\n",
  342                             in->m_len, in->m_data);
  343                         printf("m_compress *out: len 0x%x, off %p\n",
  344                             (*out)->m_len, (*out)->m_data);
  345                 }
  346 #endif
  347                 if (in->m_flags & M_EXT) {
  348                         ASSERT(in->m_len == 0);
  349                 }
  350                 if (in->m_len == 0) {
  351                         in = in->m_next;
  352                         continue;
  353                 }
  354                 if (((*out)->m_flags & M_EXT) == 0) {
  355                         int             len;
  356 
  357                         len = M_TRAILINGSPACE(*out);
  358                         len = min(len, in->m_len);
  359                         datalen += len;
  360 
  361 #ifdef ARGO_DEBUG
  362                         if (argo_debug[D_REQUEST]) {
  363                                 printf("m_compress copying len %d\n", len);
  364                         }
  365 #endif
  366                         memcpy(mtod((*out), char *) + (*out)->m_len,
  367                             mtod(in, void *), (unsigned) len);
  368 
  369                         (*out)->m_len += len;
  370                         in->m_len -= len;
  371                         continue;
  372                 } else {
  373                         /* (*out) is full */
  374                         if (((*out)->m_next = m_get(M_DONTWAIT, MT_DATA)) == NULL) {
  375                                 m_freem(*out);
  376                                 *out = in;
  377 #ifdef ARGO_DEBUG
  378                                 if (argo_debug[D_REQUEST]) {
  379                                         printf("m_compress returning -1: B\n");
  380                                 }
  381 #endif
  382                                 splx(s);
  383                                 return -1;
  384                         }
  385                         (*out)->m_len = 0;
  386                         (*out)->m_nextpkt = NULL;
  387                         *out = (*out)->m_next;
  388                 }
  389         }
  390         m_freem(in);
  391 #ifdef ARGO_DEBUG
  392         if (argo_debug[D_REQUEST]) {
  393                 printf("m_compress returning 0x%x: A\n", datalen);
  394         }
  395 #endif
  396         splx(s);
  397         return datalen;
  398 }

Cache object: f6e093003e359e69ac7d039d3661ebf1


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