name_table.c 28 KB

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