xref: /openbsd-src/usr.sbin/tcpdump/in_cksum.c (revision 3a50f0a93a2072911d0ba6ababa815fb04bf9a71)
1*3a50f0a9Sjmc /*	$OpenBSD: in_cksum.c,v 1.3 2022/12/28 21:30:19 jmc Exp $	*/
27988cf09Slteo 
37988cf09Slteo /*
47988cf09Slteo  * Copyright (c) 1988, 1992, 1993
57988cf09Slteo  *	The Regents of the University of California.  All rights reserved.
67988cf09Slteo  *
77988cf09Slteo  * Redistribution and use in source and binary forms, with or without
87988cf09Slteo  * modification, are permitted provided that the following conditions
97988cf09Slteo  * are met:
107988cf09Slteo  * 1. Redistributions of source code must retain the above copyright
117988cf09Slteo  *    notice, this list of conditions and the following disclaimer.
127988cf09Slteo  * 2. Redistributions in binary form must reproduce the above copyright
137988cf09Slteo  *    notice, this list of conditions and the following disclaimer in the
147988cf09Slteo  *    documentation and/or other materials provided with the distribution.
157988cf09Slteo  * 3. Neither the name of the University nor the names of its contributors
167988cf09Slteo  *    may be used to endorse or promote products derived from this software
177988cf09Slteo  *    without specific prior written permission.
187988cf09Slteo  *
197988cf09Slteo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
207988cf09Slteo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217988cf09Slteo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227988cf09Slteo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
237988cf09Slteo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247988cf09Slteo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257988cf09Slteo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267988cf09Slteo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277988cf09Slteo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287988cf09Slteo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297988cf09Slteo  * SUCH DAMAGE.
307988cf09Slteo  *
317988cf09Slteo  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
327988cf09Slteo  */
337988cf09Slteo 
347988cf09Slteo #include <sys/types.h>
357988cf09Slteo 
367988cf09Slteo #include "interface.h"
377988cf09Slteo 
387988cf09Slteo /*
397988cf09Slteo  * Given the host-byte-order value of the checksum field in a packet
407988cf09Slteo  * header, and the network-byte-order computed checksum of the data
417988cf09Slteo  * that the checksum covers (including the checksum itself), compute
427988cf09Slteo  * what the checksum field *should* have been.
437988cf09Slteo  */
447988cf09Slteo u_int16_t
in_cksum_shouldbe(u_int16_t sum,u_int16_t computed_sum)457988cf09Slteo in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
467988cf09Slteo {
477988cf09Slteo 	u_int32_t shouldbe;
487988cf09Slteo 
497988cf09Slteo 	/*
507988cf09Slteo 	 * The value that should have gone into the checksum field
517988cf09Slteo 	 * is the negative of the value gotten by summing up everything
527988cf09Slteo 	 * *but* the checksum field.
537988cf09Slteo 	 *
547988cf09Slteo 	 * We can compute that by subtracting the value of the checksum
557988cf09Slteo 	 * field from the sum of all the data in the packet, and then
567988cf09Slteo 	 * computing the negative of that value.
577988cf09Slteo 	 *
587988cf09Slteo 	 * "sum" is the value of the checksum field, and "computed_sum"
597988cf09Slteo 	 * is the negative of the sum of all the data in the packets,
607988cf09Slteo 	 * so that's -(-computed_sum - sum), or (sum + computed_sum).
617988cf09Slteo 	 *
627988cf09Slteo 	 * All the arithmetic in question is one's complement, so the
637988cf09Slteo 	 * addition must include an end-around carry; we do this by
647988cf09Slteo 	 * doing the arithmetic in 32 bits (with no sign-extension),
657988cf09Slteo 	 * and then adding the upper 16 bits of the sum, which contain
667988cf09Slteo 	 * the carry, to the lower 16 bits of the sum, and then do it
677988cf09Slteo 	 * again in case *that* sum produced a carry.
687988cf09Slteo 	 *
697988cf09Slteo 	 * As RFC 1071 notes, the checksum can be computed without
707988cf09Slteo 	 * byte-swapping the 16-bit words; summing 16-bit words
717988cf09Slteo 	 * on a big-endian machine gives a big-endian checksum, which
727988cf09Slteo 	 * can be directly stuffed into the big-endian checksum fields
737988cf09Slteo 	 * in protocol headers, and summing words on a little-endian
747988cf09Slteo 	 * machine gives a little-endian checksum, which must be
757988cf09Slteo 	 * byte-swapped before being stuffed into a big-endian checksum
767988cf09Slteo 	 * field.
777988cf09Slteo 	 *
787988cf09Slteo 	 * "computed_sum" is a network-byte-order value, so we must put
797988cf09Slteo 	 * it in host byte order before subtracting it from the
807988cf09Slteo 	 * host-byte-order value from the header; the adjusted checksum
817988cf09Slteo 	 * will be in host byte order, which is what we'll return.
827988cf09Slteo 	 */
837988cf09Slteo 	shouldbe = sum;
847988cf09Slteo 	shouldbe += ntohs(computed_sum);
857988cf09Slteo 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
867988cf09Slteo 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
877988cf09Slteo 	return shouldbe;
887988cf09Slteo }
89dd17d5caSdlg 
90dd17d5caSdlg uint32_t
in_cksum_add(const void * buf,size_t len,uint32_t sum)91dd17d5caSdlg in_cksum_add(const void *buf, size_t len, uint32_t sum)
92dd17d5caSdlg {
93dd17d5caSdlg 	const uint16_t *words = buf;
94dd17d5caSdlg 
95dd17d5caSdlg 	while (len > 1) {
96dd17d5caSdlg 		sum += *words++;
97dd17d5caSdlg 		len -= sizeof(*words);
98dd17d5caSdlg 	}
99dd17d5caSdlg 
100dd17d5caSdlg 	if (len == 1) {
101dd17d5caSdlg 		uint8_t byte = *(const uint8_t *)words;
102dd17d5caSdlg 		sum += htons(byte << 8);
103dd17d5caSdlg 	}
104dd17d5caSdlg 
105dd17d5caSdlg 	return (sum);
106dd17d5caSdlg }
107dd17d5caSdlg 
108dd17d5caSdlg uint16_t
in_cksum_fini(uint32_t sum)109dd17d5caSdlg in_cksum_fini(uint32_t sum)
110dd17d5caSdlg {
111dd17d5caSdlg 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
112dd17d5caSdlg 	sum += (sum >> 16);			/* add carry */
113dd17d5caSdlg 
114dd17d5caSdlg 	return (~sum);
115dd17d5caSdlg }
116dd17d5caSdlg 
117dd17d5caSdlg /*
118dd17d5caSdlg  * compute an IP header checksum.
119*3a50f0a9Sjmc  * don't modify the packet.
120dd17d5caSdlg  */
121dd17d5caSdlg uint16_t
in_cksum(const void * addr,size_t len,uint32_t sum)122dd17d5caSdlg in_cksum(const void *addr, size_t len, uint32_t sum)
123dd17d5caSdlg {
124dd17d5caSdlg 	return (in_cksum_fini(in_cksum_add(addr, len, sum)));
125dd17d5caSdlg }
126