1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
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.
21 /* This driver handles only L2TP data frames; control frames are handled by a
22 * userspace application.
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.
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.
33 * Here's example userspace code to create a socket for sending/receiving data
34 * over an L2TP session:-
36 * struct sockaddr_pppol2tp sax;
40 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
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;
53 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
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.
60 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
62 #include <linux/module.h>
63 #include <linux/string.h>
64 #include <linux/list.h>
65 #include <linux/uaccess.h>
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>
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>
81 #include <linux/udp.h>
82 #include <linux/if_pppox.h>
83 #include <linux/if_pppol2tp.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>
101 #include <asm/byteorder.h>
102 #include <linux/atomic.h>
104 #include "l2tp_core.h"
106 #define PPPOL2TP_DRV_VERSION "V2.0"
108 /* Space for UDP, L2TP and PPP headers */
109 #define PPPOL2TP_HEADER_OVERHEAD 40
111 /* Number of bytes to build transmit L2TP headers.
112 * Unfortunately the size is different depending on whether sequence numbers
115 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
116 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
118 /* Private data of each session. This data lives at the end of struct
119 * l2tp_session, referenced via session->priv[].
121 struct pppol2tp_session {
122 int owner; /* pid that opened the socket */
124 struct sock *sock; /* Pointer to the session
126 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
128 int flags; /* accessed by PPPIOCGFLAGS.
132 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
134 static const struct ppp_channel_ops pppol2tp_chan_ops = {
135 .start_xmit = pppol2tp_xmit,
138 static const struct proto_ops pppol2tp_ops;
140 /* Helpers to obtain tunnel/session contexts from sockets.
142 static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
144 struct l2tp_session *session;
150 session = (struct l2tp_session *)(sk->sk_user_data);
151 if (session == NULL) {
156 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
162 /*****************************************************************************
163 * Receive data handling
164 *****************************************************************************/
166 static int pppol2tp_recv_payload_hook(struct sk_buff *skb)
168 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
169 * don't send the PPP header (PPP header compression enabled), but
170 * other clients can include the header. So we cope with both cases
171 * here. The PPP header is always FF03 when using L2TP.
173 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
174 * the field may be unaligned.
176 if (!pskb_may_pull(skb, 2))
179 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
185 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
187 static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
188 struct msghdr *msg, size_t len,
193 struct sock *sk = sock->sk;
196 if (sk->sk_state & PPPOX_BOUND)
199 msg->msg_namelen = 0;
202 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
203 flags & MSG_DONTWAIT, &err);
209 else if (len < skb->len)
210 msg->msg_flags |= MSG_TRUNC;
212 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
213 if (likely(err == 0))
221 static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
223 struct pppol2tp_session *ps = l2tp_session_priv(session);
224 struct sock *sk = NULL;
226 /* If the socket is bound, send it in to PPP's input queue. Otherwise
227 * queue it on the session socket.
233 if (sk->sk_state & PPPOX_BOUND) {
234 struct pppox_sock *po;
235 l2tp_dbg(session, PPPOL2TP_MSG_DATA,
236 "%s: recv %d byte data frame, passing to ppp\n",
237 session->name, data_len);
239 /* We need to forget all info related to the L2TP packet
240 * gathered in the skb as we are going to reuse the same
241 * skb for the inner packet.
243 * - reset xfrm (IPSec) information as it applies to
244 * the outer L2TP packet and not to the inner one
245 * - release the dst to force a route lookup on the inner
246 * IP packet since skb->dst currently points to the dst
248 * - reset netfilter information as it doesn't apply
249 * to the inner packet either
256 ppp_input(&po->chan, skb);
258 l2tp_info(session, PPPOL2TP_MSG_DATA, "%s: socket not bound\n",
261 /* Not bound. Nothing we can do, so discard. */
262 session->stats.rx_errors++;
269 l2tp_info(session, PPPOL2TP_MSG_DATA, "%s: no socket\n", session->name);
273 static void pppol2tp_session_sock_hold(struct l2tp_session *session)
275 struct pppol2tp_session *ps = l2tp_session_priv(session);
281 static void pppol2tp_session_sock_put(struct l2tp_session *session)
283 struct pppol2tp_session *ps = l2tp_session_priv(session);
289 /************************************************************************
291 ***********************************************************************/
293 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
294 * when a user application does a sendmsg() on the session socket. L2TP and
295 * PPP headers must be inserted into the user's data.
297 static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
300 static const unsigned char ppph[2] = { 0xff, 0x03 };
301 struct sock *sk = sock->sk;
304 struct l2tp_session *session;
305 struct l2tp_tunnel *tunnel;
306 struct pppol2tp_session *ps;
310 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
313 /* Get session and tunnel contexts */
315 session = pppol2tp_sock_to_session(sk);
319 ps = l2tp_session_priv(session);
320 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
324 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
326 /* Allocate a socket buffer */
328 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
329 uhlen + session->hdr_len +
330 sizeof(ppph) + total_len,
333 goto error_put_sess_tun;
335 /* Reserve space for headers. */
336 skb_reserve(skb, NET_SKB_PAD);
337 skb_reset_network_header(skb);
338 skb_reserve(skb, sizeof(struct iphdr));
339 skb_reset_transport_header(skb);
340 skb_reserve(skb, uhlen);
343 skb->data[0] = ppph[0];
344 skb->data[1] = ppph[1];
347 /* Copy user data into skb */
348 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
351 goto error_put_sess_tun;
353 skb_put(skb, total_len);
355 l2tp_xmit_skb(session, skb, session->hdr_len);
357 sock_put(ps->tunnel_sock);
363 sock_put(ps->tunnel_sock);
370 /* Transmit function called by generic PPP driver. Sends PPP frame
371 * over PPPoL2TP socket.
373 * This is almost the same as pppol2tp_sendmsg(), but rather than
374 * being called with a msghdr from userspace, it is called with a skb
377 * The supplied skb from ppp doesn't have enough headroom for the
378 * insertion of L2TP, UDP and IP headers so we need to allocate more
379 * headroom in the skb. This will create a cloned skb. But we must be
380 * careful in the error case because the caller will expect to free
381 * the skb it supplied, not our cloned skb. So we take care to always
382 * leave the original skb unfreed if we return an error.
384 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
386 static const u8 ppph[2] = { 0xff, 0x03 };
387 struct sock *sk = (struct sock *) chan->private;
389 struct l2tp_session *session;
390 struct l2tp_tunnel *tunnel;
391 struct pppol2tp_session *ps;
394 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
397 /* Get session and tunnel contexts from the socket */
398 session = pppol2tp_sock_to_session(sk);
402 ps = l2tp_session_priv(session);
403 sk_tun = ps->tunnel_sock;
406 tunnel = l2tp_sock_to_tunnel(sk_tun);
410 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
411 headroom = NET_SKB_PAD +
412 sizeof(struct iphdr) + /* IP header */
413 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
414 session->hdr_len + /* L2TP header */
415 sizeof(ppph); /* PPP header */
416 if (skb_cow_head(skb, headroom))
417 goto abort_put_sess_tun;
419 /* Setup PPP header */
420 __skb_push(skb, sizeof(ppph));
421 skb->data[0] = ppph[0];
422 skb->data[1] = ppph[1];
424 l2tp_xmit_skb(session, skb, session->hdr_len);
435 /* Free the original skb */
440 /*****************************************************************************
441 * Session (and tunnel control) socket create/destroy.
442 *****************************************************************************/
444 /* Called by l2tp_core when a session socket is being closed.
446 static void pppol2tp_session_close(struct l2tp_session *session)
448 struct pppol2tp_session *ps = l2tp_session_priv(session);
449 struct sock *sk = ps->sock;
452 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
454 if (session->session_id == 0)
460 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
461 pppox_unbind_sock(sk);
462 sk->sk_state = PPPOX_DEAD;
463 sk->sk_state_change(sk);
466 /* Purge any queued data */
467 skb_queue_purge(&sk->sk_receive_queue);
468 skb_queue_purge(&sk->sk_write_queue);
469 while ((skb = skb_dequeue(&session->reorder_q))) {
481 /* Really kill the session socket. (Called from sock_put() if
484 static void pppol2tp_session_destruct(struct sock *sk)
486 struct l2tp_session *session;
488 if (sk->sk_user_data != NULL) {
489 session = sk->sk_user_data;
493 sk->sk_user_data = NULL;
494 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
495 l2tp_session_dec_refcount(session);
502 /* Called when the PPPoX socket (session) is closed.
504 static int pppol2tp_release(struct socket *sock)
506 struct sock *sk = sock->sk;
507 struct l2tp_session *session;
515 if (sock_flag(sk, SOCK_DEAD) != 0)
518 pppox_unbind_sock(sk);
520 /* Signal the death of the socket. */
521 sk->sk_state = PPPOX_DEAD;
525 session = pppol2tp_sock_to_session(sk);
527 /* Purge any queued data */
528 skb_queue_purge(&sk->sk_receive_queue);
529 skb_queue_purge(&sk->sk_write_queue);
530 if (session != NULL) {
532 while ((skb = skb_dequeue(&session->reorder_q))) {
541 /* This will delete the session context via
542 * pppol2tp_session_destruct() if the socket's refcnt drops to
554 static struct proto pppol2tp_sk_proto = {
556 .owner = THIS_MODULE,
557 .obj_size = sizeof(struct pppox_sock),
560 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
564 rc = l2tp_udp_encap_recv(sk, skb);
568 return NET_RX_SUCCESS;
571 /* socket() handler. Initialize a new struct sock.
573 static int pppol2tp_create(struct net *net, struct socket *sock)
578 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
582 sock_init_data(sock, sk);
584 sock->state = SS_UNCONNECTED;
585 sock->ops = &pppol2tp_ops;
587 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
588 sk->sk_protocol = PX_PROTO_OL2TP;
589 sk->sk_family = PF_PPPOX;
590 sk->sk_state = PPPOX_NONE;
591 sk->sk_type = SOCK_STREAM;
592 sk->sk_destruct = pppol2tp_session_destruct;
600 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
601 static void pppol2tp_show(struct seq_file *m, void *arg)
603 struct l2tp_session *session = arg;
604 struct pppol2tp_session *ps = l2tp_session_priv(session);
607 struct pppox_sock *po = pppox_sk(ps->sock);
609 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
614 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
616 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
617 int sockaddr_len, int flags)
619 struct sock *sk = sock->sk;
620 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
621 struct pppox_sock *po = pppox_sk(sk);
622 struct l2tp_session *session = NULL;
623 struct l2tp_tunnel *tunnel;
624 struct pppol2tp_session *ps;
625 struct dst_entry *dst;
626 struct l2tp_session_cfg cfg = { 0, };
628 u32 tunnel_id, peer_tunnel_id;
629 u32 session_id, peer_session_id;
636 if (sp->sa_protocol != PX_PROTO_OL2TP)
639 /* Check for already bound sockets */
641 if (sk->sk_state & PPPOX_CONNECTED)
644 /* We don't supporting rebinding anyway */
646 if (sk->sk_user_data)
647 goto end; /* socket is already attached */
649 /* Get params from socket address. Handle L2TPv2 and L2TPv3.
650 * This is nasty because there are different sockaddr_pppol2tp
651 * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use
652 * the sockaddr size to determine which structure the caller
656 if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
657 fd = sp->pppol2tp.fd;
658 tunnel_id = sp->pppol2tp.s_tunnel;
659 peer_tunnel_id = sp->pppol2tp.d_tunnel;
660 session_id = sp->pppol2tp.s_session;
661 peer_session_id = sp->pppol2tp.d_session;
662 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
663 struct sockaddr_pppol2tpv3 *sp3 =
664 (struct sockaddr_pppol2tpv3 *) sp;
666 fd = sp3->pppol2tp.fd;
667 tunnel_id = sp3->pppol2tp.s_tunnel;
668 peer_tunnel_id = sp3->pppol2tp.d_tunnel;
669 session_id = sp3->pppol2tp.s_session;
670 peer_session_id = sp3->pppol2tp.d_session;
671 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) {
672 struct sockaddr_pppol2tpin6 *sp6 =
673 (struct sockaddr_pppol2tpin6 *) sp;
674 fd = sp6->pppol2tp.fd;
675 tunnel_id = sp6->pppol2tp.s_tunnel;
676 peer_tunnel_id = sp6->pppol2tp.d_tunnel;
677 session_id = sp6->pppol2tp.s_session;
678 peer_session_id = sp6->pppol2tp.d_session;
679 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) {
680 struct sockaddr_pppol2tpv3in6 *sp6 =
681 (struct sockaddr_pppol2tpv3in6 *) sp;
683 fd = sp6->pppol2tp.fd;
684 tunnel_id = sp6->pppol2tp.s_tunnel;
685 peer_tunnel_id = sp6->pppol2tp.d_tunnel;
686 session_id = sp6->pppol2tp.s_session;
687 peer_session_id = sp6->pppol2tp.d_session;
690 goto end; /* bad socket address */
693 /* Don't bind if tunnel_id is 0 */
698 tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id);
700 /* Special case: create tunnel context if session_id and
701 * peer_session_id is 0. Otherwise look up tunnel using supplied
704 if ((session_id == 0) && (peer_session_id == 0)) {
705 if (tunnel == NULL) {
706 struct l2tp_tunnel_cfg tcfg = {
707 .encap = L2TP_ENCAPTYPE_UDP,
710 error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
715 /* Error if we can't find the tunnel */
720 /* Error if socket is not prepped */
721 if (tunnel->sock == NULL)
725 if (tunnel->recv_payload_hook == NULL)
726 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
728 if (tunnel->peer_tunnel_id == 0)
729 tunnel->peer_tunnel_id = peer_tunnel_id;
731 /* Create session if it doesn't already exist. We handle the
732 * case where a session was previously created by the netlink
733 * interface by checking that the session doesn't already have
734 * a socket and its tunnel socket are what we expect. If any
735 * of those checks fail, return EEXIST to the caller.
737 session = l2tp_session_find(sock_net(sk), tunnel, session_id);
738 if (session == NULL) {
739 /* Default MTU must allow space for UDP/L2TP/PPP
742 cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
744 /* Allocate and initialize a new session context. */
745 session = l2tp_session_create(sizeof(struct pppol2tp_session),
747 peer_session_id, &cfg);
748 if (session == NULL) {
753 ps = l2tp_session_priv(session);
755 if (ps->sock != NULL)
758 /* consistency checks */
759 if (ps->tunnel_sock != tunnel->sock)
763 /* Associate session with its PPPoL2TP socket */
764 ps = l2tp_session_priv(session);
765 ps->owner = current->pid;
767 ps->tunnel_sock = tunnel->sock;
769 session->recv_skb = pppol2tp_recv;
770 session->session_close = pppol2tp_session_close;
771 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
772 session->show = pppol2tp_show;
775 /* We need to know each time a skb is dropped from the reorder
778 session->ref = pppol2tp_session_sock_hold;
779 session->deref = pppol2tp_session_sock_put;
781 /* If PMTU discovery was enabled, use the MTU that was discovered */
782 dst = sk_dst_get(sk);
784 u32 pmtu = dst_mtu(__sk_dst_get(sk));
786 session->mtu = session->mru = pmtu -
787 PPPOL2TP_HEADER_OVERHEAD;
791 /* Special case: if source & dest session_id == 0x0000, this
792 * socket is being created to manage the tunnel. Just set up
793 * the internal context for use by ioctl() and sockopt()
796 if ((session->session_id == 0) &&
797 (session->peer_session_id == 0)) {
802 /* The only header we need to worry about is the L2TP
803 * header. This size is different depending on whether
804 * sequence numbers are enabled for the data channel.
806 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
808 po->chan.private = sk;
809 po->chan.ops = &pppol2tp_chan_ops;
810 po->chan.mtu = session->mtu;
812 error = ppp_register_net_channel(sock_net(sk), &po->chan);
817 /* This is how we get the session context from the socket. */
818 sk->sk_user_data = session;
819 sk->sk_state = PPPOX_CONNECTED;
820 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
829 #ifdef CONFIG_L2TP_V3
831 /* Called when creating sessions via the netlink interface.
833 static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
836 struct l2tp_tunnel *tunnel;
837 struct l2tp_session *session;
838 struct pppol2tp_session *ps;
840 tunnel = l2tp_tunnel_find(net, tunnel_id);
842 /* Error if we can't find the tunnel */
847 /* Error if tunnel socket is not prepped */
848 if (tunnel->sock == NULL)
851 /* Check that this session doesn't already exist */
853 session = l2tp_session_find(net, tunnel, session_id);
857 /* Default MTU values. */
859 cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
863 /* Allocate and initialize a new session context. */
865 session = l2tp_session_create(sizeof(struct pppol2tp_session),
867 peer_session_id, cfg);
871 ps = l2tp_session_priv(session);
872 ps->tunnel_sock = tunnel->sock;
874 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
883 /* Called when deleting sessions via the netlink interface.
885 static int pppol2tp_session_delete(struct l2tp_session *session)
887 struct pppol2tp_session *ps = l2tp_session_priv(session);
889 if (ps->sock == NULL)
890 l2tp_session_dec_refcount(session);
895 #endif /* CONFIG_L2TP_V3 */
897 /* getname() support.
899 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
900 int *usockaddr_len, int peer)
904 struct l2tp_session *session;
905 struct l2tp_tunnel *tunnel;
906 struct sock *sk = sock->sk;
907 struct inet_sock *inet;
908 struct pppol2tp_session *pls;
913 if (sk->sk_state != PPPOX_CONNECTED)
917 session = pppol2tp_sock_to_session(sk);
921 pls = l2tp_session_priv(session);
922 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
923 if (tunnel == NULL) {
928 inet = inet_sk(tunnel->sock);
929 if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
930 struct sockaddr_pppol2tp sp;
933 sp.sa_family = AF_PPPOX;
934 sp.sa_protocol = PX_PROTO_OL2TP;
935 sp.pppol2tp.fd = tunnel->fd;
936 sp.pppol2tp.pid = pls->owner;
937 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
938 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
939 sp.pppol2tp.s_session = session->session_id;
940 sp.pppol2tp.d_session = session->peer_session_id;
941 sp.pppol2tp.addr.sin_family = AF_INET;
942 sp.pppol2tp.addr.sin_port = inet->inet_dport;
943 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
944 memcpy(uaddr, &sp, len);
945 #if IS_ENABLED(CONFIG_IPV6)
946 } else if ((tunnel->version == 2) &&
947 (tunnel->sock->sk_family == AF_INET6)) {
948 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
949 struct sockaddr_pppol2tpin6 sp;
952 sp.sa_family = AF_PPPOX;
953 sp.sa_protocol = PX_PROTO_OL2TP;
954 sp.pppol2tp.fd = tunnel->fd;
955 sp.pppol2tp.pid = pls->owner;
956 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
957 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
958 sp.pppol2tp.s_session = session->session_id;
959 sp.pppol2tp.d_session = session->peer_session_id;
960 sp.pppol2tp.addr.sin6_family = AF_INET6;
961 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
962 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr,
964 memcpy(uaddr, &sp, len);
965 } else if ((tunnel->version == 3) &&
966 (tunnel->sock->sk_family == AF_INET6)) {
967 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
968 struct sockaddr_pppol2tpv3in6 sp;
971 sp.sa_family = AF_PPPOX;
972 sp.sa_protocol = PX_PROTO_OL2TP;
973 sp.pppol2tp.fd = tunnel->fd;
974 sp.pppol2tp.pid = pls->owner;
975 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
976 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
977 sp.pppol2tp.s_session = session->session_id;
978 sp.pppol2tp.d_session = session->peer_session_id;
979 sp.pppol2tp.addr.sin6_family = AF_INET6;
980 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
981 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr,
983 memcpy(uaddr, &sp, len);
985 } else if (tunnel->version == 3) {
986 struct sockaddr_pppol2tpv3 sp;
989 sp.sa_family = AF_PPPOX;
990 sp.sa_protocol = PX_PROTO_OL2TP;
991 sp.pppol2tp.fd = tunnel->fd;
992 sp.pppol2tp.pid = pls->owner;
993 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
994 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
995 sp.pppol2tp.s_session = session->session_id;
996 sp.pppol2tp.d_session = session->peer_session_id;
997 sp.pppol2tp.addr.sin_family = AF_INET;
998 sp.pppol2tp.addr.sin_port = inet->inet_dport;
999 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
1000 memcpy(uaddr, &sp, len);
1003 *usockaddr_len = len;
1005 sock_put(pls->tunnel_sock);
1014 /****************************************************************************
1017 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1018 * sockets. However, in order to control kernel tunnel features, we allow
1019 * userspace to create a special "tunnel" PPPoX socket which is used for
1020 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1021 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1023 ****************************************************************************/
1025 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1026 struct l2tp_stats *stats)
1028 dest->tx_packets = stats->tx_packets;
1029 dest->tx_bytes = stats->tx_bytes;
1030 dest->tx_errors = stats->tx_errors;
1031 dest->rx_packets = stats->rx_packets;
1032 dest->rx_bytes = stats->rx_bytes;
1033 dest->rx_seq_discards = stats->rx_seq_discards;
1034 dest->rx_oos_packets = stats->rx_oos_packets;
1035 dest->rx_errors = stats->rx_errors;
1038 /* Session ioctl helper.
1040 static int pppol2tp_session_ioctl(struct l2tp_session *session,
1041 unsigned int cmd, unsigned long arg)
1046 int val = (int) arg;
1047 struct pppol2tp_session *ps = l2tp_session_priv(session);
1048 struct l2tp_tunnel *tunnel = session->tunnel;
1049 struct pppol2tp_ioc_stats stats;
1051 l2tp_dbg(session, PPPOL2TP_MSG_CONTROL,
1052 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1053 session->name, cmd, arg);
1061 if (!(sk->sk_state & PPPOX_CONNECTED))
1065 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1067 ifr.ifr_mtu = session->mtu;
1068 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1071 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mtu=%d\n",
1072 session->name, session->mtu);
1078 if (!(sk->sk_state & PPPOX_CONNECTED))
1082 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1085 session->mtu = ifr.ifr_mtu;
1087 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mtu=%d\n",
1088 session->name, session->mtu);
1094 if (!(sk->sk_state & PPPOX_CONNECTED))
1098 if (put_user(session->mru, (int __user *) arg))
1101 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mru=%d\n",
1102 session->name, session->mru);
1108 if (!(sk->sk_state & PPPOX_CONNECTED))
1112 if (get_user(val, (int __user *) arg))
1116 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mru=%d\n",
1117 session->name, session->mru);
1123 if (put_user(ps->flags, (int __user *) arg))
1126 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get flags=%d\n",
1127 session->name, ps->flags);
1133 if (get_user(val, (int __user *) arg))
1136 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set flags=%d\n",
1137 session->name, ps->flags);
1141 case PPPIOCGL2TPSTATS:
1143 if (!(sk->sk_state & PPPOX_CONNECTED))
1146 memset(&stats, 0, sizeof(stats));
1147 stats.tunnel_id = tunnel->tunnel_id;
1148 stats.session_id = session->session_id;
1149 pppol2tp_copy_stats(&stats, &session->stats);
1150 if (copy_to_user((void __user *) arg, &stats,
1153 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1168 /* Tunnel ioctl helper.
1170 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1171 * specifies a session_id, the session ioctl handler is called. This allows an
1172 * application to retrieve session stats via a tunnel socket.
1174 static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1175 unsigned int cmd, unsigned long arg)
1179 struct pppol2tp_ioc_stats stats;
1181 l2tp_dbg(tunnel, PPPOL2TP_MSG_CONTROL,
1182 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1183 tunnel->name, cmd, arg);
1189 case PPPIOCGL2TPSTATS:
1191 if (!(sk->sk_state & PPPOX_CONNECTED))
1194 if (copy_from_user(&stats, (void __user *) arg,
1199 if (stats.session_id != 0) {
1200 /* resend to session ioctl handler */
1201 struct l2tp_session *session =
1202 l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
1203 if (session != NULL)
1204 err = pppol2tp_session_ioctl(session, cmd, arg);
1210 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1212 pppol2tp_copy_stats(&stats, &tunnel->stats);
1213 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1217 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1232 /* Main ioctl() handler.
1233 * Dispatch to tunnel or session helpers depending on the socket.
1235 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1238 struct sock *sk = sock->sk;
1239 struct l2tp_session *session;
1240 struct l2tp_tunnel *tunnel;
1241 struct pppol2tp_session *ps;
1248 if (sock_flag(sk, SOCK_DEAD) != 0)
1252 if ((sk->sk_user_data == NULL) ||
1253 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1256 /* Get session context from the socket */
1258 session = pppol2tp_sock_to_session(sk);
1259 if (session == NULL)
1262 /* Special case: if session's session_id is zero, treat ioctl as a
1265 ps = l2tp_session_priv(session);
1266 if ((session->session_id == 0) &&
1267 (session->peer_session_id == 0)) {
1269 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1273 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1274 sock_put(ps->tunnel_sock);
1278 err = pppol2tp_session_ioctl(session, cmd, arg);
1286 /*****************************************************************************
1287 * setsockopt() / getsockopt() support.
1289 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1290 * sockets. In order to control kernel tunnel features, we allow userspace to
1291 * create a special "tunnel" PPPoX socket which is used for control only.
1292 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1293 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1294 *****************************************************************************/
1296 /* Tunnel setsockopt() helper.
1298 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1299 struct l2tp_tunnel *tunnel,
1300 int optname, int val)
1305 case PPPOL2TP_SO_DEBUG:
1306 tunnel->debug = val;
1307 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
1308 tunnel->name, tunnel->debug);
1319 /* Session setsockopt helper.
1321 static int pppol2tp_session_setsockopt(struct sock *sk,
1322 struct l2tp_session *session,
1323 int optname, int val)
1326 struct pppol2tp_session *ps = l2tp_session_priv(session);
1329 case PPPOL2TP_SO_RECVSEQ:
1330 if ((val != 0) && (val != 1)) {
1334 session->recv_seq = val ? -1 : 0;
1335 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1336 "%s: set recv_seq=%d\n",
1337 session->name, session->recv_seq);
1340 case PPPOL2TP_SO_SENDSEQ:
1341 if ((val != 0) && (val != 1)) {
1345 session->send_seq = val ? -1 : 0;
1347 struct sock *ssk = ps->sock;
1348 struct pppox_sock *po = pppox_sk(ssk);
1349 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1350 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1352 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1353 "%s: set send_seq=%d\n",
1354 session->name, session->send_seq);
1357 case PPPOL2TP_SO_LNSMODE:
1358 if ((val != 0) && (val != 1)) {
1362 session->lns_mode = val ? -1 : 0;
1363 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1364 "%s: set lns_mode=%d\n",
1365 session->name, session->lns_mode);
1368 case PPPOL2TP_SO_DEBUG:
1369 session->debug = val;
1370 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
1371 session->name, session->debug);
1374 case PPPOL2TP_SO_REORDERTO:
1375 session->reorder_timeout = msecs_to_jiffies(val);
1376 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1377 "%s: set reorder_timeout=%d\n",
1378 session->name, session->reorder_timeout);
1389 /* Main setsockopt() entry point.
1390 * Does API checks, then calls either the tunnel or session setsockopt
1391 * handler, according to whether the PPPoL2TP socket is a for a regular
1392 * session or the special tunnel type.
1394 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1395 char __user *optval, unsigned int optlen)
1397 struct sock *sk = sock->sk;
1398 struct l2tp_session *session;
1399 struct l2tp_tunnel *tunnel;
1400 struct pppol2tp_session *ps;
1404 if (level != SOL_PPPOL2TP)
1405 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1407 if (optlen < sizeof(int))
1410 if (get_user(val, (int __user *)optval))
1414 if (sk->sk_user_data == NULL)
1417 /* Get session context from the socket */
1419 session = pppol2tp_sock_to_session(sk);
1420 if (session == NULL)
1423 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1425 ps = l2tp_session_priv(session);
1426 if ((session->session_id == 0) &&
1427 (session->peer_session_id == 0)) {
1429 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1433 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1434 sock_put(ps->tunnel_sock);
1436 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1446 /* Tunnel getsockopt helper. Called with sock locked.
1448 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1449 struct l2tp_tunnel *tunnel,
1450 int optname, int *val)
1455 case PPPOL2TP_SO_DEBUG:
1456 *val = tunnel->debug;
1457 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get debug=%x\n",
1458 tunnel->name, tunnel->debug);
1469 /* Session getsockopt helper. Called with sock locked.
1471 static int pppol2tp_session_getsockopt(struct sock *sk,
1472 struct l2tp_session *session,
1473 int optname, int *val)
1478 case PPPOL2TP_SO_RECVSEQ:
1479 *val = session->recv_seq;
1480 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1481 "%s: get recv_seq=%d\n", session->name, *val);
1484 case PPPOL2TP_SO_SENDSEQ:
1485 *val = session->send_seq;
1486 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1487 "%s: get send_seq=%d\n", session->name, *val);
1490 case PPPOL2TP_SO_LNSMODE:
1491 *val = session->lns_mode;
1492 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1493 "%s: get lns_mode=%d\n", session->name, *val);
1496 case PPPOL2TP_SO_DEBUG:
1497 *val = session->debug;
1498 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get debug=%d\n",
1499 session->name, *val);
1502 case PPPOL2TP_SO_REORDERTO:
1503 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1504 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1505 "%s: get reorder_timeout=%d\n", session->name, *val);
1515 /* Main getsockopt() entry point.
1516 * Does API checks, then calls either the tunnel or session getsockopt
1517 * handler, according to whether the PPPoX socket is a for a regular session
1518 * or the special tunnel type.
1520 static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1521 char __user *optval, int __user *optlen)
1523 struct sock *sk = sock->sk;
1524 struct l2tp_session *session;
1525 struct l2tp_tunnel *tunnel;
1528 struct pppol2tp_session *ps;
1530 if (level != SOL_PPPOL2TP)
1531 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1533 if (get_user(len, optlen))
1536 len = min_t(unsigned int, len, sizeof(int));
1542 if (sk->sk_user_data == NULL)
1545 /* Get the session context */
1547 session = pppol2tp_sock_to_session(sk);
1548 if (session == NULL)
1551 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1552 ps = l2tp_session_priv(session);
1553 if ((session->session_id == 0) &&
1554 (session->peer_session_id == 0)) {
1556 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1560 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1561 sock_put(ps->tunnel_sock);
1563 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1566 if (put_user(len, optlen))
1569 if (copy_to_user((void __user *) optval, &val, len))
1580 /*****************************************************************************
1581 * /proc filesystem for debug
1582 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1583 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1584 *****************************************************************************/
1586 static unsigned int pppol2tp_net_id;
1588 #ifdef CONFIG_PROC_FS
1590 struct pppol2tp_seq_data {
1591 struct seq_net_private p;
1592 int tunnel_idx; /* current tunnel */
1593 int session_idx; /* index of session within current tunnel */
1594 struct l2tp_tunnel *tunnel;
1595 struct l2tp_session *session; /* NULL means get next tunnel */
1598 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1601 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1604 if (pd->tunnel == NULL)
1607 /* Ignore L2TPv3 tunnels */
1608 if (pd->tunnel->version < 3)
1613 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1615 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1618 if (pd->session == NULL) {
1619 pd->session_idx = 0;
1620 pppol2tp_next_tunnel(net, pd);
1624 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1626 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1633 BUG_ON(m->private == NULL);
1635 net = seq_file_net(m);
1637 if (pd->tunnel == NULL)
1638 pppol2tp_next_tunnel(net, pd);
1640 pppol2tp_next_session(net, pd);
1642 /* NULL tunnel and session indicates end of list */
1643 if ((pd->tunnel == NULL) && (pd->session == NULL))
1650 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1656 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1661 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1663 struct l2tp_tunnel *tunnel = v;
1665 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1667 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1668 atomic_read(&tunnel->ref_count) - 1);
1669 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1671 (unsigned long long)tunnel->stats.tx_packets,
1672 (unsigned long long)tunnel->stats.tx_bytes,
1673 (unsigned long long)tunnel->stats.tx_errors,
1674 (unsigned long long)tunnel->stats.rx_packets,
1675 (unsigned long long)tunnel->stats.rx_bytes,
1676 (unsigned long long)tunnel->stats.rx_errors);
1679 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1681 struct l2tp_session *session = v;
1682 struct l2tp_tunnel *tunnel = session->tunnel;
1683 struct pppol2tp_session *ps = l2tp_session_priv(session);
1684 struct pppox_sock *po = pppox_sk(ps->sock);
1689 struct inet_sock *inet = inet_sk(tunnel->sock);
1690 ip = ntohl(inet->inet_saddr);
1691 port = ntohs(inet->inet_sport);
1694 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1695 "%04X/%04X %d %c\n",
1696 session->name, ip, port,
1698 session->session_id,
1699 tunnel->peer_tunnel_id,
1700 session->peer_session_id,
1702 (session == ps->sock->sk_user_data) ?
1704 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1705 session->mtu, session->mru,
1706 session->recv_seq ? 'R' : '-',
1707 session->send_seq ? 'S' : '-',
1708 session->lns_mode ? "LNS" : "LAC",
1710 jiffies_to_msecs(session->reorder_timeout));
1711 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1712 session->nr, session->ns,
1713 (unsigned long long)session->stats.tx_packets,
1714 (unsigned long long)session->stats.tx_bytes,
1715 (unsigned long long)session->stats.tx_errors,
1716 (unsigned long long)session->stats.rx_packets,
1717 (unsigned long long)session->stats.rx_bytes,
1718 (unsigned long long)session->stats.rx_errors);
1721 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
1724 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1726 struct pppol2tp_seq_data *pd = v;
1728 /* display header on line 1 */
1729 if (v == SEQ_START_TOKEN) {
1730 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1731 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1732 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1733 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1734 "dest-tid/sid state user-data-ok\n");
1735 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1736 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1740 /* Show the tunnel or session context.
1742 if (pd->session == NULL)
1743 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1745 pppol2tp_seq_session_show(m, pd->session);
1751 static const struct seq_operations pppol2tp_seq_ops = {
1752 .start = pppol2tp_seq_start,
1753 .next = pppol2tp_seq_next,
1754 .stop = pppol2tp_seq_stop,
1755 .show = pppol2tp_seq_show,
1758 /* Called when our /proc file is opened. We allocate data for use when
1759 * iterating our tunnel / session contexts and store it in the private
1760 * data of the seq_file.
1762 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1764 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1765 sizeof(struct pppol2tp_seq_data));
1768 static const struct file_operations pppol2tp_proc_fops = {
1769 .owner = THIS_MODULE,
1770 .open = pppol2tp_proc_open,
1772 .llseek = seq_lseek,
1773 .release = seq_release_net,
1776 #endif /* CONFIG_PROC_FS */
1778 /*****************************************************************************
1780 *****************************************************************************/
1782 static __net_init int pppol2tp_init_net(struct net *net)
1784 struct proc_dir_entry *pde;
1787 pde = proc_create("pppol2tp", S_IRUGO, net->proc_net,
1788 &pppol2tp_proc_fops);
1798 static __net_exit void pppol2tp_exit_net(struct net *net)
1800 remove_proc_entry("pppol2tp", net->proc_net);
1803 static struct pernet_operations pppol2tp_net_ops = {
1804 .init = pppol2tp_init_net,
1805 .exit = pppol2tp_exit_net,
1806 .id = &pppol2tp_net_id,
1809 /*****************************************************************************
1811 *****************************************************************************/
1813 static const struct proto_ops pppol2tp_ops = {
1815 .owner = THIS_MODULE,
1816 .release = pppol2tp_release,
1817 .bind = sock_no_bind,
1818 .connect = pppol2tp_connect,
1819 .socketpair = sock_no_socketpair,
1820 .accept = sock_no_accept,
1821 .getname = pppol2tp_getname,
1822 .poll = datagram_poll,
1823 .listen = sock_no_listen,
1824 .shutdown = sock_no_shutdown,
1825 .setsockopt = pppol2tp_setsockopt,
1826 .getsockopt = pppol2tp_getsockopt,
1827 .sendmsg = pppol2tp_sendmsg,
1828 .recvmsg = pppol2tp_recvmsg,
1829 .mmap = sock_no_mmap,
1830 .ioctl = pppox_ioctl,
1833 static const struct pppox_proto pppol2tp_proto = {
1834 .create = pppol2tp_create,
1835 .ioctl = pppol2tp_ioctl
1838 #ifdef CONFIG_L2TP_V3
1840 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1841 .session_create = pppol2tp_session_create,
1842 .session_delete = pppol2tp_session_delete,
1845 #endif /* CONFIG_L2TP_V3 */
1847 static int __init pppol2tp_init(void)
1851 err = register_pernet_device(&pppol2tp_net_ops);
1855 err = proto_register(&pppol2tp_sk_proto, 0);
1857 goto out_unregister_pppol2tp_pernet;
1859 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1861 goto out_unregister_pppol2tp_proto;
1863 #ifdef CONFIG_L2TP_V3
1864 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1866 goto out_unregister_pppox;
1869 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1874 #ifdef CONFIG_L2TP_V3
1875 out_unregister_pppox:
1876 unregister_pppox_proto(PX_PROTO_OL2TP);
1878 out_unregister_pppol2tp_proto:
1879 proto_unregister(&pppol2tp_sk_proto);
1880 out_unregister_pppol2tp_pernet:
1881 unregister_pernet_device(&pppol2tp_net_ops);
1885 static void __exit pppol2tp_exit(void)
1887 #ifdef CONFIG_L2TP_V3
1888 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1890 unregister_pppox_proto(PX_PROTO_OL2TP);
1891 proto_unregister(&pppol2tp_sk_proto);
1892 unregister_pernet_device(&pppol2tp_net_ops);
1895 module_init(pppol2tp_init);
1896 module_exit(pppol2tp_exit);
1899 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1900 MODULE_LICENSE("GPL");
1901 MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1902 MODULE_ALIAS("pppox-proto-" __stringify(PX_PROTO_OL2TP));