1 /* 2 * net/tipc/name_table.c: TIPC name table code 3 * 4 * Copyright (c) 2000-2006, 2014, 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 name_seq *nseq, 231 u32 type, u32 lower, u32 upper, 232 u32 scope, u32 node, u32 port, u32 key) 233 { 234 struct tipc_subscription *s; 235 struct tipc_subscription *st; 236 struct publication *publ; 237 struct sub_seq *sseq; 238 struct name_info *info; 239 int created_subseq = 0; 240 241 sseq = nameseq_find_subseq(nseq, lower); 242 if (sseq) { 243 244 /* Lower end overlaps existing entry => need an exact match */ 245 if ((sseq->lower != lower) || (sseq->upper != upper)) { 246 return NULL; 247 } 248 249 info = sseq->info; 250 251 /* Check if an identical publication already exists */ 252 list_for_each_entry(publ, &info->zone_list, zone_list) { 253 if ((publ->ref == port) && (publ->key == key) && 254 (!publ->node || (publ->node == node))) 255 return NULL; 256 } 257 } else { 258 u32 inspos; 259 struct sub_seq *freesseq; 260 261 /* Find where lower end should be inserted */ 262 inspos = nameseq_locate_subseq(nseq, lower); 263 264 /* Fail if upper end overlaps into an existing entry */ 265 if ((inspos < nseq->first_free) && 266 (upper >= nseq->sseqs[inspos].lower)) { 267 return NULL; 268 } 269 270 /* Ensure there is space for new sub-sequence */ 271 if (nseq->first_free == nseq->alloc) { 272 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2); 273 274 if (!sseqs) { 275 pr_warn("Cannot publish {%u,%u,%u}, no memory\n", 276 type, lower, upper); 277 return NULL; 278 } 279 memcpy(sseqs, nseq->sseqs, 280 nseq->alloc * sizeof(struct sub_seq)); 281 kfree(nseq->sseqs); 282 nseq->sseqs = sseqs; 283 nseq->alloc *= 2; 284 } 285 286 info = kzalloc(sizeof(*info), GFP_ATOMIC); 287 if (!info) { 288 pr_warn("Cannot publish {%u,%u,%u}, no memory\n", 289 type, lower, upper); 290 return NULL; 291 } 292 293 INIT_LIST_HEAD(&info->node_list); 294 INIT_LIST_HEAD(&info->cluster_list); 295 INIT_LIST_HEAD(&info->zone_list); 296 297 /* Insert new sub-sequence */ 298 sseq = &nseq->sseqs[inspos]; 299 freesseq = &nseq->sseqs[nseq->first_free]; 300 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq)); 301 memset(sseq, 0, sizeof(*sseq)); 302 nseq->first_free++; 303 sseq->lower = lower; 304 sseq->upper = upper; 305 sseq->info = info; 306 created_subseq = 1; 307 } 308 309 /* Insert a publication */ 310 publ = publ_create(type, lower, upper, scope, node, port, key); 311 if (!publ) 312 return NULL; 313 314 list_add(&publ->zone_list, &info->zone_list); 315 info->zone_list_size++; 316 317 if (in_own_cluster(node)) { 318 list_add(&publ->cluster_list, &info->cluster_list); 319 info->cluster_list_size++; 320 } 321 322 if (in_own_node(node)) { 323 list_add(&publ->node_list, &info->node_list); 324 info->node_list_size++; 325 } 326 327 /* Any subscriptions waiting for notification? */ 328 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) { 329 tipc_subscr_report_overlap(s, 330 publ->lower, 331 publ->upper, 332 TIPC_PUBLISHED, 333 publ->ref, 334 publ->node, 335 created_subseq); 336 } 337 return publ; 338 } 339 340 /** 341 * tipc_nameseq_remove_publ 342 * 343 * NOTE: There may be cases where TIPC is asked to remove a publication 344 * that is not in the name table. For example, if another node issues a 345 * publication for a name sequence that overlaps an existing name sequence 346 * the publication will not be recorded, which means the publication won't 347 * be found when the name sequence is later withdrawn by that node. 348 * A failed withdraw request simply returns a failure indication and lets the 349 * caller issue any error or warning messages associated with such a problem. 350 */ 351 static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst, 352 u32 node, u32 ref, u32 key) 353 { 354 struct publication *publ; 355 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst); 356 struct name_info *info; 357 struct sub_seq *free; 358 struct tipc_subscription *s, *st; 359 int removed_subseq = 0; 360 361 if (!sseq) 362 return NULL; 363 364 info = sseq->info; 365 366 /* Locate publication, if it exists */ 367 list_for_each_entry(publ, &info->zone_list, zone_list) { 368 if ((publ->key == key) && (publ->ref == ref) && 369 (!publ->node || (publ->node == node))) 370 goto found; 371 } 372 return NULL; 373 374 found: 375 /* Remove publication from zone scope list */ 376 list_del(&publ->zone_list); 377 info->zone_list_size--; 378 379 /* Remove publication from cluster scope list, if present */ 380 if (in_own_cluster(node)) { 381 list_del(&publ->cluster_list); 382 info->cluster_list_size--; 383 } 384 385 /* Remove publication from node scope list, if present */ 386 if (in_own_node(node)) { 387 list_del(&publ->node_list); 388 info->node_list_size--; 389 } 390 391 /* Contract subseq list if no more publications for that subseq */ 392 if (list_empty(&info->zone_list)) { 393 kfree(info); 394 free = &nseq->sseqs[nseq->first_free--]; 395 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq)); 396 removed_subseq = 1; 397 } 398 399 /* Notify any waiting subscriptions */ 400 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) { 401 tipc_subscr_report_overlap(s, 402 publ->lower, 403 publ->upper, 404 TIPC_WITHDRAWN, 405 publ->ref, 406 publ->node, 407 removed_subseq); 408 } 409 410 return publ; 411 } 412 413 /** 414 * tipc_nameseq_subscribe - attach a subscription, and issue 415 * the prescribed number of events if there is any sub- 416 * sequence overlapping with the requested sequence 417 */ 418 static void tipc_nameseq_subscribe(struct name_seq *nseq, 419 struct tipc_subscription *s) 420 { 421 struct sub_seq *sseq = nseq->sseqs; 422 423 list_add(&s->nameseq_list, &nseq->subscriptions); 424 425 if (!sseq) 426 return; 427 428 while (sseq != &nseq->sseqs[nseq->first_free]) { 429 if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) { 430 struct publication *crs; 431 struct name_info *info = sseq->info; 432 int must_report = 1; 433 434 list_for_each_entry(crs, &info->zone_list, zone_list) { 435 tipc_subscr_report_overlap(s, 436 sseq->lower, 437 sseq->upper, 438 TIPC_PUBLISHED, 439 crs->ref, 440 crs->node, 441 must_report); 442 must_report = 0; 443 } 444 } 445 sseq++; 446 } 447 } 448 449 static struct name_seq *nametbl_find_seq(struct net *net, u32 type) 450 { 451 struct tipc_net *tn = net_generic(net, tipc_net_id); 452 struct hlist_head *seq_head; 453 struct name_seq *ns; 454 455 seq_head = &tn->nametbl->seq_hlist[hash(type)]; 456 hlist_for_each_entry_rcu(ns, seq_head, ns_list) { 457 if (ns->type == type) 458 return ns; 459 } 460 461 return NULL; 462 }; 463 464 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type, 465 u32 lower, u32 upper, u32 scope, 466 u32 node, u32 port, u32 key) 467 { 468 struct tipc_net *tn = net_generic(net, tipc_net_id); 469 struct publication *publ; 470 struct name_seq *seq = nametbl_find_seq(net, type); 471 int index = hash(type); 472 473 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) || 474 (lower > upper)) { 475 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n", 476 type, lower, upper, scope); 477 return NULL; 478 } 479 480 if (!seq) 481 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]); 482 if (!seq) 483 return NULL; 484 485 spin_lock_bh(&seq->lock); 486 publ = tipc_nameseq_insert_publ(seq, type, lower, upper, 487 scope, node, port, key); 488 spin_unlock_bh(&seq->lock); 489 return publ; 490 } 491 492 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type, 493 u32 lower, u32 node, u32 ref, 494 u32 key) 495 { 496 struct publication *publ; 497 struct name_seq *seq = nametbl_find_seq(net, type); 498 499 if (!seq) 500 return NULL; 501 502 spin_lock_bh(&seq->lock); 503 publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key); 504 if (!seq->first_free && list_empty(&seq->subscriptions)) { 505 hlist_del_init_rcu(&seq->ns_list); 506 kfree(seq->sseqs); 507 spin_unlock_bh(&seq->lock); 508 kfree_rcu(seq, rcu); 509 return publ; 510 } 511 spin_unlock_bh(&seq->lock); 512 return publ; 513 } 514 515 /** 516 * tipc_nametbl_translate - perform name translation 517 * 518 * On entry, 'destnode' is the search domain used during translation. 519 * 520 * On exit: 521 * - if name translation is deferred to another node/cluster/zone, 522 * leaves 'destnode' unchanged (will be non-zero) and returns 0 523 * - if name translation is attempted and succeeds, sets 'destnode' 524 * to publishing node and returns port reference (will be non-zero) 525 * - if name translation is attempted and fails, sets 'destnode' to 0 526 * and returns 0 527 */ 528 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance, 529 u32 *destnode) 530 { 531 struct sub_seq *sseq; 532 struct name_info *info; 533 struct publication *publ; 534 struct name_seq *seq; 535 u32 ref = 0; 536 u32 node = 0; 537 538 if (!tipc_in_scope(*destnode, tipc_own_addr)) 539 return 0; 540 541 rcu_read_lock(); 542 seq = nametbl_find_seq(net, type); 543 if (unlikely(!seq)) 544 goto not_found; 545 spin_lock_bh(&seq->lock); 546 sseq = nameseq_find_subseq(seq, instance); 547 if (unlikely(!sseq)) 548 goto no_match; 549 info = sseq->info; 550 551 /* Closest-First Algorithm */ 552 if (likely(!*destnode)) { 553 if (!list_empty(&info->node_list)) { 554 publ = list_first_entry(&info->node_list, 555 struct publication, 556 node_list); 557 list_move_tail(&publ->node_list, 558 &info->node_list); 559 } else if (!list_empty(&info->cluster_list)) { 560 publ = list_first_entry(&info->cluster_list, 561 struct publication, 562 cluster_list); 563 list_move_tail(&publ->cluster_list, 564 &info->cluster_list); 565 } else { 566 publ = list_first_entry(&info->zone_list, 567 struct publication, 568 zone_list); 569 list_move_tail(&publ->zone_list, 570 &info->zone_list); 571 } 572 } 573 574 /* Round-Robin Algorithm */ 575 else if (*destnode == tipc_own_addr) { 576 if (list_empty(&info->node_list)) 577 goto no_match; 578 publ = list_first_entry(&info->node_list, struct publication, 579 node_list); 580 list_move_tail(&publ->node_list, &info->node_list); 581 } else if (in_own_cluster_exact(*destnode)) { 582 if (list_empty(&info->cluster_list)) 583 goto no_match; 584 publ = list_first_entry(&info->cluster_list, struct publication, 585 cluster_list); 586 list_move_tail(&publ->cluster_list, &info->cluster_list); 587 } else { 588 publ = list_first_entry(&info->zone_list, struct publication, 589 zone_list); 590 list_move_tail(&publ->zone_list, &info->zone_list); 591 } 592 593 ref = publ->ref; 594 node = publ->node; 595 no_match: 596 spin_unlock_bh(&seq->lock); 597 not_found: 598 rcu_read_unlock(); 599 *destnode = node; 600 return ref; 601 } 602 603 /** 604 * tipc_nametbl_mc_translate - find multicast destinations 605 * 606 * Creates list of all local ports that overlap the given multicast address; 607 * also determines if any off-node ports overlap. 608 * 609 * Note: Publications with a scope narrower than 'limit' are ignored. 610 * (i.e. local node-scope publications mustn't receive messages arriving 611 * from another node, even if the multcast link brought it here) 612 * 613 * Returns non-zero if any off-node ports overlap 614 */ 615 int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper, 616 u32 limit, struct tipc_port_list *dports) 617 { 618 struct name_seq *seq; 619 struct sub_seq *sseq; 620 struct sub_seq *sseq_stop; 621 struct name_info *info; 622 int res = 0; 623 624 rcu_read_lock(); 625 seq = nametbl_find_seq(net, type); 626 if (!seq) 627 goto exit; 628 629 spin_lock_bh(&seq->lock); 630 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower); 631 sseq_stop = seq->sseqs + seq->first_free; 632 for (; sseq != sseq_stop; sseq++) { 633 struct publication *publ; 634 635 if (sseq->lower > upper) 636 break; 637 638 info = sseq->info; 639 list_for_each_entry(publ, &info->node_list, node_list) { 640 if (publ->scope <= limit) 641 tipc_port_list_add(dports, publ->ref); 642 } 643 644 if (info->cluster_list_size != info->node_list_size) 645 res = 1; 646 } 647 spin_unlock_bh(&seq->lock); 648 exit: 649 rcu_read_unlock(); 650 return res; 651 } 652 653 /* 654 * tipc_nametbl_publish - add name publication to network name tables 655 */ 656 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower, 657 u32 upper, u32 scope, u32 port_ref, 658 u32 key) 659 { 660 struct publication *publ; 661 struct sk_buff *buf = NULL; 662 struct tipc_net *tn = net_generic(net, tipc_net_id); 663 664 spin_lock_bh(&tn->nametbl_lock); 665 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) { 666 pr_warn("Publication failed, local publication limit reached (%u)\n", 667 TIPC_MAX_PUBLICATIONS); 668 spin_unlock_bh(&tn->nametbl_lock); 669 return NULL; 670 } 671 672 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope, 673 tipc_own_addr, port_ref, key); 674 if (likely(publ)) { 675 tn->nametbl->local_publ_count++; 676 buf = tipc_named_publish(net, publ); 677 /* Any pending external events? */ 678 tipc_named_process_backlog(net); 679 } 680 spin_unlock_bh(&tn->nametbl_lock); 681 682 if (buf) 683 named_cluster_distribute(net, buf); 684 return publ; 685 } 686 687 /** 688 * tipc_nametbl_withdraw - withdraw name publication from network name tables 689 */ 690 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref, 691 u32 key) 692 { 693 struct publication *publ; 694 struct sk_buff *skb = NULL; 695 struct tipc_net *tn = net_generic(net, tipc_net_id); 696 697 spin_lock_bh(&tn->nametbl_lock); 698 publ = tipc_nametbl_remove_publ(net, type, lower, tipc_own_addr, 699 ref, key); 700 if (likely(publ)) { 701 tn->nametbl->local_publ_count--; 702 skb = tipc_named_withdraw(publ); 703 /* Any pending external events? */ 704 tipc_named_process_backlog(net); 705 list_del_init(&publ->pport_list); 706 kfree_rcu(publ, rcu); 707 } else { 708 pr_err("Unable to remove local publication\n" 709 "(type=%u, lower=%u, ref=%u, key=%u)\n", 710 type, lower, ref, key); 711 } 712 spin_unlock_bh(&tn->nametbl_lock); 713 714 if (skb) { 715 named_cluster_distribute(net, skb); 716 return 1; 717 } 718 return 0; 719 } 720 721 /** 722 * tipc_nametbl_subscribe - add a subscription object to the name table 723 */ 724 void tipc_nametbl_subscribe(struct tipc_subscription *s) 725 { 726 struct tipc_net *tn = net_generic(s->net, tipc_net_id); 727 u32 type = s->seq.type; 728 int index = hash(type); 729 struct name_seq *seq; 730 731 spin_lock_bh(&tn->nametbl_lock); 732 seq = nametbl_find_seq(s->net, type); 733 if (!seq) 734 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]); 735 if (seq) { 736 spin_lock_bh(&seq->lock); 737 tipc_nameseq_subscribe(seq, s); 738 spin_unlock_bh(&seq->lock); 739 } else { 740 pr_warn("Failed to create subscription for {%u,%u,%u}\n", 741 s->seq.type, s->seq.lower, s->seq.upper); 742 } 743 spin_unlock_bh(&tn->nametbl_lock); 744 } 745 746 /** 747 * tipc_nametbl_unsubscribe - remove a subscription object from name table 748 */ 749 void tipc_nametbl_unsubscribe(struct tipc_subscription *s) 750 { 751 struct tipc_net *tn = net_generic(s->net, tipc_net_id); 752 struct name_seq *seq; 753 754 spin_lock_bh(&tn->nametbl_lock); 755 seq = nametbl_find_seq(s->net, s->seq.type); 756 if (seq != NULL) { 757 spin_lock_bh(&seq->lock); 758 list_del_init(&s->nameseq_list); 759 if (!seq->first_free && list_empty(&seq->subscriptions)) { 760 hlist_del_init_rcu(&seq->ns_list); 761 kfree(seq->sseqs); 762 spin_unlock_bh(&seq->lock); 763 kfree_rcu(seq, rcu); 764 } else { 765 spin_unlock_bh(&seq->lock); 766 } 767 } 768 spin_unlock_bh(&tn->nametbl_lock); 769 } 770 771 /** 772 * subseq_list - print specified sub-sequence contents into the given buffer 773 */ 774 static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth, 775 u32 index) 776 { 777 char portIdStr[27]; 778 const char *scope_str[] = {"", " zone", " cluster", " node"}; 779 struct publication *publ; 780 struct name_info *info; 781 int ret; 782 783 ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper); 784 785 if (depth == 2) { 786 ret += tipc_snprintf(buf - ret, len + ret, "\n"); 787 return ret; 788 } 789 790 info = sseq->info; 791 792 list_for_each_entry(publ, &info->zone_list, zone_list) { 793 sprintf(portIdStr, "<%u.%u.%u:%u>", 794 tipc_zone(publ->node), tipc_cluster(publ->node), 795 tipc_node(publ->node), publ->ref); 796 ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr); 797 if (depth > 3) { 798 ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s", 799 publ->key, scope_str[publ->scope]); 800 } 801 if (!list_is_last(&publ->zone_list, &info->zone_list)) 802 ret += tipc_snprintf(buf + ret, len - ret, 803 "\n%33s", " "); 804 } 805 806 ret += tipc_snprintf(buf + ret, len - ret, "\n"); 807 return ret; 808 } 809 810 /** 811 * nameseq_list - print specified name sequence contents into the given buffer 812 */ 813 static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth, 814 u32 type, u32 lowbound, u32 upbound, u32 index) 815 { 816 struct sub_seq *sseq; 817 char typearea[11]; 818 int ret = 0; 819 820 if (seq->first_free == 0) 821 return 0; 822 823 sprintf(typearea, "%-10u", seq->type); 824 825 if (depth == 1) { 826 ret += tipc_snprintf(buf, len, "%s\n", typearea); 827 return ret; 828 } 829 830 for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) { 831 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) { 832 ret += tipc_snprintf(buf + ret, len - ret, "%s ", 833 typearea); 834 spin_lock_bh(&seq->lock); 835 ret += subseq_list(sseq, buf + ret, len - ret, 836 depth, index); 837 spin_unlock_bh(&seq->lock); 838 sprintf(typearea, "%10s", " "); 839 } 840 } 841 return ret; 842 } 843 844 /** 845 * nametbl_header - print name table header into the given buffer 846 */ 847 static int nametbl_header(char *buf, int len, u32 depth) 848 { 849 const char *header[] = { 850 "Type ", 851 "Lower Upper ", 852 "Port Identity ", 853 "Publication Scope" 854 }; 855 856 int i; 857 int ret = 0; 858 859 if (depth > 4) 860 depth = 4; 861 for (i = 0; i < depth; i++) 862 ret += tipc_snprintf(buf + ret, len - ret, header[i]); 863 ret += tipc_snprintf(buf + ret, len - ret, "\n"); 864 return ret; 865 } 866 867 /** 868 * nametbl_list - print specified name table contents into the given buffer 869 */ 870 static int nametbl_list(struct net *net, char *buf, int len, u32 depth_info, 871 u32 type, u32 lowbound, u32 upbound) 872 { 873 struct tipc_net *tn = net_generic(net, tipc_net_id); 874 struct hlist_head *seq_head; 875 struct name_seq *seq; 876 int all_types; 877 int ret = 0; 878 u32 depth; 879 u32 i; 880 881 all_types = (depth_info & TIPC_NTQ_ALLTYPES); 882 depth = (depth_info & ~TIPC_NTQ_ALLTYPES); 883 884 if (depth == 0) 885 return 0; 886 887 if (all_types) { 888 /* display all entries in name table to specified depth */ 889 ret += nametbl_header(buf, len, depth); 890 lowbound = 0; 891 upbound = ~0; 892 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) { 893 seq_head = &tn->nametbl->seq_hlist[i]; 894 hlist_for_each_entry_rcu(seq, seq_head, ns_list) { 895 ret += nameseq_list(seq, buf + ret, len - ret, 896 depth, seq->type, 897 lowbound, upbound, i); 898 } 899 } 900 } else { 901 /* display only the sequence that matches the specified type */ 902 if (upbound < lowbound) { 903 ret += tipc_snprintf(buf + ret, len - ret, 904 "invalid name sequence specified\n"); 905 return ret; 906 } 907 ret += nametbl_header(buf + ret, len - ret, depth); 908 i = hash(type); 909 seq_head = &tn->nametbl->seq_hlist[i]; 910 hlist_for_each_entry_rcu(seq, seq_head, ns_list) { 911 if (seq->type == type) { 912 ret += nameseq_list(seq, buf + ret, len - ret, 913 depth, type, 914 lowbound, upbound, i); 915 break; 916 } 917 } 918 } 919 return ret; 920 } 921 922 struct sk_buff *tipc_nametbl_get(struct net *net, const void *req_tlv_area, 923 int req_tlv_space) 924 { 925 struct sk_buff *buf; 926 struct tipc_name_table_query *argv; 927 struct tlv_desc *rep_tlv; 928 char *pb; 929 int pb_len; 930 int str_len; 931 932 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY)) 933 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); 934 935 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN)); 936 if (!buf) 937 return NULL; 938 939 rep_tlv = (struct tlv_desc *)buf->data; 940 pb = TLV_DATA(rep_tlv); 941 pb_len = ULTRA_STRING_MAX_LEN; 942 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area); 943 rcu_read_lock(); 944 str_len = nametbl_list(net, pb, pb_len, ntohl(argv->depth), 945 ntohl(argv->type), 946 ntohl(argv->lowbound), ntohl(argv->upbound)); 947 rcu_read_unlock(); 948 str_len += 1; /* for "\0" */ 949 skb_put(buf, TLV_SPACE(str_len)); 950 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); 951 952 return buf; 953 } 954 955 int tipc_nametbl_init(struct net *net) 956 { 957 struct tipc_net *tn = net_generic(net, tipc_net_id); 958 struct name_table *tipc_nametbl; 959 int i; 960 961 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC); 962 if (!tipc_nametbl) 963 return -ENOMEM; 964 965 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) 966 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]); 967 968 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]); 969 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]); 970 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]); 971 tn->nametbl = tipc_nametbl; 972 spin_lock_init(&tn->nametbl_lock); 973 return 0; 974 } 975 976 /** 977 * tipc_purge_publications - remove all publications for a given type 978 * 979 * tipc_nametbl_lock must be held when calling this function 980 */ 981 static void tipc_purge_publications(struct net *net, struct name_seq *seq) 982 { 983 struct publication *publ, *safe; 984 struct sub_seq *sseq; 985 struct name_info *info; 986 987 spin_lock_bh(&seq->lock); 988 sseq = seq->sseqs; 989 info = sseq->info; 990 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) { 991 tipc_nametbl_remove_publ(net, publ->type, publ->lower, 992 publ->node, publ->ref, publ->key); 993 kfree_rcu(publ, rcu); 994 } 995 hlist_del_init_rcu(&seq->ns_list); 996 kfree(seq->sseqs); 997 spin_unlock_bh(&seq->lock); 998 999 kfree_rcu(seq, rcu); 1000 } 1001 1002 void tipc_nametbl_stop(struct net *net) 1003 { 1004 u32 i; 1005 struct name_seq *seq; 1006 struct hlist_head *seq_head; 1007 struct tipc_net *tn = net_generic(net, tipc_net_id); 1008 struct name_table *tipc_nametbl = tn->nametbl; 1009 1010 /* Verify name table is empty and purge any lingering 1011 * publications, then release the name table 1012 */ 1013 spin_lock_bh(&tn->nametbl_lock); 1014 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) { 1015 if (hlist_empty(&tipc_nametbl->seq_hlist[i])) 1016 continue; 1017 seq_head = &tipc_nametbl->seq_hlist[i]; 1018 hlist_for_each_entry_rcu(seq, seq_head, ns_list) { 1019 tipc_purge_publications(net, seq); 1020 } 1021 } 1022 spin_unlock_bh(&tn->nametbl_lock); 1023 1024 synchronize_net(); 1025 kfree(tipc_nametbl); 1026 1027 } 1028 1029 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg, 1030 struct name_seq *seq, 1031 struct sub_seq *sseq, u32 *last_publ) 1032 { 1033 void *hdr; 1034 struct nlattr *attrs; 1035 struct nlattr *publ; 1036 struct publication *p; 1037 1038 if (*last_publ) { 1039 list_for_each_entry(p, &sseq->info->zone_list, zone_list) 1040 if (p->key == *last_publ) 1041 break; 1042 if (p->key != *last_publ) 1043 return -EPIPE; 1044 } else { 1045 p = list_first_entry(&sseq->info->zone_list, struct publication, 1046 zone_list); 1047 } 1048 1049 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) { 1050 *last_publ = p->key; 1051 1052 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, 1053 &tipc_genl_v2_family, NLM_F_MULTI, 1054 TIPC_NL_NAME_TABLE_GET); 1055 if (!hdr) 1056 return -EMSGSIZE; 1057 1058 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE); 1059 if (!attrs) 1060 goto msg_full; 1061 1062 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL); 1063 if (!publ) 1064 goto attr_msg_full; 1065 1066 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type)) 1067 goto publ_msg_full; 1068 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower)) 1069 goto publ_msg_full; 1070 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper)) 1071 goto publ_msg_full; 1072 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope)) 1073 goto publ_msg_full; 1074 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node)) 1075 goto publ_msg_full; 1076 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref)) 1077 goto publ_msg_full; 1078 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key)) 1079 goto publ_msg_full; 1080 1081 nla_nest_end(msg->skb, publ); 1082 nla_nest_end(msg->skb, attrs); 1083 genlmsg_end(msg->skb, hdr); 1084 } 1085 *last_publ = 0; 1086 1087 return 0; 1088 1089 publ_msg_full: 1090 nla_nest_cancel(msg->skb, publ); 1091 attr_msg_full: 1092 nla_nest_cancel(msg->skb, attrs); 1093 msg_full: 1094 genlmsg_cancel(msg->skb, hdr); 1095 1096 return -EMSGSIZE; 1097 } 1098 1099 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq, 1100 u32 *last_lower, u32 *last_publ) 1101 { 1102 struct sub_seq *sseq; 1103 struct sub_seq *sseq_start; 1104 int err; 1105 1106 if (*last_lower) { 1107 sseq_start = nameseq_find_subseq(seq, *last_lower); 1108 if (!sseq_start) 1109 return -EPIPE; 1110 } else { 1111 sseq_start = seq->sseqs; 1112 } 1113 1114 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) { 1115 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ); 1116 if (err) { 1117 *last_lower = sseq->lower; 1118 return err; 1119 } 1120 } 1121 *last_lower = 0; 1122 1123 return 0; 1124 } 1125 1126 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg, 1127 u32 *last_type, u32 *last_lower, u32 *last_publ) 1128 { 1129 struct tipc_net *tn = net_generic(net, tipc_net_id); 1130 struct hlist_head *seq_head; 1131 struct name_seq *seq = NULL; 1132 int err; 1133 int i; 1134 1135 if (*last_type) 1136 i = hash(*last_type); 1137 else 1138 i = 0; 1139 1140 for (; i < TIPC_NAMETBL_SIZE; i++) { 1141 seq_head = &tn->nametbl->seq_hlist[i]; 1142 1143 if (*last_type) { 1144 seq = nametbl_find_seq(net, *last_type); 1145 if (!seq) 1146 return -EPIPE; 1147 } else { 1148 hlist_for_each_entry_rcu(seq, seq_head, ns_list) 1149 break; 1150 if (!seq) 1151 continue; 1152 } 1153 1154 hlist_for_each_entry_from_rcu(seq, ns_list) { 1155 spin_lock_bh(&seq->lock); 1156 err = __tipc_nl_subseq_list(msg, seq, last_lower, 1157 last_publ); 1158 1159 if (err) { 1160 *last_type = seq->type; 1161 spin_unlock_bh(&seq->lock); 1162 return err; 1163 } 1164 spin_unlock_bh(&seq->lock); 1165 } 1166 *last_type = 0; 1167 } 1168 return 0; 1169 } 1170 1171 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb) 1172 { 1173 int err; 1174 int done = cb->args[3]; 1175 u32 last_type = cb->args[0]; 1176 u32 last_lower = cb->args[1]; 1177 u32 last_publ = cb->args[2]; 1178 struct net *net = sock_net(skb->sk); 1179 struct tipc_nl_msg msg; 1180 1181 if (done) 1182 return 0; 1183 1184 msg.skb = skb; 1185 msg.portid = NETLINK_CB(cb->skb).portid; 1186 msg.seq = cb->nlh->nlmsg_seq; 1187 1188 rcu_read_lock(); 1189 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ); 1190 if (!err) { 1191 done = 1; 1192 } else if (err != -EMSGSIZE) { 1193 /* We never set seq or call nl_dump_check_consistent() this 1194 * means that setting prev_seq here will cause the consistence 1195 * check to fail in the netlink callback handler. Resulting in 1196 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if 1197 * we got an error. 1198 */ 1199 cb->prev_seq = 1; 1200 } 1201 rcu_read_unlock(); 1202 1203 cb->args[0] = last_type; 1204 cb->args[1] = last_lower; 1205 cb->args[2] = last_publ; 1206 cb->args[3] = done; 1207 1208 return skb->len; 1209 } 1210