ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / tipc / netlink_compat.c
blobf86c6555a539efb0b11979df558231307a7b62e9
1 /*
2 * Copyright (c) 2014, Ericsson AB
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the names of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
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.
34 #include "core.h"
35 #include "bearer.h"
36 #include "link.h"
37 #include "name_table.h"
38 #include "socket.h"
39 #include "node.h"
40 #include "net.h"
41 #include <net/genetlink.h>
42 #include <linux/tipc_config.h>
44 /* The legacy API had an artificial message length limit called
45 * ULTRA_STRING_MAX_LEN.
47 #define ULTRA_STRING_MAX_LEN 32768
49 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
51 #define REPLY_TRUNCATED "<truncated>\n"
53 struct tipc_nl_compat_msg {
54 u16 cmd;
55 int rep_type;
56 int rep_size;
57 int req_type;
58 struct net *net;
59 struct sk_buff *rep;
60 struct tlv_desc *req;
61 struct sock *dst_sk;
64 struct tipc_nl_compat_cmd_dump {
65 int (*header)(struct tipc_nl_compat_msg *);
66 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
67 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
70 struct tipc_nl_compat_cmd_doit {
71 int (*doit)(struct sk_buff *skb, struct genl_info *info);
72 int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
73 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
76 static int tipc_skb_tailroom(struct sk_buff *skb)
78 int tailroom;
79 int limit;
81 tailroom = skb_tailroom(skb);
82 limit = TIPC_SKB_MAX - skb->len;
84 if (tailroom < limit)
85 return tailroom;
87 return limit;
90 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
92 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
94 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
95 return -EMSGSIZE;
97 skb_put(skb, TLV_SPACE(len));
98 tlv->tlv_type = htons(type);
99 tlv->tlv_len = htons(TLV_LENGTH(len));
100 if (len && data)
101 memcpy(TLV_DATA(tlv), data, len);
103 return 0;
106 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
108 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
110 TLV_SET_LEN(tlv, 0);
111 TLV_SET_TYPE(tlv, type);
112 skb_put(skb, sizeof(struct tlv_desc));
115 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
117 int n;
118 u16 len;
119 u32 rem;
120 char *buf;
121 struct tlv_desc *tlv;
122 va_list args;
124 rem = tipc_skb_tailroom(skb);
126 tlv = (struct tlv_desc *)skb->data;
127 len = TLV_GET_LEN(tlv);
128 buf = TLV_DATA(tlv) + len;
130 va_start(args, fmt);
131 n = vscnprintf(buf, rem, fmt, args);
132 va_end(args);
134 TLV_SET_LEN(tlv, n + len);
135 skb_put(skb, n);
137 return n;
140 static struct sk_buff *tipc_tlv_alloc(int size)
142 int hdr_len;
143 struct sk_buff *buf;
145 size = TLV_SPACE(size);
146 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
148 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
149 if (!buf)
150 return NULL;
152 skb_reserve(buf, hdr_len);
154 return buf;
157 static struct sk_buff *tipc_get_err_tlv(char *str)
159 int str_len = strlen(str) + 1;
160 struct sk_buff *buf;
162 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
163 if (buf)
164 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
166 return buf;
169 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
170 struct tipc_nl_compat_msg *msg,
171 struct sk_buff *arg)
173 int len = 0;
174 int err;
175 struct sk_buff *buf;
176 struct nlmsghdr *nlmsg;
177 struct netlink_callback cb;
179 memset(&cb, 0, sizeof(cb));
180 cb.nlh = (struct nlmsghdr *)arg->data;
181 cb.skb = arg;
183 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
184 if (!buf)
185 return -ENOMEM;
187 buf->sk = msg->dst_sk;
189 do {
190 int rem;
192 len = (*cmd->dumpit)(buf, &cb);
194 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
195 struct nlattr **attrs;
197 err = tipc_nlmsg_parse(nlmsg, &attrs);
198 if (err)
199 goto err_out;
201 err = (*cmd->format)(msg, attrs);
202 if (err)
203 goto err_out;
205 if (tipc_skb_tailroom(msg->rep) <= 1) {
206 err = -EMSGSIZE;
207 goto err_out;
211 skb_reset_tail_pointer(buf);
212 buf->len = 0;
214 } while (len);
216 err = 0;
218 err_out:
219 kfree_skb(buf);
221 if (err == -EMSGSIZE) {
222 /* The legacy API only considered messages filling
223 * "ULTRA_STRING_MAX_LEN" to be truncated.
225 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
226 char *tail = skb_tail_pointer(msg->rep);
228 if (*tail != '\0')
229 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
230 REPLY_TRUNCATED);
233 return 0;
236 return err;
239 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
240 struct tipc_nl_compat_msg *msg)
242 int err;
243 struct sk_buff *arg;
245 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
246 return -EINVAL;
248 msg->rep = tipc_tlv_alloc(msg->rep_size);
249 if (!msg->rep)
250 return -ENOMEM;
252 if (msg->rep_type)
253 tipc_tlv_init(msg->rep, msg->rep_type);
255 if (cmd->header)
256 (*cmd->header)(msg);
258 arg = nlmsg_new(0, GFP_KERNEL);
259 if (!arg) {
260 kfree_skb(msg->rep);
261 msg->rep = NULL;
262 return -ENOMEM;
265 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
266 if (err) {
267 kfree_skb(msg->rep);
268 msg->rep = NULL;
270 kfree_skb(arg);
272 return err;
275 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
276 struct tipc_nl_compat_msg *msg)
278 int err;
279 struct sk_buff *doit_buf;
280 struct sk_buff *trans_buf;
281 struct nlattr **attrbuf;
282 struct genl_info info;
284 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
285 if (!trans_buf)
286 return -ENOMEM;
288 err = (*cmd->transcode)(cmd, trans_buf, msg);
289 if (err)
290 goto trans_out;
292 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
293 sizeof(struct nlattr *), GFP_KERNEL);
294 if (!attrbuf) {
295 err = -ENOMEM;
296 goto trans_out;
299 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
300 (const struct nlattr *)trans_buf->data,
301 trans_buf->len, NULL);
302 if (err)
303 goto parse_out;
305 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
306 if (!doit_buf) {
307 err = -ENOMEM;
308 goto parse_out;
311 doit_buf->sk = msg->dst_sk;
313 memset(&info, 0, sizeof(info));
314 info.attrs = attrbuf;
316 err = (*cmd->doit)(doit_buf, &info);
318 kfree_skb(doit_buf);
319 parse_out:
320 kfree(attrbuf);
321 trans_out:
322 kfree_skb(trans_buf);
324 return err;
327 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
328 struct tipc_nl_compat_msg *msg)
330 int err;
332 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
333 return -EINVAL;
335 err = __tipc_nl_compat_doit(cmd, msg);
336 if (err)
337 return err;
339 /* The legacy API considered an empty message a success message */
340 msg->rep = tipc_tlv_alloc(0);
341 if (!msg->rep)
342 return -ENOMEM;
344 return 0;
347 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
348 struct nlattr **attrs)
350 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
352 nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
353 NULL);
355 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
356 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
357 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
360 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
361 struct sk_buff *skb,
362 struct tipc_nl_compat_msg *msg)
364 struct nlattr *prop;
365 struct nlattr *bearer;
366 struct tipc_bearer_config *b;
368 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
370 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
371 if (!bearer)
372 return -EMSGSIZE;
374 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
375 return -EMSGSIZE;
377 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
378 return -EMSGSIZE;
380 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
381 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
382 if (!prop)
383 return -EMSGSIZE;
384 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
385 return -EMSGSIZE;
386 nla_nest_end(skb, prop);
388 nla_nest_end(skb, bearer);
390 return 0;
393 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
394 struct sk_buff *skb,
395 struct tipc_nl_compat_msg *msg)
397 char *name;
398 struct nlattr *bearer;
400 name = (char *)TLV_DATA(msg->req);
402 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
403 if (!bearer)
404 return -EMSGSIZE;
406 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
407 return -EMSGSIZE;
409 nla_nest_end(skb, bearer);
411 return 0;
414 static inline u32 perc(u32 count, u32 total)
416 return (count * 100 + (total / 2)) / total;
419 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
420 struct nlattr *prop[], struct nlattr *stats[])
422 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
423 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
425 tipc_tlv_sprintf(msg->rep,
426 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
427 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
428 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
429 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
430 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
431 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
433 tipc_tlv_sprintf(msg->rep,
434 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
435 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
436 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
437 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
438 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
439 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
441 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
442 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
443 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
444 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
446 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
447 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
448 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
449 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
451 tipc_tlv_sprintf(msg->rep,
452 " Congestion link:%u Send queue max:%u avg:%u",
453 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
454 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
455 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
458 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
459 struct nlattr **attrs)
461 char *name;
462 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
463 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
464 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
466 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
468 nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
469 NULL);
471 nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
472 NULL);
474 name = (char *)TLV_DATA(msg->req);
475 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
476 return 0;
478 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
479 nla_data(link[TIPC_NLA_LINK_NAME]));
481 if (link[TIPC_NLA_LINK_BROADCAST]) {
482 __fill_bc_link_stat(msg, prop, stats);
483 return 0;
486 if (link[TIPC_NLA_LINK_ACTIVE])
487 tipc_tlv_sprintf(msg->rep, " ACTIVE");
488 else if (link[TIPC_NLA_LINK_UP])
489 tipc_tlv_sprintf(msg->rep, " STANDBY");
490 else
491 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
493 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
494 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
495 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
497 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
498 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
499 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
501 tipc_tlv_sprintf(msg->rep,
502 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
503 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
504 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
505 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
506 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
507 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
508 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
510 tipc_tlv_sprintf(msg->rep,
511 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
512 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
513 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
514 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
515 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
516 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
517 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
519 tipc_tlv_sprintf(msg->rep,
520 " TX profile sample:%u packets average:%u octets\n",
521 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
522 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
523 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
525 tipc_tlv_sprintf(msg->rep,
526 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
527 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
528 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
529 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
530 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
531 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
532 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
533 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
534 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
536 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
537 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
538 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
539 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
540 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
541 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
542 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
544 tipc_tlv_sprintf(msg->rep,
545 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
546 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
547 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
548 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
549 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
550 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
552 tipc_tlv_sprintf(msg->rep,
553 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
554 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
555 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
556 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
557 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
558 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
560 tipc_tlv_sprintf(msg->rep,
561 " Congestion link:%u Send queue max:%u avg:%u",
562 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
563 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
564 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
566 return 0;
569 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
570 struct nlattr **attrs)
572 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
573 struct tipc_link_info link_info;
575 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
577 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
578 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
579 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
580 TIPC_MAX_LINK_NAME);
582 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
583 &link_info, sizeof(link_info));
586 static int __tipc_add_link_prop(struct sk_buff *skb,
587 struct tipc_nl_compat_msg *msg,
588 struct tipc_link_config *lc)
590 switch (msg->cmd) {
591 case TIPC_CMD_SET_LINK_PRI:
592 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
593 case TIPC_CMD_SET_LINK_TOL:
594 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
595 case TIPC_CMD_SET_LINK_WINDOW:
596 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
599 return -EINVAL;
602 static int tipc_nl_compat_media_set(struct sk_buff *skb,
603 struct tipc_nl_compat_msg *msg)
605 struct nlattr *prop;
606 struct nlattr *media;
607 struct tipc_link_config *lc;
609 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
611 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
612 if (!media)
613 return -EMSGSIZE;
615 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
616 return -EMSGSIZE;
618 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
619 if (!prop)
620 return -EMSGSIZE;
622 __tipc_add_link_prop(skb, msg, lc);
623 nla_nest_end(skb, prop);
624 nla_nest_end(skb, media);
626 return 0;
629 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
630 struct tipc_nl_compat_msg *msg)
632 struct nlattr *prop;
633 struct nlattr *bearer;
634 struct tipc_link_config *lc;
636 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
638 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
639 if (!bearer)
640 return -EMSGSIZE;
642 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
643 return -EMSGSIZE;
645 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
646 if (!prop)
647 return -EMSGSIZE;
649 __tipc_add_link_prop(skb, msg, lc);
650 nla_nest_end(skb, prop);
651 nla_nest_end(skb, bearer);
653 return 0;
656 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
657 struct tipc_nl_compat_msg *msg)
659 struct nlattr *prop;
660 struct nlattr *link;
661 struct tipc_link_config *lc;
663 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
665 link = nla_nest_start(skb, TIPC_NLA_LINK);
666 if (!link)
667 return -EMSGSIZE;
669 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
670 return -EMSGSIZE;
672 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
673 if (!prop)
674 return -EMSGSIZE;
676 __tipc_add_link_prop(skb, msg, lc);
677 nla_nest_end(skb, prop);
678 nla_nest_end(skb, link);
680 return 0;
683 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
684 struct sk_buff *skb,
685 struct tipc_nl_compat_msg *msg)
687 struct tipc_link_config *lc;
688 struct tipc_bearer *bearer;
689 struct tipc_media *media;
691 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
693 media = tipc_media_find(lc->name);
694 if (media) {
695 cmd->doit = &tipc_nl_media_set;
696 return tipc_nl_compat_media_set(skb, msg);
699 bearer = tipc_bearer_find(msg->net, lc->name);
700 if (bearer) {
701 cmd->doit = &tipc_nl_bearer_set;
702 return tipc_nl_compat_bearer_set(skb, msg);
705 return __tipc_nl_compat_link_set(skb, msg);
708 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
709 struct sk_buff *skb,
710 struct tipc_nl_compat_msg *msg)
712 char *name;
713 struct nlattr *link;
715 name = (char *)TLV_DATA(msg->req);
717 link = nla_nest_start(skb, TIPC_NLA_LINK);
718 if (!link)
719 return -EMSGSIZE;
721 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
722 return -EMSGSIZE;
724 nla_nest_end(skb, link);
726 return 0;
729 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
731 int i;
732 u32 depth;
733 struct tipc_name_table_query *ntq;
734 static const char * const header[] = {
735 "Type ",
736 "Lower Upper ",
737 "Port Identity ",
738 "Publication Scope"
741 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
743 depth = ntohl(ntq->depth);
745 if (depth > 4)
746 depth = 4;
747 for (i = 0; i < depth; i++)
748 tipc_tlv_sprintf(msg->rep, header[i]);
749 tipc_tlv_sprintf(msg->rep, "\n");
751 return 0;
754 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
755 struct nlattr **attrs)
757 char port_str[27];
758 struct tipc_name_table_query *ntq;
759 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
760 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
761 u32 node, depth, type, lowbound, upbound;
762 static const char * const scope_str[] = {"", " zone", " cluster",
763 " node"};
765 nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
766 attrs[TIPC_NLA_NAME_TABLE], NULL);
768 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
769 NULL);
771 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
773 depth = ntohl(ntq->depth);
774 type = ntohl(ntq->type);
775 lowbound = ntohl(ntq->lowbound);
776 upbound = ntohl(ntq->upbound);
778 if (!(depth & TIPC_NTQ_ALLTYPES) &&
779 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
780 return 0;
781 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
782 return 0;
783 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
784 return 0;
786 tipc_tlv_sprintf(msg->rep, "%-10u ",
787 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
789 if (depth == 1)
790 goto out;
792 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
793 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
794 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
796 if (depth == 2)
797 goto out;
799 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
800 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
801 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
802 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
804 if (depth == 3)
805 goto out;
807 tipc_tlv_sprintf(msg->rep, "%-10u %s",
808 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
809 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
810 out:
811 tipc_tlv_sprintf(msg->rep, "\n");
813 return 0;
816 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
817 struct nlattr **attrs)
819 u32 type, lower, upper;
820 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
822 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL], NULL);
824 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
825 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
826 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
828 if (lower == upper)
829 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
830 else
831 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
833 return 0;
836 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
838 int err;
839 void *hdr;
840 struct nlattr *nest;
841 struct sk_buff *args;
842 struct tipc_nl_compat_cmd_dump dump;
844 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
845 if (!args)
846 return -ENOMEM;
848 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
849 TIPC_NL_PUBL_GET);
851 nest = nla_nest_start(args, TIPC_NLA_SOCK);
852 if (!nest) {
853 kfree_skb(args);
854 return -EMSGSIZE;
857 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
858 kfree_skb(args);
859 return -EMSGSIZE;
862 nla_nest_end(args, nest);
863 genlmsg_end(args, hdr);
865 dump.dumpit = tipc_nl_publ_dump;
866 dump.format = __tipc_nl_compat_publ_dump;
868 err = __tipc_nl_compat_dumpit(&dump, msg, args);
870 kfree_skb(args);
872 return err;
875 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
876 struct nlattr **attrs)
878 int err;
879 u32 sock_ref;
880 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
882 nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK], NULL);
884 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
885 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
887 if (sock[TIPC_NLA_SOCK_CON]) {
888 u32 node;
889 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
891 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
892 NULL);
894 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
895 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
896 tipc_zone(node),
897 tipc_cluster(node),
898 tipc_node(node),
899 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
901 if (con[TIPC_NLA_CON_FLAG])
902 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
903 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
904 nla_get_u32(con[TIPC_NLA_CON_INST]));
905 else
906 tipc_tlv_sprintf(msg->rep, "\n");
907 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
908 tipc_tlv_sprintf(msg->rep, " bound to");
910 err = tipc_nl_compat_publ_dump(msg, sock_ref);
911 if (err)
912 return err;
914 tipc_tlv_sprintf(msg->rep, "\n");
916 return 0;
919 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
920 struct nlattr **attrs)
922 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
924 nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
925 NULL);
927 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
928 nla_data(media[TIPC_NLA_MEDIA_NAME]),
929 nla_len(media[TIPC_NLA_MEDIA_NAME]));
932 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
933 struct nlattr **attrs)
935 struct tipc_node_info node_info;
936 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
938 nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE], NULL);
940 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
941 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
943 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
944 sizeof(node_info));
947 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
948 struct sk_buff *skb,
949 struct tipc_nl_compat_msg *msg)
951 u32 val;
952 struct nlattr *net;
954 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
956 net = nla_nest_start(skb, TIPC_NLA_NET);
957 if (!net)
958 return -EMSGSIZE;
960 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
961 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
962 return -EMSGSIZE;
963 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
964 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
965 return -EMSGSIZE;
967 nla_nest_end(skb, net);
969 return 0;
972 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
973 struct nlattr **attrs)
975 __be32 id;
976 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
978 nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET], NULL);
979 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
981 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
984 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
986 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
987 if (!msg->rep)
988 return -ENOMEM;
990 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
991 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
993 return 0;
996 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
998 struct tipc_nl_compat_cmd_dump dump;
999 struct tipc_nl_compat_cmd_doit doit;
1001 memset(&dump, 0, sizeof(dump));
1002 memset(&doit, 0, sizeof(doit));
1004 switch (msg->cmd) {
1005 case TIPC_CMD_NOOP:
1006 msg->rep = tipc_tlv_alloc(0);
1007 if (!msg->rep)
1008 return -ENOMEM;
1009 return 0;
1010 case TIPC_CMD_GET_BEARER_NAMES:
1011 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1012 dump.dumpit = tipc_nl_bearer_dump;
1013 dump.format = tipc_nl_compat_bearer_dump;
1014 return tipc_nl_compat_dumpit(&dump, msg);
1015 case TIPC_CMD_ENABLE_BEARER:
1016 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1017 doit.doit = tipc_nl_bearer_enable;
1018 doit.transcode = tipc_nl_compat_bearer_enable;
1019 return tipc_nl_compat_doit(&doit, msg);
1020 case TIPC_CMD_DISABLE_BEARER:
1021 msg->req_type = TIPC_TLV_BEARER_NAME;
1022 doit.doit = tipc_nl_bearer_disable;
1023 doit.transcode = tipc_nl_compat_bearer_disable;
1024 return tipc_nl_compat_doit(&doit, msg);
1025 case TIPC_CMD_SHOW_LINK_STATS:
1026 msg->req_type = TIPC_TLV_LINK_NAME;
1027 msg->rep_size = ULTRA_STRING_MAX_LEN;
1028 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1029 dump.dumpit = tipc_nl_link_dump;
1030 dump.format = tipc_nl_compat_link_stat_dump;
1031 return tipc_nl_compat_dumpit(&dump, msg);
1032 case TIPC_CMD_GET_LINKS:
1033 msg->req_type = TIPC_TLV_NET_ADDR;
1034 msg->rep_size = ULTRA_STRING_MAX_LEN;
1035 dump.dumpit = tipc_nl_link_dump;
1036 dump.format = tipc_nl_compat_link_dump;
1037 return tipc_nl_compat_dumpit(&dump, msg);
1038 case TIPC_CMD_SET_LINK_TOL:
1039 case TIPC_CMD_SET_LINK_PRI:
1040 case TIPC_CMD_SET_LINK_WINDOW:
1041 msg->req_type = TIPC_TLV_LINK_CONFIG;
1042 doit.doit = tipc_nl_link_set;
1043 doit.transcode = tipc_nl_compat_link_set;
1044 return tipc_nl_compat_doit(&doit, msg);
1045 case TIPC_CMD_RESET_LINK_STATS:
1046 msg->req_type = TIPC_TLV_LINK_NAME;
1047 doit.doit = tipc_nl_link_reset_stats;
1048 doit.transcode = tipc_nl_compat_link_reset_stats;
1049 return tipc_nl_compat_doit(&doit, msg);
1050 case TIPC_CMD_SHOW_NAME_TABLE:
1051 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1052 msg->rep_size = ULTRA_STRING_MAX_LEN;
1053 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1054 dump.header = tipc_nl_compat_name_table_dump_header;
1055 dump.dumpit = tipc_nl_name_table_dump;
1056 dump.format = tipc_nl_compat_name_table_dump;
1057 return tipc_nl_compat_dumpit(&dump, msg);
1058 case TIPC_CMD_SHOW_PORTS:
1059 msg->rep_size = ULTRA_STRING_MAX_LEN;
1060 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1061 dump.dumpit = tipc_nl_sk_dump;
1062 dump.format = tipc_nl_compat_sk_dump;
1063 return tipc_nl_compat_dumpit(&dump, msg);
1064 case TIPC_CMD_GET_MEDIA_NAMES:
1065 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1066 dump.dumpit = tipc_nl_media_dump;
1067 dump.format = tipc_nl_compat_media_dump;
1068 return tipc_nl_compat_dumpit(&dump, msg);
1069 case TIPC_CMD_GET_NODES:
1070 msg->rep_size = ULTRA_STRING_MAX_LEN;
1071 dump.dumpit = tipc_nl_node_dump;
1072 dump.format = tipc_nl_compat_node_dump;
1073 return tipc_nl_compat_dumpit(&dump, msg);
1074 case TIPC_CMD_SET_NODE_ADDR:
1075 msg->req_type = TIPC_TLV_NET_ADDR;
1076 doit.doit = tipc_nl_net_set;
1077 doit.transcode = tipc_nl_compat_net_set;
1078 return tipc_nl_compat_doit(&doit, msg);
1079 case TIPC_CMD_SET_NETID:
1080 msg->req_type = TIPC_TLV_UNSIGNED;
1081 doit.doit = tipc_nl_net_set;
1082 doit.transcode = tipc_nl_compat_net_set;
1083 return tipc_nl_compat_doit(&doit, msg);
1084 case TIPC_CMD_GET_NETID:
1085 msg->rep_size = sizeof(u32);
1086 dump.dumpit = tipc_nl_net_dump;
1087 dump.format = tipc_nl_compat_net_dump;
1088 return tipc_nl_compat_dumpit(&dump, msg);
1089 case TIPC_CMD_SHOW_STATS:
1090 return tipc_cmd_show_stats_compat(msg);
1093 return -EOPNOTSUPP;
1096 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1098 int err;
1099 int len;
1100 struct tipc_nl_compat_msg msg;
1101 struct nlmsghdr *req_nlh;
1102 struct nlmsghdr *rep_nlh;
1103 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1105 memset(&msg, 0, sizeof(msg));
1107 req_nlh = (struct nlmsghdr *)skb->data;
1108 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1109 msg.cmd = req_userhdr->cmd;
1110 msg.dst_sk = info->dst_sk;
1111 msg.net = genl_info_net(info);
1113 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1114 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1115 err = -EACCES;
1116 goto send;
1119 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1120 if (len && !TLV_OK(msg.req, len)) {
1121 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1122 err = -EOPNOTSUPP;
1123 goto send;
1126 err = tipc_nl_compat_handle(&msg);
1127 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1128 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1129 else if (err == -EINVAL)
1130 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1131 send:
1132 if (!msg.rep)
1133 return err;
1135 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1136 skb_push(msg.rep, len);
1137 rep_nlh = nlmsg_hdr(msg.rep);
1138 memcpy(rep_nlh, info->nlhdr, len);
1139 rep_nlh->nlmsg_len = msg.rep->len;
1140 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1142 return err;
1145 static struct genl_family tipc_genl_compat_family = {
1146 .id = GENL_ID_GENERATE,
1147 .name = TIPC_GENL_NAME,
1148 .version = TIPC_GENL_VERSION,
1149 .hdrsize = TIPC_GENL_HDRLEN,
1150 .maxattr = 0,
1151 .netnsok = true,
1154 static struct genl_ops tipc_genl_compat_ops[] = {
1156 .cmd = TIPC_GENL_CMD,
1157 .doit = tipc_nl_compat_recv,
1161 int tipc_netlink_compat_start(void)
1163 int res;
1165 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1166 tipc_genl_compat_ops);
1167 if (res) {
1168 pr_err("Failed to register legacy compat interface\n");
1169 return res;
1172 return 0;
1175 void tipc_netlink_compat_stop(void)
1177 genl_unregister_family(&tipc_genl_compat_family);