summaryrefslogtreecommitdiff
path: root/sys/netccitt/llc_input.c
blob: 1ee073b03dcb7fa4a75dd928e8f3d9b3fddbe4a1 (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
/*	$NetBSD: llc_input.c,v 1.2 1994/06/29 06:37:21 cgd Exp $	*/

/* 
 * Copyright (C) Dirk Husemann, Computer Science Department IV, 
 * 		 University of Erlangen-Nuremberg, Germany, 1990, 1991, 1992
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 * 
 * This code is derived from software contributed to Berkeley by
 * Dirk Husemann and the Computer Science Department (IV) of
 * the University of Erlangen-Nuremberg, Germany.
 *
 * 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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. 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.
 *
 *	@(#)llc_input.c	8.1 (Berkeley) 6/10/93
 */

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

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_llc.h>
#include <net/route.h>

#include <netccitt/dll.h>
#include <netccitt/llc_var.h>

/*
 * This module implements LLC as specified by ISO 8802-2.
 */


/*
 * llcintr() handles all LLC frames (except ISO CLNS ones for the time being)
 *           and tries to pass them on to the appropriate network layer entity.
 */
void
llcintr()
{
	register struct mbuf *m;
	register int i;
	register int frame_kind;
	register u_char cmdrsp;
	struct llc_linkcb *linkp;
	struct rtentry *sirt;
	struct npaidbentry *sapinfo;
	struct sdl_hdr *sdlhdr;
	struct llc *frame;
	char *c;
	long expected_len;

	struct ifnet   *ifp;
	struct rtentry *llrt;
	struct rtentry *nlrt;

	for (;;) {
		i = splimp();
		IF_DEQUEUE(&llcintrq, m);
		splx(i);
		if (m == 0)
			break;
#ifdef		DIAGNOSTIC
		if ((m->m_flags & M_PKTHDR) == 0)
			panic("llcintr no HDR");
#endif
		/*
		 * Get ifp this packet was received on
		 */
		ifp = m->m_pkthdr.rcvif;

		sdlhdr = mtod(m, struct sdl_hdr *);

		/*
		 * [Copied from net/ip_input.c]
		 *
		 * Check that the amount of data in the buffers is
		 * at least as much as the LLC header tells us.
		 * Trim mbufs if longer than expected.
		 * Drop packets if shorter than we think they are.
		 *
		 * Layout of mbuf chain at this point:
		 *
		 *  +-------------------------------+----+	-\
                 *  |  sockaddr_dl src - sdlhdr_src | 20 |	  \
                 *  +-------------------------------+----+	   |
		 *  |  sockaddr_dl dst - sdlhdr_dst | 20 |	    > sizeof(struct sdl_hdr) == 44
		 *  +-------------------------------+----+	   |
                 *  |  LLC frame len - sdlhdr_len   | 04 |	  /
		 *  +-------------------------------+----+	-/
		 * /
		 * | m_next
		 * \
                 *  +----------------------------+----+	 -\
                 *  |  llc DSAP 		 | 01 |	   \
		 *  +----------------------------+----+	    |
                 *  |  llc SSAP 		 | 01 |	    |
		 *  +----------------------------+----+	     > sdlhdr_len
                 *  |  llc control      	 | 01 |	    |
		 *  +----------------------------+----+	    |
		 *  |  ...                       |    |	   /
		 *      			      	 -/
		 *
		 * Thus the we expect to have exactly 
		 * (sdlhdr->sdlhdr_len+sizeof(struct sdl_hdr)) in the mbuf chain
		 */
		expected_len = sdlhdr->sdlhdr_len + sizeof(struct sdl_hdr);

		if (m->m_pkthdr.len < expected_len) {
			m_freem(m);
			continue;
		}
		if (m->m_pkthdr.len > expected_len) {
			if (m->m_len == m->m_pkthdr.len) {
				m->m_len = expected_len;
				m->m_pkthdr.len = expected_len;
			} else
				m_adj(m, expected_len - m->m_pkthdr.len);
		}

		/*
		 * Get llc header
		 */
		if (m->m_len > sizeof(struct sdl_hdr))
			frame = mtod((struct mbuf *)((struct sdl_hdr*)(m+1)),
			     	     struct llc *);
		else frame = mtod(m->m_next, struct llc *);
		if (frame == (struct llc *) NULL)
			panic("llcintr no llc header");

		/*
		 * Now check for bogus I/S frame, i.e. those with a control
		 * field telling us they're an I/S frame yet their length
		 * is less than the established I/S frame length (DSAP + SSAP +
		 * control + N(R)&P/F = 4) --- we drop those suckers
		 */
		if (((frame->llc_control & 0x03) != 0x03) 
		    && ((expected_len - sizeof(struct sdl_hdr)) < LLC_ISFRAMELEN)) {
			m_freem(m);
			printf("llc: hurz error\n");
			continue;
		}

		/*
		 * Get link control block for the addressed link connection.
		 * If there is none we take care of it later on.
		 */
		cmdrsp = (frame->llc_ssap & 0x01);
		frame->llc_ssap &= ~0x01;
		if (llrt = rtalloc1((struct sockaddr *)&sdlhdr->sdlhdr_src, 0))
			llrt->rt_refcnt--;
#ifdef notyet
		else llrt = npaidb_enter(&sdlhdr->sdlhdr_src, 0, 0, 0);
#endif /* notyet */
		else {
			/* 
			 * We cannot do anything currently here as we
			 * don't `know' this link --- drop it 
			 */
			m_freem(m);
			continue;
		}
		linkp = ((struct npaidbentry *)(llrt->rt_llinfo))->np_link;
		nlrt = ((struct npaidbentry *)(llrt->rt_llinfo))->np_rt;

		/*
		 * If the link is not existing right now, we can try and look up
		 * the SAP info block.
		 */
		if ((linkp == 0) && frame->llc_ssap) 
			sapinfo = llc_getsapinfo(frame->llc_dsap, ifp);

		/*
		 * Handle XID and TEST frames
		 * XID:		if DLSAP == 0, return 	type-of-services
		 *					window-0
		 *					DLSAP-0
		 *					format-identifier-?
		 * 		if DLSAP != 0, locate sapcb and return
		 *					type-of-services
		 *					SAP-window
		 *					format-identifier-?
		 * TEST:	swap (snpah_dst, snpah_src) and return frame
		 *
		 * Also toggle the CMD/RESP bit
		 *
		 * Is this behaviour correct? Check ISO 8802-2 (90)!
		 */
		frame_kind = llc_decode(frame, (struct llc_linkcb *)0);
		switch(frame_kind) {
		case LLCFT_XID:
			if (linkp || sapinfo) {
				if (linkp)
			   		frame->llc_window = linkp->llcl_window;
			   	else frame->llc_window = sapinfo->si_window;
			 	frame->llc_fid = 9;			/* XXX */
			  	frame->llc_class = sapinfo->si_class;
			 	frame->llc_ssap = frame->llc_dsap;
			} else {
			 	frame->llc_window = 0;
			     	frame->llc_fid = 9;
				frame->llc_class = 1;
				frame->llc_dsap = frame->llc_ssap = 0;
			}

			/* fall thru to */
		case LLCFT_TEST:
			sdl_swapaddr(&(mtod(m, struct sdl_hdr *)->sdlhdr_dst),
				     &(mtod(m, struct sdl_hdr *)->sdlhdr_src));

			/* Now set the CMD/RESP bit */
			frame->llc_ssap |= (cmdrsp == 0x0 ? 0x1 : 0x0);

			/* Ship it out again */
			(*ifp->if_output)(ifp, m,
					  (struct sockaddr *) &(mtod(m, struct sdl_hdr *)->sdlhdr_dst),
					  (struct rtentry *) 0);
			continue;
		}

		/*
		 * Create link control block in case it is not existing
		 */
		if (linkp == 0 && sapinfo) {
			if ((linkp = llc_newlink(&sdlhdr->sdlhdr_src, ifp, nlrt,
						     (nlrt == 0) ? 0 : nlrt->rt_llinfo,
						     llrt)) == 0) {
				printf("llcintr: couldn't create new link\n");
				m_freem(m);
				continue;
			}
			((struct npaidbentry *)llrt->rt_llinfo)->np_link = linkp;
		} else if (linkp == 0) {
			/* The link is not known to us, drop the frame and continue */
			m_freem(m);
			continue;
		}

		/*
		 * Drop SNPA header and get rid of empty mbuf at the
		 * front of the mbuf chain (I don't like 'em)
		 */
		m_adj(m, sizeof(struct sdl_hdr));
		/* 
		 * LLC_UFRAMELEN is sufficient, m_pullup() will pull up
		 * the min(m->m_len, maxprotohdr_len [=40]) thus doing
		 * the trick ...
		 */
		if ((m = m_pullup(m, LLC_UFRAMELEN)))
			/*
			 * Pass it on thru the elements of procedure
			 */
			llc_input(linkp, m, cmdrsp);
	}
	return;
}

/*
 * llc_input() --- We deal with the various incoming frames here.
 *                 Basically we (indirectly) call the appropriate
 *                 state handler function that's pointed to by
 *                 llcl_statehandler.
 * 
 *                 The statehandler returns an action code ---
 *                 further actions like 
 *                         o notify network layer
 *                         o block further sending
 *                         o deblock link
 *                         o ...
 *                 are then enacted accordingly.
 */
llc_input(struct llc_linkcb *linkp, struct mbuf *m, u_char cmdrsp)
{
	int frame_kind;
	int pollfinal;
	int action = 0;
	struct llc *frame;
	struct ifnet *ifp = linkp->llcl_if;

	if ((frame = mtod(m, struct llc *)) == (struct llc *) 0) {
		m_freem(m);
		return 0;
	}
	pollfinal = ((frame->llc_control & 0x03) == 0x03) ? 
		LLCGBITS(frame->llc_control, u_pf) :
			LLCGBITS(frame->llc_control_ext, s_pf);

	/*
	 * first decode the frame
	 */
	frame_kind = llc_decode(frame, linkp);

	switch (action = llc_statehandler(linkp, frame, frame_kind, cmdrsp, 
					  pollfinal)) {
	case LLC_DATA_INDICATION:
		m_adj(m, LLC_ISFRAMELEN);
		if (m = m_pullup(m, NLHDRSIZEGUESS)) {
			m->m_pkthdr.rcvif = (struct ifnet *)linkp->llcl_nlnext;
			(*linkp->llcl_sapinfo->si_input)(m);
		}
		break;
	}

	/* release mbuf if not an info frame */
	if (action != LLC_DATA_INDICATION && m)
		m_freem(m);

	/* try to get frames out ... */
	llc_start(linkp);

	return 0;
}

/*
 * This routine is called by configuration setup. It sets up a station control
 * block and notifies all registered upper level protocols.
 */
caddr_t
llc_ctlinput(int prc, struct sockaddr *addr, caddr_t info)
{
	struct ifnet *ifp;
	struct ifaddr *ifa;
	struct dll_ctlinfo *ctlinfo = (struct dll_ctlinfo *)info;
	u_char sap;
	struct dllconfig *config;
	caddr_t pcb;
	struct rtentry *nlrt;
	struct rtentry *llrt;
	struct llc_linkcb *linkp;
	register int i;

	/* info must point to something valid at all times */
	if (info == 0)
		return 0;

	if (prc == PRC_IFUP || prc == PRC_IFDOWN) {
		/* we use either this set ... */
		ifa = ifa_ifwithaddr(addr);
		ifp = ifa ? ifa->ifa_ifp : 0;
		if (ifp == 0)
			return 0;

		sap = ctlinfo->dlcti_lsap;
		config = ctlinfo->dlcti_cfg;
		pcb = (caddr_t) 0;
		nlrt = (struct rtentry *) 0;
	} else {
		/* or this one */
		sap = 0; 
		config = (struct dllconfig *) 0;
		pcb = ctlinfo->dlcti_pcb;
		nlrt = ctlinfo->dlcti_rt;

		if ((llrt = rtalloc1(nlrt->rt_gateway, 0)))
			llrt->rt_refcnt--;
		else return 0;

		linkp = ((struct npaidbentry *)llrt->rt_llinfo)->np_link;
	}
	
	switch (prc) {
	case PRC_IFUP:
		(void) llc_setsapinfo(ifp, addr->sa_family, sap, config);
		return 0;

	case PRC_IFDOWN: {
		register struct llc_linkcb *linkp;
		register struct llc_linkcb *nlinkp;
		register int i;

		/*
		 * All links are accessible over the doubly linked list llccb_q
		 */
		if (!LQEMPTY) {
			/*
			 * A for-loop is not that great an idea as the linkp
			 * will get deleted by llc_timer()
			 */
			linkp = LQFIRST;
			while (LQVALID(linkp)) {
				nlinkp = LQNEXT(linkp);
				if (linkp->llcl_if = ifp) {
					i = splimp();
					(void)llc_statehandler(linkp, (struct llc *)0,
							       NL_DISCONNECT_REQUEST,
							       0, 1);
					splx(i);
				}
				linkp = nlinkp;
			}
		}
	}
	
	case PRC_CONNECT_REQUEST: 
		if (linkp == 0) {
			if ((linkp = llc_newlink((struct sockaddr_dl *) nlrt->rt_gateway, 
						 nlrt->rt_ifp, nlrt, 
						 pcb, llrt)) == 0)
				return (0);
			((struct npaidbentry *)llrt->rt_llinfo)->np_link = linkp;
			i = splimp();
			(void)llc_statehandler(linkp, (struct llc *) 0,
						NL_CONNECT_REQUEST, 0, 1);
			splx(i);
		}
		return ((caddr_t)linkp);
	
	case PRC_DISCONNECT_REQUEST:
		if (linkp == 0) 
			panic("no link control block!");

		i = splimp();
		(void)llc_statehandler(linkp, (struct llc *) 0,
				       NL_DISCONNECT_REQUEST, 0, 1);
		splx(i);

		/*
		 * The actual removal of the link control block is done by the
		 * cleaning neutrum (i.e. llc_timer()).
		 */
		break;
	
	case PRC_RESET_REQUEST:
		if (linkp == 0) 
			panic("no link control block!");

		i = splimp();
		(void)llc_statehandler(linkp, (struct llc *) 0,
				       NL_RESET_REQUEST, 0, 1);
		splx(i);

		break;

	}
	
	return 0;
}