hdlc_ppp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * Point-to-point protocol 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. #include <net/syncppp.h>
  24. struct ppp_state {
  25. struct ppp_device pppdev;
  26. struct ppp_device *syncppp_ptr;
  27. int (*old_change_mtu)(struct net_device *dev, int new_mtu);
  28. };
  29. static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr);
  30. static inline struct ppp_state* state(hdlc_device *hdlc)
  31. {
  32. return(struct ppp_state *)(hdlc->state);
  33. }
  34. static int ppp_open(struct net_device *dev)
  35. {
  36. hdlc_device *hdlc = dev_to_hdlc(dev);
  37. int (*old_ioctl)(struct net_device *, struct ifreq *, int);
  38. int result;
  39. dev->ml_priv = &state(hdlc)->syncppp_ptr;
  40. state(hdlc)->syncppp_ptr = &state(hdlc)->pppdev;
  41. state(hdlc)->pppdev.dev = dev;
  42. old_ioctl = dev->do_ioctl;
  43. state(hdlc)->old_change_mtu = dev->change_mtu;
  44. sppp_attach(&state(hdlc)->pppdev);
  45. /* sppp_attach nukes them. We don't need syncppp's ioctl */
  46. dev->do_ioctl = old_ioctl;
  47. state(hdlc)->pppdev.sppp.pp_flags &= ~PP_CISCO;
  48. dev->type = ARPHRD_PPP;
  49. result = sppp_open(dev);
  50. if (result) {
  51. sppp_detach(dev);
  52. return result;
  53. }
  54. return 0;
  55. }
  56. static void ppp_close(struct net_device *dev)
  57. {
  58. hdlc_device *hdlc = dev_to_hdlc(dev);
  59. sppp_close(dev);
  60. sppp_detach(dev);
  61. dev->change_mtu = state(hdlc)->old_change_mtu;
  62. dev->mtu = HDLC_MAX_MTU;
  63. dev->hard_header_len = 16;
  64. }
  65. static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
  66. {
  67. return __constant_htons(ETH_P_WAN_PPP);
  68. }
  69. static struct hdlc_proto proto = {
  70. .open = ppp_open,
  71. .close = ppp_close,
  72. .type_trans = ppp_type_trans,
  73. .ioctl = ppp_ioctl,
  74. .module = THIS_MODULE,
  75. };
  76. static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
  77. {
  78. hdlc_device *hdlc = dev_to_hdlc(dev);
  79. int result;
  80. switch (ifr->ifr_settings.type) {
  81. case IF_GET_PROTO:
  82. if (dev_to_hdlc(dev)->proto != &proto)
  83. return -EINVAL;
  84. ifr->ifr_settings.type = IF_PROTO_PPP;
  85. return 0; /* return protocol only, no settable parameters */
  86. case IF_PROTO_PPP:
  87. if(!capable(CAP_NET_ADMIN))
  88. return -EPERM;
  89. if(dev->flags & IFF_UP)
  90. return -EBUSY;
  91. /* no settable parameters */
  92. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  93. if (result)
  94. return result;
  95. result = attach_hdlc_protocol(dev, &proto,
  96. sizeof(struct ppp_state));
  97. if (result)
  98. return result;
  99. dev->hard_start_xmit = hdlc->xmit;
  100. dev->type = ARPHRD_PPP;
  101. netif_dormant_off(dev);
  102. return 0;
  103. }
  104. return -EINVAL;
  105. }
  106. static int __init mod_init(void)
  107. {
  108. register_hdlc_protocol(&proto);
  109. return 0;
  110. }
  111. static void __exit mod_exit(void)
  112. {
  113. unregister_hdlc_protocol(&proto);
  114. }
  115. module_init(mod_init);
  116. module_exit(mod_exit);
  117. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  118. MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
  119. MODULE_LICENSE("GPL v2");