hdlc_raw.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * HDLC support
  4. *
  5. * Copyright (C) 1999 - 2003 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 unsigned short raw_type_trans(struct sk_buff *skb,
  25. struct net_device *dev)
  26. {
  27. return __constant_htons(ETH_P_IP);
  28. }
  29. int hdlc_raw_ioctl(struct net_device *dev, struct ifreq *ifr)
  30. {
  31. raw_hdlc_proto __user *raw_s = ifr->ifr_settings.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 (ifr->ifr_settings.type) {
  37. case IF_GET_PROTO:
  38. ifr->ifr_settings.type = IF_PROTO_HDLC;
  39. if (ifr->ifr_settings.size < size) {
  40. ifr->ifr_settings.size = size; /* data size wanted */
  41. return -ENOBUFS;
  42. }
  43. if (copy_to_user(raw_s, &hdlc->state.raw_hdlc.settings, size))
  44. return -EFAULT;
  45. return 0;
  46. case IF_PROTO_HDLC:
  47. if (!capable(CAP_NET_ADMIN))
  48. return -EPERM;
  49. if (dev->flags & IFF_UP)
  50. return -EBUSY;
  51. if (copy_from_user(&new_settings, raw_s, size))
  52. return -EFAULT;
  53. if (new_settings.encoding == ENCODING_DEFAULT)
  54. new_settings.encoding = ENCODING_NRZ;
  55. if (new_settings.parity == PARITY_DEFAULT)
  56. new_settings.parity = PARITY_CRC16_PR1_CCITT;
  57. result = hdlc->attach(dev, new_settings.encoding,
  58. new_settings.parity);
  59. if (result)
  60. return result;
  61. hdlc_proto_detach(hdlc);
  62. memcpy(&hdlc->state.raw_hdlc.settings, &new_settings, size);
  63. memset(&hdlc->proto, 0, sizeof(hdlc->proto));
  64. hdlc->proto.type_trans = raw_type_trans;
  65. hdlc->proto.id = IF_PROTO_HDLC;
  66. dev->hard_start_xmit = hdlc->xmit;
  67. dev->hard_header = NULL;
  68. dev->type = ARPHRD_RAWHDLC;
  69. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  70. dev->addr_len = 0;
  71. return 0;
  72. }
  73. return -EINVAL;
  74. }