Home | History | Annotate | Line # | Download | only in dist
      1  1.1  christos /* in_cksum.c
      2  1.1  christos  * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
      3  1.1  christos  * pointers/lengths giving the pieces to be checksummed.  Also using
      4  1.1  christos  * Tahoe/CGI version of ADDCARRY(x) macro instead of from portable version.
      5  1.1  christos  */
      6  1.1  christos 
      7  1.1  christos /*
      8  1.1  christos  * Copyright (c) 1988, 1992, 1993
      9  1.1  christos  *	The Regents of the University of California.  All rights reserved.
     10  1.1  christos  *
     11  1.1  christos  * Redistribution and use in source and binary forms, with or without
     12  1.1  christos  * modification, are permitted provided that the following conditions
     13  1.1  christos  * are met:
     14  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  christos  *    documentation and/or other materials provided with the distribution.
     19  1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     20  1.1  christos  *    may be used to endorse or promote products derived from this software
     21  1.1  christos  *    without specific prior written permission.
     22  1.1  christos  *
     23  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  christos  * SUCH DAMAGE.
     34  1.1  christos  *
     35  1.1  christos  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
     36  1.1  christos  */
     37  1.1  christos 
     38  1.2  christos #include <sys/cdefs.h>
     39  1.2  christos #ifndef lint
     40  1.4  christos __RCSID("$NetBSD: in_cksum.c,v 1.4 2024/09/02 16:15:30 christos Exp $");
     41  1.2  christos #endif
     42  1.2  christos 
     43  1.3  christos # include <config.h>
     44  1.1  christos 
     45  1.3  christos #include "netdissect-stdinc.h"
     46  1.1  christos 
     47  1.2  christos #include "netdissect.h"
     48  1.1  christos 
     49  1.1  christos /*
     50  1.1  christos  * Checksum routine for Internet Protocol family headers (Portable Version).
     51  1.1  christos  *
     52  1.1  christos  * This routine is very heavily used in the network
     53  1.1  christos  * code and should be modified for each CPU to be as fast as possible.
     54  1.1  christos  */
     55  1.1  christos 
     56  1.1  christos #define ADDCARRY(x)  {if ((x) > 65535) (x) -= 65535;}
     57  1.1  christos #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
     58  1.1  christos 
     59  1.2  christos uint16_t
     60  1.1  christos in_cksum(const struct cksum_vec *vec, int veclen)
     61  1.1  christos {
     62  1.3  christos 	const uint16_t *w;
     63  1.3  christos 	int sum = 0;
     64  1.3  christos 	int mlen = 0;
     65  1.1  christos 	int byte_swapped = 0;
     66  1.1  christos 
     67  1.1  christos 	union {
     68  1.2  christos 		uint8_t		c[2];
     69  1.2  christos 		uint16_t	s;
     70  1.1  christos 	} s_util;
     71  1.1  christos 	union {
     72  1.2  christos 		uint16_t	s[2];
     73  1.2  christos 		uint32_t	l;
     74  1.1  christos 	} l_util;
     75  1.1  christos 
     76  1.1  christos 	for (; veclen != 0; vec++, veclen--) {
     77  1.1  christos 		if (vec->len == 0)
     78  1.1  christos 			continue;
     79  1.2  christos 		w = (const uint16_t *)(const void *)vec->ptr;
     80  1.1  christos 		if (mlen == -1) {
     81  1.1  christos 			/*
     82  1.1  christos 			 * The first byte of this chunk is the continuation
     83  1.1  christos 			 * of a word spanning between this chunk and the
     84  1.1  christos 			 * last chunk.
     85  1.1  christos 			 *
     86  1.1  christos 			 * s_util.c[0] is already saved when scanning previous
     87  1.1  christos 			 * chunk.
     88  1.1  christos 			 */
     89  1.2  christos 			s_util.c[1] = *(const uint8_t *)w;
     90  1.1  christos 			sum += s_util.s;
     91  1.2  christos 			w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
     92  1.1  christos 			mlen = vec->len - 1;
     93  1.1  christos 		} else
     94  1.1  christos 			mlen = vec->len;
     95  1.1  christos 		/*
     96  1.1  christos 		 * Force to even boundary.
     97  1.1  christos 		 */
     98  1.2  christos 		if ((1 & (uintptr_t) w) && (mlen > 0)) {
     99  1.1  christos 			REDUCE;
    100  1.1  christos 			sum <<= 8;
    101  1.2  christos 			s_util.c[0] = *(const uint8_t *)w;
    102  1.2  christos 			w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
    103  1.1  christos 			mlen--;
    104  1.1  christos 			byte_swapped = 1;
    105  1.1  christos 		}
    106  1.1  christos 		/*
    107  1.1  christos 		 * Unroll the loop to make overhead from
    108  1.1  christos 		 * branches &c small.
    109  1.1  christos 		 */
    110  1.1  christos 		while ((mlen -= 32) >= 0) {
    111  1.1  christos 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    112  1.1  christos 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
    113  1.1  christos 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
    114  1.1  christos 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
    115  1.1  christos 			w += 16;
    116  1.1  christos 		}
    117  1.1  christos 		mlen += 32;
    118  1.1  christos 		while ((mlen -= 8) >= 0) {
    119  1.1  christos 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    120  1.1  christos 			w += 4;
    121  1.1  christos 		}
    122  1.1  christos 		mlen += 8;
    123  1.1  christos 		if (mlen == 0 && byte_swapped == 0)
    124  1.1  christos 			continue;
    125  1.1  christos 		REDUCE;
    126  1.1  christos 		while ((mlen -= 2) >= 0) {
    127  1.1  christos 			sum += *w++;
    128  1.1  christos 		}
    129  1.1  christos 		if (byte_swapped) {
    130  1.1  christos 			REDUCE;
    131  1.1  christos 			sum <<= 8;
    132  1.1  christos 			byte_swapped = 0;
    133  1.1  christos 			if (mlen == -1) {
    134  1.2  christos 				s_util.c[1] = *(const uint8_t *)w;
    135  1.1  christos 				sum += s_util.s;
    136  1.1  christos 				mlen = 0;
    137  1.1  christos 			} else
    138  1.1  christos 				mlen = -1;
    139  1.1  christos 		} else if (mlen == -1)
    140  1.2  christos 			s_util.c[0] = *(const uint8_t *)w;
    141  1.1  christos 	}
    142  1.1  christos 	if (mlen == -1) {
    143  1.1  christos 		/* The last mbuf has odd # of bytes. Follow the
    144  1.1  christos 		   standard (the odd byte may be shifted left by 8 bits
    145  1.1  christos 		   or not as determined by endian-ness of the machine) */
    146  1.1  christos 		s_util.c[1] = 0;
    147  1.1  christos 		sum += s_util.s;
    148  1.1  christos 	}
    149  1.1  christos 	REDUCE;
    150  1.1  christos 	return (~sum & 0xffff);
    151  1.1  christos }
    152  1.1  christos 
    153  1.1  christos /*
    154  1.1  christos  * Given the host-byte-order value of the checksum field in a packet
    155  1.1  christos  * header, and the network-byte-order computed checksum of the data
    156  1.1  christos  * that the checksum covers (including the checksum itself), compute
    157  1.1  christos  * what the checksum field *should* have been.
    158  1.1  christos  */
    159  1.2  christos uint16_t
    160  1.2  christos in_cksum_shouldbe(uint16_t sum, uint16_t computed_sum)
    161  1.1  christos {
    162  1.2  christos 	uint32_t shouldbe;
    163  1.1  christos 
    164  1.1  christos 	/*
    165  1.1  christos 	 * The value that should have gone into the checksum field
    166  1.1  christos 	 * is the negative of the value gotten by summing up everything
    167  1.1  christos 	 * *but* the checksum field.
    168  1.1  christos 	 *
    169  1.1  christos 	 * We can compute that by subtracting the value of the checksum
    170  1.1  christos 	 * field from the sum of all the data in the packet, and then
    171  1.1  christos 	 * computing the negative of that value.
    172  1.1  christos 	 *
    173  1.1  christos 	 * "sum" is the value of the checksum field, and "computed_sum"
    174  1.1  christos 	 * is the negative of the sum of all the data in the packets,
    175  1.1  christos 	 * so that's -(-computed_sum - sum), or (sum + computed_sum).
    176  1.1  christos 	 *
    177  1.1  christos 	 * All the arithmetic in question is one's complement, so the
    178  1.1  christos 	 * addition must include an end-around carry; we do this by
    179  1.1  christos 	 * doing the arithmetic in 32 bits (with no sign-extension),
    180  1.1  christos 	 * and then adding the upper 16 bits of the sum, which contain
    181  1.1  christos 	 * the carry, to the lower 16 bits of the sum, and then do it
    182  1.1  christos 	 * again in case *that* sum produced a carry.
    183  1.1  christos 	 *
    184  1.1  christos 	 * As RFC 1071 notes, the checksum can be computed without
    185  1.1  christos 	 * byte-swapping the 16-bit words; summing 16-bit words
    186  1.1  christos 	 * on a big-endian machine gives a big-endian checksum, which
    187  1.1  christos 	 * can be directly stuffed into the big-endian checksum fields
    188  1.1  christos 	 * in protocol headers, and summing words on a little-endian
    189  1.1  christos 	 * machine gives a little-endian checksum, which must be
    190  1.1  christos 	 * byte-swapped before being stuffed into a big-endian checksum
    191  1.1  christos 	 * field.
    192  1.1  christos 	 *
    193  1.1  christos 	 * "computed_sum" is a network-byte-order value, so we must put
    194  1.1  christos 	 * it in host byte order before subtracting it from the
    195  1.1  christos 	 * host-byte-order value from the header; the adjusted checksum
    196  1.1  christos 	 * will be in host byte order, which is what we'll return.
    197  1.1  christos 	 */
    198  1.1  christos 	shouldbe = sum;
    199  1.1  christos 	shouldbe += ntohs(computed_sum);
    200  1.1  christos 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
    201  1.1  christos 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
    202  1.3  christos 	return (uint16_t)shouldbe;
    203  1.1  christos }
    204