source: rtems/cpukit/libnetworking/netinet/in_cksum.c @ 05c1886

4.104.115
Last change on this file since 05c1886 was 05c1886, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/30/09 at 16:01:51

Whitespace removal.

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