patch-2.3.14 linux/drivers/isdn/hisax/isdnl2.c

Next file: linux/drivers/isdn/hisax/isdnl3.c
Previous file: linux/drivers/isdn/hisax/isdnl1.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.3.13/linux/drivers/isdn/hisax/isdnl2.c linux/drivers/isdn/hisax/isdnl2.c
@@ -1,4 +1,4 @@
-/* $Id: isdnl2.c,v 2.16 1998/11/15 23:55:01 keil Exp $
+/* $Id: isdnl2.c,v 2.19 1999/08/05 20:40:26 keil Exp $
 
  * Author       Karsten Keil (keil@isdn4linux.de)
  *              based on the teles driver from Jan den Ouden
@@ -11,6 +11,15 @@
  *              Fritz Elfert
  *
  * $Log: isdnl2.c,v $
+ * Revision 2.19  1999/08/05 20:40:26  keil
+ * Fix interlayer communication
+ *
+ * Revision 2.18  1999/07/21 14:46:16  keil
+ * changes from EICON certification
+ *
+ * Revision 2.17  1999/07/01 08:11:50  keil
+ * Common HiSax version for 2.0, 2.1, 2.2 and 2.3 kernel
+ *
  * Revision 2.16  1998/11/15 23:55:01  keil
  * changes from 2.0
  *
@@ -71,7 +80,7 @@
 #include "hisax.h"
 #include "isdnl2.h"
 
-const char *l2_revision = "$Revision: 2.16 $";
+const char *l2_revision = "$Revision: 2.19 $";
 
 static void l2m_debug(struct FsmInst *fi, char *fmt, ...);
 
@@ -106,7 +115,7 @@
 
 enum {
 	EV_L2_UI,
-	EV_L2_SABMX,
+	EV_L2_SABME,
 	EV_L2_DISC,
 	EV_L2_DM,
 	EV_L2_UA,
@@ -116,22 +125,25 @@
 	EV_L2_DL_DATA,
 	EV_L2_ACK_PULL,
 	EV_L2_DL_UNIT_DATA,
-	EV_L2_DL_ESTABLISH,
-	EV_L2_DL_RELEASE,
+	EV_L2_DL_ESTABLISH_REQ,
+	EV_L2_DL_RELEASE_REQ,
 	EV_L2_MDL_ASSIGN,
 	EV_L2_MDL_REMOVE,
 	EV_L2_MDL_ERROR,
 	EV_L1_DEACTIVATE,
 	EV_L2_T200,
 	EV_L2_T203,
+	EV_L2_SET_OWN_BUSY,
+	EV_L2_CLEAR_OWN_BUSY,
+	EV_L2_FRAME_ERROR,
 };
 
-#define L2_EVENT_COUNT (EV_L2_T203+1)
+#define L2_EVENT_COUNT (EV_L2_FRAME_ERROR+1)
 
 static char *strL2Event[] =
 {
 	"EV_L2_UI",
-	"EV_L2_SABMX",
+	"EV_L2_SABME",
 	"EV_L2_DISC",
 	"EV_L2_DM",
 	"EV_L2_UA",
@@ -141,19 +153,35 @@
 	"EV_L2_DL_DATA",
 	"EV_L2_ACK_PULL",
 	"EV_L2_DL_UNIT_DATA",
-	"EV_L2_DL_ESTABLISH",
-	"EV_L2_DL_RELEASE",
+	"EV_L2_DL_ESTABLISH_REQ",
+	"EV_L2_DL_RELEASE_REQ",
 	"EV_L2_MDL_ASSIGN",
 	"EV_L2_MDL_REMOVE",
 	"EV_L2_MDL_ERROR",
 	"EV_L1_DEACTIVATE",
 	"EV_L2_T200",
 	"EV_L2_T203",
+	"EV_L2_SET_OWN_BUSY",
+	"EV_L2_CLEAR_OWN_BUSY",
+	"EV_L2_FRAME_ERROR",
 };
 
 static int l2addrsize(struct Layer2 *l2);
 
 static void
+set_peer_busy(struct Layer2 *l2) {
+	test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
+	if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
+		test_and_set_bit(FLG_L2BLOCK, &l2->flag);
+}
+
+static void
+clear_peer_busy(struct Layer2 *l2) {
+	if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
+		test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
+}
+
+static void
 InitWin(struct Layer2 *l2)
 {
 	int i;
@@ -162,30 +190,45 @@
 		l2->windowar[i] = NULL;
 }
 
-static void
-ReleaseWin(struct Layer2 *l2)
+static int
+freewin1(struct Layer2 *l2)
 {
 	int i, cnt = 0;
 
 	for (i = 0; i < MAX_WINDOW; i++) {
 		if (l2->windowar[i]) {
 			cnt++;
-			dev_kfree_skb(l2->windowar[i]);
+			idev_kfree_skb(l2->windowar[i], FREE_WRITE);
 			l2->windowar[i] = NULL;
 		}
 	}
-	if (cnt)
+	return cnt;
+}
+
+inline void
+freewin(struct PStack *st)
+{
+	freewin1(&st->l2);
+}
+
+static void
+ReleaseWin(struct Layer2 *l2)
+{
+	int cnt;
+
+	if((cnt = freewin1(l2)))
 		printk(KERN_WARNING "isdl2 freed %d skbuffs in release\n", cnt);
 }
 
-inline int
+inline unsigned int
 cansend(struct PStack *st)
 {
-	int p1;
+	unsigned int p1;
 
-	p1 = st->l2.vs - st->l2.va;
-	if (p1 < 0)
-		p1 += (test_bit(FLG_MOD128, &st->l2.flag) ? 128 : 8);
+	if(test_bit(FLG_MOD128, &st->l2.flag))
+		p1 = (st->l2.vs - st->l2.va) % 128;
+	else
+		p1 = (st->l2.vs - st->l2.va) % 8;
 	return ((p1 < st->l2.window) && !test_bit(FLG_PEER_BUSY, &st->l2.flag));
 }
 
@@ -195,7 +238,7 @@
 	test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
 	test_and_clear_bit(FLG_REJEXC, &l2->flag);
 	test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
-	test_and_clear_bit(FLG_PEER_BUSY, &l2->flag);
+	clear_peer_busy(l2);
 }
 
 inline int
@@ -244,98 +287,197 @@
 #define enqueue_ui(a, b) enqueue_super(a, b)
 
 inline int
-IsUI(u_char * data, int ext)
+IsUI(u_char * data)
 {
 	return ((data[0] & 0xef) == UI);
 }
 
 inline int
-IsUA(u_char * data, int ext)
+IsUA(u_char * data)
 {
 	return ((data[0] & 0xef) == UA);
 }
 
 inline int
-IsDM(u_char * data, int ext)
+IsDM(u_char * data)
 {
 	return ((data[0] & 0xef) == DM);
 }
 
 inline int
-IsDISC(u_char * data, int ext)
+IsDISC(u_char * data)
 {
 	return ((data[0] & 0xef) == DISC);
 }
 
 inline int
-IsRR(u_char * data, int ext)
+IsRR(u_char * data, struct PStack *st)
 {
-	if (ext)
+	if (test_bit(FLG_MOD128, &st->l2.flag))
 		return (data[0] == RR);
 	else
 		return ((data[0] & 0xf) == 1);
 }
 
 inline int
-IsSFrame(u_char * data, int ext)
+IsSFrame(u_char * data, struct PStack *st)
 {
 	register u_char d = *data;
 	
-	if (!ext)
+	if (!test_bit(FLG_MOD128, &st->l2.flag))
 		d &= 0xf;
 	return(((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c));
 }
 
 inline int
-IsSABMX(u_char * data, int ext)
+IsSABME(u_char * data, struct PStack *st)
 {
 	u_char d = data[0] & ~0x10;
 
-	return (ext ? d == SABME : d == SABM);
+	return (test_bit(FLG_MOD128, &st->l2.flag) ? d == SABME : d == SABM);
 }
 
 inline int
-IsREJ(u_char * data, int ext)
+IsREJ(u_char * data, struct PStack *st)
 {
-	return (ext ? data[0] == REJ : (data[0] & 0xf) == REJ);
+	return (test_bit(FLG_MOD128, &st->l2.flag) ? data[0] == REJ : (data[0] & 0xf) == REJ);
 }
 
 inline int
-IsFRMR(u_char * data, int ext)
+IsFRMR(u_char * data)
 {
 	return ((data[0] & 0xef) == FRMR);
 }
 
 inline int
-IsRNR(u_char * data, int ext)
+IsRNR(u_char * data, struct PStack *st)
 {
-	return (ext ? data[0] == RNR : (data[0] & 0xf) == RNR);
+	return (test_bit(FLG_MOD128, &st->l2.flag) ? data[0] == RNR : (data[0] & 0xf) == RNR);
 }
 
-static int
-legalnr(struct PStack *st, int nr)
+int
+iframe_error(struct PStack *st, struct sk_buff *skb)
 {
-	struct Layer2 *l2 = &st->l2;
-	int lnr, lvs;
+	int i = l2addrsize(&st->l2) + (test_bit(FLG_MOD128, &st->l2.flag) ? 1 : 0);
+	int rsp = *skb->data & 0x2;
+
+	if (test_bit(FLG_ORIG, &st->l2.flag))
+		rsp = !rsp;
+
+	if (rsp)
+		return 'L';
+
+
+	if (skb->len <= i)
+		return 'N';
+
+	if ((skb->len - i) > st->l2.maxlen)
+		return 'O';
 
-	lvs = (l2->vs >= l2->va) ? l2->vs :
-	    (l2->vs + (test_bit(FLG_MOD128, &l2->flag) ? 128 : 8));
-	lnr = (nr >= l2->va) ? nr : (test_bit(FLG_MOD128, &l2->flag) ? 128 : 8);
-	return (lnr <= lvs);
+
+	return 0;
+}
+
+int
+super_error(struct PStack *st, struct sk_buff *skb)
+{
+	if (skb->len != l2addrsize(&st->l2) +
+	    (test_bit(FLG_MOD128, &st->l2.flag) ? 2 : 1))
+		return 'N';
+
+	return 0;
+}
+
+int
+unnum_error(struct PStack *st, struct sk_buff *skb, int wantrsp)
+{
+	int rsp = (*skb->data & 0x2) >> 1;
+	if (test_bit(FLG_ORIG, &st->l2.flag))
+		rsp = !rsp;
+
+	if (rsp != wantrsp)
+		return 'L';
+
+	if (skb->len != l2addrsize(&st->l2) + 1)
+		return 'N';
+
+	return 0;
+}
+
+int
+UI_error(struct PStack *st, struct sk_buff *skb)
+{
+	int rsp = *skb->data & 0x2;
+	if (test_bit(FLG_ORIG, &st->l2.flag))
+		rsp = !rsp;
+
+	if (rsp)
+		return 'L';
+
+	if (skb->len > st->l2.maxlen + l2addrsize(&st->l2) + 1)
+		return 'O';
+
+	return 0;
+}
+
+int
+FRMR_error(struct PStack *st, struct sk_buff *skb)
+{
+	int headers = l2addrsize(&st->l2) + 1;
+	u_char *datap = skb->data + headers;
+	int rsp = *skb->data & 0x2;
+
+	if (test_bit(FLG_ORIG, &st->l2.flag))
+		rsp = !rsp;
+
+	if (!rsp)
+		return 'L';
+
+	if (test_bit(FLG_MOD128, &st->l2.flag)) {
+		if (skb->len < headers + 5)
+			return 'N';
+		else
+			l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x %2x %2x",
+				datap[0], datap[1], datap[2],
+				datap[3], datap[4]);
+	} else {
+		if (skb->len < headers + 3)
+			return 'N';
+		else
+			l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x",
+				datap[0], datap[1], datap[2]);
+	}
+
+	return 0;
+}
+
+static unsigned int
+legalnr(struct PStack *st, unsigned int nr)
+{
+        struct Layer2 *l2 = &st->l2;
+
+	if(test_bit(FLG_MOD128, &l2->flag))
+		return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
+	else
+		return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
 }
 
 static void
-setva(struct PStack *st, int nr)
+setva(struct PStack *st, unsigned int nr)
 {
 	struct Layer2 *l2 = &st->l2;
 	int len;
 
 	while (l2->va != nr) {
-		l2->va = (l2->va + 1) % (test_bit(FLG_MOD128, &l2->flag) ? 128 : 8);
+		(l2->va)++;
+		if(test_bit(FLG_MOD128, &l2->flag))
+			l2->va %= 128;
+		else
+			l2->va %= 8;
 		len = l2->windowar[l2->sow]->len;
 		if (PACKET_NOACK == l2->windowar[l2->sow]->pkt_type)
 			len = -1;
-		dev_kfree_skb(l2->windowar[l2->sow]);
+		idev_kfree_skb(l2->windowar[l2->sow], FREE_WRITE);
 		l2->windowar[l2->sow] = NULL;
 		l2->sow = (l2->sow + 1) % l2->window;
 		if (st->lli.l2writewakeup && (len >=0))
@@ -356,6 +498,7 @@
 		printk(KERN_WARNING "isdl2 can't alloc sbbuff for send_uframe\n");
 		return;
 	}
+	SET_SKB_FREE(skb);
 	memcpy(skb_put(skb, i), tmp, i);
 	enqueue_super(st, skb);
 }
@@ -369,7 +512,7 @@
 inline void
 FreeSkb(struct sk_buff *skb)
 {
-	dev_kfree_skb(skb);
+	idev_kfree_skb(skb, FREE_READ);
 }
 
 
@@ -383,6 +526,48 @@
 	return (PF);
 }
 
+inline void
+start_t200(struct PStack *st, int i)
+{
+	FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, i);
+	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
+}
+
+inline void
+restart_t200(struct PStack *st, int i)
+{
+	FsmRestartTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, i);
+	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
+}
+
+inline void
+stop_t200(struct PStack *st, int i)
+{
+	if(test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
+		FsmDelTimer(&st->l2.t200, i);
+}
+
+inline void
+st5_dl_release_l2l3(struct PStack *st)
+{
+		int pr;
+
+		if(test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
+			pr = DL_RELEASE | CONFIRM;
+		else
+			pr = DL_RELEASE | INDICATION;
+
+		st->l2.l2l3(st, pr, NULL);
+}
+
+inline void
+lapb_dl_release_l2l3(struct PStack *st, int f)
+{
+		if (test_bit(FLG_LAPB, &st->l2.flag))
+			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
+		st->l2.l2l3(st, DL_RELEASE | f, NULL);
+}
+
 static void
 establishlink(struct FsmInst *fi)
 {
@@ -394,56 +579,91 @@
 	cmd = (test_bit(FLG_MOD128, &st->l2.flag) ? SABME : SABM) | 0x10;
 	send_uframe(st, cmd, CMD);
 	FsmDelTimer(&st->l2.t203, 1);
-	FsmRestartTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 1);
-	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
+	restart_t200(st, 1);
+	test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
+	freewin(st);
 	FsmChangeState(fi, ST_L2_5);
 }
 
 static void
-l2_mdl_error(struct FsmInst *fi, int event, void *arg)
+l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
 {
 	struct sk_buff *skb = arg;
 	struct PStack *st = fi->userdata;
 
-	switch (event) {
-		case EV_L2_UA:
-			if (get_PollFlagFree(st, skb))
-				st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'C');
-			else
-				st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'D');
-			break;
-		case EV_L2_DM:
-			if (get_PollFlagFree(st, skb))
-				st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
-			else {
-				st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
-				establishlink(fi);
-				test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
-			}
-			break;
-	}
+	if (get_PollFlagFree(st, skb))
+		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'C');
+	else
+		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'D');
 }
 
 static void
-l2_dl_establish(struct FsmInst *fi, int event, void *arg)
+l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
 {
+	struct sk_buff *skb = arg;
 	struct PStack *st = fi->userdata;
-	int state = fi->state;
 
-	
-	if (test_bit(FLG_FIXED_TEI, &st->l2.flag)) {
-		FsmChangeState(fi, ST_L2_4);
+	if (get_PollFlagFree(st, skb))
+		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
+	else {
+		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
 		establishlink(fi);
-		test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
-	} else {
-		FsmChangeState(fi, ST_L2_3);
-		if (state == ST_L2_1)
-			st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
+		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
+	}
+}
+
+static void
+l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
+{
+	struct sk_buff *skb = arg;
+	struct PStack *st = fi->userdata;
+
+	if (get_PollFlagFree(st, skb))
+		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
+	else {
+		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
 	}
+	establishlink(fi);
+	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
+}
+
+static void
+l2_go_st3(struct FsmInst *fi, int event, void *arg)
+{
+	FsmChangeState(fi, ST_L2_3); 
+}
+
+static void
+l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	FsmChangeState(fi, ST_L2_3); 
+	st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
+}
+
+static void
+l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+
+	skb_queue_tail(&st->l2.ui_queue, skb);
+	FsmChangeState(fi, ST_L2_2);
+	st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
+}
+
+static void
+l2_queue_ui(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+
+	skb_queue_tail(&st->l2.ui_queue, skb);
 }
 
 static void
-l2_send_ui(struct PStack *st)
+tx_ui(struct PStack *st)
 {
 	struct sk_buff *skb;
 	u_char header[MAX_HEADER_LEN];
@@ -458,18 +678,13 @@
 }
 
 static void
-l2_put_ui(struct FsmInst *fi, int event, void *arg)
+l2_send_ui(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 	struct sk_buff *skb = arg;
 
 	skb_queue_tail(&st->l2.ui_queue, skb);
-	if (fi->state == ST_L2_1) {
-		FsmChangeState(fi, ST_L2_2);
-		st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
-	}
-	if (fi->state > ST_L2_3)
-		l2_send_ui(st);
+	tx_ui(st);
 }
 
 static void
@@ -479,11 +694,12 @@
 	struct sk_buff *skb = arg;
 
 	skb_pull(skb, l2headersize(&st->l2, 1));
-	if (skb->len > st->l2.maxlen) { 
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'O');
-		FreeSkb(skb);
-	} else
-		st->l2.l2l3(st, DL_UNIT_DATA | INDICATION, skb);
+	st->l2.l2l3(st, DL_UNIT_DATA | INDICATION, skb);
+/*	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ *		in states 1-3 for broadcast
+ */
+
+
 }
 
 static void
