root / slirp / cksum.c @ 67f0875e
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 | f0cbd3ec | bellard | #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);} |
46 | f0cbd3ec | bellard | |
47 | f0cbd3ec | bellard | int cksum(struct mbuf *m, int len) |
48 | f0cbd3ec | bellard | { |
49 | f0cbd3ec | bellard | register u_int16_t *w;
|
50 | f0cbd3ec | bellard | register int sum = 0; |
51 | f0cbd3ec | bellard | register int mlen = 0; |
52 | f0cbd3ec | bellard | int byte_swapped = 0; |
53 | f0cbd3ec | bellard | |
54 | f0cbd3ec | bellard | union {
|
55 | f0cbd3ec | bellard | u_int8_t c[2];
|
56 | f0cbd3ec | bellard | u_int16_t s; |
57 | f0cbd3ec | bellard | } s_util; |
58 | f0cbd3ec | bellard | union {
|
59 | f0cbd3ec | bellard | u_int16_t s[2];
|
60 | f0cbd3ec | bellard | u_int32_t l; |
61 | f0cbd3ec | bellard | } l_util; |
62 | 5fafdf24 | ths | |
63 | f0cbd3ec | bellard | if (m->m_len == 0) |
64 | f0cbd3ec | bellard | goto cont;
|
65 | f0cbd3ec | bellard | w = mtod(m, u_int16_t *); |
66 | 5fafdf24 | ths | |
67 | f0cbd3ec | bellard | mlen = m->m_len; |
68 | 5fafdf24 | ths | |
69 | f0cbd3ec | bellard | if (len < mlen)
|
70 | f0cbd3ec | bellard | mlen = len; |
71 | f0cbd3ec | bellard | len -= mlen; |
72 | f0cbd3ec | bellard | /*
|
73 | f0cbd3ec | bellard | * Force to even boundary.
|
74 | f0cbd3ec | bellard | */
|
75 | f0cbd3ec | bellard | if ((1 & (long) w) && (mlen > 0)) { |
76 | f0cbd3ec | bellard | REDUCE; |
77 | f0cbd3ec | bellard | sum <<= 8;
|
78 | f0cbd3ec | bellard | s_util.c[0] = *(u_int8_t *)w;
|
79 | f0cbd3ec | bellard | w = (u_int16_t *)((int8_t *)w + 1);
|
80 | f0cbd3ec | bellard | mlen--; |
81 | f0cbd3ec | bellard | byte_swapped = 1;
|
82 | f0cbd3ec | bellard | } |
83 | f0cbd3ec | bellard | /*
|
84 | f0cbd3ec | bellard | * Unroll the loop to make overhead from
|
85 | f0cbd3ec | bellard | * branches &c small.
|
86 | f0cbd3ec | bellard | */
|
87 | f0cbd3ec | bellard | while ((mlen -= 32) >= 0) { |
88 | f0cbd3ec | bellard | sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; |
89 | f0cbd3ec | bellard | sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; |
90 | f0cbd3ec | bellard | sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; |
91 | f0cbd3ec | bellard | sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; |
92 | f0cbd3ec | bellard | w += 16;
|
93 | f0cbd3ec | bellard | } |
94 | f0cbd3ec | bellard | mlen += 32;
|
95 | f0cbd3ec | bellard | while ((mlen -= 8) >= 0) { |
96 | f0cbd3ec | bellard | sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; |
97 | f0cbd3ec | bellard | w += 4;
|
98 | f0cbd3ec | bellard | } |
99 | f0cbd3ec | bellard | mlen += 8;
|
100 | f0cbd3ec | bellard | if (mlen == 0 && byte_swapped == 0) |
101 | f0cbd3ec | bellard | goto cont;
|
102 | f0cbd3ec | bellard | REDUCE; |
103 | f0cbd3ec | bellard | while ((mlen -= 2) >= 0) { |
104 | f0cbd3ec | bellard | sum += *w++; |
105 | f0cbd3ec | bellard | } |
106 | 5fafdf24 | ths | |
107 | f0cbd3ec | bellard | if (byte_swapped) {
|
108 | f0cbd3ec | bellard | REDUCE; |
109 | f0cbd3ec | bellard | sum <<= 8;
|
110 | f0cbd3ec | bellard | byte_swapped = 0;
|
111 | f0cbd3ec | bellard | if (mlen == -1) { |
112 | f0cbd3ec | bellard | s_util.c[1] = *(u_int8_t *)w;
|
113 | f0cbd3ec | bellard | sum += s_util.s; |
114 | f0cbd3ec | bellard | mlen = 0;
|
115 | f0cbd3ec | bellard | } else
|
116 | 3b46e624 | ths | |
117 | f0cbd3ec | bellard | mlen = -1;
|
118 | f0cbd3ec | bellard | } else if (mlen == -1) |
119 | f0cbd3ec | bellard | s_util.c[0] = *(u_int8_t *)w;
|
120 | 5fafdf24 | ths | |
121 | f0cbd3ec | bellard | cont:
|
122 | f0cbd3ec | bellard | #ifdef DEBUG
|
123 | f0cbd3ec | bellard | if (len) {
|
124 | f0cbd3ec | bellard | DEBUG_ERROR((dfd, "cksum: out of data\n"));
|
125 | f0cbd3ec | bellard | DEBUG_ERROR((dfd, " len = %d\n", len));
|
126 | f0cbd3ec | bellard | } |
127 | f0cbd3ec | bellard | #endif
|
128 | f0cbd3ec | bellard | if (mlen == -1) { |
129 | f0cbd3ec | bellard | /* The last mbuf has odd # of bytes. Follow the
|
130 | f0cbd3ec | bellard | standard (the odd byte may be shifted left by 8 bits
|
131 | f0cbd3ec | bellard | or not as determined by endian-ness of the machine) */
|
132 | f0cbd3ec | bellard | s_util.c[1] = 0; |
133 | f0cbd3ec | bellard | sum += s_util.s; |
134 | f0cbd3ec | bellard | } |
135 | f0cbd3ec | bellard | REDUCE; |
136 | f0cbd3ec | bellard | return (~sum & 0xffff); |
137 | f0cbd3ec | bellard | } |