xref: /openbmc/linux/net/l2tp/l2tp_ppp.c (revision 6c0ec37b82834630cfe99ef42690beef18d2b400)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fd558d18SJames Chapman /*****************************************************************************
3fd558d18SJames Chapman  * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4fd558d18SJames Chapman  *
5fd558d18SJames Chapman  * PPPoX    --- Generic PPP encapsulation socket family
6fd558d18SJames Chapman  * PPPoL2TP --- PPP over L2TP (RFC 2661)
7fd558d18SJames Chapman  *
8fd558d18SJames Chapman  * Version:	2.0.0
9fd558d18SJames Chapman  *
10fd558d18SJames Chapman  * Authors:	James Chapman (jchapman@katalix.com)
11fd558d18SJames Chapman  *
12fd558d18SJames Chapman  * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
13fd558d18SJames Chapman  *
14fd558d18SJames Chapman  * License:
15fd558d18SJames Chapman  */
16fd558d18SJames Chapman 
17fd558d18SJames Chapman /* This driver handles only L2TP data frames; control frames are handled by a
18fd558d18SJames Chapman  * userspace application.
19fd558d18SJames Chapman  *
20fd558d18SJames Chapman  * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
21fd558d18SJames Chapman  * attaches it to a bound UDP socket with local tunnel_id / session_id and
22fd558d18SJames Chapman  * peer tunnel_id / session_id set. Data can then be sent or received using
23fd558d18SJames Chapman  * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
24fd558d18SJames Chapman  * can be read or modified using ioctl() or [gs]etsockopt() calls.
25fd558d18SJames Chapman  *
26fd558d18SJames Chapman  * When a PPPoL2TP socket is connected with local and peer session_id values
27fd558d18SJames Chapman  * zero, the socket is treated as a special tunnel management socket.
28fd558d18SJames Chapman  *
29fd558d18SJames Chapman  * Here's example userspace code to create a socket for sending/receiving data
30fd558d18SJames Chapman  * over an L2TP session:-
31fd558d18SJames Chapman  *
32fd558d18SJames Chapman  *	struct sockaddr_pppol2tp sax;
33fd558d18SJames Chapman  *	int fd;
34fd558d18SJames Chapman  *	int session_fd;
35fd558d18SJames Chapman  *
36fd558d18SJames Chapman  *	fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
37fd558d18SJames Chapman  *
38fd558d18SJames Chapman  *	sax.sa_family = AF_PPPOX;
39fd558d18SJames Chapman  *	sax.sa_protocol = PX_PROTO_OL2TP;
40fd558d18SJames Chapman  *	sax.pppol2tp.fd = tunnel_fd;	// bound UDP socket
41fd558d18SJames Chapman  *	sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
42fd558d18SJames Chapman  *	sax.pppol2tp.addr.sin_port = addr->sin_port;
43fd558d18SJames Chapman  *	sax.pppol2tp.addr.sin_family = AF_INET;
44fd558d18SJames Chapman  *	sax.pppol2tp.s_tunnel  = tunnel_id;
45fd558d18SJames Chapman  *	sax.pppol2tp.s_session = session_id;
46fd558d18SJames Chapman  *	sax.pppol2tp.d_tunnel  = peer_tunnel_id;
47fd558d18SJames Chapman  *	sax.pppol2tp.d_session = peer_session_id;
48fd558d18SJames Chapman  *
49fd558d18SJames Chapman  *	session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
50fd558d18SJames Chapman  *
51fd558d18SJames Chapman  * A pppd plugin that allows PPP traffic to be carried over L2TP using
52fd558d18SJames Chapman  * this driver is available from the OpenL2TP project at
53fd558d18SJames Chapman  * http://openl2tp.sourceforge.net.
54fd558d18SJames Chapman  */
55fd558d18SJames Chapman 
56a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
57a4ca44faSJoe Perches 
58fd558d18SJames Chapman #include <linux/module.h>
59fd558d18SJames Chapman #include <linux/string.h>
60fd558d18SJames Chapman #include <linux/list.h>
61fd558d18SJames Chapman #include <linux/uaccess.h>
62fd558d18SJames Chapman 
63fd558d18SJames Chapman #include <linux/kernel.h>
64fd558d18SJames Chapman #include <linux/spinlock.h>
65fd558d18SJames Chapman #include <linux/kthread.h>
66fd558d18SJames Chapman #include <linux/sched.h>
67fd558d18SJames Chapman #include <linux/slab.h>
68fd558d18SJames Chapman #include <linux/errno.h>
69fd558d18SJames Chapman #include <linux/jiffies.h>
70fd558d18SJames Chapman 
71fd558d18SJames Chapman #include <linux/netdevice.h>
72fd558d18SJames Chapman #include <linux/net.h>
73fd558d18SJames Chapman #include <linux/inetdevice.h>
74fd558d18SJames Chapman #include <linux/skbuff.h>
75fd558d18SJames Chapman #include <linux/init.h>
76fd558d18SJames Chapman #include <linux/ip.h>
77fd558d18SJames Chapman #include <linux/udp.h>
78fd558d18SJames Chapman #include <linux/if_pppox.h>
79fd558d18SJames Chapman #include <linux/if_pppol2tp.h>
80fd558d18SJames Chapman #include <net/sock.h>
81fd558d18SJames Chapman #include <linux/ppp_channel.h>
82fd558d18SJames Chapman #include <linux/ppp_defs.h>
834b32da2bSPaul Mackerras #include <linux/ppp-ioctl.h>
84fd558d18SJames Chapman #include <linux/file.h>
85fd558d18SJames Chapman #include <linux/hash.h>
86fd558d18SJames Chapman #include <linux/sort.h>
87fd558d18SJames Chapman #include <linux/proc_fs.h>
88309795f4SJames Chapman #include <linux/l2tp.h>
89fd558d18SJames Chapman #include <linux/nsproxy.h>
90fd558d18SJames Chapman #include <net/net_namespace.h>
91fd558d18SJames Chapman #include <net/netns/generic.h>
92fd558d18SJames Chapman #include <net/ip.h>
93fd558d18SJames Chapman #include <net/udp.h>
94cf2f5c88STom Parkin #include <net/inet_common.h>
95fd558d18SJames Chapman 
96fd558d18SJames Chapman #include <asm/byteorder.h>
9760063497SArun Sharma #include <linux/atomic.h>
98fd558d18SJames Chapman 
99fd558d18SJames Chapman #include "l2tp_core.h"
100fd558d18SJames Chapman 
101fd558d18SJames Chapman #define PPPOL2TP_DRV_VERSION	"V2.0"
102fd558d18SJames Chapman 
103fd558d18SJames Chapman /* Space for UDP, L2TP and PPP headers */
104fd558d18SJames Chapman #define PPPOL2TP_HEADER_OVERHEAD	40
105fd558d18SJames Chapman 
106fd558d18SJames Chapman /* Number of bytes to build transmit L2TP headers.
107fd558d18SJames Chapman  * Unfortunately the size is different depending on whether sequence numbers
108fd558d18SJames Chapman  * are enabled.
109fd558d18SJames Chapman  */
110fd558d18SJames Chapman #define PPPOL2TP_L2TP_HDR_SIZE_SEQ		10
111fd558d18SJames Chapman #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ		6
112fd558d18SJames Chapman 
113fd558d18SJames Chapman /* Private data of each session. This data lives at the end of struct
114fd558d18SJames Chapman  * l2tp_session, referenced via session->priv[].
115fd558d18SJames Chapman  */
116fd558d18SJames Chapman struct pppol2tp_session {
117fd558d18SJames Chapman 	int			owner;		/* pid that opened the socket */
118fd558d18SJames Chapman 
119ee40fb2eSGuillaume Nault 	struct mutex		sk_lock;	/* Protects .sk */
12020dcb110STom Parkin 	struct sock __rcu	*sk;		/* Pointer to the session PPPoX socket */
121ee40fb2eSGuillaume Nault 	struct sock		*__sk;		/* Copy of .sk, for cleanup */
122ee40fb2eSGuillaume Nault 	struct rcu_head		rcu;		/* For asynchronous release */
123fd558d18SJames Chapman };
124fd558d18SJames Chapman 
125fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
126fd558d18SJames Chapman 
127d7100da0Sstephen hemminger static const struct ppp_channel_ops pppol2tp_chan_ops = {
128d7100da0Sstephen hemminger 	.start_xmit =  pppol2tp_xmit,
129d7100da0Sstephen hemminger };
130d7100da0Sstephen hemminger 
131fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops;
132fd558d18SJames Chapman 
133ee40fb2eSGuillaume Nault /* Retrieves the pppol2tp socket associated to a session.
134ee40fb2eSGuillaume Nault  * A reference is held on the returned socket, so this function must be paired
135ee40fb2eSGuillaume Nault  * with sock_put().
136ee40fb2eSGuillaume Nault  */
137ee40fb2eSGuillaume Nault static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
138ee40fb2eSGuillaume Nault {
139ee40fb2eSGuillaume Nault 	struct pppol2tp_session *ps = l2tp_session_priv(session);
140ee40fb2eSGuillaume Nault 	struct sock *sk;
141ee40fb2eSGuillaume Nault 
142ee40fb2eSGuillaume Nault 	rcu_read_lock();
143ee40fb2eSGuillaume Nault 	sk = rcu_dereference(ps->sk);
144ee40fb2eSGuillaume Nault 	if (sk)
145ee40fb2eSGuillaume Nault 		sock_hold(sk);
146ee40fb2eSGuillaume Nault 	rcu_read_unlock();
147ee40fb2eSGuillaume Nault 
148ee40fb2eSGuillaume Nault 	return sk;
149ee40fb2eSGuillaume Nault }
150ee40fb2eSGuillaume Nault 
151fd558d18SJames Chapman /* Helpers to obtain tunnel/session contexts from sockets.
152fd558d18SJames Chapman  */
153fd558d18SJames Chapman static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
154fd558d18SJames Chapman {
155fd558d18SJames Chapman 	struct l2tp_session *session;
156fd558d18SJames Chapman 
1570febc7b3STom Parkin 	if (!sk)
158fd558d18SJames Chapman 		return NULL;
159fd558d18SJames Chapman 
160fd558d18SJames Chapman 	sock_hold(sk);
161fd558d18SJames Chapman 	session = (struct l2tp_session *)(sk->sk_user_data);
1620febc7b3STom Parkin 	if (!session) {
163fd558d18SJames Chapman 		sock_put(sk);
164fd558d18SJames Chapman 		goto out;
165fd558d18SJames Chapman 	}
166fd558d18SJames Chapman 
167fd558d18SJames Chapman 	BUG_ON(session->magic != L2TP_SESSION_MAGIC);
168fd558d18SJames Chapman 
169fd558d18SJames Chapman out:
170fd558d18SJames Chapman 	return session;
171fd558d18SJames Chapman }
172fd558d18SJames Chapman 
173fd558d18SJames Chapman /*****************************************************************************
174fd558d18SJames Chapman  * Receive data handling
175fd558d18SJames Chapman  *****************************************************************************/
176fd558d18SJames Chapman 
177fd558d18SJames Chapman /* Receive message. This is the recvmsg for the PPPoL2TP socket.
178fd558d18SJames Chapman  */
1791b784140SYing Xue static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
1801b784140SYing Xue 			    size_t len, int flags)
181fd558d18SJames Chapman {
182fd558d18SJames Chapman 	int err;
183fd558d18SJames Chapman 	struct sk_buff *skb;
184fd558d18SJames Chapman 	struct sock *sk = sock->sk;
185fd558d18SJames Chapman 
186fd558d18SJames Chapman 	err = -EIO;
187fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_BOUND)
188fd558d18SJames Chapman 		goto end;
189fd558d18SJames Chapman 
190fd558d18SJames Chapman 	err = 0;
191fd558d18SJames Chapman 	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
192fd558d18SJames Chapman 				flags & MSG_DONTWAIT, &err);
193fd558d18SJames Chapman 	if (!skb)
194fd558d18SJames Chapman 		goto end;
195fd558d18SJames Chapman 
196fd558d18SJames Chapman 	if (len > skb->len)
197fd558d18SJames Chapman 		len = skb->len;
198fd558d18SJames Chapman 	else if (len < skb->len)
199fd558d18SJames Chapman 		msg->msg_flags |= MSG_TRUNC;
200fd558d18SJames Chapman 
20151f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, len);
202fd558d18SJames Chapman 	if (likely(err == 0))
203fd558d18SJames Chapman 		err = len;
204fd558d18SJames Chapman 
205fd558d18SJames Chapman 	kfree_skb(skb);
206fd558d18SJames Chapman end:
207fd558d18SJames Chapman 	return err;
208fd558d18SJames Chapman }
209fd558d18SJames Chapman 
210fd558d18SJames Chapman static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
211fd558d18SJames Chapman {
212fd558d18SJames Chapman 	struct pppol2tp_session *ps = l2tp_session_priv(session);
213fd558d18SJames Chapman 	struct sock *sk = NULL;
214fd558d18SJames Chapman 
215fd558d18SJames Chapman 	/* If the socket is bound, send it in to PPP's input queue. Otherwise
216fd558d18SJames Chapman 	 * queue it on the session socket.
217fd558d18SJames Chapman 	 */
218ee40fb2eSGuillaume Nault 	rcu_read_lock();
219ee40fb2eSGuillaume Nault 	sk = rcu_dereference(ps->sk);
2200febc7b3STom Parkin 	if (!sk)
221fd558d18SJames Chapman 		goto no_sock;
222fd558d18SJames Chapman 
2232b139e6bSGuillaume Nault 	/* If the first two bytes are 0xFF03, consider that it is the PPP's
2242b139e6bSGuillaume Nault 	 * Address and Control fields and skip them. The L2TP module has always
2252b139e6bSGuillaume Nault 	 * worked this way, although, in theory, the use of these fields should
2262b139e6bSGuillaume Nault 	 * be negociated and handled at the PPP layer. These fields are
2272b139e6bSGuillaume Nault 	 * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
2282b139e6bSGuillaume Nault 	 * Information command with Poll/Final bit set to zero (RFC 1662).
2292b139e6bSGuillaume Nault 	 */
2302b139e6bSGuillaume Nault 	if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
2312b139e6bSGuillaume Nault 	    skb->data[1] == PPP_UI)
2322b139e6bSGuillaume Nault 		skb_pull(skb, 2);
2332b139e6bSGuillaume Nault 
234fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_BOUND) {
235fd558d18SJames Chapman 		struct pppox_sock *po;
23698f40b3eSGuillaume Nault 
237fba40c63SAsbjørn Sloth Tønnesen 		l2tp_dbg(session, L2TP_MSG_DATA,
238fd558d18SJames Chapman 			 "%s: recv %d byte data frame, passing to ppp\n",
239fd558d18SJames Chapman 			 session->name, data_len);
240fd558d18SJames Chapman 
241fd558d18SJames Chapman 		po = pppox_sk(sk);
242fd558d18SJames Chapman 		ppp_input(&po->chan, skb);
243fd558d18SJames Chapman 	} else {
244fba40c63SAsbjørn Sloth Tønnesen 		l2tp_dbg(session, L2TP_MSG_DATA,
2459e9cb622SGuillaume Nault 			 "%s: recv %d byte data frame, passing to L2TP socket\n",
2469e9cb622SGuillaume Nault 			 session->name, data_len);
247fd558d18SJames Chapman 
2489e9cb622SGuillaume Nault 		if (sock_queue_rcv_skb(sk, skb) < 0) {
2497b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_errors);
250fd558d18SJames Chapman 			kfree_skb(skb);
251fd558d18SJames Chapman 		}
2529e9cb622SGuillaume Nault 	}
253ee40fb2eSGuillaume Nault 	rcu_read_unlock();
254fd558d18SJames Chapman 
255fd558d18SJames Chapman 	return;
256fd558d18SJames Chapman 
257fd558d18SJames Chapman no_sock:
258ee40fb2eSGuillaume Nault 	rcu_read_unlock();
259fba40c63SAsbjørn Sloth Tønnesen 	l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
260fd558d18SJames Chapman 	kfree_skb(skb);
261fd558d18SJames Chapman }
262fd558d18SJames Chapman 
263fd558d18SJames Chapman /************************************************************************
264fd558d18SJames Chapman  * Transmit handling
265fd558d18SJames Chapman  ***********************************************************************/
266fd558d18SJames Chapman 
267fd558d18SJames Chapman /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket.  We come here
268fd558d18SJames Chapman  * when a user application does a sendmsg() on the session socket. L2TP and
269fd558d18SJames Chapman  * PPP headers must be inserted into the user's data.
270fd558d18SJames Chapman  */
2711b784140SYing Xue static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
272fd558d18SJames Chapman 			    size_t total_len)
273fd558d18SJames Chapman {
274fd558d18SJames Chapman 	struct sock *sk = sock->sk;
275fd558d18SJames Chapman 	struct sk_buff *skb;
276fd558d18SJames Chapman 	int error;
277fd558d18SJames Chapman 	struct l2tp_session *session;
278fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
2790d76751fSJames Chapman 	int uhlen;
280fd558d18SJames Chapman 
281fd558d18SJames Chapman 	error = -ENOTCONN;
282fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
283fd558d18SJames Chapman 		goto error;
284fd558d18SJames Chapman 
285fd558d18SJames Chapman 	/* Get session and tunnel contexts */
286fd558d18SJames Chapman 	error = -EBADF;
287fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
2880febc7b3STom Parkin 	if (!session)
289fd558d18SJames Chapman 		goto error;
290fd558d18SJames Chapman 
2917198c77aSGuillaume Nault 	tunnel = session->tunnel;
292fd558d18SJames Chapman 
2930d76751fSJames Chapman 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
2940d76751fSJames Chapman 
295fd558d18SJames Chapman 	/* Allocate a socket buffer */
296fd558d18SJames Chapman 	error = -ENOMEM;
297fd558d18SJames Chapman 	skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
2980d76751fSJames Chapman 			   uhlen + session->hdr_len +
29954c151d9SGao Feng 			   2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
300fd558d18SJames Chapman 			   0, GFP_KERNEL);
301fd558d18SJames Chapman 	if (!skb)
3027198c77aSGuillaume Nault 		goto error_put_sess;
303fd558d18SJames Chapman 
304fd558d18SJames Chapman 	/* Reserve space for headers. */
305fd558d18SJames Chapman 	skb_reserve(skb, NET_SKB_PAD);
306fd558d18SJames Chapman 	skb_reset_network_header(skb);
307fd558d18SJames Chapman 	skb_reserve(skb, sizeof(struct iphdr));
308fd558d18SJames Chapman 	skb_reset_transport_header(skb);
3090d76751fSJames Chapman 	skb_reserve(skb, uhlen);
310fd558d18SJames Chapman 
311fd558d18SJames Chapman 	/* Add PPP header */
31254c151d9SGao Feng 	skb->data[0] = PPP_ALLSTATIONS;
31354c151d9SGao Feng 	skb->data[1] = PPP_UI;
314fd558d18SJames Chapman 	skb_put(skb, 2);
315fd558d18SJames Chapman 
316fd558d18SJames Chapman 	/* Copy user data into skb */
3176ce8e9ceSAl Viro 	error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
318fd558d18SJames Chapman 	if (error < 0) {
319fd558d18SJames Chapman 		kfree_skb(skb);
3207198c77aSGuillaume Nault 		goto error_put_sess;
321fd558d18SJames Chapman 	}
322fd558d18SJames Chapman 
323455cc32bSEric Dumazet 	local_bh_disable();
324fd558d18SJames Chapman 	l2tp_xmit_skb(session, skb, session->hdr_len);
325455cc32bSEric Dumazet 	local_bh_enable();
326fd558d18SJames Chapman 
3278b82547eSGuillaume Nault 	sock_put(sk);
328fd558d18SJames Chapman 
329a6f79d0fSGuillaume Nault 	return total_len;
330fd558d18SJames Chapman 
331fd558d18SJames Chapman error_put_sess:
332fd558d18SJames Chapman 	sock_put(sk);
333fd558d18SJames Chapman error:
334fd558d18SJames Chapman 	return error;
335fd558d18SJames Chapman }
336fd558d18SJames Chapman 
337fd558d18SJames Chapman /* Transmit function called by generic PPP driver.  Sends PPP frame
338fd558d18SJames Chapman  * over PPPoL2TP socket.
339fd558d18SJames Chapman  *
340fd558d18SJames Chapman  * This is almost the same as pppol2tp_sendmsg(), but rather than
341fd558d18SJames Chapman  * being called with a msghdr from userspace, it is called with a skb
342fd558d18SJames Chapman  * from the kernel.
343fd558d18SJames Chapman  *
344fd558d18SJames Chapman  * The supplied skb from ppp doesn't have enough headroom for the
345fd558d18SJames Chapman  * insertion of L2TP, UDP and IP headers so we need to allocate more
346fd558d18SJames Chapman  * headroom in the skb. This will create a cloned skb. But we must be
347fd558d18SJames Chapman  * careful in the error case because the caller will expect to free
348fd558d18SJames Chapman  * the skb it supplied, not our cloned skb. So we take care to always
349fd558d18SJames Chapman  * leave the original skb unfreed if we return an error.
350fd558d18SJames Chapman  */
351fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
352fd558d18SJames Chapman {
353fd558d18SJames Chapman 	struct sock *sk = (struct sock *)chan->private;
354fd558d18SJames Chapman 	struct l2tp_session *session;
355fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
35609df57caSEric Dumazet 	int uhlen, headroom;
357fd558d18SJames Chapman 
358fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
359fd558d18SJames Chapman 		goto abort;
360fd558d18SJames Chapman 
361fd558d18SJames Chapman 	/* Get session and tunnel contexts from the socket */
362fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
3630febc7b3STom Parkin 	if (!session)
364fd558d18SJames Chapman 		goto abort;
365fd558d18SJames Chapman 
3667198c77aSGuillaume Nault 	tunnel = session->tunnel;
367fd558d18SJames Chapman 
36809df57caSEric Dumazet 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
36909df57caSEric Dumazet 	headroom = NET_SKB_PAD +
37009df57caSEric Dumazet 		   sizeof(struct iphdr) + /* IP header */
37109df57caSEric Dumazet 		   uhlen +		/* UDP header (if L2TP_ENCAPTYPE_UDP) */
37209df57caSEric Dumazet 		   session->hdr_len +	/* L2TP header */
37354c151d9SGao Feng 		   2;			/* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
37409df57caSEric Dumazet 	if (skb_cow_head(skb, headroom))
3757198c77aSGuillaume Nault 		goto abort_put_sess;
376fd558d18SJames Chapman 
377fd558d18SJames Chapman 	/* Setup PPP header */
37854c151d9SGao Feng 	__skb_push(skb, 2);
37954c151d9SGao Feng 	skb->data[0] = PPP_ALLSTATIONS;
38054c151d9SGao Feng 	skb->data[1] = PPP_UI;
381fd558d18SJames Chapman 
382455cc32bSEric Dumazet 	local_bh_disable();
383e0d4435fSJames Chapman 	l2tp_xmit_skb(session, skb, session->hdr_len);
384455cc32bSEric Dumazet 	local_bh_enable();
385fd558d18SJames Chapman 
386fd558d18SJames Chapman 	sock_put(sk);
3877198c77aSGuillaume Nault 
388fd558d18SJames Chapman 	return 1;
389fd558d18SJames Chapman 
390fd558d18SJames Chapman abort_put_sess:
391fd558d18SJames Chapman 	sock_put(sk);
392fd558d18SJames Chapman abort:
393fd558d18SJames Chapman 	/* Free the original skb */
394fd558d18SJames Chapman 	kfree_skb(skb);
395fd558d18SJames Chapman 	return 1;
396fd558d18SJames Chapman }
397fd558d18SJames Chapman 
398fd558d18SJames Chapman /*****************************************************************************
399fd558d18SJames Chapman  * Session (and tunnel control) socket create/destroy.
400fd558d18SJames Chapman  *****************************************************************************/
401fd558d18SJames Chapman 
402d02ba2a6SJames Chapman static void pppol2tp_put_sk(struct rcu_head *head)
403d02ba2a6SJames Chapman {
404d02ba2a6SJames Chapman 	struct pppol2tp_session *ps;
405d02ba2a6SJames Chapman 
406d02ba2a6SJames Chapman 	ps = container_of(head, typeof(*ps), rcu);
407d02ba2a6SJames Chapman 	sock_put(ps->__sk);
408d02ba2a6SJames Chapman }
409d02ba2a6SJames Chapman 
410fd558d18SJames Chapman /* Really kill the session socket. (Called from sock_put() if
411fd558d18SJames Chapman  * refcnt == 0.)
412fd558d18SJames Chapman  */
413fd558d18SJames Chapman static void pppol2tp_session_destruct(struct sock *sk)
414fd558d18SJames Chapman {
415f6e16b29STom Parkin 	struct l2tp_session *session = sk->sk_user_data;
416e91793bbSGuillaume Nault 
417e91793bbSGuillaume Nault 	skb_queue_purge(&sk->sk_receive_queue);
418e91793bbSGuillaume Nault 	skb_queue_purge(&sk->sk_write_queue);
419e91793bbSGuillaume Nault 
420f6e16b29STom Parkin 	if (session) {
421fd558d18SJames Chapman 		sk->sk_user_data = NULL;
422fd558d18SJames Chapman 		BUG_ON(session->magic != L2TP_SESSION_MAGIC);
423fd558d18SJames Chapman 		l2tp_session_dec_refcount(session);
424fd558d18SJames Chapman 	}
425fd558d18SJames Chapman }
426fd558d18SJames Chapman 
427fd558d18SJames Chapman /* Called when the PPPoX socket (session) is closed.
428fd558d18SJames Chapman  */
429fd558d18SJames Chapman static int pppol2tp_release(struct socket *sock)
430fd558d18SJames Chapman {
431fd558d18SJames Chapman 	struct sock *sk = sock->sk;
432fd558d18SJames Chapman 	struct l2tp_session *session;
433fd558d18SJames Chapman 	int error;
434fd558d18SJames Chapman 
435fd558d18SJames Chapman 	if (!sk)
436fd558d18SJames Chapman 		return 0;
437fd558d18SJames Chapman 
438fd558d18SJames Chapman 	error = -EBADF;
439fd558d18SJames Chapman 	lock_sock(sk);
440fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) != 0)
441fd558d18SJames Chapman 		goto error;
442fd558d18SJames Chapman 
443fd558d18SJames Chapman 	pppox_unbind_sock(sk);
444fd558d18SJames Chapman 
445fd558d18SJames Chapman 	/* Signal the death of the socket. */
446fd558d18SJames Chapman 	sk->sk_state = PPPOX_DEAD;
447fd558d18SJames Chapman 	sock_orphan(sk);
448fd558d18SJames Chapman 	sock->sk = NULL;
449fd558d18SJames Chapman 
450d02ba2a6SJames Chapman 	session = pppol2tp_sock_to_session(sk);
451d02ba2a6SJames Chapman 	if (session) {
4523d609342SGuillaume Nault 		struct pppol2tp_session *ps;
4533d609342SGuillaume Nault 
454d02ba2a6SJames Chapman 		l2tp_session_delete(session);
4553d609342SGuillaume Nault 
4563d609342SGuillaume Nault 		ps = l2tp_session_priv(session);
4573d609342SGuillaume Nault 		mutex_lock(&ps->sk_lock);
4583d609342SGuillaume Nault 		ps->__sk = rcu_dereference_protected(ps->sk,
4593d609342SGuillaume Nault 						     lockdep_is_held(&ps->sk_lock));
4603d609342SGuillaume Nault 		RCU_INIT_POINTER(ps->sk, NULL);
4613d609342SGuillaume Nault 		mutex_unlock(&ps->sk_lock);
4623d609342SGuillaume Nault 		call_rcu(&ps->rcu, pppol2tp_put_sk);
4633d609342SGuillaume Nault 
4643d609342SGuillaume Nault 		/* Rely on the sock_put() call at the end of the function for
4653d609342SGuillaume Nault 		 * dropping the reference held by pppol2tp_sock_to_session().
4663d609342SGuillaume Nault 		 * The last reference will be dropped by pppol2tp_put_sk().
4673d609342SGuillaume Nault 		 */
468cf2f5c88STom Parkin 	}
469d02ba2a6SJames Chapman 
470fd558d18SJames Chapman 	release_sock(sk);
471fd558d18SJames Chapman 
472fd558d18SJames Chapman 	/* This will delete the session context via
473fd558d18SJames Chapman 	 * pppol2tp_session_destruct() if the socket's refcnt drops to
474fd558d18SJames Chapman 	 * zero.
475fd558d18SJames Chapman 	 */
476fd558d18SJames Chapman 	sock_put(sk);
477fd558d18SJames Chapman 
478fd558d18SJames Chapman 	return 0;
479fd558d18SJames Chapman 
480fd558d18SJames Chapman error:
481fd558d18SJames Chapman 	release_sock(sk);
482fd558d18SJames Chapman 	return error;
483fd558d18SJames Chapman }
484fd558d18SJames Chapman 
485fd558d18SJames Chapman static struct proto pppol2tp_sk_proto = {
486fd558d18SJames Chapman 	.name	  = "PPPOL2TP",
487fd558d18SJames Chapman 	.owner	  = THIS_MODULE,
488fd558d18SJames Chapman 	.obj_size = sizeof(struct pppox_sock),
489fd558d18SJames Chapman };
490fd558d18SJames Chapman 
491fd558d18SJames Chapman static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
492fd558d18SJames Chapman {
493fd558d18SJames Chapman 	int rc;
494fd558d18SJames Chapman 
495fd558d18SJames Chapman 	rc = l2tp_udp_encap_recv(sk, skb);
496fd558d18SJames Chapman 	if (rc)
497fd558d18SJames Chapman 		kfree_skb(skb);
498fd558d18SJames Chapman 
499fd558d18SJames Chapman 	return NET_RX_SUCCESS;
500fd558d18SJames Chapman }
501fd558d18SJames Chapman 
502fd558d18SJames Chapman /* socket() handler. Initialize a new struct sock.
503fd558d18SJames Chapman  */
50411aa9c28SEric W. Biederman static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
505fd558d18SJames Chapman {
506fd558d18SJames Chapman 	int error = -ENOMEM;
507fd558d18SJames Chapman 	struct sock *sk;
508fd558d18SJames Chapman 
50911aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
510fd558d18SJames Chapman 	if (!sk)
511fd558d18SJames Chapman 		goto out;
512fd558d18SJames Chapman 
513fd558d18SJames Chapman 	sock_init_data(sock, sk);
514fd558d18SJames Chapman 
515fd558d18SJames Chapman 	sock->state  = SS_UNCONNECTED;
516fd558d18SJames Chapman 	sock->ops    = &pppol2tp_ops;
517fd558d18SJames Chapman 
518fd558d18SJames Chapman 	sk->sk_backlog_rcv = pppol2tp_backlog_recv;
519fd558d18SJames Chapman 	sk->sk_protocol	   = PX_PROTO_OL2TP;
520fd558d18SJames Chapman 	sk->sk_family	   = PF_PPPOX;
521fd558d18SJames Chapman 	sk->sk_state	   = PPPOX_NONE;
522fd558d18SJames Chapman 	sk->sk_type	   = SOCK_STREAM;
523fd558d18SJames Chapman 	sk->sk_destruct	   = pppol2tp_session_destruct;
524fd558d18SJames Chapman 
525fd558d18SJames Chapman 	error = 0;
526fd558d18SJames Chapman 
527fd558d18SJames Chapman out:
528fd558d18SJames Chapman 	return error;
529fd558d18SJames Chapman }
530fd558d18SJames Chapman 
5310ad66140SJames Chapman static void pppol2tp_show(struct seq_file *m, void *arg)
5320ad66140SJames Chapman {
5330ad66140SJames Chapman 	struct l2tp_session *session = arg;
534ee40fb2eSGuillaume Nault 	struct sock *sk;
5350ad66140SJames Chapman 
536ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
537ee40fb2eSGuillaume Nault 	if (sk) {
538ee40fb2eSGuillaume Nault 		struct pppox_sock *po = pppox_sk(sk);
539ee40fb2eSGuillaume Nault 
5400ad66140SJames Chapman 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
541ee40fb2eSGuillaume Nault 		sock_put(sk);
5420ad66140SJames Chapman 	}
5430ad66140SJames Chapman }
5440ad66140SJames Chapman 
545f98be6c6SGuillaume Nault static void pppol2tp_session_init(struct l2tp_session *session)
546f98be6c6SGuillaume Nault {
547f98be6c6SGuillaume Nault 	struct pppol2tp_session *ps;
548f98be6c6SGuillaume Nault 
549f98be6c6SGuillaume Nault 	session->recv_skb = pppol2tp_recv;
550c2ebc256SArnd Bergmann 	if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
551f98be6c6SGuillaume Nault 		session->show = pppol2tp_show;
552f98be6c6SGuillaume Nault 
553f98be6c6SGuillaume Nault 	ps = l2tp_session_priv(session);
554f98be6c6SGuillaume Nault 	mutex_init(&ps->sk_lock);
555f98be6c6SGuillaume Nault 	ps->owner = current->pid;
556f98be6c6SGuillaume Nault }
557f98be6c6SGuillaume Nault 
558a408194aSGuillaume Nault struct l2tp_connect_info {
559a408194aSGuillaume Nault 	u8 version;
560a408194aSGuillaume Nault 	int fd;
561a408194aSGuillaume Nault 	u32 tunnel_id;
562a408194aSGuillaume Nault 	u32 peer_tunnel_id;
563a408194aSGuillaume Nault 	u32 session_id;
564a408194aSGuillaume Nault 	u32 peer_session_id;
565a408194aSGuillaume Nault };
566a408194aSGuillaume Nault 
567a408194aSGuillaume Nault static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
568a408194aSGuillaume Nault 				      struct l2tp_connect_info *info)
569a408194aSGuillaume Nault {
570a408194aSGuillaume Nault 	switch (sa_len) {
571a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tp):
572a408194aSGuillaume Nault 	{
573a408194aSGuillaume Nault 		const struct sockaddr_pppol2tp *sa_v2in4 = sa;
574a408194aSGuillaume Nault 
575a408194aSGuillaume Nault 		if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
576a408194aSGuillaume Nault 			return -EINVAL;
577a408194aSGuillaume Nault 
578a408194aSGuillaume Nault 		info->version = 2;
579a408194aSGuillaume Nault 		info->fd = sa_v2in4->pppol2tp.fd;
580a408194aSGuillaume Nault 		info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
581a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
582a408194aSGuillaume Nault 		info->session_id = sa_v2in4->pppol2tp.s_session;
583a408194aSGuillaume Nault 		info->peer_session_id = sa_v2in4->pppol2tp.d_session;
584a408194aSGuillaume Nault 
585a408194aSGuillaume Nault 		break;
586a408194aSGuillaume Nault 	}
587a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpv3):
588a408194aSGuillaume Nault 	{
589a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
590a408194aSGuillaume Nault 
591a408194aSGuillaume Nault 		if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
592a408194aSGuillaume Nault 			return -EINVAL;
593a408194aSGuillaume Nault 
594a408194aSGuillaume Nault 		info->version = 3;
595a408194aSGuillaume Nault 		info->fd = sa_v3in4->pppol2tp.fd;
596a408194aSGuillaume Nault 		info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
597a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
598a408194aSGuillaume Nault 		info->session_id = sa_v3in4->pppol2tp.s_session;
599a408194aSGuillaume Nault 		info->peer_session_id = sa_v3in4->pppol2tp.d_session;
600a408194aSGuillaume Nault 
601a408194aSGuillaume Nault 		break;
602a408194aSGuillaume Nault 	}
603a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpin6):
604a408194aSGuillaume Nault 	{
605a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
606a408194aSGuillaume Nault 
607a408194aSGuillaume Nault 		if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
608a408194aSGuillaume Nault 			return -EINVAL;
609a408194aSGuillaume Nault 
610a408194aSGuillaume Nault 		info->version = 2;
611a408194aSGuillaume Nault 		info->fd = sa_v2in6->pppol2tp.fd;
612a408194aSGuillaume Nault 		info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
613a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
614a408194aSGuillaume Nault 		info->session_id = sa_v2in6->pppol2tp.s_session;
615a408194aSGuillaume Nault 		info->peer_session_id = sa_v2in6->pppol2tp.d_session;
616a408194aSGuillaume Nault 
617a408194aSGuillaume Nault 		break;
618a408194aSGuillaume Nault 	}
619a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpv3in6):
620a408194aSGuillaume Nault 	{
621a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
622a408194aSGuillaume Nault 
623a408194aSGuillaume Nault 		if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
624a408194aSGuillaume Nault 			return -EINVAL;
625a408194aSGuillaume Nault 
626a408194aSGuillaume Nault 		info->version = 3;
627a408194aSGuillaume Nault 		info->fd = sa_v3in6->pppol2tp.fd;
628a408194aSGuillaume Nault 		info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
629a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
630a408194aSGuillaume Nault 		info->session_id = sa_v3in6->pppol2tp.s_session;
631a408194aSGuillaume Nault 		info->peer_session_id = sa_v3in6->pppol2tp.d_session;
632a408194aSGuillaume Nault 
633a408194aSGuillaume Nault 		break;
634a408194aSGuillaume Nault 	}
635a408194aSGuillaume Nault 	default:
636a408194aSGuillaume Nault 		return -EINVAL;
637a408194aSGuillaume Nault 	}
638a408194aSGuillaume Nault 
639a408194aSGuillaume Nault 	return 0;
640a408194aSGuillaume Nault }
641a408194aSGuillaume Nault 
642789141b2SGuillaume Nault /* Rough estimation of the maximum payload size a tunnel can transmit without
643789141b2SGuillaume Nault  * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
644789141b2SGuillaume Nault  * numbers and no IP option. Not quite accurate, but the result is mostly
645789141b2SGuillaume Nault  * unused anyway.
646789141b2SGuillaume Nault  */
647789141b2SGuillaume Nault static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
648789141b2SGuillaume Nault {
649789141b2SGuillaume Nault 	int mtu;
650789141b2SGuillaume Nault 
651789141b2SGuillaume Nault 	mtu = l2tp_tunnel_dst_mtu(tunnel);
652789141b2SGuillaume Nault 	if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
653789141b2SGuillaume Nault 		return 1500 - PPPOL2TP_HEADER_OVERHEAD;
654789141b2SGuillaume Nault 
655789141b2SGuillaume Nault 	return mtu - PPPOL2TP_HEADER_OVERHEAD;
656789141b2SGuillaume Nault }
657789141b2SGuillaume Nault 
658fd558d18SJames Chapman /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
659fd558d18SJames Chapman  */
660fd558d18SJames Chapman static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
661fd558d18SJames Chapman 			    int sockaddr_len, int flags)
662fd558d18SJames Chapman {
663fd558d18SJames Chapman 	struct sock *sk = sock->sk;
664fd558d18SJames Chapman 	struct pppox_sock *po = pppox_sk(sk);
665fd558d18SJames Chapman 	struct l2tp_session *session = NULL;
666a408194aSGuillaume Nault 	struct l2tp_connect_info info;
667fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
668fd558d18SJames Chapman 	struct pppol2tp_session *ps;
669fd558d18SJames Chapman 	struct l2tp_session_cfg cfg = { 0, };
670dbdbc73bSGuillaume Nault 	bool drop_refcnt = false;
671f9e56bafSGuillaume Nault 	bool drop_tunnel = false;
672bda06be2SGuillaume Nault 	bool new_session = false;
673bda06be2SGuillaume Nault 	bool new_tunnel = false;
674a408194aSGuillaume Nault 	int error;
675a408194aSGuillaume Nault 
676a408194aSGuillaume Nault 	error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
677a408194aSGuillaume Nault 	if (error < 0)
678a408194aSGuillaume Nault 		return error;
679fd558d18SJames Chapman 
680fd558d18SJames Chapman 	lock_sock(sk);
681fd558d18SJames Chapman 
682fd558d18SJames Chapman 	/* Check for already bound sockets */
683fd558d18SJames Chapman 	error = -EBUSY;
684fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_CONNECTED)
685fd558d18SJames Chapman 		goto end;
686fd558d18SJames Chapman 
687fd558d18SJames Chapman 	/* We don't supporting rebinding anyway */
688fd558d18SJames Chapman 	error = -EALREADY;
689fd558d18SJames Chapman 	if (sk->sk_user_data)
690fd558d18SJames Chapman 		goto end; /* socket is already attached */
691fd558d18SJames Chapman 
692e0d4435fSJames Chapman 	/* Don't bind if tunnel_id is 0 */
693e0d4435fSJames Chapman 	error = -EINVAL;
694a408194aSGuillaume Nault 	if (!info.tunnel_id)
695fd558d18SJames Chapman 		goto end;
696fd558d18SJames Chapman 
697a408194aSGuillaume Nault 	tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
698f9e56bafSGuillaume Nault 	if (tunnel)
699f9e56bafSGuillaume Nault 		drop_tunnel = true;
700309795f4SJames Chapman 
701e0d4435fSJames Chapman 	/* Special case: create tunnel context if session_id and
702e0d4435fSJames Chapman 	 * peer_session_id is 0. Otherwise look up tunnel using supplied
703fd558d18SJames Chapman 	 * tunnel id.
704fd558d18SJames Chapman 	 */
705a408194aSGuillaume Nault 	if (!info.session_id && !info.peer_session_id) {
7060febc7b3STom Parkin 		if (!tunnel) {
707309795f4SJames Chapman 			struct l2tp_tunnel_cfg tcfg = {
708309795f4SJames Chapman 				.encap = L2TP_ENCAPTYPE_UDP,
709309795f4SJames Chapman 				.debug = 0,
710309795f4SJames Chapman 			};
7113e1bc8bfSGuillaume Nault 
7123e1bc8bfSGuillaume Nault 			/* Prevent l2tp_tunnel_register() from trying to set up
7133e1bc8bfSGuillaume Nault 			 * a kernel socket.
7143e1bc8bfSGuillaume Nault 			 */
715a408194aSGuillaume Nault 			if (info.fd < 0) {
7163e1bc8bfSGuillaume Nault 				error = -EBADF;
7173e1bc8bfSGuillaume Nault 				goto end;
7183e1bc8bfSGuillaume Nault 			}
7193e1bc8bfSGuillaume Nault 
720a408194aSGuillaume Nault 			error = l2tp_tunnel_create(sock_net(sk), info.fd,
721a408194aSGuillaume Nault 						   info.version,
722a408194aSGuillaume Nault 						   info.tunnel_id,
723a408194aSGuillaume Nault 						   info.peer_tunnel_id, &tcfg,
724a408194aSGuillaume Nault 						   &tunnel);
725fd558d18SJames Chapman 			if (error < 0)
726fd558d18SJames Chapman 				goto end;
7276b9f3423SGuillaume Nault 
7286b9f3423SGuillaume Nault 			l2tp_tunnel_inc_refcount(tunnel);
7296b9f3423SGuillaume Nault 			error = l2tp_tunnel_register(tunnel, sock_net(sk),
7306b9f3423SGuillaume Nault 						     &tcfg);
7316b9f3423SGuillaume Nault 			if (error < 0) {
7326b9f3423SGuillaume Nault 				kfree(tunnel);
7336b9f3423SGuillaume Nault 				goto end;
7346b9f3423SGuillaume Nault 			}
7356b9f3423SGuillaume Nault 			drop_tunnel = true;
736bda06be2SGuillaume Nault 			new_tunnel = true;
737309795f4SJames Chapman 		}
738fd558d18SJames Chapman 	} else {
739fd558d18SJames Chapman 		/* Error if we can't find the tunnel */
740fd558d18SJames Chapman 		error = -ENOENT;
7410febc7b3STom Parkin 		if (!tunnel)
742fd558d18SJames Chapman 			goto end;
743fd558d18SJames Chapman 
744fd558d18SJames Chapman 		/* Error if socket is not prepped */
7450febc7b3STom Parkin 		if (!tunnel->sock)
746fd558d18SJames Chapman 			goto end;
747fd558d18SJames Chapman 	}
748fd558d18SJames Chapman 
749b79585f5SJames Chapman 	if (tunnel->peer_tunnel_id == 0)
750a408194aSGuillaume Nault 		tunnel->peer_tunnel_id = info.peer_tunnel_id;
751fd558d18SJames Chapman 
75201e28b92SGuillaume Nault 	session = l2tp_tunnel_get_session(tunnel, info.session_id);
753dbdbc73bSGuillaume Nault 	if (session) {
754dbdbc73bSGuillaume Nault 		drop_refcnt = true;
7557ac6ab1fSGuillaume Nault 
7567ac6ab1fSGuillaume Nault 		if (session->pwtype != L2TP_PWTYPE_PPP) {
7577ac6ab1fSGuillaume Nault 			error = -EPROTOTYPE;
7587ac6ab1fSGuillaume Nault 			goto end;
7597ac6ab1fSGuillaume Nault 		}
7607ac6ab1fSGuillaume Nault 
761dbdbc73bSGuillaume Nault 		ps = l2tp_session_priv(session);
762fd558d18SJames Chapman 
763dbdbc73bSGuillaume Nault 		/* Using a pre-existing session is fine as long as it hasn't
764dbdbc73bSGuillaume Nault 		 * been connected yet.
765dbdbc73bSGuillaume Nault 		 */
766ee40fb2eSGuillaume Nault 		mutex_lock(&ps->sk_lock);
767ee40fb2eSGuillaume Nault 		if (rcu_dereference_protected(ps->sk,
7683d609342SGuillaume Nault 					      lockdep_is_held(&ps->sk_lock)) ||
7693d609342SGuillaume Nault 		    ps->__sk) {
770ee40fb2eSGuillaume Nault 			mutex_unlock(&ps->sk_lock);
771dbdbc73bSGuillaume Nault 			error = -EEXIST;
772dbdbc73bSGuillaume Nault 			goto end;
773dbdbc73bSGuillaume Nault 		}
774309795f4SJames Chapman 	} else {
77590904ff5SGuillaume Nault 		cfg.pw_type = L2TP_PWTYPE_PPP;
776fd558d18SJames Chapman 
777dbdbc73bSGuillaume Nault 		session = l2tp_session_create(sizeof(struct pppol2tp_session),
778a408194aSGuillaume Nault 					      tunnel, info.session_id,
779a408194aSGuillaume Nault 					      info.peer_session_id, &cfg);
780dbdbc73bSGuillaume Nault 		if (IS_ERR(session)) {
781dbdbc73bSGuillaume Nault 			error = PTR_ERR(session);
782309795f4SJames Chapman 			goto end;
783309795f4SJames Chapman 		}
7843953ae7bSGuillaume Nault 
785f98be6c6SGuillaume Nault 		pppol2tp_session_init(session);
786ee40fb2eSGuillaume Nault 		ps = l2tp_session_priv(session);
7873953ae7bSGuillaume Nault 		l2tp_session_inc_refcount(session);
788ee40fb2eSGuillaume Nault 
789ee40fb2eSGuillaume Nault 		mutex_lock(&ps->sk_lock);
7903953ae7bSGuillaume Nault 		error = l2tp_session_register(session, tunnel);
7913953ae7bSGuillaume Nault 		if (error < 0) {
792ee40fb2eSGuillaume Nault 			mutex_unlock(&ps->sk_lock);
7933953ae7bSGuillaume Nault 			kfree(session);
7943953ae7bSGuillaume Nault 			goto end;
7953953ae7bSGuillaume Nault 		}
7963953ae7bSGuillaume Nault 		drop_refcnt = true;
797bda06be2SGuillaume Nault 		new_session = true;
798dbdbc73bSGuillaume Nault 	}
799309795f4SJames Chapman 
800fd558d18SJames Chapman 	/* Special case: if source & dest session_id == 0x0000, this
801fd558d18SJames Chapman 	 * socket is being created to manage the tunnel. Just set up
802fd558d18SJames Chapman 	 * the internal context for use by ioctl() and sockopt()
803fd558d18SJames Chapman 	 * handlers.
804fd558d18SJames Chapman 	 */
805*6c0ec37bSTom Parkin 	if (session->session_id == 0 && session->peer_session_id == 0) {
806fd558d18SJames Chapman 		error = 0;
807fd558d18SJames Chapman 		goto out_no_ppp;
808fd558d18SJames Chapman 	}
809fd558d18SJames Chapman 
810fd558d18SJames Chapman 	/* The only header we need to worry about is the L2TP
811fd558d18SJames Chapman 	 * header. This size is different depending on whether
812fd558d18SJames Chapman 	 * sequence numbers are enabled for the data channel.
813fd558d18SJames Chapman 	 */
814fd558d18SJames Chapman 	po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
815fd558d18SJames Chapman 
816fd558d18SJames Chapman 	po->chan.private = sk;
817fd558d18SJames Chapman 	po->chan.ops	 = &pppol2tp_chan_ops;
818789141b2SGuillaume Nault 	po->chan.mtu	 = pppol2tp_tunnel_mtu(tunnel);
819fd558d18SJames Chapman 
820fd558d18SJames Chapman 	error = ppp_register_net_channel(sock_net(sk), &po->chan);
821ee40fb2eSGuillaume Nault 	if (error) {
822ee40fb2eSGuillaume Nault 		mutex_unlock(&ps->sk_lock);
823fd558d18SJames Chapman 		goto end;
824ee40fb2eSGuillaume Nault 	}
825fd558d18SJames Chapman 
826fd558d18SJames Chapman out_no_ppp:
827fd558d18SJames Chapman 	/* This is how we get the session context from the socket. */
828fd558d18SJames Chapman 	sk->sk_user_data = session;
829ee40fb2eSGuillaume Nault 	rcu_assign_pointer(ps->sk, sk);
830ee40fb2eSGuillaume Nault 	mutex_unlock(&ps->sk_lock);
831ee40fb2eSGuillaume Nault 
832f98be6c6SGuillaume Nault 	/* Keep the reference we've grabbed on the session: sk doesn't expect
833f98be6c6SGuillaume Nault 	 * the session to disappear. pppol2tp_session_destruct() is responsible
834f98be6c6SGuillaume Nault 	 * for dropping it.
835f98be6c6SGuillaume Nault 	 */
836f98be6c6SGuillaume Nault 	drop_refcnt = false;
837f98be6c6SGuillaume Nault 
838fd558d18SJames Chapman 	sk->sk_state = PPPOX_CONNECTED;
839fba40c63SAsbjørn Sloth Tønnesen 	l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
840a4ca44faSJoe Perches 		  session->name);
841fd558d18SJames Chapman 
842fd558d18SJames Chapman end:
843bda06be2SGuillaume Nault 	if (error) {
844bda06be2SGuillaume Nault 		if (new_session)
845bda06be2SGuillaume Nault 			l2tp_session_delete(session);
846bda06be2SGuillaume Nault 		if (new_tunnel)
847bda06be2SGuillaume Nault 			l2tp_tunnel_delete(tunnel);
848bda06be2SGuillaume Nault 	}
849dbdbc73bSGuillaume Nault 	if (drop_refcnt)
850dbdbc73bSGuillaume Nault 		l2tp_session_dec_refcount(session);
851f9e56bafSGuillaume Nault 	if (drop_tunnel)
852f9e56bafSGuillaume Nault 		l2tp_tunnel_dec_refcount(tunnel);
853fd558d18SJames Chapman 	release_sock(sk);
854fd558d18SJames Chapman 
855fd558d18SJames Chapman 	return error;
856fd558d18SJames Chapman }
857fd558d18SJames Chapman 
858309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
859309795f4SJames Chapman 
860f026bc29SGuillaume Nault /* Called when creating sessions via the netlink interface. */
861f026bc29SGuillaume Nault static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
862f026bc29SGuillaume Nault 				   u32 session_id, u32 peer_session_id,
863f026bc29SGuillaume Nault 				   struct l2tp_session_cfg *cfg)
864309795f4SJames Chapman {
865309795f4SJames Chapman 	int error;
866309795f4SJames Chapman 	struct l2tp_session *session;
867309795f4SJames Chapman 
868309795f4SJames Chapman 	/* Error if tunnel socket is not prepped */
869f026bc29SGuillaume Nault 	if (!tunnel->sock) {
870f026bc29SGuillaume Nault 		error = -ENOENT;
8713953ae7bSGuillaume Nault 		goto err;
872f026bc29SGuillaume Nault 	}
873309795f4SJames Chapman 
874309795f4SJames Chapman 	/* Allocate and initialize a new session context. */
875309795f4SJames Chapman 	session = l2tp_session_create(sizeof(struct pppol2tp_session),
876309795f4SJames Chapman 				      tunnel, session_id,
877309795f4SJames Chapman 				      peer_session_id, cfg);
878dbdbc73bSGuillaume Nault 	if (IS_ERR(session)) {
879dbdbc73bSGuillaume Nault 		error = PTR_ERR(session);
8803953ae7bSGuillaume Nault 		goto err;
881dbdbc73bSGuillaume Nault 	}
882309795f4SJames Chapman 
883f98be6c6SGuillaume Nault 	pppol2tp_session_init(session);
884309795f4SJames Chapman 
8853953ae7bSGuillaume Nault 	error = l2tp_session_register(session, tunnel);
8863953ae7bSGuillaume Nault 	if (error < 0)
8873953ae7bSGuillaume Nault 		goto err_sess;
888309795f4SJames Chapman 
8893953ae7bSGuillaume Nault 	return 0;
890309795f4SJames Chapman 
8913953ae7bSGuillaume Nault err_sess:
8923953ae7bSGuillaume Nault 	kfree(session);
8933953ae7bSGuillaume Nault err:
894309795f4SJames Chapman 	return error;
895309795f4SJames Chapman }
896309795f4SJames Chapman 
897309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */
898309795f4SJames Chapman 
899fd558d18SJames Chapman /* getname() support.
900fd558d18SJames Chapman  */
901fd558d18SJames Chapman static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
9029b2c45d4SDenys Vlasenko 			    int peer)
903fd558d18SJames Chapman {
904e0d4435fSJames Chapman 	int len = 0;
905fd558d18SJames Chapman 	int error = 0;
906fd558d18SJames Chapman 	struct l2tp_session *session;
907fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
908fd558d18SJames Chapman 	struct sock *sk = sock->sk;
909fd558d18SJames Chapman 	struct inet_sock *inet;
910fd558d18SJames Chapman 	struct pppol2tp_session *pls;
911fd558d18SJames Chapman 
912fd558d18SJames Chapman 	error = -ENOTCONN;
9130febc7b3STom Parkin 	if (!sk)
914fd558d18SJames Chapman 		goto end;
91556cff471SGao Feng 	if (!(sk->sk_state & PPPOX_CONNECTED))
916fd558d18SJames Chapman 		goto end;
917fd558d18SJames Chapman 
918fd558d18SJames Chapman 	error = -EBADF;
919fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
9200febc7b3STom Parkin 	if (!session)
921fd558d18SJames Chapman 		goto end;
922fd558d18SJames Chapman 
923fd558d18SJames Chapman 	pls = l2tp_session_priv(session);
9247198c77aSGuillaume Nault 	tunnel = session->tunnel;
925fd558d18SJames Chapman 
926bbdb32cbSBenjamin LaHaise 	inet = inet_sk(tunnel->sock);
927*6c0ec37bSTom Parkin 	if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET) {
928e0d4435fSJames Chapman 		struct sockaddr_pppol2tp sp;
929b71a61ccSTom Parkin 
930e0d4435fSJames Chapman 		len = sizeof(sp);
931fd558d18SJames Chapman 		memset(&sp, 0, len);
932fd558d18SJames Chapman 		sp.sa_family	= AF_PPPOX;
933fd558d18SJames Chapman 		sp.sa_protocol	= PX_PROTO_OL2TP;
934fd558d18SJames Chapman 		sp.pppol2tp.fd  = tunnel->fd;
935fd558d18SJames Chapman 		sp.pppol2tp.pid = pls->owner;
936fd558d18SJames Chapman 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
937fd558d18SJames Chapman 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
938fd558d18SJames Chapman 		sp.pppol2tp.s_session = session->session_id;
939fd558d18SJames Chapman 		sp.pppol2tp.d_session = session->peer_session_id;
940fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_family = AF_INET;
941fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
942fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
943fd558d18SJames Chapman 		memcpy(uaddr, &sp, len);
944d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
945*6c0ec37bSTom Parkin 	} else if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET6) {
946d2cf3361SBenjamin LaHaise 		struct sockaddr_pppol2tpin6 sp;
947efe4208fSEric Dumazet 
948d2cf3361SBenjamin LaHaise 		len = sizeof(sp);
949d2cf3361SBenjamin LaHaise 		memset(&sp, 0, len);
950d2cf3361SBenjamin LaHaise 		sp.sa_family	= AF_PPPOX;
951d2cf3361SBenjamin LaHaise 		sp.sa_protocol	= PX_PROTO_OL2TP;
952d2cf3361SBenjamin LaHaise 		sp.pppol2tp.fd  = tunnel->fd;
953d2cf3361SBenjamin LaHaise 		sp.pppol2tp.pid = pls->owner;
954d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
955d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
956d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_session = session->session_id;
957d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_session = session->peer_session_id;
958d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_family = AF_INET6;
959d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
960efe4208fSEric Dumazet 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
961efe4208fSEric Dumazet 		       sizeof(tunnel->sock->sk_v6_daddr));
962d2cf3361SBenjamin LaHaise 		memcpy(uaddr, &sp, len);
963*6c0ec37bSTom Parkin 	} else if (tunnel->version == 3 && tunnel->sock->sk_family == AF_INET6) {
964d2cf3361SBenjamin LaHaise 		struct sockaddr_pppol2tpv3in6 sp;
965efe4208fSEric Dumazet 
966d2cf3361SBenjamin LaHaise 		len = sizeof(sp);
967d2cf3361SBenjamin LaHaise 		memset(&sp, 0, len);
968d2cf3361SBenjamin LaHaise 		sp.sa_family	= AF_PPPOX;
969d2cf3361SBenjamin LaHaise 		sp.sa_protocol	= PX_PROTO_OL2TP;
970d2cf3361SBenjamin LaHaise 		sp.pppol2tp.fd  = tunnel->fd;
971d2cf3361SBenjamin LaHaise 		sp.pppol2tp.pid = pls->owner;
972d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
973d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
974d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_session = session->session_id;
975d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_session = session->peer_session_id;
976d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_family = AF_INET6;
977d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
978efe4208fSEric Dumazet 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
979efe4208fSEric Dumazet 		       sizeof(tunnel->sock->sk_v6_daddr));
980d2cf3361SBenjamin LaHaise 		memcpy(uaddr, &sp, len);
981d2cf3361SBenjamin LaHaise #endif
982e0d4435fSJames Chapman 	} else if (tunnel->version == 3) {
983e0d4435fSJames Chapman 		struct sockaddr_pppol2tpv3 sp;
984b71a61ccSTom Parkin 
985e0d4435fSJames Chapman 		len = sizeof(sp);
986e0d4435fSJames Chapman 		memset(&sp, 0, len);
987e0d4435fSJames Chapman 		sp.sa_family	= AF_PPPOX;
988e0d4435fSJames Chapman 		sp.sa_protocol	= PX_PROTO_OL2TP;
989e0d4435fSJames Chapman 		sp.pppol2tp.fd  = tunnel->fd;
990e0d4435fSJames Chapman 		sp.pppol2tp.pid = pls->owner;
991e0d4435fSJames Chapman 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
992e0d4435fSJames Chapman 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
993e0d4435fSJames Chapman 		sp.pppol2tp.s_session = session->session_id;
994e0d4435fSJames Chapman 		sp.pppol2tp.d_session = session->peer_session_id;
995e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_family = AF_INET;
996e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
997e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
998e0d4435fSJames Chapman 		memcpy(uaddr, &sp, len);
999e0d4435fSJames Chapman 	}
1000fd558d18SJames Chapman 
10019b2c45d4SDenys Vlasenko 	error = len;
1002fd558d18SJames Chapman 
1003fd558d18SJames Chapman 	sock_put(sk);
1004fd558d18SJames Chapman end:
1005fd558d18SJames Chapman 	return error;
1006fd558d18SJames Chapman }
1007fd558d18SJames Chapman 
1008fd558d18SJames Chapman /****************************************************************************
1009fd558d18SJames Chapman  * ioctl() handlers.
1010fd558d18SJames Chapman  *
1011fd558d18SJames Chapman  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1012fd558d18SJames Chapman  * sockets. However, in order to control kernel tunnel features, we allow
1013fd558d18SJames Chapman  * userspace to create a special "tunnel" PPPoX socket which is used for
1014fd558d18SJames Chapman  * control only.  Tunnel PPPoX sockets have session_id == 0 and simply allow
1015fd558d18SJames Chapman  * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1016fd558d18SJames Chapman  * calls.
1017fd558d18SJames Chapman  ****************************************************************************/
1018fd558d18SJames Chapman 
1019fd558d18SJames Chapman static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
10207390ed8aSGuillaume Nault 				const struct l2tp_stats *stats)
1021fd558d18SJames Chapman {
10227390ed8aSGuillaume Nault 	memset(dest, 0, sizeof(*dest));
10237390ed8aSGuillaume Nault 
10247b7c0719STom Parkin 	dest->tx_packets = atomic_long_read(&stats->tx_packets);
10257b7c0719STom Parkin 	dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
10267b7c0719STom Parkin 	dest->tx_errors = atomic_long_read(&stats->tx_errors);
10277b7c0719STom Parkin 	dest->rx_packets = atomic_long_read(&stats->rx_packets);
10287b7c0719STom Parkin 	dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
10297b7c0719STom Parkin 	dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
10307b7c0719STom Parkin 	dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
10317b7c0719STom Parkin 	dest->rx_errors = atomic_long_read(&stats->rx_errors);
1032fd558d18SJames Chapman }
1033fd558d18SJames Chapman 
1034528534f0SGuillaume Nault static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
1035528534f0SGuillaume Nault 				      struct l2tp_tunnel *tunnel)
1036528534f0SGuillaume Nault {
1037528534f0SGuillaume Nault 	struct l2tp_session *session;
1038528534f0SGuillaume Nault 
1039528534f0SGuillaume Nault 	if (!stats->session_id) {
1040528534f0SGuillaume Nault 		pppol2tp_copy_stats(stats, &tunnel->stats);
1041528534f0SGuillaume Nault 		return 0;
1042528534f0SGuillaume Nault 	}
1043528534f0SGuillaume Nault 
1044528534f0SGuillaume Nault 	/* If session_id is set, search the corresponding session in the
1045528534f0SGuillaume Nault 	 * context of this tunnel and record the session's statistics.
1046528534f0SGuillaume Nault 	 */
1047528534f0SGuillaume Nault 	session = l2tp_tunnel_get_session(tunnel, stats->session_id);
1048528534f0SGuillaume Nault 	if (!session)
1049528534f0SGuillaume Nault 		return -EBADR;
1050528534f0SGuillaume Nault 
1051528534f0SGuillaume Nault 	if (session->pwtype != L2TP_PWTYPE_PPP) {
1052528534f0SGuillaume Nault 		l2tp_session_dec_refcount(session);
1053528534f0SGuillaume Nault 		return -EBADR;
1054528534f0SGuillaume Nault 	}
1055528534f0SGuillaume Nault 
1056528534f0SGuillaume Nault 	pppol2tp_copy_stats(stats, &session->stats);
1057528534f0SGuillaume Nault 	l2tp_session_dec_refcount(session);
1058528534f0SGuillaume Nault 
1059528534f0SGuillaume Nault 	return 0;
1060528534f0SGuillaume Nault }
1061528534f0SGuillaume Nault 
1062fd558d18SJames Chapman static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1063fd558d18SJames Chapman 			  unsigned long arg)
1064fd558d18SJames Chapman {
1065528534f0SGuillaume Nault 	struct pppol2tp_ioc_stats stats;
1066fd558d18SJames Chapman 	struct l2tp_session *session;
1067fd558d18SJames Chapman 
106879e6760eSGuillaume Nault 	switch (cmd) {
106979e6760eSGuillaume Nault 	case PPPIOCGMRU:
107079e6760eSGuillaume Nault 	case PPPIOCGFLAGS:
1071bdd0292fSGuillaume Nault 		session = sock->sk->sk_user_data;
1072bdd0292fSGuillaume Nault 		if (!session)
1073bdd0292fSGuillaume Nault 			return -ENOTCONN;
1074fd558d18SJames Chapman 
107579e6760eSGuillaume Nault 		/* Not defined for tunnels */
107679e6760eSGuillaume Nault 		if (!session->session_id && !session->peer_session_id)
107779e6760eSGuillaume Nault 			return -ENOSYS;
1078bdd0292fSGuillaume Nault 
107979e6760eSGuillaume Nault 		if (put_user(0, (int __user *)arg))
108079e6760eSGuillaume Nault 			return -EFAULT;
108179e6760eSGuillaume Nault 		break;
108279e6760eSGuillaume Nault 
108379e6760eSGuillaume Nault 	case PPPIOCSMRU:
108479e6760eSGuillaume Nault 	case PPPIOCSFLAGS:
108579e6760eSGuillaume Nault 		session = sock->sk->sk_user_data;
108679e6760eSGuillaume Nault 		if (!session)
108779e6760eSGuillaume Nault 			return -ENOTCONN;
108879e6760eSGuillaume Nault 
108979e6760eSGuillaume Nault 		/* Not defined for tunnels */
109079e6760eSGuillaume Nault 		if (!session->session_id && !session->peer_session_id)
109179e6760eSGuillaume Nault 			return -ENOSYS;
109279e6760eSGuillaume Nault 
1093503c0188SJakub Kicinski 		if (!access_ok((int __user *)arg, sizeof(int)))
109479e6760eSGuillaume Nault 			return -EFAULT;
109579e6760eSGuillaume Nault 		break;
109679e6760eSGuillaume Nault 
109779e6760eSGuillaume Nault 	case PPPIOCGL2TPSTATS:
109879e6760eSGuillaume Nault 		session = sock->sk->sk_user_data;
109979e6760eSGuillaume Nault 		if (!session)
110079e6760eSGuillaume Nault 			return -ENOTCONN;
110179e6760eSGuillaume Nault 
110279e6760eSGuillaume Nault 		/* Session 0 represents the parent tunnel */
1103528534f0SGuillaume Nault 		if (!session->session_id && !session->peer_session_id) {
1104528534f0SGuillaume Nault 			u32 session_id;
1105528534f0SGuillaume Nault 			int err;
1106528534f0SGuillaume Nault 
1107528534f0SGuillaume Nault 			if (copy_from_user(&stats, (void __user *)arg,
1108528534f0SGuillaume Nault 					   sizeof(stats)))
1109528534f0SGuillaume Nault 				return -EFAULT;
1110528534f0SGuillaume Nault 
1111528534f0SGuillaume Nault 			session_id = stats.session_id;
1112528534f0SGuillaume Nault 			err = pppol2tp_tunnel_copy_stats(&stats,
1113528534f0SGuillaume Nault 							 session->tunnel);
1114528534f0SGuillaume Nault 			if (err < 0)
1115528534f0SGuillaume Nault 				return err;
1116528534f0SGuillaume Nault 
1117528534f0SGuillaume Nault 			stats.session_id = session_id;
1118528534f0SGuillaume Nault 		} else {
1119b0e29063SGuillaume Nault 			pppol2tp_copy_stats(&stats, &session->stats);
1120b0e29063SGuillaume Nault 			stats.session_id = session->session_id;
1121528534f0SGuillaume Nault 		}
1122528534f0SGuillaume Nault 		stats.tunnel_id = session->tunnel->tunnel_id;
1123528534f0SGuillaume Nault 		stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
1124528534f0SGuillaume Nault 
1125528534f0SGuillaume Nault 		if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
1126528534f0SGuillaume Nault 			return -EFAULT;
112779e6760eSGuillaume Nault 		break;
112879e6760eSGuillaume Nault 
112979e6760eSGuillaume Nault 	default:
11304f5f85e9SGuillaume Nault 		return -ENOIOCTLCMD;
1131fd558d18SJames Chapman 	}
1132fd558d18SJames Chapman 
113379e6760eSGuillaume Nault 	return 0;
1134fd558d18SJames Chapman }
1135fd558d18SJames Chapman 
1136fd558d18SJames Chapman /*****************************************************************************
1137fd558d18SJames Chapman  * setsockopt() / getsockopt() support.
1138fd558d18SJames Chapman  *
1139fd558d18SJames Chapman  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1140fd558d18SJames Chapman  * sockets. In order to control kernel tunnel features, we allow userspace to
1141fd558d18SJames Chapman  * create a special "tunnel" PPPoX socket which is used for control only.
1142fd558d18SJames Chapman  * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1143fd558d18SJames Chapman  * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1144fd558d18SJames Chapman  *****************************************************************************/
1145fd558d18SJames Chapman 
1146fd558d18SJames Chapman /* Tunnel setsockopt() helper.
1147fd558d18SJames Chapman  */
1148fd558d18SJames Chapman static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1149fd558d18SJames Chapman 				      struct l2tp_tunnel *tunnel,
1150fd558d18SJames Chapman 				      int optname, int val)
1151fd558d18SJames Chapman {
1152fd558d18SJames Chapman 	int err = 0;
1153fd558d18SJames Chapman 
1154fd558d18SJames Chapman 	switch (optname) {
1155fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1156fd558d18SJames Chapman 		tunnel->debug = val;
1157fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1158a4ca44faSJoe Perches 			  tunnel->name, tunnel->debug);
1159fd558d18SJames Chapman 		break;
1160fd558d18SJames Chapman 
1161fd558d18SJames Chapman 	default:
1162fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1163fd558d18SJames Chapman 		break;
1164fd558d18SJames Chapman 	}
1165fd558d18SJames Chapman 
1166fd558d18SJames Chapman 	return err;
1167fd558d18SJames Chapman }
1168fd558d18SJames Chapman 
1169fd558d18SJames Chapman /* Session setsockopt helper.
1170fd558d18SJames Chapman  */
1171fd558d18SJames Chapman static int pppol2tp_session_setsockopt(struct sock *sk,
1172fd558d18SJames Chapman 				       struct l2tp_session *session,
1173fd558d18SJames Chapman 				       int optname, int val)
1174fd558d18SJames Chapman {
1175fd558d18SJames Chapman 	int err = 0;
1176fd558d18SJames Chapman 
1177fd558d18SJames Chapman 	switch (optname) {
1178fd558d18SJames Chapman 	case PPPOL2TP_SO_RECVSEQ:
1179*6c0ec37bSTom Parkin 		if (val != 0 && val != 1) {
1180fd558d18SJames Chapman 			err = -EINVAL;
1181fd558d18SJames Chapman 			break;
1182fd558d18SJames Chapman 		}
11833f9b9770SAsbjørn Sloth Tønnesen 		session->recv_seq = !!val;
1184fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1185a4ca44faSJoe Perches 			  "%s: set recv_seq=%d\n",
1186a4ca44faSJoe Perches 			  session->name, session->recv_seq);
1187fd558d18SJames Chapman 		break;
1188fd558d18SJames Chapman 
1189fd558d18SJames Chapman 	case PPPOL2TP_SO_SENDSEQ:
1190*6c0ec37bSTom Parkin 		if (val != 0 && val != 1) {
1191fd558d18SJames Chapman 			err = -EINVAL;
1192fd558d18SJames Chapman 			break;
1193fd558d18SJames Chapman 		}
11943f9b9770SAsbjørn Sloth Tønnesen 		session->send_seq = !!val;
1195fd558d18SJames Chapman 		{
1196ee40fb2eSGuillaume Nault 			struct pppox_sock *po = pppox_sk(sk);
1197ee40fb2eSGuillaume Nault 
1198fd558d18SJames Chapman 			po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1199fd558d18SJames Chapman 				PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1200fd558d18SJames Chapman 		}
1201bb5016eaSGuillaume Nault 		l2tp_session_set_header_len(session, session->tunnel->version);
1202fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1203a4ca44faSJoe Perches 			  "%s: set send_seq=%d\n",
1204a4ca44faSJoe Perches 			  session->name, session->send_seq);
1205fd558d18SJames Chapman 		break;
1206fd558d18SJames Chapman 
1207fd558d18SJames Chapman 	case PPPOL2TP_SO_LNSMODE:
1208*6c0ec37bSTom Parkin 		if (val != 0 && val != 1) {
1209fd558d18SJames Chapman 			err = -EINVAL;
1210fd558d18SJames Chapman 			break;
1211fd558d18SJames Chapman 		}
12123f9b9770SAsbjørn Sloth Tønnesen 		session->lns_mode = !!val;
1213fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1214a4ca44faSJoe Perches 			  "%s: set lns_mode=%d\n",
1215a4ca44faSJoe Perches 			  session->name, session->lns_mode);
1216fd558d18SJames Chapman 		break;
1217fd558d18SJames Chapman 
1218fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1219fd558d18SJames Chapman 		session->debug = val;
1220fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1221a4ca44faSJoe Perches 			  session->name, session->debug);
1222fd558d18SJames Chapman 		break;
1223fd558d18SJames Chapman 
1224fd558d18SJames Chapman 	case PPPOL2TP_SO_REORDERTO:
1225fd558d18SJames Chapman 		session->reorder_timeout = msecs_to_jiffies(val);
1226fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1227a4ca44faSJoe Perches 			  "%s: set reorder_timeout=%d\n",
1228a4ca44faSJoe Perches 			  session->name, session->reorder_timeout);
1229fd558d18SJames Chapman 		break;
1230fd558d18SJames Chapman 
1231fd558d18SJames Chapman 	default:
1232fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1233fd558d18SJames Chapman 		break;
1234fd558d18SJames Chapman 	}
1235fd558d18SJames Chapman 
1236fd558d18SJames Chapman 	return err;
1237fd558d18SJames Chapman }
1238fd558d18SJames Chapman 
1239fd558d18SJames Chapman /* Main setsockopt() entry point.
1240fd558d18SJames Chapman  * Does API checks, then calls either the tunnel or session setsockopt
1241fd558d18SJames Chapman  * handler, according to whether the PPPoL2TP socket is a for a regular
1242fd558d18SJames Chapman  * session or the special tunnel type.
1243fd558d18SJames Chapman  */
1244fd558d18SJames Chapman static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1245fd558d18SJames Chapman 			       char __user *optval, unsigned int optlen)
1246fd558d18SJames Chapman {
1247fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1248fd558d18SJames Chapman 	struct l2tp_session *session;
1249fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1250fd558d18SJames Chapman 	int val;
1251fd558d18SJames Chapman 	int err;
1252fd558d18SJames Chapman 
1253fd558d18SJames Chapman 	if (level != SOL_PPPOL2TP)
12543cf521f7SSasha Levin 		return -EINVAL;
1255fd558d18SJames Chapman 
1256fd558d18SJames Chapman 	if (optlen < sizeof(int))
1257fd558d18SJames Chapman 		return -EINVAL;
1258fd558d18SJames Chapman 
1259fd558d18SJames Chapman 	if (get_user(val, (int __user *)optval))
1260fd558d18SJames Chapman 		return -EFAULT;
1261fd558d18SJames Chapman 
1262fd558d18SJames Chapman 	err = -ENOTCONN;
12630febc7b3STom Parkin 	if (!sk->sk_user_data)
1264fd558d18SJames Chapman 		goto end;
1265fd558d18SJames Chapman 
1266fd558d18SJames Chapman 	/* Get session context from the socket */
1267fd558d18SJames Chapman 	err = -EBADF;
1268fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
12690febc7b3STom Parkin 	if (!session)
1270fd558d18SJames Chapman 		goto end;
1271fd558d18SJames Chapman 
1272fd558d18SJames Chapman 	/* Special case: if session_id == 0x0000, treat as operation on tunnel
1273fd558d18SJames Chapman 	 */
1274*6c0ec37bSTom Parkin 	if (session->session_id == 0 && session->peer_session_id == 0) {
12757198c77aSGuillaume Nault 		tunnel = session->tunnel;
1276fd558d18SJames Chapman 		err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
12777198c77aSGuillaume Nault 	} else {
1278fd558d18SJames Chapman 		err = pppol2tp_session_setsockopt(sk, session, optname, val);
12797198c77aSGuillaume Nault 	}
1280fd558d18SJames Chapman 
1281fd558d18SJames Chapman 	sock_put(sk);
1282fd558d18SJames Chapman end:
1283fd558d18SJames Chapman 	return err;
1284fd558d18SJames Chapman }
1285fd558d18SJames Chapman 
1286fd558d18SJames Chapman /* Tunnel getsockopt helper. Called with sock locked.
1287fd558d18SJames Chapman  */
1288fd558d18SJames Chapman static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1289fd558d18SJames Chapman 				      struct l2tp_tunnel *tunnel,
1290fd558d18SJames Chapman 				      int optname, int *val)
1291fd558d18SJames Chapman {
1292fd558d18SJames Chapman 	int err = 0;
1293fd558d18SJames Chapman 
1294fd558d18SJames Chapman 	switch (optname) {
1295fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1296fd558d18SJames Chapman 		*val = tunnel->debug;
1297fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
1298a4ca44faSJoe Perches 			  tunnel->name, tunnel->debug);
1299fd558d18SJames Chapman 		break;
1300fd558d18SJames Chapman 
1301fd558d18SJames Chapman 	default:
1302fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1303fd558d18SJames Chapman 		break;
1304fd558d18SJames Chapman 	}
1305fd558d18SJames Chapman 
1306fd558d18SJames Chapman 	return err;
1307fd558d18SJames Chapman }
1308fd558d18SJames Chapman 
1309fd558d18SJames Chapman /* Session getsockopt helper. Called with sock locked.
1310fd558d18SJames Chapman  */
1311fd558d18SJames Chapman static int pppol2tp_session_getsockopt(struct sock *sk,
1312fd558d18SJames Chapman 				       struct l2tp_session *session,
1313fd558d18SJames Chapman 				       int optname, int *val)
1314fd558d18SJames Chapman {
1315fd558d18SJames Chapman 	int err = 0;
1316fd558d18SJames Chapman 
1317fd558d18SJames Chapman 	switch (optname) {
1318fd558d18SJames Chapman 	case PPPOL2TP_SO_RECVSEQ:
1319fd558d18SJames Chapman 		*val = session->recv_seq;
1320fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1321fd558d18SJames Chapman 			  "%s: get recv_seq=%d\n", session->name, *val);
1322fd558d18SJames Chapman 		break;
1323fd558d18SJames Chapman 
1324fd558d18SJames Chapman 	case PPPOL2TP_SO_SENDSEQ:
1325fd558d18SJames Chapman 		*val = session->send_seq;
1326fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1327fd558d18SJames Chapman 			  "%s: get send_seq=%d\n", session->name, *val);
1328fd558d18SJames Chapman 		break;
1329fd558d18SJames Chapman 
1330fd558d18SJames Chapman 	case PPPOL2TP_SO_LNSMODE:
1331fd558d18SJames Chapman 		*val = session->lns_mode;
1332fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1333fd558d18SJames Chapman 			  "%s: get lns_mode=%d\n", session->name, *val);
1334fd558d18SJames Chapman 		break;
1335fd558d18SJames Chapman 
1336fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1337fd558d18SJames Chapman 		*val = session->debug;
1338fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
1339a4ca44faSJoe Perches 			  session->name, *val);
1340fd558d18SJames Chapman 		break;
1341fd558d18SJames Chapman 
1342fd558d18SJames Chapman 	case PPPOL2TP_SO_REORDERTO:
1343fd558d18SJames Chapman 		*val = (int)jiffies_to_msecs(session->reorder_timeout);
1344fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1345fd558d18SJames Chapman 			  "%s: get reorder_timeout=%d\n", session->name, *val);
1346fd558d18SJames Chapman 		break;
1347fd558d18SJames Chapman 
1348fd558d18SJames Chapman 	default:
1349fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1350fd558d18SJames Chapman 	}
1351fd558d18SJames Chapman 
1352fd558d18SJames Chapman 	return err;
1353fd558d18SJames Chapman }
1354fd558d18SJames Chapman 
1355fd558d18SJames Chapman /* Main getsockopt() entry point.
1356fd558d18SJames Chapman  * Does API checks, then calls either the tunnel or session getsockopt
1357fd558d18SJames Chapman  * handler, according to whether the PPPoX socket is a for a regular session
1358fd558d18SJames Chapman  * or the special tunnel type.
1359fd558d18SJames Chapman  */
1360e3192690SJoe Perches static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1361e3192690SJoe Perches 			       char __user *optval, int __user *optlen)
1362fd558d18SJames Chapman {
1363fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1364fd558d18SJames Chapman 	struct l2tp_session *session;
1365fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1366fd558d18SJames Chapman 	int val, len;
1367fd558d18SJames Chapman 	int err;
1368fd558d18SJames Chapman 
1369fd558d18SJames Chapman 	if (level != SOL_PPPOL2TP)
13703cf521f7SSasha Levin 		return -EINVAL;
1371fd558d18SJames Chapman 
1372e3192690SJoe Perches 	if (get_user(len, optlen))
1373fd558d18SJames Chapman 		return -EFAULT;
1374fd558d18SJames Chapman 
1375fd558d18SJames Chapman 	len = min_t(unsigned int, len, sizeof(int));
1376fd558d18SJames Chapman 
1377fd558d18SJames Chapman 	if (len < 0)
1378fd558d18SJames Chapman 		return -EINVAL;
1379fd558d18SJames Chapman 
1380fd558d18SJames Chapman 	err = -ENOTCONN;
13810febc7b3STom Parkin 	if (!sk->sk_user_data)
1382fd558d18SJames Chapman 		goto end;
1383fd558d18SJames Chapman 
1384fd558d18SJames Chapman 	/* Get the session context */
1385fd558d18SJames Chapman 	err = -EBADF;
1386fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
13870febc7b3STom Parkin 	if (!session)
1388fd558d18SJames Chapman 		goto end;
1389fd558d18SJames Chapman 
1390fd558d18SJames Chapman 	/* Special case: if session_id == 0x0000, treat as operation on tunnel */
1391*6c0ec37bSTom Parkin 	if (session->session_id == 0 && session->peer_session_id == 0) {
13927198c77aSGuillaume Nault 		tunnel = session->tunnel;
1393fd558d18SJames Chapman 		err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1394321a52a3SGuillaume Nault 		if (err)
1395321a52a3SGuillaume Nault 			goto end_put_sess;
1396321a52a3SGuillaume Nault 	} else {
1397fd558d18SJames Chapman 		err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1398321a52a3SGuillaume Nault 		if (err)
1399321a52a3SGuillaume Nault 			goto end_put_sess;
1400321a52a3SGuillaume Nault 	}
1401fd558d18SJames Chapman 
1402fd558d18SJames Chapman 	err = -EFAULT;
1403e3192690SJoe Perches 	if (put_user(len, optlen))
1404fd558d18SJames Chapman 		goto end_put_sess;
1405fd558d18SJames Chapman 
1406fd558d18SJames Chapman 	if (copy_to_user((void __user *)optval, &val, len))
1407fd558d18SJames Chapman 		goto end_put_sess;
1408fd558d18SJames Chapman 
1409fd558d18SJames Chapman 	err = 0;
1410fd558d18SJames Chapman 
1411fd558d18SJames Chapman end_put_sess:
1412fd558d18SJames Chapman 	sock_put(sk);
1413fd558d18SJames Chapman end:
1414fd558d18SJames Chapman 	return err;
1415fd558d18SJames Chapman }
1416fd558d18SJames Chapman 
1417fd558d18SJames Chapman /*****************************************************************************
1418fd558d18SJames Chapman  * /proc filesystem for debug
1419f7faffa3SJames Chapman  * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1420f7faffa3SJames Chapman  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1421fd558d18SJames Chapman  *****************************************************************************/
1422fd558d18SJames Chapman 
1423fd558d18SJames Chapman static unsigned int pppol2tp_net_id;
1424fd558d18SJames Chapman 
1425fd558d18SJames Chapman #ifdef CONFIG_PROC_FS
1426fd558d18SJames Chapman 
1427fd558d18SJames Chapman struct pppol2tp_seq_data {
1428fd558d18SJames Chapman 	struct seq_net_private p;
1429fd558d18SJames Chapman 	int tunnel_idx;			/* current tunnel */
1430fd558d18SJames Chapman 	int session_idx;		/* index of session within current tunnel */
1431fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1432fd558d18SJames Chapman 	struct l2tp_session *session;	/* NULL means get next tunnel */
1433fd558d18SJames Chapman };
1434fd558d18SJames Chapman 
1435fd558d18SJames Chapman static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1436fd558d18SJames Chapman {
14370e0c3feeSGuillaume Nault 	/* Drop reference taken during previous invocation */
14380e0c3feeSGuillaume Nault 	if (pd->tunnel)
14390e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
14400e0c3feeSGuillaume Nault 
1441f7faffa3SJames Chapman 	for (;;) {
14420e0c3feeSGuillaume Nault 		pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
1443fd558d18SJames Chapman 		pd->tunnel_idx++;
1444f7faffa3SJames Chapman 
14450e0c3feeSGuillaume Nault 		/* Only accept L2TPv2 tunnels */
14460e0c3feeSGuillaume Nault 		if (!pd->tunnel || pd->tunnel->version == 2)
14470e0c3feeSGuillaume Nault 			return;
1448f7faffa3SJames Chapman 
14490e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
1450f7faffa3SJames Chapman 	}
1451fd558d18SJames Chapman }
1452fd558d18SJames Chapman 
1453fd558d18SJames Chapman static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1454fd558d18SJames Chapman {
145583494407SGuillaume Nault 	/* Drop reference taken during previous invocation */
145683494407SGuillaume Nault 	if (pd->session)
145783494407SGuillaume Nault 		l2tp_session_dec_refcount(pd->session);
145883494407SGuillaume Nault 
1459a4346210SGuillaume Nault 	pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
1460fd558d18SJames Chapman 	pd->session_idx++;
1461f7faffa3SJames Chapman 
14620febc7b3STom Parkin 	if (!pd->session) {
1463fd558d18SJames Chapman 		pd->session_idx = 0;
1464fd558d18SJames Chapman 		pppol2tp_next_tunnel(net, pd);
1465fd558d18SJames Chapman 	}
1466fd558d18SJames Chapman }
1467fd558d18SJames Chapman 
1468fd558d18SJames Chapman static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1469fd558d18SJames Chapman {
1470fd558d18SJames Chapman 	struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1471fd558d18SJames Chapman 	loff_t pos = *offs;
1472fd558d18SJames Chapman 	struct net *net;
1473fd558d18SJames Chapman 
1474fd558d18SJames Chapman 	if (!pos)
1475fd558d18SJames Chapman 		goto out;
1476fd558d18SJames Chapman 
14770febc7b3STom Parkin 	BUG_ON(!m->private);
1478fd558d18SJames Chapman 	pd = m->private;
1479fd558d18SJames Chapman 	net = seq_file_net(m);
1480fd558d18SJames Chapman 
14810febc7b3STom Parkin 	if (!pd->tunnel)
1482fd558d18SJames Chapman 		pppol2tp_next_tunnel(net, pd);
1483fd558d18SJames Chapman 	else
1484fd558d18SJames Chapman 		pppol2tp_next_session(net, pd);
1485fd558d18SJames Chapman 
1486fd558d18SJames Chapman 	/* NULL tunnel and session indicates end of list */
14870febc7b3STom Parkin 	if (!pd->tunnel && !pd->session)
1488fd558d18SJames Chapman 		pd = NULL;
1489fd558d18SJames Chapman 
1490fd558d18SJames Chapman out:
1491fd558d18SJames Chapman 	return pd;
1492fd558d18SJames Chapman }
1493fd558d18SJames Chapman 
1494fd558d18SJames Chapman static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1495fd558d18SJames Chapman {
1496fd558d18SJames Chapman 	(*pos)++;
1497fd558d18SJames Chapman 	return NULL;
1498fd558d18SJames Chapman }
1499fd558d18SJames Chapman 
1500fd558d18SJames Chapman static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1501fd558d18SJames Chapman {
15020e0c3feeSGuillaume Nault 	struct pppol2tp_seq_data *pd = v;
15030e0c3feeSGuillaume Nault 
15040e0c3feeSGuillaume Nault 	if (!pd || pd == SEQ_START_TOKEN)
15050e0c3feeSGuillaume Nault 		return;
15060e0c3feeSGuillaume Nault 
150783494407SGuillaume Nault 	/* Drop reference taken by last invocation of pppol2tp_next_session()
150883494407SGuillaume Nault 	 * or pppol2tp_next_tunnel().
150983494407SGuillaume Nault 	 */
151083494407SGuillaume Nault 	if (pd->session) {
151183494407SGuillaume Nault 		l2tp_session_dec_refcount(pd->session);
151283494407SGuillaume Nault 		pd->session = NULL;
151383494407SGuillaume Nault 	}
15145411b618SGuillaume Nault 	if (pd->tunnel) {
15150e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
15165411b618SGuillaume Nault 		pd->tunnel = NULL;
15175411b618SGuillaume Nault 	}
1518fd558d18SJames Chapman }
1519fd558d18SJames Chapman 
1520fd558d18SJames Chapman static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1521fd558d18SJames Chapman {
1522fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = v;
1523fd558d18SJames Chapman 
1524fd558d18SJames Chapman 	seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1525fd558d18SJames Chapman 		   tunnel->name,
1526fd558d18SJames Chapman 		   (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1527fbea9e07SReshetova, Elena 		   refcount_read(&tunnel->ref_count) - 1);
15287b7c0719STom Parkin 	seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1529fd558d18SJames Chapman 		   tunnel->debug,
15307b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_packets),
15317b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_bytes),
15327b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_errors),
15337b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_packets),
15347b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_bytes),
15357b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_errors));
1536fd558d18SJames Chapman }
1537fd558d18SJames Chapman 
1538fd558d18SJames Chapman static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1539fd558d18SJames Chapman {
1540fd558d18SJames Chapman 	struct l2tp_session *session = v;
1541fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1542ee40fb2eSGuillaume Nault 	unsigned char state;
1543ee40fb2eSGuillaume Nault 	char user_data_ok;
1544ee40fb2eSGuillaume Nault 	struct sock *sk;
1545fd558d18SJames Chapman 	u32 ip = 0;
1546fd558d18SJames Chapman 	u16 port = 0;
1547fd558d18SJames Chapman 
1548fd558d18SJames Chapman 	if (tunnel->sock) {
1549fd558d18SJames Chapman 		struct inet_sock *inet = inet_sk(tunnel->sock);
1550b71a61ccSTom Parkin 
1551fd558d18SJames Chapman 		ip = ntohl(inet->inet_saddr);
1552fd558d18SJames Chapman 		port = ntohs(inet->inet_sport);
1553fd558d18SJames Chapman 	}
1554fd558d18SJames Chapman 
1555ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
1556ee40fb2eSGuillaume Nault 	if (sk) {
1557ee40fb2eSGuillaume Nault 		state = sk->sk_state;
1558ee40fb2eSGuillaume Nault 		user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1559ee40fb2eSGuillaume Nault 	} else {
1560ee40fb2eSGuillaume Nault 		state = 0;
1561ee40fb2eSGuillaume Nault 		user_data_ok = 'N';
1562ee40fb2eSGuillaume Nault 	}
1563ee40fb2eSGuillaume Nault 
15649f7da9a0STom Parkin 	seq_printf(m, "  SESSION '%s' %08X/%d %04X/%04X -> %04X/%04X %d %c\n",
1565fd558d18SJames Chapman 		   session->name, ip, port,
1566fd558d18SJames Chapman 		   tunnel->tunnel_id,
1567fd558d18SJames Chapman 		   session->session_id,
1568fd558d18SJames Chapman 		   tunnel->peer_tunnel_id,
1569fd558d18SJames Chapman 		   session->peer_session_id,
1570ee40fb2eSGuillaume Nault 		   state, user_data_ok);
1571789141b2SGuillaume Nault 	seq_printf(m, "   0/0/%c/%c/%s %08x %u\n",
1572fd558d18SJames Chapman 		   session->recv_seq ? 'R' : '-',
1573fd558d18SJames Chapman 		   session->send_seq ? 'S' : '-',
1574fd558d18SJames Chapman 		   session->lns_mode ? "LNS" : "LAC",
1575fd558d18SJames Chapman 		   session->debug,
1576fd558d18SJames Chapman 		   jiffies_to_msecs(session->reorder_timeout));
15777b7c0719STom Parkin 	seq_printf(m, "   %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
1578fd558d18SJames Chapman 		   session->nr, session->ns,
15797b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_packets),
15807b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_bytes),
15817b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_errors),
15827b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_packets),
15837b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_bytes),
15847b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_errors));
15859345471bSJames Chapman 
1586ee40fb2eSGuillaume Nault 	if (sk) {
1587ee40fb2eSGuillaume Nault 		struct pppox_sock *po = pppox_sk(sk);
1588ee40fb2eSGuillaume Nault 
15899345471bSJames Chapman 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
1590ee40fb2eSGuillaume Nault 		sock_put(sk);
1591ee40fb2eSGuillaume Nault 	}
1592fd558d18SJames Chapman }
1593fd558d18SJames Chapman 
1594fd558d18SJames Chapman static int pppol2tp_seq_show(struct seq_file *m, void *v)
1595fd558d18SJames Chapman {
1596fd558d18SJames Chapman 	struct pppol2tp_seq_data *pd = v;
1597fd558d18SJames Chapman 
1598fd558d18SJames Chapman 	/* display header on line 1 */
1599fd558d18SJames Chapman 	if (v == SEQ_START_TOKEN) {
1600fd558d18SJames Chapman 		seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1601fd558d18SJames Chapman 		seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1602fd558d18SJames Chapman 		seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
16039f7da9a0STom Parkin 		seq_puts(m, "  SESSION name, addr/port src-tid/sid dest-tid/sid state user-data-ok\n");
1604fd558d18SJames Chapman 		seq_puts(m, "   mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1605fd558d18SJames Chapman 		seq_puts(m, "   nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1606fd558d18SJames Chapman 		goto out;
1607fd558d18SJames Chapman 	}
1608fd558d18SJames Chapman 
160983494407SGuillaume Nault 	if (!pd->session)
1610fd558d18SJames Chapman 		pppol2tp_seq_tunnel_show(m, pd->tunnel);
161183494407SGuillaume Nault 	else
1612fd558d18SJames Chapman 		pppol2tp_seq_session_show(m, pd->session);
1613fd558d18SJames Chapman 
1614fd558d18SJames Chapman out:
1615fd558d18SJames Chapman 	return 0;
1616fd558d18SJames Chapman }
1617fd558d18SJames Chapman 
1618fd558d18SJames Chapman static const struct seq_operations pppol2tp_seq_ops = {
1619fd558d18SJames Chapman 	.start		= pppol2tp_seq_start,
1620fd558d18SJames Chapman 	.next		= pppol2tp_seq_next,
1621fd558d18SJames Chapman 	.stop		= pppol2tp_seq_stop,
1622fd558d18SJames Chapman 	.show		= pppol2tp_seq_show,
1623fd558d18SJames Chapman };
1624fd558d18SJames Chapman #endif /* CONFIG_PROC_FS */
1625fd558d18SJames Chapman 
1626fd558d18SJames Chapman /*****************************************************************************
1627fd558d18SJames Chapman  * Network namespace
1628fd558d18SJames Chapman  *****************************************************************************/
1629fd558d18SJames Chapman 
1630fd558d18SJames Chapman static __net_init int pppol2tp_init_net(struct net *net)
1631fd558d18SJames Chapman {
1632fd558d18SJames Chapman 	struct proc_dir_entry *pde;
1633fd558d18SJames Chapman 	int err = 0;
1634fd558d18SJames Chapman 
1635c3506372SChristoph Hellwig 	pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1636c3506372SChristoph Hellwig 			      &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
1637fd558d18SJames Chapman 	if (!pde) {
1638fd558d18SJames Chapman 		err = -ENOMEM;
1639fd558d18SJames Chapman 		goto out;
1640fd558d18SJames Chapman 	}
1641fd558d18SJames Chapman 
1642fd558d18SJames Chapman out:
1643fd558d18SJames Chapman 	return err;
1644fd558d18SJames Chapman }
1645fd558d18SJames Chapman 
1646fd558d18SJames Chapman static __net_exit void pppol2tp_exit_net(struct net *net)
1647fd558d18SJames Chapman {
1648ece31ffdSGao feng 	remove_proc_entry("pppol2tp", net->proc_net);
1649fd558d18SJames Chapman }
1650fd558d18SJames Chapman 
1651fd558d18SJames Chapman static struct pernet_operations pppol2tp_net_ops = {
1652fd558d18SJames Chapman 	.init = pppol2tp_init_net,
1653fd558d18SJames Chapman 	.exit = pppol2tp_exit_net,
1654fd558d18SJames Chapman 	.id   = &pppol2tp_net_id,
1655fd558d18SJames Chapman };
1656fd558d18SJames Chapman 
1657fd558d18SJames Chapman /*****************************************************************************
1658fd558d18SJames Chapman  * Init and cleanup
1659fd558d18SJames Chapman  *****************************************************************************/
1660fd558d18SJames Chapman 
1661fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops = {
1662fd558d18SJames Chapman 	.family		= AF_PPPOX,
1663fd558d18SJames Chapman 	.owner		= THIS_MODULE,
1664fd558d18SJames Chapman 	.release	= pppol2tp_release,
1665fd558d18SJames Chapman 	.bind		= sock_no_bind,
1666fd558d18SJames Chapman 	.connect	= pppol2tp_connect,
1667fd558d18SJames Chapman 	.socketpair	= sock_no_socketpair,
1668fd558d18SJames Chapman 	.accept		= sock_no_accept,
1669fd558d18SJames Chapman 	.getname	= pppol2tp_getname,
1670a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
1671fd558d18SJames Chapman 	.listen		= sock_no_listen,
1672fd558d18SJames Chapman 	.shutdown	= sock_no_shutdown,
1673fd558d18SJames Chapman 	.setsockopt	= pppol2tp_setsockopt,
1674fd558d18SJames Chapman 	.getsockopt	= pppol2tp_getsockopt,
1675fd558d18SJames Chapman 	.sendmsg	= pppol2tp_sendmsg,
1676fd558d18SJames Chapman 	.recvmsg	= pppol2tp_recvmsg,
1677fd558d18SJames Chapman 	.mmap		= sock_no_mmap,
1678fd558d18SJames Chapman 	.ioctl		= pppox_ioctl,
1679055d8824SArnd Bergmann #ifdef CONFIG_COMPAT
1680055d8824SArnd Bergmann 	.compat_ioctl = pppox_compat_ioctl,
1681055d8824SArnd Bergmann #endif
1682fd558d18SJames Chapman };
1683fd558d18SJames Chapman 
1684756e64a0SEric Dumazet static const struct pppox_proto pppol2tp_proto = {
1685fd558d18SJames Chapman 	.create		= pppol2tp_create,
1686e1558a93SWei Yongjun 	.ioctl		= pppol2tp_ioctl,
1687e1558a93SWei Yongjun 	.owner		= THIS_MODULE,
1688fd558d18SJames Chapman };
1689fd558d18SJames Chapman 
1690309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1691309795f4SJames Chapman 
1692309795f4SJames Chapman static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1693309795f4SJames Chapman 	.session_create	= pppol2tp_session_create,
1694cf2f5c88STom Parkin 	.session_delete	= l2tp_session_delete,
1695309795f4SJames Chapman };
1696309795f4SJames Chapman 
1697309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */
1698309795f4SJames Chapman 
1699fd558d18SJames Chapman static int __init pppol2tp_init(void)
1700fd558d18SJames Chapman {
1701fd558d18SJames Chapman 	int err;
1702fd558d18SJames Chapman 
1703fd558d18SJames Chapman 	err = register_pernet_device(&pppol2tp_net_ops);
1704fd558d18SJames Chapman 	if (err)
1705fd558d18SJames Chapman 		goto out;
1706fd558d18SJames Chapman 
1707fd558d18SJames Chapman 	err = proto_register(&pppol2tp_sk_proto, 0);
1708fd558d18SJames Chapman 	if (err)
1709fd558d18SJames Chapman 		goto out_unregister_pppol2tp_pernet;
1710fd558d18SJames Chapman 
1711fd558d18SJames Chapman 	err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1712fd558d18SJames Chapman 	if (err)
1713fd558d18SJames Chapman 		goto out_unregister_pppol2tp_proto;
1714fd558d18SJames Chapman 
1715309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1716309795f4SJames Chapman 	err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1717309795f4SJames Chapman 	if (err)
1718309795f4SJames Chapman 		goto out_unregister_pppox;
1719309795f4SJames Chapman #endif
1720309795f4SJames Chapman 
1721a4ca44faSJoe Perches 	pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1722fd558d18SJames Chapman 
1723fd558d18SJames Chapman out:
1724fd558d18SJames Chapman 	return err;
1725309795f4SJames Chapman 
1726309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1727309795f4SJames Chapman out_unregister_pppox:
1728309795f4SJames Chapman 	unregister_pppox_proto(PX_PROTO_OL2TP);
1729309795f4SJames Chapman #endif
1730fd558d18SJames Chapman out_unregister_pppol2tp_proto:
1731fd558d18SJames Chapman 	proto_unregister(&pppol2tp_sk_proto);
1732fd558d18SJames Chapman out_unregister_pppol2tp_pernet:
1733fd558d18SJames Chapman 	unregister_pernet_device(&pppol2tp_net_ops);
1734fd558d18SJames Chapman 	goto out;
1735fd558d18SJames Chapman }
1736fd558d18SJames Chapman 
1737fd558d18SJames Chapman static void __exit pppol2tp_exit(void)
1738fd558d18SJames Chapman {
1739309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1740309795f4SJames Chapman 	l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1741309795f4SJames Chapman #endif
1742fd558d18SJames Chapman 	unregister_pppox_proto(PX_PROTO_OL2TP);
1743fd558d18SJames Chapman 	proto_unregister(&pppol2tp_sk_proto);
1744fd558d18SJames Chapman 	unregister_pernet_device(&pppol2tp_net_ops);
1745fd558d18SJames Chapman }
1746fd558d18SJames Chapman 
1747fd558d18SJames Chapman module_init(pppol2tp_init);
1748fd558d18SJames Chapman module_exit(pppol2tp_exit);
1749fd558d18SJames Chapman 
1750fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1751fd558d18SJames Chapman MODULE_DESCRIPTION("PPP over L2TP over UDP");
1752fd558d18SJames Chapman MODULE_LICENSE("GPL");
1753fd558d18SJames Chapman MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1754681b4d88SGuillaume Nault MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1755249ee819SGuillaume Nault MODULE_ALIAS_L2TP_PWTYPE(7);
1756