@@ -491,277 +707,244 @@
 {
 	struct PStack *st = fi->userdata;
 
-	if (fi->state != ST_L2_4)
-		discard_queue(&st->l2.i_queue);
-	if (fi->state != ST_L2_5)
-		establishlink(fi);
+	establishlink(fi);
 	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
 }
 
 static void
-l2_dl_release(struct FsmInst *fi, int event, void *arg)
+l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.i_queue);
+	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
+	test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
+}
+
+static void
+l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 
-	if (fi->state == ST_L2_4) {
-		st->l2.l2l3(st, DL_RELEASE  | CONFIRM, NULL);
-		return;
-	} else if (fi->state == ST_L2_5) {
-		test_and_set_bit(FLG_PEND_REL, &st->l2.flag);
-		return;
-	}
 	discard_queue(&st->l2.i_queue);
+	establishlink(fi);
+	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
+}
+
+static void
+l2_release(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
+}
+
+static void
+l2_pend_rel(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	test_and_set_bit(FLG_PEND_REL, &st->l2.flag);
+}
+
+static void
+l2_disconnect(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.i_queue);
+	freewin(st);
 	FsmChangeState(fi, ST_L2_6);
 	st->l2.rc = 0;
 	send_uframe(st, DISC | 0x10, CMD);
 	FsmDelTimer(&st->l2.t203, 1);
