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/string_helpers.h>
43 #include <linux/tipc_config.h>
45 /* The legacy API had an artificial message length limit called
46 * ULTRA_STRING_MAX_LEN.
48 #define ULTRA_STRING_MAX_LEN 32768
50 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
52 #define REPLY_TRUNCATED "<truncated>\n"
54 struct tipc_nl_compat_msg {
66 struct tipc_nl_compat_cmd_dump {
67 int (*header)(struct tipc_nl_compat_msg *);
68 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
69 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
72 struct tipc_nl_compat_cmd_doit {
73 int (*doit)(struct sk_buff *skb, struct genl_info *info);
74 int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
75 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
78 static int tipc_skb_tailroom(struct sk_buff *skb)
83 tailroom = skb_tailroom(skb);
84 limit = TIPC_SKB_MAX - skb->len;
92 static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
94 return TLV_GET_LEN(tlv) - TLV_SPACE(0);
97 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
99 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
101 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
104 skb_put(skb, TLV_SPACE(len));
105 tlv->tlv_type = htons(type);
106 tlv->tlv_len = htons(TLV_LENGTH(len));
108 memcpy(TLV_DATA(tlv), data, len);
113 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
115 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
118 TLV_SET_TYPE(tlv, type);
119 skb_put(skb, sizeof(struct tlv_desc));
122 static __printf(2, 3) int tipc_tlv_sprintf(struct sk_buff *skb,
123 const char *fmt, ...)
129 struct tlv_desc *tlv;
132 rem = tipc_skb_tailroom(skb);
134 tlv = (struct tlv_desc *)skb->data;
135 len = TLV_GET_LEN(tlv);
136 buf = TLV_DATA(tlv) + len;
139 n = vscnprintf(buf, rem, fmt, args);
142 TLV_SET_LEN(tlv, n + len);
148 static struct sk_buff *tipc_tlv_alloc(int size)
153 size = TLV_SPACE(size);
154 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
156 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
160 skb_reserve(buf, hdr_len);
165 static struct sk_buff *tipc_get_err_tlv(char *str)
167 int str_len = strlen(str) + 1;
170 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
172 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
177 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
178 struct tipc_nl_compat_msg *msg,
181 struct genl_dumpit_info info;
185 struct nlmsghdr *nlmsg;
186 struct netlink_callback cb;
187 struct nlattr **attrbuf;
189 memset(&cb, 0, sizeof(cb));
190 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)) {
204 attrbuf = kcalloc(tipc_genl_family.maxattr + 1,
205 sizeof(struct nlattr *), GFP_KERNEL);
211 info.info.attrs = attrbuf;
213 if (nlmsg_len(cb.nlh) > 0) {
214 err = nlmsg_parse_deprecated(cb.nlh, GENL_HDRLEN, attrbuf,
215 tipc_genl_family.maxattr,
216 tipc_genl_family.policy, NULL);
223 len = (*cmd->dumpit)(buf, &cb);
225 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
226 err = nlmsg_parse_deprecated(nlmsg, GENL_HDRLEN,
228 tipc_genl_family.maxattr,
229 tipc_genl_family.policy,
234 err = (*cmd->format)(msg, attrbuf);
238 if (tipc_skb_tailroom(msg->rep) <= 1) {
244 skb_reset_tail_pointer(buf);
256 if (err == -EMSGSIZE) {
257 /* The legacy API only considered messages filling
258 * "ULTRA_STRING_MAX_LEN" to be truncated.
260 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
261 char *tail = skb_tail_pointer(msg->rep);
264 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
274 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
275 struct tipc_nl_compat_msg *msg)
277 struct nlmsghdr *nlh;
281 if (msg->req_type && (!msg->req_size ||
282 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
285 msg->rep = tipc_tlv_alloc(msg->rep_size);
290 tipc_tlv_init(msg->rep, msg->rep_type);
293 err = (*cmd->header)(msg);
301 arg = nlmsg_new(0, GFP_KERNEL);
308 nlh = nlmsg_put(arg, 0, 0, tipc_genl_family.id, 0, NLM_F_MULTI);
317 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
327 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
328 struct tipc_nl_compat_msg *msg)
331 struct sk_buff *doit_buf;
332 struct sk_buff *trans_buf;
333 struct nlattr **attrbuf;
334 struct genl_info info;
336 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
340 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
341 sizeof(struct nlattr *),
348 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
354 memset(&info, 0, sizeof(info));
355 info.attrs = attrbuf;
358 err = (*cmd->transcode)(cmd, trans_buf, msg);
362 err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
363 (const struct nlattr *)trans_buf->data,
364 trans_buf->len, NULL, NULL);
368 doit_buf->sk = msg->dst_sk;
370 err = (*cmd->doit)(doit_buf, &info);
378 kfree_skb(trans_buf);
383 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
384 struct tipc_nl_compat_msg *msg)
388 if (msg->req_type && (!msg->req_size ||
389 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
392 err = __tipc_nl_compat_doit(cmd, msg);
396 /* The legacy API considered an empty message a success message */
397 msg->rep = tipc_tlv_alloc(0);
404 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
405 struct nlattr **attrs)
407 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
410 if (!attrs[TIPC_NLA_BEARER])
413 err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
414 attrs[TIPC_NLA_BEARER], NULL, NULL);
418 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
419 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
420 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
423 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
425 struct tipc_nl_compat_msg *msg)
428 struct nlattr *bearer;
429 struct tipc_bearer_config *b;
432 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
434 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
438 len = TLV_GET_DATA_LEN(msg->req);
439 len -= offsetof(struct tipc_bearer_config, name);
443 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
444 if (!string_is_terminated(b->name, len))
447 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
450 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
453 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
454 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
457 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
459 nla_nest_end(skb, prop);
461 nla_nest_end(skb, bearer);
466 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
468 struct tipc_nl_compat_msg *msg)
471 struct nlattr *bearer;
474 name = (char *)TLV_DATA(msg->req);
476 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
480 len = TLV_GET_DATA_LEN(msg->req);
484 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
485 if (!string_is_terminated(name, len))
488 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
491 nla_nest_end(skb, bearer);
496 static inline u32 perc(u32 count, u32 total)
498 return (count * 100 + (total / 2)) / total;
501 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
502 struct nlattr *prop[], struct nlattr *stats[])
504 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
505 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
507 tipc_tlv_sprintf(msg->rep,
508 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
509 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
510 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
511 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
512 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
513 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
515 tipc_tlv_sprintf(msg->rep,
516 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
517 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
518 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
519 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
520 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
521 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
523 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
524 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
525 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
526 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
528 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
529 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
530 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
531 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
533 tipc_tlv_sprintf(msg->rep,
534 " Congestion link:%u Send queue max:%u avg:%u",
535 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
536 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
537 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
540 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
541 struct nlattr **attrs)
544 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
545 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
546 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
550 if (!attrs[TIPC_NLA_LINK])
553 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
554 attrs[TIPC_NLA_LINK], NULL, NULL);
558 if (!link[TIPC_NLA_LINK_PROP])
561 err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
562 link[TIPC_NLA_LINK_PROP], NULL,
567 if (!link[TIPC_NLA_LINK_STATS])
570 err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
571 link[TIPC_NLA_LINK_STATS], NULL,
576 name = (char *)TLV_DATA(msg->req);
578 len = TLV_GET_DATA_LEN(msg->req);
582 len = min_t(int, len, TIPC_MAX_LINK_NAME);
583 if (!string_is_terminated(name, len))
586 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
589 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
590 (char *)nla_data(link[TIPC_NLA_LINK_NAME]));
592 if (link[TIPC_NLA_LINK_BROADCAST]) {
593 __fill_bc_link_stat(msg, prop, stats);
597 if (link[TIPC_NLA_LINK_ACTIVE])
598 tipc_tlv_sprintf(msg->rep, " ACTIVE");
599 else if (link[TIPC_NLA_LINK_UP])
600 tipc_tlv_sprintf(msg->rep, " STANDBY");
602 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
604 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
605 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
606 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
608 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
609 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
610 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
612 tipc_tlv_sprintf(msg->rep,
613 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
614 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
615 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
616 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
617 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
618 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
619 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
621 tipc_tlv_sprintf(msg->rep,
622 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
623 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
624 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
625 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
626 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
627 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
628 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
630 tipc_tlv_sprintf(msg->rep,
631 " TX profile sample:%u packets average:%u octets\n",
632 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
633 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
634 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
636 tipc_tlv_sprintf(msg->rep,
637 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
638 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
639 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
640 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
641 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
642 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
643 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
644 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
645 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
647 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
648 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
649 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
650 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
651 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
652 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
653 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
655 tipc_tlv_sprintf(msg->rep,
656 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
657 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
658 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
659 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
660 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
661 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
663 tipc_tlv_sprintf(msg->rep,
664 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
665 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
666 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
667 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
668 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
669 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
671 tipc_tlv_sprintf(msg->rep,
672 " Congestion link:%u Send queue max:%u avg:%u",
673 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
674 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
675 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
680 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
681 struct nlattr **attrs)
683 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
684 struct tipc_link_info link_info;
687 if (!attrs[TIPC_NLA_LINK])
690 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
691 attrs[TIPC_NLA_LINK], NULL, NULL);
695 link_info.dest = htonl(nla_get_flag(link[TIPC_NLA_LINK_DEST]));
696 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
697 nla_strscpy(link_info.str, link[TIPC_NLA_LINK_NAME],
700 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
701 &link_info, sizeof(link_info));
704 static int __tipc_add_link_prop(struct sk_buff *skb,
705 struct tipc_nl_compat_msg *msg,
706 struct tipc_link_config *lc)
709 case TIPC_CMD_SET_LINK_PRI:
710 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
711 case TIPC_CMD_SET_LINK_TOL:
712 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
713 case TIPC_CMD_SET_LINK_WINDOW:
714 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
720 static int tipc_nl_compat_media_set(struct sk_buff *skb,
721 struct tipc_nl_compat_msg *msg)
724 struct nlattr *media;
725 struct tipc_link_config *lc;
727 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
729 media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
733 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
736 prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
740 __tipc_add_link_prop(skb, msg, lc);
741 nla_nest_end(skb, prop);
742 nla_nest_end(skb, media);
747 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
748 struct tipc_nl_compat_msg *msg)
751 struct nlattr *bearer;
752 struct tipc_link_config *lc;
754 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
756 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
760 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
763 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
767 __tipc_add_link_prop(skb, msg, lc);
768 nla_nest_end(skb, prop);
769 nla_nest_end(skb, bearer);
774 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
775 struct tipc_nl_compat_msg *msg)
779 struct tipc_link_config *lc;
781 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
783 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
787 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
790 prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
794 __tipc_add_link_prop(skb, msg, lc);
795 nla_nest_end(skb, prop);
796 nla_nest_end(skb, link);
801 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
803 struct tipc_nl_compat_msg *msg)
805 struct tipc_link_config *lc;
806 struct tipc_bearer *bearer;
807 struct tipc_media *media;
810 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
812 len = TLV_GET_DATA_LEN(msg->req);
813 len -= offsetof(struct tipc_link_config, name);
817 len = min_t(int, len, TIPC_MAX_LINK_NAME);
818 if (!string_is_terminated(lc->name, len))
821 media = tipc_media_find(lc->name);
823 cmd->doit = &__tipc_nl_media_set;
824 return tipc_nl_compat_media_set(skb, msg);
827 bearer = tipc_bearer_find(msg->net, lc->name);
829 cmd->doit = &__tipc_nl_bearer_set;
830 return tipc_nl_compat_bearer_set(skb, msg);
833 return __tipc_nl_compat_link_set(skb, msg);
836 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
838 struct tipc_nl_compat_msg *msg)
844 name = (char *)TLV_DATA(msg->req);
846 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
850 len = TLV_GET_DATA_LEN(msg->req);
854 len = min_t(int, len, TIPC_MAX_LINK_NAME);
855 if (!string_is_terminated(name, len))
858 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
861 nla_nest_end(skb, link);
866 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
870 struct tipc_name_table_query *ntq;
871 static const char * const header[] = {
878 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
879 if (TLV_GET_DATA_LEN(msg->req) < (int)sizeof(struct tipc_name_table_query))
882 depth = ntohl(ntq->depth);
886 for (i = 0; i < depth; i++)
887 tipc_tlv_sprintf(msg->rep, header[i]);
888 tipc_tlv_sprintf(msg->rep, "\n");
893 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
894 struct nlattr **attrs)
897 struct tipc_name_table_query *ntq;
898 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
899 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
900 u32 node, depth, type, lowbound, upbound;
901 static const char * const scope_str[] = {"", " zone", " cluster",
905 if (!attrs[TIPC_NLA_NAME_TABLE])
908 err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
909 attrs[TIPC_NLA_NAME_TABLE], NULL,
914 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
917 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
918 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
923 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
925 depth = ntohl(ntq->depth);
926 type = ntohl(ntq->type);
927 lowbound = ntohl(ntq->lowbound);
928 upbound = ntohl(ntq->upbound);
930 if (!(depth & TIPC_NTQ_ALLTYPES) &&
931 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
933 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
935 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
938 tipc_tlv_sprintf(msg->rep, "%-10u ",
939 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
944 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
945 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
946 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
951 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
952 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
953 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
954 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
959 tipc_tlv_sprintf(msg->rep, "%-10u %s",
960 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
961 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
963 tipc_tlv_sprintf(msg->rep, "\n");
968 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
969 struct nlattr **attrs)
971 u32 type, lower, upper;
972 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
975 if (!attrs[TIPC_NLA_PUBL])
978 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
979 attrs[TIPC_NLA_PUBL], NULL, NULL);
983 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
984 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
985 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
988 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
990 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
995 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
1000 struct sk_buff *args;
1001 struct tipc_nl_compat_cmd_dump dump;
1003 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1007 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
1014 nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
1020 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
1025 nla_nest_end(args, nest);
1026 genlmsg_end(args, hdr);
1028 dump.dumpit = tipc_nl_publ_dump;
1029 dump.format = __tipc_nl_compat_publ_dump;
1031 err = __tipc_nl_compat_dumpit(&dump, msg, args);
1038 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1039 struct nlattr **attrs)
1043 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1045 if (!attrs[TIPC_NLA_SOCK])
1048 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
1049 attrs[TIPC_NLA_SOCK], NULL, NULL);
1053 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1054 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1056 if (sock[TIPC_NLA_SOCK_CON]) {
1058 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1060 err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
1061 sock[TIPC_NLA_SOCK_CON],
1067 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1068 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1072 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1074 if (con[TIPC_NLA_CON_FLAG])
1075 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1076 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1077 nla_get_u32(con[TIPC_NLA_CON_INST]));
1079 tipc_tlv_sprintf(msg->rep, "\n");
1080 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1081 tipc_tlv_sprintf(msg->rep, " bound to");
1083 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1087 tipc_tlv_sprintf(msg->rep, "\n");
1092 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1093 struct nlattr **attrs)
1095 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1098 if (!attrs[TIPC_NLA_MEDIA])
1101 err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1102 attrs[TIPC_NLA_MEDIA], NULL, NULL);
1106 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1107 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1108 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1111 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1112 struct nlattr **attrs)
1114 struct tipc_node_info node_info;
1115 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1118 if (!attrs[TIPC_NLA_NODE])
1121 err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
1122 attrs[TIPC_NLA_NODE], NULL, NULL);
1126 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1127 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1129 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1133 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1134 struct sk_buff *skb,
1135 struct tipc_nl_compat_msg *msg)
1140 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1142 net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
1146 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1147 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1149 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1150 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1153 nla_nest_end(skb, net);
1158 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1159 struct nlattr **attrs)
1162 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1165 if (!attrs[TIPC_NLA_NET])
1168 err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
1169 attrs[TIPC_NLA_NET], NULL, NULL);
1173 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1175 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1178 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1180 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1184 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1185 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1190 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1192 struct tipc_nl_compat_cmd_dump dump;
1193 struct tipc_nl_compat_cmd_doit doit;
1195 memset(&dump, 0, sizeof(dump));
1196 memset(&doit, 0, sizeof(doit));
1200 msg->rep = tipc_tlv_alloc(0);
1204 case TIPC_CMD_GET_BEARER_NAMES:
1205 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1206 dump.dumpit = tipc_nl_bearer_dump;
1207 dump.format = tipc_nl_compat_bearer_dump;
1208 return tipc_nl_compat_dumpit(&dump, msg);
1209 case TIPC_CMD_ENABLE_BEARER:
1210 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1211 doit.doit = __tipc_nl_bearer_enable;
1212 doit.transcode = tipc_nl_compat_bearer_enable;
1213 return tipc_nl_compat_doit(&doit, msg);
1214 case TIPC_CMD_DISABLE_BEARER:
1215 msg->req_type = TIPC_TLV_BEARER_NAME;
1216 doit.doit = __tipc_nl_bearer_disable;
1217 doit.transcode = tipc_nl_compat_bearer_disable;
1218 return tipc_nl_compat_doit(&doit, msg);
1219 case TIPC_CMD_SHOW_LINK_STATS:
1220 msg->req_type = TIPC_TLV_LINK_NAME;
1221 msg->rep_size = ULTRA_STRING_MAX_LEN;
1222 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1223 dump.dumpit = tipc_nl_node_dump_link;
1224 dump.format = tipc_nl_compat_link_stat_dump;
1225 return tipc_nl_compat_dumpit(&dump, msg);
1226 case TIPC_CMD_GET_LINKS:
1227 msg->req_type = TIPC_TLV_NET_ADDR;
1228 msg->rep_size = ULTRA_STRING_MAX_LEN;
1229 dump.dumpit = tipc_nl_node_dump_link;
1230 dump.format = tipc_nl_compat_link_dump;
1231 return tipc_nl_compat_dumpit(&dump, msg);
1232 case TIPC_CMD_SET_LINK_TOL:
1233 case TIPC_CMD_SET_LINK_PRI:
1234 case TIPC_CMD_SET_LINK_WINDOW:
1235 msg->req_type = TIPC_TLV_LINK_CONFIG;
1236 doit.doit = tipc_nl_node_set_link;
1237 doit.transcode = tipc_nl_compat_link_set;
1238 return tipc_nl_compat_doit(&doit, msg);
1239 case TIPC_CMD_RESET_LINK_STATS:
1240 msg->req_type = TIPC_TLV_LINK_NAME;
1241 doit.doit = tipc_nl_node_reset_link_stats;
1242 doit.transcode = tipc_nl_compat_link_reset_stats;
1243 return tipc_nl_compat_doit(&doit, msg);
1244 case TIPC_CMD_SHOW_NAME_TABLE:
1245 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1246 msg->rep_size = ULTRA_STRING_MAX_LEN;
1247 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1248 dump.header = tipc_nl_compat_name_table_dump_header;
1249 dump.dumpit = tipc_nl_name_table_dump;
1250 dump.format = tipc_nl_compat_name_table_dump;
1251 return tipc_nl_compat_dumpit(&dump, msg);
1252 case TIPC_CMD_SHOW_PORTS:
1253 msg->rep_size = ULTRA_STRING_MAX_LEN;
1254 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1255 dump.dumpit = tipc_nl_sk_dump;
1256 dump.format = tipc_nl_compat_sk_dump;
1257 return tipc_nl_compat_dumpit(&dump, msg);
1258 case TIPC_CMD_GET_MEDIA_NAMES:
1259 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1260 dump.dumpit = tipc_nl_media_dump;
1261 dump.format = tipc_nl_compat_media_dump;
1262 return tipc_nl_compat_dumpit(&dump, msg);
1263 case TIPC_CMD_GET_NODES:
1264 msg->rep_size = ULTRA_STRING_MAX_LEN;
1265 dump.dumpit = tipc_nl_node_dump;
1266 dump.format = tipc_nl_compat_node_dump;
1267 return tipc_nl_compat_dumpit(&dump, msg);
1268 case TIPC_CMD_SET_NODE_ADDR:
1269 msg->req_type = TIPC_TLV_NET_ADDR;
1270 doit.doit = __tipc_nl_net_set;
1271 doit.transcode = tipc_nl_compat_net_set;
1272 return tipc_nl_compat_doit(&doit, msg);
1273 case TIPC_CMD_SET_NETID:
1274 msg->req_type = TIPC_TLV_UNSIGNED;
1275 doit.doit = __tipc_nl_net_set;
1276 doit.transcode = tipc_nl_compat_net_set;
1277 return tipc_nl_compat_doit(&doit, msg);
1278 case TIPC_CMD_GET_NETID:
1279 msg->rep_size = sizeof(u32);
1280 dump.dumpit = tipc_nl_net_dump;
1281 dump.format = tipc_nl_compat_net_dump;
1282 return tipc_nl_compat_dumpit(&dump, msg);
1283 case TIPC_CMD_SHOW_STATS:
1284 return tipc_cmd_show_stats_compat(msg);
1290 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1294 struct tipc_nl_compat_msg msg;
1295 struct nlmsghdr *req_nlh;
1296 struct nlmsghdr *rep_nlh;
1297 struct tipc_genlmsghdr *req_userhdr = genl_info_userhdr(info);
1299 memset(&msg, 0, sizeof(msg));
1301 req_nlh = (struct nlmsghdr *)skb->data;
1302 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1303 msg.cmd = req_userhdr->cmd;
1304 msg.net = genl_info_net(info);
1305 msg.dst_sk = skb->sk;
1307 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1308 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1313 msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1314 if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1315 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1320 err = tipc_nl_compat_handle(&msg);
1321 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1322 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1323 else if (err == -EINVAL)
1324 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1329 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1330 skb_push(msg.rep, len);
1331 rep_nlh = nlmsg_hdr(msg.rep);
1332 memcpy(rep_nlh, info->nlhdr, len);
1333 rep_nlh->nlmsg_len = msg.rep->len;
1334 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1339 static const struct genl_small_ops tipc_genl_compat_ops[] = {
1341 .cmd = TIPC_GENL_CMD,
1342 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1343 .doit = tipc_nl_compat_recv,
1347 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1348 .name = TIPC_GENL_NAME,
1349 .version = TIPC_GENL_VERSION,
1350 .hdrsize = TIPC_GENL_HDRLEN,
1353 .module = THIS_MODULE,
1354 .small_ops = tipc_genl_compat_ops,
1355 .n_small_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1356 .resv_start_op = TIPC_GENL_CMD + 1,
1359 int __init tipc_netlink_compat_start(void)
1363 res = genl_register_family(&tipc_genl_compat_family);
1365 pr_err("Failed to register legacy compat interface\n");
1372 void tipc_netlink_compat_stop(void)
1374 genl_unregister_family(&tipc_genl_compat_family);