hdlc_raw_eth.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * HDLC Ethernet emulation support
  4. *
  5. * Copyright (C) 2002-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/random.h>
  21. #include <linux/inetdevice.h>
  22. #include <linux/lapb.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/hdlc.h>
  26. static int eth_tx(struct sk_buff *skb, struct net_device *dev)
  27. {
  28. int pad = ETH_ZLEN - skb->len;
  29. if (pad > 0) { /* Pad the frame with zeros */
  30. int len = skb->len;
  31. if (skb_tailroom(skb) < pad)
  32. if (pskb_expand_head(skb, 0, pad, GFP_ATOMIC)) {
  33. hdlc_stats(dev)->tx_dropped++;
  34. dev_kfree_skb(skb);
  35. return 0;
  36. }
  37. skb_put(skb, pad);
  38. memset(skb->data + len, 0, pad);
  39. }
  40. return dev_to_hdlc(dev)->xmit(skb, dev);
  41. }
  42. int hdlc_raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr)
  43. {
  44. raw_hdlc_proto __user *raw_s = ifr->ifr_settings.ifs_ifsu.raw_hdlc;
  45. const size_t size = sizeof(raw_hdlc_proto);
  46. raw_hdlc_proto new_settings;
  47. hdlc_device *hdlc = dev_to_hdlc(dev);
  48. int result;
  49. void *old_ch_mtu;
  50. int old_qlen;
  51. switch (ifr->ifr_settings.type) {
  52. case IF_GET_PROTO:
  53. ifr->ifr_settings.type = IF_PROTO_HDLC_ETH;
  54. if (ifr->ifr_settings.size < size) {
  55. ifr->ifr_settings.size = size; /* data size wanted */
  56. return -ENOBUFS;
  57. }
  58. if (copy_to_user(raw_s, &hdlc->state.raw_hdlc.settings, size))
  59. return -EFAULT;
  60. return 0;
  61. case IF_PROTO_HDLC_ETH:
  62. if (!capable(CAP_NET_ADMIN))
  63. return -EPERM;
  64. if (dev->flags & IFF_UP)
  65. return -EBUSY;
  66. if (copy_from_user(&new_settings, raw_s, size))
  67. return -EFAULT;
  68. if (new_settings.encoding == ENCODING_DEFAULT)
  69. new_settings.encoding = ENCODING_NRZ;
  70. if (new_settings.parity == PARITY_DEFAULT)
  71. new_settings.parity = PARITY_CRC16_PR1_CCITT;
  72. result = hdlc->attach(dev, new_settings.encoding,
  73. new_settings.parity);
  74. if (result)
  75. return result;
  76. hdlc_proto_detach(hdlc);
  77. memcpy(&hdlc->state.raw_hdlc.settings, &new_settings, size);
  78. memset(&hdlc->proto, 0, sizeof(hdlc->proto));
  79. hdlc->proto.type_trans = eth_type_trans;
  80. hdlc->proto.id = IF_PROTO_HDLC_ETH;
  81. dev->hard_start_xmit = eth_tx;
  82. old_ch_mtu = dev->change_mtu;
  83. old_qlen = dev->tx_queue_len;
  84. ether_setup(dev);
  85. dev->change_mtu = old_ch_mtu;
  86. dev->tx_queue_len = old_qlen;
  87. memcpy(dev->dev_addr, "\x00\x01", 2);
  88. get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
  89. return 0;
  90. }
  91. return -EINVAL;
  92. }