hdlc_ppp.c 3.2 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->change_mtu = state(hdlc)->old_change_mtu;
  63. dev->mtu = HDLC_MAX_MTU;
  64. dev->hard_header_len = 16;
  65. }
  66. static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
  67. {
  68. return __constant_htons(ETH_P_WAN_PPP);
  69. }
  70. static struct hdlc_proto proto = {
  71. .open = ppp_open,
  72. .close = ppp_close,
  73. .type_trans = ppp_type_trans,
  74. .ioctl = ppp_ioctl,
  75. .module = THIS_MODULE,
  76. };
  77. static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
  78. {
  79. hdlc_device *hdlc = dev_to_hdlc(dev);
  80. int result;
  81. switch (ifr->ifr_settings.type) {
  82. case IF_GET_PROTO:
  83. if (dev_to_hdlc(dev)->proto != &proto)
  84. return -EINVAL;
  85. ifr->ifr_settings.type = IF_PROTO_PPP;
  86. return 0; /* return protocol only, no settable parameters */
  87. case IF_PROTO_PPP:
  88. if(!capable(CAP_NET_ADMIN))
  89. return -EPERM;
  90. if(dev->flags & IFF_UP)
  91. return -EBUSY;
  92. /* no settable parameters */
  93. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  94. if (result)
  95. return result;
  96. result = attach_hdlc_protocol(dev, &proto, NULL,
  97. sizeof(struct ppp_state));
  98. if (result)
  99. return result;
  100. dev->hard_start_xmit = hdlc->xmit;
  101. dev->type = ARPHRD_PPP;
  102. netif_dormant_off(dev);
  103. return 0;
  104. }
  105. return -EINVAL;
  106. }
  107. static int __init mod_init(void)
  108. {
  109. register_hdlc_protocol(&proto);
  110. return 0;
  111. }
  112. static void __exit mod_exit(void)
  113. {
  114. unregister_hdlc_protocol(&proto);
  115. }
  116. module_init(mod_init);
  117. module_exit(mod_exit);
  118. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  119. MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
  120. MODULE_LICENSE("GPL v2");