discover.c 10.1 KB
Newer Older
Per Liden's avatar
Per Liden committed
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
Per Liden's avatar
Per Liden committed
6
7
 * All rights reserved.
 *
Per Liden's avatar
Per Liden committed
8
 * Redistribution and use in source and binary forms, with or without
Per Liden's avatar
Per Liden committed
9
10
 * modification, are permitted provided that the following conditions are met:
 *
Per Liden's avatar
Per Liden committed
11
12
13
14
15
16
17
18
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
Per Liden's avatar
Per Liden committed
19
 *
Per Liden's avatar
Per Liden committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Liden's avatar
Per Liden committed
34
35
36
37
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
38
#include "node.h"
Per Liden's avatar
Per Liden committed
39
40
#include "discover.h"

41
42
43
44
45
46
47
48
/* min delay during bearer start up */
#define TIPC_LINK_REQ_INIT	msecs_to_jiffies(125)
/* max delay if bearer has no links */
#define TIPC_LINK_REQ_FAST	msecs_to_jiffies(1000)
/* max delay if bearer has links */
#define TIPC_LINK_REQ_SLOW	msecs_to_jiffies(60000)
/* indicates no timer in use */
#define TIPC_LINK_REQ_INACTIVE	0xffffffff
Per Liden's avatar
Per Liden committed
49
50

/**
51
 * struct tipc_link_req - information about an ongoing link setup request
52
 * @bearer_id: identity of bearer issuing requests
53
 * @net: network namespace instance
Per Liden's avatar
Per Liden committed
54
 * @dest: destination address for request messages
55
 * @domain: network domain to which links can be established
56
 * @num_nodes: number of nodes currently discovered (i.e. with an active link)
57
 * @lock: spinlock for controlling access to requests
Per Liden's avatar
Per Liden committed
58
59
60
61
 * @buf: request message to be (repeatedly) sent
 * @timer: timer governing period between requests
 * @timer_intv: current interval between requests (in ms)
 */
62
struct tipc_link_req {
63
	u32 bearer_id;
Per Liden's avatar
Per Liden committed
64
	struct tipc_media_addr dest;
65
	struct net *net;
66
	u32 domain;
67
	int num_nodes;
68
	spinlock_t lock;
Per Liden's avatar
Per Liden committed
69
70
	struct sk_buff *buf;
	struct timer_list timer;
71
	unsigned long timer_intv;
Per Liden's avatar
Per Liden committed
72
73
};

74
/**
75
 * tipc_disc_init_msg - initialize a link setup message
76
 * @net: the applicable net namespace
Per Liden's avatar
Per Liden committed
77
78
79
 * @type: message type (request or response)
 * @b_ptr: ptr to bearer issuing message
 */
80
static void tipc_disc_init_msg(struct net *net, struct sk_buff *buf, u32 type,
81
			       struct tipc_bearer *b_ptr)
Per Liden's avatar
Per Liden committed
82
{
83
	struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Liden's avatar
Per Liden committed
84
	struct tipc_msg *msg;
85
	u32 dest_domain = b_ptr->domain;
Per Liden's avatar
Per Liden committed
86

87
	msg = buf_msg(buf);
88
	tipc_msg_init(tn->own_addr, msg, LINK_CONFIG, type,
89
		      MAX_H_SIZE, dest_domain);
90
	msg_set_non_seq(msg, 1);
91
	msg_set_node_sig(msg, tn->random);
92
	msg_set_node_capabilities(msg, TIPC_NODE_CAPABILITIES);
93
	msg_set_dest_domain(msg, dest_domain);
94
	msg_set_bc_netid(msg, tn->net_id);
95
	b_ptr->media->addr2msg(msg_media_addr(msg), &b_ptr->addr);
Per Liden's avatar
Per Liden committed
96
97
}

98
99
100
101
102
103
/**
 * disc_dupl_alert - issue node address duplication alert
 * @b_ptr: pointer to bearer detecting duplication
 * @node_addr: duplicated node address
 * @media_addr: media address advertised by duplicated node
 */
104
static void disc_dupl_alert(struct tipc_bearer *b_ptr, u32 node_addr,
105
106
107
108
109
			    struct tipc_media_addr *media_addr)
{
	char node_addr_str[16];
	char media_addr_str[64];

110
	tipc_addr_string_fill(node_addr_str, node_addr);
111
112
	tipc_media_addr_printf(media_addr_str, sizeof(media_addr_str),
			       media_addr);
113
114
	pr_warn("Duplicate %s using %s seen on <%s>\n", node_addr_str,
		media_addr_str, b_ptr->name);
115
116
}

Per Liden's avatar
Per Liden committed
117
/**
118
 * tipc_disc_rcv - handle incoming discovery message (request or response)
119
 * @net: the applicable net namespace
Per Liden's avatar
Per Liden committed
120
 * @buf: buffer containing message
121
 * @bearer: bearer that message arrived on
Per Liden's avatar
Per Liden committed
122
 */
