2 * Copyright (c) 2014, Ericsson AB
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the names of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 #include "name_table.h"
41 #include <net/genetlink.h>
42 #include <linux/tipc_config.h>
44 /* The legacy API had an artificial message length limit called
45 * ULTRA_STRING_MAX_LEN.
47 #define ULTRA_STRING_MAX_LEN 32768
49 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
51 #define REPLY_TRUNCATED "<truncated>\n"
53 struct tipc_nl_compat_msg
{
64 struct tipc_nl_compat_cmd_dump
{
65 int (*header
)(struct tipc_nl_compat_msg
*);
66 int (*dumpit
)(struct sk_buff
*, struct netlink_callback
*);
67 int (*format
)(struct tipc_nl_compat_msg
*msg
, struct nlattr
**attrs
);
70 struct tipc_nl_compat_cmd_doit
{
71 int (*doit
)(struct sk_buff
*skb
, struct genl_info
*info
);
72 int (*transcode
)(struct tipc_nl_compat_cmd_doit
*cmd
,
73 struct sk_buff
*skb
, struct tipc_nl_compat_msg
*msg
);
76 static int tipc_skb_tailroom(struct sk_buff
*skb
)
81 tailroom
= skb_tailroom(skb
);
82 limit
= TIPC_SKB_MAX
- skb
->len
;
90 static int tipc_add_tlv(struct sk_buff
*skb
, u16 type
, void *data
, u16 len
)
92 struct tlv_desc
*tlv
= (struct tlv_desc
*)skb_tail_pointer(skb
);
94 if (tipc_skb_tailroom(skb
) < TLV_SPACE(len
))
97 skb_put(skb
, TLV_SPACE(len
));
98 tlv
->tlv_type
= htons(type
);
99 tlv
->tlv_len
= htons(TLV_LENGTH(len
));
101 memcpy(TLV_DATA(tlv
), data
, len
);
106 static void tipc_tlv_init(struct sk_buff
*skb
, u16 type
)
108 struct tlv_desc
*tlv
= (struct tlv_desc
*)skb
->data
;
111 TLV_SET_TYPE(tlv
, type
);
112 skb_put(skb
, sizeof(struct tlv_desc
));
115 static int tipc_tlv_sprintf(struct sk_buff
*skb
, const char *fmt
, ...)
121 struct tlv_desc
*tlv
;
124 rem
= tipc_skb_tailroom(skb
);
126 tlv
= (struct tlv_desc
*)skb
->data
;
127 len
= TLV_GET_LEN(tlv
);
128 buf
= TLV_DATA(tlv
) + len
;
131 n
= vscnprintf(buf
, rem
, fmt
, args
);
134 TLV_SET_LEN(tlv
, n
+ len
);
140 static struct sk_buff
*tipc_tlv_alloc(int size
)
145 size
= TLV_SPACE(size
);
146 hdr_len
= nlmsg_total_size(GENL_HDRLEN
+ TIPC_GENL_HDRLEN
);
148 buf
= alloc_skb(hdr_len
+ size
, GFP_KERNEL
);
152 skb_reserve(buf
, hdr_len
);
157 static struct sk_buff
*tipc_get_err_tlv(char *str
)
159 int str_len
= strlen(str
) + 1;
162 buf
= tipc_tlv_alloc(TLV_SPACE(str_len
));
164 tipc_add_tlv(buf
, TIPC_TLV_ERROR_STRING
, str
, str_len
);
169 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump
*cmd
,
170 struct tipc_nl_compat_msg
*msg
,
176 struct nlmsghdr
*nlmsg
;
177 struct netlink_callback cb
;
179 memset(&cb
, 0, sizeof(cb
));
180 cb
.nlh
= (struct nlmsghdr
*)arg
->data
;
183 buf
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
187 buf
->sk
= msg
->dst_sk
;
192 len
= (*cmd
->dumpit
)(buf
, &cb
);
194 nlmsg_for_each_msg(nlmsg
, nlmsg_hdr(buf
), len
, rem
) {
195 struct nlattr
**attrs
;
197 err
= tipc_nlmsg_parse(nlmsg
, &attrs
);
201 err
= (*cmd
->format
)(msg
, attrs
);
205 if (tipc_skb_tailroom(msg
->rep
) <= 1) {
211 skb_reset_tail_pointer(buf
);
221 if (err
== -EMSGSIZE
) {
222 /* The legacy API only considered messages filling
223 * "ULTRA_STRING_MAX_LEN" to be truncated.
225 if ((TIPC_SKB_MAX
- msg
->rep
->len
) <= 1) {
226 char *tail
= skb_tail_pointer(msg
->rep
);
229 sprintf(tail
- sizeof(REPLY_TRUNCATED
) - 1,
239 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump
*cmd
,
240 struct tipc_nl_compat_msg
*msg
)
245 if (msg
->req_type
&& !TLV_CHECK_TYPE(msg
->req
, msg
->req_type
))
248 msg
->rep
= tipc_tlv_alloc(msg
->rep_size
);
253 tipc_tlv_init(msg
->rep
, msg
->rep_type
);
258 arg
= nlmsg_new(0, GFP_KERNEL
);
265 err
= __tipc_nl_compat_dumpit(cmd
, msg
, arg
);
275 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit
*cmd
,
276 struct tipc_nl_compat_msg
*msg
)
279 struct sk_buff
*doit_buf
;
280 struct sk_buff
*trans_buf
;
281 struct nlattr
**attrbuf
;
282 struct genl_info info
;
284 trans_buf
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
288 err
= (*cmd
->transcode
)(cmd
, trans_buf
, msg
);
292 attrbuf
= kmalloc((tipc_genl_family
.maxattr
+ 1) *
293 sizeof(struct nlattr
*), GFP_KERNEL
);
299 err
= nla_parse(attrbuf
, tipc_genl_family
.maxattr
,
300 (const struct nlattr
*)trans_buf
->data
,
301 trans_buf
->len
, NULL
, NULL
);
305 doit_buf
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
311 doit_buf
->sk
= msg
->dst_sk
;
313 memset(&info
, 0, sizeof(info
));
314 info
.attrs
= attrbuf
;
316 err
= (*cmd
->doit
)(doit_buf
, &info
);
322 kfree_skb(trans_buf
);
327 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit
*cmd
,
328 struct tipc_nl_compat_msg
*msg
)
332 if (msg
->req_type
&& !TLV_CHECK_TYPE(msg
->req
, msg
->req_type
))
335 err
= __tipc_nl_compat_doit(cmd
, msg
);
339 /* The legacy API considered an empty message a success message */
340 msg
->rep
= tipc_tlv_alloc(0);
347 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg
*msg
,
348 struct nlattr
**attrs
)
350 struct nlattr
*bearer
[TIPC_NLA_BEARER_MAX
+ 1];
353 if (!attrs
[TIPC_NLA_BEARER
])
356 err
= nla_parse_nested(bearer
, TIPC_NLA_BEARER_MAX
,
357 attrs
[TIPC_NLA_BEARER
], NULL
, NULL
);
361 return tipc_add_tlv(msg
->rep
, TIPC_TLV_BEARER_NAME
,
362 nla_data(bearer
[TIPC_NLA_BEARER_NAME
]),
363 nla_len(bearer
[TIPC_NLA_BEARER_NAME
]));
366 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit
*cmd
,
368 struct tipc_nl_compat_msg
*msg
)
371 struct nlattr
*bearer
;
372 struct tipc_bearer_config
*b
;
374 b
= (struct tipc_bearer_config
*)TLV_DATA(msg
->req
);
376 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
380 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, b
->name
))
383 if (nla_put_u32(skb
, TIPC_NLA_BEARER_DOMAIN
, ntohl(b
->disc_domain
)))
386 if (ntohl(b
->priority
) <= TIPC_MAX_LINK_PRI
) {
387 prop
= nla_nest_start(skb
, TIPC_NLA_BEARER_PROP
);
390 if (nla_put_u32(skb
, TIPC_NLA_PROP_PRIO
, ntohl(b
->priority
)))
392 nla_nest_end(skb
, prop
);
394 nla_nest_end(skb
, bearer
);
399 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit
*cmd
,
401 struct tipc_nl_compat_msg
*msg
)
404 struct nlattr
*bearer
;
406 name
= (char *)TLV_DATA(msg
->req
);
408 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
412 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, name
))
415 nla_nest_end(skb
, bearer
);
420 static inline u32
perc(u32 count
, u32 total
)
422 return (count
* 100 + (total
/ 2)) / total
;
425 static void __fill_bc_link_stat(struct tipc_nl_compat_msg
*msg
,
426 struct nlattr
*prop
[], struct nlattr
*stats
[])
428 tipc_tlv_sprintf(msg
->rep
, " Window:%u packets\n",
429 nla_get_u32(prop
[TIPC_NLA_PROP_WIN
]));
431 tipc_tlv_sprintf(msg
->rep
,
432 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
433 nla_get_u32(stats
[TIPC_NLA_STATS_RX_INFO
]),
434 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTS
]),
435 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTED
]),
436 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLES
]),
437 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLED
]));
439 tipc_tlv_sprintf(msg
->rep
,
440 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
441 nla_get_u32(stats
[TIPC_NLA_STATS_TX_INFO
]),
442 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTS
]),
443 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTED
]),
444 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLES
]),
445 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLED
]));
447 tipc_tlv_sprintf(msg
->rep
, " RX naks:%u defs:%u dups:%u\n",
448 nla_get_u32(stats
[TIPC_NLA_STATS_RX_NACKS
]),
449 nla_get_u32(stats
[TIPC_NLA_STATS_RX_DEFERRED
]),
450 nla_get_u32(stats
[TIPC_NLA_STATS_DUPLICATES
]));
452 tipc_tlv_sprintf(msg
->rep
, " TX naks:%u acks:%u dups:%u\n",
453 nla_get_u32(stats
[TIPC_NLA_STATS_TX_NACKS
]),
454 nla_get_u32(stats
[TIPC_NLA_STATS_TX_ACKS
]),
455 nla_get_u32(stats
[TIPC_NLA_STATS_RETRANSMITTED
]));
457 tipc_tlv_sprintf(msg
->rep
,
458 " Congestion link:%u Send queue max:%u avg:%u",
459 nla_get_u32(stats
[TIPC_NLA_STATS_LINK_CONGS
]),
460 nla_get_u32(stats
[TIPC_NLA_STATS_MAX_QUEUE
]),
461 nla_get_u32(stats
[TIPC_NLA_STATS_AVG_QUEUE
]));
464 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg
*msg
,
465 struct nlattr
**attrs
)
468 struct nlattr
*link
[TIPC_NLA_LINK_MAX
+ 1];
469 struct nlattr
*prop
[TIPC_NLA_PROP_MAX
+ 1];
470 struct nlattr
*stats
[TIPC_NLA_STATS_MAX
+ 1];
473 if (!attrs
[TIPC_NLA_LINK
])
476 err
= nla_parse_nested(link
, TIPC_NLA_LINK_MAX
, attrs
[TIPC_NLA_LINK
],
481 if (!link
[TIPC_NLA_LINK_PROP
])
484 err
= nla_parse_nested(prop
, TIPC_NLA_PROP_MAX
,
485 link
[TIPC_NLA_LINK_PROP
], NULL
, NULL
);
489 if (!link
[TIPC_NLA_LINK_STATS
])
492 err
= nla_parse_nested(stats
, TIPC_NLA_STATS_MAX
,
493 link
[TIPC_NLA_LINK_STATS
], NULL
, NULL
);
497 name
= (char *)TLV_DATA(msg
->req
);
498 if (strcmp(name
, nla_data(link
[TIPC_NLA_LINK_NAME
])) != 0)
501 tipc_tlv_sprintf(msg
->rep
, "\nLink <%s>\n",
502 nla_data(link
[TIPC_NLA_LINK_NAME
]));
504 if (link
[TIPC_NLA_LINK_BROADCAST
]) {
505 __fill_bc_link_stat(msg
, prop
, stats
);
509 if (link
[TIPC_NLA_LINK_ACTIVE
])
510 tipc_tlv_sprintf(msg
->rep
, " ACTIVE");
511 else if (link
[TIPC_NLA_LINK_UP
])
512 tipc_tlv_sprintf(msg
->rep
, " STANDBY");
514 tipc_tlv_sprintf(msg
->rep
, " DEFUNCT");
516 tipc_tlv_sprintf(msg
->rep
, " MTU:%u Priority:%u",
517 nla_get_u32(link
[TIPC_NLA_LINK_MTU
]),
518 nla_get_u32(prop
[TIPC_NLA_PROP_PRIO
]));
520 tipc_tlv_sprintf(msg
->rep
, " Tolerance:%u ms Window:%u packets\n",
521 nla_get_u32(prop
[TIPC_NLA_PROP_TOL
]),
522 nla_get_u32(prop
[TIPC_NLA_PROP_WIN
]));
524 tipc_tlv_sprintf(msg
->rep
,
525 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
526 nla_get_u32(link
[TIPC_NLA_LINK_RX
]) -
527 nla_get_u32(stats
[TIPC_NLA_STATS_RX_INFO
]),
528 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTS
]),
529 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTED
]),
530 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLES
]),
531 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLED
]));
533 tipc_tlv_sprintf(msg
->rep
,
534 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
535 nla_get_u32(link
[TIPC_NLA_LINK_TX
]) -
536 nla_get_u32(stats
[TIPC_NLA_STATS_TX_INFO
]),
537 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTS
]),
538 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTED
]),
539 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLES
]),
540 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLED
]));
542 tipc_tlv_sprintf(msg
->rep
,
543 " TX profile sample:%u packets average:%u octets\n",
544 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_CNT
]),
545 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_TOT
]) /
546 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
]));
548 tipc_tlv_sprintf(msg
->rep
,
549 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
550 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P0
]),
551 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
552 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P1
]),
553 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
554 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P2
]),
555 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
556 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P3
]),
557 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])));
559 tipc_tlv_sprintf(msg
->rep
, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
560 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P4
]),
561 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
562 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P5
]),
563 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
564 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P6
]),
565 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])));
567 tipc_tlv_sprintf(msg
->rep
,
568 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
569 nla_get_u32(stats
[TIPC_NLA_STATS_RX_STATES
]),
570 nla_get_u32(stats
[TIPC_NLA_STATS_RX_PROBES
]),
571 nla_get_u32(stats
[TIPC_NLA_STATS_RX_NACKS
]),
572 nla_get_u32(stats
[TIPC_NLA_STATS_RX_DEFERRED
]),
573 nla_get_u32(stats
[TIPC_NLA_STATS_DUPLICATES
]));
575 tipc_tlv_sprintf(msg
->rep
,
576 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
577 nla_get_u32(stats
[TIPC_NLA_STATS_TX_STATES
]),
578 nla_get_u32(stats
[TIPC_NLA_STATS_TX_PROBES
]),
579 nla_get_u32(stats
[TIPC_NLA_STATS_TX_NACKS
]),
580 nla_get_u32(stats
[TIPC_NLA_STATS_TX_ACKS
]),
581 nla_get_u32(stats
[TIPC_NLA_STATS_RETRANSMITTED
]));
583 tipc_tlv_sprintf(msg
->rep
,
584 " Congestion link:%u Send queue max:%u avg:%u",
585 nla_get_u32(stats
[TIPC_NLA_STATS_LINK_CONGS
]),
586 nla_get_u32(stats
[TIPC_NLA_STATS_MAX_QUEUE
]),
587 nla_get_u32(stats
[TIPC_NLA_STATS_AVG_QUEUE
]));
592 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg
*msg
,
593 struct nlattr
**attrs
)
595 struct nlattr
*link
[TIPC_NLA_LINK_MAX
+ 1];
596 struct tipc_link_info link_info
;
599 if (!attrs
[TIPC_NLA_LINK
])
602 err
= nla_parse_nested(link
, TIPC_NLA_LINK_MAX
, attrs
[TIPC_NLA_LINK
],
607 link_info
.dest
= nla_get_flag(link
[TIPC_NLA_LINK_DEST
]);
608 link_info
.up
= htonl(nla_get_flag(link
[TIPC_NLA_LINK_UP
]));
609 nla_strlcpy(link_info
.str
, link
[TIPC_NLA_LINK_NAME
],
612 return tipc_add_tlv(msg
->rep
, TIPC_TLV_LINK_INFO
,
613 &link_info
, sizeof(link_info
));
616 static int __tipc_add_link_prop(struct sk_buff
*skb
,
617 struct tipc_nl_compat_msg
*msg
,
618 struct tipc_link_config
*lc
)
621 case TIPC_CMD_SET_LINK_PRI
:
622 return nla_put_u32(skb
, TIPC_NLA_PROP_PRIO
, ntohl(lc
->value
));
623 case TIPC_CMD_SET_LINK_TOL
:
624 return nla_put_u32(skb
, TIPC_NLA_PROP_TOL
, ntohl(lc
->value
));
625 case TIPC_CMD_SET_LINK_WINDOW
:
626 return nla_put_u32(skb
, TIPC_NLA_PROP_WIN
, ntohl(lc
->value
));
632 static int tipc_nl_compat_media_set(struct sk_buff
*skb
,
633 struct tipc_nl_compat_msg
*msg
)
636 struct nlattr
*media
;
637 struct tipc_link_config
*lc
;
639 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
641 media
= nla_nest_start(skb
, TIPC_NLA_MEDIA
);
645 if (nla_put_string(skb
, TIPC_NLA_MEDIA_NAME
, lc
->name
))
648 prop
= nla_nest_start(skb
, TIPC_NLA_MEDIA_PROP
);
652 __tipc_add_link_prop(skb
, msg
, lc
);
653 nla_nest_end(skb
, prop
);
654 nla_nest_end(skb
, media
);
659 static int tipc_nl_compat_bearer_set(struct sk_buff
*skb
,
660 struct tipc_nl_compat_msg
*msg
)
663 struct nlattr
*bearer
;
664 struct tipc_link_config
*lc
;
666 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
668 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
672 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, lc
->name
))
675 prop
= nla_nest_start(skb
, TIPC_NLA_BEARER_PROP
);
679 __tipc_add_link_prop(skb
, msg
, lc
);
680 nla_nest_end(skb
, prop
);
681 nla_nest_end(skb
, bearer
);
686 static int __tipc_nl_compat_link_set(struct sk_buff
*skb
,
687 struct tipc_nl_compat_msg
*msg
)
691 struct tipc_link_config
*lc
;
693 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
695 link
= nla_nest_start(skb
, TIPC_NLA_LINK
);
699 if (nla_put_string(skb
, TIPC_NLA_LINK_NAME
, lc
->name
))
702 prop
= nla_nest_start(skb
, TIPC_NLA_LINK_PROP
);
706 __tipc_add_link_prop(skb
, msg
, lc
);
707 nla_nest_end(skb
, prop
);
708 nla_nest_end(skb
, link
);
713 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit
*cmd
,
715 struct tipc_nl_compat_msg
*msg
)
717 struct tipc_link_config
*lc
;
718 struct tipc_bearer
*bearer
;
719 struct tipc_media
*media
;
721 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
723 media
= tipc_media_find(lc
->name
);
725 cmd
->doit
= &tipc_nl_media_set
;
726 return tipc_nl_compat_media_set(skb
, msg
);
729 bearer
= tipc_bearer_find(msg
->net
, lc
->name
);
731 cmd
->doit
= &tipc_nl_bearer_set
;
732 return tipc_nl_compat_bearer_set(skb
, msg
);
735 return __tipc_nl_compat_link_set(skb
, msg
);
738 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit
*cmd
,
740 struct tipc_nl_compat_msg
*msg
)
745 name
= (char *)TLV_DATA(msg
->req
);
747 link
= nla_nest_start(skb
, TIPC_NLA_LINK
);
751 if (nla_put_string(skb
, TIPC_NLA_LINK_NAME
, name
))
754 nla_nest_end(skb
, link
);
759 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg
*msg
)
763 struct tipc_name_table_query
*ntq
;
764 static const char * const header
[] = {
771 ntq
= (struct tipc_name_table_query
*)TLV_DATA(msg
->req
);
773 depth
= ntohl(ntq
->depth
);
777 for (i
= 0; i
< depth
; i
++)
778 tipc_tlv_sprintf(msg
->rep
, header
[i
]);
779 tipc_tlv_sprintf(msg
->rep
, "\n");
784 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg
*msg
,
785 struct nlattr
**attrs
)
788 struct tipc_name_table_query
*ntq
;
789 struct nlattr
*nt
[TIPC_NLA_NAME_TABLE_MAX
+ 1];
790 struct nlattr
*publ
[TIPC_NLA_PUBL_MAX
+ 1];
791 u32 node
, depth
, type
, lowbound
, upbound
;
792 static const char * const scope_str
[] = {"", " zone", " cluster",
796 if (!attrs
[TIPC_NLA_NAME_TABLE
])
799 err
= nla_parse_nested(nt
, TIPC_NLA_NAME_TABLE_MAX
,
800 attrs
[TIPC_NLA_NAME_TABLE
], NULL
, NULL
);
804 if (!nt
[TIPC_NLA_NAME_TABLE_PUBL
])
807 err
= nla_parse_nested(publ
, TIPC_NLA_PUBL_MAX
,
808 nt
[TIPC_NLA_NAME_TABLE_PUBL
], NULL
, NULL
);
812 ntq
= (struct tipc_name_table_query
*)TLV_DATA(msg
->req
);
814 depth
= ntohl(ntq
->depth
);
815 type
= ntohl(ntq
->type
);
816 lowbound
= ntohl(ntq
->lowbound
);
817 upbound
= ntohl(ntq
->upbound
);
819 if (!(depth
& TIPC_NTQ_ALLTYPES
) &&
820 (type
!= nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
])))
822 if (lowbound
&& (lowbound
> nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
])))
824 if (upbound
&& (upbound
< nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
])))
827 tipc_tlv_sprintf(msg
->rep
, "%-10u ",
828 nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
]));
833 tipc_tlv_sprintf(msg
->rep
, "%-10u %-10u ",
834 nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
]),
835 nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
]));
840 node
= nla_get_u32(publ
[TIPC_NLA_PUBL_NODE
]);
841 sprintf(port_str
, "<%u.%u.%u:%u>", tipc_zone(node
), tipc_cluster(node
),
842 tipc_node(node
), nla_get_u32(publ
[TIPC_NLA_PUBL_REF
]));
843 tipc_tlv_sprintf(msg
->rep
, "%-26s ", port_str
);
848 tipc_tlv_sprintf(msg
->rep
, "%-10u %s",
849 nla_get_u32(publ
[TIPC_NLA_PUBL_KEY
]),
850 scope_str
[nla_get_u32(publ
[TIPC_NLA_PUBL_SCOPE
])]);
852 tipc_tlv_sprintf(msg
->rep
, "\n");
857 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg
*msg
,
858 struct nlattr
**attrs
)
860 u32 type
, lower
, upper
;
861 struct nlattr
*publ
[TIPC_NLA_PUBL_MAX
+ 1];
864 if (!attrs
[TIPC_NLA_PUBL
])
867 err
= nla_parse_nested(publ
, TIPC_NLA_PUBL_MAX
, attrs
[TIPC_NLA_PUBL
],
872 type
= nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
]);
873 lower
= nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
]);
874 upper
= nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
]);
877 tipc_tlv_sprintf(msg
->rep
, " {%u,%u}", type
, lower
);
879 tipc_tlv_sprintf(msg
->rep
, " {%u,%u,%u}", type
, lower
, upper
);
884 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg
*msg
, u32 sock
)
889 struct sk_buff
*args
;
890 struct tipc_nl_compat_cmd_dump dump
;
892 args
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
896 hdr
= genlmsg_put(args
, 0, 0, &tipc_genl_family
, NLM_F_MULTI
,
899 nest
= nla_nest_start(args
, TIPC_NLA_SOCK
);
905 if (nla_put_u32(args
, TIPC_NLA_SOCK_REF
, sock
)) {
910 nla_nest_end(args
, nest
);
911 genlmsg_end(args
, hdr
);
913 dump
.dumpit
= tipc_nl_publ_dump
;
914 dump
.format
= __tipc_nl_compat_publ_dump
;
916 err
= __tipc_nl_compat_dumpit(&dump
, msg
, args
);
923 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg
*msg
,
924 struct nlattr
**attrs
)
928 struct nlattr
*sock
[TIPC_NLA_SOCK_MAX
+ 1];
930 if (!attrs
[TIPC_NLA_SOCK
])
933 err
= nla_parse_nested(sock
, TIPC_NLA_SOCK_MAX
, attrs
[TIPC_NLA_SOCK
],
938 sock_ref
= nla_get_u32(sock
[TIPC_NLA_SOCK_REF
]);
939 tipc_tlv_sprintf(msg
->rep
, "%u:", sock_ref
);
941 if (sock
[TIPC_NLA_SOCK_CON
]) {
943 struct nlattr
*con
[TIPC_NLA_CON_MAX
+ 1];
945 nla_parse_nested(con
, TIPC_NLA_CON_MAX
,
946 sock
[TIPC_NLA_SOCK_CON
], NULL
, NULL
);
948 node
= nla_get_u32(con
[TIPC_NLA_CON_NODE
]);
949 tipc_tlv_sprintf(msg
->rep
, " connected to <%u.%u.%u:%u>",
953 nla_get_u32(con
[TIPC_NLA_CON_SOCK
]));
955 if (con
[TIPC_NLA_CON_FLAG
])
956 tipc_tlv_sprintf(msg
->rep
, " via {%u,%u}\n",
957 nla_get_u32(con
[TIPC_NLA_CON_TYPE
]),
958 nla_get_u32(con
[TIPC_NLA_CON_INST
]));
960 tipc_tlv_sprintf(msg
->rep
, "\n");
961 } else if (sock
[TIPC_NLA_SOCK_HAS_PUBL
]) {
962 tipc_tlv_sprintf(msg
->rep
, " bound to");
964 err
= tipc_nl_compat_publ_dump(msg
, sock_ref
);
968 tipc_tlv_sprintf(msg
->rep
, "\n");
973 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg
*msg
,
974 struct nlattr
**attrs
)
976 struct nlattr
*media
[TIPC_NLA_MEDIA_MAX
+ 1];
979 if (!attrs
[TIPC_NLA_MEDIA
])
982 err
= nla_parse_nested(media
, TIPC_NLA_MEDIA_MAX
,
983 attrs
[TIPC_NLA_MEDIA
], NULL
, NULL
);
987 return tipc_add_tlv(msg
->rep
, TIPC_TLV_MEDIA_NAME
,
988 nla_data(media
[TIPC_NLA_MEDIA_NAME
]),
989 nla_len(media
[TIPC_NLA_MEDIA_NAME
]));
992 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg
*msg
,
993 struct nlattr
**attrs
)
995 struct tipc_node_info node_info
;
996 struct nlattr
*node
[TIPC_NLA_NODE_MAX
+ 1];
999 if (!attrs
[TIPC_NLA_NODE
])
1002 err
= nla_parse_nested(node
, TIPC_NLA_NODE_MAX
, attrs
[TIPC_NLA_NODE
],
1007 node_info
.addr
= htonl(nla_get_u32(node
[TIPC_NLA_NODE_ADDR
]));
1008 node_info
.up
= htonl(nla_get_flag(node
[TIPC_NLA_NODE_UP
]));
1010 return tipc_add_tlv(msg
->rep
, TIPC_TLV_NODE_INFO
, &node_info
,
1014 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit
*cmd
,
1015 struct sk_buff
*skb
,
1016 struct tipc_nl_compat_msg
*msg
)
1021 val
= ntohl(*(__be32
*)TLV_DATA(msg
->req
));
1023 net
= nla_nest_start(skb
, TIPC_NLA_NET
);
1027 if (msg
->cmd
== TIPC_CMD_SET_NODE_ADDR
) {
1028 if (nla_put_u32(skb
, TIPC_NLA_NET_ADDR
, val
))
1030 } else if (msg
->cmd
== TIPC_CMD_SET_NETID
) {
1031 if (nla_put_u32(skb
, TIPC_NLA_NET_ID
, val
))
1034 nla_nest_end(skb
, net
);
1039 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg
*msg
,
1040 struct nlattr
**attrs
)
1043 struct nlattr
*net
[TIPC_NLA_NET_MAX
+ 1];
1046 if (!attrs
[TIPC_NLA_NET
])
1049 err
= nla_parse_nested(net
, TIPC_NLA_NET_MAX
, attrs
[TIPC_NLA_NET
],
1054 id
= htonl(nla_get_u32(net
[TIPC_NLA_NET_ID
]));
1056 return tipc_add_tlv(msg
->rep
, TIPC_TLV_UNSIGNED
, &id
, sizeof(id
));
1059 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg
*msg
)
1061 msg
->rep
= tipc_tlv_alloc(ULTRA_STRING_MAX_LEN
);
1065 tipc_tlv_init(msg
->rep
, TIPC_TLV_ULTRA_STRING
);
1066 tipc_tlv_sprintf(msg
->rep
, "TIPC version " TIPC_MOD_VER
"\n");
1071 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg
*msg
)
1073 struct tipc_nl_compat_cmd_dump dump
;
1074 struct tipc_nl_compat_cmd_doit doit
;
1076 memset(&dump
, 0, sizeof(dump
));
1077 memset(&doit
, 0, sizeof(doit
));
1081 msg
->rep
= tipc_tlv_alloc(0);
1085 case TIPC_CMD_GET_BEARER_NAMES
:
1086 msg
->rep_size
= MAX_BEARERS
* TLV_SPACE(TIPC_MAX_BEARER_NAME
);
1087 dump
.dumpit
= tipc_nl_bearer_dump
;
1088 dump
.format
= tipc_nl_compat_bearer_dump
;
1089 return tipc_nl_compat_dumpit(&dump
, msg
);
1090 case TIPC_CMD_ENABLE_BEARER
:
1091 msg
->req_type
= TIPC_TLV_BEARER_CONFIG
;
1092 doit
.doit
= tipc_nl_bearer_enable
;
1093 doit
.transcode
= tipc_nl_compat_bearer_enable
;
1094 return tipc_nl_compat_doit(&doit
, msg
);
1095 case TIPC_CMD_DISABLE_BEARER
:
1096 msg
->req_type
= TIPC_TLV_BEARER_NAME
;
1097 doit
.doit
= tipc_nl_bearer_disable
;
1098 doit
.transcode
= tipc_nl_compat_bearer_disable
;
1099 return tipc_nl_compat_doit(&doit
, msg
);
1100 case TIPC_CMD_SHOW_LINK_STATS
:
1101 msg
->req_type
= TIPC_TLV_LINK_NAME
;
1102 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1103 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1104 dump
.dumpit
= tipc_nl_node_dump_link
;
1105 dump
.format
= tipc_nl_compat_link_stat_dump
;
1106 return tipc_nl_compat_dumpit(&dump
, msg
);
1107 case TIPC_CMD_GET_LINKS
:
1108 msg
->req_type
= TIPC_TLV_NET_ADDR
;
1109 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1110 dump
.dumpit
= tipc_nl_node_dump_link
;
1111 dump
.format
= tipc_nl_compat_link_dump
;
1112 return tipc_nl_compat_dumpit(&dump
, msg
);
1113 case TIPC_CMD_SET_LINK_TOL
:
1114 case TIPC_CMD_SET_LINK_PRI
:
1115 case TIPC_CMD_SET_LINK_WINDOW
:
1116 msg
->req_type
= TIPC_TLV_LINK_CONFIG
;
1117 doit
.doit
= tipc_nl_node_set_link
;
1118 doit
.transcode
= tipc_nl_compat_link_set
;
1119 return tipc_nl_compat_doit(&doit
, msg
);
1120 case TIPC_CMD_RESET_LINK_STATS
:
1121 msg
->req_type
= TIPC_TLV_LINK_NAME
;
1122 doit
.doit
= tipc_nl_node_reset_link_stats
;
1123 doit
.transcode
= tipc_nl_compat_link_reset_stats
;
1124 return tipc_nl_compat_doit(&doit
, msg
);
1125 case TIPC_CMD_SHOW_NAME_TABLE
:
1126 msg
->req_type
= TIPC_TLV_NAME_TBL_QUERY
;
1127 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1128 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1129 dump
.header
= tipc_nl_compat_name_table_dump_header
;
1130 dump
.dumpit
= tipc_nl_name_table_dump
;
1131 dump
.format
= tipc_nl_compat_name_table_dump
;
1132 return tipc_nl_compat_dumpit(&dump
, msg
);
1133 case TIPC_CMD_SHOW_PORTS
:
1134 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1135 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1136 dump
.dumpit
= tipc_nl_sk_dump
;
1137 dump
.format
= tipc_nl_compat_sk_dump
;
1138 return tipc_nl_compat_dumpit(&dump
, msg
);
1139 case TIPC_CMD_GET_MEDIA_NAMES
:
1140 msg
->rep_size
= MAX_MEDIA
* TLV_SPACE(TIPC_MAX_MEDIA_NAME
);
1141 dump
.dumpit
= tipc_nl_media_dump
;
1142 dump
.format
= tipc_nl_compat_media_dump
;
1143 return tipc_nl_compat_dumpit(&dump
, msg
);
1144 case TIPC_CMD_GET_NODES
:
1145 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1146 dump
.dumpit
= tipc_nl_node_dump
;
1147 dump
.format
= tipc_nl_compat_node_dump
;
1148 return tipc_nl_compat_dumpit(&dump
, msg
);
1149 case TIPC_CMD_SET_NODE_ADDR
:
1150 msg
->req_type
= TIPC_TLV_NET_ADDR
;
1151 doit
.doit
= tipc_nl_net_set
;
1152 doit
.transcode
= tipc_nl_compat_net_set
;
1153 return tipc_nl_compat_doit(&doit
, msg
);
1154 case TIPC_CMD_SET_NETID
:
1155 msg
->req_type
= TIPC_TLV_UNSIGNED
;
1156 doit
.doit
= tipc_nl_net_set
;
1157 doit
.transcode
= tipc_nl_compat_net_set
;
1158 return tipc_nl_compat_doit(&doit
, msg
);
1159 case TIPC_CMD_GET_NETID
:
1160 msg
->rep_size
= sizeof(u32
);
1161 dump
.dumpit
= tipc_nl_net_dump
;
1162 dump
.format
= tipc_nl_compat_net_dump
;
1163 return tipc_nl_compat_dumpit(&dump
, msg
);
1164 case TIPC_CMD_SHOW_STATS
:
1165 return tipc_cmd_show_stats_compat(msg
);
1171 static int tipc_nl_compat_recv(struct sk_buff
*skb
, struct genl_info
*info
)
1175 struct tipc_nl_compat_msg msg
;
1176 struct nlmsghdr
*req_nlh
;
1177 struct nlmsghdr
*rep_nlh
;
1178 struct tipc_genlmsghdr
*req_userhdr
= info
->userhdr
;
1180 memset(&msg
, 0, sizeof(msg
));
1182 req_nlh
= (struct nlmsghdr
*)skb
->data
;
1183 msg
.req
= nlmsg_data(req_nlh
) + GENL_HDRLEN
+ TIPC_GENL_HDRLEN
;
1184 msg
.cmd
= req_userhdr
->cmd
;
1185 msg
.net
= genl_info_net(info
);
1186 msg
.dst_sk
= skb
->sk
;
1188 if ((msg
.cmd
& 0xC000) && (!netlink_net_capable(skb
, CAP_NET_ADMIN
))) {
1189 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN
);
1194 len
= nlmsg_attrlen(req_nlh
, GENL_HDRLEN
+ TIPC_GENL_HDRLEN
);
1195 if (len
&& !TLV_OK(msg
.req
, len
)) {
1196 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED
);
1201 err
= tipc_nl_compat_handle(&msg
);
1202 if ((err
== -EOPNOTSUPP
) || (err
== -EPERM
))
1203 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED
);
1204 else if (err
== -EINVAL
)
1205 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_TLV_ERROR
);
1210 len
= nlmsg_total_size(GENL_HDRLEN
+ TIPC_GENL_HDRLEN
);
1211 skb_push(msg
.rep
, len
);
1212 rep_nlh
= nlmsg_hdr(msg
.rep
);
1213 memcpy(rep_nlh
, info
->nlhdr
, len
);
1214 rep_nlh
->nlmsg_len
= msg
.rep
->len
;
1215 genlmsg_unicast(msg
.net
, msg
.rep
, NETLINK_CB(skb
).portid
);
1220 static struct genl_ops tipc_genl_compat_ops
[] = {
1222 .cmd
= TIPC_GENL_CMD
,
1223 .doit
= tipc_nl_compat_recv
,
1227 static struct genl_family tipc_genl_compat_family __ro_after_init
= {
1228 .name
= TIPC_GENL_NAME
,
1229 .version
= TIPC_GENL_VERSION
,
1230 .hdrsize
= TIPC_GENL_HDRLEN
,
1233 .module
= THIS_MODULE
,
1234 .ops
= tipc_genl_compat_ops
,
1235 .n_ops
= ARRAY_SIZE(tipc_genl_compat_ops
),
1238 int __init
tipc_netlink_compat_start(void)
1242 res
= genl_register_family(&tipc_genl_compat_family
);
1244 pr_err("Failed to register legacy compat interface\n");
1251 void tipc_netlink_compat_stop(void)
1253 genl_unregister_family(&tipc_genl_compat_family
);