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, 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
*publ
= kzalloc(sizeof(*publ
), GFP_ATOMIC
);
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
);
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 struct publication
*tipc_service_insert_publ(struct net
*net
,
331 struct tipc_service
*sc
,
333 u32 upper
, u32 scope
,
337 struct tipc_subscription
*sub
, *tmp
;
338 struct service_range
*sr
;
339 struct publication
*p
;
342 sr
= tipc_service_create_range(sc
, lower
, 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
== key
&& (!p
->node
|| p
->node
== node
))
354 /* Create and insert publication */
355 p
= tipc_publ_create(type
, lower
, upper
, scope
, node
, port
, key
);
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
);
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
);
371 pr_warn("Failed to bind to %u,%u,%u, no memory\n", type
, lower
, upper
);
376 * tipc_service_remove_publ - remove a publication from a service
377 * @sr: service_range to remove publication from
379 * @key: target publication key
381 static struct publication
*tipc_service_remove_publ(struct service_range
*sr
,
384 struct publication
*p
;
386 list_for_each_entry(p
, &sr
->all_publ
, all_publ
) {
387 if (p
->key
!= key
|| (node
&& node
!= p
->node
))
389 list_del(&p
->all_publ
);
390 list_del(&p
->local_publ
);
397 * Code reused: time_after32() for the same purpose
399 #define publication_after(pa, pb) time_after32((pa)->id, (pb)->id)
400 static int tipc_publ_sort(void *priv
, struct list_head
*a
,
403 struct publication
*pa
, *pb
;
405 pa
= container_of(a
, struct publication
, list
);
406 pb
= container_of(b
, struct publication
, list
);
407 return publication_after(pa
, pb
);
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
417 static void tipc_service_subscribe(struct tipc_service
*service
,
418 struct tipc_subscription
*sub
)
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
;
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
);
433 list_add(&sub
->service_list
, &service
->subscriptions
);
435 if (filter
& TIPC_SUB_NO_STATUS
)
438 INIT_LIST_HEAD(&publ_list
);
439 service_range_foreach_match(sr
, service
, r
.lower
, r
.upper
) {
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 */
449 list_add_tail(&first
->list
, &publ_list
);
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
,
458 list_del_init(&p
->list
);
462 static struct tipc_service
*tipc_service_find(struct net
*net
, u32 type
)
464 struct name_table
*nt
= tipc_name_table(net
);
465 struct hlist_head
*service_head
;
466 struct tipc_service
*service
;
468 service_head
= &nt
->services
[hash(type
)];
469 hlist_for_each_entry_rcu(service
, service_head
, service_list
) {
470 if (service
->type
== type
)
476 struct publication
*tipc_nametbl_insert_publ(struct net
*net
, u32 type
,
477 u32 lower
, u32 upper
,
481 struct name_table
*nt
= tipc_name_table(net
);
482 struct tipc_service
*sc
;
483 struct publication
*p
;
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
);
490 sc
= tipc_service_find(net
, type
);
492 sc
= tipc_service_create(type
, &nt
->services
[hash(type
)]);
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
);
503 struct publication
*tipc_nametbl_remove_publ(struct net
*net
, u32 type
,
504 u32 lower
, u32 upper
,
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
;
516 spin_lock_bh(&sc
->lock
);
517 sr
= tipc_service_find_range(sc
, lower
, upper
);
520 p
= tipc_service_remove_publ(sr
, node
, key
);
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
);
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
);
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
);
543 spin_unlock_bh(&sc
->lock
);
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
555 * - if translation is deferred to another node, leave 'dnode' unchanged and
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
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
565 u32
tipc_nametbl_translate(struct net
*net
, u32 type
, u32 instance
, u32
*dnode
)
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
;
577 if (!tipc_in_scope(legacy
, *dnode
, self
))
581 sc
= tipc_service_find(net
, type
);
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
))
592 p
= list_first_entry(list
, struct publication
,
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
,
599 list_move_tail(&p
->local_publ
, &sr
->local_publ
);
601 list
= &sr
->all_publ
;
602 p
= list_first_entry(list
, struct publication
,
604 list_move_tail(&p
->all_publ
, &sr
->all_publ
);
608 /* Todo: as for legacy, pick the first matching range only, a
609 * "true" round-robin will be performed as needed.
613 spin_unlock_bh(&sc
->lock
);
621 bool tipc_nametbl_lookup(struct net
*net
, u32 type
, u32 instance
, u32 scope
,
622 struct list_head
*dsts
, int *dstcnt
, u32 exclude
,
625 u32 self
= tipc_own_addr(net
);
626 struct service_range
*sr
;
627 struct tipc_service
*sc
;
628 struct publication
*p
;
632 sc
= tipc_service_find(net
, type
);
636 spin_lock_bh(&sc
->lock
);
638 /* Todo: a full search i.e. service_range_foreach_match() instead? */
639 sr
= service_range_match_first(sc
->ranges
.rb_node
, instance
, instance
);
643 list_for_each_entry(p
, &sr
->all_publ
, all_publ
) {
644 if (p
->scope
!= scope
)
646 if (p
->port
== exclude
&& p
->node
== self
)
648 tipc_dest_push(dsts
, p
->node
, p
->port
);
652 list_move_tail(&p
->all_publ
, &sr
->all_publ
);
656 spin_unlock_bh(&sc
->lock
);
659 return !list_empty(dsts
);
662 void tipc_nametbl_mc_lookup(struct net
*net
, u32 type
, u32 lower
, u32 upper
,
663 u32 scope
, bool exact
, struct list_head
*dports
)
665 struct service_range
*sr
;
666 struct tipc_service
*sc
;
667 struct publication
*p
;
670 sc
= tipc_service_find(net
, type
);
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
);
681 spin_unlock_bh(&sc
->lock
);
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
690 void tipc_nametbl_lookup_dst_nodes(struct net
*net
, u32 type
, u32 lower
,
691 u32 upper
, struct tipc_nlist
*nodes
)
693 struct service_range
*sr
;
694 struct tipc_service
*sc
;
695 struct publication
*p
;
698 sc
= tipc_service_find(net
, type
);
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
);
708 spin_unlock_bh(&sc
->lock
);
713 /* tipc_nametbl_build_group - build list of communication group members
715 void tipc_nametbl_build_group(struct net
*net
, struct tipc_group
*grp
,
718 struct service_range
*sr
;
719 struct tipc_service
*sc
;
720 struct publication
*p
;
724 sc
= tipc_service_find(net
, type
);
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
)
734 tipc_group_add_member(grp
, p
->node
, p
->port
, p
->lower
);
737 spin_unlock_bh(&sc
->lock
);
742 /* tipc_nametbl_publish - add service binding to name table
744 struct publication
*tipc_nametbl_publish(struct net
*net
, u32 type
, u32 lower
,
745 u32 upper
, u32 scope
, u32 port
,
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
;
754 spin_lock_bh(&tn
->nametbl_lock
);
756 if (nt
->local_publ_count
>= TIPC_MAX_PUBL
) {
757 pr_warn("Bind failed, max limit %u reached\n", TIPC_MAX_PUBL
);
761 p
= tipc_nametbl_insert_publ(net
, type
, lower
, upper
, scope
,
762 tipc_own_addr(net
), port
, key
);
764 nt
->local_publ_count
++;
765 skb
= tipc_named_publish(net
, p
);
767 rc_dests
= nt
->rc_dests
;
769 spin_unlock_bh(&tn
->nametbl_lock
);
772 tipc_node_broadcast(net
, skb
, rc_dests
);
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
785 int tipc_nametbl_withdraw(struct net
*net
, u32 type
, u32 lower
,
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
;
795 spin_lock_bh(&tn
->nametbl_lock
);
797 p
= tipc_nametbl_remove_publ(net
, type
, lower
, upper
, self
, key
);
799 nt
->local_publ_count
--;
800 skb
= tipc_named_withdraw(net
, p
);
801 list_del_init(&p
->binding_sock
);
804 pr_err("Failed to remove local publication {%u,%u,%u}/%u\n",
805 type
, lower
, upper
, key
);
807 rc_dests
= nt
->rc_dests
;
808 spin_unlock_bh(&tn
->nametbl_lock
);
811 tipc_node_broadcast(net
, skb
, rc_dests
);
818 * tipc_nametbl_subscribe - add a subscription object to the name table
819 * @sub: subscription to add
821 bool tipc_nametbl_subscribe(struct tipc_subscription
*sub
)
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
;
830 spin_lock_bh(&tn
->nametbl_lock
);
831 sc
= tipc_service_find(sub
->net
, type
);
833 sc
= tipc_service_create(type
, &nt
->services
[hash(type
)]);
835 spin_lock_bh(&sc
->lock
);
836 tipc_service_subscribe(sc
, sub
);
837 spin_unlock_bh(&sc
->lock
);
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
));
844 spin_unlock_bh(&tn
->nametbl_lock
);
849 * tipc_nametbl_unsubscribe - remove a subscription object from name table
850 * @sub: subscription to remove
852 void tipc_nametbl_unsubscribe(struct tipc_subscription
*sub
)
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
;
859 spin_lock_bh(&tn
->nametbl_lock
);
860 sc
= tipc_service_find(sub
->net
, type
);
864 spin_lock_bh(&sc
->lock
);
865 list_del_init(&sub
->service_list
);
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
);
873 spin_unlock_bh(&sc
->lock
);
875 spin_unlock_bh(&tn
->nametbl_lock
);
878 int tipc_nametbl_init(struct net
*net
)
880 struct tipc_net
*tn
= tipc_net(net
);
881 struct name_table
*nt
;
884 nt
= kzalloc(sizeof(*nt
), GFP_KERNEL
);
888 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++)
889 INIT_HLIST_HEAD(&nt
->services
[i
]);
891 INIT_LIST_HEAD(&nt
->node_scope
);
892 INIT_LIST_HEAD(&nt
->cluster_scope
);
893 rwlock_init(&nt
->cluster_scope_lock
);
895 spin_lock_init(&tn
->nametbl_lock
);
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
904 static void tipc_service_delete(struct net
*net
, struct tipc_service
*sc
)
906 struct service_range
*sr
, *tmpr
;
907 struct publication
*p
, *tmp
;
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
);
915 rb_erase_augmented(&sr
->tree_node
, &sc
->ranges
, &sr_callbacks
);
918 hlist_del_init_rcu(&sc
->service_list
);
919 spin_unlock_bh(&sc
->lock
);
923 void tipc_nametbl_stop(struct net
*net
)
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
;
931 /* Verify name table is empty and purge any lingering
932 * publications, then release the name table
934 spin_lock_bh(&tn
->nametbl_lock
);
935 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++) {
936 if (hlist_empty(&nt
->services
[i
]))
938 service_head
= &nt
->services
[i
];
939 hlist_for_each_entry_rcu(service
, service_head
, service_list
) {
940 tipc_service_delete(net
, service
);
943 spin_unlock_bh(&tn
->nametbl_lock
);
949 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg
*msg
,
950 struct tipc_service
*service
,
951 struct service_range
*sr
,
954 struct publication
*p
;
955 struct nlattr
*attrs
;
960 list_for_each_entry(p
, &sr
->all_publ
, all_publ
)
961 if (p
->key
== *last_key
)
963 if (p
->key
!= *last_key
)
966 p
= list_first_entry(&sr
->all_publ
,
971 list_for_each_entry_from(p
, &sr
->all_publ
, all_publ
) {
974 hdr
= genlmsg_put(msg
->skb
, msg
->portid
, msg
->seq
,
975 &tipc_genl_family
, NLM_F_MULTI
,
976 TIPC_NL_NAME_TABLE_GET
);
980 attrs
= nla_nest_start_noflag(msg
->skb
, TIPC_NLA_NAME_TABLE
);
984 b
= nla_nest_start_noflag(msg
->skb
, TIPC_NLA_NAME_TABLE_PUBL
);
988 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_TYPE
, service
->type
))
990 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_LOWER
, sr
->lower
))
992 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_UPPER
, sr
->upper
))
994 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_SCOPE
, p
->scope
))
996 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_NODE
, p
->node
))
998 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_REF
, p
->port
))
1000 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_KEY
, p
->key
))
1003 nla_nest_end(msg
->skb
, b
);
1004 nla_nest_end(msg
->skb
, attrs
);
1005 genlmsg_end(msg
->skb
, hdr
);
1012 nla_nest_cancel(msg
->skb
, b
);
1014 nla_nest_cancel(msg
->skb
, attrs
);
1016 genlmsg_cancel(msg
->skb
, hdr
);
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
)
1025 struct service_range
*sr
;
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
)
1033 err
= __tipc_nl_add_nametable_publ(msg
, sc
, sr
, last_key
);
1035 *last_lower
= sr
->lower
;
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
)
1046 struct tipc_net
*tn
= tipc_net(net
);
1047 struct tipc_service
*service
= NULL
;
1048 struct hlist_head
*head
;
1053 i
= hash(*last_type
);
1057 for (; i
< TIPC_NAMETBL_SIZE
; i
++) {
1058 head
= &tn
->nametbl
->services
[i
];
1061 (!i
&& *last_key
&& (*last_lower
== *last_key
))) {
1062 service
= tipc_service_find(net
, *last_type
);
1066 hlist_for_each_entry_rcu(service
, head
, service_list
)
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
,
1079 *last_type
= service
->type
;
1080 spin_unlock_bh(&service
->lock
);
1083 spin_unlock_bh(&service
->lock
);
1090 int tipc_nl_name_table_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
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
;
1104 msg
.portid
= NETLINK_CB(cb
->skb
).portid
;
1105 msg
.seq
= cb
->nlh
->nlmsg_seq
;
1108 err
= tipc_nl_service_list(net
, &msg
, &last_type
,
1109 &last_lower
, &last_key
);
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
1123 cb
->args
[0] = last_type
;
1124 cb
->args
[1] = last_lower
;
1125 cb
->args
[2] = last_key
;
1131 struct tipc_dest
*tipc_dest_find(struct list_head
*l
, u32 node
, u32 port
)
1133 struct tipc_dest
*dst
;
1135 list_for_each_entry(dst
, l
, list
) {
1136 if (dst
->node
== node
&& dst
->port
== port
)
1142 bool tipc_dest_push(struct list_head
*l
, u32 node
, u32 port
)
1144 struct tipc_dest
*dst
;
1146 if (tipc_dest_find(l
, node
, port
))
1149 dst
= kmalloc(sizeof(*dst
), GFP_ATOMIC
);
1154 list_add(&dst
->list
, l
);
1158 bool tipc_dest_pop(struct list_head
*l
, u32
*node
, u32
*port
)
1160 struct tipc_dest
*dst
;
1164 dst
= list_first_entry(l
, typeof(*dst
), list
);
1169 list_del(&dst
->list
);
1174 bool tipc_dest_del(struct list_head
*l
, u32 node
, u32 port
)
1176 struct tipc_dest
*dst
;
1178 dst
= tipc_dest_find(l
, node
, port
);
1181 list_del(&dst
->list
);
1186 void tipc_dest_list_purge(struct list_head
*l
)
1188 struct tipc_dest
*dst
, *tmp
;
1190 list_for_each_entry_safe(dst
, tmp
, l
, list
) {
1191 list_del(&dst
->list
);
1196 int tipc_dest_list_len(struct list_head
*l
)
1198 struct tipc_dest
*dst
;
1201 list_for_each_entry(dst
, l
, list
) {