isdnl3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 "hisax.h"
  19. #include "isdnl3.h"
  20. #include <linux/config.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 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, "newstate cr %d %d --> %d",
  138. pc->callref & 0x7F,
  139. pc->state, state);
  140. pc->state = state;
  141. }
  142. static void
  143. L3ExpireTimer(struct L3Timer *t)
  144. {
  145. t->pc->st->lli.l4l3(t->pc->st, t->event, t->pc);
  146. }
  147. void
  148. L3InitTimer(struct l3_process *pc, struct L3Timer *t)
  149. {
  150. t->pc = pc;
  151. t->tl.function = (void *) L3ExpireTimer;
  152. t->tl.data = (long) t;
  153. init_timer(&t->tl);
  154. }
  155. void
  156. L3DelTimer(struct L3Timer *t)
  157. {
  158. del_timer(&t->tl);
  159. }
  160. int
  161. L3AddTimer(struct L3Timer *t,
  162. int millisec, int event)
  163. {
  164. if (timer_pending(&t->tl)) {
  165. printk(KERN_WARNING "L3AddTimer: timer already active!\n");
  166. return -1;
  167. }
  168. init_timer(&t->tl);
  169. t->event = event;
  170. t->tl.expires = jiffies + (millisec * HZ) / 1000;
  171. add_timer(&t->tl);
  172. return 0;
  173. }
  174. void
  175. StopAllL3Timer(struct l3_process *pc)
  176. {
  177. L3DelTimer(&pc->timer);
  178. }
  179. struct sk_buff *
  180. l3_alloc_skb(int len)
  181. {
  182. struct sk_buff *skb;
  183. if (!(skb = alloc_skb(len + MAX_HEADER_LEN, GFP_ATOMIC))) {
  184. printk(KERN_WARNING "HiSax: No skb for D-channel\n");
  185. return (NULL);
  186. }
  187. skb_reserve(skb, MAX_HEADER_LEN);
  188. return (skb);
  189. }
  190. static void
  191. no_l3_proto(struct PStack *st, int pr, void *arg)
  192. {
  193. struct sk_buff *skb = arg;
  194. HiSax_putstatus(st->l1.hardware, "L3", "no D protocol");
  195. if (skb) {
  196. dev_kfree_skb(skb);
  197. }
  198. }
  199. static int
  200. no_l3_proto_spec(struct PStack *st, isdn_ctrl *ic)
  201. {
  202. printk(KERN_WARNING "HiSax: no specific protocol handler for proto %lu\n",ic->arg & 0xFF);
  203. return(-1);
  204. }
  205. #ifdef CONFIG_HISAX_EURO
  206. extern void setstack_dss1(struct PStack *st);
  207. #endif
  208. #ifdef CONFIG_HISAX_NI1
  209. extern void setstack_ni1(struct PStack *st);
  210. #endif
  211. #ifdef CONFIG_HISAX_1TR6
  212. extern void setstack_1tr6(struct PStack *st);
  213. #endif
  214. struct l3_process
  215. *getl3proc(struct PStack *st, int cr)
  216. {
  217. struct l3_process *p = st->l3.proc;
  218. while (p)
  219. if (p->callref == cr)
  220. return (p);
  221. else
  222. p = p->next;
  223. return (NULL);
  224. }
  225. struct l3_process
  226. *new_l3_process(struct PStack *st, int cr)
  227. {
  228. struct l3_process *p, *np;
  229. if (!(p = kmalloc(sizeof(struct l3_process), GFP_ATOMIC))) {
  230. printk(KERN_ERR "HiSax can't get memory for cr %d\n", cr);
  231. return (NULL);
  232. }
  233. if (!st->l3.proc)
  234. st->l3.proc = p;
  235. else {
  236. np = st->l3.proc;
  237. while (np->next)
  238. np = np->next;
  239. np->next = p;
  240. }
  241. p->next = NULL;
  242. p->debug = st->l3.debug;
  243. p->callref = cr;
  244. p->state = 0;
  245. p->chan = NULL;
  246. p->st = st;
  247. p->N303 = st->l3.N303;
  248. L3InitTimer(p, &p->timer);
  249. return (p);
  250. };
  251. void
  252. release_l3_process(struct l3_process *p)
  253. {
  254. struct l3_process *np, *pp = NULL;
  255. if (!p)
  256. return;
  257. np = p->st->l3.proc;
  258. while (np) {
  259. if (np == p) {
  260. StopAllL3Timer(p);
  261. if (pp)
  262. pp->next = np->next;
  263. else if (!(p->st->l3.proc = np->next) &&
  264. !test_bit(FLG_PTP, &p->st->l2.flag)) {
  265. if (p->debug)
  266. l3_debug(p->st, "release_l3_process: last process");
  267. if (skb_queue_empty(&p->st->l3.squeue)) {
  268. if (p->debug)
  269. l3_debug(p->st, "release_l3_process: release link");
  270. if (p->st->protocol != ISDN_PTYPE_NI1)
  271. FsmEvent(&p->st->l3.l3m, EV_RELEASE_REQ, NULL);
  272. else
  273. FsmEvent(&p->st->l3.l3m, EV_RELEASE_IND, NULL);
  274. } else {
  275. if (p->debug)
  276. l3_debug(p->st, "release_l3_process: not release link");
  277. }
  278. }
  279. kfree(p);
  280. return;
  281. }
  282. pp = np;
  283. np = np->next;
  284. }
  285. printk(KERN_ERR "HiSax internal L3 error CR(%d) not in list\n", p->callref);
  286. l3_debug(p->st, "HiSax internal L3 error CR(%d) not in list", p->callref);
  287. };
  288. static void
  289. l3ml3p(struct PStack *st, int pr)
  290. {
  291. struct l3_process *p = st->l3.proc;
  292. struct l3_process *np;
  293. while (p) {
  294. /* p might be kfreed under us, so we need to save where we want to go on */
  295. np = p->next;
  296. st->l3.l3ml3(st, pr, p);
  297. p = np;
  298. }
  299. }
  300. void
  301. setstack_l3dc(struct PStack *st, struct Channel *chanp)
  302. {
  303. char tmp[64];
  304. st->l3.proc = NULL;
  305. st->l3.global = NULL;
  306. skb_queue_head_init(&st->l3.squeue);
  307. st->l3.l3m.fsm = &l3fsm;
  308. st->l3.l3m.state = ST_L3_LC_REL;
  309. st->l3.l3m.debug = 1;
  310. st->l3.l3m.userdata = st;
  311. st->l3.l3m.userint = 0;
  312. st->l3.l3m.printdebug = l3m_debug;
  313. FsmInitTimer(&st->l3.l3m, &st->l3.l3m_timer);
  314. strcpy(st->l3.debug_id, "L3DC ");
  315. st->lli.l4l3_proto = no_l3_proto_spec;
  316. #ifdef CONFIG_HISAX_EURO
  317. if (st->protocol == ISDN_PTYPE_EURO) {
  318. setstack_dss1(st);
  319. } else
  320. #endif
  321. #ifdef CONFIG_HISAX_NI1
  322. if (st->protocol == ISDN_PTYPE_NI1) {
  323. setstack_ni1(st);
  324. } else
  325. #endif
  326. #ifdef CONFIG_HISAX_1TR6
  327. if (st->protocol == ISDN_PTYPE_1TR6) {
  328. setstack_1tr6(st);
  329. } else
  330. #endif
  331. if (st->protocol == ISDN_PTYPE_LEASED) {
  332. st->lli.l4l3 = no_l3_proto;
  333. st->l2.l2l3 = no_l3_proto;
  334. st->l3.l3ml3 = no_l3_proto;
  335. printk(KERN_INFO "HiSax: Leased line mode\n");
  336. } else {
  337. st->lli.l4l3 = no_l3_proto;
  338. st->l2.l2l3 = no_l3_proto;
  339. st->l3.l3ml3 = no_l3_proto;
  340. sprintf(tmp, "protocol %s not supported",
  341. (st->protocol == ISDN_PTYPE_1TR6) ? "1tr6" :
  342. (st->protocol == ISDN_PTYPE_EURO) ? "euro" :
  343. (st->protocol == ISDN_PTYPE_NI1) ? "ni1" :
  344. "unknown");
  345. printk(KERN_WARNING "HiSax: %s\n", tmp);
  346. st->protocol = -1;
  347. }
  348. }
  349. static void
  350. isdnl3_trans(struct PStack *st, int pr, void *arg) {
  351. st->l3.l3l2(st, pr, arg);
  352. }
  353. void
  354. releasestack_isdnl3(struct PStack *st)
  355. {
  356. while (st->l3.proc)
  357. release_l3_process(st->l3.proc);
  358. if (st->l3.global) {
  359. StopAllL3Timer(st->l3.global);
  360. kfree(st->l3.global);
  361. st->l3.global = NULL;
  362. }
  363. FsmDelTimer(&st->l3.l3m_timer, 54);
  364. skb_queue_purge(&st->l3.squeue);
  365. }
  366. void
  367. setstack_l3bc(struct PStack *st, struct Channel *chanp)
  368. {
  369. st->l3.proc = NULL;
  370. st->l3.global = NULL;
  371. skb_queue_head_init(&st->l3.squeue);
  372. st->l3.l3m.fsm = &l3fsm;
  373. st->l3.l3m.state = ST_L3_LC_REL;
  374. st->l3.l3m.debug = 1;
  375. st->l3.l3m.userdata = st;
  376. st->l3.l3m.userint = 0;
  377. st->l3.l3m.printdebug = l3m_debug;
  378. strcpy(st->l3.debug_id, "L3BC ");
  379. st->lli.l4l3 = isdnl3_trans;
  380. }
  381. #define DREL_TIMER_VALUE 40000
  382. static void
  383. lc_activate(struct FsmInst *fi, int event, void *arg)
  384. {
  385. struct PStack *st = fi->userdata;
  386. FsmChangeState(fi, ST_L3_LC_ESTAB_WAIT);
  387. st->l3.l3l2(st, DL_ESTABLISH | REQUEST, NULL);
  388. }
  389. static void
  390. lc_connect(struct FsmInst *fi, int event, void *arg)
  391. {
  392. struct PStack *st = fi->userdata;
  393. struct sk_buff *skb = arg;
  394. int dequeued = 0;
  395. FsmChangeState(fi, ST_L3_LC_ESTAB);
  396. while ((skb = skb_dequeue(&st->l3.squeue))) {
  397. st->l3.l3l2(st, DL_DATA | REQUEST, skb);
  398. dequeued++;
  399. }
  400. if ((!st->l3.proc) && dequeued) {
  401. if (st->l3.debug)
  402. l3_debug(st, "lc_connect: release link");
  403. FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
  404. } else
  405. l3ml3p(st, DL_ESTABLISH | INDICATION);
  406. }
  407. static void
  408. lc_connected(struct FsmInst *fi, int event, void *arg)
  409. {
  410. struct PStack *st = fi->userdata;
  411. struct sk_buff *skb = arg;
  412. int dequeued = 0;
  413. FsmDelTimer(&st->l3.l3m_timer, 51);
  414. FsmChangeState(fi, ST_L3_LC_ESTAB);
  415. while ((skb = skb_dequeue(&st->l3.squeue))) {
  416. st->l3.l3l2(st, DL_DATA | REQUEST, skb);
  417. dequeued++;
  418. }
  419. if ((!st->l3.proc) && dequeued) {
  420. if (st->l3.debug)
  421. l3_debug(st, "lc_connected: release link");
  422. FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
  423. } else
  424. l3ml3p(st, DL_ESTABLISH | CONFIRM);
  425. }
  426. static void
  427. lc_start_delay(struct FsmInst *fi, int event, void *arg)
  428. {
  429. struct PStack *st = fi->userdata;
  430. FsmChangeState(fi, ST_L3_LC_REL_DELAY);
  431. FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 50);
  432. }
  433. static void
  434. lc_start_delay_check(struct FsmInst *fi, int event, void *arg)
  435. /* 20/09/00 - GE timer not user for NI-1 as layer 2 should stay up */
  436. {
  437. struct PStack *st = fi->userdata;
  438. FsmChangeState(fi, ST_L3_LC_REL_DELAY);
  439. /* 19/09/00 - GE timer not user for NI-1 */
  440. if (st->protocol != ISDN_PTYPE_NI1)
  441. FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 50);
  442. }
  443. static void
  444. lc_release_req(struct FsmInst *fi, int event, void *arg)
  445. {
  446. struct PStack *st = fi->userdata;
  447. if (test_bit(FLG_L2BLOCK, &st->l2.flag)) {
  448. if (st->l3.debug)
  449. l3_debug(st, "lc_release_req: l2 blocked");
  450. /* restart release timer */
  451. FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 51);
  452. } else {
  453. FsmChangeState(fi, ST_L3_LC_REL_WAIT);
  454. st->l3.l3l2(st, DL_RELEASE | REQUEST, NULL);
  455. }
  456. }
  457. static void
  458. lc_release_ind(struct FsmInst *fi, int event, void *arg)
  459. {
  460. struct PStack *st = fi->userdata;
  461. FsmDelTimer(&st->l3.l3m_timer, 52);
  462. FsmChangeState(fi, ST_L3_LC_REL);
  463. skb_queue_purge(&st->l3.squeue);
  464. l3ml3p(st, DL_RELEASE | INDICATION);
  465. }
  466. static void
  467. lc_release_cnf(struct FsmInst *fi, int event, void *arg)
  468. {
  469. struct PStack *st = fi->userdata;
  470. FsmChangeState(fi, ST_L3_LC_REL);
  471. skb_queue_purge(&st->l3.squeue);
  472. l3ml3p(st, DL_RELEASE | CONFIRM);
  473. }
  474. /* *INDENT-OFF* */
  475. static struct FsmNode L3FnList[] __initdata =
  476. {
  477. {ST_L3_LC_REL, EV_ESTABLISH_REQ, lc_activate},
  478. {ST_L3_LC_REL, EV_ESTABLISH_IND, lc_connect},
  479. {ST_L3_LC_REL, EV_ESTABLISH_CNF, lc_connect},
  480. {ST_L3_LC_ESTAB_WAIT, EV_ESTABLISH_CNF, lc_connected},
  481. {ST_L3_LC_ESTAB_WAIT, EV_RELEASE_REQ, lc_start_delay},
  482. {ST_L3_LC_ESTAB_WAIT, EV_RELEASE_IND, lc_release_ind},
  483. {ST_L3_LC_ESTAB, EV_RELEASE_IND, lc_release_ind},
  484. {ST_L3_LC_ESTAB, EV_RELEASE_REQ, lc_start_delay_check},
  485. {ST_L3_LC_REL_DELAY, EV_RELEASE_IND, lc_release_ind},
  486. {ST_L3_LC_REL_DELAY, EV_ESTABLISH_REQ, lc_connected},
  487. {ST_L3_LC_REL_DELAY, EV_TIMEOUT, lc_release_req},
  488. {ST_L3_LC_REL_WAIT, EV_RELEASE_CNF, lc_release_cnf},
  489. {ST_L3_LC_REL_WAIT, EV_ESTABLISH_REQ, lc_activate},
  490. };
  491. /* *INDENT-ON* */
  492. #define L3_FN_COUNT (sizeof(L3FnList)/sizeof(struct FsmNode))
  493. void
  494. l3_msg(struct PStack *st, int pr, void *arg)
  495. {
  496. switch (pr) {
  497. case (DL_DATA | REQUEST):
  498. if (st->l3.l3m.state == ST_L3_LC_ESTAB) {
  499. st->l3.l3l2(st, pr, arg);
  500. } else {
  501. struct sk_buff *skb = arg;
  502. skb_queue_tail(&st->l3.squeue, skb);
  503. FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
  504. }
  505. break;
  506. case (DL_ESTABLISH | REQUEST):
  507. FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
  508. break;
  509. case (DL_ESTABLISH | CONFIRM):
  510. FsmEvent(&st->l3.l3m, EV_ESTABLISH_CNF, NULL);
  511. break;
  512. case (DL_ESTABLISH | INDICATION):
  513. FsmEvent(&st->l3.l3m, EV_ESTABLISH_IND, NULL);
  514. break;
  515. case (DL_RELEASE | INDICATION):
  516. FsmEvent(&st->l3.l3m, EV_RELEASE_IND, NULL);
  517. break;
  518. case (DL_RELEASE | CONFIRM):
  519. FsmEvent(&st->l3.l3m, EV_RELEASE_CNF, NULL);
  520. break;
  521. case (DL_RELEASE | REQUEST):
  522. FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
  523. break;
  524. }
  525. }
  526. int __init
  527. Isdnl3New(void)
  528. {
  529. l3fsm.state_count = L3_STATE_COUNT;
  530. l3fsm.event_count = L3_EVENT_COUNT;
  531. l3fsm.strEvent = strL3Event;
  532. l3fsm.strState = strL3State;
  533. return FsmNew(&l3fsm, L3FnList, L3_FN_COUNT);
  534. }
  535. void
  536. Isdnl3Free(void)
  537. {
  538. FsmFree(&l3fsm);
  539. }