1 /* $NetBSD: iso_chksum.c,v 1.22 2006/04/14 23:53:53 christos Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)iso_chksum.c 8.1 (Berkeley) 6/10/93
34 /***********************************************************
35 Copyright IBM Corporation 1987
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.
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
55 ******************************************************************/
58 * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
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.
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(1, "$NetBSD: iso_chksum.c,v 1.22 2006/04/14 23:53:53 christos Exp $");
83 #include <sys/param.h>
84 #include <sys/systm.h>
86 #include <sys/socket.h>
88 #include <uvm/uvm_extern.h>
91 #include <netiso/argo_debug.h>
92 #include <netiso/iso.h>
93 #include <netiso/iso_var.h>
97 * FUNCTION: iso_check_csum
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()
103 * RETURNS: TRUE (something non-zero) if there is a checksum error,
104 * FALSE if there was NO checksum error.
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
114 iso_check_csum(struct mbuf
*m
, int len
)
116 u_char
*p
= mtod(m
, u_char
*);
117 u_long c0
= 0, c1
= 0;
119 int cum
= 0;/* cumulative length */
123 len
= min(m
->m_len
, len
);
127 if (argo_debug
[D_CHKSUM
]) {
128 printf("iso_check_csum: m %p, l x%x, m->m_len x%x\n",
143 if (argo_debug
[D_CHKSUM
]) {
144 printf("iso_check_csum: new mbuf\n");
145 if (l
- i
< m
->m_len
)
147 "bad mbuf chain in check csum l 0x%x i 0x%x m_data %p",
152 len
= min(m
->m_len
, l
- i
);
153 p
= mtod(m
, u_char
*);
156 if (((int) c0
% 255) || ((int) c1
% 255)) {
158 if (argo_debug
[D_CHKSUM
]) {
159 printf("BAD iso_check_csum l 0x%x cum 0x%x len 0x%x, i 0x%x",
163 return ((int) c0
% 255) << 8 | ((int) c1
% 255);
169 * FUNCTION: iso_gen_csum
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().
182 * SIDE EFFECTS: Puts the 2 checksum bytes into the packet.
184 * NOTES: Ditto the note for iso_check_csum().
190 int n
, /* offset of 2 checksum bytes */
193 u_char
*p
= mtod(m
, u_char
*);
196 int loc
= n
++, len
= 0; /* n is position, loc is
200 int cum
= 0;/* cum == cumulative length */
203 if (argo_debug
[D_CHKSUM
]) {
204 printf("enter gen csum m %p n 0x%x l 0x%x\n",
210 len
= min(m
->m_len
, PAGE_SIZE
);
211 /* RAH: don't cksum more than l bytes */
212 len
= min(len
, l
- i
);
215 p
= mtod(m
, u_char
*);
219 xloc
= loc
+ mtod(m
, u_char
*);
221 if (argo_debug
[D_CHKSUM
]) {
222 printf("1: zeroing xloc %p loc 0x%x\n",
229 * both xloc and yloc are in same
234 if (argo_debug
[D_CHKSUM
]) {
236 "2: zeroing yloc %p loc 0x%x\n",
242 /* crosses boundary of mbufs */
243 yloc
= mtod(m
->m_next
, u_char
*);
245 if (argo_debug
[D_CHKSUM
]) {
247 "3: zeroing yloc %p \n", yloc
);
264 if (argo_debug
[D_CHKSUM
]) {
265 printf("gen csum final xloc %p yloc %p\n", xloc
, yloc
);
269 c1
= (((c0
* (l
- n
)) - c1
) % 255);
271 *xloc
= (u_char
) ((c1
< 0) ? c1
+ 255 : c1
);
273 c1
= (-(int) (c1
+ c0
)) % 255;
275 *yloc
= (u_char
) (c1
< 0 ? c1
+ 255 : c1
);
278 if (argo_debug
[D_CHKSUM
]) {
279 printf("gen csum end \n");
285 * FUNCTION: m_datalen
287 * PURPOSE: returns length of the mbuf chain.
288 * used all over the iso code.
298 m_datalen(struct mbuf
*m
)
302 for (datalen
= 0; m
; m
= m
->m_next
)
308 m_compress(struct mbuf
*in
, struct mbuf
**out
)
313 if (in
->m_next
== NULL
) {
316 if (argo_debug
[D_REQUEST
]) {
317 printf("m_compress returning 0x%x: A\n", in
->m_len
);
323 MGET((*out
), M_DONTWAIT
, MT_DATA
);
324 if ((*out
) == NULL
) {
327 if (argo_debug
[D_REQUEST
]) {
328 printf("m_compress returning -1: B\n");
335 (*out
)->m_nextpkt
= NULL
;
339 if (argo_debug
[D_REQUEST
]) {
340 printf("m_compress in %p *out %p\n", in
, *out
);
341 printf("m_compress in: len 0x%x, off %p\n",
342 in
->m_len
, in
->m_data
);
343 printf("m_compress *out: len 0x%x, off %p\n",
344 (*out
)->m_len
, (*out
)->m_data
);
347 if (in
->m_flags
& M_EXT
) {
348 ASSERT(in
->m_len
== 0);
350 if (in
->m_len
== 0) {
354 if (((*out
)->m_flags
& M_EXT
) == 0) {
357 len
= M_TRAILINGSPACE(*out
);
358 len
= min(len
, in
->m_len
);
362 if (argo_debug
[D_REQUEST
]) {
363 printf("m_compress copying len %d\n", len
);
366 memcpy(mtod((*out
), char *) + (*out
)->m_len
,
367 mtod(in
, void *), (unsigned) len
);
369 (*out
)->m_len
+= len
;
374 if (((*out
)->m_next
= m_get(M_DONTWAIT
, MT_DATA
)) == NULL
) {
378 if (argo_debug
[D_REQUEST
]) {
379 printf("m_compress returning -1: B\n");
386 (*out
)->m_nextpkt
= NULL
;
387 *out
= (*out
)->m_next
;
392 if (argo_debug
[D_REQUEST
]) {
393 printf("m_compress returning 0x%x: A\n", datalen
);