name_table.c 31 KB

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