name_table.c 32 KB

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