config.c 13.1 KB
Newer Older
P
Per Liden 已提交
1 2
/*
 * net/tipc/config.c: TIPC configuration management code
3
 *
P
Per Liden 已提交
4
 * Copyright (c) 2002-2006, Ericsson AB
5
 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
P
Per Liden 已提交
6 7
 * All rights reserved.
 *
P
Per Liden 已提交
8
 * Redistribution and use in source and binary forms, with or without
P
Per Liden 已提交
9 10
 * modification, are permitted provided that the following conditions are met:
 *
P
Per Liden 已提交
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.
P
Per Liden 已提交
19
 *
P
Per Liden 已提交
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
P
Per Liden 已提交
34 35 36 37 38 39 40
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "port.h"
#include "name_table.h"
#include "config.h"
41
#include "server.h"
P
Per Liden 已提交
42

43 44
#define REPLY_TRUNCATED "<truncated>\n"

45
static DEFINE_MUTEX(config_mutex);
46
static struct tipc_server cfgsrv;
P
Per Liden 已提交
47 48 49 50 51 52

static const void *req_tlv_area;	/* request message TLV area */
static int req_tlv_space;		/* request message TLV area size */
static int rep_headroom;		/* reply message headroom to use */


53
struct sk_buff *tipc_cfg_reply_alloc(int payload_size)
P
Per Liden 已提交
54 55 56 57 58 59 60 61 62
{
	struct sk_buff *buf;

	buf = alloc_skb(rep_headroom + payload_size, GFP_ATOMIC);
	if (buf)
		skb_reserve(buf, rep_headroom);
	return buf;
}

63
int tipc_cfg_append_tlv(struct sk_buff *buf, int tlv_type,
64
			void *tlv_data, int tlv_data_size)
P
Per Liden 已提交
65
{
66
	struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(buf);
P
Per Liden 已提交
67 68
	int new_tlv_space = TLV_SPACE(tlv_data_size);

69
	if (skb_tailroom(buf) < new_tlv_space)
P
Per Liden 已提交
70 71 72 73 74 75 76 77 78
		return 0;
	skb_put(buf, new_tlv_space);
	tlv->tlv_type = htons(tlv_type);
	tlv->tlv_len  = htons(TLV_LENGTH(tlv_data_size));
	if (tlv_data_size && tlv_data)
		memcpy(TLV_DATA(tlv), tlv_data, tlv_data_size);
	return 1;
}

79
static struct sk_buff *tipc_cfg_reply_unsigned_type(u16 tlv_type, u32 value)
P
Per Liden 已提交
80 81
{
	struct sk_buff *buf;
A
Al Viro 已提交
82
	__be32 value_net;
P
Per Liden 已提交
83

84
	buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(value)));
P
Per Liden 已提交
85 86
	if (buf) {
		value_net = htonl(value);
87
		tipc_cfg_append_tlv(buf, tlv_type, &value_net,
88
				    sizeof(value_net));
P
Per Liden 已提交
89 90 91 92
	}
	return buf;
}

93 94 95 96 97
static struct sk_buff *tipc_cfg_reply_unsigned(u32 value)
{
	return tipc_cfg_reply_unsigned_type(TIPC_TLV_UNSIGNED, value);
}

98
struct sk_buff *tipc_cfg_reply_string_type(u16 tlv_type, char *string)
P
Per Liden 已提交
99 100 101 102
{
	struct sk_buff *buf;
	int string_len = strlen(string) + 1;

103
	buf = tipc_cfg_reply_alloc(TLV_SPACE(string_len));
P
Per Liden 已提交
104
	if (buf)
105
		tipc_cfg_append_tlv(buf, tlv_type, string, string_len);
P
Per Liden 已提交
106 107 108
	return buf;
}

109 110 111 112
static struct sk_buff *tipc_show_stats(void)
{
	struct sk_buff *buf;
	struct tlv_desc *rep_tlv;
113 114
	char *pb;
	int pb_len;
115 116 117 118 119 120 121 122 123 124
	int str_len;
	u32 value;

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);

	value = ntohl(*(u32 *)TLV_DATA(req_tlv_area));
	if (value != 0)
		return tipc_cfg_reply_error_string("unsupported argument");

125
	buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
126 127 128 129
	if (buf == NULL)
		return NULL;

	rep_tlv = (struct tlv_desc *)buf->data;
130 131
	pb = TLV_DATA(rep_tlv);
	pb_len = ULTRA_STRING_MAX_LEN;
132

