discover.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "node.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 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)
  78. {
  79. struct tipc_net *tn = net_generic(net, tipc_net_id);
  80. struct tipc_msg *msg;
  81. u32 dest_domain = b->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, TIPC_NODE_CAPABILITIES);
  88. msg_set_dest_domain(msg, dest_domain);
  89. msg_set_bc_netid(msg, tn->net_id);
  90. b->media->addr2msg(msg_media_addr(msg), &b->addr);
  91. }
  92. /**
  93. * disc_dupl_alert - issue node address duplication alert
  94. * @b: 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, 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->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 *skb,
  116. struct tipc_bearer *bearer)
  117. {
  118. struct tipc_net *tn = net_generic(net, tipc_net_id);
  119. struct tipc_media_addr maddr;
  120. struct sk_buff *rskb;
  121. struct tipc_msg *hdr = buf_msg(skb);
  122. u32 ddom = msg_dest_domain(hdr);
  123. u32 onode = msg_prevnode(hdr);
  124. u32 net_id = msg_bc_netid(hdr);
  125. u32 mtyp = msg_type(hdr);
  126. u32 signature = msg_node_sig(hdr);
  127. u16 caps = msg_node_capabilities(hdr);
  128. bool respond = false;
  129. bool dupl_addr = false;
  130. int err;
  131. err = bearer->media->msg2addr(bearer, &maddr, msg_media_addr(hdr));
  132. kfree_skb(skb);
  133. if (err)
  134. return;
  135. /* Ensure message from node is valid and communication is permitted */
  136. if (net_id != tn->net_id)
  137. return;
  138. if (maddr.broadcast)
  139. return;
  140. if (!tipc_addr_domain_valid(ddom))
  141. return;
  142. if (!tipc_addr_node_valid(onode))
  143. return;
  144. if (in_own_node(net, onode)) {
  145. if (memcmp(&maddr, &bearer->addr, sizeof(maddr)))
  146. disc_dupl_alert(bearer, tn->own_addr, &maddr);
  147. return;
  148. }
  149. if (!tipc_in_scope(ddom, tn->own_addr))
  150. return;
  151. if (!tipc_in_scope(bearer->domain, onode))
  152. return;
  153. tipc_node_check_dest(net, onode, bearer, caps, signature,
  154. &maddr, &respond, &dupl_addr);
  155. if (dupl_addr)
  156. disc_dupl_alert(bearer, onode, &maddr);
  157. /* Send response, if necessary */
  158. if (respond && (mtyp == DSC_REQ_MSG)) {
  159. rskb = tipc_buf_acquire(MAX_H_SIZE, GFP_ATOMIC);
  160. if (!rskb)
  161. return;
  162. tipc_disc_init_msg(net, rskb, DSC_RESP_MSG, bearer);
  163. tipc_bearer_xmit_skb(net, bearer->identity, rskb, &maddr);
  164. }
  165. }
  166. /**
  167. * disc_update - update frequency of periodic link setup requests
  168. * @req: ptr to link request structure
  169. *
  170. * Reinitiates discovery process if discovery object has no associated nodes
  171. * and is either not currently searching or is searching at a slow rate
  172. */
  173. static void disc_update(struct tipc_link_req *req)
  174. {
  175. if (!req->num_nodes) {
  176. if ((req->timer_intv == TIPC_LINK_REQ_INACTIVE) ||
  177. (req->timer_intv > TIPC_LINK_REQ_FAST)) {
  178. req->timer_intv = TIPC_LINK_REQ_INIT;
  179. mod_timer(&req->timer, jiffies + req->timer_intv);
  180. }
  181. }
  182. }
  183. /**
  184. * tipc_disc_add_dest - increment set of discovered nodes
  185. * @req: ptr to link request structure
  186. */
  187. void tipc_disc_add_dest(struct tipc_link_req *req)
  188. {
  189. spin_lock_bh(&req->lock);
  190. req->num_nodes++;
  191. spin_unlock_bh(&req->lock);
  192. }
  193. /**
  194. * tipc_disc_remove_dest - decrement set of discovered nodes
  195. * @req: ptr to link request structure
  196. */
  197. void tipc_disc_remove_dest(struct tipc_link_req *req)
  198. {
  199. spin_lock_bh(&req->lock);
  200. req->num_nodes--;
  201. disc_update(req);
  202. spin_unlock_bh(&req->lock);
  203. }
  204. /**
  205. * disc_timeout - send a periodic link setup request
  206. * @data: ptr to link request structure
  207. *
  208. * Called whenever a link setup request timer associated with a bearer expires.
  209. */
  210. static void disc_timeout(unsigned long data)
  211. {
  212. struct tipc_link_req *req = (struct tipc_link_req *)data;
  213. struct sk_buff *skb;
  214. int max_delay;
  215. spin_lock_bh(&req->lock);
  216. /* Stop searching if only desired node has been found */
  217. if (tipc_node(req->domain) && req->num_nodes) {
  218. req->timer_intv = TIPC_LINK_REQ_INACTIVE;
  219. goto exit;
  220. }
  221. /*
  222. * Send discovery message, then update discovery timer
  223. *
  224. * Keep doubling time between requests until limit is reached;
  225. * hold at fast polling rate if don't have any associated nodes,
  226. * otherwise hold at slow polling rate
  227. */
  228. skb = skb_clone(req->buf, GFP_ATOMIC);
  229. if (skb)
  230. tipc_bearer_xmit_skb(req->net, req->bearer_id, skb, &req->dest);
  231. req->timer_intv *= 2;
  232. if (req->num_nodes)
  233. max_delay = TIPC_LINK_REQ_SLOW;
  234. else
  235. max_delay = TIPC_LINK_REQ_FAST;
  236. if (req->timer_intv > max_delay)
  237. req->timer_intv = max_delay;
  238. mod_timer(&req->timer, jiffies + req->timer_intv);
  239. exit:
  240. spin_unlock_bh(&req->lock);
  241. }
  242. /**
  243. * tipc_disc_create - create object to send periodic link setup requests
  244. * @net: the applicable net namespace
  245. * @b: ptr to bearer issuing requests
  246. * @dest: destination address for request messages
  247. * @dest_domain: network domain to which links can be established
  248. *
  249. * Returns 0 if successful, otherwise -errno.
  250. */
  251. int tipc_disc_create(struct net *net, struct tipc_bearer *b,
  252. struct tipc_media_addr *dest, struct sk_buff **skb)
  253. {
  254. struct tipc_link_req *req;
  255. req = kmalloc(sizeof(*req), GFP_ATOMIC);
  256. if (!req)
  257. return -ENOMEM;
  258. req->buf = tipc_buf_acquire(MAX_H_SIZE, GFP_ATOMIC);
  259. if (!req->buf) {
  260. kfree(req);
  261. return -ENOMEM;
  262. }
  263. tipc_disc_init_msg(net, req->buf, DSC_REQ_MSG, b);
  264. memcpy(&req->dest, dest, sizeof(*dest));
  265. req->net = net;
  266. req->bearer_id = b->identity;
  267. req->domain = b->domain;
  268. req->num_nodes = 0;
  269. req->timer_intv = TIPC_LINK_REQ_INIT;
  270. spin_lock_init(&req->lock);
  271. setup_timer(&req->timer, disc_timeout, (unsigned long)req);
  272. mod_timer(&req->timer, jiffies + req->timer_intv);
  273. b->link_req = req;
  274. *skb = skb_clone(req->buf, GFP_ATOMIC);
  275. return 0;
  276. }
  277. /**
  278. * tipc_disc_delete - destroy object sending periodic link setup requests
  279. * @req: ptr to link request structure
  280. */
  281. void tipc_disc_delete(struct tipc_link_req *req)
  282. {
  283. del_timer_sync(&req->timer);
  284. kfree_skb(req->buf);
  285. kfree(req);
  286. }
  287. /**
  288. * tipc_disc_reset - reset object to send periodic link setup requests
  289. * @net: the applicable net namespace
  290. * @b: ptr to bearer issuing requests
  291. * @dest_domain: network domain to which links can be established
  292. */
  293. void tipc_disc_reset(struct net *net, struct tipc_bearer *b)
  294. {
  295. struct tipc_link_req *req = b->link_req;
  296. struct sk_buff *skb;
  297. spin_lock_bh(&req->lock);
  298. tipc_disc_init_msg(net, req->buf, DSC_REQ_MSG, b);
  299. req->net = net;
  300. req->bearer_id = b->identity;
  301. req->domain = b->domain;
  302. req->num_nodes = 0;
  303. req->timer_intv = TIPC_LINK_REQ_INIT;
  304. mod_timer(&req->timer, jiffies + req->timer_intv);
  305. skb = skb_clone(req->buf, GFP_ATOMIC);
  306. if (skb)
  307. tipc_bearer_xmit_skb(net, req->bearer_id, skb, &req->dest);
  308. spin_unlock_bh(&req->lock);
  309. }