config.c 10.0 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
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
38
#include "socket.h"
P
Per Liden 已提交
39 40
#include "name_table.h"
#include "config.h"
41
#include "server.h"
P
Per Liden 已提交
42

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

P
Per Liden 已提交
45 46 47 48
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 */

49
struct sk_buff *tipc_cfg_reply_alloc(int payload_size)
P
Per Liden 已提交
50 51 52 53 54 55 56 57 58
{
	struct sk_buff *buf;

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

59
int tipc_cfg_append_tlv(struct sk_buff *buf, int tlv_type,
60
			void *tlv_data, int tlv_data_size)
P
Per Liden 已提交
61
{
62
	struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(buf);
P
Per Liden 已提交
63 64
	int new_tlv_space = TLV_SPACE(tlv_data_size);

65
	if (skb_tailroom(buf) < new_tlv_space)
P
Per Liden 已提交
66 67 68 69 70 71 72 73 74
		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;
}

75
static struct sk_buff *tipc_cfg_reply_unsigned_type(u16 tlv_type, u32 value)
P
Per Liden 已提交
76 77
{
	struct sk_buff *buf;
A
Al Viro 已提交
78
	__be32 value_net;
P
Per Liden 已提交
79

80
	buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(value)));
P
Per Liden 已提交
81 82
	if (buf) {
		value_net = htonl(value);
83
		tipc_cfg_append_tlv(buf, tlv_type, &value_net,
84
				    sizeof(value_net));
P
Per Liden 已提交
85 86 87 88
	}
	return buf;
}

89 90 91 92 93
static struct sk_buff *tipc_cfg_reply_unsigned(u32 value)
{
	return tipc_cfg_reply_unsigned_type(TIPC_TLV_UNSIGNED, value);
}

94
struct sk_buff *tipc_cfg_reply_string_type(u16 tlv_type, char *string)
P
Per Liden 已提交
95 96 97 98
{
	struct sk_buff *buf;
	int string_len = strlen(string) + 1;

99
	buf = tipc_cfg_reply_alloc(TLV_SPACE(string_len));
P
Per Liden 已提交
100
	if (buf)
101
		tipc_cfg_append_tlv(buf, tlv_type, string, string_len);
P
Per Liden 已提交
102 103 104
	return buf;
}

105 106 107 108
static struct sk_buff *tipc_show_stats(void)
{
	struct sk_buff *buf;
	struct tlv_desc *rep_tlv;
109 110
	char *pb;
	int pb_len;
111 112 113 114 115 116 117 118 119 120
	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");

121
	buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
122 123 124 125
	if (buf == NULL)
		return NULL;

	rep_tlv = (struct tlv_desc *)buf->data;
126 127
	pb = TLV_DATA(rep_tlv);
	pb_len = ULTRA_STRING_MAX_LEN;
128

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

	return buf;
}

137
static struct sk_buff *cfg_enable_bearer(struct net *net)
P
Per Liden 已提交
138 139 140 141
{
	struct tipc_bearer_config *args;

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_BEARER_CONFIG))
142
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
143 144

	args = (struct tipc_bearer_config *)TLV_DATA(req_tlv_area);
145
	if (tipc_enable_bearer(net, args->name,
146
			       ntohl(args->disc_domain),
P
Per Liden 已提交
147
			       ntohl(args->priority)))
148
		return tipc_cfg_reply_error_string("unable to enable bearer");
P
Per Liden 已提交
149

150
	return tipc_cfg_reply_none();
P
Per Liden 已提交
151 152
}

153
static struct sk_buff *cfg_disable_bearer(struct net *net)
P
Per Liden 已提交
154 155
{
	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_BEARER_NAME))
156
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
157

158
	if (tipc_disable_bearer(net, (char *)TLV_DATA(req_tlv_area)))
159
		return tipc_cfg_reply_error_string("unable to disable bearer");
P
Per Liden 已提交
160

161
	return tipc_cfg_reply_none();
P
Per Liden 已提交
162 163
}

164
static struct sk_buff *cfg_set_own_addr(struct net *net)
P
Per Liden 已提交
165
{
166
	struct tipc_net *tn = net_generic(net, tipc_net_id);
P
Per Liden 已提交
167 168 169
	u32 addr;

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

A
Al Viro 已提交
172
	addr = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
173
	if (addr == tn->own_addr)
174 175 176 177
		return tipc_cfg_reply_none();
	if (!tipc_addr_node_valid(addr))
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (node address)");
178
	if (tn->own_addr)
179 180
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
						   " (cannot change node address once assigned)");
181
	if (!tipc_net_start(net, addr))
182 183 184
		return tipc_cfg_reply_none();

	return tipc_cfg_reply_error_string("cannot change to network mode");
P
Per Liden 已提交
185 186
}

187
static struct sk_buff *cfg_set_netid(struct net *net)
P
Per Liden 已提交
188
{
189
	struct tipc_net *tn = net_generic(net, tipc_net_id);
P
Per Liden 已提交
190 191 192
	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);
A
Al Viro 已提交
194
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
195
	if (value == tn->net_id)
196
		return tipc_cfg_reply_none();
197
	if (value < 1 || value > 9999)
198 199
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (network id must be 1-9999)");
200
	if (tn->own_addr)
201
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
202
			" (cannot change network id once TIPC has joined a network)");
203
	tn->net_id = value;
204
	return tipc_cfg_reply_none();
P
Per Liden 已提交
205 206
}

207 208 209
struct sk_buff *tipc_cfg_do_cmd(struct net *net, u32 orig_node, u16 cmd,
				const void *request_area, int request_space,
				int reply_headroom)
