dlci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 int dlci_transmit(struct sk_buff *skb, struct net_device *dev)
  164. {
  165. struct dlci_local *dlp;
  166. int ret;
  167. ret = 0;
  168. if (!skb || !dev)
  169. return(0);
  170. dlp = netdev_priv(dev);
  171. netif_stop_queue(dev);
  172. ret = dlp->slave->netdev_ops->ndo_start_xmit(skb, dlp->slave);
  173. switch (ret)
  174. {
  175. case DLCI_RET_OK:
  176. dev->stats.tx_packets++;
  177. ret = 0;
  178. break;
  179. case DLCI_RET_ERR:
  180. dev->stats.tx_errors++;
  181. ret = 0;
  182. break;
  183. case DLCI_RET_DROP:
  184. dev->stats.tx_dropped++;
  185. ret = 1;
  186. break;
  187. }
  188. /* Alan Cox recommends always returning 0, and always freeing the packet */
  189. /* experience suggest a slightly more conservative approach */
  190. if (!ret)
  191. {
  192. dev_kfree_skb(skb);
  193. netif_wake_queue(dev);
  194. }
  195. return(ret);
  196. }
  197. static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
  198. {
  199. struct dlci_conf config;
  200. struct dlci_local *dlp;
  201. struct frad_local *flp;
  202. int err;
  203. dlp = netdev_priv(dev);
  204. flp = netdev_priv(dlp->slave);
  205. if (!get)
  206. {
  207. if(copy_from_user(&config, conf, sizeof(struct dlci_conf)))
  208. return -EFAULT;
  209. if (config.flags & ~DLCI_VALID_FLAGS)
  210. return(-EINVAL);
  211. memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
  212. dlp->configured = 1;
  213. }
  214. err = (*flp->dlci_conf)(dlp->slave, dev, get);
  215. if (err)
  216. return(err);
  217. if (get)
  218. {
  219. if(copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
  220. return -EFAULT;
  221. }
  222. return(0);
  223. }
  224. static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  225. {
  226. struct dlci_local *dlp;
  227. if (!capable(CAP_NET_ADMIN))
  228. return(-EPERM);
  229. dlp = netdev_priv(dev);
  230. switch(cmd)
  231. {
  232. case DLCI_GET_SLAVE:
  233. if (!*(short *)(dev->dev_addr))
  234. return(-EINVAL);
  235. strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
  236. break;
  237. case DLCI_GET_CONF:
  238. case DLCI_SET_CONF:
  239. if (!*(short *)(dev->dev_addr))
  240. return(-EINVAL);
  241. return(dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF));
  242. break;
  243. default:
  244. return(-EOPNOTSUPP);
  245. }
  246. return(0);
  247. }
  248. static int dlci_change_mtu(struct net_device *dev, int new_mtu)
  249. {
  250. struct dlci_local *dlp = netdev_priv(dev);
  251. return dev_set_mtu(dlp->slave, new_mtu);
  252. }
  253. static int dlci_open(struct net_device *dev)
  254. {
  255. struct dlci_local *dlp;
  256. struct frad_local *flp;
  257. int err;
  258. dlp = netdev_priv(dev);
  259. if (!*(short *)(dev->dev_addr))
  260. return(-EINVAL);
  261. if (!netif_running(dlp->slave))
  262. return(-ENOTCONN);
  263. flp = netdev_priv(dlp->slave);
  264. err = (*flp->activate)(dlp->slave, dev);
  265. if (err)
  266. return(err);
  267. netif_start_queue(dev);
  268. return 0;
  269. }
  270. static int dlci_close(struct net_device *dev)
  271. {
  272. struct dlci_local *dlp;
  273. struct frad_local *flp;
  274. int err;
  275. netif_stop_queue(dev);
  276. dlp = netdev_priv(dev);
  277. flp = netdev_priv(dlp->slave);
  278. err = (*flp->deactivate)(dlp->slave, dev);
  279. return 0;
  280. }
  281. static int dlci_add(struct dlci_add *dlci)
  282. {
  283. struct net_device *master, *slave;
  284. struct dlci_local *dlp;
  285. struct frad_local *flp;
  286. int err = -EINVAL;
  287. /* validate slave device */
  288. slave = dev_get_by_name(&init_net, dlci->devname);
  289. if (!slave)
  290. return -ENODEV;
  291. if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
  292. goto err1;
  293. /* create device name */
  294. master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
  295. dlci_setup);
  296. if (!master) {
  297. err = -ENOMEM;
  298. goto err1;
  299. }
  300. /* make sure same slave not already registered */
  301. rtnl_lock();
  302. list_for_each_entry(dlp, &dlci_devs, list) {
  303. if (dlp->slave == slave) {
  304. err = -EBUSY;
  305. goto err2;
  306. }
  307. }
  308. err = dev_alloc_name(master, master->name);
  309. if (err < 0)
  310. goto err2;
  311. *(short *)(master->dev_addr) = dlci->dlci;
  312. dlp = netdev_priv(master);
  313. dlp->slave = slave;
  314. dlp->master = master;
  315. flp = netdev_priv(slave);
  316. err = (*flp->assoc)(slave, master);
  317. if (err < 0)
  318. goto err2;
  319. err = register_netdevice(master);
  320. if (err < 0)
  321. goto err2;
  322. strcpy(dlci->devname, master->name);
  323. list_add(&dlp->list, &dlci_devs);
  324. rtnl_unlock();
  325. return(0);
  326. err2:
  327. rtnl_unlock();
  328. free_netdev(master);
  329. err1:
  330. dev_put(slave);
  331. return(err);
  332. }
  333. static int dlci_del(struct dlci_add *dlci)
  334. {
  335. struct dlci_local *dlp;
  336. struct frad_local *flp;
  337. struct net_device *master, *slave;
  338. int err;
  339. /* validate slave device */
  340. master = __dev_get_by_name(&init_net, dlci->devname);
  341. if (!master)
  342. return(-ENODEV);
  343. if (netif_running(master)) {
  344. return(-EBUSY);
  345. }
  346. dlp = netdev_priv(master);
  347. slave = dlp->slave;
  348. flp = netdev_priv(slave);
  349. rtnl_lock();
  350. err = (*flp->deassoc)(slave, master);
  351. if (!err) {
  352. list_del(&dlp->list);
  353. unregister_netdevice(master);
  354. dev_put(slave);
  355. }
  356. rtnl_unlock();
  357. return(err);
  358. }
  359. static int dlci_ioctl(unsigned int cmd, void __user *arg)
  360. {
  361. struct dlci_add add;
  362. int err;
  363. if (!capable(CAP_NET_ADMIN))
  364. return(-EPERM);
  365. if(copy_from_user(&add, arg, sizeof(struct dlci_add)))
  366. return -EFAULT;
  367. switch (cmd)
  368. {
  369. case SIOCADDDLCI:
  370. err = dlci_add(&add);
  371. if (!err)
  372. if(copy_to_user(arg, &add, sizeof(struct dlci_add)))
  373. return -EFAULT;
  374. break;
  375. case SIOCDELDLCI:
  376. err = dlci_del(&add);
  377. break;
  378. default:
  379. err = -EINVAL;
  380. }
  381. return(err);
  382. }
  383. static const struct header_ops dlci_header_ops = {
  384. .create = dlci_header,
  385. };
  386. static const struct net_device_ops dlci_netdev_ops = {
  387. .ndo_open = dlci_open,
  388. .ndo_stop = dlci_close,
  389. .ndo_do_ioctl = dlci_dev_ioctl,
  390. .ndo_start_xmit = dlci_transmit,
  391. .ndo_change_mtu = dlci_change_mtu,
  392. };
  393. static void dlci_setup(struct net_device *dev)
  394. {
  395. struct dlci_local *dlp = netdev_priv(dev);
  396. dev->flags = 0;
  397. dev->header_ops = &dlci_header_ops;
  398. dev->netdev_ops = &dlci_netdev_ops;
  399. dev->destructor = free_netdev;
  400. dlp->receive = dlci_receive;
  401. dev->type = ARPHRD_DLCI;
  402. dev->hard_header_len = sizeof(struct frhdr);
  403. dev->addr_len = sizeof(short);
  404. }
  405. /* if slave is unregistering, then cleanup master */
  406. static int dlci_dev_event(struct notifier_block *unused,
  407. unsigned long event, void *ptr)
  408. {
  409. struct net_device *dev = (struct net_device *) ptr;
  410. if (dev_net(dev) != &init_net)
  411. return NOTIFY_DONE;
  412. if (event == NETDEV_UNREGISTER) {
  413. struct dlci_local *dlp;
  414. list_for_each_entry(dlp, &dlci_devs, list) {
  415. if (dlp->slave == dev) {
  416. list_del(&dlp->list);
  417. unregister_netdevice(dlp->master);
  418. dev_put(dlp->slave);
  419. break;
  420. }
  421. }
  422. }
  423. return NOTIFY_DONE;
  424. }
  425. static struct notifier_block dlci_notifier = {
  426. .notifier_call = dlci_dev_event,
  427. };
  428. static int __init init_dlci(void)
  429. {
  430. dlci_ioctl_set(dlci_ioctl);
  431. register_netdevice_notifier(&dlci_notifier);
  432. printk("%s.\n", version);
  433. return 0;
  434. }
  435. static void __exit dlci_exit(void)
  436. {
  437. struct dlci_local *dlp, *nxt;
  438. dlci_ioctl_set(NULL);
  439. unregister_netdevice_notifier(&dlci_notifier);
  440. rtnl_lock();
  441. list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
  442. unregister_netdevice(dlp->master);
  443. dev_put(dlp->slave);
  444. }
  445. rtnl_unlock();
  446. }
  447. module_init(init_dlci);
  448. module_exit(dlci_exit);
  449. MODULE_AUTHOR("Mike McLagan");
  450. MODULE_DESCRIPTION("Frame Relay DLCI layer");
  451. MODULE_LICENSE("GPL");