/net/tipc/name_table.c

http://github.com/mirrors/linux · C · 1171 lines · 852 code · 139 blank · 180 comment · 153 complexity · 836f04d29695ae67a4f10a6b3c9309e8 MD5 · raw file

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