hdlc_raw.c 2.7 KB

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