name_table.c 30 KB

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