xref: /openbmc/linux/drivers/net/ppp/pppox.c (revision 4e95bc26)
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/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>
26 #include <linux/ppp-ioctl.h>
27 #include <linux/ppp_channel.h>
28 #include <linux/kmod.h>
29 
30 #include <net/sock.h>
31 
32 #include <linux/uaccess.h>
33 
34 static const struct pppox_proto *pppox_protos[PX_MAX_PROTO + 1];
35 
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 
46 void 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 
52 void pppox_unbind_sock(struct sock *sk)
53 {
54 	/* Clear connection to ppp device, if attached. */
55 
56 	if (sk->sk_state & (PPPOX_BOUND | PPPOX_CONNECTED)) {
57 		ppp_unregister_channel(&pppox_sk(sk)->chan);
58 		sk->sk_state = PPPOX_DEAD;
59 	}
60 }
61 
62 EXPORT_SYMBOL(register_pppox_proto);
63 EXPORT_SYMBOL(unregister_pppox_proto);
64 EXPORT_SYMBOL(pppox_unbind_sock);
65 
66 int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
67 {
68 	struct sock *sk = sock->sk;
69 	struct pppox_sock *po = pppox_sk(sk);
70 	int rc;
71 
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:
91 		rc = pppox_protos[sk->sk_protocol]->ioctl ?
92 			pppox_protos[sk->sk_protocol]->ioctl(sock, cmd, arg) : -ENOTTY;
93 	}
94 
95 	release_sock(sk);
96 	return rc;
97 }
98 
99 EXPORT_SYMBOL(pppox_ioctl);
100 
101 static int pppox_create(struct net *net, struct socket *sock, int protocol,
102 			int kern)
103 {
104 	int rc = -EPROTOTYPE;
105 
106 	if (protocol < 0 || protocol > PX_MAX_PROTO)
107 		goto out;
108 
109 	rc = -EPROTONOSUPPORT;
110 	if (!pppox_protos[protocol])
111 		request_module("net-pf-%d-proto-%d", PF_PPPOX, protocol);
112 	if (!pppox_protos[protocol] ||
113 	    !try_module_get(pppox_protos[protocol]->owner))
114 		goto out;
115 
116 	rc = pppox_protos[protocol]->create(net, sock, kern);
117 
118 	module_put(pppox_protos[protocol]->owner);
119 out:
120 	return rc;
121 }
122 
123 static const struct net_proto_family pppox_proto_family = {
124 	.family	= PF_PPPOX,
125 	.create	= pppox_create,
126 	.owner	= THIS_MODULE,
127 };
128 
129 static int __init pppox_init(void)
130 {
131 	return sock_register(&pppox_proto_family);
132 }
133 
134 static void __exit pppox_exit(void)
135 {
136 	sock_unregister(PF_PPPOX);
137 }
138 
139 module_init(pppox_init);
140 module_exit(pppox_exit);
141 
142 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
143 MODULE_DESCRIPTION("PPP over Ethernet driver (generic socket layer)");
144 MODULE_LICENSE("GPL");
145 MODULE_ALIAS_NETPROTO(PF_PPPOX);
146