1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
9 * Authors: James Chapman (jchapman@katalix.com)
11 * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
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);
362 sock_put(ps->tunnel_sock);
369 /* Transmit function called by generic PPP driver. Sends PPP frame
370 * over PPPoL2TP socket.
372 * This is almost the same as pppol2tp_sendmsg(), but rather than
373 * being called with a msghdr from userspace, it is called with a skb
376 * The supplied skb from ppp doesn't have enough headroom for the
377 * insertion of L2TP, UDP and IP headers so we need to allocate more
378 * headroom in the skb. This will create a cloned skb. But we must be
379 * careful in the error case because the caller will expect to free
380 * the skb it supplied, not our cloned skb. So we take care to always
381 * leave the original skb unfreed if we return an error.
383 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
385 static const u8 ppph[2] = { 0xff, 0x03 };
386 struct sock *sk = (struct sock *) chan->private;
388 struct l2tp_session *session;
389 struct l2tp_tunnel *tunnel;
390 struct pppol2tp_session *ps;
393 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
396 /* Get session and tunnel contexts from the socket */
397 session = pppol2tp_sock_to_session(sk);
401 ps = l2tp_session_priv(session);
402 sk_tun = ps->tunnel_sock;
405 tunnel = l2tp_sock_to_tunnel(sk_tun);
409 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
410 headroom = NET_SKB_PAD +
411 sizeof(struct iphdr) + /* IP header */
412 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
413 session->hdr_len + /* L2TP header */
414 sizeof(ppph); /* PPP header */
415 if (skb_cow_head(skb, headroom))
416 goto abort_put_sess_tun;
418 /* Setup PPP header */
419 __skb_push(skb, sizeof(ppph));
420 skb->data[0] = ppph[0];
421 skb->data[1] = ppph[1];
423 l2tp_xmit_skb(session, skb, session->hdr_len);
434 /* Free the original skb */
439 /*****************************************************************************
440 * Session (and tunnel control) socket create/destroy.
441 *****************************************************************************/
443 /* Called by l2tp_core when a session socket is being closed.
445 static void pppol2tp_session_close(struct l2tp_session *session)
447 struct pppol2tp_session *ps = l2tp_session_priv(session);
448 struct sock *sk = ps->sock;
451 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
453 if (session->session_id == 0)
459 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
460 pppox_unbind_sock(sk);
461 sk->sk_state = PPPOX_DEAD;
462 sk->sk_state_change(sk);
465 /* Purge any queued data */
466 skb_queue_purge(&sk->sk_receive_queue);
467 skb_queue_purge(&sk->sk_write_queue);
468 while ((skb = skb_dequeue(&session->reorder_q))) {
480 /* Really kill the session socket. (Called from sock_put() if
483 static void pppol2tp_session_destruct(struct sock *sk)
485 struct l2tp_session *session;
487 if (sk->sk_user_data != NULL) {
488 session = sk->sk_user_data;
492 sk->sk_user_data = NULL;
493 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
494 l2tp_session_dec_refcount(session);
501 /* Called when the PPPoX socket (session) is closed.
503 static int pppol2tp_release(struct socket *sock)
505 struct sock *sk = sock->sk;
506 struct l2tp_session *session;
514 if (sock_flag(sk, SOCK_DEAD) != 0)
517 pppox_unbind_sock(sk);
519 /* Signal the death of the socket. */
520 sk->sk_state = PPPOX_DEAD;
524 session = pppol2tp_sock_to_session(sk);
526 /* Purge any queued data */
527 skb_queue_purge(&sk->sk_receive_queue);
528 skb_queue_purge(&sk->sk_write_queue);
529 if (session != NULL) {
531 while ((skb = skb_dequeue(&session->reorder_q))) {
540 /* This will delete the session context via
541 * pppol2tp_session_destruct() if the socket's refcnt drops to
553 static struct proto pppol2tp_sk_proto = {
555 .owner = THIS_MODULE,
556 .obj_size = sizeof(struct pppox_sock),
559 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
563 rc = l2tp_udp_encap_recv(sk, skb);
567 return NET_RX_SUCCESS;
570 /* socket() handler. Initialize a new struct sock.
572 static int pppol2tp_create(struct net *net, struct socket *sock)
577 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
581 sock_init_data(sock, sk);
583 sock->state = SS_UNCONNECTED;
584 sock->ops = &pppol2tp_ops;
586 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
587 sk->sk_protocol = PX_PROTO_OL2TP;
588 sk->sk_family = PF_PPPOX;
589 sk->sk_state = PPPOX_NONE;
590 sk->sk_type = SOCK_STREAM;
591 sk->sk_destruct = pppol2tp_session_destruct;
599 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
600 static void pppol2tp_show(struct seq_file *m, void *arg)
602 struct l2tp_session *session = arg;
603 struct pppol2tp_session *ps = l2tp_session_priv(session);
606 struct pppox_sock *po = pppox_sk(ps->sock);
608 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
613 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
615 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
616 int sockaddr_len, int flags)
618 struct sock *sk = sock->sk;
619 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
620 struct pppox_sock *po = pppox_sk(sk);
621 struct l2tp_session *session = NULL;
622 struct l2tp_tunnel *tunnel;
623 struct pppol2tp_session *ps;
624 struct dst_entry *dst;
625 struct l2tp_session_cfg cfg = { 0, };
627 u32 tunnel_id, peer_tunnel_id;
628 u32 session_id, peer_session_id;
635 if (sp->sa_protocol != PX_PROTO_OL2TP)
638 /* Check for already bound sockets */
640 if (sk->sk_state & PPPOX_CONNECTED)
643 /* We don't supporting rebinding anyway */
645 if (sk->sk_user_data)
646 goto end; /* socket is already attached */
648 /* Get params from socket address. Handle L2TPv2 and L2TPv3.
649 * This is nasty because there are different sockaddr_pppol2tp
650 * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use
651 * the sockaddr size to determine which structure the caller
655 if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
656 fd = sp->pppol2tp.fd;
657 tunnel_id = sp->pppol2tp.s_tunnel;
658 peer_tunnel_id = sp->pppol2tp.d_tunnel;
659 session_id = sp->pppol2tp.s_session;
660 peer_session_id = sp->pppol2tp.d_session;
661 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
662 struct sockaddr_pppol2tpv3 *sp3 =
663 (struct sockaddr_pppol2tpv3 *) sp;
665 fd = sp3->pppol2tp.fd;
666 tunnel_id = sp3->pppol2tp.s_tunnel;
667 peer_tunnel_id = sp3->pppol2tp.d_tunnel;
668 session_id = sp3->pppol2tp.s_session;
669 peer_session_id = sp3->pppol2tp.d_session;
670 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) {
671 struct sockaddr_pppol2tpin6 *sp6 =
672 (struct sockaddr_pppol2tpin6 *) sp;
673 fd = sp6->pppol2tp.fd;
674 tunnel_id = sp6->pppol2tp.s_tunnel;
675 peer_tunnel_id = sp6->pppol2tp.d_tunnel;
676 session_id = sp6->pppol2tp.s_session;
677 peer_session_id = sp6->pppol2tp.d_session;
678 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) {
679 struct sockaddr_pppol2tpv3in6 *sp6 =
680 (struct sockaddr_pppol2tpv3in6 *) sp;
682 fd = sp6->pppol2tp.fd;
683 tunnel_id = sp6->pppol2tp.s_tunnel;
684 peer_tunnel_id = sp6->pppol2tp.d_tunnel;
685 session_id = sp6->pppol2tp.s_session;
686 peer_session_id = sp6->pppol2tp.d_session;
689 goto end; /* bad socket address */
692 /* Don't bind if tunnel_id is 0 */
697 tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id);
699 /* Special case: create tunnel context if session_id and
700 * peer_session_id is 0. Otherwise look up tunnel using supplied
703 if ((session_id == 0) && (peer_session_id == 0)) {
704 if (tunnel == NULL) {
705 struct l2tp_tunnel_cfg tcfg = {
706 .encap = L2TP_ENCAPTYPE_UDP,
709 error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
714 /* Error if we can't find the tunnel */
719 /* Error if socket is not prepped */
720 if (tunnel->sock == NULL)
724 if (tunnel->recv_payload_hook == NULL)
725 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
727 if (tunnel->peer_tunnel_id == 0)
728 tunnel->peer_tunnel_id = peer_tunnel_id;
730 /* Create session if it doesn't already exist. We handle the
731 * case where a session was previously created by the netlink
732 * interface by checking that the session doesn't already have
733 * a socket and its tunnel socket are what we expect. If any
734 * of those checks fail, return EEXIST to the caller.
736 session = l2tp_session_find(sock_net(sk), tunnel, session_id);
737 if (session == NULL) {
738 /* Default MTU must allow space for UDP/L2TP/PPP
741 cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
743 /* Allocate and initialize a new session context. */
744 session = l2tp_session_create(sizeof(struct pppol2tp_session),
746 peer_session_id, &cfg);
747 if (session == NULL) {
752 ps = l2tp_session_priv(session);
754 if (ps->sock != NULL)
757 /* consistency checks */
758 if (ps->tunnel_sock != tunnel->sock)
762 /* Associate session with its PPPoL2TP socket */
763 ps = l2tp_session_priv(session);
764 ps->owner = current->pid;
766 ps->tunnel_sock = tunnel->sock;
768 session->recv_skb = pppol2tp_recv;
769 session->session_close = pppol2tp_session_close;
770 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
771 session->show = pppol2tp_show;
774 /* We need to know each time a skb is dropped from the reorder
777 session->ref = pppol2tp_session_sock_hold;
778 session->deref = pppol2tp_session_sock_put;
780 /* If PMTU discovery was enabled, use the MTU that was discovered */
781 dst = sk_dst_get(sk);
783 u32 pmtu = dst_mtu(__sk_dst_get(sk));
785 session->mtu = session->mru = pmtu -
786 PPPOL2TP_HEADER_OVERHEAD;
790 /* Special case: if source & dest session_id == 0x0000, this
791 * socket is being created to manage the tunnel. Just set up
792 * the internal context for use by ioctl() and sockopt()
795 if ((session->session_id == 0) &&
796 (session->peer_session_id == 0)) {
801 /* The only header we need to worry about is the L2TP
802 * header. This size is different depending on whether
803 * sequence numbers are enabled for the data channel.
805 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
807 po->chan.private = sk;
808 po->chan.ops = &pppol2tp_chan_ops;
809 po->chan.mtu = session->mtu;
811 error = ppp_register_net_channel(sock_net(sk), &po->chan);
816 /* This is how we get the session context from the socket. */
817 sk->sk_user_data = session;
818 sk->sk_state = PPPOX_CONNECTED;
819 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
828 #ifdef CONFIG_L2TP_V3
830 /* Called when creating sessions via the netlink interface.
832 static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
835 struct l2tp_tunnel *tunnel;
836 struct l2tp_session *session;
837 struct pppol2tp_session *ps;
839 tunnel = l2tp_tunnel_find(net, tunnel_id);
841 /* Error if we can't find the tunnel */
846 /* Error if tunnel socket is not prepped */
847 if (tunnel->sock == NULL)
850 /* Check that this session doesn't already exist */
852 session = l2tp_session_find(net, tunnel, session_id);
856 /* Default MTU values. */
858 cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
862 /* Allocate and initialize a new session context. */
864 session = l2tp_session_create(sizeof(struct pppol2tp_session),
866 peer_session_id, cfg);
870 ps = l2tp_session_priv(session);
871 ps->tunnel_sock = tunnel->sock;
873 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
882 /* Called when deleting sessions via the netlink interface.
884 static int pppol2tp_session_delete(struct l2tp_session *session)
886 struct pppol2tp_session *ps = l2tp_session_priv(session);
888 if (ps->sock == NULL)
889 l2tp_session_dec_refcount(session);
894 #endif /* CONFIG_L2TP_V3 */
896 /* getname() support.
898 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
899 int *usockaddr_len, int peer)
903 struct l2tp_session *session;
904 struct l2tp_tunnel *tunnel;
905 struct sock *sk = sock->sk;
906 struct inet_sock *inet;
907 struct pppol2tp_session *pls;
912 if (sk->sk_state != PPPOX_CONNECTED)
916 session = pppol2tp_sock_to_session(sk);
920 pls = l2tp_session_priv(session);
921 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
922 if (tunnel == NULL) {
927 inet = inet_sk(tunnel->sock);
928 if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
929 struct sockaddr_pppol2tp sp;
932 sp.sa_family = AF_PPPOX;
933 sp.sa_protocol = PX_PROTO_OL2TP;
934 sp.pppol2tp.fd = tunnel->fd;
935 sp.pppol2tp.pid = pls->owner;
936 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
937 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
938 sp.pppol2tp.s_session = session->session_id;
939 sp.pppol2tp.d_session = session->peer_session_id;
940 sp.pppol2tp.addr.sin_family = AF_INET;
941 sp.pppol2tp.addr.sin_port = inet->inet_dport;
942 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
943 memcpy(uaddr, &sp, len);
944 #if IS_ENABLED(CONFIG_IPV6)
945 } else if ((tunnel->version == 2) &&
946 (tunnel->sock->sk_family == AF_INET6)) {
947 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
948 struct sockaddr_pppol2tpin6 sp;
951 sp.sa_family = AF_PPPOX;
952 sp.sa_protocol = PX_PROTO_OL2TP;
953 sp.pppol2tp.fd = tunnel->fd;
954 sp.pppol2tp.pid = pls->owner;
955 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
956 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
957 sp.pppol2tp.s_session = session->session_id;
958 sp.pppol2tp.d_session = session->peer_session_id;
959 sp.pppol2tp.addr.sin6_family = AF_INET6;
960 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
961 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr,
963 memcpy(uaddr, &sp, len);
964 } else if ((tunnel->version == 3) &&
965 (tunnel->sock->sk_family == AF_INET6)) {
966 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
967 struct sockaddr_pppol2tpv3in6 sp;
970 sp.sa_family = AF_PPPOX;
971 sp.sa_protocol = PX_PROTO_OL2TP;
972 sp.pppol2tp.fd = tunnel->fd;
973 sp.pppol2tp.pid = pls->owner;
974 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
975 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
976 sp.pppol2tp.s_session = session->session_id;
977 sp.pppol2tp.d_session = session->peer_session_id;
978 sp.pppol2tp.addr.sin6_family = AF_INET6;
979 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
980 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr,
982 memcpy(uaddr, &sp, len);
984 } else if (tunnel->version == 3) {
985 struct sockaddr_pppol2tpv3 sp;
988 sp.sa_family = AF_PPPOX;
989 sp.sa_protocol = PX_PROTO_OL2TP;
990 sp.pppol2tp.fd = tunnel->fd;
991 sp.pppol2tp.pid = pls->owner;
992 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
993 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
994 sp.pppol2tp.s_session = session->session_id;
995 sp.pppol2tp.d_session = session->peer_session_id;
996 sp.pppol2tp.addr.sin_family = AF_INET;
997 sp.pppol2tp.addr.sin_port = inet->inet_dport;
998 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
999 memcpy(uaddr, &sp, len);
1002 *usockaddr_len = len;
1004 sock_put(pls->tunnel_sock);
1013 /****************************************************************************
1016 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1017 * sockets. However, in order to control kernel tunnel features, we allow
1018 * userspace to create a special "tunnel" PPPoX socket which is used for
1019 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1020 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1022 ****************************************************************************/
1024 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1025 struct l2tp_stats *stats)
1027 dest->tx_packets = stats->tx_packets;
1028 dest->tx_bytes = stats->tx_bytes;
1029 dest->tx_errors = stats->tx_errors;
1030 dest->rx_packets = stats->rx_packets;
1031 dest->rx_bytes = stats->rx_bytes;
1032 dest->rx_seq_discards = stats->rx_seq_discards;
1033 dest->rx_oos_packets = stats->rx_oos_packets;
1034 dest->rx_errors = stats->rx_errors;
1037 /* Session ioctl helper.
1039 static int pppol2tp_session_ioctl(struct l2tp_session *session,
1040 unsigned int cmd, unsigned long arg)
1045 int val = (int) arg;
1046 struct pppol2tp_session *ps = l2tp_session_priv(session);
1047 struct l2tp_tunnel *tunnel = session->tunnel;
1048 struct pppol2tp_ioc_stats stats;
1050 l2tp_dbg(session, PPPOL2TP_MSG_CONTROL,
1051 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1052 session->name, cmd, arg);
1060 if (!(sk->sk_state & PPPOX_CONNECTED))
1064 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1066 ifr.ifr_mtu = session->mtu;
1067 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1070 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mtu=%d\n",
1071 session->name, session->mtu);
1077 if (!(sk->sk_state & PPPOX_CONNECTED))
1081 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1084 session->mtu = ifr.ifr_mtu;
1086 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mtu=%d\n",
1087 session->name, session->mtu);
1093 if (!(sk->sk_state & PPPOX_CONNECTED))
1097 if (put_user(session->mru, (int __user *) arg))
1100 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mru=%d\n",
1101 session->name, session->mru);
1107 if (!(sk->sk_state & PPPOX_CONNECTED))
1111 if (get_user(val, (int __user *) arg))
1115 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mru=%d\n",
1116 session->name, session->mru);
1122 if (put_user(ps->flags, (int __user *) arg))
1125 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get flags=%d\n",
1126 session->name, ps->flags);
1132 if (get_user(val, (int __user *) arg))
1135 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set flags=%d\n",
1136 session->name, ps->flags);
1140 case PPPIOCGL2TPSTATS:
1142 if (!(sk->sk_state & PPPOX_CONNECTED))
1145 memset(&stats, 0, sizeof(stats));
1146 stats.tunnel_id = tunnel->tunnel_id;
1147 stats.session_id = session->session_id;
1148 pppol2tp_copy_stats(&stats, &session->stats);
1149 if (copy_to_user((void __user *) arg, &stats,
1152 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1167 /* Tunnel ioctl helper.
1169 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1170 * specifies a session_id, the session ioctl handler is called. This allows an
1171 * application to retrieve session stats via a tunnel socket.
1173 static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1174 unsigned int cmd, unsigned long arg)
1178 struct pppol2tp_ioc_stats stats;
1180 l2tp_dbg(tunnel, PPPOL2TP_MSG_CONTROL,
1181 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1182 tunnel->name, cmd, arg);
1188 case PPPIOCGL2TPSTATS:
1190 if (!(sk->sk_state & PPPOX_CONNECTED))
1193 if (copy_from_user(&stats, (void __user *) arg,
1198 if (stats.session_id != 0) {
1199 /* resend to session ioctl handler */
1200 struct l2tp_session *session =
1201 l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
1202 if (session != NULL)
1203 err = pppol2tp_session_ioctl(session, cmd, arg);
1209 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1211 pppol2tp_copy_stats(&stats, &tunnel->stats);
1212 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1216 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1231 /* Main ioctl() handler.
1232 * Dispatch to tunnel or session helpers depending on the socket.
1234 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1237 struct sock *sk = sock->sk;
1238 struct l2tp_session *session;
1239 struct l2tp_tunnel *tunnel;
1240 struct pppol2tp_session *ps;
1247 if (sock_flag(sk, SOCK_DEAD) != 0)
1251 if ((sk->sk_user_data == NULL) ||
1252 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1255 /* Get session context from the socket */
1257 session = pppol2tp_sock_to_session(sk);
1258 if (session == NULL)
1261 /* Special case: if session's session_id is zero, treat ioctl as a
1264 ps = l2tp_session_priv(session);
1265 if ((session->session_id == 0) &&
1266 (session->peer_session_id == 0)) {
1268 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1272 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1273 sock_put(ps->tunnel_sock);
1277 err = pppol2tp_session_ioctl(session, cmd, arg);
1285 /*****************************************************************************
1286 * setsockopt() / getsockopt() support.
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 *****************************************************************************/
1295 /* Tunnel setsockopt() helper.
1297 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1298 struct l2tp_tunnel *tunnel,
1299 int optname, int val)
1304 case PPPOL2TP_SO_DEBUG:
1305 tunnel->debug = val;
1306 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
1307 tunnel->name, tunnel->debug);
1318 /* Session setsockopt helper.
1320 static int pppol2tp_session_setsockopt(struct sock *sk,
1321 struct l2tp_session *session,
1322 int optname, int val)
1325 struct pppol2tp_session *ps = l2tp_session_priv(session);
1328 case PPPOL2TP_SO_RECVSEQ:
1329 if ((val != 0) && (val != 1)) {
1333 session->recv_seq = val ? -1 : 0;
1334 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1335 "%s: set recv_seq=%d\n",
1336 session->name, session->recv_seq);
1339 case PPPOL2TP_SO_SENDSEQ:
1340 if ((val != 0) && (val != 1)) {
1344 session->send_seq = val ? -1 : 0;
1346 struct sock *ssk = ps->sock;
1347 struct pppox_sock *po = pppox_sk(ssk);
1348 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1349 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1351 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1352 "%s: set send_seq=%d\n",
1353 session->name, session->send_seq);
1356 case PPPOL2TP_SO_LNSMODE:
1357 if ((val != 0) && (val != 1)) {
1361 session->lns_mode = val ? -1 : 0;
1362 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1363 "%s: set lns_mode=%d\n",
1364 session->name, session->lns_mode);
1367 case PPPOL2TP_SO_DEBUG:
1368 session->debug = val;
1369 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
1370 session->name, session->debug);
1373 case PPPOL2TP_SO_REORDERTO:
1374 session->reorder_timeout = msecs_to_jiffies(val);
1375 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1376 "%s: set reorder_timeout=%d\n",
1377 session->name, session->reorder_timeout);
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.
1393 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1394 char __user *optval, unsigned int optlen)
1396 struct sock *sk = sock->sk;
1397 struct l2tp_session *session;
1398 struct l2tp_tunnel *tunnel;
1399 struct pppol2tp_session *ps;
1403 if (level != SOL_PPPOL2TP)
1404 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1406 if (optlen < sizeof(int))
1409 if (get_user(val, (int __user *)optval))
1413 if (sk->sk_user_data == NULL)
1416 /* Get session context from the socket */
1418 session = pppol2tp_sock_to_session(sk);
1419 if (session == NULL)
1422 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1424 ps = l2tp_session_priv(session);
1425 if ((session->session_id == 0) &&
1426 (session->peer_session_id == 0)) {
1428 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1432 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1433 sock_put(ps->tunnel_sock);
1435 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1445 /* Tunnel getsockopt helper. Called with sock locked.
1447 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1448 struct l2tp_tunnel *tunnel,
1449 int optname, int *val)
1454 case PPPOL2TP_SO_DEBUG:
1455 *val = tunnel->debug;
1456 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get debug=%x\n",
1457 tunnel->name, tunnel->debug);
1468 /* Session getsockopt helper. Called with sock locked.
1470 static int pppol2tp_session_getsockopt(struct sock *sk,
1471 struct l2tp_session *session,
1472 int optname, int *val)
1477 case PPPOL2TP_SO_RECVSEQ:
1478 *val = session->recv_seq;
1479 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1480 "%s: get recv_seq=%d\n", session->name, *val);
1483 case PPPOL2TP_SO_SENDSEQ:
1484 *val = session->send_seq;
1485 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1486 "%s: get send_seq=%d\n", session->name, *val);
1489 case PPPOL2TP_SO_LNSMODE:
1490 *val = session->lns_mode;
1491 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1492 "%s: get lns_mode=%d\n", session->name, *val);
1495 case PPPOL2TP_SO_DEBUG:
1496 *val = session->debug;
1497 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get debug=%d\n",
1498 session->name, *val);
1501 case PPPOL2TP_SO_REORDERTO:
1502 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1503 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1504 "%s: get reorder_timeout=%d\n", session->name, *val);
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.
1519 static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1520 char __user *optval, int __user *optlen)
1522 struct sock *sk = sock->sk;
1523 struct l2tp_session *session;
1524 struct l2tp_tunnel *tunnel;
1527 struct pppol2tp_session *ps;
1529 if (level != SOL_PPPOL2TP)
1530 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1532 if (get_user(len, optlen))
1535 len = min_t(unsigned int, len, sizeof(int));
1541 if (sk->sk_user_data == NULL)
1544 /* Get the session context */
1546 session = pppol2tp_sock_to_session(sk);
1547 if (session == NULL)
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)) {
1555 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1559 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1560 sock_put(ps->tunnel_sock);
1562 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1565 if (put_user(len, optlen))
1568 if (copy_to_user((void __user *) optval, &val, len))
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 *****************************************************************************/
1585 static unsigned int pppol2tp_net_id;
1587 #ifdef CONFIG_PROC_FS
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 */
1597 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1600 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1603 if (pd->tunnel == NULL)
1606 /* Ignore L2TPv3 tunnels */
1607 if (pd->tunnel->version < 3)
1612 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1614 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1617 if (pd->session == NULL) {
1618 pd->session_idx = 0;
1619 pppol2tp_next_tunnel(net, pd);
1623 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1625 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1632 BUG_ON(m->private == NULL);
1634 net = seq_file_net(m);
1636 if (pd->tunnel == NULL)
1637 pppol2tp_next_tunnel(net, pd);
1639 pppol2tp_next_session(net, pd);
1641 /* NULL tunnel and session indicates end of list */
1642 if ((pd->tunnel == NULL) && (pd->session == NULL))
1649 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1655 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1660 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1662 struct l2tp_tunnel *tunnel = v;
1664 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1666 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1667 atomic_read(&tunnel->ref_count) - 1);
1668 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1670 (unsigned long long)tunnel->stats.tx_packets,
1671 (unsigned long long)tunnel->stats.tx_bytes,
1672 (unsigned long long)tunnel->stats.tx_errors,
1673 (unsigned long long)tunnel->stats.rx_packets,
1674 (unsigned long long)tunnel->stats.rx_bytes,
1675 (unsigned long long)tunnel->stats.rx_errors);
1678 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1680 struct l2tp_session *session = v;
1681 struct l2tp_tunnel *tunnel = session->tunnel;
1682 struct pppol2tp_session *ps = l2tp_session_priv(session);
1683 struct pppox_sock *po = pppox_sk(ps->sock);
1688 struct inet_sock *inet = inet_sk(tunnel->sock);
1689 ip = ntohl(inet->inet_saddr);
1690 port = ntohs(inet->inet_sport);
1693 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1694 "%04X/%04X %d %c\n",
1695 session->name, ip, port,
1697 session->session_id,
1698 tunnel->peer_tunnel_id,
1699 session->peer_session_id,
1701 (session == ps->sock->sk_user_data) ?
1703 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1704 session->mtu, session->mru,
1705 session->recv_seq ? 'R' : '-',
1706 session->send_seq ? 'S' : '-',
1707 session->lns_mode ? "LNS" : "LAC",
1709 jiffies_to_msecs(session->reorder_timeout));
1710 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1711 session->nr, session->ns,
1712 (unsigned long long)session->stats.tx_packets,
1713 (unsigned long long)session->stats.tx_bytes,
1714 (unsigned long long)session->stats.tx_errors,
1715 (unsigned long long)session->stats.rx_packets,
1716 (unsigned long long)session->stats.rx_bytes,
1717 (unsigned long long)session->stats.rx_errors);
1720 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
1723 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1725 struct pppol2tp_seq_data *pd = v;
1727 /* display header on line 1 */
1728 if (v == SEQ_START_TOKEN) {
1729 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1730 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1731 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1732 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1733 "dest-tid/sid state user-data-ok\n");
1734 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1735 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1739 /* Show the tunnel or session context.
1741 if (pd->session == NULL)
1742 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1744 pppol2tp_seq_session_show(m, pd->session);
1750 static const struct seq_operations pppol2tp_seq_ops = {
1751 .start = pppol2tp_seq_start,
1752 .next = pppol2tp_seq_next,
1753 .stop = pppol2tp_seq_stop,
1754 .show = pppol2tp_seq_show,
1757 /* Called when our /proc file is opened. We allocate data for use when
1758 * iterating our tunnel / session contexts and store it in the private
1759 * data of the seq_file.
1761 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1763 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1764 sizeof(struct pppol2tp_seq_data));
1767 static const struct file_operations pppol2tp_proc_fops = {
1768 .owner = THIS_MODULE,
1769 .open = pppol2tp_proc_open,
1771 .llseek = seq_lseek,
1772 .release = seq_release_net,
1775 #endif /* CONFIG_PROC_FS */
1777 /*****************************************************************************
1779 *****************************************************************************/
1781 static __net_init int pppol2tp_init_net(struct net *net)
1783 struct proc_dir_entry *pde;
1786 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1796 static __net_exit void pppol2tp_exit_net(struct net *net)
1798 proc_net_remove(net, "pppol2tp");
1801 static struct pernet_operations pppol2tp_net_ops = {
1802 .init = pppol2tp_init_net,
1803 .exit = pppol2tp_exit_net,
1804 .id = &pppol2tp_net_id,
1807 /*****************************************************************************
1809 *****************************************************************************/
1811 static const struct proto_ops pppol2tp_ops = {
1813 .owner = THIS_MODULE,
1814 .release = pppol2tp_release,
1815 .bind = sock_no_bind,
1816 .connect = pppol2tp_connect,
1817 .socketpair = sock_no_socketpair,
1818 .accept = sock_no_accept,
1819 .getname = pppol2tp_getname,
1820 .poll = datagram_poll,
1821 .listen = sock_no_listen,
1822 .shutdown = sock_no_shutdown,
1823 .setsockopt = pppol2tp_setsockopt,
1824 .getsockopt = pppol2tp_getsockopt,
1825 .sendmsg = pppol2tp_sendmsg,
1826 .recvmsg = pppol2tp_recvmsg,
1827 .mmap = sock_no_mmap,
1828 .ioctl = pppox_ioctl,
1831 static const struct pppox_proto pppol2tp_proto = {
1832 .create = pppol2tp_create,
1833 .ioctl = pppol2tp_ioctl
1836 #ifdef CONFIG_L2TP_V3
1838 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1839 .session_create = pppol2tp_session_create,
1840 .session_delete = pppol2tp_session_delete,
1843 #endif /* CONFIG_L2TP_V3 */
1845 static int __init pppol2tp_init(void)
1849 err = register_pernet_device(&pppol2tp_net_ops);
1853 err = proto_register(&pppol2tp_sk_proto, 0);
1855 goto out_unregister_pppol2tp_pernet;
1857 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1859 goto out_unregister_pppol2tp_proto;
1861 #ifdef CONFIG_L2TP_V3
1862 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1864 goto out_unregister_pppox;
1867 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
1872 #ifdef CONFIG_L2TP_V3
1873 out_unregister_pppox:
1874 unregister_pppox_proto(PX_PROTO_OL2TP);
1876 out_unregister_pppol2tp_proto:
1877 proto_unregister(&pppol2tp_sk_proto);
1878 out_unregister_pppol2tp_pernet:
1879 unregister_pernet_device(&pppol2tp_net_ops);
1883 static void __exit pppol2tp_exit(void)
1885 #ifdef CONFIG_L2TP_V3
1886 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1888 unregister_pppox_proto(PX_PROTO_OL2TP);
1889 proto_unregister(&pppol2tp_sk_proto);
1890 unregister_pernet_device(&pppol2tp_net_ops);
1893 module_init(pppol2tp_init);
1894 module_exit(pppol2tp_exit);
1896 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1897 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1898 MODULE_LICENSE("GPL");
1899 MODULE_VERSION(PPPOL2TP_DRV_VERSION);
1900 MODULE_ALIAS("pppox-proto-" __stringify(PX_PROTO_OL2TP));