2 * net/tipc/name_table.c: TIPC name table code
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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
39 #include <linux/list_sort.h>
40 #include <linux/rbtree_augmented.h>
43 #include "name_table.h"
44 #include "name_distr.h"
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
62 struct service_range {
65 struct rb_node tree_node;
67 struct list_head local_publ;
68 struct list_head all_publ;
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
84 struct rb_root ranges;
85 struct hlist_node service_list;
86 struct list_head subscriptions;
87 spinlock_t lock; /* Covers service range list */
91 #define service_range_upper(sr) ((sr)->upper)
92 RB_DECLARE_CALLBACKS_MAX(static, sr_callbacks,
93 struct service_range, tree_node, u32, max,
96 #define service_range_entry(rbtree_node) \
97 (container_of(rbtree_node, struct service_range, tree_node))
99 #define service_range_overlap(sr, start, end) \
100 ((sr)->lower <= (end) && (sr)->upper >= (start))
103 * service_range_foreach_match - iterate over tipc service rbtree for each
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
110 #define service_range_foreach_match(sr, sc, start, end) \
111 for (sr = service_range_match_first((sc)->ranges.rb_node, \
115 sr = service_range_match_next(&(sr)->tree_node, \
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
125 * Return: the leftmost service range node in the rbtree that overlaps the
126 * specific range if any. Otherwise, returns NULL.
128 static struct service_range *service_range_match_first(struct rb_node *n,
131 struct service_range *sr;
132 struct rb_node *l, *r;
134 /* Non overlaps in tree at all? */
135 if (!n || service_range_entry(n)->max < start)
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.
149 /* No one in the left subtree can match, return if this node is
150 * an overlap i.e. leftmost.
152 sr = service_range_entry(n);
153 if (service_range_overlap(sr, start, end))
156 /* Ok, try to lookup on the right side */
158 if (sr->lower <= end &&
159 r && service_range_entry(r)->max >= start) {
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
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.
178 static struct service_range *service_range_match_next(struct rb_node *n,
181 struct service_range *sr;
182 struct rb_node *p, *r;
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.
192 return service_range_match_first(r, start, end);
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.
197 while ((p = rb_parent(n)) && n == p->rb_right)
202 /* Return if this ancestor is an overlap */
203 sr = service_range_entry(p);
204 if (service_range_overlap(sr, start, end))
207 /* Ok, try to lookup more from this ancestor */
208 if (sr->lower <= end) {
218 static int hash(int x)
220 return x & (TIPC_NAMETBL_SIZE - 1);
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
233 static struct publication *tipc_publ_create(u32 type, u32 lower, u32 upper,
234 u32 scope, u32 node, u32 port,
237 struct publication *p = kzalloc(sizeof(*p), GFP_ATOMIC);
249 INIT_LIST_HEAD(&p->binding_sock);
250 INIT_LIST_HEAD(&p->binding_node);
251 INIT_LIST_HEAD(&p->local_publ);
252 INIT_LIST_HEAD(&p->all_publ);
253 INIT_LIST_HEAD(&p->list);
258 * tipc_service_create - create a service structure for the specified 'type'
259 * @type: service type
260 * @hd: name_table services list
262 * Allocates a single range structure and sets it to all 0's.
264 static struct tipc_service *tipc_service_create(u32 type, struct hlist_head *hd)
266 struct tipc_service *service = kzalloc(sizeof(*service), GFP_ATOMIC);
269 pr_warn("Service creation failed, no memory\n");
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);
282 /* tipc_service_find_range - find service range matching publication parameters
284 static struct service_range *tipc_service_find_range(struct tipc_service *sc,
285 u32 lower, u32 upper)
287 struct service_range *sr;
289 service_range_foreach_match(sr, sc, lower, upper) {
290 /* Look for exact match */
291 if (sr->lower == lower && sr->upper == upper)
298 static struct service_range *tipc_service_create_range(struct tipc_service *sc,
299 u32 lower, u32 upper)
301 struct rb_node **n, *parent = NULL;
302 struct service_range *sr;
304 n = &sc->ranges.rb_node;
307 sr = service_range_entry(parent);
308 if (lower == sr->lower && upper == sr->upper)
312 if (lower <= sr->lower)
313 n = &parent->rb_left;
315 n = &parent->rb_right;
317 sr = kzalloc(sizeof(*sr), GFP_ATOMIC);
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);
330 static bool tipc_service_insert_publ(struct net *net,
331 struct tipc_service *sc,
332 struct publication *p)
334 struct tipc_subscription *sub, *tmp;
335 struct service_range *sr;
336 struct publication *_p;
337 u32 node = p->sk.node;
341 spin_lock_bh(&sc->lock);
342 sr = tipc_service_create_range(sc, p->sr.lower, p->sr.upper);
346 first = list_empty(&sr->all_publ);
348 /* Return if the publication already exists */
349 list_for_each_entry(_p, &sr->all_publ, all_publ) {
350 if (_p->key == p->key && (!_p->sk.node || _p->sk.node == node))
354 if (in_own_node(net, p->sk.node))
355 list_add(&p->local_publ, &sr->local_publ);
356 list_add(&p->all_publ, &sr->all_publ);
357 p->id = sc->publ_cnt++;
359 /* Any subscriptions waiting for notification? */
360 list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
361 tipc_sub_report_overlap(sub, p->sr.lower, p->sr.upper,
362 TIPC_PUBLISHED, p->sk.ref, p->sk.node,
368 pr_warn("Failed to bind to %u,%u,%u\n",
369 p->sr.type, p->sr.lower, p->sr.upper);
370 spin_unlock_bh(&sc->lock);
375 * tipc_service_remove_publ - remove a publication from a service
376 * @sr: service_range to remove publication from
378 * @key: target publication key
380 static struct publication *tipc_service_remove_publ(struct service_range *sr,
383 struct publication *p;
385 list_for_each_entry(p, &sr->all_publ, all_publ) {
386 if (p->key != key || (node && node != p->sk.node))
388 list_del(&p->all_publ);
389 list_del(&p->local_publ);
396 * Code reused: time_after32() for the same purpose
398 #define publication_after(pa, pb) time_after32((pa)->id, (pb)->id)
399 static int tipc_publ_sort(void *priv, struct list_head *a,
402 struct publication *pa, *pb;
404 pa = container_of(a, struct publication, list);
405 pb = container_of(b, struct publication, list);
406 return publication_after(pa, pb);
410 * tipc_service_subscribe - attach a subscription, and optionally
411 * issue the prescribed number of events if there is any service
412 * range overlapping with the requested range
413 * @service: the tipc_service to attach the @sub to
414 * @sub: the subscription to attach
416 static void tipc_service_subscribe(struct tipc_service *service,
417 struct tipc_subscription *sub)
419 struct tipc_subscr *sb = &sub->evt.s;
420 struct publication *p, *first, *tmp;
421 struct list_head publ_list;
422 struct service_range *sr;
423 struct tipc_service_range r;
426 r.type = tipc_sub_read(sb, seq.type);
427 r.lower = tipc_sub_read(sb, seq.lower);
428 r.upper = tipc_sub_read(sb, seq.upper);
429 filter = tipc_sub_read(sb, filter);
432 list_add(&sub->service_list, &service->subscriptions);
434 if (filter & TIPC_SUB_NO_STATUS)
437 INIT_LIST_HEAD(&publ_list);
438 service_range_foreach_match(sr, service, r.lower, r.upper) {
440 list_for_each_entry(p, &sr->all_publ, all_publ) {
441 if (filter & TIPC_SUB_PORTS)
442 list_add_tail(&p->list, &publ_list);
443 else if (!first || publication_after(first, p))
444 /* Pick this range's *first* publication */
448 list_add_tail(&first->list, &publ_list);
451 /* Sort the publications before reporting */
452 list_sort(NULL, &publ_list, tipc_publ_sort);
453 list_for_each_entry_safe(p, tmp, &publ_list, list) {
454 tipc_sub_report_overlap(sub, p->sr.lower, p->sr.upper,
455 TIPC_PUBLISHED, p->sk.ref, p->sk.node,
457 list_del_init(&p->list);
461 static struct tipc_service *tipc_service_find(struct net *net, u32 type)
463 struct name_table *nt = tipc_name_table(net);
464 struct hlist_head *service_head;
465 struct tipc_service *service;
467 service_head = &nt->services[hash(type)];
468 hlist_for_each_entry_rcu(service, service_head, service_list) {
469 if (service->type == type)
475 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
476 u32 lower, u32 upper,
480 struct name_table *nt = tipc_name_table(net);
481 struct tipc_service *sc;
482 struct publication *p;
484 p = tipc_publ_create(type, lower, upper, scope, node, port, key);
488 if (scope > TIPC_NODE_SCOPE || lower > upper) {
489 pr_debug("Failed to bind illegal {%u,%u,%u} with scope %u\n",
490 type, lower, upper, scope);
493 sc = tipc_service_find(net, type);
495 sc = tipc_service_create(type, &nt->services[hash(type)]);
496 if (sc && tipc_service_insert_publ(net, sc, p))
502 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
503 u32 lower, u32 upper,
506 struct tipc_service *sc = tipc_service_find(net, type);
507 struct tipc_subscription *sub, *tmp;
508 struct service_range *sr = NULL;
509 struct publication *p = NULL;
515 spin_lock_bh(&sc->lock);
516 sr = tipc_service_find_range(sc, lower, upper);
519 p = tipc_service_remove_publ(sr, node, key);
523 /* Notify any waiting subscriptions */
524 last = list_empty(&sr->all_publ);
525 list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
526 tipc_sub_report_overlap(sub, lower, upper, TIPC_WITHDRAWN,
527 p->sk.ref, node, p->scope, last);
530 /* Remove service range item if this was its last publication */
531 if (list_empty(&sr->all_publ)) {
532 rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
536 /* Delete service item if this no more publications and subscriptions */
537 if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
538 hlist_del_init_rcu(&sc->service_list);
542 spin_unlock_bh(&sc->lock);
547 * tipc_nametbl_translate - perform service instance to socket translation
548 * @net: network namespace
549 * @type: message type
550 * @instance: message instance
551 * @dnode: the search domain used during translation
554 * - if translation is deferred to another node, leave 'dnode' unchanged and
556 * - if translation is attempted and succeeds, set 'dnode' to the publishing
557 * node and return the published (non-zero) port number
558 * - if translation is attempted and fails, set 'dnode' to 0 and return 0
560 * Note that for legacy users (node configured with Z.C.N address format) the
561 * 'closest-first' lookup algorithm must be maintained, i.e., if dnode is 0
562 * we must look in the local binding list first
564 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *dnode)
566 struct tipc_net *tn = tipc_net(net);
567 bool legacy = tn->legacy_addr_format;
568 u32 self = tipc_own_addr(net);
569 struct service_range *sr;
570 struct tipc_service *sc;
571 struct list_head *list;
572 struct publication *p;
576 if (!tipc_in_scope(legacy, *dnode, self))
580 sc = tipc_service_find(net, type);
584 spin_lock_bh(&sc->lock);
585 service_range_foreach_match(sr, sc, instance, instance) {
586 /* Select lookup algo: local, closest-first or round-robin */
587 if (*dnode == self) {
588 list = &sr->local_publ;
589 if (list_empty(list))
591 p = list_first_entry(list, struct publication,
593 list_move_tail(&p->local_publ, &sr->local_publ);
594 } else if (legacy && !*dnode && !list_empty(&sr->local_publ)) {
595 list = &sr->local_publ;
596 p = list_first_entry(list, struct publication,
598 list_move_tail(&p->local_publ, &sr->local_publ);
600 list = &sr->all_publ;
601 p = list_first_entry(list, struct publication,
603 list_move_tail(&p->all_publ, &sr->all_publ);
607 /* Todo: as for legacy, pick the first matching range only, a
608 * "true" round-robin will be performed as needed.
612 spin_unlock_bh(&sc->lock);
620 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
621 struct list_head *dsts, int *dstcnt, u32 exclude,
624 u32 self = tipc_own_addr(net);
625 struct service_range *sr;
626 struct tipc_service *sc;
627 struct publication *p;
631 sc = tipc_service_find(net, type);
635 spin_lock_bh(&sc->lock);
637 /* Todo: a full search i.e. service_range_foreach_match() instead? */
638 sr = service_range_match_first(sc->ranges.rb_node, instance, instance);
642 list_for_each_entry(p, &sr->all_publ, all_publ) {
643 if (p->scope != scope)
645 if (p->sk.ref == exclude && p->sk.node == self)
647 tipc_dest_push(dsts, p->sk.node, p->sk.ref);
651 list_move_tail(&p->all_publ, &sr->all_publ);
655 spin_unlock_bh(&sc->lock);
658 return !list_empty(dsts);
661 void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
662 u32 scope, bool exact, struct list_head *dports)
664 struct service_range *sr;
665 struct tipc_service *sc;
666 struct publication *p;
669 sc = tipc_service_find(net, type);
673 spin_lock_bh(&sc->lock);
674 service_range_foreach_match(sr, sc, lower, upper) {
675 list_for_each_entry(p, &sr->local_publ, local_publ) {
676 if (p->scope == scope || (!exact && p->scope < scope))
677 tipc_dest_push(dports, 0, p->sk.ref);
680 spin_unlock_bh(&sc->lock);
685 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
686 * - Creates list of nodes that overlap the given multicast address
687 * - Determines if any node local destinations overlap
689 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
690 u32 upper, struct tipc_nlist *nodes)
692 struct service_range *sr;
693 struct tipc_service *sc;
694 struct publication *p;
697 sc = tipc_service_find(net, type);
701 spin_lock_bh(&sc->lock);
702 service_range_foreach_match(sr, sc, lower, upper) {
703 list_for_each_entry(p, &sr->all_publ, all_publ) {
704 tipc_nlist_add(nodes, p->sk.node);
707 spin_unlock_bh(&sc->lock);
712 /* tipc_nametbl_build_group - build list of communication group members
714 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
717 struct service_range *sr;
718 struct tipc_service *sc;
719 struct publication *p;
723 sc = tipc_service_find(net, type);
727 spin_lock_bh(&sc->lock);
728 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
729 sr = container_of(n, struct service_range, tree_node);
730 list_for_each_entry(p, &sr->all_publ, all_publ) {
731 if (p->scope != scope)
733 tipc_group_add_member(grp, p->sk.node, p->sk.ref, p->sr.lower);
736 spin_unlock_bh(&sc->lock);
741 /* tipc_nametbl_publish - add service binding to name table
743 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
744 u32 upper, u32 scope, u32 port,
747 struct name_table *nt = tipc_name_table(net);
748 struct tipc_net *tn = tipc_net(net);
749 struct publication *p = NULL;
750 struct sk_buff *skb = NULL;
753 spin_lock_bh(&tn->nametbl_lock);
755 if (nt->local_publ_count >= TIPC_MAX_PUBL) {
756 pr_warn("Bind failed, max limit %u reached\n", TIPC_MAX_PUBL);
760 p = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
761 tipc_own_addr(net), port, key);
763 nt->local_publ_count++;
764 skb = tipc_named_publish(net, p);
766 rc_dests = nt->rc_dests;
768 spin_unlock_bh(&tn->nametbl_lock);
771 tipc_node_broadcast(net, skb, rc_dests);
777 * tipc_nametbl_withdraw - withdraw a service binding
778 * @net: network namespace
779 * @type: service type
780 * @lower: service range lower bound
781 * @upper: service range upper bound
782 * @key: target publication key
784 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower,
787 struct name_table *nt = tipc_name_table(net);
788 struct tipc_net *tn = tipc_net(net);
789 u32 self = tipc_own_addr(net);
790 struct sk_buff *skb = NULL;
791 struct publication *p;
794 spin_lock_bh(&tn->nametbl_lock);
796 p = tipc_nametbl_remove_publ(net, type, lower, upper, self, key);
798 nt->local_publ_count--;
799 skb = tipc_named_withdraw(net, p);
800 list_del_init(&p->binding_sock);
803 pr_err("Failed to remove local publication {%u,%u,%u}/%u\n",
804 type, lower, upper, key);
806 rc_dests = nt->rc_dests;
807 spin_unlock_bh(&tn->nametbl_lock);
810 tipc_node_broadcast(net, skb, rc_dests);
817 * tipc_nametbl_subscribe - add a subscription object to the name table
818 * @sub: subscription to add
820 bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
822 struct name_table *nt = tipc_name_table(sub->net);
823 struct tipc_net *tn = tipc_net(sub->net);
824 struct tipc_subscr *s = &sub->evt.s;
825 u32 type = tipc_sub_read(s, seq.type);
826 struct tipc_service *sc;
829 spin_lock_bh(&tn->nametbl_lock);
830 sc = tipc_service_find(sub->net, type);
832 sc = tipc_service_create(type, &nt->services[hash(type)]);
834 spin_lock_bh(&sc->lock);
835 tipc_service_subscribe(sc, sub);
836 spin_unlock_bh(&sc->lock);
838 pr_warn("Failed to subscribe for {%u,%u,%u}\n", type,
839 tipc_sub_read(s, seq.lower),
840 tipc_sub_read(s, seq.upper));
843 spin_unlock_bh(&tn->nametbl_lock);
848 * tipc_nametbl_unsubscribe - remove a subscription object from name table
849 * @sub: subscription to remove
851 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
853 struct tipc_net *tn = tipc_net(sub->net);
854 struct tipc_subscr *s = &sub->evt.s;
855 u32 type = tipc_sub_read(s, seq.type);
856 struct tipc_service *sc;
858 spin_lock_bh(&tn->nametbl_lock);
859 sc = tipc_service_find(sub->net, type);
863 spin_lock_bh(&sc->lock);
864 list_del_init(&sub->service_list);
867 /* Delete service item if no more publications and subscriptions */
868 if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
869 hlist_del_init_rcu(&sc->service_list);
872 spin_unlock_bh(&sc->lock);
874 spin_unlock_bh(&tn->nametbl_lock);
877 int tipc_nametbl_init(struct net *net)
879 struct tipc_net *tn = tipc_net(net);
880 struct name_table *nt;
883 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
887 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
888 INIT_HLIST_HEAD(&nt->services[i]);
890 INIT_LIST_HEAD(&nt->node_scope);
891 INIT_LIST_HEAD(&nt->cluster_scope);
892 rwlock_init(&nt->cluster_scope_lock);
894 spin_lock_init(&tn->nametbl_lock);
899 * tipc_service_delete - purge all publications for a service and delete it
900 * @net: the associated network namespace
901 * @sc: tipc_service to delete
903 static void tipc_service_delete(struct net *net, struct tipc_service *sc)
905 struct service_range *sr, *tmpr;
906 struct publication *p, *tmp;
908 spin_lock_bh(&sc->lock);
909 rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
910 list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
911 tipc_service_remove_publ(sr, p->sk.node, p->key);
914 rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
917 hlist_del_init_rcu(&sc->service_list);
918 spin_unlock_bh(&sc->lock);
922 void tipc_nametbl_stop(struct net *net)
924 struct name_table *nt = tipc_name_table(net);
925 struct tipc_net *tn = tipc_net(net);
926 struct hlist_head *service_head;
927 struct tipc_service *service;
930 /* Verify name table is empty and purge any lingering
931 * publications, then release the name table
933 spin_lock_bh(&tn->nametbl_lock);
934 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
935 if (hlist_empty(&nt->services[i]))
937 service_head = &nt->services[i];
938 hlist_for_each_entry_rcu(service, service_head, service_list) {
939 tipc_service_delete(net, service);
942 spin_unlock_bh(&tn->nametbl_lock);
948 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
949 struct tipc_service *service,
950 struct service_range *sr,
953 struct publication *p;
954 struct nlattr *attrs;
959 list_for_each_entry(p, &sr->all_publ, all_publ)
960 if (p->key == *last_key)
962 if (p->key != *last_key)
965 p = list_first_entry(&sr->all_publ,
970 list_for_each_entry_from(p, &sr->all_publ, all_publ) {
973 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
974 &tipc_genl_family, NLM_F_MULTI,
975 TIPC_NL_NAME_TABLE_GET);
979 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE);
983 b = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
987 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type))
989 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower))
991 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper))
993 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
995 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->sk.node))
997 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->sk.ref))
999 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
1002 nla_nest_end(msg->skb, b);
1003 nla_nest_end(msg->skb, attrs);
1004 genlmsg_end(msg->skb, hdr);
1011 nla_nest_cancel(msg->skb, b);
1013 nla_nest_cancel(msg->skb, attrs);
1015 genlmsg_cancel(msg->skb, hdr);
1020 static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg,
1021 struct tipc_service *sc,
1022 u32 *last_lower, u32 *last_key)
1024 struct service_range *sr;
1028 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
1029 sr = container_of(n, struct service_range, tree_node);
1030 if (sr->lower < *last_lower)
1032 err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key);
1034 *last_lower = sr->lower;
1042 static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
1043 u32 *last_type, u32 *last_lower, u32 *last_key)
1045 struct tipc_net *tn = tipc_net(net);
1046 struct tipc_service *service = NULL;
1047 struct hlist_head *head;
1052 i = hash(*last_type);
1056 for (; i < TIPC_NAMETBL_SIZE; i++) {
1057 head = &tn->nametbl->services[i];
1060 (!i && *last_key && (*last_lower == *last_key))) {
1061 service = tipc_service_find(net, *last_type);
1065 hlist_for_each_entry_rcu(service, head, service_list)
1071 hlist_for_each_entry_from_rcu(service, service_list) {
1072 spin_lock_bh(&service->lock);
1073 err = __tipc_nl_service_range_list(msg, service,
1078 *last_type = service->type;
1079 spin_unlock_bh(&service->lock);
1082 spin_unlock_bh(&service->lock);
1089 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1091 struct net *net = sock_net(skb->sk);
1092 u32 last_type = cb->args[0];
1093 u32 last_lower = cb->args[1];
1094 u32 last_key = cb->args[2];
1095 int done = cb->args[3];
1096 struct tipc_nl_msg msg;
1103 msg.portid = NETLINK_CB(cb->skb).portid;
1104 msg.seq = cb->nlh->nlmsg_seq;
1107 err = tipc_nl_service_list(net, &msg, &last_type,
1108 &last_lower, &last_key);
1111 } else if (err != -EMSGSIZE) {
1112 /* We never set seq or call nl_dump_check_consistent() this
1113 * means that setting prev_seq here will cause the consistence
1114 * check to fail in the netlink callback handler. Resulting in
1115 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1122 cb->args[0] = last_type;
1123 cb->args[1] = last_lower;
1124 cb->args[2] = last_key;
1130 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1132 struct tipc_dest *dst;
1134 list_for_each_entry(dst, l, list) {
1135 if (dst->node == node && dst->port == port)
1141 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1143 struct tipc_dest *dst;
1145 if (tipc_dest_find(l, node, port))
1148 dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1153 list_add(&dst->list, l);
1157 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1159 struct tipc_dest *dst;
1163 dst = list_first_entry(l, typeof(*dst), list);
1168 list_del(&dst->list);
1173 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1175 struct tipc_dest *dst;
1177 dst = tipc_dest_find(l, node, port);
1180 list_del(&dst->list);
1185 void tipc_dest_list_purge(struct list_head *l)
1187 struct tipc_dest *dst, *tmp;
1189 list_for_each_entry_safe(dst, tmp, l, list) {
1190 list_del(&dst->list);
1195 int tipc_dest_list_len(struct list_head *l)
1197 struct tipc_dest *dst;
1200 list_for_each_entry(dst, l, list) {