dlci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * DLCI Implementation of Frame Relay protocol for Linux, according to
  3. * RFC 1490. This generic device provides en/decapsulation for an
  4. * underlying hardware driver. Routes & IPs are assigned to these
  5. * interfaces. Requires 'dlcicfg' program to create usable
  6. * interfaces, the initial one, 'dlci' is for IOCTL use only.
  7. *
  8. * Version: @(#)dlci.c 0.35 4 Jan 1997
  9. *
  10. * Author: Mike McLagan <mike.mclagan@linux.org>
  11. *
  12. * Changes:
  13. *
  14. * 0.15 Mike Mclagan Packet freeing, bug in kmalloc call
  15. * DLCI_RET handling
  16. * 0.20 Mike McLagan More conservative on which packets
  17. * are returned for retry and which are
  18. * are dropped. If DLCI_RET_DROP is
  19. * returned from the FRAD, the packet is
  20. * sent back to Linux for re-transmission
  21. * 0.25 Mike McLagan Converted to use SIOC IOCTL calls
  22. * 0.30 Jim Freeman Fixed to allow IPX traffic
  23. * 0.35 Michael Elizabeth Fixed incorrect memcpy_fromfs
  24. *
  25. * This program is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU General Public License
  27. * as published by the Free Software Foundation; either version
  28. * 2 of the License, or (at your option) any later version.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/fcntl.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/ioport.h>
  37. #include <linux/in.h>
  38. #include <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <linux/string.h>
  41. #include <linux/errno.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/if_arp.h>
  45. #include <linux/if_frad.h>
  46. #include <linux/bitops.h>
  47. #include <net/sock.h>
  48. #include <asm/system.h>
  49. #include <asm/io.h>
  50. #include <asm/dma.h>
  51. #include <asm/uaccess.h>
  52. static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
  53. static LIST_HEAD(dlci_devs);
  54. static void dlci_setup(struct net_device *);
  55. /*
  56. * these encapsulate the RFC 1490 requirements as well as
  57. * deal with packet transmission and reception, working with
  58. * the upper network layers
  59. */
  60. static int dlci_header(struct sk_buff *skb, struct net_device *dev,
  61. unsigned short type, const void *daddr,
  62. const void *saddr, unsigned len)
  63. {
  64. struct frhdr hdr;
  65. struct dlci_local *dlp;
  66. unsigned int hlen;
  67. char *dest;
  68. dlp = netdev_priv(dev);
  69. hdr.control = FRAD_I_UI;
  70. switch(type)
  71. {
  72. case ETH_P_IP:
  73. hdr.IP_NLPID = FRAD_P_IP;
  74. hlen = sizeof(hdr.control) + sizeof(hdr.IP_NLPID);
  75. break;
  76. /* feel free to add other types, if necessary */
  77. default:
  78. hdr.pad = FRAD_P_PADDING;
  79. hdr.NLPID = FRAD_P_SNAP;
  80. memset(hdr.OUI, 0, sizeof(hdr.OUI));
  81. hdr.PID = htons(type);
  82. hlen = sizeof(hdr);
  83. break;
  84. }
  85. dest = skb_push(skb, hlen);
  86. if (!dest)
  87. return(0);
  88. memcpy(dest, &hdr, hlen);
  89. return(hlen);
  90. }
  91. static void dlci_receive(struct sk_buff *skb, struct net_device *dev)
  92. {
  93. struct dlci_local *dlp;
  94. struct frhdr *hdr;
  95. int process, header;
  96. dlp = netdev_priv(dev);
  97. if (!pskb_may_pull(skb, sizeof(*hdr))) {
  98. printk(KERN_NOTICE "%s: invalid data no header\n",
  99. dev->name);
  100. dev->stats.rx_errors++;
  101. kfree_skb(skb);
  102. return;
  103. }
  104. hdr = (struct frhdr *) skb->data;
  105. process = 0;
  106. header = 0;
  107. skb->dev = dev;
  108. if (hdr->control != FRAD_I_UI)
  109. {
  110. printk(KERN_NOTICE "%s: Invalid header flag 0x%02X.\n", dev->name, hdr->control);
  111. dev->stats.rx_errors++;
  112. }
  113. else
  114. switch(hdr->IP_NLPID)
  115. {
  116. case FRAD_P_PADDING:
  117. if (hdr->NLPID != FRAD_P_SNAP)
  118. {
  119. printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->NLPID);
  120. dev->stats.rx_errors++;
  121. break;
  122. }
  123. if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0)
  124. {
  125. printk(KERN_NOTICE "%s: Unsupported organizationally unique identifier 0x%02X-%02X-%02X.\n", dev->name, hdr->OUI[0], hdr->OUI[1], hdr->OUI[2]);
  126. dev->stats.rx_errors++;
  127. break;
  128. }
  129. /* at this point, it's an EtherType frame */
  130. header = sizeof(struct frhdr);
  131. /* Already in network order ! */
  132. skb->protocol = hdr->PID;
  133. process = 1;
  134. break;
  135. case FRAD_P_IP:
  136. header = sizeof(hdr->control) + sizeof(hdr->IP_NLPID);
  137. skb->protocol = htons(ETH_P_IP);
  138. process = 1;
  139. break;
  140. case FRAD_P_SNAP:
  141. case FRAD_P_Q933:
  142. case FRAD_P_CLNP:
  143. printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->pad);
  144. dev->stats.rx_errors++;
  145. break;
  146. default:
  147. printk(KERN_NOTICE "%s: Invalid pad byte 0x%02X.\n", dev->name, hdr->pad);
  148. dev->stats.rx_errors++;
  149. break;
  150. }
  151. if (process)
  152. {
  153. /* we've set up the protocol, so discard the header */
  154. skb_reset_mac_header(skb);
  155. skb_pull(skb, header);
  156. dev->stats.rx_bytes += skb->len;
  157. netif_rx(skb);
  158. dev->stats.rx_packets++;
  159. }
  160. else
  161. dev_kfree_skb(skb);
  162. }
  163. static netdev_tx_t dlci_transmit(struct sk_buff *skb, struct net_device *dev)
  164. {
  165. struct dlci_local *dlp = netdev_priv(dev);
  166. if (skb)
  167. dlp->slave->netdev_ops->ndo_start_xmit(skb, dlp->slave);
  168. return NETDEV_TX_OK;
  169. }
  170. static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
  171. {
  172. struct dlci_conf config;
  173. struct dlci_local *dlp;
  174. struct frad_local *flp;
  175. int err;
  176. dlp = netdev_priv(dev);
  177. flp = netdev_priv(dlp->slave);
  178. if (!get)
  179. {
  180. if(copy_from_user(&config, conf, sizeof(struct dlci_conf)))
  181. return -EFAULT;
  182. if (config.flags & ~DLCI_VALID_FLAGS)
  183. return(-EINVAL);
  184. memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
  185. dlp->configured = 1;
  186. }
  187. err = (*flp->dlci_conf)(dlp->slave, dev, get);
  188. if (err)
  189. return(err);
  190. if (get)
  191. {
  192. if(copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
  193. return -EFAULT;
  194. }
  195. return(0);
  196. }
  197. static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  198. {
  199. struct dlci_local *dlp;
  200. if (!capable(CAP_NET_ADMIN))
  201. return(-EPERM);
  202. dlp = netdev_priv(dev);
  203. switch(cmd)
  204. {
  205. case DLCI_GET_SLAVE:
  206. if (!*(short *)(dev->dev_addr))
  207. return(-EINVAL);
  208. strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
  209. break;
  210. case DLCI_GET_CONF:
  211. case DLCI_SET_CONF:
  212. if (!*(short *)(dev->dev_addr))
  213. return(-EINVAL);
  214. return(dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF));
  215. break;
  216. default:
  217. return(-EOPNOTSUPP);
  218. }
  219. return(0);
  220. }
  221. static int dlci_change_mtu(struct net_device *dev, int new_mtu)
  222. {
  223. struct dlci_local *dlp = netdev_priv(dev);
  224. return dev_set_mtu(dlp->slave, new_mtu);
  225. }
  226. static int dlci_open(struct net_device *dev)
  227. {
  228. struct dlci_local *dlp;
  229. struct frad_local *flp;
  230. int err;
  231. dlp = netdev_priv(dev);
  232. if (!*(short *)(dev->dev_addr))
  233. return(-EINVAL);
  234. if (!netif_running(dlp->slave))
  235. return(-ENOTCONN);
  236. flp = netdev_priv(dlp->slave);
  237. err = (*flp->activate)(dlp->slave, dev);
  238. if (err)
  239. return(err);
  240. netif_start_queue(dev);
  241. return 0;
  242. }
  243. static int dlci_close(struct net_device *dev)
  244. {
  245. struct dlci_local *dlp;
  246. struct frad_local *flp;
  247. int err;
  248. netif_stop_queue(dev);
  249. dlp = netdev_priv(dev);
  250. flp = netdev_priv(dlp->slave);
  251. err = (*flp->deactivate)(dlp->slave, dev);
  252. return 0;
  253. }
  254. static int dlci_add(struct dlci_add *dlci)
  255. {
  256. struct net_device *master, *slave;
  257. struct dlci_local *dlp;
  258. struct frad_local *flp;
  259. int err = -EINVAL;
  260. /* validate slave device */
  261. slave = dev_get_by_name(&init_net, dlci->devname);
  262. if (!slave)
  263. return -ENODEV;
  264. if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
  265. goto err1;
  266. /* create device name */
  267. master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
  268. dlci_setup);
  269. if (!master) {
  270. err = -ENOMEM;
  271. goto err1;
  272. }
  273. /* make sure same slave not already registered */
  274. rtnl_lock();
  275. list_for_each_entry(dlp, &dlci_devs, list) {
  276. if (dlp->slave == slave) {
  277. err = -EBUSY;
  278. goto err2;
  279. }
  280. }
  281. err = dev_alloc_name(master, master->name);
  282. if (err < 0)
  283. goto err2;
  284. *(short *)(master->dev_addr) = dlci->dlci;
  285. dlp = netdev_priv(master);
  286. dlp->slave = slave;
  287. dlp->master = master;
  288. flp = netdev_priv(slave);
  289. err = (*flp->assoc)(slave, master);
  290. if (err < 0)
  291. goto err2;
  292. err = register_netdevice(master);
  293. if (err < 0)
  294. goto err2;
  295. strcpy(dlci->devname, master->name);
  296. list_add(&dlp->list, &dlci_devs);
  297. rtnl_unlock();
  298. return(0);
  299. err2:
  300. rtnl_unlock();
  301. free_netdev(master);
  302. err1:
  303. dev_put(slave);
  304. return(err);
  305. }
  306. static int dlci_del(struct dlci_add *dlci)
  307. {
  308. struct dlci_local *dlp;
  309. struct frad_local *flp;
  310. struct net_device *master, *slave;
  311. int err;
  312. /* validate slave device */
  313. master = __dev_get_by_name(&init_net, dlci->devname);
  314. if (!master)
  315. return(-ENODEV);
  316. if (netif_running(master)) {
  317. return(-EBUSY);
  318. }
  319. dlp = netdev_priv(master);
  320. slave = dlp->slave;
  321. flp = netdev_priv(slave);
  322. rtnl_lock();
  323. err = (*flp->deassoc)(slave, master);
  324. if (!err) {
  325. list_del(&dlp->list);
  326. unregister_netdevice(master);
  327. dev_put(slave);
  328. }
  329. rtnl_unlock();
  330. return(err);
  331. }
  332. static int dlci_ioctl(unsigned int cmd, void __user *arg)
  333. {
  334. struct dlci_add add;
  335. int err;
  336. if (!capable(CAP_NET_ADMIN))
  337. return(-EPERM);
  338. if(copy_from_user(&add, arg, sizeof(struct dlci_add)))
  339. return -EFAULT;
  340. switch (cmd)
  341. {
  342. case SIOCADDDLCI:
  343. err = dlci_add(&add);
  344. if (!err)
  345. if(copy_to_user(arg, &add, sizeof(struct dlci_add)))
  346. return -EFAULT;
  347. break;
  348. case SIOCDELDLCI:
  349. err = dlci_del(&add);
  350. break;
  351. default:
  352. err = -EINVAL;
  353. }
  354. return(err);
  355. }
  356. static const struct header_ops dlci_header_ops = {
  357. .create = dlci_header,
  358. };
  359. static const struct net_device_ops dlci_netdev_ops = {
  360. .ndo_open = dlci_open,
  361. .ndo_stop = dlci_close,
  362. .ndo_do_ioctl = dlci_dev_ioctl,
  363. .ndo_start_xmit = dlci_transmit,
  364. .ndo_change_mtu = dlci_change_mtu,
  365. };
  366. static void dlci_setup(struct net_device *dev)
  367. {
  368. struct dlci_local *dlp = netdev_priv(dev);
  369. dev->flags = 0;
  370. dev->header_ops = &dlci_header_ops;
  371. dev->netdev_ops = &dlci_netdev_ops;
  372. dev->destructor = free_netdev;
  373. dlp->receive = dlci_receive;
  374. dev->type = ARPHRD_DLCI;
  375. dev->hard_header_len = sizeof(struct frhdr);
  376. dev->addr_len = sizeof(short);
  377. }
  378. /* if slave is unregistering, then cleanup master */
  379. static int dlci_dev_event(struct notifier_block *unused,
  380. unsigned long event, void *ptr)
  381. {
  382. struct net_device *dev = (struct net_device *) ptr;
  383. if (dev_net(dev) != &init_net)
  384. return NOTIFY_DONE;
  385. if (event == NETDEV_UNREGISTER) {
  386. struct dlci_local *dlp;
  387. list_for_each_entry(dlp, &dlci_devs, list) {
  388. if (dlp->slave == dev) {
  389. list_del(&dlp->list);
  390. unregister_netdevice(dlp->master);
  391. dev_put(dlp->slave);
  392. break;
  393. }
  394. }
  395. }
  396. return NOTIFY_DONE;
  397. }
  398. static struct notifier_block dlci_notifier = {
  399. .notifier_call = dlci_dev_event,
  400. };
  401. static int __init init_dlci(void)
  402. {
  403. dlci_ioctl_set(dlci_ioctl);
  404. register_netdevice_notifier(&dlci_notifier);
  405. printk("%s.\n", version);
  406. return 0;
  407. }
  408. static void __exit dlci_exit(void)
  409. {
  410. struct dlci_local *dlp, *nxt;
  411. dlci_ioctl_set(NULL);
  412. unregister_netdevice_notifier(&dlci_notifier);
  413. rtnl_lock();
  414. list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
  415. unregister_netdevice(dlp->master);
  416. dev_put(dlp->slave);
  417. }
  418. rtnl_unlock();
  419. }
  420. module_init(init_dlci);
  421. module_exit(dlci_exit);
  422. MODULE_AUTHOR("Mike McLagan");
  423. MODULE_DESCRIPTION("Frame Relay DLCI layer");
  424. MODULE_LICENSE("GPL");