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