summaryrefslogtreecommitdiff
path: root/sys/netiso/clnp_input.c
blob: c77d9c6947d995d929f37bf4577c0348367f7e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*	$OpenBSD: clnp_input.c,v 1.8 2004/01/03 14:08:54 espie Exp $	*/
/*	$NetBSD: clnp_input.c,v 1.12 1996/04/13 01:34:26 cgd Exp $	*/

/*-
 * Copyright (c) 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)clnp_input.c	8.1 (Berkeley) 6/10/93
 */

/***********************************************************
		Copyright IBM Corporation 1987

                      All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of IBM not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/

/*
 * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
 */

#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/systm.h>

#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>

#include <netiso/iso.h>
#include <netiso/iso_var.h>
#include <netiso/iso_snpac.h>
#include <netiso/clnp.h>
#include <netiso/clnl.h>
#include <netiso/esis.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <netiso/eonvar.h>
#include <netiso/clnp_stat.h>
#include <netiso/argo_debug.h>

#include <sys/stdarg.h>

#ifdef ISO
u_char          clnp_protox[ISOPROTO_MAX];
struct clnl_protosw clnl_protox[256];
int             clnpqmaxlen = IFQ_MAXLEN;	/* RAH? why is this a
						 * variable */
#ifdef	ISO_X25ESIS
#if 0
void            x25esis_input();
#endif
#endif				/* ISO_X25ESIS */

/*
 * FUNCTION:		clnp_init
 *
 * PURPOSE:		clnp initialization. Fill in clnp switch tables.
 *
 * RETURNS:		none
 *
 * SIDE EFFECTS:	fills in clnp_protox table with correct offsets into
 *			the isosw table.
 *
 * NOTES:
 */
void
clnp_init()
{
	struct protosw *pr;

	/*
	 * CLNP protox initialization
	 */
	if ((pr = pffindproto(PF_ISO, ISOPROTO_RAW, SOCK_RAW)) == 0)
		printf("clnl_init: no raw CLNP\n");
	else
		clnp_protox[ISOPROTO_RAW] = pr - isosw;

	if ((pr = pffindproto(PF_ISO, ISOPROTO_TP, SOCK_SEQPACKET)) == 0)
		printf("clnl_init: no tp/clnp\n");
	else
		clnp_protox[ISOPROTO_TP] = pr - isosw;

	/*
	 *	CLNL protox initialization
	 */
	clnl_protox[ISO8473_CLNP].clnl_input = clnp_input;

	clnlintrq.ifq_maxlen = clnpqmaxlen;

	TAILQ_INIT(&iso_ifaddr);
}

/*
 * FUNCTION:		clnlintr
 *
 * PURPOSE:		Process a packet on the clnl input queue
 *
 * RETURNS:		nothing.
 *
 * SIDE EFFECTS:
 *
 * NOTES:
 */
void
clnlintr()
{
	struct mbuf *m;/* ptr to first mbuf of pkt */
	struct clnl_fixed *clnl;	/* ptr to fixed part of clnl
						 * hdr */
	int             s;	/* save and restore priority */
	struct clnl_protosw *clnlsw;	/* ptr to protocol switch */
	struct snpa_hdr sh;	/* subnetwork hdr */

	/*
	 *	Get next datagram off clnl input queue
	 */
next:
	s = splimp();
	/* IF_DEQUEUESNPAHDR(&clnlintrq, m, sh); */
	IF_DEQUEUE(&clnlintrq, m);
	splx(s);


	if (m == 0)		/* nothing to do */
		return;
	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.rcvif == 0) {
		m_freem(m);
		goto next;
	} else {
		struct ifaddr *ifa;
		for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first; ifa != 0;
		     ifa = ifa->ifa_list.tqe_next)
			if (ifa->ifa_addr->sa_family == AF_ISO)
				break;
		if (ifa == 0) {
			m_freem(m);
			goto next;
		}
	}
	bzero((caddr_t) & sh, sizeof(sh));
	sh.snh_flags = m->m_flags & (M_MCAST | M_BCAST);
	switch ((sh.snh_ifp = m->m_pkthdr.rcvif)->if_type) {
	case IFT_EON:
		bcopy(mtod(m, caddr_t), (caddr_t) sh.snh_dhost, sizeof(u_long));
		bcopy(sizeof(u_long) + mtod(m, caddr_t),
		      (caddr_t) sh.snh_shost, sizeof(u_long));
		sh.snh_dhost[4] = mtod(m, u_char *)[sizeof(struct ip) +
				     _offsetof(struct eon_hdr, eonh_class)];
		m->m_data += EONIPLEN;
		m->m_len -= EONIPLEN;
		m->m_pkthdr.len -= EONIPLEN;
		break;

	default:
		if (sh.snh_ifp->if_output == ether_output) {
			bcopy((caddr_t) (mtod(m, struct ether_header *)->ether_dhost),
			  (caddr_t) sh.snh_dhost, 2 * sizeof(sh.snh_dhost));
			m->m_data += sizeof(struct ether_header);
			m->m_len -= sizeof(struct ether_header);
			m->m_pkthdr.len -= sizeof(struct ether_header);
		}
	}
