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
;
395 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
))
398 /* Get session and tunnel contexts from the socket */
399 session
= pppol2tp_sock_to_session(sk
);
403 ps
= l2tp_session_priv(session
);
404 sk_tun
= ps
->tunnel_sock
;
407 tunnel
= l2tp_sock_to_tunnel(sk_tun
);
411 old_headroom
= skb_headroom(skb
);
412 uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
413 headroom
= NET_SKB_PAD
+
414 sizeof(struct iphdr
) + /* IP header */
415 uhlen
+ /* UDP header (if L2TP_ENCAPTYPE_UDP) */
416 session
->hdr_len
+ /* L2TP header */
417 sizeof(ppph
); /* PPP header */
418 if (skb_cow_head(skb
, headroom
))
419 goto abort_put_sess_tun
;
421 new_headroom
= skb_headroom(skb
);
422 skb
->truesize
+= new_headroom
- old_headroom
;
424 /* Setup PPP header */
425 __skb_push(skb
, sizeof(ppph
));
426 skb
->data
[0] = ppph
[0];
427 skb
->data
[1] = ppph
[1];
429 l2tp_xmit_skb(session
, skb
, session
->hdr_len
);
440 /* Free the original skb */
445 /*****************************************************************************
446 * Session (and tunnel control) socket create/destroy.
447 *****************************************************************************/
449 /* Called by l2tp_core when a session socket is being closed.
451 static void pppol2tp_session_close(struct l2tp_session
*session
)
453 struct pppol2tp_session
*ps
= l2tp_session_priv(session
);
454 struct sock
*sk
= ps
->sock
;
457 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
459 if (session
->session_id
== 0)
465 if (sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
)) {
466 pppox_unbind_sock(sk
);
467 sk
->sk_state
= PPPOX_DEAD
;
468 sk
->sk_state_change(sk
);
471 /* Purge any queued data */
472 skb_queue_purge(&sk
->sk_receive_queue
);
473 skb_queue_purge(&sk
->sk_write_queue
);
474 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
486 /* Really kill the session socket. (Called from sock_put() if
489 static void pppol2tp_session_destruct(struct sock
*sk
)
491 struct l2tp_session
*session
;
493 if (sk
->sk_user_data
!= NULL
) {
494 session
= sk
->sk_user_data
;
498 sk
->sk_user_data
= NULL
;
499 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
500 l2tp_session_dec_refcount(session
);
507 /* Called when the PPPoX socket (session) is closed.
509 static int pppol2tp_release(struct socket
*sock
)
511 struct sock
*sk
= sock
->sk
;
512 struct l2tp_session
*session
;
520 if (sock_flag(sk
, SOCK_DEAD
) != 0)
523 pppox_unbind_sock(sk
);
525 /* Signal the death of the socket. */
526 sk
->sk_state
= PPPOX_DEAD
;
530 session
= pppol2tp_sock_to_session(sk
);
532 /* Purge any queued data */
533 skb_queue_purge(&sk
->sk_receive_queue
);
534 skb_queue_purge(&sk
->sk_write_queue
);
535 if (session
!= NULL
) {
537 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
546 /* This will delete the session context via
547 * pppol2tp_session_destruct() if the socket's refcnt drops to
559 static struct proto pppol2tp_sk_proto
= {
561 .owner
= THIS_MODULE
,
562 .obj_size
= sizeof(struct pppox_sock
),
565 static int pppol2tp_backlog_recv(struct sock
*sk
, struct sk_buff
*skb
)
569 rc
= l2tp_udp_encap_recv(sk
, skb
);
573 return NET_RX_SUCCESS
;
576 /* socket() handler. Initialize a new struct sock.
578 static int pppol2tp_create(struct net
*net
, struct socket
*sock
)
583 sk
= sk_alloc(net
, PF_PPPOX
, GFP_KERNEL
, &pppol2tp_sk_proto
);
587 sock_init_data(sock
, sk
);
589 sock
->state
= SS_UNCONNECTED
;
590 sock
->ops
= &pppol2tp_ops
;
592 sk
->sk_backlog_rcv
= pppol2tp_backlog_recv
;
593 sk
->sk_protocol
= PX_PROTO_OL2TP
;
594 sk
->sk_family
= PF_PPPOX
;
595 sk
->sk_state
= PPPOX_NONE
;
596 sk
->sk_type
= SOCK_STREAM
;
597 sk
->sk_destruct
= pppol2tp_session_destruct
;
605 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
606 static void pppol2tp_show(struct seq_file
*m
, void *arg
)
608 struct l2tp_session
*session
= arg
;
609 struct pppol2tp_session
*ps
= l2tp_session_priv(session
);
612 struct pppox_sock
*po
= pppox_sk(ps
->sock
);
614 seq_printf(m
, " interface %s\n", ppp_dev_name(&po
->chan
));
619 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
621 static int pppol2tp_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
622 int sockaddr_len
, int flags
)
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 dst_entry
*dst
;
631 struct l2tp_session_cfg cfg
= { 0, };
633 u32 tunnel_id
, peer_tunnel_id
;
634 u32 session_id
, peer_session_id
;
641 if (sp
->sa_protocol
!= PX_PROTO_OL2TP
)
644 /* Check for already bound sockets */
646 if (sk
->sk_state
& PPPOX_CONNECTED
)
649 /* We don't supporting rebinding anyway */
651 if (sk
->sk_user_data
)
652 goto end
; /* socket is already attached */
654 /* Get params from socket address. Handle L2TPv2 and L2TPv3.
655 * This is nasty because there are different sockaddr_pppol2tp
656 * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use
657 * the sockaddr size to determine which structure the caller
661 if (sockaddr_len
== sizeof(struct sockaddr_pppol2tp
)) {
662 fd
= sp
->pppol2tp
.fd
;
663 tunnel_id
= sp
->pppol2tp
.s_tunnel
;
664 peer_tunnel_id
= sp
->pppol2tp
.d_tunnel
;
665 session_id
= sp
->pppol2tp
.s_session
;
666 peer_session_id
= sp
->pppol2tp
.d_session
;
667 } else if (sockaddr_len
== sizeof(struct sockaddr_pppol2tpv3
)) {
668 struct sockaddr_pppol2tpv3
*sp3
=
669 (struct sockaddr_pppol2tpv3
*) sp
;
671 fd
= sp3
->pppol2tp
.fd
;
672 tunnel_id
= sp3
->pppol2tp
.s_tunnel
;
673 peer_tunnel_id
= sp3
->pppol2tp
.d_tunnel
;
674 session_id
= sp3
->pppol2tp
.s_session
;
675 peer_session_id
= sp3
->pppol2tp
.d_session
;
676 } else if (sockaddr_len
== sizeof(struct sockaddr_pppol2tpin6
)) {
677 struct sockaddr_pppol2tpin6
*sp6
=
678 (struct sockaddr_pppol2tpin6
*) sp
;
679 fd
= sp6
->pppol2tp
.fd
;
680 tunnel_id
= sp6
->pppol2tp
.s_tunnel
;
681 peer_tunnel_id
= sp6
->pppol2tp
.d_tunnel
;
682 session_id
= sp6
->pppol2tp
.s_session
;
683 peer_session_id
= sp6
->pppol2tp
.d_session
;
684 } else if (sockaddr_len
== sizeof(struct sockaddr_pppol2tpv3in6
)) {
685 struct sockaddr_pppol2tpv3in6
*sp6
=
686 (struct sockaddr_pppol2tpv3in6
*) sp
;
688 fd
= sp6
->pppol2tp
.fd
;
689 tunnel_id
= sp6
->pppol2tp
.s_tunnel
;
690 peer_tunnel_id
= sp6
->pppol2tp
.d_tunnel
;
691 session_id
= sp6
->pppol2tp
.s_session
;
692 peer_session_id
= sp6
->pppol2tp
.d_session
;
695 goto end
; /* bad socket address */
698 /* Don't bind if tunnel_id is 0 */
703 tunnel
= l2tp_tunnel_find(sock_net(sk
), tunnel_id
);
705 /* Special case: create tunnel context if session_id and
706 * peer_session_id is 0. Otherwise look up tunnel using supplied
709 if ((session_id
== 0) && (peer_session_id
== 0)) {
710 if (tunnel
== NULL
) {
711 struct l2tp_tunnel_cfg tcfg
= {
712 .encap
= L2TP_ENCAPTYPE_UDP
,
715 error
= l2tp_tunnel_create(sock_net(sk
), fd
, ver
, tunnel_id
, peer_tunnel_id
, &tcfg
, &tunnel
);
720 /* Error if we can't find the tunnel */
725 /* Error if socket is not prepped */
726 if (tunnel
->sock
== NULL
)
730 if (tunnel
->recv_payload_hook
== NULL
)
731 tunnel
->recv_payload_hook
= pppol2tp_recv_payload_hook
;
733 if (tunnel
->peer_tunnel_id
== 0)
734 tunnel
->peer_tunnel_id
= peer_tunnel_id
;
736 /* Create session if it doesn't already exist. We handle the
737 * case where a session was previously created by the netlink
738 * interface by checking that the session doesn't already have
739 * a socket and its tunnel socket are what we expect. If any
740 * of those checks fail, return EEXIST to the caller.
742 session
= l2tp_session_find(sock_net(sk
), tunnel
, session_id
);
743 if (session
== NULL
) {
744 /* Default MTU must allow space for UDP/L2TP/PPP
747 cfg
.mtu
= cfg
.mru
= 1500 - PPPOL2TP_HEADER_OVERHEAD
;
749 /* Allocate and initialize a new session context. */
750 session
= l2tp_session_create(sizeof(struct pppol2tp_session
),
752 peer_session_id
, &cfg
);
753 if (session
== NULL
) {
758 ps
= l2tp_session_priv(session
);
760 if (ps
->sock
!= NULL
)
763 /* consistency checks */
764 if (ps
->tunnel_sock
!= tunnel
->sock
)
768 /* Associate session with its PPPoL2TP socket */
769 ps
= l2tp_session_priv(session
);
770 ps
->owner
= current
->pid
;
772 ps
->tunnel_sock
= tunnel
->sock
;
774 session
->recv_skb
= pppol2tp_recv
;
775 session
->session_close
= pppol2tp_session_close
;
776 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
777 session
->show
= pppol2tp_show
;
780 /* We need to know each time a skb is dropped from the reorder
783 session
->ref
= pppol2tp_session_sock_hold
;
784 session
->deref
= pppol2tp_session_sock_put
;
786 /* If PMTU discovery was enabled, use the MTU that was discovered */
787 dst
= sk_dst_get(sk
);
789 u32 pmtu
= dst_mtu(__sk_dst_get(sk
));
791 session
->mtu
= session
->mru
= pmtu
-
792 PPPOL2TP_HEADER_OVERHEAD
;
796 /* Special case: if source & dest session_id == 0x0000, this
797 * socket is being created to manage the tunnel. Just set up
798 * the internal context for use by ioctl() and sockopt()
801 if ((session
->session_id
== 0) &&
802 (session
->peer_session_id
== 0)) {
807 /* The only header we need to worry about is the L2TP
808 * header. This size is different depending on whether
809 * sequence numbers are enabled for the data channel.
811 po
->chan
.hdrlen
= PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
813 po
->chan
.private = sk
;
814 po
->chan
.ops
= &pppol2tp_chan_ops
;
815 po
->chan
.mtu
= session
->mtu
;
817 error
= ppp_register_net_channel(sock_net(sk
), &po
->chan
);
822 /* This is how we get the session context from the socket. */
823 sk
->sk_user_data
= session
;
824 sk
->sk_state
= PPPOX_CONNECTED
;
825 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: created\n",
834 #ifdef CONFIG_L2TP_V3
836 /* Called when creating sessions via the netlink interface.
838 static int pppol2tp_session_create(struct net
*net
, u32 tunnel_id
, u32 session_id
, u32 peer_session_id
, struct l2tp_session_cfg
*cfg
)
841 struct l2tp_tunnel
*tunnel
;
842 struct l2tp_session
*session
;
843 struct pppol2tp_session
*ps
;
845 tunnel
= l2tp_tunnel_find(net
, tunnel_id
);
847 /* Error if we can't find the tunnel */
852 /* Error if tunnel socket is not prepped */
853 if (tunnel
->sock
== NULL
)
856 /* Check that this session doesn't already exist */
858 session
= l2tp_session_find(net
, tunnel
, session_id
);
862 /* Default MTU values. */
864 cfg
->mtu
= 1500 - PPPOL2TP_HEADER_OVERHEAD
;
868 /* Allocate and initialize a new session context. */
870 session
= l2tp_session_create(sizeof(struct pppol2tp_session
),
872 peer_session_id
, cfg
);
876 ps
= l2tp_session_priv(session
);
877 ps
->tunnel_sock
= tunnel
->sock
;
879 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: created\n",
888 /* Called when deleting sessions via the netlink interface.
890 static int pppol2tp_session_delete(struct l2tp_session
*session
)
892 struct pppol2tp_session
*ps
= l2tp_session_priv(session
);
894 if (ps
->sock
== NULL
)
895 l2tp_session_dec_refcount(session
);
900 #endif /* CONFIG_L2TP_V3 */
902 /* getname() support.
904 static int pppol2tp_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
905 int *usockaddr_len
, int peer
)
909 struct l2tp_session
*session
;
910 struct l2tp_tunnel
*tunnel
;
911 struct sock
*sk
= sock
->sk
;
912 struct inet_sock
*inet
;
913 struct pppol2tp_session
*pls
;
918 if (sk
->sk_state
!= PPPOX_CONNECTED
)
922 session
= pppol2tp_sock_to_session(sk
);
926 pls
= l2tp_session_priv(session
);
927 tunnel
= l2tp_sock_to_tunnel(pls
->tunnel_sock
);
928 if (tunnel
== NULL
) {
933 inet
= inet_sk(tunnel
->sock
);
934 if ((tunnel
->version
== 2) && (tunnel
->sock
->sk_family
== AF_INET
)) {
935 struct sockaddr_pppol2tp sp
;
938 sp
.sa_family
= AF_PPPOX
;
939 sp
.sa_protocol
= PX_PROTO_OL2TP
;
940 sp
.pppol2tp
.fd
= tunnel
->fd
;
941 sp
.pppol2tp
.pid
= pls
->owner
;
942 sp
.pppol2tp
.s_tunnel
= tunnel
->tunnel_id
;
943 sp
.pppol2tp
.d_tunnel
= tunnel
->peer_tunnel_id
;
944 sp
.pppol2tp
.s_session
= session
->session_id
;
945 sp
.pppol2tp
.d_session
= session
->peer_session_id
;
946 sp
.pppol2tp
.addr
.sin_family
= AF_INET
;
947 sp
.pppol2tp
.addr
.sin_port
= inet
->inet_dport
;
948 sp
.pppol2tp
.addr
.sin_addr
.s_addr
= inet
->inet_daddr
;
949 memcpy(uaddr
, &sp
, len
);
950 #if IS_ENABLED(CONFIG_IPV6)
951 } else if ((tunnel
->version
== 2) &&
952 (tunnel
->sock
->sk_family
== AF_INET6
)) {
953 struct ipv6_pinfo
*np
= inet6_sk(tunnel
->sock
);
954 struct sockaddr_pppol2tpin6 sp
;
957 sp
.sa_family
= AF_PPPOX
;
958 sp
.sa_protocol
= PX_PROTO_OL2TP
;
959 sp
.pppol2tp
.fd
= tunnel
->fd
;
960 sp
.pppol2tp
.pid
= pls
->owner
;
961 sp
.pppol2tp
.s_tunnel
= tunnel
->tunnel_id
;
962 sp
.pppol2tp
.d_tunnel
= tunnel
->peer_tunnel_id
;
963 sp
.pppol2tp
.s_session
= session
->session_id
;
964 sp
.pppol2tp
.d_session
= session
->peer_session_id
;
965 sp
.pppol2tp
.addr
.sin6_family
= AF_INET6
;
966 sp
.pppol2tp
.addr
.sin6_port
= inet
->inet_dport
;
967 memcpy(&sp
.pppol2tp
.addr
.sin6_addr
, &np
->daddr
,
969 memcpy(uaddr
, &sp
, len
);
970 } else if ((tunnel
->version
== 3) &&
971 (tunnel
->sock
->sk_family
== AF_INET6
)) {
972 struct ipv6_pinfo
*np
= inet6_sk(tunnel
->sock
);
973 struct sockaddr_pppol2tpv3in6 sp
;
976 sp
.sa_family
= AF_PPPOX
;
977 sp
.sa_protocol
= PX_PROTO_OL2TP
;
978 sp
.pppol2tp
.fd
= tunnel
->fd
;
979 sp
.pppol2tp
.pid
= pls
->owner
;
980 sp
.pppol2tp
.s_tunnel
= tunnel
->tunnel_id
;
981 sp
.pppol2tp
.d_tunnel
= tunnel
->peer_tunnel_id
;
982 sp
.pppol2tp
.s_session
= session
->session_id
;
983 sp
.pppol2tp
.d_session
= session
->peer_session_id
;
984 sp
.pppol2tp
.addr
.sin6_family
= AF_INET6
;
985 sp
.pppol2tp
.addr
.sin6_port
= inet
->inet_dport
;
986 memcpy(&sp
.pppol2tp
.addr
.sin6_addr
, &np
->daddr
,
988 memcpy(uaddr
, &sp
, len
);
990 } else if (tunnel
->version
== 3) {
991 struct sockaddr_pppol2tpv3 sp
;
994 sp
.sa_family
= AF_PPPOX
;
995 sp
.sa_protocol
= PX_PROTO_OL2TP
;
996 sp
.pppol2tp
.fd
= tunnel
->fd
;
997 sp
.pppol2tp
.pid
= pls
->owner
;
998 sp
.pppol2tp
.s_tunnel
= tunnel
->tunnel_id
;
999 sp
.pppol2tp
.d_tunnel
= tunnel
->peer_tunnel_id
;
1000 sp
.pppol2tp
.s_session
= session
->session_id
;
1001 sp
.pppol2tp
.d_session
= session
->peer_session_id
;
1002 sp
.pppol2tp
.addr
.sin_family
= AF_INET
;
1003 sp
.pppol2tp
.addr
.sin_port
= inet
->inet_dport
;
1004 sp
.pppol2tp
.addr
.sin_addr
.s_addr
= inet
->inet_daddr
;
1005 memcpy(uaddr
, &sp
, len
);
1008 *usockaddr_len
= len
;
1010 sock_put(pls
->tunnel_sock
);
1019 /****************************************************************************
1022 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1023 * sockets. However, in order to control kernel tunnel features, we allow
1024 * userspace to create a special "tunnel" PPPoX socket which is used for
1025 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1026 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1028 ****************************************************************************/
1030 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats
*dest
,
1031 struct l2tp_stats
*stats
)
1033 dest
->tx_packets
= stats
->tx_packets
;
1034 dest
->tx_bytes
= stats
->tx_bytes
;
1035 dest
->tx_errors
= stats
->tx_errors
;
1036 dest
->rx_packets
= stats
->rx_packets
;
1037 dest
->rx_bytes
= stats
->rx_bytes
;
1038 dest
->rx_seq_discards
= stats
->rx_seq_discards
;
1039 dest
->rx_oos_packets
= stats
->rx_oos_packets
;
1040 dest
->rx_errors
= stats
->rx_errors
;
1043 /* Session ioctl helper.
1045 static int pppol2tp_session_ioctl(struct l2tp_session
*session
,
1046 unsigned int cmd
, unsigned long arg
)
1051 int val
= (int) arg
;
1052 struct pppol2tp_session
*ps
= l2tp_session_priv(session
);
1053 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1054 struct pppol2tp_ioc_stats stats
;
1056 l2tp_dbg(session
, PPPOL2TP_MSG_CONTROL
,
1057 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1058 session
->name
, cmd
, arg
);
1066 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1070 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1072 ifr
.ifr_mtu
= session
->mtu
;
1073 if (copy_to_user((void __user
*) arg
, &ifr
, sizeof(struct ifreq
)))
1076 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: get mtu=%d\n",
1077 session
->name
, session
->mtu
);
1083 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1087 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1090 session
->mtu
= ifr
.ifr_mtu
;
1092 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: set mtu=%d\n",
1093 session
->name
, session
->mtu
);
1099 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1103 if (put_user(session
->mru
, (int __user
*) arg
))
1106 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: get mru=%d\n",
1107 session
->name
, session
->mru
);
1113 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1117 if (get_user(val
, (int __user
*) arg
))
1121 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: set mru=%d\n",
1122 session
->name
, session
->mru
);
1128 if (put_user(ps
->flags
, (int __user
*) arg
))
1131 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: get flags=%d\n",
1132 session
->name
, ps
->flags
);
1138 if (get_user(val
, (int __user
*) arg
))
1141 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: set flags=%d\n",
1142 session
->name
, ps
->flags
);
1146 case PPPIOCGL2TPSTATS
:
1148 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1151 memset(&stats
, 0, sizeof(stats
));
1152 stats
.tunnel_id
= tunnel
->tunnel_id
;
1153 stats
.session_id
= session
->session_id
;
1154 pppol2tp_copy_stats(&stats
, &session
->stats
);
1155 if (copy_to_user((void __user
*) arg
, &stats
,
1158 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: get L2TP stats\n",
1173 /* Tunnel ioctl helper.
1175 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1176 * specifies a session_id, the session ioctl handler is called. This allows an
1177 * application to retrieve session stats via a tunnel socket.
1179 static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel
*tunnel
,
1180 unsigned int cmd
, unsigned long arg
)
1184 struct pppol2tp_ioc_stats stats
;
1186 l2tp_dbg(tunnel
, PPPOL2TP_MSG_CONTROL
,
1187 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1188 tunnel
->name
, cmd
, arg
);
1194 case PPPIOCGL2TPSTATS
:
1196 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1199 if (copy_from_user(&stats
, (void __user
*) arg
,
1204 if (stats
.session_id
!= 0) {
1205 /* resend to session ioctl handler */
1206 struct l2tp_session
*session
=
1207 l2tp_session_find(sock_net(sk
), tunnel
, stats
.session_id
);
1208 if (session
!= NULL
)
1209 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
1215 stats
.using_ipsec
= (sk
->sk_policy
[0] || sk
->sk_policy
[1]) ? 1 : 0;
1217 pppol2tp_copy_stats(&stats
, &tunnel
->stats
);
1218 if (copy_to_user((void __user
*) arg
, &stats
, sizeof(stats
))) {
1222 l2tp_info(tunnel
, PPPOL2TP_MSG_CONTROL
, "%s: get L2TP stats\n",
1237 /* Main ioctl() handler.
1238 * Dispatch to tunnel or session helpers depending on the socket.
1240 static int pppol2tp_ioctl(struct socket
*sock
, unsigned int cmd
,
1243 struct sock
*sk
= sock
->sk
;
1244 struct l2tp_session
*session
;
1245 struct l2tp_tunnel
*tunnel
;
1246 struct pppol2tp_session
*ps
;
1253 if (sock_flag(sk
, SOCK_DEAD
) != 0)
1257 if ((sk
->sk_user_data
== NULL
) ||
1258 (!(sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
))))
1261 /* Get session context from the socket */
1263 session
= pppol2tp_sock_to_session(sk
);
1264 if (session
== NULL
)
1267 /* Special case: if session's session_id is zero, treat ioctl as a
1270 ps
= l2tp_session_priv(session
);
1271 if ((session
->session_id
== 0) &&
1272 (session
->peer_session_id
== 0)) {
1274 tunnel
= l2tp_sock_to_tunnel(ps
->tunnel_sock
);
1278 err
= pppol2tp_tunnel_ioctl(tunnel
, cmd
, arg
);
1279 sock_put(ps
->tunnel_sock
);
1283 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
1291 /*****************************************************************************
1292 * setsockopt() / getsockopt() support.
1294 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1295 * sockets. In order to control kernel tunnel features, we allow userspace to
1296 * create a special "tunnel" PPPoX socket which is used for control only.
1297 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1298 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1299 *****************************************************************************/
1301 /* Tunnel setsockopt() helper.
1303 static int pppol2tp_tunnel_setsockopt(struct sock
*sk
,
1304 struct l2tp_tunnel
*tunnel
,
1305 int optname
, int val
)
1310 case PPPOL2TP_SO_DEBUG
:
1311 tunnel
->debug
= val
;
1312 l2tp_info(tunnel
, PPPOL2TP_MSG_CONTROL
, "%s: set debug=%x\n",
1313 tunnel
->name
, tunnel
->debug
);
1324 /* Session setsockopt helper.
1326 static int pppol2tp_session_setsockopt(struct sock
*sk
,
1327 struct l2tp_session
*session
,
1328 int optname
, int val
)
1331 struct pppol2tp_session
*ps
= l2tp_session_priv(session
);
1334 case PPPOL2TP_SO_RECVSEQ
:
1335 if ((val
!= 0) && (val
!= 1)) {
1339 session
->recv_seq
= val
? -1 : 0;
1340 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
,
1341 "%s: set recv_seq=%d\n",
1342 session
->name
, session
->recv_seq
);
1345 case PPPOL2TP_SO_SENDSEQ
:
1346 if ((val
!= 0) && (val
!= 1)) {
1350 session
->send_seq
= val
? -1 : 0;
1352 struct sock
*ssk
= ps
->sock
;
1353 struct pppox_sock
*po
= pppox_sk(ssk
);
1354 po
->chan
.hdrlen
= val
? PPPOL2TP_L2TP_HDR_SIZE_SEQ
:
1355 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
1357 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
,
1358 "%s: set send_seq=%d\n",
1359 session
->name
, session
->send_seq
);
1362 case PPPOL2TP_SO_LNSMODE
:
1363 if ((val
!= 0) && (val
!= 1)) {
1367 session
->lns_mode
= val
? -1 : 0;
1368 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
,
1369 "%s: set lns_mode=%d\n",
1370 session
->name
, session
->lns_mode
);
1373 case PPPOL2TP_SO_DEBUG
:
1374 session
->debug
= val
;
1375 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: set debug=%x\n",
1376 session
->name
, session
->debug
);
1379 case PPPOL2TP_SO_REORDERTO
:
1380 session
->reorder_timeout
= msecs_to_jiffies(val
);
1381 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
,
1382 "%s: set reorder_timeout=%d\n",
1383 session
->name
, session
->reorder_timeout
);
1394 /* Main setsockopt() entry point.
1395 * Does API checks, then calls either the tunnel or session setsockopt
1396 * handler, according to whether the PPPoL2TP socket is a for a regular
1397 * session or the special tunnel type.
1399 static int pppol2tp_setsockopt(struct socket
*sock
, int level
, int optname
,
1400 char __user
*optval
, unsigned int optlen
)
1402 struct sock
*sk
= sock
->sk
;
1403 struct l2tp_session
*session
;
1404 struct l2tp_tunnel
*tunnel
;
1405 struct pppol2tp_session
*ps
;
1409 if (level
!= SOL_PPPOL2TP
)
1410 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
1412 if (optlen
< sizeof(int))
1415 if (get_user(val
, (int __user
*)optval
))
1419 if (sk
->sk_user_data
== NULL
)
1422 /* Get session context from the socket */
1424 session
= pppol2tp_sock_to_session(sk
);
1425 if (session
== NULL
)
1428 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1430 ps
= l2tp_session_priv(session
);
1431 if ((session
->session_id
== 0) &&
1432 (session
->peer_session_id
== 0)) {
1434 tunnel
= l2tp_sock_to_tunnel(ps
->tunnel_sock
);
1438 err
= pppol2tp_tunnel_setsockopt(sk
, tunnel
, optname
, val
);
1439 sock_put(ps
->tunnel_sock
);
1441 err
= pppol2tp_session_setsockopt(sk
, session
, optname
, val
);
1451 /* Tunnel getsockopt helper. Called with sock locked.
1453 static int pppol2tp_tunnel_getsockopt(struct sock
*sk
,
1454 struct l2tp_tunnel
*tunnel
,
1455 int optname
, int *val
)
1460 case PPPOL2TP_SO_DEBUG
:
1461 *val
= tunnel
->debug
;
1462 l2tp_info(tunnel
, PPPOL2TP_MSG_CONTROL
, "%s: get debug=%x\n",
1463 tunnel
->name
, tunnel
->debug
);
1474 /* Session getsockopt helper. Called with sock locked.
1476 static int pppol2tp_session_getsockopt(struct sock
*sk
,
1477 struct l2tp_session
*session
,
1478 int optname
, int *val
)
1483 case PPPOL2TP_SO_RECVSEQ
:
1484 *val
= session
->recv_seq
;
1485 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
,
1486 "%s: get recv_seq=%d\n", session
->name
, *val
);
1489 case PPPOL2TP_SO_SENDSEQ
:
1490 *val
= session
->send_seq
;
1491 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
,
1492 "%s: get send_seq=%d\n", session
->name
, *val
);
1495 case PPPOL2TP_SO_LNSMODE
:
1496 *val
= session
->lns_mode
;
1497 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
,
1498 "%s: get lns_mode=%d\n", session
->name
, *val
);
1501 case PPPOL2TP_SO_DEBUG
:
1502 *val
= session
->debug
;
1503 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
, "%s: get debug=%d\n",
1504 session
->name
, *val
);
1507 case PPPOL2TP_SO_REORDERTO
:
1508 *val
= (int) jiffies_to_msecs(session
->reorder_timeout
);
1509 l2tp_info(session
, PPPOL2TP_MSG_CONTROL
,
1510 "%s: get reorder_timeout=%d\n", session
->name
, *val
);
1520 /* Main getsockopt() entry point.
1521 * Does API checks, then calls either the tunnel or session getsockopt
1522 * handler, according to whether the PPPoX socket is a for a regular session
1523 * or the special tunnel type.
1525 static int pppol2tp_getsockopt(struct socket
*sock
, int level
, int optname
,
1526 char __user
*optval
, int __user
*optlen
)
1528 struct sock
*sk
= sock
->sk
;
1529 struct l2tp_session
*session
;
1530 struct l2tp_tunnel
*tunnel
;
1533 struct pppol2tp_session
*ps
;
1535 if (level
!= SOL_PPPOL2TP
)
1536 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1538 if (get_user(len
, optlen
))
1541 len
= min_t(unsigned int, len
, sizeof(int));
1547 if (sk
->sk_user_data
== NULL
)
1550 /* Get the session context */
1552 session
= pppol2tp_sock_to_session(sk
);
1553 if (session
== NULL
)
1556 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1557 ps
= l2tp_session_priv(session
);
1558 if ((session
->session_id
== 0) &&
1559 (session
->peer_session_id
== 0)) {
1561 tunnel
= l2tp_sock_to_tunnel(ps
->tunnel_sock
);
1565 err
= pppol2tp_tunnel_getsockopt(sk
, tunnel
, optname
, &val
);
1566 sock_put(ps
->tunnel_sock
);
1568 err
= pppol2tp_session_getsockopt(sk
, session
, optname
, &val
);
1571 if (put_user(len
, optlen
))
1574 if (copy_to_user((void __user
*) optval
, &val
, len
))
1585 /*****************************************************************************
1586 * /proc filesystem for debug
1587 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1588 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1589 *****************************************************************************/
1591 static unsigned int pppol2tp_net_id
;
1593 #ifdef CONFIG_PROC_FS
1595 struct pppol2tp_seq_data
{
1596 struct seq_net_private p
;
1597 int tunnel_idx
; /* current tunnel */
1598 int session_idx
; /* index of session within current tunnel */
1599 struct l2tp_tunnel
*tunnel
;
1600 struct l2tp_session
*session
; /* NULL means get next tunnel */
1603 static void pppol2tp_next_tunnel(struct net
*net
, struct pppol2tp_seq_data
*pd
)
1606 pd
->tunnel
= l2tp_tunnel_find_nth(net
, pd
->tunnel_idx
);
1609 if (pd
->tunnel
== NULL
)
1612 /* Ignore L2TPv3 tunnels */
1613 if (pd
->tunnel
->version
< 3)
1618 static void pppol2tp_next_session(struct net
*net
, struct pppol2tp_seq_data
*pd
)
1620 pd
->session
= l2tp_session_find_nth(pd
->tunnel
, pd
->session_idx
);
1623 if (pd
->session
== NULL
) {
1624 pd
->session_idx
= 0;
1625 pppol2tp_next_tunnel(net
, pd
);
1629 static void *pppol2tp_seq_start(struct seq_file
*m
, loff_t
*offs
)
1631 struct pppol2tp_seq_data
*pd
= SEQ_START_TOKEN
;
1638 BUG_ON(m
->private == NULL
);
1640 net
= seq_file_net(m
);
1642 if (pd
->tunnel
== NULL
)
1643 pppol2tp_next_tunnel(net
, pd
);
1645 pppol2tp_next_session(net
, pd
);
1647 /* NULL tunnel and session indicates end of list */
1648 if ((pd
->tunnel
== NULL
) && (pd
->session
== NULL
))
1655 static void *pppol2tp_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1661 static void pppol2tp_seq_stop(struct seq_file
*p
, void *v
)
1666 static void pppol2tp_seq_tunnel_show(struct seq_file
*m
, void *v
)
1668 struct l2tp_tunnel
*tunnel
= v
;
1670 seq_printf(m
, "\nTUNNEL '%s', %c %d\n",
1672 (tunnel
== tunnel
->sock
->sk_user_data
) ? 'Y' : 'N',
1673 atomic_read(&tunnel
->ref_count
) - 1);
1674 seq_printf(m
, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1676 (unsigned long long)tunnel
->stats
.tx_packets
,
1677 (unsigned long long)tunnel
->stats
.tx_bytes
,
1678 (unsigned long long)tunnel
->stats
.tx_errors
,
1679 (unsigned long long)tunnel
->stats
.rx_packets
,
1680 (unsigned long long)tunnel
->stats
.rx_bytes
,
1681 (unsigned long long)tunnel
->stats
.rx_errors
);
1684 static void pppol2tp_seq_session_show(struct seq_file
*m
, void *v
)
1686 struct l2tp_session
*session
= v
;
1687 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1688 struct pppol2tp_session
*ps
= l2tp_session_priv(session
);
1689 struct pppox_sock
*po
= pppox_sk(ps
->sock
);
1694 struct inet_sock
*inet
= inet_sk(tunnel
->sock
);
1695 ip
= ntohl(inet
->inet_saddr
);
1696 port
= ntohs(inet
->inet_sport
);
1699 seq_printf(m
, " SESSION '%s' %08X/%d %04X/%04X -> "
1700 "%04X/%04X %d %c\n",
1701 session
->name
, ip
, port
,
1703 session
->session_id
,
1704 tunnel
->peer_tunnel_id
,
1705 session
->peer_session_id
,
1707 (session
== ps
->sock
->sk_user_data
) ?
1709 seq_printf(m
, " %d/%d/%c/%c/%s %08x %u\n",
1710 session
->mtu
, session
->mru
,
1711 session
->recv_seq
? 'R' : '-',
1712 session
->send_seq
? 'S' : '-',
1713 session
->lns_mode
? "LNS" : "LAC",
1715 jiffies_to_msecs(session
->reorder_timeout
));
1716 seq_printf(m
, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1717 session
->nr
, session
->ns
,
1718 (unsigned long long)session
->stats
.tx_packets
,
1719 (unsigned long long)session
->stats
.tx_bytes
,
1720 (unsigned long long)session
->stats
.tx_errors
,
1721 (unsigned long long)session
->stats
.rx_packets
,
1722 (unsigned long long)session
->stats
.rx_bytes
,
1723 (unsigned long long)session
->stats
.rx_errors
);
1726 seq_printf(m
, " interface %s\n", ppp_dev_name(&po
->chan
));
1729 static int pppol2tp_seq_show(struct seq_file
*m
, void *v
)
1731 struct pppol2tp_seq_data
*pd
= v
;
1733 /* display header on line 1 */
1734 if (v
== SEQ_START_TOKEN
) {
1735 seq_puts(m
, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION
"\n");
1736 seq_puts(m
, "TUNNEL name, user-data-ok session-count\n");
1737 seq_puts(m
, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1738 seq_puts(m
, " SESSION name, addr/port src-tid/sid "
1739 "dest-tid/sid state user-data-ok\n");
1740 seq_puts(m
, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1741 seq_puts(m
, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1745 /* Show the tunnel or session context.
1747 if (pd
->session
== NULL
)
1748 pppol2tp_seq_tunnel_show(m
, pd
->tunnel
);
1750 pppol2tp_seq_session_show(m
, pd
->session
);
1756 static const struct seq_operations pppol2tp_seq_ops
= {
1757 .start
= pppol2tp_seq_start
,
1758 .next
= pppol2tp_seq_next
,
1759 .stop
= pppol2tp_seq_stop
,
1760 .show
= pppol2tp_seq_show
,
1763 /* Called when our /proc file is opened. We allocate data for use when
1764 * iterating our tunnel / session contexts and store it in the private
1765 * data of the seq_file.
1767 static int pppol2tp_proc_open(struct inode
*inode
, struct file
*file
)
1769 return seq_open_net(inode
, file
, &pppol2tp_seq_ops
,
1770 sizeof(struct pppol2tp_seq_data
));
1773 static const struct file_operations pppol2tp_proc_fops
= {
1774 .owner
= THIS_MODULE
,
1775 .open
= pppol2tp_proc_open
,
1777 .llseek
= seq_lseek
,
1778 .release
= seq_release_net
,
1781 #endif /* CONFIG_PROC_FS */
1783 /*****************************************************************************
1785 *****************************************************************************/
1787 static __net_init
int pppol2tp_init_net(struct net
*net
)
1789 struct proc_dir_entry
*pde
;
1792 pde
= proc_net_fops_create(net
, "pppol2tp", S_IRUGO
, &pppol2tp_proc_fops
);
1802 static __net_exit
void pppol2tp_exit_net(struct net
*net
)
1804 proc_net_remove(net
, "pppol2tp");
1807 static struct pernet_operations pppol2tp_net_ops
= {
1808 .init
= pppol2tp_init_net
,
1809 .exit
= pppol2tp_exit_net
,
1810 .id
= &pppol2tp_net_id
,
1813 /*****************************************************************************
1815 *****************************************************************************/
1817 static const struct proto_ops pppol2tp_ops
= {
1819 .owner
= THIS_MODULE
,
1820 .release
= pppol2tp_release
,
1821 .bind
= sock_no_bind
,
1822 .connect
= pppol2tp_connect
,
1823 .socketpair
= sock_no_socketpair
,
1824 .accept
= sock_no_accept
,
1825 .getname
= pppol2tp_getname
,
1826 .poll
= datagram_poll
,
1827 .listen
= sock_no_listen
,
1828 .shutdown
= sock_no_shutdown
,
1829 .setsockopt
= pppol2tp_setsockopt
,
1830 .getsockopt
= pppol2tp_getsockopt
,
1831 .sendmsg
= pppol2tp_sendmsg
,
1832 .recvmsg
= pppol2tp_recvmsg
,
1833 .mmap
= sock_no_mmap
,
1834 .ioctl
= pppox_ioctl
,
1837 static const struct pppox_proto pppol2tp_proto
= {
1838 .create
= pppol2tp_create
,
1839 .ioctl
= pppol2tp_ioctl
1842 #ifdef CONFIG_L2TP_V3
1844 static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops
= {
1845 .session_create
= pppol2tp_session_create
,
1846 .session_delete
= pppol2tp_session_delete
,
1849 #endif /* CONFIG_L2TP_V3 */
1851 static int __init
pppol2tp_init(void)
1855 err
= register_pernet_device(&pppol2tp_net_ops
);
1859 err
= proto_register(&pppol2tp_sk_proto
, 0);
1861 goto out_unregister_pppol2tp_pernet
;
1863 err
= register_pppox_proto(PX_PROTO_OL2TP
, &pppol2tp_proto
);
1865 goto out_unregister_pppol2tp_proto
;
1867 #ifdef CONFIG_L2TP_V3
1868 err
= l2tp_nl_register_ops(L2TP_PWTYPE_PPP
, &pppol2tp_nl_cmd_ops
);
1870 goto out_unregister_pppox
;
1873 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION
);
1878 #ifdef CONFIG_L2TP_V3
1879 out_unregister_pppox
:
1880 unregister_pppox_proto(PX_PROTO_OL2TP
);
1882 out_unregister_pppol2tp_proto
:
1883 proto_unregister(&pppol2tp_sk_proto
);
1884 out_unregister_pppol2tp_pernet
:
1885 unregister_pernet_device(&pppol2tp_net_ops
);
1889 static void __exit
pppol2tp_exit(void)
1891 #ifdef CONFIG_L2TP_V3
1892 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP
);
1894 unregister_pppox_proto(PX_PROTO_OL2TP
);
1895 proto_unregister(&pppol2tp_sk_proto
);
1896 unregister_pernet_device(&pppol2tp_net_ops
);
1899 module_init(pppol2tp_init
);
1900 module_exit(pppol2tp_exit
);
1902 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1903 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1904 MODULE_LICENSE("GPL");
1905 MODULE_VERSION(PPPOL2TP_DRV_VERSION
);
1906 MODULE_ALIAS("pppox-proto-" __stringify(PX_PROTO_OL2TP
));