]> git.ipfire.org Git - thirdparty/linux.git/blame - net/tipc/name_table.c
tun: Do SIOCGSKNS out of rtnl_lock()
[thirdparty/linux.git] / net / tipc / name_table.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/name_table.c: TIPC name table code
c4307285 3 *
e50e73e1 4 * Copyright (c) 2000-2006, 2014-2018, Ericsson AB
993bfe5d 5 * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
b97bf3fd 19 *
9ea1fd3c
PL
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
4ac1c8d0 37#include <net/sock.h>
b97bf3fd 38#include "core.h"
22ae7cff 39#include "netlink.h"
b97bf3fd
PL
40#include "name_table.h"
41#include "name_distr.h"
b97bf3fd 42#include "subscr.h"
1da46568 43#include "bcast.h"
22ae7cff 44#include "addr.h"
1d7e1c25 45#include "node.h"
75da2163 46#include "group.h"
b97bf3fd
PL
47
48/**
218527fe
JM
49 * struct service_range - container for all bindings of a service range
50 * @lower: service range lower bound
51 * @upper: service range upper bound
52 * @tree_node: member of service range RB tree
53 * @local_publ: list of identical publications made from this node
54 * Used by closest_first lookup and multicast lookup algorithm
55 * @all_publ: all publications identical to this one, whatever node and scope
56 * Used by round-robin lookup algorithm
b97bf3fd 57 */
218527fe 58struct service_range {
b52124a5
AS
59 u32 lower;
60 u32 upper;
218527fe
JM
61 struct rb_node tree_node;
62 struct list_head local_publ;
63 struct list_head all_publ;
b52124a5
AS
64};
65
c4307285 66/**
218527fe
JM
67 * struct tipc_service - container for all published instances of a service type
68 * @type: 32 bit 'type' value for service
69 * @ranges: rb tree containing all service ranges for this service
70 * @service_list: links to adjacent name ranges in hash chain
71 * @subscriptions: list of subscriptions for this service type
72 * @lock: spinlock controlling access to pertaining service ranges/publications
97ede29e 73 * @rcu: RCU callback head used for deferred freeing
b97bf3fd 74 */
218527fe 75struct tipc_service {
b97bf3fd 76 u32 type;
218527fe
JM
77 struct rb_root ranges;
78 struct hlist_node service_list;
b97bf3fd 79 struct list_head subscriptions;
218527fe 80 spinlock_t lock; /* Covers service range list */
97ede29e 81 struct rcu_head rcu;
b97bf3fd
PL
82};
83
05790c64 84static int hash(int x)
b97bf3fd 85{
f046e7d9 86 return x & (TIPC_NAMETBL_SIZE - 1);
b97bf3fd
PL
87}
88
89/**
218527fe 90 * tipc_publ_create - create a publication structure
b97bf3fd 91 */
218527fe
JM
92static struct publication *tipc_publ_create(u32 type, u32 lower, u32 upper,
93 u32 scope, u32 node, u32 port,
94 u32 key)
b97bf3fd 95{
0da974f4 96 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
218527fe
JM
97
98 if (!publ)
1fc54d8f 99 return NULL;
b97bf3fd 100
b97bf3fd
PL
101 publ->type = type;
102 publ->lower = lower;
103 publ->upper = upper;
104 publ->scope = scope;
105 publ->node = node;
e50e73e1 106 publ->port = port;
b97bf3fd 107 publ->key = key;
e50e73e1 108 INIT_LIST_HEAD(&publ->binding_sock);
218527fe
JM
109 INIT_LIST_HEAD(&publ->binding_node);
110 INIT_LIST_HEAD(&publ->local_publ);
111 INIT_LIST_HEAD(&publ->all_publ);
b97bf3fd
PL
112 return publ;
113}
114
115/**
218527fe 116 * tipc_service_create - create a service structure for the specified 'type'
c4307285 117 *
218527fe 118 * Allocates a single range structure and sets it to all 0's.
b97bf3fd 119 */
218527fe 120static struct tipc_service *tipc_service_create(u32 type, struct hlist_head *hd)
b97bf3fd 121{
218527fe 122 struct tipc_service *service = kzalloc(sizeof(*service), GFP_ATOMIC);
b97bf3fd 123
218527fe
JM
124 if (!service) {
125 pr_warn("Service creation failed, no memory\n");
1fc54d8f 126 return NULL;
b97bf3fd
PL
127 }
128
218527fe
JM
129 spin_lock_init(&service->lock);
130 service->type = type;
131 service->ranges = RB_ROOT;
132 INIT_HLIST_NODE(&service->service_list);
133 INIT_LIST_HEAD(&service->subscriptions);
134 hlist_add_head_rcu(&service->service_list, hd);
135 return service;
b97bf3fd
PL
136}
137
2c53040f 138/**
218527fe 139 * tipc_service_find_range - find service range matching a service instance
c4307285 140 *
218527fe 141 * Very time-critical, so binary search through range rb tree
b97bf3fd 142 */
218527fe
JM
143static struct service_range *tipc_service_find_range(struct tipc_service *sc,
144 u32 instance)
b97bf3fd 145{
218527fe
JM
146 struct rb_node *n = sc->ranges.rb_node;
147 struct service_range *sr;
148
149 while (n) {
150 sr = container_of(n, struct service_range, tree_node);
151 if (sr->lower > instance)
152 n = n->rb_left;
153 else if (sr->upper < instance)
154 n = n->rb_right;
b97bf3fd 155 else
218527fe 156 return sr;
b97bf3fd 157 }
1fc54d8f 158 return NULL;
b97bf3fd
PL
159}
160
218527fe
JM
161static struct service_range *tipc_service_create_range(struct tipc_service *sc,
162 u32 lower, u32 upper)
b97bf3fd 163{
218527fe
JM
164 struct rb_node **n, *parent = NULL;
165 struct service_range *sr, *tmp;
166
167 n = &sc->ranges.rb_node;
168 while (*n) {
169 tmp = container_of(*n, struct service_range, tree_node);
170 parent = *n;
171 tmp = container_of(parent, struct service_range, tree_node);
172 if (lower < tmp->lower)
173 n = &(*n)->rb_left;
37922ea4
JM
174 else if (lower > tmp->lower)
175 n = &(*n)->rb_right;
176 else if (upper < tmp->upper)
177 n = &(*n)->rb_left;
218527fe
JM
178 else if (upper > tmp->upper)
179 n = &(*n)->rb_right;
b97bf3fd 180 else
37922ea4 181 return tmp;
b97bf3fd 182 }
218527fe
JM
183 sr = kzalloc(sizeof(*sr), GFP_ATOMIC);
184 if (!sr)
185 return NULL;
186 sr->lower = lower;
187 sr->upper = upper;
188 INIT_LIST_HEAD(&sr->local_publ);
189 INIT_LIST_HEAD(&sr->all_publ);
190 rb_link_node(&sr->tree_node, parent, n);
191 rb_insert_color(&sr->tree_node, &sc->ranges);
192 return sr;
b97bf3fd
PL
193}
194
218527fe
JM
195static struct publication *tipc_service_insert_publ(struct net *net,
196 struct tipc_service *sc,
34747539
YX
197 u32 type, u32 lower,
198 u32 upper, u32 scope,
218527fe
JM
199 u32 node, u32 port,
200 u32 key)
b97bf3fd 201{
218527fe
JM
202 struct tipc_subscription *sub, *tmp;
203 struct service_range *sr;
204 struct publication *p;
205 bool first = false;
b52124a5 206
37922ea4
JM
207 sr = tipc_service_create_range(sc, lower, upper);
208 if (!sr)
209 goto err;
b97bf3fd 210
37922ea4 211 first = list_empty(&sr->all_publ);
b97bf3fd 212
218527fe
JM
213 /* Return if the publication already exists */
214 list_for_each_entry(p, &sr->all_publ, all_publ) {
215 if (p->key == key && (!p->node || p->node == node))
216 return NULL;
217 }
b97bf3fd 218
218527fe
JM
219 /* Create and insert publication */
220 p = tipc_publ_create(type, lower, upper, scope, node, port, key);
221 if (!p)
222 goto err;
ba765ec6 223 if (in_own_node(net, node))
218527fe
JM
224 list_add(&p->local_publ, &sr->local_publ);
225 list_add(&p->all_publ, &sr->all_publ);
b97bf3fd 226
617d3c7a 227 /* Any subscriptions waiting for notification? */
218527fe
JM
228 list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
229 tipc_sub_report_overlap(sub, p->lower, p->upper, TIPC_PUBLISHED,
230 p->port, p->node, p->scope, first);
b97bf3fd 231 }
218527fe
JM
232 return p;
233err:
234 pr_warn("Failed to bind to %u,%u,%u, no memory\n", type, lower, upper);
235 return NULL;
b97bf3fd
PL
236}
237
238/**
218527fe 239 * tipc_service_remove_publ - remove a publication from a service
b97bf3fd 240 */
218527fe
JM
241static struct publication *tipc_service_remove_publ(struct net *net,
242 struct tipc_service *sc,
37922ea4 243 u32 lower, u32 upper,
be47e41d
JM
244 u32 node, u32 key,
245 struct service_range **rng)
b97bf3fd 246{
218527fe
JM
247 struct tipc_subscription *sub, *tmp;
248 struct service_range *sr;
249 struct publication *p;
250 bool found = false;
251 bool last = false;
37922ea4 252 struct rb_node *n;
f131072c 253
37922ea4 254 sr = tipc_service_find_range(sc, lower);
218527fe
JM
255 if (!sr)
256 return NULL;
b52124a5 257
37922ea4
JM
258 /* Find exact matching service range */
259 for (n = &sr->tree_node; n; n = rb_next(n)) {
260 sr = container_of(n, struct service_range, tree_node);
261 if (sr->upper == upper)
262 break;
263 }
264 if (!n || sr->lower != lower || sr->upper != upper)
265 return NULL;
266
218527fe
JM
267 /* Find publication, if it exists */
268 list_for_each_entry(p, &sr->all_publ, all_publ) {
269 if (p->key != key || (node && node != p->node))
270 continue;
271 found = true;
272 break;
f6f0a4d2 273 }
218527fe
JM
274 if (!found)
275 return NULL;
c4307285 276
218527fe
JM
277 list_del(&p->all_publ);
278 list_del(&p->local_publ);
be47e41d 279 if (list_empty(&sr->all_publ))
218527fe 280 last = true;
b97bf3fd 281
f131072c 282 /* Notify any waiting subscriptions */
218527fe
JM
283 list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
284 tipc_sub_report_overlap(sub, p->lower, p->upper, TIPC_WITHDRAWN,
285 p->port, p->node, p->scope, last);
b97bf3fd 286 }
be47e41d 287 *rng = sr;
218527fe 288 return p;
b97bf3fd
PL
289}
290
291/**
218527fe
JM
292 * tipc_service_subscribe - attach a subscription, and optionally
293 * issue the prescribed number of events if there is any service
294 * range overlapping with the requested range
b97bf3fd 295 */
218527fe 296static void tipc_service_subscribe(struct tipc_service *service,
8985ecc7 297 struct tipc_subscription *sub)
b97bf3fd 298{
218527fe
JM
299 struct tipc_subscr *sb = &sub->evt.s;
300 struct service_range *sr;
a4273c73 301 struct tipc_name_seq ns;
218527fe
JM
302 struct publication *p;
303 struct rb_node *n;
304 bool first;
a4273c73 305
218527fe
JM
306 ns.type = tipc_sub_read(sb, seq.type);
307 ns.lower = tipc_sub_read(sb, seq.lower);
308 ns.upper = tipc_sub_read(sb, seq.upper);
b97bf3fd 309
da0a75e8 310 tipc_sub_get(sub);
218527fe 311 list_add(&sub->service_list, &service->subscriptions);
b97bf3fd 312
218527fe 313 if (tipc_sub_read(sb, filter) & TIPC_SUB_NO_STATUS)
b97bf3fd
PL
314 return;
315
218527fe
JM
316 for (n = rb_first(&service->ranges); n; n = rb_next(n)) {
317 sr = container_of(n, struct service_range, tree_node);
318 if (sr->lower > ns.upper)
319 break;
320 if (!tipc_sub_check_overlap(&ns, sr->lower, sr->upper))
321 continue;
322 first = true;
323
324 list_for_each_entry(p, &sr->all_publ, all_publ) {
325 tipc_sub_report_overlap(sub, sr->lower, sr->upper,
326 TIPC_PUBLISHED, p->port,
327 p->node, p->scope, first);
328 first = false;
b97bf3fd 329 }
b97bf3fd
PL
330 }
331}
332
218527fe 333static struct tipc_service *tipc_service_find(struct net *net, u32 type)
b97bf3fd 334{
218527fe
JM
335 struct name_table *nt = tipc_name_table(net);
336 struct hlist_head *service_head;
337 struct tipc_service *service;
b97bf3fd 338
218527fe
JM
339 service_head = &nt->services[hash(type)];
340 hlist_for_each_entry_rcu(service, service_head, service_list) {
341 if (service->type == type)
342 return service;
343 }
1fc54d8f 344 return NULL;
b97bf3fd
PL
345};
346
4ac1c8d0 347struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
218527fe
JM
348 u32 lower, u32 upper,
349 u32 scope, u32 node,
350 u32 port, u32 key)
b97bf3fd 351{
218527fe
JM
352 struct name_table *nt = tipc_name_table(net);
353 struct tipc_service *sc;
354 struct publication *p;
b97bf3fd 355
928df188 356 if (scope > TIPC_NODE_SCOPE || lower > upper) {
218527fe 357 pr_debug("Failed to bind illegal {%u,%u,%u} with scope %u\n",
2cf8aa19 358 type, lower, upper, scope);
1fc54d8f 359 return NULL;
b97bf3fd 360 }
218527fe
JM
361 sc = tipc_service_find(net, type);
362 if (!sc)
363 sc = tipc_service_create(type, &nt->services[hash(type)]);
364 if (!sc)
1fc54d8f 365 return NULL;
b97bf3fd 366
218527fe
JM
367 spin_lock_bh(&sc->lock);
368 p = tipc_service_insert_publ(net, sc, type, lower, upper,
369 scope, node, port, key);
370 spin_unlock_bh(&sc->lock);
371 return p;
b97bf3fd
PL
372}
373
4ac1c8d0 374struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
37922ea4
JM
375 u32 lower, u32 upper,
376 u32 node, u32 key)
b97bf3fd 377{
218527fe 378 struct tipc_service *sc = tipc_service_find(net, type);
be47e41d 379 struct service_range *sr = NULL;
218527fe 380 struct publication *p = NULL;
b97bf3fd 381
218527fe 382 if (!sc)
1fc54d8f 383 return NULL;
b97bf3fd 384
218527fe 385 spin_lock_bh(&sc->lock);
be47e41d
JM
386 p = tipc_service_remove_publ(net, sc, lower, upper, node, key, &sr);
387
388 /* Remove service range item if this was its last publication */
389 if (sr && list_empty(&sr->all_publ)) {
390 rb_erase(&sr->tree_node, &sc->ranges);
391 kfree(sr);
392 }
218527fe
JM
393
394 /* Delete service item if this no more publications and subscriptions */
395 if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
396 hlist_del_init_rcu(&sc->service_list);
397 kfree_rcu(sc, rcu);
fb9962f3 398 }
218527fe
JM
399 spin_unlock_bh(&sc->lock);
400 return p;
b97bf3fd
PL
401}
402
2c53040f 403/**
218527fe 404 * tipc_nametbl_translate - perform service instance to socket translation
b97bf3fd 405 *
f20889f7 406 * On entry, 'dnode' is the search domain used during translation.
bc9f8143
AS
407 *
408 * On exit:
f20889f7
JM
409 * - if translation is deferred to another node, leave 'dnode' unchanged and
410 * return 0
411 * - if translation is attempted and succeeds, set 'dnode' to the publishing
412 * node and return the published (non-zero) port number
413 * - if translation is attempted and fails, set 'dnode' to 0 and return 0
414 *
415 * Note that for legacy users (node configured with Z.C.N address format) the
416 * 'closest-first' lookup algorithm must be maintained, i.e., if dnode is 0
417 * we must look in the local binding list first
b97bf3fd 418 */
f20889f7 419u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *dnode)
b97bf3fd 420{
b89afb11
JM
421 struct tipc_net *tn = tipc_net(net);
422 bool legacy = tn->legacy_addr_format;
423 u32 self = tipc_own_addr(net);
218527fe
JM
424 struct service_range *sr;
425 struct tipc_service *sc;
f20889f7 426 struct list_head *list;
218527fe 427 struct publication *p;
e50e73e1 428 u32 port = 0;
bc9f8143 429 u32 node = 0;
b97bf3fd 430
f20889f7 431 if (!tipc_in_scope(legacy, *dnode, self))
b97bf3fd
PL
432 return 0;
433
97ede29e 434 rcu_read_lock();
218527fe
JM
435 sc = tipc_service_find(net, type);
436 if (unlikely(!sc))
b97bf3fd 437 goto not_found;
218527fe
JM
438
439 spin_lock_bh(&sc->lock);
440 sr = tipc_service_find_range(sc, instance);
441 if (unlikely(!sr))
fb9962f3 442 goto no_match;
b97bf3fd 443
f20889f7
JM
444 /* Select lookup algorithm: local, closest-first or round-robin */
445 if (*dnode == self) {
446 list = &sr->local_publ;
447 if (list_empty(list))
f6f0a4d2 448 goto no_match;
f20889f7
JM
449 p = list_first_entry(list, struct publication, local_publ);
450 list_move_tail(&p->local_publ, &sr->local_publ);
451 } else if (legacy && !*dnode && !list_empty(&sr->local_publ)) {
452 list = &sr->local_publ;
453 p = list_first_entry(list, struct publication, local_publ);
218527fe 454 list_move_tail(&p->local_publ, &sr->local_publ);
ba765ec6 455 } else {
f20889f7
JM
456 list = &sr->all_publ;
457 p = list_first_entry(list, struct publication, all_publ);
218527fe 458 list_move_tail(&p->all_publ, &sr->all_publ);
b97bf3fd 459 }
218527fe
JM
460 port = p->port;
461 node = p->node;
f6f0a4d2 462no_match:
218527fe 463 spin_unlock_bh(&sc->lock);
b97bf3fd 464not_found:
97ede29e 465 rcu_read_unlock();
f20889f7 466 *dnode = node;
e50e73e1 467 return port;
b97bf3fd
PL
468}
469
232d07b7 470bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
ee106d7f
JM
471 struct list_head *dsts, int *dstcnt, u32 exclude,
472 bool all)
473{
474 u32 self = tipc_own_addr(net);
218527fe
JM
475 struct service_range *sr;
476 struct tipc_service *sc;
477 struct publication *p;
ee106d7f 478
ee106d7f
JM
479 *dstcnt = 0;
480 rcu_read_lock();
218527fe
JM
481 sc = tipc_service_find(net, type);
482 if (unlikely(!sc))
ee106d7f 483 goto exit;
218527fe
JM
484
485 spin_lock_bh(&sc->lock);
486
487 sr = tipc_service_find_range(sc, instance);
488 if (!sr)
489 goto no_match;
490
491 list_for_each_entry(p, &sr->all_publ, all_publ) {
492 if (p->scope != scope)
493 continue;
494 if (p->port == exclude && p->node == self)
495 continue;
496 tipc_dest_push(dsts, p->node, p->port);
497 (*dstcnt)++;
498 if (all)
499 continue;
500 list_move_tail(&p->all_publ, &sr->all_publ);
501 break;
ee106d7f 502 }
218527fe
JM
503no_match:
504 spin_unlock_bh(&sc->lock);
ee106d7f
JM
505exit:
506 rcu_read_unlock();
507 return !list_empty(dsts);
508}
509
ba765ec6
JM
510void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
511 u32 scope, bool exact, struct list_head *dports)
b97bf3fd 512{
218527fe
JM
513 struct service_range *sr;
514 struct tipc_service *sc;
232d07b7 515 struct publication *p;
218527fe 516 struct rb_node *n;
b97bf3fd 517
97ede29e 518 rcu_read_lock();
218527fe
JM
519 sc = tipc_service_find(net, type);
520 if (!sc)
b97bf3fd
PL
521 goto exit;
522
218527fe
JM
523 spin_lock_bh(&sc->lock);
524
525 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
526 sr = container_of(n, struct service_range, tree_node);
527 if (sr->upper < lower)
528 continue;
529 if (sr->lower > upper)
b97bf3fd 530 break;
218527fe 531 list_for_each_entry(p, &sr->local_publ, local_publ) {
232d07b7 532 if (p->scope == scope || (!exact && p->scope < scope))
e50e73e1 533 tipc_dest_push(dports, 0, p->port);
968edbe1 534 }
b97bf3fd 535 }
218527fe 536 spin_unlock_bh(&sc->lock);
b97bf3fd 537exit:
97ede29e 538 rcu_read_unlock();
b97bf3fd
PL
539}
540
2ae0b8af
JPM
541/* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
542 * - Creates list of nodes that overlap the given multicast address
218527fe 543 * - Determines if any node local destinations overlap
2ae0b8af
JPM
544 */
545void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
e9a03445 546 u32 upper, struct tipc_nlist *nodes)
2ae0b8af 547{
218527fe
JM
548 struct service_range *sr;
549 struct tipc_service *sc;
550 struct publication *p;
551 struct rb_node *n;
2ae0b8af
JPM
552
553 rcu_read_lock();
218527fe
JM
554 sc = tipc_service_find(net, type);
555 if (!sc)
2ae0b8af
JPM
556 goto exit;
557
218527fe
JM
558 spin_lock_bh(&sc->lock);
559
560 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
561 sr = container_of(n, struct service_range, tree_node);
562 if (sr->upper < lower)
563 continue;
564 if (sr->lower > upper)
565 break;
566 list_for_each_entry(p, &sr->all_publ, all_publ) {
567 tipc_nlist_add(nodes, p->node);
2ae0b8af
JPM
568 }
569 }
218527fe 570 spin_unlock_bh(&sc->lock);
2ae0b8af
JPM
571exit:
572 rcu_read_unlock();
573}
574
75da2163
JM
575/* tipc_nametbl_build_group - build list of communication group members
576 */
577void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
232d07b7 578 u32 type, u32 scope)
75da2163 579{
218527fe
JM
580 struct service_range *sr;
581 struct tipc_service *sc;
75da2163 582 struct publication *p;
218527fe 583 struct rb_node *n;
75da2163
JM
584
585 rcu_read_lock();
218527fe
JM
586 sc = tipc_service_find(net, type);
587 if (!sc)
75da2163
JM
588 goto exit;
589
218527fe
JM
590 spin_lock_bh(&sc->lock);
591 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
592 sr = container_of(n, struct service_range, tree_node);
593 list_for_each_entry(p, &sr->all_publ, all_publ) {
232d07b7 594 if (p->scope != scope)
75da2163 595 continue;
e50e73e1 596 tipc_group_add_member(grp, p->node, p->port, p->lower);
75da2163
JM
597 }
598 }
218527fe 599 spin_unlock_bh(&sc->lock);
75da2163
JM
600exit:
601 rcu_read_unlock();
602}
603
218527fe 604/* tipc_nametbl_publish - add service binding to name table
b97bf3fd 605 */
f2f9800d 606struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
218527fe 607 u32 upper, u32 scope, u32 port,
f2f9800d 608 u32 key)
b97bf3fd 609{
218527fe
JM
610 struct name_table *nt = tipc_name_table(net);
611 struct tipc_net *tn = tipc_net(net);
612 struct publication *p = NULL;
613 struct sk_buff *skb = NULL;
b97bf3fd 614
4ac1c8d0 615 spin_lock_bh(&tn->nametbl_lock);
218527fe
JM
616
617 if (nt->local_publ_count >= TIPC_MAX_PUBL) {
618 pr_warn("Bind failed, max limit %u reached\n", TIPC_MAX_PUBL);
619 goto exit;
b97bf3fd 620 }
b97bf3fd 621
218527fe
JM
622 p = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
623 tipc_own_addr(net), port, key);
624 if (p) {
625 nt->local_publ_count++;
626 skb = tipc_named_publish(net, p);
fd6eced8 627 }
218527fe 628exit:
4ac1c8d0 629 spin_unlock_bh(&tn->nametbl_lock);
eab8c045 630
218527fe
JM
631 if (skb)
632 tipc_node_broadcast(net, skb);
633 return p;
b97bf3fd
PL
634}
635
636/**
218527fe 637 * tipc_nametbl_withdraw - withdraw a service binding
b97bf3fd 638 */
218527fe 639int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower,
37922ea4 640 u32 upper, u32 key)
b97bf3fd 641{
218527fe
JM
642 struct name_table *nt = tipc_name_table(net);
643 struct tipc_net *tn = tipc_net(net);
644 u32 self = tipc_own_addr(net);
5492390a 645 struct sk_buff *skb = NULL;
218527fe 646 struct publication *p;
b97bf3fd 647
4ac1c8d0 648 spin_lock_bh(&tn->nametbl_lock);
218527fe 649
37922ea4 650 p = tipc_nametbl_remove_publ(net, type, lower, upper, self, key);
218527fe
JM
651 if (p) {
652 nt->local_publ_count--;
653 skb = tipc_named_withdraw(net, p);
218527fe
JM
654 list_del_init(&p->binding_sock);
655 kfree_rcu(p, rcu);
5492390a 656 } else {
218527fe 657 pr_err("Failed to remove local publication {%u,%u,%u}/%u\n",
37922ea4 658 type, lower, upper, key);
5492390a 659 }
4ac1c8d0 660 spin_unlock_bh(&tn->nametbl_lock);
eab8c045 661
5492390a 662 if (skb) {
1d7e1c25 663 tipc_node_broadcast(net, skb);
b97bf3fd
PL
664 return 1;
665 }
b97bf3fd
PL
666 return 0;
667}
668
669/**
4323add6 670 * tipc_nametbl_subscribe - add a subscription object to the name table
b97bf3fd 671 */
c3317f4d 672bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
b97bf3fd 673{
218527fe 674 struct name_table *nt = tipc_name_table(sub->net);
5c45ab24 675 struct tipc_net *tn = tipc_net(sub->net);
8985ecc7
JM
676 struct tipc_subscr *s = &sub->evt.s;
677 u32 type = tipc_sub_read(s, seq.type);
218527fe 678 struct tipc_service *sc;
c3317f4d 679 bool res = true;
b97bf3fd 680
4ac1c8d0 681 spin_lock_bh(&tn->nametbl_lock);
218527fe
JM
682 sc = tipc_service_find(sub->net, type);
683 if (!sc)
684 sc = tipc_service_create(type, &nt->services[hash(type)]);
685 if (sc) {
686 spin_lock_bh(&sc->lock);
687 tipc_service_subscribe(sc, sub);
688 spin_unlock_bh(&sc->lock);
c4307285 689 } else {
218527fe
JM
690 pr_warn("Failed to subscribe for {%u,%u,%u}\n", type,
691 tipc_sub_read(s, seq.lower),
692 tipc_sub_read(s, seq.upper));
c3317f4d 693 res = false;
c4307285 694 }
4ac1c8d0 695 spin_unlock_bh(&tn->nametbl_lock);
c3317f4d 696 return res;
b97bf3fd
PL
697}
698
699/**
4323add6 700 * tipc_nametbl_unsubscribe - remove a subscription object from name table
b97bf3fd 701 */
8985ecc7 702void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
b97bf3fd 703{
5c45ab24 704 struct tipc_net *tn = tipc_net(sub->net);
218527fe 705 struct tipc_subscr *s = &sub->evt.s;
8985ecc7 706 u32 type = tipc_sub_read(s, seq.type);
218527fe 707 struct tipc_service *sc;
b97bf3fd 708
4ac1c8d0 709 spin_lock_bh(&tn->nametbl_lock);
218527fe
JM
710 sc = tipc_service_find(sub->net, type);
711 if (!sc)
712 goto exit;
713
714 spin_lock_bh(&sc->lock);
715 list_del_init(&sub->service_list);
716 tipc_sub_put(sub);
717
718 /* Delete service item if no more publications and subscriptions */
719 if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
720 hlist_del_init_rcu(&sc->service_list);
721 kfree_rcu(sc, rcu);
c4307285 722 }
218527fe
JM
723 spin_unlock_bh(&sc->lock);
724exit:
4ac1c8d0 725 spin_unlock_bh(&tn->nametbl_lock);
b97bf3fd
PL
726}
727
4ac1c8d0 728int tipc_nametbl_init(struct net *net)
b97bf3fd 729{
218527fe
JM
730 struct tipc_net *tn = tipc_net(net);
731 struct name_table *nt;
993bfe5d
YX
732 int i;
733
218527fe
JM
734 nt = kzalloc(sizeof(*nt), GFP_ATOMIC);
735 if (!nt)
b97bf3fd
PL
736 return -ENOMEM;
737
993bfe5d 738 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
218527fe 739 INIT_HLIST_HEAD(&nt->services[i]);
993bfe5d 740
218527fe
JM
741 INIT_LIST_HEAD(&nt->node_scope);
742 INIT_LIST_HEAD(&nt->cluster_scope);
743 tn->nametbl = nt;
4ac1c8d0 744 spin_lock_init(&tn->nametbl_lock);
b97bf3fd
PL
745 return 0;
746}
747
1bb8dce5 748/**
218527fe 749 * tipc_service_delete - purge all publications for a service and delete it
1bb8dce5 750 */
218527fe 751static void tipc_service_delete(struct net *net, struct tipc_service *sc)
1bb8dce5 752{
218527fe 753 struct service_range *sr, *tmpr;
be47e41d 754 struct publication *p, *tmp;
218527fe
JM
755
756 spin_lock_bh(&sc->lock);
757 rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
be47e41d 758 list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
37922ea4 759 tipc_service_remove_publ(net, sc, p->lower, p->upper,
be47e41d 760 p->node, p->key, &sr);
218527fe
JM
761 kfree_rcu(p, rcu);
762 }
be47e41d
JM
763 rb_erase(&sr->tree_node, &sc->ranges);
764 kfree(sr);
1bb8dce5 765 }
218527fe
JM
766 hlist_del_init_rcu(&sc->service_list);
767 spin_unlock_bh(&sc->lock);
768 kfree_rcu(sc, rcu);
1bb8dce5
EH
769}
770
4ac1c8d0 771void tipc_nametbl_stop(struct net *net)
b97bf3fd 772{
218527fe
JM
773 struct name_table *nt = tipc_name_table(net);
774 struct tipc_net *tn = tipc_net(net);
775 struct hlist_head *service_head;
776 struct tipc_service *service;
b97bf3fd
PL
777 u32 i;
778
1bb8dce5
EH
779 /* Verify name table is empty and purge any lingering
780 * publications, then release the name table
781 */
4ac1c8d0 782 spin_lock_bh(&tn->nametbl_lock);
f046e7d9 783 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
218527fe 784 if (hlist_empty(&nt->services[i]))
f705ab95 785 continue;
218527fe
JM
786 service_head = &nt->services[i];
787 hlist_for_each_entry_rcu(service, service_head, service_list) {
788 tipc_service_delete(net, service);
1bb8dce5 789 }
b97bf3fd 790 }
4ac1c8d0 791 spin_unlock_bh(&tn->nametbl_lock);
993bfe5d 792
97ede29e 793 synchronize_net();
218527fe 794 kfree(nt);
b97bf3fd 795}
1593123a 796
d8182804 797static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
218527fe
JM
798 struct tipc_service *service,
799 struct service_range *sr,
800 u32 *last_key)
1593123a 801{
1593123a 802 struct publication *p;
218527fe
JM
803 struct nlattr *attrs;
804 struct nlattr *b;
805 void *hdr;
1593123a 806
218527fe
JM
807 if (*last_key) {
808 list_for_each_entry(p, &sr->all_publ, all_publ)
809 if (p->key == *last_key)
1593123a 810 break;
218527fe 811 if (p->key != *last_key)
1593123a
RA
812 return -EPIPE;
813 } else {
218527fe
JM
814 p = list_first_entry(&sr->all_publ,
815 struct publication,
e50e73e1 816 all_publ);
1593123a
RA
817 }
818
218527fe
JM
819 list_for_each_entry_from(p, &sr->all_publ, all_publ) {
820 *last_key = p->key;
1593123a
RA
821
822 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
bfb3e5dd 823 &tipc_genl_family, NLM_F_MULTI,
1593123a
RA
824 TIPC_NL_NAME_TABLE_GET);
825 if (!hdr)
826 return -EMSGSIZE;
827
828 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
829 if (!attrs)
830 goto msg_full;
831
218527fe
JM
832 b = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
833 if (!b)
1593123a
RA
834 goto attr_msg_full;
835
218527fe 836 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type))
1593123a 837 goto publ_msg_full;
218527fe 838 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower))
1593123a 839 goto publ_msg_full;
218527fe 840 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper))
1593123a
RA
841 goto publ_msg_full;
842 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
843 goto publ_msg_full;
844 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
845 goto publ_msg_full;
e50e73e1 846 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port))
1593123a
RA
847 goto publ_msg_full;
848 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
849 goto publ_msg_full;
850
218527fe 851 nla_nest_end(msg->skb, b);
1593123a
RA
852 nla_nest_end(msg->skb, attrs);
853 genlmsg_end(msg->skb, hdr);
854 }
218527fe 855 *last_key = 0;
1593123a
RA
856
857 return 0;
858
859publ_msg_full:
218527fe 860 nla_nest_cancel(msg->skb, b);
1593123a
RA
861attr_msg_full:
862 nla_nest_cancel(msg->skb, attrs);
863msg_full:
864 genlmsg_cancel(msg->skb, hdr);
865
866 return -EMSGSIZE;
867}
868
218527fe
JM
869static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg,
870 struct tipc_service *sc,
871 u32 *last_lower, u32 *last_key)
1593123a 872{
218527fe
JM
873 struct service_range *sr;
874 struct rb_node *n;
1593123a
RA
875 int err;
876
218527fe
JM
877 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
878 sr = container_of(n, struct service_range, tree_node);
879 if (sr->lower < *last_lower)
880 continue;
881 err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key);
1593123a 882 if (err) {
218527fe 883 *last_lower = sr->lower;
1593123a
RA
884 return err;
885 }
886 }
887 *last_lower = 0;
1593123a
RA
888 return 0;
889}
890
218527fe
JM
891static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
892 u32 *last_type, u32 *last_lower, u32 *last_key)
1593123a 893{
218527fe
JM
894 struct tipc_net *tn = tipc_net(net);
895 struct tipc_service *service = NULL;
896 struct hlist_head *head;
1593123a
RA
897 int err;
898 int i;
899
900 if (*last_type)
901 i = hash(*last_type);
902 else
903 i = 0;
904
905 for (; i < TIPC_NAMETBL_SIZE; i++) {
218527fe 906 head = &tn->nametbl->services[i];
1593123a
RA
907
908 if (*last_type) {
218527fe
JM
909 service = tipc_service_find(net, *last_type);
910 if (!service)
1593123a
RA
911 return -EPIPE;
912 } else {
218527fe 913 hlist_for_each_entry_rcu(service, head, service_list)
97ede29e 914 break;
218527fe 915 if (!service)
1593123a
RA
916 continue;
917 }
918
218527fe
JM
919 hlist_for_each_entry_from_rcu(service, service_list) {
920 spin_lock_bh(&service->lock);
921 err = __tipc_nl_service_range_list(msg, service,
922 last_lower,
923 last_key);
1593123a
RA
924
925 if (err) {
218527fe
JM
926 *last_type = service->type;
927 spin_unlock_bh(&service->lock);
1593123a
RA
928 return err;
929 }
218527fe 930 spin_unlock_bh(&service->lock);
1593123a
RA
931 }
932 *last_type = 0;
933 }
934 return 0;
935}
936
937int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
938{
218527fe 939 struct net *net = sock_net(skb->sk);
1593123a
RA
940 u32 last_type = cb->args[0];
941 u32 last_lower = cb->args[1];
218527fe
JM
942 u32 last_key = cb->args[2];
943 int done = cb->args[3];
1593123a 944 struct tipc_nl_msg msg;
218527fe 945 int err;
1593123a
RA
946
947 if (done)
948 return 0;
949
950 msg.skb = skb;
951 msg.portid = NETLINK_CB(cb->skb).portid;
952 msg.seq = cb->nlh->nlmsg_seq;
953
97ede29e 954 rcu_read_lock();
218527fe
JM
955 err = tipc_nl_service_list(net, &msg, &last_type,
956 &last_lower, &last_key);
1593123a
RA
957 if (!err) {
958 done = 1;
959 } else if (err != -EMSGSIZE) {
960 /* We never set seq or call nl_dump_check_consistent() this
961 * means that setting prev_seq here will cause the consistence
962 * check to fail in the netlink callback handler. Resulting in
963 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
964 * we got an error.
965 */
966 cb->prev_seq = 1;
967 }
97ede29e 968 rcu_read_unlock();
1593123a
RA
969
970 cb->args[0] = last_type;
971 cb->args[1] = last_lower;
218527fe 972 cb->args[2] = last_key;
1593123a
RA
973 cb->args[3] = done;
974
975 return skb->len;
976}
3c724acd 977
a80ae530 978struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
3c724acd 979{
a80ae530
JM
980 u64 value = (u64)node << 32 | port;
981 struct tipc_dest *dst;
3c724acd 982
a80ae530
JM
983 list_for_each_entry(dst, l, list) {
984 if (dst->value != value)
985 continue;
986 return dst;
3c724acd 987 }
a80ae530 988 return NULL;
4d8642d8
JPM
989}
990
a80ae530 991bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
4d8642d8 992{
a80ae530
JM
993 u64 value = (u64)node << 32 | port;
994 struct tipc_dest *dst;
4d8642d8 995
a80ae530 996 if (tipc_dest_find(l, node, port))
4d8642d8
JPM
997 return false;
998
a80ae530
JM
999 dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1000 if (unlikely(!dst))
1001 return false;
1002 dst->value = value;
1003 list_add(&dst->list, l);
4d8642d8
JPM
1004 return true;
1005}
1006
a80ae530 1007bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
4d8642d8 1008{
a80ae530 1009 struct tipc_dest *dst;
4d8642d8
JPM
1010
1011 if (list_empty(l))
a80ae530
JM
1012 return false;
1013 dst = list_first_entry(l, typeof(*dst), list);
1014 if (port)
1015 *port = dst->port;
1016 if (node)
1017 *node = dst->node;
1018 list_del(&dst->list);
1019 kfree(dst);
1020 return true;
4d8642d8
JPM
1021}
1022
a80ae530 1023bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
4d8642d8 1024{
a80ae530 1025 struct tipc_dest *dst;
4d8642d8 1026
a80ae530
JM
1027 dst = tipc_dest_find(l, node, port);
1028 if (!dst)
1029 return false;
1030 list_del(&dst->list);
1031 kfree(dst);
1032 return true;
4d8642d8
JPM
1033}
1034
a80ae530 1035void tipc_dest_list_purge(struct list_head *l)
4d8642d8 1036{
a80ae530 1037 struct tipc_dest *dst, *tmp;
4d8642d8 1038
a80ae530
JM
1039 list_for_each_entry_safe(dst, tmp, l, list) {
1040 list_del(&dst->list);
1041 kfree(dst);
3c724acd
JPM
1042 }
1043}
1044
a80ae530 1045int tipc_dest_list_len(struct list_head *l)
3c724acd 1046{
a80ae530 1047 struct tipc_dest *dst;
4d8642d8 1048 int i = 0;
3c724acd 1049
a80ae530 1050 list_for_each_entry(dst, l, list) {
4d8642d8 1051 i++;
3c724acd 1052 }
4d8642d8 1053 return i;
3c724acd 1054}