When a new TIPC link comes up, a protocol publication of type TIPC_LINK_STATE (2) is created and inserted into the name table through the following call path: tipc_node_write_unlock() tipc_nametbl_publish() tipc_nametbl_insert_publ() User applications subscribing to this TIPC_LINK_STATE event will be notified when the link comes up. However, code inspection revealed that if tipc_nametbl_insert_publ() fails due to a memory allocation error, the TIPC_LINK_STATE publication is not inserted into the name table. As a result, user applications are never notified of the link-up event. Fix this issue by checking the error code returned through the output parameter of tipc_nametbl_publish(). If the error indicates a memory allocation failure, reset the link so that the TIPC_LINK_STATE publication can be retried. Fixes: b97bf3fd8f6a ("[TIPC] Initial merge") Signed-off-by: Tung Nguyen --- v3: - Free sr in tipc_service_insert_publ() on memory allocation failure. - Free sc in tipc_service_find() on memory allocation failure. - Purge xmitq in tipc_rcv() in case tipc_node_write_unlock() resets a bearer. v2: https://lore.kernel.org/netdev/20261102034207.10639-5-tung.quang.nguyen@est.tech/ net/tipc/name_distr.c | 3 +- net/tipc/name_table.c | 74 +++++++++++++++++++++++++++++++++---------- net/tipc/name_table.h | 5 +-- net/tipc/net.c | 3 +- net/tipc/node.c | 16 +++++++++- net/tipc/socket.c | 3 +- 6 files changed, 82 insertions(+), 22 deletions(-) diff --git a/net/tipc/name_distr.c b/net/tipc/name_distr.c index 1dfe8475a75d..3ea5e900cb45 100644 --- a/net/tipc/name_distr.c +++ b/net/tipc/name_distr.c @@ -285,6 +285,7 @@ static bool tipc_update_nametbl(struct net *net, struct distr_item *i, struct tipc_socket_addr sk; u32 key = ntohl(i->key); struct tipc_uaddr ua; + int err = 0; /* A peer-advertised binding with lower > upper can never be matched * or withdrawn and would leak the publication; the local bind path @@ -299,7 +300,7 @@ static bool tipc_update_nametbl(struct net *net, struct distr_item *i, sk.node = node; if (dtype == PUBLICATION) { - p = tipc_nametbl_insert_publ(net, &ua, &sk, key); + p = tipc_nametbl_insert_publ(net, &ua, &sk, key, &err); if (p) { tipc_node_subscribe(net, &p->binding_node, node); return true; diff --git a/net/tipc/name_table.c b/net/tipc/name_table.c index 73b032ba2f8d..e8b768699ab9 100644 --- a/net/tipc/name_table.c +++ b/net/tipc/name_table.c @@ -329,7 +329,7 @@ static struct service_range *tipc_service_create_range(struct tipc_service *sc, static bool tipc_service_insert_publ(struct net *net, struct tipc_service *sc, - struct publication *p) + struct publication *p, int *err) { struct tipc_subscription *sub, *tmp; struct service_range *sr; @@ -341,8 +341,11 @@ static bool tipc_service_insert_publ(struct net *net, spin_lock_bh(&sc->lock); sr = tipc_service_create_range(sc, p); - if (!sr) - goto exit; + if (!sr) { + if (err) + *err = -ENOBUFS; + goto exit; + } first = list_empty(&sr->all_publ); @@ -353,6 +356,9 @@ static bool tipc_service_insert_publ(struct net *net, pr_debug("Failed to bind duplicate %u,%u,%u/%u:%u/%u\n", p->sr.type, p->sr.lower, p->sr.upper, node, p->sk.ref, key); + if (err) + *err = -EEXIST; + goto exit; } } @@ -368,9 +374,15 @@ static bool tipc_service_insert_publ(struct net *net, } res = true; exit: - if (!res) - pr_warn("Failed to bind to %u,%u,%u\n", - p->sr.type, p->sr.lower, p->sr.upper); + if (!res) { + if (err && *err == -EEXIST) { + rb_erase_augmented(&sr->tree_node, &sc->ranges, + &sr_callbacks); + kfree(sr); + } + pr_warn_ratelimited("Failed to bind to %u,%u,%u\n", + p->sr.type, p->sr.lower, p->sr.upper); + } spin_unlock_bh(&sc->lock); return res; } @@ -478,20 +490,40 @@ static struct tipc_service *tipc_service_find(struct net *net, struct publication *tipc_nametbl_insert_publ(struct net *net, struct tipc_uaddr *ua, struct tipc_socket_addr *sk, - u32 key) + u32 key, int *err) { + bool service_created = false; struct tipc_service *sc; struct publication *p; p = tipc_publ_create(ua, sk, key); - if (!p) + if (!p) { + if (err) + *err = -ENOBUFS; return NULL; + } sc = tipc_service_find(net, ua); - if (!sc) + + if (!sc) { sc = tipc_service_create(net, ua); - if (sc && tipc_service_insert_publ(net, sc, p)) - return p; + service_created = true; + } + + if (sc) { + if (tipc_service_insert_publ(net, sc, p, err)) + return p; + + spin_lock_bh(&sc->lock); + if (service_created && err && *err == -ENOBUFS) { + hlist_del_init_rcu(&sc->service_list); + kfree_rcu(sc, rcu); + } + spin_unlock_bh(&sc->lock); + } else if (err) { + *err = -ENOBUFS; + } + kfree(p); return NULL; } @@ -540,9 +572,9 @@ struct publication *tipc_nametbl_remove_publ(struct net *net, spin_unlock_bh(&sc->lock); exit: if (!p) { - pr_err("Failed to remove unknown binding: %u,%u,%u/%u:%u/%u\n", - ua->sr.type, ua->sr.lower, ua->sr.upper, - sk->node, sk->ref, key); + pr_warn_ratelimited("Remove unknown pub: %u,%u,%u/%u:%u/%u\n", + ua->sr.type, ua->sr.lower, ua->sr.upper, + sk->node, sk->ref, key); } return p; } @@ -760,7 +792,8 @@ void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp, /* tipc_nametbl_publish - add service binding to name table */ struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua, - struct tipc_socket_addr *sk, u32 key) + struct tipc_socket_addr *sk, + u32 key, int *err) { struct name_table *nt = tipc_name_table(net); u32 max_user_pub = TIPC_MAX_PUBL - 1; @@ -768,6 +801,7 @@ struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua, struct publication *p = NULL; struct sk_buff *skb = NULL; bool protocol_type = false; + int error = 0; u32 rc_dests; if (ua->sr.type == TIPC_NODE_STATE || ua->sr.type == TIPC_LINK_STATE || @@ -788,7 +822,7 @@ struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua, } insert: - p = tipc_nametbl_insert_publ(net, ua, sk, key); + p = tipc_nametbl_insert_publ(net, ua, sk, key, &error); if (p) { /* Not count node state, link state and topology server types * so that maximum nt->local_publ_count does not prevent @@ -798,6 +832,14 @@ struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua, if (!protocol_type) nt->local_publ_count++; skb = tipc_named_publish(net, p); + /* Local-scope publication is not published (skb is NULL). + * For cluster-cope one, skb is NULL when named_prepare_buf() + * returns NULL in tipc_named_publish(). + */ + if (!skb && p->scope == TIPC_CLUSTER_SCOPE && err) + *err = -ENOBUFS; + } else if ((error == -ENOBUFS) && err) { + *err = -ENOBUFS; } rc_dests = nt->rc_dests; exit: diff --git a/net/tipc/name_table.h b/net/tipc/name_table.h index 7ff6eeebaae6..3a15dcfcc2cd 100644 --- a/net/tipc/name_table.h +++ b/net/tipc/name_table.h @@ -126,13 +126,14 @@ bool tipc_nametbl_lookup_group(struct net *net, struct tipc_uaddr *ua, void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp, struct tipc_uaddr *ua); struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua, - struct tipc_socket_addr *sk, u32 key); + struct tipc_socket_addr *sk, + u32 key, int *err); void tipc_nametbl_withdraw(struct net *net, struct tipc_uaddr *ua, struct tipc_socket_addr *sk, u32 key); struct publication *tipc_nametbl_insert_publ(struct net *net, struct tipc_uaddr *ua, struct tipc_socket_addr *sk, - u32 key); + u32 key, int *err); struct publication *tipc_nametbl_remove_publ(struct net *net, struct tipc_uaddr *ua, struct tipc_socket_addr *sk, diff --git a/net/tipc/net.c b/net/tipc/net.c index 7e65d0b0c4a8..82f6d2f6ab89 100644 --- a/net/tipc/net.c +++ b/net/tipc/net.c @@ -128,6 +128,7 @@ static void tipc_net_finalize(struct net *net, u32 addr) struct tipc_net *tn = tipc_net(net); struct tipc_socket_addr sk = {0, addr}; struct tipc_uaddr ua; + int err = 0; tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_CLUSTER_SCOPE, TIPC_NODE_STATE, addr, addr); @@ -138,7 +139,7 @@ static void tipc_net_finalize(struct net *net, u32 addr) tipc_named_reinit(net); tipc_sk_reinit(net); tipc_mon_reinit_self(net); - tipc_nametbl_publish(net, &ua, &sk, addr); + tipc_nametbl_publish(net, &ua, &sk, addr, &err); } void tipc_net_finalize_work(struct work_struct *work) diff --git a/net/tipc/node.c b/net/tipc/node.c index d84b0274fefc..870f84e2fb1c 100644 --- a/net/tipc/node.c +++ b/net/tipc/node.c @@ -402,6 +402,7 @@ static void tipc_node_write_unlock(struct tipc_node *n) struct list_head *publ_list; struct tipc_uaddr ua; u32 bearer_id, node; + int err = 0; if (likely(!flags)) { write_unlock_bh(&n->lock); @@ -429,12 +430,15 @@ static void tipc_node_write_unlock(struct tipc_node *n) if (flags & TIPC_NOTIFY_LINK_UP) { tipc_mon_peer_up(net, node, bearer_id); - tipc_nametbl_publish(net, &ua, &sk, sk.ref); + tipc_nametbl_publish(net, &ua, &sk, sk.ref, &err); } if (flags & TIPC_NOTIFY_LINK_DOWN) { tipc_mon_peer_down(net, node, bearer_id); tipc_nametbl_withdraw(net, &ua, &sk, sk.ref); } + + if (err == -ENOBUFS) + tipc_node_link_down(n, bearer_id, false); } static void tipc_node_assign_peer_net(struct tipc_node *n, u32 hash_mixes) @@ -2193,6 +2197,16 @@ void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b) } } tipc_node_write_unlock(n); + + /* tipc_node_link_down() was called in + * tipc_node_write_unlock(). + */ + tipc_node_read_lock(n); + le = &n->links[bearer_id]; + if (le->link && !tipc_link_is_up(le->link) && + !skb_queue_empty(&xmitq)) + __skb_queue_purge(&xmitq); + tipc_node_read_unlock(n); } if (unlikely(rc & TIPC_LINK_UP_EVT)) diff --git a/net/tipc/socket.c b/net/tipc/socket.c index 65cc0c4964b3..a7b046f1d1d6 100644 --- a/net/tipc/socket.c +++ b/net/tipc/socket.c @@ -2920,6 +2920,7 @@ static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua) struct net *net = sock_net(sk); struct tipc_socket_addr skaddr; struct publication *p; + int err = 0; u32 key; if (tipc_sk_connected(sk)) @@ -2929,7 +2930,7 @@ static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua) return -EADDRINUSE; skaddr.ref = tsk->portid; skaddr.node = tipc_own_addr(net); - p = tipc_nametbl_publish(net, ua, &skaddr, key); + p = tipc_nametbl_publish(net, ua, &skaddr, key, &err); if (unlikely(!p)) return -EINVAL; -- 2.53.0