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