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

Merge branch 'tipc-cleanups-and-simplifications'

Jon Maloy says:

====================
tipc: cleanups and simplifications

We do a number of cleanups and simplifications, especially regarding
call signatures in the binding table. This makes the code easier to
understand and serves as preparation for upcoming functional additions.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 5c9e418b 5c834950
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* *
* Copyright (c) 2000-2006, 2018, Ericsson AB * Copyright (c) 2000-2006, 2018, Ericsson AB
* Copyright (c) 2004-2005, 2010-2011, Wind River Systems * Copyright (c) 2004-2005, 2010-2011, Wind River Systems
* Copyright (c) 2020-2021, Red Hat Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2006, 2018, Ericsson AB * Copyright (c) 2000-2006, 2018, Ericsson AB
* Copyright (c) 2004-2005, Wind River Systems * Copyright (c) 2004-2005, Wind River Systems
* Copyright (c) 2020, Red Hat Inc * Copyright (c) 2020-2021, Red Hat Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
...@@ -44,6 +44,50 @@ ...@@ -44,6 +44,50 @@
#include <net/netns/generic.h> #include <net/netns/generic.h>
#include "core.h" #include "core.h"
/* Struct tipc_uaddr: internal version of struct sockaddr_tipc.
* Must be kept aligned both regarding field positions and size.
*/
struct tipc_uaddr {
unsigned short family;
unsigned char addrtype;
signed char scope;
union {
struct {
struct tipc_service_addr sa;
u32 lookup_node;
};
struct tipc_service_range sr;
struct tipc_socket_addr sk;
};
};
static inline void tipc_uaddr(struct tipc_uaddr *ua, u32 atype, u32 scope,
u32 type, u32 lower, u32 upper)
{
ua->family = AF_TIPC;
ua->addrtype = atype;
ua->scope = scope;
ua->sr.type = type;
ua->sr.lower = lower;
ua->sr.upper = upper;
}
static inline bool tipc_uaddr_valid(struct tipc_uaddr *ua, int len)
{
u32 atype;
if (len < sizeof(struct sockaddr_tipc))
return false;
atype = ua->addrtype;
if (ua->family != AF_TIPC)
return false;
if (atype == TIPC_SERVICE_ADDR || atype == TIPC_SOCKET_ADDR)
return true;
if (atype == TIPC_SERVICE_RANGE)
return ua->sr.upper >= ua->sr.lower;
return false;
}
static inline u32 tipc_own_addr(struct net *net) static inline u32 tipc_own_addr(struct net *net)
{ {
return tipc_net(net)->node_addr; return tipc_net(net)->node_addr;
......
...@@ -707,8 +707,11 @@ bool tipc_msg_skb_clone(struct sk_buff_head *msg, struct sk_buff_head *cpy) ...@@ -707,8 +707,11 @@ bool tipc_msg_skb_clone(struct sk_buff_head *msg, struct sk_buff_head *cpy)
bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err) bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err)
{ {
struct tipc_msg *msg = buf_msg(skb); struct tipc_msg *msg = buf_msg(skb);
u32 dport, dnode; u32 scope = msg_lookup_scope(msg);
u32 onode = tipc_own_addr(net); u32 self = tipc_own_addr(net);
u32 inst = msg_nameinst(msg);
struct tipc_socket_addr sk;
struct tipc_uaddr ua;
if (!msg_isdata(msg)) if (!msg_isdata(msg))
return false; return false;
...@@ -722,16 +725,16 @@ bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err) ...@@ -722,16 +725,16 @@ bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err)
msg = buf_msg(skb); msg = buf_msg(skb);
if (msg_reroute_cnt(msg)) if (msg_reroute_cnt(msg))
return false; return false;
dnode = tipc_scope2node(net, msg_lookup_scope(msg)); tipc_uaddr(&ua, TIPC_SERVICE_RANGE, scope,
dport = tipc_nametbl_translate(net, msg_nametype(msg), msg_nametype(msg), inst, inst);
msg_nameinst(msg), &dnode); sk.node = tipc_scope2node(net, scope);
if (!dport) if (!tipc_nametbl_lookup_anycast(net, &ua, &sk))
return false; return false;
msg_incr_reroute_cnt(msg); msg_incr_reroute_cnt(msg);
if (dnode != onode) if (sk.node != self)
msg_set_prevnode(msg, onode); msg_set_prevnode(msg, self);
msg_set_destnode(msg, dnode); msg_set_destnode(msg, sk.node);
msg_set_destport(msg, dport); msg_set_destport(msg, sk.ref);
*err = TIPC_OK; *err = TIPC_OK;
return true; return true;
......
/* /*
* net/tipc/name_distr.c: TIPC name distribution code * net/tipc/name_distr.c: TIPC name distribution code
* *
* Copyright (c) 2000-2006, 2014, Ericsson AB * Copyright (c) 2000-2006, 2014-2019, Ericsson AB
* Copyright (c) 2005, 2010-2011, Wind River Systems * Copyright (c) 2005, 2010-2011, Wind River Systems
* Copyright (c) 2020-2021, Red Hat Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
...@@ -55,10 +56,10 @@ struct distr_queue_item { ...@@ -55,10 +56,10 @@ struct distr_queue_item {
*/ */
static void publ_to_item(struct distr_item *i, struct publication *p) static void publ_to_item(struct distr_item *i, struct publication *p)
{ {
i->type = htonl(p->type); i->type = htonl(p->sr.type);
i->lower = htonl(p->lower); i->lower = htonl(p->sr.lower);
i->upper = htonl(p->upper); i->upper = htonl(p->sr.upper);
i->port = htonl(p->port); i->port = htonl(p->sk.ref);
i->key = htonl(p->key); i->key = htonl(p->key);
} }
...@@ -90,20 +91,20 @@ static struct sk_buff *named_prepare_buf(struct net *net, u32 type, u32 size, ...@@ -90,20 +91,20 @@ static struct sk_buff *named_prepare_buf(struct net *net, u32 type, u32 size,
/** /**
* tipc_named_publish - tell other nodes about a new publication by this node * tipc_named_publish - tell other nodes about a new publication by this node
* @net: the associated network namespace * @net: the associated network namespace
* @publ: the new publication * @p: the new publication
*/ */
struct sk_buff *tipc_named_publish(struct net *net, struct publication *publ) struct sk_buff *tipc_named_publish(struct net *net, struct publication *p)
{ {
struct name_table *nt = tipc_name_table(net); struct name_table *nt = tipc_name_table(net);
struct distr_item *item; struct distr_item *item;
struct sk_buff *skb; struct sk_buff *skb;
if (publ->scope == TIPC_NODE_SCOPE) { if (p->scope == TIPC_NODE_SCOPE) {
list_add_tail_rcu(&publ->binding_node, &nt->node_scope); list_add_tail_rcu(&p->binding_node, &nt->node_scope);
return NULL; return NULL;
} }
write_lock_bh(&nt->cluster_scope_lock); write_lock_bh(&nt->cluster_scope_lock);
list_add_tail(&publ->binding_node, &nt->cluster_scope); list_add_tail(&p->binding_node, &nt->cluster_scope);
write_unlock_bh(&nt->cluster_scope_lock); write_unlock_bh(&nt->cluster_scope_lock);
skb = named_prepare_buf(net, PUBLICATION, ITEM_SIZE, 0); skb = named_prepare_buf(net, PUBLICATION, ITEM_SIZE, 0);
if (!skb) { if (!skb) {
...@@ -113,25 +114,25 @@ struct sk_buff *tipc_named_publish(struct net *net, struct publication *publ) ...@@ -113,25 +114,25 @@ struct sk_buff *tipc_named_publish(struct net *net, struct publication *publ)
msg_set_named_seqno(buf_msg(skb), nt->snd_nxt++); msg_set_named_seqno(buf_msg(skb), nt->snd_nxt++);
msg_set_non_legacy(buf_msg(skb)); msg_set_non_legacy(buf_msg(skb));
item = (struct distr_item *)msg_data(buf_msg(skb)); item = (struct distr_item *)msg_data(buf_msg(skb));
publ_to_item(item, publ); publ_to_item(item, p);
return skb; return skb;
} }
/** /**
* tipc_named_withdraw - tell other nodes about a withdrawn publication by this node * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
* @net: the associated network namespace * @net: the associated network namespace
* @publ: the withdrawn publication * @p: the withdrawn publication
*/ */
struct sk_buff *tipc_named_withdraw(struct net *net, struct publication *publ) struct sk_buff *tipc_named_withdraw(struct net *net, struct publication *p)
{ {
struct name_table *nt = tipc_name_table(net); struct name_table *nt = tipc_name_table(net);
struct distr_item *item; struct distr_item *item;
struct sk_buff *skb; struct sk_buff *skb;
write_lock_bh(&nt->cluster_scope_lock); write_lock_bh(&nt->cluster_scope_lock);
list_del(&publ->binding_node); list_del(&p->binding_node);
write_unlock_bh(&nt->cluster_scope_lock); write_unlock_bh(&nt->cluster_scope_lock);
if (publ->scope == TIPC_NODE_SCOPE) if (p->scope == TIPC_NODE_SCOPE)
return NULL; return NULL;
skb = named_prepare_buf(net, WITHDRAWAL, ITEM_SIZE, 0); skb = named_prepare_buf(net, WITHDRAWAL, ITEM_SIZE, 0);
...@@ -142,7 +143,7 @@ struct sk_buff *tipc_named_withdraw(struct net *net, struct publication *publ) ...@@ -142,7 +143,7 @@ struct sk_buff *tipc_named_withdraw(struct net *net, struct publication *publ)
msg_set_named_seqno(buf_msg(skb), nt->snd_nxt++); msg_set_named_seqno(buf_msg(skb), nt->snd_nxt++);
msg_set_non_legacy(buf_msg(skb)); msg_set_non_legacy(buf_msg(skb));
item = (struct distr_item *)msg_data(buf_msg(skb)); item = (struct distr_item *)msg_data(buf_msg(skb));
publ_to_item(item, publ); publ_to_item(item, p);
return skb; return skb;
} }
...@@ -233,33 +234,27 @@ void tipc_named_node_up(struct net *net, u32 dnode, u16 capabilities) ...@@ -233,33 +234,27 @@ void tipc_named_node_up(struct net *net, u32 dnode, u16 capabilities)
/** /**
* tipc_publ_purge - remove publication associated with a failed node * tipc_publ_purge - remove publication associated with a failed node
* @net: the associated network namespace * @net: the associated network namespace
* @publ: the publication to remove * @p: the publication to remove
* @addr: failed node's address * @addr: failed node's address
* *
* Invoked for each publication issued by a newly failed node. * Invoked for each publication issued by a newly failed node.
* Removes publication structure from name table & deletes it. * Removes publication structure from name table & deletes it.
*/ */
static void tipc_publ_purge(struct net *net, struct publication *publ, u32 addr) static void tipc_publ_purge(struct net *net, struct publication *p, u32 addr)
{ {
struct tipc_net *tn = tipc_net(net); struct tipc_net *tn = tipc_net(net);
struct publication *p; struct publication *_p;
struct tipc_uaddr ua;
tipc_uaddr(&ua, TIPC_SERVICE_RANGE, p->scope, p->sr.type,
p->sr.lower, p->sr.upper);
spin_lock_bh(&tn->nametbl_lock); spin_lock_bh(&tn->nametbl_lock);
p = tipc_nametbl_remove_publ(net, publ->type, publ->lower, publ->upper, _p = tipc_nametbl_remove_publ(net, &ua, &p->sk, p->key);
publ->node, publ->key); if (_p)
if (p) tipc_node_unsubscribe(net, &_p->binding_node, addr);
tipc_node_unsubscribe(net, &p->binding_node, addr);
spin_unlock_bh(&tn->nametbl_lock); spin_unlock_bh(&tn->nametbl_lock);
if (_p)
if (p != publ) { kfree_rcu(_p, rcu);
pr_err("Unable to remove publication from failed node\n"
" (type=%u, lower=%u, node=0x%x, port=%u, key=%u)\n",
publ->type, publ->lower, publ->node, publ->port,
publ->key);
}
if (p)
kfree_rcu(p, rcu);
} }
void tipc_publ_notify(struct net *net, struct list_head *nsub_list, void tipc_publ_notify(struct net *net, struct list_head *nsub_list,
...@@ -293,30 +288,30 @@ static bool tipc_update_nametbl(struct net *net, struct distr_item *i, ...@@ -293,30 +288,30 @@ static bool tipc_update_nametbl(struct net *net, struct distr_item *i,
u32 node, u32 dtype) u32 node, u32 dtype)
{ {
struct publication *p = NULL; struct publication *p = NULL;
u32 lower = ntohl(i->lower); struct tipc_socket_addr sk;
u32 upper = ntohl(i->upper); struct tipc_uaddr ua;
u32 type = ntohl(i->type);
u32 port = ntohl(i->port);
u32 key = ntohl(i->key); u32 key = ntohl(i->key);
tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_CLUSTER_SCOPE,
ntohl(i->type), ntohl(i->lower), ntohl(i->upper));
sk.ref = ntohl(i->port);
sk.node = node;
if (dtype == PUBLICATION) { if (dtype == PUBLICATION) {
p = tipc_nametbl_insert_publ(net, type, lower, upper, p = tipc_nametbl_insert_publ(net, &ua, &sk, key);
TIPC_CLUSTER_SCOPE, node,
port, key);
if (p) { if (p) {
tipc_node_subscribe(net, &p->binding_node, node); tipc_node_subscribe(net, &p->binding_node, node);
return true; return true;
} }
} else if (dtype == WITHDRAWAL) { } else if (dtype == WITHDRAWAL) {
p = tipc_nametbl_remove_publ(net, type, lower, p = tipc_nametbl_remove_publ(net, &ua, &sk, key);
upper, node, key);
if (p) { if (p) {
tipc_node_unsubscribe(net, &p->binding_node, node); tipc_node_unsubscribe(net, &p->binding_node, node);
kfree_rcu(p, rcu); kfree_rcu(p, rcu);
return true; return true;
} }
pr_warn_ratelimited("Failed to remove binding %u,%u from %x\n", pr_warn_ratelimited("Failed to remove binding %u,%u from %u\n",
type, lower, node); ua.sr.type, ua.sr.lower, node);
} else { } else {
pr_warn("Unrecognized name table message received\n"); pr_warn("Unrecognized name table message received\n");
} }
...@@ -410,15 +405,15 @@ void tipc_named_reinit(struct net *net) ...@@ -410,15 +405,15 @@ void tipc_named_reinit(struct net *net)
{ {
struct name_table *nt = tipc_name_table(net); struct name_table *nt = tipc_name_table(net);
struct tipc_net *tn = tipc_net(net); struct tipc_net *tn = tipc_net(net);
struct publication *publ; struct publication *p;
u32 self = tipc_own_addr(net); u32 self = tipc_own_addr(net);
spin_lock_bh(&tn->nametbl_lock); spin_lock_bh(&tn->nametbl_lock);
list_for_each_entry_rcu(publ, &nt->node_scope, binding_node) list_for_each_entry_rcu(p, &nt->node_scope, binding_node)
publ->node = self; p->sk.node = self;
list_for_each_entry_rcu(publ, &nt->cluster_scope, binding_node) list_for_each_entry_rcu(p, &nt->cluster_scope, binding_node)
publ->node = self; p->sk.node = self;
nt->rc_dests = 0; nt->rc_dests = 0;
spin_unlock_bh(&tn->nametbl_lock); spin_unlock_bh(&tn->nametbl_lock);
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2006, 2014-2018, Ericsson AB * Copyright (c) 2000-2006, 2014-2018, Ericsson AB
* Copyright (c) 2004-2008, 2010-2014, Wind River Systems * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
* Copyright (c) 2020, Red Hat Inc * Copyright (c) 2020-2021, Red Hat Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
...@@ -222,59 +222,57 @@ static int hash(int x) ...@@ -222,59 +222,57 @@ static int hash(int x)
/** /**
* tipc_publ_create - create a publication structure * tipc_publ_create - create a publication structure
* @type: name sequence type * @ua: the service range the user is binding to
* @lower: name sequence lower bound * @sk: the address of the socket that is bound
* @upper: name sequence upper bound
* @scope: publication scope
* @node: network address of publishing socket
* @port: publishing port
* @key: publication key * @key: publication key
*/ */
static struct publication *tipc_publ_create(u32 type, u32 lower, u32 upper, static struct publication *tipc_publ_create(struct tipc_uaddr *ua,
u32 scope, u32 node, u32 port, struct tipc_socket_addr *sk,
u32 key) u32 key)
{ {
struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC); struct publication *p = kzalloc(sizeof(*p), GFP_ATOMIC);
if (!publ) if (!p)
return NULL; return NULL;
publ->type = type; p->sr = ua->sr;
publ->lower = lower; p->sk = *sk;
publ->upper = upper; p->scope = ua->scope;
publ->scope = scope; p->key = key;
publ->node = node; INIT_LIST_HEAD(&p->binding_sock);
publ->port = port; INIT_LIST_HEAD(&p->binding_node);
publ->key = key; INIT_LIST_HEAD(&p->local_publ);
INIT_LIST_HEAD(&publ->binding_sock); INIT_LIST_HEAD(&p->all_publ);
INIT_LIST_HEAD(&publ->binding_node); INIT_LIST_HEAD(&p->list);
INIT_LIST_HEAD(&publ->local_publ); return p;
INIT_LIST_HEAD(&publ->all_publ);
INIT_LIST_HEAD(&publ->list);
return publ;
} }
/** /**
* tipc_service_create - create a service structure for the specified 'type' * tipc_service_create - create a service structure for the specified 'type'
* @type: service type * @net: network namespace
* @hd: name_table services list * @ua: address representing the service to be bound
* *
* Allocates a single range structure and sets it to all 0's. * Allocates a single range structure and sets it to all 0's.
*/ */
static struct tipc_service *tipc_service_create(u32 type, struct hlist_head *hd) static struct tipc_service *tipc_service_create(struct net *net,
struct tipc_uaddr *ua)
{ {
struct tipc_service *service = kzalloc(sizeof(*service), GFP_ATOMIC); struct name_table *nt = tipc_name_table(net);
struct tipc_service *service;
struct hlist_head *hd;
service = kzalloc(sizeof(*service), GFP_ATOMIC);
if (!service) { if (!service) {
pr_warn("Service creation failed, no memory\n"); pr_warn("Service creation failed, no memory\n");
return NULL; return NULL;
} }
spin_lock_init(&service->lock); spin_lock_init(&service->lock);
service->type = type; service->type = ua->sr.type;
service->ranges = RB_ROOT; service->ranges = RB_ROOT;
INIT_HLIST_NODE(&service->service_list); INIT_HLIST_NODE(&service->service_list);
INIT_LIST_HEAD(&service->subscriptions); INIT_LIST_HEAD(&service->subscriptions);
hd = &nt->services[hash(ua->sr.type)];
hlist_add_head_rcu(&service->service_list, hd); hlist_add_head_rcu(&service->service_list, hd);
return service; return service;
} }
...@@ -282,13 +280,13 @@ static struct tipc_service *tipc_service_create(u32 type, struct hlist_head *hd) ...@@ -282,13 +280,13 @@ static struct tipc_service *tipc_service_create(u32 type, struct hlist_head *hd)
/* tipc_service_find_range - find service range matching publication parameters /* tipc_service_find_range - find service range matching publication parameters
*/ */
static struct service_range *tipc_service_find_range(struct tipc_service *sc, static struct service_range *tipc_service_find_range(struct tipc_service *sc,
u32 lower, u32 upper) struct tipc_uaddr *ua)
{ {
struct service_range *sr; struct service_range *sr;
service_range_foreach_match(sr, sc, lower, upper) { service_range_foreach_match(sr, sc, ua->sr.lower, ua->sr.upper) {
/* Look for exact match */ /* Look for exact match */
if (sr->lower == lower && sr->upper == upper) if (sr->lower == ua->sr.lower && sr->upper == ua->sr.upper)
return sr; return sr;
} }
...@@ -296,10 +294,12 @@ static struct service_range *tipc_service_find_range(struct tipc_service *sc, ...@@ -296,10 +294,12 @@ static struct service_range *tipc_service_find_range(struct tipc_service *sc,
} }
static struct service_range *tipc_service_create_range(struct tipc_service *sc, static struct service_range *tipc_service_create_range(struct tipc_service *sc,
u32 lower, u32 upper) struct publication *p)
{ {
struct rb_node **n, *parent = NULL; struct rb_node **n, *parent = NULL;
struct service_range *sr; struct service_range *sr;
u32 lower = p->sr.lower;
u32 upper = p->sr.upper;
n = &sc->ranges.rb_node; n = &sc->ranges.rb_node;
while (*n) { while (*n) {
...@@ -327,64 +327,68 @@ static struct service_range *tipc_service_create_range(struct tipc_service *sc, ...@@ -327,64 +327,68 @@ static struct service_range *tipc_service_create_range(struct tipc_service *sc,
return sr; return sr;
} }
static struct publication *tipc_service_insert_publ(struct net *net, static bool tipc_service_insert_publ(struct net *net,
struct tipc_service *sc, struct tipc_service *sc,
u32 type, u32 lower, struct publication *p)
u32 upper, u32 scope,
u32 node, u32 port,
u32 key)
{ {
struct tipc_subscription *sub, *tmp; struct tipc_subscription *sub, *tmp;
struct service_range *sr; struct service_range *sr;
struct publication *p; struct publication *_p;
u32 node = p->sk.node;
bool first = false; bool first = false;
bool res = false;
u32 key = p->key;
sr = tipc_service_create_range(sc, lower, upper); spin_lock_bh(&sc->lock);
sr = tipc_service_create_range(sc, p);
if (!sr) if (!sr)
goto err; goto exit;
first = list_empty(&sr->all_publ); first = list_empty(&sr->all_publ);
/* Return if the publication already exists */ /* Return if the publication already exists */
list_for_each_entry(p, &sr->all_publ, all_publ) { list_for_each_entry(_p, &sr->all_publ, all_publ) {
if (p->key == key && (!p->node || p->node == node)) if (_p->key == key && (!_p->sk.node || _p->sk.node == node)) {
return NULL; 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);
goto exit;
}
} }
/* Create and insert publication */ if (in_own_node(net, p->sk.node))
p = tipc_publ_create(type, lower, upper, scope, node, port, key);
if (!p)
goto err;
/* Suppose there shouldn't be a huge gap btw publs i.e. >INT_MAX */
p->id = sc->publ_cnt++;
if (in_own_node(net, node))
list_add(&p->local_publ, &sr->local_publ); list_add(&p->local_publ, &sr->local_publ);
list_add(&p->all_publ, &sr->all_publ); list_add(&p->all_publ, &sr->all_publ);
p->id = sc->publ_cnt++;
/* Any subscriptions waiting for notification? */ /* Any subscriptions waiting for notification? */
list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) { list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
tipc_sub_report_overlap(sub, p->lower, p->upper, TIPC_PUBLISHED, tipc_sub_report_overlap(sub, p, TIPC_PUBLISHED, first);
p->port, p->node, p->scope, first);
} }
return p; res = true;
err: exit:
pr_warn("Failed to bind to %u,%u,%u, no memory\n", type, lower, upper); if (!res)
return NULL; pr_warn("Failed to bind to %u,%u,%u\n",
p->sr.type, p->sr.lower, p->sr.upper);
spin_unlock_bh(&sc->lock);
return res;
} }
/** /**
* tipc_service_remove_publ - remove a publication from a service * tipc_service_remove_publ - remove a publication from a service
* @sr: service_range to remove publication from * @r: service_range to remove publication from
* @node: target node * @sk: address publishing socket
* @key: target publication key * @key: target publication key
*/ */
static struct publication *tipc_service_remove_publ(struct service_range *sr, static struct publication *tipc_service_remove_publ(struct service_range *r,
u32 node, u32 key) struct tipc_socket_addr *sk,
u32 key)
{ {
struct publication *p; struct publication *p;
u32 node = sk->node;
list_for_each_entry(p, &sr->all_publ, all_publ) { list_for_each_entry(p, &r->all_publ, all_publ) {
if (p->key != key || (node && node != p->node)) if (p->key != key || (node && node != p->sk.node))
continue; continue;
list_del(&p->all_publ); list_del(&p->all_publ);
list_del(&p->local_publ); list_del(&p->local_publ);
...@@ -417,17 +421,14 @@ static int tipc_publ_sort(void *priv, struct list_head *a, ...@@ -417,17 +421,14 @@ static int tipc_publ_sort(void *priv, struct list_head *a,
static void tipc_service_subscribe(struct tipc_service *service, static void tipc_service_subscribe(struct tipc_service *service,
struct tipc_subscription *sub) struct tipc_subscription *sub)
{ {
struct tipc_subscr *sb = &sub->evt.s;
struct publication *p, *first, *tmp; struct publication *p, *first, *tmp;
struct list_head publ_list; struct list_head publ_list;
struct service_range *sr; struct service_range *sr;
struct tipc_service_range r; u32 filter, lower, upper;
u32 filter;
r.type = tipc_sub_read(sb, seq.type); filter = sub->s.filter;
r.lower = tipc_sub_read(sb, seq.lower); lower = sub->s.seq.lower;
r.upper = tipc_sub_read(sb, seq.upper); upper = sub->s.seq.upper;
filter = tipc_sub_read(sb, filter);
tipc_sub_get(sub); tipc_sub_get(sub);
list_add(&sub->service_list, &service->subscriptions); list_add(&sub->service_list, &service->subscriptions);
...@@ -436,7 +437,7 @@ static void tipc_service_subscribe(struct tipc_service *service, ...@@ -436,7 +437,7 @@ static void tipc_service_subscribe(struct tipc_service *service,
return; return;
INIT_LIST_HEAD(&publ_list); INIT_LIST_HEAD(&publ_list);
service_range_foreach_match(sr, service, r.lower, r.upper) { service_range_foreach_match(sr, service, lower, upper) {
first = NULL; first = NULL;
list_for_each_entry(p, &sr->all_publ, all_publ) { list_for_each_entry(p, &sr->all_publ, all_publ) {
if (filter & TIPC_SUB_PORTS) if (filter & TIPC_SUB_PORTS)
...@@ -452,80 +453,74 @@ static void tipc_service_subscribe(struct tipc_service *service, ...@@ -452,80 +453,74 @@ static void tipc_service_subscribe(struct tipc_service *service,
/* Sort the publications before reporting */ /* Sort the publications before reporting */
list_sort(NULL, &publ_list, tipc_publ_sort); list_sort(NULL, &publ_list, tipc_publ_sort);
list_for_each_entry_safe(p, tmp, &publ_list, list) { list_for_each_entry_safe(p, tmp, &publ_list, list) {
tipc_sub_report_overlap(sub, p->lower, p->upper, tipc_sub_report_overlap(sub, p, TIPC_PUBLISHED, true);
TIPC_PUBLISHED, p->port, p->node,
p->scope, true);
list_del_init(&p->list); list_del_init(&p->list);
} }
} }
static struct tipc_service *tipc_service_find(struct net *net, u32 type) static struct tipc_service *tipc_service_find(struct net *net,
struct tipc_uaddr *ua)
{ {
struct name_table *nt = tipc_name_table(net); struct name_table *nt = tipc_name_table(net);
struct hlist_head *service_head; struct hlist_head *service_head;
struct tipc_service *service; struct tipc_service *service;
service_head = &nt->services[hash(type)]; service_head = &nt->services[hash(ua->sr.type)];
hlist_for_each_entry_rcu(service, service_head, service_list) { hlist_for_each_entry_rcu(service, service_head, service_list) {
if (service->type == type) if (service->type == ua->sr.type)
return service; return service;
} }
return NULL; return NULL;
}; };
struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type, struct publication *tipc_nametbl_insert_publ(struct net *net,
u32 lower, u32 upper, struct tipc_uaddr *ua,
u32 scope, u32 node, struct tipc_socket_addr *sk,
u32 port, u32 key) u32 key)
{ {
struct name_table *nt = tipc_name_table(net);
struct tipc_service *sc; struct tipc_service *sc;
struct publication *p; struct publication *p;
if (scope > TIPC_NODE_SCOPE || lower > upper) { p = tipc_publ_create(ua, sk, key);
pr_debug("Failed to bind illegal {%u,%u,%u} with scope %u\n", if (!p)
type, lower, upper, scope);
return NULL;
}
sc = tipc_service_find(net, type);
if (!sc)
sc = tipc_service_create(type, &nt->services[hash(type)]);
if (!sc)
return NULL; return NULL;
spin_lock_bh(&sc->lock); sc = tipc_service_find(net, ua);
p = tipc_service_insert_publ(net, sc, type, lower, upper, if (!sc)
scope, node, port, key); sc = tipc_service_create(net, ua);
spin_unlock_bh(&sc->lock); if (sc && tipc_service_insert_publ(net, sc, p))
return p; return p;
kfree(p);
return NULL;
} }
struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type, struct publication *tipc_nametbl_remove_publ(struct net *net,
u32 lower, u32 upper, struct tipc_uaddr *ua,
u32 node, u32 key) struct tipc_socket_addr *sk,
u32 key)
{ {
struct tipc_service *sc = tipc_service_find(net, type);
struct tipc_subscription *sub, *tmp; struct tipc_subscription *sub, *tmp;
struct service_range *sr = NULL;
struct publication *p = NULL; struct publication *p = NULL;
struct service_range *sr;
struct tipc_service *sc;
bool last; bool last;
sc = tipc_service_find(net, ua);
if (!sc) if (!sc)
return NULL; goto exit;
spin_lock_bh(&sc->lock); spin_lock_bh(&sc->lock);
sr = tipc_service_find_range(sc, lower, upper); sr = tipc_service_find_range(sc, ua);
if (!sr) if (!sr)
goto exit; goto unlock;
p = tipc_service_remove_publ(sr, node, key); p = tipc_service_remove_publ(sr, sk, key);
if (!p) if (!p)
goto exit; goto unlock;
/* Notify any waiting subscriptions */ /* Notify any waiting subscriptions */
last = list_empty(&sr->all_publ); last = list_empty(&sr->all_publ);
list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) { list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
tipc_sub_report_overlap(sub, lower, upper, TIPC_WITHDRAWN, tipc_sub_report_overlap(sub, p, TIPC_WITHDRAWN, last);
p->port, node, p->scope, last);
} }
/* Remove service range item if this was its last publication */ /* Remove service range item if this was its last publication */
...@@ -534,77 +529,83 @@ struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type, ...@@ -534,77 +529,83 @@ struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
kfree(sr); kfree(sr);
} }
/* Delete service item if this no more publications and subscriptions */ /* Delete service item if no more publications and subscriptions */
if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) { if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
hlist_del_init_rcu(&sc->service_list); hlist_del_init_rcu(&sc->service_list);
kfree_rcu(sc, rcu); kfree_rcu(sc, rcu);
} }
exit: unlock:
spin_unlock_bh(&sc->lock); 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);
}
return p; return p;
} }
/** /**
* tipc_nametbl_translate - perform service instance to socket translation * tipc_nametbl_lookup_anycast - perform service instance to socket translation
* @net: network namespace * @net: network namespace
* @type: message type * @ua: service address to look up
* @instance: message instance * @sk: address to socket we want to find
* @dnode: the search domain used during translation
* *
* On entry, a non-zero 'sk->node' indicates the node where we want lookup to be
* performed, which may not be this one.
* On exit: * On exit:
* - if translation is deferred to another node, leave 'dnode' unchanged and * - If lookup is deferred to another node, leave 'sk->node' unchanged and
* return 0 * return 'true'.
* - if translation is attempted and succeeds, set 'dnode' to the publishing * - If lookup is successful, set the 'sk->node' and 'sk->ref' (== portid) which
* node and return the published (non-zero) port number * represent the bound socket and return 'true'.
* - if translation is attempted and fails, set 'dnode' to 0 and return 0 * - If lookup fails, return 'false'
* *
* Note that for legacy users (node configured with Z.C.N address format) the * Note that for legacy users (node configured with Z.C.N address format) the
* 'closest-first' lookup algorithm must be maintained, i.e., if dnode is 0 * 'closest-first' lookup algorithm must be maintained, i.e., if sk.node is 0
* we must look in the local binding list first * we must look in the local binding list first
*/ */
u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *dnode) bool tipc_nametbl_lookup_anycast(struct net *net,
struct tipc_uaddr *ua,
struct tipc_socket_addr *sk)
{ {
struct tipc_net *tn = tipc_net(net); struct tipc_net *tn = tipc_net(net);
bool legacy = tn->legacy_addr_format; bool legacy = tn->legacy_addr_format;
u32 self = tipc_own_addr(net); u32 self = tipc_own_addr(net);
struct service_range *sr; u32 inst = ua->sa.instance;
struct service_range *r;
struct tipc_service *sc; struct tipc_service *sc;
struct list_head *list;
struct publication *p; struct publication *p;
u32 port = 0; struct list_head *l;
u32 node = 0; bool res = false;
if (!tipc_in_scope(legacy, *dnode, self)) if (!tipc_in_scope(legacy, sk->node, self))
return 0; return true;
rcu_read_lock(); rcu_read_lock();
sc = tipc_service_find(net, type); sc = tipc_service_find(net, ua);
if (unlikely(!sc)) if (unlikely(!sc))
goto exit; goto exit;
spin_lock_bh(&sc->lock); spin_lock_bh(&sc->lock);
service_range_foreach_match(sr, sc, instance, instance) { service_range_foreach_match(r, sc, inst, inst) {
/* Select lookup algo: local, closest-first or round-robin */ /* Select lookup algo: local, closest-first or round-robin */
if (*dnode == self) { if (sk->node == self) {
list = &sr->local_publ; l = &r->local_publ;
if (list_empty(list)) if (list_empty(l))
continue; continue;
p = list_first_entry(list, struct publication, p = list_first_entry(l, struct publication, local_publ);
local_publ); list_move_tail(&p->local_publ, &r->local_publ);
list_move_tail(&p->local_publ, &sr->local_publ); } else if (legacy && !sk->node && !list_empty(&r->local_publ)) {
} else if (legacy && !*dnode && !list_empty(&sr->local_publ)) { l = &r->local_publ;
list = &sr->local_publ; p = list_first_entry(l, struct publication, local_publ);
p = list_first_entry(list, struct publication, list_move_tail(&p->local_publ, &r->local_publ);
local_publ);
list_move_tail(&p->local_publ, &sr->local_publ);
} else { } else {
list = &sr->all_publ; l = &r->all_publ;
p = list_first_entry(list, struct publication, p = list_first_entry(l, struct publication, all_publ);
all_publ); list_move_tail(&p->all_publ, &r->all_publ);
list_move_tail(&p->all_publ, &sr->all_publ);
} }
port = p->port; *sk = p->sk;
node = p->node; res = true;
/* Todo: as for legacy, pick the first matching range only, a /* Todo: as for legacy, pick the first matching range only, a
* "true" round-robin will be performed as needed. * "true" round-robin will be performed as needed.
*/ */
...@@ -614,40 +615,45 @@ u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *dnode) ...@@ -614,40 +615,45 @@ u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *dnode)
exit: exit:
rcu_read_unlock(); rcu_read_unlock();
*dnode = node; return res;
return port;
} }
bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope, /* tipc_nametbl_lookup_group(): lookup destinaton(s) in a communication group
struct list_head *dsts, int *dstcnt, u32 exclude, * Returns a list of one (== group anycast) or more (== group multicast)
bool all) * destination socket/node pairs matching the given address.
* The requester may or may not want to exclude himself from the list.
*/
bool tipc_nametbl_lookup_group(struct net *net, struct tipc_uaddr *ua,
struct list_head *dsts, int *dstcnt,
u32 exclude, bool mcast)
{ {
u32 self = tipc_own_addr(net); u32 self = tipc_own_addr(net);
u32 inst = ua->sa.instance;
struct service_range *sr; struct service_range *sr;
struct tipc_service *sc; struct tipc_service *sc;
struct publication *p; struct publication *p;
*dstcnt = 0; *dstcnt = 0;
rcu_read_lock(); rcu_read_lock();
sc = tipc_service_find(net, type); sc = tipc_service_find(net, ua);
if (unlikely(!sc)) if (unlikely(!sc))
goto exit; goto exit;
spin_lock_bh(&sc->lock); spin_lock_bh(&sc->lock);
/* Todo: a full search i.e. service_range_foreach_match() instead? */ /* Todo: a full search i.e. service_range_foreach_match() instead? */
sr = service_range_match_first(sc->ranges.rb_node, instance, instance); sr = service_range_match_first(sc->ranges.rb_node, inst, inst);
if (!sr) if (!sr)
goto no_match; goto no_match;
list_for_each_entry(p, &sr->all_publ, all_publ) { list_for_each_entry(p, &sr->all_publ, all_publ) {
if (p->scope != scope) if (p->scope != ua->scope)
continue; continue;
if (p->port == exclude && p->node == self) if (p->sk.ref == exclude && p->sk.node == self)
continue; continue;
tipc_dest_push(dsts, p->node, p->port); tipc_dest_push(dsts, p->sk.node, p->sk.ref);
(*dstcnt)++; (*dstcnt)++;
if (all) if (mcast)
continue; continue;
list_move_tail(&p->all_publ, &sr->all_publ); list_move_tail(&p->all_publ, &sr->all_publ);
break; break;
...@@ -659,23 +665,29 @@ bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope, ...@@ -659,23 +665,29 @@ bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
return !list_empty(dsts); return !list_empty(dsts);
} }
void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper, /* tipc_nametbl_lookup_mcast_sockets(): look up node local destinaton sockets
u32 scope, bool exact, struct list_head *dports) * matching the given address
* Used on nodes which have received a multicast/broadcast message
* Returns a list of local sockets
*/
void tipc_nametbl_lookup_mcast_sockets(struct net *net, struct tipc_uaddr *ua,
bool exact, struct list_head *dports)
{ {
struct service_range *sr; struct service_range *sr;
struct tipc_service *sc; struct tipc_service *sc;
struct publication *p; struct publication *p;
u32 scope = ua->scope;
rcu_read_lock(); rcu_read_lock();
sc = tipc_service_find(net, type); sc = tipc_service_find(net, ua);
if (!sc) if (!sc)
goto exit; goto exit;
spin_lock_bh(&sc->lock); spin_lock_bh(&sc->lock);
service_range_foreach_match(sr, sc, lower, upper) { service_range_foreach_match(sr, sc, ua->sr.lower, ua->sr.upper) {
list_for_each_entry(p, &sr->local_publ, local_publ) { list_for_each_entry(p, &sr->local_publ, local_publ) {
if (p->scope == scope || (!exact && p->scope < scope)) if (p->scope == scope || (!exact && p->scope < scope))
tipc_dest_push(dports, 0, p->port); tipc_dest_push(dports, 0, p->sk.ref);
} }
} }
spin_unlock_bh(&sc->lock); spin_unlock_bh(&sc->lock);
...@@ -683,26 +695,27 @@ void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper, ...@@ -683,26 +695,27 @@ void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
rcu_read_unlock(); rcu_read_unlock();
} }
/* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes /* tipc_nametbl_lookup_mcast_nodes(): look up all destination nodes matching
* - Creates list of nodes that overlap the given multicast address * the given address. Used in sending node.
* - Determines if any node local destinations overlap * Used on nodes which are sending out a multicast/broadcast message
* Returns a list of nodes, including own node if applicable
*/ */
void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower, void tipc_nametbl_lookup_mcast_nodes(struct net *net, struct tipc_uaddr *ua,
u32 upper, struct tipc_nlist *nodes) struct tipc_nlist *nodes)
{ {
struct service_range *sr; struct service_range *sr;
struct tipc_service *sc; struct tipc_service *sc;
struct publication *p; struct publication *p;
rcu_read_lock(); rcu_read_lock();
sc = tipc_service_find(net, type); sc = tipc_service_find(net, ua);
if (!sc) if (!sc)
goto exit; goto exit;
spin_lock_bh(&sc->lock); spin_lock_bh(&sc->lock);
service_range_foreach_match(sr, sc, lower, upper) { service_range_foreach_match(sr, sc, ua->sr.lower, ua->sr.upper) {
list_for_each_entry(p, &sr->all_publ, all_publ) { list_for_each_entry(p, &sr->all_publ, all_publ) {
tipc_nlist_add(nodes, p->node); tipc_nlist_add(nodes, p->sk.node);
} }
} }
spin_unlock_bh(&sc->lock); spin_unlock_bh(&sc->lock);
...@@ -713,7 +726,7 @@ void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower, ...@@ -713,7 +726,7 @@ void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
/* tipc_nametbl_build_group - build list of communication group members /* tipc_nametbl_build_group - build list of communication group members
*/ */
void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp, void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
u32 type, u32 scope) struct tipc_uaddr *ua)
{ {
struct service_range *sr; struct service_range *sr;
struct tipc_service *sc; struct tipc_service *sc;
...@@ -721,7 +734,7 @@ void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp, ...@@ -721,7 +734,7 @@ void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
struct rb_node *n; struct rb_node *n;
rcu_read_lock(); rcu_read_lock();
sc = tipc_service_find(net, type); sc = tipc_service_find(net, ua);
if (!sc) if (!sc)
goto exit; goto exit;
...@@ -729,9 +742,10 @@ void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp, ...@@ -729,9 +742,10 @@ void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
for (n = rb_first(&sc->ranges); n; n = rb_next(n)) { for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
sr = container_of(n, struct service_range, tree_node); sr = container_of(n, struct service_range, tree_node);
list_for_each_entry(p, &sr->all_publ, all_publ) { list_for_each_entry(p, &sr->all_publ, all_publ) {
if (p->scope != scope) if (p->scope != ua->scope)
continue; continue;
tipc_group_add_member(grp, p->node, p->port, p->lower); tipc_group_add_member(grp, p->sk.node, p->sk.ref,
p->sr.lower);
} }
} }
spin_unlock_bh(&sc->lock); spin_unlock_bh(&sc->lock);
...@@ -741,9 +755,8 @@ void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp, ...@@ -741,9 +755,8 @@ void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
/* tipc_nametbl_publish - add service binding to name table /* tipc_nametbl_publish - add service binding to name table
*/ */
struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower, struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua,
u32 upper, u32 scope, u32 port, struct tipc_socket_addr *sk, u32 key)
u32 key)
{ {
struct name_table *nt = tipc_name_table(net); struct name_table *nt = tipc_name_table(net);
struct tipc_net *tn = tipc_net(net); struct tipc_net *tn = tipc_net(net);
...@@ -758,8 +771,7 @@ struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower, ...@@ -758,8 +771,7 @@ struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
goto exit; goto exit;
} }
p = tipc_nametbl_insert_publ(net, type, lower, upper, scope, p = tipc_nametbl_insert_publ(net, ua, sk, key);
tipc_own_addr(net), port, key);
if (p) { if (p) {
nt->local_publ_count++; nt->local_publ_count++;
skb = tipc_named_publish(net, p); skb = tipc_named_publish(net, p);
...@@ -777,41 +789,33 @@ struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower, ...@@ -777,41 +789,33 @@ struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
/** /**
* tipc_nametbl_withdraw - withdraw a service binding * tipc_nametbl_withdraw - withdraw a service binding
* @net: network namespace * @net: network namespace
* @type: service type * @ua: service address/range being unbound
* @lower: service range lower bound * @sk: address of the socket being unbound from
* @upper: service range upper bound
* @key: target publication key * @key: target publication key
*/ */
int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, void tipc_nametbl_withdraw(struct net *net, struct tipc_uaddr *ua,
u32 upper, u32 key) struct tipc_socket_addr *sk, u32 key)
{ {
struct name_table *nt = tipc_name_table(net); struct name_table *nt = tipc_name_table(net);
struct tipc_net *tn = tipc_net(net); struct tipc_net *tn = tipc_net(net);
u32 self = tipc_own_addr(net);
struct sk_buff *skb = NULL; struct sk_buff *skb = NULL;
struct publication *p; struct publication *p;
u32 rc_dests; u32 rc_dests;
spin_lock_bh(&tn->nametbl_lock); spin_lock_bh(&tn->nametbl_lock);
p = tipc_nametbl_remove_publ(net, type, lower, upper, self, key); p = tipc_nametbl_remove_publ(net, ua, sk, key);
if (p) { if (p) {
nt->local_publ_count--; nt->local_publ_count--;
skb = tipc_named_withdraw(net, p); skb = tipc_named_withdraw(net, p);
list_del_init(&p->binding_sock); list_del_init(&p->binding_sock);
kfree_rcu(p, rcu); kfree_rcu(p, rcu);
} else {
pr_err("Failed to remove local publication {%u,%u,%u}/%u\n",
type, lower, upper, key);
} }
rc_dests = nt->rc_dests; rc_dests = nt->rc_dests;
spin_unlock_bh(&tn->nametbl_lock); spin_unlock_bh(&tn->nametbl_lock);
if (skb) { if (skb)
tipc_node_broadcast(net, skb, rc_dests); tipc_node_broadcast(net, skb, rc_dests);
return 1;
}
return 0;
} }
/** /**
...@@ -820,25 +824,25 @@ int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, ...@@ -820,25 +824,25 @@ int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower,
*/ */
bool tipc_nametbl_subscribe(struct tipc_subscription *sub) bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
{ {
struct name_table *nt = tipc_name_table(sub->net);
struct tipc_net *tn = tipc_net(sub->net); struct tipc_net *tn = tipc_net(sub->net);
struct tipc_subscr *s = &sub->evt.s; u32 type = sub->s.seq.type;
u32 type = tipc_sub_read(s, seq.type);
struct tipc_service *sc; struct tipc_service *sc;
struct tipc_uaddr ua;
bool res = true; bool res = true;
tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE, type,
sub->s.seq.lower, sub->s.seq.upper);
spin_lock_bh(&tn->nametbl_lock); spin_lock_bh(&tn->nametbl_lock);
sc = tipc_service_find(sub->net, type); sc = tipc_service_find(sub->net, &ua);
if (!sc) if (!sc)
sc = tipc_service_create(type, &nt->services[hash(type)]); sc = tipc_service_create(sub->net, &ua);
if (sc) { if (sc) {
spin_lock_bh(&sc->lock); spin_lock_bh(&sc->lock);
tipc_service_subscribe(sc, sub); tipc_service_subscribe(sc, sub);
spin_unlock_bh(&sc->lock); spin_unlock_bh(&sc->lock);
} else { } else {
pr_warn("Failed to subscribe for {%u,%u,%u}\n", type, pr_warn("Failed to subscribe for {%u,%u,%u}\n",
tipc_sub_read(s, seq.lower), type, sub->s.seq.lower, sub->s.seq.upper);
tipc_sub_read(s, seq.upper));
res = false; res = false;
} }
spin_unlock_bh(&tn->nametbl_lock); spin_unlock_bh(&tn->nametbl_lock);
...@@ -852,12 +856,13 @@ bool tipc_nametbl_subscribe(struct tipc_subscription *sub) ...@@ -852,12 +856,13 @@ bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
void tipc_nametbl_unsubscribe(struct tipc_subscription *sub) void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
{ {
struct tipc_net *tn = tipc_net(sub->net); struct tipc_net *tn = tipc_net(sub->net);
struct tipc_subscr *s = &sub->evt.s;
u32 type = tipc_sub_read(s, seq.type);
struct tipc_service *sc; struct tipc_service *sc;
struct tipc_uaddr ua;
tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
sub->s.seq.type, sub->s.seq.lower, sub->s.seq.upper);
spin_lock_bh(&tn->nametbl_lock); spin_lock_bh(&tn->nametbl_lock);
sc = tipc_service_find(sub->net, type); sc = tipc_service_find(sub->net, &ua);
if (!sc) if (!sc)
goto exit; goto exit;
...@@ -909,7 +914,7 @@ static void tipc_service_delete(struct net *net, struct tipc_service *sc) ...@@ -909,7 +914,7 @@ static void tipc_service_delete(struct net *net, struct tipc_service *sc)
spin_lock_bh(&sc->lock); spin_lock_bh(&sc->lock);
rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) { rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) { list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
tipc_service_remove_publ(sr, p->node, p->key); tipc_service_remove_publ(sr, &p->sk, p->key);
kfree_rcu(p, rcu); kfree_rcu(p, rcu);
} }
rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks); rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
...@@ -993,9 +998,9 @@ static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg, ...@@ -993,9 +998,9 @@ static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
goto publ_msg_full; goto publ_msg_full;
if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope)) if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
goto publ_msg_full; goto publ_msg_full;
if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node)) if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->sk.node))
goto publ_msg_full; goto publ_msg_full;
if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port)) if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->sk.ref))
goto publ_msg_full; goto publ_msg_full;
if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key)) if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
goto publ_msg_full; goto publ_msg_full;
...@@ -1046,6 +1051,7 @@ static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg, ...@@ -1046,6 +1051,7 @@ static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
struct tipc_net *tn = tipc_net(net); struct tipc_net *tn = tipc_net(net);
struct tipc_service *service = NULL; struct tipc_service *service = NULL;
struct hlist_head *head; struct hlist_head *head;
struct tipc_uaddr ua;
int err; int err;
int i; int i;
...@@ -1059,7 +1065,9 @@ static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg, ...@@ -1059,7 +1065,9 @@ static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
if (*last_type || if (*last_type ||
(!i && *last_key && (*last_lower == *last_key))) { (!i && *last_key && (*last_lower == *last_key))) {
service = tipc_service_find(net, *last_type); tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
*last_type, *last_lower, *last_lower);
service = tipc_service_find(net, &ua);
if (!service) if (!service)
return -EPIPE; return -EPIPE;
} else { } else {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* *
* Copyright (c) 2000-2006, 2014-2018, Ericsson AB * Copyright (c) 2000-2006, 2014-2018, Ericsson AB
* Copyright (c) 2004-2005, 2010-2011, Wind River Systems * Copyright (c) 2004-2005, 2010-2011, Wind River Systems
* Copyright (c) 2020-2021, Red Hat Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
...@@ -41,6 +42,7 @@ struct tipc_subscription; ...@@ -41,6 +42,7 @@ struct tipc_subscription;
struct tipc_plist; struct tipc_plist;
struct tipc_nlist; struct tipc_nlist;
struct tipc_group; struct tipc_group;
struct tipc_uaddr;
/* /*
* TIPC name types reserved for internal TIPC use (both current and planned) * TIPC name types reserved for internal TIPC use (both current and planned)
...@@ -50,13 +52,10 @@ struct tipc_group; ...@@ -50,13 +52,10 @@ struct tipc_group;
#define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */ #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
/** /**
* struct publication - info about a published (name or) name sequence * struct publication - info about a published service address or range
* @type: name sequence type * @sr: service range represented by this publication
* @lower: name sequence lower bound * @sk: address of socket bound to this publication
* @upper: name sequence upper bound
* @scope: scope of publication, TIPC_NODE_SCOPE or TIPC_CLUSTER_SCOPE * @scope: scope of publication, TIPC_NODE_SCOPE or TIPC_CLUSTER_SCOPE
* @node: network address of publishing socket's node
* @port: publishing port
* @key: publication key, unique across the cluster * @key: publication key, unique across the cluster
* @id: publication id * @id: publication id
* @binding_node: all publications from the same node which bound this one * @binding_node: all publications from the same node which bound this one
...@@ -74,12 +73,9 @@ struct tipc_group; ...@@ -74,12 +73,9 @@ struct tipc_group;
* @rcu: RCU callback head used for deferred freeing * @rcu: RCU callback head used for deferred freeing
*/ */
struct publication { struct publication {
u32 type; struct tipc_service_range sr;
u32 lower; struct tipc_socket_addr sk;
u32 upper; u16 scope;
u32 scope;
u32 node;
u32 port;
u32 key; u32 key;
u32 id; u32 id;
struct list_head binding_node; struct list_head binding_node;
...@@ -114,28 +110,29 @@ struct name_table { ...@@ -114,28 +110,29 @@ struct name_table {
}; };
int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb); int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb);
bool tipc_nametbl_lookup_anycast(struct net *net, struct tipc_uaddr *ua,
u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *node); struct tipc_socket_addr *sk);
void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper, void tipc_nametbl_lookup_mcast_sockets(struct net *net, struct tipc_uaddr *ua,
u32 scope, bool exact, struct list_head *dports); bool exact, struct list_head *dports);
void tipc_nametbl_lookup_mcast_nodes(struct net *net, struct tipc_uaddr *ua,
struct tipc_nlist *nodes);
bool tipc_nametbl_lookup_group(struct net *net, struct tipc_uaddr *ua,
struct list_head *dsts, int *dstcnt,
u32 exclude, bool mcast);
void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp, void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
u32 type, u32 domain); struct tipc_uaddr *ua);
void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower, struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua,
u32 upper, struct tipc_nlist *nodes); struct tipc_socket_addr *sk, u32 key);
bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 domain, void tipc_nametbl_withdraw(struct net *net, struct tipc_uaddr *ua,
struct list_head *dsts, int *dstcnt, u32 exclude, struct tipc_socket_addr *sk, u32 key);
bool all); struct publication *tipc_nametbl_insert_publ(struct net *net,
struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower, struct tipc_uaddr *ua,
u32 upper, u32 scope, u32 port, struct tipc_socket_addr *sk,
u32 key); u32 key);
int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 upper, struct publication *tipc_nametbl_remove_publ(struct net *net,
struct tipc_uaddr *ua,
struct tipc_socket_addr *sk,
u32 key); u32 key);
struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
u32 lower, u32 upper, u32 scope,
u32 node, u32 ref, u32 key);
struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
u32 lower, u32 upper,
u32 node, u32 key);
bool tipc_nametbl_subscribe(struct tipc_subscription *s); bool tipc_nametbl_subscribe(struct tipc_subscription *s);
void tipc_nametbl_unsubscribe(struct tipc_subscription *s); void tipc_nametbl_unsubscribe(struct tipc_subscription *s);
int tipc_nametbl_init(struct net *net); int tipc_nametbl_init(struct net *net);
......
...@@ -125,6 +125,11 @@ int tipc_net_init(struct net *net, u8 *node_id, u32 addr) ...@@ -125,6 +125,11 @@ int tipc_net_init(struct net *net, u8 *node_id, u32 addr)
static void tipc_net_finalize(struct net *net, u32 addr) static void tipc_net_finalize(struct net *net, u32 addr)
{ {
struct tipc_net *tn = tipc_net(net); struct tipc_net *tn = tipc_net(net);
struct tipc_socket_addr sk = {0, addr};
struct tipc_uaddr ua;
tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_CLUSTER_SCOPE,
TIPC_NODE_STATE, addr, addr);
if (cmpxchg(&tn->node_addr, 0, addr)) if (cmpxchg(&tn->node_addr, 0, addr))
return; return;
...@@ -132,8 +137,7 @@ static void tipc_net_finalize(struct net *net, u32 addr) ...@@ -132,8 +137,7 @@ static void tipc_net_finalize(struct net *net, u32 addr)
tipc_named_reinit(net); tipc_named_reinit(net);
tipc_sk_reinit(net); tipc_sk_reinit(net);
tipc_mon_reinit_self(net); tipc_mon_reinit_self(net);
tipc_nametbl_publish(net, TIPC_NODE_STATE, addr, addr, tipc_nametbl_publish(net, &ua, &sk, addr);
TIPC_CLUSTER_SCOPE, 0, addr);
} }
void tipc_net_finalize_work(struct work_struct *work) void tipc_net_finalize_work(struct work_struct *work)
......
...@@ -398,21 +398,23 @@ static void tipc_node_write_unlock_fast(struct tipc_node *n) ...@@ -398,21 +398,23 @@ static void tipc_node_write_unlock_fast(struct tipc_node *n)
static void tipc_node_write_unlock(struct tipc_node *n) static void tipc_node_write_unlock(struct tipc_node *n)
__releases(n->lock) __releases(n->lock)
{ {
struct tipc_socket_addr sk;
struct net *net = n->net; struct net *net = n->net;
u32 addr = 0;
u32 flags = n->action_flags; u32 flags = n->action_flags;
u32 link_id = 0;
u32 bearer_id;
struct list_head *publ_list; struct list_head *publ_list;
struct tipc_uaddr ua;
u32 bearer_id;
if (likely(!flags)) { if (likely(!flags)) {
write_unlock_bh(&n->lock); write_unlock_bh(&n->lock);
return; return;
} }
addr = n->addr; tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
link_id = n->link_id; TIPC_LINK_STATE, n->addr, n->addr);
bearer_id = link_id & 0xffff; sk.ref = n->link_id;
sk.node = n->addr;
bearer_id = n->link_id & 0xffff;
publ_list = &n->publ_list; publ_list = &n->publ_list;
n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP | n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
...@@ -421,20 +423,18 @@ static void tipc_node_write_unlock(struct tipc_node *n) ...@@ -421,20 +423,18 @@ static void tipc_node_write_unlock(struct tipc_node *n)
write_unlock_bh(&n->lock); write_unlock_bh(&n->lock);
if (flags & TIPC_NOTIFY_NODE_DOWN) if (flags & TIPC_NOTIFY_NODE_DOWN)
tipc_publ_notify(net, publ_list, addr, n->capabilities); tipc_publ_notify(net, publ_list, n->addr, n->capabilities);
if (flags & TIPC_NOTIFY_NODE_UP) if (flags & TIPC_NOTIFY_NODE_UP)
tipc_named_node_up(net, addr, n->capabilities); tipc_named_node_up(net, n->addr, n->capabilities);
if (flags & TIPC_NOTIFY_LINK_UP) { if (flags & TIPC_NOTIFY_LINK_UP) {
tipc_mon_peer_up(net, addr, bearer_id); tipc_mon_peer_up(net, n->addr, bearer_id);
tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr, tipc_nametbl_publish(net, &ua, &sk, n->link_id);
TIPC_NODE_SCOPE, link_id, link_id);
} }
if (flags & TIPC_NOTIFY_LINK_DOWN) { if (flags & TIPC_NOTIFY_LINK_DOWN) {
tipc_mon_peer_down(net, addr, bearer_id); tipc_mon_peer_down(net, n->addr, bearer_id);
tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr, tipc_nametbl_withdraw(net, &ua, &sk, n->link_id);
addr, link_id);
} }
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2001-2007, 2012-2019, Ericsson AB * Copyright (c) 2001-2007, 2012-2019, Ericsson AB
* Copyright (c) 2004-2008, 2010-2013, Wind River Systems * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
* Copyright (c) 2020, Red Hat Inc * Copyright (c) 2020-2021, Red Hat Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
...@@ -111,7 +111,6 @@ struct tipc_sock { ...@@ -111,7 +111,6 @@ struct tipc_sock {
struct sock sk; struct sock sk;
u32 conn_type; u32 conn_type;
u32 conn_instance; u32 conn_instance;
int published;
u32 max_pkt; u32 max_pkt;
u32 maxnagle; u32 maxnagle;
u32 portid; u32 portid;
...@@ -141,6 +140,7 @@ struct tipc_sock { ...@@ -141,6 +140,7 @@ struct tipc_sock {
bool expect_ack; bool expect_ack;
bool nodelay; bool nodelay;
bool group_is_open; bool group_is_open;
bool published;
}; };
static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb); static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
...@@ -151,10 +151,8 @@ static int tipc_release(struct socket *sock); ...@@ -151,10 +151,8 @@ static int tipc_release(struct socket *sock);
static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags, static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
bool kern); bool kern);
static void tipc_sk_timeout(struct timer_list *t); static void tipc_sk_timeout(struct timer_list *t);
static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua);
struct tipc_service_range const *seq); static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua);
static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
struct tipc_service_range const *seq);
static int tipc_sk_leave(struct tipc_sock *tsk); static int tipc_sk_leave(struct tipc_sock *tsk);
static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid); static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
static int tipc_sk_insert(struct tipc_sock *tsk); static int tipc_sk_insert(struct tipc_sock *tsk);
...@@ -644,7 +642,7 @@ static int tipc_release(struct socket *sock) ...@@ -644,7 +642,7 @@ static int tipc_release(struct socket *sock)
__tipc_shutdown(sock, TIPC_ERR_NO_PORT); __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
sk->sk_shutdown = SHUTDOWN_MASK; sk->sk_shutdown = SHUTDOWN_MASK;
tipc_sk_leave(tsk); tipc_sk_leave(tsk);
tipc_sk_withdraw(tsk, 0, NULL); tipc_sk_withdraw(tsk, NULL);
__skb_queue_purge(&tsk->mc_method.deferredq); __skb_queue_purge(&tsk->mc_method.deferredq);
sk_stop_timer(sk, &sk->sk_timer); sk_stop_timer(sk, &sk->sk_timer);
tipc_sk_remove(tsk); tipc_sk_remove(tsk);
...@@ -677,22 +675,31 @@ static int tipc_release(struct socket *sock) ...@@ -677,22 +675,31 @@ static int tipc_release(struct socket *sock)
*/ */
static int __tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen) static int __tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
{ {
struct sockaddr_tipc *addr = (struct sockaddr_tipc *)skaddr; struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
struct tipc_sock *tsk = tipc_sk(sock->sk); struct tipc_sock *tsk = tipc_sk(sock->sk);
bool unbind = false;
if (unlikely(!alen)) if (unlikely(!alen))
return tipc_sk_withdraw(tsk, 0, NULL); return tipc_sk_withdraw(tsk, NULL);
if (addr->addrtype == TIPC_SERVICE_ADDR) if (ua->addrtype == TIPC_SERVICE_ADDR) {
addr->addr.nameseq.upper = addr->addr.nameseq.lower; ua->addrtype = TIPC_SERVICE_RANGE;
ua->sr.upper = ua->sr.lower;
}
if (ua->scope < 0) {
unbind = true;
ua->scope = -ua->scope;
}
/* Users may still use deprecated TIPC_ZONE_SCOPE */
if (ua->scope != TIPC_NODE_SCOPE)
ua->scope = TIPC_CLUSTER_SCOPE;
if (tsk->group) if (tsk->group)
return -EACCES; return -EACCES;
if (addr->scope >= 0) if (unbind)
return tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq); return tipc_sk_withdraw(tsk, ua);
else return tipc_sk_publish(tsk, ua);
return tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
} }
int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen) int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
...@@ -707,18 +714,17 @@ int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen) ...@@ -707,18 +714,17 @@ int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
static int tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen) static int tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
{ {
struct sockaddr_tipc *addr = (struct sockaddr_tipc *)skaddr; struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
u32 atype = ua->addrtype;
if (alen) { if (alen) {
if (alen < sizeof(struct sockaddr_tipc)) if (!tipc_uaddr_valid(ua, alen))
return -EINVAL; return -EINVAL;
if (addr->family != AF_TIPC) if (atype == TIPC_SOCKET_ADDR)
return -EAFNOSUPPORT; return -EAFNOSUPPORT;
if (addr->addrtype > TIPC_SERVICE_ADDR) if (ua->sr.type < TIPC_RESERVED_TYPES) {
return -EAFNOSUPPORT;
if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES) {
pr_warn_once("Can't bind to reserved service type %u\n", pr_warn_once("Can't bind to reserved service type %u\n",
addr->addr.nameseq.type); ua->sr.type);
return -EACCES; return -EACCES;
} }
} }
...@@ -826,7 +832,7 @@ static __poll_t tipc_poll(struct file *file, struct socket *sock, ...@@ -826,7 +832,7 @@ static __poll_t tipc_poll(struct file *file, struct socket *sock,
/** /**
* tipc_sendmcast - send multicast message * tipc_sendmcast - send multicast message
* @sock: socket structure * @sock: socket structure
* @seq: destination address * @ua: destination address struct
* @msg: message to send * @msg: message to send
* @dlen: length of data to send * @dlen: length of data to send
* @timeout: timeout to wait for wakeup * @timeout: timeout to wait for wakeup
...@@ -834,7 +840,7 @@ static __poll_t tipc_poll(struct file *file, struct socket *sock, ...@@ -834,7 +840,7 @@ static __poll_t tipc_poll(struct file *file, struct socket *sock,
* Called from function tipc_sendmsg(), which has done all sanity checks * Called from function tipc_sendmsg(), which has done all sanity checks
* Return: the number of bytes sent on success, or errno * Return: the number of bytes sent on success, or errno
*/ */
static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq, static int tipc_sendmcast(struct socket *sock, struct tipc_uaddr *ua,
struct msghdr *msg, size_t dlen, long timeout) struct msghdr *msg, size_t dlen, long timeout)
{ {
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
...@@ -842,7 +848,6 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq, ...@@ -842,7 +848,6 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq,
struct tipc_msg *hdr = &tsk->phdr; struct tipc_msg *hdr = &tsk->phdr;
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
int mtu = tipc_bcast_get_mtu(net); int mtu = tipc_bcast_get_mtu(net);
struct tipc_mc_method *method = &tsk->mc_method;
struct sk_buff_head pkts; struct sk_buff_head pkts;
struct tipc_nlist dsts; struct tipc_nlist dsts;
int rc; int rc;
...@@ -857,8 +862,7 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq, ...@@ -857,8 +862,7 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq,
/* Lookup destination nodes */ /* Lookup destination nodes */
tipc_nlist_init(&dsts, tipc_own_addr(net)); tipc_nlist_init(&dsts, tipc_own_addr(net));
tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower, tipc_nametbl_lookup_mcast_nodes(net, ua, &dsts);
seq->upper, &dsts);
if (!dsts.local && !dsts.remote) if (!dsts.local && !dsts.remote)
return -EHOSTUNREACH; return -EHOSTUNREACH;
...@@ -868,9 +872,9 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq, ...@@ -868,9 +872,9 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq,
msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE); msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
msg_set_destport(hdr, 0); msg_set_destport(hdr, 0);
msg_set_destnode(hdr, 0); msg_set_destnode(hdr, 0);
msg_set_nametype(hdr, seq->type); msg_set_nametype(hdr, ua->sr.type);
msg_set_namelower(hdr, seq->lower); msg_set_namelower(hdr, ua->sr.lower);
msg_set_nameupper(hdr, seq->upper); msg_set_nameupper(hdr, ua->sr.upper);
/* Build message as chain of buffers */ /* Build message as chain of buffers */
__skb_queue_head_init(&pkts); __skb_queue_head_init(&pkts);
...@@ -880,7 +884,7 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq, ...@@ -880,7 +884,7 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_service_range *seq,
if (unlikely(rc == dlen)) { if (unlikely(rc == dlen)) {
trace_tipc_sk_sendmcast(sk, skb_peek(&pkts), trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
TIPC_DUMP_SK_SNDQ, " "); TIPC_DUMP_SK_SNDQ, " ");
rc = tipc_mcast_xmit(net, &pkts, method, &dsts, rc = tipc_mcast_xmit(net, &pkts, &tsk->mc_method, &dsts,
&tsk->cong_link_cnt); &tsk->cong_link_cnt);
} }
...@@ -954,7 +958,7 @@ static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m, ...@@ -954,7 +958,7 @@ static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
int dlen, long timeout) int dlen, long timeout)
{ {
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
int blks = tsk_blocks(GROUP_H_SIZE + dlen); int blks = tsk_blocks(GROUP_H_SIZE + dlen);
struct tipc_sock *tsk = tipc_sk(sk); struct tipc_sock *tsk = tipc_sk(sk);
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
...@@ -962,8 +966,8 @@ static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m, ...@@ -962,8 +966,8 @@ static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
u32 node, port; u32 node, port;
int rc; int rc;
node = dest->addr.id.node; node = ua->sk.node;
port = dest->addr.id.ref; port = ua->sk.ref;
if (!port && !node) if (!port && !node)
return -EHOSTUNREACH; return -EHOSTUNREACH;
...@@ -997,7 +1001,7 @@ static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m, ...@@ -997,7 +1001,7 @@ static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m, static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
int dlen, long timeout) int dlen, long timeout)
{ {
DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
struct tipc_sock *tsk = tipc_sk(sk); struct tipc_sock *tsk = tipc_sk(sk);
struct list_head *cong_links = &tsk->cong_links; struct list_head *cong_links = &tsk->cong_links;
...@@ -1008,16 +1012,13 @@ static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m, ...@@ -1008,16 +1012,13 @@ static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
u32 node, port, exclude; u32 node, port, exclude;
struct list_head dsts; struct list_head dsts;
u32 type, inst, scope;
int lookups = 0; int lookups = 0;
int dstcnt, rc; int dstcnt, rc;
bool cong; bool cong;
INIT_LIST_HEAD(&dsts); INIT_LIST_HEAD(&dsts);
ua->sa.type = msg_nametype(hdr);
type = msg_nametype(hdr); ua->scope = msg_lookup_scope(hdr);
inst = dest->addr.name.name.instance;
scope = msg_lookup_scope(hdr);
while (++lookups < 4) { while (++lookups < 4) {
exclude = tipc_group_exclude(tsk->group); exclude = tipc_group_exclude(tsk->group);
...@@ -1026,8 +1027,8 @@ static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m, ...@@ -1026,8 +1027,8 @@ static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
/* Look for a non-congested destination member, if any */ /* Look for a non-congested destination member, if any */
while (1) { while (1) {
if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts, if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt,
&dstcnt, exclude, false)) exclude, false))
return -EHOSTUNREACH; return -EHOSTUNREACH;
tipc_dest_pop(&dsts, &node, &port); tipc_dest_pop(&dsts, &node, &port);
cong = tipc_group_cong(tsk->group, node, port, blks, cong = tipc_group_cong(tsk->group, node, port, blks,
...@@ -1082,7 +1083,7 @@ static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m, ...@@ -1082,7 +1083,7 @@ static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m, static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
int dlen, long timeout) int dlen, long timeout)
{ {
DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
struct tipc_sock *tsk = tipc_sk(sk); struct tipc_sock *tsk = tipc_sk(sk);
...@@ -1107,9 +1108,9 @@ static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m, ...@@ -1107,9 +1108,9 @@ static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
return -EHOSTUNREACH; return -EHOSTUNREACH;
/* Complete message header */ /* Complete message header */
if (dest) { if (ua) {
msg_set_type(hdr, TIPC_GRP_MCAST_MSG); msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
msg_set_nameinst(hdr, dest->addr.name.name.instance); msg_set_nameinst(hdr, ua->sa.instance);
} else { } else {
msg_set_type(hdr, TIPC_GRP_BCAST_MSG); msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
msg_set_nameinst(hdr, 0); msg_set_nameinst(hdr, 0);
...@@ -1156,29 +1157,25 @@ static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m, ...@@ -1156,29 +1157,25 @@ static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m, static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
int dlen, long timeout) int dlen, long timeout)
{ {
struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
struct tipc_sock *tsk = tipc_sk(sk); struct tipc_sock *tsk = tipc_sk(sk);
struct tipc_group *grp = tsk->group; struct tipc_group *grp = tsk->group;
struct tipc_msg *hdr = &tsk->phdr; struct tipc_msg *hdr = &tsk->phdr;
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
u32 type, inst, scope, exclude;
struct list_head dsts; struct list_head dsts;
u32 dstcnt; u32 dstcnt, exclude;
INIT_LIST_HEAD(&dsts); INIT_LIST_HEAD(&dsts);
ua->sa.type = msg_nametype(hdr);
type = msg_nametype(hdr); ua->scope = msg_lookup_scope(hdr);
inst = dest->addr.name.name.instance;
scope = msg_lookup_scope(hdr);
exclude = tipc_group_exclude(grp); exclude = tipc_group_exclude(grp);
if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts, if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt, exclude, true))
&dstcnt, exclude, true))
return -EHOSTUNREACH; return -EHOSTUNREACH;
if (dstcnt == 1) { if (dstcnt == 1) {
tipc_dest_pop(&dsts, &dest->addr.id.node, &dest->addr.id.ref); tipc_dest_pop(&dsts, &ua->sk.node, &ua->sk.ref);
return tipc_send_group_unicast(sock, m, dlen, timeout); return tipc_send_group_unicast(sock, m, dlen, timeout);
} }
...@@ -1198,17 +1195,18 @@ void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq, ...@@ -1198,17 +1195,18 @@ void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
struct sk_buff_head *inputq) struct sk_buff_head *inputq)
{ {
u32 self = tipc_own_addr(net); u32 self = tipc_own_addr(net);
u32 type, lower, upper, scope;
struct sk_buff *skb, *_skb; struct sk_buff *skb, *_skb;
u32 portid, onode; u32 portid, onode;
struct sk_buff_head tmpq; struct sk_buff_head tmpq;
struct list_head dports; struct list_head dports;
struct tipc_msg *hdr; struct tipc_msg *hdr;
struct tipc_uaddr ua;
int user, mtyp, hlen; int user, mtyp, hlen;
bool exact; bool exact;
__skb_queue_head_init(&tmpq); __skb_queue_head_init(&tmpq);
INIT_LIST_HEAD(&dports); INIT_LIST_HEAD(&dports);
ua.addrtype = TIPC_SERVICE_RANGE;
skb = tipc_skb_peek(arrvq, &inputq->lock); skb = tipc_skb_peek(arrvq, &inputq->lock);
for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) { for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
...@@ -1217,7 +1215,7 @@ void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq, ...@@ -1217,7 +1215,7 @@ void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
mtyp = msg_type(hdr); mtyp = msg_type(hdr);
hlen = skb_headroom(skb) + msg_hdr_sz(hdr); hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
onode = msg_orignode(hdr); onode = msg_orignode(hdr);
type = msg_nametype(hdr); ua.sr.type = msg_nametype(hdr);
if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) { if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
spin_lock_bh(&inputq->lock); spin_lock_bh(&inputq->lock);
...@@ -1232,24 +1230,23 @@ void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq, ...@@ -1232,24 +1230,23 @@ void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
/* Group messages require exact scope match */ /* Group messages require exact scope match */
if (msg_in_group(hdr)) { if (msg_in_group(hdr)) {
lower = 0; ua.sr.lower = 0;
upper = ~0; ua.sr.upper = ~0;
scope = msg_lookup_scope(hdr); ua.scope = msg_lookup_scope(hdr);
exact = true; exact = true;
} else { } else {
/* TIPC_NODE_SCOPE means "any scope" in this context */ /* TIPC_NODE_SCOPE means "any scope" in this context */
if (onode == self) if (onode == self)
scope = TIPC_NODE_SCOPE; ua.scope = TIPC_NODE_SCOPE;
else else
scope = TIPC_CLUSTER_SCOPE; ua.scope = TIPC_CLUSTER_SCOPE;
exact = false; exact = false;
lower = msg_namelower(hdr); ua.sr.lower = msg_namelower(hdr);
upper = msg_nameupper(hdr); ua.sr.upper = msg_nameupper(hdr);
} }
/* Create destination port list: */ /* Create destination port list: */
tipc_nametbl_mc_lookup(net, type, lower, upper, tipc_nametbl_lookup_mcast_sockets(net, &ua, exact, &dports);
scope, exact, &dports);
/* Clone message per destination */ /* Clone message per destination */
while (tipc_dest_pop(&dports, NULL, &portid)) { while (tipc_dest_pop(&dports, NULL, &portid)) {
...@@ -1417,44 +1414,43 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen) ...@@ -1417,44 +1414,43 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
struct tipc_sock *tsk = tipc_sk(sk); struct tipc_sock *tsk = tipc_sk(sk);
DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
struct list_head *clinks = &tsk->cong_links; struct list_head *clinks = &tsk->cong_links;
bool syn = !tipc_sk_type_connectionless(sk); bool syn = !tipc_sk_type_connectionless(sk);
struct tipc_group *grp = tsk->group; struct tipc_group *grp = tsk->group;
struct tipc_msg *hdr = &tsk->phdr; struct tipc_msg *hdr = &tsk->phdr;
struct tipc_service_range *seq; struct tipc_socket_addr skaddr;
struct sk_buff_head pkts; struct sk_buff_head pkts;
u32 dport = 0, dnode = 0; int atype, mtu, rc;
u32 type = 0, inst = 0;
int mtu, rc;
if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE)) if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
return -EMSGSIZE; return -EMSGSIZE;
if (likely(dest)) { if (ua) {
if (unlikely(m->msg_namelen < sizeof(*dest))) if (!tipc_uaddr_valid(ua, m->msg_namelen))
return -EINVAL;
if (unlikely(dest->family != AF_TIPC))
return -EINVAL; return -EINVAL;
atype = ua->addrtype;
} }
/* If socket belongs to a communication group follow other paths */
if (grp) { if (grp) {
if (!dest) if (!ua)
return tipc_send_group_bcast(sock, m, dlen, timeout); return tipc_send_group_bcast(sock, m, dlen, timeout);
if (dest->addrtype == TIPC_SERVICE_ADDR) if (atype == TIPC_SERVICE_ADDR)
return tipc_send_group_anycast(sock, m, dlen, timeout); return tipc_send_group_anycast(sock, m, dlen, timeout);
if (dest->addrtype == TIPC_SOCKET_ADDR) if (atype == TIPC_SOCKET_ADDR)
return tipc_send_group_unicast(sock, m, dlen, timeout); return tipc_send_group_unicast(sock, m, dlen, timeout);
if (dest->addrtype == TIPC_ADDR_MCAST) if (atype == TIPC_SERVICE_RANGE)
return tipc_send_group_mcast(sock, m, dlen, timeout); return tipc_send_group_mcast(sock, m, dlen, timeout);
return -EINVAL; return -EINVAL;
} }
if (unlikely(!dest)) { if (!ua) {
dest = &tsk->peer; ua = (struct tipc_uaddr *)&tsk->peer;
if (!syn && dest->family != AF_TIPC) if (!syn && ua->family != AF_TIPC)
return -EDESTADDRREQ; return -EDESTADDRREQ;
atype = ua->addrtype;
} }
if (unlikely(syn)) { if (unlikely(syn)) {
...@@ -1464,54 +1460,51 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen) ...@@ -1464,54 +1460,51 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
return -EISCONN; return -EISCONN;
if (tsk->published) if (tsk->published)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (dest->addrtype == TIPC_SERVICE_ADDR) { if (atype == TIPC_SERVICE_ADDR) {
tsk->conn_type = dest->addr.name.name.type; tsk->conn_type = ua->sa.type;
tsk->conn_instance = dest->addr.name.name.instance; tsk->conn_instance = ua->sa.instance;
} }
msg_set_syn(hdr, 1); msg_set_syn(hdr, 1);
} }
seq = &dest->addr.nameseq; /* Determine destination */
if (dest->addrtype == TIPC_ADDR_MCAST) if (atype == TIPC_SERVICE_RANGE) {
return tipc_sendmcast(sock, seq, m, dlen, timeout); return tipc_sendmcast(sock, ua, m, dlen, timeout);
} else if (atype == TIPC_SERVICE_ADDR) {
if (dest->addrtype == TIPC_SERVICE_ADDR) { skaddr.node = ua->lookup_node;
type = dest->addr.name.name.type; ua->scope = tipc_node2scope(skaddr.node);
inst = dest->addr.name.name.instance; if (!tipc_nametbl_lookup_anycast(net, ua, &skaddr))
dnode = dest->addr.name.domain;
dport = tipc_nametbl_translate(net, type, inst, &dnode);
if (unlikely(!dport && !dnode))
return -EHOSTUNREACH; return -EHOSTUNREACH;
} else if (dest->addrtype == TIPC_SOCKET_ADDR) { } else if (atype == TIPC_SOCKET_ADDR) {
dnode = dest->addr.id.node; skaddr = ua->sk;
} else { } else {
return -EINVAL; return -EINVAL;
} }
/* Block or return if destination link is congested */ /* Block or return if destination link is congested */
rc = tipc_wait_for_cond(sock, &timeout, rc = tipc_wait_for_cond(sock, &timeout,
!tipc_dest_find(clinks, dnode, 0)); !tipc_dest_find(clinks, skaddr.node, 0));
if (unlikely(rc)) if (unlikely(rc))
return rc; return rc;
if (dest->addrtype == TIPC_SERVICE_ADDR) { /* Finally build message header */
msg_set_destnode(hdr, skaddr.node);
msg_set_destport(hdr, skaddr.ref);
if (atype == TIPC_SERVICE_ADDR) {
msg_set_type(hdr, TIPC_NAMED_MSG); msg_set_type(hdr, TIPC_NAMED_MSG);
msg_set_hdr_sz(hdr, NAMED_H_SIZE); msg_set_hdr_sz(hdr, NAMED_H_SIZE);
msg_set_nametype(hdr, type); msg_set_nametype(hdr, ua->sa.type);
msg_set_nameinst(hdr, inst); msg_set_nameinst(hdr, ua->sa.instance);
msg_set_lookup_scope(hdr, tipc_node2scope(dnode)); msg_set_lookup_scope(hdr, ua->scope);
msg_set_destnode(hdr, dnode);
msg_set_destport(hdr, dport);
} else { /* TIPC_SOCKET_ADDR */ } else { /* TIPC_SOCKET_ADDR */
msg_set_type(hdr, TIPC_DIRECT_MSG); msg_set_type(hdr, TIPC_DIRECT_MSG);
msg_set_lookup_scope(hdr, 0); msg_set_lookup_scope(hdr, 0);
msg_set_destnode(hdr, dnode);
msg_set_destport(hdr, dest->addr.id.ref);
msg_set_hdr_sz(hdr, BASIC_H_SIZE); msg_set_hdr_sz(hdr, BASIC_H_SIZE);
} }
/* Add message body */
__skb_queue_head_init(&pkts); __skb_queue_head_init(&pkts);
mtu = tipc_node_get_mtu(net, dnode, tsk->portid, true); mtu = tipc_node_get_mtu(net, skaddr.node, tsk->portid, true);
rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts); rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
if (unlikely(rc != dlen)) if (unlikely(rc != dlen))
return rc; return rc;
...@@ -1520,10 +1513,11 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen) ...@@ -1520,10 +1513,11 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
return -ENOMEM; return -ENOMEM;
} }
/* Send message */
trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " "); trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid); rc = tipc_node_xmit(net, &pkts, skaddr.node, tsk->portid);
if (unlikely(rc == -ELINKCONG)) { if (unlikely(rc == -ELINKCONG)) {
tipc_dest_push(clinks, dnode, 0); tipc_dest_push(clinks, skaddr.node, 0);
tsk->cong_link_cnt++; tsk->cong_link_cnt++;
rc = 0; rc = 0;
} }
...@@ -2891,66 +2885,62 @@ static void tipc_sk_timeout(struct timer_list *t) ...@@ -2891,66 +2885,62 @@ static void tipc_sk_timeout(struct timer_list *t)
sock_put(sk); sock_put(sk);
} }
static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua)
struct tipc_service_range const *seq)
{ {
struct sock *sk = &tsk->sk; struct sock *sk = &tsk->sk;
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
struct publication *publ; struct tipc_socket_addr skaddr;
struct publication *p;
u32 key; u32 key;
if (scope != TIPC_NODE_SCOPE)
scope = TIPC_CLUSTER_SCOPE;
if (tipc_sk_connected(sk)) if (tipc_sk_connected(sk))
return -EINVAL; return -EINVAL;
key = tsk->portid + tsk->pub_count + 1; key = tsk->portid + tsk->pub_count + 1;
if (key == tsk->portid) if (key == tsk->portid)
return -EADDRINUSE; return -EADDRINUSE;
skaddr.ref = tsk->portid;
publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper, skaddr.node = tipc_own_addr(net);
scope, tsk->portid, key); p = tipc_nametbl_publish(net, ua, &skaddr, key);
if (unlikely(!publ)) if (unlikely(!p))
return -EINVAL; return -EINVAL;
list_add(&publ->binding_sock, &tsk->publications); list_add(&p->binding_sock, &tsk->publications);
tsk->pub_count++; tsk->pub_count++;
tsk->published = 1; tsk->published = true;
return 0; return 0;
} }
static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua)
struct tipc_service_range const *seq)
{ {
struct net *net = sock_net(&tsk->sk); struct net *net = sock_net(&tsk->sk);
struct publication *publ; struct publication *safe, *p;
struct publication *safe; struct tipc_uaddr _ua;
int rc = -EINVAL; int rc = -EINVAL;
if (scope != TIPC_NODE_SCOPE) list_for_each_entry_safe(p, safe, &tsk->publications, binding_sock) {
scope = TIPC_CLUSTER_SCOPE; if (!ua) {
tipc_uaddr(&_ua, TIPC_SERVICE_RANGE, p->scope,
list_for_each_entry_safe(publ, safe, &tsk->publications, binding_sock) { p->sr.type, p->sr.lower, p->sr.upper);
if (seq) { tipc_nametbl_withdraw(net, &_ua, &p->sk, p->key);
if (publ->scope != scope) continue;
}
/* Unbind specific publication */
if (p->scope != ua->scope)
continue; continue;
if (publ->type != seq->type) if (p->sr.type != ua->sr.type)
continue; continue;
if (publ->lower != seq->lower) if (p->sr.lower != ua->sr.lower)
continue; continue;
if (publ->upper != seq->upper) if (p->sr.upper != ua->sr.upper)
break; break;
tipc_nametbl_withdraw(net, publ->type, publ->lower, tipc_nametbl_withdraw(net, ua, &p->sk, p->key);
publ->upper, publ->key);
rc = 0; rc = 0;
break; break;
} }
tipc_nametbl_withdraw(net, publ->type, publ->lower, if (list_empty(&tsk->publications)) {
publ->upper, publ->key); tsk->published = 0;
rc = 0; rc = 0;
} }
if (list_empty(&tsk->publications))
tsk->published = 0;
return rc; return rc;
} }
...@@ -3067,13 +3057,15 @@ static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq) ...@@ -3067,13 +3057,15 @@ static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
struct net *net = sock_net(&tsk->sk); struct net *net = sock_net(&tsk->sk);
struct tipc_group *grp = tsk->group; struct tipc_group *grp = tsk->group;
struct tipc_msg *hdr = &tsk->phdr; struct tipc_msg *hdr = &tsk->phdr;
struct tipc_service_range seq; struct tipc_uaddr ua;
int rc; int rc;
if (mreq->type < TIPC_RESERVED_TYPES) if (mreq->type < TIPC_RESERVED_TYPES)
return -EACCES; return -EACCES;
if (mreq->scope > TIPC_NODE_SCOPE) if (mreq->scope > TIPC_NODE_SCOPE)
return -EINVAL; return -EINVAL;
if (mreq->scope != TIPC_NODE_SCOPE)
mreq->scope = TIPC_CLUSTER_SCOPE;
if (grp) if (grp)
return -EACCES; return -EACCES;
grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open); grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
...@@ -3083,11 +3075,10 @@ static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq) ...@@ -3083,11 +3075,10 @@ static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
msg_set_lookup_scope(hdr, mreq->scope); msg_set_lookup_scope(hdr, mreq->scope);
msg_set_nametype(hdr, mreq->type); msg_set_nametype(hdr, mreq->type);
msg_set_dest_droppable(hdr, true); msg_set_dest_droppable(hdr, true);
seq.type = mreq->type; tipc_uaddr(&ua, TIPC_SERVICE_RANGE, mreq->scope,
seq.lower = mreq->instance; mreq->type, mreq->instance, mreq->instance);
seq.upper = seq.lower; tipc_nametbl_build_group(net, grp, &ua);
tipc_nametbl_build_group(net, grp, mreq->type, mreq->scope); rc = tipc_sk_publish(tsk, &ua);
rc = tipc_sk_publish(tsk, mreq->scope, &seq);
if (rc) { if (rc) {
tipc_group_delete(net, grp); tipc_group_delete(net, grp);
tsk->group = NULL; tsk->group = NULL;
...@@ -3104,15 +3095,17 @@ static int tipc_sk_leave(struct tipc_sock *tsk) ...@@ -3104,15 +3095,17 @@ static int tipc_sk_leave(struct tipc_sock *tsk)
{ {
struct net *net = sock_net(&tsk->sk); struct net *net = sock_net(&tsk->sk);
struct tipc_group *grp = tsk->group; struct tipc_group *grp = tsk->group;
struct tipc_service_range seq; struct tipc_uaddr ua;
int scope; int scope;
if (!grp) if (!grp)
return -EINVAL; return -EINVAL;
tipc_group_self(grp, &seq, &scope); ua.addrtype = TIPC_SERVICE_RANGE;
tipc_group_self(grp, &ua.sr, &scope);
ua.scope = scope;
tipc_group_delete(net, grp); tipc_group_delete(net, grp);
tsk->group = NULL; tsk->group = NULL;
tipc_sk_withdraw(tsk, scope, &seq); tipc_sk_withdraw(tsk, &ua);
return 0; return 0;
} }
...@@ -3711,11 +3704,11 @@ static int __tipc_nl_add_sk_publ(struct sk_buff *skb, ...@@ -3711,11 +3704,11 @@ static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key)) if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
goto attr_msg_cancel; goto attr_msg_cancel;
if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type)) if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->sr.type))
goto attr_msg_cancel; goto attr_msg_cancel;
if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower)) if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->sr.lower))
goto attr_msg_cancel; goto attr_msg_cancel;
if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper)) if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->sr.upper))
goto attr_msg_cancel; goto attr_msg_cancel;
nla_nest_end(skb, attrs); nla_nest_end(skb, attrs);
...@@ -3863,9 +3856,9 @@ bool tipc_sk_filtering(struct sock *sk) ...@@ -3863,9 +3856,9 @@ bool tipc_sk_filtering(struct sock *sk)
p = list_first_entry_or_null(&tsk->publications, p = list_first_entry_or_null(&tsk->publications,
struct publication, binding_sock); struct publication, binding_sock);
if (p) { if (p) {
type = p->type; type = p->sr.type;
lower = p->lower; lower = p->sr.lower;
upper = p->upper; upper = p->sr.upper;
} }
} }
...@@ -3964,9 +3957,9 @@ int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf) ...@@ -3964,9 +3957,9 @@ int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf)
if (tsk->published) { if (tsk->published) {
p = list_first_entry_or_null(&tsk->publications, p = list_first_entry_or_null(&tsk->publications,
struct publication, binding_sock); struct publication, binding_sock);
i += scnprintf(buf + i, sz - i, " %u", (p) ? p->type : 0); i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.type : 0);
i += scnprintf(buf + i, sz - i, " %u", (p) ? p->lower : 0); i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.lower : 0);
i += scnprintf(buf + i, sz - i, " %u", (p) ? p->upper : 0); i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.upper : 0);
} }
i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win); i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win);
i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win); i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2017, Ericsson AB * Copyright (c) 2000-2017, Ericsson AB
* Copyright (c) 2005-2007, 2010-2013, Wind River Systems * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
* Copyright (c) 2020, Red Hat Inc * Copyright (c) 2020-2021, Red Hat Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
...@@ -40,77 +40,75 @@ ...@@ -40,77 +40,75 @@
#include "subscr.h" #include "subscr.h"
static void tipc_sub_send_event(struct tipc_subscription *sub, static void tipc_sub_send_event(struct tipc_subscription *sub,
u32 found_lower, u32 found_upper, struct publication *p,
u32 event, u32 port, u32 node) u32 event)
{ {
struct tipc_subscr *s = &sub->evt.s;
struct tipc_event *evt = &sub->evt; struct tipc_event *evt = &sub->evt;
if (sub->inactive) if (sub->inactive)
return; return;
tipc_evt_write(evt, event, event); tipc_evt_write(evt, event, event);
tipc_evt_write(evt, found_lower, found_lower); if (p) {
tipc_evt_write(evt, found_upper, found_upper); tipc_evt_write(evt, found_lower, p->sr.lower);
tipc_evt_write(evt, port.ref, port); tipc_evt_write(evt, found_upper, p->sr.upper);
tipc_evt_write(evt, port.node, node); tipc_evt_write(evt, port.ref, p->sk.ref);
tipc_evt_write(evt, port.node, p->sk.node);
} else {
tipc_evt_write(evt, found_lower, s->seq.lower);
tipc_evt_write(evt, found_upper, s->seq.upper);
tipc_evt_write(evt, port.ref, 0);
tipc_evt_write(evt, port.node, 0);
}
tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt); tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt);
} }
/** /**
* tipc_sub_check_overlap - test for subscription overlap with the given values * tipc_sub_check_overlap - test for subscription overlap with the given values
* @seq: tipc_name_seq to check * @subscribed: the service range subscribed for
* @found_lower: lower value to test * @found: the service range we are checning for match
* @found_upper: upper value to test
* *
* Return: 1 if there is overlap, otherwise 0. * Returns true if there is overlap, otherwise false.
*/ */
int tipc_sub_check_overlap(struct tipc_service_range *seq, u32 found_lower, static bool tipc_sub_check_overlap(struct tipc_service_range *subscribed,
u32 found_upper) struct tipc_service_range *found)
{ {
if (found_lower < seq->lower) u32 found_lower = found->lower;
found_lower = seq->lower; u32 found_upper = found->upper;
if (found_upper > seq->upper)
found_upper = seq->upper; if (found_lower < subscribed->lower)
if (found_lower > found_upper) found_lower = subscribed->lower;
return 0; if (found_upper > subscribed->upper)
return 1; found_upper = subscribed->upper;
return found_lower <= found_upper;
} }
void tipc_sub_report_overlap(struct tipc_subscription *sub, void tipc_sub_report_overlap(struct tipc_subscription *sub,
u32 found_lower, u32 found_upper, struct publication *p,
u32 event, u32 port, u32 node, u32 event, bool must)
u32 scope, int must)
{ {
struct tipc_subscr *s = &sub->evt.s; struct tipc_service_range *sr = &sub->s.seq;
u32 filter = tipc_sub_read(s, filter); u32 filter = sub->s.filter;
struct tipc_service_range seq;
seq.type = tipc_sub_read(s, seq.type); if (!tipc_sub_check_overlap(sr, &p->sr))
seq.lower = tipc_sub_read(s, seq.lower);
seq.upper = tipc_sub_read(s, seq.upper);
if (!tipc_sub_check_overlap(&seq, found_lower, found_upper))
return; return;
if (!must && !(filter & TIPC_SUB_PORTS)) if (!must && !(filter & TIPC_SUB_PORTS))
return; return;
if (filter & TIPC_SUB_CLUSTER_SCOPE && scope == TIPC_NODE_SCOPE) if (filter & TIPC_SUB_CLUSTER_SCOPE && p->scope == TIPC_NODE_SCOPE)
return; return;
if (filter & TIPC_SUB_NODE_SCOPE && scope != TIPC_NODE_SCOPE) if (filter & TIPC_SUB_NODE_SCOPE && p->scope != TIPC_NODE_SCOPE)
return; return;
spin_lock(&sub->lock); spin_lock(&sub->lock);
tipc_sub_send_event(sub, found_lower, found_upper, tipc_sub_send_event(sub, p, event);
event, port, node);
spin_unlock(&sub->lock); spin_unlock(&sub->lock);
} }
static void tipc_sub_timeout(struct timer_list *t) static void tipc_sub_timeout(struct timer_list *t)
{ {
struct tipc_subscription *sub = from_timer(sub, t, timer); struct tipc_subscription *sub = from_timer(sub, t, timer);
struct tipc_subscr *s = &sub->evt.s;
spin_lock(&sub->lock); spin_lock(&sub->lock);
tipc_sub_send_event(sub, s->seq.lower, s->seq.upper, tipc_sub_send_event(sub, NULL, TIPC_SUBSCR_TIMEOUT);
TIPC_SUBSCR_TIMEOUT, 0, 0);
sub->inactive = true; sub->inactive = true;
spin_unlock(&sub->lock); spin_unlock(&sub->lock);
} }
...@@ -134,12 +132,14 @@ struct tipc_subscription *tipc_sub_subscribe(struct net *net, ...@@ -134,12 +132,14 @@ struct tipc_subscription *tipc_sub_subscribe(struct net *net,
struct tipc_subscr *s, struct tipc_subscr *s,
int conid) int conid)
{ {
u32 lower = tipc_sub_read(s, seq.lower);
u32 upper = tipc_sub_read(s, seq.upper);
u32 filter = tipc_sub_read(s, filter); u32 filter = tipc_sub_read(s, filter);
struct tipc_subscription *sub; struct tipc_subscription *sub;
u32 timeout; u32 timeout;
if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) || if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
(tipc_sub_read(s, seq.lower) > tipc_sub_read(s, seq.upper))) { lower > upper) {
pr_warn("Subscription rejected, illegal request\n"); pr_warn("Subscription rejected, illegal request\n");
return NULL; return NULL;
} }
...@@ -154,6 +154,12 @@ struct tipc_subscription *tipc_sub_subscribe(struct net *net, ...@@ -154,6 +154,12 @@ struct tipc_subscription *tipc_sub_subscribe(struct net *net,
sub->conid = conid; sub->conid = conid;
sub->inactive = false; sub->inactive = false;
memcpy(&sub->evt.s, s, sizeof(*s)); memcpy(&sub->evt.s, s, sizeof(*s));
sub->s.seq.type = tipc_sub_read(s, seq.type);
sub->s.seq.lower = lower;
sub->s.seq.upper = upper;
sub->s.filter = filter;
sub->s.timeout = tipc_sub_read(s, timeout);
memcpy(sub->s.usr_handle, s->usr_handle, 8);
spin_lock_init(&sub->lock); spin_lock_init(&sub->lock);
kref_init(&sub->kref); kref_init(&sub->kref);
if (!tipc_nametbl_subscribe(sub)) { if (!tipc_nametbl_subscribe(sub)) {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2003-2017, Ericsson AB * Copyright (c) 2003-2017, Ericsson AB
* Copyright (c) 2005-2007, 2012-2013, Wind River Systems * Copyright (c) 2005-2007, 2012-2013, Wind River Systems
* Copyright (c) 2020, Red Hat Inc * Copyright (c) 2020-2021, Red Hat Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#define TIPC_MAX_SUBSCR 65535 #define TIPC_MAX_SUBSCR 65535
#define TIPC_MAX_PUBL 65535 #define TIPC_MAX_PUBL 65535
struct publication;
struct tipc_subscription; struct tipc_subscription;
struct tipc_conn; struct tipc_conn;
...@@ -59,12 +60,13 @@ struct tipc_conn; ...@@ -59,12 +60,13 @@ struct tipc_conn;
* @lock: serialize up/down and timer events * @lock: serialize up/down and timer events
*/ */
struct tipc_subscription { struct tipc_subscription {
struct tipc_subscr s;
struct tipc_event evt;
struct kref kref; struct kref kref;
struct net *net; struct net *net;
struct timer_list timer; struct timer_list timer;
struct list_head service_list; struct list_head service_list;
struct list_head sub_list; struct list_head sub_list;
struct tipc_event evt;
int conid; int conid;
bool inactive; bool inactive;
spinlock_t lock; spinlock_t lock;
...@@ -74,13 +76,9 @@ struct tipc_subscription *tipc_sub_subscribe(struct net *net, ...@@ -74,13 +76,9 @@ struct tipc_subscription *tipc_sub_subscribe(struct net *net,
struct tipc_subscr *s, struct tipc_subscr *s,
int conid); int conid);
void tipc_sub_unsubscribe(struct tipc_subscription *sub); void tipc_sub_unsubscribe(struct tipc_subscription *sub);
int tipc_sub_check_overlap(struct tipc_service_range *seq,
u32 found_lower, u32 found_upper);
void tipc_sub_report_overlap(struct tipc_subscription *sub, void tipc_sub_report_overlap(struct tipc_subscription *sub,
u32 found_lower, u32 found_upper, struct publication *p,
u32 event, u32 port, u32 node, u32 event, bool must);
u32 scope, int must);
int __net_init tipc_topsrv_init_net(struct net *net); int __net_init tipc_topsrv_init_net(struct net *net);
void __net_exit tipc_topsrv_exit_net(struct net *net); void __net_exit tipc_topsrv_exit_net(struct net *net);
......
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