2 * Copyright (c) 2014, Ericsson AB
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the names of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 #include "name_table.h"
41 #include <net/genetlink.h>
42 #include <linux/tipc_config.h>
44 /* The legacy API had an artificial message length limit called
45 * ULTRA_STRING_MAX_LEN.
47 #define ULTRA_STRING_MAX_LEN 32768
49 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
51 #define REPLY_TRUNCATED "<truncated>\n"
53 struct tipc_nl_compat_msg {
65 struct tipc_nl_compat_cmd_dump {
66 int (*header)(struct tipc_nl_compat_msg *);
67 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
68 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
71 struct tipc_nl_compat_cmd_doit {
72 int (*doit)(struct sk_buff *skb, struct genl_info *info);
73 int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
74 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
77 static int tipc_skb_tailroom(struct sk_buff *skb)
82 tailroom = skb_tailroom(skb);
83 limit = TIPC_SKB_MAX - skb->len;
91 static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
93 return TLV_GET_LEN(tlv) - TLV_SPACE(0);
96 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
98 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
100 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
103 skb_put(skb, TLV_SPACE(len));
104 tlv->tlv_type = htons(type);
105 tlv->tlv_len = htons(TLV_LENGTH(len));
107 memcpy(TLV_DATA(tlv), data, len);
112 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
114 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
117 TLV_SET_TYPE(tlv, type);
118 skb_put(skb, sizeof(struct tlv_desc));
121 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
127 struct tlv_desc *tlv;
130 rem = tipc_skb_tailroom(skb);
132 tlv = (struct tlv_desc *)skb->data;
133 len = TLV_GET_LEN(tlv);
134 buf = TLV_DATA(tlv) + len;
137 n = vscnprintf(buf, rem, fmt, args);
140 TLV_SET_LEN(tlv, n + len);
146 static struct sk_buff *tipc_tlv_alloc(int size)
151 size = TLV_SPACE(size);
152 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
154 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
158 skb_reserve(buf, hdr_len);
163 static struct sk_buff *tipc_get_err_tlv(char *str)
165 int str_len = strlen(str) + 1;
168 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
170 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
175 static inline bool string_is_valid(char *s, int len)
177 return memchr(s, '\0', len) ? true : false;
180 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
181 struct tipc_nl_compat_msg *msg,
187 struct nlmsghdr *nlmsg;
188 struct netlink_callback cb;
190 memset(&cb, 0, sizeof(cb));
191 cb.nlh = (struct nlmsghdr *)arg->data;
194 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
198 buf->sk = msg->dst_sk;
199 if (__tipc_dump_start(&cb, msg->net)) {
207 len = (*cmd->dumpit)(buf, &cb);
209 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
210 struct nlattr **attrs;
212 err = tipc_nlmsg_parse(nlmsg, &attrs);
216 err = (*cmd->format)(msg, attrs);
220 if (tipc_skb_tailroom(msg->rep) <= 1) {
226 skb_reset_tail_pointer(buf);
237 if (err == -EMSGSIZE) {
238 /* The legacy API only considered messages filling
239 * "ULTRA_STRING_MAX_LEN" to be truncated.
241 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
242 char *tail = skb_tail_pointer(msg->rep);
245 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
255 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
256 struct tipc_nl_compat_msg *msg)
261 if (msg->req_type && (!msg->req_size ||
262 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
265 msg->rep = tipc_tlv_alloc(msg->rep_size);
270 tipc_tlv_init(msg->rep, msg->rep_type);
273 err = (*cmd->header)(msg);
281 arg = nlmsg_new(0, GFP_KERNEL);
288 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
298 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
299 struct tipc_nl_compat_msg *msg)
302 struct sk_buff *doit_buf;
303 struct sk_buff *trans_buf;
304 struct nlattr **attrbuf;
305 struct genl_info info;
307 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
311 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
312 sizeof(struct nlattr *),
319 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
325 memset(&info, 0, sizeof(info));
326 info.attrs = attrbuf;
329 err = (*cmd->transcode)(cmd, trans_buf, msg);
333 err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
334 (const struct nlattr *)trans_buf->data,
335 trans_buf->len, NULL, NULL);
339 doit_buf->sk = msg->dst_sk;
341 err = (*cmd->doit)(doit_buf, &info);
349 kfree_skb(trans_buf);
354 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
355 struct tipc_nl_compat_msg *msg)
359 if (msg->req_type && (!msg->req_size ||
360 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
363 err = __tipc_nl_compat_doit(cmd, msg);
367 /* The legacy API considered an empty message a success message */
368 msg->rep = tipc_tlv_alloc(0);
375 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
376 struct nlattr **attrs)
378 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
381 if (!attrs[TIPC_NLA_BEARER])
384 err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
385 attrs[TIPC_NLA_BEARER], NULL, NULL);
389 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
390 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
391 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
394 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
396 struct tipc_nl_compat_msg *msg)
399 struct nlattr *bearer;
400 struct tipc_bearer_config *b;
403 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
405 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
409 len = TLV_GET_DATA_LEN(msg->req);
410 len -= offsetof(struct tipc_bearer_config, name);
414 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
415 if (!string_is_valid(b->name, len))
418 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
421 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
424 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
425 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
428 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
430 nla_nest_end(skb, prop);
432 nla_nest_end(skb, bearer);
437 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
439 struct tipc_nl_compat_msg *msg)
442 struct nlattr *bearer;
445 name = (char *)TLV_DATA(msg->req);
447 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
451 len = TLV_GET_DATA_LEN(msg->req);
455 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
456 if (!string_is_valid(name, len))
459 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
462 nla_nest_end(skb, bearer);
467 static inline u32 perc(u32 count, u32 total)
469 return (count * 100 + (total / 2)) / total;
472 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
473 struct nlattr *prop[], struct nlattr *stats[])
475 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
476 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
478 tipc_tlv_sprintf(msg->rep,
479 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
480 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
481 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
482 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
483 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
484 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
486 tipc_tlv_sprintf(msg->rep,
487 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
488 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
489 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
490 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
491 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
492 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
494 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
495 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
496 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
497 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
499 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
500 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
501 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
502 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
504 tipc_tlv_sprintf(msg->rep,
505 " Congestion link:%u Send queue max:%u avg:%u",
506 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
507 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
508 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
511 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
512 struct nlattr **attrs)
515 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
516 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
517 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
521 if (!attrs[TIPC_NLA_LINK])
524 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
525 attrs[TIPC_NLA_LINK], NULL, NULL);
529 if (!link[TIPC_NLA_LINK_PROP])
532 err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
533 link[TIPC_NLA_LINK_PROP], NULL,
538 if (!link[TIPC_NLA_LINK_STATS])
541 err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
542 link[TIPC_NLA_LINK_STATS], NULL,
547 name = (char *)TLV_DATA(msg->req);
549 len = TLV_GET_DATA_LEN(msg->req);
553 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
554 if (!string_is_valid(name, len))
557 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
560 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
561 nla_data(link[TIPC_NLA_LINK_NAME]));
563 if (link[TIPC_NLA_LINK_BROADCAST]) {
564 __fill_bc_link_stat(msg, prop, stats);
568 if (link[TIPC_NLA_LINK_ACTIVE])
569 tipc_tlv_sprintf(msg->rep, " ACTIVE");
570 else if (link[TIPC_NLA_LINK_UP])
571 tipc_tlv_sprintf(msg->rep, " STANDBY");
573 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
575 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
576 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
577 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
579 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
580 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
581 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
583 tipc_tlv_sprintf(msg->rep,
584 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
585 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
586 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
587 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
588 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
589 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
590 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
592 tipc_tlv_sprintf(msg->rep,
593 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
594 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
595 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
596 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
597 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
598 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
599 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
601 tipc_tlv_sprintf(msg->rep,
602 " TX profile sample:%u packets average:%u octets\n",
603 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
604 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
605 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
607 tipc_tlv_sprintf(msg->rep,
608 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
609 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
610 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
611 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
612 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
613 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
614 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
615 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
616 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
618 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
619 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
620 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
621 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
622 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
623 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
624 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
626 tipc_tlv_sprintf(msg->rep,
627 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
628 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
629 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
630 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
631 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
632 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
634 tipc_tlv_sprintf(msg->rep,
635 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
636 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
637 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
638 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
639 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
640 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
642 tipc_tlv_sprintf(msg->rep,
643 " Congestion link:%u Send queue max:%u avg:%u",
644 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
645 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
646 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
651 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
652 struct nlattr **attrs)
654 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
655 struct tipc_link_info link_info;
658 if (!attrs[TIPC_NLA_LINK])
661 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
662 attrs[TIPC_NLA_LINK], NULL, NULL);
666 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
667 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
668 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
671 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
672 &link_info, sizeof(link_info));
675 static int __tipc_add_link_prop(struct sk_buff *skb,
676 struct tipc_nl_compat_msg *msg,
677 struct tipc_link_config *lc)
680 case TIPC_CMD_SET_LINK_PRI:
681 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
682 case TIPC_CMD_SET_LINK_TOL:
683 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
684 case TIPC_CMD_SET_LINK_WINDOW:
685 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
691 static int tipc_nl_compat_media_set(struct sk_buff *skb,
692 struct tipc_nl_compat_msg *msg)
695 struct nlattr *media;
696 struct tipc_link_config *lc;
698 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
700 media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
704 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
707 prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
711 __tipc_add_link_prop(skb, msg, lc);
712 nla_nest_end(skb, prop);
713 nla_nest_end(skb, media);
718 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
719 struct tipc_nl_compat_msg *msg)
722 struct nlattr *bearer;
723 struct tipc_link_config *lc;
725 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
727 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
731 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
734 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
738 __tipc_add_link_prop(skb, msg, lc);
739 nla_nest_end(skb, prop);
740 nla_nest_end(skb, bearer);
745 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
746 struct tipc_nl_compat_msg *msg)
750 struct tipc_link_config *lc;
752 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
754 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
758 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
761 prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
765 __tipc_add_link_prop(skb, msg, lc);
766 nla_nest_end(skb, prop);
767 nla_nest_end(skb, link);
772 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
774 struct tipc_nl_compat_msg *msg)
776 struct tipc_link_config *lc;
777 struct tipc_bearer *bearer;
778 struct tipc_media *media;
781 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
783 len = TLV_GET_DATA_LEN(msg->req);
784 len -= offsetof(struct tipc_link_config, name);
788 len = min_t(int, len, TIPC_MAX_LINK_NAME);
789 if (!string_is_valid(lc->name, len))
792 media = tipc_media_find(lc->name);
794 cmd->doit = &__tipc_nl_media_set;
795 return tipc_nl_compat_media_set(skb, msg);
798 bearer = tipc_bearer_find(msg->net, lc->name);
800 cmd->doit = &__tipc_nl_bearer_set;
801 return tipc_nl_compat_bearer_set(skb, msg);
804 return __tipc_nl_compat_link_set(skb, msg);
807 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
809 struct tipc_nl_compat_msg *msg)
815 name = (char *)TLV_DATA(msg->req);
817 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
821 len = TLV_GET_DATA_LEN(msg->req);
825 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
826 if (!string_is_valid(name, len))
829 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
832 nla_nest_end(skb, link);
837 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
841 struct tipc_name_table_query *ntq;
842 static const char * const header[] = {
849 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
850 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
853 depth = ntohl(ntq->depth);
857 for (i = 0; i < depth; i++)
858 tipc_tlv_sprintf(msg->rep, header[i]);
859 tipc_tlv_sprintf(msg->rep, "\n");
864 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
865 struct nlattr **attrs)
868 struct tipc_name_table_query *ntq;
869 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
870 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
871 u32 node, depth, type, lowbound, upbound;
872 static const char * const scope_str[] = {"", " zone", " cluster",
876 if (!attrs[TIPC_NLA_NAME_TABLE])
879 err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
880 attrs[TIPC_NLA_NAME_TABLE], NULL,
885 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
888 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
889 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
894 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
896 depth = ntohl(ntq->depth);
897 type = ntohl(ntq->type);
898 lowbound = ntohl(ntq->lowbound);
899 upbound = ntohl(ntq->upbound);
901 if (!(depth & TIPC_NTQ_ALLTYPES) &&
902 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
904 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
906 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
909 tipc_tlv_sprintf(msg->rep, "%-10u ",
910 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
915 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
916 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
917 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
922 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
923 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
924 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
925 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
930 tipc_tlv_sprintf(msg->rep, "%-10u %s",
931 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
932 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
934 tipc_tlv_sprintf(msg->rep, "\n");
939 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
940 struct nlattr **attrs)
942 u32 type, lower, upper;
943 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
946 if (!attrs[TIPC_NLA_PUBL])
949 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
950 attrs[TIPC_NLA_PUBL], NULL, NULL);
954 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
955 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
956 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
959 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
961 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
966 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
971 struct sk_buff *args;
972 struct tipc_nl_compat_cmd_dump dump;
974 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
978 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
985 nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
991 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
996 nla_nest_end(args, nest);
997 genlmsg_end(args, hdr);
999 dump.dumpit = tipc_nl_publ_dump;
1000 dump.format = __tipc_nl_compat_publ_dump;
1002 err = __tipc_nl_compat_dumpit(&dump, msg, args);
1009 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1010 struct nlattr **attrs)
1014 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1016 if (!attrs[TIPC_NLA_SOCK])
1019 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
1020 attrs[TIPC_NLA_SOCK], NULL, NULL);
1024 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1025 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1027 if (sock[TIPC_NLA_SOCK_CON]) {
1029 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1031 err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
1032 sock[TIPC_NLA_SOCK_CON],
1038 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1039 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1043 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1045 if (con[TIPC_NLA_CON_FLAG])
1046 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1047 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1048 nla_get_u32(con[TIPC_NLA_CON_INST]));
1050 tipc_tlv_sprintf(msg->rep, "\n");
1051 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1052 tipc_tlv_sprintf(msg->rep, " bound to");
1054 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1058 tipc_tlv_sprintf(msg->rep, "\n");
1063 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1064 struct nlattr **attrs)
1066 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1069 if (!attrs[TIPC_NLA_MEDIA])
1072 err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1073 attrs[TIPC_NLA_MEDIA], NULL, NULL);
1077 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1078 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1079 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1082 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1083 struct nlattr **attrs)
1085 struct tipc_node_info node_info;
1086 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1089 if (!attrs[TIPC_NLA_NODE])
1092 err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
1093 attrs[TIPC_NLA_NODE], NULL, NULL);
1097 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1098 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1100 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1104 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1105 struct sk_buff *skb,
1106 struct tipc_nl_compat_msg *msg)
1111 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1113 net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
1117 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1118 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1120 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1121 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1124 nla_nest_end(skb, net);
1129 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1130 struct nlattr **attrs)
1133 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1136 if (!attrs[TIPC_NLA_NET])
1139 err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
1140 attrs[TIPC_NLA_NET], NULL, NULL);
1144 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1146 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1149 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1151 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1155 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1156 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1161 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1163 struct tipc_nl_compat_cmd_dump dump;
1164 struct tipc_nl_compat_cmd_doit doit;
1166 memset(&dump, 0, sizeof(dump));
1167 memset(&doit, 0, sizeof(doit));
1171 msg->rep = tipc_tlv_alloc(0);
1175 case TIPC_CMD_GET_BEARER_NAMES:
1176 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1177 dump.dumpit = tipc_nl_bearer_dump;
1178 dump.format = tipc_nl_compat_bearer_dump;
1179 return tipc_nl_compat_dumpit(&dump, msg);
1180 case TIPC_CMD_ENABLE_BEARER:
1181 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1182 doit.doit = __tipc_nl_bearer_enable;
1183 doit.transcode = tipc_nl_compat_bearer_enable;
1184 return tipc_nl_compat_doit(&doit, msg);
1185 case TIPC_CMD_DISABLE_BEARER:
1186 msg->req_type = TIPC_TLV_BEARER_NAME;
1187 doit.doit = __tipc_nl_bearer_disable;
1188 doit.transcode = tipc_nl_compat_bearer_disable;
1189 return tipc_nl_compat_doit(&doit, msg);
1190 case TIPC_CMD_SHOW_LINK_STATS:
1191 msg->req_type = TIPC_TLV_LINK_NAME;
1192 msg->rep_size = ULTRA_STRING_MAX_LEN;
1193 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1194 dump.dumpit = tipc_nl_node_dump_link;
1195 dump.format = tipc_nl_compat_link_stat_dump;
1196 return tipc_nl_compat_dumpit(&dump, msg);
1197 case TIPC_CMD_GET_LINKS:
1198 msg->req_type = TIPC_TLV_NET_ADDR;
1199 msg->rep_size = ULTRA_STRING_MAX_LEN;
1200 dump.dumpit = tipc_nl_node_dump_link;
1201 dump.format = tipc_nl_compat_link_dump;
1202 return tipc_nl_compat_dumpit(&dump, msg);
1203 case TIPC_CMD_SET_LINK_TOL:
1204 case TIPC_CMD_SET_LINK_PRI:
1205 case TIPC_CMD_SET_LINK_WINDOW:
1206 msg->req_type = TIPC_TLV_LINK_CONFIG;
1207 doit.doit = tipc_nl_node_set_link;
1208 doit.transcode = tipc_nl_compat_link_set;
1209 return tipc_nl_compat_doit(&doit, msg);
1210 case TIPC_CMD_RESET_LINK_STATS:
1211 msg->req_type = TIPC_TLV_LINK_NAME;
1212 doit.doit = tipc_nl_node_reset_link_stats;
1213 doit.transcode = tipc_nl_compat_link_reset_stats;
1214 return tipc_nl_compat_doit(&doit, msg);
1215 case TIPC_CMD_SHOW_NAME_TABLE:
1216 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1217 msg->rep_size = ULTRA_STRING_MAX_LEN;
1218 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1219 dump.header = tipc_nl_compat_name_table_dump_header;
1220 dump.dumpit = tipc_nl_name_table_dump;
1221 dump.format = tipc_nl_compat_name_table_dump;
1222 return tipc_nl_compat_dumpit(&dump, msg);
1223 case TIPC_CMD_SHOW_PORTS:
1224 msg->rep_size = ULTRA_STRING_MAX_LEN;
1225 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1226 dump.dumpit = tipc_nl_sk_dump;
1227 dump.format = tipc_nl_compat_sk_dump;
1228 return tipc_nl_compat_dumpit(&dump, msg);
1229 case TIPC_CMD_GET_MEDIA_NAMES:
1230 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1231 dump.dumpit = tipc_nl_media_dump;
1232 dump.format = tipc_nl_compat_media_dump;
1233 return tipc_nl_compat_dumpit(&dump, msg);
1234 case TIPC_CMD_GET_NODES:
1235 msg->rep_size = ULTRA_STRING_MAX_LEN;
1236 dump.dumpit = tipc_nl_node_dump;
1237 dump.format = tipc_nl_compat_node_dump;
1238 return tipc_nl_compat_dumpit(&dump, msg);
1239 case TIPC_CMD_SET_NODE_ADDR:
1240 msg->req_type = TIPC_TLV_NET_ADDR;
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_SET_NETID:
1245 msg->req_type = TIPC_TLV_UNSIGNED;
1246 doit.doit = __tipc_nl_net_set;
1247 doit.transcode = tipc_nl_compat_net_set;
1248 return tipc_nl_compat_doit(&doit, msg);
1249 case TIPC_CMD_GET_NETID:
1250 msg->rep_size = sizeof(u32);
1251 dump.dumpit = tipc_nl_net_dump;
1252 dump.format = tipc_nl_compat_net_dump;
1253 return tipc_nl_compat_dumpit(&dump, msg);
1254 case TIPC_CMD_SHOW_STATS:
1255 return tipc_cmd_show_stats_compat(msg);
1261 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1265 struct tipc_nl_compat_msg msg;
1266 struct nlmsghdr *req_nlh;
1267 struct nlmsghdr *rep_nlh;
1268 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1270 memset(&msg, 0, sizeof(msg));
1272 req_nlh = (struct nlmsghdr *)skb->data;
1273 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1274 msg.cmd = req_userhdr->cmd;
1275 msg.net = genl_info_net(info);
1276 msg.dst_sk = skb->sk;
1278 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1279 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1284 msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1285 if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1286 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1291 err = tipc_nl_compat_handle(&msg);
1292 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1293 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1294 else if (err == -EINVAL)
1295 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1300 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1301 skb_push(msg.rep, len);
1302 rep_nlh = nlmsg_hdr(msg.rep);
1303 memcpy(rep_nlh, info->nlhdr, len);
1304 rep_nlh->nlmsg_len = msg.rep->len;
1305 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1310 static const struct genl_ops tipc_genl_compat_ops[] = {
1312 .cmd = TIPC_GENL_CMD,
1313 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1314 .doit = tipc_nl_compat_recv,
1318 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1319 .name = TIPC_GENL_NAME,
1320 .version = TIPC_GENL_VERSION,
1321 .hdrsize = TIPC_GENL_HDRLEN,
1324 .module = THIS_MODULE,
1325 .ops = tipc_genl_compat_ops,
1326 .n_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1329 int __init tipc_netlink_compat_start(void)
1333 res = genl_register_family(&tipc_genl_compat_family);
1335 pr_err("Failed to register legacy compat interface\n");
1342 void tipc_netlink_compat_stop(void)
1344 genl_unregister_family(&tipc_genl_compat_family);