1 // SPDX-License-Identifier: GPL-2.0-only
4 * Author Karsten Keil <kkeil@novell.com>
6 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
9 #include <linux/random.h>
10 #include <linux/slab.h>
21 #define TEI_ENTITY_ID 0xf
23 #define MGR_PH_ACTIVE 16
24 #define MGR_PH_NOTREADY 17
26 #define DATIMER_VAL 10000
30 static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
31 static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
32 static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
39 #define DEACT_STATE_COUNT (ST_L1_ACTIV + 1)
41 static char *strDeactState[] =
44 "ST_L1_DEACT_PENDING",
57 #define DEACT_EVENT_COUNT (EV_DATIMER + 1)
59 static char *strDeactEvent[] =
70 da_debug(struct FsmInst *fi, char *fmt, ...)
72 struct manager *mgr = fi->userdata;
76 if (!(*debug & DEBUG_L2_TEIFSM))
84 printk(KERN_DEBUG "mgr(%d): %pV\n", mgr->ch.st->dev->id, &vaf);
90 da_activate(struct FsmInst *fi, int event, void *arg)
92 struct manager *mgr = fi->userdata;
94 if (fi->state == ST_L1_DEACT_PENDING)
95 mISDN_FsmDelTimer(&mgr->datimer, 1);
96 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
100 da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
102 mISDN_FsmChangeState(fi, ST_L1_DEACT);
106 da_deactivate(struct FsmInst *fi, int event, void *arg)
108 struct manager *mgr = fi->userdata;
112 read_lock_irqsave(&mgr->lock, flags);
113 list_for_each_entry(l2, &mgr->layer2, list) {
114 if (l2->l2m.state > ST_L2_4) {
115 /* have still activ TEI */
116 read_unlock_irqrestore(&mgr->lock, flags);
120 read_unlock_irqrestore(&mgr->lock, flags);
121 /* All TEI are inactiv */
122 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
123 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
125 mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
130 da_ui(struct FsmInst *fi, int event, void *arg)
132 struct manager *mgr = fi->userdata;
134 /* restart da timer */
135 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
136 mISDN_FsmDelTimer(&mgr->datimer, 2);
137 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
143 da_timer(struct FsmInst *fi, int event, void *arg)
145 struct manager *mgr = fi->userdata;
150 read_lock_irqsave(&mgr->lock, flags);
151 list_for_each_entry(l2, &mgr->layer2, list) {
152 if (l2->l2m.state > ST_L2_4) {
153 /* have still activ TEI */
154 read_unlock_irqrestore(&mgr->lock, flags);
155 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
159 read_unlock_irqrestore(&mgr->lock, flags);
160 /* All TEI are inactiv */
161 mISDN_FsmChangeState(fi, ST_L1_DEACT);
162 _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
166 static struct FsmNode DeactFnList[] =
168 {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
169 {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
170 {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
171 {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
172 {ST_L1_DEACT_PENDING, EV_UI, da_ui},
173 {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
182 #define TEI_STATE_COUNT (ST_TEI_IDVERIFY + 1)
184 static char *strTeiState[] =
203 #define TEI_EVENT_COUNT (EV_TIMER + 1)
205 static char *strTeiEvent[] =
219 tei_debug(struct FsmInst *fi, char *fmt, ...)
221 struct teimgr *tm = fi->userdata;
222 struct va_format vaf;
225 if (!(*debug & DEBUG_L2_TEIFSM))
233 printk(KERN_DEBUG "sapi(%d) tei(%d): %pV\n",
234 tm->l2->sapi, tm->l2->tei, &vaf);
242 get_free_id(struct manager *mgr)
244 DECLARE_BITMAP(ids, 64) = { [0 ... BITS_TO_LONGS(64) - 1] = 0 };
248 list_for_each_entry(l2, &mgr->layer2, list) {
249 if (l2->ch.nr > 63) {
251 "%s: more as 63 layer2 for one device\n",
255 __set_bit(l2->ch.nr, ids);
257 i = find_next_zero_bit(ids, 64, 1);
260 printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
266 get_free_tei(struct manager *mgr)
268 DECLARE_BITMAP(ids, 64) = { [0 ... BITS_TO_LONGS(64) - 1] = 0 };
272 list_for_each_entry(l2, &mgr->layer2, list) {
275 if ((l2->ch.addr & 0xff) != 0)
277 i = l2->ch.addr >> 8;
284 i = find_first_zero_bit(ids, 64);
287 printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
293 teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
296 struct mISDNhead *hh;
299 skb = mI_alloc_skb(len, GFP_ATOMIC);
302 hh = mISDN_HEAD_P(skb);
304 hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
306 skb_put_data(skb, arg, len);
307 err = mgr->up->send(mgr->up, skb);
309 printk(KERN_WARNING "%s: err=%d\n", __func__, err);
315 new_id(struct manager *mgr)
323 id |= GROUP_TEI << 8;
329 do_send(struct manager *mgr)
331 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
334 if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
335 struct sk_buff *skb = skb_dequeue(&mgr->sendq);
338 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
341 mgr->lastid = mISDN_HEAD_ID(skb);
342 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
343 if (mgr->ch.recv(mgr->ch.peer, skb)) {
345 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
346 mgr->lastid = MISDN_ID_NONE;
352 do_ack(struct manager *mgr, u_int id)
354 if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
355 if (id == mgr->lastid) {
356 if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
359 skb = skb_dequeue(&mgr->sendq);
361 mgr->lastid = mISDN_HEAD_ID(skb);
362 if (!mgr->ch.recv(mgr->ch.peer, skb))
367 mgr->lastid = MISDN_ID_NONE;
368 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
374 mgr_send_down(struct manager *mgr, struct sk_buff *skb)
376 skb_queue_tail(&mgr->sendq, skb);
377 if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
378 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
386 dl_unit_data(struct manager *mgr, struct sk_buff *skb)
388 if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
390 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
391 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
394 skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
395 skb->data[1] = 0xff; /* TEI 127 */
396 skb->data[2] = UI; /* UI frame */
397 mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
398 mISDN_HEAD_ID(skb) = new_id(mgr);
399 skb_queue_tail(&mgr->sendq, skb);
409 get_random_bytes(&x, sizeof(x));
413 static struct layer2 *
414 findtei(struct manager *mgr, int tei)
419 read_lock_irqsave(&mgr->lock, flags);
420 list_for_each_entry(l2, &mgr->layer2, list) {
421 if ((l2->sapi == 0) && (l2->tei > 0) &&
422 (l2->tei != GROUP_TEI) && (l2->tei == tei))
427 read_unlock_irqrestore(&mgr->lock, flags);
432 put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei)
437 bp[0] = (TEI_SAPI << 2);
438 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
439 bp[0] |= 2; /* CR:=1 for net command */
440 bp[1] = (GROUP_TEI << 1) | 0x1;
442 bp[3] = TEI_ENTITY_ID;
446 bp[7] = ((tei << 1) & 0xff) | 1;
447 skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC);
449 printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
452 mgr_send_down(mgr, skb);
456 tei_id_request(struct FsmInst *fi, int event, void *arg)
458 struct teimgr *tm = fi->userdata;
460 if (tm->l2->tei != GROUP_TEI) {
461 tm->tei_m.printdebug(&tm->tei_m,
462 "assign request for already assigned tei %d",
466 tm->ri = random_ri();
467 if (*debug & DEBUG_L2_TEI)
468 tm->tei_m.printdebug(&tm->tei_m,
469 "assign request ri %d", tm->ri);
470 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
471 mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
472 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
477 tei_id_assign(struct FsmInst *fi, int event, void *arg)
479 struct teimgr *tm = fi->userdata;
484 ri = ((unsigned int) *dp++ << 8);
488 if (*debug & DEBUG_L2_TEI)
489 tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
491 l2 = findtei(tm->mgr, tei);
492 if (l2) { /* same tei is in use */
493 if (ri != l2->tm->ri) {
494 tm->tei_m.printdebug(fi,
495 "possible duplicate assignment tei %d", tei);
496 tei_l2(l2, MDL_ERROR_RSP, 0);
498 } else if (ri == tm->ri) {
499 mISDN_FsmDelTimer(&tm->timer, 1);
500 mISDN_FsmChangeState(fi, ST_TEI_NOP);
501 tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
506 tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
508 struct teimgr *tm = fi->userdata;
513 ri = ((unsigned int) *dp++ << 8);
517 if (*debug & DEBUG_L2_TEI)
518 tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
520 l2 = findtei(tm->mgr, tei);
521 if (l2) { /* same tei is in use */
522 if (ri != l2->tm->ri) { /* and it wasn't our request */
523 tm->tei_m.printdebug(fi,
524 "possible duplicate assignment tei %d", tei);
525 mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
531 tei_id_denied(struct FsmInst *fi, int event, void *arg)
533 struct teimgr *tm = fi->userdata;
537 ri = ((unsigned int) *dp++ << 8);
541 if (*debug & DEBUG_L2_TEI)
542 tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
547 tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
549 struct teimgr *tm = fi->userdata;
553 tei = *(dp + 3) >> 1;
554 if (*debug & DEBUG_L2_TEI)
555 tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
556 if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
557 (tei == tm->l2->tei))) {
558 mISDN_FsmDelTimer(&tm->timer, 4);
559 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
560 put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
565 tei_id_remove(struct FsmInst *fi, int event, void *arg)
567 struct teimgr *tm = fi->userdata;
571 tei = *(dp + 3) >> 1;
572 if (*debug & DEBUG_L2_TEI)
573 tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
574 if ((tm->l2->tei != GROUP_TEI) &&
575 ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
576 mISDN_FsmDelTimer(&tm->timer, 5);
577 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
578 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
583 tei_id_verify(struct FsmInst *fi, int event, void *arg)
585 struct teimgr *tm = fi->userdata;
587 if (*debug & DEBUG_L2_TEI)
588 tm->tei_m.printdebug(fi, "id verify request for tei %d",
590 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
591 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
592 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
597 tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
599 struct teimgr *tm = fi->userdata;
602 tm->ri = random_ri();
603 if (*debug & DEBUG_L2_TEI)
604 tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
605 4 - tm->nval, tm->ri);
606 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
607 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
609 tm->tei_m.printdebug(fi, "assign req failed");
610 tei_l2(tm->l2, MDL_ERROR_RSP, 0);
611 mISDN_FsmChangeState(fi, ST_TEI_NOP);
616 tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
618 struct teimgr *tm = fi->userdata;
621 if (*debug & DEBUG_L2_TEI)
622 tm->tei_m.printdebug(fi,
623 "id verify req(%d) for tei %d",
624 3 - tm->nval, tm->l2->tei);
625 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
626 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
628 tm->tei_m.printdebug(fi, "verify req for tei %d failed",
630 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
631 mISDN_FsmChangeState(fi, ST_TEI_NOP);
635 static struct FsmNode TeiFnListUser[] =
637 {ST_TEI_NOP, EV_IDREQ, tei_id_request},
638 {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
639 {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
640 {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
641 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
642 {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
643 {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
644 {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
645 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
646 {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
647 {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
651 tei_l2remove(struct layer2 *l2)
653 put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
654 tei_l2(l2, MDL_REMOVE_REQ, 0);
655 list_del(&l2->ch.list);
656 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
660 tei_assign_req(struct FsmInst *fi, int event, void *arg)
662 struct teimgr *tm = fi->userdata;
665 if (tm->l2->tei == GROUP_TEI) {
666 tm->tei_m.printdebug(&tm->tei_m,
667 "net tei assign request without tei");
670 tm->ri = ((unsigned int) *dp++ << 8);
672 if (*debug & DEBUG_L2_TEI)
673 tm->tei_m.printdebug(&tm->tei_m,
674 "net assign request ri %d teim %d", tm->ri, *dp);
675 put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
676 mISDN_FsmChangeState(fi, ST_TEI_NOP);
680 tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
682 struct teimgr *tm = fi->userdata;
684 if (*debug & DEBUG_L2_TEI)
685 tm->tei_m.printdebug(fi, "id check request for tei %d",
688 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
689 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
690 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
695 tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
697 struct teimgr *tm = fi->userdata;
702 if (*debug & DEBUG_L2_TEI)
703 tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
704 if (tei == tm->l2->tei)
709 tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
711 struct teimgr *tm = fi->userdata;
716 if (*debug & DEBUG_L2_TEI)
717 tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
719 if (tei == tm->l2->tei)
720 tei_id_chk_req_net(fi, event, arg);
724 tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
726 struct teimgr *tm = fi->userdata;
729 if (*debug & DEBUG_L2_TEI)
730 tm->tei_m.printdebug(fi,
731 "check req for tei %d successful\n", tm->l2->tei);
732 mISDN_FsmChangeState(fi, ST_TEI_NOP);
733 } else if (tm->rcnt > 1) {
734 /* duplicate assignment; remove */
735 tei_l2remove(tm->l2);
736 } else if (--tm->nval) {
737 if (*debug & DEBUG_L2_TEI)
738 tm->tei_m.printdebug(fi,
739 "id check req(%d) for tei %d",
740 3 - tm->nval, tm->l2->tei);
741 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
742 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
744 tm->tei_m.printdebug(fi, "check req for tei %d failed",
746 mISDN_FsmChangeState(fi, ST_TEI_NOP);
747 tei_l2remove(tm->l2);
751 static struct FsmNode TeiFnListNet[] =
753 {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
754 {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
755 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
756 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
757 {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
761 tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
763 if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
765 if (*debug & DEBUG_L2_TEI)
766 tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
767 if (mt == ID_ASSIGNED)
768 mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
769 else if (mt == ID_DENIED)
770 mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
771 else if (mt == ID_CHK_REQ)
772 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
773 else if (mt == ID_REMOVE)
774 mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
775 else if (mt == ID_VERIFY)
776 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
777 else if (mt == ID_CHK_RES)
778 mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
781 static struct layer2 *
782 create_new_tei(struct manager *mgr, int tei, int sapi)
784 unsigned long opt = 0;
788 struct channel_req rq;
792 if ((tei >= 0) && (tei < 64))
793 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
794 if (mgr->ch.st->dev->Dprotocols & ((1 << ISDN_P_TE_E1) |
795 (1 << ISDN_P_NT_E1))) {
796 test_and_set_bit(OPTION_L2_PMX, &opt);
797 rq.protocol = ISDN_P_NT_E1;
799 rq.protocol = ISDN_P_NT_S0;
801 l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi);
803 printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
806 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
809 printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
814 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
815 l2->tm->tei_m.userdata = l2->tm;
816 l2->tm->tei_m.printdebug = tei_debug;
817 l2->tm->tei_m.fsm = &teifsmn;
818 l2->tm->tei_m.state = ST_TEI_NOP;
819 l2->tm->tval = 2000; /* T202 2 sec */
820 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
821 write_lock_irqsave(&mgr->lock, flags);
822 id = get_free_id(mgr);
823 list_add_tail(&l2->list, &mgr->layer2);
824 write_unlock_irqrestore(&mgr->lock, flags);
826 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
827 printk(KERN_WARNING "%s:no free id\n", __func__);
831 __add_layer2(&l2->ch, mgr->ch.st);
832 l2->ch.recv = mgr->ch.recv;
833 l2->ch.peer = mgr->ch.peer;
834 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
835 /* We need open here L1 for the manager as well (refcounting) */
836 rq.adr.dev = mgr->ch.st->dev->id;
837 id = mgr->ch.st->own.ctrl(&mgr->ch.st->own, OPEN_CHANNEL, &rq);
839 printk(KERN_WARNING "%s: cannot open L1\n", __func__);
840 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
848 new_tei_req(struct manager *mgr, u_char *dp)
857 if (!(dp[3] & 1)) /* Extension bit != 1 */
860 tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
862 tei = get_free_tei(mgr);
864 printk(KERN_WARNING "%s:No free tei\n", __func__);
867 l2 = create_new_tei(mgr, tei, CTRL_SAPI);
871 mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
874 put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
878 ph_data_ind(struct manager *mgr, struct sk_buff *skb)
881 struct layer2 *l2, *nl2;
885 if (*debug & DEBUG_L2_TEI)
886 printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
891 if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
893 if (skb->data[0] & 1) /* EA0 formal error */
895 if (!(skb->data[1] & 1)) /* EA1 formal error */
897 if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
899 if ((skb->data[2] & 0xef) != UI) /* not UI */
901 if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
908 if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
915 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
922 if (mt == ID_REQUEST) {
923 new_tei_req(mgr, &skb->data[4]);
926 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
927 tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
934 l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
936 struct teimgr *tm = l2->tm;
938 if (test_bit(FLG_FIXED_TEI, &l2->flag))
940 if (*debug & DEBUG_L2_TEI)
941 printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
944 mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
947 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
948 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
949 if (test_bit(MGR_OPT_USER, &tm->mgr->options))
950 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
952 case MDL_STATUS_UP_IND:
953 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
954 mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
956 case MDL_STATUS_DOWN_IND:
957 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
958 mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
960 case MDL_STATUS_UI_IND:
961 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
962 mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
969 TEIrelease(struct layer2 *l2)
971 struct teimgr *tm = l2->tm;
974 mISDN_FsmDelTimer(&tm->timer, 1);
975 write_lock_irqsave(&tm->mgr->lock, flags);
977 write_unlock_irqrestore(&tm->mgr->lock, flags);
983 create_teimgr(struct manager *mgr, struct channel_req *crq)
986 unsigned long opt = 0;
989 struct channel_req l1rq;
991 if (*debug & DEBUG_L2_TEI)
992 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
993 __func__, dev_name(&mgr->ch.st->dev->dev),
994 crq->protocol, crq->adr.dev, crq->adr.channel,
995 crq->adr.sapi, crq->adr.tei);
996 if (crq->adr.tei > GROUP_TEI)
998 if (crq->adr.tei < 64)
999 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
1000 if (crq->adr.tei == 0)
1001 test_and_set_bit(OPTION_L2_PTP, &opt);
1002 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
1003 if (crq->protocol == ISDN_P_LAPD_TE)
1004 return -EPROTONOSUPPORT;
1005 if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
1009 "%s: only one network manager is allowed\n",
1013 } else if (test_bit(MGR_OPT_USER, &mgr->options)) {
1014 if (crq->protocol == ISDN_P_LAPD_NT)
1015 return -EPROTONOSUPPORT;
1016 if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
1017 return -EINVAL; /* dyn tei */
1019 if (crq->protocol == ISDN_P_LAPD_NT)
1020 test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
1021 if (crq->protocol == ISDN_P_LAPD_TE)
1022 test_and_set_bit(MGR_OPT_USER, &mgr->options);
1024 l1rq.adr = crq->adr;
1025 if (mgr->ch.st->dev->Dprotocols
1026 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
1027 test_and_set_bit(OPTION_L2_PMX, &opt);
1028 if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
1030 id = DL_INFO_L2_CONNECT;
1031 teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
1032 if (test_bit(MGR_PH_ACTIVE, &mgr->options))
1033 teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
1035 if (!list_empty(&mgr->layer2)) {
1036 read_lock_irqsave(&mgr->lock, flags);
1037 list_for_each_entry(l2, &mgr->layer2, list) {
1039 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
1041 read_unlock_irqrestore(&mgr->lock, flags);
1045 l2 = create_l2(crq->ch, crq->protocol, opt,
1046 crq->adr.tei, crq->adr.sapi);
1049 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
1052 printk(KERN_ERR "kmalloc teimgr failed\n");
1057 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
1058 l2->tm->tei_m.userdata = l2->tm;
1059 l2->tm->tei_m.printdebug = tei_debug;
1060 if (crq->protocol == ISDN_P_LAPD_TE) {
1061 l2->tm->tei_m.fsm = &teifsmu;
1062 l2->tm->tei_m.state = ST_TEI_NOP;
1063 l2->tm->tval = 1000; /* T201 1 sec */
1064 if (test_bit(OPTION_L2_PMX, &opt))
1065 l1rq.protocol = ISDN_P_TE_E1;
1067 l1rq.protocol = ISDN_P_TE_S0;
1069 l2->tm->tei_m.fsm = &teifsmn;
1070 l2->tm->tei_m.state = ST_TEI_NOP;
1071 l2->tm->tval = 2000; /* T202 2 sec */
1072 if (test_bit(OPTION_L2_PMX, &opt))
1073 l1rq.protocol = ISDN_P_NT_E1;
1075 l1rq.protocol = ISDN_P_NT_S0;
1077 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
1078 write_lock_irqsave(&mgr->lock, flags);
1079 id = get_free_id(mgr);
1080 list_add_tail(&l2->list, &mgr->layer2);
1081 write_unlock_irqrestore(&mgr->lock, flags);
1086 /* We need open here L1 for the manager as well (refcounting) */
1087 id = mgr->ch.st->own.ctrl(&mgr->ch.st->own, OPEN_CHANNEL,
1091 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1096 mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
1098 struct manager *mgr;
1099 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1102 mgr = container_of(ch, struct manager, ch);
1103 if (*debug & DEBUG_L2_RECV)
1104 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1105 __func__, hh->prim, hh->id);
1108 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
1109 ret = ph_data_ind(mgr, skb);
1112 do_ack(mgr, hh->id);
1115 case PH_ACTIVATE_IND:
1116 test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
1118 teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
1119 mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
1123 case PH_DEACTIVATE_IND:
1124 test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
1126 teiup_create(mgr, PH_DEACTIVATE_IND, 0, NULL);
1127 mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
1130 case DL_UNITDATA_REQ:
1131 return dl_unit_data(mgr, skb);
1139 free_teimanager(struct manager *mgr)
1141 struct layer2 *l2, *nl2;
1143 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1144 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
1145 /* not locked lock is taken in release tei */
1147 if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
1148 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1149 put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
1150 mutex_lock(&mgr->ch.st->lmutex);
1151 list_del(&l2->ch.list);
1152 mutex_unlock(&mgr->ch.st->lmutex);
1153 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1155 test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
1157 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1162 if (test_bit(MGR_OPT_USER, &mgr->options)) {
1163 if (list_empty(&mgr->layer2))
1164 test_and_clear_bit(MGR_OPT_USER, &mgr->options);
1166 mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
1171 ctrl_teimanager(struct manager *mgr, void *arg)
1173 /* currently we only have one option */
1174 unsigned int *val = (unsigned int *)arg;
1179 test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
1181 test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
1185 test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
1187 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1195 /* This function does create a L2 for fixed TEI in NT Mode */
1197 check_data(struct manager *mgr, struct sk_buff *skb)
1199 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1203 if (*debug & DEBUG_L2_CTRL)
1204 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1205 __func__, hh->prim, hh->id);
1206 if (test_bit(MGR_OPT_USER, &mgr->options))
1208 if (hh->prim != PH_DATA_IND)
1212 if (skb->data[0] & 3) /* EA0 and CR must be 0 */
1214 sapi = skb->data[0] >> 2;
1215 if (!(skb->data[1] & 1)) /* invalid EA1 */
1217 tei = skb->data[1] >> 1;
1218 if (tei > 63) /* not a fixed tei */
1220 if ((skb->data[2] & ~0x10) != SABME)
1222 /* We got a SABME for a fixed TEI */
1223 if (*debug & DEBUG_L2_CTRL)
1224 printk(KERN_DEBUG "%s: SABME sapi(%d) tei(%d)\n",
1225 __func__, sapi, tei);
1226 l2 = create_new_tei(mgr, tei, sapi);
1228 if (*debug & DEBUG_L2_CTRL)
1229 printk(KERN_DEBUG "%s: failed to create new tei\n",
1233 ret = l2->ch.send(&l2->ch, skb);
1238 delete_teimanager(struct mISDNchannel *ch)
1240 struct manager *mgr;
1241 struct layer2 *l2, *nl2;
1243 mgr = container_of(ch, struct manager, ch);
1244 /* not locked lock is taken in release tei */
1245 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1246 mutex_lock(&mgr->ch.st->lmutex);
1247 list_del(&l2->ch.list);
1248 mutex_unlock(&mgr->ch.st->lmutex);
1249 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1251 list_del(&mgr->ch.list);
1252 list_del(&mgr->bcast.list);
1253 skb_queue_purge(&mgr->sendq);
1258 mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1260 struct manager *mgr;
1263 mgr = container_of(ch, struct manager, ch);
1264 if (*debug & DEBUG_L2_CTRL)
1265 printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
1268 ret = create_teimgr(mgr, arg);
1271 ret = free_teimanager(mgr);
1273 case CONTROL_CHANNEL:
1274 ret = ctrl_teimanager(mgr, arg);
1277 ret = check_data(mgr, arg);
1284 mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
1286 struct manager *mgr = container_of(ch, struct manager, bcast);
1287 struct mISDNhead *hhc, *hh = mISDN_HEAD_P(skb);
1288 struct sk_buff *cskb = NULL;
1293 read_lock_irqsave(&mgr->lock, flags);
1294 list_for_each_entry(l2, &mgr->layer2, list) {
1295 if ((hh->id & MISDN_ID_SAPI_MASK) ==
1296 (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
1297 if (list_is_last(&l2->list, &mgr->layer2)) {
1302 cskb = skb_copy(skb, GFP_ATOMIC);
1305 hhc = mISDN_HEAD_P(cskb);
1306 /* save original header behind normal header */
1310 hhc->prim = DL_INTERN_MSG;
1311 hhc->id = l2->ch.nr;
1312 ret = ch->st->own.recv(&ch->st->own, cskb);
1314 if (*debug & DEBUG_SEND_ERR)
1316 "%s ch%d prim(%x) addr(%x)"
1318 __func__, l2->ch.nr,
1319 hh->prim, l2->ch.addr, ret);
1323 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
1324 __func__, ch->nr, ch->addr);
1330 read_unlock_irqrestore(&mgr->lock, flags);
1331 dev_kfree_skb(cskb);
1337 mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1344 create_teimanager(struct mISDNdevice *dev)
1346 struct manager *mgr;
1348 mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
1351 INIT_LIST_HEAD(&mgr->layer2);
1352 rwlock_init(&mgr->lock);
1353 skb_queue_head_init(&mgr->sendq);
1355 mgr->lastid = MISDN_ID_NONE;
1356 mgr->ch.send = mgr_send;
1357 mgr->ch.ctrl = mgr_ctrl;
1358 mgr->ch.st = dev->D.st;
1359 set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
1360 add_layer2(&mgr->ch, dev->D.st);
1361 mgr->bcast.send = mgr_bcast;
1362 mgr->bcast.ctrl = mgr_bcast_ctrl;
1363 mgr->bcast.st = dev->D.st;
1364 set_channel_address(&mgr->bcast, 0, GROUP_TEI);
1365 add_layer2(&mgr->bcast, dev->D.st);
1366 mgr->deact.debug = *debug & DEBUG_MANAGER;
1367 mgr->deact.userdata = mgr;
1368 mgr->deact.printdebug = da_debug;
1369 mgr->deact.fsm = &deactfsm;
1370 mgr->deact.state = ST_L1_DEACT;
1371 mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
1372 dev->teimgr = &mgr->ch;
1376 int TEIInit(u_int *deb)
1380 teifsmu.state_count = TEI_STATE_COUNT;
1381 teifsmu.event_count = TEI_EVENT_COUNT;
1382 teifsmu.strEvent = strTeiEvent;
1383 teifsmu.strState = strTeiState;
1384 res = mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
1387 teifsmn.state_count = TEI_STATE_COUNT;
1388 teifsmn.event_count = TEI_EVENT_COUNT;
1389 teifsmn.strEvent = strTeiEvent;
1390 teifsmn.strState = strTeiState;
1391 res = mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
1394 deactfsm.state_count = DEACT_STATE_COUNT;
1395 deactfsm.event_count = DEACT_EVENT_COUNT;
1396 deactfsm.strEvent = strDeactEvent;
1397 deactfsm.strState = strDeactState;
1398 res = mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
1404 mISDN_FsmFree(&teifsmn);
1406 mISDN_FsmFree(&teifsmu);
1413 mISDN_FsmFree(&teifsmu);
1414 mISDN_FsmFree(&teifsmn);
1415 mISDN_FsmFree(&deactfsm);