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 #include <linux/module.h>
61 #include <linux/string.h>
62 #include <linux/list.h>
63 #include <linux/uaccess.h>
65 #include <linux/kernel.h>
66 #include <linux/spinlock.h>
67 #include <linux/kthread.h>
68 #include <linux/sched.h>
69 #include <linux/slab.h>
70 #include <linux/errno.h>
71 #include <linux/jiffies.h>
73 #include <linux/netdevice.h>
74 #include <linux/net.h>
75 #include <linux/inetdevice.h>
76 #include <linux/skbuff.h>
77 #include <linux/init.h>
79 #include <linux/udp.h>
80 #include <linux/if_pppox.h>
81 #include <linux/if_pppol2tp.h>
83 #include <linux/ppp_channel.h>
84 #include <linux/ppp_defs.h>
85 #include <linux/if_ppp.h>
86 #include <linux/file.h>
87 #include <linux/hash.h>
88 #include <linux/sort.h>
89 #include <linux/proc_fs.h>
90 #include <linux/nsproxy.h>
91 #include <net/net_namespace.h>
92 #include <net/netns/generic.h>
98 #include <asm/byteorder.h>
99 #include <asm/atomic.h>
101 #include "l2tp_core.h"
103 #define PPPOL2TP_DRV_VERSION "V2.0"
105 /* Space for UDP, L2TP and PPP headers */
106 #define PPPOL2TP_HEADER_OVERHEAD 40
108 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
110 if ((_mask) & (_type)) \
111 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
114 /* Number of bytes to build transmit L2TP headers.
115 * Unfortunately the size is different depending on whether sequence numbers
118 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
119 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
121 /* Private data of each session. This data lives at the end of struct
122 * l2tp_session, referenced via session->priv[].
124 struct pppol2tp_session {
125 int owner; /* pid that opened the socket */
127 struct sock *sock; /* Pointer to the session
129 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
131 int flags; /* accessed by PPPIOCGFLAGS.
135 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
137 static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL };
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 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
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 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
259 "%s: socket not bound\n", session->name);
261 /* Not bound. Nothing we can do, so discard. */
262 session->stats.rx_errors++;
269 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
270 "%s: no socket\n", session->name);
274 static void pppol2tp_session_sock_hold(struct l2tp_session *session)
276 struct pppol2tp_session *ps = l2tp_session_priv(session);
282 static void pppol2tp_session_sock_put(struct l2tp_session *session)
284 struct pppol2tp_session *ps = l2tp_session_priv(session);
290 /************************************************************************
292 ***********************************************************************/
294 /* Tell how big L2TP headers are for a particular session. This
295 * depends on whether sequence numbers are being used.
297 static inline int pppol2tp_l2tp_header_len(struct l2tp_session *session)
299 if (session->send_seq)
300 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
302 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
305 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
306 * when a user application does a sendmsg() on the session socket. L2TP and
307 * PPP headers must be inserted into the user's data.
309 static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
312 static const unsigned char ppph[2] = { 0xff, 0x03 };
313 struct sock *sk = sock->sk;
316 struct l2tp_session *session;
317 struct l2tp_tunnel *tunnel;
318 struct pppol2tp_session *ps;
321 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
324 /* Get session and tunnel contexts */
326 session = pppol2tp_sock_to_session(sk);
330 ps = l2tp_session_priv(session);
331 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
335 /* Allocate a socket buffer */
337 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
338 sizeof(struct udphdr) + session->hdr_len +
339 sizeof(ppph) + total_len,
342 goto error_put_sess_tun;
344 /* Reserve space for headers. */
345 skb_reserve(skb, NET_SKB_PAD);
346 skb_reset_network_header(skb);
347 skb_reserve(skb, sizeof(struct iphdr));
348 skb_reset_transport_header(skb);
349 skb_reserve(skb, sizeof(struct udphdr));
352 skb->data[0] = ppph[0];
353 skb->data[1] = ppph[1];
356 /* Copy user data into skb */
357 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
360 goto error_put_sess_tun;
362 skb_put(skb, total_len);
364 l2tp_xmit_skb(session, skb, session->hdr_len);
366 sock_put(ps->tunnel_sock);
371 sock_put(ps->tunnel_sock);
378 /* Transmit function called by generic PPP driver. Sends PPP frame
379 * over PPPoL2TP socket.
381 * This is almost the same as pppol2tp_sendmsg(), but rather than
382 * being called with a msghdr from userspace, it is called with a skb
385 * The supplied skb from ppp doesn't have enough headroom for the
386 * insertion of L2TP, UDP and IP headers so we need to allocate more
387 * headroom in the skb. This will create a cloned skb. But we must be
388 * careful in the error case because the caller will expect to free
389 * the skb it supplied, not our cloned skb. So we take care to always
390 * leave the original skb unfreed if we return an error.
392 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
394 static const u8 ppph[2] = { 0xff, 0x03 };
395 struct sock *sk = (struct sock *) chan->private;
398 struct l2tp_session *session;
399 struct l2tp_tunnel *tunnel;
400 struct pppol2tp_session *ps;
404 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
407 /* Get session and tunnel contexts from the socket */
408 session = pppol2tp_sock_to_session(sk);
412 ps = l2tp_session_priv(session);
413 sk_tun = ps->tunnel_sock;
416 tunnel = l2tp_sock_to_tunnel(sk_tun);
420 /* What header length is configured for this session? */
421 hdr_len = pppol2tp_l2tp_header_len(session);
423 old_headroom = skb_headroom(skb);
424 if (skb_cow_head(skb, sizeof(ppph)))
425 goto abort_put_sess_tun;
427 new_headroom = skb_headroom(skb);
428 skb->truesize += new_headroom - old_headroom;
430 /* Setup PPP header */
431 __skb_push(skb, sizeof(ppph));
432 skb->data[0] = ppph[0];
433 skb->data[1] = ppph[1];
435 l2tp_xmit_skb(session, skb, hdr_len);
446 /* Free the original skb */
451 /*****************************************************************************
452 * Session (and tunnel control) socket create/destroy.
453 *****************************************************************************/
455 /* Called by l2tp_core when a session socket is being closed.
457 static void pppol2tp_session_close(struct l2tp_session *session)
459 struct pppol2tp_session *ps = l2tp_session_priv(session);
460 struct sock *sk = ps->sock;
463 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
465 if (session->session_id == 0)
471 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
472 pppox_unbind_sock(sk);
473 sk->sk_state = PPPOX_DEAD;
474 sk->sk_state_change(sk);
477 /* Purge any queued data */
478 skb_queue_purge(&sk->sk_receive_queue);
479 skb_queue_purge(&sk->sk_write_queue);
480 while ((skb = skb_dequeue(&session->reorder_q))) {
492 /* Really kill the session socket. (Called from sock_put() if
495 static void pppol2tp_session_destruct(struct sock *sk)
497 struct l2tp_session *session;
499 if (sk->sk_user_data != NULL) {
500 session = sk->sk_user_data;
504 sk->sk_user_data = NULL;
505 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
506 l2tp_session_dec_refcount(session);
513 /* Called when the PPPoX socket (session) is closed.
515 static int pppol2tp_release(struct socket *sock)
517 struct sock *sk = sock->sk;
518 struct l2tp_session *session;
526 if (sock_flag(sk, SOCK_DEAD) != 0)
529 pppox_unbind_sock(sk);
531 /* Signal the death of the socket. */
532 sk->sk_state = PPPOX_DEAD;
536 session = pppol2tp_sock_to_session(sk);
538 /* Purge any queued data */
539 skb_queue_purge(&sk->sk_receive_queue);
540 skb_queue_purge(&sk->sk_write_queue);
541 if (session != NULL) {
543 while ((skb = skb_dequeue(&session->reorder_q))) {
552 /* This will delete the session context via
553 * pppol2tp_session_destruct() if the socket's refcnt drops to
565 static struct proto pppol2tp_sk_proto = {
567 .owner = THIS_MODULE,
568 .obj_size = sizeof(struct pppox_sock),
571 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
575 rc = l2tp_udp_encap_recv(sk, skb);
579 return NET_RX_SUCCESS;
582 /* socket() handler. Initialize a new struct sock.
584 static int pppol2tp_create(struct net *net, struct socket *sock)
589 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
593 sock_init_data(sock, sk);
595 sock->state = SS_UNCONNECTED;
596 sock->ops = &pppol2tp_ops;
598 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
599 sk->sk_protocol = PX_PROTO_OL2TP;
600 sk->sk_family = PF_PPPOX;
601 sk->sk_state = PPPOX_NONE;
602 sk->sk_type = SOCK_STREAM;
603 sk->sk_destruct = pppol2tp_session_destruct;
611 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
613 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
614 int sockaddr_len, int flags)
616 struct sock *sk = sock->sk;
617 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
618 struct pppox_sock *po = pppox_sk(sk);
619 struct l2tp_session *session = NULL;
620 struct l2tp_tunnel *tunnel;
621 struct pppol2tp_session *ps;
622 struct dst_entry *dst;
623 struct l2tp_session_cfg cfg = { 0, };
629 if (sp->sa_protocol != PX_PROTO_OL2TP)
632 /* Check for already bound sockets */
634 if (sk->sk_state & PPPOX_CONNECTED)
637 /* We don't supporting rebinding anyway */
639 if (sk->sk_user_data)
640 goto end; /* socket is already attached */
642 /* Don't bind if s_tunnel is 0 */
644 if (sp->pppol2tp.s_tunnel == 0)
647 /* Special case: create tunnel context if s_session and
648 * d_session is 0. Otherwise look up tunnel using supplied
651 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
652 error = l2tp_tunnel_create(sock_net(sk), sp->pppol2tp.fd, 2, sp->pppol2tp.s_tunnel, sp->pppol2tp.d_tunnel, NULL, &tunnel);
656 tunnel = l2tp_tunnel_find(sock_net(sk), sp->pppol2tp.s_tunnel);
658 /* Error if we can't find the tunnel */
663 /* Error if socket is not prepped */
664 if (tunnel->sock == NULL)
668 if (tunnel->recv_payload_hook == NULL)
669 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
671 /* Check that this session doesn't already exist */
673 session = l2tp_session_find(tunnel, sp->pppol2tp.s_session);
677 /* Default MTU must allow space for UDP/L2TP/PPP
680 cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
681 cfg.hdr_len = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
682 cfg.debug = tunnel->debug;
684 /* Allocate and initialize a new session context. */
685 session = l2tp_session_create(sizeof(struct pppol2tp_session),
686 tunnel, sp->pppol2tp.s_session,
687 sp->pppol2tp.d_session, &cfg);
688 if (session == NULL) {
693 ps = l2tp_session_priv(session);
694 ps->owner = current->pid;
696 ps->tunnel_sock = tunnel->sock;
698 session->recv_skb = pppol2tp_recv;
699 session->session_close = pppol2tp_session_close;
701 /* We need to know each time a skb is dropped from the reorder
704 session->ref = pppol2tp_session_sock_hold;
705 session->deref = pppol2tp_session_sock_put;
707 /* If PMTU discovery was enabled, use the MTU that was discovered */
708 dst = sk_dst_get(sk);
710 u32 pmtu = dst_mtu(__sk_dst_get(sk));
712 session->mtu = session->mru = pmtu -
713 PPPOL2TP_HEADER_OVERHEAD;
717 /* Special case: if source & dest session_id == 0x0000, this
718 * socket is being created to manage the tunnel. Just set up
719 * the internal context for use by ioctl() and sockopt()
722 if ((session->session_id == 0) &&
723 (session->peer_session_id == 0)) {
728 /* The only header we need to worry about is the L2TP
729 * header. This size is different depending on whether
730 * sequence numbers are enabled for the data channel.
732 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
734 po->chan.private = sk;
735 po->chan.ops = &pppol2tp_chan_ops;
736 po->chan.mtu = session->mtu;
738 error = ppp_register_net_channel(sock_net(sk), &po->chan);
743 /* This is how we get the session context from the socket. */
744 sk->sk_user_data = session;
745 sk->sk_state = PPPOX_CONNECTED;
746 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
747 "%s: created\n", session->name);
755 /* getname() support.
757 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
758 int *usockaddr_len, int peer)
760 int len = sizeof(struct sockaddr_pppol2tp);
761 struct sockaddr_pppol2tp sp;
763 struct l2tp_session *session;
764 struct l2tp_tunnel *tunnel;
765 struct sock *sk = sock->sk;
766 struct inet_sock *inet;
767 struct pppol2tp_session *pls;
772 if (sk->sk_state != PPPOX_CONNECTED)
776 session = pppol2tp_sock_to_session(sk);
780 pls = l2tp_session_priv(session);
781 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
782 if (tunnel == NULL) {
788 sp.sa_family = AF_PPPOX;
789 sp.sa_protocol = PX_PROTO_OL2TP;
790 sp.pppol2tp.fd = tunnel->fd;
791 sp.pppol2tp.pid = pls->owner;
792 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
793 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
794 sp.pppol2tp.s_session = session->session_id;
795 sp.pppol2tp.d_session = session->peer_session_id;
797 sp.pppol2tp.addr.sin_family = AF_INET;
798 sp.pppol2tp.addr.sin_port = inet->inet_dport;
799 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
801 memcpy(uaddr, &sp, len);
803 *usockaddr_len = len;
805 sock_put(pls->tunnel_sock);
814 /****************************************************************************
817 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
818 * sockets. However, in order to control kernel tunnel features, we allow
819 * userspace to create a special "tunnel" PPPoX socket which is used for
820 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
821 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
823 ****************************************************************************/
825 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
826 struct l2tp_stats *stats)
828 dest->tx_packets = stats->tx_packets;
829 dest->tx_bytes = stats->tx_bytes;
830 dest->tx_errors = stats->tx_errors;
831 dest->rx_packets = stats->rx_packets;
832 dest->rx_bytes = stats->rx_bytes;
833 dest->rx_seq_discards = stats->rx_seq_discards;
834 dest->rx_oos_packets = stats->rx_oos_packets;
835 dest->rx_errors = stats->rx_errors;
838 /* Session ioctl helper.
840 static int pppol2tp_session_ioctl(struct l2tp_session *session,
841 unsigned int cmd, unsigned long arg)
847 struct pppol2tp_session *ps = l2tp_session_priv(session);
848 struct l2tp_tunnel *tunnel = session->tunnel;
849 struct pppol2tp_ioc_stats stats;
851 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
852 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
853 session->name, cmd, arg);
861 if (!(sk->sk_state & PPPOX_CONNECTED))
865 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
867 ifr.ifr_mtu = session->mtu;
868 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
871 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
872 "%s: get mtu=%d\n", session->name, session->mtu);
878 if (!(sk->sk_state & PPPOX_CONNECTED))
882 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
885 session->mtu = ifr.ifr_mtu;
887 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
888 "%s: set mtu=%d\n", session->name, session->mtu);
894 if (!(sk->sk_state & PPPOX_CONNECTED))
898 if (put_user(session->mru, (int __user *) arg))
901 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
902 "%s: get mru=%d\n", session->name, session->mru);
908 if (!(sk->sk_state & PPPOX_CONNECTED))
912 if (get_user(val, (int __user *) arg))
916 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
917 "%s: set mru=%d\n", session->name, session->mru);
923 if (put_user(ps->flags, (int __user *) arg))
926 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
927 "%s: get flags=%d\n", session->name, ps->flags);
933 if (get_user(val, (int __user *) arg))
936 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
937 "%s: set flags=%d\n", session->name, ps->flags);
941 case PPPIOCGL2TPSTATS:
943 if (!(sk->sk_state & PPPOX_CONNECTED))
946 memset(&stats, 0, sizeof(stats));
947 stats.tunnel_id = tunnel->tunnel_id;
948 stats.session_id = session->session_id;
949 pppol2tp_copy_stats(&stats, &session->stats);
950 if (copy_to_user((void __user *) arg, &stats,
953 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
954 "%s: get L2TP stats\n", session->name);
968 /* Tunnel ioctl helper.
970 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
971 * specifies a session_id, the session ioctl handler is called. This allows an
972 * application to retrieve session stats via a tunnel socket.
974 static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
975 unsigned int cmd, unsigned long arg)
979 struct pppol2tp_ioc_stats stats;
981 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
982 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
983 tunnel->name, cmd, arg);
989 case PPPIOCGL2TPSTATS:
991 if (!(sk->sk_state & PPPOX_CONNECTED))
994 if (copy_from_user(&stats, (void __user *) arg,
999 if (stats.session_id != 0) {
1000 /* resend to session ioctl handler */
1001 struct l2tp_session *session =
1002 l2tp_session_find(tunnel, stats.session_id);
1003 if (session != NULL)
1004 err = pppol2tp_session_ioctl(session, cmd, arg);
1010 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1012 pppol2tp_copy_stats(&stats, &tunnel->stats);
1013 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1017 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1018 "%s: get L2TP stats\n", tunnel->name);
1032 /* Main ioctl() handler.
1033 * Dispatch to tunnel or session helpers depending on the socket.
1035 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1038 struct sock *sk = sock->sk;
1039 struct l2tp_session *session;
1040 struct l2tp_tunnel *tunnel;
1041 struct pppol2tp_session *ps;
1048 if (sock_flag(sk, SOCK_DEAD) != 0)
1052 if ((sk->sk_user_data == NULL) ||
1053 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1056 /* Get session context from the socket */
1058 session = pppol2tp_sock_to_session(sk);
1059 if (session == NULL)
1062 /* Special case: if session's session_id is zero, treat ioctl as a
1065 ps = l2tp_session_priv(session);
1066 if ((session->session_id == 0) &&
1067 (session->peer_session_id == 0)) {
1069 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1073 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1074 sock_put(ps->tunnel_sock);
1078 err = pppol2tp_session_ioctl(session, cmd, arg);
1086 /*****************************************************************************
1087 * setsockopt() / getsockopt() support.
1089 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1090 * sockets. In order to control kernel tunnel features, we allow userspace to
1091 * create a special "tunnel" PPPoX socket which is used for control only.
1092 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1093 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1094 *****************************************************************************/
1096 /* Tunnel setsockopt() helper.
1098 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1099 struct l2tp_tunnel *tunnel,
1100 int optname, int val)
1105 case PPPOL2TP_SO_DEBUG:
1106 tunnel->debug = val;
1107 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1108 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1119 /* Session setsockopt helper.
1121 static int pppol2tp_session_setsockopt(struct sock *sk,
1122 struct l2tp_session *session,
1123 int optname, int val)
1126 struct pppol2tp_session *ps = l2tp_session_priv(session);
1129 case PPPOL2TP_SO_RECVSEQ:
1130 if ((val != 0) && (val != 1)) {
1134 session->recv_seq = val ? -1 : 0;
1135 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1136 "%s: set recv_seq=%d\n", session->name, session->recv_seq);
1139 case PPPOL2TP_SO_SENDSEQ:
1140 if ((val != 0) && (val != 1)) {
1144 session->send_seq = val ? -1 : 0;
1146 struct sock *ssk = ps->sock;
1147 struct pppox_sock *po = pppox_sk(ssk);
1148 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1149 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1151 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1152 "%s: set send_seq=%d\n", session->name, session->send_seq);
1155 case PPPOL2TP_SO_LNSMODE:
1156 if ((val != 0) && (val != 1)) {
1160 session->lns_mode = val ? -1 : 0;
1161 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1162 "%s: set lns_mode=%d\n", session->name, session->lns_mode);
1165 case PPPOL2TP_SO_DEBUG:
1166 session->debug = val;
1167 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1168 "%s: set debug=%x\n", session->name, session->debug);
1171 case PPPOL2TP_SO_REORDERTO:
1172 session->reorder_timeout = msecs_to_jiffies(val);
1173 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1174 "%s: set reorder_timeout=%d\n", session->name, session->reorder_timeout);
1185 /* Main setsockopt() entry point.
1186 * Does API checks, then calls either the tunnel or session setsockopt
1187 * handler, according to whether the PPPoL2TP socket is a for a regular
1188 * session or the special tunnel type.
1190 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1191 char __user *optval, unsigned int optlen)
1193 struct sock *sk = sock->sk;
1194 struct l2tp_session *session;
1195 struct l2tp_tunnel *tunnel;
1196 struct pppol2tp_session *ps;
1200 if (level != SOL_PPPOL2TP)
1201 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1203 if (optlen < sizeof(int))
1206 if (get_user(val, (int __user *)optval))
1210 if (sk->sk_user_data == NULL)
1213 /* Get session context from the socket */
1215 session = pppol2tp_sock_to_session(sk);
1216 if (session == NULL)
1219 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1221 ps = l2tp_session_priv(session);
1222 if ((session->session_id == 0) &&
1223 (session->peer_session_id == 0)) {
1225 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1229 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1230 sock_put(ps->tunnel_sock);
1232 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1242 /* Tunnel getsockopt helper. Called with sock locked.
1244 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1245 struct l2tp_tunnel *tunnel,
1246 int optname, int *val)
1251 case PPPOL2TP_SO_DEBUG:
1252 *val = tunnel->debug;
1253 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1254 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
1265 /* Session getsockopt helper. Called with sock locked.
1267 static int pppol2tp_session_getsockopt(struct sock *sk,
1268 struct l2tp_session *session,
1269 int optname, int *val)
1274 case PPPOL2TP_SO_RECVSEQ:
1275 *val = session->recv_seq;
1276 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1277 "%s: get recv_seq=%d\n", session->name, *val);
1280 case PPPOL2TP_SO_SENDSEQ:
1281 *val = session->send_seq;
1282 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1283 "%s: get send_seq=%d\n", session->name, *val);
1286 case PPPOL2TP_SO_LNSMODE:
1287 *val = session->lns_mode;
1288 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1289 "%s: get lns_mode=%d\n", session->name, *val);
1292 case PPPOL2TP_SO_DEBUG:
1293 *val = session->debug;
1294 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1295 "%s: get debug=%d\n", session->name, *val);
1298 case PPPOL2TP_SO_REORDERTO:
1299 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1300 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1301 "%s: get reorder_timeout=%d\n", session->name, *val);
1311 /* Main getsockopt() entry point.
1312 * Does API checks, then calls either the tunnel or session getsockopt
1313 * handler, according to whether the PPPoX socket is a for a regular session
1314 * or the special tunnel type.
1316 static int pppol2tp_getsockopt(struct socket *sock, int level,
1317 int optname, char __user *optval, int __user *optlen)
1319 struct sock *sk = sock->sk;
1320 struct l2tp_session *session;
1321 struct l2tp_tunnel *tunnel;
1324 struct pppol2tp_session *ps;
1326 if (level != SOL_PPPOL2TP)
1327 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1329 if (get_user(len, (int __user *) optlen))
1332 len = min_t(unsigned int, len, sizeof(int));
1338 if (sk->sk_user_data == NULL)
1341 /* Get the session context */
1343 session = pppol2tp_sock_to_session(sk);
1344 if (session == NULL)
1347 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1348 ps = l2tp_session_priv(session);
1349 if ((session->session_id == 0) &&
1350 (session->peer_session_id == 0)) {
1352 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1356 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1357 sock_put(ps->tunnel_sock);
1359 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1362 if (put_user(len, (int __user *) optlen))
1365 if (copy_to_user((void __user *) optval, &val, len))
1376 /*****************************************************************************
1377 * /proc filesystem for debug
1378 *****************************************************************************/
1380 static unsigned int pppol2tp_net_id;
1382 #ifdef CONFIG_PROC_FS
1384 struct pppol2tp_seq_data {
1385 struct seq_net_private p;
1386 int tunnel_idx; /* current tunnel */
1387 int session_idx; /* index of session within current tunnel */
1388 struct l2tp_tunnel *tunnel;
1389 struct l2tp_session *session; /* NULL means get next tunnel */
1392 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1394 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1398 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1400 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1402 if (pd->session == NULL) {
1403 pd->session_idx = 0;
1404 pppol2tp_next_tunnel(net, pd);
1408 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1410 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1417 BUG_ON(m->private == NULL);
1419 net = seq_file_net(m);
1421 if (pd->tunnel == NULL)
1422 pppol2tp_next_tunnel(net, pd);
1424 pppol2tp_next_session(net, pd);
1426 /* NULL tunnel and session indicates end of list */
1427 if ((pd->tunnel == NULL) && (pd->session == NULL))
1434 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1440 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1445 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1447 struct l2tp_tunnel *tunnel = v;
1449 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1451 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1452 atomic_read(&tunnel->ref_count) - 1);
1453 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1455 (unsigned long long)tunnel->stats.tx_packets,
1456 (unsigned long long)tunnel->stats.tx_bytes,
1457 (unsigned long long)tunnel->stats.tx_errors,
1458 (unsigned long long)tunnel->stats.rx_packets,
1459 (unsigned long long)tunnel->stats.rx_bytes,
1460 (unsigned long long)tunnel->stats.rx_errors);
1463 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1465 struct l2tp_session *session = v;
1466 struct l2tp_tunnel *tunnel = session->tunnel;
1467 struct pppol2tp_session *ps = l2tp_session_priv(session);
1472 struct inet_sock *inet = inet_sk(tunnel->sock);
1473 ip = ntohl(inet->inet_saddr);
1474 port = ntohs(inet->inet_sport);
1477 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1478 "%04X/%04X %d %c\n",
1479 session->name, ip, port,
1481 session->session_id,
1482 tunnel->peer_tunnel_id,
1483 session->peer_session_id,
1485 (session == ps->sock->sk_user_data) ?
1487 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1488 session->mtu, session->mru,
1489 session->recv_seq ? 'R' : '-',
1490 session->send_seq ? 'S' : '-',
1491 session->lns_mode ? "LNS" : "LAC",
1493 jiffies_to_msecs(session->reorder_timeout));
1494 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1495 session->nr, session->ns,
1496 (unsigned long long)session->stats.tx_packets,
1497 (unsigned long long)session->stats.tx_bytes,
1498 (unsigned long long)session->stats.tx_errors,
1499 (unsigned long long)session->stats.rx_packets,
1500 (unsigned long long)session->stats.rx_bytes,
1501 (unsigned long long)session->stats.rx_errors);
1504 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1506 struct pppol2tp_seq_data *pd = v;
1508 /* display header on line 1 */
1509 if (v == SEQ_START_TOKEN) {
1510 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1511 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1512 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1513 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1514 "dest-tid/sid state user-data-ok\n");
1515 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1516 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1520 /* Show the tunnel or session context.
1522 if (pd->session == NULL)
1523 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1525 pppol2tp_seq_session_show(m, pd->session);
1531 static const struct seq_operations pppol2tp_seq_ops = {
1532 .start = pppol2tp_seq_start,
1533 .next = pppol2tp_seq_next,
1534 .stop = pppol2tp_seq_stop,
1535 .show = pppol2tp_seq_show,
1538 /* Called when our /proc file is opened. We allocate data for use when
1539 * iterating our tunnel / session contexts and store it in the private
1540 * data of the seq_file.
1542 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1544 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1545 sizeof(struct pppol2tp_seq_data));
1548 static const struct file_operations pppol2tp_proc_fops = {
1549 .owner = THIS_MODULE,
1550 .open = pppol2tp_proc_open,
1552 .llseek = seq_lseek,
1553 .release = seq_release_net,
1556 #endif /* CONFIG_PROC_FS */
1558 /*****************************************************************************
1560 *****************************************************************************/
1562 static __net_init int pppol2tp_init_net(struct net *net)
1564 struct proc_dir_entry *pde;
1567 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1577 static __net_exit void pppol2tp_exit_net(struct net *net)
1579 proc_net_remove(net, "pppol2tp");
1582 static struct pernet_operations pppol2tp_net_ops = {
1583 .init = pppol2tp_init_net,
1584 .exit = pppol2tp_exit_net,
1585 .id = &pppol2tp_net_id,
1588 /*****************************************************************************
1590 *****************************************************************************/
1592 static const struct proto_ops pppol2tp_ops = {
1594 .owner = THIS_MODULE,
1595 .release = pppol2tp_release,
1596 .bind = sock_no_bind,
1597 .connect = pppol2tp_connect,
1598 .socketpair = sock_no_socketpair,
1599 .accept = sock_no_accept,
1600 .getname = pppol2tp_getname,
1601 .poll = datagram_poll,
1602 .listen = sock_no_listen,
1603 .shutdown = sock_no_shutdown,
1604 .setsockopt = pppol2tp_setsockopt,
1605 .getsockopt = pppol2tp_getsockopt,
1606 .sendmsg = pppol2tp_sendmsg,
1607 .recvmsg = pppol2tp_recvmsg,
1608 .mmap = sock_no_mmap,
1609 .ioctl = pppox_ioctl,
1612 static struct pppox_proto pppol2tp_proto = {
1613 .create = pppol2tp_create,
1614 .ioctl = pppol2tp_ioctl
1617 static int __init pppol2tp_init(void)
1621 err = register_pernet_device(&pppol2tp_net_ops);
1625 err = proto_register(&pppol2tp_sk_proto, 0);
1627 goto out_unregister_pppol2tp_pernet;
1629 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1631 goto out_unregister_pppol2tp_proto;
1633 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
1634 PPPOL2TP_DRV_VERSION);
1638 out_unregister_pppol2tp_proto:
1639 proto_unregister(&pppol2tp_sk_proto);
1640 out_unregister_pppol2tp_pernet:
1641 unregister_pernet_device(&pppol2tp_net_ops);
1645 static void __exit pppol2tp_exit(void)
1647 unregister_pppox_proto(PX_PROTO_OL2TP);
1648 proto_unregister(&pppol2tp_sk_proto);
1649 unregister_pernet_device(&pppol2tp_net_ops);
1652 module_init(pppol2tp_init);
1653 module_exit(pppol2tp_exit);
1655 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1656 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1657 MODULE_LICENSE("GPL");
1658 MODULE_VERSION(PPPOL2TP_DRV_VERSION);