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
RevLine 
[21580dd]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
[8d601db]29/** @addtogroup libnet
30 * @{
[21580dd]31 */
32
33/** @file
[8d601db]34 * General CRC and checksum computation implementation.
[21580dd]35 */
36
37#include <sys/types.h>
38
[849ed54]39#include <net_checksum.h>
[21580dd]40
[8d601db]41/** Big-endian encoding CRC divider. */
[609243f4]42#define CRC_DIVIDER_BE 0x04c11db7
[21580dd]43
[8d601db]44/** Little-endian encoding CRC divider. */
[609243f4]45#define CRC_DIVIDER_LE 0xedb88320
[21580dd]46
[00d7e1b]47/** Polynomial used in multicast address hashing */
48#define CRC_MCAST_POLYNOMIAL 0x04c11db6
49
[8d601db]50/** Compacts the computed checksum to the 16 bit number adding the carries.
51 *
52 * @param[in] sum Computed checksum.
[1bfd3d3]53 * @return Compacted computed checksum to the 16 bits.
[8d601db]54 */
55uint16_t compact_checksum(uint32_t sum)
56{
[28a3e74]57 /* Shorten to the 16 bits */
[8d601db]58 while (sum >> 16)
59 sum = (sum & 0xffff) + (sum >> 16);
[a64c64d]60
61 return (uint16_t) sum;
62}
63
[8d601db]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.
[1bfd3d3]71 * @return The computed checksum of the length bytes of the data.
[8d601db]72 */
73uint32_t compute_checksum(uint32_t seed, uint8_t *data, size_t length)
74{
[aadf01e]75 size_t index;
76
[28a3e74]77 /* Sum all the 16 bit fields */
[8d601db]78 for (index = 0; index + 1 < length; index += 2)
[a64c64d]79 seed += (data[index] << 8) + data[index + 1];
80
[28a3e74]81 /* Last odd byte with zero padding */
[8d601db]82 if (index + 1 == length)
[a64c64d]83 seed += data[index] << 8;
84
[21580dd]85 return seed;
86}
87
[8d601db]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.
[1bfd3d3]93 * @return The computed CRC32 of the length bits of the data.
[8d601db]94 */
95uint32_t compute_crc32_be(uint32_t seed, uint8_t * data, size_t length)
96{
[aadf01e]97 size_t index;
[21580dd]98
[28a3e74]99 /* Process full bytes */
[8d601db]100 while (length >= 8) {
[28a3e74]101 /* Add the data */
[aadf01e]102 seed ^= (*data) << 24;
[8d601db]103
[28a3e74]104 /* For each added bit */
[8d601db]105 for (index = 0; index < 8; ++index) {
[28a3e74]106 /* If the first bit is set */
[8d601db]107 if (seed & 0x80000000) {
[28a3e74]108 /* Shift and divide the checksum */
[aadf01e]109 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
[8d601db]110 } else {
[ccca251]111 /* Shift otherwise */
[21580dd]112 seed <<= 1;
113 }
114 }
[8d601db]115
[28a3e74]116 /* Move to the next byte */
[8d601db]117 ++data;
[21580dd]118 length -= 8;
119 }
[a64c64d]120
[28a3e74]121 /* Process the odd bits */
[8d601db]122 if (length > 0) {
[28a3e74]123 /* Add the data with zero padding */
[8d601db]124 seed ^= ((*data) & (0xff << (8 - length))) << 24;
125
[28a3e74]126 /* For each added bit */
[8d601db]127 for (index = 0; index < length; ++index) {
[28a3e74]128 /* If the first bit is set */
[8d601db]129 if (seed & 0x80000000) {
[28a3e74]130 /* Shift and divide the checksum */
[aadf01e]131 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
[8d601db]132 } else {
[28a3e74]133 /* Shift otherwise */
[21580dd]134 seed <<= 1;
135 }
136 }
137 }
[a64c64d]138
[21580dd]139 return seed;
140}
141
[8d601db]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.
[1bfd3d3]147 * @return The computed CRC32 of the length bits of the data.
[8d601db]148 */
149uint32_t compute_crc32_le(uint32_t seed, uint8_t * data, size_t length)
150{
[aadf01e]151 size_t index;
[21580dd]152
[28a3e74]153 /* Process full bytes */
[8d601db]154 while (length >= 8) {
[28a3e74]155 /* Add the data */
[a64c64d]156 seed ^= (*data);
[8d601db]157
[28a3e74]158 /* For each added bit */
[8d601db]159 for (index = 0; index < 8; ++index) {
[28a3e74]160 /* If the last bit is set */
[8d601db]161 if (seed & 1) {
[28a3e74]162 /* Shift and divide the checksum */
[a64c64d]163 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
[8d601db]164 } else {
[28a3e74]165 /* Shift otherwise */
[a64c64d]166 seed >>= 1;
167 }
168 }
[8d601db]169
[28a3e74]170 /* Move to the next byte */
[8d601db]171 ++data;
[a64c64d]172 length -= 8;
[21580dd]173 }
174
[28a3e74]175 /* Process the odd bits */
[8d601db]176 if (length > 0) {
[28a3e74]177 /* Add the data with zero padding */
[a64c64d]178 seed ^= (*data) >> (8 - length);
[8d601db]179
180 for (index = 0; index < length; ++index) {
[28a3e74]181 /* If the last bit is set */
[8d601db]182 if (seed & 1) {
[28a3e74]183 /* Shift and divide the checksum */
[a64c64d]184 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
[8d601db]185 } else {
[28a3e74]186 /* Shift otherwise */
[a64c64d]187 seed >>= 1;
188 }
189 }
[21580dd]190 }
191
192 return seed;
193}
194
[8d601db]195/** Returns or flips the checksum if zero.
196 *
197 * @param[in] checksum The computed checksum.
[1bfd3d3]198 * @return The internet protocol header checksum.
199 * @return 0xFFFF if the computed checksum is zero.
[8d601db]200 */
201uint16_t flip_checksum(uint16_t checksum)
202{
[28a3e74]203 /* Flip, zero is returned as 0xFFFF (not flipped) */
[8d601db]204 checksum = ~checksum;
[918e9910]205 return checksum ? checksum : IP_CHECKSUM_ZERO;
[21580dd]206}
207
[609243f4]208/** Compute the IP header checksum.
[8d601db]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.
[1bfd3d3]216 * @return The internet protocol header checksum.
217 * @return 0xFFFF if the computed checksum is zero.
[8d601db]218 */
219uint16_t ip_checksum(uint8_t *data, size_t length)
220{
[28a3e74]221 /* Compute, compact and flip the data checksum */
[8d601db]222 return flip_checksum(compact_checksum(compute_checksum(0, data,
223 length)));
[21580dd]224}
225
[00d7e1b]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
[21580dd]261/** @}
262 */
Note: See TracBrowser for help on using the repository browser.