hdlc_fr.c 30 KB

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