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, '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 /* Any pending external events? */ 624 tipc_named_process_backlog(net); 625 } 626 exit: 627 spin_unlock_bh(&tn->nametbl_lock); 628 629 if (skb) 630 tipc_node_broadcast(net, skb); 631 return p; 632 } 633 634 /** 635 * tipc_nametbl_withdraw - withdraw a service binding 636 */ 637 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, 638 u32 port, u32 key) 639 { 640 struct name_table *nt = tipc_name_table(net); 641 struct tipc_net *tn = tipc_net(net); 642 u32 self = tipc_own_addr(net); 643 struct sk_buff *skb = NULL; 644 struct publication *p; 645 646 spin_lock_bh(&tn->nametbl_lock); 647 648 p = tipc_nametbl_remove_publ(net, type, lower, self, port, key); 649 if (p) { 650 nt->local_publ_count--; 651 skb = tipc_named_withdraw(net, p); 652 /* Any pending external events? */ 653 tipc_named_process_backlog(net); 654 list_del_init(&p->binding_sock); 655 kfree_rcu(p, rcu); 656 } else { 657 pr_err("Failed to remove local publication {%u,%u,%u}/%u\n", 658 type, lower, port, key); 659 } 660 spin_unlock_bh(&tn->nametbl_lock); 661 662 if (skb) { 663 tipc_node_broadcast(net, skb); 664 return 1; 665 } 666 return 0; 667 } 668 669 /** 670 * tipc_nametbl_subscribe - add a subscription object to the name table 671 */ 672 void tipc_nametbl_subscribe(struct tipc_subscription *sub) 673 { 674 struct name_table *nt = tipc_name_table(sub->net); 675 struct tipc_net *tn = tipc_net(sub->net); 676 struct tipc_subscr *s = &sub->evt.s; 677 u32 type = tipc_sub_read(s, seq.type); 678 struct tipc_service *sc; 679 680 spin_lock_bh(&tn->nametbl_lock); 681 sc = tipc_service_find(sub->net, type); 682 if (!sc) 683 sc = tipc_service_create(type, &nt->services[hash(type)]); 684 if (sc) { 685 spin_lock_bh(&sc->lock); 686 tipc_service_subscribe(sc, sub); 687 spin_unlock_bh(&sc->lock); 688 } else { 689 pr_warn("Failed to subscribe for {%u,%u,%u}\n", type, 690 tipc_sub_read(s, seq.lower), 691 tipc_sub_read(s, seq.upper)); 692 } 693 spin_unlock_bh(&tn->nametbl_lock); 694 } 695 696 /** 697 * tipc_nametbl_unsubscribe - remove a subscription object from name table 698 */ 699 void tipc_nametbl_unsubscribe(struct tipc_subscription *sub) 700 { 701 struct tipc_net *tn = tipc_net(sub->net); 702 struct tipc_subscr *s = &sub->evt.s; 703 u32 type = tipc_sub_read(s, seq.type); 704 struct tipc_service *sc; 705 706 spin_lock_bh(&tn->nametbl_lock); 707 sc = tipc_service_find(sub->net, type); 708 if (!sc) 709 goto exit; 710 711 spin_lock_bh(&sc->lock); 712 list_del_init(&sub->service_list); 713 tipc_sub_put(sub); 714 715 /* Delete service item if no more publications and subscriptions */ 716 if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) { 717 hlist_del_init_rcu(&sc->service_list); 718 kfree_rcu(sc, rcu); 719 } 720 spin_unlock_bh(&sc->lock); 721 exit: 722 spin_unlock_bh(&tn->nametbl_lock); 723 } 724 725 int tipc_nametbl_init(struct net *net) 726 { 727 struct tipc_net *tn = tipc_net(net); 728 struct name_table *nt; 729 int i; 730 731 nt = kzalloc(sizeof(*nt), GFP_ATOMIC); 732 if (!nt) 733 return -ENOMEM; 734 735 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) 736 INIT_HLIST_HEAD(&nt->services[i]); 737 738 INIT_LIST_HEAD(&nt->node_scope); 739 INIT_LIST_HEAD(&nt->cluster_scope); 740 tn->nametbl = nt; 741 spin_lock_init(&tn->nametbl_lock); 742 return 0; 743 } 744 745 /** 746 * tipc_service_delete - purge all publications for a service and delete it 747 */ 748 static void tipc_service_delete(struct net *net, struct tipc_service *sc) 749 { 750 struct service_range *sr, *tmpr; 751 struct publication *p, *tmpb; 752 753 spin_lock_bh(&sc->lock); 754 rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) { 755 list_for_each_entry_safe(p, tmpb, 756 &sr->all_publ, all_publ) { 757 tipc_service_remove_publ(net, sc, p->lower, p->node, 758 p->port, p->key); 759 kfree_rcu(p, rcu); 760 } 761 } 762 hlist_del_init_rcu(&sc->service_list); 763 spin_unlock_bh(&sc->lock); 764 kfree_rcu(sc, rcu); 765 } 766 767 void tipc_nametbl_stop(struct net *net) 768 { 769 struct name_table *nt = tipc_name_table(net); 770 struct tipc_net *tn = tipc_net(net); 771 struct hlist_head *service_head; 772 struct tipc_service *service; 773 u32 i; 774 775 /* Verify name table is empty and purge any lingering 776 * publications, then release the name table 777 */ 778 spin_lock_bh(&tn->nametbl_lock); 779 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) { 780 if (hlist_empty(&nt->services[i])) 781 continue; 782 service_head = &nt->services[i]; 783 hlist_for_each_entry_rcu(service, service_head, service_list) { 784 tipc_service_delete(net, service); 785 } 786 } 787 spin_unlock_bh(&tn->nametbl_lock); 788 789 synchronize_net(); 790 kfree(nt); 791 } 792 793 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg, 794 struct tipc_service *service, 795 struct service_range *sr, 796 u32 *last_key) 797 { 798 struct publication *p; 799 struct nlattr *attrs; 800 struct nlattr *b; 801 void *hdr; 802 803 if (*last_key) { 804 list_for_each_entry(p, &sr->all_publ, all_publ) 805 if (p->key == *last_key) 806 break; 807 if (p->key != *last_key) 808 return -EPIPE; 809 } else { 810 p = list_first_entry(&sr->all_publ, 811 struct publication, 812 all_publ); 813 } 814 815 list_for_each_entry_from(p, &sr->all_publ, all_publ) { 816 *last_key = p->key; 817 818 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, 819 &tipc_genl_family, NLM_F_MULTI, 820 TIPC_NL_NAME_TABLE_GET); 821 if (!hdr) 822 return -EMSGSIZE; 823 824 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE); 825 if (!attrs) 826 goto msg_full; 827 828 b = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL); 829 if (!b) 830 goto attr_msg_full; 831 832 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type)) 833 goto publ_msg_full; 834 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower)) 835 goto publ_msg_full; 836 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper)) 837 goto publ_msg_full; 838 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope)) 839 goto publ_msg_full; 840 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node)) 841 goto publ_msg_full; 842 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port)) 843 goto publ_msg_full; 844 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key)) 845 goto publ_msg_full; 846 847 nla_nest_end(msg->skb, b); 848 nla_nest_end(msg->skb, attrs); 849 genlmsg_end(msg->skb, hdr); 850 } 851 *last_key = 0; 852 853 return 0; 854 855 publ_msg_full: 856 nla_nest_cancel(msg->skb, b); 857 attr_msg_full: 858 nla_nest_cancel(msg->skb, attrs); 859 msg_full: 860 genlmsg_cancel(msg->skb, hdr); 861 862 return -EMSGSIZE; 863 } 864 865 static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg, 866 struct tipc_service *sc, 867 u32 *last_lower, u32 *last_key) 868 { 869 struct service_range *sr; 870 struct rb_node *n; 871 int err; 872 873 for (n = rb_first(&sc->ranges); n; n = rb_next(n)) { 874 sr = container_of(n, struct service_range, tree_node); 875 if (sr->lower < *last_lower) 876 continue; 877 err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key); 878 if (err) { 879 *last_lower = sr->lower; 880 return err; 881 } 882 } 883 *last_lower = 0; 884 return 0; 885 } 886 887 static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg, 888 u32 *last_type, u32 *last_lower, u32 *last_key) 889 { 890 struct tipc_net *tn = tipc_net(net); 891 struct tipc_service *service = NULL; 892 struct hlist_head *head; 893 int err; 894 int i; 895 896 if (*last_type) 897 i = hash(*last_type); 898 else 899 i = 0; 900 901 for (; i < TIPC_NAMETBL_SIZE; i++) { 902 head = &tn->nametbl->services[i]; 903 904 if (*last_type) { 905 service = tipc_service_find(net, *last_type); 906 if (!service) 907 return -EPIPE; 908 } else { 909 hlist_for_each_entry_rcu(service, head, service_list) 910 break; 911 if (!service) 912 continue; 913 } 914 915 hlist_for_each_entry_from_rcu(service, service_list) { 916 spin_lock_bh(&service->lock); 917 err = __tipc_nl_service_range_list(msg, service, 918 last_lower, 919 last_key); 920 921 if (err) { 922 *last_type = service->type; 923 spin_unlock_bh(&service->lock); 924 return err; 925 } 926 spin_unlock_bh(&service->lock); 927 } 928 *last_type = 0; 929 } 930 return 0; 931 } 932 933 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb) 934 { 935 struct net *net = sock_net(skb->sk); 936 u32 last_type = cb->args[0]; 937 u32 last_lower = cb->args[1]; 938 u32 last_key = cb->args[2]; 939 int done = cb->args[3]; 940 struct tipc_nl_msg msg; 941 int err; 942 943 if (done) 944 return 0; 945 946 msg.skb = skb; 947 msg.portid = NETLINK_CB(cb->skb).portid; 948 msg.seq = cb->nlh->nlmsg_seq; 949 950 rcu_read_lock(); 951 err = tipc_nl_service_list(net, &msg, &last_type, 952 &last_lower, &last_key); 953 if (!err) { 954 done = 1; 955 } else if (err != -EMSGSIZE) { 956 /* We never set seq or call nl_dump_check_consistent() this 957 * means that setting prev_seq here will cause the consistence 958 * check to fail in the netlink callback handler. Resulting in 959 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if 960 * we got an error. 961 */ 962 cb->prev_seq = 1; 963 } 964 rcu_read_unlock(); 965 966 cb->args[0] = last_type; 967 cb->args[1] = last_lower; 968 cb->args[2] = last_key; 969 cb->args[3] = done; 970 971 return skb->len; 972 } 973 974 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port) 975 { 976 u64 value = (u64)node << 32 | port; 977 struct tipc_dest *dst; 978 979 list_for_each_entry(dst, l, list) { 980 if (dst->value != value) 981 continue; 982 return dst; 983 } 984 return NULL; 985 } 986 987 bool tipc_dest_push(struct list_head *l, u32 node, u32 port) 988 { 989 u64 value = (u64)node << 32 | port; 990 struct tipc_dest *dst; 991 992 if (tipc_dest_find(l, node, port)) 993 return false; 994 995 dst = kmalloc(sizeof(*dst), GFP_ATOMIC); 996 if (unlikely(!dst)) 997 return false; 998 dst->value = value; 999 list_add(&dst->list, l); 1000 return true; 1001 } 1002 1003 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port) 1004 { 1005 struct tipc_dest *dst; 1006 1007 if (list_empty(l)) 1008 return false; 1009 dst = list_first_entry(l, typeof(*dst), list); 1010 if (port) 1011 *port = dst->port; 1012 if (node) 1013 *node = dst->node; 1014 list_del(&dst->list); 1015 kfree(dst); 1016 return true; 1017 } 1018 1019 bool tipc_dest_del(struct list_head *l, u32 node, u32 port) 1020 { 1021 struct tipc_dest *dst; 1022 1023 dst = tipc_dest_find(l, node, port); 1024 if (!dst) 1025 return false; 1026 list_del(&dst->list); 1027 kfree(dst); 1028 return true; 1029 } 1030 1031 void tipc_dest_list_purge(struct list_head *l) 1032 { 1033 struct tipc_dest *dst, *tmp; 1034 1035 list_for_each_entry_safe(dst, tmp, l, list) { 1036 list_del(&dst->list); 1037 kfree(dst); 1038 } 1039 } 1040 1041 int tipc_dest_list_len(struct list_head *l) 1042 { 1043 struct tipc_dest *dst; 1044 int i = 0; 1045 1046 list_for_each_entry(dst, l, list) { 1047 i++; 1048 } 1049 return i; 1050 } 1051