pppox.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /** -*- linux-c -*- ***********************************************************
  2. * Linux PPP over X/Ethernet (PPPoX/PPPoE) Sockets
  3. *
  4. * PPPoX --- Generic PPP encapsulation socket family
  5. * PPPoE --- PPP over Ethernet (RFC 2516)
  6. *
  7. *
  8. * Version: 0.5.2
  9. *
  10. * Author: Michal Ostrowski <mostrows@speakeasy.net>
  11. *
  12. * 051000 : Initialization cleanup
  13. *
  14. * License:
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. */
  21. #include <linux/string.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/compat.h>
  25. #include <linux/errno.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/net.h>
  28. #include <linux/init.h>
  29. #include <linux/if_pppox.h>
  30. #include <linux/ppp_defs.h>
  31. #include <linux/ppp-ioctl.h>
  32. #include <linux/ppp_channel.h>
  33. #include <linux/kmod.h>
  34. #include <net/sock.h>
  35. #include <linux/uaccess.h>
  36. static const struct pppox_proto *pppox_protos[PX_MAX_PROTO + 1];
  37. int register_pppox_proto(int proto_num, const struct pppox_proto *pp)
  38. {
  39. if (proto_num < 0 || proto_num > PX_MAX_PROTO)
  40. return -EINVAL;
  41. if (pppox_protos[proto_num])
  42. return -EALREADY;
  43. pppox_protos[proto_num] = pp;
  44. return 0;
  45. }
  46. void unregister_pppox_proto(int proto_num)
  47. {
  48. if (proto_num >= 0 && proto_num <= PX_MAX_PROTO)
  49. pppox_protos[proto_num] = NULL;
  50. }
  51. void pppox_unbind_sock(struct sock *sk)
  52. {
  53. /* Clear connection to ppp device, if attached. */
  54. if (sk->sk_state & (PPPOX_BOUND | PPPOX_CONNECTED)) {
  55. ppp_unregister_channel(&pppox_sk(sk)->chan);
  56. sk->sk_state = PPPOX_DEAD;
  57. }
  58. }
  59. EXPORT_SYMBOL(register_pppox_proto);
  60. EXPORT_SYMBOL(unregister_pppox_proto);
  61. EXPORT_SYMBOL(pppox_unbind_sock);
  62. int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  63. {
  64. struct sock *sk = sock->sk;
  65. struct pppox_sock *po = pppox_sk(sk);
  66. int rc;
  67. lock_sock(sk);
  68. switch (cmd) {
  69. case PPPIOCGCHAN: {
  70. int index;
  71. rc = -ENOTCONN;
  72. if (!(sk->sk_state & PPPOX_CONNECTED))
  73. break;
  74. rc = -EINVAL;
  75. index = ppp_channel_index(&po->chan);
  76. if (put_user(index , (int __user *) arg))
  77. break;
  78. rc = 0;
  79. sk->sk_state |= PPPOX_BOUND;
  80. break;
  81. }
  82. default:
  83. rc = pppox_protos[sk->sk_protocol]->ioctl ?
  84. pppox_protos[sk->sk_protocol]->ioctl(sock, cmd, arg) : -ENOTTY;
  85. }
  86. release_sock(sk);
  87. return rc;
  88. }
  89. EXPORT_SYMBOL(pppox_ioctl);
  90. #ifdef CONFIG_COMPAT
  91. int pppox_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  92. {
  93. if (cmd == PPPOEIOCSFWD32)
  94. cmd = PPPOEIOCSFWD;
  95. return pppox_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
  96. }
  97. EXPORT_SYMBOL(pppox_compat_ioctl);
  98. #endif
  99. static int pppox_create(struct net *net, struct socket *sock, int protocol,
  100. int kern)
  101. {
  102. int rc = -EPROTOTYPE;
  103. if (protocol < 0 || protocol > PX_MAX_PROTO)
  104. goto out;
  105. rc = -EPROTONOSUPPORT;
  106. if (!pppox_protos[protocol])
  107. request_module("net-pf-%d-proto-%d", PF_PPPOX, protocol);
  108. if (!pppox_protos[protocol] ||
  109. !try_module_get(pppox_protos[protocol]->owner))
  110. goto out;
  111. rc = pppox_protos[protocol]->create(net, sock, kern);
  112. module_put(pppox_protos[protocol]->owner);
  113. out:
  114. return rc;
  115. }
  116. static const struct net_proto_family pppox_proto_family = {
  117. .family = PF_PPPOX,
  118. .create = pppox_create,
  119. .owner = THIS_MODULE,
  120. };
  121. static int __init pppox_init(void)
  122. {
  123. return sock_register(&pppox_proto_family);
  124. }
  125. static void __exit pppox_exit(void)
  126. {
  127. sock_unregister(PF_PPPOX);
  128. }
  129. module_init(pppox_init);
  130. module_exit(pppox_exit);
  131. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  132. MODULE_DESCRIPTION("PPP over Ethernet driver (generic socket layer)");
  133. MODULE_LICENSE("GPL");
  134. MODULE_ALIAS_NETPROTO(PF_PPPOX);