xref: /openbmc/linux/net/l2tp/l2tp_ppp.c (revision b0e29063dcb3bf14f515f95e748b60e4bab45e7c)
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/ip.h>
97fd558d18SJames Chapman #include <net/udp.h>
98cf2f5c88STom Parkin #include <net/inet_common.h>
99fd558d18SJames Chapman 
100fd558d18SJames Chapman #include <asm/byteorder.h>
10160063497SArun Sharma #include <linux/atomic.h>
102fd558d18SJames Chapman 
103fd558d18SJames Chapman #include "l2tp_core.h"
104fd558d18SJames Chapman 
105fd558d18SJames Chapman #define PPPOL2TP_DRV_VERSION	"V2.0"
106fd558d18SJames Chapman 
107fd558d18SJames Chapman /* Space for UDP, L2TP and PPP headers */
108fd558d18SJames Chapman #define PPPOL2TP_HEADER_OVERHEAD	40
109fd558d18SJames Chapman 
110fd558d18SJames Chapman /* Number of bytes to build transmit L2TP headers.
111fd558d18SJames Chapman  * Unfortunately the size is different depending on whether sequence numbers
112fd558d18SJames Chapman  * are enabled.
113fd558d18SJames Chapman  */
114fd558d18SJames Chapman #define PPPOL2TP_L2TP_HDR_SIZE_SEQ		10
115fd558d18SJames Chapman #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ		6
116fd558d18SJames Chapman 
117fd558d18SJames Chapman /* Private data of each session. This data lives at the end of struct
118fd558d18SJames Chapman  * l2tp_session, referenced via session->priv[].
119fd558d18SJames Chapman  */
120fd558d18SJames Chapman struct pppol2tp_session {
121fd558d18SJames Chapman 	int			owner;		/* pid that opened the socket */
122fd558d18SJames Chapman 
123ee40fb2eSGuillaume Nault 	struct mutex		sk_lock;	/* Protects .sk */
124ee40fb2eSGuillaume Nault 	struct sock __rcu	*sk;		/* Pointer to the session
125fd558d18SJames Chapman 						 * PPPoX socket */
126ee40fb2eSGuillaume Nault 	struct sock		*__sk;		/* Copy of .sk, for cleanup */
127ee40fb2eSGuillaume Nault 	struct rcu_head		rcu;		/* For asynchronous release */
128fd558d18SJames Chapman };
129fd558d18SJames Chapman 
130fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
131fd558d18SJames Chapman 
132d7100da0Sstephen hemminger static const struct ppp_channel_ops pppol2tp_chan_ops = {
133d7100da0Sstephen hemminger 	.start_xmit =  pppol2tp_xmit,
134d7100da0Sstephen hemminger };
135d7100da0Sstephen hemminger 
136fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops;
137fd558d18SJames Chapman 
138ee40fb2eSGuillaume Nault /* Retrieves the pppol2tp socket associated to a session.
139ee40fb2eSGuillaume Nault  * A reference is held on the returned socket, so this function must be paired
140ee40fb2eSGuillaume Nault  * with sock_put().
141ee40fb2eSGuillaume Nault  */
142ee40fb2eSGuillaume Nault static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
143ee40fb2eSGuillaume Nault {
144ee40fb2eSGuillaume Nault 	struct pppol2tp_session *ps = l2tp_session_priv(session);
145ee40fb2eSGuillaume Nault 	struct sock *sk;
146ee40fb2eSGuillaume Nault 
147ee40fb2eSGuillaume Nault 	rcu_read_lock();
148ee40fb2eSGuillaume Nault 	sk = rcu_dereference(ps->sk);
149ee40fb2eSGuillaume Nault 	if (sk)
150ee40fb2eSGuillaume Nault 		sock_hold(sk);
151ee40fb2eSGuillaume Nault 	rcu_read_unlock();
152ee40fb2eSGuillaume Nault 
153ee40fb2eSGuillaume Nault 	return sk;
154ee40fb2eSGuillaume Nault }
155ee40fb2eSGuillaume Nault 
156fd558d18SJames Chapman /* Helpers to obtain tunnel/session contexts from sockets.
157fd558d18SJames Chapman  */
158fd558d18SJames Chapman static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
159fd558d18SJames Chapman {
160fd558d18SJames Chapman 	struct l2tp_session *session;
161fd558d18SJames Chapman 
162fd558d18SJames Chapman 	if (sk == NULL)
163fd558d18SJames Chapman 		return NULL;
164fd558d18SJames Chapman 
165fd558d18SJames Chapman 	sock_hold(sk);
166fd558d18SJames Chapman 	session = (struct l2tp_session *)(sk->sk_user_data);
167fd558d18SJames Chapman 	if (session == NULL) {
168fd558d18SJames Chapman 		sock_put(sk);
169fd558d18SJames Chapman 		goto out;
170fd558d18SJames Chapman 	}
171fd558d18SJames Chapman 
172fd558d18SJames Chapman 	BUG_ON(session->magic != L2TP_SESSION_MAGIC);
173fd558d18SJames Chapman 
174fd558d18SJames Chapman out:
175fd558d18SJames Chapman 	return session;
176fd558d18SJames Chapman }
177fd558d18SJames Chapman 
178fd558d18SJames Chapman /*****************************************************************************
179fd558d18SJames Chapman  * Receive data handling
180fd558d18SJames Chapman  *****************************************************************************/
181fd558d18SJames Chapman 
182fd558d18SJames Chapman /* Receive message. This is the recvmsg for the PPPoL2TP socket.
183fd558d18SJames Chapman  */
1841b784140SYing Xue static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
1851b784140SYing Xue 			    size_t len, int flags)
186fd558d18SJames Chapman {
187fd558d18SJames Chapman 	int err;
188fd558d18SJames Chapman 	struct sk_buff *skb;
189fd558d18SJames Chapman 	struct sock *sk = sock->sk;
190fd558d18SJames Chapman 
191fd558d18SJames Chapman 	err = -EIO;
192fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_BOUND)
193fd558d18SJames Chapman 		goto end;
194fd558d18SJames Chapman 
195fd558d18SJames Chapman 	err = 0;
196fd558d18SJames Chapman 	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
197fd558d18SJames Chapman 				flags & MSG_DONTWAIT, &err);
198fd558d18SJames Chapman 	if (!skb)
199fd558d18SJames Chapman 		goto end;
200fd558d18SJames Chapman 
201fd558d18SJames Chapman 	if (len > skb->len)
202fd558d18SJames Chapman 		len = skb->len;
203fd558d18SJames Chapman 	else if (len < skb->len)
204fd558d18SJames Chapman 		msg->msg_flags |= MSG_TRUNC;
205fd558d18SJames Chapman 
20651f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, len);
207fd558d18SJames Chapman 	if (likely(err == 0))
208fd558d18SJames Chapman 		err = len;
209fd558d18SJames Chapman 
210fd558d18SJames Chapman 	kfree_skb(skb);
211fd558d18SJames Chapman end:
212fd558d18SJames Chapman 	return err;
213fd558d18SJames Chapman }
214fd558d18SJames Chapman 
215fd558d18SJames Chapman static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
216fd558d18SJames Chapman {
217fd558d18SJames Chapman 	struct pppol2tp_session *ps = l2tp_session_priv(session);
218fd558d18SJames Chapman 	struct sock *sk = NULL;
219fd558d18SJames Chapman 
220fd558d18SJames Chapman 	/* If the socket is bound, send it in to PPP's input queue. Otherwise
221fd558d18SJames Chapman 	 * queue it on the session socket.
222fd558d18SJames Chapman 	 */
223ee40fb2eSGuillaume Nault 	rcu_read_lock();
224ee40fb2eSGuillaume Nault 	sk = rcu_dereference(ps->sk);
225fd558d18SJames Chapman 	if (sk == NULL)
226fd558d18SJames Chapman 		goto no_sock;
227fd558d18SJames Chapman 
2282b139e6bSGuillaume Nault 	/* If the first two bytes are 0xFF03, consider that it is the PPP's
2292b139e6bSGuillaume Nault 	 * Address and Control fields and skip them. The L2TP module has always
2302b139e6bSGuillaume Nault 	 * worked this way, although, in theory, the use of these fields should
2312b139e6bSGuillaume Nault 	 * be negociated and handled at the PPP layer. These fields are
2322b139e6bSGuillaume Nault 	 * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
2332b139e6bSGuillaume Nault 	 * Information command with Poll/Final bit set to zero (RFC 1662).
2342b139e6bSGuillaume Nault 	 */
2352b139e6bSGuillaume Nault 	if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
2362b139e6bSGuillaume Nault 	    skb->data[1] == PPP_UI)
2372b139e6bSGuillaume Nault 		skb_pull(skb, 2);
2382b139e6bSGuillaume Nault 
239fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_BOUND) {
240fd558d18SJames Chapman 		struct pppox_sock *po;
24198f40b3eSGuillaume Nault 
242fba40c63SAsbjørn Sloth Tønnesen 		l2tp_dbg(session, L2TP_MSG_DATA,
243fd558d18SJames Chapman 			 "%s: recv %d byte data frame, passing to ppp\n",
244fd558d18SJames Chapman 			 session->name, data_len);
245fd558d18SJames Chapman 
246fd558d18SJames Chapman 		po = pppox_sk(sk);
247fd558d18SJames Chapman 		ppp_input(&po->chan, skb);
248fd558d18SJames Chapman 	} else {
249fba40c63SAsbjørn Sloth Tønnesen 		l2tp_dbg(session, L2TP_MSG_DATA,
2509e9cb622SGuillaume Nault 			 "%s: recv %d byte data frame, passing to L2TP socket\n",
2519e9cb622SGuillaume Nault 			 session->name, data_len);
252fd558d18SJames Chapman 
2539e9cb622SGuillaume Nault 		if (sock_queue_rcv_skb(sk, skb) < 0) {
2547b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_errors);
255fd558d18SJames Chapman 			kfree_skb(skb);
256fd558d18SJames Chapman 		}
2579e9cb622SGuillaume Nault 	}
258ee40fb2eSGuillaume Nault 	rcu_read_unlock();
259fd558d18SJames Chapman 
260fd558d18SJames Chapman 	return;
261fd558d18SJames Chapman 
262fd558d18SJames Chapman no_sock:
263ee40fb2eSGuillaume Nault 	rcu_read_unlock();
264fba40c63SAsbjørn Sloth Tønnesen 	l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
265fd558d18SJames Chapman 	kfree_skb(skb);
266fd558d18SJames Chapman }
267fd558d18SJames Chapman 
268fd558d18SJames Chapman /************************************************************************
269fd558d18SJames Chapman  * Transmit handling
270fd558d18SJames Chapman  ***********************************************************************/
271fd558d18SJames Chapman 
272fd558d18SJames Chapman /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket.  We come here
273fd558d18SJames Chapman  * when a user application does a sendmsg() on the session socket. L2TP and
274fd558d18SJames Chapman  * PPP headers must be inserted into the user's data.
275fd558d18SJames Chapman  */
2761b784140SYing Xue static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
277fd558d18SJames Chapman 			    size_t total_len)
278fd558d18SJames Chapman {
279fd558d18SJames Chapman 	struct sock *sk = sock->sk;
280fd558d18SJames Chapman 	struct sk_buff *skb;
281fd558d18SJames Chapman 	int error;
282fd558d18SJames Chapman 	struct l2tp_session *session;
283fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
2840d76751fSJames Chapman 	int uhlen;
285fd558d18SJames Chapman 
286fd558d18SJames Chapman 	error = -ENOTCONN;
287fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
288fd558d18SJames Chapman 		goto error;
289fd558d18SJames Chapman 
290fd558d18SJames Chapman 	/* Get session and tunnel contexts */
291fd558d18SJames Chapman 	error = -EBADF;
292fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
293fd558d18SJames Chapman 	if (session == NULL)
294fd558d18SJames Chapman 		goto error;
295fd558d18SJames Chapman 
2967198c77aSGuillaume Nault 	tunnel = session->tunnel;
297fd558d18SJames Chapman 
2980d76751fSJames Chapman 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
2990d76751fSJames Chapman 
300fd558d18SJames Chapman 	/* Allocate a socket buffer */
301fd558d18SJames Chapman 	error = -ENOMEM;
302fd558d18SJames Chapman 	skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
3030d76751fSJames Chapman 			   uhlen + session->hdr_len +
30454c151d9SGao Feng 			   2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
305fd558d18SJames Chapman 			   0, GFP_KERNEL);
306fd558d18SJames Chapman 	if (!skb)
3077198c77aSGuillaume Nault 		goto error_put_sess;
308fd558d18SJames Chapman 
309fd558d18SJames Chapman 	/* Reserve space for headers. */
310fd558d18SJames Chapman 	skb_reserve(skb, NET_SKB_PAD);
311fd558d18SJames Chapman 	skb_reset_network_header(skb);
312fd558d18SJames Chapman 	skb_reserve(skb, sizeof(struct iphdr));
313fd558d18SJames Chapman 	skb_reset_transport_header(skb);
3140d76751fSJames Chapman 	skb_reserve(skb, uhlen);
315fd558d18SJames Chapman 
316fd558d18SJames Chapman 	/* Add PPP header */
31754c151d9SGao Feng 	skb->data[0] = PPP_ALLSTATIONS;
31854c151d9SGao Feng 	skb->data[1] = PPP_UI;
319fd558d18SJames Chapman 	skb_put(skb, 2);
320fd558d18SJames Chapman 
321fd558d18SJames Chapman 	/* Copy user data into skb */
3226ce8e9ceSAl Viro 	error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
323fd558d18SJames Chapman 	if (error < 0) {
324fd558d18SJames Chapman 		kfree_skb(skb);
3257198c77aSGuillaume Nault 		goto error_put_sess;
326fd558d18SJames Chapman 	}
327fd558d18SJames Chapman 
328455cc32bSEric Dumazet 	local_bh_disable();
329fd558d18SJames Chapman 	l2tp_xmit_skb(session, skb, session->hdr_len);
330455cc32bSEric Dumazet 	local_bh_enable();
331fd558d18SJames Chapman 
3328b82547eSGuillaume Nault 	sock_put(sk);
333fd558d18SJames Chapman 
334a6f79d0fSGuillaume Nault 	return total_len;
335fd558d18SJames Chapman 
336fd558d18SJames Chapman error_put_sess:
337fd558d18SJames Chapman 	sock_put(sk);
338fd558d18SJames Chapman error:
339fd558d18SJames Chapman 	return error;
340fd558d18SJames Chapman }
341fd558d18SJames Chapman 
342fd558d18SJames Chapman /* Transmit function called by generic PPP driver.  Sends PPP frame
343fd558d18SJames Chapman  * over PPPoL2TP socket.
344fd558d18SJames Chapman  *
345fd558d18SJames Chapman  * This is almost the same as pppol2tp_sendmsg(), but rather than
346fd558d18SJames Chapman  * being called with a msghdr from userspace, it is called with a skb
347fd558d18SJames Chapman  * from the kernel.
348fd558d18SJames Chapman  *
349fd558d18SJames Chapman  * The supplied skb from ppp doesn't have enough headroom for the
350fd558d18SJames Chapman  * insertion of L2TP, UDP and IP headers so we need to allocate more
351fd558d18SJames Chapman  * headroom in the skb. This will create a cloned skb. But we must be
352fd558d18SJames Chapman  * careful in the error case because the caller will expect to free
353fd558d18SJames Chapman  * the skb it supplied, not our cloned skb. So we take care to always
354fd558d18SJames Chapman  * leave the original skb unfreed if we return an error.
355fd558d18SJames Chapman  */
356fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
357fd558d18SJames Chapman {
358fd558d18SJames Chapman 	struct sock *sk = (struct sock *) chan->private;
359fd558d18SJames Chapman 	struct l2tp_session *session;
360fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
36109df57caSEric Dumazet 	int uhlen, headroom;
362fd558d18SJames Chapman 
363fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
364fd558d18SJames Chapman 		goto abort;
365fd558d18SJames Chapman 
366fd558d18SJames Chapman 	/* Get session and tunnel contexts from the socket */
367fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
368fd558d18SJames Chapman 	if (session == NULL)
369fd558d18SJames Chapman 		goto abort;
370fd558d18SJames Chapman 
3717198c77aSGuillaume Nault 	tunnel = session->tunnel;
372fd558d18SJames Chapman 
37309df57caSEric Dumazet 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
37409df57caSEric Dumazet 	headroom = NET_SKB_PAD +
37509df57caSEric Dumazet 		   sizeof(struct iphdr) + /* IP header */
37609df57caSEric Dumazet 		   uhlen +		/* UDP header (if L2TP_ENCAPTYPE_UDP) */
37709df57caSEric Dumazet 		   session->hdr_len +	/* L2TP header */
37854c151d9SGao Feng 		   2;			/* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
37909df57caSEric Dumazet 	if (skb_cow_head(skb, headroom))
3807198c77aSGuillaume Nault 		goto abort_put_sess;
381fd558d18SJames Chapman 
382fd558d18SJames Chapman 	/* Setup PPP header */
38354c151d9SGao Feng 	__skb_push(skb, 2);
38454c151d9SGao Feng 	skb->data[0] = PPP_ALLSTATIONS;
38554c151d9SGao Feng 	skb->data[1] = PPP_UI;
386fd558d18SJames Chapman 
387455cc32bSEric Dumazet 	local_bh_disable();
388e0d4435fSJames Chapman 	l2tp_xmit_skb(session, skb, session->hdr_len);
389455cc32bSEric Dumazet 	local_bh_enable();
390fd558d18SJames Chapman 
391fd558d18SJames Chapman 	sock_put(sk);
3927198c77aSGuillaume Nault 
393fd558d18SJames Chapman 	return 1;
394fd558d18SJames Chapman 
395fd558d18SJames Chapman abort_put_sess:
396fd558d18SJames Chapman 	sock_put(sk);
397fd558d18SJames Chapman abort:
398fd558d18SJames Chapman 	/* Free the original skb */
399fd558d18SJames Chapman 	kfree_skb(skb);
400fd558d18SJames Chapman 	return 1;
401fd558d18SJames Chapman }
402fd558d18SJames Chapman 
403fd558d18SJames Chapman /*****************************************************************************
404fd558d18SJames Chapman  * Session (and tunnel control) socket create/destroy.
405fd558d18SJames Chapman  *****************************************************************************/
406fd558d18SJames Chapman 
407d02ba2a6SJames Chapman static void pppol2tp_put_sk(struct rcu_head *head)
408d02ba2a6SJames Chapman {
409d02ba2a6SJames Chapman 	struct pppol2tp_session *ps;
410d02ba2a6SJames Chapman 
411d02ba2a6SJames Chapman 	ps = container_of(head, typeof(*ps), rcu);
412d02ba2a6SJames Chapman 	sock_put(ps->__sk);
413d02ba2a6SJames Chapman }
414d02ba2a6SJames Chapman 
415fd558d18SJames Chapman /* Really kill the session socket. (Called from sock_put() if
416fd558d18SJames Chapman  * refcnt == 0.)
417fd558d18SJames Chapman  */
418fd558d18SJames Chapman static void pppol2tp_session_destruct(struct sock *sk)
419fd558d18SJames Chapman {
420f6e16b29STom Parkin 	struct l2tp_session *session = sk->sk_user_data;
421e91793bbSGuillaume Nault 
422e91793bbSGuillaume Nault 	skb_queue_purge(&sk->sk_receive_queue);
423e91793bbSGuillaume Nault 	skb_queue_purge(&sk->sk_write_queue);
424e91793bbSGuillaume Nault 
425f6e16b29STom Parkin 	if (session) {
426fd558d18SJames Chapman 		sk->sk_user_data = NULL;
427fd558d18SJames Chapman 		BUG_ON(session->magic != L2TP_SESSION_MAGIC);
428fd558d18SJames Chapman 		l2tp_session_dec_refcount(session);
429fd558d18SJames Chapman 	}
430fd558d18SJames Chapman }
431fd558d18SJames Chapman 
432fd558d18SJames Chapman /* Called when the PPPoX socket (session) is closed.
433fd558d18SJames Chapman  */
434fd558d18SJames Chapman static int pppol2tp_release(struct socket *sock)
435fd558d18SJames Chapman {
436fd558d18SJames Chapman 	struct sock *sk = sock->sk;
437fd558d18SJames Chapman 	struct l2tp_session *session;
438fd558d18SJames Chapman 	int error;
439fd558d18SJames Chapman 
440fd558d18SJames Chapman 	if (!sk)
441fd558d18SJames Chapman 		return 0;
442fd558d18SJames Chapman 
443fd558d18SJames Chapman 	error = -EBADF;
444fd558d18SJames Chapman 	lock_sock(sk);
445fd558d18SJames Chapman 	if (sock_flag(sk, SOCK_DEAD) != 0)
446fd558d18SJames Chapman 		goto error;
447fd558d18SJames Chapman 
448fd558d18SJames Chapman 	pppox_unbind_sock(sk);
449fd558d18SJames Chapman 
450fd558d18SJames Chapman 	/* Signal the death of the socket. */
451fd558d18SJames Chapman 	sk->sk_state = PPPOX_DEAD;
452fd558d18SJames Chapman 	sock_orphan(sk);
453fd558d18SJames Chapman 	sock->sk = NULL;
454fd558d18SJames Chapman 
455d02ba2a6SJames Chapman 	session = pppol2tp_sock_to_session(sk);
456d02ba2a6SJames Chapman 	if (session) {
4573d609342SGuillaume Nault 		struct pppol2tp_session *ps;
4583d609342SGuillaume Nault 
459d02ba2a6SJames Chapman 		l2tp_session_delete(session);
4603d609342SGuillaume Nault 
4613d609342SGuillaume Nault 		ps = l2tp_session_priv(session);
4623d609342SGuillaume Nault 		mutex_lock(&ps->sk_lock);
4633d609342SGuillaume Nault 		ps->__sk = rcu_dereference_protected(ps->sk,
4643d609342SGuillaume Nault 						     lockdep_is_held(&ps->sk_lock));
4653d609342SGuillaume Nault 		RCU_INIT_POINTER(ps->sk, NULL);
4663d609342SGuillaume Nault 		mutex_unlock(&ps->sk_lock);
4673d609342SGuillaume Nault 		call_rcu(&ps->rcu, pppol2tp_put_sk);
4683d609342SGuillaume Nault 
4693d609342SGuillaume Nault 		/* Rely on the sock_put() call at the end of the function for
4703d609342SGuillaume Nault 		 * dropping the reference held by pppol2tp_sock_to_session().
4713d609342SGuillaume Nault 		 * The last reference will be dropped by pppol2tp_put_sk().
4723d609342SGuillaume Nault 		 */
473cf2f5c88STom Parkin 	}
474d02ba2a6SJames Chapman 
475fd558d18SJames Chapman 	release_sock(sk);
476fd558d18SJames Chapman 
477fd558d18SJames Chapman 	/* This will delete the session context via
478fd558d18SJames Chapman 	 * pppol2tp_session_destruct() if the socket's refcnt drops to
479fd558d18SJames Chapman 	 * zero.
480fd558d18SJames Chapman 	 */
481fd558d18SJames Chapman 	sock_put(sk);
482fd558d18SJames Chapman 
483fd558d18SJames Chapman 	return 0;
484fd558d18SJames Chapman 
485fd558d18SJames Chapman error:
486fd558d18SJames Chapman 	release_sock(sk);
487fd558d18SJames Chapman 	return error;
488fd558d18SJames Chapman }
489fd558d18SJames Chapman 
490fd558d18SJames Chapman static struct proto pppol2tp_sk_proto = {
491fd558d18SJames Chapman 	.name	  = "PPPOL2TP",
492fd558d18SJames Chapman 	.owner	  = THIS_MODULE,
493fd558d18SJames Chapman 	.obj_size = sizeof(struct pppox_sock),
494fd558d18SJames Chapman };
495fd558d18SJames Chapman 
496fd558d18SJames Chapman static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
497fd558d18SJames Chapman {
498fd558d18SJames Chapman 	int rc;
499fd558d18SJames Chapman 
500fd558d18SJames Chapman 	rc = l2tp_udp_encap_recv(sk, skb);
501fd558d18SJames Chapman 	if (rc)
502fd558d18SJames Chapman 		kfree_skb(skb);
503fd558d18SJames Chapman 
504fd558d18SJames Chapman 	return NET_RX_SUCCESS;
505fd558d18SJames Chapman }
506fd558d18SJames Chapman 
507fd558d18SJames Chapman /* socket() handler. Initialize a new struct sock.
508fd558d18SJames Chapman  */
50911aa9c28SEric W. Biederman static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
510fd558d18SJames Chapman {
511fd558d18SJames Chapman 	int error = -ENOMEM;
512fd558d18SJames Chapman 	struct sock *sk;
513fd558d18SJames Chapman 
51411aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
515fd558d18SJames Chapman 	if (!sk)
516fd558d18SJames Chapman 		goto out;
517fd558d18SJames Chapman 
518fd558d18SJames Chapman 	sock_init_data(sock, sk);
519fd558d18SJames Chapman 
520fd558d18SJames Chapman 	sock->state  = SS_UNCONNECTED;
521fd558d18SJames Chapman 	sock->ops    = &pppol2tp_ops;
522fd558d18SJames Chapman 
523fd558d18SJames Chapman 	sk->sk_backlog_rcv = pppol2tp_backlog_recv;
524fd558d18SJames Chapman 	sk->sk_protocol	   = PX_PROTO_OL2TP;
525fd558d18SJames Chapman 	sk->sk_family	   = PF_PPPOX;
526fd558d18SJames Chapman 	sk->sk_state	   = PPPOX_NONE;
527fd558d18SJames Chapman 	sk->sk_type	   = SOCK_STREAM;
528fd558d18SJames Chapman 	sk->sk_destruct	   = pppol2tp_session_destruct;
529fd558d18SJames Chapman 
530fd558d18SJames Chapman 	error = 0;
531fd558d18SJames Chapman 
532fd558d18SJames Chapman out:
533fd558d18SJames Chapman 	return error;
534fd558d18SJames Chapman }
535fd558d18SJames Chapman 
5369dd79945SJavier Martinez Canillas #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
5370ad66140SJames Chapman static void pppol2tp_show(struct seq_file *m, void *arg)
5380ad66140SJames Chapman {
5390ad66140SJames Chapman 	struct l2tp_session *session = arg;
540ee40fb2eSGuillaume Nault 	struct sock *sk;
5410ad66140SJames Chapman 
542ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
543ee40fb2eSGuillaume Nault 	if (sk) {
544ee40fb2eSGuillaume Nault 		struct pppox_sock *po = pppox_sk(sk);
545ee40fb2eSGuillaume Nault 
5460ad66140SJames Chapman 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
547ee40fb2eSGuillaume Nault 		sock_put(sk);
5480ad66140SJames Chapman 	}
5490ad66140SJames Chapman }
5500ad66140SJames Chapman #endif
5510ad66140SJames Chapman 
552f98be6c6SGuillaume Nault static void pppol2tp_session_init(struct l2tp_session *session)
553f98be6c6SGuillaume Nault {
554f98be6c6SGuillaume Nault 	struct pppol2tp_session *ps;
555f98be6c6SGuillaume Nault 
556f98be6c6SGuillaume Nault 	session->recv_skb = pppol2tp_recv;
557f98be6c6SGuillaume Nault #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
558f98be6c6SGuillaume Nault 	session->show = pppol2tp_show;
559f98be6c6SGuillaume Nault #endif
560f98be6c6SGuillaume Nault 
561f98be6c6SGuillaume Nault 	ps = l2tp_session_priv(session);
562f98be6c6SGuillaume Nault 	mutex_init(&ps->sk_lock);
563f98be6c6SGuillaume Nault 	ps->owner = current->pid;
564f98be6c6SGuillaume Nault }
565f98be6c6SGuillaume Nault 
566a408194aSGuillaume Nault struct l2tp_connect_info {
567a408194aSGuillaume Nault 	u8 version;
568a408194aSGuillaume Nault 	int fd;
569a408194aSGuillaume Nault 	u32 tunnel_id;
570a408194aSGuillaume Nault 	u32 peer_tunnel_id;
571a408194aSGuillaume Nault 	u32 session_id;
572a408194aSGuillaume Nault 	u32 peer_session_id;
573a408194aSGuillaume Nault };
574a408194aSGuillaume Nault 
575a408194aSGuillaume Nault static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
576a408194aSGuillaume Nault 				      struct l2tp_connect_info *info)
577a408194aSGuillaume Nault {
578a408194aSGuillaume Nault 	switch (sa_len) {
579a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tp):
580a408194aSGuillaume Nault 	{
581a408194aSGuillaume Nault 		const struct sockaddr_pppol2tp *sa_v2in4 = sa;
582a408194aSGuillaume Nault 
583a408194aSGuillaume Nault 		if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
584a408194aSGuillaume Nault 			return -EINVAL;
585a408194aSGuillaume Nault 
586a408194aSGuillaume Nault 		info->version = 2;
587a408194aSGuillaume Nault 		info->fd = sa_v2in4->pppol2tp.fd;
588a408194aSGuillaume Nault 		info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
589a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
590a408194aSGuillaume Nault 		info->session_id = sa_v2in4->pppol2tp.s_session;
591a408194aSGuillaume Nault 		info->peer_session_id = sa_v2in4->pppol2tp.d_session;
592a408194aSGuillaume Nault 
593a408194aSGuillaume Nault 		break;
594a408194aSGuillaume Nault 	}
595a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpv3):
596a408194aSGuillaume Nault 	{
597a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
598a408194aSGuillaume Nault 
599a408194aSGuillaume Nault 		if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
600a408194aSGuillaume Nault 			return -EINVAL;
601a408194aSGuillaume Nault 
602a408194aSGuillaume Nault 		info->version = 3;
603a408194aSGuillaume Nault 		info->fd = sa_v3in4->pppol2tp.fd;
604a408194aSGuillaume Nault 		info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
605a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
606a408194aSGuillaume Nault 		info->session_id = sa_v3in4->pppol2tp.s_session;
607a408194aSGuillaume Nault 		info->peer_session_id = sa_v3in4->pppol2tp.d_session;
608a408194aSGuillaume Nault 
609a408194aSGuillaume Nault 		break;
610a408194aSGuillaume Nault 	}
611a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpin6):
612a408194aSGuillaume Nault 	{
613a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
614a408194aSGuillaume Nault 
615a408194aSGuillaume Nault 		if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
616a408194aSGuillaume Nault 			return -EINVAL;
617a408194aSGuillaume Nault 
618a408194aSGuillaume Nault 		info->version = 2;
619a408194aSGuillaume Nault 		info->fd = sa_v2in6->pppol2tp.fd;
620a408194aSGuillaume Nault 		info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
621a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
622a408194aSGuillaume Nault 		info->session_id = sa_v2in6->pppol2tp.s_session;
623a408194aSGuillaume Nault 		info->peer_session_id = sa_v2in6->pppol2tp.d_session;
624a408194aSGuillaume Nault 
625a408194aSGuillaume Nault 		break;
626a408194aSGuillaume Nault 	}
627a408194aSGuillaume Nault 	case sizeof(struct sockaddr_pppol2tpv3in6):
628a408194aSGuillaume Nault 	{
629a408194aSGuillaume Nault 		const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
630a408194aSGuillaume Nault 
631a408194aSGuillaume Nault 		if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
632a408194aSGuillaume Nault 			return -EINVAL;
633a408194aSGuillaume Nault 
634a408194aSGuillaume Nault 		info->version = 3;
635a408194aSGuillaume Nault 		info->fd = sa_v3in6->pppol2tp.fd;
636a408194aSGuillaume Nault 		info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
637a408194aSGuillaume Nault 		info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
638a408194aSGuillaume Nault 		info->session_id = sa_v3in6->pppol2tp.s_session;
639a408194aSGuillaume Nault 		info->peer_session_id = sa_v3in6->pppol2tp.d_session;
640a408194aSGuillaume Nault 
641a408194aSGuillaume Nault 		break;
642a408194aSGuillaume Nault 	}
643a408194aSGuillaume Nault 	default:
644a408194aSGuillaume Nault 		return -EINVAL;
645a408194aSGuillaume Nault 	}
646a408194aSGuillaume Nault 
647a408194aSGuillaume Nault 	return 0;
648a408194aSGuillaume Nault }
649a408194aSGuillaume Nault 
650789141b2SGuillaume Nault /* Rough estimation of the maximum payload size a tunnel can transmit without
651789141b2SGuillaume Nault  * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
652789141b2SGuillaume Nault  * numbers and no IP option. Not quite accurate, but the result is mostly
653789141b2SGuillaume Nault  * unused anyway.
654789141b2SGuillaume Nault  */
655789141b2SGuillaume Nault static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
656789141b2SGuillaume Nault {
657789141b2SGuillaume Nault 	int mtu;
658789141b2SGuillaume Nault 
659789141b2SGuillaume Nault 	mtu = l2tp_tunnel_dst_mtu(tunnel);
660789141b2SGuillaume Nault 	if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
661789141b2SGuillaume Nault 		return 1500 - PPPOL2TP_HEADER_OVERHEAD;
662789141b2SGuillaume Nault 
663789141b2SGuillaume Nault 	return mtu - PPPOL2TP_HEADER_OVERHEAD;
664789141b2SGuillaume Nault }
665789141b2SGuillaume Nault 
666fd558d18SJames Chapman /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
667fd558d18SJames Chapman  */
668fd558d18SJames Chapman static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
669fd558d18SJames Chapman 			    int sockaddr_len, int flags)
670fd558d18SJames Chapman {
671fd558d18SJames Chapman 	struct sock *sk = sock->sk;
672fd558d18SJames Chapman 	struct pppox_sock *po = pppox_sk(sk);
673fd558d18SJames Chapman 	struct l2tp_session *session = NULL;
674a408194aSGuillaume Nault 	struct l2tp_connect_info info;
675fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
676fd558d18SJames Chapman 	struct pppol2tp_session *ps;
677fd558d18SJames Chapman 	struct l2tp_session_cfg cfg = { 0, };
678dbdbc73bSGuillaume Nault 	bool drop_refcnt = false;
679f9e56bafSGuillaume Nault 	bool drop_tunnel = false;
680bda06be2SGuillaume Nault 	bool new_session = false;
681bda06be2SGuillaume Nault 	bool new_tunnel = false;
682a408194aSGuillaume Nault 	int error;
683a408194aSGuillaume Nault 
684a408194aSGuillaume Nault 	error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
685a408194aSGuillaume Nault 	if (error < 0)
686a408194aSGuillaume Nault 		return error;
687fd558d18SJames Chapman 
688fd558d18SJames Chapman 	lock_sock(sk);
689fd558d18SJames Chapman 
690fd558d18SJames Chapman 	/* Check for already bound sockets */
691fd558d18SJames Chapman 	error = -EBUSY;
692fd558d18SJames Chapman 	if (sk->sk_state & PPPOX_CONNECTED)
693fd558d18SJames Chapman 		goto end;
694fd558d18SJames Chapman 
695fd558d18SJames Chapman 	/* We don't supporting rebinding anyway */
696fd558d18SJames Chapman 	error = -EALREADY;
697fd558d18SJames Chapman 	if (sk->sk_user_data)
698fd558d18SJames Chapman 		goto end; /* socket is already attached */
699fd558d18SJames Chapman 
700e0d4435fSJames Chapman 	/* Don't bind if tunnel_id is 0 */
701e0d4435fSJames Chapman 	error = -EINVAL;
702a408194aSGuillaume Nault 	if (!info.tunnel_id)
703fd558d18SJames Chapman 		goto end;
704fd558d18SJames Chapman 
705a408194aSGuillaume Nault 	tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
706f9e56bafSGuillaume Nault 	if (tunnel)
707f9e56bafSGuillaume Nault 		drop_tunnel = true;
708309795f4SJames Chapman 
709e0d4435fSJames Chapman 	/* Special case: create tunnel context if session_id and
710e0d4435fSJames Chapman 	 * peer_session_id is 0. Otherwise look up tunnel using supplied
711fd558d18SJames Chapman 	 * tunnel id.
712fd558d18SJames Chapman 	 */
713a408194aSGuillaume Nault 	if (!info.session_id && !info.peer_session_id) {
714309795f4SJames Chapman 		if (tunnel == NULL) {
715309795f4SJames Chapman 			struct l2tp_tunnel_cfg tcfg = {
716309795f4SJames Chapman 				.encap = L2TP_ENCAPTYPE_UDP,
717309795f4SJames Chapman 				.debug = 0,
718309795f4SJames Chapman 			};
7193e1bc8bfSGuillaume Nault 
7203e1bc8bfSGuillaume Nault 			/* Prevent l2tp_tunnel_register() from trying to set up
7213e1bc8bfSGuillaume Nault 			 * a kernel socket.
7223e1bc8bfSGuillaume Nault 			 */
723a408194aSGuillaume Nault 			if (info.fd < 0) {
7243e1bc8bfSGuillaume Nault 				error = -EBADF;
7253e1bc8bfSGuillaume Nault 				goto end;
7263e1bc8bfSGuillaume Nault 			}
7273e1bc8bfSGuillaume Nault 
728a408194aSGuillaume Nault 			error = l2tp_tunnel_create(sock_net(sk), info.fd,
729a408194aSGuillaume Nault 						   info.version,
730a408194aSGuillaume Nault 						   info.tunnel_id,
731a408194aSGuillaume Nault 						   info.peer_tunnel_id, &tcfg,
732a408194aSGuillaume Nault 						   &tunnel);
733fd558d18SJames Chapman 			if (error < 0)
734fd558d18SJames Chapman 				goto end;
7356b9f3423SGuillaume Nault 
7366b9f3423SGuillaume Nault 			l2tp_tunnel_inc_refcount(tunnel);
7376b9f3423SGuillaume Nault 			error = l2tp_tunnel_register(tunnel, sock_net(sk),
7386b9f3423SGuillaume Nault 						     &tcfg);
7396b9f3423SGuillaume Nault 			if (error < 0) {
7406b9f3423SGuillaume Nault 				kfree(tunnel);
7416b9f3423SGuillaume Nault 				goto end;
7426b9f3423SGuillaume Nault 			}
7436b9f3423SGuillaume Nault 			drop_tunnel = true;
744bda06be2SGuillaume Nault 			new_tunnel = true;
745309795f4SJames Chapman 		}
746fd558d18SJames Chapman 	} else {
747fd558d18SJames Chapman 		/* Error if we can't find the tunnel */
748fd558d18SJames Chapman 		error = -ENOENT;
749fd558d18SJames Chapman 		if (tunnel == NULL)
750fd558d18SJames Chapman 			goto end;
751fd558d18SJames Chapman 
752fd558d18SJames Chapman 		/* Error if socket is not prepped */
753fd558d18SJames Chapman 		if (tunnel->sock == NULL)
754fd558d18SJames Chapman 			goto end;
755fd558d18SJames Chapman 	}
756fd558d18SJames Chapman 
757b79585f5SJames Chapman 	if (tunnel->peer_tunnel_id == 0)
758a408194aSGuillaume Nault 		tunnel->peer_tunnel_id = info.peer_tunnel_id;
759fd558d18SJames Chapman 
76001e28b92SGuillaume Nault 	session = l2tp_tunnel_get_session(tunnel, info.session_id);
761dbdbc73bSGuillaume Nault 	if (session) {
762dbdbc73bSGuillaume Nault 		drop_refcnt = true;
7637ac6ab1fSGuillaume Nault 
7647ac6ab1fSGuillaume Nault 		if (session->pwtype != L2TP_PWTYPE_PPP) {
7657ac6ab1fSGuillaume Nault 			error = -EPROTOTYPE;
7667ac6ab1fSGuillaume Nault 			goto end;
7677ac6ab1fSGuillaume Nault 		}
7687ac6ab1fSGuillaume Nault 
769dbdbc73bSGuillaume Nault 		ps = l2tp_session_priv(session);
770fd558d18SJames Chapman 
771dbdbc73bSGuillaume Nault 		/* Using a pre-existing session is fine as long as it hasn't
772dbdbc73bSGuillaume Nault 		 * been connected yet.
773dbdbc73bSGuillaume Nault 		 */
774ee40fb2eSGuillaume Nault 		mutex_lock(&ps->sk_lock);
775ee40fb2eSGuillaume Nault 		if (rcu_dereference_protected(ps->sk,
7763d609342SGuillaume Nault 					      lockdep_is_held(&ps->sk_lock)) ||
7773d609342SGuillaume Nault 		    ps->__sk) {
778ee40fb2eSGuillaume Nault 			mutex_unlock(&ps->sk_lock);
779dbdbc73bSGuillaume Nault 			error = -EEXIST;
780dbdbc73bSGuillaume Nault 			goto end;
781dbdbc73bSGuillaume Nault 		}
782309795f4SJames Chapman 	} else {
78390904ff5SGuillaume Nault 		cfg.pw_type = L2TP_PWTYPE_PPP;
784fd558d18SJames Chapman 
785dbdbc73bSGuillaume Nault 		session = l2tp_session_create(sizeof(struct pppol2tp_session),
786a408194aSGuillaume Nault 					      tunnel, info.session_id,
787a408194aSGuillaume Nault 					      info.peer_session_id, &cfg);
788dbdbc73bSGuillaume Nault 		if (IS_ERR(session)) {
789dbdbc73bSGuillaume Nault 			error = PTR_ERR(session);
790309795f4SJames Chapman 			goto end;
791309795f4SJames Chapman 		}
7923953ae7bSGuillaume Nault 
793f98be6c6SGuillaume Nault 		pppol2tp_session_init(session);
794ee40fb2eSGuillaume Nault 		ps = l2tp_session_priv(session);
7953953ae7bSGuillaume Nault 		l2tp_session_inc_refcount(session);
796ee40fb2eSGuillaume Nault 
797ee40fb2eSGuillaume Nault 		mutex_lock(&ps->sk_lock);
7983953ae7bSGuillaume Nault 		error = l2tp_session_register(session, tunnel);
7993953ae7bSGuillaume Nault 		if (error < 0) {
800ee40fb2eSGuillaume Nault 			mutex_unlock(&ps->sk_lock);
8013953ae7bSGuillaume Nault 			kfree(session);
8023953ae7bSGuillaume Nault 			goto end;
8033953ae7bSGuillaume Nault 		}
8043953ae7bSGuillaume Nault 		drop_refcnt = true;
805bda06be2SGuillaume Nault 		new_session = true;
806dbdbc73bSGuillaume Nault 	}
807309795f4SJames Chapman 
808fd558d18SJames Chapman 	/* Special case: if source & dest session_id == 0x0000, this
809fd558d18SJames Chapman 	 * socket is being created to manage the tunnel. Just set up
810fd558d18SJames Chapman 	 * the internal context for use by ioctl() and sockopt()
811fd558d18SJames Chapman 	 * handlers.
812fd558d18SJames Chapman 	 */
813fd558d18SJames Chapman 	if ((session->session_id == 0) &&
814fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
815fd558d18SJames Chapman 		error = 0;
816fd558d18SJames Chapman 		goto out_no_ppp;
817fd558d18SJames Chapman 	}
818fd558d18SJames Chapman 
819fd558d18SJames Chapman 	/* The only header we need to worry about is the L2TP
820fd558d18SJames Chapman 	 * header. This size is different depending on whether
821fd558d18SJames Chapman 	 * sequence numbers are enabled for the data channel.
822fd558d18SJames Chapman 	 */
823fd558d18SJames Chapman 	po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
824fd558d18SJames Chapman 
825fd558d18SJames Chapman 	po->chan.private = sk;
826fd558d18SJames Chapman 	po->chan.ops	 = &pppol2tp_chan_ops;
827789141b2SGuillaume Nault 	po->chan.mtu	 = pppol2tp_tunnel_mtu(tunnel);
828fd558d18SJames Chapman 
829fd558d18SJames Chapman 	error = ppp_register_net_channel(sock_net(sk), &po->chan);
830ee40fb2eSGuillaume Nault 	if (error) {
831ee40fb2eSGuillaume Nault 		mutex_unlock(&ps->sk_lock);
832fd558d18SJames Chapman 		goto end;
833ee40fb2eSGuillaume Nault 	}
834fd558d18SJames Chapman 
835fd558d18SJames Chapman out_no_ppp:
836fd558d18SJames Chapman 	/* This is how we get the session context from the socket. */
837fd558d18SJames Chapman 	sk->sk_user_data = session;
838ee40fb2eSGuillaume Nault 	rcu_assign_pointer(ps->sk, sk);
839ee40fb2eSGuillaume Nault 	mutex_unlock(&ps->sk_lock);
840ee40fb2eSGuillaume Nault 
841f98be6c6SGuillaume Nault 	/* Keep the reference we've grabbed on the session: sk doesn't expect
842f98be6c6SGuillaume Nault 	 * the session to disappear. pppol2tp_session_destruct() is responsible
843f98be6c6SGuillaume Nault 	 * for dropping it.
844f98be6c6SGuillaume Nault 	 */
845f98be6c6SGuillaume Nault 	drop_refcnt = false;
846f98be6c6SGuillaume Nault 
847fd558d18SJames Chapman 	sk->sk_state = PPPOX_CONNECTED;
848fba40c63SAsbjørn Sloth Tønnesen 	l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
849a4ca44faSJoe Perches 		  session->name);
850fd558d18SJames Chapman 
851fd558d18SJames Chapman end:
852bda06be2SGuillaume Nault 	if (error) {
853bda06be2SGuillaume Nault 		if (new_session)
854bda06be2SGuillaume Nault 			l2tp_session_delete(session);
855bda06be2SGuillaume Nault 		if (new_tunnel)
856bda06be2SGuillaume Nault 			l2tp_tunnel_delete(tunnel);
857bda06be2SGuillaume Nault 	}
858dbdbc73bSGuillaume Nault 	if (drop_refcnt)
859dbdbc73bSGuillaume Nault 		l2tp_session_dec_refcount(session);
860f9e56bafSGuillaume Nault 	if (drop_tunnel)
861f9e56bafSGuillaume Nault 		l2tp_tunnel_dec_refcount(tunnel);
862fd558d18SJames Chapman 	release_sock(sk);
863fd558d18SJames Chapman 
864fd558d18SJames Chapman 	return error;
865fd558d18SJames Chapman }
866fd558d18SJames Chapman 
867309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
868309795f4SJames Chapman 
869f026bc29SGuillaume Nault /* Called when creating sessions via the netlink interface. */
870f026bc29SGuillaume Nault static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
871f026bc29SGuillaume Nault 				   u32 session_id, u32 peer_session_id,
872f026bc29SGuillaume Nault 				   struct l2tp_session_cfg *cfg)
873309795f4SJames Chapman {
874309795f4SJames Chapman 	int error;
875309795f4SJames Chapman 	struct l2tp_session *session;
876309795f4SJames Chapman 
877309795f4SJames Chapman 	/* Error if tunnel socket is not prepped */
878f026bc29SGuillaume Nault 	if (!tunnel->sock) {
879f026bc29SGuillaume Nault 		error = -ENOENT;
8803953ae7bSGuillaume Nault 		goto err;
881f026bc29SGuillaume Nault 	}
882309795f4SJames Chapman 
883309795f4SJames Chapman 	/* Allocate and initialize a new session context. */
884309795f4SJames Chapman 	session = l2tp_session_create(sizeof(struct pppol2tp_session),
885309795f4SJames Chapman 				      tunnel, session_id,
886309795f4SJames Chapman 				      peer_session_id, cfg);
887dbdbc73bSGuillaume Nault 	if (IS_ERR(session)) {
888dbdbc73bSGuillaume Nault 		error = PTR_ERR(session);
8893953ae7bSGuillaume Nault 		goto err;
890dbdbc73bSGuillaume Nault 	}
891309795f4SJames Chapman 
892f98be6c6SGuillaume Nault 	pppol2tp_session_init(session);
893309795f4SJames Chapman 
8943953ae7bSGuillaume Nault 	error = l2tp_session_register(session, tunnel);
8953953ae7bSGuillaume Nault 	if (error < 0)
8963953ae7bSGuillaume Nault 		goto err_sess;
897309795f4SJames Chapman 
8983953ae7bSGuillaume Nault 	return 0;
899309795f4SJames Chapman 
9003953ae7bSGuillaume Nault err_sess:
9013953ae7bSGuillaume Nault 	kfree(session);
9023953ae7bSGuillaume Nault err:
903309795f4SJames Chapman 	return error;
904309795f4SJames Chapman }
905309795f4SJames Chapman 
906309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */
907309795f4SJames Chapman 
908fd558d18SJames Chapman /* getname() support.
909fd558d18SJames Chapman  */
910fd558d18SJames Chapman static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
9119b2c45d4SDenys Vlasenko 			    int peer)
912fd558d18SJames Chapman {
913e0d4435fSJames Chapman 	int len = 0;
914fd558d18SJames Chapman 	int error = 0;
915fd558d18SJames Chapman 	struct l2tp_session *session;
916fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
917fd558d18SJames Chapman 	struct sock *sk = sock->sk;
918fd558d18SJames Chapman 	struct inet_sock *inet;
919fd558d18SJames Chapman 	struct pppol2tp_session *pls;
920fd558d18SJames Chapman 
921fd558d18SJames Chapman 	error = -ENOTCONN;
922fd558d18SJames Chapman 	if (sk == NULL)
923fd558d18SJames Chapman 		goto end;
92456cff471SGao Feng 	if (!(sk->sk_state & PPPOX_CONNECTED))
925fd558d18SJames Chapman 		goto end;
926fd558d18SJames Chapman 
927fd558d18SJames Chapman 	error = -EBADF;
928fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
929fd558d18SJames Chapman 	if (session == NULL)
930fd558d18SJames Chapman 		goto end;
931fd558d18SJames Chapman 
932fd558d18SJames Chapman 	pls = l2tp_session_priv(session);
9337198c77aSGuillaume Nault 	tunnel = session->tunnel;
934fd558d18SJames Chapman 
935bbdb32cbSBenjamin LaHaise 	inet = inet_sk(tunnel->sock);
936d2cf3361SBenjamin LaHaise 	if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
937e0d4435fSJames Chapman 		struct sockaddr_pppol2tp sp;
938e0d4435fSJames Chapman 		len = sizeof(sp);
939fd558d18SJames Chapman 		memset(&sp, 0, len);
940fd558d18SJames Chapman 		sp.sa_family	= AF_PPPOX;
941fd558d18SJames Chapman 		sp.sa_protocol	= PX_PROTO_OL2TP;
942fd558d18SJames Chapman 		sp.pppol2tp.fd  = tunnel->fd;
943fd558d18SJames Chapman 		sp.pppol2tp.pid = pls->owner;
944fd558d18SJames Chapman 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
945fd558d18SJames Chapman 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
946fd558d18SJames Chapman 		sp.pppol2tp.s_session = session->session_id;
947fd558d18SJames Chapman 		sp.pppol2tp.d_session = session->peer_session_id;
948fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_family = AF_INET;
949fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
950fd558d18SJames Chapman 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
951fd558d18SJames Chapman 		memcpy(uaddr, &sp, len);
952d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
953d2cf3361SBenjamin LaHaise 	} else if ((tunnel->version == 2) &&
954d2cf3361SBenjamin LaHaise 		   (tunnel->sock->sk_family == AF_INET6)) {
955d2cf3361SBenjamin LaHaise 		struct sockaddr_pppol2tpin6 sp;
956efe4208fSEric Dumazet 
957d2cf3361SBenjamin LaHaise 		len = sizeof(sp);
958d2cf3361SBenjamin LaHaise 		memset(&sp, 0, len);
959d2cf3361SBenjamin LaHaise 		sp.sa_family	= AF_PPPOX;
960d2cf3361SBenjamin LaHaise 		sp.sa_protocol	= PX_PROTO_OL2TP;
961d2cf3361SBenjamin LaHaise 		sp.pppol2tp.fd  = tunnel->fd;
962d2cf3361SBenjamin LaHaise 		sp.pppol2tp.pid = pls->owner;
963d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
964d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
965d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_session = session->session_id;
966d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_session = session->peer_session_id;
967d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_family = AF_INET6;
968d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
969efe4208fSEric Dumazet 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
970efe4208fSEric Dumazet 		       sizeof(tunnel->sock->sk_v6_daddr));
971d2cf3361SBenjamin LaHaise 		memcpy(uaddr, &sp, len);
972d2cf3361SBenjamin LaHaise 	} else if ((tunnel->version == 3) &&
973d2cf3361SBenjamin LaHaise 		   (tunnel->sock->sk_family == AF_INET6)) {
974d2cf3361SBenjamin LaHaise 		struct sockaddr_pppol2tpv3in6 sp;
975efe4208fSEric Dumazet 
976d2cf3361SBenjamin LaHaise 		len = sizeof(sp);
977d2cf3361SBenjamin LaHaise 		memset(&sp, 0, len);
978d2cf3361SBenjamin LaHaise 		sp.sa_family	= AF_PPPOX;
979d2cf3361SBenjamin LaHaise 		sp.sa_protocol	= PX_PROTO_OL2TP;
980d2cf3361SBenjamin LaHaise 		sp.pppol2tp.fd  = tunnel->fd;
981d2cf3361SBenjamin LaHaise 		sp.pppol2tp.pid = pls->owner;
982d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
983d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
984d2cf3361SBenjamin LaHaise 		sp.pppol2tp.s_session = session->session_id;
985d2cf3361SBenjamin LaHaise 		sp.pppol2tp.d_session = session->peer_session_id;
986d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_family = AF_INET6;
987d2cf3361SBenjamin LaHaise 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
988efe4208fSEric Dumazet 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
989efe4208fSEric Dumazet 		       sizeof(tunnel->sock->sk_v6_daddr));
990d2cf3361SBenjamin LaHaise 		memcpy(uaddr, &sp, len);
991d2cf3361SBenjamin LaHaise #endif
992e0d4435fSJames Chapman 	} else if (tunnel->version == 3) {
993e0d4435fSJames Chapman 		struct sockaddr_pppol2tpv3 sp;
994e0d4435fSJames Chapman 		len = sizeof(sp);
995e0d4435fSJames Chapman 		memset(&sp, 0, len);
996e0d4435fSJames Chapman 		sp.sa_family	= AF_PPPOX;
997e0d4435fSJames Chapman 		sp.sa_protocol	= PX_PROTO_OL2TP;
998e0d4435fSJames Chapman 		sp.pppol2tp.fd  = tunnel->fd;
999e0d4435fSJames Chapman 		sp.pppol2tp.pid = pls->owner;
1000e0d4435fSJames Chapman 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
1001e0d4435fSJames Chapman 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
1002e0d4435fSJames Chapman 		sp.pppol2tp.s_session = session->session_id;
1003e0d4435fSJames Chapman 		sp.pppol2tp.d_session = session->peer_session_id;
1004e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_family = AF_INET;
1005e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
1006e0d4435fSJames Chapman 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
1007e0d4435fSJames Chapman 		memcpy(uaddr, &sp, len);
1008e0d4435fSJames Chapman 	}
1009fd558d18SJames Chapman 
10109b2c45d4SDenys Vlasenko 	error = len;
1011fd558d18SJames Chapman 
1012fd558d18SJames Chapman 	sock_put(sk);
1013fd558d18SJames Chapman end:
1014fd558d18SJames Chapman 	return error;
1015fd558d18SJames Chapman }
1016fd558d18SJames Chapman 
1017fd558d18SJames Chapman /****************************************************************************
1018fd558d18SJames Chapman  * ioctl() handlers.
1019fd558d18SJames Chapman  *
1020fd558d18SJames Chapman  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1021fd558d18SJames Chapman  * sockets. However, in order to control kernel tunnel features, we allow
1022fd558d18SJames Chapman  * userspace to create a special "tunnel" PPPoX socket which is used for
1023fd558d18SJames Chapman  * control only.  Tunnel PPPoX sockets have session_id == 0 and simply allow
1024fd558d18SJames Chapman  * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1025fd558d18SJames Chapman  * calls.
1026fd558d18SJames Chapman  ****************************************************************************/
1027fd558d18SJames Chapman 
1028fd558d18SJames Chapman static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1029fd558d18SJames Chapman 				struct l2tp_stats *stats)
1030fd558d18SJames Chapman {
10317b7c0719STom Parkin 	dest->tx_packets = atomic_long_read(&stats->tx_packets);
10327b7c0719STom Parkin 	dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
10337b7c0719STom Parkin 	dest->tx_errors = atomic_long_read(&stats->tx_errors);
10347b7c0719STom Parkin 	dest->rx_packets = atomic_long_read(&stats->rx_packets);
10357b7c0719STom Parkin 	dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
10367b7c0719STom Parkin 	dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
10377b7c0719STom Parkin 	dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
10387b7c0719STom Parkin 	dest->rx_errors = atomic_long_read(&stats->rx_errors);
1039fd558d18SJames Chapman }
1040fd558d18SJames Chapman 
1041528534f0SGuillaume Nault static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
1042528534f0SGuillaume Nault 				      struct l2tp_tunnel *tunnel)
1043528534f0SGuillaume Nault {
1044528534f0SGuillaume Nault 	struct l2tp_session *session;
1045528534f0SGuillaume Nault 
1046528534f0SGuillaume Nault 	if (!stats->session_id) {
1047528534f0SGuillaume Nault 		memset(stats, 0, sizeof(*stats));
1048528534f0SGuillaume Nault 		pppol2tp_copy_stats(stats, &tunnel->stats);
1049528534f0SGuillaume Nault 		return 0;
1050528534f0SGuillaume Nault 	}
1051528534f0SGuillaume Nault 
1052528534f0SGuillaume Nault 	/* If session_id is set, search the corresponding session in the
1053528534f0SGuillaume Nault 	 * context of this tunnel and record the session's statistics.
1054528534f0SGuillaume Nault 	 */
1055528534f0SGuillaume Nault 	session = l2tp_tunnel_get_session(tunnel, stats->session_id);
1056528534f0SGuillaume Nault 	if (!session)
1057528534f0SGuillaume Nault 		return -EBADR;
1058528534f0SGuillaume Nault 
1059528534f0SGuillaume Nault 	if (session->pwtype != L2TP_PWTYPE_PPP) {
1060528534f0SGuillaume Nault 		l2tp_session_dec_refcount(session);
1061528534f0SGuillaume Nault 		return -EBADR;
1062528534f0SGuillaume Nault 	}
1063528534f0SGuillaume Nault 
1064528534f0SGuillaume Nault 	memset(stats, 0, sizeof(*stats));
1065528534f0SGuillaume Nault 	pppol2tp_copy_stats(stats, &session->stats);
1066528534f0SGuillaume Nault 	l2tp_session_dec_refcount(session);
1067528534f0SGuillaume Nault 
1068528534f0SGuillaume Nault 	return 0;
1069528534f0SGuillaume Nault }
1070528534f0SGuillaume Nault 
1071fd558d18SJames Chapman static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1072fd558d18SJames Chapman 			  unsigned long arg)
1073fd558d18SJames Chapman {
1074528534f0SGuillaume Nault 	struct pppol2tp_ioc_stats stats;
1075fd558d18SJames Chapman 	struct l2tp_session *session;
107679e6760eSGuillaume Nault 	int val;
1077fd558d18SJames Chapman 
107879e6760eSGuillaume Nault 	switch (cmd) {
107979e6760eSGuillaume Nault 	case PPPIOCGMRU:
108079e6760eSGuillaume Nault 	case PPPIOCGFLAGS:
1081bdd0292fSGuillaume Nault 		session = sock->sk->sk_user_data;
1082bdd0292fSGuillaume Nault 		if (!session)
1083bdd0292fSGuillaume Nault 			return -ENOTCONN;
1084fd558d18SJames Chapman 
108579e6760eSGuillaume Nault 		/* Not defined for tunnels */
108679e6760eSGuillaume Nault 		if (!session->session_id && !session->peer_session_id)
108779e6760eSGuillaume Nault 			return -ENOSYS;
1088bdd0292fSGuillaume Nault 
108979e6760eSGuillaume Nault 		if (put_user(0, (int __user *)arg))
109079e6760eSGuillaume Nault 			return -EFAULT;
109179e6760eSGuillaume Nault 		break;
109279e6760eSGuillaume Nault 
109379e6760eSGuillaume Nault 	case PPPIOCSMRU:
109479e6760eSGuillaume Nault 	case PPPIOCSFLAGS:
109579e6760eSGuillaume Nault 		session = sock->sk->sk_user_data;
109679e6760eSGuillaume Nault 		if (!session)
109779e6760eSGuillaume Nault 			return -ENOTCONN;
109879e6760eSGuillaume Nault 
109979e6760eSGuillaume Nault 		/* Not defined for tunnels */
110079e6760eSGuillaume Nault 		if (!session->session_id && !session->peer_session_id)
110179e6760eSGuillaume Nault 			return -ENOSYS;
110279e6760eSGuillaume Nault 
110379e6760eSGuillaume Nault 		if (get_user(val, (int __user *)arg))
110479e6760eSGuillaume Nault 			return -EFAULT;
110579e6760eSGuillaume Nault 		break;
110679e6760eSGuillaume Nault 
110779e6760eSGuillaume Nault 	case PPPIOCGL2TPSTATS:
110879e6760eSGuillaume Nault 		session = sock->sk->sk_user_data;
110979e6760eSGuillaume Nault 		if (!session)
111079e6760eSGuillaume Nault 			return -ENOTCONN;
111179e6760eSGuillaume Nault 
111279e6760eSGuillaume Nault 		/* Session 0 represents the parent tunnel */
1113528534f0SGuillaume Nault 		if (!session->session_id && !session->peer_session_id) {
1114528534f0SGuillaume Nault 			u32 session_id;
1115528534f0SGuillaume Nault 			int err;
1116528534f0SGuillaume Nault 
1117528534f0SGuillaume Nault 			if (copy_from_user(&stats, (void __user *)arg,
1118528534f0SGuillaume Nault 					   sizeof(stats)))
1119528534f0SGuillaume Nault 				return -EFAULT;
1120528534f0SGuillaume Nault 
1121528534f0SGuillaume Nault 			session_id = stats.session_id;
1122528534f0SGuillaume Nault 			err = pppol2tp_tunnel_copy_stats(&stats,
1123528534f0SGuillaume Nault 							 session->tunnel);
1124528534f0SGuillaume Nault 			if (err < 0)
1125528534f0SGuillaume Nault 				return err;
1126528534f0SGuillaume Nault 
1127528534f0SGuillaume Nault 			stats.session_id = session_id;
1128528534f0SGuillaume Nault 		} else {
1129*b0e29063SGuillaume Nault 			memset(&stats, 0, sizeof(stats));
1130*b0e29063SGuillaume Nault 			pppol2tp_copy_stats(&stats, &session->stats);
1131*b0e29063SGuillaume Nault 			stats.session_id = session->session_id;
1132528534f0SGuillaume Nault 		}
1133528534f0SGuillaume Nault 		stats.tunnel_id = session->tunnel->tunnel_id;
1134528534f0SGuillaume Nault 		stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
1135528534f0SGuillaume Nault 
1136528534f0SGuillaume Nault 		if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
1137528534f0SGuillaume Nault 			return -EFAULT;
113879e6760eSGuillaume Nault 		break;
113979e6760eSGuillaume Nault 
114079e6760eSGuillaume Nault 	default:
114179e6760eSGuillaume Nault 		return -ENOSYS;
1142fd558d18SJames Chapman 	}
1143fd558d18SJames Chapman 
114479e6760eSGuillaume Nault 	return 0;
1145fd558d18SJames Chapman }
1146fd558d18SJames Chapman 
1147fd558d18SJames Chapman /*****************************************************************************
1148fd558d18SJames Chapman  * setsockopt() / getsockopt() support.
1149fd558d18SJames Chapman  *
1150fd558d18SJames Chapman  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1151fd558d18SJames Chapman  * sockets. In order to control kernel tunnel features, we allow userspace to
1152fd558d18SJames Chapman  * create a special "tunnel" PPPoX socket which is used for control only.
1153fd558d18SJames Chapman  * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1154fd558d18SJames Chapman  * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1155fd558d18SJames Chapman  *****************************************************************************/
1156fd558d18SJames Chapman 
1157fd558d18SJames Chapman /* Tunnel setsockopt() helper.
1158fd558d18SJames Chapman  */
1159fd558d18SJames Chapman static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1160fd558d18SJames Chapman 				      struct l2tp_tunnel *tunnel,
1161fd558d18SJames Chapman 				      int optname, int val)
1162fd558d18SJames Chapman {
1163fd558d18SJames Chapman 	int err = 0;
1164fd558d18SJames Chapman 
1165fd558d18SJames Chapman 	switch (optname) {
1166fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1167fd558d18SJames Chapman 		tunnel->debug = val;
1168fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1169a4ca44faSJoe Perches 			  tunnel->name, tunnel->debug);
1170fd558d18SJames Chapman 		break;
1171fd558d18SJames Chapman 
1172fd558d18SJames Chapman 	default:
1173fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1174fd558d18SJames Chapman 		break;
1175fd558d18SJames Chapman 	}
1176fd558d18SJames Chapman 
1177fd558d18SJames Chapman 	return err;
1178fd558d18SJames Chapman }
1179fd558d18SJames Chapman 
1180fd558d18SJames Chapman /* Session setsockopt helper.
1181fd558d18SJames Chapman  */
1182fd558d18SJames Chapman static int pppol2tp_session_setsockopt(struct sock *sk,
1183fd558d18SJames Chapman 				       struct l2tp_session *session,
1184fd558d18SJames Chapman 				       int optname, int val)
1185fd558d18SJames Chapman {
1186fd558d18SJames Chapman 	int err = 0;
1187fd558d18SJames Chapman 
1188fd558d18SJames Chapman 	switch (optname) {
1189fd558d18SJames Chapman 	case PPPOL2TP_SO_RECVSEQ:
1190fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1191fd558d18SJames Chapman 			err = -EINVAL;
1192fd558d18SJames Chapman 			break;
1193fd558d18SJames Chapman 		}
11943f9b9770SAsbjørn Sloth Tønnesen 		session->recv_seq = !!val;
1195fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1196a4ca44faSJoe Perches 			  "%s: set recv_seq=%d\n",
1197a4ca44faSJoe Perches 			  session->name, session->recv_seq);
1198fd558d18SJames Chapman 		break;
1199fd558d18SJames Chapman 
1200fd558d18SJames Chapman 	case PPPOL2TP_SO_SENDSEQ:
1201fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1202fd558d18SJames Chapman 			err = -EINVAL;
1203fd558d18SJames Chapman 			break;
1204fd558d18SJames Chapman 		}
12053f9b9770SAsbjørn Sloth Tønnesen 		session->send_seq = !!val;
1206fd558d18SJames Chapman 		{
1207ee40fb2eSGuillaume Nault 			struct pppox_sock *po = pppox_sk(sk);
1208ee40fb2eSGuillaume Nault 
1209fd558d18SJames Chapman 			po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1210fd558d18SJames Chapman 				PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1211fd558d18SJames Chapman 		}
1212bb5016eaSGuillaume Nault 		l2tp_session_set_header_len(session, session->tunnel->version);
1213fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1214a4ca44faSJoe Perches 			  "%s: set send_seq=%d\n",
1215a4ca44faSJoe Perches 			  session->name, session->send_seq);
1216fd558d18SJames Chapman 		break;
1217fd558d18SJames Chapman 
1218fd558d18SJames Chapman 	case PPPOL2TP_SO_LNSMODE:
1219fd558d18SJames Chapman 		if ((val != 0) && (val != 1)) {
1220fd558d18SJames Chapman 			err = -EINVAL;
1221fd558d18SJames Chapman 			break;
1222fd558d18SJames Chapman 		}
12233f9b9770SAsbjørn Sloth Tønnesen 		session->lns_mode = !!val;
1224fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1225a4ca44faSJoe Perches 			  "%s: set lns_mode=%d\n",
1226a4ca44faSJoe Perches 			  session->name, session->lns_mode);
1227fd558d18SJames Chapman 		break;
1228fd558d18SJames Chapman 
1229fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1230fd558d18SJames Chapman 		session->debug = val;
1231fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1232a4ca44faSJoe Perches 			  session->name, session->debug);
1233fd558d18SJames Chapman 		break;
1234fd558d18SJames Chapman 
1235fd558d18SJames Chapman 	case PPPOL2TP_SO_REORDERTO:
1236fd558d18SJames Chapman 		session->reorder_timeout = msecs_to_jiffies(val);
1237fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1238a4ca44faSJoe Perches 			  "%s: set reorder_timeout=%d\n",
1239a4ca44faSJoe Perches 			  session->name, session->reorder_timeout);
1240fd558d18SJames Chapman 		break;
1241fd558d18SJames Chapman 
1242fd558d18SJames Chapman 	default:
1243fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1244fd558d18SJames Chapman 		break;
1245fd558d18SJames Chapman 	}
1246fd558d18SJames Chapman 
1247fd558d18SJames Chapman 	return err;
1248fd558d18SJames Chapman }
1249fd558d18SJames Chapman 
1250fd558d18SJames Chapman /* Main setsockopt() entry point.
1251fd558d18SJames Chapman  * Does API checks, then calls either the tunnel or session setsockopt
1252fd558d18SJames Chapman  * handler, according to whether the PPPoL2TP socket is a for a regular
1253fd558d18SJames Chapman  * session or the special tunnel type.
1254fd558d18SJames Chapman  */
1255fd558d18SJames Chapman static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1256fd558d18SJames Chapman 			       char __user *optval, unsigned int optlen)
1257fd558d18SJames Chapman {
1258fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1259fd558d18SJames Chapman 	struct l2tp_session *session;
1260fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1261fd558d18SJames Chapman 	int val;
1262fd558d18SJames Chapman 	int err;
1263fd558d18SJames Chapman 
1264fd558d18SJames Chapman 	if (level != SOL_PPPOL2TP)
12653cf521f7SSasha Levin 		return -EINVAL;
1266fd558d18SJames Chapman 
1267fd558d18SJames Chapman 	if (optlen < sizeof(int))
1268fd558d18SJames Chapman 		return -EINVAL;
1269fd558d18SJames Chapman 
1270fd558d18SJames Chapman 	if (get_user(val, (int __user *)optval))
1271fd558d18SJames Chapman 		return -EFAULT;
1272fd558d18SJames Chapman 
1273fd558d18SJames Chapman 	err = -ENOTCONN;
1274fd558d18SJames Chapman 	if (sk->sk_user_data == NULL)
1275fd558d18SJames Chapman 		goto end;
1276fd558d18SJames Chapman 
1277fd558d18SJames Chapman 	/* Get session context from the socket */
1278fd558d18SJames Chapman 	err = -EBADF;
1279fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
1280fd558d18SJames Chapman 	if (session == NULL)
1281fd558d18SJames Chapman 		goto end;
1282fd558d18SJames Chapman 
1283fd558d18SJames Chapman 	/* Special case: if session_id == 0x0000, treat as operation on tunnel
1284fd558d18SJames Chapman 	 */
1285fd558d18SJames Chapman 	if ((session->session_id == 0) &&
1286fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
12877198c77aSGuillaume Nault 		tunnel = session->tunnel;
1288fd558d18SJames Chapman 		err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
12897198c77aSGuillaume Nault 	} else {
1290fd558d18SJames Chapman 		err = pppol2tp_session_setsockopt(sk, session, optname, val);
12917198c77aSGuillaume Nault 	}
1292fd558d18SJames Chapman 
1293fd558d18SJames Chapman 	sock_put(sk);
1294fd558d18SJames Chapman end:
1295fd558d18SJames Chapman 	return err;
1296fd558d18SJames Chapman }
1297fd558d18SJames Chapman 
1298fd558d18SJames Chapman /* Tunnel getsockopt helper. Called with sock locked.
1299fd558d18SJames Chapman  */
1300fd558d18SJames Chapman static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1301fd558d18SJames Chapman 				      struct l2tp_tunnel *tunnel,
1302fd558d18SJames Chapman 				      int optname, int *val)
1303fd558d18SJames Chapman {
1304fd558d18SJames Chapman 	int err = 0;
1305fd558d18SJames Chapman 
1306fd558d18SJames Chapman 	switch (optname) {
1307fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1308fd558d18SJames Chapman 		*val = tunnel->debug;
1309fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
1310a4ca44faSJoe Perches 			  tunnel->name, tunnel->debug);
1311fd558d18SJames Chapman 		break;
1312fd558d18SJames Chapman 
1313fd558d18SJames Chapman 	default:
1314fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1315fd558d18SJames Chapman 		break;
1316fd558d18SJames Chapman 	}
1317fd558d18SJames Chapman 
1318fd558d18SJames Chapman 	return err;
1319fd558d18SJames Chapman }
1320fd558d18SJames Chapman 
1321fd558d18SJames Chapman /* Session getsockopt helper. Called with sock locked.
1322fd558d18SJames Chapman  */
1323fd558d18SJames Chapman static int pppol2tp_session_getsockopt(struct sock *sk,
1324fd558d18SJames Chapman 				       struct l2tp_session *session,
1325fd558d18SJames Chapman 				       int optname, int *val)
1326fd558d18SJames Chapman {
1327fd558d18SJames Chapman 	int err = 0;
1328fd558d18SJames Chapman 
1329fd558d18SJames Chapman 	switch (optname) {
1330fd558d18SJames Chapman 	case PPPOL2TP_SO_RECVSEQ:
1331fd558d18SJames Chapman 		*val = session->recv_seq;
1332fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1333fd558d18SJames Chapman 			  "%s: get recv_seq=%d\n", session->name, *val);
1334fd558d18SJames Chapman 		break;
1335fd558d18SJames Chapman 
1336fd558d18SJames Chapman 	case PPPOL2TP_SO_SENDSEQ:
1337fd558d18SJames Chapman 		*val = session->send_seq;
1338fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1339fd558d18SJames Chapman 			  "%s: get send_seq=%d\n", session->name, *val);
1340fd558d18SJames Chapman 		break;
1341fd558d18SJames Chapman 
1342fd558d18SJames Chapman 	case PPPOL2TP_SO_LNSMODE:
1343fd558d18SJames Chapman 		*val = session->lns_mode;
1344fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1345fd558d18SJames Chapman 			  "%s: get lns_mode=%d\n", session->name, *val);
1346fd558d18SJames Chapman 		break;
1347fd558d18SJames Chapman 
1348fd558d18SJames Chapman 	case PPPOL2TP_SO_DEBUG:
1349fd558d18SJames Chapman 		*val = session->debug;
1350fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
1351a4ca44faSJoe Perches 			  session->name, *val);
1352fd558d18SJames Chapman 		break;
1353fd558d18SJames Chapman 
1354fd558d18SJames Chapman 	case PPPOL2TP_SO_REORDERTO:
1355fd558d18SJames Chapman 		*val = (int) jiffies_to_msecs(session->reorder_timeout);
1356fba40c63SAsbjørn Sloth Tønnesen 		l2tp_info(session, L2TP_MSG_CONTROL,
1357fd558d18SJames Chapman 			  "%s: get reorder_timeout=%d\n", session->name, *val);
1358fd558d18SJames Chapman 		break;
1359fd558d18SJames Chapman 
1360fd558d18SJames Chapman 	default:
1361fd558d18SJames Chapman 		err = -ENOPROTOOPT;
1362fd558d18SJames Chapman 	}
1363fd558d18SJames Chapman 
1364fd558d18SJames Chapman 	return err;
1365fd558d18SJames Chapman }
1366fd558d18SJames Chapman 
1367fd558d18SJames Chapman /* Main getsockopt() entry point.
1368fd558d18SJames Chapman  * Does API checks, then calls either the tunnel or session getsockopt
1369fd558d18SJames Chapman  * handler, according to whether the PPPoX socket is a for a regular session
1370fd558d18SJames Chapman  * or the special tunnel type.
1371fd558d18SJames Chapman  */
1372e3192690SJoe Perches static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1373e3192690SJoe Perches 			       char __user *optval, int __user *optlen)
1374fd558d18SJames Chapman {
1375fd558d18SJames Chapman 	struct sock *sk = sock->sk;
1376fd558d18SJames Chapman 	struct l2tp_session *session;
1377fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1378fd558d18SJames Chapman 	int val, len;
1379fd558d18SJames Chapman 	int err;
1380fd558d18SJames Chapman 
1381fd558d18SJames Chapman 	if (level != SOL_PPPOL2TP)
13823cf521f7SSasha Levin 		return -EINVAL;
1383fd558d18SJames Chapman 
1384e3192690SJoe Perches 	if (get_user(len, optlen))
1385fd558d18SJames Chapman 		return -EFAULT;
1386fd558d18SJames Chapman 
1387fd558d18SJames Chapman 	len = min_t(unsigned int, len, sizeof(int));
1388fd558d18SJames Chapman 
1389fd558d18SJames Chapman 	if (len < 0)
1390fd558d18SJames Chapman 		return -EINVAL;
1391fd558d18SJames Chapman 
1392fd558d18SJames Chapman 	err = -ENOTCONN;
1393fd558d18SJames Chapman 	if (sk->sk_user_data == NULL)
1394fd558d18SJames Chapman 		goto end;
1395fd558d18SJames Chapman 
1396fd558d18SJames Chapman 	/* Get the session context */
1397fd558d18SJames Chapman 	err = -EBADF;
1398fd558d18SJames Chapman 	session = pppol2tp_sock_to_session(sk);
1399fd558d18SJames Chapman 	if (session == NULL)
1400fd558d18SJames Chapman 		goto end;
1401fd558d18SJames Chapman 
1402fd558d18SJames Chapman 	/* Special case: if session_id == 0x0000, treat as operation on tunnel */
1403fd558d18SJames Chapman 	if ((session->session_id == 0) &&
1404fd558d18SJames Chapman 	    (session->peer_session_id == 0)) {
14057198c77aSGuillaume Nault 		tunnel = session->tunnel;
1406fd558d18SJames Chapman 		err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1407321a52a3SGuillaume Nault 		if (err)
1408321a52a3SGuillaume Nault 			goto end_put_sess;
1409321a52a3SGuillaume Nault 	} else {
1410fd558d18SJames Chapman 		err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1411321a52a3SGuillaume Nault 		if (err)
1412321a52a3SGuillaume Nault 			goto end_put_sess;
1413321a52a3SGuillaume Nault 	}
1414fd558d18SJames Chapman 
1415fd558d18SJames Chapman 	err = -EFAULT;
1416e3192690SJoe Perches 	if (put_user(len, optlen))
1417fd558d18SJames Chapman 		goto end_put_sess;
1418fd558d18SJames Chapman 
1419fd558d18SJames Chapman 	if (copy_to_user((void __user *) optval, &val, len))
1420fd558d18SJames Chapman 		goto end_put_sess;
1421fd558d18SJames Chapman 
1422fd558d18SJames Chapman 	err = 0;
1423fd558d18SJames Chapman 
1424fd558d18SJames Chapman end_put_sess:
1425fd558d18SJames Chapman 	sock_put(sk);
1426fd558d18SJames Chapman end:
1427fd558d18SJames Chapman 	return err;
1428fd558d18SJames Chapman }
1429fd558d18SJames Chapman 
1430fd558d18SJames Chapman /*****************************************************************************
1431fd558d18SJames Chapman  * /proc filesystem for debug
1432f7faffa3SJames Chapman  * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1433f7faffa3SJames Chapman  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1434fd558d18SJames Chapman  *****************************************************************************/
1435fd558d18SJames Chapman 
1436fd558d18SJames Chapman static unsigned int pppol2tp_net_id;
1437fd558d18SJames Chapman 
1438fd558d18SJames Chapman #ifdef CONFIG_PROC_FS
1439fd558d18SJames Chapman 
1440fd558d18SJames Chapman struct pppol2tp_seq_data {
1441fd558d18SJames Chapman 	struct seq_net_private p;
1442fd558d18SJames Chapman 	int tunnel_idx;			/* current tunnel */
1443fd558d18SJames Chapman 	int session_idx;		/* index of session within current tunnel */
1444fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1445fd558d18SJames Chapman 	struct l2tp_session *session;	/* NULL means get next tunnel */
1446fd558d18SJames Chapman };
1447fd558d18SJames Chapman 
1448fd558d18SJames Chapman static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1449fd558d18SJames Chapman {
14500e0c3feeSGuillaume Nault 	/* Drop reference taken during previous invocation */
14510e0c3feeSGuillaume Nault 	if (pd->tunnel)
14520e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
14530e0c3feeSGuillaume Nault 
1454f7faffa3SJames Chapman 	for (;;) {
14550e0c3feeSGuillaume Nault 		pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
1456fd558d18SJames Chapman 		pd->tunnel_idx++;
1457f7faffa3SJames Chapman 
14580e0c3feeSGuillaume Nault 		/* Only accept L2TPv2 tunnels */
14590e0c3feeSGuillaume Nault 		if (!pd->tunnel || pd->tunnel->version == 2)
14600e0c3feeSGuillaume Nault 			return;
1461f7faffa3SJames Chapman 
14620e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
1463f7faffa3SJames Chapman 	}
1464fd558d18SJames Chapman }
1465fd558d18SJames Chapman 
1466fd558d18SJames Chapman static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1467fd558d18SJames Chapman {
146883494407SGuillaume Nault 	/* Drop reference taken during previous invocation */
146983494407SGuillaume Nault 	if (pd->session)
147083494407SGuillaume Nault 		l2tp_session_dec_refcount(pd->session);
147183494407SGuillaume Nault 
1472a4346210SGuillaume Nault 	pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
1473fd558d18SJames Chapman 	pd->session_idx++;
1474f7faffa3SJames Chapman 
1475fd558d18SJames Chapman 	if (pd->session == NULL) {
1476fd558d18SJames Chapman 		pd->session_idx = 0;
1477fd558d18SJames Chapman 		pppol2tp_next_tunnel(net, pd);
1478fd558d18SJames Chapman 	}
1479fd558d18SJames Chapman }
1480fd558d18SJames Chapman 
1481fd558d18SJames Chapman static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1482fd558d18SJames Chapman {
1483fd558d18SJames Chapman 	struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1484fd558d18SJames Chapman 	loff_t pos = *offs;
1485fd558d18SJames Chapman 	struct net *net;
1486fd558d18SJames Chapman 
1487fd558d18SJames Chapman 	if (!pos)
1488fd558d18SJames Chapman 		goto out;
1489fd558d18SJames Chapman 
1490fd558d18SJames Chapman 	BUG_ON(m->private == NULL);
1491fd558d18SJames Chapman 	pd = m->private;
1492fd558d18SJames Chapman 	net = seq_file_net(m);
1493fd558d18SJames Chapman 
1494fd558d18SJames Chapman 	if (pd->tunnel == NULL)
1495fd558d18SJames Chapman 		pppol2tp_next_tunnel(net, pd);
1496fd558d18SJames Chapman 	else
1497fd558d18SJames Chapman 		pppol2tp_next_session(net, pd);
1498fd558d18SJames Chapman 
1499fd558d18SJames Chapman 	/* NULL tunnel and session indicates end of list */
1500fd558d18SJames Chapman 	if ((pd->tunnel == NULL) && (pd->session == NULL))
1501fd558d18SJames Chapman 		pd = NULL;
1502fd558d18SJames Chapman 
1503fd558d18SJames Chapman out:
1504fd558d18SJames Chapman 	return pd;
1505fd558d18SJames Chapman }
1506fd558d18SJames Chapman 
1507fd558d18SJames Chapman static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1508fd558d18SJames Chapman {
1509fd558d18SJames Chapman 	(*pos)++;
1510fd558d18SJames Chapman 	return NULL;
1511fd558d18SJames Chapman }
1512fd558d18SJames Chapman 
1513fd558d18SJames Chapman static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1514fd558d18SJames Chapman {
15150e0c3feeSGuillaume Nault 	struct pppol2tp_seq_data *pd = v;
15160e0c3feeSGuillaume Nault 
15170e0c3feeSGuillaume Nault 	if (!pd || pd == SEQ_START_TOKEN)
15180e0c3feeSGuillaume Nault 		return;
15190e0c3feeSGuillaume Nault 
152083494407SGuillaume Nault 	/* Drop reference taken by last invocation of pppol2tp_next_session()
152183494407SGuillaume Nault 	 * or pppol2tp_next_tunnel().
152283494407SGuillaume Nault 	 */
152383494407SGuillaume Nault 	if (pd->session) {
152483494407SGuillaume Nault 		l2tp_session_dec_refcount(pd->session);
152583494407SGuillaume Nault 		pd->session = NULL;
152683494407SGuillaume Nault 	}
15275411b618SGuillaume Nault 	if (pd->tunnel) {
15280e0c3feeSGuillaume Nault 		l2tp_tunnel_dec_refcount(pd->tunnel);
15295411b618SGuillaume Nault 		pd->tunnel = NULL;
15305411b618SGuillaume Nault 	}
1531fd558d18SJames Chapman }
1532fd558d18SJames Chapman 
1533fd558d18SJames Chapman static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1534fd558d18SJames Chapman {
1535fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = v;
1536fd558d18SJames Chapman 
1537fd558d18SJames Chapman 	seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1538fd558d18SJames Chapman 		   tunnel->name,
1539fd558d18SJames Chapman 		   (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1540fbea9e07SReshetova, Elena 		   refcount_read(&tunnel->ref_count) - 1);
15417b7c0719STom Parkin 	seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1542fd558d18SJames Chapman 		   tunnel->debug,
15437b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_packets),
15447b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_bytes),
15457b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.tx_errors),
15467b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_packets),
15477b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_bytes),
15487b7c0719STom Parkin 		   atomic_long_read(&tunnel->stats.rx_errors));
1549fd558d18SJames Chapman }
1550fd558d18SJames Chapman 
1551fd558d18SJames Chapman static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1552fd558d18SJames Chapman {
1553fd558d18SJames Chapman 	struct l2tp_session *session = v;
1554fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1555ee40fb2eSGuillaume Nault 	unsigned char state;
1556ee40fb2eSGuillaume Nault 	char user_data_ok;
1557ee40fb2eSGuillaume Nault 	struct sock *sk;
1558fd558d18SJames Chapman 	u32 ip = 0;
1559fd558d18SJames Chapman 	u16 port = 0;
1560fd558d18SJames Chapman 
1561fd558d18SJames Chapman 	if (tunnel->sock) {
1562fd558d18SJames Chapman 		struct inet_sock *inet = inet_sk(tunnel->sock);
1563fd558d18SJames Chapman 		ip = ntohl(inet->inet_saddr);
1564fd558d18SJames Chapman 		port = ntohs(inet->inet_sport);
1565fd558d18SJames Chapman 	}
1566fd558d18SJames Chapman 
1567ee40fb2eSGuillaume Nault 	sk = pppol2tp_session_get_sock(session);
1568ee40fb2eSGuillaume Nault 	if (sk) {
1569ee40fb2eSGuillaume Nault 		state = sk->sk_state;
1570ee40fb2eSGuillaume Nault 		user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1571ee40fb2eSGuillaume Nault 	} else {
1572ee40fb2eSGuillaume Nault 		state = 0;
1573ee40fb2eSGuillaume Nault 		user_data_ok = 'N';
1574ee40fb2eSGuillaume Nault 	}
1575ee40fb2eSGuillaume Nault 
1576fd558d18SJames Chapman 	seq_printf(m, "  SESSION '%s' %08X/%d %04X/%04X -> "
1577fd558d18SJames Chapman 		   "%04X/%04X %d %c\n",
1578fd558d18SJames Chapman 		   session->name, ip, port,
1579fd558d18SJames Chapman 		   tunnel->tunnel_id,
1580fd558d18SJames Chapman 		   session->session_id,
1581fd558d18SJames Chapman 		   tunnel->peer_tunnel_id,
1582fd558d18SJames Chapman 		   session->peer_session_id,
1583ee40fb2eSGuillaume Nault 		   state, user_data_ok);
1584789141b2SGuillaume Nault 	seq_printf(m, "   0/0/%c/%c/%s %08x %u\n",
1585fd558d18SJames Chapman 		   session->recv_seq ? 'R' : '-',
1586fd558d18SJames Chapman 		   session->send_seq ? 'S' : '-',
1587fd558d18SJames Chapman 		   session->lns_mode ? "LNS" : "LAC",
1588fd558d18SJames Chapman 		   session->debug,
1589fd558d18SJames Chapman 		   jiffies_to_msecs(session->reorder_timeout));
15907b7c0719STom Parkin 	seq_printf(m, "   %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
1591fd558d18SJames Chapman 		   session->nr, session->ns,
15927b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_packets),
15937b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_bytes),
15947b7c0719STom Parkin 		   atomic_long_read(&session->stats.tx_errors),
15957b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_packets),
15967b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_bytes),
15977b7c0719STom Parkin 		   atomic_long_read(&session->stats.rx_errors));
15989345471bSJames Chapman 
1599ee40fb2eSGuillaume Nault 	if (sk) {
1600ee40fb2eSGuillaume Nault 		struct pppox_sock *po = pppox_sk(sk);
1601ee40fb2eSGuillaume Nault 
16029345471bSJames Chapman 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
1603ee40fb2eSGuillaume Nault 		sock_put(sk);
1604ee40fb2eSGuillaume Nault 	}
1605fd558d18SJames Chapman }
1606fd558d18SJames Chapman 
1607fd558d18SJames Chapman static int pppol2tp_seq_show(struct seq_file *m, void *v)
1608fd558d18SJames Chapman {
1609fd558d18SJames Chapman 	struct pppol2tp_seq_data *pd = v;
1610fd558d18SJames Chapman 
1611fd558d18SJames Chapman 	/* display header on line 1 */
1612fd558d18SJames Chapman 	if (v == SEQ_START_TOKEN) {
1613fd558d18SJames Chapman 		seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1614fd558d18SJames Chapman 		seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1615fd558d18SJames Chapman 		seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1616fd558d18SJames Chapman 		seq_puts(m, "  SESSION name, addr/port src-tid/sid "
1617fd558d18SJames Chapman 			 "dest-tid/sid state user-data-ok\n");
1618fd558d18SJames Chapman 		seq_puts(m, "   mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1619fd558d18SJames Chapman 		seq_puts(m, "   nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1620fd558d18SJames Chapman 		goto out;
1621fd558d18SJames Chapman 	}
1622fd558d18SJames Chapman 
162383494407SGuillaume Nault 	if (!pd->session)
1624fd558d18SJames Chapman 		pppol2tp_seq_tunnel_show(m, pd->tunnel);
162583494407SGuillaume Nault 	else
1626fd558d18SJames Chapman 		pppol2tp_seq_session_show(m, pd->session);
1627fd558d18SJames Chapman 
1628fd558d18SJames Chapman out:
1629fd558d18SJames Chapman 	return 0;
1630fd558d18SJames Chapman }
1631fd558d18SJames Chapman 
1632fd558d18SJames Chapman static const struct seq_operations pppol2tp_seq_ops = {
1633fd558d18SJames Chapman 	.start		= pppol2tp_seq_start,
1634fd558d18SJames Chapman 	.next		= pppol2tp_seq_next,
1635fd558d18SJames Chapman 	.stop		= pppol2tp_seq_stop,
1636fd558d18SJames Chapman 	.show		= pppol2tp_seq_show,
1637fd558d18SJames Chapman };
1638fd558d18SJames Chapman #endif /* CONFIG_PROC_FS */
1639fd558d18SJames Chapman 
1640fd558d18SJames Chapman /*****************************************************************************
1641fd558d18SJames Chapman  * Network namespace
1642fd558d18SJames Chapman  *****************************************************************************/
1643fd558d18SJames Chapman 
1644fd558d18SJames Chapman static __net_init int pppol2tp_init_net(struct net *net)
1645fd558d18SJames Chapman {
1646fd558d18SJames Chapman 	struct proc_dir_entry *pde;
1647fd558d18SJames Chapman 	int err = 0;
1648fd558d18SJames Chapman 
1649c3506372SChristoph Hellwig 	pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1650c3506372SChristoph Hellwig 			&pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
1651fd558d18SJames Chapman 	if (!pde) {
1652fd558d18SJames Chapman 		err = -ENOMEM;
1653fd558d18SJames Chapman 		goto out;
1654fd558d18SJames Chapman 	}
1655fd558d18SJames Chapman 
1656fd558d18SJames Chapman out:
1657fd558d18SJames Chapman 	return err;
1658fd558d18SJames Chapman }
1659fd558d18SJames Chapman 
1660fd558d18SJames Chapman static __net_exit void pppol2tp_exit_net(struct net *net)
1661fd558d18SJames Chapman {
1662ece31ffdSGao feng 	remove_proc_entry("pppol2tp", net->proc_net);
1663fd558d18SJames Chapman }
1664fd558d18SJames Chapman 
1665fd558d18SJames Chapman static struct pernet_operations pppol2tp_net_ops = {
1666fd558d18SJames Chapman 	.init = pppol2tp_init_net,
1667fd558d18SJames Chapman 	.exit = pppol2tp_exit_net,
1668fd558d18SJames Chapman 	.id   = &pppol2tp_net_id,
1669fd558d18SJames Chapman };
1670fd558d18SJames Chapman 
1671fd558d18SJames Chapman /*****************************************************************************
1672fd558d18SJames Chapman  * Init and cleanup
1673fd558d18SJames Chapman  *****************************************************************************/
1674fd558d18SJames Chapman 
1675fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops = {
1676fd558d18SJames Chapman 	.family		= AF_PPPOX,
1677fd558d18SJames Chapman 	.owner		= THIS_MODULE,
1678fd558d18SJames Chapman 	.release	= pppol2tp_release,
1679fd558d18SJames Chapman 	.bind		= sock_no_bind,
1680fd558d18SJames Chapman 	.connect	= pppol2tp_connect,
1681fd558d18SJames Chapman 	.socketpair	= sock_no_socketpair,
1682fd558d18SJames Chapman 	.accept		= sock_no_accept,
1683fd558d18SJames Chapman 	.getname	= pppol2tp_getname,
1684a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
1685fd558d18SJames Chapman 	.listen		= sock_no_listen,
1686fd558d18SJames Chapman 	.shutdown	= sock_no_shutdown,
1687fd558d18SJames Chapman 	.setsockopt	= pppol2tp_setsockopt,
1688fd558d18SJames Chapman 	.getsockopt	= pppol2tp_getsockopt,
1689fd558d18SJames Chapman 	.sendmsg	= pppol2tp_sendmsg,
1690fd558d18SJames Chapman 	.recvmsg	= pppol2tp_recvmsg,
1691fd558d18SJames Chapman 	.mmap		= sock_no_mmap,
1692fd558d18SJames Chapman 	.ioctl		= pppox_ioctl,
1693fd558d18SJames Chapman };
1694fd558d18SJames Chapman 
1695756e64a0SEric Dumazet static const struct pppox_proto pppol2tp_proto = {
1696fd558d18SJames Chapman 	.create		= pppol2tp_create,
1697e1558a93SWei Yongjun 	.ioctl		= pppol2tp_ioctl,
1698e1558a93SWei Yongjun 	.owner		= THIS_MODULE,
1699fd558d18SJames Chapman };
1700fd558d18SJames Chapman 
1701309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1702309795f4SJames Chapman 
1703309795f4SJames Chapman static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1704309795f4SJames Chapman 	.session_create	= pppol2tp_session_create,
1705cf2f5c88STom Parkin 	.session_delete	= l2tp_session_delete,
1706309795f4SJames Chapman };
1707309795f4SJames Chapman 
1708309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */
1709309795f4SJames Chapman 
1710fd558d18SJames Chapman static int __init pppol2tp_init(void)
1711fd558d18SJames Chapman {
1712fd558d18SJames Chapman 	int err;
1713fd558d18SJames Chapman 
1714fd558d18SJames Chapman 	err = register_pernet_device(&pppol2tp_net_ops);
1715fd558d18SJames Chapman 	if (err)
1716fd558d18SJames Chapman 		goto out;
1717fd558d18SJames Chapman 
1718fd558d18SJames Chapman 	err = proto_register(&pppol2tp_sk_proto, 0);
1719fd558d18SJames Chapman 	if (err)
1720fd558d18SJames Chapman 		goto out_unregister_pppol2tp_pernet;
1721fd558d18SJames Chapman 
1722fd558d18SJames Chapman 	err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1723fd558d18SJames Chapman 	if (err)
1724fd558d18SJames Chapman 		goto out_unregister_pppol2tp_proto;
1725fd558d18SJames Chapman 
1726309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1727309795f4SJames Chapman 	err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1728309795f4SJames Chapman 	if (err)
1729309795f4SJames Chapman 		goto out_unregister_pppox;
1730309795f4SJames Chapman #endif
1731309795f4SJames Chapman 
1732a4ca44faSJoe Perches 	pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1733fd558d18SJames Chapman 
1734fd558d18SJames Chapman out:
1735fd558d18SJames Chapman 	return err;
1736309795f4SJames Chapman 
1737309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1738309795f4SJames Chapman out_unregister_pppox:
1739309795f4SJames Chapman 	unregister_pppox_proto(PX_PROTO_OL2TP);
1740309795f4SJames Chapman #endif
1741fd558d18SJames Chapman out_unregister_pppol2tp_proto:
1742fd558d18SJames Chapman 	proto_unregister(&pppol2tp_sk_proto);
1743fd558d18SJames Chapman out_unregister_pppol2tp_pernet:
1744fd558d18SJames Chapman 	unregister_pernet_device(&pppol2tp_net_ops);
1745fd558d18SJames Chapman 	goto out;
1746fd558d18SJames Chapman }
1747fd558d18SJames Chapman 
1748fd558d18SJames Chapman static void __exit pppol2tp_exit(void)
1749fd558d18SJames Chapman {
1750309795f4SJames Chapman #ifdef CONFIG_L2TP_V3
1751309795f4SJames Chapman 	l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1752309795f4SJames Chapman #endif
1753fd558d18SJames Chapman 	unregister_pppox_proto(PX_PROTO_OL2TP);
1754fd558d18SJames Chapman 	proto_unregister(&pppol2tp_sk_proto);
1755fd558d18SJames Chapman 	unregister_pernet_device(&pppol2tp_net_ops);
1756fd558d18SJames Chapman }
1757fd558d18SJames Chapman 
1758fd558d18SJames Chapman module_init(pppol2tp_init);
1759fd558d18SJames Chapman module_exit(pppol2tp_exit);
1760fd558d18SJames Chapman 
1761fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1762fd558d18SJames Chapman MODULE_DESCRIPTION("PPP over L2TP over UDP");
1763fd558d18SJames Chapman MODULE_LICENSE("GPL");
1764fd558d18SJames Chapman MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1765681b4d88SGuillaume Nault MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1766249ee819SGuillaume Nault MODULE_ALIAS_L2TP_PWTYPE(7);
1767