OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [bsd_tcpip/] [v2_0/] [src/] [sys/] [netinet6/] [in6_cksum.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/sys/netinet6/in6_cksum.c
4
//
5
//==========================================================================
6
//####BSDCOPYRIGHTBEGIN####
7
//
8
// -------------------------------------------
9
//
10
// Portions of this software may have been derived from OpenBSD, 
11
// FreeBSD or other sources, and are covered by the appropriate
12
// copyright disclaimers included herein.
13
//
14
// Portions created by Red Hat are
15
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16
//
17
// -------------------------------------------
18
//
19
//####BSDCOPYRIGHTEND####
20
//==========================================================================
21
 
22
/*      $KAME: in6_cksum.c,v 1.10 2000/12/03 00:53:59 itojun Exp $      */
23
 
24
/*
25
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
26
 * All rights reserved.
27
 *
28
 * Redistribution and use in source and binary forms, with or without
29
 * modification, are permitted provided that the following conditions
30
 * are met:
31
 * 1. Redistributions of source code must retain the above copyright
32
 *    notice, this list of conditions and the following disclaimer.
33
 * 2. Redistributions in binary form must reproduce the above copyright
34
 *    notice, this list of conditions and the following disclaimer in the
35
 *    documentation and/or other materials provided with the distribution.
36
 * 3. Neither the name of the project nor the names of its contributors
37
 *    may be used to endorse or promote products derived from this software
38
 *    without specific prior written permission.
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 */
52
 
53
/*
54
 * Copyright (c) 1988, 1992, 1993
55
 *      The Regents of the University of California.  All rights reserved.
56
 *
57
 * Redistribution and use in source and binary forms, with or without
58
 * modification, are permitted provided that the following conditions
59
 * are met:
60
 * 1. Redistributions of source code must retain the above copyright
61
 *    notice, this list of conditions and the following disclaimer.
62
 * 2. Redistributions in binary form must reproduce the above copyright
63
 *    notice, this list of conditions and the following disclaimer in the
64
 *    documentation and/or other materials provided with the distribution.
65
 * 3. All advertising materials mentioning features or use of this software
66
 *    must display the following acknowledgement:
67
 *      This product includes software developed by the University of
68
 *      California, Berkeley and its contributors.
69
 * 4. Neither the name of the University nor the names of its contributors
70
 *    may be used to endorse or promote products derived from this software
71
 *    without specific prior written permission.
72
 *
73
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83
 * SUCH DAMAGE.
84
 *
85
 *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
86
 */
87
 
88
#include <sys/param.h>
89
#include <sys/mbuf.h>
90
#include <netinet/in.h>
91
#include <netinet/ip6.h>
92
 
93
/*
94
 * Checksum routine for Internet Protocol family headers (Portable Version).
95
 *
96
 * This routine is very heavily used in the network
97
 * code and should be modified for each CPU to be as fast as possible.
98
 */
99
 
100
#define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
101
#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
102
 
103
/*
104
 * m MUST contain a continuous IP6 header.
105
 * off is a offset where TCP/UDP/ICMP6 header starts.
106
 * len is a total length of a transport segment.
107
 * (e.g. TCP header + TCP payload)
108
 */
109
 
110
int
111
in6_cksum(m, nxt, off, len)
112
        struct mbuf *m;
113
        u_int8_t nxt;
114
        u_int32_t off, len;
115
{
116
        u_int16_t *w;
117
        int sum = 0;
118
        int mlen = 0;
119
        int byte_swapped = 0;
120
#if 0
121
        int srcifid = 0, dstifid = 0;
122
#endif
123
        struct ip6_hdr *ip6;
124
        union {
125
                u_int16_t phs[4];
126
                struct {
127
                        u_int32_t       ph_len;
128
                        u_int8_t        ph_zero[3];
129
                        u_int8_t        ph_nxt;
130
                } ph __attribute__((__packed__));
131
        } uph;
132
        union {
133
                u_int8_t        c[2];
134
                u_int16_t       s;
135
        } s_util;
136
        union {
137
                u_int16_t s[2];
138
                u_int32_t l;
139
        } l_util;
140
 
141
        /* sanity check */
142
        if (m->m_pkthdr.len < off + len) {
143
                panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)\n",
144
                        m->m_pkthdr.len, off, len);
145
        }
146
 
147
        bzero(&uph, sizeof(uph));
148
 
149
        /*
150
         * First create IP6 pseudo header and calculate a summary.
151
         */
152
        ip6 = mtod(m, struct ip6_hdr *);
153
#if 0
154
        if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
155
                srcifid = ip6->ip6_src.s6_addr16[1];
156
                ip6->ip6_src.s6_addr16[1] = 0;
157
        }
158
        if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
159
                dstifid = ip6->ip6_dst.s6_addr16[1];
160
                ip6->ip6_dst.s6_addr16[1] = 0;
161
        }
162
#endif
163
        w = (u_int16_t *)&ip6->ip6_src;
164
        uph.ph.ph_len = htonl(len);
165
        uph.ph.ph_nxt = nxt;
166
 
167
        /* IPv6 source address */
168
        sum += w[0];
169
        if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
170
                sum += w[1];
171
        sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
172
        sum += w[6]; sum += w[7];
173
        /* IPv6 destination address */
174
        sum += w[8];
