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
;
188 if (__tipc_dump_start(&cb
, msg
->net
)) {
196 len
= (*cmd
->dumpit
)(buf
, &cb
);
198 nlmsg_for_each_msg(nlmsg
, nlmsg_hdr(buf
), len
, rem
) {
199 struct nlattr
**attrs
;
201 err
= tipc_nlmsg_parse(nlmsg
, &attrs
);
205 err
= (*cmd
->format
)(msg
, attrs
);
209 if (tipc_skb_tailroom(msg
->rep
) <= 1) {
215 skb_reset_tail_pointer(buf
);
226 if (err
== -EMSGSIZE
) {
227 /* The legacy API only considered messages filling
228 * "ULTRA_STRING_MAX_LEN" to be truncated.
230 if ((TIPC_SKB_MAX
- msg
->rep
->len
) <= 1) {
231 char *tail
= skb_tail_pointer(msg
->rep
);
234 sprintf(tail
- sizeof(REPLY_TRUNCATED
) - 1,
244 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump
*cmd
,
245 struct tipc_nl_compat_msg
*msg
)
250 if (msg
->req_type
&& !TLV_CHECK_TYPE(msg
->req
, msg
->req_type
))
253 msg
->rep
= tipc_tlv_alloc(msg
->rep_size
);
258 tipc_tlv_init(msg
->rep
, msg
->rep_type
);
263 arg
= nlmsg_new(0, GFP_KERNEL
);
270 err
= __tipc_nl_compat_dumpit(cmd
, msg
, arg
);
280 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit
*cmd
,
281 struct tipc_nl_compat_msg
*msg
)
284 struct sk_buff
*doit_buf
;
285 struct sk_buff
*trans_buf
;
286 struct nlattr
**attrbuf
;
287 struct genl_info info
;
289 trans_buf
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
293 attrbuf
= kmalloc_array(tipc_genl_family
.maxattr
+ 1,
294 sizeof(struct nlattr
*),
301 doit_buf
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
307 memset(&info
, 0, sizeof(info
));
308 info
.attrs
= attrbuf
;
311 err
= (*cmd
->transcode
)(cmd
, trans_buf
, msg
);
315 err
= nla_parse(attrbuf
, tipc_genl_family
.maxattr
,
316 (const struct nlattr
*)trans_buf
->data
,
317 trans_buf
->len
, NULL
, NULL
);
321 doit_buf
->sk
= msg
->dst_sk
;
323 err
= (*cmd
->doit
)(doit_buf
, &info
);
331 kfree_skb(trans_buf
);
336 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit
*cmd
,
337 struct tipc_nl_compat_msg
*msg
)
341 if (msg
->req_type
&& !TLV_CHECK_TYPE(msg
->req
, msg
->req_type
))
344 err
= __tipc_nl_compat_doit(cmd
, msg
);
348 /* The legacy API considered an empty message a success message */
349 msg
->rep
= tipc_tlv_alloc(0);
356 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg
*msg
,
357 struct nlattr
**attrs
)
359 struct nlattr
*bearer
[TIPC_NLA_BEARER_MAX
+ 1];
362 if (!attrs
[TIPC_NLA_BEARER
])
365 err
= nla_parse_nested(bearer
, TIPC_NLA_BEARER_MAX
,
366 attrs
[TIPC_NLA_BEARER
], NULL
, NULL
);
370 return tipc_add_tlv(msg
->rep
, TIPC_TLV_BEARER_NAME
,
371 nla_data(bearer
[TIPC_NLA_BEARER_NAME
]),
372 nla_len(bearer
[TIPC_NLA_BEARER_NAME
]));
375 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit
*cmd
,
377 struct tipc_nl_compat_msg
*msg
)
380 struct nlattr
*bearer
;
381 struct tipc_bearer_config
*b
;
383 b
= (struct tipc_bearer_config
*)TLV_DATA(msg
->req
);
385 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
389 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, b
->name
))
392 if (nla_put_u32(skb
, TIPC_NLA_BEARER_DOMAIN
, ntohl(b
->disc_domain
)))
395 if (ntohl(b
->priority
) <= TIPC_MAX_LINK_PRI
) {
396 prop
= nla_nest_start(skb
, TIPC_NLA_BEARER_PROP
);
399 if (nla_put_u32(skb
, TIPC_NLA_PROP_PRIO
, ntohl(b
->priority
)))
401 nla_nest_end(skb
, prop
);
403 nla_nest_end(skb
, bearer
);
408 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit
*cmd
,
410 struct tipc_nl_compat_msg
*msg
)
413 struct nlattr
*bearer
;
415 name
= (char *)TLV_DATA(msg
->req
);
417 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
421 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, name
))
424 nla_nest_end(skb
, bearer
);
429 static inline u32
perc(u32 count
, u32 total
)
431 return (count
* 100 + (total
/ 2)) / total
;
434 static void __fill_bc_link_stat(struct tipc_nl_compat_msg
*msg
,
435 struct nlattr
*prop
[], struct nlattr
*stats
[])
437 tipc_tlv_sprintf(msg
->rep
, " Window:%u packets\n",
438 nla_get_u32(prop
[TIPC_NLA_PROP_WIN
]));
440 tipc_tlv_sprintf(msg
->rep
,
441 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
442 nla_get_u32(stats
[TIPC_NLA_STATS_RX_INFO
]),
443 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTS
]),
444 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTED
]),
445 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLES
]),
446 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLED
]));
448 tipc_tlv_sprintf(msg
->rep
,
449 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
450 nla_get_u32(stats
[TIPC_NLA_STATS_TX_INFO
]),
451 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTS
]),
452 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTED
]),
453 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLES
]),
454 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLED
]));
456 tipc_tlv_sprintf(msg
->rep
, " RX naks:%u defs:%u dups:%u\n",
457 nla_get_u32(stats
[TIPC_NLA_STATS_RX_NACKS
]),
458 nla_get_u32(stats
[TIPC_NLA_STATS_RX_DEFERRED
]),
459 nla_get_u32(stats
[TIPC_NLA_STATS_DUPLICATES
]));
461 tipc_tlv_sprintf(msg
->rep
, " TX naks:%u acks:%u dups:%u\n",
462 nla_get_u32(stats
[TIPC_NLA_STATS_TX_NACKS
]),
463 nla_get_u32(stats
[TIPC_NLA_STATS_TX_ACKS
]),
464 nla_get_u32(stats
[TIPC_NLA_STATS_RETRANSMITTED
]));
466 tipc_tlv_sprintf(msg
->rep
,
467 " Congestion link:%u Send queue max:%u avg:%u",
468 nla_get_u32(stats
[TIPC_NLA_STATS_LINK_CONGS
]),
469 nla_get_u32(stats
[TIPC_NLA_STATS_MAX_QUEUE
]),
470 nla_get_u32(stats
[TIPC_NLA_STATS_AVG_QUEUE
]));
473 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg
*msg
,
474 struct nlattr
**attrs
)
477 struct nlattr
*link
[TIPC_NLA_LINK_MAX
+ 1];
478 struct nlattr
*prop
[TIPC_NLA_PROP_MAX
+ 1];
479 struct nlattr
*stats
[TIPC_NLA_STATS_MAX
+ 1];
482 if (!attrs
[TIPC_NLA_LINK
])
485 err
= nla_parse_nested(link
, TIPC_NLA_LINK_MAX
, attrs
[TIPC_NLA_LINK
],
490 if (!link
[TIPC_NLA_LINK_PROP
])
493 err
= nla_parse_nested(prop
, TIPC_NLA_PROP_MAX
,
494 link
[TIPC_NLA_LINK_PROP
], NULL
, NULL
);
498 if (!link
[TIPC_NLA_LINK_STATS
])
501 err
= nla_parse_nested(stats
, TIPC_NLA_STATS_MAX
,
502 link
[TIPC_NLA_LINK_STATS
], NULL
, NULL
);
506 name
= (char *)TLV_DATA(msg
->req
);
507 if (strcmp(name
, nla_data(link
[TIPC_NLA_LINK_NAME
])) != 0)
510 tipc_tlv_sprintf(msg
->rep
, "\nLink <%s>\n",
511 nla_data(link
[TIPC_NLA_LINK_NAME
]));
513 if (link
[TIPC_NLA_LINK_BROADCAST
]) {
514 __fill_bc_link_stat(msg
, prop
, stats
);
518 if (link
[TIPC_NLA_LINK_ACTIVE
])
519 tipc_tlv_sprintf(msg
->rep
, " ACTIVE");
520 else if (link
[TIPC_NLA_LINK_UP
])
521 tipc_tlv_sprintf(msg
->rep
, " STANDBY");
523 tipc_tlv_sprintf(msg
->rep
, " DEFUNCT");
525 tipc_tlv_sprintf(msg
->rep
, " MTU:%u Priority:%u",
526 nla_get_u32(link
[TIPC_NLA_LINK_MTU
]),
527 nla_get_u32(prop
[TIPC_NLA_PROP_PRIO
]));
529 tipc_tlv_sprintf(msg
->rep
, " Tolerance:%u ms Window:%u packets\n",
530 nla_get_u32(prop
[TIPC_NLA_PROP_TOL
]),
531 nla_get_u32(prop
[TIPC_NLA_PROP_WIN
]));
533 tipc_tlv_sprintf(msg
->rep
,
534 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
535 nla_get_u32(link
[TIPC_NLA_LINK_RX
]) -
536 nla_get_u32(stats
[TIPC_NLA_STATS_RX_INFO
]),
537 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTS
]),
538 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTED
]),
539 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLES
]),
540 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLED
]));
542 tipc_tlv_sprintf(msg
->rep
,
543 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
544 nla_get_u32(link
[TIPC_NLA_LINK_TX
]) -
545 nla_get_u32(stats
[TIPC_NLA_STATS_TX_INFO
]),
546 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTS
]),
547 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTED
]),
548 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLES
]),
549 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLED
]));
551 tipc_tlv_sprintf(msg
->rep
,
552 " TX profile sample:%u packets average:%u octets\n",
553 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_CNT
]),
554 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_TOT
]) /
555 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
]));
557 tipc_tlv_sprintf(msg
->rep
,
558 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
559 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P0
]),
560 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
561 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P1
]),
562 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
563 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P2
]),
564 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
565 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P3
]),
566 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])));
568 tipc_tlv_sprintf(msg
->rep
, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
569 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P4
]),
570 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
571 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P5
]),
572 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
573 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P6
]),
574 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])));
576 tipc_tlv_sprintf(msg
->rep
,
577 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
578 nla_get_u32(stats
[TIPC_NLA_STATS_RX_STATES
]),
579 nla_get_u32(stats
[TIPC_NLA_STATS_RX_PROBES
]),
580 nla_get_u32(stats
[TIPC_NLA_STATS_RX_NACKS
]),
581 nla_get_u32(stats
[TIPC_NLA_STATS_RX_DEFERRED
]),
582 nla_get_u32(stats
[TIPC_NLA_STATS_DUPLICATES
]));
584 tipc_tlv_sprintf(msg
->rep
,
585 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
586 nla_get_u32(stats
[TIPC_NLA_STATS_TX_STATES
]),
587 nla_get_u32(stats
[TIPC_NLA_STATS_TX_PROBES
]),
588 nla_get_u32(stats
[TIPC_NLA_STATS_TX_NACKS
]),
589 nla_get_u32(stats
[TIPC_NLA_STATS_TX_ACKS
]),
590 nla_get_u32(stats
[TIPC_NLA_STATS_RETRANSMITTED
]));
592 tipc_tlv_sprintf(msg
->rep
,
593 " Congestion link:%u Send queue max:%u avg:%u",
594 nla_get_u32(stats
[TIPC_NLA_STATS_LINK_CONGS
]),
595 nla_get_u32(stats
[TIPC_NLA_STATS_MAX_QUEUE
]),
596 nla_get_u32(stats
[TIPC_NLA_STATS_AVG_QUEUE
]));
601 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg
*msg
,
602 struct nlattr
**attrs
)
604 struct nlattr
*link
[TIPC_NLA_LINK_MAX
+ 1];
605 struct tipc_link_info link_info
;
608 if (!attrs
[TIPC_NLA_LINK
])
611 err
= nla_parse_nested(link
, TIPC_NLA_LINK_MAX
, attrs
[TIPC_NLA_LINK
],
616 link_info
.dest
= nla_get_flag(link
[TIPC_NLA_LINK_DEST
]);
617 link_info
.up
= htonl(nla_get_flag(link
[TIPC_NLA_LINK_UP
]));
618 nla_strlcpy(link_info
.str
, link
[TIPC_NLA_LINK_NAME
],
621 return tipc_add_tlv(msg
->rep
, TIPC_TLV_LINK_INFO
,
622 &link_info
, sizeof(link_info
));
625 static int __tipc_add_link_prop(struct sk_buff
*skb
,
626 struct tipc_nl_compat_msg
*msg
,
627 struct tipc_link_config
*lc
)
630 case TIPC_CMD_SET_LINK_PRI
:
631 return nla_put_u32(skb
, TIPC_NLA_PROP_PRIO
, ntohl(lc
->value
));
632 case TIPC_CMD_SET_LINK_TOL
:
633 return nla_put_u32(skb
, TIPC_NLA_PROP_TOL
, ntohl(lc
->value
));
634 case TIPC_CMD_SET_LINK_WINDOW
:
635 return nla_put_u32(skb
, TIPC_NLA_PROP_WIN
, ntohl(lc
->value
));
641 static int tipc_nl_compat_media_set(struct sk_buff
*skb
,
642 struct tipc_nl_compat_msg
*msg
)
645 struct nlattr
*media
;
646 struct tipc_link_config
*lc
;
648 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
650 media
= nla_nest_start(skb
, TIPC_NLA_MEDIA
);
654 if (nla_put_string(skb
, TIPC_NLA_MEDIA_NAME
, lc
->name
))
657 prop
= nla_nest_start(skb
, TIPC_NLA_MEDIA_PROP
);
661 __tipc_add_link_prop(skb
, msg
, lc
);
662 nla_nest_end(skb
, prop
);
663 nla_nest_end(skb
, media
);
668 static int tipc_nl_compat_bearer_set(struct sk_buff
*skb
,
669 struct tipc_nl_compat_msg
*msg
)
672 struct nlattr
*bearer
;
673 struct tipc_link_config
*lc
;
675 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
677 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
681 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, lc
->name
))
684 prop
= nla_nest_start(skb
, TIPC_NLA_BEARER_PROP
);
688 __tipc_add_link_prop(skb
, msg
, lc
);
689 nla_nest_end(skb
, prop
);
690 nla_nest_end(skb
, bearer
);
695 static int __tipc_nl_compat_link_set(struct sk_buff
*skb
,
696 struct tipc_nl_compat_msg
*msg
)
700 struct tipc_link_config
*lc
;
702 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
704 link
= nla_nest_start(skb
, TIPC_NLA_LINK
);
708 if (nla_put_string(skb
, TIPC_NLA_LINK_NAME
, lc
->name
))
711 prop
= nla_nest_start(skb
, TIPC_NLA_LINK_PROP
);
715 __tipc_add_link_prop(skb
, msg
, lc
);
716 nla_nest_end(skb
, prop
);
717 nla_nest_end(skb
, link
);
722 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit
*cmd
,
724 struct tipc_nl_compat_msg
*msg
)
726 struct tipc_link_config
*lc
;
727 struct tipc_bearer
*bearer
;
728 struct tipc_media
*media
;
730 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
732 media
= tipc_media_find(lc
->name
);
734 cmd
->doit
= &__tipc_nl_media_set
;
735 return tipc_nl_compat_media_set(skb
, msg
);
738 bearer
= tipc_bearer_find(msg
->net
, lc
->name
);
740 cmd
->doit
= &__tipc_nl_bearer_set
;
741 return tipc_nl_compat_bearer_set(skb
, msg
);
744 return __tipc_nl_compat_link_set(skb
, msg
);
747 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit
*cmd
,
749 struct tipc_nl_compat_msg
*msg
)
754 name
= (char *)TLV_DATA(msg
->req
);
756 link
= nla_nest_start(skb
, TIPC_NLA_LINK
);
760 if (nla_put_string(skb
, TIPC_NLA_LINK_NAME
, name
))
763 nla_nest_end(skb
, link
);
768 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg
*msg
)
772 struct tipc_name_table_query
*ntq
;
773 static const char * const header
[] = {
780 ntq
= (struct tipc_name_table_query
*)TLV_DATA(msg
->req
);
782 depth
= ntohl(ntq
->depth
);
786 for (i
= 0; i
< depth
; i
++)
787 tipc_tlv_sprintf(msg
->rep
, header
[i
]);
788 tipc_tlv_sprintf(msg
->rep
, "\n");
793 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg
*msg
,
794 struct nlattr
**attrs
)
797 struct tipc_name_table_query
*ntq
;
798 struct nlattr
*nt
[TIPC_NLA_NAME_TABLE_MAX
+ 1];
799 struct nlattr
*publ
[TIPC_NLA_PUBL_MAX
+ 1];
800 u32 node
, depth
, type
, lowbound
, upbound
;
801 static const char * const scope_str
[] = {"", " zone", " cluster",
805 if (!attrs
[TIPC_NLA_NAME_TABLE
])
808 err
= nla_parse_nested(nt
, TIPC_NLA_NAME_TABLE_MAX
,
809 attrs
[TIPC_NLA_NAME_TABLE
], NULL
, NULL
);
813 if (!nt
[TIPC_NLA_NAME_TABLE_PUBL
])
816 err
= nla_parse_nested(publ
, TIPC_NLA_PUBL_MAX
,
817 nt
[TIPC_NLA_NAME_TABLE_PUBL
], NULL
, NULL
);
821 ntq
= (struct tipc_name_table_query
*)TLV_DATA(msg
->req
);
823 depth
= ntohl(ntq
->depth
);
824 type
= ntohl(ntq
->type
);
825 lowbound
= ntohl(ntq
->lowbound
);
826 upbound
= ntohl(ntq
->upbound
);
828 if (!(depth
& TIPC_NTQ_ALLTYPES
) &&
829 (type
!= nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
])))
831 if (lowbound
&& (lowbound
> nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
])))
833 if (upbound
&& (upbound
< nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
])))
836 tipc_tlv_sprintf(msg
->rep
, "%-10u ",
837 nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
]));
842 tipc_tlv_sprintf(msg
->rep
, "%-10u %-10u ",
843 nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
]),
844 nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
]));
849 node
= nla_get_u32(publ
[TIPC_NLA_PUBL_NODE
]);
850 sprintf(port_str
, "<%u.%u.%u:%u>", tipc_zone(node
), tipc_cluster(node
),
851 tipc_node(node
), nla_get_u32(publ
[TIPC_NLA_PUBL_REF
]));
852 tipc_tlv_sprintf(msg
->rep
, "%-26s ", port_str
);
857 tipc_tlv_sprintf(msg
->rep
, "%-10u %s",
858 nla_get_u32(publ
[TIPC_NLA_PUBL_KEY
]),
859 scope_str
[nla_get_u32(publ
[TIPC_NLA_PUBL_SCOPE
])]);
861 tipc_tlv_sprintf(msg
->rep
, "\n");
866 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg
*msg
,
867 struct nlattr
**attrs
)
869 u32 type
, lower
, upper
;
870 struct nlattr
*publ
[TIPC_NLA_PUBL_MAX
+ 1];
873 if (!attrs
[TIPC_NLA_PUBL
])
876 err
= nla_parse_nested(publ
, TIPC_NLA_PUBL_MAX
, attrs
[TIPC_NLA_PUBL
],
881 type
= nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
]);
882 lower
= nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
]);
883 upper
= nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
]);
886 tipc_tlv_sprintf(msg
->rep
, " {%u,%u}", type
, lower
);
888 tipc_tlv_sprintf(msg
->rep
, " {%u,%u,%u}", type
, lower
, upper
);
893 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg
*msg
, u32 sock
)
898 struct sk_buff
*args
;
899 struct tipc_nl_compat_cmd_dump dump
;
901 args
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
905 hdr
= genlmsg_put(args
, 0, 0, &tipc_genl_family
, NLM_F_MULTI
,
908 nest
= nla_nest_start(args
, TIPC_NLA_SOCK
);
914 if (nla_put_u32(args
, TIPC_NLA_SOCK_REF
, sock
)) {
919 nla_nest_end(args
, nest
);
920 genlmsg_end(args
, hdr
);
922 dump
.dumpit
= tipc_nl_publ_dump
;
923 dump
.format
= __tipc_nl_compat_publ_dump
;
925 err
= __tipc_nl_compat_dumpit(&dump
, msg
, args
);
932 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg
*msg
,
933 struct nlattr
**attrs
)
937 struct nlattr
*sock
[TIPC_NLA_SOCK_MAX
+ 1];
939 if (!attrs
[TIPC_NLA_SOCK
])
942 err
= nla_parse_nested(sock
, TIPC_NLA_SOCK_MAX
, attrs
[TIPC_NLA_SOCK
],
947 sock_ref
= nla_get_u32(sock
[TIPC_NLA_SOCK_REF
]);
948 tipc_tlv_sprintf(msg
->rep
, "%u:", sock_ref
);
950 if (sock
[TIPC_NLA_SOCK_CON
]) {
952 struct nlattr
*con
[TIPC_NLA_CON_MAX
+ 1];
954 nla_parse_nested(con
, TIPC_NLA_CON_MAX
,
955 sock
[TIPC_NLA_SOCK_CON
], NULL
, NULL
);
957 node
= nla_get_u32(con
[TIPC_NLA_CON_NODE
]);
958 tipc_tlv_sprintf(msg
->rep
, " connected to <%u.%u.%u:%u>",
962 nla_get_u32(con
[TIPC_NLA_CON_SOCK
]));
964 if (con
[TIPC_NLA_CON_FLAG
])
965 tipc_tlv_sprintf(msg
->rep
, " via {%u,%u}\n",
966 nla_get_u32(con
[TIPC_NLA_CON_TYPE
]),
967 nla_get_u32(con
[TIPC_NLA_CON_INST
]));
969 tipc_tlv_sprintf(msg
->rep
, "\n");
970 } else if (sock
[TIPC_NLA_SOCK_HAS_PUBL
]) {
971 tipc_tlv_sprintf(msg
->rep
, " bound to");
973 err
= tipc_nl_compat_publ_dump(msg
, sock_ref
);
977 tipc_tlv_sprintf(msg
->rep
, "\n");
982 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg
*msg
,
983 struct nlattr
**attrs
)
985 struct nlattr
*media
[TIPC_NLA_MEDIA_MAX
+ 1];
988 if (!attrs
[TIPC_NLA_MEDIA
])
991 err
= nla_parse_nested(media
, TIPC_NLA_MEDIA_MAX
,
992 attrs
[TIPC_NLA_MEDIA
], NULL
, NULL
);
996 return tipc_add_tlv(msg
->rep
, TIPC_TLV_MEDIA_NAME
,
997 nla_data(media
[TIPC_NLA_MEDIA_NAME
]),
998 nla_len(media
[TIPC_NLA_MEDIA_NAME
]));
1001 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg
*msg
,
1002 struct nlattr
**attrs
)
1004 struct tipc_node_info node_info
;
1005 struct nlattr
*node
[TIPC_NLA_NODE_MAX
+ 1];
1008 if (!attrs
[TIPC_NLA_NODE
])
1011 err
= nla_parse_nested(node
, TIPC_NLA_NODE_MAX
, attrs
[TIPC_NLA_NODE
],
1016 node_info
.addr
= htonl(nla_get_u32(node
[TIPC_NLA_NODE_ADDR
]));
1017 node_info
.up
= htonl(nla_get_flag(node
[TIPC_NLA_NODE_UP
]));
1019 return tipc_add_tlv(msg
->rep
, TIPC_TLV_NODE_INFO
, &node_info
,
1023 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit
*cmd
,
1024 struct sk_buff
*skb
,
1025 struct tipc_nl_compat_msg
*msg
)
1030 val
= ntohl(*(__be32
*)TLV_DATA(msg
->req
));
1032 net
= nla_nest_start(skb
, TIPC_NLA_NET
);
1036 if (msg
->cmd
== TIPC_CMD_SET_NODE_ADDR
) {
1037 if (nla_put_u32(skb
, TIPC_NLA_NET_ADDR
, val
))
1039 } else if (msg
->cmd
== TIPC_CMD_SET_NETID
) {
1040 if (nla_put_u32(skb
, TIPC_NLA_NET_ID
, val
))
1043 nla_nest_end(skb
, net
);
1048 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg
*msg
,
1049 struct nlattr
**attrs
)
1052 struct nlattr
*net
[TIPC_NLA_NET_MAX
+ 1];
1055 if (!attrs
[TIPC_NLA_NET
])
1058 err
= nla_parse_nested(net
, TIPC_NLA_NET_MAX
, attrs
[TIPC_NLA_NET
],
1063 id
= htonl(nla_get_u32(net
[TIPC_NLA_NET_ID
]));
1065 return tipc_add_tlv(msg
->rep
, TIPC_TLV_UNSIGNED
, &id
, sizeof(id
));
1068 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg
*msg
)
1070 msg
->rep
= tipc_tlv_alloc(ULTRA_STRING_MAX_LEN
);
1074 tipc_tlv_init(msg
->rep
, TIPC_TLV_ULTRA_STRING
);
1075 tipc_tlv_sprintf(msg
->rep
, "TIPC version " TIPC_MOD_VER
"\n");
1080 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg
*msg
)
1082 struct tipc_nl_compat_cmd_dump dump
;
1083 struct tipc_nl_compat_cmd_doit doit
;
1085 memset(&dump
, 0, sizeof(dump
));
1086 memset(&doit
, 0, sizeof(doit
));
1090 msg
->rep
= tipc_tlv_alloc(0);
1094 case TIPC_CMD_GET_BEARER_NAMES
:
1095 msg
->rep_size
= MAX_BEARERS
* TLV_SPACE(TIPC_MAX_BEARER_NAME
);
1096 dump
.dumpit
= tipc_nl_bearer_dump
;
1097 dump
.format
= tipc_nl_compat_bearer_dump
;
1098 return tipc_nl_compat_dumpit(&dump
, msg
);
1099 case TIPC_CMD_ENABLE_BEARER
:
1100 msg
->req_type
= TIPC_TLV_BEARER_CONFIG
;
1101 doit
.doit
= __tipc_nl_bearer_enable
;
1102 doit
.transcode
= tipc_nl_compat_bearer_enable
;
1103 return tipc_nl_compat_doit(&doit
, msg
);
1104 case TIPC_CMD_DISABLE_BEARER
:
1105 msg
->req_type
= TIPC_TLV_BEARER_NAME
;
1106 doit
.doit
= __tipc_nl_bearer_disable
;
1107 doit
.transcode
= tipc_nl_compat_bearer_disable
;
1108 return tipc_nl_compat_doit(&doit
, msg
);
1109 case TIPC_CMD_SHOW_LINK_STATS
:
1110 msg
->req_type
= TIPC_TLV_LINK_NAME
;
1111 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1112 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1113 dump
.dumpit
= tipc_nl_node_dump_link
;
1114 dump
.format
= tipc_nl_compat_link_stat_dump
;
1115 return tipc_nl_compat_dumpit(&dump
, msg
);
1116 case TIPC_CMD_GET_LINKS
:
1117 msg
->req_type
= TIPC_TLV_NET_ADDR
;
1118 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1119 dump
.dumpit
= tipc_nl_node_dump_link
;
1120 dump
.format
= tipc_nl_compat_link_dump
;
1121 return tipc_nl_compat_dumpit(&dump
, msg
);
1122 case TIPC_CMD_SET_LINK_TOL
:
1123 case TIPC_CMD_SET_LINK_PRI
:
1124 case TIPC_CMD_SET_LINK_WINDOW
:
1125 msg
->req_type
= TIPC_TLV_LINK_CONFIG
;
1126 doit
.doit
= tipc_nl_node_set_link
;
1127 doit
.transcode
= tipc_nl_compat_link_set
;
1128 return tipc_nl_compat_doit(&doit
, msg
);
1129 case TIPC_CMD_RESET_LINK_STATS
:
1130 msg
->req_type
= TIPC_TLV_LINK_NAME
;
1131 doit
.doit
= tipc_nl_node_reset_link_stats
;
1132 doit
.transcode
= tipc_nl_compat_link_reset_stats
;
1133 return tipc_nl_compat_doit(&doit
, msg
);
1134 case TIPC_CMD_SHOW_NAME_TABLE
:
1135 msg
->req_type
= TIPC_TLV_NAME_TBL_QUERY
;
1136 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1137 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1138 dump
.header
= tipc_nl_compat_name_table_dump_header
;
1139 dump
.dumpit
= tipc_nl_name_table_dump
;
1140 dump
.format
= tipc_nl_compat_name_table_dump
;
1141 return tipc_nl_compat_dumpit(&dump
, msg
);
1142 case TIPC_CMD_SHOW_PORTS
:
1143 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1144 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1145 dump
.dumpit
= tipc_nl_sk_dump
;
1146 dump
.format
= tipc_nl_compat_sk_dump
;
1147 return tipc_nl_compat_dumpit(&dump
, msg
);
1148 case TIPC_CMD_GET_MEDIA_NAMES
:
1149 msg
->rep_size
= MAX_MEDIA
* TLV_SPACE(TIPC_MAX_MEDIA_NAME
);
1150 dump
.dumpit
= tipc_nl_media_dump
;
1151 dump
.format
= tipc_nl_compat_media_dump
;
1152 return tipc_nl_compat_dumpit(&dump
, msg
);
1153 case TIPC_CMD_GET_NODES
:
1154 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1155 dump
.dumpit
= tipc_nl_node_dump
;
1156 dump
.format
= tipc_nl_compat_node_dump
;
1157 return tipc_nl_compat_dumpit(&dump
, msg
);
1158 case TIPC_CMD_SET_NODE_ADDR
:
1159 msg
->req_type
= TIPC_TLV_NET_ADDR
;
1160 doit
.doit
= __tipc_nl_net_set
;
1161 doit
.transcode
= tipc_nl_compat_net_set
;
1162 return tipc_nl_compat_doit(&doit
, msg
);
1163 case TIPC_CMD_SET_NETID
:
1164 msg
->req_type
= TIPC_TLV_UNSIGNED
;
1165 doit
.doit
= __tipc_nl_net_set
;
1166 doit
.transcode
= tipc_nl_compat_net_set
;
1167 return tipc_nl_compat_doit(&doit
, msg
);
1168 case TIPC_CMD_GET_NETID
:
1169 msg
->rep_size
= sizeof(u32
);
1170 dump
.dumpit
= tipc_nl_net_dump
;
1171 dump
.format
= tipc_nl_compat_net_dump
;
1172 return tipc_nl_compat_dumpit(&dump
, msg
);
1173 case TIPC_CMD_SHOW_STATS
:
1174 return tipc_cmd_show_stats_compat(msg
);
1180 static int tipc_nl_compat_recv(struct sk_buff
*skb
, struct genl_info
*info
)
1184 struct tipc_nl_compat_msg msg
;
1185 struct nlmsghdr
*req_nlh
;
1186 struct nlmsghdr
*rep_nlh
;
1187 struct tipc_genlmsghdr
*req_userhdr
= info
->userhdr
;
1189 memset(&msg
, 0, sizeof(msg
));
1191 req_nlh
= (struct nlmsghdr
*)skb
->data
;
1192 msg
.req
= nlmsg_data(req_nlh
) + GENL_HDRLEN
+ TIPC_GENL_HDRLEN
;
1193 msg
.cmd
= req_userhdr
->cmd
;
1194 msg
.net
= genl_info_net(info
);
1195 msg
.dst_sk
= skb
->sk
;
1197 if ((msg
.cmd
& 0xC000) && (!netlink_net_capable(skb
, CAP_NET_ADMIN
))) {
1198 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN
);
1203 len
= nlmsg_attrlen(req_nlh
, GENL_HDRLEN
+ TIPC_GENL_HDRLEN
);
1204 if (len
&& !TLV_OK(msg
.req
, len
)) {
1205 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED
);
1210 err
= tipc_nl_compat_handle(&msg
);
1211 if ((err
== -EOPNOTSUPP
) || (err
== -EPERM
))
1212 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED
);
1213 else if (err
== -EINVAL
)
1214 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_TLV_ERROR
);
1219 len
= nlmsg_total_size(GENL_HDRLEN
+ TIPC_GENL_HDRLEN
);
1220 skb_push(msg
.rep
, len
);
1221 rep_nlh
= nlmsg_hdr(msg
.rep
);
1222 memcpy(rep_nlh
, info
->nlhdr
, len
);
1223 rep_nlh
->nlmsg_len
= msg
.rep
->len
;
1224 genlmsg_unicast(msg
.net
, msg
.rep
, NETLINK_CB(skb
).portid
);
1229 static const struct genl_ops tipc_genl_compat_ops
[] = {
1231 .cmd
= TIPC_GENL_CMD
,
1232 .doit
= tipc_nl_compat_recv
,
1236 static struct genl_family tipc_genl_compat_family __ro_after_init
= {
1237 .name
= TIPC_GENL_NAME
,
1238 .version
= TIPC_GENL_VERSION
,
1239 .hdrsize
= TIPC_GENL_HDRLEN
,
1242 .module
= THIS_MODULE
,
1243 .ops
= tipc_genl_compat_ops
,
1244 .n_ops
= ARRAY_SIZE(tipc_genl_compat_ops
),
1247 int __init
tipc_netlink_compat_start(void)
1251 res
= genl_register_family(&tipc_genl_compat_family
);
1253 pr_err("Failed to register legacy compat interface\n");
1260 void tipc_netlink_compat_stop(void)
1262 genl_unregister_family(&tipc_genl_compat_family
);