xref: /openbsd/sys/arch/m88k/m88k/in_cksum.c (revision fe1ad185)
1 /*	$OpenBSD: in_cksum.c,v 1.5 2022/02/01 15:30:10 miod Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/mbuf.h>
37 #include <sys/socketvar.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip_var.h>
41 
42 #define	REDUCE		(sum = (sum & 0xffff) + (sum >> 16))
43 #define	ROL		(sum <<= 8)
44 #define	ADDB		(ROL, sum += *w, byte_swapped ^= 1)
45 #define	ADDS		(sum += *(u_short *)w)
46 #define	SHIFT(n)	(w += (n), mlen -= (n))
47 #define	ADDCARRY	do { REDUCE; REDUCE; } while (0)
48 
49 static __inline__ int
in_cksum_internal(struct mbuf * m,int off,int len,u_int sum)50 in_cksum_internal(struct mbuf *m, int off, int len, u_int sum)
51 {
52 	u_char *w;
53 	int mlen;
54 	int byte_swapped = 0;
55 
56 	for (; m && len; m = m->m_next) {
57 		if (m->m_len == 0)
58 			continue;
59 		w = mtod(m, u_char *) + off;
60 		mlen = m->m_len - off;
61 		off = 0;
62 		if (len < mlen)
63 			mlen = len;
64 		len -= mlen;
65 
66 		if ((long)w & 1) {
67 			REDUCE;
68 			ADDB;
69 			SHIFT(1);
70 		}
71 		while (mlen >= 2) {
72 			ADDS;
73 			SHIFT(2);
74 		}
75 		REDUCE;
76 		if (mlen == 1)
77 			ADDB;
78 	}
79 	if (byte_swapped) {
80 		REDUCE;
81 		ROL;
82 	}
83 	ADDCARRY;
84 	return (0xffff ^ sum);
85 }
86 
87 int
in_cksum(struct mbuf * m,int len)88 in_cksum(struct mbuf *m, int len)
89 {
90 	return (in_cksum_internal(m, 0, len, 0));
91 }
92 
93 int
in4_cksum(struct mbuf * m,uint8_t nxt,int off,int len)94 in4_cksum(struct mbuf *m, uint8_t nxt, int off, int len)
95 {
96 	u_int16_t *w;
97 	u_int sum = 0;
98 	union {
99 		struct ipovly ipov;
100 		u_int16_t w[10];
101 	} u;
102 
103 	if (nxt != 0) {
104 		/* pseudo header */
105 		u.ipov.ih_x1[8] = 0;
106 		u.ipov.ih_pr = nxt;
107 		u.ipov.ih_len = htons(len);
108 		u.ipov.ih_src = mtod(m, struct ip *)->ip_src;
109 		u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
110 		w = u.w;
111 		/* assumes sizeof(ipov) == 20 and first 8 bytes are zeroes */
112 		sum += w[4]; sum += w[5]; sum += w[6];
113 		sum += w[7]; sum += w[8]; sum += w[9];
114 	}
115 
116 	/* skip unnecessary part */
117 	while (m && off > 0) {
118 		if (m->m_len > off)
119 			break;
120 		off -= m->m_len;
121 		m = m->m_next;
122 	}
123 
124 	return (in_cksum_internal(m, off, len, sum));
125 }
126