tei.c 34 KB

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