tei.c 32 KB

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