discover.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * net/tipc/discover.c
  3. *
  4. * Copyright (c) 2003-2006, 2014-2015, Ericsson AB
  5. * Copyright (c) 2005-2006, 2010-2011, 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 "core.h"
  37. #include "link.h"
  38. #include "discover.h"
  39. /* min delay during bearer start up */
  40. #define TIPC_LINK_REQ_INIT msecs_to_jiffies(125)
  41. /* max delay if bearer has no links */
  42. #define TIPC_LINK_REQ_FAST msecs_to_jiffies(1000)
  43. /* max delay if bearer has links */
  44. #define TIPC_LINK_REQ_SLOW msecs_to_jiffies(60000)
  45. /* indicates no timer in use */
  46. #define TIPC_LINK_REQ_INACTIVE 0xffffffff
  47. /**
  48. * struct tipc_link_req - information about an ongoing link setup request
  49. * @bearer_id: identity of bearer issuing requests
  50. * @net: network namespace instance
  51. * @dest: destination address for request messages
  52. * @domain: network domain to which links can be established
  53. * @num_nodes: number of nodes currently discovered (i.e. with an active link)
  54. * @lock: spinlock for controlling access to requests
  55. * @buf: request message to be (repeatedly) sent
  56. * @timer: timer governing period between requests
  57. * @timer_intv: current interval between requests (in ms)
  58. */
  59. struct tipc_link_req {
  60. u32 bearer_id;
  61. struct tipc_media_addr dest;
  62. struct net *net;
  63. u32 domain;
  64. int num_nodes;
  65. spinlock_t lock;
  66. struct sk_buff *buf;
  67. struct timer_list timer;
  68. unsigned long timer_intv;
  69. };
  70. /**
  71. * tipc_disc_init_msg - initialize a link setup message
  72. * @net: the applicable net namespace
  73. * @type: message type (request or response)
  74. * @b_ptr: ptr to bearer issuing message
  75. */
  76. static void tipc_disc_init_msg(struct net *net, struct sk_buff *buf, u32 type,
  77. struct tipc_bearer *b_ptr)
  78. {
  79. struct tipc_net *tn = net_generic(net, tipc_net_id);
  80. struct tipc_msg *msg;
  81. u32 dest_domain = b_ptr->domain;
  82. msg = buf_msg(buf);
  83. tipc_msg_init(tn->own_addr, msg, LINK_CONFIG, type,
  84. MAX_H_SIZE, dest_domain);
  85. msg_set_non_seq(msg, 1);
  86. msg_set_node_sig(msg, tn->random);
  87. msg_set_node_capabilities(msg, 0);
  88. msg_set_dest_domain(msg, dest_domain);
  89. msg_set_bc_netid(msg, tn->net_id);
  90. b_ptr->media->addr2msg(msg_media_addr(msg), &b_ptr->addr);
  91. }
  92. /**
  93. * disc_dupl_alert - issue node address duplication alert
  94. * @b_ptr: pointer to bearer detecting duplication
  95. * @node_addr: duplicated node address
  96. * @media_addr: media address advertised by duplicated node
  97. */
  98. static void disc_dupl_alert(struct tipc_bearer *b_ptr, u32 node_addr,
  99. struct tipc_media_addr *media_addr)
  100. {
  101. char node_addr_str[16];
  102. char media_addr_str[64];
  103. tipc_addr_string_fill(node_addr_str, node_addr);
  104. tipc_media_addr_printf(media_addr_str, sizeof(media_addr_str),
  105. media_addr);
  106. pr_warn("Duplicate %s using %s seen on <%s>\n", node_addr_str,
  107. media_addr_str, b_ptr->name);
  108. }
  109. /**
  110. * tipc_disc_rcv - handle incoming discovery message (request or response)
  111. * @net: the applicable net namespace
  112. * @buf: buffer containing message
  113. * @bearer: bearer that message arrived on
  114. */
  115. void tipc_disc_rcv(struct net *net, struct sk_buff *buf,
  116. struct tipc_bearer *bearer)
  117. {
  118. struct tipc_net *tn = net_generic(net, tipc_net_id);
  119. struct tipc_node *node;
  120. struct tipc_link *link;
  121. struct tipc_media_addr maddr;
  122. struct sk_buff *rbuf;
  123. struct tipc_msg *msg = buf_msg(buf);
  124. u32 ddom = msg_dest_domain(msg);
  125. u32 onode = msg_prevnode(msg);
  126. u32 net_id = msg_bc_netid(msg);
  127. u32 mtyp = msg_type(msg);
  128. u32 signature = msg_node_sig(msg);
  129. u16 caps = msg_node_capabilities(msg);
  130. bool addr_match = false;
  131. bool sign_match = false;
  132. bool link_up = false;
  133. bool accept_addr = false;
  134. bool accept_sign = false;
  135. bool respond = false;
  136. bearer->media->msg2addr(bearer, &maddr, msg_media_addr(msg));
  137. kfree_skb(buf);
  138. /* Ensure message from node is valid and communication is permitted */
  139. if (net_id != tn->net_id)
  140. return;
  141. if (maddr.broadcast)
  142. return;
  143. if (!tipc_addr_domain_valid(ddom))
  144. return;
  145. if (!tipc_addr_node_valid(onode))
  146. return;
  147. if (in_own_node(net, onode)) {
  148. if (memcmp(&maddr, &bearer->addr, sizeof(maddr)))
  149. disc_dupl_alert(bearer, tn->own_addr, &maddr);
  150. return;
  151. }
  152. if (!tipc_in_scope(ddom, tn->own_addr))
  153. return;
  154. if (!tipc_in_scope(bearer->domain, onode))
  155. return;
  156. node = tipc_node_create(net, onode);
  157. if (!node)
  158. return;
  159. tipc_node_lock(node);
  160. node->capabilities = caps;
  161. link = node->links[bearer->identity];
  162. /* Prepare to validate requesting node's signature and media address */
  163. sign_match = (signature == node->signature);
  164. addr_match = link && !memcmp(&link->media_addr, &maddr, sizeof(maddr));
  165. link_up = link && tipc_link_is_up(link);
  166. /* These three flags give us eight permutations: */
  167. if (sign_match && addr_match && link_up) {
  168. /* All is fine. Do nothing. */
  169. } else if (sign_match && addr_match && !link_up) {
  170. /* Respond. The link will come up in due time */
  171. respond = true;
  172. } else if (sign_match && !addr_match && link_up) {
  173. /* Peer has changed i/f address without rebooting.
  174. * If so, the link will reset soon, and the next
  175. * discovery will be accepted. So we can ignore it.
  176. * It may also be an cloned or malicious peer having
  177. * chosen the same node address and signature as an
  178. * existing one.
  179. * Ignore requests until the link goes down, if ever.
  180. */
  181. disc_dupl_alert(bearer, onode, &maddr);
  182. } else if (sign_match && !addr_match && !link_up) {
  183. /* Peer link has changed i/f address without rebooting.
  184. * It may also be a cloned or malicious peer; we can't
  185. * distinguish between the two.
  186. * The signature is correct, so we must accept.
  187. */
  188. accept_addr = true;
  189. respond = true;
  190. } else if (!sign_match && addr_match && link_up) {
  191. /* Peer node rebooted. Two possibilities:
  192. * - Delayed re-discovery; this link endpoint has already
  193. * reset and re-established contact with the peer, before
  194. * receiving a discovery message from that node.
  195. * (The peer happened to receive one from this node first).
  196. * - The peer came back so fast that our side has not
  197. * discovered it yet. Probing from this side will soon
  198. * reset the link, since there can be no working link
  199. * endpoint at the peer end, and the link will re-establish.
  200. * Accept the signature, since it comes from a known peer.
  201. */
  202. accept_sign = true;
  203. } else if (!sign_match && addr_match && !link_up) {
  204. /* The peer node has rebooted.
  205. * Accept signature, since it is a known peer.
  206. */
  207. accept_sign = true;
  208. respond = true;
  209. } else if (!sign_match && !addr_match && link_up) {
  210. /* Peer rebooted with new address, or a new/duplicate peer.
  211. * Ignore until the link goes down, if ever.
  212. */
  213. disc_dupl_alert(bearer, onode, &maddr);
  214. } else if (!sign_match && !addr_match && !link_up) {
  215. /* Peer rebooted with new address, or it is a new peer.
  216. * Accept signature and address.
  217. */
  218. accept_sign = true;
  219. accept_addr = true;
  220. respond = true;
  221. }
  222. if (accept_sign)
  223. node->signature = signature;
  224. if (accept_addr) {
  225. if (!link)
  226. link = tipc_link_create(node, bearer, &maddr);
  227. if (link) {
  228. memcpy(&link->media_addr, &maddr, sizeof(maddr));
  229. tipc_link_reset(link);
  230. } else {
  231. respond = false;
  232. }
  233. }
  234. /* Send response, if necessary */
  235. if (respond && (mtyp == DSC_REQ_MSG)) {
  236. rbuf = tipc_buf_acquire(MAX_H_SIZE);
  237. if (rbuf) {
  238. tipc_disc_init_msg(net, rbuf, DSC_RESP_MSG, bearer);
  239. tipc_bearer_send(net, bearer->identity, rbuf, &maddr);
  240. kfree_skb(rbuf);
  241. }
  242. }
  243. tipc_node_unlock(node);
  244. tipc_node_put(node);
  245. }
  246. /**
  247. * disc_update - update frequency of periodic link setup requests
  248. * @req: ptr to link request structure
  249. *
  250. * Reinitiates discovery process if discovery object has no associated nodes
  251. * and is either not currently searching or is searching at a slow rate
  252. */
  253. static void disc_update(struct tipc_link_req *req)
  254. {
  255. if (!req->num_nodes) {
  256. if ((req->timer_intv == TIPC_LINK_REQ_INACTIVE) ||
  257. (req->timer_intv > TIPC_LINK_REQ_FAST)) {
  258. req->timer_intv = TIPC_LINK_REQ_INIT;
  259. mod_timer(&req->timer, jiffies + req->timer_intv);
  260. }
  261. }
  262. }
  263. /**
  264. * tipc_disc_add_dest - increment set of discovered nodes
  265. * @req: ptr to link request structure
  266. */
  267. void tipc_disc_add_dest(struct tipc_link_req *req)
  268. {
  269. spin_lock_bh(&req->lock);
  270. req->num_nodes++;
  271. spin_unlock_bh(&req->lock);
  272. }
  273. /**
  274. * tipc_disc_remove_dest - decrement set of discovered nodes
  275. * @req: ptr to link request structure
  276. */
  277. void tipc_disc_remove_dest(struct tipc_link_req *req)
  278. {
  279. spin_lock_bh(&req->lock);
  280. req->num_nodes--;
  281. disc_update(req);
  282. spin_unlock_bh(&req->lock);
  283. }
  284. /**
  285. * disc_timeout - send a periodic link setup request
  286. * @data: ptr to link request structure
  287. *
  288. * Called whenever a link setup request timer associated with a bearer expires.
  289. */
  290. static void disc_timeout(unsigned long data)
  291. {
  292. struct tipc_link_req *req = (struct tipc_link_req *)data;
  293. int max_delay;
  294. spin_lock_bh(&req->lock);
  295. /* Stop searching if only desired node has been found */
  296. if (tipc_node(req->domain) && req->num_nodes) {
  297. req->timer_intv = TIPC_LINK_REQ_INACTIVE;
  298. goto exit;
  299. }
  300. /*
  301. * Send discovery message, then update discovery timer
  302. *
  303. * Keep doubling time between requests until limit is reached;
  304. * hold at fast polling rate if don't have any associated nodes,
  305. * otherwise hold at slow polling rate
  306. */
  307. tipc_bearer_send(req->net, req->bearer_id, req->buf, &req->dest);
  308. req->timer_intv *= 2;
  309. if (req->num_nodes)
  310. max_delay = TIPC_LINK_REQ_SLOW;
  311. else
  312. max_delay = TIPC_LINK_REQ_FAST;
  313. if (req->timer_intv > max_delay)
  314. req->timer_intv = max_delay;
  315. mod_timer(&req->timer, jiffies + req->timer_intv);
  316. exit:
  317. spin_unlock_bh(&req->lock);
  318. }
  319. /**
  320. * tipc_disc_create - create object to send periodic link setup requests
  321. * @net: the applicable net namespace
  322. * @b_ptr: ptr to bearer issuing requests
  323. * @dest: destination address for request messages
  324. * @dest_domain: network domain to which links can be established
  325. *
  326. * Returns 0 if successful, otherwise -errno.
  327. */
  328. int tipc_disc_create(struct net *net, struct tipc_bearer *b_ptr,
  329. struct tipc_media_addr *dest)
  330. {
  331. struct tipc_link_req *req;
  332. req = kmalloc(sizeof(*req), GFP_ATOMIC);
  333. if (!req)
  334. return -ENOMEM;
  335. req->buf = tipc_buf_acquire(MAX_H_SIZE);
  336. if (!req->buf) {
  337. kfree(req);
  338. return -ENOMEM;
  339. }
  340. tipc_disc_init_msg(net, req->buf, DSC_REQ_MSG, b_ptr);
  341. memcpy(&req->dest, dest, sizeof(*dest));
  342. req->net = net;
  343. req->bearer_id = b_ptr->identity;
  344. req->domain = b_ptr->domain;
  345. req->num_nodes = 0;
  346. req->timer_intv = TIPC_LINK_REQ_INIT;
  347. spin_lock_init(&req->lock);
  348. setup_timer(&req->timer, disc_timeout, (unsigned long)req);
  349. mod_timer(&req->timer, jiffies + req->timer_intv);
  350. b_ptr->link_req = req;
  351. tipc_bearer_send(net, req->bearer_id, req->buf, &req->dest);
  352. return 0;
  353. }
  354. /**
  355. * tipc_disc_delete - destroy object sending periodic link setup requests
  356. * @req: ptr to link request structure
  357. */
  358. void tipc_disc_delete(struct tipc_link_req *req)
  359. {
  360. del_timer_sync(&req->timer);
  361. kfree_skb(req->buf);
  362. kfree(req);
  363. }
  364. /**
  365. * tipc_disc_reset - reset object to send periodic link setup requests
  366. * @net: the applicable net namespace
  367. * @b_ptr: ptr to bearer issuing requests
  368. * @dest_domain: network domain to which links can be established
  369. */
  370. void tipc_disc_reset(struct net *net, struct tipc_bearer *b_ptr)
  371. {
  372. struct tipc_link_req *req = b_ptr->link_req;
  373. spin_lock_bh(&req->lock);
  374. tipc_disc_init_msg(net, req->buf, DSC_REQ_MSG, b_ptr);
  375. req->net = net;
  376. req->bearer_id = b_ptr->identity;
  377. req->domain = b_ptr->domain;
  378. req->num_nodes = 0;
  379. req->timer_intv = TIPC_LINK_REQ_INIT;
  380. mod_timer(&req->timer, jiffies + req->timer_intv);
  381. tipc_bearer_send(net, req->bearer_id, req->buf, &req->dest);
  382. spin_unlock_bh(&req->lock);
  383. }