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.
38 #include "name_table.h"
40 #include <net/genetlink.h>
41 #include <linux/tipc_config.h>
43 /* The legacy API had an artificial message length limit called
44 * ULTRA_STRING_MAX_LEN.
46 #define ULTRA_STRING_MAX_LEN 32768
48 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
50 #define REPLY_TRUNCATED "<truncated>\n"
52 struct tipc_nl_compat_msg {
62 struct tipc_nl_compat_cmd_dump {
63 int (*header)(struct tipc_nl_compat_msg *);
64 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
65 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
68 struct tipc_nl_compat_cmd_doit {
69 int (*doit)(struct sk_buff *skb, struct genl_info *info);
70 int (*transcode)(struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
73 static int tipc_skb_tailroom(struct sk_buff *skb)
78 tailroom = skb_tailroom(skb);
79 limit = TIPC_SKB_MAX - skb->len;
87 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
89 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
91 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
94 skb_put(skb, TLV_SPACE(len));
95 tlv->tlv_type = htons(type);
96 tlv->tlv_len = htons(TLV_LENGTH(len));
98 memcpy(TLV_DATA(tlv), data, len);
103 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
105 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
108 TLV_SET_TYPE(tlv, type);
109 skb_put(skb, sizeof(struct tlv_desc));
112 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
118 struct tlv_desc *tlv;
121 rem = tipc_skb_tailroom(skb);
123 tlv = (struct tlv_desc *)skb->data;
124 len = TLV_GET_LEN(tlv);
125 buf = TLV_DATA(tlv) + len;
128 n = vscnprintf(buf, rem, fmt, args);
131 TLV_SET_LEN(tlv, n + len);
137 static struct sk_buff *tipc_tlv_alloc(int size)
142 size = TLV_SPACE(size);
143 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
145 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
149 skb_reserve(buf, hdr_len);
154 static struct sk_buff *tipc_get_err_tlv(char *str)
156 int str_len = strlen(str) + 1;
159 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
161 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
166 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
167 struct tipc_nl_compat_msg *msg,
173 struct nlmsghdr *nlmsg;
174 struct netlink_callback cb;
176 memset(&cb, 0, sizeof(cb));
177 cb.nlh = (struct nlmsghdr *)arg->data;
180 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
184 buf->sk = msg->dst_sk;
189 len = (*cmd->dumpit)(buf, &cb);
191 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
192 struct nlattr **attrs;
194 err = tipc_nlmsg_parse(nlmsg, &attrs);
198 err = (*cmd->format)(msg, attrs);
202 if (tipc_skb_tailroom(msg->rep) <= 1) {
208 skb_reset_tail_pointer(buf);
218 if (err == -EMSGSIZE) {
219 /* The legacy API only considered messages filling
220 * "ULTRA_STRING_MAX_LEN" to be truncated.
222 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
223 char *tail = skb_tail_pointer(msg->rep);
226 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
236 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
237 struct tipc_nl_compat_msg *msg)
242 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
245 msg->rep = tipc_tlv_alloc(msg->rep_size);
250 tipc_tlv_init(msg->rep, msg->rep_type);
255 arg = nlmsg_new(0, GFP_KERNEL);
261 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
270 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
271 struct tipc_nl_compat_msg *msg)
274 struct sk_buff *doit_buf;
275 struct sk_buff *trans_buf;
276 struct nlattr **attrbuf;
277 struct genl_info info;
279 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
283 err = (*cmd->transcode)(trans_buf, msg);
287 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
288 sizeof(struct nlattr *), GFP_KERNEL);
294 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
295 (const struct nlattr *)trans_buf->data,
296 trans_buf->len, NULL);
300 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
306 doit_buf->sk = msg->dst_sk;
308 memset(&info, 0, sizeof(info));
309 info.attrs = attrbuf;
311 err = (*cmd->doit)(doit_buf, &info);
317 kfree_skb(trans_buf);
322 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
323 struct tipc_nl_compat_msg *msg)
327 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
330 err = __tipc_nl_compat_doit(cmd, msg);
334 /* The legacy API considered an empty message a success message */
335 msg->rep = tipc_tlv_alloc(0);
342 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
343 struct nlattr **attrs)
345 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
347 nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
350 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
351 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
352 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
355 static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
356 struct tipc_nl_compat_msg *msg)
359 struct nlattr *bearer;
360 struct tipc_bearer_config *b;
362 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
364 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
368 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
371 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
374 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
375 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
378 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
380 nla_nest_end(skb, prop);
382 nla_nest_end(skb, bearer);
387 static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
388 struct tipc_nl_compat_msg *msg)
391 struct nlattr *bearer;
393 name = (char *)TLV_DATA(msg->req);
395 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
399 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
402 nla_nest_end(skb, bearer);
407 static inline u32 perc(u32 count, u32 total)
409 return (count * 100 + (total / 2)) / total;
412 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
413 struct nlattr *prop[], struct nlattr *stats[])
415 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
416 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
418 tipc_tlv_sprintf(msg->rep,
419 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
420 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
421 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
422 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
423 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
424 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
426 tipc_tlv_sprintf(msg->rep,
427 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
428 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
429 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
430 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
431 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
432 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
434 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
435 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
436 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
437 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
439 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
440 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
441 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
442 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
444 tipc_tlv_sprintf(msg->rep,
445 " Congestion link:%u Send queue max:%u avg:%u",
446 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
447 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
448 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
451 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
452 struct nlattr **attrs)
455 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
456 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
457 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
459 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
461 nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
464 nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
467 name = (char *)TLV_DATA(msg->req);
468 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
471 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
472 nla_data(link[TIPC_NLA_LINK_NAME]));
474 if (link[TIPC_NLA_LINK_BROADCAST]) {
475 __fill_bc_link_stat(msg, prop, stats);
479 if (link[TIPC_NLA_LINK_ACTIVE])
480 tipc_tlv_sprintf(msg->rep, " ACTIVE");
481 else if (link[TIPC_NLA_LINK_UP])
482 tipc_tlv_sprintf(msg->rep, " STANDBY");
484 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
486 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
487 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
488 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
490 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
491 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
492 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
494 tipc_tlv_sprintf(msg->rep,
495 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
496 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
497 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
498 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
499 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
500 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
501 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
503 tipc_tlv_sprintf(msg->rep,
504 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
505 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
506 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
507 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
508 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
509 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
510 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
512 tipc_tlv_sprintf(msg->rep,
513 " TX profile sample:%u packets average:%u octets\n",
514 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
515 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
516 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
518 tipc_tlv_sprintf(msg->rep,
519 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
520 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
521 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
522 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
523 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
524 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
525 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
526 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
527 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
529 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
530 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
531 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
532 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
533 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
534 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
535 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
537 tipc_tlv_sprintf(msg->rep,
538 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
539 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
540 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
541 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
542 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
543 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
545 tipc_tlv_sprintf(msg->rep,
546 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
547 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
548 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
549 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
550 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
551 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
553 tipc_tlv_sprintf(msg->rep,
554 " Congestion link:%u Send queue max:%u avg:%u",
555 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
556 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
557 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
562 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
563 struct nlattr **attrs)
565 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
566 struct tipc_link_info link_info;
568 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
570 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
571 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
572 strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
574 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
575 &link_info, sizeof(link_info));
578 static int tipc_nl_compat_link_set(struct sk_buff *skb,
579 struct tipc_nl_compat_msg *msg)
583 struct tipc_link_config *lc;
585 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
587 link = nla_nest_start(skb, TIPC_NLA_LINK);
591 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
594 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
598 if (msg->cmd == TIPC_CMD_SET_LINK_PRI) {
599 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)))
601 } else if (msg->cmd == TIPC_CMD_SET_LINK_TOL) {
602 if (nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)))
604 } else if (msg->cmd == TIPC_CMD_SET_LINK_WINDOW) {
605 if (nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)))
609 nla_nest_end(skb, prop);
610 nla_nest_end(skb, link);
615 static int tipc_nl_compat_link_reset_stats(struct sk_buff *skb,
616 struct tipc_nl_compat_msg *msg)
621 name = (char *)TLV_DATA(msg->req);
623 link = nla_nest_start(skb, TIPC_NLA_LINK);
627 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
630 nla_nest_end(skb, link);
635 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
639 struct tipc_name_table_query *ntq;
640 static const char * const header[] = {
647 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
649 depth = ntohl(ntq->depth);
653 for (i = 0; i < depth; i++)
654 tipc_tlv_sprintf(msg->rep, header[i]);
655 tipc_tlv_sprintf(msg->rep, "\n");
660 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
661 struct nlattr **attrs)
664 struct tipc_name_table_query *ntq;
665 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
666 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
667 u32 node, depth, type, lowbound, upbound;
668 static const char * const scope_str[] = {"", " zone", " cluster",
671 nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
672 attrs[TIPC_NLA_NAME_TABLE], NULL);
674 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
677 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
679 depth = ntohl(ntq->depth);
680 type = ntohl(ntq->type);
681 lowbound = ntohl(ntq->lowbound);
682 upbound = ntohl(ntq->upbound);
684 if (!(depth & TIPC_NTQ_ALLTYPES) &&
685 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
687 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
689 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
692 tipc_tlv_sprintf(msg->rep, "%-10u ",
693 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
698 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
699 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
700 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
705 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
706 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
707 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
708 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
713 tipc_tlv_sprintf(msg->rep, "%-10u %s",
714 nla_get_u32(publ[TIPC_NLA_PUBL_REF]),
715 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
717 tipc_tlv_sprintf(msg->rep, "\n");
722 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
723 struct nlattr **attrs)
725 u32 type, lower, upper;
726 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
728 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL], NULL);
730 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
731 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
732 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
735 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
737 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
742 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
747 struct sk_buff *args;
748 struct tipc_nl_compat_cmd_dump dump;
750 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
754 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
757 nest = nla_nest_start(args, TIPC_NLA_SOCK);
763 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
768 nla_nest_end(args, nest);
769 genlmsg_end(args, hdr);
771 dump.dumpit = tipc_nl_publ_dump;
772 dump.format = __tipc_nl_compat_publ_dump;
774 err = __tipc_nl_compat_dumpit(&dump, msg, args);
781 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
782 struct nlattr **attrs)
786 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
788 nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK], NULL);
790 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
791 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
793 if (sock[TIPC_NLA_SOCK_CON]) {
795 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
797 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
800 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
801 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
805 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
807 if (con[TIPC_NLA_CON_FLAG])
808 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
809 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
810 nla_get_u32(con[TIPC_NLA_CON_INST]));
812 tipc_tlv_sprintf(msg->rep, "\n");
813 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
814 tipc_tlv_sprintf(msg->rep, " bound to");
816 err = tipc_nl_compat_publ_dump(msg, sock_ref);
820 tipc_tlv_sprintf(msg->rep, "\n");
825 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
827 struct tipc_nl_compat_cmd_dump dump;
828 struct tipc_nl_compat_cmd_doit doit;
830 memset(&dump, 0, sizeof(dump));
831 memset(&doit, 0, sizeof(doit));
834 case TIPC_CMD_GET_BEARER_NAMES:
835 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
836 dump.dumpit = tipc_nl_bearer_dump;
837 dump.format = tipc_nl_compat_bearer_dump;
838 return tipc_nl_compat_dumpit(&dump, msg);
839 case TIPC_CMD_ENABLE_BEARER:
840 msg->req_type = TIPC_TLV_BEARER_CONFIG;
841 doit.doit = tipc_nl_bearer_enable;
842 doit.transcode = tipc_nl_compat_bearer_enable;
843 return tipc_nl_compat_doit(&doit, msg);
844 case TIPC_CMD_DISABLE_BEARER:
845 msg->req_type = TIPC_TLV_BEARER_NAME;
846 doit.doit = tipc_nl_bearer_disable;
847 doit.transcode = tipc_nl_compat_bearer_disable;
848 return tipc_nl_compat_doit(&doit, msg);
849 case TIPC_CMD_SHOW_LINK_STATS:
850 msg->req_type = TIPC_TLV_LINK_NAME;
851 msg->rep_size = ULTRA_STRING_MAX_LEN;
852 msg->rep_type = TIPC_TLV_ULTRA_STRING;
853 dump.dumpit = tipc_nl_link_dump;
854 dump.format = tipc_nl_compat_link_stat_dump;
855 return tipc_nl_compat_dumpit(&dump, msg);
856 case TIPC_CMD_GET_LINKS:
857 msg->req_type = TIPC_TLV_NET_ADDR;
858 msg->rep_size = ULTRA_STRING_MAX_LEN;
859 dump.dumpit = tipc_nl_link_dump;
860 dump.format = tipc_nl_compat_link_dump;
861 return tipc_nl_compat_dumpit(&dump, msg);
862 case TIPC_CMD_SET_LINK_TOL:
863 case TIPC_CMD_SET_LINK_PRI:
864 case TIPC_CMD_SET_LINK_WINDOW:
865 msg->req_type = TIPC_TLV_LINK_CONFIG;
866 doit.doit = tipc_nl_link_set;
867 doit.transcode = tipc_nl_compat_link_set;
868 return tipc_nl_compat_doit(&doit, msg);
869 case TIPC_CMD_RESET_LINK_STATS:
870 msg->req_type = TIPC_TLV_LINK_NAME;
871 doit.doit = tipc_nl_link_reset_stats;
872 doit.transcode = tipc_nl_compat_link_reset_stats;
873 return tipc_nl_compat_doit(&doit, msg);
874 case TIPC_CMD_SHOW_NAME_TABLE:
875 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
876 msg->rep_size = ULTRA_STRING_MAX_LEN;
877 msg->rep_type = TIPC_TLV_ULTRA_STRING;
878 dump.header = tipc_nl_compat_name_table_dump_header;
879 dump.dumpit = tipc_nl_name_table_dump;
880 dump.format = tipc_nl_compat_name_table_dump;
881 return tipc_nl_compat_dumpit(&dump, msg);
882 case TIPC_CMD_SHOW_PORTS:
883 msg->rep_size = ULTRA_STRING_MAX_LEN;
884 msg->rep_type = TIPC_TLV_ULTRA_STRING;
885 dump.dumpit = tipc_nl_sk_dump;
886 dump.format = tipc_nl_compat_sk_dump;
887 return tipc_nl_compat_dumpit(&dump, msg);
893 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
897 struct tipc_nl_compat_msg msg;
898 struct nlmsghdr *req_nlh;
899 struct nlmsghdr *rep_nlh;
900 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
901 struct net *net = genl_info_net(info);
903 memset(&msg, 0, sizeof(msg));
905 req_nlh = (struct nlmsghdr *)skb->data;
906 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
907 msg.cmd = req_userhdr->cmd;
908 msg.dst_sk = info->dst_sk;
910 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
911 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
916 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
917 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
918 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
923 err = tipc_nl_compat_handle(&msg);
924 if (err == -EOPNOTSUPP)
925 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
926 else if (err == -EINVAL)
927 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
932 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
933 skb_push(msg.rep, len);
934 rep_nlh = nlmsg_hdr(msg.rep);
935 memcpy(rep_nlh, info->nlhdr, len);
936 rep_nlh->nlmsg_len = msg.rep->len;
937 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
942 static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
944 struct net *net = genl_info_net(info);
945 struct sk_buff *rep_buf;
946 struct nlmsghdr *rep_nlh;
947 struct nlmsghdr *req_nlh = info->nlhdr;
948 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
949 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
952 if ((req_userhdr->cmd & 0xC000) &&
953 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
954 cmd = TIPC_CMD_NOT_NET_ADMIN;
956 cmd = req_userhdr->cmd;
958 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
959 nlmsg_data(req_nlh) + GENL_HDRLEN +
961 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
962 TIPC_GENL_HDRLEN), hdr_space);
965 skb_push(rep_buf, hdr_space);
966 rep_nlh = nlmsg_hdr(rep_buf);
967 memcpy(rep_nlh, req_nlh, hdr_space);
968 rep_nlh->nlmsg_len = rep_buf->len;
969 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
975 /* Temporary function to keep functionality throughout the patchset
976 * without having to mess with the global variables and other trickery
979 static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
981 struct tipc_genlmsghdr *req = info->userhdr;
984 case TIPC_CMD_GET_BEARER_NAMES:
985 case TIPC_CMD_ENABLE_BEARER:
986 case TIPC_CMD_DISABLE_BEARER:
987 case TIPC_CMD_SHOW_LINK_STATS:
988 case TIPC_CMD_GET_LINKS:
989 case TIPC_CMD_SET_LINK_TOL:
990 case TIPC_CMD_SET_LINK_PRI:
991 case TIPC_CMD_SET_LINK_WINDOW:
992 case TIPC_CMD_RESET_LINK_STATS:
993 case TIPC_CMD_SHOW_NAME_TABLE:
994 case TIPC_CMD_SHOW_PORTS:
995 return tipc_nl_compat_recv(skb, info);
998 return handle_cmd(skb, info);
1001 static struct genl_family tipc_genl_compat_family = {
1002 .id = GENL_ID_GENERATE,
1003 .name = TIPC_GENL_NAME,
1004 .version = TIPC_GENL_VERSION,
1005 .hdrsize = TIPC_GENL_HDRLEN,
1010 static struct genl_ops tipc_genl_compat_ops[] = {
1012 .cmd = TIPC_GENL_CMD,
1013 .doit = tipc_nl_compat_tmp_wrap,
1017 int tipc_netlink_compat_start(void)
1021 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1022 tipc_genl_compat_ops);
1024 pr_err("Failed to register legacy compat interface\n");
1031 void tipc_netlink_compat_stop(void)
1033 genl_unregister_family(&tipc_genl_compat_family);