discover.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. */
  127. static bool tipc_disc_addr_trial_msg(struct tipc_discoverer *d,
  128. struct tipc_media_addr *maddr,
  129. struct tipc_bearer *b,
  130. u32 dst, u32 src,
  131. u32 sugg_addr,
  132. u8 *peer_id,
  133. int mtyp)
  134. {
  135. struct net *net = d->net;
  136. struct tipc_net *tn = tipc_net(net);
  137. bool trial = time_before(jiffies, tn->addr_trial_end);
  138. u32 self = tipc_own_addr(net);
  139. if (mtyp == DSC_TRIAL_FAIL_MSG) {
  140. if (!trial)
  141. return true;
  142. /* Ignore if somebody else already gave new suggestion */
  143. if (dst != tn->trial_addr)
  144. return true;
  145. /* Otherwise update trial address and restart trial period */
  146. tn->trial_addr = sugg_addr;
  147. msg_set_prevnode(buf_msg(d->skb), sugg_addr);
  148. tn->addr_trial_end = jiffies + msecs_to_jiffies(1000);
  149. return true;
  150. }
  151. /* Apply trial address if we just left trial period */
  152. if (!trial && !self) {
  153. tipc_net_finalize(net, tn->trial_addr);
  154. msg_set_type(buf_msg(d->skb), DSC_REQ_MSG);
  155. }
  156. if (mtyp != DSC_TRIAL_MSG)
  157. return false;
  158. sugg_addr = tipc_node_try_addr(net, peer_id, src);
  159. if (sugg_addr)
  160. tipc_disc_msg_xmit(net, DSC_TRIAL_FAIL_MSG, src,
  161. self, sugg_addr, maddr, b);
  162. return true;
  163. }
  164. /**
  165. * tipc_disc_rcv - handle incoming discovery message (request or response)
  166. * @net: applicable net namespace
  167. * @skb: buffer containing message
  168. * @b: bearer that message arrived on
  169. */
  170. void tipc_disc_rcv(struct net *net, struct sk_buff *skb,
  171. struct tipc_bearer *b)
  172. {
  173. struct tipc_net *tn = tipc_net(net);
  174. struct tipc_msg *hdr = buf_msg(skb);
  175. u16 caps = msg_node_capabilities(hdr);
  176. bool legacy = tn->legacy_addr_format;
  177. u32 sugg = msg_sugg_node_addr(hdr);
  178. u32 signature = msg_node_sig(hdr);
  179. u8 peer_id[NODE_ID_LEN] = {0,};
  180. u32 dst = msg_dest_domain(hdr);
  181. u32 net_id = msg_bc_netid(hdr);
  182. struct tipc_media_addr maddr;
  183. u32 src = msg_prevnode(hdr);
  184. u32 mtyp = msg_type(hdr);
  185. bool dupl_addr = false;
  186. bool respond = false;
  187. u32 self;
  188. int err;
  189. skb_linearize(skb);
  190. hdr = buf_msg(skb);
  191. if (caps & TIPC_NODE_ID128)
  192. memcpy(peer_id, msg_node_id(hdr), NODE_ID_LEN);
  193. else
  194. sprintf(peer_id, "%x", src);
  195. err = b->media->msg2addr(b, &maddr, msg_media_addr(hdr));
  196. kfree_skb(skb);
  197. if (err || maddr.broadcast) {
  198. pr_warn_ratelimited("Rcv corrupt discovery message\n");
  199. return;
  200. }
  201. /* Ignore discovery messages from own node */
  202. if (!memcmp(&maddr, &b->addr, sizeof(maddr)))
  203. return;
  204. if (net_id != tn->net_id)
  205. return;
  206. if (tipc_disc_addr_trial_msg(b->disc, &maddr, b, dst,
  207. src, sugg, peer_id, mtyp))
  208. return;
  209. self = tipc_own_addr(net);
  210. /* Message from somebody using this node's address */
  211. if (in_own_node(net, src)) {
  212. disc_dupl_alert(b, self, &maddr);
  213. return;
  214. }
  215. if (!tipc_in_scope(legacy, dst, self))
  216. return;
  217. if (!tipc_in_scope(legacy, b->domain, src))
  218. return;
  219. tipc_node_check_dest(net, src, peer_id, b, caps, signature,
  220. &maddr, &respond, &dupl_addr);
  221. if (dupl_addr)
  222. disc_dupl_alert(b, src, &maddr);
  223. if (!respond)
  224. return;
  225. if (mtyp != DSC_REQ_MSG)
  226. return;
  227. tipc_disc_msg_xmit(net, DSC_RESP_MSG, src, self, 0, &maddr, b);
  228. }
  229. /* tipc_disc_add_dest - increment set of discovered nodes
  230. */
  231. void tipc_disc_add_dest(struct tipc_discoverer *d)
  232. {
  233. spin_lock_bh(&d->lock);
  234. d->num_nodes++;
  235. spin_unlock_bh(&d->lock);
  236. }
  237. /* tipc_disc_remove_dest - decrement set of discovered nodes
  238. */
  239. void tipc_disc_remove_dest(struct tipc_discoverer *d)
  240. {
  241. int intv, num;
  242. spin_lock_bh(&d->lock);
  243. d->num_nodes--;
  244. num = d->num_nodes;
  245. intv = d->timer_intv;
  246. if (!num && (intv == TIPC_DISC_INACTIVE || intv > TIPC_DISC_FAST)) {
  247. d->timer_intv = TIPC_DISC_INIT;
  248. mod_timer(&d->timer, jiffies + d->timer_intv);
  249. }
  250. spin_unlock_bh(&d->lock);
  251. }
  252. /* tipc_disc_timeout - send a periodic link setup request
  253. * Called whenever a link setup request timer associated with a bearer expires.
  254. * - Keep doubling time between sent request until limit is reached;
  255. * - Hold at fast polling rate if we don't have any associated nodes
  256. * - Otherwise hold at slow polling rate
  257. */
  258. static void tipc_disc_timeout(struct timer_list *t)
  259. {
  260. struct tipc_discoverer *d = from_timer(d, t, timer);
  261. struct tipc_net *tn = tipc_net(d->net);
  262. u32 self = tipc_own_addr(d->net);
  263. struct tipc_media_addr maddr;
  264. struct sk_buff *skb = NULL;
  265. struct net *net = d->net;
  266. u32 bearer_id;
  267. spin_lock_bh(&d->lock);
  268. /* Stop searching if only desired node has been found */
  269. if (tipc_node(d->domain) && d->num_nodes) {
  270. d->timer_intv = TIPC_DISC_INACTIVE;
  271. goto exit;
  272. }
  273. /* Did we just leave the address trial period ? */
  274. if (!self && !time_before(jiffies, tn->addr_trial_end)) {
  275. self = tn->trial_addr;
  276. tipc_net_finalize(net, self);
  277. msg_set_prevnode(buf_msg(d->skb), self);
  278. msg_set_type(buf_msg(d->skb), DSC_REQ_MSG);
  279. }
  280. /* Adjust timeout interval according to discovery phase */
  281. if (time_before(jiffies, tn->addr_trial_end)) {
  282. d->timer_intv = TIPC_DISC_INIT;
  283. } else {
  284. d->timer_intv *= 2;
  285. if (d->num_nodes && d->timer_intv > TIPC_DISC_SLOW)
  286. d->timer_intv = TIPC_DISC_SLOW;
  287. else if (!d->num_nodes && d->timer_intv > TIPC_DISC_FAST)
  288. d->timer_intv = TIPC_DISC_FAST;
  289. }
  290. mod_timer(&d->timer, jiffies + d->timer_intv);
  291. memcpy(&maddr, &d->dest, sizeof(maddr));
  292. skb = skb_clone(d->skb, GFP_ATOMIC);
  293. bearer_id = d->bearer_id;
  294. exit:
  295. spin_unlock_bh(&d->lock);
  296. if (skb)
  297. tipc_bearer_xmit_skb(net, bearer_id, skb, &maddr);
  298. }
  299. /**
  300. * tipc_disc_create - create object to send periodic link setup requests
  301. * @net: the applicable net namespace
  302. * @b: ptr to bearer issuing requests
  303. * @dest: destination address for request messages
  304. * @dest_domain: network domain to which links can be established
  305. *
  306. * Returns 0 if successful, otherwise -errno.
  307. */
  308. int tipc_disc_create(struct net *net, struct tipc_bearer *b,
  309. struct tipc_media_addr *dest, struct sk_buff **skb)
  310. {
  311. struct tipc_net *tn = tipc_net(net);
  312. struct tipc_discoverer *d;
  313. d = kmalloc(sizeof(*d), GFP_ATOMIC);
  314. if (!d)
  315. return -ENOMEM;
  316. d->skb = tipc_buf_acquire(MAX_H_SIZE + NODE_ID_LEN, GFP_ATOMIC);
  317. if (!d->skb) {
  318. kfree(d);
  319. return -ENOMEM;
  320. }
  321. tipc_disc_init_msg(net, d->skb, DSC_REQ_MSG, b);
  322. /* Do we need an address trial period first ? */
  323. if (!tipc_own_addr(net)) {
  324. tn->addr_trial_end = jiffies + msecs_to_jiffies(1000);
  325. msg_set_type(buf_msg(d->skb), DSC_TRIAL_MSG);
  326. }
  327. memcpy(&d->dest, dest, sizeof(*dest));
  328. d->net = net;
  329. d->bearer_id = b->identity;
  330. d->domain = b->domain;
  331. d->num_nodes = 0;
  332. d->timer_intv = TIPC_DISC_INIT;
  333. spin_lock_init(&d->lock);
  334. timer_setup(&d->timer, tipc_disc_timeout, 0);
  335. mod_timer(&d->timer, jiffies + d->timer_intv);
  336. b->disc = d;
  337. *skb = skb_clone(d->skb, GFP_ATOMIC);
  338. return 0;
  339. }
  340. /**
  341. * tipc_disc_delete - destroy object sending periodic link setup requests
  342. * @d: ptr to link duest structure
  343. */
  344. void tipc_disc_delete(struct tipc_discoverer *d)
  345. {
  346. del_timer_sync(&d->timer);
  347. kfree_skb(d->skb);
  348. kfree(d);
  349. }
  350. /**
  351. * tipc_disc_reset - reset object to send periodic link setup requests
  352. * @net: the applicable net namespace
  353. * @b: ptr to bearer issuing requests
  354. * @dest_domain: network domain to which links can be established
  355. */
  356. void tipc_disc_reset(struct net *net, struct tipc_bearer *b)
  357. {
  358. struct tipc_discoverer *d = b->disc;
  359. struct tipc_media_addr maddr;
  360. struct sk_buff *skb;
  361. spin_lock_bh(&d->lock);
  362. tipc_disc_init_msg(net, d->skb, DSC_REQ_MSG, b);
  363. d->net = net;
  364. d->bearer_id = b->identity;
  365. d->domain = b->domain;
  366. d->num_nodes = 0;
  367. d->timer_intv = TIPC_DISC_INIT;
  368. memcpy(&maddr, &d->dest, sizeof(maddr));
  369. mod_timer(&d->timer, jiffies + d->timer_intv);
  370. skb = skb_clone(d->skb, GFP_ATOMIC);
  371. spin_unlock_bh(&d->lock);
  372. if (skb)
  373. tipc_bearer_xmit_skb(net, b->identity, skb, &maddr);
  374. }