summaryrefslogtreecommitdiffstats
path: root/net/802/cl2llc.c
blob: 8d236b4c4bf1fbd8bfc10a15c21fc4185eadd075 (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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
 * NET		An implementation of the IEEE 802.2 LLC protocol for the
 *		LINUX operating system.  LLC is implemented as a set of 
 *		state machines and callbacks for higher networking layers.
 *
 *		Class 2 llc algorithm.
 *		Pseudocode interpreter, transition table lookup,
 *			data_request & indicate primitives...
 *
 *		Code for initialization, termination, registration and 
 *		MAC layer glue.
 *
 *		Copyright Tim Alpaerts, 
 *			<Tim_Alpaerts@toyota-motor-europe.com>
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 *	Changes
 *		Alan Cox	:	Chainsawed into Linux format
 *					Modified to use llc_ names
 *					Changed callbacks
 *
 *	This file must be processed by sed before it can be compiled.
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/malloc.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <net/p8022.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <asm/byteorder.h>

#include "pseudo/pseudocode.h"
#include "transit/pdutr.h"
#include "transit/timertr.h"
#include <net/llc_frame.h>
#include <net/llc.h>

/*
 *	Data_request() is called by the client to present a data unit
 *	to the llc for transmission.
 *	In the future this function should also check if the transmit window
 *	allows the sending of another pdu, and if not put the skb on the atq
 *	for deferred sending.
 */

int llc_data_request(llcptr lp, struct sk_buff *skb)
{
	if (skb_headroom(skb) < (lp->dev->hard_header_len +4)){
		printk("cl2llc: data_request() not enough headroom in skb\n");
		return -1;
	};

	skb_push(skb, 4);

	if ((lp->state != NORMAL) && (lp->state != BUSY) && (lp->state != REJECT))
	{
		printk("cl2llc: data_request() while no llc connection\n"); 
		return -1;  
	}

	if (lp->remote_busy)
	{     /* if the remote llc is BUSY, */
		ADD_TO_ATQ(skb);      /* save skb in the await transmit queue */
		return 0;
	}                           
	else
	{
		/*
		 *	Else proceed with xmit 
		 */

		switch(lp->state)
		{
			case NORMAL:
				if(lp->p_flag)
					llc_interpret_pseudo_code(lp, NORMAL2, skb, NO_FRAME);
				else
					llc_interpret_pseudo_code(lp, NORMAL1, skb, NO_FRAME);
				break;
			case BUSY:
				if (lp->p_flag)
					llc_interpret_pseudo_code(lp, BUSY2, skb, NO_FRAME);
				else
					llc_interpret_pseudo_code(lp, BUSY1, skb, NO_FRAME);
				break;
			case REJECT:
				if (lp->p_flag)
					llc_interpret_pseudo_code(lp, REJECT2, skb, NO_FRAME);
				else
					llc_interpret_pseudo_code(lp, REJECT1, skb, NO_FRAME);
				break;
			default:
		}
		if(lp->llc_callbacks)
		{
			lp->llc_event(lp);
			lp->llc_callbacks=0;
		}
		return 0;  
	}              
}



/* 
 *	Disconnect_request() requests that the llc to terminate a connection
 */

void disconnect_request(llcptr lp)
{
	if ((lp->state == NORMAL) ||
    		(lp->state == BUSY) ||
		(lp->state == REJECT) ||
		(lp->state == AWAIT) ||
		(lp->state == AWAIT_BUSY) ||
		(lp->state == AWAIT_REJECT))
	{
		lp->state = D_CONN;
		llc_interpret_pseudo_code(lp, SH1, NULL, NO_FRAME);
		if(lp->llc_callbacks)
		{
			lp->llc_event(lp);
			lp->llc_callbacks=0;
		}
		/*
 		 *	lp may be invalid after the callback
		 */
	}
}


/*
 *	Connect_request() requests that the llc to start a connection
 */

void connect_request(llcptr lp)
{
	if (lp->state == ADM)
	{
		lp->state = SETUP;
		llc_interpret_pseudo_code(lp, ADM1, NULL, NO_FRAME);
		if(lp->llc_callbacks)
		{
			lp->llc_event(lp);
			lp->llc_callbacks=0;
		}
		/*
 		 *	lp may be invalid after the callback
		 */
	}
}


/*
 *	Interpret_pseudo_code() executes the actions in the connection component
 *	state transition table. Table 4 in document on p88.
 *
 *	If this function is called to handle an incomming pdu, skb will point
 *	to the buffer with the pdu and type will contain the decoded pdu type.
 *
 *	If called by data_request skb points to an skb that was skb_alloc-ed by 
 *	the llc client to hold the information unit to be transmitted, there is
 *	no valid type in this case.  
 *
 *	If called because a timer expired no skb is passed, and there is no 
 *	type.
 */

void llc_interpret_pseudo_code(llcptr lp, int pc_label, struct sk_buff *skb, 
		char type)
{    
	short int pc;	/* program counter in pseudo code array */ 
	char p_flag_received;
	frameptr fr;
	int resend_count;   /* number of pdus resend by llc_resend_ipdu() */
	int ack_count;      /* number of pdus acknowledged */
	struct sk_buff *skb2;

	if (skb != NULL) 
	{
		fr = (frameptr) skb->data;
	}
	else
		fr = NULL;

	pc = pseudo_code_idx[pc_label];
	while(pseudo_code[pc])
	{
		switch(pseudo_code[pc])
		{
			case 9:
				if ((type != I_CMD) || (fr->i_hdr.i_pflag == 0))
					break;
			case 1:
				lp->remote_busy = 0;
				llc_stop_timer(lp, BUSY_TIMER);
				if ((lp->state == NORMAL) ||
					(lp->state == REJECT) ||
					(lp->state == BUSY))
				{
					skb2 = llc_pull_from_atq(lp);
					if (skb2 != NULL) 
						llc_start_timer(lp, ACK_TIMER);
					while (skb2 != NULL)
					{
						llc_sendipdu( lp, I_CMD, 0, skb2);
						skb2 = llc_pull_from_atq(lp);
					}
				}	   
				break;
			case 2:
				lp->state = NORMAL;  /* needed to eliminate connect_response() */
				lp->llc_mode = MODE_ABM;
				lp->llc_callbacks|=LLC_CONN_INDICATION;
				break;
			case 3:
				lp->llc_mode = MODE_ABM;
				lp->llc_callbacks|=LLC_CONN_CONFIRM;
				break;
			case 4:
				skb_pull(skb, 4);
				lp->inc_skb=skb;
				lp->llc_callbacks|=LLC_DATA_INDIC;
				break;
			case 5:
				lp->llc_mode = MODE_ADM;
				lp->llc_callbacks|=LLC_DISC_INDICATION;
				break;
			case 70:
				lp->llc_callbacks|=LLC_RESET_INDIC_LOC;
				break;
			case 71:
				lp->llc_callbacks|=LLC_RESET_INDIC_REM;
				break;
			case 7:
				lp->llc_callbacks|=LLC_RST_CONFIRM;
				break;
			case 66:
				lp->llc_callbacks|=LLC_FRMR_RECV;
				break;
			case 67:
				lp->llc_callbacks|=LLC_FRMR_SENT;
				break;
			case 68:
				lp->llc_callbacks|=LLC_REMOTE_BUSY;
				break;
			case 69:
				lp->llc_callbacks|=LLC_REMOTE_NOTBUSY;
				break;
			case 11:
				llc_sendpdu(lp, DISC_CMD, lp->f_flag, 0, NULL);
				break;
			case 12:
				llc_sendpdu(lp, DM_RSP, 0, 0, NULL);
				break;
			case 13:                        
				lp->frmr_info_fld.cntl1 = fr->pdu_cntl.byte1;
				lp->frmr_info_fld.cntl2 = fr->pdu_cntl.byte2;
				lp->frmr_info_fld.vs = lp->vs;
				lp->frmr_info_fld.vr_cr = lp->vr;
				llc_sendpdu(lp, FRMR_RSP, 0, 5, (char *) &lp->frmr_info_fld);
				break;
			case 14:
				llc_sendpdu(lp, FRMR_RSP, 0, 5, (char *) &lp->frmr_info_fld);
				break;
			case 15:
				llc_sendpdu(lp, FRMR_RSP, lp->p_flag,
					5, (char *) &lp->frmr_info_fld);
				break;
			case 16:
				llc_sendipdu(lp, I_CMD, 1, skb);   
				break;
			case 17:
				resend_count = llc_resend_ipdu(lp, fr->i_hdr.nr, I_CMD, 1);
				break;
			case 18:
				resend_count = llc_resend_ipdu(lp, fr->i_hdr.nr, I_CMD, 1);
				if (resend_count == 0) 
				{
					llc_sendpdu(lp, RR_CMD, 1, 0, NULL);
				}    
				break;
			case 19:
				llc_sendipdu(lp, I_CMD, 0, skb);   
				break;
			case 20:
				resend_count = llc_resend_ipdu(lp, fr->i_hdr.nr, I_CMD, 0);
				break;
			case 21:
				resend_count = llc_resend_ipdu(lp, fr->i_hdr.nr, I_CMD, 0);
				if (resend_count == 0) 
				{
					llc_sendpdu(lp, RR_CMD, 0, 0, NULL);
				}    
				break;
			case 22:
				resend_count = llc_resend_ipdu(lp, fr->i_hdr.nr, I_RSP, 1);
				break;
			case 23:
				llc_sendpdu(lp, REJ_CMD, 1, 0, NULL);
				break;
			case 24:
				llc_sendpdu(lp, REJ_RSP, 1, 0, NULL);
				break;
			case 25:
				if (IS_RSP(fr))
					llc_sendpdu(lp, REJ_CMD, 0, 0, NULL);
				else
					llc_sendpdu(lp, REJ_RSP, 0, 0, NULL);
				break;
			case 26:
				llc_sendpdu(lp, RNR_CMD, 1, 0, NULL);
				break;
			case 27:
				llc_sendpdu(lp, RNR_RSP, 1, 0, NULL);
				break;
			case 28:
				if (IS_RSP(fr))
					llc_sendpdu(lp, RNR_CMD, 0, 0, NULL);
				else
					llc_sendpdu(lp, RNR_RSP, 0, 0, NULL);
				break;
			case 29:
				if (lp->remote_busy == 0)
				{
					lp->remote_busy = 1;
					llc_start_timer(lp, BUSY_TIMER);
					lp->llc_callbacks|=LLC_REMOTE_BUSY;
				}
				else if (lp->timer_state[BUSY_TIMER] == TIMER_IDLE)
				{
					llc_start_timer(lp, BUSY_TIMER);
				}
				break;
			case 30:
				if (IS_RSP(fr)) 
					llc_sendpdu(lp, RNR_CMD, 0, 0, NULL);
				else
					llc_sendpdu(lp, RNR_RSP, 0, 0, NULL);
				break;
			case 31:
				llc_sendpdu(lp, RR_CMD, 1, 0, NULL);
				break;
			case 32:
				llc_sendpdu(lp, RR_CMD, 1, 0, NULL);
				break;
			case 33:
				llc_sendpdu(lp, RR_RSP, 1, 0, NULL);
				break;
			case 34:
				llc_sendpdu(lp, RR_RSP, 1, 0, NULL);
				break;
			case 35:
				llc_sendpdu(lp, RR_RSP, 0, 0, NULL);
				break;
			case 36:
				if (IS_RSP(fr)) 
					llc_sendpdu(lp, RR_CMD, 0, 0, NULL);
				else
					llc_sendpdu(lp, RR_RSP, 0, 0, NULL);
				break;
			case 37:
				llc_sendpdu(lp, SABME_CMD, 0, 0, NULL);
				lp->f_flag = 0;
				break;
			case 38:
				llc_sendpdu(lp, UA_RSP, lp->f_flag, 0, NULL);
				break;
			case 39:
				lp->s_flag = 0;
				break;
			case 40:
				lp->s_flag = 1;
				break;
			case 41:
				if(lp->timer_state[P_TIMER] == TIMER_RUNNING)
					llc_stop_timer(lp, P_TIMER);
				llc_start_timer(lp, P_TIMER);
				if (lp->p_flag == 0)
				{
					lp->retry_count = 0;
					lp->p_flag = 1;
				}
				break;
			case 44:
				if (lp->timer_state[ACK_TIMER] == TIMER_IDLE)
					llc_start_timer(lp, ACK_TIMER);
				break;
			case 42:
				llc_start_timer(lp, ACK_TIMER);
				break;
			case 43:
				llc_start_timer(lp, REJ_TIMER);
				break;
			case 45:
				llc_stop_timer(lp, ACK_TIMER);
				break;
			case 46:
				llc_stop_timer(lp, ACK_TIMER);
				lp->p_flag = 0;
				break;
			case 10:
				if (lp->data_flag == 2)
					llc_stop_timer(lp, REJ_TIMER);
				break;
			case 47:
				llc_stop_timer(lp, REJ_TIMER);
				break;
			case 48:
				llc_stop_timer(lp, ACK_TIMER);
				llc_stop_timer(lp, P_TIMER);
				llc_stop_timer(lp, REJ_TIMER);
				llc_stop_timer(lp, BUSY_TIMER);
				break;
			case 49:
				llc_stop_timer(lp, P_TIMER);
				llc_stop_timer(lp, REJ_TIMER);
				llc_stop_timer(lp, BUSY_TIMER);
				break;
			case 50:             
				ack_count = llc_free_acknowledged_skbs(lp,
					(unsigned char) fr->s_hdr.nr);
				if (ack_count > 0)
				{
					lp->retry_count = 0;
					llc_stop_timer(lp, ACK_TIMER);  
					if (skb_peek(&lp->rtq) != NULL)
					{
						/*
 						 *	Re-transmit queue not empty 
						 */
						llc_start_timer(lp, ACK_TIMER);  
					}
				}        
				break;
			case 51:
				if (IS_UFRAME(fr)) 
					p_flag_received = fr->u_hdr.u_pflag;
				else
					p_flag_received = fr->i_hdr.i_pflag;
				if ((fr->pdu_hdr.ssap & 0x01) && (p_flag_received))
				{
					lp->p_flag = 0;
					llc_stop_timer(lp, P_TIMER);  
				}
				break;
			case 52:
				lp->data_flag = 2;
				break;
			case 53:
				lp->data_flag = 0;
				break;
			case 54:
				lp->data_flag = 1;
				break;
			case 55:
				if (lp->data_flag == 0)
					lp->data_flag = 1;
				break;
			case 56:
				lp->p_flag = 0;
				break;
			case 57:
				lp->p_flag = lp->f_flag;
				break;
			case 58:
				lp->remote_busy = 0;
				break;
			case 59:
				lp->retry_count = 0;
				break;
			case 60:
				lp->retry_count++;
				break;
			case 61:
				lp->vr = 0;
				break;
			case 62:
				lp->vr++;
				break;
			case 63:
				lp->vs = 0;
				break;
			case 64:
				lp->vs = fr->i_hdr.nr;
				break;
			case 65:
				if (IS_UFRAME(fr)) 
					lp->f_flag = fr->u_hdr.u_pflag;
				else
					lp->f_flag = fr->i_hdr.i_pflag;
				break;
			default:
		}
		pc++;	
	}
}


/*
 *	Process_otype2_frame will handle incoming frames
 *	for 802.2 Type 2 Procedure.
 */

void llc_process_otype2_frame(llcptr lp, struct sk_buff *skb, char type)
{
	int idx;		/*	index in transition table */
	int pc_label;		/*	action to perform, from tr tbl */
	int validation;		/*	result of validate_seq_nos */
	int p_flag_received;	/*	p_flag in received frame */
	frameptr fr;

	fr = (frameptr) skb->data;

	if (IS_UFRAME(fr))
		p_flag_received = fr->u_hdr.u_pflag;
	else
		p_flag_received = fr->i_hdr.i_pflag;

	switch(lp->state)
	{
		/*	Compute index in transition table: */
		case ADM:
			idx = type;
			idx = (idx << 1) + p_flag_received;
			break;
		case CONN:
		case RESET_WAIT:
		case RESET_CHECK:
		case ERROR:
			idx = type;
			break;
		case SETUP:
		case RESET:
		case D_CONN:
			idx = type;
			idx = (idx << 1) + lp->p_flag;
			break;
		case NORMAL:
		case BUSY:
		case REJECT:
		case AWAIT:
		case AWAIT_BUSY:
		case AWAIT_REJECT:
			validation = llc_validate_seq_nos(lp, fr);
			if (validation > 3) 
				type = BAD_FRAME;
			idx = type;
			idx = (idx << 1);
			if (validation & 1) 
				idx = idx +1;
			idx = (idx << 1) + p_flag_received;
			idx = (idx << 1) + lp->p_flag;
		default:
			printk("llc_proc: bad state\n");
			return;
	}
	idx = (idx << 1) + pdutr_offset[lp->state];
	lp->state = pdutr_entry[idx +1]; 
	pc_label = pdutr_entry[idx];
	if (pc_label != 0)
	{ 
		llc_interpret_pseudo_code(lp, pc_label, skb, type);
		if(lp->llc_callbacks)
		{
			lp->llc_event(lp);
			lp->llc_callbacks=0;
		}
		/*
 		 *	lp may no longer be valid after this point. Be
		 *	careful what is added!
		 */
	}
}


void llc_timer_expired(llcptr lp, int t)
{
	int idx;		/* index in transition table	*/
	int pc_label;       	/* action to perform, from tr tbl */

	lp->timer_state[t] = TIMER_EXPIRED;
	idx = lp->state;            /* Compute index in transition table: */
	idx = (idx << 2) + t;
	idx = idx << 1;
	if (lp->retry_count >= lp->n2) 
		idx = idx + 1;
	idx = (idx << 1) + lp->s_flag;
	idx = (idx << 1) + lp->p_flag;
	idx = idx << 1;             /* 2 bytes per entry: action & newstate */

	pc_label = timertr_entry[idx];
	if (pc_label != 0)
	{
		llc_interpret_pseudo_code(lp, pc_label, NULL, NO_FRAME);
		lp->state = timertr_entry[idx +1];
	}
	lp->timer_state[t] = TIMER_IDLE;
	if(lp->llc_callbacks)
	{
		lp->llc_event(lp);
		lp->llc_callbacks=0;
	}
	/*
 	 *	And lp may have vanished in the event callback
 	 */
}