1 | /* $Id: cksum.c 28449 2010-04-19 09:52:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - IP checksum generation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*
|
---|
23 | * This code is based on:
|
---|
24 | *
|
---|
25 | * Copyright (c) 1988, 1992, 1993
|
---|
26 | * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
|
---|
37 | * must display the following acknowledgement:
|
---|
38 | * This product includes software developed by the University of
|
---|
39 | * California, Berkeley and its contributors.
|
---|
40 | * 4. Neither the name of the University nor the names of its contributors
|
---|
41 | * may be used to endorse or promote products derived from this software
|
---|
42 | * without specific prior written permission.
|
---|
43 | *
|
---|
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
54 | * SUCH DAMAGE.
|
---|
55 | *
|
---|
56 | * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
|
---|
57 | * in_cksum.c,v 1.2 1994/08/02 07:48:16 davidg Exp
|
---|
58 | */
|
---|
59 |
|
---|
60 | #include <slirp.h>
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Checksum routine for Internet Protocol family headers (Portable Version).
|
---|
64 | *
|
---|
65 | * This routine is very heavily used in the network
|
---|
66 | * code and should be modified for each CPU to be as fast as possible.
|
---|
67 | *
|
---|
68 | * XXX Since we will never span more than 1 mbuf, we can optimise this
|
---|
69 | */
|
---|
70 |
|
---|
71 | #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
|
---|
72 | #define REDUCE { l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum); }
|
---|
73 |
|
---|
74 | int cksum(struct mbuf *m, int len)
|
---|
75 | {
|
---|
76 | register u_int16_t *w;
|
---|
77 | register int sum = 0;
|
---|
78 | register int mlen = 0;
|
---|
79 | int byte_swapped = 0;
|
---|
80 |
|
---|
81 | union
|
---|
82 | {
|
---|
83 | u_int8_t c[2];
|
---|
84 | u_int16_t s;
|
---|
85 | } s_util;
|
---|
86 | union
|
---|
87 | {
|
---|
88 | u_int16_t s[2];
|
---|
89 | u_int32_t l;
|
---|
90 | } l_util;
|
---|
91 |
|
---|
92 | if (m->m_len == 0)
|
---|
93 | goto cont;
|
---|
94 | w = mtod(m, u_int16_t *);
|
---|
95 |
|
---|
96 | mlen = m->m_len;
|
---|
97 |
|
---|
98 | if (len < mlen)
|
---|
99 | mlen = len;
|
---|
100 | len -= mlen;
|
---|
101 | /*
|
---|
102 | * Force to even boundary.
|
---|
103 | */
|
---|
104 | if ((1 & (long) w) && (mlen > 0))
|
---|
105 | {
|
---|
106 | REDUCE;
|
---|
107 | sum <<= 8;
|
---|
108 | s_util.c[0] = *(u_int8_t *)w;
|
---|
109 | w = (u_int16_t *)((int8_t *)w + 1);
|
---|
110 | mlen--;
|
---|
111 | byte_swapped = 1;
|
---|
112 | }
|
---|
113 | /*
|
---|
114 | * Unroll the loop to make overhead from
|
---|
115 | * branches &c small.
|
---|
116 | */
|
---|
117 | while ((mlen -= 32) >= 0)
|
---|
118 | {
|
---|
119 | sum += w[ 0]; sum += w[ 1]; sum += w[ 2]; sum += w[ 3];
|
---|
120 | sum += w[ 4]; sum += w[ 5]; sum += w[ 6]; sum += w[ 7];
|
---|
121 | sum += w[ 8]; sum += w[ 9]; sum += w[10]; sum += w[11];
|
---|
122 | sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
|
---|
123 | w += 16;
|
---|
124 | }
|
---|
125 | mlen += 32;
|
---|
126 | while ((mlen -= 8) >= 0)
|
---|
127 | {
|
---|
128 | sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
|
---|
129 | w += 4;
|
---|
130 | }
|
---|
131 | mlen += 8;
|
---|
132 | if (mlen == 0 && byte_swapped == 0)
|
---|
133 | goto cont;
|
---|
134 | REDUCE;
|
---|
135 | while ((mlen -= 2) >= 0)
|
---|
136 | {
|
---|
137 | sum += *w++;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (byte_swapped)
|
---|
141 | {
|
---|
142 | REDUCE;
|
---|
143 | sum <<= 8;
|
---|
144 | byte_swapped = 0;
|
---|
145 | if (mlen == -1)
|
---|
146 | {
|
---|
147 | s_util.c[1] = *(u_int8_t *)w;
|
---|
148 | sum += s_util.s;
|
---|
149 | mlen = 0;
|
---|
150 | }
|
---|
151 | else
|
---|
152 | mlen = -1;
|
---|
153 | }
|
---|
154 | else if (mlen == -1)
|
---|
155 | s_util.c[0] = *(u_int8_t *)w;
|
---|
156 |
|
---|
157 | cont:
|
---|
158 | #ifdef DEBUG
|
---|
159 | if (len)
|
---|
160 | {
|
---|
161 | DEBUG_ERROR((dfd, "cksum: out of data\n"));
|
---|
162 | DEBUG_ERROR((dfd, " len = %d\n", len));
|
---|
163 | }
|
---|
164 | #endif
|
---|
165 | if (mlen == -1)
|
---|
166 | {
|
---|
167 | /* The last mbuf has odd # of bytes. Follow the
|
---|
168 | standard (the odd byte may be shifted left by 8 bits
|
---|
169 | or not as determined by endian-ness of the machine) */
|
---|
170 | s_util.c[1] = 0;
|
---|
171 | sum += s_util.s;
|
---|
172 | }
|
---|
173 | REDUCE;
|
---|
174 | return (~sum & 0xffff);
|
---|
175 | }
|
---|