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 memset(tlv, 0, TLV_SPACE(len));
106 tlv->tlv_type = htons(type);
107 tlv->tlv_len = htons(TLV_LENGTH(len));
109 memcpy(TLV_DATA(tlv), data, len);
114 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
116 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
119 TLV_SET_TYPE(tlv, type);
120 skb_put(skb, sizeof(struct tlv_desc));
123 static __printf(2, 3) int tipc_tlv_sprintf(struct sk_buff *skb,
124 const char *fmt, ...)
130 struct tlv_desc *tlv;
133 rem = tipc_skb_tailroom(skb);
135 tlv = (struct tlv_desc *)skb->data;
136 len = TLV_GET_LEN(tlv);
137 buf = TLV_DATA(tlv) + len;
140 n = vscnprintf(buf, rem, fmt, args);
143 TLV_SET_LEN(tlv, n + len);
149 static struct sk_buff *tipc_tlv_alloc(int size)
154 size = TLV_SPACE(size);
155 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
157 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
161 skb_reserve(buf, hdr_len);
166 static struct sk_buff *tipc_get_err_tlv(char *str)
168 int str_len = strlen(str) + 1;
171 buf = tipc_tlv_alloc(str_len);
173 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
178 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
179 struct tipc_nl_compat_msg *msg,
182 struct genl_dumpit_info info;
186 struct nlmsghdr *nlmsg;
187 struct netlink_callback cb;
188 struct nlattr **attrbuf;
190 memset(&cb, 0, sizeof(cb));
191 cb.nlh = (struct nlmsghdr *)arg->data;
195 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
199 buf->sk = msg->dst_sk;
200 if (__tipc_dump_start(&cb, msg->net)) {
205 attrbuf = kcalloc(tipc_genl_family.maxattr + 1,
206 sizeof(struct nlattr *), GFP_KERNEL);
212 info.info.attrs = attrbuf;
214 if (nlmsg_len(cb.nlh) > 0) {
215 err = nlmsg_parse_deprecated(cb.nlh, GENL_HDRLEN, attrbuf,
216 tipc_genl_family.maxattr,
217 tipc_genl_family.policy, NULL);
224 len = (*cmd->dumpit)(buf, &cb);
226 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
227 err = nlmsg_parse_deprecated(nlmsg, GENL_HDRLEN,
229 tipc_genl_family.maxattr,
230 tipc_genl_family.policy,
235 err = (*cmd->format)(msg, attrbuf);
239 if (tipc_skb_tailroom(msg->rep) <= 1) {
245 skb_reset_tail_pointer(buf);
257 if (err == -EMSGSIZE) {
258 /* The legacy API only considered messages filling
259 * "ULTRA_STRING_MAX_LEN" to be truncated.
261 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
262 char *tail = skb_tail_pointer(msg->rep);
265 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
275 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
276 struct tipc_nl_compat_msg *msg)
278 struct nlmsghdr *nlh;
282 if (msg->req_type && (!msg->req_size ||
283 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
286 msg->rep = tipc_tlv_alloc(msg->rep_size);
291 tipc_tlv_init(msg->rep, msg->rep_type);
294 err = (*cmd->header)(msg);
302 arg = nlmsg_new(0, GFP_KERNEL);
309 nlh = nlmsg_put(arg, 0, 0, tipc_genl_family.id, 0, NLM_F_MULTI);
318 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
328 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
329 struct tipc_nl_compat_msg *msg)
332 struct sk_buff *doit_buf;
333 struct sk_buff *trans_buf;
334 struct nlattr **attrbuf;
335 struct genl_info info;
337 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
341 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
342 sizeof(struct nlattr *),
349 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
355 memset(&info, 0, sizeof(info));
356 info.attrs = attrbuf;
359 err = (*cmd->transcode)(cmd, trans_buf, msg);
363 err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
364 (const struct nlattr *)trans_buf->data,
365 trans_buf->len, NULL, NULL);
369 doit_buf->sk = msg->dst_sk;
371 err = (*cmd->doit)(doit_buf, &info);
379 kfree_skb(trans_buf);
384 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
385 struct tipc_nl_compat_msg *msg)
389 if (msg->req_type && (!msg->req_size ||
390 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
393 err = __tipc_nl_compat_doit(cmd, msg);
397 /* The legacy API considered an empty message a success message */
398 msg->rep = tipc_tlv_alloc(0);
405 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
406 struct nlattr **attrs)
408 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
411 if (!attrs[TIPC_NLA_BEARER])
414 err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
415 attrs[TIPC_NLA_BEARER], NULL, NULL);
419 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
420 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
421 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
424 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
426 struct tipc_nl_compat_msg *msg)
429 struct nlattr *bearer;
430 struct tipc_bearer_config *b;
433 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
435 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
439 len = TLV_GET_DATA_LEN(msg->req);
440 len -= offsetof(struct tipc_bearer_config, name);
444 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
445 if (!string_is_terminated(b->name, len))
448 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
451 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
454 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
455 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
458 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
460 nla_nest_end(skb, prop);
462 nla_nest_end(skb, bearer);
467 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
469 struct tipc_nl_compat_msg *msg)
472 struct nlattr *bearer;
475 name = (char *)TLV_DATA(msg->req);
477 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
481 len = TLV_GET_DATA_LEN(msg->req);
485 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
486 if (!string_is_terminated(name, len))
489 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
492 nla_nest_end(skb, bearer);
497 static inline u32 perc(u32 count, u32 total)
499 return (count * 100 + (total / 2)) / total;
502 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
503 struct nlattr *prop[], struct nlattr *stats[])
505 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
506 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
508 tipc_tlv_sprintf(msg->rep,
509 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
510 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
511 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
512 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
513 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
514 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
516 tipc_tlv_sprintf(msg->rep,
517 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
518 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
519 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
520 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
521 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
522 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
524 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
525 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
526 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
527 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
529 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
530 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
531 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
532 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
534 tipc_tlv_sprintf(msg->rep,
535 " Congestion link:%u Send queue max:%u avg:%u",
536 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
537 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
538 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
541 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
542 struct nlattr **attrs)
545 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
546 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
547 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
551 if (!attrs[TIPC_NLA_LINK])
554 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
555 attrs[TIPC_NLA_LINK], NULL, NULL);
559 if (!link[TIPC_NLA_LINK_PROP])
562 err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
563 link[TIPC_NLA_LINK_PROP], NULL,
568 if (!link[TIPC_NLA_LINK_STATS])
571 err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
572 link[TIPC_NLA_LINK_STATS], NULL,
577 name = (char *)TLV_DATA(msg->req);
579 len = TLV_GET_DATA_LEN(msg->req);
583 len = min_t(int, len, TIPC_MAX_LINK_NAME);
584 if (!string_is_terminated(name, len))
587 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
590 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
591 (char *)nla_data(link[TIPC_NLA_LINK_NAME]));
593 if (link[TIPC_NLA_LINK_BROADCAST]) {
594 __fill_bc_link_stat(msg, prop, stats);
598 if (link[TIPC_NLA_LINK_ACTIVE])
599 tipc_tlv_sprintf(msg->rep, " ACTIVE");
600 else if (link[TIPC_NLA_LINK_UP])
601 tipc_tlv_sprintf(msg->rep, " STANDBY");
603 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
605 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
606 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
607 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
609 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
610 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
611 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
613 tipc_tlv_sprintf(msg->rep,
614 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
615 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
616 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
617 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
618 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
619 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
620 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
622 tipc_tlv_sprintf(msg->rep,
623 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
624 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
625 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
626 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
627 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
628 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
629 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
631 tipc_tlv_sprintf(msg->rep,
632 " TX profile sample:%u packets average:%u octets\n",
633 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
634 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
635 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
637 tipc_tlv_sprintf(msg->rep,
638 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
639 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
640 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
641 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
642 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
643 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
644 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
645 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
646 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
648 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
649 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
650 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
651 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
652 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
653 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
654 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
656 tipc_tlv_sprintf(msg->rep,
657 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
658 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
659 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
660 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
661 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
662 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
664 tipc_tlv_sprintf(msg->rep,
665 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
666 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
667 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
668 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
669 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
670 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
672 tipc_tlv_sprintf(msg->rep,
673 " Congestion link:%u Send queue max:%u avg:%u",
674 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
675 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
676 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
681 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
682 struct nlattr **attrs)
684 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
685 struct tipc_link_info link_info;
688 if (!attrs[TIPC_NLA_LINK])
691 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
692 attrs[TIPC_NLA_LINK], NULL, NULL);
696 link_info.dest = htonl(nla_get_flag(link[TIPC_NLA_LINK_DEST]));
697 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
698 nla_strscpy(link_info.str, link[TIPC_NLA_LINK_NAME],
701 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
702 &link_info, sizeof(link_info));
705 static int __tipc_add_link_prop(struct sk_buff *skb,
706 struct tipc_nl_compat_msg *msg,
707 struct tipc_link_config *lc)
710 case TIPC_CMD_SET_LINK_PRI:
711 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
712 case TIPC_CMD_SET_LINK_TOL:
713 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
714 case TIPC_CMD_SET_LINK_WINDOW:
715 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
721 static int tipc_nl_compat_media_set(struct sk_buff *skb,
722 struct tipc_nl_compat_msg *msg)
725 struct nlattr *media;
726 struct tipc_link_config *lc;
728 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
730 media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
734 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
737 prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
741 __tipc_add_link_prop(skb, msg, lc);
742 nla_nest_end(skb, prop);
743 nla_nest_end(skb, media);
748 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
749 struct tipc_nl_compat_msg *msg)
752 struct nlattr *bearer;
753 struct tipc_link_config *lc;
755 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
757 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
761 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
764 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
768 __tipc_add_link_prop(skb, msg, lc);
769 nla_nest_end(skb, prop);
770 nla_nest_end(skb, bearer);
775 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
776 struct tipc_nl_compat_msg *msg)
780 struct tipc_link_config *lc;
782 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
784 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
788 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
791 prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
795 __tipc_add_link_prop(skb, msg, lc);
796 nla_nest_end(skb, prop);
797 nla_nest_end(skb, link);
802 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
804 struct tipc_nl_compat_msg *msg)
806 struct tipc_link_config *lc;
807 struct tipc_bearer *bearer;
808 struct tipc_media *media;
811 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
813 len = TLV_GET_DATA_LEN(msg->req);
814 len -= offsetof(struct tipc_link_config, name);
818 len = min_t(int, len, TIPC_MAX_LINK_NAME);
819 if (!string_is_terminated(lc->name, len))
822 media = tipc_media_find(lc->name);
824 cmd->doit = &__tipc_nl_media_set;
825 return tipc_nl_compat_media_set(skb, msg);
828 bearer = tipc_bearer_find(msg->net, lc->name);
830 cmd->doit = &__tipc_nl_bearer_set;
831 return tipc_nl_compat_bearer_set(skb, msg);
834 return __tipc_nl_compat_link_set(skb, msg);
837 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
839 struct tipc_nl_compat_msg *msg)
845 name = (char *)TLV_DATA(msg->req);
847 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
851 len = TLV_GET_DATA_LEN(msg->req);
855 len = min_t(int, len, TIPC_MAX_LINK_NAME);
856 if (!string_is_terminated(name, len))
859 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
862 nla_nest_end(skb, link);
867 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
871 struct tipc_name_table_query *ntq;
872 static const char * const header[] = {
879 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
880 if (TLV_GET_DATA_LEN(msg->req) < (int)sizeof(struct tipc_name_table_query))
883 depth = ntohl(ntq->depth);
887 for (i = 0; i < depth; i++)
888 tipc_tlv_sprintf(msg->rep, header[i]);
889 tipc_tlv_sprintf(msg->rep, "\n");
894 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
895 struct nlattr **attrs)
898 struct tipc_name_table_query *ntq;
899 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
900 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
901 u32 node, depth, type, lowbound, upbound;
902 static const char * const scope_str[] = {"", " zone", " cluster",
906 if (!attrs[TIPC_NLA_NAME_TABLE])
909 err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
910 attrs[TIPC_NLA_NAME_TABLE], NULL,
915 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
918 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
919 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
924 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
926 depth = ntohl(ntq->depth);
927 type = ntohl(ntq->type);
928 lowbound = ntohl(ntq->lowbound);
929 upbound = ntohl(ntq->upbound);
931 if (!(depth & TIPC_NTQ_ALLTYPES) &&
932 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
934 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
936 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
939 tipc_tlv_sprintf(msg->rep, "%-10u ",
940 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
945 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
946 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
947 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
952 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
953 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
954 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
955 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
960 tipc_tlv_sprintf(msg->rep, "%-10u %s",
961 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
962 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
964 tipc_tlv_sprintf(msg->rep, "\n");
969 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
970 struct nlattr **attrs)
972 u32 type, lower, upper;
973 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
976 if (!attrs[TIPC_NLA_PUBL])
979 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
980 attrs[TIPC_NLA_PUBL], NULL, NULL);
984 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
985 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
986 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
989 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
991 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
996 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
1000 struct nlattr *nest;
1001 struct sk_buff *args;
1002 struct tipc_nl_compat_cmd_dump dump;
1004 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1008 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
1015 nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
1021 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
1026 nla_nest_end(args, nest);
1027 genlmsg_end(args, hdr);
1029 dump.dumpit = tipc_nl_publ_dump;
1030 dump.format = __tipc_nl_compat_publ_dump;
1032 err = __tipc_nl_compat_dumpit(&dump, msg, args);
1039 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1040 struct nlattr **attrs)
1044 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1046 if (!attrs[TIPC_NLA_SOCK])
1049 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
1050 attrs[TIPC_NLA_SOCK], NULL, NULL);
1054 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1055 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1057 if (sock[TIPC_NLA_SOCK_CON]) {
1059 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1061 err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
1062 sock[TIPC_NLA_SOCK_CON],
1068 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1069 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1073 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1075 if (con[TIPC_NLA_CON_FLAG])
1076 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1077 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1078 nla_get_u32(con[TIPC_NLA_CON_INST]));
1080 tipc_tlv_sprintf(msg->rep, "\n");
1081 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1082 tipc_tlv_sprintf(msg->rep, " bound to");
1084 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1088 tipc_tlv_sprintf(msg->rep, "\n");
1093 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1094 struct nlattr **attrs)
1096 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1099 if (!attrs[TIPC_NLA_MEDIA])
1102 err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1103 attrs[TIPC_NLA_MEDIA], NULL, NULL);
1107 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1108 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1109 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1112 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1113 struct nlattr **attrs)
1115 struct tipc_node_info node_info;
1116 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1119 if (!attrs[TIPC_NLA_NODE])
1122 err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
1123 attrs[TIPC_NLA_NODE], NULL, NULL);
1127 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1128 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1130 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1134 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1135 struct sk_buff *skb,
1136 struct tipc_nl_compat_msg *msg)
1141 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1143 net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
1147 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1148 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1150 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1151 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1154 nla_nest_end(skb, net);
1159 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1160 struct nlattr **attrs)
1163 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1166 if (!attrs[TIPC_NLA_NET])
1169 err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
1170 attrs[TIPC_NLA_NET], NULL, NULL);
1174 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1176 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1179 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1181 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1185 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1186 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1191 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1193 struct tipc_nl_compat_cmd_dump dump;
1194 struct tipc_nl_compat_cmd_doit doit;
1196 memset(&dump, 0, sizeof(dump));
1197 memset(&doit, 0, sizeof(doit));
1201 msg->rep = tipc_tlv_alloc(0);
1205 case TIPC_CMD_GET_BEARER_NAMES:
1206 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1207 dump.dumpit = tipc_nl_bearer_dump;
1208 dump.format = tipc_nl_compat_bearer_dump;
1209 return tipc_nl_compat_dumpit(&dump, msg);
1210 case TIPC_CMD_ENABLE_BEARER:
1211 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1212 doit.doit = __tipc_nl_bearer_enable;
1213 doit.transcode = tipc_nl_compat_bearer_enable;
1214 return tipc_nl_compat_doit(&doit, msg);
1215 case TIPC_CMD_DISABLE_BEARER:
1216 msg->req_type = TIPC_TLV_BEARER_NAME;
1217 doit.doit = __tipc_nl_bearer_disable;
1218 doit.transcode = tipc_nl_compat_bearer_disable;
1219 return tipc_nl_compat_doit(&doit, msg);
1220 case TIPC_CMD_SHOW_LINK_STATS:
1221 msg->req_type = TIPC_TLV_LINK_NAME;
1222 msg->rep_size = ULTRA_STRING_MAX_LEN;
1223 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1224 dump.dumpit = tipc_nl_node_dump_link;
1225 dump.format = tipc_nl_compat_link_stat_dump;
1226 return tipc_nl_compat_dumpit(&dump, msg);
1227 case TIPC_CMD_GET_LINKS:
1228 msg->req_type = TIPC_TLV_NET_ADDR;
1229 msg->rep_size = ULTRA_STRING_MAX_LEN;
1230 dump.dumpit = tipc_nl_node_dump_link;
1231 dump.format = tipc_nl_compat_link_dump;
1232 return tipc_nl_compat_dumpit(&dump, msg);
1233 case TIPC_CMD_SET_LINK_TOL:
1234 case TIPC_CMD_SET_LINK_PRI:
1235 case TIPC_CMD_SET_LINK_WINDOW:
1236 msg->req_type = TIPC_TLV_LINK_CONFIG;
1237 doit.doit = tipc_nl_node_set_link;
1238 doit.transcode = tipc_nl_compat_link_set;
1239 return tipc_nl_compat_doit(&doit, msg);
1240 case TIPC_CMD_RESET_LINK_STATS:
1241 msg->req_type = TIPC_TLV_LINK_NAME;
1242 doit.doit = tipc_nl_node_reset_link_stats;
1243 doit.transcode = tipc_nl_compat_link_reset_stats;
1244 return tipc_nl_compat_doit(&doit, msg);
1245 case TIPC_CMD_SHOW_NAME_TABLE:
1246 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1247 msg->rep_size = ULTRA_STRING_MAX_LEN;
1248 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1249 dump.header = tipc_nl_compat_name_table_dump_header;
1250 dump.dumpit = tipc_nl_name_table_dump;
1251 dump.format = tipc_nl_compat_name_table_dump;
1252 return tipc_nl_compat_dumpit(&dump, msg);
1253 case TIPC_CMD_SHOW_PORTS:
1254 msg->rep_size = ULTRA_STRING_MAX_LEN;
1255 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1256 dump.dumpit = tipc_nl_sk_dump;
1257 dump.format = tipc_nl_compat_sk_dump;
1258 return tipc_nl_compat_dumpit(&dump, msg);
1259 case TIPC_CMD_GET_MEDIA_NAMES:
1260 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1261 dump.dumpit = tipc_nl_media_dump;
1262 dump.format = tipc_nl_compat_media_dump;
1263 return tipc_nl_compat_dumpit(&dump, msg);
1264 case TIPC_CMD_GET_NODES:
1265 msg->rep_size = ULTRA_STRING_MAX_LEN;
1266 dump.dumpit = tipc_nl_node_dump;
1267 dump.format = tipc_nl_compat_node_dump;
1268 return tipc_nl_compat_dumpit(&dump, msg);
1269 case TIPC_CMD_SET_NODE_ADDR:
1270 msg->req_type = TIPC_TLV_NET_ADDR;
1271 doit.doit = __tipc_nl_net_set;
1272 doit.transcode = tipc_nl_compat_net_set;
1273 return tipc_nl_compat_doit(&doit, msg);
1274 case TIPC_CMD_SET_NETID:
1275 msg->req_type = TIPC_TLV_UNSIGNED;
1276 doit.doit = __tipc_nl_net_set;
1277 doit.transcode = tipc_nl_compat_net_set;
1278 return tipc_nl_compat_doit(&doit, msg);
1279 case TIPC_CMD_GET_NETID:
1280 msg->rep_size = sizeof(u32);
1281 dump.dumpit = tipc_nl_net_dump;
1282 dump.format = tipc_nl_compat_net_dump;
1283 return tipc_nl_compat_dumpit(&dump, msg);
1284 case TIPC_CMD_SHOW_STATS:
1285 return tipc_cmd_show_stats_compat(msg);
1291 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1295 struct tipc_nl_compat_msg msg;
1296 struct nlmsghdr *req_nlh;
1297 struct nlmsghdr *rep_nlh;
1298 struct tipc_genlmsghdr *req_userhdr = genl_info_userhdr(info);
1300 memset(&msg, 0, sizeof(msg));
1302 req_nlh = (struct nlmsghdr *)skb->data;
1303 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1304 msg.cmd = req_userhdr->cmd;
1305 msg.net = genl_info_net(info);
1306 msg.dst_sk = skb->sk;
1308 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1309 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1314 msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1315 if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1316 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1321 err = tipc_nl_compat_handle(&msg);
1322 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1323 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1324 else if (err == -EINVAL)
1325 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1330 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1331 skb_push(msg.rep, len);
1332 rep_nlh = nlmsg_hdr(msg.rep);
1333 memcpy(rep_nlh, info->nlhdr, len);
1334 rep_nlh->nlmsg_len = msg.rep->len;
1335 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1340 static const struct genl_small_ops tipc_genl_compat_ops[] = {
1342 .cmd = TIPC_GENL_CMD,
1343 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1344 .doit = tipc_nl_compat_recv,
1348 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1349 .name = TIPC_GENL_NAME,
1350 .version = TIPC_GENL_VERSION,
1351 .hdrsize = TIPC_GENL_HDRLEN,
1354 .module = THIS_MODULE,
1355 .small_ops = tipc_genl_compat_ops,
1356 .n_small_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1357 .resv_start_op = TIPC_GENL_CMD + 1,
1360 int __init tipc_netlink_compat_start(void)
1364 res = genl_register_family(&tipc_genl_compat_family);
1366 pr_err("Failed to register legacy compat interface\n");
1373 void tipc_netlink_compat_stop(void)
1375 genl_unregister_family(&tipc_genl_compat_family);