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 | /** Compacts the computed checksum to the 16 bit number adding the carries.
|
---|
48 | *
|
---|
49 | * @param[in] sum Computed checksum.
|
---|
50 | * @return Compacted computed checksum to the 16 bits.
|
---|
51 | */
|
---|
52 | uint16_t compact_checksum(uint32_t sum)
|
---|
53 | {
|
---|
54 | // shorten to the 16 bits
|
---|
55 | while (sum >> 16)
|
---|
56 | sum = (sum & 0xffff) + (sum >> 16);
|
---|
57 |
|
---|
58 | return (uint16_t) sum;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Computes sum of the 2 byte fields.
|
---|
62 | *
|
---|
63 | * Padds one zero (0) byte if odd.
|
---|
64 | *
|
---|
65 | * @param[in] seed Initial value. Often used as 0 or ~0.
|
---|
66 | * @param[in] data Pointer to the beginning of data to process.
|
---|
67 | * @param[in] length Length of the data in bytes.
|
---|
68 | * @return The computed checksum of the length bytes of the data.
|
---|
69 | */
|
---|
70 | uint32_t compute_checksum(uint32_t seed, uint8_t *data, size_t length)
|
---|
71 | {
|
---|
72 | size_t index;
|
---|
73 |
|
---|
74 | // sum all the 16 bit fields
|
---|
75 | for (index = 0; index + 1 < length; index += 2)
|
---|
76 | seed += (data[index] << 8) + data[index + 1];
|
---|
77 |
|
---|
78 | // last odd byte with zero padding
|
---|
79 | if (index + 1 == length)
|
---|
80 | seed += data[index] << 8;
|
---|
81 |
|
---|
82 | return seed;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Computes CRC32 value in the big-endian environment.
|
---|
86 | *
|
---|
87 | * @param[in] seed Initial value. Often used as 0 or ~0.
|
---|
88 | * @param[in] data Pointer to the beginning of data to process.
|
---|
89 | * @param[in] length Length of the data in bits.
|
---|
90 | * @return The computed CRC32 of the length bits of the data.
|
---|
91 | */
|
---|
92 | uint32_t compute_crc32_be(uint32_t seed, uint8_t * data, size_t length)
|
---|
93 | {
|
---|
94 | size_t index;
|
---|
95 |
|
---|
96 | // process full bytes
|
---|
97 | while (length >= 8) {
|
---|
98 | // add the data
|
---|
99 | seed ^= (*data) << 24;
|
---|
100 |
|
---|
101 | // for each added bit
|
---|
102 | for (index = 0; index < 8; ++index) {
|
---|
103 | // if the first bit is set
|
---|
104 | if (seed & 0x80000000) {
|
---|
105 | // shift and divide the checksum
|
---|
106 | seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
|
---|
107 | } else {
|
---|
108 | // shift otherwise
|
---|
109 | seed <<= 1;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | // move to the next byte
|
---|
114 | ++data;
|
---|
115 | length -= 8;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // process the odd bits
|
---|
119 | if (length > 0) {
|
---|
120 | // add the data with zero padding
|
---|
121 | seed ^= ((*data) & (0xff << (8 - length))) << 24;
|
---|
122 |
|
---|
123 | // for each added bit
|
---|
124 | for (index = 0; index < length; ++index) {
|
---|
125 | // if the first bit is set
|
---|
126 | if (seed & 0x80000000) {
|
---|
127 | // shift and divide the checksum
|
---|
128 | seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
|
---|
129 | } else {
|
---|
130 | // shift otherwise
|
---|
131 | seed <<= 1;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | return seed;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Computes CRC32 value in the little-endian environment.
|
---|
140 | *
|
---|
141 | * @param[in] seed Initial value. Often used as 0 or ~0.
|
---|
142 | * @param[in] data Pointer to the beginning of data to process.
|
---|
143 | * @param[in] length Length of the data in bits.
|
---|
144 | * @return The computed CRC32 of the length bits of the data.
|
---|
145 | */
|
---|
146 | uint32_t compute_crc32_le(uint32_t seed, uint8_t * data, size_t length)
|
---|
147 | {
|
---|
148 | size_t index;
|
---|
149 |
|
---|
150 | // process full bytes
|
---|
151 | while (length >= 8) {
|
---|
152 | // add the data
|
---|
153 | seed ^= (*data);
|
---|
154 |
|
---|
155 | // for each added bit
|
---|
156 | for (index = 0; index < 8; ++index) {
|
---|
157 | // if the last bit is set
|
---|
158 | if (seed & 1) {
|
---|
159 | // shift and divide the checksum
|
---|
160 | seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
|
---|
161 | } else {
|
---|
162 | // shift otherwise
|
---|
163 | seed >>= 1;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | // move to the next byte
|
---|
168 | ++data;
|
---|
169 | length -= 8;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // process the odd bits
|
---|
173 | if (length > 0) {
|
---|
174 | // add the data with zero padding
|
---|
175 | seed ^= (*data) >> (8 - length);
|
---|
176 |
|
---|
177 | for (index = 0; index < length; ++index) {
|
---|
178 | // if the last bit is set
|
---|
179 | if (seed & 1) {
|
---|
180 | // shift and divide the checksum
|
---|
181 | seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
|
---|
182 | } else {
|
---|
183 | // shift otherwise
|
---|
184 | seed >>= 1;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return seed;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Returns or flips the checksum if zero.
|
---|
193 | *
|
---|
194 | * @param[in] checksum The computed checksum.
|
---|
195 | * @return The internet protocol header checksum.
|
---|
196 | * @return 0xFFFF if the computed checksum is zero.
|
---|
197 | */
|
---|
198 | uint16_t flip_checksum(uint16_t checksum)
|
---|
199 | {
|
---|
200 | // flip, zero is returned as 0xFFFF (not flipped)
|
---|
201 | checksum = ~checksum;
|
---|
202 | return checksum ? checksum : IP_CHECKSUM_ZERO;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Computes the ip header checksum.
|
---|
206 | *
|
---|
207 | * To compute the checksum of a new packet, the checksum header field must be
|
---|
208 | * zero. To check the checksum of a received packet, the checksum may be left
|
---|
209 | * set. Zero will be returned in this case if valid.
|
---|
210 | *
|
---|
211 | * @param[in] data The header data.
|
---|
212 | * @param[in] length The header length in bytes.
|
---|
213 | * @return The internet protocol header checksum.
|
---|
214 | * @return 0xFFFF if the computed checksum is zero.
|
---|
215 | */
|
---|
216 | uint16_t ip_checksum(uint8_t *data, size_t length)
|
---|
217 | {
|
---|
218 | // compute, compact and flip the data checksum
|
---|
219 | return flip_checksum(compact_checksum(compute_checksum(0, data,
|
---|
220 | length)));
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** @}
|
---|
224 | */
|
---|