1 /*
2  *	LAPB release 002
3  *
4  *	This code REQUIRES 2.1.15 or higher/ NET3.038
5  *
6  *	This module:
7  *		This module is free software; you can redistribute it and/or
8  *		modify it under the terms of the GNU General Public License
9  *		as published by the Free Software Foundation; either version
10  *		2 of the License, or (at your option) any later version.
11  *
12  *	History
13  *	LAPB 001	Jonathan Naulor	Started Coding
14  *	LAPB 002	Jonathan Naylor	New timer architecture.
15  *	2000-10-29	Henner Eisen	lapb_data_indication() return status.
16  */
17 
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/socket.h>
21 #include <linux/in.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/skbuff.h>
31 #include <net/sock.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <linux/fcntl.h>
35 #include <linux/mm.h>
36 #include <linux/interrupt.h>
37 #include <net/lapb.h>
38 
39 /*
40  *	State machine for state 0, Disconnected State.
41  *	The handling of the timer(s) is in file lapb_timer.c.
42  */
lapb_state0_machine(lapb_cb * lapb,struct sk_buff * skb,struct lapb_frame * frame)43 static void lapb_state0_machine(lapb_cb *lapb, struct sk_buff *skb, struct lapb_frame *frame)
44 {
45 	switch (frame->type) {
46 		case LAPB_SABM:
47 #if LAPB_DEBUG > 1
48 			printk(KERN_DEBUG "lapb: (%p) S0 RX SABM(%d)\n", lapb->token, frame->pf);
49 #endif
50 			if (lapb->mode & LAPB_EXTENDED) {
51 #if LAPB_DEBUG > 1
52 				printk(KERN_DEBUG "lapb: (%p) S0 TX DM(%d)\n", lapb->token, frame->pf);
53 #endif
54 				lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
55 			} else {
56 #if LAPB_DEBUG > 1
57 				printk(KERN_DEBUG "lapb: (%p) S0 TX UA(%d)\n", lapb->token, frame->pf);
58 #endif
59 #if LAPB_DEBUG > 0
60 				printk(KERN_DEBUG "lapb: (%p) S0 -> S3\n", lapb->token);
61 #endif
62 				lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
63 				lapb_stop_t1timer(lapb);
64 				lapb_stop_t2timer(lapb);
65 				lapb->state     = LAPB_STATE_3;
66 				lapb->condition = 0x00;
67 				lapb->n2count   = 0;
68 				lapb->vs        = 0;
69 				lapb->vr        = 0;
70 				lapb->va        = 0;
71 				lapb_connect_indication(lapb, LAPB_OK);
72 			}
73 			break;
74 
75 		case LAPB_SABME:
76 #if LAPB_DEBUG > 1
77 			printk(KERN_DEBUG "lapb: (%p) S0 RX SABME(%d)\n", lapb->token, frame->pf);
78 #endif
79 			if (lapb->mode & LAPB_EXTENDED) {
80 #if LAPB_DEBUG > 1
81 				printk(KERN_DEBUG "lapb: (%p) S0 TX UA(%d)\n", lapb->token, frame->pf);
82 #endif
83 #if LAPB_DEBUG > 0
84 				printk(KERN_DEBUG "lapb: (%p) S0 -> S3\n", lapb->token);
85 #endif
86 				lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
87 				lapb_stop_t1timer(lapb);
88 				lapb_stop_t2timer(lapb);
89 				lapb->state     = LAPB_STATE_3;
90 				lapb->condition = 0x00;
91 				lapb->n2count   = 0;
92 				lapb->vs        = 0;
93 				lapb->vr        = 0;
94 				lapb->va        = 0;
95 				lapb_connect_indication(lapb, LAPB_OK);
96 			} else {
97 #if LAPB_DEBUG > 1
98 				printk(KERN_DEBUG "lapb: (%p) S0 TX DM(%d)\n", lapb->token, frame->pf);
99 #endif
100 				lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
101 			}
102 			break;
103 
104 		case LAPB_DISC:
105 #if LAPB_DEBUG > 1
106 			printk(KERN_DEBUG "lapb: (%p) S0 RX DISC(%d)\n", lapb->token, frame->pf);
107 			printk(KERN_DEBUG "lapb: (%p) S0 TX UA(%d)\n", lapb->token, frame->pf);
108 #endif
109 			lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
110 			break;
111 
112 		default:
113 			break;
114 	}
115 
116 	kfree_skb(skb);
117 }
118 
119 /*
120  *	State machine for state 1, Awaiting Connection State.
121  *	The handling of the timer(s) is in file lapb_timer.c.
122  */
lapb_state1_machine(lapb_cb * lapb,struct sk_buff * skb,struct lapb_frame * frame)123 static void lapb_state1_machine(lapb_cb *lapb, struct sk_buff *skb, struct lapb_frame *frame)
124 {
125 	switch (frame->type) {
126 		case LAPB_SABM:
127 #if LAPB_DEBUG > 1
128 			printk(KERN_DEBUG "lapb: (%p) S1 RX SABM(%d)\n", lapb->token, frame->pf);
129 #endif
130 			if (lapb->mode & LAPB_EXTENDED) {
131 #if LAPB_DEBUG > 1
132 				printk(KERN_DEBUG "lapb: (%p) S1 TX DM(%d)\n", lapb->token, frame->pf);
133 #endif
134 				lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
135 			} else {
136 #if LAPB_DEBUG > 1
137 				printk(KERN_DEBUG "lapb: (%p) S1 TX UA(%d)\n", lapb->token, frame->pf);
138 #endif
139 				lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
140 			}
141 			break;
142 
143 		case LAPB_SABME:
144 #if LAPB_DEBUG > 1
145 			printk(KERN_DEBUG "lapb: (%p) S1 RX SABME(%d)\n", lapb->token, frame->pf);
146 #endif
147 			if (lapb->mode & LAPB_EXTENDED) {
148 #if LAPB_DEBUG > 1
149 				printk(KERN_DEBUG "lapb: (%p) S1 TX UA(%d)\n", lapb->token, frame->pf);
150 #endif
151 				lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
152 			} else {
153 #if LAPB_DEBUG > 1
154 				printk(KERN_DEBUG "lapb: (%p) S1 TX DM(%d)\n", lapb->token, frame->pf);
155 #endif
156 				lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
157 			}
158 			break;
159 
160 		case LAPB_DISC:
161 #if LAPB_DEBUG > 1
162 			printk(KERN_DEBUG "lapb: (%p) S1 RX DISC(%d)\n", lapb->token, frame->pf);
163 			printk(KERN_DEBUG "lapb: (%p) S1 TX DM(%d)\n", lapb->token, frame->pf);
164 #endif
165 			lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
166 			break;
167 
168 		case LAPB_UA:
169 #if LAPB_DEBUG > 1
170 			printk(KERN_DEBUG "lapb: (%p) S1 RX UA(%d)\n", lapb->token, frame->pf);
171 #endif
172 			if (frame->pf) {
173 #if LAPB_DEBUG > 0
174 				printk(KERN_DEBUG "lapb: (%p) S1 -> S3\n", lapb->token);
175 #endif
176 				lapb_stop_t1timer(lapb);
177 				lapb_stop_t2timer(lapb);
178 				lapb->state     = LAPB_STATE_3;
179 				lapb->condition = 0x00;
180 				lapb->n2count   = 0;
181 				lapb->vs        = 0;
182 				lapb->vr        = 0;
183 				lapb->va        = 0;
184 				lapb_connect_confirmation(lapb, LAPB_OK);
185 			}
186 			break;
187 
188 		case LAPB_DM:
189 #if LAPB_DEBUG > 1
190 			printk(KERN_DEBUG "lapb: (%p) S1 RX DM(%d)\n", lapb->token, frame->pf);
191 #endif
192 			if (frame->pf) {
193 #if LAPB_DEBUG > 0
194 				printk(KERN_DEBUG "lapb: (%p) S1 -> S0\n", lapb->token);
195 #endif
196 				lapb_clear_queues(lapb);
197 				lapb->state = LAPB_STATE_0;
198 				lapb_start_t1timer(lapb);
199 				lapb_stop_t2timer(lapb);
200 				lapb_disconnect_indication(lapb, LAPB_REFUSED);
201 			}
202 			break;
203 
204 		default:
205 			break;
206 	}
207 
208 	kfree_skb(skb);
209 }
210 
211 /*
212  *	State machine for state 2, Awaiting Release State.
213  *	The handling of the timer(s) is in file lapb_timer.c
214  */
lapb_state2_machine(lapb_cb * lapb,struct sk_buff * skb,struct lapb_frame * frame)215 static void lapb_state2_machine(lapb_cb *lapb, struct sk_buff *skb, struct lapb_frame *frame)
216 {
217 	switch (frame->type) {
218 		case LAPB_SABM:
219 		case LAPB_SABME:
220 #if LAPB_DEBUG > 1
221 			printk(KERN_DEBUG "lapb: (%p) S2 RX {SABM,SABME}(%d)\n", lapb->token, frame->pf);
222 			printk(KERN_DEBUG "lapb: (%p) S2 TX DM(%d)\n", lapb->token, frame->pf);
223 #endif
224 			lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
225 			break;
226 
227 		case LAPB_DISC:
228 #if LAPB_DEBUG > 1
229 			printk(KERN_DEBUG "lapb: (%p) S2 RX DISC(%d)\n", lapb->token, frame->pf);
230 			printk(KERN_DEBUG "lapb: (%p) S2 TX UA(%d)\n", lapb->token, frame->pf);
231 #endif
232 			lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
233 			break;
234 
235 		case LAPB_UA:
236 #if LAPB_DEBUG > 1
237 			printk(KERN_DEBUG "lapb: (%p) S2 RX UA(%d)\n", lapb->token, frame->pf);
238 #endif
239 			if (frame->pf) {
240 #if LAPB_DEBUG > 0
241 				printk(KERN_DEBUG "lapb: (%p) S2 -> S0\n", lapb->token);
242 #endif
243 				lapb->state = LAPB_STATE_0;
244 				lapb_start_t1timer(lapb);
245 				lapb_stop_t2timer(lapb);
246 				lapb_disconnect_confirmation(lapb, LAPB_OK);
247 			}
248 			break;
249 
250 		case LAPB_DM:
251 #if LAPB_DEBUG > 1
252 			printk(KERN_DEBUG "lapb: (%p) S2 RX DM(%d)\n", lapb->token, frame->pf);
253 #endif
254 			if (frame->pf) {
255 #if LAPB_DEBUG > 0
256 				printk(KERN_DEBUG "lapb: (%p) S2 -> S0\n", lapb->token);
257 #endif
258 				lapb->state = LAPB_STATE_0;
259 				lapb_start_t1timer(lapb);
260 				lapb_stop_t2timer(lapb);
261 				lapb_disconnect_confirmation(lapb, LAPB_NOTCONNECTED);
262 			}
263 			break;
264 
265 		case LAPB_I:
266 		case LAPB_REJ:
267 		case LAPB_RNR:
268 		case LAPB_RR:
269 #if LAPB_DEBUG > 1
270 			printk(KERN_DEBUG "lapb: (%p) S2 RX {I,REJ,RNR,RR}(%d)\n", lapb->token, frame->pf);
271 			printk(KERN_DEBUG "lapb: (%p) S2 RX DM(%d)\n", lapb->token, frame->pf);
272 #endif
273 			if (frame->pf) lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
274 			break;
275 
276 		default:
277 			break;
278 	}
279 
280 	kfree_skb(skb);
281 }
282 
283 /*
284  *	State machine for state 3, Connected State.
285  *	The handling of the timer(s) is in file lapb_timer.c
286  */
lapb_state3_machine(lapb_cb * lapb,struct sk_buff * skb,struct lapb_frame * frame)287 static void lapb_state3_machine(lapb_cb *lapb, struct sk_buff *skb, struct lapb_frame *frame)
288 {
289 	int queued = 0;
290 	int modulus;
291 
292 	modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
293 
294 	switch (frame->type) {
295 		case LAPB_SABM:
296 #if LAPB_DEBUG > 1
297 			printk(KERN_DEBUG "lapb: (%p) S3 RX SABM(%d)\n", lapb->token, frame->pf);
298 #endif
299 			if (lapb->mode & LAPB_EXTENDED) {
300 #if LAPB_DEBUG > 1
301 				printk(KERN_DEBUG "lapb: (%p) S3 TX DM(%d)\n", lapb->token, frame->pf);
302 #endif
303 				lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
304 			} else {
305 #if LAPB_DEBUG > 1
306 				printk(KERN_DEBUG "lapb: (%p) S3 TX UA(%d)\n", lapb->token, frame->pf);
307 #endif
308 				lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
309 				lapb_stop_t1timer(lapb);
310 				lapb_stop_t2timer(lapb);
311 				lapb->condition = 0x00;
312 				lapb->n2count   = 0;
313 				lapb->vs        = 0;
314 				lapb->vr        = 0;
315 				lapb->va        = 0;
316 				lapb_requeue_frames(lapb);
317 			}
318 			break;
319 
320 		case LAPB_SABME:
321 #if LAPB_DEBUG > 1
322 			printk(KERN_DEBUG "lapb: (%p) S3 RX SABME(%d)\n", lapb->token, frame->pf);
323 #endif
324 			if (lapb->mode & LAPB_EXTENDED) {
325 #if LAPB_DEBUG > 1
326 				printk(KERN_DEBUG "lapb: (%p) S3 TX UA(%d)\n", lapb->token, frame->pf);
327 #endif
328 				lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
329 				lapb_stop_t1timer(lapb);
330 				lapb_stop_t2timer(lapb);
331 				lapb->condition = 0x00;
332 				lapb->n2count   = 0;
333 				lapb->vs        = 0;
334 				lapb->vr        = 0;
335 				lapb->va        = 0;
336 				lapb_requeue_frames(lapb);
337 			} else {
338 #if LAPB_DEBUG > 1
339 				printk(KERN_DEBUG "lapb: (%p) S3 TX DM(%d)\n", lapb->token, frame->pf);
340 #endif
341 				lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
342 			}
343 			break;
344 
345 		case LAPB_DISC:
346 #if LAPB_DEBUG > 1
347 			printk(KERN_DEBUG "lapb: (%p) S3 RX DISC(%d)\n", lapb->token, frame->pf);
348 #endif
349 #if LAPB_DEBUG > 0
350 			printk(KERN_DEBUG "lapb: (%p) S3 -> S0\n", lapb->token);
351 #endif
352 			lapb_clear_queues(lapb);
353 			lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
354 			lapb_start_t1timer(lapb);
355 			lapb_stop_t2timer(lapb);
356 			lapb->state = LAPB_STATE_0;
357 			lapb_disconnect_indication(lapb, LAPB_OK);
358 			break;
359 
360 		case LAPB_DM:
361 #if LAPB_DEBUG > 1
362 			printk(KERN_DEBUG "lapb: (%p) S3 RX DM(%d)\n", lapb->token, frame->pf);
363 #endif
364 #if LAPB_DEBUG > 0
365 			printk(KERN_DEBUG "lapb: (%p) S3 -> S0\n", lapb->token);
366 #endif
367 			lapb_clear_queues(lapb);
368 			lapb->state = LAPB_STATE_0;
369 			lapb_start_t1timer(lapb);
370 			lapb_stop_t2timer(lapb);
371 			lapb_disconnect_indication(lapb, LAPB_NOTCONNECTED);
372 			break;
373 
374 		case LAPB_RNR:
375 #if LAPB_DEBUG > 1
376 			printk(KERN_DEBUG "lapb: (%p) S3 RX RNR(%d) R%d\n", lapb->token, frame->pf, frame->nr);
377 #endif
378 			lapb->condition |= LAPB_PEER_RX_BUSY_CONDITION;
379 			lapb_check_need_response(lapb, frame->cr, frame->pf);
380 			if (lapb_validate_nr(lapb, frame->nr)) {
381 				lapb_check_iframes_acked(lapb, frame->nr);
382 			} else {
383 				lapb->frmr_data = *frame;
384 				lapb->frmr_type = LAPB_FRMR_Z;
385 				lapb_transmit_frmr(lapb);
386 #if LAPB_DEBUG > 0
387 				printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->token);
388 #endif
389 				lapb_start_t1timer(lapb);
390 				lapb_stop_t2timer(lapb);
391 				lapb->state   = LAPB_STATE_4;
392 				lapb->n2count = 0;
393 			}
394 			break;
395 
396 		case LAPB_RR:
397 #if LAPB_DEBUG > 1
398 			printk(KERN_DEBUG "lapb: (%p) S3 RX RR(%d) R%d\n", lapb->token, frame->pf, frame->nr);
399 #endif
400 			lapb->condition &= ~LAPB_PEER_RX_BUSY_CONDITION;
401 			lapb_check_need_response(lapb, frame->cr, frame->pf);
402 			if (lapb_validate_nr(lapb, frame->nr)) {
403 				lapb_check_iframes_acked(lapb, frame->nr);
404 			} else {
405 				lapb->frmr_data = *frame;
406 				lapb->frmr_type = LAPB_FRMR_Z;
407 				lapb_transmit_frmr(lapb);
408 #if LAPB_DEBUG > 0
409 				printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->token);
410 #endif
411 				lapb_start_t1timer(lapb);
412 				lapb_stop_t2timer(lapb);
413 				lapb->state   = LAPB_STATE_4;
414 				lapb->n2count = 0;
415 			}
416 			break;
417 
418 		case LAPB_REJ:
419 #if LAPB_DEBUG > 1
420 			printk(KERN_DEBUG "lapb: (%p) S3 RX REJ(%d) R%d\n", lapb->token, frame->pf, frame->nr);
421 #endif
422 			lapb->condition &= ~LAPB_PEER_RX_BUSY_CONDITION;
423 			lapb_check_need_response(lapb, frame->cr, frame->pf);
424 			if (lapb_validate_nr(lapb, frame->nr)) {
425 				lapb_frames_acked(lapb, frame->nr);
426 				lapb_stop_t1timer(lapb);
427 				lapb->n2count = 0;
428 				lapb_requeue_frames(lapb);
429 			} else {
430 				lapb->frmr_data = *frame;
431 				lapb->frmr_type = LAPB_FRMR_Z;
432 				lapb_transmit_frmr(lapb);
433 #if LAPB_DEBUG > 0
434 				printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->token);
435 #endif
436 				lapb_start_t1timer(lapb);
437 				lapb_stop_t2timer(lapb);
438 				lapb->state   = LAPB_STATE_4;
439 				lapb->n2count = 0;
440 			}
441 			break;
442 
443 		case LAPB_I:
444 #if LAPB_DEBUG > 1
445 			printk(KERN_DEBUG "lapb: (%p) S3 RX I(%d) S%d R%d\n", lapb->token, frame->pf, frame->ns, frame->nr);
446 #endif
447 			if (!lapb_validate_nr(lapb, frame->nr)) {
448 				lapb->frmr_data = *frame;
449 				lapb->frmr_type = LAPB_FRMR_Z;
450 				lapb_transmit_frmr(lapb);
451 #if LAPB_DEBUG > 0
452 				printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->token);
453 #endif
454 				lapb_start_t1timer(lapb);
455 				lapb_stop_t2timer(lapb);
456 				lapb->state   = LAPB_STATE_4;
457 				lapb->n2count = 0;
458 				break;
459 			}
460 			if (lapb->condition & LAPB_PEER_RX_BUSY_CONDITION) {
461 				lapb_frames_acked(lapb, frame->nr);
462 			} else {
463 				lapb_check_iframes_acked(lapb, frame->nr);
464 			}
465 			if (frame->ns == lapb->vr) {
466 				int cn;
467 				cn = lapb_data_indication(lapb, skb);
468 				queued = 1;
469 				/*
470 				 * If upper layer has dropped the frame, we
471 				 * basically ignore any further protocol
472 				 * processing. This will cause the peer
473 				 * to re-transmit the frame later like
474 				 * a frame lost on the wire.
475 				 */
476 				if(cn == NET_RX_DROP){
477 					printk(KERN_DEBUG "LAPB: rx congestion\n");
478 					break;
479 				}
480 				lapb->vr = (lapb->vr + 1) % modulus;
481 				lapb->condition &= ~LAPB_REJECT_CONDITION;
482 				if (frame->pf) {
483 					lapb_enquiry_response(lapb);
484 				} else {
485 					if (!(lapb->condition & LAPB_ACK_PENDING_CONDITION)) {
486 						lapb->condition |= LAPB_ACK_PENDING_CONDITION;
487 						lapb_start_t2timer(lapb);
488 					}
489 				}
490 			} else {
491 				if (lapb->condition & LAPB_REJECT_CONDITION) {
492 					if (frame->pf)
493 						lapb_enquiry_response(lapb);
494 				} else {
495 #if LAPB_DEBUG > 1
496 					printk(KERN_DEBUG "lapb: (%p) S3 TX REJ(%d) R%d\n", lapb->token, frame->pf, lapb->vr);
497 #endif
498 					lapb->condition |= LAPB_REJECT_CONDITION;
499 					lapb_send_control(lapb, LAPB_REJ, frame->pf, LAPB_RESPONSE);
500 					lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
501 				}
502 			}
503 			break;
504 
505 		case LAPB_FRMR:
506 #if LAPB_DEBUG > 1
507 			printk(KERN_DEBUG "lapb: (%p) S3 RX FRMR(%d) %02X %02X %02X %02X %02X\n", lapb->token, frame->pf, skb->data[0], skb->data[1], skb->data[2], skb->data[3], skb->data[4]);
508 #endif
509 			lapb_establish_data_link(lapb);
510 #if LAPB_DEBUG > 0
511 			printk(KERN_DEBUG "lapb: (%p) S3 -> S1\n", lapb->token);
512 #endif
513 			lapb_requeue_frames(lapb);
514 			lapb->state = LAPB_STATE_1;
515 			break;
516 
517 		case LAPB_ILLEGAL:
518 #if LAPB_DEBUG > 1
519 			printk(KERN_DEBUG "lapb: (%p) S3 RX ILLEGAL(%d)\n", lapb->token, frame->pf);
520 #endif
521 			lapb->frmr_data = *frame;
522 			lapb->frmr_type = LAPB_FRMR_W;
523 			lapb_transmit_frmr(lapb);
524 #if LAPB_DEBUG > 0
525 			printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->token);
526 #endif
527 			lapb_start_t1timer(lapb);
528 			lapb_stop_t2timer(lapb);
529 			lapb->state   = LAPB_STATE_4;
530 			lapb->n2count = 0;
531 			break;
532 
533 		default:
534 			break;
535 	}
536 
537 	if (!queued)
538 		kfree_skb(skb);
539 }
540 
541 /*
542  *	State machine for state 4, Frame Reject State.
543  *	The handling of the timer(s) is in file lapb_timer.c.
544  */
lapb_state4_machine(lapb_cb * lapb,struct sk_buff * skb,struct lapb_frame * frame)545 static void lapb_state4_machine(lapb_cb *lapb, struct sk_buff *skb, struct lapb_frame *frame)
546 {
547 	switch (frame->type) {
548 		case LAPB_SABM:
549 #if LAPB_DEBUG > 1
550 			printk(KERN_DEBUG "lapb: (%p) S4 RX SABM(%d)\n", lapb->token, frame->pf);
551 #endif
552 			if (lapb->mode & LAPB_EXTENDED) {
553 #if LAPB_DEBUG > 1
554 				printk(KERN_DEBUG "lapb: (%p) S4 TX DM(%d)\n", lapb->token, frame->pf);
555 #endif
556 				lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
557 			} else {
558 #if LAPB_DEBUG > 1
559 				printk(KERN_DEBUG "lapb: (%p) S4 TX UA(%d)\n", lapb->token, frame->pf);
560 #endif
561 #if LAPB_DEBUG > 0
562 				printk(KERN_DEBUG "lapb: (%p) S4 -> S3\n", lapb->token);
563 #endif
564 				lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
565 				lapb_stop_t1timer(lapb);
566 				lapb_stop_t2timer(lapb);
567 				lapb->state     = LAPB_STATE_3;
568 				lapb->condition = 0x00;
569 				lapb->n2count   = 0;
570 				lapb->vs        = 0;
571 				lapb->vr        = 0;
572 				lapb->va        = 0;
573 				lapb_connect_indication(lapb, LAPB_OK);
574 			}
575 			break;
576 
577 		case LAPB_SABME:
578 #if LAPB_DEBUG > 1
579 			printk(KERN_DEBUG "lapb: (%p) S4 RX SABME(%d)\n", lapb->token, frame->pf);
580 #endif
581 			if (lapb->mode & LAPB_EXTENDED) {
582 #if LAPB_DEBUG > 1
583 				printk(KERN_DEBUG "lapb: (%p) S4 TX UA(%d)\n", lapb->token, frame->pf);
584 #endif
585 #if LAPB_DEBUG > 0
586 				printk(KERN_DEBUG "lapb: (%p) S4 -> S3\n", lapb->token);
587 #endif
588 				lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
589 				lapb_stop_t1timer(lapb);
590 				lapb_stop_t2timer(lapb);
591 				lapb->state     = LAPB_STATE_3;
592 				lapb->condition = 0x00;
593 				lapb->n2count   = 0;
594 				lapb->vs        = 0;
595 				lapb->vr        = 0;
596 				lapb->va        = 0;
597 				lapb_connect_indication(lapb, LAPB_OK);
598 			} else {
599 #if LAPB_DEBUG > 1
600 				printk(KERN_DEBUG "lapb: (%p) S4 TX DM(%d)\n", lapb->token, frame->pf);
601 #endif
602 				lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
603 			}
604 			break;
605 
606 		default:
607 			break;
608 	}
609 
610 	kfree_skb(skb);
611 }
612 
613 /*
614  *	Process an incoming LAPB frame
615  */
lapb_data_input(lapb_cb * lapb,struct sk_buff * skb)616 void lapb_data_input(lapb_cb *lapb, struct sk_buff *skb)
617 {
618 	struct lapb_frame frame;
619 
620 	lapb_decode(lapb, skb, &frame);
621 
622 	switch (lapb->state) {
623 		case LAPB_STATE_0:
624 			lapb_state0_machine(lapb, skb, &frame);
625 			break;
626 		case LAPB_STATE_1:
627 			lapb_state1_machine(lapb, skb, &frame);
628 			break;
629 		case LAPB_STATE_2:
630 			lapb_state2_machine(lapb, skb, &frame);
631 			break;
632 		case LAPB_STATE_3:
633 			lapb_state3_machine(lapb, skb, &frame);
634 			break;
635 		case LAPB_STATE_4:
636 			lapb_state4_machine(lapb, skb, &frame);
637 			break;
638 	}
639 
640 	lapb_kick(lapb);
641 }
642