tei.c 31 KB

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