blob: 0d3394e71287711cf7c19e3dfe083e457488f93e [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <stdint.h>
2#include <ip_checksum.h>
3
Aaron Durbin30c64be2016-12-03 21:05:53 -06004unsigned long compute_ip_checksum(const void *addr, unsigned long length)
Eric Biederman8ca8d762003-04-22 19:02:15 +00005{
Aaron Durbin30c64be2016-12-03 21:05:53 -06006 const uint8_t *ptr;
Eric Biederman8ca8d762003-04-22 19:02:15 +00007 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 Leahy45fde702017-03-08 18:02:24 -080018 for (i = 0; i < length; i++) {
Stefan Reinauer45dffef2009-03-31 16:42:57 +000019 unsigned long v;
20 v = ptr[i];
Lee Leahy2f919ec2017-03-08 17:37:06 -080021 if (i & 1)
Stefan Reinauer45dffef2009-03-31 16:42:57 +000022 v <<= 8;
Eric Biederman8ca8d762003-04-22 19:02:15 +000023 /* Add the new value */
Stefan Reinauer45dffef2009-03-31 16:42:57 +000024 sum += v;
Eric Biederman8ca8d762003-04-22 19:02:15 +000025 /* Wrap around the carry */
Lee Leahy2f919ec2017-03-08 17:37:06 -080026 if (sum > 0xFFFF)
Eric Biederman8ca8d762003-04-22 19:02:15 +000027 sum = (sum + (sum >> 16)) & 0xFFFF;
Eric Biederman8ca8d762003-04-22 19:02:15 +000028 }
29 value.byte[0] = sum & 0xff;
30 value.byte[1] = (sum >> 8) & 0xff;
31 return (~value.word) & 0xFFFF;
32}
33
34unsigned long add_ip_checksums(unsigned long offset, unsigned long sum, unsigned long new)
35{
36 unsigned long checksum;
37 sum = ~sum & 0xFFFF;
38 new = ~new & 0xFFFF;
39 if (offset & 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000040 /* byte swap the sum if it came from an odd offset
Martin Rothcbf2bd72013-07-09 21:51:14 -060041 * since the computation is endian independent this
Eric Biederman8ca8d762003-04-22 19:02:15 +000042 * works.
43 */
44 new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
45 }
46 checksum = sum + new;
Lee Leahy2f919ec2017-03-08 17:37:06 -080047 if (checksum > 0xFFFF)
Eric Biederman8ca8d762003-04-22 19:02:15 +000048 checksum -= 0xFFFF;
Eric Biederman8ca8d762003-04-22 19:02:15 +000049 return (~checksum) & 0xFFFF;
50}