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