hdlc_raw_eth.c 3.3 KB

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