net/l2tp: don't fall back on UDP [get|set]sockopt
[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(__sk_dst_get(tunnel->sock));
778                 if (pmtu != 0)
779                         session->mtu = session->mru = pmtu -
780                                 PPPOL2TP_HEADER_OVERHEAD;
781                 dst_release(dst);
782         }
783
784         /* Special case: if source & dest session_id == 0x0000, this
785          * socket is being created to manage the tunnel. Just set up
786          * the internal context for use by ioctl() and sockopt()
787          * handlers.
788          */
789         if ((session->session_id == 0) &&
790             (session->peer_session_id == 0)) {
791                 error = 0;
792                 goto out_no_ppp;
793         }
794
795         /* The only header we need to worry about is the L2TP
796          * header. This size is different depending on whether
797          * sequence numbers are enabled for the data channel.
798          */
799         po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
800
801         po->chan.private = sk;
802         po->chan.ops     = &pppol2tp_chan_ops;
803         po->chan.mtu     = session->mtu;
804
805         error = ppp_register_net_channel(sock_net(sk), &po->chan);
806         if (error)
807                 goto end;
808
809 out_no_ppp:
810         /* This is how we get the session context from the socket. */
811         sk->sk_user_data = session;
812         sk->sk_state = PPPOX_CONNECTED;
813         PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
814                "%s: created\n", session->name);
815
816 end:
817         release_sock(sk);
818
819         return error;
820 }
821
822 #ifdef CONFIG_L2TP_V3
823
824 /* Called when creating sessions via the netlink interface.
825  */
826 static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
827 {
828         int error;
829         struct l2tp_tunnel *tunnel;
830         struct l2tp_session *session;
831         struct pppol2tp_session *ps;
832
833         tunnel = l2tp_tunnel_find(net, tunnel_id);
834
835         /* Error if we can't find the tunnel */
836         error = -ENOENT;
837         if (tunnel == NULL)
838                 goto out;
839
840         /* Error if tunnel socket is not prepped */
841         if (tunnel->sock == NULL)
842                 goto out;
843
844         /* Check that this session doesn't already exist */
845         error = -EEXIST;
846         session = l2tp_session_find(net, tunnel, session_id);
847         if (session != NULL)
848                 goto out;
849
850         /* Default MTU values. */
851         if (cfg->mtu == 0)
852                 cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
853         if (cfg->mru == 0)
854                 cfg->mru = cfg->mtu;
855
856         /* Allocate and initialize a new session context. */
857         error = -ENOMEM;
858         session = l2tp_session_create(sizeof(struct pppol2tp_session),
859                                       tunnel, session_id,
860                                       peer_session_id, cfg);
861         if (session == NULL)
862                 goto out;
863
864         ps = l2tp_session_priv(session);
865         ps->tunnel_sock = tunnel->sock;
866
867         PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
868                "%s: created\n", session->name);
869
870         error = 0;
871
872 out:
873         return error;
874 }
875
876 /* Called when deleting sessions via the netlink interface.
877  */
878 static int pppol2tp_session_delete(struct l2tp_session *session)
879 {
880         struct pppol2tp_session *ps = l2tp_session_priv(session);
881
882         if (ps->sock == NULL)
883                 l2tp_session_dec_refcount(session);
884
885         return 0;
886 }
887
888 #endif /* CONFIG_L2TP_V3 */
889
890 /* getname() support.
891  */
892 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
893                             int *usockaddr_len, int peer)
894 {
895         int len = 0;
896         int error = 0;
897         struct l2tp_session *session;
898         struct l2tp_tunnel *tunnel;
899         struct sock *sk = sock->sk;
900         struct inet_sock *inet;
901         struct pppol2tp_session *pls;
902
903         error = -ENOTCONN;
904         if (sk == NULL)
905                 goto end;
906         if (sk->sk_state != PPPOX_CONNECTED)
907                 goto end;
908
909         error = -EBADF;
910         session = pppol2tp_sock_to_session(sk);
911         if (session == NULL)
912                 goto end;
913
914         pls = l2tp_session_priv(session);
915         tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
916         if (tunnel == NULL) {
917                 error = -EBADF;
918                 goto end_put_sess;
919         }
920
921         inet = inet_sk(tunnel->sock);
922         if (tunnel->version == 2) {
923                 struct sockaddr_pppol2tp sp;
924                 len = sizeof(sp);
925                 memset(&sp, 0, len);
926                 sp.sa_family    = AF_PPPOX;
927                 sp.sa_protocol  = PX_PROTO_OL2TP;
928                 sp.pppol2tp.fd  = tunnel->fd;
929                 sp.pppol2tp.pid = pls->owner;
930                 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
931                 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
932                 sp.pppol2tp.s_session = session->session_id;
933                 sp.pppol2tp.d_session = session->peer_session_id;
934                 sp.pppol2tp.addr.sin_family = AF_INET;
935                 sp.pppol2tp.addr.sin_port = inet->inet_dport;
936                 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
937                 memcpy(uaddr, &sp, len);
938         } else if (tunnel->version == 3) {
939                 struct sockaddr_pppol2tpv3 sp;
940                 len = sizeof(sp);
941                 memset(&sp, 0, len);
942                 sp.sa_family    = AF_PPPOX;
943                 sp.sa_protocol  = PX_PROTO_OL2TP;
944                 sp.pppol2tp.fd  = tunnel->fd;
945                 sp.pppol2tp.pid = pls->owner;
946                 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
947                 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
948                 sp.pppol2tp.s_session = session->session_id;
949                 sp.pppol2tp.d_session = session->peer_session_id;
950                 sp.pppol2tp.addr.sin_family = AF_INET;
951                 sp.pppol2tp.addr.sin_port = inet->inet_dport;
952                 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
953                 memcpy(uaddr, &sp, len);
954         }
955
956         *usockaddr_len = len;
957
958         sock_put(pls->tunnel_sock);
959 end_put_sess:
960         sock_put(sk);
961         error = 0;
962
963 end:
964         return error;
965 }
966
967 /****************************************************************************
968  * ioctl() handlers.
969  *
970  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
971  * sockets. However, in order to control kernel tunnel features, we allow
972  * userspace to create a special "tunnel" PPPoX socket which is used for
973  * control only.  Tunnel PPPoX sockets have session_id == 0 and simply allow
974  * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
975  * calls.
976  ****************************************************************************/
977
978 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
979                                 struct l2tp_stats *stats)
980 {
981         dest->tx_packets = stats->tx_packets;
982         dest->tx_bytes = stats->tx_bytes;
983         dest->tx_errors = stats->tx_errors;
984         dest->rx_packets = stats->rx_packets;
985         dest->rx_bytes = stats->rx_bytes;
986         dest->rx_seq_discards = stats->rx_seq_discards;
987         dest->rx_oos_packets = stats->rx_oos_packets;
988         dest->rx_errors = stats->rx_errors;
989 }
990
991 /* Session ioctl helper.
992  */
993 static int pppol2tp_session_ioctl(struct l2tp_session *session,
994                                   unsigned int cmd, unsigned long arg)
995 {
996         struct ifreq ifr;
997         int err = 0;
998         struct sock *sk;
999         int val = (int) arg;
1000         struct pppol2tp_session *ps = l2tp_session_priv(session);
1001         struct l2tp_tunnel *tunnel = session->tunnel;
1002         struct pppol2tp_ioc_stats stats;
1003
1004         PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1005                "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1006                session->name, cmd, arg);
1007
1008         sk = ps->sock;
1009         sock_hold(sk);
1010
1011         switch (cmd) {
1012         case SIOCGIFMTU:
1013                 err = -ENXIO;
1014                 if (!(sk->sk_state & PPPOX_CONNECTED))
1015                         break;
1016
1017                 err = -EFAULT;
1018                 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1019                         break;
1020                 ifr.ifr_mtu = session->mtu;
1021                 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1022                         break;
1023
1024                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1025                        "%s: get mtu=%d\n", session->name, session->mtu);
1026                 err = 0;
1027                 break;
1028
1029         case SIOCSIFMTU:
1030                 err = -ENXIO;
1031                 if (!(sk->sk_state & PPPOX_CONNECTED))
1032                         break;
1033
1034                 err = -EFAULT;
1035                 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1036                         break;
1037
1038                 session->mtu = ifr.ifr_mtu;
1039
1040                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1041                        "%s: set mtu=%d\n", session->name, session->mtu);
1042                 err = 0;
1043                 break;
1044
1045         case PPPIOCGMRU:
1046                 err = -ENXIO;
1047                 if (!(sk->sk_state & PPPOX_CONNECTED))
1048                         break;
1049
1050                 err = -EFAULT;
1051                 if (put_user(session->mru, (int __user *) arg))
1052                         break;
1053
1054                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1055                        "%s: get mru=%d\n", session->name, session->mru);
1056                 err = 0;
1057                 break;
1058
1059         case PPPIOCSMRU:
1060                 err = -ENXIO;
1061                 if (!(sk->sk_state & PPPOX_CONNECTED))
1062                         break;
1063
1064                 err = -EFAULT;
1065                 if (get_user(val, (int __user *) arg))
1066                         break;
1067
1068                 session->mru = val;
1069                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1070                        "%s: set mru=%d\n", session->name, session->mru);
1071                 err = 0;
1072                 break;
1073
1074         case PPPIOCGFLAGS:
1075                 err = -EFAULT;
1076                 if (put_user(ps->flags, (int __user *) arg))
1077                         break;
1078
1079                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1080                        "%s: get flags=%d\n", session->name, ps->flags);
1081                 err = 0;
1082                 break;
1083
1084         case PPPIOCSFLAGS:
1085                 err = -EFAULT;
1086                 if (get_user(val, (int __user *) arg))
1087                         break;
1088                 ps->flags = val;
1089                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1090                        "%s: set flags=%d\n", session->name, ps->flags);
1091                 err = 0;
1092                 break;
1093
1094         case PPPIOCGL2TPSTATS:
1095                 err = -ENXIO;
1096                 if (!(sk->sk_state & PPPOX_CONNECTED))
1097                         break;
1098
1099                 memset(&stats, 0, sizeof(stats));
1100                 stats.tunnel_id = tunnel->tunnel_id;
1101                 stats.session_id = session->session_id;
1102                 pppol2tp_copy_stats(&stats, &session->stats);
1103                 if (copy_to_user((void __user *) arg, &stats,
1104                                  sizeof(stats)))
1105                         break;
1106                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1107                        "%s: get L2TP stats\n", session->name);
1108                 err = 0;
1109                 break;
1110
1111         default:
1112                 err = -ENOSYS;
1113                 break;
1114         }
1115
1116         sock_put(sk);
1117
1118         return err;
1119 }
1120
1121 /* Tunnel ioctl helper.
1122  *
1123  * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1124  * specifies a session_id, the session ioctl handler is called. This allows an
1125  * application to retrieve session stats via a tunnel socket.
1126  */
1127 static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1128                                  unsigned int cmd, unsigned long arg)
1129 {
1130         int err = 0;
1131         struct sock *sk;
1132         struct pppol2tp_ioc_stats stats;
1133
1134         PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1135                "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1136                tunnel->name, cmd, arg);
1137
1138         sk = tunnel->sock;
1139         sock_hold(sk);
1140
1141         switch (cmd) {
1142         case PPPIOCGL2TPSTATS:
1143                 err = -ENXIO;
1144                 if (!(sk->sk_state & PPPOX_CONNECTED))
1145                         break;
1146
1147                 if (copy_from_user(&stats, (void __user *) arg,
1148                                    sizeof(stats))) {
1149                         err = -EFAULT;
1150                         break;
1151                 }
1152                 if (stats.session_id != 0) {
1153                         /* resend to session ioctl handler */
1154                         struct l2tp_session *session =
1155                                 l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
1156                         if (session != NULL)
1157                                 err = pppol2tp_session_ioctl(session, cmd, arg);
1158                         else
1159                                 err = -EBADR;
1160                         break;
1161                 }
1162 #ifdef CONFIG_XFRM
1163                 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1164 #endif
1165                 pppol2tp_copy_stats(&stats, &tunnel->stats);
1166                 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1167                         err = -EFAULT;
1168                         break;
1169                 }
1170                 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1171                        "%s: get L2TP stats\n", tunnel->name);
1172                 err = 0;
1173                 break;
1174
1175         default:
1176                 err = -ENOSYS;
1177                 break;
1178         }
1179
1180         sock_put(sk);
1181
1182         return err;
1183 }
1184
1185 /* Main ioctl() handler.
1186  * Dispatch to tunnel or session helpers depending on the socket.
1187  */
1188 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1189                           unsigned long arg)
1190 {
1191         struct sock *sk = sock->sk;
1192         struct l2tp_session *session;
1193         struct l2tp_tunnel *tunnel;
1194         struct pppol2tp_session *ps;
1195         int err;
1196
1197         if (!sk)
1198                 return 0;
1199
1200         err = -EBADF;
1201         if (sock_flag(sk, SOCK_DEAD) != 0)
1202                 goto end;
1203
1204         err = -ENOTCONN;
1205         if ((sk->sk_user_data == NULL) ||
1206             (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1207                 goto end;
1208
1209         /* Get session context from the socket */
1210         err = -EBADF;
1211         session = pppol2tp_sock_to_session(sk);
1212         if (session == NULL)
1213                 goto end;
1214
1215         /* Special case: if session's session_id is zero, treat ioctl as a
1216          * tunnel ioctl
1217          */
1218         ps = l2tp_session_priv(session);
1219         if ((session->session_id == 0) &&
1220             (session->peer_session_id == 0)) {
1221                 err = -EBADF;
1222                 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1223                 if (tunnel == NULL)
1224                         goto end_put_sess;
1225
1226                 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1227                 sock_put(ps->tunnel_sock);
1228                 goto end_put_sess;
1229         }
1230
1231         err = pppol2tp_session_ioctl(session, cmd, arg);
1232
1233 end_put_sess:
1234         sock_put(sk);
1235 end:
1236         return err;
1237 }
1238
1239 /*****************************************************************************
1240  * setsockopt() / getsockopt() support.
1241  *
1242  * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1243  * sockets. In order to control kernel tunnel features, we allow userspace to
1244  * create a special "tunnel" PPPoX socket which is used for control only.
1245  * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1246  * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1247  *****************************************************************************/
1248
1249 /* Tunnel setsockopt() helper.
1250  */
1251 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1252                                       struct l2tp_tunnel *tunnel,
1253                                       int optname, int val)
1254 {
1255         int err = 0;
1256
1257         switch (optname) {
1258         case PPPOL2TP_SO_DEBUG:
1259                 tunnel->debug = val;
1260                 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1261                        "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1262                 break;
1263
1264         default:
1265                 err = -ENOPROTOOPT;
1266                 break;
1267         }
1268
1269         return err;
1270 }
1271
1272 /* Session setsockopt helper.
1273  */
1274 static int pppol2tp_session_setsockopt(struct sock *sk,
1275                                        struct l2tp_session *session,
1276                                        int optname, int val)
1277 {
1278         int err = 0;
1279         struct pppol2tp_session *ps = l2tp_session_priv(session);
1280
1281         switch (optname) {
1282         case PPPOL2TP_SO_RECVSEQ:
1283                 if ((val != 0) && (val != 1)) {
1284                         err = -EINVAL;
1285                         break;
1286                 }
1287                 session->recv_seq = val ? -1 : 0;
1288                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1289                        "%s: set recv_seq=%d\n", session->name, session->recv_seq);
1290                 break;
1291
1292         case PPPOL2TP_SO_SENDSEQ:
1293                 if ((val != 0) && (val != 1)) {
1294                         err = -EINVAL;
1295                         break;
1296                 }
1297                 session->send_seq = val ? -1 : 0;
1298                 {
1299                         struct sock *ssk      = ps->sock;
1300                         struct pppox_sock *po = pppox_sk(ssk);
1301                         po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1302                                 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1303                 }
1304                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1305                        "%s: set send_seq=%d\n", session->name, session->send_seq);
1306                 break;
1307
1308         case PPPOL2TP_SO_LNSMODE:
1309                 if ((val != 0) && (val != 1)) {
1310                         err = -EINVAL;
1311                         break;
1312                 }
1313                 session->lns_mode = val ? -1 : 0;
1314                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1315                        "%s: set lns_mode=%d\n", session->name, session->lns_mode);
1316                 break;
1317
1318         case PPPOL2TP_SO_DEBUG:
1319                 session->debug = val;
1320                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1321                        "%s: set debug=%x\n", session->name, session->debug);
1322                 break;
1323
1324         case PPPOL2TP_SO_REORDERTO:
1325                 session->reorder_timeout = msecs_to_jiffies(val);
1326                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1327                        "%s: set reorder_timeout=%d\n", session->name, session->reorder_timeout);
1328                 break;
1329
1330         default:
1331                 err = -ENOPROTOOPT;
1332                 break;
1333         }
1334
1335         return err;
1336 }
1337
1338 /* Main setsockopt() entry point.
1339  * Does API checks, then calls either the tunnel or session setsockopt
1340  * handler, according to whether the PPPoL2TP socket is a for a regular
1341  * session or the special tunnel type.
1342  */
1343 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1344                                char __user *optval, unsigned int optlen)
1345 {
1346         struct sock *sk = sock->sk;
1347         struct l2tp_session *session;
1348         struct l2tp_tunnel *tunnel;
1349         struct pppol2tp_session *ps;
1350         int val;
1351         int err;
1352
1353         if (level != SOL_PPPOL2TP)
1354                 return -EINVAL;
1355
1356         if (optlen < sizeof(int))
1357                 return -EINVAL;
1358
1359         if (get_user(val, (int __user *)optval))
1360                 return -EFAULT;
1361
1362         err = -ENOTCONN;
1363         if (sk->sk_user_data == NULL)
1364                 goto end;
1365
1366         /* Get session context from the socket */
1367         err = -EBADF;
1368         session = pppol2tp_sock_to_session(sk);
1369         if (session == NULL)
1370                 goto end;
1371
1372         /* Special case: if session_id == 0x0000, treat as operation on tunnel
1373          */
1374         ps = l2tp_session_priv(session);
1375         if ((session->session_id == 0) &&
1376             (session->peer_session_id == 0)) {
1377                 err = -EBADF;
1378                 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1379                 if (tunnel == NULL)
1380                         goto end_put_sess;
1381
1382                 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1383                 sock_put(ps->tunnel_sock);
1384         } else
1385                 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1386
1387         err = 0;
1388
1389 end_put_sess:
1390         sock_put(sk);
1391 end:
1392         return err;
1393 }
1394
1395 /* Tunnel getsockopt helper. Called with sock locked.
1396  */
1397 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1398                                       struct l2tp_tunnel *tunnel,
1399                                       int optname, int *val)
1400 {
1401         int err = 0;
1402
1403         switch (optname) {
1404         case PPPOL2TP_SO_DEBUG:
1405                 *val = tunnel->debug;
1406                 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1407                        "%s: get debug=%x\n", tunnel->name, tunnel->debug);
1408                 break;
1409
1410         default:
1411                 err = -ENOPROTOOPT;
1412                 break;
1413         }
1414
1415         return err;
1416 }
1417
1418 /* Session getsockopt helper. Called with sock locked.
1419  */
1420 static int pppol2tp_session_getsockopt(struct sock *sk,
1421                                        struct l2tp_session *session,
1422                                        int optname, int *val)
1423 {
1424         int err = 0;
1425
1426         switch (optname) {
1427         case PPPOL2TP_SO_RECVSEQ:
1428                 *val = session->recv_seq;
1429                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1430                        "%s: get recv_seq=%d\n", session->name, *val);
1431                 break;
1432
1433         case PPPOL2TP_SO_SENDSEQ:
1434                 *val = session->send_seq;
1435                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1436                        "%s: get send_seq=%d\n", session->name, *val);
1437                 break;
1438
1439         case PPPOL2TP_SO_LNSMODE:
1440                 *val = session->lns_mode;
1441                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1442                        "%s: get lns_mode=%d\n", session->name, *val);
1443                 break;
1444
1445         case PPPOL2TP_SO_DEBUG:
1446                 *val = session->debug;
1447                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1448                        "%s: get debug=%d\n", session->name, *val);
1449                 break;
1450
1451         case PPPOL2TP_SO_REORDERTO:
1452                 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1453                 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1454                        "%s: get reorder_timeout=%d\n", session->name, *val);
1455                 break;
1456
1457         default:
1458                 err = -ENOPROTOOPT;
1459         }
1460
1461         return err;
1462 }
1463
1464 /* Main getsockopt() entry point.
1465  * Does API checks, then calls either the tunnel or session getsockopt
1466  * handler, according to whether the PPPoX socket is a for a regular session
1467  * or the special tunnel type.
1468  */
1469 static int pppol2tp_getsockopt(struct socket *sock, int level,
1470                                int optname, char __user *optval, int __user *optlen)
1471 {
1472         struct sock *sk = sock->sk;
1473         struct l2tp_session *session;
1474         struct l2tp_tunnel *tunnel;
1475         int val, len;
1476         int err;
1477         struct pppol2tp_session *ps;
1478
1479         if (level != SOL_PPPOL2TP)
1480                 return -EINVAL;
1481
1482         if (get_user(len, (int __user *) optlen))
1483                 return -EFAULT;
1484
1485         len = min_t(unsigned int, len, sizeof(int));
1486
1487         if (len < 0)
1488                 return -EINVAL;
1489
1490         err = -ENOTCONN;
1491         if (sk->sk_user_data == NULL)
1492                 goto end;
1493
1494         /* Get the session context */
1495         err = -EBADF;
1496         session = pppol2tp_sock_to_session(sk);
1497         if (session == NULL)
1498                 goto end;
1499
1500         /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1501         ps = l2tp_session_priv(session);
1502         if ((session->session_id == 0) &&
1503             (session->peer_session_id == 0)) {
1504                 err = -EBADF;
1505                 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1506                 if (tunnel == NULL)
1507                         goto end_put_sess;
1508
1509                 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1510                 sock_put(ps->tunnel_sock);
1511         } else
1512                 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1513
1514         err = -EFAULT;
1515         if (put_user(len, (int __user *) optlen))
1516                 goto end_put_sess;
1517
1518         if (copy_to_user((void __user *) optval, &val, len))
1519                 goto end_put_sess;
1520
1521         err = 0;
1522
1523 end_put_sess:
1524         sock_put(sk);
1525 end:
1526         return err;
1527 }
1528
1529 /*****************************************************************************
1530  * /proc filesystem for debug
1531  * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1532  * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1533  *****************************************************************************/
1534
1535 static unsigned int pppol2tp_net_id;
1536
1537 #ifdef CONFIG_PROC_FS
1538
1539 struct pppol2tp_seq_data {
1540         struct seq_net_private p;
1541         int tunnel_idx;                 /* current tunnel */
1542         int session_idx;                /* index of session within current tunnel */
1543         struct l2tp_tunnel *tunnel;
1544         struct l2tp_session *session;   /* NULL means get next tunnel */
1545 };
1546
1547 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1548 {
1549         for (;;) {
1550                 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1551                 pd->tunnel_idx++;
1552
1553                 if (pd->tunnel == NULL)
1554                         break;
1555
1556                 /* Ignore L2TPv3 tunnels */
1557                 if (pd->tunnel->version < 3)
1558                         break;
1559         }
1560 }
1561
1562 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1563 {
1564         pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1565         pd->session_idx++;
1566
1567         if (pd->session == NULL) {
1568                 pd->session_idx = 0;
1569                 pppol2tp_next_tunnel(net, pd);
1570         }
1571 }
1572
1573 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1574 {
1575         struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1576         loff_t pos = *offs;
1577         struct net *net;
1578
1579         if (!pos)
1580                 goto out;
1581
1582         BUG_ON(m->private == NULL);
1583         pd = m->private;
1584         net = seq_file_net(m);
1585
1586         if (pd->tunnel == NULL)
1587                 pppol2tp_next_tunnel(net, pd);
1588         else
1589                 pppol2tp_next_session(net, pd);
1590
1591         /* NULL tunnel and session indicates end of list */
1592         if ((pd->tunnel == NULL) && (pd->session == NULL))
1593                 pd = NULL;
1594
1595 out:
1596         return pd;
1597 }
1598
1599 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1600 {
1601         (*pos)++;
1602         return NULL;
1603 }
1604
1605 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1606 {
1607         /* nothing to do */
1608 }
1609
1610 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1611 {
1612         struct l2tp_tunnel *tunnel = v;
1613
1614         seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1615                    tunnel->name,
1616                    (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1617                    atomic_read(&tunnel->ref_count) - 1);
1618         seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1619                    tunnel->debug,
1620                    (unsigned long long)tunnel->stats.tx_packets,
1621                    (unsigned long long)tunnel->stats.tx_bytes,
1622                    (unsigned long long)tunnel->stats.tx_errors,
1623                    (unsigned long long)tunnel->stats.rx_packets,
1624                    (unsigned long long)tunnel->stats.rx_bytes,
1625                    (unsigned long long)tunnel->stats.rx_errors);
1626 }
1627
1628 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1629 {
1630         struct l2tp_session *session = v;
1631         struct l2tp_tunnel *tunnel = session->tunnel;
1632         struct pppol2tp_session *ps = l2tp_session_priv(session);
1633         struct pppox_sock *po = pppox_sk(ps->sock);
1634         u32 ip = 0;
1635         u16 port = 0;
1636
1637         if (tunnel->sock) {
1638                 struct inet_sock *inet = inet_sk(tunnel->sock);
1639                 ip = ntohl(inet->inet_saddr);
1640                 port = ntohs(inet->inet_sport);
1641         }
1642
1643         seq_printf(m, "  SESSION '%s' %08X/%d %04X/%04X -> "
1644                    "%04X/%04X %d %c\n",
1645                    session->name, ip, port,
1646                    tunnel->tunnel_id,
1647                    session->session_id,
1648                    tunnel->peer_tunnel_id,
1649                    session->peer_session_id,
1650                    ps->sock->sk_state,
1651                    (session == ps->sock->sk_user_data) ?
1652                    'Y' : 'N');
1653         seq_printf(m, "   %d/%d/%c/%c/%s %08x %u\n",
1654                    session->mtu, session->mru,
1655                    session->recv_seq ? 'R' : '-',
1656                    session->send_seq ? 'S' : '-',
1657                    session->lns_mode ? "LNS" : "LAC",
1658                    session->debug,
1659                    jiffies_to_msecs(session->reorder_timeout));
1660         seq_printf(m, "   %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1661                    session->nr, session->ns,
1662                    (unsigned long long)session->stats.tx_packets,
1663                    (unsigned long long)session->stats.tx_bytes,
1664                    (unsigned long long)session->stats.tx_errors,
1665                    (unsigned long long)session->stats.rx_packets,
1666                    (unsigned long long)session->stats.rx_bytes,
1667                    (unsigned long long)session->stats.rx_errors);
1668
1669         if (po)
1670                 seq_printf(m, "   interface %s\n", ppp_dev_name(&po->chan));
1671 }
1672
1673 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1674 {
1675         struct pppol2tp_seq_data *pd = v;
1676
1677         /* display header on line 1 */
1678         if (v == SEQ_START_TOKEN) {
1679                 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1680                 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1681                 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1682                 seq_puts(m, "  SESSION name, addr/port src-tid/sid "
1683                          "dest-tid/sid state user-data-ok\n");
1684                 seq_puts(m, "   mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1685                 seq_puts(m, "   nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1686                 goto out;
1687         }
1688
1689         /* Show the tunnel or session context.
1690          */
1691         if (pd->session == NULL)
1692                 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1693         else
1694                 pppol2tp_seq_session_show(m, pd->session);
1695
1696 out:
1697         return 0;
1698 }
1699
1700 static const struct seq_operations pppol2tp_seq_ops = {
1701         .start          = pppol2tp_seq_start,
1702         .next           = pppol2tp_seq_next,
1703         .stop           = pppol2tp_seq_stop,
1704         .show           = pppol2tp_seq_show,
1705 };
1706
1707 /* Called when our /proc file is opened. We allocate data for use when
1708  * iterating our tunnel / session contexts and store it in the private
1709  * data of the seq_file.
1710  */
1711 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1712 {
1713         return seq_open_net(inode, file, &pppol2tp_seq_ops,
1714                             sizeof(struct pppol2tp_seq_data));
1715 }
1716
1717 static const struct file_operations pppol2tp_proc_fops = {
1718         .owner          = THIS_MODULE,
1719         .open           = pppol2tp_proc_open,
1720         .read           = seq_read,
1721         .llseek         = seq_lseek,
1722         .release        = seq_release_net,
1723 };
1724
1725 #endif /* CONFIG_PROC_FS */
1726
1727 /*****************************************************************************
1728  * Network namespace
1729  *****************************************************************************/
1730
1731 static __net_init int pppol2tp_init_net(struct net *net)
1732 {
1733         struct proc_dir_entry *pde;
1734         int err = 0;
1735
1736         pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1737         if (!pde) {
1738                 err = -ENOMEM;
1739                 goto out;
1740         }
1741
1742 out:
1743         return err;
1744 }
1745
1746 static __net_exit void pppol2tp_exit_net(struct net *net)
1747 {
1748         proc_net_remove(net, "pppol2tp");
1749 }
1750
1751 static struct pernet_operations pppol2tp_net_ops = {
1752         .init = pppol2tp_init_net,
1753         .exit = pppol2tp_exit_net,
1754         .id   = &pppol2tp_net_id,
1755 };
1756
1757 /*****************************************************************************
1758  * Init and cleanup
1759  *****************************************************************************/
1760
1761 static const struct proto_ops pppol2tp_ops = {
1762         .family         = AF_PPPOX,
1763         .owner          = THIS_MODULE,
1764         .release        = pppol2tp_release,
1765         .bind           = sock_no_bind,
1766         .connect        = pppol2tp_connect,
1767         .socketpair     = sock_no_socketpair,
1768         .accept         = sock_no_accept,
1769         .getname        = pppol2tp_getname,
1770         .poll           = datagram_poll,
1771         .listen         = sock_no_listen,
1772         .shutdown       = sock_no_shutdown,
1773         .setsockopt     = pppol2tp_setsockopt,
1774         .getsockopt     = pppol2tp_getsockopt,
1775         .sendmsg        = pppol2tp_sendmsg,
1776         .recvmsg        = pppol2tp_recvmsg,
1777         .mmap           = sock_no_mmap,
1778         .ioctl          = pppox_ioctl,
1779 };
1780
1781 static const struct pppox_proto pppol2tp_proto = {
1782         .create         = pppol2tp_create,
1783         .ioctl          = pppol2tp_ioctl,
1784         .owner          = THIS_MODULE,
1785 };
1786
1787 #ifdef CONFIG_L2TP_V3
1788
1789 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1790         .session_create = pppol2tp_session_create,
1791         .session_delete = pppol2tp_session_delete,
1792 };
1793
1794 #endif /* CONFIG_L2TP_V3 */
1795
1796 static int __init pppol2tp_init(void)
1797 {
1798         int err;
1799
1800         err = register_pernet_device(&pppol2tp_net_ops);
1801         if (err)
1802                 goto out;
1803
1804         err = proto_register(&pppol2tp_sk_proto, 0);
1805         if (err)
1806                 goto out_unregister_pppol2tp_pernet;
1807
1808         err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1809         if (err)
1810                 goto out_unregister_pppol2tp_proto;
1811
1812 #ifdef CONFIG_L2TP_V3
1813         err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1814         if (err)
1815                 goto out_unregister_pppox;
1816 #endif
1817
1818         printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
1819                PPPOL2TP_DRV_VERSION);
1820
1821 out:
1822         return err;
1823
1824 #ifdef CONFIG_L2TP_V3
1825 out_unregister_pppox:
1826         unregister_pppox_proto(PX_PROTO_OL2TP);
1827 #endif
1828 out_unregister_pppol2tp_proto:
1829         proto_unregister(&pppol2tp_sk_proto);
1830 out_unregister_pppol2tp_pernet:
1831         unregister_pernet_device(&pppol2tp_net_ops);
1832         goto out;
1833 }
1834
1835 static void __exit pppol2tp_exit(void)
1836 {
1837 #ifdef CONFIG_L2TP_V3
1838         l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1839 #endif
1840         unregister_pppox_proto(PX_PROTO_OL2TP);
1841         proto_unregister(&pppol2tp_sk_proto);
1842         unregister_pernet_device(&pppol2tp_net_ops);
1843 }
1844
1845 module_init(pppol2tp_init);
1846 module_exit(pppol2tp_exit);
1847
1848 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1849 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1850 MODULE_LICENSE("GPL");
1851 MODULE_VERSION(PPPOL2TP_DRV_VERSION);