layer2.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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 (sapi %d tei %d): ", l2->sapi, 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. if (*debug & DEBUG_L2)
  1647. printk(KERN_DEBUG "%s: sapi %d/%d mismatch\n",
  1648. __func__, psapi, l2->sapi);
  1649. dev_kfree_skb(skb);
  1650. return 0;
  1651. }
  1652. if ((ptei != l2->tei) && (ptei != GROUP_TEI)) {
  1653. /* not our bussiness */
  1654. if (*debug & DEBUG_L2)
  1655. printk(KERN_DEBUG "%s: tei %d/%d mismatch\n",
  1656. __func__, ptei, l2->tei);
  1657. dev_kfree_skb(skb);
  1658. return 0;
  1659. }
  1660. } else
  1661. datap += l;
  1662. if (!(*datap & 1)) { /* I-Frame */
  1663. c = iframe_error(l2, skb);
  1664. if (!c)
  1665. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_I, skb);
  1666. } else if (IsSFrame(datap, l2)) { /* S-Frame */
  1667. c = super_error(l2, skb);
  1668. if (!c)
  1669. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SUPER, skb);
  1670. } else if (IsUI(datap)) {
  1671. c = UI_error(l2, skb);
  1672. if (!c)
  1673. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UI, skb);
  1674. } else if (IsSABME(datap, l2)) {
  1675. c = unnum_error(l2, skb, CMD);
  1676. if (!c)
  1677. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SABME, skb);
  1678. } else if (IsUA(datap)) {
  1679. c = unnum_error(l2, skb, RSP);
  1680. if (!c)
  1681. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UA, skb);
  1682. } else if (IsDISC(datap)) {
  1683. c = unnum_error(l2, skb, CMD);
  1684. if (!c)
  1685. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DISC, skb);
  1686. } else if (IsDM(datap)) {
  1687. c = unnum_error(l2, skb, RSP);
  1688. if (!c)
  1689. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DM, skb);
  1690. } else if (IsFRMR(datap)) {
  1691. c = FRMR_error(l2, skb);
  1692. if (!c)
  1693. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_FRMR, skb);
  1694. } else
  1695. c = 'L';
  1696. if (c) {
  1697. printk(KERN_WARNING "l2 D-channel frame error %c\n", c);
  1698. mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
  1699. }
  1700. return ret;
  1701. }
  1702. static int
  1703. l2_send(struct mISDNchannel *ch, struct sk_buff *skb)
  1704. {
  1705. struct layer2 *l2 = container_of(ch, struct layer2, ch);
  1706. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1707. int ret = -EINVAL;
  1708. if (*debug & DEBUG_L2_RECV)
  1709. printk(KERN_DEBUG "%s: prim(%x) id(%x) sapi(%d) tei(%d)\n",
  1710. __func__, hh->prim, hh->id, l2->sapi, l2->tei);
  1711. switch (hh->prim) {
  1712. case PH_DATA_IND:
  1713. ret = ph_data_indication(l2, hh, skb);
  1714. break;
  1715. case PH_DATA_CNF:
  1716. ret = ph_data_confirm(l2, hh, skb);
  1717. break;
  1718. case PH_ACTIVATE_IND:
  1719. test_and_set_bit(FLG_L1_ACTIV, &l2->flag);
  1720. l2up_create(l2, MPH_ACTIVATE_IND, 0, NULL);
  1721. if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
  1722. ret = mISDN_FsmEvent(&l2->l2m,
  1723. EV_L2_DL_ESTABLISH_REQ, skb);
  1724. break;
  1725. case PH_DEACTIVATE_IND:
  1726. test_and_clear_bit(FLG_L1_ACTIV, &l2->flag);
  1727. l2up_create(l2, MPH_DEACTIVATE_IND, 0, NULL);
  1728. ret = mISDN_FsmEvent(&l2->l2m, EV_L1_DEACTIVATE, skb);
  1729. break;
  1730. case MPH_INFORMATION_IND:
  1731. if (!l2->up)
  1732. break;
  1733. ret = l2->up->send(l2->up, skb);
  1734. break;
  1735. case DL_DATA_REQ:
  1736. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_DATA, skb);
  1737. break;
  1738. case DL_UNITDATA_REQ:
  1739. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_UNITDATA, skb);
  1740. break;
  1741. case DL_ESTABLISH_REQ:
  1742. if (test_bit(FLG_LAPB, &l2->flag))
  1743. test_and_set_bit(FLG_ORIG, &l2->flag);
  1744. if (test_bit(FLG_L1_ACTIV, &l2->flag)) {
  1745. if (test_bit(FLG_LAPD, &l2->flag) ||
  1746. test_bit(FLG_ORIG, &l2->flag))
  1747. ret = mISDN_FsmEvent(&l2->l2m,
  1748. EV_L2_DL_ESTABLISH_REQ, skb);
  1749. } else {
  1750. if (test_bit(FLG_LAPD, &l2->flag) ||
  1751. test_bit(FLG_ORIG, &l2->flag)) {
  1752. test_and_set_bit(FLG_ESTAB_PEND,
  1753. &l2->flag);
  1754. }
  1755. ret = l2down(l2, PH_ACTIVATE_REQ, l2_newid(l2),
  1756. skb);
  1757. }
  1758. break;
  1759. case DL_RELEASE_REQ:
  1760. if (test_bit(FLG_LAPB, &l2->flag))
  1761. l2down_create(l2, PH_DEACTIVATE_REQ,
  1762. l2_newid(l2), 0, NULL);
  1763. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_RELEASE_REQ,
  1764. skb);
  1765. break;
  1766. default:
  1767. if (*debug & DEBUG_L2)
  1768. l2m_debug(&l2->l2m, "l2 unknown pr %04x",
  1769. hh->prim);
  1770. }
  1771. if (ret) {
  1772. dev_kfree_skb(skb);
  1773. ret = 0;
  1774. }
  1775. return ret;
  1776. }
  1777. int
  1778. tei_l2(struct layer2 *l2, u_int cmd, u_long arg)
  1779. {
  1780. int ret = -EINVAL;
  1781. if (*debug & DEBUG_L2_TEI)
  1782. printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
  1783. switch (cmd) {
  1784. case (MDL_ASSIGN_REQ):
  1785. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ASSIGN, (void *)arg);
  1786. break;
  1787. case (MDL_REMOVE_REQ):
  1788. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_REMOVE, NULL);
  1789. break;
  1790. case (MDL_ERROR_IND):
  1791. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
  1792. break;
  1793. case (MDL_ERROR_RSP):
  1794. /* ETS 300-125 5.3.2.1 Test: TC13010 */
  1795. printk(KERN_NOTICE "MDL_ERROR|REQ (tei_l2)\n");
  1796. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
  1797. break;
  1798. }
  1799. return ret;
  1800. }
  1801. static void
  1802. release_l2(struct layer2 *l2)
  1803. {
  1804. mISDN_FsmDelTimer(&l2->t200, 21);
  1805. mISDN_FsmDelTimer(&l2->t203, 16);
  1806. skb_queue_purge(&l2->i_queue);
  1807. skb_queue_purge(&l2->ui_queue);
  1808. skb_queue_purge(&l2->down_queue);
  1809. ReleaseWin(l2);
  1810. if (test_bit(FLG_LAPD, &l2->flag)) {
  1811. TEIrelease(l2);
  1812. if (l2->ch.st)
  1813. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D,
  1814. CLOSE_CHANNEL, NULL);
  1815. }
  1816. kfree(l2);
  1817. }
  1818. static int
  1819. l2_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1820. {
  1821. struct layer2 *l2 = container_of(ch, struct layer2, ch);
  1822. u_int info;
  1823. if (*debug & DEBUG_L2_CTRL)
  1824. printk(KERN_DEBUG "%s:(%x)\n", __func__, cmd);
  1825. switch (cmd) {
  1826. case OPEN_CHANNEL:
  1827. if (test_bit(FLG_LAPD, &l2->flag)) {
  1828. set_channel_address(&l2->ch, l2->sapi, l2->tei);
  1829. info = DL_INFO_L2_CONNECT;
  1830. l2up_create(l2, DL_INFORMATION_IND,
  1831. sizeof(info), &info);
  1832. }
  1833. break;
  1834. case CLOSE_CHANNEL:
  1835. if (l2->ch.peer)
  1836. l2->ch.peer->ctrl(l2->ch.peer, CLOSE_CHANNEL, NULL);
  1837. release_l2(l2);
  1838. break;
  1839. }
  1840. return 0;
  1841. }
  1842. struct layer2 *
  1843. create_l2(struct mISDNchannel *ch, u_int protocol, u_long options, int tei,
  1844. int sapi)
  1845. {
  1846. struct layer2 *l2;
  1847. struct channel_req rq;
  1848. l2 = kzalloc(sizeof(struct layer2), GFP_KERNEL);
  1849. if (!l2) {
  1850. printk(KERN_ERR "kzalloc layer2 failed\n");
  1851. return NULL;
  1852. }
  1853. l2->next_id = 1;
  1854. l2->down_id = MISDN_ID_NONE;
  1855. l2->up = ch;
  1856. l2->ch.st = ch->st;
  1857. l2->ch.send = l2_send;
  1858. l2->ch.ctrl = l2_ctrl;
  1859. switch (protocol) {
  1860. case ISDN_P_LAPD_NT:
  1861. test_and_set_bit(FLG_LAPD, &l2->flag);
  1862. test_and_set_bit(FLG_LAPD_NET, &l2->flag);
  1863. test_and_set_bit(FLG_MOD128, &l2->flag);
  1864. l2->sapi = sapi;
  1865. l2->maxlen = MAX_DFRAME_LEN;
  1866. if (test_bit(OPTION_L2_PMX, &options))
  1867. l2->window = 7;
  1868. else
  1869. l2->window = 1;
  1870. if (test_bit(OPTION_L2_PTP, &options))
  1871. test_and_set_bit(FLG_PTP, &l2->flag);
  1872. if (test_bit(OPTION_L2_FIXEDTEI, &options))
  1873. test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
  1874. l2->tei = tei;
  1875. l2->T200 = 1000;
  1876. l2->N200 = 3;
  1877. l2->T203 = 10000;
  1878. if (test_bit(OPTION_L2_PMX, &options))
  1879. rq.protocol = ISDN_P_NT_E1;
  1880. else
  1881. rq.protocol = ISDN_P_NT_S0;
  1882. rq.adr.channel = 0;
  1883. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
  1884. break;
  1885. case ISDN_P_LAPD_TE:
  1886. test_and_set_bit(FLG_LAPD, &l2->flag);
  1887. test_and_set_bit(FLG_MOD128, &l2->flag);
  1888. test_and_set_bit(FLG_ORIG, &l2->flag);
  1889. l2->sapi = sapi;
  1890. l2->maxlen = MAX_DFRAME_LEN;
  1891. if (test_bit(OPTION_L2_PMX, &options))
  1892. l2->window = 7;
  1893. else
  1894. l2->window = 1;
  1895. if (test_bit(OPTION_L2_PTP, &options))
  1896. test_and_set_bit(FLG_PTP, &l2->flag);
  1897. if (test_bit(OPTION_L2_FIXEDTEI, &options))
  1898. test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
  1899. l2->tei = tei;
  1900. l2->T200 = 1000;
  1901. l2->N200 = 3;
  1902. l2->T203 = 10000;
  1903. if (test_bit(OPTION_L2_PMX, &options))
  1904. rq.protocol = ISDN_P_TE_E1;
  1905. else
  1906. rq.protocol = ISDN_P_TE_S0;
  1907. rq.adr.channel = 0;
  1908. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
  1909. break;
  1910. case ISDN_P_B_X75SLP:
  1911. test_and_set_bit(FLG_LAPB, &l2->flag);
  1912. l2->window = 7;
  1913. l2->maxlen = MAX_DATA_SIZE;
  1914. l2->T200 = 1000;
  1915. l2->N200 = 4;
  1916. l2->T203 = 5000;
  1917. l2->addr.A = 3;
  1918. l2->addr.B = 1;
  1919. break;
  1920. default:
  1921. printk(KERN_ERR "layer2 create failed prt %x\n",
  1922. protocol);
  1923. kfree(l2);
  1924. return NULL;
  1925. }
  1926. skb_queue_head_init(&l2->i_queue);
  1927. skb_queue_head_init(&l2->ui_queue);
  1928. skb_queue_head_init(&l2->down_queue);
  1929. skb_queue_head_init(&l2->tmp_queue);
  1930. InitWin(l2);
  1931. l2->l2m.fsm = &l2fsm;
  1932. if (test_bit(FLG_LAPB, &l2->flag) ||
  1933. test_bit(FLG_PTP, &l2->flag) ||
  1934. test_bit(FLG_LAPD_NET, &l2->flag))
  1935. l2->l2m.state = ST_L2_4;
  1936. else
  1937. l2->l2m.state = ST_L2_1;
  1938. l2->l2m.debug = *debug;
  1939. l2->l2m.userdata = l2;
  1940. l2->l2m.userint = 0;
  1941. l2->l2m.printdebug = l2m_debug;
  1942. mISDN_FsmInitTimer(&l2->l2m, &l2->t200);
  1943. mISDN_FsmInitTimer(&l2->l2m, &l2->t203);
  1944. return l2;
  1945. }
  1946. static int
  1947. x75create(struct channel_req *crq)
  1948. {
  1949. struct layer2 *l2;
  1950. if (crq->protocol != ISDN_P_B_X75SLP)
  1951. return -EPROTONOSUPPORT;
  1952. l2 = create_l2(crq->ch, crq->protocol, 0, 0, 0);
  1953. if (!l2)
  1954. return -ENOMEM;
  1955. crq->ch = &l2->ch;
  1956. crq->protocol = ISDN_P_B_HDLC;
  1957. return 0;
  1958. }
  1959. static struct Bprotocol X75SLP = {
  1960. .Bprotocols = (1 << (ISDN_P_B_X75SLP & ISDN_P_B_MASK)),
  1961. .name = "X75SLP",
  1962. .create = x75create
  1963. };
  1964. int
  1965. Isdnl2_Init(u_int *deb)
  1966. {
  1967. debug = deb;
  1968. mISDN_register_Bprotocol(&X75SLP);
  1969. l2fsm.state_count = L2_STATE_COUNT;
  1970. l2fsm.event_count = L2_EVENT_COUNT;
  1971. l2fsm.strEvent = strL2Event;
  1972. l2fsm.strState = strL2State;
  1973. mISDN_FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
  1974. TEIInit(deb);
  1975. return 0;
  1976. }
  1977. void
  1978. Isdnl2_cleanup(void)
  1979. {
  1980. mISDN_unregister_Bprotocol(&X75SLP);
  1981. TEIFree();
  1982. mISDN_FsmFree(&l2fsm);
  1983. }