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