Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / netiso / iso_chksum.c
blobd8d3d43e058d0c64d39b262383fbca084dbc6718
1 /* $NetBSD: iso_chksum.c,v 1.22 2006/04/14 23:53:53 christos Exp $ */
3 /*-
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
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.
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.
31 * @(#)iso_chksum.c 8.1 (Berkeley) 6/10/93
34 /***********************************************************
35 Copyright IBM Corporation 1987
37 All Rights Reserved
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
53 SOFTWARE.
55 ******************************************************************/
58 * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
61 * ISO CHECKSUM
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 $");
80 #include "opt_iso.h"
82 #ifdef ISO
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/mbuf.h>
86 #include <sys/socket.h>
88 #include <uvm/uvm_extern.h>
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 */
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.
106 * SIDE EFFECTS: none
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.
114 iso_check_csum(struct mbuf *m, int len)
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;
122 l = len;
123 len = min(m->m_len, len);
124 i = 0;
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);
131 #endif
133 while (i < l) {
134 cum += len;
135 while (i < cum) {
136 c0 = c0 + *(p++);
137 c1 += c0;
138 i++;
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);
150 #endif
151 ASSERT(m != NULL);
152 len = min(m->m_len, l - i);
153 p = mtod(m, u_char *);
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);
162 #endif
163 return ((int) c0 % 255) << 8 | ((int) c1 % 255);
165 return 0;
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().
180 * RETURNS: Rien
182 * SIDE EFFECTS: Puts the 2 checksum bytes into the packet.
184 * NOTES: Ditto the note for iso_check_csum().
187 void
188 iso_gen_csum(
189 struct mbuf *m,
190 int n, /* offset of 2 checksum bytes */
191 int l)
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 */
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);
207 #endif
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);
214 cum += len;
215 p = mtod(m, u_char *);
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);
225 #endif
226 *xloc = (u_char) 0;
227 if (loc + 1 < len) {
229 * both xloc and yloc are in same
230 * mbuf
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);
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);
249 #endif
250 *yloc = (u_char) 0;
253 loc -= len;
255 while (i < cum) {
256 c0 = (c0 + *p);
257 c1 += c0;
258 i++;
259 p++;
261 m = m->m_next;
263 #ifdef ARGO_DEBUG
264 if (argo_debug[D_CHKSUM]) {
265 printf("gen csum final xloc %p yloc %p\n", xloc, yloc);
267 #endif
269 c1 = (((c0 * (l - n)) - c1) % 255);
270 if (xloc)
271 *xloc = (u_char) ((c1 < 0) ? c1 + 255 : c1);
273 c1 = (-(int) (c1 + c0)) % 255;
274 if (yloc)
275 *yloc = (u_char) (c1 < 0 ? c1 + 255 : c1);
277 #ifdef ARGO_DEBUG
278 if (argo_debug[D_CHKSUM]) {
279 printf("gen csum end \n");
281 #endif
285 * FUNCTION: m_datalen
287 * PURPOSE: returns length of the mbuf chain.
288 * used all over the iso code.
290 * RETURNS: integer
292 * SIDE EFFECTS: none
294 * NOTES:
298 m_datalen(struct mbuf *m)
300 int datalen;
302 for (datalen = 0; m; m = m->m_next)
303 datalen += m->m_len;
304 return datalen;
308 m_compress(struct mbuf *in, struct mbuf **out)
310 int datalen = 0;
311 int s = splnet();
313 if (in->m_next == NULL) {
314 *out = in;
315 #ifdef ARGO_DEBUG
316 if (argo_debug[D_REQUEST]) {
317 printf("m_compress returning 0x%x: A\n", in->m_len);
319 #endif
320 splx(s);
321 return in->m_len;
323 MGET((*out), M_DONTWAIT, MT_DATA);
324 if ((*out) == NULL) {
325 *out = in;
326 #ifdef ARGO_DEBUG
327 if (argo_debug[D_REQUEST]) {
328 printf("m_compress returning -1: B\n");
330 #endif
331 splx(s);
332 return -1;
334 (*out)->m_len = 0;
335 (*out)->m_nextpkt = NULL;
337 while (in) {
338 #ifdef ARGO_DEBUG
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);
346 #endif
347 if (in->m_flags & M_EXT) {
348 ASSERT(in->m_len == 0);
350 if (in->m_len == 0) {
351 in = in->m_next;
352 continue;
354 if (((*out)->m_flags & M_EXT) == 0) {
355 int len;
357 len = M_TRAILINGSPACE(*out);
358 len = min(len, in->m_len);
359 datalen += len;
361 #ifdef ARGO_DEBUG
362 if (argo_debug[D_REQUEST]) {
363 printf("m_compress copying len %d\n", len);
365 #endif
366 memcpy(mtod((*out), char *) + (*out)->m_len,
367 mtod(in, void *), (unsigned) len);
369 (*out)->m_len += len;
370 in->m_len -= len;
371 continue;
372 } else {
373 /* (*out) is full */
374 if (((*out)->m_next = m_get(M_DONTWAIT, MT_DATA)) == NULL) {
375 m_freem(*out);
376 *out = in;
377 #ifdef ARGO_DEBUG
378 if (argo_debug[D_REQUEST]) {
379 printf("m_compress returning -1: B\n");
381 #endif
382 splx(s);
383 return -1;
385 (*out)->m_len = 0;
386 (*out)->m_nextpkt = NULL;
387 *out = (*out)->m_next;
390 m_freem(in);
391 #ifdef ARGO_DEBUG
392 if (argo_debug[D_REQUEST]) {
393 printf("m_compress returning 0x%x: A\n", datalen);
395 #endif
396 splx(s);
397 return datalen;