Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Author   Karsten Keil <kkeil@novell.com>
0005  *
0006  * Copyright 2008  by Karsten Keil <kkeil@novell.com>
0007  */
0008 #include "layer2.h"
0009 #include <linux/random.h>
0010 #include <linux/slab.h>
0011 #include "core.h"
0012 
0013 #define ID_REQUEST  1
0014 #define ID_ASSIGNED 2
0015 #define ID_DENIED   3
0016 #define ID_CHK_REQ  4
0017 #define ID_CHK_RES  5
0018 #define ID_REMOVE   6
0019 #define ID_VERIFY   7
0020 
0021 #define TEI_ENTITY_ID   0xf
0022 
0023 #define MGR_PH_ACTIVE   16
0024 #define MGR_PH_NOTREADY 17
0025 
0026 #define DATIMER_VAL 10000
0027 
0028 static  u_int   *debug;
0029 
0030 static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
0031 static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
0032 static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
0033 
0034 enum {
0035     ST_L1_DEACT,
0036     ST_L1_DEACT_PENDING,
0037     ST_L1_ACTIV,
0038 };
0039 #define DEACT_STATE_COUNT (ST_L1_ACTIV + 1)
0040 
0041 static char *strDeactState[] =
0042 {
0043     "ST_L1_DEACT",
0044     "ST_L1_DEACT_PENDING",
0045     "ST_L1_ACTIV",
0046 };
0047 
0048 enum {
0049     EV_ACTIVATE,
0050     EV_ACTIVATE_IND,
0051     EV_DEACTIVATE,
0052     EV_DEACTIVATE_IND,
0053     EV_UI,
0054     EV_DATIMER,
0055 };
0056 
0057 #define DEACT_EVENT_COUNT (EV_DATIMER + 1)
0058 
0059 static char *strDeactEvent[] =
0060 {
0061     "EV_ACTIVATE",
0062     "EV_ACTIVATE_IND",
0063     "EV_DEACTIVATE",
0064     "EV_DEACTIVATE_IND",
0065     "EV_UI",
0066     "EV_DATIMER",
0067 };
0068 
0069 static void
0070 da_debug(struct FsmInst *fi, char *fmt, ...)
0071 {
0072     struct manager  *mgr = fi->userdata;
0073     struct va_format vaf;
0074     va_list va;
0075 
0076     if (!(*debug & DEBUG_L2_TEIFSM))
0077         return;
0078 
0079     va_start(va, fmt);
0080 
0081     vaf.fmt = fmt;
0082     vaf.va = &va;
0083 
0084     printk(KERN_DEBUG "mgr(%d): %pV\n", mgr->ch.st->dev->id, &vaf);
0085 
0086     va_end(va);
0087 }
0088 
0089 static void
0090 da_activate(struct FsmInst *fi, int event, void *arg)
0091 {
0092     struct manager  *mgr = fi->userdata;
0093 
0094     if (fi->state == ST_L1_DEACT_PENDING)
0095         mISDN_FsmDelTimer(&mgr->datimer, 1);
0096     mISDN_FsmChangeState(fi, ST_L1_ACTIV);
0097 }
0098 
0099 static void
0100 da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
0101 {
0102     mISDN_FsmChangeState(fi, ST_L1_DEACT);
0103 }
0104 
0105 static void
0106 da_deactivate(struct FsmInst *fi, int event, void *arg)
0107 {
0108     struct manager  *mgr = fi->userdata;
0109     struct layer2   *l2;
0110     u_long      flags;
0111 
0112     read_lock_irqsave(&mgr->lock, flags);
0113     list_for_each_entry(l2, &mgr->layer2, list) {
0114         if (l2->l2m.state > ST_L2_4) {
0115             /* have still activ TEI */
0116             read_unlock_irqrestore(&mgr->lock, flags);
0117             return;
0118         }
0119     }
0120     read_unlock_irqrestore(&mgr->lock, flags);
0121     /* All TEI are inactiv */
0122     if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
0123         mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
0124                   NULL, 1);
0125         mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
0126     }
0127 }
0128 
0129 static void
0130 da_ui(struct FsmInst *fi, int event, void *arg)
0131 {
0132     struct manager  *mgr = fi->userdata;
0133 
0134     /* restart da timer */
0135     if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
0136         mISDN_FsmDelTimer(&mgr->datimer, 2);
0137         mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
0138                   NULL, 2);
0139     }
0140 }
0141 
0142 static void
0143 da_timer(struct FsmInst *fi, int event, void *arg)
0144 {
0145     struct manager  *mgr = fi->userdata;
0146     struct layer2   *l2;
0147     u_long      flags;
0148 
0149     /* check again */
0150     read_lock_irqsave(&mgr->lock, flags);
0151     list_for_each_entry(l2, &mgr->layer2, list) {
0152         if (l2->l2m.state > ST_L2_4) {
0153             /* have still activ TEI */
0154             read_unlock_irqrestore(&mgr->lock, flags);
0155             mISDN_FsmChangeState(fi, ST_L1_ACTIV);
0156             return;
0157         }
0158     }
0159     read_unlock_irqrestore(&mgr->lock, flags);
0160     /* All TEI are inactiv */
0161     mISDN_FsmChangeState(fi, ST_L1_DEACT);
0162     _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
0163             GFP_ATOMIC);
0164 }
0165 
0166 static struct FsmNode DeactFnList[] =
0167 {
0168     {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
0169     {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
0170     {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
0171     {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
0172     {ST_L1_DEACT_PENDING, EV_UI, da_ui},
0173     {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
0174 };
0175 
0176 enum {
0177     ST_TEI_NOP,
0178     ST_TEI_IDREQ,
0179     ST_TEI_IDVERIFY,
0180 };
0181 
0182 #define TEI_STATE_COUNT (ST_TEI_IDVERIFY + 1)
0183 
0184 static char *strTeiState[] =
0185 {
0186     "ST_TEI_NOP",
0187     "ST_TEI_IDREQ",
0188     "ST_TEI_IDVERIFY",
0189 };
0190 
0191 enum {
0192     EV_IDREQ,
0193     EV_ASSIGN,
0194     EV_ASSIGN_REQ,
0195     EV_DENIED,
0196     EV_CHKREQ,
0197     EV_CHKRESP,
0198     EV_REMOVE,
0199     EV_VERIFY,
0200     EV_TIMER,
0201 };
0202 
0203 #define TEI_EVENT_COUNT (EV_TIMER + 1)
0204 
0205 static char *strTeiEvent[] =
0206 {
0207     "EV_IDREQ",
0208     "EV_ASSIGN",
0209     "EV_ASSIGN_REQ",
0210     "EV_DENIED",
0211     "EV_CHKREQ",
0212     "EV_CHKRESP",
0213     "EV_REMOVE",
0214     "EV_VERIFY",
0215     "EV_TIMER",
0216 };
0217 
0218 static void
0219 tei_debug(struct FsmInst *fi, char *fmt, ...)
0220 {
0221     struct teimgr   *tm = fi->userdata;
0222     struct va_format vaf;
0223     va_list va;
0224 
0225     if (!(*debug & DEBUG_L2_TEIFSM))
0226         return;
0227 
0228     va_start(va, fmt);
0229 
0230     vaf.fmt = fmt;
0231     vaf.va = &va;
0232 
0233     printk(KERN_DEBUG "sapi(%d) tei(%d): %pV\n",
0234            tm->l2->sapi, tm->l2->tei, &vaf);
0235 
0236     va_end(va);
0237 }
0238 
0239 
0240 
0241 static int
0242 get_free_id(struct manager *mgr)
0243 {
0244     DECLARE_BITMAP(ids, 64) = { [0 ... BITS_TO_LONGS(64) - 1] = 0 };
0245     int     i;
0246     struct layer2   *l2;
0247 
0248     list_for_each_entry(l2, &mgr->layer2, list) {
0249         if (l2->ch.nr > 63) {
0250             printk(KERN_WARNING
0251                    "%s: more as 63 layer2 for one device\n",
0252                    __func__);
0253             return -EBUSY;
0254         }
0255         __set_bit(l2->ch.nr, ids);
0256     }
0257     i = find_next_zero_bit(ids, 64, 1);
0258     if (i < 64)
0259         return i;
0260     printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
0261            __func__);
0262     return -EBUSY;
0263 }
0264 
0265 static int
0266 get_free_tei(struct manager *mgr)
0267 {
0268     DECLARE_BITMAP(ids, 64) = { [0 ... BITS_TO_LONGS(64) - 1] = 0 };
0269     int     i;
0270     struct layer2   *l2;
0271 
0272     list_for_each_entry(l2, &mgr->layer2, list) {
0273         if (l2->ch.nr == 0)
0274             continue;
0275         if ((l2->ch.addr & 0xff) != 0)
0276             continue;
0277         i = l2->ch.addr >> 8;
0278         if (i < 64)
0279             continue;
0280         i -= 64;
0281 
0282         __set_bit(i, ids);
0283     }
0284     i = find_first_zero_bit(ids, 64);
0285     if (i < 64)
0286         return i + 64;
0287     printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
0288            __func__);
0289     return -1;
0290 }
0291 
0292 static void
0293 teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
0294 {
0295     struct sk_buff  *skb;
0296     struct mISDNhead *hh;
0297     int     err;
0298 
0299     skb = mI_alloc_skb(len, GFP_ATOMIC);
0300     if (!skb)
0301         return;
0302     hh = mISDN_HEAD_P(skb);
0303     hh->prim = prim;
0304     hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
0305     if (len)
0306         skb_put_data(skb, arg, len);
0307     err = mgr->up->send(mgr->up, skb);
0308     if (err) {
0309         printk(KERN_WARNING "%s: err=%d\n", __func__, err);
0310         dev_kfree_skb(skb);
0311     }
0312 }
0313 
0314 static u_int
0315 new_id(struct manager *mgr)
0316 {
0317     u_int   id;
0318 
0319     id = mgr->nextid++;
0320     if (id == 0x7fff)
0321         mgr->nextid = 1;
0322     id <<= 16;
0323     id |= GROUP_TEI << 8;
0324     id |= TEI_SAPI;
0325     return id;
0326 }
0327 
0328 static void
0329 do_send(struct manager *mgr)
0330 {
0331     if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
0332         return;
0333 
0334     if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
0335         struct sk_buff  *skb = skb_dequeue(&mgr->sendq);
0336 
0337         if (!skb) {
0338             test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
0339             return;
0340         }
0341         mgr->lastid = mISDN_HEAD_ID(skb);
0342         mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
0343         if (mgr->ch.recv(mgr->ch.peer, skb)) {
0344             dev_kfree_skb(skb);
0345             test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
0346             mgr->lastid = MISDN_ID_NONE;
0347         }
0348     }
0349 }
0350 
0351 static void
0352 do_ack(struct manager *mgr, u_int id)
0353 {
0354     if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
0355         if (id == mgr->lastid) {
0356             if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
0357                 struct sk_buff  *skb;
0358 
0359                 skb = skb_dequeue(&mgr->sendq);
0360                 if (skb) {
0361                     mgr->lastid = mISDN_HEAD_ID(skb);
0362                     if (!mgr->ch.recv(mgr->ch.peer, skb))
0363                         return;
0364                     dev_kfree_skb(skb);
0365                 }
0366             }
0367             mgr->lastid = MISDN_ID_NONE;
0368             test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
0369         }
0370     }
0371 }
0372 
0373 static void
0374 mgr_send_down(struct manager *mgr, struct sk_buff *skb)
0375 {
0376     skb_queue_tail(&mgr->sendq, skb);
0377     if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
0378         _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
0379                 NULL, GFP_KERNEL);
0380     } else {
0381         do_send(mgr);
0382     }
0383 }
0384 
0385 static int
0386 dl_unit_data(struct manager *mgr, struct sk_buff *skb)
0387 {
0388     if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
0389         return -EINVAL;
0390     if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
0391         _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
0392                 NULL, GFP_KERNEL);
0393     skb_push(skb, 3);
0394     skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
0395     skb->data[1] = 0xff; /* TEI 127 */
0396     skb->data[2] = UI;   /* UI frame */
0397     mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
0398     mISDN_HEAD_ID(skb) = new_id(mgr);
0399     skb_queue_tail(&mgr->sendq, skb);
0400     do_send(mgr);
0401     return 0;
0402 }
0403 
0404 static unsigned int
0405 random_ri(void)
0406 {
0407     u16 x;
0408 
0409     get_random_bytes(&x, sizeof(x));
0410     return x;
0411 }
0412 
0413 static struct layer2 *
0414 findtei(struct manager *mgr, int tei)
0415 {
0416     struct layer2   *l2;
0417     u_long      flags;
0418 
0419     read_lock_irqsave(&mgr->lock, flags);
0420     list_for_each_entry(l2, &mgr->layer2, list) {
0421         if ((l2->sapi == 0) && (l2->tei > 0) &&
0422             (l2->tei != GROUP_TEI) && (l2->tei == tei))
0423             goto done;
0424     }
0425     l2 = NULL;
0426 done:
0427     read_unlock_irqrestore(&mgr->lock, flags);
0428     return l2;
0429 }
0430 
0431 static void
0432 put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei)
0433 {
0434     struct sk_buff *skb;
0435     u_char bp[8];
0436 
0437     bp[0] = (TEI_SAPI << 2);
0438     if (test_bit(MGR_OPT_NETWORK, &mgr->options))
0439         bp[0] |= 2; /* CR:=1 for net command */
0440     bp[1] = (GROUP_TEI << 1) | 0x1;
0441     bp[2] = UI;
0442     bp[3] = TEI_ENTITY_ID;
0443     bp[4] = ri >> 8;
0444     bp[5] = ri & 0xff;
0445     bp[6] = m_id;
0446     bp[7] = ((tei << 1) & 0xff) | 1;
0447     skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC);
0448     if (!skb) {
0449         printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
0450         return;
0451     }
0452     mgr_send_down(mgr, skb);
0453 }
0454 
0455 static void
0456 tei_id_request(struct FsmInst *fi, int event, void *arg)
0457 {
0458     struct teimgr *tm = fi->userdata;
0459 
0460     if (tm->l2->tei != GROUP_TEI) {
0461         tm->tei_m.printdebug(&tm->tei_m,
0462                      "assign request for already assigned tei %d",
0463                      tm->l2->tei);
0464         return;
0465     }
0466     tm->ri = random_ri();
0467     if (*debug & DEBUG_L2_TEI)
0468         tm->tei_m.printdebug(&tm->tei_m,
0469                      "assign request ri %d", tm->ri);
0470     put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
0471     mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
0472     mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
0473     tm->nval = 3;
0474 }
0475 
0476 static void
0477 tei_id_assign(struct FsmInst *fi, int event, void *arg)
0478 {
0479     struct teimgr   *tm = fi->userdata;
0480     struct layer2   *l2;
0481     u_char *dp = arg;
0482     int ri, tei;
0483 
0484     ri = ((unsigned int) *dp++ << 8);
0485     ri += *dp++;
0486     dp++;
0487     tei = *dp >> 1;
0488     if (*debug & DEBUG_L2_TEI)
0489         tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
0490                      ri, tei);
0491     l2 = findtei(tm->mgr, tei);
0492     if (l2) {   /* same tei is in use */
0493         if (ri != l2->tm->ri) {
0494             tm->tei_m.printdebug(fi,
0495                          "possible duplicate assignment tei %d", tei);
0496             tei_l2(l2, MDL_ERROR_RSP, 0);
0497         }
0498     } else if (ri == tm->ri) {
0499         mISDN_FsmDelTimer(&tm->timer, 1);
0500         mISDN_FsmChangeState(fi, ST_TEI_NOP);
0501         tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
0502     }
0503 }
0504 
0505 static void
0506 tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
0507 {
0508     struct teimgr   *tm = fi->userdata;
0509     struct layer2   *l2;
0510     u_char *dp = arg;
0511     int tei, ri;
0512 
0513     ri = ((unsigned int) *dp++ << 8);
0514     ri += *dp++;
0515     dp++;
0516     tei = *dp >> 1;
0517     if (*debug & DEBUG_L2_TEI)
0518         tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
0519                      ri, tei);
0520     l2 = findtei(tm->mgr, tei);
0521     if (l2) {   /* same tei is in use */
0522         if (ri != l2->tm->ri) { /* and it wasn't our request */
0523             tm->tei_m.printdebug(fi,
0524                          "possible duplicate assignment tei %d", tei);
0525             mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
0526         }
0527     }
0528 }
0529 
0530 static void
0531 tei_id_denied(struct FsmInst *fi, int event, void *arg)
0532 {
0533     struct teimgr *tm = fi->userdata;
0534     u_char *dp = arg;
0535     int ri, tei;
0536 
0537     ri = ((unsigned int) *dp++ << 8);
0538     ri += *dp++;
0539     dp++;
0540     tei = *dp >> 1;
0541     if (*debug & DEBUG_L2_TEI)
0542         tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
0543                      ri, tei);
0544 }
0545 
0546 static void
0547 tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
0548 {
0549     struct teimgr *tm = fi->userdata;
0550     u_char *dp = arg;
0551     int tei;
0552 
0553     tei = *(dp + 3) >> 1;
0554     if (*debug & DEBUG_L2_TEI)
0555         tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
0556     if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
0557                        (tei == tm->l2->tei))) {
0558         mISDN_FsmDelTimer(&tm->timer, 4);
0559         mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
0560         put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
0561     }
0562 }
0563 
0564 static void
0565 tei_id_remove(struct FsmInst *fi, int event, void *arg)
0566 {
0567     struct teimgr *tm = fi->userdata;
0568     u_char *dp = arg;
0569     int tei;
0570 
0571     tei = *(dp + 3) >> 1;
0572     if (*debug & DEBUG_L2_TEI)
0573         tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
0574     if ((tm->l2->tei != GROUP_TEI) &&
0575         ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
0576         mISDN_FsmDelTimer(&tm->timer, 5);
0577         mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
0578         tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
0579     }
0580 }
0581 
0582 static void
0583 tei_id_verify(struct FsmInst *fi, int event, void *arg)
0584 {
0585     struct teimgr *tm = fi->userdata;
0586 
0587     if (*debug & DEBUG_L2_TEI)
0588         tm->tei_m.printdebug(fi, "id verify request for tei %d",
0589                      tm->l2->tei);
0590     put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
0591     mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
0592     mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
0593     tm->nval = 2;
0594 }
0595 
0596 static void
0597 tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
0598 {
0599     struct teimgr *tm = fi->userdata;
0600 
0601     if (--tm->nval) {
0602         tm->ri = random_ri();
0603         if (*debug & DEBUG_L2_TEI)
0604             tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
0605                          4 - tm->nval, tm->ri);
0606         put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
0607         mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
0608     } else {
0609         tm->tei_m.printdebug(fi, "assign req failed");
0610         tei_l2(tm->l2, MDL_ERROR_RSP, 0);
0611         mISDN_FsmChangeState(fi, ST_TEI_NOP);
0612     }
0613 }
0614 
0615 static void
0616 tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
0617 {
0618     struct teimgr *tm = fi->userdata;
0619 
0620     if (--tm->nval) {
0621         if (*debug & DEBUG_L2_TEI)
0622             tm->tei_m.printdebug(fi,
0623                          "id verify req(%d) for tei %d",
0624                          3 - tm->nval, tm->l2->tei);
0625         put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
0626         mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
0627     } else {
0628         tm->tei_m.printdebug(fi, "verify req for tei %d failed",
0629                      tm->l2->tei);
0630         tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
0631         mISDN_FsmChangeState(fi, ST_TEI_NOP);
0632     }
0633 }
0634 
0635 static struct FsmNode TeiFnListUser[] =
0636 {
0637     {ST_TEI_NOP, EV_IDREQ, tei_id_request},
0638     {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
0639     {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
0640     {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
0641     {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
0642     {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
0643     {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
0644     {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
0645     {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
0646     {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
0647     {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
0648 };
0649 
0650 static void
0651 tei_l2remove(struct layer2 *l2)
0652 {
0653     put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
0654     tei_l2(l2, MDL_REMOVE_REQ, 0);
0655     list_del(&l2->ch.list);
0656     l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
0657 }
0658 
0659 static void
0660 tei_assign_req(struct FsmInst *fi, int event, void *arg)
0661 {
0662     struct teimgr *tm = fi->userdata;
0663     u_char *dp = arg;
0664 
0665     if (tm->l2->tei == GROUP_TEI) {
0666         tm->tei_m.printdebug(&tm->tei_m,
0667                      "net tei assign request without tei");
0668         return;
0669     }
0670     tm->ri = ((unsigned int) *dp++ << 8);
0671     tm->ri += *dp++;
0672     if (*debug & DEBUG_L2_TEI)
0673         tm->tei_m.printdebug(&tm->tei_m,
0674                      "net assign request ri %d teim %d", tm->ri, *dp);
0675     put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
0676     mISDN_FsmChangeState(fi, ST_TEI_NOP);
0677 }
0678 
0679 static void
0680 tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
0681 {
0682     struct teimgr   *tm = fi->userdata;
0683 
0684     if (*debug & DEBUG_L2_TEI)
0685         tm->tei_m.printdebug(fi, "id check request for tei %d",
0686                      tm->l2->tei);
0687     tm->rcnt = 0;
0688     put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
0689     mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
0690     mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
0691     tm->nval = 2;
0692 }
0693 
0694 static void
0695 tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
0696 {
0697     struct teimgr *tm = fi->userdata;
0698     u_char *dp = arg;
0699     int tei;
0700 
0701     tei = dp[3] >> 1;
0702     if (*debug & DEBUG_L2_TEI)
0703         tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
0704     if (tei == tm->l2->tei)
0705         tm->rcnt++;
0706 }
0707 
0708 static void
0709 tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
0710 {
0711     struct teimgr *tm = fi->userdata;
0712     u_char *dp = arg;
0713     int tei;
0714 
0715     tei = dp[3] >> 1;
0716     if (*debug & DEBUG_L2_TEI)
0717         tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
0718                      tei, tm->l2->tei);
0719     if (tei == tm->l2->tei)
0720         tei_id_chk_req_net(fi, event, arg);
0721 }
0722 
0723 static void
0724 tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
0725 {
0726     struct teimgr *tm = fi->userdata;
0727 
0728     if (tm->rcnt == 1) {
0729         if (*debug & DEBUG_L2_TEI)
0730             tm->tei_m.printdebug(fi,
0731                          "check req for tei %d successful\n", tm->l2->tei);
0732         mISDN_FsmChangeState(fi, ST_TEI_NOP);
0733     } else if (tm->rcnt > 1) {
0734         /* duplicate assignment; remove */
0735         tei_l2remove(tm->l2);
0736     } else if (--tm->nval) {
0737         if (*debug & DEBUG_L2_TEI)
0738             tm->tei_m.printdebug(fi,
0739                          "id check req(%d) for tei %d",
0740                          3 - tm->nval, tm->l2->tei);
0741         put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
0742         mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
0743     } else {
0744         tm->tei_m.printdebug(fi, "check req for tei %d failed",
0745                      tm->l2->tei);
0746         mISDN_FsmChangeState(fi, ST_TEI_NOP);
0747         tei_l2remove(tm->l2);
0748     }
0749 }
0750 
0751 static struct FsmNode TeiFnListNet[] =
0752 {
0753     {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
0754     {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
0755     {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
0756     {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
0757     {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
0758 };
0759 
0760 static void
0761 tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
0762 {
0763     if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
0764         return;
0765     if (*debug & DEBUG_L2_TEI)
0766         tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
0767     if (mt == ID_ASSIGNED)
0768         mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
0769     else if (mt == ID_DENIED)
0770         mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
0771     else if (mt == ID_CHK_REQ)
0772         mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
0773     else if (mt == ID_REMOVE)
0774         mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
0775     else if (mt == ID_VERIFY)
0776         mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
0777     else if (mt == ID_CHK_RES)
0778         mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
0779 }
0780 
0781 static struct layer2 *
0782 create_new_tei(struct manager *mgr, int tei, int sapi)
0783 {
0784     unsigned long       opt = 0;
0785     unsigned long       flags;
0786     int         id;
0787     struct layer2       *l2;
0788     struct channel_req  rq;
0789 
0790     if (!mgr->up)
0791         return NULL;
0792     if ((tei >= 0) && (tei < 64))
0793         test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
0794     if (mgr->ch.st->dev->Dprotocols & ((1 << ISDN_P_TE_E1) |
0795         (1 << ISDN_P_NT_E1))) {
0796         test_and_set_bit(OPTION_L2_PMX, &opt);
0797         rq.protocol = ISDN_P_NT_E1;
0798     } else {
0799         rq.protocol = ISDN_P_NT_S0;
0800     }
0801     l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi);
0802     if (!l2) {
0803         printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
0804         return NULL;
0805     }
0806     l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
0807     if (!l2->tm) {
0808         kfree(l2);
0809         printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
0810         return NULL;
0811     }
0812     l2->tm->mgr = mgr;
0813     l2->tm->l2 = l2;
0814     l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
0815     l2->tm->tei_m.userdata = l2->tm;
0816     l2->tm->tei_m.printdebug = tei_debug;
0817     l2->tm->tei_m.fsm = &teifsmn;
0818     l2->tm->tei_m.state = ST_TEI_NOP;
0819     l2->tm->tval = 2000; /* T202  2 sec */
0820     mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
0821     write_lock_irqsave(&mgr->lock, flags);
0822     id = get_free_id(mgr);
0823     list_add_tail(&l2->list, &mgr->layer2);
0824     write_unlock_irqrestore(&mgr->lock, flags);
0825     if (id < 0) {
0826         l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
0827         printk(KERN_WARNING "%s:no free id\n", __func__);
0828         return NULL;
0829     } else {
0830         l2->ch.nr = id;
0831         __add_layer2(&l2->ch, mgr->ch.st);
0832         l2->ch.recv = mgr->ch.recv;
0833         l2->ch.peer = mgr->ch.peer;
0834         l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
0835         /* We need open here L1 for the manager as well (refcounting) */
0836         rq.adr.dev = mgr->ch.st->dev->id;
0837         id = mgr->ch.st->own.ctrl(&mgr->ch.st->own, OPEN_CHANNEL, &rq);
0838         if (id < 0) {
0839             printk(KERN_WARNING "%s: cannot open L1\n", __func__);
0840             l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
0841             l2 = NULL;
0842         }
0843     }
0844     return l2;
0845 }
0846 
0847 static void
0848 new_tei_req(struct manager *mgr, u_char *dp)
0849 {
0850     int     tei, ri;
0851     struct layer2   *l2;
0852 
0853     ri = dp[0] << 8;
0854     ri += dp[1];
0855     if (!mgr->up)
0856         goto denied;
0857     if (!(dp[3] & 1)) /* Extension bit != 1 */
0858         goto denied;
0859     if (dp[3] != 0xff)
0860         tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
0861     else
0862         tei = get_free_tei(mgr);
0863     if (tei < 0) {
0864         printk(KERN_WARNING "%s:No free tei\n", __func__);
0865         goto denied;
0866     }
0867     l2 = create_new_tei(mgr, tei, CTRL_SAPI);
0868     if (!l2)
0869         goto denied;
0870     else
0871         mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
0872     return;
0873 denied:
0874     put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
0875 }
0876 
0877 static int
0878 ph_data_ind(struct manager *mgr, struct sk_buff *skb)
0879 {
0880     int     ret = -EINVAL;
0881     struct layer2   *l2, *nl2;
0882     u_char      mt;
0883 
0884     if (skb->len < 8) {
0885         if (*debug  & DEBUG_L2_TEI)
0886             printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
0887                    __func__, skb->len);
0888         goto done;
0889     }
0890 
0891     if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
0892         goto done;
0893     if (skb->data[0] & 1) /* EA0 formal error */
0894         goto done;
0895     if (!(skb->data[1] & 1)) /* EA1 formal error */
0896         goto done;
0897     if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
0898         goto done;
0899     if ((skb->data[2] & 0xef) != UI) /* not UI */
0900         goto done;
0901     if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
0902         goto done;
0903     mt = skb->data[6];
0904     switch (mt) {
0905     case ID_REQUEST:
0906     case ID_CHK_RES:
0907     case ID_VERIFY:
0908         if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
0909             goto done;
0910         break;
0911     case ID_ASSIGNED:
0912     case ID_DENIED:
0913     case ID_CHK_REQ:
0914     case ID_REMOVE:
0915         if (test_bit(MGR_OPT_NETWORK, &mgr->options))
0916             goto done;
0917         break;
0918     default:
0919         goto done;
0920     }
0921     ret = 0;
0922     if (mt == ID_REQUEST) {
0923         new_tei_req(mgr, &skb->data[4]);
0924         goto done;
0925     }
0926     list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
0927         tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
0928     }
0929 done:
0930     return ret;
0931 }
0932 
0933 int
0934 l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
0935 {
0936     struct teimgr   *tm = l2->tm;
0937 
0938     if (test_bit(FLG_FIXED_TEI, &l2->flag))
0939         return 0;
0940     if (*debug & DEBUG_L2_TEI)
0941         printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
0942     switch (cmd) {
0943     case MDL_ASSIGN_IND:
0944         mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
0945         break;
0946     case MDL_ERROR_IND:
0947         if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
0948             mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
0949         if (test_bit(MGR_OPT_USER, &tm->mgr->options))
0950             mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
0951         break;
0952     case MDL_STATUS_UP_IND:
0953         if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
0954             mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
0955         break;
0956     case MDL_STATUS_DOWN_IND:
0957         if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
0958             mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
0959         break;
0960     case MDL_STATUS_UI_IND:
0961         if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
0962             mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
0963         break;
0964     }
0965     return 0;
0966 }
0967 
0968 void
0969 TEIrelease(struct layer2 *l2)
0970 {
0971     struct teimgr   *tm = l2->tm;
0972     u_long      flags;
0973 
0974     mISDN_FsmDelTimer(&tm->timer, 1);
0975     write_lock_irqsave(&tm->mgr->lock, flags);
0976     list_del(&l2->list);
0977     write_unlock_irqrestore(&tm->mgr->lock, flags);
0978     l2->tm = NULL;
0979     kfree(tm);
0980 }
0981 
0982 static int
0983 create_teimgr(struct manager *mgr, struct channel_req *crq)
0984 {
0985     struct layer2       *l2;
0986     unsigned long       opt = 0;
0987     unsigned long       flags;
0988     int         id;
0989     struct channel_req  l1rq;
0990 
0991     if (*debug & DEBUG_L2_TEI)
0992         printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
0993                __func__, dev_name(&mgr->ch.st->dev->dev),
0994                crq->protocol, crq->adr.dev, crq->adr.channel,
0995                crq->adr.sapi, crq->adr.tei);
0996     if (crq->adr.tei > GROUP_TEI)
0997         return -EINVAL;
0998     if (crq->adr.tei < 64)
0999         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))
1006             return -EINVAL;
1007         if (mgr->up) {
1008             printk(KERN_WARNING
1009                    "%s: only one network manager is allowed\n",
1010                    __func__);
1011             return -EBUSY;
1012         }
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 */
1018     } else {
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);
1023     }
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)) {
1029         mgr->up = crq->ch;
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);
1034         crq->ch = NULL;
1035         if (!list_empty(&mgr->layer2)) {
1036             read_lock_irqsave(&mgr->lock, flags);
1037             list_for_each_entry(l2, &mgr->layer2, list) {
1038                 l2->up = mgr->up;
1039                 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
1040             }
1041             read_unlock_irqrestore(&mgr->lock, flags);
1042         }
1043         return 0;
1044     }
1045     l2 = create_l2(crq->ch, crq->protocol, opt,
1046                crq->adr.tei, crq->adr.sapi);
1047     if (!l2)
1048         return -ENOMEM;
1049     l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
1050     if (!l2->tm) {
1051         kfree(l2);
1052         printk(KERN_ERR "kmalloc teimgr failed\n");
1053         return -ENOMEM;
1054     }
1055     l2->tm->mgr = mgr;
1056     l2->tm->l2 = l2;
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;
1066         else
1067             l1rq.protocol = ISDN_P_TE_S0;
1068     } else {
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;
1074         else
1075             l1rq.protocol = ISDN_P_NT_S0;
1076     }
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);
1082     if (id >= 0) {
1083         l2->ch.nr = id;
1084         l2->up->nr = id;
1085         crq->ch = &l2->ch;
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,
1088                       &l1rq);
1089     }
1090     if (id < 0)
1091         l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1092     return id;
1093 }
1094 
1095 static int
1096 mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
1097 {
1098     struct manager  *mgr;
1099     struct mISDNhead    *hh =  mISDN_HEAD_P(skb);
1100     int         ret = -EINVAL;
1101 
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);
1106     switch (hh->prim) {
1107     case PH_DATA_IND:
1108         mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
1109         ret = ph_data_ind(mgr, skb);
1110         break;
1111     case PH_DATA_CNF:
1112         do_ack(mgr, hh->id);
1113         ret = 0;
1114         break;
1115     case PH_ACTIVATE_IND:
1116         test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
1117         if (mgr->up)
1118             teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
1119         mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
1120         do_send(mgr);
1121         ret = 0;
1122         break;
1123     case PH_DEACTIVATE_IND:
1124         test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
1125         if (mgr->up)
1126             teiup_create(mgr, PH_DEACTIVATE_IND, 0, NULL);
1127         mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
1128         ret = 0;
1129         break;
1130     case DL_UNITDATA_REQ:
1131         return dl_unit_data(mgr, skb);
1132     }
1133     if (!ret)
1134         dev_kfree_skb(skb);
1135     return ret;
1136 }
1137 
1138 static int
1139 free_teimanager(struct manager *mgr)
1140 {
1141     struct layer2   *l2, *nl2;
1142 
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 */
1146         mgr->up = NULL;
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);
1154             }
1155             test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
1156         } else {
1157             list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1158                 l2->up = NULL;
1159             }
1160         }
1161     }
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);
1165     }
1166     mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
1167     return 0;
1168 }
1169 
1170 static int
1171 ctrl_teimanager(struct manager *mgr, void *arg)
1172 {
1173     /* currently we only have one option */
1174     unsigned int *val = (unsigned int *)arg;
1175 
1176     switch (val[0]) {
1177     case IMCLEAR_L2:
1178         if (val[1])
1179             test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
1180         else
1181             test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
1182         break;
1183     case IMHOLD_L1:
1184         if (val[1])
1185             test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
1186         else
1187             test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1188         break;
1189     default:
1190         return -EINVAL;
1191     }
1192     return 0;
1193 }
1194 
1195 /* This function does create a L2 for fixed TEI in NT Mode */
1196 static int
1197 check_data(struct manager *mgr, struct sk_buff *skb)
1198 {
1199     struct mISDNhead    *hh =  mISDN_HEAD_P(skb);
1200     int         ret, tei, sapi;
1201     struct layer2       *l2;
1202 
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))
1207         return -ENOTCONN;
1208     if (hh->prim != PH_DATA_IND)
1209         return -ENOTCONN;
1210     if (skb->len != 3)
1211         return -ENOTCONN;
1212     if (skb->data[0] & 3) /* EA0 and CR must be  0 */
1213         return -EINVAL;
1214     sapi = skb->data[0] >> 2;
1215     if (!(skb->data[1] & 1)) /* invalid EA1 */
1216         return -EINVAL;
1217     tei = skb->data[1] >> 1;
1218     if (tei > 63) /* not a fixed tei */
1219         return -ENOTCONN;
1220     if ((skb->data[2] & ~0x10) != SABME)
1221         return -ENOTCONN;
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);
1227     if (!l2) {
1228         if (*debug & DEBUG_L2_CTRL)
1229             printk(KERN_DEBUG "%s: failed to create new tei\n",
1230                    __func__);
1231         return -ENOMEM;
1232     }
1233     ret = l2->ch.send(&l2->ch, skb);
1234     return ret;
1235 }
1236 
1237 void
1238 delete_teimanager(struct mISDNchannel *ch)
1239 {
1240     struct manager  *mgr;
1241     struct layer2   *l2, *nl2;
1242 
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);
1250     }
1251     list_del(&mgr->ch.list);
1252     list_del(&mgr->bcast.list);
1253     skb_queue_purge(&mgr->sendq);
1254     kfree(mgr);
1255 }
1256 
1257 static int
1258 mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1259 {
1260     struct manager  *mgr;
1261     int     ret = -EINVAL;
1262 
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);
1266     switch (cmd) {
1267     case OPEN_CHANNEL:
1268         ret = create_teimgr(mgr, arg);
1269         break;
1270     case CLOSE_CHANNEL:
1271         ret = free_teimanager(mgr);
1272         break;
1273     case CONTROL_CHANNEL:
1274         ret = ctrl_teimanager(mgr, arg);
1275         break;
1276     case CHECK_DATA:
1277         ret = check_data(mgr, arg);
1278         break;
1279     }
1280     return ret;
1281 }
1282 
1283 static int
1284 mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
1285 {
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;
1289     struct layer2       *l2;
1290     u_long          flags;
1291     int         ret;
1292 
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)) {
1298                 cskb = skb;
1299                 skb = NULL;
1300             } else {
1301                 if (!cskb)
1302                     cskb = skb_copy(skb, GFP_ATOMIC);
1303             }
1304             if (cskb) {
1305                 hhc = mISDN_HEAD_P(cskb);
1306                 /* save original header behind normal header */
1307                 hhc++;
1308                 *hhc = *hh;
1309                 hhc--;
1310                 hhc->prim = DL_INTERN_MSG;
1311                 hhc->id = l2->ch.nr;
1312                 ret = ch->st->own.recv(&ch->st->own, cskb);
1313                 if (ret) {
1314                     if (*debug & DEBUG_SEND_ERR)
1315                         printk(KERN_DEBUG
1316                                "%s ch%d prim(%x) addr(%x)"
1317                                " err %d\n",
1318                                __func__, l2->ch.nr,
1319                                hh->prim, l2->ch.addr, ret);
1320                 } else
1321                     cskb = NULL;
1322             } else {
1323                 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
1324                        __func__, ch->nr, ch->addr);
1325                 goto out;
1326             }
1327         }
1328     }
1329 out:
1330     read_unlock_irqrestore(&mgr->lock, flags);
1331     dev_kfree_skb(cskb);
1332     dev_kfree_skb(skb);
1333     return 0;
1334 }
1335 
1336 static int
1337 mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1338 {
1339 
1340     return -EINVAL;
1341 }
1342 
1343 int
1344 create_teimanager(struct mISDNdevice *dev)
1345 {
1346     struct manager *mgr;
1347 
1348     mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
1349     if (!mgr)
1350         return -ENOMEM;
1351     INIT_LIST_HEAD(&mgr->layer2);
1352     rwlock_init(&mgr->lock);
1353     skb_queue_head_init(&mgr->sendq);
1354     mgr->nextid = 1;
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;
1373     return 0;
1374 }
1375 
1376 int TEIInit(u_int *deb)
1377 {
1378     int res;
1379     debug = 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));
1385     if (res)
1386         goto error;
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));
1392     if (res)
1393         goto error_smn;
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));
1399     if (res)
1400         goto error_deact;
1401     return 0;
1402 
1403 error_deact:
1404     mISDN_FsmFree(&teifsmn);
1405 error_smn:
1406     mISDN_FsmFree(&teifsmu);
1407 error:
1408     return res;
1409 }
1410 
1411 void TEIFree(void)
1412 {
1413     mISDN_FsmFree(&teifsmu);
1414     mISDN_FsmFree(&teifsmn);
1415     mISDN_FsmFree(&deactfsm);
1416 }