config.c 17.4 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, 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "dbg.h"
#include "bearer.h"
#include "port.h"
#include "link.h"
#include "zone.h"
#include "addr.h"
#include "name_table.h"
#include "node.h"
#include "config.h"
#include "discover.h"

struct subscr_data {
	char usr_handle[8];
	u32 domain;
	u32 port_ref;
	struct list_head subd_list;
};

struct manager {
	u32 user_ref;
	u32 port_ref;
};

static struct manager mng = { 0};

I
Ingo Molnar 已提交
63
static DEFINE_SPINLOCK(config_lock);
P
Per Liden 已提交
64 65 66 67 68 69

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 */


70
struct sk_buff *tipc_cfg_reply_alloc(int payload_size)
P
Per Liden 已提交
71 72 73 74 75 76 77 78 79
{
	struct sk_buff *buf;

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

80
int tipc_cfg_append_tlv(struct sk_buff *buf, int tlv_type,
81
			void *tlv_data, int tlv_data_size)
P
Per Liden 已提交
82
{
83
	struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(buf);
P
Per Liden 已提交
84 85 86
	int new_tlv_space = TLV_SPACE(tlv_data_size);

	if (skb_tailroom(buf) < new_tlv_space) {
87
		dbg("tipc_cfg_append_tlv unable to append TLV\n");
P
Per Liden 已提交
88 89 90 91 92 93 94 95 96 97
		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;
}

98
static struct sk_buff *tipc_cfg_reply_unsigned_type(u16 tlv_type, u32 value)
P
Per Liden 已提交
99 100
{
	struct sk_buff *buf;
A
Al Viro 已提交
101
	__be32 value_net;
P
Per Liden 已提交
102

103
	buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(value)));
P
Per Liden 已提交
104 105
	if (buf) {
		value_net = htonl(value);
106
		tipc_cfg_append_tlv(buf, tlv_type, &value_net,
107
				    sizeof(value_net));
P
Per Liden 已提交
108 109 110 111
	}
	return buf;
}

112 113 114 115 116
static struct sk_buff *tipc_cfg_reply_unsigned(u32 value)
{
	return tipc_cfg_reply_unsigned_type(TIPC_TLV_UNSIGNED, value);
}

117
struct sk_buff *tipc_cfg_reply_string_type(u16 tlv_type, char *string)
P
Per Liden 已提交
118 119 120 121
{
	struct sk_buff *buf;
	int string_len = strlen(string) + 1;

122
	buf = tipc_cfg_reply_alloc(TLV_SPACE(string_len));
P
Per Liden 已提交
123
	if (buf)
124
		tipc_cfg_append_tlv(buf, tlv_type, string, string_len);
P
Per Liden 已提交
125 126 127
	return buf;
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#define MAX_STATS_INFO 2000

static struct sk_buff *tipc_show_stats(void)
{
	struct sk_buff *buf;
	struct tlv_desc *rep_tlv;
	struct print_buf pb;
	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");

	buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_STATS_INFO));
	if (buf == NULL)
		return NULL;

	rep_tlv = (struct tlv_desc *)buf->data;
	tipc_printbuf_init(&pb, (char *)TLV_DATA(rep_tlv), MAX_STATS_INFO);

	tipc_printf(&pb, "TIPC version " TIPC_MOD_VER "\n");

	/* Use additional tipc_printf()'s to return more info ... */

	str_len = tipc_printbuf_validate(&pb);
	skb_put(buf, TLV_SPACE(str_len));
	TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);

	return buf;
}

P
Per Liden 已提交
163 164 165 166 167
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))
168
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
169 170 171 172 173

	args = (struct tipc_bearer_config *)TLV_DATA(req_tlv_area);
	if (tipc_enable_bearer(args->name,
			       ntohl(args->detect_scope),
			       ntohl(args->priority)))
174
		return tipc_cfg_reply_error_string("unable to enable bearer");
P
Per Liden 已提交
175

176
	return tipc_cfg_reply_none();
P
Per Liden 已提交
177 178 179 180 181
}

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

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

187
	return tipc_cfg_reply_none();
P
Per Liden 已提交
188 189 190 191 192 193 194
}

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

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

A
Al Viro 已提交
197
	addr = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
P
Per Liden 已提交
198
	if (addr == tipc_own_addr)
199 200 201 202
		return tipc_cfg_reply_none();
	if (!tipc_addr_node_valid(addr))
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (node address)");
203
	if (tipc_mode == TIPC_NET_MODE)
