2  * net/tipc/name_table.c: TIPC name table code
 
   4  * Copyright (c) 2000-2006, 2014-2015, 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"
 
  45 #define TIPC_NAMETBL_SIZE 1024          /* must be a power of 2 */
 
  47 static const struct nla_policy
 
  48 tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = {
 
  49         [TIPC_NLA_NAME_TABLE_UNSPEC]    = { .type = NLA_UNSPEC },
 
  50         [TIPC_NLA_NAME_TABLE_PUBL]      = { .type = NLA_NESTED }
 
  54  * struct name_info - name sequence publication info
 
  55  * @node_list: circular list of publications made by own node
 
  56  * @cluster_list: circular list of publications made by own cluster
 
  57  * @zone_list: circular list of publications made by own zone
 
  58  * @node_list_size: number of entries in "node_list"
 
  59  * @cluster_list_size: number of entries in "cluster_list"
 
  60  * @zone_list_size: number of entries in "zone_list"
 
  62  * Note: The zone list always contains at least one entry, since all
 
  63  *       publications of the associated name sequence belong to it.
 
  64  *       (The cluster and node lists may be empty.)
 
  67         struct list_head node_list;
 
  68         struct list_head cluster_list;
 
  69         struct list_head zone_list;
 
  71         u32 cluster_list_size;
 
  76  * struct sub_seq - container for all published instances of a name sequence
 
  77  * @lower: name sequence lower bound
 
  78  * @upper: name sequence upper bound
 
  79  * @info: pointer to name sequence publication info
 
  84         struct name_info *info;
 
  88  * struct name_seq - container for all published instances of a name type
 
  89  * @type: 32 bit 'type' value for name sequence
 
  90  * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
 
  91  *        sub-sequences are sorted in ascending order
 
  92  * @alloc: number of sub-sequences currently in array
 
  93  * @first_free: array index of first unused sub-sequence entry
 
  94  * @ns_list: links to adjacent name sequences in hash chain
 
  95  * @subscriptions: list of subscriptions for this 'type'
 
  96  * @lock: spinlock controlling access to publication lists of all sub-sequences
 
  97  * @rcu: RCU callback head used for deferred freeing
 
 101         struct sub_seq *sseqs;
 
 104         struct hlist_node ns_list;
 
 105         struct list_head subscriptions;
 
 110 static int hash(int x)
 
 112         return x & (TIPC_NAMETBL_SIZE - 1);
 
 116  * publ_create - create a publication structure
 
 118 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
 
 119                                        u32 scope, u32 node, u32 port_ref,
 
 122         struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
 
 124                 pr_warn("Publication creation failure, no memory\n");
 
 133         publ->ref = port_ref;
 
 135         INIT_LIST_HEAD(&publ->pport_list);
 
 140  * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
 
 142 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
 
 144         return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
 
 148  * tipc_nameseq_create - create a name sequence structure for the specified 'type'
 
 150  * Allocates a single sub-sequence structure and sets it to all 0's.
 
 152 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
 
 154         struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
 
 155         struct sub_seq *sseq = tipc_subseq_alloc(1);
 
 157         if (!nseq || !sseq) {
 
 158                 pr_warn("Name sequence creation failed, no memory\n");
 
 164         spin_lock_init(&nseq->lock);
 
 168         INIT_HLIST_NODE(&nseq->ns_list);
 
 169         INIT_LIST_HEAD(&nseq->subscriptions);
 
 170         hlist_add_head_rcu(&nseq->ns_list, seq_head);
 
 175  * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
 
 177  * Very time-critical, so binary searches through sub-sequence array.
 
 179 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
 
 182         struct sub_seq *sseqs = nseq->sseqs;
 
 184         int high = nseq->first_free - 1;
 
 187         while (low <= high) {
 
 188                 mid = (low + high) / 2;
 
 189                 if (instance < sseqs[mid].lower)
 
 191                 else if (instance > sseqs[mid].upper)
 
 200  * nameseq_locate_subseq - determine position of name instance in sub-sequence
 
 202  * Returns index in sub-sequence array of the entry that contains the specified
 
 203  * instance value; if no entry contains that value, returns the position
 
 204  * where a new entry for it would be inserted in the array.
 
 206  * Note: Similar to binary search code for locating a sub-sequence.
 
 208 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
 
 210         struct sub_seq *sseqs = nseq->sseqs;
 
 212         int high = nseq->first_free - 1;
 
 215         while (low <= high) {
 
 216                 mid = (low + high) / 2;
 
 217                 if (instance < sseqs[mid].lower)
 
 219                 else if (instance > sseqs[mid].upper)
 
 228  * tipc_nameseq_insert_publ
 
 230 static struct publication *tipc_nameseq_insert_publ(struct net *net,
 
 231                                                     struct name_seq *nseq,
 
 233                                                     u32 upper, u32 scope,
 
 234                                                     u32 node, u32 port, u32 key)
 
 236         struct tipc_subscription *s;
 
 237         struct tipc_subscription *st;
 
 238         struct publication *publ;
 
 239         struct sub_seq *sseq;
 
 240         struct name_info *info;
 
 241         int created_subseq = 0;
 
 243         sseq = nameseq_find_subseq(nseq, lower);
 
 246                 /* Lower end overlaps existing entry => need an exact match */
 
 247                 if ((sseq->lower != lower) || (sseq->upper != upper)) {
 
 253                 /* Check if an identical publication already exists */
 
 254                 list_for_each_entry(publ, &info->zone_list, zone_list) {
 
 255                         if ((publ->ref == port) && (publ->key == key) &&
 
 256                             (!publ->node || (publ->node == node)))
 
 261                 struct sub_seq *freesseq;
 
 263                 /* Find where lower end should be inserted */
 
 264                 inspos = nameseq_locate_subseq(nseq, lower);
 
 266                 /* Fail if upper end overlaps into an existing entry */
 
 267                 if ((inspos < nseq->first_free) &&
 
 268                     (upper >= nseq->sseqs[inspos].lower)) {
 
 272                 /* Ensure there is space for new sub-sequence */
 
 273                 if (nseq->first_free == nseq->alloc) {
 
 274                         struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
 
 277                                 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
 
 281                         memcpy(sseqs, nseq->sseqs,
 
 282                                nseq->alloc * sizeof(struct sub_seq));
 
 288                 info = kzalloc(sizeof(*info), GFP_ATOMIC);
 
 290                         pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
 
 295                 INIT_LIST_HEAD(&info->node_list);
 
 296                 INIT_LIST_HEAD(&info->cluster_list);
 
 297                 INIT_LIST_HEAD(&info->zone_list);
 
 299                 /* Insert new sub-sequence */
 
 300                 sseq = &nseq->sseqs[inspos];
 
 301                 freesseq = &nseq->sseqs[nseq->first_free];
 
 302                 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
 
 303                 memset(sseq, 0, sizeof(*sseq));
 
 311         /* Insert a publication */
 
 312         publ = publ_create(type, lower, upper, scope, node, port, key);
 
 316         list_add(&publ->zone_list, &info->zone_list);
 
 317         info->zone_list_size++;
 
 319         if (in_own_cluster(net, node)) {
 
 320                 list_add(&publ->cluster_list, &info->cluster_list);
 
 321                 info->cluster_list_size++;
 
 324         if (in_own_node(net, node)) {
 
 325                 list_add(&publ->node_list, &info->node_list);
 
 326                 info->node_list_size++;
 
 329         /* Any subscriptions waiting for notification?  */
 
 330         list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
 
 331                 tipc_subscr_report_overlap(s,
 
 343  * tipc_nameseq_remove_publ
 
 345  * NOTE: There may be cases where TIPC is asked to remove a publication
 
 346  * that is not in the name table.  For example, if another node issues a
 
 347  * publication for a name sequence that overlaps an existing name sequence
 
 348  * the publication will not be recorded, which means the publication won't
 
 349  * be found when the name sequence is later withdrawn by that node.
 
 350  * A failed withdraw request simply returns a failure indication and lets the
 
 351  * caller issue any error or warning messages associated with such a problem.
 
 353 static struct publication *tipc_nameseq_remove_publ(struct net *net,
 
 354                                                     struct name_seq *nseq,
 
 358         struct publication *publ;
 
 359         struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
 
 360         struct name_info *info;
 
 361         struct sub_seq *free;
 
 362         struct tipc_subscription *s, *st;
 
 363         int removed_subseq = 0;
 
 370         /* Locate publication, if it exists */
 
 371         list_for_each_entry(publ, &info->zone_list, zone_list) {
 
 372                 if ((publ->key == key) && (publ->ref == ref) &&
 
 373                     (!publ->node || (publ->node == node)))
 
 379         /* Remove publication from zone scope list */
 
 380         list_del(&publ->zone_list);
 
 381         info->zone_list_size--;
 
 383         /* Remove publication from cluster scope list, if present */
 
 384         if (in_own_cluster(net, node)) {
 
 385                 list_del(&publ->cluster_list);
 
 386                 info->cluster_list_size--;
 
 389         /* Remove publication from node scope list, if present */
 
 390         if (in_own_node(net, node)) {
 
 391                 list_del(&publ->node_list);
 
 392                 info->node_list_size--;
 
 395         /* Contract subseq list if no more publications for that subseq */
 
 396         if (list_empty(&info->zone_list)) {
 
 398                 free = &nseq->sseqs[nseq->first_free--];
 
 399                 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
 
 403         /* Notify any waiting subscriptions */
 
 404         list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
 
 405                 tipc_subscr_report_overlap(s,
 
 418  * tipc_nameseq_subscribe - attach a subscription, and issue
 
 419  * the prescribed number of events if there is any sub-
 
 420  * sequence overlapping with the requested sequence
 
 422 static void tipc_nameseq_subscribe(struct name_seq *nseq,
 
 423                                    struct tipc_subscription *s)
 
 425         struct sub_seq *sseq = nseq->sseqs;
 
 427         list_add(&s->nameseq_list, &nseq->subscriptions);
 
 432         while (sseq != &nseq->sseqs[nseq->first_free]) {
 
 433                 if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
 
 434                         struct publication *crs;
 
 435                         struct name_info *info = sseq->info;
 
 438                         list_for_each_entry(crs, &info->zone_list, zone_list) {
 
 439                                 tipc_subscr_report_overlap(s,
 
 453 static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
 
 455         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
 456         struct hlist_head *seq_head;
 
 459         seq_head = &tn->nametbl->seq_hlist[hash(type)];
 
 460         hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
 
 461                 if (ns->type == type)
 
 468 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
 
 469                                              u32 lower, u32 upper, u32 scope,
 
 470                                              u32 node, u32 port, u32 key)
 
 472         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
 473         struct publication *publ;
 
 474         struct name_seq *seq = nametbl_find_seq(net, type);
 
 475         int index = hash(type);
 
 477         if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
 
 479                 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
 
 480                          type, lower, upper, scope);
 
 485                 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
 
 489         spin_lock_bh(&seq->lock);
 
 490         publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
 
 491                                         scope, node, port, key);
 
 492         spin_unlock_bh(&seq->lock);
 
 496 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
 
 497                                              u32 lower, u32 node, u32 ref,
 
 500         struct publication *publ;
 
 501         struct name_seq *seq = nametbl_find_seq(net, type);
 
 506         spin_lock_bh(&seq->lock);
 
 507         publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
 
 508         if (!seq->first_free && list_empty(&seq->subscriptions)) {
 
 509                 hlist_del_init_rcu(&seq->ns_list);
 
 511                 spin_unlock_bh(&seq->lock);
 
 515         spin_unlock_bh(&seq->lock);
 
 520  * tipc_nametbl_translate - perform name translation
 
 522  * On entry, 'destnode' is the search domain used during translation.
 
 525  * - if name translation is deferred to another node/cluster/zone,
 
 526  *   leaves 'destnode' unchanged (will be non-zero) and returns 0
 
 527  * - if name translation is attempted and succeeds, sets 'destnode'
 
 528  *   to publishing node and returns port reference (will be non-zero)
 
 529  * - if name translation is attempted and fails, sets 'destnode' to 0
 
 532 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
 
 535         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
 536         struct sub_seq *sseq;
 
 537         struct name_info *info;
 
 538         struct publication *publ;
 
 539         struct name_seq *seq;
 
 543         if (!tipc_in_scope(*destnode, tn->own_addr))
 
 547         seq = nametbl_find_seq(net, type);
 
 550         spin_lock_bh(&seq->lock);
 
 551         sseq = nameseq_find_subseq(seq, instance);
 
 556         /* Closest-First Algorithm */
 
 557         if (likely(!*destnode)) {
 
 558                 if (!list_empty(&info->node_list)) {
 
 559                         publ = list_first_entry(&info->node_list,
 
 562                         list_move_tail(&publ->node_list,
 
 564                 } else if (!list_empty(&info->cluster_list)) {
 
 565                         publ = list_first_entry(&info->cluster_list,
 
 568                         list_move_tail(&publ->cluster_list,
 
 569                                        &info->cluster_list);
 
 571                         publ = list_first_entry(&info->zone_list,
 
 574                         list_move_tail(&publ->zone_list,
 
 579         /* Round-Robin Algorithm */
 
 580         else if (*destnode == tn->own_addr) {
 
 581                 if (list_empty(&info->node_list))
 
 583                 publ = list_first_entry(&info->node_list, struct publication,
 
 585                 list_move_tail(&publ->node_list, &info->node_list);
 
 586         } else if (in_own_cluster_exact(net, *destnode)) {
 
 587                 if (list_empty(&info->cluster_list))
 
 589                 publ = list_first_entry(&info->cluster_list, struct publication,
 
 591                 list_move_tail(&publ->cluster_list, &info->cluster_list);
 
 593                 publ = list_first_entry(&info->zone_list, struct publication,
 
 595                 list_move_tail(&publ->zone_list, &info->zone_list);
 
 601         spin_unlock_bh(&seq->lock);
 
 609  * tipc_nametbl_mc_translate - find multicast destinations
 
 611  * Creates list of all local ports that overlap the given multicast address;
 
 612  * also determines if any off-node ports overlap.
 
 614  * Note: Publications with a scope narrower than 'limit' are ignored.
 
 615  * (i.e. local node-scope publications mustn't receive messages arriving
 
 616  * from another node, even if the multcast link brought it here)
 
 618  * Returns non-zero if any off-node ports overlap
 
 620 int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper,
 
 621                               u32 limit, struct tipc_plist *dports)
 
 623         struct name_seq *seq;
 
 624         struct sub_seq *sseq;
 
 625         struct sub_seq *sseq_stop;
 
 626         struct name_info *info;
 
 630         seq = nametbl_find_seq(net, type);
 
 634         spin_lock_bh(&seq->lock);
 
 635         sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
 
 636         sseq_stop = seq->sseqs + seq->first_free;
 
 637         for (; sseq != sseq_stop; sseq++) {
 
 638                 struct publication *publ;
 
 640                 if (sseq->lower > upper)
 
 644                 list_for_each_entry(publ, &info->node_list, node_list) {
 
 645                         if (publ->scope <= limit)
 
 646                                 tipc_plist_push(dports, publ->ref);
 
 649                 if (info->cluster_list_size != info->node_list_size)
 
 652         spin_unlock_bh(&seq->lock);
 
 659  * tipc_nametbl_publish - add name publication to network name tables
 
 661 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
 
 662                                          u32 upper, u32 scope, u32 port_ref,
 
 665         struct publication *publ;
 
 666         struct sk_buff *buf = NULL;
 
 667         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
 669         spin_lock_bh(&tn->nametbl_lock);
 
 670         if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
 
 671                 pr_warn("Publication failed, local publication limit reached (%u)\n",
 
 672                         TIPC_MAX_PUBLICATIONS);
 
 673                 spin_unlock_bh(&tn->nametbl_lock);
 
 677         publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
 
 678                                         tn->own_addr, port_ref, key);
 
 680                 tn->nametbl->local_publ_count++;
 
 681                 buf = tipc_named_publish(net, publ);
 
 682                 /* Any pending external events? */
 
 683                 tipc_named_process_backlog(net);
 
 685         spin_unlock_bh(&tn->nametbl_lock);
 
 688                 named_cluster_distribute(net, buf);
 
 693  * tipc_nametbl_withdraw - withdraw name publication from network name tables
 
 695 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
 
 698         struct publication *publ;
 
 699         struct sk_buff *skb = NULL;
 
 700         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
 702         spin_lock_bh(&tn->nametbl_lock);
 
 703         publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
 
 706                 tn->nametbl->local_publ_count--;
 
 707                 skb = tipc_named_withdraw(net, publ);
 
 708                 /* Any pending external events? */
 
 709                 tipc_named_process_backlog(net);
 
 710                 list_del_init(&publ->pport_list);
 
 711                 kfree_rcu(publ, rcu);
 
 713                 pr_err("Unable to remove local publication\n"
 
 714                        "(type=%u, lower=%u, ref=%u, key=%u)\n",
 
 715                        type, lower, ref, key);
 
 717         spin_unlock_bh(&tn->nametbl_lock);
 
 720                 named_cluster_distribute(net, skb);
 
 727  * tipc_nametbl_subscribe - add a subscription object to the name table
 
 729 void tipc_nametbl_subscribe(struct tipc_subscription *s)
 
 731         struct tipc_net *tn = net_generic(s->net, tipc_net_id);
 
 732         u32 type = s->seq.type;
 
 733         int index = hash(type);
 
 734         struct name_seq *seq;
 
 736         spin_lock_bh(&tn->nametbl_lock);
 
 737         seq = nametbl_find_seq(s->net, type);
 
 739                 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
 
 741                 spin_lock_bh(&seq->lock);
 
 742                 tipc_nameseq_subscribe(seq, s);
 
 743                 spin_unlock_bh(&seq->lock);
 
 745                 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
 
 746                         s->seq.type, s->seq.lower, s->seq.upper);
 
 748         spin_unlock_bh(&tn->nametbl_lock);
 
 752  * tipc_nametbl_unsubscribe - remove a subscription object from name table
 
 754 void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
 
 756         struct tipc_net *tn = net_generic(s->net, tipc_net_id);
 
 757         struct name_seq *seq;
 
 759         spin_lock_bh(&tn->nametbl_lock);
 
 760         seq = nametbl_find_seq(s->net, s->seq.type);
 
 762                 spin_lock_bh(&seq->lock);
 
 763                 list_del_init(&s->nameseq_list);
 
 764                 if (!seq->first_free && list_empty(&seq->subscriptions)) {
 
 765                         hlist_del_init_rcu(&seq->ns_list);
 
 767                         spin_unlock_bh(&seq->lock);
 
 770                         spin_unlock_bh(&seq->lock);
 
 773         spin_unlock_bh(&tn->nametbl_lock);
 
 777  * subseq_list - print specified sub-sequence contents into the given buffer
 
 779 static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth,
 
 783         const char *scope_str[] = {"", " zone", " cluster", " node"};
 
 784         struct publication *publ;
 
 785         struct name_info *info;
 
 788         ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper);
 
 791                 ret += tipc_snprintf(buf - ret, len + ret, "\n");
 
 797         list_for_each_entry(publ, &info->zone_list, zone_list) {
 
 798                 sprintf(portIdStr, "<%u.%u.%u:%u>",
 
 799                          tipc_zone(publ->node), tipc_cluster(publ->node),
 
 800                          tipc_node(publ->node), publ->ref);
 
 801                 ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr);
 
 803                         ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s",
 
 804                                              publ->key, scope_str[publ->scope]);
 
 806                 if (!list_is_last(&publ->zone_list, &info->zone_list))
 
 807                         ret += tipc_snprintf(buf + ret, len - ret,
 
 811         ret += tipc_snprintf(buf + ret, len - ret, "\n");
 
 816  * nameseq_list - print specified name sequence contents into the given buffer
 
 818 static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth,
 
 819                         u32 type, u32 lowbound, u32 upbound, u32 index)
 
 821         struct sub_seq *sseq;
 
 825         if (seq->first_free == 0)
 
 828         sprintf(typearea, "%-10u", seq->type);
 
 831                 ret += tipc_snprintf(buf, len, "%s\n", typearea);
 
 835         for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
 
 836                 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
 
 837                         ret += tipc_snprintf(buf + ret, len - ret, "%s ",
 
 839                         spin_lock_bh(&seq->lock);
 
 840                         ret += subseq_list(sseq, buf + ret, len - ret,
 
 842                         spin_unlock_bh(&seq->lock);
 
 843                         sprintf(typearea, "%10s", " ");
 
 850  * nametbl_header - print name table header into the given buffer
 
 852 static int nametbl_header(char *buf, int len, u32 depth)
 
 854         const char *header[] = {
 
 866         for (i = 0; i < depth; i++)
 
 867                 ret += tipc_snprintf(buf + ret, len - ret, header[i]);
 
 868         ret += tipc_snprintf(buf + ret, len - ret, "\n");
 
 873  * nametbl_list - print specified name table contents into the given buffer
 
 875 static int nametbl_list(struct net *net, char *buf, int len, u32 depth_info,
 
 876                         u32 type, u32 lowbound, u32 upbound)
 
 878         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
 879         struct hlist_head *seq_head;
 
 880         struct name_seq *seq;
 
 886         all_types = (depth_info & TIPC_NTQ_ALLTYPES);
 
 887         depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
 
 893                 /* display all entries in name table to specified depth */
 
 894                 ret += nametbl_header(buf, len, depth);
 
 897                 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
 
 898                         seq_head = &tn->nametbl->seq_hlist[i];
 
 899                         hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
 
 900                                 ret += nameseq_list(seq, buf + ret, len - ret,
 
 902                                                    lowbound, upbound, i);
 
 906                 /* display only the sequence that matches the specified type */
 
 907                 if (upbound < lowbound) {
 
 908                         ret += tipc_snprintf(buf + ret, len - ret,
 
 909                                         "invalid name sequence specified\n");
 
 912                 ret += nametbl_header(buf + ret, len - ret, depth);
 
 914                 seq_head = &tn->nametbl->seq_hlist[i];
 
 915                 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
 
 916                         if (seq->type == type) {
 
 917                                 ret += nameseq_list(seq, buf + ret, len - ret,
 
 919                                                    lowbound, upbound, i);
 
 927 struct sk_buff *tipc_nametbl_get(struct net *net, const void *req_tlv_area,
 
 931         struct tipc_name_table_query *argv;
 
 932         struct tlv_desc *rep_tlv;
 
 937         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
 
 938                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
 
 940         buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
 
 944         rep_tlv = (struct tlv_desc *)buf->data;
 
 945         pb = TLV_DATA(rep_tlv);
 
 946         pb_len = ULTRA_STRING_MAX_LEN;
 
 947         argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
 
 949         str_len = nametbl_list(net, pb, pb_len, ntohl(argv->depth),
 
 951                                ntohl(argv->lowbound), ntohl(argv->upbound));
 
 953         str_len += 1;   /* for "\0" */
 
 954         skb_put(buf, TLV_SPACE(str_len));
 
 955         TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
 
 960 int tipc_nametbl_init(struct net *net)
 
 962         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
 963         struct name_table *tipc_nametbl;
 
 966         tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
 
 970         for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
 
 971                 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
 
 973         INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
 
 974         INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
 
 975         INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
 
 976         tn->nametbl = tipc_nametbl;
 
 977         spin_lock_init(&tn->nametbl_lock);
 
 982  * tipc_purge_publications - remove all publications for a given type
 
 984  * tipc_nametbl_lock must be held when calling this function
 
 986 static void tipc_purge_publications(struct net *net, struct name_seq *seq)
 
 988         struct publication *publ, *safe;
 
 989         struct sub_seq *sseq;
 
 990         struct name_info *info;
 
 992         spin_lock_bh(&seq->lock);
 
 995         list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
 
 996                 tipc_nametbl_remove_publ(net, publ->type, publ->lower,
 
 997                                          publ->node, publ->ref, publ->key);
 
 998                 kfree_rcu(publ, rcu);
 
1000         hlist_del_init_rcu(&seq->ns_list);
 
1002         spin_unlock_bh(&seq->lock);
 
1004         kfree_rcu(seq, rcu);
 
1007 void tipc_nametbl_stop(struct net *net)
 
1010         struct name_seq *seq;
 
1011         struct hlist_head *seq_head;
 
1012         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
1013         struct name_table *tipc_nametbl = tn->nametbl;
 
1015         /* Verify name table is empty and purge any lingering
 
1016          * publications, then release the name table
 
1018         spin_lock_bh(&tn->nametbl_lock);
 
1019         for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
 
1020                 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
 
1022                 seq_head = &tipc_nametbl->seq_hlist[i];
 
1023                 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
 
1024                         tipc_purge_publications(net, seq);
 
1027         spin_unlock_bh(&tn->nametbl_lock);
 
1030         kfree(tipc_nametbl);
 
1034 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
 
1035                                         struct name_seq *seq,
 
1036                                         struct sub_seq *sseq, u32 *last_publ)
 
1039         struct nlattr *attrs;
 
1040         struct nlattr *publ;
 
1041         struct publication *p;
 
1044                 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
 
1045                         if (p->key == *last_publ)
 
1047                 if (p->key != *last_publ)
 
1050                 p = list_first_entry(&sseq->info->zone_list, struct publication,
 
1054         list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
 
1055                 *last_publ = p->key;
 
1057                 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
 
1058                                   &tipc_genl_family, NLM_F_MULTI,
 
1059                                   TIPC_NL_NAME_TABLE_GET);
 
1063                 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
 
1067                 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
 
1071                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
 
1073                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
 
1075                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
 
1077                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
 
1079                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
 
1081                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
 
1083                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
 
1086                 nla_nest_end(msg->skb, publ);
 
1087                 nla_nest_end(msg->skb, attrs);
 
1088                 genlmsg_end(msg->skb, hdr);
 
1095         nla_nest_cancel(msg->skb, publ);
 
1097         nla_nest_cancel(msg->skb, attrs);
 
1099         genlmsg_cancel(msg->skb, hdr);
 
1104 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
 
1105                                  u32 *last_lower, u32 *last_publ)
 
1107         struct sub_seq *sseq;
 
1108         struct sub_seq *sseq_start;
 
1112                 sseq_start = nameseq_find_subseq(seq, *last_lower);
 
1116                 sseq_start = seq->sseqs;
 
1119         for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
 
1120                 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
 
1122                         *last_lower = sseq->lower;
 
1131 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
 
1132                             u32 *last_type, u32 *last_lower, u32 *last_publ)
 
1134         struct tipc_net *tn = net_generic(net, tipc_net_id);
 
1135         struct hlist_head *seq_head;
 
1136         struct name_seq *seq = NULL;
 
1141                 i = hash(*last_type);
 
1145         for (; i < TIPC_NAMETBL_SIZE; i++) {
 
1146                 seq_head = &tn->nametbl->seq_hlist[i];
 
1149                         seq = nametbl_find_seq(net, *last_type);
 
1153                         hlist_for_each_entry_rcu(seq, seq_head, ns_list)
 
1159                 hlist_for_each_entry_from_rcu(seq, ns_list) {
 
1160                         spin_lock_bh(&seq->lock);
 
1161                         err = __tipc_nl_subseq_list(msg, seq, last_lower,
 
1165                                 *last_type = seq->type;
 
1166                                 spin_unlock_bh(&seq->lock);
 
1169                         spin_unlock_bh(&seq->lock);
 
1176 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
 
1179         int done = cb->args[3];
 
1180         u32 last_type = cb->args[0];
 
1181         u32 last_lower = cb->args[1];
 
1182         u32 last_publ = cb->args[2];
 
1183         struct net *net = sock_net(skb->sk);
 
1184         struct tipc_nl_msg msg;
 
1190         msg.portid = NETLINK_CB(cb->skb).portid;
 
1191         msg.seq = cb->nlh->nlmsg_seq;
 
1194         err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
 
1197         } else if (err != -EMSGSIZE) {
 
1198                 /* We never set seq or call nl_dump_check_consistent() this
 
1199                  * means that setting prev_seq here will cause the consistence
 
1200                  * check to fail in the netlink callback handler. Resulting in
 
1201                  * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
 
1208         cb->args[0] = last_type;
 
1209         cb->args[1] = last_lower;
 
1210         cb->args[2] = last_publ;
 
1216 void tipc_plist_push(struct tipc_plist *pl, u32 port)
 
1218         struct tipc_plist *nl;
 
1220         if (likely(!pl->port)) {
 
1224         if (pl->port == port)
 
1226         list_for_each_entry(nl, &pl->list, list) {
 
1227                 if (nl->port == port)
 
1230         nl = kmalloc(sizeof(*nl), GFP_ATOMIC);
 
1233                 list_add(&nl->list, &pl->list);
 
1237 u32 tipc_plist_pop(struct tipc_plist *pl)
 
1239         struct tipc_plist *nl;
 
1242         if (likely(list_empty(&pl->list))) {
 
1247         nl = list_first_entry(&pl->list, typeof(*nl), list);
 
1249         list_del(&nl->list);