tei.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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 "core.h"
  20. #define ID_REQUEST 1
  21. #define ID_ASSIGNED 2
  22. #define ID_DENIED 3
  23. #define ID_CHK_REQ 4
  24. #define ID_CHK_RES 5
  25. #define ID_REMOVE 6
  26. #define ID_VERIFY 7
  27. #define TEI_ENTITY_ID 0xf
  28. #define MGR_PH_ACTIVE 16
  29. #define MGR_PH_NOTREADY 17
  30. #define DATIMER_VAL 10000
  31. static u_int *debug;
  32. static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
  33. static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
  34. static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
  35. enum {
  36. ST_L1_DEACT,
  37. ST_L1_DEACT_PENDING,
  38. ST_L1_ACTIV,
  39. };
  40. #define DEACT_STATE_COUNT (ST_L1_ACTIV+1)
  41. static char *strDeactState[] =
  42. {
  43. "ST_L1_DEACT",
  44. "ST_L1_DEACT_PENDING",
  45. "ST_L1_ACTIV",
  46. };
  47. enum {
  48. EV_ACTIVATE,
  49. EV_ACTIVATE_IND,
  50. EV_DEACTIVATE,
  51. EV_DEACTIVATE_IND,
  52. EV_UI,
  53. EV_DATIMER,
  54. };
  55. #define DEACT_EVENT_COUNT (EV_DATIMER+1)
  56. static char *strDeactEvent[] =
  57. {
  58. "EV_ACTIVATE",
  59. "EV_ACTIVATE_IND",
  60. "EV_DEACTIVATE",
  61. "EV_DEACTIVATE_IND",
  62. "EV_UI",
  63. "EV_DATIMER",
  64. };
  65. static void
  66. da_debug(struct FsmInst *fi, char *fmt, ...)
  67. {
  68. struct manager *mgr = fi->userdata;
  69. va_list va;
  70. if (!(*debug & DEBUG_L2_TEIFSM))
  71. return;
  72. va_start(va, fmt);
  73. printk(KERN_DEBUG "mgr(%d): ", mgr->ch.st->dev->id);
  74. vprintk(fmt, va);
  75. printk("\n");
  76. va_end(va);
  77. }
  78. static void
  79. da_activate(struct FsmInst *fi, int event, void *arg)
  80. {
  81. struct manager *mgr = fi->userdata;
  82. if (fi->state == ST_L1_DEACT_PENDING)
  83. mISDN_FsmDelTimer(&mgr->datimer, 1);
  84. mISDN_FsmChangeState(fi, ST_L1_ACTIV);
  85. }
  86. static void
  87. da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
  88. {
  89. mISDN_FsmChangeState(fi, ST_L1_DEACT);
  90. }
  91. static void
  92. da_deactivate(struct FsmInst *fi, int event, void *arg)
  93. {
  94. struct manager *mgr = fi->userdata;
  95. struct layer2 *l2;
  96. u_long flags;
  97. read_lock_irqsave(&mgr->lock, flags);
  98. list_for_each_entry(l2, &mgr->layer2, list) {
  99. if (l2->l2m.state > ST_L2_4) {
  100. /* have still activ TEI */
  101. read_unlock_irqrestore(&mgr->lock, flags);
  102. return;
  103. }
  104. }
  105. read_unlock_irqrestore(&mgr->lock, flags);
  106. /* All TEI are inactiv */
  107. if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
  108. mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
  109. NULL, 1);
  110. mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
  111. }
  112. }
  113. static void
  114. da_ui(struct FsmInst *fi, int event, void *arg)
  115. {
  116. struct manager *mgr = fi->userdata;
  117. /* restart da timer */
  118. if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
  119. mISDN_FsmDelTimer(&mgr->datimer, 2);
  120. mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
  121. NULL, 2);
  122. }
  123. }
  124. static void
  125. da_timer(struct FsmInst *fi, int event, void *arg)
  126. {
  127. struct manager *mgr = fi->userdata;
  128. struct layer2 *l2;
  129. u_long flags;
  130. /* check again */
  131. read_lock_irqsave(&mgr->lock, flags);
  132. list_for_each_entry(l2, &mgr->layer2, list) {
  133. if (l2->l2m.state > ST_L2_4) {
  134. /* have still activ TEI */
  135. read_unlock_irqrestore(&mgr->lock, flags);
  136. mISDN_FsmChangeState(fi, ST_L1_ACTIV);
  137. return;
  138. }
  139. }
  140. read_unlock_irqrestore(&mgr->lock, flags);
  141. /* All TEI are inactiv */
  142. mISDN_FsmChangeState(fi, ST_L1_DEACT);
  143. _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
  144. GFP_ATOMIC);
  145. }
  146. static struct FsmNode DeactFnList[] =
  147. {
  148. {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
  149. {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
  150. {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
  151. {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
  152. {ST_L1_DEACT_PENDING, EV_UI, da_ui},
  153. {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
  154. };
  155. enum {
  156. ST_TEI_NOP,
  157. ST_TEI_IDREQ,
  158. ST_TEI_IDVERIFY,
  159. };
  160. #define TEI_STATE_COUNT (ST_TEI_IDVERIFY+1)
  161. static char *strTeiState[] =
  162. {
  163. "ST_TEI_NOP",
  164. "ST_TEI_IDREQ",
  165. "ST_TEI_IDVERIFY",
  166. };
  167. enum {
  168. EV_IDREQ,
  169. EV_ASSIGN,
  170. EV_ASSIGN_REQ,
  171. EV_DENIED,
  172. EV_CHKREQ,
  173. EV_CHKRESP,
  174. EV_REMOVE,
  175. EV_VERIFY,
  176. EV_TIMER,
  177. };
  178. #define TEI_EVENT_COUNT (EV_TIMER+1)
  179. static char *strTeiEvent[] =
  180. {
  181. "EV_IDREQ",
  182. "EV_ASSIGN",
  183. "EV_ASSIGN_REQ",
  184. "EV_DENIED",
  185. "EV_CHKREQ",
  186. "EV_CHKRESP",
  187. "EV_REMOVE",
  188. "EV_VERIFY",
  189. "EV_TIMER",
  190. };
  191. static void
  192. tei_debug(struct FsmInst *fi, char *fmt, ...)
  193. {
  194. struct teimgr *tm = fi->userdata;
  195. va_list va;
  196. if (!(*debug & DEBUG_L2_TEIFSM))
  197. return;
  198. va_start(va, fmt);
  199. printk(KERN_DEBUG "sapi(%d) tei(%d): ", tm->l2->sapi, tm->l2->tei);
  200. vprintk(fmt, va);
  201. printk("\n");
  202. va_end(va);
  203. }
  204. static int
  205. get_free_id(struct manager *mgr)
  206. {
  207. u64 ids = 0;
  208. int i;
  209. struct layer2 *l2;
  210. list_for_each_entry(l2, &mgr->layer2, list) {
  211. if (l2->ch.nr > 63) {
  212. printk(KERN_WARNING
  213. "%s: more as 63 layer2 for one device\n",
  214. __func__);
  215. return -EBUSY;
  216. }
  217. test_and_set_bit(l2->ch.nr, (u_long *)&ids);
  218. }
  219. for (i = 1; i < 64; i++)
  220. if (!test_bit(i, (u_long *)&ids))
  221. return i;
  222. printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
  223. __func__);
  224. return -EBUSY;
  225. }
  226. static int
  227. get_free_tei(struct manager *mgr)
  228. {
  229. u64 ids = 0;
  230. int i;
  231. struct layer2 *l2;
  232. list_for_each_entry(l2, &mgr->layer2, list) {
  233. if (l2->ch.nr == 0)
  234. continue;
  235. if ((l2->ch.addr & 0xff) != 0)
  236. continue;
  237. i = l2->ch.addr >> 8;
  238. if (i < 64)
  239. continue;
  240. i -= 64;
  241. test_and_set_bit(i, (u_long *)&ids);
  242. }
  243. for (i = 0; i < 64; i++)
  244. if (!test_bit(i, (u_long *)&ids))
  245. return i + 64;
  246. printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
  247. __func__);
  248. return -1;
  249. }
  250. static void
  251. teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
  252. {
  253. struct sk_buff *skb;
  254. struct mISDNhead *hh;
  255. int err;
  256. skb = mI_alloc_skb(len, GFP_ATOMIC);
  257. if (!skb)
  258. return;
  259. hh = mISDN_HEAD_P(skb);
  260. hh->prim = prim;
  261. hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
  262. if (len)
  263. memcpy(skb_put(skb, len), arg, len);
  264. err = mgr->up->send(mgr->up, skb);
  265. if (err) {
  266. printk(KERN_WARNING "%s: err=%d\n", __func__, err);
  267. dev_kfree_skb(skb);
  268. }
  269. }
  270. static u_int
  271. new_id(struct manager *mgr)
  272. {
  273. u_int id;
  274. id = mgr->nextid++;
  275. if (id == 0x7fff)
  276. mgr->nextid = 1;
  277. id <<= 16;
  278. id |= GROUP_TEI << 8;
  279. id |= TEI_SAPI;
  280. return id;
  281. }
  282. static void
  283. do_send(struct manager *mgr)
  284. {
  285. if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
  286. return;
  287. if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
  288. struct sk_buff *skb = skb_dequeue(&mgr->sendq);
  289. if (!skb) {
  290. test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
  291. return;
  292. }
  293. mgr->lastid = mISDN_HEAD_ID(skb);
  294. mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
  295. if (mgr->ch.recv(mgr->ch.peer, skb)) {
  296. dev_kfree_skb(skb);
  297. test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
  298. mgr->lastid = MISDN_ID_NONE;
  299. }
  300. }
  301. }
  302. static void
  303. do_ack(struct manager *mgr, u_int id)
  304. {
  305. if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
  306. if (id == mgr->lastid) {
  307. if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
  308. struct sk_buff *skb;
  309. skb = skb_dequeue(&mgr->sendq);
  310. if (skb) {
  311. mgr->lastid = mISDN_HEAD_ID(skb);
  312. if (!mgr->ch.recv(mgr->ch.peer, skb))
  313. return;
  314. dev_kfree_skb(skb);
  315. }
  316. }
  317. mgr->lastid = MISDN_ID_NONE;
  318. test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
  319. }
  320. }
  321. }
  322. static void
  323. mgr_send_down(struct manager *mgr, struct sk_buff *skb)
  324. {
  325. skb_queue_tail(&mgr->sendq, skb);
  326. if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
  327. _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
  328. NULL, GFP_KERNEL);
  329. } else {
  330. do_send(mgr);
  331. }
  332. }
  333. static int
  334. dl_unit_data(struct manager *mgr, struct sk_buff *skb)
  335. {
  336. if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
  337. return -EINVAL;
  338. if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
  339. _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
  340. NULL, GFP_KERNEL);
  341. skb_push(skb, 3);
  342. skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
  343. skb->data[1] = 0xff; /* TEI 127 */
  344. skb->data[2] = UI; /* UI frame */
  345. mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
  346. mISDN_HEAD_ID(skb) = new_id(mgr);
  347. skb_queue_tail(&mgr->sendq, skb);
  348. do_send(mgr);
  349. return 0;
  350. }
  351. static unsigned int
  352. random_ri(void)
  353. {
  354. u16 x;
  355. get_random_bytes(&x, sizeof(x));
  356. return x;
  357. }
  358. static struct layer2 *
  359. findtei(struct manager *mgr, int tei)
  360. {
  361. struct layer2 *l2;
  362. u_long flags;
  363. read_lock_irqsave(&mgr->lock, flags);
  364. list_for_each_entry(l2, &mgr->layer2, list) {
  365. if ((l2->sapi == 0) && (l2->tei > 0) &&
  366. (l2->tei != GROUP_TEI) && (l2->tei == tei))
  367. goto done;
  368. }
  369. l2 = NULL;
  370. done:
  371. read_unlock_irqrestore(&mgr->lock, flags);
  372. return l2;
  373. }
  374. static void
  375. put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei)
  376. {
  377. struct sk_buff *skb;
  378. u_char bp[8];
  379. bp[0] = (TEI_SAPI << 2);
  380. if (test_bit(MGR_OPT_NETWORK, &mgr->options))
  381. bp[0] |= 2; /* CR:=1 for net command */
  382. bp[1] = (GROUP_TEI << 1) | 0x1;
  383. bp[2] = UI;
  384. bp[3] = TEI_ENTITY_ID;
  385. bp[4] = ri >> 8;
  386. bp[5] = ri & 0xff;
  387. bp[6] = m_id;
  388. bp[7] = ((tei << 1) & 0xff) | 1;
  389. skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC);
  390. if (!skb) {
  391. printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
  392. return;
  393. }
  394. mgr_send_down(mgr, skb);
  395. }
  396. static void
  397. tei_id_request(struct FsmInst *fi, int event, void *arg)
  398. {
  399. struct teimgr *tm = fi->userdata;
  400. if (tm->l2->tei != GROUP_TEI) {
  401. tm->tei_m.printdebug(&tm->tei_m,
  402. "assign request for allready assigned tei %d",
  403. tm->l2->tei);
  404. return;
  405. }
  406. tm->ri = random_ri();
  407. if (*debug & DEBUG_L2_TEI)
  408. tm->tei_m.printdebug(&tm->tei_m,
  409. "assign request ri %d", tm->ri);
  410. put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
  411. mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
  412. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
  413. tm->nval = 3;
  414. }
  415. static void
  416. tei_id_assign(struct FsmInst *fi, int event, void *arg)
  417. {
  418. struct teimgr *tm = fi->userdata;
  419. struct layer2 *l2;
  420. u_char *dp = arg;
  421. int ri, tei;
  422. ri = ((unsigned int) *dp++ << 8);
  423. ri += *dp++;
  424. dp++;
  425. tei = *dp >> 1;
  426. if (*debug & DEBUG_L2_TEI)
  427. tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
  428. ri, tei);
  429. l2 = findtei(tm->mgr, tei);
  430. if (l2) { /* same tei is in use */
  431. if (ri != l2->tm->ri) {
  432. tm->tei_m.printdebug(fi,
  433. "possible duplicate assignment tei %d", tei);
  434. tei_l2(l2, MDL_ERROR_RSP, 0);
  435. }
  436. } else if (ri == tm->ri) {
  437. mISDN_FsmDelTimer(&tm->timer, 1);
  438. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  439. tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
  440. }
  441. }
  442. static void
  443. tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
  444. {
  445. struct teimgr *tm = fi->userdata;
  446. struct layer2 *l2;
  447. u_char *dp = arg;
  448. int tei, ri;
  449. ri = ((unsigned int) *dp++ << 8);
  450. ri += *dp++;
  451. dp++;
  452. tei = *dp >> 1;
  453. if (*debug & DEBUG_L2_TEI)
  454. tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
  455. ri, tei);
  456. l2 = findtei(tm->mgr, tei);
  457. if (l2) { /* same tei is in use */
  458. if (ri != l2->tm->ri) { /* and it wasn't our request */
  459. tm->tei_m.printdebug(fi,
  460. "possible duplicate assignment tei %d", tei);
  461. mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
  462. }
  463. }
  464. }
  465. static void
  466. tei_id_denied(struct FsmInst *fi, int event, void *arg)
  467. {
  468. struct teimgr *tm = fi->userdata;
  469. u_char *dp = arg;
  470. int ri, tei;
  471. ri = ((unsigned int) *dp++ << 8);
  472. ri += *dp++;
  473. dp++;
  474. tei = *dp >> 1;
  475. if (*debug & DEBUG_L2_TEI)
  476. tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
  477. ri, tei);
  478. }
  479. static void
  480. tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
  481. {
  482. struct teimgr *tm = fi->userdata;
  483. u_char *dp = arg;
  484. int tei;
  485. tei = *(dp+3) >> 1;
  486. if (*debug & DEBUG_L2_TEI)
  487. tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
  488. if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
  489. (tei == tm->l2->tei))) {
  490. mISDN_FsmDelTimer(&tm->timer, 4);
  491. mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
  492. put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
  493. }
  494. }
  495. static void
  496. tei_id_remove(struct FsmInst *fi, int event, void *arg)
  497. {
  498. struct teimgr *tm = fi->userdata;
  499. u_char *dp = arg;
  500. int tei;
  501. tei = *(dp+3) >> 1;
  502. if (*debug & DEBUG_L2_TEI)
  503. tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
  504. if ((tm->l2->tei != GROUP_TEI) &&
  505. ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
  506. mISDN_FsmDelTimer(&tm->timer, 5);
  507. mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
  508. tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
  509. }
  510. }
  511. static void
  512. tei_id_verify(struct FsmInst *fi, int event, void *arg)
  513. {
  514. struct teimgr *tm = fi->userdata;
  515. if (*debug & DEBUG_L2_TEI)
  516. tm->tei_m.printdebug(fi, "id verify request for tei %d",
  517. tm->l2->tei);
  518. put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
  519. mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
  520. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
  521. tm->nval = 2;
  522. }
  523. static void
  524. tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
  525. {
  526. struct teimgr *tm = fi->userdata;
  527. if (--tm->nval) {
  528. tm->ri = random_ri();
  529. if (*debug & DEBUG_L2_TEI)
  530. tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
  531. 4 - tm->nval, tm->ri);
  532. put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
  533. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
  534. } else {
  535. tm->tei_m.printdebug(fi, "assign req failed");
  536. tei_l2(tm->l2, MDL_ERROR_RSP, 0);
  537. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  538. }
  539. }
  540. static void
  541. tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
  542. {
  543. struct teimgr *tm = fi->userdata;
  544. if (--tm->nval) {
  545. if (*debug & DEBUG_L2_TEI)
  546. tm->tei_m.printdebug(fi,
  547. "id verify req(%d) for tei %d",
  548. 3 - tm->nval, tm->l2->tei);
  549. put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
  550. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
  551. } else {
  552. tm->tei_m.printdebug(fi, "verify req for tei %d failed",
  553. tm->l2->tei);
  554. tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
  555. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  556. }
  557. }
  558. static struct FsmNode TeiFnListUser[] =
  559. {
  560. {ST_TEI_NOP, EV_IDREQ, tei_id_request},
  561. {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
  562. {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
  563. {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
  564. {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
  565. {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
  566. {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
  567. {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
  568. {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
  569. {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
  570. {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
  571. };
  572. static void
  573. tei_l2remove(struct layer2 *l2)
  574. {
  575. put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
  576. tei_l2(l2, MDL_REMOVE_REQ, 0);
  577. list_del(&l2->ch.list);
  578. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  579. }
  580. static void
  581. tei_assign_req(struct FsmInst *fi, int event, void *arg)
  582. {
  583. struct teimgr *tm = fi->userdata;
  584. u_char *dp = arg;
  585. if (tm->l2->tei == GROUP_TEI) {
  586. tm->tei_m.printdebug(&tm->tei_m,
  587. "net tei assign request without tei");
  588. return;
  589. }
  590. tm->ri = ((unsigned int) *dp++ << 8);
  591. tm->ri += *dp++;
  592. if (*debug & DEBUG_L2_TEI)
  593. tm->tei_m.printdebug(&tm->tei_m,
  594. "net assign request ri %d teim %d", tm->ri, *dp);
  595. put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
  596. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  597. }
  598. static void
  599. tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
  600. {
  601. struct teimgr *tm = fi->userdata;
  602. if (*debug & DEBUG_L2_TEI)
  603. tm->tei_m.printdebug(fi, "id check request for tei %d",
  604. tm->l2->tei);
  605. tm->rcnt = 0;
  606. put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
  607. mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
  608. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
  609. tm->nval = 2;
  610. }
  611. static void
  612. tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
  613. {
  614. struct teimgr *tm = fi->userdata;
  615. u_char *dp = arg;
  616. int tei;
  617. tei = dp[3] >> 1;
  618. if (*debug & DEBUG_L2_TEI)
  619. tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
  620. if (tei == tm->l2->tei)
  621. tm->rcnt++;
  622. }
  623. static void
  624. tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
  625. {
  626. struct teimgr *tm = fi->userdata;
  627. u_char *dp = arg;
  628. int tei;
  629. tei = dp[3] >> 1;
  630. if (*debug & DEBUG_L2_TEI)
  631. tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
  632. tei, tm->l2->tei);
  633. if (tei == tm->l2->tei)
  634. tei_id_chk_req_net(fi, event, arg);
  635. }
  636. static void
  637. tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
  638. {
  639. struct teimgr *tm = fi->userdata;
  640. if (tm->rcnt == 1) {
  641. if (*debug & DEBUG_L2_TEI)
  642. tm->tei_m.printdebug(fi,
  643. "check req for tei %d sucessful\n", tm->l2->tei);
  644. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  645. } else if (tm->rcnt > 1) {
  646. /* duplicate assignment; remove */
  647. tei_l2remove(tm->l2);
  648. } else if (--tm->nval) {
  649. if (*debug & DEBUG_L2_TEI)
  650. tm->tei_m.printdebug(fi,
  651. "id check req(%d) for tei %d",
  652. 3 - tm->nval, tm->l2->tei);
  653. put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
  654. mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
  655. } else {
  656. tm->tei_m.printdebug(fi, "check req for tei %d failed",
  657. tm->l2->tei);
  658. mISDN_FsmChangeState(fi, ST_TEI_NOP);
  659. tei_l2remove(tm->l2);
  660. }
  661. }
  662. static struct FsmNode TeiFnListNet[] =
  663. {
  664. {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
  665. {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
  666. {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
  667. {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
  668. {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
  669. };
  670. static void
  671. tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
  672. {
  673. if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
  674. return;
  675. if (*debug & DEBUG_L2_TEI)
  676. tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
  677. if (mt == ID_ASSIGNED)
  678. mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
  679. else if (mt == ID_DENIED)
  680. mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
  681. else if (mt == ID_CHK_REQ)
  682. mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
  683. else if (mt == ID_REMOVE)
  684. mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
  685. else if (mt == ID_VERIFY)
  686. mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
  687. else if (mt == ID_CHK_RES)
  688. mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
  689. }
  690. static struct layer2 *
  691. create_new_tei(struct manager *mgr, int tei, int sapi)
  692. {
  693. u_long opt = 0;
  694. u_long flags;
  695. int id;
  696. struct layer2 *l2;
  697. if (!mgr->up)
  698. return NULL;
  699. if ((tei >= 0) && (tei < 64))
  700. test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
  701. if (mgr->ch.st->dev->Dprotocols
  702. & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
  703. test_and_set_bit(OPTION_L2_PMX, &opt);
  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. }
  739. return l2;
  740. }
  741. static void
  742. new_tei_req(struct manager *mgr, u_char *dp)
  743. {
  744. int tei, ri;
  745. struct layer2 *l2;
  746. ri = dp[0] << 8;
  747. ri += dp[1];
  748. if (!mgr->up)
  749. goto denied;
  750. if (!(dp[3] & 1)) /* Extension bit != 1 */
  751. goto denied;
  752. if (dp[3] != 0xff)
  753. tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
  754. else
  755. tei = get_free_tei(mgr);
  756. if (tei < 0) {
  757. printk(KERN_WARNING "%s:No free tei\n", __func__);
  758. goto denied;
  759. }
  760. l2 = create_new_tei(mgr, tei, CTRL_SAPI);
  761. if (!l2)
  762. goto denied;
  763. else
  764. mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
  765. return;
  766. denied:
  767. put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
  768. }
  769. static int
  770. ph_data_ind(struct manager *mgr, struct sk_buff *skb)
  771. {
  772. int ret = -EINVAL;
  773. struct layer2 *l2;
  774. u_long flags;
  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 (*debug & DEBUG_L2_TEI)
  783. if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
  784. goto done;
  785. if (skb->data[0] & 1) /* EA0 formal error */
  786. goto done;
  787. if (!(skb->data[1] & 1)) /* EA1 formal error */
  788. goto done;
  789. if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
  790. goto done;
  791. if ((skb->data[2] & 0xef) != UI) /* not UI */
  792. goto done;
  793. if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
  794. goto done;
  795. mt = skb->data[6];
  796. switch (mt) {
  797. case ID_REQUEST:
  798. case ID_CHK_RES:
  799. case ID_VERIFY:
  800. if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
  801. goto done;
  802. break;
  803. case ID_ASSIGNED:
  804. case ID_DENIED:
  805. case ID_CHK_REQ:
  806. case ID_REMOVE:
  807. if (test_bit(MGR_OPT_NETWORK, &mgr->options))
  808. goto done;
  809. break;
  810. default:
  811. goto done;
  812. }
  813. ret = 0;
  814. if (mt == ID_REQUEST) {
  815. new_tei_req(mgr, &skb->data[4]);
  816. goto done;
  817. }
  818. read_lock_irqsave(&mgr->lock, flags);
  819. list_for_each_entry(l2, &mgr->layer2, list) {
  820. tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
  821. }
  822. read_unlock_irqrestore(&mgr->lock, flags);
  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. }