blob: e01c9c691ba2e115d77e906954f8011eed936c36 [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"
Jon Maloy75da2162017-10-13 11:04:23 +020046#include "group.h"
Richard Alpe22ae7cf2015-02-09 09:50:18 +010047#include <net/genetlink.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Ying Xuef046e7d2012-08-16 12:09:11 +000049#define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
51/**
Allan Stephensb52124a2011-05-30 09:44:38 -040052 * struct name_info - name sequence publication info
Allan Stephens968edbe2008-07-14 22:45:33 -070053 * @node_list: circular list of publications made by own node
54 * @cluster_list: circular list of publications made by own cluster
55 * @zone_list: circular list of publications made by own zone
56 * @node_list_size: number of entries in "node_list"
57 * @cluster_list_size: number of entries in "cluster_list"
58 * @zone_list_size: number of entries in "zone_list"
59 *
60 * Note: The zone list always contains at least one entry, since all
61 * publications of the associated name sequence belong to it.
62 * (The cluster and node lists may be empty.)
Per Lidenb97bf3f2006-01-02 19:04:38 +010063 */
Allan Stephensb52124a2011-05-30 09:44:38 -040064struct name_info {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -040065 struct list_head node_list;
66 struct list_head cluster_list;
67 struct list_head zone_list;
Allan Stephens968edbe2008-07-14 22:45:33 -070068 u32 node_list_size;
69 u32 cluster_list_size;
70 u32 zone_list_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +010071};
72
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090073/**
Allan Stephensb52124a2011-05-30 09:44:38 -040074 * struct sub_seq - container for all published instances of a name sequence
75 * @lower: name sequence lower bound
76 * @upper: name sequence upper bound
77 * @info: pointer to name sequence publication info
78 */
Allan Stephensb52124a2011-05-30 09:44:38 -040079struct sub_seq {
80 u32 lower;
81 u32 upper;
82 struct name_info *info;
83};
84
85/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010086 * struct name_seq - container for all published instances of a name type
87 * @type: 32 bit 'type' value for name sequence
88 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
89 * sub-sequences are sorted in ascending order
90 * @alloc: number of sub-sequences currently in array
Allan Stephensf1310722006-06-25 23:51:37 -070091 * @first_free: array index of first unused sub-sequence entry
Per Lidenb97bf3f2006-01-02 19:04:38 +010092 * @ns_list: links to adjacent name sequences in hash chain
93 * @subscriptions: list of subscriptions for this 'type'
Allan Stephens307fdf52008-06-04 17:38:22 -070094 * @lock: spinlock controlling access to publication lists of all sub-sequences
Ying Xue97ede292014-12-02 15:00:30 +080095 * @rcu: RCU callback head used for deferred freeing
Per Lidenb97bf3f2006-01-02 19:04:38 +010096 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010097struct name_seq {
98 u32 type;
99 struct sub_seq *sseqs;
100 u32 alloc;
101 u32 first_free;
102 struct hlist_node ns_list;
103 struct list_head subscriptions;
104 spinlock_t lock;
Ying Xue97ede292014-12-02 15:00:30 +0800105 struct rcu_head rcu;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106};
107
Sam Ravnborg05790c62006-03-20 22:37:04 -0800108static int hash(int x)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109{
Ying Xuef046e7d2012-08-16 12:09:11 +0000110 return x & (TIPC_NAMETBL_SIZE - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111}
112
113/**
114 * publ_create - create a publication structure
115 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900116static struct publication *publ_create(u32 type, u32 lower, u32 upper,
117 u32 scope, u32 node, u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 u32 key)
119{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700120 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 if (publ == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400122 pr_warn("Publication creation failure, no memory\n");
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800123 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 }
125
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126 publ->type = type;
127 publ->lower = lower;
128 publ->upper = upper;
129 publ->scope = scope;
130 publ->node = node;
131 publ->ref = port_ref;
132 publ->key = key;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133 INIT_LIST_HEAD(&publ->pport_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 return publ;
135}
136
137/**
Per Liden4323add2006-01-18 00:38:21 +0100138 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800140static struct sub_seq *tipc_subseq_alloc(u32 cnt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141{
wangweidong0cee6bb2013-12-12 09:36:39 +0800142 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143}
144
145/**
Per Liden4323add2006-01-18 00:38:21 +0100146 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900147 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 * Allocates a single sub-sequence structure and sets it to all 0's.
149 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800150static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700152 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
Per Liden4323add2006-01-18 00:38:21 +0100153 struct sub_seq *sseq = tipc_subseq_alloc(1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154
155 if (!nseq || !sseq) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400156 pr_warn("Name sequence creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 kfree(nseq);
158 kfree(sseq);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800159 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 }
161
Ingo Molnar34af9462006-06-27 02:53:55 -0700162 spin_lock_init(&nseq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 nseq->type = type;
164 nseq->sseqs = sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 nseq->alloc = 1;
166 INIT_HLIST_NODE(&nseq->ns_list);
167 INIT_LIST_HEAD(&nseq->subscriptions);
Ying Xue97ede292014-12-02 15:00:30 +0800168 hlist_add_head_rcu(&nseq->ns_list, seq_head);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 return nseq;
170}
171
Ben Hutchings2c530402012-07-10 10:55:09 +0000172/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900174 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 * Very time-critical, so binary searches through sub-sequence array.
176 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800177static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
178 u32 instance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179{
180 struct sub_seq *sseqs = nseq->sseqs;
181 int low = 0;
182 int high = nseq->first_free - 1;
183 int mid;
184
185 while (low <= high) {
186 mid = (low + high) / 2;
187 if (instance < sseqs[mid].lower)
188 high = mid - 1;
189 else if (instance > sseqs[mid].upper)
190 low = mid + 1;
191 else
192 return &sseqs[mid];
193 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800194 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195}
196
197/**
198 * nameseq_locate_subseq - determine position of name instance in sub-sequence
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900199 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 * Returns index in sub-sequence array of the entry that contains the specified
201 * instance value; if no entry contains that value, returns the position
202 * where a new entry for it would be inserted in the array.
203 *
204 * Note: Similar to binary search code for locating a sub-sequence.
205 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
207{
208 struct sub_seq *sseqs = nseq->sseqs;
209 int low = 0;
210 int high = nseq->first_free - 1;
211 int mid;
212
213 while (low <= high) {
214 mid = (low + high) / 2;
215 if (instance < sseqs[mid].lower)
216 high = mid - 1;
217 else if (instance > sseqs[mid].upper)
218 low = mid + 1;
219 else
220 return mid;
221 }
222 return low;
223}
224
225/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400226 * tipc_nameseq_insert_publ
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 */
Ying Xue34747532015-01-09 15:27:10 +0800228static struct publication *tipc_nameseq_insert_publ(struct net *net,
229 struct name_seq *nseq,
230 u32 type, u32 lower,
231 u32 upper, u32 scope,
232 u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500234 struct tipc_subscription *s;
235 struct tipc_subscription *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 struct publication *publ;
237 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400238 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 int created_subseq = 0;
240
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 sseq = nameseq_find_subseq(nseq, lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 if (sseq) {
243
244 /* Lower end overlaps existing entry => need an exact match */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 if ((sseq->lower != lower) || (sseq->upper != upper)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800246 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400248
249 info = sseq->info;
Allan Stephensf80c24d2011-11-03 11:12:01 -0400250
251 /* Check if an identical publication already exists */
252 list_for_each_entry(publ, &info->zone_list, zone_list) {
253 if ((publ->ref == port) && (publ->key == key) &&
254 (!publ->node || (publ->node == node)))
255 return NULL;
256 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 } else {
258 u32 inspos;
259 struct sub_seq *freesseq;
260
261 /* Find where lower end should be inserted */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 inspos = nameseq_locate_subseq(nseq, lower);
263
264 /* Fail if upper end overlaps into an existing entry */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 if ((inspos < nseq->first_free) &&
266 (upper >= nseq->sseqs[inspos].lower)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800267 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 }
269
270 /* Ensure there is space for new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 if (nseq->first_free == nseq->alloc) {
Allan Stephens9ab230f2006-06-25 23:37:24 -0700272 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
273
274 if (!sseqs) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400275 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
276 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800277 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 }
Allan Stephens9ab230f2006-06-25 23:37:24 -0700279 memcpy(sseqs, nseq->sseqs,
280 nseq->alloc * sizeof(struct sub_seq));
281 kfree(nseq->sseqs);
282 nseq->sseqs = sseqs;
283 nseq->alloc *= 2;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285
Allan Stephensb52124a2011-05-30 09:44:38 -0400286 info = kzalloc(sizeof(*info), GFP_ATOMIC);
287 if (!info) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400288 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
289 type, lower, upper);
Allan Stephensb52124a2011-05-30 09:44:38 -0400290 return NULL;
291 }
292
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400293 INIT_LIST_HEAD(&info->node_list);
294 INIT_LIST_HEAD(&info->cluster_list);
295 INIT_LIST_HEAD(&info->zone_list);
296
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 /* Insert new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 sseq = &nseq->sseqs[inspos];
299 freesseq = &nseq->sseqs[nseq->first_free];
Allan Stephens0e659672010-12-31 18:59:32 +0000300 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
301 memset(sseq, 0, sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 nseq->first_free++;
303 sseq->lower = lower;
304 sseq->upper = upper;
Allan Stephensb52124a2011-05-30 09:44:38 -0400305 sseq->info = info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 created_subseq = 1;
307 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400309 /* Insert a publication */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 publ = publ_create(type, lower, upper, scope, node, port, key);
311 if (!publ)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800312 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400314 list_add(&publ->zone_list, &info->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400315 info->zone_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316
Ying Xue34747532015-01-09 15:27:10 +0800317 if (in_own_cluster(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400318 list_add(&publ->cluster_list, &info->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400319 info->cluster_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 }
321
Ying Xue34747532015-01-09 15:27:10 +0800322 if (in_own_node(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400323 list_add(&publ->node_list, &info->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400324 info->node_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 }
326
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400327 /* Any subscriptions waiting for notification? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Jon Maloyda0a75e2018-02-15 10:40:48 +0100329 tipc_sub_report_overlap(s, publ->lower, publ->upper,
330 TIPC_PUBLISHED, publ->ref,
331 publ->node, publ->scope,
332 created_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 }
334 return publ;
335}
336
337/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400338 * tipc_nameseq_remove_publ
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339 *
Allan Stephensf1310722006-06-25 23:51:37 -0700340 * NOTE: There may be cases where TIPC is asked to remove a publication
341 * that is not in the name table. For example, if another node issues a
342 * publication for a name sequence that overlaps an existing name sequence
343 * the publication will not be recorded, which means the publication won't
344 * be found when the name sequence is later withdrawn by that node.
345 * A failed withdraw request simply returns a failure indication and lets the
346 * caller issue any error or warning messages associated with such a problem.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 */
Ying Xue34747532015-01-09 15:27:10 +0800348static struct publication *tipc_nameseq_remove_publ(struct net *net,
349 struct name_seq *nseq,
350 u32 inst, u32 node,
351 u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352{
353 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
Allan Stephensb52124a2011-05-30 09:44:38 -0400355 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 struct sub_seq *free;
Paul Gortmakerfead3902011-12-29 20:43:44 -0500357 struct tipc_subscription *s, *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 int removed_subseq = 0;
359
Allan Stephensf1310722006-06-25 23:51:37 -0700360 if (!sseq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800361 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700362
Allan Stephensb52124a2011-05-30 09:44:38 -0400363 info = sseq->info;
364
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400365 /* Locate publication, if it exists */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400366 list_for_each_entry(publ, &info->zone_list, zone_list) {
367 if ((publ->key == key) && (publ->ref == ref) &&
368 (!publ->node || (publ->node == node)))
369 goto found;
370 }
371 return NULL;
372
373found:
Allan Stephensf1310722006-06-25 23:51:37 -0700374 /* Remove publication from zone scope list */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400375 list_del(&publ->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400376 info->zone_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377
Allan Stephensf1310722006-06-25 23:51:37 -0700378 /* Remove publication from cluster scope list, if present */
Ying Xue34747532015-01-09 15:27:10 +0800379 if (in_own_cluster(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400380 list_del(&publ->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400381 info->cluster_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 }
Allan Stephensf1310722006-06-25 23:51:37 -0700383
384 /* Remove publication from node scope list, if present */
Ying Xue34747532015-01-09 15:27:10 +0800385 if (in_own_node(net, node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400386 list_del(&publ->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400387 info->node_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389
Allan Stephensf1310722006-06-25 23:51:37 -0700390 /* Contract subseq list if no more publications for that subseq */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400391 if (list_empty(&info->zone_list)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400392 kfree(info);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 free = &nseq->sseqs[nseq->first_free--];
Allan Stephens0e659672010-12-31 18:59:32 +0000394 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 removed_subseq = 1;
396 }
397
Allan Stephensf1310722006-06-25 23:51:37 -0700398 /* Notify any waiting subscriptions */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Jon Maloyda0a75e2018-02-15 10:40:48 +0100400 tipc_sub_report_overlap(s, publ->lower, publ->upper,
401 TIPC_WITHDRAWN, publ->ref, publ->node,
402 publ->scope, removed_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 }
Allan Stephensf1310722006-06-25 23:51:37 -0700404
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 return publ;
406}
407
408/**
Jon Maloy83485002018-01-08 21:03:29 +0100409 * tipc_nameseq_subscribe - attach a subscription, and optionally
410 * issue the prescribed number of events if there is any sub-
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 * sequence overlapping with the requested sequence
412 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500413static void tipc_nameseq_subscribe(struct name_seq *nseq,
Jon Maloy8985ecc2018-02-15 10:40:46 +0100414 struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415{
416 struct sub_seq *sseq = nseq->sseqs;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100417 struct tipc_name_seq ns;
Jon Maloy8985ecc2018-02-15 10:40:46 +0100418 struct tipc_subscr *s = &sub->evt.s;
419 bool no_status;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100420
Jon Maloy8985ecc2018-02-15 10:40:46 +0100421 ns.type = tipc_sub_read(s, seq.type);
422 ns.lower = tipc_sub_read(s, seq.lower);
423 ns.upper = tipc_sub_read(s, seq.upper);
424 no_status = tipc_sub_read(s, filter) & TIPC_SUB_NO_STATUS;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425
Jon Maloyda0a75e2018-02-15 10:40:48 +0100426 tipc_sub_get(sub);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100427 list_add(&sub->nameseq_list, &nseq->subscriptions);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428
Jon Maloy8985ecc2018-02-15 10:40:46 +0100429 if (no_status || !sseq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 return;
431
432 while (sseq != &nseq->sseqs[nseq->first_free]) {
Jon Maloyda0a75e2018-02-15 10:40:48 +0100433 if (tipc_sub_check_overlap(&ns, sseq->lower, sseq->upper)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400434 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) {
Jon Maloyda0a75e2018-02-15 10:40:48 +0100439 tipc_sub_report_overlap(sub, sseq->lower,
440 sseq->upper,
441 TIPC_PUBLISHED,
442 crs->ref, crs->node,
443 crs->scope,
444 must_report);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 must_report = 0;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400446 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 }
448 sseq++;
449 }
450}
451
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800452static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800454 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 struct name_seq *ns;
457
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800458 seq_head = &tn->nametbl->seq_hlist[hash(type)];
Ying Xue97ede292014-12-02 15:00:30 +0800459 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
Allan Stephensb29f1422010-12-31 18:59:25 +0000460 if (ns->type == type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 return ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 }
463
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800464 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465};
466
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800467struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
468 u32 lower, u32 upper, u32 scope,
469 u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800471 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuefb9962f2014-12-02 15:00:26 +0800472 struct publication *publ;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800473 struct name_seq *seq = nametbl_find_seq(net, type);
Ying Xue993bfe52014-12-02 15:00:24 +0800474 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475
Allan Stephens8f177892012-04-26 17:57:17 -0400476 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
477 (lower > upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400478 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
479 type, lower, upper, scope);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800480 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 }
482
Allan Stephensb29f1422010-12-31 18:59:25 +0000483 if (!seq)
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800484 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800486 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487
Ying Xuefb9962f2014-12-02 15:00:26 +0800488 spin_lock_bh(&seq->lock);
Ying Xue34747532015-01-09 15:27:10 +0800489 publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
Per Liden4323add2006-01-18 00:38:21 +0100490 scope, node, port, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800491 spin_unlock_bh(&seq->lock);
492 return publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493}
494
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800495struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
496 u32 lower, u32 node, u32 ref,
497 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498{
499 struct publication *publ;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800500 struct name_seq *seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501
502 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800503 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504
Ying Xuefb9962f2014-12-02 15:00:26 +0800505 spin_lock_bh(&seq->lock);
Ying Xue34747532015-01-09 15:27:10 +0800506 publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800507 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800508 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800509 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800510 spin_unlock_bh(&seq->lock);
511 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800512 return publ;
513 }
514 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 return publ;
516}
517
Ben Hutchings2c530402012-07-10 10:55:09 +0000518/**
Allan Stephensbc9f8142011-11-07 17:00:54 -0500519 * tipc_nametbl_translate - perform name translation
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 *
Allan Stephensbc9f8142011-11-07 17:00:54 -0500521 * On entry, 'destnode' is the search domain used during translation.
522 *
523 * On exit:
524 * - if name translation is deferred to another node/cluster/zone,
525 * leaves 'destnode' unchanged (will be non-zero) and returns 0
526 * - if name translation is attempted and succeeds, sets 'destnode'
527 * to publishing node and returns port reference (will be non-zero)
528 * - if name translation is attempted and fails, sets 'destnode' to 0
529 * and returns 0
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800531u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
532 u32 *destnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533{
Ying Xue34747532015-01-09 15:27:10 +0800534 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400536 struct name_info *info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400537 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 struct name_seq *seq;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400539 u32 ref = 0;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500540 u32 node = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541
Ying Xue34747532015-01-09 15:27:10 +0800542 if (!tipc_in_scope(*destnode, tn->own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 return 0;
544
Ying Xue97ede292014-12-02 15:00:30 +0800545 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800546 seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 if (unlikely(!seq))
548 goto not_found;
Ying Xuefb9962f2014-12-02 15:00:26 +0800549 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 sseq = nameseq_find_subseq(seq, instance);
551 if (unlikely(!sseq))
Ying Xuefb9962f2014-12-02 15:00:26 +0800552 goto no_match;
Allan Stephensb52124a2011-05-30 09:44:38 -0400553 info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400555 /* Closest-First Algorithm */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 if (likely(!*destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400557 if (!list_empty(&info->node_list)) {
558 publ = list_first_entry(&info->node_list,
559 struct publication,
560 node_list);
561 list_move_tail(&publ->node_list,
562 &info->node_list);
563 } else if (!list_empty(&info->cluster_list)) {
564 publ = list_first_entry(&info->cluster_list,
565 struct publication,
566 cluster_list);
567 list_move_tail(&publ->cluster_list,
568 &info->cluster_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400569 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400570 publ = list_first_entry(&info->zone_list,
571 struct publication,
572 zone_list);
573 list_move_tail(&publ->zone_list,
574 &info->zone_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400575 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 }
577
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400578 /* Round-Robin Algorithm */
Ying Xue34747532015-01-09 15:27:10 +0800579 else if (*destnode == tn->own_addr) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400580 if (list_empty(&info->node_list))
581 goto no_match;
582 publ = list_first_entry(&info->node_list, struct publication,
583 node_list);
584 list_move_tail(&publ->node_list, &info->node_list);
Ying Xue34747532015-01-09 15:27:10 +0800585 } else if (in_own_cluster_exact(net, *destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400586 if (list_empty(&info->cluster_list))
587 goto no_match;
588 publ = list_first_entry(&info->cluster_list, struct publication,
589 cluster_list);
590 list_move_tail(&publ->cluster_list, &info->cluster_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400592 publ = list_first_entry(&info->zone_list, struct publication,
593 zone_list);
594 list_move_tail(&publ->zone_list, &info->zone_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400596
597 ref = publ->ref;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500598 node = publ->node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400599no_match:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 spin_unlock_bh(&seq->lock);
601not_found:
Ying Xue97ede292014-12-02 15:00:30 +0800602 rcu_read_unlock();
Allan Stephensbc9f8142011-11-07 17:00:54 -0500603 *destnode = node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400604 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605}
606
Jon Maloy232d07b2018-01-08 21:03:30 +0100607bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
Jon Maloyee106d72017-10-13 11:04:28 +0200608 struct list_head *dsts, int *dstcnt, u32 exclude,
609 bool all)
610{
611 u32 self = tipc_own_addr(net);
612 struct publication *publ;
613 struct name_info *info;
614 struct name_seq *seq;
615 struct sub_seq *sseq;
616
Jon Maloyee106d72017-10-13 11:04:28 +0200617 *dstcnt = 0;
618 rcu_read_lock();
619 seq = nametbl_find_seq(net, type);
620 if (unlikely(!seq))
621 goto exit;
622 spin_lock_bh(&seq->lock);
623 sseq = nameseq_find_subseq(seq, instance);
624 if (likely(sseq)) {
625 info = sseq->info;
626 list_for_each_entry(publ, &info->zone_list, zone_list) {
Jon Maloy232d07b2018-01-08 21:03:30 +0100627 if (publ->scope != scope)
Jon Maloyee106d72017-10-13 11:04:28 +0200628 continue;
629 if (publ->ref == exclude && publ->node == self)
630 continue;
631 tipc_dest_push(dsts, publ->node, publ->ref);
632 (*dstcnt)++;
633 if (all)
634 continue;
635 list_move_tail(&publ->zone_list, &info->zone_list);
636 break;
637 }
638 }
639 spin_unlock_bh(&seq->lock);
640exit:
641 rcu_read_unlock();
642 return !list_empty(dsts);
643}
644
Jon Maloy232d07b2018-01-08 21:03:30 +0100645int tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
646 u32 scope, bool exact, struct list_head *dports)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 struct sub_seq *sseq_stop;
Allan Stephensb52124a2011-05-30 09:44:38 -0400649 struct name_info *info;
Jon Maloy232d07b2018-01-08 21:03:30 +0100650 struct publication *p;
651 struct name_seq *seq;
652 struct sub_seq *sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 int res = 0;
654
Ying Xue97ede292014-12-02 15:00:30 +0800655 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800656 seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 if (!seq)
658 goto exit;
659
660 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
662 sseq_stop = seq->sseqs + seq->first_free;
663 for (; sseq != sseq_stop; sseq++) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 if (sseq->lower > upper)
665 break;
Allan Stephensb52124a2011-05-30 09:44:38 -0400666 info = sseq->info;
Jon Maloy232d07b2018-01-08 21:03:30 +0100667 list_for_each_entry(p, &info->node_list, node_list) {
668 if (p->scope == scope || (!exact && p->scope < scope))
669 tipc_dest_push(dports, 0, p->ref);
Allan Stephens968edbe2008-07-14 22:45:33 -0700670 }
671
Allan Stephensb52124a2011-05-30 09:44:38 -0400672 if (info->cluster_list_size != info->node_list_size)
Allan Stephens968edbe2008-07-14 22:45:33 -0700673 res = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 spin_unlock_bh(&seq->lock);
676exit:
Ying Xue97ede292014-12-02 15:00:30 +0800677 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 return res;
679}
680
Jon Paul Maloy2ae0b8a2017-01-18 13:50:51 -0500681/* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
682 * - Creates list of nodes that overlap the given multicast address
683 * - Determines if any node local ports overlap
684 */
685void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
Jon Maloye9a03442018-01-12 20:56:50 +0100686 u32 upper, struct tipc_nlist *nodes)
Jon Paul Maloy2ae0b8a2017-01-18 13:50:51 -0500687{
688 struct sub_seq *sseq, *stop;
689 struct publication *publ;
690 struct name_info *info;
691 struct name_seq *seq;
692
693 rcu_read_lock();
694 seq = nametbl_find_seq(net, type);
695 if (!seq)
696 goto exit;
697
698 spin_lock_bh(&seq->lock);
699 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
700 stop = seq->sseqs + seq->first_free;
Jon Maloyf65163f2017-10-25 16:19:52 +0200701 for (; sseq != stop && sseq->lower <= upper; sseq++) {
Jon Paul Maloy2ae0b8a2017-01-18 13:50:51 -0500702 info = sseq->info;
703 list_for_each_entry(publ, &info->zone_list, zone_list) {
Jon Maloye9a03442018-01-12 20:56:50 +0100704 tipc_nlist_add(nodes, publ->node);
Jon Paul Maloy2ae0b8a2017-01-18 13:50:51 -0500705 }
706 }
707 spin_unlock_bh(&seq->lock);
708exit:
709 rcu_read_unlock();
710}
711
Jon Maloy75da2162017-10-13 11:04:23 +0200712/* tipc_nametbl_build_group - build list of communication group members
713 */
714void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
Jon Maloy232d07b2018-01-08 21:03:30 +0100715 u32 type, u32 scope)
Jon Maloy75da2162017-10-13 11:04:23 +0200716{
717 struct sub_seq *sseq, *stop;
718 struct name_info *info;
719 struct publication *p;
720 struct name_seq *seq;
721
722 rcu_read_lock();
723 seq = nametbl_find_seq(net, type);
724 if (!seq)
725 goto exit;
726
727 spin_lock_bh(&seq->lock);
728 sseq = seq->sseqs;
729 stop = seq->sseqs + seq->first_free;
730 for (; sseq != stop; sseq++) {
731 info = sseq->info;
732 list_for_each_entry(p, &info->zone_list, zone_list) {
Jon Maloy232d07b2018-01-08 21:03:30 +0100733 if (p->scope != scope)
Jon Maloy75da2162017-10-13 11:04:23 +0200734 continue;
Jon Maloyd12d2e12018-01-08 21:03:28 +0100735 tipc_group_add_member(grp, p->node, p->ref, p->lower);
Jon Maloy75da2162017-10-13 11:04:23 +0200736 }
737 }
738 spin_unlock_bh(&seq->lock);
739exit:
740 rcu_read_unlock();
741}
742
Allan Stephensc422f1b2011-11-02 15:49:40 -0400743/*
Per Liden4323add2006-01-18 00:38:21 +0100744 * tipc_nametbl_publish - add name publication to network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 */
Ying Xuef2f98002015-01-09 15:27:05 +0800746struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
747 u32 upper, u32 scope, u32 port_ref,
748 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749{
750 struct publication *publ;
Ying Xueeab8c0452014-04-28 18:00:10 +0800751 struct sk_buff *buf = NULL;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800752 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800754 spin_lock_bh(&tn->nametbl_lock);
755 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400756 pr_warn("Publication failed, local publication limit reached (%u)\n",
Ying Xuee6a04b12012-08-16 12:09:14 +0000757 TIPC_MAX_PUBLICATIONS);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800758 spin_unlock_bh(&tn->nametbl_lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800759 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800762 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
Ying Xue34747532015-01-09 15:27:10 +0800763 tn->own_addr, port_ref, key);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500764 if (likely(publ)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800765 tn->nametbl->local_publ_count++;
766 buf = tipc_named_publish(net, publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200767 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800768 tipc_named_process_backlog(net);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500769 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800770 spin_unlock_bh(&tn->nametbl_lock);
Ying Xueeab8c0452014-04-28 18:00:10 +0800771
772 if (buf)
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500773 tipc_node_broadcast(net, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 return publ;
775}
776
777/**
Per Liden4323add2006-01-18 00:38:21 +0100778 * tipc_nametbl_withdraw - withdraw name publication from network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 */
Ying Xuef2f98002015-01-09 15:27:05 +0800780int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
781 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782{
783 struct publication *publ;
Ying Xue54923902014-12-02 15:00:28 +0800784 struct sk_buff *skb = NULL;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800785 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800787 spin_lock_bh(&tn->nametbl_lock);
Ying Xue34747532015-01-09 15:27:10 +0800788 publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800789 ref, key);
Allan Stephensf1310722006-06-25 23:51:37 -0700790 if (likely(publ)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800791 tn->nametbl->local_publ_count--;
Ying Xue34747532015-01-09 15:27:10 +0800792 skb = tipc_named_withdraw(net, publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200793 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800794 tipc_named_process_backlog(net);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 list_del_init(&publ->pport_list);
Ying Xue97ede292014-12-02 15:00:30 +0800796 kfree_rcu(publ, rcu);
Ying Xue54923902014-12-02 15:00:28 +0800797 } else {
798 pr_err("Unable to remove local publication\n"
799 "(type=%u, lower=%u, ref=%u, key=%u)\n",
800 type, lower, ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800802 spin_unlock_bh(&tn->nametbl_lock);
Ying Xue54923902014-12-02 15:00:28 +0800803
804 if (skb) {
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500805 tipc_node_broadcast(net, skb);
Ying Xue54923902014-12-02 15:00:28 +0800806 return 1;
807 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 return 0;
809}
810
811/**
Per Liden4323add2006-01-18 00:38:21 +0100812 * tipc_nametbl_subscribe - add a subscription object to the name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 */
Jon Maloy8985ecc2018-02-15 10:40:46 +0100814void tipc_nametbl_subscribe(struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100816 struct tipc_net *tn = tipc_net(sub->net);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100817 struct tipc_subscr *s = &sub->evt.s;
818 u32 type = tipc_sub_read(s, seq.type);
Ying Xue993bfe52014-12-02 15:00:24 +0800819 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 struct name_seq *seq;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100821 struct tipc_name_seq ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800823 spin_lock_bh(&tn->nametbl_lock);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100824 seq = nametbl_find_seq(sub->net, type);
Allan Stephensa0168922010-12-31 18:59:35 +0000825 if (!seq)
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800826 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
Allan Stephens0e659672010-12-31 18:59:32 +0000827 if (seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828 spin_lock_bh(&seq->lock);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100829 tipc_nameseq_subscribe(seq, sub);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900830 spin_unlock_bh(&seq->lock);
831 } else {
Jon Maloy8985ecc2018-02-15 10:40:46 +0100832 ns.type = tipc_sub_read(s, seq.type);
833 ns.lower = tipc_sub_read(s, seq.lower);
834 ns.upper = tipc_sub_read(s, seq.upper);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400835 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100836 ns.type, ns.lower, ns.upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900837 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800838 spin_unlock_bh(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839}
840
841/**
Per Liden4323add2006-01-18 00:38:21 +0100842 * tipc_nametbl_unsubscribe - remove a subscription object from name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 */
Jon Maloy8985ecc2018-02-15 10:40:46 +0100844void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845{
Jon Maloy8985ecc2018-02-15 10:40:46 +0100846 struct tipc_subscr *s = &sub->evt.s;
Jon Maloy5c45ab22018-02-15 10:40:49 +0100847 struct tipc_net *tn = tipc_net(sub->net);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 struct name_seq *seq;
Jon Maloy8985ecc2018-02-15 10:40:46 +0100849 u32 type = tipc_sub_read(s, seq.type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800851 spin_lock_bh(&tn->nametbl_lock);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100852 seq = nametbl_find_seq(sub->net, type);
Allan Stephens0e659672010-12-31 18:59:32 +0000853 if (seq != NULL) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900854 spin_lock_bh(&seq->lock);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100855 list_del_init(&sub->nameseq_list);
Jon Maloyda0a75e2018-02-15 10:40:48 +0100856 tipc_sub_put(sub);
Ying Xuefb9962f2014-12-02 15:00:26 +0800857 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800858 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800859 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800860 spin_unlock_bh(&seq->lock);
861 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800862 } else {
863 spin_unlock_bh(&seq->lock);
864 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900865 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800866 spin_unlock_bh(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867}
868
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800869int tipc_nametbl_init(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800871 struct tipc_net *tn = net_generic(net, tipc_net_id);
872 struct name_table *tipc_nametbl;
Ying Xue993bfe52014-12-02 15:00:24 +0800873 int i;
874
875 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
876 if (!tipc_nametbl)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 return -ENOMEM;
878
Ying Xue993bfe52014-12-02 15:00:24 +0800879 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
880 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
881
882 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
883 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
884 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800885 tn->nametbl = tipc_nametbl;
886 spin_lock_init(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 return 0;
888}
889
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100890/**
891 * tipc_purge_publications - remove all publications for a given type
892 *
893 * tipc_nametbl_lock must be held when calling this function
894 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800895static void tipc_purge_publications(struct net *net, struct name_seq *seq)
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100896{
897 struct publication *publ, *safe;
898 struct sub_seq *sseq;
899 struct name_info *info;
900
Ying Xuefb9962f2014-12-02 15:00:26 +0800901 spin_lock_bh(&seq->lock);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100902 sseq = seq->sseqs;
903 info = sseq->info;
904 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
Ying Xue84605042015-03-18 09:32:58 +0800905 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
906 publ->ref, publ->key);
Ying Xue97ede292014-12-02 15:00:30 +0800907 kfree_rcu(publ, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100908 }
Ying Xue97ede292014-12-02 15:00:30 +0800909 hlist_del_init_rcu(&seq->ns_list);
910 kfree(seq->sseqs);
Ying Xue023160b2014-12-09 15:17:56 +0800911 spin_unlock_bh(&seq->lock);
Ying Xuefb9962f2014-12-02 15:00:26 +0800912
Ying Xue97ede292014-12-02 15:00:30 +0800913 kfree_rcu(seq, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100914}
915
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800916void tipc_nametbl_stop(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 u32 i;
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100919 struct name_seq *seq;
920 struct hlist_head *seq_head;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800921 struct tipc_net *tn = net_generic(net, tipc_net_id);
922 struct name_table *tipc_nametbl = tn->nametbl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100924 /* Verify name table is empty and purge any lingering
925 * publications, then release the name table
926 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800927 spin_lock_bh(&tn->nametbl_lock);
Ying Xuef046e7d2012-08-16 12:09:11 +0000928 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue993bfe52014-12-02 15:00:24 +0800929 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
Paul Gortmakerf705ab92012-07-11 17:35:01 -0400930 continue;
Ying Xue993bfe52014-12-02 15:00:24 +0800931 seq_head = &tipc_nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +0800932 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800933 tipc_purge_publications(net, seq);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100934 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800936 spin_unlock_bh(&tn->nametbl_lock);
Ying Xue993bfe52014-12-02 15:00:24 +0800937
Ying Xue97ede292014-12-02 15:00:30 +0800938 synchronize_net();
Ying Xue993bfe52014-12-02 15:00:24 +0800939 kfree(tipc_nametbl);
940
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941}
Richard Alpe15931232014-11-20 10:29:20 +0100942
Richard Alped8182802014-11-24 11:10:29 +0100943static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
944 struct name_seq *seq,
945 struct sub_seq *sseq, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +0100946{
947 void *hdr;
948 struct nlattr *attrs;
949 struct nlattr *publ;
950 struct publication *p;
951
952 if (*last_publ) {
953 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
954 if (p->key == *last_publ)
955 break;
956 if (p->key != *last_publ)
957 return -EPIPE;
958 } else {
959 p = list_first_entry(&sseq->info->zone_list, struct publication,
960 zone_list);
961 }
962
963 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
964 *last_publ = p->key;
965
966 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100967 &tipc_genl_family, NLM_F_MULTI,
Richard Alpe15931232014-11-20 10:29:20 +0100968 TIPC_NL_NAME_TABLE_GET);
969 if (!hdr)
970 return -EMSGSIZE;
971
972 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
973 if (!attrs)
974 goto msg_full;
975
976 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
977 if (!publ)
978 goto attr_msg_full;
979
980 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
981 goto publ_msg_full;
982 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
983 goto publ_msg_full;
984 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
985 goto publ_msg_full;
986 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
987 goto publ_msg_full;
988 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
989 goto publ_msg_full;
990 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
991 goto publ_msg_full;
992 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
993 goto publ_msg_full;
994
995 nla_nest_end(msg->skb, publ);
996 nla_nest_end(msg->skb, attrs);
997 genlmsg_end(msg->skb, hdr);
998 }
999 *last_publ = 0;
1000
1001 return 0;
1002
1003publ_msg_full:
1004 nla_nest_cancel(msg->skb, publ);
1005attr_msg_full:
1006 nla_nest_cancel(msg->skb, attrs);
1007msg_full:
1008 genlmsg_cancel(msg->skb, hdr);
1009
1010 return -EMSGSIZE;
1011}
1012
Richard Alped8182802014-11-24 11:10:29 +01001013static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1014 u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +01001015{
1016 struct sub_seq *sseq;
1017 struct sub_seq *sseq_start;
1018 int err;
1019
1020 if (*last_lower) {
1021 sseq_start = nameseq_find_subseq(seq, *last_lower);
1022 if (!sseq_start)
1023 return -EPIPE;
1024 } else {
1025 sseq_start = seq->sseqs;
1026 }
1027
1028 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
1029 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
1030 if (err) {
1031 *last_lower = sseq->lower;
1032 return err;
1033 }
1034 }
1035 *last_lower = 0;
1036
1037 return 0;
1038}
1039
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001040static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
1041 u32 *last_type, u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +01001042{
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001043 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe15931232014-11-20 10:29:20 +01001044 struct hlist_head *seq_head;
Ying Xue97ede292014-12-02 15:00:30 +08001045 struct name_seq *seq = NULL;
Richard Alpe15931232014-11-20 10:29:20 +01001046 int err;
1047 int i;
1048
1049 if (*last_type)
1050 i = hash(*last_type);
1051 else
1052 i = 0;
1053
1054 for (; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001055 seq_head = &tn->nametbl->seq_hlist[i];
Richard Alpe15931232014-11-20 10:29:20 +01001056
1057 if (*last_type) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001058 seq = nametbl_find_seq(net, *last_type);
Richard Alpe15931232014-11-20 10:29:20 +01001059 if (!seq)
1060 return -EPIPE;
1061 } else {
Ying Xue97ede292014-12-02 15:00:30 +08001062 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1063 break;
Richard Alpe15931232014-11-20 10:29:20 +01001064 if (!seq)
1065 continue;
1066 }
1067
Ying Xue97ede292014-12-02 15:00:30 +08001068 hlist_for_each_entry_from_rcu(seq, ns_list) {
Richard Alpe15931232014-11-20 10:29:20 +01001069 spin_lock_bh(&seq->lock);
Richard Alpe15931232014-11-20 10:29:20 +01001070 err = __tipc_nl_subseq_list(msg, seq, last_lower,
1071 last_publ);
1072
1073 if (err) {
1074 *last_type = seq->type;
1075 spin_unlock_bh(&seq->lock);
1076 return err;
1077 }
1078 spin_unlock_bh(&seq->lock);
1079 }
1080 *last_type = 0;
1081 }
1082 return 0;
1083}
1084
1085int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1086{
1087 int err;
1088 int done = cb->args[3];
1089 u32 last_type = cb->args[0];
1090 u32 last_lower = cb->args[1];
1091 u32 last_publ = cb->args[2];
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001092 struct net *net = sock_net(skb->sk);
Richard Alpe15931232014-11-20 10:29:20 +01001093 struct tipc_nl_msg msg;
1094
1095 if (done)
1096 return 0;
1097
1098 msg.skb = skb;
1099 msg.portid = NETLINK_CB(cb->skb).portid;
1100 msg.seq = cb->nlh->nlmsg_seq;
1101
Ying Xue97ede292014-12-02 15:00:30 +08001102 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001103 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
Richard Alpe15931232014-11-20 10:29:20 +01001104 if (!err) {
1105 done = 1;
1106 } else if (err != -EMSGSIZE) {
1107 /* We never set seq or call nl_dump_check_consistent() this
1108 * means that setting prev_seq here will cause the consistence
1109 * check to fail in the netlink callback handler. Resulting in
1110 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1111 * we got an error.
1112 */
1113 cb->prev_seq = 1;
1114 }
Ying Xue97ede292014-12-02 15:00:30 +08001115 rcu_read_unlock();
Richard Alpe15931232014-11-20 10:29:20 +01001116
1117 cb->args[0] = last_type;
1118 cb->args[1] = last_lower;
1119 cb->args[2] = last_publ;
1120 cb->args[3] = done;
1121
1122 return skb->len;
1123}
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001124
Jon Maloya80ae532017-10-13 11:04:22 +02001125struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001126{
Jon Maloya80ae532017-10-13 11:04:22 +02001127 u64 value = (u64)node << 32 | port;
1128 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001129
Jon Maloya80ae532017-10-13 11:04:22 +02001130 list_for_each_entry(dst, l, list) {
1131 if (dst->value != value)
1132 continue;
1133 return dst;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001134 }
Jon Maloya80ae532017-10-13 11:04:22 +02001135 return NULL;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001136}
1137
Jon Maloya80ae532017-10-13 11:04:22 +02001138bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001139{
Jon Maloya80ae532017-10-13 11:04:22 +02001140 u64 value = (u64)node << 32 | port;
1141 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001142
Jon Maloya80ae532017-10-13 11:04:22 +02001143 if (tipc_dest_find(l, node, port))
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001144 return false;
1145
Jon Maloya80ae532017-10-13 11:04:22 +02001146 dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1147 if (unlikely(!dst))
1148 return false;
1149 dst->value = value;
1150 list_add(&dst->list, l);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001151 return true;
1152}
1153
Jon Maloya80ae532017-10-13 11:04:22 +02001154bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001155{
Jon Maloya80ae532017-10-13 11:04:22 +02001156 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001157
1158 if (list_empty(l))
Jon Maloya80ae532017-10-13 11:04:22 +02001159 return false;
1160 dst = list_first_entry(l, typeof(*dst), list);
1161 if (port)
1162 *port = dst->port;
1163 if (node)
1164 *node = dst->node;
1165 list_del(&dst->list);
1166 kfree(dst);
1167 return true;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001168}
1169
Jon Maloya80ae532017-10-13 11:04:22 +02001170bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001171{
Jon Maloya80ae532017-10-13 11:04:22 +02001172 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001173
Jon Maloya80ae532017-10-13 11:04:22 +02001174 dst = tipc_dest_find(l, node, port);
1175 if (!dst)
1176 return false;
1177 list_del(&dst->list);
1178 kfree(dst);
1179 return true;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001180}
1181
Jon Maloya80ae532017-10-13 11:04:22 +02001182void tipc_dest_list_purge(struct list_head *l)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001183{
Jon Maloya80ae532017-10-13 11:04:22 +02001184 struct tipc_dest *dst, *tmp;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001185
Jon Maloya80ae532017-10-13 11:04:22 +02001186 list_for_each_entry_safe(dst, tmp, l, list) {
1187 list_del(&dst->list);
1188 kfree(dst);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001189 }
1190}
1191
Jon Maloya80ae532017-10-13 11:04:22 +02001192int tipc_dest_list_len(struct list_head *l)
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001193{
Jon Maloya80ae532017-10-13 11:04:22 +02001194 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001195 int i = 0;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001196
Jon Maloya80ae532017-10-13 11:04:22 +02001197 list_for_each_entry(dst, l, list) {
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001198 i++;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001199 }
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001200 return i;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001201}