blob: c75af762a067a10c08b94e56c6b6c195708680bc [file] [log] [blame]
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001/*
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 "core.h"
20
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
29#define TEI_ENTITY_ID 0xf
30
31#define MGR_PH_ACTIVE 16
32#define MGR_PH_NOTREADY 17
33
34#define DATIMER_VAL 10000
35
36static u_int *debug;
37
38static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
39static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
40static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
41
42enum {
43 ST_L1_DEACT,
44 ST_L1_DEACT_PENDING,
45 ST_L1_ACTIV,
46};
47#define DEACT_STATE_COUNT (ST_L1_ACTIV+1)
48
49static char *strDeactState[] =
50{
51 "ST_L1_DEACT",
52 "ST_L1_DEACT_PENDING",
53 "ST_L1_ACTIV",
54};
55
56enum {
57 EV_ACTIVATE,
58 EV_ACTIVATE_IND,
59 EV_DEACTIVATE,
60 EV_DEACTIVATE_IND,
61 EV_UI,
62 EV_DATIMER,
63};
64
65#define DEACT_EVENT_COUNT (EV_DATIMER+1)
66
67static char *strDeactEvent[] =
68{
69 "EV_ACTIVATE",
70 "EV_ACTIVATE_IND",
71 "EV_DEACTIVATE",
72 "EV_DEACTIVATE_IND",
73 "EV_UI",
74 "EV_DATIMER",
75};
76
77static void
78da_debug(struct FsmInst *fi, char *fmt, ...)
79{
80 struct manager *mgr = fi->userdata;
81 va_list va;
82
83 if (!(*debug & DEBUG_L2_TEIFSM))
84 return;
85 va_start(va, fmt);
86 printk(KERN_DEBUG "mgr(%d): ", mgr->ch.st->dev->id);
87 vprintk(fmt, va);
88 printk("\n");
89 va_end(va);
90}
91
92static void
93da_activate(struct FsmInst *fi, int event, void *arg)
94{
95 struct manager *mgr = fi->userdata;
96
97 if (fi->state == ST_L1_DEACT_PENDING)
98 mISDN_FsmDelTimer(&mgr->datimer, 1);
99 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
100}
101
102static void
103da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
104{
105 mISDN_FsmChangeState(fi, ST_L1_DEACT);
106}
107
108static void
109da_deactivate(struct FsmInst *fi, int event, void *arg)
110{
111 struct manager *mgr = fi->userdata;
112 struct layer2 *l2;
113 u_long flags;
114
115 read_lock_irqsave(&mgr->lock, flags);
116 list_for_each_entry(l2, &mgr->layer2, list) {
117 if (l2->l2m.state > ST_L2_4) {
118 /* have still activ TEI */
119 read_unlock_irqrestore(&mgr->lock, flags);
120 return;
121 }
122 }
123 read_unlock_irqrestore(&mgr->lock, flags);
124 /* All TEI are inactiv */
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000125 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
126 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
127 NULL, 1);
128 mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
129 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200130}
131
132static void
133da_ui(struct FsmInst *fi, int event, void *arg)
134{
135 struct manager *mgr = fi->userdata;
136
137 /* restart da timer */
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000138 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
139 mISDN_FsmDelTimer(&mgr->datimer, 2);
140 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
141 NULL, 2);
142 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200143}
144
145static void
146da_timer(struct FsmInst *fi, int event, void *arg)
147{
148 struct manager *mgr = fi->userdata;
149 struct layer2 *l2;
150 u_long flags;
151
152 /* check again */
153 read_lock_irqsave(&mgr->lock, flags);
154 list_for_each_entry(l2, &mgr->layer2, list) {
155 if (l2->l2m.state > ST_L2_4) {
156 /* have still activ TEI */
157 read_unlock_irqrestore(&mgr->lock, flags);
158 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
159 return;
160 }
161 }
162 read_unlock_irqrestore(&mgr->lock, flags);
163 /* All TEI are inactiv */
164 mISDN_FsmChangeState(fi, ST_L1_DEACT);
165 _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
166 GFP_ATOMIC);
167}
168
169static struct FsmNode DeactFnList[] =
170{
171 {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
172 {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
173 {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
174 {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
175 {ST_L1_DEACT_PENDING, EV_UI, da_ui},
176 {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
177};
178
179enum {
180 ST_TEI_NOP,
181 ST_TEI_IDREQ,
182 ST_TEI_IDVERIFY,
183};
184
185#define TEI_STATE_COUNT (ST_TEI_IDVERIFY+1)
186
187static char *strTeiState[] =
188{
189 "ST_TEI_NOP",
190 "ST_TEI_IDREQ",
191 "ST_TEI_IDVERIFY",
192};
193
194enum {
195 EV_IDREQ,
196 EV_ASSIGN,
197 EV_ASSIGN_REQ,
198 EV_DENIED,
199 EV_CHKREQ,
200 EV_CHKRESP,
201 EV_REMOVE,
202 EV_VERIFY,
203 EV_TIMER,
204};
205
206#define TEI_EVENT_COUNT (EV_TIMER+1)
207
208static char *strTeiEvent[] =
209{
210 "EV_IDREQ",
211 "EV_ASSIGN",
212 "EV_ASSIGN_REQ",
213 "EV_DENIED",
214 "EV_CHKREQ",
215 "EV_CHKRESP",
216 "EV_REMOVE",
217 "EV_VERIFY",
218 "EV_TIMER",
219};
220
221static void
222tei_debug(struct FsmInst *fi, char *fmt, ...)
223{
224 struct teimgr *tm = fi->userdata;
225 va_list va;
226
227 if (!(*debug & DEBUG_L2_TEIFSM))
228 return;
229 va_start(va, fmt);
230 printk(KERN_DEBUG "tei(%d): ", tm->l2->tei);
231 vprintk(fmt, va);
232 printk("\n");
233 va_end(va);
234}
235
236
237
238static int
239get_free_id(struct manager *mgr)
240{
241 u64 ids = 0;
242 int i;
243 struct layer2 *l2;
244
245 list_for_each_entry(l2, &mgr->layer2, list) {
246 if (l2->ch.nr > 63) {
247 printk(KERN_WARNING
248 "%s: more as 63 layer2 for one device\n",
249 __func__);
250 return -EBUSY;
251 }
252 test_and_set_bit(l2->ch.nr, (u_long *)&ids);
253 }
254 for (i = 1; i < 64; i++)
255 if (!test_bit(i, (u_long *)&ids))
256 return i;
257 printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
258 __func__);
259 return -EBUSY;
260}
261
262static int
263get_free_tei(struct manager *mgr)
264{
265 u64 ids = 0;
266 int i;
267 struct layer2 *l2;
268
269 list_for_each_entry(l2, &mgr->layer2, list) {
270 if (l2->ch.nr == 0)
271 continue;
272 if ((l2->ch.addr & 0xff) != 0)
273 continue;
274 i = l2->ch.addr >> 8;
275 if (i < 64)
276 continue;
277 i -= 64;
278
279 test_and_set_bit(i, (u_long *)&ids);
280 }
281 for (i = 0; i < 64; i++)
282 if (!test_bit(i, (u_long *)&ids))
283 return i + 64;
284 printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
285 __func__);
286 return -1;
287}
288
289static void
290teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
291{
292 struct sk_buff *skb;
293 struct mISDNhead *hh;
294 int err;
295
296 skb = mI_alloc_skb(len, GFP_ATOMIC);
297 if (!skb)
298 return;
299 hh = mISDN_HEAD_P(skb);
300 hh->prim = prim;
301 hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
302 if (len)
303 memcpy(skb_put(skb, len), arg, len);
304 err = mgr->up->send(mgr->up, skb);
305 if (err) {
306 printk(KERN_WARNING "%s: err=%d\n", __func__, err);
307 dev_kfree_skb(skb);
308 }
309}
310
311static u_int
312new_id(struct manager *mgr)
313{
314 u_int id;
315
316 id = mgr->nextid++;
317 if (id == 0x7fff)
318 mgr->nextid = 1;
319 id <<= 16;
320 id |= GROUP_TEI << 8;
321 id |= TEI_SAPI;
322 return id;
323}
324
325static void
326do_send(struct manager *mgr)
327{
328 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
329 return;
330
331 if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
332 struct sk_buff *skb = skb_dequeue(&mgr->sendq);
333
334 if (!skb) {
335 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
336 return;
337 }
338 mgr->lastid = mISDN_HEAD_ID(skb);
339 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
340 if (mgr->ch.recv(mgr->ch.peer, skb)) {
341 dev_kfree_skb(skb);
342 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
343 mgr->lastid = MISDN_ID_NONE;
344 }
345 }
346}
347
348static void
349do_ack(struct manager *mgr, u_int id)
350{
351 if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
352 if (id == mgr->lastid) {
353 if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
354 struct sk_buff *skb;
355
356 skb = skb_dequeue(&mgr->sendq);
357 if (skb) {
358 mgr->lastid = mISDN_HEAD_ID(skb);
359 if (!mgr->ch.recv(mgr->ch.peer, skb))
360 return;
361 dev_kfree_skb(skb);
362 }
363 }
364 mgr->lastid = MISDN_ID_NONE;
365 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
366 }
367 }
368}
369
370static void
371mgr_send_down(struct manager *mgr, struct sk_buff *skb)
372{
373 skb_queue_tail(&mgr->sendq, skb);
374 if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
375 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
376 NULL, GFP_KERNEL);
377 } else {
378 do_send(mgr);
379 }
380}
381
382static int
383dl_unit_data(struct manager *mgr, struct sk_buff *skb)
384{
385 if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
386 return -EINVAL;
387 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
388 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
389 NULL, GFP_KERNEL);
390 skb_push(skb, 3);
391 skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
392 skb->data[1] = 0xff; /* TEI 127 */
393 skb->data[2] = UI; /* UI frame */
394 mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
395 mISDN_HEAD_ID(skb) = new_id(mgr);
396 skb_queue_tail(&mgr->sendq, skb);
397 do_send(mgr);
398 return 0;
399}
400
Hannes Eder5b834352008-12-12 21:15:17 -0800401static unsigned int
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200402random_ri(void)
403{
404 u16 x;
405
406 get_random_bytes(&x, sizeof(x));
407 return x;
408}
409
410static struct layer2 *
411findtei(struct manager *mgr, int tei)
412{
413 struct layer2 *l2;
414 u_long flags;
415
416 read_lock_irqsave(&mgr->lock, flags);
417 list_for_each_entry(l2, &mgr->layer2, list) {
418 if ((l2->sapi == 0) && (l2->tei > 0) &&
419 (l2->tei != GROUP_TEI) && (l2->tei == tei))
420 goto done;
421 }
422 l2 = NULL;
423done:
424 read_unlock_irqrestore(&mgr->lock, flags);
425 return l2;
426}
427
428static void
429put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, u_char tei)
430{
431 struct sk_buff *skb;
432 u_char bp[8];
433
434 bp[0] = (TEI_SAPI << 2);
435 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
436 bp[0] |= 2; /* CR:=1 for net command */
437 bp[1] = (GROUP_TEI << 1) | 0x1;
438 bp[2] = UI;
439 bp[3] = TEI_ENTITY_ID;
440 bp[4] = ri >> 8;
441 bp[5] = ri & 0xff;
442 bp[6] = m_id;
443 bp[7] = (tei << 1) | 1;
444 skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr),
445 8, bp, GFP_ATOMIC);
446 if (!skb) {
447 printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
448 return;
449 }
450 mgr_send_down(mgr, skb);
451}
452
453static void
454tei_id_request(struct FsmInst *fi, int event, void *arg)
455{
456 struct teimgr *tm = fi->userdata;
457
458 if (tm->l2->tei != GROUP_TEI) {
459 tm->tei_m.printdebug(&tm->tei_m,
460 "assign request for allready assigned tei %d",
461 tm->l2->tei);
462 return;
463 }
464 tm->ri = random_ri();
465 if (*debug & DEBUG_L2_TEI)
466 tm->tei_m.printdebug(&tm->tei_m,
467 "assign request ri %d", tm->ri);
468 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
469 mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
470 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
471 tm->nval = 3;
472}
473
474static void
475tei_id_assign(struct FsmInst *fi, int event, void *arg)
476{
477 struct teimgr *tm = fi->userdata;
478 struct layer2 *l2;
479 u_char *dp = arg;
480 int ri, tei;
481
482 ri = ((unsigned int) *dp++ << 8);
483 ri += *dp++;
484 dp++;
485 tei = *dp >> 1;
486 if (*debug & DEBUG_L2_TEI)
487 tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
488 ri, tei);
489 l2 = findtei(tm->mgr, tei);
490 if (l2) { /* same tei is in use */
491 if (ri != l2->tm->ri) {
492 tm->tei_m.printdebug(fi,
493 "possible duplicate assignment tei %d", tei);
494 tei_l2(l2, MDL_ERROR_RSP, 0);
495 }
496 } else if (ri == tm->ri) {
497 mISDN_FsmDelTimer(&tm->timer, 1);
498 mISDN_FsmChangeState(fi, ST_TEI_NOP);
499 tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
500 }
501}
502
503static void
504tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
505{
506 struct teimgr *tm = fi->userdata;
507 struct layer2 *l2;
508 u_char *dp = arg;
509 int tei, ri;
510
511 ri = ((unsigned int) *dp++ << 8);
512 ri += *dp++;
513 dp++;
514 tei = *dp >> 1;
515 if (*debug & DEBUG_L2_TEI)
516 tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
517 ri, tei);
518 l2 = findtei(tm->mgr, tei);
519 if (l2) { /* same tei is in use */
520 if (ri != l2->tm->ri) { /* and it wasn't our request */
521 tm->tei_m.printdebug(fi,
522 "possible duplicate assignment tei %d", tei);
523 mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
524 }
525 }
526}
527
528static void
529tei_id_denied(struct FsmInst *fi, int event, void *arg)
530{
531 struct teimgr *tm = fi->userdata;
532 u_char *dp = arg;
533 int ri, tei;
534
535 ri = ((unsigned int) *dp++ << 8);
536 ri += *dp++;
537 dp++;
538 tei = *dp >> 1;
539 if (*debug & DEBUG_L2_TEI)
540 tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
541 ri, tei);
542}
543
544static void
545tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
546{
547 struct teimgr *tm = fi->userdata;
548 u_char *dp = arg;
549 int tei;
550
551 tei = *(dp+3) >> 1;
552 if (*debug & DEBUG_L2_TEI)
553 tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
554 if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
555 (tei == tm->l2->tei))) {
556 mISDN_FsmDelTimer(&tm->timer, 4);
557 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
558 put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
559 }
560}
561
562static void
563tei_id_remove(struct FsmInst *fi, int event, void *arg)
564{
565 struct teimgr *tm = fi->userdata;
566 u_char *dp = arg;
567 int tei;
568
569 tei = *(dp+3) >> 1;
570 if (*debug & DEBUG_L2_TEI)
571 tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
572 if ((tm->l2->tei != GROUP_TEI) &&
573 ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
574 mISDN_FsmDelTimer(&tm->timer, 5);
575 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
576 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
577 }
578}
579
580static void
581tei_id_verify(struct FsmInst *fi, int event, void *arg)
582{
583 struct teimgr *tm = fi->userdata;
584
585 if (*debug & DEBUG_L2_TEI)
586 tm->tei_m.printdebug(fi, "id verify request for tei %d",
587 tm->l2->tei);
588 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
589 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
590 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
591 tm->nval = 2;
592}
593
594static void
595tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
596{
597 struct teimgr *tm = fi->userdata;
598
599 if (--tm->nval) {
600 tm->ri = random_ri();
601 if (*debug & DEBUG_L2_TEI)
602 tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
603 4 - tm->nval, tm->ri);
604 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
605 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
606 } else {
607 tm->tei_m.printdebug(fi, "assign req failed");
608 tei_l2(tm->l2, MDL_ERROR_RSP, 0);
609 mISDN_FsmChangeState(fi, ST_TEI_NOP);
610 }
611}
612
613static void
614tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
615{
616 struct teimgr *tm = fi->userdata;
617
618 if (--tm->nval) {
619 if (*debug & DEBUG_L2_TEI)
620 tm->tei_m.printdebug(fi,
621 "id verify req(%d) for tei %d",
622 3 - tm->nval, tm->l2->tei);
623 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
624 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
625 } else {
626 tm->tei_m.printdebug(fi, "verify req for tei %d failed",
627 tm->l2->tei);
628 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
629 mISDN_FsmChangeState(fi, ST_TEI_NOP);
630 }
631}
632
633static struct FsmNode TeiFnListUser[] =
634{
635 {ST_TEI_NOP, EV_IDREQ, tei_id_request},
636 {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
637 {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
638 {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
639 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
640 {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
641 {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
642 {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
643 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
644 {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
645 {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
646};
647
648static void
649tei_l2remove(struct layer2 *l2)
650{
651 put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
652 tei_l2(l2, MDL_REMOVE_REQ, 0);
653 list_del(&l2->ch.list);
654 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
655}
656
657static void
658tei_assign_req(struct FsmInst *fi, int event, void *arg)
659{
660 struct teimgr *tm = fi->userdata;
661 u_char *dp = arg;
662
663 if (tm->l2->tei == GROUP_TEI) {
664 tm->tei_m.printdebug(&tm->tei_m,
665 "net tei assign request without tei");
666 return;
667 }
668 tm->ri = ((unsigned int) *dp++ << 8);
669 tm->ri += *dp++;
670 if (*debug & DEBUG_L2_TEI)
671 tm->tei_m.printdebug(&tm->tei_m,
672 "net assign request ri %d teim %d", tm->ri, *dp);
673 put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
674 mISDN_FsmChangeState(fi, ST_TEI_NOP);
675}
676
677static void
678tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
679{
680 struct teimgr *tm = fi->userdata;
681
682 if (*debug & DEBUG_L2_TEI)
683 tm->tei_m.printdebug(fi, "id check request for tei %d",
684 tm->l2->tei);
685 tm->rcnt = 0;
686 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
687 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
688 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
689 tm->nval = 2;
690}
691
692static void
693tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
694{
695 struct teimgr *tm = fi->userdata;
696 u_char *dp = arg;
697 int tei;
698
699 tei = dp[3] >> 1;
700 if (*debug & DEBUG_L2_TEI)
701 tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
702 if (tei == tm->l2->tei)
703 tm->rcnt++;
704}
705
706static void
707tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
708{
709 struct teimgr *tm = fi->userdata;
710 u_char *dp = arg;
711 int tei;
712
713 tei = dp[3] >> 1;
714 if (*debug & DEBUG_L2_TEI)
715 tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
716 tei, tm->l2->tei);
717 if (tei == tm->l2->tei)
718 tei_id_chk_req_net(fi, event, arg);
719}
720
721static void
722tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
723{
724 struct teimgr *tm = fi->userdata;
725
726 if (tm->rcnt == 1) {
727 if (*debug & DEBUG_L2_TEI)
728 tm->tei_m.printdebug(fi,
729 "check req for tei %d sucessful\n", tm->l2->tei);
730 mISDN_FsmChangeState(fi, ST_TEI_NOP);
731 } else if (tm->rcnt > 1) {
732 /* duplicate assignment; remove */
733 tei_l2remove(tm->l2);
734 } else if (--tm->nval) {
735 if (*debug & DEBUG_L2_TEI)
736 tm->tei_m.printdebug(fi,
737 "id check req(%d) for tei %d",
738 3 - tm->nval, tm->l2->tei);
739 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
740 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
741 } else {
742 tm->tei_m.printdebug(fi, "check req for tei %d failed",
743 tm->l2->tei);
744 mISDN_FsmChangeState(fi, ST_TEI_NOP);
745 tei_l2remove(tm->l2);
746 }
747}
748
749static struct FsmNode TeiFnListNet[] =
750{
751 {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
752 {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
753 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
754 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
755 {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
756};
757
758static void
759tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
760{
761 if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
762 return;
763 if (*debug & DEBUG_L2_TEI)
764 tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
765 if (mt == ID_ASSIGNED)
766 mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
767 else if (mt == ID_DENIED)
768 mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
769 else if (mt == ID_CHK_REQ)
770 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
771 else if (mt == ID_REMOVE)
772 mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
773 else if (mt == ID_VERIFY)
774 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
775 else if (mt == ID_CHK_RES)
776 mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
777}
778
779static struct layer2 *
780create_new_tei(struct manager *mgr, int tei)
781{
782 u_long opt = 0;
783 u_long flags;
784 int id;
785 struct layer2 *l2;
786
787 if (!mgr->up)
788 return NULL;
789 if (tei < 64)
790 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
791 if (mgr->ch.st->dev->Dprotocols
792 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
793 test_and_set_bit(OPTION_L2_PMX, &opt);
794 l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, (u_int)opt, (u_long)tei);
795 if (!l2) {
796 printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
797 return NULL;
798 }
799 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
800 if (!l2->tm) {
801 kfree(l2);
802 printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
803 return NULL;
804 }
805 l2->tm->mgr = mgr;
806 l2->tm->l2 = l2;
807 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
808 l2->tm->tei_m.userdata = l2->tm;
809 l2->tm->tei_m.printdebug = tei_debug;
810 l2->tm->tei_m.fsm = &teifsmn;
811 l2->tm->tei_m.state = ST_TEI_NOP;
812 l2->tm->tval = 2000; /* T202 2 sec */
813 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
814 write_lock_irqsave(&mgr->lock, flags);
815 id = get_free_id(mgr);
816 list_add_tail(&l2->list, &mgr->layer2);
817 write_unlock_irqrestore(&mgr->lock, flags);
818 if (id < 0) {
819 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
820 printk(KERN_WARNING "%s:no free id\n", __func__);
821 return NULL;
822 } else {
823 l2->ch.nr = id;
824 __add_layer2(&l2->ch, mgr->ch.st);
825 l2->ch.recv = mgr->ch.recv;
826 l2->ch.peer = mgr->ch.peer;
827 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
828 }
829 return l2;
830}
831
832static void
833new_tei_req(struct manager *mgr, u_char *dp)
834{
835 int tei, ri;
836 struct layer2 *l2;
837
838 ri = dp[0] << 8;
839 ri += dp[1];
840 if (!mgr->up)
841 goto denied;
842 tei = get_free_tei(mgr);
843 if (tei < 0) {
844 printk(KERN_WARNING "%s:No free tei\n", __func__);
845 goto denied;
846 }
847 l2 = create_new_tei(mgr, tei);
848 if (!l2)
849 goto denied;
850 else
851 mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
852 return;
853denied:
854 put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
855}
856
857static int
858ph_data_ind(struct manager *mgr, struct sk_buff *skb)
859{
860 int ret = -EINVAL;
861 struct layer2 *l2;
862 u_long flags;
863 u_char mt;
864
865 if (skb->len < 8) {
866 if (*debug & DEBUG_L2_TEI)
867 printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
868 __func__, skb->len);
869 goto done;
870 }
871 if (*debug & DEBUG_L2_TEI)
872
873 if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
874 goto done;
875 if (skb->data[0] & 1) /* EA0 formal error */
876 goto done;
877 if (!(skb->data[1] & 1)) /* EA1 formal error */
878 goto done;
879 if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
880 goto done;
881 if ((skb->data[2] & 0xef) != UI) /* not UI */
882 goto done;
883 if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
884 goto done;
885 mt = skb->data[6];
886 switch (mt) {
887 case ID_REQUEST:
888 case ID_CHK_RES:
889 case ID_VERIFY:
890 if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
891 goto done;
892 break;
893 case ID_ASSIGNED:
894 case ID_DENIED:
895 case ID_CHK_REQ:
896 case ID_REMOVE:
897 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
898 goto done;
899 break;
900 default:
901 goto done;
902 }
903 ret = 0;
904 if (mt == ID_REQUEST) {
905 new_tei_req(mgr, &skb->data[4]);
906 goto done;
907 }
908 read_lock_irqsave(&mgr->lock, flags);
909 list_for_each_entry(l2, &mgr->layer2, list) {
910 tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
911 }
912 read_unlock_irqrestore(&mgr->lock, flags);
913done:
914 return ret;
915}
916
917int
918l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
919{
920 struct teimgr *tm = l2->tm;
921
922 if (test_bit(FLG_FIXED_TEI, &l2->flag))
923 return 0;
924 if (*debug & DEBUG_L2_TEI)
925 printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
926 switch (cmd) {
927 case MDL_ASSIGN_IND:
928 mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
929 break;
930 case MDL_ERROR_IND:
931 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
932 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
933 if (test_bit(MGR_OPT_USER, &tm->mgr->options))
934 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
935 break;
936 case MDL_STATUS_UP_IND:
937 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
938 mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
939 break;
940 case MDL_STATUS_DOWN_IND:
941 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
942 mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
943 break;
944 case MDL_STATUS_UI_IND:
945 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
946 mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
947 break;
948 }
949 return 0;
950}
951
952void
Karsten Keilc5b61d52008-07-27 18:32:50 +0200953TEIrelease(struct layer2 *l2)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200954{
955 struct teimgr *tm = l2->tm;
956 u_long flags;
957
958 mISDN_FsmDelTimer(&tm->timer, 1);
959 write_lock_irqsave(&tm->mgr->lock, flags);
960 list_del(&l2->list);
961 write_unlock_irqrestore(&tm->mgr->lock, flags);
962 l2->tm = NULL;
963 kfree(tm);
964}
965
966static int
967create_teimgr(struct manager *mgr, struct channel_req *crq)
968{
969 struct layer2 *l2;
970 u_long opt = 0;
971 u_long flags;
972 int id;
973
974 if (*debug & DEBUG_L2_TEI)
975 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
Matthias Urlichs837468d2008-08-16 00:04:33 +0200976 __func__, dev_name(&mgr->ch.st->dev->dev),
977 crq->protocol, crq->adr.dev, crq->adr.channel,
978 crq->adr.sapi, crq->adr.tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200979 if (crq->adr.sapi != 0) /* not supported yet */
980 return -EINVAL;
981 if (crq->adr.tei > GROUP_TEI)
982 return -EINVAL;
983 if (crq->adr.tei < 64)
984 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
985 if (crq->adr.tei == 0)
986 test_and_set_bit(OPTION_L2_PTP, &opt);
987 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
988 if (crq->protocol == ISDN_P_LAPD_TE)
989 return -EPROTONOSUPPORT;
990 if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
991 return -EINVAL;
992 if (mgr->up) {
993 printk(KERN_WARNING
994 "%s: only one network manager is allowed\n",
995 __func__);
996 return -EBUSY;
997 }
998 } else if (test_bit(MGR_OPT_USER, &mgr->options)) {
999 if (crq->protocol == ISDN_P_LAPD_NT)
1000 return -EPROTONOSUPPORT;
1001 if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
1002 return -EINVAL; /* dyn tei */
1003 } else {
1004 if (crq->protocol == ISDN_P_LAPD_NT)
1005 test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
1006 if (crq->protocol == ISDN_P_LAPD_TE)
1007 test_and_set_bit(MGR_OPT_USER, &mgr->options);
1008 }
1009 if (mgr->ch.st->dev->Dprotocols
1010 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
1011 test_and_set_bit(OPTION_L2_PMX, &opt);
1012 if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
1013 mgr->up = crq->ch;
1014 id = DL_INFO_L2_CONNECT;
1015 teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
1016 crq->ch = NULL;
1017 if (!list_empty(&mgr->layer2)) {
1018 read_lock_irqsave(&mgr->lock, flags);
1019 list_for_each_entry(l2, &mgr->layer2, list) {
1020 l2->up = mgr->up;
1021 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
1022 }
1023 read_unlock_irqrestore(&mgr->lock, flags);
1024 }
1025 return 0;
1026 }
1027 l2 = create_l2(crq->ch, crq->protocol, (u_int)opt,
1028 (u_long)crq->adr.tei);
1029 if (!l2)
1030 return -ENOMEM;
1031 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
1032 if (!l2->tm) {
1033 kfree(l2);
1034 printk(KERN_ERR "kmalloc teimgr failed\n");
1035 return -ENOMEM;
1036 }
1037 l2->tm->mgr = mgr;
1038 l2->tm->l2 = l2;
1039 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
1040 l2->tm->tei_m.userdata = l2->tm;
1041 l2->tm->tei_m.printdebug = tei_debug;
1042 if (crq->protocol == ISDN_P_LAPD_TE) {
1043 l2->tm->tei_m.fsm = &teifsmu;
1044 l2->tm->tei_m.state = ST_TEI_NOP;
1045 l2->tm->tval = 1000; /* T201 1 sec */
1046 } else {
1047 l2->tm->tei_m.fsm = &teifsmn;
1048 l2->tm->tei_m.state = ST_TEI_NOP;
1049 l2->tm->tval = 2000; /* T202 2 sec */
1050 }
1051 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
1052 write_lock_irqsave(&mgr->lock, flags);
1053 id = get_free_id(mgr);
1054 list_add_tail(&l2->list, &mgr->layer2);
1055 write_unlock_irqrestore(&mgr->lock, flags);
1056 if (id < 0) {
1057 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1058 } else {
1059 l2->ch.nr = id;
1060 l2->up->nr = id;
1061 crq->ch = &l2->ch;
1062 id = 0;
1063 }
1064 return id;
1065}
1066
1067static int
1068mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
1069{
1070 struct manager *mgr;
1071 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1072 int ret = -EINVAL;
1073
1074 mgr = container_of(ch, struct manager, ch);
1075 if (*debug & DEBUG_L2_RECV)
1076 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1077 __func__, hh->prim, hh->id);
1078 switch (hh->prim) {
1079 case PH_DATA_IND:
1080 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
1081 ret = ph_data_ind(mgr, skb);
1082 break;
1083 case PH_DATA_CNF:
1084 do_ack(mgr, hh->id);
1085 ret = 0;
1086 break;
1087 case PH_ACTIVATE_IND:
1088 test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
1089 mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
1090 do_send(mgr);
1091 ret = 0;
1092 break;
1093 case PH_DEACTIVATE_IND:
1094 test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
1095 mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
1096 ret = 0;
1097 break;
1098 case DL_UNITDATA_REQ:
1099 return dl_unit_data(mgr, skb);
1100 }
1101 if (!ret)
1102 dev_kfree_skb(skb);
1103 return ret;
1104}
1105
1106static int
1107free_teimanager(struct manager *mgr)
1108{
1109 struct layer2 *l2, *nl2;
1110
Andreas Eversberge73f6b22009-05-22 11:04:48 +00001111 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001112 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
1113 /* not locked lock is taken in release tei */
1114 mgr->up = NULL;
1115 if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
1116 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1117 put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
1118 mutex_lock(&mgr->ch.st->lmutex);
1119 list_del(&l2->ch.list);
1120 mutex_unlock(&mgr->ch.st->lmutex);
1121 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1122 }
1123 test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
1124 } else {
1125 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1126 l2->up = NULL;
1127 }
1128 }
1129 }
1130 if (test_bit(MGR_OPT_USER, &mgr->options)) {
1131 if (list_empty(&mgr->layer2))
1132 test_and_clear_bit(MGR_OPT_USER, &mgr->options);
1133 }
1134 mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
1135 return 0;
1136}
1137
1138static int
1139ctrl_teimanager(struct manager *mgr, void *arg)
1140{
1141 /* currently we only have one option */
Andreas Eversberge73f6b22009-05-22 11:04:48 +00001142 int *val = (int *)arg;
1143 int ret = 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001144
Andreas Eversberge73f6b22009-05-22 11:04:48 +00001145 switch (val[0]) {
1146 case IMCLEAR_L2:
1147 if (val[1])
1148 test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
1149 else
1150 test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
1151 break;
1152 case IMHOLD_L1:
1153 if (val[1])
1154 test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
1155 else
1156 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1157 break;
1158 default:
1159 ret = -EINVAL;
1160 }
1161 return ret;
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001162}
1163
1164/* This function does create a L2 for fixed TEI in NT Mode */
1165static int
1166check_data(struct manager *mgr, struct sk_buff *skb)
1167{
1168 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1169 int ret, tei;
1170 struct layer2 *l2;
1171
1172 if (*debug & DEBUG_L2_CTRL)
1173 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1174 __func__, hh->prim, hh->id);
1175 if (test_bit(MGR_OPT_USER, &mgr->options))
1176 return -ENOTCONN;
1177 if (hh->prim != PH_DATA_IND)
1178 return -ENOTCONN;
1179 if (skb->len != 3)
1180 return -ENOTCONN;
1181 if (skb->data[0] != 0)
1182 /* only SAPI 0 command */
1183 return -ENOTCONN;
1184 if (!(skb->data[1] & 1)) /* invalid EA1 */
1185 return -EINVAL;
1186 tei = skb->data[1] >> 0;
1187 if (tei > 63) /* not a fixed tei */
1188 return -ENOTCONN;
1189 if ((skb->data[2] & ~0x10) != SABME)
1190 return -ENOTCONN;
1191 /* We got a SABME for a fixed TEI */
1192 l2 = create_new_tei(mgr, tei);
1193 if (!l2)
1194 return -ENOMEM;
1195 ret = l2->ch.send(&l2->ch, skb);
1196 return ret;
1197}
1198
1199void
1200delete_teimanager(struct mISDNchannel *ch)
1201{
1202 struct manager *mgr;
1203 struct layer2 *l2, *nl2;
1204
1205 mgr = container_of(ch, struct manager, ch);
1206 /* not locked lock is taken in release tei */
1207 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1208 mutex_lock(&mgr->ch.st->lmutex);
1209 list_del(&l2->ch.list);
1210 mutex_unlock(&mgr->ch.st->lmutex);
1211 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1212 }
1213 list_del(&mgr->ch.list);
1214 list_del(&mgr->bcast.list);
1215 skb_queue_purge(&mgr->sendq);
1216 kfree(mgr);
1217}
1218
1219static int
1220mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1221{
1222 struct manager *mgr;
1223 int ret = -EINVAL;
1224
1225 mgr = container_of(ch, struct manager, ch);
1226 if (*debug & DEBUG_L2_CTRL)
1227 printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
1228 switch (cmd) {
1229 case OPEN_CHANNEL:
1230 ret = create_teimgr(mgr, arg);
1231 break;
1232 case CLOSE_CHANNEL:
1233 ret = free_teimanager(mgr);
1234 break;
1235 case CONTROL_CHANNEL:
1236 ret = ctrl_teimanager(mgr, arg);
1237 break;
1238 case CHECK_DATA:
1239 ret = check_data(mgr, arg);
1240 break;
1241 }
1242 return ret;
1243}
1244
1245static int
1246mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
1247{
1248 struct manager *mgr = container_of(ch, struct manager, bcast);
1249 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1250 struct sk_buff *cskb = NULL;
1251 struct layer2 *l2;
1252 u_long flags;
1253 int ret;
1254
1255 read_lock_irqsave(&mgr->lock, flags);
1256 list_for_each_entry(l2, &mgr->layer2, list) {
1257 if ((hh->id & MISDN_ID_SAPI_MASK) ==
1258 (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
1259 if (list_is_last(&l2->list, &mgr->layer2)) {
1260 cskb = skb;
1261 skb = NULL;
1262 } else {
1263 if (!cskb)
1264 cskb = skb_copy(skb, GFP_KERNEL);
1265 }
1266 if (cskb) {
1267 ret = l2->ch.send(&l2->ch, cskb);
1268 if (ret) {
1269 if (*debug & DEBUG_SEND_ERR)
1270 printk(KERN_DEBUG
1271 "%s ch%d prim(%x) addr(%x)"
1272 " err %d\n",
1273 __func__, l2->ch.nr,
1274 hh->prim, l2->ch.addr, ret);
1275 } else
1276 cskb = NULL;
1277 } else {
1278 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
1279 __func__, ch->nr, ch->addr);
1280 goto out;
1281 }
1282 }
1283 }
1284out:
1285 read_unlock_irqrestore(&mgr->lock, flags);
1286 if (cskb)
1287 dev_kfree_skb(cskb);
1288 if (skb)
1289 dev_kfree_skb(skb);
1290 return 0;
1291}
1292
1293static int
1294mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1295{
1296
1297 return -EINVAL;
1298}
1299
1300int
1301create_teimanager(struct mISDNdevice *dev)
1302{
1303 struct manager *mgr;
1304
1305 mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
1306 if (!mgr)
1307 return -ENOMEM;
1308 INIT_LIST_HEAD(&mgr->layer2);
Karsten Keil702c7902008-12-12 21:21:57 -08001309 rwlock_init(&mgr->lock);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001310 skb_queue_head_init(&mgr->sendq);
1311 mgr->nextid = 1;
1312 mgr->lastid = MISDN_ID_NONE;
1313 mgr->ch.send = mgr_send;
1314 mgr->ch.ctrl = mgr_ctrl;
1315 mgr->ch.st = dev->D.st;
1316 set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
1317 add_layer2(&mgr->ch, dev->D.st);
1318 mgr->bcast.send = mgr_bcast;
1319 mgr->bcast.ctrl = mgr_bcast_ctrl;
1320 mgr->bcast.st = dev->D.st;
1321 set_channel_address(&mgr->bcast, 0, GROUP_TEI);
1322 add_layer2(&mgr->bcast, dev->D.st);
1323 mgr->deact.debug = *debug & DEBUG_MANAGER;
1324 mgr->deact.userdata = mgr;
1325 mgr->deact.printdebug = da_debug;
1326 mgr->deact.fsm = &deactfsm;
1327 mgr->deact.state = ST_L1_DEACT;
1328 mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
1329 dev->teimgr = &mgr->ch;
1330 return 0;
1331}
1332
1333int TEIInit(u_int *deb)
1334{
1335 debug = deb;
1336 teifsmu.state_count = TEI_STATE_COUNT;
1337 teifsmu.event_count = TEI_EVENT_COUNT;
1338 teifsmu.strEvent = strTeiEvent;
1339 teifsmu.strState = strTeiState;
1340 mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
1341 teifsmn.state_count = TEI_STATE_COUNT;
1342 teifsmn.event_count = TEI_EVENT_COUNT;
1343 teifsmn.strEvent = strTeiEvent;
1344 teifsmn.strState = strTeiState;
1345 mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
1346 deactfsm.state_count = DEACT_STATE_COUNT;
1347 deactfsm.event_count = DEACT_EVENT_COUNT;
1348 deactfsm.strEvent = strDeactEvent;
1349 deactfsm.strState = strDeactState;
1350 mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
1351 return 0;
1352}
1353
1354void TEIFree(void)
1355{
1356 mISDN_FsmFree(&teifsmu);
1357 mISDN_FsmFree(&teifsmn);
1358 mISDN_FsmFree(&deactfsm);
1359}