dlci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. dlp->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. dlp->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. dlp->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. dlp->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. dlp->stats.rx_errors++;
  145. break;
  146. default:
  147. printk(KERN_NOTICE "%s: Invalid pad byte 0x%02X.\n", dev->name, hdr->pad);
  148. dlp->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. dlp->stats.rx_bytes += skb->len;
  157. netif_rx(skb);
  158. dlp->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->hard_start_xmit(skb, dlp->slave);
  173. switch (ret)
  174. {
  175. case DLCI_RET_OK:
  176. dlp->stats.tx_packets++;
  177. ret = 0;
  178. break;
  179. case DLCI_RET_ERR:
  180. dlp->stats.tx_errors++;
  181. ret = 0;
  182. break;
  183. case DLCI_RET_DROP:
  184. dlp->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;
  251. dlp = netdev_priv(dev);
  252. return((*dlp->slave->change_mtu)(dlp->slave, new_mtu));
  253. }
  254. static int dlci_open(struct net_device *dev)
  255. {
  256. struct dlci_local *dlp;
  257. struct frad_local *flp;
  258. int err;
  259. dlp = netdev_priv(dev);
  260. if (!*(short *)(dev->dev_addr))
  261. return(-EINVAL);
  262. if (!netif_running(dlp->slave))
  263. return(-ENOTCONN);
  264. flp = netdev_priv(dlp->slave);
  265. err = (*flp->activate)(dlp->slave, dev);
  266. if (err)
  267. return(err);
  268. netif_start_queue(dev);
  269. return 0;
  270. }
  271. static int dlci_close(struct net_device *dev)
  272. {
  273. struct dlci_local *dlp;
  274. struct frad_local *flp;
  275. int err;
  276. netif_stop_queue(dev);
  277. dlp = netdev_priv(dev);
  278. flp = netdev_priv(dlp->slave);
  279. err = (*flp->deactivate)(dlp->slave, dev);
  280. return 0;
  281. }
  282. static struct net_device_stats *dlci_get_stats(struct net_device *dev)
  283. {
  284. struct dlci_local *dlp;
  285. dlp = netdev_priv(dev);
  286. return(&dlp->stats);
  287. }
  288. static int dlci_add(struct dlci_add *dlci)
  289. {
  290. struct net_device *master, *slave;
  291. struct dlci_local *dlp;
  292. struct frad_local *flp;
  293. int err = -EINVAL;
  294. /* validate slave device */
  295. slave = dev_get_by_name(&init_net, dlci->devname);
  296. if (!slave)
  297. return -ENODEV;
  298. if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
  299. goto err1;
  300. /* create device name */
  301. master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
  302. dlci_setup);
  303. if (!master) {
  304. err = -ENOMEM;
  305. goto err1;
  306. }
  307. /* make sure same slave not already registered */
  308. rtnl_lock();
  309. list_for_each_entry(dlp, &dlci_devs, list) {
  310. if (dlp->slave == slave) {
  311. err = -EBUSY;
  312. goto err2;
  313. }
  314. }
  315. err = dev_alloc_name(master, master->name);
  316. if (err < 0)
  317. goto err2;
  318. *(short *)(master->dev_addr) = dlci->dlci;
  319. dlp = netdev_priv(master);
  320. dlp->slave = slave;
  321. dlp->master = master;
  322. flp = netdev_priv(slave);
  323. err = (*flp->assoc)(slave, master);
  324. if (err < 0)
  325. goto err2;
  326. err = register_netdevice(master);
  327. if (err < 0)
  328. goto err2;
  329. strcpy(dlci->devname, master->name);
  330. list_add(&dlp->list, &dlci_devs);
  331. rtnl_unlock();
  332. return(0);
  333. err2:
  334. rtnl_unlock();
  335. free_netdev(master);
  336. err1:
  337. dev_put(slave);
  338. return(err);
  339. }
  340. static int dlci_del(struct dlci_add *dlci)
  341. {
  342. struct dlci_local *dlp;
  343. struct frad_local *flp;
  344. struct net_device *master, *slave;
  345. int err;
  346. /* validate slave device */
  347. master = __dev_get_by_name(&init_net, dlci->devname);
  348. if (!master)
  349. return(-ENODEV);
  350. if (netif_running(master)) {
  351. return(-EBUSY);
  352. }
  353. dlp = netdev_priv(master);
  354. slave = dlp->slave;
  355. flp = netdev_priv(slave);
  356. rtnl_lock();
  357. err = (*flp->deassoc)(slave, master);
  358. if (!err) {
  359. list_del(&dlp->list);
  360. unregister_netdevice(master);
  361. dev_put(slave);
  362. }
  363. rtnl_unlock();
  364. return(err);
  365. }
  366. static int dlci_ioctl(unsigned int cmd, void __user *arg)
  367. {
  368. struct dlci_add add;
  369. int err;
  370. if (!capable(CAP_NET_ADMIN))
  371. return(-EPERM);
  372. if(copy_from_user(&add, arg, sizeof(struct dlci_add)))
  373. return -EFAULT;
  374. switch (cmd)
  375. {
  376. case SIOCADDDLCI:
  377. err = dlci_add(&add);
  378. if (!err)
  379. if(copy_to_user(arg, &add, sizeof(struct dlci_add)))
  380. return -EFAULT;
  381. break;
  382. case SIOCDELDLCI:
  383. err = dlci_del(&add);
  384. break;
  385. default:
  386. err = -EINVAL;
  387. }
  388. return(err);
  389. }
  390. static const struct header_ops dlci_header_ops = {
  391. .create = dlci_header,
  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->open = dlci_open;
  398. dev->stop = dlci_close;
  399. dev->do_ioctl = dlci_dev_ioctl;
  400. dev->hard_start_xmit = dlci_transmit;
  401. dev->header_ops = &dlci_header_ops;
  402. dev->get_stats = dlci_get_stats;
  403. dev->change_mtu = dlci_change_mtu;
  404. dev->destructor = free_netdev;
  405. dlp->receive = dlci_receive;
  406. dev->type = ARPHRD_DLCI;
  407. dev->hard_header_len = sizeof(struct frhdr);
  408. dev->addr_len = sizeof(short);
  409. }
  410. /* if slave is unregistering, then cleanup master */
  411. static int dlci_dev_event(struct notifier_block *unused,
  412. unsigned long event, void *ptr)
  413. {
  414. struct net_device *dev = (struct net_device *) ptr;
  415. if (dev_net(dev) != &init_net)
  416. return NOTIFY_DONE;
  417. if (event == NETDEV_UNREGISTER) {
  418. struct dlci_local *dlp;
  419. list_for_each_entry(dlp, &dlci_devs, list) {
  420. if (dlp->slave == dev) {
  421. list_del(&dlp->list);
  422. unregister_netdevice(dlp->master);
  423. dev_put(dlp->slave);
  424. break;
  425. }
  426. }
  427. }
  428. return NOTIFY_DONE;
  429. }
  430. static struct notifier_block dlci_notifier = {
  431. .notifier_call = dlci_dev_event,
  432. };
  433. static int __init init_dlci(void)
  434. {
  435. dlci_ioctl_set(dlci_ioctl);
  436. register_netdevice_notifier(&dlci_notifier);
  437. printk("%s.\n", version);
  438. return 0;
  439. }
  440. static void __exit dlci_exit(void)
  441. {
  442. struct dlci_local *dlp, *nxt;
  443. dlci_ioctl_set(NULL);
  444. unregister_netdevice_notifier(&dlci_notifier);
  445. rtnl_lock();
  446. list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
  447. unregister_netdevice(dlp->master);
  448. dev_put(dlp->slave);
  449. }
  450. rtnl_unlock();
  451. }
  452. module_init(init_dlci);
  453. module_exit(dlci_exit);
  454. MODULE_AUTHOR("Mike McLagan");
  455. MODULE_DESCRIPTION("Frame Relay DLCI layer");
  456. MODULE_LICENSE("GPL");