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 int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
92 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
94 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
97 skb_put(skb, TLV_SPACE(len));
98 tlv->tlv_type = htons(type);
99 tlv->tlv_len = htons(TLV_LENGTH(len));
101 memcpy(TLV_DATA(tlv), data, len);
106 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
108 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
111 TLV_SET_TYPE(tlv, type);
112 skb_put(skb, sizeof(struct tlv_desc));
115 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
121 struct tlv_desc *tlv;
124 rem = tipc_skb_tailroom(skb);
126 tlv = (struct tlv_desc *)skb->data;
127 len = TLV_GET_LEN(tlv);
128 buf = TLV_DATA(tlv) + len;
131 n = vscnprintf(buf, rem, fmt, args);
134 TLV_SET_LEN(tlv, n + len);
140 static struct sk_buff *tipc_tlv_alloc(int size)
145 size = TLV_SPACE(size);
146 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
148 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
152 skb_reserve(buf, hdr_len);
157 static struct sk_buff *tipc_get_err_tlv(char *str)
159 int str_len = strlen(str) + 1;
162 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
164 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
169 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
170 struct tipc_nl_compat_msg *msg,
176 struct nlmsghdr *nlmsg;
177 struct netlink_callback cb;
179 memset(&cb, 0, sizeof(cb));
180 cb.nlh = (struct nlmsghdr *)arg->data;
183 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
187 buf->sk = msg->dst_sk;
192 len = (*cmd->dumpit)(buf, &cb);
194 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
195 struct nlattr **attrs;
197 err = tipc_nlmsg_parse(nlmsg, &attrs);
201 err = (*cmd->format)(msg, attrs);
205 if (tipc_skb_tailroom(msg->rep) <= 1) {
211 skb_reset_tail_pointer(buf);
221 if (err == -EMSGSIZE) {
222 /* The legacy API only considered messages filling
223 * "ULTRA_STRING_MAX_LEN" to be truncated.
225 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
226 char *tail = skb_tail_pointer(msg->rep);
229 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
239 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
240 struct tipc_nl_compat_msg *msg)
245 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
248 msg->rep = tipc_tlv_alloc(msg->rep_size);
253 tipc_tlv_init(msg->rep, msg->rep_type);
258 arg = nlmsg_new(0, GFP_KERNEL);
264 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
273 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
274 struct tipc_nl_compat_msg *msg)
277 struct sk_buff *doit_buf;
278 struct sk_buff *trans_buf;
279 struct nlattr **attrbuf;
280 struct genl_info info;
282 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
286 err = (*cmd->transcode)(cmd, trans_buf, msg);
290 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
291 sizeof(struct nlattr *), GFP_KERNEL);
297 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
298 (const struct nlattr *)trans_buf->data,
299 trans_buf->len, NULL);
303 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
309 doit_buf->sk = msg->dst_sk;
311 memset(&info, 0, sizeof(info));
312 info.attrs = attrbuf;
314 err = (*cmd->doit)(doit_buf, &info);
320 kfree_skb(trans_buf);
325 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
326 struct tipc_nl_compat_msg *msg)
330 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
333 err = __tipc_nl_compat_doit(cmd, msg);
337 /* The legacy API considered an empty message a success message */
338 msg->rep = tipc_tlv_alloc(0);
345 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
346 struct nlattr **attrs)
348 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
351 if (!attrs[TIPC_NLA_BEARER])
354 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
355 attrs[TIPC_NLA_BEARER], NULL);
359 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
360 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
361 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
364 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
366 struct tipc_nl_compat_msg *msg)
369 struct nlattr *bearer;
370 struct tipc_bearer_config *b;
372 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
374 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
378 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
381 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
384 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
385 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
388 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
390 nla_nest_end(skb, prop);
392 nla_nest_end(skb, bearer);
397 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
399 struct tipc_nl_compat_msg *msg)
402 struct nlattr *bearer;
404 name = (char *)TLV_DATA(msg->req);
406 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
410 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
413 nla_nest_end(skb, bearer);
418 static inline u32 perc(u32 count, u32 total)
420 return (count * 100 + (total / 2)) / total;
423 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
424 struct nlattr *prop[], struct nlattr *stats[])
426 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
427 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
429 tipc_tlv_sprintf(msg->rep,
430 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
431 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
432 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
433 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
434 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
435 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
437 tipc_tlv_sprintf(msg->rep,
438 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
439 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
440 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
441 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
442 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
443 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
445 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
446 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
447 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
448 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
450 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
451 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
452 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
453 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
455 tipc_tlv_sprintf(msg->rep,
456 " Congestion link:%u Send queue max:%u avg:%u",
457 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
458 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
459 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
462 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
463 struct nlattr **attrs)
466 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
467 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
468 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
471 if (!attrs[TIPC_NLA_LINK])
474 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
479 if (!link[TIPC_NLA_LINK_PROP])
482 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
483 link[TIPC_NLA_LINK_PROP], NULL);
487 if (!link[TIPC_NLA_LINK_STATS])
490 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
491 link[TIPC_NLA_LINK_STATS], NULL);
495 name = (char *)TLV_DATA(msg->req);
496 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
499 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
500 nla_data(link[TIPC_NLA_LINK_NAME]));
502 if (link[TIPC_NLA_LINK_BROADCAST]) {
503 __fill_bc_link_stat(msg, prop, stats);
507 if (link[TIPC_NLA_LINK_ACTIVE])
508 tipc_tlv_sprintf(msg->rep, " ACTIVE");
509 else if (link[TIPC_NLA_LINK_UP])
510 tipc_tlv_sprintf(msg->rep, " STANDBY");
512 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
514 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
515 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
516 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
518 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
519 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
520 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
522 tipc_tlv_sprintf(msg->rep,
523 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
524 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
525 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
526 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
527 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
528 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
529 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
531 tipc_tlv_sprintf(msg->rep,
532 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
533 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
534 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
535 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
536 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
537 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
538 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
540 tipc_tlv_sprintf(msg->rep,
541 " TX profile sample:%u packets average:%u octets\n",
542 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
543 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
544 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
546 tipc_tlv_sprintf(msg->rep,
547 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
548 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
549 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
550 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
551 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
552 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
553 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
554 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
555 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
557 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
558 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
559 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
560 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
561 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
562 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
563 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
565 tipc_tlv_sprintf(msg->rep,
566 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
567 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
568 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
569 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
570 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
571 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
573 tipc_tlv_sprintf(msg->rep,
574 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
575 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
576 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
577 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
578 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
579 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
581 tipc_tlv_sprintf(msg->rep,
582 " Congestion link:%u Send queue max:%u avg:%u",
583 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
584 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
585 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
590 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
591 struct nlattr **attrs)
593 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
594 struct tipc_link_info link_info;
597 if (!attrs[TIPC_NLA_LINK])
600 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
605 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
606 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
607 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
610 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
611 &link_info, sizeof(link_info));
614 static int __tipc_add_link_prop(struct sk_buff *skb,
615 struct tipc_nl_compat_msg *msg,
616 struct tipc_link_config *lc)
619 case TIPC_CMD_SET_LINK_PRI:
620 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
621 case TIPC_CMD_SET_LINK_TOL:
622 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
623 case TIPC_CMD_SET_LINK_WINDOW:
624 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
630 static int tipc_nl_compat_media_set(struct sk_buff *skb,
631 struct tipc_nl_compat_msg *msg)
634 struct nlattr *media;
635 struct tipc_link_config *lc;
637 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
639 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
643 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
646 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
650 __tipc_add_link_prop(skb, msg, lc);
651 nla_nest_end(skb, prop);
652 nla_nest_end(skb, media);
657 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
658 struct tipc_nl_compat_msg *msg)
661 struct nlattr *bearer;
662 struct tipc_link_config *lc;
664 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
666 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
670 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
673 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
677 __tipc_add_link_prop(skb, msg, lc);
678 nla_nest_end(skb, prop);
679 nla_nest_end(skb, bearer);
684 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
685 struct tipc_nl_compat_msg *msg)
689 struct tipc_link_config *lc;
691 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
693 link = nla_nest_start(skb, TIPC_NLA_LINK);
697 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
700 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
704 __tipc_add_link_prop(skb, msg, lc);
705 nla_nest_end(skb, prop);
706 nla_nest_end(skb, link);
711 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
713 struct tipc_nl_compat_msg *msg)
715 struct tipc_link_config *lc;
716 struct tipc_bearer *bearer;
717 struct tipc_media *media;
719 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
721 media = tipc_media_find(lc->name);
723 cmd->doit = &tipc_nl_media_set;
724 return tipc_nl_compat_media_set(skb, msg);
727 bearer = tipc_bearer_find(msg->net, lc->name);
729 cmd->doit = &tipc_nl_bearer_set;
730 return tipc_nl_compat_bearer_set(skb, msg);
733 return __tipc_nl_compat_link_set(skb, msg);
736 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
738 struct tipc_nl_compat_msg *msg)
743 name = (char *)TLV_DATA(msg->req);
745 link = nla_nest_start(skb, TIPC_NLA_LINK);
749 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
752 nla_nest_end(skb, link);
757 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
761 struct tipc_name_table_query *ntq;
762 static const char * const header[] = {
769 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
771 depth = ntohl(ntq->depth);
775 for (i = 0; i < depth; i++)
776 tipc_tlv_sprintf(msg->rep, header[i]);
777 tipc_tlv_sprintf(msg->rep, "\n");
782 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
783 struct nlattr **attrs)
786 struct tipc_name_table_query *ntq;
787 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
788 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
789 u32 node, depth, type, lowbound, upbound;
790 static const char * const scope_str[] = {"", " zone", " cluster",
794 if (!attrs[TIPC_NLA_NAME_TABLE])
797 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
798 attrs[TIPC_NLA_NAME_TABLE], NULL);
802 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
805 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
806 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
810 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
812 depth = ntohl(ntq->depth);
813 type = ntohl(ntq->type);
814 lowbound = ntohl(ntq->lowbound);
815 upbound = ntohl(ntq->upbound);
817 if (!(depth & TIPC_NTQ_ALLTYPES) &&
818 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
820 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
822 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
825 tipc_tlv_sprintf(msg->rep, "%-10u ",
826 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
831 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
832 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
833 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
838 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
839 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
840 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
841 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
846 tipc_tlv_sprintf(msg->rep, "%-10u %s",
847 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
848 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
850 tipc_tlv_sprintf(msg->rep, "\n");
855 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
856 struct nlattr **attrs)
858 u32 type, lower, upper;
859 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
862 if (!attrs[TIPC_NLA_PUBL])
865 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
870 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
871 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
872 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
875 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
877 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
882 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
887 struct sk_buff *args;
888 struct tipc_nl_compat_cmd_dump dump;
890 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
894 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
897 nest = nla_nest_start(args, TIPC_NLA_SOCK);
903 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
908 nla_nest_end(args, nest);
909 genlmsg_end(args, hdr);
911 dump.dumpit = tipc_nl_publ_dump;
912 dump.format = __tipc_nl_compat_publ_dump;
914 err = __tipc_nl_compat_dumpit(&dump, msg, args);
921 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
922 struct nlattr **attrs)
926 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
928 if (!attrs[TIPC_NLA_SOCK])
931 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
936 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
937 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
939 if (sock[TIPC_NLA_SOCK_CON]) {
941 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
943 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
946 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
947 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
951 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
953 if (con[TIPC_NLA_CON_FLAG])
954 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
955 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
956 nla_get_u32(con[TIPC_NLA_CON_INST]));
958 tipc_tlv_sprintf(msg->rep, "\n");
959 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
960 tipc_tlv_sprintf(msg->rep, " bound to");
962 err = tipc_nl_compat_publ_dump(msg, sock_ref);
966 tipc_tlv_sprintf(msg->rep, "\n");
971 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
972 struct nlattr **attrs)
974 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
977 if (!attrs[TIPC_NLA_MEDIA])
980 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
985 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
986 nla_data(media[TIPC_NLA_MEDIA_NAME]),
987 nla_len(media[TIPC_NLA_MEDIA_NAME]));
990 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
991 struct nlattr **attrs)
993 struct tipc_node_info node_info;
994 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
997 if (!attrs[TIPC_NLA_NODE])
1000 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1005 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1006 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1008 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1012 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1013 struct sk_buff *skb,
1014 struct tipc_nl_compat_msg *msg)
1019 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1021 net = nla_nest_start(skb, TIPC_NLA_NET);
1025 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1026 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1028 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1029 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1032 nla_nest_end(skb, net);
1037 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1038 struct nlattr **attrs)
1041 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1044 if (!attrs[TIPC_NLA_NET])
1047 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1052 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1054 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1057 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1059 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1063 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1064 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1069 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1071 struct tipc_nl_compat_cmd_dump dump;
1072 struct tipc_nl_compat_cmd_doit doit;
1074 memset(&dump, 0, sizeof(dump));
1075 memset(&doit, 0, sizeof(doit));
1079 msg->rep = tipc_tlv_alloc(0);
1083 case TIPC_CMD_GET_BEARER_NAMES:
1084 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1085 dump.dumpit = tipc_nl_bearer_dump;
1086 dump.format = tipc_nl_compat_bearer_dump;
1087 return tipc_nl_compat_dumpit(&dump, msg);
1088 case TIPC_CMD_ENABLE_BEARER:
1089 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1090 doit.doit = tipc_nl_bearer_enable;
1091 doit.transcode = tipc_nl_compat_bearer_enable;
1092 return tipc_nl_compat_doit(&doit, msg);
1093 case TIPC_CMD_DISABLE_BEARER:
1094 msg->req_type = TIPC_TLV_BEARER_NAME;
1095 doit.doit = tipc_nl_bearer_disable;
1096 doit.transcode = tipc_nl_compat_bearer_disable;
1097 return tipc_nl_compat_doit(&doit, msg);
1098 case TIPC_CMD_SHOW_LINK_STATS:
1099 msg->req_type = TIPC_TLV_LINK_NAME;
1100 msg->rep_size = ULTRA_STRING_MAX_LEN;
1101 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1102 dump.dumpit = tipc_nl_node_dump_link;
1103 dump.format = tipc_nl_compat_link_stat_dump;
1104 return tipc_nl_compat_dumpit(&dump, msg);
1105 case TIPC_CMD_GET_LINKS:
1106 msg->req_type = TIPC_TLV_NET_ADDR;
1107 msg->rep_size = ULTRA_STRING_MAX_LEN;
1108 dump.dumpit = tipc_nl_node_dump_link;
1109 dump.format = tipc_nl_compat_link_dump;
1110 return tipc_nl_compat_dumpit(&dump, msg);
1111 case TIPC_CMD_SET_LINK_TOL:
1112 case TIPC_CMD_SET_LINK_PRI:
1113 case TIPC_CMD_SET_LINK_WINDOW:
1114 msg->req_type = TIPC_TLV_LINK_CONFIG;
1115 doit.doit = tipc_nl_node_set_link;
1116 doit.transcode = tipc_nl_compat_link_set;
1117 return tipc_nl_compat_doit(&doit, msg);
1118 case TIPC_CMD_RESET_LINK_STATS:
1119 msg->req_type = TIPC_TLV_LINK_NAME;
1120 doit.doit = tipc_nl_node_reset_link_stats;
1121 doit.transcode = tipc_nl_compat_link_reset_stats;
1122 return tipc_nl_compat_doit(&doit, msg);
1123 case TIPC_CMD_SHOW_NAME_TABLE:
1124 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1125 msg->rep_size = ULTRA_STRING_MAX_LEN;
1126 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1127 dump.header = tipc_nl_compat_name_table_dump_header;
1128 dump.dumpit = tipc_nl_name_table_dump;
1129 dump.format = tipc_nl_compat_name_table_dump;
1130 return tipc_nl_compat_dumpit(&dump, msg);
1131 case TIPC_CMD_SHOW_PORTS:
1132 msg->rep_size = ULTRA_STRING_MAX_LEN;
1133 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1134 dump.dumpit = tipc_nl_sk_dump;
1135 dump.format = tipc_nl_compat_sk_dump;
1136 return tipc_nl_compat_dumpit(&dump, msg);
1137 case TIPC_CMD_GET_MEDIA_NAMES:
1138 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1139 dump.dumpit = tipc_nl_media_dump;
1140 dump.format = tipc_nl_compat_media_dump;
1141 return tipc_nl_compat_dumpit(&dump, msg);
1142 case TIPC_CMD_GET_NODES:
1143 msg->rep_size = ULTRA_STRING_MAX_LEN;
1144 dump.dumpit = tipc_nl_node_dump;
1145 dump.format = tipc_nl_compat_node_dump;
1146 return tipc_nl_compat_dumpit(&dump, msg);
1147 case TIPC_CMD_SET_NODE_ADDR:
1148 msg->req_type = TIPC_TLV_NET_ADDR;
1149 doit.doit = tipc_nl_net_set;
1150 doit.transcode = tipc_nl_compat_net_set;
1151 return tipc_nl_compat_doit(&doit, msg);
1152 case TIPC_CMD_SET_NETID:
1153 msg->req_type = TIPC_TLV_UNSIGNED;
1154 doit.doit = tipc_nl_net_set;
1155 doit.transcode = tipc_nl_compat_net_set;
1156 return tipc_nl_compat_doit(&doit, msg);
1157 case TIPC_CMD_GET_NETID:
1158 msg->rep_size = sizeof(u32);
1159 dump.dumpit = tipc_nl_net_dump;
1160 dump.format = tipc_nl_compat_net_dump;
1161 return tipc_nl_compat_dumpit(&dump, msg);
1162 case TIPC_CMD_SHOW_STATS:
1163 return tipc_cmd_show_stats_compat(msg);
1169 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1173 struct tipc_nl_compat_msg msg;
1174 struct nlmsghdr *req_nlh;
1175 struct nlmsghdr *rep_nlh;
1176 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1178 memset(&msg, 0, sizeof(msg));
1180 req_nlh = (struct nlmsghdr *)skb->data;
1181 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1182 msg.cmd = req_userhdr->cmd;
1183 msg.net = genl_info_net(info);
1184 msg.dst_sk = skb->sk;
1186 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1187 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1192 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1193 if (len && !TLV_OK(msg.req, len)) {
1194 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1199 err = tipc_nl_compat_handle(&msg);
1200 if ((err == -EOPNOTSUPP) || (err == -EPERM))
1201 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1202 else if (err == -EINVAL)
1203 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1208 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1209 skb_push(msg.rep, len);
1210 rep_nlh = nlmsg_hdr(msg.rep);
1211 memcpy(rep_nlh, info->nlhdr, len);
1212 rep_nlh->nlmsg_len = msg.rep->len;
1213 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1218 static struct genl_family tipc_genl_compat_family = {
1219 .id = GENL_ID_GENERATE,
1220 .name = TIPC_GENL_NAME,
1221 .version = TIPC_GENL_VERSION,
1222 .hdrsize = TIPC_GENL_HDRLEN,
1227 static struct genl_ops tipc_genl_compat_ops[] = {
1229 .cmd = TIPC_GENL_CMD,
1230 .doit = tipc_nl_compat_recv,
1234 int tipc_netlink_compat_start(void)
1238 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1239 tipc_genl_compat_ops);
1241 pr_err("Failed to register legacy compat interface\n");
1248 void tipc_netlink_compat_stop(void)
1250 genl_unregister_family(&tipc_genl_compat_family);