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 {
64 struct tipc_nl_compat_cmd_dump {
65 int (*header)(struct tipc_nl_compat_msg *);
66 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
67 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
70 struct tipc_nl_compat_cmd_doit {
71 int (*doit)(struct sk_buff *skb, struct genl_info *info);
72 int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
73 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
76 static int tipc_skb_tailroom(struct sk_buff *skb)
81 tailroom = skb_tailroom(skb);
82 limit = TIPC_SKB_MAX - skb->len;
90 static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
92 return TLV_GET_LEN(tlv) - TLV_SPACE(0);
95 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
97 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
99 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
102 skb_put(skb, TLV_SPACE(len));
103 tlv->tlv_type = htons(type);
104 tlv->tlv_len = htons(TLV_LENGTH(len));
106 memcpy(TLV_DATA(tlv), data, len);
111 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
113 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
116 TLV_SET_TYPE(tlv, type);
117 skb_put(skb, sizeof(struct tlv_desc));
120 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
126 struct tlv_desc *tlv;
129 rem = tipc_skb_tailroom(skb);
131 tlv = (struct tlv_desc *)skb->data;
132 len = TLV_GET_LEN(tlv);
133 buf = TLV_DATA(tlv) + len;
136 n = vscnprintf(buf, rem, fmt, args);
139 TLV_SET_LEN(tlv, n + len);
145 static struct sk_buff *tipc_tlv_alloc(int size)
150 size = TLV_SPACE(size);
151 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
153 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
157 skb_reserve(buf, hdr_len);
162 static struct sk_buff *tipc_get_err_tlv(char *str)
164 int str_len = strlen(str) + 1;
167 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
169 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
174 static inline bool string_is_valid(char *s, int len)
176 return memchr(s, '\0', len) ? true : false;
179 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
180 struct tipc_nl_compat_msg *msg,
186 struct nlmsghdr *nlmsg;
187 struct netlink_callback cb;
189 memset(&cb, 0, sizeof(cb));
190 cb.nlh = (struct nlmsghdr *)arg->data;
193 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
197 buf->sk = msg->dst_sk;
198 if (__tipc_dump_start(&cb, msg->net)) {
206 len = (*cmd->dumpit)(buf, &cb);
208 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
209 struct nlattr **attrs;
211 err = tipc_nlmsg_parse(nlmsg, &attrs);
215 err = (*cmd->format)(msg, attrs);
219 if (tipc_skb_tailroom(msg->rep) <= 1) {
225 skb_reset_tail_pointer(buf);
236 if (err == -EMSGSIZE) {
237 /* The legacy API only considered messages filling
238 * "ULTRA_STRING_MAX_LEN" to be truncated.
240 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
241 char *tail = skb_tail_pointer(msg->rep);
244 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
254 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
255 struct tipc_nl_compat_msg *msg)
260 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
263 msg->rep = tipc_tlv_alloc(msg->rep_size);
268 tipc_tlv_init(msg->rep, msg->rep_type);
271 err = (*cmd->header)(msg);
279 arg = nlmsg_new(0, GFP_KERNEL);
286 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
296 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
297 struct tipc_nl_compat_msg *msg)
300 struct sk_buff *doit_buf;
301 struct sk_buff *trans_buf;
302 struct nlattr **attrbuf;
303 struct genl_info info;
305 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
309 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
310 sizeof(struct nlattr *),
317 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
323 memset(&info, 0, sizeof(info));
324 info.attrs = attrbuf;
327 err = (*cmd->transcode)(cmd, trans_buf, msg);
331 err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
332 (const struct nlattr *)trans_buf->data,
333 trans_buf->len, NULL, NULL);
337 doit_buf->sk = msg->dst_sk;
339 err = (*cmd->doit)(doit_buf, &info);
347 kfree_skb(trans_buf);
352 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
353 struct tipc_nl_compat_msg *msg)
357 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
360 err = __tipc_nl_compat_doit(cmd, msg);
364 /* The legacy API considered an empty message a success message */
365 msg->rep = tipc_tlv_alloc(0);
372 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
373 struct nlattr **attrs)
375 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
378 if (!attrs[TIPC_NLA_BEARER])
381 err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
382 attrs[TIPC_NLA_BEARER], NULL, NULL);
386 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
387 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
388 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
391 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
393 struct tipc_nl_compat_msg *msg)
396 struct nlattr *bearer;
397 struct tipc_bearer_config *b;
400 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
402 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
406 len = TLV_GET_DATA_LEN(msg->req);
407 len -= offsetof(struct tipc_bearer_config, name);
411 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
412 if (!string_is_valid(b->name, len))
415 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
418 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
421 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
422 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
425 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
427 nla_nest_end(skb, prop);
429 nla_nest_end(skb, bearer);
434 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
436 struct tipc_nl_compat_msg *msg)
439 struct nlattr *bearer;
442 name = (char *)TLV_DATA(msg->req);
444 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
448 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
449 if (!string_is_valid(name, len))
452 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
455 nla_nest_end(skb, bearer);
460 static inline u32 perc(u32 count, u32 total)
462 return (count * 100 + (total / 2)) / total;
465 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
466 struct nlattr *prop[], struct nlattr *stats[])
468 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
469 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
471 tipc_tlv_sprintf(msg->rep,
472 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
473 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
474 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
475 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
476 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
477 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
479 tipc_tlv_sprintf(msg->rep,
480 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
481 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
482 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
483 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
484 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
485 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
487 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
488 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
489 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
490 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
492 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
493 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
494 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
495 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
497 tipc_tlv_sprintf(msg->rep,
498 " Congestion link:%u Send queue max:%u avg:%u",
499 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
500 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
501 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
504 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
505 struct nlattr **attrs)
508 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
509 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
510 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
514 if (!attrs[TIPC_NLA_LINK])
517 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
518 attrs[TIPC_NLA_LINK], NULL, NULL);
522 if (!link[TIPC_NLA_LINK_PROP])
525 err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
526 link[TIPC_NLA_LINK_PROP], NULL,
531 if (!link[TIPC_NLA_LINK_STATS])
534 err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
535 link[TIPC_NLA_LINK_STATS], NULL,
540 name = (char *)TLV_DATA(msg->req);
542 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
543 if (!string_is_valid(name, len))
546 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
549 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
550 nla_data(link[TIPC_NLA_LINK_NAME]));
552 if (link[TIPC_NLA_LINK_BROADCAST]) {
553 __fill_bc_link_stat(msg, prop, stats);
557 if (link[TIPC_NLA_LINK_ACTIVE])
558 tipc_tlv_sprintf(msg->rep, " ACTIVE");
559 else if (link[TIPC_NLA_LINK_UP])
560 tipc_tlv_sprintf(msg->rep, " STANDBY");
562 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
564 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
565 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
566 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
568 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
569 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
570 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
572 tipc_tlv_sprintf(msg->rep,
573 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
574 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
575 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
576 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
577 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
578 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
579 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
581 tipc_tlv_sprintf(msg->rep,
582 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
583 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
584 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
585 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
586 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
587 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
588 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
590 tipc_tlv_sprintf(msg->rep,
591 " TX profile sample:%u packets average:%u octets\n",
592 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
593 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
594 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
596 tipc_tlv_sprintf(msg->rep,
597 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
598 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
599 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
600 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
601 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
602 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
603 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
604 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
605 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
607 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
608 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
609 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
610 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
611 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
612 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
613 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
615 tipc_tlv_sprintf(msg->rep,
616 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
617 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
618 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
619 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
620 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
621 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
623 tipc_tlv_sprintf(msg->rep,
624 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
625 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
626 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
627 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
628 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
629 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
631 tipc_tlv_sprintf(msg->rep,
632 " Congestion link:%u Send queue max:%u avg:%u",
633 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
634 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
635 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
640 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
641 struct nlattr **attrs)
643 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
644 struct tipc_link_info link_info;
647 if (!attrs[TIPC_NLA_LINK])
650 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
651 attrs[TIPC_NLA_LINK], NULL, NULL);
655 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
656 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
657 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
660 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
661 &link_info, sizeof(link_info));
664 static int __tipc_add_link_prop(struct sk_buff *skb,
665 struct tipc_nl_compat_msg *msg,
666 struct tipc_link_config *lc)
669 case TIPC_CMD_SET_LINK_PRI:
670 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
671 case TIPC_CMD_SET_LINK_TOL:
672 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
673 case TIPC_CMD_SET_LINK_WINDOW:
674 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
680 static int tipc_nl_compat_media_set(struct sk_buff *skb,
681 struct tipc_nl_compat_msg *msg)
684 struct nlattr *media;
685 struct tipc_link_config *lc;
688 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
690 media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
694 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
695 if (!string_is_valid(lc->name, len))
698 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
701 prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
705 __tipc_add_link_prop(skb, msg, lc);
706 nla_nest_end(skb, prop);
707 nla_nest_end(skb, media);
712 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
713 struct tipc_nl_compat_msg *msg)
716 struct nlattr *bearer;
717 struct tipc_link_config *lc;
720 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
722 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
726 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
727 if (!string_is_valid(lc->name, len))
730 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
733 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
737 __tipc_add_link_prop(skb, msg, lc);
738 nla_nest_end(skb, prop);
739 nla_nest_end(skb, bearer);
744 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
745 struct tipc_nl_compat_msg *msg)
749 struct tipc_link_config *lc;
751 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
753 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
757 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
760 prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
764 __tipc_add_link_prop(skb, msg, lc);
765 nla_nest_end(skb, prop);
766 nla_nest_end(skb, link);
771 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
773 struct tipc_nl_compat_msg *msg)
775 struct tipc_link_config *lc;
776 struct tipc_bearer *bearer;
777 struct tipc_media *media;
780 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
782 len = TLV_GET_DATA_LEN(msg->req);
783 len -= offsetof(struct tipc_link_config, name);
787 len = min_t(int, len, TIPC_MAX_LINK_NAME);
788 if (!string_is_valid(lc->name, len))
791 media = tipc_media_find(lc->name);
793 cmd->doit = &__tipc_nl_media_set;
794 return tipc_nl_compat_media_set(skb, msg);
797 bearer = tipc_bearer_find(msg->net, lc->name);
799 cmd->doit = &__tipc_nl_bearer_set;
800 return tipc_nl_compat_bearer_set(skb, msg);
803 return __tipc_nl_compat_link_set(skb, msg);
806 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
808 struct tipc_nl_compat_msg *msg)
814 name = (char *)TLV_DATA(msg->req);
816 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
820 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
821 if (!string_is_valid(name, len))
824 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
827 nla_nest_end(skb, link);
832 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
836 struct tipc_name_table_query *ntq;
837 static const char * const header[] = {
844 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
845 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
848 depth = ntohl(ntq->depth);
852 for (i = 0; i < depth; i++)
853 tipc_tlv_sprintf(msg->rep, header[i]);
854 tipc_tlv_sprintf(msg->rep, "\n");
859 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
860 struct nlattr **attrs)
863 struct tipc_name_table_query *ntq;
864 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
865 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
866 u32 node, depth, type, lowbound, upbound;
867 static const char * const scope_str[] = {"", " zone", " cluster",
871 if (!attrs[TIPC_NLA_NAME_TABLE])
874 err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
875 attrs[TIPC_NLA_NAME_TABLE], NULL,
880 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
883 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
884 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
889 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
891 depth = ntohl(ntq->depth);
892 type = ntohl(ntq->type);
893 lowbound = ntohl(ntq->lowbound);
894 upbound = ntohl(ntq->upbound);
896 if (!(depth & TIPC_NTQ_ALLTYPES) &&
897 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
899 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
901 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
904 tipc_tlv_sprintf(msg->rep, "%-10u ",
905 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
910 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
911 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
912 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
917 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
918 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
919 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
920 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
925 tipc_tlv_sprintf(msg->rep, "%-10u %s",
926 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
927 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
929 tipc_tlv_sprintf(msg->rep, "\n");
934 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
935 struct nlattr **attrs)
937 u32 type, lower, upper;
938 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
941 if (!attrs[TIPC_NLA_PUBL])
944 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
945 attrs[TIPC_NLA_PUBL], NULL, NULL);
949 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
950 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
951 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
954 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
956 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
961 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
966 struct sk_buff *args;
967 struct tipc_nl_compat_cmd_dump dump;
969 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
973 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
980 nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
986 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
991 nla_nest_end(args, nest);
992 genlmsg_end(args, hdr);
994 dump.dumpit = tipc_nl_publ_dump;
995 dump.format = __tipc_nl_compat_publ_dump;
997 err = __tipc_nl_compat_dumpit(&dump, msg, args);
1004 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1005 struct nlattr **attrs)
1009 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1011 if (!attrs[TIPC_NLA_SOCK])
1014 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
1015 attrs[TIPC_NLA_SOCK], NULL, NULL);
1019 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1020 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1022 if (sock[TIPC_NLA_SOCK_CON]) {
1024 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1026 err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
1027 sock[TIPC_NLA_SOCK_CON],
1033 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1034 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1038 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1040 if (con[TIPC_NLA_CON_FLAG])
1041 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1042 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1043 nla_get_u32(con[TIPC_NLA_CON_INST]));
1045 tipc_tlv_sprintf(msg->rep, "\n");
1046 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1047 tipc_tlv_sprintf(msg->rep, " bound to");
1049 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1053 tipc_tlv_sprintf(msg->rep, "\n");
1058 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1059 struct nlattr **attrs)
1061 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1064 if (!attrs[TIPC_NLA_MEDIA])
1067 err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1068 attrs[TIPC_NLA_MEDIA], NULL, NULL);
1072 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1073 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1074 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1077 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1078 struct nlattr **attrs)
1080 struct tipc_node_info node_info;
1081 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1084 if (!attrs[TIPC_NLA_NODE])
1087 err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
1088 attrs[TIPC_NLA_NODE], NULL, NULL);
1092 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1093 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1095 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1099 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1100 struct sk_buff *skb,
1101 struct tipc_nl_compat_msg *msg)
1106 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1108 net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
1112 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1113 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1115 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1116 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1119 nla_nest_end(skb, net);
1124 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1125 struct nlattr **attrs)
1128 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1131 if (!attrs[TIPC_NLA_NET])
1134 err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
1135 attrs[TIPC_NLA_NET], NULL, NULL);
1139 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1141 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1144 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1146 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1150 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1151 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1156 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1158 struct tipc_nl_compat_cmd_dump dump;
1159 struct tipc_nl_compat_cmd_doit doit;
1161 memset(&dump, 0, sizeof(dump));
1162 memset(&doit, 0, sizeof(doit));
1166 msg->rep = tipc_tlv_alloc(0);
1170 case TIPC_CMD_GET_BEARER_NAMES:
1171 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1172 dump.dumpit = tipc_nl_bearer_dump;
1173 dump.format = tipc_nl_compat_bearer_dump;
1174 return tipc_nl_compat_dumpit(&dump, msg);
1175 case TIPC_CMD_ENABLE_BEARER:
1176 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1177 doit.doit = __tipc_nl_bearer_enable;
1178 doit.transcode = tipc_nl_compat_bearer_enable;
1179 return tipc_nl_compat_doit(&doit, msg);
1180 case TIPC_CMD_DISABLE_BEARER:
1181 msg->req_type = TIPC_TLV_BEARER_NAME;
1182 doit.doit = __tipc_nl_bearer_disable;
1183 doit.transcode = tipc_nl_compat_bearer_disable;
1184 return tipc_nl_compat_doit(&doit, msg);
1185 case TIPC_CMD_SHOW_LINK_STATS:
1186 msg->req_type = TIPC_TLV_LINK_NAME;
1187 msg->rep_size = ULTRA_STRING_MAX_LEN;
1188 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1189 dump.dumpit = tipc_nl_node_dump_link;
1190 dump.format = tipc_nl_compat_link_stat_dump;
1191 return tipc_nl_compat_dumpit(&dump, msg);
1192 case TIPC_CMD_GET_LINKS:
1193 msg->req_type = TIPC_TLV_NET_ADDR;
1194 msg->rep_size = ULTRA_STRING_MAX_LEN;
1195 dump.dumpit = tipc_nl_node_dump_link;
1196 dump.format = tipc_nl_compat_link_dump;
1197 return tipc_nl_compat_dumpit(&dump, msg);
1198 case TIPC_CMD_SET_LINK_TOL:
1199 case TIPC_CMD_SET_LINK_PRI:
1200 case TIPC_CMD_SET_LINK_WINDOW:
1201 msg->req_type = TIPC_TLV_LINK_CONFIG;
1202 doit.doit = tipc_nl_node_set_link;
1203 doit.transcode = tipc_nl_compat_link_set;
1204 return tipc_nl_compat_doit(&doit, msg);
1205 case TIPC_CMD_RESET_LINK_STATS:
1206 msg->req_type = TIPC_TLV_LINK_NAME;
1207 doit.doit = tipc_nl_node_reset_link_stats;
1208 doit.transcode = tipc_nl_compat_link_reset_stats;
1209 return tipc_nl_compat_doit(&doit, msg);
1210 case TIPC_CMD_SHOW_NAME_TABLE:
1211 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1212 msg->rep_size = ULTRA_STRING_MAX_LEN;
1213 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1214 dump.header = tipc_nl_compat_name_table_dump_header;
1215 dump.dumpit = tipc_nl_name_table_dump;
1216 dump.format = tipc_nl_compat_name_table_dump;
1217 return tipc_nl_compat_dumpit(&dump, msg);
1218 case TIPC_CMD_SHOW_PORTS:
1219 msg->rep_size = ULTRA_STRING_MAX_LEN;
1220 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1221 dump.dumpit = tipc_nl_sk_dump;
1222 dump.format = tipc_nl_compat_sk_dump;
1223 return tipc_nl_compat_dumpit(&dump, msg);
1224 case TIPC_CMD_GET_MEDIA_NAMES:
1225 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1226 dump.dumpit = tipc_nl_media_dump;
1227 dump.format = tipc_nl_compat_media_dump;
1228 return tipc_nl_compat_dumpit(&dump, msg);
1229 case TIPC_CMD_GET_NODES:
1230 msg->rep_size = ULTRA_STRING_MAX_LEN;
1231 dump.dumpit = tipc_nl_node_dump;
1232 dump.format = tipc_nl_compat_node_dump;
1233 return tipc_nl_compat_dumpit(&dump, msg);
1234 case TIPC_CMD_SET_NODE_ADDR:
1235 msg->req_type = TIPC_TLV_NET_ADDR;
1236 doit.doit = __tipc_nl_net_set;
1237 doit.transcode = tipc_nl_compat_net_set;
1238 return tipc_nl_compat_doit(&doit, msg);
1239 case TIPC_CMD_SET_NETID:
1240 msg->req_type = TIPC_TLV_UNSIGNED;
1241 doit.doit = __tipc_nl_net_set;
1242 doit.transcode = tipc_nl_compat_net_set;
1243 return tipc_nl_compat_doit(&doit, msg);
1244 case TIPC_CMD_GET_NETID:
1245 msg->rep_size = sizeof(u32);
1246 dump.dumpit = tipc_nl_net_dump;
1247 dump.format = tipc_nl_compat_net_dump;
1248 return tipc_nl_compat_dumpit(&dump, msg);
1249 case TIPC_CMD_SHOW_STATS:
1250 return tipc_cmd_show_stats_compat(msg);
1256 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1260 struct tipc_nl_compat_msg msg;
1261 struct nlmsghdr *req_nlh;
1262 struct nlmsghdr *rep_nlh;
1263 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1265 memset(&msg, 0, sizeof(msg));
1267 req_nlh = (struct nlmsghdr *)skb->data;
1268 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1269 msg.cmd = req_userhdr->cmd;
1270 msg.net = genl_info_net(info);
1271 msg.dst_sk = skb->sk;
1273 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1274 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1279 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1280 if (!len || !TLV_OK(msg.req, len)) {
1281 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1286 err = tipc_nl_compat_handle(&msg);
1287 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1288 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1289 else if (err == -EINVAL)
1290 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1295 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1296 skb_push(msg.rep, len);
1297 rep_nlh = nlmsg_hdr(msg.rep);
1298 memcpy(rep_nlh, info->nlhdr, len);
1299 rep_nlh->nlmsg_len = msg.rep->len;
1300 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1305 static const struct genl_ops tipc_genl_compat_ops[] = {
1307 .cmd = TIPC_GENL_CMD,
1308 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1309 .doit = tipc_nl_compat_recv,
1313 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1314 .name = TIPC_GENL_NAME,
1315 .version = TIPC_GENL_VERSION,
1316 .hdrsize = TIPC_GENL_HDRLEN,
1319 .module = THIS_MODULE,
1320 .ops = tipc_genl_compat_ops,
1321 .n_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1324 int __init tipc_netlink_compat_start(void)
1328 res = genl_register_family(&tipc_genl_compat_family);
1330 pr_err("Failed to register legacy compat interface\n");
1337 void tipc_netlink_compat_stop(void)
1339 genl_unregister_family(&tipc_genl_compat_family);