source: mainline/uspace/lib/net/generic/net_checksum.c@ c3887ad

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c3887ad was 00d7e1b, checked in by Martin Decky <martin@…>, 14 years ago

networking improvements

  • start the networking stack from init
  • add loopback network interface driver (cherrypicked and sanitized from lp:~helenos-nicf/helenos/nicf)
  • add libnic and various small pieces from lp:~helenos-nicf/helenos/nicf
  • fix client side of NIC_GET_ADDRESS
  • net binary overhaul

Note: "ping 127.0.0.1" works, but the first three pings timeout for some reason

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libnet
30 * @{
31 */
32
33/** @file
34 * General CRC and checksum computation implementation.
35 */
36
37#include <sys/types.h>
38
39#include <net_checksum.h>
40
41/** Big-endian encoding CRC divider. */
42#define CRC_DIVIDER_BE 0x04c11db7
43
44/** Little-endian encoding CRC divider. */
45#define CRC_DIVIDER_LE 0xedb88320
46
47/** Polynomial used in multicast address hashing */
48#define CRC_MCAST_POLYNOMIAL 0x04c11db6
49
50/** Compacts the computed checksum to the 16 bit number adding the carries.
51 *
52 * @param[in] sum Computed checksum.
53 * @return Compacted computed checksum to the 16 bits.
54 */
55uint16_t compact_checksum(uint32_t sum)
56{
57 /* Shorten to the 16 bits */
58 while (sum >> 16)
59 sum = (sum & 0xffff) + (sum >> 16);
60
61 return (uint16_t) sum;
62}
63
64/** Computes sum of the 2 byte fields.
65 *
66 * Padds one zero (0) byte if odd.
67 *
68 * @param[in] seed Initial value. Often used as 0 or ~0.
69 * @param[in] data Pointer to the beginning of data to process.
70 * @param[in] length Length of the data in bytes.
71 * @return The computed checksum of the length bytes of the data.
72 */
73uint32_t compute_checksum(uint32_t seed, uint8_t *data, size_t length)
74{
75 size_t index;
76
77 /* Sum all the 16 bit fields */
78 for (index = 0; index + 1 < length; index += 2)
79 seed += (data[index] << 8) + data[index + 1];
80
81 /* Last odd byte with zero padding */
82 if (index + 1 == length)
83 seed += data[index] << 8;
84
85 return seed;
86}
87
88/** Computes CRC32 value in the big-endian environment.
89 *
90 * @param[in] seed Initial value. Often used as 0 or ~0.
91 * @param[in] data Pointer to the beginning of data to process.
92 * @param[in] length Length of the data in bits.
93 * @return The computed CRC32 of the length bits of the data.
94 */
95uint32_t compute_crc32_be(uint32_t seed, uint8_t * data, size_t length)
96{
97 size_t index;
98
99 /* Process full bytes */
100 while (length >= 8) {
101 /* Add the data */
102 seed ^= (*data) << 24;
103
104 /* For each added bit */
105 for (index = 0; index < 8; ++index) {
106 /* If the first bit is set */
107 if (seed & 0x80000000) {
108 /* Shift and divide the checksum */
109 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
110 } else {
111 /* Shift otherwise */
112 seed <<= 1;
113 }
114 }
115
116 /* Move to the next byte */
117 ++data;
118 length -= 8;
119 }
120
121 /* Process the odd bits */
122 if (length > 0) {
123 /* Add the data with zero padding */
124 seed ^= ((*data) & (0xff << (8 - length))) << 24;
125
126 /* For each added bit */
127 for (index = 0; index < length; ++index) {
128 /* If the first bit is set */
129 if (seed & 0x80000000) {
130 /* Shift and divide the checksum */
131 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
132 } else {
133 /* Shift otherwise */
134 seed <<= 1;
135 }
136 }
137 }
138
139 return seed;
140}
141
142/** Computes CRC32 value in the little-endian environment.
143 *
144 * @param[in] seed Initial value. Often used as 0 or ~0.
145 * @param[in] data Pointer to the beginning of data to process.
146 * @param[in] length Length of the data in bits.
147 * @return The computed CRC32 of the length bits of the data.
148 */
149uint32_t compute_crc32_le(uint32_t seed, uint8_t * data, size_t length)
150{
151 size_t index;
152
153 /* Process full bytes */
154 while (length >= 8) {
155 /* Add the data */
156 seed ^= (*data);
157
158 /* For each added bit */
159 for (index = 0; index < 8; ++index) {
160 /* If the last bit is set */
161 if (seed & 1) {
162 /* Shift and divide the checksum */
163 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
164 } else {
165 /* Shift otherwise */
166 seed >>= 1;
167 }
168 }
169
170 /* Move to the next byte */
171 ++data;
172 length -= 8;
173 }
174
175 /* Process the odd bits */
176 if (length > 0) {
177 /* Add the data with zero padding */
178 seed ^= (*data) >> (8 - length);
179
180 for (index = 0; index < length; ++index) {
181 /* If the last bit is set */
182 if (seed & 1) {
183 /* Shift and divide the checksum */
184 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
185 } else {
186 /* Shift otherwise */
187 seed >>= 1;
188 }
189 }
190 }
191
192 return seed;
193}
194
195/** Returns or flips the checksum if zero.
196 *
197 * @param[in] checksum The computed checksum.
198 * @return The internet protocol header checksum.
199 * @return 0xFFFF if the computed checksum is zero.
200 */
201uint16_t flip_checksum(uint16_t checksum)
202{
203 /* Flip, zero is returned as 0xFFFF (not flipped) */
204 checksum = ~checksum;
205 return checksum ? checksum : IP_CHECKSUM_ZERO;
206}
207
208/** Compute the IP header checksum.
209 *
210 * To compute the checksum of a new packet, the checksum header field must be
211 * zero. To check the checksum of a received packet, the checksum may be left
212 * set. Zero will be returned in this case if valid.
213 *
214 * @param[in] data The header data.
215 * @param[in] length The header length in bytes.
216 * @return The internet protocol header checksum.
217 * @return 0xFFFF if the computed checksum is zero.
218 */
219uint16_t ip_checksum(uint8_t *data, size_t length)
220{
221 /* Compute, compact and flip the data checksum */
222 return flip_checksum(compact_checksum(compute_checksum(0, data,
223 length)));
224}
225
226/** Compute the standard hash from MAC
227 *
228 * Hashing MAC into 64 possible values and using the value as index to
229 * 64bit number.
230 *
231 * The code is copied from qemu-0.13's implementation of ne2000 and rt8139
232 * drivers, but according to documentation there it originates in FreeBSD.
233 *
234 * @param[in] addr The 6-byte MAC address to be hashed
235 *
236 * @return 64-bit number with only single bit set to 1
237 *
238 */
239uint64_t multicast_hash(const uint8_t addr[6])
240{
241 uint32_t crc;
242 int carry, i, j;
243 uint8_t b;
244
245 crc = 0xffffffff;
246 for (i = 0; i < 6; i++) {
247 b = addr[i];
248 for (j = 0; j < 8; j++) {
249 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
250 crc <<= 1;
251 b >>= 1;
252 if (carry)
253 crc = ((crc ^ CRC_MCAST_POLYNOMIAL) | carry);
254 }
255 }
256
257 uint64_t one64 = 1;
258 return one64 << (crc >> 26);
259}
260
261/** @}
262 */
Note: See TracBrowser for help on using the repository browser.