tei.c 32 KB

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