This exact one's-complement algorithm is defined in RFC 1071 and reused for TCP and UDP checksums, plus it's what NIC checksum-offload hardware recomputes in silicon on every outgoing packet. Getting the carry-fold step wrong is a classic bug: Linux's csum_partial() and BSD's in_cksum() both exist because naive 16-bit summation silently drops overflow.