xref: /openbmc/linux/net/l2tp/l2tp_ppp.c (revision 2b139e6b1ec86e1d3646039965dd79ad05d8dca4)
1fd558d18SJames Chapman /*****************************************************************************
2fd558d18SJames Chapman  * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
3fd558d18SJames Chapman  *
4fd558d18SJames Chapman  * PPPoX    --- Generic PPP encapsulation socket family
5fd558d18SJames Chapman  * PPPoL2TP --- PPP over L2TP (RFC 2661)
6fd558d18SJames Chapman  *
7fd558d18SJames Chapman  * Version:	2.0.0
8fd558d18SJames Chapman  *
9fd558d18SJames Chapman  * Authors:	James Chapman (jchapman@katalix.com)
10fd558d18SJames Chapman  *
11fd558d18SJames Chapman  * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
12fd558d18SJames Chapman  *
13fd558d18SJames Chapman  * License:
14fd558d18SJames Chapman  *		This program is free software; you can redistribute it and/or
15fd558d18SJames Chapman  *		modify it under the terms of the GNU General Public License
16fd558d18SJames Chapman  *		as published by the Free Software Foundation; either version
17fd558d18SJames Chapman  *		2 of the License, or (at your option) any later version.
18fd558d18SJames Chapman  *
19fd558d18SJames Chapman  */
20fd558d18SJames Chapman 
21fd558d18SJames Chapman /* This driver handles only L2TP data frames; control frames are handled by a
22fd558d18SJames Chapman  * userspace application.
23fd558d18SJames Chapman  *
24fd558d18SJames Chapman  * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
25fd558d18SJames Chapman  * attaches it to a bound UDP socket with local tunnel_id / session_id and
26fd558d18SJames Chapman  * peer tunnel_id / session_id set. Data can then be sent or received using
27fd558d18SJames Chapman  * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
28fd558d18SJames Chapman  * can be read or modified using ioctl() or [gs]etsockopt() calls.
29fd558d18SJames Chapman  *
30fd558d18SJames Chapman  * When a PPPoL2TP socket is connected with local and peer session_id values
31fd558d18SJames Chapman  * zero, the socket is treated as a special tunnel management socket.
32fd558d18SJames Chapman  *
33fd558d18SJames Chapman  * Here's example userspace code to create a socket for sending/receiving data
34fd558d18SJames Chapman  * over an L2TP session:-
35fd558d18SJames Chapman  *
36fd558d18SJames Chapman  *	struct sockaddr_pppol2tp sax;
37fd558d18SJames Chapman  *	int fd;
38fd558d18SJames Chapman  *	int session_fd;
39fd558d18SJames Chapman  *
40fd558d18SJames Chapman  *	fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
41fd558d18SJames Chapman  *
42fd558d18SJames Chapman  *	sax.sa_family = AF_PPPOX;
43fd558d18SJames Chapman  *	sax.sa_protocol = PX_PROTO_OL2TP;
44fd558d18SJames Chapman  *	sax.pppol2tp.fd = tunnel_fd;	// bound UDP socket
45fd558d18SJames Chapman  *	sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
46fd558d18SJames Chapman  *	sax.pppol2tp.addr.sin_port = addr->sin_port;
47fd558d18SJames Chapman  *	sax.pppol2tp.addr.sin_family = AF_INET;
48fd558d18SJames Chapman  *	sax.pppol2tp.s_tunnel  = tunnel_id;
49fd558d18SJames Chapman  *	sax.pppol2tp.s_session = session_id;
50fd558d18SJames Chapman  *	sax.pppol2tp.d_tunnel  = peer_tunnel_id;
51fd558d18SJames Chapman  *	sax.pppol2tp.d_session = peer_session_id;
52fd558d18SJames Chapman  *
53fd558d18SJames Chapman  *	session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
54fd558d18SJames Chapman  *
55fd558d18SJames Chapman  * A pppd plugin that allows PPP traffic to be carried over L2TP using
56fd558d18SJames Chapman  * this driver is available from the OpenL2TP project at
57fd558d18SJames Chapman  * http://openl2tp.sourceforge.net.
58fd558d18SJames Chapman  */
59fd558d18SJames Chapman 
60a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
61a4ca44faSJoe Perches 
62fd558d18SJames Chapman #include <linux/module.h>
63fd558d18SJames Chapman #include <linux/string.h>
64fd558d18SJames Chapman #include <linux/list.h>
65fd558d18SJames Chapman #include <linux/uaccess.h>
66fd558d18SJames Chapman 
67fd558d18SJames Chapman #include <linux/kernel.h>
68fd558d18SJames Chapman #include <linux/spinlock.h>
69fd558d18SJames Chapman #include <linux/kthread.h>
70fd558d18SJames Chapman #include <linux/sched.h>
71fd558d18SJames Chapman #include <linux/slab.h>
72fd558d18SJames Chapman #include <linux/errno.h>
73fd558d18SJames Chapman #include <linux/jiffies.h>
74fd558d18SJames Chapman 
75fd558d18SJames Chapman #include <linux/netdevice.h>
76fd558d18SJames Chapman #include <linux/net.h>
77fd558d18SJames Chapman #include <linux/inetdevice.h>
78fd558d18SJames Chapman #include <linux/skbuff.h>
79fd558d18SJames Chapman #include <linux/init.h>
80fd558d18SJames Chapman #include <linux/ip.h>
81fd558d18SJames Chapman #include <linux/udp.h>
82fd558d18SJames Chapman #include <linux/if_pppox.h>
83fd558d18SJames Chapman #include <linux/if_pppol2tp.h>
84fd558d18SJames Chapman #include <net/sock.h>
85fd558d18SJames Chapman #include <linux/ppp_channel.h>
86fd558d18SJames Chapman #include <linux/ppp_defs.h>
874b32da2bSPaul Mackerras #include <linux/ppp-ioctl.h>
88fd558d18SJames Chapman #include <linux/file.h>
89fd558d18SJames Chapman #include <linux/hash.h>
90fd558d18SJames Chapman #include <linux/sort.h>
91fd558d18SJames Chapman #include <linux/proc_fs.h>
92309795f4SJames Chapman #include <linux/l2tp.h>
93fd558d18SJames Chapman #include <linux/nsproxy.h>
94fd558d18SJames Chapman #include <net/net_namespace.h>
95fd558d18SJames Chapman #include <net/netns/generic.h>
96fd558d18SJames Chapman #include <net/dst.h>
97fd558d18SJames Chapman #include <net/ip.h>
98fd558d18SJames Chapman #include <net/udp.h>
99fd558d18SJames Chapman #include <net/xfrm.h>
100cf2f5c88STom Parkin #include <net/inet_common.h>
101fd558d18SJames Chapman 
102fd558d18SJames Chapman #include <asm/byteorder.h>
10360063497SArun Sharma #include <linux/atomic.h>
104fd558d18SJames Chapman 
105fd558d18SJames Chapman #include "l2tp_core.h"
106fd558d18SJames Chapman 
107fd558d18SJames Chapman #define PPPOL2TP_DRV_VERSION	"V2.0"
108fd558d18SJames Chapman 
109fd558d18SJames Chapman /* Space for UDP, L2TP and PPP headers */
110fd558d18SJames Chapman #define PPPOL2TP_HEADER_OVERHEAD	40
111fd558d18SJames Chapman 
112fd558d18SJames Chapman /* Number of bytes to build transmit L2TP headers.
113fd558d18SJames Chapman  * Unfortunately the size is different depending on whether sequence numbers
114fd558d18SJames Chapman  * are enabled.
115fd558d18SJames Chapman  */
116fd558d18SJames Chapman #define PPPOL2TP_L2TP_HDR_SIZE_SEQ		10
117fd558d18SJames Chapman #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ		6
118fd558d18SJames Chapman 
119fd558d18SJames Chapman /* Private data of each session. This data lives at the end of struct
120fd558d18SJames Chapman  * l2tp_session, referenced via session->priv[].
121fd558d18SJames Chapman  */
122fd558d18SJames Chapman struct pppol2tp_session {
123fd558d18SJames Chapman 	int			owner;		/* pid that opened the socket */
124fd558d18SJames Chapman 
125ee40fb2eSGuillaume Nault 	struct mutex		sk_lock;	/* Protects .sk */
126ee40fb2eSGuillaume Nault 	struct sock __rcu	*sk;		/* Pointer to the session
127fd558d18SJames Chapman 						 * PPPoX socket */
128ee40fb2eSGuillaume Nault 	struct sock		*__sk;		/* Copy of .sk, for cleanup */
129ee40fb2eSGuillaume Nault 	struct rcu_head		rcu;		/* For asynchronous release */
130fd558d18SJames Chapman 	int			flags;		/* accessed by PPPIOCGFLAGS.
131fd558d18SJames Chapman 						 * Unused. */
132fd558d18SJames Chapman };
133fd558d18SJames Chapman 
134fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
135fd558d18SJames Chapman 
136d7100da0Sstephen hemminger static const struct ppp_channel_ops pppol2tp_chan_ops = {
137d7100da0Sstephen hemminger 	.start_xmit =  pppol2tp_xmit,
138d7100da0Sstephen hemminger };
139d7100da0Sstephen hemminger 
140fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops;
141fd558d18SJames Chapman 
142ee40fb2eSGuillaume Nault /* Retrieves the pppol2tp socket associated to a session.
143ee40fb2eSGuillaume Nault  * A reference is held on the returned socket, so this function must be paired
144ee40fb2eSGuillaume Nault  * with sock_put().
145ee40fb2eSGuillaume Nault  */
146ee40fb2eSGuillaume Nault static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
147ee40fb2eSGuillaume Nault {
148ee40fb2eSGuillaume Nault 	struct pppol2tp_session *ps = l2tp_session_priv(session);
149ee40fb2eSGuillaume Nault 	struct sock *sk;
150ee40fb2eSGuillaume Nault 
151ee40fb2eSGuillaume Nault 	rcu_read_lock();
152ee40fb2eSGuillaume Nault 	sk = rcu_dereference(ps->sk);
153ee40fb2eSGuillaume Nault 	if (sk)
154ee40fb2eSGuillaume Nault 		sock_hold(sk);
155ee40fb2eSGuillaume Nault 	rcu_read_unlock();
156ee40fb2eSGuillaume Nault 
157ee40fb2eSGuillaume Nault 	return sk;
158ee40fb2eSGuillaume Nault }
159ee40fb2eSGuillaume Nault 
160fd558d18SJames Chapman /* Helpers to obtain tunnel/session contexts from sockets.
161fd558d18SJames Chapman  */
162fd558d18SJames Chapman static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
163fd558d18SJames Chapman {
164fd558d18SJames Chapman 	struct l2tp_session *session;
165fd558d18SJames Chapman 
166fd558d18SJames Chapman 	if (sk == NULL)
167fd558d18SJames Chapman 		return NULL;
168fd558d18SJames Chapman 
169fd558d18SJames Chapman 	sock_hold(sk);
170fd558d18SJames Chapman 	session = (struct l2tp_session *)(sk->sk_user_data);
171fd558d18SJames Chapman 	if (session == NULL) {
172fd558d18SJames Chapman 		sock_put(sk);
173fd558d18SJames Chapman 		goto out;
174fd558d18SJames Chapman 	}
175fd558d18SJames Chapman 
176fd558d18SJames Chapman 	BUG_ON(session->magic != L2TP_SESSION_MAGIC);
177fd558d18SJames Chapman 
178fd558d18SJames Chapman out:
179fd558d18SJames Chapman 	return session;
180fd558d18SJames Chapman }
181fd558d18SJames Chapman 
182fd558d18SJames Chapman /*****************************************************************************
183fd558d18SJames Chapman  * Receive data handling
184fd558d18SJames Chapman  *****************************************************************************/
185fd558d18SJames Chapman 
186fd558d18SJames Chapman /* Receive message. This is the recvmsg for the PPPoL2TP socket.
187fd558d18SJames Chapman  */
1881b784140SYing Xue static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
1891b784140SYing Xue 			    size_t len, int flags)
190fd558d18SJames Chapman {
191fd558d18SJames Chapman 	int err;
192fd558d18SJames Chapman 	struct sk_buff *skb;
193fd558d18SJames Chapman 	struct sock *sk = sock->sk;
194fd558d18SJames Chapman 
195fd558d18SJames Chapman 	err = -EIO;
196fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_BOUND)
197fd558d18SJames Chapman 		goto end;
198fd558d18SJames Chapman 
199fd558d18SJames Chapman 	err = 0;
200fd558d18SJames Chapman 	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
201fd558d18SJames Chapman 				flags & MSG_DONTWAIT, &err);
202fd558d18SJames Chapman 	if (!skb)
203fd558d18SJames Chapman 		goto end;
204fd558d18SJames Chapman 
205fd558d18SJames Chapman 	if (len > skb->len)
206fd558d18SJames Chapman 		len = skb->len;
207fd558d18SJames Chapman 	else if (len < skb->len)
208fd558d18SJames Chapman 		msg->msg_flags |= MSG_TRUNC;
209fd558d18SJames Chapman 
21051f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, len);
211fd558d18SJames Chapman 	if (likely(err == 0))
212fd558d18SJames Chapman 		err = len;
213fd558d18SJames Chapman 
214fd558d18SJames Chapman 	kfree_skb(skb);
215fd558d18SJames Chapman end:
216fd558d18SJames Chapman 	return err;
217fd558d18SJames Chapman }
218fd558d18SJames Chapman 
219fd558d18SJames Chapman static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
220fd558d18SJames Chapman {
221fd558d18SJames Chapman 	struct pppol2tp_session *ps = l2tp_session_priv(session);
222fd558d18SJames Chapman 	struct sock *sk = NULL;
223fd558d18SJames Chapman 
224fd558d18SJames Chapman 	/* If the socket is bound, send it in to PPP's input queue. Otherwise
225fd558d18SJames Chapman 	 * queue it on the session socket.
226fd558d18SJames Chapman 	 */
227ee40fb2eSGuillaume Nault 	rcu_read_lock();
228ee40fb2eSGuillaume Nault 	sk = rcu_dereference(ps->sk);
229fd558d18SJames Chapman 	if (sk == NULL)
230fd558d18SJames Chapman 		goto no_sock;
231fd558d18SJames Chapman 
232*2b139e6bSGuillaume Nault 	/* If the first two bytes are 0xFF03, consider that it is the PPP's
233*2b139e6bSGuillaume Nault 	 * Address and Control fields and skip them. The L2TP module has always
234*2b139e6bSGuillaume Nault 	 * worked this way, although, in theory, the use of these fields should
235*2b139e6bSGuillaume Nault 	 * be negociated and handled at the PPP layer. These fields are
236*2b139e6bSGuillaume Nault 	 * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
237*2b139e6bSGuillaume Nault 	 * Information command with Poll/Final bit set to zero (RFC 1662).
238*2b139e6bSGuillaume Nault 	 */
239*2b139e6bSGuillaume Nault 	if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
240*2b139e6bSGuillaume Nault 	    skb->data[1] == PPP_UI)
241*2b139e6bSGuillaume Nault 		skb_pull(skb, 2);
242*2b139e6bSGuillaume Nault 
243fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_BOUND) {
244fd558d18SJames Chapman 		struct pppox_sock *po;
24598f40b3eSGuillaume Nault 
246fba40c63SAsbjørn Sloth Tønnesen 		l2tp_dbg(session, L2TP_MSG_DATA,
247fd558d18SJames Chapman 			 "%s: recv %d byte data frame, passing to ppp\n",
248fd558d18SJames Chapman 			 session->name, data_len);
249fd558d18SJames Chapman 
250fd558d18SJames Chapman 		po = pppox_sk(sk);
251fd558d18SJames Chapman 		ppp_input(&po->chan, skb);
252fd558d18SJames Chapman 	} else {
253fba40c63SAsbjørn Sloth Tønnesen 		l2tp_dbg(session, L2TP_MSG_DATA,
2549e9cb622SGuillaume Nault 			 "%s: recv %d byte data frame, passing to L2TP socket\n",
2559e9cb622SGuillaume Nault 			 session->name, data_len);
256fd558d18SJames Chapman 
2579e9cb622SGuillaume Nault 		if (sock_queue_rcv_skb(sk, skb) < 0) {
2587b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_errors);
259fd558d18SJames Chapman 			kfree_skb(skb);
260fd558d18SJames Chapman 		}
2619e9cb622SGuillaume Nault 	}
262ee40fb2eSGuillaume Nault 	rcu_read_unlock();
263fd558d18SJames Chapman 
264fd558d18SJames Chapman 	return;
265fd558d18SJames Chapman 
266fd558d18SJames Chapman no_sock:
267ee40fb2eSGuillaume Nault 	rcu_read_unlock();
268fba40c63SAsbjørn Sloth Tønnesen 	l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
269fd558d18SJames Chapman 	kfree_skb(skb);
270fd558d18SJames Chapman }
271fd558d18SJames Chapman 
272fd558d18SJames Chapman /************************************************************************
273fd558d18SJames Chapman  * Transmit handling
274fd558d18SJames Chapman  ***********************************************************************/
275fd558d18SJames Chapman 
276fd558d18SJames Chapman /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket.  We come here
277fd558d18SJames Chapman  * when a user application does a sendmsg() on the session socket. L2TP and
278fd558d18SJames Chapman  * PPP headers must be inserted into the user's data.
279fd558d18SJames Chapman  */
2801b784140SYing Xue static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
281fd558d18SJames Chapman 			    size_t total_len)
282fd558d18SJames Chapman {
283fd558d18SJames Chapman 	struct sock *sk = sock->sk;
284fd558d18SJames Chapman 	struct sk_buff *skb;
285fd558d18SJames Chapman 	int error;
286fd558d18SJames Chapman 	struct l2tp_session *session;
287fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
2880d76751fSJames Chapman 	int uhlen;
289fd558d18SJames Chapman 
290fd558d18SJames Chapman 	error = -ENOTCONN;
291fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
292fd558d18SJames Chapman 		goto error;
293fd558d18SJames Chapman 
294fd558d18SJames Chapman 	/* Get session and tunnel contexts */
295fd558d18SJames Chapman 	error = -EBADF;
296fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
297fd558d18SJames Chapman 	if (session == NULL)
298fd558d18SJames Chapman 		goto error;
299fd558d18SJames Chapman 
3007198c77aSGuillaume Nault 	tunnel = session->tunnel;
301fd558d18SJames Chapman 
3020d76751fSJames Chapman 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
3030d76751fSJames Chapman 
304fd558d18SJames Chapman 	/* Allocate a socket buffer */
305fd558d18SJames Chapman 	error = -ENOMEM;
306fd558d18SJames Chapman 	skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
3070d76751fSJames Chapman 			   uhlen + session->hdr_len +
30854c151d9SGao Feng 			   2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
309fd558d18SJames Chapman 			   0, GFP_KERNEL);
310fd558d18SJames Chapman 	if (!skb)
3117198c77aSGuillaume Nault 		goto error_put_sess;
312fd558d18SJames Chapman 
313fd558d18SJames Chapman 	/* Reserve space for headers. */
314fd558d18SJames Chapman 	skb_reserve(skb, NET_SKB_PAD);
315fd558d18SJames Chapman 	skb_reset_network_header(skb);
316fd558d18SJames Chapman 	skb_reserve(skb, sizeof(struct iphdr));
317fd558d18SJames Chapman 	skb_reset_transport_header(skb);
3180d76751fSJames Chapman 	skb_reserve(skb, uhlen);
319fd558d18SJames Chapman 
320fd558d18SJames Chapman 	/* Add PPP header */
32154c151d9SGao Feng 	skb->data[0] = PPP_ALLSTATIONS;
32254c151d9SGao Feng 	skb->data[1] = PPP_UI;
323fd558d18SJames Chapman 	skb_put(skb, 2);
324fd558d18SJames Chapman 
325fd558d18SJames Chapman 	/* Copy user data into skb */
3266ce8e9ceSAl Viro 	error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
327fd558d18SJames Chapman 	if (error < 0) {
328fd558d18SJames Chapman 		kfree_skb(skb);
3297198c77aSGuillaume Nault 		goto error_put_sess;
330fd558d18SJames Chapman 	}
331fd558d18SJames Chapman 
332455cc32bSEric Dumazet 	local_bh_disable();
333fd558d18SJames Chapman 	l2tp_xmit_skb(session, skb, session->hdr_len);
334455cc32bSEric Dumazet 	local_bh_enable();
335fd558d18SJames Chapman 
3368b82547eSGuillaume Nault 	sock_put(sk);
337fd558d18SJames Chapman 
338a6f79d0fSGuillaume Nault 	return total_len;
339fd558d18SJames Chapman 
340fd558d18SJames Chapman error_put_sess:
341fd558d18SJames Chapman 	sock_put(sk);
342fd558d18SJames Chapman error:
343fd558d18SJames Chapman 	return error;
344fd558d18SJames Chapman }
345fd558d18SJames Chapman 
346fd558d18SJames Chapman /* Transmit function called by generic PPP driver.  Sends PPP frame
347fd558d18SJames Chapman  * over PPPoL2TP socket.
348fd558d18SJames Chapman  *
349fd558d18SJames Chapman  * This is almost the same as pppol2tp_sendmsg(), but rather than
350fd558d18SJames Chapman  * being called with a msghdr from userspace, it is called with a skb
351fd558d18SJames Chapman  * from the kernel.
352fd558d18SJames Chapman  *
353fd558d18SJames Chapman  * The supplied skb from ppp doesn't have enough headroom for the
354fd558d18SJames Chapman  * insertion of L2TP, UDP and IP headers so we need to allocate more
355fd558d18SJames Chapman  * headroom in the skb. This will create a cloned skb. But we must be
356fd558d18SJames Chapman  * careful in the error case because the caller will expect to free
357fd558d18SJames Chapman  * the skb it supplied, not our cloned skb. So we take care to always
358fd558d18SJames Chapman  * leave the original skb unfreed if we return an error.
359fd558d18SJames Chapman  */
360fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
361fd558d18SJames Chapman {
362fd558d18SJames Chapman 	struct sock *sk = (struct sock *) chan->private;
363fd558d18SJames Chapman 	struct l2tp_session *session;
364fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
36509df57caSEric Dumazet 	int uhlen, headroom;
366fd558d18SJames Chapman 
367fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
368fd558d18SJames Chapman 		goto abort;
369fd558d18SJames Chapman 
370fd558d18SJames Chapman 	/* Get session and tunnel contexts from the socket */
371fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
372fd558d18SJames Chapman 	if (session == NULL)
373fd558d18SJames Chapman 		goto abort;
374fd558d18SJames Chapman 
3757198c77aSGuillaume Nault 	tunnel = session->tunnel;
376fd558d18SJames Chapman 
37709df57caSEric Dumazet 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
37809df57caSEric Dumazet 	headroom = NET_SKB_PAD +
37909df57caSEric Dumazet 		   sizeof(struct iphdr) + /* IP header */
38009df57caSEric Dumazet 		   uhlen +		/* UDP header (if L2TP_ENCAPTYPE_UDP) */
38109df57caSEric Dumazet 		   session->hdr_len +	/* L2TP header */
38254c151d9SGao Feng 		   2;			/* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
38309df57caSEric Dumazet 	if (skb_cow_head(skb, headroom))
3847198c77aSGuillaume Nault 		goto abort_put_sess;
385fd558d18SJames Chapman 
386fd558d18SJames Chapman 	/* Setup PPP header */
38754c151d9SGao Feng 	__skb_push(skb, 2);
38854c151d9SGao Feng 	skb->data[0] = PPP_ALLSTATIONS;
38954c151d9SGao Feng 	skb->data[1] = PPP_UI;
390fd558d18SJames Chapman 
391455cc32bSEric Dumazet 	local_bh_disable();
392e0d4435fSJames Chapman 	l2tp_xmit_skb(session, skb, session->hdr_len);
393455cc32bSEric Dumazet 	local_bh_enable();
394fd558d18SJames Chapman 
395fd558d18SJames Chapman 	sock_put(sk);
3967198c77aSGuillaume Nault 
397fd558d18SJames Chapman 	return 1;
398fd558d18SJames Chapman 
399fd558d18SJames Chapman abort_put_sess:
400fd558d18SJames Chapman 	sock_put(sk);
401fd558d18SJames Chapman abort:
402fd558d18SJames Chapman 	/* Free the original skb */
403fd558d18SJames Chapman 	kfree_skb(skb);
404fd558d18SJames Chapman 	return 1;
405fd558d18SJames Chapman }
406fd558d18SJames Chapman 
407fd558d18SJames Chapman /*****************************************************************************
408fd558d18SJames Chapman  * Session (and tunnel control) socket create/destroy.
409fd558d18SJames Chapman  *****************************************************************************/
410fd558d18SJames Chapman 
411d02ba2a6SJames Chapman static void pppol2tp_put_sk(struct rcu_head *head)
412d02ba2a6SJames Chapman {
413d02ba2a6SJames Chapman 	struct pppol2tp_session *ps;
414d02ba2a6SJames Chapman 
415d02ba2a6SJames Chapman 	ps = container_of(head, typeof(*ps), rcu);
416d02ba2a6SJames Chapman 	sock_put(ps->__sk);
417d02ba2a6SJames Chapman }
418d02ba2a6SJames Chapman 
419fd558d18SJames Chapman /* Really kill the session socket. (Called from sock_put() if
420fd558d18SJames Chapman  * refcnt == 0.)
421fd558d18SJames Chapman  */
422fd558d18SJames Chapman static void pppol2tp_session_destruct(struct sock *sk)
423fd558d18SJames Chapman {
424f6e16b29STom Parkin 	struct l2tp_session *session = sk->sk_user_data;
425e91793bbSGuillaume Nault 
426e91793bbSGuillaume Nault 	skb_queue_purge(&sk->sk_receive_queue);
427e91793bbSGuillaume Nault 	skb_queue_purge(&sk->sk_write_queue);
428e91793bbSGuillaume Nault 
429f6e16b29STom Parkin 	if (session) {
430fd558d18SJames Chapman 		sk->sk_user_data = NULL;
431fd558d18SJames Chapman 		BUG_ON(session->magic != L2TP_SESSION_MAGIC);
432fd558d18SJames Chapman 		l2tp_session_dec_refcount(session);
433fd558d18SJames Chapman 	}
434fd558d18SJames Chapman }
435fd558d18SJames Chapman 
436fd558d18SJames Chapman /* Called when the PPPoX socket (session) is closed.
437fd558d18SJames Chapman  */
438fd558d18SJames Chapman static int pppol2tp_release(struct socket *sock)
439fd558d18SJames Chapman {
440fd558d18SJames Chapman 	struct sock *sk = sock->sk;
441fd558d18SJames Chapman 	struct l2tp_session *session;
442fd558d18SJames Chapman 	int error;
443fd558d18SJames Chapman 
444fd558d18SJames Chapman 	if (!sk)
445fd558d18SJames Chapman 		return 0;
446fd558d18SJames Chapman 
447fd558d18SJames Chapman 	error = -EBADF;
448fd558d18SJames Chapman 	lock_sock(sk);
449fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) != 0)
450fd558d18SJames Chapman 		goto error;
451fd558d18SJames Chapman 
452fd558d18SJames Chapman 	pppox_unbind_sock(sk);
453fd558d18SJames Chapman 
454fd558d18SJames Chapman 	/* Signal the death of the socket. */
455fd558d18SJames Chapman 	sk->sk_state = PPPOX_DEAD;
456fd558d18SJames Chapman 	sock_orphan(sk);
457fd558d18SJames Chapman 	sock->sk = NULL;
458fd558d18SJames Chapman 
459d02ba2a6SJames Chapman 	session = pppol2tp_sock_to_session(sk);
460d02ba2a6SJames Chapman 	if (session) {
4613d609342SGuillaume Nault 		struct pppol2tp_session *ps;
4623d609342SGuillaume Nault 
463d02ba2a6SJames Chapman 		l2tp_session_delete(session);
4643d609342SGuillaume Nault 
4653d609342SGuillaume Nault 		ps = l2tp_session_priv(session);
4663d609342SGuillaume Nault 		mutex_lock(&ps->sk_lock);
4673d609342SGuillaume Nault 		ps->__sk = rcu_dereference_protected(ps->sk,
4683d609342SGuillaume Nault 						     lockdep_is_held(&ps->sk_lock));
4693d609342SGuillaume Nault 		RCU_INIT_POINTER(ps->sk, NULL);
4703d609342SGuillaume Nault 		mutex_unlock(&ps->sk_lock);
4713d609342SGuillaume Nault 		call_rcu(&ps->rcu, pppol2tp_put_sk);
4723d609342SGuillaume Nault 
4733d609342SGuillaume Nault 		/* Rely on the sock_put() call at the end of the function for
4743d609342SGuillaume Nault 		 * dropping the reference held by pppol2tp_sock_to_session().
4753d609342SGuillaume Nault 		 * The last reference will be dropped by pppol2tp_put_sk().
4763d609342SGuillaume Nault 		 */
477cf2f5c88STom Parkin 	}
478d02ba2a6SJames Chapman 
479fd558d18SJames Chapman 	release_sock(sk);
480fd558d18SJames Chapman 
481fd558d18SJames Chapman 	/* This will delete the session context via
482fd558d18SJames Chapman 	 * pppol2tp_session_destruct() if the socket's refcnt drops to
483fd558d18SJames Chapman 	 * zero.
484fd558d18SJames Chapman 	 */
485fd558d18SJames Chapman 	sock_put(sk);
486fd558d18SJames Chapman 
487fd558d18SJames Chapman 	return 0;
488fd558d18SJames Chapman 
489fd558d18SJames Chapman error:
490fd558d18SJames Chapman 	release_sock(sk);
491fd558d18SJames Chapman 	return error;
492fd558d18SJames Chapman }
493fd558d18SJames Chapman 
494fd558d18SJames Chapman static struct proto pppol2tp_sk_proto = {
495fd558d18SJames Chapman 	.name	  = "PPPOL2TP",
496fd558d18SJames Chapman 	.owner	  = THIS_MODULE,
497fd558d18SJames Chapman 	.obj_size = sizeof(struct pppox_sock),
498fd558d18SJames Chapman };
499fd558d18SJames Chapman 
500fd558d18SJames Chapman static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
501fd558d18SJames Chapman {
502fd558d18SJames Chapman 	int rc;
503fd558d18SJames Chapman 
504fd558d18SJames Chapman 	rc = l2tp_udp_encap_recv(sk, skb);
505fd558d18SJames Chapman 	if (rc)
506fd558d18SJames Chapman 		kfree_skb(skb);
507fd558d18SJames Chapman 
508fd558d18SJames Chapman 	return NET_RX_SUCCESS;
509fd558d18SJames Chapman }
510fd558d18SJames Chapman 
511fd558d18SJames Chapman /* socket() handler. Initialize a new struct sock.
512fd558d18SJames Chapman  */
51311aa9c28SEric W. Biederman static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
514fd558d18SJames Chapman {
515fd558d18SJames Chapman 	int error = -ENOMEM;
516fd558d18SJames Chapman 	struct sock *sk;
517fd558d18SJames Chapman 
51811aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
519fd558d18SJames Chapman 	if (!sk)
520fd558d18SJames Chapman 		goto out;
521fd558d18SJames Chapman 
522fd558d18SJames Chapman 	sock_init_data(sock, sk);
523fd558d18SJames Chapman 
524fd558d18SJames Chapman 	sock->state  = SS_UNCONNECTED;
525fd558d18SJames Chapman 	sock->ops    = &pppol2tp_ops;
526fd558d18SJames Chapman 
527fd558d18SJames Chapman 	sk->sk_backlog_rcv = pppol2tp_backlog_recv;
528fd558d18SJames Chapman 	sk->sk_protocol	   = PX_PROTO_OL2TP;
529fd558d18SJames Chapman 	sk->sk_family	   = PF_PPPOX;
530fd558d18SJames Chapman 	sk->sk_state	   = PPPOX_NONE;
531fd558d18SJames Chapman 	sk->sk_type	   = SOCK_STREAM;
532fd558d18SJames Chapman 	sk->sk_destruct	   = pppol2tp_session_destruct;
533fd558d18SJames Chapman 
534fd558d18SJames Chapman 	error = 0;
535fd558d18SJames Chapman 
536fd558d18SJames Chapman out:
537fd558d18SJames Chapman 	return error;
538fd558d18SJames Chapman }
539fd558d18SJames Chapman 
5409dd79945SJavier Martinez Canillas #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
5410ad66140SJames Chapman static void pppol2tp_show(struct seq_file *m, void *arg)
5420ad66140SJames Chapman {
5430ad66140SJames Chapman 	struct l2tp_session *session = arg;
544ee40fb2eSGuillaume Nault 	struct sock *sk;
5450ad66140SJames Chapman 
546ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
547ee40fb2eSGuillaume Nault 	if (sk) {
548ee40fb2eSGuillaume Nault 		struct pppox_sock *po = pppox_sk(sk);
549ee40fb2eSGuillaume Nault 
5500ad66140SJames Chapman 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
551ee40fb2eSGuillaume Nault 		sock_put(sk);
5520ad66140SJames Chapman 	}
5530ad66140SJames Chapman }
5540ad66140SJames Chapman #endif
5550ad66140SJames Chapman 
556f98be6c6SGuillaume Nault static void pppol2tp_session_init(struct l2tp_session *session)
557f98be6c6SGuillaume Nault {
558f98be6c6SGuillaume Nault 	struct pppol2tp_session *ps;
559f98be6c6SGuillaume Nault 	struct dst_entry *dst;
560f98be6c6SGuillaume Nault 
561f98be6c6SGuillaume Nault 	session->recv_skb = pppol2tp_recv;
562f98be6c6SGuillaume Nault #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
563f98be6c6SGuillaume Nault 	session->show = pppol2tp_show;
564f98be6c6SGuillaume Nault #endif
565f98be6c6SGuillaume Nault 
566f98be6c6SGuillaume Nault 	ps = l2tp_session_priv(session);
567f98be6c6SGuillaume Nault 	mutex_init(&ps->sk_lock);
568f98be6c6SGuillaume Nault 	ps->owner = current->pid;
569f98be6c6SGuillaume Nault 
570f98be6c6SGuillaume Nault 	/* If PMTU discovery was enabled, use the MTU that was discovered */
571f98be6c6SGuillaume Nault 	dst = sk_dst_get(session->tunnel->sock);
572f98be6c6SGuillaume Nault 	if (dst) {
573f98be6c6SGuillaume Nault 		u32 pmtu = dst_mtu(dst);
574f98be6c6SGuillaume Nault 
575f98be6c6SGuillaume Nault 		if (pmtu) {
576f98be6c6SGuillaume Nault 			session->mtu = pmtu - PPPOL2TP_HEADER_OVERHEAD;
577f98be6c6SGuillaume Nault 			session->mru = pmtu - PPPOL2TP_HEADER_OVERHEAD;
578f98be6c6SGuillaume Nault 		}
579f98be6c6SGuillaume Nault 		dst_release(dst);
580f98be6c6SGuillaume Nault 	}
581f98be6c6SGuillaume Nault }
582f98be6c6SGuillaume Nault 
583a408194aSGuillaume Nault struct l2tp_connect_info {
584a408194aSGuillaume Nault 	u8 version;
585a408194aSGuillaume Nault 	int fd;
586a408194aSGuillaume Nault 	u32 tunnel_id;
587a408194aSGuillaume Nault 	u32 peer_tunnel_id;
588a408194aSGuillaume Nault 	u32 session_id;
589a408194aSGuillaume Nault 	u32 peer_session_id;
590a408194aSGuillaume Nault };
591a408194aSGuillaume Nault 
592a408194aSGuillaume Nault static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
593a408194aSGuillaume Nault 				      struct l2tp_connect_info *info)
594a408194aSGuillaume Nault {
595a408194aSGuillaume Nault 	switch (sa_len) {
596a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tp):
597a408194aSGuillaume Nault 	{
598a408194aSGuillaume Nault 		const struct sockaddr_pppol2tp *sa_v2in4 = sa;
599a408194aSGuillaume Nault 
600a408194aSGuillaume Nault 		if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
601a408194aSGuillaume Nault 			return -EINVAL;
602a408194aSGuillaume Nault 
603a408194aSGuillaume Nault 		info->version = 2;
604a408194aSGuillaume Nault 		info->fd = sa_v2in4->pppol2tp.fd;
605a408194aSGuillaume Nault 		info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
606a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
607a408194aSGuillaume Nault 		info->session_id = sa_v2in4->pppol2tp.s_session;
608a408194aSGuillaume Nault 		info->peer_session_id = sa_v2in4->pppol2tp.d_session;
609a408194aSGuillaume Nault 
610a408194aSGuillaume Nault 		break;
611a408194aSGuillaume Nault 	}
612a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpv3):
613a408194aSGuillaume Nault 	{
614a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
615a408194aSGuillaume Nault 
616a408194aSGuillaume Nault 		if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
617a408194aSGuillaume Nault 			return -EINVAL;
618a408194aSGuillaume Nault 
619a408194aSGuillaume Nault 		info->version = 3;
620a408194aSGuillaume Nault 		info->fd = sa_v3in4->pppol2tp.fd;
621a408194aSGuillaume Nault 		info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
622a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
623a408194aSGuillaume Nault 		info->session_id = sa_v3in4->pppol2tp.s_session;
624a408194aSGuillaume Nault 		info->peer_session_id = sa_v3in4->pppol2tp.d_session;
625a408194aSGuillaume Nault 
626a408194aSGuillaume Nault 		break;
627a408194aSGuillaume Nault 	}
628a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpin6):
629a408194aSGuillaume Nault 	{
630a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
631a408194aSGuillaume Nault 
632a408194aSGuillaume Nault 		if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
633a408194aSGuillaume Nault 			return -EINVAL;
634a408194aSGuillaume Nault 
635a408194aSGuillaume Nault 		info->version = 2;
636a408194aSGuillaume Nault 		info->fd = sa_v2in6->pppol2tp.fd;
637a408194aSGuillaume Nault 		info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
638a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
639a408194aSGuillaume Nault 		info->session_id = sa_v2in6->pppol2tp.s_session;
640a408194aSGuillaume Nault 		info->peer_session_id = sa_v2in6->pppol2tp.d_session;
641a408194aSGuillaume Nault 
642a408194aSGuillaume Nault 		break;
643a408194aSGuillaume Nault 	}
644a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpv3in6):
645a408194aSGuillaume Nault 	{
646a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
647a408194aSGuillaume Nault 
648a408194aSGuillaume Nault 		if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
649a408194aSGuillaume Nault 			return -EINVAL;
650a408194aSGuillaume Nault 
651a408194aSGuillaume Nault 		info->version = 3;
652a408194aSGuillaume Nault 		info->fd = sa_v3in6->pppol2tp.fd;
653a408194aSGuillaume Nault 		info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
654a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
655a408194aSGuillaume Nault 		info->session_id = sa_v3in6->pppol2tp.s_session;
656a408194aSGuillaume Nault 		info->peer_session_id = sa_v3in6->pppol2tp.d_session;
657a408194aSGuillaume Nault 
658a408194aSGuillaume Nault 		break;
659a408194aSGuillaume Nault 	}
660a408194aSGuillaume Nault 	default:
661a408194aSGuillaume Nault 		return -EINVAL;
662a408194aSGuillaume Nault 	}
663a408194aSGuillaume Nault 
664a408194aSGuillaume Nault 	return 0;
665a408194aSGuillaume Nault }
666a408194aSGuillaume Nault 
667fd558d18SJames Chapman /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
668fd558d18SJames Chapman  */
669fd558d18SJames Chapman static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
670fd558d18SJames Chapman 			    int sockaddr_len, int flags)
671fd558d18SJames Chapman {
672fd558d18SJames Chapman 	struct sock *sk = sock->sk;
673fd558d18SJames Chapman 	struct pppox_sock *po = pppox_sk(sk);
674fd558d18SJames Chapman 	struct l2tp_session *session = NULL;
675a408194aSGuillaume Nault 	struct l2tp_connect_info info;
676fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
677fd558d18SJames Chapman 	struct pppol2tp_session *ps;
678fd558d18SJames Chapman 	struct l2tp_session_cfg cfg = { 0, };
679dbdbc73bSGuillaume Nault 	bool drop_refcnt = false;
680f9e56bafSGuillaume Nault 	bool drop_tunnel = false;
681bda06be2SGuillaume Nault 	bool new_session = false;
682bda06be2SGuillaume Nault 	bool new_tunnel = false;
683a408194aSGuillaume Nault 	int error;
684a408194aSGuillaume Nault 
685a408194aSGuillaume Nault 	error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
686a408194aSGuillaume Nault 	if (error < 0)
687a408194aSGuillaume Nault 		return error;
688fd558d18SJames Chapman 
689fd558d18SJames Chapman 	lock_sock(sk);
690fd558d18SJames Chapman 
691fd558d18SJames Chapman 	/* Check for already bound sockets */
692fd558d18SJames Chapman 	error = -EBUSY;
693fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_CONNECTED)
694fd558d18SJames Chapman 		goto end;
695fd558d18SJames Chapman 
696fd558d18SJames Chapman 	/* We don't supporting rebinding anyway */
697fd558d18SJames Chapman 	error = -EALREADY;
698fd558d18SJames Chapman 	if (sk->sk_user_data)
699fd558d18SJames Chapman 		goto end; /* socket is already attached */
700fd558d18SJames Chapman 
701e0d4435fSJames Chapman 	/* Don't bind if tunnel_id is 0 */
702e0d4435fSJames Chapman 	error = -EINVAL;
703a408194aSGuillaume Nault 	if (!info.tunnel_id)
704fd558d18SJames Chapman 		goto end;
705fd558d18SJames Chapman 
706a408194aSGuillaume Nault 	tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
707f9e56bafSGuillaume Nault 	if (tunnel)
708f9e56bafSGuillaume Nault 		drop_tunnel = true;
709309795f4SJames Chapman 
710e0d4435fSJames Chapman 	/* Special case: create tunnel context if session_id and
711e0d4435fSJames Chapman 	 * peer_session_id is 0. Otherwise look up tunnel using supplied
712fd558d18SJames Chapman 	 * tunnel id.
713fd558d18SJames Chapman 	 */
714a408194aSGuillaume Nault 	if (!info.session_id && !info.peer_session_id) {
715309795f4SJames Chapman 		if (tunnel == NULL) {
716309795f4SJames Chapman 			struct l2tp_tunnel_cfg tcfg = {
717309795f4SJames Chapman 				.encap = L2TP_ENCAPTYPE_UDP,
718309795f4SJames Chapman 				.debug = 0,
719309795f4SJames Chapman 			};
7203e1bc8bfSGuillaume Nault 
7213e1bc8bfSGuillaume Nault 			/* Prevent l2tp_tunnel_register() from trying to set up
7223e1bc8bfSGuillaume Nault 			 * a kernel socket.
7233e1bc8bfSGuillaume Nault 			 */
724a408194aSGuillaume Nault 			if (info.fd < 0) {
7253e1bc8bfSGuillaume Nault 				error = -EBADF;
7263e1bc8bfSGuillaume Nault 				goto end;
7273e1bc8bfSGuillaume Nault 			}
7283e1bc8bfSGuillaume Nault 
729a408194aSGuillaume Nault 			error = l2tp_tunnel_create(sock_net(sk), info.fd,
730a408194aSGuillaume Nault 						   info.version,
731a408194aSGuillaume Nault 						   info.tunnel_id,
732a408194aSGuillaume Nault 						   info.peer_tunnel_id, &tcfg,
733a408194aSGuillaume Nault 						   &tunnel);
734fd558d18SJames Chapman 			if (error < 0)
735fd558d18SJames Chapman 				goto end;
7366b9f3423SGuillaume Nault 
7376b9f3423SGuillaume Nault 			l2tp_tunnel_inc_refcount(tunnel);
7386b9f3423SGuillaume Nault 			error = l2tp_tunnel_register(tunnel, sock_net(sk),
7396b9f3423SGuillaume Nault 						     &tcfg);
7406b9f3423SGuillaume Nault 			if (error < 0) {
7416b9f3423SGuillaume Nault 				kfree(tunnel);
7426b9f3423SGuillaume Nault 				goto end;
7436b9f3423SGuillaume Nault 			}
7446b9f3423SGuillaume Nault 			drop_tunnel = true;
745bda06be2SGuillaume Nault 			new_tunnel = true;
746309795f4SJames Chapman 		}
747fd558d18SJames Chapman 	} else {
748fd558d18SJames Chapman 		/* Error if we can't find the tunnel */
749fd558d18SJames Chapman 		error = -ENOENT;
750fd558d18SJames Chapman 		if (tunnel == NULL)
751fd558d18SJames Chapman 			goto end;
752fd558d18SJames Chapman 
753fd558d18SJames Chapman 		/* Error if socket is not prepped */
754fd558d18SJames Chapman 		if (tunnel->sock == NULL)
755fd558d18SJames Chapman 			goto end;
756fd558d18SJames Chapman 	}
757fd558d18SJames Chapman 
758b79585f5SJames Chapman 	if (tunnel->peer_tunnel_id == 0)
759a408194aSGuillaume Nault 		tunnel->peer_tunnel_id = info.peer_tunnel_id;
760fd558d18SJames Chapman 
761a408194aSGuillaume Nault 	session = l2tp_session_get(sock_net(sk), tunnel, info.session_id);
762dbdbc73bSGuillaume Nault 	if (session) {
763dbdbc73bSGuillaume Nault 		drop_refcnt = true;
7647ac6ab1fSGuillaume Nault 
7657ac6ab1fSGuillaume Nault 		if (session->pwtype != L2TP_PWTYPE_PPP) {
7667ac6ab1fSGuillaume Nault 			error = -EPROTOTYPE;
7677ac6ab1fSGuillaume Nault 			goto end;
7687ac6ab1fSGuillaume Nault 		}
7697ac6ab1fSGuillaume Nault 
770dbdbc73bSGuillaume Nault 		ps = l2tp_session_priv(session);
771fd558d18SJames Chapman 
772dbdbc73bSGuillaume Nault 		/* Using a pre-existing session is fine as long as it hasn't
773dbdbc73bSGuillaume Nault 		 * been connected yet.
774dbdbc73bSGuillaume Nault 		 */
775ee40fb2eSGuillaume Nault 		mutex_lock(&ps->sk_lock);
776ee40fb2eSGuillaume Nault 		if (rcu_dereference_protected(ps->sk,
7773d609342SGuillaume Nault 					      lockdep_is_held(&ps->sk_lock)) ||
7783d609342SGuillaume Nault 		    ps->__sk) {
779ee40fb2eSGuillaume Nault 			mutex_unlock(&ps->sk_lock);
780dbdbc73bSGuillaume Nault 			error = -EEXIST;
781dbdbc73bSGuillaume Nault 			goto end;
782dbdbc73bSGuillaume Nault 		}
783309795f4SJames Chapman 	} else {
784dbdbc73bSGuillaume Nault 		/* Default MTU must allow space for UDP/L2TP/PPP headers */
785dbdbc73bSGuillaume Nault 		cfg.mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
786dbdbc73bSGuillaume Nault 		cfg.mru = cfg.mtu;
78790904ff5SGuillaume Nault 		cfg.pw_type = L2TP_PWTYPE_PPP;
788fd558d18SJames Chapman 
789dbdbc73bSGuillaume Nault 		session = l2tp_session_create(sizeof(struct pppol2tp_session),
790a408194aSGuillaume Nault 					      tunnel, info.session_id,
791a408194aSGuillaume Nault 					      info.peer_session_id, &cfg);
792dbdbc73bSGuillaume Nault 		if (IS_ERR(session)) {
793dbdbc73bSGuillaume Nault 			error = PTR_ERR(session);
794309795f4SJames Chapman 			goto end;
795309795f4SJames Chapman 		}
7963953ae7bSGuillaume Nault 
797f98be6c6SGuillaume Nault 		pppol2tp_session_init(session);
798ee40fb2eSGuillaume Nault 		ps = l2tp_session_priv(session);
7993953ae7bSGuillaume Nault 		l2tp_session_inc_refcount(session);
800ee40fb2eSGuillaume Nault 
801ee40fb2eSGuillaume Nault 		mutex_lock(&ps->sk_lock);
8023953ae7bSGuillaume Nault 		error = l2tp_session_register(session, tunnel);
8033953ae7bSGuillaume Nault 		if (error < 0) {
804ee40fb2eSGuillaume Nault 			mutex_unlock(&ps->sk_lock);
8053953ae7bSGuillaume Nault 			kfree(session);
8063953ae7bSGuillaume Nault 			goto end;
8073953ae7bSGuillaume Nault 		}
8083953ae7bSGuillaume Nault 		drop_refcnt = true;
809bda06be2SGuillaume Nault 		new_session = true;
810dbdbc73bSGuillaume Nault 	}
811309795f4SJames Chapman 
812fd558d18SJames Chapman 	/* Special case: if source & dest session_id == 0x0000, this
813fd558d18SJames Chapman 	 * socket is being created to manage the tunnel. Just set up
814fd558d18SJames Chapman 	 * the internal context for use by ioctl() and sockopt()
815fd558d18SJames Chapman 	 * handlers.
816fd558d18SJames Chapman 	 */
817fd558d18SJames Chapman 	if ((session->session_id == 0) &&
818fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
819fd558d18SJames Chapman 		error = 0;
820fd558d18SJames Chapman 		goto out_no_ppp;
821fd558d18SJames Chapman 	}
822fd558d18SJames Chapman 
823fd558d18SJames Chapman 	/* The only header we need to worry about is the L2TP
824fd558d18SJames Chapman 	 * header. This size is different depending on whether
825fd558d18SJames Chapman 	 * sequence numbers are enabled for the data channel.
826fd558d18SJames Chapman 	 */
827fd558d18SJames Chapman 	po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
828fd558d18SJames Chapman 
829fd558d18SJames Chapman 	po->chan.private = sk;
830fd558d18SJames Chapman 	po->chan.ops	 = &pppol2tp_chan_ops;
831fd558d18SJames Chapman 	po->chan.mtu	 = session->mtu;
832fd558d18SJames Chapman 
833fd558d18SJames Chapman 	error = ppp_register_net_channel(sock_net(sk), &po->chan);
834ee40fb2eSGuillaume Nault 	if (error) {
835ee40fb2eSGuillaume Nault 		mutex_unlock(&ps->sk_lock);
836fd558d18SJames Chapman 		goto end;
837ee40fb2eSGuillaume Nault 	}
838fd558d18SJames Chapman 
839fd558d18SJames Chapman out_no_ppp:
840fd558d18SJames Chapman 	/* This is how we get the session context from the socket. */
841fd558d18SJames Chapman 	sk->sk_user_data = session;
842ee40fb2eSGuillaume Nault 	rcu_assign_pointer(ps->sk, sk);
843ee40fb2eSGuillaume Nault 	mutex_unlock(&ps->sk_lock);
844ee40fb2eSGuillaume Nault 
845f98be6c6SGuillaume Nault 	/* Keep the reference we've grabbed on the session: sk doesn't expect
846f98be6c6SGuillaume Nault 	 * the session to disappear. pppol2tp_session_destruct() is responsible
847f98be6c6SGuillaume Nault 	 * for dropping it.
848f98be6c6SGuillaume Nault 	 */
849f98be6c6SGuillaume Nault 	drop_refcnt = false;
850f98be6c6SGuillaume Nault 
851fd558d18SJames Chapman 	sk->sk_state = PPPOX_CONNECTED;
852fba40c63SAsbjørn Sloth Tønnesen 	l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
853a4ca44faSJoe Perches 		  session->name);
854fd558d18SJames Chapman 
855fd558d18SJames Chapman end:
856bda06be2SGuillaume Nault 	if (error) {
857bda06be2SGuillaume Nault 		if (new_session)
858bda06be2SGuillaume Nault 			l2tp_session_delete(session);
859bda06be2SGuillaume Nault 		if (new_tunnel)
860bda06be2SGuillaume Nault 			l2tp_tunnel_delete(tunnel);
861bda06be2SGuillaume Nault 	}
862dbdbc73bSGuillaume Nault 	if (drop_refcnt)
863dbdbc73bSGuillaume Nault 		l2tp_session_dec_refcount(session);
864f9e56bafSGuillaume Nault 	if (drop_tunnel)
865f9e56bafSGuillaume Nault 		l2tp_tunnel_dec_refcount(tunnel);
866fd558d18SJames Chapman 	release_sock(sk);
867fd558d18SJames Chapman 
868fd558d18SJames Chapman 	return error;
869fd558d18SJames Chapman }
870fd558d18SJames Chapman 
871309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
872309795f4SJames Chapman 
873f026bc29SGuillaume Nault /* Called when creating sessions via the netlink interface. */
874f026bc29SGuillaume Nault static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
875f026bc29SGuillaume Nault 				   u32 session_id, u32 peer_session_id,
876f026bc29SGuillaume Nault 				   struct l2tp_session_cfg *cfg)
877309795f4SJames Chapman {
878309795f4SJames Chapman 	int error;
879309795f4SJames Chapman 	struct l2tp_session *session;
880309795f4SJames Chapman 
881309795f4SJames Chapman 	/* Error if tunnel socket is not prepped */
882f026bc29SGuillaume Nault 	if (!tunnel->sock) {
883f026bc29SGuillaume Nault 		error = -ENOENT;
8843953ae7bSGuillaume Nault 		goto err;
885f026bc29SGuillaume Nault 	}
886309795f4SJames Chapman 
887309795f4SJames Chapman 	/* Default MTU values. */
888309795f4SJames Chapman 	if (cfg->mtu == 0)
889309795f4SJames Chapman 		cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
890309795f4SJames Chapman 	if (cfg->mru == 0)
891309795f4SJames Chapman 		cfg->mru = cfg->mtu;
892309795f4SJames Chapman 
893309795f4SJames Chapman 	/* Allocate and initialize a new session context. */
894309795f4SJames Chapman 	session = l2tp_session_create(sizeof(struct pppol2tp_session),
895309795f4SJames Chapman 				      tunnel, session_id,
896309795f4SJames Chapman 				      peer_session_id, cfg);
897dbdbc73bSGuillaume Nault 	if (IS_ERR(session)) {
898dbdbc73bSGuillaume Nault 		error = PTR_ERR(session);
8993953ae7bSGuillaume Nault 		goto err;
900dbdbc73bSGuillaume Nault 	}
901309795f4SJames Chapman 
902f98be6c6SGuillaume Nault 	pppol2tp_session_init(session);
903309795f4SJames Chapman 
9043953ae7bSGuillaume Nault 	error = l2tp_session_register(session, tunnel);
9053953ae7bSGuillaume Nault 	if (error < 0)
9063953ae7bSGuillaume Nault 		goto err_sess;
907309795f4SJames Chapman 
9083953ae7bSGuillaume Nault 	return 0;
909309795f4SJames Chapman 
9103953ae7bSGuillaume Nault err_sess:
9113953ae7bSGuillaume Nault 	kfree(session);
9123953ae7bSGuillaume Nault err:
913309795f4SJames Chapman 	return error;
914309795f4SJames Chapman }
915309795f4SJames Chapman 
916309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */
917309795f4SJames Chapman 
918fd558d18SJames Chapman /* getname() support.
919fd558d18SJames Chapman  */
920fd558d18SJames Chapman static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
9219b2c45d4SDenys Vlasenko 			    int peer)
922fd558d18SJames Chapman {
923e0d4435fSJames Chapman 	int len = 0;
924fd558d18SJames Chapman 	int error = 0;
925fd558d18SJames Chapman 	struct l2tp_session *session;
926fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
927fd558d18SJames Chapman 	struct sock *sk = sock->sk;
928fd558d18SJames Chapman 	struct inet_sock *inet;
929fd558d18SJames Chapman 	struct pppol2tp_session *pls;
930fd558d18SJames Chapman 
931fd558d18SJames Chapman 	error = -ENOTCONN;
932fd558d18SJames Chapman 	if (sk == NULL)
933fd558d18SJames Chapman 		goto end;
93456cff471SGao Feng 	if (!(sk->sk_state & PPPOX_CONNECTED))
935fd558d18SJames Chapman 		goto end;
936fd558d18SJames Chapman 
937fd558d18SJames Chapman 	error = -EBADF;
938fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
939fd558d18SJames Chapman 	if (session == NULL)
940fd558d18SJames Chapman 		goto end;
941fd558d18SJames Chapman 
942fd558d18SJames Chapman 	pls = l2tp_session_priv(session);
9437198c77aSGuillaume Nault 	tunnel = session->tunnel;
944fd558d18SJames Chapman 
945bbdb32cbSBenjamin LaHaise 	inet = inet_sk(tunnel->sock);
946d2cf3361SBenjamin LaHaise 	if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
947e0d4435fSJames Chapman 		struct sockaddr_pppol2tp sp;
948e0d4435fSJames Chapman 		len = sizeof(sp);
949fd558d18SJames Chapman 		memset(&sp, 0, len);
950fd558d18SJames Chapman 		sp.sa_family	= AF_PPPOX;
951fd558d18SJames Chapman 		sp.sa_protocol	= PX_PROTO_OL2TP;
952fd558d18SJames Chapman 		sp.pppol2tp.fd  = tunnel->fd;
953fd558d18SJames Chapman 		sp.pppol2tp.pid = pls->owner;
954fd558d18SJames Chapman 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
955fd558d18SJames Chapman 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
956fd558d18SJames Chapman 		sp.pppol2tp.s_session = session->session_id;
957fd558d18SJames Chapman 		sp.pppol2tp.d_session = session->peer_session_id;
958fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_family = AF_INET;
959fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
960fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
961fd558d18SJames Chapman 		memcpy(uaddr, &sp, len);
962d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
963d2cf3361SBenjamin LaHaise 	} else if ((tunnel->version == 2) &&
964d2cf3361SBenjamin LaHaise 		   (tunnel->sock->sk_family == AF_INET6)) {
965d2cf3361SBenjamin LaHaise 		struct sockaddr_pppol2tpin6 sp;
966efe4208fSEric Dumazet 
967d2cf3361SBenjamin LaHaise 		len = sizeof(sp);
968d2cf3361SBenjamin LaHaise 		memset(&sp, 0, len);
969d2cf3361SBenjamin LaHaise 		sp.sa_family	= AF_PPPOX;
970d2cf3361SBenjamin LaHaise 		sp.sa_protocol	= PX_PROTO_OL2TP;
971d2cf3361SBenjamin LaHaise 		sp.pppol2tp.fd  = tunnel->fd;
972d2cf3361SBenjamin LaHaise 		sp.pppol2tp.pid = pls->owner;
973d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
974d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
975d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_session = session->session_id;
976d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_session = session->peer_session_id;
977d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_family = AF_INET6;
978d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
979efe4208fSEric Dumazet 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
980efe4208fSEric Dumazet 		       sizeof(tunnel->sock->sk_v6_daddr));
981d2cf3361SBenjamin LaHaise 		memcpy(uaddr, &sp, len);
982d2cf3361SBenjamin LaHaise 	} else if ((tunnel->version == 3) &&
983d2cf3361SBenjamin LaHaise 		   (tunnel->sock->sk_family == AF_INET6)) {
984d2cf3361SBenjamin LaHaise 		struct sockaddr_pppol2tpv3in6 sp;
985efe4208fSEric Dumazet 
986d2cf3361SBenjamin LaHaise 		len = sizeof(sp);
987d2cf3361SBenjamin LaHaise 		memset(&sp, 0, len);
988d2cf3361SBenjamin LaHaise 		sp.sa_family	= AF_PPPOX;
989d2cf3361SBenjamin LaHaise 		sp.sa_protocol	= PX_PROTO_OL2TP;
990d2cf3361SBenjamin LaHaise 		sp.pppol2tp.fd  = tunnel->fd;
991d2cf3361SBenjamin LaHaise 		sp.pppol2tp.pid = pls->owner;
992d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
993d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
994d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_session = session->session_id;
995d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_session = session->peer_session_id;
996d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_family = AF_INET6;
997d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
998efe4208fSEric Dumazet 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
999efe4208fSEric Dumazet 		       sizeof(tunnel->sock->sk_v6_daddr));
1000d2cf3361SBenjamin LaHaise 		memcpy(uaddr, &sp, len);
1001d2cf3361SBenjamin LaHaise #endif
1002e0d4435fSJames Chapman 	} else if (tunnel->version == 3) {
1003e0d4435fSJames Chapman 		struct sockaddr_pppol2tpv3 sp;
1004e0d4435fSJames Chapman 		len = sizeof(sp);
1005e0d4435fSJames Chapman 		memset(&sp, 0, len);
1006e0d4435fSJames Chapman 		sp.sa_family	= AF_PPPOX;
1007e0d4435fSJames Chapman 		sp.sa_protocol	= PX_PROTO_OL2TP;
1008e0d4435fSJames Chapman 		sp.pppol2tp.fd  = tunnel->fd;
1009e0d4435fSJames Chapman 		sp.pppol2tp.pid = pls->owner;
1010e0d4435fSJames Chapman 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
1011e0d4435fSJames Chapman 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
1012e0d4435fSJames Chapman 		sp.pppol2tp.s_session = session->session_id;
1013e0d4435fSJames Chapman 		sp.pppol2tp.d_session = session->peer_session_id;
1014e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_family = AF_INET;
1015e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
1016e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
1017e0d4435fSJames Chapman 		memcpy(uaddr, &sp, len);
1018e0d4435fSJames Chapman 	}
1019fd558d18SJames Chapman 
10209b2c45d4SDenys Vlasenko 	error = len;
1021fd558d18SJames Chapman 
1022fd558d18SJames Chapman 	sock_put(sk);
1023fd558d18SJames Chapman end:
1024fd558d18SJames Chapman 	return error;
1025fd558d18SJames Chapman }
1026fd558d18SJames Chapman 
1027fd558d18SJames Chapman /****************************************************************************
1028fd558d18SJames Chapman  * ioctl() handlers.
1029fd558d18SJames Chapman  *
1030fd558d18SJames Chapman  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1031fd558d18SJames Chapman  * sockets. However, in order to control kernel tunnel features, we allow
1032fd558d18SJames Chapman  * userspace to create a special "tunnel" PPPoX socket which is used for
1033fd558d18SJames Chapman  * control only.  Tunnel PPPoX sockets have session_id == 0 and simply allow
1034fd558d18SJames Chapman  * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1035fd558d18SJames Chapman  * calls.
1036fd558d18SJames Chapman  ****************************************************************************/
1037fd558d18SJames Chapman 
1038fd558d18SJames Chapman static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1039fd558d18SJames Chapman 				struct l2tp_stats *stats)
1040fd558d18SJames Chapman {
10417b7c0719STom Parkin 	dest->tx_packets = atomic_long_read(&stats->tx_packets);
10427b7c0719STom Parkin 	dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
10437b7c0719STom Parkin 	dest->tx_errors = atomic_long_read(&stats->tx_errors);
10447b7c0719STom Parkin 	dest->rx_packets = atomic_long_read(&stats->rx_packets);
10457b7c0719STom Parkin 	dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
10467b7c0719STom Parkin 	dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
10477b7c0719STom Parkin 	dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
10487b7c0719STom Parkin 	dest->rx_errors = atomic_long_read(&stats->rx_errors);
1049fd558d18SJames Chapman }
1050fd558d18SJames Chapman 
1051fd558d18SJames Chapman /* Session ioctl helper.
1052fd558d18SJames Chapman  */
1053fd558d18SJames Chapman static int pppol2tp_session_ioctl(struct l2tp_session *session,
1054fd558d18SJames Chapman 				  unsigned int cmd, unsigned long arg)
1055fd558d18SJames Chapman {
1056fd558d18SJames Chapman 	struct ifreq ifr;
1057fd558d18SJames Chapman 	int err = 0;
1058fd558d18SJames Chapman 	struct sock *sk;
1059fd558d18SJames Chapman 	int val = (int) arg;
1060fd558d18SJames Chapman 	struct pppol2tp_session *ps = l2tp_session_priv(session);
1061fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1062fd558d18SJames Chapman 	struct pppol2tp_ioc_stats stats;
1063fd558d18SJames Chapman 
1064fba40c63SAsbjørn Sloth Tønnesen 	l2tp_dbg(session, L2TP_MSG_CONTROL,
1065fd558d18SJames Chapman 		 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1066fd558d18SJames Chapman 		 session->name, cmd, arg);
1067fd558d18SJames Chapman 
1068ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
10695903f594SGuillaume Nault 	if (!sk)
10705903f594SGuillaume Nault 		return -EBADR;
10715903f594SGuillaume Nault 
1072fd558d18SJames Chapman 	switch (cmd) {
1073fd558d18SJames Chapman 	case SIOCGIFMTU:
1074fd558d18SJames Chapman 		err = -ENXIO;
1075fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1076fd558d18SJames Chapman 			break;
1077fd558d18SJames Chapman 
1078fd558d18SJames Chapman 		err = -EFAULT;
1079fd558d18SJames Chapman 		if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1080fd558d18SJames Chapman 			break;
1081fd558d18SJames Chapman 		ifr.ifr_mtu = session->mtu;
1082fd558d18SJames Chapman 		if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1083fd558d18SJames Chapman 			break;
1084fd558d18SJames Chapman 
1085fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mtu=%d\n",
1086a4ca44faSJoe Perches 			  session->name, session->mtu);
1087fd558d18SJames Chapman 		err = 0;
1088fd558d18SJames Chapman 		break;
1089fd558d18SJames Chapman 
1090fd558d18SJames Chapman 	case SIOCSIFMTU:
1091fd558d18SJames Chapman 		err = -ENXIO;
1092fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1093fd558d18SJames Chapman 			break;
1094fd558d18SJames Chapman 
1095fd558d18SJames Chapman 		err = -EFAULT;
1096fd558d18SJames Chapman 		if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1097fd558d18SJames Chapman 			break;
1098fd558d18SJames Chapman 
1099fd558d18SJames Chapman 		session->mtu = ifr.ifr_mtu;
1100fd558d18SJames Chapman 
1101fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mtu=%d\n",
1102a4ca44faSJoe Perches 			  session->name, session->mtu);
1103fd558d18SJames Chapman 		err = 0;
1104fd558d18SJames Chapman 		break;
1105fd558d18SJames Chapman 
1106fd558d18SJames Chapman 	case PPPIOCGMRU:
1107fd558d18SJames Chapman 		err = -ENXIO;
1108fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1109fd558d18SJames Chapman 			break;
1110fd558d18SJames Chapman 
1111fd558d18SJames Chapman 		err = -EFAULT;
1112fd558d18SJames Chapman 		if (put_user(session->mru, (int __user *) arg))
1113fd558d18SJames Chapman 			break;
1114fd558d18SJames Chapman 
1115fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mru=%d\n",
1116a4ca44faSJoe Perches 			  session->name, session->mru);
1117fd558d18SJames Chapman 		err = 0;
1118fd558d18SJames Chapman 		break;
1119fd558d18SJames Chapman 
1120fd558d18SJames Chapman 	case PPPIOCSMRU:
1121fd558d18SJames Chapman 		err = -ENXIO;
1122fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1123fd558d18SJames Chapman 			break;
1124fd558d18SJames Chapman 
1125fd558d18SJames Chapman 		err = -EFAULT;
1126fd558d18SJames Chapman 		if (get_user(val, (int __user *) arg))
1127fd558d18SJames Chapman 			break;
1128fd558d18SJames Chapman 
1129fd558d18SJames Chapman 		session->mru = val;
1130fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mru=%d\n",
1131a4ca44faSJoe Perches 			  session->name, session->mru);
1132fd558d18SJames Chapman 		err = 0;
1133fd558d18SJames Chapman 		break;
1134fd558d18SJames Chapman 
1135fd558d18SJames Chapman 	case PPPIOCGFLAGS:
1136fd558d18SJames Chapman 		err = -EFAULT;
1137fd558d18SJames Chapman 		if (put_user(ps->flags, (int __user *) arg))
1138fd558d18SJames Chapman 			break;
1139fd558d18SJames Chapman 
1140fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get flags=%d\n",
1141a4ca44faSJoe Perches 			  session->name, ps->flags);
1142fd558d18SJames Chapman 		err = 0;
1143fd558d18SJames Chapman 		break;
1144fd558d18SJames Chapman 
1145fd558d18SJames Chapman 	case PPPIOCSFLAGS:
1146fd558d18SJames Chapman 		err = -EFAULT;
1147fd558d18SJames Chapman 		if (get_user(val, (int __user *) arg))
1148fd558d18SJames Chapman 			break;
1149fd558d18SJames Chapman 		ps->flags = val;
1150fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set flags=%d\n",
1151a4ca44faSJoe Perches 			  session->name, ps->flags);
1152fd558d18SJames Chapman 		err = 0;
1153fd558d18SJames Chapman 		break;
1154fd558d18SJames Chapman 
1155fd558d18SJames Chapman 	case PPPIOCGL2TPSTATS:
1156fd558d18SJames Chapman 		err = -ENXIO;
1157fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1158fd558d18SJames Chapman 			break;
1159fd558d18SJames Chapman 
1160fd558d18SJames Chapman 		memset(&stats, 0, sizeof(stats));
1161fd558d18SJames Chapman 		stats.tunnel_id = tunnel->tunnel_id;
1162fd558d18SJames Chapman 		stats.session_id = session->session_id;
1163fd558d18SJames Chapman 		pppol2tp_copy_stats(&stats, &session->stats);
1164fd558d18SJames Chapman 		if (copy_to_user((void __user *) arg, &stats,
1165fd558d18SJames Chapman 				 sizeof(stats)))
1166fd558d18SJames Chapman 			break;
1167fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1168a4ca44faSJoe Perches 			  session->name);
1169fd558d18SJames Chapman 		err = 0;
1170fd558d18SJames Chapman 		break;
1171fd558d18SJames Chapman 
1172fd558d18SJames Chapman 	default:
1173fd558d18SJames Chapman 		err = -ENOSYS;
1174fd558d18SJames Chapman 		break;
1175fd558d18SJames Chapman 	}
1176fd558d18SJames Chapman 
1177fd558d18SJames Chapman 	sock_put(sk);
1178fd558d18SJames Chapman 
1179fd558d18SJames Chapman 	return err;
1180fd558d18SJames Chapman }
1181fd558d18SJames Chapman 
1182fd558d18SJames Chapman /* Tunnel ioctl helper.
1183fd558d18SJames Chapman  *
1184fd558d18SJames Chapman  * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1185fd558d18SJames Chapman  * specifies a session_id, the session ioctl handler is called. This allows an
1186fd558d18SJames Chapman  * application to retrieve session stats via a tunnel socket.
1187fd558d18SJames Chapman  */
1188fd558d18SJames Chapman static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1189fd558d18SJames Chapman 				 unsigned int cmd, unsigned long arg)
1190fd558d18SJames Chapman {
1191fd558d18SJames Chapman 	int err = 0;
1192fd558d18SJames Chapman 	struct sock *sk;
1193fd558d18SJames Chapman 	struct pppol2tp_ioc_stats stats;
1194fd558d18SJames Chapman 
1195fba40c63SAsbjørn Sloth Tønnesen 	l2tp_dbg(tunnel, L2TP_MSG_CONTROL,
1196fd558d18SJames Chapman 		 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1197fd558d18SJames Chapman 		 tunnel->name, cmd, arg);
1198fd558d18SJames Chapman 
1199fd558d18SJames Chapman 	sk = tunnel->sock;
1200fd558d18SJames Chapman 	sock_hold(sk);
1201fd558d18SJames Chapman 
1202fd558d18SJames Chapman 	switch (cmd) {
1203fd558d18SJames Chapman 	case PPPIOCGL2TPSTATS:
1204fd558d18SJames Chapman 		err = -ENXIO;
1205fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1206fd558d18SJames Chapman 			break;
1207fd558d18SJames Chapman 
1208fd558d18SJames Chapman 		if (copy_from_user(&stats, (void __user *) arg,
1209fd558d18SJames Chapman 				   sizeof(stats))) {
1210fd558d18SJames Chapman 			err = -EFAULT;
1211fd558d18SJames Chapman 			break;
1212fd558d18SJames Chapman 		}
1213fd558d18SJames Chapman 		if (stats.session_id != 0) {
1214fd558d18SJames Chapman 			/* resend to session ioctl handler */
1215fd558d18SJames Chapman 			struct l2tp_session *session =
121657377d63SGuillaume Nault 				l2tp_session_get(sock_net(sk), tunnel,
1217a4346210SGuillaume Nault 						 stats.session_id);
121857377d63SGuillaume Nault 
1219ecd012e4SGuillaume Nault 			if (session && session->pwtype == L2TP_PWTYPE_PPP) {
122057377d63SGuillaume Nault 				err = pppol2tp_session_ioctl(session, cmd,
122157377d63SGuillaume Nault 							     arg);
122257377d63SGuillaume Nault 				l2tp_session_dec_refcount(session);
122357377d63SGuillaume Nault 			} else {
1224fd558d18SJames Chapman 				err = -EBADR;
122557377d63SGuillaume Nault 			}
1226fd558d18SJames Chapman 			break;
1227fd558d18SJames Chapman 		}
1228fd558d18SJames Chapman #ifdef CONFIG_XFRM
1229fd558d18SJames Chapman 		stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1230fd558d18SJames Chapman #endif
1231fd558d18SJames Chapman 		pppol2tp_copy_stats(&stats, &tunnel->stats);
1232fd558d18SJames Chapman 		if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1233fd558d18SJames Chapman 			err = -EFAULT;
1234fd558d18SJames Chapman 			break;
1235fd558d18SJames Chapman 		}
1236fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1237a4ca44faSJoe Perches 			  tunnel->name);
1238fd558d18SJames Chapman 		err = 0;
1239fd558d18SJames Chapman 		break;
1240fd558d18SJames Chapman 
1241fd558d18SJames Chapman 	default:
1242fd558d18SJames Chapman 		err = -ENOSYS;
1243fd558d18SJames Chapman 		break;
1244fd558d18SJames Chapman 	}
1245fd558d18SJames Chapman 
1246fd558d18SJames Chapman 	sock_put(sk);
1247fd558d18SJames Chapman 
1248fd558d18SJames Chapman 	return err;
1249fd558d18SJames Chapman }
1250fd558d18SJames Chapman 
1251fd558d18SJames Chapman /* Main ioctl() handler.
1252fd558d18SJames Chapman  * Dispatch to tunnel or session helpers depending on the socket.
1253fd558d18SJames Chapman  */
1254fd558d18SJames Chapman static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1255fd558d18SJames Chapman 			  unsigned long arg)
1256fd558d18SJames Chapman {
1257fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1258fd558d18SJames Chapman 	struct l2tp_session *session;
1259fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1260fd558d18SJames Chapman 	int err;
1261fd558d18SJames Chapman 
1262fd558d18SJames Chapman 	if (!sk)
1263fd558d18SJames Chapman 		return 0;
1264fd558d18SJames Chapman 
1265fd558d18SJames Chapman 	err = -EBADF;
1266fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) != 0)
1267fd558d18SJames Chapman 		goto end;
1268fd558d18SJames Chapman 
1269fd558d18SJames Chapman 	err = -ENOTCONN;
1270fd558d18SJames Chapman 	if ((sk->sk_user_data == NULL) ||
1271fd558d18SJames Chapman 	    (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1272fd558d18SJames Chapman 		goto end;
1273fd558d18SJames Chapman 
1274fd558d18SJames Chapman 	/* Get session context from the socket */
1275fd558d18SJames Chapman 	err = -EBADF;
1276fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
1277fd558d18SJames Chapman 	if (session == NULL)
1278fd558d18SJames Chapman 		goto end;
1279fd558d18SJames Chapman 
1280fd558d18SJames Chapman 	/* Special case: if session's session_id is zero, treat ioctl as a
1281fd558d18SJames Chapman 	 * tunnel ioctl
1282fd558d18SJames Chapman 	 */
1283fd558d18SJames Chapman 	if ((session->session_id == 0) &&
1284fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
12857198c77aSGuillaume Nault 		tunnel = session->tunnel;
1286fd558d18SJames Chapman 		err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1287fd558d18SJames Chapman 		goto end_put_sess;
1288fd558d18SJames Chapman 	}
1289fd558d18SJames Chapman 
1290fd558d18SJames Chapman 	err = pppol2tp_session_ioctl(session, cmd, arg);
1291fd558d18SJames Chapman 
1292fd558d18SJames Chapman end_put_sess:
1293fd558d18SJames Chapman 	sock_put(sk);
1294fd558d18SJames Chapman end:
1295fd558d18SJames Chapman 	return err;
1296fd558d18SJames Chapman }
1297fd558d18SJames Chapman 
1298fd558d18SJames Chapman /*****************************************************************************
1299fd558d18SJames Chapman  * setsockopt() / getsockopt() support.
1300fd558d18SJames Chapman  *
1301fd558d18SJames Chapman  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1302fd558d18SJames Chapman  * sockets. In order to control kernel tunnel features, we allow userspace to
1303fd558d18SJames Chapman  * create a special "tunnel" PPPoX socket which is used for control only.
1304fd558d18SJames Chapman  * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1305fd558d18SJames Chapman  * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1306fd558d18SJames Chapman  *****************************************************************************/
1307fd558d18SJames Chapman 
1308fd558d18SJames Chapman /* Tunnel setsockopt() helper.
1309fd558d18SJames Chapman  */
1310fd558d18SJames Chapman static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1311fd558d18SJames Chapman 				      struct l2tp_tunnel *tunnel,
1312fd558d18SJames Chapman 				      int optname, int val)
1313fd558d18SJames Chapman {
1314fd558d18SJames Chapman 	int err = 0;
1315fd558d18SJames Chapman 
1316fd558d18SJames Chapman 	switch (optname) {
1317fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1318fd558d18SJames Chapman 		tunnel->debug = val;
1319fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1320a4ca44faSJoe Perches 			  tunnel->name, tunnel->debug);
1321fd558d18SJames Chapman 		break;
1322fd558d18SJames Chapman 
1323fd558d18SJames Chapman 	default:
1324fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1325fd558d18SJames Chapman 		break;
1326fd558d18SJames Chapman 	}
1327fd558d18SJames Chapman 
1328fd558d18SJames Chapman 	return err;
1329fd558d18SJames Chapman }
1330fd558d18SJames Chapman 
1331fd558d18SJames Chapman /* Session setsockopt helper.
1332fd558d18SJames Chapman  */
1333fd558d18SJames Chapman static int pppol2tp_session_setsockopt(struct sock *sk,
1334fd558d18SJames Chapman 				       struct l2tp_session *session,
1335fd558d18SJames Chapman 				       int optname, int val)
1336fd558d18SJames Chapman {
1337fd558d18SJames Chapman 	int err = 0;
1338fd558d18SJames Chapman 
1339fd558d18SJames Chapman 	switch (optname) {
1340fd558d18SJames Chapman 	case PPPOL2TP_SO_RECVSEQ:
1341fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1342fd558d18SJames Chapman 			err = -EINVAL;
1343fd558d18SJames Chapman 			break;
1344fd558d18SJames Chapman 		}
13453f9b9770SAsbjørn Sloth Tønnesen 		session->recv_seq = !!val;
1346fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1347a4ca44faSJoe Perches 			  "%s: set recv_seq=%d\n",
1348a4ca44faSJoe Perches 			  session->name, session->recv_seq);
1349fd558d18SJames Chapman 		break;
1350fd558d18SJames Chapman 
1351fd558d18SJames Chapman 	case PPPOL2TP_SO_SENDSEQ:
1352fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1353fd558d18SJames Chapman 			err = -EINVAL;
1354fd558d18SJames Chapman 			break;
1355fd558d18SJames Chapman 		}
13563f9b9770SAsbjørn Sloth Tønnesen 		session->send_seq = !!val;
1357fd558d18SJames Chapman 		{
1358ee40fb2eSGuillaume Nault 			struct pppox_sock *po = pppox_sk(sk);
1359ee40fb2eSGuillaume Nault 
1360fd558d18SJames Chapman 			po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1361fd558d18SJames Chapman 				PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1362fd558d18SJames Chapman 		}
1363bb5016eaSGuillaume Nault 		l2tp_session_set_header_len(session, session->tunnel->version);
1364fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1365a4ca44faSJoe Perches 			  "%s: set send_seq=%d\n",
1366a4ca44faSJoe Perches 			  session->name, session->send_seq);
1367fd558d18SJames Chapman 		break;
1368fd558d18SJames Chapman 
1369fd558d18SJames Chapman 	case PPPOL2TP_SO_LNSMODE:
1370fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1371fd558d18SJames Chapman 			err = -EINVAL;
1372fd558d18SJames Chapman 			break;
1373fd558d18SJames Chapman 		}
13743f9b9770SAsbjørn Sloth Tønnesen 		session->lns_mode = !!val;
1375fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1376a4ca44faSJoe Perches 			  "%s: set lns_mode=%d\n",
1377a4ca44faSJoe Perches 			  session->name, session->lns_mode);
1378fd558d18SJames Chapman 		break;
1379fd558d18SJames Chapman 
1380fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1381fd558d18SJames Chapman 		session->debug = val;
1382fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1383a4ca44faSJoe Perches 			  session->name, session->debug);
1384fd558d18SJames Chapman 		break;
1385fd558d18SJames Chapman 
1386fd558d18SJames Chapman 	case PPPOL2TP_SO_REORDERTO:
1387fd558d18SJames Chapman 		session->reorder_timeout = msecs_to_jiffies(val);
1388fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1389a4ca44faSJoe Perches 			  "%s: set reorder_timeout=%d\n",
1390a4ca44faSJoe Perches 			  session->name, session->reorder_timeout);
1391fd558d18SJames Chapman 		break;
1392fd558d18SJames Chapman 
1393fd558d18SJames Chapman 	default:
1394fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1395fd558d18SJames Chapman 		break;
1396fd558d18SJames Chapman 	}
1397fd558d18SJames Chapman 
1398fd558d18SJames Chapman 	return err;
1399fd558d18SJames Chapman }
1400fd558d18SJames Chapman 
1401fd558d18SJames Chapman /* Main setsockopt() entry point.
1402fd558d18SJames Chapman  * Does API checks, then calls either the tunnel or session setsockopt
1403fd558d18SJames Chapman  * handler, according to whether the PPPoL2TP socket is a for a regular
1404fd558d18SJames Chapman  * session or the special tunnel type.
1405fd558d18SJames Chapman  */
1406fd558d18SJames Chapman static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1407fd558d18SJames Chapman 			       char __user *optval, unsigned int optlen)
1408fd558d18SJames Chapman {
1409fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1410fd558d18SJames Chapman 	struct l2tp_session *session;
1411fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1412fd558d18SJames Chapman 	int val;
1413fd558d18SJames Chapman 	int err;
1414fd558d18SJames Chapman 
1415fd558d18SJames Chapman 	if (level != SOL_PPPOL2TP)
14163cf521f7SSasha Levin 		return -EINVAL;
1417fd558d18SJames Chapman 
1418fd558d18SJames Chapman 	if (optlen < sizeof(int))
1419fd558d18SJames Chapman 		return -EINVAL;
1420fd558d18SJames Chapman 
1421fd558d18SJames Chapman 	if (get_user(val, (int __user *)optval))
1422fd558d18SJames Chapman 		return -EFAULT;
1423fd558d18SJames Chapman 
1424fd558d18SJames Chapman 	err = -ENOTCONN;
1425fd558d18SJames Chapman 	if (sk->sk_user_data == NULL)
1426fd558d18SJames Chapman 		goto end;
1427fd558d18SJames Chapman 
1428fd558d18SJames Chapman 	/* Get session context from the socket */
1429fd558d18SJames Chapman 	err = -EBADF;
1430fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
1431fd558d18SJames Chapman 	if (session == NULL)
1432fd558d18SJames Chapman 		goto end;
1433fd558d18SJames Chapman 
1434fd558d18SJames Chapman 	/* Special case: if session_id == 0x0000, treat as operation on tunnel
1435fd558d18SJames Chapman 	 */
1436fd558d18SJames Chapman 	if ((session->session_id == 0) &&
1437fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
14387198c77aSGuillaume Nault 		tunnel = session->tunnel;
1439fd558d18SJames Chapman 		err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
14407198c77aSGuillaume Nault 	} else {
1441fd558d18SJames Chapman 		err = pppol2tp_session_setsockopt(sk, session, optname, val);
14427198c77aSGuillaume Nault 	}
1443fd558d18SJames Chapman 
1444fd558d18SJames Chapman 	sock_put(sk);
1445fd558d18SJames Chapman end:
1446fd558d18SJames Chapman 	return err;
1447fd558d18SJames Chapman }
1448fd558d18SJames Chapman 
1449fd558d18SJames Chapman /* Tunnel getsockopt helper. Called with sock locked.
1450fd558d18SJames Chapman  */
1451fd558d18SJames Chapman static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1452fd558d18SJames Chapman 				      struct l2tp_tunnel *tunnel,
1453fd558d18SJames Chapman 				      int optname, int *val)
1454fd558d18SJames Chapman {
1455fd558d18SJames Chapman 	int err = 0;
1456fd558d18SJames Chapman 
1457fd558d18SJames Chapman 	switch (optname) {
1458fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1459fd558d18SJames Chapman 		*val = tunnel->debug;
1460fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
1461a4ca44faSJoe Perches 			  tunnel->name, tunnel->debug);
1462fd558d18SJames Chapman 		break;
1463fd558d18SJames Chapman 
1464fd558d18SJames Chapman 	default:
1465fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1466fd558d18SJames Chapman 		break;
1467fd558d18SJames Chapman 	}
1468fd558d18SJames Chapman 
1469fd558d18SJames Chapman 	return err;
1470fd558d18SJames Chapman }
1471fd558d18SJames Chapman 
1472fd558d18SJames Chapman /* Session getsockopt helper. Called with sock locked.
1473fd558d18SJames Chapman  */
1474fd558d18SJames Chapman static int pppol2tp_session_getsockopt(struct sock *sk,
1475fd558d18SJames Chapman 				       struct l2tp_session *session,
1476fd558d18SJames Chapman 				       int optname, int *val)
1477fd558d18SJames Chapman {
1478fd558d18SJames Chapman 	int err = 0;
1479fd558d18SJames Chapman 
1480fd558d18SJames Chapman 	switch (optname) {
1481fd558d18SJames Chapman 	case PPPOL2TP_SO_RECVSEQ:
1482fd558d18SJames Chapman 		*val = session->recv_seq;
1483fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1484fd558d18SJames Chapman 			  "%s: get recv_seq=%d\n", session->name, *val);
1485fd558d18SJames Chapman 		break;
1486fd558d18SJames Chapman 
1487fd558d18SJames Chapman 	case PPPOL2TP_SO_SENDSEQ:
1488fd558d18SJames Chapman 		*val = session->send_seq;
1489fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1490fd558d18SJames Chapman 			  "%s: get send_seq=%d\n", session->name, *val);
1491fd558d18SJames Chapman 		break;
1492fd558d18SJames Chapman 
1493fd558d18SJames Chapman 	case PPPOL2TP_SO_LNSMODE:
1494fd558d18SJames Chapman 		*val = session->lns_mode;
1495fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1496fd558d18SJames Chapman 			  "%s: get lns_mode=%d\n", session->name, *val);
1497fd558d18SJames Chapman 		break;
1498fd558d18SJames Chapman 
1499fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1500fd558d18SJames Chapman 		*val = session->debug;
1501fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
1502a4ca44faSJoe Perches 			  session->name, *val);
1503fd558d18SJames Chapman 		break;
1504fd558d18SJames Chapman 
1505fd558d18SJames Chapman 	case PPPOL2TP_SO_REORDERTO:
1506fd558d18SJames Chapman 		*val = (int) jiffies_to_msecs(session->reorder_timeout);
1507fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1508fd558d18SJames Chapman 			  "%s: get reorder_timeout=%d\n", session->name, *val);
1509fd558d18SJames Chapman 		break;
1510fd558d18SJames Chapman 
1511fd558d18SJames Chapman 	default:
1512fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1513fd558d18SJames Chapman 	}
1514fd558d18SJames Chapman 
1515fd558d18SJames Chapman 	return err;
1516fd558d18SJames Chapman }
1517fd558d18SJames Chapman 
1518fd558d18SJames Chapman /* Main getsockopt() entry point.
1519fd558d18SJames Chapman  * Does API checks, then calls either the tunnel or session getsockopt
1520fd558d18SJames Chapman  * handler, according to whether the PPPoX socket is a for a regular session
1521fd558d18SJames Chapman  * or the special tunnel type.
1522fd558d18SJames Chapman  */
1523e3192690SJoe Perches static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1524e3192690SJoe Perches 			       char __user *optval, int __user *optlen)
1525fd558d18SJames Chapman {
1526fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1527fd558d18SJames Chapman 	struct l2tp_session *session;
1528fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1529fd558d18SJames Chapman 	int val, len;
1530fd558d18SJames Chapman 	int err;
1531fd558d18SJames Chapman 
1532fd558d18SJames Chapman 	if (level != SOL_PPPOL2TP)
15333cf521f7SSasha Levin 		return -EINVAL;
1534fd558d18SJames Chapman 
1535e3192690SJoe Perches 	if (get_user(len, optlen))
1536fd558d18SJames Chapman 		return -EFAULT;
1537fd558d18SJames Chapman 
1538fd558d18SJames Chapman 	len = min_t(unsigned int, len, sizeof(int));
1539fd558d18SJames Chapman 
1540fd558d18SJames Chapman 	if (len < 0)
1541fd558d18SJames Chapman 		return -EINVAL;
1542fd558d18SJames Chapman 
1543fd558d18SJames Chapman 	err = -ENOTCONN;
1544fd558d18SJames Chapman 	if (sk->sk_user_data == NULL)
1545fd558d18SJames Chapman 		goto end;
1546fd558d18SJames Chapman 
1547fd558d18SJames Chapman 	/* Get the session context */
1548fd558d18SJames Chapman 	err = -EBADF;
1549fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
1550fd558d18SJames Chapman 	if (session == NULL)
1551fd558d18SJames Chapman 		goto end;
1552fd558d18SJames Chapman 
1553fd558d18SJames Chapman 	/* Special case: if session_id == 0x0000, treat as operation on tunnel */
1554fd558d18SJames Chapman 	if ((session->session_id == 0) &&
1555fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
15567198c77aSGuillaume Nault 		tunnel = session->tunnel;
1557fd558d18SJames Chapman 		err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1558321a52a3SGuillaume Nault 		if (err)
1559321a52a3SGuillaume Nault 			goto end_put_sess;
1560321a52a3SGuillaume Nault 	} else {
1561fd558d18SJames Chapman 		err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1562321a52a3SGuillaume Nault 		if (err)
1563321a52a3SGuillaume Nault 			goto end_put_sess;
1564321a52a3SGuillaume Nault 	}
1565fd558d18SJames Chapman 
1566fd558d18SJames Chapman 	err = -EFAULT;
1567e3192690SJoe Perches 	if (put_user(len, optlen))
1568fd558d18SJames Chapman 		goto end_put_sess;
1569fd558d18SJames Chapman 
1570fd558d18SJames Chapman 	if (copy_to_user((void __user *) optval, &val, len))
1571fd558d18SJames Chapman 		goto end_put_sess;
1572fd558d18SJames Chapman 
1573fd558d18SJames Chapman 	err = 0;
1574fd558d18SJames Chapman 
1575fd558d18SJames Chapman end_put_sess:
1576fd558d18SJames Chapman 	sock_put(sk);
1577fd558d18SJames Chapman end:
1578fd558d18SJames Chapman 	return err;
1579fd558d18SJames Chapman }
1580fd558d18SJames Chapman 
1581fd558d18SJames Chapman /*****************************************************************************
1582fd558d18SJames Chapman  * /proc filesystem for debug
1583f7faffa3SJames Chapman  * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1584f7faffa3SJames Chapman  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1585fd558d18SJames Chapman  *****************************************************************************/
1586fd558d18SJames Chapman 
1587fd558d18SJames Chapman static unsigned int pppol2tp_net_id;
1588fd558d18SJames Chapman 
1589fd558d18SJames Chapman #ifdef CONFIG_PROC_FS
1590fd558d18SJames Chapman 
1591fd558d18SJames Chapman struct pppol2tp_seq_data {
1592fd558d18SJames Chapman 	struct seq_net_private p;
1593fd558d18SJames Chapman 	int tunnel_idx;			/* current tunnel */
1594fd558d18SJames Chapman 	int session_idx;		/* index of session within current tunnel */
1595fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1596fd558d18SJames Chapman 	struct l2tp_session *session;	/* NULL means get next tunnel */
1597fd558d18SJames Chapman };
1598fd558d18SJames Chapman 
1599fd558d18SJames Chapman static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1600fd558d18SJames Chapman {
16010e0c3feeSGuillaume Nault 	/* Drop reference taken during previous invocation */
16020e0c3feeSGuillaume Nault 	if (pd->tunnel)
16030e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
16040e0c3feeSGuillaume Nault 
1605f7faffa3SJames Chapman 	for (;;) {
16060e0c3feeSGuillaume Nault 		pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
1607fd558d18SJames Chapman 		pd->tunnel_idx++;
1608f7faffa3SJames Chapman 
16090e0c3feeSGuillaume Nault 		/* Only accept L2TPv2 tunnels */
16100e0c3feeSGuillaume Nault 		if (!pd->tunnel || pd->tunnel->version == 2)
16110e0c3feeSGuillaume Nault 			return;
1612f7faffa3SJames Chapman 
16130e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
1614f7faffa3SJames Chapman 	}
1615fd558d18SJames Chapman }
1616fd558d18SJames Chapman 
1617fd558d18SJames Chapman static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1618fd558d18SJames Chapman {
161983494407SGuillaume Nault 	/* Drop reference taken during previous invocation */
162083494407SGuillaume Nault 	if (pd->session)
162183494407SGuillaume Nault 		l2tp_session_dec_refcount(pd->session);
162283494407SGuillaume Nault 
1623a4346210SGuillaume Nault 	pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
1624fd558d18SJames Chapman 	pd->session_idx++;
1625f7faffa3SJames Chapman 
1626fd558d18SJames Chapman 	if (pd->session == NULL) {
1627fd558d18SJames Chapman 		pd->session_idx = 0;
1628fd558d18SJames Chapman 		pppol2tp_next_tunnel(net, pd);
1629fd558d18SJames Chapman 	}
1630fd558d18SJames Chapman }
1631fd558d18SJames Chapman 
1632fd558d18SJames Chapman static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1633fd558d18SJames Chapman {
1634fd558d18SJames Chapman 	struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1635fd558d18SJames Chapman 	loff_t pos = *offs;
1636fd558d18SJames Chapman 	struct net *net;
1637fd558d18SJames Chapman 
1638fd558d18SJames Chapman 	if (!pos)
1639fd558d18SJames Chapman 		goto out;
1640fd558d18SJames Chapman 
1641fd558d18SJames Chapman 	BUG_ON(m->private == NULL);
1642fd558d18SJames Chapman 	pd = m->private;
1643fd558d18SJames Chapman 	net = seq_file_net(m);
1644fd558d18SJames Chapman 
1645fd558d18SJames Chapman 	if (pd->tunnel == NULL)
1646fd558d18SJames Chapman 		pppol2tp_next_tunnel(net, pd);
1647fd558d18SJames Chapman 	else
1648fd558d18SJames Chapman 		pppol2tp_next_session(net, pd);
1649fd558d18SJames Chapman 
1650fd558d18SJames Chapman 	/* NULL tunnel and session indicates end of list */
1651fd558d18SJames Chapman 	if ((pd->tunnel == NULL) && (pd->session == NULL))
1652fd558d18SJames Chapman 		pd = NULL;
1653fd558d18SJames Chapman 
1654fd558d18SJames Chapman out:
1655fd558d18SJames Chapman 	return pd;
1656fd558d18SJames Chapman }
1657fd558d18SJames Chapman 
1658fd558d18SJames Chapman static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1659fd558d18SJames Chapman {
1660fd558d18SJames Chapman 	(*pos)++;
1661fd558d18SJames Chapman 	return NULL;
1662fd558d18SJames Chapman }
1663fd558d18SJames Chapman 
1664fd558d18SJames Chapman static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1665fd558d18SJames Chapman {
16660e0c3feeSGuillaume Nault 	struct pppol2tp_seq_data *pd = v;
16670e0c3feeSGuillaume Nault 
16680e0c3feeSGuillaume Nault 	if (!pd || pd == SEQ_START_TOKEN)
16690e0c3feeSGuillaume Nault 		return;
16700e0c3feeSGuillaume Nault 
167183494407SGuillaume Nault 	/* Drop reference taken by last invocation of pppol2tp_next_session()
167283494407SGuillaume Nault 	 * or pppol2tp_next_tunnel().
167383494407SGuillaume Nault 	 */
167483494407SGuillaume Nault 	if (pd->session) {
167583494407SGuillaume Nault 		l2tp_session_dec_refcount(pd->session);
167683494407SGuillaume Nault 		pd->session = NULL;
167783494407SGuillaume Nault 	}
16785411b618SGuillaume Nault 	if (pd->tunnel) {
16790e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
16805411b618SGuillaume Nault 		pd->tunnel = NULL;
16815411b618SGuillaume Nault 	}
1682fd558d18SJames Chapman }
1683fd558d18SJames Chapman 
1684fd558d18SJames Chapman static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1685fd558d18SJames Chapman {
1686fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = v;
1687fd558d18SJames Chapman 
1688fd558d18SJames Chapman 	seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1689fd558d18SJames Chapman 		   tunnel->name,
1690fd558d18SJames Chapman 		   (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1691fbea9e07SReshetova, Elena 		   refcount_read(&tunnel->ref_count) - 1);
16927b7c0719STom Parkin 	seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1693fd558d18SJames Chapman 		   tunnel->debug,
16947b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_packets),
16957b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_bytes),
16967b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_errors),
16977b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_packets),
16987b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_bytes),
16997b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_errors));
1700fd558d18SJames Chapman }
1701fd558d18SJames Chapman 
1702fd558d18SJames Chapman static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1703fd558d18SJames Chapman {
1704fd558d18SJames Chapman 	struct l2tp_session *session = v;
1705fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1706ee40fb2eSGuillaume Nault 	unsigned char state;
1707ee40fb2eSGuillaume Nault 	char user_data_ok;
1708ee40fb2eSGuillaume Nault 	struct sock *sk;
1709fd558d18SJames Chapman 	u32 ip = 0;
1710fd558d18SJames Chapman 	u16 port = 0;
1711fd558d18SJames Chapman 
1712fd558d18SJames Chapman 	if (tunnel->sock) {
1713fd558d18SJames Chapman 		struct inet_sock *inet = inet_sk(tunnel->sock);
1714fd558d18SJames Chapman 		ip = ntohl(inet->inet_saddr);
1715fd558d18SJames Chapman 		port = ntohs(inet->inet_sport);
1716fd558d18SJames Chapman 	}
1717fd558d18SJames Chapman 
1718ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
1719ee40fb2eSGuillaume Nault 	if (sk) {
1720ee40fb2eSGuillaume Nault 		state = sk->sk_state;
1721ee40fb2eSGuillaume Nault 		user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1722ee40fb2eSGuillaume Nault 	} else {
1723ee40fb2eSGuillaume Nault 		state = 0;
1724ee40fb2eSGuillaume Nault 		user_data_ok = 'N';
1725ee40fb2eSGuillaume Nault 	}
1726ee40fb2eSGuillaume Nault 
1727fd558d18SJames Chapman 	seq_printf(m, "  SESSION '%s' %08X/%d %04X/%04X -> "
1728fd558d18SJames Chapman 		   "%04X/%04X %d %c\n",
1729fd558d18SJames Chapman 		   session->name, ip, port,
1730fd558d18SJames Chapman 		   tunnel->tunnel_id,
1731fd558d18SJames Chapman 		   session->session_id,
1732fd558d18SJames Chapman 		   tunnel->peer_tunnel_id,
1733fd558d18SJames Chapman 		   session->peer_session_id,
1734ee40fb2eSGuillaume Nault 		   state, user_data_ok);
1735fd558d18SJames Chapman 	seq_printf(m, "   %d/%d/%c/%c/%s %08x %u\n",
1736fd558d18SJames Chapman 		   session->mtu, session->mru,
1737fd558d18SJames Chapman 		   session->recv_seq ? 'R' : '-',
1738fd558d18SJames Chapman 		   session->send_seq ? 'S' : '-',
1739fd558d18SJames Chapman 		   session->lns_mode ? "LNS" : "LAC",
1740fd558d18SJames Chapman 		   session->debug,
1741fd558d18SJames Chapman 		   jiffies_to_msecs(session->reorder_timeout));
17427b7c0719STom Parkin 	seq_printf(m, "   %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
1743fd558d18SJames Chapman 		   session->nr, session->ns,
17447b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_packets),
17457b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_bytes),
17467b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_errors),
17477b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_packets),
17487b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_bytes),
17497b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_errors));
17509345471bSJames Chapman 
1751ee40fb2eSGuillaume Nault 	if (sk) {
1752ee40fb2eSGuillaume Nault 		struct pppox_sock *po = pppox_sk(sk);
1753ee40fb2eSGuillaume Nault 
17549345471bSJames Chapman 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
1755ee40fb2eSGuillaume Nault 		sock_put(sk);
1756ee40fb2eSGuillaume Nault 	}
1757fd558d18SJames Chapman }
1758fd558d18SJames Chapman 
1759fd558d18SJames Chapman static int pppol2tp_seq_show(struct seq_file *m, void *v)
1760fd558d18SJames Chapman {
1761fd558d18SJames Chapman 	struct pppol2tp_seq_data *pd = v;
1762fd558d18SJames Chapman 
1763fd558d18SJames Chapman 	/* display header on line 1 */
1764fd558d18SJames Chapman 	if (v == SEQ_START_TOKEN) {
1765fd558d18SJames Chapman 		seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1766fd558d18SJames Chapman 		seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1767fd558d18SJames Chapman 		seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1768fd558d18SJames Chapman 		seq_puts(m, "  SESSION name, addr/port src-tid/sid "
1769fd558d18SJames Chapman 			 "dest-tid/sid state user-data-ok\n");
1770fd558d18SJames Chapman 		seq_puts(m, "   mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1771fd558d18SJames Chapman 		seq_puts(m, "   nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1772fd558d18SJames Chapman 		goto out;
1773fd558d18SJames Chapman 	}
1774fd558d18SJames Chapman 
177583494407SGuillaume Nault 	if (!pd->session)
1776fd558d18SJames Chapman 		pppol2tp_seq_tunnel_show(m, pd->tunnel);
177783494407SGuillaume Nault 	else
1778fd558d18SJames Chapman 		pppol2tp_seq_session_show(m, pd->session);
1779fd558d18SJames Chapman 
1780fd558d18SJames Chapman out:
1781fd558d18SJames Chapman 	return 0;
1782fd558d18SJames Chapman }
1783fd558d18SJames Chapman 
1784fd558d18SJames Chapman static const struct seq_operations pppol2tp_seq_ops = {
1785fd558d18SJames Chapman 	.start		= pppol2tp_seq_start,
1786fd558d18SJames Chapman 	.next		= pppol2tp_seq_next,
1787fd558d18SJames Chapman 	.stop		= pppol2tp_seq_stop,
1788fd558d18SJames Chapman 	.show		= pppol2tp_seq_show,
1789fd558d18SJames Chapman };
1790fd558d18SJames Chapman #endif /* CONFIG_PROC_FS */
1791fd558d18SJames Chapman 
1792fd558d18SJames Chapman /*****************************************************************************
1793fd558d18SJames Chapman  * Network namespace
1794fd558d18SJames Chapman  *****************************************************************************/
1795fd558d18SJames Chapman 
1796fd558d18SJames Chapman static __net_init int pppol2tp_init_net(struct net *net)
1797fd558d18SJames Chapman {
1798fd558d18SJames Chapman 	struct proc_dir_entry *pde;
1799fd558d18SJames Chapman 	int err = 0;
1800fd558d18SJames Chapman 
1801c3506372SChristoph Hellwig 	pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1802c3506372SChristoph Hellwig 			&pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
1803fd558d18SJames Chapman 	if (!pde) {
1804fd558d18SJames Chapman 		err = -ENOMEM;
1805fd558d18SJames Chapman 		goto out;
1806fd558d18SJames Chapman 	}
1807fd558d18SJames Chapman 
1808fd558d18SJames Chapman out:
1809fd558d18SJames Chapman 	return err;
1810fd558d18SJames Chapman }
1811fd558d18SJames Chapman 
1812fd558d18SJames Chapman static __net_exit void pppol2tp_exit_net(struct net *net)
1813fd558d18SJames Chapman {
1814ece31ffdSGao feng 	remove_proc_entry("pppol2tp", net->proc_net);
1815fd558d18SJames Chapman }
1816fd558d18SJames Chapman 
1817fd558d18SJames Chapman static struct pernet_operations pppol2tp_net_ops = {
1818fd558d18SJames Chapman 	.init = pppol2tp_init_net,
1819fd558d18SJames Chapman 	.exit = pppol2tp_exit_net,
1820fd558d18SJames Chapman 	.id   = &pppol2tp_net_id,
1821fd558d18SJames Chapman };
1822fd558d18SJames Chapman 
1823fd558d18SJames Chapman /*****************************************************************************
1824fd558d18SJames Chapman  * Init and cleanup
1825fd558d18SJames Chapman  *****************************************************************************/
1826fd558d18SJames Chapman 
1827fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops = {
1828fd558d18SJames Chapman 	.family		= AF_PPPOX,
1829fd558d18SJames Chapman 	.owner		= THIS_MODULE,
1830fd558d18SJames Chapman 	.release	= pppol2tp_release,
1831fd558d18SJames Chapman 	.bind		= sock_no_bind,
1832fd558d18SJames Chapman 	.connect	= pppol2tp_connect,
1833fd558d18SJames Chapman 	.socketpair	= sock_no_socketpair,
1834fd558d18SJames Chapman 	.accept		= sock_no_accept,
1835fd558d18SJames Chapman 	.getname	= pppol2tp_getname,
1836a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
1837fd558d18SJames Chapman 	.listen		= sock_no_listen,
1838fd558d18SJames Chapman 	.shutdown	= sock_no_shutdown,
1839fd558d18SJames Chapman 	.setsockopt	= pppol2tp_setsockopt,
1840fd558d18SJames Chapman 	.getsockopt	= pppol2tp_getsockopt,
1841fd558d18SJames Chapman 	.sendmsg	= pppol2tp_sendmsg,
1842fd558d18SJames Chapman 	.recvmsg	= pppol2tp_recvmsg,
1843fd558d18SJames Chapman 	.mmap		= sock_no_mmap,
1844fd558d18SJames Chapman 	.ioctl		= pppox_ioctl,
1845fd558d18SJames Chapman };
1846fd558d18SJames Chapman 
1847756e64a0SEric Dumazet static const struct pppox_proto pppol2tp_proto = {
1848fd558d18SJames Chapman 	.create		= pppol2tp_create,
1849e1558a93SWei Yongjun 	.ioctl		= pppol2tp_ioctl,
1850e1558a93SWei Yongjun 	.owner		= THIS_MODULE,
1851fd558d18SJames Chapman };
1852fd558d18SJames Chapman 
1853309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1854309795f4SJames Chapman 
1855309795f4SJames Chapman static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1856309795f4SJames Chapman 	.session_create	= pppol2tp_session_create,
1857cf2f5c88STom Parkin 	.session_delete	= l2tp_session_delete,
1858309795f4SJames Chapman };
1859309795f4SJames Chapman 
1860309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */
1861309795f4SJames Chapman 
1862fd558d18SJames Chapman static int __init pppol2tp_init(void)
1863fd558d18SJames Chapman {
1864fd558d18SJames Chapman 	int err;
1865fd558d18SJames Chapman 
1866fd558d18SJames Chapman 	err = register_pernet_device(&pppol2tp_net_ops);
1867fd558d18SJames Chapman 	if (err)
1868fd558d18SJames Chapman 		goto out;
1869fd558d18SJames Chapman 
1870fd558d18SJames Chapman 	err = proto_register(&pppol2tp_sk_proto, 0);
1871fd558d18SJames Chapman 	if (err)
1872fd558d18SJames Chapman 		goto out_unregister_pppol2tp_pernet;
1873fd558d18SJames Chapman 
1874fd558d18SJames Chapman 	err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1875fd558d18SJames Chapman 	if (err)
1876fd558d18SJames Chapman 		goto out_unregister_pppol2tp_proto;
1877fd558d18SJames Chapman 
1878309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1879309795f4SJames Chapman 	err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1880309795f4SJames Chapman 	if (err)
1881309795f4SJames Chapman 		goto out_unregister_pppox;
1882309795f4SJames Chapman #endif
1883309795f4SJames Chapman 
1884a4ca44faSJoe Perches 	pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1885fd558d18SJames Chapman 
1886fd558d18SJames Chapman out:
1887fd558d18SJames Chapman 	return err;
1888309795f4SJames Chapman 
1889309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1890309795f4SJames Chapman out_unregister_pppox:
1891309795f4SJames Chapman 	unregister_pppox_proto(PX_PROTO_OL2TP);
1892309795f4SJames Chapman #endif
1893fd558d18SJames Chapman out_unregister_pppol2tp_proto:
1894fd558d18SJames Chapman 	proto_unregister(&pppol2tp_sk_proto);
1895fd558d18SJames Chapman out_unregister_pppol2tp_pernet:
1896fd558d18SJames Chapman 	unregister_pernet_device(&pppol2tp_net_ops);
1897fd558d18SJames Chapman 	goto out;
1898fd558d18SJames Chapman }
1899fd558d18SJames Chapman 
1900fd558d18SJames Chapman static void __exit pppol2tp_exit(void)
1901fd558d18SJames Chapman {
1902309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1903309795f4SJames Chapman 	l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1904309795f4SJames Chapman #endif
1905fd558d18SJames Chapman 	unregister_pppox_proto(PX_PROTO_OL2TP);
1906fd558d18SJames Chapman 	proto_unregister(&pppol2tp_sk_proto);
1907fd558d18SJames Chapman 	unregister_pernet_device(&pppol2tp_net_ops);
1908fd558d18SJames Chapman }
1909fd558d18SJames Chapman 
1910fd558d18SJames Chapman module_init(pppol2tp_init);
1911fd558d18SJames Chapman module_exit(pppol2tp_exit);
1912fd558d18SJames Chapman 
1913fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1914fd558d18SJames Chapman MODULE_DESCRIPTION("PPP over L2TP over UDP");
1915fd558d18SJames Chapman MODULE_LICENSE("GPL");
1916fd558d18SJames Chapman MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1917681b4d88SGuillaume Nault MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1918249ee819SGuillaume Nault MODULE_ALIAS_L2TP_PWTYPE(7);
1919