133 134
	str_len = tipc_snprintf(pb, pb_len, "TIPC version " TIPC_MOD_VER "\n");
	str_len += 1;	/* for "\0" */
135 136 137 138 139 140
	skb_put(buf, TLV_SPACE(str_len));
	TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);

	return buf;
}

P
Per Liden 已提交
141 142 143 144 145
static struct sk_buff *cfg_enable_bearer(void)
{
	struct tipc_bearer_config *args;

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_BEARER_CONFIG))
146
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
147 148 149

	args = (struct tipc_bearer_config *)TLV_DATA(req_tlv_area);
	if (tipc_enable_bearer(args->name,
150
			       ntohl(args->disc_domain),
P
Per Liden 已提交
151
			       ntohl(args->priority)))
152
		return tipc_cfg_reply_error_string("unable to enable bearer");
P
Per Liden 已提交
153

154
	return tipc_cfg_reply_none();
P
Per Liden 已提交
155 156 157 158 159
}

static struct sk_buff *cfg_disable_bearer(void)
{
	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_BEARER_NAME))
160
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
161 162

	if (tipc_disable_bearer((char *)TLV_DATA(req_tlv_area)))
163
		return tipc_cfg_reply_error_string("unable to disable bearer");
P
Per Liden 已提交
164

165
	return tipc_cfg_reply_none();
P
Per Liden 已提交
166 167 168 169 170 171 172
}

static struct sk_buff *cfg_set_own_addr(void)
{
	u32 addr;

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
173
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
174

A
Al Viro 已提交
175
	addr = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
P
Per Liden 已提交
176
	if (addr == tipc_own_addr)
177 178 179 180
		return tipc_cfg_reply_none();
	if (!tipc_addr_node_valid(addr))
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (node address)");
181
	if (tipc_own_addr)
182 183
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
						   " (cannot change node address once assigned)");
184
	tipc_net_start(addr);
185
	return tipc_cfg_reply_none();
P
Per Liden 已提交
186 187 188 189 190 191 192
}

static struct sk_buff *cfg_set_remote_mng(void)
{
	u32 value;

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
193
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
194

A
Al Viro 已提交
195
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
P
Per Liden 已提交
196
	tipc_remote_management = (value != 0);
197
	return tipc_cfg_reply_none();
P
Per Liden 已提交
198 199 200 201 202 203 204
}

static struct sk_buff *cfg_set_max_ports(void)
{
	u32 value;

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
205
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
A
Al Viro 已提交
206
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
207 208
	if (value == tipc_max_ports)
		return tipc_cfg_reply_none();
209
	if (value < 127 || value > 65535)
210 211
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (max ports must be 127-65535)");
212 213
	return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
		" (cannot change max ports while TIPC is active)");
P
Per Liden 已提交
214 215 216 217 218 219 220
}

static struct sk_buff *cfg_set_netid(void)
{
	u32 value;

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
221
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
A
Al Viro 已提交
222
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
223 224
	if (value == tipc_net_id)
		return tipc_cfg_reply_none();
225
	if (value < 1 || value > 9999)
226 227
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (network id must be 1-9999)");
228
	if (tipc_own_addr)
229
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
230 231 232
			" (cannot change network id once TIPC has joined a network)");
	tipc_net_id = value;
	return tipc_cfg_reply_none();
P
Per Liden 已提交
233 234
}

235 236
struct sk_buff *tipc_cfg_do_cmd(u32 orig_node, u16 cmd, const void *request_area,
				int request_space, int reply_headroom)
P
Per Liden 已提交
237 238 239
{
	struct sk_buff *rep_tlv_buf;

240
	mutex_lock(&config_mutex);
P
Per Liden 已提交
241 242 243 244 245 246 247

	/* Save request and reply details in a well-known location */
	req_tlv_area = request_area;
	req_tlv_space = request_space;
	rep_headroom = reply_headroom;

	/* Check command authorization */
248
	if (likely(in_own_node(orig_node))) {
P
Per Liden 已提交
249 250
		/* command is permitted */
	} else if (cmd >= 0x8000) {
251 252
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
							  " (cannot be done remotely)");
P
Per Liden 已提交
253 254
		goto exit;
	} else if (!tipc_remote_management) {
255
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NO_REMOTE);
P
Per Liden 已提交
256
		goto exit;
