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-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: iso_chksum.c,v 1.18 2003/08/07 16:33:36 agc 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.18 2003/08/07 16:33:36 agc 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(m, len)
  115         struct mbuf    *m;
  116         int             len;
  117 {
  118         u_char *p = mtod(m, u_char *);
  119         u_long c0 = 0, c1 = 0;
  120         int    i = 0;
  121         int             cum = 0;/* cumulative length */
  122         int             l;
  123 
  124         l = len;
  125         len = min(m->m_len, len);
  126         i = 0;
  127 
  128 #ifdef ARGO_DEBUG
  129         if (argo_debug[D_CHKSUM]) {
  130                 printf("iso_check_csum: m %p, l x%x, m->m_len x%x\n",
  131                     m, l, m->m_len);
  132         }
  133 #endif
  134 
  135         while (i < l) {
  136                 cum += len;
  137                 while (i < cum) {
  138                         c0 = c0 + *(p++);
  139                         c1 += c0;
  140                         i++;
  141                 }
  142                 if (i < l) {
  143                         m = m->m_next;
  144 #ifdef ARGO_DEBUG
  145                         if (argo_debug[D_CHKSUM]) {
  146                                 printf("iso_check_csum: new mbuf\n");
  147                                 if (l - i < m->m_len)
  148                                         printf(
  149                        "bad mbuf chain in check csum l 0x%x i 0x%x m_data %p",
  150                                                l, i, m->m_data);
  151                         }
  152 #endif
  153                         ASSERT(m != NULL);
  154                         len = min(m->m_len, l - i);
  155                         p = mtod(m, u_char *);
  156                 }
  157         }
  158         if (((int) c0 % 255) || ((int) c1 % 255)) {
  159 #ifdef ARGO_DEBUG
  160                 if (argo_debug[D_CHKSUM]) {
  161                         printf("BAD iso_check_csum l 0x%x cum 0x%x len 0x%x, i 0x%x",
  162                             l, cum, len, i);
  163                 }
  164 #endif
  165                 return ((int) c0 % 255) << 8 | ((int) c1 % 255);
  166         }
  167         return 0;
  168 }
  169 
  170 /*
  171  * FUNCTION:    iso_gen_csum
  172  *
  173  * PURPOSE:     To generate the checksum of the packet in the mbuf chain (m).
  174  *              The first of the 2 (logically) adjacent checksum bytes
  175  *              (x and y) go at offset (n).
  176  *              (n) is an offset relative to the beginning of the data,
  177  *              not the beginning of the mbuf.
  178  *              (l) is the length of the total mbuf chain's data.
  179  *              Called from tp_emit(), tp_error_emit()
  180  *              clnp_emit_er(), clnp_forward(), clnp_output().
  181  *
  182  * RETURNS:     Rien
  183  *
  184  * SIDE EFFECTS: Puts the 2 checksum bytes into the packet.
  185  *
  186  * NOTES:       Ditto the note for iso_check_csum().
  187  */
  188 
  189 void
  190 iso_gen_csum(m, n, l)
  191         struct mbuf    *m;
  192         int             n;      /* offset of 2 checksum bytes */
  193         int             l;
  194 {
  195         u_char *p = mtod(m, u_char *);
  196         int    c0 = 0, c1 = 0;
  197         int    i = 0;
  198         int             loc = n++, len = 0;     /* n is position, loc is
  199                                                  * offset */
  200         u_char         *xloc = NULL;
  201         u_char         *yloc = NULL;
  202         int             cum = 0;/* cum == cumulative length */
  203 
  204 #ifdef ARGO_DEBUG
  205         if (argo_debug[D_CHKSUM]) {
  206                 printf("enter gen csum m %p n 0x%x l 0x%x\n", 
  207                     m, n - 1, l);
  208         }
  209 #endif
  210 
  211         while (i < l) {
  212                 len = min(m->m_len, PAGE_SIZE);
  213                 /* RAH: don't cksum more than l bytes */
  214                 len = min(len, l - i);
  215 
  216                 cum += len;
  217                 p = mtod(m, u_char *);
  218 
  219                 if (loc >= 0) {
  220                         if (loc < len) {
  221                                 xloc = loc + mtod(m, u_char *);
  222 #ifdef ARGO_DEBUG
  223                                 if (argo_debug[D_CHKSUM]) {
  224                                         printf("1: zeroing xloc %p loc 0x%x\n",
  225                                             xloc, loc);
  226                                 }
  227 #endif
  228                                 *xloc = (u_char) 0;
  229                                 if (loc + 1 < len) {
  230                                         /*
  231                                          * both xloc and yloc are in same
  232                                          * mbuf
  233                                          */
  234                                         yloc = 1 + xloc;
  235 #ifdef ARGO_DEBUG
  236                                         if (argo_debug[D_CHKSUM]) {
  237                                                 printf(
  238                                         "2: zeroing yloc %p loc 0x%x\n",
  239                                         yloc, loc);
  240                                         }
  241 #endif
  242                                         *yloc = (u_char) 0;
  243                                 } else {
  244                                         /* crosses boundary of mbufs */
  245                                         yloc = mtod(m->m_next, u_char *);
  246 #ifdef ARGO_DEBUG
  247                                         if (argo_debug[D_CHKSUM]) {
  248                                                 printf(
  249                                             "3: zeroing yloc %p \n", yloc);
  250                                         }
  251 #endif
  252                                         *yloc = (u_char) 0;
  253                                 }
  254                         }
  255                         loc -= len;
  256                 }
  257                 while (i < cum) {
  258                         c0 = (c0 + *p);
  259                         c1 += c0;
  260                         i++;
  261                         p++;
  262                 }
  263                 m = m->m_next;
  264         }
  265 #ifdef ARGO_DEBUG
  266         if (argo_debug[D_CHKSUM]) {
  267                 printf("gen csum final xloc %p yloc %p\n", xloc, yloc);
  268         }
  269 #endif
  270 
  271         c1 = (((c0 * (l - n)) - c1) % 255);
  272         *xloc = (u_char) ((c1 < 0) ? c1 + 255 : c1);
  273 
  274         c1 = (-(int) (c1 + c0)) % 255;
  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(m)
  299         struct mbuf *m;
  300 {
  301         int    datalen;
  302 
  303         for (datalen = 0; m; m = m->m_next)
  304                 datalen += m->m_len;
  305         return datalen;
  306 }
  307 
  308 int
  309 m_compress(in, out)
  310         struct mbuf *in, **out;
  311 {
  312         int    datalen = 0;
  313         int             s = splnet();
  314 
  315         if (in->m_next == NULL) {
  316                 *out = in;
  317 #ifdef ARGO_DEBUG
  318                 if (argo_debug[D_REQUEST]) {
  319                         printf("m_compress returning 0x%x: A\n", in->m_len);
  320                 }
  321 #endif
  322                 splx(s);
  323                 return in->m_len;
  324         }
  325         MGET((*out), M_DONTWAIT, MT_DATA);
  326         if ((*out) == NULL) {
  327                 *out = in;
  328 #ifdef ARGO_DEBUG
  329                 if (argo_debug[D_REQUEST]) {
  330                         printf("m_compress returning -1: B\n");
  331                 }
  332 #endif
  333                 splx(s);
  334                 return -1;
  335         }
  336         (*out)->m_len = 0;
  337         (*out)->m_nextpkt = NULL;
  338 
  339         while (in) {
  340 #ifdef ARGO_DEBUG
  341                 if (argo_debug[D_REQUEST]) {
  342                         printf("m_compress in %p *out %p\n", in, *out);
  343                         printf("m_compress in: len 0x%x, off %p\n",
  344                             in->m_len, in->m_data);
  345                         printf("m_compress *out: len 0x%x, off %p\n",
  346                             (*out)->m_len, (*out)->m_data);
  347                 }
  348 #endif
  349                 if (in->m_flags & M_EXT) {
  350                         ASSERT(in->m_len == 0);
  351                 }
  352                 if (in->m_len == 0) {
  353                         in = in->m_next;
  354                         continue;
  355                 }
  356                 if (((*out)->m_flags & M_EXT) == 0) {
  357                         int             len;
  358 
  359                         len = M_TRAILINGSPACE(*out);
  360                         len = min(len, in->m_len);
  361                         datalen += len;
  362 
  363 #ifdef ARGO_DEBUG
  364                         if (argo_debug[D_REQUEST]) {
  365                                 printf("m_compress copying len %d\n", len);
  366                         }
  367 #endif
  368                         bcopy(mtod(in, caddr_t), mtod((*out), caddr_t) + (*out)->m_len,
  369                               (unsigned) len);
  370 
  371                         (*out)->m_len += len;
  372                         in->m_len -= len;
  373                         continue;
  374                 } else {
  375                         /* (*out) is full */
  376                         if (((*out)->m_next = m_get(M_DONTWAIT, MT_DATA)) == NULL) {
  377                                 m_freem(*out);
  378                                 *out = in;
  379 #ifdef ARGO_DEBUG
  380                                 if (argo_debug[D_REQUEST]) {
  381                                         printf("m_compress returning -1: B\n");
  382                                 }
  383 #endif
  384                                 splx(s);
  385                                 return -1;
  386                         }
  387                         (*out)->m_len = 0;
  388                         (*out)->m_nextpkt = NULL;
  389                         *out = (*out)->m_next;
  390                 }
  391         }
  392         m_freem(in);
  393 #ifdef ARGO_DEBUG
  394         if (argo_debug[D_REQUEST]) {
  395                 printf("m_compress returning 0x%x: A\n", datalen);
  396         }
  397 #endif
  398         splx(s);
  399         return datalen;
  400 }

Cache object: b757f915b6aa5cb2127a5f99ae39f123


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