name_table.c 29 KB

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