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