blob: 58a6bf1ee47117e4ea8b862a1874bc937e2c2164 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <stdint.h>
2#include <ip_checksum.h>
3
4unsigned long compute_ip_checksum(void *addr, unsigned long length)
5{
6 uint8_t *ptr;
7 volatile union {
8 uint8_t byte[2];
9 uint16_t word;
10 } value;
11 unsigned long sum;
12 unsigned long i;
13 /* In the most straight forward way possible,
14 * compute an ip style checksum.
15 */
16 sum = 0;
17 ptr = addr;
18 for(i = 0; i < length; i++) {
Stefan Reinauer45dffef2009-03-31 16:42:57 +000019 unsigned long v;
20 v = ptr[i];
Eric Biederman8ca8d762003-04-22 19:02:15 +000021 if (i & 1) {
Stefan Reinauer45dffef2009-03-31 16:42:57 +000022 v <<= 8;
Eric Biederman8ca8d762003-04-22 19:02:15 +000023 }
24 /* Add the new value */
Stefan Reinauer45dffef2009-03-31 16:42:57 +000025 sum += v;
Eric Biederman8ca8d762003-04-22 19:02:15 +000026 /* Wrap around the carry */
27 if (sum > 0xFFFF) {
28 sum = (sum + (sum >> 16)) & 0xFFFF;
29 }
30 }
31 value.byte[0] = sum & 0xff;
32 value.byte[1] = (sum >> 8) & 0xff;
33 return (~value.word) & 0xFFFF;
34}
35
36unsigned long add_ip_checksums(unsigned long offset, unsigned long sum, unsigned long new)
37{
38 unsigned long checksum;
39 sum = ~sum & 0xFFFF;
40 new = ~new & 0xFFFF;
41 if (offset & 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000042 /* byte swap the sum if it came from an odd offset
Martin Rothcbf2bd72013-07-09 21:51:14 -060043 * since the computation is endian independent this
Eric Biederman8ca8d762003-04-22 19:02:15 +000044 * works.
45 */
46 new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
47 }
48 checksum = sum + new;
49 if (checksum > 0xFFFF) {
50 checksum -= 0xFFFF;
51 }
52 return (~checksum) & 0xFFFF;
53}