1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*****************************************************************************
3  * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4  *
5  * PPPoX    --- Generic PPP encapsulation socket family
6  * PPPoL2TP --- PPP over L2TP (RFC 2661)
7  *
8  * Version:	2.0.0
9  *
10  * Authors:	James Chapman (jchapman@katalix.com)
11  *
12  * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
13  *
14  * License:
15  */
16 
17 /* This driver handles only L2TP data frames; control frames are handled by a
18  * userspace application.
19  *
20  * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
21  * attaches it to a bound UDP socket with local tunnel_id / session_id and
22  * peer tunnel_id / session_id set. Data can then be sent or received using
23  * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
24  * can be read or modified using ioctl() or [gs]etsockopt() calls.
25  *
26  * When a PPPoL2TP socket is connected with local and peer session_id values
27  * zero, the socket is treated as a special tunnel management socket.
28  *
29  * Here's example userspace code to create a socket for sending/receiving data
30  * over an L2TP session:-
31  *
32  *	struct sockaddr_pppol2tp sax;
33  *	int fd;
34  *	int session_fd;
35  *
36  *	fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
37  *
38  *	sax.sa_family = AF_PPPOX;
39  *	sax.sa_protocol = PX_PROTO_OL2TP;
40  *	sax.pppol2tp.fd = tunnel_fd;	// bound UDP socket
41  *	sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
42  *	sax.pppol2tp.addr.sin_port = addr->sin_port;
43  *	sax.pppol2tp.addr.sin_family = AF_INET;
44  *	sax.pppol2tp.s_tunnel  = tunnel_id;
45  *	sax.pppol2tp.s_session = session_id;
46  *	sax.pppol2tp.d_tunnel  = peer_tunnel_id;
47  *	sax.pppol2tp.d_session = peer_session_id;
48  *
49  *	session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
50  *
51  * A pppd plugin that allows PPP traffic to be carried over L2TP using
52  * this driver is available from the OpenL2TP project at
53  * http://openl2tp.sourceforge.net.
54  */
55 
56 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
57 
58 #include <linux/module.h>
59 #include <linux/string.h>
60 #include <linux/list.h>
61 #include <linux/uaccess.h>
62 
63 #include <linux/kernel.h>
64 #include <linux/spinlock.h>
65 #include <linux/kthread.h>
66 #include <linux/sched.h>
67 #include <linux/slab.h>
68 #include <linux/errno.h>
69 #include <linux/jiffies.h>
70 
71 #include <linux/netdevice.h>
72 #include <linux/net.h>
73 #include <linux/inetdevice.h>
74 #include <linux/skbuff.h>
75 #include <linux/init.h>
76 #include <linux/ip.h>
77 #include <linux/udp.h>
78 #include <linux/if_pppox.h>
79 #include <linux/if_pppol2tp.h>
80 #include <net/sock.h>
81 #include <linux/ppp_channel.h>
82 #include <linux/ppp_defs.h>
83 #include <linux/ppp-ioctl.h>
84 #include <linux/file.h>
85 #include <linux/hash.h>
86 #include <linux/sort.h>
87 #include <linux/proc_fs.h>
88 #include <linux/l2tp.h>
89 #include <linux/nsproxy.h>
90 #include <net/net_namespace.h>
91 #include <net/netns/generic.h>
92 #include <net/ip.h>
93 #include <net/udp.h>
94 #include <net/inet_common.h>
95 
96 #include <asm/byteorder.h>
97 #include <linux/atomic.h>
98 
99 #include "l2tp_core.h"
100 
101 #define PPPOL2TP_DRV_VERSION	"V2.0"
102 
103 /* Space for UDP, L2TP and PPP headers */
104 #define PPPOL2TP_HEADER_OVERHEAD	40
105 
106 /* Number of bytes to build transmit L2TP headers.
107  * Unfortunately the size is different depending on whether sequence numbers
108  * are enabled.
109  */
110 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ		10
111 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ		6
112 
113 /* Private data of each session. This data lives at the end of struct
114  * l2tp_session, referenced via session->priv[].
115  */
116 struct pppol2tp_session {
117 	int			owner;		/* pid that opened the socket */
118 
119 	struct mutex		sk_lock;	/* Protects .sk */
120 	struct sock __rcu	*sk;		/* Pointer to the session PPPoX socket */
121 	struct sock		*__sk;		/* Copy of .sk, for cleanup */
122 };
123 
124 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
125 
126 static const struct ppp_channel_ops pppol2tp_chan_ops = {
127 	.start_xmit =  pppol2tp_xmit,
128 };
129 
130 static const struct proto_ops pppol2tp_ops;
131 
132 /* Retrieves the pppol2tp socket associated to a session. */
pppol2tp_session_get_sock(struct l2tp_session * session)133 static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
134 {
135 	struct pppol2tp_session *ps = l2tp_session_priv(session);
136 
137 	return rcu_dereference(ps->sk);
138 }
139 
140 /* Helpers to obtain tunnel/session contexts from sockets.
141  */
pppol2tp_sock_to_session(struct sock * sk)142 static struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
143 {
144 	struct l2tp_session *session;
145 
146 	if (!sk)
147 		return NULL;
148 
149 	rcu_read_lock();
150 	session = rcu_dereference_sk_user_data(sk);
151 	if (session && refcount_inc_not_zero(&session->ref_count)) {
152 		rcu_read_unlock();
153 		WARN_ON_ONCE(session->magic != L2TP_SESSION_MAGIC);
154 		return session;
155 	}
156 	rcu_read_unlock();
157 
158 	return NULL;
159 }
160 
161 /*****************************************************************************
162  * Receive data handling
163  *****************************************************************************/
164 
165 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
166  */
pppol2tp_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)167 static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
168 			    size_t len, int flags)
169 {
170 	int err;
171 	struct sk_buff *skb;
172 	struct sock *sk = sock->sk;
173 
174 	err = -EIO;
175 	if (sk->sk_state & PPPOX_BOUND)
176 		goto end;
177 
178 	err = 0;
179 	skb = skb_recv_datagram(sk, flags, &err);
180 	if (!skb)
181 		goto end;
182 
183 	if (len > skb->len)
184 		len = skb->len;
185 	else if (len < skb->len)
186 		msg->msg_flags |= MSG_TRUNC;
187 
188 	err = skb_copy_datagram_msg(skb, 0, msg, len);
189 	if (likely(err == 0))
190 		err = len;
191 
192 	kfree_skb(skb);
193 end:
194 	return err;
195 }
196 
pppol2tp_recv(struct l2tp_session * session,struct sk_buff * skb,int data_len)197 static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
198 {
199 	struct sock *sk;
200 
201 	/* If the socket is bound, send it in to PPP's input queue. Otherwise
202 	 * queue it on the session socket.
203 	 */
204 	rcu_read_lock();
205 	sk = pppol2tp_session_get_sock(session);
206 	if (!sk)
207 		goto no_sock;
208 
209 	/* If the first two bytes are 0xFF03, consider that it is the PPP's
210 	 * Address and Control fields and skip them. The L2TP module has always
211 	 * worked this way, although, in theory, the use of these fields should
212 	 * be negotiated and handled at the PPP layer. These fields are
213 	 * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
214 	 * Information command with Poll/Final bit set to zero (RFC 1662).
215 	 */
216 	if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
217 	    skb->data[1] == PPP_UI)
218 		skb_pull(skb, 2);
219 
220 	if (sk->sk_state & PPPOX_BOUND) {
221 		struct pppox_sock *po;
222 
223 		po = pppox_sk(sk);
224 		ppp_input(&po->chan, skb);
225 	} else {
226 		if (sock_queue_rcv_skb(sk, skb) < 0) {
227 			atomic_long_inc(&session->stats.rx_errors);
228 			kfree_skb(skb);
229 		}
230 	}
231 	rcu_read_unlock();
232 
233 	return;
234 
235 no_sock:
236 	rcu_read_unlock();
237 	pr_warn_ratelimited("%s: no socket in recv\n", session->name);
238 	kfree_skb(skb);
239 }
240 
241 /************************************************************************
242  * Transmit handling
243  ***********************************************************************/
244 
245 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket.  We come here
246  * when a user application does a sendmsg() on the session socket. L2TP and
247  * PPP headers must be inserted into the user's data.
248  */
pppol2tp_sendmsg(struct socket * sock,struct msghdr * m,size_t total_len)249 static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
250 			    size_t total_len)
251 {
252 	struct sock *sk = sock->sk;
253 	struct sk_buff *skb;
254 	int error;
255 	struct l2tp_session *session;
256 	struct l2tp_tunnel *tunnel;
257 	int uhlen;
258 
259 	error = -ENOTCONN;
260 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
261 		goto error;
262 
263 	/* Get session and tunnel contexts */
264 	error = -EBADF;
265 	session = pppol2tp_sock_to_session(sk);
266 	if (!session)
267 		goto error;
268 
269 	tunnel = session->tunnel;
270 
271 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
272 
273 	/* Allocate a socket buffer */
274 	error = -ENOMEM;
275 	skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
276 			   uhlen + session->hdr_len +
277 			   2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
278 			   0, GFP_KERNEL);
279 	if (!skb)
280 		goto error_put_sess;
281 
282 	/* Reserve space for headers. */
283 	skb_reserve(skb, NET_SKB_PAD);
284 	skb_reset_network_header(skb);
285 	skb_reserve(skb, sizeof(struct iphdr));
286 	skb_reset_transport_header(skb);
287 	skb_reserve(skb, uhlen);
288 
289 	/* Add PPP header */
290 	skb->data[0] = PPP_ALLSTATIONS;
291 	skb->data[1] = PPP_UI;
292 	skb_put(skb, 2);
293 
294 	/* Copy user data into skb */
295 	error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
296 	if (error < 0) {
297 		kfree_skb(skb);
298 		goto error_put_sess;
299 	}
300 
301 	local_bh_disable();
302 	l2tp_xmit_skb(session, skb);
303 	local_bh_enable();
304 
305 	l2tp_session_put(session);
306 
307 	return total_len;
308 
309 error_put_sess:
310 	l2tp_session_put(session);
311 error:
312 	return error;
313 }
314 
315 /* Transmit function called by generic PPP driver.  Sends PPP frame
316  * over PPPoL2TP socket.
317  *
318  * This is almost the same as pppol2tp_sendmsg(), but rather than
319  * being called with a msghdr from userspace, it is called with a skb
320  * from the kernel.
321  *
322  * The supplied skb from ppp doesn't have enough headroom for the
323  * insertion of L2TP, UDP and IP headers so we need to allocate more
324  * headroom in the skb. This will create a cloned skb. But we must be
325  * careful in the error case because the caller will expect to free
326  * the skb it supplied, not our cloned skb. So we take care to always
327  * leave the original skb unfreed if we return an error.
328  */
pppol2tp_xmit(struct ppp_channel * chan,struct sk_buff * skb)329 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
330 {
331 	struct sock *sk = (struct sock *)chan->private;
332 	struct l2tp_session *session;
333 	struct l2tp_tunnel *tunnel;
334 	int uhlen, headroom;
335 
336 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
337 		goto abort;
338 
339 	/* Get session and tunnel contexts from the socket */
340 	session = pppol2tp_sock_to_session(sk);
341 	if (!session)
342 		goto abort;
343 
344 	tunnel = session->tunnel;
345 
346 	uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
347 	headroom = NET_SKB_PAD +
348 		   sizeof(struct iphdr) + /* IP header */
349 		   uhlen +		/* UDP header (if L2TP_ENCAPTYPE_UDP) */
350 		   session->hdr_len +	/* L2TP header */
351 		   2;			/* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
352 	if (skb_cow_head(skb, headroom))
353 		goto abort_put_sess;
354 
355 	/* Setup PPP header */
356 	__skb_push(skb, 2);
357 	skb->data[0] = PPP_ALLSTATIONS;
358 	skb->data[1] = PPP_UI;
359 
360 	local_bh_disable();
361 	l2tp_xmit_skb(session, skb);
362 	local_bh_enable();
363 
364 	l2tp_session_put(session);
365 
366 	return 1;
367 
368 abort_put_sess:
369 	l2tp_session_put(session);
370 abort:
371 	/* Free the original skb */
372 	kfree_skb(skb);
373 	return 1;
374 }
375 
376 /*****************************************************************************
377  * Session (and tunnel control) socket create/destroy.
378  *****************************************************************************/
379 
380 /* Really kill the session socket. (Called from sock_put() if
381  * refcnt == 0.)
382  */
pppol2tp_session_destruct(struct sock * sk)383 static void pppol2tp_session_destruct(struct sock *sk)
384 {
385 	skb_queue_purge(&sk->sk_receive_queue);
386 	skb_queue_purge(&sk->sk_write_queue);
387 }
388 
pppol2tp_session_close(struct l2tp_session * session)389 static void pppol2tp_session_close(struct l2tp_session *session)
390 {
391 	struct pppol2tp_session *ps;
392 
393 	ps = l2tp_session_priv(session);
394 	mutex_lock(&ps->sk_lock);
395 	ps->__sk = rcu_dereference_protected(ps->sk,
396 					     lockdep_is_held(&ps->sk_lock));
397 	RCU_INIT_POINTER(ps->sk, NULL);
398 	mutex_unlock(&ps->sk_lock);
399 	if (ps->__sk) {
400 		/* detach socket */
401 		rcu_assign_sk_user_data(ps->__sk, NULL);
402 		sock_put(ps->__sk);
403 
404 		/* drop ref taken when we referenced socket via sk_user_data */
405 		l2tp_session_put(session);
406 	}
407 }
408 
409 /* Called when the PPPoX socket (session) is closed.
410  */
pppol2tp_release(struct socket * sock)411 static int pppol2tp_release(struct socket *sock)
412 {
413 	struct sock *sk = sock->sk;
414 	struct l2tp_session *session;
415 	int error;
416 
417 	if (!sk)
418 		return 0;
419 
420 	error = -EBADF;
421 	lock_sock(sk);
422 	if (sock_flag(sk, SOCK_DEAD) != 0)
423 		goto error;
424 
425 	pppox_unbind_sock(sk);
426 
427 	/* Signal the death of the socket. */
428 	sk->sk_state = PPPOX_DEAD;
429 	sock_orphan(sk);
430 	sock->sk = NULL;
431 
432 	session = pppol2tp_sock_to_session(sk);
433 	if (session) {
434 		l2tp_session_delete(session);
435 		/* drop ref taken by pppol2tp_sock_to_session */
436 		l2tp_session_put(session);
437 	}
438 
439 	release_sock(sk);
440 
441 	sock_put(sk);
442 
443 	return 0;
444 
445 error:
446 	release_sock(sk);
447 	return error;
448 }
449 
450 static struct proto pppol2tp_sk_proto = {
451 	.name	  = "PPPOL2TP",
452 	.owner	  = THIS_MODULE,
453 	.obj_size = sizeof(struct pppox_sock),
454 };
455 
pppol2tp_backlog_recv(struct sock * sk,struct sk_buff * skb)456 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
457 {
458 	int rc;
459 
460 	rc = l2tp_udp_encap_recv(sk, skb);
461 	if (rc)
462 		kfree_skb(skb);
463 
464 	return NET_RX_SUCCESS;
465 }
466 
467 /* socket() handler. Initialize a new struct sock.
468  */
pppol2tp_create(struct net * net,struct socket * sock,int kern)469 static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
470 {
471 	int error = -ENOMEM;
472 	struct sock *sk;
473 
474 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
475 	if (!sk)
476 		goto out;
477 
478 	sock_init_data(sock, sk);
479 	sock_set_flag(sk, SOCK_RCU_FREE);
480 
481 	sock->state  = SS_UNCONNECTED;
482 	sock->ops    = &pppol2tp_ops;
483 
484 	sk->sk_backlog_rcv = pppol2tp_backlog_recv;
485 	sk->sk_protocol	   = PX_PROTO_OL2TP;
486 	sk->sk_family	   = PF_PPPOX;
487 	sk->sk_state	   = PPPOX_NONE;
488 	sk->sk_type	   = SOCK_STREAM;
489 	sk->sk_destruct	   = pppol2tp_session_destruct;
490 
491 	error = 0;
492 
493 out:
494 	return error;
495 }
496 
pppol2tp_show(struct seq_file * m,void * arg)497 static void pppol2tp_show(struct seq_file *m, void *arg)
498 {
499 	struct l2tp_session *session = arg;
500 	struct sock *sk;
501 
502 	rcu_read_lock();
503 	sk = pppol2tp_session_get_sock(session);
504 	if (sk) {
505 		struct pppox_sock *po = pppox_sk(sk);
506 
507 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
508 	}
509 	rcu_read_unlock();
510 }
511 
pppol2tp_session_init(struct l2tp_session * session)512 static void pppol2tp_session_init(struct l2tp_session *session)
513 {
514 	struct pppol2tp_session *ps;
515 
516 	session->recv_skb = pppol2tp_recv;
517 	session->session_close = pppol2tp_session_close;
518 	if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
519 		session->show = pppol2tp_show;
520 
521 	ps = l2tp_session_priv(session);
522 	mutex_init(&ps->sk_lock);
523 	ps->owner = current->pid;
524 }
525 
526 struct l2tp_connect_info {
527 	u8 version;
528 	int fd;
529 	u32 tunnel_id;
530 	u32 peer_tunnel_id;
531 	u32 session_id;
532 	u32 peer_session_id;
533 };
534 
pppol2tp_sockaddr_get_info(const void * sa,int sa_len,struct l2tp_connect_info * info)535 static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
536 				      struct l2tp_connect_info *info)
537 {
538 	switch (sa_len) {
539 	case sizeof(struct sockaddr_pppol2tp):
540 	{
541 		const struct sockaddr_pppol2tp *sa_v2in4 = sa;
542 
543 		if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
544 			return -EINVAL;
545 
546 		info->version = 2;
547 		info->fd = sa_v2in4->pppol2tp.fd;
548 		info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
549 		info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
550 		info->session_id = sa_v2in4->pppol2tp.s_session;
551 		info->peer_session_id = sa_v2in4->pppol2tp.d_session;
552 
553 		break;
554 	}
555 	case sizeof(struct sockaddr_pppol2tpv3):
556 	{
557 		const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
558 
559 		if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
560 			return -EINVAL;
561 
562 		info->version = 3;
563 		info->fd = sa_v3in4->pppol2tp.fd;
564 		info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
565 		info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
566 		info->session_id = sa_v3in4->pppol2tp.s_session;
567 		info->peer_session_id = sa_v3in4->pppol2tp.d_session;
568 
569 		break;
570 	}
571 	case sizeof(struct sockaddr_pppol2tpin6):
572 	{
573 		const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
574 
575 		if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
576 			return -EINVAL;
577 
578 		info->version = 2;
579 		info->fd = sa_v2in6->pppol2tp.fd;
580 		info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
581 		info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
582 		info->session_id = sa_v2in6->pppol2tp.s_session;
583 		info->peer_session_id = sa_v2in6->pppol2tp.d_session;
584 
585 		break;
586 	}
587 	case sizeof(struct sockaddr_pppol2tpv3in6):
588 	{
589 		const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
590 
591 		if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
592 			return -EINVAL;
593 
594 		info->version = 3;
595 		info->fd = sa_v3in6->pppol2tp.fd;
596 		info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
597 		info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
598 		info->session_id = sa_v3in6->pppol2tp.s_session;
599 		info->peer_session_id = sa_v3in6->pppol2tp.d_session;
600 
601 		break;
602 	}
603 	default:
604 		return -EINVAL;
605 	}
606 
607 	return 0;
608 }
609 
610 /* Rough estimation of the maximum payload size a tunnel can transmit without
611  * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
612  * numbers and no IP option. Not quite accurate, but the result is mostly
613  * unused anyway.
614  */
pppol2tp_tunnel_mtu(const struct l2tp_tunnel * tunnel)615 static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
616 {
617 	int mtu;
618 
619 	mtu = l2tp_tunnel_dst_mtu(tunnel);
620 	if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
621 		return 1500 - PPPOL2TP_HEADER_OVERHEAD;
622 
623 	return mtu - PPPOL2TP_HEADER_OVERHEAD;
624 }
625 
pppol2tp_tunnel_get(struct net * net,const struct l2tp_connect_info * info,bool * new_tunnel)626 static struct l2tp_tunnel *pppol2tp_tunnel_get(struct net *net,
627 					       const struct l2tp_connect_info *info,
628 					       bool *new_tunnel)
629 {
630 	struct l2tp_tunnel *tunnel;
631 	int error;
632 
633 	*new_tunnel = false;
634 
635 	tunnel = l2tp_tunnel_get(net, info->tunnel_id);
636 
637 	/* Special case: create tunnel context if session_id and
638 	 * peer_session_id is 0. Otherwise look up tunnel using supplied
639 	 * tunnel id.
640 	 */
641 	if (!info->session_id && !info->peer_session_id) {
642 		if (!tunnel) {
643 			struct l2tp_tunnel_cfg tcfg = {
644 				.encap = L2TP_ENCAPTYPE_UDP,
645 			};
646 
647 			/* Prevent l2tp_tunnel_register() from trying to set up
648 			 * a kernel socket.
649 			 */
650 			if (info->fd < 0)
651 				return ERR_PTR(-EBADF);
652 
653 			error = l2tp_tunnel_create(info->fd,
654 						   info->version,
655 						   info->tunnel_id,
656 						   info->peer_tunnel_id, &tcfg,
657 						   &tunnel);
658 			if (error < 0)
659 				return ERR_PTR(error);
660 
661 			refcount_inc(&tunnel->ref_count);
662 			error = l2tp_tunnel_register(tunnel, net, &tcfg);
663 			if (error < 0) {
664 				kfree(tunnel);
665 				return ERR_PTR(error);
666 			}
667 
668 			*new_tunnel = true;
669 		}
670 	} else {
671 		/* Error if we can't find the tunnel */
672 		if (!tunnel)
673 			return ERR_PTR(-ENOENT);
674 
675 		/* Error if socket is not prepped */
676 		if (!tunnel->sock) {
677 			l2tp_tunnel_put(tunnel);
678 			return ERR_PTR(-ENOENT);
679 		}
680 	}
681 
682 	return tunnel;
683 }
684 
685 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
686  */
pppol2tp_connect(struct socket * sock,struct sockaddr * uservaddr,int sockaddr_len,int flags)687 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
688 			    int sockaddr_len, int flags)
689 {
690 	struct sock *sk = sock->sk;
691 	struct pppox_sock *po = pppox_sk(sk);
692 	struct l2tp_session *session = NULL;
693 	struct l2tp_connect_info info;
694 	struct l2tp_tunnel *tunnel;
695 	struct pppol2tp_session *ps;
696 	struct l2tp_session_cfg cfg = { 0, };
697 	bool drop_refcnt = false;
698 	bool new_session = false;
699 	bool new_tunnel = false;
700 	int error;
701 
702 	error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
703 	if (error < 0)
704 		return error;
705 
706 	/* Don't bind if tunnel_id is 0 */
707 	if (!info.tunnel_id)
708 		return -EINVAL;
709 
710 	tunnel = pppol2tp_tunnel_get(sock_net(sk), &info, &new_tunnel);
711 	if (IS_ERR(tunnel))
712 		return PTR_ERR(tunnel);
713 
714 	lock_sock(sk);
715 
716 	/* Check for already bound sockets */
717 	error = -EBUSY;
718 	if (sk->sk_state & PPPOX_CONNECTED)
719 		goto end;
720 
721 	/* We don't supporting rebinding anyway */
722 	error = -EALREADY;
723 	if (sk->sk_user_data)
724 		goto end; /* socket is already attached */
725 
726 	if (tunnel->peer_tunnel_id == 0)
727 		tunnel->peer_tunnel_id = info.peer_tunnel_id;
728 
729 	session = l2tp_session_get(sock_net(sk), tunnel->sock, tunnel->version,
730 				   info.tunnel_id, info.session_id);
731 	if (session) {
732 		drop_refcnt = true;
733 
734 		if (session->pwtype != L2TP_PWTYPE_PPP) {
735 			error = -EPROTOTYPE;
736 			goto end;
737 		}
738 
739 		ps = l2tp_session_priv(session);
740 
741 		/* Using a pre-existing session is fine as long as it hasn't
742 		 * been connected yet.
743 		 */
744 		mutex_lock(&ps->sk_lock);
745 		if (rcu_dereference_protected(ps->sk,
746 					      lockdep_is_held(&ps->sk_lock)) ||
747 		    ps->__sk) {
748 			mutex_unlock(&ps->sk_lock);
749 			error = -EEXIST;
750 			goto end;
751 		}
752 	} else {
753 		cfg.pw_type = L2TP_PWTYPE_PPP;
754 
755 		session = l2tp_session_create(sizeof(struct pppol2tp_session),
756 					      tunnel, info.session_id,
757 					      info.peer_session_id, &cfg);
758 		if (IS_ERR(session)) {
759 			error = PTR_ERR(session);
760 			goto end;
761 		}
762 
763 		drop_refcnt = true;
764 
765 		pppol2tp_session_init(session);
766 		ps = l2tp_session_priv(session);
767 		refcount_inc(&session->ref_count);
768 
769 		mutex_lock(&ps->sk_lock);
770 		error = l2tp_session_register(session, tunnel);
771 		if (error < 0) {
772 			mutex_unlock(&ps->sk_lock);
773 			l2tp_session_put(session);
774 			goto end;
775 		}
776 
777 		new_session = true;
778 	}
779 
780 	/* Special case: if source & dest session_id == 0x0000, this
781 	 * socket is being created to manage the tunnel. Just set up
782 	 * the internal context for use by ioctl() and sockopt()
783 	 * handlers.
784 	 */
785 	if (session->session_id == 0 && session->peer_session_id == 0) {
786 		error = 0;
787 		goto out_no_ppp;
788 	}
789 
790 	/* The only header we need to worry about is the L2TP
791 	 * header. This size is different depending on whether
792 	 * sequence numbers are enabled for the data channel.
793 	 */
794 	po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
795 
796 	po->chan.private = sk;
797 	po->chan.ops	 = &pppol2tp_chan_ops;
798 	po->chan.mtu	 = pppol2tp_tunnel_mtu(tunnel);
799 
800 	error = ppp_register_net_channel(sock_net(sk), &po->chan);
801 	if (error) {
802 		mutex_unlock(&ps->sk_lock);
803 		goto end;
804 	}
805 
806 out_no_ppp:
807 	/* This is how we get the session context from the socket. */
808 	sock_hold(sk);
809 	rcu_assign_sk_user_data(sk, session);
810 	rcu_assign_pointer(ps->sk, sk);
811 	mutex_unlock(&ps->sk_lock);
812 
813 	/* Keep the reference we've grabbed on the session: sk doesn't expect
814 	 * the session to disappear. pppol2tp_session_close() is responsible
815 	 * for dropping it.
816 	 */
817 	drop_refcnt = false;
818 
819 	sk->sk_state = PPPOX_CONNECTED;
820 
821 end:
822 	if (error) {
823 		if (new_session)
824 			l2tp_session_delete(session);
825 		if (new_tunnel)
826 			l2tp_tunnel_delete(tunnel);
827 	}
828 	if (drop_refcnt)
829 		l2tp_session_put(session);
830 	l2tp_tunnel_put(tunnel);
831 	release_sock(sk);
832 
833 	return error;
834 }
835 
836 #ifdef CONFIG_L2TP_V3
837 
838 /* Called when creating sessions via the netlink interface. */
pppol2tp_session_create(struct net * net,struct l2tp_tunnel * tunnel,u32 session_id,u32 peer_session_id,struct l2tp_session_cfg * cfg)839 static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
840 				   u32 session_id, u32 peer_session_id,
841 				   struct l2tp_session_cfg *cfg)
842 {
843 	int error;
844 	struct l2tp_session *session;
845 
846 	/* Error if tunnel socket is not prepped */
847 	if (!tunnel->sock) {
848 		error = -ENOENT;
849 		goto err;
850 	}
851 
852 	/* Allocate and initialize a new session context. */
853 	session = l2tp_session_create(sizeof(struct pppol2tp_session),
854 				      tunnel, session_id,
855 				      peer_session_id, cfg);
856 	if (IS_ERR(session)) {
857 		error = PTR_ERR(session);
858 		goto err;
859 	}
860 
861 	pppol2tp_session_init(session);
862 
863 	error = l2tp_session_register(session, tunnel);
864 	if (error < 0)
865 		goto err_sess;
866 
867 	return 0;
868 
869 err_sess:
870 	l2tp_session_put(session);
871 err:
872 	return error;
873 }
874 
875 #endif /* CONFIG_L2TP_V3 */
876 
877 /* getname() support.
878  */
pppol2tp_getname(struct socket * sock,struct sockaddr * uaddr,int peer)879 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
880 			    int peer)
881 {
882 	int len = 0;
883 	int error = 0;
884 	struct l2tp_session *session;
885 	struct l2tp_tunnel *tunnel;
886 	struct sock *sk = sock->sk;
887 	struct inet_sock *inet;
888 	struct pppol2tp_session *pls;
889 
890 	error = -ENOTCONN;
891 	if (!sk)
892 		goto end;
893 	if (!(sk->sk_state & PPPOX_CONNECTED))
894 		goto end;
895 
896 	error = -EBADF;
897 	session = pppol2tp_sock_to_session(sk);
898 	if (!session)
899 		goto end;
900 
901 	pls = l2tp_session_priv(session);
902 	tunnel = session->tunnel;
903 
904 	inet = inet_sk(tunnel->sock);
905 	if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET) {
906 		struct sockaddr_pppol2tp sp;
907 
908 		len = sizeof(sp);
909 		memset(&sp, 0, len);
910 		sp.sa_family	= AF_PPPOX;
911 		sp.sa_protocol	= PX_PROTO_OL2TP;
912 		sp.pppol2tp.fd  = tunnel->fd;
913 		sp.pppol2tp.pid = pls->owner;
914 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
915 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
916 		sp.pppol2tp.s_session = session->session_id;
917 		sp.pppol2tp.d_session = session->peer_session_id;
918 		sp.pppol2tp.addr.sin_family = AF_INET;
919 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
920 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
921 		memcpy(uaddr, &sp, len);
922 #if IS_ENABLED(CONFIG_IPV6)
923 	} else if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET6) {
924 		struct sockaddr_pppol2tpin6 sp;
925 
926 		len = sizeof(sp);
927 		memset(&sp, 0, len);
928 		sp.sa_family	= AF_PPPOX;
929 		sp.sa_protocol	= PX_PROTO_OL2TP;
930 		sp.pppol2tp.fd  = tunnel->fd;
931 		sp.pppol2tp.pid = pls->owner;
932 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
933 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
934 		sp.pppol2tp.s_session = session->session_id;
935 		sp.pppol2tp.d_session = session->peer_session_id;
936 		sp.pppol2tp.addr.sin6_family = AF_INET6;
937 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
938 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
939 		       sizeof(tunnel->sock->sk_v6_daddr));
940 		memcpy(uaddr, &sp, len);
941 	} else if (tunnel->version == 3 && tunnel->sock->sk_family == AF_INET6) {
942 		struct sockaddr_pppol2tpv3in6 sp;
943 
944 		len = sizeof(sp);
945 		memset(&sp, 0, len);
946 		sp.sa_family	= AF_PPPOX;
947 		sp.sa_protocol	= PX_PROTO_OL2TP;
948 		sp.pppol2tp.fd  = tunnel->fd;
949 		sp.pppol2tp.pid = pls->owner;
950 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
951 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
952 		sp.pppol2tp.s_session = session->session_id;
953 		sp.pppol2tp.d_session = session->peer_session_id;
954 		sp.pppol2tp.addr.sin6_family = AF_INET6;
955 		sp.pppol2tp.addr.sin6_port = inet->inet_dport;
956 		memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
957 		       sizeof(tunnel->sock->sk_v6_daddr));
958 		memcpy(uaddr, &sp, len);
959 #endif
960 	} else if (tunnel->version == 3) {
961 		struct sockaddr_pppol2tpv3 sp;
962 
963 		len = sizeof(sp);
964 		memset(&sp, 0, len);
965 		sp.sa_family	= AF_PPPOX;
966 		sp.sa_protocol	= PX_PROTO_OL2TP;
967 		sp.pppol2tp.fd  = tunnel->fd;
968 		sp.pppol2tp.pid = pls->owner;
969 		sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
970 		sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
971 		sp.pppol2tp.s_session = session->session_id;
972 		sp.pppol2tp.d_session = session->peer_session_id;
973 		sp.pppol2tp.addr.sin_family = AF_INET;
974 		sp.pppol2tp.addr.sin_port = inet->inet_dport;
975 		sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
976 		memcpy(uaddr, &sp, len);
977 	}
978 
979 	error = len;
980 
981 	l2tp_session_put(session);
982 end:
983 	return error;
984 }
985 
986 /****************************************************************************
987  * ioctl() handlers.
988  *
989  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
990  * sockets. However, in order to control kernel tunnel features, we allow
991  * userspace to create a special "tunnel" PPPoX socket which is used for
992  * control only.  Tunnel PPPoX sockets have session_id == 0 and simply allow
993  * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
994  * calls.
995  ****************************************************************************/
996 
pppol2tp_copy_stats(struct pppol2tp_ioc_stats * dest,const struct l2tp_stats * stats)997 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
998 				const struct l2tp_stats *stats)
999 {
1000 	memset(dest, 0, sizeof(*dest));
1001 
1002 	dest->tx_packets = atomic_long_read(&stats->tx_packets);
1003 	dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1004 	dest->tx_errors = atomic_long_read(&stats->tx_errors);
1005 	dest->rx_packets = atomic_long_read(&stats->rx_packets);
1006 	dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1007 	dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1008 	dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1009 	dest->rx_errors = atomic_long_read(&stats->rx_errors);
1010 }
1011 
pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats * stats,struct l2tp_tunnel * tunnel)1012 static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
1013 				      struct l2tp_tunnel *tunnel)
1014 {
1015 	struct l2tp_session *session;
1016 
1017 	if (!stats->session_id) {
1018 		pppol2tp_copy_stats(stats, &tunnel->stats);
1019 		return 0;
1020 	}
1021 
1022 	/* If session_id is set, search the corresponding session in the
1023 	 * context of this tunnel and record the session's statistics.
1024 	 */
1025 	session = l2tp_session_get(tunnel->l2tp_net, tunnel->sock, tunnel->version,
1026 				   tunnel->tunnel_id, stats->session_id);
1027 	if (!session)
1028 		return -EBADR;
1029 
1030 	if (session->pwtype != L2TP_PWTYPE_PPP) {
1031 		l2tp_session_put(session);
1032 		return -EBADR;
1033 	}
1034 
1035 	pppol2tp_copy_stats(stats, &session->stats);
1036 	l2tp_session_put(session);
1037 
1038 	return 0;
1039 }
1040 
pppol2tp_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)1041 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1042 			  unsigned long arg)
1043 {
1044 	struct pppol2tp_ioc_stats stats;
1045 	struct l2tp_session *session;
1046 
1047 	switch (cmd) {
1048 	case PPPIOCGMRU:
1049 	case PPPIOCGFLAGS:
1050 		session = sock->sk->sk_user_data;
1051 		if (!session)
1052 			return -ENOTCONN;
1053 
1054 		if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1055 			return -EBADF;
1056 
1057 		/* Not defined for tunnels */
1058 		if (!session->session_id && !session->peer_session_id)
1059 			return -ENOSYS;
1060 
1061 		if (put_user(0, (int __user *)arg))
1062 			return -EFAULT;
1063 		break;
1064 
1065 	case PPPIOCSMRU:
1066 	case PPPIOCSFLAGS:
1067 		session = sock->sk->sk_user_data;
1068 		if (!session)
1069 			return -ENOTCONN;
1070 
1071 		if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1072 			return -EBADF;
1073 
1074 		/* Not defined for tunnels */
1075 		if (!session->session_id && !session->peer_session_id)
1076 			return -ENOSYS;
1077 
1078 		if (!access_ok((int __user *)arg, sizeof(int)))
1079 			return -EFAULT;
1080 		break;
1081 
1082 	case PPPIOCGL2TPSTATS:
1083 		session = sock->sk->sk_user_data;
1084 		if (!session)
1085 			return -ENOTCONN;
1086 
1087 		if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1088 			return -EBADF;
1089 
1090 		/* Session 0 represents the parent tunnel */
1091 		if (!session->session_id && !session->peer_session_id) {
1092 			u32 session_id;
1093 			int err;
1094 
1095 			if (copy_from_user(&stats, (void __user *)arg,
1096 					   sizeof(stats)))
1097 				return -EFAULT;
1098 
1099 			session_id = stats.session_id;
1100 			err = pppol2tp_tunnel_copy_stats(&stats,
1101 							 session->tunnel);
1102 			if (err < 0)
1103 				return err;
1104 
1105 			stats.session_id = session_id;
1106 		} else {
1107 			pppol2tp_copy_stats(&stats, &session->stats);
1108 			stats.session_id = session->session_id;
1109 		}
1110 		stats.tunnel_id = session->tunnel->tunnel_id;
1111 		stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
1112 
1113 		if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
1114 			return -EFAULT;
1115 		break;
1116 
1117 	default:
1118 		return -ENOIOCTLCMD;
1119 	}
1120 
1121 	return 0;
1122 }
1123 
1124 /*****************************************************************************
1125  * setsockopt() / getsockopt() support.
1126  *
1127  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1128  * sockets. In order to control kernel tunnel features, we allow userspace to
1129  * create a special "tunnel" PPPoX socket which is used for control only.
1130  * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1131  * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1132  *****************************************************************************/
1133 
1134 /* Tunnel setsockopt() helper.
1135  */
pppol2tp_tunnel_setsockopt(struct sock * sk,struct l2tp_tunnel * tunnel,int optname,int val)1136 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1137 				      struct l2tp_tunnel *tunnel,
1138 				      int optname, int val)
1139 {
1140 	int err = 0;
1141 
1142 	switch (optname) {
1143 	case PPPOL2TP_SO_DEBUG:
1144 		/* Tunnel debug flags option is deprecated */
1145 		break;
1146 
1147 	default:
1148 		err = -ENOPROTOOPT;
1149 		break;
1150 	}
1151 
1152 	return err;
1153 }
1154 
1155 /* Session setsockopt helper.
1156  */
pppol2tp_session_setsockopt(struct sock * sk,struct l2tp_session * session,int optname,int val)1157 static int pppol2tp_session_setsockopt(struct sock *sk,
1158 				       struct l2tp_session *session,
1159 				       int optname, int val)
1160 {
1161 	int err = 0;
1162 
1163 	switch (optname) {
1164 	case PPPOL2TP_SO_RECVSEQ:
1165 		if (val != 0 && val != 1) {
1166 			err = -EINVAL;
1167 			break;
1168 		}
1169 		session->recv_seq = !!val;
1170 		break;
1171 
1172 	case PPPOL2TP_SO_SENDSEQ:
1173 		if (val != 0 && val != 1) {
1174 			err = -EINVAL;
1175 			break;
1176 		}
1177 		session->send_seq = !!val;
1178 		{
1179 			struct pppox_sock *po = pppox_sk(sk);
1180 
1181 			po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1182 				PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1183 		}
1184 		l2tp_session_set_header_len(session, session->tunnel->version,
1185 					    session->tunnel->encap);
1186 		break;
1187 
1188 	case PPPOL2TP_SO_LNSMODE:
1189 		if (val != 0 && val != 1) {
1190 			err = -EINVAL;
1191 			break;
1192 		}
1193 		session->lns_mode = !!val;
1194 		break;
1195 
1196 	case PPPOL2TP_SO_DEBUG:
1197 		/* Session debug flags option is deprecated */
1198 		break;
1199 
1200 	case PPPOL2TP_SO_REORDERTO:
1201 		session->reorder_timeout = msecs_to_jiffies(val);
1202 		break;
1203 
1204 	default:
1205 		err = -ENOPROTOOPT;
1206 		break;
1207 	}
1208 
1209 	return err;
1210 }
1211 
1212 /* Main setsockopt() entry point.
1213  * Does API checks, then calls either the tunnel or session setsockopt
1214  * handler, according to whether the PPPoL2TP socket is a for a regular
1215  * session or the special tunnel type.
1216  */
pppol2tp_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)1217 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1218 			       sockptr_t optval, unsigned int optlen)
1219 {
1220 	struct sock *sk = sock->sk;
1221 	struct l2tp_session *session;
1222 	struct l2tp_tunnel *tunnel;
1223 	int val;
1224 	int err;
1225 
1226 	if (level != SOL_PPPOL2TP)
1227 		return -EINVAL;
1228 
1229 	if (optlen < sizeof(int))
1230 		return -EINVAL;
1231 
1232 	if (copy_from_sockptr(&val, optval, sizeof(int)))
1233 		return -EFAULT;
1234 
1235 	err = -ENOTCONN;
1236 	if (!sk->sk_user_data)
1237 		goto end;
1238 
1239 	/* Get session context from the socket */
1240 	err = -EBADF;
1241 	session = pppol2tp_sock_to_session(sk);
1242 	if (!session)
1243 		goto end;
1244 
1245 	/* Special case: if session_id == 0x0000, treat as operation on tunnel
1246 	 */
1247 	if (session->session_id == 0 && session->peer_session_id == 0) {
1248 		tunnel = session->tunnel;
1249 		err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1250 	} else {
1251 		err = pppol2tp_session_setsockopt(sk, session, optname, val);
1252 	}
1253 
1254 	l2tp_session_put(session);
1255 end:
1256 	return err;
1257 }
1258 
1259 /* Tunnel getsockopt helper. Called with sock locked.
1260  */
pppol2tp_tunnel_getsockopt(struct sock * sk,struct l2tp_tunnel * tunnel,int optname,int * val)1261 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1262 				      struct l2tp_tunnel *tunnel,
1263 				      int optname, int *val)
1264 {
1265 	int err = 0;
1266 
1267 	switch (optname) {
1268 	case PPPOL2TP_SO_DEBUG:
1269 		/* Tunnel debug flags option is deprecated */
1270 		*val = 0;
1271 		break;
1272 
1273 	default:
1274 		err = -ENOPROTOOPT;
1275 		break;
1276 	}
1277 
1278 	return err;
1279 }
1280 
1281 /* Session getsockopt helper. Called with sock locked.
1282  */
pppol2tp_session_getsockopt(struct sock * sk,struct l2tp_session * session,int optname,int * val)1283 static int pppol2tp_session_getsockopt(struct sock *sk,
1284 				       struct l2tp_session *session,
1285 				       int optname, int *val)
1286 {
1287 	int err = 0;
1288 
1289 	switch (optname) {
1290 	case PPPOL2TP_SO_RECVSEQ:
1291 		*val = session->recv_seq;
1292 		break;
1293 
1294 	case PPPOL2TP_SO_SENDSEQ:
1295 		*val = session->send_seq;
1296 		break;
1297 
1298 	case PPPOL2TP_SO_LNSMODE:
1299 		*val = session->lns_mode;
1300 		break;
1301 
1302 	case PPPOL2TP_SO_DEBUG:
1303 		/* Session debug flags option is deprecated */
1304 		*val = 0;
1305 		break;
1306 
1307 	case PPPOL2TP_SO_REORDERTO:
1308 		*val = (int)jiffies_to_msecs(session->reorder_timeout);
1309 		break;
1310 
1311 	default:
1312 		err = -ENOPROTOOPT;
1313 	}
1314 
1315 	return err;
1316 }
1317 
1318 /* Main getsockopt() entry point.
1319  * Does API checks, then calls either the tunnel or session getsockopt
1320  * handler, according to whether the PPPoX socket is a for a regular session
1321  * or the special tunnel type.
1322  */
pppol2tp_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)1323 static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1324 			       char __user *optval, int __user *optlen)
1325 {
1326 	struct sock *sk = sock->sk;
1327 	struct l2tp_session *session;
1328 	struct l2tp_tunnel *tunnel;
1329 	int val, len;
1330 	int err;
1331 
1332 	if (level != SOL_PPPOL2TP)
1333 		return -EINVAL;
1334 
1335 	if (get_user(len, optlen))
1336 		return -EFAULT;
1337 
1338 	if (len < 0)
1339 		return -EINVAL;
1340 
1341 	len = min_t(unsigned int, len, sizeof(int));
1342 
1343 	err = -ENOTCONN;
1344 	if (!sk->sk_user_data)
1345 		goto end;
1346 
1347 	/* Get the session context */
1348 	err = -EBADF;
1349 	session = pppol2tp_sock_to_session(sk);
1350 	if (!session)
1351 		goto end;
1352 
1353 	/* Special case: if session_id == 0x0000, treat as operation on tunnel */
1354 	if (session->session_id == 0 && session->peer_session_id == 0) {
1355 		tunnel = session->tunnel;
1356 		err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1357 		if (err)
1358 			goto end_put_sess;
1359 	} else {
1360 		err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1361 		if (err)
1362 			goto end_put_sess;
1363 	}
1364 
1365 	err = -EFAULT;
1366 	if (put_user(len, optlen))
1367 		goto end_put_sess;
1368 
1369 	if (copy_to_user((void __user *)optval, &val, len))
1370 		goto end_put_sess;
1371 
1372 	err = 0;
1373 
1374 end_put_sess:
1375 	l2tp_session_put(session);
1376 end:
1377 	return err;
1378 }
1379 
1380 /*****************************************************************************
1381  * /proc filesystem for debug
1382  * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1383  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1384  *****************************************************************************/
1385 
1386 #ifdef CONFIG_PROC_FS
1387 
1388 struct pppol2tp_seq_data {
1389 	struct seq_net_private p;
1390 	unsigned long tkey;		/* lookup key of current tunnel */
1391 	unsigned long skey;		/* lookup key of current session */
1392 	struct l2tp_tunnel *tunnel;
1393 	struct l2tp_session *session;	/* NULL means get next tunnel */
1394 };
1395 
pppol2tp_next_tunnel(struct net * net,struct pppol2tp_seq_data * pd)1396 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1397 {
1398 	/* Drop reference taken during previous invocation */
1399 	if (pd->tunnel)
1400 		l2tp_tunnel_put(pd->tunnel);
1401 
1402 	for (;;) {
1403 		pd->tunnel = l2tp_tunnel_get_next(net, &pd->tkey);
1404 		pd->tkey++;
1405 
1406 		/* Only accept L2TPv2 tunnels */
1407 		if (!pd->tunnel || pd->tunnel->version == 2)
1408 			return;
1409 
1410 		l2tp_tunnel_put(pd->tunnel);
1411 	}
1412 }
1413 
pppol2tp_next_session(struct net * net,struct pppol2tp_seq_data * pd)1414 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1415 {
1416 	/* Drop reference taken during previous invocation */
1417 	if (pd->session)
1418 		l2tp_session_put(pd->session);
1419 
1420 	pd->session = l2tp_session_get_next(net, pd->tunnel->sock,
1421 					    pd->tunnel->version,
1422 					    pd->tunnel->tunnel_id, &pd->skey);
1423 	pd->skey++;
1424 
1425 	if (!pd->session) {
1426 		pd->skey = 0;
1427 		pppol2tp_next_tunnel(net, pd);
1428 	}
1429 }
1430 
pppol2tp_seq_start(struct seq_file * m,loff_t * offs)1431 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1432 {
1433 	struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1434 	loff_t pos = *offs;
1435 	struct net *net;
1436 
1437 	if (!pos)
1438 		goto out;
1439 
1440 	if (WARN_ON(!m->private)) {
1441 		pd = NULL;
1442 		goto out;
1443 	}
1444 
1445 	pd = m->private;
1446 	net = seq_file_net(m);
1447 
1448 	if (!pd->tunnel)
1449 		pppol2tp_next_tunnel(net, pd);
1450 	else
1451 		pppol2tp_next_session(net, pd);
1452 
1453 	/* NULL tunnel and session indicates end of list */
1454 	if (!pd->tunnel && !pd->session)
1455 		pd = NULL;
1456 
1457 out:
1458 	return pd;
1459 }
1460 
pppol2tp_seq_next(struct seq_file * m,void * v,loff_t * pos)1461 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1462 {
1463 	(*pos)++;
1464 	return NULL;
1465 }
1466 
pppol2tp_seq_stop(struct seq_file * p,void * v)1467 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1468 {
1469 	struct pppol2tp_seq_data *pd = v;
1470 
1471 	if (!pd || pd == SEQ_START_TOKEN)
1472 		return;
1473 
1474 	/* Drop reference taken by last invocation of pppol2tp_next_session()
1475 	 * or pppol2tp_next_tunnel().
1476 	 */
1477 	if (pd->session) {
1478 		l2tp_session_put(pd->session);
1479 		pd->session = NULL;
1480 	}
1481 	if (pd->tunnel) {
1482 		l2tp_tunnel_put(pd->tunnel);
1483 		pd->tunnel = NULL;
1484 	}
1485 }
1486 
pppol2tp_seq_tunnel_show(struct seq_file * m,void * v)1487 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1488 {
1489 	struct l2tp_tunnel *tunnel = v;
1490 
1491 	seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1492 		   tunnel->name,
1493 		   tunnel->sock ? 'Y' : 'N',
1494 		   refcount_read(&tunnel->ref_count) - 1);
1495 	seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1496 		   0,
1497 		   atomic_long_read(&tunnel->stats.tx_packets),
1498 		   atomic_long_read(&tunnel->stats.tx_bytes),
1499 		   atomic_long_read(&tunnel->stats.tx_errors),
1500 		   atomic_long_read(&tunnel->stats.rx_packets),
1501 		   atomic_long_read(&tunnel->stats.rx_bytes),
1502 		   atomic_long_read(&tunnel->stats.rx_errors));
1503 }
1504 
pppol2tp_seq_session_show(struct seq_file * m,void * v)1505 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1506 {
1507 	struct l2tp_session *session = v;
1508 	struct l2tp_tunnel *tunnel = session->tunnel;
1509 	unsigned char state;
1510 	char user_data_ok;
1511 	struct sock *sk;
1512 	u32 ip = 0;
1513 	u16 port = 0;
1514 
1515 	if (tunnel->sock) {
1516 		struct inet_sock *inet = inet_sk(tunnel->sock);
1517 
1518 		ip = ntohl(inet->inet_saddr);
1519 		port = ntohs(inet->inet_sport);
1520 	}
1521 
1522 	rcu_read_lock();
1523 	sk = pppol2tp_session_get_sock(session);
1524 	if (sk) {
1525 		state = sk->sk_state;
1526 		user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1527 	} else {
1528 		state = 0;
1529 		user_data_ok = 'N';
1530 	}
1531 
1532 	seq_printf(m, "  SESSION '%s' %08X/%d %04X/%04X -> %04X/%04X %d %c\n",
1533 		   session->name, ip, port,
1534 		   tunnel->tunnel_id,
1535 		   session->session_id,
1536 		   tunnel->peer_tunnel_id,
1537 		   session->peer_session_id,
1538 		   state, user_data_ok);
1539 	seq_printf(m, "   0/0/%c/%c/%s %08x %u\n",
1540 		   session->recv_seq ? 'R' : '-',
1541 		   session->send_seq ? 'S' : '-',
1542 		   session->lns_mode ? "LNS" : "LAC",
1543 		   0,
1544 		   jiffies_to_msecs(session->reorder_timeout));
1545 	seq_printf(m, "   %u/%u %ld/%ld/%ld %ld/%ld/%ld\n",
1546 		   session->nr, session->ns,
1547 		   atomic_long_read(&session->stats.tx_packets),
1548 		   atomic_long_read(&session->stats.tx_bytes),
1549 		   atomic_long_read(&session->stats.tx_errors),
1550 		   atomic_long_read(&session->stats.rx_packets),
1551 		   atomic_long_read(&session->stats.rx_bytes),
1552 		   atomic_long_read(&session->stats.rx_errors));
1553 
1554 	if (sk) {
1555 		struct pppox_sock *po = pppox_sk(sk);
1556 
1557 		seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
1558 	}
1559 	rcu_read_unlock();
1560 }
1561 
pppol2tp_seq_show(struct seq_file * m,void * v)1562 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1563 {
1564 	struct pppol2tp_seq_data *pd = v;
1565 
1566 	/* display header on line 1 */
1567 	if (v == SEQ_START_TOKEN) {
1568 		seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1569 		seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1570 		seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1571 		seq_puts(m, "  SESSION name, addr/port src-tid/sid dest-tid/sid state user-data-ok\n");
1572 		seq_puts(m, "   mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1573 		seq_puts(m, "   nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1574 		goto out;
1575 	}
1576 
1577 	if (!pd->session)
1578 		pppol2tp_seq_tunnel_show(m, pd->tunnel);
1579 	else
1580 		pppol2tp_seq_session_show(m, pd->session);
1581 
1582 out:
1583 	return 0;
1584 }
1585 
1586 static const struct seq_operations pppol2tp_seq_ops = {
1587 	.start		= pppol2tp_seq_start,
1588 	.next		= pppol2tp_seq_next,
1589 	.stop		= pppol2tp_seq_stop,
1590 	.show		= pppol2tp_seq_show,
1591 };
1592 #endif /* CONFIG_PROC_FS */
1593 
1594 /*****************************************************************************
1595  * Network namespace
1596  *****************************************************************************/
1597 
pppol2tp_init_net(struct net * net)1598 static __net_init int pppol2tp_init_net(struct net *net)
1599 {
1600 	struct proc_dir_entry *pde;
1601 	int err = 0;
1602 
1603 	pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1604 			      &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
1605 	if (!pde) {
1606 		err = -ENOMEM;
1607 		goto out;
1608 	}
1609 
1610 out:
1611 	return err;
1612 }
1613 
pppol2tp_exit_net(struct net * net)1614 static __net_exit void pppol2tp_exit_net(struct net *net)
1615 {
1616 	remove_proc_entry("pppol2tp", net->proc_net);
1617 }
1618 
1619 static struct pernet_operations pppol2tp_net_ops = {
1620 	.init = pppol2tp_init_net,
1621 	.exit = pppol2tp_exit_net,
1622 };
1623 
1624 /*****************************************************************************
1625  * Init and cleanup
1626  *****************************************************************************/
1627 
1628 static const struct proto_ops pppol2tp_ops = {
1629 	.family		= AF_PPPOX,
1630 	.owner		= THIS_MODULE,
1631 	.release	= pppol2tp_release,
1632 	.bind		= sock_no_bind,
1633 	.connect	= pppol2tp_connect,
1634 	.socketpair	= sock_no_socketpair,
1635 	.accept		= sock_no_accept,
1636 	.getname	= pppol2tp_getname,
1637 	.poll		= datagram_poll,
1638 	.listen		= sock_no_listen,
1639 	.shutdown	= sock_no_shutdown,
1640 	.setsockopt	= pppol2tp_setsockopt,
1641 	.getsockopt	= pppol2tp_getsockopt,
1642 	.sendmsg	= pppol2tp_sendmsg,
1643 	.recvmsg	= pppol2tp_recvmsg,
1644 	.mmap		= sock_no_mmap,
1645 	.ioctl		= pppox_ioctl,
1646 #ifdef CONFIG_COMPAT
1647 	.compat_ioctl = pppox_compat_ioctl,
1648 #endif
1649 };
1650 
1651 static const struct pppox_proto pppol2tp_proto = {
1652 	.create		= pppol2tp_create,
1653 	.ioctl		= pppol2tp_ioctl,
1654 	.owner		= THIS_MODULE,
1655 };
1656 
1657 #ifdef CONFIG_L2TP_V3
1658 
1659 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1660 	.session_create	= pppol2tp_session_create,
1661 	.session_delete	= l2tp_session_delete,
1662 };
1663 
1664 #endif /* CONFIG_L2TP_V3 */
1665 
pppol2tp_init(void)1666 static int __init pppol2tp_init(void)
1667 {
1668 	int err;
1669 
1670 	err = register_pernet_device(&pppol2tp_net_ops);
1671 	if (err)
1672 		goto out;
1673 
1674 	err = proto_register(&pppol2tp_sk_proto, 0);
1675 	if (err)
1676 		goto out_unregister_pppol2tp_pernet;
1677 
1678 	err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1679 	if (err)
1680 		goto out_unregister_pppol2tp_proto;
1681 
1682 #ifdef CONFIG_L2TP_V3
1683 	err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1684 	if (err)
1685 		goto out_unregister_pppox;
1686 #endif
1687 
1688 	pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1689 
1690 out:
1691 	return err;
1692 
1693 #ifdef CONFIG_L2TP_V3
1694 out_unregister_pppox:
1695 	unregister_pppox_proto(PX_PROTO_OL2TP);
1696 #endif
1697 out_unregister_pppol2tp_proto:
1698 	proto_unregister(&pppol2tp_sk_proto);
1699 out_unregister_pppol2tp_pernet:
1700 	unregister_pernet_device(&pppol2tp_net_ops);
1701 	goto out;
1702 }
1703 
pppol2tp_exit(void)1704 static void __exit pppol2tp_exit(void)
1705 {
1706 #ifdef CONFIG_L2TP_V3
1707 	l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1708 #endif
1709 	unregister_pppox_proto(PX_PROTO_OL2TP);
1710 	proto_unregister(&pppol2tp_sk_proto);
1711 	unregister_pernet_device(&pppol2tp_net_ops);
1712 }
1713 
1714 module_init(pppol2tp_init);
1715 module_exit(pppol2tp_exit);
1716 
1717 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1718 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1719 MODULE_LICENSE("GPL");
1720 MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1721 MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1722 MODULE_ALIAS_L2TP_PWTYPE(7);
1723