pppox.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/errno.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/net.h>
  27. #include <linux/init.h>
  28. #include <linux/if_pppox.h>
  29. #include <linux/ppp_defs.h>
  30. #include <linux/ppp-ioctl.h>
  31. #include <linux/ppp_channel.h>
  32. #include <linux/kmod.h>
  33. #include <net/sock.h>
  34. #include <asm/uaccess.h>
  35. static const struct pppox_proto *pppox_protos[PX_MAX_PROTO + 1];
  36. int register_pppox_proto(int proto_num, const struct pppox_proto *pp)
  37. {
  38. if (proto_num < 0 || proto_num > PX_MAX_PROTO)
  39. return -EINVAL;
  40. if (pppox_protos[proto_num])
  41. return -EALREADY;
  42. pppox_protos[proto_num] = pp;
  43. return 0;
  44. }
  45. void unregister_pppox_proto(int proto_num)
  46. {
  47. if (proto_num >= 0 && proto_num <= PX_MAX_PROTO)
  48. pppox_protos[proto_num] = NULL;
  49. }
  50. void pppox_unbind_sock(struct sock *sk)
  51. {
  52. /* Clear connection to ppp device, if attached. */
  53. if (sk->sk_state & (PPPOX_BOUND | PPPOX_CONNECTED | PPPOX_ZOMBIE)) {
  54. ppp_unregister_channel(&pppox_sk(sk)->chan);
  55. sk->sk_state = PPPOX_DEAD;
  56. }
  57. }
  58. EXPORT_SYMBOL(register_pppox_proto);
  59. EXPORT_SYMBOL(unregister_pppox_proto);
  60. EXPORT_SYMBOL(pppox_unbind_sock);
  61. int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  62. {
  63. struct sock *sk = sock->sk;
  64. struct pppox_sock *po = pppox_sk(sk);
  65. int rc;
  66. lock_sock(sk);
  67. switch (cmd) {
  68. case PPPIOCGCHAN: {
  69. int index;
  70. rc = -ENOTCONN;
  71. if (!(sk->sk_state & PPPOX_CONNECTED))
  72. break;
  73. rc = -EINVAL;
  74. index = ppp_channel_index(&po->chan);
  75. if (put_user(index , (int __user *) arg))
  76. break;
  77. rc = 0;
  78. sk->sk_state |= PPPOX_BOUND;
  79. break;
  80. }
  81. default:
  82. rc = pppox_protos[sk->sk_protocol]->ioctl ?
  83. pppox_protos[sk->sk_protocol]->ioctl(sock, cmd, arg) : -ENOTTY;
  84. }
  85. release_sock(sk);
  86. return rc;
  87. }
  88. EXPORT_SYMBOL(pppox_ioctl);
  89. static int pppox_create(struct net *net, struct socket *sock, int protocol,
  90. int kern)
  91. {
  92. int rc = -EPROTOTYPE;
  93. if (protocol < 0 || protocol > PX_MAX_PROTO)
  94. goto out;
  95. rc = -EPROTONOSUPPORT;
  96. if (!pppox_protos[protocol])
  97. request_module("pppox-proto-%d", protocol);
  98. if (!pppox_protos[protocol] ||
  99. !try_module_get(pppox_protos[protocol]->owner))
  100. goto out;
  101. rc = pppox_protos[protocol]->create(net, sock, kern);
  102. module_put(pppox_protos[protocol]->owner);
  103. out:
  104. return rc;
  105. }
  106. static const struct net_proto_family pppox_proto_family = {
  107. .family = PF_PPPOX,
  108. .create = pppox_create,
  109. .owner = THIS_MODULE,
  110. };
  111. static int __init pppox_init(void)
  112. {
  113. return sock_register(&pppox_proto_family);
  114. }
  115. static void __exit pppox_exit(void)
  116. {
  117. sock_unregister(PF_PPPOX);
  118. }
  119. module_init(pppox_init);
  120. module_exit(pppox_exit);
  121. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  122. MODULE_DESCRIPTION("PPP over Ethernet driver (generic socket layer)");
  123. MODULE_LICENSE("GPL");