hdlc_raw.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic HDLC support routines for Linux
  4. * HDLC support
  5. *
  6. * Copyright (C) 1999 - 2006 Krzysztof Halasa <[email protected]>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/hdlc.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/inetdevice.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/pkt_sched.h>
  16. #include <linux/poll.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/skbuff.h>
  19. static int raw_ioctl(struct net_device *dev, struct if_settings *ifs);
  20. static __be16 raw_type_trans(struct sk_buff *skb, struct net_device *dev)
  21. {
  22. return cpu_to_be16(ETH_P_IP);
  23. }
  24. static struct hdlc_proto proto = {
  25. .type_trans = raw_type_trans,
  26. .ioctl = raw_ioctl,
  27. .module = THIS_MODULE,
  28. };
  29. static int raw_ioctl(struct net_device *dev, struct if_settings *ifs)
  30. {
  31. raw_hdlc_proto __user *raw_s = ifs->ifs_ifsu.raw_hdlc;
  32. const size_t size = sizeof(raw_hdlc_proto);
  33. raw_hdlc_proto new_settings;
  34. hdlc_device *hdlc = dev_to_hdlc(dev);
  35. int result;
  36. switch (ifs->type) {
  37. case IF_GET_PROTO:
  38. if (dev_to_hdlc(dev)->proto != &proto)
  39. return -EINVAL;
  40. ifs->type = IF_PROTO_HDLC;
  41. if (ifs->size < size) {
  42. ifs->size = size; /* data size wanted */
  43. return -ENOBUFS;
  44. }
  45. if (copy_to_user(raw_s, hdlc->state, size))
  46. return -EFAULT;
  47. return 0;
  48. case IF_PROTO_HDLC:
  49. if (!capable(CAP_NET_ADMIN))
  50. return -EPERM;
  51. if (dev->flags & IFF_UP)
  52. return -EBUSY;
  53. if (copy_from_user(&new_settings, raw_s, size))
  54. return -EFAULT;
  55. if (new_settings.encoding == ENCODING_DEFAULT)
  56. new_settings.encoding = ENCODING_NRZ;
  57. if (new_settings.parity == PARITY_DEFAULT)
  58. new_settings.parity = PARITY_CRC16_PR1_CCITT;
  59. result = hdlc->attach(dev, new_settings.encoding,
  60. new_settings.parity);
  61. if (result)
  62. return result;
  63. result = attach_hdlc_protocol(dev, &proto,
  64. sizeof(raw_hdlc_proto));
  65. if (result)
  66. return result;
  67. memcpy(hdlc->state, &new_settings, size);
  68. dev->type = ARPHRD_RAWHDLC;
  69. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  70. netif_dormant_off(dev);
  71. return 0;
  72. }
  73. return -EINVAL;
  74. }
  75. static int __init hdlc_raw_init(void)
  76. {
  77. register_hdlc_protocol(&proto);
  78. return 0;
  79. }
  80. static void __exit hdlc_raw_exit(void)
  81. {
  82. unregister_hdlc_protocol(&proto);
  83. }
  84. module_init(hdlc_raw_init);
  85. module_exit(hdlc_raw_exit);
  86. MODULE_AUTHOR("Krzysztof Halasa <[email protected]>");
  87. MODULE_DESCRIPTION("Raw HDLC protocol support for generic HDLC");
  88. MODULE_LICENSE("GPL v2");