discover.c 12 KB

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