Statistics
| Branch: | Revision:

root / slirp / cksum.c @ d523d5d6

History | View | Annotate | Download (3.8 kB)

1 f0cbd3ec bellard
/*
2 f0cbd3ec bellard
 * Copyright (c) 1988, 1992, 1993
3 f0cbd3ec bellard
 *        The Regents of the University of California.  All rights reserved.
4 f0cbd3ec bellard
 *
5 f0cbd3ec bellard
 * Redistribution and use in source and binary forms, with or without
6 f0cbd3ec bellard
 * modification, are permitted provided that the following conditions
7 f0cbd3ec bellard
 * are met:
8 f0cbd3ec bellard
 * 1. Redistributions of source code must retain the above copyright
9 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer.
10 f0cbd3ec bellard
 * 2. Redistributions in binary form must reproduce the above copyright
11 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer in the
12 f0cbd3ec bellard
 *    documentation and/or other materials provided with the distribution.
13 2f5f8996 aliguori
 * 3. Neither the name of the University nor the names of its contributors
14 f0cbd3ec bellard
 *    may be used to endorse or promote products derived from this software
15 f0cbd3ec bellard
 *    without specific prior written permission.
16 f0cbd3ec bellard
 *
17 f0cbd3ec bellard
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 f0cbd3ec bellard
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 f0cbd3ec bellard
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 f0cbd3ec bellard
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 f0cbd3ec bellard
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 f0cbd3ec bellard
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 f0cbd3ec bellard
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 f0cbd3ec bellard
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f0cbd3ec bellard
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 f0cbd3ec bellard
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 f0cbd3ec bellard
 * SUCH DAMAGE.
28 f0cbd3ec bellard
 *
29 f0cbd3ec bellard
 *        @(#)in_cksum.c        8.1 (Berkeley) 6/10/93
30 f0cbd3ec bellard
 * in_cksum.c,v 1.2 1994/08/02 07:48:16 davidg Exp
31 f0cbd3ec bellard
 */
32 f0cbd3ec bellard
33 f0cbd3ec bellard
#include <slirp.h>
34 f0cbd3ec bellard
35 f0cbd3ec bellard
/*
36 f0cbd3ec bellard
 * Checksum routine for Internet Protocol family headers (Portable Version).
37 f0cbd3ec bellard
 *
38 f0cbd3ec bellard
 * This routine is very heavily used in the network
39 f0cbd3ec bellard
 * code and should be modified for each CPU to be as fast as possible.
40 5fafdf24 ths
 *
41 f0cbd3ec bellard
 * XXX Since we will never span more than 1 mbuf, we can optimise this
42 f0cbd3ec bellard
 */
43 f0cbd3ec bellard
44 f0cbd3ec bellard
#define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
45 7107944a Blue Swirl
#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1];        \
46 7107944a Blue Swirl
        (void)ADDCARRY(sum);}
