]> asedeno.scripts.mit.edu Git - linux.git/blob - net/tipc/name_table.c
Merge tag 'sound-5.5-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
[linux.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  * 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 <linux/list_sort.h>
39 #include "core.h"
40 #include "netlink.h"
41 #include "name_table.h"
42 #include "name_distr.h"
43 #include "subscr.h"
44 #include "bcast.h"
45 #include "addr.h"
46 #include "node.h"
47 #include "group.h"
48
49 /**
50  * struct service_range - container for all bindings of a service range
51  * @lower: service range lower bound
52  * @upper: service range upper bound
53  * @tree_node: member of service range RB tree
54  * @local_publ: list of identical publications made from this node
55  *   Used by closest_first lookup and multicast lookup algorithm
56  * @all_publ: all publications identical to this one, whatever node and scope
57  *   Used by round-robin lookup algorithm
58  */
59 struct service_range {
60         u32 lower;
61         u32 upper;
62         struct rb_node tree_node;
63         struct list_head local_publ;
64         struct list_head all_publ;
65 };
66
67 /**
68  * struct tipc_service - container for all published instances of a service type
69  * @type: 32 bit 'type' value for service
70  * @publ_cnt: increasing counter for publications in this service
71  * @ranges: rb tree containing all service ranges for this service
72  * @service_list: links to adjacent name ranges in hash chain
73  * @subscriptions: list of subscriptions for this service type
74  * @lock: spinlock controlling access to pertaining service ranges/publications
75  * @rcu: RCU callback head used for deferred freeing
76  */
77 struct tipc_service {
78         u32 type;
79         u32 publ_cnt;
80         struct rb_root ranges;
81         struct hlist_node service_list;
82         struct list_head subscriptions;
83         spinlock_t lock; /* Covers service range list */
84         struct rcu_head rcu;
85 };
86
87 static int hash(int x)
88 {
89         return x & (TIPC_NAMETBL_SIZE - 1);
90 }
91
92 /**
93  * tipc_publ_create - create a publication structure
94  */
95 static struct publication *tipc_publ_create(u32 type, u32 lower, u32 upper,
96                                             u32 scope, u32 node, u32 port,
97                                             u32 key)
98 {
99         struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
100
101         if (!publ)
102                 return NULL;
103
104         publ->type = type;
105         publ->lower = lower;
106         publ->upper = upper;
107         publ->scope = scope;
108         publ->node = node;
109         publ->port = port;
110         publ->key = key;
111         INIT_LIST_HEAD(&publ->binding_sock);
112         INIT_LIST_HEAD(&publ->binding_node);
113         INIT_LIST_HEAD(&publ->local_publ);
114         INIT_LIST_HEAD(&publ->all_publ);
115         INIT_LIST_HEAD(&publ->list);
116         return publ;
117 }
118
119 /**
120  * tipc_service_create - create a service structure for the specified 'type'
121  *
122  * Allocates a single range structure and sets it to all 0's.
123  */
124 static struct tipc_service *tipc_service_create(u32 type, struct hlist_head *hd)
125 {
126         struct tipc_service *service = kzalloc(sizeof(*service), GFP_ATOMIC);
127
128         if (!service) {
129                 pr_warn("Service creation failed, no memory\n");
130                 return NULL;
131         }
132
133         spin_lock_init(&service->lock);
134         service->type = type;
135         service->ranges = RB_ROOT;
136         INIT_HLIST_NODE(&service->service_list);
137         INIT_LIST_HEAD(&service->subscriptions);
138         hlist_add_head_rcu(&service->service_list, hd);
139         return service;
140 }
141
142 /**
143  * tipc_service_first_range - find first service range in tree matching instance
144  *
145  * Very time-critical, so binary search through range rb tree
146  */
147 static struct service_range *tipc_service_first_range(struct tipc_service *sc,
148                                                       u32 instance)
149 {
150         struct rb_node *n = sc->ranges.rb_node;
151         struct service_range *sr;
152
153         while (n) {
154                 sr = container_of(n, struct service_range, tree_node);
155                 if (sr->lower > instance)
156                         n = n->rb_left;
157                 else if (sr->upper < instance)
158                         n = n->rb_right;
159                 else
160                         return sr;
161         }
162         return NULL;
163 }
164
165 /*  tipc_service_find_range - find service range matching publication parameters
166  */
167 static struct service_range *tipc_service_find_range(struct tipc_service *sc,
168                                                      u32 lower, u32 upper)
169 {
170         struct rb_node *n = sc->ranges.rb_node;
171         struct service_range *sr;
172
173         sr = tipc_service_first_range(sc, lower);
174         if (!sr)
175                 return NULL;
176
177         /* Look for exact match */
178         for (n = &sr->tree_node; n; n = rb_next(n)) {
179                 sr = container_of(n, struct service_range, tree_node);
180                 if (sr->upper == upper)
181                         break;
182         }
183         if (!n || sr->lower != lower || sr->upper != upper)
184                 return NULL;
185
186         return sr;
187 }
188
189 static struct service_range *tipc_service_create_range(struct tipc_service *sc,
190                                                        u32 lower, u32 upper)
191 {
192         struct rb_node **n, *parent = NULL;
193         struct service_range *sr, *tmp;
194
195         n = &sc->ranges.rb_node;
196         while (*n) {
197                 tmp = container_of(*n, struct service_range, tree_node);
198                 parent = *n;
199                 tmp = container_of(parent, struct service_range, tree_node);
200                 if (lower < tmp->lower)
201                         n = &(*n)->rb_left;
202                 else if (lower > tmp->lower)
203                         n = &(*n)->rb_right;
204                 else if (upper < tmp->upper)
205                         n = &(*n)->rb_left;
206                 else if (upper > tmp->upper)
207                         n = &(*n)->rb_right;
208                 else
209                         return tmp;
210         }
211         sr = kzalloc(sizeof(*sr), GFP_ATOMIC);
212         if (!sr)
213                 return NULL;
214         sr->lower = lower;
215         sr->upper = upper;
216         INIT_LIST_HEAD(&sr->local_publ);
217         INIT_LIST_HEAD(&sr->all_publ);
218         rb_link_node(&sr->tree_node, parent, n);
219         rb_insert_color(&sr->tree_node, &sc->ranges);
220         return sr;
221 }
222
223 static struct publication *tipc_service_insert_publ(struct net *net,
224                                                     struct tipc_service *sc,
225                                                     u32 type, u32 lower,
226                                                     u32 upper, u32 scope,
227                                                     u32 node, u32 port,
228                                                     u32 key)
229 {
230         struct tipc_subscription *sub, *tmp;
231         struct service_range *sr;
232         struct publication *p;
233         bool first = false;
234
235         sr = tipc_service_create_range(sc, lower, upper);
236         if (!sr)
237                 goto  err;
238
239         first = list_empty(&sr->all_publ);
240
241         /* Return if the publication already exists */
242         list_for_each_entry(p, &sr->all_publ, all_publ) {
243                 if (p->key == key && (!p->node || p->node == node))
244                         return NULL;
245         }
246
247         /* Create and insert publication */
248         p = tipc_publ_create(type, lower, upper, scope, node, port, key);
249         if (!p)
250                 goto err;
251         /* Suppose there shouldn't be a huge gap btw publs i.e. >INT_MAX */
252         p->id = sc->publ_cnt++;
253         if (in_own_node(net, node))
254                 list_add(&p->local_publ, &sr->local_publ);
255         list_add(&p->all_publ, &sr->all_publ);
256
257         /* Any subscriptions waiting for notification?  */
258         list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
259                 tipc_sub_report_overlap(sub, p->lower, p->upper, TIPC_PUBLISHED,
260                                         p->port, p->node, p->scope, first);
261         }
262         return p;
263 err:
264         pr_warn("Failed to bind to %u,%u,%u, no memory\n", type, lower, upper);
265         return NULL;
266 }
267
268 /**
269  * tipc_service_remove_publ - remove a publication from a service
270  */
271 static struct publication *tipc_service_remove_publ(struct service_range *sr,
272                                                     u32 node, u32 key)
273 {
274         struct publication *p;
275
276         list_for_each_entry(p, &sr->all_publ, all_publ) {
277                 if (p->key != key || (node && node != p->node))
278                         continue;
279                 list_del(&p->all_publ);
280                 list_del(&p->local_publ);
281                 return p;
282         }
283         return NULL;
284 }
285
286 /**
287  * Code reused: time_after32() for the same purpose
288  */
289 #define publication_after(pa, pb) time_after32((pa)->id, (pb)->id)
290 static int tipc_publ_sort(void *priv, struct list_head *a,
291                           struct list_head *b)
292 {
293         struct publication *pa, *pb;
294
295         pa = container_of(a, struct publication, list);
296         pb = container_of(b, struct publication, list);
297         return publication_after(pa, pb);
298 }
299
300 /**
301  * tipc_service_subscribe - attach a subscription, and optionally
302  * issue the prescribed number of events if there is any service
303  * range overlapping with the requested range
304  */
305 static void tipc_service_subscribe(struct tipc_service *service,
306                                    struct tipc_subscription *sub)
307 {
308         struct tipc_subscr *sb = &sub->evt.s;
309         struct publication *p, *first, *tmp;
310         struct list_head publ_list;
311         struct service_range *sr;
312         struct tipc_name_seq ns;
313         struct rb_node *n;
314         u32 filter;
315
316         ns.type = tipc_sub_read(sb, seq.type);
317         ns.lower = tipc_sub_read(sb, seq.lower);
318         ns.upper = tipc_sub_read(sb, seq.upper);
319         filter = tipc_sub_read(sb, filter);
320
321         tipc_sub_get(sub);
322         list_add(&sub->service_list, &service->subscriptions);
323
324         if (filter & TIPC_SUB_NO_STATUS)
325                 return;
326
327         INIT_LIST_HEAD(&publ_list);
328         for (n = rb_first(&service->ranges); n; n = rb_next(n)) {
329                 sr = container_of(n, struct service_range, tree_node);
330                 if (sr->lower > ns.upper)
331                         break;
332                 if (!tipc_sub_check_overlap(&ns, sr->lower, sr->upper))
333                         continue;
334
335                 first = NULL;
336                 list_for_each_entry(p, &sr->all_publ, all_publ) {
337                         if (filter & TIPC_SUB_PORTS)
338                                 list_add_tail(&p->list, &publ_list);
339                         else if (!first || publication_after(first, p))
340                                 /* Pick this range's *first* publication */
341                                 first = p;
342                 }
343                 if (first)
344                         list_add_tail(&first->list, &publ_list);
345         }
346
347         /* Sort the publications before reporting */
348         list_sort(NULL, &publ_list, tipc_publ_sort);
349         list_for_each_entry_safe(p, tmp, &publ_list, list) {
350                 tipc_sub_report_overlap(sub, p->lower, p->upper,
351                                         TIPC_PUBLISHED, p->port, p->node,
352                                         p->scope, true);
353                 list_del_init(&p->list);
354         }
355 }
356
357 static struct tipc_service *tipc_service_find(struct net *net, u32 type)
358 {
359         struct name_table *nt = tipc_name_table(net);
360         struct hlist_head *service_head;
361         struct tipc_service *service;
362
363         service_head = &nt->services[hash(type)];
364         hlist_for_each_entry_rcu(service, service_head, service_list) {
365                 if (service->type == type)
366                         return service;
367         }
368         return NULL;
369 };
370
371 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
372                                              u32 lower, u32 upper,
373                                              u32 scope, u32 node,
374                                              u32 port, u32 key)
375 {
376         struct name_table *nt = tipc_name_table(net);
377         struct tipc_service *sc;
378         struct publication *p;
379
380         if (scope > TIPC_NODE_SCOPE || lower > upper) {
381                 pr_debug("Failed to bind illegal {%u,%u,%u} with scope %u\n",
382                          type, lower, upper, scope);
383                 return NULL;
384         }
385         sc = tipc_service_find(net, type);
386         if (!sc)
387                 sc = tipc_service_create(type, &nt->services[hash(type)]);
388         if (!sc)
389                 return NULL;
390
391         spin_lock_bh(&sc->lock);
392         p = tipc_service_insert_publ(net, sc, type, lower, upper,
393                                      scope, node, port, key);
394         spin_unlock_bh(&sc->lock);
395         return p;
396 }
397
398 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
399                                              u32 lower, u32 upper,
400                                              u32 node, u32 key)
401 {
402         struct tipc_service *sc = tipc_service_find(net, type);
403         struct tipc_subscription *sub, *tmp;
404         struct service_range *sr = NULL;
405         struct publication *p = NULL;
406         bool last;
407
408         if (!sc)
409                 return NULL;
410
411         spin_lock_bh(&sc->lock);
412         sr = tipc_service_find_range(sc, lower, upper);
413         if (!sr)
414                 goto exit;
415         p = tipc_service_remove_publ(sr, node, key);
416         if (!p)
417                 goto exit;
418
419         /* Notify any waiting subscriptions */
420         last = list_empty(&sr->all_publ);
421         list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
422                 tipc_sub_report_overlap(sub, lower, upper, TIPC_WITHDRAWN,
423                                         p->port, node, p->scope, last);
424         }
425
426         /* Remove service range item if this was its last publication */
427         if (list_empty(&sr->all_publ)) {
428                 rb_erase(&sr->tree_node, &sc->ranges);
429                 kfree(sr);
430         }
431
432         /* Delete service item if this no more publications and subscriptions */
433         if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
434                 hlist_del_init_rcu(&sc->service_list);
435                 kfree_rcu(sc, rcu);
436         }
437 exit:
438         spin_unlock_bh(&sc->lock);
439         return p;
440 }
441
442 /**
443  * tipc_nametbl_translate - perform service instance to socket translation
444  *
445  * On entry, 'dnode' is the search domain used during translation.
446  *
447  * On exit:
448  * - if translation is deferred to another node, leave 'dnode' unchanged and
449  *   return 0
450  * - if translation is attempted and succeeds, set 'dnode' to the publishing
451  *   node and return the published (non-zero) port number
452  * - if translation is attempted and fails, set 'dnode' to 0 and return 0
453  *
454  * Note that for legacy users (node configured with Z.C.N address format) the
455  * 'closest-first' lookup algorithm must be maintained, i.e., if dnode is 0
456  * we must look in the local binding list first
457  */
458 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, u32 *dnode)
459 {
460         struct tipc_net *tn = tipc_net(net);
461         bool legacy = tn->legacy_addr_format;
462         u32 self = tipc_own_addr(net);
463         struct service_range *sr;
464         struct tipc_service *sc;
465         struct list_head *list;
466         struct publication *p;
467         u32 port = 0;
468         u32 node = 0;
469
470         if (!tipc_in_scope(legacy, *dnode, self))
471                 return 0;
472
473         rcu_read_lock();
474         sc = tipc_service_find(net, type);
475         if (unlikely(!sc))
476                 goto not_found;
477
478         spin_lock_bh(&sc->lock);
479         sr = tipc_service_first_range(sc, instance);
480         if (unlikely(!sr))
481                 goto no_match;
482
483         /* Select lookup algorithm: local, closest-first or round-robin */
484         if (*dnode == self) {
485                 list = &sr->local_publ;
486                 if (list_empty(list))
487                         goto no_match;
488                 p = list_first_entry(list, struct publication, local_publ);
489                 list_move_tail(&p->local_publ, &sr->local_publ);
490         } else if (legacy && !*dnode && !list_empty(&sr->local_publ)) {
491                 list = &sr->local_publ;
492                 p = list_first_entry(list, struct publication, local_publ);
493                 list_move_tail(&p->local_publ, &sr->local_publ);
494         } else {
495                 list = &sr->all_publ;
496                 p = list_first_entry(list, struct publication, all_publ);
497                 list_move_tail(&p->all_publ, &sr->all_publ);
498         }
499         port = p->port;
500         node = p->node;
501 no_match:
502         spin_unlock_bh(&sc->lock);
503 not_found:
504         rcu_read_unlock();
505         *dnode = node;
506         return port;
507 }
508
509 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
510                          struct list_head *dsts, int *dstcnt, u32 exclude,
511                          bool all)
512 {
513         u32 self = tipc_own_addr(net);
514         struct service_range *sr;
515         struct tipc_service *sc;
516         struct publication *p;
517
518         *dstcnt = 0;
519         rcu_read_lock();
520         sc = tipc_service_find(net, type);
521         if (unlikely(!sc))
522                 goto exit;
523
524         spin_lock_bh(&sc->lock);
525
526         sr = tipc_service_first_range(sc, instance);
527         if (!sr)
528                 goto no_match;
529
530         list_for_each_entry(p, &sr->all_publ, all_publ) {
531                 if (p->scope != scope)
532                         continue;
533                 if (p->port == exclude && p->node == self)
534                         continue;
535                 tipc_dest_push(dsts, p->node, p->port);
536                 (*dstcnt)++;
537                 if (all)
538                         continue;
539                 list_move_tail(&p->all_publ, &sr->all_publ);
540                 break;
541         }
542 no_match:
543         spin_unlock_bh(&sc->lock);
544 exit:
545         rcu_read_unlock();
546         return !list_empty(dsts);
547 }
548
549 void tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
550                             u32 scope, bool exact, struct list_head *dports)
551 {
552         struct service_range *sr;
553         struct tipc_service *sc;
554         struct publication *p;
555         struct rb_node *n;
556
557         rcu_read_lock();
558         sc = tipc_service_find(net, type);
559         if (!sc)
560                 goto exit;
561
562         spin_lock_bh(&sc->lock);
563
564         for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
565                 sr = container_of(n, struct service_range, tree_node);
566                 if (sr->upper < lower)
567                         continue;
568                 if (sr->lower > upper)
569                         break;
570                 list_for_each_entry(p, &sr->local_publ, local_publ) {
571                         if (p->scope == scope || (!exact && p->scope < scope))
572                                 tipc_dest_push(dports, 0, p->port);
573                 }
574         }
575         spin_unlock_bh(&sc->lock);
576 exit:
577         rcu_read_unlock();
578 }
579
580 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
581  * - Creates list of nodes that overlap the given multicast address
582  * - Determines if any node local destinations overlap
583  */
584 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
585                                    u32 upper, struct tipc_nlist *nodes)
586 {
587         struct service_range *sr;
588         struct tipc_service *sc;
589         struct publication *p;
590         struct rb_node *n;
591
592         rcu_read_lock();
593         sc = tipc_service_find(net, type);
594         if (!sc)
595                 goto exit;
596
597         spin_lock_bh(&sc->lock);
598
599         for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
600                 sr = container_of(n, struct service_range, tree_node);
601                 if (sr->upper < lower)
602                         continue;
603                 if (sr->lower > upper)
604                         break;
605                 list_for_each_entry(p, &sr->all_publ, all_publ) {
606                         tipc_nlist_add(nodes, p->node);
607                 }
608         }
609         spin_unlock_bh(&sc->lock);
610 exit:
611         rcu_read_unlock();
612 }
613
614 /* tipc_nametbl_build_group - build list of communication group members
615  */
616 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
617                               u32 type, u32 scope)
618 {
619         struct service_range *sr;
620         struct tipc_service *sc;
621         struct publication *p;
622         struct rb_node *n;
623
624         rcu_read_lock();
625         sc = tipc_service_find(net, type);
626         if (!sc)
627                 goto exit;
628
629         spin_lock_bh(&sc->lock);
630         for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
631                 sr = container_of(n, struct service_range, tree_node);
632                 list_for_each_entry(p, &sr->all_publ, all_publ) {
633                         if (p->scope != scope)
634                                 continue;
635                         tipc_group_add_member(grp, p->node, p->port, p->lower);
636                 }
637         }
638         spin_unlock_bh(&sc->lock);
639 exit:
640         rcu_read_unlock();
641 }
642
643 /* tipc_nametbl_publish - add service binding to name table
644  */
645 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
646                                          u32 upper, u32 scope, u32 port,
647                                          u32 key)
648 {
649         struct name_table *nt = tipc_name_table(net);
650         struct tipc_net *tn = tipc_net(net);
651         struct publication *p = NULL;
652         struct sk_buff *skb = NULL;
653
654         spin_lock_bh(&tn->nametbl_lock);
655
656         if (nt->local_publ_count >= TIPC_MAX_PUBL) {
657                 pr_warn("Bind failed, max limit %u reached\n", TIPC_MAX_PUBL);
658                 goto exit;
659         }
660
661         p = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
662                                      tipc_own_addr(net), port, key);
663         if (p) {
664                 nt->local_publ_count++;
665                 skb = tipc_named_publish(net, p);
666         }
667 exit:
668         spin_unlock_bh(&tn->nametbl_lock);
669
670         if (skb)
671                 tipc_node_broadcast(net, skb);
672         return p;
673 }
674
675 /**
676  * tipc_nametbl_withdraw - withdraw a service binding
677  */
678 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower,
679                           u32 upper, u32 key)
680 {
681         struct name_table *nt = tipc_name_table(net);
682         struct tipc_net *tn = tipc_net(net);
683         u32 self = tipc_own_addr(net);
684         struct sk_buff *skb = NULL;
685         struct publication *p;
686
687         spin_lock_bh(&tn->nametbl_lock);
688
689         p = tipc_nametbl_remove_publ(net, type, lower, upper, self, key);
690         if (p) {
691                 nt->local_publ_count--;
692                 skb = tipc_named_withdraw(net, p);
693                 list_del_init(&p->binding_sock);
694                 kfree_rcu(p, rcu);
695         } else {
696                 pr_err("Failed to remove local publication {%u,%u,%u}/%u\n",
697                        type, lower, upper, key);
698         }
699         spin_unlock_bh(&tn->nametbl_lock);
700
701         if (skb) {
702                 tipc_node_broadcast(net, skb);
703                 return 1;
704         }
705         return 0;
706 }
707
708 /**
709  * tipc_nametbl_subscribe - add a subscription object to the name table
710  */
711 bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
712 {
713         struct name_table *nt = tipc_name_table(sub->net);
714         struct tipc_net *tn = tipc_net(sub->net);
715         struct tipc_subscr *s = &sub->evt.s;
716         u32 type = tipc_sub_read(s, seq.type);
717         struct tipc_service *sc;
718         bool res = true;
719
720         spin_lock_bh(&tn->nametbl_lock);
721         sc = tipc_service_find(sub->net, type);
722         if (!sc)
723                 sc = tipc_service_create(type, &nt->services[hash(type)]);
724         if (sc) {
725                 spin_lock_bh(&sc->lock);
726                 tipc_service_subscribe(sc, sub);
727                 spin_unlock_bh(&sc->lock);
728         } else {
729                 pr_warn("Failed to subscribe for {%u,%u,%u}\n", type,
730                         tipc_sub_read(s, seq.lower),
731                         tipc_sub_read(s, seq.upper));
732                 res = false;
733         }
734         spin_unlock_bh(&tn->nametbl_lock);
735         return res;
736 }
737
738 /**
739  * tipc_nametbl_unsubscribe - remove a subscription object from name table
740  */
741 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
742 {
743         struct tipc_net *tn = tipc_net(sub->net);
744         struct tipc_subscr *s = &sub->evt.s;
745         u32 type = tipc_sub_read(s, seq.type);
746         struct tipc_service *sc;
747
748         spin_lock_bh(&tn->nametbl_lock);
749         sc = tipc_service_find(sub->net, type);
750         if (!sc)
751                 goto exit;
752
753         spin_lock_bh(&sc->lock);
754         list_del_init(&sub->service_list);
755         tipc_sub_put(sub);
756
757         /* Delete service item if no more publications and subscriptions */
758         if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
759                 hlist_del_init_rcu(&sc->service_list);
760                 kfree_rcu(sc, rcu);
761         }
762         spin_unlock_bh(&sc->lock);
763 exit:
764         spin_unlock_bh(&tn->nametbl_lock);
765 }
766
767 int tipc_nametbl_init(struct net *net)
768 {
769         struct tipc_net *tn = tipc_net(net);
770         struct name_table *nt;
771         int i;
772
773         nt = kzalloc(sizeof(*nt), GFP_KERNEL);
774         if (!nt)
775                 return -ENOMEM;
776
777         for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
778                 INIT_HLIST_HEAD(&nt->services[i]);
779
780         INIT_LIST_HEAD(&nt->node_scope);
781         INIT_LIST_HEAD(&nt->cluster_scope);
782         rwlock_init(&nt->cluster_scope_lock);
783         tn->nametbl = nt;
784         spin_lock_init(&tn->nametbl_lock);
785         return 0;
786 }
787
788 /**
789  *  tipc_service_delete - purge all publications for a service and delete it
790  */
791 static void tipc_service_delete(struct net *net, struct tipc_service *sc)
792 {
793         struct service_range *sr, *tmpr;
794         struct publication *p, *tmp;
795
796         spin_lock_bh(&sc->lock);
797         rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
798                 list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
799                         tipc_service_remove_publ(sr, p->node, p->key);
800                         kfree_rcu(p, rcu);
801                 }
802                 rb_erase(&sr->tree_node, &sc->ranges);
803                 kfree(sr);
804         }
805         hlist_del_init_rcu(&sc->service_list);
806         spin_unlock_bh(&sc->lock);
807         kfree_rcu(sc, rcu);
808 }
809
810 void tipc_nametbl_stop(struct net *net)
811 {
812         struct name_table *nt = tipc_name_table(net);
813         struct tipc_net *tn = tipc_net(net);
814         struct hlist_head *service_head;
815         struct tipc_service *service;
816         u32 i;
817
818         /* Verify name table is empty and purge any lingering
819          * publications, then release the name table
820          */
821         spin_lock_bh(&tn->nametbl_lock);
822         for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
823                 if (hlist_empty(&nt->services[i]))
824                         continue;
825                 service_head = &nt->services[i];
826                 hlist_for_each_entry_rcu(service, service_head, service_list) {
827                         tipc_service_delete(net, service);
828                 }
829         }
830         spin_unlock_bh(&tn->nametbl_lock);
831
832         synchronize_net();
833         kfree(nt);
834 }
835
836 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
837                                         struct tipc_service *service,
838                                         struct service_range *sr,
839                                         u32 *last_key)
840 {
841         struct publication *p;
842         struct nlattr *attrs;
843         struct nlattr *b;
844         void *hdr;
845
846         if (*last_key) {
847                 list_for_each_entry(p, &sr->all_publ, all_publ)
848                         if (p->key == *last_key)
849                                 break;
850                 if (p->key != *last_key)
851                         return -EPIPE;
852         } else {
853                 p = list_first_entry(&sr->all_publ,
854                                      struct publication,
855                                      all_publ);
856         }
857
858         list_for_each_entry_from(p, &sr->all_publ, all_publ) {
859                 *last_key = p->key;
860
861                 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
862                                   &tipc_genl_family, NLM_F_MULTI,
863                                   TIPC_NL_NAME_TABLE_GET);
864                 if (!hdr)
865                         return -EMSGSIZE;
866
867                 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE);
868                 if (!attrs)
869                         goto msg_full;
870
871                 b = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
872                 if (!b)
873                         goto attr_msg_full;
874
875                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type))
876                         goto publ_msg_full;
877                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower))
878                         goto publ_msg_full;
879                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper))
880                         goto publ_msg_full;
881                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
882                         goto publ_msg_full;
883                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
884                         goto publ_msg_full;
885                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port))
886                         goto publ_msg_full;
887                 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
888                         goto publ_msg_full;
889
890                 nla_nest_end(msg->skb, b);
891                 nla_nest_end(msg->skb, attrs);
892                 genlmsg_end(msg->skb, hdr);
893         }
894         *last_key = 0;
895
896         return 0;
897
898 publ_msg_full:
899         nla_nest_cancel(msg->skb, b);
900 attr_msg_full:
901         nla_nest_cancel(msg->skb, attrs);
902 msg_full:
903         genlmsg_cancel(msg->skb, hdr);
904
905         return -EMSGSIZE;
906 }
907
908 static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg,
909                                         struct tipc_service *sc,
910                                         u32 *last_lower, u32 *last_key)
911 {
912         struct service_range *sr;
913         struct rb_node *n;
914         int err;
915
916         for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
917                 sr = container_of(n, struct service_range, tree_node);
918                 if (sr->lower < *last_lower)
919                         continue;
920                 err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key);
921                 if (err) {
922                         *last_lower = sr->lower;
923                         return err;
924                 }
925         }
926         *last_lower = 0;
927         return 0;
928 }
929
930 static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
931                                 u32 *last_type, u32 *last_lower, u32 *last_key)
932 {
933         struct tipc_net *tn = tipc_net(net);
934         struct tipc_service *service = NULL;
935         struct hlist_head *head;
936         int err;
937         int i;
938
939         if (*last_type)
940                 i = hash(*last_type);
941         else
942                 i = 0;
943
944         for (; i < TIPC_NAMETBL_SIZE; i++) {
945                 head = &tn->nametbl->services[i];
946
947                 if (*last_type ||
948                     (!i && *last_key && (*last_lower == *last_key))) {
949                         service = tipc_service_find(net, *last_type);
950                         if (!service)
951                                 return -EPIPE;
952                 } else {
953                         hlist_for_each_entry_rcu(service, head, service_list)
954                                 break;
955                         if (!service)
956                                 continue;
957                 }
958
959                 hlist_for_each_entry_from_rcu(service, service_list) {
960                         spin_lock_bh(&service->lock);
961                         err = __tipc_nl_service_range_list(msg, service,
962                                                            last_lower,
963                                                            last_key);
964
965                         if (err) {
966                                 *last_type = service->type;
967                                 spin_unlock_bh(&service->lock);
968                                 return err;
969                         }
970                         spin_unlock_bh(&service->lock);
971                 }
972                 *last_type = 0;
973         }
974         return 0;
975 }
976
977 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
978 {
979         struct net *net = sock_net(skb->sk);
980         u32 last_type = cb->args[0];
981         u32 last_lower = cb->args[1];
982         u32 last_key = cb->args[2];
983         int done = cb->args[3];
984         struct tipc_nl_msg msg;
985         int err;
986
987         if (done)
988                 return 0;
989
990         msg.skb = skb;
991         msg.portid = NETLINK_CB(cb->skb).portid;
992         msg.seq = cb->nlh->nlmsg_seq;
993
994         rcu_read_lock();
995         err = tipc_nl_service_list(net, &msg, &last_type,
996                                    &last_lower, &last_key);
997         if (!err) {
998                 done = 1;
999         } else if (err != -EMSGSIZE) {
1000                 /* We never set seq or call nl_dump_check_consistent() this
1001                  * means that setting prev_seq here will cause the consistence
1002                  * check to fail in the netlink callback handler. Resulting in
1003                  * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1004                  * we got an error.
1005                  */
1006                 cb->prev_seq = 1;
1007         }
1008         rcu_read_unlock();
1009
1010         cb->args[0] = last_type;
1011         cb->args[1] = last_lower;
1012         cb->args[2] = last_key;
1013         cb->args[3] = done;
1014
1015         return skb->len;
1016 }
1017
1018 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1019 {
1020         struct tipc_dest *dst;
1021
1022         list_for_each_entry(dst, l, list) {
1023                 if (dst->node == node && dst->port == port)
1024                         return dst;
1025         }
1026         return NULL;
1027 }
1028
1029 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1030 {
1031         struct tipc_dest *dst;
1032
1033         if (tipc_dest_find(l, node, port))
1034                 return false;
1035
1036         dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1037         if (unlikely(!dst))
1038                 return false;
1039         dst->node = node;
1040         dst->port = port;
1041         list_add(&dst->list, l);
1042         return true;
1043 }
1044
1045 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1046 {
1047         struct tipc_dest *dst;
1048
1049         if (list_empty(l))
1050                 return false;
1051         dst = list_first_entry(l, typeof(*dst), list);
1052         if (port)
1053                 *port = dst->port;
1054         if (node)
1055                 *node = dst->node;
1056         list_del(&dst->list);
1057         kfree(dst);
1058         return true;
1059 }
1060
1061 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1062 {
1063         struct tipc_dest *dst;
1064
1065         dst = tipc_dest_find(l, node, port);
1066         if (!dst)
1067                 return false;
1068         list_del(&dst->list);
1069         kfree(dst);
1070         return true;
1071 }
1072
1073 void tipc_dest_list_purge(struct list_head *l)
1074 {
1075         struct tipc_dest *dst, *tmp;
1076
1077         list_for_each_entry_safe(dst, tmp, l, list) {
1078                 list_del(&dst->list);
1079                 kfree(dst);
1080         }
1081 }
1082
1083 int tipc_dest_list_len(struct list_head *l)
1084 {
1085         struct tipc_dest *dst;
1086         int i = 0;
1087
1088         list_for_each_entry(dst, l, list) {
1089                 i++;
1090         }
1091         return i;
1092 }