isdnl3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /* $Id: isdnl3.c,v 2.22.2.3 2004/01/13 14:31:25 keil Exp $
  2. *
  3. * Author Karsten Keil
  4. * based on the teles driver from Jan den Ouden
  5. * Copyright by Karsten Keil <keil@isdn4linux.de>
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. * For changes and modifications please read
  11. * Documentation/isdn/HiSax.cert
  12. *
  13. * Thanks to Jan den Ouden
  14. * Fritz Elfert
  15. *
  16. */
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include "hisax.h"
  20. #include "isdnl3.h"
  21. const char *l3_revision = "$Revision: 2.22.2.3 $";
  22. static struct Fsm l3fsm;
  23. enum {
  24. ST_L3_LC_REL,
  25. ST_L3_LC_ESTAB_WAIT,
  26. ST_L3_LC_REL_DELAY,
  27. ST_L3_LC_REL_WAIT,
  28. ST_L3_LC_ESTAB,
  29. };
  30. #define L3_STATE_COUNT (ST_L3_LC_ESTAB + 1)
  31. static char *strL3State[] =
  32. {
  33. "ST_L3_LC_REL",
  34. "ST_L3_LC_ESTAB_WAIT",
  35. "ST_L3_LC_REL_DELAY",
  36. "ST_L3_LC_REL_WAIT",
  37. "ST_L3_LC_ESTAB",
  38. };
  39. enum {
  40. EV_ESTABLISH_REQ,
  41. EV_ESTABLISH_IND,
  42. EV_ESTABLISH_CNF,
  43. EV_RELEASE_REQ,
  44. EV_RELEASE_CNF,
  45. EV_RELEASE_IND,
  46. EV_TIMEOUT,
  47. };
  48. #define L3_EVENT_COUNT (EV_TIMEOUT + 1)
  49. static char *strL3Event[] =
  50. {
  51. "EV_ESTABLISH_REQ",
  52. "EV_ESTABLISH_IND",
  53. "EV_ESTABLISH_CNF",
  54. "EV_RELEASE_REQ",
  55. "EV_RELEASE_CNF",
  56. "EV_RELEASE_IND",
  57. "EV_TIMEOUT",
  58. };
  59. static __printf(2, 3) void
  60. l3m_debug(struct FsmInst *fi, char *fmt, ...)
  61. {
  62. va_list args;
  63. struct PStack *st = fi->userdata;
  64. va_start(args, fmt);
  65. VHiSax_putstatus(st->l1.hardware, st->l3.debug_id, fmt, args);
  66. va_end(args);
  67. }
  68. u_char *
  69. findie(u_char *p, int size, u_char ie, int wanted_set)
  70. {
  71. int l, codeset, maincodeset;
  72. u_char *pend = p + size;
  73. /* skip protocol discriminator, callref and message type */
  74. p++;
  75. l = (*p++) & 0xf;
  76. p += l;
  77. p++;
  78. codeset = 0;
  79. maincodeset = 0;
  80. /* while there are bytes left... */
  81. while (p < pend) {
  82. if ((*p & 0xf0) == 0x90) {
  83. codeset = *p & 0x07;
  84. if (!(*p & 0x08))
  85. maincodeset = codeset;
  86. }
  87. if (*p & 0x80)
  88. p++;
  89. else {
  90. if (codeset == wanted_set) {
  91. if (*p == ie)
  92. { /* improved length check (Werner Cornelius) */
  93. if ((pend - p) < 2)
  94. return (NULL);
  95. if (*(p + 1) > (pend - (p + 2)))
  96. return (NULL);
  97. return (p);
  98. }
  99. if (*p > ie)
  100. return (NULL);
  101. }
  102. p++;
  103. l = *p++;
  104. p += l;
  105. codeset = maincodeset;
  106. }
  107. }
  108. return (NULL);
  109. }
  110. int
  111. getcallref(u_char *p)
  112. {
  113. int l, cr = 0;
  114. p++; /* prot discr */
  115. if (*p & 0xfe) /* wrong callref BRI only 1 octet*/
  116. return (-2);
  117. l = 0xf & *p++; /* callref length */
  118. if (!l) /* dummy CallRef */
  119. return (-1);
  120. cr = *p++;
  121. return (cr);
  122. }
  123. static int OrigCallRef = 0;
  124. int
  125. newcallref(void)
  126. {
  127. if (OrigCallRef == 127)
  128. OrigCallRef = 1;
  129. else
  130. OrigCallRef++;
  131. return (OrigCallRef);
  132. }
  133. void
  134. newl3state(struct l3_process *pc, int state)
  135. {
  136. if (pc->debug & L3_DEB_STATE)
  137. l3_debug(pc->st, "%s cr %d %d --> %d", __func__,
  138. pc->callref & 0x7F,
  139. pc->state, state);
  140. pc->state = state;
  141. }
  142. static void
  143. L3ExpireTimer(struct timer_list *timer)
  144. {
  145. struct L3Timer *t = from_timer(t, timer, tl);
  146. t->pc->st->lli.l4l3(t->pc->st, t->event, t->pc);
  147. }
  148. void
  149. L3InitTimer(struct l3_process *pc, struct L3Timer *t)
  150. {
  151. t->pc = pc;
  152. timer_setup(&t->tl, L3ExpireTimer, 0);
  153. }
  154. void
  155. L3DelTimer(struct L3Timer *t)
  156. {
  157. del_timer(&t->tl);
  158. }
  159. int
  160. L3AddTimer(struct L3Timer *t,
  161. int millisec, int event)
  162. {
  163. if (timer_pending(&t->tl)) {
  164. printk(KERN_WARNING "L3AddTimer: timer already active!\n");
  165. return -1;
  166. }
  167. t->event = event;
  168. t->tl.expires = jiffies + (millisec * HZ) / 1000;
  169. add_timer(&t->tl);
  170. return 0;
  171. }
  172. void
  173. StopAllL3Timer(struct l3_process *pc)
  174. {
  175. L3DelTimer(&pc->timer);
  176. }
  177. struct sk_buff *
  178. l3_alloc_skb(int len)
  179. {
  180. struct sk_buff *skb;
  181. if (!(skb = alloc_skb(len + MAX_HEADER_LEN, GFP_ATOMIC))) {
  182. printk(KERN_WARNING "HiSax: No skb for D-channel\n");
  183. return (NULL);
  184. }
  185. skb_reserve(skb, MAX_HEADER_LEN);
  186. return (skb);
  187. }
  188. static void
  189. no_l3_proto(struct PStack *st, int pr, void *arg)
  190. {
  191. struct sk_buff *skb = arg;
  192. HiSax_putstatus(st->l1.hardware, "L3", "no D protocol");
  193. if (skb) {
  194. dev_kfree_skb(skb);
  195. }
  196. }
  197. static int
  198. no_l3_proto_spec(struct PStack *st, isdn_ctrl *ic)
  199. {
  200. printk(KERN_WARNING "HiSax: no specific protocol handler for proto %lu\n", ic->arg & 0xFF);
  201. return (-1);
  202. }
  203. struct l3_process
  204. *getl3proc(struct PStack *st, int cr)
  205. {
  206. struct l3_process *p = st->l3.proc;
  207. while (p)
  208. if (p->callref == cr)
  209. return (p);
  210. else
  211. p = p->next;
  212. return (NULL);
  213. }
  214. struct l3_process
  215. *new_l3_process(struct PStack *st, int cr)
  216. {
  217. struct l3_process *p, *np;
  218. if (!(p = kmalloc(sizeof(struct l3_process), GFP_ATOMIC))) {
  219. printk(KERN_ERR "HiSax can't get memory for cr %d\n", cr);
  220. return (NULL);
  221. }
  222. if (!st->l3.proc)
  223. st->l3.proc = p;
  224. else {
  225. np = st->l3.proc;
  226. while (np->next)
  227. np = np->next;
  228. np->next = p;
  229. }
  230. p->next = NULL;
  231. p->debug = st->l3.debug;
  232. p->callref = cr;
  233. p->state = 0;
  234. p->chan = NULL;
  235. p->st = st;
  236. p->N303 = st->l3.N303;
  237. L3InitTimer(p, &p->timer);
  238. return (p);
  239. };
  240. void
  241. release_l3_process(struct l3_process *p)
  242. {
  243. struct l3_process *np, *pp = NULL;
  244. if (!p)
  245. return;
  246. np = p->st->l3.proc;
  247. while (np) {
  248. if (np == p) {
  249. StopAllL3Timer(p);
  250. if (pp)
  251. pp->next = np->next;
  252. else if (!(p->st->l3.proc = np->next) &&
  253. !test_bit(FLG_PTP, &p->st->l2.flag)) {
  254. if (p->debug)
  255. l3_debug(p->st, "release_l3_process: last process");
  256. if (skb_queue_empty(&p->st->l3.squeue)) {
  257. if (p->debug)
  258. l3_debug(p->st, "release_l3_process: release link");
  259. if (p->st->protocol != ISDN_PTYPE_NI1)
  260. FsmEvent(&p->st->l3.l3m, EV_RELEASE_REQ, NULL);
  261. else
  262. FsmEvent(&p->st->l3.l3m, EV_RELEASE_IND, NULL);
  263. } else {
  264. if (p->debug)
  265. l3_debug(p->st, "release_l3_process: not release link");
  266. }
  267. }
  268. kfree(p);
  269. return;
  270. }
  271. pp = np;
  272. np = np->next;
  273. }
  274. printk(KERN_ERR "HiSax internal L3 error CR(%d) not in list\n", p->callref);
  275. l3_debug(p->st, "HiSax internal L3 error CR(%d) not in list", p->callref);
  276. };
  277. static void
  278. l3ml3p(struct PStack *st, int pr)
  279. {
  280. struct l3_process *p = st->l3.proc;
  281. struct l3_process *np;
  282. while (p) {
  283. /* p might be kfreed under us, so we need to save where we want to go on */
  284. np = p->next;
  285. st->l3.l3ml3(st, pr, p);
  286. p = np;
  287. }
  288. }
  289. void
  290. setstack_l3dc(struct PStack *st, struct Channel *chanp)
  291. {
  292. char tmp[64];
  293. st->l3.proc = NULL;
  294. st->l3.global = NULL;
  295. skb_queue_head_init(&st->l3.squeue);
  296. st->l3.l3m.fsm = &l3fsm;
  297. st->l3.l3m.state = ST_L3_LC_REL;
  298. st->l3.l3m.debug = 1;
  299. st->l3.l3m.userdata = st;
  300. st->l3.l3m.userint = 0;
  301. st->l3.l3m.printdebug = l3m_debug;
  302. FsmInitTimer(&st->l3.l3m, &st->l3.l3m_timer);
  303. strcpy(st->l3.debug_id, "L3DC ");
  304. st->lli.l4l3_proto = no_l3_proto_spec;
  305. #ifdef CONFIG_HISAX_EURO
  306. if (st->protocol == ISDN_PTYPE_EURO) {
  307. setstack_dss1(st);
  308. } else
  309. #endif
  310. #ifdef CONFIG_HISAX_NI1
  311. if (st->protocol == ISDN_PTYPE_NI1) {
  312. setstack_ni1(st);
  313. } else
  314. #endif
  315. #ifdef CONFIG_HISAX_1TR6
  316. if (st->protocol == ISDN_PTYPE_1TR6) {
  317. setstack_1tr6(st);
  318. } else
  319. #endif
  320. if (st->protocol == ISDN_PTYPE_LEASED) {
  321. st->lli.l4l3 = no_l3_proto;
  322. st->l2.l2l3 = no_l3_proto;
  323. st->l3.l3ml3 = no_l3_proto;
  324. printk(KERN_INFO "HiSax: Leased line mode\n");
  325. } else {
  326. st->lli.l4l3 = no_l3_proto;
  327. st->l2.l2l3 = no_l3_proto;
  328. st->l3.l3ml3 = no_l3_proto;
  329. sprintf(tmp, "protocol %s not supported",
  330. (st->protocol == ISDN_PTYPE_1TR6) ? "1tr6" :
  331. (st->protocol == ISDN_PTYPE_EURO) ? "euro" :
  332. (st->protocol == ISDN_PTYPE_NI1) ? "ni1" :
  333. "unknown");
  334. printk(KERN_WARNING "HiSax: %s\n", tmp);
  335. st->protocol = -1;
  336. }
  337. }
  338. static void
  339. isdnl3_trans(struct PStack *st, int pr, void *arg) {
  340. st->l3.l3l2(st, pr, arg);
  341. }
  342. void
  343. releasestack_isdnl3(struct PStack *st)
  344. {
  345. while (st->l3.proc)
  346. release_l3_process(st->l3.proc);
  347. if (st->l3.global) {
  348. StopAllL3Timer(st->l3.global);
  349. kfree(st->l3.global);
  350. st->l3.global = NULL;
  351. }
  352. FsmDelTimer(&st->l3.l3m_timer, 54);
  353. skb_queue_purge(&st->l3.squeue);
  354. }
  355. void
  356. setstack_l3bc(struct PStack *st, struct Channel *chanp)
  357. {
  358. st->l3.proc = NULL;
  359. st->l3.global = NULL;
  360. skb_queue_head_init(&st->l3.squeue);
  361. st->l3.l3m.fsm = &l3fsm;
  362. st->l3.l3m.state = ST_L3_LC_REL;
  363. st->l3.l3m.debug = 1;
  364. st->l3.l3m.userdata = st;
  365. st->l3.l3m.userint = 0;
  366. st->l3.l3m.printdebug = l3m_debug;
  367. strcpy(st->l3.debug_id, "L3BC ");
  368. st->lli.l4l3 = isdnl3_trans;
  369. }
  370. #define DREL_TIMER_VALUE 40000
  371. static void
  372. lc_activate(struct FsmInst *fi, int event, void *arg)
  373. {
  374. struct PStack *st = fi->userdata;
  375. FsmChangeState(fi, ST_L3_LC_ESTAB_WAIT);
  376. st->l3.l3l2(st, DL_ESTABLISH | REQUEST, NULL);
  377. }
  378. static void
  379. lc_connect(struct FsmInst *fi, int event, void *arg)
  380. {
  381. struct PStack *st = fi->userdata;
  382. struct sk_buff *skb = arg;
  383. int dequeued = 0;
  384. FsmChangeState(fi, ST_L3_LC_ESTAB);
  385. while ((skb = skb_dequeue(&st->l3.squeue))) {
  386. st->l3.l3l2(st, DL_DATA | REQUEST, skb);
  387. dequeued++;
  388. }
  389. if ((!st->l3.proc) && dequeued) {
  390. if (st->l3.debug)
  391. l3_debug(st, "lc_connect: release link");
  392. FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
  393. } else
  394. l3ml3p(st, DL_ESTABLISH | INDICATION);
  395. }
  396. static void
  397. lc_connected(struct FsmInst *fi, int event, void *arg)
  398. {
  399. struct PStack *st = fi->userdata;
  400. struct sk_buff *skb = arg;
  401. int dequeued = 0;
  402. FsmDelTimer(&st->l3.l3m_timer, 51);
  403. FsmChangeState(fi, ST_L3_LC_ESTAB);
  404. while ((skb = skb_dequeue(&st->l3.squeue))) {
  405. st->l3.l3l2(st, DL_DATA | REQUEST, skb);
  406. dequeued++;
  407. }
  408. if ((!st->l3.proc) && dequeued) {
  409. if (st->l3.debug)
  410. l3_debug(st, "lc_connected: release link");
  411. FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
  412. } else
  413. l3ml3p(st, DL_ESTABLISH | CONFIRM);
  414. }
  415. static void
  416. lc_start_delay(struct FsmInst *fi, int event, void *arg)
  417. {
  418. struct PStack *st = fi->userdata;
  419. FsmChangeState(fi, ST_L3_LC_REL_DELAY);
  420. FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 50);
  421. }
  422. static void
  423. lc_start_delay_check(struct FsmInst *fi, int event, void *arg)
  424. /* 20/09/00 - GE timer not user for NI-1 as layer 2 should stay up */
  425. {
  426. struct PStack *st = fi->userdata;
  427. FsmChangeState(fi, ST_L3_LC_REL_DELAY);
  428. /* 19/09/00 - GE timer not user for NI-1 */
  429. if (st->protocol != ISDN_PTYPE_NI1)
  430. FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 50);
  431. }
  432. static void
  433. lc_release_req(struct FsmInst *fi, int event, void *arg)
  434. {
  435. struct PStack *st = fi->userdata;
  436. if (test_bit(FLG_L2BLOCK, &st->l2.flag)) {
  437. if (st->l3.debug)
  438. l3_debug(st, "lc_release_req: l2 blocked");
  439. /* restart release timer */
  440. FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 51);
  441. } else {
  442. FsmChangeState(fi, ST_L3_LC_REL_WAIT);
  443. st->l3.l3l2(st, DL_RELEASE | REQUEST, NULL);
  444. }
  445. }
  446. static void
  447. lc_release_ind(struct FsmInst *fi, int event, void *arg)
  448. {
  449. struct PStack *st = fi->userdata;
  450. FsmDelTimer(&st->l3.l3m_timer, 52);
  451. FsmChangeState(fi, ST_L3_LC_REL);
  452. skb_queue_purge(&st->l3.squeue);
  453. l3ml3p(st, DL_RELEASE | INDICATION);
  454. }
  455. static void
  456. lc_release_cnf(struct FsmInst *fi, int event, void *arg)
  457. {
  458. struct PStack *st = fi->userdata;
  459. FsmChangeState(fi, ST_L3_LC_REL);
  460. skb_queue_purge(&st->l3.squeue);
  461. l3ml3p(st, DL_RELEASE | CONFIRM);
  462. }
  463. /* *INDENT-OFF* */
  464. static struct FsmNode L3FnList[] __initdata =
  465. {
  466. {ST_L3_LC_REL, EV_ESTABLISH_REQ, lc_activate},
  467. {ST_L3_LC_REL, EV_ESTABLISH_IND, lc_connect},
  468. {ST_L3_LC_REL, EV_ESTABLISH_CNF, lc_connect},
  469. {ST_L3_LC_ESTAB_WAIT, EV_ESTABLISH_CNF, lc_connected},
  470. {ST_L3_LC_ESTAB_WAIT, EV_RELEASE_REQ, lc_start_delay},
  471. {ST_L3_LC_ESTAB_WAIT, EV_RELEASE_IND, lc_release_ind},
  472. {ST_L3_LC_ESTAB, EV_RELEASE_IND, lc_release_ind},
  473. {ST_L3_LC_ESTAB, EV_RELEASE_REQ, lc_start_delay_check},
  474. {ST_L3_LC_REL_DELAY, EV_RELEASE_IND, lc_release_ind},
  475. {ST_L3_LC_REL_DELAY, EV_ESTABLISH_REQ, lc_connected},
  476. {ST_L3_LC_REL_DELAY, EV_TIMEOUT, lc_release_req},
  477. {ST_L3_LC_REL_WAIT, EV_RELEASE_CNF, lc_release_cnf},
  478. {ST_L3_LC_REL_WAIT, EV_ESTABLISH_REQ, lc_activate},
  479. };
  480. /* *INDENT-ON* */
  481. void
  482. l3_msg(struct PStack *st, int pr, void *arg)
  483. {
  484. switch (pr) {
  485. case (DL_DATA | REQUEST):
  486. if (st->l3.l3m.state == ST_L3_LC_ESTAB) {
  487. st->l3.l3l2(st, pr, arg);
  488. } else {
  489. struct sk_buff *skb = arg;
  490. skb_queue_tail(&st->l3.squeue, skb);
  491. FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
  492. }
  493. break;
  494. case (DL_ESTABLISH | REQUEST):
  495. FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
  496. break;
  497. case (DL_ESTABLISH | CONFIRM):
  498. FsmEvent(&st->l3.l3m, EV_ESTABLISH_CNF, NULL);
  499. break;
  500. case (DL_ESTABLISH | INDICATION):
  501. FsmEvent(&st->l3.l3m, EV_ESTABLISH_IND, NULL);
  502. break;
  503. case (DL_RELEASE | INDICATION):
  504. FsmEvent(&st->l3.l3m, EV_RELEASE_IND, NULL);
  505. break;
  506. case (DL_RELEASE | CONFIRM):
  507. FsmEvent(&st->l3.l3m, EV_RELEASE_CNF, NULL);
  508. break;
  509. case (DL_RELEASE | REQUEST):
  510. FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
  511. break;
  512. }
  513. }
  514. int __init
  515. Isdnl3New(void)
  516. {
  517. l3fsm.state_count = L3_STATE_COUNT;
  518. l3fsm.event_count = L3_EVENT_COUNT;
  519. l3fsm.strEvent = strL3Event;
  520. l3fsm.strState = strL3State;
  521. return FsmNew(&l3fsm, L3FnList, ARRAY_SIZE(L3FnList));
  522. }
  523. void
  524. Isdnl3Free(void)
  525. {
  526. FsmFree(&l3fsm);
  527. }