Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <ip_checksum.h> |
| 3 | |
Aaron Durbin | 30c64be | 2016-12-03 21:05:53 -0600 | [diff] [blame] | 4 | unsigned long compute_ip_checksum(const void *addr, unsigned long length) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 5 | { |
Aaron Durbin | 30c64be | 2016-12-03 21:05:53 -0600 | [diff] [blame] | 6 | const uint8_t *ptr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 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; |
Lee Leahy | 45fde70 | 2017-03-08 18:02:24 -0800 | [diff] [blame] | 18 | for (i = 0; i < length; i++) { |
Stefan Reinauer | 45dffef | 2009-03-31 16:42:57 +0000 | [diff] [blame] | 19 | unsigned long v; |
| 20 | v = ptr[i]; |
Lee Leahy | 2f919ec | 2017-03-08 17:37:06 -0800 | [diff] [blame] | 21 | if (i & 1) |
Stefan Reinauer | 45dffef | 2009-03-31 16:42:57 +0000 | [diff] [blame] | 22 | v <<= 8; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | /* Add the new value */ |
Stefan Reinauer | 45dffef | 2009-03-31 16:42:57 +0000 | [diff] [blame] | 24 | sum += v; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 25 | /* Wrap around the carry */ |
Lee Leahy | 2f919ec | 2017-03-08 17:37:06 -0800 | [diff] [blame] | 26 | if (sum > 0xFFFF) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 27 | sum = (sum + (sum >> 16)) & 0xFFFF; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | } |
| 29 | value.byte[0] = sum & 0xff; |
| 30 | value.byte[1] = (sum >> 8) & 0xff; |
| 31 | return (~value.word) & 0xFFFF; |
| 32 | } |
| 33 | |
Lee Leahy | 7340217 | 2017-03-10 15:23:24 -0800 | [diff] [blame] | 34 | unsigned long add_ip_checksums(unsigned long offset, unsigned long sum, |
| 35 | unsigned long new) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 36 | { |
| 37 | unsigned long checksum; |
| 38 | sum = ~sum & 0xFFFF; |
| 39 | new = ~new & 0xFFFF; |
| 40 | if (offset & 1) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 41 | /* byte swap the sum if it came from an odd offset |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 42 | * since the computation is endian independent this |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 43 | * works. |
| 44 | */ |
| 45 | new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00); |
| 46 | } |
| 47 | checksum = sum + new; |
Lee Leahy | 2f919ec | 2017-03-08 17:37:06 -0800 | [diff] [blame] | 48 | if (checksum > 0xFFFF) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 49 | checksum -= 0xFFFF; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 50 | return (~checksum) & 0xFFFF; |
| 51 | } |