-	FsmRestartTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 2);
-	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
+	restart_t200(st, 2);
+}
+
+static void
+l2_start_multi(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+
+	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
+
+	clear_exception(&st->l2);
+	st->l2.vs = 0;
+	st->l2.va = 0;
+	st->l2.vr = 0;
+	st->l2.sow = 0;
+	FsmChangeState(fi, ST_L2_7);
+	FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 3);
+
+	st->l2.l2l3(st, DL_ESTABLISH | INDICATION, NULL);
 }
 
 static void
-l2_got_SABMX(struct FsmInst *fi, int event, void *arg)
+l2_send_UA(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 	struct sk_buff *skb = arg;
-	int est = 1, state, rsp;
-	u_char PollFlag;
+
+	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
+}
+
+static void
+l2_send_DM(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+
+	send_uframe(st, DM | get_PollFlagFree(st, skb), RSP);
+}
+
+static void
+l2_restart_multi(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+	int est = 0, state;
 
 	state = fi->state;
-	rsp = *skb->data & 0x2;
-	if (test_bit(FLG_ORIG, &st->l2.flag))
-		rsp = !rsp;
-	if (rsp) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'L');
-		FreeSkb(skb);
-		if ((state == ST_L2_7) || (state == ST_L2_8))
-			establishlink(fi);
-		return;
-	}	
-	if (skb->len != (l2addrsize(&st->l2) + 1)) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-		FreeSkb(skb);
-		if ((state == ST_L2_7) || (state == ST_L2_8))
-			establishlink(fi);
-		return;
-	}
-	PollFlag = get_PollFlagFree(st, skb);
-	if (ST_L2_6 == state) {
-		send_uframe(st, DM | PollFlag, RSP);
-		return;
-	} else
-		send_uframe(st, UA | PollFlag, RSP);
-	if (ST_L2_5 == state)
-		return;
-	if (ST_L2_4 != state) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'F');
-		if (st->l2.vs != st->l2.va) {
-			discard_queue(&st->l2.i_queue);
-			est = 1;
-		} else
-			est = 0;
+
+	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
+
+	st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'F');
+
+	if (st->l2.vs != st->l2.va) {
+		discard_queue(&st->l2.i_queue);
+		est = 1;
 	}
+
 	clear_exception(&st->l2);
 	st->l2.vs = 0;
 	st->l2.va = 0;
 	st->l2.vr = 0;
 	st->l2.sow = 0;
 	FsmChangeState(fi, ST_L2_7);
-	if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-		FsmDelTimer(&st->l2.t200, 2);
+	stop_t200(st, 3);
 	FsmRestartTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 3);
 
 	if (est)
 		st->l2.l2l3(st, DL_ESTABLISH | INDICATION, NULL);
 
-	if (ST_L2_8 == state)
-		if (skb_queue_len(&st->l2.i_queue) && cansend(st))
-			st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
+	if ((ST_L2_7==state) || (ST_L2_8 == state))
+		if (skb_queue_len(&st->l2.i_queue) && cansend(st))
+			st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
+}
+
+static void
+l2_stop_multi(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+
+	FsmChangeState(fi, ST_L2_4);
+	FsmDelTimer(&st->l2.t203, 3);
+	stop_t200(st, 4);
+
+	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
+
+	discard_queue(&st->l2.i_queue);
+	freewin(st);
+	lapb_dl_release_l2l3(st, INDICATION);
+}
+
+static void
+l2_connected(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+	int pr=-1;
+
+	if (!get_PollFlag(st, skb)) {
+		l2_mdl_error_ua(fi, event, arg);
+		return;
+	}
+	FreeSkb(skb);
+
+	if (test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
+		l2_disconnect(fi, event, arg);
+
+	if (test_and_clear_bit(FLG_L3_INIT, &st->l2.flag)) {
+		pr = DL_ESTABLISH | CONFIRM;
+	} else if (st->l2.vs != st->l2.va) {
+		discard_queue(&st->l2.i_queue);
+		pr = DL_ESTABLISH | INDICATION;
+	}
+
+	stop_t200(st, 5);
+
+	st->l2.vr = 0;
+	st->l2.vs = 0;
+	st->l2.va = 0;
+	st->l2.sow = 0;
+	FsmChangeState(fi, ST_L2_7);
+	FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 4);
+
+	if (pr != -1)
+		st->l2.l2l3(st, pr, NULL);
+
+	if (skb_queue_len(&st->l2.i_queue) && cansend(st))
+		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
+}
+
+static void
+l2_released(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+
+	if (!get_PollFlag(st, skb)) {
+		l2_mdl_error_ua(fi, event, arg);
+		return;
+	}
+	FreeSkb(skb);
+
+	stop_t200(st, 6);
+	lapb_dl_release_l2l3(st, CONFIRM);
+	FsmChangeState(fi, ST_L2_4);
 }
 
 static void
