l2tp: protect sock pointer of struct pppol2tp_session with RCU
[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         struct sock *sk;
471
472         BUG_ON(session->magic != L2TP_SESSION_MAGIC);
473
474
475         sk = pppol2tp_session_get_sock(session);
476         if (sk) {
477                 if (sk->sk_socket)
478                         inet_shutdown(sk->sk_socket, SEND_SHUTDOWN);
479                 sock_put(sk);
480         }
481
482         /* Don't let the session go away before our socket does */
483         l2tp_session_inc_refcount(session);
484         return;
485 }
486
487 /* Really kill the session socket. (Called from sock_put() if
488  * refcnt == 0.)
489  */
490 static void pppol2tp_session_destruct(struct sock *sk)
491 {
492         struct l2tp_session *session;
493
494         if (sk->sk_user_data != NULL) {
495                 session = sk->sk_user_data;
496                 if (session == NULL)
497                         goto out;
498
499                 sk->sk_user_data = NULL;
500                 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
501                 l2tp_session_dec_refcount(session);
502         }
503
504 out:
505         return;
506 }
507
508 static void pppol2tp_put_sk(struct rcu_head *head)
509 {
510         struct pppol2tp_session *ps;
511
512         ps = container_of(head, typeof(*ps), rcu);
513         sock_put(ps->__sk);
514 }
515
516 /* Called when the PPPoX socket (session) is closed.
517  */
518 static int pppol2tp_release(struct socket *sock)
519 {
520         struct sock *sk = sock->sk;
521         struct l2tp_session *session;
522         int error;
523
524         if (!sk)
525                 return 0;
526
527         error = -EBADF;
528         lock_sock(sk);
529         if (sock_flag(sk, SOCK_DEAD) != 0)
530                 goto error;
531
532         pppox_unbind_sock(sk);
533
534         /* Signal the death of the socket. */
535         sk->sk_state = PPPOX_DEAD;
536         sock_orphan(sk);
537         sock->sk = NULL;
538
539         session = pppol2tp_sock_to_session(sk);
540
541         if (session != NULL) {
542                 struct pppol2tp_session *ps;
543
544                 l2tp_session_queue_purge(session);
545
546                 ps = l2tp_session_priv(session);
547                 mutex_lock(&ps->sk_lock);
548                 ps->__sk = rcu_dereference_protected(ps->sk,
549                                                      lockdep_is_held(&ps->sk_lock));
550                 RCU_INIT_POINTER(ps->sk, NULL);
551                 mutex_unlock(&ps->sk_lock);
552                 call_rcu(&ps->rcu, pppol2tp_put_sk);
553
554                 /* Rely on the sock_put() call at the end of the function for
555                  * dropping the reference held by pppol2tp_sock_to_session().
556                  * The last reference will be dropped by pppol2tp_put_sk().
557                  */
558         }
559         skb_queue_purge(&sk->sk_receive_queue);
560         skb_queue_purge(&sk->sk_write_queue);
561
562         release_sock(sk);
563
564         /* This will delete the session context via
565          * pppol2tp_session_destruct() if the socket's refcnt drops to
566          * zero.
567          */
568         sock_put(sk);
569
570         return 0;
571
572 error:
573         release_sock(sk);
574         return error;
575 }
576
577 static struct proto pppol2tp_sk_proto = {
578         .name     = "PPPOL2TP",
579         .owner    = THIS_MODULE,
580         .obj_size = sizeof(struct pppox_sock),
581 };
582
583 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
584 {
585         int rc;
586
587         rc = l2tp_udp_encap_recv(sk, skb);
588         if (rc)
589                 kfree_skb(skb);
590
591         return NET_RX_SUCCESS;
592 }
593
594 /* socket() handler. Initialize a new struct sock.
595  */
596 static int pppol2tp_create(struct net *net, struct socket *sock)
597 {
598         int error = -ENOMEM;
599         struct sock *sk;
600
601         sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
602         if (!sk)
603                 goto out;
604
605         sock_init_data(sock, sk);
606
607         sock->state  = SS_UNCONNECTED;
608         sock->ops    = &pppol2tp_ops;
609
610         sk->sk_backlog_rcv = pppol2tp_backlog_recv;
611         sk->sk_protocol    = PX_PROTO_OL2TP;
612         sk->sk_family      = PF_PPPOX;
613         sk->sk_state       = PPPOX_NONE;
614         sk->sk_type        = SOCK_STREAM;
615         sk->sk_destruct    = pppol2tp_session_destruct;
616
617         error = 0;
618
619 out:
620         return error;
621 }
622
623 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
624 static void pppol2tp_show(struct seq_file *m, void *arg)
625 {
626         struct l2tp_session *session = arg;
627         struct sock *sk;
628
629         sk = pppol2tp_session_get_sock(session);
630         if (sk) {
631                 struct pppox_sock *po = pppox_sk(sk);
632
633                 seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
634                 sock_put(sk);
635         }
636 }
637 #endif
638
639 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
640  */
641 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
642                             int sockaddr_len, int flags)
643 {
644         struct sock *sk = sock->sk;
645         struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
646         struct sockaddr_pppol2tpv3 *sp3 = (struct sockaddr_pppol2tpv3 *) uservaddr;
647         struct pppox_sock *po = pppox_sk(sk);
648         struct l2tp_session *session = NULL;
649         struct l2tp_tunnel *tunnel;
650         struct pppol2tp_session *ps;
651         struct dst_entry *dst;
652         struct l2tp_session_cfg cfg = { 0, };
653         int error = 0;
654         u32 tunnel_id, peer_tunnel_id;
655         u32 session_id, peer_session_id;
656         bool drop_refcnt = false;
657         bool drop_tunnel = false;
658         int ver = 2;
659         int fd;
660
661         lock_sock(sk);
662
663         error = -EINVAL;
664         if (sp->sa_protocol != PX_PROTO_OL2TP)
665                 goto end;
666
667         /* Check for already bound sockets */
668         error = -EBUSY;
669         if (sk->sk_state & PPPOX_CONNECTED)
670                 goto end;
671
672         /* We don't supporting rebinding anyway */
673         error = -EALREADY;
674         if (sk->sk_user_data)
675                 goto end; /* socket is already attached */
676
677         /* Get params from socket address. Handle L2TPv2 and L2TPv3 */
678         if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
679                 fd = sp->pppol2tp.fd;
680                 tunnel_id = sp->pppol2tp.s_tunnel;
681                 peer_tunnel_id = sp->pppol2tp.d_tunnel;
682                 session_id = sp->pppol2tp.s_session;
683                 peer_session_id = sp->pppol2tp.d_session;
684         } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
685                 ver = 3;
686                 fd = sp3->pppol2tp.fd;
687                 tunnel_id = sp3->pppol2tp.s_tunnel;
688                 peer_tunnel_id = sp3->pppol2tp.d_tunnel;
689                 session_id = sp3->pppol2tp.s_session;
690                 peer_session_id = sp3->pppol2tp.d_session;
691         } else {
692                 error = -EINVAL;
693                 goto end; /* bad socket address */
694         }
695
696         /* Don't bind if tunnel_id is 0 */
697         error = -EINVAL;
698         if (tunnel_id == 0)
699                 goto end;
700
701         tunnel = l2tp_tunnel_get(sock_net(sk), tunnel_id);
702         if (tunnel)
703                 drop_tunnel = true;
704
705         /* Special case: create tunnel context if session_id and
706          * peer_session_id is 0. Otherwise look up tunnel using supplied
707          * tunnel id.
708          */
709         if ((session_id == 0) && (peer_session_id == 0)) {
710                 if (tunnel == NULL) {
711                         struct l2tp_tunnel_cfg tcfg = {
712                                 .encap = L2TP_ENCAPTYPE_UDP,
713                                 .debug = 0,
714                         };
715                         error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
716                         if (error < 0)
717                                 goto end;
718                 }
719         } else {
720                 /* Error if we can't find the tunnel */
721                 error = -ENOENT;
722                 if (tunnel == NULL)
723                         goto end;
724
725                 /* Error if socket is not prepped */
726                 if (tunnel->sock == NULL)
727                         goto end;
728         }
729
730         if (tunnel->recv_payload_hook == NULL)
731                 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
732
733         if (tunnel->peer_tunnel_id == 0) {
734                 if (ver == 2)
735                         tunnel->peer_tunnel_id = sp->pppol2tp.d_tunnel;
736                 else
737                         tunnel->peer_tunnel_id = sp3->pppol2tp.d_tunnel;
738         }
739
740         session = l2tp_session_get(sock_net(sk), tunnel, session_id, false);
741         if (session) {
742                 drop_refcnt = true;
743                 ps = l2tp_session_priv(session);
744
745                 /* Using a pre-existing session is fine as long as it hasn't
746                  * been connected yet.
747                  */
748                 mutex_lock(&ps->sk_lock);
749                 if (rcu_dereference_protected(ps->sk,
750                                               lockdep_is_held(&ps->sk_lock))) {
751                         mutex_unlock(&ps->sk_lock);
752                         error = -EEXIST;
753                         goto end;
754                 }
755
756                 /* consistency checks */
757                 if (ps->tunnel_sock != tunnel->sock) {
758                         mutex_unlock(&ps->sk_lock);
759                         error = -EEXIST;
760                         goto end;
761                 }
762         } else {
763                 /* Default MTU must allow space for UDP/L2TP/PPP headers */
764                 cfg.mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
765                 cfg.mru = cfg.mtu;
766
767                 session = l2tp_session_create(sizeof(struct pppol2tp_session),
768                                               tunnel, session_id,
769                                               peer_session_id, &cfg);
770                 if (IS_ERR(session)) {
771                         error = PTR_ERR(session);
772                         goto end;
773                 }
774
775                 ps = l2tp_session_priv(session);
776                 mutex_init(&ps->sk_lock);
777                 l2tp_session_inc_refcount(session);
778
779                 mutex_lock(&ps->sk_lock);
780                 error = l2tp_session_register(session, tunnel);
781                 if (error < 0) {
782                         mutex_unlock(&ps->sk_lock);
783                         kfree(session);
784                         goto end;
785                 }
786                 drop_refcnt = true;
787         }
788
789         ps->owner            = current->pid;
790         ps->tunnel_sock = tunnel->sock;
791
792         session->recv_skb       = pppol2tp_recv;
793         session->session_close  = pppol2tp_session_close;
794 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
795         session->show           = pppol2tp_show;
796 #endif
797
798         /* If PMTU discovery was enabled, use the MTU that was discovered */
799         dst = sk_dst_get(tunnel->sock);
800         if (dst != NULL) {
801                 u32 pmtu = dst_mtu(dst);
802
803                 if (pmtu != 0)
804                         session->mtu = session->mru = pmtu -
805                                 PPPOL2TP_HEADER_OVERHEAD;
806                 dst_release(dst);
807         }
808
809         /* Special case: if source & dest session_id == 0x0000, this
810          * socket is being created to manage the tunnel. Just set up
811          * the internal context for use by ioctl() and sockopt()
812          * handlers.
813          */
814         if ((session->session_id == 0) &&
815             (session->peer_session_id == 0)) {
816                 error = 0;
817                 goto out_no_ppp;
818         }
819
820         /* The only header we need to worry about is the L2TP
821          * header. This size is different depending on whether
822          * sequence numbers are enabled for the data channel.
823          */
824         po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
825
826         po->chan.private = sk;
827         po->chan.ops     = &pppol2tp_chan_ops;
828         po->chan.mtu     = session->mtu;
829
830         error = ppp_register_net_channel(sock_net(sk), &po->chan);
831         if (error) {
832                 mutex_unlock(&ps->sk_lock);
833                 goto end;
834         }
835
836 out_no_ppp:
837         /* This is how we get the session context from the socket. */
838         sk->sk_user_data = session;
839         rcu_assign_pointer(ps->sk, sk);
840         mutex_unlock(&ps->sk_lock);
841
842         sk->sk_state = PPPOX_CONNECTED;
843         PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
844                "%s: created\n", session->name);
845
846 end:
847         if (drop_refcnt)
848                 l2tp_session_dec_refcount(session);
849         if (drop_tunnel)
850                 l2tp_tunnel_dec_refcount(tunnel);
851         release_sock(sk);
852
853         return error;
854 }
855
856 #ifdef CONFIG_L2TP_V3
857
858 /* Called when creating sessions via the netlink interface. */
859 static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
860                                    u32 session_id, u32 peer_session_id,
861                                    struct l2tp_session_cfg *cfg)
862 {
863         int error;
864         struct l2tp_session *session;
865         struct pppol2tp_session *ps;
866
867         /* Error if tunnel socket is not prepped */
868         if (!tunnel->sock) {
869                 error = -ENOENT;
870                 goto err;
871         }
872
873         /* Default MTU values. */
874         if (cfg->mtu == 0)
875                 cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
876         if (cfg->mru == 0)
877                 cfg->mru = cfg->mtu;
878
879         /* Allocate and initialize a new session context. */
880         session = l2tp_session_create(sizeof(struct pppol2tp_session),
881                                       tunnel, session_id,
882                                       peer_session_id, cfg);
883         if (IS_ERR(session)) {
884                 error = PTR_ERR(session);
885                 goto err;
886         }
887
888         ps = l2tp_session_priv(session);
889         mutex_init(&ps->sk_lock);
890         ps->tunnel_sock = tunnel->sock;
891
892         error = l2tp_session_register(session, tunnel);
893         if (error < 0)
894                 goto err_sess;
895
896         return 0;
897
898 err_sess:
899         kfree(session);
900 err:
901         return error;
902 }
903
904 #endif /* CONFIG_L2TP_V3 */
905
906 /* getname() support.
907  */
908 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
909                             int *usockaddr_len, int peer)
910 {
911         int len = 0;
912         int error = 0;
913         struct l2tp_session *session;
914         struct l2tp_tunnel *tunnel;
915         struct sock *sk = sock->sk;
916         struct inet_sock *inet;
917         struct pppol2tp_session *pls;
918
919         error = -ENOTCONN;
920         if (sk == NULL)
921                 goto end;
922         if (sk->sk_state != PPPOX_CONNECTED)
923                 goto end;
924
925         error = -EBADF;
926         session = pppol2tp_sock_to_session(sk);
927         if (session == NULL)
928                 goto end;
929
930         pls = l2tp_session_priv(session);
931         tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
932         if (tunnel == NULL)
933                 goto end_put_sess;
934
935         inet = inet_sk(tunnel->sock);
936         if (tunnel->version == 2) {
937                 struct sockaddr_pppol2tp sp;
938                 len = sizeof(sp);
939                 memset(&sp, 0, len);
940                 sp.sa_family    = AF_PPPOX;
941                 sp.sa_protocol  = PX_PROTO_OL2TP;
942                 sp.pppol2tp.fd  = tunnel->fd;
943                 sp.pppol2tp.pid = pls->owner;
944                 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
945                 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
946                 sp.pppol2tp.s_session = session->session_id;
947                 sp.pppol2tp.d_session = session->peer_session_id;
948                 sp.pppol2tp.addr.sin_family = AF_INET;
949                 sp.pppol2tp.addr.sin_port = inet->inet_dport;
950                 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
951                 memcpy(uaddr, &sp, len);
952         } else if (tunnel->version == 3) {
953                 struct sockaddr_pppol2tpv3 sp;
954                 len = sizeof(sp);
955                 memset(&sp, 0, len);
956                 sp.sa_family    = AF_PPPOX;
957                 sp.sa_protocol  = PX_PROTO_OL2TP;
958                 sp.pppol2tp.fd  = tunnel->fd;
959                 sp.pppol2tp.pid = pls->owner;
960                 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
961                 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
962                 sp.pppol2tp.s_session = session->session_id;
963                 sp.pppol2tp.d_session = session->peer_session_id;
964                 sp.pppol2tp.addr.sin_family = AF_INET;
965                 sp.pppol2tp.addr.sin_port = inet->inet_dport;
966                 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
967                 memcpy(uaddr, &sp, len);
968         }
969
970         *usockaddr_len = len;
971         error = 0;
972
973         sock_put(pls->tunnel_sock);
974 end_put_sess:
975         sock_put(sk);
976 end:
977         return error;
978 }
979
980 /****************************************************************************
981  * ioctl() handlers.
982  *
983  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
984  * sockets. However, in order to control kernel tunnel features, we allow
985  * userspace to create a special "tunnel" PPPoX socket which is used for
986  * control only.  Tunnel PPPoX sockets have session_id == 0 and simply allow
987  * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
988  * calls.
989  ****************************************************************************/
990
991 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
992                                 struct l2tp_stats *stats)
993 {
994         dest->tx_packets = stats->tx_packets;
995         dest->tx_bytes = stats->tx_bytes;
996         dest->tx_errors = stats->tx_errors;
997         dest->rx_packets = stats->rx_packets;
998         dest->rx_bytes = stats->rx_bytes;
999         dest->rx_seq_discards = stats->rx_seq_discards;
1000         dest->rx_oos_packets = stats->rx_oos_packets;
1001         dest->rx_errors = stats->rx_errors;
1002 }
1003
1004 /* Session ioctl helper.
1005  */
1006 static int pppol2tp_session_ioctl(struct l2tp_session *session,
1007                                   unsigned int cmd, unsigned long arg)
1008 {
1009         struct ifreq ifr;
1010         int err = 0;
1011         struct sock *sk;
1012         int val = (int) arg;
1013         struct pppol2tp_session *ps = l2tp_session_priv(session);
1014         struct l2tp_tunnel *tunnel = session->tunnel;
1015         struct pppol2tp_ioc_stats stats;
1016
1017         PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1018                "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1019                session->name, cmd, arg);
1020
1021         sk = pppol2tp_session_get_sock(session);
1022         if (!sk)
1023                 return -EBADR;
1024
1025         switch (cmd) {
1026         case SIOCGIFMTU:
1027                 err = -ENXIO;
1028                 if (!(sk->sk_state & PPPOX_CONNECTED))
1029                         break;
1030
1031                 err = -EFAULT;
1032                 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1033                         break;
1034                 ifr.ifr_mtu = session->mtu;
1035                 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1036                         break;
1037
1038                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1039                        "%s: get mtu=%d\n", session->name, session->mtu);
1040                 err = 0;
1041                 break;
1042
1043         case SIOCSIFMTU:
1044                 err = -ENXIO;
1045                 if (!(sk->sk_state & PPPOX_CONNECTED))
1046                         break;
1047
1048                 err = -EFAULT;
1049                 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1050                         break;
1051
1052                 session->mtu = ifr.ifr_mtu;
1053
1054                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1055                        "%s: set mtu=%d\n", session->name, session->mtu);
1056                 err = 0;
1057                 break;
1058
1059         case PPPIOCGMRU:
1060                 err = -ENXIO;
1061                 if (!(sk->sk_state & PPPOX_CONNECTED))
1062                         break;
1063
1064                 err = -EFAULT;
1065                 if (put_user(session->mru, (int __user *) arg))
1066                         break;
1067
1068                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1069                        "%s: get mru=%d\n", session->name, session->mru);
1070                 err = 0;
1071                 break;
1072
1073         case PPPIOCSMRU:
1074                 err = -ENXIO;
1075                 if (!(sk->sk_state & PPPOX_CONNECTED))
1076                         break;
1077
1078                 err = -EFAULT;
1079                 if (get_user(val, (int __user *) arg))
1080                         break;
1081
1082                 session->mru = val;
1083                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1084                        "%s: set mru=%d\n", session->name, session->mru);
1085                 err = 0;
1086                 break;
1087
1088         case PPPIOCGFLAGS:
1089                 err = -EFAULT;
1090                 if (put_user(ps->flags, (int __user *) arg))
1091                         break;
1092
1093                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1094                        "%s: get flags=%d\n", session->name, ps->flags);
1095                 err = 0;
1096                 break;
1097
1098         case PPPIOCSFLAGS:
1099                 err = -EFAULT;
1100                 if (get_user(val, (int __user *) arg))
1101                         break;
1102                 ps->flags = val;
1103                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1104                        "%s: set flags=%d\n", session->name, ps->flags);
1105                 err = 0;
1106                 break;
1107
1108         case PPPIOCGL2TPSTATS:
1109                 err = -ENXIO;
1110                 if (!(sk->sk_state & PPPOX_CONNECTED))
1111                         break;
1112
1113                 memset(&stats, 0, sizeof(stats));
1114                 stats.tunnel_id = tunnel->tunnel_id;
1115                 stats.session_id = session->session_id;
1116                 pppol2tp_copy_stats(&stats, &session->stats);
1117                 if (copy_to_user((void __user *) arg, &stats,
1118                                  sizeof(stats)))
1119                         break;
1120                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1121                        "%s: get L2TP stats\n", session->name);
1122                 err = 0;
1123                 break;
1124
1125         default:
1126                 err = -ENOSYS;
1127                 break;
1128         }
1129
1130         sock_put(sk);
1131
1132         return err;
1133 }
1134
1135 /* Tunnel ioctl helper.
1136  *
1137  * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1138  * specifies a session_id, the session ioctl handler is called. This allows an
1139  * application to retrieve session stats via a tunnel socket.
1140  */
1141 static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1142                                  unsigned int cmd, unsigned long arg)
1143 {
1144         int err = 0;
1145         struct sock *sk;
1146         struct pppol2tp_ioc_stats stats;
1147
1148         PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1149                "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1150                tunnel->name, cmd, arg);
1151
1152         sk = tunnel->sock;
1153         sock_hold(sk);
1154
1155         switch (cmd) {
1156         case PPPIOCGL2TPSTATS:
1157                 err = -ENXIO;
1158                 if (!(sk->sk_state & PPPOX_CONNECTED))
1159                         break;
1160
1161                 if (copy_from_user(&stats, (void __user *) arg,
1162                                    sizeof(stats))) {
1163                         err = -EFAULT;
1164                         break;
1165                 }
1166                 if (stats.session_id != 0) {
1167                         /* resend to session ioctl handler */
1168                         struct l2tp_session *session =
1169                                 l2tp_session_get(sock_net(sk), tunnel,
1170                                                  stats.session_id, true);
1171
1172                         if (session) {
1173                                 err = pppol2tp_session_ioctl(session, cmd,
1174                                                              arg);
1175                                 if (session->deref)
1176                                         session->deref(session);
1177                                 l2tp_session_dec_refcount(session);
1178                         } else {
1179                                 err = -EBADR;
1180                         }
1181                         break;
1182                 }
1183 #ifdef CONFIG_XFRM
1184                 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1185 #endif
1186                 pppol2tp_copy_stats(&stats, &tunnel->stats);
1187                 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1188                         err = -EFAULT;
1189                         break;
1190                 }
1191                 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1192                        "%s: get L2TP stats\n", tunnel->name);
1193                 err = 0;
1194                 break;
1195
1196         default:
1197                 err = -ENOSYS;
1198                 break;
1199         }
1200
1201         sock_put(sk);
1202
1203         return err;
1204 }
1205
1206 /* Main ioctl() handler.
1207  * Dispatch to tunnel or session helpers depending on the socket.
1208  */
1209 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1210                           unsigned long arg)
1211 {
1212         struct sock *sk = sock->sk;
1213         struct l2tp_session *session;
1214         struct l2tp_tunnel *tunnel;
1215         struct pppol2tp_session *ps;
1216         int err;
1217
1218         if (!sk)
1219                 return 0;
1220
1221         err = -EBADF;
1222         if (sock_flag(sk, SOCK_DEAD) != 0)
1223                 goto end;
1224
1225         err = -ENOTCONN;
1226         if ((sk->sk_user_data == NULL) ||
1227             (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1228                 goto end;
1229
1230         /* Get session context from the socket */
1231         err = -EBADF;
1232         session = pppol2tp_sock_to_session(sk);
1233         if (session == NULL)
1234                 goto end;
1235
1236         /* Special case: if session's session_id is zero, treat ioctl as a
1237          * tunnel ioctl
1238          */
1239         ps = l2tp_session_priv(session);
1240         if ((session->session_id == 0) &&
1241             (session->peer_session_id == 0)) {
1242                 err = -EBADF;
1243                 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1244                 if (tunnel == NULL)
1245                         goto end_put_sess;
1246
1247                 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1248                 sock_put(ps->tunnel_sock);
1249                 goto end_put_sess;
1250         }
1251
1252         err = pppol2tp_session_ioctl(session, cmd, arg);
1253
1254 end_put_sess:
1255         sock_put(sk);
1256 end:
1257         return err;
1258 }
1259
1260 /*****************************************************************************
1261  * setsockopt() / getsockopt() support.
1262  *
1263  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1264  * sockets. In order to control kernel tunnel features, we allow userspace to
1265  * create a special "tunnel" PPPoX socket which is used for control only.
1266  * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1267  * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1268  *****************************************************************************/
1269
1270 /* Tunnel setsockopt() helper.
1271  */
1272 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1273                                       struct l2tp_tunnel *tunnel,
1274                                       int optname, int val)
1275 {
1276         int err = 0;
1277
1278         switch (optname) {
1279         case PPPOL2TP_SO_DEBUG:
1280                 tunnel->debug = val;
1281                 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1282                        "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1283                 break;
1284
1285         default:
1286                 err = -ENOPROTOOPT;
1287                 break;
1288         }
1289
1290         return err;
1291 }
1292
1293 /* Session setsockopt helper.
1294  */
1295 static int pppol2tp_session_setsockopt(struct sock *sk,
1296                                        struct l2tp_session *session,
1297                                        int optname, int val)
1298 {
1299         int err = 0;
1300
1301         switch (optname) {
1302         case PPPOL2TP_SO_RECVSEQ:
1303                 if ((val != 0) && (val != 1)) {
1304                         err = -EINVAL;
1305                         break;
1306                 }
1307                 session->recv_seq = val ? -1 : 0;
1308                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1309                        "%s: set recv_seq=%d\n", session->name, session->recv_seq);
1310                 break;
1311
1312         case PPPOL2TP_SO_SENDSEQ:
1313                 if ((val != 0) && (val != 1)) {
1314                         err = -EINVAL;
1315                         break;
1316                 }
1317                 session->send_seq = val ? -1 : 0;
1318                 {
1319                         struct pppox_sock *po = pppox_sk(sk);
1320
1321                         po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1322                                 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1323                 }
1324                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1325                        "%s: set send_seq=%d\n", session->name, session->send_seq);
1326                 break;
1327
1328         case PPPOL2TP_SO_LNSMODE:
1329                 if ((val != 0) && (val != 1)) {
1330                         err = -EINVAL;
1331                         break;
1332                 }
1333                 session->lns_mode = val ? -1 : 0;
1334                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1335                        "%s: set lns_mode=%d\n", session->name, session->lns_mode);
1336                 break;
1337
1338         case PPPOL2TP_SO_DEBUG:
1339                 session->debug = val;
1340                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1341                        "%s: set debug=%x\n", session->name, session->debug);
1342                 break;
1343
1344         case PPPOL2TP_SO_REORDERTO:
1345                 session->reorder_timeout = msecs_to_jiffies(val);
1346                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1347                        "%s: set reorder_timeout=%d\n", session->name, session->reorder_timeout);
1348                 break;
1349
1350         default:
1351                 err = -ENOPROTOOPT;
1352                 break;
1353         }
1354
1355         return err;
1356 }
1357
1358 /* Main setsockopt() entry point.
1359  * Does API checks, then calls either the tunnel or session setsockopt
1360  * handler, according to whether the PPPoL2TP socket is a for a regular
1361  * session or the special tunnel type.
1362  */
1363 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1364                                char __user *optval, unsigned int optlen)
1365 {
1366         struct sock *sk = sock->sk;
1367         struct l2tp_session *session;
1368         struct l2tp_tunnel *tunnel;
1369         struct pppol2tp_session *ps;
1370         int val;
1371         int err;
1372
1373         if (level != SOL_PPPOL2TP)
1374                 return -EINVAL;
1375
1376         if (optlen < sizeof(int))
1377                 return -EINVAL;
1378
1379         if (get_user(val, (int __user *)optval))
1380                 return -EFAULT;
1381
1382         err = -ENOTCONN;
1383         if (sk->sk_user_data == NULL)
1384                 goto end;
1385
1386         /* Get session context from the socket */
1387         err = -EBADF;
1388         session = pppol2tp_sock_to_session(sk);
1389         if (session == NULL)
1390                 goto end;
1391
1392         /* Special case: if session_id == 0x0000, treat as operation on tunnel
1393          */
1394         ps = l2tp_session_priv(session);
1395         if ((session->session_id == 0) &&
1396             (session->peer_session_id == 0)) {
1397                 err = -EBADF;
1398                 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1399                 if (tunnel == NULL)
1400                         goto end_put_sess;
1401
1402                 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1403                 sock_put(ps->tunnel_sock);
1404         } else
1405                 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1406
1407 end_put_sess:
1408         sock_put(sk);
1409 end:
1410         return err;
1411 }
1412
1413 /* Tunnel getsockopt helper. Called with sock locked.
1414  */
1415 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1416                                       struct l2tp_tunnel *tunnel,
1417                                       int optname, int *val)
1418 {
1419         int err = 0;
1420
1421         switch (optname) {
1422         case PPPOL2TP_SO_DEBUG:
1423                 *val = tunnel->debug;
1424                 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1425                        "%s: get debug=%x\n", tunnel->name, tunnel->debug);
1426                 break;
1427
1428         default:
1429                 err = -ENOPROTOOPT;
1430                 break;
1431         }
1432
1433         return err;
1434 }
1435
1436 /* Session getsockopt helper. Called with sock locked.
1437  */
1438 static int pppol2tp_session_getsockopt(struct sock *sk,
1439                                        struct l2tp_session *session,
1440                                        int optname, int *val)
1441 {
1442         int err = 0;
1443
1444         switch (optname) {
1445         case PPPOL2TP_SO_RECVSEQ:
1446                 *val = session->recv_seq;
1447                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1448                        "%s: get recv_seq=%d\n", session->name, *val);
1449                 break;
1450
1451         case PPPOL2TP_SO_SENDSEQ:
1452                 *val = session->send_seq;
1453                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1454                        "%s: get send_seq=%d\n", session->name, *val);
1455                 break;
1456
1457         case PPPOL2TP_SO_LNSMODE:
1458                 *val = session->lns_mode;
1459                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1460                        "%s: get lns_mode=%d\n", session->name, *val);
1461                 break;
1462
1463         case PPPOL2TP_SO_DEBUG:
1464                 *val = session->debug;
1465                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1466                        "%s: get debug=%d\n", session->name, *val);
1467                 break;
1468
1469         case PPPOL2TP_SO_REORDERTO:
1470                 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1471                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1472                        "%s: get reorder_timeout=%d\n", session->name, *val);
1473                 break;
1474
1475         default:
1476                 err = -ENOPROTOOPT;
1477         }
1478
1479         return err;
1480 }
1481
1482 /* Main getsockopt() entry point.
1483  * Does API checks, then calls either the tunnel or session getsockopt
1484  * handler, according to whether the PPPoX socket is a for a regular session
1485  * or the special tunnel type.
1486  */
1487 static int pppol2tp_getsockopt(struct socket *sock, int level,
1488                                int optname, char __user *optval, int __user *optlen)
1489 {
1490         struct sock *sk = sock->sk;
1491         struct l2tp_session *session;
1492         struct l2tp_tunnel *tunnel;
1493         int val, len;
1494         int err;
1495         struct pppol2tp_session *ps;
1496
1497         if (level != SOL_PPPOL2TP)
1498                 return -EINVAL;
1499
1500         if (get_user(len, (int __user *) optlen))
1501                 return -EFAULT;
1502
1503         len = min_t(unsigned int, len, sizeof(int));
1504
1505         if (len < 0)
1506                 return -EINVAL;
1507
1508         err = -ENOTCONN;
1509         if (sk->sk_user_data == NULL)
1510                 goto end;
1511
1512         /* Get the session context */
1513         err = -EBADF;
1514         session = pppol2tp_sock_to_session(sk);
1515         if (session == NULL)
1516                 goto end;
1517
1518         /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1519         ps = l2tp_session_priv(session);
1520         if ((session->session_id == 0) &&
1521             (session->peer_session_id == 0)) {
1522                 err = -EBADF;
1523                 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1524                 if (tunnel == NULL)
1525                         goto end_put_sess;
1526
1527                 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1528                 sock_put(ps->tunnel_sock);
1529                 if (err)
1530                         goto end_put_sess;
1531         } else {
1532                 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1533                 if (err)
1534                         goto end_put_sess;
1535         }
1536
1537         err = -EFAULT;
1538         if (put_user(len, (int __user *) optlen))
1539                 goto end_put_sess;
1540
1541         if (copy_to_user((void __user *) optval, &val, len))
1542                 goto end_put_sess;
1543
1544         err = 0;
1545
1546 end_put_sess:
1547         sock_put(sk);
1548 end:
1549         return err;
1550 }
1551
1552 /*****************************************************************************
1553  * /proc filesystem for debug
1554  * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1555  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1556  *****************************************************************************/
1557
1558 static unsigned int pppol2tp_net_id;
1559
1560 #ifdef CONFIG_PROC_FS
1561
1562 struct pppol2tp_seq_data {
1563         struct seq_net_private p;
1564         int tunnel_idx;                 /* current tunnel */
1565         int session_idx;                /* index of session within current tunnel */
1566         struct l2tp_tunnel *tunnel;
1567         struct l2tp_session *session;   /* NULL means get next tunnel */
1568 };
1569
1570 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1571 {
1572         for (;;) {
1573                 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1574                 pd->tunnel_idx++;
1575
1576                 if (pd->tunnel == NULL)
1577                         break;
1578
1579                 /* Ignore L2TPv3 tunnels */
1580                 if (pd->tunnel->version < 3)
1581                         break;
1582         }
1583 }
1584
1585 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1586 {
1587         pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx, true);
1588         pd->session_idx++;
1589
1590         if (pd->session == NULL) {
1591                 pd->session_idx = 0;
1592                 pppol2tp_next_tunnel(net, pd);
1593         }
1594 }
1595
1596 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1597 {
1598         struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1599         loff_t pos = *offs;
1600         struct net *net;
1601
1602         if (!pos)
1603                 goto out;
1604
1605         BUG_ON(m->private == NULL);
1606         pd = m->private;
1607         net = seq_file_net(m);
1608
1609         if (pd->tunnel == NULL)
1610                 pppol2tp_next_tunnel(net, pd);
1611         else
1612                 pppol2tp_next_session(net, pd);
1613
1614         /* NULL tunnel and session indicates end of list */
1615         if ((pd->tunnel == NULL) && (pd->session == NULL))
1616                 pd = NULL;
1617
1618 out:
1619         return pd;
1620 }
1621
1622 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1623 {
1624         (*pos)++;
1625         return NULL;
1626 }
1627
1628 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1629 {
1630         /* nothing to do */
1631 }
1632
1633 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1634 {
1635         struct l2tp_tunnel *tunnel = v;
1636
1637         seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1638                    tunnel->name,
1639                    (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1640                    atomic_read(&tunnel->ref_count) - 1);
1641         seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1642                    tunnel->debug,
1643                    (unsigned long long)tunnel->stats.tx_packets,
1644                    (unsigned long long)tunnel->stats.tx_bytes,
1645                    (unsigned long long)tunnel->stats.tx_errors,
1646                    (unsigned long long)tunnel->stats.rx_packets,
1647                    (unsigned long long)tunnel->stats.rx_bytes,
1648                    (unsigned long long)tunnel->stats.rx_errors);
1649 }
1650
1651 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1652 {
1653         struct l2tp_session *session = v;
1654         struct l2tp_tunnel *tunnel = session->tunnel;
1655         unsigned char state;
1656         char user_data_ok;
1657         struct sock *sk;
1658         u32 ip = 0;
1659         u16 port = 0;
1660
1661         if (tunnel->sock) {
1662                 struct inet_sock *inet = inet_sk(tunnel->sock);
1663                 ip = ntohl(inet->inet_saddr);
1664                 port = ntohs(inet->inet_sport);
1665         }
1666
1667         sk = pppol2tp_session_get_sock(session);
1668         if (sk) {
1669                 state = sk->sk_state;
1670                 user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1671         } else {
1672                 state = 0;
1673                 user_data_ok = 'N';
1674         }
1675
1676         seq_printf(m, "  SESSION '%s' %08X/%d %04X/%04X -> "
1677                    "%04X/%04X %d %c\n",
1678                    session->name, ip, port,
1679                    tunnel->tunnel_id,
1680                    session->session_id,
1681                    tunnel->peer_tunnel_id,
1682                    session->peer_session_id,
1683                    state, user_data_ok);
1684         seq_printf(m, "   %d/%d/%c/%c/%s %08x %u\n",
1685                    session->mtu, session->mru,
1686                    session->recv_seq ? 'R' : '-',
1687                    session->send_seq ? 'S' : '-',
1688                    session->lns_mode ? "LNS" : "LAC",
1689                    session->debug,
1690                    jiffies_to_msecs(session->reorder_timeout));
1691         seq_printf(m, "   %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1692                    session->nr, session->ns,
1693                    (unsigned long long)session->stats.tx_packets,
1694                    (unsigned long long)session->stats.tx_bytes,
1695                    (unsigned long long)session->stats.tx_errors,
1696                    (unsigned long long)session->stats.rx_packets,
1697                    (unsigned long long)session->stats.rx_bytes,
1698                    (unsigned long long)session->stats.rx_errors);
1699
1700         if (sk) {
1701                 struct pppox_sock *po = pppox_sk(sk);
1702
1703                 seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
1704                 sock_put(sk);
1705         }
1706 }
1707
1708 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1709 {
1710         struct pppol2tp_seq_data *pd = v;
1711
1712         /* display header on line 1 */
1713         if (v == SEQ_START_TOKEN) {
1714                 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1715                 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1716                 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1717                 seq_puts(m, "  SESSION name, addr/port src-tid/sid "
1718                          "dest-tid/sid state user-data-ok\n");
1719                 seq_puts(m, "   mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1720                 seq_puts(m, "   nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1721                 goto out;
1722         }
1723
1724         /* Show the tunnel or session context.
1725          */
1726         if (!pd->session) {
1727                 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1728         } else {
1729                 pppol2tp_seq_session_show(m, pd->session);
1730                 if (pd->session->deref)
1731                         pd->session->deref(pd->session);
1732                 l2tp_session_dec_refcount(pd->session);
1733         }
1734
1735 out:
1736         return 0;
1737 }
1738
1739 static const struct seq_operations pppol2tp_seq_ops = {
1740         .start          = pppol2tp_seq_start,
1741         .next           = pppol2tp_seq_next,
1742         .stop           = pppol2tp_seq_stop,
1743         .show           = pppol2tp_seq_show,
1744 };
1745
1746 /* Called when our /proc file is opened. We allocate data for use when
1747  * iterating our tunnel / session contexts and store it in the private
1748  * data of the seq_file.
1749  */
1750 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1751 {
1752         return seq_open_net(inode, file, &pppol2tp_seq_ops,
1753                             sizeof(struct pppol2tp_seq_data));
1754 }
1755
1756 static const struct file_operations pppol2tp_proc_fops = {
1757         .owner          = THIS_MODULE,
1758         .open           = pppol2tp_proc_open,
1759         .read           = seq_read,
1760         .llseek         = seq_lseek,
1761         .release        = seq_release_net,
1762 };
1763
1764 #endif /* CONFIG_PROC_FS */
1765
1766 /*****************************************************************************
1767  * Network namespace
1768  *****************************************************************************/
1769
1770 static __net_init int pppol2tp_init_net(struct net *net)
1771 {
1772         struct proc_dir_entry *pde;
1773         int err = 0;
1774
1775         pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1776         if (!pde) {
1777                 err = -ENOMEM;
1778                 goto out;
1779         }
1780
1781 out:
1782         return err;
1783 }
1784
1785 static __net_exit void pppol2tp_exit_net(struct net *net)
1786 {
1787         proc_net_remove(net, "pppol2tp");
1788 }
1789
1790 static struct pernet_operations pppol2tp_net_ops = {
1791         .init = pppol2tp_init_net,
1792         .exit = pppol2tp_exit_net,
1793         .id   = &pppol2tp_net_id,
1794 };
1795
1796 /*****************************************************************************
1797  * Init and cleanup
1798  *****************************************************************************/
1799
1800 static const struct proto_ops pppol2tp_ops = {
1801         .family         = AF_PPPOX,
1802         .owner          = THIS_MODULE,
1803         .release        = pppol2tp_release,
1804         .bind           = sock_no_bind,
1805         .connect        = pppol2tp_connect,
1806         .socketpair     = sock_no_socketpair,
1807         .accept         = sock_no_accept,
1808         .getname        = pppol2tp_getname,
1809         .poll           = datagram_poll,
1810         .listen         = sock_no_listen,
1811         .shutdown       = sock_no_shutdown,
1812         .setsockopt     = pppol2tp_setsockopt,
1813         .getsockopt     = pppol2tp_getsockopt,
1814         .sendmsg        = pppol2tp_sendmsg,
1815         .recvmsg        = pppol2tp_recvmsg,
1816         .mmap           = sock_no_mmap,
1817         .ioctl          = pppox_ioctl,
1818 };
1819
1820 static const struct pppox_proto pppol2tp_proto = {
1821         .create         = pppol2tp_create,
1822         .ioctl          = pppol2tp_ioctl,
1823         .owner          = THIS_MODULE,
1824 };
1825
1826 #ifdef CONFIG_L2TP_V3
1827
1828 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1829         .session_create = pppol2tp_session_create,
1830         .session_delete = l2tp_session_delete,
1831 };
1832
1833 #endif /* CONFIG_L2TP_V3 */
1834
1835 static int __init pppol2tp_init(void)
1836 {
1837         int err;
1838
1839         err = register_pernet_device(&pppol2tp_net_ops);
1840         if (err)
1841                 goto out;
1842
1843         err = proto_register(&pppol2tp_sk_proto, 0);
1844         if (err)
1845                 goto out_unregister_pppol2tp_pernet;
1846
1847         err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1848         if (err)
1849                 goto out_unregister_pppol2tp_proto;
1850
1851 #ifdef CONFIG_L2TP_V3
1852         err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1853         if (err)
1854                 goto out_unregister_pppox;
1855 #endif
1856
1857         printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
1858                PPPOL2TP_DRV_VERSION);
1859
1860 out:
1861         return err;
1862
1863 #ifdef CONFIG_L2TP_V3
1864 out_unregister_pppox:
1865         unregister_pppox_proto(PX_PROTO_OL2TP);
1866 #endif
1867 out_unregister_pppol2tp_proto:
1868         proto_unregister(&pppol2tp_sk_proto);
1869 out_unregister_pppol2tp_pernet:
1870         unregister_pernet_device(&pppol2tp_net_ops);
1871         goto out;
1872 }
1873
1874 static void __exit pppol2tp_exit(void)
1875 {
1876 #ifdef CONFIG_L2TP_V3
1877         l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1878 #endif
1879         unregister_pppox_proto(PX_PROTO_OL2TP);
1880         proto_unregister(&pppol2tp_sk_proto);
1881         unregister_pernet_device(&pppol2tp_net_ops);
1882 }
1883
1884 module_init(pppol2tp_init);
1885 module_exit(pppol2tp_exit);
1886
1887 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1888 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1889 MODULE_LICENSE("GPL");
1890 MODULE_VERSION(PPPOL2TP_DRV_VERSION);