1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 /* This driver handles only L2TP data frames; control frames are handled by a
22 * userspace application.
24 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
25 * attaches it to a bound UDP socket with local tunnel_id / session_id and
26 * peer tunnel_id / session_id set. Data can then be sent or received using
27 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
28 * can be read or modified using ioctl() or [gs]etsockopt() calls.
30 * When a PPPoL2TP socket is connected with local and peer session_id values
31 * zero, the socket is treated as a special tunnel management socket.
33 * Here's example userspace code to create a socket for sending/receiving data
34 * over an L2TP session:-
36 * struct sockaddr_pppol2tp sax;
40 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
42 * sax.sa_family = AF_PPPOX;
43 * sax.sa_protocol = PX_PROTO_OL2TP;
44 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
45 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
46 * sax.pppol2tp.addr.sin_port = addr->sin_port;
47 * sax.pppol2tp.addr.sin_family = AF_INET;
48 * sax.pppol2tp.s_tunnel = tunnel_id;
49 * sax.pppol2tp.s_session = session_id;
50 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
51 * sax.pppol2tp.d_session = peer_session_id;
53 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
55 * A pppd plugin that allows PPP traffic to be carried over L2TP using
56 * this driver is available from the OpenL2TP project at
57 * http://openl2tp.sourceforge.net.
60 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
62 #include <linux/module.h>
63 #include <linux/string.h>
64 #include <linux/list.h>
65 #include <linux/uaccess.h>
67 #include <linux/kernel.h>
68 #include <linux/spinlock.h>
69 #include <linux/kthread.h>
70 #include <linux/sched.h>
71 #include <linux/slab.h>
72 #include <linux/errno.h>
73 #include <linux/jiffies.h>
75 #include <linux/netdevice.h>
76 #include <linux/net.h>
77 #include <linux/inetdevice.h>
78 #include <linux/skbuff.h>
79 #include <linux/init.h>
81 #include <linux/udp.h>
82 #include <linux/if_pppox.h>
83 #include <linux/if_pppol2tp.h>
85 #include <linux/ppp_channel.h>
86 #include <linux/ppp_defs.h>
87 #include <linux/ppp-ioctl.h>
88 #include <linux/file.h>
89 #include <linux/hash.h>
90 #include <linux/sort.h>
91 #include <linux/proc_fs.h>
92 #include <linux/l2tp.h>
93 #include <linux/nsproxy.h>
94 #include <net/net_namespace.h>
95 #include <net/netns/generic.h>
98 #include <net/inet_common.h>
100 #include <asm/byteorder.h>
101 #include <linux/atomic.h>
103 #include "l2tp_core.h"
105 #define PPPOL2TP_DRV_VERSION "V2.0"
107 /* Space for UDP, L2TP and PPP headers */
108 #define PPPOL2TP_HEADER_OVERHEAD 40
110 /* Number of bytes to build transmit L2TP headers.
111 * Unfortunately the size is different depending on whether sequence numbers
114 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
115 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
117 /* Private data of each session. This data lives at the end of struct
118 * l2tp_session, referenced via session->priv[].
120 struct pppol2tp_session {
121 int owner; /* pid that opened the socket */
123 struct mutex sk_lock; /* Protects .sk */
124 struct sock __rcu *sk; /* Pointer to the session
126 struct sock *__sk; /* Copy of .sk, for cleanup */
127 struct rcu_head rcu; /* For asynchronous release */
130 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
132 static const struct ppp_channel_ops pppol2tp_chan_ops = {
133 .start_xmit = pppol2tp_xmit,
136 static const struct proto_ops pppol2tp_ops;
138 /* Retrieves the pppol2tp socket associated to a session.
139 * A reference is held on the returned socket, so this function must be paired
142 static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
144 struct pppol2tp_session *ps = l2tp_session_priv(session);
148 sk = rcu_dereference(ps->sk);
156 /* Helpers to obtain tunnel/session contexts from sockets.
158 static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
160 struct l2tp_session *session;
166 session = (struct l2tp_session *)(sk->sk_user_data);
167 if (session == NULL) {
172 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
178 /*****************************************************************************
179 * Receive data handling
180 *****************************************************************************/
182 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
184 static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
185 size_t len, int flags)
189 struct sock *sk = sock->sk;
192 if (sk->sk_state & PPPOX_BOUND)
196 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
197 flags & MSG_DONTWAIT, &err);
203 else if (len < skb->len)
204 msg->msg_flags |= MSG_TRUNC;
206 err = skb_copy_datagram_msg(skb, 0, msg, len);
207 if (likely(err == 0))
215 static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
217 struct pppol2tp_session *ps = l2tp_session_priv(session);
218 struct sock *sk = NULL;
220 /* If the socket is bound, send it in to PPP's input queue. Otherwise
221 * queue it on the session socket.
224 sk = rcu_dereference(ps->sk);
228 /* If the first two bytes are 0xFF03, consider that it is the PPP's
229 * Address and Control fields and skip them. The L2TP module has always
230 * worked this way, although, in theory, the use of these fields should
231 * be negociated and handled at the PPP layer. These fields are
232 * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
233 * Information command with Poll/Final bit set to zero (RFC 1662).
235 if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
236 skb->data[1] == PPP_UI)
239 if (sk->sk_state & PPPOX_BOUND) {
240 struct pppox_sock *po;
242 l2tp_dbg(session, L2TP_MSG_DATA,
243 "%s: recv %d byte data frame, passing to ppp\n",
244 session->name, data_len);
247 ppp_input(&po->chan, skb);
249 l2tp_dbg(session, L2TP_MSG_DATA,
250 "%s: recv %d byte data frame, passing to L2TP socket\n",
251 session->name, data_len);
253 if (sock_queue_rcv_skb(sk, skb) < 0) {
254 atomic_long_inc(&session->stats.rx_errors);
264 l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
268 /************************************************************************
270 ***********************************************************************/
272 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
273 * when a user application does a sendmsg() on the session socket. L2TP and
274 * PPP headers must be inserted into the user's data.
276 static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
279 struct sock *sk = sock->sk;
282 struct l2tp_session *session;
283 struct l2tp_tunnel *tunnel;
287 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
290 /* Get session and tunnel contexts */
292 session = pppol2tp_sock_to_session(sk);
296 tunnel = session->tunnel;
298 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
300 /* Allocate a socket buffer */
302 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
303 uhlen + session->hdr_len +
304 2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
309 /* Reserve space for headers. */
310 skb_reserve(skb, NET_SKB_PAD);
311 skb_reset_network_header(skb);
312 skb_reserve(skb, sizeof(struct iphdr));
313 skb_reset_transport_header(skb);
314 skb_reserve(skb, uhlen);
317 skb->data[0] = PPP_ALLSTATIONS;
318 skb->data[1] = PPP_UI;
321 /* Copy user data into skb */
322 error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
329 l2tp_xmit_skb(session, skb, session->hdr_len);
342 /* Transmit function called by generic PPP driver. Sends PPP frame
343 * over PPPoL2TP socket.
345 * This is almost the same as pppol2tp_sendmsg(), but rather than
346 * being called with a msghdr from userspace, it is called with a skb
349 * The supplied skb from ppp doesn't have enough headroom for the
350 * insertion of L2TP, UDP and IP headers so we need to allocate more
351 * headroom in the skb. This will create a cloned skb. But we must be
352 * careful in the error case because the caller will expect to free
353 * the skb it supplied, not our cloned skb. So we take care to always
354 * leave the original skb unfreed if we return an error.
356 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
358 struct sock *sk = (struct sock *) chan->private;
359 struct l2tp_session *session;
360 struct l2tp_tunnel *tunnel;
363 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
366 /* Get session and tunnel contexts from the socket */
367 session = pppol2tp_sock_to_session(sk);
371 tunnel = session->tunnel;
373 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
374 headroom = NET_SKB_PAD +
375 sizeof(struct iphdr) + /* IP header */
376 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
377 session->hdr_len + /* L2TP header */
378 2; /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
379 if (skb_cow_head(skb, headroom))
382 /* Setup PPP header */
384 skb->data[0] = PPP_ALLSTATIONS;
385 skb->data[1] = PPP_UI;
388 l2tp_xmit_skb(session, skb, session->hdr_len);
398 /* Free the original skb */
403 /*****************************************************************************
404 * Session (and tunnel control) socket create/destroy.
405 *****************************************************************************/
407 static void pppol2tp_put_sk(struct rcu_head *head)
409 struct pppol2tp_session *ps;
411 ps = container_of(head, typeof(*ps), rcu);
415 /* Really kill the session socket. (Called from sock_put() if
418 static void pppol2tp_session_destruct(struct sock *sk)
420 struct l2tp_session *session = sk->sk_user_data;
422 skb_queue_purge(&sk->sk_receive_queue);
423 skb_queue_purge(&sk->sk_write_queue);
426 sk->sk_user_data = NULL;
427 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
428 l2tp_session_dec_refcount(session);
432 /* Called when the PPPoX socket (session) is closed.
434 static int pppol2tp_release(struct socket *sock)
436 struct sock *sk = sock->sk;
437 struct l2tp_session *session;
445 if (sock_flag(sk, SOCK_DEAD) != 0)
448 pppox_unbind_sock(sk);
450 /* Signal the death of the socket. */
451 sk->sk_state = PPPOX_DEAD;
455 session = pppol2tp_sock_to_session(sk);
457 struct pppol2tp_session *ps;
459 l2tp_session_delete(session);
461 ps = l2tp_session_priv(session);
462 mutex_lock(&ps->sk_lock);
463 ps->__sk = rcu_dereference_protected(ps->sk,
464 lockdep_is_held(&ps->sk_lock));
465 RCU_INIT_POINTER(ps->sk, NULL);
466 mutex_unlock(&ps->sk_lock);
467 call_rcu(&ps->rcu, pppol2tp_put_sk);
469 /* Rely on the sock_put() call at the end of the function for
470 * dropping the reference held by pppol2tp_sock_to_session().
471 * The last reference will be dropped by pppol2tp_put_sk().
477 /* This will delete the session context via
478 * pppol2tp_session_destruct() if the socket's refcnt drops to
490 static struct proto pppol2tp_sk_proto = {
492 .owner = THIS_MODULE,
493 .obj_size = sizeof(struct pppox_sock),
496 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
500 rc = l2tp_udp_encap_recv(sk, skb);
504 return NET_RX_SUCCESS;
507 /* socket() handler. Initialize a new struct sock.
509 static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
514 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
518 sock_init_data(sock, sk);
520 sock->state = SS_UNCONNECTED;
521 sock->ops = &pppol2tp_ops;
523 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
524 sk->sk_protocol = PX_PROTO_OL2TP;
525 sk->sk_family = PF_PPPOX;
526 sk->sk_state = PPPOX_NONE;
527 sk->sk_type = SOCK_STREAM;
528 sk->sk_destruct = pppol2tp_session_destruct;
536 static void pppol2tp_show(struct seq_file *m, void *arg)
538 struct l2tp_session *session = arg;
541 sk = pppol2tp_session_get_sock(session);
543 struct pppox_sock *po = pppox_sk(sk);
545 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
550 static void pppol2tp_session_init(struct l2tp_session *session)
552 struct pppol2tp_session *ps;
554 session->recv_skb = pppol2tp_recv;
555 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
556 session->show = pppol2tp_show;
558 ps = l2tp_session_priv(session);
559 mutex_init(&ps->sk_lock);
560 ps->owner = current->pid;
563 struct l2tp_connect_info {
572 static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
573 struct l2tp_connect_info *info)
576 case sizeof(struct sockaddr_pppol2tp):
578 const struct sockaddr_pppol2tp *sa_v2in4 = sa;
580 if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
584 info->fd = sa_v2in4->pppol2tp.fd;
585 info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
586 info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
587 info->session_id = sa_v2in4->pppol2tp.s_session;
588 info->peer_session_id = sa_v2in4->pppol2tp.d_session;
592 case sizeof(struct sockaddr_pppol2tpv3):
594 const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
596 if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
600 info->fd = sa_v3in4->pppol2tp.fd;
601 info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
602 info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
603 info->session_id = sa_v3in4->pppol2tp.s_session;
604 info->peer_session_id = sa_v3in4->pppol2tp.d_session;
608 case sizeof(struct sockaddr_pppol2tpin6):
610 const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
612 if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
616 info->fd = sa_v2in6->pppol2tp.fd;
617 info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
618 info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
619 info->session_id = sa_v2in6->pppol2tp.s_session;
620 info->peer_session_id = sa_v2in6->pppol2tp.d_session;
624 case sizeof(struct sockaddr_pppol2tpv3in6):
626 const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
628 if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
632 info->fd = sa_v3in6->pppol2tp.fd;
633 info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
634 info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
635 info->session_id = sa_v3in6->pppol2tp.s_session;
636 info->peer_session_id = sa_v3in6->pppol2tp.d_session;
647 /* Rough estimation of the maximum payload size a tunnel can transmit without
648 * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
649 * numbers and no IP option. Not quite accurate, but the result is mostly
652 static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
656 mtu = l2tp_tunnel_dst_mtu(tunnel);
657 if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
658 return 1500 - PPPOL2TP_HEADER_OVERHEAD;
660 return mtu - PPPOL2TP_HEADER_OVERHEAD;
663 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
665 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
666 int sockaddr_len, int flags)
668 struct sock *sk = sock->sk;
669 struct pppox_sock *po = pppox_sk(sk);
670 struct l2tp_session *session = NULL;
671 struct l2tp_connect_info info;
672 struct l2tp_tunnel *tunnel;
673 struct pppol2tp_session *ps;
674 struct l2tp_session_cfg cfg = { 0, };
675 bool drop_refcnt = false;
676 bool drop_tunnel = false;
677 bool new_session = false;
678 bool new_tunnel = false;
681 error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
687 /* Check for already bound sockets */
689 if (sk->sk_state & PPPOX_CONNECTED)
692 /* We don't supporting rebinding anyway */
694 if (sk->sk_user_data)
695 goto end; /* socket is already attached */
697 /* Don't bind if tunnel_id is 0 */
702 tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
706 /* Special case: create tunnel context if session_id and
707 * peer_session_id is 0. Otherwise look up tunnel using supplied
710 if (!info.session_id && !info.peer_session_id) {
711 if (tunnel == NULL) {
712 struct l2tp_tunnel_cfg tcfg = {
713 .encap = L2TP_ENCAPTYPE_UDP,
717 /* Prevent l2tp_tunnel_register() from trying to set up
725 error = l2tp_tunnel_create(sock_net(sk), info.fd,
728 info.peer_tunnel_id, &tcfg,
733 l2tp_tunnel_inc_refcount(tunnel);
734 error = l2tp_tunnel_register(tunnel, sock_net(sk),
744 /* Error if we can't find the tunnel */
749 /* Error if socket is not prepped */
750 if (tunnel->sock == NULL)
754 if (tunnel->peer_tunnel_id == 0)
755 tunnel->peer_tunnel_id = info.peer_tunnel_id;
757 session = l2tp_tunnel_get_session(tunnel, info.session_id);
761 if (session->pwtype != L2TP_PWTYPE_PPP) {
766 ps = l2tp_session_priv(session);
768 /* Using a pre-existing session is fine as long as it hasn't
769 * been connected yet.
771 mutex_lock(&ps->sk_lock);
772 if (rcu_dereference_protected(ps->sk,
773 lockdep_is_held(&ps->sk_lock)) ||
775 mutex_unlock(&ps->sk_lock);
780 cfg.pw_type = L2TP_PWTYPE_PPP;
782 session = l2tp_session_create(sizeof(struct pppol2tp_session),
783 tunnel, info.session_id,
784 info.peer_session_id, &cfg);
785 if (IS_ERR(session)) {
786 error = PTR_ERR(session);
790 pppol2tp_session_init(session);
791 ps = l2tp_session_priv(session);
792 l2tp_session_inc_refcount(session);
794 mutex_lock(&ps->sk_lock);
795 error = l2tp_session_register(session, tunnel);
797 mutex_unlock(&ps->sk_lock);
805 /* Special case: if source & dest session_id == 0x0000, this
806 * socket is being created to manage the tunnel. Just set up
807 * the internal context for use by ioctl() and sockopt()
810 if ((session->session_id == 0) &&
811 (session->peer_session_id == 0)) {
816 /* The only header we need to worry about is the L2TP
817 * header. This size is different depending on whether
818 * sequence numbers are enabled for the data channel.
820 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
822 po->chan.private = sk;
823 po->chan.ops = &pppol2tp_chan_ops;
824 po->chan.mtu = pppol2tp_tunnel_mtu(tunnel);
826 error = ppp_register_net_channel(sock_net(sk), &po->chan);
828 mutex_unlock(&ps->sk_lock);
833 /* This is how we get the session context from the socket. */
834 sk->sk_user_data = session;
835 rcu_assign_pointer(ps->sk, sk);
836 mutex_unlock(&ps->sk_lock);
838 /* Keep the reference we've grabbed on the session: sk doesn't expect
839 * the session to disappear. pppol2tp_session_destruct() is responsible
844 sk->sk_state = PPPOX_CONNECTED;
845 l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
851 l2tp_session_delete(session);
853 l2tp_tunnel_delete(tunnel);
856 l2tp_session_dec_refcount(session);
858 l2tp_tunnel_dec_refcount(tunnel);
864 #ifdef CONFIG_L2TP_V3
866 /* Called when creating sessions via the netlink interface. */
867 static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
868 u32 session_id, u32 peer_session_id,
869 struct l2tp_session_cfg *cfg)
872 struct l2tp_session *session;
874 /* Error if tunnel socket is not prepped */
880 /* Allocate and initialize a new session context. */
881 session = l2tp_session_create(sizeof(struct pppol2tp_session),
883 peer_session_id, cfg);
884 if (IS_ERR(session)) {
885 error = PTR_ERR(session);
889 pppol2tp_session_init(session);
891 error = l2tp_session_register(session, tunnel);
903 #endif /* CONFIG_L2TP_V3 */
905 /* getname() support.
907 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
912 struct l2tp_session *session;
913 struct l2tp_tunnel *tunnel;
914 struct sock *sk = sock->sk;
915 struct inet_sock *inet;
916 struct pppol2tp_session *pls;
921 if (!(sk->sk_state & PPPOX_CONNECTED))
925 session = pppol2tp_sock_to_session(sk);
929 pls = l2tp_session_priv(session);
930 tunnel = session->tunnel;
932 inet = inet_sk(tunnel->sock);
933 if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
934 struct sockaddr_pppol2tp sp;
937 sp.sa_family = AF_PPPOX;
938 sp.sa_protocol = PX_PROTO_OL2TP;
939 sp.pppol2tp.fd = tunnel->fd;
940 sp.pppol2tp.pid = pls->owner;
941 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
942 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
943 sp.pppol2tp.s_session = session->session_id;
944 sp.pppol2tp.d_session = session->peer_session_id;
945 sp.pppol2tp.addr.sin_family = AF_INET;
946 sp.pppol2tp.addr.sin_port = inet->inet_dport;
947 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
948 memcpy(uaddr, &sp, len);
949 #if IS_ENABLED(CONFIG_IPV6)
950 } else if ((tunnel->version == 2) &&
951 (tunnel->sock->sk_family == AF_INET6)) {
952 struct sockaddr_pppol2tpin6 sp;
956 sp.sa_family = AF_PPPOX;
957 sp.sa_protocol = PX_PROTO_OL2TP;
958 sp.pppol2tp.fd = tunnel->fd;
959 sp.pppol2tp.pid = pls->owner;
960 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
961 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
962 sp.pppol2tp.s_session = session->session_id;
963 sp.pppol2tp.d_session = session->peer_session_id;
964 sp.pppol2tp.addr.sin6_family = AF_INET6;
965 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
966 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
967 sizeof(tunnel->sock->sk_v6_daddr));
968 memcpy(uaddr, &sp, len);
969 } else if ((tunnel->version == 3) &&
970 (tunnel->sock->sk_family == AF_INET6)) {
971 struct sockaddr_pppol2tpv3in6 sp;
975 sp.sa_family = AF_PPPOX;
976 sp.sa_protocol = PX_PROTO_OL2TP;
977 sp.pppol2tp.fd = tunnel->fd;
978 sp.pppol2tp.pid = pls->owner;
979 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
980 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
981 sp.pppol2tp.s_session = session->session_id;
982 sp.pppol2tp.d_session = session->peer_session_id;
983 sp.pppol2tp.addr.sin6_family = AF_INET6;
984 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
985 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
986 sizeof(tunnel->sock->sk_v6_daddr));
987 memcpy(uaddr, &sp, len);
989 } else if (tunnel->version == 3) {
990 struct sockaddr_pppol2tpv3 sp;
993 sp.sa_family = AF_PPPOX;
994 sp.sa_protocol = PX_PROTO_OL2TP;
995 sp.pppol2tp.fd = tunnel->fd;
996 sp.pppol2tp.pid = pls->owner;
997 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
998 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
999 sp.pppol2tp.s_session = session->session_id;
1000 sp.pppol2tp.d_session = session->peer_session_id;
1001 sp.pppol2tp.addr.sin_family = AF_INET;
1002 sp.pppol2tp.addr.sin_port = inet->inet_dport;
1003 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
1004 memcpy(uaddr, &sp, len);
1014 /****************************************************************************
1017 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1018 * sockets. However, in order to control kernel tunnel features, we allow
1019 * userspace to create a special "tunnel" PPPoX socket which is used for
1020 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1021 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1023 ****************************************************************************/
1025 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1026 const struct l2tp_stats *stats)
1028 memset(dest, 0, sizeof(*dest));
1030 dest->tx_packets = atomic_long_read(&stats->tx_packets);
1031 dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1032 dest->tx_errors = atomic_long_read(&stats->tx_errors);
1033 dest->rx_packets = atomic_long_read(&stats->rx_packets);
1034 dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1035 dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1036 dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1037 dest->rx_errors = atomic_long_read(&stats->rx_errors);
1040 static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
1041 struct l2tp_tunnel *tunnel)
1043 struct l2tp_session *session;
1045 if (!stats->session_id) {
1046 pppol2tp_copy_stats(stats, &tunnel->stats);
1050 /* If session_id is set, search the corresponding session in the
1051 * context of this tunnel and record the session's statistics.
1053 session = l2tp_tunnel_get_session(tunnel, stats->session_id);
1057 if (session->pwtype != L2TP_PWTYPE_PPP) {
1058 l2tp_session_dec_refcount(session);
1062 pppol2tp_copy_stats(stats, &session->stats);
1063 l2tp_session_dec_refcount(session);
1068 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1071 struct pppol2tp_ioc_stats stats;
1072 struct l2tp_session *session;
1077 session = sock->sk->sk_user_data;
1081 /* Not defined for tunnels */
1082 if (!session->session_id && !session->peer_session_id)
1085 if (put_user(0, (int __user *)arg))
1091 session = sock->sk->sk_user_data;
1095 /* Not defined for tunnels */
1096 if (!session->session_id && !session->peer_session_id)
1099 if (!access_ok((int __user *)arg, sizeof(int)))
1103 case PPPIOCGL2TPSTATS:
1104 session = sock->sk->sk_user_data;
1108 /* Session 0 represents the parent tunnel */
1109 if (!session->session_id && !session->peer_session_id) {
1113 if (copy_from_user(&stats, (void __user *)arg,
1117 session_id = stats.session_id;
1118 err = pppol2tp_tunnel_copy_stats(&stats,
1123 stats.session_id = session_id;
1125 pppol2tp_copy_stats(&stats, &session->stats);
1126 stats.session_id = session->session_id;
1128 stats.tunnel_id = session->tunnel->tunnel_id;
1129 stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
1131 if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
1136 return -ENOIOCTLCMD;
1142 /*****************************************************************************
1143 * setsockopt() / getsockopt() support.
1145 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1146 * sockets. In order to control kernel tunnel features, we allow userspace to
1147 * create a special "tunnel" PPPoX socket which is used for control only.
1148 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1149 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1150 *****************************************************************************/
1152 /* Tunnel setsockopt() helper.
1154 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1155 struct l2tp_tunnel *tunnel,
1156 int optname, int val)
1161 case PPPOL2TP_SO_DEBUG:
1162 tunnel->debug = val;
1163 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1164 tunnel->name, tunnel->debug);
1175 /* Session setsockopt helper.
1177 static int pppol2tp_session_setsockopt(struct sock *sk,
1178 struct l2tp_session *session,
1179 int optname, int val)
1184 case PPPOL2TP_SO_RECVSEQ:
1185 if ((val != 0) && (val != 1)) {
1189 session->recv_seq = !!val;
1190 l2tp_info(session, L2TP_MSG_CONTROL,
1191 "%s: set recv_seq=%d\n",
1192 session->name, session->recv_seq);
1195 case PPPOL2TP_SO_SENDSEQ:
1196 if ((val != 0) && (val != 1)) {
1200 session->send_seq = !!val;
1202 struct pppox_sock *po = pppox_sk(sk);
1204 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1205 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1207 l2tp_session_set_header_len(session, session->tunnel->version);
1208 l2tp_info(session, L2TP_MSG_CONTROL,
1209 "%s: set send_seq=%d\n",
1210 session->name, session->send_seq);
1213 case PPPOL2TP_SO_LNSMODE:
1214 if ((val != 0) && (val != 1)) {
1218 session->lns_mode = !!val;
1219 l2tp_info(session, L2TP_MSG_CONTROL,
1220 "%s: set lns_mode=%d\n",
1221 session->name, session->lns_mode);
1224 case PPPOL2TP_SO_DEBUG:
1225 session->debug = val;
1226 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
1227 session->name, session->debug);
1230 case PPPOL2TP_SO_REORDERTO:
1231 session->reorder_timeout = msecs_to_jiffies(val);
1232 l2tp_info(session, L2TP_MSG_CONTROL,
1233 "%s: set reorder_timeout=%d\n",
1234 session->name, session->reorder_timeout);
1245 /* Main setsockopt() entry point.
1246 * Does API checks, then calls either the tunnel or session setsockopt
1247 * handler, according to whether the PPPoL2TP socket is a for a regular
1248 * session or the special tunnel type.
1250 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1251 char __user *optval, unsigned int optlen)
1253 struct sock *sk = sock->sk;
1254 struct l2tp_session *session;
1255 struct l2tp_tunnel *tunnel;
1259 if (level != SOL_PPPOL2TP)
1262 if (optlen < sizeof(int))
1265 if (get_user(val, (int __user *)optval))
1269 if (sk->sk_user_data == NULL)
1272 /* Get session context from the socket */
1274 session = pppol2tp_sock_to_session(sk);
1275 if (session == NULL)
1278 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1280 if ((session->session_id == 0) &&
1281 (session->peer_session_id == 0)) {
1282 tunnel = session->tunnel;
1283 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1285 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1293 /* Tunnel getsockopt helper. Called with sock locked.
1295 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1296 struct l2tp_tunnel *tunnel,
1297 int optname, int *val)
1302 case PPPOL2TP_SO_DEBUG:
1303 *val = tunnel->debug;
1304 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
1305 tunnel->name, tunnel->debug);
1316 /* Session getsockopt helper. Called with sock locked.
1318 static int pppol2tp_session_getsockopt(struct sock *sk,
1319 struct l2tp_session *session,
1320 int optname, int *val)
1325 case PPPOL2TP_SO_RECVSEQ:
1326 *val = session->recv_seq;
1327 l2tp_info(session, L2TP_MSG_CONTROL,
1328 "%s: get recv_seq=%d\n", session->name, *val);
1331 case PPPOL2TP_SO_SENDSEQ:
1332 *val = session->send_seq;
1333 l2tp_info(session, L2TP_MSG_CONTROL,
1334 "%s: get send_seq=%d\n", session->name, *val);
1337 case PPPOL2TP_SO_LNSMODE:
1338 *val = session->lns_mode;
1339 l2tp_info(session, L2TP_MSG_CONTROL,
1340 "%s: get lns_mode=%d\n", session->name, *val);
1343 case PPPOL2TP_SO_DEBUG:
1344 *val = session->debug;
1345 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
1346 session->name, *val);
1349 case PPPOL2TP_SO_REORDERTO:
1350 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1351 l2tp_info(session, L2TP_MSG_CONTROL,
1352 "%s: get reorder_timeout=%d\n", session->name, *val);
1362 /* Main getsockopt() entry point.
1363 * Does API checks, then calls either the tunnel or session getsockopt
1364 * handler, according to whether the PPPoX socket is a for a regular session
1365 * or the special tunnel type.
1367 static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1368 char __user *optval, int __user *optlen)
1370 struct sock *sk = sock->sk;
1371 struct l2tp_session *session;
1372 struct l2tp_tunnel *tunnel;
1376 if (level != SOL_PPPOL2TP)
1379 if (get_user(len, optlen))
1382 len = min_t(unsigned int, len, sizeof(int));
1388 if (sk->sk_user_data == NULL)
1391 /* Get the session context */
1393 session = pppol2tp_sock_to_session(sk);
1394 if (session == NULL)
1397 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1398 if ((session->session_id == 0) &&
1399 (session->peer_session_id == 0)) {
1400 tunnel = session->tunnel;
1401 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1405 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1411 if (put_user(len, optlen))
1414 if (copy_to_user((void __user *) optval, &val, len))
1425 /*****************************************************************************
1426 * /proc filesystem for debug
1427 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1428 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1429 *****************************************************************************/
1431 static unsigned int pppol2tp_net_id;
1433 #ifdef CONFIG_PROC_FS
1435 struct pppol2tp_seq_data {
1436 struct seq_net_private p;
1437 int tunnel_idx; /* current tunnel */
1438 int session_idx; /* index of session within current tunnel */
1439 struct l2tp_tunnel *tunnel;
1440 struct l2tp_session *session; /* NULL means get next tunnel */
1443 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1445 /* Drop reference taken during previous invocation */
1447 l2tp_tunnel_dec_refcount(pd->tunnel);
1450 pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
1453 /* Only accept L2TPv2 tunnels */
1454 if (!pd->tunnel || pd->tunnel->version == 2)
1457 l2tp_tunnel_dec_refcount(pd->tunnel);
1461 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1463 /* Drop reference taken during previous invocation */
1465 l2tp_session_dec_refcount(pd->session);
1467 pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
1470 if (pd->session == NULL) {
1471 pd->session_idx = 0;
1472 pppol2tp_next_tunnel(net, pd);
1476 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1478 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1485 BUG_ON(m->private == NULL);
1487 net = seq_file_net(m);
1489 if (pd->tunnel == NULL)
1490 pppol2tp_next_tunnel(net, pd);
1492 pppol2tp_next_session(net, pd);
1494 /* NULL tunnel and session indicates end of list */
1495 if ((pd->tunnel == NULL) && (pd->session == NULL))
1502 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1508 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1510 struct pppol2tp_seq_data *pd = v;
1512 if (!pd || pd == SEQ_START_TOKEN)
1515 /* Drop reference taken by last invocation of pppol2tp_next_session()
1516 * or pppol2tp_next_tunnel().
1519 l2tp_session_dec_refcount(pd->session);
1523 l2tp_tunnel_dec_refcount(pd->tunnel);
1528 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1530 struct l2tp_tunnel *tunnel = v;
1532 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1534 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1535 refcount_read(&tunnel->ref_count) - 1);
1536 seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1538 atomic_long_read(&tunnel->stats.tx_packets),
1539 atomic_long_read(&tunnel->stats.tx_bytes),
1540 atomic_long_read(&tunnel->stats.tx_errors),
1541 atomic_long_read(&tunnel->stats.rx_packets),
1542 atomic_long_read(&tunnel->stats.rx_bytes),
1543 atomic_long_read(&tunnel->stats.rx_errors));
1546 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1548 struct l2tp_session *session = v;
1549 struct l2tp_tunnel *tunnel = session->tunnel;
1550 unsigned char state;
1557 struct inet_sock *inet = inet_sk(tunnel->sock);
1558 ip = ntohl(inet->inet_saddr);
1559 port = ntohs(inet->inet_sport);
1562 sk = pppol2tp_session_get_sock(session);
1564 state = sk->sk_state;
1565 user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1571 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1572 "%04X/%04X %d %c\n",
1573 session->name, ip, port,
1575 session->session_id,
1576 tunnel->peer_tunnel_id,
1577 session->peer_session_id,
1578 state, user_data_ok);
1579 seq_printf(m, " 0/0/%c/%c/%s %08x %u\n",
1580 session->recv_seq ? 'R' : '-',
1581 session->send_seq ? 'S' : '-',
1582 session->lns_mode ? "LNS" : "LAC",
1584 jiffies_to_msecs(session->reorder_timeout));
1585 seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
1586 session->nr, session->ns,
1587 atomic_long_read(&session->stats.tx_packets),
1588 atomic_long_read(&session->stats.tx_bytes),
1589 atomic_long_read(&session->stats.tx_errors),
1590 atomic_long_read(&session->stats.rx_packets),
1591 atomic_long_read(&session->stats.rx_bytes),
1592 atomic_long_read(&session->stats.rx_errors));
1595 struct pppox_sock *po = pppox_sk(sk);
1597 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
1602 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1604 struct pppol2tp_seq_data *pd = v;
1606 /* display header on line 1 */
1607 if (v == SEQ_START_TOKEN) {
1608 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1609 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1610 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1611 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1612 "dest-tid/sid state user-data-ok\n");
1613 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1614 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1619 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1621 pppol2tp_seq_session_show(m, pd->session);
1627 static const struct seq_operations pppol2tp_seq_ops = {
1628 .start = pppol2tp_seq_start,
1629 .next = pppol2tp_seq_next,
1630 .stop = pppol2tp_seq_stop,
1631 .show = pppol2tp_seq_show,
1633 #endif /* CONFIG_PROC_FS */
1635 /*****************************************************************************
1637 *****************************************************************************/
1639 static __net_init int pppol2tp_init_net(struct net *net)
1641 struct proc_dir_entry *pde;
1644 pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1645 &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
1655 static __net_exit void pppol2tp_exit_net(struct net *net)
1657 remove_proc_entry("pppol2tp", net->proc_net);
1660 static struct pernet_operations pppol2tp_net_ops = {
1661 .init = pppol2tp_init_net,
1662 .exit = pppol2tp_exit_net,
1663 .id = &pppol2tp_net_id,
1666 /*****************************************************************************
1668 *****************************************************************************/
1670 static const struct proto_ops pppol2tp_ops = {
1672 .owner = THIS_MODULE,
1673 .release = pppol2tp_release,
1674 .bind = sock_no_bind,
1675 .connect = pppol2tp_connect,
1676 .socketpair = sock_no_socketpair,
1677 .accept = sock_no_accept,
1678 .getname = pppol2tp_getname,
1679 .poll = datagram_poll,
1680 .listen = sock_no_listen,
1681 .shutdown = sock_no_shutdown,
1682 .setsockopt = pppol2tp_setsockopt,
1683 .getsockopt = pppol2tp_getsockopt,
1684 .sendmsg = pppol2tp_sendmsg,
1685 .recvmsg = pppol2tp_recvmsg,
1686 .mmap = sock_no_mmap,
1687 .ioctl = pppox_ioctl,
1690 static const struct pppox_proto pppol2tp_proto = {
1691 .create = pppol2tp_create,
1692 .ioctl = pppol2tp_ioctl,
1693 .owner = THIS_MODULE,
1696 #ifdef CONFIG_L2TP_V3
1698 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1699 .session_create = pppol2tp_session_create,
1700 .session_delete = l2tp_session_delete,
1703 #endif /* CONFIG_L2TP_V3 */
1705 static int __init pppol2tp_init(void)
1709 err = register_pernet_device(&pppol2tp_net_ops);
1713 err = proto_register(&pppol2tp_sk_proto, 0);
1715 goto out_unregister_pppol2tp_pernet;
1717 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1719 goto out_unregister_pppol2tp_proto;
1721 #ifdef CONFIG_L2TP_V3
1722 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1724 goto out_unregister_pppox;
1727 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1732 #ifdef CONFIG_L2TP_V3
1733 out_unregister_pppox:
1734 unregister_pppox_proto(PX_PROTO_OL2TP);
1736 out_unregister_pppol2tp_proto:
1737 proto_unregister(&pppol2tp_sk_proto);
1738 out_unregister_pppol2tp_pernet:
1739 unregister_pernet_device(&pppol2tp_net_ops);
1743 static void __exit pppol2tp_exit(void)
1745 #ifdef CONFIG_L2TP_V3
1746 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1748 unregister_pppox_proto(PX_PROTO_OL2TP);
1749 proto_unregister(&pppol2tp_sk_proto);
1750 unregister_pernet_device(&pppol2tp_net_ops);
1753 module_init(pppol2tp_init);
1754 module_exit(pppol2tp_exit);
1757 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1758 MODULE_LICENSE("GPL");
1759 MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1760 MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1761 MODULE_ALIAS_L2TP_PWTYPE(7);