blob: 5ef422a43d70b479e44991062cbdc03de9aa8951 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/** -*- linux-c -*- ***********************************************************
3 * Linux PPP over X/Ethernet (PPPoX/PPPoE) Sockets
4 *
5 * PPPoX --- Generic PPP encapsulation socket family
6 * PPPoE --- PPP over Ethernet (RFC 2516)
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Version: 0.5.2
9 *
10 * Author: Michal Ostrowski <mostrows@speakeasy.net>
11 *
12 * 051000 : Initialization cleanup
13 *
14 * License:
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#include <linux/string.h>
18#include <linux/module.h>
19#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
21#include <linux/netdevice.h>
22#include <linux/net.h>
23#include <linux/init.h>
24#include <linux/if_pppox.h>
25#include <linux/ppp_defs.h>
Paul Mackerras4b32da2b2012-03-04 12:56:55 +000026#include <linux/ppp-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/ppp_channel.h>
James Chapman65def812007-04-30 00:21:02 -070028#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <net/sock.h>
31
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080032#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Eric Dumazet756e64a2010-09-21 06:43:54 +000034static const struct pppox_proto *pppox_protos[PX_MAX_PROTO + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Eric Dumazet756e64a2010-09-21 06:43:54 +000036int register_pppox_proto(int proto_num, const struct pppox_proto *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
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
46void 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
52void pppox_unbind_sock(struct sock *sk)
53{
54 /* Clear connection to ppp device, if attached. */
55
Guillaume Naulta8acce62015-11-19 12:53:21 +010056 if (sk->sk_state & (PPPOX_BOUND | PPPOX_CONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 ppp_unregister_channel(&pppox_sk(sk)->chan);
58 sk->sk_state = PPPOX_DEAD;
59 }
60}
61
62EXPORT_SYMBOL(register_pppox_proto);
63EXPORT_SYMBOL(unregister_pppox_proto);
64EXPORT_SYMBOL(pppox_unbind_sock);
65
David S. Miller17ba15f2005-12-27 20:57:40 -080066int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct sock *sk = sock->sk;
69 struct pppox_sock *po = pppox_sk(sk);
Florian Zumbiehl86c1dcf2007-07-30 17:48:23 -070070 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 lock_sock(sk);
73
74 switch (cmd) {
75 case PPPIOCGCHAN: {
76 int index;
77 rc = -ENOTCONN;
78 if (!(sk->sk_state & PPPOX_CONNECTED))
79 break;
80
81 rc = -EINVAL;
82 index = ppp_channel_index(&po->chan);
83 if (put_user(index , (int __user *) arg))
84 break;
85
86 rc = 0;
87 sk->sk_state |= PPPOX_BOUND;
88 break;
89 }
90 default:
Florian Zumbiehl86c1dcf2007-07-30 17:48:23 -070091 rc = pppox_protos[sk->sk_protocol]->ioctl ?
92 pppox_protos[sk->sk_protocol]->ioctl(sock, cmd, arg) : -ENOTTY;
93 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 release_sock(sk);
96 return rc;
97}
98
David S. Miller17ba15f2005-12-27 20:57:40 -080099EXPORT_SYMBOL(pppox_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Eric Paris3f378b62009-11-05 22:18:14 -0800101static int pppox_create(struct net *net, struct socket *sock, int protocol,
102 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 int rc = -EPROTOTYPE;
105
106 if (protocol < 0 || protocol > PX_MAX_PROTO)
107 goto out;
108
109 rc = -EPROTONOSUPPORT;
Johannes Berga65e5d72008-07-09 10:28:38 +0200110 if (!pppox_protos[protocol])
Guillaume Nault681b4d82015-12-02 16:27:39 +0100111 request_module("net-pf-%d-proto-%d", PF_PPPOX, protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (!pppox_protos[protocol] ||
113 !try_module_get(pppox_protos[protocol]->owner))
114 goto out;
115
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500116 rc = pppox_protos[protocol]->create(net, sock, kern);
David S. Miller17ba15f2005-12-27 20:57:40 -0800117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 module_put(pppox_protos[protocol]->owner);
119out:
120 return rc;
121}
122
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000123static const struct net_proto_family pppox_proto_family = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .family = PF_PPPOX,
125 .create = pppox_create,
126 .owner = THIS_MODULE,
127};
128
129static int __init pppox_init(void)
130{
131 return sock_register(&pppox_proto_family);
132}
133
134static void __exit pppox_exit(void)
135{
136 sock_unregister(PF_PPPOX);
137}
138
139module_init(pppox_init);
140module_exit(pppox_exit);
141
142MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
143MODULE_DESCRIPTION("PPP over Ethernet driver (generic socket layer)");
144MODULE_LICENSE("GPL");
Guillaume Nault681b4d82015-12-02 16:27:39 +0100145MODULE_ALIAS_NETPROTO(PF_PPPOX);