tipc: make tipc broadcast link support net namespace
[deliverable/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, 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 "core.h"
38 #include "config.h"
39 #include "name_table.h"
40 #include "name_distr.h"
41 #include "subscr.h"
42 #include "bcast.h"
43
44 #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
45
46 static const struct nla_policy
47 tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = {
48 [TIPC_NLA_NAME_TABLE_UNSPEC] = { .type = NLA_UNSPEC },
49 [TIPC_NLA_NAME_TABLE_PUBL] = { .type = NLA_NESTED }
50 };
51
52 /**
53 * struct name_info - name sequence publication info
54 * @node_list: circular list of publications made by own node
55 * @cluster_list: circular list of publications made by own cluster
56 * @zone_list: circular list of publications made by own zone
57 * @node_list_size: number of entries in "node_list"
58 * @cluster_list_size: number of entries in "cluster_list"
59 * @zone_list_size: number of entries in "zone_list"
60 *
61 * Note: The zone list always contains at least one entry, since all
62 * publications of the associated name sequence belong to it.
63 * (The cluster and node lists may be empty.)
64 */
65 struct name_info {
66 struct list_head node_list;
67 struct list_head cluster_list;
68 struct list_head zone_list;
69 u32 node_list_size;
70 u32 cluster_list_size;
71 u32 zone_list_size;
72 };
73
74 /**
75 * struct sub_seq - container for all published instances of a name sequence
76 * @lower: name sequence lower bound
77 * @upper: name sequence upper bound
78 * @info: pointer to name sequence publication info
79 */
80 struct sub_seq {
81 u32 lower;
82 u32 upper;
83 struct name_info *info;
84 };
85
86 /**
87 * struct name_seq - container for all published instances of a name type
88 * @type: 32 bit 'type' value for name sequence
89 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
90 * sub-sequences are sorted in ascending order
91 * @alloc: number of sub-sequences currently in array
92 * @first_free: array index of first unused sub-sequence entry
93 * @ns_list: links to adjacent name sequences in hash chain
94 * @subscriptions: list of subscriptions for this 'type'
95 * @lock: spinlock controlling access to publication lists of all sub-sequences
96 * @rcu: RCU callback head used for deferred freeing
97 */
98 struct name_seq {
99 u32 type;
100 struct sub_seq *sseqs;
101 u32 alloc;
102 u32 first_free;
103 struct hlist_node ns_list;
104 struct list_head subscriptions;
105 spinlock_t lock;
106 struct rcu_head rcu;
107 };
108
109 struct name_table *tipc_nametbl;
110 DEFINE_SPINLOCK(tipc_nametbl_lock);
111
112 static int hash(int x)
113 {
114 return x & (TIPC_NAMETBL_SIZE - 1);
115 }
116
117 /**
118 * publ_create - create a publication structure
119 */
120 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
121 u32 scope, u32 node, u32 port_ref,
122 u32 key)
123 {
124 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
125 if (publ == NULL) {
126 pr_warn("Publication creation failure, no memory\n");
127 return NULL;
128 }
129
130 publ->type = type;
131 publ->lower = lower;
132 publ->upper = upper;
133 publ->scope = scope;
134 publ->node = node;
135 publ->ref = port_ref;
136 publ->key = key;
137 INIT_LIST_HEAD(&publ->pport_list);
138 return publ;
139 }
140
141 /**
142 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
143 */
144 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
145 {
146 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
147 }
148
149 /**
150 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
151 *
152 * Allocates a single sub-sequence structure and sets it to all 0's.
153 */
154 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
155 {
156 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
157 struct sub_seq *sseq = tipc_subseq_alloc(1);
158
159 if (!nseq || !sseq) {
160 pr_warn("Name sequence creation failed, no memory\n");
161 kfree(nseq);
162 kfree(sseq);
163 return NULL;
164 }
165
166 spin_lock_init(&nseq->lock);
167 nseq->type = type;
168 nseq->sseqs = sseq;
169 nseq->alloc = 1;
170 INIT_HLIST_NODE(&nseq->ns_list);
171 INIT_LIST_HEAD(&nseq->subscriptions);
172 hlist_add_head_rcu(&nseq->ns_list, seq_head);
173 return nseq;
174 }
175
176 /**
177 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
178 *
179 * Very time-critical, so binary searches through sub-sequence array.
180 */
181 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
182 u32 instance)
183 {
184 struct sub_seq *sseqs = nseq->sseqs;
185 int low = 0;
186 int high = nseq->first_free - 1;
187 int mid;
188
189 while (low <= high) {
190 mid = (low + high) / 2;
191 if (instance < sseqs[mid].lower)
192 high = mid - 1;
193 else if (instance > sseqs[mid].upper)
194 low = mid + 1;
195 else
196 return &sseqs[mid];
197 }
198 return NULL;
199 }
200
201 /**
202 * nameseq_locate_subseq - determine position of name instance in sub-sequence
203 *
204 * Returns index in sub-sequence array of the entry that contains the specified
205 * instance value; if no entry contains that value, returns the position
206 * where a new entry for it would be inserted in the array.
207 *
208 * Note: Similar to binary search code for locating a sub-sequence.
209 */
210 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
211 {
212 struct sub_seq *sseqs = nseq->sseqs;
213 int low = 0;
214 int high = nseq->first_free - 1;
215 int mid;
216
217 while (low <= high) {
218 mid = (low + high) / 2;
219 if (instance < sseqs[mid].lower)
220 high = mid - 1;
221 else if (instance > sseqs[mid].upper)
222 low = mid + 1;
223 else
224 return mid;
225 }
226 return low;
227 }
228
229 /**
230 * tipc_nameseq_insert_publ
231 */
232 static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
233 u32 type, u32 lower, u32 upper,
234 u32 scope, u32 node, u32 port, u32 key)
235 {
236 struct tipc_subscription *s;
237 struct tipc_subscription *st;
238 struct publication *publ;
239 struct sub_seq *sseq;
240 struct name_info *info;
241 int created_subseq = 0;
242
243 sseq = nameseq_find_subseq(nseq, lower);
244 if (sseq) {
245
246 /* Lower end overlaps existing entry => need an exact match */
247 if ((sseq->lower != lower) || (sseq->upper != upper)) {
248 return NULL;
249 }
250
251 info = sseq->info;
252
253 /* Check if an identical publication already exists */
254 list_for_each_entry(publ, &info->zone_list, zone_list) {
255 if ((publ->ref == port) && (publ->key == key) &&
256 (!publ->node || (publ->node == node)))
257 return NULL;
258 }
259 } else {
260 u32 inspos;
261 struct sub_seq *freesseq;
262
263 /* Find where lower end should be inserted */
264 inspos = nameseq_locate_subseq(nseq, lower);
265
266 /* Fail if upper end overlaps into an existing entry */
267 if ((inspos < nseq->first_free) &&
268 (upper >= nseq->sseqs[inspos].lower)) {
269 return NULL;
270 }
271
272 /* Ensure there is space for new sub-sequence */
273 if (nseq->first_free == nseq->alloc) {
274 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
275
276 if (!sseqs) {
277 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
278 type, lower, upper);
279 return NULL;
280 }
281 memcpy(sseqs, nseq->sseqs,
282 nseq->alloc * sizeof(struct sub_seq));
283 kfree(nseq->sseqs);
284 nseq->sseqs = sseqs;
285 nseq->alloc *= 2;
286 }
287
288 info = kzalloc(sizeof(*info), GFP_ATOMIC);
289 if (!info) {
290 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
291 type, lower, upper);
292 return NULL;
293 }
294
295 INIT_LIST_HEAD(&info->node_list);
296 INIT_LIST_HEAD(&info->cluster_list);
297 INIT_LIST_HEAD(&info->zone_list);
298
299 /* Insert new sub-sequence */
300 sseq = &nseq->sseqs[inspos];
301 freesseq = &nseq->sseqs[nseq->first_free];
302 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
303 memset(sseq, 0, sizeof(*sseq));
304 nseq->first_free++;
305 sseq->lower = lower;
306 sseq->upper = upper;
307 sseq->info = info;
308 created_subseq = 1;
309 }
310
311 /* Insert a publication */
312 publ = publ_create(type, lower, upper, scope, node, port, key);
313 if (!publ)
314 return NULL;
315
316 list_add(&publ->zone_list, &info->zone_list);
317 info->zone_list_size++;
318
319 if (in_own_cluster(node)) {
320 list_add(&publ->cluster_list, &info->cluster_list);
321 info->cluster_list_size++;
322 }
323
324 if (in_own_node(node)) {
325 list_add(&publ->node_list, &info->node_list);
326 info->node_list_size++;
327 }
328
329 /* Any subscriptions waiting for notification? */
330 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
331 tipc_subscr_report_overlap(s,
332 publ->lower,
333 publ->upper,
334 TIPC_PUBLISHED,
335 publ->ref,
336 publ->node,
337 created_subseq);
338 }
339 return publ;
340 }
341
342 /**
343 * tipc_nameseq_remove_publ
344 *
345 * NOTE: There may be cases where TIPC is asked to remove a publication
346 * that is not in the name table. For example, if another node issues a
347 * publication for a name sequence that overlaps an existing name sequence
348 * the publication will not be recorded, which means the publication won't
349 * be found when the name sequence is later withdrawn by that node.
350 * A failed withdraw request simply returns a failure indication and lets the
351 * caller issue any error or warning messages associated with such a problem.
352 */
353 static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
354 u32 node, u32 ref, u32 key)
355 {
356 struct publication *publ;
357 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
358 struct name_info *info;
359 struct sub_seq *free;
360 struct tipc_subscription *s, *st;
361 int removed_subseq = 0;
362
363 if (!sseq)
364 return NULL;
365
366 info = sseq->info;
367
368 /* Locate publication, if it exists */
369 list_for_each_entry(publ, &info->zone_list, zone_list) {
370 if ((publ->key == key) && (publ->ref == ref) &&
371 (!publ->node || (publ->node == node)))
372 goto found;
373 }
374 return NULL;
375
376 found:
377 /* Remove publication from zone scope list */
378 list_del(&publ->zone_list);
379 info->zone_list_size--;
380
381 /* Remove publication from cluster scope list, if present */
382 if (in_own_cluster(node)) {
383 list_del(&publ->cluster_list);
384 info->cluster_list_size--;
385 }
386
387 /* Remove publication from node scope list, if present */
388 if (in_own_node(node)) {
389 list_del(&publ->node_list);
390 info->node_list_size--;
391 }
392
393 /* Contract subseq list if no more publications for that subseq */
394 if (list_empty(&info->zone_list)) {
395 kfree(info);
396 free = &nseq->sseqs[nseq->first_free--];
397 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
398 removed_subseq = 1;
399 }
400
401 /* Notify any waiting subscriptions */
402 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
403 tipc_subscr_report_overlap(s,
404 publ->lower,
405 publ->upper,
406 TIPC_WITHDRAWN,
407 publ->ref,
408 publ->node,
409 removed_subseq);
410 }
411
412 return publ;
413 }
414
415 /**
416 * tipc_nameseq_subscribe - attach a subscription, and issue
417 * the prescribed number of events if there is any sub-
418 * sequence overlapping with the requested sequence
419 */
420 static void tipc_nameseq_subscribe(struct name_seq *nseq,
421 struct tipc_subscription *s)
422 {
423 struct sub_seq *sseq = nseq->sseqs;
424
425 list_add(&s->nameseq_list, &nseq->subscriptions);
426
427 if (!sseq)
428 return;
429
430 while (sseq != &nseq->sseqs[nseq->first_free]) {
431 if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
432 struct publication *crs;
433 struct name_info *info = sseq->info;
434 int must_report = 1;
435
436 list_for_each_entry(crs, &info->zone_list, zone_list) {
437 tipc_subscr_report_overlap(s,
438 sseq->lower,
439 sseq->upper,
440 TIPC_PUBLISHED,
441 crs->ref,
442 crs->node,
443 must_report);
444 must_report = 0;
445 }
446 }
447 sseq++;
448 }
449 }
450
451 static struct name_seq *nametbl_find_seq(u32 type)
452 {
453 struct hlist_head *seq_head;
454 struct name_seq *ns;
455
456 seq_head = &tipc_nametbl->seq_hlist[hash(type)];
457 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
458 if (ns->type == type)
459 return ns;
460 }
461
462 return NULL;
463 };
464
465 struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
466 u32 scope, u32 node, u32 port, u32 key)
467 {
468 struct publication *publ;
469 struct name_seq *seq = nametbl_find_seq(type);
470 int index = hash(type);
471
472 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
473 (lower > upper)) {
474 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
475 type, lower, upper, scope);
476 return NULL;
477 }
478
479 if (!seq)
480 seq = tipc_nameseq_create(type,
481 &tipc_nametbl->seq_hlist[index]);
482 if (!seq)
483 return NULL;
484
485 spin_lock_bh(&seq->lock);
486 publ = tipc_nameseq_insert_publ(seq, type, lower, upper,
487 scope, node, port, key);
488 spin_unlock_bh(&seq->lock);
489 return publ;
490 }
491
492 struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
493 u32 node, u32 ref, u32 key)
494 {
495 struct publication *publ;
496 struct name_seq *seq = nametbl_find_seq(type);
497
498 if (!seq)
499 return NULL;
500
501 spin_lock_bh(&seq->lock);
502 publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
503 if (!seq->first_free && list_empty(&seq->subscriptions)) {
504 hlist_del_init_rcu(&seq->ns_list);
505 kfree(seq->sseqs);
506 spin_unlock_bh(&seq->lock);
507 kfree_rcu(seq, rcu);
508 return publ;
509 }
510 spin_unlock_bh(&seq->lock);
511 return publ;
512 }
513
514 /**
515 * tipc_nametbl_translate - perform name translation
516 *
517 * On entry, 'destnode' is the search domain used during translation.
518 *
519 * On exit:
520 * - if name translation is deferred to another node/cluster/zone,
521 * leaves 'destnode' unchanged (will be non-zero) and returns 0
522 * - if name translation is attempted and succeeds, sets 'destnode'
523 * to publishing node and returns port reference (will be non-zero)
524 * - if name translation is attempted and fails, sets 'destnode' to 0
525 * and returns 0
526 */
527 u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
528 {
529 struct sub_seq *sseq;
530 struct name_info *info;
531 struct publication *publ;
532 struct name_seq *seq;
533 u32 ref = 0;
534 u32 node = 0;
535
536 if (!tipc_in_scope(*destnode, tipc_own_addr))
537 return 0;
538
539 rcu_read_lock();
540 seq = nametbl_find_seq(type);
541 if (unlikely(!seq))
542 goto not_found;
543 spin_lock_bh(&seq->lock);
544 sseq = nameseq_find_subseq(seq, instance);
545 if (unlikely(!sseq))
546 goto no_match;
547 info = sseq->info;
548
549 /* Closest-First Algorithm */
550 if (likely(!*destnode)) {
551 if (!list_empty(&info->node_list)) {
552 publ = list_first_entry(&info->node_list,
553 struct publication,
554 node_list);
555 list_move_tail(&publ->node_list,
556 &info->node_list);
557 } else if (!list_empty(&info->cluster_list)) {
558 publ = list_first_entry(&info->cluster_list,
559 struct publication,
560 cluster_list);
561 list_move_tail(&publ->cluster_list,
562 &info->cluster_list);
563 } else {
564 publ = list_first_entry(&info->zone_list,
565 struct publication,
566 zone_list);
567 list_move_tail(&publ->zone_list,
568 &info->zone_list);
569 }
570 }
571
572 /* Round-Robin Algorithm */
573 else if (*destnode == tipc_own_addr) {
574 if (list_empty(&info->node_list))
575 goto no_match;
576 publ = list_first_entry(&info->node_list, struct publication,
577 node_list);
578 list_move_tail(&publ->node_list, &info->node_list);
579 } else if (in_own_cluster_exact(*destnode)) {
580 if (list_empty(&info->cluster_list))
581 goto no_match;
582 publ = list_first_entry(&info->cluster_list, struct publication,
583 cluster_list);
584 list_move_tail(&publ->cluster_list, &info->cluster_list);
585 } else {
586 publ = list_first_entry(&info->zone_list, struct publication,
587 zone_list);
588 list_move_tail(&publ->zone_list, &info->zone_list);
589 }
590
591 ref = publ->ref;
592 node = publ->node;
593 no_match:
594 spin_unlock_bh(&seq->lock);
595 not_found:
596 rcu_read_unlock();
597 *destnode = node;
598 return ref;
599 }
600
601 /**
602 * tipc_nametbl_mc_translate - find multicast destinations
603 *
604 * Creates list of all local ports that overlap the given multicast address;
605 * also determines if any off-node ports overlap.
606 *
607 * Note: Publications with a scope narrower than 'limit' are ignored.
608 * (i.e. local node-scope publications mustn't receive messages arriving
609 * from another node, even if the multcast link brought it here)
610 *
611 * Returns non-zero if any off-node ports overlap
612 */
613 int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
614 struct tipc_port_list *dports)
615 {
616 struct name_seq *seq;
617 struct sub_seq *sseq;
618 struct sub_seq *sseq_stop;
619 struct name_info *info;
620 int res = 0;
621
622 rcu_read_lock();
623 seq = nametbl_find_seq(type);
624 if (!seq)
625 goto exit;
626
627 spin_lock_bh(&seq->lock);
628 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
629 sseq_stop = seq->sseqs + seq->first_free;
630 for (; sseq != sseq_stop; sseq++) {
631 struct publication *publ;
632
633 if (sseq->lower > upper)
634 break;
635
636 info = sseq->info;
637 list_for_each_entry(publ, &info->node_list, node_list) {
638 if (publ->scope <= limit)
639 tipc_port_list_add(dports, publ->ref);
640 }
641
642 if (info->cluster_list_size != info->node_list_size)
643 res = 1;
644 }
645 spin_unlock_bh(&seq->lock);
646 exit:
647 rcu_read_unlock();
648 return res;
649 }
650
651 /*
652 * tipc_nametbl_publish - add name publication to network name tables
653 */
654 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
655 u32 upper, u32 scope, u32 port_ref,
656 u32 key)
657 {
658 struct publication *publ;
659 struct sk_buff *buf = NULL;
660
661 spin_lock_bh(&tipc_nametbl_lock);
662 if (tipc_nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
663 pr_warn("Publication failed, local publication limit reached (%u)\n",
664 TIPC_MAX_PUBLICATIONS);
665 spin_unlock_bh(&tipc_nametbl_lock);
666 return NULL;
667 }
668
669 publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
670 tipc_own_addr, port_ref, key);
671 if (likely(publ)) {
672 tipc_nametbl->local_publ_count++;
673 buf = tipc_named_publish(publ);
674 /* Any pending external events? */
675 tipc_named_process_backlog(net);
676 }
677 spin_unlock_bh(&tipc_nametbl_lock);
678
679 if (buf)
680 named_cluster_distribute(net, buf);
681 return publ;
682 }
683
684 /**
685 * tipc_nametbl_withdraw - withdraw name publication from network name tables
686 */
687 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
688 u32 key)
689 {
690 struct publication *publ;
691 struct sk_buff *skb = NULL;
692
693 spin_lock_bh(&tipc_nametbl_lock);
694 publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
695 if (likely(publ)) {
696 tipc_nametbl->local_publ_count--;
697 skb = tipc_named_withdraw(publ);
698 /* Any pending external events? */
699 tipc_named_process_backlog(net);
700 list_del_init(&publ->pport_list);
701 kfree_rcu(publ, rcu);
702 } else {
703 pr_err("Unable to remove local publication\n"
704 "(type=%u, lower=%u, ref=%u, key=%u)\n",
705 type, lower, ref, key);
706 }
707 spin_unlock_bh(&tipc_nametbl_lock);
708
709 if (skb) {
710 named_cluster_distribute(net, skb);
711 return 1;
712 }
713 return 0;
714 }
715
716 /**
717 * tipc_nametbl_subscribe - add a subscription object to the name table
718 */
719 void tipc_nametbl_subscribe(struct tipc_subscription *s)
720 {
721 u32 type = s->seq.type;
722 int index = hash(type);
723 struct name_seq *seq;
724
725 spin_lock_bh(&tipc_nametbl_lock);
726 seq = nametbl_find_seq(type);
727 if (!seq)
728 seq = tipc_nameseq_create(type,
729 &tipc_nametbl->seq_hlist[index]);
730 if (seq) {
731 spin_lock_bh(&seq->lock);
732 tipc_nameseq_subscribe(seq, s);
733 spin_unlock_bh(&seq->lock);
734 } else {
735 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
736 s->seq.type, s->seq.lower, s->seq.upper);
737 }
738 spin_unlock_bh(&tipc_nametbl_lock);
739 }
740
741 /**
742 * tipc_nametbl_unsubscribe - remove a subscription object from name table
743 */
744 void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
745 {
746 struct name_seq *seq;
747
748 spin_lock_bh(&tipc_nametbl_lock);
749 seq = nametbl_find_seq(s->seq.type);
750 if (seq != NULL) {
751 spin_lock_bh(&seq->lock);
752 list_del_init(&s->nameseq_list);
753 if (!seq->first_free && list_empty(&seq->subscriptions)) {
754 hlist_del_init_rcu(&seq->ns_list);
755 kfree(seq->sseqs);
756 spin_unlock_bh(&seq->lock);
757 kfree_rcu(seq, rcu);
758 } else {
759 spin_unlock_bh(&seq->lock);
760 }
761 }
762 spin_unlock_bh(&tipc_nametbl_lock);
763 }
764
765 /**
766 * subseq_list - print specified sub-sequence contents into the given buffer
767 */
768 static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth,
769 u32 index)
770 {
771 char portIdStr[27];
772 const char *scope_str[] = {"", " zone", " cluster", " node"};
773 struct publication *publ;
774 struct name_info *info;
775 int ret;
776
777 ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper);
778
779 if (depth == 2) {
780 ret += tipc_snprintf(buf - ret, len + ret, "\n");
781 return ret;
782 }
783
784 info = sseq->info;
785
786 list_for_each_entry(publ, &info->zone_list, zone_list) {
787 sprintf(portIdStr, "<%u.%u.%u:%u>",
788 tipc_zone(publ->node), tipc_cluster(publ->node),
789 tipc_node(publ->node), publ->ref);
790 ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr);
791 if (depth > 3) {
792 ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s",
793 publ->key, scope_str[publ->scope]);
794 }
795 if (!list_is_last(&publ->zone_list, &info->zone_list))
796 ret += tipc_snprintf(buf + ret, len - ret,
797 "\n%33s", " ");
798 }
799
800 ret += tipc_snprintf(buf + ret, len - ret, "\n");
801 return ret;
802 }
803
804 /**
805 * nameseq_list - print specified name sequence contents into the given buffer
806 */
807 static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth,
808 u32 type, u32 lowbound, u32 upbound, u32 index)
809 {
810 struct sub_seq *sseq;
811 char typearea[11];
812 int ret = 0;
813
814 if (seq->first_free == 0)
815 return 0;
816
817 sprintf(typearea, "%-10u", seq->type);
818
819 if (depth == 1) {
820 ret += tipc_snprintf(buf, len, "%s\n", typearea);
821 return ret;
822 }
823
824 for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
825 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
826 ret += tipc_snprintf(buf + ret, len - ret, "%s ",
827 typearea);
828 spin_lock_bh(&seq->lock);
829 ret += subseq_list(sseq, buf + ret, len - ret,
830 depth, index);
831 spin_unlock_bh(&seq->lock);
832 sprintf(typearea, "%10s", " ");
833 }
834 }
835 return ret;
836 }
837
838 /**
839 * nametbl_header - print name table header into the given buffer
840 */
841 static int nametbl_header(char *buf, int len, u32 depth)
842 {
843 const char *header[] = {
844 "Type ",
845 "Lower Upper ",
846 "Port Identity ",
847 "Publication Scope"
848 };
849
850 int i;
851 int ret = 0;
852
853 if (depth > 4)
854 depth = 4;
855 for (i = 0; i < depth; i++)
856 ret += tipc_snprintf(buf + ret, len - ret, header[i]);
857 ret += tipc_snprintf(buf + ret, len - ret, "\n");
858 return ret;
859 }
860
861 /**
862 * nametbl_list - print specified name table contents into the given buffer
863 */
864 static int nametbl_list(char *buf, int len, u32 depth_info,
865 u32 type, u32 lowbound, u32 upbound)
866 {
867 struct hlist_head *seq_head;
868 struct name_seq *seq;
869 int all_types;
870 int ret = 0;
871 u32 depth;
872 u32 i;
873
874 all_types = (depth_info & TIPC_NTQ_ALLTYPES);
875 depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
876
877 if (depth == 0)
878 return 0;
879
880 if (all_types) {
881 /* display all entries in name table to specified depth */
882 ret += nametbl_header(buf, len, depth);
883 lowbound = 0;
884 upbound = ~0;
885 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
886 seq_head = &tipc_nametbl->seq_hlist[i];
887 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
888 ret += nameseq_list(seq, buf + ret, len - ret,
889 depth, seq->type,
890 lowbound, upbound, i);
891 }
892 }
893 } else {
894 /* display only the sequence that matches the specified type */
895 if (upbound < lowbound) {
896 ret += tipc_snprintf(buf + ret, len - ret,
897 "invalid name sequence specified\n");
898 return ret;
899 }
900 ret += nametbl_header(buf + ret, len - ret, depth);
901 i = hash(type);
902 seq_head = &tipc_nametbl->seq_hlist[i];
903 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
904 if (seq->type == type) {
905 ret += nameseq_list(seq, buf + ret, len - ret,
906 depth, type,
907 lowbound, upbound, i);
908 break;
909 }
910 }
911 }
912 return ret;
913 }
914
915 struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
916 {
917 struct sk_buff *buf;
918 struct tipc_name_table_query *argv;
919 struct tlv_desc *rep_tlv;
920 char *pb;
921 int pb_len;
922 int str_len;
923
924 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
925 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
926
927 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
928 if (!buf)
929 return NULL;
930
931 rep_tlv = (struct tlv_desc *)buf->data;
932 pb = TLV_DATA(rep_tlv);
933 pb_len = ULTRA_STRING_MAX_LEN;
934 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
935 rcu_read_lock();
936 str_len = nametbl_list(pb, pb_len, ntohl(argv->depth),
937 ntohl(argv->type),
938 ntohl(argv->lowbound), ntohl(argv->upbound));
939 rcu_read_unlock();
940 str_len += 1; /* for "\0" */
941 skb_put(buf, TLV_SPACE(str_len));
942 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
943
944 return buf;
945 }
946
947 int tipc_nametbl_init(void)
948 {
949 int i;
950
951 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
952 if (!tipc_nametbl)
953 return -ENOMEM;
954
955 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
956 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
957
958 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
959 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
960 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
961 return 0;
962 }
963
964 /**
965 * tipc_purge_publications - remove all publications for a given type
966 *
967 * tipc_nametbl_lock must be held when calling this function
968 */
969 static void tipc_purge_publications(struct name_seq *seq)
970 {
971 struct publication *publ, *safe;
972 struct sub_seq *sseq;
973 struct name_info *info;
974
975 spin_lock_bh(&seq->lock);
976 sseq = seq->sseqs;
977 info = sseq->info;
978 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
979 tipc_nametbl_remove_publ(publ->type, publ->lower, publ->node,
980 publ->ref, publ->key);
981 kfree_rcu(publ, rcu);
982 }
983 hlist_del_init_rcu(&seq->ns_list);
984 kfree(seq->sseqs);
985 spin_unlock_bh(&seq->lock);
986
987 kfree_rcu(seq, rcu);
988 }
989
990 void tipc_nametbl_stop(void)
991 {
992 u32 i;
993 struct name_seq *seq;
994 struct hlist_head *seq_head;
995
996 /* Verify name table is empty and purge any lingering
997 * publications, then release the name table
998 */
999 spin_lock_bh(&tipc_nametbl_lock);
1000 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
1001 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
1002 continue;
1003 seq_head = &tipc_nametbl->seq_hlist[i];
1004 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
1005 tipc_purge_publications(seq);
1006 }
1007 }
1008 spin_unlock_bh(&tipc_nametbl_lock);
1009
1010 synchronize_net();
1011 kfree(tipc_nametbl);
1012
1013 }
1014
1015 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
1016 struct name_seq *seq,
1017 struct sub_seq *sseq, u32 *last_publ)
1018 {
1019 void *hdr;
1020 struct nlattr *attrs;
1021 struct nlattr *publ;
1022 struct publication *p;
1023
1024 if (*last_publ) {
1025 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
1026 if (p->key == *last_publ)
1027 break;
1028 if (p->key != *last_publ)
1029 return -EPIPE;
1030 } else {
1031 p = list_first_entry(&sseq->info->zone_list, struct publication,
1032 zone_list);
1033 }
1034
1035 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
1036 *last_publ = p->key;
1037
1038 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
1039 &tipc_genl_v2_family, NLM_F_MULTI,
1040 TIPC_NL_NAME_TABLE_GET);
1041 if (!hdr)
1042 return -EMSGSIZE;
1043
1044 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
1045 if (!attrs)
1046 goto msg_full;
1047
1048 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
1049 if (!publ)
1050 goto attr_msg_full;
1051
1052 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
1053 goto publ_msg_full;
1054 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
1055 goto publ_msg_full;
1056 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
1057 goto publ_msg_full;
1058 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
1059 goto publ_msg_full;
1060 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
1061 goto publ_msg_full;
1062 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
1063 goto publ_msg_full;
1064 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
1065 goto publ_msg_full;
1066
1067 nla_nest_end(msg->skb, publ);
1068 nla_nest_end(msg->skb, attrs);
1069 genlmsg_end(msg->skb, hdr);
1070 }
1071 *last_publ = 0;
1072
1073 return 0;
1074
1075 publ_msg_full:
1076 nla_nest_cancel(msg->skb, publ);
1077 attr_msg_full:
1078 nla_nest_cancel(msg->skb, attrs);
1079 msg_full:
1080 genlmsg_cancel(msg->skb, hdr);
1081
1082 return -EMSGSIZE;
1083 }
1084
1085 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1086 u32 *last_lower, u32 *last_publ)
1087 {
1088 struct sub_seq *sseq;
1089 struct sub_seq *sseq_start;
1090 int err;
1091
1092 if (*last_lower) {
1093 sseq_start = nameseq_find_subseq(seq, *last_lower);
1094 if (!sseq_start)
1095 return -EPIPE;
1096 } else {
1097 sseq_start = seq->sseqs;
1098 }
1099
1100 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
1101 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
1102 if (err) {
1103 *last_lower = sseq->lower;
1104 return err;
1105 }
1106 }
1107 *last_lower = 0;
1108
1109 return 0;
1110 }
1111
1112 static int __tipc_nl_seq_list(struct tipc_nl_msg *msg, u32 *last_type,
1113 u32 *last_lower, u32 *last_publ)
1114 {
1115 struct hlist_head *seq_head;
1116 struct name_seq *seq = NULL;
1117 int err;
1118 int i;
1119
1120 if (*last_type)
1121 i = hash(*last_type);
1122 else
1123 i = 0;
1124
1125 for (; i < TIPC_NAMETBL_SIZE; i++) {
1126 seq_head = &tipc_nametbl->seq_hlist[i];
1127
1128 if (*last_type) {
1129 seq = nametbl_find_seq(*last_type);
1130 if (!seq)
1131 return -EPIPE;
1132 } else {
1133 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1134 break;
1135 if (!seq)
1136 continue;
1137 }
1138
1139 hlist_for_each_entry_from_rcu(seq, ns_list) {
1140 spin_lock_bh(&seq->lock);
1141 err = __tipc_nl_subseq_list(msg, seq, last_lower,
1142 last_publ);
1143
1144 if (err) {
1145 *last_type = seq->type;
1146 spin_unlock_bh(&seq->lock);
1147 return err;
1148 }
1149 spin_unlock_bh(&seq->lock);
1150 }
1151 *last_type = 0;
1152 }
1153 return 0;
1154 }
1155
1156 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1157 {
1158 int err;
1159 int done = cb->args[3];
1160 u32 last_type = cb->args[0];
1161 u32 last_lower = cb->args[1];
1162 u32 last_publ = cb->args[2];
1163 struct tipc_nl_msg msg;
1164
1165 if (done)
1166 return 0;
1167
1168 msg.skb = skb;
1169 msg.portid = NETLINK_CB(cb->skb).portid;
1170 msg.seq = cb->nlh->nlmsg_seq;
1171
1172 rcu_read_lock();
1173 err = __tipc_nl_seq_list(&msg, &last_type, &last_lower, &last_publ);
1174 if (!err) {
1175 done = 1;
1176 } else if (err != -EMSGSIZE) {
1177 /* We never set seq or call nl_dump_check_consistent() this
1178 * means that setting prev_seq here will cause the consistence
1179 * check to fail in the netlink callback handler. Resulting in
1180 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1181 * we got an error.
1182 */
1183 cb->prev_seq = 1;
1184 }
1185 rcu_read_unlock();
1186
1187 cb->args[0] = last_type;
1188 cb->args[1] = last_lower;
1189 cb->args[2] = last_publ;
1190 cb->args[3] = done;
1191
1192 return skb->len;
1193 }
This page took 0.081819 seconds and 6 git commands to generate.