204 205
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
						   " (cannot change node address once assigned)");
206

207
	/*
208 209 210 211 212 213 214 215
	 * Must release all spinlocks before calling start_net() because
	 * Linux version of TIPC calls eth_media_start() which calls
	 * register_netdevice_notifier() which may block!
	 *
	 * Temporarily releasing the lock should be harmless for non-Linux TIPC,
	 * but Linux version of eth_media_start() should really be reworked
	 * so that it can be called with spinlocks held.
	 */
P
Per Liden 已提交
216 217

	spin_unlock_bh(&config_lock);
218
	tipc_core_start_net(addr);
P
Per Liden 已提交
219
	spin_lock_bh(&config_lock);
220
	return tipc_cfg_reply_none();
P
Per Liden 已提交
221 222 223 224 225 226 227
}

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

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

A
Al Viro 已提交
230
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
P
Per Liden 已提交
231
	tipc_remote_management = (value != 0);
232
	return tipc_cfg_reply_none();
P
Per Liden 已提交
233 234 235 236 237 238 239
}

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

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

A
Al Viro 已提交
242
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
P
Per Liden 已提交
243
	if (value != delimit(value, 1, 65535))
244 245
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (max publications must be 1-65535)");
P
Per Liden 已提交
246
	tipc_max_publications = value;
247
	return tipc_cfg_reply_none();
P
Per Liden 已提交
248 249 250 251 252 253 254
}

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

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

A
Al Viro 已提交
257
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
P
Per Liden 已提交
258
	if (value != delimit(value, 1, 65535))
259 260
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (max subscriptions must be 1-65535");
P
Per Liden 已提交
261
	tipc_max_subscriptions = value;
262
	return tipc_cfg_reply_none();
P
Per Liden 已提交
263 264 265 266 267 268 269
}

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

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
270
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
A
Al Viro 已提交
271
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
272 273
	if (value == tipc_max_ports)
		return tipc_cfg_reply_none();
P
Per Liden 已提交
274
	if (value != delimit(value, 127, 65535))
275 276
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (max ports must be 127-65535)");
277
	if (tipc_mode != TIPC_NOT_RUNNING)
278
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
279
			" (cannot change max ports while TIPC is active)");
P
Per Liden 已提交
280
	tipc_max_ports = value;
281
	return tipc_cfg_reply_none();
P
Per Liden 已提交
282 283 284 285 286 287 288
}

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

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
289
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
A
Al Viro 已提交
290
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
291 292
	if (value == tipc_max_zones)
		return tipc_cfg_reply_none();
P
Per Liden 已提交
293
	if (value != delimit(value, 1, 255))
294 295
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (max zones must be 1-255)");
296 297 298 299 300
	if (tipc_mode == TIPC_NET_MODE)
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
			" (cannot change max zones once TIPC has joined a network)");
	tipc_max_zones = value;
	return tipc_cfg_reply_none();
P
Per Liden 已提交
301 302 303 304 305 306 307
}

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

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
308
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
A
Al Viro 已提交
309
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
310 311
	if (value != delimit(value, 1, 1))
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
312 313
						   " (max clusters fixed at 1)");
	return tipc_cfg_reply_none();
P
Per Liden 已提交
314 315 316 317 318 319 320
}

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

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
321
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
A
Al Viro 已提交
322
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
323 324
	if (value == tipc_max_nodes)
		return tipc_cfg_reply_none();
P
Per Liden 已提交
325
	if (value != delimit(value, 8, 2047))
326 327
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (max nodes must be 8-2047)");
328 329 330 331 332
	if (tipc_mode == TIPC_NET_MODE)
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
			" (cannot change max nodes once TIPC has joined a network)");
	tipc_max_nodes = value;
	return tipc_cfg_reply_none();
P
Per Liden 已提交
333 334 335 336 337 338 339
}

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

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
340
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
A
Al Viro 已提交
341
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
P
Per Liden 已提交
342
	if (value != 0)
343 344 345
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
						   " (max secondary nodes fixed at 0)");
	return tipc_cfg_reply_none();
P
Per Liden 已提交
346 347 348 349 350 351 352
}

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

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
353
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
A
Al Viro 已提交
354
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
355 356
	if (value == tipc_net_id)
		return tipc_cfg_reply_none();
P
Per Liden 已提交
357
	if (value != delimit(value, 1, 9999))
358 359
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (network id must be 1-9999)");
360
	if (tipc_mode == TIPC_NET_MODE)
