2 * net/tipc/config.c: TIPC configuration management code
4 * Copyright (c) 2002-2006, Ericsson AB
5 * Copyright (c) 2004-2007, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 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.
20 * 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.
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
34 * POSSIBILITY OF SUCH DAMAGE.
44 #include "name_table.h"
53 struct list_head subd_list
;
61 static struct manager mng
= { 0};
63 static DEFINE_SPINLOCK(config_lock
);
65 static const void *req_tlv_area
; /* request message TLV area */
66 static int req_tlv_space
; /* request message TLV area size */
67 static int rep_headroom
; /* reply message headroom to use */
70 struct sk_buff
*tipc_cfg_reply_alloc(int payload_size
)
74 buf
= alloc_skb(rep_headroom
+ payload_size
, GFP_ATOMIC
);
76 skb_reserve(buf
, rep_headroom
);
80 int tipc_cfg_append_tlv(struct sk_buff
*buf
, int tlv_type
,
81 void *tlv_data
, int tlv_data_size
)
83 struct tlv_desc
*tlv
= (struct tlv_desc
*)skb_tail_pointer(buf
);
84 int new_tlv_space
= TLV_SPACE(tlv_data_size
);
86 if (skb_tailroom(buf
) < new_tlv_space
) {
87 dbg("tipc_cfg_append_tlv unable to append TLV\n");
90 skb_put(buf
, new_tlv_space
);
91 tlv
->tlv_type
= htons(tlv_type
);
92 tlv
->tlv_len
= htons(TLV_LENGTH(tlv_data_size
));
93 if (tlv_data_size
&& tlv_data
)
94 memcpy(TLV_DATA(tlv
), tlv_data
, tlv_data_size
);
98 static struct sk_buff
*tipc_cfg_reply_unsigned_type(u16 tlv_type
, u32 value
)
103 buf
= tipc_cfg_reply_alloc(TLV_SPACE(sizeof(value
)));
105 value_net
= htonl(value
);
106 tipc_cfg_append_tlv(buf
, tlv_type
, &value_net
,
112 static struct sk_buff
*tipc_cfg_reply_unsigned(u32 value
)
114 return tipc_cfg_reply_unsigned_type(TIPC_TLV_UNSIGNED
, value
);
117 struct sk_buff
*tipc_cfg_reply_string_type(u16 tlv_type
, char *string
)
120 int string_len
= strlen(string
) + 1;
122 buf
= tipc_cfg_reply_alloc(TLV_SPACE(string_len
));
124 tipc_cfg_append_tlv(buf
, tlv_type
, string
, string_len
);
128 #define MAX_STATS_INFO 2000
130 static struct sk_buff
*tipc_show_stats(void)
133 struct tlv_desc
*rep_tlv
;
138 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
139 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
141 value
= ntohl(*(u32
*)TLV_DATA(req_tlv_area
));
143 return tipc_cfg_reply_error_string("unsupported argument");
145 buf
= tipc_cfg_reply_alloc(TLV_SPACE(MAX_STATS_INFO
));
149 rep_tlv
= (struct tlv_desc
*)buf
->data
;
150 tipc_printbuf_init(&pb
, (char *)TLV_DATA(rep_tlv
), MAX_STATS_INFO
);
152 tipc_printf(&pb
, "TIPC version " TIPC_MOD_VER
"\n");
154 /* Use additional tipc_printf()'s to return more info ... */
156 str_len
= tipc_printbuf_validate(&pb
);
157 skb_put(buf
, TLV_SPACE(str_len
));
158 TLV_SET(rep_tlv
, TIPC_TLV_ULTRA_STRING
, NULL
, str_len
);
163 static struct sk_buff
*cfg_enable_bearer(void)
165 struct tipc_bearer_config
*args
;
167 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_BEARER_CONFIG
))
168 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
170 args
= (struct tipc_bearer_config
*)TLV_DATA(req_tlv_area
);
171 if (tipc_enable_bearer(args
->name
,
172 ntohl(args
->detect_scope
),
173 ntohl(args
->priority
)))
174 return tipc_cfg_reply_error_string("unable to enable bearer");
176 return tipc_cfg_reply_none();
179 static struct sk_buff
*cfg_disable_bearer(void)
181 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_BEARER_NAME
))
182 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
184 if (tipc_disable_bearer((char *)TLV_DATA(req_tlv_area
)))
185 return tipc_cfg_reply_error_string("unable to disable bearer");
187 return tipc_cfg_reply_none();
190 static struct sk_buff
*cfg_set_own_addr(void)
194 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
195 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
197 addr
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
198 if (addr
== tipc_own_addr
)
199 return tipc_cfg_reply_none();
200 if (!tipc_addr_node_valid(addr
))
201 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
203 if (tipc_mode
== TIPC_NET_MODE
)
204 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
205 " (cannot change node address once assigned)");
208 * Must release all spinlocks before calling start_net() because
209 * Linux version of TIPC calls eth_media_start() which calls
210 * register_netdevice_notifier() which may block!
212 * Temporarily releasing the lock should be harmless for non-Linux TIPC,
213 * but Linux version of eth_media_start() should really be reworked
214 * so that it can be called with spinlocks held.
217 spin_unlock_bh(&config_lock
);
218 tipc_core_start_net(addr
);
219 spin_lock_bh(&config_lock
);
220 return tipc_cfg_reply_none();
223 static struct sk_buff
*cfg_set_remote_mng(void)
227 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
228 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
230 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
231 tipc_remote_management
= (value
!= 0);
232 return tipc_cfg_reply_none();
235 static struct sk_buff
*cfg_set_max_publications(void)
239 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
240 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
242 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
243 if (value
!= delimit(value
, 1, 65535))
244 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
245 " (max publications must be 1-65535)");
246 tipc_max_publications
= value
;
247 return tipc_cfg_reply_none();
250 static struct sk_buff
*cfg_set_max_subscriptions(void)
254 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
255 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
257 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
258 if (value
!= delimit(value
, 1, 65535))
259 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
260 " (max subscriptions must be 1-65535");
261 tipc_max_subscriptions
= value
;
262 return tipc_cfg_reply_none();
265 static struct sk_buff
*cfg_set_max_ports(void)
269 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
270 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
271 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
272 if (value
== tipc_max_ports
)
273 return tipc_cfg_reply_none();
274 if (value
!= delimit(value
, 127, 65535))
275 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
276 " (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)");
280 tipc_max_ports
= value
;
281 return tipc_cfg_reply_none();
284 static struct sk_buff
*cfg_set_max_zones(void)
288 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
289 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
290 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
291 if (value
== tipc_max_zones
)
292 return tipc_cfg_reply_none();
293 if (value
!= delimit(value
, 1, 255))
294 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
295 " (max zones must be 1-255)");
296 if (tipc_mode
== TIPC_NET_MODE
)
297 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
298 " (cannot change max zones once TIPC has joined a network)");
299 tipc_max_zones
= value
;
300 return tipc_cfg_reply_none();
303 static struct sk_buff
*cfg_set_max_clusters(void)
307 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
308 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
309 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
310 if (value
!= delimit(value
, 1, 1))
311 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
312 " (max clusters fixed at 1)");
313 return tipc_cfg_reply_none();
316 static struct sk_buff
*cfg_set_max_nodes(void)
320 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
321 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
322 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
323 if (value
== tipc_max_nodes
)
324 return tipc_cfg_reply_none();
325 if (value
!= delimit(value
, 8, 2047))
326 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
327 " (max nodes must be 8-2047)");
328 if (tipc_mode
== TIPC_NET_MODE
)
329 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
330 " (cannot change max nodes once TIPC has joined a network)");
331 tipc_max_nodes
= value
;
332 return tipc_cfg_reply_none();
335 static struct sk_buff
*cfg_set_max_slaves(void)
339 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
340 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
341 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
343 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
344 " (max secondary nodes fixed at 0)");
345 return tipc_cfg_reply_none();
348 static struct sk_buff
*cfg_set_netid(void)
352 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
353 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
354 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
355 if (value
== tipc_net_id
)
356 return tipc_cfg_reply_none();
357 if (value
!= delimit(value
, 1, 9999))
358 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
359 " (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 " (cannot change network id once TIPC has joined a network)");
364 return tipc_cfg_reply_none();
367 struct sk_buff
*tipc_cfg_do_cmd(u32 orig_node
, u16 cmd
, const void *request_area
,
368 int request_space
, int reply_headroom
)
370 struct sk_buff
*rep_tlv_buf
;
372 spin_lock_bh(&config_lock
);
374 /* Save request and reply details in a well-known location */
376 req_tlv_area
= request_area
;
377 req_tlv_space
= request_space
;
378 rep_headroom
= reply_headroom
;
380 /* Check command authorization */
382 if (likely(orig_node
== tipc_own_addr
)) {
383 /* command is permitted */
384 } else if (cmd
>= 0x8000) {
385 rep_tlv_buf
= tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
386 " (cannot be done remotely)");
388 } else if (!tipc_remote_management
) {
389 rep_tlv_buf
= tipc_cfg_reply_error_string(TIPC_CFG_NO_REMOTE
);
392 else if (cmd
>= 0x4000) {
395 if ((tipc_nametbl_translate(TIPC_ZM_SRV
, 0, &domain
) == 0) ||
396 (domain
!= orig_node
)) {
397 rep_tlv_buf
= tipc_cfg_reply_error_string(TIPC_CFG_NOT_ZONE_MSTR
);
402 /* Call appropriate processing routine */
406 rep_tlv_buf
= tipc_cfg_reply_none();
408 case TIPC_CMD_GET_NODES
:
409 rep_tlv_buf
= tipc_node_get_nodes(req_tlv_area
, req_tlv_space
);
411 case TIPC_CMD_GET_LINKS
:
412 rep_tlv_buf
= tipc_node_get_links(req_tlv_area
, req_tlv_space
);
414 case TIPC_CMD_SHOW_LINK_STATS
:
415 rep_tlv_buf
= tipc_link_cmd_show_stats(req_tlv_area
, req_tlv_space
);
417 case TIPC_CMD_RESET_LINK_STATS
:
418 rep_tlv_buf
= tipc_link_cmd_reset_stats(req_tlv_area
, req_tlv_space
);
420 case TIPC_CMD_SHOW_NAME_TABLE
:
421 rep_tlv_buf
= tipc_nametbl_get(req_tlv_area
, req_tlv_space
);
423 case TIPC_CMD_GET_BEARER_NAMES
:
424 rep_tlv_buf
= tipc_bearer_get_names();
426 case TIPC_CMD_GET_MEDIA_NAMES
:
427 rep_tlv_buf
= tipc_media_get_names();
429 case TIPC_CMD_SHOW_PORTS
:
430 rep_tlv_buf
= tipc_port_get_ports();
432 case TIPC_CMD_SET_LOG_SIZE
:
433 rep_tlv_buf
= tipc_log_resize_cmd(req_tlv_area
, req_tlv_space
);
435 case TIPC_CMD_DUMP_LOG
:
436 rep_tlv_buf
= tipc_log_dump();
438 case TIPC_CMD_SHOW_STATS
:
439 rep_tlv_buf
= tipc_show_stats();
441 case TIPC_CMD_SET_LINK_TOL
:
442 case TIPC_CMD_SET_LINK_PRI
:
443 case TIPC_CMD_SET_LINK_WINDOW
:
444 rep_tlv_buf
= tipc_link_cmd_config(req_tlv_area
, req_tlv_space
, cmd
);
446 case TIPC_CMD_ENABLE_BEARER
:
447 rep_tlv_buf
= cfg_enable_bearer();
449 case TIPC_CMD_DISABLE_BEARER
:
450 rep_tlv_buf
= cfg_disable_bearer();
452 case TIPC_CMD_SET_NODE_ADDR
:
453 rep_tlv_buf
= cfg_set_own_addr();
455 case TIPC_CMD_SET_REMOTE_MNG
:
456 rep_tlv_buf
= cfg_set_remote_mng();
458 case TIPC_CMD_SET_MAX_PORTS
:
459 rep_tlv_buf
= cfg_set_max_ports();
461 case TIPC_CMD_SET_MAX_PUBL
:
462 rep_tlv_buf
= cfg_set_max_publications();
464 case TIPC_CMD_SET_MAX_SUBSCR
:
465 rep_tlv_buf
= cfg_set_max_subscriptions();
467 case TIPC_CMD_SET_MAX_ZONES
:
468 rep_tlv_buf
= cfg_set_max_zones();
470 case TIPC_CMD_SET_MAX_CLUSTERS
:
471 rep_tlv_buf
= cfg_set_max_clusters();
473 case TIPC_CMD_SET_MAX_NODES
:
474 rep_tlv_buf
= cfg_set_max_nodes();
476 case TIPC_CMD_SET_MAX_SLAVES
:
477 rep_tlv_buf
= cfg_set_max_slaves();
479 case TIPC_CMD_SET_NETID
:
480 rep_tlv_buf
= cfg_set_netid();
482 case TIPC_CMD_GET_REMOTE_MNG
:
483 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_remote_management
);
485 case TIPC_CMD_GET_MAX_PORTS
:
486 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_max_ports
);
488 case TIPC_CMD_GET_MAX_PUBL
:
489 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_max_publications
);
491 case TIPC_CMD_GET_MAX_SUBSCR
:
492 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_max_subscriptions
);
494 case TIPC_CMD_GET_MAX_ZONES
:
495 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_max_zones
);
497 case TIPC_CMD_GET_MAX_CLUSTERS
:
498 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_max_clusters
);
500 case TIPC_CMD_GET_MAX_NODES
:
501 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_max_nodes
);
503 case TIPC_CMD_GET_MAX_SLAVES
:
504 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_max_slaves
);
506 case TIPC_CMD_GET_NETID
:
507 rep_tlv_buf
= tipc_cfg_reply_unsigned(tipc_net_id
);
509 case TIPC_CMD_NOT_NET_ADMIN
:
511 tipc_cfg_reply_error_string(TIPC_CFG_NOT_NET_ADMIN
);
514 rep_tlv_buf
= tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
515 " (unknown command)");
519 /* Return reply buffer */
521 spin_unlock_bh(&config_lock
);
525 static void cfg_named_msg_event(void *userdata
,
527 struct sk_buff
**buf
,
531 struct tipc_portid
const *orig
,
532 struct tipc_name_seq
const *dest
)
534 struct tipc_cfg_msg_hdr
*req_hdr
;
535 struct tipc_cfg_msg_hdr
*rep_hdr
;
536 struct sk_buff
*rep_buf
;
538 /* Validate configuration message header (ignore invalid message) */
540 req_hdr
= (struct tipc_cfg_msg_hdr
*)msg
;
541 if ((size
< sizeof(*req_hdr
)) ||
542 (size
!= TCM_ALIGN(ntohl(req_hdr
->tcm_len
))) ||
543 (ntohs(req_hdr
->tcm_flags
) != TCM_F_REQUEST
)) {
544 warn("Invalid configuration message discarded\n");
548 /* 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 msg
+ sizeof(*req_hdr
),
553 size
- sizeof(*req_hdr
),
554 BUF_HEADROOM
+ MAX_H_SIZE
+ sizeof(*rep_hdr
));
556 skb_push(rep_buf
, sizeof(*rep_hdr
));
557 rep_hdr
= (struct tipc_cfg_msg_hdr
*)rep_buf
->data
;
558 memcpy(rep_hdr
, req_hdr
, sizeof(*rep_hdr
));
559 rep_hdr
->tcm_len
= htonl(rep_buf
->len
);
560 rep_hdr
->tcm_flags
&= htons(~TCM_F_REQUEST
);
566 /* NEED TO ADD CODE TO HANDLE FAILED SEND (SUCH AS CONGESTION) */
567 tipc_send_buf2port(port_ref
, orig
, rep_buf
, rep_buf
->len
);
570 int tipc_cfg_init(void)
572 struct tipc_name_seq seq
;
575 res
= tipc_attach(&mng
.user_ref
, NULL
, NULL
);
579 res
= tipc_createport(mng
.user_ref
, NULL
, TIPC_CRITICAL_IMPORTANCE
,
581 NULL
, cfg_named_msg_event
, NULL
,
582 NULL
, &mng
.port_ref
);
586 seq
.type
= TIPC_CFG_SRV
;
587 seq
.lower
= seq
.upper
= tipc_own_addr
;
588 res
= tipc_nametbl_publish_rsv(mng
.port_ref
, TIPC_ZONE_SCOPE
, &seq
);
595 err("Unable to create configuration service\n");
596 tipc_detach(mng
.user_ref
);
601 void tipc_cfg_stop(void)
604 tipc_detach(mng
.user_ref
);