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"
47 #include <net/genetlink.h>
49 #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
52 * struct name_info - name sequence publication info
53 * @node_list: list of publications on own node of this <type,lower,upper>
54 * @all_publ: list of all publications of this <type,lower,upper>
57 struct list_head local_publ;
58 struct list_head all_publ;
62 * struct sub_seq - container for all published instances of a name sequence
63 * @lower: name sequence lower bound
64 * @upper: name sequence upper bound
65 * @info: pointer to name sequence publication info
70 struct name_info *info;
74 * struct name_seq - container for all published instances of a name type
75 * @type: 32 bit 'type' value for name sequence
76 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
77 * sub-sequences are sorted in ascending order
78 * @alloc: number of sub-sequences currently in array
79 * @first_free: array index of first unused sub-sequence entry
80 * @ns_list: links to adjacent name sequences in hash chain
81 * @subscriptions: list of subscriptions for this 'type'
82 * @lock: spinlock controlling access to publication lists of all sub-sequences
83 * @rcu: RCU callback head used for deferred freeing
87 struct sub_seq *sseqs;
90 struct hlist_node ns_list;
91 struct list_head subscriptions;
96 static int hash(int x)
98 return x & (TIPC_NAMETBL_SIZE - 1);
102 * publ_create - create a publication structure
104 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
105 u32 scope, u32 node, u32 port,
108 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
110 pr_warn("Publication creation failure, no memory\n");
121 INIT_LIST_HEAD(&publ->binding_sock);
126 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
128 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
130 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
134 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
136 * Allocates a single sub-sequence structure and sets it to all 0's.
138 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
140 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
141 struct sub_seq *sseq = tipc_subseq_alloc(1);
143 if (!nseq || !sseq) {
144 pr_warn("Name sequence creation failed, no memory\n");
150 spin_lock_init(&nseq->lock);
154 INIT_HLIST_NODE(&nseq->ns_list);
155 INIT_LIST_HEAD(&nseq->subscriptions);
156 hlist_add_head_rcu(&nseq->ns_list, seq_head);
161 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
163 * Very time-critical, so binary searches through sub-sequence array.
165 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
168 struct sub_seq *sseqs = nseq->sseqs;
170 int high = nseq->first_free - 1;
173 while (low <= high) {
174 mid = (low + high) / 2;
175 if (instance < sseqs[mid].lower)
177 else if (instance > sseqs[mid].upper)
186 * nameseq_locate_subseq - determine position of name instance in sub-sequence
188 * Returns index in sub-sequence array of the entry that contains the specified
189 * instance value; if no entry contains that value, returns the position
190 * where a new entry for it would be inserted in the array.
192 * Note: Similar to binary search code for locating a sub-sequence.
194 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
196 struct sub_seq *sseqs = nseq->sseqs;
198 int high = nseq->first_free - 1;
201 while (low <= high) {
202 mid = (low + high) / 2;
203 if (instance < sseqs[mid].lower)
205 else if (instance > sseqs[mid].upper)
214 * tipc_nameseq_insert_publ
216 static struct publication *tipc_nameseq_insert_publ(struct net *net,
217 struct name_seq *nseq,
219 u32 upper, u32 scope,
220 u32 node, u32 port, u32 key)
222 struct tipc_subscription *s;
223 struct tipc_subscription *st;
224 struct publication *publ;
225 struct sub_seq *sseq;
226 struct name_info *info;
227 int created_subseq = 0;
229 sseq = nameseq_find_subseq(nseq, lower);
232 /* Lower end overlaps existing entry => need an exact match */
233 if ((sseq->lower != lower) || (sseq->upper != upper)) {
239 /* Check if an identical publication already exists */
240 list_for_each_entry(publ, &info->all_publ, all_publ) {
241 if (publ->port == port && publ->key == key &&
242 (!publ->node || publ->node == node))
247 struct sub_seq *freesseq;
249 /* Find where lower end should be inserted */
250 inspos = nameseq_locate_subseq(nseq, lower);
252 /* Fail if upper end overlaps into an existing entry */
253 if ((inspos < nseq->first_free) &&
254 (upper >= nseq->sseqs[inspos].lower)) {
258 /* Ensure there is space for new sub-sequence */
259 if (nseq->first_free == nseq->alloc) {
260 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
263 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
267 memcpy(sseqs, nseq->sseqs,
268 nseq->alloc * sizeof(struct sub_seq));
274 info = kzalloc(sizeof(*info), GFP_ATOMIC);
276 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
281 INIT_LIST_HEAD(&info->local_publ);
282 INIT_LIST_HEAD(&info->all_publ);
284 /* Insert new sub-sequence */
285 sseq = &nseq->sseqs[inspos];
286 freesseq = &nseq->sseqs[nseq->first_free];
287 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
288 memset(sseq, 0, sizeof(*sseq));
296 /* Insert a publication */
297 publ = publ_create(type, lower, upper, scope, node, port, key);
301 list_add(&publ->all_publ, &info->all_publ);
303 if (in_own_node(net, node))
304 list_add(&publ->local_publ, &info->local_publ);
306 /* Any subscriptions waiting for notification? */
307 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
308 tipc_sub_report_overlap(s, publ->lower, publ->upper,
309 TIPC_PUBLISHED, publ->port,
310 publ->node, publ->scope,
317 * tipc_nameseq_remove_publ
319 * NOTE: There may be cases where TIPC is asked to remove a publication
320 * that is not in the name table. For example, if another node issues a
321 * publication for a name sequence that overlaps an existing name sequence
322 * the publication will not be recorded, which means the publication won't
323 * be found when the name sequence is later withdrawn by that node.
324 * A failed withdraw request simply returns a failure indication and lets the
325 * caller issue any error or warning messages associated with such a problem.
327 static struct publication *tipc_nameseq_remove_publ(struct net *net,
328 struct name_seq *nseq,
332 struct publication *publ;
333 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
334 struct name_info *info;
335 struct sub_seq *free;
336 struct tipc_subscription *s, *st;
337 int removed_subseq = 0;
344 /* Locate publication, if it exists */
345 list_for_each_entry(publ, &info->all_publ, all_publ) {
346 if (publ->key == key && publ->port == port &&
347 (!publ->node || publ->node == node))
353 list_del(&publ->all_publ);
354 if (in_own_node(net, node))
355 list_del(&publ->local_publ);
357 /* Contract subseq list if no more publications for that subseq */
358 if (list_empty(&info->all_publ)) {
360 free = &nseq->sseqs[nseq->first_free--];
361 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
365 /* Notify any waiting subscriptions */
366 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
367 tipc_sub_report_overlap(s, publ->lower, publ->upper,
368 TIPC_WITHDRAWN, publ->port,
369 publ->node, publ->scope,
377 * tipc_nameseq_subscribe - attach a subscription, and optionally
378 * issue the prescribed number of events if there is any sub-
379 * sequence overlapping with the requested sequence
381 static void tipc_nameseq_subscribe(struct name_seq *nseq,
382 struct tipc_subscription *sub)
384 struct sub_seq *sseq = nseq->sseqs;
385 struct tipc_name_seq ns;
386 struct tipc_subscr *s = &sub->evt.s;
389 ns.type = tipc_sub_read(s, seq.type);
390 ns.lower = tipc_sub_read(s, seq.lower);
391 ns.upper = tipc_sub_read(s, seq.upper);
392 no_status = tipc_sub_read(s, filter) & TIPC_SUB_NO_STATUS;
395 list_add(&sub->nameseq_list, &nseq->subscriptions);
397 if (no_status || !sseq)
400 while (sseq != &nseq->sseqs[nseq->first_free]) {
401 if (tipc_sub_check_overlap(&ns, sseq->lower, sseq->upper)) {
402 struct publication *crs;
403 struct name_info *info = sseq->info;
406 list_for_each_entry(crs, &info->all_publ, all_publ) {
407 tipc_sub_report_overlap(sub, sseq->lower,
421 static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
423 struct tipc_net *tn = net_generic(net, tipc_net_id);
424 struct hlist_head *seq_head;
427 seq_head = &tn->nametbl->seq_hlist[hash(type)];
428 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
429 if (ns->type == type)
436 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
437 u32 lower, u32 upper, u32 scope,
438 u32 node, u32 port, u32 key)
440 struct tipc_net *tn = net_generic(net, tipc_net_id);
441 struct publication *publ;
442 struct name_seq *seq = nametbl_find_seq(net, type);
443 int index = hash(type);
445 if (scope > TIPC_NODE_SCOPE || lower > upper) {
446 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
447 type, lower, upper, scope);
452 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
456 spin_lock_bh(&seq->lock);
457 publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
458 scope, node, port, key);
459 spin_unlock_bh(&seq->lock);
463 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
464 u32 lower, u32 node, u32 port,
467 struct publication *publ;
468 struct name_seq *seq = nametbl_find_seq(net, type);
473 spin_lock_bh(&seq->lock);
474 publ = tipc_nameseq_remove_publ(net, seq, lower, node, port, key);
475 if (!seq->first_free && list_empty(&seq->subscriptions)) {
476 hlist_del_init_rcu(&seq->ns_list);
478 spin_unlock_bh(&seq->lock);
482 spin_unlock_bh(&seq->lock);
487 * tipc_nametbl_translate - perform name translation
489 * On entry, 'destnode' is the search domain used during translation.
492 * - if name translation is deferred to another node/cluster/zone,
493 * leaves 'destnode' unchanged (will be non-zero) and returns 0
494 * - if name translation is attempted and succeeds, sets 'destnode'
495 * to publishing node and returns port reference (will be non-zero)
496 * - if name translation is attempted and fails, sets 'destnode' to 0
499 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
502 struct tipc_net *tn = tipc_net(net);
503 bool legacy = tn->legacy_addr_format;
504 u32 self = tipc_own_addr(net);
505 struct sub_seq *sseq;
506 struct name_info *info;
507 struct publication *publ;
508 struct name_seq *seq;
512 if (!tipc_in_scope(legacy, *destnode, self))
516 seq = nametbl_find_seq(net, type);
519 spin_lock_bh(&seq->lock);
520 sseq = nameseq_find_subseq(seq, instance);
525 /* Closest-First Algorithm */
526 if (legacy && !*destnode) {
527 if (!list_empty(&info->local_publ)) {
528 publ = list_first_entry(&info->local_publ,
531 list_move_tail(&publ->local_publ,
534 publ = list_first_entry(&info->all_publ,
537 list_move_tail(&publ->all_publ,
542 /* Round-Robin Algorithm */
543 else if (*destnode == tipc_own_addr(net)) {
544 if (list_empty(&info->local_publ))
546 publ = list_first_entry(&info->local_publ, struct publication,
548 list_move_tail(&publ->local_publ, &info->local_publ);
550 publ = list_first_entry(&info->all_publ, struct publication,
552 list_move_tail(&publ->all_publ, &info->all_publ);
558 spin_unlock_bh(&seq->lock);
565 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
566 struct list_head *dsts, int *dstcnt, u32 exclude,
569 u32 self = tipc_own_addr(net);
570 struct publication *publ;
571 struct name_info *info;
572 struct name_seq *seq;
573 struct sub_seq *sseq;
577 seq = nametbl_find_seq(net, type);
580 spin_lock_bh(&seq->lock);
581 sseq = nameseq_find_subseq(seq, instance);
584 list_for_each_entry(publ, &info->all_publ, all_publ) {
585 if (publ->scope != scope)
587 if (publ->port == exclude && publ->node == self)
589 tipc_dest_push(dsts, publ->node, publ->port);
593 list_move_tail(&publ->all_publ, &info->all_publ);
597 spin_unlock_bh(&seq->lock);
600 return !list_empty(dsts);
603 void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
604 u32 scope, bool exact, struct list_head *dports)
606 struct sub_seq *sseq_stop;
607 struct name_info *info;
608 struct publication *p;
609 struct name_seq *seq;
610 struct sub_seq *sseq;
613 seq = nametbl_find_seq(net, type);
617 spin_lock_bh(&seq->lock);
618 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
619 sseq_stop = seq->sseqs + seq->first_free;
620 for (; sseq != sseq_stop; sseq++) {
621 if (sseq->lower > upper)
624 list_for_each_entry(p, &info->local_publ, local_publ) {
625 if (p->scope == scope || (!exact && p->scope < scope))
626 tipc_dest_push(dports, 0, p->port);
629 spin_unlock_bh(&seq->lock);
634 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
635 * - Creates list of nodes that overlap the given multicast address
636 * - Determines if any node local ports overlap
638 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
639 u32 upper, struct tipc_nlist *nodes)
641 struct sub_seq *sseq, *stop;
642 struct publication *publ;
643 struct name_info *info;
644 struct name_seq *seq;
647 seq = nametbl_find_seq(net, type);
651 spin_lock_bh(&seq->lock);
652 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
653 stop = seq->sseqs + seq->first_free;
654 for (; sseq != stop && sseq->lower <= upper; sseq++) {
656 list_for_each_entry(publ, &info->all_publ, all_publ) {
657 tipc_nlist_add(nodes, publ->node);
660 spin_unlock_bh(&seq->lock);
665 /* tipc_nametbl_build_group - build list of communication group members
667 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
670 struct sub_seq *sseq, *stop;
671 struct name_info *info;
672 struct publication *p;
673 struct name_seq *seq;
676 seq = nametbl_find_seq(net, type);
680 spin_lock_bh(&seq->lock);
682 stop = seq->sseqs + seq->first_free;
683 for (; sseq != stop; sseq++) {
685 list_for_each_entry(p, &info->all_publ, all_publ) {
686 if (p->scope != scope)
688 tipc_group_add_member(grp, p->node, p->port, p->lower);
691 spin_unlock_bh(&seq->lock);
697 * tipc_nametbl_publish - add name publication to network name tables
699 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
700 u32 upper, u32 scope, u32 port_ref,
703 struct publication *publ;
704 struct sk_buff *buf = NULL;
705 struct tipc_net *tn = net_generic(net, tipc_net_id);
707 spin_lock_bh(&tn->nametbl_lock);
708 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
709 pr_warn("Publication failed, local publication limit reached (%u)\n",
710 TIPC_MAX_PUBLICATIONS);
711 spin_unlock_bh(&tn->nametbl_lock);
715 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
716 tipc_own_addr(net), port_ref, key);
718 tn->nametbl->local_publ_count++;
719 buf = tipc_named_publish(net, publ);
720 /* Any pending external events? */
721 tipc_named_process_backlog(net);
723 spin_unlock_bh(&tn->nametbl_lock);
726 tipc_node_broadcast(net, buf);
731 * tipc_nametbl_withdraw - withdraw name publication from network name tables
733 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 port,
736 struct publication *publ;
737 struct sk_buff *skb = NULL;
738 struct tipc_net *tn = net_generic(net, tipc_net_id);
740 spin_lock_bh(&tn->nametbl_lock);
741 publ = tipc_nametbl_remove_publ(net, type, lower, tipc_own_addr(net),
744 tn->nametbl->local_publ_count--;
745 skb = tipc_named_withdraw(net, publ);
746 /* Any pending external events? */
747 tipc_named_process_backlog(net);
748 list_del_init(&publ->binding_sock);
749 kfree_rcu(publ, rcu);
751 pr_err("Unable to remove local publication\n"
752 "(type=%u, lower=%u, port=%u, key=%u)\n",
753 type, lower, port, key);
755 spin_unlock_bh(&tn->nametbl_lock);
758 tipc_node_broadcast(net, skb);
765 * tipc_nametbl_subscribe - add a subscription object to the name table
767 void tipc_nametbl_subscribe(struct tipc_subscription *sub)
769 struct tipc_net *tn = tipc_net(sub->net);
770 struct tipc_subscr *s = &sub->evt.s;
771 u32 type = tipc_sub_read(s, seq.type);
772 int index = hash(type);
773 struct name_seq *seq;
774 struct tipc_name_seq ns;
776 spin_lock_bh(&tn->nametbl_lock);
777 seq = nametbl_find_seq(sub->net, type);
779 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
781 spin_lock_bh(&seq->lock);
782 tipc_nameseq_subscribe(seq, sub);
783 spin_unlock_bh(&seq->lock);
785 ns.type = tipc_sub_read(s, seq.type);
786 ns.lower = tipc_sub_read(s, seq.lower);
787 ns.upper = tipc_sub_read(s, seq.upper);
788 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
789 ns.type, ns.lower, ns.upper);
791 spin_unlock_bh(&tn->nametbl_lock);
795 * tipc_nametbl_unsubscribe - remove a subscription object from name table
797 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
799 struct tipc_subscr *s = &sub->evt.s;
800 struct tipc_net *tn = tipc_net(sub->net);
801 struct name_seq *seq;
802 u32 type = tipc_sub_read(s, seq.type);
804 spin_lock_bh(&tn->nametbl_lock);
805 seq = nametbl_find_seq(sub->net, type);
807 spin_lock_bh(&seq->lock);
808 list_del_init(&sub->nameseq_list);
810 if (!seq->first_free && list_empty(&seq->subscriptions)) {
811 hlist_del_init_rcu(&seq->ns_list);
813 spin_unlock_bh(&seq->lock);
816 spin_unlock_bh(&seq->lock);
819 spin_unlock_bh(&tn->nametbl_lock);
822 int tipc_nametbl_init(struct net *net)
824 struct tipc_net *tn = net_generic(net, tipc_net_id);
825 struct name_table *tipc_nametbl;
828 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
832 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
833 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
835 INIT_LIST_HEAD(&tipc_nametbl->node_scope);
836 INIT_LIST_HEAD(&tipc_nametbl->cluster_scope);
837 tn->nametbl = tipc_nametbl;
838 spin_lock_init(&tn->nametbl_lock);
843 * tipc_purge_publications - remove all publications for a given type
845 * tipc_nametbl_lock must be held when calling this function
847 static void tipc_purge_publications(struct net *net, struct name_seq *seq)
849 struct publication *publ, *safe;
850 struct sub_seq *sseq;
851 struct name_info *info;
853 spin_lock_bh(&seq->lock);
856 list_for_each_entry_safe(publ, safe, &info->all_publ, all_publ) {
857 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
858 publ->port, publ->key);
859 kfree_rcu(publ, rcu);
861 hlist_del_init_rcu(&seq->ns_list);
863 spin_unlock_bh(&seq->lock);
868 void tipc_nametbl_stop(struct net *net)
871 struct name_seq *seq;
872 struct hlist_head *seq_head;
873 struct tipc_net *tn = net_generic(net, tipc_net_id);
874 struct name_table *tipc_nametbl = tn->nametbl;
876 /* Verify name table is empty and purge any lingering
877 * publications, then release the name table
879 spin_lock_bh(&tn->nametbl_lock);
880 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
881 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
883 seq_head = &tipc_nametbl->seq_hlist[i];
884 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
885 tipc_purge_publications(net, seq);
888 spin_unlock_bh(&tn->nametbl_lock);
895 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
896 struct name_seq *seq,
897 struct sub_seq *sseq, u32 *last_publ)
900 struct nlattr *attrs;
902 struct publication *p;
905 list_for_each_entry(p, &sseq->info->all_publ, all_publ)
906 if (p->key == *last_publ)
908 if (p->key != *last_publ)
911 p = list_first_entry(&sseq->info->all_publ, struct publication,
915 list_for_each_entry_from(p, &sseq->info->all_publ, all_publ) {
918 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
919 &tipc_genl_family, NLM_F_MULTI,
920 TIPC_NL_NAME_TABLE_GET);
924 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
928 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
932 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
934 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
936 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
938 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
940 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
942 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port))
944 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
947 nla_nest_end(msg->skb, publ);
948 nla_nest_end(msg->skb, attrs);
949 genlmsg_end(msg->skb, hdr);
956 nla_nest_cancel(msg->skb, publ);
958 nla_nest_cancel(msg->skb, attrs);
960 genlmsg_cancel(msg->skb, hdr);
965 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
966 u32 *last_lower, u32 *last_publ)
968 struct sub_seq *sseq;
969 struct sub_seq *sseq_start;
973 sseq_start = nameseq_find_subseq(seq, *last_lower);
977 sseq_start = seq->sseqs;
980 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
981 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
983 *last_lower = sseq->lower;
992 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
993 u32 *last_type, u32 *last_lower, u32 *last_publ)
995 struct tipc_net *tn = net_generic(net, tipc_net_id);
996 struct hlist_head *seq_head;
997 struct name_seq *seq = NULL;
1002 i = hash(*last_type);
1006 for (; i < TIPC_NAMETBL_SIZE; i++) {
1007 seq_head = &tn->nametbl->seq_hlist[i];
1010 seq = nametbl_find_seq(net, *last_type);
1014 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1020 hlist_for_each_entry_from_rcu(seq, ns_list) {
1021 spin_lock_bh(&seq->lock);
1022 err = __tipc_nl_subseq_list(msg, seq, last_lower,
1026 *last_type = seq->type;
1027 spin_unlock_bh(&seq->lock);
1030 spin_unlock_bh(&seq->lock);
1037 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1040 int done = cb->args[3];
1041 u32 last_type = cb->args[0];
1042 u32 last_lower = cb->args[1];
1043 u32 last_publ = cb->args[2];
1044 struct net *net = sock_net(skb->sk);
1045 struct tipc_nl_msg msg;
1051 msg.portid = NETLINK_CB(cb->skb).portid;
1052 msg.seq = cb->nlh->nlmsg_seq;
1055 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
1058 } else if (err != -EMSGSIZE) {
1059 /* We never set seq or call nl_dump_check_consistent() this
1060 * means that setting prev_seq here will cause the consistence
1061 * check to fail in the netlink callback handler. Resulting in
1062 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1069 cb->args[0] = last_type;
1070 cb->args[1] = last_lower;
1071 cb->args[2] = last_publ;
1077 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1079 u64 value = (u64)node << 32 | port;
1080 struct tipc_dest *dst;
1082 list_for_each_entry(dst, l, list) {
1083 if (dst->value != value)
1090 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1092 u64 value = (u64)node << 32 | port;
1093 struct tipc_dest *dst;
1095 if (tipc_dest_find(l, node, port))
1098 dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1102 list_add(&dst->list, l);
1106 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1108 struct tipc_dest *dst;
1112 dst = list_first_entry(l, typeof(*dst), list);
1117 list_del(&dst->list);
1122 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1124 struct tipc_dest *dst;
1126 dst = tipc_dest_find(l, node, port);
1129 list_del(&dst->list);
1134 void tipc_dest_list_purge(struct list_head *l)
1136 struct tipc_dest *dst, *tmp;
1138 list_for_each_entry_safe(dst, tmp, l, list) {
1139 list_del(&dst->list);
1144 int tipc_dest_list_len(struct list_head *l)
1146 struct tipc_dest *dst;
1149 list_for_each_entry(dst, l, list) {