discover.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * net/tipc/discover.c
  3. *
  4. * Copyright (c) 2003-2006, 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: bearer issuing requests
  46. * @dest: destination address for request messages
  47. * @num_nodes: number of nodes currently discovered (i.e. with an active link)
  48. * @lock: spinlock for controlling access to requests
  49. * @buf: request message to be (repeatedly) sent
  50. * @timer: timer governing period between requests
  51. * @timer_intv: current interval between requests (in ms)
  52. */
  53. struct tipc_link_req {
  54. struct tipc_bearer *bearer;
  55. struct tipc_media_addr dest;
  56. int num_nodes;
  57. spinlock_t lock;
  58. struct sk_buff *buf;
  59. struct timer_list timer;
  60. unsigned int timer_intv;
  61. };
  62. /**
  63. * tipc_disc_init_msg - initialize a link setup message
  64. * @type: message type (request or response)
  65. * @b_ptr: ptr to bearer issuing message
  66. */
  67. static struct sk_buff *tipc_disc_init_msg(u32 type, struct tipc_bearer *b_ptr)
  68. {
  69. struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE);
  70. struct tipc_msg *msg;
  71. u32 dest_domain = b_ptr->domain;
  72. if (buf) {
  73. msg = buf_msg(buf);
  74. tipc_msg_init(msg, LINK_CONFIG, type, INT_H_SIZE, dest_domain);
  75. msg_set_non_seq(msg, 1);
  76. msg_set_node_sig(msg, tipc_random);
  77. msg_set_dest_domain(msg, dest_domain);
  78. msg_set_bc_netid(msg, tipc_net_id);
  79. b_ptr->media->addr2msg(&b_ptr->addr, msg_media_addr(msg));
  80. }
  81. return buf;
  82. }
  83. /**
  84. * disc_dupl_alert - issue node address duplication alert
  85. * @b_ptr: pointer to bearer detecting duplication
  86. * @node_addr: duplicated node address
  87. * @media_addr: media address advertised by duplicated node
  88. */
  89. static void disc_dupl_alert(struct tipc_bearer *b_ptr, u32 node_addr,
  90. struct tipc_media_addr *media_addr)
  91. {
  92. char node_addr_str[16];
  93. char media_addr_str[64];
  94. tipc_addr_string_fill(node_addr_str, node_addr);
  95. tipc_media_addr_printf(media_addr_str, sizeof(media_addr_str),
  96. media_addr);
  97. pr_warn("Duplicate %s using %s seen on <%s>\n", node_addr_str,
  98. media_addr_str, b_ptr->name);
  99. }
  100. /**
  101. * tipc_disc_rcv - handle incoming link setup message (request or response)
  102. * @buf: buffer containing message
  103. * @b_ptr: bearer that message arrived on
  104. */
  105. void tipc_disc_rcv(struct sk_buff *buf, struct tipc_bearer *b_ptr)
  106. {
  107. struct tipc_node *n_ptr;
  108. struct tipc_link *link;
  109. struct tipc_media_addr media_addr;
  110. struct sk_buff *rbuf;
  111. struct tipc_msg *msg = buf_msg(buf);
  112. u32 dest = msg_dest_domain(msg);
  113. u32 orig = msg_prevnode(msg);
  114. u32 net_id = msg_bc_netid(msg);
  115. u32 type = msg_type(msg);
  116. u32 signature = msg_node_sig(msg);
  117. int addr_mismatch;
  118. int link_fully_up;
  119. media_addr.broadcast = 1;
  120. b_ptr->media->msg2addr(b_ptr, &media_addr, msg_media_addr(msg));
  121. kfree_skb(buf);
  122. /* Ensure message from node is valid and communication is permitted */
  123. if (net_id != tipc_net_id)
  124. return;
  125. if (media_addr.broadcast)
  126. return;
  127. if (!tipc_addr_domain_valid(dest))
  128. return;
  129. if (!tipc_addr_node_valid(orig))
  130. return;
  131. if (orig == tipc_own_addr) {
  132. if (memcmp(&media_addr, &b_ptr->addr, sizeof(media_addr)))
  133. disc_dupl_alert(b_ptr, tipc_own_addr, &media_addr);
  134. return;
  135. }
  136. if (!tipc_in_scope(dest, tipc_own_addr))
  137. return;
  138. if (!tipc_in_scope(b_ptr->domain, orig))
  139. return;
  140. /* Locate structure corresponding to requesting node */
  141. n_ptr = tipc_node_find(orig);
  142. if (!n_ptr) {
  143. n_ptr = tipc_node_create(orig);
  144. if (!n_ptr)
  145. return;
  146. }
  147. tipc_node_lock(n_ptr);
  148. /* Prepare to validate requesting node's signature and media address */
  149. link = n_ptr->links[b_ptr->identity];
  150. addr_mismatch = (link != NULL) &&
  151. memcmp(&link->media_addr, &media_addr, sizeof(media_addr));
  152. /*
  153. * Ensure discovery message's signature is correct
  154. *
  155. * If signature is incorrect and there is no working link to the node,
  156. * accept the new signature but invalidate all existing links to the
  157. * node so they won't re-activate without a new discovery message.
  158. *
  159. * If signature is incorrect and the requested link to the node is
  160. * working, accept the new signature. (This is an instance of delayed
  161. * rediscovery, where a link endpoint was able to re-establish contact
  162. * with its peer endpoint on a node that rebooted before receiving a
  163. * discovery message from that node.)
  164. *
  165. * If signature is incorrect and there is a working link to the node
  166. * that is not the requested link, reject the request (must be from
  167. * a duplicate node).
  168. */
  169. if (signature != n_ptr->signature) {
  170. if (n_ptr->working_links == 0) {
  171. struct tipc_link *curr_link;
  172. int i;
  173. for (i = 0; i < MAX_BEARERS; i++) {
  174. curr_link = n_ptr->links[i];
  175. if (curr_link) {
  176. memset(&curr_link->media_addr, 0,
  177. sizeof(media_addr));
  178. tipc_link_reset(curr_link);
  179. }
  180. }
  181. addr_mismatch = (link != NULL);
  182. } else if (tipc_link_is_up(link) && !addr_mismatch) {
  183. /* delayed rediscovery */
  184. } else {
  185. disc_dupl_alert(b_ptr, orig, &media_addr);
  186. tipc_node_unlock(n_ptr);
  187. return;
  188. }
  189. n_ptr->signature = signature;
  190. }
  191. /*
  192. * Ensure requesting node's media address is correct
  193. *
  194. * If media address doesn't match and the link is working, reject the
  195. * request (must be from a duplicate node).
  196. *
  197. * If media address doesn't match and the link is not working, accept
  198. * the new media address and reset the link to ensure it starts up
  199. * cleanly.
  200. */
  201. if (addr_mismatch) {
  202. if (tipc_link_is_up(link)) {
  203. disc_dupl_alert(b_ptr, orig, &media_addr);
  204. tipc_node_unlock(n_ptr);
  205. return;
  206. } else {
  207. memcpy(&link->media_addr, &media_addr,
  208. sizeof(media_addr));
  209. tipc_link_reset(link);
  210. }
  211. }
  212. /* Create a link endpoint for this bearer, if necessary */
  213. if (!link) {
  214. link = tipc_link_create(n_ptr, b_ptr, &media_addr);
  215. if (!link) {
  216. tipc_node_unlock(n_ptr);
  217. return;
  218. }
  219. }
  220. /* Accept discovery message & send response, if necessary */
  221. link_fully_up = link_working_working(link);
  222. if ((type == DSC_REQ_MSG) && !link_fully_up) {
  223. rbuf = tipc_disc_init_msg(DSC_RESP_MSG, b_ptr);
  224. if (rbuf) {
  225. tipc_bearer_send(b_ptr, rbuf, &media_addr);
  226. kfree_skb(rbuf);
  227. }
  228. }
  229. tipc_node_unlock(n_ptr);
  230. }
  231. /**
  232. * disc_update - update frequency of periodic link setup requests
  233. * @req: ptr to link request structure
  234. *
  235. * Reinitiates discovery process if discovery object has no associated nodes
  236. * and is either not currently searching or is searching at a slow rate
  237. */
  238. static void disc_update(struct tipc_link_req *req)
  239. {
  240. if (!req->num_nodes) {
  241. if ((req->timer_intv == TIPC_LINK_REQ_INACTIVE) ||
  242. (req->timer_intv > TIPC_LINK_REQ_FAST)) {
  243. req->timer_intv = TIPC_LINK_REQ_INIT;
  244. k_start_timer(&req->timer, req->timer_intv);
  245. }
  246. }
  247. }
  248. /**
  249. * tipc_disc_add_dest - increment set of discovered nodes
  250. * @req: ptr to link request structure
  251. */
  252. void tipc_disc_add_dest(struct tipc_link_req *req)
  253. {
  254. spin_lock_bh(&req->lock);
  255. req->num_nodes++;
  256. spin_unlock_bh(&req->lock);
  257. }
  258. /**
  259. * tipc_disc_remove_dest - decrement set of discovered nodes
  260. * @req: ptr to link request structure
  261. */
  262. void tipc_disc_remove_dest(struct tipc_link_req *req)
  263. {
  264. spin_lock_bh(&req->lock);
  265. req->num_nodes--;
  266. disc_update(req);
  267. spin_unlock_bh(&req->lock);
  268. }
  269. /**
  270. * disc_timeout - send a periodic link setup request
  271. * @req: ptr to link request structure
  272. *
  273. * Called whenever a link setup request timer associated with a bearer expires.
  274. */
  275. static void disc_timeout(struct tipc_link_req *req)
  276. {
  277. int max_delay;
  278. spin_lock_bh(&req->lock);
  279. /* Stop searching if only desired node has been found */
  280. if (tipc_node(req->bearer->domain) && req->num_nodes) {
  281. req->timer_intv = TIPC_LINK_REQ_INACTIVE;
  282. goto exit;
  283. }
  284. /*
  285. * Send discovery message, then update discovery timer
  286. *
  287. * Keep doubling time between requests until limit is reached;
  288. * hold at fast polling rate if don't have any associated nodes,
  289. * otherwise hold at slow polling rate
  290. */
  291. tipc_bearer_send(req->bearer, req->buf, &req->dest);
  292. req->timer_intv *= 2;
  293. if (req->num_nodes)
  294. max_delay = TIPC_LINK_REQ_SLOW;
  295. else
  296. max_delay = TIPC_LINK_REQ_FAST;
  297. if (req->timer_intv > max_delay)
  298. req->timer_intv = max_delay;
  299. k_start_timer(&req->timer, req->timer_intv);
  300. exit:
  301. spin_unlock_bh(&req->lock);
  302. }
  303. /**
  304. * tipc_disc_create - create object to send periodic link setup requests
  305. * @b_ptr: ptr to bearer issuing requests
  306. * @dest: destination address for request messages
  307. * @dest_domain: network domain to which links can be established
  308. *
  309. * Returns 0 if successful, otherwise -errno.
  310. */
  311. int tipc_disc_create(struct tipc_bearer *b_ptr, struct tipc_media_addr *dest)
  312. {
  313. struct tipc_link_req *req;
  314. req = kmalloc(sizeof(*req), GFP_ATOMIC);
  315. if (!req)
  316. return -ENOMEM;
  317. req->buf = tipc_disc_init_msg(DSC_REQ_MSG, b_ptr);
  318. if (!req->buf) {
  319. kfree(req);
  320. return -ENOMSG;
  321. }
  322. memcpy(&req->dest, dest, sizeof(*dest));
  323. req->bearer = b_ptr;
  324. req->num_nodes = 0;
  325. req->timer_intv = TIPC_LINK_REQ_INIT;
  326. spin_lock_init(&req->lock);
  327. k_init_timer(&req->timer, (Handler)disc_timeout, (unsigned long)req);
  328. k_start_timer(&req->timer, req->timer_intv);
  329. b_ptr->link_req = req;
  330. tipc_bearer_send(req->bearer, req->buf, &req->dest);
  331. return 0;
  332. }
  333. /**
  334. * tipc_disc_delete - destroy object sending periodic link setup requests
  335. * @req: ptr to link request structure
  336. */
  337. void tipc_disc_delete(struct tipc_link_req *req)
  338. {
  339. k_cancel_timer(&req->timer);
  340. k_term_timer(&req->timer);
  341. kfree_skb(req->buf);
  342. kfree(req);
  343. }