2 * Copyright (c) 2014, Ericsson AB
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.
37 #include "name_table.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 {
65 struct tipc_nl_compat_cmd_dump {
66 int (*header)(struct tipc_nl_compat_msg *);
67 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
68 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
71 struct tipc_nl_compat_cmd_doit {
72 int (*doit)(struct sk_buff *skb, struct genl_info *info);
73 int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
74 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
77 static int tipc_skb_tailroom(struct sk_buff *skb)
82 tailroom = skb_tailroom(skb);
83 limit = TIPC_SKB_MAX - skb->len;
91 static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
93 return TLV_GET_LEN(tlv) - TLV_SPACE(0);
96 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
98 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
100 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
103 skb_put(skb, TLV_SPACE(len));
104 tlv->tlv_type = htons(type);
105 tlv->tlv_len = htons(TLV_LENGTH(len));
107 memcpy(TLV_DATA(tlv), data, len);
112 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
114 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
117 TLV_SET_TYPE(tlv, type);
118 skb_put(skb, sizeof(struct tlv_desc));
121 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
127 struct tlv_desc *tlv;
130 rem = tipc_skb_tailroom(skb);
132 tlv = (struct tlv_desc *)skb->data;
133 len = TLV_GET_LEN(tlv);
134 buf = TLV_DATA(tlv) + len;
137 n = vscnprintf(buf, rem, fmt, args);
140 TLV_SET_LEN(tlv, n + len);
146 static struct sk_buff *tipc_tlv_alloc(int size)
151 size = TLV_SPACE(size);
152 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
154 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
158 skb_reserve(buf, hdr_len);
163 static struct sk_buff *tipc_get_err_tlv(char *str)
165 int str_len = strlen(str) + 1;
168 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
170 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
175 static inline bool string_is_valid(char *s, int len)
177 return memchr(s, '\0', len) ? true : false;
180 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
181 struct tipc_nl_compat_msg *msg,
187 struct nlmsghdr *nlmsg;
188 struct netlink_callback cb;
190 memset(&cb, 0, sizeof(cb));
191 cb.nlh = (struct nlmsghdr *)arg->data;
194 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
198 buf->sk = msg->dst_sk;
199 if (__tipc_dump_start(&cb, msg->net)) {
207 len = (*cmd->dumpit)(buf, &cb);
209 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
210 struct nlattr **attrs;
212 err = tipc_nlmsg_parse(nlmsg, &attrs);
216 err = (*cmd->format)(msg, attrs);
220 if (tipc_skb_tailroom(msg->rep) <= 1) {
226 skb_reset_tail_pointer(buf);
237 if (err == -EMSGSIZE) {
238 /* The legacy API only considered messages filling
239 * "ULTRA_STRING_MAX_LEN" to be truncated.
241 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
242 char *tail = skb_tail_pointer(msg->rep);
245 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
255 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
256 struct tipc_nl_compat_msg *msg)
258 struct nlmsghdr *nlh;
262 if (msg->req_type && (!msg->req_size ||
263 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
266 msg->rep = tipc_tlv_alloc(msg->rep_size);
271 tipc_tlv_init(msg->rep, msg->rep_type);
274 err = (*cmd->header)(msg);
282 arg = nlmsg_new(0, GFP_KERNEL);
289 nlh = nlmsg_put(arg, 0, 0, tipc_genl_family.id, 0, NLM_F_MULTI);
298 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
308 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
309 struct tipc_nl_compat_msg *msg)
312 struct sk_buff *doit_buf;
313 struct sk_buff *trans_buf;
314 struct nlattr **attrbuf;
315 struct genl_info info;
317 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
321 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
322 sizeof(struct nlattr *),
329 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
335 memset(&info, 0, sizeof(info));
336 info.attrs = attrbuf;
339 err = (*cmd->transcode)(cmd, trans_buf, msg);
343 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
344 (const struct nlattr *)trans_buf->data,
345 trans_buf->len, NULL, NULL);
349 doit_buf->sk = msg->dst_sk;
351 err = (*cmd->doit)(doit_buf, &info);
359 kfree_skb(trans_buf);
364 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
365 struct tipc_nl_compat_msg *msg)
369 if (msg->req_type && (!msg->req_size ||
370 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
373 err = __tipc_nl_compat_doit(cmd, msg);
377 /* The legacy API considered an empty message a success message */
378 msg->rep = tipc_tlv_alloc(0);
385 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
386 struct nlattr **attrs)
388 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
391 if (!attrs[TIPC_NLA_BEARER])
394 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
395 attrs[TIPC_NLA_BEARER], NULL, NULL);
399 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
400 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
401 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
404 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
406 struct tipc_nl_compat_msg *msg)
409 struct nlattr *bearer;
410 struct tipc_bearer_config *b;
413 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
415 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
419 len = TLV_GET_DATA_LEN(msg->req);
420 len -= offsetof(struct tipc_bearer_config, name);
424 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
425 if (!string_is_valid(b->name, len))
428 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
431 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
434 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
435 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
438 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
440 nla_nest_end(skb, prop);
442 nla_nest_end(skb, bearer);
447 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
449 struct tipc_nl_compat_msg *msg)
452 struct nlattr *bearer;
455 name = (char *)TLV_DATA(msg->req);
457 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
461 len = TLV_GET_DATA_LEN(msg->req);
465 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
466 if (!string_is_valid(name, len))
469 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
472 nla_nest_end(skb, bearer);
477 static inline u32 perc(u32 count, u32 total)
479 return (count * 100 + (total / 2)) / total;
482 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
483 struct nlattr *prop[], struct nlattr *stats[])
485 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
486 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
488 tipc_tlv_sprintf(msg->rep,
489 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
490 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
491 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
492 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
493 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
494 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
496 tipc_tlv_sprintf(msg->rep,
497 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
498 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
499 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
500 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
501 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
502 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
504 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
505 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
506 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
507 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
509 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
510 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
511 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
512 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
514 tipc_tlv_sprintf(msg->rep,
515 " Congestion link:%u Send queue max:%u avg:%u",
516 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
517 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
518 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
521 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
522 struct nlattr **attrs)
525 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
526 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
527 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
531 if (!attrs[TIPC_NLA_LINK])
534 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
539 if (!link[TIPC_NLA_LINK_PROP])
542 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
543 link[TIPC_NLA_LINK_PROP], NULL, NULL);
547 if (!link[TIPC_NLA_LINK_STATS])
550 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
551 link[TIPC_NLA_LINK_STATS], NULL, NULL);
555 name = (char *)TLV_DATA(msg->req);
557 len = TLV_GET_DATA_LEN(msg->req);
561 len = min_t(int, len, TIPC_MAX_LINK_NAME);
562 if (!string_is_valid(name, len))
565 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
568 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
569 nla_data(link[TIPC_NLA_LINK_NAME]));
571 if (link[TIPC_NLA_LINK_BROADCAST]) {
572 __fill_bc_link_stat(msg, prop, stats);
576 if (link[TIPC_NLA_LINK_ACTIVE])
577 tipc_tlv_sprintf(msg->rep, " ACTIVE");
578 else if (link[TIPC_NLA_LINK_UP])
579 tipc_tlv_sprintf(msg->rep, " STANDBY");
581 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
583 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
584 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
585 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
587 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
588 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
589 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
591 tipc_tlv_sprintf(msg->rep,
592 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
593 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
594 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
595 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
596 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
597 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
598 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
600 tipc_tlv_sprintf(msg->rep,
601 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
602 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
603 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
604 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
605 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
606 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
607 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
609 tipc_tlv_sprintf(msg->rep,
610 " TX profile sample:%u packets average:%u octets\n",
611 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
612 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
613 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
615 tipc_tlv_sprintf(msg->rep,
616 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
617 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
618 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
619 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
620 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
621 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
622 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
623 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
624 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
626 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
627 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
628 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
629 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
630 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
631 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
632 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
634 tipc_tlv_sprintf(msg->rep,
635 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
636 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
637 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
638 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
639 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
640 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
642 tipc_tlv_sprintf(msg->rep,
643 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
644 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
645 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
646 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
647 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
648 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
650 tipc_tlv_sprintf(msg->rep,
651 " Congestion link:%u Send queue max:%u avg:%u",
652 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
653 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
654 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
659 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
660 struct nlattr **attrs)
662 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
663 struct tipc_link_info link_info;
666 if (!attrs[TIPC_NLA_LINK])
669 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
674 link_info.dest = htonl(nla_get_flag(link[TIPC_NLA_LINK_DEST]));
675 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
676 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
679 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
680 &link_info, sizeof(link_info));
683 static int __tipc_add_link_prop(struct sk_buff *skb,
684 struct tipc_nl_compat_msg *msg,
685 struct tipc_link_config *lc)
688 case TIPC_CMD_SET_LINK_PRI:
689 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
690 case TIPC_CMD_SET_LINK_TOL:
691 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
692 case TIPC_CMD_SET_LINK_WINDOW:
693 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
699 static int tipc_nl_compat_media_set(struct sk_buff *skb,
700 struct tipc_nl_compat_msg *msg)
703 struct nlattr *media;
704 struct tipc_link_config *lc;
707 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
709 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
713 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
714 if (!string_is_valid(lc->name, len))
717 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
720 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
724 __tipc_add_link_prop(skb, msg, lc);
725 nla_nest_end(skb, prop);
726 nla_nest_end(skb, media);
731 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
732 struct tipc_nl_compat_msg *msg)
735 struct nlattr *bearer;
736 struct tipc_link_config *lc;
739 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
741 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
745 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
746 if (!string_is_valid(lc->name, len))
749 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
752 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
756 __tipc_add_link_prop(skb, msg, lc);
757 nla_nest_end(skb, prop);
758 nla_nest_end(skb, bearer);
763 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
764 struct tipc_nl_compat_msg *msg)
768 struct tipc_link_config *lc;
770 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
772 link = nla_nest_start(skb, TIPC_NLA_LINK);
776 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
779 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
783 __tipc_add_link_prop(skb, msg, lc);
784 nla_nest_end(skb, prop);
785 nla_nest_end(skb, link);
790 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
792 struct tipc_nl_compat_msg *msg)
794 struct tipc_link_config *lc;
795 struct tipc_bearer *bearer;
796 struct tipc_media *media;
799 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
801 len = TLV_GET_DATA_LEN(msg->req);
802 len -= offsetof(struct tipc_link_config, name);
806 len = min_t(int, len, TIPC_MAX_LINK_NAME);
807 if (!string_is_valid(lc->name, len))
810 media = tipc_media_find(lc->name);
812 cmd->doit = &__tipc_nl_media_set;
813 return tipc_nl_compat_media_set(skb, msg);
816 bearer = tipc_bearer_find(msg->net, lc->name);
818 cmd->doit = &__tipc_nl_bearer_set;
819 return tipc_nl_compat_bearer_set(skb, msg);
822 return __tipc_nl_compat_link_set(skb, msg);
825 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
827 struct tipc_nl_compat_msg *msg)
833 name = (char *)TLV_DATA(msg->req);
835 link = nla_nest_start(skb, TIPC_NLA_LINK);
839 len = TLV_GET_DATA_LEN(msg->req);
843 len = min_t(int, len, TIPC_MAX_LINK_NAME);
844 if (!string_is_valid(name, len))
847 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
850 nla_nest_end(skb, link);
855 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
859 struct tipc_name_table_query *ntq;
860 static const char * const header[] = {
867 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
868 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
871 depth = ntohl(ntq->depth);
875 for (i = 0; i < depth; i++)
876 tipc_tlv_sprintf(msg->rep, header[i]);
877 tipc_tlv_sprintf(msg->rep, "\n");
882 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
883 struct nlattr **attrs)
886 struct tipc_name_table_query *ntq;
887 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
888 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
889 u32 node, depth, type, lowbound, upbound;
890 static const char * const scope_str[] = {"", " zone", " cluster",
894 if (!attrs[TIPC_NLA_NAME_TABLE])
897 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
898 attrs[TIPC_NLA_NAME_TABLE], NULL, NULL);
902 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
905 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
906 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL, NULL);
910 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
912 depth = ntohl(ntq->depth);
913 type = ntohl(ntq->type);
914 lowbound = ntohl(ntq->lowbound);
915 upbound = ntohl(ntq->upbound);
917 if (!(depth & TIPC_NTQ_ALLTYPES) &&
918 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
920 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
922 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
925 tipc_tlv_sprintf(msg->rep, "%-10u ",
926 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
931 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
932 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
933 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
938 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
939 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
940 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
941 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
946 tipc_tlv_sprintf(msg->rep, "%-10u %s",
947 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
948 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
950 tipc_tlv_sprintf(msg->rep, "\n");
955 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
956 struct nlattr **attrs)
958 u32 type, lower, upper;
959 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
962 if (!attrs[TIPC_NLA_PUBL])
965 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
970 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
971 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
972 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
975 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
977 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
982 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
987 struct sk_buff *args;
988 struct tipc_nl_compat_cmd_dump dump;
990 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
994 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
1001 nest = nla_nest_start(args, TIPC_NLA_SOCK);
1007 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
1012 nla_nest_end(args, nest);
1013 genlmsg_end(args, hdr);
1015 dump.dumpit = tipc_nl_publ_dump;
1016 dump.format = __tipc_nl_compat_publ_dump;
1018 err = __tipc_nl_compat_dumpit(&dump, msg, args);
1025 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1026 struct nlattr **attrs)
1030 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1032 if (!attrs[TIPC_NLA_SOCK])
1035 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
1040 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1041 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1043 if (sock[TIPC_NLA_SOCK_CON]) {
1045 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1047 err = nla_parse_nested(con, TIPC_NLA_CON_MAX,
1048 sock[TIPC_NLA_SOCK_CON], NULL, NULL);
1053 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1054 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1058 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1060 if (con[TIPC_NLA_CON_FLAG])
1061 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1062 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1063 nla_get_u32(con[TIPC_NLA_CON_INST]));
1065 tipc_tlv_sprintf(msg->rep, "\n");
1066 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1067 tipc_tlv_sprintf(msg->rep, " bound to");
1069 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1073 tipc_tlv_sprintf(msg->rep, "\n");
1078 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1079 struct nlattr **attrs)
1081 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1084 if (!attrs[TIPC_NLA_MEDIA])
1087 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX,
1088 attrs[TIPC_NLA_MEDIA], NULL, NULL);
1092 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1093 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1094 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1097 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1098 struct nlattr **attrs)
1100 struct tipc_node_info node_info;
1101 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1104 if (!attrs[TIPC_NLA_NODE])
1107 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1112 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1113 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1115 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1119 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1120 struct sk_buff *skb,
1121 struct tipc_nl_compat_msg *msg)
1126 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1128 net = nla_nest_start(skb, TIPC_NLA_NET);
1132 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1133 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1135 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1136 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1139 nla_nest_end(skb, net);
1144 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1145 struct nlattr **attrs)
1148 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1151 if (!attrs[TIPC_NLA_NET])
1154 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1159 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1161 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1164 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1166 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1170 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1171 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1176 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1178 struct tipc_nl_compat_cmd_dump dump;
1179 struct tipc_nl_compat_cmd_doit doit;
1181 memset(&dump, 0, sizeof(dump));
1182 memset(&doit, 0, sizeof(doit));
1186 msg->rep = tipc_tlv_alloc(0);
1190 case TIPC_CMD_GET_BEARER_NAMES:
1191 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1192 dump.dumpit = tipc_nl_bearer_dump;
1193 dump.format = tipc_nl_compat_bearer_dump;
1194 return tipc_nl_compat_dumpit(&dump, msg);
1195 case TIPC_CMD_ENABLE_BEARER:
1196 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1197 doit.doit = __tipc_nl_bearer_enable;
1198 doit.transcode = tipc_nl_compat_bearer_enable;
1199 return tipc_nl_compat_doit(&doit, msg);
1200 case TIPC_CMD_DISABLE_BEARER:
1201 msg->req_type = TIPC_TLV_BEARER_NAME;
1202 doit.doit = __tipc_nl_bearer_disable;
1203 doit.transcode = tipc_nl_compat_bearer_disable;
1204 return tipc_nl_compat_doit(&doit, msg);
1205 case TIPC_CMD_SHOW_LINK_STATS:
1206 msg->req_type = TIPC_TLV_LINK_NAME;
1207 msg->rep_size = ULTRA_STRING_MAX_LEN;
1208 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1209 dump.dumpit = tipc_nl_node_dump_link;
1210 dump.format = tipc_nl_compat_link_stat_dump;
1211 return tipc_nl_compat_dumpit(&dump, msg);
1212 case TIPC_CMD_GET_LINKS:
1213 msg->req_type = TIPC_TLV_NET_ADDR;
1214 msg->rep_size = ULTRA_STRING_MAX_LEN;
1215 dump.dumpit = tipc_nl_node_dump_link;
1216 dump.format = tipc_nl_compat_link_dump;
1217 return tipc_nl_compat_dumpit(&dump, msg);
1218 case TIPC_CMD_SET_LINK_TOL:
1219 case TIPC_CMD_SET_LINK_PRI:
1220 case TIPC_CMD_SET_LINK_WINDOW:
1221 msg->req_type = TIPC_TLV_LINK_CONFIG;
1222 doit.doit = tipc_nl_node_set_link;
1223 doit.transcode = tipc_nl_compat_link_set;
1224 return tipc_nl_compat_doit(&doit, msg);
1225 case TIPC_CMD_RESET_LINK_STATS:
1226 msg->req_type = TIPC_TLV_LINK_NAME;
1227 doit.doit = tipc_nl_node_reset_link_stats;
1228 doit.transcode = tipc_nl_compat_link_reset_stats;
1229 return tipc_nl_compat_doit(&doit, msg);
1230 case TIPC_CMD_SHOW_NAME_TABLE:
1231 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1232 msg->rep_size = ULTRA_STRING_MAX_LEN;
1233 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1234 dump.header = tipc_nl_compat_name_table_dump_header;
1235 dump.dumpit = tipc_nl_name_table_dump;
1236 dump.format = tipc_nl_compat_name_table_dump;
1237 return tipc_nl_compat_dumpit(&dump, msg);
1238 case TIPC_CMD_SHOW_PORTS:
1239 msg->rep_size = ULTRA_STRING_MAX_LEN;
1240 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1241 dump.dumpit = tipc_nl_sk_dump;
1242 dump.format = tipc_nl_compat_sk_dump;
1243 return tipc_nl_compat_dumpit(&dump, msg);
1244 case TIPC_CMD_GET_MEDIA_NAMES:
1245 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1246 dump.dumpit = tipc_nl_media_dump;
1247 dump.format = tipc_nl_compat_media_dump;
1248 return tipc_nl_compat_dumpit(&dump, msg);
1249 case TIPC_CMD_GET_NODES:
1250 msg->rep_size = ULTRA_STRING_MAX_LEN;
1251 dump.dumpit = tipc_nl_node_dump;
1252 dump.format = tipc_nl_compat_node_dump;
1253 return tipc_nl_compat_dumpit(&dump, msg);
1254 case TIPC_CMD_SET_NODE_ADDR:
1255 msg->req_type = TIPC_TLV_NET_ADDR;
1256 doit.doit = __tipc_nl_net_set;
1257 doit.transcode = tipc_nl_compat_net_set;
1258 return tipc_nl_compat_doit(&doit, msg);
1259 case TIPC_CMD_SET_NETID:
1260 msg->req_type = TIPC_TLV_UNSIGNED;
1261 doit.doit = __tipc_nl_net_set;
1262 doit.transcode = tipc_nl_compat_net_set;
1263 return tipc_nl_compat_doit(&doit, msg);
1264 case TIPC_CMD_GET_NETID:
1265 msg->rep_size = sizeof(u32);
1266 dump.dumpit = tipc_nl_net_dump;
1267 dump.format = tipc_nl_compat_net_dump;
1268 return tipc_nl_compat_dumpit(&dump, msg);
1269 case TIPC_CMD_SHOW_STATS:
1270 return tipc_cmd_show_stats_compat(msg);
1276 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1280 struct tipc_nl_compat_msg msg;
1281 struct nlmsghdr *req_nlh;
1282 struct nlmsghdr *rep_nlh;
1283 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1285 memset(&msg, 0, sizeof(msg));
1287 req_nlh = (struct nlmsghdr *)skb->data;
1288 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1289 msg.cmd = req_userhdr->cmd;
1290 msg.net = genl_info_net(info);
1291 msg.dst_sk = skb->sk;
1293 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1294 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1299 msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1300 if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1301 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1306 err = tipc_nl_compat_handle(&msg);
1307 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1308 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1309 else if (err == -EINVAL)
1310 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1315 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1316 skb_push(msg.rep, len);
1317 rep_nlh = nlmsg_hdr(msg.rep);
1318 memcpy(rep_nlh, info->nlhdr, len);
1319 rep_nlh->nlmsg_len = msg.rep->len;
1320 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1325 static const struct genl_ops tipc_genl_compat_ops[] = {
1327 .cmd = TIPC_GENL_CMD,
1328 .doit = tipc_nl_compat_recv,
1332 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1333 .name = TIPC_GENL_NAME,
1334 .version = TIPC_GENL_VERSION,
1335 .hdrsize = TIPC_GENL_HDRLEN,
1338 .module = THIS_MODULE,
1339 .ops = tipc_genl_compat_ops,
1340 .n_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1343 int __init tipc_netlink_compat_start(void)
1347 res = genl_register_family(&tipc_genl_compat_family);
1349 pr_err("Failed to register legacy compat interface\n");
1356 void tipc_netlink_compat_stop(void)
1358 genl_unregister_family(&tipc_genl_compat_family);