name_table.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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(sizeof(*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(sizeof(*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(sizeof(*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.node || _p->sk.node == node)) {
  317. pr_debug("Failed to bind duplicate %u,%u,%u/%u:%u/%u\n",
  318. p->sr.type, p->sr.lower, p->sr.upper,
  319. node, p->sk.ref, key);
  320. goto exit;
  321. }
  322. }
  323. if (in_own_node(net, p->sk.node))
  324. list_add(&p->local_publ, &sr->local_publ);
  325. list_add(&p->all_publ, &sr->all_publ);
  326. p->id = sc->publ_cnt++;
  327. /* Any subscriptions waiting for notification? */
  328. list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
  329. tipc_sub_report_overlap(sub, p, TIPC_PUBLISHED, first);
  330. }
  331. res = true;
  332. exit:
  333. if (!res)
  334. pr_warn("Failed to bind to %u,%u,%u\n",
  335. p->sr.type, p->sr.lower, p->sr.upper);
  336. spin_unlock_bh(&sc->lock);
  337. return res;
  338. }
  339. /**
  340. * tipc_service_remove_publ - remove a publication from a service
  341. * @r: service_range to remove publication from
  342. * @sk: address publishing socket
  343. * @key: target publication key
  344. */
  345. static struct publication *tipc_service_remove_publ(struct service_range *r,
  346. struct tipc_socket_addr *sk,
  347. u32 key)
  348. {
  349. struct publication *p;
  350. u32 node = sk->node;
  351. list_for_each_entry(p, &r->all_publ, all_publ) {
  352. if (p->key != key || (node && node != p->sk.node))
  353. continue;
  354. list_del(&p->all_publ);
  355. list_del(&p->local_publ);
  356. return p;
  357. }
  358. return NULL;
  359. }
  360. /*
  361. * Code reused: time_after32() for the same purpose
  362. */
  363. #define publication_after(pa, pb) time_after32((pa)->id, (pb)->id)
  364. static int tipc_publ_sort(void *priv, const struct list_head *a,
  365. const struct list_head *b)
  366. {
  367. struct publication *pa, *pb;
  368. pa = container_of(a, struct publication, list);
  369. pb = container_of(b, struct publication, list);
  370. return publication_after(pa, pb);
  371. }
  372. /**
  373. * tipc_service_subscribe - attach a subscription, and optionally
  374. * issue the prescribed number of events if there is any service
  375. * range overlapping with the requested range
  376. * @service: the tipc_service to attach the @sub to
  377. * @sub: the subscription to attach
  378. */
  379. static void tipc_service_subscribe(struct tipc_service *service,
  380. struct tipc_subscription *sub)
  381. {
  382. struct publication *p, *first, *tmp;
  383. struct list_head publ_list;
  384. struct service_range *sr;
  385. u32 filter, lower, upper;
  386. filter = sub->s.filter;
  387. lower = sub->s.seq.lower;
  388. upper = sub->s.seq.upper;
  389. tipc_sub_get(sub);
  390. list_add(&sub->service_list, &service->subscriptions);
  391. if (filter & TIPC_SUB_NO_STATUS)
  392. return;
  393. INIT_LIST_HEAD(&publ_list);
  394. service_range_foreach_match(sr, service, lower, upper) {
  395. first = NULL;
  396. list_for_each_entry(p, &sr->all_publ, all_publ) {
  397. if (filter & TIPC_SUB_PORTS)
  398. list_add_tail(&p->list, &publ_list);
  399. else if (!first || publication_after(first, p))
  400. /* Pick this range's *first* publication */
  401. first = p;
  402. }
  403. if (first)
  404. list_add_tail(&first->list, &publ_list);
  405. }
  406. /* Sort the publications before reporting */
  407. list_sort(NULL, &publ_list, tipc_publ_sort);
  408. list_for_each_entry_safe(p, tmp, &publ_list, list) {
  409. tipc_sub_report_overlap(sub, p, TIPC_PUBLISHED, true);
  410. list_del_init(&p->list);
  411. }
  412. }
  413. static struct tipc_service *tipc_service_find(struct net *net,
  414. struct tipc_uaddr *ua)
  415. {
  416. struct name_table *nt = tipc_name_table(net);
  417. struct hlist_head *service_head;
  418. struct tipc_service *service;
  419. service_head = &nt->services[hash(ua->sr.type)];
  420. hlist_for_each_entry_rcu(service, service_head, service_list) {
  421. if (service->type == ua->sr.type)
  422. return service;
  423. }
  424. return NULL;
  425. };
  426. struct publication *tipc_nametbl_insert_publ(struct net *net,
  427. struct tipc_uaddr *ua,
  428. struct tipc_socket_addr *sk,
  429. u32 key)
  430. {
  431. struct tipc_service *sc;
  432. struct publication *p;
  433. p = tipc_publ_create(ua, sk, key);
  434. if (!p)
  435. return NULL;
  436. sc = tipc_service_find(net, ua);
  437. if (!sc)
  438. sc = tipc_service_create(net, ua);
  439. if (sc && tipc_service_insert_publ(net, sc, p))
  440. return p;
  441. kfree(p);
  442. return NULL;
  443. }
  444. struct publication *tipc_nametbl_remove_publ(struct net *net,
  445. struct tipc_uaddr *ua,
  446. struct tipc_socket_addr *sk,
  447. u32 key)
  448. {
  449. struct tipc_subscription *sub, *tmp;
  450. struct publication *p = NULL;
  451. struct service_range *sr;
  452. struct tipc_service *sc;
  453. bool last;
  454. sc = tipc_service_find(net, ua);
  455. if (!sc)
  456. goto exit;
  457. spin_lock_bh(&sc->lock);
  458. sr = tipc_service_find_range(sc, ua);
  459. if (!sr)
  460. goto unlock;
  461. p = tipc_service_remove_publ(sr, sk, key);
  462. if (!p)
  463. goto unlock;
  464. /* Notify any waiting subscriptions */
  465. last = list_empty(&sr->all_publ);
  466. list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) {
  467. tipc_sub_report_overlap(sub, p, TIPC_WITHDRAWN, last);
  468. }
  469. /* Remove service range item if this was its last publication */
  470. if (list_empty(&sr->all_publ)) {
  471. rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
  472. kfree(sr);
  473. }
  474. /* Delete service item if no more publications and subscriptions */
  475. if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
  476. hlist_del_init_rcu(&sc->service_list);
  477. kfree_rcu(sc, rcu);
  478. }
  479. unlock:
  480. spin_unlock_bh(&sc->lock);
  481. exit:
  482. if (!p) {
  483. pr_err("Failed to remove unknown binding: %u,%u,%u/%u:%u/%u\n",
  484. ua->sr.type, ua->sr.lower, ua->sr.upper,
  485. sk->node, sk->ref, key);
  486. }
  487. return p;
  488. }
  489. /**
  490. * tipc_nametbl_lookup_anycast - perform service instance to socket translation
  491. * @net: network namespace
  492. * @ua: service address to look up
  493. * @sk: address to socket we want to find
  494. *
  495. * On entry, a non-zero 'sk->node' indicates the node where we want lookup to be
  496. * performed, which may not be this one.
  497. *
  498. * On exit:
  499. *
  500. * - If lookup is deferred to another node, leave 'sk->node' unchanged and
  501. * return 'true'.
  502. * - If lookup is successful, set the 'sk->node' and 'sk->ref' (== portid) which
  503. * represent the bound socket and return 'true'.
  504. * - If lookup fails, return 'false'
  505. *
  506. * Note that for legacy users (node configured with Z.C.N address format) the
  507. * 'closest-first' lookup algorithm must be maintained, i.e., if sk.node is 0
  508. * we must look in the local binding list first
  509. */
  510. bool tipc_nametbl_lookup_anycast(struct net *net,
  511. struct tipc_uaddr *ua,
  512. struct tipc_socket_addr *sk)
  513. {
  514. struct tipc_net *tn = tipc_net(net);
  515. bool legacy = tn->legacy_addr_format;
  516. u32 self = tipc_own_addr(net);
  517. u32 inst = ua->sa.instance;
  518. struct service_range *r;
  519. struct tipc_service *sc;
  520. struct publication *p;
  521. struct list_head *l;
  522. bool res = false;
  523. if (!tipc_in_scope(legacy, sk->node, self))
  524. return true;
  525. rcu_read_lock();
  526. sc = tipc_service_find(net, ua);
  527. if (unlikely(!sc))
  528. goto exit;
  529. spin_lock_bh(&sc->lock);
  530. service_range_foreach_match(r, sc, inst, inst) {
  531. /* Select lookup algo: local, closest-first or round-robin */
  532. if (sk->node == self) {
  533. l = &r->local_publ;
  534. if (list_empty(l))
  535. continue;
  536. p = list_first_entry(l, struct publication, local_publ);
  537. list_move_tail(&p->local_publ, &r->local_publ);
  538. } else if (legacy && !sk->node && !list_empty(&r->local_publ)) {
  539. l = &r->local_publ;
  540. p = list_first_entry(l, struct publication, local_publ);
  541. list_move_tail(&p->local_publ, &r->local_publ);
  542. } else {
  543. l = &r->all_publ;
  544. p = list_first_entry(l, struct publication, all_publ);
  545. list_move_tail(&p->all_publ, &r->all_publ);
  546. }
  547. *sk = p->sk;
  548. res = true;
  549. /* Todo: as for legacy, pick the first matching range only, a
  550. * "true" round-robin will be performed as needed.
  551. */
  552. break;
  553. }
  554. spin_unlock_bh(&sc->lock);
  555. exit:
  556. rcu_read_unlock();
  557. return res;
  558. }
  559. /* tipc_nametbl_lookup_group(): lookup destinaton(s) in a communication group
  560. * Returns a list of one (== group anycast) or more (== group multicast)
  561. * destination socket/node pairs matching the given address.
  562. * The requester may or may not want to exclude himself from the list.
  563. */
  564. bool tipc_nametbl_lookup_group(struct net *net, struct tipc_uaddr *ua,
  565. struct list_head *dsts, int *dstcnt,
  566. u32 exclude, bool mcast)
  567. {
  568. u32 self = tipc_own_addr(net);
  569. u32 inst = ua->sa.instance;
  570. struct service_range *sr;
  571. struct tipc_service *sc;
  572. struct publication *p;
  573. *dstcnt = 0;
  574. rcu_read_lock();
  575. sc = tipc_service_find(net, ua);
  576. if (unlikely(!sc))
  577. goto exit;
  578. spin_lock_bh(&sc->lock);
  579. /* Todo: a full search i.e. service_range_foreach_match() instead? */
  580. sr = service_range_match_first(sc->ranges.rb_node, inst, inst);
  581. if (!sr)
  582. goto no_match;
  583. list_for_each_entry(p, &sr->all_publ, all_publ) {
  584. if (p->scope != ua->scope)
  585. continue;
  586. if (p->sk.ref == exclude && p->sk.node == self)
  587. continue;
  588. tipc_dest_push(dsts, p->sk.node, p->sk.ref);
  589. (*dstcnt)++;
  590. if (mcast)
  591. continue;
  592. list_move_tail(&p->all_publ, &sr->all_publ);
  593. break;
  594. }
  595. no_match:
  596. spin_unlock_bh(&sc->lock);
  597. exit:
  598. rcu_read_unlock();
  599. return !list_empty(dsts);
  600. }
  601. /* tipc_nametbl_lookup_mcast_sockets(): look up node local destinaton sockets
  602. * matching the given address
  603. * Used on nodes which have received a multicast/broadcast message
  604. * Returns a list of local sockets
  605. */
  606. void tipc_nametbl_lookup_mcast_sockets(struct net *net, struct tipc_uaddr *ua,
  607. struct list_head *dports)
  608. {
  609. struct service_range *sr;
  610. struct tipc_service *sc;
  611. struct publication *p;
  612. u8 scope = ua->scope;
  613. rcu_read_lock();
  614. sc = tipc_service_find(net, ua);
  615. if (!sc)
  616. goto exit;
  617. spin_lock_bh(&sc->lock);
  618. service_range_foreach_match(sr, sc, ua->sr.lower, ua->sr.upper) {
  619. list_for_each_entry(p, &sr->local_publ, local_publ) {
  620. if (scope == p->scope || scope == TIPC_ANY_SCOPE)
  621. tipc_dest_push(dports, 0, p->sk.ref);
  622. }
  623. }
  624. spin_unlock_bh(&sc->lock);
  625. exit:
  626. rcu_read_unlock();
  627. }
  628. /* tipc_nametbl_lookup_mcast_nodes(): look up all destination nodes matching
  629. * the given address. Used in sending node.
  630. * Used on nodes which are sending out a multicast/broadcast message
  631. * Returns a list of nodes, including own node if applicable
  632. */
  633. void tipc_nametbl_lookup_mcast_nodes(struct net *net, struct tipc_uaddr *ua,
  634. struct tipc_nlist *nodes)
  635. {
  636. struct service_range *sr;
  637. struct tipc_service *sc;
  638. struct publication *p;
  639. rcu_read_lock();
  640. sc = tipc_service_find(net, ua);
  641. if (!sc)
  642. goto exit;
  643. spin_lock_bh(&sc->lock);
  644. service_range_foreach_match(sr, sc, ua->sr.lower, ua->sr.upper) {
  645. list_for_each_entry(p, &sr->all_publ, all_publ) {
  646. tipc_nlist_add(nodes, p->sk.node);
  647. }
  648. }
  649. spin_unlock_bh(&sc->lock);
  650. exit:
  651. rcu_read_unlock();
  652. }
  653. /* tipc_nametbl_build_group - build list of communication group members
  654. */
  655. void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
  656. struct tipc_uaddr *ua)
  657. {
  658. struct service_range *sr;
  659. struct tipc_service *sc;
  660. struct publication *p;
  661. struct rb_node *n;
  662. rcu_read_lock();
  663. sc = tipc_service_find(net, ua);
  664. if (!sc)
  665. goto exit;
  666. spin_lock_bh(&sc->lock);
  667. for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
  668. sr = container_of(n, struct service_range, tree_node);
  669. list_for_each_entry(p, &sr->all_publ, all_publ) {
  670. if (p->scope != ua->scope)
  671. continue;
  672. tipc_group_add_member(grp, p->sk.node, p->sk.ref,
  673. p->sr.lower);
  674. }
  675. }
  676. spin_unlock_bh(&sc->lock);
  677. exit:
  678. rcu_read_unlock();
  679. }
  680. /* tipc_nametbl_publish - add service binding to name table
  681. */
  682. struct publication *tipc_nametbl_publish(struct net *net, struct tipc_uaddr *ua,
  683. struct tipc_socket_addr *sk, u32 key)
  684. {
  685. struct name_table *nt = tipc_name_table(net);
  686. struct tipc_net *tn = tipc_net(net);
  687. struct publication *p = NULL;
  688. struct sk_buff *skb = NULL;
  689. u32 rc_dests;
  690. spin_lock_bh(&tn->nametbl_lock);
  691. if (nt->local_publ_count >= TIPC_MAX_PUBL) {
  692. pr_warn("Bind failed, max limit %u reached\n", TIPC_MAX_PUBL);
  693. goto exit;
  694. }
  695. p = tipc_nametbl_insert_publ(net, ua, sk, key);
  696. if (p) {
  697. nt->local_publ_count++;
  698. skb = tipc_named_publish(net, p);
  699. }
  700. rc_dests = nt->rc_dests;
  701. exit:
  702. spin_unlock_bh(&tn->nametbl_lock);
  703. if (skb)
  704. tipc_node_broadcast(net, skb, rc_dests);
  705. return p;
  706. }
  707. /**
  708. * tipc_nametbl_withdraw - withdraw a service binding
  709. * @net: network namespace
  710. * @ua: service address/range being unbound
  711. * @sk: address of the socket being unbound from
  712. * @key: target publication key
  713. */
  714. void tipc_nametbl_withdraw(struct net *net, struct tipc_uaddr *ua,
  715. struct tipc_socket_addr *sk, u32 key)
  716. {
  717. struct name_table *nt = tipc_name_table(net);
  718. struct tipc_net *tn = tipc_net(net);
  719. struct sk_buff *skb = NULL;
  720. struct publication *p;
  721. u32 rc_dests;
  722. spin_lock_bh(&tn->nametbl_lock);
  723. p = tipc_nametbl_remove_publ(net, ua, sk, key);
  724. if (p) {
  725. nt->local_publ_count--;
  726. skb = tipc_named_withdraw(net, p);
  727. list_del_init(&p->binding_sock);
  728. kfree_rcu(p, rcu);
  729. }
  730. rc_dests = nt->rc_dests;
  731. spin_unlock_bh(&tn->nametbl_lock);
  732. if (skb)
  733. tipc_node_broadcast(net, skb, rc_dests);
  734. }
  735. /**
  736. * tipc_nametbl_subscribe - add a subscription object to the name table
  737. * @sub: subscription to add
  738. */
  739. bool tipc_nametbl_subscribe(struct tipc_subscription *sub)
  740. {
  741. struct tipc_net *tn = tipc_net(sub->net);
  742. u32 type = sub->s.seq.type;
  743. struct tipc_service *sc;
  744. struct tipc_uaddr ua;
  745. bool res = true;
  746. tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE, type,
  747. sub->s.seq.lower, sub->s.seq.upper);
  748. spin_lock_bh(&tn->nametbl_lock);
  749. sc = tipc_service_find(sub->net, &ua);
  750. if (!sc)
  751. sc = tipc_service_create(sub->net, &ua);
  752. if (sc) {
  753. spin_lock_bh(&sc->lock);
  754. tipc_service_subscribe(sc, sub);
  755. spin_unlock_bh(&sc->lock);
  756. } else {
  757. pr_warn("Failed to subscribe for {%u,%u,%u}\n",
  758. type, sub->s.seq.lower, sub->s.seq.upper);
  759. res = false;
  760. }
  761. spin_unlock_bh(&tn->nametbl_lock);
  762. return res;
  763. }
  764. /**
  765. * tipc_nametbl_unsubscribe - remove a subscription object from name table
  766. * @sub: subscription to remove
  767. */
  768. void tipc_nametbl_unsubscribe(struct tipc_subscription *sub)
  769. {
  770. struct tipc_net *tn = tipc_net(sub->net);
  771. struct tipc_service *sc;
  772. struct tipc_uaddr ua;
  773. tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
  774. sub->s.seq.type, sub->s.seq.lower, sub->s.seq.upper);
  775. spin_lock_bh(&tn->nametbl_lock);
  776. sc = tipc_service_find(sub->net, &ua);
  777. if (!sc)
  778. goto exit;
  779. spin_lock_bh(&sc->lock);
  780. list_del_init(&sub->service_list);
  781. tipc_sub_put(sub);
  782. /* Delete service item if no more publications and subscriptions */
  783. if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) {
  784. hlist_del_init_rcu(&sc->service_list);
  785. kfree_rcu(sc, rcu);
  786. }
  787. spin_unlock_bh(&sc->lock);
  788. exit:
  789. spin_unlock_bh(&tn->nametbl_lock);
  790. }
  791. int tipc_nametbl_init(struct net *net)
  792. {
  793. struct tipc_net *tn = tipc_net(net);
  794. struct name_table *nt;
  795. int i;
  796. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  797. if (!nt)
  798. return -ENOMEM;
  799. for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
  800. INIT_HLIST_HEAD(&nt->services[i]);
  801. INIT_LIST_HEAD(&nt->node_scope);
  802. INIT_LIST_HEAD(&nt->cluster_scope);
  803. rwlock_init(&nt->cluster_scope_lock);
  804. tn->nametbl = nt;
  805. spin_lock_init(&tn->nametbl_lock);
  806. return 0;
  807. }
  808. /**
  809. * tipc_service_delete - purge all publications for a service and delete it
  810. * @net: the associated network namespace
  811. * @sc: tipc_service to delete
  812. */
  813. static void tipc_service_delete(struct net *net, struct tipc_service *sc)
  814. {
  815. struct service_range *sr, *tmpr;
  816. struct publication *p, *tmp;
  817. spin_lock_bh(&sc->lock);
  818. rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
  819. list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
  820. tipc_service_remove_publ(sr, &p->sk, p->key);
  821. kfree_rcu(p, rcu);
  822. }
  823. rb_erase_augmented(&sr->tree_node, &sc->ranges, &sr_callbacks);
  824. kfree(sr);
  825. }
  826. hlist_del_init_rcu(&sc->service_list);
  827. spin_unlock_bh(&sc->lock);
  828. kfree_rcu(sc, rcu);
  829. }
  830. void tipc_nametbl_stop(struct net *net)
  831. {
  832. struct name_table *nt = tipc_name_table(net);
  833. struct tipc_net *tn = tipc_net(net);
  834. struct hlist_head *service_head;
  835. struct tipc_service *service;
  836. u32 i;
  837. /* Verify name table is empty and purge any lingering
  838. * publications, then release the name table
  839. */
  840. spin_lock_bh(&tn->nametbl_lock);
  841. for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
  842. if (hlist_empty(&nt->services[i]))
  843. continue;
  844. service_head = &nt->services[i];
  845. hlist_for_each_entry_rcu(service, service_head, service_list) {
  846. tipc_service_delete(net, service);
  847. }
  848. }
  849. spin_unlock_bh(&tn->nametbl_lock);
  850. synchronize_net();
  851. kfree(nt);
  852. }
  853. static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
  854. struct tipc_service *service,
  855. struct service_range *sr,
  856. u32 *last_key)
  857. {
  858. struct publication *p;
  859. struct nlattr *attrs;
  860. struct nlattr *b;
  861. void *hdr;
  862. if (*last_key) {
  863. list_for_each_entry(p, &sr->all_publ, all_publ)
  864. if (p->key == *last_key)
  865. break;
  866. if (list_entry_is_head(p, &sr->all_publ, all_publ))
  867. return -EPIPE;
  868. } else {
  869. p = list_first_entry(&sr->all_publ,
  870. struct publication,
  871. all_publ);
  872. }
  873. list_for_each_entry_from(p, &sr->all_publ, all_publ) {
  874. *last_key = p->key;
  875. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
  876. &tipc_genl_family, NLM_F_MULTI,
  877. TIPC_NL_NAME_TABLE_GET);
  878. if (!hdr)
  879. return -EMSGSIZE;
  880. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE);
  881. if (!attrs)
  882. goto msg_full;
  883. b = nla_nest_start_noflag(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
  884. if (!b)
  885. goto attr_msg_full;
  886. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type))
  887. goto publ_msg_full;
  888. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower))
  889. goto publ_msg_full;
  890. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper))
  891. goto publ_msg_full;
  892. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
  893. goto publ_msg_full;
  894. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->sk.node))
  895. goto publ_msg_full;
  896. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->sk.ref))
  897. goto publ_msg_full;
  898. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
  899. goto publ_msg_full;
  900. nla_nest_end(msg->skb, b);
  901. nla_nest_end(msg->skb, attrs);
  902. genlmsg_end(msg->skb, hdr);
  903. }
  904. *last_key = 0;
  905. return 0;
  906. publ_msg_full:
  907. nla_nest_cancel(msg->skb, b);
  908. attr_msg_full:
  909. nla_nest_cancel(msg->skb, attrs);
  910. msg_full:
  911. genlmsg_cancel(msg->skb, hdr);
  912. return -EMSGSIZE;
  913. }
  914. static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg,
  915. struct tipc_service *sc,
  916. u32 *last_lower, u32 *last_key)
  917. {
  918. struct service_range *sr;
  919. struct rb_node *n;
  920. int err;
  921. for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
  922. sr = container_of(n, struct service_range, tree_node);
  923. if (sr->lower < *last_lower)
  924. continue;
  925. err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key);
  926. if (err) {
  927. *last_lower = sr->lower;
  928. return err;
  929. }
  930. }
  931. *last_lower = 0;
  932. return 0;
  933. }
  934. static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
  935. u32 *last_type, u32 *last_lower, u32 *last_key)
  936. {
  937. struct tipc_net *tn = tipc_net(net);
  938. struct tipc_service *service = NULL;
  939. struct hlist_head *head;
  940. struct tipc_uaddr ua;
  941. int err;
  942. int i;
  943. if (*last_type)
  944. i = hash(*last_type);
  945. else
  946. i = 0;
  947. for (; i < TIPC_NAMETBL_SIZE; i++) {
  948. head = &tn->nametbl->services[i];
  949. if (*last_type ||
  950. (!i && *last_key && (*last_lower == *last_key))) {
  951. tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
  952. *last_type, *last_lower, *last_lower);
  953. service = tipc_service_find(net, &ua);
  954. if (!service)
  955. return -EPIPE;
  956. } else {
  957. hlist_for_each_entry_rcu(service, head, service_list)
  958. break;
  959. if (!service)
  960. continue;
  961. }
  962. hlist_for_each_entry_from_rcu(service, service_list) {
  963. spin_lock_bh(&service->lock);
  964. err = __tipc_nl_service_range_list(msg, service,
  965. last_lower,
  966. last_key);
  967. if (err) {
  968. *last_type = service->type;
  969. spin_unlock_bh(&service->lock);
  970. return err;
  971. }
  972. spin_unlock_bh(&service->lock);
  973. }
  974. *last_type = 0;
  975. }
  976. return 0;
  977. }
  978. int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
  979. {
  980. struct net *net = sock_net(skb->sk);
  981. u32 last_type = cb->args[0];
  982. u32 last_lower = cb->args[1];
  983. u32 last_key = cb->args[2];
  984. int done = cb->args[3];
  985. struct tipc_nl_msg msg;
  986. int err;
  987. if (done)
  988. return 0;
  989. msg.skb = skb;
  990. msg.portid = NETLINK_CB(cb->skb).portid;
  991. msg.seq = cb->nlh->nlmsg_seq;
  992. rcu_read_lock();
  993. err = tipc_nl_service_list(net, &msg, &last_type,
  994. &last_lower, &last_key);
  995. if (!err) {
  996. done = 1;
  997. } else if (err != -EMSGSIZE) {
  998. /* We never set seq or call nl_dump_check_consistent() this
  999. * means that setting prev_seq here will cause the consistence
  1000. * check to fail in the netlink callback handler. Resulting in
  1001. * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
  1002. * we got an error.
  1003. */
  1004. cb->prev_seq = 1;
  1005. }
  1006. rcu_read_unlock();
  1007. cb->args[0] = last_type;
  1008. cb->args[1] = last_lower;
  1009. cb->args[2] = last_key;
  1010. cb->args[3] = done;
  1011. return skb->len;
  1012. }
  1013. struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
  1014. {
  1015. struct tipc_dest *dst;
  1016. list_for_each_entry(dst, l, list) {
  1017. if (dst->node == node && dst->port == port)
  1018. return dst;
  1019. }
  1020. return NULL;
  1021. }
  1022. bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
  1023. {
  1024. struct tipc_dest *dst;
  1025. if (tipc_dest_find(l, node, port))
  1026. return false;
  1027. dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
  1028. if (unlikely(!dst))
  1029. return false;
  1030. dst->node = node;
  1031. dst->port = port;
  1032. list_add(&dst->list, l);
  1033. return true;
  1034. }
  1035. bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
  1036. {
  1037. struct tipc_dest *dst;
  1038. if (list_empty(l))
  1039. return false;
  1040. dst = list_first_entry(l, typeof(*dst), list);
  1041. if (port)
  1042. *port = dst->port;
  1043. if (node)
  1044. *node = dst->node;
  1045. list_del(&dst->list);
  1046. kfree(dst);
  1047. return true;
  1048. }
  1049. bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
  1050. {
  1051. struct tipc_dest *dst;
  1052. dst = tipc_dest_find(l, node, port);
  1053. if (!dst)
  1054. return false;
  1055. list_del(&dst->list);
  1056. kfree(dst);
  1057. return true;
  1058. }
  1059. void tipc_dest_list_purge(struct list_head *l)
  1060. {
  1061. struct tipc_dest *dst, *tmp;
  1062. list_for_each_entry_safe(dst, tmp, l, list) {
  1063. list_del(&dst->list);
  1064. kfree(dst);
  1065. }
  1066. }