Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Paul Mackerras | 4b32da2b | 2012-03-04 12:56:55 +0000 | [diff] [blame] | 31 | #include <linux/ppp-ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/ppp_channel.h> |
James Chapman | 65def81 | 2007-04-30 00:21:02 -0700 | [diff] [blame] | 33 | #include <linux/kmod.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <net/sock.h> |
| 36 | |
| 37 | #include <asm/uaccess.h> |
| 38 | |
Eric Dumazet | 756e64a | 2010-09-21 06:43:54 +0000 | [diff] [blame] | 39 | static const struct pppox_proto *pppox_protos[PX_MAX_PROTO + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Eric Dumazet | 756e64a | 2010-09-21 06:43:54 +0000 | [diff] [blame] | 41 | int register_pppox_proto(int proto_num, const struct pppox_proto *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | if (proto_num < 0 || proto_num > PX_MAX_PROTO) |
| 44 | return -EINVAL; |
| 45 | if (pppox_protos[proto_num]) |
| 46 | return -EALREADY; |
| 47 | pppox_protos[proto_num] = pp; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | void unregister_pppox_proto(int proto_num) |
| 52 | { |
| 53 | if (proto_num >= 0 && proto_num <= PX_MAX_PROTO) |
| 54 | pppox_protos[proto_num] = NULL; |
| 55 | } |
| 56 | |
| 57 | void pppox_unbind_sock(struct sock *sk) |
| 58 | { |
| 59 | /* Clear connection to ppp device, if attached. */ |
| 60 | |
Guillaume Nault | a8acce6 | 2015-11-19 12:53:21 +0100 | [diff] [blame] | 61 | if (sk->sk_state & (PPPOX_BOUND | PPPOX_CONNECTED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | ppp_unregister_channel(&pppox_sk(sk)->chan); |
| 63 | sk->sk_state = PPPOX_DEAD; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | EXPORT_SYMBOL(register_pppox_proto); |
| 68 | EXPORT_SYMBOL(unregister_pppox_proto); |
| 69 | EXPORT_SYMBOL(pppox_unbind_sock); |
| 70 | |
David S. Miller | 17ba15f | 2005-12-27 20:57:40 -0800 | [diff] [blame] | 71 | int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | struct sock *sk = sock->sk; |
| 74 | struct pppox_sock *po = pppox_sk(sk); |
Florian Zumbiehl | 86c1dcf | 2007-07-30 17:48:23 -0700 | [diff] [blame] | 75 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | lock_sock(sk); |
| 78 | |
| 79 | switch (cmd) { |
| 80 | case PPPIOCGCHAN: { |
| 81 | int index; |
| 82 | rc = -ENOTCONN; |
| 83 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
| 84 | break; |
| 85 | |
| 86 | rc = -EINVAL; |
| 87 | index = ppp_channel_index(&po->chan); |
| 88 | if (put_user(index , (int __user *) arg)) |
| 89 | break; |
| 90 | |
| 91 | rc = 0; |
| 92 | sk->sk_state |= PPPOX_BOUND; |
| 93 | break; |
| 94 | } |
| 95 | default: |
Florian Zumbiehl | 86c1dcf | 2007-07-30 17:48:23 -0700 | [diff] [blame] | 96 | rc = pppox_protos[sk->sk_protocol]->ioctl ? |
| 97 | pppox_protos[sk->sk_protocol]->ioctl(sock, cmd, arg) : -ENOTTY; |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | release_sock(sk); |
| 101 | return rc; |
| 102 | } |
| 103 | |
David S. Miller | 17ba15f | 2005-12-27 20:57:40 -0800 | [diff] [blame] | 104 | EXPORT_SYMBOL(pppox_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 106 | static int pppox_create(struct net *net, struct socket *sock, int protocol, |
| 107 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | int rc = -EPROTOTYPE; |
| 110 | |
| 111 | if (protocol < 0 || protocol > PX_MAX_PROTO) |
| 112 | goto out; |
| 113 | |
| 114 | rc = -EPROTONOSUPPORT; |
Johannes Berg | a65e5d7 | 2008-07-09 10:28:38 +0200 | [diff] [blame] | 115 | if (!pppox_protos[protocol]) |
Guillaume Nault | 681b4d8 | 2015-12-02 16:27:39 +0100 | [diff] [blame] | 116 | request_module("net-pf-%d-proto-%d", PF_PPPOX, protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | if (!pppox_protos[protocol] || |
| 118 | !try_module_get(pppox_protos[protocol]->owner)) |
| 119 | goto out; |
| 120 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 121 | rc = pppox_protos[protocol]->create(net, sock, kern); |
David S. Miller | 17ba15f | 2005-12-27 20:57:40 -0800 | [diff] [blame] | 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | module_put(pppox_protos[protocol]->owner); |
| 124 | out: |
| 125 | return rc; |
| 126 | } |
| 127 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 128 | static const struct net_proto_family pppox_proto_family = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | .family = PF_PPPOX, |
| 130 | .create = pppox_create, |
| 131 | .owner = THIS_MODULE, |
| 132 | }; |
| 133 | |
| 134 | static int __init pppox_init(void) |
| 135 | { |
| 136 | return sock_register(&pppox_proto_family); |
| 137 | } |
| 138 | |
| 139 | static void __exit pppox_exit(void) |
| 140 | { |
| 141 | sock_unregister(PF_PPPOX); |
| 142 | } |
| 143 | |
| 144 | module_init(pppox_init); |
| 145 | module_exit(pppox_exit); |
| 146 | |
| 147 | MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>"); |
| 148 | MODULE_DESCRIPTION("PPP over Ethernet driver (generic socket layer)"); |
| 149 | MODULE_LICENSE("GPL"); |
Guillaume Nault | 681b4d8 | 2015-12-02 16:27:39 +0100 | [diff] [blame] | 150 | MODULE_ALIAS_NETPROTO(PF_PPPOX); |