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