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