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 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
295 * when a user application does a sendmsg() on the session socket. L2TP and
296 * PPP headers must be inserted into the user's data.
298 static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
301 static const unsigned char ppph[2] = { 0xff, 0x03 };
302 struct sock *sk = sock->sk;
305 struct l2tp_session *session;
306 struct l2tp_tunnel *tunnel;
307 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 /* Allocate a socket buffer */
326 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
327 sizeof(struct udphdr) + session->hdr_len +
328 sizeof(ppph) + total_len,
331 goto error_put_sess_tun;
333 /* Reserve space for headers. */
334 skb_reserve(skb, NET_SKB_PAD);
335 skb_reset_network_header(skb);
336 skb_reserve(skb, sizeof(struct iphdr));
337 skb_reset_transport_header(skb);
338 skb_reserve(skb, sizeof(struct udphdr));
341 skb->data[0] = ppph[0];
342 skb->data[1] = ppph[1];
345 /* Copy user data into skb */
346 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
349 goto error_put_sess_tun;
351 skb_put(skb, total_len);
353 l2tp_xmit_skb(session, skb, session->hdr_len);
355 sock_put(ps->tunnel_sock);
360 sock_put(ps->tunnel_sock);
367 /* Transmit function called by generic PPP driver. Sends PPP frame
368 * over PPPoL2TP socket.
370 * This is almost the same as pppol2tp_sendmsg(), but rather than
371 * being called with a msghdr from userspace, it is called with a skb
374 * The supplied skb from ppp doesn't have enough headroom for the
375 * insertion of L2TP, UDP and IP headers so we need to allocate more
376 * headroom in the skb. This will create a cloned skb. But we must be
377 * careful in the error case because the caller will expect to free
378 * the skb it supplied, not our cloned skb. So we take care to always
379 * leave the original skb unfreed if we return an error.
381 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
383 static const u8 ppph[2] = { 0xff, 0x03 };
384 struct sock *sk = (struct sock *) chan->private;
386 struct l2tp_session *session;
387 struct l2tp_tunnel *tunnel;
388 struct pppol2tp_session *ps;
392 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
395 /* Get session and tunnel contexts from the socket */
396 session = pppol2tp_sock_to_session(sk);
400 ps = l2tp_session_priv(session);
401 sk_tun = ps->tunnel_sock;
404 tunnel = l2tp_sock_to_tunnel(sk_tun);
408 old_headroom = skb_headroom(skb);
409 if (skb_cow_head(skb, sizeof(ppph)))
410 goto abort_put_sess_tun;
412 new_headroom = skb_headroom(skb);
413 skb->truesize += new_headroom - old_headroom;
415 /* Setup PPP header */
416 __skb_push(skb, sizeof(ppph));
417 skb->data[0] = ppph[0];
418 skb->data[1] = ppph[1];
420 l2tp_xmit_skb(session, skb, session->hdr_len);
431 /* Free the original skb */
436 /*****************************************************************************
437 * Session (and tunnel control) socket create/destroy.
438 *****************************************************************************/
440 /* Called by l2tp_core when a session socket is being closed.
442 static void pppol2tp_session_close(struct l2tp_session *session)
444 struct pppol2tp_session *ps = l2tp_session_priv(session);
445 struct sock *sk = ps->sock;
448 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
450 if (session->session_id == 0)
456 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
457 pppox_unbind_sock(sk);
458 sk->sk_state = PPPOX_DEAD;
459 sk->sk_state_change(sk);
462 /* Purge any queued data */
463 skb_queue_purge(&sk->sk_receive_queue);
464 skb_queue_purge(&sk->sk_write_queue);
465 while ((skb = skb_dequeue(&session->reorder_q))) {
477 /* Really kill the session socket. (Called from sock_put() if
480 static void pppol2tp_session_destruct(struct sock *sk)
482 struct l2tp_session *session;
484 if (sk->sk_user_data != NULL) {
485 session = sk->sk_user_data;
489 sk->sk_user_data = NULL;
490 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
491 l2tp_session_dec_refcount(session);
498 /* Called when the PPPoX socket (session) is closed.
500 static int pppol2tp_release(struct socket *sock)
502 struct sock *sk = sock->sk;
503 struct l2tp_session *session;
511 if (sock_flag(sk, SOCK_DEAD) != 0)
514 pppox_unbind_sock(sk);
516 /* Signal the death of the socket. */
517 sk->sk_state = PPPOX_DEAD;
521 session = pppol2tp_sock_to_session(sk);
523 /* Purge any queued data */
524 skb_queue_purge(&sk->sk_receive_queue);
525 skb_queue_purge(&sk->sk_write_queue);
526 if (session != NULL) {
528 while ((skb = skb_dequeue(&session->reorder_q))) {
537 /* This will delete the session context via
538 * pppol2tp_session_destruct() if the socket's refcnt drops to
550 static struct proto pppol2tp_sk_proto = {
552 .owner = THIS_MODULE,
553 .obj_size = sizeof(struct pppox_sock),
556 static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
560 rc = l2tp_udp_encap_recv(sk, skb);
564 return NET_RX_SUCCESS;
567 /* socket() handler. Initialize a new struct sock.
569 static int pppol2tp_create(struct net *net, struct socket *sock)
574 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
578 sock_init_data(sock, sk);
580 sock->state = SS_UNCONNECTED;
581 sock->ops = &pppol2tp_ops;
583 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
584 sk->sk_protocol = PX_PROTO_OL2TP;
585 sk->sk_family = PF_PPPOX;
586 sk->sk_state = PPPOX_NONE;
587 sk->sk_type = SOCK_STREAM;
588 sk->sk_destruct = pppol2tp_session_destruct;
596 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
598 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
599 int sockaddr_len, int flags)
601 struct sock *sk = sock->sk;
602 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
603 struct sockaddr_pppol2tpv3 *sp3 = (struct sockaddr_pppol2tpv3 *) uservaddr;
604 struct pppox_sock *po = pppox_sk(sk);
605 struct l2tp_session *session = NULL;
606 struct l2tp_tunnel *tunnel;
607 struct pppol2tp_session *ps;
608 struct dst_entry *dst;
609 struct l2tp_session_cfg cfg = { 0, };
611 u32 tunnel_id, peer_tunnel_id;
612 u32 session_id, peer_session_id;
619 if (sp->sa_protocol != PX_PROTO_OL2TP)
622 /* Check for already bound sockets */
624 if (sk->sk_state & PPPOX_CONNECTED)
627 /* We don't supporting rebinding anyway */
629 if (sk->sk_user_data)
630 goto end; /* socket is already attached */
632 /* Get params from socket address. Handle L2TPv2 and L2TPv3 */
633 if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
634 fd = sp->pppol2tp.fd;
635 tunnel_id = sp->pppol2tp.s_tunnel;
636 peer_tunnel_id = sp->pppol2tp.d_tunnel;
637 session_id = sp->pppol2tp.s_session;
638 peer_session_id = sp->pppol2tp.d_session;
639 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
641 fd = sp3->pppol2tp.fd;
642 tunnel_id = sp3->pppol2tp.s_tunnel;
643 peer_tunnel_id = sp3->pppol2tp.d_tunnel;
644 session_id = sp3->pppol2tp.s_session;
645 peer_session_id = sp3->pppol2tp.d_session;
648 goto end; /* bad socket address */
651 /* Don't bind if tunnel_id is 0 */
656 /* Special case: create tunnel context if session_id and
657 * peer_session_id is 0. Otherwise look up tunnel using supplied
660 if ((session_id == 0) && (peer_session_id == 0)) {
661 error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, NULL, &tunnel);
665 tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id);
667 /* Error if we can't find the tunnel */
672 /* Error if socket is not prepped */
673 if (tunnel->sock == NULL)
677 if (tunnel->recv_payload_hook == NULL)
678 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
680 /* Check that this session doesn't already exist */
682 session = l2tp_session_find(sock_net(sk), tunnel, session_id);
686 /* Default MTU values. */
688 cfg.mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
691 cfg.debug = tunnel->debug;
693 /* Allocate and initialize a new session context. */
694 session = l2tp_session_create(sizeof(struct pppol2tp_session),
696 peer_session_id, &cfg);
697 if (session == NULL) {
702 ps = l2tp_session_priv(session);
703 ps->owner = current->pid;
705 ps->tunnel_sock = tunnel->sock;
707 session->recv_skb = pppol2tp_recv;
708 session->session_close = pppol2tp_session_close;
710 /* We need to know each time a skb is dropped from the reorder
713 session->ref = pppol2tp_session_sock_hold;
714 session->deref = pppol2tp_session_sock_put;
716 /* If PMTU discovery was enabled, use the MTU that was discovered */
717 dst = sk_dst_get(sk);
719 u32 pmtu = dst_mtu(__sk_dst_get(sk));
721 session->mtu = session->mru = pmtu -
722 PPPOL2TP_HEADER_OVERHEAD;
726 /* Special case: if source & dest session_id == 0x0000, this
727 * socket is being created to manage the tunnel. Just set up
728 * the internal context for use by ioctl() and sockopt()
731 if ((session->session_id == 0) &&
732 (session->peer_session_id == 0)) {
737 /* The only header we need to worry about is the L2TP
738 * header. This size is different depending on whether
739 * sequence numbers are enabled for the data channel.
741 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
743 po->chan.private = sk;
744 po->chan.ops = &pppol2tp_chan_ops;
745 po->chan.mtu = session->mtu;
747 error = ppp_register_net_channel(sock_net(sk), &po->chan);
752 /* This is how we get the session context from the socket. */
753 sk->sk_user_data = session;
754 sk->sk_state = PPPOX_CONNECTED;
755 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
756 "%s: created\n", session->name);
764 /* getname() support.
766 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
767 int *usockaddr_len, int peer)
771 struct l2tp_session *session;
772 struct l2tp_tunnel *tunnel;
773 struct sock *sk = sock->sk;
774 struct inet_sock *inet;
775 struct pppol2tp_session *pls;
780 if (sk->sk_state != PPPOX_CONNECTED)
784 session = pppol2tp_sock_to_session(sk);
788 pls = l2tp_session_priv(session);
789 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
790 if (tunnel == NULL) {
796 if (tunnel->version == 2) {
797 struct sockaddr_pppol2tp sp;
800 sp.sa_family = AF_PPPOX;
801 sp.sa_protocol = PX_PROTO_OL2TP;
802 sp.pppol2tp.fd = tunnel->fd;
803 sp.pppol2tp.pid = pls->owner;
804 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
805 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
806 sp.pppol2tp.s_session = session->session_id;
807 sp.pppol2tp.d_session = session->peer_session_id;
808 sp.pppol2tp.addr.sin_family = AF_INET;
809 sp.pppol2tp.addr.sin_port = inet->inet_dport;
810 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
811 memcpy(uaddr, &sp, len);
812 } else if (tunnel->version == 3) {
813 struct sockaddr_pppol2tpv3 sp;
816 sp.sa_family = AF_PPPOX;
817 sp.sa_protocol = PX_PROTO_OL2TP;
818 sp.pppol2tp.fd = tunnel->fd;
819 sp.pppol2tp.pid = pls->owner;
820 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
821 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
822 sp.pppol2tp.s_session = session->session_id;
823 sp.pppol2tp.d_session = session->peer_session_id;
824 sp.pppol2tp.addr.sin_family = AF_INET;
825 sp.pppol2tp.addr.sin_port = inet->inet_dport;
826 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
827 memcpy(uaddr, &sp, len);
830 *usockaddr_len = len;
832 sock_put(pls->tunnel_sock);
841 /****************************************************************************
844 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
845 * sockets. However, in order to control kernel tunnel features, we allow
846 * userspace to create a special "tunnel" PPPoX socket which is used for
847 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
848 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
850 ****************************************************************************/
852 static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
853 struct l2tp_stats *stats)
855 dest->tx_packets = stats->tx_packets;
856 dest->tx_bytes = stats->tx_bytes;
857 dest->tx_errors = stats->tx_errors;
858 dest->rx_packets = stats->rx_packets;
859 dest->rx_bytes = stats->rx_bytes;
860 dest->rx_seq_discards = stats->rx_seq_discards;
861 dest->rx_oos_packets = stats->rx_oos_packets;
862 dest->rx_errors = stats->rx_errors;
865 /* Session ioctl helper.
867 static int pppol2tp_session_ioctl(struct l2tp_session *session,
868 unsigned int cmd, unsigned long arg)
874 struct pppol2tp_session *ps = l2tp_session_priv(session);
875 struct l2tp_tunnel *tunnel = session->tunnel;
876 struct pppol2tp_ioc_stats stats;
878 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
879 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
880 session->name, cmd, arg);
888 if (!(sk->sk_state & PPPOX_CONNECTED))
892 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
894 ifr.ifr_mtu = session->mtu;
895 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
898 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
899 "%s: get mtu=%d\n", session->name, session->mtu);
905 if (!(sk->sk_state & PPPOX_CONNECTED))
909 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
912 session->mtu = ifr.ifr_mtu;
914 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
915 "%s: set mtu=%d\n", session->name, session->mtu);
921 if (!(sk->sk_state & PPPOX_CONNECTED))
925 if (put_user(session->mru, (int __user *) arg))
928 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
929 "%s: get mru=%d\n", session->name, session->mru);
935 if (!(sk->sk_state & PPPOX_CONNECTED))
939 if (get_user(val, (int __user *) arg))
943 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
944 "%s: set mru=%d\n", session->name, session->mru);
950 if (put_user(ps->flags, (int __user *) arg))
953 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
954 "%s: get flags=%d\n", session->name, ps->flags);
960 if (get_user(val, (int __user *) arg))
963 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
964 "%s: set flags=%d\n", session->name, ps->flags);
968 case PPPIOCGL2TPSTATS:
970 if (!(sk->sk_state & PPPOX_CONNECTED))
973 memset(&stats, 0, sizeof(stats));
974 stats.tunnel_id = tunnel->tunnel_id;
975 stats.session_id = session->session_id;
976 pppol2tp_copy_stats(&stats, &session->stats);
977 if (copy_to_user((void __user *) arg, &stats,
980 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
981 "%s: get L2TP stats\n", session->name);
995 /* Tunnel ioctl helper.
997 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
998 * specifies a session_id, the session ioctl handler is called. This allows an
999 * application to retrieve session stats via a tunnel socket.
1001 static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1002 unsigned int cmd, unsigned long arg)
1006 struct pppol2tp_ioc_stats stats;
1008 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1009 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1010 tunnel->name, cmd, arg);
1016 case PPPIOCGL2TPSTATS:
1018 if (!(sk->sk_state & PPPOX_CONNECTED))
1021 if (copy_from_user(&stats, (void __user *) arg,
1026 if (stats.session_id != 0) {
1027 /* resend to session ioctl handler */
1028 struct l2tp_session *session =
1029 l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
1030 if (session != NULL)
1031 err = pppol2tp_session_ioctl(session, cmd, arg);
1037 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1039 pppol2tp_copy_stats(&stats, &tunnel->stats);
1040 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1044 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1045 "%s: get L2TP stats\n", tunnel->name);
1059 /* Main ioctl() handler.
1060 * Dispatch to tunnel or session helpers depending on the socket.
1062 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1065 struct sock *sk = sock->sk;
1066 struct l2tp_session *session;
1067 struct l2tp_tunnel *tunnel;
1068 struct pppol2tp_session *ps;
1075 if (sock_flag(sk, SOCK_DEAD) != 0)
1079 if ((sk->sk_user_data == NULL) ||
1080 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1083 /* Get session context from the socket */
1085 session = pppol2tp_sock_to_session(sk);
1086 if (session == NULL)
1089 /* Special case: if session's session_id is zero, treat ioctl as a
1092 ps = l2tp_session_priv(session);
1093 if ((session->session_id == 0) &&
1094 (session->peer_session_id == 0)) {
1096 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1100 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1101 sock_put(ps->tunnel_sock);
1105 err = pppol2tp_session_ioctl(session, cmd, arg);
1113 /*****************************************************************************
1114 * setsockopt() / getsockopt() support.
1116 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1117 * sockets. In order to control kernel tunnel features, we allow userspace to
1118 * create a special "tunnel" PPPoX socket which is used for control only.
1119 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1120 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1121 *****************************************************************************/
1123 /* Tunnel setsockopt() helper.
1125 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1126 struct l2tp_tunnel *tunnel,
1127 int optname, int val)
1132 case PPPOL2TP_SO_DEBUG:
1133 tunnel->debug = val;
1134 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1135 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1146 /* Session setsockopt helper.
1148 static int pppol2tp_session_setsockopt(struct sock *sk,
1149 struct l2tp_session *session,
1150 int optname, int val)
1153 struct pppol2tp_session *ps = l2tp_session_priv(session);
1156 case PPPOL2TP_SO_RECVSEQ:
1157 if ((val != 0) && (val != 1)) {
1161 session->recv_seq = val ? -1 : 0;
1162 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1163 "%s: set recv_seq=%d\n", session->name, session->recv_seq);
1166 case PPPOL2TP_SO_SENDSEQ:
1167 if ((val != 0) && (val != 1)) {
1171 session->send_seq = val ? -1 : 0;
1173 struct sock *ssk = ps->sock;
1174 struct pppox_sock *po = pppox_sk(ssk);
1175 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1176 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1178 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1179 "%s: set send_seq=%d\n", session->name, session->send_seq);
1182 case PPPOL2TP_SO_LNSMODE:
1183 if ((val != 0) && (val != 1)) {
1187 session->lns_mode = val ? -1 : 0;
1188 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1189 "%s: set lns_mode=%d\n", session->name, session->lns_mode);
1192 case PPPOL2TP_SO_DEBUG:
1193 session->debug = val;
1194 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1195 "%s: set debug=%x\n", session->name, session->debug);
1198 case PPPOL2TP_SO_REORDERTO:
1199 session->reorder_timeout = msecs_to_jiffies(val);
1200 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1201 "%s: set reorder_timeout=%d\n", session->name, session->reorder_timeout);
1212 /* Main setsockopt() entry point.
1213 * Does API checks, then calls either the tunnel or session setsockopt
1214 * handler, according to whether the PPPoL2TP socket is a for a regular
1215 * session or the special tunnel type.
1217 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1218 char __user *optval, unsigned int optlen)
1220 struct sock *sk = sock->sk;
1221 struct l2tp_session *session;
1222 struct l2tp_tunnel *tunnel;
1223 struct pppol2tp_session *ps;
1227 if (level != SOL_PPPOL2TP)
1228 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1230 if (optlen < sizeof(int))
1233 if (get_user(val, (int __user *)optval))
1237 if (sk->sk_user_data == NULL)
1240 /* Get session context from the socket */
1242 session = pppol2tp_sock_to_session(sk);
1243 if (session == NULL)
1246 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1248 ps = l2tp_session_priv(session);
1249 if ((session->session_id == 0) &&
1250 (session->peer_session_id == 0)) {
1252 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1256 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1257 sock_put(ps->tunnel_sock);
1259 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1269 /* Tunnel getsockopt helper. Called with sock locked.
1271 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1272 struct l2tp_tunnel *tunnel,
1273 int optname, int *val)
1278 case PPPOL2TP_SO_DEBUG:
1279 *val = tunnel->debug;
1280 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1281 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
1292 /* Session getsockopt helper. Called with sock locked.
1294 static int pppol2tp_session_getsockopt(struct sock *sk,
1295 struct l2tp_session *session,
1296 int optname, int *val)
1301 case PPPOL2TP_SO_RECVSEQ:
1302 *val = session->recv_seq;
1303 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1304 "%s: get recv_seq=%d\n", session->name, *val);
1307 case PPPOL2TP_SO_SENDSEQ:
1308 *val = session->send_seq;
1309 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1310 "%s: get send_seq=%d\n", session->name, *val);
1313 case PPPOL2TP_SO_LNSMODE:
1314 *val = session->lns_mode;
1315 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1316 "%s: get lns_mode=%d\n", session->name, *val);
1319 case PPPOL2TP_SO_DEBUG:
1320 *val = session->debug;
1321 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1322 "%s: get debug=%d\n", session->name, *val);
1325 case PPPOL2TP_SO_REORDERTO:
1326 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1327 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1328 "%s: get reorder_timeout=%d\n", session->name, *val);
1338 /* Main getsockopt() entry point.
1339 * Does API checks, then calls either the tunnel or session getsockopt
1340 * handler, according to whether the PPPoX socket is a for a regular session
1341 * or the special tunnel type.
1343 static int pppol2tp_getsockopt(struct socket *sock, int level,
1344 int optname, char __user *optval, int __user *optlen)
1346 struct sock *sk = sock->sk;
1347 struct l2tp_session *session;
1348 struct l2tp_tunnel *tunnel;
1351 struct pppol2tp_session *ps;
1353 if (level != SOL_PPPOL2TP)
1354 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1356 if (get_user(len, (int __user *) optlen))
1359 len = min_t(unsigned int, len, sizeof(int));
1365 if (sk->sk_user_data == NULL)
1368 /* Get the session context */
1370 session = pppol2tp_sock_to_session(sk);
1371 if (session == NULL)
1374 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1375 ps = l2tp_session_priv(session);
1376 if ((session->session_id == 0) &&
1377 (session->peer_session_id == 0)) {
1379 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1383 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1384 sock_put(ps->tunnel_sock);
1386 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1389 if (put_user(len, (int __user *) optlen))
1392 if (copy_to_user((void __user *) optval, &val, len))
1403 /*****************************************************************************
1404 * /proc filesystem for debug
1405 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1406 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
1407 *****************************************************************************/
1409 static unsigned int pppol2tp_net_id;
1411 #ifdef CONFIG_PROC_FS
1413 struct pppol2tp_seq_data {
1414 struct seq_net_private p;
1415 int tunnel_idx; /* current tunnel */
1416 int session_idx; /* index of session within current tunnel */
1417 struct l2tp_tunnel *tunnel;
1418 struct l2tp_session *session; /* NULL means get next tunnel */
1421 static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1424 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1427 if (pd->tunnel == NULL)
1430 /* Ignore L2TPv3 tunnels */
1431 if (pd->tunnel->version < 3)
1436 static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1438 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1441 if (pd->session == NULL) {
1442 pd->session_idx = 0;
1443 pppol2tp_next_tunnel(net, pd);
1447 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1449 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1456 BUG_ON(m->private == NULL);
1458 net = seq_file_net(m);
1460 if (pd->tunnel == NULL)
1461 pppol2tp_next_tunnel(net, pd);
1463 pppol2tp_next_session(net, pd);
1465 /* NULL tunnel and session indicates end of list */
1466 if ((pd->tunnel == NULL) && (pd->session == NULL))
1473 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1479 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1484 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1486 struct l2tp_tunnel *tunnel = v;
1488 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1490 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1491 atomic_read(&tunnel->ref_count) - 1);
1492 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1494 (unsigned long long)tunnel->stats.tx_packets,
1495 (unsigned long long)tunnel->stats.tx_bytes,
1496 (unsigned long long)tunnel->stats.tx_errors,
1497 (unsigned long long)tunnel->stats.rx_packets,
1498 (unsigned long long)tunnel->stats.rx_bytes,
1499 (unsigned long long)tunnel->stats.rx_errors);
1502 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1504 struct l2tp_session *session = v;
1505 struct l2tp_tunnel *tunnel = session->tunnel;
1506 struct pppol2tp_session *ps = l2tp_session_priv(session);
1507 struct pppox_sock *po = pppox_sk(ps->sock);
1512 struct inet_sock *inet = inet_sk(tunnel->sock);
1513 ip = ntohl(inet->inet_saddr);
1514 port = ntohs(inet->inet_sport);
1517 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1518 "%04X/%04X %d %c\n",
1519 session->name, ip, port,
1521 session->session_id,
1522 tunnel->peer_tunnel_id,
1523 session->peer_session_id,
1525 (session == ps->sock->sk_user_data) ?
1527 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1528 session->mtu, session->mru,
1529 session->recv_seq ? 'R' : '-',
1530 session->send_seq ? 'S' : '-',
1531 session->lns_mode ? "LNS" : "LAC",
1533 jiffies_to_msecs(session->reorder_timeout));
1534 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1535 session->nr, session->ns,
1536 (unsigned long long)session->stats.tx_packets,
1537 (unsigned long long)session->stats.tx_bytes,
1538 (unsigned long long)session->stats.tx_errors,
1539 (unsigned long long)session->stats.rx_packets,
1540 (unsigned long long)session->stats.rx_bytes,
1541 (unsigned long long)session->stats.rx_errors);
1544 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
1547 static int pppol2tp_seq_show(struct seq_file *m, void *v)
1549 struct pppol2tp_seq_data *pd = v;
1551 /* display header on line 1 */
1552 if (v == SEQ_START_TOKEN) {
1553 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1554 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1555 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1556 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1557 "dest-tid/sid state user-data-ok\n");
1558 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1559 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1563 /* Show the tunnel or session context.
1565 if (pd->session == NULL)
1566 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1568 pppol2tp_seq_session_show(m, pd->session);
1574 static const struct seq_operations pppol2tp_seq_ops = {
1575 .start = pppol2tp_seq_start,
1576 .next = pppol2tp_seq_next,
1577 .stop = pppol2tp_seq_stop,
1578 .show = pppol2tp_seq_show,
1581 /* Called when our /proc file is opened. We allocate data for use when
1582 * iterating our tunnel / session contexts and store it in the private
1583 * data of the seq_file.
1585 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1587 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1588 sizeof(struct pppol2tp_seq_data));
1591 static const struct file_operations pppol2tp_proc_fops = {
1592 .owner = THIS_MODULE,
1593 .open = pppol2tp_proc_open,
1595 .llseek = seq_lseek,
1596 .release = seq_release_net,
1599 #endif /* CONFIG_PROC_FS */
1601 /*****************************************************************************
1603 *****************************************************************************/
1605 static __net_init int pppol2tp_init_net(struct net *net)
1607 struct proc_dir_entry *pde;
1610 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1620 static __net_exit void pppol2tp_exit_net(struct net *net)
1622 proc_net_remove(net, "pppol2tp");
1625 static struct pernet_operations pppol2tp_net_ops = {
1626 .init = pppol2tp_init_net,
1627 .exit = pppol2tp_exit_net,
1628 .id = &pppol2tp_net_id,
1631 /*****************************************************************************
1633 *****************************************************************************/
1635 static const struct proto_ops pppol2tp_ops = {
1637 .owner = THIS_MODULE,
1638 .release = pppol2tp_release,
1639 .bind = sock_no_bind,
1640 .connect = pppol2tp_connect,
1641 .socketpair = sock_no_socketpair,
1642 .accept = sock_no_accept,
1643 .getname = pppol2tp_getname,
1644 .poll = datagram_poll,
1645 .listen = sock_no_listen,
1646 .shutdown = sock_no_shutdown,
1647 .setsockopt = pppol2tp_setsockopt,
1648 .getsockopt = pppol2tp_getsockopt,
1649 .sendmsg = pppol2tp_sendmsg,
1650 .recvmsg = pppol2tp_recvmsg,
1651 .mmap = sock_no_mmap,
1652 .ioctl = pppox_ioctl,
1655 static struct pppox_proto pppol2tp_proto = {
1656 .create = pppol2tp_create,
1657 .ioctl = pppol2tp_ioctl
1660 static int __init pppol2tp_init(void)
1664 err = register_pernet_device(&pppol2tp_net_ops);
1668 err = proto_register(&pppol2tp_sk_proto, 0);
1670 goto out_unregister_pppol2tp_pernet;
1672 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1674 goto out_unregister_pppol2tp_proto;
1676 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
1677 PPPOL2TP_DRV_VERSION);
1681 out_unregister_pppol2tp_proto:
1682 proto_unregister(&pppol2tp_sk_proto);
1683 out_unregister_pppol2tp_pernet:
1684 unregister_pernet_device(&pppol2tp_net_ops);
1688 static void __exit pppol2tp_exit(void)
1690 unregister_pppox_proto(PX_PROTO_OL2TP);
1691 proto_unregister(&pppol2tp_sk_proto);
1692 unregister_pernet_device(&pppol2tp_net_ops);
1695 module_init(pppol2tp_init);
1696 module_exit(pppol2tp_exit);
1698 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1699 MODULE_DESCRIPTION("PPP over L2TP over UDP");
1700 MODULE_LICENSE("GPL");
1701 MODULE_VERSION(PPPOL2TP_DRV_VERSION);