11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1985, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 4. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes */
291553Srgrimes
301553Srgrimes#ifndef lint
3130642Scharnier#if 0
321553Srgrimesstatic char sccsid[] = "@(#)cksum.c	8.1 (Berkeley) 6/6/93";
3330642Scharnier#endif
3430642Scharnierstatic const char rcsid[] =
3550479Speter  "$FreeBSD$";
361553Srgrimes#endif /* not lint */
371553Srgrimes
381553Srgrimes#include <sys/types.h>
391553Srgrimes
401553Srgrimes/*
411553Srgrimes *			I N _ C K S U M
421553Srgrimes *
431553Srgrimes * Checksum routine for Internet Protocol family headers (C Version)
441553Srgrimes *
451553Srgrimes * There is no profit in a specialized version of the checksum
461553Srgrimes * function for any machine where int's are 32 bits and shorts are 16.
471553Srgrimes *
481553Srgrimes * All timed packets are smaller than 32K shorts, so there is no need to
491553Srgrimes * worry about carries except at the end.
501553Srgrimes */
511553Srgrimesint
52246209Scharnierin_cksum(u_short *addr, int len)
531553Srgrimes{
541553Srgrimes	register int nleft = len;
551553Srgrimes	register u_short *w = addr;
561553Srgrimes	register u_short answer;
571553Srgrimes	register int sum = 0;
581553Srgrimes
591553Srgrimes	/*
601553Srgrimes	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
611553Srgrimes	 *  we add sequential 16 bit words to it, and at the end, fold
621553Srgrimes	 *  back all the carry bits from the top 16 bits into the lower
631553Srgrimes	 *  16 bits.
641553Srgrimes	 */
651553Srgrimes	while( nleft > 1 )  {
661553Srgrimes		sum += *w++;
671553Srgrimes		nleft -= 2;
681553Srgrimes	}
691553Srgrimes
701553Srgrimes	/* mop up an odd byte, if necessary */
711553Srgrimes	if( nleft == 1 )
721553Srgrimes		sum += (*(u_char *)w) << 8;
731553Srgrimes
741553Srgrimes	/*
751553Srgrimes	 * add back carry outs from top 16 bits to low 16 bits
761553Srgrimes	 */
771553Srgrimes	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
781553Srgrimes	sum += (sum >> 16);			/* add carry */
791553Srgrimes	answer = ~sum;				/* truncate to 16 bits */
801553Srgrimes	return (answer);
811553Srgrimes}
82