2 * This file was originally taken from the FreeBSD project.
4 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
5 * Copyright (c) 2008 coresystems GmbH
8 * SPDX-License-Identifier: BSD-2-Clause
14 unsigned compute_ip_checksum(const void *vptr, unsigned nbytes)
17 const unsigned short *ptr = vptr;
26 ((u8 *)&oddbyte)[0] = *(u8 *)ptr;
27 ((u8 *)&oddbyte)[1] = 0;
30 sum = (sum >> 16) + (sum & 0xffff);
37 unsigned add_ip_checksums(unsigned offset, unsigned sum, unsigned new)
39 unsigned long checksum;
45 * byte-swap the sum if it came from an odd offset; since the
46 * computation is endian independant this works.
48 new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
51 if (checksum > 0xffff)
54 return (~checksum) & 0xffff;
57 int ip_checksum_ok(const void *addr, unsigned nbytes)
59 return !(compute_ip_checksum(addr, nbytes) & 0xfffe);