47 f0cbd3ec bellard
48 f0cbd3ec bellard
int cksum(struct mbuf *m, int len)
49 f0cbd3ec bellard
{
50 b6dce92e Stefan Weil
        register uint16_t *w;
51 f0cbd3ec bellard
        register int sum = 0;
52 f0cbd3ec bellard
        register int mlen = 0;
53 f0cbd3ec bellard
        int byte_swapped = 0;
54 f0cbd3ec bellard
55 f0cbd3ec bellard
        union {
56 b6dce92e Stefan Weil
                uint8_t  c[2];
57 b6dce92e Stefan Weil
                uint16_t s;
58 f0cbd3ec bellard
        } s_util;
59 f0cbd3ec bellard
        union {
60 b6dce92e Stefan Weil
                uint16_t s[2];
61 b6dce92e Stefan Weil
                uint32_t l;
62 f0cbd3ec bellard
        } l_util;
63 5fafdf24 ths
64 f0cbd3ec bellard
        if (m->m_len == 0)
65 f0cbd3ec bellard
           goto cont;
66 b6dce92e Stefan Weil
        w = mtod(m, uint16_t *);
67 5fafdf24 ths
68 f0cbd3ec bellard
        mlen = m->m_len;
69 5fafdf24 ths
70 f0cbd3ec bellard
        if (len < mlen)
71 f0cbd3ec bellard
           mlen = len;
72 369c86e7 Blue Swirl
#ifdef DEBUG
73 f0cbd3ec bellard
        len -= mlen;
74 369c86e7 Blue Swirl
#endif
75 f0cbd3ec bellard
        /*
76 f0cbd3ec bellard
         * Force to even boundary.
77 f0cbd3ec bellard
         */
78 f0cbd3ec bellard
        if ((1 & (long) w) && (mlen > 0)) {
79 f0cbd3ec bellard
                REDUCE;
80 f0cbd3ec bellard
                sum <<= 8;
81 b6dce92e Stefan Weil
                s_util.c[0] = *(uint8_t *)w;
82 b6dce92e Stefan Weil
                w = (uint16_t *)((int8_t *)w + 1);
83 f0cbd3ec bellard
                mlen--;
84 f0cbd3ec bellard
                byte_swapped = 1;
85 f0cbd3ec bellard
        }
86 f0cbd3ec bellard
        /*
87 f0cbd3ec bellard
         * Unroll the loop to make overhead from
88 f0cbd3ec bellard
         * branches &c small.
89 f0cbd3ec bellard
         */
90 f0cbd3ec bellard
        while ((mlen -= 32) >= 0) {
91 f0cbd3ec bellard
                sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
92 f0cbd3ec bellard
                sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
93 f0cbd3ec bellard
                sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
94 f0cbd3ec bellard
                sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
95 f0cbd3ec bellard
                w += 16;
96 f0cbd3ec bellard
        }
97 f0cbd3ec bellard
        mlen += 32;
98 f0cbd3ec bellard
        while ((mlen -= 8) >= 0) {
99 f0cbd3ec bellard
                sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
100 f0cbd3ec bellard
                w += 4;
101 f0cbd3ec bellard
        }
102 f0cbd3ec bellard
        mlen += 8;
103 f0cbd3ec bellard
        if (mlen == 0 && byte_swapped == 0)
104 f0cbd3ec bellard
           goto cont;
105 f0cbd3ec bellard
        REDUCE;
106 f0cbd3ec bellard
        while ((mlen -= 2) >= 0) {
107 f0cbd3ec bellard
                sum += *w++;
108 f0cbd3ec bellard
        }
109 5fafdf24 ths
110 f0cbd3ec bellard
        if (byte_swapped) {
111 f0cbd3ec bellard
                REDUCE;
112 f0cbd3ec bellard
                sum <<= 8;
113 f0cbd3ec bellard
                if (mlen == -1) {
114 b6dce92e Stefan Weil
                        s_util.c[1] = *(uint8_t *)w;
115 f0cbd3ec bellard
                        sum += s_util.s;
116 f0cbd3ec bellard
                        mlen = 0;
117 f0cbd3ec bellard
                } else
118 3b46e624 ths
119 f0cbd3ec bellard
                   mlen = -1;
120 f0cbd3ec bellard
        } else if (mlen == -1)
121 b6dce92e Stefan Weil
           s_util.c[0] = *(uint8_t *)w;
122 5fafdf24 ths
123 f0cbd3ec bellard
cont:
124 f0cbd3ec bellard
#ifdef DEBUG
125 f0cbd3ec bellard
        if (len) {
126 f0cbd3ec bellard
                DEBUG_ERROR((dfd, "cksum: out of data\n"));
127 f0cbd3ec bellard
                DEBUG_ERROR((dfd, " len = %d\n", len));
128 f0cbd3ec bellard
        }
129 f0cbd3ec bellard
#endif
130 f0cbd3ec bellard
        if (mlen == -1) {
131 f0cbd3ec bellard
                /* The last mbuf has odd # of bytes. Follow the
132 f0cbd3ec bellard
                 standard (the odd byte may be shifted left by 8 bits
133 f0cbd3ec bellard
                           or not as determined by endian-ness of the machine) */
134 f0cbd3ec bellard
                s_util.c[1] = 0;
135 f0cbd3ec bellard
                sum += s_util.s;
136 f0cbd3ec bellard
        }
137 f0cbd3ec bellard
        REDUCE;
138 f0cbd3ec bellard
        return (~sum & 0xffff);
139 f0cbd3ec bellard
}