Commit e5c1e519 authored by David S. Miller's avatar David S. Miller

Merge branch 'l2tp_session_find-fixes'

Guillaume Nault says:

====================
l2tp: fix usage of l2tp_session_find()

l2tp_session_find() doesn't take a reference on the session returned to
its caller. Virtually all l2tp_session_find() users are racy, either
because the session can disappear from under them or because they take
a reference too late. This leads to bugs like 'use after free' or
failure to notice duplicate session creations.

In some cases, taking a reference on the session is not enough. The
special callbacks .ref() and .deref() also have to be called in cases
where the PPP pseudo-wire uses the socket associated with the session.
Therefore, when looking up a session, we also have to pass a flag
indicating if the .ref() callback has to be called.

In the future, we probably could drop the .ref() and .deref() callbacks
entirely by protecting the .sock field of struct pppol2tp_session with
RCU, thus allowing it to be freed and set to NULL even if the L2TP
session is still alive.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents afe89962 2777e2ab
...@@ -278,6 +278,55 @@ struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunn ...@@ -278,6 +278,55 @@ struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunn
} }
EXPORT_SYMBOL_GPL(l2tp_session_find); EXPORT_SYMBOL_GPL(l2tp_session_find);
/* Like l2tp_session_find() but takes a reference on the returned session.
* Optionally calls session->ref() too if do_ref is true.
*/
struct l2tp_session *l2tp_session_get(struct net *net,
struct l2tp_tunnel *tunnel,
u32 session_id, bool do_ref)
{
struct hlist_head *session_list;
struct l2tp_session *session;
if (!tunnel) {
struct l2tp_net *pn = l2tp_pernet(net);
session_list = l2tp_session_id_hash_2(pn, session_id);
rcu_read_lock_bh();
hlist_for_each_entry_rcu(session, session_list, global_hlist) {
if (session->session_id == session_id) {
l2tp_session_inc_refcount(session);
if (do_ref && session->ref)
session->ref(session);
rcu_read_unlock_bh();
return session;
}
}
rcu_read_unlock_bh();
return NULL;
}
session_list = l2tp_session_id_hash(tunnel, session_id);
read_lock_bh(&tunnel->hlist_lock);
hlist_for_each_entry(session, session_list, hlist) {
if (session->session_id == session_id) {
l2tp_session_inc_refcount(session);
if (do_ref && session->ref)
session->ref(session);
read_unlock_bh(&tunnel->hlist_lock);
return session;
}
}
read_unlock_bh(&tunnel->hlist_lock);
return NULL;
}
EXPORT_SYMBOL_GPL(l2tp_session_get);
struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth) struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
{ {
int hash; int hash;
...@@ -303,7 +352,8 @@ EXPORT_SYMBOL_GPL(l2tp_session_find_nth); ...@@ -303,7 +352,8 @@ EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
/* Lookup a session by interface name. /* Lookup a session by interface name.
* This is very inefficient but is only used by management interfaces. * This is very inefficient but is only used by management interfaces.
*/ */
struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname) struct l2tp_session *l2tp_session_get_by_ifname(struct net *net, char *ifname,
bool do_ref)
{ {
struct l2tp_net *pn = l2tp_pernet(net); struct l2tp_net *pn = l2tp_pernet(net);
int hash; int hash;
...@@ -313,7 +363,11 @@ struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname) ...@@ -313,7 +363,11 @@ struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
if (!strcmp(session->ifname, ifname)) { if (!strcmp(session->ifname, ifname)) {
l2tp_session_inc_refcount(session);
if (do_ref && session->ref)
session->ref(session);
rcu_read_unlock_bh(); rcu_read_unlock_bh();
return session; return session;
} }
} }
...@@ -323,7 +377,49 @@ struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname) ...@@ -323,7 +377,49 @@ struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
return NULL; return NULL;
} }
EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname); EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel,
struct l2tp_session *session)
{
struct l2tp_session *session_walk;
struct hlist_head *g_head;
struct hlist_head *head;
struct l2tp_net *pn;
head = l2tp_session_id_hash(tunnel, session->session_id);
write_lock_bh(&tunnel->hlist_lock);
hlist_for_each_entry(session_walk, head, hlist)
if (session_walk->session_id == session->session_id)
goto exist;
if (tunnel->version == L2TP_HDR_VER_3) {
pn = l2tp_pernet(tunnel->l2tp_net);
g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
session->session_id);
spin_lock_bh(&pn->l2tp_session_hlist_lock);
hlist_for_each_entry(session_walk, g_head, global_hlist)
if (session_walk->session_id == session->session_id)
goto exist_glob;
hlist_add_head_rcu(&session->global_hlist, g_head);
spin_unlock_bh(&pn->l2tp_session_hlist_lock);
}
hlist_add_head(&session->hlist, head);
write_unlock_bh(&tunnel->hlist_lock);
return 0;
exist_glob:
spin_unlock_bh(&pn->l2tp_session_hlist_lock);
exist:
write_unlock_bh(&tunnel->hlist_lock);
return -EEXIST;
}
/* Lookup a tunnel by id /* Lookup a tunnel by id
*/ */
...@@ -633,6 +729,9 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) ...@@ -633,6 +729,9 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
* a data (not control) frame before coming here. Fields up to the * a data (not control) frame before coming here. Fields up to the
* session-id have already been parsed and ptr points to the data * session-id have already been parsed and ptr points to the data
* after the session-id. * after the session-id.
*
* session->ref() must have been called prior to l2tp_recv_common().
* session->deref() will be called automatically after skb is processed.
*/ */
void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
unsigned char *ptr, unsigned char *optr, u16 hdrflags, unsigned char *ptr, unsigned char *optr, u16 hdrflags,
...@@ -642,14 +741,6 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, ...@@ -642,14 +741,6 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
int offset; int offset;
u32 ns, nr; u32 ns, nr;
/* The ref count is increased since we now hold a pointer to
* the session. Take care to decrement the refcnt when exiting
* this function from now on...
*/
l2tp_session_inc_refcount(session);
if (session->ref)
(*session->ref)(session);
/* Parse and check optional cookie */ /* Parse and check optional cookie */
if (session->peer_cookie_len > 0) { if (session->peer_cookie_len > 0) {
if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
...@@ -802,8 +893,6 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, ...@@ -802,8 +893,6 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
/* Try to dequeue as many skbs from reorder_q as we can. */ /* Try to dequeue as many skbs from reorder_q as we can. */
l2tp_recv_dequeue(session); l2tp_recv_dequeue(session);
l2tp_session_dec_refcount(session);
return; return;
discard: discard:
...@@ -812,8 +901,6 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, ...@@ -812,8 +901,6 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
if (session->deref) if (session->deref)
(*session->deref)(session); (*session->deref)(session);
l2tp_session_dec_refcount(session);
} }
EXPORT_SYMBOL(l2tp_recv_common); EXPORT_SYMBOL(l2tp_recv_common);
...@@ -920,8 +1007,14 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, ...@@ -920,8 +1007,14 @@ 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_find(tunnel->l2tp_net, tunnel, session_id); session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
if (!session || !session->recv_skb) { if (!session || !session->recv_skb) {
if (session) {
if (session->deref)
session->deref(session);
l2tp_session_dec_refcount(session);
}
/* Not found? Pass to userspace to deal with */ /* Not found? Pass to userspace to deal with */
l2tp_info(tunnel, L2TP_MSG_DATA, l2tp_info(tunnel, L2TP_MSG_DATA,
"%s: no session found (%u/%u). Passing up.\n", "%s: no session found (%u/%u). Passing up.\n",
...@@ -930,6 +1023,7 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, ...@@ -930,6 +1023,7 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
} }
l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
l2tp_session_dec_refcount(session);
return 0; return 0;
...@@ -1738,6 +1832,7 @@ EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); ...@@ -1738,6 +1832,7 @@ EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
{ {
struct l2tp_session *session; struct l2tp_session *session;
int err;
session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
if (session != NULL) { if (session != NULL) {
...@@ -1793,6 +1888,13 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn ...@@ -1793,6 +1888,13 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn
l2tp_session_set_header_len(session, tunnel->version); l2tp_session_set_header_len(session, tunnel->version);
err = l2tp_session_add_to_tunnel(tunnel, session);
if (err) {
kfree(session);
return ERR_PTR(err);
}
/* Bump the reference count. The session context is deleted /* Bump the reference count. The session context is deleted
* only when this drops to zero. * only when this drops to zero.
*/ */
...@@ -1802,28 +1904,14 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn ...@@ -1802,28 +1904,14 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn
/* Ensure tunnel socket isn't deleted */ /* Ensure tunnel socket isn't deleted */
sock_hold(tunnel->sock); sock_hold(tunnel->sock);
/* Add session to the tunnel's hash list */
write_lock_bh(&tunnel->hlist_lock);
hlist_add_head(&session->hlist,
l2tp_session_id_hash(tunnel, session_id));
write_unlock_bh(&tunnel->hlist_lock);
/* And to the global session list if L2TPv3 */
if (tunnel->version != L2TP_HDR_VER_2) {
struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
spin_lock_bh(&pn->l2tp_session_hlist_lock);
hlist_add_head_rcu(&session->global_hlist,
l2tp_session_id_hash_2(pn, session_id));
spin_unlock_bh(&pn->l2tp_session_hlist_lock);
}
/* Ignore management session in session count value */ /* Ignore management session in session count value */
if (session->session_id != 0) if (session->session_id != 0)
atomic_inc(&l2tp_session_count); atomic_inc(&l2tp_session_count);
return session;
} }
return session; return ERR_PTR(-ENOMEM);
} }
EXPORT_SYMBOL_GPL(l2tp_session_create); EXPORT_SYMBOL_GPL(l2tp_session_create);
......
...@@ -230,11 +230,15 @@ static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk) ...@@ -230,11 +230,15 @@ static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
return tunnel; return tunnel;
} }
struct l2tp_session *l2tp_session_get(struct net *net,
struct l2tp_tunnel *tunnel,
u32 session_id, bool do_ref);
struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_session *l2tp_session_find(struct net *net,
struct l2tp_tunnel *tunnel, struct l2tp_tunnel *tunnel,
u32 session_id); u32 session_id);
struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth); struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname); struct l2tp_session *l2tp_session_get_by_ifname(struct net *net, char *ifname,
bool do_ref);
struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id); struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);
struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth); struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth);
......
...@@ -221,12 +221,6 @@ static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 p ...@@ -221,12 +221,6 @@ static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 p
goto out; goto out;
} }
session = l2tp_session_find(net, tunnel, session_id);
if (session) {
rc = -EEXIST;
goto out;
}
if (cfg->ifname) { if (cfg->ifname) {
dev = dev_get_by_name(net, cfg->ifname); dev = dev_get_by_name(net, cfg->ifname);
if (dev) { if (dev) {
...@@ -240,8 +234,8 @@ static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 p ...@@ -240,8 +234,8 @@ static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 p
session = l2tp_session_create(sizeof(*spriv), tunnel, session_id, session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
peer_session_id, cfg); peer_session_id, cfg);
if (!session) { if (IS_ERR(session)) {
rc = -ENOMEM; rc = PTR_ERR(session);
goto out; goto out;
} }
......
...@@ -143,19 +143,19 @@ static int l2tp_ip_recv(struct sk_buff *skb) ...@@ -143,19 +143,19 @@ 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_find(net, NULL, session_id); session = l2tp_session_get(net, NULL, session_id, true);
if (session == NULL) if (!session)
goto discard; goto discard;
tunnel = session->tunnel; tunnel = session->tunnel;
if (tunnel == NULL) if (!tunnel)
goto discard; goto discard_sess;
/* Trace packet contents, if enabled */ /* Trace packet contents, if enabled */
if (tunnel->debug & L2TP_MSG_DATA) { if (tunnel->debug & L2TP_MSG_DATA) {
length = min(32u, skb->len); length = min(32u, skb->len);
if (!pskb_may_pull(skb, length)) if (!pskb_may_pull(skb, length))
goto discard; goto discard_sess;
/* Point to L2TP header */ /* Point to L2TP header */
optr = ptr = skb->data; optr = ptr = skb->data;
...@@ -165,6 +165,7 @@ static int l2tp_ip_recv(struct sk_buff *skb) ...@@ -165,6 +165,7 @@ static int l2tp_ip_recv(struct sk_buff *skb)
} }
l2tp_recv_common(session, skb, ptr, optr, 0, skb->len, tunnel->recv_payload_hook); l2tp_recv_common(session, skb, ptr, optr, 0, skb->len, tunnel->recv_payload_hook);
l2tp_session_dec_refcount(session);
return 0; return 0;
...@@ -203,6 +204,12 @@ static int l2tp_ip_recv(struct sk_buff *skb) ...@@ -203,6 +204,12 @@ static int l2tp_ip_recv(struct sk_buff *skb)
return sk_receive_skb(sk, skb, 1); return sk_receive_skb(sk, skb, 1);
discard_sess:
if (session->deref)
session->deref(session);
l2tp_session_dec_refcount(session);
goto discard;
discard_put: discard_put:
sock_put(sk); sock_put(sk);
......
...@@ -156,19 +156,19 @@ static int l2tp_ip6_recv(struct sk_buff *skb) ...@@ -156,19 +156,19 @@ 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_find(net, NULL, session_id); session = l2tp_session_get(net, NULL, session_id, true);
if (session == NULL) if (!session)
goto discard; goto discard;
tunnel = session->tunnel; tunnel = session->tunnel;
if (tunnel == NULL) if (!tunnel)
goto discard; goto discard_sess;
/* Trace packet contents, if enabled */ /* Trace packet contents, if enabled */
if (tunnel->debug & L2TP_MSG_DATA) { if (tunnel->debug & L2TP_MSG_DATA) {
length = min(32u, skb->len); length = min(32u, skb->len);
if (!pskb_may_pull(skb, length)) if (!pskb_may_pull(skb, length))
goto discard; goto discard_sess;
/* Point to L2TP header */ /* Point to L2TP header */
optr = ptr = skb->data; optr = ptr = skb->data;
...@@ -179,6 +179,8 @@ static int l2tp_ip6_recv(struct sk_buff *skb) ...@@ -179,6 +179,8 @@ static int l2tp_ip6_recv(struct sk_buff *skb)
l2tp_recv_common(session, skb, ptr, optr, 0, skb->len, l2tp_recv_common(session, skb, ptr, optr, 0, skb->len,
tunnel->recv_payload_hook); tunnel->recv_payload_hook);
l2tp_session_dec_refcount(session);
return 0; return 0;
pass_up: pass_up:
...@@ -216,6 +218,12 @@ static int l2tp_ip6_recv(struct sk_buff *skb) ...@@ -216,6 +218,12 @@ static int l2tp_ip6_recv(struct sk_buff *skb)
return sk_receive_skb(sk, skb, 1); return sk_receive_skb(sk, skb, 1);
discard_sess:
if (session->deref)
session->deref(session);
l2tp_session_dec_refcount(session);
goto discard;
discard_put: discard_put:
sock_put(sk); sock_put(sk);
......
...@@ -48,7 +48,8 @@ static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, ...@@ -48,7 +48,8 @@ static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
/* Accessed under genl lock */ /* Accessed under genl lock */
static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX]; static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info) static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info,
bool do_ref)
{ {
u32 tunnel_id; u32 tunnel_id;
u32 session_id; u32 session_id;
...@@ -59,14 +60,15 @@ static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info) ...@@ -59,14 +60,15 @@ static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
if (info->attrs[L2TP_ATTR_IFNAME]) { if (info->attrs[L2TP_ATTR_IFNAME]) {
ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]); ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
session = l2tp_session_find_by_ifname(net, ifname); session = l2tp_session_get_by_ifname(net, ifname, do_ref);
} else if ((info->attrs[L2TP_ATTR_SESSION_ID]) && } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
(info->attrs[L2TP_ATTR_CONN_ID])) { (info->attrs[L2TP_ATTR_CONN_ID])) {
tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]); tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
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_find(net, tunnel_id); tunnel = l2tp_tunnel_find(net, tunnel_id);
if (tunnel) if (tunnel)
session = l2tp_session_find(net, tunnel, session_id); session = l2tp_session_get(net, tunnel, session_id,
do_ref);
} }
return session; return session;
...@@ -642,10 +644,12 @@ static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *inf ...@@ -642,10 +644,12 @@ static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *inf
session_id, peer_session_id, &cfg); session_id, peer_session_id, &cfg);
if (ret >= 0) { if (ret >= 0) {
session = l2tp_session_find(net, tunnel, session_id); session = l2tp_session_get(net, tunnel, session_id, false);
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);
l2tp_session_dec_refcount(session);
}
} }
out: out:
...@@ -658,7 +662,7 @@ static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *inf ...@@ -658,7 +662,7 @@ static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *inf
struct l2tp_session *session; struct l2tp_session *session;
u16 pw_type; u16 pw_type;
session = l2tp_nl_session_find(info); session = l2tp_nl_session_get(info, true);
if (session == NULL) { if (session == NULL) {
ret = -ENODEV; ret = -ENODEV;
goto out; goto out;
...@@ -672,6 +676,10 @@ static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *inf ...@@ -672,6 +676,10 @@ static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *inf
if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete) if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session); ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
if (session->deref)
session->deref(session);
l2tp_session_dec_refcount(session);
out: out:
return ret; return ret;
} }
...@@ -681,7 +689,7 @@ static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *inf ...@@ -681,7 +689,7 @@ static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *inf
int ret = 0; int ret = 0;
struct l2tp_session *session; struct l2tp_session *session;
session = l2tp_nl_session_find(info); session = l2tp_nl_session_get(info, false);
if (session == NULL) { if (session == NULL) {
ret = -ENODEV; ret = -ENODEV;
goto out; goto out;
...@@ -716,6 +724,8 @@ static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *inf ...@@ -716,6 +724,8 @@ static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *inf
ret = l2tp_session_notify(&l2tp_nl_family, info, ret = l2tp_session_notify(&l2tp_nl_family, info,
session, L2TP_CMD_SESSION_MODIFY); session, L2TP_CMD_SESSION_MODIFY);
l2tp_session_dec_refcount(session);
out: out:
return ret; return ret;
} }
...@@ -811,29 +821,34 @@ static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info) ...@@ -811,29 +821,34 @@ static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
struct sk_buff *msg; struct sk_buff *msg;
int ret; int ret;
session = l2tp_nl_session_find(info); session = l2tp_nl_session_get(info, false);
if (session == NULL) { if (session == NULL) {
ret = -ENODEV; ret = -ENODEV;
goto out; goto err;
} }
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!msg) { if (!msg) {
ret = -ENOMEM; ret = -ENOMEM;
goto out; goto err_ref;
} }
ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq, ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
0, session, L2TP_CMD_SESSION_GET); 0, session, L2TP_CMD_SESSION_GET);
if (ret < 0) if (ret < 0)
goto err_out; goto err_ref_msg;
return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid); ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
err_out: l2tp_session_dec_refcount(session);
nlmsg_free(msg);
out: return ret;
err_ref_msg:
nlmsg_free(msg);
err_ref:
l2tp_session_dec_refcount(session);
err:
return ret; return ret;
} }
......
...@@ -583,6 +583,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, ...@@ -583,6 +583,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
int error = 0; int error = 0;
u32 tunnel_id, peer_tunnel_id; u32 tunnel_id, peer_tunnel_id;
u32 session_id, peer_session_id; u32 session_id, peer_session_id;
bool drop_refcnt = false;
int ver = 2; int ver = 2;
int fd; int fd;
...@@ -684,36 +685,36 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, ...@@ -684,36 +685,36 @@ 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 = peer_tunnel_id; tunnel->peer_tunnel_id = peer_tunnel_id;
/* Create session if it doesn't already exist. We handle the session = l2tp_session_get(sock_net(sk), tunnel, session_id, false);
* case where a session was previously created by the netlink if (session) {
* interface by checking that the session doesn't already have drop_refcnt = true;
* a socket and its tunnel socket are what we expect. If any ps = l2tp_session_priv(session);
* of those checks fail, return EEXIST to the caller.
*/ /* Using a pre-existing session is fine as long as it hasn't
session = l2tp_session_find(sock_net(sk), tunnel, session_id); * been connected yet.
if (session == NULL) {
/* Default MTU must allow space for UDP/L2TP/PPP
* headers.
*/ */
cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD; if (ps->sock) {
error = -EEXIST;
goto end;
}
/* Allocate and initialize a new session context. */ /* consistency checks */
session = l2tp_session_create(sizeof(struct pppol2tp_session), if (ps->tunnel_sock != tunnel->sock) {
tunnel, session_id, error = -EEXIST;
peer_session_id, &cfg);
if (session == NULL) {
error = -ENOMEM;
goto end; goto end;
} }
} else { } else {
ps = l2tp_session_priv(session); /* Default MTU must allow space for UDP/L2TP/PPP headers */
error = -EEXIST; cfg.mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
if (ps->sock != NULL) cfg.mru = cfg.mtu;
goto end;
/* consistency checks */ session = l2tp_session_create(sizeof(struct pppol2tp_session),
if (ps->tunnel_sock != tunnel->sock) tunnel, session_id,
peer_session_id, &cfg);
if (IS_ERR(session)) {
error = PTR_ERR(session);
goto end; goto end;
}
} }
/* Associate session with its PPPoL2TP socket */ /* Associate session with its PPPoL2TP socket */
...@@ -778,6 +779,8 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, ...@@ -778,6 +779,8 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
session->name); session->name);
end: end:
if (drop_refcnt)
l2tp_session_dec_refcount(session);
release_sock(sk); release_sock(sk);
return error; return error;
...@@ -805,12 +808,6 @@ static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_i ...@@ -805,12 +808,6 @@ static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_i
if (tunnel->sock == NULL) if (tunnel->sock == NULL)
goto out; goto out;
/* Check that this session doesn't already exist */
error = -EEXIST;
session = l2tp_session_find(net, tunnel, session_id);
if (session != NULL)
goto out;
/* Default MTU values. */ /* Default MTU values. */
if (cfg->mtu == 0) if (cfg->mtu == 0)
cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD; cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
...@@ -818,12 +815,13 @@ static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_i ...@@ -818,12 +815,13 @@ static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_i
cfg->mru = cfg->mtu; cfg->mru = cfg->mtu;
/* Allocate and initialize a new session context. */ /* Allocate and initialize a new session context. */
error = -ENOMEM;
session = l2tp_session_create(sizeof(struct pppol2tp_session), session = l2tp_session_create(sizeof(struct pppol2tp_session),
tunnel, session_id, tunnel, session_id,
peer_session_id, cfg); peer_session_id, cfg);
if (session == NULL) if (IS_ERR(session)) {
error = PTR_ERR(session);
goto out; goto out;
}
ps = l2tp_session_priv(session); ps = l2tp_session_priv(session);
ps->tunnel_sock = tunnel->sock; ps->tunnel_sock = tunnel->sock;
...@@ -1141,11 +1139,18 @@ static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel, ...@@ -1141,11 +1139,18 @@ static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
if (stats.session_id != 0) { if (stats.session_id != 0) {
/* resend to session ioctl handler */ /* resend to session ioctl handler */
struct l2tp_session *session = struct l2tp_session *session =
l2tp_session_find(sock_net(sk), tunnel, stats.session_id); l2tp_session_get(sock_net(sk), tunnel,
if (session != NULL) stats.session_id, true);
err = pppol2tp_session_ioctl(session, cmd, arg);
else if (session) {
err = pppol2tp_session_ioctl(session, cmd,
arg);
if (session->deref)
session->deref(session);
l2tp_session_dec_refcount(session);
} else {
err = -EBADR; err = -EBADR;
}
break; break;
} }
#ifdef CONFIG_XFRM #ifdef CONFIG_XFRM
......
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