blob: 969766f5f82b8f016957f5baccf7618fc4b307b2 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020020#include "core.h"
21
22#define ID_REQUEST 1
23#define ID_ASSIGNED 2
24#define ID_DENIED 3
25#define ID_CHK_REQ 4
26#define ID_CHK_RES 5
27#define ID_REMOVE 6
28#define ID_VERIFY 7
29
30#define TEI_ENTITY_ID 0xf
31
32#define MGR_PH_ACTIVE 16
33#define MGR_PH_NOTREADY 17
34
35#define DATIMER_VAL 10000
36
Joe Perches475be4d2012-02-19 19:52:38 -080037static u_int *debug;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020038
39static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
40static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
41static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
42
43enum {
44 ST_L1_DEACT,
45 ST_L1_DEACT_PENDING,
46 ST_L1_ACTIV,
47};
Joe Perches475be4d2012-02-19 19:52:38 -080048#define DEACT_STATE_COUNT (ST_L1_ACTIV + 1)
Karsten Keil1b2b03f2008-07-27 01:54:58 +020049
50static char *strDeactState[] =
51{
52 "ST_L1_DEACT",
53 "ST_L1_DEACT_PENDING",
54 "ST_L1_ACTIV",
55};
56
57enum {
58 EV_ACTIVATE,
59 EV_ACTIVATE_IND,
60 EV_DEACTIVATE,
61 EV_DEACTIVATE_IND,
62 EV_UI,
63 EV_DATIMER,
64};
65
Joe Perches475be4d2012-02-19 19:52:38 -080066#define DEACT_EVENT_COUNT (EV_DATIMER + 1)
Karsten Keil1b2b03f2008-07-27 01:54:58 +020067
68static char *strDeactEvent[] =
69{
70 "EV_ACTIVATE",
71 "EV_ACTIVATE_IND",
72 "EV_DEACTIVATE",
73 "EV_DEACTIVATE_IND",
74 "EV_UI",
75 "EV_DATIMER",
76};
77
78static void
79da_debug(struct FsmInst *fi, char *fmt, ...)
80{
81 struct manager *mgr = fi->userdata;
Joe Perches020f01e2010-11-09 14:35:16 +000082 struct va_format vaf;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020083 va_list va;
84
85 if (!(*debug & DEBUG_L2_TEIFSM))
86 return;
Joe Perches020f01e2010-11-09 14:35:16 +000087
Karsten Keil1b2b03f2008-07-27 01:54:58 +020088 va_start(va, fmt);
Joe Perches020f01e2010-11-09 14:35:16 +000089
90 vaf.fmt = fmt;
91 vaf.va = &va;
92
93 printk(KERN_DEBUG "mgr(%d): %pV\n", mgr->ch.st->dev->id, &vaf);
94
Karsten Keil1b2b03f2008-07-27 01:54:58 +020095 va_end(va);
96}
97
98static void
99da_activate(struct FsmInst *fi, int event, void *arg)
100{
101 struct manager *mgr = fi->userdata;
102
103 if (fi->state == ST_L1_DEACT_PENDING)
104 mISDN_FsmDelTimer(&mgr->datimer, 1);
105 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
106}
107
108static void
109da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
110{
111 mISDN_FsmChangeState(fi, ST_L1_DEACT);
112}
113
114static void
115da_deactivate(struct FsmInst *fi, int event, void *arg)
116{
117 struct manager *mgr = fi->userdata;
118 struct layer2 *l2;
119 u_long flags;
120
121 read_lock_irqsave(&mgr->lock, flags);
122 list_for_each_entry(l2, &mgr->layer2, list) {
123 if (l2->l2m.state > ST_L2_4) {
124 /* have still activ TEI */
125 read_unlock_irqrestore(&mgr->lock, flags);
126 return;
127 }
128 }
129 read_unlock_irqrestore(&mgr->lock, flags);
130 /* All TEI are inactiv */
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000131 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
132 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
Joe Perches475be4d2012-02-19 19:52:38 -0800133 NULL, 1);
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000134 mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
135 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200136}
137
138static void
139da_ui(struct FsmInst *fi, int event, void *arg)
140{
141 struct manager *mgr = fi->userdata;
142
143 /* restart da timer */
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000144 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
145 mISDN_FsmDelTimer(&mgr->datimer, 2);
146 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
Joe Perches475be4d2012-02-19 19:52:38 -0800147 NULL, 2);
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000148 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200149}
150
151static void
152da_timer(struct FsmInst *fi, int event, void *arg)
153{
154 struct manager *mgr = fi->userdata;
155 struct layer2 *l2;
156 u_long flags;
157
158 /* check again */
159 read_lock_irqsave(&mgr->lock, flags);
160 list_for_each_entry(l2, &mgr->layer2, list) {
161 if (l2->l2m.state > ST_L2_4) {
162 /* have still activ TEI */
163 read_unlock_irqrestore(&mgr->lock, flags);
164 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
165 return;
166 }
167 }
168 read_unlock_irqrestore(&mgr->lock, flags);
169 /* All TEI are inactiv */
170 mISDN_FsmChangeState(fi, ST_L1_DEACT);
171 _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
Joe Perches475be4d2012-02-19 19:52:38 -0800172 GFP_ATOMIC);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200173}
174
175static struct FsmNode DeactFnList[] =
176{
177 {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
178 {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
179 {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
180 {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
181 {ST_L1_DEACT_PENDING, EV_UI, da_ui},
182 {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
183};
184
185enum {
186 ST_TEI_NOP,
187 ST_TEI_IDREQ,
188 ST_TEI_IDVERIFY,
189};
190
Joe Perches475be4d2012-02-19 19:52:38 -0800191#define TEI_STATE_COUNT (ST_TEI_IDVERIFY + 1)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200192
193static char *strTeiState[] =
194{
195 "ST_TEI_NOP",
196 "ST_TEI_IDREQ",
197 "ST_TEI_IDVERIFY",
198};
199
200enum {
201 EV_IDREQ,
202 EV_ASSIGN,
203 EV_ASSIGN_REQ,
204 EV_DENIED,
205 EV_CHKREQ,
206 EV_CHKRESP,
207 EV_REMOVE,
208 EV_VERIFY,
209 EV_TIMER,
210};
211
Joe Perches475be4d2012-02-19 19:52:38 -0800212#define TEI_EVENT_COUNT (EV_TIMER + 1)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200213
214static char *strTeiEvent[] =
215{
216 "EV_IDREQ",
217 "EV_ASSIGN",
218 "EV_ASSIGN_REQ",
219 "EV_DENIED",
220 "EV_CHKREQ",
221 "EV_CHKRESP",
222 "EV_REMOVE",
223 "EV_VERIFY",
224 "EV_TIMER",
225};
226
227static void
228tei_debug(struct FsmInst *fi, char *fmt, ...)
229{
230 struct teimgr *tm = fi->userdata;
Joe Perches020f01e2010-11-09 14:35:16 +0000231 struct va_format vaf;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200232 va_list va;
233
234 if (!(*debug & DEBUG_L2_TEIFSM))
235 return;
Joe Perches020f01e2010-11-09 14:35:16 +0000236
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200237 va_start(va, fmt);
Joe Perches020f01e2010-11-09 14:35:16 +0000238
239 vaf.fmt = fmt;
240 vaf.va = &va;
241
242 printk(KERN_DEBUG "sapi(%d) tei(%d): %pV\n",
243 tm->l2->sapi, tm->l2->tei, &vaf);
244
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200245 va_end(va);
246}
247
248
249
250static int
251get_free_id(struct manager *mgr)
252{
253 u64 ids = 0;
254 int i;
255 struct layer2 *l2;
256
257 list_for_each_entry(l2, &mgr->layer2, list) {
258 if (l2->ch.nr > 63) {
259 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800260 "%s: more as 63 layer2 for one device\n",
261 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200262 return -EBUSY;
263 }
264 test_and_set_bit(l2->ch.nr, (u_long *)&ids);
265 }
266 for (i = 1; i < 64; i++)
267 if (!test_bit(i, (u_long *)&ids))
268 return i;
269 printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800270 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200271 return -EBUSY;
272}
273
274static int
275get_free_tei(struct manager *mgr)
276{
277 u64 ids = 0;
278 int i;
279 struct layer2 *l2;
280
281 list_for_each_entry(l2, &mgr->layer2, list) {
282 if (l2->ch.nr == 0)
283 continue;
284 if ((l2->ch.addr & 0xff) != 0)
285 continue;
286 i = l2->ch.addr >> 8;
287 if (i < 64)
288 continue;
289 i -= 64;
290
291 test_and_set_bit(i, (u_long *)&ids);
292 }
293 for (i = 0; i < 64; i++)
294 if (!test_bit(i, (u_long *)&ids))
295 return i + 64;
296 printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800297 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200298 return -1;
299}
300
301static void
302teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
303{
304 struct sk_buff *skb;
305 struct mISDNhead *hh;
306 int err;
307
308 skb = mI_alloc_skb(len, GFP_ATOMIC);
309 if (!skb)
310 return;
311 hh = mISDN_HEAD_P(skb);
312 hh->prim = prim;
313 hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
314 if (len)
315 memcpy(skb_put(skb, len), arg, len);
316 err = mgr->up->send(mgr->up, skb);
317 if (err) {
318 printk(KERN_WARNING "%s: err=%d\n", __func__, err);
319 dev_kfree_skb(skb);
320 }
321}
322
323static u_int
324new_id(struct manager *mgr)
325{
326 u_int id;
327
328 id = mgr->nextid++;
329 if (id == 0x7fff)
330 mgr->nextid = 1;
331 id <<= 16;
332 id |= GROUP_TEI << 8;
333 id |= TEI_SAPI;
334 return id;
335}
336
337static void
338do_send(struct manager *mgr)
339{
340 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
341 return;
342
343 if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
344 struct sk_buff *skb = skb_dequeue(&mgr->sendq);
345
346 if (!skb) {
347 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
348 return;
349 }
350 mgr->lastid = mISDN_HEAD_ID(skb);
351 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
352 if (mgr->ch.recv(mgr->ch.peer, skb)) {
353 dev_kfree_skb(skb);
354 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
355 mgr->lastid = MISDN_ID_NONE;
356 }
357 }
358}
359
360static void
361do_ack(struct manager *mgr, u_int id)
362{
363 if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
364 if (id == mgr->lastid) {
365 if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
366 struct sk_buff *skb;
367
368 skb = skb_dequeue(&mgr->sendq);
369 if (skb) {
370 mgr->lastid = mISDN_HEAD_ID(skb);
371 if (!mgr->ch.recv(mgr->ch.peer, skb))
372 return;
373 dev_kfree_skb(skb);
374 }
375 }
376 mgr->lastid = MISDN_ID_NONE;
377 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
378 }
379 }
380}
381
382static void
383mgr_send_down(struct manager *mgr, struct sk_buff *skb)
384{
385 skb_queue_tail(&mgr->sendq, skb);
386 if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
387 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -0800388 NULL, GFP_KERNEL);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200389 } else {
390 do_send(mgr);
391 }
392}
393
394static int
395dl_unit_data(struct manager *mgr, struct sk_buff *skb)
396{
397 if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
398 return -EINVAL;
399 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
400 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -0800401 NULL, GFP_KERNEL);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200402 skb_push(skb, 3);
403 skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
404 skb->data[1] = 0xff; /* TEI 127 */
405 skb->data[2] = UI; /* UI frame */
406 mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
407 mISDN_HEAD_ID(skb) = new_id(mgr);
408 skb_queue_tail(&mgr->sendq, skb);
409 do_send(mgr);
410 return 0;
411}
412
Hannes Eder5b834352008-12-12 21:15:17 -0800413static unsigned int
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200414random_ri(void)
415{
416 u16 x;
417
418 get_random_bytes(&x, sizeof(x));
419 return x;
420}
421
422static struct layer2 *
423findtei(struct manager *mgr, int tei)
424{
425 struct layer2 *l2;
426 u_long flags;
427
428 read_lock_irqsave(&mgr->lock, flags);
429 list_for_each_entry(l2, &mgr->layer2, list) {
430 if ((l2->sapi == 0) && (l2->tei > 0) &&
431 (l2->tei != GROUP_TEI) && (l2->tei == tei))
432 goto done;
433 }
434 l2 = NULL;
435done:
436 read_unlock_irqrestore(&mgr->lock, flags);
437 return l2;
438}
439
440static void
Andreas Eversberg5b277b82009-05-25 00:50:02 -0700441put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200442{
443 struct sk_buff *skb;
444 u_char bp[8];
445
446 bp[0] = (TEI_SAPI << 2);
447 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
448 bp[0] |= 2; /* CR:=1 for net command */
449 bp[1] = (GROUP_TEI << 1) | 0x1;
450 bp[2] = UI;
451 bp[3] = TEI_ENTITY_ID;
452 bp[4] = ri >> 8;
453 bp[5] = ri & 0xff;
454 bp[6] = m_id;
Andreas Eversberg5b277b82009-05-25 00:50:02 -0700455 bp[7] = ((tei << 1) & 0xff) | 1;
456 skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200457 if (!skb) {
458 printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
459 return;
460 }
461 mgr_send_down(mgr, skb);
462}
463
464static void
465tei_id_request(struct FsmInst *fi, int event, void *arg)
466{
467 struct teimgr *tm = fi->userdata;
468
469 if (tm->l2->tei != GROUP_TEI) {
470 tm->tei_m.printdebug(&tm->tei_m,
Joe Perches475be4d2012-02-19 19:52:38 -0800471 "assign request for already assigned tei %d",
472 tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200473 return;
474 }
475 tm->ri = random_ri();
476 if (*debug & DEBUG_L2_TEI)
477 tm->tei_m.printdebug(&tm->tei_m,
Joe Perches475be4d2012-02-19 19:52:38 -0800478 "assign request ri %d", tm->ri);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200479 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
480 mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
481 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
482 tm->nval = 3;
483}
484
485static void
486tei_id_assign(struct FsmInst *fi, int event, void *arg)
487{
488 struct teimgr *tm = fi->userdata;
489 struct layer2 *l2;
490 u_char *dp = arg;
491 int ri, tei;
492
493 ri = ((unsigned int) *dp++ << 8);
494 ri += *dp++;
495 dp++;
496 tei = *dp >> 1;
497 if (*debug & DEBUG_L2_TEI)
498 tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800499 ri, tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200500 l2 = findtei(tm->mgr, tei);
501 if (l2) { /* same tei is in use */
502 if (ri != l2->tm->ri) {
503 tm->tei_m.printdebug(fi,
Joe Perches475be4d2012-02-19 19:52:38 -0800504 "possible duplicate assignment tei %d", tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200505 tei_l2(l2, MDL_ERROR_RSP, 0);
506 }
507 } else if (ri == tm->ri) {
508 mISDN_FsmDelTimer(&tm->timer, 1);
509 mISDN_FsmChangeState(fi, ST_TEI_NOP);
510 tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
511 }
512}
513
514static void
515tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
516{
517 struct teimgr *tm = fi->userdata;
518 struct layer2 *l2;
519 u_char *dp = arg;
520 int tei, ri;
521
522 ri = ((unsigned int) *dp++ << 8);
523 ri += *dp++;
524 dp++;
525 tei = *dp >> 1;
526 if (*debug & DEBUG_L2_TEI)
527 tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800528 ri, tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200529 l2 = findtei(tm->mgr, tei);
530 if (l2) { /* same tei is in use */
531 if (ri != l2->tm->ri) { /* and it wasn't our request */
532 tm->tei_m.printdebug(fi,
Joe Perches475be4d2012-02-19 19:52:38 -0800533 "possible duplicate assignment tei %d", tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200534 mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
535 }
536 }
537}
538
539static void
540tei_id_denied(struct FsmInst *fi, int event, void *arg)
541{
542 struct teimgr *tm = fi->userdata;
543 u_char *dp = arg;
544 int ri, tei;
545
546 ri = ((unsigned int) *dp++ << 8);
547 ri += *dp++;
548 dp++;
549 tei = *dp >> 1;
550 if (*debug & DEBUG_L2_TEI)
551 tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800552 ri, tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200553}
554
555static void
556tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
557{
558 struct teimgr *tm = fi->userdata;
559 u_char *dp = arg;
560 int tei;
561
Joe Perches475be4d2012-02-19 19:52:38 -0800562 tei = *(dp + 3) >> 1;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200563 if (*debug & DEBUG_L2_TEI)
564 tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
565 if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
Joe Perches475be4d2012-02-19 19:52:38 -0800566 (tei == tm->l2->tei))) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200567 mISDN_FsmDelTimer(&tm->timer, 4);
568 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
569 put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
570 }
571}
572
573static void
574tei_id_remove(struct FsmInst *fi, int event, void *arg)
575{
576 struct teimgr *tm = fi->userdata;
577 u_char *dp = arg;
578 int tei;
579
Joe Perches475be4d2012-02-19 19:52:38 -0800580 tei = *(dp + 3) >> 1;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200581 if (*debug & DEBUG_L2_TEI)
582 tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
583 if ((tm->l2->tei != GROUP_TEI) &&
584 ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
585 mISDN_FsmDelTimer(&tm->timer, 5);
586 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
587 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
588 }
589}
590
591static void
592tei_id_verify(struct FsmInst *fi, int event, void *arg)
593{
594 struct teimgr *tm = fi->userdata;
595
596 if (*debug & DEBUG_L2_TEI)
597 tm->tei_m.printdebug(fi, "id verify request for tei %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800598 tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200599 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
600 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
601 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
602 tm->nval = 2;
603}
604
605static void
606tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
607{
608 struct teimgr *tm = fi->userdata;
609
610 if (--tm->nval) {
611 tm->ri = random_ri();
612 if (*debug & DEBUG_L2_TEI)
613 tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800614 4 - tm->nval, tm->ri);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200615 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
616 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
617 } else {
618 tm->tei_m.printdebug(fi, "assign req failed");
619 tei_l2(tm->l2, MDL_ERROR_RSP, 0);
620 mISDN_FsmChangeState(fi, ST_TEI_NOP);
621 }
622}
623
624static void
625tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
626{
627 struct teimgr *tm = fi->userdata;
628
629 if (--tm->nval) {
630 if (*debug & DEBUG_L2_TEI)
631 tm->tei_m.printdebug(fi,
Joe Perches475be4d2012-02-19 19:52:38 -0800632 "id verify req(%d) for tei %d",
633 3 - tm->nval, tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200634 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
635 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
636 } else {
637 tm->tei_m.printdebug(fi, "verify req for tei %d failed",
Joe Perches475be4d2012-02-19 19:52:38 -0800638 tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200639 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
640 mISDN_FsmChangeState(fi, ST_TEI_NOP);
641 }
642}
643
644static struct FsmNode TeiFnListUser[] =
645{
646 {ST_TEI_NOP, EV_IDREQ, tei_id_request},
647 {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
648 {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
649 {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
650 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
651 {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
652 {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
653 {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
654 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
655 {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
656 {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
657};
658
659static void
660tei_l2remove(struct layer2 *l2)
661{
662 put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
663 tei_l2(l2, MDL_REMOVE_REQ, 0);
664 list_del(&l2->ch.list);
665 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
666}
667
668static void
669tei_assign_req(struct FsmInst *fi, int event, void *arg)
670{
671 struct teimgr *tm = fi->userdata;
672 u_char *dp = arg;
673
674 if (tm->l2->tei == GROUP_TEI) {
675 tm->tei_m.printdebug(&tm->tei_m,
Joe Perches475be4d2012-02-19 19:52:38 -0800676 "net tei assign request without tei");
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200677 return;
678 }
679 tm->ri = ((unsigned int) *dp++ << 8);
680 tm->ri += *dp++;
681 if (*debug & DEBUG_L2_TEI)
682 tm->tei_m.printdebug(&tm->tei_m,
Joe Perches475be4d2012-02-19 19:52:38 -0800683 "net assign request ri %d teim %d", tm->ri, *dp);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200684 put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
685 mISDN_FsmChangeState(fi, ST_TEI_NOP);
686}
687
688static void
689tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
690{
691 struct teimgr *tm = fi->userdata;
692
693 if (*debug & DEBUG_L2_TEI)
694 tm->tei_m.printdebug(fi, "id check request for tei %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800695 tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200696 tm->rcnt = 0;
697 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
698 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
699 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
700 tm->nval = 2;
701}
702
703static void
704tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
705{
706 struct teimgr *tm = fi->userdata;
707 u_char *dp = arg;
708 int tei;
709
710 tei = dp[3] >> 1;
711 if (*debug & DEBUG_L2_TEI)
712 tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
713 if (tei == tm->l2->tei)
714 tm->rcnt++;
715}
716
717static void
718tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
719{
720 struct teimgr *tm = fi->userdata;
721 u_char *dp = arg;
722 int tei;
723
724 tei = dp[3] >> 1;
725 if (*debug & DEBUG_L2_TEI)
726 tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
Joe Perches475be4d2012-02-19 19:52:38 -0800727 tei, tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200728 if (tei == tm->l2->tei)
729 tei_id_chk_req_net(fi, event, arg);
730}
731
732static void
733tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
734{
735 struct teimgr *tm = fi->userdata;
736
737 if (tm->rcnt == 1) {
738 if (*debug & DEBUG_L2_TEI)
739 tm->tei_m.printdebug(fi,
Joe Perches475be4d2012-02-19 19:52:38 -0800740 "check req for tei %d successful\n", tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200741 mISDN_FsmChangeState(fi, ST_TEI_NOP);
742 } else if (tm->rcnt > 1) {
743 /* duplicate assignment; remove */
744 tei_l2remove(tm->l2);
745 } else if (--tm->nval) {
746 if (*debug & DEBUG_L2_TEI)
747 tm->tei_m.printdebug(fi,
Joe Perches475be4d2012-02-19 19:52:38 -0800748 "id check req(%d) for tei %d",
749 3 - tm->nval, tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200750 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
751 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
752 } else {
753 tm->tei_m.printdebug(fi, "check req for tei %d failed",
Joe Perches475be4d2012-02-19 19:52:38 -0800754 tm->l2->tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200755 mISDN_FsmChangeState(fi, ST_TEI_NOP);
756 tei_l2remove(tm->l2);
757 }
758}
759
760static struct FsmNode TeiFnListNet[] =
761{
762 {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
763 {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
764 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
765 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
766 {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
767};
768
769static void
770tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
771{
772 if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
773 return;
774 if (*debug & DEBUG_L2_TEI)
775 tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
776 if (mt == ID_ASSIGNED)
777 mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
778 else if (mt == ID_DENIED)
779 mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
780 else if (mt == ID_CHK_REQ)
781 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
782 else if (mt == ID_REMOVE)
783 mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
784 else if (mt == ID_VERIFY)
785 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
786 else if (mt == ID_CHK_RES)
787 mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
788}
789
790static struct layer2 *
Andreas Eversberg5b277b82009-05-25 00:50:02 -0700791create_new_tei(struct manager *mgr, int tei, int sapi)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200792{
793 u_long opt = 0;
794 u_long flags;
795 int id;
796 struct layer2 *l2;
797
798 if (!mgr->up)
799 return NULL;
Andreas Eversberg5b277b82009-05-25 00:50:02 -0700800 if ((tei >= 0) && (tei < 64))
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200801 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
802 if (mgr->ch.st->dev->Dprotocols
Joe Perches475be4d2012-02-19 19:52:38 -0800803 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200804 test_and_set_bit(OPTION_L2_PMX, &opt);
Andreas Eversberg5b277b82009-05-25 00:50:02 -0700805 l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200806 if (!l2) {
807 printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
808 return NULL;
809 }
810 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
811 if (!l2->tm) {
812 kfree(l2);
813 printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
814 return NULL;
815 }
816 l2->tm->mgr = mgr;
817 l2->tm->l2 = l2;
818 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
819 l2->tm->tei_m.userdata = l2->tm;
820 l2->tm->tei_m.printdebug = tei_debug;
821 l2->tm->tei_m.fsm = &teifsmn;
822 l2->tm->tei_m.state = ST_TEI_NOP;
823 l2->tm->tval = 2000; /* T202 2 sec */
824 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
825 write_lock_irqsave(&mgr->lock, flags);
826 id = get_free_id(mgr);
827 list_add_tail(&l2->list, &mgr->layer2);
828 write_unlock_irqrestore(&mgr->lock, flags);
829 if (id < 0) {
830 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
831 printk(KERN_WARNING "%s:no free id\n", __func__);
832 return NULL;
833 } else {
834 l2->ch.nr = id;
835 __add_layer2(&l2->ch, mgr->ch.st);
836 l2->ch.recv = mgr->ch.recv;
837 l2->ch.peer = mgr->ch.peer;
838 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
839 }
840 return l2;
841}
842
843static void
844new_tei_req(struct manager *mgr, u_char *dp)
845{
846 int tei, ri;
847 struct layer2 *l2;
848
849 ri = dp[0] << 8;
850 ri += dp[1];
851 if (!mgr->up)
852 goto denied;
Andreas Eversberg5b277b82009-05-25 00:50:02 -0700853 if (!(dp[3] & 1)) /* Extension bit != 1 */
854 goto denied;
855 if (dp[3] != 0xff)
856 tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
857 else
858 tei = get_free_tei(mgr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200859 if (tei < 0) {
860 printk(KERN_WARNING "%s:No free tei\n", __func__);
861 goto denied;
862 }
Andreas Eversberg5b277b82009-05-25 00:50:02 -0700863 l2 = create_new_tei(mgr, tei, CTRL_SAPI);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200864 if (!l2)
865 goto denied;
866 else
867 mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
868 return;
869denied:
870 put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
871}
872
873static int
874ph_data_ind(struct manager *mgr, struct sk_buff *skb)
875{
876 int ret = -EINVAL;
Karsten Keil81644912009-06-09 14:38:39 +0200877 struct layer2 *l2, *nl2;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200878 u_char mt;
879
880 if (skb->len < 8) {
881 if (*debug & DEBUG_L2_TEI)
882 printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800883 __func__, skb->len);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200884 goto done;
885 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200886
887 if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
888 goto done;
889 if (skb->data[0] & 1) /* EA0 formal error */
890 goto done;
891 if (!(skb->data[1] & 1)) /* EA1 formal error */
892 goto done;
893 if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
894 goto done;
895 if ((skb->data[2] & 0xef) != UI) /* not UI */
896 goto done;
897 if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
898 goto done;
899 mt = skb->data[6];
900 switch (mt) {
901 case ID_REQUEST:
902 case ID_CHK_RES:
903 case ID_VERIFY:
904 if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
905 goto done;
906 break;
907 case ID_ASSIGNED:
908 case ID_DENIED:
909 case ID_CHK_REQ:
910 case ID_REMOVE:
911 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
912 goto done;
913 break;
914 default:
915 goto done;
916 }
917 ret = 0;
918 if (mt == ID_REQUEST) {
919 new_tei_req(mgr, &skb->data[4]);
920 goto done;
921 }
Karsten Keil81644912009-06-09 14:38:39 +0200922 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200923 tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
924 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200925done:
926 return ret;
927}
928
929int
930l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
931{
932 struct teimgr *tm = l2->tm;
933
934 if (test_bit(FLG_FIXED_TEI, &l2->flag))
935 return 0;
936 if (*debug & DEBUG_L2_TEI)
937 printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
938 switch (cmd) {
939 case MDL_ASSIGN_IND:
940 mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
941 break;
942 case MDL_ERROR_IND:
943 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
944 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
945 if (test_bit(MGR_OPT_USER, &tm->mgr->options))
946 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
947 break;
948 case MDL_STATUS_UP_IND:
949 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
950 mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
951 break;
952 case MDL_STATUS_DOWN_IND:
953 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
954 mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
955 break;
956 case MDL_STATUS_UI_IND:
957 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
958 mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
959 break;
960 }
961 return 0;
962}
963
964void
Karsten Keilc5b61d52008-07-27 18:32:50 +0200965TEIrelease(struct layer2 *l2)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200966{
967 struct teimgr *tm = l2->tm;
968 u_long flags;
969
970 mISDN_FsmDelTimer(&tm->timer, 1);
971 write_lock_irqsave(&tm->mgr->lock, flags);
972 list_del(&l2->list);
973 write_unlock_irqrestore(&tm->mgr->lock, flags);
974 l2->tm = NULL;
975 kfree(tm);
976}
977
978static int
979create_teimgr(struct manager *mgr, struct channel_req *crq)
980{
981 struct layer2 *l2;
Joe Perches475be4d2012-02-19 19:52:38 -0800982 u_long opt = 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200983 u_long flags;
984 int id;
985
986 if (*debug & DEBUG_L2_TEI)
987 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800988 __func__, dev_name(&mgr->ch.st->dev->dev),
989 crq->protocol, crq->adr.dev, crq->adr.channel,
990 crq->adr.sapi, crq->adr.tei);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200991 if (crq->adr.tei > GROUP_TEI)
992 return -EINVAL;
993 if (crq->adr.tei < 64)
994 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
995 if (crq->adr.tei == 0)
996 test_and_set_bit(OPTION_L2_PTP, &opt);
997 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
998 if (crq->protocol == ISDN_P_LAPD_TE)
999 return -EPROTONOSUPPORT;
1000 if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
1001 return -EINVAL;
1002 if (mgr->up) {
1003 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001004 "%s: only one network manager is allowed\n",
1005 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001006 return -EBUSY;
1007 }
1008 } else if (test_bit(MGR_OPT_USER, &mgr->options)) {
1009 if (crq->protocol == ISDN_P_LAPD_NT)
1010 return -EPROTONOSUPPORT;
1011 if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
1012 return -EINVAL; /* dyn tei */
1013 } else {
1014 if (crq->protocol == ISDN_P_LAPD_NT)
1015 test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
1016 if (crq->protocol == ISDN_P_LAPD_TE)
1017 test_and_set_bit(MGR_OPT_USER, &mgr->options);
1018 }
1019 if (mgr->ch.st->dev->Dprotocols
Joe Perches475be4d2012-02-19 19:52:38 -08001020 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001021 test_and_set_bit(OPTION_L2_PMX, &opt);
1022 if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
1023 mgr->up = crq->ch;
1024 id = DL_INFO_L2_CONNECT;
1025 teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
Andreas Eversberg82107b72012-05-04 04:15:30 +00001026 if (test_bit(MGR_PH_ACTIVE, &mgr->options))
1027 teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001028 crq->ch = NULL;
1029 if (!list_empty(&mgr->layer2)) {
1030 read_lock_irqsave(&mgr->lock, flags);
1031 list_for_each_entry(l2, &mgr->layer2, list) {
1032 l2->up = mgr->up;
1033 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
1034 }
1035 read_unlock_irqrestore(&mgr->lock, flags);
1036 }
1037 return 0;
1038 }
Andreas Eversberg5b277b82009-05-25 00:50:02 -07001039 l2 = create_l2(crq->ch, crq->protocol, opt,
Joe Perches475be4d2012-02-19 19:52:38 -08001040 crq->adr.tei, crq->adr.sapi);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001041 if (!l2)
1042 return -ENOMEM;
1043 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
1044 if (!l2->tm) {
1045 kfree(l2);
1046 printk(KERN_ERR "kmalloc teimgr failed\n");
1047 return -ENOMEM;
1048 }
1049 l2->tm->mgr = mgr;
1050 l2->tm->l2 = l2;
1051 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
1052 l2->tm->tei_m.userdata = l2->tm;
1053 l2->tm->tei_m.printdebug = tei_debug;
1054 if (crq->protocol == ISDN_P_LAPD_TE) {
1055 l2->tm->tei_m.fsm = &teifsmu;
1056 l2->tm->tei_m.state = ST_TEI_NOP;
1057 l2->tm->tval = 1000; /* T201 1 sec */
1058 } else {
1059 l2->tm->tei_m.fsm = &teifsmn;
1060 l2->tm->tei_m.state = ST_TEI_NOP;
1061 l2->tm->tval = 2000; /* T202 2 sec */
1062 }
1063 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
1064 write_lock_irqsave(&mgr->lock, flags);
1065 id = get_free_id(mgr);
1066 list_add_tail(&l2->list, &mgr->layer2);
1067 write_unlock_irqrestore(&mgr->lock, flags);
1068 if (id < 0) {
1069 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1070 } else {
1071 l2->ch.nr = id;
1072 l2->up->nr = id;
1073 crq->ch = &l2->ch;
1074 id = 0;
1075 }
1076 return id;
1077}
1078
1079static int
1080mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
1081{
1082 struct manager *mgr;
1083 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1084 int ret = -EINVAL;
1085
1086 mgr = container_of(ch, struct manager, ch);
1087 if (*debug & DEBUG_L2_RECV)
1088 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001089 __func__, hh->prim, hh->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001090 switch (hh->prim) {
1091 case PH_DATA_IND:
1092 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
1093 ret = ph_data_ind(mgr, skb);
1094 break;
1095 case PH_DATA_CNF:
1096 do_ack(mgr, hh->id);
1097 ret = 0;
1098 break;
1099 case PH_ACTIVATE_IND:
1100 test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
Andreas Eversberg82107b72012-05-04 04:15:30 +00001101 if (mgr->up)
1102 teiup_create(mgr, PH_ACTIVATE_IND, 0, NULL);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001103 mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
1104 do_send(mgr);
1105 ret = 0;
1106 break;
1107 case PH_DEACTIVATE_IND:
1108 test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
Andreas Eversberg82107b72012-05-04 04:15:30 +00001109 if (mgr->up)
1110 teiup_create(mgr, PH_DEACTIVATE_IND, 0, NULL);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001111 mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
1112 ret = 0;
1113 break;
1114 case DL_UNITDATA_REQ:
1115 return dl_unit_data(mgr, skb);
1116 }
1117 if (!ret)
1118 dev_kfree_skb(skb);
1119 return ret;
1120}
1121
1122static int
1123free_teimanager(struct manager *mgr)
1124{
1125 struct layer2 *l2, *nl2;
1126
Andreas Eversberge73f6b22009-05-22 11:04:48 +00001127 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001128 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
1129 /* not locked lock is taken in release tei */
1130 mgr->up = NULL;
1131 if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
1132 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1133 put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
1134 mutex_lock(&mgr->ch.st->lmutex);
1135 list_del(&l2->ch.list);
1136 mutex_unlock(&mgr->ch.st->lmutex);
1137 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1138 }
1139 test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
1140 } else {
1141 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1142 l2->up = NULL;
1143 }
1144 }
1145 }
1146 if (test_bit(MGR_OPT_USER, &mgr->options)) {
1147 if (list_empty(&mgr->layer2))
1148 test_and_clear_bit(MGR_OPT_USER, &mgr->options);
1149 }
1150 mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
1151 return 0;
1152}
1153
1154static int
1155ctrl_teimanager(struct manager *mgr, void *arg)
1156{
1157 /* currently we only have one option */
Andreas Eversberge73f6b22009-05-22 11:04:48 +00001158 int *val = (int *)arg;
1159 int ret = 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001160
Andreas Eversberge73f6b22009-05-22 11:04:48 +00001161 switch (val[0]) {
1162 case IMCLEAR_L2:
1163 if (val[1])
1164 test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
1165 else
1166 test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
1167 break;
1168 case IMHOLD_L1:
1169 if (val[1])
1170 test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
1171 else
1172 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1173 break;
1174 default:
1175 ret = -EINVAL;
1176 }
1177 return ret;
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001178}
1179
1180/* This function does create a L2 for fixed TEI in NT Mode */
1181static int
1182check_data(struct manager *mgr, struct sk_buff *skb)
1183{
1184 struct mISDNhead *hh = mISDN_HEAD_P(skb);
Andreas Eversberg5b277b82009-05-25 00:50:02 -07001185 int ret, tei, sapi;
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001186 struct layer2 *l2;
1187
1188 if (*debug & DEBUG_L2_CTRL)
1189 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001190 __func__, hh->prim, hh->id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001191 if (test_bit(MGR_OPT_USER, &mgr->options))
1192 return -ENOTCONN;
1193 if (hh->prim != PH_DATA_IND)
1194 return -ENOTCONN;
1195 if (skb->len != 3)
1196 return -ENOTCONN;
Andreas Eversberg5b277b82009-05-25 00:50:02 -07001197 if (skb->data[0] & 3) /* EA0 and CR must be 0 */
1198 return -EINVAL;
1199 sapi = skb->data[0] >> 2;
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001200 if (!(skb->data[1] & 1)) /* invalid EA1 */
1201 return -EINVAL;
Andreas Eversberg5b277b82009-05-25 00:50:02 -07001202 tei = skb->data[1] >> 1;
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001203 if (tei > 63) /* not a fixed tei */
1204 return -ENOTCONN;
1205 if ((skb->data[2] & ~0x10) != SABME)
1206 return -ENOTCONN;
1207 /* We got a SABME for a fixed TEI */
Andreas Eversbergd7965092009-05-22 11:04:51 +00001208 if (*debug & DEBUG_L2_CTRL)
1209 printk(KERN_DEBUG "%s: SABME sapi(%d) tei(%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001210 __func__, sapi, tei);
Andreas Eversberg5b277b82009-05-25 00:50:02 -07001211 l2 = create_new_tei(mgr, tei, sapi);
Andreas Eversbergd7965092009-05-22 11:04:51 +00001212 if (!l2) {
1213 if (*debug & DEBUG_L2_CTRL)
1214 printk(KERN_DEBUG "%s: failed to create new tei\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001215 __func__);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001216 return -ENOMEM;
Andreas Eversbergd7965092009-05-22 11:04:51 +00001217 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001218 ret = l2->ch.send(&l2->ch, skb);
1219 return ret;
1220}
1221
1222void
1223delete_teimanager(struct mISDNchannel *ch)
1224{
1225 struct manager *mgr;
1226 struct layer2 *l2, *nl2;
1227
1228 mgr = container_of(ch, struct manager, ch);
1229 /* not locked lock is taken in release tei */
1230 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1231 mutex_lock(&mgr->ch.st->lmutex);
1232 list_del(&l2->ch.list);
1233 mutex_unlock(&mgr->ch.st->lmutex);
1234 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1235 }
1236 list_del(&mgr->ch.list);
1237 list_del(&mgr->bcast.list);
1238 skb_queue_purge(&mgr->sendq);
1239 kfree(mgr);
1240}
1241
1242static int
1243mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1244{
1245 struct manager *mgr;
1246 int ret = -EINVAL;
1247
1248 mgr = container_of(ch, struct manager, ch);
1249 if (*debug & DEBUG_L2_CTRL)
1250 printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
1251 switch (cmd) {
1252 case OPEN_CHANNEL:
1253 ret = create_teimgr(mgr, arg);
1254 break;
1255 case CLOSE_CHANNEL:
1256 ret = free_teimanager(mgr);
1257 break;
1258 case CONTROL_CHANNEL:
1259 ret = ctrl_teimanager(mgr, arg);
1260 break;
1261 case CHECK_DATA:
1262 ret = check_data(mgr, arg);
1263 break;
1264 }
1265 return ret;
1266}
1267
1268static int
1269mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
1270{
1271 struct manager *mgr = container_of(ch, struct manager, bcast);
1272 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1273 struct sk_buff *cskb = NULL;
1274 struct layer2 *l2;
1275 u_long flags;
1276 int ret;
1277
1278 read_lock_irqsave(&mgr->lock, flags);
1279 list_for_each_entry(l2, &mgr->layer2, list) {
1280 if ((hh->id & MISDN_ID_SAPI_MASK) ==
1281 (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
1282 if (list_is_last(&l2->list, &mgr->layer2)) {
1283 cskb = skb;
1284 skb = NULL;
1285 } else {
1286 if (!cskb)
1287 cskb = skb_copy(skb, GFP_KERNEL);
1288 }
1289 if (cskb) {
1290 ret = l2->ch.send(&l2->ch, cskb);
1291 if (ret) {
1292 if (*debug & DEBUG_SEND_ERR)
1293 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001294 "%s ch%d prim(%x) addr(%x)"
1295 " err %d\n",
1296 __func__, l2->ch.nr,
1297 hh->prim, l2->ch.addr, ret);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001298 } else
1299 cskb = NULL;
1300 } else {
1301 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001302 __func__, ch->nr, ch->addr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001303 goto out;
1304 }
1305 }
1306 }
1307out:
1308 read_unlock_irqrestore(&mgr->lock, flags);
1309 if (cskb)
1310 dev_kfree_skb(cskb);
1311 if (skb)
1312 dev_kfree_skb(skb);
1313 return 0;
1314}
1315
1316static int
1317mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1318{
1319
1320 return -EINVAL;
1321}
1322
1323int
1324create_teimanager(struct mISDNdevice *dev)
1325{
1326 struct manager *mgr;
1327
1328 mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
1329 if (!mgr)
1330 return -ENOMEM;
1331 INIT_LIST_HEAD(&mgr->layer2);
Karsten Keil702c7902008-12-12 21:21:57 -08001332 rwlock_init(&mgr->lock);
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001333 skb_queue_head_init(&mgr->sendq);
1334 mgr->nextid = 1;
1335 mgr->lastid = MISDN_ID_NONE;
1336 mgr->ch.send = mgr_send;
1337 mgr->ch.ctrl = mgr_ctrl;
1338 mgr->ch.st = dev->D.st;
1339 set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
1340 add_layer2(&mgr->ch, dev->D.st);
1341 mgr->bcast.send = mgr_bcast;
1342 mgr->bcast.ctrl = mgr_bcast_ctrl;
1343 mgr->bcast.st = dev->D.st;
1344 set_channel_address(&mgr->bcast, 0, GROUP_TEI);
1345 add_layer2(&mgr->bcast, dev->D.st);
1346 mgr->deact.debug = *debug & DEBUG_MANAGER;
1347 mgr->deact.userdata = mgr;
1348 mgr->deact.printdebug = da_debug;
1349 mgr->deact.fsm = &deactfsm;
1350 mgr->deact.state = ST_L1_DEACT;
1351 mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
1352 dev->teimgr = &mgr->ch;
1353 return 0;
1354}
1355
1356int TEIInit(u_int *deb)
1357{
1358 debug = deb;
1359 teifsmu.state_count = TEI_STATE_COUNT;
1360 teifsmu.event_count = TEI_EVENT_COUNT;
1361 teifsmu.strEvent = strTeiEvent;
1362 teifsmu.strState = strTeiState;
1363 mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
1364 teifsmn.state_count = TEI_STATE_COUNT;
1365 teifsmn.event_count = TEI_EVENT_COUNT;
1366 teifsmn.strEvent = strTeiEvent;
1367 teifsmn.strState = strTeiState;
1368 mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
1369 deactfsm.state_count = DEACT_STATE_COUNT;
1370 deactfsm.event_count = DEACT_EVENT_COUNT;
1371 deactfsm.strEvent = strDeactEvent;
1372 deactfsm.strState = strDeactState;
1373 mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
1374 return 0;
1375}
1376
1377void TEIFree(void)
1378{
1379 mISDN_FsmFree(&teifsmu);
1380 mISDN_FsmFree(&teifsmn);
1381 mISDN_FsmFree(&deactfsm);
1382}