xref: /openbmc/linux/net/tipc/name_table.c (revision 8348500f)
1 /*
2  * net/tipc/name_table.c: TIPC name table code
3  *
4  * Copyright (c) 2000-2006, 2014-2015, 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: 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.)
63  */
64 struct name_info {
65 	struct list_head node_list;
66 	struct list_head cluster_list;
67 	struct list_head zone_list;
68 	u32 node_list_size;
69 	u32 cluster_list_size;
70 	u32 zone_list_size;
71 };
72 
73 /**
74  * 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  */
79 struct sub_seq {
80 	u32 lower;
81 	u32 upper;
82 	struct name_info *info;
83 };
84 
85 /**
86  * 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
91  * @first_free: array index of first unused sub-sequence entry
92  * @ns_list: links to adjacent name sequences in hash chain
93  * @subscriptions: list of subscriptions for this 'type'
94  * @lock: spinlock controlling access to publication lists of all sub-sequences
95  * @rcu: RCU callback head used for deferred freeing
96  */
97 struct 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;
105 	struct rcu_head rcu;
106 };
107 
108 static int hash(int x)
109 {
110 	return x & (TIPC_NAMETBL_SIZE - 1);
111 }
112 
113 /**
114  * publ_create - create a publication structure
115  */
116 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
117 				       u32 scope, u32 node, u32 port_ref,
118 				       u32 key)
119 {
120 	struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
121 	if (publ == NULL) {
122 		pr_warn("Publication creation failure, no memory\n");
123 		return NULL;
124 	}
125 
126 	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;
133 	INIT_LIST_HEAD(&publ->pport_list);
134 	return publ;
135 }
136 
137 /**
138  * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
139  */
140 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
141 {
142 	return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
143 }
144 
145 /**
146  * tipc_nameseq_create - create a name sequence structure for the specified 'type'
147  *
148  * Allocates a single sub-sequence structure and sets it to all 0's.
149  */
150 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
151 {
152 	struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
153 	struct sub_seq *sseq = tipc_subseq_alloc(1);
154 
155 	if (!nseq || !sseq) {
156 		pr_warn("Name sequence creation failed, no memory\n");
157 		kfree(nseq);
158 		kfree(sseq);
159 		return NULL;
160 	}
161 
162 	spin_lock_init(&nseq->lock);
163 	nseq->type = type;
164 	nseq->sseqs = sseq;
165 	nseq->alloc = 1;
166 	INIT_HLIST_NODE(&nseq->ns_list);
167 	INIT_LIST_HEAD(&nseq->subscriptions);
168 	hlist_add_head_rcu(&nseq->ns_list, seq_head);
169 	return nseq;
170 }
171 
172 /**
173  * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
174  *
175  * Very time-critical, so binary searches through sub-sequence array.
176  */
177 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
178 					   u32 instance)
179 {
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 	}
194 	return NULL;
195 }
196 
197 /**
198  * nameseq_locate_subseq - determine position of name instance in sub-sequence
199  *
200  * 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  */
206 static 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 /**
226  * tipc_nameseq_insert_publ
227  */
228 static 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)
233 {
234 	struct tipc_subscription *s;
235 	struct tipc_subscription *st;
236 	struct publication *publ;
237 	struct sub_seq *sseq;
238 	struct name_info *info;
239 	int created_subseq = 0;
240 
241 	sseq = nameseq_find_subseq(nseq, lower);
242 	if (sseq) {
243 
244 		/* Lower end overlaps existing entry => need an exact match */
245 		if ((sseq->lower != lower) || (sseq->upper != upper)) {
246 			return NULL;
247 		}
248 
249 		info = sseq->info;
250 
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 		}
257 	} else {
258 		u32 inspos;
259 		struct sub_seq *freesseq;
260 
261 		/* Find where lower end should be inserted */
262 		inspos = nameseq_locate_subseq(nseq, lower);
263 
264 		/* Fail if upper end overlaps into an existing entry */
265 		if ((inspos < nseq->first_free) &&
266 		    (upper >= nseq->sseqs[inspos].lower)) {
267 			return NULL;
268 		}
269 
270 		/* Ensure there is space for new sub-sequence */
271 		if (nseq->first_free == nseq->alloc) {
272 			struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
273 
274 			if (!sseqs) {
275 				pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
276 					type, lower, upper);
277 				return NULL;
278 			}
279 			memcpy(sseqs, nseq->sseqs,
280 			       nseq->alloc * sizeof(struct sub_seq));
281 			kfree(nseq->sseqs);
282 			nseq->sseqs = sseqs;
283 			nseq->alloc *= 2;
284 		}
285 
286 		info = kzalloc(sizeof(*info), GFP_ATOMIC);
287 		if (!info) {
288 			pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
289 				type, lower, upper);
290 			return NULL;
291 		}
292 
293 		INIT_LIST_HEAD(&info->node_list);
294 		INIT_LIST_HEAD(&info->cluster_list);
295 		INIT_LIST_HEAD(&info->zone_list);
296 
297 		/* Insert new sub-sequence */
298 		sseq = &nseq->sseqs[inspos];
299 		freesseq = &nseq->sseqs[nseq->first_free];
300 		memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
301 		memset(sseq, 0, sizeof(*sseq));
302 		nseq->first_free++;
303 		sseq->lower = lower;
304 		sseq->upper = upper;
305 		sseq->info = info;
306 		created_subseq = 1;
307 	}
308 
309 	/* Insert a publication */
310 	publ = publ_create(type, lower, upper, scope, node, port, key);
311 	if (!publ)
312 		return NULL;
313 
314 	list_add(&publ->zone_list, &info->zone_list);
315 	info->zone_list_size++;
316 
317 	if (in_own_cluster(net, node)) {
318 		list_add(&publ->cluster_list, &info->cluster_list);
319 		info->cluster_list_size++;
320 	}
321 
322 	if (in_own_node(net, node)) {
323 		list_add(&publ->node_list, &info->node_list);
324 		info->node_list_size++;
325 	}
326 
327 	/* Any subscriptions waiting for notification?  */
328 	list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
329 		tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
330 					    TIPC_PUBLISHED, publ->ref,
331 					    publ->node, created_subseq);
332 	}
333 	return publ;
334 }
335 
336 /**
337  * tipc_nameseq_remove_publ
338  *
339  * NOTE: There may be cases where TIPC is asked to remove a publication
340  * that is not in the name table.  For example, if another node issues a
341  * publication for a name sequence that overlaps an existing name sequence
342  * the publication will not be recorded, which means the publication won't
343  * be found when the name sequence is later withdrawn by that node.
344  * A failed withdraw request simply returns a failure indication and lets the
345  * caller issue any error or warning messages associated with such a problem.
346  */
347 static struct publication *tipc_nameseq_remove_publ(struct net *net,
348 						    struct name_seq *nseq,
349 						    u32 inst, u32 node,
350 						    u32 ref, u32 key)
351 {
352 	struct publication *publ;
353 	struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
354 	struct name_info *info;
355 	struct sub_seq *free;
356 	struct tipc_subscription *s, *st;
357 	int removed_subseq = 0;
358 
359 	if (!sseq)
360 		return NULL;
361 
362 	info = sseq->info;
363 
364 	/* Locate publication, if it exists */
365 	list_for_each_entry(publ, &info->zone_list, zone_list) {
366 		if ((publ->key == key) && (publ->ref == ref) &&
367 		    (!publ->node || (publ->node == node)))
368 			goto found;
369 	}
370 	return NULL;
371 
372 found:
373 	/* Remove publication from zone scope list */
374 	list_del(&publ->zone_list);
375 	info->zone_list_size--;
376 
377 	/* Remove publication from cluster scope list, if present */
378 	if (in_own_cluster(net, node)) {
379 		list_del(&publ->cluster_list);
380 		info->cluster_list_size--;
381 	}
382 
383 	/* Remove publication from node scope list, if present */
384 	if (in_own_node(net, node)) {
385 		list_del(&publ->node_list);
386 		info->node_list_size--;
387 	}
388 
389 	/* Contract subseq list if no more publications for that subseq */
390 	if (list_empty(&info->zone_list)) {
391 		kfree(info);
392 		free = &nseq->sseqs[nseq->first_free--];
393 		memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
394 		removed_subseq = 1;
395 	}
396 
397 	/* Notify any waiting subscriptions */
398 	list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
399 		tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
400 					    TIPC_WITHDRAWN, publ->ref,
401 					    publ->node, removed_subseq);
402 	}
403 
404 	return publ;
405 }
406 
407 /**
408  * tipc_nameseq_subscribe - attach a subscription, and optionally
409  * issue the prescribed number of events if there is any sub-
410  * sequence overlapping with the requested sequence
411  */
412 static void tipc_nameseq_subscribe(struct name_seq *nseq,
413 				   struct tipc_subscription *s,
414 				   bool status)
415 {
416 	struct sub_seq *sseq = nseq->sseqs;
417 	struct tipc_name_seq ns;
418 
419 	tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
420 
421 	tipc_subscrp_get(s);
422 	list_add(&s->nameseq_list, &nseq->subscriptions);
423 
424 	if (!status || !sseq)
425 		return;
426 
427 	while (sseq != &nseq->sseqs[nseq->first_free]) {
428 		if (tipc_subscrp_check_overlap(&ns, sseq->lower, sseq->upper)) {
429 			struct publication *crs;
430 			struct name_info *info = sseq->info;
431 			int must_report = 1;
432 
433 			list_for_each_entry(crs, &info->zone_list, zone_list) {
434 				tipc_subscrp_report_overlap(s, sseq->lower,
435 							    sseq->upper,
436 							    TIPC_PUBLISHED,
437 							    crs->ref, crs->node,
438 							    must_report);
439 				must_report = 0;
440 			}
441 		}
442 		sseq++;
443 	}
444 }
445 
446 static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
447 {
448 	struct tipc_net *tn = net_generic(net, tipc_net_id);
449 	struct hlist_head *seq_head;
450 	struct name_seq *ns;
451 
452 	seq_head = &tn->nametbl->seq_hlist[hash(type)];
453 	hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
454 		if (ns->type == type)
455 			return ns;
456 	}
457 
458 	return NULL;
459 };
460 
461 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
462 					     u32 lower, u32 upper, u32 scope,
463 					     u32 node, u32 port, u32 key)
464 {
465 	struct tipc_net *tn = net_generic(net, tipc_net_id);
466 	struct publication *publ;
467 	struct name_seq *seq = nametbl_find_seq(net, type);
468 	int index = hash(type);
469 
470 	if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
471 	    (lower > upper)) {
472 		pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
473 			 type, lower, upper, scope);
474 		return NULL;
475 	}
476 
477 	if (!seq)
478 		seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
479 	if (!seq)
480 		return NULL;
481 
482 	spin_lock_bh(&seq->lock);
483 	publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
484 					scope, node, port, key);
485 	spin_unlock_bh(&seq->lock);
486 	return publ;
487 }
488 
489 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
490 					     u32 lower, u32 node, u32 ref,
491 					     u32 key)
492 {
493 	struct publication *publ;
494 	struct name_seq *seq = nametbl_find_seq(net, type);
495 
496 	if (!seq)
497 		return NULL;
498 
499 	spin_lock_bh(&seq->lock);
500 	publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
501 	if (!seq->first_free && list_empty(&seq->subscriptions)) {
502 		hlist_del_init_rcu(&seq->ns_list);
503 		kfree(seq->sseqs);
504 		spin_unlock_bh(&seq->lock);
505 		kfree_rcu(seq, rcu);
506 		return publ;
507 	}
508 	spin_unlock_bh(&seq->lock);
509 	return publ;
510 }
511 
512 /**
513  * tipc_nametbl_translate - perform name translation
514  *
515  * On entry, 'destnode' is the search domain used during translation.
516  *
517  * On exit:
518  * - if name translation is deferred to another node/cluster/zone,
519  *   leaves 'destnode' unchanged (will be non-zero) and returns 0
520  * - if name translation is attempted and succeeds, sets 'destnode'
521  *   to publishing node and returns port reference (will be non-zero)
522  * - if name translation is attempted and fails, sets 'destnode' to 0
523  *   and returns 0
524  */
525 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
526 			   u32 *destnode)
527 {
528 	struct tipc_net *tn = net_generic(net, tipc_net_id);
529 	struct sub_seq *sseq;
530 	struct name_info *info;
531 	struct publication *publ;
532 	struct name_seq *seq;
533 	u32 ref = 0;
534 	u32 node = 0;
535 
536 	if (!tipc_in_scope(*destnode, tn->own_addr))
537 		return 0;
538 
539 	rcu_read_lock();
540 	seq = nametbl_find_seq(net, type);
541 	if (unlikely(!seq))
542 		goto not_found;
543 	spin_lock_bh(&seq->lock);
544 	sseq = nameseq_find_subseq(seq, instance);
545 	if (unlikely(!sseq))
546 		goto no_match;
547 	info = sseq->info;
548 
549 	/* Closest-First Algorithm */
550 	if (likely(!*destnode)) {
551 		if (!list_empty(&info->node_list)) {
552 			publ = list_first_entry(&info->node_list,
553 						struct publication,
554 						node_list);
555 			list_move_tail(&publ->node_list,
556 				       &info->node_list);
557 		} else if (!list_empty(&info->cluster_list)) {
558 			publ = list_first_entry(&info->cluster_list,
559 						struct publication,
560 						cluster_list);
561 			list_move_tail(&publ->cluster_list,
562 				       &info->cluster_list);
563 		} else {
564 			publ = list_first_entry(&info->zone_list,
565 						struct publication,
566 						zone_list);
567 			list_move_tail(&publ->zone_list,
568 				       &info->zone_list);
569 		}
570 	}
571 
572 	/* Round-Robin Algorithm */
573 	else if (*destnode == tn->own_addr) {
574 		if (list_empty(&info->node_list))
575 			goto no_match;
576 		publ = list_first_entry(&info->node_list, struct publication,
577 					node_list);
578 		list_move_tail(&publ->node_list, &info->node_list);
579 	} else if (in_own_cluster_exact(net, *destnode)) {
580 		if (list_empty(&info->cluster_list))
581 			goto no_match;
582 		publ = list_first_entry(&info->cluster_list, struct publication,
583 					cluster_list);
584 		list_move_tail(&publ->cluster_list, &info->cluster_list);
585 	} else {
586 		publ = list_first_entry(&info->zone_list, struct publication,
587 					zone_list);
588 		list_move_tail(&publ->zone_list, &info->zone_list);
589 	}
590 
591 	ref = publ->ref;
592 	node = publ->node;
593 no_match:
594 	spin_unlock_bh(&seq->lock);
595 not_found:
596 	rcu_read_unlock();
597 	*destnode = node;
598 	return ref;
599 }
600 
601 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 domain,
602 			 struct list_head *dsts, int *dstcnt, u32 exclude,
603 			 bool all)
604 {
605 	u32 self = tipc_own_addr(net);
606 	struct publication *publ;
607 	struct name_info *info;
608 	struct name_seq *seq;
609 	struct sub_seq *sseq;
610 
611 	if (!tipc_in_scope(domain, self))
612 		return false;
613 
614 	*dstcnt = 0;
615 	rcu_read_lock();
616 	seq = nametbl_find_seq(net, type);
617 	if (unlikely(!seq))
618 		goto exit;
619 	spin_lock_bh(&seq->lock);
620 	sseq = nameseq_find_subseq(seq, instance);
621 	if (likely(sseq)) {
622 		info = sseq->info;
623 		list_for_each_entry(publ, &info->zone_list, zone_list) {
624 			if (!tipc_in_scope(domain, publ->node))
625 				continue;
626 			if (publ->ref == exclude && publ->node == self)
627 				continue;
628 			tipc_dest_push(dsts, publ->node, publ->ref);
629 			(*dstcnt)++;
630 			if (all)
631 				continue;
632 			list_move_tail(&publ->zone_list, &info->zone_list);
633 			break;
634 		}
635 	}
636 	spin_unlock_bh(&seq->lock);
637 exit:
638 	rcu_read_unlock();
639 	return !list_empty(dsts);
640 }
641 
642 int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper,
643 			      u32 limit, struct list_head *dports)
644 {
645 	struct name_seq *seq;
646 	struct sub_seq *sseq;
647 	struct sub_seq *sseq_stop;
648 	struct name_info *info;
649 	int res = 0;
650 
651 	rcu_read_lock();
652 	seq = nametbl_find_seq(net, type);
653 	if (!seq)
654 		goto exit;
655 
656 	spin_lock_bh(&seq->lock);
657 	sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
658 	sseq_stop = seq->sseqs + seq->first_free;
659 	for (; sseq != sseq_stop; sseq++) {
660 		struct publication *publ;
661 
662 		if (sseq->lower > upper)
663 			break;
664 
665 		info = sseq->info;
666 		list_for_each_entry(publ, &info->node_list, node_list) {
667 			if (publ->scope <= limit)
668 				tipc_dest_push(dports, 0, publ->ref);
669 		}
670 
671 		if (info->cluster_list_size != info->node_list_size)
672 			res = 1;
673 	}
674 	spin_unlock_bh(&seq->lock);
675 exit:
676 	rcu_read_unlock();
677 	return res;
678 }
679 
680 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
681  * - Creates list of nodes that overlap the given multicast address
682  * - Determines if any node local ports overlap
683  */
684 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
685 				   u32 upper, u32 domain,
686 				   struct tipc_nlist *nodes)
687 {
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;
701 	for (; sseq != stop && sseq->lower <= upper; sseq++) {
702 		info = sseq->info;
703 		list_for_each_entry(publ, &info->zone_list, zone_list) {
704 			if (tipc_in_scope(domain, publ->node))
705 				tipc_nlist_add(nodes, publ->node);
706 		}
707 	}
708 	spin_unlock_bh(&seq->lock);
709 exit:
710 	rcu_read_unlock();
711 }
712 
713 /* tipc_nametbl_build_group - build list of communication group members
714  */
715 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
716 			      u32 type, u32 domain)
717 {
718 	struct sub_seq *sseq, *stop;
719 	struct name_info *info;
720 	struct publication *p;
721 	struct name_seq *seq;
722 
723 	rcu_read_lock();
724 	seq = nametbl_find_seq(net, type);
725 	if (!seq)
726 		goto exit;
727 
728 	spin_lock_bh(&seq->lock);
729 	sseq = seq->sseqs;
730 	stop = seq->sseqs + seq->first_free;
731 	for (; sseq != stop; sseq++) {
732 		info = sseq->info;
733 		list_for_each_entry(p, &info->zone_list, zone_list) {
734 			if (!tipc_in_scope(domain, p->node))
735 				continue;
736 			tipc_group_add_member(grp, p->node, p->ref, p->lower);
737 		}
738 	}
739 	spin_unlock_bh(&seq->lock);
740 exit:
741 	rcu_read_unlock();
742 }
743 
744 /*
745  * tipc_nametbl_publish - add name publication to network name tables
746  */
747 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
748 					 u32 upper, u32 scope, u32 port_ref,
749 					 u32 key)
750 {
751 	struct publication *publ;
752 	struct sk_buff *buf = NULL;
753 	struct tipc_net *tn = net_generic(net, tipc_net_id);
754 
755 	spin_lock_bh(&tn->nametbl_lock);
756 	if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
757 		pr_warn("Publication failed, local publication limit reached (%u)\n",
758 			TIPC_MAX_PUBLICATIONS);
759 		spin_unlock_bh(&tn->nametbl_lock);
760 		return NULL;
761 	}
762 
763 	publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
764 					tn->own_addr, port_ref, key);
765 	if (likely(publ)) {
766 		tn->nametbl->local_publ_count++;
767 		buf = tipc_named_publish(net, publ);
768 		/* Any pending external events? */
769 		tipc_named_process_backlog(net);
770 	}
771 	spin_unlock_bh(&tn->nametbl_lock);
772 
773 	if (buf)
774 		tipc_node_broadcast(net, buf);
775 	return publ;
776 }
777 
778 /**
779  * tipc_nametbl_withdraw - withdraw name publication from network name tables
780  */
781 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
782 			  u32 key)
783 {
784 	struct publication *publ;
785 	struct sk_buff *skb = NULL;
786 	struct tipc_net *tn = net_generic(net, tipc_net_id);
787 
788 	spin_lock_bh(&tn->nametbl_lock);
789 	publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
790 					ref, key);
791 	if (likely(publ)) {
792 		tn->nametbl->local_publ_count--;
793 		skb = tipc_named_withdraw(net, publ);
794 		/* Any pending external events? */
795 		tipc_named_process_backlog(net);
796 		list_del_init(&publ->pport_list);
797 		kfree_rcu(publ, rcu);
798 	} else {
799 		pr_err("Unable to remove local publication\n"
800 		       "(type=%u, lower=%u, ref=%u, key=%u)\n",
801 		       type, lower, ref, key);
802 	}
803 	spin_unlock_bh(&tn->nametbl_lock);
804 
805 	if (skb) {
806 		tipc_node_broadcast(net, skb);
807 		return 1;
808 	}
809 	return 0;
810 }
811 
812 /**
813  * tipc_nametbl_subscribe - add a subscription object to the name table
814  */
815 void tipc_nametbl_subscribe(struct tipc_subscription *s, bool status)
816 {
817 	struct tipc_net *tn = net_generic(s->net, tipc_net_id);
818 	u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
819 	int index = hash(type);
820 	struct name_seq *seq;
821 	struct tipc_name_seq ns;
822 
823 	spin_lock_bh(&tn->nametbl_lock);
824 	seq = nametbl_find_seq(s->net, type);
825 	if (!seq)
826 		seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
827 	if (seq) {
828 		spin_lock_bh(&seq->lock);
829 		tipc_nameseq_subscribe(seq, s, status);
830 		spin_unlock_bh(&seq->lock);
831 	} else {
832 		tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
833 		pr_warn("Failed to create subscription for {%u,%u,%u}\n",
834 			ns.type, ns.lower, ns.upper);
835 	}
836 	spin_unlock_bh(&tn->nametbl_lock);
837 }
838 
839 /**
840  * tipc_nametbl_unsubscribe - remove a subscription object from name table
841  */
842 void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
843 {
844 	struct tipc_net *tn = net_generic(s->net, tipc_net_id);
845 	struct name_seq *seq;
846 	u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
847 
848 	spin_lock_bh(&tn->nametbl_lock);
849 	seq = nametbl_find_seq(s->net, type);
850 	if (seq != NULL) {
851 		spin_lock_bh(&seq->lock);
852 		list_del_init(&s->nameseq_list);
853 		tipc_subscrp_put(s);
854 		if (!seq->first_free && list_empty(&seq->subscriptions)) {
855 			hlist_del_init_rcu(&seq->ns_list);
856 			kfree(seq->sseqs);
857 			spin_unlock_bh(&seq->lock);
858 			kfree_rcu(seq, rcu);
859 		} else {
860 			spin_unlock_bh(&seq->lock);
861 		}
862 	}
863 	spin_unlock_bh(&tn->nametbl_lock);
864 }
865 
866 int tipc_nametbl_init(struct net *net)
867 {
868 	struct tipc_net *tn = net_generic(net, tipc_net_id);
869 	struct name_table *tipc_nametbl;
870 	int i;
871 
872 	tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
873 	if (!tipc_nametbl)
874 		return -ENOMEM;
875 
876 	for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
877 		INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
878 
879 	INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
880 	INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
881 	INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
882 	tn->nametbl = tipc_nametbl;
883 	spin_lock_init(&tn->nametbl_lock);
884 	return 0;
885 }
886 
887 /**
888  * tipc_purge_publications - remove all publications for a given type
889  *
890  * tipc_nametbl_lock must be held when calling this function
891  */
892 static void tipc_purge_publications(struct net *net, struct name_seq *seq)
893 {
894 	struct publication *publ, *safe;
895 	struct sub_seq *sseq;
896 	struct name_info *info;
897 
898 	spin_lock_bh(&seq->lock);
899 	sseq = seq->sseqs;
900 	info = sseq->info;
901 	list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
902 		tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
903 					 publ->ref, publ->key);
904 		kfree_rcu(publ, rcu);
905 	}
906 	hlist_del_init_rcu(&seq->ns_list);
907 	kfree(seq->sseqs);
908 	spin_unlock_bh(&seq->lock);
909 
910 	kfree_rcu(seq, rcu);
911 }
912 
913 void tipc_nametbl_stop(struct net *net)
914 {
915 	u32 i;
916 	struct name_seq *seq;
917 	struct hlist_head *seq_head;
918 	struct tipc_net *tn = net_generic(net, tipc_net_id);
919 	struct name_table *tipc_nametbl = tn->nametbl;
920 
921 	/* Verify name table is empty and purge any lingering
922 	 * publications, then release the name table
923 	 */
924 	spin_lock_bh(&tn->nametbl_lock);
925 	for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
926 		if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
927 			continue;
928 		seq_head = &tipc_nametbl->seq_hlist[i];
929 		hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
930 			tipc_purge_publications(net, seq);
931 		}
932 	}
933 	spin_unlock_bh(&tn->nametbl_lock);
934 
935 	synchronize_net();
936 	kfree(tipc_nametbl);
937 
938 }
939 
940 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
941 					struct name_seq *seq,
942 					struct sub_seq *sseq, u32 *last_publ)
943 {
944 	void *hdr;
945 	struct nlattr *attrs;
946 	struct nlattr *publ;
947 	struct publication *p;
948 
949 	if (*last_publ) {
950 		list_for_each_entry(p, &sseq->info->zone_list, zone_list)
951 			if (p->key == *last_publ)
952 				break;
953 		if (p->key != *last_publ)
954 			return -EPIPE;
955 	} else {
956 		p = list_first_entry(&sseq->info->zone_list, struct publication,
957 				     zone_list);
958 	}
959 
960 	list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
961 		*last_publ = p->key;
962 
963 		hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
964 				  &tipc_genl_family, NLM_F_MULTI,
965 				  TIPC_NL_NAME_TABLE_GET);
966 		if (!hdr)
967 			return -EMSGSIZE;
968 
969 		attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
970 		if (!attrs)
971 			goto msg_full;
972 
973 		publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
974 		if (!publ)
975 			goto attr_msg_full;
976 
977 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
978 			goto publ_msg_full;
979 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
980 			goto publ_msg_full;
981 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
982 			goto publ_msg_full;
983 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
984 			goto publ_msg_full;
985 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
986 			goto publ_msg_full;
987 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
988 			goto publ_msg_full;
989 		if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
990 			goto publ_msg_full;
991 
992 		nla_nest_end(msg->skb, publ);
993 		nla_nest_end(msg->skb, attrs);
994 		genlmsg_end(msg->skb, hdr);
995 	}
996 	*last_publ = 0;
997 
998 	return 0;
999 
1000 publ_msg_full:
1001 	nla_nest_cancel(msg->skb, publ);
1002 attr_msg_full:
1003 	nla_nest_cancel(msg->skb, attrs);
1004 msg_full:
1005 	genlmsg_cancel(msg->skb, hdr);
1006 
1007 	return -EMSGSIZE;
1008 }
1009 
1010 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1011 				 u32 *last_lower, u32 *last_publ)
1012 {
1013 	struct sub_seq *sseq;
1014 	struct sub_seq *sseq_start;
1015 	int err;
1016 
1017 	if (*last_lower) {
1018 		sseq_start = nameseq_find_subseq(seq, *last_lower);
1019 		if (!sseq_start)
1020 			return -EPIPE;
1021 	} else {
1022 		sseq_start = seq->sseqs;
1023 	}
1024 
1025 	for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
1026 		err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
1027 		if (err) {
1028 			*last_lower = sseq->lower;
1029 			return err;
1030 		}
1031 	}
1032 	*last_lower = 0;
1033 
1034 	return 0;
1035 }
1036 
1037 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
1038 			    u32 *last_type, u32 *last_lower, u32 *last_publ)
1039 {
1040 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1041 	struct hlist_head *seq_head;
1042 	struct name_seq *seq = NULL;
1043 	int err;
1044 	int i;
1045 
1046 	if (*last_type)
1047 		i = hash(*last_type);
1048 	else
1049 		i = 0;
1050 
1051 	for (; i < TIPC_NAMETBL_SIZE; i++) {
1052 		seq_head = &tn->nametbl->seq_hlist[i];
1053 
1054 		if (*last_type) {
1055 			seq = nametbl_find_seq(net, *last_type);
1056 			if (!seq)
1057 				return -EPIPE;
1058 		} else {
1059 			hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1060 				break;
1061 			if (!seq)
1062 				continue;
1063 		}
1064 
1065 		hlist_for_each_entry_from_rcu(seq, ns_list) {
1066 			spin_lock_bh(&seq->lock);
1067 			err = __tipc_nl_subseq_list(msg, seq, last_lower,
1068 						    last_publ);
1069 
1070 			if (err) {
1071 				*last_type = seq->type;
1072 				spin_unlock_bh(&seq->lock);
1073 				return err;
1074 			}
1075 			spin_unlock_bh(&seq->lock);
1076 		}
1077 		*last_type = 0;
1078 	}
1079 	return 0;
1080 }
1081 
1082 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1083 {
1084 	int err;
1085 	int done = cb->args[3];
1086 	u32 last_type = cb->args[0];
1087 	u32 last_lower = cb->args[1];
1088 	u32 last_publ = cb->args[2];
1089 	struct net *net = sock_net(skb->sk);
1090 	struct tipc_nl_msg msg;
1091 
1092 	if (done)
1093 		return 0;
1094 
1095 	msg.skb = skb;
1096 	msg.portid = NETLINK_CB(cb->skb).portid;
1097 	msg.seq = cb->nlh->nlmsg_seq;
1098 
1099 	rcu_read_lock();
1100 	err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
1101 	if (!err) {
1102 		done = 1;
1103 	} else if (err != -EMSGSIZE) {
1104 		/* We never set seq or call nl_dump_check_consistent() this
1105 		 * means that setting prev_seq here will cause the consistence
1106 		 * check to fail in the netlink callback handler. Resulting in
1107 		 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1108 		 * we got an error.
1109 		 */
1110 		cb->prev_seq = 1;
1111 	}
1112 	rcu_read_unlock();
1113 
1114 	cb->args[0] = last_type;
1115 	cb->args[1] = last_lower;
1116 	cb->args[2] = last_publ;
1117 	cb->args[3] = done;
1118 
1119 	return skb->len;
1120 }
1121 
1122 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1123 {
1124 	u64 value = (u64)node << 32 | port;
1125 	struct tipc_dest *dst;
1126 
1127 	list_for_each_entry(dst, l, list) {
1128 		if (dst->value != value)
1129 			continue;
1130 		return dst;
1131 	}
1132 	return NULL;
1133 }
1134 
1135 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1136 {
1137 	u64 value = (u64)node << 32 | port;
1138 	struct tipc_dest *dst;
1139 
1140 	if (tipc_dest_find(l, node, port))
1141 		return false;
1142 
1143 	dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1144 	if (unlikely(!dst))
1145 		return false;
1146 	dst->value = value;
1147 	list_add(&dst->list, l);
1148 	return true;
1149 }
1150 
1151 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1152 {
1153 	struct tipc_dest *dst;
1154 
1155 	if (list_empty(l))
1156 		return false;
1157 	dst = list_first_entry(l, typeof(*dst), list);
1158 	if (port)
1159 		*port = dst->port;
1160 	if (node)
1161 		*node = dst->node;
1162 	list_del(&dst->list);
1163 	kfree(dst);
1164 	return true;
1165 }
1166 
1167 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1168 {
1169 	struct tipc_dest *dst;
1170 
1171 	dst = tipc_dest_find(l, node, port);
1172 	if (!dst)
1173 		return false;
1174 	list_del(&dst->list);
1175 	kfree(dst);
1176 	return true;
1177 }
1178 
1179 void tipc_dest_list_purge(struct list_head *l)
1180 {
1181 	struct tipc_dest *dst, *tmp;
1182 
1183 	list_for_each_entry_safe(dst, tmp, l, list) {
1184 		list_del(&dst->list);
1185 		kfree(dst);
1186 	}
1187 }
1188 
1189 int tipc_dest_list_len(struct list_head *l)
1190 {
1191 	struct tipc_dest *dst;
1192 	int i = 0;
1193 
1194 	list_for_each_entry(dst, l, list) {
1195 		i++;
1196 	}
1197 	return i;
1198 }
1199