discover.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * net/tipc/discover.c
  3. *
  4. * Copyright (c) 2003-2006, 2014-2018, 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_DISC_INIT msecs_to_jiffies(125)
  41. /* max delay if bearer has no links */
  42. #define TIPC_DISC_FAST msecs_to_jiffies(1000)
  43. /* max delay if bearer has links */
  44. #define TIPC_DISC_SLOW msecs_to_jiffies(60000)
  45. /* indicates no timer in use */
  46. #define TIPC_DISC_INACTIVE 0xffffffff
  47. /**
  48. * struct tipc_discoverer - 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. * @skb: 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_discoverer {
  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 *skb;
  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 *skb,
  77. u32 mtyp, struct tipc_bearer *b)
  78. {
  79. struct tipc_net *tn = tipc_net(net);
  80. u32 dest_domain = b->domain;
  81. struct tipc_msg *hdr;
  82. hdr = buf_msg(skb);
  83. tipc_msg_init(tn->trial_addr, hdr, LINK_CONFIG, mtyp,
  84. MAX_H_SIZE, dest_domain);
  85. msg_set_size(hdr, MAX_H_SIZE + NODE_ID_LEN);
  86. msg_set_non_seq(hdr, 1);
  87. msg_set_node_sig(hdr, tn->random);
  88. msg_set_node_capabilities(hdr, TIPC_NODE_CAPABILITIES);
  89. msg_set_dest_domain(hdr, dest_domain);
  90. msg_set_bc_netid(hdr, tn->net_id);
  91. b->media->addr2msg(msg_media_addr(hdr), &b->addr);
  92. msg_set_node_id(hdr, tipc_own_id(net));
  93. }
  94. static void tipc_disc_msg_xmit(struct net *net, u32 mtyp, u32 dst,
  95. u32 src, u32 sugg_addr,
  96. struct tipc_media_addr *maddr,
  97. struct tipc_bearer *b)
  98. {
  99. struct tipc_msg *hdr;
  100. struct sk_buff *skb;
  101. skb = tipc_buf_acquire(MAX_H_SIZE + NODE_ID_LEN, GFP_ATOMIC);
  102. if (!skb)
  103. return;
  104. hdr = buf_msg(skb);
  105. tipc_disc_init_msg(net, skb, mtyp, b);
  106. msg_set_sugg_node_addr(hdr, sugg_addr);
  107. msg_set_dest_domain(hdr, dst);
  108. tipc_bearer_xmit_skb(net, b->identity, skb, maddr);
  109. }
  110. /**
  111. * disc_dupl_alert - issue node address duplication alert
  112. * @b: pointer to bearer detecting duplication
  113. * @node_addr: duplicated node address
  114. * @media_addr: media address advertised by duplicated node
  115. */
  116. static void disc_dupl_alert(struct tipc_bearer *b, u32 node_addr,
  117. struct tipc_media_addr *media_addr)
  118. {
  119. char media_addr_str[64];
  120. tipc_media_addr_printf(media_addr_str, sizeof(media_addr_str),
  121. media_addr);
  122. pr_warn("Duplicate %x using %s seen on <%s>\n", node_addr,
  123. media_addr_str, b->name);
  124. }
  125. /* tipc_disc_addr_trial(): - handle an address uniqueness trial from peer
  126. * Returns true if message should be dropped by caller, i.e., if it is a
  127. * trial message or we are inside trial period. Otherwise false.
  128. */
  129. static bool tipc_disc_addr_trial_msg(struct tipc_discoverer *d,
  130. struct tipc_media_addr *maddr,
  131. struct tipc_bearer *b,
  132. u32 dst, u32 src,
  133. u32 sugg_addr,
  134. u8 *peer_id,
  135. int mtyp)
  136. {
  137. struct net *net = d->net;
  138. struct tipc_net *tn = tipc_net(net);
  139. bool trial = time_before(jiffies, tn->addr_trial_end);
  140. u32 self = tipc_own_addr(net);
  141. if (mtyp == DSC_TRIAL_FAIL_MSG) {
  142. if (!trial)
  143. return true;
  144. /* Ignore if somebody else already gave new suggestion */
  145. if (dst != tn->trial_addr)
  146. return true;
  147. /* Otherwise update trial address and restart trial period */
  148. tn->trial_addr = sugg_addr;
  149. msg_set_prevnode(buf_msg(d->skb), sugg_addr);
  150. tn->addr_trial_end = jiffies + msecs_to_jiffies(1000);
  151. return true;
  152. }
  153. /* Apply trial address if we just left trial period */
  154. if (!trial && !self) {
  155. tipc_sched_net_finalize(net, tn->trial_addr);
  156. msg_set_prevnode(buf_msg(d->skb), tn->trial_addr);
  157. msg_set_type(buf_msg(d->skb), DSC_REQ_MSG);
  158. }
  159. /* Accept regular link requests/responses only after trial period */
  160. if (mtyp != DSC_TRIAL_MSG)
  161. return trial;
  162. sugg_addr = tipc_node_try_addr(net, peer_id, src);
  163. if (sugg_addr)
  164. tipc_disc_msg_xmit(net, DSC_TRIAL_FAIL_MSG, src,
  165. self, sugg_addr, maddr, b);
  166. return true;
  167. }
  168. /**
  169. * tipc_disc_rcv - handle incoming discovery message (request or response)
  170. * @net: applicable net namespace
  171. * @skb: buffer containing message
  172. * @b: bearer that message arrived on
  173. */
  174. void tipc_disc_rcv(struct net *net, struct sk_buff *skb,
  175. struct tipc_bearer *b)
  176. {
  177. struct tipc_net *tn = tipc_net(net);
  178. struct tipc_msg *hdr = buf_msg(skb);
  179. u16 caps = msg_node_capabilities(hdr);
  180. bool legacy = tn->legacy_addr_format;
  181. u32 sugg = msg_sugg_node_addr(hdr);
  182. u32 signature = msg_node_sig(hdr);
  183. u8 peer_id[NODE_ID_LEN] = {0,};
  184. u32 dst = msg_dest_domain(hdr);
  185. u32 net_id = msg_bc_netid(hdr);
  186. struct tipc_media_addr maddr;
  187. u32 src = msg_prevnode(hdr);
  188. u32 mtyp = msg_type(hdr);
  189. bool dupl_addr = false;
  190. bool respond = false;
  191. u32 self;
  192. int err;
  193. skb_linearize(skb);
  194. hdr = buf_msg(skb);
  195. if (caps & TIPC_NODE_ID128)
  196. memcpy(peer_id, msg_node_id(hdr), NODE_ID_LEN);
  197. else
  198. sprintf(peer_id, "%x", src);
  199. err = b->media->msg2addr(b, &maddr, msg_media_addr(hdr));
  200. kfree_skb(skb);
  201. if (err || maddr.broadcast) {
  202. pr_warn_ratelimited("Rcv corrupt discovery message\n");
  203. return;
  204. }
  205. /* Ignore discovery messages from own node */
  206. if (!memcmp(&maddr, &b->addr, sizeof(maddr)))
  207. return;
  208. if (net_id != tn->net_id)
  209. return;
  210. if (tipc_disc_addr_trial_msg(b->disc, &maddr, b, dst,
  211. src, sugg, peer_id, mtyp))
  212. return;
  213. self = tipc_own_addr(net);
  214. /* Message from somebody using this node's address */
  215. if (in_own_node(net, src)) {
  216. disc_dupl_alert(b, self, &maddr);
  217. return;
  218. }
  219. if (!tipc_in_scope(legacy, dst, self))
  220. return;
  221. if (!tipc_in_scope(legacy, b->domain, src))
  222. return;
  223. tipc_node_check_dest(net, src, peer_id, b, caps, signature,
  224. &maddr, &respond, &dupl_addr);
  225. if (dupl_addr)
  226. disc_dupl_alert(b, src, &maddr);
  227. if (!respond)
  228. return;
  229. if (mtyp != DSC_REQ_MSG)
  230. return;
  231. tipc_disc_msg_xmit(net, DSC_RESP_MSG, src, self, 0, &maddr, b);
  232. }
  233. /* tipc_disc_add_dest - increment set of discovered nodes
  234. */
  235. void tipc_disc_add_dest(struct tipc_discoverer *d)
  236. {
  237. spin_lock_bh(&d->lock);
  238. d->num_nodes++;
  239. spin_unlock_bh(&d->lock);
  240. }
  241. /* tipc_disc_remove_dest - decrement set of discovered nodes
  242. */
  243. void tipc_disc_remove_dest(struct tipc_discoverer *d)
  244. {
  245. int intv, num;
  246. spin_lock_bh(&d->lock);
  247. d->num_nodes--;
  248. num = d->num_nodes;
  249. intv = d->timer_intv;
  250. if (!num && (intv == TIPC_DISC_INACTIVE || intv > TIPC_DISC_FAST)) {
  251. d->timer_intv = TIPC_DISC_INIT;
  252. mod_timer(&d->timer, jiffies + d->timer_intv);
  253. }
  254. spin_unlock_bh(&d->lock);
  255. }
  256. /* tipc_disc_timeout - send a periodic link setup request
  257. * Called whenever a link setup request timer associated with a bearer expires.
  258. * - Keep doubling time between sent request until limit is reached;
  259. * - Hold at fast polling rate if we don't have any associated nodes
  260. * - Otherwise hold at slow polling rate
  261. */
  262. static void tipc_disc_timeout(struct timer_list *t)
  263. {
  264. struct tipc_discoverer *d = from_timer(d, t, timer);
  265. struct tipc_net *tn = tipc_net(d->net);
  266. struct tipc_media_addr maddr;
  267. struct sk_buff *skb = NULL;
  268. struct net *net = d->net;
  269. u32 bearer_id;
  270. spin_lock_bh(&d->lock);
  271. /* Stop searching if only desired node has been found */
  272. if (tipc_node(d->domain) && d->num_nodes) {
  273. d->timer_intv = TIPC_DISC_INACTIVE;
  274. goto exit;
  275. }
  276. /* Did we just leave trial period ? */
  277. if (!time_before(jiffies, tn->addr_trial_end) && !tipc_own_addr(net)) {
  278. mod_timer(&d->timer, jiffies + TIPC_DISC_INIT);
  279. spin_unlock_bh(&d->lock);
  280. tipc_sched_net_finalize(net, tn->trial_addr);
  281. return;
  282. }
  283. /* Adjust timeout interval according to discovery phase */
  284. if (time_before(jiffies, tn->addr_trial_end)) {
  285. d->timer_intv = TIPC_DISC_INIT;
  286. } else {
  287. d->timer_intv *= 2;
  288. if (d->num_nodes && d->timer_intv > TIPC_DISC_SLOW)
  289. d->timer_intv = TIPC_DISC_SLOW;
  290. else if (!d->num_nodes && d->timer_intv > TIPC_DISC_FAST)
  291. d->timer_intv = TIPC_DISC_FAST;
  292. msg_set_type(buf_msg(d->skb), DSC_REQ_MSG);
  293. msg_set_prevnode(buf_msg(d->skb), tn->trial_addr);
  294. }
  295. mod_timer(&d->timer, jiffies + d->timer_intv);
  296. memcpy(&maddr, &d->dest, sizeof(maddr));
  297. skb = skb_clone(d->skb, GFP_ATOMIC);
  298. bearer_id = d->bearer_id;
  299. exit:
  300. spin_unlock_bh(&d->lock);
  301. if (skb)
  302. tipc_bearer_xmit_skb(net, bearer_id, skb, &maddr);
  303. }
  304. /**
  305. * tipc_disc_create - create object to send periodic link setup requests
  306. * @net: the applicable net namespace
  307. * @b: 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 net *net, struct tipc_bearer *b,
  314. struct tipc_media_addr *dest, struct sk_buff **skb)
  315. {
  316. struct tipc_net *tn = tipc_net(net);
  317. struct tipc_discoverer *d;
  318. d = kmalloc(sizeof(*d), GFP_ATOMIC);
  319. if (!d)
  320. return -ENOMEM;
  321. d->skb = tipc_buf_acquire(MAX_H_SIZE + NODE_ID_LEN, GFP_ATOMIC);
  322. if (!d->skb) {
  323. kfree(d);
  324. return -ENOMEM;
  325. }
  326. tipc_disc_init_msg(net, d->skb, DSC_REQ_MSG, b);
  327. /* Do we need an address trial period first ? */
  328. if (!tipc_own_addr(net)) {
  329. tn->addr_trial_end = jiffies + msecs_to_jiffies(1000);
  330. msg_set_type(buf_msg(d->skb), DSC_TRIAL_MSG);
  331. }
  332. memcpy(&d->dest, dest, sizeof(*dest));
  333. d->net = net;
  334. d->bearer_id = b->identity;
  335. d->domain = b->domain;
  336. d->num_nodes = 0;
  337. d->timer_intv = TIPC_DISC_INIT;
  338. spin_lock_init(&d->lock);
  339. timer_setup(&d->timer, tipc_disc_timeout, 0);
  340. mod_timer(&d->timer, jiffies + d->timer_intv);
  341. b->disc = d;
  342. *skb = skb_clone(d->skb, GFP_ATOMIC);
  343. return 0;
  344. }
  345. /**
  346. * tipc_disc_delete - destroy object sending periodic link setup requests
  347. * @d: ptr to link duest structure
  348. */
  349. void tipc_disc_delete(struct tipc_discoverer *d)
  350. {
  351. del_timer_sync(&d->timer);
  352. kfree_skb(d->skb);
  353. kfree(d);
  354. }
  355. /**
  356. * tipc_disc_reset - reset object to send periodic link setup requests
  357. * @net: the applicable net namespace
  358. * @b: ptr to bearer issuing requests
  359. * @dest_domain: network domain to which links can be established
  360. */
  361. void tipc_disc_reset(struct net *net, struct tipc_bearer *b)
  362. {
  363. struct tipc_discoverer *d = b->disc;
  364. struct tipc_media_addr maddr;
  365. struct sk_buff *skb;
  366. spin_lock_bh(&d->lock);
  367. tipc_disc_init_msg(net, d->skb, DSC_REQ_MSG, b);
  368. d->net = net;
  369. d->bearer_id = b->identity;
  370. d->domain = b->domain;
  371. d->num_nodes = 0;
  372. d->timer_intv = TIPC_DISC_INIT;
  373. memcpy(&maddr, &d->dest, sizeof(maddr));
  374. mod_timer(&d->timer, jiffies + d->timer_intv);
  375. skb = skb_clone(d->skb, GFP_ATOMIC);
  376. spin_unlock_bh(&d->lock);
  377. if (skb)
  378. tipc_bearer_xmit_skb(net, b->identity, skb, &maddr);
  379. }