name_table.c 27 KB

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