xref: /openbmc/linux/net/tipc/name_table.c (revision e50e73e1)
1 /*
2  * net/tipc/name_table.c: TIPC name table code
3  *
4  * Copyright (c) 2000-2006, 2014-2018, Ericsson AB
5  * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 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.
19  *
20  * 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
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <net/sock.h>
38 #include "core.h"
39 #include "netlink.h"
40 #include "name_table.h"
41 #include "name_distr.h"
42 #include "subscr.h"
43 #include "bcast.h"
44 #include "addr.h"
45 #include "node.h"
46 #include "group.h"
47 #include <net/genetlink.h>
48 
49 #define TIPC_NAMETBL_SIZE 1024		/* must be a power of 2 */
50 
51 /**
52  * struct name_info - name sequence publication info
53  * @node_list: list of publications on own node of this <type,lower,upper>
54  * @all_publ: list of all publications of this <type,lower,upper>
55  */
56 struct name_info {
57 	struct list_head local_publ;
58 	struct list_head all_publ;
59 };
60 
61 /**
62  * 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  */
67 struct sub_seq {
68 	u32 lower;
69 	u32 upper;
70 	struct name_info *info;
71 };
72 
73 /**
74  * 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
79  * @first_free: array index of first unused sub-sequence entry
80  * @ns_list: links to adjacent name sequences in hash chain
81  * @subscriptions: list of subscriptions for this 'type'
82  * @lock: spinlock controlling access to publication lists of all sub-sequences
83  * @rcu: RCU callback head used for deferred freeing
84  */
85 struct 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;
93 	struct rcu_head rcu;
94 };
95 
96 static int hash(int x)
97 {
98 	return x & (TIPC_NAMETBL_SIZE - 1);
99 }
100 
101 /**
102  * publ_create - create a publication structure
103  */
104 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
105 				       u32 scope, u32 node, u32 port,
106 				       u32 key)
107 {
108 	struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
109 	if (publ == NULL) {
110 		pr_warn("Publication creation failure, no memory\n");
111 		return NULL;
112 	}
113 
114 	publ->type = type;
115 	publ->lower = lower;
116 	publ->upper = upper;
117 	publ->scope = scope;
118 	publ->node = node;
119 	publ->port = port;
120 	publ->key = key;
121 	INIT_LIST_HEAD(&publ->binding_sock);
122 	return publ;
123 }
124 
125 /**
126  * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
127  */
128 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
129 {
130 	return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
131 }
132 
133 /**
134  * tipc_nameseq_create - create a name sequence structure for the specified 'type'
135  *
136  * Allocates a single sub-sequence structure and sets it to all 0's.
137  */
138 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
139 {
140 	struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
141 	struct sub_seq *sseq = tipc_subseq_alloc(1);
142 
143 	if (!nseq || !sseq) {
144 		pr_warn("Name sequence creation failed, no memory\n");
145 		kfree(nseq);
146 		kfree(sseq);
147 		return NULL;
148 	}
149 
150 	spin_lock_init(&nseq->lock);
151 	nseq->type = type;
152 	nseq->sseqs = sseq;
153 	nseq->alloc = 1;
154 	INIT_HLIST_NODE(&nseq->ns_list);
155 	INIT_LIST_HEAD(&nseq->subscriptions);
156 	hlist_add_head_rcu(&nseq->ns_list, seq_head);
157 	return nseq;
158 }
159 
160 /**
161  * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
162  *
163  * Very time-critical, so binary searches through sub-sequence array.
164  */
165 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
166 					   u32 instance)
167 {
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 	}
182 	return NULL;
183 }
184 
185 /**
186  * nameseq_locate_subseq - determine position of name instance in sub-sequence
187  *
188  * 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  */
194 static 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 /**
214  * tipc_nameseq_insert_publ
215  */
216 static 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)
221 {
222 	struct tipc_subscription *s;
223 	struct tipc_subscription *st;
224 	struct publication *publ;
225 	struct sub_seq *sseq;
226 	struct name_info *info;
227 	int created_subseq = 0;
228 
229 	sseq = nameseq_find_subseq(nseq, lower);
230 	if (sseq) {
231 
232 		/* Lower end overlaps existing entry => need an exact match */
233 		if ((sseq->lower != lower) || (sseq->upper != upper)) {
234 			return NULL;
235 		}
236 
237 		info = sseq->info;
238 
239 		/* Check if an identical publication already exists */
240 		list_for_each_entry(publ, &info->all_publ, all_publ) {
241 			if (publ->port == port && publ->key == key &&
242 			    (!publ->node || publ->node == node))
243 				return NULL;
244 		}
245 	} else {
246 		u32 inspos;
247 		struct sub_seq *freesseq;
248 
249 		/* Find where lower end should be inserted */
250 		inspos = nameseq_locate_subseq(nseq, lower);
251 
252 		/* Fail if upper end overlaps into an existing entry */
253 		if ((inspos < nseq->first_free) &&
254 		    (upper >= nseq->sseqs[inspos].lower)) {
255 			return NULL;
256 		}
257 
258 		/* Ensure there is space for new sub-sequence */
259 		if (nseq->first_free == nseq->alloc) {
260 			struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
261 
262 			if (!sseqs) {
263 				pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
264 					type, lower, upper);
265 				return NULL;
266 			}
267 			memcpy(sseqs, nseq->sseqs,
268 			       nseq->alloc * sizeof(struct sub_seq));
269 			kfree(nseq->sseqs);
270 			nseq->sseqs = sseqs;
271 			nseq->alloc *= 2;
272 		}
273 
274 		info = kzalloc(sizeof(*info), GFP_ATOMIC);
275 		if (!info) {
276 			pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
277 				type, lower, upper);
278 			return NULL;
279 		}
280 
281 		INIT_LIST_HEAD(&info->local_publ);
282 		INIT_LIST_HEAD(&info->all_publ);
283 
284 		/* Insert new sub-sequence */
285 		sseq = &nseq->sseqs[inspos];
286 		freesseq = &nseq->sseqs[nseq->first_free];
287 		memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
288 		memset(sseq, 0, sizeof(*sseq));
289 		nseq->first_free++;
290 		sseq->lower = lower;
291 		sseq->upper = upper;
292 		sseq->info = info;
293 		created_subseq = 1;
294 	}
295 
296 	/* Insert a publication */
297 	publ = publ_create(type, lower, upper, scope, node, port, key);
298 	if (!publ)
299 		return NULL;
300 
301 	list_add(&publ->all_publ, &info->all_publ);
302 
303 	if (in_own_node(net, node))
304 		list_add(&publ->local_publ, &info->local_publ);
305 
306 	/* Any subscriptions waiting for notification?  */
307 	list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
308 		tipc_sub_report_overlap(s, publ->lower, publ->upper,
309 					TIPC_PUBLISHED, publ->port,
310 					publ->node, publ->scope,
311 					created_subseq);
312 	}
313 	return publ;
314 }
315 
316 /**
317  * tipc_nameseq_remove_publ
318  *
319  * 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.
326  */
327 static struct publication *tipc_nameseq_remove_publ(struct net *net,
328 						    struct name_seq *nseq,
329 						    u32 inst, u32 node,
330 						    u32 port, u32 key)
331 {
332 	struct publication *publ;
333 	struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
334 	struct name_info *info;
335 	struct sub_seq *free;
336 	struct tipc_subscription *s, *st;
337 	int removed_subseq = 0;
338 
339 	if (!sseq)
340 		return NULL;
341 
342 	info = sseq->info;
343 
344 	/* Locate publication, if it exists */
345 	list_for_each_entry(publ, &info->all_publ, all_publ) {
346 		if (publ->key == key && publ->port == port &&
347 		    (!publ->node || publ->node == node))
348 			goto found;
349 	}
350 	return NULL;
351 
352 found:
353 	list_del(&publ->all_publ);
354 	if (in_own_node(net, node))
355 		list_del(&publ->local_publ);
356 
357 	/* Contract subseq list if no more publications for that subseq */
358 	if (list_empty(&info->all_publ)) {
359 		kfree(info);
360 		free = &nseq->sseqs[nseq->first_free--];
361 		memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
362 		removed_subseq = 1;
363 	}
364 
365 	/* Notify any waiting subscriptions */
366 	list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
367 		tipc_sub_report_overlap(s, publ->lower, publ->upper,
368 					TIPC_WITHDRAWN, publ->port,
369 					publ->node, publ->scope,
370 					removed_subseq);
371 	}
372 
373 	return publ;
374 }
375 
376 /**
377  * tipc_nameseq_subscribe - attach a subscription, and optionally
378  * issue the prescribed number of events if there is any sub-
379  * sequence overlapping with the requested sequence
380  */
381 static void tipc_nameseq_subscribe(struct name_seq *nseq,
382 				   struct tipc_subscription *sub)
383 {
384 	struct sub_seq *sseq = nseq->sseqs;
385 	struct tipc_name_seq ns;
386 	struct tipc_subscr *s = &sub->evt.s;
387 	bool no_status;
388 
389 	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;
393 
394 	tipc_sub_get(sub);
395 	list_add(&sub->nameseq_list, &nseq->subscriptions);
396 
397 	if (no_status || !sseq)
398 		return;
399 
400 	while (sseq != &nseq->sseqs[nseq->first_free]) {
401 		if (tipc_sub_check_overlap(&ns, sseq->lower, sseq->upper)) {
402 			struct publication *crs;
403 			struct name_info *info = sseq->info;
404 			int must_report = 1;
405 
406 			list_for_each_entry(crs, &info->all_publ, all_publ) {
407 				tipc_sub_report_overlap(sub, sseq->lower,
408 							sseq->upper,
409 							TIPC_PUBLISHED,
410 							crs->port,
411 							crs->node,
412 							crs->scope,
413 							must_report);
414 				must_report = 0;
415 			}
416 		}
417 		sseq++;
418 	}
419 }
420 
421 static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
422 {
423 	struct tipc_net *tn = net_generic(net, tipc_net_id);
424 	struct hlist_head *seq_head;
425 	struct name_seq *ns;
426 
427 	seq_head = &tn->nametbl->seq_hlist[hash(type)];
428 	hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
429 		if (ns->type == type)
430 			return ns;
431 	}
432 
433 	return NULL;
434 };
435 
436 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
437 					     u32 lower, u32 upper, u32 scope,
438 					     u32 node, u32 port, u32 key)
439 {
440 	struct tipc_net *tn = net_generic(net, tipc_net_id);
441 	struct publication *publ;
442 	struct name_seq *seq = nametbl_find_seq(net, type);
443 	int index = hash(type);
444 
445 	if (scope > TIPC_NODE_SCOPE || lower > upper) {
446 		pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
447 			 type, lower, upper, scope);
448 		return NULL;
449 	}
450 
451 	if (!seq)
452 		seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
453 	if (!seq)
454 		return NULL;
455 
456 	spin_lock_bh(&seq->lock);
457 	publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
458 					scope, node, port, key);
459 	spin_unlock_bh(&seq->lock);
460 	return publ;
461 }
462 
463 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
464 					     u32 lower, u32 node, u32 port,
465 					     u32 key)
466 {
467 	struct publication *publ;
468 	struct name_seq *seq = nametbl_find_seq(net, type);
469 
470 	if (!seq)
471 		return NULL;
472 
473 	spin_lock_bh(&seq->lock);
474 	publ = tipc_nameseq_remove_publ(net, seq, lower, node, port, key);
475 	if (!seq->first_free && list_empty(&seq->subscriptions)) {
476 		hlist_del_init_rcu(&seq->ns_list);
477 		kfree(seq->sseqs);
478 		spin_unlock_bh(&seq->lock);
479 		kfree_rcu(seq, rcu);
480 		return publ;
481 	}
482 	spin_unlock_bh(&seq->lock);
483 	return publ;
484 }
485 
486 /**
487  * tipc_nametbl_translate - perform name translation
488  *
489  * 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
498  */
499 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
500 			   u32 *destnode)
501 {
502 	struct tipc_net *tn = net_generic(net, tipc_net_id);
503 	struct sub_seq *sseq;
504 	struct name_info *info;
505 	struct publication *publ;
506 	struct name_seq *seq;
507 	u32 port = 0;
508 	u32 node = 0;
509 
510 	if (!tipc_in_scope(*destnode, tn->own_addr))
511 		return 0;
512 
513 	rcu_read_lock();
514 	seq = nametbl_find_seq(net, type);
515 	if (unlikely(!seq))
516 		goto not_found;
517 	spin_lock_bh(&seq->lock);
518 	sseq = nameseq_find_subseq(seq, instance);
519 	if (unlikely(!sseq))
520 		goto no_match;
521 	info = sseq->info;
522 
523 	/* Closest-First Algorithm */
524 	if (likely(!*destnode)) {
525 		if (!list_empty(&info->local_publ)) {
526 			publ = list_first_entry(&info->local_publ,
527 						struct publication,
528 						local_publ);
529 			list_move_tail(&publ->local_publ,
530 				       &info->local_publ);
531 		} else {
532 			publ = list_first_entry(&info->all_publ,
533 						struct publication,
534 						all_publ);
535 			list_move_tail(&publ->all_publ,
536 				       &info->all_publ);
537 		}
538 	}
539 
540 	/* Round-Robin Algorithm */
541 	else if (*destnode == tn->own_addr) {
542 		if (list_empty(&info->local_publ))
543 			goto no_match;
544 		publ = list_first_entry(&info->local_publ, struct publication,
545 					local_publ);
546 		list_move_tail(&publ->local_publ, &info->local_publ);
547 	} else {
548 		publ = list_first_entry(&info->all_publ, struct publication,
549 					all_publ);
550 		list_move_tail(&publ->all_publ, &info->all_publ);
551 	}
552 
553 	port = publ->port;
554 	node = publ->node;
555 no_match:
556 	spin_unlock_bh(&seq->lock);
557 not_found:
558 	rcu_read_unlock();
559 	*destnode = node;
560 	return port;
561 }
562 
563 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
564 			 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 
573 	*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;
582 		list_for_each_entry(publ, &info->all_publ, all_publ) {
583 			if (publ->scope != scope)
584 				continue;
585 			if (publ->port == exclude && publ->node == self)
586 				continue;
587 			tipc_dest_push(dsts, publ->node, publ->port);
588 			(*dstcnt)++;
589 			if (all)
590 				continue;
591 			list_move_tail(&publ->all_publ, &info->all_publ);
592 			break;
593 		}
594 	}
595 	spin_unlock_bh(&seq->lock);
596 exit:
597 	rcu_read_unlock();
598 	return !list_empty(dsts);
599 }
600 
601 void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
602 			    u32 scope, bool exact, struct list_head *dports)
603 {
604 	struct sub_seq *sseq_stop;
605 	struct name_info *info;
606 	struct publication *p;
607 	struct name_seq *seq;
608 	struct sub_seq *sseq;
609 
610 	rcu_read_lock();
611 	seq = nametbl_find_seq(net, type);
612 	if (!seq)
613 		goto exit;
614 
615 	spin_lock_bh(&seq->lock);
616 	sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
617 	sseq_stop = seq->sseqs + seq->first_free;
618 	for (; sseq != sseq_stop; sseq++) {
619 		if (sseq->lower > upper)
620 			break;
621 		info = sseq->info;
622 		list_for_each_entry(p, &info->local_publ, local_publ) {
623 			if (p->scope == scope || (!exact && p->scope < scope))
624 				tipc_dest_push(dports, 0, p->port);
625 		}
626 	}
627 	spin_unlock_bh(&seq->lock);
628 exit:
629 	rcu_read_unlock();
630 }
631 
632 /* 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  */
636 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
637 				   u32 upper, struct tipc_nlist *nodes)
638 {
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;
652 	for (; sseq != stop && sseq->lower <= upper; sseq++) {
653 		info = sseq->info;
654 		list_for_each_entry(publ, &info->all_publ, all_publ) {
655 			tipc_nlist_add(nodes, publ->node);
656 		}
657 	}
658 	spin_unlock_bh(&seq->lock);
659 exit:
660 	rcu_read_unlock();
661 }
662 
663 /* tipc_nametbl_build_group - build list of communication group members
664  */
665 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
666 			      u32 type, u32 scope)
667 {
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;
683 		list_for_each_entry(p, &info->all_publ, all_publ) {
684 			if (p->scope != scope)
685 				continue;
686 			tipc_group_add_member(grp, p->node, p->port, p->lower);
687 		}
688 	}
689 	spin_unlock_bh(&seq->lock);
690 exit:
691 	rcu_read_unlock();
692 }
693 
694 /*
695  * tipc_nametbl_publish - add name publication to network name tables
696  */
697 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
698 					 u32 upper, u32 scope, u32 port_ref,
699 					 u32 key)
700 {
701 	struct publication *publ;
702 	struct sk_buff *buf = NULL;
703 	struct tipc_net *tn = net_generic(net, tipc_net_id);
704 
705 	spin_lock_bh(&tn->nametbl_lock);
706 	if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
707 		pr_warn("Publication failed, local publication limit reached (%u)\n",
708 			TIPC_MAX_PUBLICATIONS);
709 		spin_unlock_bh(&tn->nametbl_lock);
710 		return NULL;
711 	}
712 
713 	publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
714 					tn->own_addr, port_ref, key);
715 	if (likely(publ)) {
716 		tn->nametbl->local_publ_count++;
717 		buf = tipc_named_publish(net, publ);
718 		/* Any pending external events? */
719 		tipc_named_process_backlog(net);
720 	}
721 	spin_unlock_bh(&tn->nametbl_lock);
722 
723 	if (buf)
724 		tipc_node_broadcast(net, buf);
725 	return publ;
726 }
727 
728 /**
729  * tipc_nametbl_withdraw - withdraw name publication from network name tables
730  */
731 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 port,
732 			  u32 key)
733 {
734 	struct publication *publ;
735 	struct sk_buff *skb = NULL;
736 	struct tipc_net *tn = net_generic(net, tipc_net_id);
737 
738 	spin_lock_bh(&tn->nametbl_lock);
739 	publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
740 					port, key);
741 	if (likely(publ)) {
742 		tn->nametbl->local_publ_count--;
743 		skb = tipc_named_withdraw(net, publ);
744 		/* Any pending external events? */
745 		tipc_named_process_backlog(net);
746 		list_del_init(&publ->binding_sock);
747 		kfree_rcu(publ, rcu);
748 	} else {
749 		pr_err("Unable to remove local publication\n"
750 		       "(type=%u, lower=%u, port=%u, key=%u)\n",
751 		       type, lower, port, key);
752 	}
753 	spin_unlock_bh(&tn->nametbl_lock);
754 
755 	if (skb) {
756 		tipc_node_broadcast(net, skb);
757 		return 1;
758 	}
759 	return 0;
760 }
761 
762 /**
763  * tipc_nametbl_subscribe - add a subscription object to the name table
764  */
765 void tipc_nametbl_subscribe(struct tipc_subscription *sub)
766 {
767 	struct tipc_net *tn = tipc_net(sub->net);
768 	struct tipc_subscr *s = &sub->evt.s;
769 	u32 type = tipc_sub_read(s, seq.type);
770 	int index = hash(type);
771 	struct name_seq *seq;
772 	struct tipc_name_seq ns;
773 
774 	spin_lock_bh(&tn->nametbl_lock);
775 	seq = nametbl_find_seq(sub->net, type);
776 	if (!seq)
777 		seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
778 	if (seq) {
779 		spin_lock_bh(&seq->lock);
780 		tipc_nameseq_subscribe(seq, sub);
781 		spin_unlock_bh(&seq->lock);
782 	} else {
783 		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);
786 		pr_warn("Failed to create subscription for {%u,%u,%u}\n",
787 			ns.type, ns.lower, ns.upper);
788 	}
789 	spin_unlock_bh(&tn->nametbl_lock);
790 }
791 
792 /**
793  * tipc_nametbl_unsubscribe - remove a subscription object from name table
794  */
795 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
796 {
797 	struct tipc_subscr *s = &sub->evt.s;
798 	struct tipc_net *tn = tipc_net(sub->net);
799 	struct name_seq *seq;
800 	u32 type = tipc_sub_read(s, seq.type);
801 
802 	spin_lock_bh(&tn->nametbl_lock);
803 	seq = nametbl_find_seq(sub->net, type);
804 	if (seq != NULL) {
805 		spin_lock_bh(&seq->lock);
806 		list_del_init(&sub->nameseq_list);
807 		tipc_sub_put(sub);
808 		if (!seq->first_free && list_empty(&seq->subscriptions)) {
809 			hlist_del_init_rcu(&seq->ns_list);
810 			kfree(seq->sseqs);
811 			spin_unlock_bh(&seq->lock);
812 			kfree_rcu(seq, rcu);
813 		} else {
814 			spin_unlock_bh(&seq->lock);
815 		}
816 	}
817 	spin_unlock_bh(&tn->nametbl_lock);
818 }
819 
820 int tipc_nametbl_init(struct net *net)
821 {
822 	struct tipc_net *tn = net_generic(net, tipc_net_id);
823 	struct name_table *tipc_nametbl;
824 	int i;
825 
826 	tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
827 	if (!tipc_nametbl)
828 		return -ENOMEM;
829 
830 	for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
831 		INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
832 
833 	INIT_LIST_HEAD(&tipc_nametbl->node_scope);
834 	INIT_LIST_HEAD(&tipc_nametbl->cluster_scope);
835 	tn->nametbl = tipc_nametbl;
836 	spin_lock_init(&tn->nametbl_lock);
837 	return 0;
838 }
839 
840 /**
841  * tipc_purge_publications - remove all publications for a given type
842  *
843  * tipc_nametbl_lock must be held when calling this function
844  */
845 static void tipc_purge_publications(struct net *net, struct name_seq *seq)
846 {
847 	struct publication *publ, *safe;
848 	struct sub_seq *sseq;
849 	struct name_info *info;
850 
851 	spin_lock_bh(&seq->lock);
852 	sseq = seq->sseqs;
853 	info = sseq->info;
854 	list_for_each_entry_safe(publ, safe, &info->all_publ, all_publ) {
855 		tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
856 					 publ->port, publ->key);
857 		kfree_rcu(publ, rcu);
858 	}
859 	hlist_del_init_rcu(&seq->ns_list);
860 	kfree(seq->sseqs);
861 	spin_unlock_bh(&seq->lock);
862 
863 	kfree_rcu(seq, rcu);
864 }
865 
866 void tipc_nametbl_stop(struct net *net)
867 {
868 	u32 i;
869 	struct name_seq *seq;
870 	struct hlist_head *seq_head;
871 	struct tipc_net *tn = net_generic(net, tipc_net_id);
872 	struct name_table *tipc_nametbl = tn->nametbl;
873 
874 	/* Verify name table is empty and purge any lingering
875 	 * publications, then release the name table
876 	 */
877 	spin_lock_bh(&tn->nametbl_lock);
878 	for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
879 		if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
880 			continue;
881 		seq_head = &tipc_nametbl->seq_hlist[i];
882 		hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
883 			tipc_purge_publications(net, seq);
884 		}
885 	}
886 	spin_unlock_bh(&tn->nametbl_lock);
887 
888 	synchronize_net();
889 	kfree(tipc_nametbl);
890 
891 }
892 
893 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
894 					struct name_seq *seq,
895 					struct sub_seq *sseq, u32 *last_publ)
896 {
897 	void *hdr;
898 	struct nlattr *attrs;
899 	struct nlattr *publ;
900 	struct publication *p;
901 
902 	if (*last_publ) {
903 		list_for_each_entry(p, &sseq->info->all_publ, all_publ)
904 			if (p->key == *last_publ)
905 				break;
906 		if (p->key != *last_publ)
907 			return -EPIPE;
908 	} else {
909 		p = list_first_entry(&sseq->info->all_publ, struct publication,
910 				     all_publ);
911 	}
912 
913 	list_for_each_entry_from(p, &sseq->info->all_publ, all_publ) {
914 		*last_publ = p->key;
915 
916 		hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
917 				  &tipc_genl_family, NLM_F_MULTI,
918 				  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;
940 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port))
941 			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 
953 publ_msg_full:
954 	nla_nest_cancel(msg->skb, publ);
955 attr_msg_full:
956 	nla_nest_cancel(msg->skb, attrs);
957 msg_full:
958 	genlmsg_cancel(msg->skb, hdr);
959 
960 	return -EMSGSIZE;
961 }
962 
963 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
964 				 u32 *last_lower, u32 *last_publ)
965 {
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 
990 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
991 			    u32 *last_type, u32 *last_lower, u32 *last_publ)
992 {
993 	struct tipc_net *tn = net_generic(net, tipc_net_id);
994 	struct hlist_head *seq_head;
995 	struct name_seq *seq = NULL;
996 	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++) {
1005 		seq_head = &tn->nametbl->seq_hlist[i];
1006 
1007 		if (*last_type) {
1008 			seq = nametbl_find_seq(net, *last_type);
1009 			if (!seq)
1010 				return -EPIPE;
1011 		} else {
1012 			hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1013 				break;
1014 			if (!seq)
1015 				continue;
1016 		}
1017 
1018 		hlist_for_each_entry_from_rcu(seq, ns_list) {
1019 			spin_lock_bh(&seq->lock);
1020 			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 
1035 int 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];
1042 	struct net *net = sock_net(skb->sk);
1043 	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 
1052 	rcu_read_lock();
1053 	err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
1054 	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 	}
1065 	rcu_read_unlock();
1066 
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 }
1074 
1075 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1076 {
1077 	u64 value = (u64)node << 32 | port;
1078 	struct tipc_dest *dst;
1079 
1080 	list_for_each_entry(dst, l, list) {
1081 		if (dst->value != value)
1082 			continue;
1083 		return dst;
1084 	}
1085 	return NULL;
1086 }
1087 
1088 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1089 {
1090 	u64 value = (u64)node << 32 | port;
1091 	struct tipc_dest *dst;
1092 
1093 	if (tipc_dest_find(l, node, port))
1094 		return false;
1095 
1096 	dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1097 	if (unlikely(!dst))
1098 		return false;
1099 	dst->value = value;
1100 	list_add(&dst->list, l);
1101 	return true;
1102 }
1103 
1104 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1105 {
1106 	struct tipc_dest *dst;
1107 
1108 	if (list_empty(l))
1109 		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;
1118 }
1119 
1120 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1121 {
1122 	struct tipc_dest *dst;
1123 
1124 	dst = tipc_dest_find(l, node, port);
1125 	if (!dst)
1126 		return false;
1127 	list_del(&dst->list);
1128 	kfree(dst);
1129 	return true;
1130 }
1131 
1132 void tipc_dest_list_purge(struct list_head *l)
1133 {
1134 	struct tipc_dest *dst, *tmp;
1135 
1136 	list_for_each_entry_safe(dst, tmp, l, list) {
1137 		list_del(&dst->list);
1138 		kfree(dst);
1139 	}
1140 }
1141 
1142 int tipc_dest_list_len(struct list_head *l)
1143 {
1144 	struct tipc_dest *dst;
1145 	int i = 0;
1146 
1147 	list_for_each_entry(dst, l, list) {
1148 		i++;
1149 	}
1150 	return i;
1151 }
1152