1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*****************************************************************************
3 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
5 * PPPoX --- Generic PPP encapsulation socket family
6 * PPPoL2TP --- PPP over L2TP (RFC 2661)
10 * Authors: James Chapman (jchapman@katalix.com)
12 * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
17 /* This driver handles only L2TP data frames; control frames are handled by a
18 * userspace application.
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.
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.
29 * Here's example userspace code to create a socket for sending/receiving data
30 * over an L2TP session:-
32 * struct sockaddr_pppol2tp sax;
36 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
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;
49 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
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.
56 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
58 #include <linux/module.h>
59 #include <linux/string.h>
60 #include <linux/list.h>
61 #include <linux/uaccess.h>
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>
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>
77 #include <linux/udp.h>
78 #include <linux/if_pppox.h>
79 #include <linux/if_pppol2tp.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>
94 #include <net/inet_common.h>
96 #include <asm/byteorder.h>
97 #include <linux/atomic.h>
99 #include "l2tp_core.h"
101 #define PPPOL2TP_DRV_VERSION "V2.0"
103 /* Space for UDP, L2TP and PPP headers */
104 #define PPPOL2TP_HEADER_OVERHEAD 40
106 /* Number of bytes to build transmit L2TP headers.
107 * Unfortunately the size is different depending on whether sequence numbers
110 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
111 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
113 /* Private data of each session. This data lives at the end of struct
114 * l2tp_session, referenced via session->priv[].
116 struct pppol2tp_session {
117 int owner; /* pid that opened the socket */
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 struct rcu_head rcu; /* For asynchronous release */
125 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
127 static const struct ppp_channel_ops pppol2tp_chan_ops = {
128 .start_xmit = pppol2tp_xmit,
131 static const struct proto_ops pppol2tp_ops;
133 /* Retrieves the pppol2tp socket associated to a session.
134 * A reference is held on the returned socket, so this function must be paired
137 static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
139 struct pppol2tp_session *ps = l2tp_session_priv(session);
143 sk = rcu_dereference(ps->sk);
151 /* Helpers to obtain tunnel/session contexts from sockets.
153 static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
155 struct l2tp_session *session;
161 session = (struct l2tp_session *)(sk->sk_user_data);
166 if (WARN_ON(session->magic != L2TP_SESSION_MAGIC)) {
176 /*****************************************************************************
177 * Receive data handling
178 *****************************************************************************/
180 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
182 static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
183 size_t len, int flags)
187 struct sock *sk = sock->sk;
190 if (sk->sk_state & PPPOX_BOUND)
194 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
195 flags & MSG_DONTWAIT, &err);
201 else if (len < skb->len)
202 msg->msg_flags |= MSG_TRUNC;
204 err = skb_copy_datagram_msg(skb, 0, msg, len);
205 if (likely(err == 0))
213 static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
215 struct pppol2tp_session *ps = l2tp_session_priv(session);
216 struct sock *sk = NULL;
218 /* If the socket is bound, send it in to PPP's input queue. Otherwise
219 * queue it on the session socket.
222 sk = rcu_dereference(ps->sk);
226 /* If the first two bytes are 0xFF03, consider that it is the PPP's
227 * Address and Control fields and skip them. The L2TP module has always
228 * worked this way, although, in theory, the use of these fields should
229 * be negociated and handled at the PPP layer. These fields are
230 * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
231 * Information command with Poll/Final bit set to zero (RFC 1662).
233 if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
234 skb->data[1] == PPP_UI)
237 if (sk->sk_state & PPPOX_BOUND) {
238 struct pppox_sock *po;
240 l2tp_dbg(session, L2TP_MSG_DATA,
241 "%s: recv %d byte data frame, passing to ppp\n",
242 session->name, data_len);
245 ppp_input(&po->chan, skb);
247 l2tp_dbg(session, L2TP_MSG_DATA,
248 "%s: recv %d byte data frame, passing to L2TP socket\n",
249 session->name, data_len);
251 if (sock_queue_rcv_skb(sk, skb) < 0) {
252 atomic_long_inc(&session->stats.rx_errors);
262 l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
266 /************************************************************************
268 ***********************************************************************/
270 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
271 * when a user application does a sendmsg() on the session socket. L2TP and
272 * PPP headers must be inserted into the user's data.
274 static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
277 struct sock *sk = sock->sk;
280 struct l2tp_session *session;
281 struct l2tp_tunnel *tunnel;
285 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
288 /* Get session and tunnel contexts */
290 session = pppol2tp_sock_to_session(sk);
294 tunnel = session->tunnel;
296 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
298 /* Allocate a socket buffer */
300 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
301 uhlen + session->hdr_len +
302 2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
307 /* Reserve space for headers. */
308 skb_reserve(skb, NET_SKB_PAD);
309 skb_reset_network_header(skb);
310 skb_reserve(skb, sizeof(struct iphdr));
311 skb_reset_transport_header(skb);
312 skb_reserve(skb, uhlen);
315 skb->data[0] = PPP_ALLSTATIONS;
316 skb->data[1] = PPP_UI;
319 /* Copy user data into skb */
320 error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
327 l2tp_xmit_skb(session, skb, session->hdr_len);
340 /* Transmit function called by generic PPP driver. Sends PPP frame
341 * over PPPoL2TP socket.
343 * This is almost the same as pppol2tp_sendmsg(), but rather than
344 * being called with a msghdr from userspace, it is called with a skb
347 * The supplied skb from ppp doesn't have enough headroom for the
348 * insertion of L2TP, UDP and IP headers so we need to allocate more
349 * headroom in the skb. This will create a cloned skb. But we must be
350 * careful in the error case because the caller will expect to free
351 * the skb it supplied, not our cloned skb. So we take care to always
352 * leave the original skb unfreed if we return an error.
354 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
356 struct sock *sk = (struct sock *)chan->private;
357 struct l2tp_session *session;
358 struct l2tp_tunnel *tunnel;
361 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
364 /* Get session and tunnel contexts from the socket */
365 session = pppol2tp_sock_to_session(sk);
369 tunnel = session->tunnel;
371 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
372 headroom = NET_SKB_PAD +
373 sizeof(struct iphdr) + /* IP header */
374 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
375 session->hdr_len + /* L2TP header */
376 2; /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
377 if (skb_cow_head(skb, headroom))
380 /* Setup PPP header */
382 skb->data[0] = PPP_ALLSTATIONS;
383 skb->data[1] = PPP_UI;
386 l2tp_xmit_skb(session, skb, session->hdr_len);
396 /* Free the original skb */
401 /*****************************************************************************
402 * Session (and tunnel control) socket create/destroy.
403 *****************************************************************************/
405 static void pppol2tp_put_sk(struct rcu_head *head)
407 struct pppol2tp_session *ps;
409 ps = container_of(head, typeof(*ps), rcu);
413 /* Really kill the session socket. (Called from sock_put() if
416 static void pppol2tp_session_destruct(struct sock *sk)
418 struct l2tp_session *session = sk->sk_user_data;
420 skb_queue_purge(&sk->sk_receive_queue);
421 skb_queue_purge(&sk->sk_write_queue);
424 sk->sk_user_data = NULL;
425 if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
427 l2tp_session_dec_refcount(session);
431 /* Called when the PPPoX socket (session) is closed.
433 static int pppol2tp_release(struct socket *sock)
435 struct sock *sk = sock->sk;
436 struct l2tp_session *session;
444 if (sock_flag(sk, SOCK_DEAD) != 0)
447 pppox_unbind_sock(sk);
449 /* Signal the death of the socket. */
450 sk->sk_state = PPPOX_DEAD;
454 session = pppol2tp_sock_to_session(sk);
456 struct pppol2tp_session *ps;
458 l2tp_session_delete(session);
460 ps = l2tp_session_priv(session);
461 mutex_lock(&ps->sk_lock);
462 ps->__sk = rcu_dereference_protected(ps->sk,
463 lockdep_is_held(&ps->sk_lock));
464 RCU_INIT_POINTER(ps->sk, NULL);
465 mutex_unlock(&ps->sk_lock);
466 call_rcu(&ps->rcu, pppol2tp_put_sk);
468 /* Rely on the sock_put() call at the end of the function for
469 * dropping the reference held by pppol2tp_sock_to_session().
470 * The last reference will be dropped by pppol2tp_put_sk().
476 /* This will delete the session context via
477 * pppol2tp_session_destruct() if the socket's refcnt drops to
489 static struct proto pppol2tp_sk_proto = {
491 .owner = THIS_MODULE,
492 .obj_size = sizeof(struct pppox_sock),
495 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
499 rc = l2tp_udp_encap_recv(sk, skb);
503 return NET_RX_SUCCESS;
506 /* socket() handler. Initialize a new struct sock.
508 static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
513 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
517 sock_init_data(sock, sk);
519 sock->state = SS_UNCONNECTED;
520 sock->ops = &pppol2tp_ops;
522 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
523 sk->sk_protocol = PX_PROTO_OL2TP;
524 sk->sk_family = PF_PPPOX;
525 sk->sk_state = PPPOX_NONE;
526 sk->sk_type = SOCK_STREAM;
527 sk->sk_destruct = pppol2tp_session_destruct;
535 static void pppol2tp_show(struct seq_file *m, void *arg)
537 struct l2tp_session *session = arg;
540 sk = pppol2tp_session_get_sock(session);
542 struct pppox_sock *po = pppox_sk(sk);
544 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
549 static void pppol2tp_session_init(struct l2tp_session *session)
551 struct pppol2tp_session *ps;
553 session->recv_skb = pppol2tp_recv;
554 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
555 session->show = pppol2tp_show;
557 ps = l2tp_session_priv(session);
558 mutex_init(&ps->sk_lock);
559 ps->owner = current->pid;
562 struct l2tp_connect_info {
571 static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
572 struct l2tp_connect_info *info)
575 case sizeof(struct sockaddr_pppol2tp):
577 const struct sockaddr_pppol2tp *sa_v2in4 = sa;
579 if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
583 info->fd = sa_v2in4->pppol2tp.fd;
584 info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
585 info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
586 info->session_id = sa_v2in4->pppol2tp.s_session;
587 info->peer_session_id = sa_v2in4->pppol2tp.d_session;
591 case sizeof(struct sockaddr_pppol2tpv3):
593 const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
595 if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
599 info->fd = sa_v3in4->pppol2tp.fd;
600 info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
601 info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
602 info->session_id = sa_v3in4->pppol2tp.s_session;
603 info->peer_session_id = sa_v3in4->pppol2tp.d_session;
607 case sizeof(struct sockaddr_pppol2tpin6):
609 const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
611 if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
615 info->fd = sa_v2in6->pppol2tp.fd;
616 info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
617 info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
618 info->session_id = sa_v2in6->pppol2tp.s_session;
619 info->peer_session_id = sa_v2in6->pppol2tp.d_session;
623 case sizeof(struct sockaddr_pppol2tpv3in6):
625 const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
627 if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
631 info->fd = sa_v3in6->pppol2tp.fd;
632 info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
633 info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
634 info->session_id = sa_v3in6->pppol2tp.s_session;
635 info->peer_session_id = sa_v3in6->pppol2tp.d_session;
646 /* Rough estimation of the maximum payload size a tunnel can transmit without
647 * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
648 * numbers and no IP option. Not quite accurate, but the result is mostly
651 static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
655 mtu = l2tp_tunnel_dst_mtu(tunnel);
656 if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
657 return 1500 - PPPOL2TP_HEADER_OVERHEAD;
659 return mtu - PPPOL2TP_HEADER_OVERHEAD;
662 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
664 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
665 int sockaddr_len, int flags)
667 struct sock *sk = sock->sk;
668 struct pppox_sock *po = pppox_sk(sk);
669 struct l2tp_session *session = NULL;
670 struct l2tp_connect_info info;
671 struct l2tp_tunnel *tunnel;
672 struct pppol2tp_session *ps;
673 struct l2tp_session_cfg cfg = { 0, };
674 bool drop_refcnt = false;
675 bool drop_tunnel = false;
676 bool new_session = false;
677 bool new_tunnel = false;
680 error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
686 /* Check for already bound sockets */
688 if (sk->sk_state & PPPOX_CONNECTED)
691 /* We don't supporting rebinding anyway */
693 if (sk->sk_user_data)
694 goto end; /* socket is already attached */
696 /* Don't bind if tunnel_id is 0 */
701 tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
705 /* Special case: create tunnel context if session_id and
706 * peer_session_id is 0. Otherwise look up tunnel using supplied
709 if (!info.session_id && !info.peer_session_id) {
711 struct l2tp_tunnel_cfg tcfg = {
712 .encap = L2TP_ENCAPTYPE_UDP,
716 /* Prevent l2tp_tunnel_register() from trying to set up
724 error = l2tp_tunnel_create(sock_net(sk), info.fd,
727 info.peer_tunnel_id, &tcfg,
732 l2tp_tunnel_inc_refcount(tunnel);
733 error = l2tp_tunnel_register(tunnel, sock_net(sk),
743 /* Error if we can't find the tunnel */
748 /* Error if socket is not prepped */
753 if (tunnel->peer_tunnel_id == 0)
754 tunnel->peer_tunnel_id = info.peer_tunnel_id;
756 session = l2tp_tunnel_get_session(tunnel, info.session_id);
760 if (session->pwtype != L2TP_PWTYPE_PPP) {
765 ps = l2tp_session_priv(session);
767 /* Using a pre-existing session is fine as long as it hasn't
768 * been connected yet.
770 mutex_lock(&ps->sk_lock);
771 if (rcu_dereference_protected(ps->sk,
772 lockdep_is_held(&ps->sk_lock)) ||
774 mutex_unlock(&ps->sk_lock);
779 cfg.pw_type = L2TP_PWTYPE_PPP;
781 session = l2tp_session_create(sizeof(struct pppol2tp_session),
782 tunnel, info.session_id,
783 info.peer_session_id, &cfg);
784 if (IS_ERR(session)) {
785 error = PTR_ERR(session);
789 pppol2tp_session_init(session);
790 ps = l2tp_session_priv(session);
791 l2tp_session_inc_refcount(session);
793 mutex_lock(&ps->sk_lock);
794 error = l2tp_session_register(session, tunnel);
796 mutex_unlock(&ps->sk_lock);
804 /* Special case: if source & dest session_id == 0x0000, this
805 * socket is being created to manage the tunnel. Just set up
806 * the internal context for use by ioctl() and sockopt()
809 if (session->session_id == 0 && session->peer_session_id == 0) {
814 /* The only header we need to worry about is the L2TP
815 * header. This size is different depending on whether
816 * sequence numbers are enabled for the data channel.
818 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
820 po->chan.private = sk;
821 po->chan.ops = &pppol2tp_chan_ops;
822 po->chan.mtu = pppol2tp_tunnel_mtu(tunnel);
824 error = ppp_register_net_channel(sock_net(sk), &po->chan);
826 mutex_unlock(&ps->sk_lock);
831 /* This is how we get the session context from the socket. */
832 sk->sk_user_data = session;
833 rcu_assign_pointer(ps->sk, sk);
834 mutex_unlock(&ps->sk_lock);
836 /* Keep the reference we've grabbed on the session: sk doesn't expect
837 * the session to disappear. pppol2tp_session_destruct() is responsible
842 sk->sk_state = PPPOX_CONNECTED;
843 l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
849 l2tp_session_delete(session);
851 l2tp_tunnel_delete(tunnel);
854 l2tp_session_dec_refcount(session);
856 l2tp_tunnel_dec_refcount(tunnel);
862 #ifdef CONFIG_L2TP_V3
864 /* Called when creating sessions via the netlink interface. */
865 static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
866 u32 session_id, u32 peer_session_id,
867 struct l2tp_session_cfg *cfg)
870 struct l2tp_session *session;
872 /* Error if tunnel socket is not prepped */
878 /* Allocate and initialize a new session context. */
879 session = l2tp_session_create(sizeof(struct pppol2tp_session),
881 peer_session_id, cfg);
882 if (IS_ERR(session)) {
883 error = PTR_ERR(session);
887 pppol2tp_session_init(session);
889 error = l2tp_session_register(session, tunnel);
901 #endif /* CONFIG_L2TP_V3 */
903 /* getname() support.
905 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
910 struct l2tp_session *session;
911 struct l2tp_tunnel *tunnel;
912 struct sock *sk = sock->sk;
913 struct inet_sock *inet;
914 struct pppol2tp_session *pls;
919 if (!(sk->sk_state & PPPOX_CONNECTED))
923 session = pppol2tp_sock_to_session(sk);
927 pls = l2tp_session_priv(session);
928 tunnel = session->tunnel;
930 inet = inet_sk(tunnel->sock);
931 if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET) {
932 struct sockaddr_pppol2tp sp;
936 sp.sa_family = AF_PPPOX;
937 sp.sa_protocol = PX_PROTO_OL2TP;
938 sp.pppol2tp.fd = tunnel->fd;
939 sp.pppol2tp.pid = pls->owner;
940 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
941 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
942 sp.pppol2tp.s_session = session->session_id;
943 sp.pppol2tp.d_session = session->peer_session_id;
944 sp.pppol2tp.addr.sin_family = AF_INET;
945 sp.pppol2tp.addr.sin_port = inet->inet_dport;
946 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
947 memcpy(uaddr, &sp, len);
948 #if IS_ENABLED(CONFIG_IPV6)
949 } else if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET6) {
950 struct sockaddr_pppol2tpin6 sp;
954 sp.sa_family = AF_PPPOX;
955 sp.sa_protocol = PX_PROTO_OL2TP;
956 sp.pppol2tp.fd = tunnel->fd;
957 sp.pppol2tp.pid = pls->owner;
958 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
959 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
960 sp.pppol2tp.s_session = session->session_id;
961 sp.pppol2tp.d_session = session->peer_session_id;
962 sp.pppol2tp.addr.sin6_family = AF_INET6;
963 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
964 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
965 sizeof(tunnel->sock->sk_v6_daddr));
966 memcpy(uaddr, &sp, len);
967 } else if (tunnel->version == 3 && tunnel->sock->sk_family == AF_INET6) {
968 struct sockaddr_pppol2tpv3in6 sp;
972 sp.sa_family = AF_PPPOX;
973 sp.sa_protocol = PX_PROTO_OL2TP;
974 sp.pppol2tp.fd = tunnel->fd;
975 sp.pppol2tp.pid = pls->owner;
976 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
977 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
978 sp.pppol2tp.s_session = session->session_id;
979 sp.pppol2tp.d_session = session->peer_session_id;
980 sp.pppol2tp.addr.sin6_family = AF_INET6;
981 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
982 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
983 sizeof(tunnel->sock->sk_v6_daddr));
984 memcpy(uaddr, &sp, len);
986 } else if (tunnel->version == 3) {
987 struct sockaddr_pppol2tpv3 sp;
991 sp.sa_family = AF_PPPOX;
992 sp.sa_protocol = PX_PROTO_OL2TP;
993 sp.pppol2tp.fd = tunnel->fd;
994 sp.pppol2tp.pid = pls->owner;
995 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
996 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
997 sp.pppol2tp.s_session = session->session_id;
998 sp.pppol2tp.d_session = session->peer_session_id;
999 sp.pppol2tp.addr.sin_family = AF_INET;
1000 sp.pppol2tp.addr.sin_port = inet->inet_dport;
1001 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
1002 memcpy(uaddr, &sp, len);
1012 /****************************************************************************
1015 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1016 * sockets. However, in order to control kernel tunnel features, we allow
1017 * userspace to create a special "tunnel" PPPoX socket which is used for
1018 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1019 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1021 ****************************************************************************/
1023 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1024 const struct l2tp_stats *stats)
1026 memset(dest, 0, sizeof(*dest));
1028 dest->tx_packets = atomic_long_read(&stats->tx_packets);
1029 dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1030 dest->tx_errors = atomic_long_read(&stats->tx_errors);
1031 dest->rx_packets = atomic_long_read(&stats->rx_packets);
1032 dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1033 dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1034 dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1035 dest->rx_errors = atomic_long_read(&stats->rx_errors);
1038 static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
1039 struct l2tp_tunnel *tunnel)
1041 struct l2tp_session *session;
1043 if (!stats->session_id) {
1044 pppol2tp_copy_stats(stats, &tunnel->stats);
1048 /* If session_id is set, search the corresponding session in the
1049 * context of this tunnel and record the session's statistics.
1051 session = l2tp_tunnel_get_session(tunnel, stats->session_id);
1055 if (session->pwtype != L2TP_PWTYPE_PPP) {
1056 l2tp_session_dec_refcount(session);
1060 pppol2tp_copy_stats(stats, &session->stats);
1061 l2tp_session_dec_refcount(session);
1066 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1069 struct pppol2tp_ioc_stats stats;
1070 struct l2tp_session *session;
1075 session = sock->sk->sk_user_data;
1079 /* Not defined for tunnels */
1080 if (!session->session_id && !session->peer_session_id)
1083 if (put_user(0, (int __user *)arg))
1089 session = sock->sk->sk_user_data;
1093 /* Not defined for tunnels */
1094 if (!session->session_id && !session->peer_session_id)
1097 if (!access_ok((int __user *)arg, sizeof(int)))
1101 case PPPIOCGL2TPSTATS:
1102 session = sock->sk->sk_user_data;
1106 /* Session 0 represents the parent tunnel */
1107 if (!session->session_id && !session->peer_session_id) {
1111 if (copy_from_user(&stats, (void __user *)arg,
1115 session_id = stats.session_id;
1116 err = pppol2tp_tunnel_copy_stats(&stats,
1121 stats.session_id = session_id;
1123 pppol2tp_copy_stats(&stats, &session->stats);
1124 stats.session_id = session->session_id;
1126 stats.tunnel_id = session->tunnel->tunnel_id;
1127 stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
1129 if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
1134 return -ENOIOCTLCMD;
1140 /*****************************************************************************
1141 * setsockopt() / getsockopt() support.
1143 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1144 * sockets. In order to control kernel tunnel features, we allow userspace to
1145 * create a special "tunnel" PPPoX socket which is used for control only.
1146 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1147 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1148 *****************************************************************************/
1150 /* Tunnel setsockopt() helper.
1152 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1153 struct l2tp_tunnel *tunnel,
1154 int optname, int val)
1159 case PPPOL2TP_SO_DEBUG:
1160 tunnel->debug = val;
1161 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1162 tunnel->name, tunnel->debug);
1173 /* Session setsockopt helper.
1175 static int pppol2tp_session_setsockopt(struct sock *sk,
1176 struct l2tp_session *session,
1177 int optname, int val)
1182 case PPPOL2TP_SO_RECVSEQ:
1183 if (val != 0 && val != 1) {
1187 session->recv_seq = !!val;
1188 l2tp_info(session, L2TP_MSG_CONTROL,
1189 "%s: set recv_seq=%d\n",
1190 session->name, session->recv_seq);
1193 case PPPOL2TP_SO_SENDSEQ:
1194 if (val != 0 && val != 1) {
1198 session->send_seq = !!val;
1200 struct pppox_sock *po = pppox_sk(sk);
1202 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1203 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1205 l2tp_session_set_header_len(session, session->tunnel->version);
1206 l2tp_info(session, L2TP_MSG_CONTROL,
1207 "%s: set send_seq=%d\n",
1208 session->name, session->send_seq);
1211 case PPPOL2TP_SO_LNSMODE:
1212 if (val != 0 && val != 1) {
1216 session->lns_mode = !!val;
1217 l2tp_info(session, L2TP_MSG_CONTROL,
1218 "%s: set lns_mode=%d\n",
1219 session->name, session->lns_mode);
1222 case PPPOL2TP_SO_DEBUG:
1223 session->debug = val;
1224 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1225 session->name, session->debug);
1228 case PPPOL2TP_SO_REORDERTO:
1229 session->reorder_timeout = msecs_to_jiffies(val);
1230 l2tp_info(session, L2TP_MSG_CONTROL,
1231 "%s: set reorder_timeout=%d\n",
1232 session->name, session->reorder_timeout);
1243 /* Main setsockopt() entry point.
1244 * Does API checks, then calls either the tunnel or session setsockopt
1245 * handler, according to whether the PPPoL2TP socket is a for a regular
1246 * session or the special tunnel type.
1248 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1249 sockptr_t optval, unsigned int optlen)
1251 struct sock *sk = sock->sk;
1252 struct l2tp_session *session;
1253 struct l2tp_tunnel *tunnel;
1257 if (level != SOL_PPPOL2TP)
1260 if (optlen < sizeof(int))
1263 if (copy_from_sockptr(&val, optval, sizeof(int)))
1267 if (!sk->sk_user_data)
1270 /* Get session context from the socket */
1272 session = pppol2tp_sock_to_session(sk);
1276 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1278 if (session->session_id == 0 && session->peer_session_id == 0) {
1279 tunnel = session->tunnel;
1280 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1282 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1290 /* Tunnel getsockopt helper. Called with sock locked.
1292 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1293 struct l2tp_tunnel *tunnel,
1294 int optname, int *val)
1299 case PPPOL2TP_SO_DEBUG:
1300 *val = tunnel->debug;
1301 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
1302 tunnel->name, tunnel->debug);
1313 /* Session getsockopt helper. Called with sock locked.
1315 static int pppol2tp_session_getsockopt(struct sock *sk,
1316 struct l2tp_session *session,
1317 int optname, int *val)
1322 case PPPOL2TP_SO_RECVSEQ:
1323 *val = session->recv_seq;
1324 l2tp_info(session, L2TP_MSG_CONTROL,
1325 "%s: get recv_seq=%d\n", session->name, *val);
1328 case PPPOL2TP_SO_SENDSEQ:
1329 *val = session->send_seq;
1330 l2tp_info(session, L2TP_MSG_CONTROL,
1331 "%s: get send_seq=%d\n", session->name, *val);
1334 case PPPOL2TP_SO_LNSMODE:
1335 *val = session->lns_mode;
1336 l2tp_info(session, L2TP_MSG_CONTROL,
1337 "%s: get lns_mode=%d\n", session->name, *val);
1340 case PPPOL2TP_SO_DEBUG:
1341 *val = session->debug;
1342 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
1343 session->name, *val);
1346 case PPPOL2TP_SO_REORDERTO:
1347 *val = (int)jiffies_to_msecs(session->reorder_timeout);
1348 l2tp_info(session, L2TP_MSG_CONTROL,
1349 "%s: get reorder_timeout=%d\n", session->name, *val);
1359 /* Main getsockopt() entry point.
1360 * Does API checks, then calls either the tunnel or session getsockopt
1361 * handler, according to whether the PPPoX socket is a for a regular session
1362 * or the special tunnel type.
1364 static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1365 char __user *optval, int __user *optlen)
1367 struct sock *sk = sock->sk;
1368 struct l2tp_session *session;
1369 struct l2tp_tunnel *tunnel;
1373 if (level != SOL_PPPOL2TP)
1376 if (get_user(len, optlen))
1379 len = min_t(unsigned int, len, sizeof(int));
1385 if (!sk->sk_user_data)
1388 /* Get the session context */
1390 session = pppol2tp_sock_to_session(sk);
1394 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1395 if (session->session_id == 0 && session->peer_session_id == 0) {
1396 tunnel = session->tunnel;
1397 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1401 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1407 if (put_user(len, optlen))
1410 if (copy_to_user((void __user *)optval, &val, len))
1421 /*****************************************************************************
1422 * /proc filesystem for debug
1423 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1424 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1425 *****************************************************************************/
1427 static unsigned int pppol2tp_net_id;
1429 #ifdef CONFIG_PROC_FS
1431 struct pppol2tp_seq_data {
1432 struct seq_net_private p;
1433 int tunnel_idx; /* current tunnel */
1434 int session_idx; /* index of session within current tunnel */
1435 struct l2tp_tunnel *tunnel;
1436 struct l2tp_session *session; /* NULL means get next tunnel */
1439 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1441 /* Drop reference taken during previous invocation */
1443 l2tp_tunnel_dec_refcount(pd->tunnel);
1446 pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
1449 /* Only accept L2TPv2 tunnels */
1450 if (!pd->tunnel || pd->tunnel->version == 2)
1453 l2tp_tunnel_dec_refcount(pd->tunnel);
1457 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1459 /* Drop reference taken during previous invocation */
1461 l2tp_session_dec_refcount(pd->session);
1463 pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
1467 pd->session_idx = 0;
1468 pppol2tp_next_tunnel(net, pd);
1472 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1474 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1481 BUG_ON(!m->private);
1483 net = seq_file_net(m);
1486 pppol2tp_next_tunnel(net, pd);
1488 pppol2tp_next_session(net, pd);
1490 /* NULL tunnel and session indicates end of list */
1491 if (!pd->tunnel && !pd->session)
1498 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1504 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1506 struct pppol2tp_seq_data *pd = v;
1508 if (!pd || pd == SEQ_START_TOKEN)
1511 /* Drop reference taken by last invocation of pppol2tp_next_session()
1512 * or pppol2tp_next_tunnel().
1515 l2tp_session_dec_refcount(pd->session);
1519 l2tp_tunnel_dec_refcount(pd->tunnel);
1524 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1526 struct l2tp_tunnel *tunnel = v;
1528 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1530 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1531 refcount_read(&tunnel->ref_count) - 1);
1532 seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1534 atomic_long_read(&tunnel->stats.tx_packets),
1535 atomic_long_read(&tunnel->stats.tx_bytes),
1536 atomic_long_read(&tunnel->stats.tx_errors),
1537 atomic_long_read(&tunnel->stats.rx_packets),
1538 atomic_long_read(&tunnel->stats.rx_bytes),
1539 atomic_long_read(&tunnel->stats.rx_errors));
1542 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1544 struct l2tp_session *session = v;
1545 struct l2tp_tunnel *tunnel = session->tunnel;
1546 unsigned char state;
1553 struct inet_sock *inet = inet_sk(tunnel->sock);
1555 ip = ntohl(inet->inet_saddr);
1556 port = ntohs(inet->inet_sport);
1559 sk = pppol2tp_session_get_sock(session);
1561 state = sk->sk_state;
1562 user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1568 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> %04X/%04X %d %c\n",
1569 session->name, ip, port,
1571 session->session_id,
1572 tunnel->peer_tunnel_id,
1573 session->peer_session_id,
1574 state, user_data_ok);
1575 seq_printf(m, " 0/0/%c/%c/%s %08x %u\n",
1576 session->recv_seq ? 'R' : '-',
1577 session->send_seq ? 'S' : '-',
1578 session->lns_mode ? "LNS" : "LAC",
1580 jiffies_to_msecs(session->reorder_timeout));
1581 seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
1582 session->nr, session->ns,
1583 atomic_long_read(&session->stats.tx_packets),
1584 atomic_long_read(&session->stats.tx_bytes),
1585 atomic_long_read(&session->stats.tx_errors),
1586 atomic_long_read(&session->stats.rx_packets),
1587 atomic_long_read(&session->stats.rx_bytes),
1588 atomic_long_read(&session->stats.rx_errors));
1591 struct pppox_sock *po = pppox_sk(sk);
1593 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
1598 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1600 struct pppol2tp_seq_data *pd = v;
1602 /* display header on line 1 */
1603 if (v == SEQ_START_TOKEN) {
1604 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1605 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1606 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1607 seq_puts(m, " SESSION name, addr/port src-tid/sid dest-tid/sid state user-data-ok\n");
1608 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1609 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1614 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1616 pppol2tp_seq_session_show(m, pd->session);
1622 static const struct seq_operations pppol2tp_seq_ops = {
1623 .start = pppol2tp_seq_start,
1624 .next = pppol2tp_seq_next,
1625 .stop = pppol2tp_seq_stop,
1626 .show = pppol2tp_seq_show,
1628 #endif /* CONFIG_PROC_FS */
1630 /*****************************************************************************
1632 *****************************************************************************/
1634 static __net_init int pppol2tp_init_net(struct net *net)
1636 struct proc_dir_entry *pde;
1639 pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1640 &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
1650 static __net_exit void pppol2tp_exit_net(struct net *net)
1652 remove_proc_entry("pppol2tp", net->proc_net);
1655 static struct pernet_operations pppol2tp_net_ops = {
1656 .init = pppol2tp_init_net,
1657 .exit = pppol2tp_exit_net,
1658 .id = &pppol2tp_net_id,
1661 /*****************************************************************************
1663 *****************************************************************************/
1665 static const struct proto_ops pppol2tp_ops = {
1667 .owner = THIS_MODULE,
1668 .release = pppol2tp_release,
1669 .bind = sock_no_bind,
1670 .connect = pppol2tp_connect,
1671 .socketpair = sock_no_socketpair,
1672 .accept = sock_no_accept,
1673 .getname = pppol2tp_getname,
1674 .poll = datagram_poll,
1675 .listen = sock_no_listen,
1676 .shutdown = sock_no_shutdown,
1677 .setsockopt = pppol2tp_setsockopt,
1678 .getsockopt = pppol2tp_getsockopt,
1679 .sendmsg = pppol2tp_sendmsg,
1680 .recvmsg = pppol2tp_recvmsg,
1681 .mmap = sock_no_mmap,
1682 .ioctl = pppox_ioctl,
1683 #ifdef CONFIG_COMPAT
1684 .compat_ioctl = pppox_compat_ioctl,
1688 static const struct pppox_proto pppol2tp_proto = {
1689 .create = pppol2tp_create,
1690 .ioctl = pppol2tp_ioctl,
1691 .owner = THIS_MODULE,
1694 #ifdef CONFIG_L2TP_V3
1696 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1697 .session_create = pppol2tp_session_create,
1698 .session_delete = l2tp_session_delete,
1701 #endif /* CONFIG_L2TP_V3 */
1703 static int __init pppol2tp_init(void)
1707 err = register_pernet_device(&pppol2tp_net_ops);
1711 err = proto_register(&pppol2tp_sk_proto, 0);
1713 goto out_unregister_pppol2tp_pernet;
1715 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1717 goto out_unregister_pppol2tp_proto;
1719 #ifdef CONFIG_L2TP_V3
1720 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1722 goto out_unregister_pppox;
1725 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1730 #ifdef CONFIG_L2TP_V3
1731 out_unregister_pppox:
1732 unregister_pppox_proto(PX_PROTO_OL2TP);
1734 out_unregister_pppol2tp_proto:
1735 proto_unregister(&pppol2tp_sk_proto);
1736 out_unregister_pppol2tp_pernet:
1737 unregister_pernet_device(&pppol2tp_net_ops);
1741 static void __exit pppol2tp_exit(void)
1743 #ifdef CONFIG_L2TP_V3
1744 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1746 unregister_pppox_proto(PX_PROTO_OL2TP);
1747 proto_unregister(&pppol2tp_sk_proto);
1748 unregister_pernet_device(&pppol2tp_net_ops);
1751 module_init(pppol2tp_init);
1752 module_exit(pppol2tp_exit);
1754 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1755 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1756 MODULE_LICENSE("GPL");
1757 MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1758 MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1759 MODULE_ALIAS_L2TP_PWTYPE(7);