blob: 6acf32a9eef8bedb03cd3ba16d7cfbf726ce3fb4 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/discover.c
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden9da1c8b2006-01-11 18:40:41 +01004 * Copyright (c) 2003-2006, Ericsson AB
Allan Stephens2d627b92011-01-07 13:00:11 -05005 * Copyright (c) 2005-2006, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "discover.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040
41#define TIPC_LINK_REQ_INIT 125 /* min delay during bearer start up */
42#define TIPC_LINK_REQ_FAST 2000 /* normal delay if bearer has no links */
43#define TIPC_LINK_REQ_SLOW 600000 /* normal delay if bearer has links */
44
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090045/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010046 * TODO: Most of the inter-cluster setup stuff should be
47 * rewritten, and be made conformant with specification.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090048 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
50
51/**
52 * struct link_req - information about an ongoing link setup request
53 * @bearer: bearer issuing requests
54 * @dest: destination address for request messages
Allan Stephens66e019a2011-04-20 16:24:07 -050055 * @domain: network domain to which links can be established
Per Lidenb97bf3f2006-01-02 19:04:38 +010056 * @buf: request message to be (repeatedly) sent
57 * @timer: timer governing period between requests
58 * @timer_intv: current interval between requests (in ms)
59 */
60struct link_req {
Allan Stephens2d627b92011-01-07 13:00:11 -050061 struct tipc_bearer *bearer;
Per Lidenb97bf3f2006-01-02 19:04:38 +010062 struct tipc_media_addr dest;
Allan Stephens66e019a2011-04-20 16:24:07 -050063 u32 domain;
Per Lidenb97bf3f2006-01-02 19:04:38 +010064 struct sk_buff *buf;
65 struct timer_list timer;
66 unsigned int timer_intv;
67};
68
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090069/**
Per Liden4323add2006-01-18 00:38:21 +010070 * tipc_disc_init_msg - initialize a link setup message
Per Lidenb97bf3f2006-01-02 19:04:38 +010071 * @type: message type (request or response)
Per Lidenb97bf3f2006-01-02 19:04:38 +010072 * @dest_domain: network domain of node(s) which should respond to message
73 * @b_ptr: ptr to bearer issuing message
74 */
75
Adrian Bunk988f0882006-03-20 22:37:52 -080076static struct sk_buff *tipc_disc_init_msg(u32 type,
Adrian Bunk988f0882006-03-20 22:37:52 -080077 u32 dest_domain,
Allan Stephens2d627b92011-01-07 13:00:11 -050078 struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010079{
Allan Stephensd901a422011-02-28 16:02:30 -050080 struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +010081 struct tipc_msg *msg;
82
83 if (buf) {
84 msg = buf_msg(buf);
Allan Stephensd901a422011-02-28 16:02:30 -050085 tipc_msg_init(msg, LINK_CONFIG, type, INT_H_SIZE, dest_domain);
Allan Stephens40aecb12008-06-04 17:54:48 -070086 msg_set_non_seq(msg, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +010087 msg_set_dest_domain(msg, dest_domain);
88 msg_set_bc_netid(msg, tipc_net_id);
Allan Stephens2d627b92011-01-07 13:00:11 -050089 msg_set_media_addr(msg, &b_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010090 }
91 return buf;
92}
93
94/**
Allan Stephense91ed0b2006-10-16 21:44:59 -070095 * disc_dupl_alert - issue node address duplication alert
96 * @b_ptr: pointer to bearer detecting duplication
97 * @node_addr: duplicated node address
98 * @media_addr: media address advertised by duplicated node
99 */
100
Allan Stephens2d627b92011-01-07 13:00:11 -0500101static void disc_dupl_alert(struct tipc_bearer *b_ptr, u32 node_addr,
Allan Stephense91ed0b2006-10-16 21:44:59 -0700102 struct tipc_media_addr *media_addr)
103{
104 char node_addr_str[16];
105 char media_addr_str[64];
106 struct print_buf pb;
107
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000108 tipc_addr_string_fill(node_addr_str, node_addr);
Allan Stephense91ed0b2006-10-16 21:44:59 -0700109 tipc_printbuf_init(&pb, media_addr_str, sizeof(media_addr_str));
110 tipc_media_addr_printf(&pb, media_addr);
111 tipc_printbuf_validate(&pb);
112 warn("Duplicate %s using %s seen on <%s>\n",
Allan Stephens2d627b92011-01-07 13:00:11 -0500113 node_addr_str, media_addr_str, b_ptr->name);
Allan Stephense91ed0b2006-10-16 21:44:59 -0700114}
115
116/**
Per Liden4323add2006-01-18 00:38:21 +0100117 * tipc_disc_recv_msg - handle incoming link setup message (request or response)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 * @buf: buffer containing message
Allan Stephens1265a022008-06-04 17:32:35 -0700119 * @b_ptr: bearer that message arrived on
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 */
121
Allan Stephens2d627b92011-01-07 13:00:11 -0500122void tipc_disc_recv_msg(struct sk_buff *buf, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123{
Allan Stephensa7287502011-02-28 10:03:05 -0500124 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 struct link *link;
Allan Stephensa7287502011-02-28 10:03:05 -0500126 struct tipc_media_addr media_addr, *addr;
127 struct sk_buff *rbuf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 struct tipc_msg *msg = buf_msg(buf);
129 u32 dest = msg_dest_domain(msg);
130 u32 orig = msg_prevnode(msg);
131 u32 net_id = msg_bc_netid(msg);
132 u32 type = msg_type(msg);
Allan Stephensa7287502011-02-28 10:03:05 -0500133 int link_fully_up;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134
Allan Stephens0e659672010-12-31 18:59:32 +0000135 msg_get_media_addr(msg, &media_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 buf_discard(buf);
137
Allan Stephensa7287502011-02-28 10:03:05 -0500138 /* Validate discovery message from requesting node */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 if (net_id != tipc_net_id)
140 return;
Per Liden4323add2006-01-18 00:38:21 +0100141 if (!tipc_addr_domain_valid(dest))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 return;
Per Liden4323add2006-01-18 00:38:21 +0100143 if (!tipc_addr_node_valid(orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 return;
Allan Stephense91ed0b2006-10-16 21:44:59 -0700145 if (orig == tipc_own_addr) {
Allan Stephens2d627b92011-01-07 13:00:11 -0500146 if (memcmp(&media_addr, &b_ptr->addr, sizeof(media_addr)))
Allan Stephense91ed0b2006-10-16 21:44:59 -0700147 disc_dupl_alert(b_ptr, tipc_own_addr, &media_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 return;
Allan Stephense91ed0b2006-10-16 21:44:59 -0700149 }
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000150 if (!tipc_in_scope(dest, tipc_own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 return;
Allan Stephens66e019a2011-04-20 16:24:07 -0500152 if (!tipc_in_scope(b_ptr->link_req->domain, orig))
Allan Stephensa7287502011-02-28 10:03:05 -0500153 return;
Allan Stephens2de07f62006-10-16 21:57:13 -0700154
Allan Stephensa7287502011-02-28 10:03:05 -0500155 /* Locate structure corresponding to requesting node */
156 n_ptr = tipc_node_find(orig);
157 if (!n_ptr) {
158 n_ptr = tipc_node_create(orig);
159 if (!n_ptr)
Allan Stephens5a68d5e2010-08-17 11:00:16 +0000160 return;
Allan Stephensa7287502011-02-28 10:03:05 -0500161 }
162 tipc_node_lock(n_ptr);
Allan Stephens5a68d5e2010-08-17 11:00:16 +0000163
Allan Stephensa7287502011-02-28 10:03:05 -0500164 /* Don't talk to neighbor during cleanup after last session */
165 if (n_ptr->cleanup_required) {
166 tipc_node_unlock(n_ptr);
167 return;
168 }
169
170 link = n_ptr->links[b_ptr->identity];
171
172 /* Create a link endpoint for this bearer, if necessary */
173 if (!link) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500174 link = tipc_link_create(n_ptr, b_ptr, &media_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 if (!link) {
Allan Stephensa7287502011-02-28 10:03:05 -0500176 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 }
179 }
Allan Stephensa7287502011-02-28 10:03:05 -0500180
181 /*
182 * Ensure requesting node's media address is correct
183 *
184 * If media address doesn't match and the link is working, reject the
185 * request (must be from a duplicate node).
186 *
187 * If media address doesn't match and the link is not working, accept
188 * the new media address and reset the link to ensure it starts up
189 * cleanly.
190 */
191 addr = &link->media_addr;
192 if (memcmp(addr, &media_addr, sizeof(*addr))) {
193 if (tipc_link_is_up(link) || (!link->started)) {
194 disc_dupl_alert(b_ptr, orig, &media_addr);
195 tipc_node_unlock(n_ptr);
196 return;
197 }
198 warn("Resetting link <%s>, peer interface address changed\n",
199 link->name);
200 memcpy(addr, &media_addr, sizeof(*addr));
201 tipc_link_reset(link);
202 }
203
204 /* Accept discovery message & send response, if necessary */
205 link_fully_up = link_working_working(link);
Allan Stephensfa2bae22011-02-28 10:56:23 -0500206
Allan Stephensf9107eb2011-02-28 11:04:08 -0500207 if ((type == DSC_REQ_MSG) && !link_fully_up && !b_ptr->blocked) {
Allan Stephensfa2bae22011-02-28 10:56:23 -0500208 rbuf = tipc_disc_init_msg(DSC_RESP_MSG, orig, b_ptr);
209 if (rbuf) {
210 b_ptr->media->send_msg(rbuf, b_ptr, &media_addr);
211 buf_discard(rbuf);
212 }
Allan Stephensa7287502011-02-28 10:03:05 -0500213 }
Allan Stephensfa2bae22011-02-28 10:56:23 -0500214
215 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216}
217
218/**
Per Liden4323add2006-01-18 00:38:21 +0100219 * tipc_disc_update_link_req - update frequency of periodic link setup requests
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 * @req: ptr to link request structure
221 */
222
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900223void tipc_disc_update_link_req(struct link_req *req)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224{
225 if (!req)
226 return;
227
228 if (req->timer_intv == TIPC_LINK_REQ_SLOW) {
229 if (!req->bearer->nodes.count) {
230 req->timer_intv = TIPC_LINK_REQ_FAST;
231 k_start_timer(&req->timer, req->timer_intv);
232 }
233 } else if (req->timer_intv == TIPC_LINK_REQ_FAST) {
234 if (req->bearer->nodes.count) {
235 req->timer_intv = TIPC_LINK_REQ_SLOW;
236 k_start_timer(&req->timer, req->timer_intv);
237 }
238 } else {
239 /* leave timer "as is" if haven't yet reached a "normal" rate */
240 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900241}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242
243/**
244 * disc_timeout - send a periodic link setup request
245 * @req: ptr to link request structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900246 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 * Called whenever a link setup request timer associated with a bearer expires.
248 */
249
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900250static void disc_timeout(struct link_req *req)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251{
Allan Stephens2d627b92011-01-07 13:00:11 -0500252 spin_lock_bh(&req->bearer->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253
Allan Stephens2d627b92011-01-07 13:00:11 -0500254 req->bearer->media->send_msg(req->buf, req->bearer, &req->dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255
256 if ((req->timer_intv == TIPC_LINK_REQ_SLOW) ||
257 (req->timer_intv == TIPC_LINK_REQ_FAST)) {
258 /* leave timer interval "as is" if already at a "normal" rate */
259 } else {
260 req->timer_intv *= 2;
Allan Stephens3ba07e62006-06-25 23:53:47 -0700261 if (req->timer_intv > TIPC_LINK_REQ_FAST)
262 req->timer_intv = TIPC_LINK_REQ_FAST;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900263 if ((req->timer_intv == TIPC_LINK_REQ_FAST) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 (req->bearer->nodes.count))
265 req->timer_intv = TIPC_LINK_REQ_SLOW;
266 }
267 k_start_timer(&req->timer, req->timer_intv);
268
Allan Stephens2d627b92011-01-07 13:00:11 -0500269 spin_unlock_bh(&req->bearer->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270}
271
272/**
Allan Stephens3a777ff2011-04-21 13:58:26 -0500273 * tipc_disc_create - create object to send periodic link setup requests
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274 * @b_ptr: ptr to bearer issuing requests
275 * @dest: destination address for request messages
Allan Stephens66e019a2011-04-20 16:24:07 -0500276 * @dest_domain: network domain to which links can be established
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900277 *
Allan Stephens3a777ff2011-04-21 13:58:26 -0500278 * Returns 0 if successful, otherwise -errno.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279 */
280
Allan Stephens3a777ff2011-04-21 13:58:26 -0500281int tipc_disc_create(struct tipc_bearer *b_ptr,
282 struct tipc_media_addr *dest, u32 dest_domain)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283{
284 struct link_req *req;
285
Panagiotis Issaris9df3f3d2006-07-21 15:52:20 -0700286 req = kmalloc(sizeof(*req), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 if (!req)
Allan Stephens3a777ff2011-04-21 13:58:26 -0500288 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289
Allan Stephens2e07dda2011-01-25 14:39:59 -0500290 req->buf = tipc_disc_init_msg(DSC_REQ_MSG, dest_domain, b_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 if (!req->buf) {
292 kfree(req);
Allan Stephens3a777ff2011-04-21 13:58:26 -0500293 return -ENOMSG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 }
295
296 memcpy(&req->dest, dest, sizeof(*dest));
297 req->bearer = b_ptr;
Allan Stephens66e019a2011-04-20 16:24:07 -0500298 req->domain = dest_domain;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 req->timer_intv = TIPC_LINK_REQ_INIT;
300 k_init_timer(&req->timer, (Handler)disc_timeout, (unsigned long)req);
301 k_start_timer(&req->timer, req->timer_intv);
Allan Stephens3a777ff2011-04-21 13:58:26 -0500302 b_ptr->link_req = req;
303 return 0;
304}
305
306/**
307 * tipc_disc_delete - destroy object sending periodic link setup requests
308 * @req: ptr to link request structure
309 */
310
311void tipc_disc_delete(struct link_req *req)
312{
313 k_cancel_timer(&req->timer);
314 k_term_timer(&req->timer);
315 buf_discard(req->buf);
316 kfree(req);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900317}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318