name_table.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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_subscrp_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_subscrp_report_overlap(s, publ->lower, publ->upper,
  359. TIPC_WITHDRAWN, publ->ref,
  360. publ->node, publ->scope,
  361. removed_subseq);
  362. }
  363. return publ;
  364. }
  365. /**
  366. * tipc_nameseq_subscribe - attach a subscription, and optionally
  367. * issue the prescribed number of events if there is any sub-
  368. * sequence overlapping with the requested sequence
  369. */
  370. static void tipc_nameseq_subscribe(struct name_seq *nseq,
  371. struct tipc_subscription *s,
  372. bool status)
  373. {
  374. struct sub_seq *sseq = nseq->sseqs;
  375. struct tipc_name_seq ns;
  376. tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
  377. tipc_subscrp_get(s);
  378. list_add(&s->nameseq_list, &nseq->subscriptions);
  379. if (!status || !sseq)
  380. return;
  381. while (sseq != &nseq->sseqs[nseq->first_free]) {
  382. if (tipc_subscrp_check_overlap(&ns, sseq->lower, sseq->upper)) {
  383. struct publication *crs;
  384. struct name_info *info = sseq->info;
  385. int must_report = 1;
  386. list_for_each_entry(crs, &info->zone_list, zone_list) {
  387. tipc_subscrp_report_overlap(s, sseq->lower,
  388. sseq->upper,
  389. TIPC_PUBLISHED,
  390. crs->ref, crs->node,
  391. crs->scope,
  392. must_report);
  393. must_report = 0;
  394. }
  395. }
  396. sseq++;
  397. }
  398. }
  399. static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
  400. {
  401. struct tipc_net *tn = net_generic(net, tipc_net_id);
  402. struct hlist_head *seq_head;
  403. struct name_seq *ns;
  404. seq_head = &tn->nametbl->seq_hlist[hash(type)];
  405. hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
  406. if (ns->type == type)
  407. return ns;
  408. }
  409. return NULL;
  410. };
  411. struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
  412. u32 lower, u32 upper, u32 scope,
  413. u32 node, u32 port, u32 key)
  414. {
  415. struct tipc_net *tn = net_generic(net, tipc_net_id);
  416. struct publication *publ;
  417. struct name_seq *seq = nametbl_find_seq(net, type);
  418. int index = hash(type);
  419. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
  420. (lower > upper)) {
  421. pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
  422. type, lower, upper, scope);
  423. return NULL;
  424. }
  425. if (!seq)
  426. seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
  427. if (!seq)
  428. return NULL;
  429. spin_lock_bh(&seq->lock);
  430. publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
  431. scope, node, port, key);
  432. spin_unlock_bh(&seq->lock);
  433. return publ;
  434. }
  435. struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
  436. u32 lower, u32 node, u32 ref,
  437. u32 key)
  438. {
  439. struct publication *publ;
  440. struct name_seq *seq = nametbl_find_seq(net, type);
  441. if (!seq)
  442. return NULL;
  443. spin_lock_bh(&seq->lock);
  444. publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
  445. if (!seq->first_free && list_empty(&seq->subscriptions)) {
  446. hlist_del_init_rcu(&seq->ns_list);
  447. kfree(seq->sseqs);
  448. spin_unlock_bh(&seq->lock);
  449. kfree_rcu(seq, rcu);
  450. return publ;
  451. }
  452. spin_unlock_bh(&seq->lock);
  453. return publ;
  454. }
  455. /**
  456. * tipc_nametbl_translate - perform name translation
  457. *
  458. * On entry, 'destnode' is the search domain used during translation.
  459. *
  460. * On exit:
  461. * - if name translation is deferred to another node/cluster/zone,
  462. * leaves 'destnode' unchanged (will be non-zero) and returns 0
  463. * - if name translation is attempted and succeeds, sets 'destnode'
  464. * to publishing node and returns port reference (will be non-zero)
  465. * - if name translation is attempted and fails, sets 'destnode' to 0
  466. * and returns 0
  467. */
  468. u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
  469. u32 *destnode)
  470. {
  471. struct tipc_net *tn = net_generic(net, tipc_net_id);
  472. struct sub_seq *sseq;
  473. struct name_info *info;
  474. struct publication *publ;
  475. struct name_seq *seq;
  476. u32 ref = 0;
  477. u32 node = 0;
  478. if (!tipc_in_scope(*destnode, tn->own_addr))
  479. return 0;
  480. rcu_read_lock();
  481. seq = nametbl_find_seq(net, type);
  482. if (unlikely(!seq))
  483. goto not_found;
  484. spin_lock_bh(&seq->lock);
  485. sseq = nameseq_find_subseq(seq, instance);
  486. if (unlikely(!sseq))
  487. goto no_match;
  488. info = sseq->info;
  489. /* Closest-First Algorithm */
  490. if (likely(!*destnode)) {
  491. if (!list_empty(&info->node_list)) {
  492. publ = list_first_entry(&info->node_list,
  493. struct publication,
  494. node_list);
  495. list_move_tail(&publ->node_list,
  496. &info->node_list);
  497. } else if (!list_empty(&info->cluster_list)) {
  498. publ = list_first_entry(&info->cluster_list,
  499. struct publication,
  500. cluster_list);
  501. list_move_tail(&publ->cluster_list,
  502. &info->cluster_list);
  503. } else {
  504. publ = list_first_entry(&info->zone_list,
  505. struct publication,
  506. zone_list);
  507. list_move_tail(&publ->zone_list,
  508. &info->zone_list);
  509. }
  510. }
  511. /* Round-Robin Algorithm */
  512. else if (*destnode == tn->own_addr) {
  513. if (list_empty(&info->node_list))
  514. goto no_match;
  515. publ = list_first_entry(&info->node_list, struct publication,
  516. node_list);
  517. list_move_tail(&publ->node_list, &info->node_list);
  518. } else if (in_own_cluster_exact(net, *destnode)) {
  519. if (list_empty(&info->cluster_list))
  520. goto no_match;
  521. publ = list_first_entry(&info->cluster_list, struct publication,
  522. cluster_list);
  523. list_move_tail(&publ->cluster_list, &info->cluster_list);
  524. } else {
  525. publ = list_first_entry(&info->zone_list, struct publication,
  526. zone_list);
  527. list_move_tail(&publ->zone_list, &info->zone_list);
  528. }
  529. ref = publ->ref;
  530. node = publ->node;
  531. no_match:
  532. spin_unlock_bh(&seq->lock);
  533. not_found:
  534. rcu_read_unlock();
  535. *destnode = node;
  536. return ref;
  537. }
  538. bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 scope,
  539. struct list_head *dsts, int *dstcnt, u32 exclude,
  540. bool all)
  541. {
  542. u32 self = tipc_own_addr(net);
  543. struct publication *publ;
  544. struct name_info *info;
  545. struct name_seq *seq;
  546. struct sub_seq *sseq;
  547. *dstcnt = 0;
  548. rcu_read_lock();
  549. seq = nametbl_find_seq(net, type);
  550. if (unlikely(!seq))
  551. goto exit;
  552. spin_lock_bh(&seq->lock);
  553. sseq = nameseq_find_subseq(seq, instance);
  554. if (likely(sseq)) {
  555. info = sseq->info;
  556. list_for_each_entry(publ, &info->zone_list, zone_list) {
  557. if (publ->scope != scope)
  558. continue;
  559. if (publ->ref == exclude && publ->node == self)
  560. continue;
  561. tipc_dest_push(dsts, publ->node, publ->ref);
  562. (*dstcnt)++;
  563. if (all)
  564. continue;
  565. list_move_tail(&publ->zone_list, &info->zone_list);
  566. break;
  567. }
  568. }
  569. spin_unlock_bh(&seq->lock);
  570. exit:
  571. rcu_read_unlock();
  572. return !list_empty(dsts);
  573. }
  574. int tipc_nametbl_mc_lookup(struct net *net, u32 type, u32 lower, u32 upper,
  575. u32 scope, bool exact, struct list_head *dports)
  576. {
  577. struct sub_seq *sseq_stop;
  578. struct name_info *info;
  579. struct publication *p;
  580. struct name_seq *seq;
  581. struct sub_seq *sseq;
  582. int res = 0;
  583. rcu_read_lock();
  584. seq = nametbl_find_seq(net, type);
  585. if (!seq)
  586. goto exit;
  587. spin_lock_bh(&seq->lock);
  588. sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
  589. sseq_stop = seq->sseqs + seq->first_free;
  590. for (; sseq != sseq_stop; sseq++) {
  591. if (sseq->lower > upper)
  592. break;
  593. info = sseq->info;
  594. list_for_each_entry(p, &info->node_list, node_list) {
  595. if (p->scope == scope || (!exact && p->scope < scope))
  596. tipc_dest_push(dports, 0, p->ref);
  597. }
  598. if (info->cluster_list_size != info->node_list_size)
  599. res = 1;
  600. }
  601. spin_unlock_bh(&seq->lock);
  602. exit:
  603. rcu_read_unlock();
  604. return res;
  605. }
  606. /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
  607. * - Creates list of nodes that overlap the given multicast address
  608. * - Determines if any node local ports overlap
  609. */
  610. void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
  611. u32 upper, struct tipc_nlist *nodes)
  612. {
  613. struct sub_seq *sseq, *stop;
  614. struct publication *publ;
  615. struct name_info *info;
  616. struct name_seq *seq;
  617. rcu_read_lock();
  618. seq = nametbl_find_seq(net, type);
  619. if (!seq)
  620. goto exit;
  621. spin_lock_bh(&seq->lock);
  622. sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
  623. stop = seq->sseqs + seq->first_free;
  624. for (; sseq != stop && sseq->lower <= upper; sseq++) {
  625. info = sseq->info;
  626. list_for_each_entry(publ, &info->zone_list, zone_list) {
  627. tipc_nlist_add(nodes, publ->node);
  628. }
  629. }
  630. spin_unlock_bh(&seq->lock);
  631. exit:
  632. rcu_read_unlock();
  633. }
  634. /* tipc_nametbl_build_group - build list of communication group members
  635. */
  636. void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
  637. u32 type, u32 scope)
  638. {
  639. struct sub_seq *sseq, *stop;
  640. struct name_info *info;
  641. struct publication *p;
  642. struct name_seq *seq;
  643. rcu_read_lock();
  644. seq = nametbl_find_seq(net, type);
  645. if (!seq)
  646. goto exit;
  647. spin_lock_bh(&seq->lock);
  648. sseq = seq->sseqs;
  649. stop = seq->sseqs + seq->first_free;
  650. for (; sseq != stop; sseq++) {
  651. info = sseq->info;
  652. list_for_each_entry(p, &info->zone_list, zone_list) {
  653. if (p->scope != scope)
  654. continue;
  655. tipc_group_add_member(grp, p->node, p->ref, p->lower);
  656. }
  657. }
  658. spin_unlock_bh(&seq->lock);
  659. exit:
  660. rcu_read_unlock();
  661. }
  662. /*
  663. * tipc_nametbl_publish - add name publication to network name tables
  664. */
  665. struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
  666. u32 upper, u32 scope, u32 port_ref,
  667. u32 key)
  668. {
  669. struct publication *publ;
  670. struct sk_buff *buf = NULL;
  671. struct tipc_net *tn = net_generic(net, tipc_net_id);
  672. spin_lock_bh(&tn->nametbl_lock);
  673. if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
  674. pr_warn("Publication failed, local publication limit reached (%u)\n",
  675. TIPC_MAX_PUBLICATIONS);
  676. spin_unlock_bh(&tn->nametbl_lock);
  677. return NULL;
  678. }
  679. publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
  680. tn->own_addr, port_ref, key);
  681. if (likely(publ)) {
  682. tn->nametbl->local_publ_count++;
  683. buf = tipc_named_publish(net, publ);
  684. /* Any pending external events? */
  685. tipc_named_process_backlog(net);
  686. }
  687. spin_unlock_bh(&tn->nametbl_lock);
  688. if (buf)
  689. tipc_node_broadcast(net, buf);
  690. return publ;
  691. }
  692. /**
  693. * tipc_nametbl_withdraw - withdraw name publication from network name tables
  694. */
  695. int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
  696. u32 key)
  697. {
  698. struct publication *publ;
  699. struct sk_buff *skb = NULL;
  700. struct tipc_net *tn = net_generic(net, tipc_net_id);
  701. spin_lock_bh(&tn->nametbl_lock);
  702. publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
  703. ref, key);
  704. if (likely(publ)) {
  705. tn->nametbl->local_publ_count--;
  706. skb = tipc_named_withdraw(net, publ);
  707. /* Any pending external events? */
  708. tipc_named_process_backlog(net);
  709. list_del_init(&publ->pport_list);
  710. kfree_rcu(publ, rcu);
  711. } else {
  712. pr_err("Unable to remove local publication\n"
  713. "(type=%u, lower=%u, ref=%u, key=%u)\n",
  714. type, lower, ref, key);
  715. }
  716. spin_unlock_bh(&tn->nametbl_lock);
  717. if (skb) {
  718. tipc_node_broadcast(net, skb);
  719. return 1;
  720. }
  721. return 0;
  722. }
  723. /**
  724. * tipc_nametbl_subscribe - add a subscription object to the name table
  725. */
  726. void tipc_nametbl_subscribe(struct tipc_subscription *s, bool status)
  727. {
  728. struct tipc_net *tn = net_generic(s->net, tipc_net_id);
  729. u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
  730. int index = hash(type);
  731. struct name_seq *seq;
  732. struct tipc_name_seq ns;
  733. spin_lock_bh(&tn->nametbl_lock);
  734. seq = nametbl_find_seq(s->net, type);
  735. if (!seq)
  736. seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
  737. if (seq) {
  738. spin_lock_bh(&seq->lock);
  739. tipc_nameseq_subscribe(seq, s, status);
  740. spin_unlock_bh(&seq->lock);
  741. } else {
  742. tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
  743. pr_warn("Failed to create subscription for {%u,%u,%u}\n",
  744. ns.type, ns.lower, ns.upper);
  745. }
  746. spin_unlock_bh(&tn->nametbl_lock);
  747. }
  748. /**
  749. * tipc_nametbl_unsubscribe - remove a subscription object from name table
  750. */
  751. void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
  752. {
  753. struct tipc_net *tn = net_generic(s->net, tipc_net_id);
  754. struct name_seq *seq;
  755. u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
  756. spin_lock_bh(&tn->nametbl_lock);
  757. seq = nametbl_find_seq(s->net, type);
  758. if (seq != NULL) {
  759. spin_lock_bh(&seq->lock);
  760. list_del_init(&s->nameseq_list);
  761. tipc_subscrp_put(s);
  762. if (!seq->first_free && list_empty(&seq->subscriptions)) {
  763. hlist_del_init_rcu(&seq->ns_list);
  764. kfree(seq->sseqs);
  765. spin_unlock_bh(&seq->lock);
  766. kfree_rcu(seq, rcu);
  767. } else {
  768. spin_unlock_bh(&seq->lock);
  769. }
  770. }
  771. spin_unlock_bh(&tn->nametbl_lock);
  772. }
  773. int tipc_nametbl_init(struct net *net)
  774. {
  775. struct tipc_net *tn = net_generic(net, tipc_net_id);
  776. struct name_table *tipc_nametbl;
  777. int i;
  778. tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
  779. if (!tipc_nametbl)
  780. return -ENOMEM;
  781. for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
  782. INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
  783. INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
  784. INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
  785. INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
  786. tn->nametbl = tipc_nametbl;
  787. spin_lock_init(&tn->nametbl_lock);
  788. return 0;
  789. }
  790. /**
  791. * tipc_purge_publications - remove all publications for a given type
  792. *
  793. * tipc_nametbl_lock must be held when calling this function
  794. */
  795. static void tipc_purge_publications(struct net *net, struct name_seq *seq)
  796. {
  797. struct publication *publ, *safe;
  798. struct sub_seq *sseq;
  799. struct name_info *info;
  800. spin_lock_bh(&seq->lock);
  801. sseq = seq->sseqs;
  802. info = sseq->info;
  803. list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
  804. tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
  805. publ->ref, publ->key);
  806. kfree_rcu(publ, rcu);
  807. }
  808. hlist_del_init_rcu(&seq->ns_list);
  809. kfree(seq->sseqs);
  810. spin_unlock_bh(&seq->lock);
  811. kfree_rcu(seq, rcu);
  812. }
  813. void tipc_nametbl_stop(struct net *net)
  814. {
  815. u32 i;
  816. struct name_seq *seq;
  817. struct hlist_head *seq_head;
  818. struct tipc_net *tn = net_generic(net, tipc_net_id);
  819. struct name_table *tipc_nametbl = tn->nametbl;
  820. /* Verify name table is empty and purge any lingering
  821. * publications, then release the name table
  822. */
  823. spin_lock_bh(&tn->nametbl_lock);
  824. for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
  825. if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
  826. continue;
  827. seq_head = &tipc_nametbl->seq_hlist[i];
  828. hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
  829. tipc_purge_publications(net, seq);
  830. }
  831. }
  832. spin_unlock_bh(&tn->nametbl_lock);
  833. synchronize_net();
  834. kfree(tipc_nametbl);
  835. }
  836. static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
  837. struct name_seq *seq,
  838. struct sub_seq *sseq, u32 *last_publ)
  839. {
  840. void *hdr;
  841. struct nlattr *attrs;
  842. struct nlattr *publ;
  843. struct publication *p;
  844. if (*last_publ) {
  845. list_for_each_entry(p, &sseq->info->zone_list, zone_list)
  846. if (p->key == *last_publ)
  847. break;
  848. if (p->key != *last_publ)
  849. return -EPIPE;
  850. } else {
  851. p = list_first_entry(&sseq->info->zone_list, struct publication,
  852. zone_list);
  853. }
  854. list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
  855. *last_publ = p->key;
  856. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
  857. &tipc_genl_family, NLM_F_MULTI,
  858. TIPC_NL_NAME_TABLE_GET);
  859. if (!hdr)
  860. return -EMSGSIZE;
  861. attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
  862. if (!attrs)
  863. goto msg_full;
  864. publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
  865. if (!publ)
  866. goto attr_msg_full;
  867. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
  868. goto publ_msg_full;
  869. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
  870. goto publ_msg_full;
  871. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
  872. goto publ_msg_full;
  873. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
  874. goto publ_msg_full;
  875. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
  876. goto publ_msg_full;
  877. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
  878. goto publ_msg_full;
  879. if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
  880. goto publ_msg_full;
  881. nla_nest_end(msg->skb, publ);
  882. nla_nest_end(msg->skb, attrs);
  883. genlmsg_end(msg->skb, hdr);
  884. }
  885. *last_publ = 0;
  886. return 0;
  887. publ_msg_full:
  888. nla_nest_cancel(msg->skb, publ);
  889. attr_msg_full:
  890. nla_nest_cancel(msg->skb, attrs);
  891. msg_full:
  892. genlmsg_cancel(msg->skb, hdr);
  893. return -EMSGSIZE;
  894. }
  895. static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
  896. u32 *last_lower, u32 *last_publ)
  897. {
  898. struct sub_seq *sseq;
  899. struct sub_seq *sseq_start;
  900. int err;
  901. if (*last_lower) {
  902. sseq_start = nameseq_find_subseq(seq, *last_lower);
  903. if (!sseq_start)
  904. return -EPIPE;
  905. } else {
  906. sseq_start = seq->sseqs;
  907. }
  908. for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
  909. err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
  910. if (err) {
  911. *last_lower = sseq->lower;
  912. return err;
  913. }
  914. }
  915. *last_lower = 0;
  916. return 0;
  917. }
  918. static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
  919. u32 *last_type, u32 *last_lower, u32 *last_publ)
  920. {
  921. struct tipc_net *tn = net_generic(net, tipc_net_id);
  922. struct hlist_head *seq_head;
  923. struct name_seq *seq = NULL;
  924. int err;
  925. int i;
  926. if (*last_type)
  927. i = hash(*last_type);
  928. else
  929. i = 0;
  930. for (; i < TIPC_NAMETBL_SIZE; i++) {
  931. seq_head = &tn->nametbl->seq_hlist[i];
  932. if (*last_type) {
  933. seq = nametbl_find_seq(net, *last_type);
  934. if (!seq)
  935. return -EPIPE;
  936. } else {
  937. hlist_for_each_entry_rcu(seq, seq_head, ns_list)
  938. break;
  939. if (!seq)
  940. continue;
  941. }
  942. hlist_for_each_entry_from_rcu(seq, ns_list) {
  943. spin_lock_bh(&seq->lock);
  944. err = __tipc_nl_subseq_list(msg, seq, last_lower,
  945. last_publ);
  946. if (err) {
  947. *last_type = seq->type;
  948. spin_unlock_bh(&seq->lock);
  949. return err;
  950. }
  951. spin_unlock_bh(&seq->lock);
  952. }
  953. *last_type = 0;
  954. }
  955. return 0;
  956. }
  957. int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
  958. {
  959. int err;
  960. int done = cb->args[3];
  961. u32 last_type = cb->args[0];
  962. u32 last_lower = cb->args[1];
  963. u32 last_publ = cb->args[2];
  964. struct net *net = sock_net(skb->sk);
  965. struct tipc_nl_msg msg;
  966. if (done)
  967. return 0;
  968. msg.skb = skb;
  969. msg.portid = NETLINK_CB(cb->skb).portid;
  970. msg.seq = cb->nlh->nlmsg_seq;
  971. rcu_read_lock();
  972. err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
  973. if (!err) {
  974. done = 1;
  975. } else if (err != -EMSGSIZE) {
  976. /* We never set seq or call nl_dump_check_consistent() this
  977. * means that setting prev_seq here will cause the consistence
  978. * check to fail in the netlink callback handler. Resulting in
  979. * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
  980. * we got an error.
  981. */
  982. cb->prev_seq = 1;
  983. }
  984. rcu_read_unlock();
  985. cb->args[0] = last_type;
  986. cb->args[1] = last_lower;
  987. cb->args[2] = last_publ;
  988. cb->args[3] = done;
  989. return skb->len;
  990. }
  991. struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
  992. {
  993. u64 value = (u64)node << 32 | port;
  994. struct tipc_dest *dst;
  995. list_for_each_entry(dst, l, list) {
  996. if (dst->value != value)
  997. continue;
  998. return dst;
  999. }
  1000. return NULL;
  1001. }
  1002. bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
  1003. {
  1004. u64 value = (u64)node << 32 | port;
  1005. struct tipc_dest *dst;
  1006. if (tipc_dest_find(l, node, port))
  1007. return false;
  1008. dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
  1009. if (unlikely(!dst))
  1010. return false;
  1011. dst->value = value;
  1012. list_add(&dst->list, l);
  1013. return true;
  1014. }
  1015. bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
  1016. {
  1017. struct tipc_dest *dst;
  1018. if (list_empty(l))
  1019. return false;
  1020. dst = list_first_entry(l, typeof(*dst), list);
  1021. if (port)
  1022. *port = dst->port;
  1023. if (node)
  1024. *node = dst->node;
  1025. list_del(&dst->list);
  1026. kfree(dst);
  1027. return true;
  1028. }
  1029. bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
  1030. {
  1031. struct tipc_dest *dst;
  1032. dst = tipc_dest_find(l, node, port);
  1033. if (!dst)
  1034. return false;
  1035. list_del(&dst->list);
  1036. kfree(dst);
  1037. return true;
  1038. }
  1039. void tipc_dest_list_purge(struct list_head *l)
  1040. {
  1041. struct tipc_dest *dst, *tmp;
  1042. list_for_each_entry_safe(dst, tmp, l, list) {
  1043. list_del(&dst->list);
  1044. kfree(dst);
  1045. }
  1046. }
  1047. int tipc_dest_list_len(struct list_head *l)
  1048. {
  1049. struct tipc_dest *dst;
  1050. int i = 0;
  1051. list_for_each_entry(dst, l, list) {
  1052. i++;
  1053. }
  1054. return i;
  1055. }