257
	} else if (cmd >= 0x4000) {
P
Per Liden 已提交
258 259
		u32 domain = 0;

260
		if ((tipc_nametbl_translate(TIPC_ZM_SRV, 0, &domain) == 0) ||
P
Per Liden 已提交
261
		    (domain != orig_node)) {
262
			rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_ZONE_MSTR);
P
Per Liden 已提交
263 264 265 266 267 268 269
			goto exit;
		}
	}

	/* Call appropriate processing routine */
	switch (cmd) {
	case TIPC_CMD_NOOP:
270
		rep_tlv_buf = tipc_cfg_reply_none();
P
Per Liden 已提交
271 272
		break;
	case TIPC_CMD_GET_NODES:
273
		rep_tlv_buf = tipc_node_get_nodes(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
274 275
		break;
	case TIPC_CMD_GET_LINKS:
276
		rep_tlv_buf = tipc_node_get_links(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
277 278
		break;
	case TIPC_CMD_SHOW_LINK_STATS:
279
		rep_tlv_buf = tipc_link_cmd_show_stats(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
280 281
		break;
	case TIPC_CMD_RESET_LINK_STATS:
282
		rep_tlv_buf = tipc_link_cmd_reset_stats(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
283 284
		break;
	case TIPC_CMD_SHOW_NAME_TABLE:
285
		rep_tlv_buf = tipc_nametbl_get(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
286 287
		break;
	case TIPC_CMD_GET_BEARER_NAMES:
288
		rep_tlv_buf = tipc_bearer_get_names();
P
Per Liden 已提交
289 290
		break;
	case TIPC_CMD_GET_MEDIA_NAMES:
291
		rep_tlv_buf = tipc_media_get_names();
P
Per Liden 已提交
292 293
		break;
	case TIPC_CMD_SHOW_PORTS:
294
		rep_tlv_buf = tipc_port_get_ports();
P
Per Liden 已提交
295
		break;
296 297 298
	case TIPC_CMD_SHOW_STATS:
		rep_tlv_buf = tipc_show_stats();
		break;
P
Per Liden 已提交
299 300 301
	case TIPC_CMD_SET_LINK_TOL:
	case TIPC_CMD_SET_LINK_PRI:
	case TIPC_CMD_SET_LINK_WINDOW:
302
		rep_tlv_buf = tipc_link_cmd_config(req_tlv_area, req_tlv_space, cmd);
P
Per Liden 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		break;
	case TIPC_CMD_ENABLE_BEARER:
		rep_tlv_buf = cfg_enable_bearer();
		break;
	case TIPC_CMD_DISABLE_BEARER:
		rep_tlv_buf = cfg_disable_bearer();
		break;
	case TIPC_CMD_SET_NODE_ADDR:
		rep_tlv_buf = cfg_set_own_addr();
		break;
	case TIPC_CMD_SET_REMOTE_MNG:
		rep_tlv_buf = cfg_set_remote_mng();
		break;
	case TIPC_CMD_SET_MAX_PORTS:
		rep_tlv_buf = cfg_set_max_ports();
		break;
	case TIPC_CMD_SET_NETID:
		rep_tlv_buf = cfg_set_netid();
		break;
	case TIPC_CMD_GET_REMOTE_MNG:
323
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_remote_management);
P
Per Liden 已提交
324 325
		break;
	case TIPC_CMD_GET_MAX_PORTS:
326
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_ports);
P
Per Liden 已提交
327 328
		break;
	case TIPC_CMD_GET_NETID:
329
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_net_id);
P
Per Liden 已提交
330
		break;
331 332 333 334
	case TIPC_CMD_NOT_NET_ADMIN:
		rep_tlv_buf =
			tipc_cfg_reply_error_string(TIPC_CFG_NOT_NET_ADMIN);
		break;
335 336
	case TIPC_CMD_SET_MAX_ZONES:
	case TIPC_CMD_GET_MAX_ZONES:
337 338
	case TIPC_CMD_SET_MAX_SLAVES:
	case TIPC_CMD_GET_MAX_SLAVES:
339 340
	case TIPC_CMD_SET_MAX_CLUSTERS:
	case TIPC_CMD_GET_MAX_CLUSTERS:
341 342
	case TIPC_CMD_SET_MAX_NODES:
	case TIPC_CMD_GET_MAX_NODES:
343 344
	case TIPC_CMD_SET_MAX_SUBSCR:
	case TIPC_CMD_GET_MAX_SUBSCR:
345 346
	case TIPC_CMD_SET_MAX_PUBL:
	case TIPC_CMD_GET_MAX_PUBL:
347 348
	case TIPC_CMD_SET_LOG_SIZE:
	case TIPC_CMD_DUMP_LOG:
349 350 351
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
							  " (obsolete command)");
		break;
P
Per Liden 已提交
352
	default:
353 354
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
							  " (unknown command)");
P
Per Liden 已提交
355 356 357
		break;
	}

