blob: 777b979b84634fbd98aa3ed49388c33e0a9472c7 [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"
Richard Alpe22ae7cf2015-02-09 09:50:18 +010039#include "netlink.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"
Richard Alpe22ae7cf2015-02-09 09:50:18 +010044#include "addr.h"
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -050045#include "node.h"
Richard Alpe22ae7cf2015-02-09 09:50:18 +010046#include <net/genetlink.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
Ying Xuef046e7d2012-08-16 12:09:11 +000048#define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Richard Alpe15931232014-11-20 10:29:20 +010050static const struct nla_policy
51tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = {
52 [TIPC_NLA_NAME_TABLE_UNSPEC] = { .type = NLA_UNSPEC },
53 [TIPC_NLA_NAME_TABLE_PUBL] = { .type = NLA_NESTED }
54};
55
Per Lidenb97bf3f2006-01-02 19:04:38 +010056/**
Allan Stephensb52124a2011-05-30 09:44:38 -040057 * struct name_info - name sequence publication info
Allan Stephens968edbe2008-07-14 22:45:33 -070058 * @node_list: circular list of publications made by own node
59 * @cluster_list: circular list of publications made by own cluster
60 * @zone_list: circular list of publications made by own zone
61 * @node_list_size: number of entries in "node_list"
62 * @cluster_list_size: number of entries in "cluster_list"
63 * @zone_list_size: number of entries in "zone_list"
64 *
65 * Note: The zone list always contains at least one entry, since all
66 * publications of the associated name sequence belong to it.
67 * (The cluster and node lists may be empty.)
Per Lidenb97bf3f2006-01-02 19:04:38 +010068 */
Allan Stephensb52124a2011-05-30 09:44:38 -040069struct name_info {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -040070 struct list_head node_list;
71 struct list_head cluster_list;
72 struct list_head zone_list;
Allan Stephens968edbe2008-07-14 22:45:33 -070073 u32 node_list_size;
74 u32 cluster_list_size;
75 u32 zone_list_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +010076};
77
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090078/**
Allan Stephensb52124a2011-05-30 09:44:38 -040079 * struct sub_seq - container for all published instances of a name sequence
80 * @lower: name sequence lower bound
81 * @upper: name sequence upper bound
82 * @info: pointer to name sequence publication info
83 */
Allan Stephensb52124a2011-05-30 09:44:38 -040084struct sub_seq {
85 u32 lower;
86 u32 upper;
87 struct name_info *info;
88};
89
90/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010091 * struct name_seq - container for all published instances of a name type
92 * @type: 32 bit 'type' value for name sequence
93 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
94 * sub-sequences are sorted in ascending order
95 * @alloc: number of sub-sequences currently in array
Allan Stephensf1310722006-06-25 23:51:37 -070096 * @first_free: array index of first unused sub-sequence entry
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 * @ns_list: links to adjacent name sequences in hash chain
98 * @subscriptions: list of subscriptions for this 'type'
Allan Stephens307fdf52008-06-04 17:38:22 -070099 * @lock: spinlock controlling access to publication lists of all sub-sequences
Ying Xue97ede292014-12-02 15:00:30 +0800100 * @rcu: RCU callback head used for deferred freeing
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102struct name_seq {
103 u32 type;
104 struct sub_seq *sseqs;
105 u32 alloc;
106 u32 first_free;
107 struct hlist_node ns_list;
108 struct list_head subscriptions;
109 spinlock_t lock;
Ying Xue97ede292014-12-02 15:00:30 +0800110 struct rcu_head rcu;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111};
112
Sam Ravnborg05790c62006-03-20 22:37:04 -0800113static int hash(int x)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114{
Ying Xuef046e7d2012-08-16 12:09:11 +0000115 return x & (TIPC_NAMETBL_SIZE - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116}
117
118/**
119 * publ_create - create a publication structure
120 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900121static struct publication *publ_create(u32 type, u32 lower, u32 upper,
122 u32 scope, u32 node, u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123 u32 key)
124{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700125 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126 if (publ == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400127 pr_warn("Publication creation failure, no memory\n");
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800128 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 }
130
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 publ->type = type;
132 publ->lower = lower;
133 publ->upper = upper;
134 publ->scope = scope;
135 publ->node = node;
136 publ->ref = port_ref;
137 publ->key = key;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138 INIT_LIST_HEAD(&publ->pport_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 return publ;
140}
141
142/**
Per Liden4323add2006-01-18 00:38:21 +0100143 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800145static struct sub_seq *tipc_subseq_alloc(u32 cnt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146{
wangweidong0cee6bb2013-12-12 09:36:39 +0800147 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148}
149
150/**
Per Liden4323add2006-01-18 00:38:21 +0100151 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 * Allocates a single sub-sequence structure and sets it to all 0's.
154 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800155static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700157 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
Per Liden4323add2006-01-18 00:38:21 +0100158 struct sub_seq *sseq = tipc_subseq_alloc(1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159
160 if (!nseq || !sseq) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400161 pr_warn("Name sequence creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 kfree(nseq);
163 kfree(sseq);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800164 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 }
166
Ingo Molnar34af9462006-06-27 02:53:55 -0700167 spin_lock_init(&nseq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 nseq->type = type;
169 nseq->sseqs = sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 nseq->alloc = 1;
171 INIT_HLIST_NODE(&nseq->ns_list);
172 INIT_LIST_HEAD(&nseq->subscriptions);
Ying Xue97ede292014-12-02 15:00:30 +0800173 hlist_add_head_rcu(&nseq->ns_list, seq_head);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 return nseq;
175}
176
Ben Hutchings2c530402012-07-10 10:55:09 +0000177/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900179 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 * Very time-critical, so binary searches through sub-sequence array.
181 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800182static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
183 u32 instance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184{
185 struct sub_seq *sseqs = nseq->sseqs;
186 int low = 0;
187 int high = nseq->first_free - 1;
188 int mid;
189
190 while (low <= high) {
191 mid = (low + high) / 2;
192 if (instance < sseqs[mid].lower)
193 high = mid - 1;
194 else if (instance > sseqs[mid].upper)
195 low = mid + 1;
196 else
197 return &sseqs[mid];
198 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800199 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200}
201
202/**
203 * nameseq_locate_subseq - determine position of name instance in sub-sequence
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900204 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205 * Returns index in sub-sequence array of the entry that contains the specified
206 * instance value; if no entry contains that value, returns the position
207 * where a new entry for it would be inserted in the array.
208 *
209 * Note: Similar to binary search code for locating a sub-sequence.
210 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
212{
213 struct sub_seq *sseqs = nseq->sseqs;
214 int low = 0;
215 int high = nseq->first_free - 1;
216 int mid;
217
218 while (low <= high) {
219 mid = (low + high) / 2;
220 if (instance < sseqs[mid].lower)
221 high = mid - 1;
222 else if (instance > sseqs[mid].upper)
223 low = mid + 1;
224 else
225 return mid;
226 }
227 return low;
228}
229
230/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400231 * tipc_nameseq_insert_publ
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 */
Ying Xue34747532015-01-09 15:27:10 +0800233static struct publication *tipc_nameseq_insert_publ(struct net *net,
234 struct name_seq *nseq,
235 u32 type, u32 lower,
236 u32 upper, u32 scope,
237 u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500239 struct tipc_subscription *s;
240 struct tipc_subscription *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 struct publication *publ;
242 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400243 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 int created_subseq = 0;
245
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 sseq = nameseq_find_subseq(nseq, lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 if (sseq) {
248
249 /* Lower end overlaps existing entry => need an exact match */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250 if ((sseq->lower != lower) || (sseq->upper != upper)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800251 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400253
254 info = sseq->info;
Allan Stephensf80c24d2011-11-03 11:12:01 -0400255
256 /* Check if an identical publication already exists */
257 list_for_each_entry(publ, &info->zone_list, zone_list) {
258 if ((publ->ref == port) && (publ->key == key) &&
259 (!publ->node || (publ->node == node)))
260 return NULL;
261 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 } else {
263 u32 inspos;
264 struct sub_seq *freesseq;
265
266 /* Find where lower end should be inserted */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 inspos = nameseq_locate_subseq(nseq, lower);
268
269 /* Fail if upper end overlaps into an existing entry */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 if ((inspos < nseq->first_free) &&
271 (upper >= nseq->sseqs[inspos].lower)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800272 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 }
274
275 /* Ensure there is space for new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 if (nseq->first_free == nseq->alloc) {
Allan Stephens9ab230f2006-06-25 23:37:24 -0700277 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
278
279 if (!sseqs) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400280 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
281 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800282 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 }
Allan Stephens9ab230f2006-06-25 23:37:24 -0700284 memcpy(sseqs, nseq->sseqs,
285 nseq->alloc * sizeof(struct sub_seq));
286 kfree(nseq->sseqs);
287 nseq->sseqs = sseqs;
288 nseq->alloc *= 2;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290
Allan Stephensb52124a2011-05-30 09:44:38 -0400291 info = kzalloc(sizeof(*info), GFP_ATOMIC);
292 if (!info) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400293 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
294 type, lower, upper);
Allan Stephensb52124a2011-05-30 09:44:38 -0400295 return NULL;
296 }
297
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400298 INIT_LIST_HEAD(&info->node_list);
299 INIT_LIST_HEAD(&info->cluster_list);
300 INIT_LIST_HEAD(&info->zone_list);
301
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 /* Insert new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 sseq = &nseq->sseqs[inspos];
304 freesseq = &nseq->sseqs[nseq->first_free];
Allan Stephens0e659672010-12-31 18:59:32 +0000305 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
306 memset(sseq, 0, sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 nseq->first_free++;
308 sseq->lower = lower;
309 sseq->upper = upper;
Allan Stephensb52124a2011-05-30 09:44:38 -0400310 sseq->info = info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 created_subseq = 1;
312 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400314 /* Insert a publication */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 publ = publ_create(type, lower, upper, scope, node, port, key);
316 if (!publ)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800317 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400319 list_add(&publ->zone_list, &info->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400320 info->zone_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321
Ying Xue34747532015-01-09 15:27:10 +0800322 if (in_own_cluster(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400323 list_add(&publ->cluster_list, &info->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400324 info->cluster_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 }
326
Ying Xue34747532015-01-09 15:27:10 +0800327 if (in_own_node(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400328 list_add(&publ->node_list, &info->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400329 info->node_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 }
331
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400332 /* Any subscriptions waiting for notification? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Ying Xue57f1d182015-05-04 10:36:44 +0800334 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
335 TIPC_PUBLISHED, publ->ref,
336 publ->node, created_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 }
338 return publ;
339}
340
341/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400342 * tipc_nameseq_remove_publ
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900343 *
Allan Stephensf1310722006-06-25 23:51:37 -0700344 * NOTE: There may be cases where TIPC is asked to remove a publication
345 * that is not in the name table. For example, if another node issues a
346 * publication for a name sequence that overlaps an existing name sequence
347 * the publication will not be recorded, which means the publication won't
348 * be found when the name sequence is later withdrawn by that node.
349 * A failed withdraw request simply returns a failure indication and lets the
350 * caller issue any error or warning messages associated with such a problem.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 */
Ying Xue34747532015-01-09 15:27:10 +0800352static struct publication *tipc_nameseq_remove_publ(struct net *net,
353 struct name_seq *nseq,
354 u32 inst, u32 node,
355 u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356{
357 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
Allan Stephensb52124a2011-05-30 09:44:38 -0400359 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 struct sub_seq *free;
Paul Gortmakerfead3902011-12-29 20:43:44 -0500361 struct tipc_subscription *s, *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 int removed_subseq = 0;
363
Allan Stephensf1310722006-06-25 23:51:37 -0700364 if (!sseq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800365 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700366
Allan Stephensb52124a2011-05-30 09:44:38 -0400367 info = sseq->info;
368
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400369 /* Locate publication, if it exists */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400370 list_for_each_entry(publ, &info->zone_list, zone_list) {
371 if ((publ->key == key) && (publ->ref == ref) &&
372 (!publ->node || (publ->node == node)))
373 goto found;
374 }
375 return NULL;
376
377found:
Allan Stephensf1310722006-06-25 23:51:37 -0700378 /* Remove publication from zone scope list */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400379 list_del(&publ->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400380 info->zone_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381
Allan Stephensf1310722006-06-25 23:51:37 -0700382 /* Remove publication from cluster scope list, if present */
Ying Xue34747532015-01-09 15:27:10 +0800383 if (in_own_cluster(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400384 list_del(&publ->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400385 info->cluster_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 }
Allan Stephensf1310722006-06-25 23:51:37 -0700387
388 /* Remove publication from node scope list, if present */
Ying Xue34747532015-01-09 15:27:10 +0800389 if (in_own_node(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400390 list_del(&publ->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400391 info->node_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393
Allan Stephensf1310722006-06-25 23:51:37 -0700394 /* Contract subseq list if no more publications for that subseq */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400395 if (list_empty(&info->zone_list)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400396 kfree(info);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 free = &nseq->sseqs[nseq->first_free--];
Allan Stephens0e659672010-12-31 18:59:32 +0000398 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 removed_subseq = 1;
400 }
401
Allan Stephensf1310722006-06-25 23:51:37 -0700402 /* Notify any waiting subscriptions */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Ying Xue57f1d182015-05-04 10:36:44 +0800404 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
405 TIPC_WITHDRAWN, publ->ref,
406 publ->node, removed_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 }
Allan Stephensf1310722006-06-25 23:51:37 -0700408
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 return publ;
410}
411
412/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000413 * tipc_nameseq_subscribe - attach a subscription, and issue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 * the prescribed number of events if there is any sub-
415 * sequence overlapping with the requested sequence
416 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500417static void tipc_nameseq_subscribe(struct name_seq *nseq,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400418 struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419{
420 struct sub_seq *sseq = nseq->sseqs;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100421 struct tipc_name_seq ns;
422
423 tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424
425 list_add(&s->nameseq_list, &nseq->subscriptions);
426
427 if (!sseq)
428 return;
429
430 while (sseq != &nseq->sseqs[nseq->first_free]) {
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100431 if (tipc_subscrp_check_overlap(&ns, sseq->lower, sseq->upper)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400432 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) {
Ying Xue57f1d182015-05-04 10:36:44 +0800437 tipc_subscrp_report_overlap(s, sseq->lower,
438 sseq->upper,
439 TIPC_PUBLISHED,
440 crs->ref, crs->node,
441 must_report);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 must_report = 0;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400443 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 }
445 sseq++;
446 }
447}
448
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800449static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800451 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 struct name_seq *ns;
454
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800455 seq_head = &tn->nametbl->seq_hlist[hash(type)];
Ying Xue97ede292014-12-02 15:00:30 +0800456 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
Allan Stephensb29f1422010-12-31 18:59:25 +0000457 if (ns->type == type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 return ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 }
460
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800461 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462};
463
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800464struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
465 u32 lower, u32 upper, u32 scope,
466 u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800468 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuefb9962f2014-12-02 15:00:26 +0800469 struct publication *publ;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800470 struct name_seq *seq = nametbl_find_seq(net, type);
Ying Xue993bfe52014-12-02 15:00:24 +0800471 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472
Allan Stephens8f177892012-04-26 17:57:17 -0400473 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
474 (lower > upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400475 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
476 type, lower, upper, scope);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800477 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478 }
479
Allan Stephensb29f1422010-12-31 18:59:25 +0000480 if (!seq)
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800481 seq = tipc_nameseq_create(type, &tn->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);
Ying Xue34747532015-01-09 15:27:10 +0800486 publ = tipc_nameseq_insert_publ(net, 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
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800492struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
493 u32 lower, u32 node, u32 ref,
494 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495{
496 struct publication *publ;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800497 struct name_seq *seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
499 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800500 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501
Ying Xuefb9962f2014-12-02 15:00:26 +0800502 spin_lock_bh(&seq->lock);
Ying Xue34747532015-01-09 15:27:10 +0800503 publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800504 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800505 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800506 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800507 spin_unlock_bh(&seq->lock);
508 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800509 return publ;
510 }
511 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 return publ;
513}
514
Ben Hutchings2c530402012-07-10 10:55:09 +0000515/**
Allan Stephensbc9f8142011-11-07 17:00:54 -0500516 * tipc_nametbl_translate - perform name translation
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 *
Allan Stephensbc9f8142011-11-07 17:00:54 -0500518 * On entry, 'destnode' is the search domain used during translation.
519 *
520 * On exit:
521 * - if name translation is deferred to another node/cluster/zone,
522 * leaves 'destnode' unchanged (will be non-zero) and returns 0
523 * - if name translation is attempted and succeeds, sets 'destnode'
524 * to publishing node and returns port reference (will be non-zero)
525 * - if name translation is attempted and fails, sets 'destnode' to 0
526 * and returns 0
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800528u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
529 u32 *destnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530{
Ying Xue34747532015-01-09 15:27:10 +0800531 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400533 struct name_info *info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400534 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 struct name_seq *seq;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400536 u32 ref = 0;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500537 u32 node = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538
Ying Xue34747532015-01-09 15:27:10 +0800539 if (!tipc_in_scope(*destnode, tn->own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 return 0;
541
Ying Xue97ede292014-12-02 15:00:30 +0800542 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800543 seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 if (unlikely(!seq))
545 goto not_found;
Ying Xuefb9962f2014-12-02 15:00:26 +0800546 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 sseq = nameseq_find_subseq(seq, instance);
548 if (unlikely(!sseq))
Ying Xuefb9962f2014-12-02 15:00:26 +0800549 goto no_match;
Allan Stephensb52124a2011-05-30 09:44:38 -0400550 info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400552 /* Closest-First Algorithm */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 if (likely(!*destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400554 if (!list_empty(&info->node_list)) {
555 publ = list_first_entry(&info->node_list,
556 struct publication,
557 node_list);
558 list_move_tail(&publ->node_list,
559 &info->node_list);
560 } else if (!list_empty(&info->cluster_list)) {
561 publ = list_first_entry(&info->cluster_list,
562 struct publication,
563 cluster_list);
564 list_move_tail(&publ->cluster_list,
565 &info->cluster_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400566 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400567 publ = list_first_entry(&info->zone_list,
568 struct publication,
569 zone_list);
570 list_move_tail(&publ->zone_list,
571 &info->zone_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400572 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 }
574
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400575 /* Round-Robin Algorithm */
Ying Xue34747532015-01-09 15:27:10 +0800576 else if (*destnode == tn->own_addr) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400577 if (list_empty(&info->node_list))
578 goto no_match;
579 publ = list_first_entry(&info->node_list, struct publication,
580 node_list);
581 list_move_tail(&publ->node_list, &info->node_list);
Ying Xue34747532015-01-09 15:27:10 +0800582 } else if (in_own_cluster_exact(net, *destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400583 if (list_empty(&info->cluster_list))
584 goto no_match;
585 publ = list_first_entry(&info->cluster_list, struct publication,
586 cluster_list);
587 list_move_tail(&publ->cluster_list, &info->cluster_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400589 publ = list_first_entry(&info->zone_list, struct publication,
590 zone_list);
591 list_move_tail(&publ->zone_list, &info->zone_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400593
594 ref = publ->ref;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500595 node = publ->node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400596no_match:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 spin_unlock_bh(&seq->lock);
598not_found:
Ying Xue97ede292014-12-02 15:00:30 +0800599 rcu_read_unlock();
Allan Stephensbc9f8142011-11-07 17:00:54 -0500600 *destnode = node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400601 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602}
603
604/**
Per Liden4323add2006-01-18 00:38:21 +0100605 * tipc_nametbl_mc_translate - find multicast destinations
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 * Creates list of all local ports that overlap the given multicast address;
608 * also determines if any off-node ports overlap.
609 *
610 * Note: Publications with a scope narrower than 'limit' are ignored.
611 * (i.e. local node-scope publications mustn't receive messages arriving
612 * from another node, even if the multcast link brought it here)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900613 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 * Returns non-zero if any off-node ports overlap
615 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800616int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper,
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500617 u32 limit, struct tipc_plist *dports)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618{
619 struct name_seq *seq;
620 struct sub_seq *sseq;
621 struct sub_seq *sseq_stop;
Allan Stephensb52124a2011-05-30 09:44:38 -0400622 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 int res = 0;
624
Ying Xue97ede292014-12-02 15:00:30 +0800625 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800626 seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 if (!seq)
628 goto exit;
629
630 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
632 sseq_stop = seq->sseqs + seq->first_free;
633 for (; sseq != sseq_stop; sseq++) {
634 struct publication *publ;
635
636 if (sseq->lower > upper)
637 break;
Allan Stephens968edbe2008-07-14 22:45:33 -0700638
Allan Stephensb52124a2011-05-30 09:44:38 -0400639 info = sseq->info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400640 list_for_each_entry(publ, &info->node_list, node_list) {
641 if (publ->scope <= limit)
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500642 tipc_plist_push(dports, publ->ref);
Allan Stephens968edbe2008-07-14 22:45:33 -0700643 }
644
Allan Stephensb52124a2011-05-30 09:44:38 -0400645 if (info->cluster_list_size != info->node_list_size)
Allan Stephens968edbe2008-07-14 22:45:33 -0700646 res = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 spin_unlock_bh(&seq->lock);
649exit:
Ying Xue97ede292014-12-02 15:00:30 +0800650 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 return res;
652}
653
Allan Stephensc422f1b2011-11-02 15:49:40 -0400654/*
Per Liden4323add2006-01-18 00:38:21 +0100655 * tipc_nametbl_publish - add name publication to network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 */
Ying Xuef2f98002015-01-09 15:27:05 +0800657struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
658 u32 upper, u32 scope, u32 port_ref,
659 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660{
661 struct publication *publ;
Ying Xueeab8c0452014-04-28 18:00:10 +0800662 struct sk_buff *buf = NULL;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800663 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800665 spin_lock_bh(&tn->nametbl_lock);
666 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400667 pr_warn("Publication failed, local publication limit reached (%u)\n",
Ying Xuee6a04b12012-08-16 12:09:14 +0000668 TIPC_MAX_PUBLICATIONS);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800669 spin_unlock_bh(&tn->nametbl_lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800670 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800673 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
Ying Xue34747532015-01-09 15:27:10 +0800674 tn->own_addr, port_ref, key);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500675 if (likely(publ)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800676 tn->nametbl->local_publ_count++;
677 buf = tipc_named_publish(net, publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200678 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800679 tipc_named_process_backlog(net);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500680 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800681 spin_unlock_bh(&tn->nametbl_lock);
Ying Xueeab8c0452014-04-28 18:00:10 +0800682
683 if (buf)
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500684 tipc_node_broadcast(net, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 return publ;
686}
687
688/**
Per Liden4323add2006-01-18 00:38:21 +0100689 * tipc_nametbl_withdraw - withdraw name publication from network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 */
Ying Xuef2f98002015-01-09 15:27:05 +0800691int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
692 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693{
694 struct publication *publ;
Ying Xue54923902014-12-02 15:00:28 +0800695 struct sk_buff *skb = NULL;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800696 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800698 spin_lock_bh(&tn->nametbl_lock);
Ying Xue34747532015-01-09 15:27:10 +0800699 publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800700 ref, key);
Allan Stephensf1310722006-06-25 23:51:37 -0700701 if (likely(publ)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800702 tn->nametbl->local_publ_count--;
Ying Xue34747532015-01-09 15:27:10 +0800703 skb = tipc_named_withdraw(net, publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200704 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800705 tipc_named_process_backlog(net);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 list_del_init(&publ->pport_list);
Ying Xue97ede292014-12-02 15:00:30 +0800707 kfree_rcu(publ, rcu);
Ying Xue54923902014-12-02 15:00:28 +0800708 } else {
709 pr_err("Unable to remove local publication\n"
710 "(type=%u, lower=%u, ref=%u, key=%u)\n",
711 type, lower, ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800713 spin_unlock_bh(&tn->nametbl_lock);
Ying Xue54923902014-12-02 15:00:28 +0800714
715 if (skb) {
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500716 tipc_node_broadcast(net, skb);
Ying Xue54923902014-12-02 15:00:28 +0800717 return 1;
718 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 return 0;
720}
721
722/**
Per Liden4323add2006-01-18 00:38:21 +0100723 * tipc_nametbl_subscribe - add a subscription object to the name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500725void tipc_nametbl_subscribe(struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800727 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100728 u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
Ying Xue993bfe52014-12-02 15:00:24 +0800729 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 struct name_seq *seq;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100731 struct tipc_name_seq ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800733 spin_lock_bh(&tn->nametbl_lock);
734 seq = nametbl_find_seq(s->net, type);
Allan Stephensa0168922010-12-31 18:59:35 +0000735 if (!seq)
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800736 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
Allan Stephens0e659672010-12-31 18:59:32 +0000737 if (seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900738 spin_lock_bh(&seq->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900739 tipc_nameseq_subscribe(seq, s);
740 spin_unlock_bh(&seq->lock);
741 } else {
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100742 tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400743 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100744 ns.type, ns.lower, ns.upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900745 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800746 spin_unlock_bh(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747}
748
749/**
Per Liden4323add2006-01-18 00:38:21 +0100750 * tipc_nametbl_unsubscribe - remove a subscription object from name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500752void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800754 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 struct name_seq *seq;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100756 u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800758 spin_lock_bh(&tn->nametbl_lock);
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100759 seq = nametbl_find_seq(s->net, type);
Allan Stephens0e659672010-12-31 18:59:32 +0000760 if (seq != NULL) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 spin_lock_bh(&seq->lock);
762 list_del_init(&s->nameseq_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800763 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800764 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800765 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800766 spin_unlock_bh(&seq->lock);
767 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800768 } else {
769 spin_unlock_bh(&seq->lock);
770 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900771 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800772 spin_unlock_bh(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773}
774
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800775int tipc_nametbl_init(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800777 struct tipc_net *tn = net_generic(net, tipc_net_id);
778 struct name_table *tipc_nametbl;
Ying Xue993bfe52014-12-02 15:00:24 +0800779 int i;
780
781 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
782 if (!tipc_nametbl)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 return -ENOMEM;
784
Ying Xue993bfe52014-12-02 15:00:24 +0800785 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
786 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
787
788 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
789 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
790 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800791 tn->nametbl = tipc_nametbl;
792 spin_lock_init(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 return 0;
794}
795
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100796/**
797 * tipc_purge_publications - remove all publications for a given type
798 *
799 * tipc_nametbl_lock must be held when calling this function
800 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800801static void tipc_purge_publications(struct net *net, struct name_seq *seq)
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100802{
803 struct publication *publ, *safe;
804 struct sub_seq *sseq;
805 struct name_info *info;
806
Ying Xuefb9962f2014-12-02 15:00:26 +0800807 spin_lock_bh(&seq->lock);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100808 sseq = seq->sseqs;
809 info = sseq->info;
810 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
Ying Xue84605042015-03-18 09:32:58 +0800811 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
812 publ->ref, publ->key);
Ying Xue97ede292014-12-02 15:00:30 +0800813 kfree_rcu(publ, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100814 }
Ying Xue97ede292014-12-02 15:00:30 +0800815 hlist_del_init_rcu(&seq->ns_list);
816 kfree(seq->sseqs);
Ying Xue023160b2014-12-09 15:17:56 +0800817 spin_unlock_bh(&seq->lock);
Ying Xuefb9962f2014-12-02 15:00:26 +0800818
Ying Xue97ede292014-12-02 15:00:30 +0800819 kfree_rcu(seq, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100820}
821
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800822void tipc_nametbl_stop(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 u32 i;
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100825 struct name_seq *seq;
826 struct hlist_head *seq_head;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800827 struct tipc_net *tn = net_generic(net, tipc_net_id);
828 struct name_table *tipc_nametbl = tn->nametbl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100830 /* Verify name table is empty and purge any lingering
831 * publications, then release the name table
832 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800833 spin_lock_bh(&tn->nametbl_lock);
Ying Xuef046e7d2012-08-16 12:09:11 +0000834 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue993bfe52014-12-02 15:00:24 +0800835 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
Paul Gortmakerf705ab92012-07-11 17:35:01 -0400836 continue;
Ying Xue993bfe52014-12-02 15:00:24 +0800837 seq_head = &tipc_nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +0800838 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800839 tipc_purge_publications(net, seq);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100840 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800842 spin_unlock_bh(&tn->nametbl_lock);
Ying Xue993bfe52014-12-02 15:00:24 +0800843
Ying Xue97ede292014-12-02 15:00:30 +0800844 synchronize_net();
Ying Xue993bfe52014-12-02 15:00:24 +0800845 kfree(tipc_nametbl);
846
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847}
Richard Alpe15931232014-11-20 10:29:20 +0100848
Richard Alped8182802014-11-24 11:10:29 +0100849static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
850 struct name_seq *seq,
851 struct sub_seq *sseq, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +0100852{
853 void *hdr;
854 struct nlattr *attrs;
855 struct nlattr *publ;
856 struct publication *p;
857
858 if (*last_publ) {
859 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
860 if (p->key == *last_publ)
861 break;
862 if (p->key != *last_publ)
863 return -EPIPE;
864 } else {
865 p = list_first_entry(&sseq->info->zone_list, struct publication,
866 zone_list);
867 }
868
869 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
870 *last_publ = p->key;
871
872 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100873 &tipc_genl_family, NLM_F_MULTI,
Richard Alpe15931232014-11-20 10:29:20 +0100874 TIPC_NL_NAME_TABLE_GET);
875 if (!hdr)
876 return -EMSGSIZE;
877
878 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
879 if (!attrs)
880 goto msg_full;
881
882 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
883 if (!publ)
884 goto attr_msg_full;
885
886 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
887 goto publ_msg_full;
888 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
889 goto publ_msg_full;
890 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
891 goto publ_msg_full;
892 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
893 goto publ_msg_full;
894 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
895 goto publ_msg_full;
896 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
897 goto publ_msg_full;
898 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
899 goto publ_msg_full;
900
901 nla_nest_end(msg->skb, publ);
902 nla_nest_end(msg->skb, attrs);
903 genlmsg_end(msg->skb, hdr);
904 }
905 *last_publ = 0;
906
907 return 0;
908
909publ_msg_full:
910 nla_nest_cancel(msg->skb, publ);
911attr_msg_full:
912 nla_nest_cancel(msg->skb, attrs);
913msg_full:
914 genlmsg_cancel(msg->skb, hdr);
915
916 return -EMSGSIZE;
917}
918
Richard Alped8182802014-11-24 11:10:29 +0100919static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
920 u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +0100921{
922 struct sub_seq *sseq;
923 struct sub_seq *sseq_start;
924 int err;
925
926 if (*last_lower) {
927 sseq_start = nameseq_find_subseq(seq, *last_lower);
928 if (!sseq_start)
929 return -EPIPE;
930 } else {
931 sseq_start = seq->sseqs;
932 }
933
934 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
935 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
936 if (err) {
937 *last_lower = sseq->lower;
938 return err;
939 }
940 }
941 *last_lower = 0;
942
943 return 0;
944}
945
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800946static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
947 u32 *last_type, u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +0100948{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800949 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe15931232014-11-20 10:29:20 +0100950 struct hlist_head *seq_head;
Ying Xue97ede292014-12-02 15:00:30 +0800951 struct name_seq *seq = NULL;
Richard Alpe15931232014-11-20 10:29:20 +0100952 int err;
953 int i;
954
955 if (*last_type)
956 i = hash(*last_type);
957 else
958 i = 0;
959
960 for (; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800961 seq_head = &tn->nametbl->seq_hlist[i];
Richard Alpe15931232014-11-20 10:29:20 +0100962
963 if (*last_type) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800964 seq = nametbl_find_seq(net, *last_type);
Richard Alpe15931232014-11-20 10:29:20 +0100965 if (!seq)
966 return -EPIPE;
967 } else {
Ying Xue97ede292014-12-02 15:00:30 +0800968 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
969 break;
Richard Alpe15931232014-11-20 10:29:20 +0100970 if (!seq)
971 continue;
972 }
973
Ying Xue97ede292014-12-02 15:00:30 +0800974 hlist_for_each_entry_from_rcu(seq, ns_list) {
Richard Alpe15931232014-11-20 10:29:20 +0100975 spin_lock_bh(&seq->lock);
Richard Alpe15931232014-11-20 10:29:20 +0100976 err = __tipc_nl_subseq_list(msg, seq, last_lower,
977 last_publ);
978
979 if (err) {
980 *last_type = seq->type;
981 spin_unlock_bh(&seq->lock);
982 return err;
983 }
984 spin_unlock_bh(&seq->lock);
985 }
986 *last_type = 0;
987 }
988 return 0;
989}
990
991int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
992{
993 int err;
994 int done = cb->args[3];
995 u32 last_type = cb->args[0];
996 u32 last_lower = cb->args[1];
997 u32 last_publ = cb->args[2];
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800998 struct net *net = sock_net(skb->sk);
Richard Alpe15931232014-11-20 10:29:20 +0100999 struct tipc_nl_msg msg;
1000
1001 if (done)
1002 return 0;
1003
1004 msg.skb = skb;
1005 msg.portid = NETLINK_CB(cb->skb).portid;
1006 msg.seq = cb->nlh->nlmsg_seq;
1007
Ying Xue97ede292014-12-02 15:00:30 +08001008 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001009 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
Richard Alpe15931232014-11-20 10:29:20 +01001010 if (!err) {
1011 done = 1;
1012 } else if (err != -EMSGSIZE) {
1013 /* We never set seq or call nl_dump_check_consistent() this
1014 * means that setting prev_seq here will cause the consistence
1015 * check to fail in the netlink callback handler. Resulting in
1016 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1017 * we got an error.
1018 */
1019 cb->prev_seq = 1;
1020 }
Ying Xue97ede292014-12-02 15:00:30 +08001021 rcu_read_unlock();
Richard Alpe15931232014-11-20 10:29:20 +01001022
1023 cb->args[0] = last_type;
1024 cb->args[1] = last_lower;
1025 cb->args[2] = last_publ;
1026 cb->args[3] = done;
1027
1028 return skb->len;
1029}
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001030
1031void tipc_plist_push(struct tipc_plist *pl, u32 port)
1032{
1033 struct tipc_plist *nl;
1034
1035 if (likely(!pl->port)) {
1036 pl->port = port;
1037 return;
1038 }
1039 if (pl->port == port)
1040 return;
1041 list_for_each_entry(nl, &pl->list, list) {
1042 if (nl->port == port)
1043 return;
1044 }
1045 nl = kmalloc(sizeof(*nl), GFP_ATOMIC);
1046 if (nl) {
1047 nl->port = port;
1048 list_add(&nl->list, &pl->list);
1049 }
1050}
1051
1052u32 tipc_plist_pop(struct tipc_plist *pl)
1053{
1054 struct tipc_plist *nl;
1055 u32 port = 0;
1056
1057 if (likely(list_empty(&pl->list))) {
1058 port = pl->port;
1059 pl->port = 0;
1060 return port;
1061 }
1062 nl = list_first_entry(&pl->list, typeof(*nl), list);
1063 port = nl->port;
1064 list_del(&nl->list);
1065 kfree(nl);
1066 return port;
1067}