]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/tipc/name_table.c
UBUNTU: Ubuntu-4.15.0-96.97
[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 *
3c724acd 4 * Copyright (c) 2000-2006, 2014-2015, 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
4ac1c8d0 37#include <net/sock.h>
b97bf3fd 38#include "core.h"
22ae7cff 39#include "netlink.h"
b97bf3fd
PL
40#include "name_table.h"
41#include "name_distr.h"
b97bf3fd 42#include "subscr.h"
1da46568 43#include "bcast.h"
22ae7cff 44#include "addr.h"
1d7e1c25 45#include "node.h"
75da2163 46#include "group.h"
22ae7cff 47#include <net/genetlink.h>
b97bf3fd 48
f046e7d9 49#define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
b97bf3fd
PL
50
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
05790c64 108static int hash(int x)
b97bf3fd 109{
f046e7d9 110 return x & (TIPC_NAMETBL_SIZE - 1);
b97bf3fd
PL
111}
112
113/**
114 * publ_create - create a publication structure
115 */
c4307285
YH
116static struct publication *publ_create(u32 type, u32 lower, u32 upper,
117 u32 scope, u32 node, u32 port_ref,
b97bf3fd
PL
118 u32 key)
119{
0da974f4 120 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
b97bf3fd 121 if (publ == NULL) {
2cf8aa19 122 pr_warn("Publication creation failure, no memory\n");
1fc54d8f 123 return NULL;
b97bf3fd
PL
124 }
125
b97bf3fd
PL
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;
b97bf3fd 133 INIT_LIST_HEAD(&publ->pport_list);
b97bf3fd
PL
134 return publ;
135}
136
137/**
4323add6 138 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
b97bf3fd 139 */
988f088a 140static struct sub_seq *tipc_subseq_alloc(u32 cnt)
b97bf3fd 141{
0cee6bbe 142 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
b97bf3fd
PL
143}
144
145/**
4323add6 146 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
c4307285 147 *
b97bf3fd
PL
148 * Allocates a single sub-sequence structure and sets it to all 0's.
149 */
988f088a 150static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
b97bf3fd 151{
0da974f4 152 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
4323add6 153 struct sub_seq *sseq = tipc_subseq_alloc(1);
b97bf3fd
PL
154
155 if (!nseq || !sseq) {
2cf8aa19 156 pr_warn("Name sequence creation failed, no memory\n");
b97bf3fd
PL
157 kfree(nseq);
158 kfree(sseq);
1fc54d8f 159 return NULL;
b97bf3fd
PL
160 }
161
34af946a 162 spin_lock_init(&nseq->lock);
b97bf3fd
PL
163 nseq->type = type;
164 nseq->sseqs = sseq;
b97bf3fd
PL
165 nseq->alloc = 1;
166 INIT_HLIST_NODE(&nseq->ns_list);
167 INIT_LIST_HEAD(&nseq->subscriptions);
97ede29e 168 hlist_add_head_rcu(&nseq->ns_list, seq_head);
b97bf3fd
PL
169 return nseq;
170}
171
2c53040f 172/**
b97bf3fd 173 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
c4307285 174 *
b97bf3fd
PL
175 * Very time-critical, so binary searches through sub-sequence array.
176 */
05790c64
SR
177static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
178 u32 instance)
b97bf3fd
PL
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 }
1fc54d8f 194 return NULL;
b97bf3fd
PL
195}
196
197/**
198 * nameseq_locate_subseq - determine position of name instance in sub-sequence
c4307285 199 *
b97bf3fd
PL
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 */
b97bf3fd
PL
206static 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/**
617d3c7a 226 * tipc_nameseq_insert_publ
b97bf3fd 227 */
34747539
YX
228static 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)
b97bf3fd 233{
fead3909
PG
234 struct tipc_subscription *s;
235 struct tipc_subscription *st;
b97bf3fd
PL
236 struct publication *publ;
237 struct sub_seq *sseq;
b52124a5 238 struct name_info *info;
b97bf3fd
PL
239 int created_subseq = 0;
240
b97bf3fd 241 sseq = nameseq_find_subseq(nseq, lower);
b97bf3fd
PL
242 if (sseq) {
243
244 /* Lower end overlaps existing entry => need an exact match */
b97bf3fd 245 if ((sseq->lower != lower) || (sseq->upper != upper)) {
1fc54d8f 246 return NULL;
b97bf3fd 247 }
b52124a5
AS
248
249 info = sseq->info;
f80c24d9
AS
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 }
b97bf3fd
PL
257 } else {
258 u32 inspos;
259 struct sub_seq *freesseq;
260
261 /* Find where lower end should be inserted */
b97bf3fd
PL
262 inspos = nameseq_locate_subseq(nseq, lower);
263
264 /* Fail if upper end overlaps into an existing entry */
b97bf3fd
PL
265 if ((inspos < nseq->first_free) &&
266 (upper >= nseq->sseqs[inspos].lower)) {
1fc54d8f 267 return NULL;
b97bf3fd
PL
268 }
269
270 /* Ensure there is space for new sub-sequence */
b97bf3fd 271 if (nseq->first_free == nseq->alloc) {
9ab230f8
AS
272 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
273
274 if (!sseqs) {
2cf8aa19
EH
275 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
276 type, lower, upper);
1fc54d8f 277 return NULL;
b97bf3fd 278 }
9ab230f8
AS
279 memcpy(sseqs, nseq->sseqs,
280 nseq->alloc * sizeof(struct sub_seq));
281 kfree(nseq->sseqs);
282 nseq->sseqs = sseqs;
283 nseq->alloc *= 2;
b97bf3fd 284 }
b97bf3fd 285
b52124a5
AS
286 info = kzalloc(sizeof(*info), GFP_ATOMIC);
287 if (!info) {
2cf8aa19
EH
288 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
289 type, lower, upper);
b52124a5
AS
290 return NULL;
291 }
292
f6f0a4d2
AS
293 INIT_LIST_HEAD(&info->node_list);
294 INIT_LIST_HEAD(&info->cluster_list);
295 INIT_LIST_HEAD(&info->zone_list);
296
b97bf3fd 297 /* Insert new sub-sequence */
b97bf3fd
PL
298 sseq = &nseq->sseqs[inspos];
299 freesseq = &nseq->sseqs[nseq->first_free];
0e65967e
AS
300 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
301 memset(sseq, 0, sizeof(*sseq));
b97bf3fd
PL
302 nseq->first_free++;
303 sseq->lower = lower;
304 sseq->upper = upper;
b52124a5 305 sseq->info = info;
b97bf3fd
PL
306 created_subseq = 1;
307 }
b97bf3fd 308
617d3c7a 309 /* Insert a publication */
b97bf3fd
PL
310 publ = publ_create(type, lower, upper, scope, node, port, key);
311 if (!publ)
1fc54d8f 312 return NULL;
b97bf3fd 313
f6f0a4d2 314 list_add(&publ->zone_list, &info->zone_list);
b52124a5 315 info->zone_list_size++;
b97bf3fd 316
34747539 317 if (in_own_cluster(net, node)) {
f6f0a4d2 318 list_add(&publ->cluster_list, &info->cluster_list);
b52124a5 319 info->cluster_list_size++;
b97bf3fd
PL
320 }
321
34747539 322 if (in_own_node(net, node)) {
f6f0a4d2 323 list_add(&publ->node_list, &info->node_list);
b52124a5 324 info->node_list_size++;
b97bf3fd
PL
325 }
326
617d3c7a 327 /* Any subscriptions waiting for notification? */
b97bf3fd 328 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
57f1d186
YX
329 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
330 TIPC_PUBLISHED, publ->ref,
331 publ->node, created_subseq);
b97bf3fd
PL
332 }
333 return publ;
334}
335
336/**
617d3c7a 337 * tipc_nameseq_remove_publ
c4307285 338 *
f131072c
AS
339 * NOTE: There may be cases where TIPC is asked to remove a publication
340 * that is not in the name table. For example, if another node issues a
341 * publication for a name sequence that overlaps an existing name sequence
342 * the publication will not be recorded, which means the publication won't
343 * be found when the name sequence is later withdrawn by that node.
344 * A failed withdraw request simply returns a failure indication and lets the
345 * caller issue any error or warning messages associated with such a problem.
b97bf3fd 346 */
34747539
YX
347static struct publication *tipc_nameseq_remove_publ(struct net *net,
348 struct name_seq *nseq,
349 u32 inst, u32 node,
350 u32 ref, u32 key)
b97bf3fd
PL
351{
352 struct publication *publ;
b97bf3fd 353 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
b52124a5 354 struct name_info *info;
b97bf3fd 355 struct sub_seq *free;
fead3909 356 struct tipc_subscription *s, *st;
b97bf3fd
PL
357 int removed_subseq = 0;
358
f131072c 359 if (!sseq)
1fc54d8f 360 return NULL;
f131072c 361
b52124a5
AS
362 info = sseq->info;
363
f6f0a4d2 364 /* Locate publication, if it exists */
f6f0a4d2
AS
365 list_for_each_entry(publ, &info->zone_list, zone_list) {
366 if ((publ->key == key) && (publ->ref == ref) &&
367 (!publ->node || (publ->node == node)))
368 goto found;
369 }
370 return NULL;
c4307285 371
f6f0a4d2
AS
372found:
373 /* Remove publication from zone scope list */
f6f0a4d2 374 list_del(&publ->zone_list);
b52124a5 375 info->zone_list_size--;
b97bf3fd 376
f131072c 377 /* Remove publication from cluster scope list, if present */
34747539 378 if (in_own_cluster(net, node)) {
f6f0a4d2 379 list_del(&publ->cluster_list);
b52124a5 380 info->cluster_list_size--;
b97bf3fd 381 }
f131072c
AS
382
383 /* Remove publication from node scope list, if present */
34747539 384 if (in_own_node(net, node)) {
f6f0a4d2 385 list_del(&publ->node_list);
b52124a5 386 info->node_list_size--;
b97bf3fd 387 }
b97bf3fd 388
f131072c 389 /* Contract subseq list if no more publications for that subseq */
f6f0a4d2 390 if (list_empty(&info->zone_list)) {
b52124a5 391 kfree(info);
b97bf3fd 392 free = &nseq->sseqs[nseq->first_free--];
0e65967e 393 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
b97bf3fd
PL
394 removed_subseq = 1;
395 }
396
f131072c 397 /* Notify any waiting subscriptions */
b97bf3fd 398 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
57f1d186
YX
399 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
400 TIPC_WITHDRAWN, publ->ref,
401 publ->node, removed_subseq);
b97bf3fd 402 }
f131072c 403
b97bf3fd
PL
404 return publ;
405}
406
407/**
2c53040f 408 * tipc_nameseq_subscribe - attach a subscription, and issue
b97bf3fd
PL
409 * the prescribed number of events if there is any sub-
410 * sequence overlapping with the requested sequence
411 */
fead3909 412static void tipc_nameseq_subscribe(struct name_seq *nseq,
ae8509c4 413 struct tipc_subscription *s)
b97bf3fd
PL
414{
415 struct sub_seq *sseq = nseq->sseqs;
a4273c73
PB
416 struct tipc_name_seq ns;
417
418 tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
b97bf3fd 419
7efea60d 420 tipc_subscrp_get(s);
b97bf3fd
PL
421 list_add(&s->nameseq_list, &nseq->subscriptions);
422
423 if (!sseq)
424 return;
425
426 while (sseq != &nseq->sseqs[nseq->first_free]) {
a4273c73 427 if (tipc_subscrp_check_overlap(&ns, sseq->lower, sseq->upper)) {
f6f0a4d2
AS
428 struct publication *crs;
429 struct name_info *info = sseq->info;
b97bf3fd
PL
430 int must_report = 1;
431
f6f0a4d2 432 list_for_each_entry(crs, &info->zone_list, zone_list) {
57f1d186
YX
433 tipc_subscrp_report_overlap(s, sseq->lower,
434 sseq->upper,
435 TIPC_PUBLISHED,
436 crs->ref, crs->node,
437 must_report);
b97bf3fd 438 must_report = 0;
f6f0a4d2 439 }
b97bf3fd
PL
440 }
441 sseq++;
442 }
443}
444
4ac1c8d0 445static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
b97bf3fd 446{
4ac1c8d0 447 struct tipc_net *tn = net_generic(net, tipc_net_id);
b97bf3fd 448 struct hlist_head *seq_head;
b97bf3fd
PL
449 struct name_seq *ns;
450
4ac1c8d0 451 seq_head = &tn->nametbl->seq_hlist[hash(type)];
97ede29e 452 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
b29f1428 453 if (ns->type == type)
b97bf3fd 454 return ns;
b97bf3fd
PL
455 }
456
1fc54d8f 457 return NULL;
b97bf3fd
PL
458};
459
4ac1c8d0
YX
460struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
461 u32 lower, u32 upper, u32 scope,
462 u32 node, u32 port, u32 key)
b97bf3fd 463{
4ac1c8d0 464 struct tipc_net *tn = net_generic(net, tipc_net_id);
fb9962f3 465 struct publication *publ;
4ac1c8d0 466 struct name_seq *seq = nametbl_find_seq(net, type);
993bfe5d 467 int index = hash(type);
b97bf3fd 468
8f177896
AS
469 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
470 (lower > upper)) {
2cf8aa19
EH
471 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
472 type, lower, upper, scope);
1fc54d8f 473 return NULL;
b97bf3fd
PL
474 }
475
b29f1428 476 if (!seq)
4ac1c8d0 477 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
b97bf3fd 478 if (!seq)
1fc54d8f 479 return NULL;
b97bf3fd 480
fb9962f3 481 spin_lock_bh(&seq->lock);
34747539 482 publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
4323add6 483 scope, node, port, key);
fb9962f3
YX
484 spin_unlock_bh(&seq->lock);
485 return publ;
b97bf3fd
PL
486}
487
4ac1c8d0
YX
488struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
489 u32 lower, u32 node, u32 ref,
490 u32 key)
b97bf3fd
PL
491{
492 struct publication *publ;
4ac1c8d0 493 struct name_seq *seq = nametbl_find_seq(net, type);
b97bf3fd
PL
494
495 if (!seq)
1fc54d8f 496 return NULL;
b97bf3fd 497
fb9962f3 498 spin_lock_bh(&seq->lock);
34747539 499 publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
fb9962f3 500 if (!seq->first_free && list_empty(&seq->subscriptions)) {
97ede29e 501 hlist_del_init_rcu(&seq->ns_list);
fb9962f3 502 kfree(seq->sseqs);
97ede29e
YX
503 spin_unlock_bh(&seq->lock);
504 kfree_rcu(seq, rcu);
fb9962f3
YX
505 return publ;
506 }
507 spin_unlock_bh(&seq->lock);
b97bf3fd
PL
508 return publ;
509}
510
2c53040f 511/**
bc9f8143 512 * tipc_nametbl_translate - perform name translation
b97bf3fd 513 *
bc9f8143
AS
514 * On entry, 'destnode' is the search domain used during translation.
515 *
516 * On exit:
517 * - if name translation is deferred to another node/cluster/zone,
518 * leaves 'destnode' unchanged (will be non-zero) and returns 0
519 * - if name translation is attempted and succeeds, sets 'destnode'
520 * to publishing node and returns port reference (will be non-zero)
521 * - if name translation is attempted and fails, sets 'destnode' to 0
522 * and returns 0
b97bf3fd 523 */
4ac1c8d0
YX
524u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
525 u32 *destnode)
b97bf3fd 526{
34747539 527 struct tipc_net *tn = net_generic(net, tipc_net_id);
b97bf3fd 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
34747539 535 if (!tipc_in_scope(*destnode, tn->own_addr))
b97bf3fd
PL
536 return 0;
537
97ede29e 538 rcu_read_lock();
4ac1c8d0 539 seq = nametbl_find_seq(net, type);
b97bf3fd
PL
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 */
34747539 572 else if (*destnode == tn->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);
34747539 578 } else if (in_own_cluster_exact(net, *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
ee106d7f
JM
600bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 domain,
601 struct list_head *dsts, int *dstcnt, u32 exclude,
602 bool all)
603{
604 u32 self = tipc_own_addr(net);
605 struct publication *publ;
606 struct name_info *info;
607 struct name_seq *seq;
608 struct sub_seq *sseq;
609
610 if (!tipc_in_scope(domain, self))
611 return false;
612
613 *dstcnt = 0;
614 rcu_read_lock();
615 seq = nametbl_find_seq(net, type);
616 if (unlikely(!seq))
617 goto exit;
618 spin_lock_bh(&seq->lock);
619 sseq = nameseq_find_subseq(seq, instance);
620 if (likely(sseq)) {
621 info = sseq->info;
622 list_for_each_entry(publ, &info->zone_list, zone_list) {
623 if (!tipc_in_scope(domain, publ->node))
624 continue;
625 if (publ->ref == exclude && publ->node == self)
626 continue;
627 tipc_dest_push(dsts, publ->node, publ->ref);
628 (*dstcnt)++;
629 if (all)
630 continue;
631 list_move_tail(&publ->zone_list, &info->zone_list);
632 break;
633 }
634 }
635 spin_unlock_bh(&seq->lock);
636exit:
637 rcu_read_unlock();
638 return !list_empty(dsts);
639}
640
4ac1c8d0 641int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper,
4d8642d8 642 u32 limit, struct list_head *dports)
b97bf3fd
PL
643{
644 struct name_seq *seq;
645 struct sub_seq *sseq;
646 struct sub_seq *sseq_stop;
b52124a5 647 struct name_info *info;
b97bf3fd
PL
648 int res = 0;
649
97ede29e 650 rcu_read_lock();
4ac1c8d0 651 seq = nametbl_find_seq(net, type);
b97bf3fd
PL
652 if (!seq)
653 goto exit;
654
655 spin_lock_bh(&seq->lock);
b97bf3fd
PL
656 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
657 sseq_stop = seq->sseqs + seq->first_free;
658 for (; sseq != sseq_stop; sseq++) {
659 struct publication *publ;
660
661 if (sseq->lower > upper)
662 break;
968edbe1 663
b52124a5 664 info = sseq->info;
f6f0a4d2
AS
665 list_for_each_entry(publ, &info->node_list, node_list) {
666 if (publ->scope <= limit)
a80ae530 667 tipc_dest_push(dports, 0, publ->ref);
968edbe1
AS
668 }
669
b52124a5 670 if (info->cluster_list_size != info->node_list_size)
968edbe1 671 res = 1;
b97bf3fd 672 }
b97bf3fd
PL
673 spin_unlock_bh(&seq->lock);
674exit:
97ede29e 675 rcu_read_unlock();
b97bf3fd
PL
676 return res;
677}
678
2ae0b8af
JPM
679/* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
680 * - Creates list of nodes that overlap the given multicast address
681 * - Determines if any node local ports overlap
682 */
683void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
684 u32 upper, u32 domain,
685 struct tipc_nlist *nodes)
686{
687 struct sub_seq *sseq, *stop;
688 struct publication *publ;
689 struct name_info *info;
690 struct name_seq *seq;
691
692 rcu_read_lock();
693 seq = nametbl_find_seq(net, type);
694 if (!seq)
695 goto exit;
696
697 spin_lock_bh(&seq->lock);
698 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
699 stop = seq->sseqs + seq->first_free;
f65163fe 700 for (; sseq != stop && sseq->lower <= upper; sseq++) {
2ae0b8af
JPM
701 info = sseq->info;
702 list_for_each_entry(publ, &info->zone_list, zone_list) {
703 if (tipc_in_scope(domain, publ->node))
704 tipc_nlist_add(nodes, publ->node);
705 }
706 }
707 spin_unlock_bh(&seq->lock);
708exit:
709 rcu_read_unlock();
710}
711
75da2163
JM
712/* tipc_nametbl_build_group - build list of communication group members
713 */
714void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
715 u32 type, u32 domain)
716{
717 struct sub_seq *sseq, *stop;
718 struct name_info *info;
719 struct publication *p;
720 struct name_seq *seq;
721
722 rcu_read_lock();
723 seq = nametbl_find_seq(net, type);
724 if (!seq)
725 goto exit;
726
727 spin_lock_bh(&seq->lock);
728 sseq = seq->sseqs;
729 stop = seq->sseqs + seq->first_free;
730 for (; sseq != stop; sseq++) {
731 info = sseq->info;
732 list_for_each_entry(p, &info->zone_list, zone_list) {
733 if (!tipc_in_scope(domain, p->node))
734 continue;
735 tipc_group_add_member(grp, p->node, p->ref);
736 }
737 }
738 spin_unlock_bh(&seq->lock);
739exit:
740 rcu_read_unlock();
741}
742
c422f1bd 743/*
4323add6 744 * tipc_nametbl_publish - add name publication to network name tables
b97bf3fd 745 */
f2f9800d
YX
746struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
747 u32 upper, u32 scope, u32 port_ref,
748 u32 key)
b97bf3fd
PL
749{
750 struct publication *publ;
eab8c045 751 struct sk_buff *buf = NULL;
4ac1c8d0 752 struct tipc_net *tn = net_generic(net, tipc_net_id);
b97bf3fd 753
4ac1c8d0
YX
754 spin_lock_bh(&tn->nametbl_lock);
755 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
2cf8aa19 756 pr_warn("Publication failed, local publication limit reached (%u)\n",
e6a04b1d 757 TIPC_MAX_PUBLICATIONS);
4ac1c8d0 758 spin_unlock_bh(&tn->nametbl_lock);
1fc54d8f 759 return NULL;
b97bf3fd 760 }
b97bf3fd 761
4ac1c8d0 762 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
34747539 763 tn->own_addr, port_ref, key);
fd6eced8 764 if (likely(publ)) {
4ac1c8d0
YX
765 tn->nametbl->local_publ_count++;
766 buf = tipc_named_publish(net, publ);
a5325ae5 767 /* Any pending external events? */
f2f9800d 768 tipc_named_process_backlog(net);
fd6eced8 769 }
4ac1c8d0 770 spin_unlock_bh(&tn->nametbl_lock);
eab8c045
YX
771
772 if (buf)
1d7e1c25 773 tipc_node_broadcast(net, buf);
b97bf3fd
PL
774 return publ;
775}
776
777/**
4323add6 778 * tipc_nametbl_withdraw - withdraw name publication from network name tables
b97bf3fd 779 */
f2f9800d
YX
780int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
781 u32 key)
b97bf3fd
PL
782{
783 struct publication *publ;
5492390a 784 struct sk_buff *skb = NULL;
4ac1c8d0 785 struct tipc_net *tn = net_generic(net, tipc_net_id);
b97bf3fd 786
4ac1c8d0 787 spin_lock_bh(&tn->nametbl_lock);
34747539 788 publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
4ac1c8d0 789 ref, key);
f131072c 790 if (likely(publ)) {
4ac1c8d0 791 tn->nametbl->local_publ_count--;
34747539 792 skb = tipc_named_withdraw(net, publ);
a5325ae5 793 /* Any pending external events? */
f2f9800d 794 tipc_named_process_backlog(net);
b97bf3fd 795 list_del_init(&publ->pport_list);
97ede29e 796 kfree_rcu(publ, rcu);
5492390a
YX
797 } else {
798 pr_err("Unable to remove local publication\n"
799 "(type=%u, lower=%u, ref=%u, key=%u)\n",
800 type, lower, ref, key);
801 }
4ac1c8d0 802 spin_unlock_bh(&tn->nametbl_lock);
eab8c045 803
5492390a 804 if (skb) {
1d7e1c25 805 tipc_node_broadcast(net, skb);
b97bf3fd
PL
806 return 1;
807 }
b97bf3fd
PL
808 return 0;
809}
810
811/**
4323add6 812 * tipc_nametbl_subscribe - add a subscription object to the name table
b97bf3fd 813 */
fead3909 814void tipc_nametbl_subscribe(struct tipc_subscription *s)
b97bf3fd 815{
4ac1c8d0 816 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
a4273c73 817 u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
993bfe5d 818 int index = hash(type);
b97bf3fd 819 struct name_seq *seq;
a4273c73 820 struct tipc_name_seq ns;
b97bf3fd 821
4ac1c8d0
YX
822 spin_lock_bh(&tn->nametbl_lock);
823 seq = nametbl_find_seq(s->net, type);
a016892c 824 if (!seq)
4ac1c8d0 825 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
0e65967e 826 if (seq) {
c4307285 827 spin_lock_bh(&seq->lock);
c4307285
YH
828 tipc_nameseq_subscribe(seq, s);
829 spin_unlock_bh(&seq->lock);
830 } else {
a4273c73 831 tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
2cf8aa19 832 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
a4273c73 833 ns.type, ns.lower, ns.upper);
c4307285 834 }
4ac1c8d0 835 spin_unlock_bh(&tn->nametbl_lock);
b97bf3fd
PL
836}
837
838/**
4323add6 839 * tipc_nametbl_unsubscribe - remove a subscription object from name table
b97bf3fd 840 */
fead3909 841void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
b97bf3fd 842{
4ac1c8d0 843 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
b97bf3fd 844 struct name_seq *seq;
a4273c73 845 u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
b97bf3fd 846
4ac1c8d0 847 spin_lock_bh(&tn->nametbl_lock);
a4273c73 848 seq = nametbl_find_seq(s->net, type);
0e65967e 849 if (seq != NULL) {
c4307285
YH
850 spin_lock_bh(&seq->lock);
851 list_del_init(&s->nameseq_list);
7efea60d 852 tipc_subscrp_put(s);
fb9962f3 853 if (!seq->first_free && list_empty(&seq->subscriptions)) {
97ede29e 854 hlist_del_init_rcu(&seq->ns_list);
fb9962f3 855 kfree(seq->sseqs);
97ede29e
YX
856 spin_unlock_bh(&seq->lock);
857 kfree_rcu(seq, rcu);
fb9962f3
YX
858 } else {
859 spin_unlock_bh(&seq->lock);
860 }
c4307285 861 }
4ac1c8d0 862 spin_unlock_bh(&tn->nametbl_lock);
b97bf3fd
PL
863}
864
4ac1c8d0 865int tipc_nametbl_init(struct net *net)
b97bf3fd 866{
4ac1c8d0
YX
867 struct tipc_net *tn = net_generic(net, tipc_net_id);
868 struct name_table *tipc_nametbl;
993bfe5d
YX
869 int i;
870
871 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
872 if (!tipc_nametbl)
b97bf3fd
PL
873 return -ENOMEM;
874
993bfe5d
YX
875 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
876 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
877
878 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
879 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
880 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
4ac1c8d0
YX
881 tn->nametbl = tipc_nametbl;
882 spin_lock_init(&tn->nametbl_lock);
b97bf3fd
PL
883 return 0;
884}
885
1bb8dce5
EH
886/**
887 * tipc_purge_publications - remove all publications for a given type
888 *
889 * tipc_nametbl_lock must be held when calling this function
890 */
4ac1c8d0 891static void tipc_purge_publications(struct net *net, struct name_seq *seq)
1bb8dce5
EH
892{
893 struct publication *publ, *safe;
894 struct sub_seq *sseq;
895 struct name_info *info;
896
fb9962f3 897 spin_lock_bh(&seq->lock);
1bb8dce5
EH
898 sseq = seq->sseqs;
899 info = sseq->info;
900 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
8460504b
YX
901 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
902 publ->ref, publ->key);
97ede29e 903 kfree_rcu(publ, rcu);
1bb8dce5 904 }
97ede29e
YX
905 hlist_del_init_rcu(&seq->ns_list);
906 kfree(seq->sseqs);
023160bc 907 spin_unlock_bh(&seq->lock);
fb9962f3 908
97ede29e 909 kfree_rcu(seq, rcu);
1bb8dce5
EH
910}
911
4ac1c8d0 912void tipc_nametbl_stop(struct net *net)
b97bf3fd 913{
b97bf3fd 914 u32 i;
1bb8dce5
EH
915 struct name_seq *seq;
916 struct hlist_head *seq_head;
4ac1c8d0
YX
917 struct tipc_net *tn = net_generic(net, tipc_net_id);
918 struct name_table *tipc_nametbl = tn->nametbl;
b97bf3fd 919
1bb8dce5
EH
920 /* Verify name table is empty and purge any lingering
921 * publications, then release the name table
922 */
4ac1c8d0 923 spin_lock_bh(&tn->nametbl_lock);
f046e7d9 924 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
993bfe5d 925 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
f705ab95 926 continue;
993bfe5d 927 seq_head = &tipc_nametbl->seq_hlist[i];
97ede29e 928 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
4ac1c8d0 929 tipc_purge_publications(net, seq);
1bb8dce5 930 }
b97bf3fd 931 }
4ac1c8d0 932 spin_unlock_bh(&tn->nametbl_lock);
993bfe5d 933
97ede29e 934 synchronize_net();
993bfe5d
YX
935 kfree(tipc_nametbl);
936
b97bf3fd 937}
1593123a 938
d8182804
RA
939static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
940 struct name_seq *seq,
941 struct sub_seq *sseq, u32 *last_publ)
1593123a
RA
942{
943 void *hdr;
944 struct nlattr *attrs;
945 struct nlattr *publ;
946 struct publication *p;
947
948 if (*last_publ) {
949 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
950 if (p->key == *last_publ)
951 break;
952 if (p->key != *last_publ)
953 return -EPIPE;
954 } else {
955 p = list_first_entry(&sseq->info->zone_list, struct publication,
956 zone_list);
957 }
958
959 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
960 *last_publ = p->key;
961
962 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
bfb3e5dd 963 &tipc_genl_family, NLM_F_MULTI,
1593123a
RA
964 TIPC_NL_NAME_TABLE_GET);
965 if (!hdr)
966 return -EMSGSIZE;
967
968 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
969 if (!attrs)
970 goto msg_full;
971
972 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
973 if (!publ)
974 goto attr_msg_full;
975
976 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
977 goto publ_msg_full;
978 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
979 goto publ_msg_full;
980 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
981 goto publ_msg_full;
982 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
983 goto publ_msg_full;
984 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
985 goto publ_msg_full;
986 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
987 goto publ_msg_full;
988 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
989 goto publ_msg_full;
990
991 nla_nest_end(msg->skb, publ);
992 nla_nest_end(msg->skb, attrs);
993 genlmsg_end(msg->skb, hdr);
994 }
995 *last_publ = 0;
996
997 return 0;
998
999publ_msg_full:
1000 nla_nest_cancel(msg->skb, publ);
1001attr_msg_full:
1002 nla_nest_cancel(msg->skb, attrs);
1003msg_full:
1004 genlmsg_cancel(msg->skb, hdr);
1005
1006 return -EMSGSIZE;
1007}
1008
d8182804
RA
1009static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1010 u32 *last_lower, u32 *last_publ)
1593123a
RA
1011{
1012 struct sub_seq *sseq;
1013 struct sub_seq *sseq_start;
1014 int err;
1015
1016 if (*last_lower) {
1017 sseq_start = nameseq_find_subseq(seq, *last_lower);
1018 if (!sseq_start)
1019 return -EPIPE;
1020 } else {
1021 sseq_start = seq->sseqs;
1022 }
1023
1024 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
1025 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
1026 if (err) {
1027 *last_lower = sseq->lower;
1028 return err;
1029 }
1030 }
1031 *last_lower = 0;
1032
1033 return 0;
1034}
1035
4ac1c8d0
YX
1036static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
1037 u32 *last_type, u32 *last_lower, u32 *last_publ)
1593123a 1038{
4ac1c8d0 1039 struct tipc_net *tn = net_generic(net, tipc_net_id);
1593123a 1040 struct hlist_head *seq_head;
97ede29e 1041 struct name_seq *seq = NULL;
1593123a
RA
1042 int err;
1043 int i;
1044
1045 if (*last_type)
1046 i = hash(*last_type);
1047 else
1048 i = 0;
1049
1050 for (; i < TIPC_NAMETBL_SIZE; i++) {
4ac1c8d0 1051 seq_head = &tn->nametbl->seq_hlist[i];
1593123a
RA
1052
1053 if (*last_type) {
4ac1c8d0 1054 seq = nametbl_find_seq(net, *last_type);
1593123a
RA
1055 if (!seq)
1056 return -EPIPE;
1057 } else {
97ede29e
YX
1058 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1059 break;
1593123a
RA
1060 if (!seq)
1061 continue;
1062 }
1063
97ede29e 1064 hlist_for_each_entry_from_rcu(seq, ns_list) {
1593123a 1065 spin_lock_bh(&seq->lock);
1593123a
RA
1066 err = __tipc_nl_subseq_list(msg, seq, last_lower,
1067 last_publ);
1068
1069 if (err) {
1070 *last_type = seq->type;
1071 spin_unlock_bh(&seq->lock);
1072 return err;
1073 }
1074 spin_unlock_bh(&seq->lock);
1075 }
1076 *last_type = 0;
1077 }
1078 return 0;
1079}
1080
1081int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1082{
1083 int err;
1084 int done = cb->args[3];
1085 u32 last_type = cb->args[0];
1086 u32 last_lower = cb->args[1];
1087 u32 last_publ = cb->args[2];
4ac1c8d0 1088 struct net *net = sock_net(skb->sk);
1593123a
RA
1089 struct tipc_nl_msg msg;
1090
1091 if (done)
1092 return 0;
1093
1094 msg.skb = skb;
1095 msg.portid = NETLINK_CB(cb->skb).portid;
1096 msg.seq = cb->nlh->nlmsg_seq;
1097
97ede29e 1098 rcu_read_lock();
4ac1c8d0 1099 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
1593123a
RA
1100 if (!err) {
1101 done = 1;
1102 } else if (err != -EMSGSIZE) {
1103 /* We never set seq or call nl_dump_check_consistent() this
1104 * means that setting prev_seq here will cause the consistence
1105 * check to fail in the netlink callback handler. Resulting in
1106 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1107 * we got an error.
1108 */
1109 cb->prev_seq = 1;
1110 }
97ede29e 1111 rcu_read_unlock();
1593123a
RA
1112
1113 cb->args[0] = last_type;
1114 cb->args[1] = last_lower;
1115 cb->args[2] = last_publ;
1116 cb->args[3] = done;
1117
1118 return skb->len;
1119}
3c724acd 1120
a80ae530 1121struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
3c724acd 1122{
a80ae530 1123 struct tipc_dest *dst;
3c724acd 1124
a80ae530 1125 list_for_each_entry(dst, l, list) {
366f68c7
HB
1126 if (dst->node == node && dst->port == port)
1127 return dst;
3c724acd 1128 }
a80ae530 1129 return NULL;
4d8642d8
JPM
1130}
1131
a80ae530 1132bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
4d8642d8 1133{
a80ae530 1134 struct tipc_dest *dst;
4d8642d8 1135
a80ae530 1136 if (tipc_dest_find(l, node, port))
4d8642d8
JPM
1137 return false;
1138
a80ae530
JM
1139 dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1140 if (unlikely(!dst))
1141 return false;
366f68c7
HB
1142 dst->node = node;
1143 dst->port = port;
a80ae530 1144 list_add(&dst->list, l);
4d8642d8
JPM
1145 return true;
1146}
1147
a80ae530 1148bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
4d8642d8 1149{
a80ae530 1150 struct tipc_dest *dst;
4d8642d8
JPM
1151
1152 if (list_empty(l))
a80ae530
JM
1153 return false;
1154 dst = list_first_entry(l, typeof(*dst), list);
1155 if (port)
1156 *port = dst->port;
1157 if (node)
1158 *node = dst->node;
1159 list_del(&dst->list);
1160 kfree(dst);
1161 return true;
4d8642d8
JPM
1162}
1163
a80ae530 1164bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
4d8642d8 1165{
a80ae530 1166 struct tipc_dest *dst;
4d8642d8 1167
a80ae530
JM
1168 dst = tipc_dest_find(l, node, port);
1169 if (!dst)
1170 return false;
1171 list_del(&dst->list);
1172 kfree(dst);
1173 return true;
4d8642d8
JPM
1174}
1175
a80ae530 1176void tipc_dest_list_purge(struct list_head *l)
4d8642d8 1177{
a80ae530 1178 struct tipc_dest *dst, *tmp;
4d8642d8 1179
a80ae530
JM
1180 list_for_each_entry_safe(dst, tmp, l, list) {
1181 list_del(&dst->list);
1182 kfree(dst);
3c724acd
JPM
1183 }
1184}
1185
a80ae530 1186int tipc_dest_list_len(struct list_head *l)
3c724acd 1187{
a80ae530 1188 struct tipc_dest *dst;
4d8642d8 1189 int i = 0;
3c724acd 1190
a80ae530 1191 list_for_each_entry(dst, l, list) {
4d8642d8 1192 i++;
3c724acd 1193 }
4d8642d8 1194 return i;
3c724acd 1195}