hdlc_fr.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * Frame Relay support
  4. *
  5. * Copyright (C) 1999 - 2003 Krzysztof Halasa <khc@pm.waw.pl>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of version 2 of the GNU General Public License
  9. * as published by the Free Software Foundation.
  10. *
  11. Theory of PVC state
  12. DCE mode:
  13. (exist,new) -> 0,0 when "PVC create" or if "link unreliable"
  14. 0,x -> 1,1 if "link reliable" when sending FULL STATUS
  15. 1,1 -> 1,0 if received FULL STATUS ACK
  16. (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create"
  17. -> 1 when "PVC up" and (exist,new) = 1,0
  18. DTE mode:
  19. (exist,new,active) = FULL STATUS if "link reliable"
  20. = 0, 0, 0 if "link unreliable"
  21. No LMI:
  22. active = open and "link reliable"
  23. exist = new = not used
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/poll.h>
  29. #include <linux/errno.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/init.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/pkt_sched.h>
  34. #include <linux/random.h>
  35. #include <linux/inetdevice.h>
  36. #include <linux/lapb.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/hdlc.h>
  40. #undef DEBUG_PKT
  41. #undef DEBUG_ECN
  42. #undef DEBUG_LINK
  43. #define MAXLEN_LMISTAT 20 /* max size of status enquiry frame */
  44. #define PVC_STATE_NEW 0x01
  45. #define PVC_STATE_ACTIVE 0x02
  46. #define PVC_STATE_FECN 0x08 /* FECN condition */
  47. #define PVC_STATE_BECN 0x10 /* BECN condition */
  48. #define FR_UI 0x03
  49. #define FR_PAD 0x00
  50. #define NLPID_IP 0xCC
  51. #define NLPID_IPV6 0x8E
  52. #define NLPID_SNAP 0x80
  53. #define NLPID_PAD 0x00
  54. #define NLPID_Q933 0x08
  55. #define LMI_DLCI 0 /* LMI DLCI */
  56. #define LMI_PROTO 0x08
  57. #define LMI_CALLREF 0x00 /* Call Reference */
  58. #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI lockshift */
  59. #define LMI_REPTYPE 1 /* report type */
  60. #define LMI_CCITT_REPTYPE 0x51
  61. #define LMI_ALIVE 3 /* keep alive */
  62. #define LMI_CCITT_ALIVE 0x53
  63. #define LMI_PVCSTAT 7 /* pvc status */
  64. #define LMI_CCITT_PVCSTAT 0x57
  65. #define LMI_FULLREP 0 /* full report */
  66. #define LMI_INTEGRITY 1 /* link integrity report */
  67. #define LMI_SINGLE 2 /* single pvc report */
  68. #define LMI_STATUS_ENQUIRY 0x75
  69. #define LMI_STATUS 0x7D /* reply */
  70. #define LMI_REPT_LEN 1 /* report type element length */
  71. #define LMI_INTEG_LEN 2 /* link integrity element length */
  72. #define LMI_LENGTH 13 /* standard LMI frame length */
  73. #define LMI_ANSI_LENGTH 14
  74. typedef struct {
  75. #if defined(__LITTLE_ENDIAN_BITFIELD)
  76. unsigned ea1: 1;
  77. unsigned cr: 1;
  78. unsigned dlcih: 6;
  79. unsigned ea2: 1;
  80. unsigned de: 1;
  81. unsigned becn: 1;
  82. unsigned fecn: 1;
  83. unsigned dlcil: 4;
  84. #else
  85. unsigned dlcih: 6;
  86. unsigned cr: 1;
  87. unsigned ea1: 1;
  88. unsigned dlcil: 4;
  89. unsigned fecn: 1;
  90. unsigned becn: 1;
  91. unsigned de: 1;
  92. unsigned ea2: 1;
  93. #endif
  94. }__attribute__ ((packed)) fr_hdr;
  95. static inline u16 q922_to_dlci(u8 *hdr)
  96. {
  97. return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
  98. }
  99. static inline void dlci_to_q922(u8 *hdr, u16 dlci)
  100. {
  101. hdr[0] = (dlci >> 2) & 0xFC;
  102. hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
  103. }
  104. static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
  105. {
  106. pvc_device *pvc = hdlc->state.fr.first_pvc;
  107. while (pvc) {
  108. if (pvc->dlci == dlci)
  109. return pvc;
  110. if (pvc->dlci > dlci)
  111. return NULL; /* the listed is sorted */
  112. pvc = pvc->next;
  113. }
  114. return NULL;
  115. }
  116. static inline pvc_device* add_pvc(struct net_device *dev, u16 dlci)
  117. {
  118. hdlc_device *hdlc = dev_to_hdlc(dev);
  119. pvc_device *pvc, **pvc_p = &hdlc->state.fr.first_pvc;
  120. while (*pvc_p) {
  121. if ((*pvc_p)->dlci == dlci)
  122. return *pvc_p;
  123. if ((*pvc_p)->dlci > dlci)
  124. break; /* the list is sorted */
  125. pvc_p = &(*pvc_p)->next;
  126. }
  127. pvc = kmalloc(sizeof(pvc_device), GFP_ATOMIC);
  128. if (!pvc)
  129. return NULL;
  130. memset(pvc, 0, sizeof(pvc_device));
  131. pvc->dlci = dlci;
  132. pvc->master = dev;
  133. pvc->next = *pvc_p; /* Put it in the chain */
  134. *pvc_p = pvc;
  135. return pvc;
  136. }
  137. static inline int pvc_is_used(pvc_device *pvc)
  138. {
  139. return pvc->main != NULL || pvc->ether != NULL;
  140. }
  141. static inline void pvc_carrier(int on, pvc_device *pvc)
  142. {
  143. if (on) {
  144. if (pvc->main)
  145. if (!netif_carrier_ok(pvc->main))
  146. netif_carrier_on(pvc->main);
  147. if (pvc->ether)
  148. if (!netif_carrier_ok(pvc->ether))
  149. netif_carrier_on(pvc->ether);
  150. } else {
  151. if (pvc->main)
  152. if (netif_carrier_ok(pvc->main))
  153. netif_carrier_off(pvc->main);
  154. if (pvc->ether)
  155. if (netif_carrier_ok(pvc->ether))
  156. netif_carrier_off(pvc->ether);
  157. }
  158. }
  159. static inline void delete_unused_pvcs(hdlc_device *hdlc)
  160. {
  161. pvc_device **pvc_p = &hdlc->state.fr.first_pvc;
  162. while (*pvc_p) {
  163. if (!pvc_is_used(*pvc_p)) {
  164. pvc_device *pvc = *pvc_p;
  165. *pvc_p = pvc->next;
  166. kfree(pvc);
  167. continue;
  168. }
  169. pvc_p = &(*pvc_p)->next;
  170. }
  171. }
  172. static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
  173. {
  174. if (type == ARPHRD_ETHER)
  175. return &pvc->ether;
  176. else
  177. return &pvc->main;
  178. }
  179. static inline u16 status_to_dlci(u8 *status, int *active, int *new)
  180. {
  181. *new = (status[2] & 0x08) ? 1 : 0;
  182. *active = (status[2] & 0x02) ? 1 : 0;
  183. return ((status[0] & 0x3F) << 4) | ((status[1] & 0x78) >> 3);
  184. }
  185. static inline void dlci_to_status(u16 dlci, u8 *status, int active, int new)
  186. {
  187. status[0] = (dlci >> 4) & 0x3F;
  188. status[1] = ((dlci << 3) & 0x78) | 0x80;
  189. status[2] = 0x80;
  190. if (new)
  191. status[2] |= 0x08;
  192. else if (active)
  193. status[2] |= 0x02;
  194. }
  195. static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
  196. {
  197. u16 head_len;
  198. struct sk_buff *skb = *skb_p;
  199. switch (skb->protocol) {
  200. case __constant_ntohs(ETH_P_IP):
  201. head_len = 4;
  202. skb_push(skb, head_len);
  203. skb->data[3] = NLPID_IP;
  204. break;
  205. case __constant_ntohs(ETH_P_IPV6):
  206. head_len = 4;
  207. skb_push(skb, head_len);
  208. skb->data[3] = NLPID_IPV6;
  209. break;
  210. case __constant_ntohs(LMI_PROTO):
  211. head_len = 4;
  212. skb_push(skb, head_len);
  213. skb->data[3] = LMI_PROTO;
  214. break;
  215. case __constant_ntohs(ETH_P_802_3):
  216. head_len = 10;
  217. if (skb_headroom(skb) < head_len) {
  218. struct sk_buff *skb2 = skb_realloc_headroom(skb,
  219. head_len);
  220. if (!skb2)
  221. return -ENOBUFS;
  222. dev_kfree_skb(skb);
  223. skb = *skb_p = skb2;
  224. }
  225. skb_push(skb, head_len);
  226. skb->data[3] = FR_PAD;
  227. skb->data[4] = NLPID_SNAP;
  228. skb->data[5] = FR_PAD;
  229. skb->data[6] = 0x80;
  230. skb->data[7] = 0xC2;
  231. skb->data[8] = 0x00;
  232. skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
  233. break;
  234. default:
  235. head_len = 10;
  236. skb_push(skb, head_len);
  237. skb->data[3] = FR_PAD;
  238. skb->data[4] = NLPID_SNAP;
  239. skb->data[5] = FR_PAD;
  240. skb->data[6] = FR_PAD;
  241. skb->data[7] = FR_PAD;
  242. *(u16*)(skb->data + 8) = skb->protocol;
  243. }
  244. dlci_to_q922(skb->data, dlci);
  245. skb->data[2] = FR_UI;
  246. return 0;
  247. }
  248. static int pvc_open(struct net_device *dev)
  249. {
  250. pvc_device *pvc = dev_to_pvc(dev);
  251. if ((pvc->master->flags & IFF_UP) == 0)
  252. return -EIO; /* Master must be UP in order to activate PVC */
  253. if (pvc->open_count++ == 0) {
  254. hdlc_device *hdlc = dev_to_hdlc(pvc->master);
  255. if (hdlc->state.fr.settings.lmi == LMI_NONE)
  256. pvc->state.active = hdlc->carrier;
  257. pvc_carrier(pvc->state.active, pvc);
  258. hdlc->state.fr.dce_changed = 1;
  259. }
  260. return 0;
  261. }
  262. static int pvc_close(struct net_device *dev)
  263. {
  264. pvc_device *pvc = dev_to_pvc(dev);
  265. if (--pvc->open_count == 0) {
  266. hdlc_device *hdlc = dev_to_hdlc(pvc->master);
  267. if (hdlc->state.fr.settings.lmi == LMI_NONE)
  268. pvc->state.active = 0;
  269. if (hdlc->state.fr.settings.dce) {
  270. hdlc->state.fr.dce_changed = 1;
  271. pvc->state.active = 0;
  272. }
  273. }
  274. return 0;
  275. }
  276. int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  277. {
  278. pvc_device *pvc = dev_to_pvc(dev);
  279. fr_proto_pvc_info info;
  280. if (ifr->ifr_settings.type == IF_GET_PROTO) {
  281. if (dev->type == ARPHRD_ETHER)
  282. ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
  283. else
  284. ifr->ifr_settings.type = IF_PROTO_FR_PVC;
  285. if (ifr->ifr_settings.size < sizeof(info)) {
  286. /* data size wanted */
  287. ifr->ifr_settings.size = sizeof(info);
  288. return -ENOBUFS;
  289. }
  290. info.dlci = pvc->dlci;
  291. memcpy(info.master, pvc->master->name, IFNAMSIZ);
  292. if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
  293. &info, sizeof(info)))
  294. return -EFAULT;
  295. return 0;
  296. }
  297. return -EINVAL;
  298. }
  299. static inline struct net_device_stats *pvc_get_stats(struct net_device *dev)
  300. {
  301. return netdev_priv(dev);
  302. }
  303. static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
  304. {
  305. pvc_device *pvc = dev_to_pvc(dev);
  306. struct net_device_stats *stats = pvc_get_stats(dev);
  307. if (pvc->state.active) {
  308. if (dev->type == ARPHRD_ETHER) {
  309. int pad = ETH_ZLEN - skb->len;
  310. if (pad > 0) { /* Pad the frame with zeros */
  311. int len = skb->len;
  312. if (skb_tailroom(skb) < pad)
  313. if (pskb_expand_head(skb, 0, pad,
  314. GFP_ATOMIC)) {
  315. stats->tx_dropped++;
  316. dev_kfree_skb(skb);
  317. return 0;
  318. }
  319. skb_put(skb, pad);
  320. memset(skb->data + len, 0, pad);
  321. }
  322. skb->protocol = __constant_htons(ETH_P_802_3);
  323. }
  324. if (!fr_hard_header(&skb, pvc->dlci)) {
  325. stats->tx_bytes += skb->len;
  326. stats->tx_packets++;
  327. if (pvc->state.fecn) /* TX Congestion counter */
  328. stats->tx_compressed++;
  329. skb->dev = pvc->master;
  330. dev_queue_xmit(skb);
  331. return 0;
  332. }
  333. }
  334. stats->tx_dropped++;
  335. dev_kfree_skb(skb);
  336. return 0;
  337. }
  338. static int pvc_change_mtu(struct net_device *dev, int new_mtu)
  339. {
  340. if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
  341. return -EINVAL;
  342. dev->mtu = new_mtu;
  343. return 0;
  344. }
  345. static inline void fr_log_dlci_active(pvc_device *pvc)
  346. {
  347. printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
  348. pvc->master->name,
  349. pvc->dlci,
  350. pvc->main ? pvc->main->name : "",
  351. pvc->main && pvc->ether ? " " : "",
  352. pvc->ether ? pvc->ether->name : "",
  353. pvc->state.new ? " new" : "",
  354. !pvc->state.exist ? "deleted" :
  355. pvc->state.active ? "active" : "inactive");
  356. }
  357. static inline u8 fr_lmi_nextseq(u8 x)
  358. {
  359. x++;
  360. return x ? x : 1;
  361. }
  362. static void fr_lmi_send(struct net_device *dev, int fullrep)
  363. {
  364. hdlc_device *hdlc = dev_to_hdlc(dev);
  365. struct sk_buff *skb;
  366. pvc_device *pvc = hdlc->state.fr.first_pvc;
  367. int len = (hdlc->state.fr.settings.lmi == LMI_ANSI) ? LMI_ANSI_LENGTH
  368. : LMI_LENGTH;
  369. int stat_len = 3;
  370. u8 *data;
  371. int i = 0;
  372. if (hdlc->state.fr.settings.dce && fullrep) {
  373. len += hdlc->state.fr.dce_pvc_count * (2 + stat_len);
  374. if (len > HDLC_MAX_MRU) {
  375. printk(KERN_WARNING "%s: Too many PVCs while sending "
  376. "LMI full report\n", dev->name);
  377. return;
  378. }
  379. }
  380. skb = dev_alloc_skb(len);
  381. if (!skb) {
  382. printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
  383. dev->name);
  384. return;
  385. }
  386. memset(skb->data, 0, len);
  387. skb_reserve(skb, 4);
  388. skb->protocol = __constant_htons(LMI_PROTO);
  389. fr_hard_header(&skb, LMI_DLCI);
  390. data = skb->tail;
  391. data[i++] = LMI_CALLREF;
  392. data[i++] = hdlc->state.fr.settings.dce
  393. ? LMI_STATUS : LMI_STATUS_ENQUIRY;
  394. if (hdlc->state.fr.settings.lmi == LMI_ANSI)
  395. data[i++] = LMI_ANSI_LOCKSHIFT;
  396. data[i++] = (hdlc->state.fr.settings.lmi == LMI_CCITT)
  397. ? LMI_CCITT_REPTYPE : LMI_REPTYPE;
  398. data[i++] = LMI_REPT_LEN;
  399. data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
  400. data[i++] = (hdlc->state.fr.settings.lmi == LMI_CCITT)
  401. ? LMI_CCITT_ALIVE : LMI_ALIVE;
  402. data[i++] = LMI_INTEG_LEN;
  403. data[i++] = hdlc->state.fr.txseq =fr_lmi_nextseq(hdlc->state.fr.txseq);
  404. data[i++] = hdlc->state.fr.rxseq;
  405. if (hdlc->state.fr.settings.dce && fullrep) {
  406. while (pvc) {
  407. data[i++] = (hdlc->state.fr.settings.lmi == LMI_CCITT)
  408. ? LMI_CCITT_PVCSTAT : LMI_PVCSTAT;
  409. data[i++] = stat_len;
  410. /* LMI start/restart */
  411. if (hdlc->state.fr.reliable && !pvc->state.exist) {
  412. pvc->state.exist = pvc->state.new = 1;
  413. fr_log_dlci_active(pvc);
  414. }
  415. /* ifconfig PVC up */
  416. if (pvc->open_count && !pvc->state.active &&
  417. pvc->state.exist && !pvc->state.new) {
  418. pvc_carrier(1, pvc);
  419. pvc->state.active = 1;
  420. fr_log_dlci_active(pvc);
  421. }
  422. dlci_to_status(pvc->dlci, data + i,
  423. pvc->state.active, pvc->state.new);
  424. i += stat_len;
  425. pvc = pvc->next;
  426. }
  427. }
  428. skb_put(skb, i);
  429. skb->priority = TC_PRIO_CONTROL;
  430. skb->dev = dev;
  431. skb->nh.raw = skb->data;
  432. dev_queue_xmit(skb);
  433. }
  434. static void fr_set_link_state(int reliable, struct net_device *dev)
  435. {
  436. hdlc_device *hdlc = dev_to_hdlc(dev);
  437. pvc_device *pvc = hdlc->state.fr.first_pvc;
  438. hdlc->state.fr.reliable = reliable;
  439. if (reliable) {
  440. if (!netif_carrier_ok(dev))
  441. netif_carrier_on(dev);
  442. hdlc->state.fr.n391cnt = 0; /* Request full status */
  443. hdlc->state.fr.dce_changed = 1;
  444. if (hdlc->state.fr.settings.lmi == LMI_NONE) {
  445. while (pvc) { /* Activate all PVCs */
  446. pvc_carrier(1, pvc);
  447. pvc->state.exist = pvc->state.active = 1;
  448. pvc->state.new = 0;
  449. pvc = pvc->next;
  450. }
  451. }
  452. } else {
  453. if (netif_carrier_ok(dev))
  454. netif_carrier_off(dev);
  455. while (pvc) { /* Deactivate all PVCs */
  456. pvc_carrier(0, pvc);
  457. pvc->state.exist = pvc->state.active = 0;
  458. pvc->state.new = 0;
  459. pvc = pvc->next;
  460. }
  461. }
  462. }
  463. static void fr_timer(unsigned long arg)
  464. {
  465. struct net_device *dev = (struct net_device *)arg;
  466. hdlc_device *hdlc = dev_to_hdlc(dev);
  467. int i, cnt = 0, reliable;
  468. u32 list;
  469. if (hdlc->state.fr.settings.dce)
  470. reliable = hdlc->state.fr.request &&
  471. time_before(jiffies, hdlc->state.fr.last_poll +
  472. hdlc->state.fr.settings.t392 * HZ);
  473. else {
  474. hdlc->state.fr.last_errors <<= 1; /* Shift the list */
  475. if (hdlc->state.fr.request) {
  476. if (hdlc->state.fr.reliable)
  477. printk(KERN_INFO "%s: No LMI status reply "
  478. "received\n", dev->name);
  479. hdlc->state.fr.last_errors |= 1;
  480. }
  481. list = hdlc->state.fr.last_errors;
  482. for (i = 0; i < hdlc->state.fr.settings.n393; i++, list >>= 1)
  483. cnt += (list & 1); /* errors count */
  484. reliable = (cnt < hdlc->state.fr.settings.n392);
  485. }
  486. if (hdlc->state.fr.reliable != reliable) {
  487. printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
  488. reliable ? "" : "un");
  489. fr_set_link_state(reliable, dev);
  490. }
  491. if (hdlc->state.fr.settings.dce)
  492. hdlc->state.fr.timer.expires = jiffies +
  493. hdlc->state.fr.settings.t392 * HZ;
  494. else {
  495. if (hdlc->state.fr.n391cnt)
  496. hdlc->state.fr.n391cnt--;
  497. fr_lmi_send(dev, hdlc->state.fr.n391cnt == 0);
  498. hdlc->state.fr.last_poll = jiffies;
  499. hdlc->state.fr.request = 1;
  500. hdlc->state.fr.timer.expires = jiffies +
  501. hdlc->state.fr.settings.t391 * HZ;
  502. }
  503. hdlc->state.fr.timer.function = fr_timer;
  504. hdlc->state.fr.timer.data = arg;
  505. add_timer(&hdlc->state.fr.timer);
  506. }
  507. static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
  508. {
  509. hdlc_device *hdlc = dev_to_hdlc(dev);
  510. int stat_len;
  511. pvc_device *pvc;
  512. int reptype = -1, error, no_ram;
  513. u8 rxseq, txseq;
  514. int i;
  515. if (skb->len < ((hdlc->state.fr.settings.lmi == LMI_ANSI)
  516. ? LMI_ANSI_LENGTH : LMI_LENGTH)) {
  517. printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
  518. return 1;
  519. }
  520. if (skb->data[5] != (!hdlc->state.fr.settings.dce ?
  521. LMI_STATUS : LMI_STATUS_ENQUIRY)) {
  522. printk(KERN_INFO "%s: LMI msgtype=%x, Not LMI status %s\n",
  523. dev->name, skb->data[2],
  524. hdlc->state.fr.settings.dce ? "enquiry" : "reply");
  525. return 1;
  526. }
  527. i = (hdlc->state.fr.settings.lmi == LMI_ANSI) ? 7 : 6;
  528. if (skb->data[i] !=
  529. ((hdlc->state.fr.settings.lmi == LMI_CCITT)
  530. ? LMI_CCITT_REPTYPE : LMI_REPTYPE)) {
  531. printk(KERN_INFO "%s: Not a report type=%x\n",
  532. dev->name, skb->data[i]);
  533. return 1;
  534. }
  535. i++;
  536. i++; /* Skip length field */
  537. reptype = skb->data[i++];
  538. if (skb->data[i]!=
  539. ((hdlc->state.fr.settings.lmi == LMI_CCITT)
  540. ? LMI_CCITT_ALIVE : LMI_ALIVE)) {
  541. printk(KERN_INFO "%s: Unsupported status element=%x\n",
  542. dev->name, skb->data[i]);
  543. return 1;
  544. }
  545. i++;
  546. i++; /* Skip length field */
  547. hdlc->state.fr.rxseq = skb->data[i++]; /* TX sequence from peer */
  548. rxseq = skb->data[i++]; /* Should confirm our sequence */
  549. txseq = hdlc->state.fr.txseq;
  550. if (hdlc->state.fr.settings.dce) {
  551. if (reptype != LMI_FULLREP && reptype != LMI_INTEGRITY) {
  552. printk(KERN_INFO "%s: Unsupported report type=%x\n",
  553. dev->name, reptype);
  554. return 1;
  555. }
  556. hdlc->state.fr.last_poll = jiffies;
  557. }
  558. error = 0;
  559. if (!hdlc->state.fr.reliable)
  560. error = 1;
  561. if (rxseq == 0 || rxseq != txseq) {
  562. hdlc->state.fr.n391cnt = 0; /* Ask for full report next time */
  563. error = 1;
  564. }
  565. if (hdlc->state.fr.settings.dce) {
  566. if (hdlc->state.fr.fullrep_sent && !error) {
  567. /* Stop sending full report - the last one has been confirmed by DTE */
  568. hdlc->state.fr.fullrep_sent = 0;
  569. pvc = hdlc->state.fr.first_pvc;
  570. while (pvc) {
  571. if (pvc->state.new) {
  572. pvc->state.new = 0;
  573. /* Tell DTE that new PVC is now active */
  574. hdlc->state.fr.dce_changed = 1;
  575. }
  576. pvc = pvc->next;
  577. }
  578. }
  579. if (hdlc->state.fr.dce_changed) {
  580. reptype = LMI_FULLREP;
  581. hdlc->state.fr.fullrep_sent = 1;
  582. hdlc->state.fr.dce_changed = 0;
  583. }
  584. fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
  585. return 0;
  586. }
  587. /* DTE */
  588. hdlc->state.fr.request = 0; /* got response, no request pending */
  589. if (error)
  590. return 0;
  591. if (reptype != LMI_FULLREP)
  592. return 0;
  593. stat_len = 3;
  594. pvc = hdlc->state.fr.first_pvc;
  595. while (pvc) {
  596. pvc->state.deleted = 1;
  597. pvc = pvc->next;
  598. }
  599. no_ram = 0;
  600. while (skb->len >= i + 2 + stat_len) {
  601. u16 dlci;
  602. unsigned int active, new;
  603. if (skb->data[i] != ((hdlc->state.fr.settings.lmi == LMI_CCITT)
  604. ? LMI_CCITT_PVCSTAT : LMI_PVCSTAT)) {
  605. printk(KERN_WARNING "%s: Invalid PVCSTAT ID: %x\n",
  606. dev->name, skb->data[i]);
  607. return 1;
  608. }
  609. i++;
  610. if (skb->data[i] != stat_len) {
  611. printk(KERN_WARNING "%s: Invalid PVCSTAT length: %x\n",
  612. dev->name, skb->data[i]);
  613. return 1;
  614. }
  615. i++;
  616. dlci = status_to_dlci(skb->data + i, &active, &new);
  617. pvc = add_pvc(dev, dlci);
  618. if (!pvc && !no_ram) {
  619. printk(KERN_WARNING
  620. "%s: Memory squeeze on fr_lmi_recv()\n",
  621. dev->name);
  622. no_ram = 1;
  623. }
  624. if (pvc) {
  625. pvc->state.exist = 1;
  626. pvc->state.deleted = 0;
  627. if (active != pvc->state.active ||
  628. new != pvc->state.new ||
  629. !pvc->state.exist) {
  630. pvc->state.new = new;
  631. pvc->state.active = active;
  632. pvc_carrier(active, pvc);
  633. fr_log_dlci_active(pvc);
  634. }
  635. }
  636. i += stat_len;
  637. }
  638. pvc = hdlc->state.fr.first_pvc;
  639. while (pvc) {
  640. if (pvc->state.deleted && pvc->state.exist) {
  641. pvc_carrier(0, pvc);
  642. pvc->state.active = pvc->state.new = 0;
  643. pvc->state.exist = 0;
  644. fr_log_dlci_active(pvc);
  645. }
  646. pvc = pvc->next;
  647. }
  648. /* Next full report after N391 polls */
  649. hdlc->state.fr.n391cnt = hdlc->state.fr.settings.n391;
  650. return 0;
  651. }
  652. static int fr_rx(struct sk_buff *skb)
  653. {
  654. struct net_device *ndev = skb->dev;
  655. hdlc_device *hdlc = dev_to_hdlc(ndev);
  656. fr_hdr *fh = (fr_hdr*)skb->data;
  657. u8 *data = skb->data;
  658. u16 dlci;
  659. pvc_device *pvc;
  660. struct net_device *dev = NULL;
  661. if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
  662. goto rx_error;
  663. dlci = q922_to_dlci(skb->data);
  664. if (dlci == LMI_DLCI) {
  665. if (hdlc->state.fr.settings.lmi == LMI_NONE)
  666. goto rx_error; /* LMI packet with no LMI? */
  667. if (data[3] == LMI_PROTO) {
  668. if (fr_lmi_recv(ndev, skb))
  669. goto rx_error;
  670. else {
  671. dev_kfree_skb_any(skb);
  672. return NET_RX_SUCCESS;
  673. }
  674. }
  675. printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
  676. ndev->name);
  677. goto rx_error;
  678. }
  679. pvc = find_pvc(hdlc, dlci);
  680. if (!pvc) {
  681. #ifdef DEBUG_PKT
  682. printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
  683. ndev->name, dlci);
  684. #endif
  685. dev_kfree_skb_any(skb);
  686. return NET_RX_DROP;
  687. }
  688. if (pvc->state.fecn != fh->fecn) {
  689. #ifdef DEBUG_ECN
  690. printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", ndev->name,
  691. dlci, fh->fecn ? "N" : "FF");
  692. #endif
  693. pvc->state.fecn ^= 1;
  694. }
  695. if (pvc->state.becn != fh->becn) {
  696. #ifdef DEBUG_ECN
  697. printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", ndev->name,
  698. dlci, fh->becn ? "N" : "FF");
  699. #endif
  700. pvc->state.becn ^= 1;
  701. }
  702. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  703. hdlc->stats.rx_dropped++;
  704. return NET_RX_DROP;
  705. }
  706. if (data[3] == NLPID_IP) {
  707. skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
  708. dev = pvc->main;
  709. skb->protocol = htons(ETH_P_IP);
  710. } else if (data[3] == NLPID_IPV6) {
  711. skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
  712. dev = pvc->main;
  713. skb->protocol = htons(ETH_P_IPV6);
  714. } else if (skb->len > 10 && data[3] == FR_PAD &&
  715. data[4] == NLPID_SNAP && data[5] == FR_PAD) {
  716. u16 oui = ntohs(*(u16*)(data + 6));
  717. u16 pid = ntohs(*(u16*)(data + 8));
  718. skb_pull(skb, 10);
  719. switch ((((u32)oui) << 16) | pid) {
  720. case ETH_P_ARP: /* routed frame with SNAP */
  721. case ETH_P_IPX:
  722. case ETH_P_IP: /* a long variant */
  723. case ETH_P_IPV6:
  724. dev = pvc->main;
  725. skb->protocol = htons(pid);
  726. break;
  727. case 0x80C20007: /* bridged Ethernet frame */
  728. if ((dev = pvc->ether) != NULL)
  729. skb->protocol = eth_type_trans(skb, dev);
  730. break;
  731. default:
  732. printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
  733. "PID=%x\n", ndev->name, oui, pid);
  734. dev_kfree_skb_any(skb);
  735. return NET_RX_DROP;
  736. }
  737. } else {
  738. printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
  739. "length = %i\n", ndev->name, data[3], skb->len);
  740. dev_kfree_skb_any(skb);
  741. return NET_RX_DROP;
  742. }
  743. if (dev) {
  744. struct net_device_stats *stats = pvc_get_stats(dev);
  745. stats->rx_packets++; /* PVC traffic */
  746. stats->rx_bytes += skb->len;
  747. if (pvc->state.becn)
  748. stats->rx_compressed++;
  749. skb->dev = dev;
  750. netif_rx(skb);
  751. return NET_RX_SUCCESS;
  752. } else {
  753. dev_kfree_skb_any(skb);
  754. return NET_RX_DROP;
  755. }
  756. rx_error:
  757. hdlc->stats.rx_errors++; /* Mark error */
  758. dev_kfree_skb_any(skb);
  759. return NET_RX_DROP;
  760. }
  761. static void fr_start(struct net_device *dev)
  762. {
  763. hdlc_device *hdlc = dev_to_hdlc(dev);
  764. #ifdef DEBUG_LINK
  765. printk(KERN_DEBUG "fr_start\n");
  766. #endif
  767. if (hdlc->state.fr.settings.lmi != LMI_NONE) {
  768. hdlc->state.fr.reliable = 0;
  769. hdlc->state.fr.dce_changed = 1;
  770. hdlc->state.fr.request = 0;
  771. hdlc->state.fr.fullrep_sent = 0;
  772. hdlc->state.fr.last_errors = 0xFFFFFFFF;
  773. hdlc->state.fr.n391cnt = 0;
  774. hdlc->state.fr.txseq = hdlc->state.fr.rxseq = 0;
  775. init_timer(&hdlc->state.fr.timer);
  776. /* First poll after 1 s */
  777. hdlc->state.fr.timer.expires = jiffies + HZ;
  778. hdlc->state.fr.timer.function = fr_timer;
  779. hdlc->state.fr.timer.data = (unsigned long)dev;
  780. add_timer(&hdlc->state.fr.timer);
  781. } else
  782. fr_set_link_state(1, dev);
  783. }
  784. static void fr_stop(struct net_device *dev)
  785. {
  786. hdlc_device *hdlc = dev_to_hdlc(dev);
  787. #ifdef DEBUG_LINK
  788. printk(KERN_DEBUG "fr_stop\n");
  789. #endif
  790. if (hdlc->state.fr.settings.lmi != LMI_NONE)
  791. del_timer_sync(&hdlc->state.fr.timer);
  792. fr_set_link_state(0, dev);
  793. }
  794. static void fr_close(struct net_device *dev)
  795. {
  796. hdlc_device *hdlc = dev_to_hdlc(dev);
  797. pvc_device *pvc = hdlc->state.fr.first_pvc;
  798. while (pvc) { /* Shutdown all PVCs for this FRAD */
  799. if (pvc->main)
  800. dev_close(pvc->main);
  801. if (pvc->ether)
  802. dev_close(pvc->ether);
  803. pvc = pvc->next;
  804. }
  805. }
  806. static void dlci_setup(struct net_device *dev)
  807. {
  808. dev->type = ARPHRD_DLCI;
  809. dev->flags = IFF_POINTOPOINT;
  810. dev->hard_header_len = 10;
  811. dev->addr_len = 2;
  812. }
  813. static int fr_add_pvc(struct net_device *master, unsigned int dlci, int type)
  814. {
  815. hdlc_device *hdlc = dev_to_hdlc(master);
  816. pvc_device *pvc = NULL;
  817. struct net_device *dev;
  818. int result, used;
  819. char * prefix = "pvc%d";
  820. if (type == ARPHRD_ETHER)
  821. prefix = "pvceth%d";
  822. if ((pvc = add_pvc(master, dlci)) == NULL) {
  823. printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
  824. master->name);
  825. return -ENOBUFS;
  826. }
  827. if (*get_dev_p(pvc, type))
  828. return -EEXIST;
  829. used = pvc_is_used(pvc);
  830. if (type == ARPHRD_ETHER)
  831. dev = alloc_netdev(sizeof(struct net_device_stats),
  832. "pvceth%d", ether_setup);
  833. else
  834. dev = alloc_netdev(sizeof(struct net_device_stats),
  835. "pvc%d", dlci_setup);
  836. if (!dev) {
  837. printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
  838. master->name);
  839. delete_unused_pvcs(hdlc);
  840. return -ENOBUFS;
  841. }
  842. if (type == ARPHRD_ETHER) {
  843. memcpy(dev->dev_addr, "\x00\x01", 2);
  844. get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
  845. } else {
  846. *(u16*)dev->dev_addr = htons(dlci);
  847. dlci_to_q922(dev->broadcast, dlci);
  848. }
  849. dev->hard_start_xmit = pvc_xmit;
  850. dev->get_stats = pvc_get_stats;
  851. dev->open = pvc_open;
  852. dev->stop = pvc_close;
  853. dev->do_ioctl = pvc_ioctl;
  854. dev->change_mtu = pvc_change_mtu;
  855. dev->mtu = HDLC_MAX_MTU;
  856. dev->tx_queue_len = 0;
  857. dev->priv = pvc;
  858. result = dev_alloc_name(dev, dev->name);
  859. if (result < 0) {
  860. free_netdev(dev);
  861. delete_unused_pvcs(hdlc);
  862. return result;
  863. }
  864. if (register_netdevice(dev) != 0) {
  865. free_netdev(dev);
  866. delete_unused_pvcs(hdlc);
  867. return -EIO;
  868. }
  869. dev->destructor = free_netdev;
  870. *get_dev_p(pvc, type) = dev;
  871. if (!used) {
  872. hdlc->state.fr.dce_changed = 1;
  873. hdlc->state.fr.dce_pvc_count++;
  874. }
  875. return 0;
  876. }
  877. static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
  878. {
  879. pvc_device *pvc;
  880. struct net_device *dev;
  881. if ((pvc = find_pvc(hdlc, dlci)) == NULL)
  882. return -ENOENT;
  883. if ((dev = *get_dev_p(pvc, type)) == NULL)
  884. return -ENOENT;
  885. if (dev->flags & IFF_UP)
  886. return -EBUSY; /* PVC in use */
  887. unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
  888. *get_dev_p(pvc, type) = NULL;
  889. if (!pvc_is_used(pvc)) {
  890. hdlc->state.fr.dce_pvc_count--;
  891. hdlc->state.fr.dce_changed = 1;
  892. }
  893. delete_unused_pvcs(hdlc);
  894. return 0;
  895. }
  896. static void fr_destroy(hdlc_device *hdlc)
  897. {
  898. pvc_device *pvc;
  899. pvc = hdlc->state.fr.first_pvc;
  900. hdlc->state.fr.first_pvc = NULL; /* All PVCs destroyed */
  901. hdlc->state.fr.dce_pvc_count = 0;
  902. hdlc->state.fr.dce_changed = 1;
  903. while (pvc) {
  904. pvc_device *next = pvc->next;
  905. /* destructors will free_netdev() main and ether */
  906. if (pvc->main)
  907. unregister_netdevice(pvc->main);
  908. if (pvc->ether)
  909. unregister_netdevice(pvc->ether);
  910. kfree(pvc);
  911. pvc = next;
  912. }
  913. }
  914. int hdlc_fr_ioctl(struct net_device *dev, struct ifreq *ifr)
  915. {
  916. fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
  917. const size_t size = sizeof(fr_proto);
  918. fr_proto new_settings;
  919. hdlc_device *hdlc = dev_to_hdlc(dev);
  920. fr_proto_pvc pvc;
  921. int result;
  922. switch (ifr->ifr_settings.type) {
  923. case IF_GET_PROTO:
  924. ifr->ifr_settings.type = IF_PROTO_FR;
  925. if (ifr->ifr_settings.size < size) {
  926. ifr->ifr_settings.size = size; /* data size wanted */
  927. return -ENOBUFS;
  928. }
  929. if (copy_to_user(fr_s, &hdlc->state.fr.settings, size))
  930. return -EFAULT;
  931. return 0;
  932. case IF_PROTO_FR:
  933. if(!capable(CAP_NET_ADMIN))
  934. return -EPERM;
  935. if(dev->flags & IFF_UP)
  936. return -EBUSY;
  937. if (copy_from_user(&new_settings, fr_s, size))
  938. return -EFAULT;
  939. if (new_settings.lmi == LMI_DEFAULT)
  940. new_settings.lmi = LMI_ANSI;
  941. if ((new_settings.lmi != LMI_NONE &&
  942. new_settings.lmi != LMI_ANSI &&
  943. new_settings.lmi != LMI_CCITT) ||
  944. new_settings.t391 < 1 ||
  945. new_settings.t392 < 2 ||
  946. new_settings.n391 < 1 ||
  947. new_settings.n392 < 1 ||
  948. new_settings.n393 < new_settings.n392 ||
  949. new_settings.n393 > 32 ||
  950. (new_settings.dce != 0 &&
  951. new_settings.dce != 1))
  952. return -EINVAL;
  953. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  954. if (result)
  955. return result;
  956. if (hdlc->proto.id != IF_PROTO_FR) {
  957. hdlc_proto_detach(hdlc);
  958. hdlc->state.fr.first_pvc = NULL;
  959. hdlc->state.fr.dce_pvc_count = 0;
  960. }
  961. memcpy(&hdlc->state.fr.settings, &new_settings, size);
  962. memset(&hdlc->proto, 0, sizeof(hdlc->proto));
  963. hdlc->proto.close = fr_close;
  964. hdlc->proto.start = fr_start;
  965. hdlc->proto.stop = fr_stop;
  966. hdlc->proto.detach = fr_destroy;
  967. hdlc->proto.netif_rx = fr_rx;
  968. hdlc->proto.id = IF_PROTO_FR;
  969. dev->hard_start_xmit = hdlc->xmit;
  970. dev->hard_header = NULL;
  971. dev->type = ARPHRD_FRAD;
  972. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  973. dev->addr_len = 0;
  974. return 0;
  975. case IF_PROTO_FR_ADD_PVC:
  976. case IF_PROTO_FR_DEL_PVC:
  977. case IF_PROTO_FR_ADD_ETH_PVC:
  978. case IF_PROTO_FR_DEL_ETH_PVC:
  979. if(!capable(CAP_NET_ADMIN))
  980. return -EPERM;
  981. if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
  982. sizeof(fr_proto_pvc)))
  983. return -EFAULT;
  984. if (pvc.dlci <= 0 || pvc.dlci >= 1024)
  985. return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
  986. if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
  987. ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
  988. result = ARPHRD_ETHER; /* bridged Ethernet device */
  989. else
  990. result = ARPHRD_DLCI;
  991. if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
  992. ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
  993. return fr_add_pvc(dev, pvc.dlci, result);
  994. else
  995. return fr_del_pvc(hdlc, pvc.dlci, result);
  996. }
  997. return -EINVAL;
  998. }