xref: /linux/net/tipc/name_table.c (revision b3b76e9f4f2476f1135b2ba7743a821db4a0df4b)
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-2021, 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  * @ua: the service range the user is binding to
226  * @sk: the address of the socket that is bound
227  * @key: publication key
228  */
229 static struct publication *tipc_publ_create(struct tipc_uaddr *ua,
230 					    struct tipc_socket_addr *sk,
231 					    u32 key)
232 {
233 	struct publication *p = kzalloc_obj(*p, GFP_ATOMIC);
234 
235 	if (!p)
236 		return NULL;
237 
238 	p->sr = ua->sr;
239 	p->sk = *sk;
240 	p->scope = ua->scope;
241 	p->key = key;
242 	INIT_LIST_HEAD(&p->binding_sock);
243 	INIT_LIST_HEAD(&p->binding_node);
244 	INIT_LIST_HEAD(&p->local_publ);
245 	INIT_LIST_HEAD(&p->all_publ);
246 	INIT_LIST_HEAD(&p->list);
247 	return p;
248 }
249 
250 /**
251  * tipc_service_create - create a service structure for the specified 'type'
252  * @net: network namespace
253  * @ua: address representing the service to be bound
254  *
255  * Allocates a single range structure and sets it to all 0's.
256  */
257 static struct tipc_service *tipc_service_create(struct net *net,
258 						struct tipc_uaddr *ua)
259 {
260 	struct name_table *nt = tipc_name_table(net);
261 	struct tipc_service *service;
262 	struct hlist_head *hd;
263 
264 	service = kzalloc_obj(*service, GFP_ATOMIC);
265 	if (!service) {
266 		pr_warn("Service creation failed, no memory\n");
267 		return NULL;
268 	}
269 
270 	spin_lock_init(&service->lock);
271 	service->type = ua->sr.type;
272 	service->ranges = RB_ROOT;
273 	INIT_HLIST_NODE(&service->service_list);
274 	INIT_LIST_HEAD(&service->subscriptions);
275 	hd = &nt->services[hash(ua->sr.type)];
276 	hlist_add_head_rcu(&service->service_list, hd);
277 	return service;
278 }
279 
280 /*  tipc_service_find_range - find service range matching publication parameters
281  */
282 static struct service_range *tipc_service_find_range(struct tipc_service *sc,
283 						     struct tipc_uaddr *ua)
284 {
285 	struct service_range *sr;
286 
287 	service_range_foreach_match(sr, sc, ua->sr.lower, ua->sr.upper) {
288 		/* Look for exact match */
289 		if (sr->lower == ua->sr.lower && sr->upper == ua->sr.upper)
290 			return sr;
291 	}
292 
293 	return NULL;
294 }
295 
296 static struct service_range *tipc_service_create_range(struct tipc_service *sc,
297 						       struct publication *p)
298 {
299 	struct rb_node **n, *parent = NULL;
300 	struct service_range *sr;
301 	u32 lower = p->sr.lower;
302 	u32 upper = p->sr.upper;
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_obj(*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 bool tipc_service_insert_publ(struct net *net,
331 				     struct tipc_service *sc,
332 				     struct publication *p)
333 {
334 	struct tipc_subscription *sub, *tmp;
335 	struct service_range *sr;
336 	struct publication *_p;
337 	u32 node = p->sk.node;
338 	bool first = false;
339 	bool res = false;
340 	u32 key = p->key;
341 
342 	spin_lock_bh(&sc->lock);
343 	sr = tipc_service_create_range(sc, p);
344 	if (!sr)
345 		goto  exit;
346 
347 	first = list_empty(&sr->all_publ);
348 
349 	/* Return if the publication already exists */
350 	list_for_each_entry(_p, &sr->all_publ, all_publ) {
351 		if (_p->key == key && _p->sk.ref == p->sk.ref &&
352 		    (!_p->sk.node || _p->sk.node == node)) {
353 			pr_debug("Failed to bind duplicate %u,%u,%u/%u:%u/%u\n",
354 				 p->sr.type, p->sr.lower, p->sr.upper,
355 				 node, p->sk.ref, key);
356 			goto exit;
357 		}
358 	}
359 
360 	if (in_own_node(net, p->sk.node))
361 		list_add(&p->local_publ, &sr->local_publ);
362 	list_add(&p->all_publ, &sr->all_publ);
363 	p->id = sc->publ_cnt++;
364 
365 	/* Any subscriptions waiting for notification?  */
366 	list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
367 		tipc_sub_report_overlap(sub, p, TIPC_PUBLISHED, first);
368 	}
369 	res = true;
370 exit:
371 	if (!res)
372 		pr_warn("Failed to bind to %u,%u,%u\n",
373 			p->sr.type, p->sr.lower, p->sr.upper);
374 	spin_unlock_bh(&sc->lock);
375 	return res;
376 }
377 
378 /**
379  * tipc_service_remove_publ - remove a publication from a service
380  * @r: service_range to remove publication from
381  * @sk: address publishing socket
382  * @key: target publication key
383  */
384 static struct publication *tipc_service_remove_publ(struct service_range *r,
385 						    struct tipc_socket_addr *sk,
386 						    u32 key)
387 {
388 	struct publication *p;
389 	u32 node = sk->node;
390 
391 	list_for_each_entry(p, &r->all_publ, all_publ) {
392 		if (p->key != key || p->sk.ref != sk->ref ||
393 		    (node && node != p->sk.node))
394 			continue;
395 		list_del(&p->all_publ);
396 		list_del(&p->local_publ);
397 		return p;
398 	}
399 	return NULL;
400 }
401 
402 /*
403  * Code reused: time_after32() for the same purpose
404  */
405 #define publication_after(pa, pb) time_after32((pa)->id, (pb)->id)
406 static int tipc_publ_sort(void *priv, const struct list_head *a,
407 			  const struct list_head *b)
408 {
409 	struct publication *pa, *pb;
410 
411 	pa = container_of(a, struct publication, list);
412 	pb = container_of(b, struct publication, list);
413 	return publication_after(pa, pb);
414 }
415 
416 /**
417  * tipc_service_subscribe - attach a subscription, and optionally
418  * issue the prescribed number of events if there is any service
419  * range overlapping with the requested range
420  * @service: the tipc_service to attach the @sub to
421  * @sub: the subscription to attach
422  */
423 static void tipc_service_subscribe(struct tipc_service *service,
424 				   struct tipc_subscription *sub)
425 {
426 	struct publication *p, *first, *tmp;
427 	struct list_head publ_list;
428 	struct service_range *sr;
429 	u32 filter, lower, upper;
430 
431 	filter = sub->s.filter;
432 	lower = sub->s.seq.lower;
433 	upper = sub->s.seq.upper;
434 
435 	tipc_sub_get(sub);
436 	list_add(&sub->service_list, &service->subscriptions);
437 
438 	if (filter & TIPC_SUB_NO_STATUS)
439 		return;
440 
441 	INIT_LIST_HEAD(&publ_list);
442 	service_range_foreach_match(sr, service, lower, upper) {
443 		first = NULL;
444 		list_for_each_entry(p, &sr->all_publ, all_publ) {
445 			if (filter & TIPC_SUB_PORTS)
446 				list_add_tail(&p->list, &publ_list);
447 			else if (!first || publication_after(first, p))
448 				/* Pick this range's *first* publication */
449 				first = p;
450 		}
451 		if (first)
452 			list_add_tail(&first->list, &publ_list);
453 	}
454 
455 	/* Sort the publications before reporting */
456 	list_sort(NULL, &publ_list, tipc_publ_sort);
457 	list_for_each_entry_safe(p, tmp, &publ_list, list) {
458 		tipc_sub_report_overlap(sub, p, TIPC_PUBLISHED, true);
459 		list_del_init(&p->list);
460 	}
461 }
462 
463 static struct tipc_service *tipc_service_find(struct net *net,
464 					      struct tipc_uaddr *ua)
465 {
466 	struct name_table *nt = tipc_name_table(net);
467 	struct hlist_head *service_head;
468 	struct tipc_service *service;
469 
470 	service_head = &nt->services[hash(ua->sr.type)];
471 	hlist_for_each_entry_rcu(service, service_head, service_list) {
472 		if (service->type == ua->sr.type)
473 			return service;
474 	}
475 	return NULL;
476 };
477 
478 struct publication *tipc_nametbl_insert_publ(struct net *net,
479 					     struct tipc_uaddr *ua,
480 					     struct tipc_socket_addr *sk,
481 					     u32 key)
482 {
483 	struct tipc_service *sc;
484 	struct publication *p;
485 
486 	p = tipc_publ_create(ua, sk, key);
487 	if (!p)
488 		return NULL;
489 
490 	sc = tipc_service_find(net, ua);
491 	if (!sc)
492 		sc = tipc_service_create(net, ua);
493 	if (sc && tipc_service_insert_publ(net, sc, p))
494 		return p;
495 	kfree(p);
496 	return NULL;
497 }
498 
499 struct publication *tipc_nametbl_remove_publ(struct net *net,
500 					     struct tipc_uaddr *ua,
501 					     struct tipc_socket_addr *sk,
502 					     u32 key)
503 {
504 	struct tipc_subscription *sub, *tmp;
505 	struct publication *p = NULL;
506 	struct service_range *sr;
507 	struct tipc_service *sc;
508 	bool last;
509 
510 	sc = tipc_service_find(net, ua);
511 	if (!sc)
512 		goto exit;
513 
514 	spin_lock_bh(&sc->lock);
515 	sr = tipc_service_find_range(sc, ua);
516 	if (!sr)
517 		goto unlock;
518 	p = tipc_service_remove_publ(sr, sk, key);
519 	if (!p)
520 		goto unlock;
521 
522 	/* Notify any waiting subscriptions */
523 	last = list_empty(&sr->all_publ);
524 	list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
525 		tipc_sub_report_overlap(sub, p, TIPC_WITHDRAWN, last);
526 	}
527 
528 	/* Remove service range item if this was its last publication */
529 	if (list_empty(&sr->all_publ)) {
530 		rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
531 		kfree(sr);
532 	}
533 
534 	/* Delete service item if no more publications and subscriptions */
535 	if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
536 		hlist_del_init_rcu(&sc->service_list);
537 		kfree_rcu(sc, rcu);
538 	}
539 unlock:
540 	spin_unlock_bh(&sc->lock);
541 exit:
542 	if (!p) {
543 		pr_err("Failed to remove unknown binding: %u,%u,%u/%u:%u/%u\n",
544 		       ua->sr.type, ua->sr.lower, ua->sr.upper,
545 		       sk->node, sk->ref, key);
546 	}
547 	return p;
548 }
549 
550 /**
551  * tipc_nametbl_lookup_anycast - perform service instance to socket translation
552  * @net: network namespace
553  * @ua: service address to look up
554  * @sk: address to socket we want to find
555  *
556  * On entry, a non-zero 'sk->node' indicates the node where we want lookup to be
557  * performed, which may not be this one.
558  *
559  * On exit:
560  *
561  * - If lookup is deferred to another node, leave 'sk->node' unchanged and
562  *   return 'true'.
563  * - If lookup is successful, set the 'sk->node' and 'sk->ref' (== portid) which
564  *   represent the bound socket and return 'true'.
565  * - If lookup fails, return 'false'
566  *
567  * Note that for legacy users (node configured with Z.C.N address format) the
568  * 'closest-first' lookup algorithm must be maintained, i.e., if sk.node is 0
569  * we must look in the local binding list first
570  */
571 bool tipc_nametbl_lookup_anycast(struct net *net,
572 				 struct tipc_uaddr *ua,
573 				 struct tipc_socket_addr *sk)
574 {
575 	struct tipc_net *tn = tipc_net(net);
576 	bool legacy = tn->legacy_addr_format;
577 	u32 self = tipc_own_addr(net);
578 	u32 inst = ua->sa.instance;
579 	struct service_range *r;
580 	struct tipc_service *sc;
581 	struct publication *p;
582 	struct list_head *l;
583 	bool res = false;
584 
585 	if (!tipc_in_scope(legacy, sk->node, self))
586 		return true;
587 
588 	rcu_read_lock();
589 	sc = tipc_service_find(net, ua);
590 	if (unlikely(!sc))
591 		goto exit;
592 
593 	spin_lock_bh(&sc->lock);
594 	service_range_foreach_match(r, sc, inst, inst) {
595 		/* Select lookup algo: local, closest-first or round-robin */
596 		if (sk->node == self) {
597 			l = &r->local_publ;
598 			if (list_empty(l))
599 				continue;
600 			p = list_first_entry(l, struct publication, local_publ);
601 			list_move_tail(&p->local_publ, &r->local_publ);
602 		} else if (legacy && !sk->node && !list_empty(&r->local_publ)) {
603 			l = &r->local_publ;
604 			p = list_first_entry(l, struct publication, local_publ);
605 			list_move_tail(&p->local_publ, &r->local_publ);
606 		} else {
607 			l = &r->all_publ;
608 			p = list_first_entry(l, struct publication, all_publ);
609 			list_move_tail(&p->all_publ, &r->all_publ);
610 		}
611 		*sk = p->sk;
612 		res = true;
613 		/* Todo: as for legacy, pick the first matching range only, a
614 		 * "true" round-robin will be performed as needed.
615 		 */
616 		break;
617 	}
618 	spin_unlock_bh(&sc->lock);
619 
620 exit:
621 	rcu_read_unlock();
622 	return res;
623 }
624 
625 /* tipc_nametbl_lookup_group(): lookup destinaton(s) in a communication group
626  * Returns a list of one (== group anycast) or more (== group multicast)
627  * destination socket/node pairs matching the given address.
628  * The requester may or may not want to exclude himself from the list.
629  */
630 bool tipc_nametbl_lookup_group(struct net *net, struct tipc_uaddr *ua,
631 			       struct list_head *dsts, int *dstcnt,
632 			       u32 exclude, bool mcast)
633 {
634 	u32 self = tipc_own_addr(net);
635 	u32 inst = ua->sa.instance;
636 	struct service_range *sr;
637 	struct tipc_service *sc;
638 	struct publication *p;
639 
640 	*dstcnt = 0;
641 	rcu_read_lock();
642 	sc = tipc_service_find(net, ua);
643 	if (unlikely(!sc))
644 		goto exit;
645 
646 	spin_lock_bh(&sc->lock);
647 
648 	/* Todo: a full search i.e. service_range_foreach_match() instead? */
649 	sr = service_range_match_first(sc->ranges.rb_node, inst, inst);
650 	if (!sr)
651 		goto no_match;
652 
653 	list_for_each_entry(p, &sr->all_publ, all_publ) {
654 		if (p->scope != ua->scope)
655 			continue;
656 		if (p->sk.ref == exclude && p->sk.node == self)
657 			continue;
658 		tipc_dest_push(dsts, p->sk.node, p->sk.ref);
659 		(*dstcnt)++;
660 		if (mcast)
661 			continue;
662 		list_move_tail(&p->all_publ, &sr->all_publ);
663 		break;
664 	}
665 no_match:
666 	spin_unlock_bh(&sc->lock);
667 exit:
668 	rcu_read_unlock();
669 	return !list_empty(dsts);
670 }
671 
672 /* tipc_nametbl_lookup_mcast_sockets(): look up node local destinaton sockets
673  *                                      matching the given address
674  * Used on nodes which have received a multicast/broadcast message
675  * Returns a list of local sockets
676  */
677 void tipc_nametbl_lookup_mcast_sockets(struct net *net, struct tipc_uaddr *ua,
678 				       struct list_head *dports)
679 {
680 	struct service_range *sr;
681 	struct tipc_service *sc;
682 	struct publication *p;
683 	u8 scope = ua->scope;
684 
685 	rcu_read_lock();
686 	sc = tipc_service_find(net, ua);
687 	if (!sc)
688 		goto exit;
689 
690 	spin_lock_bh(&sc->lock);
691 	service_range_foreach_match(sr, sc, ua->sr.lower, ua->sr.upper) {
692 		list_for_each_entry(p, &sr->local_publ, local_publ) {
693 			if (scope == p->scope || scope == TIPC_ANY_SCOPE)
694 				tipc_dest_push(dports, 0, p->sk.ref);
695 		}
696 	}
697 	spin_unlock_bh(&sc->lock);
698 exit:
699 	rcu_read_unlock();
700 }
701 
702 /* tipc_nametbl_lookup_mcast_nodes(): look up all destination nodes matching
703  *                                    the given address. Used in sending node.
704  * Used on nodes which are sending out a multicast/broadcast message
705  * Returns a list of nodes, including own node if applicable
706  */
707 void tipc_nametbl_lookup_mcast_nodes(struct net *net, struct tipc_uaddr *ua,
708 				     struct tipc_nlist *nodes)
709 {
710 	struct service_range *sr;
711 	struct tipc_service *sc;
712 	struct publication *p;
713 
714 	rcu_read_lock();
715 	sc = tipc_service_find(net, ua);
716 	if (!sc)
717 		goto exit;
718 
719 	spin_lock_bh(&sc->lock);
720 	service_range_foreach_match(sr, sc, ua->sr.lower, ua->sr.upper) {
721 		list_for_each_entry(p, &sr->all_publ, all_publ) {
722 			tipc_nlist_add(nodes, p->sk.node);
723 		}
724 	}
725 	spin_unlock_bh(&sc->lock);
726 exit:
727 	rcu_read_unlock();
728 }
729 
730 /* tipc_nametbl_build_group - build list of communication group members
731  */
732 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
733 			      struct tipc_uaddr *ua)
734 {
735 	struct service_range *sr;
736 	struct tipc_service *sc;
737 	struct publication *p;
738 	struct rb_node *n;
739 
740 	rcu_read_lock();
741 	sc = tipc_service_find(net, ua);
742 	if (!sc)
743 		goto exit;
744 
745 	spin_lock_bh(&sc->lock);
746 	for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
747 		sr = container_of(n, struct service_range, tree_node);
748 		list_for_each_entry(p, &sr->all_publ, all_publ) {
749 			if (p->scope != ua->scope)
750 				continue;
751 			tipc_group_add_member(grp, p->sk.node, p->sk.ref,
752 					      p->sr.lower);
753 		}
754 	}
755 	spin_unlock_bh(&sc->lock);
756 exit:
757 	rcu_read_unlock();
758 }
759 
760 /* tipc_nametbl_publish - add service binding to name table
761  */
762 struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua,
763 					 struct tipc_socket_addr *sk, u32 key)
764 {
765 	struct name_table *nt = tipc_name_table(net);
766 	u32 max_user_pub = TIPC_MAX_PUBL - 1;
767 	struct tipc_net *tn = tipc_net(net);
768 	struct publication *p = NULL;
769 	struct sk_buff *skb = NULL;
770 	bool protocol_type = false;
771 	u32 rc_dests;
772 
773 	if (ua->sr.type == TIPC_NODE_STATE || ua->sr.type == TIPC_LINK_STATE ||
774 	    ua->sr.type == TIPC_TOP_SRV)
775 		protocol_type = true;
776 
777 	spin_lock_bh(&tn->nametbl_lock);
778 	if (protocol_type)
779 		goto insert;
780 
781 	/* Reserve one entry for node state service type because it has cluster
782 	 * scope and it is distributed in bulk. So, the maximum number of user's
783 	 * publications is (TIPC_MAX_PUBL - 1).
784 	 */
785 	if (nt->local_publ_count >= max_user_pub) {
786 		pr_warn("Bind failed, max limit %u reached\n", max_user_pub);
787 		goto exit;
788 	}
789 
790 insert:
791 	p = tipc_nametbl_insert_publ(net, ua, sk, key);
792 	if (p) {
793 		/* Not count node state, link state and topology server types
794 		 * so that maximum nt->local_publ_count does not prevent
795 		 * protocol service types from being inserted into the name
796 		 * table.
797 		 */
798 		if (!protocol_type)
799 			nt->local_publ_count++;
800 		skb = tipc_named_publish(net, p);
801 	}
802 	rc_dests = nt->rc_dests;
803 exit:
804 	spin_unlock_bh(&tn->nametbl_lock);
805 
806 	if (skb)
807 		tipc_node_broadcast(net, skb, rc_dests);
808 	return p;
809 
810 }
811 
812 /**
813  * tipc_nametbl_withdraw - withdraw a service binding
814  * @net: network namespace
815  * @ua: service address/range being unbound
816  * @sk: address of the socket being unbound from
817  * @key: target publication key
818  */
819 void tipc_nametbl_withdraw(struct net *net, struct tipc_uaddr *ua,
820 			   struct tipc_socket_addr *sk, u32 key)
821 {
822 	struct name_table *nt = tipc_name_table(net);
823 	struct tipc_net *tn = tipc_net(net);
824 	struct sk_buff *skb = NULL;
825 	struct publication *p;
826 	u32 rc_dests;
827 
828 	spin_lock_bh(&tn->nametbl_lock);
829 
830 	p = tipc_nametbl_remove_publ(net, ua, sk, key);
831 	if (p) {
832 		if (p->sr.type != TIPC_NODE_STATE &&
833 		    p->sr.type != TIPC_LINK_STATE &&
834 		    p->sr.type != TIPC_TOP_SRV)
835 			nt->local_publ_count--;
836 		skb = tipc_named_withdraw(net, p);
837 		list_del_init(&p->binding_sock);
838 		kfree_rcu(p, rcu);
839 	}
840 	rc_dests = nt->rc_dests;
841 	spin_unlock_bh(&tn->nametbl_lock);
842 
843 	if (skb)
844 		tipc_node_broadcast(net, skb, rc_dests);
845 }
846 
847 /**
848  * tipc_nametbl_subscribe - add a subscription object to the name table
849  * @sub: subscription to add
850  */
851 bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
852 {
853 	struct tipc_net *tn = tipc_net(sub->net);
854 	u32 type = sub->s.seq.type;
855 	struct tipc_service *sc;
856 	struct tipc_uaddr ua;
857 	bool res = true;
858 
859 	tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE, type,
860 		   sub->s.seq.lower, sub->s.seq.upper);
861 	spin_lock_bh(&tn->nametbl_lock);
862 	sc = tipc_service_find(sub->net, &ua);
863 	if (!sc)
864 		sc = tipc_service_create(sub->net, &ua);
865 	if (sc) {
866 		spin_lock_bh(&sc->lock);
867 		tipc_service_subscribe(sc, sub);
868 		spin_unlock_bh(&sc->lock);
869 	} else {
870 		pr_warn("Failed to subscribe for {%u,%u,%u}\n",
871 			type, sub->s.seq.lower, sub->s.seq.upper);
872 		res = false;
873 	}
874 	spin_unlock_bh(&tn->nametbl_lock);
875 	return res;
876 }
877 
878 /**
879  * tipc_nametbl_unsubscribe - remove a subscription object from name table
880  * @sub: subscription to remove
881  */
882 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
883 {
884 	struct tipc_net *tn = tipc_net(sub->net);
885 	struct tipc_service *sc;
886 	struct tipc_uaddr ua;
887 
888 	tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
889 		   sub->s.seq.type, sub->s.seq.lower, sub->s.seq.upper);
890 	spin_lock_bh(&tn->nametbl_lock);
891 	sc = tipc_service_find(sub->net, &ua);
892 	if (!sc)
893 		goto exit;
894 
895 	spin_lock_bh(&sc->lock);
896 	list_del_init(&sub->service_list);
897 	tipc_sub_put(sub);
898 
899 	/* Delete service item if no more publications and subscriptions */
900 	if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
901 		hlist_del_init_rcu(&sc->service_list);
902 		kfree_rcu(sc, rcu);
903 	}
904 	spin_unlock_bh(&sc->lock);
905 exit:
906 	spin_unlock_bh(&tn->nametbl_lock);
907 }
908 
909 int tipc_nametbl_init(struct net *net)
910 {
911 	struct tipc_net *tn = tipc_net(net);
912 	struct name_table *nt;
913 	int i;
914 
915 	nt = kzalloc_obj(*nt);
916 	if (!nt)
917 		return -ENOMEM;
918 
919 	for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
920 		INIT_HLIST_HEAD(&nt->services[i]);
921 
922 	INIT_LIST_HEAD(&nt->node_scope);
923 	INIT_LIST_HEAD(&nt->cluster_scope);
924 	rwlock_init(&nt->cluster_scope_lock);
925 	tn->nametbl = nt;
926 	spin_lock_init(&tn->nametbl_lock);
927 	return 0;
928 }
929 
930 /**
931  * tipc_service_delete - purge all publications for a service and delete it
932  * @net: the associated network namespace
933  * @sc: tipc_service to delete
934  */
935 static void tipc_service_delete(struct net *net, struct tipc_service *sc)
936 {
937 	struct service_range *sr, *tmpr;
938 	struct publication *p, *tmp;
939 
940 	spin_lock_bh(&sc->lock);
941 	rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
942 		list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
943 			tipc_service_remove_publ(sr, &p->sk, p->key);
944 			kfree_rcu(p, rcu);
945 		}
946 		rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
947 		kfree(sr);
948 	}
949 	hlist_del_init_rcu(&sc->service_list);
950 	spin_unlock_bh(&sc->lock);
951 	kfree_rcu(sc, rcu);
952 }
953 
954 void tipc_nametbl_stop(struct net *net)
955 {
956 	struct name_table *nt = tipc_name_table(net);
957 	struct tipc_net *tn = tipc_net(net);
958 	struct hlist_head *service_head;
959 	struct tipc_service *service;
960 	u32 i;
961 
962 	/* Verify name table is empty and purge any lingering
963 	 * publications, then release the name table
964 	 */
965 	spin_lock_bh(&tn->nametbl_lock);
966 	for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
967 		if (hlist_empty(&nt->services[i]))
968 			continue;
969 		service_head = &nt->services[i];
970 		hlist_for_each_entry_rcu(service, service_head, service_list) {
971 			tipc_service_delete(net, service);
972 		}
973 	}
974 	spin_unlock_bh(&tn->nametbl_lock);
975 
976 	/* TODO: clear tn->nametbl, implement proper RCU rules ? */
977 	kfree_rcu(nt, rcu);
978 }
979 
980 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
981 					struct tipc_service *service,
982 					struct service_range *sr,
983 					u32 *last_key)
984 {
985 	struct publication *p;
986 	struct nlattr *attrs;
987 	struct nlattr *b;
988 	void *hdr;
989 
990 	if (*last_key) {
991 		list_for_each_entry(p, &sr->all_publ, all_publ)
992 			if (p->key == *last_key)
993 				break;
994 		if (list_entry_is_head(p, &sr->all_publ, all_publ))
995 			return -EPIPE;
996 	} else {
997 		p = list_first_entry(&sr->all_publ,
998 				     struct publication,
999 				     all_publ);
1000 	}
1001 
1002 	list_for_each_entry_from(p, &sr->all_publ, all_publ) {
1003 		*last_key = p->key;
1004 
1005 		hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
1006 				  &tipc_genl_family, NLM_F_MULTI,
1007 				  TIPC_NL_NAME_TABLE_GET);
1008 		if (!hdr)
1009 			return -EMSGSIZE;
1010 
1011 		attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE);
1012 		if (!attrs)
1013 			goto msg_full;
1014 
1015 		b = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
1016 		if (!b)
1017 			goto attr_msg_full;
1018 
1019 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type))
1020 			goto publ_msg_full;
1021 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower))
1022 			goto publ_msg_full;
1023 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper))
1024 			goto publ_msg_full;
1025 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
1026 			goto publ_msg_full;
1027 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->sk.node))
1028 			goto publ_msg_full;
1029 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->sk.ref))
1030 			goto publ_msg_full;
1031 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
1032 			goto publ_msg_full;
1033 
1034 		nla_nest_end(msg->skb, b);
1035 		nla_nest_end(msg->skb, attrs);
1036 		genlmsg_end(msg->skb, hdr);
1037 	}
1038 	*last_key = 0;
1039 
1040 	return 0;
1041 
1042 publ_msg_full:
1043 	nla_nest_cancel(msg->skb, b);
1044 attr_msg_full:
1045 	nla_nest_cancel(msg->skb, attrs);
1046 msg_full:
1047 	genlmsg_cancel(msg->skb, hdr);
1048 
1049 	return -EMSGSIZE;
1050 }
1051 
1052 static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg,
1053 					struct tipc_service *sc,
1054 					u32 *last_lower, u32 *last_key)
1055 {
1056 	struct service_range *sr;
1057 	struct rb_node *n;
1058 	int err;
1059 
1060 	for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
1061 		sr = container_of(n, struct service_range, tree_node);
1062 		if (sr->lower < *last_lower)
1063 			continue;
1064 		err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key);
1065 		if (err) {
1066 			*last_lower = sr->lower;
1067 			return err;
1068 		}
1069 	}
1070 	*last_lower = 0;
1071 	return 0;
1072 }
1073 
1074 static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
1075 				u32 *last_type, u32 *last_lower, u32 *last_key)
1076 {
1077 	struct tipc_net *tn = tipc_net(net);
1078 	struct tipc_service *service = NULL;
1079 	struct hlist_head *head;
1080 	struct tipc_uaddr ua;
1081 	int err;
1082 	int i;
1083 
1084 	if (*last_type)
1085 		i = hash(*last_type);
1086 	else
1087 		i = 0;
1088 
1089 	for (; i < TIPC_NAMETBL_SIZE; i++) {
1090 		head = &tn->nametbl->services[i];
1091 
1092 		if (*last_type ||
1093 		    (!i && *last_key && (*last_lower == *last_key))) {
1094 			tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
1095 				   *last_type, *last_lower, *last_lower);
1096 			service = tipc_service_find(net, &ua);
1097 			if (!service)
1098 				return -EPIPE;
1099 		} else {
1100 			hlist_for_each_entry_rcu(service, head, service_list)
1101 				break;
1102 			if (!service)
1103 				continue;
1104 		}
1105 
1106 		hlist_for_each_entry_from_rcu(service, service_list) {
1107 			spin_lock_bh(&service->lock);
1108 			err = __tipc_nl_service_range_list(msg, service,
1109 							   last_lower,
1110 							   last_key);
1111 
1112 			if (err) {
1113 				*last_type = service->type;
1114 				spin_unlock_bh(&service->lock);
1115 				return err;
1116 			}
1117 			spin_unlock_bh(&service->lock);
1118 		}
1119 		*last_type = 0;
1120 	}
1121 	return 0;
1122 }
1123 
1124 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1125 {
1126 	struct net *net = sock_net(skb->sk);
1127 	u32 last_type = cb->args[0];
1128 	u32 last_lower = cb->args[1];
1129 	u32 last_key = cb->args[2];
1130 	int done = cb->args[3];
1131 	struct tipc_nl_msg msg;
1132 	int err;
1133 
1134 	if (done)
1135 		return 0;
1136 
1137 	msg.skb = skb;
1138 	msg.portid = NETLINK_CB(cb->skb).portid;
1139 	msg.seq = cb->nlh->nlmsg_seq;
1140 
1141 	rcu_read_lock();
1142 	err = tipc_nl_service_list(net, &msg, &last_type,
1143 				   &last_lower, &last_key);
1144 	if (!err) {
1145 		done = 1;
1146 	} else if (err != -EMSGSIZE) {
1147 		/* We never set seq or call nl_dump_check_consistent() this
1148 		 * means that setting prev_seq here will cause the consistence
1149 		 * check to fail in the netlink callback handler. Resulting in
1150 		 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1151 		 * we got an error.
1152 		 */
1153 		cb->prev_seq = 1;
1154 	}
1155 	rcu_read_unlock();
1156 
1157 	cb->args[0] = last_type;
1158 	cb->args[1] = last_lower;
1159 	cb->args[2] = last_key;
1160 	cb->args[3] = done;
1161 
1162 	return skb->len;
1163 }
1164 
1165 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1166 {
1167 	struct tipc_dest *dst;
1168 
1169 	list_for_each_entry(dst, l, list) {
1170 		if (dst->node == node && dst->port == port)
1171 			return dst;
1172 	}
1173 	return NULL;
1174 }
1175 
1176 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1177 {
1178 	struct tipc_dest *dst;
1179 
1180 	if (tipc_dest_find(l, node, port))
1181 		return false;
1182 
1183 	dst = kmalloc_obj(*dst, GFP_ATOMIC);
1184 	if (unlikely(!dst))
1185 		return false;
1186 	dst->node = node;
1187 	dst->port = port;
1188 	list_add(&dst->list, l);
1189 	return true;
1190 }
1191 
1192 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1193 {
1194 	struct tipc_dest *dst;
1195 
1196 	if (list_empty(l))
1197 		return false;
1198 	dst = list_first_entry(l, typeof(*dst), list);
1199 	if (port)
1200 		*port = dst->port;
1201 	if (node)
1202 		*node = dst->node;
1203 	list_del(&dst->list);
1204 	kfree(dst);
1205 	return true;
1206 }
1207 
1208 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1209 {
1210 	struct tipc_dest *dst;
1211 
1212 	dst = tipc_dest_find(l, node, port);
1213 	if (!dst)
1214 		return false;
1215 	list_del(&dst->list);
1216 	kfree(dst);
1217 	return true;
1218 }
1219 
1220 void tipc_dest_list_purge(struct list_head *l)
1221 {
1222 	struct tipc_dest *dst, *tmp;
1223 
1224 	list_for_each_entry_safe(dst, tmp, l, list) {
1225 		list_del(&dst->list);
1226 		kfree(dst);
1227 	}
1228 }
1229