blob: bbbfc0702634da360be3ffe8d28b923598e69e66 [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 Maloye50e73e2018-03-15 16:48:55 +01004 * Copyright (c) 2000-2006, 2014-2018, 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
Jon Maloyba765ec2018-03-15 16:48:53 +010053 * @node_list: list of publications on own node of this <type,lower,upper>
Jon Maloye50e73e2018-03-15 16:48:55 +010054 * @all_publ: list of all publications of this <type,lower,upper>
Per Lidenb97bf3f2006-01-02 19:04:38 +010055 */
Allan Stephensb52124a2011-05-30 09:44:38 -040056struct name_info {
Jon Maloye50e73e2018-03-15 16:48:55 +010057 struct list_head local_publ;
58 struct list_head all_publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +010059};
60
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090061/**
Allan Stephensb52124a2011-05-30 09:44:38 -040062 * struct sub_seq - container for all published instances of a name sequence
63 * @lower: name sequence lower bound
64 * @upper: name sequence upper bound
65 * @info: pointer to name sequence publication info
66 */
Allan Stephensb52124a2011-05-30 09:44:38 -040067struct sub_seq {
68 u32 lower;
69 u32 upper;
70 struct name_info *info;
71};
72
73/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010074 * struct name_seq - container for all published instances of a name type
75 * @type: 32 bit 'type' value for name sequence
76 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
77 * sub-sequences are sorted in ascending order
78 * @alloc: number of sub-sequences currently in array
Allan Stephensf1310722006-06-25 23:51:37 -070079 * @first_free: array index of first unused sub-sequence entry
Per Lidenb97bf3f2006-01-02 19:04:38 +010080 * @ns_list: links to adjacent name sequences in hash chain
81 * @subscriptions: list of subscriptions for this 'type'
Allan Stephens307fdf52008-06-04 17:38:22 -070082 * @lock: spinlock controlling access to publication lists of all sub-sequences
Ying Xue97ede292014-12-02 15:00:30 +080083 * @rcu: RCU callback head used for deferred freeing
Per Lidenb97bf3f2006-01-02 19:04:38 +010084 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010085struct name_seq {
86 u32 type;
87 struct sub_seq *sseqs;
88 u32 alloc;
89 u32 first_free;
90 struct hlist_node ns_list;
91 struct list_head subscriptions;
92 spinlock_t lock;
Ying Xue97ede292014-12-02 15:00:30 +080093 struct rcu_head rcu;
Per Lidenb97bf3f2006-01-02 19:04:38 +010094};
95
Sam Ravnborg05790c62006-03-20 22:37:04 -080096static int hash(int x)
Per Lidenb97bf3f2006-01-02 19:04:38 +010097{
Ying Xuef046e7d2012-08-16 12:09:11 +000098 return x & (TIPC_NAMETBL_SIZE - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +010099}
100
101/**
102 * publ_create - create a publication structure
103 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900104static struct publication *publ_create(u32 type, u32 lower, u32 upper,
Jon Maloye50e73e2018-03-15 16:48:55 +0100105 u32 scope, u32 node, u32 port,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106 u32 key)
107{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700108 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109 if (publ == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400110 pr_warn("Publication creation failure, no memory\n");
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800111 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 }
113
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 publ->type = type;
115 publ->lower = lower;
116 publ->upper = upper;
117 publ->scope = scope;
118 publ->node = node;
Jon Maloye50e73e2018-03-15 16:48:55 +0100119 publ->port = port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 publ->key = key;
Jon Maloye50e73e2018-03-15 16:48:55 +0100121 INIT_LIST_HEAD(&publ->binding_sock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 return publ;
123}
124
125/**
Per Liden4323add2006-01-18 00:38:21 +0100126 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800128static struct sub_seq *tipc_subseq_alloc(u32 cnt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129{
wangweidong0cee6bb2013-12-12 09:36:39 +0800130 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131}
132
133/**
Per Liden4323add2006-01-18 00:38:21 +0100134 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900135 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 * Allocates a single sub-sequence structure and sets it to all 0's.
137 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800138static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700140 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
Per Liden4323add2006-01-18 00:38:21 +0100141 struct sub_seq *sseq = tipc_subseq_alloc(1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142
143 if (!nseq || !sseq) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400144 pr_warn("Name sequence creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 kfree(nseq);
146 kfree(sseq);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800147 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 }
149
Ingo Molnar34af9462006-06-27 02:53:55 -0700150 spin_lock_init(&nseq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 nseq->type = type;
152 nseq->sseqs = sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 nseq->alloc = 1;
154 INIT_HLIST_NODE(&nseq->ns_list);
155 INIT_LIST_HEAD(&nseq->subscriptions);
Ying Xue97ede292014-12-02 15:00:30 +0800156 hlist_add_head_rcu(&nseq->ns_list, seq_head);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 return nseq;
158}
159
Ben Hutchings2c530402012-07-10 10:55:09 +0000160/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900162 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 * Very time-critical, so binary searches through sub-sequence array.
164 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800165static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
166 u32 instance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167{
168 struct sub_seq *sseqs = nseq->sseqs;
169 int low = 0;
170 int high = nseq->first_free - 1;
171 int mid;
172
173 while (low <= high) {
174 mid = (low + high) / 2;
175 if (instance < sseqs[mid].lower)
176 high = mid - 1;
177 else if (instance > sseqs[mid].upper)
178 low = mid + 1;
179 else
180 return &sseqs[mid];
181 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800182 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183}
184
185/**
186 * nameseq_locate_subseq - determine position of name instance in sub-sequence
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900187 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188 * Returns index in sub-sequence array of the entry that contains the specified
189 * instance value; if no entry contains that value, returns the position
190 * where a new entry for it would be inserted in the array.
191 *
192 * Note: Similar to binary search code for locating a sub-sequence.
193 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
195{
196 struct sub_seq *sseqs = nseq->sseqs;
197 int low = 0;
198 int high = nseq->first_free - 1;
199 int mid;
200
201 while (low <= high) {
202 mid = (low + high) / 2;
203 if (instance < sseqs[mid].lower)
204 high = mid - 1;
205 else if (instance > sseqs[mid].upper)
206 low = mid + 1;
207 else
208 return mid;
209 }
210 return low;
211}
212
213/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400214 * tipc_nameseq_insert_publ
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 */
Ying Xue34747532015-01-09 15:27:10 +0800216static struct publication *tipc_nameseq_insert_publ(struct net *net,
217 struct name_seq *nseq,
218 u32 type, u32 lower,
219 u32 upper, u32 scope,
220 u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500222 struct tipc_subscription *s;
223 struct tipc_subscription *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 struct publication *publ;
225 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400226 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 int created_subseq = 0;
228
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 sseq = nameseq_find_subseq(nseq, lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 if (sseq) {
231
232 /* Lower end overlaps existing entry => need an exact match */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233 if ((sseq->lower != lower) || (sseq->upper != upper)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800234 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400236
237 info = sseq->info;
Allan Stephensf80c24d2011-11-03 11:12:01 -0400238
239 /* Check if an identical publication already exists */
Jon Maloye50e73e2018-03-15 16:48:55 +0100240 list_for_each_entry(publ, &info->all_publ, all_publ) {
241 if (publ->port == port && publ->key == key &&
242 (!publ->node || publ->node == node))
Allan Stephensf80c24d2011-11-03 11:12:01 -0400243 return NULL;
244 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 } else {
246 u32 inspos;
247 struct sub_seq *freesseq;
248
249 /* Find where lower end should be inserted */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250 inspos = nameseq_locate_subseq(nseq, lower);
251
252 /* Fail if upper end overlaps into an existing entry */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 if ((inspos < nseq->first_free) &&
254 (upper >= nseq->sseqs[inspos].lower)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800255 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 }
257
258 /* Ensure there is space for new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 if (nseq->first_free == nseq->alloc) {
Allan Stephens9ab230f2006-06-25 23:37:24 -0700260 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
261
262 if (!sseqs) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400263 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
264 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800265 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 }
Allan Stephens9ab230f2006-06-25 23:37:24 -0700267 memcpy(sseqs, nseq->sseqs,
268 nseq->alloc * sizeof(struct sub_seq));
269 kfree(nseq->sseqs);
270 nseq->sseqs = sseqs;
271 nseq->alloc *= 2;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273
Allan Stephensb52124a2011-05-30 09:44:38 -0400274 info = kzalloc(sizeof(*info), GFP_ATOMIC);
275 if (!info) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400276 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
277 type, lower, upper);
Allan Stephensb52124a2011-05-30 09:44:38 -0400278 return NULL;
279 }
280
Jon Maloye50e73e2018-03-15 16:48:55 +0100281 INIT_LIST_HEAD(&info->local_publ);
282 INIT_LIST_HEAD(&info->all_publ);
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400283
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 /* Insert new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 sseq = &nseq->sseqs[inspos];
286 freesseq = &nseq->sseqs[nseq->first_free];
Allan Stephens0e659672010-12-31 18:59:32 +0000287 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
288 memset(sseq, 0, sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 nseq->first_free++;
290 sseq->lower = lower;
291 sseq->upper = upper;
Allan Stephensb52124a2011-05-30 09:44:38 -0400292 sseq->info = info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 created_subseq = 1;
294 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400296 /* Insert a publication */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 publ = publ_create(type, lower, upper, scope, node, port, key);
298 if (!publ)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800299 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300
Jon Maloye50e73e2018-03-15 16:48:55 +0100301 list_add(&publ->all_publ, &info->all_publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302
Jon Maloyba765ec2018-03-15 16:48:53 +0100303 if (in_own_node(net, node))
Jon Maloye50e73e2018-03-15 16:48:55 +0100304 list_add(&publ->local_publ, &info->local_publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400306 /* Any subscriptions waiting for notification? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Jon Maloyda0a75e2018-02-15 10:40:48 +0100308 tipc_sub_report_overlap(s, publ->lower, publ->upper,
Jon Maloye50e73e2018-03-15 16:48:55 +0100309 TIPC_PUBLISHED, publ->port,
Jon Maloyda0a75e2018-02-15 10:40:48 +0100310 publ->node, publ->scope,
311 created_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 }
313 return publ;
314}
315
316/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400317 * tipc_nameseq_remove_publ
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900318 *
Allan Stephensf1310722006-06-25 23:51:37 -0700319 * NOTE: There may be cases where TIPC is asked to remove a publication
320 * that is not in the name table. For example, if another node issues a
321 * publication for a name sequence that overlaps an existing name sequence
322 * the publication will not be recorded, which means the publication won't
323 * be found when the name sequence is later withdrawn by that node.
324 * A failed withdraw request simply returns a failure indication and lets the
325 * caller issue any error or warning messages associated with such a problem.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 */
Ying Xue34747532015-01-09 15:27:10 +0800327static struct publication *tipc_nameseq_remove_publ(struct net *net,
328 struct name_seq *nseq,
329 u32 inst, u32 node,
Jon Maloye50e73e2018-03-15 16:48:55 +0100330 u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331{
332 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
Allan Stephensb52124a2011-05-30 09:44:38 -0400334 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 struct sub_seq *free;
Paul Gortmakerfead3902011-12-29 20:43:44 -0500336 struct tipc_subscription *s, *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 int removed_subseq = 0;
338
Allan Stephensf1310722006-06-25 23:51:37 -0700339 if (!sseq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800340 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700341
Allan Stephensb52124a2011-05-30 09:44:38 -0400342 info = sseq->info;
343
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400344 /* Locate publication, if it exists */
Jon Maloye50e73e2018-03-15 16:48:55 +0100345 list_for_each_entry(publ, &info->all_publ, all_publ) {
346 if (publ->key == key && publ->port == port &&
347 (!publ->node || publ->node == node))
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400348 goto found;
349 }
350 return NULL;
351
352found:
Jon Maloye50e73e2018-03-15 16:48:55 +0100353 list_del(&publ->all_publ);
Jon Maloyba765ec2018-03-15 16:48:53 +0100354 if (in_own_node(net, node))
Jon Maloye50e73e2018-03-15 16:48:55 +0100355 list_del(&publ->local_publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356
Allan Stephensf1310722006-06-25 23:51:37 -0700357 /* Contract subseq list if no more publications for that subseq */
Jon Maloye50e73e2018-03-15 16:48:55 +0100358 if (list_empty(&info->all_publ)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400359 kfree(info);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 free = &nseq->sseqs[nseq->first_free--];
Allan Stephens0e659672010-12-31 18:59:32 +0000361 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 removed_subseq = 1;
363 }
364
Allan Stephensf1310722006-06-25 23:51:37 -0700365 /* Notify any waiting subscriptions */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Jon Maloyda0a75e2018-02-15 10:40:48 +0100367 tipc_sub_report_overlap(s, publ->lower, publ->upper,
Jon Maloye50e73e2018-03-15 16:48:55 +0100368 TIPC_WITHDRAWN, publ->port,
369 publ->node, publ->scope,
370 removed_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 }
Allan Stephensf1310722006-06-25 23:51:37 -0700372
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 return publ;
374}
375
376/**
Jon Maloy83485002018-01-08 21:03:29 +0100377 * tipc_nameseq_subscribe - attach a subscription, and optionally
378 * issue the prescribed number of events if there is any sub-
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 * sequence overlapping with the requested sequence
380 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500381static void tipc_nameseq_subscribe(struct name_seq *nseq,
Jon Maloy8985ecc2018-02-15 10:40:46 +0100382 struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383{
384 struct sub_seq *sseq = nseq->sseqs;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100385 struct tipc_name_seq ns;
Jon Maloy8985ecc2018-02-15 10:40:46 +0100386 struct tipc_subscr *s = &sub->evt.s;
387 bool no_status;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100388
Jon Maloy8985ecc2018-02-15 10:40:46 +0100389 ns.type = tipc_sub_read(s, seq.type);
390 ns.lower = tipc_sub_read(s, seq.lower);
391 ns.upper = tipc_sub_read(s, seq.upper);
392 no_status = tipc_sub_read(s, filter) & TIPC_SUB_NO_STATUS;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393
Jon Maloyda0a75e2018-02-15 10:40:48 +0100394 tipc_sub_get(sub);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100395 list_add(&sub->nameseq_list, &nseq->subscriptions);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396
Jon Maloy8985ecc2018-02-15 10:40:46 +0100397 if (no_status || !sseq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 return;
399
400 while (sseq != &nseq->sseqs[nseq->first_free]) {
Jon Maloyda0a75e2018-02-15 10:40:48 +0100401 if (tipc_sub_check_overlap(&ns, sseq->lower, sseq->upper)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400402 struct publication *crs;
403 struct name_info *info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 int must_report = 1;
405
Jon Maloye50e73e2018-03-15 16:48:55 +0100406 list_for_each_entry(crs, &info->all_publ, all_publ) {
Jon Maloyda0a75e2018-02-15 10:40:48 +0100407 tipc_sub_report_overlap(sub, sseq->lower,
408 sseq->upper,
409 TIPC_PUBLISHED,
Jon Maloye50e73e2018-03-15 16:48:55 +0100410 crs->port,
411 crs->node,
Jon Maloyda0a75e2018-02-15 10:40:48 +0100412 crs->scope,
413 must_report);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 must_report = 0;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400415 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 }
417 sseq++;
418 }
419}
420
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800421static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800423 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 struct name_seq *ns;
426
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800427 seq_head = &tn->nametbl->seq_hlist[hash(type)];
Ying Xue97ede292014-12-02 15:00:30 +0800428 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
Allan Stephensb29f1422010-12-31 18:59:25 +0000429 if (ns->type == type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 return ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 }
432
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800433 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434};
435
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800436struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
437 u32 lower, u32 upper, u32 scope,
438 u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800440 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuefb9962f2014-12-02 15:00:26 +0800441 struct publication *publ;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800442 struct name_seq *seq = nametbl_find_seq(net, type);
Ying Xue993bfe52014-12-02 15:00:24 +0800443 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444
Jon Maloy928df182018-03-15 16:48:51 +0100445 if (scope > TIPC_NODE_SCOPE || lower > upper) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400446 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
447 type, lower, upper, scope);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800448 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 }
450
Allan Stephensb29f1422010-12-31 18:59:25 +0000451 if (!seq)
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800452 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800454 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455
Ying Xuefb9962f2014-12-02 15:00:26 +0800456 spin_lock_bh(&seq->lock);
Ying Xue34747532015-01-09 15:27:10 +0800457 publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
Per Liden4323add2006-01-18 00:38:21 +0100458 scope, node, port, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800459 spin_unlock_bh(&seq->lock);
460 return publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461}
462
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800463struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
Jon Maloye50e73e2018-03-15 16:48:55 +0100464 u32 lower, u32 node, u32 port,
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800465 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466{
467 struct publication *publ;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800468 struct name_seq *seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469
470 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800471 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472
Ying Xuefb9962f2014-12-02 15:00:26 +0800473 spin_lock_bh(&seq->lock);
Jon Maloye50e73e2018-03-15 16:48:55 +0100474 publ = tipc_nameseq_remove_publ(net, seq, lower, node, port, key);
Ying Xuefb9962f2014-12-02 15:00:26 +0800475 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800476 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800477 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800478 spin_unlock_bh(&seq->lock);
479 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800480 return publ;
481 }
482 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 return publ;
484}
485
Ben Hutchings2c530402012-07-10 10:55:09 +0000486/**
Allan Stephensbc9f8142011-11-07 17:00:54 -0500487 * tipc_nametbl_translate - perform name translation
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 *
Allan Stephensbc9f8142011-11-07 17:00:54 -0500489 * On entry, 'destnode' is the search domain used during translation.
490 *
491 * On exit:
492 * - if name translation is deferred to another node/cluster/zone,
493 * leaves 'destnode' unchanged (will be non-zero) and returns 0
494 * - if name translation is attempted and succeeds, sets 'destnode'
495 * to publishing node and returns port reference (will be non-zero)
496 * - if name translation is attempted and fails, sets 'destnode' to 0
497 * and returns 0
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800499u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
500 u32 *destnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501{
Ying Xue34747532015-01-09 15:27:10 +0800502 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400504 struct name_info *info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400505 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 struct name_seq *seq;
Jon Maloye50e73e2018-03-15 16:48:55 +0100507 u32 port = 0;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500508 u32 node = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509
Ying Xue34747532015-01-09 15:27:10 +0800510 if (!tipc_in_scope(*destnode, tn->own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 return 0;
512
Ying Xue97ede292014-12-02 15:00:30 +0800513 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800514 seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 if (unlikely(!seq))
516 goto not_found;
Ying Xuefb9962f2014-12-02 15:00:26 +0800517 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 sseq = nameseq_find_subseq(seq, instance);
519 if (unlikely(!sseq))
Ying Xuefb9962f2014-12-02 15:00:26 +0800520 goto no_match;
Allan Stephensb52124a2011-05-30 09:44:38 -0400521 info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400523 /* Closest-First Algorithm */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 if (likely(!*destnode)) {
Jon Maloye50e73e2018-03-15 16:48:55 +0100525 if (!list_empty(&info->local_publ)) {
526 publ = list_first_entry(&info->local_publ,
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400527 struct publication,
Jon Maloye50e73e2018-03-15 16:48:55 +0100528 local_publ);
529 list_move_tail(&publ->local_publ,
530 &info->local_publ);
Jon Maloyba765ec2018-03-15 16:48:53 +0100531 } else {
Jon Maloye50e73e2018-03-15 16:48:55 +0100532 publ = list_first_entry(&info->all_publ,
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400533 struct publication,
Jon Maloye50e73e2018-03-15 16:48:55 +0100534 all_publ);
535 list_move_tail(&publ->all_publ,
536 &info->all_publ);
Allan Stephens8af46382011-05-30 11:27:50 -0400537 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 }
539
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400540 /* Round-Robin Algorithm */
Ying Xue34747532015-01-09 15:27:10 +0800541 else if (*destnode == tn->own_addr) {
Jon Maloye50e73e2018-03-15 16:48:55 +0100542 if (list_empty(&info->local_publ))
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400543 goto no_match;
Jon Maloye50e73e2018-03-15 16:48:55 +0100544 publ = list_first_entry(&info->local_publ, struct publication,
545 local_publ);
546 list_move_tail(&publ->local_publ, &info->local_publ);
Jon Maloyba765ec2018-03-15 16:48:53 +0100547 } else {
Jon Maloye50e73e2018-03-15 16:48:55 +0100548 publ = list_first_entry(&info->all_publ, struct publication,
549 all_publ);
550 list_move_tail(&publ->all_publ, &info->all_publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400552
Jon Maloye50e73e2018-03-15 16:48:55 +0100553 port = publ->port;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500554 node = publ->node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400555no_match:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 spin_unlock_bh(&seq->lock);
557not_found:
Ying Xue97ede292014-12-02 15:00:30 +0800558 rcu_read_unlock();
Allan Stephensbc9f8142011-11-07 17:00:54 -0500559 *destnode = node;
Jon Maloye50e73e2018-03-15 16:48:55 +0100560 return port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561}
562
Jon Maloy232d07b2018-01-08 21:03:30 +0100563bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
Jon Maloyee106d72017-10-13 11:04:28 +0200564 struct list_head *dsts, int *dstcnt, u32 exclude,
565 bool all)
566{
567 u32 self = tipc_own_addr(net);
568 struct publication *publ;
569 struct name_info *info;
570 struct name_seq *seq;
571 struct sub_seq *sseq;
572
Jon Maloyee106d72017-10-13 11:04:28 +0200573 *dstcnt = 0;
574 rcu_read_lock();
575 seq = nametbl_find_seq(net, type);
576 if (unlikely(!seq))
577 goto exit;
578 spin_lock_bh(&seq->lock);
579 sseq = nameseq_find_subseq(seq, instance);
580 if (likely(sseq)) {
581 info = sseq->info;
Jon Maloye50e73e2018-03-15 16:48:55 +0100582 list_for_each_entry(publ, &info->all_publ, all_publ) {
Jon Maloy232d07b2018-01-08 21:03:30 +0100583 if (publ->scope != scope)
Jon Maloyee106d72017-10-13 11:04:28 +0200584 continue;
Jon Maloye50e73e2018-03-15 16:48:55 +0100585 if (publ->port == exclude && publ->node == self)
Jon Maloyee106d72017-10-13 11:04:28 +0200586 continue;
Jon Maloye50e73e2018-03-15 16:48:55 +0100587 tipc_dest_push(dsts, publ->node, publ->port);
Jon Maloyee106d72017-10-13 11:04:28 +0200588 (*dstcnt)++;
589 if (all)
590 continue;
Jon Maloye50e73e2018-03-15 16:48:55 +0100591 list_move_tail(&publ->all_publ, &info->all_publ);
Jon Maloyee106d72017-10-13 11:04:28 +0200592 break;
593 }
594 }
595 spin_unlock_bh(&seq->lock);
596exit:
597 rcu_read_unlock();
598 return !list_empty(dsts);
599}
600
Jon Maloyba765ec2018-03-15 16:48:53 +0100601void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
602 u32 scope, bool exact, struct list_head *dports)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 struct sub_seq *sseq_stop;
Allan Stephensb52124a2011-05-30 09:44:38 -0400605 struct name_info *info;
Jon Maloy232d07b2018-01-08 21:03:30 +0100606 struct publication *p;
607 struct name_seq *seq;
608 struct sub_seq *sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609
Ying Xue97ede292014-12-02 15:00:30 +0800610 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800611 seq = nametbl_find_seq(net, type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 if (!seq)
613 goto exit;
614
615 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
617 sseq_stop = seq->sseqs + seq->first_free;
618 for (; sseq != sseq_stop; sseq++) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 if (sseq->lower > upper)
620 break;
Allan Stephensb52124a2011-05-30 09:44:38 -0400621 info = sseq->info;
Jon Maloye50e73e2018-03-15 16:48:55 +0100622 list_for_each_entry(p, &info->local_publ, local_publ) {
Jon Maloy232d07b2018-01-08 21:03:30 +0100623 if (p->scope == scope || (!exact && p->scope < scope))
Jon Maloye50e73e2018-03-15 16:48:55 +0100624 tipc_dest_push(dports, 0, p->port);
Allan Stephens968edbe2008-07-14 22:45:33 -0700625 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 spin_unlock_bh(&seq->lock);
628exit:
Ying Xue97ede292014-12-02 15:00:30 +0800629 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630}
631
Jon Paul Maloy2ae0b8a2017-01-18 13:50:51 -0500632/* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
633 * - Creates list of nodes that overlap the given multicast address
634 * - Determines if any node local ports overlap
635 */
636void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
Jon Maloye9a03442018-01-12 20:56:50 +0100637 u32 upper, struct tipc_nlist *nodes)
Jon Paul Maloy2ae0b8a2017-01-18 13:50:51 -0500638{
639 struct sub_seq *sseq, *stop;
640 struct publication *publ;
641 struct name_info *info;
642 struct name_seq *seq;
643
644 rcu_read_lock();
645 seq = nametbl_find_seq(net, type);
646 if (!seq)
647 goto exit;
648
649 spin_lock_bh(&seq->lock);
650 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
651 stop = seq->sseqs + seq->first_free;
Jon Maloyf65163f2017-10-25 16:19:52 +0200652 for (; sseq != stop && sseq->lower <= upper; sseq++) {
Jon Paul Maloy2ae0b8a2017-01-18 13:50:51 -0500653 info = sseq->info;
Jon Maloye50e73e2018-03-15 16:48:55 +0100654 list_for_each_entry(publ, &info->all_publ, all_publ) {
Jon Maloye9a03442018-01-12 20:56:50 +0100655 tipc_nlist_add(nodes, publ->node);
Jon Paul Maloy2ae0b8a2017-01-18 13:50:51 -0500656 }
657 }
658 spin_unlock_bh(&seq->lock);
659exit:
660 rcu_read_unlock();
661}
662
Jon Maloy75da2162017-10-13 11:04:23 +0200663/* tipc_nametbl_build_group - build list of communication group members
664 */
665void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
Jon Maloy232d07b2018-01-08 21:03:30 +0100666 u32 type, u32 scope)
Jon Maloy75da2162017-10-13 11:04:23 +0200667{
668 struct sub_seq *sseq, *stop;
669 struct name_info *info;
670 struct publication *p;
671 struct name_seq *seq;
672
673 rcu_read_lock();
674 seq = nametbl_find_seq(net, type);
675 if (!seq)
676 goto exit;
677
678 spin_lock_bh(&seq->lock);
679 sseq = seq->sseqs;
680 stop = seq->sseqs + seq->first_free;
681 for (; sseq != stop; sseq++) {
682 info = sseq->info;
Jon Maloye50e73e2018-03-15 16:48:55 +0100683 list_for_each_entry(p, &info->all_publ, all_publ) {
Jon Maloy232d07b2018-01-08 21:03:30 +0100684 if (p->scope != scope)
Jon Maloy75da2162017-10-13 11:04:23 +0200685 continue;
Jon Maloye50e73e2018-03-15 16:48:55 +0100686 tipc_group_add_member(grp, p->node, p->port, p->lower);
Jon Maloy75da2162017-10-13 11:04:23 +0200687 }
688 }
689 spin_unlock_bh(&seq->lock);
690exit:
691 rcu_read_unlock();
692}
693
Allan Stephensc422f1b2011-11-02 15:49:40 -0400694/*
Per Liden4323add2006-01-18 00:38:21 +0100695 * tipc_nametbl_publish - add name publication to network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 */
Ying Xuef2f98002015-01-09 15:27:05 +0800697struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
698 u32 upper, u32 scope, u32 port_ref,
699 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700{
701 struct publication *publ;
Ying Xueeab8c0452014-04-28 18:00:10 +0800702 struct sk_buff *buf = NULL;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800703 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800705 spin_lock_bh(&tn->nametbl_lock);
706 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400707 pr_warn("Publication failed, local publication limit reached (%u)\n",
Ying Xuee6a04b12012-08-16 12:09:14 +0000708 TIPC_MAX_PUBLICATIONS);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800709 spin_unlock_bh(&tn->nametbl_lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800710 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800713 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
Ying Xue34747532015-01-09 15:27:10 +0800714 tn->own_addr, port_ref, key);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500715 if (likely(publ)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800716 tn->nametbl->local_publ_count++;
717 buf = tipc_named_publish(net, publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200718 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800719 tipc_named_process_backlog(net);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500720 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800721 spin_unlock_bh(&tn->nametbl_lock);
Ying Xueeab8c0452014-04-28 18:00:10 +0800722
723 if (buf)
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500724 tipc_node_broadcast(net, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 return publ;
726}
727
728/**
Per Liden4323add2006-01-18 00:38:21 +0100729 * tipc_nametbl_withdraw - withdraw name publication from network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 */
Jon Maloye50e73e2018-03-15 16:48:55 +0100731int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 port,
Ying Xuef2f98002015-01-09 15:27:05 +0800732 u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733{
734 struct publication *publ;
Ying Xue54923902014-12-02 15:00:28 +0800735 struct sk_buff *skb = NULL;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800736 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800738 spin_lock_bh(&tn->nametbl_lock);
Ying Xue34747532015-01-09 15:27:10 +0800739 publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
Jon Maloye50e73e2018-03-15 16:48:55 +0100740 port, key);
Allan Stephensf1310722006-06-25 23:51:37 -0700741 if (likely(publ)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800742 tn->nametbl->local_publ_count--;
Ying Xue34747532015-01-09 15:27:10 +0800743 skb = tipc_named_withdraw(net, publ);
Erik Hugnea5325ae2014-08-28 09:08:47 +0200744 /* Any pending external events? */
Ying Xuef2f98002015-01-09 15:27:05 +0800745 tipc_named_process_backlog(net);
Jon Maloye50e73e2018-03-15 16:48:55 +0100746 list_del_init(&publ->binding_sock);
Ying Xue97ede292014-12-02 15:00:30 +0800747 kfree_rcu(publ, rcu);
Ying Xue54923902014-12-02 15:00:28 +0800748 } else {
749 pr_err("Unable to remove local publication\n"
Jon Maloye50e73e2018-03-15 16:48:55 +0100750 "(type=%u, lower=%u, port=%u, key=%u)\n",
751 type, lower, port, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800753 spin_unlock_bh(&tn->nametbl_lock);
Ying Xue54923902014-12-02 15:00:28 +0800754
755 if (skb) {
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500756 tipc_node_broadcast(net, skb);
Ying Xue54923902014-12-02 15:00:28 +0800757 return 1;
758 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 return 0;
760}
761
762/**
Per Liden4323add2006-01-18 00:38:21 +0100763 * tipc_nametbl_subscribe - add a subscription object to the name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 */
Jon Maloy8985ecc2018-02-15 10:40:46 +0100765void tipc_nametbl_subscribe(struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100767 struct tipc_net *tn = tipc_net(sub->net);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100768 struct tipc_subscr *s = &sub->evt.s;
769 u32 type = tipc_sub_read(s, seq.type);
Ying Xue993bfe52014-12-02 15:00:24 +0800770 int index = hash(type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 struct name_seq *seq;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100772 struct tipc_name_seq ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800774 spin_lock_bh(&tn->nametbl_lock);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100775 seq = nametbl_find_seq(sub->net, type);
Allan Stephensa0168922010-12-31 18:59:35 +0000776 if (!seq)
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800777 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
Allan Stephens0e659672010-12-31 18:59:32 +0000778 if (seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900779 spin_lock_bh(&seq->lock);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100780 tipc_nameseq_subscribe(seq, sub);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900781 spin_unlock_bh(&seq->lock);
782 } else {
Jon Maloy8985ecc2018-02-15 10:40:46 +0100783 ns.type = tipc_sub_read(s, seq.type);
784 ns.lower = tipc_sub_read(s, seq.lower);
785 ns.upper = tipc_sub_read(s, seq.upper);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400786 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +0100787 ns.type, ns.lower, ns.upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900788 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800789 spin_unlock_bh(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790}
791
792/**
Per Liden4323add2006-01-18 00:38:21 +0100793 * tipc_nametbl_unsubscribe - remove a subscription object from name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 */
Jon Maloy8985ecc2018-02-15 10:40:46 +0100795void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796{
Jon Maloy8985ecc2018-02-15 10:40:46 +0100797 struct tipc_subscr *s = &sub->evt.s;
Jon Maloy5c45ab22018-02-15 10:40:49 +0100798 struct tipc_net *tn = tipc_net(sub->net);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 struct name_seq *seq;
Jon Maloy8985ecc2018-02-15 10:40:46 +0100800 u32 type = tipc_sub_read(s, seq.type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800802 spin_lock_bh(&tn->nametbl_lock);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100803 seq = nametbl_find_seq(sub->net, type);
Allan Stephens0e659672010-12-31 18:59:32 +0000804 if (seq != NULL) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805 spin_lock_bh(&seq->lock);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100806 list_del_init(&sub->nameseq_list);
Jon Maloyda0a75e2018-02-15 10:40:48 +0100807 tipc_sub_put(sub);
Ying Xuefb9962f2014-12-02 15:00:26 +0800808 if (!seq->first_free && list_empty(&seq->subscriptions)) {
Ying Xue97ede292014-12-02 15:00:30 +0800809 hlist_del_init_rcu(&seq->ns_list);
Ying Xuefb9962f2014-12-02 15:00:26 +0800810 kfree(seq->sseqs);
Ying Xue97ede292014-12-02 15:00:30 +0800811 spin_unlock_bh(&seq->lock);
812 kfree_rcu(seq, rcu);
Ying Xuefb9962f2014-12-02 15:00:26 +0800813 } else {
814 spin_unlock_bh(&seq->lock);
815 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900816 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800817 spin_unlock_bh(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818}
819
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800820int tipc_nametbl_init(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800822 struct tipc_net *tn = net_generic(net, tipc_net_id);
823 struct name_table *tipc_nametbl;
Ying Xue993bfe52014-12-02 15:00:24 +0800824 int i;
825
826 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
827 if (!tipc_nametbl)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 return -ENOMEM;
829
Ying Xue993bfe52014-12-02 15:00:24 +0800830 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
831 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
832
Jon Maloy64a52b26d52018-03-15 16:48:52 +0100833 INIT_LIST_HEAD(&tipc_nametbl->node_scope);
834 INIT_LIST_HEAD(&tipc_nametbl->cluster_scope);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800835 tn->nametbl = tipc_nametbl;
836 spin_lock_init(&tn->nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837 return 0;
838}
839
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100840/**
841 * tipc_purge_publications - remove all publications for a given type
842 *
843 * tipc_nametbl_lock must be held when calling this function
844 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800845static void tipc_purge_publications(struct net *net, struct name_seq *seq)
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100846{
847 struct publication *publ, *safe;
848 struct sub_seq *sseq;
849 struct name_info *info;
850
Ying Xuefb9962f2014-12-02 15:00:26 +0800851 spin_lock_bh(&seq->lock);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100852 sseq = seq->sseqs;
853 info = sseq->info;
Jon Maloye50e73e2018-03-15 16:48:55 +0100854 list_for_each_entry_safe(publ, safe, &info->all_publ, all_publ) {
Ying Xue84605042015-03-18 09:32:58 +0800855 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
Jon Maloye50e73e2018-03-15 16:48:55 +0100856 publ->port, publ->key);
Ying Xue97ede292014-12-02 15:00:30 +0800857 kfree_rcu(publ, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100858 }
Ying Xue97ede292014-12-02 15:00:30 +0800859 hlist_del_init_rcu(&seq->ns_list);
860 kfree(seq->sseqs);
Ying Xue023160b2014-12-09 15:17:56 +0800861 spin_unlock_bh(&seq->lock);
Ying Xuefb9962f2014-12-02 15:00:26 +0800862
Ying Xue97ede292014-12-02 15:00:30 +0800863 kfree_rcu(seq, rcu);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100864}
865
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800866void tipc_nametbl_stop(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 u32 i;
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100869 struct name_seq *seq;
870 struct hlist_head *seq_head;
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800871 struct tipc_net *tn = net_generic(net, tipc_net_id);
872 struct name_table *tipc_nametbl = tn->nametbl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100874 /* Verify name table is empty and purge any lingering
875 * publications, then release the name table
876 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800877 spin_lock_bh(&tn->nametbl_lock);
Ying Xuef046e7d2012-08-16 12:09:11 +0000878 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue993bfe52014-12-02 15:00:24 +0800879 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
Paul Gortmakerf705ab92012-07-11 17:35:01 -0400880 continue;
Ying Xue993bfe52014-12-02 15:00:24 +0800881 seq_head = &tipc_nametbl->seq_hlist[i];
Ying Xue97ede292014-12-02 15:00:30 +0800882 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800883 tipc_purge_publications(net, seq);
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100884 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 }
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800886 spin_unlock_bh(&tn->nametbl_lock);
Ying Xue993bfe52014-12-02 15:00:24 +0800887
Ying Xue97ede292014-12-02 15:00:30 +0800888 synchronize_net();
Ying Xue993bfe52014-12-02 15:00:24 +0800889 kfree(tipc_nametbl);
890
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891}
Richard Alpe15931232014-11-20 10:29:20 +0100892
Richard Alped8182802014-11-24 11:10:29 +0100893static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
894 struct name_seq *seq,
895 struct sub_seq *sseq, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +0100896{
897 void *hdr;
898 struct nlattr *attrs;
899 struct nlattr *publ;
900 struct publication *p;
901
902 if (*last_publ) {
Jon Maloye50e73e2018-03-15 16:48:55 +0100903 list_for_each_entry(p, &sseq->info->all_publ, all_publ)
Richard Alpe15931232014-11-20 10:29:20 +0100904 if (p->key == *last_publ)
905 break;
906 if (p->key != *last_publ)
907 return -EPIPE;
908 } else {
Jon Maloye50e73e2018-03-15 16:48:55 +0100909 p = list_first_entry(&sseq->info->all_publ, struct publication,
910 all_publ);
Richard Alpe15931232014-11-20 10:29:20 +0100911 }
912
Jon Maloye50e73e2018-03-15 16:48:55 +0100913 list_for_each_entry_from(p, &sseq->info->all_publ, all_publ) {
Richard Alpe15931232014-11-20 10:29:20 +0100914 *last_publ = p->key;
915
916 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100917 &tipc_genl_family, NLM_F_MULTI,
Richard Alpe15931232014-11-20 10:29:20 +0100918 TIPC_NL_NAME_TABLE_GET);
919 if (!hdr)
920 return -EMSGSIZE;
921
922 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
923 if (!attrs)
924 goto msg_full;
925
926 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
927 if (!publ)
928 goto attr_msg_full;
929
930 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
931 goto publ_msg_full;
932 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
933 goto publ_msg_full;
934 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
935 goto publ_msg_full;
936 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
937 goto publ_msg_full;
938 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
939 goto publ_msg_full;
Jon Maloye50e73e2018-03-15 16:48:55 +0100940 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port))
Richard Alpe15931232014-11-20 10:29:20 +0100941 goto publ_msg_full;
942 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
943 goto publ_msg_full;
944
945 nla_nest_end(msg->skb, publ);
946 nla_nest_end(msg->skb, attrs);
947 genlmsg_end(msg->skb, hdr);
948 }
949 *last_publ = 0;
950
951 return 0;
952
953publ_msg_full:
954 nla_nest_cancel(msg->skb, publ);
955attr_msg_full:
956 nla_nest_cancel(msg->skb, attrs);
957msg_full:
958 genlmsg_cancel(msg->skb, hdr);
959
960 return -EMSGSIZE;
961}
962
Richard Alped8182802014-11-24 11:10:29 +0100963static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
964 u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +0100965{
966 struct sub_seq *sseq;
967 struct sub_seq *sseq_start;
968 int err;
969
970 if (*last_lower) {
971 sseq_start = nameseq_find_subseq(seq, *last_lower);
972 if (!sseq_start)
973 return -EPIPE;
974 } else {
975 sseq_start = seq->sseqs;
976 }
977
978 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
979 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
980 if (err) {
981 *last_lower = sseq->lower;
982 return err;
983 }
984 }
985 *last_lower = 0;
986
987 return 0;
988}
989
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800990static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
991 u32 *last_type, u32 *last_lower, u32 *last_publ)
Richard Alpe15931232014-11-20 10:29:20 +0100992{
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800993 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe15931232014-11-20 10:29:20 +0100994 struct hlist_head *seq_head;
Ying Xue97ede292014-12-02 15:00:30 +0800995 struct name_seq *seq = NULL;
Richard Alpe15931232014-11-20 10:29:20 +0100996 int err;
997 int i;
998
999 if (*last_type)
1000 i = hash(*last_type);
1001 else
1002 i = 0;
1003
1004 for (; i < TIPC_NAMETBL_SIZE; i++) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001005 seq_head = &tn->nametbl->seq_hlist[i];
Richard Alpe15931232014-11-20 10:29:20 +01001006
1007 if (*last_type) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001008 seq = nametbl_find_seq(net, *last_type);
Richard Alpe15931232014-11-20 10:29:20 +01001009 if (!seq)
1010 return -EPIPE;
1011 } else {
Ying Xue97ede292014-12-02 15:00:30 +08001012 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1013 break;
Richard Alpe15931232014-11-20 10:29:20 +01001014 if (!seq)
1015 continue;
1016 }
1017
Ying Xue97ede292014-12-02 15:00:30 +08001018 hlist_for_each_entry_from_rcu(seq, ns_list) {
Richard Alpe15931232014-11-20 10:29:20 +01001019 spin_lock_bh(&seq->lock);
Richard Alpe15931232014-11-20 10:29:20 +01001020 err = __tipc_nl_subseq_list(msg, seq, last_lower,
1021 last_publ);
1022
1023 if (err) {
1024 *last_type = seq->type;
1025 spin_unlock_bh(&seq->lock);
1026 return err;
1027 }
1028 spin_unlock_bh(&seq->lock);
1029 }
1030 *last_type = 0;
1031 }
1032 return 0;
1033}
1034
1035int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1036{
1037 int err;
1038 int done = cb->args[3];
1039 u32 last_type = cb->args[0];
1040 u32 last_lower = cb->args[1];
1041 u32 last_publ = cb->args[2];
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001042 struct net *net = sock_net(skb->sk);
Richard Alpe15931232014-11-20 10:29:20 +01001043 struct tipc_nl_msg msg;
1044
1045 if (done)
1046 return 0;
1047
1048 msg.skb = skb;
1049 msg.portid = NETLINK_CB(cb->skb).portid;
1050 msg.seq = cb->nlh->nlmsg_seq;
1051
Ying Xue97ede292014-12-02 15:00:30 +08001052 rcu_read_lock();
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001053 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
Richard Alpe15931232014-11-20 10:29:20 +01001054 if (!err) {
1055 done = 1;
1056 } else if (err != -EMSGSIZE) {
1057 /* We never set seq or call nl_dump_check_consistent() this
1058 * means that setting prev_seq here will cause the consistence
1059 * check to fail in the netlink callback handler. Resulting in
1060 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1061 * we got an error.
1062 */
1063 cb->prev_seq = 1;
1064 }
Ying Xue97ede292014-12-02 15:00:30 +08001065 rcu_read_unlock();
Richard Alpe15931232014-11-20 10:29:20 +01001066
1067 cb->args[0] = last_type;
1068 cb->args[1] = last_lower;
1069 cb->args[2] = last_publ;
1070 cb->args[3] = done;
1071
1072 return skb->len;
1073}
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001074
Jon Maloya80ae532017-10-13 11:04:22 +02001075struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001076{
Jon Maloya80ae532017-10-13 11:04:22 +02001077 u64 value = (u64)node << 32 | port;
1078 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001079
Jon Maloya80ae532017-10-13 11:04:22 +02001080 list_for_each_entry(dst, l, list) {
1081 if (dst->value != value)
1082 continue;
1083 return dst;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001084 }
Jon Maloya80ae532017-10-13 11:04:22 +02001085 return NULL;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001086}
1087
Jon Maloya80ae532017-10-13 11:04:22 +02001088bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001089{
Jon Maloya80ae532017-10-13 11:04:22 +02001090 u64 value = (u64)node << 32 | port;
1091 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001092
Jon Maloya80ae532017-10-13 11:04:22 +02001093 if (tipc_dest_find(l, node, port))
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001094 return false;
1095
Jon Maloya80ae532017-10-13 11:04:22 +02001096 dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1097 if (unlikely(!dst))
1098 return false;
1099 dst->value = value;
1100 list_add(&dst->list, l);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001101 return true;
1102}
1103
Jon Maloya80ae532017-10-13 11:04:22 +02001104bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001105{
Jon Maloya80ae532017-10-13 11:04:22 +02001106 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001107
1108 if (list_empty(l))
Jon Maloya80ae532017-10-13 11:04:22 +02001109 return false;
1110 dst = list_first_entry(l, typeof(*dst), list);
1111 if (port)
1112 *port = dst->port;
1113 if (node)
1114 *node = dst->node;
1115 list_del(&dst->list);
1116 kfree(dst);
1117 return true;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001118}
1119
Jon Maloya80ae532017-10-13 11:04:22 +02001120bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001121{
Jon Maloya80ae532017-10-13 11:04:22 +02001122 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001123
Jon Maloya80ae532017-10-13 11:04:22 +02001124 dst = tipc_dest_find(l, node, port);
1125 if (!dst)
1126 return false;
1127 list_del(&dst->list);
1128 kfree(dst);
1129 return true;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001130}
1131
Jon Maloya80ae532017-10-13 11:04:22 +02001132void tipc_dest_list_purge(struct list_head *l)
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001133{
Jon Maloya80ae532017-10-13 11:04:22 +02001134 struct tipc_dest *dst, *tmp;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001135
Jon Maloya80ae532017-10-13 11:04:22 +02001136 list_for_each_entry_safe(dst, tmp, l, list) {
1137 list_del(&dst->list);
1138 kfree(dst);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001139 }
1140}
1141
Jon Maloya80ae532017-10-13 11:04:22 +02001142int tipc_dest_list_len(struct list_head *l)
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001143{
Jon Maloya80ae532017-10-13 11:04:22 +02001144 struct tipc_dest *dst;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001145 int i = 0;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001146
Jon Maloya80ae532017-10-13 11:04:22 +02001147 list_for_each_entry(dst, l, list) {
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001148 i++;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001149 }
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001150 return i;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001151}