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