xref: /illumos-gate/usr/src/boot/libsa/in_cksum.c (revision 22028508)
1199767f8SToomas Soome /*	$NetBSD: in_cksum.c,v 1.6 2000/03/31 19:55:09 castor Exp $	*/
2199767f8SToomas Soome 
3199767f8SToomas Soome /*
4199767f8SToomas Soome  * Copyright (c) 1992 Regents of the University of California.
5199767f8SToomas Soome  * All rights reserved.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * This software was developed by the Computer Systems Engineering group
8199767f8SToomas Soome  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9199767f8SToomas Soome  * contributed to Berkeley.
10199767f8SToomas Soome  *
11199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
12199767f8SToomas Soome  * modification, are permitted provided that the following conditions
13199767f8SToomas Soome  * are met:
14199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
15199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
16199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
17199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
18199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
190384eafeSToomas Soome  * 3. Neither the name of the University nor the names of its contributors
20199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
21199767f8SToomas Soome  *    without specific prior written permission.
22199767f8SToomas Soome  *
23199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33199767f8SToomas Soome  * SUCH DAMAGE.
34199767f8SToomas Soome  *
35199767f8SToomas Soome  * @(#) Header: in_cksum.c,v 1.1 92/09/11 01:15:55 leres Exp  (LBL)
36199767f8SToomas Soome  */
37199767f8SToomas Soome 
38199767f8SToomas Soome #include <sys/cdefs.h>
39199767f8SToomas Soome 
40199767f8SToomas Soome #include <sys/types.h>
41199767f8SToomas Soome #include <machine/endian.h>
42199767f8SToomas Soome 
43199767f8SToomas Soome #include "stand.h"
44199767f8SToomas Soome 
45199767f8SToomas Soome /*
46199767f8SToomas Soome  * Checksum routine for Internet Protocol family headers.
47199767f8SToomas Soome  * This routine is very heavily used in the network
48199767f8SToomas Soome  * code and should be modified for each CPU to be as fast as possible.
49199767f8SToomas Soome  * In particular, it should not be this one.
50199767f8SToomas Soome  */
51199767f8SToomas Soome int
in_cksum(void * p,int len)520384eafeSToomas Soome in_cksum(void *p, int len)
53199767f8SToomas Soome {
54199767f8SToomas Soome 	int sum = 0, oddbyte = 0, v = 0;
550384eafeSToomas Soome 	uchar_t *cp = p;
56199767f8SToomas Soome 
57199767f8SToomas Soome 	/* we assume < 2^16 bytes being summed */
58199767f8SToomas Soome 	while (len > 0) {
59199767f8SToomas Soome 		if (oddbyte) {
60199767f8SToomas Soome 			sum += v + *cp++;
61199767f8SToomas Soome 			len--;
62199767f8SToomas Soome 		}
63199767f8SToomas Soome 		if (((long)cp & 1) == 0) {
64199767f8SToomas Soome 			while ((len -= 2) >= 0) {
650384eafeSToomas Soome 				sum += *(ushort_t *)cp;
66199767f8SToomas Soome 				cp += 2;
67199767f8SToomas Soome 			}
68199767f8SToomas Soome 		} else {
69199767f8SToomas Soome 			while ((len -= 2) >= 0) {
70199767f8SToomas Soome #if BYTE_ORDER == BIG_ENDIAN
71199767f8SToomas Soome 				sum += *cp++ << 8;
72199767f8SToomas Soome 				sum += *cp++;
73199767f8SToomas Soome #else
74199767f8SToomas Soome 				sum += *cp++;
75199767f8SToomas Soome 				sum += *cp++ << 8;
76199767f8SToomas Soome #endif
77199767f8SToomas Soome 			}
78199767f8SToomas Soome 		}
79199767f8SToomas Soome 		if ((oddbyte = len & 1) != 0)
80199767f8SToomas Soome #if BYTE_ORDER == BIG_ENDIAN
81199767f8SToomas Soome 			v = *cp << 8;
82199767f8SToomas Soome #else
83199767f8SToomas Soome 			v = *cp;
84199767f8SToomas Soome #endif
85199767f8SToomas Soome 	}
86199767f8SToomas Soome 	if (oddbyte)
87199767f8SToomas Soome 		sum += v;
88199767f8SToomas Soome 	sum = (sum >> 16) + (sum & 0xffff); /* add in accumulated carries */
89199767f8SToomas Soome 	sum += sum >> 16;		/* add potential last carry */
90199767f8SToomas Soome 	return (0xffff & ~sum);
91199767f8SToomas Soome }
92