hdlc_raw.c 2.2 KB

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