358 359 360 361 362 363 364 365 366
	WARN_ON(rep_tlv_buf->len > TLV_SPACE(ULTRA_STRING_MAX_LEN));

	/* Append an error message if we cannot return all requested data */
	if (rep_tlv_buf->len == TLV_SPACE(ULTRA_STRING_MAX_LEN)) {
		if (*(rep_tlv_buf->data + ULTRA_STRING_MAX_LEN) != '\0')
			sprintf(rep_tlv_buf->data + rep_tlv_buf->len -
				sizeof(REPLY_TRUNCATED) - 1, REPLY_TRUNCATED);
	}

P
Per Liden 已提交
367 368
	/* Return reply buffer */
exit:
369
	mutex_unlock(&config_mutex);
P
Per Liden 已提交
370 371 372
	return rep_tlv_buf;
}

373 374
static void cfg_conn_msg_event(int conid, struct sockaddr_tipc *addr,
			       void *usr_data, void *buf, size_t len)
P
Per Liden 已提交
375 376 377 378 379 380
{
	struct tipc_cfg_msg_hdr *req_hdr;
	struct tipc_cfg_msg_hdr *rep_hdr;
	struct sk_buff *rep_buf;

	/* Validate configuration message header (ignore invalid message) */
381 382 383
	req_hdr = (struct tipc_cfg_msg_hdr *)buf;
	if ((len < sizeof(*req_hdr)) ||
	    (len != TCM_ALIGN(ntohl(req_hdr->tcm_len))) ||
P
Per Liden 已提交
384
	    (ntohs(req_hdr->tcm_flags) != TCM_F_REQUEST)) {
385
		pr_warn("Invalid configuration message discarded\n");
P
Per Liden 已提交
386 387 388 389
		return;
	}

	/* Generate reply for request (if can't, return request) */
390 391 392
	rep_buf = tipc_cfg_do_cmd(addr->addr.id.node, ntohs(req_hdr->tcm_type),
				  buf + sizeof(*req_hdr),
				  len - sizeof(*req_hdr),
393
				  BUF_HEADROOM + MAX_H_SIZE + sizeof(*rep_hdr));
P
Per Liden 已提交
394 395 396 397 398 399
	if (rep_buf) {
		skb_push(rep_buf, sizeof(*rep_hdr));
		rep_hdr = (struct tipc_cfg_msg_hdr *)rep_buf->data;
		memcpy(rep_hdr, req_hdr, sizeof(*rep_hdr));
		rep_hdr->tcm_len = htonl(rep_buf->len);
		rep_hdr->tcm_flags &= htons(~TCM_F_REQUEST);
400 401
		tipc_conn_sendmsg(&cfgsrv, conid, addr, rep_buf->data,
				  rep_buf->len);
402 403
		kfree_skb(rep_buf);
	}
P
Per Liden 已提交
404 405
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
static struct sockaddr_tipc cfgsrv_addr __read_mostly = {
	.family			= AF_TIPC,
	.addrtype		= TIPC_ADDR_NAMESEQ,
	.addr.nameseq.type	= TIPC_CFG_SRV,
	.addr.nameseq.lower	= 0,
	.addr.nameseq.upper	= 0,
	.scope			= TIPC_ZONE_SCOPE
};

static struct tipc_server cfgsrv __read_mostly = {
	.saddr			= &cfgsrv_addr,
	.imp			= TIPC_CRITICAL_IMPORTANCE,
	.type			= SOCK_RDM,
	.max_rcvbuf_size	= 64 * 1024,
	.name			= "cfg_server",
	.tipc_conn_recvmsg	= cfg_conn_msg_event,
	.tipc_conn_new		= NULL,
	.tipc_conn_shutdown	= NULL
};

426
int tipc_cfg_init(void)
P
Per Liden 已提交
427
{
428
	return tipc_server_start(&cfgsrv);
P
Per Liden 已提交
429 430
}

431 432
void tipc_cfg_reinit(void)
{
433
	tipc_server_stop(&cfgsrv);
434

435 436 437
	cfgsrv_addr.addr.nameseq.lower = tipc_own_addr;
	cfgsrv_addr.addr.nameseq.upper = tipc_own_addr;
	tipc_server_start(&cfgsrv);
438 439
}

440
void tipc_cfg_stop(void)
P
Per Liden 已提交
441
{
442
	tipc_server_stop(&cfgsrv);
P
Per Liden 已提交
443
}