name_table.c 32 KB

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