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