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