discover.c 13 KB

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