tei.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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. DECLARE_BITMAP(ids, 64) = { [0 ... BITS_TO_LONGS(64) - 1] = 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. __set_bit(l2->ch.nr, ids);
  222. }
  223. i = find_next_zero_bit(ids, 64, 1);
  224. if (i < 64)
  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. DECLARE_BITMAP(ids, 64) = { [0 ... BITS_TO_LONGS(64) - 1] = 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. __set_bit(i, ids);
  246. }
  247. i = find_first_zero_bit(ids, 64);
  248. if (i < 64)
  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. skb_put_data(skb, 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. unsigned long opt = 0;
  698. unsigned long flags;
  699. int id;
  700. struct layer2 *l2;
  701. struct channel_req rq;
  702. if (!mgr->up)
  703. return NULL;
  704. if ((tei >= 0) && (tei < 64))
  705. test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
  706. if (mgr->ch.st->dev->Dprotocols & ((1 << ISDN_P_TE_E1) |
  707. (1 << ISDN_P_NT_E1))) {
  708. test_and_set_bit(OPTION_L2_PMX, &opt);
  709. rq.protocol = ISDN_P_NT_E1;
  710. } else {
  711. rq.protocol = ISDN_P_NT_S0;
  712. }
  713. l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi);
  714. if (!l2) {
  715. printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
  716. return NULL;
  717. }
  718. l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
  719. if (!l2->tm) {
  720. kfree(l2);
  721. printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
  722. return NULL;
  723. }
  724. l2->tm->mgr = mgr;
  725. l2->tm->l2 = l2;
  726. l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
  727. l2->tm->tei_m.userdata = l2->tm;
  728. l2->tm->tei_m.printdebug = tei_debug;
  729. l2->tm->tei_m.fsm = &teifsmn;
  730. l2->tm->tei_m.state = ST_TEI_NOP;
  731. l2->tm->tval = 2000; /* T202 2 sec */
  732. mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
  733. write_lock_irqsave(&mgr->lock, flags);
  734. id = get_free_id(mgr);
  735. list_add_tail(&l2->list, &mgr->layer2);
  736. write_unlock_irqrestore(&mgr->lock, flags);
  737. if (id < 0) {
  738. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  739. printk(KERN_WARNING "%s:no free id\n", __func__);
  740. return NULL;
  741. } else {
  742. l2->ch.nr = id;
  743. __add_layer2(&l2->ch, mgr->ch.st);
  744. l2->ch.recv = mgr->ch.recv;
  745. l2->ch.peer = mgr->ch.peer;
  746. l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
  747. /* We need open here L1 for the manager as well (refcounting) */
  748. rq.adr.dev = mgr->ch.st->dev->id;
  749. id = mgr->ch.st->own.ctrl(&mgr->ch.st->own, OPEN_CHANNEL, &rq);
  750. if (id < 0) {
  751. printk(KERN_WARNING "%s: cannot open L1\n", __func__);
  752. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  753. l2 = NULL;
  754. }
  755. }
  756. return l2;
  757. }
  758. static void
  759. new_tei_req(struct manager *mgr, u_char *dp)
  760. {
  761. int tei, ri;
  762. struct layer2 *l2;
  763. ri = dp[0] << 8;
  764. ri += dp[1];
  765. if (!mgr->up)
  766. goto denied;
  767. if (!(dp[3] & 1)) /* Extension bit != 1 */
  768. goto denied;
  769. if (dp[3] != 0xff)
  770. tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
  771. else
  772. tei = get_free_tei(mgr);
  773. if (tei < 0) {
  774. printk(KERN_WARNING "%s:No free tei\n", __func__);
  775. goto denied;
  776. }
  777. l2 = create_new_tei(mgr, tei, CTRL_SAPI);
  778. if (!l2)
  779. goto denied;
  780. else
  781. mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
  782. return;
  783. denied:
  784. put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
  785. }
  786. static int
  787. ph_data_ind(struct manager *mgr, struct sk_buff *skb)
  788. {
  789. int ret = -EINVAL;
  790. struct layer2 *l2, *nl2;
  791. u_char mt;
  792. if (skb->len < 8) {
  793. if (*debug & DEBUG_L2_TEI)
  794. printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
  795. __func__, skb->len);
  796. goto done;
  797. }
  798. if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
  799. goto done;
  800. if (skb->data[0] & 1) /* EA0 formal error */
  801. goto done;
  802. if (!(skb->data[1] & 1)) /* EA1 formal error */
  803. goto done;
  804. if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
  805. goto done;
  806. if ((skb->data[2] & 0xef) != UI) /* not UI */
  807. goto done;
  808. if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
  809. goto done;
  810. mt = skb->data[6];
  811. switch (mt) {
  812. case ID_REQUEST:
  813. case ID_CHK_RES:
  814. case ID_VERIFY:
  815. if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
  816. goto done;
  817. break;
  818. case ID_ASSIGNED:
  819. case ID_DENIED:
  820. case ID_CHK_REQ:
  821. case ID_REMOVE:
  822. if (test_bit(MGR_OPT_NETWORK, &mgr->options))
  823. goto done;
  824. break;
  825. default:
  826. goto done;
  827. }
  828. ret = 0;
  829. if (mt == ID_REQUEST) {
  830. new_tei_req(mgr, &skb->data[4]);
  831. goto done;
  832. }
  833. list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
  834. tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
  835. }
  836. done:
  837. return ret;
  838. }
  839. int
  840. l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
  841. {
  842. struct teimgr *tm = l2->tm;
  843. if (test_bit(FLG_FIXED_TEI, &l2->flag))
  844. return 0;
  845. if (*debug & DEBUG_L2_TEI)
  846. printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
  847. switch (cmd) {
  848. case MDL_ASSIGN_IND:
  849. mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
  850. break;
  851. case MDL_ERROR_IND:
  852. if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
  853. mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
  854. if (test_bit(MGR_OPT_USER, &tm->mgr->options))
  855. mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
  856. break;
  857. case MDL_STATUS_UP_IND:
  858. if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
  859. mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
  860. break;
  861. case MDL_STATUS_DOWN_IND:
  862. if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
  863. mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
  864. break;
  865. case MDL_STATUS_UI_IND:
  866. if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
  867. mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
  868. break;
  869. }
  870. return 0;
  871. }
  872. void
  873. TEIrelease(struct layer2 *l2)
  874. {
  875. struct teimgr *tm = l2->tm;
  876. u_long flags;
  877. mISDN_FsmDelTimer(&tm->timer, 1);
  878. write_lock_irqsave(&tm->mgr->lock, flags);
  879. list_del(&l2->list);
  880. write_unlock_irqrestore(&tm->mgr->lock, flags);
  881. l2->tm = NULL;
  882. kfree(tm);
  883. }
  884. static int
  885. create_teimgr(struct manager *mgr, struct channel_req *crq)
  886. {
  887. struct layer2 *l2;
  888. unsigned long opt = 0;
  889. unsigned long flags;
  890. int id;
  891. struct channel_req l1rq;
  892. if (*debug & DEBUG_L2_TEI)
  893. printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
  894. __func__, dev_name(&mgr->ch.st->dev->dev),
  895. crq->protocol, crq->adr.dev, crq->adr.channel,
  896. crq->adr.sapi, crq->adr.tei);
  897. if (crq->adr.tei > GROUP_TEI)
  898. return -EINVAL;
  899. if (crq->adr.tei < 64)
  900. test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
  901. if (crq->adr.tei == 0)
  902. test_and_set_bit(OPTION_L2_PTP, &opt);
  903. if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
  904. if (crq->protocol == ISDN_P_LAPD_TE)
  905. return -EPROTONOSUPPORT;
  906. if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
  907. return -EINVAL;
  908. if (mgr->up) {
  909. printk(KERN_WARNING
  910. "%s: only one network manager is allowed\n",
  911. __func__);
  912. return -EBUSY;
  913. }
  914. } else if (test_bit(MGR_OPT_USER, &mgr->options)) {
  915. if (crq->protocol == ISDN_P_LAPD_NT)
  916. return -EPROTONOSUPPORT;
  917. if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
  918. return -EINVAL; /* dyn tei */
  919. } else {
  920. if (crq->protocol == ISDN_P_LAPD_NT)
  921. test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
  922. if (crq->protocol == ISDN_P_LAPD_TE)
  923. test_and_set_bit(MGR_OPT_USER, &mgr->options);
  924. }
  925. l1rq.adr = crq->adr;
  926. if (mgr->ch.st->dev->Dprotocols
  927. & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
  928. test_and_set_bit(OPTION_L2_PMX, &opt);
  929. if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
  930. mgr->up = crq->ch;
  931. id = DL_INFO_L2_CONNECT;
  932. teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
  933. if (test_bit(MGR_PH_ACTIVE, &mgr->options))
  934. teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
  935. crq->ch = NULL;
  936. if (!list_empty(&mgr->layer2)) {
  937. read_lock_irqsave(&mgr->lock, flags);
  938. list_for_each_entry(l2, &mgr->layer2, list) {
  939. l2->up = mgr->up;
  940. l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
  941. }
  942. read_unlock_irqrestore(&mgr->lock, flags);
  943. }
  944. return 0;
  945. }
  946. l2 = create_l2(crq->ch, crq->protocol, opt,
  947. crq->adr.tei, crq->adr.sapi);
  948. if (!l2)
  949. return -ENOMEM;
  950. l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
  951. if (!l2->tm) {
  952. kfree(l2);
  953. printk(KERN_ERR "kmalloc teimgr failed\n");
  954. return -ENOMEM;
  955. }
  956. l2->tm->mgr = mgr;
  957. l2->tm->l2 = l2;
  958. l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
  959. l2->tm->tei_m.userdata = l2->tm;
  960. l2->tm->tei_m.printdebug = tei_debug;
  961. if (crq->protocol == ISDN_P_LAPD_TE) {
  962. l2->tm->tei_m.fsm = &teifsmu;
  963. l2->tm->tei_m.state = ST_TEI_NOP;
  964. l2->tm->tval = 1000; /* T201 1 sec */
  965. if (test_bit(OPTION_L2_PMX, &opt))
  966. l1rq.protocol = ISDN_P_TE_E1;
  967. else
  968. l1rq.protocol = ISDN_P_TE_S0;
  969. } else {
  970. l2->tm->tei_m.fsm = &teifsmn;
  971. l2->tm->tei_m.state = ST_TEI_NOP;
  972. l2->tm->tval = 2000; /* T202 2 sec */
  973. if (test_bit(OPTION_L2_PMX, &opt))
  974. l1rq.protocol = ISDN_P_NT_E1;
  975. else
  976. l1rq.protocol = ISDN_P_NT_S0;
  977. }
  978. mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
  979. write_lock_irqsave(&mgr->lock, flags);
  980. id = get_free_id(mgr);
  981. list_add_tail(&l2->list, &mgr->layer2);
  982. write_unlock_irqrestore(&mgr->lock, flags);
  983. if (id >= 0) {
  984. l2->ch.nr = id;
  985. l2->up->nr = id;
  986. crq->ch = &l2->ch;
  987. /* We need open here L1 for the manager as well (refcounting) */
  988. id = mgr->ch.st->own.ctrl(&mgr->ch.st->own, OPEN_CHANNEL,
  989. &l1rq);
  990. }
  991. if (id < 0)
  992. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  993. return id;
  994. }
  995. static int
  996. mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
  997. {
  998. struct manager *mgr;
  999. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1000. int ret = -EINVAL;
  1001. mgr = container_of(ch, struct manager, ch);
  1002. if (*debug & DEBUG_L2_RECV)
  1003. printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
  1004. __func__, hh->prim, hh->id);
  1005. switch (hh->prim) {
  1006. case PH_DATA_IND:
  1007. mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
  1008. ret = ph_data_ind(mgr, skb);
  1009. break;
  1010. case PH_DATA_CNF:
  1011. do_ack(mgr, hh->id);
  1012. ret = 0;
  1013. break;
  1014. case PH_ACTIVATE_IND:
  1015. test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
  1016. if (mgr->up)
  1017. teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
  1018. mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
  1019. do_send(mgr);
  1020. ret = 0;
  1021. break;
  1022. case PH_DEACTIVATE_IND:
  1023. test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
  1024. if (mgr->up)
  1025. teiup_create(mgr, PH_DEACTIVATE_IND, 0, NULL);
  1026. mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
  1027. ret = 0;
  1028. break;
  1029. case DL_UNITDATA_REQ:
  1030. return dl_unit_data(mgr, skb);
  1031. }
  1032. if (!ret)
  1033. dev_kfree_skb(skb);
  1034. return ret;
  1035. }
  1036. static int
  1037. free_teimanager(struct manager *mgr)
  1038. {
  1039. struct layer2 *l2, *nl2;
  1040. test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
  1041. if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
  1042. /* not locked lock is taken in release tei */
  1043. mgr->up = NULL;
  1044. if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
  1045. list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
  1046. put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
  1047. mutex_lock(&mgr->ch.st->lmutex);
  1048. list_del(&l2->ch.list);
  1049. mutex_unlock(&mgr->ch.st->lmutex);
  1050. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  1051. }
  1052. test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
  1053. } else {
  1054. list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
  1055. l2->up = NULL;
  1056. }
  1057. }
  1058. }
  1059. if (test_bit(MGR_OPT_USER, &mgr->options)) {
  1060. if (list_empty(&mgr->layer2))
  1061. test_and_clear_bit(MGR_OPT_USER, &mgr->options);
  1062. }
  1063. mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
  1064. return 0;
  1065. }
  1066. static int
  1067. ctrl_teimanager(struct manager *mgr, void *arg)
  1068. {
  1069. /* currently we only have one option */
  1070. unsigned int *val = (unsigned int *)arg;
  1071. switch (val[0]) {
  1072. case IMCLEAR_L2:
  1073. if (val[1])
  1074. test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
  1075. else
  1076. test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
  1077. break;
  1078. case IMHOLD_L1:
  1079. if (val[1])
  1080. test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
  1081. else
  1082. test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
  1083. break;
  1084. default:
  1085. return -EINVAL;
  1086. }
  1087. return 0;
  1088. }
  1089. /* This function does create a L2 for fixed TEI in NT Mode */
  1090. static int
  1091. check_data(struct manager *mgr, struct sk_buff *skb)
  1092. {
  1093. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1094. int ret, tei, sapi;
  1095. struct layer2 *l2;
  1096. if (*debug & DEBUG_L2_CTRL)
  1097. printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
  1098. __func__, hh->prim, hh->id);
  1099. if (test_bit(MGR_OPT_USER, &mgr->options))
  1100. return -ENOTCONN;
  1101. if (hh->prim != PH_DATA_IND)
  1102. return -ENOTCONN;
  1103. if (skb->len != 3)
  1104. return -ENOTCONN;
  1105. if (skb->data[0] & 3) /* EA0 and CR must be 0 */
  1106. return -EINVAL;
  1107. sapi = skb->data[0] >> 2;
  1108. if (!(skb->data[1] & 1)) /* invalid EA1 */
  1109. return -EINVAL;
  1110. tei = skb->data[1] >> 1;
  1111. if (tei > 63) /* not a fixed tei */
  1112. return -ENOTCONN;
  1113. if ((skb->data[2] & ~0x10) != SABME)
  1114. return -ENOTCONN;
  1115. /* We got a SABME for a fixed TEI */
  1116. if (*debug & DEBUG_L2_CTRL)
  1117. printk(KERN_DEBUG "%s: SABME sapi(%d) tei(%d)\n",
  1118. __func__, sapi, tei);
  1119. l2 = create_new_tei(mgr, tei, sapi);
  1120. if (!l2) {
  1121. if (*debug & DEBUG_L2_CTRL)
  1122. printk(KERN_DEBUG "%s: failed to create new tei\n",
  1123. __func__);
  1124. return -ENOMEM;
  1125. }
  1126. ret = l2->ch.send(&l2->ch, skb);
  1127. return ret;
  1128. }
  1129. void
  1130. delete_teimanager(struct mISDNchannel *ch)
  1131. {
  1132. struct manager *mgr;
  1133. struct layer2 *l2, *nl2;
  1134. mgr = container_of(ch, struct manager, ch);
  1135. /* not locked lock is taken in release tei */
  1136. list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
  1137. mutex_lock(&mgr->ch.st->lmutex);
  1138. list_del(&l2->ch.list);
  1139. mutex_unlock(&mgr->ch.st->lmutex);
  1140. l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
  1141. }
  1142. list_del(&mgr->ch.list);
  1143. list_del(&mgr->bcast.list);
  1144. skb_queue_purge(&mgr->sendq);
  1145. kfree(mgr);
  1146. }
  1147. static int
  1148. mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1149. {
  1150. struct manager *mgr;
  1151. int ret = -EINVAL;
  1152. mgr = container_of(ch, struct manager, ch);
  1153. if (*debug & DEBUG_L2_CTRL)
  1154. printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
  1155. switch (cmd) {
  1156. case OPEN_CHANNEL:
  1157. ret = create_teimgr(mgr, arg);
  1158. break;
  1159. case CLOSE_CHANNEL:
  1160. ret = free_teimanager(mgr);
  1161. break;
  1162. case CONTROL_CHANNEL:
  1163. ret = ctrl_teimanager(mgr, arg);
  1164. break;
  1165. case CHECK_DATA:
  1166. ret = check_data(mgr, arg);
  1167. break;
  1168. }
  1169. return ret;
  1170. }
  1171. static int
  1172. mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
  1173. {
  1174. struct manager *mgr = container_of(ch, struct manager, bcast);
  1175. struct mISDNhead *hhc, *hh = mISDN_HEAD_P(skb);
  1176. struct sk_buff *cskb = NULL;
  1177. struct layer2 *l2;
  1178. u_long flags;
  1179. int ret;
  1180. read_lock_irqsave(&mgr->lock, flags);
  1181. list_for_each_entry(l2, &mgr->layer2, list) {
  1182. if ((hh->id & MISDN_ID_SAPI_MASK) ==
  1183. (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
  1184. if (list_is_last(&l2->list, &mgr->layer2)) {
  1185. cskb = skb;
  1186. skb = NULL;
  1187. } else {
  1188. if (!cskb)
  1189. cskb = skb_copy(skb, GFP_ATOMIC);
  1190. }
  1191. if (cskb) {
  1192. hhc = mISDN_HEAD_P(cskb);
  1193. /* save original header behind normal header */
  1194. hhc++;
  1195. *hhc = *hh;
  1196. hhc--;
  1197. hhc->prim = DL_INTERN_MSG;
  1198. hhc->id = l2->ch.nr;
  1199. ret = ch->st->own.recv(&ch->st->own, cskb);
  1200. if (ret) {
  1201. if (*debug & DEBUG_SEND_ERR)
  1202. printk(KERN_DEBUG
  1203. "%s ch%d prim(%x) addr(%x)"
  1204. " err %d\n",
  1205. __func__, l2->ch.nr,
  1206. hh->prim, l2->ch.addr, ret);
  1207. } else
  1208. cskb = NULL;
  1209. } else {
  1210. printk(KERN_WARNING "%s ch%d addr %x no mem\n",
  1211. __func__, ch->nr, ch->addr);
  1212. goto out;
  1213. }
  1214. }
  1215. }
  1216. out:
  1217. read_unlock_irqrestore(&mgr->lock, flags);
  1218. if (cskb)
  1219. dev_kfree_skb(cskb);
  1220. if (skb)
  1221. dev_kfree_skb(skb);
  1222. return 0;
  1223. }
  1224. static int
  1225. mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1226. {
  1227. return -EINVAL;
  1228. }
  1229. int
  1230. create_teimanager(struct mISDNdevice *dev)
  1231. {
  1232. struct manager *mgr;
  1233. mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
  1234. if (!mgr)
  1235. return -ENOMEM;
  1236. INIT_LIST_HEAD(&mgr->layer2);
  1237. rwlock_init(&mgr->lock);
  1238. skb_queue_head_init(&mgr->sendq);
  1239. mgr->nextid = 1;
  1240. mgr->lastid = MISDN_ID_NONE;
  1241. mgr->ch.send = mgr_send;
  1242. mgr->ch.ctrl = mgr_ctrl;
  1243. mgr->ch.st = dev->D.st;
  1244. set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
  1245. add_layer2(&mgr->ch, dev->D.st);
  1246. mgr->bcast.send = mgr_bcast;
  1247. mgr->bcast.ctrl = mgr_bcast_ctrl;
  1248. mgr->bcast.st = dev->D.st;
  1249. set_channel_address(&mgr->bcast, 0, GROUP_TEI);
  1250. add_layer2(&mgr->bcast, dev->D.st);
  1251. mgr->deact.debug = *debug & DEBUG_MANAGER;
  1252. mgr->deact.userdata = mgr;
  1253. mgr->deact.printdebug = da_debug;
  1254. mgr->deact.fsm = &deactfsm;
  1255. mgr->deact.state = ST_L1_DEACT;
  1256. mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
  1257. dev->teimgr = &mgr->ch;
  1258. return 0;
  1259. }
  1260. int TEIInit(u_int *deb)
  1261. {
  1262. int res;
  1263. debug = deb;
  1264. teifsmu.state_count = TEI_STATE_COUNT;
  1265. teifsmu.event_count = TEI_EVENT_COUNT;
  1266. teifsmu.strEvent = strTeiEvent;
  1267. teifsmu.strState = strTeiState;
  1268. res = mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
  1269. if (res)
  1270. goto error;
  1271. teifsmn.state_count = TEI_STATE_COUNT;
  1272. teifsmn.event_count = TEI_EVENT_COUNT;
  1273. teifsmn.strEvent = strTeiEvent;
  1274. teifsmn.strState = strTeiState;
  1275. res = mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
  1276. if (res)
  1277. goto error_smn;
  1278. deactfsm.state_count = DEACT_STATE_COUNT;
  1279. deactfsm.event_count = DEACT_EVENT_COUNT;
  1280. deactfsm.strEvent = strDeactEvent;
  1281. deactfsm.strState = strDeactState;
  1282. res = mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
  1283. if (res)
  1284. goto error_deact;
  1285. return 0;
  1286. error_deact:
  1287. mISDN_FsmFree(&teifsmn);
  1288. error_smn:
  1289. mISDN_FsmFree(&teifsmu);
  1290. error:
  1291. return res;
  1292. }
  1293. void TEIFree(void)
  1294. {
  1295. mISDN_FsmFree(&teifsmu);
  1296. mISDN_FsmFree(&teifsmn);
  1297. mISDN_FsmFree(&deactfsm);
  1298. }