Merge tag 'wireless-drivers-next-for-davem-2016-02-12' of git://git.kernel.org/pub...
[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-2015, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <net/sock.h>
38 #include "core.h"
39 #include "netlink.h"
40 #include "name_table.h"
41 #include "name_distr.h"
42 #include "subscr.h"
43 #include "bcast.h"
44 #include "addr.h"
45 #include "node.h"
46 #include <net/genetlink.h>
47
48 #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
49
50 static const struct nla_policy
51 tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = {
52 [TIPC_NLA_NAME_TABLE_UNSPEC] = { .type = NLA_UNSPEC },
53 [TIPC_NLA_NAME_TABLE_PUBL] = { .type = NLA_NESTED }
54 };
55
56 /**
57 * struct name_info - name sequence publication info
58 * @node_list: circular list of publications made by own node
59 * @cluster_list: circular list of publications made by own cluster
60 * @zone_list: circular list of publications made by own zone
61 * @node_list_size: number of entries in "node_list"
62 * @cluster_list_size: number of entries in "cluster_list"
63 * @zone_list_size: number of entries in "zone_list"
64 *
65 * Note: The zone list always contains at least one entry, since all
66 * publications of the associated name sequence belong to it.
67 * (The cluster and node lists may be empty.)
68 */
69 struct name_info {
70 struct list_head node_list;
71 struct list_head cluster_list;
72 struct list_head zone_list;
73 u32 node_list_size;
74 u32 cluster_list_size;
75 u32 zone_list_size;
76 };
77
78 /**
79 * struct sub_seq - container for all published instances of a name sequence
80 * @lower: name sequence lower bound
81 * @upper: name sequence upper bound
82 * @info: pointer to name sequence publication info
83 */
84 struct sub_seq {
85 u32 lower;
86 u32 upper;
87 struct name_info *info;
88 };
89
90 /**
91 * struct name_seq - container for all published instances of a name type
92 * @type: 32 bit 'type' value for name sequence
93 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
94 * sub-sequences are sorted in ascending order
95 * @alloc: number of sub-sequences currently in array
96 * @first_free: array index of first unused sub-sequence entry
97 * @ns_list: links to adjacent name sequences in hash chain
98 * @subscriptions: list of subscriptions for this 'type'
99 * @lock: spinlock controlling access to publication lists of all sub-sequences
100 * @rcu: RCU callback head used for deferred freeing
101 */
102 struct name_seq {
103 u32 type;
104 struct sub_seq *sseqs;
105 u32 alloc;
106 u32 first_free;
107 struct hlist_node ns_list;
108 struct list_head subscriptions;
109 spinlock_t lock;
110 struct rcu_head rcu;
111 };
112
113 static int hash(int x)
114 {
115 return x & (TIPC_NAMETBL_SIZE - 1);
116 }
117
118 /**
119 * publ_create - create a publication structure
120 */
121 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
122 u32 scope, u32 node, u32 port_ref,
123 u32 key)
124 {
125 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
126 if (publ == NULL) {
127 pr_warn("Publication creation failure, no memory\n");
128 return NULL;
129 }
130
131 publ->type = type;
132 publ->lower = lower;
133 publ->upper = upper;
134 publ->scope = scope;
135 publ->node = node;
136 publ->ref = port_ref;
137 publ->key = key;
138 INIT_LIST_HEAD(&publ->pport_list);
139 return publ;
140 }
141
142 /**
143 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
144 */
145 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
146 {
147 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
148 }
149
150 /**
151 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
152 *
153 * Allocates a single sub-sequence structure and sets it to all 0's.
154 */
155 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
156 {
157 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
158 struct sub_seq *sseq = tipc_subseq_alloc(1);
159
160 if (!nseq || !sseq) {
161 pr_warn("Name sequence creation failed, no memory\n");
162 kfree(nseq);
163 kfree(sseq);
164 return NULL;
165 }
166
167 spin_lock_init(&nseq->lock);
168 nseq->type = type;
169 nseq->sseqs = sseq;
170 nseq->alloc = 1;
171 INIT_HLIST_NODE(&nseq->ns_list);
172 INIT_LIST_HEAD(&nseq->subscriptions);
173 hlist_add_head_rcu(&nseq->ns_list, seq_head);
174 return nseq;
175 }
176
177 /**
178 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
179 *
180 * Very time-critical, so binary searches through sub-sequence array.
181 */
182 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
183 u32 instance)
184 {
185 struct sub_seq *sseqs = nseq->sseqs;
186 int low = 0;
187 int high = nseq->first_free - 1;
188 int mid;
189
190 while (low <= high) {
191 mid = (low + high) / 2;
192 if (instance < sseqs[mid].lower)
193 high = mid - 1;
194 else if (instance > sseqs[mid].upper)
195 low = mid + 1;
196 else
197 return &sseqs[mid];
198 }
199 return NULL;
200 }
201
202 /**
203 * nameseq_locate_subseq - determine position of name instance in sub-sequence
204 *
205 * Returns index in sub-sequence array of the entry that contains the specified
206 * instance value; if no entry contains that value, returns the position
207 * where a new entry for it would be inserted in the array.
208 *
209 * Note: Similar to binary search code for locating a sub-sequence.
210 */
211 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
212 {
213 struct sub_seq *sseqs = nseq->sseqs;
214 int low = 0;
215 int high = nseq->first_free - 1;
216 int mid;
217
218 while (low <= high) {
219 mid = (low + high) / 2;
220 if (instance < sseqs[mid].lower)
221 high = mid - 1;
222 else if (instance > sseqs[mid].upper)
223 low = mid + 1;
224 else
225 return mid;
226 }
227 return low;
228 }
229
230 /**
231 * tipc_nameseq_insert_publ
232 */
233 static struct publication *tipc_nameseq_insert_publ(struct net *net,
234 struct name_seq *nseq,
235 u32 type, u32 lower,
236 u32 upper, u32 scope,
237 u32 node, u32 port, u32 key)
238 {
239 struct tipc_subscription *s;
240 struct tipc_subscription *st;
241 struct publication *publ;
242 struct sub_seq *sseq;
243 struct name_info *info;
244 int created_subseq = 0;
245
246 sseq = nameseq_find_subseq(nseq, lower);
247 if (sseq) {
248
249 /* Lower end overlaps existing entry => need an exact match */
250 if ((sseq->lower != lower) || (sseq->upper != upper)) {
251 return NULL;
252 }
253
254 info = sseq->info;
255
256 /* Check if an identical publication already exists */
257 list_for_each_entry(publ, &info->zone_list, zone_list) {
258 if ((publ->ref == port) && (publ->key == key) &&
259 (!publ->node || (publ->node == node)))
260 return NULL;
261 }
262 } else {
263 u32 inspos;
264 struct sub_seq *freesseq;
265
266 /* Find where lower end should be inserted */
267 inspos = nameseq_locate_subseq(nseq, lower);
268
269 /* Fail if upper end overlaps into an existing entry */
270 if ((inspos < nseq->first_free) &&
271 (upper >= nseq->sseqs[inspos].lower)) {
272 return NULL;
273 }
274
275 /* Ensure there is space for new sub-sequence */
276 if (nseq->first_free == nseq->alloc) {
277 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
278
279 if (!sseqs) {
280 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
281 type, lower, upper);
282 return NULL;
283 }
284 memcpy(sseqs, nseq->sseqs,
285 nseq->alloc * sizeof(struct sub_seq));
286 kfree(nseq->sseqs);
287 nseq->sseqs = sseqs;
288 nseq->alloc *= 2;
289 }
290
291 info = kzalloc(sizeof(*info), GFP_ATOMIC);
292 if (!info) {
293 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
294 type, lower, upper);
295 return NULL;
296 }
297
298 INIT_LIST_HEAD(&info->node_list);
299 INIT_LIST_HEAD(&info->cluster_list);
300 INIT_LIST_HEAD(&info->zone_list);
301
302 /* Insert new sub-sequence */
303 sseq = &nseq->sseqs[inspos];
304 freesseq = &nseq->sseqs[nseq->first_free];
305 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
306 memset(sseq, 0, sizeof(*sseq));
307 nseq->first_free++;
308 sseq->lower = lower;
309 sseq->upper = upper;
310 sseq->info = info;
311 created_subseq = 1;
312 }
313
314 /* Insert a publication */
315 publ = publ_create(type, lower, upper, scope, node, port, key);
316 if (!publ)
317 return NULL;
318
319 list_add(&publ->zone_list, &info->zone_list);
320 info->zone_list_size++;
321
322 if (in_own_cluster(net, node)) {
323 list_add(&publ->cluster_list, &info->cluster_list);
324 info->cluster_list_size++;
325 }
326
327 if (in_own_node(net, node)) {
328 list_add(&publ->node_list, &info->node_list);
329 info->node_list_size++;
330 }
331
332 /* Any subscriptions waiting for notification? */
333 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
334 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
335 TIPC_PUBLISHED, publ->ref,
336 publ->node, created_subseq);
337 }
338 return publ;
339 }
340
341 /**
342 * tipc_nameseq_remove_publ
343 *
344 * NOTE: There may be cases where TIPC is asked to remove a publication
345 * that is not in the name table. For example, if another node issues a
346 * publication for a name sequence that overlaps an existing name sequence
347 * the publication will not be recorded, which means the publication won't
348 * be found when the name sequence is later withdrawn by that node.
349 * A failed withdraw request simply returns a failure indication and lets the
350 * caller issue any error or warning messages associated with such a problem.
351 */
352 static struct publication *tipc_nameseq_remove_publ(struct net *net,
353 struct name_seq *nseq,
354 u32 inst, u32 node,
355 u32 ref, u32 key)
356 {
357 struct publication *publ;
358 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
359 struct name_info *info;
360 struct sub_seq *free;
361 struct tipc_subscription *s, *st;
362 int removed_subseq = 0;
363
364 if (!sseq)
365 return NULL;
366
367 info = sseq->info;
368
369 /* Locate publication, if it exists */
370 list_for_each_entry(publ, &info->zone_list, zone_list) {
371 if ((publ->key == key) && (publ->ref == ref) &&
372 (!publ->node || (publ->node == node)))
373 goto found;
374 }
375 return NULL;
376
377 found:
378 /* Remove publication from zone scope list */
379 list_del(&publ->zone_list);
380 info->zone_list_size--;
381
382 /* Remove publication from cluster scope list, if present */
383 if (in_own_cluster(net, node)) {
384 list_del(&publ->cluster_list);
385 info->cluster_list_size--;
386 }
387
388 /* Remove publication from node scope list, if present */
389 if (in_own_node(net, node)) {
390 list_del(&publ->node_list);
391 info->node_list_size--;
392 }
393
394 /* Contract subseq list if no more publications for that subseq */
395 if (list_empty(&info->zone_list)) {
396 kfree(info);
397 free = &nseq->sseqs[nseq->first_free--];
398 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
399 removed_subseq = 1;
400 }
401
402 /* Notify any waiting subscriptions */
403 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
404 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
405 TIPC_WITHDRAWN, publ->ref,
406 publ->node, removed_subseq);
407 }
408
409 return publ;
410 }
411
412 /**
413 * tipc_nameseq_subscribe - attach a subscription, and issue
414 * the prescribed number of events if there is any sub-
415 * sequence overlapping with the requested sequence
416 */
417 static void tipc_nameseq_subscribe(struct name_seq *nseq,
418 struct tipc_subscription *s)
419 {
420 struct sub_seq *sseq = nseq->sseqs;
421 struct tipc_name_seq ns;
422
423 tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
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_subscrp_check_overlap(&ns, 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_subscrp_report_overlap(s, sseq->lower,
438 sseq->upper,
439 TIPC_PUBLISHED,
440 crs->ref, crs->node,
441 must_report);
442 must_report = 0;
443 }
444 }
445 sseq++;
446 }
447 }
448
449 static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
450 {
451 struct tipc_net *tn = net_generic(net, tipc_net_id);
452 struct hlist_head *seq_head;
453 struct name_seq *ns;
454
455 seq_head = &tn->nametbl->seq_hlist[hash(type)];
456 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
457 if (ns->type == type)
458 return ns;
459 }
460
461 return NULL;
462 };
463
464 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
465 u32 lower, u32 upper, u32 scope,
466 u32 node, u32 port, u32 key)
467 {
468 struct tipc_net *tn = net_generic(net, tipc_net_id);
469 struct publication *publ;
470 struct name_seq *seq = nametbl_find_seq(net, type);
471 int index = hash(type);
472
473 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
474 (lower > upper)) {
475 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
476 type, lower, upper, scope);
477 return NULL;
478 }
479
480 if (!seq)
481 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
482 if (!seq)
483 return NULL;
484
485 spin_lock_bh(&seq->lock);
486 publ = tipc_nameseq_insert_publ(net, 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(struct net *net, u32 type,
493 u32 lower, u32 node, u32 ref,
494 u32 key)
495 {
496 struct publication *publ;
497 struct name_seq *seq = nametbl_find_seq(net, type);
498
499 if (!seq)
500 return NULL;
501
502 spin_lock_bh(&seq->lock);
503 publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
504 if (!seq->first_free && list_empty(&seq->subscriptions)) {
505 hlist_del_init_rcu(&seq->ns_list);
506 kfree(seq->sseqs);
507 spin_unlock_bh(&seq->lock);
508 kfree_rcu(seq, rcu);
509 return publ;
510 }
511 spin_unlock_bh(&seq->lock);
512 return publ;
513 }
514
515 /**
516 * tipc_nametbl_translate - perform name translation
517 *
518 * On entry, 'destnode' is the search domain used during translation.
519 *
520 * On exit:
521 * - if name translation is deferred to another node/cluster/zone,
522 * leaves 'destnode' unchanged (will be non-zero) and returns 0
523 * - if name translation is attempted and succeeds, sets 'destnode'
524 * to publishing node and returns port reference (will be non-zero)
525 * - if name translation is attempted and fails, sets 'destnode' to 0
526 * and returns 0
527 */
528 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
529 u32 *destnode)
530 {
531 struct tipc_net *tn = net_generic(net, tipc_net_id);
532 struct sub_seq *sseq;
533 struct name_info *info;
534 struct publication *publ;
535 struct name_seq *seq;
536 u32 ref = 0;
537 u32 node = 0;
538
539 if (!tipc_in_scope(*destnode, tn->own_addr))
540 return 0;
541
542 rcu_read_lock();
543 seq = nametbl_find_seq(net, type);
544 if (unlikely(!seq))
545 goto not_found;
546 spin_lock_bh(&seq->lock);
547 sseq = nameseq_find_subseq(seq, instance);
548 if (unlikely(!sseq))
549 goto no_match;
550 info = sseq->info;
551
552 /* Closest-First Algorithm */
553 if (likely(!*destnode)) {
554 if (!list_empty(&info->node_list)) {
555 publ = list_first_entry(&info->node_list,
556 struct publication,
557 node_list);
558 list_move_tail(&publ->node_list,
559 &info->node_list);
560 } else if (!list_empty(&info->cluster_list)) {
561 publ = list_first_entry(&info->cluster_list,
562 struct publication,
563 cluster_list);
564 list_move_tail(&publ->cluster_list,
565 &info->cluster_list);
566 } else {
567 publ = list_first_entry(&info->zone_list,
568 struct publication,
569 zone_list);
570 list_move_tail(&publ->zone_list,
571 &info->zone_list);
572 }
573 }
574
575 /* Round-Robin Algorithm */
576 else if (*destnode == tn->own_addr) {
577 if (list_empty(&info->node_list))
578 goto no_match;
579 publ = list_first_entry(&info->node_list, struct publication,
580 node_list);
581 list_move_tail(&publ->node_list, &info->node_list);
582 } else if (in_own_cluster_exact(net, *destnode)) {
583 if (list_empty(&info->cluster_list))
584 goto no_match;
585 publ = list_first_entry(&info->cluster_list, struct publication,
586 cluster_list);
587 list_move_tail(&publ->cluster_list, &info->cluster_list);
588 } else {
589 publ = list_first_entry(&info->zone_list, struct publication,
590 zone_list);
591 list_move_tail(&publ->zone_list, &info->zone_list);
592 }
593
594 ref = publ->ref;
595 node = publ->node;
596 no_match:
597 spin_unlock_bh(&seq->lock);
598 not_found:
599 rcu_read_unlock();
600 *destnode = node;
601 return ref;
602 }
603
604 /**
605 * tipc_nametbl_mc_translate - find multicast destinations
606 *
607 * Creates list of all local ports that overlap the given multicast address;
608 * also determines if any off-node ports overlap.
609 *
610 * Note: Publications with a scope narrower than 'limit' are ignored.
611 * (i.e. local node-scope publications mustn't receive messages arriving
612 * from another node, even if the multcast link brought it here)
613 *
614 * Returns non-zero if any off-node ports overlap
615 */
616 int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper,
617 u32 limit, struct tipc_plist *dports)
618 {
619 struct name_seq *seq;
620 struct sub_seq *sseq;
621 struct sub_seq *sseq_stop;
622 struct name_info *info;
623 int res = 0;
624
625 rcu_read_lock();
626 seq = nametbl_find_seq(net, type);
627 if (!seq)
628 goto exit;
629
630 spin_lock_bh(&seq->lock);
631 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
632 sseq_stop = seq->sseqs + seq->first_free;
633 for (; sseq != sseq_stop; sseq++) {
634 struct publication *publ;
635
636 if (sseq->lower > upper)
637 break;
638
639 info = sseq->info;
640 list_for_each_entry(publ, &info->node_list, node_list) {
641 if (publ->scope <= limit)
642 tipc_plist_push(dports, publ->ref);
643 }
644
645 if (info->cluster_list_size != info->node_list_size)
646 res = 1;
647 }
648 spin_unlock_bh(&seq->lock);
649 exit:
650 rcu_read_unlock();
651 return res;
652 }
653
654 /*
655 * tipc_nametbl_publish - add name publication to network name tables
656 */
657 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
658 u32 upper, u32 scope, u32 port_ref,
659 u32 key)
660 {
661 struct publication *publ;
662 struct sk_buff *buf = NULL;
663 struct tipc_net *tn = net_generic(net, tipc_net_id);
664
665 spin_lock_bh(&tn->nametbl_lock);
666 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
667 pr_warn("Publication failed, local publication limit reached (%u)\n",
668 TIPC_MAX_PUBLICATIONS);
669 spin_unlock_bh(&tn->nametbl_lock);
670 return NULL;
671 }
672
673 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
674 tn->own_addr, port_ref, key);
675 if (likely(publ)) {
676 tn->nametbl->local_publ_count++;
677 buf = tipc_named_publish(net, publ);
678 /* Any pending external events? */
679 tipc_named_process_backlog(net);
680 }
681 spin_unlock_bh(&tn->nametbl_lock);
682
683 if (buf)
684 tipc_node_broadcast(net, buf);
685 return publ;
686 }
687
688 /**
689 * tipc_nametbl_withdraw - withdraw name publication from network name tables
690 */
691 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
692 u32 key)
693 {
694 struct publication *publ;
695 struct sk_buff *skb = NULL;
696 struct tipc_net *tn = net_generic(net, tipc_net_id);
697
698 spin_lock_bh(&tn->nametbl_lock);
699 publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
700 ref, key);
701 if (likely(publ)) {
702 tn->nametbl->local_publ_count--;
703 skb = tipc_named_withdraw(net, publ);
704 /* Any pending external events? */
705 tipc_named_process_backlog(net);
706 list_del_init(&publ->pport_list);
707 kfree_rcu(publ, rcu);
708 } else {
709 pr_err("Unable to remove local publication\n"
710 "(type=%u, lower=%u, ref=%u, key=%u)\n",
711 type, lower, ref, key);
712 }
713 spin_unlock_bh(&tn->nametbl_lock);
714
715 if (skb) {
716 tipc_node_broadcast(net, skb);
717 return 1;
718 }
719 return 0;
720 }
721
722 /**
723 * tipc_nametbl_subscribe - add a subscription object to the name table
724 */
725 void tipc_nametbl_subscribe(struct tipc_subscription *s)
726 {
727 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
728 u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
729 int index = hash(type);
730 struct name_seq *seq;
731 struct tipc_name_seq ns;
732
733 spin_lock_bh(&tn->nametbl_lock);
734 seq = nametbl_find_seq(s->net, type);
735 if (!seq)
736 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
737 if (seq) {
738 spin_lock_bh(&seq->lock);
739 tipc_nameseq_subscribe(seq, s);
740 spin_unlock_bh(&seq->lock);
741 } else {
742 tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
743 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
744 ns.type, ns.lower, ns.upper);
745 }
746 spin_unlock_bh(&tn->nametbl_lock);
747 }
748
749 /**
750 * tipc_nametbl_unsubscribe - remove a subscription object from name table
751 */
752 void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
753 {
754 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
755 struct name_seq *seq;
756 u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
757
758 spin_lock_bh(&tn->nametbl_lock);
759 seq = nametbl_find_seq(s->net, type);
760 if (seq != NULL) {
761 spin_lock_bh(&seq->lock);
762 list_del_init(&s->nameseq_list);
763 if (!seq->first_free && list_empty(&seq->subscriptions)) {
764 hlist_del_init_rcu(&seq->ns_list);
765 kfree(seq->sseqs);
766 spin_unlock_bh(&seq->lock);
767 kfree_rcu(seq, rcu);
768 } else {
769 spin_unlock_bh(&seq->lock);
770 }
771 }
772 spin_unlock_bh(&tn->nametbl_lock);
773 }
774
775 int tipc_nametbl_init(struct net *net)
776 {
777 struct tipc_net *tn = net_generic(net, tipc_net_id);
778 struct name_table *tipc_nametbl;
779 int i;
780
781 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
782 if (!tipc_nametbl)
783 return -ENOMEM;
784
785 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
786 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
787
788 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
789 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
790 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
791 tn->nametbl = tipc_nametbl;
792 spin_lock_init(&tn->nametbl_lock);
793 return 0;
794 }
795
796 /**
797 * tipc_purge_publications - remove all publications for a given type
798 *
799 * tipc_nametbl_lock must be held when calling this function
800 */
801 static void tipc_purge_publications(struct net *net, struct name_seq *seq)
802 {
803 struct publication *publ, *safe;
804 struct sub_seq *sseq;
805 struct name_info *info;
806
807 spin_lock_bh(&seq->lock);
808 sseq = seq->sseqs;
809 info = sseq->info;
810 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
811 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
812 publ->ref, publ->key);
813 kfree_rcu(publ, rcu);
814 }
815 hlist_del_init_rcu(&seq->ns_list);
816 kfree(seq->sseqs);
817 spin_unlock_bh(&seq->lock);
818
819 kfree_rcu(seq, rcu);
820 }
821
822 void tipc_nametbl_stop(struct net *net)
823 {
824 u32 i;
825 struct name_seq *seq;
826 struct hlist_head *seq_head;
827 struct tipc_net *tn = net_generic(net, tipc_net_id);
828 struct name_table *tipc_nametbl = tn->nametbl;
829
830 /* Verify name table is empty and purge any lingering
831 * publications, then release the name table
832 */
833 spin_lock_bh(&tn->nametbl_lock);
834 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
835 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
836 continue;
837 seq_head = &tipc_nametbl->seq_hlist[i];
838 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
839 tipc_purge_publications(net, seq);
840 }
841 }
842 spin_unlock_bh(&tn->nametbl_lock);
843
844 synchronize_net();
845 kfree(tipc_nametbl);
846
847 }
848
849 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
850 struct name_seq *seq,
851 struct sub_seq *sseq, u32 *last_publ)
852 {
853 void *hdr;
854 struct nlattr *attrs;
855 struct nlattr *publ;
856 struct publication *p;
857
858 if (*last_publ) {
859 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
860 if (p->key == *last_publ)
861 break;
862 if (p->key != *last_publ)
863 return -EPIPE;
864 } else {
865 p = list_first_entry(&sseq->info->zone_list, struct publication,
866 zone_list);
867 }
868
869 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
870 *last_publ = p->key;
871
872 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
873 &tipc_genl_family, NLM_F_MULTI,
874 TIPC_NL_NAME_TABLE_GET);
875 if (!hdr)
876 return -EMSGSIZE;
877
878 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
879 if (!attrs)
880 goto msg_full;
881
882 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
883 if (!publ)
884 goto attr_msg_full;
885
886 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
887 goto publ_msg_full;
888 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
889 goto publ_msg_full;
890 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
891 goto publ_msg_full;
892 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
893 goto publ_msg_full;
894 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
895 goto publ_msg_full;
896 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
897 goto publ_msg_full;
898 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
899 goto publ_msg_full;
900
901 nla_nest_end(msg->skb, publ);
902 nla_nest_end(msg->skb, attrs);
903 genlmsg_end(msg->skb, hdr);
904 }
905 *last_publ = 0;
906
907 return 0;
908
909 publ_msg_full:
910 nla_nest_cancel(msg->skb, publ);
911 attr_msg_full:
912 nla_nest_cancel(msg->skb, attrs);
913 msg_full:
914 genlmsg_cancel(msg->skb, hdr);
915
916 return -EMSGSIZE;
917 }
918
919 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
920 u32 *last_lower, u32 *last_publ)
921 {
922 struct sub_seq *sseq;
923 struct sub_seq *sseq_start;
924 int err;
925
926 if (*last_lower) {
927 sseq_start = nameseq_find_subseq(seq, *last_lower);
928 if (!sseq_start)
929 return -EPIPE;
930 } else {
931 sseq_start = seq->sseqs;
932 }
933
934 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
935 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
936 if (err) {
937 *last_lower = sseq->lower;
938 return err;
939 }
940 }
941 *last_lower = 0;
942
943 return 0;
944 }
945
946 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
947 u32 *last_type, u32 *last_lower, u32 *last_publ)
948 {
949 struct tipc_net *tn = net_generic(net, tipc_net_id);
950 struct hlist_head *seq_head;
951 struct name_seq *seq = NULL;
952 int err;
953 int i;
954
955 if (*last_type)
956 i = hash(*last_type);
957 else
958 i = 0;
959
960 for (; i < TIPC_NAMETBL_SIZE; i++) {
961 seq_head = &tn->nametbl->seq_hlist[i];
962
963 if (*last_type) {
964 seq = nametbl_find_seq(net, *last_type);
965 if (!seq)
966 return -EPIPE;
967 } else {
968 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
969 break;
970 if (!seq)
971 continue;
972 }
973
974 hlist_for_each_entry_from_rcu(seq, ns_list) {
975 spin_lock_bh(&seq->lock);
976 err = __tipc_nl_subseq_list(msg, seq, last_lower,
977 last_publ);
978
979 if (err) {
980 *last_type = seq->type;
981 spin_unlock_bh(&seq->lock);
982 return err;
983 }
984 spin_unlock_bh(&seq->lock);
985 }
986 *last_type = 0;
987 }
988 return 0;
989 }
990
991 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
992 {
993 int err;
994 int done = cb->args[3];
995 u32 last_type = cb->args[0];
996 u32 last_lower = cb->args[1];
997 u32 last_publ = cb->args[2];
998 struct net *net = sock_net(skb->sk);
999 struct tipc_nl_msg msg;
1000
1001 if (done)
1002 return 0;
1003
1004 msg.skb = skb;
1005 msg.portid = NETLINK_CB(cb->skb).portid;
1006 msg.seq = cb->nlh->nlmsg_seq;
1007
1008 rcu_read_lock();
1009 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
1010 if (!err) {
1011 done = 1;
1012 } else if (err != -EMSGSIZE) {
1013 /* We never set seq or call nl_dump_check_consistent() this
1014 * means that setting prev_seq here will cause the consistence
1015 * check to fail in the netlink callback handler. Resulting in
1016 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1017 * we got an error.
1018 */
1019 cb->prev_seq = 1;
1020 }
1021 rcu_read_unlock();
1022
1023 cb->args[0] = last_type;
1024 cb->args[1] = last_lower;
1025 cb->args[2] = last_publ;
1026 cb->args[3] = done;
1027
1028 return skb->len;
1029 }
1030
1031 void tipc_plist_push(struct tipc_plist *pl, u32 port)
1032 {
1033 struct tipc_plist *nl;
1034
1035 if (likely(!pl->port)) {
1036 pl->port = port;
1037 return;
1038 }
1039 if (pl->port == port)
1040 return;
1041 list_for_each_entry(nl, &pl->list, list) {
1042 if (nl->port == port)
1043 return;
1044 }
1045 nl = kmalloc(sizeof(*nl), GFP_ATOMIC);
1046 if (nl) {
1047 nl->port = port;
1048 list_add(&nl->list, &pl->list);
1049 }
1050 }
1051
1052 u32 tipc_plist_pop(struct tipc_plist *pl)
1053 {
1054 struct tipc_plist *nl;
1055 u32 port = 0;
1056
1057 if (likely(list_empty(&pl->list))) {
1058 port = pl->port;
1059 pl->port = 0;
1060 return port;
1061 }
1062 nl = list_first_entry(&pl->list, typeof(*nl), list);
1063 port = nl->port;
1064 list_del(&nl->list);
1065 kfree(nl);
1066 return port;
1067 }
This page took 0.053445 seconds and 6 git commands to generate.