blob: beed5fdda00451199b1fe120e5b4b2a733a29a1f [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/name_table.c: TIPC name table code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Richard Alpe15931232014-11-20 10:29:20 +01004 * Copyright (c) 2000-2006, 2014, Ericsson AB
Ying Xue993bfe52014-12-02 15:00:24 +08005 * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "name_table.h"
40#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "subscr.h"
Ying Xue1da46562015-01-09 15:27:07 +080042#include "bcast.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
Ying Xuef046e7d2012-08-16 12:09:11 +000044#define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010045
Richard Alpe15931232014-11-20 10:29:20 +010046static const struct nla_policy
47tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = {
48 [TIPC_NLA_NAME_TABLE_UNSPEC] = { .type = NLA_UNSPEC },
49 [TIPC_NLA_NAME_TABLE_PUBL] = { .type = NLA_NESTED }
50};
51
Per Lidenb97bf3f2006-01-02 19:04:38 +010052/**
Allan Stephensb52124a2011-05-30 09:44:38 -040053 * struct name_info - name sequence publication info
Allan Stephens968edbe2008-07-14 22:45:33 -070054 * @node_list: circular list of publications made by own node
55 * @cluster_list: circular list of publications made by own cluster
56 * @zone_list: circular list of publications made by own zone
57 * @node_list_size: number of entries in "node_list"
58 * @cluster_list_size: number of entries in "cluster_list"
59 * @zone_list_size: number of entries in "zone_list"
60 *
61 * Note: The zone list always contains at least one entry, since all
62 * publications of the associated name sequence belong to it.
63 * (The cluster and node lists may be empty.)
Per Lidenb97bf3f2006-01-02 19:04:38 +010064 */
Allan Stephensb52124a2011-05-30 09:44:38 -040065struct name_info {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -040066 struct list_head node_list;
67 struct list_head cluster_list;
68 struct list_head zone_list;
Allan Stephens968edbe2008-07-14 22:45:33 -070069 u32 node_list_size;
70 u32 cluster_list_size;
71 u32 zone_list_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +010072};
73
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090074/**
Allan Stephensb52124a2011-05-30 09:44:38 -040075 * struct sub_seq - container for all published instances of a name sequence
76 * @lower: name sequence lower bound
77 * @upper: name sequence upper bound
78 * @info: pointer to name sequence publication info
79 */
Allan Stephensb52124a2011-05-30 09:44:38 -040080struct sub_seq {
81 u32 lower;
82 u32 upper;
83 struct name_info *info;
84};
85
86/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010087 * struct name_seq - container for all published instances of a name type
88 * @type: 32 bit 'type' value for name sequence
89 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
90 * sub-sequences are sorted in ascending order
91 * @alloc: number of sub-sequences currently in array
Allan Stephensf1310722006-06-25 23:51:37 -070092 * @first_free: array index of first unused sub-sequence entry
Per Lidenb97bf3f2006-01-02 19:04:38 +010093 * @ns_list: links to adjacent name sequences in hash chain
94 * @subscriptions: list of subscriptions for this 'type'
Allan Stephens307fdf52008-06-04 17:38:22 -070095 * @lock: spinlock controlling access to publication lists of all sub-sequences
Ying Xue97ede292014-12-02 15:00:30 +080096 * @rcu: RCU callback head used for deferred freeing
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010098struct name_seq {
99 u32 type;
100 struct sub_seq *sseqs;
101 u32 alloc;
102 u32 first_free;
103 struct hlist_node ns_list;
104 struct list_head subscriptions;
105 spinlock_t lock;
Ying Xue97ede292014-12-02 15:00:30 +0800106 struct rcu_head rcu;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107};
108
Ying Xue993bfe52014-12-02 15:00:24 +0800109struct name_table *tipc_nametbl;
Ying Xue97ede292014-12-02 15:00:30 +0800110DEFINE_SPINLOCK(tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111
Sam Ravnborg05790c62006-03-20 22:37:04 -0800112static int hash(int x)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113{
Ying Xuef046e7d2012-08-16 12:09:11 +0000114 return x & (TIPC_NAMETBL_SIZE - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115}
116
117/**
118 * publ_create - create a publication structure
119 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900120static struct publication *publ_create(u32 type, u32 lower, u32 upper,
121 u32 scope, u32 node, u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 u32 key)
123{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700124 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 if (publ == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400126 pr_warn("Publication creation failure, no memory\n");
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800127 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 }
129
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130 publ->type = type;
131 publ->lower = lower;
132 publ->upper = upper;
133 publ->scope = scope;
134 publ->node = node;
135 publ->ref = port_ref;
136 publ->key = key;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 INIT_LIST_HEAD(&publ->pport_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138 return publ;
139}
140
141/**
Per Liden4323add2006-01-18 00:38:21 +0100142 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800144static struct sub_seq *tipc_subseq_alloc(u32 cnt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145{
wangweidong0cee6bb2013-12-12 09:36:39 +0800146 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147}
148
149/**
Per Liden4323add2006-01-18 00:38:21 +0100150 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900151 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 * Allocates a single sub-sequence structure and sets it to all 0's.
153 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800154static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700156 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
Per Liden4323add2006-01-18 00:38:21 +0100157 struct sub_seq *sseq = tipc_subseq_alloc(1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158
159 if (!nseq || !sseq) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400160 pr_warn("Name sequence creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 kfree(nseq);
162 kfree(sseq);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800163 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 }
165
Ingo Molnar34af9462006-06-27 02:53:55 -0700166 spin_lock_init(&nseq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 nseq->type = type;
168 nseq->sseqs = sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 nseq->alloc = 1;
170 INIT_HLIST_NODE(&nseq->ns_list);
171 INIT_LIST_HEAD(&nseq->subscriptions);
Ying Xue97ede292014-12-02 15:00:30 +0800172 hlist_add_head_rcu(&nseq->ns_list, seq_head);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 return nseq;
174}
175
Ben Hutchings2c530402012-07-10 10:55:09 +0000176/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900178 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 * Very time-critical, so binary searches through sub-sequence array.
180 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800181static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
182 u32 instance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183{
184 struct sub_seq *sseqs = nseq->sseqs;
185 int low = 0;
186 int high = nseq->first_free - 1;
187 int mid;
188
189 while (low <= high) {
190 mid = (low + high) / 2;
191 if (instance < sseqs[mid].lower)
192 high = mid - 1;
193 else if (instance > sseqs[mid].upper)
194 low = mid + 1;
195 else
196 return &sseqs[mid];
197 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800198 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199}
200
201/**
202 * nameseq_locate_subseq - determine position of name instance in sub-sequence
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900203 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 * Returns index in sub-sequence array of the entry that contains the specified
205 * instance value; if no entry contains that value, returns the position
206 * where a new entry for it would be inserted in the array.
207 *
208 * Note: Similar to binary search code for locating a sub-sequence.
209 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
211{
212 struct sub_seq *sseqs = nseq->sseqs;
213 int low = 0;
214 int high = nseq->first_free - 1;
215 int mid;
216
217 while (low <= high) {
218 mid = (low + high) / 2;
219 if (instance < sseqs[mid].lower)
220 high = mid - 1;
221 else if (instance > sseqs[mid].upper)
222 low = mid + 1;
223 else
224 return mid;
225 }
226 return low;
227}
228
229/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400230 * tipc_nameseq_insert_publ
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800232static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
233 u32 type, u32 lower, u32 upper,
234 u32 scope, u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500236 struct tipc_subscription *s;
237 struct tipc_subscription *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 struct publication *publ;
239 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400240 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 int created_subseq = 0;
242
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 sseq = nameseq_find_subseq(nseq, lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 if (sseq) {
245
246 /* Lower end overlaps existing entry => need an exact match */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 if ((sseq->lower != lower) || (sseq->upper != upper)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800248 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400250
251 info = sseq->info;
Allan Stephensf80c24d2011-11-03 11:12:01 -0400252
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 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 } else {
260 u32 inspos;
261 struct sub_seq *freesseq;
262
263 /* Find where lower end should be inserted */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 inspos = nameseq_locate_subseq(nseq, lower);
265
266 /* Fail if upper end overlaps into an existing entry */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 if ((inspos < nseq->first_free) &&
268 (upper >= nseq->sseqs[inspos].lower)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800269 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 }
271
272 /* Ensure there is space for new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 if (nseq->first_free == nseq->alloc) {
Allan Stephens9ab230f2006-06-25 23:37:24 -0700274 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
275
276 if (!sseqs) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400277 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
278 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800279 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 }
Allan Stephens9ab230f2006-06-25 23:37:24 -0700281 memcpy(sseqs, nseq->sseqs,
282 nseq->alloc * sizeof(struct sub_seq));
283 kfree(nseq->sseqs);
284 nseq->sseqs = sseqs;
285 nseq->alloc *= 2;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287
Allan Stephensb52124a2011-05-30 09:44:38 -0400288 info = kzalloc(sizeof(*info), GFP_ATOMIC);
289 if (!info) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400290 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
291 type, lower, upper);
Allan Stephensb52124a2011-05-30 09:44:38 -0400292 return NULL;
293 }
294
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400295 INIT_LIST_HEAD(&info->node_list);
296 INIT_LIST_HEAD(&info->cluster_list);
297 INIT_LIST_HEAD(&info->zone_list);
298
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 /* Insert new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 sseq = &nseq->sseqs[inspos];
301 freesseq = &nseq->sseqs[nseq->first_free];
Allan Stephens0e659672010-12-31 18:59:32 +0000302 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
303 memset(sseq, 0, sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 nseq->first_free++;
305 sseq->lower = lower;
306 sseq->upper = upper;
Allan Stephensb52124a2011-05-30 09:44:38 -0400307 sseq->info = info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 created_subseq = 1;
309 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400311 /* Insert a publication */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 publ = publ_create(type, lower, upper, scope, node, port, key);
313 if (!publ)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800314 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400316 list_add(&publ->zone_list, &info->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400317 info->zone_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318
Allan Stephensd4f5c122012-04-17 18:16:34 -0400319 if (in_own_cluster(node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400320 list_add(&publ->cluster_list, &info->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400321 info->cluster_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 }
323
Allan Stephensd4f5c122012-04-17 18:16:34 -0400324 if (in_own_node(node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400325 list_add(&publ->node_list, &info->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400326 info->node_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 }
328
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400329 /* Any subscriptions waiting for notification? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Per Liden4323add2006-01-18 00:38:21 +0100331 tipc_subscr_report_overlap(s,
332 publ->lower,
333 publ->upper,
334 TIPC_PUBLISHED,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100336 publ->node,
337 created_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 }
339 return publ;
340}
341
342/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400343 * tipc_nameseq_remove_publ
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900344 *
Allan Stephensf1310722006-06-25 23:51:37 -0700345 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800353static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
354 u32 node, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
356 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
Allan Stephensb52124a2011-05-30 09:44:38 -0400358 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 struct sub_seq *free;
Paul Gortmakerfead3902011-12-29 20:43:44 -0500360 struct tipc_subscription *s, *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 int removed_subseq = 0;
362
Allan Stephensf1310722006-06-25 23:51:37 -0700363 if (!sseq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800364 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700365
Allan Stephensb52124a2011-05-30 09:44:38 -0400366 info = sseq->info;
367
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400368 /* Locate publication, if it exists */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400369 list_for_each_entry(publ, &info->zone_list, zone_list) {
370 if ((publ->key == key) && (publ->ref == ref) &&
371 (!publ->node || (publ->node == node)))
372 goto found;
373 }
374 return NULL;
375
376found:
Allan Stephensf1310722006-06-25 23:51:37 -0700377 /* Remove publication from zone scope list */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400378 list_del(&publ->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400379 info->zone_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380
Allan Stephensf1310722006-06-25 23:51:37 -0700381 /* Remove publication from cluster scope list, if present */
Allan Stephensd4f5c122012-04-17 18:16:34 -0400382 if (in_own_cluster(node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400383 list_del(&publ->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400384 info->cluster_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 }
Allan Stephensf1310722006-06-25 23:51:37 -0700386
387 /* Remove publication from node scope list, if present */
Allan Stephensd4f5c122012-04-17 18:16:34 -0400388 if (in_own_node(node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400389 list_del(&publ->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400390 info->node_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392
Allan Stephensf1310722006-06-25 23:51:37 -0700393 /* Contract subseq list if no more publications for that subseq */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400394 if (list_empty(&info->zone_list)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400395 kfree(info);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396 free = &nseq->sseqs[nseq->first_free--];
Allan Stephens0e659672010-12-31 18:59:32 +0000397 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 removed_subseq = 1;
399 }
400
Allan Stephensf1310722006-06-25 23:51:37 -0700401 /* Notify any waiting subscriptions */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Per Liden4323add2006-01-18 00:38:21 +0100403 tipc_subscr_report_overlap(s,
404 publ->lower,
405 publ->upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900406 TIPC_WITHDRAWN,
407 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100408 publ->node,
409 removed_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 }
Allan Stephensf1310722006-06-25 23:51:37 -0700411
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 return publ;
413}
414
415/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000416 * tipc_nameseq_subscribe - attach a subscription, and issue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 * the prescribed number of events if there is any sub-
418 * sequence overlapping with the requested sequence
419 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500420static void tipc_nameseq_subscribe(struct name_seq *nseq,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400421 struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422{
423 struct sub_seq *sseq = nseq->sseqs;
424
425 list_add(&s->nameseq_list, &nseq->subscriptions);
426
427 if (!sseq)
428 return;
429
430 while (sseq != &nseq->sseqs[nseq->first_free]) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400431 if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
432 struct publication *crs;
433 struct name_info *info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 int must_report = 1;
435
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400436 list_for_each_entry(crs, &info->zone_list, zone_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900437 tipc_subscr_report_overlap(s,
438 sseq->lower,
Per Liden4323add2006-01-18 00:38:21 +0100439 sseq->upper,
440 TIPC_PUBLISHED,
441 crs->ref,
442 crs->node,
443 must_report);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 must_report = 0;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400445 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 }
447 sseq++;
448 }
449}
450
451static struct name_seq *nametbl_find_seq(u32 type)
452{
453 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 struct name_seq *ns;
455
Ying Xue993bfe52014-12-02 15:00:24 +0800456 seq_head = &tipc_nametbl->seq_hlist[hash(type)];
Ying Xue97ede292014-12-02 15:00:30 +0800457 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
Allan Stephensb29f1422010-12-31 18:59:25 +0000458 if (ns->type == type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 return ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 }
461
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800462 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463};
464
Per Liden4323add2006-01-18 00:38:21 +0100465struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
466 u32 scope, u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467{
Ying Xuefb9962f2014-12-02 15:00:26 +0800468 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 struct name_seq *seq = nametbl_find_seq(type);
Ying Xue993bfe52014-12-02 15:00:24 +0800470 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471
Allan Stephens8f177892012-04-26 17:57:17 -0400472 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
473 (lower > upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400474 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
475 type, lower, upper, scope);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800476 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 }
478
Allan Stephensb29f1422010-12-31 18:59:25 +0000479 if (!seq)
Ying Xue993bfe52014-12-02 15:00:24 +0800480 seq = tipc_nameseq_create(type,
481 &tipc_nametbl->seq_hlist[index]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800483 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484
Ying Xuefb9962f2014-12-02 15:00:26 +0800485 spin_lock_bh(&seq->lock);
486 publ = tipc_nameseq_insert_publ(seq, type, lower, upper,
Per Liden4323add2006-01-18 00:38:21 +0100487 scope, node, port, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800488 spin_unlock_bh(&seq->lock);
489 return publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490}
491
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900492struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
Per Liden4323add2006-01-18 00:38:21 +0100493 u32 node, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494{
495 struct publication *publ;
496 struct name_seq *seq = nametbl_find_seq(type);
497
498 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800499 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500
Ying Xuefb9962f2014-12-02 15:00:26 +0800501 spin_lock_bh(&seq->lock);
Per Liden4323add2006-01-18 00:38:21 +0100502 publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800503 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800504 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800505 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800506 spin_unlock_bh(&seq->lock);
507 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800508 return publ;
509 }
510 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 return publ;
512}
513
Ben Hutchings2c530402012-07-10 10:55:09 +0000514/**
Allan Stephensbc9f8142011-11-07 17:00:54 -0500515 * tipc_nametbl_translate - perform name translation
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 *
Allan Stephensbc9f8142011-11-07 17:00:54 -0500517 * On entry, 'destnode' is the search domain used during translation.
518 *
519 * On exit:
520 * - if name translation is deferred to another node/cluster/zone,
521 * leaves 'destnode' unchanged (will be non-zero) and returns 0
522 * - if name translation is attempted and succeeds, sets 'destnode'
523 * to publishing node and returns port reference (will be non-zero)
524 * - if name translation is attempted and fails, sets 'destnode' to 0
525 * and returns 0
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 */
Per Liden4323add2006-01-18 00:38:21 +0100527u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528{
529 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400530 struct name_info *info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400531 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 struct name_seq *seq;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400533 u32 ref = 0;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500534 u32 node = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000536 if (!tipc_in_scope(*destnode, tipc_own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 return 0;
538
Ying Xue97ede292014-12-02 15:00:30 +0800539 rcu_read_lock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 seq = nametbl_find_seq(type);
541 if (unlikely(!seq))
542 goto not_found;
Ying Xuefb9962f2014-12-02 15:00:26 +0800543 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 sseq = nameseq_find_subseq(seq, instance);
545 if (unlikely(!sseq))
Ying Xuefb9962f2014-12-02 15:00:26 +0800546 goto no_match;
Allan Stephensb52124a2011-05-30 09:44:38 -0400547 info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400549 /* Closest-First Algorithm */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 if (likely(!*destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400551 if (!list_empty(&info->node_list)) {
552 publ = list_first_entry(&info->node_list,
553 struct publication,
554 node_list);
555 list_move_tail(&publ->node_list,
556 &info->node_list);
557 } else if (!list_empty(&info->cluster_list)) {
558 publ = list_first_entry(&info->cluster_list,
559 struct publication,
560 cluster_list);
561 list_move_tail(&publ->cluster_list,
562 &info->cluster_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400563 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400564 publ = list_first_entry(&info->zone_list,
565 struct publication,
566 zone_list);
567 list_move_tail(&publ->zone_list,
568 &info->zone_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400569 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 }
571
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400572 /* Round-Robin Algorithm */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 else if (*destnode == tipc_own_addr) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400574 if (list_empty(&info->node_list))
575 goto no_match;
576 publ = list_first_entry(&info->node_list, struct publication,
577 node_list);
578 list_move_tail(&publ->node_list, &info->node_list);
Allan Stephens336ebf52012-04-17 18:02:01 -0400579 } else if (in_own_cluster_exact(*destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400580 if (list_empty(&info->cluster_list))
581 goto no_match;
582 publ = list_first_entry(&info->cluster_list, struct publication,
583 cluster_list);
584 list_move_tail(&publ->cluster_list, &info->cluster_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400586 publ = list_first_entry(&info->zone_list, struct publication,
587 zone_list);
588 list_move_tail(&publ->zone_list, &info->zone_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400590
591 ref = publ->ref;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500592 node = publ->node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400593no_match:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 spin_unlock_bh(&seq->lock);
595not_found:
Ying Xue97ede292014-12-02 15:00:30 +0800596 rcu_read_unlock();
Allan Stephensbc9f8142011-11-07 17:00:54 -0500597 *destnode = node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400598 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599}
600
601/**
Per Liden4323add2006-01-18 00:38:21 +0100602 * tipc_nametbl_mc_translate - find multicast destinations
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 * Creates list of all local ports that overlap the given multicast address;
605 * also determines if any off-node ports overlap.
606 *
607 * Note: Publications with a scope narrower than 'limit' are ignored.
608 * (i.e. local node-scope publications mustn't receive messages arriving
609 * from another node, even if the multcast link brought it here)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * Returns non-zero if any off-node ports overlap
612 */
Per Liden4323add2006-01-18 00:38:21 +0100613int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
Paul Gortmaker45843102011-12-29 20:33:30 -0500614 struct tipc_port_list *dports)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615{
616 struct name_seq *seq;
617 struct sub_seq *sseq;
618 struct sub_seq *sseq_stop;
Allan Stephensb52124a2011-05-30 09:44:38 -0400619 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 int res = 0;
621
Ying Xue97ede292014-12-02 15:00:30 +0800622 rcu_read_lock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 seq = nametbl_find_seq(type);
624 if (!seq)
625 goto exit;
626
627 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
629 sseq_stop = seq->sseqs + seq->first_free;
630 for (; sseq != sseq_stop; sseq++) {
631 struct publication *publ;
632
633 if (sseq->lower > upper)
634 break;
Allan Stephens968edbe2008-07-14 22:45:33 -0700635
Allan Stephensb52124a2011-05-30 09:44:38 -0400636 info = sseq->info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400637 list_for_each_entry(publ, &info->node_list, node_list) {
638 if (publ->scope <= limit)
639 tipc_port_list_add(dports, publ->ref);
Allan Stephens968edbe2008-07-14 22:45:33 -0700640 }
641
Allan Stephensb52124a2011-05-30 09:44:38 -0400642 if (info->cluster_list_size != info->node_list_size)
Allan Stephens968edbe2008-07-14 22:45:33 -0700643 res = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 spin_unlock_bh(&seq->lock);
646exit:
Ying Xue97ede292014-12-02 15:00:30 +0800647 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 return res;
649}
650
Allan Stephensc422f1b2011-11-02 15:49:40 -0400651/*
Per Liden4323add2006-01-18 00:38:21 +0100652 * tipc_nametbl_publish - add name publication to network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 */
Ying Xuef2f98002015-01-09 15:27:05 +0800654struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
655 u32 upper, u32 scope, u32 port_ref,
656 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657{
658 struct publication *publ;
Ying Xueeab8c0452014-04-28 18:00:10 +0800659 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660
Ying Xue97ede292014-12-02 15:00:30 +0800661 spin_lock_bh(&tipc_nametbl_lock);
Ying Xue993bfe52014-12-02 15:00:24 +0800662 if (tipc_nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400663 pr_warn("Publication failed, local publication limit reached (%u)\n",
Ying Xuee6a04b12012-08-16 12:09:14 +0000664 TIPC_MAX_PUBLICATIONS);
Ying Xue97ede292014-12-02 15:00:30 +0800665 spin_unlock_bh(&tipc_nametbl_lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800666 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668
Per Liden4323add2006-01-18 00:38:21 +0100669 publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 tipc_own_addr, port_ref, key);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500671 if (likely(publ)) {
Ying Xue993bfe52014-12-02 15:00:24 +0800672 tipc_nametbl->local_publ_count++;
Ying Xueeab8c0452014-04-28 18:00:10 +0800673 buf = tipc_named_publish(publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200674 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800675 tipc_named_process_backlog(net);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500676 }
Ying Xue97ede292014-12-02 15:00:30 +0800677 spin_unlock_bh(&tipc_nametbl_lock);
Ying Xueeab8c0452014-04-28 18:00:10 +0800678
679 if (buf)
Ying Xuef2f98002015-01-09 15:27:05 +0800680 named_cluster_distribute(net, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 return publ;
682}
683
684/**
Per Liden4323add2006-01-18 00:38:21 +0100685 * tipc_nametbl_withdraw - withdraw name publication from network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 */
Ying Xuef2f98002015-01-09 15:27:05 +0800687int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
688 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689{
690 struct publication *publ;
Ying Xue54923902014-12-02 15:00:28 +0800691 struct sk_buff *skb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692
Ying Xue97ede292014-12-02 15:00:30 +0800693 spin_lock_bh(&tipc_nametbl_lock);
Per Liden4323add2006-01-18 00:38:21 +0100694 publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
Allan Stephensf1310722006-06-25 23:51:37 -0700695 if (likely(publ)) {
Ying Xue993bfe52014-12-02 15:00:24 +0800696 tipc_nametbl->local_publ_count--;
Ying Xue54923902014-12-02 15:00:28 +0800697 skb = tipc_named_withdraw(publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200698 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800699 tipc_named_process_backlog(net);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700 list_del_init(&publ->pport_list);
Ying Xue97ede292014-12-02 15:00:30 +0800701 kfree_rcu(publ, rcu);
Ying Xue54923902014-12-02 15:00:28 +0800702 } else {
703 pr_err("Unable to remove local publication\n"
704 "(type=%u, lower=%u, ref=%u, key=%u)\n",
705 type, lower, ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 }
Ying Xue97ede292014-12-02 15:00:30 +0800707 spin_unlock_bh(&tipc_nametbl_lock);
Ying Xue54923902014-12-02 15:00:28 +0800708
709 if (skb) {
Ying Xuef2f98002015-01-09 15:27:05 +0800710 named_cluster_distribute(net, skb);
Ying Xue54923902014-12-02 15:00:28 +0800711 return 1;
712 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 return 0;
714}
715
716/**
Per Liden4323add2006-01-18 00:38:21 +0100717 * tipc_nametbl_subscribe - add a subscription object to the name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500719void tipc_nametbl_subscribe(struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720{
721 u32 type = s->seq.type;
Ying Xue993bfe52014-12-02 15:00:24 +0800722 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723 struct name_seq *seq;
724
Ying Xue97ede292014-12-02 15:00:30 +0800725 spin_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 seq = nametbl_find_seq(type);
Allan Stephensa0168922010-12-31 18:59:35 +0000727 if (!seq)
Ying Xue993bfe52014-12-02 15:00:24 +0800728 seq = tipc_nameseq_create(type,
729 &tipc_nametbl->seq_hlist[index]);
Allan Stephens0e659672010-12-31 18:59:32 +0000730 if (seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900731 spin_lock_bh(&seq->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900732 tipc_nameseq_subscribe(seq, s);
733 spin_unlock_bh(&seq->lock);
734 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400735 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
736 s->seq.type, s->seq.lower, s->seq.upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 }
Ying Xue97ede292014-12-02 15:00:30 +0800738 spin_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739}
740
741/**
Per Liden4323add2006-01-18 00:38:21 +0100742 * tipc_nametbl_unsubscribe - remove a subscription object from name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500744void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745{
746 struct name_seq *seq;
747
Ying Xue97ede292014-12-02 15:00:30 +0800748 spin_lock_bh(&tipc_nametbl_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900749 seq = nametbl_find_seq(s->seq.type);
Allan Stephens0e659672010-12-31 18:59:32 +0000750 if (seq != NULL) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900751 spin_lock_bh(&seq->lock);
752 list_del_init(&s->nameseq_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800753 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800754 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800755 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800756 spin_unlock_bh(&seq->lock);
757 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800758 } else {
759 spin_unlock_bh(&seq->lock);
760 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 }
Ying Xue97ede292014-12-02 15:00:30 +0800762 spin_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763}
764
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000766 * subseq_list - print specified sub-sequence contents into the given buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400768static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400769 u32 index)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770{
771 char portIdStr[27];
Allan Stephensc2de5812010-08-17 11:00:14 +0000772 const char *scope_str[] = {"", " zone", " cluster", " node"};
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400773 struct publication *publ;
774 struct name_info *info;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400775 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776
Erik Hugnedc1aed32012-06-29 00:50:23 -0400777 ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400779 if (depth == 2) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400780 ret += tipc_snprintf(buf - ret, len + ret, "\n");
781 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 }
783
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400784 info = sseq->info;
785
786 list_for_each_entry(publ, &info->zone_list, zone_list) {
Allan Stephens0e659672010-12-31 18:59:32 +0000787 sprintf(portIdStr, "<%u.%u.%u:%u>",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 tipc_zone(publ->node), tipc_cluster(publ->node),
789 tipc_node(publ->node), publ->ref);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400790 ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 if (depth > 3) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400792 ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s",
793 publ->key, scope_str[publ->scope]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400795 if (!list_is_last(&publ->zone_list, &info->zone_list))
Erik Hugnedc1aed32012-06-29 00:50:23 -0400796 ret += tipc_snprintf(buf + ret, len - ret,
797 "\n%33s", " ");
Peter Senna Tschudinadccff32012-09-18 07:10:45 +0000798 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799
Erik Hugnedc1aed32012-06-29 00:50:23 -0400800 ret += tipc_snprintf(buf + ret, len - ret, "\n");
801 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802}
803
804/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000805 * nameseq_list - print specified name sequence contents into the given buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400807static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400808 u32 type, u32 lowbound, u32 upbound, u32 index)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809{
810 struct sub_seq *sseq;
811 char typearea[11];
Erik Hugnedc1aed32012-06-29 00:50:23 -0400812 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813
Allan Stephens0f15d362008-06-04 17:37:59 -0700814 if (seq->first_free == 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400815 return 0;
Allan Stephens0f15d362008-06-04 17:37:59 -0700816
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 sprintf(typearea, "%-10u", seq->type);
818
819 if (depth == 1) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400820 ret += tipc_snprintf(buf, len, "%s\n", typearea);
821 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 }
823
824 for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
825 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400826 ret += tipc_snprintf(buf + ret, len - ret, "%s ",
827 typearea);
Allan Stephens307fdf52008-06-04 17:38:22 -0700828 spin_lock_bh(&seq->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400829 ret += subseq_list(sseq, buf + ret, len - ret,
830 depth, index);
Allan Stephens307fdf52008-06-04 17:38:22 -0700831 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 sprintf(typearea, "%10s", " ");
833 }
834 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400835 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836}
837
838/**
839 * nametbl_header - print name table header into the given buffer
840 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400841static int nametbl_header(char *buf, int len, u32 depth)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842{
Allan Stephensc2de5812010-08-17 11:00:14 +0000843 const char *header[] = {
844 "Type ",
845 "Lower Upper ",
846 "Port Identity ",
847 "Publication Scope"
848 };
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849
Allan Stephensc2de5812010-08-17 11:00:14 +0000850 int i;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400851 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852
Allan Stephensc2de5812010-08-17 11:00:14 +0000853 if (depth > 4)
854 depth = 4;
855 for (i = 0; i < depth; i++)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400856 ret += tipc_snprintf(buf + ret, len - ret, header[i]);
857 ret += tipc_snprintf(buf + ret, len - ret, "\n");
858 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859}
860
861/**
862 * nametbl_list - print specified name table contents into the given buffer
863 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400864static int nametbl_list(char *buf, int len, u32 depth_info,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400865 u32 type, u32 lowbound, u32 upbound)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866{
867 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 struct name_seq *seq;
869 int all_types;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400870 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 u32 depth;
872 u32 i;
873
874 all_types = (depth_info & TIPC_NTQ_ALLTYPES);
875 depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
876
877 if (depth == 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400878 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879
880 if (all_types) {
881 /* display all entries in name table to specified depth */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400882 ret += nametbl_header(buf, len, depth);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 lowbound = 0;
884 upbound = ~0;
Ying Xuef046e7d2012-08-16 12:09:11 +0000885 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue993bfe52014-12-02 15:00:24 +0800886 seq_head = &tipc_nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +0800887 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400888 ret += nameseq_list(seq, buf + ret, len - ret,
889 depth, seq->type,
890 lowbound, upbound, i);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 }
892 }
893 } else {
894 /* display only the sequence that matches the specified type */
895 if (upbound < lowbound) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400896 ret += tipc_snprintf(buf + ret, len - ret,
897 "invalid name sequence specified\n");
898 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400900 ret += nametbl_header(buf + ret, len - ret, depth);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 i = hash(type);
Ying Xue993bfe52014-12-02 15:00:24 +0800902 seq_head = &tipc_nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +0800903 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 if (seq->type == type) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400905 ret += nameseq_list(seq, buf + ret, len - ret,
906 depth, type,
907 lowbound, upbound, i);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 break;
909 }
910 }
911 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400912 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913}
914
Per Liden4323add2006-01-18 00:38:21 +0100915struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916{
917 struct sk_buff *buf;
918 struct tipc_name_table_query *argv;
919 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400920 char *pb;
921 int pb_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 int str_len;
923
924 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
Per Liden4323add2006-01-18 00:38:21 +0100925 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926
Erik Hugnedc1aed32012-06-29 00:50:23 -0400927 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 if (!buf)
929 return NULL;
930
931 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400932 pb = TLV_DATA(rep_tlv);
933 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
Ying Xue97ede292014-12-02 15:00:30 +0800935 rcu_read_lock();
Erik Hugnedc1aed32012-06-29 00:50:23 -0400936 str_len = nametbl_list(pb, pb_len, ntohl(argv->depth),
937 ntohl(argv->type),
938 ntohl(argv->lowbound), ntohl(argv->upbound));
Ying Xue97ede292014-12-02 15:00:30 +0800939 rcu_read_unlock();
Erik Hugnedc1aed32012-06-29 00:50:23 -0400940 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 skb_put(buf, TLV_SPACE(str_len));
942 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
943
944 return buf;
945}
946
Per Liden4323add2006-01-18 00:38:21 +0100947int tipc_nametbl_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948{
Ying Xue993bfe52014-12-02 15:00:24 +0800949 int i;
950
951 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
952 if (!tipc_nametbl)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953 return -ENOMEM;
954
Ying Xue993bfe52014-12-02 15:00:24 +0800955 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
956 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
957
958 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
959 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
960 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961 return 0;
962}
963
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100964/**
965 * tipc_purge_publications - remove all publications for a given type
966 *
967 * tipc_nametbl_lock must be held when calling this function
968 */
969static void tipc_purge_publications(struct name_seq *seq)
970{
971 struct publication *publ, *safe;
972 struct sub_seq *sseq;
973 struct name_info *info;
974
Ying Xuefb9962f2014-12-02 15:00:26 +0800975 spin_lock_bh(&seq->lock);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100976 sseq = seq->sseqs;
977 info = sseq->info;
978 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
979 tipc_nametbl_remove_publ(publ->type, publ->lower, publ->node,
980 publ->ref, publ->key);
Ying Xue97ede292014-12-02 15:00:30 +0800981 kfree_rcu(publ, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100982 }
Ying Xue97ede292014-12-02 15:00:30 +0800983 hlist_del_init_rcu(&seq->ns_list);
984 kfree(seq->sseqs);
Ying Xue023160b2014-12-09 15:17:56 +0800985 spin_unlock_bh(&seq->lock);
Ying Xuefb9962f2014-12-02 15:00:26 +0800986
Ying Xue97ede292014-12-02 15:00:30 +0800987 kfree_rcu(seq, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100988}
989
Per Liden4323add2006-01-18 00:38:21 +0100990void tipc_nametbl_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 u32 i;
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100993 struct name_seq *seq;
994 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100996 /* Verify name table is empty and purge any lingering
997 * publications, then release the name table
998 */
Ying Xue97ede292014-12-02 15:00:30 +0800999 spin_lock_bh(&tipc_nametbl_lock);
Ying Xuef046e7d2012-08-16 12:09:11 +00001000 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue993bfe52014-12-02 15:00:24 +08001001 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
Paul Gortmakerf705ab92012-07-11 17:35:01 -04001002 continue;
Ying Xue993bfe52014-12-02 15:00:24 +08001003 seq_head = &tipc_nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +08001004 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Erik Hugne1bb8dce2014-03-06 14:40:20 +01001005 tipc_purge_publications(seq);
1006 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 }
Ying Xue97ede292014-12-02 15:00:30 +08001008 spin_unlock_bh(&tipc_nametbl_lock);
Ying Xue993bfe52014-12-02 15:00:24 +08001009
Ying Xue97ede292014-12-02 15:00:30 +08001010 synchronize_net();
Ying Xue993bfe52014-12-02 15:00:24 +08001011 kfree(tipc_nametbl);
1012
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013}
Richard Alpe15931232014-11-20 10:29:20 +01001014
Richard Alped8182802014-11-24 11:10:29 +01001015static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
1016 struct name_seq *seq,
1017 struct sub_seq *sseq, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +01001018{
1019 void *hdr;
1020 struct nlattr *attrs;
1021 struct nlattr *publ;
1022 struct publication *p;
1023
1024 if (*last_publ) {
1025 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
1026 if (p->key == *last_publ)
1027 break;
1028 if (p->key != *last_publ)
1029 return -EPIPE;
1030 } else {
1031 p = list_first_entry(&sseq->info->zone_list, struct publication,
1032 zone_list);
1033 }
1034
1035 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
1036 *last_publ = p->key;
1037
1038 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
1039 &tipc_genl_v2_family, NLM_F_MULTI,
1040 TIPC_NL_NAME_TABLE_GET);
1041 if (!hdr)
1042 return -EMSGSIZE;
1043
1044 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
1045 if (!attrs)
1046 goto msg_full;
1047
1048 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
1049 if (!publ)
1050 goto attr_msg_full;
1051
1052 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
1053 goto publ_msg_full;
1054 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
1055 goto publ_msg_full;
1056 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
1057 goto publ_msg_full;
1058 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
1059 goto publ_msg_full;
1060 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
1061 goto publ_msg_full;
1062 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
1063 goto publ_msg_full;
1064 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
1065 goto publ_msg_full;
1066
1067 nla_nest_end(msg->skb, publ);
1068 nla_nest_end(msg->skb, attrs);
1069 genlmsg_end(msg->skb, hdr);
1070 }
1071 *last_publ = 0;
1072
1073 return 0;
1074
1075publ_msg_full:
1076 nla_nest_cancel(msg->skb, publ);
1077attr_msg_full:
1078 nla_nest_cancel(msg->skb, attrs);
1079msg_full:
1080 genlmsg_cancel(msg->skb, hdr);
1081
1082 return -EMSGSIZE;
1083}
1084
Richard Alped8182802014-11-24 11:10:29 +01001085static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1086 u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +01001087{
1088 struct sub_seq *sseq;
1089 struct sub_seq *sseq_start;
1090 int err;
1091
1092 if (*last_lower) {
1093 sseq_start = nameseq_find_subseq(seq, *last_lower);
1094 if (!sseq_start)
1095 return -EPIPE;
1096 } else {
1097 sseq_start = seq->sseqs;
1098 }
1099
1100 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
1101 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
1102 if (err) {
1103 *last_lower = sseq->lower;
1104 return err;
1105 }
1106 }
1107 *last_lower = 0;
1108
1109 return 0;
1110}
1111
Richard Alped8182802014-11-24 11:10:29 +01001112static int __tipc_nl_seq_list(struct tipc_nl_msg *msg, u32 *last_type,
1113 u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +01001114{
1115 struct hlist_head *seq_head;
Ying Xue97ede292014-12-02 15:00:30 +08001116 struct name_seq *seq = NULL;
Richard Alpe15931232014-11-20 10:29:20 +01001117 int err;
1118 int i;
1119
1120 if (*last_type)
1121 i = hash(*last_type);
1122 else
1123 i = 0;
1124
1125 for (; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue993bfe52014-12-02 15:00:24 +08001126 seq_head = &tipc_nametbl->seq_hlist[i];
Richard Alpe15931232014-11-20 10:29:20 +01001127
1128 if (*last_type) {
1129 seq = nametbl_find_seq(*last_type);
1130 if (!seq)
1131 return -EPIPE;
1132 } else {
Ying Xue97ede292014-12-02 15:00:30 +08001133 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1134 break;
Richard Alpe15931232014-11-20 10:29:20 +01001135 if (!seq)
1136 continue;
1137 }
1138
Ying Xue97ede292014-12-02 15:00:30 +08001139 hlist_for_each_entry_from_rcu(seq, ns_list) {
Richard Alpe15931232014-11-20 10:29:20 +01001140 spin_lock_bh(&seq->lock);
Richard Alpe15931232014-11-20 10:29:20 +01001141 err = __tipc_nl_subseq_list(msg, seq, last_lower,
1142 last_publ);
1143
1144 if (err) {
1145 *last_type = seq->type;
1146 spin_unlock_bh(&seq->lock);
1147 return err;
1148 }
1149 spin_unlock_bh(&seq->lock);
1150 }
1151 *last_type = 0;
1152 }
1153 return 0;
1154}
1155
1156int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1157{
1158 int err;
1159 int done = cb->args[3];
1160 u32 last_type = cb->args[0];
1161 u32 last_lower = cb->args[1];
1162 u32 last_publ = cb->args[2];
1163 struct tipc_nl_msg msg;
1164
1165 if (done)
1166 return 0;
1167
1168 msg.skb = skb;
1169 msg.portid = NETLINK_CB(cb->skb).portid;
1170 msg.seq = cb->nlh->nlmsg_seq;
1171
Ying Xue97ede292014-12-02 15:00:30 +08001172 rcu_read_lock();
Richard Alpe15931232014-11-20 10:29:20 +01001173 err = __tipc_nl_seq_list(&msg, &last_type, &last_lower, &last_publ);
1174 if (!err) {
1175 done = 1;
1176 } else if (err != -EMSGSIZE) {
1177 /* We never set seq or call nl_dump_check_consistent() this
1178 * means that setting prev_seq here will cause the consistence
1179 * check to fail in the netlink callback handler. Resulting in
1180 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1181 * we got an error.
1182 */
1183 cb->prev_seq = 1;
1184 }
Ying Xue97ede292014-12-02 15:00:30 +08001185 rcu_read_unlock();
Richard Alpe15931232014-11-20 10:29:20 +01001186
1187 cb->args[0] = last_type;
1188 cb->args[1] = last_lower;
1189 cb->args[2] = last_publ;
1190 cb->args[3] = done;
1191
1192 return skb->len;
1193}