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