name_table.c 31 KB

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