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