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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
40 #include "name_table.h"
41 #include "name_distr.h"
49 * struct service_range - container for all bindings of a service range
50 * @lower: service range lower bound
51 * @upper: service range upper bound
52 * @tree_node: member of service range RB tree
53 * @local_publ: list of identical publications made from this node
54 * Used by closest_first lookup and multicast lookup algorithm
55 * @all_publ: all publications identical to this one, whatever node and scope
56 * Used by round-robin lookup algorithm
58 struct service_range
{
61 struct rb_node tree_node
;
62 struct list_head local_publ
;
63 struct list_head all_publ
;
67 * struct tipc_service - container for all published instances of a service type
68 * @type: 32 bit 'type' value for service
69 * @ranges: rb tree containing all service ranges for this service
70 * @service_list: links to adjacent name ranges in hash chain
71 * @subscriptions: list of subscriptions for this service type
72 * @lock: spinlock controlling access to pertaining service ranges/publications
73 * @rcu: RCU callback head used for deferred freeing
77 struct rb_root ranges
;
78 struct hlist_node service_list
;
79 struct list_head subscriptions
;
80 spinlock_t lock
; /* Covers service range list */
84 static int hash(int x
)
86 return x
& (TIPC_NAMETBL_SIZE
- 1);
90 * tipc_publ_create - create a publication structure
92 static struct publication
*tipc_publ_create(u32 type
, u32 lower
, u32 upper
,
93 u32 scope
, u32 node
, u32 port
,
96 struct publication
*publ
= kzalloc(sizeof(*publ
), GFP_ATOMIC
);
108 INIT_LIST_HEAD(&publ
->binding_sock
);
109 INIT_LIST_HEAD(&publ
->binding_node
);
110 INIT_LIST_HEAD(&publ
->local_publ
);
111 INIT_LIST_HEAD(&publ
->all_publ
);
116 * tipc_service_create - create a service structure for the specified 'type'
118 * Allocates a single range structure and sets it to all 0's.
120 static struct tipc_service
*tipc_service_create(u32 type
, struct hlist_head
*hd
)
122 struct tipc_service
*service
= kzalloc(sizeof(*service
), GFP_ATOMIC
);
125 pr_warn("Service creation failed, no memory\n");
129 spin_lock_init(&service
->lock
);
130 service
->type
= type
;
131 service
->ranges
= RB_ROOT
;
132 INIT_HLIST_NODE(&service
->service_list
);
133 INIT_LIST_HEAD(&service
->subscriptions
);
134 hlist_add_head_rcu(&service
->service_list
, hd
);
139 * tipc_service_first_range - find first service range in tree matching instance
141 * Very time-critical, so binary search through range rb tree
143 static struct service_range
*tipc_service_first_range(struct tipc_service
*sc
,
146 struct rb_node
*n
= sc
->ranges
.rb_node
;
147 struct service_range
*sr
;
150 sr
= container_of(n
, struct service_range
, tree_node
);
151 if (sr
->lower
> instance
)
153 else if (sr
->upper
< instance
)
161 /* tipc_service_find_range - find service range matching publication parameters
163 static struct service_range
*tipc_service_find_range(struct tipc_service
*sc
,
164 u32 lower
, u32 upper
)
166 struct rb_node
*n
= sc
->ranges
.rb_node
;
167 struct service_range
*sr
;
169 sr
= tipc_service_first_range(sc
, lower
);
173 /* Look for exact match */
174 for (n
= &sr
->tree_node
; n
; n
= rb_next(n
)) {
175 sr
= container_of(n
, struct service_range
, tree_node
);
176 if (sr
->upper
== upper
)
179 if (!n
|| sr
->lower
!= lower
|| sr
->upper
!= upper
)
185 static struct service_range
*tipc_service_create_range(struct tipc_service
*sc
,
186 u32 lower
, u32 upper
)
188 struct rb_node
**n
, *parent
= NULL
;
189 struct service_range
*sr
, *tmp
;
191 n
= &sc
->ranges
.rb_node
;
193 tmp
= container_of(*n
, struct service_range
, tree_node
);
195 tmp
= container_of(parent
, struct service_range
, tree_node
);
196 if (lower
< tmp
->lower
)
198 else if (lower
> tmp
->lower
)
200 else if (upper
< tmp
->upper
)
202 else if (upper
> tmp
->upper
)
207 sr
= kzalloc(sizeof(*sr
), GFP_ATOMIC
);
212 INIT_LIST_HEAD(&sr
->local_publ
);
213 INIT_LIST_HEAD(&sr
->all_publ
);
214 rb_link_node(&sr
->tree_node
, parent
, n
);
215 rb_insert_color(&sr
->tree_node
, &sc
->ranges
);
219 static struct publication
*tipc_service_insert_publ(struct net
*net
,
220 struct tipc_service
*sc
,
222 u32 upper
, u32 scope
,
226 struct tipc_subscription
*sub
, *tmp
;
227 struct service_range
*sr
;
228 struct publication
*p
;
231 sr
= tipc_service_create_range(sc
, lower
, upper
);
235 first
= list_empty(&sr
->all_publ
);
237 /* Return if the publication already exists */
238 list_for_each_entry(p
, &sr
->all_publ
, all_publ
) {
239 if (p
->key
== key
&& (!p
->node
|| p
->node
== node
))
243 /* Create and insert publication */
244 p
= tipc_publ_create(type
, lower
, upper
, scope
, node
, port
, key
);
247 if (in_own_node(net
, node
))
248 list_add(&p
->local_publ
, &sr
->local_publ
);
249 list_add(&p
->all_publ
, &sr
->all_publ
);
251 /* Any subscriptions waiting for notification? */
252 list_for_each_entry_safe(sub
, tmp
, &sc
->subscriptions
, service_list
) {
253 tipc_sub_report_overlap(sub
, p
->lower
, p
->upper
, TIPC_PUBLISHED
,
254 p
->port
, p
->node
, p
->scope
, first
);
258 pr_warn("Failed to bind to %u,%u,%u, no memory\n", type
, lower
, upper
);
263 * tipc_service_remove_publ - remove a publication from a service
265 static struct publication
*tipc_service_remove_publ(struct service_range
*sr
,
268 struct publication
*p
;
270 list_for_each_entry(p
, &sr
->all_publ
, all_publ
) {
271 if (p
->key
!= key
|| (node
&& node
!= p
->node
))
273 list_del(&p
->all_publ
);
274 list_del(&p
->local_publ
);
281 * tipc_service_subscribe - attach a subscription, and optionally
282 * issue the prescribed number of events if there is any service
283 * range overlapping with the requested range
285 static void tipc_service_subscribe(struct tipc_service
*service
,
286 struct tipc_subscription
*sub
)
288 struct tipc_subscr
*sb
= &sub
->evt
.s
;
289 struct service_range
*sr
;
290 struct tipc_name_seq ns
;
291 struct publication
*p
;
295 ns
.type
= tipc_sub_read(sb
, seq
.type
);
296 ns
.lower
= tipc_sub_read(sb
, seq
.lower
);
297 ns
.upper
= tipc_sub_read(sb
, seq
.upper
);
300 list_add(&sub
->service_list
, &service
->subscriptions
);
302 if (tipc_sub_read(sb
, filter
) & TIPC_SUB_NO_STATUS
)
305 for (n
= rb_first(&service
->ranges
); n
; n
= rb_next(n
)) {
306 sr
= container_of(n
, struct service_range
, tree_node
);
307 if (sr
->lower
> ns
.upper
)
309 if (!tipc_sub_check_overlap(&ns
, sr
->lower
, sr
->upper
))
313 list_for_each_entry(p
, &sr
->all_publ
, all_publ
) {
314 tipc_sub_report_overlap(sub
, sr
->lower
, sr
->upper
,
315 TIPC_PUBLISHED
, p
->port
,
316 p
->node
, p
->scope
, first
);
322 static struct tipc_service
*tipc_service_find(struct net
*net
, u32 type
)
324 struct name_table
*nt
= tipc_name_table(net
);
325 struct hlist_head
*service_head
;
326 struct tipc_service
*service
;
328 service_head
= &nt
->services
[hash(type
)];
329 hlist_for_each_entry_rcu(service
, service_head
, service_list
) {
330 if (service
->type
== type
)
336 struct publication
*tipc_nametbl_insert_publ(struct net
*net
, u32 type
,
337 u32 lower
, u32 upper
,
341 struct name_table
*nt
= tipc_name_table(net
);
342 struct tipc_service
*sc
;
343 struct publication
*p
;
345 if (scope
> TIPC_NODE_SCOPE
|| lower
> upper
) {
346 pr_debug("Failed to bind illegal {%u,%u,%u} with scope %u\n",
347 type
, lower
, upper
, scope
);
350 sc
= tipc_service_find(net
, type
);
352 sc
= tipc_service_create(type
, &nt
->services
[hash(type
)]);
356 spin_lock_bh(&sc
->lock
);
357 p
= tipc_service_insert_publ(net
, sc
, type
, lower
, upper
,
358 scope
, node
, port
, key
);
359 spin_unlock_bh(&sc
->lock
);
363 struct publication
*tipc_nametbl_remove_publ(struct net
*net
, u32 type
,
364 u32 lower
, u32 upper
,
367 struct tipc_service
*sc
= tipc_service_find(net
, type
);
368 struct tipc_subscription
*sub
, *tmp
;
369 struct service_range
*sr
= NULL
;
370 struct publication
*p
= NULL
;
376 spin_lock_bh(&sc
->lock
);
377 sr
= tipc_service_find_range(sc
, lower
, upper
);
380 p
= tipc_service_remove_publ(sr
, node
, key
);
384 /* Notify any waiting subscriptions */
385 last
= list_empty(&sr
->all_publ
);
386 list_for_each_entry_safe(sub
, tmp
, &sc
->subscriptions
, service_list
) {
387 tipc_sub_report_overlap(sub
, lower
, upper
, TIPC_WITHDRAWN
,
388 p
->port
, node
, p
->scope
, last
);
391 /* Remove service range item if this was its last publication */
392 if (list_empty(&sr
->all_publ
)) {
393 rb_erase(&sr
->tree_node
, &sc
->ranges
);
397 /* Delete service item if this no more publications and subscriptions */
398 if (RB_EMPTY_ROOT(&sc
->ranges
) && list_empty(&sc
->subscriptions
)) {
399 hlist_del_init_rcu(&sc
->service_list
);
403 spin_unlock_bh(&sc
->lock
);
408 * tipc_nametbl_translate - perform service instance to socket translation
410 * On entry, 'dnode' is the search domain used during translation.
413 * - if translation is deferred to another node, leave 'dnode' unchanged and
415 * - if translation is attempted and succeeds, set 'dnode' to the publishing
416 * node and return the published (non-zero) port number
417 * - if translation is attempted and fails, set 'dnode' to 0 and return 0
419 * Note that for legacy users (node configured with Z.C.N address format) the
420 * 'closest-first' lookup algorithm must be maintained, i.e., if dnode is 0
421 * we must look in the local binding list first
423 u32
tipc_nametbl_translate(struct net
*net
, u32 type
, u32 instance
, u32
*dnode
)
425 struct tipc_net
*tn
= tipc_net(net
);
426 bool legacy
= tn
->legacy_addr_format
;
427 u32 self
= tipc_own_addr(net
);
428 struct service_range
*sr
;
429 struct tipc_service
*sc
;
430 struct list_head
*list
;
431 struct publication
*p
;
435 if (!tipc_in_scope(legacy
, *dnode
, self
))
439 sc
= tipc_service_find(net
, type
);
443 spin_lock_bh(&sc
->lock
);
444 sr
= tipc_service_first_range(sc
, instance
);
448 /* Select lookup algorithm: local, closest-first or round-robin */
449 if (*dnode
== self
) {
450 list
= &sr
->local_publ
;
451 if (list_empty(list
))
453 p
= list_first_entry(list
, struct publication
, local_publ
);
454 list_move_tail(&p
->local_publ
, &sr
->local_publ
);
455 } else if (legacy
&& !*dnode
&& !list_empty(&sr
->local_publ
)) {
456 list
= &sr
->local_publ
;
457 p
= list_first_entry(list
, struct publication
, local_publ
);
458 list_move_tail(&p
->local_publ
, &sr
->local_publ
);
460 list
= &sr
->all_publ
;
461 p
= list_first_entry(list
, struct publication
, all_publ
);
462 list_move_tail(&p
->all_publ
, &sr
->all_publ
);
467 spin_unlock_bh(&sc
->lock
);
474 bool tipc_nametbl_lookup(struct net
*net
, u32 type
, u32 instance
, u32 scope
,
475 struct list_head
*dsts
, int *dstcnt
, u32 exclude
,
478 u32 self
= tipc_own_addr(net
);
479 struct service_range
*sr
;
480 struct tipc_service
*sc
;
481 struct publication
*p
;
485 sc
= tipc_service_find(net
, type
);
489 spin_lock_bh(&sc
->lock
);
491 sr
= tipc_service_first_range(sc
, instance
);
495 list_for_each_entry(p
, &sr
->all_publ
, all_publ
) {
496 if (p
->scope
!= scope
)
498 if (p
->port
== exclude
&& p
->node
== self
)
500 tipc_dest_push(dsts
, p
->node
, p
->port
);
504 list_move_tail(&p
->all_publ
, &sr
->all_publ
);
508 spin_unlock_bh(&sc
->lock
);
511 return !list_empty(dsts
);
514 void tipc_nametbl_mc_lookup(struct net
*net
, u32 type
, u32 lower
, u32 upper
,
515 u32 scope
, bool exact
, struct list_head
*dports
)
517 struct service_range
*sr
;
518 struct tipc_service
*sc
;
519 struct publication
*p
;
523 sc
= tipc_service_find(net
, type
);
527 spin_lock_bh(&sc
->lock
);
529 for (n
= rb_first(&sc
->ranges
); n
; n
= rb_next(n
)) {
530 sr
= container_of(n
, struct service_range
, tree_node
);
531 if (sr
->upper
< lower
)
533 if (sr
->lower
> upper
)
535 list_for_each_entry(p
, &sr
->local_publ
, local_publ
) {
536 if (p
->scope
== scope
|| (!exact
&& p
->scope
< scope
))
537 tipc_dest_push(dports
, 0, p
->port
);
540 spin_unlock_bh(&sc
->lock
);
545 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
546 * - Creates list of nodes that overlap the given multicast address
547 * - Determines if any node local destinations overlap
549 void tipc_nametbl_lookup_dst_nodes(struct net
*net
, u32 type
, u32 lower
,
550 u32 upper
, struct tipc_nlist
*nodes
)
552 struct service_range
*sr
;
553 struct tipc_service
*sc
;
554 struct publication
*p
;
558 sc
= tipc_service_find(net
, type
);
562 spin_lock_bh(&sc
->lock
);
564 for (n
= rb_first(&sc
->ranges
); n
; n
= rb_next(n
)) {
565 sr
= container_of(n
, struct service_range
, tree_node
);
566 if (sr
->upper
< lower
)
568 if (sr
->lower
> upper
)
570 list_for_each_entry(p
, &sr
->all_publ
, all_publ
) {
571 tipc_nlist_add(nodes
, p
->node
);
574 spin_unlock_bh(&sc
->lock
);
579 /* tipc_nametbl_build_group - build list of communication group members
581 void tipc_nametbl_build_group(struct net
*net
, struct tipc_group
*grp
,
584 struct service_range
*sr
;
585 struct tipc_service
*sc
;
586 struct publication
*p
;
590 sc
= tipc_service_find(net
, type
);
594 spin_lock_bh(&sc
->lock
);
595 for (n
= rb_first(&sc
->ranges
); n
; n
= rb_next(n
)) {
596 sr
= container_of(n
, struct service_range
, tree_node
);
597 list_for_each_entry(p
, &sr
->all_publ
, all_publ
) {
598 if (p
->scope
!= scope
)
600 tipc_group_add_member(grp
, p
->node
, p
->port
, p
->lower
);
603 spin_unlock_bh(&sc
->lock
);
608 /* tipc_nametbl_publish - add service binding to name table
610 struct publication
*tipc_nametbl_publish(struct net
*net
, u32 type
, u32 lower
,
611 u32 upper
, u32 scope
, u32 port
,
614 struct name_table
*nt
= tipc_name_table(net
);
615 struct tipc_net
*tn
= tipc_net(net
);
616 struct publication
*p
= NULL
;
617 struct sk_buff
*skb
= NULL
;
619 spin_lock_bh(&tn
->nametbl_lock
);
621 if (nt
->local_publ_count
>= TIPC_MAX_PUBL
) {
622 pr_warn("Bind failed, max limit %u reached\n", TIPC_MAX_PUBL
);
626 p
= tipc_nametbl_insert_publ(net
, type
, lower
, upper
, scope
,
627 tipc_own_addr(net
), port
, key
);
629 nt
->local_publ_count
++;
630 skb
= tipc_named_publish(net
, p
);
633 spin_unlock_bh(&tn
->nametbl_lock
);
636 tipc_node_broadcast(net
, skb
);
641 * tipc_nametbl_withdraw - withdraw a service binding
643 int tipc_nametbl_withdraw(struct net
*net
, u32 type
, u32 lower
,
646 struct name_table
*nt
= tipc_name_table(net
);
647 struct tipc_net
*tn
= tipc_net(net
);
648 u32 self
= tipc_own_addr(net
);
649 struct sk_buff
*skb
= NULL
;
650 struct publication
*p
;
652 spin_lock_bh(&tn
->nametbl_lock
);
654 p
= tipc_nametbl_remove_publ(net
, type
, lower
, upper
, self
, key
);
656 nt
->local_publ_count
--;
657 skb
= tipc_named_withdraw(net
, p
);
658 list_del_init(&p
->binding_sock
);
661 pr_err("Failed to remove local publication {%u,%u,%u}/%u\n",
662 type
, lower
, upper
, key
);
664 spin_unlock_bh(&tn
->nametbl_lock
);
667 tipc_node_broadcast(net
, skb
);
674 * tipc_nametbl_subscribe - add a subscription object to the name table
676 bool tipc_nametbl_subscribe(struct tipc_subscription
*sub
)
678 struct name_table
*nt
= tipc_name_table(sub
->net
);
679 struct tipc_net
*tn
= tipc_net(sub
->net
);
680 struct tipc_subscr
*s
= &sub
->evt
.s
;
681 u32 type
= tipc_sub_read(s
, seq
.type
);
682 struct tipc_service
*sc
;
685 spin_lock_bh(&tn
->nametbl_lock
);
686 sc
= tipc_service_find(sub
->net
, type
);
688 sc
= tipc_service_create(type
, &nt
->services
[hash(type
)]);
690 spin_lock_bh(&sc
->lock
);
691 tipc_service_subscribe(sc
, sub
);
692 spin_unlock_bh(&sc
->lock
);
694 pr_warn("Failed to subscribe for {%u,%u,%u}\n", type
,
695 tipc_sub_read(s
, seq
.lower
),
696 tipc_sub_read(s
, seq
.upper
));
699 spin_unlock_bh(&tn
->nametbl_lock
);
704 * tipc_nametbl_unsubscribe - remove a subscription object from name table
706 void tipc_nametbl_unsubscribe(struct tipc_subscription
*sub
)
708 struct tipc_net
*tn
= tipc_net(sub
->net
);
709 struct tipc_subscr
*s
= &sub
->evt
.s
;
710 u32 type
= tipc_sub_read(s
, seq
.type
);
711 struct tipc_service
*sc
;
713 spin_lock_bh(&tn
->nametbl_lock
);
714 sc
= tipc_service_find(sub
->net
, type
);
718 spin_lock_bh(&sc
->lock
);
719 list_del_init(&sub
->service_list
);
722 /* Delete service item if no more publications and subscriptions */
723 if (RB_EMPTY_ROOT(&sc
->ranges
) && list_empty(&sc
->subscriptions
)) {
724 hlist_del_init_rcu(&sc
->service_list
);
727 spin_unlock_bh(&sc
->lock
);
729 spin_unlock_bh(&tn
->nametbl_lock
);
732 int tipc_nametbl_init(struct net
*net
)
734 struct tipc_net
*tn
= tipc_net(net
);
735 struct name_table
*nt
;
738 nt
= kzalloc(sizeof(*nt
), GFP_KERNEL
);
742 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++)
743 INIT_HLIST_HEAD(&nt
->services
[i
]);
745 INIT_LIST_HEAD(&nt
->node_scope
);
746 INIT_LIST_HEAD(&nt
->cluster_scope
);
747 rwlock_init(&nt
->cluster_scope_lock
);
749 spin_lock_init(&tn
->nametbl_lock
);
754 * tipc_service_delete - purge all publications for a service and delete it
756 static void tipc_service_delete(struct net
*net
, struct tipc_service
*sc
)
758 struct service_range
*sr
, *tmpr
;
759 struct publication
*p
, *tmp
;
761 spin_lock_bh(&sc
->lock
);
762 rbtree_postorder_for_each_entry_safe(sr
, tmpr
, &sc
->ranges
, tree_node
) {
763 list_for_each_entry_safe(p
, tmp
, &sr
->all_publ
, all_publ
) {
764 tipc_service_remove_publ(sr
, p
->node
, p
->key
);
767 rb_erase(&sr
->tree_node
, &sc
->ranges
);
770 hlist_del_init_rcu(&sc
->service_list
);
771 spin_unlock_bh(&sc
->lock
);
775 void tipc_nametbl_stop(struct net
*net
)
777 struct name_table
*nt
= tipc_name_table(net
);
778 struct tipc_net
*tn
= tipc_net(net
);
779 struct hlist_head
*service_head
;
780 struct tipc_service
*service
;
783 /* Verify name table is empty and purge any lingering
784 * publications, then release the name table
786 spin_lock_bh(&tn
->nametbl_lock
);
787 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++) {
788 if (hlist_empty(&nt
->services
[i
]))
790 service_head
= &nt
->services
[i
];
791 hlist_for_each_entry_rcu(service
, service_head
, service_list
) {
792 tipc_service_delete(net
, service
);
795 spin_unlock_bh(&tn
->nametbl_lock
);
801 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg
*msg
,
802 struct tipc_service
*service
,
803 struct service_range
*sr
,
806 struct publication
*p
;
807 struct nlattr
*attrs
;
812 list_for_each_entry(p
, &sr
->all_publ
, all_publ
)
813 if (p
->key
== *last_key
)
815 if (p
->key
!= *last_key
)
818 p
= list_first_entry(&sr
->all_publ
,
823 list_for_each_entry_from(p
, &sr
->all_publ
, all_publ
) {
826 hdr
= genlmsg_put(msg
->skb
, msg
->portid
, msg
->seq
,
827 &tipc_genl_family
, NLM_F_MULTI
,
828 TIPC_NL_NAME_TABLE_GET
);
832 attrs
= nla_nest_start(msg
->skb
, TIPC_NLA_NAME_TABLE
);
836 b
= nla_nest_start(msg
->skb
, TIPC_NLA_NAME_TABLE_PUBL
);
840 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_TYPE
, service
->type
))
842 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_LOWER
, sr
->lower
))
844 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_UPPER
, sr
->upper
))
846 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_SCOPE
, p
->scope
))
848 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_NODE
, p
->node
))
850 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_REF
, p
->port
))
852 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_KEY
, p
->key
))
855 nla_nest_end(msg
->skb
, b
);
856 nla_nest_end(msg
->skb
, attrs
);
857 genlmsg_end(msg
->skb
, hdr
);
864 nla_nest_cancel(msg
->skb
, b
);
866 nla_nest_cancel(msg
->skb
, attrs
);
868 genlmsg_cancel(msg
->skb
, hdr
);
873 static int __tipc_nl_service_range_list(struct tipc_nl_msg
*msg
,
874 struct tipc_service
*sc
,
875 u32
*last_lower
, u32
*last_key
)
877 struct service_range
*sr
;
881 for (n
= rb_first(&sc
->ranges
); n
; n
= rb_next(n
)) {
882 sr
= container_of(n
, struct service_range
, tree_node
);
883 if (sr
->lower
< *last_lower
)
885 err
= __tipc_nl_add_nametable_publ(msg
, sc
, sr
, last_key
);
887 *last_lower
= sr
->lower
;
895 static int tipc_nl_service_list(struct net
*net
, struct tipc_nl_msg
*msg
,
896 u32
*last_type
, u32
*last_lower
, u32
*last_key
)
898 struct tipc_net
*tn
= tipc_net(net
);
899 struct tipc_service
*service
= NULL
;
900 struct hlist_head
*head
;
905 i
= hash(*last_type
);
909 for (; i
< TIPC_NAMETBL_SIZE
; i
++) {
910 head
= &tn
->nametbl
->services
[i
];
913 (!i
&& *last_key
&& (*last_lower
== *last_key
))) {
914 service
= tipc_service_find(net
, *last_type
);
918 hlist_for_each_entry_rcu(service
, head
, service_list
)
924 hlist_for_each_entry_from_rcu(service
, service_list
) {
925 spin_lock_bh(&service
->lock
);
926 err
= __tipc_nl_service_range_list(msg
, service
,
931 *last_type
= service
->type
;
932 spin_unlock_bh(&service
->lock
);
935 spin_unlock_bh(&service
->lock
);
942 int tipc_nl_name_table_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
944 struct net
*net
= sock_net(skb
->sk
);
945 u32 last_type
= cb
->args
[0];
946 u32 last_lower
= cb
->args
[1];
947 u32 last_key
= cb
->args
[2];
948 int done
= cb
->args
[3];
949 struct tipc_nl_msg msg
;
956 msg
.portid
= NETLINK_CB(cb
->skb
).portid
;
957 msg
.seq
= cb
->nlh
->nlmsg_seq
;
960 err
= tipc_nl_service_list(net
, &msg
, &last_type
,
961 &last_lower
, &last_key
);
964 } else if (err
!= -EMSGSIZE
) {
965 /* We never set seq or call nl_dump_check_consistent() this
966 * means that setting prev_seq here will cause the consistence
967 * check to fail in the netlink callback handler. Resulting in
968 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
975 cb
->args
[0] = last_type
;
976 cb
->args
[1] = last_lower
;
977 cb
->args
[2] = last_key
;
983 struct tipc_dest
*tipc_dest_find(struct list_head
*l
, u32 node
, u32 port
)
985 struct tipc_dest
*dst
;
987 list_for_each_entry(dst
, l
, list
) {
988 if (dst
->node
== node
&& dst
->port
== port
)
994 bool tipc_dest_push(struct list_head
*l
, u32 node
, u32 port
)
996 struct tipc_dest
*dst
;
998 if (tipc_dest_find(l
, node
, port
))
1001 dst
= kmalloc(sizeof(*dst
), GFP_ATOMIC
);
1006 list_add(&dst
->list
, l
);
1010 bool tipc_dest_pop(struct list_head
*l
, u32
*node
, u32
*port
)
1012 struct tipc_dest
*dst
;
1016 dst
= list_first_entry(l
, typeof(*dst
), list
);
1021 list_del(&dst
->list
);
1026 bool tipc_dest_del(struct list_head
*l
, u32 node
, u32 port
)
1028 struct tipc_dest
*dst
;
1030 dst
= tipc_dest_find(l
, node
, port
);
1033 list_del(&dst
->list
);
1038 void tipc_dest_list_purge(struct list_head
*l
)
1040 struct tipc_dest
*dst
, *tmp
;
1042 list_for_each_entry_safe(dst
, tmp
, l
, list
) {
1043 list_del(&dst
->list
);
1048 int tipc_dest_list_len(struct list_head
*l
)
1050 struct tipc_dest
*dst
;
1053 list_for_each_entry(dst
, l
, list
) {