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