1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** -*- 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 *
8 * Version: 0.5.2
9 *
10 * Author: Michal Ostrowski <mostrows@speakeasy.net>
11 *
12 * 051000 : Initialization cleanup
13 *
14 * License:
15 */
16
17 #include <linux/string.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/compat.h>
21 #include <linux/errno.h>
22 #include <linux/netdevice.h>
23 #include <linux/net.h>
24 #include <linux/init.h>
25 #include <linux/if_pppox.h>
26 #include <linux/ppp_defs.h>
27 #include <linux/ppp-ioctl.h>
28 #include <linux/ppp_channel.h>
29 #include <linux/kmod.h>
30
31 #include <net/sock.h>
32
33 #include <linux/uaccess.h>
34
35 static const struct pppox_proto *pppox_protos[PX_MAX_PROTO + 1];
36
register_pppox_proto(int proto_num,const struct pppox_proto * pp)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
unregister_pppox_proto(int proto_num)47 void unregister_pppox_proto(int proto_num)
48 {
49 if (proto_num >= 0 && proto_num <= PX_MAX_PROTO)
50 pppox_protos[proto_num] = NULL;
51 }
52
pppox_unbind_sock(struct sock * sk)53 void pppox_unbind_sock(struct sock *sk)
54 {
55 /* Clear connection to ppp device, if attached. */
56
57 if (sk->sk_state & (PPPOX_BOUND | PPPOX_CONNECTED)) {
58 ppp_unregister_channel(&pppox_sk(sk)->chan);
59 sk->sk_state = PPPOX_DEAD;
60 }
61 }
62
63 EXPORT_SYMBOL(register_pppox_proto);
64 EXPORT_SYMBOL(unregister_pppox_proto);
65 EXPORT_SYMBOL(pppox_unbind_sock);
66
pppox_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)67 int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
68 {
69 struct sock *sk = sock->sk;
70 struct pppox_sock *po = pppox_sk(sk);
71 int rc;
72
73 lock_sock(sk);
74
75 switch (cmd) {
76 case PPPIOCGCHAN: {
77 struct sk_buff *skb;
78 int index;
79
80 rc = -ENOTCONN;
81 if (!(sk->sk_state & PPPOX_CONNECTED))
82 break;
83
84 rc = -EINVAL;
85 index = ppp_channel_index(&po->chan);
86 if (put_user(index , (int __user *) arg))
87 break;
88
89 rc = 0;
90 /* PPPIOCGCHAN historically marks the userspace handoff to
91 * generic PPP; pppd then attaches the returned channel to
92 * /dev/ppp.
93 */
94 sk->sk_state |= PPPOX_BOUND;
95 /* Let lockless receive paths finish queueing against the old
96 * state.
97 */
98 synchronize_net();
99 /* Drain packets queued before the handoff because a bound
100 * socket is no longer readable.
101 */
102 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
103 skb_orphan(skb);
104 ppp_input(&po->chan, skb);
105 }
106 break;
107 }
108 default:
109 rc = pppox_protos[sk->sk_protocol]->ioctl ?
110 pppox_protos[sk->sk_protocol]->ioctl(sock, cmd, arg) : -ENOTTY;
111 }
112
113 release_sock(sk);
114 return rc;
115 }
116
117 EXPORT_SYMBOL(pppox_ioctl);
118
119 #ifdef CONFIG_COMPAT
pppox_compat_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)120 int pppox_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
121 {
122 return pppox_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
123 }
124
125 EXPORT_SYMBOL(pppox_compat_ioctl);
126 #endif
127
pppox_create(struct net * net,struct socket * sock,int protocol,int kern)128 static int pppox_create(struct net *net, struct socket *sock, int protocol,
129 int kern)
130 {
131 int rc = -EPROTOTYPE;
132
133 if (protocol < 0 || protocol > PX_MAX_PROTO)
134 goto out;
135
136 rc = -EPROTONOSUPPORT;
137 if (!pppox_protos[protocol])
138 request_module("net-pf-%d-proto-%d", PF_PPPOX, protocol);
139 if (!pppox_protos[protocol] ||
140 !try_module_get(pppox_protos[protocol]->owner))
141 goto out;
142
143 rc = pppox_protos[protocol]->create(net, sock, kern);
144
145 module_put(pppox_protos[protocol]->owner);
146 out:
147 return rc;
148 }
149
150 static const struct net_proto_family pppox_proto_family = {
151 .family = PF_PPPOX,
152 .create = pppox_create,
153 .owner = THIS_MODULE,
154 };
155
pppox_init(void)156 static int __init pppox_init(void)
157 {
158 return sock_register(&pppox_proto_family);
159 }
160
pppox_exit(void)161 static void __exit pppox_exit(void)
162 {
163 sock_unregister(PF_PPPOX);
164 }
165
166 module_init(pppox_init);
167 module_exit(pppox_exit);
168
169 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
170 MODULE_DESCRIPTION("PPP over Ethernet driver (generic socket layer)");
171 MODULE_LICENSE("GPL");
172 MODULE_ALIAS_NETPROTO(PF_PPPOX);
173