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 "config.h" 40 #include "name_table.h" 41 #include "name_distr.h" 42 #include "subscr.h" 43 #include "bcast.h" 44 45 #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */ 46 47 static const struct nla_policy 48 tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = { 49 [TIPC_NLA_NAME_TABLE_UNSPEC] = { .type = NLA_UNSPEC }, 50 [TIPC_NLA_NAME_TABLE_PUBL] = { .type = NLA_NESTED } 51 }; 52 53 /** 54 * struct name_info - name sequence publication info 55 * @node_list: circular list of publications made by own node 56 * @cluster_list: circular list of publications made by own cluster 57 * @zone_list: circular list of publications made by own zone 58 * @node_list_size: number of entries in "node_list" 59 * @cluster_list_size: number of entries in "cluster_list" 60 * @zone_list_size: number of entries in "zone_list" 61 * 62 * Note: The zone list always contains at least one entry, since all 63 * publications of the associated name sequence belong to it. 64 * (The cluster and node lists may be empty.) 65 */ 66 struct name_info { 67 struct list_head node_list; 68 struct list_head cluster_list; 69 struct list_head zone_list; 70 u32 node_list_size; 71 u32 cluster_list_size; 72 u32 zone_list_size; 73 }; 74 75 /** 76 * struct sub_seq - container for all published instances of a name sequence 77 * @lower: name sequence lower bound 78 * @upper: name sequence upper bound 79 * @info: pointer to name sequence publication info 80 */ 81 struct sub_seq { 82 u32 lower; 83 u32 upper; 84 struct name_info *info; 85 }; 86 87 /** 88 * struct name_seq - container for all published instances of a name type 89 * @type: 32 bit 'type' value for name sequence 90 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type'; 91 * sub-sequences are sorted in ascending order 92 * @alloc: number of sub-sequences currently in array 93 * @first_free: array index of first unused sub-sequence entry 94 * @ns_list: links to adjacent name sequences in hash chain 95 * @subscriptions: list of subscriptions for this 'type' 96 * @lock: spinlock controlling access to publication lists of all sub-sequences 97 * @rcu: RCU callback head used for deferred freeing 98 */ 99 struct name_seq { 100 u32 type; 101 struct sub_seq *sseqs; 102 u32 alloc; 103 u32 first_free; 104 struct hlist_node ns_list; 105 struct list_head subscriptions; 106 spinlock_t lock; 107 struct rcu_head rcu; 108 }; 109 110 static int hash(int x) 111 { 112 return x & (TIPC_NAMETBL_SIZE - 1); 113 } 114 115 /** 116 * publ_create - create a publication structure 117 */ 118 static struct publication *publ_create(u32 type, u32 lower, u32 upper, 119 u32 scope, u32 node, u32 port_ref, 120 u32 key) 121 { 122 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC); 123 if (publ == NULL) { 124 pr_warn("Publication creation failure, no memory\n"); 125 return NULL; 126 } 127 128 publ->type = type; 129 publ->lower = lower; 130 publ->upper = upper; 131 publ->scope = scope; 132 publ->node = node; 133 publ->ref = port_ref; 134 publ->key = key; 135 INIT_LIST_HEAD(&publ->pport_list); 136 return publ; 137 } 138 139 /** 140 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures 141 */ 142 static struct sub_seq *tipc_subseq_alloc(u32 cnt) 143 { 144 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC); 145 } 146 147 /** 148 * tipc_nameseq_create - create a name sequence structure for the specified 'type' 149 * 150 * Allocates a single sub-sequence structure and sets it to all 0's. 151 */ 152 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head) 153 { 154 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC); 155 struct sub_seq *sseq = tipc_subseq_alloc(1); 156 157 if (!nseq || !sseq) { 158 pr_warn("Name sequence creation failed, no memory\n"); 159 kfree(nseq); 160 kfree(sseq); 161 return NULL; 162 } 163 164 spin_lock_init(&nseq->lock); 165 nseq->type = type; 166 nseq->sseqs = sseq; 167 nseq->alloc = 1; 168 INIT_HLIST_NODE(&nseq->ns_list); 169 INIT_LIST_HEAD(&nseq->subscriptions); 170 hlist_add_head_rcu(&nseq->ns_list, seq_head); 171 return nseq; 172 } 173 174 /** 175 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance 176 * 177 * Very time-critical, so binary searches through sub-sequence array. 178 */ 179 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq, 180 u32 instance) 181 { 182 struct sub_seq *sseqs = nseq->sseqs; 183 int low = 0; 184 int high = nseq->first_free - 1; 185 int mid; 186 187 while (low <= high) { 188 mid = (low + high) / 2; 189 if (instance < sseqs[mid].lower) 190 high = mid - 1; 191 else if (instance > sseqs[mid].upper) 192 low = mid + 1; 193 else 194 return &sseqs[mid]; 195 } 196 return NULL; 197 } 198 199 /** 200 * nameseq_locate_subseq - determine position of name instance in sub-sequence 201 * 202 * Returns index in sub-sequence array of the entry that contains the specified 203 * instance value; if no entry contains that value, returns the position 204 * where a new entry for it would be inserted in the array. 205 * 206 * Note: Similar to binary search code for locating a sub-sequence. 207 */ 208 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance) 209 { 210 struct sub_seq *sseqs = nseq->sseqs; 211 int low = 0; 212 int high = nseq->first_free - 1; 213 int mid; 214 215 while (low <= high) { 216 mid = (low + high) / 2; 217 if (instance < sseqs[mid].lower) 218 high = mid - 1; 219 else if (instance > sseqs[mid].upper) 220 low = mid + 1; 221 else 222 return mid; 223 } 224 return low; 225 } 226 227 /** 228 * tipc_nameseq_insert_publ 229 */ 230 static struct publication *tipc_nameseq_insert_publ(struct net *net, 231 struct name_seq *nseq, 232 u32 type, u32 lower, 233 u32 upper, u32 scope, 234 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(net, node)) { 320 list_add(&publ->cluster_list, &info->cluster_list); 321 info->cluster_list_size++; 322 } 323 324 if (in_own_node(net, 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 net *net, 354 struct name_seq *nseq, 355 u32 inst, u32 node, 356 u32 ref, u32 key) 357 { 358 struct publication *publ; 359 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst); 360 struct name_info *info; 361 struct sub_seq *free; 362 struct tipc_subscription *s, *st; 363 int removed_subseq = 0; 364 365 if (!sseq) 366 return NULL; 367 368 info = sseq->info; 369 370 /* Locate publication, if it exists */ 371 list_for_each_entry(publ, &info->zone_list, zone_list) { 372 if ((publ->key == key) && (publ->ref == ref) && 373 (!publ->node || (publ->node == node))) 374 goto found; 375 } 376 return NULL; 377 378 found: 379 /* Remove publication from zone scope list */ 380 list_del(&publ->zone_list); 381 info->zone_list_size--; 382 383 /* Remove publication from cluster scope list, if present */ 384 if (in_own_cluster(net, node)) { 385 list_del(&publ->cluster_list); 386 info->cluster_list_size--; 387 } 388 389 /* Remove publication from node scope list, if present */ 390 if (in_own_node(net, node)) { 391 list_del(&publ->node_list); 392 info->node_list_size--; 393 } 394 395 /* Contract subseq list if no more publications for that subseq */ 396 if (list_empty(&info->zone_list)) { 397 kfree(info); 398 free = &nseq->sseqs[nseq->first_free--]; 399 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq)); 400 removed_subseq = 1; 401 } 402 403 /* Notify any waiting subscriptions */ 404 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) { 405 tipc_subscr_report_overlap(s, 406 publ->lower, 407 publ->upper, 408 TIPC_WITHDRAWN, 409 publ->ref, 410 publ->node, 411 removed_subseq); 412 } 413 414 return publ; 415 } 416 417 /** 418 * tipc_nameseq_subscribe - attach a subscription, and issue 419 * the prescribed number of events if there is any sub- 420 * sequence overlapping with the requested sequence 421 */ 422 static void tipc_nameseq_subscribe(struct name_seq *nseq, 423 struct tipc_subscription *s) 424 { 425 struct sub_seq *sseq = nseq->sseqs; 426 427 list_add(&s->nameseq_list, &nseq->subscriptions); 428 429 if (!sseq) 430 return; 431 432 while (sseq != &nseq->sseqs[nseq->first_free]) { 433 if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) { 434 struct publication *crs; 435 struct name_info *info = sseq->info; 436 int must_report = 1; 437 438 list_for_each_entry(crs, &info->zone_list, zone_list) { 439 tipc_subscr_report_overlap(s, 440 sseq->lower, 441 sseq->upper, 442 TIPC_PUBLISHED, 443 crs->ref, 444 crs->node, 445 must_report); 446 must_report = 0; 447 } 448 } 449 sseq++; 450 } 451 } 452 453 static struct name_seq *nametbl_find_seq(struct net *net, u32 type) 454 { 455 struct tipc_net *tn = net_generic(net, tipc_net_id); 456 struct hlist_head *seq_head; 457 struct name_seq *ns; 458 459 seq_head = &tn->nametbl->seq_hlist[hash(type)]; 460 hlist_for_each_entry_rcu(ns, seq_head, ns_list) { 461 if (ns->type == type) 462 return ns; 463 } 464 465 return NULL; 466 }; 467 468 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type, 469 u32 lower, u32 upper, u32 scope, 470 u32 node, u32 port, u32 key) 471 { 472 struct tipc_net *tn = net_generic(net, tipc_net_id); 473 struct publication *publ; 474 struct name_seq *seq = nametbl_find_seq(net, type); 475 int index = hash(type); 476 477 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) || 478 (lower > upper)) { 479 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n", 480 type, lower, upper, scope); 481 return NULL; 482 } 483 484 if (!seq) 485 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]); 486 if (!seq) 487 return NULL; 488 489 spin_lock_bh(&seq->lock); 490 publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper, 491 scope, node, port, key); 492 spin_unlock_bh(&seq->lock); 493 return publ; 494 } 495 496 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type, 497 u32 lower, u32 node, u32 ref, 498 u32 key) 499 { 500 struct publication *publ; 501 struct name_seq *seq = nametbl_find_seq(net, type); 502 503 if (!seq) 504 return NULL; 505 506 spin_lock_bh(&seq->lock); 507 publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key); 508 if (!seq->first_free && list_empty(&seq->subscriptions)) { 509 hlist_del_init_rcu(&seq->ns_list); 510 kfree(seq->sseqs); 511 spin_unlock_bh(&seq->lock); 512 kfree_rcu(seq, rcu); 513 return publ; 514 } 515 spin_unlock_bh(&seq->lock); 516 return publ; 517 } 518 519 /** 520 * tipc_nametbl_translate - perform name translation 521 * 522 * On entry, 'destnode' is the search domain used during translation. 523 * 524 * On exit: 525 * - if name translation is deferred to another node/cluster/zone, 526 * leaves 'destnode' unchanged (will be non-zero) and returns 0 527 * - if name translation is attempted and succeeds, sets 'destnode' 528 * to publishing node and returns port reference (will be non-zero) 529 * - if name translation is attempted and fails, sets 'destnode' to 0 530 * and returns 0 531 */ 532 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, 533 u32 *destnode) 534 { 535 struct tipc_net *tn = net_generic(net, tipc_net_id); 536 struct sub_seq *sseq; 537 struct name_info *info; 538 struct publication *publ; 539 struct name_seq *seq; 540 u32 ref = 0; 541 u32 node = 0; 542 543 if (!tipc_in_scope(*destnode, tn->own_addr)) 544 return 0; 545 546 rcu_read_lock(); 547 seq = nametbl_find_seq(net, type); 548 if (unlikely(!seq)) 549 goto not_found; 550 spin_lock_bh(&seq->lock); 551 sseq = nameseq_find_subseq(seq, instance); 552 if (unlikely(!sseq)) 553 goto no_match; 554 info = sseq->info; 555 556 /* Closest-First Algorithm */ 557 if (likely(!*destnode)) { 558 if (!list_empty(&info->node_list)) { 559 publ = list_first_entry(&info->node_list, 560 struct publication, 561 node_list); 562 list_move_tail(&publ->node_list, 563 &info->node_list); 564 } else if (!list_empty(&info->cluster_list)) { 565 publ = list_first_entry(&info->cluster_list, 566 struct publication, 567 cluster_list); 568 list_move_tail(&publ->cluster_list, 569 &info->cluster_list); 570 } else { 571 publ = list_first_entry(&info->zone_list, 572 struct publication, 573 zone_list); 574 list_move_tail(&publ->zone_list, 575 &info->zone_list); 576 } 577 } 578 579 /* Round-Robin Algorithm */ 580 else if (*destnode == tn->own_addr) { 581 if (list_empty(&info->node_list)) 582 goto no_match; 583 publ = list_first_entry(&info->node_list, struct publication, 584 node_list); 585 list_move_tail(&publ->node_list, &info->node_list); 586 } else if (in_own_cluster_exact(net, *destnode)) { 587 if (list_empty(&info->cluster_list)) 588 goto no_match; 589 publ = list_first_entry(&info->cluster_list, struct publication, 590 cluster_list); 591 list_move_tail(&publ->cluster_list, &info->cluster_list); 592 } else { 593 publ = list_first_entry(&info->zone_list, struct publication, 594 zone_list); 595 list_move_tail(&publ->zone_list, &info->zone_list); 596 } 597 598 ref = publ->ref; 599 node = publ->node; 600 no_match: 601 spin_unlock_bh(&seq->lock); 602 not_found: 603 rcu_read_unlock(); 604 *destnode = node; 605 return ref; 606 } 607 608 /** 609 * tipc_nametbl_mc_translate - find multicast destinations 610 * 611 * Creates list of all local ports that overlap the given multicast address; 612 * also determines if any off-node ports overlap. 613 * 614 * Note: Publications with a scope narrower than 'limit' are ignored. 615 * (i.e. local node-scope publications mustn't receive messages arriving 616 * from another node, even if the multcast link brought it here) 617 * 618 * Returns non-zero if any off-node ports overlap 619 */ 620 int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper, 621 u32 limit, struct tipc_plist *dports) 622 { 623 struct name_seq *seq; 624 struct sub_seq *sseq; 625 struct sub_seq *sseq_stop; 626 struct name_info *info; 627 int res = 0; 628 629 rcu_read_lock(); 630 seq = nametbl_find_seq(net, type); 631 if (!seq) 632 goto exit; 633 634 spin_lock_bh(&seq->lock); 635 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower); 636 sseq_stop = seq->sseqs + seq->first_free; 637 for (; sseq != sseq_stop; sseq++) { 638 struct publication *publ; 639 640 if (sseq->lower > upper) 641 break; 642 643 info = sseq->info; 644 list_for_each_entry(publ, &info->node_list, node_list) { 645 if (publ->scope <= limit) 646 tipc_plist_push(dports, publ->ref); 647 } 648 649 if (info->cluster_list_size != info->node_list_size) 650 res = 1; 651 } 652 spin_unlock_bh(&seq->lock); 653 exit: 654 rcu_read_unlock(); 655 return res; 656 } 657 658 /* 659 * tipc_nametbl_publish - add name publication to network name tables 660 */ 661 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower, 662 u32 upper, u32 scope, u32 port_ref, 663 u32 key) 664 { 665 struct publication *publ; 666 struct sk_buff *buf = NULL; 667 struct tipc_net *tn = net_generic(net, tipc_net_id); 668 669 spin_lock_bh(&tn->nametbl_lock); 670 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) { 671 pr_warn("Publication failed, local publication limit reached (%u)\n", 672 TIPC_MAX_PUBLICATIONS); 673 spin_unlock_bh(&tn->nametbl_lock); 674 return NULL; 675 } 676 677 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope, 678 tn->own_addr, port_ref, key); 679 if (likely(publ)) { 680 tn->nametbl->local_publ_count++; 681 buf = tipc_named_publish(net, publ); 682 /* Any pending external events? */ 683 tipc_named_process_backlog(net); 684 } 685 spin_unlock_bh(&tn->nametbl_lock); 686 687 if (buf) 688 named_cluster_distribute(net, buf); 689 return publ; 690 } 691 692 /** 693 * tipc_nametbl_withdraw - withdraw name publication from network name tables 694 */ 695 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref, 696 u32 key) 697 { 698 struct publication *publ; 699 struct sk_buff *skb = NULL; 700 struct tipc_net *tn = net_generic(net, tipc_net_id); 701 702 spin_lock_bh(&tn->nametbl_lock); 703 publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr, 704 ref, key); 705 if (likely(publ)) { 706 tn->nametbl->local_publ_count--; 707 skb = tipc_named_withdraw(net, publ); 708 /* Any pending external events? */ 709 tipc_named_process_backlog(net); 710 list_del_init(&publ->pport_list); 711 kfree_rcu(publ, rcu); 712 } else { 713 pr_err("Unable to remove local publication\n" 714 "(type=%u, lower=%u, ref=%u, key=%u)\n", 715 type, lower, ref, key); 716 } 717 spin_unlock_bh(&tn->nametbl_lock); 718 719 if (skb) { 720 named_cluster_distribute(net, skb); 721 return 1; 722 } 723 return 0; 724 } 725 726 /** 727 * tipc_nametbl_subscribe - add a subscription object to the name table 728 */ 729 void tipc_nametbl_subscribe(struct tipc_subscription *s) 730 { 731 struct tipc_net *tn = net_generic(s->net, tipc_net_id); 732 u32 type = s->seq.type; 733 int index = hash(type); 734 struct name_seq *seq; 735 736 spin_lock_bh(&tn->nametbl_lock); 737 seq = nametbl_find_seq(s->net, type); 738 if (!seq) 739 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]); 740 if (seq) { 741 spin_lock_bh(&seq->lock); 742 tipc_nameseq_subscribe(seq, s); 743 spin_unlock_bh(&seq->lock); 744 } else { 745 pr_warn("Failed to create subscription for {%u,%u,%u}\n", 746 s->seq.type, s->seq.lower, s->seq.upper); 747 } 748 spin_unlock_bh(&tn->nametbl_lock); 749 } 750 751 /** 752 * tipc_nametbl_unsubscribe - remove a subscription object from name table 753 */ 754 void tipc_nametbl_unsubscribe(struct tipc_subscription *s) 755 { 756 struct tipc_net *tn = net_generic(s->net, tipc_net_id); 757 struct name_seq *seq; 758 759 spin_lock_bh(&tn->nametbl_lock); 760 seq = nametbl_find_seq(s->net, s->seq.type); 761 if (seq != NULL) { 762 spin_lock_bh(&seq->lock); 763 list_del_init(&s->nameseq_list); 764 if (!seq->first_free && list_empty(&seq->subscriptions)) { 765 hlist_del_init_rcu(&seq->ns_list); 766 kfree(seq->sseqs); 767 spin_unlock_bh(&seq->lock); 768 kfree_rcu(seq, rcu); 769 } else { 770 spin_unlock_bh(&seq->lock); 771 } 772 } 773 spin_unlock_bh(&tn->nametbl_lock); 774 } 775 776 int tipc_nametbl_init(struct net *net) 777 { 778 struct tipc_net *tn = net_generic(net, tipc_net_id); 779 struct name_table *tipc_nametbl; 780 int i; 781 782 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC); 783 if (!tipc_nametbl) 784 return -ENOMEM; 785 786 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) 787 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]); 788 789 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]); 790 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]); 791 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]); 792 tn->nametbl = tipc_nametbl; 793 spin_lock_init(&tn->nametbl_lock); 794 return 0; 795 } 796 797 /** 798 * tipc_purge_publications - remove all publications for a given type 799 * 800 * tipc_nametbl_lock must be held when calling this function 801 */ 802 static void tipc_purge_publications(struct net *net, struct name_seq *seq) 803 { 804 struct publication *publ, *safe; 805 struct sub_seq *sseq; 806 struct name_info *info; 807 808 spin_lock_bh(&seq->lock); 809 sseq = seq->sseqs; 810 info = sseq->info; 811 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) { 812 tipc_nametbl_remove_publ(net, publ->type, publ->lower, 813 publ->node, publ->ref, publ->key); 814 kfree_rcu(publ, rcu); 815 } 816 hlist_del_init_rcu(&seq->ns_list); 817 kfree(seq->sseqs); 818 spin_unlock_bh(&seq->lock); 819 820 kfree_rcu(seq, rcu); 821 } 822 823 void tipc_nametbl_stop(struct net *net) 824 { 825 u32 i; 826 struct name_seq *seq; 827 struct hlist_head *seq_head; 828 struct tipc_net *tn = net_generic(net, tipc_net_id); 829 struct name_table *tipc_nametbl = tn->nametbl; 830 831 /* Verify name table is empty and purge any lingering 832 * publications, then release the name table 833 */ 834 spin_lock_bh(&tn->nametbl_lock); 835 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) { 836 if (hlist_empty(&tipc_nametbl->seq_hlist[i])) 837 continue; 838 seq_head = &tipc_nametbl->seq_hlist[i]; 839 hlist_for_each_entry_rcu(seq, seq_head, ns_list) { 840 tipc_purge_publications(net, seq); 841 } 842 } 843 spin_unlock_bh(&tn->nametbl_lock); 844 845 synchronize_net(); 846 kfree(tipc_nametbl); 847 848 } 849 850 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg, 851 struct name_seq *seq, 852 struct sub_seq *sseq, u32 *last_publ) 853 { 854 void *hdr; 855 struct nlattr *attrs; 856 struct nlattr *publ; 857 struct publication *p; 858 859 if (*last_publ) { 860 list_for_each_entry(p, &sseq->info->zone_list, zone_list) 861 if (p->key == *last_publ) 862 break; 863 if (p->key != *last_publ) 864 return -EPIPE; 865 } else { 866 p = list_first_entry(&sseq->info->zone_list, struct publication, 867 zone_list); 868 } 869 870 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) { 871 *last_publ = p->key; 872 873 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, 874 &tipc_genl_family, NLM_F_MULTI, 875 TIPC_NL_NAME_TABLE_GET); 876 if (!hdr) 877 return -EMSGSIZE; 878 879 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE); 880 if (!attrs) 881 goto msg_full; 882 883 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL); 884 if (!publ) 885 goto attr_msg_full; 886 887 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type)) 888 goto publ_msg_full; 889 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower)) 890 goto publ_msg_full; 891 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper)) 892 goto publ_msg_full; 893 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope)) 894 goto publ_msg_full; 895 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node)) 896 goto publ_msg_full; 897 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref)) 898 goto publ_msg_full; 899 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key)) 900 goto publ_msg_full; 901 902 nla_nest_end(msg->skb, publ); 903 nla_nest_end(msg->skb, attrs); 904 genlmsg_end(msg->skb, hdr); 905 } 906 *last_publ = 0; 907 908 return 0; 909 910 publ_msg_full: 911 nla_nest_cancel(msg->skb, publ); 912 attr_msg_full: 913 nla_nest_cancel(msg->skb, attrs); 914 msg_full: 915 genlmsg_cancel(msg->skb, hdr); 916 917 return -EMSGSIZE; 918 } 919 920 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq, 921 u32 *last_lower, u32 *last_publ) 922 { 923 struct sub_seq *sseq; 924 struct sub_seq *sseq_start; 925 int err; 926 927 if (*last_lower) { 928 sseq_start = nameseq_find_subseq(seq, *last_lower); 929 if (!sseq_start) 930 return -EPIPE; 931 } else { 932 sseq_start = seq->sseqs; 933 } 934 935 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) { 936 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ); 937 if (err) { 938 *last_lower = sseq->lower; 939 return err; 940 } 941 } 942 *last_lower = 0; 943 944 return 0; 945 } 946 947 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg, 948 u32 *last_type, u32 *last_lower, u32 *last_publ) 949 { 950 struct tipc_net *tn = net_generic(net, tipc_net_id); 951 struct hlist_head *seq_head; 952 struct name_seq *seq = NULL; 953 int err; 954 int i; 955 956 if (*last_type) 957 i = hash(*last_type); 958 else 959 i = 0; 960 961 for (; i < TIPC_NAMETBL_SIZE; i++) { 962 seq_head = &tn->nametbl->seq_hlist[i]; 963 964 if (*last_type) { 965 seq = nametbl_find_seq(net, *last_type); 966 if (!seq) 967 return -EPIPE; 968 } else { 969 hlist_for_each_entry_rcu(seq, seq_head, ns_list) 970 break; 971 if (!seq) 972 continue; 973 } 974 975 hlist_for_each_entry_from_rcu(seq, ns_list) { 976 spin_lock_bh(&seq->lock); 977 err = __tipc_nl_subseq_list(msg, seq, last_lower, 978 last_publ); 979 980 if (err) { 981 *last_type = seq->type; 982 spin_unlock_bh(&seq->lock); 983 return err; 984 } 985 spin_unlock_bh(&seq->lock); 986 } 987 *last_type = 0; 988 } 989 return 0; 990 } 991 992 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb) 993 { 994 int err; 995 int done = cb->args[3]; 996 u32 last_type = cb->args[0]; 997 u32 last_lower = cb->args[1]; 998 u32 last_publ = cb->args[2]; 999 struct net *net = sock_net(skb->sk); 1000 struct tipc_nl_msg msg; 1001 1002 if (done) 1003 return 0; 1004 1005 msg.skb = skb; 1006 msg.portid = NETLINK_CB(cb->skb).portid; 1007 msg.seq = cb->nlh->nlmsg_seq; 1008 1009 rcu_read_lock(); 1010 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ); 1011 if (!err) { 1012 done = 1; 1013 } else if (err != -EMSGSIZE) { 1014 /* We never set seq or call nl_dump_check_consistent() this 1015 * means that setting prev_seq here will cause the consistence 1016 * check to fail in the netlink callback handler. Resulting in 1017 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if 1018 * we got an error. 1019 */ 1020 cb->prev_seq = 1; 1021 } 1022 rcu_read_unlock(); 1023 1024 cb->args[0] = last_type; 1025 cb->args[1] = last_lower; 1026 cb->args[2] = last_publ; 1027 cb->args[3] = done; 1028 1029 return skb->len; 1030 } 1031 1032 void tipc_plist_push(struct tipc_plist *pl, u32 port) 1033 { 1034 struct tipc_plist *nl; 1035 1036 if (likely(!pl->port)) { 1037 pl->port = port; 1038 return; 1039 } 1040 if (pl->port == port) 1041 return; 1042 list_for_each_entry(nl, &pl->list, list) { 1043 if (nl->port == port) 1044 return; 1045 } 1046 nl = kmalloc(sizeof(*nl), GFP_ATOMIC); 1047 if (nl) { 1048 nl->port = port; 1049 list_add(&nl->list, &pl->list); 1050 } 1051 } 1052 1053 u32 tipc_plist_pop(struct tipc_plist *pl) 1054 { 1055 struct tipc_plist *nl; 1056 u32 port = 0; 1057 1058 if (likely(list_empty(&pl->list))) { 1059 port = pl->port; 1060 pl->port = 0; 1061 return port; 1062 } 1063 nl = list_first_entry(&pl->list, typeof(*nl), list); 1064 port = nl->port; 1065 list_del(&nl->list); 1066 kfree(nl); 1067 return port; 1068 } 1069