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"
42 #include <net/genetlink.h>
43 #include <linux/tipc_config.h>
45 /* The legacy API had an artificial message length limit called
46 * ULTRA_STRING_MAX_LEN.
48 #define ULTRA_STRING_MAX_LEN 32768
50 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
52 #define REPLY_TRUNCATED "<truncated>\n"
54 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 sk_buff *skb, struct tipc_nl_compat_msg *msg);
75 static int tipc_skb_tailroom(struct sk_buff *skb)
80 tailroom = skb_tailroom(skb);
81 limit = TIPC_SKB_MAX - skb->len;
89 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
91 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
93 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
96 skb_put(skb, TLV_SPACE(len));
97 tlv->tlv_type = htons(type);
98 tlv->tlv_len = htons(TLV_LENGTH(len));
100 memcpy(TLV_DATA(tlv), data, len);
105 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
107 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
110 TLV_SET_TYPE(tlv, type);
111 skb_put(skb, sizeof(struct tlv_desc));
114 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
120 struct tlv_desc *tlv;
123 rem = tipc_skb_tailroom(skb);
125 tlv = (struct tlv_desc *)skb->data;
126 len = TLV_GET_LEN(tlv);
127 buf = TLV_DATA(tlv) + len;
130 n = vscnprintf(buf, rem, fmt, args);
133 TLV_SET_LEN(tlv, n + len);
139 static struct sk_buff *tipc_tlv_alloc(int size)
144 size = TLV_SPACE(size);
145 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
147 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
151 skb_reserve(buf, hdr_len);
156 static struct sk_buff *tipc_get_err_tlv(char *str)
158 int str_len = strlen(str) + 1;
161 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
163 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
168 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
169 struct tipc_nl_compat_msg *msg,
175 struct nlmsghdr *nlmsg;
176 struct netlink_callback cb;
178 memset(&cb, 0, sizeof(cb));
179 cb.nlh = (struct nlmsghdr *)arg->data;
182 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
186 buf->sk = msg->dst_sk;
191 len = (*cmd->dumpit)(buf, &cb);
193 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
194 struct nlattr **attrs;
196 err = tipc_nlmsg_parse(nlmsg, &attrs);
200 err = (*cmd->format)(msg, attrs);
204 if (tipc_skb_tailroom(msg->rep) <= 1) {
210 skb_reset_tail_pointer(buf);
220 if (err == -EMSGSIZE) {
221 /* The legacy API only considered messages filling
222 * "ULTRA_STRING_MAX_LEN" to be truncated.
224 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
225 char *tail = skb_tail_pointer(msg->rep);
228 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
238 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
239 struct tipc_nl_compat_msg *msg)
244 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
247 msg->rep = tipc_tlv_alloc(msg->rep_size);
252 tipc_tlv_init(msg->rep, msg->rep_type);
257 arg = nlmsg_new(0, GFP_KERNEL);
263 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
272 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
273 struct tipc_nl_compat_msg *msg)
276 struct sk_buff *doit_buf;
277 struct sk_buff *trans_buf;
278 struct nlattr **attrbuf;
279 struct genl_info info;
281 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
285 err = (*cmd->transcode)(trans_buf, msg);
289 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
290 sizeof(struct nlattr *), GFP_KERNEL);
296 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
297 (const struct nlattr *)trans_buf->data,
298 trans_buf->len, NULL);
302 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
308 doit_buf->sk = msg->dst_sk;
310 memset(&info, 0, sizeof(info));
311 info.attrs = attrbuf;
313 err = (*cmd->doit)(doit_buf, &info);
319 kfree_skb(trans_buf);
324 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
325 struct tipc_nl_compat_msg *msg)
329 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
332 err = __tipc_nl_compat_doit(cmd, msg);
336 /* The legacy API considered an empty message a success message */
337 msg->rep = tipc_tlv_alloc(0);
344 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
345 struct nlattr **attrs)
347 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
349 nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
352 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
353 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
354 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
357 static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
358 struct tipc_nl_compat_msg *msg)
361 struct nlattr *bearer;
362 struct tipc_bearer_config *b;
364 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
366 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
370 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
373 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
376 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
377 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
380 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
382 nla_nest_end(skb, prop);
384 nla_nest_end(skb, bearer);
389 static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
390 struct tipc_nl_compat_msg *msg)
393 struct nlattr *bearer;
395 name = (char *)TLV_DATA(msg->req);
397 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
401 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
404 nla_nest_end(skb, bearer);
409 static inline u32 perc(u32 count, u32 total)
411 return (count * 100 + (total / 2)) / total;
414 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
415 struct nlattr *prop[], struct nlattr *stats[])
417 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
418 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
420 tipc_tlv_sprintf(msg->rep,
421 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
422 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
423 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
424 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
425 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
426 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
428 tipc_tlv_sprintf(msg->rep,
429 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
430 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
431 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
432 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
433 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
434 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
436 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
437 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
438 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
439 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
441 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
442 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
443 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
444 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
446 tipc_tlv_sprintf(msg->rep,
447 " Congestion link:%u Send queue max:%u avg:%u",
448 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
449 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
450 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
453 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
454 struct nlattr **attrs)
457 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
458 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
459 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
461 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
463 nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
466 nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
469 name = (char *)TLV_DATA(msg->req);
470 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
473 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
474 nla_data(link[TIPC_NLA_LINK_NAME]));
476 if (link[TIPC_NLA_LINK_BROADCAST]) {
477 __fill_bc_link_stat(msg, prop, stats);
481 if (link[TIPC_NLA_LINK_ACTIVE])
482 tipc_tlv_sprintf(msg->rep, " ACTIVE");
483 else if (link[TIPC_NLA_LINK_UP])
484 tipc_tlv_sprintf(msg->rep, " STANDBY");
486 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
488 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
489 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
490 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
492 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
493 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
494 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
496 tipc_tlv_sprintf(msg->rep,
497 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
498 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
499 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
500 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
501 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
502 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
503 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
505 tipc_tlv_sprintf(msg->rep,
506 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
507 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
508 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
509 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
510 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
511 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
512 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
514 tipc_tlv_sprintf(msg->rep,
515 " TX profile sample:%u packets average:%u octets\n",
516 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
517 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
518 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
520 tipc_tlv_sprintf(msg->rep,
521 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
522 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
523 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
524 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
525 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
526 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
527 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
528 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
529 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
531 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
532 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
533 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
534 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
535 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
536 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
537 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
539 tipc_tlv_sprintf(msg->rep,
540 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
541 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
542 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
543 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
544 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
545 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
547 tipc_tlv_sprintf(msg->rep,
548 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
549 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
550 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
551 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
552 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
553 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
555 tipc_tlv_sprintf(msg->rep,
556 " Congestion link:%u Send queue max:%u avg:%u",
557 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
558 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
559 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
564 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
565 struct nlattr **attrs)
567 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
568 struct tipc_link_info link_info;
570 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
572 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
573 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
574 strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
576 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
577 &link_info, sizeof(link_info));
580 static int tipc_nl_compat_link_set(struct sk_buff *skb,
581 struct tipc_nl_compat_msg *msg)
585 struct tipc_link_config *lc;
587 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
589 link = nla_nest_start(skb, TIPC_NLA_LINK);
593 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
596 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
600 if (msg->cmd == TIPC_CMD_SET_LINK_PRI) {
601 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)))
603 } else if (msg->cmd == TIPC_CMD_SET_LINK_TOL) {
604 if (nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)))
606 } else if (msg->cmd == TIPC_CMD_SET_LINK_WINDOW) {
607 if (nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)))
611 nla_nest_end(skb, prop);
612 nla_nest_end(skb, link);
617 static int tipc_nl_compat_link_reset_stats(struct sk_buff *skb,
618 struct tipc_nl_compat_msg *msg)
623 name = (char *)TLV_DATA(msg->req);
625 link = nla_nest_start(skb, TIPC_NLA_LINK);
629 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
632 nla_nest_end(skb, link);
637 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
641 struct tipc_name_table_query *ntq;
642 static const char * const header[] = {
649 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
651 depth = ntohl(ntq->depth);
655 for (i = 0; i < depth; i++)
656 tipc_tlv_sprintf(msg->rep, header[i]);
657 tipc_tlv_sprintf(msg->rep, "\n");
662 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
663 struct nlattr **attrs)
666 struct tipc_name_table_query *ntq;
667 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
668 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
669 u32 node, depth, type, lowbound, upbound;
670 static const char * const scope_str[] = {"", " zone", " cluster",
673 nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
674 attrs[TIPC_NLA_NAME_TABLE], NULL);
676 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
679 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
681 depth = ntohl(ntq->depth);
682 type = ntohl(ntq->type);
683 lowbound = ntohl(ntq->lowbound);
684 upbound = ntohl(ntq->upbound);
686 if (!(depth & TIPC_NTQ_ALLTYPES) &&
687 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
689 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
691 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
694 tipc_tlv_sprintf(msg->rep, "%-10u ",
695 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
700 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
701 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
702 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
707 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
708 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
709 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
710 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
715 tipc_tlv_sprintf(msg->rep, "%-10u %s",
716 nla_get_u32(publ[TIPC_NLA_PUBL_REF]),
717 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
719 tipc_tlv_sprintf(msg->rep, "\n");
724 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
725 struct nlattr **attrs)
727 u32 type, lower, upper;
728 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
730 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL], NULL);
732 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
733 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
734 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
737 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
739 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
744 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
749 struct sk_buff *args;
750 struct tipc_nl_compat_cmd_dump dump;
752 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
756 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
759 nest = nla_nest_start(args, TIPC_NLA_SOCK);
765 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
770 nla_nest_end(args, nest);
771 genlmsg_end(args, hdr);
773 dump.dumpit = tipc_nl_publ_dump;
774 dump.format = __tipc_nl_compat_publ_dump;
776 err = __tipc_nl_compat_dumpit(&dump, msg, args);
783 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
784 struct nlattr **attrs)
788 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
790 nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK], NULL);
792 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
793 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
795 if (sock[TIPC_NLA_SOCK_CON]) {
797 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
799 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
802 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
803 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
807 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
809 if (con[TIPC_NLA_CON_FLAG])
810 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
811 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
812 nla_get_u32(con[TIPC_NLA_CON_INST]));
814 tipc_tlv_sprintf(msg->rep, "\n");
815 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
816 tipc_tlv_sprintf(msg->rep, " bound to");
818 err = tipc_nl_compat_publ_dump(msg, sock_ref);
822 tipc_tlv_sprintf(msg->rep, "\n");
827 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
828 struct nlattr **attrs)
830 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
832 nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
835 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
836 nla_data(media[TIPC_NLA_MEDIA_NAME]),
837 nla_len(media[TIPC_NLA_MEDIA_NAME]));
840 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
841 struct nlattr **attrs)
843 struct tipc_node_info node_info;
844 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
846 nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE], NULL);
848 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
849 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
851 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
855 static int tipc_nl_compat_net_set(struct sk_buff *skb,
856 struct tipc_nl_compat_msg *msg)
861 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
863 net = nla_nest_start(skb, TIPC_NLA_NET);
867 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
868 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
870 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
871 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
874 nla_nest_end(skb, net);
879 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
880 struct nlattr **attrs)
883 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
885 nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET], NULL);
886 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
888 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
891 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
893 struct tipc_nl_compat_cmd_dump dump;
894 struct tipc_nl_compat_cmd_doit doit;
896 memset(&dump, 0, sizeof(dump));
897 memset(&doit, 0, sizeof(doit));
900 case TIPC_CMD_GET_BEARER_NAMES:
901 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
902 dump.dumpit = tipc_nl_bearer_dump;
903 dump.format = tipc_nl_compat_bearer_dump;
904 return tipc_nl_compat_dumpit(&dump, msg);
905 case TIPC_CMD_ENABLE_BEARER:
906 msg->req_type = TIPC_TLV_BEARER_CONFIG;
907 doit.doit = tipc_nl_bearer_enable;
908 doit.transcode = tipc_nl_compat_bearer_enable;
909 return tipc_nl_compat_doit(&doit, msg);
910 case TIPC_CMD_DISABLE_BEARER:
911 msg->req_type = TIPC_TLV_BEARER_NAME;
912 doit.doit = tipc_nl_bearer_disable;
913 doit.transcode = tipc_nl_compat_bearer_disable;
914 return tipc_nl_compat_doit(&doit, msg);
915 case TIPC_CMD_SHOW_LINK_STATS:
916 msg->req_type = TIPC_TLV_LINK_NAME;
917 msg->rep_size = ULTRA_STRING_MAX_LEN;
918 msg->rep_type = TIPC_TLV_ULTRA_STRING;
919 dump.dumpit = tipc_nl_link_dump;
920 dump.format = tipc_nl_compat_link_stat_dump;
921 return tipc_nl_compat_dumpit(&dump, msg);
922 case TIPC_CMD_GET_LINKS:
923 msg->req_type = TIPC_TLV_NET_ADDR;
924 msg->rep_size = ULTRA_STRING_MAX_LEN;
925 dump.dumpit = tipc_nl_link_dump;
926 dump.format = tipc_nl_compat_link_dump;
927 return tipc_nl_compat_dumpit(&dump, msg);
928 case TIPC_CMD_SET_LINK_TOL:
929 case TIPC_CMD_SET_LINK_PRI:
930 case TIPC_CMD_SET_LINK_WINDOW:
931 msg->req_type = TIPC_TLV_LINK_CONFIG;
932 doit.doit = tipc_nl_link_set;
933 doit.transcode = tipc_nl_compat_link_set;
934 return tipc_nl_compat_doit(&doit, msg);
935 case TIPC_CMD_RESET_LINK_STATS:
936 msg->req_type = TIPC_TLV_LINK_NAME;
937 doit.doit = tipc_nl_link_reset_stats;
938 doit.transcode = tipc_nl_compat_link_reset_stats;
939 return tipc_nl_compat_doit(&doit, msg);
940 case TIPC_CMD_SHOW_NAME_TABLE:
941 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
942 msg->rep_size = ULTRA_STRING_MAX_LEN;
943 msg->rep_type = TIPC_TLV_ULTRA_STRING;
944 dump.header = tipc_nl_compat_name_table_dump_header;
945 dump.dumpit = tipc_nl_name_table_dump;
946 dump.format = tipc_nl_compat_name_table_dump;
947 return tipc_nl_compat_dumpit(&dump, msg);
948 case TIPC_CMD_SHOW_PORTS:
949 msg->rep_size = ULTRA_STRING_MAX_LEN;
950 msg->rep_type = TIPC_TLV_ULTRA_STRING;
951 dump.dumpit = tipc_nl_sk_dump;
952 dump.format = tipc_nl_compat_sk_dump;
953 return tipc_nl_compat_dumpit(&dump, msg);
954 case TIPC_CMD_GET_MEDIA_NAMES:
955 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
956 dump.dumpit = tipc_nl_media_dump;
957 dump.format = tipc_nl_compat_media_dump;
958 return tipc_nl_compat_dumpit(&dump, msg);
959 case TIPC_CMD_GET_NODES:
960 msg->rep_size = ULTRA_STRING_MAX_LEN;
961 dump.dumpit = tipc_nl_node_dump;
962 dump.format = tipc_nl_compat_node_dump;
963 return tipc_nl_compat_dumpit(&dump, msg);
964 case TIPC_CMD_SET_NODE_ADDR:
965 msg->req_type = TIPC_TLV_NET_ADDR;
966 doit.doit = tipc_nl_net_set;
967 doit.transcode = tipc_nl_compat_net_set;
968 return tipc_nl_compat_doit(&doit, msg);
969 case TIPC_CMD_SET_NETID:
970 msg->req_type = TIPC_TLV_UNSIGNED;
971 doit.doit = tipc_nl_net_set;
972 doit.transcode = tipc_nl_compat_net_set;
973 return tipc_nl_compat_doit(&doit, msg);
974 case TIPC_CMD_GET_NETID:
975 msg->rep_size = sizeof(u32);
976 dump.dumpit = tipc_nl_net_dump;
977 dump.format = tipc_nl_compat_net_dump;
978 return tipc_nl_compat_dumpit(&dump, msg);
984 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
988 struct tipc_nl_compat_msg msg;
989 struct nlmsghdr *req_nlh;
990 struct nlmsghdr *rep_nlh;
991 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
992 struct net *net = genl_info_net(info);
994 memset(&msg, 0, sizeof(msg));
996 req_nlh = (struct nlmsghdr *)skb->data;
997 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
998 msg.cmd = req_userhdr->cmd;
999 msg.dst_sk = info->dst_sk;
1001 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1002 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1007 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1008 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
1009 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1014 err = tipc_nl_compat_handle(&msg);
1015 if (err == -EOPNOTSUPP)
1016 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1017 else if (err == -EINVAL)
1018 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1023 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1024 skb_push(msg.rep, len);
1025 rep_nlh = nlmsg_hdr(msg.rep);
1026 memcpy(rep_nlh, info->nlhdr, len);
1027 rep_nlh->nlmsg_len = msg.rep->len;
1028 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
1033 static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
1035 struct net *net = genl_info_net(info);
1036 struct sk_buff *rep_buf;
1037 struct nlmsghdr *rep_nlh;
1038 struct nlmsghdr *req_nlh = info->nlhdr;
1039 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1040 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1043 if ((req_userhdr->cmd & 0xC000) &&
1044 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
1045 cmd = TIPC_CMD_NOT_NET_ADMIN;
1047 cmd = req_userhdr->cmd;
1049 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
1050 nlmsg_data(req_nlh) + GENL_HDRLEN +
1052 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
1053 TIPC_GENL_HDRLEN), hdr_space);
1056 skb_push(rep_buf, hdr_space);
1057 rep_nlh = nlmsg_hdr(rep_buf);
1058 memcpy(rep_nlh, req_nlh, hdr_space);
1059 rep_nlh->nlmsg_len = rep_buf->len;
1060 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
1066 /* Temporary function to keep functionality throughout the patchset
1067 * without having to mess with the global variables and other trickery
1070 static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
1072 struct tipc_genlmsghdr *req = info->userhdr;
1075 case TIPC_CMD_GET_BEARER_NAMES:
1076 case TIPC_CMD_ENABLE_BEARER:
1077 case TIPC_CMD_DISABLE_BEARER:
1078 case TIPC_CMD_SHOW_LINK_STATS:
1079 case TIPC_CMD_GET_LINKS:
1080 case TIPC_CMD_SET_LINK_TOL:
1081 case TIPC_CMD_SET_LINK_PRI:
1082 case TIPC_CMD_SET_LINK_WINDOW:
1083 case TIPC_CMD_RESET_LINK_STATS:
1084 case TIPC_CMD_SHOW_NAME_TABLE:
1085 case TIPC_CMD_SHOW_PORTS:
1086 case TIPC_CMD_GET_MEDIA_NAMES:
1087 case TIPC_CMD_GET_NODES:
1088 case TIPC_CMD_SET_NODE_ADDR:
1089 case TIPC_CMD_SET_NETID:
1090 case TIPC_CMD_GET_NETID:
1091 return tipc_nl_compat_recv(skb, info);
1094 return handle_cmd(skb, info);
1097 static struct genl_family tipc_genl_compat_family = {
1098 .id = GENL_ID_GENERATE,
1099 .name = TIPC_GENL_NAME,
1100 .version = TIPC_GENL_VERSION,
1101 .hdrsize = TIPC_GENL_HDRLEN,
1106 static struct genl_ops tipc_genl_compat_ops[] = {
1108 .cmd = TIPC_GENL_CMD,
1109 .doit = tipc_nl_compat_tmp_wrap,
1113 int tipc_netlink_compat_start(void)
1117 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1118 tipc_genl_compat_ops);
1120 pr_err("Failed to register legacy compat interface\n");
1127 void tipc_netlink_compat_stop(void)
1129 genl_unregister_family(&tipc_genl_compat_family);