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;
203 len = (*cmd->dumpit)(buf, &cb);
205 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
206 struct nlattr **attrs;
208 err = tipc_nlmsg_parse(nlmsg, &attrs);
212 err = (*cmd->format)(msg, attrs);
216 if (tipc_skb_tailroom(msg->rep) <= 1) {
222 skb_reset_tail_pointer(buf);
232 if (err == -EMSGSIZE) {
233 /* The legacy API only considered messages filling
234 * "ULTRA_STRING_MAX_LEN" to be truncated.
236 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
237 char *tail = skb_tail_pointer(msg->rep);
240 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
250 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
251 struct tipc_nl_compat_msg *msg)
253 struct nlmsghdr *nlh;
257 if (msg->req_type && (!msg->req_size ||
258 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
261 msg->rep = tipc_tlv_alloc(msg->rep_size);
266 tipc_tlv_init(msg->rep, msg->rep_type);
269 err = (*cmd->header)(msg);
277 arg = nlmsg_new(0, GFP_KERNEL);
284 nlh = nlmsg_put(arg, 0, 0, tipc_genl_family.id, 0, NLM_F_MULTI);
293 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
303 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
304 struct tipc_nl_compat_msg *msg)
307 struct sk_buff *doit_buf;
308 struct sk_buff *trans_buf;
309 struct nlattr **attrbuf;
310 struct genl_info info;
312 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
316 err = (*cmd->transcode)(cmd, trans_buf, msg);
320 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
321 sizeof(struct nlattr *), GFP_KERNEL);
327 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
328 (const struct nlattr *)trans_buf->data,
329 trans_buf->len, NULL);
333 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
339 doit_buf->sk = msg->dst_sk;
341 memset(&info, 0, sizeof(info));
342 info.attrs = attrbuf;
344 err = (*cmd->doit)(doit_buf, &info);
350 kfree_skb(trans_buf);
355 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
356 struct tipc_nl_compat_msg *msg)
360 if (msg->req_type && (!msg->req_size ||
361 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
364 err = __tipc_nl_compat_doit(cmd, msg);
368 /* The legacy API considered an empty message a success message */
369 msg->rep = tipc_tlv_alloc(0);
376 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
377 struct nlattr **attrs)
379 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
382 if (!attrs[TIPC_NLA_BEARER])
385 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
386 attrs[TIPC_NLA_BEARER], NULL);
390 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
391 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
392 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
395 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
397 struct tipc_nl_compat_msg *msg)
400 struct nlattr *bearer;
401 struct tipc_bearer_config *b;
404 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
406 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
410 len = TLV_GET_DATA_LEN(msg->req);
411 len -= offsetof(struct tipc_bearer_config, name);
415 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
416 if (!string_is_valid(b->name, len))
419 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
422 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
425 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
426 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
429 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
431 nla_nest_end(skb, prop);
433 nla_nest_end(skb, bearer);
438 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
440 struct tipc_nl_compat_msg *msg)
443 struct nlattr *bearer;
446 name = (char *)TLV_DATA(msg->req);
448 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
452 len = TLV_GET_DATA_LEN(msg->req);
456 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
457 if (!string_is_valid(name, len))
460 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
463 nla_nest_end(skb, bearer);
468 static inline u32 perc(u32 count, u32 total)
470 return (count * 100 + (total / 2)) / total;
473 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
474 struct nlattr *prop[], struct nlattr *stats[])
476 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
477 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
479 tipc_tlv_sprintf(msg->rep,
480 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
481 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
482 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
483 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
484 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
485 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
487 tipc_tlv_sprintf(msg->rep,
488 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
489 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
490 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
491 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
492 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
493 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
495 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
496 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
497 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
498 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
500 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
501 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
502 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
503 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
505 tipc_tlv_sprintf(msg->rep,
506 " Congestion link:%u Send queue max:%u avg:%u",
507 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
508 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
509 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
512 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
513 struct nlattr **attrs)
516 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
517 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
518 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
522 if (!attrs[TIPC_NLA_LINK])
525 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
530 if (!link[TIPC_NLA_LINK_PROP])
533 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
534 link[TIPC_NLA_LINK_PROP], NULL);
538 if (!link[TIPC_NLA_LINK_STATS])
541 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
542 link[TIPC_NLA_LINK_STATS], NULL);
546 name = (char *)TLV_DATA(msg->req);
548 len = TLV_GET_DATA_LEN(msg->req);
552 len = min_t(int, len, TIPC_MAX_LINK_NAME);
553 if (!string_is_valid(name, len))
556 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
559 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
560 nla_data(link[TIPC_NLA_LINK_NAME]));
562 if (link[TIPC_NLA_LINK_BROADCAST]) {
563 __fill_bc_link_stat(msg, prop, stats);
567 if (link[TIPC_NLA_LINK_ACTIVE])
568 tipc_tlv_sprintf(msg->rep, " ACTIVE");
569 else if (link[TIPC_NLA_LINK_UP])
570 tipc_tlv_sprintf(msg->rep, " STANDBY");
572 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
574 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
575 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
576 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
578 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
579 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
580 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
582 tipc_tlv_sprintf(msg->rep,
583 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
584 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
585 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
586 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
587 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
588 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
589 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
591 tipc_tlv_sprintf(msg->rep,
592 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
593 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
594 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
595 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
596 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
597 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
598 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
600 tipc_tlv_sprintf(msg->rep,
601 " TX profile sample:%u packets average:%u octets\n",
602 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
603 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
604 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
606 tipc_tlv_sprintf(msg->rep,
607 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
608 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
609 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
610 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
611 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
612 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
613 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
614 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
615 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
617 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
618 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
619 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
620 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
621 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
622 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
623 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
625 tipc_tlv_sprintf(msg->rep,
626 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
627 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
628 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
629 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
630 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
631 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
633 tipc_tlv_sprintf(msg->rep,
634 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
635 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
636 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
637 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
638 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
639 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
641 tipc_tlv_sprintf(msg->rep,
642 " Congestion link:%u Send queue max:%u avg:%u",
643 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
644 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
645 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
650 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
651 struct nlattr **attrs)
653 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
654 struct tipc_link_info link_info;
657 if (!attrs[TIPC_NLA_LINK])
660 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
665 link_info.dest = htonl(nla_get_flag(link[TIPC_NLA_LINK_DEST]));
666 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
667 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
670 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
671 &link_info, sizeof(link_info));
674 static int __tipc_add_link_prop(struct sk_buff *skb,
675 struct tipc_nl_compat_msg *msg,
676 struct tipc_link_config *lc)
679 case TIPC_CMD_SET_LINK_PRI:
680 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
681 case TIPC_CMD_SET_LINK_TOL:
682 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
683 case TIPC_CMD_SET_LINK_WINDOW:
684 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
690 static int tipc_nl_compat_media_set(struct sk_buff *skb,
691 struct tipc_nl_compat_msg *msg)
694 struct nlattr *media;
695 struct tipc_link_config *lc;
698 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
700 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
704 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
705 if (!string_is_valid(lc->name, len))
708 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
711 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
715 __tipc_add_link_prop(skb, msg, lc);
716 nla_nest_end(skb, prop);
717 nla_nest_end(skb, media);
722 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
723 struct tipc_nl_compat_msg *msg)
726 struct nlattr *bearer;
727 struct tipc_link_config *lc;
730 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
732 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
736 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
737 if (!string_is_valid(lc->name, len))
740 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
743 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
747 __tipc_add_link_prop(skb, msg, lc);
748 nla_nest_end(skb, prop);
749 nla_nest_end(skb, bearer);
754 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
755 struct tipc_nl_compat_msg *msg)
759 struct tipc_link_config *lc;
761 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
763 link = nla_nest_start(skb, TIPC_NLA_LINK);
767 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
770 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
774 __tipc_add_link_prop(skb, msg, lc);
775 nla_nest_end(skb, prop);
776 nla_nest_end(skb, link);
781 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
783 struct tipc_nl_compat_msg *msg)
785 struct tipc_link_config *lc;
786 struct tipc_bearer *bearer;
787 struct tipc_media *media;
790 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
792 len = TLV_GET_DATA_LEN(msg->req);
793 len -= offsetof(struct tipc_link_config, name);
797 len = min_t(int, len, TIPC_MAX_LINK_NAME);
798 if (!string_is_valid(lc->name, len))
801 media = tipc_media_find(lc->name);
803 cmd->doit = &tipc_nl_media_set;
804 return tipc_nl_compat_media_set(skb, msg);
807 bearer = tipc_bearer_find(msg->net, lc->name);
809 cmd->doit = &tipc_nl_bearer_set;
810 return tipc_nl_compat_bearer_set(skb, msg);
813 return __tipc_nl_compat_link_set(skb, msg);
816 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
818 struct tipc_nl_compat_msg *msg)
824 name = (char *)TLV_DATA(msg->req);
826 link = nla_nest_start(skb, TIPC_NLA_LINK);
830 len = TLV_GET_DATA_LEN(msg->req);
834 len = min_t(int, len, TIPC_MAX_LINK_NAME);
835 if (!string_is_valid(name, len))
838 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
841 nla_nest_end(skb, link);
846 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
850 struct tipc_name_table_query *ntq;
851 static const char * const header[] = {
858 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
859 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
862 depth = ntohl(ntq->depth);
866 for (i = 0; i < depth; i++)
867 tipc_tlv_sprintf(msg->rep, header[i]);
868 tipc_tlv_sprintf(msg->rep, "\n");
873 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
874 struct nlattr **attrs)
877 struct tipc_name_table_query *ntq;
878 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
879 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
880 u32 node, depth, type, lowbound, upbound;
881 static const char * const scope_str[] = {"", " zone", " cluster",
885 if (!attrs[TIPC_NLA_NAME_TABLE])
888 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
889 attrs[TIPC_NLA_NAME_TABLE], NULL);
893 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
896 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
897 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
901 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
903 depth = ntohl(ntq->depth);
904 type = ntohl(ntq->type);
905 lowbound = ntohl(ntq->lowbound);
906 upbound = ntohl(ntq->upbound);
908 if (!(depth & TIPC_NTQ_ALLTYPES) &&
909 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
911 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
913 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
916 tipc_tlv_sprintf(msg->rep, "%-10u ",
917 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
922 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
923 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
924 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
929 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
930 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
931 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
932 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
937 tipc_tlv_sprintf(msg->rep, "%-10u %s",
938 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
939 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
941 tipc_tlv_sprintf(msg->rep, "\n");
946 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
947 struct nlattr **attrs)
949 u32 type, lower, upper;
950 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
953 if (!attrs[TIPC_NLA_PUBL])
956 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
961 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
962 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
963 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
966 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
968 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
973 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
978 struct sk_buff *args;
979 struct tipc_nl_compat_cmd_dump dump;
981 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
985 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
992 nest = nla_nest_start(args, TIPC_NLA_SOCK);
998 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
1003 nla_nest_end(args, nest);
1004 genlmsg_end(args, hdr);
1006 dump.dumpit = tipc_nl_publ_dump;
1007 dump.format = __tipc_nl_compat_publ_dump;
1009 err = __tipc_nl_compat_dumpit(&dump, msg, args);
1016 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1017 struct nlattr **attrs)
1021 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1023 if (!attrs[TIPC_NLA_SOCK])
1026 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
1031 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1032 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1034 if (sock[TIPC_NLA_SOCK_CON]) {
1036 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1038 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
1041 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1042 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1046 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1048 if (con[TIPC_NLA_CON_FLAG])
1049 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1050 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1051 nla_get_u32(con[TIPC_NLA_CON_INST]));
1053 tipc_tlv_sprintf(msg->rep, "\n");
1054 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1055 tipc_tlv_sprintf(msg->rep, " bound to");
1057 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1061 tipc_tlv_sprintf(msg->rep, "\n");
1066 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1067 struct nlattr **attrs)
1069 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1072 if (!attrs[TIPC_NLA_MEDIA])
1075 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
1080 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1081 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1082 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1085 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1086 struct nlattr **attrs)
1088 struct tipc_node_info node_info;
1089 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1092 if (!attrs[TIPC_NLA_NODE])
1095 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1100 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1101 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1103 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1107 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1108 struct sk_buff *skb,
1109 struct tipc_nl_compat_msg *msg)
1114 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1116 net = nla_nest_start(skb, TIPC_NLA_NET);
1120 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1121 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1123 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1124 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1127 nla_nest_end(skb, net);
1132 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1133 struct nlattr **attrs)
1136 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1139 if (!attrs[TIPC_NLA_NET])
1142 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1147 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1149 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1152 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1154 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1158 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1159 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1164 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1166 struct tipc_nl_compat_cmd_dump dump;
1167 struct tipc_nl_compat_cmd_doit doit;
1169 memset(&dump, 0, sizeof(dump));
1170 memset(&doit, 0, sizeof(doit));
1174 msg->rep = tipc_tlv_alloc(0);
1178 case TIPC_CMD_GET_BEARER_NAMES:
1179 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1180 dump.dumpit = tipc_nl_bearer_dump;
1181 dump.format = tipc_nl_compat_bearer_dump;
1182 return tipc_nl_compat_dumpit(&dump, msg);
1183 case TIPC_CMD_ENABLE_BEARER:
1184 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1185 doit.doit = tipc_nl_bearer_enable;
1186 doit.transcode = tipc_nl_compat_bearer_enable;
1187 return tipc_nl_compat_doit(&doit, msg);
1188 case TIPC_CMD_DISABLE_BEARER:
1189 msg->req_type = TIPC_TLV_BEARER_NAME;
1190 doit.doit = tipc_nl_bearer_disable;
1191 doit.transcode = tipc_nl_compat_bearer_disable;
1192 return tipc_nl_compat_doit(&doit, msg);
1193 case TIPC_CMD_SHOW_LINK_STATS:
1194 msg->req_type = TIPC_TLV_LINK_NAME;
1195 msg->rep_size = ULTRA_STRING_MAX_LEN;
1196 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1197 dump.dumpit = tipc_nl_node_dump_link;
1198 dump.format = tipc_nl_compat_link_stat_dump;
1199 return tipc_nl_compat_dumpit(&dump, msg);
1200 case TIPC_CMD_GET_LINKS:
1201 msg->req_type = TIPC_TLV_NET_ADDR;
1202 msg->rep_size = ULTRA_STRING_MAX_LEN;
1203 dump.dumpit = tipc_nl_node_dump_link;
1204 dump.format = tipc_nl_compat_link_dump;
1205 return tipc_nl_compat_dumpit(&dump, msg);
1206 case TIPC_CMD_SET_LINK_TOL:
1207 case TIPC_CMD_SET_LINK_PRI:
1208 case TIPC_CMD_SET_LINK_WINDOW:
1209 msg->req_type = TIPC_TLV_LINK_CONFIG;
1210 doit.doit = tipc_nl_node_set_link;
1211 doit.transcode = tipc_nl_compat_link_set;
1212 return tipc_nl_compat_doit(&doit, msg);
1213 case TIPC_CMD_RESET_LINK_STATS:
1214 msg->req_type = TIPC_TLV_LINK_NAME;
1215 doit.doit = tipc_nl_node_reset_link_stats;
1216 doit.transcode = tipc_nl_compat_link_reset_stats;
1217 return tipc_nl_compat_doit(&doit, msg);
1218 case TIPC_CMD_SHOW_NAME_TABLE:
1219 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1220 msg->rep_size = ULTRA_STRING_MAX_LEN;
1221 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1222 dump.header = tipc_nl_compat_name_table_dump_header;
1223 dump.dumpit = tipc_nl_name_table_dump;
1224 dump.format = tipc_nl_compat_name_table_dump;
1225 return tipc_nl_compat_dumpit(&dump, msg);
1226 case TIPC_CMD_SHOW_PORTS:
1227 msg->rep_size = ULTRA_STRING_MAX_LEN;
1228 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1229 dump.dumpit = tipc_nl_sk_dump;
1230 dump.format = tipc_nl_compat_sk_dump;
1231 return tipc_nl_compat_dumpit(&dump, msg);
1232 case TIPC_CMD_GET_MEDIA_NAMES:
1233 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1234 dump.dumpit = tipc_nl_media_dump;
1235 dump.format = tipc_nl_compat_media_dump;
1236 return tipc_nl_compat_dumpit(&dump, msg);
1237 case TIPC_CMD_GET_NODES:
1238 msg->rep_size = ULTRA_STRING_MAX_LEN;
1239 dump.dumpit = tipc_nl_node_dump;
1240 dump.format = tipc_nl_compat_node_dump;
1241 return tipc_nl_compat_dumpit(&dump, msg);
1242 case TIPC_CMD_SET_NODE_ADDR:
1243 msg->req_type = TIPC_TLV_NET_ADDR;
1244 doit.doit = tipc_nl_net_set;
1245 doit.transcode = tipc_nl_compat_net_set;
1246 return tipc_nl_compat_doit(&doit, msg);
1247 case TIPC_CMD_SET_NETID:
1248 msg->req_type = TIPC_TLV_UNSIGNED;
1249 doit.doit = tipc_nl_net_set;
1250 doit.transcode = tipc_nl_compat_net_set;
1251 return tipc_nl_compat_doit(&doit, msg);
1252 case TIPC_CMD_GET_NETID:
1253 msg->rep_size = sizeof(u32);
1254 dump.dumpit = tipc_nl_net_dump;
1255 dump.format = tipc_nl_compat_net_dump;
1256 return tipc_nl_compat_dumpit(&dump, msg);
1257 case TIPC_CMD_SHOW_STATS:
1258 return tipc_cmd_show_stats_compat(msg);
1264 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1268 struct tipc_nl_compat_msg msg;
1269 struct nlmsghdr *req_nlh;
1270 struct nlmsghdr *rep_nlh;
1271 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1273 memset(&msg, 0, sizeof(msg));
1275 req_nlh = (struct nlmsghdr *)skb->data;
1276 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1277 msg.cmd = req_userhdr->cmd;
1278 msg.net = genl_info_net(info);
1279 msg.dst_sk = skb->sk;
1281 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1282 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1287 msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1288 if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1289 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1294 err = tipc_nl_compat_handle(&msg);
1295 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1296 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1297 else if (err == -EINVAL)
1298 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1303 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1304 skb_push(msg.rep, len);
1305 rep_nlh = nlmsg_hdr(msg.rep);
1306 memcpy(rep_nlh, info->nlhdr, len);
1307 rep_nlh->nlmsg_len = msg.rep->len;
1308 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1313 static struct genl_family tipc_genl_compat_family = {
1314 .id = GENL_ID_GENERATE,
1315 .name = TIPC_GENL_NAME,
1316 .version = TIPC_GENL_VERSION,
1317 .hdrsize = TIPC_GENL_HDRLEN,
1322 static struct genl_ops tipc_genl_compat_ops[] = {
1324 .cmd = TIPC_GENL_CMD,
1325 .doit = tipc_nl_compat_recv,
1329 int tipc_netlink_compat_start(void)
1333 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1334 tipc_genl_compat_ops);
1336 pr_err("Failed to register legacy compat interface\n");
1343 void tipc_netlink_compat_stop(void)
1345 genl_unregister_family(&tipc_genl_compat_family);