#ifdef ARGO_DEBUG
	if (argo_debug[D_INPUT]) {
		int             i;
		printf("clnlintr: src:");
		for (i = 0; i < 6; i++)
			printf("%x%c", sh.snh_shost[i] & 0xff,
			       (i < 5) ? ':' : ' ');
		printf(" dst:");
		for (i = 0; i < 6; i++)
			printf("%x%c", sh.snh_dhost[i] & 0xff,
			       (i < 5) ? ':' : ' ');
		printf("\n");
	}
#endif

	/*
	 * Get the fixed part of the clnl header into the first mbuf.
	 * Drop the packet if this fails.
	 * Do not call m_pullup if we have a cluster mbuf or the
	 * data is not there.
	 */
	if ((IS_CLUSTER(m) || (m->m_len < sizeof(struct clnl_fixed))) &&
	    ((m = m_pullup(m, sizeof(struct clnl_fixed))) == 0)) {
		INCSTAT(cns_toosmall);	/* TODO: use clnl stats */
		goto next;	/* m_pullup discards mbuf */
	}
	clnl = mtod(m, struct clnl_fixed *);

	/*
	 * Drop packet if the length of the header is not reasonable.
	 */
	if ((clnl->cnf_hdr_len < CLNP_HDR_MIN) ||
	    (clnl->cnf_hdr_len > CLNP_HDR_MAX)) {
		INCSTAT(cns_badhlen);	/* TODO: use clnl stats */
		m_freem(m);
		goto next;
	}
	/*
	 *	If the header is not contained in this mbuf, make it so.
	 *	Drop packet if this fails.
	 *	Note: m_pullup will allocate a cluster mbuf if necessary
	 */
	if (clnl->cnf_hdr_len > m->m_len) {
		if ((m = m_pullup(m, (int) clnl->cnf_hdr_len)) == 0) {
			INCSTAT(cns_badhlen);	/* TODO: use clnl stats */
			goto next;	/* m_pullup discards mbuf */
		}
		clnl = mtod(m, struct clnl_fixed *);
	}
	clnlsw = &clnl_protox[clnl->cnf_proto_id];


	if (clnlsw->clnl_input)
		(*clnlsw->clnl_input) (m, &sh);
	else
		m_freem(m);

	goto next;
}

/*
 * FUNCTION:		clnp_input
 *
 * PURPOSE:		process an incoming clnp packet
 *
 * RETURNS:		nothing
 *
 * SIDE EFFECTS:	increments fields of clnp_stat structure.
 *
 * NOTES:
 *	TODO: I would like to make seg_part a pointer into the mbuf, but
 *	will it be correctly aligned?
 */