P
Per Liden 已提交
210 211
{
	struct sk_buff *rep_tlv_buf;
212
	struct tipc_net *tn = net_generic(net, tipc_net_id);
P
Per Liden 已提交
213

214
	rtnl_lock();
P
Per Liden 已提交
215 216 217 218 219 220 221

	/* 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 */
222
	if (likely(in_own_node(net, orig_node))) {
P
Per Liden 已提交
223
		/* command is permitted */
224
	} else {
225 226
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
							  " (cannot be done remotely)");
P
Per Liden 已提交
227 228 229 230 231 232
		goto exit;
	}

	/* Call appropriate processing routine */
	switch (cmd) {
	case TIPC_CMD_NOOP:
233
		rep_tlv_buf = tipc_cfg_reply_none();
P
Per Liden 已提交
234 235
		break;
	case TIPC_CMD_GET_NODES:
236 237
		rep_tlv_buf = tipc_node_get_nodes(net, req_tlv_area,
						  req_tlv_space);
P
Per Liden 已提交
238 239
		break;
	case TIPC_CMD_GET_LINKS:
240 241
		rep_tlv_buf = tipc_node_get_links(net, req_tlv_area,
						  req_tlv_space);
P
Per Liden 已提交
242 243
		break;
	case TIPC_CMD_SHOW_LINK_STATS:
244 245
		rep_tlv_buf = tipc_link_cmd_show_stats(net, req_tlv_area,
						       req_tlv_space);
P
Per Liden 已提交
246 247
		break;
	case TIPC_CMD_RESET_LINK_STATS:
248 249
		rep_tlv_buf = tipc_link_cmd_reset_stats(net, req_tlv_area,
							req_tlv_space);
P
Per Liden 已提交
250 251
		break;
	case TIPC_CMD_SHOW_NAME_TABLE:
252 253
		rep_tlv_buf = tipc_nametbl_get(net, req_tlv_area,
					       req_tlv_space);
P
Per Liden 已提交
254 255
		break;
	case TIPC_CMD_GET_MEDIA_NAMES:
256
		rep_tlv_buf = tipc_media_get_names();
P
Per Liden 已提交
257 258
		break;
	case TIPC_CMD_SHOW_PORTS:
259
		rep_tlv_buf = tipc_sk_socks_show(net);
P
Per Liden 已提交
260
		break;
261 262 263
	case TIPC_CMD_SHOW_STATS:
		rep_tlv_buf = tipc_show_stats();
		break;
P
Per Liden 已提交
264 265 266
	case TIPC_CMD_SET_LINK_TOL:
	case TIPC_CMD_SET_LINK_PRI:
	case TIPC_CMD_SET_LINK_WINDOW:
267 268
		rep_tlv_buf = tipc_link_cmd_config(net, req_tlv_area,
						   req_tlv_space, cmd);
P
Per Liden 已提交
269 270
		break;
	case TIPC_CMD_ENABLE_BEARER:
271
		rep_tlv_buf = cfg_enable_bearer(net);
P
Per Liden 已提交
272 273
		break;
	case TIPC_CMD_DISABLE_BEARER:
274
		rep_tlv_buf = cfg_disable_bearer(net);
P
Per Liden 已提交
275 276
		break;
	case TIPC_CMD_SET_NODE_ADDR:
277
		rep_tlv_buf = cfg_set_own_addr(net);
P
Per Liden 已提交
278 279
		break;
	case TIPC_CMD_SET_NETID:
280
		rep_tlv_buf = cfg_set_netid(net);
P
Per Liden 已提交
281 282
		break;
	case TIPC_CMD_GET_NETID:
283
		rep_tlv_buf = tipc_cfg_reply_unsigned(tn->net_id);
P
Per Liden 已提交
284
		break;
285 286 287 288
	case TIPC_CMD_NOT_NET_ADMIN:
		rep_tlv_buf =
			tipc_cfg_reply_error_string(TIPC_CFG_NOT_NET_ADMIN);
		break;
289 290
	case TIPC_CMD_SET_MAX_ZONES:
	case TIPC_CMD_GET_MAX_ZONES:
291 292
	case TIPC_CMD_SET_MAX_SLAVES:
	case TIPC_CMD_GET_MAX_SLAVES:
293 294
	case TIPC_CMD_SET_MAX_CLUSTERS:
	case TIPC_CMD_GET_MAX_CLUSTERS:
295 296
	case TIPC_CMD_SET_MAX_NODES:
	case TIPC_CMD_GET_MAX_NODES:
297 298
	case TIPC_CMD_SET_MAX_SUBSCR:
	case TIPC_CMD_GET_MAX_SUBSCR:
299 300
	case TIPC_CMD_SET_MAX_PUBL:
	case TIPC_CMD_GET_MAX_PUBL:
301
	case TIPC_CMD_SET_LOG_SIZE:
302 303
	case TIPC_CMD_SET_REMOTE_MNG:
	case TIPC_CMD_GET_REMOTE_MNG:
304
	case TIPC_CMD_DUMP_LOG:
305 306
	case TIPC_CMD_SET_MAX_PORTS:
	case TIPC_CMD_GET_MAX_PORTS:
307 308 309
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
							  " (obsolete command)");
		break;
P
Per Liden 已提交
310
	default:
311 312
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
							  " (unknown command)");
P
Per Liden 已提交
313 314 315
		break;
	}

316 317 318 319 320 321 322 323 324
	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 已提交
325 326
	/* Return reply buffer */
exit:
327
	rtnl_unlock();
P
Per Liden 已提交
328 329
	return rep_tlv_buf;
}