1 // SPDX-License-Identifier: BSD-2-Clause
3 * This file was originally taken from the FreeBSD project.
5 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
6 * Copyright (c) 2008 coresystems GmbH
13 unsigned compute_ip_checksum(const void *vptr, unsigned nbytes)
16 const unsigned short *ptr = vptr;
25 ((u8 *)&oddbyte)[0] = *(u8 *)ptr;
26 ((u8 *)&oddbyte)[1] = 0;
29 sum = (sum >> 16) + (sum & 0xffff);
36 unsigned add_ip_checksums(unsigned offset, unsigned sum, unsigned new)
38 unsigned long checksum;
44 * byte-swap the sum if it came from an odd offset; since the
45 * computation is endian independant this works.
47 new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
50 if (checksum > 0xffff)
53 return (~checksum) & 0xffff;
56 int ip_checksum_ok(const void *addr, unsigned nbytes)
58 return !(compute_ip_checksum(addr, nbytes) & 0xfffe);