hdlc_raw_eth.c 3.2 KB

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