361
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
362 363 364
			" (cannot change network id once TIPC has joined a network)");
	tipc_net_id = value;
	return tipc_cfg_reply_none();
P
Per Liden 已提交
365 366
}

367 368
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 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
{
	struct sk_buff *rep_tlv_buf;

	spin_lock_bh(&config_lock);

	/* 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 */

	if (likely(orig_node == tipc_own_addr)) {
		/* command is permitted */
	} else if (cmd >= 0x8000) {
385 386
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
							  " (cannot be done remotely)");
P
Per Liden 已提交
387 388
		goto exit;
	} else if (!tipc_remote_management) {
389
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NO_REMOTE);
P
Per Liden 已提交
390 391 392 393 394
		goto exit;
	}
	else if (cmd >= 0x4000) {
		u32 domain = 0;

395
		if ((tipc_nametbl_translate(TIPC_ZM_SRV, 0, &domain) == 0) ||
P
Per Liden 已提交
396
		    (domain != orig_node)) {
397
			rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_ZONE_MSTR);
P
Per Liden 已提交
398 399 400 401 402 403 404 405
			goto exit;
		}
	}

	/* Call appropriate processing routine */

	switch (cmd) {
	case TIPC_CMD_NOOP:
406
		rep_tlv_buf = tipc_cfg_reply_none();
P
Per Liden 已提交
407 408
		break;
	case TIPC_CMD_GET_NODES:
409
		rep_tlv_buf = tipc_node_get_nodes(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
410 411
		break;
	case TIPC_CMD_GET_LINKS:
412
		rep_tlv_buf = tipc_node_get_links(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
413 414
		break;
	case TIPC_CMD_SHOW_LINK_STATS:
415
		rep_tlv_buf = tipc_link_cmd_show_stats(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
416 417
		break;
	case TIPC_CMD_RESET_LINK_STATS:
418
		rep_tlv_buf = tipc_link_cmd_reset_stats(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
419 420
		break;
	case TIPC_CMD_SHOW_NAME_TABLE:
421
		rep_tlv_buf = tipc_nametbl_get(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
422 423
		break;
	case TIPC_CMD_GET_BEARER_NAMES:
424
		rep_tlv_buf = tipc_bearer_get_names();
P
Per Liden 已提交
425 426
		break;
	case TIPC_CMD_GET_MEDIA_NAMES:
427
		rep_tlv_buf = tipc_media_get_names();
P
Per Liden 已提交
428 429
		break;
	case TIPC_CMD_SHOW_PORTS:
430
		rep_tlv_buf = tipc_port_get_ports();
P
Per Liden 已提交
431 432
		break;
	case TIPC_CMD_SET_LOG_SIZE:
433
		rep_tlv_buf = tipc_log_resize_cmd(req_tlv_area, req_tlv_space);
P
Per Liden 已提交
434 435
		break;
	case TIPC_CMD_DUMP_LOG:
436
		rep_tlv_buf = tipc_log_dump();
P
Per Liden 已提交
437
		break;
438 439 440
	case TIPC_CMD_SHOW_STATS:
		rep_tlv_buf = tipc_show_stats();
		break;
P
Per Liden 已提交
441 442 443
	case TIPC_CMD_SET_LINK_TOL:
	case TIPC_CMD_SET_LINK_PRI:
	case TIPC_CMD_SET_LINK_WINDOW:
444
		rep_tlv_buf = tipc_link_cmd_config(req_tlv_area, req_tlv_space, cmd);
P
Per Liden 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
		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_MAX_PUBL:
		rep_tlv_buf = cfg_set_max_publications();
		break;
	case TIPC_CMD_SET_MAX_SUBSCR:
		rep_tlv_buf = cfg_set_max_subscriptions();
		break;
	case TIPC_CMD_SET_MAX_ZONES:
		rep_tlv_buf = cfg_set_max_zones();
		break;
	case TIPC_CMD_SET_MAX_CLUSTERS:
		rep_tlv_buf = cfg_set_max_clusters();
		break;
	case TIPC_CMD_SET_MAX_NODES:
		rep_tlv_buf = cfg_set_max_nodes();
		break;
	case TIPC_CMD_SET_MAX_SLAVES:
		rep_tlv_buf = cfg_set_max_slaves();
		break;
	case TIPC_CMD_SET_NETID:
		rep_tlv_buf = cfg_set_netid();
		break;
	case TIPC_CMD_GET_REMOTE_MNG:
483
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_remote_management);
P
Per Liden 已提交
484 485
		break;
	case TIPC_CMD_GET_MAX_PORTS:
486
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_ports);
P
Per Liden 已提交
487 488
		break;
	case TIPC_CMD_GET_MAX_PUBL:
489
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_publications);
P
Per Liden 已提交
490 491
		break;
	case TIPC_CMD_GET_MAX_SUBSCR:
492
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_subscriptions);
P
Per Liden 已提交
493 494
		break;
	case TIPC_CMD_GET_MAX_ZONES:
495
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_zones);
P
Per Liden 已提交
496 497
		break;
	case TIPC_CMD_GET_MAX_CLUSTERS:
498
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_clusters);
P
Per Liden 已提交
499 500
		break;
	case TIPC_CMD_GET_MAX_NODES:
