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