123
void tipc_disc_rcv(struct net *net, struct sk_buff *skb,
124
		   struct tipc_bearer *bearer)
Per Liden's avatar
Per Liden committed
125
{
126
	struct tipc_net *tn = net_generic(net, tipc_net_id);
127
	struct tipc_media_addr maddr;
128
129
130
131
132
133
134
135
	struct sk_buff *rskb;
	struct tipc_msg *hdr = buf_msg(skb);
	u32 ddom = msg_dest_domain(hdr);
	u32 onode = msg_prevnode(hdr);
	u32 net_id = msg_bc_netid(hdr);
	u32 mtyp = msg_type(hdr);
	u32 signature = msg_node_sig(hdr);
	u16 caps = msg_node_capabilities(hdr);
136
	bool respond = false;
137
	bool dupl_addr = false;
Per Liden's avatar
Per Liden committed
138

139
140
	bearer->media->msg2addr(bearer, &maddr, msg_media_addr(hdr));
	kfree_skb(skb);
Per Liden's avatar
Per Liden committed
141

142
	/* Ensure message from node is valid and communication is permitted */
143
	if (net_id != tn->net_id)
Per Liden's avatar
Per Liden committed
144
		return;
145
	if (maddr.broadcast)
146
		return;
147
	if (!tipc_addr_domain_valid(ddom))
Per Liden's avatar
Per Liden committed
148
		return;
149
	if (!tipc_addr_node_valid(onode))
Per Liden's avatar
Per Liden committed
150
		return;
151

152
	if (in_own_node(net, onode)) {
153
		if (memcmp(&maddr, &bearer->addr, sizeof(maddr)))
154
			disc_dupl_alert(bearer, tn->own_addr, &maddr);
Per Liden's avatar
Per Liden committed
155
		return;
156
	}
157
	if (!tipc_in_scope(ddom, tn->own_addr))
Per Liden's avatar
Per Liden committed
158
		return;
159
	if (!tipc_in_scope(bearer->domain, onode))
160
		return;
161

162
163
164
	tipc_node_check_dest(net, onode, bearer, caps, signature,
			     &maddr, &respond, &dupl_addr);
	if (dupl_addr)
165
		disc_dupl_alert(bearer, onode, &maddr);
166

167
168
	/* Send response, if necessary */
	if (respond && (mtyp == DSC_REQ_MSG)) {
169
170
171
172
173
		rskb = tipc_buf_acquire(MAX_H_SIZE);
		if (rskb) {
			tipc_disc_init_msg(net, rskb, DSC_RESP_MSG, bearer);
			tipc_bearer_send(net, bearer->identity, rskb, &maddr);
			kfree_skb(rskb);
174
		}
Per Liden's avatar
Per Liden committed
175
176
177
178
	}
}

/**
179
 * disc_update - update frequency of periodic link setup requests
Per Liden's avatar
Per Liden committed
180
 * @req: ptr to link request structure
181
182
183
 *
 * Reinitiates discovery process if discovery object has no associated nodes
 * and is either not currently searching or is searching at a slow rate
Per Liden's avatar
Per Liden committed
184
 */
185
static void disc_update(struct tipc_link_req *req)
Per Liden's avatar
Per Liden committed
186
{
187
188
189
190
	if (!req->num_nodes) {
		if ((req->timer_intv == TIPC_LINK_REQ_INACTIVE) ||
		    (req->timer_intv > TIPC_LINK_REQ_FAST)) {
			req->timer_intv = TIPC_LINK_REQ_INIT;
191
			mod_timer(&req->timer, jiffies + req->timer_intv);
Per Liden's avatar
Per Liden committed
192
193
		}
	}
194
}
Per Liden's avatar
Per Liden committed
195

196
197
198
199
/**
 * tipc_disc_add_dest - increment set of discovered nodes
 * @req: ptr to link request structure
 */
200
void tipc_disc_add_dest(struct tipc_link_req *req)
201
{
202
	spin_lock_bh(&req->lock);
203
	req->num_nodes++;
204
	spin_unlock_bh(&req->lock);
205
206
207
208
209
210
}

/**
 * tipc_disc_remove_dest - decrement set of discovered nodes
 * @req: ptr to link request structure
 */
211
void tipc_disc_remove_dest(struct tipc_link_req *req)
212
{
213
	spin_lock_bh(&req->lock);
214
215
	req->num_nodes--;
	disc_update(req);
216
	spin_unlock_bh(&req->lock);
217
218
}

Per Liden's avatar
Per Liden committed
219
220
/**
 * disc_timeout - send a periodic link setup request
221
 * @data: ptr to link request structure
222
 *
Per Liden's avatar
Per Liden committed
223
224
 * Called whenever a link setup request timer associated with a bearer expires.
 */
