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