2 * net/tipc/config.c: TIPC configuration management code
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2004-2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
41 #include "name_table.h"
50 struct list_head subd_list
;
57 u32 link_subscriptions
;
58 struct list_head link_subscribers
;
61 static struct manager mng
= { 0};
63 static spinlock_t config_lock
= SPIN_LOCK_UNLOCKED
;
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 void cfg_link_event(u32 addr
, char *name
, int up
)
72 /* TIPC DOESN'T HANDLE LINK EVENT SUBSCRIPTIONS AT THE MOMENT */
76 struct sk_buff
*cfg_reply_alloc(int payload_size
)
80 buf
= alloc_skb(rep_headroom
+ payload_size
, GFP_ATOMIC
);
82 skb_reserve(buf
, rep_headroom
);
86 int cfg_append_tlv(struct sk_buff
*buf
, int tlv_type
,
87 void *tlv_data
, int tlv_data_size
)
89 struct tlv_desc
*tlv
= (struct tlv_desc
*)buf
->tail
;
90 int new_tlv_space
= TLV_SPACE(tlv_data_size
);
92 if (skb_tailroom(buf
) < new_tlv_space
) {
93 dbg("cfg_append_tlv unable to append TLV\n");
96 skb_put(buf
, new_tlv_space
);
97 tlv
->tlv_type
= htons(tlv_type
);
98 tlv
->tlv_len
= htons(TLV_LENGTH(tlv_data_size
));
99 if (tlv_data_size
&& tlv_data
)
100 memcpy(TLV_DATA(tlv
), tlv_data
, tlv_data_size
);
104 struct sk_buff
*cfg_reply_unsigned_type(u16 tlv_type
, u32 value
)
109 buf
= cfg_reply_alloc(TLV_SPACE(sizeof(value
)));
111 value_net
= htonl(value
);
112 cfg_append_tlv(buf
, tlv_type
, &value_net
,
118 struct sk_buff
*cfg_reply_string_type(u16 tlv_type
, char *string
)
121 int string_len
= strlen(string
) + 1;
123 buf
= cfg_reply_alloc(TLV_SPACE(string_len
));
125 cfg_append_tlv(buf
, tlv_type
, string
, string_len
);
134 /* Now obsolete code for handling commands not yet implemented the new way */
136 int tipc_cfg_cmd(const struct tipc_cmd_msg
* msg
,
140 struct tipc_portid
*orig
)
147 case TIPC_REMOVE_LINK
:
148 case TIPC_CMD_BLOCK_LINK
:
149 case TIPC_CMD_UNBLOCK_LINK
:
150 if (!cfg_check_connection(orig
))
151 rv
= link_control(msg
->argv
.link_name
, msg
->cmd
, 0);
157 tipc_isconnected(mng
.conn_port_ref
, &connected
);
158 if (connected
|| !orig
) {
162 rv
= tipc_connect2port(mng
.conn_port_ref
, orig
);
167 case TIPC_GET_PEER_ADDRESS
:
168 *ret_size
= link_peer_addr(msg
->argv
.link_name
, data
, sz
);
170 case TIPC_GET_ROUTES
:
180 static void cfg_cmd_event(struct tipc_cmd_msg
*msg
,
183 struct tipc_portid
const *orig
)
186 struct tipc_cmd_result_msg rmsg
;
187 struct iovec msg_sect
[2];
190 msg
->cmd
= ntohl(msg
->cmd
);
192 cfg_prepare_res_msg(msg
->cmd
, msg
->usr_handle
, rv
, &rmsg
, msg_sect
,
194 if (ntohl(msg
->magic
) != TIPC_MAGIC
)
198 case TIPC_CREATE_LINK
:
199 if (!cfg_check_connection(orig
))
200 rv
= disc_create_link(&msg
->argv
.create_link
);
202 case TIPC_LINK_SUBSCRIBE
:
204 struct subscr_data
*sub
;
206 if (mng
.link_subscriptions
> 64)
208 sub
= (struct subscr_data
*)kmalloc(sizeof(*sub
),
211 warn("Memory squeeze; dropped remote link subscription\n");
214 INIT_LIST_HEAD(&sub
->subd_list
);
215 tipc_createport(mng
.user_ref
,
217 TIPC_HIGH_IMPORTANCE
,
220 (tipc_conn_shutdown_event
)cfg_linksubscr_cancel
,
223 (tipc_conn_msg_event
)cfg_linksubscr_cancel
,
226 if (!sub
->port_ref
) {
230 memcpy(sub
->usr_handle
,msg
->usr_handle
,
231 sizeof(sub
->usr_handle
));
232 sub
->domain
= msg
->argv
.domain
;
233 list_add_tail(&sub
->subd_list
, &mng
.link_subscribers
);
234 tipc_connect2port(sub
->port_ref
, orig
);
235 rmsg
.retval
= TIPC_OK
;
236 tipc_send(sub
->port_ref
, 2u, msg_sect
);
237 mng
.link_subscriptions
++;
241 rv
= tipc_cfg_cmd(msg
, data
, sz
, (u32
*)&msg_sect
[1].iov_len
, orig
);
244 rmsg
.result_len
= htonl(msg_sect
[1].iov_len
);
245 rmsg
.retval
= htonl(rv
);
246 cfg_respond(msg_sect
, 2u, orig
);
250 static struct sk_buff
*cfg_enable_bearer(void)
252 struct tipc_bearer_config
*args
;
254 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_BEARER_CONFIG
))
255 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
257 args
= (struct tipc_bearer_config
*)TLV_DATA(req_tlv_area
);
258 if (tipc_enable_bearer(args
->name
,
259 ntohl(args
->detect_scope
),
260 ntohl(args
->priority
)))
261 return cfg_reply_error_string("unable to enable bearer");
263 return cfg_reply_none();
266 static struct sk_buff
*cfg_disable_bearer(void)
268 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_BEARER_NAME
))
269 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
271 if (tipc_disable_bearer((char *)TLV_DATA(req_tlv_area
)))
272 return cfg_reply_error_string("unable to disable bearer");
274 return cfg_reply_none();
277 static struct sk_buff
*cfg_set_own_addr(void)
281 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
282 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
284 addr
= *(u32
*)TLV_DATA(req_tlv_area
);
286 if (addr
== tipc_own_addr
)
287 return cfg_reply_none();
288 if (!addr_node_valid(addr
))
289 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
292 return cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
293 " (cannot change node address once assigned)");
295 spin_unlock_bh(&config_lock
);
297 tipc_own_addr
= addr
;
299 spin_lock_bh(&config_lock
);
300 return cfg_reply_none();
303 static struct sk_buff
*cfg_set_remote_mng(void)
307 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
308 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
310 value
= *(u32
*)TLV_DATA(req_tlv_area
);
311 value
= ntohl(value
);
312 tipc_remote_management
= (value
!= 0);
313 return cfg_reply_none();
316 static struct sk_buff
*cfg_set_max_publications(void)
320 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
321 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
323 value
= *(u32
*)TLV_DATA(req_tlv_area
);
324 value
= ntohl(value
);
325 if (value
!= delimit(value
, 1, 65535))
326 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
327 " (max publications must be 1-65535)");
328 tipc_max_publications
= value
;
329 return cfg_reply_none();
332 static struct sk_buff
*cfg_set_max_subscriptions(void)
336 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
337 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
339 value
= *(u32
*)TLV_DATA(req_tlv_area
);
340 value
= ntohl(value
);
341 if (value
!= delimit(value
, 1, 65535))
342 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
343 " (max subscriptions must be 1-65535");
344 tipc_max_subscriptions
= value
;
345 return cfg_reply_none();
348 static struct sk_buff
*cfg_set_max_ports(void)
353 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
354 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
355 value
= *(u32
*)TLV_DATA(req_tlv_area
);
356 value
= ntohl(value
);
357 if (value
!= delimit(value
, 127, 65535))
358 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
359 " (max ports must be 127-65535)");
361 if (value
== tipc_max_ports
)
362 return cfg_reply_none();
364 if (atomic_read(&tipc_user_count
) > 2)
365 return cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
366 " (cannot change max ports while TIPC users exist)");
368 spin_unlock_bh(&config_lock
);
369 orig_mode
= tipc_get_mode();
370 if (orig_mode
== TIPC_NET_MODE
)
373 tipc_max_ports
= value
;
375 if (orig_mode
== TIPC_NET_MODE
)
377 spin_lock_bh(&config_lock
);
378 return cfg_reply_none();
381 static struct sk_buff
*set_net_max(int value
, int *parameter
)
385 if (value
!= *parameter
) {
386 orig_mode
= tipc_get_mode();
387 if (orig_mode
== TIPC_NET_MODE
)
390 if (orig_mode
== TIPC_NET_MODE
)
394 return cfg_reply_none();
397 static struct sk_buff
*cfg_set_max_zones(void)
401 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
402 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
403 value
= *(u32
*)TLV_DATA(req_tlv_area
);
404 value
= ntohl(value
);
405 if (value
!= delimit(value
, 1, 255))
406 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
407 " (max zones must be 1-255)");
408 return set_net_max(value
, &tipc_max_zones
);
411 static struct sk_buff
*cfg_set_max_clusters(void)
415 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
416 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
417 value
= *(u32
*)TLV_DATA(req_tlv_area
);
418 value
= ntohl(value
);
420 return cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
421 " (max clusters fixed at 1)");
422 return cfg_reply_none();
425 static struct sk_buff
*cfg_set_max_nodes(void)
429 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
430 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
431 value
= *(u32
*)TLV_DATA(req_tlv_area
);
432 value
= ntohl(value
);
433 if (value
!= delimit(value
, 8, 2047))
434 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
435 " (max nodes must be 8-2047)");
436 return set_net_max(value
, &tipc_max_nodes
);
439 static struct sk_buff
*cfg_set_max_slaves(void)
443 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
444 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
445 value
= *(u32
*)TLV_DATA(req_tlv_area
);
446 value
= ntohl(value
);
448 return cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
449 " (max secondary nodes fixed at 0)");
450 return cfg_reply_none();
453 static struct sk_buff
*cfg_set_netid(void)
457 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
458 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
459 value
= *(u32
*)TLV_DATA(req_tlv_area
);
460 value
= ntohl(value
);
461 if (value
!= delimit(value
, 1, 9999))
462 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
463 " (network id must be 1-9999)");
466 return cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
467 " (cannot change network id once part of network)");
469 return set_net_max(value
, &tipc_net_id
);
472 struct sk_buff
*cfg_do_cmd(u32 orig_node
, u16 cmd
, const void *request_area
,
473 int request_space
, int reply_headroom
)
475 struct sk_buff
*rep_tlv_buf
;
477 spin_lock_bh(&config_lock
);
479 /* Save request and reply details in a well-known location */
481 req_tlv_area
= request_area
;
482 req_tlv_space
= request_space
;
483 rep_headroom
= reply_headroom
;
485 /* Check command authorization */
487 if (likely(orig_node
== tipc_own_addr
)) {
488 /* command is permitted */
489 } else if (cmd
>= 0x8000) {
490 rep_tlv_buf
= cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
491 " (cannot be done remotely)");
493 } else if (!tipc_remote_management
) {
494 rep_tlv_buf
= cfg_reply_error_string(TIPC_CFG_NO_REMOTE
);
497 else if (cmd
>= 0x4000) {
500 if ((nametbl_translate(TIPC_ZM_SRV
, 0, &domain
) == 0) ||
501 (domain
!= orig_node
)) {
502 rep_tlv_buf
= cfg_reply_error_string(TIPC_CFG_NOT_ZONE_MSTR
);
507 /* Call appropriate processing routine */
511 rep_tlv_buf
= cfg_reply_none();
513 case TIPC_CMD_GET_NODES
:
514 rep_tlv_buf
= node_get_nodes(req_tlv_area
, req_tlv_space
);
516 case TIPC_CMD_GET_LINKS
:
517 rep_tlv_buf
= node_get_links(req_tlv_area
, req_tlv_space
);
519 case TIPC_CMD_SHOW_LINK_STATS
:
520 rep_tlv_buf
= link_cmd_show_stats(req_tlv_area
, req_tlv_space
);
522 case TIPC_CMD_RESET_LINK_STATS
:
523 rep_tlv_buf
= link_cmd_reset_stats(req_tlv_area
, req_tlv_space
);
525 case TIPC_CMD_SHOW_NAME_TABLE
:
526 rep_tlv_buf
= nametbl_get(req_tlv_area
, req_tlv_space
);
528 case TIPC_CMD_GET_BEARER_NAMES
:
529 rep_tlv_buf
= bearer_get_names();
531 case TIPC_CMD_GET_MEDIA_NAMES
:
532 rep_tlv_buf
= media_get_names();
534 case TIPC_CMD_SHOW_PORTS
:
535 rep_tlv_buf
= port_get_ports();
538 case TIPC_CMD_SHOW_PORT_STATS
:
539 rep_tlv_buf
= port_show_stats(req_tlv_area
, req_tlv_space
);
541 case TIPC_CMD_RESET_PORT_STATS
:
542 rep_tlv_buf
= cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
);
545 case TIPC_CMD_SET_LOG_SIZE
:
546 rep_tlv_buf
= log_resize(req_tlv_area
, req_tlv_space
);
548 case TIPC_CMD_DUMP_LOG
:
549 rep_tlv_buf
= log_dump();
551 case TIPC_CMD_SET_LINK_TOL
:
552 case TIPC_CMD_SET_LINK_PRI
:
553 case TIPC_CMD_SET_LINK_WINDOW
:
554 rep_tlv_buf
= link_cmd_config(req_tlv_area
, req_tlv_space
, cmd
);
556 case TIPC_CMD_ENABLE_BEARER
:
557 rep_tlv_buf
= cfg_enable_bearer();
559 case TIPC_CMD_DISABLE_BEARER
:
560 rep_tlv_buf
= cfg_disable_bearer();
562 case TIPC_CMD_SET_NODE_ADDR
:
563 rep_tlv_buf
= cfg_set_own_addr();
565 case TIPC_CMD_SET_REMOTE_MNG
:
566 rep_tlv_buf
= cfg_set_remote_mng();
568 case TIPC_CMD_SET_MAX_PORTS
:
569 rep_tlv_buf
= cfg_set_max_ports();
571 case TIPC_CMD_SET_MAX_PUBL
:
572 rep_tlv_buf
= cfg_set_max_publications();
574 case TIPC_CMD_SET_MAX_SUBSCR
:
575 rep_tlv_buf
= cfg_set_max_subscriptions();
577 case TIPC_CMD_SET_MAX_ZONES
:
578 rep_tlv_buf
= cfg_set_max_zones();
580 case TIPC_CMD_SET_MAX_CLUSTERS
:
581 rep_tlv_buf
= cfg_set_max_clusters();
583 case TIPC_CMD_SET_MAX_NODES
:
584 rep_tlv_buf
= cfg_set_max_nodes();
586 case TIPC_CMD_SET_MAX_SLAVES
:
587 rep_tlv_buf
= cfg_set_max_slaves();
589 case TIPC_CMD_SET_NETID
:
590 rep_tlv_buf
= cfg_set_netid();
592 case TIPC_CMD_GET_REMOTE_MNG
:
593 rep_tlv_buf
= cfg_reply_unsigned(tipc_remote_management
);
595 case TIPC_CMD_GET_MAX_PORTS
:
596 rep_tlv_buf
= cfg_reply_unsigned(tipc_max_ports
);
598 case TIPC_CMD_GET_MAX_PUBL
:
599 rep_tlv_buf
= cfg_reply_unsigned(tipc_max_publications
);
601 case TIPC_CMD_GET_MAX_SUBSCR
:
602 rep_tlv_buf
= cfg_reply_unsigned(tipc_max_subscriptions
);
604 case TIPC_CMD_GET_MAX_ZONES
:
605 rep_tlv_buf
= cfg_reply_unsigned(tipc_max_zones
);
607 case TIPC_CMD_GET_MAX_CLUSTERS
:
608 rep_tlv_buf
= cfg_reply_unsigned(tipc_max_clusters
);
610 case TIPC_CMD_GET_MAX_NODES
:
611 rep_tlv_buf
= cfg_reply_unsigned(tipc_max_nodes
);
613 case TIPC_CMD_GET_MAX_SLAVES
:
614 rep_tlv_buf
= cfg_reply_unsigned(tipc_max_slaves
);
616 case TIPC_CMD_GET_NETID
:
617 rep_tlv_buf
= cfg_reply_unsigned(tipc_net_id
);
624 /* Return reply buffer */
626 spin_unlock_bh(&config_lock
);
630 static void cfg_named_msg_event(void *userdata
,
632 struct sk_buff
**buf
,
636 struct tipc_portid
const *orig
,
637 struct tipc_name_seq
const *dest
)
639 struct tipc_cfg_msg_hdr
*req_hdr
;
640 struct tipc_cfg_msg_hdr
*rep_hdr
;
641 struct sk_buff
*rep_buf
;
643 /* Validate configuration message header (ignore invalid message) */
645 req_hdr
= (struct tipc_cfg_msg_hdr
*)msg
;
646 if ((size
< sizeof(*req_hdr
)) ||
647 (size
!= TCM_ALIGN(ntohl(req_hdr
->tcm_len
))) ||
648 (ntohs(req_hdr
->tcm_flags
) != TCM_F_REQUEST
)) {
649 warn("discarded invalid configuration message\n");
653 /* Generate reply for request (if can't, return request) */
655 rep_buf
= cfg_do_cmd(orig
->node
,
656 ntohs(req_hdr
->tcm_type
),
657 msg
+ sizeof(*req_hdr
),
658 size
- sizeof(*req_hdr
),
659 BUF_HEADROOM
+ MAX_H_SIZE
+ sizeof(*rep_hdr
));
661 skb_push(rep_buf
, sizeof(*rep_hdr
));
662 rep_hdr
= (struct tipc_cfg_msg_hdr
*)rep_buf
->data
;
663 memcpy(rep_hdr
, req_hdr
, sizeof(*rep_hdr
));
664 rep_hdr
->tcm_len
= htonl(rep_buf
->len
);
665 rep_hdr
->tcm_flags
&= htons(~TCM_F_REQUEST
);
671 /* NEED TO ADD CODE TO HANDLE FAILED SEND (SUCH AS CONGESTION) */
672 tipc_send_buf2port(port_ref
, orig
, rep_buf
, rep_buf
->len
);
677 struct tipc_name_seq seq
;
680 memset(&mng
, 0, sizeof(mng
));
681 INIT_LIST_HEAD(&mng
.link_subscribers
);
683 res
= tipc_attach(&mng
.user_ref
, 0, 0);
687 res
= tipc_createport(mng
.user_ref
, 0, TIPC_CRITICAL_IMPORTANCE
,
689 NULL
, cfg_named_msg_event
, NULL
,
690 NULL
, &mng
.port_ref
);
694 seq
.type
= TIPC_CFG_SRV
;
695 seq
.lower
= seq
.upper
= tipc_own_addr
;
696 res
= nametbl_publish_rsv(mng
.port_ref
, TIPC_ZONE_SCOPE
, &seq
);
703 err("Unable to create configuration service\n");
704 tipc_detach(mng
.user_ref
);
712 tipc_detach(mng
.user_ref
);