name_table.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /*
  2. * net/tipc/name_table.c: TIPC name table code
  3. *
  4. * Copyright (c) 2000-2006, 2014-2015, 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 <net/genetlink.h>
  46. #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
  47. static const struct nla_policy
  48. tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = {
  49. [TIPC_NLA_NAME_TABLE_UNSPEC] = { .type = NLA_UNSPEC },
  50. [TIPC_NLA_NAME_TABLE_PUBL] = { .type = NLA_NESTED }
  51. };
  52. /**
  53. * struct name_info - name sequence publication info
  54. * @node_list: circular list of publications made by own node
  55. * @cluster_list: circular list of publications made by own cluster
  56. * @zone_list: circular list of publications made by own zone
  57. * @node_list_size: number of entries in "node_list"
  58. * @cluster_list_size: number of entries in "cluster_list"
  59. * @zone_list_size: number of entries in "zone_list"
  60. *
  61. * Note: The zone list always contains at least one entry, since all
  62. * publications of the associated name sequence belong to it.
  63. * (The cluster and node lists may be empty.)
  64. */
  65. struct name_info {
  66. struct list_head node_list;
  67. struct list_head cluster_list;
  68. struct list_head zone_list;
  69. u32 node_list_size;
  70. u32 cluster_list_size;
  71. u32 zone_list_size;
  72. };
  73. /**
  74. * struct sub_seq - container for all published instances of a name sequence
  75. * @lower: name sequence lower bound
  76. * @upper: name sequence upper bound
  77. * @info: pointer to name sequence publication info
  78. */
  79. struct sub_seq {
  80. u32 lower;
  81. u32 upper;
  82. struct name_info *info;
  83. };
  84. /**
  85. * struct name_seq - container for all published instances of a name type
  86. * @type: 32 bit 'type' value for name sequence
  87. * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
  88. * sub-sequences are sorted in ascending order
  89. * @alloc: number of sub-sequences currently in array
  90. * @first_free: array index of first unused sub-sequence entry
  91. * @ns_list: links to adjacent name sequences in hash chain
  92. * @subscriptions: list of subscriptions for this 'type'
  93. * @lock: spinlock controlling access to publication lists of all sub-sequences
  94. * @rcu: RCU callback head used for deferred freeing
  95. */
  96. struct name_seq {
  97. u32 type;
  98. struct sub_seq *sseqs;
  99. u32 alloc;
  100. u32 first_free;
  101. struct hlist_node ns_list;
  102. struct list_head subscriptions;
  103. spinlock_t lock;
  104. struct rcu_head rcu;
  105. };
  106. static int hash(int x)
  107. {
  108. return x & (TIPC_NAMETBL_SIZE - 1);
  109. }
  110. /**
  111. * publ_create - create a publication structure
  112. */
  113. static struct publication *publ_create(u32 type, u32 lower, u32 upper,
  114. u32 scope, u32 node, u32 port_ref,
  115. u32 key)
  116. {
  117. struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
  118. if (publ == NULL) {
  119. pr_warn("Publication creation failure, no memory\n");
  120. return NULL;
  121. }
  122. publ->type = type;
  123. publ->lower = lower;
  124. publ->upper = upper;
  125. publ->scope = scope;
  126. publ->node = node;
  127. publ->ref = port_ref;
  128. publ->key = key;
  129. INIT_LIST_HEAD(&publ->pport_list);
  130. return publ;
  131. }
  132. /**
  133. * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
  134. */
  135. static struct sub_seq *tipc_subseq_alloc(u32 cnt)
  136. {
  137. return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
  138. }
  139. /**
  140. * tipc_nameseq_create - create a name sequence structure for the specified 'type'
  141. *
  142. * Allocates a single sub-sequence structure and sets it to all 0's.
  143. */
  144. static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
  145. {
  146. struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
  147. struct sub_seq *sseq = tipc_subseq_alloc(1);
  148. if (!nseq || !sseq) {
  149. pr_warn("Name sequence creation failed, no memory\n");
  150. kfree(nseq);
  151. kfree(sseq);
  152. return NULL;
  153. }
  154. spin_lock_init(&nseq->lock);
  155. nseq->type = type;
  156. nseq->sseqs = sseq;
  157. nseq->alloc = 1;
  158. INIT_HLIST_NODE(&nseq->ns_list);
  159. INIT_LIST_HEAD(&nseq->subscriptions);
  160. hlist_add_head_rcu(&nseq->ns_list, seq_head);
  161. return nseq;
  162. }
  163. /**
  164. * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
  165. *
  166. * Very time-critical, so binary searches through sub-sequence array.
  167. */
  168. static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
  169. u32 instance)
  170. {
  171. struct sub_seq *sseqs = nseq->sseqs;
  172. int low = 0;
  173. int high = nseq->first_free - 1;
  174. int mid;
  175. while (low <= high) {
  176. mid = (low + high) / 2;
  177. if (instance < sseqs[mid].lower)
  178. high = mid - 1;
  179. else if (instance > sseqs[mid].upper)
  180. low = mid + 1;
  181. else
  182. return &sseqs[mid];
  183. }
  184. return NULL;
  185. }
  186. /**
  187. * nameseq_locate_subseq - determine position of name instance in sub-sequence
  188. *
  189. * Returns index in sub-sequence array of the entry that contains the specified
  190. * instance value; if no entry contains that value, returns the position
  191. * where a new entry for it would be inserted in the array.
  192. *
  193. * Note: Similar to binary search code for locating a sub-sequence.
  194. */
  195. static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
  196. {
  197. struct sub_seq *sseqs = nseq->sseqs;
  198. int low = 0;
  199. int high = nseq->first_free - 1;
  200. int mid;
  201. while (low <= high) {
  202. mid = (low + high) / 2;
  203. if (instance < sseqs[mid].lower)
  204. high = mid - 1;
  205. else if (instance > sseqs[mid].upper)
  206. low = mid + 1;
  207. else
  208. return mid;
  209. }
  210. return low;
  211. }
  212. /**
  213. * tipc_nameseq_insert_publ
  214. */
  215. static struct publication *tipc_nameseq_insert_publ(struct net *net,
  216. struct name_seq *nseq,
  217. u32 type, u32 lower,
  218. u32 upper, u32 scope,
  219. u32 node, u32 port, u32 key)
  220. {
  221. struct tipc_subscription *s;
  222. struct tipc_subscription *st;
  223. struct publication *publ;
  224. struct sub_seq *sseq;
  225. struct name_info *info;
  226. int created_subseq = 0;
  227. sseq = nameseq_find_subseq(nseq, lower);
  228. if (sseq) {
  229. /* Lower end overlaps existing entry => need an exact match */
  230. if ((sseq->lower != lower) || (sseq->upper != upper)) {
  231. return NULL;
  232. }
  233. info = sseq->info;
  234. /* Check if an identical publication already exists */
  235. list_for_each_entry(publ, &info->zone_list, zone_list) {
  236. if ((publ->ref == port) && (publ->key == key) &&
  237. (!publ->node || (publ->node == node)))
  238. return NULL;
  239. }
  240. } else {
  241. u32 inspos;
  242. struct sub_seq *freesseq;
  243. /* Find where lower end should be inserted */
  244. inspos = nameseq_locate_subseq(nseq, lower);
  245. /* Fail if upper end overlaps into an existing entry */
  246. if ((inspos < nseq->first_free) &&
  247. (upper >= nseq->sseqs[inspos].lower)) {
  248. return NULL;
  249. }
  250. /* Ensure there is space for new sub-sequence */
  251. if (nseq->first_free == nseq->alloc) {
  252. struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
  253. if (!sseqs) {
  254. pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
  255. type, lower, upper);
  256. return NULL;
  257. }
  258. memcpy(sseqs, nseq->sseqs,
  259. nseq->alloc * sizeof(struct sub_seq));
  260. kfree(nseq->sseqs);
  261. nseq->sseqs = sseqs;
  262. nseq->alloc *= 2;
  263. }
  264. info = kzalloc(sizeof(*info), GFP_ATOMIC);
  265. if (!info) {
  266. pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
  267. type, lower, upper);
  268. return NULL;
  269. }
  270. INIT_LIST_HEAD(&info->node_list);
  271. INIT_LIST_HEAD(&info->cluster_list);
  272. INIT_LIST_HEAD(&info->zone_list);
  273. /* Insert new sub-sequence */
  274. sseq = &nseq->sseqs[inspos];
  275. freesseq = &nseq->sseqs[nseq->first_free];
  276. memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
  277. memset(sseq, 0, sizeof(*sseq));
  278. nseq->first_free++;
  279. sseq->lower = lower;
  280. sseq->upper = upper;
  281. sseq->info = info;
  282. created_subseq = 1;
  283. }
  284. /* Insert a publication */
  285. publ = publ_create(type, lower, upper, scope, node, port, key);
  286. if (!publ)
  287. return NULL;
  288. list_add(&publ->zone_list, &info->zone_list);
  289. info->zone_list_size++;
  290. if (in_own_cluster(net, node)) {
  291. list_add(&publ->cluster_list, &info->cluster_list);
  292. info->cluster_list_size++;
  293. }
  294. if (in_own_node(net, node)) {
  295. list_add(&publ->node_list, &info->node_list);
  296. info->node_list_size++;
  297. }
  298. /* Any subscriptions waiting for notification? */
  299. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  300. tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
  301. TIPC_PUBLISHED, publ->ref,
  302. publ->node, created_subseq);
  303. }
  304. return publ;
  305. }
  306. /**
  307. * tipc_nameseq_remove_publ
  308. *
  309. * NOTE: There may be cases where TIPC is asked to remove a publication
  310. * that is not in the name table. For example, if another node issues a
  311. * publication for a name sequence that overlaps an existing name sequence
  312. * the publication will not be recorded, which means the publication won't
  313. * be found when the name sequence is later withdrawn by that node.
  314. * A failed withdraw request simply returns a failure indication and lets the
  315. * caller issue any error or warning messages associated with such a problem.
  316. */
  317. static struct publication *tipc_nameseq_remove_publ(struct net *net,
  318. struct name_seq *nseq,
  319. u32 inst, u32 node,
  320. u32 ref, u32 key)
  321. {
  322. struct publication *publ;
  323. struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
  324. struct name_info *info;
  325. struct sub_seq *free;
  326. struct tipc_subscription *s, *st;
  327. int removed_subseq = 0;
  328. if (!sseq)
  329. return NULL;
  330. info = sseq->info;
  331. /* Locate publication, if it exists */
  332. list_for_each_entry(publ, &info->zone_list, zone_list) {
  333. if ((publ->key == key) && (publ->ref == ref) &&
  334. (!publ->node || (publ->node == node)))
  335. goto found;
  336. }
  337. return NULL;
  338. found:
  339. /* Remove publication from zone scope list */
  340. list_del(&publ->zone_list);
  341. info->zone_list_size--;
  342. /* Remove publication from cluster scope list, if present */
  343. if (in_own_cluster(net, node)) {
  344. list_del(&publ->cluster_list);
  345. info->cluster_list_size--;
  346. }
  347. /* Remove publication from node scope list, if present */
  348. if (in_own_node(net, node)) {
  349. list_del(&publ->node_list);
  350. info->node_list_size--;
  351. }
  352. /* Contract subseq list if no more publications for that subseq */
  353. if (list_empty(&info->zone_list)) {
  354. kfree(info);
  355. free = &nseq->sseqs[nseq->first_free--];
  356. memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
  357. removed_subseq = 1;
  358. }
  359. /* Notify any waiting subscriptions */
  360. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  361. tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
  362. TIPC_WITHDRAWN, publ->ref,
  363. publ->node, removed_subseq);
  364. }
  365. return publ;
  366. }
  367. /**
  368. * tipc_nameseq_subscribe - attach a subscription, and issue
  369. * the prescribed number of events if there is any sub-
  370. * sequence overlapping with the requested sequence
  371. */
  372. static void tipc_nameseq_subscribe(struct name_seq *nseq,
  373. struct tipc_subscription *s)
  374. {
  375. struct sub_seq *sseq = nseq->sseqs;
  376. struct tipc_name_seq ns;
  377. tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
  378. list_add(&s->nameseq_list, &nseq->subscriptions);
  379. if (!sseq)
  380. return;
  381. while (sseq != &nseq->sseqs[nseq->first_free]) {
  382. if (tipc_subscrp_check_overlap(&ns, sseq->lower, sseq->upper)) {
  383. struct publication *crs;
  384. struct name_info *info = sseq->info;
  385. int must_report = 1;
  386. list_for_each_entry(crs, &info->zone_list, zone_list) {
  387. tipc_subscrp_report_overlap(s, sseq->lower,
  388. sseq->upper,
  389. TIPC_PUBLISHED,
  390. crs->ref, crs->node,
  391. must_report);
  392. must_report = 0;
  393. }
  394. }
  395. sseq++;
  396. }
  397. }
  398. static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
  399. {
  400. struct tipc_net *tn = net_generic(net, tipc_net_id);
  401. struct hlist_head *seq_head;
  402. struct name_seq *ns;
  403. seq_head = &tn->nametbl->seq_hlist[hash(type)];
  404. hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
  405. if (ns->type == type)
  406. return ns;
  407. }
  408. return NULL;
  409. };
  410. struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
  411. u32 lower, u32 upper, u32 scope,
  412. u32 node, u32 port, u32 key)
  413. {
  414. struct tipc_net *tn = net_generic(net, tipc_net_id);
  415. struct publication *publ;
  416. struct name_seq *seq = nametbl_find_seq(net, type);
  417. int index = hash(type);
  418. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
  419. (lower > upper)) {
  420. pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
  421. type, lower, upper, scope);
  422. return NULL;
  423. }
  424. if (!seq)
  425. seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
  426. if (!seq)
  427. return NULL;
  428. spin_lock_bh(&seq->lock);
  429. publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
  430. scope, node, port, key);
  431. spin_unlock_bh(&seq->lock);
  432. return publ;
  433. }
  434. struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
  435. u32 lower, u32 node, u32 ref,
  436. u32 key)
  437. {
  438. struct publication *publ;
  439. struct name_seq *seq = nametbl_find_seq(net, type);
  440. if (!seq)
  441. return NULL;
  442. spin_lock_bh(&seq->lock);
  443. publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
  444. if (!seq->first_free && list_empty(&seq->subscriptions)) {
  445. hlist_del_init_rcu(&seq->ns_list);
  446. kfree(seq->sseqs);
  447. spin_unlock_bh(&seq->lock);
  448. kfree_rcu(seq, rcu);
  449. return publ;
  450. }
  451. spin_unlock_bh(&seq->lock);
  452. return publ;
  453. }
  454. /**
  455. * tipc_nametbl_translate - perform name translation
  456. *
  457. * On entry, 'destnode' is the search domain used during translation.
  458. *
  459. * On exit:
  460. * - if name translation is deferred to another node/cluster/zone,
  461. * leaves 'destnode' unchanged (will be non-zero) and returns 0
  462. * - if name translation is attempted and succeeds, sets 'destnode'
  463. * to publishing node and returns port reference (will be non-zero)
  464. * - if name translation is attempted and fails, sets 'destnode' to 0
  465. * and returns 0
  466. */
  467. u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
  468. u32 *destnode)
  469. {
  470. struct tipc_net *tn = net_generic(net, tipc_net_id);
  471. struct sub_seq *sseq;
  472. struct name_info *info;
  473. struct publication *publ;
  474. struct name_seq *seq;
  475. u32 ref = 0;
  476. u32 node = 0;
  477. if (!tipc_in_scope(*destnode, tn->own_addr))
  478. return 0;
  479. rcu_read_lock();
  480. seq = nametbl_find_seq(net, type);
  481. if (unlikely(!seq))
  482. goto not_found;
  483. spin_lock_bh(&seq->lock);
  484. sseq = nameseq_find_subseq(seq, instance);
  485. if (unlikely(!sseq))
  486. goto no_match;
  487. info = sseq->info;
  488. /* Closest-First Algorithm */
  489. if (likely(!*destnode)) {
  490. if (!list_empty(&info->node_list)) {
  491. publ = list_first_entry(&info->node_list,
  492. struct publication,
  493. node_list);
  494. list_move_tail(&publ->node_list,
  495. &info->node_list);
  496. } else if (!list_empty(&info->cluster_list)) {
  497. publ = list_first_entry(&info->cluster_list,
  498. struct publication,
  499. cluster_list);
  500. list_move_tail(&publ->cluster_list,
  501. &info->cluster_list);
  502. } else {
  503. publ = list_first_entry(&info->zone_list,
  504. struct publication,
  505. zone_list);
  506. list_move_tail(&publ->zone_list,
  507. &info->zone_list);
  508. }
  509. }
  510. /* Round-Robin Algorithm */
  511. else if (*destnode == tn->own_addr) {
  512. if (list_empty(&info->node_list))
  513. goto no_match;
  514. publ = list_first_entry(&info->node_list, struct publication,
  515. node_list);
  516. list_move_tail(&publ->node_list, &info->node_list);
  517. } else if (in_own_cluster_exact(net, *destnode)) {
  518. if (list_empty(&info->cluster_list))
  519. goto no_match;
  520. publ = list_first_entry(&info->cluster_list, struct publication,
  521. cluster_list);
  522. list_move_tail(&publ->cluster_list, &info->cluster_list);
  523. } else {
  524. publ = list_first_entry(&info->zone_list, struct publication,
  525. zone_list);
  526. list_move_tail(&publ->zone_list, &info->zone_list);
  527. }
  528. ref = publ->ref;
  529. node = publ->node;
  530. no_match:
  531. spin_unlock_bh(&seq->lock);
  532. not_found:
  533. rcu_read_unlock();
  534. *destnode = node;
  535. return ref;
  536. }
  537. /**
  538. * tipc_nametbl_mc_translate - find multicast destinations
  539. *
  540. * Creates list of all local ports that overlap the given multicast address;
  541. * also determines if any off-node ports overlap.
  542. *
  543. * Note: Publications with a scope narrower than 'limit' are ignored.
  544. * (i.e. local node-scope publications mustn't receive messages arriving
  545. * from another node, even if the multcast link brought it here)
  546. *
  547. * Returns non-zero if any off-node ports overlap
  548. */
  549. int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper,
  550. u32 limit, struct tipc_plist *dports)
  551. {
  552. struct name_seq *seq;
  553. struct sub_seq *sseq;
  554. struct sub_seq *sseq_stop;
  555. struct name_info *info;
  556. int res = 0;
  557. rcu_read_lock();
  558. seq = nametbl_find_seq(net, type);
  559. if (!seq)
  560. goto exit;
  561. spin_lock_bh(&seq->lock);
  562. sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
  563. sseq_stop = seq->sseqs + seq->first_free;
  564. for (; sseq != sseq_stop; sseq++) {
  565. struct publication *publ;
  566. if (sseq->lower > upper)
  567. break;
  568. info = sseq->info;
  569. list_for_each_entry(publ, &info->node_list, node_list) {
  570. if (publ->scope <= limit)
  571. tipc_plist_push(dports, publ->ref);
  572. }
  573. if (info->cluster_list_size != info->node_list_size)
  574. res = 1;
  575. }
  576. spin_unlock_bh(&seq->lock);
  577. exit:
  578. rcu_read_unlock();
  579. return res;
  580. }
  581. /*
  582. * tipc_nametbl_publish - add name publication to network name tables
  583. */
  584. struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
  585. u32 upper, u32 scope, u32 port_ref,
  586. u32 key)
  587. {
  588. struct publication *publ;
  589. struct sk_buff *buf = NULL;
  590. struct tipc_net *tn = net_generic(net, tipc_net_id);
  591. spin_lock_bh(&tn->nametbl_lock);
  592. if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
  593. pr_warn("Publication failed, local publication limit reached (%u)\n",
  594. TIPC_MAX_PUBLICATIONS);
  595. spin_unlock_bh(&tn->nametbl_lock);
  596. return NULL;
  597. }
  598. publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
  599. tn->own_addr, port_ref, key);
  600. if (likely(publ)) {
  601. tn->nametbl->local_publ_count++;
  602. buf = tipc_named_publish(net, publ);
  603. /* Any pending external events? */
  604. tipc_named_process_backlog(net);
  605. }
  606. spin_unlock_bh(&tn->nametbl_lock);
  607. if (buf)
  608. tipc_node_broadcast(net, buf);
  609. return publ;
  610. }
  611. /**
  612. * tipc_nametbl_withdraw - withdraw name publication from network name tables
  613. */
  614. int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
  615. u32 key)
  616. {
  617. struct publication *publ;
  618. struct sk_buff *skb = NULL;
  619. struct tipc_net *tn = net_generic(net, tipc_net_id);
  620. spin_lock_bh(&tn->nametbl_lock);
  621. publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
  622. ref, key);
  623. if (likely(publ)) {
  624. tn->nametbl->local_publ_count--;
  625. skb = tipc_named_withdraw(net, publ);
  626. /* Any pending external events? */
  627. tipc_named_process_backlog(net);
  628. list_del_init(&publ->pport_list);
  629. kfree_rcu(publ, rcu);
  630. } else {
  631. pr_err("Unable to remove local publication\n"
  632. "(type=%u, lower=%u, ref=%u, key=%u)\n",
  633. type, lower, ref, key);
  634. }
  635. spin_unlock_bh(&tn->nametbl_lock);
  636. if (skb) {
  637. tipc_node_broadcast(net, skb);
  638. return 1;
  639. }
  640. return 0;
  641. }
  642. /**
  643. * tipc_nametbl_subscribe - add a subscription object to the name table
  644. */
  645. void tipc_nametbl_subscribe(struct tipc_subscription *s)
  646. {
  647. struct tipc_net *tn = net_generic(s->net, tipc_net_id);
  648. u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
  649. int index = hash(type);
  650. struct name_seq *seq;
  651. struct tipc_name_seq ns;
  652. spin_lock_bh(&tn->nametbl_lock);
  653. seq = nametbl_find_seq(s->net, type);
  654. if (!seq)
  655. seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
  656. if (seq) {
  657. spin_lock_bh(&seq->lock);
  658. tipc_nameseq_subscribe(seq, s);
  659. spin_unlock_bh(&seq->lock);
  660. } else {
  661. tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
  662. pr_warn("Failed to create subscription for {%u,%u,%u}\n",
  663. ns.type, ns.lower, ns.upper);
  664. }
  665. spin_unlock_bh(&tn->nametbl_lock);
  666. }
  667. /**
  668. * tipc_nametbl_unsubscribe - remove a subscription object from name table
  669. */
  670. void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
  671. {
  672. struct tipc_net *tn = net_generic(s->net, tipc_net_id);
  673. struct name_seq *seq;
  674. u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
  675. spin_lock_bh(&tn->nametbl_lock);
  676. seq = nametbl_find_seq(s->net, type);
  677. if (seq != NULL) {
  678. spin_lock_bh(&seq->lock);
  679. list_del_init(&s->nameseq_list);
  680. if (!seq->first_free && list_empty(&seq->subscriptions)) {
  681. hlist_del_init_rcu(&seq->ns_list);
  682. kfree(seq->sseqs);
  683. spin_unlock_bh(&seq->lock);
  684. kfree_rcu(seq, rcu);
  685. } else {
  686. spin_unlock_bh(&seq->lock);
  687. }
  688. }
  689. spin_unlock_bh(&tn->nametbl_lock);
  690. }
  691. int tipc_nametbl_init(struct net *net)
  692. {
  693. struct tipc_net *tn = net_generic(net, tipc_net_id);
  694. struct name_table *tipc_nametbl;
  695. int i;
  696. tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
  697. if (!tipc_nametbl)
  698. return -ENOMEM;
  699. for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
  700. INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
  701. INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
  702. INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
  703. INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
  704. tn->nametbl = tipc_nametbl;
  705. spin_lock_init(&tn->nametbl_lock);
  706. return 0;
  707. }
  708. /**
  709. * tipc_purge_publications - remove all publications for a given type
  710. *
  711. * tipc_nametbl_lock must be held when calling this function
  712. */
  713. static void tipc_purge_publications(struct net *net, struct name_seq *seq)
  714. {
  715. struct publication *publ, *safe;
  716. struct sub_seq *sseq;
  717. struct name_info *info;
  718. spin_lock_bh(&seq->lock);
  719. sseq = seq->sseqs;
  720. info = sseq->info;
  721. list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
  722. tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
  723. publ->ref, publ->key);
  724. kfree_rcu(publ, rcu);
  725. }
  726. hlist_del_init_rcu(&seq->ns_list);
  727. kfree(seq->sseqs);
  728. spin_unlock_bh(&seq->lock);
  729. kfree_rcu(seq, rcu);
  730. }
  731. void tipc_nametbl_stop(struct net *net)
  732. {
  733. u32 i;
  734. struct name_seq *seq;
  735. struct hlist_head *seq_head;
  736. struct tipc_net *tn = net_generic(net, tipc_net_id);
  737. struct name_table *tipc_nametbl = tn->nametbl;
  738. /* Verify name table is empty and purge any lingering
  739. * publications, then release the name table
  740. */
  741. spin_lock_bh(&tn->nametbl_lock);
  742. for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
  743. if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
  744. continue;
  745. seq_head = &tipc_nametbl->seq_hlist[i];
  746. hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
  747. tipc_purge_publications(net, seq);
  748. }
  749. }
  750. spin_unlock_bh(&tn->nametbl_lock);
  751. synchronize_net();
  752. kfree(tipc_nametbl);
  753. }
  754. static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
  755. struct name_seq *seq,
  756. struct sub_seq *sseq, u32 *last_publ)
  757. {
  758. void *hdr;
  759. struct nlattr *attrs;
  760. struct nlattr *publ;
  761. struct publication *p;
  762. if (*last_publ) {
  763. list_for_each_entry(p, &sseq->info->zone_list, zone_list)
  764. if (p->key == *last_publ)
  765. break;
  766. if (p->key != *last_publ)
  767. return -EPIPE;
  768. } else {
  769. p = list_first_entry(&sseq->info->zone_list, struct publication,
  770. zone_list);
  771. }
  772. list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
  773. *last_publ = p->key;
  774. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
  775. &tipc_genl_family, NLM_F_MULTI,
  776. TIPC_NL_NAME_TABLE_GET);
  777. if (!hdr)
  778. return -EMSGSIZE;
  779. attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
  780. if (!attrs)
  781. goto msg_full;
  782. publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
  783. if (!publ)
  784. goto attr_msg_full;
  785. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
  786. goto publ_msg_full;
  787. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
  788. goto publ_msg_full;
  789. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
  790. goto publ_msg_full;
  791. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
  792. goto publ_msg_full;
  793. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
  794. goto publ_msg_full;
  795. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
  796. goto publ_msg_full;
  797. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
  798. goto publ_msg_full;
  799. nla_nest_end(msg->skb, publ);
  800. nla_nest_end(msg->skb, attrs);
  801. genlmsg_end(msg->skb, hdr);
  802. }
  803. *last_publ = 0;
  804. return 0;
  805. publ_msg_full:
  806. nla_nest_cancel(msg->skb, publ);
  807. attr_msg_full:
  808. nla_nest_cancel(msg->skb, attrs);
  809. msg_full:
  810. genlmsg_cancel(msg->skb, hdr);
  811. return -EMSGSIZE;
  812. }
  813. static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
  814. u32 *last_lower, u32 *last_publ)
  815. {
  816. struct sub_seq *sseq;
  817. struct sub_seq *sseq_start;
  818. int err;
  819. if (*last_lower) {
  820. sseq_start = nameseq_find_subseq(seq, *last_lower);
  821. if (!sseq_start)
  822. return -EPIPE;
  823. } else {
  824. sseq_start = seq->sseqs;
  825. }
  826. for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
  827. err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
  828. if (err) {
  829. *last_lower = sseq->lower;
  830. return err;
  831. }
  832. }
  833. *last_lower = 0;
  834. return 0;
  835. }
  836. static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
  837. u32 *last_type, u32 *last_lower, u32 *last_publ)
  838. {
  839. struct tipc_net *tn = net_generic(net, tipc_net_id);
  840. struct hlist_head *seq_head;
  841. struct name_seq *seq = NULL;
  842. int err;
  843. int i;
  844. if (*last_type)
  845. i = hash(*last_type);
  846. else
  847. i = 0;
  848. for (; i < TIPC_NAMETBL_SIZE; i++) {
  849. seq_head = &tn->nametbl->seq_hlist[i];
  850. if (*last_type) {
  851. seq = nametbl_find_seq(net, *last_type);
  852. if (!seq)
  853. return -EPIPE;
  854. } else {
  855. hlist_for_each_entry_rcu(seq, seq_head, ns_list)
  856. break;
  857. if (!seq)
  858. continue;
  859. }
  860. hlist_for_each_entry_from_rcu(seq, ns_list) {
  861. spin_lock_bh(&seq->lock);
  862. err = __tipc_nl_subseq_list(msg, seq, last_lower,
  863. last_publ);
  864. if (err) {
  865. *last_type = seq->type;
  866. spin_unlock_bh(&seq->lock);
  867. return err;
  868. }
  869. spin_unlock_bh(&seq->lock);
  870. }
  871. *last_type = 0;
  872. }
  873. return 0;
  874. }
  875. int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
  876. {
  877. int err;
  878. int done = cb->args[3];
  879. u32 last_type = cb->args[0];
  880. u32 last_lower = cb->args[1];
  881. u32 last_publ = cb->args[2];
  882. struct net *net = sock_net(skb->sk);
  883. struct tipc_nl_msg msg;
  884. if (done)
  885. return 0;
  886. msg.skb = skb;
  887. msg.portid = NETLINK_CB(cb->skb).portid;
  888. msg.seq = cb->nlh->nlmsg_seq;
  889. rcu_read_lock();
  890. err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
  891. if (!err) {
  892. done = 1;
  893. } else if (err != -EMSGSIZE) {
  894. /* We never set seq or call nl_dump_check_consistent() this
  895. * means that setting prev_seq here will cause the consistence
  896. * check to fail in the netlink callback handler. Resulting in
  897. * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
  898. * we got an error.
  899. */
  900. cb->prev_seq = 1;
  901. }
  902. rcu_read_unlock();
  903. cb->args[0] = last_type;
  904. cb->args[1] = last_lower;
  905. cb->args[2] = last_publ;
  906. cb->args[3] = done;
  907. return skb->len;
  908. }
  909. void tipc_plist_push(struct tipc_plist *pl, u32 port)
  910. {
  911. struct tipc_plist *nl;
  912. if (likely(!pl->port)) {
  913. pl->port = port;
  914. return;
  915. }
  916. if (pl->port == port)
  917. return;
  918. list_for_each_entry(nl, &pl->list, list) {
  919. if (nl->port == port)
  920. return;
  921. }
  922. nl = kmalloc(sizeof(*nl), GFP_ATOMIC);
  923. if (nl) {
  924. nl->port = port;
  925. list_add(&nl->list, &pl->list);
  926. }
  927. }
  928. u32 tipc_plist_pop(struct tipc_plist *pl)
  929. {
  930. struct tipc_plist *nl;
  931. u32 port = 0;
  932. if (likely(list_empty(&pl->list))) {
  933. port = pl->port;
  934. pl->port = 0;
  935. return port;
  936. }
  937. nl = list_first_entry(&pl->list, typeof(*nl), list);
  938. port = nl->port;
  939. list_del(&nl->list);
  940. kfree(nl);
  941. return port;
  942. }