501
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_nodes);
P
Per Liden 已提交
502 503
		break;
	case TIPC_CMD_GET_MAX_SLAVES:
504
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_slaves);
P
Per Liden 已提交
505 506
		break;
	case TIPC_CMD_GET_NETID:
507
		rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_net_id);
P
Per Liden 已提交
508
		break;
509 510 511 512
	case TIPC_CMD_NOT_NET_ADMIN:
		rep_tlv_buf =
			tipc_cfg_reply_error_string(TIPC_CFG_NOT_NET_ADMIN);
		break;
P
Per Liden 已提交
513
	default:
514 515
		rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
							  " (unknown command)");
P
Per Liden 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529
		break;
	}

	/* Return reply buffer */
exit:
	spin_unlock_bh(&config_lock);
	return rep_tlv_buf;
}

static void cfg_named_msg_event(void *userdata,
				u32 port_ref,
				struct sk_buff **buf,
				const unchar *msg,
				u32 size,
530
				u32 importance,
P
Per Liden 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543
				struct tipc_portid const *orig,
				struct tipc_name_seq const *dest)
{
	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) */

	req_hdr = (struct tipc_cfg_msg_hdr *)msg;
	if ((size < sizeof(*req_hdr)) ||
	    (size != TCM_ALIGN(ntohl(req_hdr->tcm_len))) ||
	    (ntohs(req_hdr->tcm_flags) != TCM_F_REQUEST)) {
544
		warn("Invalid configuration message discarded\n");
P
Per Liden 已提交
545 546 547 548 549
		return;
	}

	/* Generate reply for request (if can't, return request) */

550
	rep_buf = tipc_cfg_do_cmd(orig->node,
551
				  ntohs(req_hdr->tcm_type),
552 553 554
				  msg + sizeof(*req_hdr),
				  size - sizeof(*req_hdr),
				  BUF_HEADROOM + MAX_H_SIZE + sizeof(*rep_hdr));
P
Per Liden 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	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);
	} else {
		rep_buf = *buf;
		*buf = NULL;
	}

	/* NEED TO ADD CODE TO HANDLE FAILED SEND (SUCH AS CONGESTION) */
	tipc_send_buf2port(port_ref, orig, rep_buf, rep_buf->len);
}

570
int tipc_cfg_init(void)
P
Per Liden 已提交
571 572 573 574
{
	struct tipc_name_seq seq;
	int res;

575
	res = tipc_attach(&mng.user_ref, NULL, NULL);
P
Per Liden 已提交
576 577 578
	if (res)
		goto failed;

579
	res = tipc_createport(mng.user_ref, NULL, TIPC_CRITICAL_IMPORTANCE,
P
Per Liden 已提交
580 581 582 583 584 585 586 587
			      NULL, NULL, NULL,
			      NULL, cfg_named_msg_event, NULL,
			      NULL, &mng.port_ref);
	if (res)
		goto failed;

	seq.type = TIPC_CFG_SRV;
	seq.lower = seq.upper = tipc_own_addr;
588
	res = tipc_nametbl_publish_rsv(mng.port_ref, TIPC_ZONE_SCOPE, &seq);
P
Per Liden 已提交
589 590 591 592 593 594 595 596 597 598 599 600
	if (res)
		goto failed;

	return 0;

failed:
	err("Unable to create configuration service\n");
	tipc_detach(mng.user_ref);
	mng.user_ref = 0;
	return res;
}

601
void tipc_cfg_stop(void)
P
Per Liden 已提交
602 603 604 605 606 607
{
	if (mng.user_ref) {
		tipc_detach(mng.user_ref);
		mng.user_ref = 0;
	}
}