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