Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*****************************************************************************
0003  * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
0004  *
0005  * PPPoX    --- Generic PPP encapsulation socket family
0006  * PPPoL2TP --- PPP over L2TP (RFC 2661)
0007  *
0008  * Version: 2.0.0
0009  *
0010  * Authors: James Chapman (jchapman@katalix.com)
0011  *
0012  * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
0013  *
0014  * License:
0015  */
0016 
0017 /* This driver handles only L2TP data frames; control frames are handled by a
0018  * userspace application.
0019  *
0020  * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
0021  * attaches it to a bound UDP socket with local tunnel_id / session_id and
0022  * peer tunnel_id / session_id set. Data can then be sent or received using
0023  * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
0024  * can be read or modified using ioctl() or [gs]etsockopt() calls.
0025  *
0026  * When a PPPoL2TP socket is connected with local and peer session_id values
0027  * zero, the socket is treated as a special tunnel management socket.
0028  *
0029  * Here's example userspace code to create a socket for sending/receiving data
0030  * over an L2TP session:-
0031  *
0032  *  struct sockaddr_pppol2tp sax;
0033  *  int fd;
0034  *  int session_fd;
0035  *
0036  *  fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
0037  *
0038  *  sax.sa_family = AF_PPPOX;
0039  *  sax.sa_protocol = PX_PROTO_OL2TP;
0040  *  sax.pppol2tp.fd = tunnel_fd;    // bound UDP socket
0041  *  sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
0042  *  sax.pppol2tp.addr.sin_port = addr->sin_port;
0043  *  sax.pppol2tp.addr.sin_family = AF_INET;
0044  *  sax.pppol2tp.s_tunnel  = tunnel_id;
0045  *  sax.pppol2tp.s_session = session_id;
0046  *  sax.pppol2tp.d_tunnel  = peer_tunnel_id;
0047  *  sax.pppol2tp.d_session = peer_session_id;
0048  *
0049  *  session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
0050  *
0051  * A pppd plugin that allows PPP traffic to be carried over L2TP using
0052  * this driver is available from the OpenL2TP project at
0053  * http://openl2tp.sourceforge.net.
0054  */
0055 
0056 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0057 
0058 #include <linux/module.h>
0059 #include <linux/string.h>
0060 #include <linux/list.h>
0061 #include <linux/uaccess.h>
0062 
0063 #include <linux/kernel.h>
0064 #include <linux/spinlock.h>
0065 #include <linux/kthread.h>
0066 #include <linux/sched.h>
0067 #include <linux/slab.h>
0068 #include <linux/errno.h>
0069 #include <linux/jiffies.h>
0070 
0071 #include <linux/netdevice.h>
0072 #include <linux/net.h>
0073 #include <linux/inetdevice.h>
0074 #include <linux/skbuff.h>
0075 #include <linux/init.h>
0076 #include <linux/ip.h>
0077 #include <linux/udp.h>
0078 #include <linux/if_pppox.h>
0079 #include <linux/if_pppol2tp.h>
0080 #include <net/sock.h>
0081 #include <linux/ppp_channel.h>
0082 #include <linux/ppp_defs.h>
0083 #include <linux/ppp-ioctl.h>
0084 #include <linux/file.h>
0085 #include <linux/hash.h>
0086 #include <linux/sort.h>
0087 #include <linux/proc_fs.h>
0088 #include <linux/l2tp.h>
0089 #include <linux/nsproxy.h>
0090 #include <net/net_namespace.h>
0091 #include <net/netns/generic.h>
0092 #include <net/ip.h>
0093 #include <net/udp.h>
0094 #include <net/inet_common.h>
0095 
0096 #include <asm/byteorder.h>
0097 #include <linux/atomic.h>
0098 
0099 #include "l2tp_core.h"
0100 
0101 #define PPPOL2TP_DRV_VERSION    "V2.0"
0102 
0103 /* Space for UDP, L2TP and PPP headers */
0104 #define PPPOL2TP_HEADER_OVERHEAD    40
0105 
0106 /* Number of bytes to build transmit L2TP headers.
0107  * Unfortunately the size is different depending on whether sequence numbers
0108  * are enabled.
0109  */
0110 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ      10
0111 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ        6
0112 
0113 /* Private data of each session. This data lives at the end of struct
0114  * l2tp_session, referenced via session->priv[].
0115  */
0116 struct pppol2tp_session {
0117     int         owner;      /* pid that opened the socket */
0118 
0119     struct mutex        sk_lock;    /* Protects .sk */
0120     struct sock __rcu   *sk;        /* Pointer to the session PPPoX socket */
0121     struct sock     *__sk;      /* Copy of .sk, for cleanup */
0122     struct rcu_head     rcu;        /* For asynchronous release */
0123 };
0124 
0125 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
0126 
0127 static const struct ppp_channel_ops pppol2tp_chan_ops = {
0128     .start_xmit =  pppol2tp_xmit,
0129 };
0130 
0131 static const struct proto_ops pppol2tp_ops;
0132 
0133 /* Retrieves the pppol2tp socket associated to a session.
0134  * A reference is held on the returned socket, so this function must be paired
0135  * with sock_put().
0136  */
0137 static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
0138 {
0139     struct pppol2tp_session *ps = l2tp_session_priv(session);
0140     struct sock *sk;
0141 
0142     rcu_read_lock();
0143     sk = rcu_dereference(ps->sk);
0144     if (sk)
0145         sock_hold(sk);
0146     rcu_read_unlock();
0147 
0148     return sk;
0149 }
0150 
0151 /* Helpers to obtain tunnel/session contexts from sockets.
0152  */
0153 static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
0154 {
0155     struct l2tp_session *session;
0156 
0157     if (!sk)
0158         return NULL;
0159 
0160     sock_hold(sk);
0161     session = (struct l2tp_session *)(sk->sk_user_data);
0162     if (!session) {
0163         sock_put(sk);
0164         goto out;
0165     }
0166     if (WARN_ON(session->magic != L2TP_SESSION_MAGIC)) {
0167         session = NULL;
0168         sock_put(sk);
0169         goto out;
0170     }
0171 
0172 out:
0173     return session;
0174 }
0175 
0176 /*****************************************************************************
0177  * Receive data handling
0178  *****************************************************************************/
0179 
0180 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
0181  */
0182 static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
0183                 size_t len, int flags)
0184 {
0185     int err;
0186     struct sk_buff *skb;
0187     struct sock *sk = sock->sk;
0188 
0189     err = -EIO;
0190     if (sk->sk_state & PPPOX_BOUND)
0191         goto end;
0192 
0193     err = 0;
0194     skb = skb_recv_datagram(sk, flags, &err);
0195     if (!skb)
0196         goto end;
0197 
0198     if (len > skb->len)
0199         len = skb->len;
0200     else if (len < skb->len)
0201         msg->msg_flags |= MSG_TRUNC;
0202 
0203     err = skb_copy_datagram_msg(skb, 0, msg, len);
0204     if (likely(err == 0))
0205         err = len;
0206 
0207     kfree_skb(skb);
0208 end:
0209     return err;
0210 }
0211 
0212 static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
0213 {
0214     struct pppol2tp_session *ps = l2tp_session_priv(session);
0215     struct sock *sk = NULL;
0216 
0217     /* If the socket is bound, send it in to PPP's input queue. Otherwise
0218      * queue it on the session socket.
0219      */
0220     rcu_read_lock();
0221     sk = rcu_dereference(ps->sk);
0222     if (!sk)
0223         goto no_sock;
0224 
0225     /* If the first two bytes are 0xFF03, consider that it is the PPP's
0226      * Address and Control fields and skip them. The L2TP module has always
0227      * worked this way, although, in theory, the use of these fields should
0228      * be negotiated and handled at the PPP layer. These fields are
0229      * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
0230      * Information command with Poll/Final bit set to zero (RFC 1662).
0231      */
0232     if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
0233         skb->data[1] == PPP_UI)
0234         skb_pull(skb, 2);
0235 
0236     if (sk->sk_state & PPPOX_BOUND) {
0237         struct pppox_sock *po;
0238 
0239         po = pppox_sk(sk);
0240         ppp_input(&po->chan, skb);
0241     } else {
0242         if (sock_queue_rcv_skb(sk, skb) < 0) {
0243             atomic_long_inc(&session->stats.rx_errors);
0244             kfree_skb(skb);
0245         }
0246     }
0247     rcu_read_unlock();
0248 
0249     return;
0250 
0251 no_sock:
0252     rcu_read_unlock();
0253     pr_warn_ratelimited("%s: no socket in recv\n", session->name);
0254     kfree_skb(skb);
0255 }
0256 
0257 /************************************************************************
0258  * Transmit handling
0259  ***********************************************************************/
0260 
0261 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket.  We come here
0262  * when a user application does a sendmsg() on the session socket. L2TP and
0263  * PPP headers must be inserted into the user's data.
0264  */
0265 static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
0266                 size_t total_len)
0267 {
0268     struct sock *sk = sock->sk;
0269     struct sk_buff *skb;
0270     int error;
0271     struct l2tp_session *session;
0272     struct l2tp_tunnel *tunnel;
0273     int uhlen;
0274 
0275     error = -ENOTCONN;
0276     if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
0277         goto error;
0278 
0279     /* Get session and tunnel contexts */
0280     error = -EBADF;
0281     session = pppol2tp_sock_to_session(sk);
0282     if (!session)
0283         goto error;
0284 
0285     tunnel = session->tunnel;
0286 
0287     uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
0288 
0289     /* Allocate a socket buffer */
0290     error = -ENOMEM;
0291     skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
0292                uhlen + session->hdr_len +
0293                2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
0294                0, GFP_KERNEL);
0295     if (!skb)
0296         goto error_put_sess;
0297 
0298     /* Reserve space for headers. */
0299     skb_reserve(skb, NET_SKB_PAD);
0300     skb_reset_network_header(skb);
0301     skb_reserve(skb, sizeof(struct iphdr));
0302     skb_reset_transport_header(skb);
0303     skb_reserve(skb, uhlen);
0304 
0305     /* Add PPP header */
0306     skb->data[0] = PPP_ALLSTATIONS;
0307     skb->data[1] = PPP_UI;
0308     skb_put(skb, 2);
0309 
0310     /* Copy user data into skb */
0311     error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
0312     if (error < 0) {
0313         kfree_skb(skb);
0314         goto error_put_sess;
0315     }
0316 
0317     local_bh_disable();
0318     l2tp_xmit_skb(session, skb);
0319     local_bh_enable();
0320 
0321     sock_put(sk);
0322 
0323     return total_len;
0324 
0325 error_put_sess:
0326     sock_put(sk);
0327 error:
0328     return error;
0329 }
0330 
0331 /* Transmit function called by generic PPP driver.  Sends PPP frame
0332  * over PPPoL2TP socket.
0333  *
0334  * This is almost the same as pppol2tp_sendmsg(), but rather than
0335  * being called with a msghdr from userspace, it is called with a skb
0336  * from the kernel.
0337  *
0338  * The supplied skb from ppp doesn't have enough headroom for the
0339  * insertion of L2TP, UDP and IP headers so we need to allocate more
0340  * headroom in the skb. This will create a cloned skb. But we must be
0341  * careful in the error case because the caller will expect to free
0342  * the skb it supplied, not our cloned skb. So we take care to always
0343  * leave the original skb unfreed if we return an error.
0344  */
0345 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
0346 {
0347     struct sock *sk = (struct sock *)chan->private;
0348     struct l2tp_session *session;
0349     struct l2tp_tunnel *tunnel;
0350     int uhlen, headroom;
0351 
0352     if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
0353         goto abort;
0354 
0355     /* Get session and tunnel contexts from the socket */
0356     session = pppol2tp_sock_to_session(sk);
0357     if (!session)
0358         goto abort;
0359 
0360     tunnel = session->tunnel;
0361 
0362     uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
0363     headroom = NET_SKB_PAD +
0364            sizeof(struct iphdr) + /* IP header */
0365            uhlen +      /* UDP header (if L2TP_ENCAPTYPE_UDP) */
0366            session->hdr_len +   /* L2TP header */
0367            2;           /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
0368     if (skb_cow_head(skb, headroom))
0369         goto abort_put_sess;
0370 
0371     /* Setup PPP header */
0372     __skb_push(skb, 2);
0373     skb->data[0] = PPP_ALLSTATIONS;
0374     skb->data[1] = PPP_UI;
0375 
0376     local_bh_disable();
0377     l2tp_xmit_skb(session, skb);
0378     local_bh_enable();
0379 
0380     sock_put(sk);
0381 
0382     return 1;
0383 
0384 abort_put_sess:
0385     sock_put(sk);
0386 abort:
0387     /* Free the original skb */
0388     kfree_skb(skb);
0389     return 1;
0390 }
0391 
0392 /*****************************************************************************
0393  * Session (and tunnel control) socket create/destroy.
0394  *****************************************************************************/
0395 
0396 static void pppol2tp_put_sk(struct rcu_head *head)
0397 {
0398     struct pppol2tp_session *ps;
0399 
0400     ps = container_of(head, typeof(*ps), rcu);
0401     sock_put(ps->__sk);
0402 }
0403 
0404 /* Really kill the session socket. (Called from sock_put() if
0405  * refcnt == 0.)
0406  */
0407 static void pppol2tp_session_destruct(struct sock *sk)
0408 {
0409     struct l2tp_session *session = sk->sk_user_data;
0410 
0411     skb_queue_purge(&sk->sk_receive_queue);
0412     skb_queue_purge(&sk->sk_write_queue);
0413 
0414     if (session) {
0415         sk->sk_user_data = NULL;
0416         if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
0417             return;
0418         l2tp_session_dec_refcount(session);
0419     }
0420 }
0421 
0422 /* Called when the PPPoX socket (session) is closed.
0423  */
0424 static int pppol2tp_release(struct socket *sock)
0425 {
0426     struct sock *sk = sock->sk;
0427     struct l2tp_session *session;
0428     int error;
0429 
0430     if (!sk)
0431         return 0;
0432 
0433     error = -EBADF;
0434     lock_sock(sk);
0435     if (sock_flag(sk, SOCK_DEAD) != 0)
0436         goto error;
0437 
0438     pppox_unbind_sock(sk);
0439 
0440     /* Signal the death of the socket. */
0441     sk->sk_state = PPPOX_DEAD;
0442     sock_orphan(sk);
0443     sock->sk = NULL;
0444 
0445     session = pppol2tp_sock_to_session(sk);
0446     if (session) {
0447         struct pppol2tp_session *ps;
0448 
0449         l2tp_session_delete(session);
0450 
0451         ps = l2tp_session_priv(session);
0452         mutex_lock(&ps->sk_lock);
0453         ps->__sk = rcu_dereference_protected(ps->sk,
0454                              lockdep_is_held(&ps->sk_lock));
0455         RCU_INIT_POINTER(ps->sk, NULL);
0456         mutex_unlock(&ps->sk_lock);
0457         call_rcu(&ps->rcu, pppol2tp_put_sk);
0458 
0459         /* Rely on the sock_put() call at the end of the function for
0460          * dropping the reference held by pppol2tp_sock_to_session().
0461          * The last reference will be dropped by pppol2tp_put_sk().
0462          */
0463     }
0464 
0465     release_sock(sk);
0466 
0467     /* This will delete the session context via
0468      * pppol2tp_session_destruct() if the socket's refcnt drops to
0469      * zero.
0470      */
0471     sock_put(sk);
0472 
0473     return 0;
0474 
0475 error:
0476     release_sock(sk);
0477     return error;
0478 }
0479 
0480 static struct proto pppol2tp_sk_proto = {
0481     .name     = "PPPOL2TP",
0482     .owner    = THIS_MODULE,
0483     .obj_size = sizeof(struct pppox_sock),
0484 };
0485 
0486 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
0487 {
0488     int rc;
0489 
0490     rc = l2tp_udp_encap_recv(sk, skb);
0491     if (rc)
0492         kfree_skb(skb);
0493 
0494     return NET_RX_SUCCESS;
0495 }
0496 
0497 /* socket() handler. Initialize a new struct sock.
0498  */
0499 static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
0500 {
0501     int error = -ENOMEM;
0502     struct sock *sk;
0503 
0504     sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
0505     if (!sk)
0506         goto out;
0507 
0508     sock_init_data(sock, sk);
0509 
0510     sock->state  = SS_UNCONNECTED;
0511     sock->ops    = &pppol2tp_ops;
0512 
0513     sk->sk_backlog_rcv = pppol2tp_backlog_recv;
0514     sk->sk_protocol    = PX_PROTO_OL2TP;
0515     sk->sk_family      = PF_PPPOX;
0516     sk->sk_state       = PPPOX_NONE;
0517     sk->sk_type    = SOCK_STREAM;
0518     sk->sk_destruct    = pppol2tp_session_destruct;
0519 
0520     error = 0;
0521 
0522 out:
0523     return error;
0524 }
0525 
0526 static void pppol2tp_show(struct seq_file *m, void *arg)
0527 {
0528     struct l2tp_session *session = arg;
0529     struct sock *sk;
0530 
0531     sk = pppol2tp_session_get_sock(session);
0532     if (sk) {
0533         struct pppox_sock *po = pppox_sk(sk);
0534 
0535         seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
0536         sock_put(sk);
0537     }
0538 }
0539 
0540 static void pppol2tp_session_init(struct l2tp_session *session)
0541 {
0542     struct pppol2tp_session *ps;
0543 
0544     session->recv_skb = pppol2tp_recv;
0545     if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
0546         session->show = pppol2tp_show;
0547 
0548     ps = l2tp_session_priv(session);
0549     mutex_init(&ps->sk_lock);
0550     ps->owner = current->pid;
0551 }
0552 
0553 struct l2tp_connect_info {
0554     u8 version;
0555     int fd;
0556     u32 tunnel_id;
0557     u32 peer_tunnel_id;
0558     u32 session_id;
0559     u32 peer_session_id;
0560 };
0561 
0562 static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
0563                       struct l2tp_connect_info *info)
0564 {
0565     switch (sa_len) {
0566     case sizeof(struct sockaddr_pppol2tp):
0567     {
0568         const struct sockaddr_pppol2tp *sa_v2in4 = sa;
0569 
0570         if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
0571             return -EINVAL;
0572 
0573         info->version = 2;
0574         info->fd = sa_v2in4->pppol2tp.fd;
0575         info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
0576         info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
0577         info->session_id = sa_v2in4->pppol2tp.s_session;
0578         info->peer_session_id = sa_v2in4->pppol2tp.d_session;
0579 
0580         break;
0581     }
0582     case sizeof(struct sockaddr_pppol2tpv3):
0583     {
0584         const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
0585 
0586         if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
0587             return -EINVAL;
0588 
0589         info->version = 3;
0590         info->fd = sa_v3in4->pppol2tp.fd;
0591         info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
0592         info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
0593         info->session_id = sa_v3in4->pppol2tp.s_session;
0594         info->peer_session_id = sa_v3in4->pppol2tp.d_session;
0595 
0596         break;
0597     }
0598     case sizeof(struct sockaddr_pppol2tpin6):
0599     {
0600         const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
0601 
0602         if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
0603             return -EINVAL;
0604 
0605         info->version = 2;
0606         info->fd = sa_v2in6->pppol2tp.fd;
0607         info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
0608         info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
0609         info->session_id = sa_v2in6->pppol2tp.s_session;
0610         info->peer_session_id = sa_v2in6->pppol2tp.d_session;
0611 
0612         break;
0613     }
0614     case sizeof(struct sockaddr_pppol2tpv3in6):
0615     {
0616         const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
0617 
0618         if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
0619             return -EINVAL;
0620 
0621         info->version = 3;
0622         info->fd = sa_v3in6->pppol2tp.fd;
0623         info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
0624         info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
0625         info->session_id = sa_v3in6->pppol2tp.s_session;
0626         info->peer_session_id = sa_v3in6->pppol2tp.d_session;
0627 
0628         break;
0629     }
0630     default:
0631         return -EINVAL;
0632     }
0633 
0634     return 0;
0635 }
0636 
0637 /* Rough estimation of the maximum payload size a tunnel can transmit without
0638  * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
0639  * numbers and no IP option. Not quite accurate, but the result is mostly
0640  * unused anyway.
0641  */
0642 static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
0643 {
0644     int mtu;
0645 
0646     mtu = l2tp_tunnel_dst_mtu(tunnel);
0647     if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
0648         return 1500 - PPPOL2TP_HEADER_OVERHEAD;
0649 
0650     return mtu - PPPOL2TP_HEADER_OVERHEAD;
0651 }
0652 
0653 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
0654  */
0655 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
0656                 int sockaddr_len, int flags)
0657 {
0658     struct sock *sk = sock->sk;
0659     struct pppox_sock *po = pppox_sk(sk);
0660     struct l2tp_session *session = NULL;
0661     struct l2tp_connect_info info;
0662     struct l2tp_tunnel *tunnel;
0663     struct pppol2tp_session *ps;
0664     struct l2tp_session_cfg cfg = { 0, };
0665     bool drop_refcnt = false;
0666     bool drop_tunnel = false;
0667     bool new_session = false;
0668     bool new_tunnel = false;
0669     int error;
0670 
0671     error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
0672     if (error < 0)
0673         return error;
0674 
0675     lock_sock(sk);
0676 
0677     /* Check for already bound sockets */
0678     error = -EBUSY;
0679     if (sk->sk_state & PPPOX_CONNECTED)
0680         goto end;
0681 
0682     /* We don't supporting rebinding anyway */
0683     error = -EALREADY;
0684     if (sk->sk_user_data)
0685         goto end; /* socket is already attached */
0686 
0687     /* Don't bind if tunnel_id is 0 */
0688     error = -EINVAL;
0689     if (!info.tunnel_id)
0690         goto end;
0691 
0692     tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
0693     if (tunnel)
0694         drop_tunnel = true;
0695 
0696     /* Special case: create tunnel context if session_id and
0697      * peer_session_id is 0. Otherwise look up tunnel using supplied
0698      * tunnel id.
0699      */
0700     if (!info.session_id && !info.peer_session_id) {
0701         if (!tunnel) {
0702             struct l2tp_tunnel_cfg tcfg = {
0703                 .encap = L2TP_ENCAPTYPE_UDP,
0704             };
0705 
0706             /* Prevent l2tp_tunnel_register() from trying to set up
0707              * a kernel socket.
0708              */
0709             if (info.fd < 0) {
0710                 error = -EBADF;
0711                 goto end;
0712             }
0713 
0714             error = l2tp_tunnel_create(info.fd,
0715                            info.version,
0716                            info.tunnel_id,
0717                            info.peer_tunnel_id, &tcfg,
0718                            &tunnel);
0719             if (error < 0)
0720                 goto end;
0721 
0722             l2tp_tunnel_inc_refcount(tunnel);
0723             error = l2tp_tunnel_register(tunnel, sock_net(sk),
0724                              &tcfg);
0725             if (error < 0) {
0726                 kfree(tunnel);
0727                 goto end;
0728             }
0729             drop_tunnel = true;
0730             new_tunnel = true;
0731         }
0732     } else {
0733         /* Error if we can't find the tunnel */
0734         error = -ENOENT;
0735         if (!tunnel)
0736             goto end;
0737 
0738         /* Error if socket is not prepped */
0739         if (!tunnel->sock)
0740             goto end;
0741     }
0742 
0743     if (tunnel->peer_tunnel_id == 0)
0744         tunnel->peer_tunnel_id = info.peer_tunnel_id;
0745 
0746     session = l2tp_tunnel_get_session(tunnel, info.session_id);
0747     if (session) {
0748         drop_refcnt = true;
0749 
0750         if (session->pwtype != L2TP_PWTYPE_PPP) {
0751             error = -EPROTOTYPE;
0752             goto end;
0753         }
0754 
0755         ps = l2tp_session_priv(session);
0756 
0757         /* Using a pre-existing session is fine as long as it hasn't
0758          * been connected yet.
0759          */
0760         mutex_lock(&ps->sk_lock);
0761         if (rcu_dereference_protected(ps->sk,
0762                           lockdep_is_held(&ps->sk_lock)) ||
0763             ps->__sk) {
0764             mutex_unlock(&ps->sk_lock);
0765             error = -EEXIST;
0766             goto end;
0767         }
0768     } else {
0769         cfg.pw_type = L2TP_PWTYPE_PPP;
0770 
0771         session = l2tp_session_create(sizeof(struct pppol2tp_session),
0772                           tunnel, info.session_id,
0773                           info.peer_session_id, &cfg);
0774         if (IS_ERR(session)) {
0775             error = PTR_ERR(session);
0776             goto end;
0777         }
0778 
0779         pppol2tp_session_init(session);
0780         ps = l2tp_session_priv(session);
0781         l2tp_session_inc_refcount(session);
0782 
0783         mutex_lock(&ps->sk_lock);
0784         error = l2tp_session_register(session, tunnel);
0785         if (error < 0) {
0786             mutex_unlock(&ps->sk_lock);
0787             kfree(session);
0788             goto end;
0789         }
0790         drop_refcnt = true;
0791         new_session = true;
0792     }
0793 
0794     /* Special case: if source & dest session_id == 0x0000, this
0795      * socket is being created to manage the tunnel. Just set up
0796      * the internal context for use by ioctl() and sockopt()
0797      * handlers.
0798      */
0799     if (session->session_id == 0 && session->peer_session_id == 0) {
0800         error = 0;
0801         goto out_no_ppp;
0802     }
0803 
0804     /* The only header we need to worry about is the L2TP
0805      * header. This size is different depending on whether
0806      * sequence numbers are enabled for the data channel.
0807      */
0808     po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
0809 
0810     po->chan.private = sk;
0811     po->chan.ops     = &pppol2tp_chan_ops;
0812     po->chan.mtu     = pppol2tp_tunnel_mtu(tunnel);
0813 
0814     error = ppp_register_net_channel(sock_net(sk), &po->chan);
0815     if (error) {
0816         mutex_unlock(&ps->sk_lock);
0817         goto end;
0818     }
0819 
0820 out_no_ppp:
0821     /* This is how we get the session context from the socket. */
0822     sk->sk_user_data = session;
0823     rcu_assign_pointer(ps->sk, sk);
0824     mutex_unlock(&ps->sk_lock);
0825 
0826     /* Keep the reference we've grabbed on the session: sk doesn't expect
0827      * the session to disappear. pppol2tp_session_destruct() is responsible
0828      * for dropping it.
0829      */
0830     drop_refcnt = false;
0831 
0832     sk->sk_state = PPPOX_CONNECTED;
0833 
0834 end:
0835     if (error) {
0836         if (new_session)
0837             l2tp_session_delete(session);
0838         if (new_tunnel)
0839             l2tp_tunnel_delete(tunnel);
0840     }
0841     if (drop_refcnt)
0842         l2tp_session_dec_refcount(session);
0843     if (drop_tunnel)
0844         l2tp_tunnel_dec_refcount(tunnel);
0845     release_sock(sk);
0846 
0847     return error;
0848 }
0849 
0850 #ifdef CONFIG_L2TP_V3
0851 
0852 /* Called when creating sessions via the netlink interface. */
0853 static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
0854                    u32 session_id, u32 peer_session_id,
0855                    struct l2tp_session_cfg *cfg)
0856 {
0857     int error;
0858     struct l2tp_session *session;
0859 
0860     /* Error if tunnel socket is not prepped */
0861     if (!tunnel->sock) {
0862         error = -ENOENT;
0863         goto err;
0864     }
0865 
0866     /* Allocate and initialize a new session context. */
0867     session = l2tp_session_create(sizeof(struct pppol2tp_session),
0868                       tunnel, session_id,
0869                       peer_session_id, cfg);
0870     if (IS_ERR(session)) {
0871         error = PTR_ERR(session);
0872         goto err;
0873     }
0874 
0875     pppol2tp_session_init(session);
0876 
0877     error = l2tp_session_register(session, tunnel);
0878     if (error < 0)
0879         goto err_sess;
0880 
0881     return 0;
0882 
0883 err_sess:
0884     kfree(session);
0885 err:
0886     return error;
0887 }
0888 
0889 #endif /* CONFIG_L2TP_V3 */
0890 
0891 /* getname() support.
0892  */
0893 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
0894                 int peer)
0895 {
0896     int len = 0;
0897     int error = 0;
0898     struct l2tp_session *session;
0899     struct l2tp_tunnel *tunnel;
0900     struct sock *sk = sock->sk;
0901     struct inet_sock *inet;
0902     struct pppol2tp_session *pls;
0903 
0904     error = -ENOTCONN;
0905     if (!sk)
0906         goto end;
0907     if (!(sk->sk_state & PPPOX_CONNECTED))
0908         goto end;
0909 
0910     error = -EBADF;
0911     session = pppol2tp_sock_to_session(sk);
0912     if (!session)
0913         goto end;
0914 
0915     pls = l2tp_session_priv(session);
0916     tunnel = session->tunnel;
0917 
0918     inet = inet_sk(tunnel->sock);
0919     if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET) {
0920         struct sockaddr_pppol2tp sp;
0921 
0922         len = sizeof(sp);
0923         memset(&sp, 0, len);
0924         sp.sa_family    = AF_PPPOX;
0925         sp.sa_protocol  = PX_PROTO_OL2TP;
0926         sp.pppol2tp.fd  = tunnel->fd;
0927         sp.pppol2tp.pid = pls->owner;
0928         sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
0929         sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
0930         sp.pppol2tp.s_session = session->session_id;
0931         sp.pppol2tp.d_session = session->peer_session_id;
0932         sp.pppol2tp.addr.sin_family = AF_INET;
0933         sp.pppol2tp.addr.sin_port = inet->inet_dport;
0934         sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
0935         memcpy(uaddr, &sp, len);
0936 #if IS_ENABLED(CONFIG_IPV6)
0937     } else if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET6) {
0938         struct sockaddr_pppol2tpin6 sp;
0939 
0940         len = sizeof(sp);
0941         memset(&sp, 0, len);
0942         sp.sa_family    = AF_PPPOX;
0943         sp.sa_protocol  = PX_PROTO_OL2TP;
0944         sp.pppol2tp.fd  = tunnel->fd;
0945         sp.pppol2tp.pid = pls->owner;
0946         sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
0947         sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
0948         sp.pppol2tp.s_session = session->session_id;
0949         sp.pppol2tp.d_session = session->peer_session_id;
0950         sp.pppol2tp.addr.sin6_family = AF_INET6;
0951         sp.pppol2tp.addr.sin6_port = inet->inet_dport;
0952         memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
0953                sizeof(tunnel->sock->sk_v6_daddr));
0954         memcpy(uaddr, &sp, len);
0955     } else if (tunnel->version == 3 && tunnel->sock->sk_family == AF_INET6) {
0956         struct sockaddr_pppol2tpv3in6 sp;
0957 
0958         len = sizeof(sp);
0959         memset(&sp, 0, len);
0960         sp.sa_family    = AF_PPPOX;
0961         sp.sa_protocol  = PX_PROTO_OL2TP;
0962         sp.pppol2tp.fd  = tunnel->fd;
0963         sp.pppol2tp.pid = pls->owner;
0964         sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
0965         sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
0966         sp.pppol2tp.s_session = session->session_id;
0967         sp.pppol2tp.d_session = session->peer_session_id;
0968         sp.pppol2tp.addr.sin6_family = AF_INET6;
0969         sp.pppol2tp.addr.sin6_port = inet->inet_dport;
0970         memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
0971                sizeof(tunnel->sock->sk_v6_daddr));
0972         memcpy(uaddr, &sp, len);
0973 #endif
0974     } else if (tunnel->version == 3) {
0975         struct sockaddr_pppol2tpv3 sp;
0976 
0977         len = sizeof(sp);
0978         memset(&sp, 0, len);
0979         sp.sa_family    = AF_PPPOX;
0980         sp.sa_protocol  = PX_PROTO_OL2TP;
0981         sp.pppol2tp.fd  = tunnel->fd;
0982         sp.pppol2tp.pid = pls->owner;
0983         sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
0984         sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
0985         sp.pppol2tp.s_session = session->session_id;
0986         sp.pppol2tp.d_session = session->peer_session_id;
0987         sp.pppol2tp.addr.sin_family = AF_INET;
0988         sp.pppol2tp.addr.sin_port = inet->inet_dport;
0989         sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
0990         memcpy(uaddr, &sp, len);
0991     }
0992 
0993     error = len;
0994 
0995     sock_put(sk);
0996 end:
0997     return error;
0998 }
0999 
1000 /****************************************************************************
1001  * ioctl() handlers.
1002  *
1003  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1004  * sockets. However, in order to control kernel tunnel features, we allow
1005  * userspace to create a special "tunnel" PPPoX socket which is used for
1006  * control only.  Tunnel PPPoX sockets have session_id == 0 and simply allow
1007  * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1008  * calls.
1009  ****************************************************************************/
1010 
1011 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1012                 const struct l2tp_stats *stats)
1013 {
1014     memset(dest, 0, sizeof(*dest));
1015 
1016     dest->tx_packets = atomic_long_read(&stats->tx_packets);
1017     dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1018     dest->tx_errors = atomic_long_read(&stats->tx_errors);
1019     dest->rx_packets = atomic_long_read(&stats->rx_packets);
1020     dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1021     dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1022     dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1023     dest->rx_errors = atomic_long_read(&stats->rx_errors);
1024 }
1025 
1026 static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
1027                       struct l2tp_tunnel *tunnel)
1028 {
1029     struct l2tp_session *session;
1030 
1031     if (!stats->session_id) {
1032         pppol2tp_copy_stats(stats, &tunnel->stats);
1033         return 0;
1034     }
1035 
1036     /* If session_id is set, search the corresponding session in the
1037      * context of this tunnel and record the session's statistics.
1038      */
1039     session = l2tp_tunnel_get_session(tunnel, stats->session_id);
1040     if (!session)
1041         return -EBADR;
1042 
1043     if (session->pwtype != L2TP_PWTYPE_PPP) {
1044         l2tp_session_dec_refcount(session);
1045         return -EBADR;
1046     }
1047 
1048     pppol2tp_copy_stats(stats, &session->stats);
1049     l2tp_session_dec_refcount(session);
1050 
1051     return 0;
1052 }
1053 
1054 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1055               unsigned long arg)
1056 {
1057     struct pppol2tp_ioc_stats stats;
1058     struct l2tp_session *session;
1059 
1060     switch (cmd) {
1061     case PPPIOCGMRU:
1062     case PPPIOCGFLAGS:
1063         session = sock->sk->sk_user_data;
1064         if (!session)
1065             return -ENOTCONN;
1066 
1067         if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1068             return -EBADF;
1069 
1070         /* Not defined for tunnels */
1071         if (!session->session_id && !session->peer_session_id)
1072             return -ENOSYS;
1073 
1074         if (put_user(0, (int __user *)arg))
1075             return -EFAULT;
1076         break;
1077 
1078     case PPPIOCSMRU:
1079     case PPPIOCSFLAGS:
1080         session = sock->sk->sk_user_data;
1081         if (!session)
1082             return -ENOTCONN;
1083 
1084         if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1085             return -EBADF;
1086 
1087         /* Not defined for tunnels */
1088         if (!session->session_id && !session->peer_session_id)
1089             return -ENOSYS;
1090 
1091         if (!access_ok((int __user *)arg, sizeof(int)))
1092             return -EFAULT;
1093         break;
1094 
1095     case PPPIOCGL2TPSTATS:
1096         session = sock->sk->sk_user_data;
1097         if (!session)
1098             return -ENOTCONN;
1099 
1100         if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
1101             return -EBADF;
1102 
1103         /* Session 0 represents the parent tunnel */
1104         if (!session->session_id && !session->peer_session_id) {
1105             u32 session_id;
1106             int err;
1107 
1108             if (copy_from_user(&stats, (void __user *)arg,
1109                        sizeof(stats)))
1110                 return -EFAULT;
1111 
1112             session_id = stats.session_id;
1113             err = pppol2tp_tunnel_copy_stats(&stats,
1114                              session->tunnel);
1115             if (err < 0)
1116                 return err;
1117 
1118             stats.session_id = session_id;
1119         } else {
1120             pppol2tp_copy_stats(&stats, &session->stats);
1121             stats.session_id = session->session_id;
1122         }
1123         stats.tunnel_id = session->tunnel->tunnel_id;
1124         stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
1125 
1126         if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
1127             return -EFAULT;
1128         break;
1129 
1130     default:
1131         return -ENOIOCTLCMD;
1132     }
1133 
1134     return 0;
1135 }
1136 
1137 /*****************************************************************************
1138  * setsockopt() / getsockopt() support.
1139  *
1140  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1141  * sockets. In order to control kernel tunnel features, we allow userspace to
1142  * create a special "tunnel" PPPoX socket which is used for control only.
1143  * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1144  * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1145  *****************************************************************************/
1146 
1147 /* Tunnel setsockopt() helper.
1148  */
1149 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1150                       struct l2tp_tunnel *tunnel,
1151                       int optname, int val)
1152 {
1153     int err = 0;
1154 
1155     switch (optname) {
1156     case PPPOL2TP_SO_DEBUG:
1157         /* Tunnel debug flags option is deprecated */
1158         break;
1159 
1160     default:
1161         err = -ENOPROTOOPT;
1162         break;
1163     }
1164 
1165     return err;
1166 }
1167 
1168 /* Session setsockopt helper.
1169  */
1170 static int pppol2tp_session_setsockopt(struct sock *sk,
1171                        struct l2tp_session *session,
1172                        int optname, int val)
1173 {
1174     int err = 0;
1175 
1176     switch (optname) {
1177     case PPPOL2TP_SO_RECVSEQ:
1178         if (val != 0 && val != 1) {
1179             err = -EINVAL;
1180             break;
1181         }
1182         session->recv_seq = !!val;
1183         break;
1184 
1185     case PPPOL2TP_SO_SENDSEQ:
1186         if (val != 0 && val != 1) {
1187             err = -EINVAL;
1188             break;
1189         }
1190         session->send_seq = !!val;
1191         {
1192             struct pppox_sock *po = pppox_sk(sk);
1193 
1194             po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1195                 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1196         }
1197         l2tp_session_set_header_len(session, session->tunnel->version);
1198         break;
1199 
1200     case PPPOL2TP_SO_LNSMODE:
1201         if (val != 0 && val != 1) {
1202             err = -EINVAL;
1203             break;
1204         }
1205         session->lns_mode = !!val;
1206         break;
1207 
1208     case PPPOL2TP_SO_DEBUG:
1209         /* Session debug flags option is deprecated */
1210         break;
1211 
1212     case PPPOL2TP_SO_REORDERTO:
1213         session->reorder_timeout = msecs_to_jiffies(val);
1214         break;
1215 
1216     default:
1217         err = -ENOPROTOOPT;
1218         break;
1219     }
1220 
1221     return err;
1222 }
1223 
1224 /* Main setsockopt() entry point.
1225  * Does API checks, then calls either the tunnel or session setsockopt
1226  * handler, according to whether the PPPoL2TP socket is a for a regular
1227  * session or the special tunnel type.
1228  */
1229 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1230                    sockptr_t optval, unsigned int optlen)
1231 {
1232     struct sock *sk = sock->sk;
1233     struct l2tp_session *session;
1234     struct l2tp_tunnel *tunnel;
1235     int val;
1236     int err;
1237 
1238     if (level != SOL_PPPOL2TP)
1239         return -EINVAL;
1240 
1241     if (optlen < sizeof(int))
1242         return -EINVAL;
1243 
1244     if (copy_from_sockptr(&val, optval, sizeof(int)))
1245         return -EFAULT;
1246 
1247     err = -ENOTCONN;
1248     if (!sk->sk_user_data)
1249         goto end;
1250 
1251     /* Get session context from the socket */
1252     err = -EBADF;
1253     session = pppol2tp_sock_to_session(sk);
1254     if (!session)
1255         goto end;
1256 
1257     /* Special case: if session_id == 0x0000, treat as operation on tunnel
1258      */
1259     if (session->session_id == 0 && session->peer_session_id == 0) {
1260         tunnel = session->tunnel;
1261         err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1262     } else {
1263         err = pppol2tp_session_setsockopt(sk, session, optname, val);
1264     }
1265 
1266     sock_put(sk);
1267 end:
1268     return err;
1269 }
1270 
1271 /* Tunnel getsockopt helper. Called with sock locked.
1272  */
1273 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1274                       struct l2tp_tunnel *tunnel,
1275                       int optname, int *val)
1276 {
1277     int err = 0;
1278 
1279     switch (optname) {
1280     case PPPOL2TP_SO_DEBUG:
1281         /* Tunnel debug flags option is deprecated */
1282         *val = 0;
1283         break;
1284 
1285     default:
1286         err = -ENOPROTOOPT;
1287         break;
1288     }
1289 
1290     return err;
1291 }
1292 
1293 /* Session getsockopt helper. Called with sock locked.
1294  */
1295 static int pppol2tp_session_getsockopt(struct sock *sk,
1296                        struct l2tp_session *session,
1297                        int optname, int *val)
1298 {
1299     int err = 0;
1300 
1301     switch (optname) {
1302     case PPPOL2TP_SO_RECVSEQ:
1303         *val = session->recv_seq;
1304         break;
1305 
1306     case PPPOL2TP_SO_SENDSEQ:
1307         *val = session->send_seq;
1308         break;
1309 
1310     case PPPOL2TP_SO_LNSMODE:
1311         *val = session->lns_mode;
1312         break;
1313 
1314     case PPPOL2TP_SO_DEBUG:
1315         /* Session debug flags option is deprecated */
1316         *val = 0;
1317         break;
1318 
1319     case PPPOL2TP_SO_REORDERTO:
1320         *val = (int)jiffies_to_msecs(session->reorder_timeout);
1321         break;
1322 
1323     default:
1324         err = -ENOPROTOOPT;
1325     }
1326 
1327     return err;
1328 }
1329 
1330 /* Main getsockopt() entry point.
1331  * Does API checks, then calls either the tunnel or session getsockopt
1332  * handler, according to whether the PPPoX socket is a for a regular session
1333  * or the special tunnel type.
1334  */
1335 static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1336                    char __user *optval, int __user *optlen)
1337 {
1338     struct sock *sk = sock->sk;
1339     struct l2tp_session *session;
1340     struct l2tp_tunnel *tunnel;
1341     int val, len;
1342     int err;
1343 
1344     if (level != SOL_PPPOL2TP)
1345         return -EINVAL;
1346 
1347     if (get_user(len, optlen))
1348         return -EFAULT;
1349 
1350     len = min_t(unsigned int, len, sizeof(int));
1351 
1352     if (len < 0)
1353         return -EINVAL;
1354 
1355     err = -ENOTCONN;
1356     if (!sk->sk_user_data)
1357         goto end;
1358 
1359     /* Get the session context */
1360     err = -EBADF;
1361     session = pppol2tp_sock_to_session(sk);
1362     if (!session)
1363         goto end;
1364 
1365     /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1366     if (session->session_id == 0 && session->peer_session_id == 0) {
1367         tunnel = session->tunnel;
1368         err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1369         if (err)
1370             goto end_put_sess;
1371     } else {
1372         err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1373         if (err)
1374             goto end_put_sess;
1375     }
1376 
1377     err = -EFAULT;
1378     if (put_user(len, optlen))
1379         goto end_put_sess;
1380 
1381     if (copy_to_user((void __user *)optval, &val, len))
1382         goto end_put_sess;
1383 
1384     err = 0;
1385 
1386 end_put_sess:
1387     sock_put(sk);
1388 end:
1389     return err;
1390 }
1391 
1392 /*****************************************************************************
1393  * /proc filesystem for debug
1394  * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1395  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1396  *****************************************************************************/
1397 
1398 static unsigned int pppol2tp_net_id;
1399 
1400 #ifdef CONFIG_PROC_FS
1401 
1402 struct pppol2tp_seq_data {
1403     struct seq_net_private p;
1404     int tunnel_idx;         /* current tunnel */
1405     int session_idx;        /* index of session within current tunnel */
1406     struct l2tp_tunnel *tunnel;
1407     struct l2tp_session *session;   /* NULL means get next tunnel */
1408 };
1409 
1410 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1411 {
1412     /* Drop reference taken during previous invocation */
1413     if (pd->tunnel)
1414         l2tp_tunnel_dec_refcount(pd->tunnel);
1415 
1416     for (;;) {
1417         pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
1418         pd->tunnel_idx++;
1419 
1420         /* Only accept L2TPv2 tunnels */
1421         if (!pd->tunnel || pd->tunnel->version == 2)
1422             return;
1423 
1424         l2tp_tunnel_dec_refcount(pd->tunnel);
1425     }
1426 }
1427 
1428 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1429 {
1430     /* Drop reference taken during previous invocation */
1431     if (pd->session)
1432         l2tp_session_dec_refcount(pd->session);
1433 
1434     pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
1435     pd->session_idx++;
1436 
1437     if (!pd->session) {
1438         pd->session_idx = 0;
1439         pppol2tp_next_tunnel(net, pd);
1440     }
1441 }
1442 
1443 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1444 {
1445     struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1446     loff_t pos = *offs;
1447     struct net *net;
1448 
1449     if (!pos)
1450         goto out;
1451 
1452     if (WARN_ON(!m->private)) {
1453         pd = NULL;
1454         goto out;
1455     }
1456 
1457     pd = m->private;
1458     net = seq_file_net(m);
1459 
1460     if (!pd->tunnel)
1461         pppol2tp_next_tunnel(net, pd);
1462     else
1463         pppol2tp_next_session(net, pd);
1464 
1465     /* NULL tunnel and session indicates end of list */
1466     if (!pd->tunnel && !pd->session)
1467         pd = NULL;
1468 
1469 out:
1470     return pd;
1471 }
1472 
1473 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1474 {
1475     (*pos)++;
1476     return NULL;
1477 }
1478 
1479 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1480 {
1481     struct pppol2tp_seq_data *pd = v;
1482 
1483     if (!pd || pd == SEQ_START_TOKEN)
1484         return;
1485 
1486     /* Drop reference taken by last invocation of pppol2tp_next_session()
1487      * or pppol2tp_next_tunnel().
1488      */
1489     if (pd->session) {
1490         l2tp_session_dec_refcount(pd->session);
1491         pd->session = NULL;
1492     }
1493     if (pd->tunnel) {
1494         l2tp_tunnel_dec_refcount(pd->tunnel);
1495         pd->tunnel = NULL;
1496     }
1497 }
1498 
1499 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1500 {
1501     struct l2tp_tunnel *tunnel = v;
1502 
1503     seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1504            tunnel->name,
1505            (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1506            refcount_read(&tunnel->ref_count) - 1);
1507     seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
1508            0,
1509            atomic_long_read(&tunnel->stats.tx_packets),
1510            atomic_long_read(&tunnel->stats.tx_bytes),
1511            atomic_long_read(&tunnel->stats.tx_errors),
1512            atomic_long_read(&tunnel->stats.rx_packets),
1513            atomic_long_read(&tunnel->stats.rx_bytes),
1514            atomic_long_read(&tunnel->stats.rx_errors));
1515 }
1516 
1517 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1518 {
1519     struct l2tp_session *session = v;
1520     struct l2tp_tunnel *tunnel = session->tunnel;
1521     unsigned char state;
1522     char user_data_ok;
1523     struct sock *sk;
1524     u32 ip = 0;
1525     u16 port = 0;
1526 
1527     if (tunnel->sock) {
1528         struct inet_sock *inet = inet_sk(tunnel->sock);
1529 
1530         ip = ntohl(inet->inet_saddr);
1531         port = ntohs(inet->inet_sport);
1532     }
1533 
1534     sk = pppol2tp_session_get_sock(session);
1535     if (sk) {
1536         state = sk->sk_state;
1537         user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1538     } else {
1539         state = 0;
1540         user_data_ok = 'N';
1541     }
1542 
1543     seq_printf(m, "  SESSION '%s' %08X/%d %04X/%04X -> %04X/%04X %d %c\n",
1544            session->name, ip, port,
1545            tunnel->tunnel_id,
1546            session->session_id,
1547            tunnel->peer_tunnel_id,
1548            session->peer_session_id,
1549            state, user_data_ok);
1550     seq_printf(m, "   0/0/%c/%c/%s %08x %u\n",
1551            session->recv_seq ? 'R' : '-',
1552            session->send_seq ? 'S' : '-',
1553            session->lns_mode ? "LNS" : "LAC",
1554            0,
1555            jiffies_to_msecs(session->reorder_timeout));
1556     seq_printf(m, "   %u/%u %ld/%ld/%ld %ld/%ld/%ld\n",
1557            session->nr, session->ns,
1558            atomic_long_read(&session->stats.tx_packets),
1559            atomic_long_read(&session->stats.tx_bytes),
1560            atomic_long_read(&session->stats.tx_errors),
1561            atomic_long_read(&session->stats.rx_packets),
1562            atomic_long_read(&session->stats.rx_bytes),
1563            atomic_long_read(&session->stats.rx_errors));
1564 
1565     if (sk) {
1566         struct pppox_sock *po = pppox_sk(sk);
1567 
1568         seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
1569         sock_put(sk);
1570     }
1571 }
1572 
1573 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1574 {
1575     struct pppol2tp_seq_data *pd = v;
1576 
1577     /* display header on line 1 */
1578     if (v == SEQ_START_TOKEN) {
1579         seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1580         seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1581         seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1582         seq_puts(m, "  SESSION name, addr/port src-tid/sid dest-tid/sid state user-data-ok\n");
1583         seq_puts(m, "   mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1584         seq_puts(m, "   nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1585         goto out;
1586     }
1587 
1588     if (!pd->session)
1589         pppol2tp_seq_tunnel_show(m, pd->tunnel);
1590     else
1591         pppol2tp_seq_session_show(m, pd->session);
1592 
1593 out:
1594     return 0;
1595 }
1596 
1597 static const struct seq_operations pppol2tp_seq_ops = {
1598     .start      = pppol2tp_seq_start,
1599     .next       = pppol2tp_seq_next,
1600     .stop       = pppol2tp_seq_stop,
1601     .show       = pppol2tp_seq_show,
1602 };
1603 #endif /* CONFIG_PROC_FS */
1604 
1605 /*****************************************************************************
1606  * Network namespace
1607  *****************************************************************************/
1608 
1609 static __net_init int pppol2tp_init_net(struct net *net)
1610 {
1611     struct proc_dir_entry *pde;
1612     int err = 0;
1613 
1614     pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1615                   &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
1616     if (!pde) {
1617         err = -ENOMEM;
1618         goto out;
1619     }
1620 
1621 out:
1622     return err;
1623 }
1624 
1625 static __net_exit void pppol2tp_exit_net(struct net *net)
1626 {
1627     remove_proc_entry("pppol2tp", net->proc_net);
1628 }
1629 
1630 static struct pernet_operations pppol2tp_net_ops = {
1631     .init = pppol2tp_init_net,
1632     .exit = pppol2tp_exit_net,
1633     .id   = &pppol2tp_net_id,
1634 };
1635 
1636 /*****************************************************************************
1637  * Init and cleanup
1638  *****************************************************************************/
1639 
1640 static const struct proto_ops pppol2tp_ops = {
1641     .family     = AF_PPPOX,
1642     .owner      = THIS_MODULE,
1643     .release    = pppol2tp_release,
1644     .bind       = sock_no_bind,
1645     .connect    = pppol2tp_connect,
1646     .socketpair = sock_no_socketpair,
1647     .accept     = sock_no_accept,
1648     .getname    = pppol2tp_getname,
1649     .poll       = datagram_poll,
1650     .listen     = sock_no_listen,
1651     .shutdown   = sock_no_shutdown,
1652     .setsockopt = pppol2tp_setsockopt,
1653     .getsockopt = pppol2tp_getsockopt,
1654     .sendmsg    = pppol2tp_sendmsg,
1655     .recvmsg    = pppol2tp_recvmsg,
1656     .mmap       = sock_no_mmap,
1657     .ioctl      = pppox_ioctl,
1658 #ifdef CONFIG_COMPAT
1659     .compat_ioctl = pppox_compat_ioctl,
1660 #endif
1661 };
1662 
1663 static const struct pppox_proto pppol2tp_proto = {
1664     .create     = pppol2tp_create,
1665     .ioctl      = pppol2tp_ioctl,
1666     .owner      = THIS_MODULE,
1667 };
1668 
1669 #ifdef CONFIG_L2TP_V3
1670 
1671 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1672     .session_create = pppol2tp_session_create,
1673     .session_delete = l2tp_session_delete,
1674 };
1675 
1676 #endif /* CONFIG_L2TP_V3 */
1677 
1678 static int __init pppol2tp_init(void)
1679 {
1680     int err;
1681 
1682     err = register_pernet_device(&pppol2tp_net_ops);
1683     if (err)
1684         goto out;
1685 
1686     err = proto_register(&pppol2tp_sk_proto, 0);
1687     if (err)
1688         goto out_unregister_pppol2tp_pernet;
1689 
1690     err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1691     if (err)
1692         goto out_unregister_pppol2tp_proto;
1693 
1694 #ifdef CONFIG_L2TP_V3
1695     err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1696     if (err)
1697         goto out_unregister_pppox;
1698 #endif
1699 
1700     pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1701 
1702 out:
1703     return err;
1704 
1705 #ifdef CONFIG_L2TP_V3
1706 out_unregister_pppox:
1707     unregister_pppox_proto(PX_PROTO_OL2TP);
1708 #endif
1709 out_unregister_pppol2tp_proto:
1710     proto_unregister(&pppol2tp_sk_proto);
1711 out_unregister_pppol2tp_pernet:
1712     unregister_pernet_device(&pppol2tp_net_ops);
1713     goto out;
1714 }
1715 
1716 static void __exit pppol2tp_exit(void)
1717 {
1718 #ifdef CONFIG_L2TP_V3
1719     l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1720 #endif
1721     unregister_pppox_proto(PX_PROTO_OL2TP);
1722     proto_unregister(&pppol2tp_sk_proto);
1723     unregister_pernet_device(&pppol2tp_net_ops);
1724 }
1725 
1726 module_init(pppol2tp_init);
1727 module_exit(pppol2tp_exit);
1728 
1729 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1730 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1731 MODULE_LICENSE("GPL");
1732 MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1733 MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
1734 MODULE_ALIAS_L2TP_PWTYPE(7);