void
clnp_input(struct mbuf *m, ...)
{
	struct snpa_hdr *shp;	/* subnetwork header */
	struct clnp_fixed *clnp;	/* ptr to fixed part of
						 * header */
	struct sockaddr_iso source;	/* source address of pkt */
	struct sockaddr_iso target;	/* destination address of pkt */
#define src	source.siso_addr
#define dst	target.siso_addr
	caddr_t         hoff;	/* current offset in packet */
	caddr_t         hend;	/* address of end of header info */
	struct clnp_segment seg_part;	/* segment part of hdr */
	int             seg_off = 0;	/* offset of segment part of hdr */
	int             seg_len;/* length of packet data&hdr in bytes */
	struct clnp_optidx oidx, *oidxp = NULL;	/* option index */
	extern int      iso_systype;	/* used by ESIS config resp */
	extern struct sockaddr_iso blank_siso;	/* used for initializing */
	int             need_afrin = 0;
	/* true if congestion experienced */
	/* which means you need afrin nose */
	/* spray. How clever! */
	va_list ap;

	va_start(ap, m);
	shp = va_arg(ap, struct snpa_hdr *);
	va_end(ap);


#ifdef ARGO_DEBUG
	if (argo_debug[D_INPUT]) {
		printf(
		       "clnp_input: processing dg; First mbuf m_len %d, m_type x%x, %s\n",
		 m->m_len, m->m_type, IS_CLUSTER(m) ? "cluster" : "normal");
	}
#endif
	need_afrin = 0;

	/*
	 *	If no iso addresses have been set, there is nothing
	 *	to do with the packet.
	 */
	if (iso_ifaddr.tqh_first == 0) {
		clnp_discard(m, ADDR_DESTUNREACH);
		return;
	}
	INCSTAT(cns_total);
	clnp = mtod(m, struct clnp_fixed *);

#ifdef ARGO_DEBUG
	if (argo_debug[D_DUMPIN]) {
		struct mbuf    *mhead;
		int             total_len = 0;
		printf("clnp_input: clnp header:\n");
		dump_buf(mtod(m, caddr_t), clnp->cnf_hdr_len);
		printf("clnp_input: mbuf chain:\n");
		for (mhead = m; mhead != NULL; mhead = mhead->m_next) {
			printf("m %p, len %d\n", mhead, mhead->m_len);
			total_len += mhead->m_len;
		}
		printf("clnp_input: total length of mbuf chain %d:\n",
		       total_len);
	}
#endif

	/*
	 *	Compute checksum (if necessary) and drop packet if
	 *	checksum does not match
	 */
	if (CKSUM_REQUIRED(clnp) &&
	    iso_check_csum(m, (int) clnp->cnf_hdr_len)) {
		INCSTAT(cns_badcsum);
		clnp_discard(m, GEN_BADCSUM);
		return;
	}
	if (clnp->cnf_vers != ISO8473_V1) {
		INCSTAT(cns_badvers);
		clnp_discard(m, DISC_UNSUPPVERS);
		return;
	}
	/* check mbuf data length: clnp_data_ck will free mbuf upon error */
	CTOH(clnp->cnf_seglen_msb, clnp->cnf_seglen_lsb, seg_len);
	if ((m = clnp_data_ck(m, seg_len)) == 0)
		return;

	clnp = mtod(m, struct clnp_fixed *);
	hend = (caddr_t) clnp + clnp->cnf_hdr_len;

	/*
	 * extract the source and destination address drop packet on failure
	 */
	source = target = blank_siso;

	hoff = (caddr_t) clnp + sizeof(struct clnp_fixed);
	CLNP_EXTRACT_ADDR(dst, hoff, hend);
	if (hoff == (caddr_t) 0) {
		INCSTAT(cns_badaddr);
		clnp_discard(m, GEN_INCOMPLETE);
		return;
	}
	CLNP_EXTRACT_ADDR(src, hoff, hend);
	if (hoff == (caddr_t) 0) {
		INCSTAT(cns_badaddr);
		clnp_discard(m, GEN_INCOMPLETE);
		return;
	}
#ifdef ARGO_DEBUG
	if (argo_debug[D_INPUT]) {
		printf("clnp_input: from %s", clnp_iso_addrp(&src));
		printf(" to %s\n", clnp_iso_addrp(&dst));
	}
#endif

	/*
	 * extract the segmentation information, if it is present.
	 * drop packet on failure
	 */
	if (((clnp->cnf_type & CNF_TYPE) != CLNP_ER) &&
	    (clnp->cnf_type & CNF_SEG_OK)) {
		if (hoff + sizeof(struct clnp_segment) > hend) {
			INCSTAT(cns_noseg);
			clnp_discard(m, GEN_INCOMPLETE);
			return;
		} else {
			(void) bcopy(hoff, (caddr_t) & seg_part,
				     sizeof(struct clnp_segment));
			/* make sure segmentation fields are in host order */
			seg_part.cng_id = ntohs(seg_part.cng_id);
			seg_part.cng_off = ntohs(seg_part.cng_off);
			seg_part.cng_tot_len = ntohs(seg_part.cng_tot_len);
			seg_off = hoff - (caddr_t) clnp;
			hoff += sizeof(struct clnp_segment);
		}
	}
	/*
	 * process options if present. If clnp_opt_sanity returns
	 * false (indicating an error was found in the options) or
	 * an unsupported option was found
	 * then drop packet and emit an ER.
	 */
	if (hoff < hend) {
		int             errcode;

		oidxp = &oidx;
		errcode = clnp_opt_sanity(m, hoff, hend - hoff, oidxp);

		/* we do not support security */
		if ((errcode == 0) && (oidxp->cni_securep))
			errcode = DISC_UNSUPPSECURE;

		/* the er option is valid with ER pdus only */
		if ((errcode == 0) && (oidxp->cni_er_reason != ER_INVALREAS) &&
		    ((clnp->cnf_type & CNF_TYPE) != CLNP_ER))
			errcode = DISC_UNSUPPOPT;

#ifdef	DECBIT
		/* check if the congestion experienced bit is set */
		if (oidxp->cni_qos_formatp) {
			caddr_t         qosp = CLNP_OFFTOOPT(m, oidxp->cni_qos_formatp);
			u_char          qos = *qosp;

			need_afrin = ((qos & (CLNPOVAL_GLOBAL | CLNPOVAL_CONGESTED)) ==
				    (CLNPOVAL_GLOBAL | CLNPOVAL_CONGESTED));
			if (need_afrin)
				INCSTAT(cns_congest_rcvd);
		}
#endif				/* DECBIT */

		if (errcode != 0) {
			clnp_discard(m, (char) errcode);
#ifdef ARGO_DEBUG
			if (argo_debug[D_INPUT]) {
				printf(
				       "clnp_input: dropped (err x%x) due to bad options\n",
				       errcode);
			}
#endif
			return;
		}
	}
	/*
	 *	check if this packet is for us. if not, then forward
	 */
	if (clnp_ours(&dst) == 0) {
#ifdef ARGO_DEBUG
		if (argo_debug[D_INPUT]) {
			printf("clnp_input: forwarding packet not for us\n");
		}
#endif
		clnp_forward(m, seg_len, &dst, oidxp, seg_off, shp);
		return;
	}
	/*
	 *	ESIS Configuration Response Function
	 *
	 *	If the packet received was sent to the multicast address
	 *	all end systems, then send an esh to the source
	 */
	if ((shp->snh_flags & M_MCAST) && (iso_systype == SNPA_ES)) {
		extern short    esis_holding_time;

		esis_shoutput(shp->snh_ifp, ESIS_ESH, esis_holding_time,
			      shp->snh_shost, 6, &dst);
	}
	/*
	 * If this is a fragment, then try to reassemble it. If clnp_reass
	 * returns non NULL, the packet has been reassembled, and should
	 * be give to TP. Otherwise the fragment has been delt with
	 * by the reassembly code (either stored or deleted). In either case
	 * we should have nothing more to do with it.
	 */
	if (((clnp->cnf_type & CNF_TYPE) != CLNP_ER) &&
	    (clnp->cnf_type & CNF_SEG_OK) &&
	    (seg_len != seg_part.cng_tot_len)) {
		struct mbuf    *m0;

		if ((m0 = clnp_reass(m, &src, &dst, &seg_part)) != NULL) {
			m = m0;
			clnp = mtod(m, struct clnp_fixed *);
			INCSTAT(cns_reassembled);
		} else {
			return;
		}
	}
	/*
	 *	give the packet to the higher layer
	 *
	 *	Note: the total length of packet
	 *	is the total length field of the segmentation part,
	 *	or, if absent, the segment length field of the
	 *	header.
	 */
	INCSTAT(cns_delivered);
	switch (clnp->cnf_type & CNF_TYPE) {
	case CLNP_ER:
		/*
		 *	This ER must have the er option.
		 *	If the option is not present, discard datagram.
		 */
		if (oidxp == NULL || oidxp->cni_er_reason == ER_INVALREAS) {
			clnp_discard(m, GEN_HDRSYNTAX);
		} else {
			clnp_er_input(m, &src, oidxp->cni_er_reason);
		}
		break;

	case CLNP_DT:
		(*isosw[clnp_protox[ISOPROTO_TP]].pr_input)(m, &source, &target,
					     clnp->cnf_hdr_len, need_afrin);
		break;

	case CLNP_RAW:
	case CLNP_ECR:
#ifdef ARGO_DEBUG
		if (argo_debug[D_INPUT]) {
			printf("clnp_input: raw input of %d bytes\n",
			       clnp->cnf_type & CNF_SEG_OK ?
			       seg_part.cng_tot_len : seg_len);
		}
#endif
		(*isosw[clnp_protox[ISOPROTO_RAW]].pr_input)(m, &source,
							     &target,
							 clnp->cnf_hdr_len);
		break;

	case CLNP_EC:
#ifdef ARGO_DEBUG
		if (argo_debug[D_INPUT]) {
			printf("clnp_input: echoing packet\n");
		}
#endif
		(void) clnp_echoreply(m, (clnp->cnf_type & CNF_SEG_OK ?
				      (int) seg_part.cng_tot_len : seg_len),
				      &source, &target, oidxp);
		break;

	default:
		printf("clnp_input: unknown clnp pkt type %d\n",
		       clnp->cnf_type & CNF_TYPE);
		clnp_stat.cns_delivered--;
		clnp_stat.cns_noproto++;
		clnp_discard(m, GEN_HDRSYNTAX);
		break;
	}
}
#endif				/* ISO */