tipc: move creation of publication item one level up in call chain
[platform/kernel/linux-starfive.git] / net / tipc / name_table.c
1 /*
2  * net/tipc/name_table.c: TIPC name table code
3  *
4  * Copyright (c) 2000-2006, 2014-2018, Ericsson AB
5  * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
6  * Copyright (c) 2020-2021, Red Hat Inc
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the names of the copyright holders nor the names of its
18  *    contributors may be used to endorse or promote products derived from
19  *    this software without specific prior written permission.
20  *
21  * Alternatively, this software may be distributed under the terms of the
22  * GNU General Public License ("GPL") version 2 as published by the Free
23  * Software Foundation.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <net/sock.h>
39 #include <linux/list_sort.h>
40 #include <linux/rbtree_augmented.h>
41 #include "core.h"
42 #include "netlink.h"
43 #include "name_table.h"
44 #include "name_distr.h"
45 #include "subscr.h"
46 #include "bcast.h"
47 #include "addr.h"
48 #include "node.h"
49 #include "group.h"
50
51 /**
52  * struct service_range - container for all bindings of a service range
53  * @lower: service range lower bound
54  * @upper: service range upper bound
55  * @tree_node: member of service range RB tree
56  * @max: largest 'upper' in this node subtree
57  * @local_publ: list of identical publications made from this node
58  *   Used by closest_first lookup and multicast lookup algorithm
59  * @all_publ: all publications identical to this one, whatever node and scope
60  *   Used by round-robin lookup algorithm
61  */
62 struct service_range {
63         u32 lower;
64         u32 upper;
65         struct rb_node tree_node;
66         u32 max;
67         struct list_head local_publ;
68         struct list_head all_publ;
69 };
70
71 /**
72  * struct tipc_service - container for all published instances of a service type
73  * @type: 32 bit 'type' value for service
74  * @publ_cnt: increasing counter for publications in this service
75  * @ranges: rb tree containing all service ranges for this service
76  * @service_list: links to adjacent name ranges in hash chain
77  * @subscriptions: list of subscriptions for this service type
78  * @lock: spinlock controlling access to pertaining service ranges/publications
79  * @rcu: RCU callback head used for deferred freeing
80  */
81 struct tipc_service {
82         u32 type;
83         u32 publ_cnt;
84         struct rb_root ranges;
85         struct hlist_node service_list;
86         struct list_head subscriptions;
87         spinlock_t lock; /* Covers service range list */
88         struct rcu_head rcu;
89 };
90
91 #define service_range_upper(sr) ((sr)->upper)
92 RB_DECLARE_CALLBACKS_MAX(static, sr_callbacks,
93                          struct service_range, tree_node, u32, max,
94                          service_range_upper)
95
96 #define service_range_entry(rbtree_node)                                \
97         (container_of(rbtree_node, struct service_range, tree_node))
98
99 #define service_range_overlap(sr, start, end)                           \
100         ((sr)->lower <= (end) && (sr)->upper >= (start))
101
102 /**
103  * service_range_foreach_match - iterate over tipc service rbtree for each
104  *                               range match
105  * @sr: the service range pointer as a loop cursor
106  * @sc: the pointer to tipc service which holds the service range rbtree
107  * @start: beginning of the search range (end >= start) for matching
108  * @end: end of the search range (end >= start) for matching
109  */
110 #define service_range_foreach_match(sr, sc, start, end)                 \
111         for (sr = service_range_match_first((sc)->ranges.rb_node,       \
112                                             start,                      \
113                                             end);                       \
114              sr;                                                        \
115              sr = service_range_match_next(&(sr)->tree_node,            \
116                                            start,                       \
117                                            end))
118
119 /**
120  * service_range_match_first - find first service range matching a range
121  * @n: the root node of service range rbtree for searching
122  * @start: beginning of the search range (end >= start) for matching
123  * @end: end of the search range (end >= start) for matching
124  *
125  * Return: the leftmost service range node in the rbtree that overlaps the
126  * specific range if any. Otherwise, returns NULL.
127  */
128 static struct service_range *service_range_match_first(struct rb_node *n,
129                                                        u32 start, u32 end)
130 {
131         struct service_range *sr;
132         struct rb_node *l, *r;
133
134         /* Non overlaps in tree at all? */
135         if (!n || service_range_entry(n)->max < start)
136                 return NULL;
137
138         while (n) {
139                 l = n->rb_left;
140                 if (l && service_range_entry(l)->max >= start) {
141                         /* A leftmost overlap range node must be one in the left
142                          * subtree. If not, it has lower > end, then nodes on
143                          * the right side cannot satisfy the condition either.
144                          */
145                         n = l;
146                         continue;
147                 }
148
149                 /* No one in the left subtree can match, return if this node is
150                  * an overlap i.e. leftmost.
151                  */
152                 sr = service_range_entry(n);
153                 if (service_range_overlap(sr, start, end))
154                         return sr;
155
156                 /* Ok, try to lookup on the right side */
157                 r = n->rb_right;
158                 if (sr->lower <= end &&
159                     r && service_range_entry(r)->max >= start) {
160                         n = r;
161                         continue;
162                 }
163                 break;
164         }
165
166         return NULL;
167 }
168
169 /**
170  * service_range_match_next - find next service range matching a range
171  * @n: a node in service range rbtree from which the searching starts
172  * @start: beginning of the search range (end >= start) for matching
173  * @end: end of the search range (end >= start) for matching
174  *
175  * Return: the next service range node to the given node in the rbtree that
176  * overlaps the specific range if any. Otherwise, returns NULL.
177  */
178 static struct service_range *service_range_match_next(struct rb_node *n,
179                                                       u32 start, u32 end)
180 {
181         struct service_range *sr;
182         struct rb_node *p, *r;
183
184         while (n) {
185                 r = n->rb_right;
186                 if (r && service_range_entry(r)->max >= start)
187                         /* A next overlap range node must be one in the right
188                          * subtree. If not, it has lower > end, then any next
189                          * successor (- an ancestor) of this node cannot
190                          * satisfy the condition either.
191                          */
192                         return service_range_match_first(r, start, end);
193
194                 /* No one in the right subtree can match, go up to find an
195                  * ancestor of this node which is parent of a left-hand child.
196                  */
197                 while ((p = rb_parent(n)) && n == p->rb_right)
198                         n = p;
199                 if (!p)
200                         break;
201
202                 /* Return if this ancestor is an overlap */
203                 sr = service_range_entry(p);
204                 if (service_range_overlap(sr, start, end))
205                         return sr;
206
207                 /* Ok, try to lookup more from this ancestor */
208                 if (sr->lower <= end) {
209                         n = p;
210                         continue;
211                 }
212                 break;
213         }
214
215         return NULL;
216 }
217
218 static int hash(int x)
219 {
220         return x & (TIPC_NAMETBL_SIZE - 1);
221 }
222
223 /**
224  * tipc_publ_create - create a publication structure
225  * @type: name sequence type
226  * @lower: name sequence lower bound
227  * @upper: name sequence upper bound
228  * @scope: publication scope
229  * @node: network address of publishing socket
230  * @port: publishing port
231  * @key: publication key
232  */
233 static struct publication *tipc_publ_create(u32 type, u32 lower, u32 upper,
234                                             u32 scope, u32 node, u32 port,
235                                             u32 key)
236 {
237         struct publication *p = kzalloc(sizeof(*p), GFP_ATOMIC);
238
239         if (!p)
240                 return NULL;
241
242         p->sr.type = type;
243         p->sr.lower = lower;
244         p->sr.upper = upper;
245         p->scope = scope;
246         p->sk.node = node;
247         p->sk.ref = port;
248         p->key = key;
249         INIT_LIST_HEAD(&p->binding_sock);
250         INIT_LIST_HEAD(&p->binding_node);
251         INIT_LIST_HEAD(&p->local_publ);
252         INIT_LIST_HEAD(&p->all_publ);
253         INIT_LIST_HEAD(&p->list);
254         return p;
255 }
256
257 /**
258  * tipc_service_create - create a service structure for the specified 'type'
259  * @type: service type
260  * @hd: name_table services list
261  *
262  * Allocates a single range structure and sets it to all 0's.
263  */
264 static struct tipc_service *tipc_service_create(u32 type, struct hlist_head *hd)
265 {
266         struct tipc_service *service = kzalloc(sizeof(*service), GFP_ATOMIC);
267
268         if (!service) {
269                 pr_warn("Service creation failed, no memory\n");
270                 return NULL;
271         }
272
273         spin_lock_init(&service->lock);
274         service->type = type;
275         service->ranges = RB_ROOT;
276         INIT_HLIST_NODE(&service->service_list);
277         INIT_LIST_HEAD(&service->subscriptions);
278         hlist_add_head_rcu(&service->service_list, hd);
279         return service;
280 }
281
282 /*  tipc_service_find_range - find service range matching publication parameters
283  */
284 static struct service_range *tipc_service_find_range(struct tipc_service *sc,
285                                                      u32 lower, u32 upper)
286 {
287         struct service_range *sr;
288
289         service_range_foreach_match(sr, sc, lower, upper) {
290                 /* Look for exact match */
291                 if (sr->lower == lower && sr->upper == upper)
292                         return sr;
293         }
294
295         return NULL;
296 }
297
298 static struct service_range *tipc_service_create_range(struct tipc_service *sc,
299                                                        u32 lower, u32 upper)
300 {
301         struct rb_node **n, *parent = NULL;
302         struct service_range *sr;
303
304         n = &sc->ranges.rb_node;
305         while (*n) {
306                 parent = *n;
307                 sr = service_range_entry(parent);
308                 if (lower == sr->lower && upper == sr->upper)
309                         return sr;
310                 if (sr->max < upper)
311                         sr->max = upper;
312                 if (lower <= sr->lower)
313                         n = &parent->rb_left;
314                 else
315                         n = &parent->rb_right;
316         }
317         sr = kzalloc(sizeof(*sr), GFP_ATOMIC);
318         if (!sr)
319                 return NULL;
320         sr->lower = lower;
321         sr->upper = upper;
322         sr->max = upper;
323         INIT_LIST_HEAD(&sr->local_publ);
324         INIT_LIST_HEAD(&sr->all_publ);
325         rb_link_node(&sr->tree_node, parent, n);
326         rb_insert_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
327         return sr;
328 }
329
330 static bool tipc_service_insert_publ(struct net *net,
331                                      struct tipc_service *sc,
332                                      struct publication *p)
333 {
334         struct tipc_subscription *sub, *tmp;
335         struct service_range *sr;
336         struct publication *_p;
337         u32 node = p->sk.node;
338         bool first = false;
339         bool res = false;
340
341         spin_lock_bh(&sc->lock);
342         sr = tipc_service_create_range(sc, p->sr.lower, p->sr.upper);
343         if (!sr)
344                 goto  exit;
345
346         first = list_empty(&sr->all_publ);
347
348         /* Return if the publication already exists */
349         list_for_each_entry(_p, &sr->all_publ, all_publ) {
350                 if (_p->key == p->key && (!_p->sk.node || _p->sk.node == node))
351                         goto exit;
352         }
353
354         if (in_own_node(net, p->sk.node))
355                 list_add(&p->local_publ, &sr->local_publ);
356         list_add(&p->all_publ, &sr->all_publ);
357         p->id = sc->publ_cnt++;
358
359         /* Any subscriptions waiting for notification?  */
360         list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
361                 tipc_sub_report_overlap(sub, p->sr.lower, p->sr.upper,
362                                         TIPC_PUBLISHED, p->sk.ref, p->sk.node,
363                                         p->scope, first);
364         }
365         res = true;
366 exit:
367         if (!res)
368                 pr_warn("Failed to bind to %u,%u,%u\n",
369                         p->sr.type, p->sr.lower, p->sr.upper);
370         spin_unlock_bh(&sc->lock);
371         return res;
372 }
373
374 /**
375  * tipc_service_remove_publ - remove a publication from a service
376  * @sr: service_range to remove publication from
377  * @node: target node
378  * @key: target publication key
379  */
380 static struct publication *tipc_service_remove_publ(struct service_range *sr,
381                                                     u32 node, u32 key)
382 {
383         struct publication *p;
384
385         list_for_each_entry(p, &sr->all_publ, all_publ) {
386                 if (p->key != key || (node && node != p->sk.node))
387                         continue;
388                 list_del(&p->all_publ);
389                 list_del(&p->local_publ);
390                 return p;
391         }
392         return NULL;
393 }
394
395 /*
396  * Code reused: time_after32() for the same purpose
397  */
398 #define publication_after(pa, pb) time_after32((pa)->id, (pb)->id)
399 static int tipc_publ_sort(void *priv, struct list_head *a,
400                           struct list_head *b)
401 {
402         struct publication *pa, *pb;
403
404         pa = container_of(a, struct publication, list);
405         pb = container_of(b, struct publication, list);
406         return publication_after(pa, pb);
407 }
408
409 /**
410  * tipc_service_subscribe - attach a subscription, and optionally
411  * issue the prescribed number of events if there is any service
412  * range overlapping with the requested range
413  * @service: the tipc_service to attach the @sub to
414  * @sub: the subscription to attach
415  */
416 static void tipc_service_subscribe(struct tipc_service *service,
417                                    struct tipc_subscription *sub)
418 {
419         struct tipc_subscr *sb = &sub->evt.s;
420         struct publication *p, *first, *tmp;
421         struct list_head publ_list;
422         struct service_range *sr;
423         struct tipc_service_range r;
424         u32 filter;
425
426         r.type = tipc_sub_read(sb, seq.type);
427         r.lower = tipc_sub_read(sb, seq.lower);
428         r.upper = tipc_sub_read(sb, seq.upper);
429         filter = tipc_sub_read(sb, filter);
430
431         tipc_sub_get(sub);
432         list_add(&sub->service_list, &service->subscriptions);
433
434         if (filter & TIPC_SUB_NO_STATUS)
435                 return;
436
437         INIT_LIST_HEAD(&publ_list);
438         service_range_foreach_match(sr, service, r.lower, r.upper) {
439                 first = NULL;
440                 list_for_each_entry(p, &sr->all_publ, all_publ) {
441                         if (filter & TIPC_SUB_PORTS)
442                                 list_add_tail(&p->list, &publ_list);
443                         else if (!first || publication_after(first, p))
444                                 /* Pick this range's *first* publication */
445                                 first = p;
446                 }
447                 if (first)
448                         list_add_tail(&first->list, &publ_list);
449         }
450
451         /* Sort the publications before reporting */
452         list_sort(NULL, &publ_list, tipc_publ_sort);
453         list_for_each_entry_safe(p, tmp, &publ_list, list) {
454                 tipc_sub_report_overlap(sub, p->sr.lower, p->sr.upper,
455                                         TIPC_PUBLISHED, p->sk.ref, p->sk.node,
456                                         p->scope, true);
457                 list_del_init(&p->list);
458         }
459 }
460
461 static struct tipc_service *tipc_service_find(struct net *net, u32 type)
462 {
463         struct name_table *nt = tipc_name_table(net);
464         struct hlist_head *service_head;
465         struct tipc_service *service;
466
467         service_head = &nt->services[hash(type)];
468         hlist_for_each_entry_rcu(service, service_head, service_list) {
469                 if (service->type == type)
470                         return service;
471         }
472         return NULL;
473 };
474
475 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
476                                              u32 lower, u32 upper,
477                                              u32 scope, u32 node,
478                                              u32 port, u32 key)
479 {
480         struct name_table *nt = tipc_name_table(net);
481         struct tipc_service *sc;
482         struct publication *p;
483
484         p = tipc_publ_create(type, lower, upper, scope, node, port, key);
485         if (!p)
486                 return NULL;
487
488         if (scope > TIPC_NODE_SCOPE || lower > upper) {
489                 pr_debug("Failed to bind illegal {%u,%u,%u} with scope %u\n",
490                          type, lower, upper, scope);
491                 return NULL;
492         }
493         sc = tipc_service_find(net, type);
494         if (!sc)
495                 sc = tipc_service_create(type, &nt->services[hash(type)]);
496         if (sc && tipc_service_insert_publ(net, sc, p))
497                 return p;
498         kfree(p);
499         return NULL;
500 }
501
502 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
503                                              u32 lower, u32 upper,
504                                              u32 node, u32 key)
505 {
506         struct tipc_service *sc = tipc_service_find(net, type);
507         struct tipc_subscription *sub, *tmp;
508         struct service_range *sr = NULL;
509         struct publication *p = NULL;
510         bool last;
511
512         if (!sc)
513                 return NULL;
514
515         spin_lock_bh(&sc->lock);
516         sr = tipc_service_find_range(sc, lower, upper);
517         if (!sr)
518                 goto exit;
519         p = tipc_service_remove_publ(sr, node, key);
520         if (!p)
521                 goto exit;
522
523         /* Notify any waiting subscriptions */
524         last = list_empty(&sr->all_publ);
525         list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
526                 tipc_sub_report_overlap(sub, lower, upper, TIPC_WITHDRAWN,
527                                         p->sk.ref, node, p->scope, last);
528         }
529
530         /* Remove service range item if this was its last publication */
531         if (list_empty(&sr->all_publ)) {
532                 rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
533                 kfree(sr);
534         }
535
536         /* Delete service item if this no more publications and subscriptions */
537         if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
538                 hlist_del_init_rcu(&sc->service_list);
539                 kfree_rcu(sc, rcu);
540         }
541 exit:
542         spin_unlock_bh(&sc->lock);
543         return p;
544 }
545
546 /**
547  * tipc_nametbl_translate - perform service instance to socket translation
548  * @net: network namespace
549  * @type: message type
550  * @instance: message instance
551  * @dnode: the search domain used during translation
552  *
553  * On exit:
554  * - if translation is deferred to another node, leave 'dnode' unchanged and
555  * return 0
556  * - if translation is attempted and succeeds, set 'dnode' to the publishing
557  * node and return the published (non-zero) port number
558  * - if translation is attempted and fails, set 'dnode' to 0 and return 0
559  *
560  * Note that for legacy users (node configured with Z.C.N address format) the
561  * 'closest-first' lookup algorithm must be maintained, i.e., if dnode is 0
562  * we must look in the local binding list first
563  */
564 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *dnode)
565 {
566         struct tipc_net *tn = tipc_net(net);
567         bool legacy = tn->legacy_addr_format;
568         u32 self = tipc_own_addr(net);
569         struct service_range *sr;
570         struct tipc_service *sc;
571         struct list_head *list;
572         struct publication *p;
573         u32 port = 0;
574         u32 node = 0;
575
576         if (!tipc_in_scope(legacy, *dnode, self))
577                 return 0;
578
579         rcu_read_lock();
580         sc = tipc_service_find(net, type);
581         if (unlikely(!sc))
582                 goto exit;
583
584         spin_lock_bh(&sc->lock);
585         service_range_foreach_match(sr, sc, instance, instance) {
586                 /* Select lookup algo: local, closest-first or round-robin */
587                 if (*dnode == self) {
588                         list = &sr->local_publ;
589                         if (list_empty(list))
590                                 continue;
591                         p = list_first_entry(list, struct publication,
592                                              local_publ);
593                         list_move_tail(&p->local_publ, &sr->local_publ);
594                 } else if (legacy && !*dnode && !list_empty(&sr->local_publ)) {
595                         list = &sr->local_publ;
596                         p = list_first_entry(list, struct publication,
597                                              local_publ);
598                         list_move_tail(&p->local_publ, &sr->local_publ);
599                 } else {
600                         list = &sr->all_publ;
601                         p = list_first_entry(list, struct publication,
602                                              all_publ);
603                         list_move_tail(&p->all_publ, &sr->all_publ);
604                 }
605                 port = p->sk.ref;
606                 node = p->sk.node;
607                 /* Todo: as for legacy, pick the first matching range only, a
608                  * "true" round-robin will be performed as needed.
609                  */
610                 break;
611         }
612         spin_unlock_bh(&sc->lock);
613
614 exit:
615         rcu_read_unlock();
616         *dnode = node;
617         return port;
618 }
619
620 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
621                          struct list_head *dsts, int *dstcnt, u32 exclude,
622                          bool all)
623 {
624         u32 self = tipc_own_addr(net);
625         struct service_range *sr;
626         struct tipc_service *sc;
627         struct publication *p;
628
629         *dstcnt = 0;
630         rcu_read_lock();
631         sc = tipc_service_find(net, type);
632         if (unlikely(!sc))
633                 goto exit;
634
635         spin_lock_bh(&sc->lock);
636
637         /* Todo: a full search i.e. service_range_foreach_match() instead? */
638         sr = service_range_match_first(sc->ranges.rb_node, instance, instance);
639         if (!sr)
640                 goto no_match;
641
642         list_for_each_entry(p, &sr->all_publ, all_publ) {
643                 if (p->scope != scope)
644                         continue;
645                 if (p->sk.ref == exclude && p->sk.node == self)
646                         continue;
647                 tipc_dest_push(dsts, p->sk.node, p->sk.ref);
648                 (*dstcnt)++;
649                 if (all)
650                         continue;
651                 list_move_tail(&p->all_publ, &sr->all_publ);
652                 break;
653         }
654 no_match:
655         spin_unlock_bh(&sc->lock);
656 exit:
657         rcu_read_unlock();
658         return !list_empty(dsts);
659 }
660
661 void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
662                             u32 scope, bool exact, struct list_head *dports)
663 {
664         struct service_range *sr;
665         struct tipc_service *sc;
666         struct publication *p;
667
668         rcu_read_lock();
669         sc = tipc_service_find(net, type);
670         if (!sc)
671                 goto exit;
672
673         spin_lock_bh(&sc->lock);
674         service_range_foreach_match(sr, sc, lower, upper) {
675                 list_for_each_entry(p, &sr->local_publ, local_publ) {
676                         if (p->scope == scope || (!exact && p->scope < scope))
677                                 tipc_dest_push(dports, 0, p->sk.ref);
678                 }
679         }
680         spin_unlock_bh(&sc->lock);
681 exit:
682         rcu_read_unlock();
683 }
684
685 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
686  * - Creates list of nodes that overlap the given multicast address
687  * - Determines if any node local destinations overlap
688  */
689 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
690                                    u32 upper, struct tipc_nlist *nodes)
691 {
692         struct service_range *sr;
693         struct tipc_service *sc;
694         struct publication *p;
695
696         rcu_read_lock();
697         sc = tipc_service_find(net, type);
698         if (!sc)
699                 goto exit;
700
701         spin_lock_bh(&sc->lock);
702         service_range_foreach_match(sr, sc, lower, upper) {
703                 list_for_each_entry(p, &sr->all_publ, all_publ) {
704                         tipc_nlist_add(nodes, p->sk.node);
705                 }
706         }
707         spin_unlock_bh(&sc->lock);
708 exit:
709         rcu_read_unlock();
710 }
711
712 /* tipc_nametbl_build_group - build list of communication group members
713  */
714 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
715                               u32 type, u32 scope)
716 {
717         struct service_range *sr;
718         struct tipc_service *sc;
719         struct publication *p;
720         struct rb_node *n;
721
722         rcu_read_lock();
723         sc = tipc_service_find(net, type);
724         if (!sc)
725                 goto exit;
726
727         spin_lock_bh(&sc->lock);
728         for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
729                 sr = container_of(n, struct service_range, tree_node);
730                 list_for_each_entry(p, &sr->all_publ, all_publ) {
731                         if (p->scope != scope)
732                                 continue;
733                         tipc_group_add_member(grp, p->sk.node, p->sk.ref, p->sr.lower);
734                 }
735         }
736         spin_unlock_bh(&sc->lock);
737 exit:
738         rcu_read_unlock();
739 }
740
741 /* tipc_nametbl_publish - add service binding to name table
742  */
743 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
744                                          u32 upper, u32 scope, u32 port,
745                                          u32 key)
746 {
747         struct name_table *nt = tipc_name_table(net);
748         struct tipc_net *tn = tipc_net(net);
749         struct publication *p = NULL;
750         struct sk_buff *skb = NULL;
751         u32 rc_dests;
752
753         spin_lock_bh(&tn->nametbl_lock);
754
755         if (nt->local_publ_count >= TIPC_MAX_PUBL) {
756                 pr_warn("Bind failed, max limit %u reached\n", TIPC_MAX_PUBL);
757                 goto exit;
758         }
759
760         p = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
761                                      tipc_own_addr(net), port, key);
762         if (p) {
763                 nt->local_publ_count++;
764                 skb = tipc_named_publish(net, p);
765         }
766         rc_dests = nt->rc_dests;
767 exit:
768         spin_unlock_bh(&tn->nametbl_lock);
769
770         if (skb)
771                 tipc_node_broadcast(net, skb, rc_dests);
772         return p;
773
774 }
775
776 /**
777  * tipc_nametbl_withdraw - withdraw a service binding
778  * @net: network namespace
779  * @type: service type
780  * @lower: service range lower bound
781  * @upper: service range upper bound
782  * @key: target publication key
783  */
784 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower,
785                           u32 upper, u32 key)
786 {
787         struct name_table *nt = tipc_name_table(net);
788         struct tipc_net *tn = tipc_net(net);
789         u32 self = tipc_own_addr(net);
790         struct sk_buff *skb = NULL;
791         struct publication *p;
792         u32 rc_dests;
793
794         spin_lock_bh(&tn->nametbl_lock);
795
796         p = tipc_nametbl_remove_publ(net, type, lower, upper, self, key);
797         if (p) {
798                 nt->local_publ_count--;
799                 skb = tipc_named_withdraw(net, p);
800                 list_del_init(&p->binding_sock);
801                 kfree_rcu(p, rcu);
802         } else {
803                 pr_err("Failed to remove local publication {%u,%u,%u}/%u\n",
804                        type, lower, upper, key);
805         }
806         rc_dests = nt->rc_dests;
807         spin_unlock_bh(&tn->nametbl_lock);
808
809         if (skb) {
810                 tipc_node_broadcast(net, skb, rc_dests);
811                 return 1;
812         }
813         return 0;
814 }
815
816 /**
817  * tipc_nametbl_subscribe - add a subscription object to the name table
818  * @sub: subscription to add
819  */
820 bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
821 {
822         struct name_table *nt = tipc_name_table(sub->net);
823         struct tipc_net *tn = tipc_net(sub->net);
824         struct tipc_subscr *s = &sub->evt.s;
825         u32 type = tipc_sub_read(s, seq.type);
826         struct tipc_service *sc;
827         bool res = true;
828
829         spin_lock_bh(&tn->nametbl_lock);
830         sc = tipc_service_find(sub->net, type);
831         if (!sc)
832                 sc = tipc_service_create(type, &nt->services[hash(type)]);
833         if (sc) {
834                 spin_lock_bh(&sc->lock);
835                 tipc_service_subscribe(sc, sub);
836                 spin_unlock_bh(&sc->lock);
837         } else {
838                 pr_warn("Failed to subscribe for {%u,%u,%u}\n", type,
839                         tipc_sub_read(s, seq.lower),
840                         tipc_sub_read(s, seq.upper));
841                 res = false;
842         }
843         spin_unlock_bh(&tn->nametbl_lock);
844         return res;
845 }
846
847 /**
848  * tipc_nametbl_unsubscribe - remove a subscription object from name table
849  * @sub: subscription to remove
850  */
851 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
852 {
853         struct tipc_net *tn = tipc_net(sub->net);
854         struct tipc_subscr *s = &sub->evt.s;
855         u32 type = tipc_sub_read(s, seq.type);
856         struct tipc_service *sc;
857
858         spin_lock_bh(&tn->nametbl_lock);
859         sc = tipc_service_find(sub->net, type);
860         if (!sc)
861                 goto exit;
862
863         spin_lock_bh(&sc->lock);
864         list_del_init(&sub->service_list);
865         tipc_sub_put(sub);
866
867         /* Delete service item if no more publications and subscriptions */
868         if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
869                 hlist_del_init_rcu(&sc->service_list);
870                 kfree_rcu(sc, rcu);
871         }
872         spin_unlock_bh(&sc->lock);
873 exit:
874         spin_unlock_bh(&tn->nametbl_lock);
875 }
876
877 int tipc_nametbl_init(struct net *net)
878 {
879         struct tipc_net *tn = tipc_net(net);
880         struct name_table *nt;
881         int i;
882
883         nt = kzalloc(sizeof(*nt), GFP_KERNEL);
884         if (!nt)
885                 return -ENOMEM;
886
887         for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
888                 INIT_HLIST_HEAD(&nt->services[i]);
889
890         INIT_LIST_HEAD(&nt->node_scope);
891         INIT_LIST_HEAD(&nt->cluster_scope);
892         rwlock_init(&nt->cluster_scope_lock);
893         tn->nametbl = nt;
894         spin_lock_init(&tn->nametbl_lock);
895         return 0;
896 }
897
898 /**
899  * tipc_service_delete - purge all publications for a service and delete it
900  * @net: the associated network namespace
901  * @sc: tipc_service to delete
902  */
903 static void tipc_service_delete(struct net *net, struct tipc_service *sc)
904 {
905         struct service_range *sr, *tmpr;
906         struct publication *p, *tmp;
907
908         spin_lock_bh(&sc->lock);
909         rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
910                 list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
911                         tipc_service_remove_publ(sr, p->sk.node, p->key);
912                         kfree_rcu(p, rcu);
913                 }
914                 rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
915                 kfree(sr);
916         }
917         hlist_del_init_rcu(&sc->service_list);
918         spin_unlock_bh(&sc->lock);
919         kfree_rcu(sc, rcu);
920 }
921
922 void tipc_nametbl_stop(struct net *net)
923 {
924         struct name_table *nt = tipc_name_table(net);
925         struct tipc_net *tn = tipc_net(net);
926         struct hlist_head *service_head;
927         struct tipc_service *service;
928         u32 i;
929
930         /* Verify name table is empty and purge any lingering
931          * publications, then release the name table
932          */
933         spin_lock_bh(&tn->nametbl_lock);
934         for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
935                 if (hlist_empty(&nt->services[i]))
936                         continue;
937                 service_head = &nt->services[i];
938                 hlist_for_each_entry_rcu(service, service_head, service_list) {
939                         tipc_service_delete(net, service);
940                 }
941         }
942         spin_unlock_bh(&tn->nametbl_lock);
943
944         synchronize_net();
945         kfree(nt);
946 }
947
948 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
949                                         struct tipc_service *service,
950                                         struct service_range *sr,
951                                         u32 *last_key)
952 {
953         struct publication *p;
954         struct nlattr *attrs;
955         struct nlattr *b;
956         void *hdr;
957
958         if (*last_key) {
959                 list_for_each_entry(p, &sr->all_publ, all_publ)
960                         if (p->key == *last_key)
961                                 break;
962                 if (p->key != *last_key)
963                         return -EPIPE;
964         } else {
965                 p = list_first_entry(&sr->all_publ,
966                                      struct publication,
967                                      all_publ);
968         }
969
970         list_for_each_entry_from(p, &sr->all_publ, all_publ) {
971                 *last_key = p->key;
972
973                 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
974                                   &tipc_genl_family, NLM_F_MULTI,
975                                   TIPC_NL_NAME_TABLE_GET);
976                 if (!hdr)
977                         return -EMSGSIZE;
978
979                 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE);
980                 if (!attrs)
981                         goto msg_full;
982
983                 b = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
984                 if (!b)
985                         goto attr_msg_full;
986
987                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type))
988                         goto publ_msg_full;
989                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower))
990                         goto publ_msg_full;
991                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper))
992                         goto publ_msg_full;
993                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
994                         goto publ_msg_full;
995                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->sk.node))
996                         goto publ_msg_full;
997                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->sk.ref))
998                         goto publ_msg_full;
999                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
1000                         goto publ_msg_full;
1001
1002                 nla_nest_end(msg->skb, b);
1003                 nla_nest_end(msg->skb, attrs);
1004                 genlmsg_end(msg->skb, hdr);
1005         }
1006         *last_key = 0;
1007
1008         return 0;
1009
1010 publ_msg_full:
1011         nla_nest_cancel(msg->skb, b);
1012 attr_msg_full:
1013         nla_nest_cancel(msg->skb, attrs);
1014 msg_full:
1015         genlmsg_cancel(msg->skb, hdr);
1016
1017         return -EMSGSIZE;
1018 }
1019
1020 static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg,
1021                                         struct tipc_service *sc,
1022                                         u32 *last_lower, u32 *last_key)
1023 {
1024         struct service_range *sr;
1025         struct rb_node *n;
1026         int err;
1027
1028         for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
1029                 sr = container_of(n, struct service_range, tree_node);
1030                 if (sr->lower < *last_lower)
1031                         continue;
1032                 err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key);
1033                 if (err) {
1034                         *last_lower = sr->lower;
1035                         return err;
1036                 }
1037         }
1038         *last_lower = 0;
1039         return 0;
1040 }
1041
1042 static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
1043                                 u32 *last_type, u32 *last_lower, u32 *last_key)
1044 {
1045         struct tipc_net *tn = tipc_net(net);
1046         struct tipc_service *service = NULL;
1047         struct hlist_head *head;
1048         int err;
1049         int i;
1050
1051         if (*last_type)
1052                 i = hash(*last_type);
1053         else
1054                 i = 0;
1055
1056         for (; i < TIPC_NAMETBL_SIZE; i++) {
1057                 head = &tn->nametbl->services[i];
1058
1059                 if (*last_type ||
1060                     (!i && *last_key && (*last_lower == *last_key))) {
1061                         service = tipc_service_find(net, *last_type);
1062                         if (!service)
1063                                 return -EPIPE;
1064                 } else {
1065                         hlist_for_each_entry_rcu(service, head, service_list)
1066                                 break;
1067                         if (!service)
1068                                 continue;
1069                 }
1070
1071                 hlist_for_each_entry_from_rcu(service, service_list) {
1072                         spin_lock_bh(&service->lock);
1073                         err = __tipc_nl_service_range_list(msg, service,
1074                                                            last_lower,
1075                                                            last_key);
1076
1077                         if (err) {
1078                                 *last_type = service->type;
1079                                 spin_unlock_bh(&service->lock);
1080                                 return err;
1081                         }
1082                         spin_unlock_bh(&service->lock);
1083                 }
1084                 *last_type = 0;
1085         }
1086         return 0;
1087 }
1088
1089 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1090 {
1091         struct net *net = sock_net(skb->sk);
1092         u32 last_type = cb->args[0];
1093         u32 last_lower = cb->args[1];
1094         u32 last_key = cb->args[2];
1095         int done = cb->args[3];
1096         struct tipc_nl_msg msg;
1097         int err;
1098
1099         if (done)
1100                 return 0;
1101
1102         msg.skb = skb;
1103         msg.portid = NETLINK_CB(cb->skb).portid;
1104         msg.seq = cb->nlh->nlmsg_seq;
1105
1106         rcu_read_lock();
1107         err = tipc_nl_service_list(net, &msg, &last_type,
1108                                    &last_lower, &last_key);
1109         if (!err) {
1110                 done = 1;
1111         } else if (err != -EMSGSIZE) {
1112                 /* We never set seq or call nl_dump_check_consistent() this
1113                  * means that setting prev_seq here will cause the consistence
1114                  * check to fail in the netlink callback handler. Resulting in
1115                  * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1116                  * we got an error.
1117                  */
1118                 cb->prev_seq = 1;
1119         }
1120         rcu_read_unlock();
1121
1122         cb->args[0] = last_type;
1123         cb->args[1] = last_lower;
1124         cb->args[2] = last_key;
1125         cb->args[3] = done;
1126
1127         return skb->len;
1128 }
1129
1130 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1131 {
1132         struct tipc_dest *dst;
1133
1134         list_for_each_entry(dst, l, list) {
1135                 if (dst->node == node && dst->port == port)
1136                         return dst;
1137         }
1138         return NULL;
1139 }
1140
1141 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1142 {
1143         struct tipc_dest *dst;
1144
1145         if (tipc_dest_find(l, node, port))
1146                 return false;
1147
1148         dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1149         if (unlikely(!dst))
1150                 return false;
1151         dst->node = node;
1152         dst->port = port;
1153         list_add(&dst->list, l);
1154         return true;
1155 }
1156
1157 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1158 {
1159         struct tipc_dest *dst;
1160
1161         if (list_empty(l))
1162                 return false;
1163         dst = list_first_entry(l, typeof(*dst), list);
1164         if (port)
1165                 *port = dst->port;
1166         if (node)
1167                 *node = dst->node;
1168         list_del(&dst->list);
1169         kfree(dst);
1170         return true;
1171 }
1172
1173 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1174 {
1175         struct tipc_dest *dst;
1176
1177         dst = tipc_dest_find(l, node, port);
1178         if (!dst)
1179                 return false;
1180         list_del(&dst->list);
1181         kfree(dst);
1182         return true;
1183 }
1184
1185 void tipc_dest_list_purge(struct list_head *l)
1186 {
1187         struct tipc_dest *dst, *tmp;
1188
1189         list_for_each_entry_safe(dst, tmp, l, list) {
1190                 list_del(&dst->list);
1191                 kfree(dst);
1192         }
1193 }
1194
1195 int tipc_dest_list_len(struct list_head *l)
1196 {
1197         struct tipc_dest *dst;
1198         int i = 0;
1199
1200         list_for_each_entry(dst, l, list) {
1201                 i++;
1202         }
1203         return i;
1204 }