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 negotiated 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;
241 ppp_input(&po->chan, skb);
243 if (sock_queue_rcv_skb(sk, skb) < 0) {
244 atomic_long_inc(&session->stats.rx_errors);
254 pr_warn_ratelimited("%s: no socket in recv\n", session->name);
258 /************************************************************************
260 ***********************************************************************/
262 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
263 * when a user application does a sendmsg() on the session socket. L2TP and
264 * PPP headers must be inserted into the user's data.
266 static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
269 struct sock *sk = sock->sk;
272 struct l2tp_session *session;
273 struct l2tp_tunnel *tunnel;
277 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
280 /* Get session and tunnel contexts */
282 session = pppol2tp_sock_to_session(sk);
286 tunnel = session->tunnel;
288 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
290 /* Allocate a socket buffer */
292 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
293 uhlen + session->hdr_len +
294 2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
299 /* Reserve space for headers. */
300 skb_reserve(skb, NET_SKB_PAD);
301 skb_reset_network_header(skb);
302 skb_reserve(skb, sizeof(struct iphdr));
303 skb_reset_transport_header(skb);
304 skb_reserve(skb, uhlen);
307 skb->data[0] = PPP_ALLSTATIONS;
308 skb->data[1] = PPP_UI;
311 /* Copy user data into skb */
312 error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
319 l2tp_xmit_skb(session, skb);
332 /* Transmit function called by generic PPP driver. Sends PPP frame
333 * over PPPoL2TP socket.
335 * This is almost the same as pppol2tp_sendmsg(), but rather than
336 * being called with a msghdr from userspace, it is called with a skb
339 * The supplied skb from ppp doesn't have enough headroom for the
340 * insertion of L2TP, UDP and IP headers so we need to allocate more
341 * headroom in the skb. This will create a cloned skb. But we must be
342 * careful in the error case because the caller will expect to free
343 * the skb it supplied, not our cloned skb. So we take care to always
344 * leave the original skb unfreed if we return an error.
346 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
348 struct sock *sk = (struct sock *)chan->private;
349 struct l2tp_session *session;
350 struct l2tp_tunnel *tunnel;
353 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
356 /* Get session and tunnel contexts from the socket */
357 session = pppol2tp_sock_to_session(sk);
361 tunnel = session->tunnel;
363 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
364 headroom = NET_SKB_PAD +
365 sizeof(struct iphdr) + /* IP header */
366 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
367 session->hdr_len + /* L2TP header */
368 2; /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
369 if (skb_cow_head(skb, headroom))
372 /* Setup PPP header */
374 skb->data[0] = PPP_ALLSTATIONS;
375 skb->data[1] = PPP_UI;
378 l2tp_xmit_skb(session, skb);
388 /* Free the original skb */
393 /*****************************************************************************
394 * Session (and tunnel control) socket create/destroy.
395 *****************************************************************************/
397 static void pppol2tp_put_sk(struct rcu_head *head)
399 struct pppol2tp_session *ps;
401 ps = container_of(head, typeof(*ps), rcu);
405 /* Really kill the session socket. (Called from sock_put() if
408 static void pppol2tp_session_destruct(struct sock *sk)
410 struct l2tp_session *session = sk->sk_user_data;
412 skb_queue_purge(&sk->sk_receive_queue);
413 skb_queue_purge(&sk->sk_write_queue);
416 sk->sk_user_data = NULL;
417 if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
419 l2tp_session_dec_refcount(session);
423 /* Called when the PPPoX socket (session) is closed.
425 static int pppol2tp_release(struct socket *sock)
427 struct sock *sk = sock->sk;
428 struct l2tp_session *session;
436 if (sock_flag(sk, SOCK_DEAD) != 0)
439 pppox_unbind_sock(sk);
441 /* Signal the death of the socket. */
442 sk->sk_state = PPPOX_DEAD;
446 session = pppol2tp_sock_to_session(sk);
448 struct pppol2tp_session *ps;
450 l2tp_session_delete(session);
452 ps = l2tp_session_priv(session);
453 mutex_lock(&ps->sk_lock);
454 ps->__sk = rcu_dereference_protected(ps->sk,
455 lockdep_is_held(&ps->sk_lock));
456 RCU_INIT_POINTER(ps->sk, NULL);
457 mutex_unlock(&ps->sk_lock);
458 call_rcu(&ps->rcu, pppol2tp_put_sk);
460 /* Rely on the sock_put() call at the end of the function for
461 * dropping the reference held by pppol2tp_sock_to_session().
462 * The last reference will be dropped by pppol2tp_put_sk().
468 /* This will delete the session context via
469 * pppol2tp_session_destruct() if the socket's refcnt drops to
481 static struct proto pppol2tp_sk_proto = {
483 .owner = THIS_MODULE,
484 .obj_size = sizeof(struct pppox_sock),
487 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
491 rc = l2tp_udp_encap_recv(sk, skb);
495 return NET_RX_SUCCESS;
498 /* socket() handler. Initialize a new struct sock.
500 static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
505 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
509 sock_init_data(sock, sk);
511 sock->state = SS_UNCONNECTED;
512 sock->ops = &pppol2tp_ops;
514 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
515 sk->sk_protocol = PX_PROTO_OL2TP;
516 sk->sk_family = PF_PPPOX;
517 sk->sk_state = PPPOX_NONE;
518 sk->sk_type = SOCK_STREAM;
519 sk->sk_destruct = pppol2tp_session_destruct;
527 static void pppol2tp_show(struct seq_file *m, void *arg)
529 struct l2tp_session *session = arg;
532 sk = pppol2tp_session_get_sock(session);
534 struct pppox_sock *po = pppox_sk(sk);
536 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
541 static void pppol2tp_session_init(struct l2tp_session *session)
543 struct pppol2tp_session *ps;
545 session->recv_skb = pppol2tp_recv;
546 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
547 session->show = pppol2tp_show;
549 ps = l2tp_session_priv(session);
550 mutex_init(&ps->sk_lock);
551 ps->owner = current->pid;
554 struct l2tp_connect_info {
563 static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
564 struct l2tp_connect_info *info)
567 case sizeof(struct sockaddr_pppol2tp):
569 const struct sockaddr_pppol2tp *sa_v2in4 = sa;
571 if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
575 info->fd = sa_v2in4->pppol2tp.fd;
576 info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
577 info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
578 info->session_id = sa_v2in4->pppol2tp.s_session;
579 info->peer_session_id = sa_v2in4->pppol2tp.d_session;
583 case sizeof(struct sockaddr_pppol2tpv3):
585 const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
587 if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
591 info->fd = sa_v3in4->pppol2tp.fd;
592 info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
593 info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
594 info->session_id = sa_v3in4->pppol2tp.s_session;
595 info->peer_session_id = sa_v3in4->pppol2tp.d_session;
599 case sizeof(struct sockaddr_pppol2tpin6):
601 const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
603 if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
607 info->fd = sa_v2in6->pppol2tp.fd;
608 info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
609 info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
610 info->session_id = sa_v2in6->pppol2tp.s_session;
611 info->peer_session_id = sa_v2in6->pppol2tp.d_session;
615 case sizeof(struct sockaddr_pppol2tpv3in6):
617 const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
619 if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
623 info->fd = sa_v3in6->pppol2tp.fd;
624 info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
625 info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
626 info->session_id = sa_v3in6->pppol2tp.s_session;
627 info->peer_session_id = sa_v3in6->pppol2tp.d_session;
638 /* Rough estimation of the maximum payload size a tunnel can transmit without
639 * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
640 * numbers and no IP option. Not quite accurate, but the result is mostly
643 static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
647 mtu = l2tp_tunnel_dst_mtu(tunnel);
648 if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
649 return 1500 - PPPOL2TP_HEADER_OVERHEAD;
651 return mtu - PPPOL2TP_HEADER_OVERHEAD;
654 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
656 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
657 int sockaddr_len, int flags)
659 struct sock *sk = sock->sk;
660 struct pppox_sock *po = pppox_sk(sk);
661 struct l2tp_session *session = NULL;
662 struct l2tp_connect_info info;
663 struct l2tp_tunnel *tunnel;
664 struct pppol2tp_session *ps;
665 struct l2tp_session_cfg cfg = { 0, };
666 bool drop_refcnt = false;
667 bool drop_tunnel = false;
668 bool new_session = false;
669 bool new_tunnel = false;
672 error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
678 /* Check for already bound sockets */
680 if (sk->sk_state & PPPOX_CONNECTED)
683 /* We don't supporting rebinding anyway */
685 if (sk->sk_user_data)
686 goto end; /* socket is already attached */
688 /* Don't bind if tunnel_id is 0 */
693 tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
697 /* Special case: create tunnel context if session_id and
698 * peer_session_id is 0. Otherwise look up tunnel using supplied
701 if (!info.session_id && !info.peer_session_id) {
703 struct l2tp_tunnel_cfg tcfg = {
704 .encap = L2TP_ENCAPTYPE_UDP,
707 /* Prevent l2tp_tunnel_register() from trying to set up
715 error = l2tp_tunnel_create(info.fd,
718 info.peer_tunnel_id, &tcfg,
723 l2tp_tunnel_inc_refcount(tunnel);
724 error = l2tp_tunnel_register(tunnel, sock_net(sk),
734 /* Error if we can't find the tunnel */
739 /* Error if socket is not prepped */
744 if (tunnel->peer_tunnel_id == 0)
745 tunnel->peer_tunnel_id = info.peer_tunnel_id;
747 session = l2tp_tunnel_get_session(tunnel, info.session_id);
751 if (session->pwtype != L2TP_PWTYPE_PPP) {
756 ps = l2tp_session_priv(session);
758 /* Using a pre-existing session is fine as long as it hasn't
759 * been connected yet.
761 mutex_lock(&ps->sk_lock);
762 if (rcu_dereference_protected(ps->sk,
763 lockdep_is_held(&ps->sk_lock)) ||
765 mutex_unlock(&ps->sk_lock);
770 cfg.pw_type = L2TP_PWTYPE_PPP;
772 session = l2tp_session_create(sizeof(struct pppol2tp_session),
773 tunnel, info.session_id,
774 info.peer_session_id, &cfg);
775 if (IS_ERR(session)) {
776 error = PTR_ERR(session);
780 pppol2tp_session_init(session);
781 ps = l2tp_session_priv(session);
782 l2tp_session_inc_refcount(session);
784 mutex_lock(&ps->sk_lock);
785 error = l2tp_session_register(session, tunnel);
787 mutex_unlock(&ps->sk_lock);
795 /* Special case: if source & dest session_id == 0x0000, this
796 * socket is being created to manage the tunnel. Just set up
797 * the internal context for use by ioctl() and sockopt()
800 if (session->session_id == 0 && session->peer_session_id == 0) {
805 /* The only header we need to worry about is the L2TP
806 * header. This size is different depending on whether
807 * sequence numbers are enabled for the data channel.
809 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
811 po->chan.private = sk;
812 po->chan.ops = &pppol2tp_chan_ops;
813 po->chan.mtu = pppol2tp_tunnel_mtu(tunnel);
815 error = ppp_register_net_channel(sock_net(sk), &po->chan);
817 mutex_unlock(&ps->sk_lock);
822 /* This is how we get the session context from the socket. */
823 sk->sk_user_data = session;
824 rcu_assign_pointer(ps->sk, sk);
825 mutex_unlock(&ps->sk_lock);
827 /* Keep the reference we've grabbed on the session: sk doesn't expect
828 * the session to disappear. pppol2tp_session_destruct() is responsible
833 sk->sk_state = PPPOX_CONNECTED;
838 l2tp_session_delete(session);
840 l2tp_tunnel_delete(tunnel);
843 l2tp_session_dec_refcount(session);
845 l2tp_tunnel_dec_refcount(tunnel);
851 #ifdef CONFIG_L2TP_V3
853 /* Called when creating sessions via the netlink interface. */
854 static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
855 u32 session_id, u32 peer_session_id,
856 struct l2tp_session_cfg *cfg)
859 struct l2tp_session *session;
861 /* Error if tunnel socket is not prepped */
867 /* Allocate and initialize a new session context. */
868 session = l2tp_session_create(sizeof(struct pppol2tp_session),
870 peer_session_id, cfg);
871 if (IS_ERR(session)) {
872 error = PTR_ERR(session);
876 pppol2tp_session_init(session);
878 error = l2tp_session_register(session, tunnel);
890 #endif /* CONFIG_L2TP_V3 */
892 /* getname() support.
894 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
899 struct l2tp_session *session;
900 struct l2tp_tunnel *tunnel;
901 struct sock *sk = sock->sk;
902 struct inet_sock *inet;
903 struct pppol2tp_session *pls;
908 if (!(sk->sk_state & PPPOX_CONNECTED))
912 session = pppol2tp_sock_to_session(sk);
916 pls = l2tp_session_priv(session);
917 tunnel = session->tunnel;
919 inet = inet_sk(tunnel->sock);
920 if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET) {
921 struct sockaddr_pppol2tp sp;
925 sp.sa_family = AF_PPPOX;
926 sp.sa_protocol = PX_PROTO_OL2TP;
927 sp.pppol2tp.fd = tunnel->fd;
928 sp.pppol2tp.pid = pls->owner;
929 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
930 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
931 sp.pppol2tp.s_session = session->session_id;
932 sp.pppol2tp.d_session = session->peer_session_id;
933 sp.pppol2tp.addr.sin_family = AF_INET;
934 sp.pppol2tp.addr.sin_port = inet->inet_dport;
935 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
936 memcpy(uaddr, &sp, len);
937 #if IS_ENABLED(CONFIG_IPV6)
938 } else if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET6) {
939 struct sockaddr_pppol2tpin6 sp;
943 sp.sa_family = AF_PPPOX;
944 sp.sa_protocol = PX_PROTO_OL2TP;
945 sp.pppol2tp.fd = tunnel->fd;
946 sp.pppol2tp.pid = pls->owner;
947 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
948 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
949 sp.pppol2tp.s_session = session->session_id;
950 sp.pppol2tp.d_session = session->peer_session_id;
951 sp.pppol2tp.addr.sin6_family = AF_INET6;
952 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
953 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
954 sizeof(tunnel->sock->sk_v6_daddr));
955 memcpy(uaddr, &sp, len);
956 } else if (tunnel->version == 3 && tunnel->sock->sk_family == AF_INET6) {
957 struct sockaddr_pppol2tpv3in6 sp;
961 sp.sa_family = AF_PPPOX;
962 sp.sa_protocol = PX_PROTO_OL2TP;
963 sp.pppol2tp.fd = tunnel->fd;
964 sp.pppol2tp.pid = pls->owner;
965 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
966 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
967 sp.pppol2tp.s_session = session->session_id;
968 sp.pppol2tp.d_session = session->peer_session_id;
969 sp.pppol2tp.addr.sin6_family = AF_INET6;
970 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
971 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
972 sizeof(tunnel->sock->sk_v6_daddr));
973 memcpy(uaddr, &sp, len);
975 } else if (tunnel->version == 3) {
976 struct sockaddr_pppol2tpv3 sp;
980 sp.sa_family = AF_PPPOX;
981 sp.sa_protocol = PX_PROTO_OL2TP;
982 sp.pppol2tp.fd = tunnel->fd;
983 sp.pppol2tp.pid = pls->owner;
984 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
985 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
986 sp.pppol2tp.s_session = session->session_id;
987 sp.pppol2tp.d_session = session->peer_session_id;
988 sp.pppol2tp.addr.sin_family = AF_INET;
989 sp.pppol2tp.addr.sin_port = inet->inet_dport;
990 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
991 memcpy(uaddr, &sp, len);
1001 /****************************************************************************
1004 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1005 * sockets. However, in order to control kernel tunnel features, we allow
1006 * userspace to create a special "tunnel" PPPoX socket which is used for
1007 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1008 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1010 ****************************************************************************/
1012 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1013 const struct l2tp_stats *stats)
1015 memset(dest, 0, sizeof(*dest));
1017 dest->tx_packets = atomic_long_read(&stats->tx_packets);
1018 dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1019 dest->tx_errors = atomic_long_read(&stats->tx_errors);
1020 dest->rx_packets = atomic_long_read(&stats->rx_packets);
1021 dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1022 dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1023 dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1024 dest->rx_errors = atomic_long_read(&stats->rx_errors);
1027 static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
1028 struct l2tp_tunnel *tunnel)
1030 struct l2tp_session *session;
1032 if (!stats->session_id) {
1033 pppol2tp_copy_stats(stats, &tunnel->stats);
1037 /* If session_id is set, search the corresponding session in the
1038 * context of this tunnel and record the session's statistics.
1040 session = l2tp_tunnel_get_session(tunnel, stats->session_id);
1044 if (session->pwtype != L2TP_PWTYPE_PPP) {
1045 l2tp_session_dec_refcount(session);
1049 pppol2tp_copy_stats(stats, &session->stats);
1050 l2tp_session_dec_refcount(session);
1055 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1058 struct pppol2tp_ioc_stats stats;
1059 struct l2tp_session *session;
1064 session = sock->sk->sk_user_data;
1068 if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1071 /* Not defined for tunnels */
1072 if (!session->session_id && !session->peer_session_id)
1075 if (put_user(0, (int __user *)arg))
1081 session = sock->sk->sk_user_data;
1085 if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1088 /* Not defined for tunnels */
1089 if (!session->session_id && !session->peer_session_id)
1092 if (!access_ok((int __user *)arg, sizeof(int)))
1096 case PPPIOCGL2TPSTATS:
1097 session = sock->sk->sk_user_data;
1101 if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1104 /* Session 0 represents the parent tunnel */
1105 if (!session->session_id && !session->peer_session_id) {
1109 if (copy_from_user(&stats, (void __user *)arg,
1113 session_id = stats.session_id;
1114 err = pppol2tp_tunnel_copy_stats(&stats,
1119 stats.session_id = session_id;
1121 pppol2tp_copy_stats(&stats, &session->stats);
1122 stats.session_id = session->session_id;
1124 stats.tunnel_id = session->tunnel->tunnel_id;
1125 stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
1127 if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
1132 return -ENOIOCTLCMD;
1138 /*****************************************************************************
1139 * setsockopt() / getsockopt() support.
1141 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1142 * sockets. In order to control kernel tunnel features, we allow userspace to
1143 * create a special "tunnel" PPPoX socket which is used for control only.
1144 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1145 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1146 *****************************************************************************/
1148 /* Tunnel setsockopt() helper.
1150 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1151 struct l2tp_tunnel *tunnel,
1152 int optname, int val)
1157 case PPPOL2TP_SO_DEBUG:
1158 /* Tunnel debug flags option is deprecated */
1169 /* Session setsockopt helper.
1171 static int pppol2tp_session_setsockopt(struct sock *sk,
1172 struct l2tp_session *session,
1173 int optname, int val)
1178 case PPPOL2TP_SO_RECVSEQ:
1179 if (val != 0 && val != 1) {
1183 session->recv_seq = !!val;
1186 case PPPOL2TP_SO_SENDSEQ:
1187 if (val != 0 && val != 1) {
1191 session->send_seq = !!val;
1193 struct pppox_sock *po = pppox_sk(sk);
1195 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1196 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1198 l2tp_session_set_header_len(session, session->tunnel->version);
1201 case PPPOL2TP_SO_LNSMODE:
1202 if (val != 0 && val != 1) {
1206 session->lns_mode = !!val;
1209 case PPPOL2TP_SO_DEBUG:
1210 /* Session debug flags option is deprecated */
1213 case PPPOL2TP_SO_REORDERTO:
1214 session->reorder_timeout = msecs_to_jiffies(val);
1225 /* Main setsockopt() entry point.
1226 * Does API checks, then calls either the tunnel or session setsockopt
1227 * handler, according to whether the PPPoL2TP socket is a for a regular
1228 * session or the special tunnel type.
1230 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1231 sockptr_t optval, unsigned int optlen)
1233 struct sock *sk = sock->sk;
1234 struct l2tp_session *session;
1235 struct l2tp_tunnel *tunnel;
1239 if (level != SOL_PPPOL2TP)
1242 if (optlen < sizeof(int))
1245 if (copy_from_sockptr(&val, optval, sizeof(int)))
1249 if (!sk->sk_user_data)
1252 /* Get session context from the socket */
1254 session = pppol2tp_sock_to_session(sk);
1258 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1260 if (session->session_id == 0 && session->peer_session_id == 0) {
1261 tunnel = session->tunnel;
1262 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1264 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1272 /* Tunnel getsockopt helper. Called with sock locked.
1274 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1275 struct l2tp_tunnel *tunnel,
1276 int optname, int *val)
1281 case PPPOL2TP_SO_DEBUG:
1282 /* Tunnel debug flags option is deprecated */
1294 /* Session getsockopt helper. Called with sock locked.
1296 static int pppol2tp_session_getsockopt(struct sock *sk,
1297 struct l2tp_session *session,
1298 int optname, int *val)
1303 case PPPOL2TP_SO_RECVSEQ:
1304 *val = session->recv_seq;
1307 case PPPOL2TP_SO_SENDSEQ:
1308 *val = session->send_seq;
1311 case PPPOL2TP_SO_LNSMODE:
1312 *val = session->lns_mode;
1315 case PPPOL2TP_SO_DEBUG:
1316 /* Session debug flags option is deprecated */
1320 case PPPOL2TP_SO_REORDERTO:
1321 *val = (int)jiffies_to_msecs(session->reorder_timeout);
1331 /* Main getsockopt() entry point.
1332 * Does API checks, then calls either the tunnel or session getsockopt
1333 * handler, according to whether the PPPoX socket is a for a regular session
1334 * or the special tunnel type.
1336 static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1337 char __user *optval, int __user *optlen)
1339 struct sock *sk = sock->sk;
1340 struct l2tp_session *session;
1341 struct l2tp_tunnel *tunnel;
1345 if (level != SOL_PPPOL2TP)
1348 if (get_user(len, optlen))
1351 len = min_t(unsigned int, len, sizeof(int));
1357 if (!sk->sk_user_data)
1360 /* Get the session context */
1362 session = pppol2tp_sock_to_session(sk);
1366 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1367 if (session->session_id == 0 && session->peer_session_id == 0) {
1368 tunnel = session->tunnel;
1369 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1373 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1379 if (put_user(len, optlen))
1382 if (copy_to_user((void __user *)optval, &val, len))
1393 /*****************************************************************************
1394 * /proc filesystem for debug
1395 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1396 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1397 *****************************************************************************/
1399 static unsigned int pppol2tp_net_id;
1401 #ifdef CONFIG_PROC_FS
1403 struct pppol2tp_seq_data {
1404 struct seq_net_private p;
1405 int tunnel_idx; /* current tunnel */
1406 int session_idx; /* index of session within current tunnel */
1407 struct l2tp_tunnel *tunnel;
1408 struct l2tp_session *session; /* NULL means get next tunnel */
1411 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1413 /* Drop reference taken during previous invocation */
1415 l2tp_tunnel_dec_refcount(pd->tunnel);
1418 pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
1421 /* Only accept L2TPv2 tunnels */
1422 if (!pd->tunnel || pd->tunnel->version == 2)
1425 l2tp_tunnel_dec_refcount(pd->tunnel);
1429 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1431 /* Drop reference taken during previous invocation */
1433 l2tp_session_dec_refcount(pd->session);
1435 pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
1439 pd->session_idx = 0;
1440 pppol2tp_next_tunnel(net, pd);
1444 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1446 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1453 if (WARN_ON(!m->private)) {
1459 net = seq_file_net(m);
1462 pppol2tp_next_tunnel(net, pd);
1464 pppol2tp_next_session(net, pd);
1466 /* NULL tunnel and session indicates end of list */
1467 if (!pd->tunnel && !pd->session)
1474 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1480 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1482 struct pppol2tp_seq_data *pd = v;
1484 if (!pd || pd == SEQ_START_TOKEN)
1487 /* Drop reference taken by last invocation of pppol2tp_next_session()
1488 * or pppol2tp_next_tunnel().
1491 l2tp_session_dec_refcount(pd->session);
1495 l2tp_tunnel_dec_refcount(pd->tunnel);
1500 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1502 struct l2tp_tunnel *tunnel = v;
1504 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1506 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1507 refcount_read(&tunnel->ref_count) - 1);
1508 seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1510 atomic_long_read(&tunnel->stats.tx_packets),
1511 atomic_long_read(&tunnel->stats.tx_bytes),
1512 atomic_long_read(&tunnel->stats.tx_errors),
1513 atomic_long_read(&tunnel->stats.rx_packets),
1514 atomic_long_read(&tunnel->stats.rx_bytes),
1515 atomic_long_read(&tunnel->stats.rx_errors));
1518 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1520 struct l2tp_session *session = v;
1521 struct l2tp_tunnel *tunnel = session->tunnel;
1522 unsigned char state;
1529 struct inet_sock *inet = inet_sk(tunnel->sock);
1531 ip = ntohl(inet->inet_saddr);
1532 port = ntohs(inet->inet_sport);
1535 sk = pppol2tp_session_get_sock(session);
1537 state = sk->sk_state;
1538 user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1544 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> %04X/%04X %d %c\n",
1545 session->name, ip, port,
1547 session->session_id,
1548 tunnel->peer_tunnel_id,
1549 session->peer_session_id,
1550 state, user_data_ok);
1551 seq_printf(m, " 0/0/%c/%c/%s %08x %u\n",
1552 session->recv_seq ? 'R' : '-',
1553 session->send_seq ? 'S' : '-',
1554 session->lns_mode ? "LNS" : "LAC",
1556 jiffies_to_msecs(session->reorder_timeout));
1557 seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
1558 session->nr, session->ns,
1559 atomic_long_read(&session->stats.tx_packets),
1560 atomic_long_read(&session->stats.tx_bytes),
1561 atomic_long_read(&session->stats.tx_errors),
1562 atomic_long_read(&session->stats.rx_packets),
1563 atomic_long_read(&session->stats.rx_bytes),
1564 atomic_long_read(&session->stats.rx_errors));
1567 struct pppox_sock *po = pppox_sk(sk);
1569 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
1574 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1576 struct pppol2tp_seq_data *pd = v;
1578 /* display header on line 1 */
1579 if (v == SEQ_START_TOKEN) {
1580 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1581 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1582 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1583 seq_puts(m, " SESSION name, addr/port src-tid/sid dest-tid/sid state user-data-ok\n");
1584 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1585 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1590 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1592 pppol2tp_seq_session_show(m, pd->session);
1598 static const struct seq_operations pppol2tp_seq_ops = {
1599 .start = pppol2tp_seq_start,
1600 .next = pppol2tp_seq_next,
1601 .stop = pppol2tp_seq_stop,
1602 .show = pppol2tp_seq_show,
1604 #endif /* CONFIG_PROC_FS */
1606 /*****************************************************************************
1608 *****************************************************************************/
1610 static __net_init int pppol2tp_init_net(struct net *net)
1612 struct proc_dir_entry *pde;
1615 pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1616 &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
1626 static __net_exit void pppol2tp_exit_net(struct net *net)
1628 remove_proc_entry("pppol2tp", net->proc_net);
1631 static struct pernet_operations pppol2tp_net_ops = {
1632 .init = pppol2tp_init_net,
1633 .exit = pppol2tp_exit_net,
1634 .id = &pppol2tp_net_id,
1637 /*****************************************************************************
1639 *****************************************************************************/
1641 static const struct proto_ops pppol2tp_ops = {
1643 .owner = THIS_MODULE,
1644 .release = pppol2tp_release,
1645 .bind = sock_no_bind,
1646 .connect = pppol2tp_connect,
1647 .socketpair = sock_no_socketpair,
1648 .accept = sock_no_accept,
1649 .getname = pppol2tp_getname,
1650 .poll = datagram_poll,
1651 .listen = sock_no_listen,
1652 .shutdown = sock_no_shutdown,
1653 .setsockopt = pppol2tp_setsockopt,
1654 .getsockopt = pppol2tp_getsockopt,
1655 .sendmsg = pppol2tp_sendmsg,
1656 .recvmsg = pppol2tp_recvmsg,
1657 .mmap = sock_no_mmap,
1658 .ioctl = pppox_ioctl,
1659 #ifdef CONFIG_COMPAT
1660 .compat_ioctl = pppox_compat_ioctl,
1664 static const struct pppox_proto pppol2tp_proto = {
1665 .create = pppol2tp_create,
1666 .ioctl = pppol2tp_ioctl,
1667 .owner = THIS_MODULE,
1670 #ifdef CONFIG_L2TP_V3
1672 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1673 .session_create = pppol2tp_session_create,
1674 .session_delete = l2tp_session_delete,
1677 #endif /* CONFIG_L2TP_V3 */
1679 static int __init pppol2tp_init(void)
1683 err = register_pernet_device(&pppol2tp_net_ops);
1687 err = proto_register(&pppol2tp_sk_proto, 0);
1689 goto out_unregister_pppol2tp_pernet;
1691 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1693 goto out_unregister_pppol2tp_proto;
1695 #ifdef CONFIG_L2TP_V3
1696 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1698 goto out_unregister_pppox;
1701 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1706 #ifdef CONFIG_L2TP_V3
1707 out_unregister_pppox:
1708 unregister_pppox_proto(PX_PROTO_OL2TP);
1710 out_unregister_pppol2tp_proto:
1711 proto_unregister(&pppol2tp_sk_proto);
1712 out_unregister_pppol2tp_pernet:
1713 unregister_pernet_device(&pppol2tp_net_ops);
1717 static void __exit pppol2tp_exit(void)
1719 #ifdef CONFIG_L2TP_V3
1720 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1722 unregister_pppox_proto(PX_PROTO_OL2TP);
1723 proto_unregister(&pppol2tp_sk_proto);
1724 unregister_pernet_device(&pppol2tp_net_ops);
1727 module_init(pppol2tp_init);
1728 module_exit(pppol2tp_exit);
1730 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1731 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1732 MODULE_LICENSE("GPL");
1733 MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1734 MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1735 MODULE_ALIAS_L2TP_PWTYPE(7);