hdlc_fr.c 31 KB

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