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 <net/genetlink.h>
39 #include <linux/tipc_config.h>
41 /* The legacy API had an artificial message length limit called
42 * ULTRA_STRING_MAX_LEN.
44 #define ULTRA_STRING_MAX_LEN 32768
46 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
48 #define REPLY_TRUNCATED "<truncated>\n"
50 struct tipc_nl_compat_msg {
60 struct tipc_nl_compat_cmd_dump {
61 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
62 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
65 struct tipc_nl_compat_cmd_doit {
66 int (*doit)(struct sk_buff *skb, struct genl_info *info);
67 int (*transcode)(struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
70 static int tipc_skb_tailroom(struct sk_buff *skb)
75 tailroom = skb_tailroom(skb);
76 limit = TIPC_SKB_MAX - skb->len;
84 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
86 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
88 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
91 skb_put(skb, TLV_SPACE(len));
92 tlv->tlv_type = htons(type);
93 tlv->tlv_len = htons(TLV_LENGTH(len));
95 memcpy(TLV_DATA(tlv), data, len);
100 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
102 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
105 TLV_SET_TYPE(tlv, type);
106 skb_put(skb, sizeof(struct tlv_desc));
109 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
115 struct tlv_desc *tlv;
118 rem = tipc_skb_tailroom(skb);
120 tlv = (struct tlv_desc *)skb->data;
121 len = TLV_GET_LEN(tlv);
122 buf = TLV_DATA(tlv) + len;
125 n = vscnprintf(buf, rem, fmt, args);
128 TLV_SET_LEN(tlv, n + len);
134 static struct sk_buff *tipc_tlv_alloc(int size)
139 size = TLV_SPACE(size);
140 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
142 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
146 skb_reserve(buf, hdr_len);
151 static struct sk_buff *tipc_get_err_tlv(char *str)
153 int str_len = strlen(str) + 1;
156 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
158 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
163 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
164 struct tipc_nl_compat_msg *msg,
170 struct nlmsghdr *nlmsg;
171 struct netlink_callback cb;
173 memset(&cb, 0, sizeof(cb));
174 cb.nlh = (struct nlmsghdr *)arg->data;
177 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
181 buf->sk = msg->dst_sk;
186 len = (*cmd->dumpit)(buf, &cb);
188 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
189 struct nlattr **attrs;
191 err = tipc_nlmsg_parse(nlmsg, &attrs);
195 err = (*cmd->format)(msg, attrs);
199 if (tipc_skb_tailroom(msg->rep) <= 1) {
205 skb_reset_tail_pointer(buf);
215 if (err == -EMSGSIZE) {
216 /* The legacy API only considered messages filling
217 * "ULTRA_STRING_MAX_LEN" to be truncated.
219 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
220 char *tail = skb_tail_pointer(msg->rep);
223 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
233 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
234 struct tipc_nl_compat_msg *msg)
239 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
242 msg->rep = tipc_tlv_alloc(msg->rep_size);
247 tipc_tlv_init(msg->rep, msg->rep_type);
249 arg = nlmsg_new(0, GFP_KERNEL);
255 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
264 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
265 struct tipc_nl_compat_msg *msg)
268 struct sk_buff *doit_buf;
269 struct sk_buff *trans_buf;
270 struct nlattr **attrbuf;
271 struct genl_info info;
273 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
277 err = (*cmd->transcode)(trans_buf, msg);
281 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
282 sizeof(struct nlattr *), GFP_KERNEL);
288 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
289 (const struct nlattr *)trans_buf->data,
290 trans_buf->len, NULL);
294 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
300 doit_buf->sk = msg->dst_sk;
302 memset(&info, 0, sizeof(info));
303 info.attrs = attrbuf;
305 err = (*cmd->doit)(doit_buf, &info);
311 kfree_skb(trans_buf);
316 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
317 struct tipc_nl_compat_msg *msg)
321 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
324 err = __tipc_nl_compat_doit(cmd, msg);
328 /* The legacy API considered an empty message a success message */
329 msg->rep = tipc_tlv_alloc(0);
336 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
337 struct nlattr **attrs)
339 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
341 nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
344 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
345 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
346 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
349 static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
350 struct tipc_nl_compat_msg *msg)
353 struct nlattr *bearer;
354 struct tipc_bearer_config *b;
356 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
358 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
362 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
365 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
368 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
369 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
372 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
374 nla_nest_end(skb, prop);
376 nla_nest_end(skb, bearer);
381 static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
382 struct tipc_nl_compat_msg *msg)
385 struct nlattr *bearer;
387 name = (char *)TLV_DATA(msg->req);
389 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
393 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
396 nla_nest_end(skb, bearer);
401 static inline u32 perc(u32 count, u32 total)
403 return (count * 100 + (total / 2)) / total;
406 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
407 struct nlattr *prop[], struct nlattr *stats[])
409 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
410 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
412 tipc_tlv_sprintf(msg->rep,
413 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
414 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
415 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
416 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
417 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
418 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
420 tipc_tlv_sprintf(msg->rep,
421 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
422 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
423 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
424 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
425 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
426 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
428 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
429 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
430 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
431 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
433 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
434 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
435 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
436 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
438 tipc_tlv_sprintf(msg->rep,
439 " Congestion link:%u Send queue max:%u avg:%u",
440 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
441 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
442 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
445 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
446 struct nlattr **attrs)
449 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
450 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
451 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
453 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
455 nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
458 nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
461 name = (char *)TLV_DATA(msg->req);
462 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
465 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
466 nla_data(link[TIPC_NLA_LINK_NAME]));
468 if (link[TIPC_NLA_LINK_BROADCAST]) {
469 __fill_bc_link_stat(msg, prop, stats);
473 if (link[TIPC_NLA_LINK_ACTIVE])
474 tipc_tlv_sprintf(msg->rep, " ACTIVE");
475 else if (link[TIPC_NLA_LINK_UP])
476 tipc_tlv_sprintf(msg->rep, " STANDBY");
478 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
480 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
481 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
482 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
484 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
485 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
486 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
488 tipc_tlv_sprintf(msg->rep,
489 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
490 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
491 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
492 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
493 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
494 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
495 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
497 tipc_tlv_sprintf(msg->rep,
498 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
499 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
500 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
501 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
502 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
503 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
504 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
506 tipc_tlv_sprintf(msg->rep,
507 " TX profile sample:%u packets average:%u octets\n",
508 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
509 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
510 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
512 tipc_tlv_sprintf(msg->rep,
513 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
514 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
515 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
516 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
517 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
518 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
519 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
520 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
521 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
523 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
524 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
525 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
526 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
527 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
528 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
529 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
531 tipc_tlv_sprintf(msg->rep,
532 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
533 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
534 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
535 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
536 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
537 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
539 tipc_tlv_sprintf(msg->rep,
540 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
541 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
542 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
543 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
544 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
545 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
547 tipc_tlv_sprintf(msg->rep,
548 " Congestion link:%u Send queue max:%u avg:%u",
549 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
550 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
551 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
556 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
557 struct nlattr **attrs)
559 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
560 struct tipc_link_info link_info;
562 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
564 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
565 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
566 strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
568 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
569 &link_info, sizeof(link_info));
572 static int tipc_nl_compat_link_set(struct sk_buff *skb,
573 struct tipc_nl_compat_msg *msg)
577 struct tipc_link_config *lc;
579 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
581 link = nla_nest_start(skb, TIPC_NLA_LINK);
585 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
588 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
592 if (msg->cmd == TIPC_CMD_SET_LINK_PRI) {
593 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)))
595 } else if (msg->cmd == TIPC_CMD_SET_LINK_TOL) {
596 if (nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)))
598 } else if (msg->cmd == TIPC_CMD_SET_LINK_WINDOW) {
599 if (nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)))
603 nla_nest_end(skb, prop);
604 nla_nest_end(skb, link);
609 static int tipc_nl_compat_link_reset_stats(struct sk_buff *skb,
610 struct tipc_nl_compat_msg *msg)
615 name = (char *)TLV_DATA(msg->req);
617 link = nla_nest_start(skb, TIPC_NLA_LINK);
621 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
624 nla_nest_end(skb, link);
629 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
631 struct tipc_nl_compat_cmd_dump dump;
632 struct tipc_nl_compat_cmd_doit doit;
634 memset(&dump, 0, sizeof(dump));
635 memset(&doit, 0, sizeof(doit));
638 case TIPC_CMD_GET_BEARER_NAMES:
639 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
640 dump.dumpit = tipc_nl_bearer_dump;
641 dump.format = tipc_nl_compat_bearer_dump;
642 return tipc_nl_compat_dumpit(&dump, msg);
643 case TIPC_CMD_ENABLE_BEARER:
644 msg->req_type = TIPC_TLV_BEARER_CONFIG;
645 doit.doit = tipc_nl_bearer_enable;
646 doit.transcode = tipc_nl_compat_bearer_enable;
647 return tipc_nl_compat_doit(&doit, msg);
648 case TIPC_CMD_DISABLE_BEARER:
649 msg->req_type = TIPC_TLV_BEARER_NAME;
650 doit.doit = tipc_nl_bearer_disable;
651 doit.transcode = tipc_nl_compat_bearer_disable;
652 return tipc_nl_compat_doit(&doit, msg);
653 case TIPC_CMD_SHOW_LINK_STATS:
654 msg->req_type = TIPC_TLV_LINK_NAME;
655 msg->rep_size = ULTRA_STRING_MAX_LEN;
656 msg->rep_type = TIPC_TLV_ULTRA_STRING;
657 dump.dumpit = tipc_nl_link_dump;
658 dump.format = tipc_nl_compat_link_stat_dump;
659 return tipc_nl_compat_dumpit(&dump, msg);
660 case TIPC_CMD_GET_LINKS:
661 msg->req_type = TIPC_TLV_NET_ADDR;
662 msg->rep_size = ULTRA_STRING_MAX_LEN;
663 dump.dumpit = tipc_nl_link_dump;
664 dump.format = tipc_nl_compat_link_dump;
665 return tipc_nl_compat_dumpit(&dump, msg);
666 case TIPC_CMD_SET_LINK_TOL:
667 case TIPC_CMD_SET_LINK_PRI:
668 case TIPC_CMD_SET_LINK_WINDOW:
669 msg->req_type = TIPC_TLV_LINK_CONFIG;
670 doit.doit = tipc_nl_link_set;
671 doit.transcode = tipc_nl_compat_link_set;
672 return tipc_nl_compat_doit(&doit, msg);
673 case TIPC_CMD_RESET_LINK_STATS:
674 msg->req_type = TIPC_TLV_LINK_NAME;
675 doit.doit = tipc_nl_link_reset_stats;
676 doit.transcode = tipc_nl_compat_link_reset_stats;
677 return tipc_nl_compat_doit(&doit, msg);
683 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
687 struct tipc_nl_compat_msg msg;
688 struct nlmsghdr *req_nlh;
689 struct nlmsghdr *rep_nlh;
690 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
691 struct net *net = genl_info_net(info);
693 memset(&msg, 0, sizeof(msg));
695 req_nlh = (struct nlmsghdr *)skb->data;
696 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
697 msg.cmd = req_userhdr->cmd;
698 msg.dst_sk = info->dst_sk;
700 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
701 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
706 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
707 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
708 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
713 err = tipc_nl_compat_handle(&msg);
714 if (err == -EOPNOTSUPP)
715 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
716 else if (err == -EINVAL)
717 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
722 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
723 skb_push(msg.rep, len);
724 rep_nlh = nlmsg_hdr(msg.rep);
725 memcpy(rep_nlh, info->nlhdr, len);
726 rep_nlh->nlmsg_len = msg.rep->len;
727 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
732 static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
734 struct net *net = genl_info_net(info);
735 struct sk_buff *rep_buf;
736 struct nlmsghdr *rep_nlh;
737 struct nlmsghdr *req_nlh = info->nlhdr;
738 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
739 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
742 if ((req_userhdr->cmd & 0xC000) &&
743 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
744 cmd = TIPC_CMD_NOT_NET_ADMIN;
746 cmd = req_userhdr->cmd;
748 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
749 nlmsg_data(req_nlh) + GENL_HDRLEN +
751 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
752 TIPC_GENL_HDRLEN), hdr_space);
755 skb_push(rep_buf, hdr_space);
756 rep_nlh = nlmsg_hdr(rep_buf);
757 memcpy(rep_nlh, req_nlh, hdr_space);
758 rep_nlh->nlmsg_len = rep_buf->len;
759 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
765 /* Temporary function to keep functionality throughout the patchset
766 * without having to mess with the global variables and other trickery
769 static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
771 struct tipc_genlmsghdr *req = info->userhdr;
774 case TIPC_CMD_GET_BEARER_NAMES:
775 case TIPC_CMD_ENABLE_BEARER:
776 case TIPC_CMD_DISABLE_BEARER:
777 case TIPC_CMD_SHOW_LINK_STATS:
778 case TIPC_CMD_GET_LINKS:
779 case TIPC_CMD_SET_LINK_TOL:
780 case TIPC_CMD_SET_LINK_PRI:
781 case TIPC_CMD_SET_LINK_WINDOW:
782 case TIPC_CMD_RESET_LINK_STATS:
783 return tipc_nl_compat_recv(skb, info);
786 return handle_cmd(skb, info);
789 static struct genl_family tipc_genl_compat_family = {
790 .id = GENL_ID_GENERATE,
791 .name = TIPC_GENL_NAME,
792 .version = TIPC_GENL_VERSION,
793 .hdrsize = TIPC_GENL_HDRLEN,
798 static struct genl_ops tipc_genl_compat_ops[] = {
800 .cmd = TIPC_GENL_CMD,
801 .doit = tipc_nl_compat_tmp_wrap,
805 int tipc_netlink_compat_start(void)
809 res = genl_register_family_with_ops(&tipc_genl_compat_family,
810 tipc_genl_compat_ops);
812 pr_err("Failed to register legacy compat interface\n");
819 void tipc_netlink_compat_stop(void)
821 genl_unregister_family(&tipc_genl_compat_family);