hdlc_raw_eth.c 3.0 KB

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