hdlc_ppp.c 3.3 KB

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