Commit 3305f9a9 authored by David S. Miller's avatar David S. Miller

Merge branch 'l2tp-rework-pppol2tp-ioctl-handling'

Guillaume Nault says:

====================
l2tp: rework pppol2tp ioctl handling

The current ioctl() handling code can be simplified. It tests for
non-relevant conditions and uselessly holds sockets. Once useless
code is removed, it becomes even simpler to let pppol2tp_ioctl() handle
commands directly, rather than dispatch them to pppol2tp_tunnel_ioctl()
or pppol2tp_session_ioctl(). That is the approach taken by this series.

Patch #1 and #2 define helper functions aimed at simplifying the rest
of the patch set.

Patch #3 drops useless tests in pppol2p_ioctl() and avoid holding a
refcount on the socket.

Patches #4, #5 and #6 are the core of the series. They let
pppol2tp_ioctl() handle all ioctls and drop the tunnel and session
specific functions.

Then patch #6 brings a little bit of consolidation.

Finally, patch #7 takes advantage of the simplified code to make
pppol2tp sockets compatible with dev_ioctl(). Certainly not a killer
feature, but it is trivial and it is always nice to see l2tp getting
better integration with the rest of the stack.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 8a8982d1 4f5f85e9
...@@ -68,7 +68,7 @@ struct ppp_option_data { ...@@ -68,7 +68,7 @@ struct ppp_option_data {
struct pppol2tp_ioc_stats { struct pppol2tp_ioc_stats {
__u16 tunnel_id; /* redundant */ __u16 tunnel_id; /* redundant */
__u16 session_id; /* if zero, get tunnel stats */ __u16 session_id; /* if zero, get tunnel stats */
__u32 using_ipsec:1; /* valid only for session_id == 0 */ __u32 using_ipsec:1;
__aligned_u64 tx_packets; __aligned_u64 tx_packets;
__aligned_u64 tx_bytes; __aligned_u64 tx_bytes;
__aligned_u64 tx_errors; __aligned_u64 tx_errors;
......
...@@ -203,44 +203,44 @@ struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth) ...@@ -203,44 +203,44 @@ struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
} }
EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth); EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
/* Lookup a session. A new reference is held on the returned session. */ struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
struct l2tp_tunnel *tunnel,
u32 session_id)
{ {
struct hlist_head *session_list; struct hlist_head *session_list;
struct l2tp_session *session; struct l2tp_session *session;
if (!tunnel) { session_list = l2tp_session_id_hash(tunnel, session_id);
struct l2tp_net *pn = l2tp_pernet(net);
session_list = l2tp_session_id_hash_2(pn, session_id);
rcu_read_lock_bh(); read_lock_bh(&tunnel->hlist_lock);
hlist_for_each_entry_rcu(session, session_list, global_hlist) { hlist_for_each_entry(session, session_list, hlist)
if (session->session_id == session_id) { if (session->session_id == session_id) {
l2tp_session_inc_refcount(session); l2tp_session_inc_refcount(session);
rcu_read_unlock_bh(); read_unlock_bh(&tunnel->hlist_lock);
return session; return session;
}
} }
rcu_read_unlock_bh(); read_unlock_bh(&tunnel->hlist_lock);
return NULL; return NULL;
} }
EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session);
session_list = l2tp_session_id_hash(tunnel, session_id); struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
read_lock_bh(&tunnel->hlist_lock); {
hlist_for_each_entry(session, session_list, hlist) { struct hlist_head *session_list;
struct l2tp_session *session;
session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id);
rcu_read_lock_bh();
hlist_for_each_entry_rcu(session, session_list, global_hlist)
if (session->session_id == session_id) { if (session->session_id == session_id) {
l2tp_session_inc_refcount(session); l2tp_session_inc_refcount(session);
read_unlock_bh(&tunnel->hlist_lock); rcu_read_unlock_bh();
return session; return session;
} }
} rcu_read_unlock_bh();
read_unlock_bh(&tunnel->hlist_lock);
return NULL; return NULL;
} }
...@@ -872,7 +872,7 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb) ...@@ -872,7 +872,7 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
} }
/* Find the session context */ /* Find the session context */
session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id); session = l2tp_tunnel_get_session(tunnel, session_id);
if (!session || !session->recv_skb) { if (!session || !session->recv_skb) {
if (session) if (session)
l2tp_session_dec_refcount(session); l2tp_session_dec_refcount(session);
......
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
#include <net/dst.h> #include <net/dst.h>
#include <net/sock.h> #include <net/sock.h>
#ifdef CONFIG_XFRM
#include <net/xfrm.h>
#endif
/* Just some random numbers */ /* Just some random numbers */
#define L2TP_TUNNEL_MAGIC 0x42114DDA #define L2TP_TUNNEL_MAGIC 0x42114DDA
#define L2TP_SESSION_MAGIC 0x0C04EB7D #define L2TP_SESSION_MAGIC 0x0C04EB7D
...@@ -192,12 +196,12 @@ static inline void *l2tp_session_priv(struct l2tp_session *session) ...@@ -192,12 +196,12 @@ static inline void *l2tp_session_priv(struct l2tp_session *session)
struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id); struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth); struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
u32 session_id);
void l2tp_tunnel_free(struct l2tp_tunnel *tunnel); void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
struct l2tp_session *l2tp_session_get(const struct net *net, struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
struct l2tp_tunnel *tunnel,
u32 session_id);
struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth); struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
const char *ifname); const char *ifname);
...@@ -284,6 +288,21 @@ static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel) ...@@ -284,6 +288,21 @@ static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
return mtu; return mtu;
} }
#ifdef CONFIG_XFRM
static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
{
struct sock *sk = tunnel->sock;
return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
rcu_access_pointer(sk->sk_policy[1]));
}
#else
static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
{
return false;
}
#endif
#define l2tp_printk(ptr, type, func, fmt, ...) \ #define l2tp_printk(ptr, type, func, fmt, ...) \
do { \ do { \
if (((ptr)->debug) & (type)) \ if (((ptr)->debug) & (type)) \
......
...@@ -144,7 +144,7 @@ static int l2tp_ip_recv(struct sk_buff *skb) ...@@ -144,7 +144,7 @@ static int l2tp_ip_recv(struct sk_buff *skb)
} }
/* Ok, this is a data packet. Lookup the session. */ /* Ok, this is a data packet. Lookup the session. */
session = l2tp_session_get(net, NULL, session_id); session = l2tp_session_get(net, session_id);
if (!session) if (!session)
goto discard; goto discard;
......
...@@ -157,7 +157,7 @@ static int l2tp_ip6_recv(struct sk_buff *skb) ...@@ -157,7 +157,7 @@ static int l2tp_ip6_recv(struct sk_buff *skb)
} }
/* Ok, this is a data packet. Lookup the session. */ /* Ok, this is a data packet. Lookup the session. */
session = l2tp_session_get(net, NULL, session_id); session = l2tp_session_get(net, session_id);
if (!session) if (!session)
goto discard; goto discard;
......
...@@ -66,7 +66,7 @@ static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info) ...@@ -66,7 +66,7 @@ static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info)
session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]); session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
tunnel = l2tp_tunnel_get(net, tunnel_id); tunnel = l2tp_tunnel_get(net, tunnel_id);
if (tunnel) { if (tunnel) {
session = l2tp_session_get(net, tunnel, session_id); session = l2tp_tunnel_get_session(tunnel, session_id);
l2tp_tunnel_dec_refcount(tunnel); l2tp_tunnel_dec_refcount(tunnel);
} }
} }
...@@ -627,7 +627,7 @@ static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *inf ...@@ -627,7 +627,7 @@ static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *inf
&cfg); &cfg);
if (ret >= 0) { if (ret >= 0) {
session = l2tp_session_get(net, tunnel, session_id); session = l2tp_tunnel_get_session(tunnel, session_id);
if (session) { if (session) {
ret = l2tp_session_notify(&l2tp_nl_family, info, session, ret = l2tp_session_notify(&l2tp_nl_family, info, session,
L2TP_CMD_SESSION_CREATE); L2TP_CMD_SESSION_CREATE);
...@@ -710,9 +710,6 @@ static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int fl ...@@ -710,9 +710,6 @@ static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int fl
void *hdr; void *hdr;
struct nlattr *nest; struct nlattr *nest;
struct l2tp_tunnel *tunnel = session->tunnel; struct l2tp_tunnel *tunnel = session->tunnel;
struct sock *sk = NULL;
sk = tunnel->sock;
hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd); hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
if (!hdr) if (!hdr)
...@@ -738,10 +735,8 @@ static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int fl ...@@ -738,10 +735,8 @@ static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int fl
nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) || nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) || nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) || nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
#ifdef CONFIG_XFRM (l2tp_tunnel_uses_xfrm(tunnel) &&
(((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) || nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
#endif
(session->reorder_timeout && (session->reorder_timeout &&
nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
session->reorder_timeout, L2TP_ATTR_PAD))) session->reorder_timeout, L2TP_ATTR_PAD)))
......
...@@ -95,7 +95,6 @@ ...@@ -95,7 +95,6 @@
#include <net/netns/generic.h> #include <net/netns/generic.h>
#include <net/ip.h> #include <net/ip.h>
#include <net/udp.h> #include <net/udp.h>
#include <net/xfrm.h>
#include <net/inet_common.h> #include <net/inet_common.h>
#include <asm/byteorder.h> #include <asm/byteorder.h>
...@@ -758,7 +757,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, ...@@ -758,7 +757,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
if (tunnel->peer_tunnel_id == 0) if (tunnel->peer_tunnel_id == 0)
tunnel->peer_tunnel_id = info.peer_tunnel_id; tunnel->peer_tunnel_id = info.peer_tunnel_id;
session = l2tp_session_get(sock_net(sk), tunnel, info.session_id); session = l2tp_tunnel_get_session(tunnel, info.session_id);
if (session) { if (session) {
drop_refcnt = true; drop_refcnt = true;
...@@ -1027,8 +1026,10 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr, ...@@ -1027,8 +1026,10 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
****************************************************************************/ ****************************************************************************/
static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest, static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
struct l2tp_stats *stats) const struct l2tp_stats *stats)
{ {
memset(dest, 0, sizeof(*dest));
dest->tx_packets = atomic_long_read(&stats->tx_packets); dest->tx_packets = atomic_long_read(&stats->tx_packets);
dest->tx_bytes = atomic_long_read(&stats->tx_bytes); dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
dest->tx_errors = atomic_long_read(&stats->tx_errors); dest->tx_errors = atomic_long_read(&stats->tx_errors);
...@@ -1039,188 +1040,107 @@ static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest, ...@@ -1039,188 +1040,107 @@ static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
dest->rx_errors = atomic_long_read(&stats->rx_errors); dest->rx_errors = atomic_long_read(&stats->rx_errors);
} }
/* Session ioctl helper. static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
*/ struct l2tp_tunnel *tunnel)
static int pppol2tp_session_ioctl(struct l2tp_session *session,
unsigned int cmd, unsigned long arg)
{ {
int err = 0; struct l2tp_session *session;
struct sock *sk;
int val = (int) arg;
struct l2tp_tunnel *tunnel = session->tunnel;
struct pppol2tp_ioc_stats stats;
l2tp_dbg(session, L2TP_MSG_CONTROL, if (!stats->session_id) {
"%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n", pppol2tp_copy_stats(stats, &tunnel->stats);
session->name, cmd, arg); return 0;
}
sk = pppol2tp_session_get_sock(session); /* If session_id is set, search the corresponding session in the
if (!sk) * context of this tunnel and record the session's statistics.
*/
session = l2tp_tunnel_get_session(tunnel, stats->session_id);
if (!session)
return -EBADR; return -EBADR;
switch (cmd) { if (session->pwtype != L2TP_PWTYPE_PPP) {
case PPPIOCGMRU: l2tp_session_dec_refcount(session);
case PPPIOCGFLAGS: return -EBADR;
err = -EFAULT;
if (put_user(0, (int __user *)arg))
break;
err = 0;
break;
case PPPIOCSMRU:
case PPPIOCSFLAGS:
err = -EFAULT;
if (get_user(val, (int __user *)arg))
break;
err = 0;
break;
case PPPIOCGL2TPSTATS:
err = -ENXIO;
if (!(sk->sk_state & PPPOX_CONNECTED))
break;
memset(&stats, 0, sizeof(stats));
stats.tunnel_id = tunnel->tunnel_id;
stats.session_id = session->session_id;
pppol2tp_copy_stats(&stats, &session->stats);
if (copy_to_user((void __user *) arg, &stats,
sizeof(stats)))
break;
l2tp_info(session, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
session->name);
err = 0;
break;
default:
err = -ENOSYS;
break;
} }
sock_put(sk); pppol2tp_copy_stats(stats, &session->stats);
l2tp_session_dec_refcount(session);
return err; return 0;
} }
/* Tunnel ioctl helper. static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
* unsigned long arg)
* Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
* specifies a session_id, the session ioctl handler is called. This allows an
* application to retrieve session stats via a tunnel socket.
*/
static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
unsigned int cmd, unsigned long arg)
{ {
int err = 0;
struct sock *sk;
struct pppol2tp_ioc_stats stats; struct pppol2tp_ioc_stats stats;
struct l2tp_session *session;
l2tp_dbg(tunnel, L2TP_MSG_CONTROL, int val;
"%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
tunnel->name, cmd, arg);
sk = tunnel->sock;
sock_hold(sk);
switch (cmd) { switch (cmd) {
case PPPIOCGL2TPSTATS: case PPPIOCGMRU:
err = -ENXIO; case PPPIOCGFLAGS:
if (!(sk->sk_state & PPPOX_CONNECTED)) session = sock->sk->sk_user_data;
break; if (!session)
return -ENOTCONN;
if (copy_from_user(&stats, (void __user *) arg,
sizeof(stats))) {
err = -EFAULT;
break;
}
if (stats.session_id != 0) {
/* resend to session ioctl handler */
struct l2tp_session *session =
l2tp_session_get(sock_net(sk), tunnel,
stats.session_id);
if (!session) {
err = -EBADR;
break;
}
if (session->pwtype != L2TP_PWTYPE_PPP) {
l2tp_session_dec_refcount(session);
err = -EBADR;
break;
}
err = pppol2tp_session_ioctl(session, cmd, arg); /* Not defined for tunnels */
l2tp_session_dec_refcount(session); if (!session->session_id && !session->peer_session_id)
break; return -ENOSYS;
}
#ifdef CONFIG_XFRM
stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
#endif
pppol2tp_copy_stats(&stats, &tunnel->stats);
if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
err = -EFAULT;
break;
}
l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
tunnel->name);
err = 0;
break;
default: if (put_user(0, (int __user *)arg))
err = -ENOSYS; return -EFAULT;
break; break;
}
sock_put(sk);
return err;
}
/* Main ioctl() handler. case PPPIOCSMRU:
* Dispatch to tunnel or session helpers depending on the socket. case PPPIOCSFLAGS:
*/ session = sock->sk->sk_user_data;
static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd, if (!session)
unsigned long arg) return -ENOTCONN;
{
struct sock *sk = sock->sk;
struct l2tp_session *session;
struct l2tp_tunnel *tunnel;
int err;
if (!sk) /* Not defined for tunnels */
return 0; if (!session->session_id && !session->peer_session_id)
return -ENOSYS;
err = -EBADF; if (get_user(val, (int __user *)arg))
if (sock_flag(sk, SOCK_DEAD) != 0) return -EFAULT;
goto end; break;
err = -ENOTCONN; case PPPIOCGL2TPSTATS:
if ((sk->sk_user_data == NULL) || session = sock->sk->sk_user_data;
(!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)))) if (!session)
goto end; return -ENOTCONN;
/* Session 0 represents the parent tunnel */
if (!session->session_id && !session->peer_session_id) {
u32 session_id;
int err;
if (copy_from_user(&stats, (void __user *)arg,
sizeof(stats)))
return -EFAULT;
session_id = stats.session_id;
err = pppol2tp_tunnel_copy_stats(&stats,
session->tunnel);
if (err < 0)
return err;
stats.session_id = session_id;
} else {
pppol2tp_copy_stats(&stats, &session->stats);
stats.session_id = session->session_id;
}
stats.tunnel_id = session->tunnel->tunnel_id;
stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
/* Get session context from the socket */ if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
err = -EBADF; return -EFAULT;
session = pppol2tp_sock_to_session(sk); break;
if (session == NULL)
goto end;
/* Special case: if session's session_id is zero, treat ioctl as a default:
* tunnel ioctl return -ENOIOCTLCMD;
*/
if ((session->session_id == 0) &&
(session->peer_session_id == 0)) {
tunnel = session->tunnel;
err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
goto end_put_sess;
} }
err = pppol2tp_session_ioctl(session, cmd, arg); return 0;
end_put_sess:
sock_put(sk);
end:
return err;
} }
/***************************************************************************** /*****************************************************************************
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment