source: rtems/cpukit/libnetworking/netinet/in_cksum.c @ 6e401331

4.104.115
Last change on this file since 6e401331 was b25b88e7, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/28/10 at 05:50:29

Add HAVE_CONFIG_H support to let files receive configure defines.

  • 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#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
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__) && !defined(__thumb__)))
46
47/* This currently does not support Thumb assembly */
48#include "in_cksum_arm.h"
49
50#elif (defined(__GNUC__) && defined(__i386__))
51
52#include "in_cksum_i386.h"
53
54#elif (defined(__GNUC__) && (defined(__mc68000__) || defined(__m68k__)))
55
56#include "in_cksum_m68k.h"
57#elif (defined(__GNUC__) && defined(__PPC__))
58
59#include "in_cksum_powerpc.h"
60
61#else
62
63#include <stdio.h> /* for puts */
64
65/*
66 * Checksum routine for Internet Protocol family headers (Portable Version).
67 *
68 * This routine is very heavily used in the network
69 * code and should be modified for each CPU to be as fast as possible.
70 */
71
72#define ADDCARRY(x)  (x > 65535L ? x -= 65535L : x)
73#define REDUCE \
74  {l_util.l = sum; sum = l_util.s[0] + l_util.s[1];  ADDCARRY(sum);}
75
76int
77in_cksum(
78        struct mbuf *m,
79        uint32_t len )
80{
81        register u_short *w;
82        register int32_t sum = 0;
83        register int32_t mlen = 0;
84        int byte_swapped = 0;
85
86        union {
87                char    c[2];
88                u_short s;
89        } s_util;
90        union {
91                u_short s[2];
92                long    l;
93        } l_util;
94
95        for (;m && len; m = m->m_next) {
96                if (m->m_len == 0)
97                        continue;
98                w = mtod(m, u_short *);
99                if (mlen == -1) {
100                        /*
101                         * The first byte of this mbuf is the continuation
102                         * of a word spanning between this mbuf and the
103                         * last mbuf.
104                         *
105                         * s_util.c[0] is already saved when scanning previous
106                         * mbuf.
107                         */
108                        s_util.c[1] = *(char *)w;
109                        sum += s_util.s;
110                        w = (u_short *)((char *)w + 1);
111                        mlen = m->m_len - 1;
112                        len--;
113                } else
114                        mlen = m->m_len;
115                if (len < mlen)
116                        mlen = len;
117                len -= mlen;
118                /*
119                 * Force to even boundary.
120                 */
121                if ((1 & (intptr_t) w) && (mlen > 0)) {
122                        REDUCE;
123                        sum <<= 8;
124                        s_util.c[0] = *(u_char *)w;
125                        w = (u_short *)((char *)w + 1);
126                        mlen--;
127                        byte_swapped = 1;
128                }
129                /*
130                 * Unroll the loop to make overhead from
131                 * branches &c small.
132                 */
133                while ((mlen -= 32) >= 0) {
134                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
135                        sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
136                        sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
137                        sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
138                        w += 16;
139                }
140                mlen += 32;
141                while ((mlen -= 8) >= 0) {
142                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
143                        w += 4;
144                }
145                mlen += 8;
146                if (mlen == 0 && byte_swapped == 0)
147                        continue;
148                REDUCE;
149                while ((mlen -= 2) >= 0) {
150                        sum += *w++;
151                }
152                if (byte_swapped) {
153                        REDUCE;
154                        sum <<= 8;
155                        byte_swapped = 0;
156                        if (mlen == -1) {
157                                s_util.c[1] = *(char *)w;
158                                sum += s_util.s;
159                                mlen = 0;
160                        } else
161                                mlen = -1;
162                } else if (mlen == -1)
163                        s_util.c[0] = *(char *)w;
164        }
165        if (len)
166                puts("cksum: out of data");
167        if (mlen == -1) {
168                /* The last mbuf has odd # of bytes. Follow the
169                   standard (the odd byte may be shifted left by 8 bits
170                   or not as determined by endian-ness of the machine) */
171                s_util.c[1] = 0;
172                sum += s_util.s;
173        }
174        REDUCE;
175        return (~sum & 0xffff);
176}
177#endif
Note: See TracBrowser for help on using the repository browser.