name_distr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * net/tipc/name_distr.c: TIPC name distribution code
  3. *
  4. * Copyright (c) 2000-2006, 2014-2019, Ericsson AB
  5. * Copyright (c) 2005, 2010-2011, Wind River Systems
  6. * Copyright (c) 2020-2021, Red Hat Inc
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * Alternatively, this software may be distributed under the terms of the
  22. * GNU General Public License ("GPL") version 2 as published by the Free
  23. * Software Foundation.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "core.h"
  38. #include "link.h"
  39. #include "name_distr.h"
  40. int sysctl_tipc_named_timeout __read_mostly = 2000;
  41. /**
  42. * publ_to_item - add publication info to a publication message
  43. * @p: publication info
  44. * @i: location of item in the message
  45. */
  46. static void publ_to_item(struct distr_item *i, struct publication *p)
  47. {
  48. i->type = htonl(p->sr.type);
  49. i->lower = htonl(p->sr.lower);
  50. i->upper = htonl(p->sr.upper);
  51. i->port = htonl(p->sk.ref);
  52. i->key = htonl(p->key);
  53. }
  54. /**
  55. * named_prepare_buf - allocate & initialize a publication message
  56. * @net: the associated network namespace
  57. * @type: message type
  58. * @size: payload size
  59. * @dest: destination node
  60. *
  61. * The buffer returned is of size INT_H_SIZE + payload size
  62. */
  63. static struct sk_buff *named_prepare_buf(struct net *net, u32 type, u32 size,
  64. u32 dest)
  65. {
  66. struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size, GFP_ATOMIC);
  67. u32 self = tipc_own_addr(net);
  68. struct tipc_msg *msg;
  69. if (buf != NULL) {
  70. msg = buf_msg(buf);
  71. tipc_msg_init(self, msg, NAME_DISTRIBUTOR,
  72. type, INT_H_SIZE, dest);
  73. msg_set_size(msg, INT_H_SIZE + size);
  74. }
  75. return buf;
  76. }
  77. /**
  78. * tipc_named_publish - tell other nodes about a new publication by this node
  79. * @net: the associated network namespace
  80. * @p: the new publication
  81. */
  82. struct sk_buff *tipc_named_publish(struct net *net, struct publication *p)
  83. {
  84. struct name_table *nt = tipc_name_table(net);
  85. struct distr_item *item;
  86. struct sk_buff *skb;
  87. if (p->scope == TIPC_NODE_SCOPE) {
  88. list_add_tail_rcu(&p->binding_node, &nt->node_scope);
  89. return NULL;
  90. }
  91. write_lock_bh(&nt->cluster_scope_lock);
  92. list_add_tail(&p->binding_node, &nt->cluster_scope);
  93. write_unlock_bh(&nt->cluster_scope_lock);
  94. skb = named_prepare_buf(net, PUBLICATION, ITEM_SIZE, 0);
  95. if (!skb) {
  96. pr_warn("Publication distribution failure\n");
  97. return NULL;
  98. }
  99. msg_set_named_seqno(buf_msg(skb), nt->snd_nxt++);
  100. msg_set_non_legacy(buf_msg(skb));
  101. item = (struct distr_item *)msg_data(buf_msg(skb));
  102. publ_to_item(item, p);
  103. return skb;
  104. }
  105. /**
  106. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  107. * @net: the associated network namespace
  108. * @p: the withdrawn publication
  109. */
  110. struct sk_buff *tipc_named_withdraw(struct net *net, struct publication *p)
  111. {
  112. struct name_table *nt = tipc_name_table(net);
  113. struct distr_item *item;
  114. struct sk_buff *skb;
  115. write_lock_bh(&nt->cluster_scope_lock);
  116. list_del(&p->binding_node);
  117. write_unlock_bh(&nt->cluster_scope_lock);
  118. if (p->scope == TIPC_NODE_SCOPE)
  119. return NULL;
  120. skb = named_prepare_buf(net, WITHDRAWAL, ITEM_SIZE, 0);
  121. if (!skb) {
  122. pr_warn("Withdrawal distribution failure\n");
  123. return NULL;
  124. }
  125. msg_set_named_seqno(buf_msg(skb), nt->snd_nxt++);
  126. msg_set_non_legacy(buf_msg(skb));
  127. item = (struct distr_item *)msg_data(buf_msg(skb));
  128. publ_to_item(item, p);
  129. return skb;
  130. }
  131. /**
  132. * named_distribute - prepare name info for bulk distribution to another node
  133. * @net: the associated network namespace
  134. * @list: list of messages (buffers) to be returned from this function
  135. * @dnode: node to be updated
  136. * @pls: linked list of publication items to be packed into buffer chain
  137. * @seqno: sequence number for this message
  138. */
  139. static void named_distribute(struct net *net, struct sk_buff_head *list,
  140. u32 dnode, struct list_head *pls, u16 seqno)
  141. {
  142. struct publication *publ;
  143. struct sk_buff *skb = NULL;
  144. struct distr_item *item = NULL;
  145. u32 msg_dsz = ((tipc_node_get_mtu(net, dnode, 0, false) - INT_H_SIZE) /
  146. ITEM_SIZE) * ITEM_SIZE;
  147. u32 msg_rem = msg_dsz;
  148. struct tipc_msg *hdr;
  149. list_for_each_entry(publ, pls, binding_node) {
  150. /* Prepare next buffer: */
  151. if (!skb) {
  152. skb = named_prepare_buf(net, PUBLICATION, msg_rem,
  153. dnode);
  154. if (!skb) {
  155. pr_warn("Bulk publication failure\n");
  156. return;
  157. }
  158. hdr = buf_msg(skb);
  159. msg_set_bc_ack_invalid(hdr, true);
  160. msg_set_bulk(hdr);
  161. msg_set_non_legacy(hdr);
  162. item = (struct distr_item *)msg_data(hdr);
  163. }
  164. /* Pack publication into message: */
  165. publ_to_item(item, publ);
  166. item++;
  167. msg_rem -= ITEM_SIZE;
  168. /* Append full buffer to list: */
  169. if (!msg_rem) {
  170. __skb_queue_tail(list, skb);
  171. skb = NULL;
  172. msg_rem = msg_dsz;
  173. }
  174. }
  175. if (skb) {
  176. hdr = buf_msg(skb);
  177. msg_set_size(hdr, INT_H_SIZE + (msg_dsz - msg_rem));
  178. skb_trim(skb, INT_H_SIZE + (msg_dsz - msg_rem));
  179. __skb_queue_tail(list, skb);
  180. }
  181. hdr = buf_msg(skb_peek_tail(list));
  182. msg_set_last_bulk(hdr);
  183. msg_set_named_seqno(hdr, seqno);
  184. }
  185. /**
  186. * tipc_named_node_up - tell specified node about all publications by this node
  187. * @net: the associated network namespace
  188. * @dnode: destination node
  189. * @capabilities: peer node's capabilities
  190. */
  191. void tipc_named_node_up(struct net *net, u32 dnode, u16 capabilities)
  192. {
  193. struct name_table *nt = tipc_name_table(net);
  194. struct tipc_net *tn = tipc_net(net);
  195. struct sk_buff_head head;
  196. u16 seqno;
  197. __skb_queue_head_init(&head);
  198. spin_lock_bh(&tn->nametbl_lock);
  199. if (!(capabilities & TIPC_NAMED_BCAST))
  200. nt->rc_dests++;
  201. seqno = nt->snd_nxt;
  202. spin_unlock_bh(&tn->nametbl_lock);
  203. read_lock_bh(&nt->cluster_scope_lock);
  204. named_distribute(net, &head, dnode, &nt->cluster_scope, seqno);
  205. tipc_node_xmit(net, &head, dnode, 0);
  206. read_unlock_bh(&nt->cluster_scope_lock);
  207. }
  208. /**
  209. * tipc_publ_purge - remove publication associated with a failed node
  210. * @net: the associated network namespace
  211. * @p: the publication to remove
  212. * @addr: failed node's address
  213. *
  214. * Invoked for each publication issued by a newly failed node.
  215. * Removes publication structure from name table & deletes it.
  216. */
  217. static void tipc_publ_purge(struct net *net, struct publication *p, u32 addr)
  218. {
  219. struct tipc_net *tn = tipc_net(net);
  220. struct publication *_p;
  221. struct tipc_uaddr ua;
  222. tipc_uaddr(&ua, TIPC_SERVICE_RANGE, p->scope, p->sr.type,
  223. p->sr.lower, p->sr.upper);
  224. spin_lock_bh(&tn->nametbl_lock);
  225. _p = tipc_nametbl_remove_publ(net, &ua, &p->sk, p->key);
  226. if (_p)
  227. tipc_node_unsubscribe(net, &_p->binding_node, addr);
  228. spin_unlock_bh(&tn->nametbl_lock);
  229. if (_p)
  230. kfree_rcu(_p, rcu);
  231. }
  232. void tipc_publ_notify(struct net *net, struct list_head *nsub_list,
  233. u32 addr, u16 capabilities)
  234. {
  235. struct name_table *nt = tipc_name_table(net);
  236. struct tipc_net *tn = tipc_net(net);
  237. struct publication *publ, *tmp;
  238. list_for_each_entry_safe(publ, tmp, nsub_list, binding_node)
  239. tipc_publ_purge(net, publ, addr);
  240. spin_lock_bh(&tn->nametbl_lock);
  241. if (!(capabilities & TIPC_NAMED_BCAST))
  242. nt->rc_dests--;
  243. spin_unlock_bh(&tn->nametbl_lock);
  244. }
  245. /**
  246. * tipc_update_nametbl - try to process a nametable update and notify
  247. * subscribers
  248. * @net: the associated network namespace
  249. * @i: location of item in the message
  250. * @node: node address
  251. * @dtype: name distributor message type
  252. *
  253. * tipc_nametbl_lock must be held.
  254. * Return: the publication item if successful, otherwise NULL.
  255. */
  256. static bool tipc_update_nametbl(struct net *net, struct distr_item *i,
  257. u32 node, u32 dtype)
  258. {
  259. struct publication *p = NULL;
  260. struct tipc_socket_addr sk;
  261. struct tipc_uaddr ua;
  262. u32 key = ntohl(i->key);
  263. tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_CLUSTER_SCOPE,
  264. ntohl(i->type), ntohl(i->lower), ntohl(i->upper));
  265. sk.ref = ntohl(i->port);
  266. sk.node = node;
  267. if (dtype == PUBLICATION) {
  268. p = tipc_nametbl_insert_publ(net, &ua, &sk, key);
  269. if (p) {
  270. tipc_node_subscribe(net, &p->binding_node, node);
  271. return true;
  272. }
  273. } else if (dtype == WITHDRAWAL) {
  274. p = tipc_nametbl_remove_publ(net, &ua, &sk, key);
  275. if (p) {
  276. tipc_node_unsubscribe(net, &p->binding_node, node);
  277. kfree_rcu(p, rcu);
  278. return true;
  279. }
  280. pr_warn_ratelimited("Failed to remove binding %u,%u from %u\n",
  281. ua.sr.type, ua.sr.lower, node);
  282. } else {
  283. pr_warn_ratelimited("Unknown name table message received\n");
  284. }
  285. return false;
  286. }
  287. static struct sk_buff *tipc_named_dequeue(struct sk_buff_head *namedq,
  288. u16 *rcv_nxt, bool *open)
  289. {
  290. struct sk_buff *skb, *tmp;
  291. struct tipc_msg *hdr;
  292. u16 seqno;
  293. spin_lock_bh(&namedq->lock);
  294. skb_queue_walk_safe(namedq, skb, tmp) {
  295. if (unlikely(skb_linearize(skb))) {
  296. __skb_unlink(skb, namedq);
  297. kfree_skb(skb);
  298. continue;
  299. }
  300. hdr = buf_msg(skb);
  301. seqno = msg_named_seqno(hdr);
  302. if (msg_is_last_bulk(hdr)) {
  303. *rcv_nxt = seqno;
  304. *open = true;
  305. }
  306. if (msg_is_bulk(hdr) || msg_is_legacy(hdr)) {
  307. __skb_unlink(skb, namedq);
  308. spin_unlock_bh(&namedq->lock);
  309. return skb;
  310. }
  311. if (*open && (*rcv_nxt == seqno)) {
  312. (*rcv_nxt)++;
  313. __skb_unlink(skb, namedq);
  314. spin_unlock_bh(&namedq->lock);
  315. return skb;
  316. }
  317. if (less(seqno, *rcv_nxt)) {
  318. __skb_unlink(skb, namedq);
  319. kfree_skb(skb);
  320. continue;
  321. }
  322. }
  323. spin_unlock_bh(&namedq->lock);
  324. return NULL;
  325. }
  326. /**
  327. * tipc_named_rcv - process name table update messages sent by another node
  328. * @net: the associated network namespace
  329. * @namedq: queue to receive from
  330. * @rcv_nxt: store last received seqno here
  331. * @open: last bulk msg was received (FIXME)
  332. */
  333. void tipc_named_rcv(struct net *net, struct sk_buff_head *namedq,
  334. u16 *rcv_nxt, bool *open)
  335. {
  336. struct tipc_net *tn = tipc_net(net);
  337. struct distr_item *item;
  338. struct tipc_msg *hdr;
  339. struct sk_buff *skb;
  340. u32 count, node;
  341. spin_lock_bh(&tn->nametbl_lock);
  342. while ((skb = tipc_named_dequeue(namedq, rcv_nxt, open))) {
  343. hdr = buf_msg(skb);
  344. node = msg_orignode(hdr);
  345. item = (struct distr_item *)msg_data(hdr);
  346. count = msg_data_sz(hdr) / ITEM_SIZE;
  347. while (count--) {
  348. tipc_update_nametbl(net, item, node, msg_type(hdr));
  349. item++;
  350. }
  351. kfree_skb(skb);
  352. }
  353. spin_unlock_bh(&tn->nametbl_lock);
  354. }
  355. /**
  356. * tipc_named_reinit - re-initialize local publications
  357. * @net: the associated network namespace
  358. *
  359. * This routine is called whenever TIPC networking is enabled.
  360. * All name table entries published by this node are updated to reflect
  361. * the node's new network address.
  362. */
  363. void tipc_named_reinit(struct net *net)
  364. {
  365. struct name_table *nt = tipc_name_table(net);
  366. struct tipc_net *tn = tipc_net(net);
  367. struct publication *p;
  368. u32 self = tipc_own_addr(net);
  369. spin_lock_bh(&tn->nametbl_lock);
  370. list_for_each_entry_rcu(p, &nt->node_scope, binding_node)
  371. p->sk.node = self;
  372. list_for_each_entry_rcu(p, &nt->cluster_scope, binding_node)
  373. p->sk.node = self;
  374. nt->rc_dests = 0;
  375. spin_unlock_bh(&tn->nametbl_lock);
  376. }