name_table.c 31 KB

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