blob: 2251912264a23103081a72f271c953d9ea1f3906 [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 *
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05004 * Copyright (c) 2000-2006, 2014-2015, 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
Ying Xue4ac1c8d2015-01-09 15:27:09 +080037#include <net/sock.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "core.h"
39#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_table.h"
41#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "subscr.h"
Ying Xue1da46562015-01-09 15:27:07 +080043#include "bcast.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010044
Ying Xuef046e7d2012-08-16 12:09:11 +000045#define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010046
Richard Alpe15931232014-11-20 10:29:20 +010047static 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010053/**
Allan Stephensb52124a2011-05-30 09:44:38 -040054 * struct name_info - name sequence publication info
Allan Stephens968edbe2008-07-14 22:45:33 -070055 * @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.)
Per Lidenb97bf3f2006-01-02 19:04:38 +010065 */
Allan Stephensb52124a2011-05-30 09:44:38 -040066struct name_info {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -040067 struct list_head node_list;
68 struct list_head cluster_list;
69 struct list_head zone_list;
Allan Stephens968edbe2008-07-14 22:45:33 -070070 u32 node_list_size;
71 u32 cluster_list_size;
72 u32 zone_list_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +010073};
74
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090075/**
Allan Stephensb52124a2011-05-30 09:44:38 -040076 * 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 */
Allan Stephensb52124a2011-05-30 09:44:38 -040081struct sub_seq {
82 u32 lower;
83 u32 upper;
84 struct name_info *info;
85};
86
87/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010088 * 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
Allan Stephensf1310722006-06-25 23:51:37 -070093 * @first_free: array index of first unused sub-sequence entry
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 * @ns_list: links to adjacent name sequences in hash chain
95 * @subscriptions: list of subscriptions for this 'type'
Allan Stephens307fdf52008-06-04 17:38:22 -070096 * @lock: spinlock controlling access to publication lists of all sub-sequences
Ying Xue97ede292014-12-02 15:00:30 +080097 * @rcu: RCU callback head used for deferred freeing
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010099struct 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;
Ying Xue97ede292014-12-02 15:00:30 +0800107 struct rcu_head rcu;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108};
109
Sam Ravnborg05790c62006-03-20 22:37:04 -0800110static int hash(int x)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111{
Ying Xuef046e7d2012-08-16 12:09:11 +0000112 return x & (TIPC_NAMETBL_SIZE - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113}
114
115/**
116 * publ_create - create a publication structure
117 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900118static struct publication *publ_create(u32 type, u32 lower, u32 upper,
119 u32 scope, u32 node, u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 u32 key)
121{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700122 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123 if (publ == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400124 pr_warn("Publication creation failure, no memory\n");
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800125 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126 }
127
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 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;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 INIT_LIST_HEAD(&publ->pport_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 return publ;
137}
138
139/**
Per Liden4323add2006-01-18 00:38:21 +0100140 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800142static struct sub_seq *tipc_subseq_alloc(u32 cnt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143{
wangweidong0cee6bb2013-12-12 09:36:39 +0800144 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145}
146
147/**
Per Liden4323add2006-01-18 00:38:21 +0100148 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900149 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 * Allocates a single sub-sequence structure and sets it to all 0's.
151 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800152static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700154 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
Per Liden4323add2006-01-18 00:38:21 +0100155 struct sub_seq *sseq = tipc_subseq_alloc(1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156
157 if (!nseq || !sseq) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400158 pr_warn("Name sequence creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 kfree(nseq);
160 kfree(sseq);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800161 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 }
163
Ingo Molnar34af9462006-06-27 02:53:55 -0700164 spin_lock_init(&nseq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 nseq->type = type;
166 nseq->sseqs = sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 nseq->alloc = 1;
168 INIT_HLIST_NODE(&nseq->ns_list);
169 INIT_LIST_HEAD(&nseq->subscriptions);
Ying Xue97ede292014-12-02 15:00:30 +0800170 hlist_add_head_rcu(&nseq->ns_list, seq_head);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 return nseq;
172}
173
Ben Hutchings2c530402012-07-10 10:55:09 +0000174/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900176 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 * Very time-critical, so binary searches through sub-sequence array.
178 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800179static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
180 u32 instance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181{
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 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800196 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197}
198
199/**
200 * nameseq_locate_subseq - determine position of name instance in sub-sequence
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900201 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 * 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 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208static 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/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400228 * tipc_nameseq_insert_publ
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 */
Ying Xue34747532015-01-09 15:27:10 +0800230static 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)
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
Ying Xue34747532015-01-09 15:27:10 +0800319 if (in_own_cluster(net, 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
Ying Xue34747532015-01-09 15:27:10 +0800324 if (in_own_node(net, 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 */
Ying Xue34747532015-01-09 15:27:10 +0800353static struct publication *tipc_nameseq_remove_publ(struct net *net,
354 struct name_seq *nseq,
355 u32 inst, u32 node,
356 u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357{
358 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
Allan Stephensb52124a2011-05-30 09:44:38 -0400360 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 struct sub_seq *free;
Paul Gortmakerfead3902011-12-29 20:43:44 -0500362 struct tipc_subscription *s, *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 int removed_subseq = 0;
364
Allan Stephensf1310722006-06-25 23:51:37 -0700365 if (!sseq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800366 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700367
Allan Stephensb52124a2011-05-30 09:44:38 -0400368 info = sseq->info;
369
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400370 /* Locate publication, if it exists */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400371 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;
377
378found:
Allan Stephensf1310722006-06-25 23:51:37 -0700379 /* Remove publication from zone scope list */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400380 list_del(&publ->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400381 info->zone_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382
Allan Stephensf1310722006-06-25 23:51:37 -0700383 /* Remove publication from cluster scope list, if present */
Ying Xue34747532015-01-09 15:27:10 +0800384 if (in_own_cluster(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400385 list_del(&publ->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400386 info->cluster_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 }
Allan Stephensf1310722006-06-25 23:51:37 -0700388
389 /* Remove publication from node scope list, if present */
Ying Xue34747532015-01-09 15:27:10 +0800390 if (in_own_node(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400391 list_del(&publ->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400392 info->node_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394
Allan Stephensf1310722006-06-25 23:51:37 -0700395 /* Contract subseq list if no more publications for that subseq */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400396 if (list_empty(&info->zone_list)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400397 kfree(info);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 free = &nseq->sseqs[nseq->first_free--];
Allan Stephens0e659672010-12-31 18:59:32 +0000399 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 removed_subseq = 1;
401 }
402
Allan Stephensf1310722006-06-25 23:51:37 -0700403 /* Notify any waiting subscriptions */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Per Liden4323add2006-01-18 00:38:21 +0100405 tipc_subscr_report_overlap(s,
406 publ->lower,
407 publ->upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900408 TIPC_WITHDRAWN,
409 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100410 publ->node,
411 removed_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 }
Allan Stephensf1310722006-06-25 23:51:37 -0700413
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 return publ;
415}
416
417/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000418 * tipc_nameseq_subscribe - attach a subscription, and issue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 * the prescribed number of events if there is any sub-
420 * sequence overlapping with the requested sequence
421 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500422static void tipc_nameseq_subscribe(struct name_seq *nseq,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400423 struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424{
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]) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400433 if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
434 struct publication *crs;
435 struct name_info *info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 int must_report = 1;
437
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400438 list_for_each_entry(crs, &info->zone_list, zone_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900439 tipc_subscr_report_overlap(s,
440 sseq->lower,
Per Liden4323add2006-01-18 00:38:21 +0100441 sseq->upper,
442 TIPC_PUBLISHED,
443 crs->ref,
444 crs->node,
445 must_report);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 must_report = 0;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400447 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 }
449 sseq++;
450 }
451}
452
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800453static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800455 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 struct name_seq *ns;
458
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800459 seq_head = &tn->nametbl->seq_hlist[hash(type)];
Ying Xue97ede292014-12-02 15:00:30 +0800460 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
Allan Stephensb29f1422010-12-31 18:59:25 +0000461 if (ns->type == type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 return ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 }
464
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800465 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466};
467
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800468struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
469 u32 lower, u32 upper, u32 scope,
470 u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800472 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuefb9962f2014-12-02 15:00:26 +0800473 struct publication *publ;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800474 struct name_seq *seq = nametbl_find_seq(net, type);
Ying Xue993bfe52014-12-02 15:00:24 +0800475 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476
Allan Stephens8f177892012-04-26 17:57:17 -0400477 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
478 (lower > upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400479 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
480 type, lower, upper, scope);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800481 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 }
483
Allan Stephensb29f1422010-12-31 18:59:25 +0000484 if (!seq)
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800485 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800487 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488
Ying Xuefb9962f2014-12-02 15:00:26 +0800489 spin_lock_bh(&seq->lock);
Ying Xue34747532015-01-09 15:27:10 +0800490 publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
Per Liden4323add2006-01-18 00:38:21 +0100491 scope, node, port, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800492 spin_unlock_bh(&seq->lock);
493 return publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494}
495
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800496struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
497 u32 lower, u32 node, u32 ref,
498 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499{
500 struct publication *publ;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800501 struct name_seq *seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502
503 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800504 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505
Ying Xuefb9962f2014-12-02 15:00:26 +0800506 spin_lock_bh(&seq->lock);
Ying Xue34747532015-01-09 15:27:10 +0800507 publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800508 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800509 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800510 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800511 spin_unlock_bh(&seq->lock);
512 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800513 return publ;
514 }
515 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 return publ;
517}
518
Ben Hutchings2c530402012-07-10 10:55:09 +0000519/**
Allan Stephensbc9f8142011-11-07 17:00:54 -0500520 * tipc_nametbl_translate - perform name translation
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 *
Allan Stephensbc9f8142011-11-07 17:00:54 -0500522 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800532u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
533 u32 *destnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534{
Ying Xue34747532015-01-09 15:27:10 +0800535 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400537 struct name_info *info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400538 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 struct name_seq *seq;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400540 u32 ref = 0;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500541 u32 node = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542
Ying Xue34747532015-01-09 15:27:10 +0800543 if (!tipc_in_scope(*destnode, tn->own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 return 0;
545
Ying Xue97ede292014-12-02 15:00:30 +0800546 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800547 seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 if (unlikely(!seq))
549 goto not_found;
Ying Xuefb9962f2014-12-02 15:00:26 +0800550 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 sseq = nameseq_find_subseq(seq, instance);
552 if (unlikely(!sseq))
Ying Xuefb9962f2014-12-02 15:00:26 +0800553 goto no_match;
Allan Stephensb52124a2011-05-30 09:44:38 -0400554 info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400556 /* Closest-First Algorithm */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 if (likely(!*destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400558 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);
Allan Stephens8af46382011-05-30 11:27:50 -0400570 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400571 publ = list_first_entry(&info->zone_list,
572 struct publication,
573 zone_list);
574 list_move_tail(&publ->zone_list,
575 &info->zone_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400576 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 }
578
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400579 /* Round-Robin Algorithm */
Ying Xue34747532015-01-09 15:27:10 +0800580 else if (*destnode == tn->own_addr) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400581 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);
Ying Xue34747532015-01-09 15:27:10 +0800586 } else if (in_own_cluster_exact(net, *destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400587 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);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400593 publ = list_first_entry(&info->zone_list, struct publication,
594 zone_list);
595 list_move_tail(&publ->zone_list, &info->zone_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400597
598 ref = publ->ref;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500599 node = publ->node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400600no_match:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 spin_unlock_bh(&seq->lock);
602not_found:
Ying Xue97ede292014-12-02 15:00:30 +0800603 rcu_read_unlock();
Allan Stephensbc9f8142011-11-07 17:00:54 -0500604 *destnode = node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400605 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606}
607
608/**
Per Liden4323add2006-01-18 00:38:21 +0100609 * tipc_nametbl_mc_translate - find multicast destinations
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * 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)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900617 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 * Returns non-zero if any off-node ports overlap
619 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800620int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper,
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500621 u32 limit, struct tipc_plist *dports)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622{
623 struct name_seq *seq;
624 struct sub_seq *sseq;
625 struct sub_seq *sseq_stop;
Allan Stephensb52124a2011-05-30 09:44:38 -0400626 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 int res = 0;
628
Ying Xue97ede292014-12-02 15:00:30 +0800629 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800630 seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 if (!seq)
632 goto exit;
633
634 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 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;
Allan Stephens968edbe2008-07-14 22:45:33 -0700642
Allan Stephensb52124a2011-05-30 09:44:38 -0400643 info = sseq->info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400644 list_for_each_entry(publ, &info->node_list, node_list) {
645 if (publ->scope <= limit)
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500646 tipc_plist_push(dports, publ->ref);
Allan Stephens968edbe2008-07-14 22:45:33 -0700647 }
648
Allan Stephensb52124a2011-05-30 09:44:38 -0400649 if (info->cluster_list_size != info->node_list_size)
Allan Stephens968edbe2008-07-14 22:45:33 -0700650 res = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 spin_unlock_bh(&seq->lock);
653exit:
Ying Xue97ede292014-12-02 15:00:30 +0800654 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 return res;
656}
657
Allan Stephensc422f1b2011-11-02 15:49:40 -0400658/*
Per Liden4323add2006-01-18 00:38:21 +0100659 * tipc_nametbl_publish - add name publication to network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 */
Ying Xuef2f98002015-01-09 15:27:05 +0800661struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
662 u32 upper, u32 scope, u32 port_ref,
663 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664{
665 struct publication *publ;
Ying Xueeab8c0452014-04-28 18:00:10 +0800666 struct sk_buff *buf = NULL;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800667 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800669 spin_lock_bh(&tn->nametbl_lock);
670 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400671 pr_warn("Publication failed, local publication limit reached (%u)\n",
Ying Xuee6a04b12012-08-16 12:09:14 +0000672 TIPC_MAX_PUBLICATIONS);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800673 spin_unlock_bh(&tn->nametbl_lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800674 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800677 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
Ying Xue34747532015-01-09 15:27:10 +0800678 tn->own_addr, port_ref, key);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500679 if (likely(publ)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800680 tn->nametbl->local_publ_count++;
681 buf = tipc_named_publish(net, publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200682 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800683 tipc_named_process_backlog(net);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500684 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800685 spin_unlock_bh(&tn->nametbl_lock);
Ying Xueeab8c0452014-04-28 18:00:10 +0800686
687 if (buf)
Ying Xuef2f98002015-01-09 15:27:05 +0800688 named_cluster_distribute(net, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 return publ;
690}
691
692/**
Per Liden4323add2006-01-18 00:38:21 +0100693 * tipc_nametbl_withdraw - withdraw name publication from network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694 */
Ying Xuef2f98002015-01-09 15:27:05 +0800695int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
696 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697{
698 struct publication *publ;
Ying Xue54923902014-12-02 15:00:28 +0800699 struct sk_buff *skb = NULL;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800700 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800702 spin_lock_bh(&tn->nametbl_lock);
Ying Xue34747532015-01-09 15:27:10 +0800703 publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800704 ref, key);
Allan Stephensf1310722006-06-25 23:51:37 -0700705 if (likely(publ)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800706 tn->nametbl->local_publ_count--;
Ying Xue34747532015-01-09 15:27:10 +0800707 skb = tipc_named_withdraw(net, publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200708 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800709 tipc_named_process_backlog(net);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 list_del_init(&publ->pport_list);
Ying Xue97ede292014-12-02 15:00:30 +0800711 kfree_rcu(publ, rcu);
Ying Xue54923902014-12-02 15:00:28 +0800712 } 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);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800717 spin_unlock_bh(&tn->nametbl_lock);
Ying Xue54923902014-12-02 15:00:28 +0800718
719 if (skb) {
Ying Xuef2f98002015-01-09 15:27:05 +0800720 named_cluster_distribute(net, skb);
Ying Xue54923902014-12-02 15:00:28 +0800721 return 1;
722 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723 return 0;
724}
725
726/**
Per Liden4323add2006-01-18 00:38:21 +0100727 * tipc_nametbl_subscribe - add a subscription object to the name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500729void tipc_nametbl_subscribe(struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800731 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 u32 type = s->seq.type;
Ying Xue993bfe52014-12-02 15:00:24 +0800733 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 struct name_seq *seq;
735
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800736 spin_lock_bh(&tn->nametbl_lock);
737 seq = nametbl_find_seq(s->net, type);
Allan Stephensa0168922010-12-31 18:59:35 +0000738 if (!seq)
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800739 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
Allan Stephens0e659672010-12-31 18:59:32 +0000740 if (seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900741 spin_lock_bh(&seq->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900742 tipc_nameseq_subscribe(seq, s);
743 spin_unlock_bh(&seq->lock);
744 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400745 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
746 s->seq.type, s->seq.lower, s->seq.upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900747 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800748 spin_unlock_bh(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749}
750
751/**
Per Liden4323add2006-01-18 00:38:21 +0100752 * tipc_nametbl_unsubscribe - remove a subscription object from name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500754void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800756 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757 struct name_seq *seq;
758
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800759 spin_lock_bh(&tn->nametbl_lock);
760 seq = nametbl_find_seq(s->net, s->seq.type);
Allan Stephens0e659672010-12-31 18:59:32 +0000761 if (seq != NULL) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900762 spin_lock_bh(&seq->lock);
763 list_del_init(&s->nameseq_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800764 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800765 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800766 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800767 spin_unlock_bh(&seq->lock);
768 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800769 } else {
770 spin_unlock_bh(&seq->lock);
771 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800773 spin_unlock_bh(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774}
775
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000777 * subseq_list - print specified sub-sequence contents into the given buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400779static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400780 u32 index)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781{
782 char portIdStr[27];
Allan Stephensc2de5812010-08-17 11:00:14 +0000783 const char *scope_str[] = {"", " zone", " cluster", " node"};
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400784 struct publication *publ;
785 struct name_info *info;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400786 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787
Erik Hugnedc1aed32012-06-29 00:50:23 -0400788 ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400790 if (depth == 2) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400791 ret += tipc_snprintf(buf - ret, len + ret, "\n");
792 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 }
794
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400795 info = sseq->info;
796
797 list_for_each_entry(publ, &info->zone_list, zone_list) {
Allan Stephens0e659672010-12-31 18:59:32 +0000798 sprintf(portIdStr, "<%u.%u.%u:%u>",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 tipc_zone(publ->node), tipc_cluster(publ->node),
800 tipc_node(publ->node), publ->ref);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400801 ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 if (depth > 3) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400803 ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s",
804 publ->key, scope_str[publ->scope]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400806 if (!list_is_last(&publ->zone_list, &info->zone_list))
Erik Hugnedc1aed32012-06-29 00:50:23 -0400807 ret += tipc_snprintf(buf + ret, len - ret,
808 "\n%33s", " ");
Peter Senna Tschudinadccff32012-09-18 07:10:45 +0000809 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810
Erik Hugnedc1aed32012-06-29 00:50:23 -0400811 ret += tipc_snprintf(buf + ret, len - ret, "\n");
812 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813}
814
815/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000816 * nameseq_list - print specified name sequence contents into the given buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400818static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400819 u32 type, u32 lowbound, u32 upbound, u32 index)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820{
821 struct sub_seq *sseq;
822 char typearea[11];
Erik Hugnedc1aed32012-06-29 00:50:23 -0400823 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824
Allan Stephens0f15d362008-06-04 17:37:59 -0700825 if (seq->first_free == 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400826 return 0;
Allan Stephens0f15d362008-06-04 17:37:59 -0700827
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 sprintf(typearea, "%-10u", seq->type);
829
830 if (depth == 1) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400831 ret += tipc_snprintf(buf, len, "%s\n", typearea);
832 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 }
834
835 for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
836 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400837 ret += tipc_snprintf(buf + ret, len - ret, "%s ",
838 typearea);
Allan Stephens307fdf52008-06-04 17:38:22 -0700839 spin_lock_bh(&seq->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400840 ret += subseq_list(sseq, buf + ret, len - ret,
841 depth, index);
Allan Stephens307fdf52008-06-04 17:38:22 -0700842 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 sprintf(typearea, "%10s", " ");
844 }
845 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400846 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847}
848
849/**
850 * nametbl_header - print name table header into the given buffer
851 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400852static int nametbl_header(char *buf, int len, u32 depth)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853{
Allan Stephensc2de5812010-08-17 11:00:14 +0000854 const char *header[] = {
855 "Type ",
856 "Lower Upper ",
857 "Port Identity ",
858 "Publication Scope"
859 };
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860
Allan Stephensc2de5812010-08-17 11:00:14 +0000861 int i;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400862 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863
Allan Stephensc2de5812010-08-17 11:00:14 +0000864 if (depth > 4)
865 depth = 4;
866 for (i = 0; i < depth; i++)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400867 ret += tipc_snprintf(buf + ret, len - ret, header[i]);
868 ret += tipc_snprintf(buf + ret, len - ret, "\n");
869 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870}
871
872/**
873 * nametbl_list - print specified name table contents into the given buffer
874 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800875static int nametbl_list(struct net *net, char *buf, int len, u32 depth_info,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400876 u32 type, u32 lowbound, u32 upbound)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800878 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 struct name_seq *seq;
881 int all_types;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400882 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 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)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400890 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891
892 if (all_types) {
893 /* display all entries in name table to specified depth */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400894 ret += nametbl_header(buf, len, depth);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 lowbound = 0;
896 upbound = ~0;
Ying Xuef046e7d2012-08-16 12:09:11 +0000897 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800898 seq_head = &tn->nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +0800899 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400900 ret += nameseq_list(seq, buf + ret, len - ret,
901 depth, seq->type,
902 lowbound, upbound, i);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 }
904 }
905 } else {
906 /* display only the sequence that matches the specified type */
907 if (upbound < lowbound) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400908 ret += tipc_snprintf(buf + ret, len - ret,
909 "invalid name sequence specified\n");
910 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400912 ret += nametbl_header(buf + ret, len - ret, depth);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 i = hash(type);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800914 seq_head = &tn->nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +0800915 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 if (seq->type == type) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400917 ret += nameseq_list(seq, buf + ret, len - ret,
918 depth, type,
919 lowbound, upbound, i);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 break;
921 }
922 }
923 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400924 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925}
926
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800927struct sk_buff *tipc_nametbl_get(struct net *net, const void *req_tlv_area,
928 int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929{
930 struct sk_buff *buf;
931 struct tipc_name_table_query *argv;
932 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400933 char *pb;
934 int pb_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 int str_len;
936
937 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
Per Liden4323add2006-01-18 00:38:21 +0100938 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939
Erik Hugnedc1aed32012-06-29 00:50:23 -0400940 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 if (!buf)
942 return NULL;
943
944 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400945 pb = TLV_DATA(rep_tlv);
946 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
Ying Xue97ede292014-12-02 15:00:30 +0800948 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800949 str_len = nametbl_list(net, pb, pb_len, ntohl(argv->depth),
Erik Hugnedc1aed32012-06-29 00:50:23 -0400950 ntohl(argv->type),
951 ntohl(argv->lowbound), ntohl(argv->upbound));
Ying Xue97ede292014-12-02 15:00:30 +0800952 rcu_read_unlock();
Erik Hugnedc1aed32012-06-29 00:50:23 -0400953 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 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
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800960int tipc_nametbl_init(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800962 struct tipc_net *tn = net_generic(net, tipc_net_id);
963 struct name_table *tipc_nametbl;
Ying Xue993bfe52014-12-02 15:00:24 +0800964 int i;
965
966 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
967 if (!tipc_nametbl)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 return -ENOMEM;
969
Ying Xue993bfe52014-12-02 15:00:24 +0800970 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]);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800976 tn->nametbl = tipc_nametbl;
977 spin_lock_init(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 return 0;
979}
980
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100981/**
982 * tipc_purge_publications - remove all publications for a given type
983 *
984 * tipc_nametbl_lock must be held when calling this function
985 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800986static void tipc_purge_publications(struct net *net, struct name_seq *seq)
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100987{
988 struct publication *publ, *safe;
989 struct sub_seq *sseq;
990 struct name_info *info;
991
Ying Xuefb9962f2014-12-02 15:00:26 +0800992 spin_lock_bh(&seq->lock);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100993 sseq = seq->sseqs;
994 info = sseq->info;
995 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800996 tipc_nametbl_remove_publ(net, publ->type, publ->lower,
997 publ->node, publ->ref, publ->key);
Ying Xue97ede292014-12-02 15:00:30 +0800998 kfree_rcu(publ, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100999 }
Ying Xue97ede292014-12-02 15:00:30 +08001000 hlist_del_init_rcu(&seq->ns_list);
1001 kfree(seq->sseqs);
Ying Xue023160b2014-12-09 15:17:56 +08001002 spin_unlock_bh(&seq->lock);
Ying Xuefb9962f2014-12-02 15:00:26 +08001003
Ying Xue97ede292014-12-02 15:00:30 +08001004 kfree_rcu(seq, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +01001005}
1006
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001007void tipc_nametbl_stop(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 u32 i;
Erik Hugne1bb8dce2014-03-06 14:40:20 +01001010 struct name_seq *seq;
1011 struct hlist_head *seq_head;
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001012 struct tipc_net *tn = net_generic(net, tipc_net_id);
1013 struct name_table *tipc_nametbl = tn->nametbl;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014
Erik Hugne1bb8dce2014-03-06 14:40:20 +01001015 /* Verify name table is empty and purge any lingering
1016 * publications, then release the name table
1017 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001018 spin_lock_bh(&tn->nametbl_lock);
Ying Xuef046e7d2012-08-16 12:09:11 +00001019 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue993bfe52014-12-02 15:00:24 +08001020 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
Paul Gortmakerf705ab92012-07-11 17:35:01 -04001021 continue;
Ying Xue993bfe52014-12-02 15:00:24 +08001022 seq_head = &tipc_nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +08001023 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001024 tipc_purge_publications(net, seq);
Erik Hugne1bb8dce2014-03-06 14:40:20 +01001025 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001027 spin_unlock_bh(&tn->nametbl_lock);
Ying Xue993bfe52014-12-02 15:00:24 +08001028
Ying Xue97ede292014-12-02 15:00:30 +08001029 synchronize_net();
Ying Xue993bfe52014-12-02 15:00:24 +08001030 kfree(tipc_nametbl);
1031
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032}
Richard Alpe15931232014-11-20 10:29:20 +01001033
Richard Alped8182802014-11-24 11:10:29 +01001034static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
1035 struct name_seq *seq,
1036 struct sub_seq *sseq, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +01001037{
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,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001058 &tipc_genl_family, NLM_F_MULTI,
Richard Alpe15931232014-11-20 10:29:20 +01001059 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
Richard Alped8182802014-11-24 11:10:29 +01001104static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1105 u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +01001106{
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
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001131static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
1132 u32 *last_type, u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +01001133{
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001134 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe15931232014-11-20 10:29:20 +01001135 struct hlist_head *seq_head;
Ying Xue97ede292014-12-02 15:00:30 +08001136 struct name_seq *seq = NULL;
Richard Alpe15931232014-11-20 10:29:20 +01001137 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++) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001146 seq_head = &tn->nametbl->seq_hlist[i];
Richard Alpe15931232014-11-20 10:29:20 +01001147
1148 if (*last_type) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001149 seq = nametbl_find_seq(net, *last_type);
Richard Alpe15931232014-11-20 10:29:20 +01001150 if (!seq)
1151 return -EPIPE;
1152 } else {
Ying Xue97ede292014-12-02 15:00:30 +08001153 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1154 break;
Richard Alpe15931232014-11-20 10:29:20 +01001155 if (!seq)
1156 continue;
1157 }
1158
Ying Xue97ede292014-12-02 15:00:30 +08001159 hlist_for_each_entry_from_rcu(seq, ns_list) {
Richard Alpe15931232014-11-20 10:29:20 +01001160 spin_lock_bh(&seq->lock);
Richard Alpe15931232014-11-20 10:29:20 +01001161 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];
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001183 struct net *net = sock_net(skb->sk);
Richard Alpe15931232014-11-20 10:29:20 +01001184 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
Ying Xue97ede292014-12-02 15:00:30 +08001193 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001194 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
Richard Alpe15931232014-11-20 10:29:20 +01001195 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 }
Ying Xue97ede292014-12-02 15:00:30 +08001206 rcu_read_unlock();
Richard Alpe15931232014-11-20 10:29:20 +01001207
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}
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001215
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}