layer2.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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, u_long arg)
  1846. {
  1847. struct layer2 *l2;
  1848. struct channel_req rq;
  1849. l2 = kzalloc(sizeof(struct layer2), GFP_KERNEL);
  1850. if (!l2) {
  1851. printk(KERN_ERR "kzalloc layer2 failed\n");
  1852. return NULL;
  1853. }
  1854. l2->next_id = 1;
  1855. l2->down_id = MISDN_ID_NONE;
  1856. l2->up = ch;
  1857. l2->ch.st = ch->st;
  1858. l2->ch.send = l2_send;
  1859. l2->ch.ctrl = l2_ctrl;
  1860. switch (protocol) {
  1861. case ISDN_P_LAPD_NT:
  1862. test_and_set_bit(FLG_LAPD, &l2->flag);
  1863. test_and_set_bit(FLG_LAPD_NET, &l2->flag);
  1864. test_and_set_bit(FLG_MOD128, &l2->flag);
  1865. l2->sapi = 0;
  1866. l2->maxlen = MAX_DFRAME_LEN;
  1867. if (test_bit(OPTION_L2_PMX, &options))
  1868. l2->window = 7;
  1869. else
  1870. l2->window = 1;
  1871. if (test_bit(OPTION_L2_PTP, &options))
  1872. test_and_set_bit(FLG_PTP, &l2->flag);
  1873. if (test_bit(OPTION_L2_FIXEDTEI, &options))
  1874. test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
  1875. l2->tei = (u_int)arg;
  1876. l2->T200 = 1000;
  1877. l2->N200 = 3;
  1878. l2->T203 = 10000;
  1879. if (test_bit(OPTION_L2_PMX, &options))
  1880. rq.protocol = ISDN_P_NT_E1;
  1881. else
  1882. rq.protocol = ISDN_P_NT_S0;
  1883. rq.adr.channel = 0;
  1884. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
  1885. break;
  1886. case ISDN_P_LAPD_TE:
  1887. test_and_set_bit(FLG_LAPD, &l2->flag);
  1888. test_and_set_bit(FLG_MOD128, &l2->flag);
  1889. test_and_set_bit(FLG_ORIG, &l2->flag);
  1890. l2->sapi = 0;
  1891. l2->maxlen = MAX_DFRAME_LEN;
  1892. if (test_bit(OPTION_L2_PMX, &options))
  1893. l2->window = 7;
  1894. else
  1895. l2->window = 1;
  1896. if (test_bit(OPTION_L2_PTP, &options))
  1897. test_and_set_bit(FLG_PTP, &l2->flag);
  1898. if (test_bit(OPTION_L2_FIXEDTEI, &options))
  1899. test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
  1900. l2->tei = (u_int)arg;
  1901. l2->T200 = 1000;
  1902. l2->N200 = 3;
  1903. l2->T203 = 10000;
  1904. if (test_bit(OPTION_L2_PMX, &options))
  1905. rq.protocol = ISDN_P_TE_E1;
  1906. else
  1907. rq.protocol = ISDN_P_TE_S0;
  1908. rq.adr.channel = 0;
  1909. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
  1910. break;
  1911. case ISDN_P_B_X75SLP:
  1912. test_and_set_bit(FLG_LAPB, &l2->flag);
  1913. l2->window = 7;
  1914. l2->maxlen = MAX_DATA_SIZE;
  1915. l2->T200 = 1000;
  1916. l2->N200 = 4;
  1917. l2->T203 = 5000;
  1918. l2->addr.A = 3;
  1919. l2->addr.B = 1;
  1920. break;
  1921. default:
  1922. printk(KERN_ERR "layer2 create failed prt %x\n",
  1923. protocol);
  1924. kfree(l2);
  1925. return NULL;
  1926. }
  1927. skb_queue_head_init(&l2->i_queue);
  1928. skb_queue_head_init(&l2->ui_queue);
  1929. skb_queue_head_init(&l2->down_queue);
  1930. skb_queue_head_init(&l2->tmp_queue);
  1931. InitWin(l2);
  1932. l2->l2m.fsm = &l2fsm;
  1933. if (test_bit(FLG_LAPB, &l2->flag) ||
  1934. test_bit(FLG_PTP, &l2->flag) ||
  1935. test_bit(FLG_LAPD_NET, &l2->flag))
  1936. l2->l2m.state = ST_L2_4;
  1937. else
  1938. l2->l2m.state = ST_L2_1;
  1939. l2->l2m.debug = *debug;
  1940. l2->l2m.userdata = l2;
  1941. l2->l2m.userint = 0;
  1942. l2->l2m.printdebug = l2m_debug;
  1943. mISDN_FsmInitTimer(&l2->l2m, &l2->t200);
  1944. mISDN_FsmInitTimer(&l2->l2m, &l2->t203);
  1945. return l2;
  1946. }
  1947. static int
  1948. x75create(struct channel_req *crq)
  1949. {
  1950. struct layer2 *l2;
  1951. if (crq->protocol != ISDN_P_B_X75SLP)
  1952. return -EPROTONOSUPPORT;
  1953. l2 = create_l2(crq->ch, crq->protocol, 0, 0);
  1954. if (!l2)
  1955. return -ENOMEM;
  1956. crq->ch = &l2->ch;
  1957. crq->protocol = ISDN_P_B_HDLC;
  1958. return 0;
  1959. }
  1960. static struct Bprotocol X75SLP = {
  1961. .Bprotocols = (1 << (ISDN_P_B_X75SLP & ISDN_P_B_MASK)),
  1962. .name = "X75SLP",
  1963. .create = x75create
  1964. };
  1965. int
  1966. Isdnl2_Init(u_int *deb)
  1967. {
  1968. debug = deb;
  1969. mISDN_register_Bprotocol(&X75SLP);
  1970. l2fsm.state_count = L2_STATE_COUNT;
  1971. l2fsm.event_count = L2_EVENT_COUNT;
  1972. l2fsm.strEvent = strL2Event;
  1973. l2fsm.strState = strL2State;
  1974. mISDN_FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
  1975. TEIInit(deb);
  1976. return 0;
  1977. }
  1978. void
  1979. Isdnl2_cleanup(void)
  1980. {
  1981. mISDN_unregister_Bprotocol(&X75SLP);
  1982. TEIFree();
  1983. mISDN_FsmFree(&l2fsm);
  1984. }