summaryrefslogtreecommitdiffstats
path: root/net/802/llc_utility.c
blob: d0a58018fe2592a427157b27b874755631647fb7 (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
/*
 * 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.
 *
 *		Small utilities, Linux timer handling.
 *
 *		Written by 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 form.
 *					Added llc_ function name prefixes.
 *					Fixed bug in stop/start timer.
 *					Added llc_cancel_timers for closing
 *						down an llc
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <net/llc_frame.h>
#include <net/llc.h>

int llc_decode_frametype(frameptr fr)
{
	if (IS_UFRAME(fr)) 
	{      /* unnumbered cmd/rsp */
		switch(fr->u_mm.mm & 0x3B)
		{
			case 0x1B:
			    return(SABME_CMD);
			    break;
			case 0x10:
			    return(DISC_CMD);
			    break;
			case 0x18:
			    return(UA_RSP);
			    break;
			case 0x03:
			    return(DM_RSP);
			    break;
			case 0x21:
			    return(FRMR_RSP);
			    break;
			case 0x00:
			    return(UI_CMD);
			    break;
			case 0x2B:
		 	    if (IS_RSP(fr)) 
		 	    	return(XID_RSP);
			    else
			    	return(XID_CMD);
			    break;
			case 0x38:
		    	    if (IS_RSP(fr))
		    	    	return(TEST_RSP);
			    else
				return(TEST_CMD);
			    break;
			default:
			    return(BAD_FRAME);
		}
	}
	else if (IS_SFRAME(fr))
	{  /* supervisory cmd/rsp */
		switch(fr->s_hdr.ss)
		{
			case 0x00:
			    if (IS_RSP(fr)) 
			    	return(RR_RSP);
			    else
			    	return(RR_CMD);
			    break;
			case 0x02:
			    if (IS_RSP(fr))
			    	return(REJ_RSP);
			    else
			    	return(REJ_CMD);
			    break;
			case 0x01:
			    if (IS_RSP(fr))
			    	return(RNR_RSP);
			    else
			    	return(RNR_CMD);
			    break;
			default:
			    return(BAD_FRAME);
		}
	}
	else
	{			  /* information xfer */
		if (IS_RSP(fr)) 
			return(I_RSP);
		else	
			return(I_CMD);
	}
}


/*
 *	Validate_seq_nos will check N(S) and N(R) to see if they are
 *	invalid or unexpected.
 *	"unexpected" is explained on p44 Send State Variable.
 *	The return value is:
 *		4 * invalid N(R) +
 *		2 * invalid N(S) +
 *		1 * unexpected N(S)
 */

int llc_validate_seq_nos(llcptr lp, frameptr fr)
{
	int res;
     
	/*
	 *	A U-frame is always good 
	 */

	if (IS_UFRAME(fr)) 
		return(0);	

	/*
	 *	For S- and I-frames check N(R): 
	 */

	if (fr->i_hdr.nr == lp->vs) 
	{    	/* if N(R) = V(S)  */
        	res = 0;                        /* N(R) is good */
	}
	else
	{				/* lp->k = transmit window size */
    		if (lp->vs >= lp->k) 
    		{	/* if window not wrapped around 127 */
			if ((fr->i_hdr.nr < lp->vs) &&
				(fr->i_hdr.nr > (lp->vs - lp->k)))
				res = 0;
			else 
				res = 4;		/* N(R) invalid */
		}
		else
		{	/* window wraps around 127 */
			if ((fr->i_hdr.nr < lp->vs) ||
				(fr->i_hdr.nr > (128 + lp->vs - lp->k))) 
				res = 0;
			else
				res = 4;		/* N(R) invalid */
		}
	}

	/*
	 *	For an I-frame, must check N(S) also:  
	 */

	if (IS_IFRAME(fr)) 
	{
    		if (fr->i_hdr.ns == lp->vr) 
    			return res;   /* N(S) good */
		if (lp->vr >= lp->rw) 
		{
			/* if receive window not wrapped */

			if ((fr->i_hdr.ns < lp->vr) &&
				(fr->i_hdr.ns > (lp->vr - lp->k)))
				res = res +1;   	/* N(S) unexpected */
			else  
				res = res +2;         /* N(S) invalid */            
		}
		else
		{
			/* Window wraps around 127 */

			if ((fr->i_hdr.ns < lp->vr) ||
				(fr->i_hdr.ns > (128 + lp->vr - lp->k)))
				res = res +1;   	/* N(S) unexpected */
			else
				res = res +2;         /* N(S) invalid */            
		}
	}					
	return(res);
}

/* **************** timer management routines ********************* */

static void llc_p_timer_expired(unsigned long ulp)
{
	llc_timer_expired((llcptr) ulp, P_TIMER);
}

static void llc_rej_timer_expired(unsigned long ulp)
{
	llc_timer_expired((llcptr) ulp, REJ_TIMER);
}

static void llc_ack_timer_expired(unsigned long ulp)
{
	llc_timer_expired((llcptr) ulp, ACK_TIMER);
} 

static void llc_busy_timer_expired(unsigned long ulp)
{
	llc_timer_expired((llcptr) ulp, BUSY_TIMER);
}

/* exp_fcn is an array holding the 4 entry points of the
   timer expiry routines above.
   It is required to keep start_timer() generic.
   Thank you cdecl.
 */

static void (* exp_fcn[])(unsigned long) = 
{
	llc_p_timer_expired,
	llc_rej_timer_expired,
	llc_ack_timer_expired,
	llc_busy_timer_expired
};   

void llc_start_timer(llcptr lp, int t)
{
	if (lp->timer_state[t] == TIMER_IDLE)
	{
    		lp->tl[t].expires = jiffies + lp->timer_interval[t];
    		lp->tl[t].data = (unsigned long) lp;
    		lp->tl[t].function = exp_fcn[t];
    		add_timer(&lp->tl[t]);
    		lp->timer_state[t] = TIMER_RUNNING;
	}
}

void llc_stop_timer(llcptr lp, int t)
{
	if (lp->timer_state[t] == TIMER_RUNNING)
	{
        	del_timer(&lp->tl[t]);
        	lp->timer_state[t] = TIMER_IDLE;
	}
}

void llc_cancel_timers(llcptr lp)
{
	llc_stop_timer(lp, P_TIMER);
	llc_stop_timer(lp, REJ_TIMER);
	llc_stop_timer(lp, ACK_TIMER);
	llc_stop_timer(lp, BUSY_TIMER);
}