-l2_got_disconn(struct FsmInst *fi, int event, void *arg)
+l2_reestablish(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 	struct sk_buff *skb = arg;
-	u_char PollFlag, cmd = UA;
-	int state, rel = 1, cst = 1, rsp;
-
-	state = fi->state;
-	rsp = *skb->data & 0x2;
-	if (test_bit(FLG_ORIG, &st->l2.flag))
-		rsp = !rsp;
 
-	if (rsp) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'L');
-		FreeSkb(skb);
-		if ((state == ST_L2_7) || (state == ST_L2_8))
-			establishlink(fi);
-		return;
-	}	
-	if (skb->len != (l2addrsize(&st->l2) + 1)) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-		FreeSkb(skb);
-		if ((state == ST_L2_7) || (state == ST_L2_8))
-			establishlink(fi);
-		return;
-	}
-	PollFlag = get_PollFlagFree(st, skb);
-	if ((state == ST_L2_4) || (state == ST_L2_5)) {
-		rel = 0;
-		cst = 0;
-		cmd = DM;
-	} else if (state == ST_L2_6) {
-		rel = 0;
-		cst = 0;
-	}
-	if (cst) {
-		FsmChangeState(fi, ST_L2_4);
-		FsmDelTimer(&st->l2.t203, 3);
-		if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag)) 
-			FsmDelTimer(&st->l2.t200, 2);
-	}
-	send_uframe(st, cmd | PollFlag, RSP);
-	if (rel) {
-		if (test_bit(FLG_LAPB, &st->l2.flag))
-			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
-		st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
+	if (!get_PollFlagFree(st, skb)) {
+		establishlink(fi);
+		test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
 	}
 }
 
-
 static void
-l2_got_ua(struct FsmInst *fi, int event, void *arg)
+l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 	struct sk_buff *skb = arg;
-	int pr=-1;
-	u_char PollFlag;
-	int state,rsp;
-
-	state = fi->state;
-	rsp = *skb->data & 0x2;
-	if (test_bit(FLG_ORIG, &st->l2.flag))
-		rsp = !rsp;
-
-	if (!rsp) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'L');
-		FreeSkb(skb);
-		if ((state == ST_L2_7) || (state == ST_L2_8))
-			establishlink(fi);
-		return;
-	}	
-	if (skb->len != (l2addrsize(&st->l2) + 1)) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-		FreeSkb(skb);
-		if ((fi->state == ST_L2_7) || (fi->state == ST_L2_8))
-			establishlink(fi);
-		return;
-	}
-	PollFlag = get_PollFlag(st, skb);
-	if (!PollFlag) {
-		l2_mdl_error(fi, event, arg);
-		return;
-	}
-	FreeSkb(skb);
 
-	if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-		FsmDelTimer(&st->l2.t200, 2);
-	if (fi->state == ST_L2_5) {
-		if (test_and_clear_bit(FLG_PEND_REL, &st->l2.flag)) {
+	if (get_PollFlagFree(st, skb)) {
+		stop_t200(st, 7);
+	 	if (!test_bit(FLG_L3_INIT, &st->l2.flag))
 			discard_queue(&st->l2.i_queue);
-			st->l2.rc = 0;
-			send_uframe(st, DISC | 0x10, CMD);
-			FsmChangeState(fi, ST_L2_6);
-			FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 4);
-			test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
-		} else {
-			if (test_and_clear_bit(FLG_L3_INIT, &st->l2.flag)) {
-				pr = DL_ESTABLISH | CONFIRM;
-			} else if (st->l2.vs != st->l2.va) {
-				discard_queue(&st->l2.i_queue);
-				pr = DL_ESTABLISH | INDICATION;
-			}
-			st->l2.vs = 0;
-			st->l2.va = 0;
-			st->l2.vr = 0;
-			st->l2.sow = 0;
-			FsmChangeState(fi, ST_L2_7);
-			if (pr > -1)
-				st->l2.l2l3(st, pr, NULL);
-		}
-	} else {		/* ST_L2_6 */
 		if (test_bit(FLG_LAPB, &st->l2.flag))
 			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
-		st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
+		st5_dl_release_l2l3(st);
 		FsmChangeState(fi, ST_L2_4);
 	}
 }
 
 static void
-l2_got_dm(struct FsmInst *fi, int event, void *arg)
+l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 	struct sk_buff *skb = arg;
-	u_char PollFlag;
-	int state,rsp;
-
-	state = fi->state;
-	rsp = *skb->data & 0x2;
-	if (test_bit(FLG_ORIG, &st->l2.flag))
-		rsp = !rsp;
 
-	if (!rsp) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'L');
-		FreeSkb(skb);
-		if ((state == ST_L2_7) || (state == ST_L2_8))
-			establishlink(fi);
-		return;
-	}	
-	if (skb->len != (l2addrsize(&st->l2) + 1)) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-		FreeSkb(skb);
-		if ((fi->state == ST_L2_7) || (fi->state == ST_L2_8))
-			establishlink(fi);
-		return;
-	}
-	PollFlag = get_PollFlagFree(st, skb);
-	if (!PollFlag) {
-		if (fi->state == ST_L2_4) {
-			establishlink(fi);
-			test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
-			FsmChangeState(fi, ST_L2_5);
-		} else if ((fi->state == ST_L2_7) || (fi->state == ST_L2_8)) {
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
-			establishlink(fi);
-		}
-	} else {
-		switch (fi->state) {
-			case ST_L2_8:
-				 establishlink(fi);
-			case ST_L2_7:
-				st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
-				break;
-			case ST_L2_4:
-				break;
-			case ST_L2_5:
-				if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-					FsmDelTimer(&st->l2.t200, 2);
-				discard_queue(&st->l2.i_queue);
-				if (test_bit(FLG_LAPB, &st->l2.flag))
-					st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
-				st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
-				FsmChangeState(fi, ST_L2_4);
-				break;
-			case ST_L2_6:
-				if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-					FsmDelTimer(&st->l2.t200, 2);
-				if (test_bit(FLG_LAPB, &st->l2.flag))
-					st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
-				st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
-				FsmChangeState(fi, ST_L2_4);
-				break;
-		}
+	if (get_PollFlagFree(st, skb)) {
+		stop_t200(st, 8);
+		lapb_dl_release_l2l3(st, CONFIRM);
+		FsmChangeState(fi, ST_L2_4);
 	}
 }
 
@@ -784,6 +967,7 @@
 		printk(KERN_WARNING "isdl2 can't alloc sbbuff for enquiry_cr\n");
 		return;
 	}
+	SET_SKB_FREE(skb);
 	memcpy(skb_put(skb, i), tmp, i);
 	enqueue_super(st, skb);
 }
@@ -806,8 +990,7 @@
 	else
 		enquiry_cr(st, RR, CMD, 1);
 	test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
-	FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 12);
-	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
+	start_t200(st, 9);
 }
 
 
@@ -818,42 +1001,42 @@
 
 	st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'J');
 	establishlink(fi);
+	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
 }
 
 static void
-invoke_retransmission(struct PStack *st, int nr)
+invoke_retransmission(struct PStack *st, unsigned int nr)
 {
 	struct Layer2 *l2 = &st->l2;
-	int p1;
-	long flags;
+	unsigned int p1;
 
 	if (l2->vs != nr) {
-		save_flags(flags);
-		cli();
 		while (l2->vs != nr) {
-			l2->vs = l2->vs - 1;
-			if (l2->vs < 0)
-				l2->vs += (test_bit(FLG_MOD128, &l2->flag) ? 128 : 8);
-			p1 = l2->vs - l2->va;
-			if (p1 < 0)
-				p1 += (test_bit(FLG_MOD128, &l2->flag) ? 128 : 8);
+			(l2->vs)--;
+			if(test_bit(FLG_MOD128, &l2->flag)) {
+				l2->vs %= 128;
+				p1 = (l2->vs - l2->va) % 128;
+			} else {
+				l2->vs %= 8;
+				p1 = (l2->vs - l2->va) % 8;
+			}
 			p1 = (p1 + l2->sow) % l2->window;
 			if (test_bit(FLG_LAPB, &l2->flag))
 				st->l1.bcs->tx_cnt += l2->windowar[p1]->len + l2headersize(l2, 0);
 			skb_queue_head(&l2->i_queue, l2->windowar[p1]);
 			l2->windowar[p1] = NULL;
 		}
-		restore_flags(flags);
 		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
 	}
 }
 
 static void
-l2_got_st7_super(struct FsmInst *fi, int event, void *arg)
+l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 	struct sk_buff *skb = arg;
-	int PollFlag, nr, rsp, typ = RR;
+	int PollFlag, rsp, typ = RR;
+	unsigned int nr;
 	struct Layer2 *l2 = &st->l2;
 
 	rsp = *skb->data & 0x2;
@@ -861,72 +1044,77 @@
 		rsp = !rsp;
 
 	skb_pull(skb, l2addrsize(l2));
