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