2fbd0a20a9586981aa0929f52824763b07f9490f
[linux-2.6-microblaze.git] / net / tipc / name_table.c
1 /*
2  * net/tipc/name_table.c: TIPC name table code
3  *
4  * Copyright (c) 2000-2006, 2014-2015, Ericsson AB
5  * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
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.
19  *
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.
23  *
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.
35  */
36
37 #include <net/sock.h>
38 #include "core.h"
39 #include "netlink.h"
40 #include "name_table.h"
41 #include "name_distr.h"
42 #include "subscr.h"
43 #include "bcast.h"
44 #include "addr.h"
45 #include "node.h"
46 #include "group.h"
47 #include <net/genetlink.h>
48
49 #define TIPC_NAMETBL_SIZE 1024          /* must be a power of 2 */
50
51 /**
52  * struct name_info - name sequence publication info
53  * @node_list: circular list of publications made by own node
54  * @cluster_list: circular list of publications made by own cluster
55  * @zone_list: circular list of publications made by own zone
56  * @node_list_size: number of entries in "node_list"
57  * @cluster_list_size: number of entries in "cluster_list"
58  * @zone_list_size: number of entries in "zone_list"
59  *
60  * Note: The zone list always contains at least one entry, since all
61  *       publications of the associated name sequence belong to it.
62  *       (The cluster and node lists may be empty.)
63  */
64 struct name_info {
65         struct list_head node_list;
66         struct list_head cluster_list;
67         struct list_head zone_list;
68         u32 node_list_size;
69         u32 cluster_list_size;
70         u32 zone_list_size;
71 };
72
73 /**
74  * struct sub_seq - container for all published instances of a name sequence
75  * @lower: name sequence lower bound
76  * @upper: name sequence upper bound
77  * @info: pointer to name sequence publication info
78  */
79 struct sub_seq {
80         u32 lower;
81         u32 upper;
82         struct name_info *info;
83 };
84
85 /**
86  * struct name_seq - container for all published instances of a name type
87  * @type: 32 bit 'type' value for name sequence
88  * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
89  *        sub-sequences are sorted in ascending order
90  * @alloc: number of sub-sequences currently in array
91  * @first_free: array index of first unused sub-sequence entry
92  * @ns_list: links to adjacent name sequences in hash chain
93  * @subscriptions: list of subscriptions for this 'type'
94  * @lock: spinlock controlling access to publication lists of all sub-sequences
95  * @rcu: RCU callback head used for deferred freeing
96  */
97 struct name_seq {
98         u32 type;
99         struct sub_seq *sseqs;
100         u32 alloc;
101         u32 first_free;
102         struct hlist_node ns_list;
103         struct list_head subscriptions;
104         spinlock_t lock;
105         struct rcu_head rcu;
106 };
107
108 static int hash(int x)
109 {
110         return x & (TIPC_NAMETBL_SIZE - 1);
111 }
112
113 /**
114  * publ_create - create a publication structure
115  */
116 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
117                                        u32 scope, u32 node, u32 port_ref,
118                                        u32 key)
119 {
120         struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
121         if (publ == NULL) {
122                 pr_warn("Publication creation failure, no memory\n");
123                 return NULL;
124         }
125
126         publ->type = type;
127         publ->lower = lower;
128         publ->upper = upper;
129         publ->scope = scope;
130         publ->node = node;
131         publ->ref = port_ref;
132         publ->key = key;
133         INIT_LIST_HEAD(&publ->pport_list);
134         return publ;
135 }
136
137 /**
138  * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
139  */
140 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
141 {
142         return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
143 }
144
145 /**
146  * tipc_nameseq_create - create a name sequence structure for the specified 'type'
147  *
148  * Allocates a single sub-sequence structure and sets it to all 0's.
149  */
150 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
151 {
152         struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
153         struct sub_seq *sseq = tipc_subseq_alloc(1);
154
155         if (!nseq || !sseq) {
156                 pr_warn("Name sequence creation failed, no memory\n");
157                 kfree(nseq);
158                 kfree(sseq);
159                 return NULL;
160         }
161
162         spin_lock_init(&nseq->lock);
163         nseq->type = type;
164         nseq->sseqs = sseq;
165         nseq->alloc = 1;
166         INIT_HLIST_NODE(&nseq->ns_list);
167         INIT_LIST_HEAD(&nseq->subscriptions);
168         hlist_add_head_rcu(&nseq->ns_list, seq_head);
169         return nseq;
170 }
171
172 /**
173  * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
174  *
175  * Very time-critical, so binary searches through sub-sequence array.
176  */
177 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
178                                            u32 instance)
179 {
180         struct sub_seq *sseqs = nseq->sseqs;
181         int low = 0;
182         int high = nseq->first_free - 1;
183         int mid;
184
185         while (low <= high) {
186                 mid = (low + high) / 2;
187                 if (instance < sseqs[mid].lower)
188                         high = mid - 1;
189                 else if (instance > sseqs[mid].upper)
190                         low = mid + 1;
191                 else
192                         return &sseqs[mid];
193         }
194         return NULL;
195 }
196
197 /**
198  * nameseq_locate_subseq - determine position of name instance in sub-sequence
199  *
200  * Returns index in sub-sequence array of the entry that contains the specified
201  * instance value; if no entry contains that value, returns the position
202  * where a new entry for it would be inserted in the array.
203  *
204  * Note: Similar to binary search code for locating a sub-sequence.
205  */
206 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
207 {
208         struct sub_seq *sseqs = nseq->sseqs;
209         int low = 0;
210         int high = nseq->first_free - 1;
211         int mid;
212
213         while (low <= high) {
214                 mid = (low + high) / 2;
215                 if (instance < sseqs[mid].lower)
216                         high = mid - 1;
217                 else if (instance > sseqs[mid].upper)
218                         low = mid + 1;
219                 else
220                         return mid;
221         }
222         return low;
223 }
224
225 /**
226  * tipc_nameseq_insert_publ
227  */
228 static struct publication *tipc_nameseq_insert_publ(struct net *net,
229                                                     struct name_seq *nseq,
230                                                     u32 type, u32 lower,
231                                                     u32 upper, u32 scope,
232                                                     u32 node, u32 port, u32 key)
233 {
234         struct tipc_subscription *s;
235         struct tipc_subscription *st;
236         struct publication *publ;
237         struct sub_seq *sseq;
238         struct name_info *info;
239         int created_subseq = 0;
240
241         sseq = nameseq_find_subseq(nseq, lower);
242         if (sseq) {
243
244                 /* Lower end overlaps existing entry => need an exact match */
245                 if ((sseq->lower != lower) || (sseq->upper != upper)) {
246                         return NULL;
247                 }
248
249                 info = sseq->info;
250
251                 /* Check if an identical publication already exists */
252                 list_for_each_entry(publ, &info->zone_list, zone_list) {
253                         if ((publ->ref == port) && (publ->key == key) &&
254                             (!publ->node || (publ->node == node)))
255                                 return NULL;
256                 }
257         } else {
258                 u32 inspos;
259                 struct sub_seq *freesseq;
260
261                 /* Find where lower end should be inserted */
262                 inspos = nameseq_locate_subseq(nseq, lower);
263
264                 /* Fail if upper end overlaps into an existing entry */
265                 if ((inspos < nseq->first_free) &&
266                     (upper >= nseq->sseqs[inspos].lower)) {
267                         return NULL;
268                 }
269
270                 /* Ensure there is space for new sub-sequence */
271                 if (nseq->first_free == nseq->alloc) {
272                         struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
273
274                         if (!sseqs) {
275                                 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
276                                         type, lower, upper);
277                                 return NULL;
278                         }
279                         memcpy(sseqs, nseq->sseqs,
280                                nseq->alloc * sizeof(struct sub_seq));
281                         kfree(nseq->sseqs);
282                         nseq->sseqs = sseqs;
283                         nseq->alloc *= 2;
284                 }
285
286                 info = kzalloc(sizeof(*info), GFP_ATOMIC);
287                 if (!info) {
288                         pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
289                                 type, lower, upper);
290                         return NULL;
291                 }
292
293                 INIT_LIST_HEAD(&info->node_list);
294                 INIT_LIST_HEAD(&info->cluster_list);
295                 INIT_LIST_HEAD(&info->zone_list);
296
297                 /* Insert new sub-sequence */
298                 sseq = &nseq->sseqs[inspos];
299                 freesseq = &nseq->sseqs[nseq->first_free];
300                 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
301                 memset(sseq, 0, sizeof(*sseq));
302                 nseq->first_free++;
303                 sseq->lower = lower;
304                 sseq->upper = upper;
305                 sseq->info = info;
306                 created_subseq = 1;
307         }
308
309         /* Insert a publication */
310         publ = publ_create(type, lower, upper, scope, node, port, key);
311         if (!publ)
312                 return NULL;
313
314         list_add(&publ->zone_list, &info->zone_list);
315         info->zone_list_size++;
316
317         if (in_own_cluster(net, node)) {
318                 list_add(&publ->cluster_list, &info->cluster_list);
319                 info->cluster_list_size++;
320         }
321
322         if (in_own_node(net, node)) {
323                 list_add(&publ->node_list, &info->node_list);
324                 info->node_list_size++;
325         }
326
327         /* Any subscriptions waiting for notification?  */
328         list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
329                 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
330                                             TIPC_PUBLISHED, publ->ref,
331                                             publ->node, publ->scope,
332                                             created_subseq);
333         }
334         return publ;
335 }
336
337 /**
338  * tipc_nameseq_remove_publ
339  *
340  * NOTE: There may be cases where TIPC is asked to remove a publication
341  * that is not in the name table.  For example, if another node issues a
342  * publication for a name sequence that overlaps an existing name sequence
343  * the publication will not be recorded, which means the publication won't
344  * be found when the name sequence is later withdrawn by that node.
345  * A failed withdraw request simply returns a failure indication and lets the
346  * caller issue any error or warning messages associated with such a problem.
347  */
348 static struct publication *tipc_nameseq_remove_publ(struct net *net,
349                                                     struct name_seq *nseq,
350                                                     u32 inst, u32 node,
351                                                     u32 ref, u32 key)
352 {
353         struct publication *publ;
354         struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
355         struct name_info *info;
356         struct sub_seq *free;
357         struct tipc_subscription *s, *st;
358         int removed_subseq = 0;
359
360         if (!sseq)
361                 return NULL;
362
363         info = sseq->info;
364
365         /* Locate publication, if it exists */
366         list_for_each_entry(publ, &info->zone_list, zone_list) {
367                 if ((publ->key == key) && (publ->ref == ref) &&
368                     (!publ->node || (publ->node == node)))
369                         goto found;
370         }
371         return NULL;
372
373 found:
374         /* Remove publication from zone scope list */
375         list_del(&publ->zone_list);
376         info->zone_list_size--;
377
378         /* Remove publication from cluster scope list, if present */
379         if (in_own_cluster(net, node)) {
380                 list_del(&publ->cluster_list);
381                 info->cluster_list_size--;
382         }
383
384         /* Remove publication from node scope list, if present */
385         if (in_own_node(net, node)) {
386                 list_del(&publ->node_list);
387                 info->node_list_size--;
388         }
389
390         /* Contract subseq list if no more publications for that subseq */
391         if (list_empty(&info->zone_list)) {
392                 kfree(info);
393                 free = &nseq->sseqs[nseq->first_free--];
394                 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
395                 removed_subseq = 1;
396         }
397
398         /* Notify any waiting subscriptions */
399         list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
400                 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
401                                             TIPC_WITHDRAWN, publ->ref,
402                                             publ->node, publ->scope,
403                                             removed_subseq);
404         }
405
406         return publ;
407 }
408
409 /**
410  * tipc_nameseq_subscribe - attach a subscription, and optionally
411  * issue the prescribed number of events if there is any sub-
412  * sequence overlapping with the requested sequence
413  */
414 static void tipc_nameseq_subscribe(struct name_seq *nseq,
415                                    struct tipc_subscription *sub)
416 {
417         struct sub_seq *sseq = nseq->sseqs;
418         struct tipc_name_seq ns;
419         struct tipc_subscr *s = &sub->evt.s;
420         bool no_status;
421
422         ns.type = tipc_sub_read(s, seq.type);
423         ns.lower = tipc_sub_read(s, seq.lower);
424         ns.upper = tipc_sub_read(s, seq.upper);
425         no_status = tipc_sub_read(s, filter) & TIPC_SUB_NO_STATUS;
426
427         tipc_subscrp_get(sub);
428         list_add(&sub->nameseq_list, &nseq->subscriptions);
429
430         if (no_status || !sseq)
431                 return;
432
433         while (sseq != &nseq->sseqs[nseq->first_free]) {
434                 if (tipc_subscrp_check_overlap(&ns, sseq->lower, sseq->upper)) {
435                         struct publication *crs;
436                         struct name_info *info = sseq->info;
437                         int must_report = 1;
438
439                         list_for_each_entry(crs, &info->zone_list, zone_list) {
440                                 tipc_subscrp_report_overlap(sub, sseq->lower,
441                                                             sseq->upper,
442                                                             TIPC_PUBLISHED,
443                                                             crs->ref, crs->node,
444                                                             crs->scope,
445                                                             must_report);
446                                 must_report = 0;
447                         }
448                 }
449                 sseq++;
450         }
451 }
452
453 static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
454 {
455         struct tipc_net *tn = net_generic(net, tipc_net_id);
456         struct hlist_head *seq_head;
457         struct name_seq *ns;
458
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)
462                         return ns;
463         }
464
465         return NULL;
466 };
467
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)
471 {
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);
476
477         if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
478             (lower > upper)) {
479                 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
480                          type, lower, upper, scope);
481                 return NULL;
482         }
483
484         if (!seq)
485                 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
486         if (!seq)
487                 return NULL;
488
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);
493         return publ;
494 }
495
496 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
497                                              u32 lower, u32 node, u32 ref,
498                                              u32 key)
499 {
500         struct publication *publ;
501         struct name_seq *seq = nametbl_find_seq(net, type);
502
503         if (!seq)
504                 return NULL;
505
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);
510                 kfree(seq->sseqs);
511                 spin_unlock_bh(&seq->lock);
512                 kfree_rcu(seq, rcu);
513                 return publ;
514         }
515         spin_unlock_bh(&seq->lock);
516         return publ;
517 }
518
519 /**
520  * tipc_nametbl_translate - perform name translation
521  *
522  * On entry, 'destnode' is the search domain used during translation.
523  *
524  * On exit:
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
530  *   and returns 0
531  */
532 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
533                            u32 *destnode)
534 {
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;
540         u32 ref = 0;
541         u32 node = 0;
542
543         if (!tipc_in_scope(*destnode, tn->own_addr))
544                 return 0;
545
546         rcu_read_lock();
547         seq = nametbl_find_seq(net, type);
548         if (unlikely(!seq))
549                 goto not_found;
550         spin_lock_bh(&seq->lock);
551         sseq = nameseq_find_subseq(seq, instance);
552         if (unlikely(!sseq))
553                 goto no_match;
554         info = sseq->info;
555
556         /* Closest-First Algorithm */
557         if (likely(!*destnode)) {
558                 if (!list_empty(&info->node_list)) {
559                         publ = list_first_entry(&info->node_list,
560                                                 struct publication,
561                                                 node_list);
562                         list_move_tail(&publ->node_list,
563                                        &info->node_list);
564                 } else if (!list_empty(&info->cluster_list)) {
565                         publ = list_first_entry(&info->cluster_list,
566                                                 struct publication,
567                                                 cluster_list);
568                         list_move_tail(&publ->cluster_list,
569                                        &info->cluster_list);
570                 } else {
571                         publ = list_first_entry(&info->zone_list,
572                                                 struct publication,
573                                                 zone_list);
574                         list_move_tail(&publ->zone_list,
575                                        &info->zone_list);
576                 }
577         }
578
579         /* Round-Robin Algorithm */
580         else if (*destnode == tn->own_addr) {
581                 if (list_empty(&info->node_list))
582                         goto no_match;
583                 publ = list_first_entry(&info->node_list, struct publication,
584                                         node_list);
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))
588                         goto no_match;
589                 publ = list_first_entry(&info->cluster_list, struct publication,
590                                         cluster_list);
591                 list_move_tail(&publ->cluster_list, &info->cluster_list);
592         } else {
593                 publ = list_first_entry(&info->zone_list, struct publication,
594                                         zone_list);
595                 list_move_tail(&publ->zone_list, &info->zone_list);
596         }
597
598         ref = publ->ref;
599         node = publ->node;
600 no_match:
601         spin_unlock_bh(&seq->lock);
602 not_found:
603         rcu_read_unlock();
604         *destnode = node;
605         return ref;
606 }
607
608 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
609                          struct list_head *dsts, int *dstcnt, u32 exclude,
610                          bool all)
611 {
612         u32 self = tipc_own_addr(net);
613         struct publication *publ;
614         struct name_info *info;
615         struct name_seq *seq;
616         struct sub_seq *sseq;
617
618         *dstcnt = 0;
619         rcu_read_lock();
620         seq = nametbl_find_seq(net, type);
621         if (unlikely(!seq))
622                 goto exit;
623         spin_lock_bh(&seq->lock);
624         sseq = nameseq_find_subseq(seq, instance);
625         if (likely(sseq)) {
626                 info = sseq->info;
627                 list_for_each_entry(publ, &info->zone_list, zone_list) {
628                         if (publ->scope != scope)
629                                 continue;
630                         if (publ->ref == exclude && publ->node == self)
631                                 continue;
632                         tipc_dest_push(dsts, publ->node, publ->ref);
633                         (*dstcnt)++;
634                         if (all)
635                                 continue;
636                         list_move_tail(&publ->zone_list, &info->zone_list);
637                         break;
638                 }
639         }
640         spin_unlock_bh(&seq->lock);
641 exit:
642         rcu_read_unlock();
643         return !list_empty(dsts);
644 }
645
646 int tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
647                            u32 scope, bool exact, struct list_head *dports)
648 {
649         struct sub_seq *sseq_stop;
650         struct name_info *info;
651         struct publication *p;
652         struct name_seq *seq;
653         struct sub_seq *sseq;
654         int res = 0;
655
656         rcu_read_lock();
657         seq = nametbl_find_seq(net, type);
658         if (!seq)
659                 goto exit;
660
661         spin_lock_bh(&seq->lock);
662         sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
663         sseq_stop = seq->sseqs + seq->first_free;
664         for (; sseq != sseq_stop; sseq++) {
665                 if (sseq->lower > upper)
666                         break;
667                 info = sseq->info;
668                 list_for_each_entry(p, &info->node_list, node_list) {
669                         if (p->scope == scope || (!exact && p->scope < scope))
670                                 tipc_dest_push(dports, 0, p->ref);
671                 }
672
673                 if (info->cluster_list_size != info->node_list_size)
674                         res = 1;
675         }
676         spin_unlock_bh(&seq->lock);
677 exit:
678         rcu_read_unlock();
679         return res;
680 }
681
682 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
683  * - Creates list of nodes that overlap the given multicast address
684  * - Determines if any node local ports overlap
685  */
686 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
687                                    u32 upper, struct tipc_nlist *nodes)
688 {
689         struct sub_seq *sseq, *stop;
690         struct publication *publ;
691         struct name_info *info;
692         struct name_seq *seq;
693
694         rcu_read_lock();
695         seq = nametbl_find_seq(net, type);
696         if (!seq)
697                 goto exit;
698
699         spin_lock_bh(&seq->lock);
700         sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
701         stop = seq->sseqs + seq->first_free;
702         for (; sseq != stop && sseq->lower <= upper; sseq++) {
703                 info = sseq->info;
704                 list_for_each_entry(publ, &info->zone_list, zone_list) {
705                         tipc_nlist_add(nodes, publ->node);
706                 }
707         }
708         spin_unlock_bh(&seq->lock);
709 exit:
710         rcu_read_unlock();
711 }
712
713 /* tipc_nametbl_build_group - build list of communication group members
714  */
715 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
716                               u32 type, u32 scope)
717 {
718         struct sub_seq *sseq, *stop;
719         struct name_info *info;
720         struct publication *p;
721         struct name_seq *seq;
722
723         rcu_read_lock();
724         seq = nametbl_find_seq(net, type);
725         if (!seq)
726                 goto exit;
727
728         spin_lock_bh(&seq->lock);
729         sseq = seq->sseqs;
730         stop = seq->sseqs + seq->first_free;
731         for (; sseq != stop; sseq++) {
732                 info = sseq->info;
733                 list_for_each_entry(p, &info->zone_list, zone_list) {
734                         if (p->scope != scope)
735                                 continue;
736                         tipc_group_add_member(grp, p->node, p->ref, p->lower);
737                 }
738         }
739         spin_unlock_bh(&seq->lock);
740 exit:
741         rcu_read_unlock();
742 }
743
744 /*
745  * tipc_nametbl_publish - add name publication to network name tables
746  */
747 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
748                                          u32 upper, u32 scope, u32 port_ref,
749                                          u32 key)
750 {
751         struct publication *publ;
752         struct sk_buff *buf = NULL;
753         struct tipc_net *tn = net_generic(net, tipc_net_id);
754
755         spin_lock_bh(&tn->nametbl_lock);
756         if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
757                 pr_warn("Publication failed, local publication limit reached (%u)\n",
758                         TIPC_MAX_PUBLICATIONS);
759                 spin_unlock_bh(&tn->nametbl_lock);
760                 return NULL;
761         }
762
763         publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
764                                         tn->own_addr, port_ref, key);
765         if (likely(publ)) {
766                 tn->nametbl->local_publ_count++;
767                 buf = tipc_named_publish(net, publ);
768                 /* Any pending external events? */
769                 tipc_named_process_backlog(net);
770         }
771         spin_unlock_bh(&tn->nametbl_lock);
772
773         if (buf)
774                 tipc_node_broadcast(net, buf);
775         return publ;
776 }
777
778 /**
779  * tipc_nametbl_withdraw - withdraw name publication from network name tables
780  */
781 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
782                           u32 key)
783 {
784         struct publication *publ;
785         struct sk_buff *skb = NULL;
786         struct tipc_net *tn = net_generic(net, tipc_net_id);
787
788         spin_lock_bh(&tn->nametbl_lock);
789         publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
790                                         ref, key);
791         if (likely(publ)) {
792                 tn->nametbl->local_publ_count--;
793                 skb = tipc_named_withdraw(net, publ);
794                 /* Any pending external events? */
795                 tipc_named_process_backlog(net);
796                 list_del_init(&publ->pport_list);
797                 kfree_rcu(publ, rcu);
798         } else {
799                 pr_err("Unable to remove local publication\n"
800                        "(type=%u, lower=%u, ref=%u, key=%u)\n",
801                        type, lower, ref, key);
802         }
803         spin_unlock_bh(&tn->nametbl_lock);
804
805         if (skb) {
806                 tipc_node_broadcast(net, skb);
807                 return 1;
808         }
809         return 0;
810 }
811
812 /**
813  * tipc_nametbl_subscribe - add a subscription object to the name table
814  */
815 void tipc_nametbl_subscribe(struct tipc_subscription *sub)
816 {
817         struct tipc_server *srv = sub->server;
818         struct tipc_net *tn = tipc_net(srv->net);
819         struct tipc_subscr *s = &sub->evt.s;
820         u32 type = tipc_sub_read(s, seq.type);
821         int index = hash(type);
822         struct name_seq *seq;
823         struct tipc_name_seq ns;
824
825         spin_lock_bh(&tn->nametbl_lock);
826         seq = nametbl_find_seq(srv->net, type);
827         if (!seq)
828                 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
829         if (seq) {
830                 spin_lock_bh(&seq->lock);
831                 tipc_nameseq_subscribe(seq, sub);
832                 spin_unlock_bh(&seq->lock);
833         } else {
834                 ns.type = tipc_sub_read(s, seq.type);
835                 ns.lower = tipc_sub_read(s, seq.lower);
836                 ns.upper = tipc_sub_read(s, seq.upper);
837                 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
838                         ns.type, ns.lower, ns.upper);
839         }
840         spin_unlock_bh(&tn->nametbl_lock);
841 }
842
843 /**
844  * tipc_nametbl_unsubscribe - remove a subscription object from name table
845  */
846 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
847 {
848         struct tipc_server *srv = sub->server;
849         struct tipc_subscr *s = &sub->evt.s;
850         struct tipc_net *tn = tipc_net(srv->net);
851         struct name_seq *seq;
852         u32 type = tipc_sub_read(s, seq.type);
853
854         spin_lock_bh(&tn->nametbl_lock);
855         seq = nametbl_find_seq(srv->net, type);
856         if (seq != NULL) {
857                 spin_lock_bh(&seq->lock);
858                 list_del_init(&sub->nameseq_list);
859                 tipc_subscrp_put(sub);
860                 if (!seq->first_free && list_empty(&seq->subscriptions)) {
861                         hlist_del_init_rcu(&seq->ns_list);
862                         kfree(seq->sseqs);
863                         spin_unlock_bh(&seq->lock);
864                         kfree_rcu(seq, rcu);
865                 } else {
866                         spin_unlock_bh(&seq->lock);
867                 }
868         }
869         spin_unlock_bh(&tn->nametbl_lock);
870 }
871
872 int tipc_nametbl_init(struct net *net)
873 {
874         struct tipc_net *tn = net_generic(net, tipc_net_id);
875         struct name_table *tipc_nametbl;
876         int i;
877
878         tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
879         if (!tipc_nametbl)
880                 return -ENOMEM;
881
882         for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
883                 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
884
885         INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
886         INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
887         INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
888         tn->nametbl = tipc_nametbl;
889         spin_lock_init(&tn->nametbl_lock);
890         return 0;
891 }
892
893 /**
894  * tipc_purge_publications - remove all publications for a given type
895  *
896  * tipc_nametbl_lock must be held when calling this function
897  */
898 static void tipc_purge_publications(struct net *net, struct name_seq *seq)
899 {
900         struct publication *publ, *safe;
901         struct sub_seq *sseq;
902         struct name_info *info;
903
904         spin_lock_bh(&seq->lock);
905         sseq = seq->sseqs;
906         info = sseq->info;
907         list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
908                 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
909                                          publ->ref, publ->key);
910                 kfree_rcu(publ, rcu);
911         }
912         hlist_del_init_rcu(&seq->ns_list);
913         kfree(seq->sseqs);
914         spin_unlock_bh(&seq->lock);
915
916         kfree_rcu(seq, rcu);
917 }
918
919 void tipc_nametbl_stop(struct net *net)
920 {
921         u32 i;
922         struct name_seq *seq;
923         struct hlist_head *seq_head;
924         struct tipc_net *tn = net_generic(net, tipc_net_id);
925         struct name_table *tipc_nametbl = tn->nametbl;
926
927         /* Verify name table is empty and purge any lingering
928          * publications, then release the name table
929          */
930         spin_lock_bh(&tn->nametbl_lock);
931         for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
932                 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
933                         continue;
934                 seq_head = &tipc_nametbl->seq_hlist[i];
935                 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
936                         tipc_purge_publications(net, seq);
937                 }
938         }
939         spin_unlock_bh(&tn->nametbl_lock);
940
941         synchronize_net();
942         kfree(tipc_nametbl);
943
944 }
945
946 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
947                                         struct name_seq *seq,
948                                         struct sub_seq *sseq, u32 *last_publ)
949 {
950         void *hdr;
951         struct nlattr *attrs;
952         struct nlattr *publ;
953         struct publication *p;
954
955         if (*last_publ) {
956                 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
957                         if (p->key == *last_publ)
958                                 break;
959                 if (p->key != *last_publ)
960                         return -EPIPE;
961         } else {
962                 p = list_first_entry(&sseq->info->zone_list, struct publication,
963                                      zone_list);
964         }
965
966         list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
967                 *last_publ = p->key;
968
969                 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
970                                   &tipc_genl_family, NLM_F_MULTI,
971                                   TIPC_NL_NAME_TABLE_GET);
972                 if (!hdr)
973                         return -EMSGSIZE;
974
975                 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
976                 if (!attrs)
977                         goto msg_full;
978
979                 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
980                 if (!publ)
981                         goto attr_msg_full;
982
983                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
984                         goto publ_msg_full;
985                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
986                         goto publ_msg_full;
987                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
988                         goto publ_msg_full;
989                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
990                         goto publ_msg_full;
991                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
992                         goto publ_msg_full;
993                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
994                         goto publ_msg_full;
995                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
996                         goto publ_msg_full;
997
998                 nla_nest_end(msg->skb, publ);
999                 nla_nest_end(msg->skb, attrs);
1000                 genlmsg_end(msg->skb, hdr);
1001         }
1002         *last_publ = 0;
1003
1004         return 0;
1005
1006 publ_msg_full:
1007         nla_nest_cancel(msg->skb, publ);
1008 attr_msg_full:
1009         nla_nest_cancel(msg->skb, attrs);
1010 msg_full:
1011         genlmsg_cancel(msg->skb, hdr);
1012
1013         return -EMSGSIZE;
1014 }
1015
1016 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1017                                  u32 *last_lower, u32 *last_publ)
1018 {
1019         struct sub_seq *sseq;
1020         struct sub_seq *sseq_start;
1021         int err;
1022
1023         if (*last_lower) {
1024                 sseq_start = nameseq_find_subseq(seq, *last_lower);
1025                 if (!sseq_start)
1026                         return -EPIPE;
1027         } else {
1028                 sseq_start = seq->sseqs;
1029         }
1030
1031         for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
1032                 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
1033                 if (err) {
1034                         *last_lower = sseq->lower;
1035                         return err;
1036                 }
1037         }
1038         *last_lower = 0;
1039
1040         return 0;
1041 }
1042
1043 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
1044                             u32 *last_type, u32 *last_lower, u32 *last_publ)
1045 {
1046         struct tipc_net *tn = net_generic(net, tipc_net_id);
1047         struct hlist_head *seq_head;
1048         struct name_seq *seq = NULL;
1049         int err;
1050         int i;
1051
1052         if (*last_type)
1053                 i = hash(*last_type);
1054         else
1055                 i = 0;
1056
1057         for (; i < TIPC_NAMETBL_SIZE; i++) {
1058                 seq_head = &tn->nametbl->seq_hlist[i];
1059
1060                 if (*last_type) {
1061                         seq = nametbl_find_seq(net, *last_type);
1062                         if (!seq)
1063                                 return -EPIPE;
1064                 } else {
1065                         hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1066                                 break;
1067                         if (!seq)
1068                                 continue;
1069                 }
1070
1071                 hlist_for_each_entry_from_rcu(seq, ns_list) {
1072                         spin_lock_bh(&seq->lock);
1073                         err = __tipc_nl_subseq_list(msg, seq, last_lower,
1074                                                     last_publ);
1075
1076                         if (err) {
1077                                 *last_type = seq->type;
1078                                 spin_unlock_bh(&seq->lock);
1079                                 return err;
1080                         }
1081                         spin_unlock_bh(&seq->lock);
1082                 }
1083                 *last_type = 0;
1084         }
1085         return 0;
1086 }
1087
1088 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1089 {
1090         int err;
1091         int done = cb->args[3];
1092         u32 last_type = cb->args[0];
1093         u32 last_lower = cb->args[1];
1094         u32 last_publ = cb->args[2];
1095         struct net *net = sock_net(skb->sk);
1096         struct tipc_nl_msg msg;
1097
1098         if (done)
1099                 return 0;
1100
1101         msg.skb = skb;
1102         msg.portid = NETLINK_CB(cb->skb).portid;
1103         msg.seq = cb->nlh->nlmsg_seq;
1104
1105         rcu_read_lock();
1106         err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
1107         if (!err) {
1108                 done = 1;
1109         } else if (err != -EMSGSIZE) {
1110                 /* We never set seq or call nl_dump_check_consistent() this
1111                  * means that setting prev_seq here will cause the consistence
1112                  * check to fail in the netlink callback handler. Resulting in
1113                  * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1114                  * we got an error.
1115                  */
1116                 cb->prev_seq = 1;
1117         }
1118         rcu_read_unlock();
1119
1120         cb->args[0] = last_type;
1121         cb->args[1] = last_lower;
1122         cb->args[2] = last_publ;
1123         cb->args[3] = done;
1124
1125         return skb->len;
1126 }
1127
1128 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1129 {
1130         u64 value = (u64)node << 32 | port;
1131         struct tipc_dest *dst;
1132
1133         list_for_each_entry(dst, l, list) {
1134                 if (dst->value != value)
1135                         continue;
1136                 return dst;
1137         }
1138         return NULL;
1139 }
1140
1141 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1142 {
1143         u64 value = (u64)node << 32 | port;
1144         struct tipc_dest *dst;
1145
1146         if (tipc_dest_find(l, node, port))
1147                 return false;
1148
1149         dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1150         if (unlikely(!dst))
1151                 return false;
1152         dst->value = value;
1153         list_add(&dst->list, l);
1154         return true;
1155 }
1156
1157 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1158 {
1159         struct tipc_dest *dst;
1160
1161         if (list_empty(l))
1162                 return false;
1163         dst = list_first_entry(l, typeof(*dst), list);
1164         if (port)
1165                 *port = dst->port;
1166         if (node)
1167                 *node = dst->node;
1168         list_del(&dst->list);
1169         kfree(dst);
1170         return true;
1171 }
1172
1173 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1174 {
1175         struct tipc_dest *dst;
1176
1177         dst = tipc_dest_find(l, node, port);
1178         if (!dst)
1179                 return false;
1180         list_del(&dst->list);
1181         kfree(dst);
1182         return true;
1183 }
1184
1185 void tipc_dest_list_purge(struct list_head *l)
1186 {
1187         struct tipc_dest *dst, *tmp;
1188
1189         list_for_each_entry_safe(dst, tmp, l, list) {
1190                 list_del(&dst->list);
1191                 kfree(dst);
1192         }
1193 }
1194
1195 int tipc_dest_list_len(struct list_head *l)
1196 {
1197         struct tipc_dest *dst;
1198         int i = 0;
1199
1200         list_for_each_entry(dst, l, list) {
1201                 i++;
1202         }
1203         return i;
1204 }