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