Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 36 | static u_int *debug; |
| 37 | |
| 38 | static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL}; |
| 39 | static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL}; |
| 40 | static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL}; |
| 41 | |
| 42 | enum { |
| 43 | ST_L1_DEACT, |
| 44 | ST_L1_DEACT_PENDING, |
| 45 | ST_L1_ACTIV, |
| 46 | }; |
| 47 | #define DEACT_STATE_COUNT (ST_L1_ACTIV+1) |
| 48 | |
| 49 | static char *strDeactState[] = |
| 50 | { |
| 51 | "ST_L1_DEACT", |
| 52 | "ST_L1_DEACT_PENDING", |
| 53 | "ST_L1_ACTIV", |
| 54 | }; |
| 55 | |
| 56 | enum { |
| 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 | |
| 67 | static 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 | |
| 77 | static void |
| 78 | da_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 | |
| 92 | static void |
| 93 | da_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 | |
| 102 | static void |
| 103 | da_deactivate_ind(struct FsmInst *fi, int event, void *arg) |
| 104 | { |
| 105 | mISDN_FsmChangeState(fi, ST_L1_DEACT); |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | da_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 Eversberg | e73f6b2 | 2009-05-22 11:04:48 +0000 | [diff] [blame] | 125 | 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 Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void |
| 133 | da_ui(struct FsmInst *fi, int event, void *arg) |
| 134 | { |
| 135 | struct manager *mgr = fi->userdata; |
| 136 | |
| 137 | /* restart da timer */ |
Andreas Eversberg | e73f6b2 | 2009-05-22 11:04:48 +0000 | [diff] [blame] | 138 | 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 Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void |
| 146 | da_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 | |
| 169 | static 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 | |
| 179 | enum { |
| 180 | ST_TEI_NOP, |
| 181 | ST_TEI_IDREQ, |
| 182 | ST_TEI_IDVERIFY, |
| 183 | }; |
| 184 | |
| 185 | #define TEI_STATE_COUNT (ST_TEI_IDVERIFY+1) |
| 186 | |
| 187 | static char *strTeiState[] = |
| 188 | { |
| 189 | "ST_TEI_NOP", |
| 190 | "ST_TEI_IDREQ", |
| 191 | "ST_TEI_IDVERIFY", |
| 192 | }; |
| 193 | |
| 194 | enum { |
| 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 | |
| 208 | static 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 | |
| 221 | static void |
| 222 | tei_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); |
Andreas Eversberg | d796509 | 2009-05-22 11:04:51 +0000 | [diff] [blame^] | 230 | printk(KERN_DEBUG "sapi(%d) tei(%d): ", tm->l2->sapi, tm->l2->tei); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 231 | vprintk(fmt, va); |
| 232 | printk("\n"); |
| 233 | va_end(va); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | static int |
| 239 | get_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 | |
| 262 | static int |
| 263 | get_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 | |
| 289 | static void |
| 290 | teiup_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 | |
| 311 | static u_int |
| 312 | new_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 | |
| 325 | static void |
| 326 | do_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 | |
| 348 | static void |
| 349 | do_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 | |
| 370 | static void |
| 371 | mgr_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 | |
| 382 | static int |
| 383 | dl_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 Eder | 5b83435 | 2008-12-12 21:15:17 -0800 | [diff] [blame] | 401 | static unsigned int |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 402 | random_ri(void) |
| 403 | { |
| 404 | u16 x; |
| 405 | |
| 406 | get_random_bytes(&x, sizeof(x)); |
| 407 | return x; |
| 408 | } |
| 409 | |
| 410 | static struct layer2 * |
| 411 | findtei(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; |
| 423 | done: |
| 424 | read_unlock_irqrestore(&mgr->lock, flags); |
| 425 | return l2; |
| 426 | } |
| 427 | |
| 428 | static void |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 429 | put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 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; |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 443 | bp[7] = ((tei << 1) & 0xff) | 1; |
| 444 | skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 445 | if (!skb) { |
| 446 | printk(KERN_WARNING "%s: no skb for tei msg\n", __func__); |
| 447 | return; |
| 448 | } |
| 449 | mgr_send_down(mgr, skb); |
| 450 | } |
| 451 | |
| 452 | static void |
| 453 | tei_id_request(struct FsmInst *fi, int event, void *arg) |
| 454 | { |
| 455 | struct teimgr *tm = fi->userdata; |
| 456 | |
| 457 | if (tm->l2->tei != GROUP_TEI) { |
| 458 | tm->tei_m.printdebug(&tm->tei_m, |
| 459 | "assign request for allready assigned tei %d", |
| 460 | tm->l2->tei); |
| 461 | return; |
| 462 | } |
| 463 | tm->ri = random_ri(); |
| 464 | if (*debug & DEBUG_L2_TEI) |
| 465 | tm->tei_m.printdebug(&tm->tei_m, |
| 466 | "assign request ri %d", tm->ri); |
| 467 | put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI); |
| 468 | mISDN_FsmChangeState(fi, ST_TEI_IDREQ); |
| 469 | mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1); |
| 470 | tm->nval = 3; |
| 471 | } |
| 472 | |
| 473 | static void |
| 474 | tei_id_assign(struct FsmInst *fi, int event, void *arg) |
| 475 | { |
| 476 | struct teimgr *tm = fi->userdata; |
| 477 | struct layer2 *l2; |
| 478 | u_char *dp = arg; |
| 479 | int ri, tei; |
| 480 | |
| 481 | ri = ((unsigned int) *dp++ << 8); |
| 482 | ri += *dp++; |
| 483 | dp++; |
| 484 | tei = *dp >> 1; |
| 485 | if (*debug & DEBUG_L2_TEI) |
| 486 | tm->tei_m.printdebug(fi, "identity assign ri %d tei %d", |
| 487 | ri, tei); |
| 488 | l2 = findtei(tm->mgr, tei); |
| 489 | if (l2) { /* same tei is in use */ |
| 490 | if (ri != l2->tm->ri) { |
| 491 | tm->tei_m.printdebug(fi, |
| 492 | "possible duplicate assignment tei %d", tei); |
| 493 | tei_l2(l2, MDL_ERROR_RSP, 0); |
| 494 | } |
| 495 | } else if (ri == tm->ri) { |
| 496 | mISDN_FsmDelTimer(&tm->timer, 1); |
| 497 | mISDN_FsmChangeState(fi, ST_TEI_NOP); |
| 498 | tei_l2(tm->l2, MDL_ASSIGN_REQ, tei); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | static void |
| 503 | tei_id_test_dup(struct FsmInst *fi, int event, void *arg) |
| 504 | { |
| 505 | struct teimgr *tm = fi->userdata; |
| 506 | struct layer2 *l2; |
| 507 | u_char *dp = arg; |
| 508 | int tei, ri; |
| 509 | |
| 510 | ri = ((unsigned int) *dp++ << 8); |
| 511 | ri += *dp++; |
| 512 | dp++; |
| 513 | tei = *dp >> 1; |
| 514 | if (*debug & DEBUG_L2_TEI) |
| 515 | tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d", |
| 516 | ri, tei); |
| 517 | l2 = findtei(tm->mgr, tei); |
| 518 | if (l2) { /* same tei is in use */ |
| 519 | if (ri != l2->tm->ri) { /* and it wasn't our request */ |
| 520 | tm->tei_m.printdebug(fi, |
| 521 | "possible duplicate assignment tei %d", tei); |
| 522 | mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | static void |
| 528 | tei_id_denied(struct FsmInst *fi, int event, void *arg) |
| 529 | { |
| 530 | struct teimgr *tm = fi->userdata; |
| 531 | u_char *dp = arg; |
| 532 | int ri, tei; |
| 533 | |
| 534 | ri = ((unsigned int) *dp++ << 8); |
| 535 | ri += *dp++; |
| 536 | dp++; |
| 537 | tei = *dp >> 1; |
| 538 | if (*debug & DEBUG_L2_TEI) |
| 539 | tm->tei_m.printdebug(fi, "identity denied ri %d tei %d", |
| 540 | ri, tei); |
| 541 | } |
| 542 | |
| 543 | static void |
| 544 | tei_id_chk_req(struct FsmInst *fi, int event, void *arg) |
| 545 | { |
| 546 | struct teimgr *tm = fi->userdata; |
| 547 | u_char *dp = arg; |
| 548 | int tei; |
| 549 | |
| 550 | tei = *(dp+3) >> 1; |
| 551 | if (*debug & DEBUG_L2_TEI) |
| 552 | tm->tei_m.printdebug(fi, "identity check req tei %d", tei); |
| 553 | if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) || |
| 554 | (tei == tm->l2->tei))) { |
| 555 | mISDN_FsmDelTimer(&tm->timer, 4); |
| 556 | mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP); |
| 557 | put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static void |
| 562 | tei_id_remove(struct FsmInst *fi, int event, void *arg) |
| 563 | { |
| 564 | struct teimgr *tm = fi->userdata; |
| 565 | u_char *dp = arg; |
| 566 | int tei; |
| 567 | |
| 568 | tei = *(dp+3) >> 1; |
| 569 | if (*debug & DEBUG_L2_TEI) |
| 570 | tm->tei_m.printdebug(fi, "identity remove tei %d", tei); |
| 571 | if ((tm->l2->tei != GROUP_TEI) && |
| 572 | ((tei == GROUP_TEI) || (tei == tm->l2->tei))) { |
| 573 | mISDN_FsmDelTimer(&tm->timer, 5); |
| 574 | mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP); |
| 575 | tei_l2(tm->l2, MDL_REMOVE_REQ, 0); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | tei_id_verify(struct FsmInst *fi, int event, void *arg) |
| 581 | { |
| 582 | struct teimgr *tm = fi->userdata; |
| 583 | |
| 584 | if (*debug & DEBUG_L2_TEI) |
| 585 | tm->tei_m.printdebug(fi, "id verify request for tei %d", |
| 586 | tm->l2->tei); |
| 587 | put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei); |
| 588 | mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY); |
| 589 | mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2); |
| 590 | tm->nval = 2; |
| 591 | } |
| 592 | |
| 593 | static void |
| 594 | tei_id_req_tout(struct FsmInst *fi, int event, void *arg) |
| 595 | { |
| 596 | struct teimgr *tm = fi->userdata; |
| 597 | |
| 598 | if (--tm->nval) { |
| 599 | tm->ri = random_ri(); |
| 600 | if (*debug & DEBUG_L2_TEI) |
| 601 | tm->tei_m.printdebug(fi, "assign req(%d) ri %d", |
| 602 | 4 - tm->nval, tm->ri); |
| 603 | put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI); |
| 604 | mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3); |
| 605 | } else { |
| 606 | tm->tei_m.printdebug(fi, "assign req failed"); |
| 607 | tei_l2(tm->l2, MDL_ERROR_RSP, 0); |
| 608 | mISDN_FsmChangeState(fi, ST_TEI_NOP); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | static void |
| 613 | tei_id_ver_tout(struct FsmInst *fi, int event, void *arg) |
| 614 | { |
| 615 | struct teimgr *tm = fi->userdata; |
| 616 | |
| 617 | if (--tm->nval) { |
| 618 | if (*debug & DEBUG_L2_TEI) |
| 619 | tm->tei_m.printdebug(fi, |
| 620 | "id verify req(%d) for tei %d", |
| 621 | 3 - tm->nval, tm->l2->tei); |
| 622 | put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei); |
| 623 | mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4); |
| 624 | } else { |
| 625 | tm->tei_m.printdebug(fi, "verify req for tei %d failed", |
| 626 | tm->l2->tei); |
| 627 | tei_l2(tm->l2, MDL_REMOVE_REQ, 0); |
| 628 | mISDN_FsmChangeState(fi, ST_TEI_NOP); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static struct FsmNode TeiFnListUser[] = |
| 633 | { |
| 634 | {ST_TEI_NOP, EV_IDREQ, tei_id_request}, |
| 635 | {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup}, |
| 636 | {ST_TEI_NOP, EV_VERIFY, tei_id_verify}, |
| 637 | {ST_TEI_NOP, EV_REMOVE, tei_id_remove}, |
| 638 | {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req}, |
| 639 | {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout}, |
| 640 | {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign}, |
| 641 | {ST_TEI_IDREQ, EV_DENIED, tei_id_denied}, |
| 642 | {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout}, |
| 643 | {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove}, |
| 644 | {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req}, |
| 645 | }; |
| 646 | |
| 647 | static void |
| 648 | tei_l2remove(struct layer2 *l2) |
| 649 | { |
| 650 | put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei); |
| 651 | tei_l2(l2, MDL_REMOVE_REQ, 0); |
| 652 | list_del(&l2->ch.list); |
| 653 | l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL); |
| 654 | } |
| 655 | |
| 656 | static void |
| 657 | tei_assign_req(struct FsmInst *fi, int event, void *arg) |
| 658 | { |
| 659 | struct teimgr *tm = fi->userdata; |
| 660 | u_char *dp = arg; |
| 661 | |
| 662 | if (tm->l2->tei == GROUP_TEI) { |
| 663 | tm->tei_m.printdebug(&tm->tei_m, |
| 664 | "net tei assign request without tei"); |
| 665 | return; |
| 666 | } |
| 667 | tm->ri = ((unsigned int) *dp++ << 8); |
| 668 | tm->ri += *dp++; |
| 669 | if (*debug & DEBUG_L2_TEI) |
| 670 | tm->tei_m.printdebug(&tm->tei_m, |
| 671 | "net assign request ri %d teim %d", tm->ri, *dp); |
| 672 | put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei); |
| 673 | mISDN_FsmChangeState(fi, ST_TEI_NOP); |
| 674 | } |
| 675 | |
| 676 | static void |
| 677 | tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg) |
| 678 | { |
| 679 | struct teimgr *tm = fi->userdata; |
| 680 | |
| 681 | if (*debug & DEBUG_L2_TEI) |
| 682 | tm->tei_m.printdebug(fi, "id check request for tei %d", |
| 683 | tm->l2->tei); |
| 684 | tm->rcnt = 0; |
| 685 | put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei); |
| 686 | mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY); |
| 687 | mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2); |
| 688 | tm->nval = 2; |
| 689 | } |
| 690 | |
| 691 | static void |
| 692 | tei_id_chk_resp(struct FsmInst *fi, int event, void *arg) |
| 693 | { |
| 694 | struct teimgr *tm = fi->userdata; |
| 695 | u_char *dp = arg; |
| 696 | int tei; |
| 697 | |
| 698 | tei = dp[3] >> 1; |
| 699 | if (*debug & DEBUG_L2_TEI) |
| 700 | tm->tei_m.printdebug(fi, "identity check resp tei %d", tei); |
| 701 | if (tei == tm->l2->tei) |
| 702 | tm->rcnt++; |
| 703 | } |
| 704 | |
| 705 | static void |
| 706 | tei_id_verify_net(struct FsmInst *fi, int event, void *arg) |
| 707 | { |
| 708 | struct teimgr *tm = fi->userdata; |
| 709 | u_char *dp = arg; |
| 710 | int tei; |
| 711 | |
| 712 | tei = dp[3] >> 1; |
| 713 | if (*debug & DEBUG_L2_TEI) |
| 714 | tm->tei_m.printdebug(fi, "identity verify req tei %d/%d", |
| 715 | tei, tm->l2->tei); |
| 716 | if (tei == tm->l2->tei) |
| 717 | tei_id_chk_req_net(fi, event, arg); |
| 718 | } |
| 719 | |
| 720 | static void |
| 721 | tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg) |
| 722 | { |
| 723 | struct teimgr *tm = fi->userdata; |
| 724 | |
| 725 | if (tm->rcnt == 1) { |
| 726 | if (*debug & DEBUG_L2_TEI) |
| 727 | tm->tei_m.printdebug(fi, |
| 728 | "check req for tei %d sucessful\n", tm->l2->tei); |
| 729 | mISDN_FsmChangeState(fi, ST_TEI_NOP); |
| 730 | } else if (tm->rcnt > 1) { |
| 731 | /* duplicate assignment; remove */ |
| 732 | tei_l2remove(tm->l2); |
| 733 | } else if (--tm->nval) { |
| 734 | if (*debug & DEBUG_L2_TEI) |
| 735 | tm->tei_m.printdebug(fi, |
| 736 | "id check req(%d) for tei %d", |
| 737 | 3 - tm->nval, tm->l2->tei); |
| 738 | put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei); |
| 739 | mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4); |
| 740 | } else { |
| 741 | tm->tei_m.printdebug(fi, "check req for tei %d failed", |
| 742 | tm->l2->tei); |
| 743 | mISDN_FsmChangeState(fi, ST_TEI_NOP); |
| 744 | tei_l2remove(tm->l2); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | static struct FsmNode TeiFnListNet[] = |
| 749 | { |
| 750 | {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req}, |
| 751 | {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net}, |
| 752 | {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net}, |
| 753 | {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net}, |
| 754 | {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp}, |
| 755 | }; |
| 756 | |
| 757 | static void |
| 758 | tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len) |
| 759 | { |
| 760 | if (test_bit(FLG_FIXED_TEI, &tm->l2->flag)) |
| 761 | return; |
| 762 | if (*debug & DEBUG_L2_TEI) |
| 763 | tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt); |
| 764 | if (mt == ID_ASSIGNED) |
| 765 | mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp); |
| 766 | else if (mt == ID_DENIED) |
| 767 | mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp); |
| 768 | else if (mt == ID_CHK_REQ) |
| 769 | mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp); |
| 770 | else if (mt == ID_REMOVE) |
| 771 | mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp); |
| 772 | else if (mt == ID_VERIFY) |
| 773 | mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp); |
| 774 | else if (mt == ID_CHK_RES) |
| 775 | mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp); |
| 776 | } |
| 777 | |
| 778 | static struct layer2 * |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 779 | create_new_tei(struct manager *mgr, int tei, int sapi) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 780 | { |
| 781 | u_long opt = 0; |
| 782 | u_long flags; |
| 783 | int id; |
| 784 | struct layer2 *l2; |
| 785 | |
| 786 | if (!mgr->up) |
| 787 | return NULL; |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 788 | if ((tei >= 0) && (tei < 64)) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 789 | test_and_set_bit(OPTION_L2_FIXEDTEI, &opt); |
| 790 | if (mgr->ch.st->dev->Dprotocols |
| 791 | & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1))) |
| 792 | test_and_set_bit(OPTION_L2_PMX, &opt); |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 793 | l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 794 | if (!l2) { |
| 795 | printk(KERN_WARNING "%s:no memory for layer2\n", __func__); |
| 796 | return NULL; |
| 797 | } |
| 798 | l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL); |
| 799 | if (!l2->tm) { |
| 800 | kfree(l2); |
| 801 | printk(KERN_WARNING "%s:no memory for teimgr\n", __func__); |
| 802 | return NULL; |
| 803 | } |
| 804 | l2->tm->mgr = mgr; |
| 805 | l2->tm->l2 = l2; |
| 806 | l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM; |
| 807 | l2->tm->tei_m.userdata = l2->tm; |
| 808 | l2->tm->tei_m.printdebug = tei_debug; |
| 809 | l2->tm->tei_m.fsm = &teifsmn; |
| 810 | l2->tm->tei_m.state = ST_TEI_NOP; |
| 811 | l2->tm->tval = 2000; /* T202 2 sec */ |
| 812 | mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer); |
| 813 | write_lock_irqsave(&mgr->lock, flags); |
| 814 | id = get_free_id(mgr); |
| 815 | list_add_tail(&l2->list, &mgr->layer2); |
| 816 | write_unlock_irqrestore(&mgr->lock, flags); |
| 817 | if (id < 0) { |
| 818 | l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL); |
| 819 | printk(KERN_WARNING "%s:no free id\n", __func__); |
| 820 | return NULL; |
| 821 | } else { |
| 822 | l2->ch.nr = id; |
| 823 | __add_layer2(&l2->ch, mgr->ch.st); |
| 824 | l2->ch.recv = mgr->ch.recv; |
| 825 | l2->ch.peer = mgr->ch.peer; |
| 826 | l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL); |
| 827 | } |
| 828 | return l2; |
| 829 | } |
| 830 | |
| 831 | static void |
| 832 | new_tei_req(struct manager *mgr, u_char *dp) |
| 833 | { |
| 834 | int tei, ri; |
| 835 | struct layer2 *l2; |
| 836 | |
| 837 | ri = dp[0] << 8; |
| 838 | ri += dp[1]; |
| 839 | if (!mgr->up) |
| 840 | goto denied; |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 841 | if (!(dp[3] & 1)) /* Extension bit != 1 */ |
| 842 | goto denied; |
| 843 | if (dp[3] != 0xff) |
| 844 | tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */ |
| 845 | else |
| 846 | tei = get_free_tei(mgr); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 847 | if (tei < 0) { |
| 848 | printk(KERN_WARNING "%s:No free tei\n", __func__); |
| 849 | goto denied; |
| 850 | } |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 851 | l2 = create_new_tei(mgr, tei, CTRL_SAPI); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 852 | if (!l2) |
| 853 | goto denied; |
| 854 | else |
| 855 | mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp); |
| 856 | return; |
| 857 | denied: |
| 858 | put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI); |
| 859 | } |
| 860 | |
| 861 | static int |
| 862 | ph_data_ind(struct manager *mgr, struct sk_buff *skb) |
| 863 | { |
| 864 | int ret = -EINVAL; |
| 865 | struct layer2 *l2; |
| 866 | u_long flags; |
| 867 | u_char mt; |
| 868 | |
| 869 | if (skb->len < 8) { |
| 870 | if (*debug & DEBUG_L2_TEI) |
| 871 | printk(KERN_DEBUG "%s: short mgr frame %d/8\n", |
| 872 | __func__, skb->len); |
| 873 | goto done; |
| 874 | } |
| 875 | if (*debug & DEBUG_L2_TEI) |
| 876 | |
| 877 | if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */ |
| 878 | goto done; |
| 879 | if (skb->data[0] & 1) /* EA0 formal error */ |
| 880 | goto done; |
| 881 | if (!(skb->data[1] & 1)) /* EA1 formal error */ |
| 882 | goto done; |
| 883 | if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */ |
| 884 | goto done; |
| 885 | if ((skb->data[2] & 0xef) != UI) /* not UI */ |
| 886 | goto done; |
| 887 | if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */ |
| 888 | goto done; |
| 889 | mt = skb->data[6]; |
| 890 | switch (mt) { |
| 891 | case ID_REQUEST: |
| 892 | case ID_CHK_RES: |
| 893 | case ID_VERIFY: |
| 894 | if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) |
| 895 | goto done; |
| 896 | break; |
| 897 | case ID_ASSIGNED: |
| 898 | case ID_DENIED: |
| 899 | case ID_CHK_REQ: |
| 900 | case ID_REMOVE: |
| 901 | if (test_bit(MGR_OPT_NETWORK, &mgr->options)) |
| 902 | goto done; |
| 903 | break; |
| 904 | default: |
| 905 | goto done; |
| 906 | } |
| 907 | ret = 0; |
| 908 | if (mt == ID_REQUEST) { |
| 909 | new_tei_req(mgr, &skb->data[4]); |
| 910 | goto done; |
| 911 | } |
| 912 | read_lock_irqsave(&mgr->lock, flags); |
| 913 | list_for_each_entry(l2, &mgr->layer2, list) { |
| 914 | tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4); |
| 915 | } |
| 916 | read_unlock_irqrestore(&mgr->lock, flags); |
| 917 | done: |
| 918 | return ret; |
| 919 | } |
| 920 | |
| 921 | int |
| 922 | l2_tei(struct layer2 *l2, u_int cmd, u_long arg) |
| 923 | { |
| 924 | struct teimgr *tm = l2->tm; |
| 925 | |
| 926 | if (test_bit(FLG_FIXED_TEI, &l2->flag)) |
| 927 | return 0; |
| 928 | if (*debug & DEBUG_L2_TEI) |
| 929 | printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd); |
| 930 | switch (cmd) { |
| 931 | case MDL_ASSIGN_IND: |
| 932 | mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL); |
| 933 | break; |
| 934 | case MDL_ERROR_IND: |
| 935 | if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options)) |
| 936 | mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei); |
| 937 | if (test_bit(MGR_OPT_USER, &tm->mgr->options)) |
| 938 | mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL); |
| 939 | break; |
| 940 | case MDL_STATUS_UP_IND: |
| 941 | if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options)) |
| 942 | mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL); |
| 943 | break; |
| 944 | case MDL_STATUS_DOWN_IND: |
| 945 | if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options)) |
| 946 | mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL); |
| 947 | break; |
| 948 | case MDL_STATUS_UI_IND: |
| 949 | if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options)) |
| 950 | mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL); |
| 951 | break; |
| 952 | } |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | void |
Karsten Keil | c5b61d5 | 2008-07-27 18:32:50 +0200 | [diff] [blame] | 957 | TEIrelease(struct layer2 *l2) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 958 | { |
| 959 | struct teimgr *tm = l2->tm; |
| 960 | u_long flags; |
| 961 | |
| 962 | mISDN_FsmDelTimer(&tm->timer, 1); |
| 963 | write_lock_irqsave(&tm->mgr->lock, flags); |
| 964 | list_del(&l2->list); |
| 965 | write_unlock_irqrestore(&tm->mgr->lock, flags); |
| 966 | l2->tm = NULL; |
| 967 | kfree(tm); |
| 968 | } |
| 969 | |
| 970 | static int |
| 971 | create_teimgr(struct manager *mgr, struct channel_req *crq) |
| 972 | { |
| 973 | struct layer2 *l2; |
| 974 | u_long opt = 0; |
| 975 | u_long flags; |
| 976 | int id; |
| 977 | |
| 978 | if (*debug & DEBUG_L2_TEI) |
| 979 | printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n", |
Matthias Urlichs | 837468d | 2008-08-16 00:04:33 +0200 | [diff] [blame] | 980 | __func__, dev_name(&mgr->ch.st->dev->dev), |
| 981 | crq->protocol, crq->adr.dev, crq->adr.channel, |
| 982 | crq->adr.sapi, crq->adr.tei); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 983 | if (crq->adr.tei > GROUP_TEI) |
| 984 | return -EINVAL; |
| 985 | if (crq->adr.tei < 64) |
| 986 | test_and_set_bit(OPTION_L2_FIXEDTEI, &opt); |
| 987 | if (crq->adr.tei == 0) |
| 988 | test_and_set_bit(OPTION_L2_PTP, &opt); |
| 989 | if (test_bit(MGR_OPT_NETWORK, &mgr->options)) { |
| 990 | if (crq->protocol == ISDN_P_LAPD_TE) |
| 991 | return -EPROTONOSUPPORT; |
| 992 | if ((crq->adr.tei != 0) && (crq->adr.tei != 127)) |
| 993 | return -EINVAL; |
| 994 | if (mgr->up) { |
| 995 | printk(KERN_WARNING |
| 996 | "%s: only one network manager is allowed\n", |
| 997 | __func__); |
| 998 | return -EBUSY; |
| 999 | } |
| 1000 | } else if (test_bit(MGR_OPT_USER, &mgr->options)) { |
| 1001 | if (crq->protocol == ISDN_P_LAPD_NT) |
| 1002 | return -EPROTONOSUPPORT; |
| 1003 | if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI)) |
| 1004 | return -EINVAL; /* dyn tei */ |
| 1005 | } else { |
| 1006 | if (crq->protocol == ISDN_P_LAPD_NT) |
| 1007 | test_and_set_bit(MGR_OPT_NETWORK, &mgr->options); |
| 1008 | if (crq->protocol == ISDN_P_LAPD_TE) |
| 1009 | test_and_set_bit(MGR_OPT_USER, &mgr->options); |
| 1010 | } |
| 1011 | if (mgr->ch.st->dev->Dprotocols |
| 1012 | & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1))) |
| 1013 | test_and_set_bit(OPTION_L2_PMX, &opt); |
| 1014 | if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) { |
| 1015 | mgr->up = crq->ch; |
| 1016 | id = DL_INFO_L2_CONNECT; |
| 1017 | teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id); |
| 1018 | crq->ch = NULL; |
| 1019 | if (!list_empty(&mgr->layer2)) { |
| 1020 | read_lock_irqsave(&mgr->lock, flags); |
| 1021 | list_for_each_entry(l2, &mgr->layer2, list) { |
| 1022 | l2->up = mgr->up; |
| 1023 | l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL); |
| 1024 | } |
| 1025 | read_unlock_irqrestore(&mgr->lock, flags); |
| 1026 | } |
| 1027 | return 0; |
| 1028 | } |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 1029 | l2 = create_l2(crq->ch, crq->protocol, opt, |
| 1030 | crq->adr.tei, crq->adr.sapi); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1031 | if (!l2) |
| 1032 | return -ENOMEM; |
| 1033 | l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL); |
| 1034 | if (!l2->tm) { |
| 1035 | kfree(l2); |
| 1036 | printk(KERN_ERR "kmalloc teimgr failed\n"); |
| 1037 | return -ENOMEM; |
| 1038 | } |
| 1039 | l2->tm->mgr = mgr; |
| 1040 | l2->tm->l2 = l2; |
| 1041 | l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM; |
| 1042 | l2->tm->tei_m.userdata = l2->tm; |
| 1043 | l2->tm->tei_m.printdebug = tei_debug; |
| 1044 | if (crq->protocol == ISDN_P_LAPD_TE) { |
| 1045 | l2->tm->tei_m.fsm = &teifsmu; |
| 1046 | l2->tm->tei_m.state = ST_TEI_NOP; |
| 1047 | l2->tm->tval = 1000; /* T201 1 sec */ |
| 1048 | } else { |
| 1049 | l2->tm->tei_m.fsm = &teifsmn; |
| 1050 | l2->tm->tei_m.state = ST_TEI_NOP; |
| 1051 | l2->tm->tval = 2000; /* T202 2 sec */ |
| 1052 | } |
| 1053 | mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer); |
| 1054 | write_lock_irqsave(&mgr->lock, flags); |
| 1055 | id = get_free_id(mgr); |
| 1056 | list_add_tail(&l2->list, &mgr->layer2); |
| 1057 | write_unlock_irqrestore(&mgr->lock, flags); |
| 1058 | if (id < 0) { |
| 1059 | l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL); |
| 1060 | } else { |
| 1061 | l2->ch.nr = id; |
| 1062 | l2->up->nr = id; |
| 1063 | crq->ch = &l2->ch; |
| 1064 | id = 0; |
| 1065 | } |
| 1066 | return id; |
| 1067 | } |
| 1068 | |
| 1069 | static int |
| 1070 | mgr_send(struct mISDNchannel *ch, struct sk_buff *skb) |
| 1071 | { |
| 1072 | struct manager *mgr; |
| 1073 | struct mISDNhead *hh = mISDN_HEAD_P(skb); |
| 1074 | int ret = -EINVAL; |
| 1075 | |
| 1076 | mgr = container_of(ch, struct manager, ch); |
| 1077 | if (*debug & DEBUG_L2_RECV) |
| 1078 | printk(KERN_DEBUG "%s: prim(%x) id(%x)\n", |
| 1079 | __func__, hh->prim, hh->id); |
| 1080 | switch (hh->prim) { |
| 1081 | case PH_DATA_IND: |
| 1082 | mISDN_FsmEvent(&mgr->deact, EV_UI, NULL); |
| 1083 | ret = ph_data_ind(mgr, skb); |
| 1084 | break; |
| 1085 | case PH_DATA_CNF: |
| 1086 | do_ack(mgr, hh->id); |
| 1087 | ret = 0; |
| 1088 | break; |
| 1089 | case PH_ACTIVATE_IND: |
| 1090 | test_and_set_bit(MGR_PH_ACTIVE, &mgr->options); |
| 1091 | mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL); |
| 1092 | do_send(mgr); |
| 1093 | ret = 0; |
| 1094 | break; |
| 1095 | case PH_DEACTIVATE_IND: |
| 1096 | test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options); |
| 1097 | mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL); |
| 1098 | ret = 0; |
| 1099 | break; |
| 1100 | case DL_UNITDATA_REQ: |
| 1101 | return dl_unit_data(mgr, skb); |
| 1102 | } |
| 1103 | if (!ret) |
| 1104 | dev_kfree_skb(skb); |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
| 1108 | static int |
| 1109 | free_teimanager(struct manager *mgr) |
| 1110 | { |
| 1111 | struct layer2 *l2, *nl2; |
| 1112 | |
Andreas Eversberg | e73f6b2 | 2009-05-22 11:04:48 +0000 | [diff] [blame] | 1113 | test_and_clear_bit(OPTION_L1_HOLD, &mgr->options); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1114 | if (test_bit(MGR_OPT_NETWORK, &mgr->options)) { |
| 1115 | /* not locked lock is taken in release tei */ |
| 1116 | mgr->up = NULL; |
| 1117 | if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) { |
| 1118 | list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) { |
| 1119 | put_tei_msg(mgr, ID_REMOVE, 0, l2->tei); |
| 1120 | mutex_lock(&mgr->ch.st->lmutex); |
| 1121 | list_del(&l2->ch.list); |
| 1122 | mutex_unlock(&mgr->ch.st->lmutex); |
| 1123 | l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL); |
| 1124 | } |
| 1125 | test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options); |
| 1126 | } else { |
| 1127 | list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) { |
| 1128 | l2->up = NULL; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | if (test_bit(MGR_OPT_USER, &mgr->options)) { |
| 1133 | if (list_empty(&mgr->layer2)) |
| 1134 | test_and_clear_bit(MGR_OPT_USER, &mgr->options); |
| 1135 | } |
| 1136 | mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL); |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | static int |
| 1141 | ctrl_teimanager(struct manager *mgr, void *arg) |
| 1142 | { |
| 1143 | /* currently we only have one option */ |
Andreas Eversberg | e73f6b2 | 2009-05-22 11:04:48 +0000 | [diff] [blame] | 1144 | int *val = (int *)arg; |
| 1145 | int ret = 0; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1146 | |
Andreas Eversberg | e73f6b2 | 2009-05-22 11:04:48 +0000 | [diff] [blame] | 1147 | switch (val[0]) { |
| 1148 | case IMCLEAR_L2: |
| 1149 | if (val[1]) |
| 1150 | test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options); |
| 1151 | else |
| 1152 | test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options); |
| 1153 | break; |
| 1154 | case IMHOLD_L1: |
| 1155 | if (val[1]) |
| 1156 | test_and_set_bit(OPTION_L1_HOLD, &mgr->options); |
| 1157 | else |
| 1158 | test_and_clear_bit(OPTION_L1_HOLD, &mgr->options); |
| 1159 | break; |
| 1160 | default: |
| 1161 | ret = -EINVAL; |
| 1162 | } |
| 1163 | return ret; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | /* This function does create a L2 for fixed TEI in NT Mode */ |
| 1167 | static int |
| 1168 | check_data(struct manager *mgr, struct sk_buff *skb) |
| 1169 | { |
| 1170 | struct mISDNhead *hh = mISDN_HEAD_P(skb); |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 1171 | int ret, tei, sapi; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1172 | struct layer2 *l2; |
| 1173 | |
| 1174 | if (*debug & DEBUG_L2_CTRL) |
| 1175 | printk(KERN_DEBUG "%s: prim(%x) id(%x)\n", |
| 1176 | __func__, hh->prim, hh->id); |
| 1177 | if (test_bit(MGR_OPT_USER, &mgr->options)) |
| 1178 | return -ENOTCONN; |
| 1179 | if (hh->prim != PH_DATA_IND) |
| 1180 | return -ENOTCONN; |
| 1181 | if (skb->len != 3) |
| 1182 | return -ENOTCONN; |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 1183 | if (skb->data[0] & 3) /* EA0 and CR must be 0 */ |
| 1184 | return -EINVAL; |
| 1185 | sapi = skb->data[0] >> 2; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1186 | if (!(skb->data[1] & 1)) /* invalid EA1 */ |
| 1187 | return -EINVAL; |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 1188 | tei = skb->data[1] >> 1; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1189 | if (tei > 63) /* not a fixed tei */ |
| 1190 | return -ENOTCONN; |
| 1191 | if ((skb->data[2] & ~0x10) != SABME) |
| 1192 | return -ENOTCONN; |
| 1193 | /* We got a SABME for a fixed TEI */ |
Andreas Eversberg | d796509 | 2009-05-22 11:04:51 +0000 | [diff] [blame^] | 1194 | if (*debug & DEBUG_L2_CTRL) |
| 1195 | printk(KERN_DEBUG "%s: SABME sapi(%d) tei(%d)\n", |
| 1196 | __func__, sapi, tei); |
Andreas Eversberg | 5b277b8 | 2009-05-25 00:50:02 -0700 | [diff] [blame] | 1197 | l2 = create_new_tei(mgr, tei, sapi); |
Andreas Eversberg | d796509 | 2009-05-22 11:04:51 +0000 | [diff] [blame^] | 1198 | if (!l2) { |
| 1199 | if (*debug & DEBUG_L2_CTRL) |
| 1200 | printk(KERN_DEBUG "%s: failed to create new tei\n", |
| 1201 | __func__); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1202 | return -ENOMEM; |
Andreas Eversberg | d796509 | 2009-05-22 11:04:51 +0000 | [diff] [blame^] | 1203 | } |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1204 | ret = l2->ch.send(&l2->ch, skb); |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
| 1208 | void |
| 1209 | delete_teimanager(struct mISDNchannel *ch) |
| 1210 | { |
| 1211 | struct manager *mgr; |
| 1212 | struct layer2 *l2, *nl2; |
| 1213 | |
| 1214 | mgr = container_of(ch, struct manager, ch); |
| 1215 | /* not locked lock is taken in release tei */ |
| 1216 | list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) { |
| 1217 | mutex_lock(&mgr->ch.st->lmutex); |
| 1218 | list_del(&l2->ch.list); |
| 1219 | mutex_unlock(&mgr->ch.st->lmutex); |
| 1220 | l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL); |
| 1221 | } |
| 1222 | list_del(&mgr->ch.list); |
| 1223 | list_del(&mgr->bcast.list); |
| 1224 | skb_queue_purge(&mgr->sendq); |
| 1225 | kfree(mgr); |
| 1226 | } |
| 1227 | |
| 1228 | static int |
| 1229 | mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg) |
| 1230 | { |
| 1231 | struct manager *mgr; |
| 1232 | int ret = -EINVAL; |
| 1233 | |
| 1234 | mgr = container_of(ch, struct manager, ch); |
| 1235 | if (*debug & DEBUG_L2_CTRL) |
| 1236 | printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg); |
| 1237 | switch (cmd) { |
| 1238 | case OPEN_CHANNEL: |
| 1239 | ret = create_teimgr(mgr, arg); |
| 1240 | break; |
| 1241 | case CLOSE_CHANNEL: |
| 1242 | ret = free_teimanager(mgr); |
| 1243 | break; |
| 1244 | case CONTROL_CHANNEL: |
| 1245 | ret = ctrl_teimanager(mgr, arg); |
| 1246 | break; |
| 1247 | case CHECK_DATA: |
| 1248 | ret = check_data(mgr, arg); |
| 1249 | break; |
| 1250 | } |
| 1251 | return ret; |
| 1252 | } |
| 1253 | |
| 1254 | static int |
| 1255 | mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb) |
| 1256 | { |
| 1257 | struct manager *mgr = container_of(ch, struct manager, bcast); |
| 1258 | struct mISDNhead *hh = mISDN_HEAD_P(skb); |
| 1259 | struct sk_buff *cskb = NULL; |
| 1260 | struct layer2 *l2; |
| 1261 | u_long flags; |
| 1262 | int ret; |
| 1263 | |
| 1264 | read_lock_irqsave(&mgr->lock, flags); |
| 1265 | list_for_each_entry(l2, &mgr->layer2, list) { |
| 1266 | if ((hh->id & MISDN_ID_SAPI_MASK) == |
| 1267 | (l2->ch.addr & MISDN_ID_SAPI_MASK)) { |
| 1268 | if (list_is_last(&l2->list, &mgr->layer2)) { |
| 1269 | cskb = skb; |
| 1270 | skb = NULL; |
| 1271 | } else { |
| 1272 | if (!cskb) |
| 1273 | cskb = skb_copy(skb, GFP_KERNEL); |
| 1274 | } |
| 1275 | if (cskb) { |
| 1276 | ret = l2->ch.send(&l2->ch, cskb); |
| 1277 | if (ret) { |
| 1278 | if (*debug & DEBUG_SEND_ERR) |
| 1279 | printk(KERN_DEBUG |
| 1280 | "%s ch%d prim(%x) addr(%x)" |
| 1281 | " err %d\n", |
| 1282 | __func__, l2->ch.nr, |
| 1283 | hh->prim, l2->ch.addr, ret); |
| 1284 | } else |
| 1285 | cskb = NULL; |
| 1286 | } else { |
| 1287 | printk(KERN_WARNING "%s ch%d addr %x no mem\n", |
| 1288 | __func__, ch->nr, ch->addr); |
| 1289 | goto out; |
| 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | out: |
| 1294 | read_unlock_irqrestore(&mgr->lock, flags); |
| 1295 | if (cskb) |
| 1296 | dev_kfree_skb(cskb); |
| 1297 | if (skb) |
| 1298 | dev_kfree_skb(skb); |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | static int |
| 1303 | mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg) |
| 1304 | { |
| 1305 | |
| 1306 | return -EINVAL; |
| 1307 | } |
| 1308 | |
| 1309 | int |
| 1310 | create_teimanager(struct mISDNdevice *dev) |
| 1311 | { |
| 1312 | struct manager *mgr; |
| 1313 | |
| 1314 | mgr = kzalloc(sizeof(struct manager), GFP_KERNEL); |
| 1315 | if (!mgr) |
| 1316 | return -ENOMEM; |
| 1317 | INIT_LIST_HEAD(&mgr->layer2); |
Karsten Keil | 702c790 | 2008-12-12 21:21:57 -0800 | [diff] [blame] | 1318 | rwlock_init(&mgr->lock); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 1319 | skb_queue_head_init(&mgr->sendq); |
| 1320 | mgr->nextid = 1; |
| 1321 | mgr->lastid = MISDN_ID_NONE; |
| 1322 | mgr->ch.send = mgr_send; |
| 1323 | mgr->ch.ctrl = mgr_ctrl; |
| 1324 | mgr->ch.st = dev->D.st; |
| 1325 | set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI); |
| 1326 | add_layer2(&mgr->ch, dev->D.st); |
| 1327 | mgr->bcast.send = mgr_bcast; |
| 1328 | mgr->bcast.ctrl = mgr_bcast_ctrl; |
| 1329 | mgr->bcast.st = dev->D.st; |
| 1330 | set_channel_address(&mgr->bcast, 0, GROUP_TEI); |
| 1331 | add_layer2(&mgr->bcast, dev->D.st); |
| 1332 | mgr->deact.debug = *debug & DEBUG_MANAGER; |
| 1333 | mgr->deact.userdata = mgr; |
| 1334 | mgr->deact.printdebug = da_debug; |
| 1335 | mgr->deact.fsm = &deactfsm; |
| 1336 | mgr->deact.state = ST_L1_DEACT; |
| 1337 | mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer); |
| 1338 | dev->teimgr = &mgr->ch; |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | int TEIInit(u_int *deb) |
| 1343 | { |
| 1344 | debug = deb; |
| 1345 | teifsmu.state_count = TEI_STATE_COUNT; |
| 1346 | teifsmu.event_count = TEI_EVENT_COUNT; |
| 1347 | teifsmu.strEvent = strTeiEvent; |
| 1348 | teifsmu.strState = strTeiState; |
| 1349 | mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser)); |
| 1350 | teifsmn.state_count = TEI_STATE_COUNT; |
| 1351 | teifsmn.event_count = TEI_EVENT_COUNT; |
| 1352 | teifsmn.strEvent = strTeiEvent; |
| 1353 | teifsmn.strState = strTeiState; |
| 1354 | mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet)); |
| 1355 | deactfsm.state_count = DEACT_STATE_COUNT; |
| 1356 | deactfsm.event_count = DEACT_EVENT_COUNT; |
| 1357 | deactfsm.strEvent = strDeactEvent; |
| 1358 | deactfsm.strState = strDeactState; |
| 1359 | mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList)); |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | void TEIFree(void) |
| 1364 | { |
| 1365 | mISDN_FsmFree(&teifsmu); |
| 1366 | mISDN_FsmFree(&teifsmn); |
| 1367 | mISDN_FsmFree(&deactfsm); |
| 1368 | } |