OSDN Git Service

c2ad2ff32a153a71e0edefa7a669f05d9b0ef6c3
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / net / tipc / config.c
1 /*
2  * net/tipc/config.c: TIPC configuration management code
3  *
4  * Copyright (c) 2002-2006, Ericsson AB
5  * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
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.
19  *
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.
23  *
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.
35  */
36
37 #include "core.h"
38 #include "socket.h"
39 #include "name_table.h"
40 #include "config.h"
41 #include "server.h"
42
43 #define REPLY_TRUNCATED "<truncated>\n"
44
45 static const void *req_tlv_area;        /* request message TLV area */
46 static int req_tlv_space;               /* request message TLV area size */
47 static int rep_headroom;                /* reply message headroom to use */
48
49 struct sk_buff *tipc_cfg_reply_alloc(int payload_size)
50 {
51         struct sk_buff *buf;
52
53         buf = alloc_skb(rep_headroom + payload_size, GFP_ATOMIC);
54         if (buf)
55                 skb_reserve(buf, rep_headroom);
56         return buf;
57 }
58
59 int tipc_cfg_append_tlv(struct sk_buff *buf, int tlv_type,
60                         void *tlv_data, int tlv_data_size)
61 {
62         struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(buf);
63         int new_tlv_space = TLV_SPACE(tlv_data_size);
64
65         if (skb_tailroom(buf) < new_tlv_space)
66                 return 0;
67         skb_put(buf, new_tlv_space);
68         tlv->tlv_type = htons(tlv_type);
69         tlv->tlv_len  = htons(TLV_LENGTH(tlv_data_size));
70         if (tlv_data_size && tlv_data)
71                 memcpy(TLV_DATA(tlv), tlv_data, tlv_data_size);
72         return 1;
73 }
74
75 struct sk_buff *tipc_cfg_reply_string_type(u16 tlv_type, char *string)
76 {
77         struct sk_buff *buf;
78         int string_len = strlen(string) + 1;
79
80         buf = tipc_cfg_reply_alloc(TLV_SPACE(string_len));
81         if (buf)
82                 tipc_cfg_append_tlv(buf, tlv_type, string, string_len);
83         return buf;
84 }
85
86 struct sk_buff *tipc_cfg_do_cmd(struct net *net, u32 orig_node, u16 cmd,
87                                 const void *request_area, int request_space,
88                                 int reply_headroom)
89 {
90         struct sk_buff *rep_tlv_buf;
91
92         rtnl_lock();
93
94         /* Save request and reply details in a well-known location */
95         req_tlv_area = request_area;
96         req_tlv_space = request_space;
97         rep_headroom = reply_headroom;
98
99         /* Check command authorization */
100         if (likely(in_own_node(net, orig_node))) {
101                 /* command is permitted */
102         } else {
103                 rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
104                                                           " (cannot be done remotely)");
105                 goto exit;
106         }
107
108         /* Call appropriate processing routine */
109         switch (cmd) {
110         case TIPC_CMD_NOOP:
111                 rep_tlv_buf = tipc_cfg_reply_none();
112                 break;
113         case TIPC_CMD_NOT_NET_ADMIN:
114                 rep_tlv_buf =
115                         tipc_cfg_reply_error_string(TIPC_CFG_NOT_NET_ADMIN);
116                 break;
117         case TIPC_CMD_SET_MAX_ZONES:
118         case TIPC_CMD_GET_MAX_ZONES:
119         case TIPC_CMD_SET_MAX_SLAVES:
120         case TIPC_CMD_GET_MAX_SLAVES:
121         case TIPC_CMD_SET_MAX_CLUSTERS:
122         case TIPC_CMD_GET_MAX_CLUSTERS:
123         case TIPC_CMD_SET_MAX_NODES:
124         case TIPC_CMD_GET_MAX_NODES:
125         case TIPC_CMD_SET_MAX_SUBSCR:
126         case TIPC_CMD_GET_MAX_SUBSCR:
127         case TIPC_CMD_SET_MAX_PUBL:
128         case TIPC_CMD_GET_MAX_PUBL:
129         case TIPC_CMD_SET_LOG_SIZE:
130         case TIPC_CMD_SET_REMOTE_MNG:
131         case TIPC_CMD_GET_REMOTE_MNG:
132         case TIPC_CMD_DUMP_LOG:
133         case TIPC_CMD_SET_MAX_PORTS:
134         case TIPC_CMD_GET_MAX_PORTS:
135                 rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
136                                                           " (obsolete command)");
137                 break;
138         default:
139                 rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
140                                                           " (unknown command)");
141                 break;
142         }
143
144         WARN_ON(rep_tlv_buf->len > TLV_SPACE(ULTRA_STRING_MAX_LEN));
145
146         /* Append an error message if we cannot return all requested data */
147         if (rep_tlv_buf->len == TLV_SPACE(ULTRA_STRING_MAX_LEN)) {
148                 if (*(rep_tlv_buf->data + ULTRA_STRING_MAX_LEN) != '\0')
149                         sprintf(rep_tlv_buf->data + rep_tlv_buf->len -
150                                 sizeof(REPLY_TRUNCATED) - 1, REPLY_TRUNCATED);
151         }
152
153         /* Return reply buffer */
154 exit:
155         rtnl_unlock();
156         return rep_tlv_buf;
157 }