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