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