-	if (IsRNR(skb->data, test_bit(FLG_MOD128, &l2->flag))) {
-		test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
+	if (IsRNR(skb->data, st)) {
+		set_peer_busy(l2);
 		typ = RNR;
 	} else
-		test_and_clear_bit(FLG_PEER_BUSY, &l2->flag);
-	if (IsREJ(skb->data, test_bit(FLG_MOD128, &l2->flag)))
+		clear_peer_busy(l2);
+	if (IsREJ(skb->data, st))
 		typ = REJ;
+
 	if (test_bit(FLG_MOD128, &l2->flag)) {
-		if (skb->len == 2) {
-			PollFlag = (skb->data[1] & 0x1) == 0x1;
-			nr = skb->data[1] >> 1;
-		} else {
-			if (skb->len >2) {
-				st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-				establishlink(fi);
-			}
-			FreeSkb(skb);
-			return;
-		}
+		PollFlag = (skb->data[1] & 0x1) == 0x1;
+		nr = skb->data[1] >> 1;
 	} else {
-		if (skb->len == 1) {
-			PollFlag = (skb->data[0] & 0x10);
-			nr = (skb->data[0] >> 5) & 0x7;
-		} else {
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-			FreeSkb(skb);
-			establishlink(fi);
-			return;
-		}
+		PollFlag = (skb->data[0] & 0x10);
+		nr = (skb->data[0] >> 5) & 0x7;
 	}
 	FreeSkb(skb);
 
-	if ((!rsp) && PollFlag)
-		enquiry_response(st);
-	if (rsp && PollFlag)
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'A');
+	if (PollFlag) {
+		if (rsp)
+			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'A');
+		else
+			enquiry_response(st);
+	}
 	if (legalnr(st, nr)) {
 		if (typ == REJ) {
 			setva(st, nr);
 			invoke_retransmission(st, nr);
-			if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-				FsmDelTimer(&st->l2.t200, 9);
+			stop_t200(st, 10);
 			if (FsmAddTimer(&st->l2.t203, st->l2.T203,
 					EV_L2_T203, NULL, 6))
 				l2m_debug(&st->l2.l2m, "Restart T203 ST7 REJ");
 		} else if ((nr == l2->vs) && (typ == RR)) {
 			setva(st, nr);
-			if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-				FsmDelTimer(&st->l2.t200, 9);
+			stop_t200(st, 11);
 			FsmRestartTimer(&st->l2.t203, st->l2.T203,
 					EV_L2_T203, NULL, 7);
 		} else if ((l2->va != nr) || (typ == RNR)) {
 			setva(st, nr);
-			FsmDelTimer(&st->l2.t203, 9);
-			FsmRestartTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 6);
-			test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
+			if(typ != RR) FsmDelTimer(&st->l2.t203, 9);
+			restart_t200(st, 12);
 		}
 		if (skb_queue_len(&st->l2.i_queue) && (typ == RR))
 			st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
 	} else
 		nrerrorrecovery(fi);
+}
 
-	if ((fi->userint & LC_FLUSH_WAIT) && rsp && !(skb_queue_len(&st->l2.i_queue))) {
-		fi->userint &= ~LC_FLUSH_WAIT;
-		st->l2.l2l3(st, DL_FLUSH | INDICATION, NULL);
-	}
+static void
+l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+
+	if (test_bit(FLG_LAPB, &st->l2.flag))
+		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
+	if (!test_bit(FLG_L3_INIT, &st->l2.flag))
+		skb_queue_tail(&st->l2.i_queue, skb);
+	else
+		FreeSkb(skb);
+}
+
+static void
+l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+	struct sk_buff *skb = arg;
+
+	if (test_bit(FLG_LAPB, &st->l2.flag))
+		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
+	skb_queue_tail(&st->l2.i_queue, skb);
+	st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
 }
 
 static void
@@ -937,10 +1125,7 @@
 
 	if (test_bit(FLG_LAPB, &st->l2.flag))
 		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
-	if (!((fi->state == ST_L2_5) && test_bit(FLG_L3_INIT, &st->l2.flag)))
-		skb_queue_tail(&st->l2.i_queue, skb);
-	if (fi->state == ST_L2_7)
-		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
+	skb_queue_tail(&st->l2.i_queue, skb);
 }
 
 static void
@@ -949,54 +1134,30 @@
 	struct PStack *st = fi->userdata;
 	struct sk_buff *skb = arg;
 	struct Layer2 *l2 = &(st->l2);
-	int PollFlag, ns, nr, i, rsp;
-
-	rsp = *skb->data & 0x2;
-	if (test_bit(FLG_ORIG, &l2->flag))
-		rsp = !rsp;
+	int PollFlag, ns, i;
+	unsigned int nr;
 
-	if (rsp) {
-		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'L');
-		FreeSkb(skb);
-		establishlink(fi);
-		return;
-	}	
 	i = l2addrsize(l2);
 	if (test_bit(FLG_MOD128, &l2->flag)) {
-		if (skb->len <= (i + 1)) {
-			FreeSkb(skb);
-			return;
-		} else if ((skb->len - i - 1) > l2->maxlen) { 
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'O');
-			FreeSkb(skb);
-			establishlink(fi);
-			return;
-		}
 		PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
 		ns = skb->data[i] >> 1;
 		nr = (skb->data[i + 1] >> 1) & 0x7f;
 	} else {
-		if (skb->len <= i) {
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-			FreeSkb(skb);
-			establishlink(fi);
-			return;
-		} else if ((skb->len - i) > l2->maxlen) { 
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'O');
-			FreeSkb(skb);
-			establishlink(fi);
-			return;
-		}
 		PollFlag = (skb->data[i] & 0x10);
 		ns = (skb->data[i] >> 1) & 0x7;
 		nr = (skb->data[i] >> 5) & 0x7;
 	}
 	if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
 		FreeSkb(skb);
-		enquiry_response(st);
+		if(PollFlag) enquiry_response(st);
 	} else if (l2->vr == ns) {
-		l2->vr = (l2->vr + 1) % (test_bit(FLG_MOD128, &l2->flag) ? 128 : 8);
+		(l2->vr)++;
+		if(test_bit(FLG_MOD128, &l2->flag))
+			l2->vr %= 128;
+		else
+			l2->vr %= 8;
 		test_and_clear_bit(FLG_REJEXC, &l2->flag);
+
 		if (PollFlag)
 			enquiry_response(st);
 		else
@@ -1016,19 +1177,15 @@
 	}
 
 	if (legalnr(st, nr)) {
-		setva(st, nr);
 		if (!test_bit(FLG_PEER_BUSY, &st->l2.flag) && (fi->state == ST_L2_7)) {
 			if (nr == st->l2.vs) {
-				if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-					FsmDelTimer(&st->l2.t200, 10);
+				stop_t200(st, 13);
 				FsmRestartTimer(&st->l2.t203, st->l2.T203,
 						EV_L2_T203, NULL, 7);
-			} else if (nr != st->l2.va) {
-				FsmRestartTimer(&st->l2.t200, st->l2.T200, EV_L2_T200,
-						NULL, 8);
-				test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
-			}
+			} else if (nr != st->l2.va)
+				restart_t200(st, 14);
 		}
+		setva(st, nr);
 	} else {
 		nrerrorrecovery(fi);
 		return;
@@ -1053,7 +1210,7 @@
 	} else
 		FsmChangeState(fi, ST_L2_4);
 	if (skb_queue_len(&st->l2.ui_queue))
-		l2_send_ui(st);
+		tx_ui(st);
 }
 
 static void
@@ -1071,7 +1228,7 @@
 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'G');
 		if (test_bit(FLG_LAPB, &st->l2.flag))
 			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
-		st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
+		st5_dl_release_l2l3(st);
 	} else {
 		st->l2.rc++;
 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
@@ -1090,10 +1247,9 @@
 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
 	} else if (st->l2.rc == st->l2.N200) {
 		FsmChangeState(fi, ST_L2_4);
+		test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'H');
-		if (test_bit(FLG_LAPB, &st->l2.flag))
-			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
-		st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
+		lapb_dl_release_l2l3(st, CONFIRM);
 	} else {
 		st->l2.rc++;
 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200,
@@ -1103,7 +1259,7 @@
 }
 
 static void
-l2_st78_tout_200(struct FsmInst *fi, int event, void *arg)
+l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 
@@ -1113,12 +1269,28 @@
 		return;
 	}
 	test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
-	if (fi->state == ST_L2_7) {
-		st->l2.rc = 0;
-		FsmChangeState(fi, ST_L2_8);
+	st->l2.rc = 0;
+	FsmChangeState(fi, ST_L2_8);
+
+	transmit_enquiry(st);
+	st->l2.rc++;
+}
+
+static void
+l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	if (test_bit(FLG_LAPD, &st->l2.flag) &&
+		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
+		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
+		return;
 	}
