source: rtems/cpukit/libnetworking/netinet/in_cksum.c @ ea7fd1ec

4.104.114.84.95
Last change on this file since ea7fd1ec was ea7fd1ec, checked in by Joel Sherrill <joel.sherrill@…>, on 09/11/06 at 17:34:10

2006-09-11 Jerry Needell <jerry.needell@…>

  • libnetworking/netinet/in_cksum.c: Joel should have used int32_t not uint32_t when removing warnings.
  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 1988, 1992, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by the University of
16 *      California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
34 * $Id$
35 */
36
37#include <sys/param.h>
38#include <sys/mbuf.h>
39
40/*
41 *  Try to use a CPU specific version, then punt to the portable C one.
42 */
43
44
45#if (defined(__GNUC__) && defined(__arm__))
46
47#include "in_cksum_arm.h"
48
49#elif (defined(__GNUC__) && defined(__i386__))
50
51#include "in_cksum_i386.h"
52
53#elif (defined(__GNUC__) && (defined(__mc68000__) || defined(__m68k__)))
54
55#include "in_cksum_m68k.h"
56#elif (defined(__GNUC__) && defined(__PPC__))
57
58#include "in_cksum_powerpc.h"
59
60#else
61
62#include <stdio.h> /* for puts */
63
64/*
65 * Checksum routine for Internet Protocol family headers (Portable Version).
66 *
67 * This routine is very heavily used in the network
68 * code and should be modified for each CPU to be as fast as possible.
69 */
70
71#define ADDCARRY(x)  (x > 65535L ? x -= 65535L : x)
72#define REDUCE \
73  {l_util.l = sum; sum = l_util.s[0] + l_util.s[1];  ADDCARRY(sum);}
74
75int
76in_cksum(m, len)
77        register struct mbuf *m;
78        register uint32_t len;
79{
80        register u_short *w;
81        register int32_t sum = 0;
82        register int32_t mlen = 0;
83        int byte_swapped = 0;
84
85        union {
86                char    c[2];
87                u_short s;
88        } s_util;
89        union {
90                u_short s[2];
91                long    l;
92        } l_util;
93
94        for (;m && len; m = m->m_next) {
95                if (m->m_len == 0)
96                        continue;
97                w = mtod(m, u_short *);
98                if (mlen == -1) {
99                        /*
100                         * The first byte of this mbuf is the continuation
101                         * of a word spanning between this mbuf and the
102                         * last mbuf.
103                         *
104                         * s_util.c[0] is already saved when scanning previous
105                         * mbuf.
106                         */
107                        s_util.c[1] = *(char *)w;
108                        sum += s_util.s;
109                        w = (u_short *)((char *)w + 1);
110                        mlen = m->m_len - 1;
111                        len--;
112                } else
113                        mlen = m->m_len;
114                if (len < mlen)
115                        mlen = len;
116                len -= mlen;
117                /*
118                 * Force to even boundary.
119                 */
120                if ((1 & (intptr_t) w) && (mlen > 0)) {
121                        REDUCE;
122                        sum <<= 8;
123                        s_util.c[0] = *(u_char *)w;
124                        w = (u_short *)((char *)w + 1);
125                        mlen--;
126                        byte_swapped = 1;
127                }
128                /*
129                 * Unroll the loop to make overhead from
130                 * branches &c small.
131                 */
132                while ((mlen -= 32) >= 0) {
133                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
134                        sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
135                        sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
136                        sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
137                        w += 16;
138                }
139                mlen += 32;
140                while ((mlen -= 8) >= 0) {
141                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
142                        w += 4;
143                }
144                mlen += 8;
145                if (mlen == 0 && byte_swapped == 0)
146                        continue;
147                REDUCE;
148                while ((mlen -= 2) >= 0) {
149                        sum += *w++;
150                }
151                if (byte_swapped) {
152                        REDUCE;
153                        sum <<= 8;
154                        byte_swapped = 0;
155                        if (mlen == -1) {
156                                s_util.c[1] = *(char *)w;
157                                sum += s_util.s;
158                                mlen = 0;
159                        } else
160                                mlen = -1;
161                } else if (mlen == -1)
162                        s_util.c[0] = *(char *)w;
163        }
164        if (len)
165                puts("cksum: out of data");
166        if (mlen == -1) {
167                /* The last mbuf has odd # of bytes. Follow the
168                   standard (the odd byte may be shifted left by 8 bits
169                   or not as determined by endian-ness of the machine) */
170                s_util.c[1] = 0;
171                sum += s_util.s;
172        }
173        REDUCE;
174        return (~sum & 0xffff);
175}
176#endif
Note: See TracBrowser for help on using the repository browser.