tei.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Author Karsten Keil <[email protected]>
  5. *
  6. * Copyright 2008 by Karsten Keil <[email protected]>
  7. */
  8. #include "layer2.h"
  9. #include <linux/random.h>
  10. #include <linux/slab.h>
  11. #include "core.h"
  12. #define ID_REQUEST 1
  13. #define ID_ASSIGNED 2
  14. #define ID_DENIED 3
  15. #define ID_CHK_REQ 4
  16. #define ID_CHK_RES 5
  17. #define ID_REMOVE 6
  18. #define ID_VERIFY 7
  19. #define TEI_ENTITY_ID 0xf
  20. #define MGR_PH_ACTIVE 16
  21. #define MGR_PH_NOTREADY 17
  22. #define DATIMER_VAL 10000
  23. static u_int *debug;
  24. static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
  25. static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
  26. static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
  27. enum {
  28. ST_L1_DEACT,
  29. ST_L1_DEACT_PENDING,
  30. ST_L1_ACTIV,
  31. };
  32. #define DEACT_STATE_COUNT (ST_L1_ACTIV + 1)
  33. static char *strDeactState[] =
  34. {
  35. "ST_L1_DEACT",
  36. "ST_L1_DEACT_PENDING",
  37. "ST_L1_ACTIV",
  38. };
  39. enum {
  40. EV_ACTIVATE,
  41. EV_ACTIVATE_IND,
  42. EV_DEACTIVATE,
  43. EV_DEACTIVATE_IND,
  44. EV_UI,
  45. EV_DATIMER,
  46. };
  47. #define DEACT_EVENT_COUNT (EV_DATIMER + 1)
  48. static char *strDeactEvent[] =
  49. {
  50. "EV_ACTIVATE",
  51. "EV_ACTIVATE_IND",
  52. "EV_DEACTIVATE",
  53. "EV_DEACTIVATE_IND",
  54. "EV_UI",
  55. "EV_DATIMER",
  56. };
  57. static void
  58. da_debug(struct FsmInst *fi, char *fmt, ...)
  59. {
  60. struct manager *mgr = fi->userdata;
  61. struct va_format vaf;
  62. va_list va;
  63. if (!(*debug & DEBUG_L2_TEIFSM))
  64. return;
  65. va_start(va, fmt);
  66. vaf.fmt = fmt;
  67. vaf.va = &va;
  68. printk(KERN_DEBUG "mgr(%d): %pV\n", mgr->ch.st->dev->id, &vaf);
  69. va_end(va);
  70. }
  71. static void
  72. da_activate(struct FsmInst *fi, int event, void *arg)
  73. {
  74. struct manager *mgr = fi->userdata;
  75. if (fi->state == ST_L1_DEACT_PENDING)
  76. mISDN_FsmDelTimer(&mgr->datimer, 1);
  77. mISDN_FsmChangeState(fi, ST_L1_ACTIV);
  78. }
  79. static void
  80. da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
  81. {
  82. mISDN_FsmChangeState(fi, ST_L1_DEACT);
  83. }
  84. static void
  85. da_deactivate(struct FsmInst *fi, int event, void *arg)
  86. {
  87. struct manager *mgr = fi->userdata;
  88. struct layer2 *l2;
  89. u_long flags;
  90. read_lock_irqsave(&mgr->lock, flags);
  91. list_for_each_entry(l2, &mgr->layer2, list) {
  92. if (l2->l2m.state > ST_L2_4) {
  93. /* have still activ TEI */
  94. read_unlock_irqrestore(&mgr->lock, flags);
  95. return;
  96. }
  97. }
  98. read_unlock_irqrestore(&mgr->lock, flags);
  99. /* All TEI are inactiv */
  100. if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
  101. mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
  102. NULL, 1);
  103. mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
  104. }
  105. }
  106. static void
  107. da_ui(struct FsmInst *fi, int event, void *arg)
  108. {
  109. struct manager *mgr = fi->userdata;
  110. /* restart da timer */
  111. if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
  112. mISDN_FsmDelTimer(&mgr->datimer, 2);
  113. mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
  114. NULL, 2);
  115. }
  116. }
  117. static void
  118. da_timer(struct FsmInst *fi, int event, void *arg)
  119. {
  120. struct manager *mgr = fi->userdata;
  121. struct layer2 *l2;
  122. u_long flags;
  123. /* check again */
  124. read_lock_irqsave(&mgr->lock, flags);
  125. list_for_each_entry(l2, &mgr->layer2, list) {
  126. if (l2->l2m.state > ST_L2_4) {
  127. /* have still activ TEI */
  128. read_unlock_irqrestore(&mgr->lock, flags);
  129. mISDN_FsmChangeState(fi, ST_L1_ACTIV);
  130. return;
  131. }
  132. }
  133. read_unlock_irqrestore(&mgr->lock, flags);
  134. /* All TEI are inactiv */
  135. mISDN_FsmChangeState(fi, ST_L1_DEACT);
  136. _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
  137. GFP_ATOMIC);
  138. }
  139. static struct FsmNode DeactFnList[] =
  140. {
  141. {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
  142. {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
  143. {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
  144. {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
  145. {ST_L1_DEACT_PENDING, EV_UI, da_ui},
  146. {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
  147. };
  148. enum {
  149. ST_TEI_NOP,
  150. ST_TEI_IDREQ,
  151. ST_TEI_IDVERIFY,
  152. };
  153. #define TEI_STATE_COUNT (ST_TEI_IDVERIFY + 1)
  154. static char *strTeiState[] =
  155. {
  156. "ST_TEI_NOP",
  157. "ST_TEI_IDREQ",
  158. "ST_TEI_IDVERIFY",
  159. };
  160. enum {
  161. EV_IDREQ,
  162. EV_ASSIGN,
  163. EV_ASSIGN_REQ,
  164. EV_DENIED,
  165. EV_CHKREQ,
  166. EV_CHKRESP,
  167. EV_REMOVE,
  168. EV_VERIFY,
  169. EV_TIMER,
  170. };
  171. #define TEI_EVENT_COUNT (EV_TIMER + 1)
  172. static char *strTeiEvent[] =
  173. {
  174. "EV_IDREQ",
  175. "EV_ASSIGN",
  176. "EV_ASSIGN_REQ",
  177. "EV_DENIED",
  178. "EV_CHKREQ",
  179. "EV_CHKRESP",
  180. "EV_REMOVE",
  181. "EV_VERIFY",
  182. "EV_TIMER",
  183. };
  184. static void
  185. tei_debug(struct FsmInst *fi, char *fmt, ...)
  186. {
  187. struct teimgr *tm = fi->userdata;
  188. struct va_format vaf;
  189. va_list va;
  190. if (!(*debug & DEBUG_L2_TEIFSM))
  191. return;
  192. va_start(va, fmt);
  193. vaf.fmt = fmt;
  194. vaf.va = &va;
  195. printk(KERN_DEBUG "sapi(%d) tei(%d): %pV\n",
  196. tm->l2->sapi, tm->l2->tei, &vaf);
  197. va_end(va);
  198. }
  199. static int
  200. get_free_id(struct manager *mgr)
  201. {
  202. DECLARE_BITMAP(ids, 64) = { [0 ... BITS_TO_LONGS(64) - 1] = 0 };
  203. int i;
  204. struct layer2 *l2;
  205. list_for_each_entry(l2, &mgr->layer2, list) {
  206. if (l2->ch.nr > 63) {
  207. printk(KERN_WARNING
  208. "%s: more as 63 layer2 for one device\n",
  209. __func__);
  210. return -EBUSY;
  211. }
  212. __set_bit(l2->ch.nr, ids);
  213. }
  214. i = find_next_zero_bit(ids, 64, 1);
  215. if (i < 64)
  216. return i;
  217. printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
  218. __func__);
  219. return -EBUSY;
  220. }
  221. static int
  222. get_free_tei(struct manager *mgr)
  223. {
  224. DECLARE_BITMAP(ids, 64) = { [0 ... BITS_TO_LONGS(64) - 1] = 0 };
  225. int i;
  226. struct layer2 *l2;
  227. list_for_each_entry(l2, &mgr->layer2, list) {
  228. if (l2->ch.nr == 0)
  229. continue;
  230. if ((l2->ch.addr & 0xff) != 0)
  231. continue;
  232. i = l2->ch.addr >> 8;
  233. if (i < 64)
  234. continue;
  235. i -= 64;
  236. __set_bit(i, ids);
  237. }
  238. i = find_first_zero_bit(ids, 64);
  239. if (i < 64)
  240. return i + 64;
  241. printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
  242. __func__);
  243. return -1;
  244. }
  245. static void
  246. teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
  247. {
  248. struct sk_buff *skb;
  249. struct mISDNhead *hh;
  250. int err;
  251. skb = mI_alloc_skb(len, GFP_ATOMIC);
  252. if (!skb)
  253. return;
  254. hh = mISDN_HEAD_P(skb);
  255. hh->prim = prim;
  256. hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
  257. if (len)
  258. skb_put_data(skb, arg, len);
  259. err = mgr->up->send(mgr->up, skb);
  260. if (err) {
  261. printk(KERN_WARNING "%s: err=%d\n", __func__, err);
  262. dev_kfree_skb(skb);
  263. }
  264. }
  265. static u_int
  266. new_id(struct manager *mgr)
  267. {
  268. u_int id;
  269. id = mgr->nextid++;
  270. if (id == 0x7fff)
  271. mgr->nextid = 1;
  272. id <<= 16;
  273. id |= GROUP_TEI << 8;
  274. id |= TEI_SAPI;
  275. return id;
  276. }
  277. static void
  278. do_send(struct manager *mgr)
  279. {
  280. if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
  281. return;
  282. if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
  283. struct sk_buff *skb = skb_dequeue(&mgr->sendq);
  284. if (!skb) {
  285. test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
  286. return;
  287. }
  288. mgr->lastid = mISDN_HEAD_ID(skb);
  289. mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
  290. if (mgr->ch.recv(mgr->ch.peer, skb)) {
  291. dev_kfree_skb(skb);
  292. test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
  293. mgr->lastid = MISDN_ID_NONE;
  294. }
  295. }
  296. }
  297. static void
  298. do_ack(struct manager *mgr, u_int id)
  299. {
  300. if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
  301. if (id == mgr->lastid) {
  302. if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
  303. struct sk_buff *skb;
  304. skb = skb_dequeue(&mgr->sendq);
  305. if (skb) {
  306. mgr->lastid = mISDN_HEAD_ID(skb);
  307. if (!mgr->ch.recv(mgr->ch.peer, skb))
  308. return;
  309. dev_kfree_skb(skb);
  310. }
  311. }
  312. mgr->lastid = MISDN_ID_NONE;
  313. test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
  314. }
  315. }
  316. }
  317. static void
  318. mgr_send_down(struct manager *mgr, struct sk_buff *skb)
  319. {
  320. skb_queue_tail(&mgr->sendq, skb);
  321. if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
  322. _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
  323. NULL, GFP_KERNEL);
  324. } else {
  325. do_send(mgr);
  326. }
  327. }
  328. static int
  329. dl_unit_data(struct manager *mgr, struct sk_buff *skb)
  330. {
  331. if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
  332. return -EINVAL;
  333. if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
  334. _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
  335. NULL, GFP_KERNEL);
  336. skb_push(skb, 3);
  337. skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
  338. skb->data[1] = 0xff; /* TEI 127 */
  339. skb->data[2] = UI; /* UI frame */
  340. mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
  341. mISDN_HEAD_ID(skb) = new_id(mgr);
  342. skb_queue_tail(&mgr->sendq, skb);
  343. do_send(mgr);
  344. return 0;
  345. }
  346. static unsigned int
  347. random_ri(void)
  348. {
  349. u16 x;
  350. get_random_bytes(&x, sizeof(x));
  351. return x;
  352. }
  353. static struct layer2 *
  354. findtei(struct manager *mgr, int tei)
  355. {
  356. struct layer2 *l2;
  357. u_long flags;
  358. read_lock_irqsave(&mgr->lock, flags);
  359. list_for_each_entry(l2, &mgr->layer2, list) {
  360. if ((l2->sapi == 0) && (l2->tei > 0) &&
  361. (l2->tei != GROUP_TEI) && (l2->tei == tei))
  362. goto done;
  363. }
  364. l2 = NULL;
  365. done:
  366. read_unlock_irqrestore(&mgr->lock, flags);
  367. return l2;
  368. }
  369. static void
  370. put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei)
  371. {
  372. struct sk_buff *skb;
  373. u_char bp[8];
  374. bp[0] = (TEI_SAPI << 2);
  375. if (test_bit(MGR_OPT_NETWORK, &mgr->options))
  376. bp[0] |= 2; /* CR:=1 for net command */
  377. bp[1] = (GROUP_TEI << 1) | 0x1;
  378. bp[2] = UI;
  379. bp[3] = TEI_ENTITY_ID;
  380. bp[4] = ri >> 8;
  381. bp[5] = ri & 0xff;
  382. bp[6] = m_id;
  383. bp[7] = ((tei << 1) & 0xff) | 1;
  384. skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC);
  385. if (!skb) {
  386. printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
  387. return;
  388. }
  389. mgr_send_down(mgr, skb);
  390. }
  391. static void
  392. tei_id_request(struct FsmInst *fi, int event, void *arg)
  393. {
  394. struct teimgr *tm = fi->userdata;
  395. if (tm->l2->tei != GROUP_TEI) {
  396. tm->tei_m.printdebug(&tm->tei_m,
  397. "assign request for already assigned tei %d",
  398. tm->l2->tei);
  399. return;
  400. }
  401. tm->ri = random_ri();
  402. if (*debug & DEBUG_L2_TEI)
  403. tm->tei_m.printdebug(&tm->tei_m,
  404. "assign request ri %d", tm->ri);
  405. put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
  406. mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
  407. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
  408. tm->nval = 3;
  409. }
  410. static void
  411. tei_id_assign(struct FsmInst *fi, int event, void *arg)
  412. {
  413. struct teimgr *tm = fi->userdata;
  414. struct layer2 *l2;
  415. u_char *dp = arg;
  416. int ri, tei;
  417. ri = ((unsigned int) *dp++ << 8);
  418. ri += *dp++;
  419. dp++;
  420. tei = *dp >> 1;
  421. if (*debug & DEBUG_L2_TEI)
  422. tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
  423. ri, tei);
  424. l2 = findtei(tm->mgr, tei);
  425. if (l2) { /* same tei is in use */
  426. if (ri != l2->tm->ri) {
  427. tm->tei_m.printdebug(fi,
  428. "possible duplicate assignment tei %d", tei);
  429. tei_l2(l2, MDL_ERROR_RSP, 0);
  430. }
  431. } else if (ri == tm->ri) {
  432. mISDN_FsmDelTimer(&tm->timer, 1);
  433. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  434. tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
  435. }
  436. }
  437. static void
  438. tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
  439. {
  440. struct teimgr *tm = fi->userdata;
  441. struct layer2 *l2;
  442. u_char *dp = arg;
  443. int tei, ri;
  444. ri = ((unsigned int) *dp++ << 8);
  445. ri += *dp++;
  446. dp++;
  447. tei = *dp >> 1;
  448. if (*debug & DEBUG_L2_TEI)
  449. tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
  450. ri, tei);
  451. l2 = findtei(tm->mgr, tei);
  452. if (l2) { /* same tei is in use */
  453. if (ri != l2->tm->ri) { /* and it wasn't our request */
  454. tm->tei_m.printdebug(fi,
  455. "possible duplicate assignment tei %d", tei);
  456. mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
  457. }
  458. }
  459. }
  460. static void
  461. tei_id_denied(struct FsmInst *fi, int event, void *arg)
  462. {
  463. struct teimgr *tm = fi->userdata;
  464. u_char *dp = arg;
  465. int ri, tei;
  466. ri = ((unsigned int) *dp++ << 8);
  467. ri += *dp++;
  468. dp++;
  469. tei = *dp >> 1;
  470. if (*debug & DEBUG_L2_TEI)
  471. tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
  472. ri, tei);
  473. }
  474. static void
  475. tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
  476. {
  477. struct teimgr *tm = fi->userdata;
  478. u_char *dp = arg;
  479. int tei;
  480. tei = *(dp + 3) >> 1;
  481. if (*debug & DEBUG_L2_TEI)
  482. tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
  483. if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
  484. (tei == tm->l2->tei))) {
  485. mISDN_FsmDelTimer(&tm->timer, 4);
  486. mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
  487. put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
  488. }
  489. }
  490. static void
  491. tei_id_remove(struct FsmInst *fi, int event, void *arg)
  492. {
  493. struct teimgr *tm = fi->userdata;
  494. u_char *dp = arg;
  495. int tei;
  496. tei = *(dp + 3) >> 1;
  497. if (*debug & DEBUG_L2_TEI)
  498. tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
  499. if ((tm->l2->tei != GROUP_TEI) &&
  500. ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
  501. mISDN_FsmDelTimer(&tm->timer, 5);
  502. mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
  503. tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
  504. }
  505. }
  506. static void
  507. tei_id_verify(struct FsmInst *fi, int event, void *arg)
  508. {
  509. struct teimgr *tm = fi->userdata;
  510. if (*debug & DEBUG_L2_TEI)
  511. tm->tei_m.printdebug(fi, "id verify request for tei %d",
  512. tm->l2->tei);
  513. put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
  514. mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
  515. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
  516. tm->nval = 2;
  517. }
  518. static void
  519. tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
  520. {
  521. struct teimgr *tm = fi->userdata;
  522. if (--tm->nval) {
  523. tm->ri = random_ri();
  524. if (*debug & DEBUG_L2_TEI)
  525. tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
  526. 4 - tm->nval, tm->ri);
  527. put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
  528. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
  529. } else {
  530. tm->tei_m.printdebug(fi, "assign req failed");
  531. tei_l2(tm->l2, MDL_ERROR_RSP, 0);
  532. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  533. }
  534. }
  535. static void
  536. tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
  537. {
  538. struct teimgr *tm = fi->userdata;
  539. if (--tm->nval) {
  540. if (*debug & DEBUG_L2_TEI)
  541. tm->tei_m.printdebug(fi,
  542. "id verify req(%d) for tei %d",
  543. 3 - tm->nval, tm->l2->tei);
  544. put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
  545. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
  546. } else {
  547. tm->tei_m.printdebug(fi, "verify req for tei %d failed",
  548. tm->l2->tei);
  549. tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
  550. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  551. }
  552. }
  553. static struct FsmNode TeiFnListUser[] =
  554. {
  555. {ST_TEI_NOP, EV_IDREQ, tei_id_request},
  556. {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
  557. {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
  558. {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
  559. {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
  560. {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
  561. {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
  562. {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
  563. {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
  564. {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
  565. {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
  566. };
  567. static void
  568. tei_l2remove(struct layer2 *l2)
  569. {
  570. put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
  571. tei_l2(l2, MDL_REMOVE_REQ, 0);
  572. list_del(&l2->ch.list);
  573. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  574. }
  575. static void
  576. tei_assign_req(struct FsmInst *fi, int event, void *arg)
  577. {
  578. struct teimgr *tm = fi->userdata;
  579. u_char *dp = arg;
  580. if (tm->l2->tei == GROUP_TEI) {
  581. tm->tei_m.printdebug(&tm->tei_m,
  582. "net tei assign request without tei");
  583. return;
  584. }
  585. tm->ri = ((unsigned int) *dp++ << 8);
  586. tm->ri += *dp++;
  587. if (*debug & DEBUG_L2_TEI)
  588. tm->tei_m.printdebug(&tm->tei_m,
  589. "net assign request ri %d teim %d", tm->ri, *dp);
  590. put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
  591. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  592. }
  593. static void
  594. tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
  595. {
  596. struct teimgr *tm = fi->userdata;
  597. if (*debug & DEBUG_L2_TEI)
  598. tm->tei_m.printdebug(fi, "id check request for tei %d",
  599. tm->l2->tei);
  600. tm->rcnt = 0;
  601. put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
  602. mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
  603. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
  604. tm->nval = 2;
  605. }
  606. static void
  607. tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
  608. {
  609. struct teimgr *tm = fi->userdata;
  610. u_char *dp = arg;
  611. int tei;
  612. tei = dp[3] >> 1;
  613. if (*debug & DEBUG_L2_TEI)
  614. tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
  615. if (tei == tm->l2->tei)
  616. tm->rcnt++;
  617. }
  618. static void
  619. tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
  620. {
  621. struct teimgr *tm = fi->userdata;
  622. u_char *dp = arg;
  623. int tei;
  624. tei = dp[3] >> 1;
  625. if (*debug & DEBUG_L2_TEI)
  626. tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
  627. tei, tm->l2->tei);
  628. if (tei == tm->l2->tei)
  629. tei_id_chk_req_net(fi, event, arg);
  630. }
  631. static void
  632. tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
  633. {
  634. struct teimgr *tm = fi->userdata;
  635. if (tm->rcnt == 1) {
  636. if (*debug & DEBUG_L2_TEI)
  637. tm->tei_m.printdebug(fi,
  638. "check req for tei %d successful\n", tm->l2->tei);
  639. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  640. } else if (tm->rcnt > 1) {
  641. /* duplicate assignment; remove */
  642. tei_l2remove(tm->l2);
  643. } else if (--tm->nval) {
  644. if (*debug & DEBUG_L2_TEI)
  645. tm->tei_m.printdebug(fi,
  646. "id check req(%d) for tei %d",
  647. 3 - tm->nval, tm->l2->tei);
  648. put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
  649. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
  650. } else {
  651. tm->tei_m.printdebug(fi, "check req for tei %d failed",
  652. tm->l2->tei);
  653. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  654. tei_l2remove(tm->l2);
  655. }
  656. }
  657. static struct FsmNode TeiFnListNet[] =
  658. {
  659. {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
  660. {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
  661. {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
  662. {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
  663. {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
  664. };
  665. static void
  666. tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
  667. {
  668. if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
  669. return;
  670. if (*debug & DEBUG_L2_TEI)
  671. tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
  672. if (mt == ID_ASSIGNED)
  673. mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
  674. else if (mt == ID_DENIED)
  675. mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
  676. else if (mt == ID_CHK_REQ)
  677. mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
  678. else if (mt == ID_REMOVE)
  679. mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
  680. else if (mt == ID_VERIFY)
  681. mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
  682. else if (mt == ID_CHK_RES)
  683. mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
  684. }
  685. static struct layer2 *
  686. create_new_tei(struct manager *mgr, int tei, int sapi)
  687. {
  688. unsigned long opt = 0;
  689. unsigned long flags;
  690. int id;
  691. struct layer2 *l2;
  692. struct channel_req rq;
  693. if (!mgr->up)
  694. return NULL;
  695. if ((tei >= 0) && (tei < 64))
  696. test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
  697. if (mgr->ch.st->dev->Dprotocols & ((1 << ISDN_P_TE_E1) |
  698. (1 << ISDN_P_NT_E1))) {
  699. test_and_set_bit(OPTION_L2_PMX, &opt);
  700. rq.protocol = ISDN_P_NT_E1;
  701. } else {
  702. rq.protocol = ISDN_P_NT_S0;
  703. }
  704. l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi);
  705. if (!l2) {
  706. printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
  707. return NULL;
  708. }
  709. l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
  710. if (!l2->tm) {
  711. kfree(l2);
  712. printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
  713. return NULL;
  714. }
  715. l2->tm->mgr = mgr;
  716. l2->tm->l2 = l2;
  717. l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
  718. l2->tm->tei_m.userdata = l2->tm;
  719. l2->tm->tei_m.printdebug = tei_debug;
  720. l2->tm->tei_m.fsm = &teifsmn;
  721. l2->tm->tei_m.state = ST_TEI_NOP;
  722. l2->tm->tval = 2000; /* T202 2 sec */
  723. mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
  724. write_lock_irqsave(&mgr->lock, flags);
  725. id = get_free_id(mgr);
  726. list_add_tail(&l2->list, &mgr->layer2);
  727. write_unlock_irqrestore(&mgr->lock, flags);
  728. if (id < 0) {
  729. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  730. printk(KERN_WARNING "%s:no free id\n", __func__);
  731. return NULL;
  732. } else {
  733. l2->ch.nr = id;
  734. __add_layer2(&l2->ch, mgr->ch.st);
  735. l2->ch.recv = mgr->ch.recv;
  736. l2->ch.peer = mgr->ch.peer;
  737. l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
  738. /* We need open here L1 for the manager as well (refcounting) */
  739. rq.adr.dev = mgr->ch.st->dev->id;
  740. id = mgr->ch.st->own.ctrl(&mgr->ch.st->own, OPEN_CHANNEL, &rq);
  741. if (id < 0) {
  742. printk(KERN_WARNING "%s: cannot open L1\n", __func__);
  743. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  744. l2 = NULL;
  745. }
  746. }
  747. return l2;
  748. }
  749. static void
  750. new_tei_req(struct manager *mgr, u_char *dp)
  751. {
  752. int tei, ri;
  753. struct layer2 *l2;
  754. ri = dp[0] << 8;
  755. ri += dp[1];
  756. if (!mgr->up)
  757. goto denied;
  758. if (!(dp[3] & 1)) /* Extension bit != 1 */
  759. goto denied;
  760. if (dp[3] != 0xff)
  761. tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
  762. else
  763. tei = get_free_tei(mgr);
  764. if (tei < 0) {
  765. printk(KERN_WARNING "%s:No free tei\n", __func__);
  766. goto denied;
  767. }
  768. l2 = create_new_tei(mgr, tei, CTRL_SAPI);
  769. if (!l2)
  770. goto denied;
  771. else
  772. mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
  773. return;
  774. denied:
  775. put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
  776. }
  777. static int
  778. ph_data_ind(struct manager *mgr, struct sk_buff *skb)
  779. {
  780. int ret = -EINVAL;
  781. struct layer2 *l2, *nl2;
  782. u_char mt;
  783. if (skb->len < 8) {
  784. if (*debug & DEBUG_L2_TEI)
  785. printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
  786. __func__, skb->len);
  787. goto done;
  788. }
  789. if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
  790. goto done;
  791. if (skb->data[0] & 1) /* EA0 formal error */
  792. goto done;
  793. if (!(skb->data[1] & 1)) /* EA1 formal error */
  794. goto done;
  795. if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
  796. goto done;
  797. if ((skb->data[2] & 0xef) != UI) /* not UI */
  798. goto done;
  799. if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
  800. goto done;
  801. mt = skb->data[6];
  802. switch (mt) {
  803. case ID_REQUEST:
  804. case ID_CHK_RES:
  805. case ID_VERIFY:
  806. if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
  807. goto done;
  808. break;
  809. case ID_ASSIGNED:
  810. case ID_DENIED:
  811. case ID_CHK_REQ:
  812. case ID_REMOVE:
  813. if (test_bit(MGR_OPT_NETWORK, &mgr->options))
  814. goto done;
  815. break;
  816. default:
  817. goto done;
  818. }
  819. ret = 0;
  820. if (mt == ID_REQUEST) {
  821. new_tei_req(mgr, &skb->data[4]);
  822. goto done;
  823. }
  824. list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
  825. tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
  826. }
  827. done:
  828. return ret;
  829. }
  830. int
  831. l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
  832. {
  833. struct teimgr *tm = l2->tm;
  834. if (test_bit(FLG_FIXED_TEI, &l2->flag))
  835. return 0;
  836. if (*debug & DEBUG_L2_TEI)
  837. printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
  838. switch (cmd) {
  839. case MDL_ASSIGN_IND:
  840. mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
  841. break;
  842. case MDL_ERROR_IND:
  843. if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
  844. mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
  845. if (test_bit(MGR_OPT_USER, &tm->mgr->options))
  846. mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
  847. break;
  848. case MDL_STATUS_UP_IND:
  849. if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
  850. mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
  851. break;
  852. case MDL_STATUS_DOWN_IND:
  853. if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
  854. mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
  855. break;
  856. case MDL_STATUS_UI_IND:
  857. if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
  858. mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
  859. break;
  860. }
  861. return 0;
  862. }
  863. void
  864. TEIrelease(struct layer2 *l2)
  865. {
  866. struct teimgr *tm = l2->tm;
  867. u_long flags;
  868. mISDN_FsmDelTimer(&tm->timer, 1);
  869. write_lock_irqsave(&tm->mgr->lock, flags);
  870. list_del(&l2->list);
  871. write_unlock_irqrestore(&tm->mgr->lock, flags);
  872. l2->tm = NULL;
  873. kfree(tm);
  874. }
  875. static int
  876. create_teimgr(struct manager *mgr, struct channel_req *crq)
  877. {
  878. struct layer2 *l2;
  879. unsigned long opt = 0;
  880. unsigned long flags;
  881. int id;
  882. struct channel_req l1rq;
  883. if (*debug & DEBUG_L2_TEI)
  884. printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
  885. __func__, dev_name(&mgr->ch.st->dev->dev),
  886. crq->protocol, crq->adr.dev, crq->adr.channel,
  887. crq->adr.sapi, crq->adr.tei);
  888. if (crq->adr.tei > GROUP_TEI)
  889. return -EINVAL;
  890. if (crq->adr.tei < 64)
  891. test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
  892. if (crq->adr.tei == 0)
  893. test_and_set_bit(OPTION_L2_PTP, &opt);
  894. if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
  895. if (crq->protocol == ISDN_P_LAPD_TE)
  896. return -EPROTONOSUPPORT;
  897. if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
  898. return -EINVAL;
  899. if (mgr->up) {
  900. printk(KERN_WARNING
  901. "%s: only one network manager is allowed\n",
  902. __func__);
  903. return -EBUSY;
  904. }
  905. } else if (test_bit(MGR_OPT_USER, &mgr->options)) {
  906. if (crq->protocol == ISDN_P_LAPD_NT)
  907. return -EPROTONOSUPPORT;
  908. if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
  909. return -EINVAL; /* dyn tei */
  910. } else {
  911. if (crq->protocol == ISDN_P_LAPD_NT)
  912. test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
  913. if (crq->protocol == ISDN_P_LAPD_TE)
  914. test_and_set_bit(MGR_OPT_USER, &mgr->options);
  915. }
  916. l1rq.adr = crq->adr;
  917. if (mgr->ch.st->dev->Dprotocols
  918. & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
  919. test_and_set_bit(OPTION_L2_PMX, &opt);
  920. if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
  921. mgr->up = crq->ch;
  922. id = DL_INFO_L2_CONNECT;
  923. teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
  924. if (test_bit(MGR_PH_ACTIVE, &mgr->options))
  925. teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
  926. crq->ch = NULL;
  927. if (!list_empty(&mgr->layer2)) {
  928. read_lock_irqsave(&mgr->lock, flags);
  929. list_for_each_entry(l2, &mgr->layer2, list) {
  930. l2->up = mgr->up;
  931. l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
  932. }
  933. read_unlock_irqrestore(&mgr->lock, flags);
  934. }
  935. return 0;
  936. }
  937. l2 = create_l2(crq->ch, crq->protocol, opt,
  938. crq->adr.tei, crq->adr.sapi);
  939. if (!l2)
  940. return -ENOMEM;
  941. l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
  942. if (!l2->tm) {
  943. kfree(l2);
  944. printk(KERN_ERR "kmalloc teimgr failed\n");
  945. return -ENOMEM;
  946. }
  947. l2->tm->mgr = mgr;
  948. l2->tm->l2 = l2;
  949. l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
  950. l2->tm->tei_m.userdata = l2->tm;
  951. l2->tm->tei_m.printdebug = tei_debug;
  952. if (crq->protocol == ISDN_P_LAPD_TE) {
  953. l2->tm->tei_m.fsm = &teifsmu;
  954. l2->tm->tei_m.state = ST_TEI_NOP;
  955. l2->tm->tval = 1000; /* T201 1 sec */
  956. if (test_bit(OPTION_L2_PMX, &opt))
  957. l1rq.protocol = ISDN_P_TE_E1;
  958. else
  959. l1rq.protocol = ISDN_P_TE_S0;
  960. } else {
  961. l2->tm->tei_m.fsm = &teifsmn;
  962. l2->tm->tei_m.state = ST_TEI_NOP;
  963. l2->tm->tval = 2000; /* T202 2 sec */
  964. if (test_bit(OPTION_L2_PMX, &opt))
  965. l1rq.protocol = ISDN_P_NT_E1;
  966. else
  967. l1rq.protocol = ISDN_P_NT_S0;
  968. }
  969. mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
  970. write_lock_irqsave(&mgr->lock, flags);
  971. id = get_free_id(mgr);
  972. list_add_tail(&l2->list, &mgr->layer2);
  973. write_unlock_irqrestore(&mgr->lock, flags);
  974. if (id >= 0) {
  975. l2->ch.nr = id;
  976. l2->up->nr = id;
  977. crq->ch = &l2->ch;
  978. /* We need open here L1 for the manager as well (refcounting) */
  979. id = mgr->ch.st->own.ctrl(&mgr->ch.st->own, OPEN_CHANNEL,
  980. &l1rq);
  981. }
  982. if (id < 0)
  983. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  984. return id;
  985. }
  986. static int
  987. mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
  988. {
  989. struct manager *mgr;
  990. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  991. int ret = -EINVAL;
  992. mgr = container_of(ch, struct manager, ch);
  993. if (*debug & DEBUG_L2_RECV)
  994. printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
  995. __func__, hh->prim, hh->id);
  996. switch (hh->prim) {
  997. case PH_DATA_IND:
  998. mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
  999. ret = ph_data_ind(mgr, skb);
  1000. break;
  1001. case PH_DATA_CNF:
  1002. do_ack(mgr, hh->id);
  1003. ret = 0;
  1004. break;
  1005. case PH_ACTIVATE_IND:
  1006. test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
  1007. if (mgr->up)
  1008. teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
  1009. mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
  1010. do_send(mgr);
  1011. ret = 0;
  1012. break;
  1013. case PH_DEACTIVATE_IND:
  1014. test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
  1015. if (mgr->up)
  1016. teiup_create(mgr, PH_DEACTIVATE_IND, 0, NULL);
  1017. mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
  1018. ret = 0;
  1019. break;
  1020. case DL_UNITDATA_REQ:
  1021. return dl_unit_data(mgr, skb);
  1022. }
  1023. if (!ret)
  1024. dev_kfree_skb(skb);
  1025. return ret;
  1026. }
  1027. static int
  1028. free_teimanager(struct manager *mgr)
  1029. {
  1030. struct layer2 *l2, *nl2;
  1031. test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
  1032. if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
  1033. /* not locked lock is taken in release tei */
  1034. mgr->up = NULL;
  1035. if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
  1036. list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
  1037. put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
  1038. mutex_lock(&mgr->ch.st->lmutex);
  1039. list_del(&l2->ch.list);
  1040. mutex_unlock(&mgr->ch.st->lmutex);
  1041. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  1042. }
  1043. test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
  1044. } else {
  1045. list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
  1046. l2->up = NULL;
  1047. }
  1048. }
  1049. }
  1050. if (test_bit(MGR_OPT_USER, &mgr->options)) {
  1051. if (list_empty(&mgr->layer2))
  1052. test_and_clear_bit(MGR_OPT_USER, &mgr->options);
  1053. }
  1054. mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
  1055. return 0;
  1056. }
  1057. static int
  1058. ctrl_teimanager(struct manager *mgr, void *arg)
  1059. {
  1060. /* currently we only have one option */
  1061. unsigned int *val = (unsigned int *)arg;
  1062. switch (val[0]) {
  1063. case IMCLEAR_L2:
  1064. if (val[1])
  1065. test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
  1066. else
  1067. test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
  1068. break;
  1069. case IMHOLD_L1:
  1070. if (val[1])
  1071. test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
  1072. else
  1073. test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
  1074. break;
  1075. default:
  1076. return -EINVAL;
  1077. }
  1078. return 0;
  1079. }
  1080. /* This function does create a L2 for fixed TEI in NT Mode */
  1081. static int
  1082. check_data(struct manager *mgr, struct sk_buff *skb)
  1083. {
  1084. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1085. int ret, tei, sapi;
  1086. struct layer2 *l2;
  1087. if (*debug & DEBUG_L2_CTRL)
  1088. printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
  1089. __func__, hh->prim, hh->id);
  1090. if (test_bit(MGR_OPT_USER, &mgr->options))
  1091. return -ENOTCONN;
  1092. if (hh->prim != PH_DATA_IND)
  1093. return -ENOTCONN;
  1094. if (skb->len != 3)
  1095. return -ENOTCONN;
  1096. if (skb->data[0] & 3) /* EA0 and CR must be 0 */
  1097. return -EINVAL;
  1098. sapi = skb->data[0] >> 2;
  1099. if (!(skb->data[1] & 1)) /* invalid EA1 */
  1100. return -EINVAL;
  1101. tei = skb->data[1] >> 1;
  1102. if (tei > 63) /* not a fixed tei */
  1103. return -ENOTCONN;
  1104. if ((skb->data[2] & ~0x10) != SABME)
  1105. return -ENOTCONN;
  1106. /* We got a SABME for a fixed TEI */
  1107. if (*debug & DEBUG_L2_CTRL)
  1108. printk(KERN_DEBUG "%s: SABME sapi(%d) tei(%d)\n",
  1109. __func__, sapi, tei);
  1110. l2 = create_new_tei(mgr, tei, sapi);
  1111. if (!l2) {
  1112. if (*debug & DEBUG_L2_CTRL)
  1113. printk(KERN_DEBUG "%s: failed to create new tei\n",
  1114. __func__);
  1115. return -ENOMEM;
  1116. }
  1117. ret = l2->ch.send(&l2->ch, skb);
  1118. return ret;
  1119. }
  1120. void
  1121. delete_teimanager(struct mISDNchannel *ch)
  1122. {
  1123. struct manager *mgr;
  1124. struct layer2 *l2, *nl2;
  1125. mgr = container_of(ch, struct manager, ch);
  1126. /* not locked lock is taken in release tei */
  1127. list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
  1128. mutex_lock(&mgr->ch.st->lmutex);
  1129. list_del(&l2->ch.list);
  1130. mutex_unlock(&mgr->ch.st->lmutex);
  1131. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  1132. }
  1133. list_del(&mgr->ch.list);
  1134. list_del(&mgr->bcast.list);
  1135. skb_queue_purge(&mgr->sendq);
  1136. kfree(mgr);
  1137. }
  1138. static int
  1139. mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1140. {
  1141. struct manager *mgr;
  1142. int ret = -EINVAL;
  1143. mgr = container_of(ch, struct manager, ch);
  1144. if (*debug & DEBUG_L2_CTRL)
  1145. printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
  1146. switch (cmd) {
  1147. case OPEN_CHANNEL:
  1148. ret = create_teimgr(mgr, arg);
  1149. break;
  1150. case CLOSE_CHANNEL:
  1151. ret = free_teimanager(mgr);
  1152. break;
  1153. case CONTROL_CHANNEL:
  1154. ret = ctrl_teimanager(mgr, arg);
  1155. break;
  1156. case CHECK_DATA:
  1157. ret = check_data(mgr, arg);
  1158. break;
  1159. }
  1160. return ret;
  1161. }
  1162. static int
  1163. mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
  1164. {
  1165. struct manager *mgr = container_of(ch, struct manager, bcast);
  1166. struct mISDNhead *hhc, *hh = mISDN_HEAD_P(skb);
  1167. struct sk_buff *cskb = NULL;
  1168. struct layer2 *l2;
  1169. u_long flags;
  1170. int ret;
  1171. read_lock_irqsave(&mgr->lock, flags);
  1172. list_for_each_entry(l2, &mgr->layer2, list) {
  1173. if ((hh->id & MISDN_ID_SAPI_MASK) ==
  1174. (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
  1175. if (list_is_last(&l2->list, &mgr->layer2)) {
  1176. cskb = skb;
  1177. skb = NULL;
  1178. } else {
  1179. if (!cskb)
  1180. cskb = skb_copy(skb, GFP_ATOMIC);
  1181. }
  1182. if (cskb) {
  1183. hhc = mISDN_HEAD_P(cskb);
  1184. /* save original header behind normal header */
  1185. hhc++;
  1186. *hhc = *hh;
  1187. hhc--;
  1188. hhc->prim = DL_INTERN_MSG;
  1189. hhc->id = l2->ch.nr;
  1190. ret = ch->st->own.recv(&ch->st->own, cskb);
  1191. if (ret) {
  1192. if (*debug & DEBUG_SEND_ERR)
  1193. printk(KERN_DEBUG
  1194. "%s ch%d prim(%x) addr(%x)"
  1195. " err %d\n",
  1196. __func__, l2->ch.nr,
  1197. hh->prim, l2->ch.addr, ret);
  1198. } else
  1199. cskb = NULL;
  1200. } else {
  1201. printk(KERN_WARNING "%s ch%d addr %x no mem\n",
  1202. __func__, ch->nr, ch->addr);
  1203. goto out;
  1204. }
  1205. }
  1206. }
  1207. out:
  1208. read_unlock_irqrestore(&mgr->lock, flags);
  1209. dev_kfree_skb(cskb);
  1210. dev_kfree_skb(skb);
  1211. return 0;
  1212. }
  1213. static int
  1214. mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1215. {
  1216. return -EINVAL;
  1217. }
  1218. int
  1219. create_teimanager(struct mISDNdevice *dev)
  1220. {
  1221. struct manager *mgr;
  1222. mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
  1223. if (!mgr)
  1224. return -ENOMEM;
  1225. INIT_LIST_HEAD(&mgr->layer2);
  1226. rwlock_init(&mgr->lock);
  1227. skb_queue_head_init(&mgr->sendq);
  1228. mgr->nextid = 1;
  1229. mgr->lastid = MISDN_ID_NONE;
  1230. mgr->ch.send = mgr_send;
  1231. mgr->ch.ctrl = mgr_ctrl;
  1232. mgr->ch.st = dev->D.st;
  1233. set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
  1234. add_layer2(&mgr->ch, dev->D.st);
  1235. mgr->bcast.send = mgr_bcast;
  1236. mgr->bcast.ctrl = mgr_bcast_ctrl;
  1237. mgr->bcast.st = dev->D.st;
  1238. set_channel_address(&mgr->bcast, 0, GROUP_TEI);
  1239. add_layer2(&mgr->bcast, dev->D.st);
  1240. mgr->deact.debug = *debug & DEBUG_MANAGER;
  1241. mgr->deact.userdata = mgr;
  1242. mgr->deact.printdebug = da_debug;
  1243. mgr->deact.fsm = &deactfsm;
  1244. mgr->deact.state = ST_L1_DEACT;
  1245. mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
  1246. dev->teimgr = &mgr->ch;
  1247. return 0;
  1248. }
  1249. int TEIInit(u_int *deb)
  1250. {
  1251. int res;
  1252. debug = deb;
  1253. teifsmu.state_count = TEI_STATE_COUNT;
  1254. teifsmu.event_count = TEI_EVENT_COUNT;
  1255. teifsmu.strEvent = strTeiEvent;
  1256. teifsmu.strState = strTeiState;
  1257. res = mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
  1258. if (res)
  1259. goto error;
  1260. teifsmn.state_count = TEI_STATE_COUNT;
  1261. teifsmn.event_count = TEI_EVENT_COUNT;
  1262. teifsmn.strEvent = strTeiEvent;
  1263. teifsmn.strState = strTeiState;
  1264. res = mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
  1265. if (res)
  1266. goto error_smn;
  1267. deactfsm.state_count = DEACT_STATE_COUNT;
  1268. deactfsm.event_count = DEACT_EVENT_COUNT;
  1269. deactfsm.strEvent = strDeactEvent;
  1270. deactfsm.strState = strDeactState;
  1271. res = mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
  1272. if (res)
  1273. goto error_deact;
  1274. return 0;
  1275. error_deact:
  1276. mISDN_FsmFree(&teifsmn);
  1277. error_smn:
  1278. mISDN_FsmFree(&teifsmu);
  1279. error:
  1280. return res;
  1281. }
  1282. void TEIFree(void)
  1283. {
  1284. mISDN_FsmFree(&teifsmu);
  1285. mISDN_FsmFree(&teifsmn);
  1286. mISDN_FsmFree(&deactfsm);
  1287. }