+	test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
 	if (st->l2.rc == st->l2.N200) {
+		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'I');
 		establishlink(fi);
+		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
 	} else {
 		transmit_enquiry(st);
 		st->l2.rc++;
@@ -1147,7 +1319,9 @@
 	struct sk_buff *skb, *oskb;
 	struct Layer2 *l2 = &st->l2;
 	u_char header[MAX_HEADER_LEN];
-	int p1, i;
+	int i;
+	int unsigned p1;
+	long flags;
 
 	if (!cansend(st))
 		return;
@@ -1156,14 +1330,17 @@
 	if (!skb)
 		return;
 
-	p1 = l2->vs - l2->va;
-	if (p1 < 0)
-		p1 += test_bit(FLG_MOD128, &l2->flag) ? 128 : 8;
+	save_flags(flags);
+	cli();
+	if(test_bit(FLG_MOD128, &l2->flag))
+		p1 = (l2->vs - l2->va) % 128;
+	else
+		p1 = (l2->vs - l2->va) % 8;
 	p1 = (p1 + l2->sow) % l2->window;
 	if (l2->windowar[p1]) {
 		printk(KERN_WARNING "isdnl2 try overwrite ack queue entry %d\n",
 		       p1);
-		dev_kfree_skb(l2->windowar[p1]);
+		idev_kfree_skb(l2->windowar[p1], FREE_WRITE);
 	}
 	l2->windowar[p1] = skb_clone(skb, GFP_ATOMIC);
 
@@ -1177,6 +1354,8 @@
 		header[i++] = (l2->vr << 5) | (l2->vs << 1);
 		l2->vs = (l2->vs + 1) % 8;
 	}
+	restore_flags(flags);
+
 	p1 = skb->data - skb->head;
 	if (p1 >= i)
 		memcpy(skb_push(skb, i), header, i);
@@ -1185,6 +1364,7 @@
 		"isdl2 pull_iqueue skb header(%d/%d) too short\n", i, p1);
 		oskb = skb;
 		skb = alloc_skb(oskb->len + i, GFP_ATOMIC);
+		SET_SKB_FREE(skb);
 		memcpy(skb_put(skb, i), header, i);
 		memcpy(skb_put(skb, oskb->len), oskb->data, oskb->len);
 		FreeSkb(oskb);
@@ -1200,74 +1380,58 @@
 }
 
 static void
-l2_got_st8_super(struct FsmInst *fi, int event, void *arg)
+l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 	struct sk_buff *skb = arg;
-	int PollFlag, nr, rsp, rnr = 0;
+	int PollFlag, rsp, rnr = 0;
+	unsigned int nr;
 	struct Layer2 *l2 = &st->l2;
 
 	rsp = *skb->data & 0x2;
 	if (test_bit(FLG_ORIG, &l2->flag))
 		rsp = !rsp;
+
 	skb_pull(skb, l2addrsize(l2));
 
-	if (IsRNR(skb->data, test_bit(FLG_MOD128, &l2->flag))) {
-		test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
+	if (IsRNR(skb->data, st)) {
+		set_peer_busy(l2);
 		rnr = 1;
 	} else
-		test_and_clear_bit(FLG_PEER_BUSY, &l2->flag);
+		clear_peer_busy(l2);
+
 	if (test_bit(FLG_MOD128, &l2->flag)) {
-		if (skb->len == 2) {
-			PollFlag = (skb->data[1] & 0x1) == 0x1;
-			nr = skb->data[1] >> 1;
-		} else {
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-			FreeSkb(skb);
-			establishlink(fi);
-			return;
-		}
+		PollFlag = (skb->data[1] & 0x1) == 0x1;
+		nr = skb->data[1] >> 1;
 	} else {
-		if (skb->len == 1) {
-			PollFlag = (skb->data[0] & 0x10);
-			nr = (skb->data[0] >> 5) & 0x7;
-		} else {
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-			FreeSkb(skb);
-			establishlink(fi);
-			return;
-		}
+		PollFlag = (skb->data[0] & 0x10);
+		nr = (skb->data[0] >> 5) & 0x7;
 	}
 	FreeSkb(skb);
 
 	if (rsp && PollFlag) {
 		if (legalnr(st, nr)) {
-			setva(st, nr);
-			if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-				FsmDelTimer(&st->l2.t200, 7);
-			FsmDelTimer(&l2->t203, 8);
 			if (rnr) {
-				FsmRestartTimer(&l2->t200, l2->T200,
-						EV_L2_T200, NULL, 14);
-				test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
-			} else
+				restart_t200(st, 15);
+			} else {
+				stop_t200(st, 16);
 				FsmAddTimer(&l2->t203, l2->T203,
 					    EV_L2_T203, NULL, 5);
+				setva(st, nr);
+			}
 			invoke_retransmission(st, nr);
 			FsmChangeState(fi, ST_L2_7);
 			if (skb_queue_len(&l2->i_queue) && cansend(st))
 				st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
-			else if (fi->userint & LC_FLUSH_WAIT) {
-				fi->userint &= ~LC_FLUSH_WAIT;
-				st->l2.l2l3(st, DL_FLUSH | INDICATION, NULL);
-			}
-		}
+		} else
+			nrerrorrecovery(fi);
 	} else {
 		if (!rsp && PollFlag)
 			enquiry_response(st);
 		if (legalnr(st, nr)) {
 			setva(st, nr);
-		}
+		} else
+			nrerrorrecovery(fi);
 	}
 }
 
@@ -1278,22 +1442,9 @@
 	struct sk_buff *skb = arg;
 
 	skb_pull(skb, l2addrsize(&st->l2) + 1);
-	if (test_bit(FLG_MOD128, &st->l2.flag)) {
-		if (skb->len < 5)
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-		else
-			l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x %2x %2x",
-				skb->data[0], skb->data[1], skb->data[2],
-				skb->data[3], skb->data[4]);
-	} else {
-		if (skb->len < 3)
-			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
-		else
-			l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x",
-				skb->data[0], skb->data[1], skb->data[2]);
-	}
+
 	if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) ||		/* I or S */
-	    (IsUA(skb->data, 0) && (fi->state == ST_L2_7))) {
+	    (IsUA(skb->data) && (fi->state == ST_L2_7))) {
 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'K');
 		establishlink(fi);
 		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
@@ -1302,115 +1453,209 @@
 }
 
 static void
+l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.ui_queue);
+	st->l2.tei = -1;
+	FsmChangeState(fi, ST_L2_1);
+}
+
+static void
+l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.ui_queue);
+	st->l2.tei = -1;
+	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
+	FsmChangeState(fi, ST_L2_1);
+}
+
+static void
+l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.i_queue);
+	discard_queue(&st->l2.ui_queue);
+	freewin(st);
+	st->l2.tei = -1;
+	stop_t200(st, 17);
+	st5_dl_release_l2l3(st);
+	FsmChangeState(fi, ST_L2_1);
+}
+
+static void
+l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.ui_queue);
+	st->l2.tei = -1;
+	stop_t200(st, 18);
+	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
+	FsmChangeState(fi, ST_L2_1);
+}
+
+static void
 l2_tei_remove(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
 
 	discard_queue(&st->l2.i_queue);
 	discard_queue(&st->l2.ui_queue);
+	freewin(st);
 	st->l2.tei = -1;
-	if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-		FsmDelTimer(&st->l2.t200, 18);
+	stop_t200(st, 17);
 	FsmDelTimer(&st->l2.t203, 19);
-	if (fi->state != ST_L2_4)
-		st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
+	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
 	FsmChangeState(fi, ST_L2_1);
 }
 
 static void
