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