hdlc_raw_eth.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * HDLC Ethernet emulation support
  4. *
  5. * Copyright (C) 2002-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. #include <linux/errno.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/gfp.h>
  14. #include <linux/hdlc.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/inetdevice.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pkt_sched.h>
  21. #include <linux/poll.h>
  22. #include <linux/rtnetlink.h>
  23. #include <linux/skbuff.h>
  24. static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr);
  25. static netdev_tx_t eth_tx(struct sk_buff *skb, struct net_device *dev)
  26. {
  27. int pad = ETH_ZLEN - skb->len;
  28. if (pad > 0) { /* Pad the frame with zeros */
  29. int len = skb->len;
  30. if (skb_tailroom(skb) < pad)
  31. if (pskb_expand_head(skb, 0, pad, GFP_ATOMIC)) {
  32. dev->stats.tx_dropped++;
  33. dev_kfree_skb(skb);
  34. return 0;
  35. }
  36. skb_put(skb, pad);
  37. memset(skb->data + len, 0, pad);
  38. }
  39. return dev_to_hdlc(dev)->xmit(skb, dev);
  40. }
  41. static struct hdlc_proto proto = {
  42. .type_trans = eth_type_trans,
  43. .xmit = eth_tx,
  44. .ioctl = raw_eth_ioctl,
  45. .module = THIS_MODULE,
  46. };
  47. static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr)
  48. {
  49. raw_hdlc_proto __user *raw_s = ifr->ifr_settings.ifs_ifsu.raw_hdlc;
  50. const size_t size = sizeof(raw_hdlc_proto);
  51. raw_hdlc_proto new_settings;
  52. hdlc_device *hdlc = dev_to_hdlc(dev);
  53. int result, old_qlen;
  54. switch (ifr->ifr_settings.type) {
  55. case IF_GET_PROTO:
  56. if (dev_to_hdlc(dev)->proto != &proto)
  57. return -EINVAL;
  58. ifr->ifr_settings.type = IF_PROTO_HDLC_ETH;
  59. if (ifr->ifr_settings.size < size) {
  60. ifr->ifr_settings.size = size; /* data size wanted */
  61. return -ENOBUFS;
  62. }
  63. if (copy_to_user(raw_s, hdlc->state, size))
  64. return -EFAULT;
  65. return 0;
  66. case IF_PROTO_HDLC_ETH:
  67. if (!capable(CAP_NET_ADMIN))
  68. return -EPERM;
  69. if (dev->flags & IFF_UP)
  70. return -EBUSY;
  71. if (copy_from_user(&new_settings, raw_s, size))
  72. return -EFAULT;
  73. if (new_settings.encoding == ENCODING_DEFAULT)
  74. new_settings.encoding = ENCODING_NRZ;
  75. if (new_settings.parity == PARITY_DEFAULT)
  76. new_settings.parity = PARITY_CRC16_PR1_CCITT;
  77. result = hdlc->attach(dev, new_settings.encoding,
  78. new_settings.parity);
  79. if (result)
  80. return result;
  81. result = attach_hdlc_protocol(dev, &proto,
  82. sizeof(raw_hdlc_proto));
  83. if (result)
  84. return result;
  85. memcpy(hdlc->state, &new_settings, size);
  86. old_qlen = dev->tx_queue_len;
  87. ether_setup(dev);
  88. dev->tx_queue_len = old_qlen;
  89. eth_hw_addr_random(dev);
  90. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  91. netif_dormant_off(dev);
  92. return 0;
  93. }
  94. return -EINVAL;
  95. }
  96. static int __init mod_init(void)
  97. {
  98. register_hdlc_protocol(&proto);
  99. return 0;
  100. }
  101. static void __exit mod_exit(void)
  102. {
  103. unregister_hdlc_protocol(&proto);
  104. }
  105. module_init(mod_init);
  106. module_exit(mod_exit);
  107. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  108. MODULE_DESCRIPTION("Ethernet encapsulation support for generic HDLC");
  109. MODULE_LICENSE("GPL v2");