Home | History | Annotate | Line # | Download | only in dist
in_cksum.c revision 1.1
      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.1  christos #ifdef HAVE_CONFIG_H
     39  1.1  christos # include "config.h"
     40  1.1  christos #endif
     41  1.1  christos 
     42  1.1  christos #include <tcpdump-stdinc.h>
     43  1.1  christos 
     44  1.1  christos #include "interface.h"
     45  1.1  christos 
     46  1.1  christos /*
     47  1.1  christos  * Checksum routine for Internet Protocol family headers (Portable Version).
     48  1.1  christos  *
     49  1.1  christos  * This routine is very heavily used in the network
     50  1.1  christos  * code and should be modified for each CPU to be as fast as possible.
     51  1.1  christos  */
     52  1.1  christos 
     53  1.1  christos #define ADDCARRY(x)  {if ((x) > 65535) (x) -= 65535;}
     54  1.1  christos #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
     55  1.1  christos 
     56  1.1  christos u_int16_t
     57  1.1  christos in_cksum(const struct cksum_vec *vec, int veclen)
     58  1.1  christos {
     59  1.1  christos 	register const u_int16_t *w;
     60  1.1  christos 	register int sum = 0;
     61  1.1  christos 	register int mlen = 0;
     62  1.1  christos 	int byte_swapped = 0;
     63  1.1  christos 
     64  1.1  christos 	union {
     65  1.1  christos 		u_int8_t	c[2];
     66  1.1  christos 		u_int16_t	s;
     67  1.1  christos 	} s_util;
     68  1.1  christos 	union {
     69  1.1  christos 		u_int16_t	s[2];
     70  1.1  christos 		u_int32_t	l;
     71  1.1  christos 	} l_util;
     72  1.1  christos 
     73  1.1  christos 	for (; veclen != 0; vec++, veclen--) {
     74  1.1  christos 		if (vec->len == 0)
     75  1.1  christos 			continue;
     76  1.1  christos 		w = (const u_int16_t *)(void *)vec->ptr;
     77  1.1  christos 		if (mlen == -1) {
     78  1.1  christos 			/*
     79  1.1  christos 			 * The first byte of this chunk is the continuation
     80  1.1  christos 			 * of a word spanning between this chunk and the
     81  1.1  christos 			 * last chunk.
     82  1.1  christos 			 *
     83  1.1  christos 			 * s_util.c[0] is already saved when scanning previous
     84  1.1  christos 			 * chunk.
     85  1.1  christos 			 */
     86  1.1  christos 			s_util.c[1] = *(const u_int8_t *)w;
     87  1.1  christos 			sum += s_util.s;
     88  1.1  christos 			w = (const u_int16_t *)(void *)((const u_int8_t *)w + 1);
     89  1.1  christos 			mlen = vec->len - 1;
     90  1.1  christos 		} else
     91  1.1  christos 			mlen = vec->len;
     92  1.1  christos 		/*
     93  1.1  christos 		 * Force to even boundary.
     94  1.1  christos 		 */
     95  1.1  christos 		if ((1 & (unsigned long) w) && (mlen > 0)) {
     96  1.1  christos 			REDUCE;
     97  1.1  christos 			sum <<= 8;
     98  1.1  christos 			s_util.c[0] = *(const u_int8_t *)w;
     99  1.1  christos 			w = (const u_int16_t *)(void *)((const u_int8_t *)w + 1);
    100  1.1  christos 			mlen--;
    101  1.1  christos 			byte_swapped = 1;
    102  1.1  christos 		}
    103  1.1  christos 		/*
    104  1.1  christos 		 * Unroll the loop to make overhead from
    105  1.1  christos 		 * branches &c small.
    106  1.1  christos 		 */
    107  1.1  christos 		while ((mlen -= 32) >= 0) {
    108  1.1  christos 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    109  1.1  christos 			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
    110  1.1  christos 			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
    111  1.1  christos 			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
    112  1.1  christos 			w += 16;
    113  1.1  christos 		}
    114  1.1  christos 		mlen += 32;
    115  1.1  christos 		while ((mlen -= 8) >= 0) {
    116  1.1  christos 			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
    117  1.1  christos 			w += 4;
    118  1.1  christos 		}
    119  1.1  christos 		mlen += 8;
    120  1.1  christos 		if (mlen == 0 && byte_swapped == 0)
    121  1.1  christos 			continue;
    122  1.1  christos 		REDUCE;
    123  1.1  christos 		while ((mlen -= 2) >= 0) {
    124  1.1  christos 			sum += *w++;
    125  1.1  christos 		}
    126  1.1  christos 		if (byte_swapped) {
    127  1.1  christos 			REDUCE;
    128  1.1  christos 			sum <<= 8;
    129  1.1  christos 			byte_swapped = 0;
    130  1.1  christos 			if (mlen == -1) {
    131  1.1  christos 				s_util.c[1] = *(const u_int8_t *)w;
    132  1.1  christos 				sum += s_util.s;
    133  1.1  christos 				mlen = 0;
    134  1.1  christos 			} else
    135  1.1  christos 				mlen = -1;
    136  1.1  christos 		} else if (mlen == -1)
    137  1.1  christos 			s_util.c[0] = *(const u_int8_t *)w;
    138  1.1  christos 	}
    139  1.1  christos 	if (mlen == -1) {
    140  1.1  christos 		/* The last mbuf has odd # of bytes. Follow the
    141  1.1  christos 		   standard (the odd byte may be shifted left by 8 bits
    142  1.1  christos 		   or not as determined by endian-ness of the machine) */
    143  1.1  christos 		s_util.c[1] = 0;
    144  1.1  christos 		sum += s_util.s;
    145  1.1  christos 	}
    146  1.1  christos 	REDUCE;
    147  1.1  christos 	return (~sum & 0xffff);
    148  1.1  christos }
    149  1.1  christos 
    150  1.1  christos /*
    151  1.1  christos  * Given the host-byte-order value of the checksum field in a packet
    152  1.1  christos  * header, and the network-byte-order computed checksum of the data
    153  1.1  christos  * that the checksum covers (including the checksum itself), compute
    154  1.1  christos  * what the checksum field *should* have been.
    155  1.1  christos  */
    156  1.1  christos u_int16_t
    157  1.1  christos in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
    158  1.1  christos {
    159  1.1  christos 	u_int32_t shouldbe;
    160  1.1  christos 
    161  1.1  christos 	/*
    162  1.1  christos 	 * The value that should have gone into the checksum field
    163  1.1  christos 	 * is the negative of the value gotten by summing up everything
    164  1.1  christos 	 * *but* the checksum field.
    165  1.1  christos 	 *
    166  1.1  christos 	 * We can compute that by subtracting the value of the checksum
    167  1.1  christos 	 * field from the sum of all the data in the packet, and then
    168  1.1  christos 	 * computing the negative of that value.
    169  1.1  christos 	 *
    170  1.1  christos 	 * "sum" is the value of the checksum field, and "computed_sum"
    171  1.1  christos 	 * is the negative of the sum of all the data in the packets,
    172  1.1  christos 	 * so that's -(-computed_sum - sum), or (sum + computed_sum).
    173  1.1  christos 	 *
    174  1.1  christos 	 * All the arithmetic in question is one's complement, so the
    175  1.1  christos 	 * addition must include an end-around carry; we do this by
    176  1.1  christos 	 * doing the arithmetic in 32 bits (with no sign-extension),
    177  1.1  christos 	 * and then adding the upper 16 bits of the sum, which contain
    178  1.1  christos 	 * the carry, to the lower 16 bits of the sum, and then do it
    179  1.1  christos 	 * again in case *that* sum produced a carry.
    180  1.1  christos 	 *
    181  1.1  christos 	 * As RFC 1071 notes, the checksum can be computed without
    182  1.1  christos 	 * byte-swapping the 16-bit words; summing 16-bit words
    183  1.1  christos 	 * on a big-endian machine gives a big-endian checksum, which
    184  1.1  christos 	 * can be directly stuffed into the big-endian checksum fields
    185  1.1  christos 	 * in protocol headers, and summing words on a little-endian
    186  1.1  christos 	 * machine gives a little-endian checksum, which must be
    187  1.1  christos 	 * byte-swapped before being stuffed into a big-endian checksum
    188  1.1  christos 	 * field.
    189  1.1  christos 	 *
    190  1.1  christos 	 * "computed_sum" is a network-byte-order value, so we must put
    191  1.1  christos 	 * it in host byte order before subtracting it from the
    192  1.1  christos 	 * host-byte-order value from the header; the adjusted checksum
    193  1.1  christos 	 * will be in host byte order, which is what we'll return.
    194  1.1  christos 	 */
    195  1.1  christos 	shouldbe = sum;
    196  1.1  christos 	shouldbe += ntohs(computed_sum);
    197  1.1  christos 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
    198  1.1  christos 	shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
    199  1.1  christos 	return shouldbe;
    200  1.1  christos }
    201