isdnl3.c 13 KB

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