-l2_persistant_da(struct FsmInst *fi, int event, void *arg)
+l2_st14_persistant_da(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
-	int rel = DL_RELEASE | INDICATION;
-	
 	
 	discard_queue(&st->l2.i_queue);
 	discard_queue(&st->l2.ui_queue);
-	if (test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
-		FsmDelTimer(&st->l2.t200, 18);
+	if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
+		st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
+}
+
+static void
+l2_st5_persistant_da(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.i_queue);
+	discard_queue(&st->l2.ui_queue);
+	freewin(st);
+	stop_t200(st, 19);
+	st5_dl_release_l2l3(st);
+	FsmChangeState(fi, ST_L2_4);
+}
+
+static void
+l2_st6_persistant_da(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.ui_queue);
+	stop_t200(st, 20);
+	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
+	FsmChangeState(fi, ST_L2_4);
+}
+
+static void
+l2_persistant_da(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	discard_queue(&st->l2.i_queue);
+	discard_queue(&st->l2.ui_queue);
+	freewin(st);
+	stop_t200(st, 19);
 	FsmDelTimer(&st->l2.t203, 19);
-	clear_exception(&st->l2);
-	switch (fi->state) {
-		case ST_L2_1:
-			if (!test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
-				break;
-		case ST_L2_3:
-			st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
-		case ST_L2_2:
-			FsmChangeState(fi, ST_L2_1);
-			break;
-		case ST_L2_6:
-			rel = DL_RELEASE | CONFIRM;
-		case ST_L2_5:
-			if (test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
-				rel = DL_RELEASE | CONFIRM;
-		case ST_L2_7:
-		case ST_L2_8:
-			st->l2.l2l3(st, rel, NULL);
-			FsmChangeState(fi, ST_L2_4);
-			break;
-		case ST_L2_4:
-			if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
-				st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
-			break;
+	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
+	FsmChangeState(fi, ST_L2_4);
+}
+
+static void
+l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	if(!test_and_set_bit(FLG_OWN_BUSY, &st->l2.flag)) {
+		enquiry_cr(st, RNR, RSP, 0);
+		test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
 	}
-	test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
-	test_and_clear_bit(FLG_L1_ACTIV, &st->l2.flag);
+}
+
+static void
+l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	if(!test_and_clear_bit(FLG_OWN_BUSY, &st->l2.flag)) {
+		enquiry_cr(st, RR, RSP, 0);
+		test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
+	}
+}
+
+static void
+l2_frame_error(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	st->ma.layer(st, MDL_ERROR | INDICATION, arg);
+}
+
+static void
+l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
+{
+	struct PStack *st = fi->userdata;
+
+	st->ma.layer(st, MDL_ERROR | INDICATION, arg);
+	establishlink(fi);
+	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
 }
 
 static struct FsmNode L2FnList[] HISAX_INITDATA =
 {
-	{ST_L2_1, EV_L2_DL_ESTABLISH, l2_dl_establish},
-	{ST_L2_2, EV_L2_DL_ESTABLISH, l2_dl_establish},
-	{ST_L2_4, EV_L2_DL_ESTABLISH, l2_establish},
-	{ST_L2_5, EV_L2_DL_ESTABLISH, l2_establish},
-	{ST_L2_7, EV_L2_DL_ESTABLISH, l2_establish},
-	{ST_L2_8, EV_L2_DL_ESTABLISH, l2_establish},
-	{ST_L2_4, EV_L2_DL_RELEASE, l2_dl_release},
-	{ST_L2_5, EV_L2_DL_RELEASE, l2_dl_release},
-	{ST_L2_7, EV_L2_DL_RELEASE, l2_dl_release},
-	{ST_L2_8, EV_L2_DL_RELEASE, l2_dl_release},
-	{ST_L2_5, EV_L2_DL_DATA, l2_feed_iqueue},
-	{ST_L2_7, EV_L2_DL_DATA, l2_feed_iqueue},
+	{ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
+	{ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
+	{ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
+	{ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
+	{ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
+	{ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
+	{ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
+	{ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
+	{ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
+	{ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
+	{ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
+	{ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
 	{ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
-	{ST_L2_1, EV_L2_DL_UNIT_DATA, l2_put_ui},
-	{ST_L2_2, EV_L2_DL_UNIT_DATA, l2_put_ui},
-	{ST_L2_3, EV_L2_DL_UNIT_DATA, l2_put_ui},
-	{ST_L2_4, EV_L2_DL_UNIT_DATA, l2_put_ui},
-	{ST_L2_5, EV_L2_DL_UNIT_DATA, l2_put_ui},
-	{ST_L2_6, EV_L2_DL_UNIT_DATA, l2_put_ui},
-	{ST_L2_7, EV_L2_DL_UNIT_DATA, l2_put_ui},
-	{ST_L2_8, EV_L2_DL_UNIT_DATA, l2_put_ui},
+	{ST_L2_1, EV_L2_DL_UNIT_DATA, l2_queue_ui_assign},
+	{ST_L2_2, EV_L2_DL_UNIT_DATA, l2_queue_ui},
+	{ST_L2_3, EV_L2_DL_UNIT_DATA, l2_queue_ui},
+	{ST_L2_4, EV_L2_DL_UNIT_DATA, l2_send_ui},
+	{ST_L2_5, EV_L2_DL_UNIT_DATA, l2_send_ui},
+	{ST_L2_6, EV_L2_DL_UNIT_DATA, l2_send_ui},
+	{ST_L2_7, EV_L2_DL_UNIT_DATA, l2_send_ui},
+	{ST_L2_8, EV_L2_DL_UNIT_DATA, l2_send_ui},
 	{ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
 	{ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
 	{ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
-	{ST_L2_2, EV_L2_MDL_ERROR, l2_tei_remove},
-	{ST_L2_3, EV_L2_MDL_ERROR, l2_tei_remove},
-	{ST_L2_4, EV_L2_MDL_REMOVE, l2_tei_remove},
-	{ST_L2_5, EV_L2_MDL_REMOVE, l2_tei_remove},
-	{ST_L2_6, EV_L2_MDL_REMOVE, l2_tei_remove},
+	{ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
+	{ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
+	{ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
+	{ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
+	{ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
 	{ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
 	{ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
-	{ST_L2_4, EV_L2_SABMX, l2_got_SABMX},
-	{ST_L2_5, EV_L2_SABMX, l2_got_SABMX},
-	{ST_L2_6, EV_L2_SABMX, l2_got_SABMX},
-	{ST_L2_7, EV_L2_SABMX, l2_got_SABMX},
-	{ST_L2_8, EV_L2_SABMX, l2_got_SABMX},
-	{ST_L2_4, EV_L2_DISC, l2_got_disconn},
-	{ST_L2_5, EV_L2_DISC, l2_got_disconn},
-	{ST_L2_6, EV_L2_DISC, l2_got_disconn},
-	{ST_L2_7, EV_L2_DISC, l2_got_disconn},
-	{ST_L2_8, EV_L2_DISC, l2_got_disconn},
-	{ST_L2_4, EV_L2_UA, l2_mdl_error},
-	{ST_L2_5, EV_L2_UA, l2_got_ua},
-	{ST_L2_6, EV_L2_UA, l2_got_ua},
-	{ST_L2_7, EV_L2_UA, l2_mdl_error},
-	{ST_L2_8, EV_L2_UA, l2_mdl_error},
-	{ST_L2_4, EV_L2_DM, l2_got_dm},
-	{ST_L2_5, EV_L2_DM, l2_got_dm},
-	{ST_L2_6, EV_L2_DM, l2_got_dm},
-	{ST_L2_7, EV_L2_DM, l2_got_dm},
-	{ST_L2_8, EV_L2_DM, l2_got_dm},
+	{ST_L2_4, EV_L2_SABME, l2_start_multi},
+	{ST_L2_5, EV_L2_SABME, l2_send_UA},
+	{ST_L2_6, EV_L2_SABME, l2_send_DM},
+	{ST_L2_7, EV_L2_SABME, l2_restart_multi},
+	{ST_L2_8, EV_L2_SABME, l2_restart_multi},
+	{ST_L2_4, EV_L2_DISC, l2_send_DM},
+	{ST_L2_5, EV_L2_DISC, l2_send_DM},
+	{ST_L2_6, EV_L2_DISC, l2_send_UA},
+	{ST_L2_7, EV_L2_DISC, l2_stop_multi},
+	{ST_L2_8, EV_L2_DISC, l2_stop_multi},
+	{ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
+	{ST_L2_5, EV_L2_UA, l2_connected},
+	{ST_L2_6, EV_L2_UA, l2_released},
+	{ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
+	{ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
+	{ST_L2_4, EV_L2_DM, l2_reestablish},
+	{ST_L2_5, EV_L2_DM, l2_st5_dm_release},
+	{ST_L2_6, EV_L2_DM, l2_st6_dm_release},
+	{ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
+	{ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
 	{ST_L2_1, EV_L2_UI, l2_got_ui},
 	{ST_L2_2, EV_L2_UI, l2_got_ui},
 	{ST_L2_3, EV_L2_UI, l2_got_ui},
@@ -1421,22 +1666,31 @@
 	{ST_L2_8, EV_L2_UI, l2_got_ui},
 	{ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
 	{ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
-	{ST_L2_7, EV_L2_SUPER, l2_got_st7_super},
-	{ST_L2_8, EV_L2_SUPER, l2_got_st8_super},
+	{ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
+	{ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
 	{ST_L2_7, EV_L2_I, l2_got_iframe},
 	{ST_L2_8, EV_L2_I, l2_got_iframe},
 	{ST_L2_5, EV_L2_T200, l2_st5_tout_200},
 	{ST_L2_6, EV_L2_T200, l2_st6_tout_200},
-	{ST_L2_7, EV_L2_T200, l2_st78_tout_200},
-	{ST_L2_8, EV_L2_T200, l2_st78_tout_200},
+	{ST_L2_7, EV_L2_T200, l2_st7_tout_200},
+	{ST_L2_8, EV_L2_T200, l2_st8_tout_200},
 	{ST_L2_7, EV_L2_T203, l2_st7_tout_203},
 	{ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
-	{ST_L2_1, EV_L1_DEACTIVATE, l2_persistant_da},
-	{ST_L2_2, EV_L1_DEACTIVATE, l2_persistant_da},
-	{ST_L2_3, EV_L1_DEACTIVATE, l2_persistant_da},
-	{ST_L2_4, EV_L1_DEACTIVATE, l2_persistant_da},
-	{ST_L2_5, EV_L1_DEACTIVATE, l2_persistant_da},
-	{ST_L2_6, EV_L1_DEACTIVATE, l2_persistant_da},
+	{ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
+	{ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
+	{ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
+	{ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
+	{ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
+	{ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
+	{ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
+	{ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
+	{ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
+	{ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistant_da},
+	{ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
+	{ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
+	{ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistant_da},
+	{ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistant_da},
+	{ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistant_da},
 	{ST_L2_7, EV_L1_DEACTIVATE, l2_persistant_da},
 	{ST_L2_8, EV_L1_DEACTIVATE, l2_persistant_da},
 };
@@ -1449,6 +1703,7 @@
 	struct sk_buff *skb = arg;
 	u_char *datap;
 	int ret = 1, len;
+	int c = 0;
 
 	switch (pr) {
 		case (PH_DATA | INDICATION):
@@ -1457,36 +1712,46 @@
 			if (skb->len > len)
 				datap += len;
 			else {
-				st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'N');
+				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) 'N');
 				FreeSkb(skb);
 				return;
 			}
-			if (!(*datap & 1))	/* I-Frame */
-				ret = FsmEvent(&st->l2.l2m, EV_L2_I, skb);
-			else if (IsSFrame(datap, test_bit(FLG_MOD128, &st->l2.flag)))
-				ret = FsmEvent(&st->l2.l2m, EV_L2_SUPER, skb);
-			else if (IsUI(datap, test_bit(FLG_MOD128, &st->l2.flag)))
-				ret = FsmEvent(&st->l2.l2m, EV_L2_UI, skb);
-			else if (IsSABMX(datap, test_bit(FLG_MOD128, &st->l2.flag)))
-				ret = FsmEvent(&st->l2.l2m, EV_L2_SABMX, skb);
-			else if (IsUA(datap, test_bit(FLG_MOD128, &st->l2.flag)))
-				ret = FsmEvent(&st->l2.l2m, EV_L2_UA, skb);
-			else if (IsDISC(datap, test_bit(FLG_MOD128, &st->l2.flag)))
-				ret = FsmEvent(&st->l2.l2m, EV_L2_DISC, skb);
-			else if (IsDM(datap, test_bit(FLG_MOD128, &st->l2.flag)))
-				ret = FsmEvent(&st->l2.l2m, EV_L2_DM, skb);
-			else if (IsFRMR(datap, test_bit(FLG_MOD128, &st->l2.flag)))
-				ret = FsmEvent(&st->l2.l2m, EV_L2_FRMR, skb);
-			else {
-				ret = 1;
-				if ((st->l2.l2m.state == ST_L2_7) ||
-					(st->l2.l2m.state == ST_L2_8))
-					establishlink(&st->l2.l2m);
-				st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'L');
+			if (!(*datap & 1)) {	/* I-Frame */
+				if(!(c = iframe_error(st, skb)))
+					ret = FsmEvent(&st->l2.l2m, EV_L2_I, skb);
+			} else if (IsSFrame(datap, st)) {	/* S-Frame */
+				if(!(c = super_error(st, skb)))
+					ret = FsmEvent(&st->l2.l2m, EV_L2_SUPER, skb);
+			} else if (IsUI(datap)) {
+				if(!(c = UI_error(st, skb)))
+					ret = FsmEvent(&st->l2.l2m, EV_L2_UI, skb);
+			} else if (IsSABME(datap, st)) {
+				if(!(c = unnum_error(st, skb, CMD)))
+					ret = FsmEvent(&st->l2.l2m, EV_L2_SABME, skb);
+			} else if (IsUA(datap)) {
+				if(!(c = unnum_error(st, skb, RSP)))
+					ret = FsmEvent(&st->l2.l2m, EV_L2_UA, skb);
+			} else if (IsDISC(datap)) {
+				if(!(c = unnum_error(st, skb, CMD)))
+					ret = FsmEvent(&st->l2.l2m, EV_L2_DISC, skb);
+			} else if (IsDM(datap)) {
+				if(!(c = unnum_error(st, skb, RSP)))
+					ret = FsmEvent(&st->l2.l2m, EV_L2_DM, skb);
+			} else if (IsFRMR(datap)) {
+				if(!(c = FRMR_error(st,skb)))
+					ret = FsmEvent(&st->l2.l2m, EV_L2_FRMR, skb);
+			} else {
+				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) 'L');
+				FreeSkb(skb);
+				ret = 0;
 			}
-			if (ret) {
+			if(c) {
 				FreeSkb(skb);
+				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) c);
+				ret = 0;
 			}
+			if (ret)
+				FreeSkb(skb);
 			break;
 		case (PH_PULL | CONFIRM):
 			FsmEvent(&st->l2.l2m, EV_L2_ACK_PULL, arg);
@@ -1501,7 +1766,7 @@
 		case (PH_ACTIVATE | INDICATION):
 			test_and_set_bit(FLG_L1_ACTIV, &st->l2.flag);
 			if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
-				FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH, arg);
+				FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH_REQ, arg);
 			break;
 		case (PH_DEACTIVATE | INDICATION):
 		case (PH_DEACTIVATE | CONFIRM):
@@ -1520,19 +1785,19 @@
 	switch (pr) {
 		case (DL_DATA | REQUEST):
 			if (FsmEvent(&st->l2.l2m, EV_L2_DL_DATA, arg)) {
-				dev_kfree_skb((struct sk_buff *) arg);
+				idev_kfree_skb((struct sk_buff *) arg, FREE_READ);
 			}
 			break;
 		case (DL_UNIT_DATA | REQUEST):
 			if (FsmEvent(&st->l2.l2m, EV_L2_DL_UNIT_DATA, arg)) {
-				dev_kfree_skb((struct sk_buff *) arg);
+				idev_kfree_skb((struct sk_buff *) arg, FREE_READ);
 			}
 			break;
 		case (DL_ESTABLISH | REQUEST):
 			if (test_bit(FLG_L1_ACTIV, &st->l2.flag)) {
 				if (test_bit(FLG_LAPD, &st->l2.flag) ||
 					test_bit(FLG_ORIG, &st->l2.flag)) {
-					FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH, arg);
+					FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH_REQ, arg);
 				}
 			} else {
 				if (test_bit(FLG_LAPD, &st->l2.flag) ||
@@ -1546,10 +1811,7 @@
 			if (test_bit(FLG_LAPB, &st->l2.flag)) {
 				st->l2.l2l1(st, PH_DEACTIVATE, NULL);
 			}
-			FsmEvent(&st->l2.l2m, EV_L2_DL_RELEASE, arg);
-			break;
-		case (DL_FLUSH | REQUEST):
-			(&st->l2.l2m)->userint |= LC_FLUSH_WAIT;
+			FsmEvent(&st->l2.l2m, EV_L2_DL_RELEASE_REQ, arg);
 			break;
 		case (MDL_ASSIGN | REQUEST):
 			FsmEvent(&st->l2.l2m, EV_L2_MDL_ASSIGN, arg);
@@ -1566,7 +1828,7 @@
 void
 releasestack_isdnl2(struct PStack *st)
 {
-	FsmDelTimer(&st->l2.t200, 15);
+	FsmDelTimer(&st->l2.t200, 21);
 	FsmDelTimer(&st->l2.t203, 16);
 	discard_queue(&st->l2.i_queue);
 	discard_queue(&st->l2.ui_queue);

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)