1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 *
7 * @(#)iso_chksum.c 8.1 (Berkeley) 06/10/93
8 */
9
10 /***********************************************************
11 Copyright IBM Corporation 1987
12
13 All Rights Reserved
14
15 Permission to use, copy, modify, and distribute this software and its
16 documentation for any purpose and without fee is hereby granted,
17 provided that the above copyright notice appear in all copies and that
18 both that copyright notice and this permission notice appear in
19 supporting documentation, and that the name of IBM not be
20 used in advertising or publicity pertaining to distribution of the
21 software without specific, written prior permission.
22
23 IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
25 IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
26 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
27 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
28 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
29 SOFTWARE.
30
31 ******************************************************************/
32
33 /*
34 * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
35 */
36 /*
37 * $Header: iso_chksum.c,v 4.7 88/07/29 15:31:26 nhall Exp $
38 * $Source: /usr/argo/sys/netiso/RCS/iso_chksum.c,v $
39 *
40 * ISO CHECKSUM
41 *
42 * The checksum generation and check routines are here.
43 * The checksum is 2 bytes such that the sum of all the bytes b(i) == 0
44 * and the sum of i * b(i) == 0.
45 * The whole thing is complicated by the fact that the data are in mbuf
46 * chains.
47 * Furthermore, there is the possibility of wraparound in the running
48 * sums after adding up 4102 octets. In order to avoid doing a mod
49 * operation after EACH add, we have restricted this implementation to
50 * negotiating a maximum of 4096-octets per TPDU (for the transport layer).
51 * The routine iso_check_csum doesn't need to know where the checksum
52 * octets are.
53 * The routine iso_gen_csum takes a pointer to an mbuf chain (logically
54 * a chunk of data), an offset into the chunk at which the 2 octets are to
55 * be stuffed, and the length of the chunk. The 2 octets have to be
56 * logically adjacent, but may be physically located in separate mbufs.
57 */
58
59 #ifdef ISO
60 #include <netiso/argo_debug.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/mbuf.h>
64 #endif /* ISO */
65
66 #ifndef MNULL
67 #define MNULL (struct mbuf *)0
68 #endif /* MNULL */
69
70 /*
71 * FUNCTION: iso_check_csum
72 *
73 * PURPOSE: To check the checksum of the packet in the mbuf chain (m).
74 * The total length of the packet is (len).
75 * Called from tp_input() and clnp_intr()
76 *
77 * RETURNS: TRUE (something non-zero) if there is a checksum error,
78 * FALSE if there was NO checksum error.
79 *
80 * SIDE EFFECTS: none
81 *
82 * NOTES: It might be possible to gain something by optimizing
83 * this routine (unrolling loops, etc). But it is such
84 * a horrible thing to fiddle with anyway, it probably
85 * isn't worth it.
86 */
87 int
iso_check_csum(m,len)88 iso_check_csum(m, len)
89 struct mbuf *m;
90 int len;
91 {
92 register u_char *p = mtod(m, u_char *);
93 register u_long c0=0, c1=0;
94 register int i=0;
95 int cum = 0; /* cumulative length */
96 int l;
97
98 l = len;
99 len = min(m->m_len, len);
100 i = 0;
101
102 IFDEBUG(D_CHKSUM)
103 printf("iso_check_csum: m x%x, l x%x, m->m_len x%x\n", m, l, m->m_len);
104 ENDDEBUG
105
106 while( i<l ) {
107 cum += len;
108 while (i<cum) {
109 c0 = c0 + *(p++);
110 c1 += c0;
111 i++;
112 }
113 if(i < l) {
114 m = m->m_next;
115 IFDEBUG(D_CHKSUM)
116 printf("iso_check_csum: new mbuf\n");
117 if(l-i < m->m_len)
118 printf(
119 "bad mbuf chain in check csum l 0x%x i 0x%x m_data 0x%x",
120 l,i,m->m_data);
121 ENDDEBUG
122 ASSERT( m != MNULL);
123 len = min( m->m_len, l-i);
124 p = mtod(m, u_char *);
125 }
126 }
127 if ( ((int)c0 % 255) || ((int)c1 % 255) ) {
128 IFDEBUG(D_CHKSUM)
129 printf("BAD iso_check_csum l 0x%x cum 0x%x len 0x%x, i 0x%x",
130 l, cum, len, i);
131 ENDDEBUG
132 return ((int)c0 % 255)<<8 | ((int)c1 % 255);
133 }
134 return 0;
135 }
136
137 /*
138 * FUNCTION: iso_gen_csum
139 *
140 * PURPOSE: To generate the checksum of the packet in the mbuf chain (m).
141 * The first of the 2 (logically) adjacent checksum bytes
142 * (x and y) go at offset (n).
143 * (n) is an offset relative to the beginning of the data,
144 * not the beginning of the mbuf.
145 * (l) is the length of the total mbuf chain's data.
146 * Called from tp_emit(), tp_error_emit()
147 * clnp_emit_er(), clnp_forward(), clnp_output().
148 *
149 * RETURNS: Rien
150 *
151 * SIDE EFFECTS: Puts the 2 checksum bytes into the packet.
152 *
153 * NOTES: Ditto the note for iso_check_csum().
154 */
155
156 void
iso_gen_csum(m,n,l)157 iso_gen_csum(m,n,l)
158 struct mbuf *m;
159 int n; /* offset of 2 checksum bytes */
160 int l;
161 {
162 register u_char *p = mtod(m, u_char *);
163 register int c0=0, c1=0;
164 register int i=0;
165 int loc = n++, len=0; /* n is position, loc is offset */
166 u_char *xloc;
167 u_char *yloc;
168 int cum=0; /* cum == cumulative length */
169
170 IFDEBUG(D_CHKSUM)
171 printf("enter gen csum m 0x%x n 0x%x l 0x%x\n",m, n-1 ,l );
172 ENDDEBUG
173
174 while(i < l) {
175 len = min(m->m_len, CLBYTES);
176 /* RAH: don't cksum more than l bytes */
177 len = min(len, l - i);
178
179 cum +=len;
180 p = mtod(m, u_char *);
181
182 if(loc>=0) {
183 if (loc < len) {
184 xloc = loc + mtod(m, u_char *);
185 IFDEBUG(D_CHKSUM)
186 printf("1: zeroing xloc 0x%x loc 0x%x\n",xloc, loc );
187 ENDDEBUG
188 *xloc = (u_char)0;
189 if (loc+1 < len) {
190 /* both xloc and yloc are in same mbuf */
191 yloc = 1 + xloc;
192 IFDEBUG(D_CHKSUM)
193 printf("2: zeroing yloc 0x%x loc 0x%x\n",yloc, loc );
194 ENDDEBUG
195 *yloc = (u_char)0;
196 } else {
197 /* crosses boundary of mbufs */
198 yloc = mtod(m->m_next, u_char *);
199 IFDEBUG(D_CHKSUM)
200 printf("3: zeroing yloc 0x%x \n",yloc );
201 ENDDEBUG
202 *yloc = (u_char)0;
203 }
204 }
205 loc -= len;
206 }
207
208 while(i < cum) {
209 c0 = (c0 + *p);
210 c1 += c0 ;
211 i++;
212 p++;
213 }
214 m = m->m_next;
215 }
216 IFDEBUG(D_CHKSUM)
217 printf("gen csum final xloc 0x%x yloc 0x%x\n",xloc, yloc );
218 ENDDEBUG
219
220 c1 = (((c0 * (l-n))-c1)%255) ;
221 *xloc = (u_char) ((c1 < 0)? c1+255 : c1);
222
223 c1 = (-(int)(c1+c0))%255;
224 *yloc = (u_char) (c1 < 0? c1 + 255 : c1);
225
226 IFDEBUG(D_CHKSUM)
227 printf("gen csum end \n");
228 ENDDEBUG
229 }
230
231 /*
232 * FUNCTION: m_datalen
233 *
234 * PURPOSE: returns length of the mbuf chain.
235 * used all over the iso code.
236 *
237 * RETURNS: integer
238 *
239 * SIDE EFFECTS: none
240 *
241 * NOTES:
242 */
243
244 int
m_datalen(m)245 m_datalen (m)
246 register struct mbuf *m;
247 {
248 register int datalen;
249
250 for (datalen = 0; m; m = m->m_next)
251 datalen += m->m_len;
252 return datalen;
253 }
254
255 int
m_compress(in,out)256 m_compress(in, out)
257 register struct mbuf *in, **out;
258 {
259 register int datalen = 0;
260 int s = splimp();
261
262 if( in->m_next == MNULL ) {
263 *out = in;
264 IFDEBUG(D_REQUEST)
265 printf("m_compress returning 0x%x: A\n", in->m_len);
266 ENDDEBUG
267 splx(s);
268 return in->m_len;
269 }
270 MGET((*out), M_DONTWAIT, MT_DATA);
271 if((*out) == MNULL) {
272 *out = in;
273 IFDEBUG(D_REQUEST)
274 printf("m_compress returning -1: B\n");
275 ENDDEBUG
276 splx(s);
277 return -1;
278 }
279 (*out)->m_len = 0;
280 (*out)->m_act = MNULL;
281
282 while (in) {
283 IFDEBUG(D_REQUEST)
284 printf("m_compress in 0x%x *out 0x%x\n", in, *out);
285 printf("m_compress in: len 0x%x, off 0x%x\n", in->m_len, in->m_data);
286 printf("m_compress *out: len 0x%x, off 0x%x\n", (*out)->m_len,
287 (*out)->m_data);
288 ENDDEBUG
289 if (in->m_flags & M_EXT) {
290 ASSERT(in->m_len == 0);
291 }
292 if ( in->m_len == 0) {
293 in = in->m_next;
294 continue;
295 }
296 if (((*out)->m_flags & M_EXT) == 0) {
297 int len;
298
299 len = M_TRAILINGSPACE(*out);
300 len = min(len, in->m_len);
301 datalen += len;
302
303 IFDEBUG(D_REQUEST)
304 printf("m_compress copying len %d\n", len);
305 ENDDEBUG
306 bcopy(mtod(in, caddr_t), mtod((*out), caddr_t) + (*out)->m_len,
307 (unsigned)len);
308
309 (*out)->m_len += len;
310 in->m_len -= len;
311 continue;
312 } else {
313 /* (*out) is full */
314 if(( (*out)->m_next = m_get(M_DONTWAIT, MT_DATA) ) == MNULL) {
315 m_freem(*out);
316 *out = in;
317 IFDEBUG(D_REQUEST)
318 printf("m_compress returning -1: B\n");
319 ENDDEBUG
320 splx(s);
321 return -1;
322 }
323 (*out)->m_len = 0;
324 (*out)->m_act = MNULL;
325 *out = (*out)->m_next;
326 }
327 }
328 m_freem(in);
329 IFDEBUG(D_REQUEST)
330 printf("m_compress returning 0x%x: A\n", datalen);
331 ENDDEBUG
332 splx(s);
333 return datalen;
334 }
335