225
static void disc_timeout(unsigned long data)
Per Liden's avatar
Per Liden committed
226
{
227
	struct tipc_link_req *req = (struct tipc_link_req *)data;
228
229
	int max_delay;

230
	spin_lock_bh(&req->lock);
Per Liden's avatar
Per Liden committed
231

232
	/* Stop searching if only desired node has been found */
233
	if (tipc_node(req->domain) && req->num_nodes) {
234
235
		req->timer_intv = TIPC_LINK_REQ_INACTIVE;
		goto exit;
Per Liden's avatar
Per Liden committed
236
237
	}

238
239
240
241
242
243
244
	/*
	 * Send discovery message, then update discovery timer
	 *
	 * Keep doubling time between requests until limit is reached;
	 * hold at fast polling rate if don't have any associated nodes,
	 * otherwise hold at slow polling rate
	 */
245
	tipc_bearer_send(req->net, req->bearer_id, req->buf, &req->dest);
246

247
248
249
250
251
252
253
254
255

	req->timer_intv *= 2;
	if (req->num_nodes)
		max_delay = TIPC_LINK_REQ_SLOW;
	else
		max_delay = TIPC_LINK_REQ_FAST;
	if (req->timer_intv > max_delay)
		req->timer_intv = max_delay;

256
	mod_timer(&req->timer, jiffies + req->timer_intv);
257
exit:
258
	spin_unlock_bh(&req->lock);
Per Liden's avatar
Per Liden committed
259
260
261
}

/**
262
 * tipc_disc_create - create object to send periodic link setup requests
263
 * @net: the applicable net namespace
Per Liden's avatar
Per Liden committed
264
265
 * @b_ptr: ptr to bearer issuing requests
 * @dest: destination address for request messages
266
 * @dest_domain: network domain to which links can be established
267
 *
268
 * Returns 0 if successful, otherwise -errno.
Per Liden's avatar
Per Liden committed
269
 */
270
271
int tipc_disc_create(struct net *net, struct tipc_bearer *b_ptr,
		     struct tipc_media_addr *dest)
Per Liden's avatar
Per Liden committed
272
{
273
	struct tipc_link_req *req;
Per Liden's avatar
Per Liden committed
274

275
	req = kmalloc(sizeof(*req), GFP_ATOMIC);
Per Liden's avatar
Per Liden committed
276
	if (!req)
277
		return -ENOMEM;
278
	req->buf = tipc_buf_acquire(MAX_H_SIZE);
Ying Xue's avatar
Ying Xue committed
279
280
	if (!req->buf) {
		kfree(req);
281
		return -ENOMEM;
Ying Xue's avatar
Ying Xue committed
282
	}
Per Liden's avatar
Per Liden committed
283

284
	tipc_disc_init_msg(net, req->buf, DSC_REQ_MSG, b_ptr);
Per Liden's avatar
Per Liden committed
285
	memcpy(&req->dest, dest, sizeof(*dest));
286
	req->net = net;
287
288
	req->bearer_id = b_ptr->identity;
	req->domain = b_ptr->domain;
289
	req->num_nodes = 0;
Per Liden's avatar
Per Liden committed
290
	req->timer_intv = TIPC_LINK_REQ_INIT;
291
	spin_lock_init(&req->lock);
292
293
	setup_timer(&req->timer, disc_timeout, (unsigned long)req);
	mod_timer(&req->timer, jiffies + req->timer_intv);
294
	b_ptr->link_req = req;
295
	tipc_bearer_send(net, req->bearer_id, req->buf, &req->dest);
296
297
298
299
300
301
302
	return 0;
}

/**
 * tipc_disc_delete - destroy object sending periodic link setup requests
 * @req: ptr to link request structure
 */
303
void tipc_disc_delete(struct tipc_link_req *req)
304
{
305
	del_timer_sync(&req->timer);
306
	kfree_skb(req->buf);
307
	kfree(req);
308
}
309
310
311

/**
 * tipc_disc_reset - reset object to send periodic link setup requests
312
 * @net: the applicable net namespace
313
314
315
 * @b_ptr: ptr to bearer issuing requests
 * @dest_domain: network domain to which links can be established
 */
316
void tipc_disc_reset(struct net *net, struct tipc_bearer *b_ptr)
317
318
319
320
{
	struct tipc_link_req *req = b_ptr->link_req;

	spin_lock_bh(&req->lock);
321
	tipc_disc_init_msg(net, req->buf, DSC_REQ_MSG, b_ptr);
322
	req->net = net;
323
324
325
326
	req->bearer_id = b_ptr->identity;
	req->domain = b_ptr->domain;
	req->num_nodes = 0;
	req->timer_intv = TIPC_LINK_REQ_INIT;
327
	mod_timer(&req->timer, jiffies + req->timer_intv);
328
	tipc_bearer_send(net, req->bearer_id, req->buf, &req->dest);
329
330
	spin_unlock_bh(&req->lock);
}