Commit 2e7a2806 authored by James Chapman's avatar James Chapman Committed by David S. Miller

l2tp: lookup tunnel from socket without using sk_user_data

l2tp_sk_to_tunnel derives the tunnel from sk_user_data. Instead,
lookup the tunnel by walking the tunnel IDR for a tunnel using the
indicated sock. This is slow but l2tp_sk_to_tunnel is not used in
the datapath so performance isn't critical.

l2tp_tunnel_destruct needs a variant of l2tp_sk_to_tunnel which does
not bump the tunnel refcount since the tunnel refcount is already 0.

Change l2tp_sk_to_tunnel sk arg to const since it does not modify sk.
Signed-off-by: default avatarJames Chapman <jchapman@katalix.com>
Signed-off-by: default avatarTom Parkin <tparkin@katalix.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 0a658d08
...@@ -150,15 +150,43 @@ static void l2tp_session_free(struct l2tp_session *session) ...@@ -150,15 +150,43 @@ static void l2tp_session_free(struct l2tp_session *session)
kfree(session); kfree(session);
} }
struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk) static struct l2tp_tunnel *__l2tp_sk_to_tunnel(const struct sock *sk)
{ {
struct l2tp_tunnel *tunnel = sk->sk_user_data; const struct net *net = sock_net(sk);
unsigned long tunnel_id, tmp;
struct l2tp_tunnel *tunnel;
struct l2tp_net *pn;
WARN_ON_ONCE(!rcu_read_lock_bh_held());
pn = l2tp_pernet(net);
idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
if (tunnel && tunnel->sock == sk)
return tunnel;
}
return NULL;
}
if (tunnel) struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk)
if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC)) {
return NULL; const struct net *net = sock_net(sk);
unsigned long tunnel_id, tmp;
struct l2tp_tunnel *tunnel;
struct l2tp_net *pn;
rcu_read_lock_bh();
pn = l2tp_pernet(net);
idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
if (tunnel &&
tunnel->sock == sk &&
refcount_inc_not_zero(&tunnel->ref_count)) {
rcu_read_unlock_bh();
return tunnel;
}
}
rcu_read_unlock_bh();
return tunnel; return NULL;
} }
EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel); EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel);
...@@ -1213,8 +1241,10 @@ EXPORT_SYMBOL_GPL(l2tp_xmit_skb); ...@@ -1213,8 +1241,10 @@ EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
*/ */
static void l2tp_tunnel_destruct(struct sock *sk) static void l2tp_tunnel_destruct(struct sock *sk)
{ {
struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); struct l2tp_tunnel *tunnel;
rcu_read_lock_bh();
tunnel = __l2tp_sk_to_tunnel(sk);
if (!tunnel) if (!tunnel)
goto end; goto end;
...@@ -1242,6 +1272,7 @@ static void l2tp_tunnel_destruct(struct sock *sk) ...@@ -1242,6 +1272,7 @@ static void l2tp_tunnel_destruct(struct sock *sk)
kfree_rcu(tunnel, rcu); kfree_rcu(tunnel, rcu);
end: end:
rcu_read_unlock_bh();
return; return;
} }
...@@ -1308,10 +1339,13 @@ static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) ...@@ -1308,10 +1339,13 @@ static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
/* Tunnel socket destroy hook for UDP encapsulation */ /* Tunnel socket destroy hook for UDP encapsulation */
static void l2tp_udp_encap_destroy(struct sock *sk) static void l2tp_udp_encap_destroy(struct sock *sk)
{ {
struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); struct l2tp_tunnel *tunnel;
if (tunnel) tunnel = l2tp_sk_to_tunnel(sk);
if (tunnel) {
l2tp_tunnel_delete(tunnel); l2tp_tunnel_delete(tunnel);
l2tp_tunnel_dec_refcount(tunnel);
}
} }
static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel) static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel)
......
...@@ -273,10 +273,7 @@ void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type); ...@@ -273,10 +273,7 @@ void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
/* IOCTL helper for IP encap modules. */ /* IOCTL helper for IP encap modules. */
int l2tp_ioctl(struct sock *sk, int cmd, int *karg); int l2tp_ioctl(struct sock *sk, int cmd, int *karg);
/* Extract the tunnel structure from a socket's sk_user_data pointer, struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk);
* validating the tunnel magic feather.
*/
struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk);
static inline int l2tp_get_l2specific_len(struct l2tp_session *session) static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
{ {
......
...@@ -235,14 +235,17 @@ static void l2tp_ip_close(struct sock *sk, long timeout) ...@@ -235,14 +235,17 @@ static void l2tp_ip_close(struct sock *sk, long timeout)
static void l2tp_ip_destroy_sock(struct sock *sk) static void l2tp_ip_destroy_sock(struct sock *sk)
{ {
struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); struct l2tp_tunnel *tunnel;
struct sk_buff *skb; struct sk_buff *skb;
while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
kfree_skb(skb); kfree_skb(skb);
if (tunnel) tunnel = l2tp_sk_to_tunnel(sk);
if (tunnel) {
l2tp_tunnel_delete(tunnel); l2tp_tunnel_delete(tunnel);
l2tp_tunnel_dec_refcount(tunnel);
}
} }
static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
......
...@@ -246,14 +246,17 @@ static void l2tp_ip6_close(struct sock *sk, long timeout) ...@@ -246,14 +246,17 @@ static void l2tp_ip6_close(struct sock *sk, long timeout)
static void l2tp_ip6_destroy_sock(struct sock *sk) static void l2tp_ip6_destroy_sock(struct sock *sk)
{ {
struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); struct l2tp_tunnel *tunnel;
lock_sock(sk); lock_sock(sk);
ip6_flush_pending_frames(sk); ip6_flush_pending_frames(sk);
release_sock(sk); release_sock(sk);
if (tunnel) tunnel = l2tp_sk_to_tunnel(sk);
if (tunnel) {
l2tp_tunnel_delete(tunnel); l2tp_tunnel_delete(tunnel);
l2tp_tunnel_dec_refcount(tunnel);
}
} }
static int l2tp_ip6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) static int l2tp_ip6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
......
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