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