1139823Simp/*-
21541Srgrimes * Copyright (c) 1988, 1992, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
301541Srgrimes */
311541Srgrimes
32172467Ssilby#include <sys/cdefs.h>
33172467Ssilby__FBSDID("$FreeBSD$");
34172467Ssilby
351541Srgrimes#include <sys/param.h>
361541Srgrimes#include <sys/mbuf.h>
371541Srgrimes
381541Srgrimes/*
391541Srgrimes * Checksum routine for Internet Protocol family headers (Portable Version).
401541Srgrimes *
411541Srgrimes * This routine is very heavily used in the network
421541Srgrimes * code and should be modified for each CPU to be as fast as possible.
431541Srgrimes */
441541Srgrimes
451541Srgrimes#define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
461541Srgrimes#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
471541Srgrimes
481541Srgrimesint
49169454Srwatsonin_cksum(struct mbuf *m, int len)
501541Srgrimes{
511541Srgrimes	register u_short *w;
521541Srgrimes	register int sum = 0;
531541Srgrimes	register int mlen = 0;
541541Srgrimes	int byte_swapped = 0;
551541Srgrimes
561541Srgrimes	union {
571541Srgrimes		char	c[2];
581541Srgrimes		u_short	s;
591541Srgrimes	} s_util;
601541Srgrimes	union {
611541Srgrimes		u_short s[2];
621541Srgrimes		long	l;
631541Srgrimes	} l_util;
641541Srgrimes
651541Srgrimes	for (;m && len; m = m->m_next) {
661541Srgrimes		if (m->m_len == 0)
671541Srgrimes			continue;
681541Srgrimes		w = mtod(m, u_short *);
691541Srgrimes		if (mlen == -1) {
701541Srgrimes			/*
711541Srgrimes			 * The first byte of this mbuf is the continuation
721541Srgrimes			 * of a word spanning between this mbuf and the
731541Srgrimes			 * last mbuf.
741541Srgrimes			 *
758876Srgrimes			 * s_util.c[0] is already saved when scanning previous
761541Srgrimes			 * mbuf.
771541Srgrimes			 */
781541Srgrimes			s_util.c[1] = *(char *)w;
791541Srgrimes			sum += s_util.s;
801541Srgrimes			w = (u_short *)((char *)w + 1);
811541Srgrimes			mlen = m->m_len - 1;
821541Srgrimes			len--;
831541Srgrimes		} else
841541Srgrimes			mlen = m->m_len;
851541Srgrimes		if (len < mlen)
861541Srgrimes			mlen = len;
871541Srgrimes		len -= mlen;
881541Srgrimes		/*
891541Srgrimes		 * Force to even boundary.
901541Srgrimes		 */
91238941Sluigi		if ((1 & (uintptr_t) w) && (mlen > 0)) {
921541Srgrimes			REDUCE;
931541Srgrimes			sum <<= 8;
941541Srgrimes			s_util.c[0] = *(u_char *)w;
951541Srgrimes			w = (u_short *)((char *)w + 1);
961541Srgrimes			mlen--;
971541Srgrimes			byte_swapped = 1;
981541Srgrimes		}
991541Srgrimes		/*
1001541Srgrimes		 * Unroll the loop to make overhead from
1011541Srgrimes		 * branches &c small.
1021541Srgrimes		 */
1031541Srgrimes		while ((mlen -= 32) >= 0) {
1041541Srgrimes			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
1051541Srgrimes			sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
1061541Srgrimes			sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
1071541Srgrimes			sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
1081541Srgrimes			w += 16;
1091541Srgrimes		}
1101541Srgrimes		mlen += 32;
1111541Srgrimes		while ((mlen -= 8) >= 0) {
1121541Srgrimes			sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
1131541Srgrimes			w += 4;
1141541Srgrimes		}
1151541Srgrimes		mlen += 8;
1161541Srgrimes		if (mlen == 0 && byte_swapped == 0)
1171541Srgrimes			continue;
1181541Srgrimes		REDUCE;
1191541Srgrimes		while ((mlen -= 2) >= 0) {
1201541Srgrimes			sum += *w++;
1211541Srgrimes		}
1221541Srgrimes		if (byte_swapped) {
1231541Srgrimes			REDUCE;
1241541Srgrimes			sum <<= 8;
1251541Srgrimes			byte_swapped = 0;
1261541Srgrimes			if (mlen == -1) {
1271541Srgrimes				s_util.c[1] = *(char *)w;
1281541Srgrimes				sum += s_util.s;
1291541Srgrimes				mlen = 0;
1301541Srgrimes			} else
1311541Srgrimes				mlen = -1;
1321541Srgrimes		} else if (mlen == -1)
1331541Srgrimes			s_util.c[0] = *(char *)w;
1341541Srgrimes	}
1351541Srgrimes	if (len)
1361541Srgrimes		printf("cksum: out of data\n");
1371541Srgrimes	if (mlen == -1) {
1381541Srgrimes		/* The last mbuf has odd # of bytes. Follow the
1391541Srgrimes		   standard (the odd byte may be shifted left by 8 bits
1401541Srgrimes		   or not as determined by endian-ness of the machine) */
1411541Srgrimes		s_util.c[1] = 0;
1421541Srgrimes		sum += s_util.s;
1431541Srgrimes	}
1441541Srgrimes	REDUCE;
1451541Srgrimes	return (~sum & 0xffff);
1461541Srgrimes}
147