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