name_table.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. tn->nametbl = nt;
  665. spin_lock_init(&tn->nametbl_lock);
  666. return 0;
  667. }
  668. /**
  669. * tipc_service_delete - purge all publications for a service and delete it
  670. */
  671. static void tipc_service_delete(struct net *net, struct tipc_service *sc)
  672. {
  673. struct service_range *sr, *tmpr;
  674. struct publication *p, *tmp;
  675. spin_lock_bh(&sc->lock);
  676. rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) {
  677. list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) {
  678. tipc_service_remove_publ(sr, p->node, p->key);
  679. kfree_rcu(p, rcu);
  680. }
  681. rb_erase(&sr->tree_node, &sc->ranges);
  682. kfree(sr);
  683. }
  684. hlist_del_init_rcu(&sc->service_list);
  685. spin_unlock_bh(&sc->lock);
  686. kfree_rcu(sc, rcu);
  687. }
  688. void tipc_nametbl_stop(struct net *net)
  689. {
  690. struct name_table *nt = tipc_name_table(net);
  691. struct tipc_net *tn = tipc_net(net);
  692. struct hlist_head *service_head;
  693. struct tipc_service *service;
  694. u32 i;
  695. /* Verify name table is empty and purge any lingering
  696. * publications, then release the name table
  697. */
  698. spin_lock_bh(&tn->nametbl_lock);
  699. for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
  700. if (hlist_empty(&nt->services[i]))
  701. continue;
  702. service_head = &nt->services[i];
  703. hlist_for_each_entry_rcu(service, service_head, service_list) {
  704. tipc_service_delete(net, service);
  705. }
  706. }
  707. spin_unlock_bh(&tn->nametbl_lock);
  708. synchronize_net();
  709. kfree(nt);
  710. }
  711. static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
  712. struct tipc_service *service,
  713. struct service_range *sr,
  714. u32 *last_key)
  715. {
  716. struct publication *p;
  717. struct nlattr *attrs;
  718. struct nlattr *b;
  719. void *hdr;
  720. if (*last_key) {
  721. list_for_each_entry(p, &sr->all_publ, all_publ)
  722. if (p->key == *last_key)
  723. break;
  724. if (p->key != *last_key)
  725. return -EPIPE;
  726. } else {
  727. p = list_first_entry(&sr->all_publ,
  728. struct publication,
  729. all_publ);
  730. }
  731. list_for_each_entry_from(p, &sr->all_publ, all_publ) {
  732. *last_key = p->key;
  733. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
  734. &tipc_genl_family, NLM_F_MULTI,
  735. TIPC_NL_NAME_TABLE_GET);
  736. if (!hdr)
  737. return -EMSGSIZE;
  738. attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
  739. if (!attrs)
  740. goto msg_full;
  741. b = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
  742. if (!b)
  743. goto attr_msg_full;
  744. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, service->type))
  745. goto publ_msg_full;
  746. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sr->lower))
  747. goto publ_msg_full;
  748. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sr->upper))
  749. goto publ_msg_full;
  750. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
  751. goto publ_msg_full;
  752. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
  753. goto publ_msg_full;
  754. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->port))
  755. goto publ_msg_full;
  756. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
  757. goto publ_msg_full;
  758. nla_nest_end(msg->skb, b);
  759. nla_nest_end(msg->skb, attrs);
  760. genlmsg_end(msg->skb, hdr);
  761. }
  762. *last_key = 0;
  763. return 0;
  764. publ_msg_full:
  765. nla_nest_cancel(msg->skb, b);
  766. attr_msg_full:
  767. nla_nest_cancel(msg->skb, attrs);
  768. msg_full:
  769. genlmsg_cancel(msg->skb, hdr);
  770. return -EMSGSIZE;
  771. }
  772. static int __tipc_nl_service_range_list(struct tipc_nl_msg *msg,
  773. struct tipc_service *sc,
  774. u32 *last_lower, u32 *last_key)
  775. {
  776. struct service_range *sr;
  777. struct rb_node *n;
  778. int err;
  779. for (n = rb_first(&sc->ranges); n; n = rb_next(n)) {
  780. sr = container_of(n, struct service_range, tree_node);
  781. if (sr->lower < *last_lower)
  782. continue;
  783. err = __tipc_nl_add_nametable_publ(msg, sc, sr, last_key);
  784. if (err) {
  785. *last_lower = sr->lower;
  786. return err;
  787. }
  788. }
  789. *last_lower = 0;
  790. return 0;
  791. }
  792. static int tipc_nl_service_list(struct net *net, struct tipc_nl_msg *msg,
  793. u32 *last_type, u32 *last_lower, u32 *last_key)
  794. {
  795. struct tipc_net *tn = tipc_net(net);
  796. struct tipc_service *service = NULL;
  797. struct hlist_head *head;
  798. int err;
  799. int i;
  800. if (*last_type)
  801. i = hash(*last_type);
  802. else
  803. i = 0;
  804. for (; i < TIPC_NAMETBL_SIZE; i++) {
  805. head = &tn->nametbl->services[i];
  806. if (*last_type) {
  807. service = tipc_service_find(net, *last_type);
  808. if (!service)
  809. return -EPIPE;
  810. } else {
  811. hlist_for_each_entry_rcu(service, head, service_list)
  812. break;
  813. if (!service)
  814. continue;
  815. }
  816. hlist_for_each_entry_from_rcu(service, service_list) {
  817. spin_lock_bh(&service->lock);
  818. err = __tipc_nl_service_range_list(msg, service,
  819. last_lower,
  820. last_key);
  821. if (err) {
  822. *last_type = service->type;
  823. spin_unlock_bh(&service->lock);
  824. return err;
  825. }
  826. spin_unlock_bh(&service->lock);
  827. }
  828. *last_type = 0;
  829. }
  830. return 0;
  831. }
  832. int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
  833. {
  834. struct net *net = sock_net(skb->sk);
  835. u32 last_type = cb->args[0];
  836. u32 last_lower = cb->args[1];
  837. u32 last_key = cb->args[2];
  838. int done = cb->args[3];
  839. struct tipc_nl_msg msg;
  840. int err;
  841. if (done)
  842. return 0;
  843. msg.skb = skb;
  844. msg.portid = NETLINK_CB(cb->skb).portid;
  845. msg.seq = cb->nlh->nlmsg_seq;
  846. rcu_read_lock();
  847. err = tipc_nl_service_list(net, &msg, &last_type,
  848. &last_lower, &last_key);
  849. if (!err) {
  850. done = 1;
  851. } else if (err != -EMSGSIZE) {
  852. /* We never set seq or call nl_dump_check_consistent() this
  853. * means that setting prev_seq here will cause the consistence
  854. * check to fail in the netlink callback handler. Resulting in
  855. * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
  856. * we got an error.
  857. */
  858. cb->prev_seq = 1;
  859. }
  860. rcu_read_unlock();
  861. cb->args[0] = last_type;
  862. cb->args[1] = last_lower;
  863. cb->args[2] = last_key;
  864. cb->args[3] = done;
  865. return skb->len;
  866. }
  867. struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
  868. {
  869. struct tipc_dest *dst;
  870. list_for_each_entry(dst, l, list) {
  871. if (dst->node == node && dst->port == port)
  872. return dst;
  873. }
  874. return NULL;
  875. }
  876. bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
  877. {
  878. struct tipc_dest *dst;
  879. if (tipc_dest_find(l, node, port))
  880. return false;
  881. dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
  882. if (unlikely(!dst))
  883. return false;
  884. dst->node = node;
  885. dst->port = port;
  886. list_add(&dst->list, l);
  887. return true;
  888. }
  889. bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
  890. {
  891. struct tipc_dest *dst;
  892. if (list_empty(l))
  893. return false;
  894. dst = list_first_entry(l, typeof(*dst), list);
  895. if (port)
  896. *port = dst->port;
  897. if (node)
  898. *node = dst->node;
  899. list_del(&dst->list);
  900. kfree(dst);
  901. return true;
  902. }
  903. bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
  904. {
  905. struct tipc_dest *dst;
  906. dst = tipc_dest_find(l, node, port);
  907. if (!dst)
  908. return false;
  909. list_del(&dst->list);
  910. kfree(dst);
  911. return true;
  912. }
  913. void tipc_dest_list_purge(struct list_head *l)
  914. {
  915. struct tipc_dest *dst, *tmp;
  916. list_for_each_entry_safe(dst, tmp, l, list) {
  917. list_del(&dst->list);
  918. kfree(dst);
  919. }
  920. }
  921. int tipc_dest_list_len(struct list_head *l)
  922. {
  923. struct tipc_dest *dst;
  924. int i = 0;
  925. list_for_each_entry(dst, l, list) {
  926. i++;
  927. }
  928. return i;
  929. }