Merge commit '4cb38750d49010ae72e718d46605ac9ba5a851b4' into stable/for-linus-3.6
[cascardo/linux.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-2011, 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 "port.h"
39 #include "name_table.h"
40 #include "config.h"
41
42 #define REPLY_TRUNCATED "<truncated>\n"
43
44 static u32 config_port_ref;
45
46 static DEFINE_SPINLOCK(config_lock);
47
48 static const void *req_tlv_area;        /* request message TLV area */
49 static int req_tlv_space;               /* request message TLV area size */
50 static int rep_headroom;                /* reply message headroom to use */
51
52
53 struct sk_buff *tipc_cfg_reply_alloc(int payload_size)
54 {
55         struct sk_buff *buf;
56
57         buf = alloc_skb(rep_headroom + payload_size, GFP_ATOMIC);
58         if (buf)
59                 skb_reserve(buf, rep_headroom);
60         return buf;
61 }
62
63 int tipc_cfg_append_tlv(struct sk_buff *buf, int tlv_type,
64                         void *tlv_data, int tlv_data_size)
65 {
66         struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(buf);
67         int new_tlv_space = TLV_SPACE(tlv_data_size);
68
69         if (skb_tailroom(buf) < new_tlv_space)
70                 return 0;
71         skb_put(buf, new_tlv_space);
72         tlv->tlv_type = htons(tlv_type);
73         tlv->tlv_len  = htons(TLV_LENGTH(tlv_data_size));
74         if (tlv_data_size && tlv_data)
75                 memcpy(TLV_DATA(tlv), tlv_data, tlv_data_size);
76         return 1;
77 }
78
79 static struct sk_buff *tipc_cfg_reply_unsigned_type(u16 tlv_type, u32 value)
80 {
81         struct sk_buff *buf;
82         __be32 value_net;
83
84         buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(value)));
85         if (buf) {
86                 value_net = htonl(value);
87                 tipc_cfg_append_tlv(buf, tlv_type, &value_net,
88                                     sizeof(value_net));
89         }
90         return buf;
91 }
92
93 static struct sk_buff *tipc_cfg_reply_unsigned(u32 value)
94 {
95         return tipc_cfg_reply_unsigned_type(TIPC_TLV_UNSIGNED, value);
96 }
97
98 struct sk_buff *tipc_cfg_reply_string_type(u16 tlv_type, char *string)
99 {
100         struct sk_buff *buf;
101         int string_len = strlen(string) + 1;
102
103         buf = tipc_cfg_reply_alloc(TLV_SPACE(string_len));
104         if (buf)
105                 tipc_cfg_append_tlv(buf, tlv_type, string, string_len);
106         return buf;
107 }
108
109 static struct sk_buff *tipc_show_stats(void)
110 {
111         struct sk_buff *buf;
112         struct tlv_desc *rep_tlv;
113         char *pb;
114         int pb_len;
115         int str_len;
116         u32 value;
117
118         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
119                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
120
121         value = ntohl(*(u32 *)TLV_DATA(req_tlv_area));
122         if (value != 0)
123                 return tipc_cfg_reply_error_string("unsupported argument");
124
125         buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
126         if (buf == NULL)
127                 return NULL;
128
129         rep_tlv = (struct tlv_desc *)buf->data;
130         pb = TLV_DATA(rep_tlv);
131         pb_len = ULTRA_STRING_MAX_LEN;
132
133         str_len = tipc_snprintf(pb, pb_len, "TIPC version " TIPC_MOD_VER "\n");
134         str_len += 1;   /* for "\0" */
135         skb_put(buf, TLV_SPACE(str_len));
136         TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
137
138         return buf;
139 }
140
141 static struct sk_buff *cfg_enable_bearer(void)
142 {
143         struct tipc_bearer_config *args;
144
145         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_BEARER_CONFIG))
146                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
147
148         args = (struct tipc_bearer_config *)TLV_DATA(req_tlv_area);
149         if (tipc_enable_bearer(args->name,
150                                ntohl(args->disc_domain),
151                                ntohl(args->priority)))
152                 return tipc_cfg_reply_error_string("unable to enable bearer");
153
154         return tipc_cfg_reply_none();
155 }
156
157 static struct sk_buff *cfg_disable_bearer(void)
158 {
159         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_BEARER_NAME))
160                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
161
162         if (tipc_disable_bearer((char *)TLV_DATA(req_tlv_area)))
163                 return tipc_cfg_reply_error_string("unable to disable bearer");
164
165         return tipc_cfg_reply_none();
166 }
167
168 static struct sk_buff *cfg_set_own_addr(void)
169 {
170         u32 addr;
171
172         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
173                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
174
175         addr = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
176         if (addr == tipc_own_addr)
177                 return tipc_cfg_reply_none();
178         if (!tipc_addr_node_valid(addr))
179                 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
180                                                    " (node address)");
181         if (tipc_own_addr)
182                 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
183                                                    " (cannot change node address once assigned)");
184
185         /*
186          * Must temporarily release configuration spinlock while switching into
187          * networking mode as it calls tipc_eth_media_start(), which may sleep.
188          * Releasing the lock is harmless as other locally-issued configuration
189          * commands won't occur until this one completes, and remotely-issued
190          * configuration commands can't be received until a local configuration
191          * command to enable the first bearer is received and processed.
192          */
193         spin_unlock_bh(&config_lock);
194         tipc_core_start_net(addr);
195         spin_lock_bh(&config_lock);
196         return tipc_cfg_reply_none();
197 }
198
199 static struct sk_buff *cfg_set_remote_mng(void)
200 {
201         u32 value;
202
203         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
204                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
205
206         value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
207         tipc_remote_management = (value != 0);
208         return tipc_cfg_reply_none();
209 }
210
211 static struct sk_buff *cfg_set_max_publications(void)
212 {
213         u32 value;
214
215         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
216                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
217
218         value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
219         if (value < 1 || value > 65535)
220                 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
221                                                    " (max publications must be 1-65535)");
222         tipc_max_publications = value;
223         return tipc_cfg_reply_none();
224 }
225
226 static struct sk_buff *cfg_set_max_subscriptions(void)
227 {
228         u32 value;
229
230         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
231                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
232
233         value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
234         if (value < 1 || value > 65535)
235                 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
236                                                    " (max subscriptions must be 1-65535");
237         tipc_max_subscriptions = value;
238         return tipc_cfg_reply_none();
239 }
240
241 static struct sk_buff *cfg_set_max_ports(void)
242 {
243         u32 value;
244
245         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
246                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
247         value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
248         if (value == tipc_max_ports)
249                 return tipc_cfg_reply_none();
250         if (value < 127 || value > 65535)
251                 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
252                                                    " (max ports must be 127-65535)");
253         return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
254                 " (cannot change max ports while TIPC is active)");
255 }
256
257 static struct sk_buff *cfg_set_netid(void)
258 {
259         u32 value;
260
261         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
262                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
263         value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
264         if (value == tipc_net_id)
265                 return tipc_cfg_reply_none();
266         if (value < 1 || value > 9999)
267                 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
268                                                    " (network id must be 1-9999)");
269         if (tipc_own_addr)
270                 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
271                         " (cannot change network id once TIPC has joined a network)");
272         tipc_net_id = value;
273         return tipc_cfg_reply_none();
274 }
275
276 struct sk_buff *tipc_cfg_do_cmd(u32 orig_node, u16 cmd, const void *request_area,
277                                 int request_space, int reply_headroom)
278 {
279         struct sk_buff *rep_tlv_buf;
280
281         spin_lock_bh(&config_lock);
282
283         /* Save request and reply details in a well-known location */
284         req_tlv_area = request_area;
285         req_tlv_space = request_space;
286         rep_headroom = reply_headroom;
287
288         /* Check command authorization */
289         if (likely(in_own_node(orig_node))) {
290                 /* command is permitted */
291         } else if (cmd >= 0x8000) {
292                 rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
293                                                           " (cannot be done remotely)");
294                 goto exit;
295         } else if (!tipc_remote_management) {
296                 rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NO_REMOTE);
297                 goto exit;
298         } else if (cmd >= 0x4000) {
299                 u32 domain = 0;
300
301                 if ((tipc_nametbl_translate(TIPC_ZM_SRV, 0, &domain) == 0) ||
302                     (domain != orig_node)) {
303                         rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_ZONE_MSTR);
304                         goto exit;
305                 }
306         }
307
308         /* Call appropriate processing routine */
309         switch (cmd) {
310         case TIPC_CMD_NOOP:
311                 rep_tlv_buf = tipc_cfg_reply_none();
312                 break;
313         case TIPC_CMD_GET_NODES:
314                 rep_tlv_buf = tipc_node_get_nodes(req_tlv_area, req_tlv_space);
315                 break;
316         case TIPC_CMD_GET_LINKS:
317                 rep_tlv_buf = tipc_node_get_links(req_tlv_area, req_tlv_space);
318                 break;
319         case TIPC_CMD_SHOW_LINK_STATS:
320                 rep_tlv_buf = tipc_link_cmd_show_stats(req_tlv_area, req_tlv_space);
321                 break;
322         case TIPC_CMD_RESET_LINK_STATS:
323                 rep_tlv_buf = tipc_link_cmd_reset_stats(req_tlv_area, req_tlv_space);
324                 break;
325         case TIPC_CMD_SHOW_NAME_TABLE:
326                 rep_tlv_buf = tipc_nametbl_get(req_tlv_area, req_tlv_space);
327                 break;
328         case TIPC_CMD_GET_BEARER_NAMES:
329                 rep_tlv_buf = tipc_bearer_get_names();
330                 break;
331         case TIPC_CMD_GET_MEDIA_NAMES:
332                 rep_tlv_buf = tipc_media_get_names();
333                 break;
334         case TIPC_CMD_SHOW_PORTS:
335                 rep_tlv_buf = tipc_port_get_ports();
336                 break;
337         case TIPC_CMD_SHOW_STATS:
338                 rep_tlv_buf = tipc_show_stats();
339                 break;
340         case TIPC_CMD_SET_LINK_TOL:
341         case TIPC_CMD_SET_LINK_PRI:
342         case TIPC_CMD_SET_LINK_WINDOW:
343                 rep_tlv_buf = tipc_link_cmd_config(req_tlv_area, req_tlv_space, cmd);
344                 break;
345         case TIPC_CMD_ENABLE_BEARER:
346                 rep_tlv_buf = cfg_enable_bearer();
347                 break;
348         case TIPC_CMD_DISABLE_BEARER:
349                 rep_tlv_buf = cfg_disable_bearer();
350                 break;
351         case TIPC_CMD_SET_NODE_ADDR:
352                 rep_tlv_buf = cfg_set_own_addr();
353                 break;
354         case TIPC_CMD_SET_REMOTE_MNG:
355                 rep_tlv_buf = cfg_set_remote_mng();
356                 break;
357         case TIPC_CMD_SET_MAX_PORTS:
358                 rep_tlv_buf = cfg_set_max_ports();
359                 break;
360         case TIPC_CMD_SET_MAX_PUBL:
361                 rep_tlv_buf = cfg_set_max_publications();
362                 break;
363         case TIPC_CMD_SET_MAX_SUBSCR:
364                 rep_tlv_buf = cfg_set_max_subscriptions();
365                 break;
366         case TIPC_CMD_SET_NETID:
367                 rep_tlv_buf = cfg_set_netid();
368                 break;
369         case TIPC_CMD_GET_REMOTE_MNG:
370                 rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_remote_management);
371                 break;
372         case TIPC_CMD_GET_MAX_PORTS:
373                 rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_ports);
374                 break;
375         case TIPC_CMD_GET_MAX_PUBL:
376                 rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_publications);
377                 break;
378         case TIPC_CMD_GET_MAX_SUBSCR:
379                 rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_max_subscriptions);
380                 break;
381         case TIPC_CMD_GET_NETID:
382                 rep_tlv_buf = tipc_cfg_reply_unsigned(tipc_net_id);
383                 break;
384         case TIPC_CMD_NOT_NET_ADMIN:
385                 rep_tlv_buf =
386                         tipc_cfg_reply_error_string(TIPC_CFG_NOT_NET_ADMIN);
387                 break;
388         case TIPC_CMD_SET_MAX_ZONES:
389         case TIPC_CMD_GET_MAX_ZONES:
390         case TIPC_CMD_SET_MAX_SLAVES:
391         case TIPC_CMD_GET_MAX_SLAVES:
392         case TIPC_CMD_SET_MAX_CLUSTERS:
393         case TIPC_CMD_GET_MAX_CLUSTERS:
394         case TIPC_CMD_SET_MAX_NODES:
395         case TIPC_CMD_GET_MAX_NODES:
396         case TIPC_CMD_SET_LOG_SIZE:
397         case TIPC_CMD_DUMP_LOG:
398                 rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
399                                                           " (obsolete command)");
400                 break;
401         default:
402                 rep_tlv_buf = tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
403                                                           " (unknown command)");
404                 break;
405         }
406
407         WARN_ON(rep_tlv_buf->len > TLV_SPACE(ULTRA_STRING_MAX_LEN));
408
409         /* Append an error message if we cannot return all requested data */
410         if (rep_tlv_buf->len == TLV_SPACE(ULTRA_STRING_MAX_LEN)) {
411                 if (*(rep_tlv_buf->data + ULTRA_STRING_MAX_LEN) != '\0')
412                         sprintf(rep_tlv_buf->data + rep_tlv_buf->len -
413                                 sizeof(REPLY_TRUNCATED) - 1, REPLY_TRUNCATED);
414         }
415
416         /* Return reply buffer */
417 exit:
418         spin_unlock_bh(&config_lock);
419         return rep_tlv_buf;
420 }
421
422 static void cfg_named_msg_event(void *userdata,
423                                 u32 port_ref,
424                                 struct sk_buff **buf,
425                                 const unchar *msg,
426                                 u32 size,
427                                 u32 importance,
428                                 struct tipc_portid const *orig,
429                                 struct tipc_name_seq const *dest)
430 {
431         struct tipc_cfg_msg_hdr *req_hdr;
432         struct tipc_cfg_msg_hdr *rep_hdr;
433         struct sk_buff *rep_buf;
434
435         /* Validate configuration message header (ignore invalid message) */
436         req_hdr = (struct tipc_cfg_msg_hdr *)msg;
437         if ((size < sizeof(*req_hdr)) ||
438             (size != TCM_ALIGN(ntohl(req_hdr->tcm_len))) ||
439             (ntohs(req_hdr->tcm_flags) != TCM_F_REQUEST)) {
440                 pr_warn("Invalid configuration message discarded\n");
441                 return;
442         }
443
444         /* Generate reply for request (if can't, return request) */
445         rep_buf = tipc_cfg_do_cmd(orig->node,
446                                   ntohs(req_hdr->tcm_type),
447                                   msg + sizeof(*req_hdr),
448                                   size - sizeof(*req_hdr),
449                                   BUF_HEADROOM + MAX_H_SIZE + sizeof(*rep_hdr));
450         if (rep_buf) {
451                 skb_push(rep_buf, sizeof(*rep_hdr));
452                 rep_hdr = (struct tipc_cfg_msg_hdr *)rep_buf->data;
453                 memcpy(rep_hdr, req_hdr, sizeof(*rep_hdr));
454                 rep_hdr->tcm_len = htonl(rep_buf->len);
455                 rep_hdr->tcm_flags &= htons(~TCM_F_REQUEST);
456         } else {
457                 rep_buf = *buf;
458                 *buf = NULL;
459         }
460
461         /* NEED TO ADD CODE TO HANDLE FAILED SEND (SUCH AS CONGESTION) */
462         tipc_send_buf2port(port_ref, orig, rep_buf, rep_buf->len);
463 }
464
465 int tipc_cfg_init(void)
466 {
467         struct tipc_name_seq seq;
468         int res;
469
470         res = tipc_createport(NULL, TIPC_CRITICAL_IMPORTANCE,
471                               NULL, NULL, NULL,
472                               NULL, cfg_named_msg_event, NULL,
473                               NULL, &config_port_ref);
474         if (res)
475                 goto failed;
476
477         seq.type = TIPC_CFG_SRV;
478         seq.lower = seq.upper = tipc_own_addr;
479         res = tipc_publish(config_port_ref, TIPC_ZONE_SCOPE, &seq);
480         if (res)
481                 goto failed;
482
483         return 0;
484
485 failed:
486         pr_err("Unable to create configuration service\n");
487         return res;
488 }
489
490 void tipc_cfg_reinit(void)
491 {
492         struct tipc_name_seq seq;
493         int res;
494
495         seq.type = TIPC_CFG_SRV;
496         seq.lower = seq.upper = 0;
497         tipc_withdraw(config_port_ref, TIPC_ZONE_SCOPE, &seq);
498
499         seq.lower = seq.upper = tipc_own_addr;
500         res = tipc_publish(config_port_ref, TIPC_ZONE_SCOPE, &seq);
501         if (res)
502                 pr_err("Unable to reinitialize configuration service\n");
503 }
504
505 void tipc_cfg_stop(void)
506 {
507         tipc_deleteport(config_port_ref);
508         config_port_ref = 0;
509 }