tei.c 32 KB

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