layer2.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  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 <linux/mISDNif.h>
  18. #include "core.h"
  19. #include "fsm.h"
  20. #include "layer2.h"
  21. static u_int *debug;
  22. static
  23. struct Fsm l2fsm = {NULL, 0, 0, NULL, NULL};
  24. static char *strL2State[] =
  25. {
  26. "ST_L2_1",
  27. "ST_L2_2",
  28. "ST_L2_3",
  29. "ST_L2_4",
  30. "ST_L2_5",
  31. "ST_L2_6",
  32. "ST_L2_7",
  33. "ST_L2_8",
  34. };
  35. enum {
  36. EV_L2_UI,
  37. EV_L2_SABME,
  38. EV_L2_DISC,
  39. EV_L2_DM,
  40. EV_L2_UA,
  41. EV_L2_FRMR,
  42. EV_L2_SUPER,
  43. EV_L2_I,
  44. EV_L2_DL_DATA,
  45. EV_L2_ACK_PULL,
  46. EV_L2_DL_UNITDATA,
  47. EV_L2_DL_ESTABLISH_REQ,
  48. EV_L2_DL_RELEASE_REQ,
  49. EV_L2_MDL_ASSIGN,
  50. EV_L2_MDL_REMOVE,
  51. EV_L2_MDL_ERROR,
  52. EV_L1_DEACTIVATE,
  53. EV_L2_T200,
  54. EV_L2_T203,
  55. EV_L2_SET_OWN_BUSY,
  56. EV_L2_CLEAR_OWN_BUSY,
  57. EV_L2_FRAME_ERROR,
  58. };
  59. #define L2_EVENT_COUNT (EV_L2_FRAME_ERROR+1)
  60. static char *strL2Event[] =
  61. {
  62. "EV_L2_UI",
  63. "EV_L2_SABME",
  64. "EV_L2_DISC",
  65. "EV_L2_DM",
  66. "EV_L2_UA",
  67. "EV_L2_FRMR",
  68. "EV_L2_SUPER",
  69. "EV_L2_I",
  70. "EV_L2_DL_DATA",
  71. "EV_L2_ACK_PULL",
  72. "EV_L2_DL_UNITDATA",
  73. "EV_L2_DL_ESTABLISH_REQ",
  74. "EV_L2_DL_RELEASE_REQ",
  75. "EV_L2_MDL_ASSIGN",
  76. "EV_L2_MDL_REMOVE",
  77. "EV_L2_MDL_ERROR",
  78. "EV_L1_DEACTIVATE",
  79. "EV_L2_T200",
  80. "EV_L2_T203",
  81. "EV_L2_SET_OWN_BUSY",
  82. "EV_L2_CLEAR_OWN_BUSY",
  83. "EV_L2_FRAME_ERROR",
  84. };
  85. static void
  86. l2m_debug(struct FsmInst *fi, char *fmt, ...)
  87. {
  88. struct layer2 *l2 = fi->userdata;
  89. va_list va;
  90. if (!(*debug & DEBUG_L2_FSM))
  91. return;
  92. va_start(va, fmt);
  93. printk(KERN_DEBUG "l2 (tei %d): ", l2->tei);
  94. vprintk(fmt, va);
  95. printk("\n");
  96. va_end(va);
  97. }
  98. inline u_int
  99. l2headersize(struct layer2 *l2, int ui)
  100. {
  101. return ((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
  102. (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
  103. }
  104. inline u_int
  105. l2addrsize(struct layer2 *l2)
  106. {
  107. return test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
  108. }
  109. static u_int
  110. l2_newid(struct layer2 *l2)
  111. {
  112. u_int id;
  113. id = l2->next_id++;
  114. if (id == 0x7fff)
  115. l2->next_id = 1;
  116. id <<= 16;
  117. id |= l2->tei << 8;
  118. id |= l2->sapi;
  119. return id;
  120. }
  121. static void
  122. l2up(struct layer2 *l2, u_int prim, struct sk_buff *skb)
  123. {
  124. int err;
  125. if (!l2->up)
  126. return;
  127. mISDN_HEAD_PRIM(skb) = prim;
  128. mISDN_HEAD_ID(skb) = (l2->ch.nr << 16) | l2->ch.addr;
  129. err = l2->up->send(l2->up, skb);
  130. if (err) {
  131. printk(KERN_WARNING "%s: err=%d\n", __func__, err);
  132. dev_kfree_skb(skb);
  133. }
  134. }
  135. static void
  136. l2up_create(struct layer2 *l2, u_int prim, int len, void *arg)
  137. {
  138. struct sk_buff *skb;
  139. struct mISDNhead *hh;
  140. int err;
  141. if (!l2->up)
  142. return;
  143. skb = mI_alloc_skb(len, GFP_ATOMIC);
  144. if (!skb)
  145. return;
  146. hh = mISDN_HEAD_P(skb);
  147. hh->prim = prim;
  148. hh->id = (l2->ch.nr << 16) | l2->ch.addr;
  149. if (len)
  150. memcpy(skb_put(skb, len), arg, len);
  151. err = l2->up->send(l2->up, skb);
  152. if (err) {
  153. printk(KERN_WARNING "%s: err=%d\n", __func__, err);
  154. dev_kfree_skb(skb);
  155. }
  156. }
  157. static int
  158. l2down_skb(struct layer2 *l2, struct sk_buff *skb) {
  159. int ret;
  160. ret = l2->ch.recv(l2->ch.peer, skb);
  161. if (ret && (*debug & DEBUG_L2_RECV))
  162. printk(KERN_DEBUG "l2down_skb: ret(%d)\n", ret);
  163. return ret;
  164. }
  165. static int
  166. l2down_raw(struct layer2 *l2, struct sk_buff *skb)
  167. {
  168. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  169. if (hh->prim == PH_DATA_REQ) {
  170. if (test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
  171. skb_queue_tail(&l2->down_queue, skb);
  172. return 0;
  173. }
  174. l2->down_id = mISDN_HEAD_ID(skb);
  175. }
  176. return l2down_skb(l2, skb);
  177. }
  178. static int
  179. l2down(struct layer2 *l2, u_int prim, u_int id, struct sk_buff *skb)
  180. {
  181. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  182. hh->prim = prim;
  183. hh->id = id;
  184. return l2down_raw(l2, skb);
  185. }
  186. static int
  187. l2down_create(struct layer2 *l2, u_int prim, u_int id, int len, void *arg)
  188. {
  189. struct sk_buff *skb;
  190. int err;
  191. struct mISDNhead *hh;
  192. skb = mI_alloc_skb(len, GFP_ATOMIC);
  193. if (!skb)
  194. return -ENOMEM;
  195. hh = mISDN_HEAD_P(skb);
  196. hh->prim = prim;
  197. hh->id = id;
  198. if (len)
  199. memcpy(skb_put(skb, len), arg, len);
  200. err = l2down_raw(l2, skb);
  201. if (err)
  202. dev_kfree_skb(skb);
  203. return err;
  204. }
  205. static int
  206. ph_data_confirm(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb) {
  207. struct sk_buff *nskb = skb;
  208. int ret = -EAGAIN;
  209. if (test_bit(FLG_L1_NOTREADY, &l2->flag)) {
  210. if (hh->id == l2->down_id) {
  211. nskb = skb_dequeue(&l2->down_queue);
  212. if (nskb) {
  213. l2->down_id = mISDN_HEAD_ID(nskb);
  214. if (l2down_skb(l2, nskb)) {
  215. dev_kfree_skb(nskb);
  216. l2->down_id = MISDN_ID_NONE;
  217. }
  218. } else
  219. l2->down_id = MISDN_ID_NONE;
  220. if (ret) {
  221. dev_kfree_skb(skb);
  222. ret = 0;
  223. }
  224. if (l2->down_id == MISDN_ID_NONE) {
  225. test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
  226. mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
  227. }
  228. }
  229. }
  230. if (!test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
  231. nskb = skb_dequeue(&l2->down_queue);
  232. if (nskb) {
  233. l2->down_id = mISDN_HEAD_ID(nskb);
  234. if (l2down_skb(l2, nskb)) {
  235. dev_kfree_skb(nskb);
  236. l2->down_id = MISDN_ID_NONE;
  237. test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
  238. }
  239. } else
  240. test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
  241. }
  242. return ret;
  243. }
  244. static int
  245. l2mgr(struct layer2 *l2, u_int prim, void *arg) {
  246. long c = (long)arg;
  247. printk(KERN_WARNING
  248. "l2mgr: addr:%x prim %x %c\n", l2->id, prim, (char)c);
  249. if (test_bit(FLG_LAPD, &l2->flag) &&
  250. !test_bit(FLG_FIXED_TEI, &l2->flag)) {
  251. switch (c) {
  252. case 'C':
  253. case 'D':
  254. case 'G':
  255. case 'H':
  256. l2_tei(l2, prim, (u_long)arg);
  257. break;
  258. }
  259. }
  260. return 0;
  261. }
  262. static void
  263. set_peer_busy(struct layer2 *l2) {
  264. test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
  265. if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
  266. test_and_set_bit(FLG_L2BLOCK, &l2->flag);
  267. }
  268. static void
  269. clear_peer_busy(struct layer2 *l2) {
  270. if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
  271. test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
  272. }
  273. static void
  274. InitWin(struct layer2 *l2)
  275. {
  276. int i;
  277. for (i = 0; i < MAX_WINDOW; i++)
  278. l2->windowar[i] = NULL;
  279. }
  280. static int
  281. freewin(struct layer2 *l2)
  282. {
  283. int i, cnt = 0;
  284. for (i = 0; i < MAX_WINDOW; i++) {
  285. if (l2->windowar[i]) {
  286. cnt++;
  287. dev_kfree_skb(l2->windowar[i]);
  288. l2->windowar[i] = NULL;
  289. }
  290. }
  291. return cnt;
  292. }
  293. static void
  294. ReleaseWin(struct layer2 *l2)
  295. {
  296. int cnt = freewin(l2);
  297. if (cnt)
  298. printk(KERN_WARNING
  299. "isdnl2 freed %d skbuffs in release\n", cnt);
  300. }
  301. inline unsigned int
  302. cansend(struct layer2 *l2)
  303. {
  304. unsigned int p1;
  305. if (test_bit(FLG_MOD128, &l2->flag))
  306. p1 = (l2->vs - l2->va) % 128;
  307. else
  308. p1 = (l2->vs - l2->va) % 8;
  309. return (p1 < l2->window) && !test_bit(FLG_PEER_BUSY, &l2->flag);
  310. }
  311. inline void
  312. clear_exception(struct layer2 *l2)
  313. {
  314. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  315. test_and_clear_bit(FLG_REJEXC, &l2->flag);
  316. test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
  317. clear_peer_busy(l2);
  318. }
  319. static int
  320. sethdraddr(struct layer2 *l2, u_char *header, int rsp)
  321. {
  322. u_char *ptr = header;
  323. int crbit = rsp;
  324. if (test_bit(FLG_LAPD, &l2->flag)) {
  325. if (test_bit(FLG_LAPD_NET, &l2->flag))
  326. crbit = !crbit;
  327. *ptr++ = (l2->sapi << 2) | (crbit ? 2 : 0);
  328. *ptr++ = (l2->tei << 1) | 1;
  329. return 2;
  330. } else {
  331. if (test_bit(FLG_ORIG, &l2->flag))
  332. crbit = !crbit;
  333. if (crbit)
  334. *ptr++ = l2->addr.B;
  335. else
  336. *ptr++ = l2->addr.A;
  337. return 1;
  338. }
  339. }
  340. static inline void
  341. enqueue_super(struct layer2 *l2, struct sk_buff *skb)
  342. {
  343. if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
  344. dev_kfree_skb(skb);
  345. }
  346. static inline void
  347. enqueue_ui(struct layer2 *l2, struct sk_buff *skb)
  348. {
  349. if (l2->tm)
  350. l2_tei(l2, MDL_STATUS_UI_IND, 0);
  351. if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
  352. dev_kfree_skb(skb);
  353. }
  354. inline int
  355. IsUI(u_char *data)
  356. {
  357. return (data[0] & 0xef) == UI;
  358. }
  359. inline int
  360. IsUA(u_char *data)
  361. {
  362. return (data[0] & 0xef) == UA;
  363. }
  364. inline int
  365. IsDM(u_char *data)
  366. {
  367. return (data[0] & 0xef) == DM;
  368. }
  369. inline int
  370. IsDISC(u_char *data)
  371. {
  372. return (data[0] & 0xef) == DISC;
  373. }
  374. inline int
  375. IsRR(u_char *data, struct layer2 *l2)
  376. {
  377. if (test_bit(FLG_MOD128, &l2->flag))
  378. return data[0] == RR;
  379. else
  380. return (data[0] & 0xf) == 1;
  381. }
  382. inline int
  383. IsSFrame(u_char *data, struct layer2 *l2)
  384. {
  385. register u_char d = *data;
  386. if (!test_bit(FLG_MOD128, &l2->flag))
  387. d &= 0xf;
  388. return ((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c);
  389. }
  390. inline int
  391. IsSABME(u_char *data, struct layer2 *l2)
  392. {
  393. u_char d = data[0] & ~0x10;
  394. return test_bit(FLG_MOD128, &l2->flag) ? d == SABME : d == SABM;
  395. }
  396. inline int
  397. IsREJ(u_char *data, struct layer2 *l2)
  398. {
  399. return test_bit(FLG_MOD128, &l2->flag) ?
  400. data[0] == REJ : (data[0] & 0xf) == REJ;
  401. }
  402. inline int
  403. IsFRMR(u_char *data)
  404. {
  405. return (data[0] & 0xef) == FRMR;
  406. }
  407. inline int
  408. IsRNR(u_char *data, struct layer2 *l2)
  409. {
  410. return test_bit(FLG_MOD128, &l2->flag) ?
  411. data[0] == RNR : (data[0] & 0xf) == RNR;
  412. }
  413. static int
  414. iframe_error(struct layer2 *l2, struct sk_buff *skb)
  415. {
  416. u_int i;
  417. int rsp = *skb->data & 0x2;
  418. i = l2addrsize(l2) + (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1);
  419. if (test_bit(FLG_ORIG, &l2->flag))
  420. rsp = !rsp;
  421. if (rsp)
  422. return 'L';
  423. if (skb->len < i)
  424. return 'N';
  425. if ((skb->len - i) > l2->maxlen)
  426. return 'O';
  427. return 0;
  428. }
  429. static int
  430. super_error(struct layer2 *l2, struct sk_buff *skb)
  431. {
  432. if (skb->len != l2addrsize(l2) +
  433. (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1))
  434. return 'N';
  435. return 0;
  436. }
  437. static int
  438. unnum_error(struct layer2 *l2, struct sk_buff *skb, int wantrsp)
  439. {
  440. int rsp = (*skb->data & 0x2) >> 1;
  441. if (test_bit(FLG_ORIG, &l2->flag))
  442. rsp = !rsp;
  443. if (rsp != wantrsp)
  444. return 'L';
  445. if (skb->len != l2addrsize(l2) + 1)
  446. return 'N';
  447. return 0;
  448. }
  449. static int
  450. UI_error(struct layer2 *l2, struct sk_buff *skb)
  451. {
  452. int rsp = *skb->data & 0x2;
  453. if (test_bit(FLG_ORIG, &l2->flag))
  454. rsp = !rsp;
  455. if (rsp)
  456. return 'L';
  457. if (skb->len > l2->maxlen + l2addrsize(l2) + 1)
  458. return 'O';
  459. return 0;
  460. }
  461. static int
  462. FRMR_error(struct layer2 *l2, struct sk_buff *skb)
  463. {
  464. u_int headers = l2addrsize(l2) + 1;
  465. u_char *datap = skb->data + headers;
  466. int rsp = *skb->data & 0x2;
  467. if (test_bit(FLG_ORIG, &l2->flag))
  468. rsp = !rsp;
  469. if (!rsp)
  470. return 'L';
  471. if (test_bit(FLG_MOD128, &l2->flag)) {
  472. if (skb->len < headers + 5)
  473. return 'N';
  474. else if (*debug & DEBUG_L2)
  475. l2m_debug(&l2->l2m,
  476. "FRMR information %2x %2x %2x %2x %2x",
  477. datap[0], datap[1], datap[2], datap[3], datap[4]);
  478. } else {
  479. if (skb->len < headers + 3)
  480. return 'N';
  481. else if (*debug & DEBUG_L2)
  482. l2m_debug(&l2->l2m,
  483. "FRMR information %2x %2x %2x",
  484. datap[0], datap[1], datap[2]);
  485. }
  486. return 0;
  487. }
  488. static unsigned int
  489. legalnr(struct layer2 *l2, unsigned int nr)
  490. {
  491. if (test_bit(FLG_MOD128, &l2->flag))
  492. return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
  493. else
  494. return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
  495. }
  496. static void
  497. setva(struct layer2 *l2, unsigned int nr)
  498. {
  499. struct sk_buff *skb;
  500. while (l2->va != nr) {
  501. l2->va++;
  502. if (test_bit(FLG_MOD128, &l2->flag))
  503. l2->va %= 128;
  504. else
  505. l2->va %= 8;
  506. if (l2->windowar[l2->sow]) {
  507. skb_trim(l2->windowar[l2->sow], 0);
  508. skb_queue_tail(&l2->tmp_queue, l2->windowar[l2->sow]);
  509. l2->windowar[l2->sow] = NULL;
  510. }
  511. l2->sow = (l2->sow + 1) % l2->window;
  512. }
  513. skb = skb_dequeue(&l2->tmp_queue);
  514. while (skb) {
  515. dev_kfree_skb(skb);
  516. skb = skb_dequeue(&l2->tmp_queue);
  517. }
  518. }
  519. static void
  520. send_uframe(struct layer2 *l2, struct sk_buff *skb, u_char cmd, u_char cr)
  521. {
  522. u_char tmp[MAX_L2HEADER_LEN];
  523. int i;
  524. i = sethdraddr(l2, tmp, cr);
  525. tmp[i++] = cmd;
  526. if (skb)
  527. skb_trim(skb, 0);
  528. else {
  529. skb = mI_alloc_skb(i, GFP_ATOMIC);
  530. if (!skb) {
  531. printk(KERN_WARNING "%s: can't alloc skbuff\n",
  532. __func__);
  533. return;
  534. }
  535. }
  536. memcpy(skb_put(skb, i), tmp, i);
  537. enqueue_super(l2, skb);
  538. }
  539. inline u_char
  540. get_PollFlag(struct layer2 *l2, struct sk_buff *skb)
  541. {
  542. return skb->data[l2addrsize(l2)] & 0x10;
  543. }
  544. inline u_char
  545. get_PollFlagFree(struct layer2 *l2, struct sk_buff *skb)
  546. {
  547. u_char PF;
  548. PF = get_PollFlag(l2, skb);
  549. dev_kfree_skb(skb);
  550. return PF;
  551. }
  552. inline void
  553. start_t200(struct layer2 *l2, int i)
  554. {
  555. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
  556. test_and_set_bit(FLG_T200_RUN, &l2->flag);
  557. }
  558. inline void
  559. restart_t200(struct layer2 *l2, int i)
  560. {
  561. mISDN_FsmRestartTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
  562. test_and_set_bit(FLG_T200_RUN, &l2->flag);
  563. }
  564. inline void
  565. stop_t200(struct layer2 *l2, int i)
  566. {
  567. if (test_and_clear_bit(FLG_T200_RUN, &l2->flag))
  568. mISDN_FsmDelTimer(&l2->t200, i);
  569. }
  570. inline void
  571. st5_dl_release_l2l3(struct layer2 *l2)
  572. {
  573. int pr;
  574. if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
  575. pr = DL_RELEASE_CNF;
  576. else
  577. pr = DL_RELEASE_IND;
  578. l2up_create(l2, pr, 0, NULL);
  579. }
  580. inline void
  581. lapb_dl_release_l2l3(struct layer2 *l2, int f)
  582. {
  583. if (test_bit(FLG_LAPB, &l2->flag))
  584. l2down_create(l2, PH_DEACTIVATE_REQ, l2_newid(l2), 0, NULL);
  585. l2up_create(l2, f, 0, NULL);
  586. }
  587. static void
  588. establishlink(struct FsmInst *fi)
  589. {
  590. struct layer2 *l2 = fi->userdata;
  591. u_char cmd;
  592. clear_exception(l2);
  593. l2->rc = 0;
  594. cmd = (test_bit(FLG_MOD128, &l2->flag) ? SABME : SABM) | 0x10;
  595. send_uframe(l2, NULL, cmd, CMD);
  596. mISDN_FsmDelTimer(&l2->t203, 1);
  597. restart_t200(l2, 1);
  598. test_and_clear_bit(FLG_PEND_REL, &l2->flag);
  599. freewin(l2);
  600. mISDN_FsmChangeState(fi, ST_L2_5);
  601. }
  602. static void
  603. l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
  604. {
  605. struct sk_buff *skb = arg;
  606. struct layer2 *l2 = fi->userdata;
  607. if (get_PollFlagFree(l2, skb))
  608. l2mgr(l2, MDL_ERROR_IND, (void *) 'C');
  609. else
  610. l2mgr(l2, MDL_ERROR_IND, (void *) 'D');
  611. }
  612. static void
  613. l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
  614. {
  615. struct sk_buff *skb = arg;
  616. struct layer2 *l2 = fi->userdata;
  617. if (get_PollFlagFree(l2, skb))
  618. l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
  619. else {
  620. l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
  621. establishlink(fi);
  622. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  623. }
  624. }
  625. static void
  626. l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
  627. {
  628. struct sk_buff *skb = arg;
  629. struct layer2 *l2 = fi->userdata;
  630. if (get_PollFlagFree(l2, skb))
  631. l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
  632. else
  633. l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
  634. establishlink(fi);
  635. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  636. }
  637. static void
  638. l2_go_st3(struct FsmInst *fi, int event, void *arg)
  639. {
  640. dev_kfree_skb((struct sk_buff *)arg);
  641. mISDN_FsmChangeState(fi, ST_L2_3);
  642. }
  643. static void
  644. l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
  645. {
  646. struct layer2 *l2 = fi->userdata;
  647. mISDN_FsmChangeState(fi, ST_L2_3);
  648. dev_kfree_skb((struct sk_buff *)arg);
  649. l2_tei(l2, MDL_ASSIGN_IND, 0);
  650. }
  651. static void
  652. l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
  653. {
  654. struct layer2 *l2 = fi->userdata;
  655. struct sk_buff *skb = arg;
  656. skb_queue_tail(&l2->ui_queue, skb);
  657. mISDN_FsmChangeState(fi, ST_L2_2);
  658. l2_tei(l2, MDL_ASSIGN_IND, 0);
  659. }
  660. static void
  661. l2_queue_ui(struct FsmInst *fi, int event, void *arg)
  662. {
  663. struct layer2 *l2 = fi->userdata;
  664. struct sk_buff *skb = arg;
  665. skb_queue_tail(&l2->ui_queue, skb);
  666. }
  667. static void
  668. tx_ui(struct layer2 *l2)
  669. {
  670. struct sk_buff *skb;
  671. u_char header[MAX_L2HEADER_LEN];
  672. int i;
  673. i = sethdraddr(l2, header, CMD);
  674. if (test_bit(FLG_LAPD_NET, &l2->flag))
  675. header[1] = 0xff; /* tei 127 */
  676. header[i++] = UI;
  677. while ((skb = skb_dequeue(&l2->ui_queue))) {
  678. memcpy(skb_push(skb, i), header, i);
  679. enqueue_ui(l2, skb);
  680. }
  681. }
  682. static void
  683. l2_send_ui(struct FsmInst *fi, int event, void *arg)
  684. {
  685. struct layer2 *l2 = fi->userdata;
  686. struct sk_buff *skb = arg;
  687. skb_queue_tail(&l2->ui_queue, skb);
  688. tx_ui(l2);
  689. }
  690. static void
  691. l2_got_ui(struct FsmInst *fi, int event, void *arg)
  692. {
  693. struct layer2 *l2 = fi->userdata;
  694. struct sk_buff *skb = arg;
  695. skb_pull(skb, l2headersize(l2, 1));
  696. /*
  697. * in states 1-3 for broadcast
  698. */
  699. if (l2->tm)
  700. l2_tei(l2, MDL_STATUS_UI_IND, 0);
  701. l2up(l2, DL_UNITDATA_IND, skb);
  702. }
  703. static void
  704. l2_establish(struct FsmInst *fi, int event, void *arg)
  705. {
  706. struct sk_buff *skb = arg;
  707. struct layer2 *l2 = fi->userdata;
  708. establishlink(fi);
  709. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  710. dev_kfree_skb(skb);
  711. }
  712. static void
  713. l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
  714. {
  715. struct sk_buff *skb = arg;
  716. struct layer2 *l2 = fi->userdata;
  717. skb_queue_purge(&l2->i_queue);
  718. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  719. test_and_clear_bit(FLG_PEND_REL, &l2->flag);
  720. dev_kfree_skb(skb);
  721. }
  722. static void
  723. l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
  724. {
  725. struct sk_buff *skb = arg;
  726. struct layer2 *l2 = fi->userdata;
  727. skb_queue_purge(&l2->i_queue);
  728. establishlink(fi);
  729. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  730. dev_kfree_skb(skb);
  731. }
  732. static void
  733. l2_release(struct FsmInst *fi, int event, void *arg)
  734. {
  735. struct layer2 *l2 = fi->userdata;
  736. struct sk_buff *skb = arg;
  737. skb_trim(skb, 0);
  738. l2up(l2, DL_RELEASE_CNF, skb);
  739. }
  740. static void
  741. l2_pend_rel(struct FsmInst *fi, int event, void *arg)
  742. {
  743. struct sk_buff *skb = arg;
  744. struct layer2 *l2 = fi->userdata;
  745. test_and_set_bit(FLG_PEND_REL, &l2->flag);
  746. dev_kfree_skb(skb);
  747. }
  748. static void
  749. l2_disconnect(struct FsmInst *fi, int event, void *arg)
  750. {
  751. struct layer2 *l2 = fi->userdata;
  752. struct sk_buff *skb = arg;
  753. skb_queue_purge(&l2->i_queue);
  754. freewin(l2);
  755. mISDN_FsmChangeState(fi, ST_L2_6);
  756. l2->rc = 0;
  757. send_uframe(l2, NULL, DISC | 0x10, CMD);
  758. mISDN_FsmDelTimer(&l2->t203, 1);
  759. restart_t200(l2, 2);
  760. if (skb)
  761. dev_kfree_skb(skb);
  762. }
  763. static void
  764. l2_start_multi(struct FsmInst *fi, int event, void *arg)
  765. {
  766. struct layer2 *l2 = fi->userdata;
  767. struct sk_buff *skb = arg;
  768. l2->vs = 0;
  769. l2->va = 0;
  770. l2->vr = 0;
  771. l2->sow = 0;
  772. clear_exception(l2);
  773. send_uframe(l2, NULL, UA | get_PollFlag(l2, skb), RSP);
  774. mISDN_FsmChangeState(fi, ST_L2_7);
  775. mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
  776. skb_trim(skb, 0);
  777. l2up(l2, DL_ESTABLISH_IND, skb);
  778. if (l2->tm)
  779. l2_tei(l2, MDL_STATUS_UP_IND, 0);
  780. }
  781. static void
  782. l2_send_UA(struct FsmInst *fi, int event, void *arg)
  783. {
  784. struct layer2 *l2 = fi->userdata;
  785. struct sk_buff *skb = arg;
  786. send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
  787. }
  788. static void
  789. l2_send_DM(struct FsmInst *fi, int event, void *arg)
  790. {
  791. struct layer2 *l2 = fi->userdata;
  792. struct sk_buff *skb = arg;
  793. send_uframe(l2, skb, DM | get_PollFlag(l2, skb), RSP);
  794. }
  795. static void
  796. l2_restart_multi(struct FsmInst *fi, int event, void *arg)
  797. {
  798. struct layer2 *l2 = fi->userdata;
  799. struct sk_buff *skb = arg;
  800. int est = 0;
  801. send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
  802. l2mgr(l2, MDL_ERROR_IND, (void *) 'F');
  803. if (l2->vs != l2->va) {
  804. skb_queue_purge(&l2->i_queue);
  805. est = 1;
  806. }
  807. clear_exception(l2);
  808. l2->vs = 0;
  809. l2->va = 0;
  810. l2->vr = 0;
  811. l2->sow = 0;
  812. mISDN_FsmChangeState(fi, ST_L2_7);
  813. stop_t200(l2, 3);
  814. mISDN_FsmRestartTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
  815. if (est)
  816. l2up_create(l2, DL_ESTABLISH_IND, 0, NULL);
  817. /* mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
  818. * MGR_SHORTSTATUS | INDICATION, SSTATUS_L2_ESTABLISHED,
  819. * 0, NULL, 0);
  820. */
  821. if (skb_queue_len(&l2->i_queue) && cansend(l2))
  822. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  823. }
  824. static void
  825. l2_stop_multi(struct FsmInst *fi, int event, void *arg)
  826. {
  827. struct layer2 *l2 = fi->userdata;
  828. struct sk_buff *skb = arg;
  829. mISDN_FsmChangeState(fi, ST_L2_4);
  830. mISDN_FsmDelTimer(&l2->t203, 3);
  831. stop_t200(l2, 4);
  832. send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
  833. skb_queue_purge(&l2->i_queue);
  834. freewin(l2);
  835. lapb_dl_release_l2l3(l2, DL_RELEASE_IND);
  836. if (l2->tm)
  837. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  838. }
  839. static void
  840. l2_connected(struct FsmInst *fi, int event, void *arg)
  841. {
  842. struct layer2 *l2 = fi->userdata;
  843. struct sk_buff *skb = arg;
  844. int pr = -1;
  845. if (!get_PollFlag(l2, skb)) {
  846. l2_mdl_error_ua(fi, event, arg);
  847. return;
  848. }
  849. dev_kfree_skb(skb);
  850. if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
  851. l2_disconnect(fi, event, NULL);
  852. if (test_and_clear_bit(FLG_L3_INIT, &l2->flag)) {
  853. pr = DL_ESTABLISH_CNF;
  854. } else if (l2->vs != l2->va) {
  855. skb_queue_purge(&l2->i_queue);
  856. pr = DL_ESTABLISH_IND;
  857. }
  858. stop_t200(l2, 5);
  859. l2->vr = 0;
  860. l2->vs = 0;
  861. l2->va = 0;
  862. l2->sow = 0;
  863. mISDN_FsmChangeState(fi, ST_L2_7);
  864. mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 4);
  865. if (pr != -1)
  866. l2up_create(l2, pr, 0, NULL);
  867. if (skb_queue_len(&l2->i_queue) && cansend(l2))
  868. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  869. if (l2->tm)
  870. l2_tei(l2, MDL_STATUS_UP_IND, 0);
  871. }
  872. static void
  873. l2_released(struct FsmInst *fi, int event, void *arg)
  874. {
  875. struct layer2 *l2 = fi->userdata;
  876. struct sk_buff *skb = arg;
  877. if (!get_PollFlag(l2, skb)) {
  878. l2_mdl_error_ua(fi, event, arg);
  879. return;
  880. }
  881. dev_kfree_skb(skb);
  882. stop_t200(l2, 6);
  883. lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
  884. mISDN_FsmChangeState(fi, ST_L2_4);
  885. if (l2->tm)
  886. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  887. }
  888. static void
  889. l2_reestablish(struct FsmInst *fi, int event, void *arg)
  890. {
  891. struct layer2 *l2 = fi->userdata;
  892. struct sk_buff *skb = arg;
  893. if (!get_PollFlagFree(l2, skb)) {
  894. establishlink(fi);
  895. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  896. }
  897. }
  898. static void
  899. l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
  900. {
  901. struct layer2 *l2 = fi->userdata;
  902. struct sk_buff *skb = arg;
  903. if (get_PollFlagFree(l2, skb)) {
  904. stop_t200(l2, 7);
  905. if (!test_bit(FLG_L3_INIT, &l2->flag))
  906. skb_queue_purge(&l2->i_queue);
  907. if (test_bit(FLG_LAPB, &l2->flag))
  908. l2down_create(l2, PH_DEACTIVATE_REQ,
  909. l2_newid(l2), 0, NULL);
  910. st5_dl_release_l2l3(l2);
  911. mISDN_FsmChangeState(fi, ST_L2_4);
  912. if (l2->tm)
  913. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  914. }
  915. }
  916. static void
  917. l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
  918. {
  919. struct layer2 *l2 = fi->userdata;
  920. struct sk_buff *skb = arg;
  921. if (get_PollFlagFree(l2, skb)) {
  922. stop_t200(l2, 8);
  923. lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
  924. mISDN_FsmChangeState(fi, ST_L2_4);
  925. if (l2->tm)
  926. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  927. }
  928. }
  929. static void
  930. enquiry_cr(struct layer2 *l2, u_char typ, u_char cr, u_char pf)
  931. {
  932. struct sk_buff *skb;
  933. u_char tmp[MAX_L2HEADER_LEN];
  934. int i;
  935. i = sethdraddr(l2, tmp, cr);
  936. if (test_bit(FLG_MOD128, &l2->flag)) {
  937. tmp[i++] = typ;
  938. tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
  939. } else
  940. tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
  941. skb = mI_alloc_skb(i, GFP_ATOMIC);
  942. if (!skb) {
  943. printk(KERN_WARNING
  944. "isdnl2 can't alloc sbbuff for enquiry_cr\n");
  945. return;
  946. }
  947. memcpy(skb_put(skb, i), tmp, i);
  948. enqueue_super(l2, skb);
  949. }
  950. inline void
  951. enquiry_response(struct layer2 *l2)
  952. {
  953. if (test_bit(FLG_OWN_BUSY, &l2->flag))
  954. enquiry_cr(l2, RNR, RSP, 1);
  955. else
  956. enquiry_cr(l2, RR, RSP, 1);
  957. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  958. }
  959. inline void
  960. transmit_enquiry(struct layer2 *l2)
  961. {
  962. if (test_bit(FLG_OWN_BUSY, &l2->flag))
  963. enquiry_cr(l2, RNR, CMD, 1);
  964. else
  965. enquiry_cr(l2, RR, CMD, 1);
  966. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  967. start_t200(l2, 9);
  968. }
  969. static void
  970. nrerrorrecovery(struct FsmInst *fi)
  971. {
  972. struct layer2 *l2 = fi->userdata;
  973. l2mgr(l2, MDL_ERROR_IND, (void *) 'J');
  974. establishlink(fi);
  975. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  976. }
  977. static void
  978. invoke_retransmission(struct layer2 *l2, unsigned int nr)
  979. {
  980. u_int p1;
  981. if (l2->vs != nr) {
  982. while (l2->vs != nr) {
  983. (l2->vs)--;
  984. if (test_bit(FLG_MOD128, &l2->flag)) {
  985. l2->vs %= 128;
  986. p1 = (l2->vs - l2->va) % 128;
  987. } else {
  988. l2->vs %= 8;
  989. p1 = (l2->vs - l2->va) % 8;
  990. }
  991. p1 = (p1 + l2->sow) % l2->window;
  992. if (l2->windowar[p1])
  993. skb_queue_head(&l2->i_queue, l2->windowar[p1]);
  994. else
  995. printk(KERN_WARNING
  996. "%s: windowar[%d] is NULL\n",
  997. __func__, p1);
  998. l2->windowar[p1] = NULL;
  999. }
  1000. mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
  1001. }
  1002. }
  1003. static void
  1004. l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
  1005. {
  1006. struct layer2 *l2 = fi->userdata;
  1007. struct sk_buff *skb = arg;
  1008. int PollFlag, rsp, typ = RR;
  1009. unsigned int nr;
  1010. rsp = *skb->data & 0x2;
  1011. if (test_bit(FLG_ORIG, &l2->flag))
  1012. rsp = !rsp;
  1013. skb_pull(skb, l2addrsize(l2));
  1014. if (IsRNR(skb->data, l2)) {
  1015. set_peer_busy(l2);
  1016. typ = RNR;
  1017. } else
  1018. clear_peer_busy(l2);
  1019. if (IsREJ(skb->data, l2))
  1020. typ = REJ;
  1021. if (test_bit(FLG_MOD128, &l2->flag)) {
  1022. PollFlag = (skb->data[1] & 0x1) == 0x1;
  1023. nr = skb->data[1] >> 1;
  1024. } else {
  1025. PollFlag = (skb->data[0] & 0x10);
  1026. nr = (skb->data[0] >> 5) & 0x7;
  1027. }
  1028. dev_kfree_skb(skb);
  1029. if (PollFlag) {
  1030. if (rsp)
  1031. l2mgr(l2, MDL_ERROR_IND, (void *) 'A');
  1032. else
  1033. enquiry_response(l2);
  1034. }
  1035. if (legalnr(l2, nr)) {
  1036. if (typ == REJ) {
  1037. setva(l2, nr);
  1038. invoke_retransmission(l2, nr);
  1039. stop_t200(l2, 10);
  1040. if (mISDN_FsmAddTimer(&l2->t203, l2->T203,
  1041. EV_L2_T203, NULL, 6))
  1042. l2m_debug(&l2->l2m, "Restart T203 ST7 REJ");
  1043. } else if ((nr == l2->vs) && (typ == RR)) {
  1044. setva(l2, nr);
  1045. stop_t200(l2, 11);
  1046. mISDN_FsmRestartTimer(&l2->t203, l2->T203,
  1047. EV_L2_T203, NULL, 7);
  1048. } else if ((l2->va != nr) || (typ == RNR)) {
  1049. setva(l2, nr);
  1050. if (typ != RR)
  1051. mISDN_FsmDelTimer(&l2->t203, 9);
  1052. restart_t200(l2, 12);
  1053. }
  1054. if (skb_queue_len(&l2->i_queue) && (typ == RR))
  1055. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  1056. } else
  1057. nrerrorrecovery(fi);
  1058. }
  1059. static void
  1060. l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
  1061. {
  1062. struct layer2 *l2 = fi->userdata;
  1063. struct sk_buff *skb = arg;
  1064. if (!test_bit(FLG_L3_INIT, &l2->flag))
  1065. skb_queue_tail(&l2->i_queue, skb);
  1066. else
  1067. dev_kfree_skb(skb);
  1068. }
  1069. static void
  1070. l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
  1071. {
  1072. struct layer2 *l2 = fi->userdata;
  1073. struct sk_buff *skb = arg;
  1074. skb_queue_tail(&l2->i_queue, skb);
  1075. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  1076. }
  1077. static void
  1078. l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
  1079. {
  1080. struct layer2 *l2 = fi->userdata;
  1081. struct sk_buff *skb = arg;
  1082. skb_queue_tail(&l2->i_queue, skb);
  1083. }
  1084. static void
  1085. l2_got_iframe(struct FsmInst *fi, int event, void *arg)
  1086. {
  1087. struct layer2 *l2 = fi->userdata;
  1088. struct sk_buff *skb = arg;
  1089. int PollFlag, i;
  1090. u_int ns, nr;
  1091. i = l2addrsize(l2);
  1092. if (test_bit(FLG_MOD128, &l2->flag)) {
  1093. PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
  1094. ns = skb->data[i] >> 1;
  1095. nr = (skb->data[i + 1] >> 1) & 0x7f;
  1096. } else {
  1097. PollFlag = (skb->data[i] & 0x10);
  1098. ns = (skb->data[i] >> 1) & 0x7;
  1099. nr = (skb->data[i] >> 5) & 0x7;
  1100. }
  1101. if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
  1102. dev_kfree_skb(skb);
  1103. if (PollFlag)
  1104. enquiry_response(l2);
  1105. } else {
  1106. if (l2->vr == ns) {
  1107. l2->vr++;
  1108. if (test_bit(FLG_MOD128, &l2->flag))
  1109. l2->vr %= 128;
  1110. else
  1111. l2->vr %= 8;
  1112. test_and_clear_bit(FLG_REJEXC, &l2->flag);
  1113. if (PollFlag)
  1114. enquiry_response(l2);
  1115. else
  1116. test_and_set_bit(FLG_ACK_PEND, &l2->flag);
  1117. skb_pull(skb, l2headersize(l2, 0));
  1118. l2up(l2, DL_DATA_IND, skb);
  1119. } else {
  1120. /* n(s)!=v(r) */
  1121. dev_kfree_skb(skb);
  1122. if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
  1123. if (PollFlag)
  1124. enquiry_response(l2);
  1125. } else {
  1126. enquiry_cr(l2, REJ, RSP, PollFlag);
  1127. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1128. }
  1129. }
  1130. }
  1131. if (legalnr(l2, nr)) {
  1132. if (!test_bit(FLG_PEER_BUSY, &l2->flag) &&
  1133. (fi->state == ST_L2_7)) {
  1134. if (nr == l2->vs) {
  1135. stop_t200(l2, 13);
  1136. mISDN_FsmRestartTimer(&l2->t203, l2->T203,
  1137. EV_L2_T203, NULL, 7);
  1138. } else if (nr != l2->va)
  1139. restart_t200(l2, 14);
  1140. }
  1141. setva(l2, nr);
  1142. } else {
  1143. nrerrorrecovery(fi);
  1144. return;
  1145. }
  1146. if (skb_queue_len(&l2->i_queue) && (fi->state == ST_L2_7))
  1147. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  1148. if (test_and_clear_bit(FLG_ACK_PEND, &l2->flag))
  1149. enquiry_cr(l2, RR, RSP, 0);
  1150. }
  1151. static void
  1152. l2_got_tei(struct FsmInst *fi, int event, void *arg)
  1153. {
  1154. struct layer2 *l2 = fi->userdata;
  1155. u_int info;
  1156. l2->tei = (signed char)(long)arg;
  1157. set_channel_address(&l2->ch, l2->sapi, l2->tei);
  1158. info = DL_INFO_L2_CONNECT;
  1159. l2up_create(l2, DL_INFORMATION_IND, sizeof(info), &info);
  1160. if (fi->state == ST_L2_3) {
  1161. establishlink(fi);
  1162. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  1163. } else
  1164. mISDN_FsmChangeState(fi, ST_L2_4);
  1165. if (skb_queue_len(&l2->ui_queue))
  1166. tx_ui(l2);
  1167. }
  1168. static void
  1169. l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
  1170. {
  1171. struct layer2 *l2 = fi->userdata;
  1172. if (test_bit(FLG_LAPD, &l2->flag) &&
  1173. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1174. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1175. } else if (l2->rc == l2->N200) {
  1176. mISDN_FsmChangeState(fi, ST_L2_4);
  1177. test_and_clear_bit(FLG_T200_RUN, &l2->flag);
  1178. skb_queue_purge(&l2->i_queue);
  1179. l2mgr(l2, MDL_ERROR_IND, (void *) 'G');
  1180. if (test_bit(FLG_LAPB, &l2->flag))
  1181. l2down_create(l2, PH_DEACTIVATE_REQ,
  1182. l2_newid(l2), 0, NULL);
  1183. st5_dl_release_l2l3(l2);
  1184. if (l2->tm)
  1185. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1186. } else {
  1187. l2->rc++;
  1188. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1189. send_uframe(l2, NULL, (test_bit(FLG_MOD128, &l2->flag) ?
  1190. SABME : SABM) | 0x10, CMD);
  1191. }
  1192. }
  1193. static void
  1194. l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
  1195. {
  1196. struct layer2 *l2 = fi->userdata;
  1197. if (test_bit(FLG_LAPD, &l2->flag) &&
  1198. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1199. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1200. } else if (l2->rc == l2->N200) {
  1201. mISDN_FsmChangeState(fi, ST_L2_4);
  1202. test_and_clear_bit(FLG_T200_RUN, &l2->flag);
  1203. l2mgr(l2, MDL_ERROR_IND, (void *) 'H');
  1204. lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
  1205. if (l2->tm)
  1206. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1207. } else {
  1208. l2->rc++;
  1209. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200,
  1210. NULL, 9);
  1211. send_uframe(l2, NULL, DISC | 0x10, CMD);
  1212. }
  1213. }
  1214. static void
  1215. l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
  1216. {
  1217. struct layer2 *l2 = fi->userdata;
  1218. if (test_bit(FLG_LAPD, &l2->flag) &&
  1219. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1220. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1221. return;
  1222. }
  1223. test_and_clear_bit(FLG_T200_RUN, &l2->flag);
  1224. l2->rc = 0;
  1225. mISDN_FsmChangeState(fi, ST_L2_8);
  1226. transmit_enquiry(l2);
  1227. l2->rc++;
  1228. }
  1229. static void
  1230. l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
  1231. {
  1232. struct layer2 *l2 = fi->userdata;
  1233. if (test_bit(FLG_LAPD, &l2->flag) &&
  1234. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1235. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1236. return;
  1237. }
  1238. test_and_clear_bit(FLG_T200_RUN, &l2->flag);
  1239. if (l2->rc == l2->N200) {
  1240. l2mgr(l2, MDL_ERROR_IND, (void *) 'I');
  1241. establishlink(fi);
  1242. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  1243. } else {
  1244. transmit_enquiry(l2);
  1245. l2->rc++;
  1246. }
  1247. }
  1248. static void
  1249. l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
  1250. {
  1251. struct layer2 *l2 = fi->userdata;
  1252. if (test_bit(FLG_LAPD, &l2->flag) &&
  1253. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1254. mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 9);
  1255. return;
  1256. }
  1257. mISDN_FsmChangeState(fi, ST_L2_8);
  1258. transmit_enquiry(l2);
  1259. l2->rc = 0;
  1260. }
  1261. static void
  1262. l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
  1263. {
  1264. struct layer2 *l2 = fi->userdata;
  1265. struct sk_buff *skb, *nskb, *oskb;
  1266. u_char header[MAX_L2HEADER_LEN];
  1267. u_int i, p1;
  1268. if (!cansend(l2))
  1269. return;
  1270. skb = skb_dequeue(&l2->i_queue);
  1271. if (!skb)
  1272. return;
  1273. if (test_bit(FLG_MOD128, &l2->flag))
  1274. p1 = (l2->vs - l2->va) % 128;
  1275. else
  1276. p1 = (l2->vs - l2->va) % 8;
  1277. p1 = (p1 + l2->sow) % l2->window;
  1278. if (l2->windowar[p1]) {
  1279. printk(KERN_WARNING "isdnl2 try overwrite ack queue entry %d\n",
  1280. p1);
  1281. dev_kfree_skb(l2->windowar[p1]);
  1282. }
  1283. l2->windowar[p1] = skb;
  1284. i = sethdraddr(l2, header, CMD);
  1285. if (test_bit(FLG_MOD128, &l2->flag)) {
  1286. header[i++] = l2->vs << 1;
  1287. header[i++] = l2->vr << 1;
  1288. l2->vs = (l2->vs + 1) % 128;
  1289. } else {
  1290. header[i++] = (l2->vr << 5) | (l2->vs << 1);
  1291. l2->vs = (l2->vs + 1) % 8;
  1292. }
  1293. nskb = skb_clone(skb, GFP_ATOMIC);
  1294. p1 = skb_headroom(nskb);
  1295. if (p1 >= i)
  1296. memcpy(skb_push(nskb, i), header, i);
  1297. else {
  1298. printk(KERN_WARNING
  1299. "isdnl2 pull_iqueue skb header(%d/%d) too short\n", i, p1);
  1300. oskb = nskb;
  1301. nskb = mI_alloc_skb(oskb->len + i, GFP_ATOMIC);
  1302. if (!nskb) {
  1303. dev_kfree_skb(oskb);
  1304. printk(KERN_WARNING "%s: no skb mem\n", __func__);
  1305. return;
  1306. }
  1307. memcpy(skb_put(nskb, i), header, i);
  1308. memcpy(skb_put(nskb, oskb->len), oskb->data, oskb->len);
  1309. dev_kfree_skb(oskb);
  1310. }
  1311. l2down(l2, PH_DATA_REQ, l2_newid(l2), nskb);
  1312. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1313. if (!test_and_set_bit(FLG_T200_RUN, &l2->flag)) {
  1314. mISDN_FsmDelTimer(&l2->t203, 13);
  1315. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 11);
  1316. }
  1317. }
  1318. static void
  1319. l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
  1320. {
  1321. struct layer2 *l2 = fi->userdata;
  1322. struct sk_buff *skb = arg;
  1323. int PollFlag, rsp, rnr = 0;
  1324. unsigned int nr;
  1325. rsp = *skb->data & 0x2;
  1326. if (test_bit(FLG_ORIG, &l2->flag))
  1327. rsp = !rsp;
  1328. skb_pull(skb, l2addrsize(l2));
  1329. if (IsRNR(skb->data, l2)) {
  1330. set_peer_busy(l2);
  1331. rnr = 1;
  1332. } else
  1333. clear_peer_busy(l2);
  1334. if (test_bit(FLG_MOD128, &l2->flag)) {
  1335. PollFlag = (skb->data[1] & 0x1) == 0x1;
  1336. nr = skb->data[1] >> 1;
  1337. } else {
  1338. PollFlag = (skb->data[0] & 0x10);
  1339. nr = (skb->data[0] >> 5) & 0x7;
  1340. }
  1341. dev_kfree_skb(skb);
  1342. if (rsp && PollFlag) {
  1343. if (legalnr(l2, nr)) {
  1344. if (rnr) {
  1345. restart_t200(l2, 15);
  1346. } else {
  1347. stop_t200(l2, 16);
  1348. mISDN_FsmAddTimer(&l2->t203, l2->T203,
  1349. EV_L2_T203, NULL, 5);
  1350. setva(l2, nr);
  1351. }
  1352. invoke_retransmission(l2, nr);
  1353. mISDN_FsmChangeState(fi, ST_L2_7);
  1354. if (skb_queue_len(&l2->i_queue) && cansend(l2))
  1355. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  1356. } else
  1357. nrerrorrecovery(fi);
  1358. } else {
  1359. if (!rsp && PollFlag)
  1360. enquiry_response(l2);
  1361. if (legalnr(l2, nr))
  1362. setva(l2, nr);
  1363. else
  1364. nrerrorrecovery(fi);
  1365. }
  1366. }
  1367. static void
  1368. l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
  1369. {
  1370. struct layer2 *l2 = fi->userdata;
  1371. struct sk_buff *skb = arg;
  1372. skb_pull(skb, l2addrsize(l2) + 1);
  1373. if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) || /* I or S */
  1374. (IsUA(skb->data) && (fi->state == ST_L2_7))) {
  1375. l2mgr(l2, MDL_ERROR_IND, (void *) 'K');
  1376. establishlink(fi);
  1377. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  1378. }
  1379. dev_kfree_skb(skb);
  1380. }
  1381. static void
  1382. l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
  1383. {
  1384. struct layer2 *l2 = fi->userdata;
  1385. skb_queue_purge(&l2->ui_queue);
  1386. l2->tei = GROUP_TEI;
  1387. mISDN_FsmChangeState(fi, ST_L2_1);
  1388. }
  1389. static void
  1390. l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
  1391. {
  1392. struct layer2 *l2 = fi->userdata;
  1393. skb_queue_purge(&l2->ui_queue);
  1394. l2->tei = GROUP_TEI;
  1395. l2up_create(l2, DL_RELEASE_IND, 0, NULL);
  1396. mISDN_FsmChangeState(fi, ST_L2_1);
  1397. }
  1398. static void
  1399. l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
  1400. {
  1401. struct layer2 *l2 = fi->userdata;
  1402. skb_queue_purge(&l2->i_queue);
  1403. skb_queue_purge(&l2->ui_queue);
  1404. freewin(l2);
  1405. l2->tei = GROUP_TEI;
  1406. stop_t200(l2, 17);
  1407. st5_dl_release_l2l3(l2);
  1408. mISDN_FsmChangeState(fi, ST_L2_1);
  1409. }
  1410. static void
  1411. l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
  1412. {
  1413. struct layer2 *l2 = fi->userdata;
  1414. skb_queue_purge(&l2->ui_queue);
  1415. l2->tei = GROUP_TEI;
  1416. stop_t200(l2, 18);
  1417. l2up_create(l2, DL_RELEASE_IND, 0, NULL);
  1418. mISDN_FsmChangeState(fi, ST_L2_1);
  1419. }
  1420. static void
  1421. l2_tei_remove(struct FsmInst *fi, int event, void *arg)
  1422. {
  1423. struct layer2 *l2 = fi->userdata;
  1424. skb_queue_purge(&l2->i_queue);
  1425. skb_queue_purge(&l2->ui_queue);
  1426. freewin(l2);
  1427. l2->tei = GROUP_TEI;
  1428. stop_t200(l2, 17);
  1429. mISDN_FsmDelTimer(&l2->t203, 19);
  1430. l2up_create(l2, DL_RELEASE_IND, 0, NULL);
  1431. /* mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
  1432. * MGR_SHORTSTATUS_IND, SSTATUS_L2_RELEASED,
  1433. * 0, NULL, 0);
  1434. */
  1435. mISDN_FsmChangeState(fi, ST_L2_1);
  1436. }
  1437. static void
  1438. l2_st14_persistant_da(struct FsmInst *fi, int event, void *arg)
  1439. {
  1440. struct layer2 *l2 = fi->userdata;
  1441. struct sk_buff *skb = arg;
  1442. skb_queue_purge(&l2->i_queue);
  1443. skb_queue_purge(&l2->ui_queue);
  1444. if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
  1445. l2up(l2, DL_RELEASE_IND, skb);
  1446. else
  1447. dev_kfree_skb(skb);
  1448. }
  1449. static void
  1450. l2_st5_persistant_da(struct FsmInst *fi, int event, void *arg)
  1451. {
  1452. struct layer2 *l2 = fi->userdata;
  1453. struct sk_buff *skb = arg;
  1454. skb_queue_purge(&l2->i_queue);
  1455. skb_queue_purge(&l2->ui_queue);
  1456. freewin(l2);
  1457. stop_t200(l2, 19);
  1458. st5_dl_release_l2l3(l2);
  1459. mISDN_FsmChangeState(fi, ST_L2_4);
  1460. if (l2->tm)
  1461. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1462. dev_kfree_skb(skb);
  1463. }
  1464. static void
  1465. l2_st6_persistant_da(struct FsmInst *fi, int event, void *arg)
  1466. {
  1467. struct layer2 *l2 = fi->userdata;
  1468. struct sk_buff *skb = arg;
  1469. skb_queue_purge(&l2->ui_queue);
  1470. stop_t200(l2, 20);
  1471. l2up(l2, DL_RELEASE_CNF, skb);
  1472. mISDN_FsmChangeState(fi, ST_L2_4);
  1473. if (l2->tm)
  1474. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1475. }
  1476. static void
  1477. l2_persistant_da(struct FsmInst *fi, int event, void *arg)
  1478. {
  1479. struct layer2 *l2 = fi->userdata;
  1480. struct sk_buff *skb = arg;
  1481. skb_queue_purge(&l2->i_queue);
  1482. skb_queue_purge(&l2->ui_queue);
  1483. freewin(l2);
  1484. stop_t200(l2, 19);
  1485. mISDN_FsmDelTimer(&l2->t203, 19);
  1486. l2up(l2, DL_RELEASE_IND, skb);
  1487. mISDN_FsmChangeState(fi, ST_L2_4);
  1488. if (l2->tm)
  1489. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1490. }
  1491. static void
  1492. l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
  1493. {
  1494. struct layer2 *l2 = fi->userdata;
  1495. struct sk_buff *skb = arg;
  1496. if (!test_and_set_bit(FLG_OWN_BUSY, &l2->flag)) {
  1497. enquiry_cr(l2, RNR, RSP, 0);
  1498. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1499. }
  1500. if (skb)
  1501. dev_kfree_skb(skb);
  1502. }
  1503. static void
  1504. l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
  1505. {
  1506. struct layer2 *l2 = fi->userdata;
  1507. struct sk_buff *skb = arg;
  1508. if (!test_and_clear_bit(FLG_OWN_BUSY, &l2->flag)) {
  1509. enquiry_cr(l2, RR, RSP, 0);
  1510. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1511. }
  1512. if (skb)
  1513. dev_kfree_skb(skb);
  1514. }
  1515. static void
  1516. l2_frame_error(struct FsmInst *fi, int event, void *arg)
  1517. {
  1518. struct layer2 *l2 = fi->userdata;
  1519. l2mgr(l2, MDL_ERROR_IND, arg);
  1520. }
  1521. static void
  1522. l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
  1523. {
  1524. struct layer2 *l2 = fi->userdata;
  1525. l2mgr(l2, MDL_ERROR_IND, arg);
  1526. establishlink(fi);
  1527. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  1528. }
  1529. static struct FsmNode L2FnList[] =
  1530. {
  1531. {ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
  1532. {ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
  1533. {ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
  1534. {ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
  1535. {ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
  1536. {ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
  1537. {ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
  1538. {ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
  1539. {ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
  1540. {ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
  1541. {ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
  1542. {ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
  1543. {ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
  1544. {ST_L2_1, EV_L2_DL_UNITDATA, l2_queue_ui_assign},
  1545. {ST_L2_2, EV_L2_DL_UNITDATA, l2_queue_ui},
  1546. {ST_L2_3, EV_L2_DL_UNITDATA, l2_queue_ui},
  1547. {ST_L2_4, EV_L2_DL_UNITDATA, l2_send_ui},
  1548. {ST_L2_5, EV_L2_DL_UNITDATA, l2_send_ui},
  1549. {ST_L2_6, EV_L2_DL_UNITDATA, l2_send_ui},
  1550. {ST_L2_7, EV_L2_DL_UNITDATA, l2_send_ui},
  1551. {ST_L2_8, EV_L2_DL_UNITDATA, l2_send_ui},
  1552. {ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
  1553. {ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
  1554. {ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
  1555. {ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
  1556. {ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
  1557. {ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
  1558. {ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
  1559. {ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
  1560. {ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
  1561. {ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
  1562. {ST_L2_4, EV_L2_SABME, l2_start_multi},
  1563. {ST_L2_5, EV_L2_SABME, l2_send_UA},
  1564. {ST_L2_6, EV_L2_SABME, l2_send_DM},
  1565. {ST_L2_7, EV_L2_SABME, l2_restart_multi},
  1566. {ST_L2_8, EV_L2_SABME, l2_restart_multi},
  1567. {ST_L2_4, EV_L2_DISC, l2_send_DM},
  1568. {ST_L2_5, EV_L2_DISC, l2_send_DM},
  1569. {ST_L2_6, EV_L2_DISC, l2_send_UA},
  1570. {ST_L2_7, EV_L2_DISC, l2_stop_multi},
  1571. {ST_L2_8, EV_L2_DISC, l2_stop_multi},
  1572. {ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
  1573. {ST_L2_5, EV_L2_UA, l2_connected},
  1574. {ST_L2_6, EV_L2_UA, l2_released},
  1575. {ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
  1576. {ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
  1577. {ST_L2_4, EV_L2_DM, l2_reestablish},
  1578. {ST_L2_5, EV_L2_DM, l2_st5_dm_release},
  1579. {ST_L2_6, EV_L2_DM, l2_st6_dm_release},
  1580. {ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
  1581. {ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
  1582. {ST_L2_1, EV_L2_UI, l2_got_ui},
  1583. {ST_L2_2, EV_L2_UI, l2_got_ui},
  1584. {ST_L2_3, EV_L2_UI, l2_got_ui},
  1585. {ST_L2_4, EV_L2_UI, l2_got_ui},
  1586. {ST_L2_5, EV_L2_UI, l2_got_ui},
  1587. {ST_L2_6, EV_L2_UI, l2_got_ui},
  1588. {ST_L2_7, EV_L2_UI, l2_got_ui},
  1589. {ST_L2_8, EV_L2_UI, l2_got_ui},
  1590. {ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
  1591. {ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
  1592. {ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
  1593. {ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
  1594. {ST_L2_7, EV_L2_I, l2_got_iframe},
  1595. {ST_L2_8, EV_L2_I, l2_got_iframe},
  1596. {ST_L2_5, EV_L2_T200, l2_st5_tout_200},
  1597. {ST_L2_6, EV_L2_T200, l2_st6_tout_200},
  1598. {ST_L2_7, EV_L2_T200, l2_st7_tout_200},
  1599. {ST_L2_8, EV_L2_T200, l2_st8_tout_200},
  1600. {ST_L2_7, EV_L2_T203, l2_st7_tout_203},
  1601. {ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
  1602. {ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
  1603. {ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
  1604. {ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
  1605. {ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
  1606. {ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
  1607. {ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
  1608. {ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
  1609. {ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
  1610. {ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
  1611. {ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistant_da},
  1612. {ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
  1613. {ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
  1614. {ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistant_da},
  1615. {ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistant_da},
  1616. {ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistant_da},
  1617. {ST_L2_7, EV_L1_DEACTIVATE, l2_persistant_da},
  1618. {ST_L2_8, EV_L1_DEACTIVATE, l2_persistant_da},
  1619. };
  1620. #define L2_FN_COUNT (sizeof(L2FnList)/sizeof(struct FsmNode))
  1621. static int
  1622. ph_data_indication(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb)
  1623. {
  1624. u_char *datap = skb->data;
  1625. int ret = -EINVAL;
  1626. int psapi, ptei;
  1627. u_int l;
  1628. int c = 0;
  1629. l = l2addrsize(l2);
  1630. if (skb->len <= l) {
  1631. mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *) 'N');
  1632. return ret;
  1633. }
  1634. if (test_bit(FLG_LAPD, &l2->flag)) { /* Maybe not needed */
  1635. psapi = *datap++;
  1636. ptei = *datap++;
  1637. if ((psapi & 1) || !(ptei & 1)) {
  1638. printk(KERN_WARNING
  1639. "l2 D-channel frame wrong EA0/EA1\n");
  1640. return ret;
  1641. }
  1642. psapi >>= 2;
  1643. ptei >>= 1;
  1644. if (psapi != l2->sapi) {
  1645. /* not our bussiness
  1646. * printk(KERN_DEBUG "%s: sapi %d/%d sapi mismatch\n",
  1647. * __func__,
  1648. * psapi, l2->sapi);
  1649. */
  1650. dev_kfree_skb(skb);
  1651. return 0;
  1652. }
  1653. if ((ptei != l2->tei) && (ptei != GROUP_TEI)) {
  1654. /* not our bussiness
  1655. * printk(KERN_DEBUG "%s: tei %d/%d sapi %d mismatch\n",
  1656. * __func__,
  1657. * ptei, l2->tei, psapi);
  1658. */
  1659. dev_kfree_skb(skb);
  1660. return 0;
  1661. }
  1662. } else
  1663. datap += l;
  1664. if (!(*datap & 1)) { /* I-Frame */
  1665. c = iframe_error(l2, skb);
  1666. if (!c)
  1667. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_I, skb);
  1668. } else if (IsSFrame(datap, l2)) { /* S-Frame */
  1669. c = super_error(l2, skb);
  1670. if (!c)
  1671. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SUPER, skb);
  1672. } else if (IsUI(datap)) {
  1673. c = UI_error(l2, skb);
  1674. if (!c)
  1675. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UI, skb);
  1676. } else if (IsSABME(datap, l2)) {
  1677. c = unnum_error(l2, skb, CMD);
  1678. if (!c)
  1679. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SABME, skb);
  1680. } else if (IsUA(datap)) {
  1681. c = unnum_error(l2, skb, RSP);
  1682. if (!c)
  1683. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UA, skb);
  1684. } else if (IsDISC(datap)) {
  1685. c = unnum_error(l2, skb, CMD);
  1686. if (!c)
  1687. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DISC, skb);
  1688. } else if (IsDM(datap)) {
  1689. c = unnum_error(l2, skb, RSP);
  1690. if (!c)
  1691. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DM, skb);
  1692. } else if (IsFRMR(datap)) {
  1693. c = FRMR_error(l2, skb);
  1694. if (!c)
  1695. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_FRMR, skb);
  1696. } else
  1697. c = 'L';
  1698. if (c) {
  1699. printk(KERN_WARNING "l2 D-channel frame error %c\n", c);
  1700. mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
  1701. }
  1702. return ret;
  1703. }
  1704. static int
  1705. l2_send(struct mISDNchannel *ch, struct sk_buff *skb)
  1706. {
  1707. struct layer2 *l2 = container_of(ch, struct layer2, ch);
  1708. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1709. int ret = -EINVAL;
  1710. if (*debug & DEBUG_L2_RECV)
  1711. printk(KERN_DEBUG "%s: prim(%x) id(%x) tei(%d)\n",
  1712. __func__, hh->prim, hh->id, l2->tei);
  1713. switch (hh->prim) {
  1714. case PH_DATA_IND:
  1715. ret = ph_data_indication(l2, hh, skb);
  1716. break;
  1717. case PH_DATA_CNF:
  1718. ret = ph_data_confirm(l2, hh, skb);
  1719. break;
  1720. case PH_ACTIVATE_IND:
  1721. test_and_set_bit(FLG_L1_ACTIV, &l2->flag);
  1722. l2up_create(l2, MPH_ACTIVATE_IND, 0, NULL);
  1723. if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
  1724. ret = mISDN_FsmEvent(&l2->l2m,
  1725. EV_L2_DL_ESTABLISH_REQ, skb);
  1726. break;
  1727. case PH_DEACTIVATE_IND:
  1728. test_and_clear_bit(FLG_L1_ACTIV, &l2->flag);
  1729. l2up_create(l2, MPH_DEACTIVATE_IND, 0, NULL);
  1730. ret = mISDN_FsmEvent(&l2->l2m, EV_L1_DEACTIVATE, skb);
  1731. break;
  1732. case MPH_INFORMATION_IND:
  1733. if (!l2->up)
  1734. break;
  1735. ret = l2->up->send(l2->up, skb);
  1736. break;
  1737. case DL_DATA_REQ:
  1738. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_DATA, skb);
  1739. break;
  1740. case DL_UNITDATA_REQ:
  1741. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_UNITDATA, skb);
  1742. break;
  1743. case DL_ESTABLISH_REQ:
  1744. if (test_bit(FLG_LAPB, &l2->flag))
  1745. test_and_set_bit(FLG_ORIG, &l2->flag);
  1746. if (test_bit(FLG_L1_ACTIV, &l2->flag)) {
  1747. if (test_bit(FLG_LAPD, &l2->flag) ||
  1748. test_bit(FLG_ORIG, &l2->flag))
  1749. ret = mISDN_FsmEvent(&l2->l2m,
  1750. EV_L2_DL_ESTABLISH_REQ, skb);
  1751. } else {
  1752. if (test_bit(FLG_LAPD, &l2->flag) ||
  1753. test_bit(FLG_ORIG, &l2->flag)) {
  1754. test_and_set_bit(FLG_ESTAB_PEND,
  1755. &l2->flag);
  1756. }
  1757. ret = l2down(l2, PH_ACTIVATE_REQ, l2_newid(l2),
  1758. skb);
  1759. }
  1760. break;
  1761. case DL_RELEASE_REQ:
  1762. if (test_bit(FLG_LAPB, &l2->flag))
  1763. l2down_create(l2, PH_DEACTIVATE_REQ,
  1764. l2_newid(l2), 0, NULL);
  1765. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_RELEASE_REQ,
  1766. skb);
  1767. break;
  1768. default:
  1769. if (*debug & DEBUG_L2)
  1770. l2m_debug(&l2->l2m, "l2 unknown pr %04x",
  1771. hh->prim);
  1772. }
  1773. if (ret) {
  1774. dev_kfree_skb(skb);
  1775. ret = 0;
  1776. }
  1777. return ret;
  1778. }
  1779. int
  1780. tei_l2(struct layer2 *l2, u_int cmd, u_long arg)
  1781. {
  1782. int ret = -EINVAL;
  1783. if (*debug & DEBUG_L2_TEI)
  1784. printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
  1785. switch (cmd) {
  1786. case (MDL_ASSIGN_REQ):
  1787. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ASSIGN, (void *)arg);
  1788. break;
  1789. case (MDL_REMOVE_REQ):
  1790. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_REMOVE, NULL);
  1791. break;
  1792. case (MDL_ERROR_IND):
  1793. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
  1794. break;
  1795. case (MDL_ERROR_RSP):
  1796. /* ETS 300-125 5.3.2.1 Test: TC13010 */
  1797. printk(KERN_NOTICE "MDL_ERROR|REQ (tei_l2)\n");
  1798. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
  1799. break;
  1800. }
  1801. return ret;
  1802. }
  1803. static void
  1804. release_l2(struct layer2 *l2)
  1805. {
  1806. mISDN_FsmDelTimer(&l2->t200, 21);
  1807. mISDN_FsmDelTimer(&l2->t203, 16);
  1808. skb_queue_purge(&l2->i_queue);
  1809. skb_queue_purge(&l2->ui_queue);
  1810. skb_queue_purge(&l2->down_queue);
  1811. ReleaseWin(l2);
  1812. if (test_bit(FLG_LAPD, &l2->flag)) {
  1813. TEIrelease(l2);
  1814. if (l2->ch.st)
  1815. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D,
  1816. CLOSE_CHANNEL, NULL);
  1817. }
  1818. kfree(l2);
  1819. }
  1820. static int
  1821. l2_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1822. {
  1823. struct layer2 *l2 = container_of(ch, struct layer2, ch);
  1824. u_int info;
  1825. if (*debug & DEBUG_L2_CTRL)
  1826. printk(KERN_DEBUG "%s:(%x)\n", __func__, cmd);
  1827. switch (cmd) {
  1828. case OPEN_CHANNEL:
  1829. if (test_bit(FLG_LAPD, &l2->flag)) {
  1830. set_channel_address(&l2->ch, l2->sapi, l2->tei);
  1831. info = DL_INFO_L2_CONNECT;
  1832. l2up_create(l2, DL_INFORMATION_IND,
  1833. sizeof(info), &info);
  1834. }
  1835. break;
  1836. case CLOSE_CHANNEL:
  1837. if (l2->ch.peer)
  1838. l2->ch.peer->ctrl(l2->ch.peer, CLOSE_CHANNEL, NULL);
  1839. release_l2(l2);
  1840. break;
  1841. }
  1842. return 0;
  1843. }
  1844. struct layer2 *
  1845. create_l2(struct mISDNchannel *ch, u_int protocol, u_long options, int tei,
  1846. int sapi)
  1847. {
  1848. struct layer2 *l2;
  1849. struct channel_req rq;
  1850. l2 = kzalloc(sizeof(struct layer2), GFP_KERNEL);
  1851. if (!l2) {
  1852. printk(KERN_ERR "kzalloc layer2 failed\n");
  1853. return NULL;
  1854. }
  1855. l2->next_id = 1;
  1856. l2->down_id = MISDN_ID_NONE;
  1857. l2->up = ch;
  1858. l2->ch.st = ch->st;
  1859. l2->ch.send = l2_send;
  1860. l2->ch.ctrl = l2_ctrl;
  1861. switch (protocol) {
  1862. case ISDN_P_LAPD_NT:
  1863. test_and_set_bit(FLG_LAPD, &l2->flag);
  1864. test_and_set_bit(FLG_LAPD_NET, &l2->flag);
  1865. test_and_set_bit(FLG_MOD128, &l2->flag);
  1866. l2->sapi = sapi;
  1867. l2->maxlen = MAX_DFRAME_LEN;
  1868. if (test_bit(OPTION_L2_PMX, &options))
  1869. l2->window = 7;
  1870. else
  1871. l2->window = 1;
  1872. if (test_bit(OPTION_L2_PTP, &options))
  1873. test_and_set_bit(FLG_PTP, &l2->flag);
  1874. if (test_bit(OPTION_L2_FIXEDTEI, &options))
  1875. test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
  1876. l2->tei = tei;
  1877. l2->T200 = 1000;
  1878. l2->N200 = 3;
  1879. l2->T203 = 10000;
  1880. if (test_bit(OPTION_L2_PMX, &options))
  1881. rq.protocol = ISDN_P_NT_E1;
  1882. else
  1883. rq.protocol = ISDN_P_NT_S0;
  1884. rq.adr.channel = 0;
  1885. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
  1886. break;
  1887. case ISDN_P_LAPD_TE:
  1888. test_and_set_bit(FLG_LAPD, &l2->flag);
  1889. test_and_set_bit(FLG_MOD128, &l2->flag);
  1890. test_and_set_bit(FLG_ORIG, &l2->flag);
  1891. l2->sapi = sapi;
  1892. l2->maxlen = MAX_DFRAME_LEN;
  1893. if (test_bit(OPTION_L2_PMX, &options))
  1894. l2->window = 7;
  1895. else
  1896. l2->window = 1;
  1897. if (test_bit(OPTION_L2_PTP, &options))
  1898. test_and_set_bit(FLG_PTP, &l2->flag);
  1899. if (test_bit(OPTION_L2_FIXEDTEI, &options))
  1900. test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
  1901. l2->tei = tei;
  1902. l2->T200 = 1000;
  1903. l2->N200 = 3;
  1904. l2->T203 = 10000;
  1905. if (test_bit(OPTION_L2_PMX, &options))
  1906. rq.protocol = ISDN_P_TE_E1;
  1907. else
  1908. rq.protocol = ISDN_P_TE_S0;
  1909. rq.adr.channel = 0;
  1910. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
  1911. break;
  1912. case ISDN_P_B_X75SLP:
  1913. test_and_set_bit(FLG_LAPB, &l2->flag);
  1914. l2->window = 7;
  1915. l2->maxlen = MAX_DATA_SIZE;
  1916. l2->T200 = 1000;
  1917. l2->N200 = 4;
  1918. l2->T203 = 5000;
  1919. l2->addr.A = 3;
  1920. l2->addr.B = 1;
  1921. break;
  1922. default:
  1923. printk(KERN_ERR "layer2 create failed prt %x\n",
  1924. protocol);
  1925. kfree(l2);
  1926. return NULL;
  1927. }
  1928. skb_queue_head_init(&l2->i_queue);
  1929. skb_queue_head_init(&l2->ui_queue);
  1930. skb_queue_head_init(&l2->down_queue);
  1931. skb_queue_head_init(&l2->tmp_queue);
  1932. InitWin(l2);
  1933. l2->l2m.fsm = &l2fsm;
  1934. if (test_bit(FLG_LAPB, &l2->flag) ||
  1935. test_bit(FLG_PTP, &l2->flag) ||
  1936. test_bit(FLG_LAPD_NET, &l2->flag))
  1937. l2->l2m.state = ST_L2_4;
  1938. else
  1939. l2->l2m.state = ST_L2_1;
  1940. l2->l2m.debug = *debug;
  1941. l2->l2m.userdata = l2;
  1942. l2->l2m.userint = 0;
  1943. l2->l2m.printdebug = l2m_debug;
  1944. mISDN_FsmInitTimer(&l2->l2m, &l2->t200);
  1945. mISDN_FsmInitTimer(&l2->l2m, &l2->t203);
  1946. return l2;
  1947. }
  1948. static int
  1949. x75create(struct channel_req *crq)
  1950. {
  1951. struct layer2 *l2;
  1952. if (crq->protocol != ISDN_P_B_X75SLP)
  1953. return -EPROTONOSUPPORT;
  1954. l2 = create_l2(crq->ch, crq->protocol, 0, 0, 0);
  1955. if (!l2)
  1956. return -ENOMEM;
  1957. crq->ch = &l2->ch;
  1958. crq->protocol = ISDN_P_B_HDLC;
  1959. return 0;
  1960. }
  1961. static struct Bprotocol X75SLP = {
  1962. .Bprotocols = (1 << (ISDN_P_B_X75SLP & ISDN_P_B_MASK)),
  1963. .name = "X75SLP",
  1964. .create = x75create
  1965. };
  1966. int
  1967. Isdnl2_Init(u_int *deb)
  1968. {
  1969. debug = deb;
  1970. mISDN_register_Bprotocol(&X75SLP);
  1971. l2fsm.state_count = L2_STATE_COUNT;
  1972. l2fsm.event_count = L2_EVENT_COUNT;
  1973. l2fsm.strEvent = strL2Event;
  1974. l2fsm.strState = strL2State;
  1975. mISDN_FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
  1976. TEIInit(deb);
  1977. return 0;
  1978. }
  1979. void
  1980. Isdnl2_cleanup(void)
  1981. {
  1982. mISDN_unregister_Bprotocol(&X75SLP);
  1983. TEIFree();
  1984. mISDN_FsmFree(&l2fsm);
  1985. }