pppox.c 3.2 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/slab.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/if_ppp.h>
  32. #include <linux/ppp_channel.h>
  33. #include <net/sock.h>
  34. #include <asm/uaccess.h>
  35. static struct pppox_proto *pppox_protos[PX_MAX_PROTO + 1];
  36. int register_pppox_proto(int proto_num, 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_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 = 0;
  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. if (pppox_protos[sk->sk_protocol]->ioctl)
  83. rc = pppox_protos[sk->sk_protocol]->ioctl(sock, cmd,
  84. arg);
  85. break;
  86. };
  87. release_sock(sk);
  88. return rc;
  89. }
  90. EXPORT_SYMBOL(pppox_ioctl);
  91. static int pppox_create(struct socket *sock, int protocol)
  92. {
  93. int rc = -EPROTOTYPE;
  94. if (protocol < 0 || protocol > PX_MAX_PROTO)
  95. goto out;
  96. rc = -EPROTONOSUPPORT;
  97. if (!pppox_protos[protocol] ||
  98. !try_module_get(pppox_protos[protocol]->owner))
  99. goto out;
  100. rc = pppox_protos[protocol]->create(sock);
  101. module_put(pppox_protos[protocol]->owner);
  102. out:
  103. return rc;
  104. }
  105. static struct net_proto_family pppox_proto_family = {
  106. .family = PF_PPPOX,
  107. .create = pppox_create,
  108. .owner = THIS_MODULE,
  109. };
  110. static int __init pppox_init(void)
  111. {
  112. return sock_register(&pppox_proto_family);
  113. }
  114. static void __exit pppox_exit(void)
  115. {
  116. sock_unregister(PF_PPPOX);
  117. }
  118. module_init(pppox_init);
  119. module_exit(pppox_exit);
  120. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  121. MODULE_DESCRIPTION("PPP over Ethernet driver (generic socket layer)");
  122. MODULE_LICENSE("GPL");