175
        if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
176
                sum += w[9];
177
        sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13];
178
        sum += w[14]; sum += w[15];
179
        /* Payload length and upper layer identifier */
180
        sum += uph.phs[0];  sum += uph.phs[1];
181
        sum += uph.phs[2];  sum += uph.phs[3];
182
 
183
#if 0
184
        if (srcifid)
185
                ip6->ip6_src.s6_addr16[1] = srcifid;
186
        if (dstifid)
187
                ip6->ip6_dst.s6_addr16[1] = dstifid;
188
#endif
189
        /*
190
         * Secondly calculate a summary of the first mbuf excluding offset.
191
         */
192
        while (m != NULL && off > 0) {
193
                if (m->m_len <= off)
194
                        off -= m->m_len;
195
                else
196
                        break;
197
                m = m->m_next;
198
        }
199
        w = (u_int16_t *)(mtod(m, u_char *) + off);
200
        mlen = m->m_len - off;
201
        if (len < mlen)
202
                mlen = len;
203
        len -= mlen;
204
        /*
205
         * Force to even boundary.
206
         */
207
        if ((1 & (long) w) && (mlen > 0)) {
208
                REDUCE;
209
                sum <<= 8;
210
                s_util.c[0] = *(u_char *)w;
211
                w = (u_int16_t *)((char *)w + 1);
212
                mlen--;
213
                byte_swapped = 1;
214
        }
215
        /*
216
         * Unroll the loop to make overhead from
217
         * branches &c small.
218
         */
219
        while ((mlen -= 32) >= 0) {
220
                sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
221
                sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
222
                sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
223
                sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
224
                w += 16;
225
        }
226
        mlen += 32;
227
        while ((mlen -= 8) >= 0) {
228
                sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
229
                w += 4;
230
        }
231
        mlen += 8;
232
        if (mlen == 0 && byte_swapped == 0)
233
                goto next;
234
        REDUCE;
235
        while ((mlen -= 2) >= 0) {
236
                sum += *w++;
237
        }
238
        if (byte_swapped) {
239
                REDUCE;
240
                sum <<= 8;
241
                byte_swapped = 0;
242
                if (mlen == -1) {
243
                        s_util.c[1] = *(char *)w;
244
                        sum += s_util.s;
245
                        mlen = 0;
246
                } else
247
                        mlen = -1;
248
        } else if (mlen == -1)
249
                s_util.c[0] = *(char *)w;
250
 next:
251
        m = m->m_next;
252
 
253
        /*
254
         * Lastly calculate a summary of the rest of mbufs.
255
         */
256
 
257
        for (;m && len; m = m->m_next) {
258
                if (m->m_len == 0)
259
                        continue;
260
                w = mtod(m, u_int16_t *);
261
                if (mlen == -1) {
262
                        /*
263
                         * The first byte of this mbuf is the continuation
264
                         * of a word spanning between this mbuf and the
265
                         * last mbuf.
266
                         *
267
                         * s_util.c[0] is already saved when scanning previous
268
                         * mbuf.
269
                         */
270
                        s_util.c[1] = *(char *)w;
271
                        sum += s_util.s;
272
                        w = (u_int16_t *)((char *)w + 1);
273
                        mlen = m->m_len - 1;
274
                        len--;
275
                } else
276
                        mlen = m->m_len;
277
                if (len < mlen)
278
                        mlen = len;
279
                len -= mlen;
280
                /*
281
                 * Force to even boundary.
282
                 */
283
                if ((1 & (long) w) && (mlen > 0)) {
284
                        REDUCE;
285
                        sum <<= 8;
286
                        s_util.c[0] = *(u_char *)w;
287
                        w = (u_int16_t *)((char *)w + 1);
288
                        mlen--;
289
                        byte_swapped = 1;
290
                }
291
                /*
292
                 * Unroll the loop to make overhead from
293
                 * branches &c small.
294
                 */
295
                while ((mlen -= 32) >= 0) {
296
                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
297
                        sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
298
                        sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
299
                        sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
300
                        w += 16;
301
                }
302
                mlen += 32;
303
                while ((mlen -= 8) >= 0) {
304
                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
305
                        w += 4;
306
                }
307
                mlen += 8;
308
                if (mlen == 0 && byte_swapped == 0)
309
                        continue;
310
                REDUCE;
311
                while ((mlen -= 2) >= 0) {
312
                        sum += *w++;
313
                }
314
                if (byte_swapped) {
315
                        REDUCE;
316
                        sum <<= 8;
317
                        byte_swapped = 0;
318
                        if (mlen == -1) {
319
                                s_util.c[1] = *(char *)w;
320
                                sum += s_util.s;
321
                                mlen = 0;
322
                        } else
323
                                mlen = -1;
324
                } else if (mlen == -1)
325
                        s_util.c[0] = *(char *)w;
326
        }
327
        if (len)
328
                panic("in6_cksum: out of data\n");
329
        if (mlen == -1) {
330
                /* The last mbuf has odd # of bytes. Follow the
331
                   standard (the odd byte may be shifted left by 8 bits
332
                   or not as determined by endian-ness of the machine) */
333
                s_util.c[1] = 0;
334
                sum += s_util.s;
335
        }
336
        REDUCE;
337
        return (~sum & 0xffff);
338
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.