xref: /illumos-gate/usr/src/uts/common/inet/ip/ipcsum.c (revision 2d6eb4a5)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1992,1997 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
32*7c478bd9Sstevel@tonic-gate #include <inet/common.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #define	FOLD_SUM(sum) \
35*7c478bd9Sstevel@tonic-gate { sum = (sum >> 16) + (sum & 0xFFFF); sum = (sum >> 16) + (sum & 0xFFFF); }
36*7c478bd9Sstevel@tonic-gate #define	U16AM(p, i, m)	((((uint16_t *)(p))[i]) & (uint32_t)(m))
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * For maximum efficiency, these access macros should be redone for
40*7c478bd9Sstevel@tonic-gate  * machines that can access unaligned data.  NOTE: these assume
41*7c478bd9Sstevel@tonic-gate  * ability to fetch from a zero extended 'uint8_t' and 'uint16_t'.  Add explicit
42*7c478bd9Sstevel@tonic-gate  * masks in the U8_FETCH, U16_FETCH, PREV_TWO and NEXT_TWO as needed.
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #ifdef	_LITTLE_ENDIAN
46*7c478bd9Sstevel@tonic-gate #define	U8_FETCH_FIRST(p)	((p)[0])
47*7c478bd9Sstevel@tonic-gate #define	U8_FETCH_SECOND(p)	(((uint32_t)(p)[0]) << 8)
48*7c478bd9Sstevel@tonic-gate #define	PREV_ONE(p)		U16AM(p, -1, 0xFF00)
49*7c478bd9Sstevel@tonic-gate #define	NEXT_ONE(p)		U16AM(p, 0, 0xFF)
50*7c478bd9Sstevel@tonic-gate #else
51*7c478bd9Sstevel@tonic-gate #define	U8_FETCH_FIRST(p)	((uint32_t)((p)[0]) << 8)
52*7c478bd9Sstevel@tonic-gate #define	U8_FETCH_SECOND(p)	((p)[0])
53*7c478bd9Sstevel@tonic-gate #define	PREV_ONE(p)		U16AM(p, -1, 0xFF)
54*7c478bd9Sstevel@tonic-gate #define	NEXT_ONE(p)		U16AM(p, 0, 0xFF00)
55*7c478bd9Sstevel@tonic-gate #endif
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #define	U16_FETCH(p)		U8_FETCH_FIRST(p) + U8_FETCH_SECOND(p+1)
58*7c478bd9Sstevel@tonic-gate #define	PREV_TWO(p)		((uint32_t)(((uint16_t *)(p))[-1]))
59*7c478bd9Sstevel@tonic-gate #define	NEXT_TWO(p)		((uint32_t)(((uint16_t *)(p))[0]))
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * Return the ones complement checksum from the mblk chain at mp,
63*7c478bd9Sstevel@tonic-gate  * after skipping offset bytes, and adding in the supplied partial
64*7c478bd9Sstevel@tonic-gate  * sum.  Note that a final complement of the return value is needed
65*7c478bd9Sstevel@tonic-gate  * if no further contributions to the checksum are forthcoming.
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate uint16_t
ip_csum(mp,offset,sum)68*7c478bd9Sstevel@tonic-gate ip_csum(mp, offset, sum)
69*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
70*7c478bd9Sstevel@tonic-gate 	int	offset;
71*7c478bd9Sstevel@tonic-gate 	uint32_t	sum;
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	uint8_t	*startp = mp->b_rptr + offset;
74*7c478bd9Sstevel@tonic-gate 	uint8_t	*endp = mp->b_wptr;
75*7c478bd9Sstevel@tonic-gate /* >= 0x2 means flipped for memory align, 0x1 means last count was odd */
76*7c478bd9Sstevel@tonic-gate 	int	odd_total = 0;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #ifdef	TEST_COVERAGE
79*7c478bd9Sstevel@tonic-gate 	mblk_t *safe_mp;
80*7c478bd9Sstevel@tonic-gate #define	INIT_COVERAGE()	(safe_mp = mp, safe_mp->b_next = NULL)
81*7c478bd9Sstevel@tonic-gate #define	MARK_COVERAGE(flag) (safe_mp->b_next = \
82*7c478bd9Sstevel@tonic-gate 	(mblk_t *)((uint32_t)safe_mp->b_next | flag))
83*7c478bd9Sstevel@tonic-gate #else
84*7c478bd9Sstevel@tonic-gate #define	INIT_COVERAGE()	/* */
85*7c478bd9Sstevel@tonic-gate #define	MARK_COVERAGE(flag)	/* */
86*7c478bd9Sstevel@tonic-gate #endif
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	for (;;) {
89*7c478bd9Sstevel@tonic-gate 		INIT_COVERAGE();
90*7c478bd9Sstevel@tonic-gate 		if ((endp - startp) < 10) {
91*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x1);
92*7c478bd9Sstevel@tonic-gate 			while ((endp - startp) >= 2) {
93*7c478bd9Sstevel@tonic-gate 				MARK_COVERAGE(0x2);
94*7c478bd9Sstevel@tonic-gate 				sum += U16_FETCH(startp);
95*7c478bd9Sstevel@tonic-gate 				startp += 2;
96*7c478bd9Sstevel@tonic-gate 			}
97*7c478bd9Sstevel@tonic-gate 			if ((endp - startp) >= 1) {
98*7c478bd9Sstevel@tonic-gate 				MARK_COVERAGE(0x4);
99*7c478bd9Sstevel@tonic-gate 				odd_total = 1;
100*7c478bd9Sstevel@tonic-gate 				sum += U8_FETCH_FIRST(startp);
101*7c478bd9Sstevel@tonic-gate 			}
102*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x8);
103*7c478bd9Sstevel@tonic-gate 			FOLD_SUM(sum);
104*7c478bd9Sstevel@tonic-gate 			goto next_frag;
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 		if ((uint32_t)startp & 0x1) {
107*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x10);
108*7c478bd9Sstevel@tonic-gate 			odd_total = 3;
109*7c478bd9Sstevel@tonic-gate 			startp++;
110*7c478bd9Sstevel@tonic-gate 			sum = (sum << 8) + PREV_ONE(startp);
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 		if ((uint32_t)startp & 0x2) {
113*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x20);
114*7c478bd9Sstevel@tonic-gate 			startp += 2;
115*7c478bd9Sstevel@tonic-gate 			sum += PREV_TWO(startp);
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 		if ((uint32_t)endp & 0x1) {
118*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x40);
119*7c478bd9Sstevel@tonic-gate 			odd_total ^= 0x1;
120*7c478bd9Sstevel@tonic-gate 			endp--;
121*7c478bd9Sstevel@tonic-gate 			sum += NEXT_ONE(endp);
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 		if ((uint32_t)endp & 0x2) {
124*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x80);
125*7c478bd9Sstevel@tonic-gate 			endp -= 2;
126*7c478bd9Sstevel@tonic-gate 			sum += NEXT_TWO(endp);
127*7c478bd9Sstevel@tonic-gate 		}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 		{
130*7c478bd9Sstevel@tonic-gate #ifdef	NOT_ALL_PTRS_EQUAL
131*7c478bd9Sstevel@tonic-gate #define	INC_PTR(cnt)	ptr += cnt
132*7c478bd9Sstevel@tonic-gate #define	INC_ENDPTR(cnt)	endptr += cnt
133*7c478bd9Sstevel@tonic-gate 			uint32_t	*ptr = (uint32_t *)startp;
134*7c478bd9Sstevel@tonic-gate 			uint32_t	*endptr = (uint32_t *)endp;
135*7c478bd9Sstevel@tonic-gate #else
136*7c478bd9Sstevel@tonic-gate #define	INC_PTR(cnt)	startp += (cnt * sizeof (uint32_t))
137*7c478bd9Sstevel@tonic-gate #define	INC_ENDPTR(cnt)	endp += (cnt * sizeof (uint32_t))
138*7c478bd9Sstevel@tonic-gate #define	ptr		((uint32_t *)startp)
139*7c478bd9Sstevel@tonic-gate #define	endptr		((uint32_t *)endp)
140*7c478bd9Sstevel@tonic-gate #endif
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate #ifdef	USE_FETCH_AND_SHIFT
144*7c478bd9Sstevel@tonic-gate 			uint32_t	u1, u2;
145*7c478bd9Sstevel@tonic-gate 			uint32_t	mask = 0xFFFF;
146*7c478bd9Sstevel@tonic-gate #define	LOAD1(i)	u1 = ptr[i]
147*7c478bd9Sstevel@tonic-gate #define	LOAD2(i)	u2 = ptr[i]
148*7c478bd9Sstevel@tonic-gate #define	SUM1(i)		sum += (u1 & mask) + (u1 >> 16)
149*7c478bd9Sstevel@tonic-gate #define	SUM2(i)		sum += (u2 & mask) + (u2 >> 16)
150*7c478bd9Sstevel@tonic-gate #endif
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate #ifdef	USE_FETCH_AND_ADDC
153*7c478bd9Sstevel@tonic-gate 			uint32_t	u1, u2;
154*7c478bd9Sstevel@tonic-gate #define	LOAD1(i)	u1 = ptr[i]
155*7c478bd9Sstevel@tonic-gate #define	LOAD2(i)	u2 = ptr[i]
156*7c478bd9Sstevel@tonic-gate #define	SUM1(i)		sum += u1
157*7c478bd9Sstevel@tonic-gate #define	SUM2(i)		sum += u2
158*7c478bd9Sstevel@tonic-gate #endif
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate #ifdef	USE_ADDC
161*7c478bd9Sstevel@tonic-gate #define	SUM1(i)		sum += ptr[i]
162*7c478bd9Sstevel@tonic-gate #endif
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate #ifdef	USE_POSTINC
165*7c478bd9Sstevel@tonic-gate #define	SUM1(i)		sum += *((uint16_t *)ptr)++; sum += *((uint16_t *)ptr)++
166*7c478bd9Sstevel@tonic-gate #undef	INC_PTR
167*7c478bd9Sstevel@tonic-gate #define	INC_PTR(i)	/* */
168*7c478bd9Sstevel@tonic-gate #endif
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate #ifndef	LOAD1
171*7c478bd9Sstevel@tonic-gate #define	LOAD1(i)	/* */
172*7c478bd9Sstevel@tonic-gate #endif
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate #ifndef	LOAD2
175*7c478bd9Sstevel@tonic-gate #define	LOAD2(i)	/* */
176*7c478bd9Sstevel@tonic-gate #endif
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate #ifndef	SUM2
179*7c478bd9Sstevel@tonic-gate #define	SUM2(i)		SUM1(i)
180*7c478bd9Sstevel@tonic-gate #endif
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate /* USE_INDEXING is the default */
183*7c478bd9Sstevel@tonic-gate #ifndef	SUM1
184*7c478bd9Sstevel@tonic-gate #define	SUM1(i)
185*7c478bd9Sstevel@tonic-gate 	sum += ((uint16_t *)ptr)[i * 2]; sum += ((uint16_t *)ptr)[(i * 2) + 1]
186*7c478bd9Sstevel@tonic-gate #endif
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 		LOAD1(0);
189*7c478bd9Sstevel@tonic-gate 		INC_ENDPTR(-8);
190*7c478bd9Sstevel@tonic-gate 		if (ptr <= endptr) {
191*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x100);
192*7c478bd9Sstevel@tonic-gate 			do {
193*7c478bd9Sstevel@tonic-gate 				LOAD2(1); SUM1(0);
194*7c478bd9Sstevel@tonic-gate 				LOAD1(2); SUM2(1);
195*7c478bd9Sstevel@tonic-gate 				LOAD2(3); SUM1(2);
196*7c478bd9Sstevel@tonic-gate 				LOAD1(4); SUM2(3);
197*7c478bd9Sstevel@tonic-gate 				LOAD2(5); SUM1(4);
198*7c478bd9Sstevel@tonic-gate 				LOAD1(6); SUM2(5);
199*7c478bd9Sstevel@tonic-gate 				LOAD2(7); SUM1(6);
200*7c478bd9Sstevel@tonic-gate 				LOAD1(8); SUM2(7);
201*7c478bd9Sstevel@tonic-gate 				INC_PTR(8);
202*7c478bd9Sstevel@tonic-gate 			} while (ptr <= endptr);
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate #ifdef USE_TAIL_SWITCH
205*7c478bd9Sstevel@tonic-gate 		switch ((endptr + 8) - ptr) {
206*7c478bd9Sstevel@tonic-gate 		case 7:	LOAD2(6); SUM2(6);
207*7c478bd9Sstevel@tonic-gate 		case 6:	LOAD2(5); SUM2(5);
208*7c478bd9Sstevel@tonic-gate 		case 5:	LOAD2(4); SUM2(4);
209*7c478bd9Sstevel@tonic-gate 		case 4:	LOAD2(3); SUM2(3);
210*7c478bd9Sstevel@tonic-gate 		case 3:	LOAD2(2); SUM2(2);
211*7c478bd9Sstevel@tonic-gate 		case 2:	LOAD2(1); SUM2(1);
212*7c478bd9Sstevel@tonic-gate 		case 1:	SUM1(0);
213*7c478bd9Sstevel@tonic-gate 		case 0:	break;
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate #else
216*7c478bd9Sstevel@tonic-gate 		INC_ENDPTR(4);
217*7c478bd9Sstevel@tonic-gate 		if (ptr <= endptr) {
218*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x200);
219*7c478bd9Sstevel@tonic-gate 			LOAD2(1); SUM1(0);
220*7c478bd9Sstevel@tonic-gate 			LOAD1(2); SUM2(1);
221*7c478bd9Sstevel@tonic-gate 			LOAD2(3); SUM1(2);
222*7c478bd9Sstevel@tonic-gate 			LOAD1(4); SUM2(3);
223*7c478bd9Sstevel@tonic-gate 			INC_PTR(4);
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 		INC_ENDPTR(4);
226*7c478bd9Sstevel@tonic-gate 		if (ptr < endptr) {
227*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x400);
228*7c478bd9Sstevel@tonic-gate 			do {
229*7c478bd9Sstevel@tonic-gate 				SUM1(0); LOAD1(1);
230*7c478bd9Sstevel@tonic-gate 				INC_PTR(1);
231*7c478bd9Sstevel@tonic-gate 			} while (ptr < endptr);
232*7c478bd9Sstevel@tonic-gate 		}
233*7c478bd9Sstevel@tonic-gate #endif
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 		FOLD_SUM(sum);
237*7c478bd9Sstevel@tonic-gate 		if (odd_total > 1) {
238*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x800);
239*7c478bd9Sstevel@tonic-gate 			sum = ((sum << 8) | (sum >> 8)) & 0xFFFF;
240*7c478bd9Sstevel@tonic-gate 			odd_total -= 2;
241*7c478bd9Sstevel@tonic-gate 		}
242*7c478bd9Sstevel@tonic-gate next_frag:
243*7c478bd9Sstevel@tonic-gate 		mp = mp->b_cont;
244*7c478bd9Sstevel@tonic-gate 		if (!mp) {
245*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x1000);
246*7c478bd9Sstevel@tonic-gate 			{
247*7c478bd9Sstevel@tonic-gate 			uint32_t	u1 = sum;
248*7c478bd9Sstevel@tonic-gate 			return ((uint16_t)u1);
249*7c478bd9Sstevel@tonic-gate 			}
250*7c478bd9Sstevel@tonic-gate 		}
251*7c478bd9Sstevel@tonic-gate 		MARK_COVERAGE(0x4000);
252*7c478bd9Sstevel@tonic-gate 		startp = mp->b_rptr;
253*7c478bd9Sstevel@tonic-gate 		endp = mp->b_wptr;
254*7c478bd9Sstevel@tonic-gate 		if (odd_total && (endp > startp)) {
255*7c478bd9Sstevel@tonic-gate 			MARK_COVERAGE(0x8000);
256*7c478bd9Sstevel@tonic-gate 			odd_total = 0;
257*7c478bd9Sstevel@tonic-gate 			sum += U8_FETCH_SECOND(startp);
258*7c478bd9Sstevel@tonic-gate 			startp++;
259*7c478bd9Sstevel@tonic-gate 		}
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate #undef	endptr
263*7c478bd9Sstevel@tonic-gate #undef	INIT_COVERAGE
264*7c478bd9Sstevel@tonic-gate #undef	INC_PTR
265*7c478bd9Sstevel@tonic-gate #undef	INC_ENDPTR
266*7c478bd9Sstevel@tonic-gate #undef	LOAD1
267*7c478bd9Sstevel@tonic-gate #undef	LOAD2
268*7c478bd9Sstevel@tonic-gate #undef	MARK_COVERAGE
269*7c478bd9Sstevel@tonic-gate #undef	ptr
270*7c478bd9Sstevel@tonic-gate #undef	SUM1
271*7c478bd9Sstevel@tonic-gate #undef	SUM2
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate #undef	FOLD_SUM
276*7c478bd9Sstevel@tonic-gate #undef	NEXT_ONE
277*7c478bd9Sstevel@tonic-gate #undef	NEXT_TWO
278*7c478bd9Sstevel@tonic-gate #undef	PREV_ONE
279*7c478bd9Sstevel@tonic-gate #undef	PREV_TWO
280*7c478bd9Sstevel@tonic-gate #undef	U8_FETCH_FIRST
281*7c478bd9Sstevel@tonic-gate #undef	U8_FETCH_SECOND
282*7c478bd9Sstevel@tonic-gate #undef	U16AM
283*7c478bd9Sstevel@tonic-gate #undef	U16_FETCH
284