xref: /openbmc/linux/net/l2tp/l2tp_ppp.c (revision 0e0c3fee3a59a387aeecc4fca6f3a2e9615a5443)
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 static int pppol2tp_recv_payload_hook(struct sk_buff *skb)
187fd558d18SJames Chapman {
188fd558d18SJames Chapman 	/* Skip PPP header, if present.	 In testing, Microsoft L2TP clients
189fd558d18SJames Chapman 	 * don't send the PPP header (PPP header compression enabled), but
190fd558d18SJames Chapman 	 * other clients can include the header. So we cope with both cases
191fd558d18SJames Chapman 	 * here. The PPP header is always FF03 when using L2TP.
192fd558d18SJames Chapman 	 *
193fd558d18SJames Chapman 	 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
194fd558d18SJames Chapman 	 * the field may be unaligned.
195fd558d18SJames Chapman 	 */
196fd558d18SJames Chapman 	if (!pskb_may_pull(skb, 2))
197fd558d18SJames Chapman 		return 1;
198fd558d18SJames Chapman 
19954c151d9SGao Feng 	if ((skb->data[0] == PPP_ALLSTATIONS) && (skb->data[1] == PPP_UI))
200fd558d18SJames Chapman 		skb_pull(skb, 2);
201fd558d18SJames Chapman 
202fd558d18SJames Chapman 	return 0;
203fd558d18SJames Chapman }
204fd558d18SJames Chapman 
205fd558d18SJames Chapman /* Receive message. This is the recvmsg for the PPPoL2TP socket.
206fd558d18SJames Chapman  */
2071b784140SYing Xue static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
2081b784140SYing Xue 			    size_t len, int flags)
209fd558d18SJames Chapman {
210fd558d18SJames Chapman 	int err;
211fd558d18SJames Chapman 	struct sk_buff *skb;
212fd558d18SJames Chapman 	struct sock *sk = sock->sk;
213fd558d18SJames Chapman 
214fd558d18SJames Chapman 	err = -EIO;
215fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_BOUND)
216fd558d18SJames Chapman 		goto end;
217fd558d18SJames Chapman 
218fd558d18SJames Chapman 	err = 0;
219fd558d18SJames Chapman 	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
220fd558d18SJames Chapman 				flags & MSG_DONTWAIT, &err);
221fd558d18SJames Chapman 	if (!skb)
222fd558d18SJames Chapman 		goto end;
223fd558d18SJames Chapman 
224fd558d18SJames Chapman 	if (len > skb->len)
225fd558d18SJames Chapman 		len = skb->len;
226fd558d18SJames Chapman 	else if (len < skb->len)
227fd558d18SJames Chapman 		msg->msg_flags |= MSG_TRUNC;
228fd558d18SJames Chapman 
22951f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, len);
230fd558d18SJames Chapman 	if (likely(err == 0))
231fd558d18SJames Chapman 		err = len;
232fd558d18SJames Chapman 
233fd558d18SJames Chapman 	kfree_skb(skb);
234fd558d18SJames Chapman end:
235fd558d18SJames Chapman 	return err;
236fd558d18SJames Chapman }
237fd558d18SJames Chapman 
238fd558d18SJames Chapman static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
239fd558d18SJames Chapman {
240fd558d18SJames Chapman 	struct pppol2tp_session *ps = l2tp_session_priv(session);
241fd558d18SJames Chapman 	struct sock *sk = NULL;
242fd558d18SJames Chapman 
243fd558d18SJames Chapman 	/* If the socket is bound, send it in to PPP's input queue. Otherwise
244fd558d18SJames Chapman 	 * queue it on the session socket.
245fd558d18SJames Chapman 	 */
246ee40fb2eSGuillaume Nault 	rcu_read_lock();
247ee40fb2eSGuillaume Nault 	sk = rcu_dereference(ps->sk);
248fd558d18SJames Chapman 	if (sk == NULL)
249fd558d18SJames Chapman 		goto no_sock;
250fd558d18SJames Chapman 
251fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_BOUND) {
252fd558d18SJames Chapman 		struct pppox_sock *po;
25398f40b3eSGuillaume Nault 
254fba40c63SAsbjørn Sloth Tønnesen 		l2tp_dbg(session, L2TP_MSG_DATA,
255fd558d18SJames Chapman 			 "%s: recv %d byte data frame, passing to ppp\n",
256fd558d18SJames Chapman 			 session->name, data_len);
257fd558d18SJames Chapman 
258fd558d18SJames Chapman 		po = pppox_sk(sk);
259fd558d18SJames Chapman 		ppp_input(&po->chan, skb);
260fd558d18SJames Chapman 	} else {
261fba40c63SAsbjørn Sloth Tønnesen 		l2tp_dbg(session, L2TP_MSG_DATA,
2629e9cb622SGuillaume Nault 			 "%s: recv %d byte data frame, passing to L2TP socket\n",
2639e9cb622SGuillaume Nault 			 session->name, data_len);
264fd558d18SJames Chapman 
2659e9cb622SGuillaume Nault 		if (sock_queue_rcv_skb(sk, skb) < 0) {
2667b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_errors);
267fd558d18SJames Chapman 			kfree_skb(skb);
268fd558d18SJames Chapman 		}
2699e9cb622SGuillaume Nault 	}
270ee40fb2eSGuillaume Nault 	rcu_read_unlock();
271fd558d18SJames Chapman 
272fd558d18SJames Chapman 	return;
273fd558d18SJames Chapman 
274fd558d18SJames Chapman no_sock:
275ee40fb2eSGuillaume Nault 	rcu_read_unlock();
276fba40c63SAsbjørn Sloth Tønnesen 	l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
277fd558d18SJames Chapman 	kfree_skb(skb);
278fd558d18SJames Chapman }
279fd558d18SJames Chapman 
280fd558d18SJames Chapman /************************************************************************
281fd558d18SJames Chapman  * Transmit handling
282fd558d18SJames Chapman  ***********************************************************************/
283fd558d18SJames Chapman 
284fd558d18SJames Chapman /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket.  We come here
285fd558d18SJames Chapman  * when a user application does a sendmsg() on the session socket. L2TP and
286fd558d18SJames Chapman  * PPP headers must be inserted into the user's data.
287fd558d18SJames Chapman  */
2881b784140SYing Xue static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
289fd558d18SJames Chapman 			    size_t total_len)
290fd558d18SJames Chapman {
291fd558d18SJames Chapman 	struct sock *sk = sock->sk;
292fd558d18SJames Chapman 	struct sk_buff *skb;
293fd558d18SJames Chapman 	int error;
294fd558d18SJames Chapman 	struct l2tp_session *session;
295fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
2960d76751fSJames Chapman 	int uhlen;
297fd558d18SJames Chapman 
298fd558d18SJames Chapman 	error = -ENOTCONN;
299fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
300fd558d18SJames Chapman 		goto error;
301fd558d18SJames Chapman 
302fd558d18SJames Chapman 	/* Get session and tunnel contexts */
303fd558d18SJames Chapman 	error = -EBADF;
304fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
305fd558d18SJames Chapman 	if (session == NULL)
306fd558d18SJames Chapman 		goto error;
307fd558d18SJames Chapman 
3087198c77aSGuillaume Nault 	tunnel = session->tunnel;
309fd558d18SJames Chapman 
3100d76751fSJames Chapman 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
3110d76751fSJames Chapman 
312fd558d18SJames Chapman 	/* Allocate a socket buffer */
313fd558d18SJames Chapman 	error = -ENOMEM;
314fd558d18SJames Chapman 	skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
3150d76751fSJames Chapman 			   uhlen + session->hdr_len +
31654c151d9SGao Feng 			   2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
317fd558d18SJames Chapman 			   0, GFP_KERNEL);
318fd558d18SJames Chapman 	if (!skb)
3197198c77aSGuillaume Nault 		goto error_put_sess;
320fd558d18SJames Chapman 
321fd558d18SJames Chapman 	/* Reserve space for headers. */
322fd558d18SJames Chapman 	skb_reserve(skb, NET_SKB_PAD);
323fd558d18SJames Chapman 	skb_reset_network_header(skb);
324fd558d18SJames Chapman 	skb_reserve(skb, sizeof(struct iphdr));
325fd558d18SJames Chapman 	skb_reset_transport_header(skb);
3260d76751fSJames Chapman 	skb_reserve(skb, uhlen);
327fd558d18SJames Chapman 
328fd558d18SJames Chapman 	/* Add PPP header */
32954c151d9SGao Feng 	skb->data[0] = PPP_ALLSTATIONS;
33054c151d9SGao Feng 	skb->data[1] = PPP_UI;
331fd558d18SJames Chapman 	skb_put(skb, 2);
332fd558d18SJames Chapman 
333fd558d18SJames Chapman 	/* Copy user data into skb */
3346ce8e9ceSAl Viro 	error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
335fd558d18SJames Chapman 	if (error < 0) {
336fd558d18SJames Chapman 		kfree_skb(skb);
3377198c77aSGuillaume Nault 		goto error_put_sess;
338fd558d18SJames Chapman 	}
339fd558d18SJames Chapman 
340455cc32bSEric Dumazet 	local_bh_disable();
341fd558d18SJames Chapman 	l2tp_xmit_skb(session, skb, session->hdr_len);
342455cc32bSEric Dumazet 	local_bh_enable();
343fd558d18SJames Chapman 
3448b82547eSGuillaume Nault 	sock_put(sk);
345fd558d18SJames Chapman 
346a6f79d0fSGuillaume Nault 	return total_len;
347fd558d18SJames Chapman 
348fd558d18SJames Chapman error_put_sess:
349fd558d18SJames Chapman 	sock_put(sk);
350fd558d18SJames Chapman error:
351fd558d18SJames Chapman 	return error;
352fd558d18SJames Chapman }
353fd558d18SJames Chapman 
354fd558d18SJames Chapman /* Transmit function called by generic PPP driver.  Sends PPP frame
355fd558d18SJames Chapman  * over PPPoL2TP socket.
356fd558d18SJames Chapman  *
357fd558d18SJames Chapman  * This is almost the same as pppol2tp_sendmsg(), but rather than
358fd558d18SJames Chapman  * being called with a msghdr from userspace, it is called with a skb
359fd558d18SJames Chapman  * from the kernel.
360fd558d18SJames Chapman  *
361fd558d18SJames Chapman  * The supplied skb from ppp doesn't have enough headroom for the
362fd558d18SJames Chapman  * insertion of L2TP, UDP and IP headers so we need to allocate more
363fd558d18SJames Chapman  * headroom in the skb. This will create a cloned skb. But we must be
364fd558d18SJames Chapman  * careful in the error case because the caller will expect to free
365fd558d18SJames Chapman  * the skb it supplied, not our cloned skb. So we take care to always
366fd558d18SJames Chapman  * leave the original skb unfreed if we return an error.
367fd558d18SJames Chapman  */
368fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
369fd558d18SJames Chapman {
370fd558d18SJames Chapman 	struct sock *sk = (struct sock *) chan->private;
371fd558d18SJames Chapman 	struct l2tp_session *session;
372fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
37309df57caSEric Dumazet 	int uhlen, headroom;
374fd558d18SJames Chapman 
375fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
376fd558d18SJames Chapman 		goto abort;
377fd558d18SJames Chapman 
378fd558d18SJames Chapman 	/* Get session and tunnel contexts from the socket */
379fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
380fd558d18SJames Chapman 	if (session == NULL)
381fd558d18SJames Chapman 		goto abort;
382fd558d18SJames Chapman 
3837198c77aSGuillaume Nault 	tunnel = session->tunnel;
384fd558d18SJames Chapman 
38509df57caSEric Dumazet 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
38609df57caSEric Dumazet 	headroom = NET_SKB_PAD +
38709df57caSEric Dumazet 		   sizeof(struct iphdr) + /* IP header */
38809df57caSEric Dumazet 		   uhlen +		/* UDP header (if L2TP_ENCAPTYPE_UDP) */
38909df57caSEric Dumazet 		   session->hdr_len +	/* L2TP header */
39054c151d9SGao Feng 		   2;			/* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
39109df57caSEric Dumazet 	if (skb_cow_head(skb, headroom))
3927198c77aSGuillaume Nault 		goto abort_put_sess;
393fd558d18SJames Chapman 
394fd558d18SJames Chapman 	/* Setup PPP header */
39554c151d9SGao Feng 	__skb_push(skb, 2);
39654c151d9SGao Feng 	skb->data[0] = PPP_ALLSTATIONS;
39754c151d9SGao Feng 	skb->data[1] = PPP_UI;
398fd558d18SJames Chapman 
399455cc32bSEric Dumazet 	local_bh_disable();
400e0d4435fSJames Chapman 	l2tp_xmit_skb(session, skb, session->hdr_len);
401455cc32bSEric Dumazet 	local_bh_enable();
402fd558d18SJames Chapman 
403fd558d18SJames Chapman 	sock_put(sk);
4047198c77aSGuillaume Nault 
405fd558d18SJames Chapman 	return 1;
406fd558d18SJames Chapman 
407fd558d18SJames Chapman abort_put_sess:
408fd558d18SJames Chapman 	sock_put(sk);
409fd558d18SJames Chapman abort:
410fd558d18SJames Chapman 	/* Free the original skb */
411fd558d18SJames Chapman 	kfree_skb(skb);
412fd558d18SJames Chapman 	return 1;
413fd558d18SJames Chapman }
414fd558d18SJames Chapman 
415fd558d18SJames Chapman /*****************************************************************************
416fd558d18SJames Chapman  * Session (and tunnel control) socket create/destroy.
417fd558d18SJames Chapman  *****************************************************************************/
418fd558d18SJames Chapman 
419d02ba2a6SJames Chapman static void pppol2tp_put_sk(struct rcu_head *head)
420d02ba2a6SJames Chapman {
421d02ba2a6SJames Chapman 	struct pppol2tp_session *ps;
422d02ba2a6SJames Chapman 
423d02ba2a6SJames Chapman 	ps = container_of(head, typeof(*ps), rcu);
424d02ba2a6SJames Chapman 	sock_put(ps->__sk);
425d02ba2a6SJames Chapman }
426d02ba2a6SJames Chapman 
427fd558d18SJames Chapman /* Called by l2tp_core when a session socket is being closed.
428fd558d18SJames Chapman  */
429fd558d18SJames Chapman static void pppol2tp_session_close(struct l2tp_session *session)
430fd558d18SJames Chapman {
431d02ba2a6SJames Chapman 	struct pppol2tp_session *ps;
432fd558d18SJames Chapman 
433d02ba2a6SJames Chapman 	ps = l2tp_session_priv(session);
434d02ba2a6SJames Chapman 	mutex_lock(&ps->sk_lock);
435d02ba2a6SJames Chapman 	ps->__sk = rcu_dereference_protected(ps->sk,
436d02ba2a6SJames Chapman 					     lockdep_is_held(&ps->sk_lock));
437d02ba2a6SJames Chapman 	RCU_INIT_POINTER(ps->sk, NULL);
438d02ba2a6SJames Chapman 	if (ps->__sk)
439d02ba2a6SJames Chapman 		call_rcu(&ps->rcu, pppol2tp_put_sk);
440d02ba2a6SJames Chapman 	mutex_unlock(&ps->sk_lock);
441fd558d18SJames Chapman }
442fd558d18SJames Chapman 
443fd558d18SJames Chapman /* Really kill the session socket. (Called from sock_put() if
444fd558d18SJames Chapman  * refcnt == 0.)
445fd558d18SJames Chapman  */
446fd558d18SJames Chapman static void pppol2tp_session_destruct(struct sock *sk)
447fd558d18SJames Chapman {
448f6e16b29STom Parkin 	struct l2tp_session *session = sk->sk_user_data;
449e91793bbSGuillaume Nault 
450e91793bbSGuillaume Nault 	skb_queue_purge(&sk->sk_receive_queue);
451e91793bbSGuillaume Nault 	skb_queue_purge(&sk->sk_write_queue);
452e91793bbSGuillaume Nault 
453f6e16b29STom Parkin 	if (session) {
454fd558d18SJames Chapman 		sk->sk_user_data = NULL;
455fd558d18SJames Chapman 		BUG_ON(session->magic != L2TP_SESSION_MAGIC);
456fd558d18SJames Chapman 		l2tp_session_dec_refcount(session);
457fd558d18SJames Chapman 	}
458fd558d18SJames Chapman }
459fd558d18SJames Chapman 
460fd558d18SJames Chapman /* Called when the PPPoX socket (session) is closed.
461fd558d18SJames Chapman  */
462fd558d18SJames Chapman static int pppol2tp_release(struct socket *sock)
463fd558d18SJames Chapman {
464fd558d18SJames Chapman 	struct sock *sk = sock->sk;
465fd558d18SJames Chapman 	struct l2tp_session *session;
466fd558d18SJames Chapman 	int error;
467fd558d18SJames Chapman 
468fd558d18SJames Chapman 	if (!sk)
469fd558d18SJames Chapman 		return 0;
470fd558d18SJames Chapman 
471fd558d18SJames Chapman 	error = -EBADF;
472fd558d18SJames Chapman 	lock_sock(sk);
473fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) != 0)
474fd558d18SJames Chapman 		goto error;
475fd558d18SJames Chapman 
476fd558d18SJames Chapman 	pppox_unbind_sock(sk);
477fd558d18SJames Chapman 
478fd558d18SJames Chapman 	/* Signal the death of the socket. */
479fd558d18SJames Chapman 	sk->sk_state = PPPOX_DEAD;
480fd558d18SJames Chapman 	sock_orphan(sk);
481fd558d18SJames Chapman 	sock->sk = NULL;
482fd558d18SJames Chapman 
483d02ba2a6SJames Chapman 	/* If the socket is associated with a session,
484d02ba2a6SJames Chapman 	 * l2tp_session_delete will call pppol2tp_session_close which
485d02ba2a6SJames Chapman 	 * will drop the session's ref on the socket.
486ee40fb2eSGuillaume Nault 	 */
487d02ba2a6SJames Chapman 	session = pppol2tp_sock_to_session(sk);
488d02ba2a6SJames Chapman 	if (session) {
489d02ba2a6SJames Chapman 		l2tp_session_delete(session);
490d02ba2a6SJames Chapman 		/* drop the ref obtained by pppol2tp_sock_to_session */
491d02ba2a6SJames Chapman 		sock_put(sk);
492cf2f5c88STom Parkin 	}
493d02ba2a6SJames Chapman 
494fd558d18SJames Chapman 	release_sock(sk);
495fd558d18SJames Chapman 
496fd558d18SJames Chapman 	/* This will delete the session context via
497fd558d18SJames Chapman 	 * pppol2tp_session_destruct() if the socket's refcnt drops to
498fd558d18SJames Chapman 	 * zero.
499fd558d18SJames Chapman 	 */
500fd558d18SJames Chapman 	sock_put(sk);
501fd558d18SJames Chapman 
502fd558d18SJames Chapman 	return 0;
503fd558d18SJames Chapman 
504fd558d18SJames Chapman error:
505fd558d18SJames Chapman 	release_sock(sk);
506fd558d18SJames Chapman 	return error;
507fd558d18SJames Chapman }
508fd558d18SJames Chapman 
509fd558d18SJames Chapman static struct proto pppol2tp_sk_proto = {
510fd558d18SJames Chapman 	.name	  = "PPPOL2TP",
511fd558d18SJames Chapman 	.owner	  = THIS_MODULE,
512fd558d18SJames Chapman 	.obj_size = sizeof(struct pppox_sock),
513fd558d18SJames Chapman };
514fd558d18SJames Chapman 
515fd558d18SJames Chapman static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
516fd558d18SJames Chapman {
517fd558d18SJames Chapman 	int rc;
518fd558d18SJames Chapman 
519fd558d18SJames Chapman 	rc = l2tp_udp_encap_recv(sk, skb);
520fd558d18SJames Chapman 	if (rc)
521fd558d18SJames Chapman 		kfree_skb(skb);
522fd558d18SJames Chapman 
523fd558d18SJames Chapman 	return NET_RX_SUCCESS;
524fd558d18SJames Chapman }
525fd558d18SJames Chapman 
526fd558d18SJames Chapman /* socket() handler. Initialize a new struct sock.
527fd558d18SJames Chapman  */
52811aa9c28SEric W. Biederman static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
529fd558d18SJames Chapman {
530fd558d18SJames Chapman 	int error = -ENOMEM;
531fd558d18SJames Chapman 	struct sock *sk;
532fd558d18SJames Chapman 
53311aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
534fd558d18SJames Chapman 	if (!sk)
535fd558d18SJames Chapman 		goto out;
536fd558d18SJames Chapman 
537fd558d18SJames Chapman 	sock_init_data(sock, sk);
538fd558d18SJames Chapman 
539fd558d18SJames Chapman 	sock->state  = SS_UNCONNECTED;
540fd558d18SJames Chapman 	sock->ops    = &pppol2tp_ops;
541fd558d18SJames Chapman 
542fd558d18SJames Chapman 	sk->sk_backlog_rcv = pppol2tp_backlog_recv;
543fd558d18SJames Chapman 	sk->sk_protocol	   = PX_PROTO_OL2TP;
544fd558d18SJames Chapman 	sk->sk_family	   = PF_PPPOX;
545fd558d18SJames Chapman 	sk->sk_state	   = PPPOX_NONE;
546fd558d18SJames Chapman 	sk->sk_type	   = SOCK_STREAM;
547fd558d18SJames Chapman 	sk->sk_destruct	   = pppol2tp_session_destruct;
548fd558d18SJames Chapman 
549fd558d18SJames Chapman 	error = 0;
550fd558d18SJames Chapman 
551fd558d18SJames Chapman out:
552fd558d18SJames Chapman 	return error;
553fd558d18SJames Chapman }
554fd558d18SJames Chapman 
5559dd79945SJavier Martinez Canillas #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
5560ad66140SJames Chapman static void pppol2tp_show(struct seq_file *m, void *arg)
5570ad66140SJames Chapman {
5580ad66140SJames Chapman 	struct l2tp_session *session = arg;
559ee40fb2eSGuillaume Nault 	struct sock *sk;
5600ad66140SJames Chapman 
561ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
562ee40fb2eSGuillaume Nault 	if (sk) {
563ee40fb2eSGuillaume Nault 		struct pppox_sock *po = pppox_sk(sk);
564ee40fb2eSGuillaume Nault 
5650ad66140SJames Chapman 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
566ee40fb2eSGuillaume Nault 		sock_put(sk);
5670ad66140SJames Chapman 	}
5680ad66140SJames Chapman }
5690ad66140SJames Chapman #endif
5700ad66140SJames Chapman 
571f98be6c6SGuillaume Nault static void pppol2tp_session_init(struct l2tp_session *session)
572f98be6c6SGuillaume Nault {
573f98be6c6SGuillaume Nault 	struct pppol2tp_session *ps;
574f98be6c6SGuillaume Nault 	struct dst_entry *dst;
575f98be6c6SGuillaume Nault 
576f98be6c6SGuillaume Nault 	session->recv_skb = pppol2tp_recv;
577f98be6c6SGuillaume Nault 	session->session_close = pppol2tp_session_close;
578f98be6c6SGuillaume Nault #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
579f98be6c6SGuillaume Nault 	session->show = pppol2tp_show;
580f98be6c6SGuillaume Nault #endif
581f98be6c6SGuillaume Nault 
582f98be6c6SGuillaume Nault 	ps = l2tp_session_priv(session);
583f98be6c6SGuillaume Nault 	mutex_init(&ps->sk_lock);
584f98be6c6SGuillaume Nault 	ps->owner = current->pid;
585f98be6c6SGuillaume Nault 
586f98be6c6SGuillaume Nault 	/* If PMTU discovery was enabled, use the MTU that was discovered */
587f98be6c6SGuillaume Nault 	dst = sk_dst_get(session->tunnel->sock);
588f98be6c6SGuillaume Nault 	if (dst) {
589f98be6c6SGuillaume Nault 		u32 pmtu = dst_mtu(dst);
590f98be6c6SGuillaume Nault 
591f98be6c6SGuillaume Nault 		if (pmtu) {
592f98be6c6SGuillaume Nault 			session->mtu = pmtu - PPPOL2TP_HEADER_OVERHEAD;
593f98be6c6SGuillaume Nault 			session->mru = pmtu - PPPOL2TP_HEADER_OVERHEAD;
594f98be6c6SGuillaume Nault 		}
595f98be6c6SGuillaume Nault 		dst_release(dst);
596f98be6c6SGuillaume Nault 	}
597f98be6c6SGuillaume Nault }
598f98be6c6SGuillaume Nault 
599fd558d18SJames Chapman /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
600fd558d18SJames Chapman  */
601fd558d18SJames Chapman static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
602fd558d18SJames Chapman 			    int sockaddr_len, int flags)
603fd558d18SJames Chapman {
604fd558d18SJames Chapman 	struct sock *sk = sock->sk;
605fd558d18SJames Chapman 	struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
606fd558d18SJames Chapman 	struct pppox_sock *po = pppox_sk(sk);
607fd558d18SJames Chapman 	struct l2tp_session *session = NULL;
608fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
609fd558d18SJames Chapman 	struct pppol2tp_session *ps;
610fd558d18SJames Chapman 	struct l2tp_session_cfg cfg = { 0, };
611fd558d18SJames Chapman 	int error = 0;
612e0d4435fSJames Chapman 	u32 tunnel_id, peer_tunnel_id;
613e0d4435fSJames Chapman 	u32 session_id, peer_session_id;
614dbdbc73bSGuillaume Nault 	bool drop_refcnt = false;
615f9e56bafSGuillaume Nault 	bool drop_tunnel = false;
616e0d4435fSJames Chapman 	int ver = 2;
617e0d4435fSJames Chapman 	int fd;
618fd558d18SJames Chapman 
619fd558d18SJames Chapman 	lock_sock(sk);
620fd558d18SJames Chapman 
621fd558d18SJames Chapman 	error = -EINVAL;
622fd558d18SJames Chapman 	if (sp->sa_protocol != PX_PROTO_OL2TP)
623fd558d18SJames Chapman 		goto end;
624fd558d18SJames Chapman 
625fd558d18SJames Chapman 	/* Check for already bound sockets */
626fd558d18SJames Chapman 	error = -EBUSY;
627fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_CONNECTED)
628fd558d18SJames Chapman 		goto end;
629fd558d18SJames Chapman 
630fd558d18SJames Chapman 	/* We don't supporting rebinding anyway */
631fd558d18SJames Chapman 	error = -EALREADY;
632fd558d18SJames Chapman 	if (sk->sk_user_data)
633fd558d18SJames Chapman 		goto end; /* socket is already attached */
634fd558d18SJames Chapman 
635b79585f5SJames Chapman 	/* Get params from socket address. Handle L2TPv2 and L2TPv3.
636b79585f5SJames Chapman 	 * This is nasty because there are different sockaddr_pppol2tp
637b79585f5SJames Chapman 	 * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use
638b79585f5SJames Chapman 	 * the sockaddr size to determine which structure the caller
639b79585f5SJames Chapman 	 * is using.
640b79585f5SJames Chapman 	 */
641b79585f5SJames Chapman 	peer_tunnel_id = 0;
642e0d4435fSJames Chapman 	if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
643e0d4435fSJames Chapman 		fd = sp->pppol2tp.fd;
644e0d4435fSJames Chapman 		tunnel_id = sp->pppol2tp.s_tunnel;
645e0d4435fSJames Chapman 		peer_tunnel_id = sp->pppol2tp.d_tunnel;
646e0d4435fSJames Chapman 		session_id = sp->pppol2tp.s_session;
647e0d4435fSJames Chapman 		peer_session_id = sp->pppol2tp.d_session;
648e0d4435fSJames Chapman 	} else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
649b79585f5SJames Chapman 		struct sockaddr_pppol2tpv3 *sp3 =
650b79585f5SJames Chapman 			(struct sockaddr_pppol2tpv3 *) sp;
651e0d4435fSJames Chapman 		ver = 3;
652e0d4435fSJames Chapman 		fd = sp3->pppol2tp.fd;
653e0d4435fSJames Chapman 		tunnel_id = sp3->pppol2tp.s_tunnel;
654e0d4435fSJames Chapman 		peer_tunnel_id = sp3->pppol2tp.d_tunnel;
655e0d4435fSJames Chapman 		session_id = sp3->pppol2tp.s_session;
656e0d4435fSJames Chapman 		peer_session_id = sp3->pppol2tp.d_session;
657b79585f5SJames Chapman 	} else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) {
658b79585f5SJames Chapman 		struct sockaddr_pppol2tpin6 *sp6 =
659b79585f5SJames Chapman 			(struct sockaddr_pppol2tpin6 *) sp;
660b79585f5SJames Chapman 		fd = sp6->pppol2tp.fd;
661b79585f5SJames Chapman 		tunnel_id = sp6->pppol2tp.s_tunnel;
662b79585f5SJames Chapman 		peer_tunnel_id = sp6->pppol2tp.d_tunnel;
663b79585f5SJames Chapman 		session_id = sp6->pppol2tp.s_session;
664b79585f5SJames Chapman 		peer_session_id = sp6->pppol2tp.d_session;
665b79585f5SJames Chapman 	} else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) {
666b79585f5SJames Chapman 		struct sockaddr_pppol2tpv3in6 *sp6 =
667b79585f5SJames Chapman 			(struct sockaddr_pppol2tpv3in6 *) sp;
668b79585f5SJames Chapman 		ver = 3;
669b79585f5SJames Chapman 		fd = sp6->pppol2tp.fd;
670b79585f5SJames Chapman 		tunnel_id = sp6->pppol2tp.s_tunnel;
671b79585f5SJames Chapman 		peer_tunnel_id = sp6->pppol2tp.d_tunnel;
672b79585f5SJames Chapman 		session_id = sp6->pppol2tp.s_session;
673b79585f5SJames Chapman 		peer_session_id = sp6->pppol2tp.d_session;
674e0d4435fSJames Chapman 	} else {
675fd558d18SJames Chapman 		error = -EINVAL;
676e0d4435fSJames Chapman 		goto end; /* bad socket address */
677e0d4435fSJames Chapman 	}
678e0d4435fSJames Chapman 
679e0d4435fSJames Chapman 	/* Don't bind if tunnel_id is 0 */
680e0d4435fSJames Chapman 	error = -EINVAL;
681e0d4435fSJames Chapman 	if (tunnel_id == 0)
682fd558d18SJames Chapman 		goto end;
683fd558d18SJames Chapman 
684f9e56bafSGuillaume Nault 	tunnel = l2tp_tunnel_get(sock_net(sk), tunnel_id);
685f9e56bafSGuillaume Nault 	if (tunnel)
686f9e56bafSGuillaume Nault 		drop_tunnel = true;
687309795f4SJames Chapman 
688e0d4435fSJames Chapman 	/* Special case: create tunnel context if session_id and
689e0d4435fSJames Chapman 	 * peer_session_id is 0. Otherwise look up tunnel using supplied
690fd558d18SJames Chapman 	 * tunnel id.
691fd558d18SJames Chapman 	 */
692e0d4435fSJames Chapman 	if ((session_id == 0) && (peer_session_id == 0)) {
693309795f4SJames Chapman 		if (tunnel == NULL) {
694309795f4SJames Chapman 			struct l2tp_tunnel_cfg tcfg = {
695309795f4SJames Chapman 				.encap = L2TP_ENCAPTYPE_UDP,
696309795f4SJames Chapman 				.debug = 0,
697309795f4SJames Chapman 			};
698309795f4SJames Chapman 			error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
699fd558d18SJames Chapman 			if (error < 0)
700fd558d18SJames Chapman 				goto end;
7016b9f3423SGuillaume Nault 
7026b9f3423SGuillaume Nault 			l2tp_tunnel_inc_refcount(tunnel);
7036b9f3423SGuillaume Nault 			error = l2tp_tunnel_register(tunnel, sock_net(sk),
7046b9f3423SGuillaume Nault 						     &tcfg);
7056b9f3423SGuillaume Nault 			if (error < 0) {
7066b9f3423SGuillaume Nault 				kfree(tunnel);
7076b9f3423SGuillaume Nault 				goto end;
7086b9f3423SGuillaume Nault 			}
7096b9f3423SGuillaume Nault 			drop_tunnel = true;
710309795f4SJames Chapman 		}
711fd558d18SJames Chapman 	} else {
712fd558d18SJames Chapman 		/* Error if we can't find the tunnel */
713fd558d18SJames Chapman 		error = -ENOENT;
714fd558d18SJames Chapman 		if (tunnel == NULL)
715fd558d18SJames Chapman 			goto end;
716fd558d18SJames Chapman 
717fd558d18SJames Chapman 		/* Error if socket is not prepped */
718fd558d18SJames Chapman 		if (tunnel->sock == NULL)
719fd558d18SJames Chapman 			goto end;
720fd558d18SJames Chapman 	}
721fd558d18SJames Chapman 
722fd558d18SJames Chapman 	if (tunnel->recv_payload_hook == NULL)
723fd558d18SJames Chapman 		tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
724fd558d18SJames Chapman 
725b79585f5SJames Chapman 	if (tunnel->peer_tunnel_id == 0)
726b79585f5SJames Chapman 		tunnel->peer_tunnel_id = peer_tunnel_id;
727fd558d18SJames Chapman 
728a4346210SGuillaume Nault 	session = l2tp_session_get(sock_net(sk), tunnel, session_id);
729dbdbc73bSGuillaume Nault 	if (session) {
730dbdbc73bSGuillaume Nault 		drop_refcnt = true;
731dbdbc73bSGuillaume Nault 		ps = l2tp_session_priv(session);
732fd558d18SJames Chapman 
733dbdbc73bSGuillaume Nault 		/* Using a pre-existing session is fine as long as it hasn't
734dbdbc73bSGuillaume Nault 		 * been connected yet.
735dbdbc73bSGuillaume Nault 		 */
736ee40fb2eSGuillaume Nault 		mutex_lock(&ps->sk_lock);
737ee40fb2eSGuillaume Nault 		if (rcu_dereference_protected(ps->sk,
738ee40fb2eSGuillaume Nault 					      lockdep_is_held(&ps->sk_lock))) {
739ee40fb2eSGuillaume Nault 			mutex_unlock(&ps->sk_lock);
740dbdbc73bSGuillaume Nault 			error = -EEXIST;
741dbdbc73bSGuillaume Nault 			goto end;
742dbdbc73bSGuillaume Nault 		}
743309795f4SJames Chapman 	} else {
744dbdbc73bSGuillaume Nault 		/* Default MTU must allow space for UDP/L2TP/PPP headers */
745dbdbc73bSGuillaume Nault 		cfg.mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
746dbdbc73bSGuillaume Nault 		cfg.mru = cfg.mtu;
747fd558d18SJames Chapman 
748dbdbc73bSGuillaume Nault 		session = l2tp_session_create(sizeof(struct pppol2tp_session),
749dbdbc73bSGuillaume Nault 					      tunnel, session_id,
750dbdbc73bSGuillaume Nault 					      peer_session_id, &cfg);
751dbdbc73bSGuillaume Nault 		if (IS_ERR(session)) {
752dbdbc73bSGuillaume Nault 			error = PTR_ERR(session);
753309795f4SJames Chapman 			goto end;
754309795f4SJames Chapman 		}
7553953ae7bSGuillaume Nault 
756f98be6c6SGuillaume Nault 		pppol2tp_session_init(session);
757ee40fb2eSGuillaume Nault 		ps = l2tp_session_priv(session);
7583953ae7bSGuillaume Nault 		l2tp_session_inc_refcount(session);
759ee40fb2eSGuillaume Nault 
760ee40fb2eSGuillaume Nault 		mutex_lock(&ps->sk_lock);
7613953ae7bSGuillaume Nault 		error = l2tp_session_register(session, tunnel);
7623953ae7bSGuillaume Nault 		if (error < 0) {
763ee40fb2eSGuillaume Nault 			mutex_unlock(&ps->sk_lock);
7643953ae7bSGuillaume Nault 			kfree(session);
7653953ae7bSGuillaume Nault 			goto end;
7663953ae7bSGuillaume Nault 		}
7673953ae7bSGuillaume Nault 		drop_refcnt = true;
768dbdbc73bSGuillaume Nault 	}
769309795f4SJames Chapman 
770fd558d18SJames Chapman 	/* Special case: if source & dest session_id == 0x0000, this
771fd558d18SJames Chapman 	 * socket is being created to manage the tunnel. Just set up
772fd558d18SJames Chapman 	 * the internal context for use by ioctl() and sockopt()
773fd558d18SJames Chapman 	 * handlers.
774fd558d18SJames Chapman 	 */
775fd558d18SJames Chapman 	if ((session->session_id == 0) &&
776fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
777fd558d18SJames Chapman 		error = 0;
778fd558d18SJames Chapman 		goto out_no_ppp;
779fd558d18SJames Chapman 	}
780fd558d18SJames Chapman 
781fd558d18SJames Chapman 	/* The only header we need to worry about is the L2TP
782fd558d18SJames Chapman 	 * header. This size is different depending on whether
783fd558d18SJames Chapman 	 * sequence numbers are enabled for the data channel.
784fd558d18SJames Chapman 	 */
785fd558d18SJames Chapman 	po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
786fd558d18SJames Chapman 
787fd558d18SJames Chapman 	po->chan.private = sk;
788fd558d18SJames Chapman 	po->chan.ops	 = &pppol2tp_chan_ops;
789fd558d18SJames Chapman 	po->chan.mtu	 = session->mtu;
790fd558d18SJames Chapman 
791fd558d18SJames Chapman 	error = ppp_register_net_channel(sock_net(sk), &po->chan);
792ee40fb2eSGuillaume Nault 	if (error) {
793ee40fb2eSGuillaume Nault 		mutex_unlock(&ps->sk_lock);
794fd558d18SJames Chapman 		goto end;
795ee40fb2eSGuillaume Nault 	}
796fd558d18SJames Chapman 
797fd558d18SJames Chapman out_no_ppp:
798fd558d18SJames Chapman 	/* This is how we get the session context from the socket. */
799d02ba2a6SJames Chapman 	sock_hold(sk);
800fd558d18SJames Chapman 	sk->sk_user_data = session;
801ee40fb2eSGuillaume Nault 	rcu_assign_pointer(ps->sk, sk);
802ee40fb2eSGuillaume Nault 	mutex_unlock(&ps->sk_lock);
803ee40fb2eSGuillaume Nault 
804f98be6c6SGuillaume Nault 	/* Keep the reference we've grabbed on the session: sk doesn't expect
805f98be6c6SGuillaume Nault 	 * the session to disappear. pppol2tp_session_destruct() is responsible
806f98be6c6SGuillaume Nault 	 * for dropping it.
807f98be6c6SGuillaume Nault 	 */
808f98be6c6SGuillaume Nault 	drop_refcnt = false;
809f98be6c6SGuillaume Nault 
810fd558d18SJames Chapman 	sk->sk_state = PPPOX_CONNECTED;
811fba40c63SAsbjørn Sloth Tønnesen 	l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
812a4ca44faSJoe Perches 		  session->name);
813fd558d18SJames Chapman 
814fd558d18SJames Chapman end:
815dbdbc73bSGuillaume Nault 	if (drop_refcnt)
816dbdbc73bSGuillaume Nault 		l2tp_session_dec_refcount(session);
817f9e56bafSGuillaume Nault 	if (drop_tunnel)
818f9e56bafSGuillaume Nault 		l2tp_tunnel_dec_refcount(tunnel);
819fd558d18SJames Chapman 	release_sock(sk);
820fd558d18SJames Chapman 
821fd558d18SJames Chapman 	return error;
822fd558d18SJames Chapman }
823fd558d18SJames Chapman 
824309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
825309795f4SJames Chapman 
826f026bc29SGuillaume Nault /* Called when creating sessions via the netlink interface. */
827f026bc29SGuillaume Nault static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
828f026bc29SGuillaume Nault 				   u32 session_id, u32 peer_session_id,
829f026bc29SGuillaume Nault 				   struct l2tp_session_cfg *cfg)
830309795f4SJames Chapman {
831309795f4SJames Chapman 	int error;
832309795f4SJames Chapman 	struct l2tp_session *session;
833309795f4SJames Chapman 
834309795f4SJames Chapman 	/* Error if tunnel socket is not prepped */
835f026bc29SGuillaume Nault 	if (!tunnel->sock) {
836f026bc29SGuillaume Nault 		error = -ENOENT;
8373953ae7bSGuillaume Nault 		goto err;
838f026bc29SGuillaume Nault 	}
839309795f4SJames Chapman 
840309795f4SJames Chapman 	/* Default MTU values. */
841309795f4SJames Chapman 	if (cfg->mtu == 0)
842309795f4SJames Chapman 		cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
843309795f4SJames Chapman 	if (cfg->mru == 0)
844309795f4SJames Chapman 		cfg->mru = cfg->mtu;
845309795f4SJames Chapman 
846309795f4SJames Chapman 	/* Allocate and initialize a new session context. */
847309795f4SJames Chapman 	session = l2tp_session_create(sizeof(struct pppol2tp_session),
848309795f4SJames Chapman 				      tunnel, session_id,
849309795f4SJames Chapman 				      peer_session_id, cfg);
850dbdbc73bSGuillaume Nault 	if (IS_ERR(session)) {
851dbdbc73bSGuillaume Nault 		error = PTR_ERR(session);
8523953ae7bSGuillaume Nault 		goto err;
853dbdbc73bSGuillaume Nault 	}
854309795f4SJames Chapman 
855f98be6c6SGuillaume Nault 	pppol2tp_session_init(session);
856309795f4SJames Chapman 
8573953ae7bSGuillaume Nault 	error = l2tp_session_register(session, tunnel);
8583953ae7bSGuillaume Nault 	if (error < 0)
8593953ae7bSGuillaume Nault 		goto err_sess;
860309795f4SJames Chapman 
8613953ae7bSGuillaume Nault 	return 0;
862309795f4SJames Chapman 
8633953ae7bSGuillaume Nault err_sess:
8643953ae7bSGuillaume Nault 	kfree(session);
8653953ae7bSGuillaume Nault err:
866309795f4SJames Chapman 	return error;
867309795f4SJames Chapman }
868309795f4SJames Chapman 
869309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */
870309795f4SJames Chapman 
871fd558d18SJames Chapman /* getname() support.
872fd558d18SJames Chapman  */
873fd558d18SJames Chapman static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
8749b2c45d4SDenys Vlasenko 			    int peer)
875fd558d18SJames Chapman {
876e0d4435fSJames Chapman 	int len = 0;
877fd558d18SJames Chapman 	int error = 0;
878fd558d18SJames Chapman 	struct l2tp_session *session;
879fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
880fd558d18SJames Chapman 	struct sock *sk = sock->sk;
881fd558d18SJames Chapman 	struct inet_sock *inet;
882fd558d18SJames Chapman 	struct pppol2tp_session *pls;
883fd558d18SJames Chapman 
884fd558d18SJames Chapman 	error = -ENOTCONN;
885fd558d18SJames Chapman 	if (sk == NULL)
886fd558d18SJames Chapman 		goto end;
88756cff471SGao Feng 	if (!(sk->sk_state & PPPOX_CONNECTED))
888fd558d18SJames Chapman 		goto end;
889fd558d18SJames Chapman 
890fd558d18SJames Chapman 	error = -EBADF;
891fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
892fd558d18SJames Chapman 	if (session == NULL)
893fd558d18SJames Chapman 		goto end;
894fd558d18SJames Chapman 
895fd558d18SJames Chapman 	pls = l2tp_session_priv(session);
8967198c77aSGuillaume Nault 	tunnel = session->tunnel;
897fd558d18SJames Chapman 
898bbdb32cbSBenjamin LaHaise 	inet = inet_sk(tunnel->sock);
899d2cf3361SBenjamin LaHaise 	if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
900e0d4435fSJames Chapman 		struct sockaddr_pppol2tp sp;
901e0d4435fSJames Chapman 		len = sizeof(sp);
902fd558d18SJames Chapman 		memset(&sp, 0, len);
903fd558d18SJames Chapman 		sp.sa_family	= AF_PPPOX;
904fd558d18SJames Chapman 		sp.sa_protocol	= PX_PROTO_OL2TP;
905fd558d18SJames Chapman 		sp.pppol2tp.fd  = tunnel->fd;
906fd558d18SJames Chapman 		sp.pppol2tp.pid = pls->owner;
907fd558d18SJames Chapman 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
908fd558d18SJames Chapman 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
909fd558d18SJames Chapman 		sp.pppol2tp.s_session = session->session_id;
910fd558d18SJames Chapman 		sp.pppol2tp.d_session = session->peer_session_id;
911fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_family = AF_INET;
912fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
913fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
914fd558d18SJames Chapman 		memcpy(uaddr, &sp, len);
915d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
916d2cf3361SBenjamin LaHaise 	} else if ((tunnel->version == 2) &&
917d2cf3361SBenjamin LaHaise 		   (tunnel->sock->sk_family == AF_INET6)) {
918d2cf3361SBenjamin LaHaise 		struct sockaddr_pppol2tpin6 sp;
919efe4208fSEric Dumazet 
920d2cf3361SBenjamin LaHaise 		len = sizeof(sp);
921d2cf3361SBenjamin LaHaise 		memset(&sp, 0, len);
922d2cf3361SBenjamin LaHaise 		sp.sa_family	= AF_PPPOX;
923d2cf3361SBenjamin LaHaise 		sp.sa_protocol	= PX_PROTO_OL2TP;
924d2cf3361SBenjamin LaHaise 		sp.pppol2tp.fd  = tunnel->fd;
925d2cf3361SBenjamin LaHaise 		sp.pppol2tp.pid = pls->owner;
926d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
927d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
928d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_session = session->session_id;
929d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_session = session->peer_session_id;
930d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_family = AF_INET6;
931d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
932efe4208fSEric Dumazet 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
933efe4208fSEric Dumazet 		       sizeof(tunnel->sock->sk_v6_daddr));
934d2cf3361SBenjamin LaHaise 		memcpy(uaddr, &sp, len);
935d2cf3361SBenjamin LaHaise 	} else if ((tunnel->version == 3) &&
936d2cf3361SBenjamin LaHaise 		   (tunnel->sock->sk_family == AF_INET6)) {
937d2cf3361SBenjamin LaHaise 		struct sockaddr_pppol2tpv3in6 sp;
938efe4208fSEric Dumazet 
939d2cf3361SBenjamin LaHaise 		len = sizeof(sp);
940d2cf3361SBenjamin LaHaise 		memset(&sp, 0, len);
941d2cf3361SBenjamin LaHaise 		sp.sa_family	= AF_PPPOX;
942d2cf3361SBenjamin LaHaise 		sp.sa_protocol	= PX_PROTO_OL2TP;
943d2cf3361SBenjamin LaHaise 		sp.pppol2tp.fd  = tunnel->fd;
944d2cf3361SBenjamin LaHaise 		sp.pppol2tp.pid = pls->owner;
945d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
946d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
947d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_session = session->session_id;
948d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_session = session->peer_session_id;
949d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_family = AF_INET6;
950d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
951efe4208fSEric Dumazet 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
952efe4208fSEric Dumazet 		       sizeof(tunnel->sock->sk_v6_daddr));
953d2cf3361SBenjamin LaHaise 		memcpy(uaddr, &sp, len);
954d2cf3361SBenjamin LaHaise #endif
955e0d4435fSJames Chapman 	} else if (tunnel->version == 3) {
956e0d4435fSJames Chapman 		struct sockaddr_pppol2tpv3 sp;
957e0d4435fSJames Chapman 		len = sizeof(sp);
958e0d4435fSJames Chapman 		memset(&sp, 0, len);
959e0d4435fSJames Chapman 		sp.sa_family	= AF_PPPOX;
960e0d4435fSJames Chapman 		sp.sa_protocol	= PX_PROTO_OL2TP;
961e0d4435fSJames Chapman 		sp.pppol2tp.fd  = tunnel->fd;
962e0d4435fSJames Chapman 		sp.pppol2tp.pid = pls->owner;
963e0d4435fSJames Chapman 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
964e0d4435fSJames Chapman 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
965e0d4435fSJames Chapman 		sp.pppol2tp.s_session = session->session_id;
966e0d4435fSJames Chapman 		sp.pppol2tp.d_session = session->peer_session_id;
967e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_family = AF_INET;
968e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
969e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
970e0d4435fSJames Chapman 		memcpy(uaddr, &sp, len);
971e0d4435fSJames Chapman 	}
972fd558d18SJames Chapman 
9739b2c45d4SDenys Vlasenko 	error = len;
974fd558d18SJames Chapman 
975fd558d18SJames Chapman 	sock_put(sk);
976fd558d18SJames Chapman end:
977fd558d18SJames Chapman 	return error;
978fd558d18SJames Chapman }
979fd558d18SJames Chapman 
980fd558d18SJames Chapman /****************************************************************************
981fd558d18SJames Chapman  * ioctl() handlers.
982fd558d18SJames Chapman  *
983fd558d18SJames Chapman  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
984fd558d18SJames Chapman  * sockets. However, in order to control kernel tunnel features, we allow
985fd558d18SJames Chapman  * userspace to create a special "tunnel" PPPoX socket which is used for
986fd558d18SJames Chapman  * control only.  Tunnel PPPoX sockets have session_id == 0 and simply allow
987fd558d18SJames Chapman  * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
988fd558d18SJames Chapman  * calls.
989fd558d18SJames Chapman  ****************************************************************************/
990fd558d18SJames Chapman 
991fd558d18SJames Chapman static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
992fd558d18SJames Chapman 				struct l2tp_stats *stats)
993fd558d18SJames Chapman {
9947b7c0719STom Parkin 	dest->tx_packets = atomic_long_read(&stats->tx_packets);
9957b7c0719STom Parkin 	dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
9967b7c0719STom Parkin 	dest->tx_errors = atomic_long_read(&stats->tx_errors);
9977b7c0719STom Parkin 	dest->rx_packets = atomic_long_read(&stats->rx_packets);
9987b7c0719STom Parkin 	dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
9997b7c0719STom Parkin 	dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
10007b7c0719STom Parkin 	dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
10017b7c0719STom Parkin 	dest->rx_errors = atomic_long_read(&stats->rx_errors);
1002fd558d18SJames Chapman }
1003fd558d18SJames Chapman 
1004fd558d18SJames Chapman /* Session ioctl helper.
1005fd558d18SJames Chapman  */
1006fd558d18SJames Chapman static int pppol2tp_session_ioctl(struct l2tp_session *session,
1007fd558d18SJames Chapman 				  unsigned int cmd, unsigned long arg)
1008fd558d18SJames Chapman {
1009fd558d18SJames Chapman 	struct ifreq ifr;
1010fd558d18SJames Chapman 	int err = 0;
1011fd558d18SJames Chapman 	struct sock *sk;
1012fd558d18SJames Chapman 	int val = (int) arg;
1013fd558d18SJames Chapman 	struct pppol2tp_session *ps = l2tp_session_priv(session);
1014fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1015fd558d18SJames Chapman 	struct pppol2tp_ioc_stats stats;
1016fd558d18SJames Chapman 
1017fba40c63SAsbjørn Sloth Tønnesen 	l2tp_dbg(session, L2TP_MSG_CONTROL,
1018fd558d18SJames Chapman 		 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1019fd558d18SJames Chapman 		 session->name, cmd, arg);
1020fd558d18SJames Chapman 
1021ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
10225903f594SGuillaume Nault 	if (!sk)
10235903f594SGuillaume Nault 		return -EBADR;
10245903f594SGuillaume Nault 
1025fd558d18SJames Chapman 	switch (cmd) {
1026fd558d18SJames Chapman 	case SIOCGIFMTU:
1027fd558d18SJames Chapman 		err = -ENXIO;
1028fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1029fd558d18SJames Chapman 			break;
1030fd558d18SJames Chapman 
1031fd558d18SJames Chapman 		err = -EFAULT;
1032fd558d18SJames Chapman 		if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1033fd558d18SJames Chapman 			break;
1034fd558d18SJames Chapman 		ifr.ifr_mtu = session->mtu;
1035fd558d18SJames Chapman 		if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1036fd558d18SJames Chapman 			break;
1037fd558d18SJames Chapman 
1038fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mtu=%d\n",
1039a4ca44faSJoe Perches 			  session->name, session->mtu);
1040fd558d18SJames Chapman 		err = 0;
1041fd558d18SJames Chapman 		break;
1042fd558d18SJames Chapman 
1043fd558d18SJames Chapman 	case SIOCSIFMTU:
1044fd558d18SJames Chapman 		err = -ENXIO;
1045fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1046fd558d18SJames Chapman 			break;
1047fd558d18SJames Chapman 
1048fd558d18SJames Chapman 		err = -EFAULT;
1049fd558d18SJames Chapman 		if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1050fd558d18SJames Chapman 			break;
1051fd558d18SJames Chapman 
1052fd558d18SJames Chapman 		session->mtu = ifr.ifr_mtu;
1053fd558d18SJames Chapman 
1054fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mtu=%d\n",
1055a4ca44faSJoe Perches 			  session->name, session->mtu);
1056fd558d18SJames Chapman 		err = 0;
1057fd558d18SJames Chapman 		break;
1058fd558d18SJames Chapman 
1059fd558d18SJames Chapman 	case PPPIOCGMRU:
1060fd558d18SJames Chapman 		err = -ENXIO;
1061fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1062fd558d18SJames Chapman 			break;
1063fd558d18SJames Chapman 
1064fd558d18SJames Chapman 		err = -EFAULT;
1065fd558d18SJames Chapman 		if (put_user(session->mru, (int __user *) arg))
1066fd558d18SJames Chapman 			break;
1067fd558d18SJames Chapman 
1068fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mru=%d\n",
1069a4ca44faSJoe Perches 			  session->name, session->mru);
1070fd558d18SJames Chapman 		err = 0;
1071fd558d18SJames Chapman 		break;
1072fd558d18SJames Chapman 
1073fd558d18SJames Chapman 	case PPPIOCSMRU:
1074fd558d18SJames Chapman 		err = -ENXIO;
1075fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1076fd558d18SJames Chapman 			break;
1077fd558d18SJames Chapman 
1078fd558d18SJames Chapman 		err = -EFAULT;
1079fd558d18SJames Chapman 		if (get_user(val, (int __user *) arg))
1080fd558d18SJames Chapman 			break;
1081fd558d18SJames Chapman 
1082fd558d18SJames Chapman 		session->mru = val;
1083fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mru=%d\n",
1084a4ca44faSJoe Perches 			  session->name, session->mru);
1085fd558d18SJames Chapman 		err = 0;
1086fd558d18SJames Chapman 		break;
1087fd558d18SJames Chapman 
1088fd558d18SJames Chapman 	case PPPIOCGFLAGS:
1089fd558d18SJames Chapman 		err = -EFAULT;
1090fd558d18SJames Chapman 		if (put_user(ps->flags, (int __user *) arg))
1091fd558d18SJames Chapman 			break;
1092fd558d18SJames Chapman 
1093fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get flags=%d\n",
1094a4ca44faSJoe Perches 			  session->name, ps->flags);
1095fd558d18SJames Chapman 		err = 0;
1096fd558d18SJames Chapman 		break;
1097fd558d18SJames Chapman 
1098fd558d18SJames Chapman 	case PPPIOCSFLAGS:
1099fd558d18SJames Chapman 		err = -EFAULT;
1100fd558d18SJames Chapman 		if (get_user(val, (int __user *) arg))
1101fd558d18SJames Chapman 			break;
1102fd558d18SJames Chapman 		ps->flags = val;
1103fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set flags=%d\n",
1104a4ca44faSJoe Perches 			  session->name, ps->flags);
1105fd558d18SJames Chapman 		err = 0;
1106fd558d18SJames Chapman 		break;
1107fd558d18SJames Chapman 
1108fd558d18SJames Chapman 	case PPPIOCGL2TPSTATS:
1109fd558d18SJames Chapman 		err = -ENXIO;
1110fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1111fd558d18SJames Chapman 			break;
1112fd558d18SJames Chapman 
1113fd558d18SJames Chapman 		memset(&stats, 0, sizeof(stats));
1114fd558d18SJames Chapman 		stats.tunnel_id = tunnel->tunnel_id;
1115fd558d18SJames Chapman 		stats.session_id = session->session_id;
1116fd558d18SJames Chapman 		pppol2tp_copy_stats(&stats, &session->stats);
1117fd558d18SJames Chapman 		if (copy_to_user((void __user *) arg, &stats,
1118fd558d18SJames Chapman 				 sizeof(stats)))
1119fd558d18SJames Chapman 			break;
1120fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1121a4ca44faSJoe Perches 			  session->name);
1122fd558d18SJames Chapman 		err = 0;
1123fd558d18SJames Chapman 		break;
1124fd558d18SJames Chapman 
1125fd558d18SJames Chapman 	default:
1126fd558d18SJames Chapman 		err = -ENOSYS;
1127fd558d18SJames Chapman 		break;
1128fd558d18SJames Chapman 	}
1129fd558d18SJames Chapman 
1130fd558d18SJames Chapman 	sock_put(sk);
1131fd558d18SJames Chapman 
1132fd558d18SJames Chapman 	return err;
1133fd558d18SJames Chapman }
1134fd558d18SJames Chapman 
1135fd558d18SJames Chapman /* Tunnel ioctl helper.
1136fd558d18SJames Chapman  *
1137fd558d18SJames Chapman  * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1138fd558d18SJames Chapman  * specifies a session_id, the session ioctl handler is called. This allows an
1139fd558d18SJames Chapman  * application to retrieve session stats via a tunnel socket.
1140fd558d18SJames Chapman  */
1141fd558d18SJames Chapman static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1142fd558d18SJames Chapman 				 unsigned int cmd, unsigned long arg)
1143fd558d18SJames Chapman {
1144fd558d18SJames Chapman 	int err = 0;
1145fd558d18SJames Chapman 	struct sock *sk;
1146fd558d18SJames Chapman 	struct pppol2tp_ioc_stats stats;
1147fd558d18SJames Chapman 
1148fba40c63SAsbjørn Sloth Tønnesen 	l2tp_dbg(tunnel, L2TP_MSG_CONTROL,
1149fd558d18SJames Chapman 		 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1150fd558d18SJames Chapman 		 tunnel->name, cmd, arg);
1151fd558d18SJames Chapman 
1152fd558d18SJames Chapman 	sk = tunnel->sock;
1153fd558d18SJames Chapman 	sock_hold(sk);
1154fd558d18SJames Chapman 
1155fd558d18SJames Chapman 	switch (cmd) {
1156fd558d18SJames Chapman 	case PPPIOCGL2TPSTATS:
1157fd558d18SJames Chapman 		err = -ENXIO;
1158fd558d18SJames Chapman 		if (!(sk->sk_state & PPPOX_CONNECTED))
1159fd558d18SJames Chapman 			break;
1160fd558d18SJames Chapman 
1161fd558d18SJames Chapman 		if (copy_from_user(&stats, (void __user *) arg,
1162fd558d18SJames Chapman 				   sizeof(stats))) {
1163fd558d18SJames Chapman 			err = -EFAULT;
1164fd558d18SJames Chapman 			break;
1165fd558d18SJames Chapman 		}
1166fd558d18SJames Chapman 		if (stats.session_id != 0) {
1167fd558d18SJames Chapman 			/* resend to session ioctl handler */
1168fd558d18SJames Chapman 			struct l2tp_session *session =
116957377d63SGuillaume Nault 				l2tp_session_get(sock_net(sk), tunnel,
1170a4346210SGuillaume Nault 						 stats.session_id);
117157377d63SGuillaume Nault 
117257377d63SGuillaume Nault 			if (session) {
117357377d63SGuillaume Nault 				err = pppol2tp_session_ioctl(session, cmd,
117457377d63SGuillaume Nault 							     arg);
117557377d63SGuillaume Nault 				l2tp_session_dec_refcount(session);
117657377d63SGuillaume Nault 			} else {
1177fd558d18SJames Chapman 				err = -EBADR;
117857377d63SGuillaume Nault 			}
1179fd558d18SJames Chapman 			break;
1180fd558d18SJames Chapman 		}
1181fd558d18SJames Chapman #ifdef CONFIG_XFRM
1182fd558d18SJames Chapman 		stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1183fd558d18SJames Chapman #endif
1184fd558d18SJames Chapman 		pppol2tp_copy_stats(&stats, &tunnel->stats);
1185fd558d18SJames Chapman 		if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1186fd558d18SJames Chapman 			err = -EFAULT;
1187fd558d18SJames Chapman 			break;
1188fd558d18SJames Chapman 		}
1189fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1190a4ca44faSJoe Perches 			  tunnel->name);
1191fd558d18SJames Chapman 		err = 0;
1192fd558d18SJames Chapman 		break;
1193fd558d18SJames Chapman 
1194fd558d18SJames Chapman 	default:
1195fd558d18SJames Chapman 		err = -ENOSYS;
1196fd558d18SJames Chapman 		break;
1197fd558d18SJames Chapman 	}
1198fd558d18SJames Chapman 
1199fd558d18SJames Chapman 	sock_put(sk);
1200fd558d18SJames Chapman 
1201fd558d18SJames Chapman 	return err;
1202fd558d18SJames Chapman }
1203fd558d18SJames Chapman 
1204fd558d18SJames Chapman /* Main ioctl() handler.
1205fd558d18SJames Chapman  * Dispatch to tunnel or session helpers depending on the socket.
1206fd558d18SJames Chapman  */
1207fd558d18SJames Chapman static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1208fd558d18SJames Chapman 			  unsigned long arg)
1209fd558d18SJames Chapman {
1210fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1211fd558d18SJames Chapman 	struct l2tp_session *session;
1212fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1213fd558d18SJames Chapman 	int err;
1214fd558d18SJames Chapman 
1215fd558d18SJames Chapman 	if (!sk)
1216fd558d18SJames Chapman 		return 0;
1217fd558d18SJames Chapman 
1218fd558d18SJames Chapman 	err = -EBADF;
1219fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) != 0)
1220fd558d18SJames Chapman 		goto end;
1221fd558d18SJames Chapman 
1222fd558d18SJames Chapman 	err = -ENOTCONN;
1223fd558d18SJames Chapman 	if ((sk->sk_user_data == NULL) ||
1224fd558d18SJames Chapman 	    (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1225fd558d18SJames Chapman 		goto end;
1226fd558d18SJames Chapman 
1227fd558d18SJames Chapman 	/* Get session context from the socket */
1228fd558d18SJames Chapman 	err = -EBADF;
1229fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
1230fd558d18SJames Chapman 	if (session == NULL)
1231fd558d18SJames Chapman 		goto end;
1232fd558d18SJames Chapman 
1233fd558d18SJames Chapman 	/* Special case: if session's session_id is zero, treat ioctl as a
1234fd558d18SJames Chapman 	 * tunnel ioctl
1235fd558d18SJames Chapman 	 */
1236fd558d18SJames Chapman 	if ((session->session_id == 0) &&
1237fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
12387198c77aSGuillaume Nault 		tunnel = session->tunnel;
1239fd558d18SJames Chapman 		err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1240fd558d18SJames Chapman 		goto end_put_sess;
1241fd558d18SJames Chapman 	}
1242fd558d18SJames Chapman 
1243fd558d18SJames Chapman 	err = pppol2tp_session_ioctl(session, cmd, arg);
1244fd558d18SJames Chapman 
1245fd558d18SJames Chapman end_put_sess:
1246fd558d18SJames Chapman 	sock_put(sk);
1247fd558d18SJames Chapman end:
1248fd558d18SJames Chapman 	return err;
1249fd558d18SJames Chapman }
1250fd558d18SJames Chapman 
1251fd558d18SJames Chapman /*****************************************************************************
1252fd558d18SJames Chapman  * setsockopt() / getsockopt() support.
1253fd558d18SJames Chapman  *
1254fd558d18SJames Chapman  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1255fd558d18SJames Chapman  * sockets. In order to control kernel tunnel features, we allow userspace to
1256fd558d18SJames Chapman  * create a special "tunnel" PPPoX socket which is used for control only.
1257fd558d18SJames Chapman  * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1258fd558d18SJames Chapman  * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1259fd558d18SJames Chapman  *****************************************************************************/
1260fd558d18SJames Chapman 
1261fd558d18SJames Chapman /* Tunnel setsockopt() helper.
1262fd558d18SJames Chapman  */
1263fd558d18SJames Chapman static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1264fd558d18SJames Chapman 				      struct l2tp_tunnel *tunnel,
1265fd558d18SJames Chapman 				      int optname, int val)
1266fd558d18SJames Chapman {
1267fd558d18SJames Chapman 	int err = 0;
1268fd558d18SJames Chapman 
1269fd558d18SJames Chapman 	switch (optname) {
1270fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1271fd558d18SJames Chapman 		tunnel->debug = val;
1272fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1273a4ca44faSJoe Perches 			  tunnel->name, tunnel->debug);
1274fd558d18SJames Chapman 		break;
1275fd558d18SJames Chapman 
1276fd558d18SJames Chapman 	default:
1277fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1278fd558d18SJames Chapman 		break;
1279fd558d18SJames Chapman 	}
1280fd558d18SJames Chapman 
1281fd558d18SJames Chapman 	return err;
1282fd558d18SJames Chapman }
1283fd558d18SJames Chapman 
1284fd558d18SJames Chapman /* Session setsockopt helper.
1285fd558d18SJames Chapman  */
1286fd558d18SJames Chapman static int pppol2tp_session_setsockopt(struct sock *sk,
1287fd558d18SJames Chapman 				       struct l2tp_session *session,
1288fd558d18SJames Chapman 				       int optname, int val)
1289fd558d18SJames Chapman {
1290fd558d18SJames Chapman 	int err = 0;
1291fd558d18SJames Chapman 
1292fd558d18SJames Chapman 	switch (optname) {
1293fd558d18SJames Chapman 	case PPPOL2TP_SO_RECVSEQ:
1294fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1295fd558d18SJames Chapman 			err = -EINVAL;
1296fd558d18SJames Chapman 			break;
1297fd558d18SJames Chapman 		}
12983f9b9770SAsbjørn Sloth Tønnesen 		session->recv_seq = !!val;
1299fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1300a4ca44faSJoe Perches 			  "%s: set recv_seq=%d\n",
1301a4ca44faSJoe Perches 			  session->name, session->recv_seq);
1302fd558d18SJames Chapman 		break;
1303fd558d18SJames Chapman 
1304fd558d18SJames Chapman 	case PPPOL2TP_SO_SENDSEQ:
1305fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1306fd558d18SJames Chapman 			err = -EINVAL;
1307fd558d18SJames Chapman 			break;
1308fd558d18SJames Chapman 		}
13093f9b9770SAsbjørn Sloth Tønnesen 		session->send_seq = !!val;
1310fd558d18SJames Chapman 		{
1311ee40fb2eSGuillaume Nault 			struct pppox_sock *po = pppox_sk(sk);
1312ee40fb2eSGuillaume Nault 
1313fd558d18SJames Chapman 			po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1314fd558d18SJames Chapman 				PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1315fd558d18SJames Chapman 		}
1316bb5016eaSGuillaume Nault 		l2tp_session_set_header_len(session, session->tunnel->version);
1317fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1318a4ca44faSJoe Perches 			  "%s: set send_seq=%d\n",
1319a4ca44faSJoe Perches 			  session->name, session->send_seq);
1320fd558d18SJames Chapman 		break;
1321fd558d18SJames Chapman 
1322fd558d18SJames Chapman 	case PPPOL2TP_SO_LNSMODE:
1323fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1324fd558d18SJames Chapman 			err = -EINVAL;
1325fd558d18SJames Chapman 			break;
1326fd558d18SJames Chapman 		}
13273f9b9770SAsbjørn Sloth Tønnesen 		session->lns_mode = !!val;
1328fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1329a4ca44faSJoe Perches 			  "%s: set lns_mode=%d\n",
1330a4ca44faSJoe Perches 			  session->name, session->lns_mode);
1331fd558d18SJames Chapman 		break;
1332fd558d18SJames Chapman 
1333fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1334fd558d18SJames Chapman 		session->debug = val;
1335fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1336a4ca44faSJoe Perches 			  session->name, session->debug);
1337fd558d18SJames Chapman 		break;
1338fd558d18SJames Chapman 
1339fd558d18SJames Chapman 	case PPPOL2TP_SO_REORDERTO:
1340fd558d18SJames Chapman 		session->reorder_timeout = msecs_to_jiffies(val);
1341fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1342a4ca44faSJoe Perches 			  "%s: set reorder_timeout=%d\n",
1343a4ca44faSJoe Perches 			  session->name, session->reorder_timeout);
1344fd558d18SJames Chapman 		break;
1345fd558d18SJames Chapman 
1346fd558d18SJames Chapman 	default:
1347fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1348fd558d18SJames Chapman 		break;
1349fd558d18SJames Chapman 	}
1350fd558d18SJames Chapman 
1351fd558d18SJames Chapman 	return err;
1352fd558d18SJames Chapman }
1353fd558d18SJames Chapman 
1354fd558d18SJames Chapman /* Main setsockopt() entry point.
1355fd558d18SJames Chapman  * Does API checks, then calls either the tunnel or session setsockopt
1356fd558d18SJames Chapman  * handler, according to whether the PPPoL2TP socket is a for a regular
1357fd558d18SJames Chapman  * session or the special tunnel type.
1358fd558d18SJames Chapman  */
1359fd558d18SJames Chapman static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1360fd558d18SJames Chapman 			       char __user *optval, unsigned int optlen)
1361fd558d18SJames Chapman {
1362fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1363fd558d18SJames Chapman 	struct l2tp_session *session;
1364fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1365fd558d18SJames Chapman 	int val;
1366fd558d18SJames Chapman 	int err;
1367fd558d18SJames Chapman 
1368fd558d18SJames Chapman 	if (level != SOL_PPPOL2TP)
13693cf521f7SSasha Levin 		return -EINVAL;
1370fd558d18SJames Chapman 
1371fd558d18SJames Chapman 	if (optlen < sizeof(int))
1372fd558d18SJames Chapman 		return -EINVAL;
1373fd558d18SJames Chapman 
1374fd558d18SJames Chapman 	if (get_user(val, (int __user *)optval))
1375fd558d18SJames Chapman 		return -EFAULT;
1376fd558d18SJames Chapman 
1377fd558d18SJames Chapman 	err = -ENOTCONN;
1378fd558d18SJames Chapman 	if (sk->sk_user_data == NULL)
1379fd558d18SJames Chapman 		goto end;
1380fd558d18SJames Chapman 
1381fd558d18SJames Chapman 	/* Get session context from the socket */
1382fd558d18SJames Chapman 	err = -EBADF;
1383fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
1384fd558d18SJames Chapman 	if (session == NULL)
1385fd558d18SJames Chapman 		goto end;
1386fd558d18SJames Chapman 
1387fd558d18SJames Chapman 	/* Special case: if session_id == 0x0000, treat as operation on tunnel
1388fd558d18SJames Chapman 	 */
1389fd558d18SJames Chapman 	if ((session->session_id == 0) &&
1390fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
13917198c77aSGuillaume Nault 		tunnel = session->tunnel;
1392fd558d18SJames Chapman 		err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
13937198c77aSGuillaume Nault 	} else {
1394fd558d18SJames Chapman 		err = pppol2tp_session_setsockopt(sk, session, optname, val);
13957198c77aSGuillaume Nault 	}
1396fd558d18SJames Chapman 
1397fd558d18SJames Chapman 	sock_put(sk);
1398fd558d18SJames Chapman end:
1399fd558d18SJames Chapman 	return err;
1400fd558d18SJames Chapman }
1401fd558d18SJames Chapman 
1402fd558d18SJames Chapman /* Tunnel getsockopt helper. Called with sock locked.
1403fd558d18SJames Chapman  */
1404fd558d18SJames Chapman static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1405fd558d18SJames Chapman 				      struct l2tp_tunnel *tunnel,
1406fd558d18SJames Chapman 				      int optname, int *val)
1407fd558d18SJames Chapman {
1408fd558d18SJames Chapman 	int err = 0;
1409fd558d18SJames Chapman 
1410fd558d18SJames Chapman 	switch (optname) {
1411fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1412fd558d18SJames Chapman 		*val = tunnel->debug;
1413fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
1414a4ca44faSJoe Perches 			  tunnel->name, tunnel->debug);
1415fd558d18SJames Chapman 		break;
1416fd558d18SJames Chapman 
1417fd558d18SJames Chapman 	default:
1418fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1419fd558d18SJames Chapman 		break;
1420fd558d18SJames Chapman 	}
1421fd558d18SJames Chapman 
1422fd558d18SJames Chapman 	return err;
1423fd558d18SJames Chapman }
1424fd558d18SJames Chapman 
1425fd558d18SJames Chapman /* Session getsockopt helper. Called with sock locked.
1426fd558d18SJames Chapman  */
1427fd558d18SJames Chapman static int pppol2tp_session_getsockopt(struct sock *sk,
1428fd558d18SJames Chapman 				       struct l2tp_session *session,
1429fd558d18SJames Chapman 				       int optname, int *val)
1430fd558d18SJames Chapman {
1431fd558d18SJames Chapman 	int err = 0;
1432fd558d18SJames Chapman 
1433fd558d18SJames Chapman 	switch (optname) {
1434fd558d18SJames Chapman 	case PPPOL2TP_SO_RECVSEQ:
1435fd558d18SJames Chapman 		*val = session->recv_seq;
1436fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1437fd558d18SJames Chapman 			  "%s: get recv_seq=%d\n", session->name, *val);
1438fd558d18SJames Chapman 		break;
1439fd558d18SJames Chapman 
1440fd558d18SJames Chapman 	case PPPOL2TP_SO_SENDSEQ:
1441fd558d18SJames Chapman 		*val = session->send_seq;
1442fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1443fd558d18SJames Chapman 			  "%s: get send_seq=%d\n", session->name, *val);
1444fd558d18SJames Chapman 		break;
1445fd558d18SJames Chapman 
1446fd558d18SJames Chapman 	case PPPOL2TP_SO_LNSMODE:
1447fd558d18SJames Chapman 		*val = session->lns_mode;
1448fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1449fd558d18SJames Chapman 			  "%s: get lns_mode=%d\n", session->name, *val);
1450fd558d18SJames Chapman 		break;
1451fd558d18SJames Chapman 
1452fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1453fd558d18SJames Chapman 		*val = session->debug;
1454fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
1455a4ca44faSJoe Perches 			  session->name, *val);
1456fd558d18SJames Chapman 		break;
1457fd558d18SJames Chapman 
1458fd558d18SJames Chapman 	case PPPOL2TP_SO_REORDERTO:
1459fd558d18SJames Chapman 		*val = (int) jiffies_to_msecs(session->reorder_timeout);
1460fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1461fd558d18SJames Chapman 			  "%s: get reorder_timeout=%d\n", session->name, *val);
1462fd558d18SJames Chapman 		break;
1463fd558d18SJames Chapman 
1464fd558d18SJames Chapman 	default:
1465fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1466fd558d18SJames Chapman 	}
1467fd558d18SJames Chapman 
1468fd558d18SJames Chapman 	return err;
1469fd558d18SJames Chapman }
1470fd558d18SJames Chapman 
1471fd558d18SJames Chapman /* Main getsockopt() entry point.
1472fd558d18SJames Chapman  * Does API checks, then calls either the tunnel or session getsockopt
1473fd558d18SJames Chapman  * handler, according to whether the PPPoX socket is a for a regular session
1474fd558d18SJames Chapman  * or the special tunnel type.
1475fd558d18SJames Chapman  */
1476e3192690SJoe Perches static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1477e3192690SJoe Perches 			       char __user *optval, int __user *optlen)
1478fd558d18SJames Chapman {
1479fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1480fd558d18SJames Chapman 	struct l2tp_session *session;
1481fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1482fd558d18SJames Chapman 	int val, len;
1483fd558d18SJames Chapman 	int err;
1484fd558d18SJames Chapman 
1485fd558d18SJames Chapman 	if (level != SOL_PPPOL2TP)
14863cf521f7SSasha Levin 		return -EINVAL;
1487fd558d18SJames Chapman 
1488e3192690SJoe Perches 	if (get_user(len, optlen))
1489fd558d18SJames Chapman 		return -EFAULT;
1490fd558d18SJames Chapman 
1491fd558d18SJames Chapman 	len = min_t(unsigned int, len, sizeof(int));
1492fd558d18SJames Chapman 
1493fd558d18SJames Chapman 	if (len < 0)
1494fd558d18SJames Chapman 		return -EINVAL;
1495fd558d18SJames Chapman 
1496fd558d18SJames Chapman 	err = -ENOTCONN;
1497fd558d18SJames Chapman 	if (sk->sk_user_data == NULL)
1498fd558d18SJames Chapman 		goto end;
1499fd558d18SJames Chapman 
1500fd558d18SJames Chapman 	/* Get the session context */
1501fd558d18SJames Chapman 	err = -EBADF;
1502fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
1503fd558d18SJames Chapman 	if (session == NULL)
1504fd558d18SJames Chapman 		goto end;
1505fd558d18SJames Chapman 
1506fd558d18SJames Chapman 	/* Special case: if session_id == 0x0000, treat as operation on tunnel */
1507fd558d18SJames Chapman 	if ((session->session_id == 0) &&
1508fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
15097198c77aSGuillaume Nault 		tunnel = session->tunnel;
1510fd558d18SJames Chapman 		err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1511321a52a3SGuillaume Nault 		if (err)
1512321a52a3SGuillaume Nault 			goto end_put_sess;
1513321a52a3SGuillaume Nault 	} else {
1514fd558d18SJames Chapman 		err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1515321a52a3SGuillaume Nault 		if (err)
1516321a52a3SGuillaume Nault 			goto end_put_sess;
1517321a52a3SGuillaume Nault 	}
1518fd558d18SJames Chapman 
1519fd558d18SJames Chapman 	err = -EFAULT;
1520e3192690SJoe Perches 	if (put_user(len, optlen))
1521fd558d18SJames Chapman 		goto end_put_sess;
1522fd558d18SJames Chapman 
1523fd558d18SJames Chapman 	if (copy_to_user((void __user *) optval, &val, len))
1524fd558d18SJames Chapman 		goto end_put_sess;
1525fd558d18SJames Chapman 
1526fd558d18SJames Chapman 	err = 0;
1527fd558d18SJames Chapman 
1528fd558d18SJames Chapman end_put_sess:
1529fd558d18SJames Chapman 	sock_put(sk);
1530fd558d18SJames Chapman end:
1531fd558d18SJames Chapman 	return err;
1532fd558d18SJames Chapman }
1533fd558d18SJames Chapman 
1534fd558d18SJames Chapman /*****************************************************************************
1535fd558d18SJames Chapman  * /proc filesystem for debug
1536f7faffa3SJames Chapman  * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1537f7faffa3SJames Chapman  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1538fd558d18SJames Chapman  *****************************************************************************/
1539fd558d18SJames Chapman 
1540fd558d18SJames Chapman static unsigned int pppol2tp_net_id;
1541fd558d18SJames Chapman 
1542fd558d18SJames Chapman #ifdef CONFIG_PROC_FS
1543fd558d18SJames Chapman 
1544fd558d18SJames Chapman struct pppol2tp_seq_data {
1545fd558d18SJames Chapman 	struct seq_net_private p;
1546fd558d18SJames Chapman 	int tunnel_idx;			/* current tunnel */
1547fd558d18SJames Chapman 	int session_idx;		/* index of session within current tunnel */
1548fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1549fd558d18SJames Chapman 	struct l2tp_session *session;	/* NULL means get next tunnel */
1550fd558d18SJames Chapman };
1551fd558d18SJames Chapman 
1552fd558d18SJames Chapman static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1553fd558d18SJames Chapman {
1554*0e0c3feeSGuillaume Nault 	/* Drop reference taken during previous invocation */
1555*0e0c3feeSGuillaume Nault 	if (pd->tunnel)
1556*0e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
1557*0e0c3feeSGuillaume Nault 
1558f7faffa3SJames Chapman 	for (;;) {
1559*0e0c3feeSGuillaume Nault 		pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
1560fd558d18SJames Chapman 		pd->tunnel_idx++;
1561f7faffa3SJames Chapman 
1562*0e0c3feeSGuillaume Nault 		/* Only accept L2TPv2 tunnels */
1563*0e0c3feeSGuillaume Nault 		if (!pd->tunnel || pd->tunnel->version == 2)
1564*0e0c3feeSGuillaume Nault 			return;
1565f7faffa3SJames Chapman 
1566*0e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
1567f7faffa3SJames Chapman 	}
1568fd558d18SJames Chapman }
1569fd558d18SJames Chapman 
1570fd558d18SJames Chapman static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1571fd558d18SJames Chapman {
1572a4346210SGuillaume Nault 	pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
1573fd558d18SJames Chapman 	pd->session_idx++;
1574f7faffa3SJames Chapman 
1575fd558d18SJames Chapman 	if (pd->session == NULL) {
1576fd558d18SJames Chapman 		pd->session_idx = 0;
1577fd558d18SJames Chapman 		pppol2tp_next_tunnel(net, pd);
1578fd558d18SJames Chapman 	}
1579fd558d18SJames Chapman }
1580fd558d18SJames Chapman 
1581fd558d18SJames Chapman static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1582fd558d18SJames Chapman {
1583fd558d18SJames Chapman 	struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1584fd558d18SJames Chapman 	loff_t pos = *offs;
1585fd558d18SJames Chapman 	struct net *net;
1586fd558d18SJames Chapman 
1587fd558d18SJames Chapman 	if (!pos)
1588fd558d18SJames Chapman 		goto out;
1589fd558d18SJames Chapman 
1590fd558d18SJames Chapman 	BUG_ON(m->private == NULL);
1591fd558d18SJames Chapman 	pd = m->private;
1592fd558d18SJames Chapman 	net = seq_file_net(m);
1593fd558d18SJames Chapman 
1594fd558d18SJames Chapman 	if (pd->tunnel == NULL)
1595fd558d18SJames Chapman 		pppol2tp_next_tunnel(net, pd);
1596fd558d18SJames Chapman 	else
1597fd558d18SJames Chapman 		pppol2tp_next_session(net, pd);
1598fd558d18SJames Chapman 
1599fd558d18SJames Chapman 	/* NULL tunnel and session indicates end of list */
1600fd558d18SJames Chapman 	if ((pd->tunnel == NULL) && (pd->session == NULL))
1601fd558d18SJames Chapman 		pd = NULL;
1602fd558d18SJames Chapman 
1603fd558d18SJames Chapman out:
1604fd558d18SJames Chapman 	return pd;
1605fd558d18SJames Chapman }
1606fd558d18SJames Chapman 
1607fd558d18SJames Chapman static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1608fd558d18SJames Chapman {
1609fd558d18SJames Chapman 	(*pos)++;
1610fd558d18SJames Chapman 	return NULL;
1611fd558d18SJames Chapman }
1612fd558d18SJames Chapman 
1613fd558d18SJames Chapman static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1614fd558d18SJames Chapman {
1615*0e0c3feeSGuillaume Nault 	struct pppol2tp_seq_data *pd = v;
1616*0e0c3feeSGuillaume Nault 
1617*0e0c3feeSGuillaume Nault 	if (!pd || pd == SEQ_START_TOKEN)
1618*0e0c3feeSGuillaume Nault 		return;
1619*0e0c3feeSGuillaume Nault 
1620*0e0c3feeSGuillaume Nault 	/* Drop reference taken by last invocation of pppol2tp_next_tunnel() */
1621*0e0c3feeSGuillaume Nault 	if (pd->tunnel)
1622*0e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
1623fd558d18SJames Chapman }
1624fd558d18SJames Chapman 
1625fd558d18SJames Chapman static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1626fd558d18SJames Chapman {
1627fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = v;
1628fd558d18SJames Chapman 
1629fd558d18SJames Chapman 	seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1630fd558d18SJames Chapman 		   tunnel->name,
1631fd558d18SJames Chapman 		   (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1632fbea9e07SReshetova, Elena 		   refcount_read(&tunnel->ref_count) - 1);
16337b7c0719STom Parkin 	seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1634fd558d18SJames Chapman 		   tunnel->debug,
16357b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_packets),
16367b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_bytes),
16377b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_errors),
16387b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_packets),
16397b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_bytes),
16407b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_errors));
1641fd558d18SJames Chapman }
1642fd558d18SJames Chapman 
1643fd558d18SJames Chapman static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1644fd558d18SJames Chapman {
1645fd558d18SJames Chapman 	struct l2tp_session *session = v;
1646fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1647ee40fb2eSGuillaume Nault 	unsigned char state;
1648ee40fb2eSGuillaume Nault 	char user_data_ok;
1649ee40fb2eSGuillaume Nault 	struct sock *sk;
1650fd558d18SJames Chapman 	u32 ip = 0;
1651fd558d18SJames Chapman 	u16 port = 0;
1652fd558d18SJames Chapman 
1653fd558d18SJames Chapman 	if (tunnel->sock) {
1654fd558d18SJames Chapman 		struct inet_sock *inet = inet_sk(tunnel->sock);
1655fd558d18SJames Chapman 		ip = ntohl(inet->inet_saddr);
1656fd558d18SJames Chapman 		port = ntohs(inet->inet_sport);
1657fd558d18SJames Chapman 	}
1658fd558d18SJames Chapman 
1659ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
1660ee40fb2eSGuillaume Nault 	if (sk) {
1661ee40fb2eSGuillaume Nault 		state = sk->sk_state;
1662ee40fb2eSGuillaume Nault 		user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1663ee40fb2eSGuillaume Nault 	} else {
1664ee40fb2eSGuillaume Nault 		state = 0;
1665ee40fb2eSGuillaume Nault 		user_data_ok = 'N';
1666ee40fb2eSGuillaume Nault 	}
1667ee40fb2eSGuillaume Nault 
1668fd558d18SJames Chapman 	seq_printf(m, "  SESSION '%s' %08X/%d %04X/%04X -> "
1669fd558d18SJames Chapman 		   "%04X/%04X %d %c\n",
1670fd558d18SJames Chapman 		   session->name, ip, port,
1671fd558d18SJames Chapman 		   tunnel->tunnel_id,
1672fd558d18SJames Chapman 		   session->session_id,
1673fd558d18SJames Chapman 		   tunnel->peer_tunnel_id,
1674fd558d18SJames Chapman 		   session->peer_session_id,
1675ee40fb2eSGuillaume Nault 		   state, user_data_ok);
1676fd558d18SJames Chapman 	seq_printf(m, "   %d/%d/%c/%c/%s %08x %u\n",
1677fd558d18SJames Chapman 		   session->mtu, session->mru,
1678fd558d18SJames Chapman 		   session->recv_seq ? 'R' : '-',
1679fd558d18SJames Chapman 		   session->send_seq ? 'S' : '-',
1680fd558d18SJames Chapman 		   session->lns_mode ? "LNS" : "LAC",
1681fd558d18SJames Chapman 		   session->debug,
1682fd558d18SJames Chapman 		   jiffies_to_msecs(session->reorder_timeout));
16837b7c0719STom Parkin 	seq_printf(m, "   %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
1684fd558d18SJames Chapman 		   session->nr, session->ns,
16857b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_packets),
16867b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_bytes),
16877b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_errors),
16887b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_packets),
16897b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_bytes),
16907b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_errors));
16919345471bSJames Chapman 
1692ee40fb2eSGuillaume Nault 	if (sk) {
1693ee40fb2eSGuillaume Nault 		struct pppox_sock *po = pppox_sk(sk);
1694ee40fb2eSGuillaume Nault 
16959345471bSJames Chapman 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
1696ee40fb2eSGuillaume Nault 		sock_put(sk);
1697ee40fb2eSGuillaume Nault 	}
1698fd558d18SJames Chapman }
1699fd558d18SJames Chapman 
1700fd558d18SJames Chapman static int pppol2tp_seq_show(struct seq_file *m, void *v)
1701fd558d18SJames Chapman {
1702fd558d18SJames Chapman 	struct pppol2tp_seq_data *pd = v;
1703fd558d18SJames Chapman 
1704fd558d18SJames Chapman 	/* display header on line 1 */
1705fd558d18SJames Chapman 	if (v == SEQ_START_TOKEN) {
1706fd558d18SJames Chapman 		seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1707fd558d18SJames Chapman 		seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1708fd558d18SJames Chapman 		seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1709fd558d18SJames Chapman 		seq_puts(m, "  SESSION name, addr/port src-tid/sid "
1710fd558d18SJames Chapman 			 "dest-tid/sid state user-data-ok\n");
1711fd558d18SJames Chapman 		seq_puts(m, "   mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1712fd558d18SJames Chapman 		seq_puts(m, "   nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1713fd558d18SJames Chapman 		goto out;
1714fd558d18SJames Chapman 	}
1715fd558d18SJames Chapman 
1716fd558d18SJames Chapman 	/* Show the tunnel or session context.
1717fd558d18SJames Chapman 	 */
1718e08293a4SGuillaume Nault 	if (!pd->session) {
1719fd558d18SJames Chapman 		pppol2tp_seq_tunnel_show(m, pd->tunnel);
1720e08293a4SGuillaume Nault 	} else {
1721fd558d18SJames Chapman 		pppol2tp_seq_session_show(m, pd->session);
1722e08293a4SGuillaume Nault 		l2tp_session_dec_refcount(pd->session);
1723e08293a4SGuillaume Nault 	}
1724fd558d18SJames Chapman 
1725fd558d18SJames Chapman out:
1726fd558d18SJames Chapman 	return 0;
1727fd558d18SJames Chapman }
1728fd558d18SJames Chapman 
1729fd558d18SJames Chapman static const struct seq_operations pppol2tp_seq_ops = {
1730fd558d18SJames Chapman 	.start		= pppol2tp_seq_start,
1731fd558d18SJames Chapman 	.next		= pppol2tp_seq_next,
1732fd558d18SJames Chapman 	.stop		= pppol2tp_seq_stop,
1733fd558d18SJames Chapman 	.show		= pppol2tp_seq_show,
1734fd558d18SJames Chapman };
1735fd558d18SJames Chapman 
1736fd558d18SJames Chapman /* Called when our /proc file is opened. We allocate data for use when
1737fd558d18SJames Chapman  * iterating our tunnel / session contexts and store it in the private
1738fd558d18SJames Chapman  * data of the seq_file.
1739fd558d18SJames Chapman  */
1740fd558d18SJames Chapman static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1741fd558d18SJames Chapman {
1742fd558d18SJames Chapman 	return seq_open_net(inode, file, &pppol2tp_seq_ops,
1743fd558d18SJames Chapman 			    sizeof(struct pppol2tp_seq_data));
1744fd558d18SJames Chapman }
1745fd558d18SJames Chapman 
1746fd558d18SJames Chapman static const struct file_operations pppol2tp_proc_fops = {
1747fd558d18SJames Chapman 	.open		= pppol2tp_proc_open,
1748fd558d18SJames Chapman 	.read		= seq_read,
1749fd558d18SJames Chapman 	.llseek		= seq_lseek,
1750fd558d18SJames Chapman 	.release	= seq_release_net,
1751fd558d18SJames Chapman };
1752fd558d18SJames Chapman 
1753fd558d18SJames Chapman #endif /* CONFIG_PROC_FS */
1754fd558d18SJames Chapman 
1755fd558d18SJames Chapman /*****************************************************************************
1756fd558d18SJames Chapman  * Network namespace
1757fd558d18SJames Chapman  *****************************************************************************/
1758fd558d18SJames Chapman 
1759fd558d18SJames Chapman static __net_init int pppol2tp_init_net(struct net *net)
1760fd558d18SJames Chapman {
1761fd558d18SJames Chapman 	struct proc_dir_entry *pde;
1762fd558d18SJames Chapman 	int err = 0;
1763fd558d18SJames Chapman 
1764d6444062SJoe Perches 	pde = proc_create("pppol2tp", 0444, net->proc_net,
1765d4beaa66SGao feng 			  &pppol2tp_proc_fops);
1766fd558d18SJames Chapman 	if (!pde) {
1767fd558d18SJames Chapman 		err = -ENOMEM;
1768fd558d18SJames Chapman 		goto out;
1769fd558d18SJames Chapman 	}
1770fd558d18SJames Chapman 
1771fd558d18SJames Chapman out:
1772fd558d18SJames Chapman 	return err;
1773fd558d18SJames Chapman }
1774fd558d18SJames Chapman 
1775fd558d18SJames Chapman static __net_exit void pppol2tp_exit_net(struct net *net)
1776fd558d18SJames Chapman {
1777ece31ffdSGao feng 	remove_proc_entry("pppol2tp", net->proc_net);
1778fd558d18SJames Chapman }
1779fd558d18SJames Chapman 
1780fd558d18SJames Chapman static struct pernet_operations pppol2tp_net_ops = {
1781fd558d18SJames Chapman 	.init = pppol2tp_init_net,
1782fd558d18SJames Chapman 	.exit = pppol2tp_exit_net,
1783fd558d18SJames Chapman 	.id   = &pppol2tp_net_id,
1784fd558d18SJames Chapman };
1785fd558d18SJames Chapman 
1786fd558d18SJames Chapman /*****************************************************************************
1787fd558d18SJames Chapman  * Init and cleanup
1788fd558d18SJames Chapman  *****************************************************************************/
1789fd558d18SJames Chapman 
1790fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops = {
1791fd558d18SJames Chapman 	.family		= AF_PPPOX,
1792fd558d18SJames Chapman 	.owner		= THIS_MODULE,
1793fd558d18SJames Chapman 	.release	= pppol2tp_release,
1794fd558d18SJames Chapman 	.bind		= sock_no_bind,
1795fd558d18SJames Chapman 	.connect	= pppol2tp_connect,
1796fd558d18SJames Chapman 	.socketpair	= sock_no_socketpair,
1797fd558d18SJames Chapman 	.accept		= sock_no_accept,
1798fd558d18SJames Chapman 	.getname	= pppol2tp_getname,
1799fd558d18SJames Chapman 	.poll		= datagram_poll,
1800fd558d18SJames Chapman 	.listen		= sock_no_listen,
1801fd558d18SJames Chapman 	.shutdown	= sock_no_shutdown,
1802fd558d18SJames Chapman 	.setsockopt	= pppol2tp_setsockopt,
1803fd558d18SJames Chapman 	.getsockopt	= pppol2tp_getsockopt,
1804fd558d18SJames Chapman 	.sendmsg	= pppol2tp_sendmsg,
1805fd558d18SJames Chapman 	.recvmsg	= pppol2tp_recvmsg,
1806fd558d18SJames Chapman 	.mmap		= sock_no_mmap,
1807fd558d18SJames Chapman 	.ioctl		= pppox_ioctl,
1808fd558d18SJames Chapman };
1809fd558d18SJames Chapman 
1810756e64a0SEric Dumazet static const struct pppox_proto pppol2tp_proto = {
1811fd558d18SJames Chapman 	.create		= pppol2tp_create,
1812e1558a93SWei Yongjun 	.ioctl		= pppol2tp_ioctl,
1813e1558a93SWei Yongjun 	.owner		= THIS_MODULE,
1814fd558d18SJames Chapman };
1815fd558d18SJames Chapman 
1816309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1817309795f4SJames Chapman 
1818309795f4SJames Chapman static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1819309795f4SJames Chapman 	.session_create	= pppol2tp_session_create,
1820cf2f5c88STom Parkin 	.session_delete	= l2tp_session_delete,
1821309795f4SJames Chapman };
1822309795f4SJames Chapman 
1823309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */
1824309795f4SJames Chapman 
1825fd558d18SJames Chapman static int __init pppol2tp_init(void)
1826fd558d18SJames Chapman {
1827fd558d18SJames Chapman 	int err;
1828fd558d18SJames Chapman 
1829fd558d18SJames Chapman 	err = register_pernet_device(&pppol2tp_net_ops);
1830fd558d18SJames Chapman 	if (err)
1831fd558d18SJames Chapman 		goto out;
1832fd558d18SJames Chapman 
1833fd558d18SJames Chapman 	err = proto_register(&pppol2tp_sk_proto, 0);
1834fd558d18SJames Chapman 	if (err)
1835fd558d18SJames Chapman 		goto out_unregister_pppol2tp_pernet;
1836fd558d18SJames Chapman 
1837fd558d18SJames Chapman 	err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1838fd558d18SJames Chapman 	if (err)
1839fd558d18SJames Chapman 		goto out_unregister_pppol2tp_proto;
1840fd558d18SJames Chapman 
1841309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1842309795f4SJames Chapman 	err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1843309795f4SJames Chapman 	if (err)
1844309795f4SJames Chapman 		goto out_unregister_pppox;
1845309795f4SJames Chapman #endif
1846309795f4SJames Chapman 
1847a4ca44faSJoe Perches 	pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1848fd558d18SJames Chapman 
1849fd558d18SJames Chapman out:
1850fd558d18SJames Chapman 	return err;
1851309795f4SJames Chapman 
1852309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1853309795f4SJames Chapman out_unregister_pppox:
1854309795f4SJames Chapman 	unregister_pppox_proto(PX_PROTO_OL2TP);
1855309795f4SJames Chapman #endif
1856fd558d18SJames Chapman out_unregister_pppol2tp_proto:
1857fd558d18SJames Chapman 	proto_unregister(&pppol2tp_sk_proto);
1858fd558d18SJames Chapman out_unregister_pppol2tp_pernet:
1859fd558d18SJames Chapman 	unregister_pernet_device(&pppol2tp_net_ops);
1860fd558d18SJames Chapman 	goto out;
1861fd558d18SJames Chapman }
1862fd558d18SJames Chapman 
1863fd558d18SJames Chapman static void __exit pppol2tp_exit(void)
1864fd558d18SJames Chapman {
1865309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1866309795f4SJames Chapman 	l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1867309795f4SJames Chapman #endif
1868fd558d18SJames Chapman 	unregister_pppox_proto(PX_PROTO_OL2TP);
1869fd558d18SJames Chapman 	proto_unregister(&pppol2tp_sk_proto);
1870fd558d18SJames Chapman 	unregister_pernet_device(&pppol2tp_net_ops);
1871fd558d18SJames Chapman }
1872fd558d18SJames Chapman 
1873fd558d18SJames Chapman module_init(pppol2tp_init);
1874fd558d18SJames Chapman module_exit(pppol2tp_exit);
1875fd558d18SJames Chapman 
1876fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1877fd558d18SJames Chapman MODULE_DESCRIPTION("PPP over L2TP over UDP");
1878fd558d18SJames Chapman MODULE_LICENSE("GPL");
1879fd558d18SJames Chapman MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1880681b4d88SGuillaume Nault MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1881249ee819SGuillaume Nault MODULE_ALIAS_L2TP_PWTYPE(7);
1882