hdlc_raw.c 2.6 KB

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