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 memset(tlv, 0, 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 inline bool string_is_valid(char *s, int len)
179 return memchr(s, '\0', len) ? true : false;
182 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
183 struct tipc_nl_compat_msg *msg,
186 struct genl_dumpit_info info;
190 struct nlmsghdr *nlmsg;
191 struct netlink_callback cb;
192 struct nlattr **attrbuf;
194 memset(&cb, 0, sizeof(cb));
195 cb.nlh = (struct nlmsghdr *)arg->data;
199 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
203 buf->sk = msg->dst_sk;
204 if (__tipc_dump_start(&cb, msg->net)) {
209 attrbuf = kcalloc(tipc_genl_family.maxattr + 1,
210 sizeof(struct nlattr *), GFP_KERNEL);
216 info.attrs = attrbuf;
218 if (nlmsg_len(cb.nlh) > 0) {
219 err = nlmsg_parse_deprecated(cb.nlh, GENL_HDRLEN, attrbuf,
220 tipc_genl_family.maxattr,
221 tipc_genl_family.policy, NULL);
228 len = (*cmd->dumpit)(buf, &cb);
230 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
231 err = nlmsg_parse_deprecated(nlmsg, GENL_HDRLEN,
233 tipc_genl_family.maxattr,
234 tipc_genl_family.policy,
239 err = (*cmd->format)(msg, attrbuf);
243 if (tipc_skb_tailroom(msg->rep) <= 1) {
249 skb_reset_tail_pointer(buf);
261 if (err == -EMSGSIZE) {
262 /* The legacy API only considered messages filling
263 * "ULTRA_STRING_MAX_LEN" to be truncated.
265 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
266 char *tail = skb_tail_pointer(msg->rep);
269 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
279 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
280 struct tipc_nl_compat_msg *msg)
282 struct nlmsghdr *nlh;
286 if (msg->req_type && (!msg->req_size ||
287 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
290 msg->rep = tipc_tlv_alloc(msg->rep_size);
295 tipc_tlv_init(msg->rep, msg->rep_type);
298 err = (*cmd->header)(msg);
306 arg = nlmsg_new(0, GFP_KERNEL);
313 nlh = nlmsg_put(arg, 0, 0, tipc_genl_family.id, 0, NLM_F_MULTI);
322 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
332 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
333 struct tipc_nl_compat_msg *msg)
336 struct sk_buff *doit_buf;
337 struct sk_buff *trans_buf;
338 struct nlattr **attrbuf;
339 struct genl_info info;
341 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
345 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
346 sizeof(struct nlattr *),
353 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
359 memset(&info, 0, sizeof(info));
360 info.attrs = attrbuf;
363 err = (*cmd->transcode)(cmd, trans_buf, msg);
367 err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
368 (const struct nlattr *)trans_buf->data,
369 trans_buf->len, NULL, NULL);
373 doit_buf->sk = msg->dst_sk;
375 err = (*cmd->doit)(doit_buf, &info);
383 kfree_skb(trans_buf);
388 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
389 struct tipc_nl_compat_msg *msg)
393 if (msg->req_type && (!msg->req_size ||
394 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
397 err = __tipc_nl_compat_doit(cmd, msg);
401 /* The legacy API considered an empty message a success message */
402 msg->rep = tipc_tlv_alloc(0);
409 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
410 struct nlattr **attrs)
412 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
415 if (!attrs[TIPC_NLA_BEARER])
418 err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
419 attrs[TIPC_NLA_BEARER], NULL, NULL);
423 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
424 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
425 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
428 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
430 struct tipc_nl_compat_msg *msg)
433 struct nlattr *bearer;
434 struct tipc_bearer_config *b;
437 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
439 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
443 len = TLV_GET_DATA_LEN(msg->req);
444 len -= offsetof(struct tipc_bearer_config, name);
448 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
449 if (!string_is_valid(b->name, len))
452 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
455 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
458 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
459 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
462 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
464 nla_nest_end(skb, prop);
466 nla_nest_end(skb, bearer);
471 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
473 struct tipc_nl_compat_msg *msg)
476 struct nlattr *bearer;
479 name = (char *)TLV_DATA(msg->req);
481 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
485 len = TLV_GET_DATA_LEN(msg->req);
489 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
490 if (!string_is_valid(name, len))
493 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
496 nla_nest_end(skb, bearer);
501 static inline u32 perc(u32 count, u32 total)
503 return (count * 100 + (total / 2)) / total;
506 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
507 struct nlattr *prop[], struct nlattr *stats[])
509 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
510 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
512 tipc_tlv_sprintf(msg->rep,
513 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
514 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
515 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
516 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
517 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
518 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
520 tipc_tlv_sprintf(msg->rep,
521 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
522 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
523 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
524 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
525 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
526 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
528 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
529 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
530 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
531 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
533 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
534 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
535 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
536 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
538 tipc_tlv_sprintf(msg->rep,
539 " Congestion link:%u Send queue max:%u avg:%u",
540 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
541 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
542 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
545 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
546 struct nlattr **attrs)
549 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
550 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
551 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
555 if (!attrs[TIPC_NLA_LINK])
558 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
559 attrs[TIPC_NLA_LINK], NULL, NULL);
563 if (!link[TIPC_NLA_LINK_PROP])
566 err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
567 link[TIPC_NLA_LINK_PROP], NULL,
572 if (!link[TIPC_NLA_LINK_STATS])
575 err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
576 link[TIPC_NLA_LINK_STATS], NULL,
581 name = (char *)TLV_DATA(msg->req);
583 len = TLV_GET_DATA_LEN(msg->req);
587 len = min_t(int, len, TIPC_MAX_LINK_NAME);
588 if (!string_is_valid(name, len))
591 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
594 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
595 (char *)nla_data(link[TIPC_NLA_LINK_NAME]));
597 if (link[TIPC_NLA_LINK_BROADCAST]) {
598 __fill_bc_link_stat(msg, prop, stats);
602 if (link[TIPC_NLA_LINK_ACTIVE])
603 tipc_tlv_sprintf(msg->rep, " ACTIVE");
604 else if (link[TIPC_NLA_LINK_UP])
605 tipc_tlv_sprintf(msg->rep, " STANDBY");
607 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
609 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
610 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
611 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
613 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
614 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
615 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
617 tipc_tlv_sprintf(msg->rep,
618 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
619 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
620 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
621 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
622 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
623 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
624 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
626 tipc_tlv_sprintf(msg->rep,
627 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
628 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
629 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
630 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
631 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
632 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
633 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
635 tipc_tlv_sprintf(msg->rep,
636 " TX profile sample:%u packets average:%u octets\n",
637 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
638 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
639 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
641 tipc_tlv_sprintf(msg->rep,
642 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
643 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
644 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
645 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
646 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
647 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
648 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
649 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
650 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
652 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
653 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
654 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
655 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
656 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
657 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
658 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
660 tipc_tlv_sprintf(msg->rep,
661 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
662 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
663 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
664 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
665 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
666 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
668 tipc_tlv_sprintf(msg->rep,
669 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
670 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
671 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
672 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
673 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
674 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
676 tipc_tlv_sprintf(msg->rep,
677 " Congestion link:%u Send queue max:%u avg:%u",
678 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
679 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
680 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
685 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
686 struct nlattr **attrs)
688 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
689 struct tipc_link_info link_info;
692 if (!attrs[TIPC_NLA_LINK])
695 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
696 attrs[TIPC_NLA_LINK], NULL, NULL);
700 link_info.dest = htonl(nla_get_flag(link[TIPC_NLA_LINK_DEST]));
701 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
702 nla_strscpy(link_info.str, link[TIPC_NLA_LINK_NAME],
705 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
706 &link_info, sizeof(link_info));
709 static int __tipc_add_link_prop(struct sk_buff *skb,
710 struct tipc_nl_compat_msg *msg,
711 struct tipc_link_config *lc)
714 case TIPC_CMD_SET_LINK_PRI:
715 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
716 case TIPC_CMD_SET_LINK_TOL:
717 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
718 case TIPC_CMD_SET_LINK_WINDOW:
719 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
725 static int tipc_nl_compat_media_set(struct sk_buff *skb,
726 struct tipc_nl_compat_msg *msg)
729 struct nlattr *media;
730 struct tipc_link_config *lc;
732 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
734 media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
738 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
741 prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
745 __tipc_add_link_prop(skb, msg, lc);
746 nla_nest_end(skb, prop);
747 nla_nest_end(skb, media);
752 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
753 struct tipc_nl_compat_msg *msg)
756 struct nlattr *bearer;
757 struct tipc_link_config *lc;
759 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
761 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
765 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
768 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
772 __tipc_add_link_prop(skb, msg, lc);
773 nla_nest_end(skb, prop);
774 nla_nest_end(skb, bearer);
779 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
780 struct tipc_nl_compat_msg *msg)
784 struct tipc_link_config *lc;
786 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
788 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
792 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
795 prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
799 __tipc_add_link_prop(skb, msg, lc);
800 nla_nest_end(skb, prop);
801 nla_nest_end(skb, link);
806 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
808 struct tipc_nl_compat_msg *msg)
810 struct tipc_link_config *lc;
811 struct tipc_bearer *bearer;
812 struct tipc_media *media;
815 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
817 len = TLV_GET_DATA_LEN(msg->req);
818 len -= offsetof(struct tipc_link_config, name);
822 len = min_t(int, len, TIPC_MAX_LINK_NAME);
823 if (!string_is_valid(lc->name, len))
826 media = tipc_media_find(lc->name);
828 cmd->doit = &__tipc_nl_media_set;
829 return tipc_nl_compat_media_set(skb, msg);
832 bearer = tipc_bearer_find(msg->net, lc->name);
834 cmd->doit = &__tipc_nl_bearer_set;
835 return tipc_nl_compat_bearer_set(skb, msg);
838 return __tipc_nl_compat_link_set(skb, msg);
841 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
843 struct tipc_nl_compat_msg *msg)
849 name = (char *)TLV_DATA(msg->req);
851 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
855 len = TLV_GET_DATA_LEN(msg->req);
859 len = min_t(int, len, TIPC_MAX_LINK_NAME);
860 if (!string_is_valid(name, len))
863 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
866 nla_nest_end(skb, link);
871 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
875 struct tipc_name_table_query *ntq;
876 static const char * const header[] = {
883 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
884 if (TLV_GET_DATA_LEN(msg->req) < (int)sizeof(struct tipc_name_table_query))
887 depth = ntohl(ntq->depth);
891 for (i = 0; i < depth; i++)
892 tipc_tlv_sprintf(msg->rep, header[i]);
893 tipc_tlv_sprintf(msg->rep, "\n");
898 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
899 struct nlattr **attrs)
902 struct tipc_name_table_query *ntq;
903 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
904 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
905 u32 node, depth, type, lowbound, upbound;
906 static const char * const scope_str[] = {"", " zone", " cluster",
910 if (!attrs[TIPC_NLA_NAME_TABLE])
913 err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
914 attrs[TIPC_NLA_NAME_TABLE], NULL,
919 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
922 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
923 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
928 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
930 depth = ntohl(ntq->depth);
931 type = ntohl(ntq->type);
932 lowbound = ntohl(ntq->lowbound);
933 upbound = ntohl(ntq->upbound);
935 if (!(depth & TIPC_NTQ_ALLTYPES) &&
936 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
938 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
940 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
943 tipc_tlv_sprintf(msg->rep, "%-10u ",
944 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
949 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
950 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
951 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
956 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
957 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
958 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
959 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
964 tipc_tlv_sprintf(msg->rep, "%-10u %s",
965 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
966 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
968 tipc_tlv_sprintf(msg->rep, "\n");
973 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
974 struct nlattr **attrs)
976 u32 type, lower, upper;
977 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
980 if (!attrs[TIPC_NLA_PUBL])
983 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
984 attrs[TIPC_NLA_PUBL], NULL, NULL);
988 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
989 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
990 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
993 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
995 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
1000 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
1004 struct nlattr *nest;
1005 struct sk_buff *args;
1006 struct tipc_nl_compat_cmd_dump dump;
1008 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1012 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
1019 nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
1025 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
1030 nla_nest_end(args, nest);
1031 genlmsg_end(args, hdr);
1033 dump.dumpit = tipc_nl_publ_dump;
1034 dump.format = __tipc_nl_compat_publ_dump;
1036 err = __tipc_nl_compat_dumpit(&dump, msg, args);
1043 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1044 struct nlattr **attrs)
1048 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1050 if (!attrs[TIPC_NLA_SOCK])
1053 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
1054 attrs[TIPC_NLA_SOCK], NULL, NULL);
1058 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1059 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1061 if (sock[TIPC_NLA_SOCK_CON]) {
1063 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1065 err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
1066 sock[TIPC_NLA_SOCK_CON],
1072 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1073 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1077 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1079 if (con[TIPC_NLA_CON_FLAG])
1080 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1081 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1082 nla_get_u32(con[TIPC_NLA_CON_INST]));
1084 tipc_tlv_sprintf(msg->rep, "\n");
1085 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1086 tipc_tlv_sprintf(msg->rep, " bound to");
1088 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1092 tipc_tlv_sprintf(msg->rep, "\n");
1097 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1098 struct nlattr **attrs)
1100 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1103 if (!attrs[TIPC_NLA_MEDIA])
1106 err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1107 attrs[TIPC_NLA_MEDIA], NULL, NULL);
1111 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1112 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1113 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1116 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1117 struct nlattr **attrs)
1119 struct tipc_node_info node_info;
1120 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1123 if (!attrs[TIPC_NLA_NODE])
1126 err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
1127 attrs[TIPC_NLA_NODE], NULL, NULL);
1131 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1132 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1134 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1138 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1139 struct sk_buff *skb,
1140 struct tipc_nl_compat_msg *msg)
1145 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1147 net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
1151 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1152 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1154 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1155 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1158 nla_nest_end(skb, net);
1163 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1164 struct nlattr **attrs)
1167 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1170 if (!attrs[TIPC_NLA_NET])
1173 err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
1174 attrs[TIPC_NLA_NET], NULL, NULL);
1178 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1180 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1183 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1185 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1189 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1190 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1195 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1197 struct tipc_nl_compat_cmd_dump dump;
1198 struct tipc_nl_compat_cmd_doit doit;
1200 memset(&dump, 0, sizeof(dump));
1201 memset(&doit, 0, sizeof(doit));
1205 msg->rep = tipc_tlv_alloc(0);
1209 case TIPC_CMD_GET_BEARER_NAMES:
1210 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1211 dump.dumpit = tipc_nl_bearer_dump;
1212 dump.format = tipc_nl_compat_bearer_dump;
1213 return tipc_nl_compat_dumpit(&dump, msg);
1214 case TIPC_CMD_ENABLE_BEARER:
1215 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1216 doit.doit = __tipc_nl_bearer_enable;
1217 doit.transcode = tipc_nl_compat_bearer_enable;
1218 return tipc_nl_compat_doit(&doit, msg);
1219 case TIPC_CMD_DISABLE_BEARER:
1220 msg->req_type = TIPC_TLV_BEARER_NAME;
1221 doit.doit = __tipc_nl_bearer_disable;
1222 doit.transcode = tipc_nl_compat_bearer_disable;
1223 return tipc_nl_compat_doit(&doit, msg);
1224 case TIPC_CMD_SHOW_LINK_STATS:
1225 msg->req_type = TIPC_TLV_LINK_NAME;
1226 msg->rep_size = ULTRA_STRING_MAX_LEN;
1227 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1228 dump.dumpit = tipc_nl_node_dump_link;
1229 dump.format = tipc_nl_compat_link_stat_dump;
1230 return tipc_nl_compat_dumpit(&dump, msg);
1231 case TIPC_CMD_GET_LINKS:
1232 msg->req_type = TIPC_TLV_NET_ADDR;
1233 msg->rep_size = ULTRA_STRING_MAX_LEN;
1234 dump.dumpit = tipc_nl_node_dump_link;
1235 dump.format = tipc_nl_compat_link_dump;
1236 return tipc_nl_compat_dumpit(&dump, msg);
1237 case TIPC_CMD_SET_LINK_TOL:
1238 case TIPC_CMD_SET_LINK_PRI:
1239 case TIPC_CMD_SET_LINK_WINDOW:
1240 msg->req_type = TIPC_TLV_LINK_CONFIG;
1241 doit.doit = tipc_nl_node_set_link;
1242 doit.transcode = tipc_nl_compat_link_set;
1243 return tipc_nl_compat_doit(&doit, msg);
1244 case TIPC_CMD_RESET_LINK_STATS:
1245 msg->req_type = TIPC_TLV_LINK_NAME;
1246 doit.doit = tipc_nl_node_reset_link_stats;
1247 doit.transcode = tipc_nl_compat_link_reset_stats;
1248 return tipc_nl_compat_doit(&doit, msg);
1249 case TIPC_CMD_SHOW_NAME_TABLE:
1250 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1251 msg->rep_size = ULTRA_STRING_MAX_LEN;
1252 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1253 dump.header = tipc_nl_compat_name_table_dump_header;
1254 dump.dumpit = tipc_nl_name_table_dump;
1255 dump.format = tipc_nl_compat_name_table_dump;
1256 return tipc_nl_compat_dumpit(&dump, msg);
1257 case TIPC_CMD_SHOW_PORTS:
1258 msg->rep_size = ULTRA_STRING_MAX_LEN;
1259 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1260 dump.dumpit = tipc_nl_sk_dump;
1261 dump.format = tipc_nl_compat_sk_dump;
1262 return tipc_nl_compat_dumpit(&dump, msg);
1263 case TIPC_CMD_GET_MEDIA_NAMES:
1264 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1265 dump.dumpit = tipc_nl_media_dump;
1266 dump.format = tipc_nl_compat_media_dump;
1267 return tipc_nl_compat_dumpit(&dump, msg);
1268 case TIPC_CMD_GET_NODES:
1269 msg->rep_size = ULTRA_STRING_MAX_LEN;
1270 dump.dumpit = tipc_nl_node_dump;
1271 dump.format = tipc_nl_compat_node_dump;
1272 return tipc_nl_compat_dumpit(&dump, msg);
1273 case TIPC_CMD_SET_NODE_ADDR:
1274 msg->req_type = TIPC_TLV_NET_ADDR;
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_SET_NETID:
1279 msg->req_type = TIPC_TLV_UNSIGNED;
1280 doit.doit = __tipc_nl_net_set;
1281 doit.transcode = tipc_nl_compat_net_set;
1282 return tipc_nl_compat_doit(&doit, msg);
1283 case TIPC_CMD_GET_NETID:
1284 msg->rep_size = sizeof(u32);
1285 dump.dumpit = tipc_nl_net_dump;
1286 dump.format = tipc_nl_compat_net_dump;
1287 return tipc_nl_compat_dumpit(&dump, msg);
1288 case TIPC_CMD_SHOW_STATS:
1289 return tipc_cmd_show_stats_compat(msg);
1295 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1299 struct tipc_nl_compat_msg msg;
1300 struct nlmsghdr *req_nlh;
1301 struct nlmsghdr *rep_nlh;
1302 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1304 memset(&msg, 0, sizeof(msg));
1306 req_nlh = (struct nlmsghdr *)skb->data;
1307 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1308 msg.cmd = req_userhdr->cmd;
1309 msg.net = genl_info_net(info);
1310 msg.dst_sk = skb->sk;
1312 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1313 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1318 msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1319 if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1320 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1325 err = tipc_nl_compat_handle(&msg);
1326 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1327 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1328 else if (err == -EINVAL)
1329 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1334 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1335 skb_push(msg.rep, len);
1336 rep_nlh = nlmsg_hdr(msg.rep);
1337 memcpy(rep_nlh, info->nlhdr, len);
1338 rep_nlh->nlmsg_len = msg.rep->len;
1339 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1344 static const struct genl_small_ops tipc_genl_compat_ops[] = {
1346 .cmd = TIPC_GENL_CMD,
1347 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1348 .doit = tipc_nl_compat_recv,
1352 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1353 .name = TIPC_GENL_NAME,
1354 .version = TIPC_GENL_VERSION,
1355 .hdrsize = TIPC_GENL_HDRLEN,
1358 .module = THIS_MODULE,
1359 .small_ops = tipc_genl_compat_ops,
1360 .n_small_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1361 .resv_start_op = TIPC_GENL_CMD + 1,
1364 int __init tipc_netlink_compat_start(void)
1368 res = genl_register_family(&tipc_genl_compat_family);
1370 pr_err("Failed to register legacy compat interface\n");
1377 void tipc_netlink_compat_stop(void)
1379 genl_unregister_family(&tipc_genl_compat_family);