Annotation of sys/arch/m88k/m88k/in_cksum.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: in_cksum.c,v 1.2 2005/05/05 14:28:34 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/in_systm.h>
40: #include <netinet/ip.h>
41: #include <netinet/ip_var.h>
42:
43: #define REDUCE (sum = (sum & 0xffff) + (sum >> 16))
44: #define ROL (sum <<= 8)
45: #define ADDB (ROL, sum += *w, byte_swapped ^= 1)
46: #define ADDS (sum += *(u_short *)w)
47: #define SHIFT(n) (w += (n), mlen -= (n))
48: #define ADDCARRY do { while (sum > 0xffff) REDUCE; } while (0)
49:
50: static __inline__ int
51: in_cksum_internal(struct mbuf *m, int off, int len, u_int sum)
52: {
53: u_char *w;
54: int mlen;
55: int byte_swapped = 0;
56:
57: for (; m && len; m = m->m_next) {
58: if (m->m_len == 0)
59: continue;
60: w = mtod(m, u_char *) + off;
61: mlen = m->m_len - off;
62: off = 0;
63: if (len < mlen)
64: mlen = len;
65: len -= mlen;
66:
67: if ((long)w & 1) {
68: REDUCE;
69: ADDB;
70: SHIFT(1);
71: }
72: while (mlen >= 2) {
73: ADDS;
74: SHIFT(2);
75: }
76: REDUCE;
77: if (mlen == 1)
78: ADDB;
79: }
80: if (byte_swapped) {
81: REDUCE;
82: ROL;
83: }
84: ADDCARRY;
85: return (0xffff ^ sum);
86: }
87:
88: int
89: in_cksum(struct mbuf *m, int len)
90: {
91: return (in_cksum_internal(m, 0, len, 0));
92: }
93:
94: int
95: in4_cksum(struct mbuf *m, uint8_t nxt, int off, int len)
96: {
97: u_int16_t *w;
98: u_int sum = 0;
99: struct ipovly ipov;
100:
101: if (nxt != 0) {
102: /* pseudo header */
103: bzero(&ipov, sizeof(ipov));
104: ipov.ih_len = htons(len);
105: ipov.ih_pr = nxt;
106: ipov.ih_src = mtod(m, struct ip *)->ip_src;
107: ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
108: w = (u_int16_t *)&ipov;
109: /* assumes sizeof(ipov) == 20 */
110: sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];
111: sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];
112: }
113:
114: /* skip unnecessary part */
115: while (m && off > 0) {
116: if (m->m_len > off)
117: break;
118: off -= m->m_len;
119: m = m->m_next;
120: }
121:
122: return (in_cksum_internal(m, off, len, sum));
123: }
CVSweb