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.
38 #include <linux/list_sort.h>
41 #include "name_table.h"
42 #include "name_distr.h"
50 * struct service_range - container for all bindings of a service range
51 * @lower: service range lower bound
52 * @upper: service range upper bound
53 * @tree_node: member of service range RB tree
54 * @local_publ: list of identical publications made from this node
55 * Used by closest_first lookup and multicast lookup algorithm
56 * @all_publ: all publications identical to this one, whatever node and scope
57 * Used by round-robin lookup algorithm
59 struct service_range {
62 struct rb_node tree_node;
63 struct list_head local_publ;
64 struct list_head all_publ;
68 * struct tipc_service - container for all published instances of a service type
69 * @type: 32 bit 'type' value for service
70 * @publ_cnt: increasing counter for publications in this service
71 * @ranges: rb tree containing all service ranges for this service
72 * @service_list: links to adjacent name ranges in hash chain
73 * @subscriptions: list of subscriptions for this service type
74 * @lock: spinlock controlling access to pertaining service ranges/publications
75 * @rcu: RCU callback head used for deferred freeing
80 struct rb_root ranges;
81 struct hlist_node service_list;
82 struct list_head subscriptions;
83 spinlock_t lock; /* Covers service range list */
87 static int hash(int x)
89 return x & (TIPC_NAMETBL_SIZE - 1);
93 * tipc_publ_create - create a publication structure
95 static struct publication *tipc_publ_create(u32 type, u32 lower, u32 upper,
96 u32 scope, u32 node, u32 port,
99 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
111 INIT_LIST_HEAD(&publ->binding_sock);
112 INIT_LIST_HEAD(&publ->binding_node);
113 INIT_LIST_HEAD(&publ->local_publ);
114 INIT_LIST_HEAD(&publ->all_publ);
115 INIT_LIST_HEAD(&publ->list);
120 * tipc_service_create - create a service structure for the specified 'type'
122 * Allocates a single range structure and sets it to all 0's.
124 static struct tipc_service *tipc_service_create(u32 type, struct hlist_head *hd)
126 struct tipc_service *service = kzalloc(sizeof(*service), GFP_ATOMIC);
129 pr_warn("Service creation failed, no memory\n");
133 spin_lock_init(&service->lock);
134 service->type = type;
135 service->ranges = RB_ROOT;
136 INIT_HLIST_NODE(&service->service_list);
137 INIT_LIST_HEAD(&service->subscriptions);
138 hlist_add_head_rcu(&service->service_list, hd);
143 * tipc_service_first_range - find first service range in tree matching instance
145 * Very time-critical, so binary search through range rb tree
147 static struct service_range *tipc_service_first_range(struct tipc_service *sc,
150 struct rb_node *n = sc->ranges.rb_node;
151 struct service_range *sr;
154 sr = container_of(n, struct service_range, tree_node);
155 if (sr->lower > instance)
157 else if (sr->upper < instance)
165 /* tipc_service_find_range - find service range matching publication parameters
167 static struct service_range *tipc_service_find_range(struct tipc_service *sc,
168 u32 lower, u32 upper)
170 struct rb_node *n = sc->ranges.rb_node;
171 struct service_range *sr;
173 sr = tipc_service_first_range(sc, lower);
177 /* Look for exact match */
178 for (n = &sr->tree_node; n; n = rb_next(n)) {
179 sr = container_of(n, struct service_range, tree_node);
180 if (sr->upper == upper)
183 if (!n || sr->lower != lower || sr->upper != upper)
189 static struct service_range *tipc_service_create_range(struct tipc_service *sc,
190 u32 lower, u32 upper)
192 struct rb_node **n, *parent = NULL;
193 struct service_range *sr, *tmp;
195 n = &sc->ranges.rb_node;
197 tmp = container_of(*n, struct service_range, tree_node);
199 tmp = container_of(parent, struct service_range, tree_node);
200 if (lower < tmp->lower)
202 else if (lower > tmp->lower)
204 else if (upper < tmp->upper)
206 else if (upper > tmp->upper)
211 sr = kzalloc(sizeof(*sr), GFP_ATOMIC);
216 INIT_LIST_HEAD(&sr->local_publ);
217 INIT_LIST_HEAD(&sr->all_publ);
218 rb_link_node(&sr->tree_node, parent, n);
219 rb_insert_color(&sr->tree_node, &sc->ranges);
223 static struct publication *tipc_service_insert_publ(struct net *net,
224 struct tipc_service *sc,
226 u32 upper, u32 scope,
230 struct tipc_subscription *sub, *tmp;
231 struct service_range *sr;
232 struct publication *p;
235 sr = tipc_service_create_range(sc, lower, upper);
239 first = list_empty(&sr->all_publ);
241 /* Return if the publication already exists */
242 list_for_each_entry(p, &sr->all_publ, all_publ) {
243 if (p->key == key && (!p->node || p->node == node))
247 /* Create and insert publication */
248 p = tipc_publ_create(type, lower, upper, scope, node, port, key);
251 /* Suppose there shouldn't be a huge gap btw publs i.e. >INT_MAX */
252 p->id = sc->publ_cnt++;
253 if (in_own_node(net, node))
254 list_add(&p->local_publ, &sr->local_publ);
255 list_add(&p->all_publ, &sr->all_publ);
257 /* Any subscriptions waiting for notification? */
258 list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
259 tipc_sub_report_overlap(sub, p->lower, p->upper, TIPC_PUBLISHED,
260 p->port, p->node, p->scope, first);
264 pr_warn("Failed to bind to %u,%u,%u, no memory\n", type, lower, upper);
269 * tipc_service_remove_publ - remove a publication from a service
271 static struct publication *tipc_service_remove_publ(struct service_range *sr,
274 struct publication *p;
276 list_for_each_entry(p, &sr->all_publ, all_publ) {
277 if (p->key != key || (node && node != p->node))
279 list_del(&p->all_publ);
280 list_del(&p->local_publ);
287 * Code reused: time_after32() for the same purpose
289 #define publication_after(pa, pb) time_after32((pa)->id, (pb)->id)
290 static int tipc_publ_sort(void *priv, struct list_head *a,
293 struct publication *pa, *pb;
295 pa = container_of(a, struct publication, list);
296 pb = container_of(b, struct publication, list);
297 return publication_after(pa, pb);
301 * tipc_service_subscribe - attach a subscription, and optionally
302 * issue the prescribed number of events if there is any service
303 * range overlapping with the requested range
305 static void tipc_service_subscribe(struct tipc_service *service,
306 struct tipc_subscription *sub)
308 struct tipc_subscr *sb = &sub->evt.s;
309 struct publication *p, *first, *tmp;
310 struct list_head publ_list;
311 struct service_range *sr;
312 struct tipc_name_seq ns;
316 ns.type = tipc_sub_read(sb, seq.type);
317 ns.lower = tipc_sub_read(sb, seq.lower);
318 ns.upper = tipc_sub_read(sb, seq.upper);
319 filter = tipc_sub_read(sb, filter);
322 list_add(&sub->service_list, &service->subscriptions);
324 if (filter & TIPC_SUB_NO_STATUS)
327 INIT_LIST_HEAD(&publ_list);
328 for (n = rb_first(&service->ranges); n; n = rb_next(n)) {
329 sr = container_of(n, struct service_range, tree_node);
330 if (sr->lower > ns.upper)
332 if (!tipc_sub_check_overlap(&ns, sr->lower, sr->upper))
336 list_for_each_entry(p, &sr->all_publ, all_publ) {
337 if (filter & TIPC_SUB_PORTS)
338 list_add_tail(&p->list, &publ_list);
339 else if (!first || publication_after(first, p))
340 /* Pick this range's *first* publication */
344 list_add_tail(&first->list, &publ_list);
347 /* Sort the publications before reporting */
348 list_sort(NULL, &publ_list, tipc_publ_sort);
349 list_for_each_entry_safe(p, tmp, &publ_list, list) {
350 tipc_sub_report_overlap(sub, p->lower, p->upper,
351 TIPC_PUBLISHED, p->port, p->node,
353 list_del_init(&p->list);
357 static struct tipc_service *tipc_service_find(struct net *net, u32 type)
359 struct name_table *nt = tipc_name_table(net);
360 struct hlist_head *service_head;
361 struct tipc_service *service;
363 service_head = &nt->services[hash(type)];
364 hlist_for_each_entry_rcu(service, service_head, service_list) {
365 if (service->type == type)
371 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
372 u32 lower, u32 upper,
376 struct name_table *nt = tipc_name_table(net);
377 struct tipc_service *sc;
378 struct publication *p;
380 if (scope > TIPC_NODE_SCOPE || lower > upper) {
381 pr_debug("Failed to bind illegal {%u,%u,%u} with scope %u\n",
382 type, lower, upper, scope);
385 sc = tipc_service_find(net, type);
387 sc = tipc_service_create(type, &nt->services[hash(type)]);
391 spin_lock_bh(&sc->lock);
392 p = tipc_service_insert_publ(net, sc, type, lower, upper,
393 scope, node, port, key);
394 spin_unlock_bh(&sc->lock);
398 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
399 u32 lower, u32 upper,
402 struct tipc_service *sc = tipc_service_find(net, type);
403 struct tipc_subscription *sub, *tmp;
404 struct service_range *sr = NULL;
405 struct publication *p = NULL;
411 spin_lock_bh(&sc->lock);
412 sr = tipc_service_find_range(sc, lower, upper);
415 p = tipc_service_remove_publ(sr, node, key);
419 /* Notify any waiting subscriptions */
420 last = list_empty(&sr->all_publ);
421 list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
422 tipc_sub_report_overlap(sub, lower, upper, TIPC_WITHDRAWN,
423 p->port, node, p->scope, last);
426 /* Remove service range item if this was its last publication */
427 if (list_empty(&sr->all_publ)) {
428 rb_erase(&sr->tree_node, &sc->ranges);
432 /* Delete service item if this no more publications and subscriptions */
433 if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
434 hlist_del_init_rcu(&sc->service_list);
438 spin_unlock_bh(&sc->lock);
443 * tipc_nametbl_translate - perform service instance to socket translation
445 * On entry, 'dnode' is the search domain used during translation.
448 * - if translation is deferred to another node, leave 'dnode' unchanged and
450 * - if translation is attempted and succeeds, set 'dnode' to the publishing
451 * node and return the published (non-zero) port number
452 * - if translation is attempted and fails, set 'dnode' to 0 and return 0
454 * Note that for legacy users (node configured with Z.C.N address format) the
455 * 'closest-first' lookup algorithm must be maintained, i.e., if dnode is 0
456 * we must look in the local binding list first
458 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *dnode)
460 struct tipc_net *tn = tipc_net(net);
461 bool legacy = tn->legacy_addr_format;
462 u32 self = tipc_own_addr(net);
463 struct service_range *sr;
464 struct tipc_service *sc;
465 struct list_head *list;
466 struct publication *p;
470 if (!tipc_in_scope(legacy, *dnode, self))
474 sc = tipc_service_find(net, type);
478 spin_lock_bh(&sc->lock);
479 sr = tipc_service_first_range(sc, instance);
483 /* Select lookup algorithm: local, closest-first or round-robin */
484 if (*dnode == self) {
485 list = &sr->local_publ;
486 if (list_empty(list))
488 p = list_first_entry(list, struct publication, local_publ);
489 list_move_tail(&p->local_publ, &sr->local_publ);
490 } else if (legacy && !*dnode && !list_empty(&sr->local_publ)) {
491 list = &sr->local_publ;
492 p = list_first_entry(list, struct publication, local_publ);
493 list_move_tail(&p->local_publ, &sr->local_publ);
495 list = &sr->all_publ;
496 p = list_first_entry(list, struct publication, all_publ);
497 list_move_tail(&p->all_publ, &sr->all_publ);
502 spin_unlock_bh(&sc->lock);
509 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
510 struct list_head *dsts, int *dstcnt, u32 exclude,
513 u32 self = tipc_own_addr(net);
514 struct service_range *sr;
515 struct tipc_service *sc;
516 struct publication *p;
520 sc = tipc_service_find(net, type);
524 spin_lock_bh(&sc->lock);
526 sr = tipc_service_first_range(sc, instance);
530 list_for_each_entry(p, &sr->all_publ, all_publ) {
531 if (p->scope != scope)
533 if (p->port == exclude && p->node == self)
535 tipc_dest_push(dsts, p->node, p->port);
539 list_move_tail(&p->all_publ, &sr->all_publ);
543 spin_unlock_bh(&sc->lock);
546 return !list_empty(dsts);
549 void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
550 u32 scope, bool exact, struct list_head *dports)
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->local_publ, local_publ) {
571 if (p->scope == scope || (!exact && p->scope < scope))
572 tipc_dest_push(dports, 0, p->port);
575 spin_unlock_bh(&sc->lock);
580 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
581 * - Creates list of nodes that overlap the given multicast address
582 * - Determines if any node local destinations overlap
584 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
585 u32 upper, struct tipc_nlist *nodes)
587 struct service_range *sr;
588 struct tipc_service *sc;
589 struct publication *p;
593 sc = tipc_service_find(net, type);
597 spin_lock_bh(&sc->lock);
599 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
600 sr = container_of(n, struct service_range, tree_node);
601 if (sr->upper < lower)
603 if (sr->lower > upper)
605 list_for_each_entry(p, &sr->all_publ, all_publ) {
606 tipc_nlist_add(nodes, p->node);
609 spin_unlock_bh(&sc->lock);
614 /* tipc_nametbl_build_group - build list of communication group members
616 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
619 struct service_range *sr;
620 struct tipc_service *sc;
621 struct publication *p;
625 sc = tipc_service_find(net, type);
629 spin_lock_bh(&sc->lock);
630 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
631 sr = container_of(n, struct service_range, tree_node);
632 list_for_each_entry(p, &sr->all_publ, all_publ) {
633 if (p->scope != scope)
635 tipc_group_add_member(grp, p->node, p->port, p->lower);
638 spin_unlock_bh(&sc->lock);
643 /* tipc_nametbl_publish - add service binding to name table
645 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
646 u32 upper, u32 scope, u32 port,
649 struct name_table *nt = tipc_name_table(net);
650 struct tipc_net *tn = tipc_net(net);
651 struct publication *p = NULL;
652 struct sk_buff *skb = NULL;
654 spin_lock_bh(&tn->nametbl_lock);
656 if (nt->local_publ_count >= TIPC_MAX_PUBL) {
657 pr_warn("Bind failed, max limit %u reached\n", TIPC_MAX_PUBL);
661 p = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
662 tipc_own_addr(net), port, key);
664 nt->local_publ_count++;
665 skb = tipc_named_publish(net, p);
668 spin_unlock_bh(&tn->nametbl_lock);
671 tipc_node_broadcast(net, skb);
676 * tipc_nametbl_withdraw - withdraw a service binding
678 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower,
681 struct name_table *nt = tipc_name_table(net);
682 struct tipc_net *tn = tipc_net(net);
683 u32 self = tipc_own_addr(net);
684 struct sk_buff *skb = NULL;
685 struct publication *p;
687 spin_lock_bh(&tn->nametbl_lock);
689 p = tipc_nametbl_remove_publ(net, type, lower, upper, self, key);
691 nt->local_publ_count--;
692 skb = tipc_named_withdraw(net, p);
693 list_del_init(&p->binding_sock);
696 pr_err("Failed to remove local publication {%u,%u,%u}/%u\n",
697 type, lower, upper, key);
699 spin_unlock_bh(&tn->nametbl_lock);
702 tipc_node_broadcast(net, skb);
709 * tipc_nametbl_subscribe - add a subscription object to the name table
711 bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
713 struct name_table *nt = tipc_name_table(sub->net);
714 struct tipc_net *tn = tipc_net(sub->net);
715 struct tipc_subscr *s = &sub->evt.s;
716 u32 type = tipc_sub_read(s, seq.type);
717 struct tipc_service *sc;
720 spin_lock_bh(&tn->nametbl_lock);
721 sc = tipc_service_find(sub->net, type);
723 sc = tipc_service_create(type, &nt->services[hash(type)]);
725 spin_lock_bh(&sc->lock);
726 tipc_service_subscribe(sc, sub);
727 spin_unlock_bh(&sc->lock);
729 pr_warn("Failed to subscribe for {%u,%u,%u}\n", type,
730 tipc_sub_read(s, seq.lower),
731 tipc_sub_read(s, seq.upper));
734 spin_unlock_bh(&tn->nametbl_lock);
739 * tipc_nametbl_unsubscribe - remove a subscription object from name table
741 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
743 struct tipc_net *tn = tipc_net(sub->net);
744 struct tipc_subscr *s = &sub->evt.s;
745 u32 type = tipc_sub_read(s, seq.type);
746 struct tipc_service *sc;
748 spin_lock_bh(&tn->nametbl_lock);
749 sc = tipc_service_find(sub->net, type);
753 spin_lock_bh(&sc->lock);
754 list_del_init(&sub->service_list);
757 /* Delete service item if no more publications and subscriptions */
758 if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
759 hlist_del_init_rcu(&sc->service_list);
762 spin_unlock_bh(&sc->lock);
764 spin_unlock_bh(&tn->nametbl_lock);
767 int tipc_nametbl_init(struct net *net)
769 struct tipc_net *tn = tipc_net(net);
770 struct name_table *nt;
773 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
777 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
778 INIT_HLIST_HEAD(&nt->services[i]);
780 INIT_LIST_HEAD(&nt->node_scope);
781 INIT_LIST_HEAD(&nt->cluster_scope);
782 rwlock_init(&nt->cluster_scope_lock);
784 spin_lock_init(&tn->nametbl_lock);
789 * tipc_service_delete - purge all publications for a service and delete it
791 static void tipc_service_delete(struct net *net, struct tipc_service *sc)
793 struct service_range *sr, *tmpr;
794 struct publication *p, *tmp;
796 spin_lock_bh(&sc->lock);
797 rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
798 list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
799 tipc_service_remove_publ(sr, p->node, p->key);
802 rb_erase(&sr->tree_node, &sc->ranges);
805 hlist_del_init_rcu(&sc->service_list);
806 spin_unlock_bh(&sc->lock);
810 void tipc_nametbl_stop(struct net *net)
812 struct name_table *nt = tipc_name_table(net);
813 struct tipc_net *tn = tipc_net(net);
814 struct hlist_head *service_head;
815 struct tipc_service *service;
818 /* Verify name table is empty and purge any lingering
819 * publications, then release the name table
821 spin_lock_bh(&tn->nametbl_lock);
822 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
823 if (hlist_empty(&nt->services[i]))
825 service_head = &nt->services[i];
826 hlist_for_each_entry_rcu(service, service_head, service_list) {
827 tipc_service_delete(net, service);
830 spin_unlock_bh(&tn->nametbl_lock);
836 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
837 struct tipc_service *service,
838 struct service_range *sr,
841 struct publication *p;
842 struct nlattr *attrs;
847 list_for_each_entry(p, &sr->all_publ, all_publ)
848 if (p->key == *last_key)
850 if (p->key != *last_key)
853 p = list_first_entry(&sr->all_publ,
858 list_for_each_entry_from(p, &sr->all_publ, all_publ) {
861 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
862 &tipc_genl_family, NLM_F_MULTI,
863 TIPC_NL_NAME_TABLE_GET);
867 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE);
871 b = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
875 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type))
877 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower))
879 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper))
881 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
883 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
885 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port))
887 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
890 nla_nest_end(msg->skb, b);
891 nla_nest_end(msg->skb, attrs);
892 genlmsg_end(msg->skb, hdr);
899 nla_nest_cancel(msg->skb, b);
901 nla_nest_cancel(msg->skb, attrs);
903 genlmsg_cancel(msg->skb, hdr);
908 static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg,
909 struct tipc_service *sc,
910 u32 *last_lower, u32 *last_key)
912 struct service_range *sr;
916 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
917 sr = container_of(n, struct service_range, tree_node);
918 if (sr->lower < *last_lower)
920 err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key);
922 *last_lower = sr->lower;
930 static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
931 u32 *last_type, u32 *last_lower, u32 *last_key)
933 struct tipc_net *tn = tipc_net(net);
934 struct tipc_service *service = NULL;
935 struct hlist_head *head;
940 i = hash(*last_type);
944 for (; i < TIPC_NAMETBL_SIZE; i++) {
945 head = &tn->nametbl->services[i];
948 (!i && *last_key && (*last_lower == *last_key))) {
949 service = tipc_service_find(net, *last_type);
953 hlist_for_each_entry_rcu(service, head, service_list)
959 hlist_for_each_entry_from_rcu(service, service_list) {
960 spin_lock_bh(&service->lock);
961 err = __tipc_nl_service_range_list(msg, service,
966 *last_type = service->type;
967 spin_unlock_bh(&service->lock);
970 spin_unlock_bh(&service->lock);
977 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
979 struct net *net = sock_net(skb->sk);
980 u32 last_type = cb->args[0];
981 u32 last_lower = cb->args[1];
982 u32 last_key = cb->args[2];
983 int done = cb->args[3];
984 struct tipc_nl_msg msg;
991 msg.portid = NETLINK_CB(cb->skb).portid;
992 msg.seq = cb->nlh->nlmsg_seq;
995 err = tipc_nl_service_list(net, &msg, &last_type,
996 &last_lower, &last_key);
999 } else if (err != -EMSGSIZE) {
1000 /* We never set seq or call nl_dump_check_consistent() this
1001 * means that setting prev_seq here will cause the consistence
1002 * check to fail in the netlink callback handler. Resulting in
1003 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1010 cb->args[0] = last_type;
1011 cb->args[1] = last_lower;
1012 cb->args[2] = last_key;
1018 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1020 struct tipc_dest *dst;
1022 list_for_each_entry(dst, l, list) {
1023 if (dst->node == node && dst->port == port)
1029 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1031 struct tipc_dest *dst;
1033 if (tipc_dest_find(l, node, port))
1036 dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1041 list_add(&dst->list, l);
1045 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1047 struct tipc_dest *dst;
1051 dst = list_first_entry(l, typeof(*dst), list);
1056 list_del(&dst->list);
1061 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1063 struct tipc_dest *dst;
1065 dst = tipc_dest_find(l, node, port);
1068 list_del(&dst->list);
1073 void tipc_dest_list_purge(struct list_head *l)
1075 struct tipc_dest *dst, *tmp;
1077 list_for_each_entry_safe(dst, tmp, l, list) {
1078 list_del(&dst->list);
1083 int tipc_dest_list_len(struct list_head *l)
1085 struct tipc_dest *dst;
1088 list_for_each_entry(dst, l, list) {