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
);
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];
352 nla_parse_nested(bearer
, TIPC_NLA_BEARER_MAX
, attrs
[TIPC_NLA_BEARER
],
355 return tipc_add_tlv(msg
->rep
, TIPC_TLV_BEARER_NAME
,
356 nla_data(bearer
[TIPC_NLA_BEARER_NAME
]),
357 nla_len(bearer
[TIPC_NLA_BEARER_NAME
]));
360 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit
*cmd
,
362 struct tipc_nl_compat_msg
*msg
)
365 struct nlattr
*bearer
;
366 struct tipc_bearer_config
*b
;
368 b
= (struct tipc_bearer_config
*)TLV_DATA(msg
->req
);
370 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
374 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, b
->name
))
377 if (nla_put_u32(skb
, TIPC_NLA_BEARER_DOMAIN
, ntohl(b
->disc_domain
)))
380 if (ntohl(b
->priority
) <= TIPC_MAX_LINK_PRI
) {
381 prop
= nla_nest_start(skb
, TIPC_NLA_BEARER_PROP
);
384 if (nla_put_u32(skb
, TIPC_NLA_PROP_PRIO
, ntohl(b
->priority
)))
386 nla_nest_end(skb
, prop
);
388 nla_nest_end(skb
, bearer
);
393 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit
*cmd
,
395 struct tipc_nl_compat_msg
*msg
)
398 struct nlattr
*bearer
;
400 name
= (char *)TLV_DATA(msg
->req
);
402 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
406 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, name
))
409 nla_nest_end(skb
, bearer
);
414 static inline u32
perc(u32 count
, u32 total
)
416 return (count
* 100 + (total
/ 2)) / total
;
419 static void __fill_bc_link_stat(struct tipc_nl_compat_msg
*msg
,
420 struct nlattr
*prop
[], struct nlattr
*stats
[])
422 tipc_tlv_sprintf(msg
->rep
, " Window:%u packets\n",
423 nla_get_u32(prop
[TIPC_NLA_PROP_WIN
]));
425 tipc_tlv_sprintf(msg
->rep
,
426 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
427 nla_get_u32(stats
[TIPC_NLA_STATS_RX_INFO
]),
428 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTS
]),
429 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTED
]),
430 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLES
]),
431 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLED
]));
433 tipc_tlv_sprintf(msg
->rep
,
434 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
435 nla_get_u32(stats
[TIPC_NLA_STATS_TX_INFO
]),
436 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTS
]),
437 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTED
]),
438 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLES
]),
439 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLED
]));
441 tipc_tlv_sprintf(msg
->rep
, " RX naks:%u defs:%u dups:%u\n",
442 nla_get_u32(stats
[TIPC_NLA_STATS_RX_NACKS
]),
443 nla_get_u32(stats
[TIPC_NLA_STATS_RX_DEFERRED
]),
444 nla_get_u32(stats
[TIPC_NLA_STATS_DUPLICATES
]));
446 tipc_tlv_sprintf(msg
->rep
, " TX naks:%u acks:%u dups:%u\n",
447 nla_get_u32(stats
[TIPC_NLA_STATS_TX_NACKS
]),
448 nla_get_u32(stats
[TIPC_NLA_STATS_TX_ACKS
]),
449 nla_get_u32(stats
[TIPC_NLA_STATS_RETRANSMITTED
]));
451 tipc_tlv_sprintf(msg
->rep
,
452 " Congestion link:%u Send queue max:%u avg:%u",
453 nla_get_u32(stats
[TIPC_NLA_STATS_LINK_CONGS
]),
454 nla_get_u32(stats
[TIPC_NLA_STATS_MAX_QUEUE
]),
455 nla_get_u32(stats
[TIPC_NLA_STATS_AVG_QUEUE
]));
458 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg
*msg
,
459 struct nlattr
**attrs
)
462 struct nlattr
*link
[TIPC_NLA_LINK_MAX
+ 1];
463 struct nlattr
*prop
[TIPC_NLA_PROP_MAX
+ 1];
464 struct nlattr
*stats
[TIPC_NLA_STATS_MAX
+ 1];
466 nla_parse_nested(link
, TIPC_NLA_LINK_MAX
, attrs
[TIPC_NLA_LINK
], NULL
);
468 nla_parse_nested(prop
, TIPC_NLA_PROP_MAX
, link
[TIPC_NLA_LINK_PROP
],
471 nla_parse_nested(stats
, TIPC_NLA_STATS_MAX
, link
[TIPC_NLA_LINK_STATS
],
474 name
= (char *)TLV_DATA(msg
->req
);
475 if (strcmp(name
, nla_data(link
[TIPC_NLA_LINK_NAME
])) != 0)
478 tipc_tlv_sprintf(msg
->rep
, "\nLink <%s>\n",
479 nla_data(link
[TIPC_NLA_LINK_NAME
]));
481 if (link
[TIPC_NLA_LINK_BROADCAST
]) {
482 __fill_bc_link_stat(msg
, prop
, stats
);
486 if (link
[TIPC_NLA_LINK_ACTIVE
])
487 tipc_tlv_sprintf(msg
->rep
, " ACTIVE");
488 else if (link
[TIPC_NLA_LINK_UP
])
489 tipc_tlv_sprintf(msg
->rep
, " STANDBY");
491 tipc_tlv_sprintf(msg
->rep
, " DEFUNCT");
493 tipc_tlv_sprintf(msg
->rep
, " MTU:%u Priority:%u",
494 nla_get_u32(link
[TIPC_NLA_LINK_MTU
]),
495 nla_get_u32(prop
[TIPC_NLA_PROP_PRIO
]));
497 tipc_tlv_sprintf(msg
->rep
, " Tolerance:%u ms Window:%u packets\n",
498 nla_get_u32(prop
[TIPC_NLA_PROP_TOL
]),
499 nla_get_u32(prop
[TIPC_NLA_PROP_WIN
]));
501 tipc_tlv_sprintf(msg
->rep
,
502 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
503 nla_get_u32(link
[TIPC_NLA_LINK_RX
]) -
504 nla_get_u32(stats
[TIPC_NLA_STATS_RX_INFO
]),
505 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTS
]),
506 nla_get_u32(stats
[TIPC_NLA_STATS_RX_FRAGMENTED
]),
507 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLES
]),
508 nla_get_u32(stats
[TIPC_NLA_STATS_RX_BUNDLED
]));
510 tipc_tlv_sprintf(msg
->rep
,
511 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
512 nla_get_u32(link
[TIPC_NLA_LINK_TX
]) -
513 nla_get_u32(stats
[TIPC_NLA_STATS_TX_INFO
]),
514 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTS
]),
515 nla_get_u32(stats
[TIPC_NLA_STATS_TX_FRAGMENTED
]),
516 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLES
]),
517 nla_get_u32(stats
[TIPC_NLA_STATS_TX_BUNDLED
]));
519 tipc_tlv_sprintf(msg
->rep
,
520 " TX profile sample:%u packets average:%u octets\n",
521 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_CNT
]),
522 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_TOT
]) /
523 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
]));
525 tipc_tlv_sprintf(msg
->rep
,
526 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
527 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P0
]),
528 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
529 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P1
]),
530 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
531 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P2
]),
532 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
533 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P3
]),
534 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])));
536 tipc_tlv_sprintf(msg
->rep
, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
537 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P4
]),
538 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
539 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P5
]),
540 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])),
541 perc(nla_get_u32(stats
[TIPC_NLA_STATS_MSG_LEN_P6
]),
542 nla_get_u32(stats
[TIPC_NLA_STATS_MSG_PROF_TOT
])));
544 tipc_tlv_sprintf(msg
->rep
,
545 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
546 nla_get_u32(stats
[TIPC_NLA_STATS_RX_STATES
]),
547 nla_get_u32(stats
[TIPC_NLA_STATS_RX_PROBES
]),
548 nla_get_u32(stats
[TIPC_NLA_STATS_RX_NACKS
]),
549 nla_get_u32(stats
[TIPC_NLA_STATS_RX_DEFERRED
]),
550 nla_get_u32(stats
[TIPC_NLA_STATS_DUPLICATES
]));
552 tipc_tlv_sprintf(msg
->rep
,
553 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
554 nla_get_u32(stats
[TIPC_NLA_STATS_TX_STATES
]),
555 nla_get_u32(stats
[TIPC_NLA_STATS_TX_PROBES
]),
556 nla_get_u32(stats
[TIPC_NLA_STATS_TX_NACKS
]),
557 nla_get_u32(stats
[TIPC_NLA_STATS_TX_ACKS
]),
558 nla_get_u32(stats
[TIPC_NLA_STATS_RETRANSMITTED
]));
560 tipc_tlv_sprintf(msg
->rep
,
561 " Congestion link:%u Send queue max:%u avg:%u",
562 nla_get_u32(stats
[TIPC_NLA_STATS_LINK_CONGS
]),
563 nla_get_u32(stats
[TIPC_NLA_STATS_MAX_QUEUE
]),
564 nla_get_u32(stats
[TIPC_NLA_STATS_AVG_QUEUE
]));
569 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg
*msg
,
570 struct nlattr
**attrs
)
572 struct nlattr
*link
[TIPC_NLA_LINK_MAX
+ 1];
573 struct tipc_link_info link_info
;
575 nla_parse_nested(link
, TIPC_NLA_LINK_MAX
, attrs
[TIPC_NLA_LINK
], NULL
);
577 link_info
.dest
= nla_get_flag(link
[TIPC_NLA_LINK_DEST
]);
578 link_info
.up
= htonl(nla_get_flag(link
[TIPC_NLA_LINK_UP
]));
579 nla_strlcpy(link_info
.str
, link
[TIPC_NLA_LINK_NAME
],
582 return tipc_add_tlv(msg
->rep
, TIPC_TLV_LINK_INFO
,
583 &link_info
, sizeof(link_info
));
586 static int __tipc_add_link_prop(struct sk_buff
*skb
,
587 struct tipc_nl_compat_msg
*msg
,
588 struct tipc_link_config
*lc
)
591 case TIPC_CMD_SET_LINK_PRI
:
592 return nla_put_u32(skb
, TIPC_NLA_PROP_PRIO
, ntohl(lc
->value
));
593 case TIPC_CMD_SET_LINK_TOL
:
594 return nla_put_u32(skb
, TIPC_NLA_PROP_TOL
, ntohl(lc
->value
));
595 case TIPC_CMD_SET_LINK_WINDOW
:
596 return nla_put_u32(skb
, TIPC_NLA_PROP_WIN
, ntohl(lc
->value
));
602 static int tipc_nl_compat_media_set(struct sk_buff
*skb
,
603 struct tipc_nl_compat_msg
*msg
)
606 struct nlattr
*media
;
607 struct tipc_link_config
*lc
;
609 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
611 media
= nla_nest_start(skb
, TIPC_NLA_MEDIA
);
615 if (nla_put_string(skb
, TIPC_NLA_MEDIA_NAME
, lc
->name
))
618 prop
= nla_nest_start(skb
, TIPC_NLA_MEDIA_PROP
);
622 __tipc_add_link_prop(skb
, msg
, lc
);
623 nla_nest_end(skb
, prop
);
624 nla_nest_end(skb
, media
);
629 static int tipc_nl_compat_bearer_set(struct sk_buff
*skb
,
630 struct tipc_nl_compat_msg
*msg
)
633 struct nlattr
*bearer
;
634 struct tipc_link_config
*lc
;
636 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
638 bearer
= nla_nest_start(skb
, TIPC_NLA_BEARER
);
642 if (nla_put_string(skb
, TIPC_NLA_BEARER_NAME
, lc
->name
))
645 prop
= nla_nest_start(skb
, TIPC_NLA_BEARER_PROP
);
649 __tipc_add_link_prop(skb
, msg
, lc
);
650 nla_nest_end(skb
, prop
);
651 nla_nest_end(skb
, bearer
);
656 static int __tipc_nl_compat_link_set(struct sk_buff
*skb
,
657 struct tipc_nl_compat_msg
*msg
)
661 struct tipc_link_config
*lc
;
663 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
665 link
= nla_nest_start(skb
, TIPC_NLA_LINK
);
669 if (nla_put_string(skb
, TIPC_NLA_LINK_NAME
, lc
->name
))
672 prop
= nla_nest_start(skb
, TIPC_NLA_LINK_PROP
);
676 __tipc_add_link_prop(skb
, msg
, lc
);
677 nla_nest_end(skb
, prop
);
678 nla_nest_end(skb
, link
);
683 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit
*cmd
,
685 struct tipc_nl_compat_msg
*msg
)
687 struct tipc_link_config
*lc
;
688 struct tipc_bearer
*bearer
;
689 struct tipc_media
*media
;
691 lc
= (struct tipc_link_config
*)TLV_DATA(msg
->req
);
693 media
= tipc_media_find(lc
->name
);
695 cmd
->doit
= &tipc_nl_media_set
;
696 return tipc_nl_compat_media_set(skb
, msg
);
699 bearer
= tipc_bearer_find(msg
->net
, lc
->name
);
701 cmd
->doit
= &tipc_nl_bearer_set
;
702 return tipc_nl_compat_bearer_set(skb
, msg
);
705 return __tipc_nl_compat_link_set(skb
, msg
);
708 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit
*cmd
,
710 struct tipc_nl_compat_msg
*msg
)
715 name
= (char *)TLV_DATA(msg
->req
);
717 link
= nla_nest_start(skb
, TIPC_NLA_LINK
);
721 if (nla_put_string(skb
, TIPC_NLA_LINK_NAME
, name
))
724 nla_nest_end(skb
, link
);
729 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg
*msg
)
733 struct tipc_name_table_query
*ntq
;
734 static const char * const header
[] = {
741 ntq
= (struct tipc_name_table_query
*)TLV_DATA(msg
->req
);
743 depth
= ntohl(ntq
->depth
);
747 for (i
= 0; i
< depth
; i
++)
748 tipc_tlv_sprintf(msg
->rep
, header
[i
]);
749 tipc_tlv_sprintf(msg
->rep
, "\n");
754 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg
*msg
,
755 struct nlattr
**attrs
)
758 struct tipc_name_table_query
*ntq
;
759 struct nlattr
*nt
[TIPC_NLA_NAME_TABLE_MAX
+ 1];
760 struct nlattr
*publ
[TIPC_NLA_PUBL_MAX
+ 1];
761 u32 node
, depth
, type
, lowbound
, upbound
;
762 static const char * const scope_str
[] = {"", " zone", " cluster",
765 nla_parse_nested(nt
, TIPC_NLA_NAME_TABLE_MAX
,
766 attrs
[TIPC_NLA_NAME_TABLE
], NULL
);
768 nla_parse_nested(publ
, TIPC_NLA_PUBL_MAX
, nt
[TIPC_NLA_NAME_TABLE_PUBL
],
771 ntq
= (struct tipc_name_table_query
*)TLV_DATA(msg
->req
);
773 depth
= ntohl(ntq
->depth
);
774 type
= ntohl(ntq
->type
);
775 lowbound
= ntohl(ntq
->lowbound
);
776 upbound
= ntohl(ntq
->upbound
);
778 if (!(depth
& TIPC_NTQ_ALLTYPES
) &&
779 (type
!= nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
])))
781 if (lowbound
&& (lowbound
> nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
])))
783 if (upbound
&& (upbound
< nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
])))
786 tipc_tlv_sprintf(msg
->rep
, "%-10u ",
787 nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
]));
792 tipc_tlv_sprintf(msg
->rep
, "%-10u %-10u ",
793 nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
]),
794 nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
]));
799 node
= nla_get_u32(publ
[TIPC_NLA_PUBL_NODE
]);
800 sprintf(port_str
, "<%u.%u.%u:%u>", tipc_zone(node
), tipc_cluster(node
),
801 tipc_node(node
), nla_get_u32(publ
[TIPC_NLA_PUBL_REF
]));
802 tipc_tlv_sprintf(msg
->rep
, "%-26s ", port_str
);
807 tipc_tlv_sprintf(msg
->rep
, "%-10u %s",
808 nla_get_u32(publ
[TIPC_NLA_PUBL_KEY
]),
809 scope_str
[nla_get_u32(publ
[TIPC_NLA_PUBL_SCOPE
])]);
811 tipc_tlv_sprintf(msg
->rep
, "\n");
816 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg
*msg
,
817 struct nlattr
**attrs
)
819 u32 type
, lower
, upper
;
820 struct nlattr
*publ
[TIPC_NLA_PUBL_MAX
+ 1];
822 nla_parse_nested(publ
, TIPC_NLA_PUBL_MAX
, attrs
[TIPC_NLA_PUBL
], NULL
);
824 type
= nla_get_u32(publ
[TIPC_NLA_PUBL_TYPE
]);
825 lower
= nla_get_u32(publ
[TIPC_NLA_PUBL_LOWER
]);
826 upper
= nla_get_u32(publ
[TIPC_NLA_PUBL_UPPER
]);
829 tipc_tlv_sprintf(msg
->rep
, " {%u,%u}", type
, lower
);
831 tipc_tlv_sprintf(msg
->rep
, " {%u,%u,%u}", type
, lower
, upper
);
836 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg
*msg
, u32 sock
)
841 struct sk_buff
*args
;
842 struct tipc_nl_compat_cmd_dump dump
;
844 args
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
848 hdr
= genlmsg_put(args
, 0, 0, &tipc_genl_family
, NLM_F_MULTI
,
851 nest
= nla_nest_start(args
, TIPC_NLA_SOCK
);
857 if (nla_put_u32(args
, TIPC_NLA_SOCK_REF
, sock
)) {
862 nla_nest_end(args
, nest
);
863 genlmsg_end(args
, hdr
);
865 dump
.dumpit
= tipc_nl_publ_dump
;
866 dump
.format
= __tipc_nl_compat_publ_dump
;
868 err
= __tipc_nl_compat_dumpit(&dump
, msg
, args
);
875 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg
*msg
,
876 struct nlattr
**attrs
)
880 struct nlattr
*sock
[TIPC_NLA_SOCK_MAX
+ 1];
882 nla_parse_nested(sock
, TIPC_NLA_SOCK_MAX
, attrs
[TIPC_NLA_SOCK
], NULL
);
884 sock_ref
= nla_get_u32(sock
[TIPC_NLA_SOCK_REF
]);
885 tipc_tlv_sprintf(msg
->rep
, "%u:", sock_ref
);
887 if (sock
[TIPC_NLA_SOCK_CON
]) {
889 struct nlattr
*con
[TIPC_NLA_CON_MAX
+ 1];
891 nla_parse_nested(con
, TIPC_NLA_CON_MAX
, sock
[TIPC_NLA_SOCK_CON
],
894 node
= nla_get_u32(con
[TIPC_NLA_CON_NODE
]);
895 tipc_tlv_sprintf(msg
->rep
, " connected to <%u.%u.%u:%u>",
899 nla_get_u32(con
[TIPC_NLA_CON_SOCK
]));
901 if (con
[TIPC_NLA_CON_FLAG
])
902 tipc_tlv_sprintf(msg
->rep
, " via {%u,%u}\n",
903 nla_get_u32(con
[TIPC_NLA_CON_TYPE
]),
904 nla_get_u32(con
[TIPC_NLA_CON_INST
]));
906 tipc_tlv_sprintf(msg
->rep
, "\n");
907 } else if (sock
[TIPC_NLA_SOCK_HAS_PUBL
]) {
908 tipc_tlv_sprintf(msg
->rep
, " bound to");
910 err
= tipc_nl_compat_publ_dump(msg
, sock_ref
);
914 tipc_tlv_sprintf(msg
->rep
, "\n");
919 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg
*msg
,
920 struct nlattr
**attrs
)
922 struct nlattr
*media
[TIPC_NLA_MEDIA_MAX
+ 1];
924 nla_parse_nested(media
, TIPC_NLA_MEDIA_MAX
, attrs
[TIPC_NLA_MEDIA
],
927 return tipc_add_tlv(msg
->rep
, TIPC_TLV_MEDIA_NAME
,
928 nla_data(media
[TIPC_NLA_MEDIA_NAME
]),
929 nla_len(media
[TIPC_NLA_MEDIA_NAME
]));
932 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg
*msg
,
933 struct nlattr
**attrs
)
935 struct tipc_node_info node_info
;
936 struct nlattr
*node
[TIPC_NLA_NODE_MAX
+ 1];
938 nla_parse_nested(node
, TIPC_NLA_NODE_MAX
, attrs
[TIPC_NLA_NODE
], NULL
);
940 node_info
.addr
= htonl(nla_get_u32(node
[TIPC_NLA_NODE_ADDR
]));
941 node_info
.up
= htonl(nla_get_flag(node
[TIPC_NLA_NODE_UP
]));
943 return tipc_add_tlv(msg
->rep
, TIPC_TLV_NODE_INFO
, &node_info
,
947 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit
*cmd
,
949 struct tipc_nl_compat_msg
*msg
)
954 val
= ntohl(*(__be32
*)TLV_DATA(msg
->req
));
956 net
= nla_nest_start(skb
, TIPC_NLA_NET
);
960 if (msg
->cmd
== TIPC_CMD_SET_NODE_ADDR
) {
961 if (nla_put_u32(skb
, TIPC_NLA_NET_ADDR
, val
))
963 } else if (msg
->cmd
== TIPC_CMD_SET_NETID
) {
964 if (nla_put_u32(skb
, TIPC_NLA_NET_ID
, val
))
967 nla_nest_end(skb
, net
);
972 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg
*msg
,
973 struct nlattr
**attrs
)
976 struct nlattr
*net
[TIPC_NLA_NET_MAX
+ 1];
978 nla_parse_nested(net
, TIPC_NLA_NET_MAX
, attrs
[TIPC_NLA_NET
], NULL
);
979 id
= htonl(nla_get_u32(net
[TIPC_NLA_NET_ID
]));
981 return tipc_add_tlv(msg
->rep
, TIPC_TLV_UNSIGNED
, &id
, sizeof(id
));
984 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg
*msg
)
986 msg
->rep
= tipc_tlv_alloc(ULTRA_STRING_MAX_LEN
);
990 tipc_tlv_init(msg
->rep
, TIPC_TLV_ULTRA_STRING
);
991 tipc_tlv_sprintf(msg
->rep
, "TIPC version " TIPC_MOD_VER
"\n");
996 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg
*msg
)
998 struct tipc_nl_compat_cmd_dump dump
;
999 struct tipc_nl_compat_cmd_doit doit
;
1001 memset(&dump
, 0, sizeof(dump
));
1002 memset(&doit
, 0, sizeof(doit
));
1006 msg
->rep
= tipc_tlv_alloc(0);
1010 case TIPC_CMD_GET_BEARER_NAMES
:
1011 msg
->rep_size
= MAX_BEARERS
* TLV_SPACE(TIPC_MAX_BEARER_NAME
);
1012 dump
.dumpit
= tipc_nl_bearer_dump
;
1013 dump
.format
= tipc_nl_compat_bearer_dump
;
1014 return tipc_nl_compat_dumpit(&dump
, msg
);
1015 case TIPC_CMD_ENABLE_BEARER
:
1016 msg
->req_type
= TIPC_TLV_BEARER_CONFIG
;
1017 doit
.doit
= tipc_nl_bearer_enable
;
1018 doit
.transcode
= tipc_nl_compat_bearer_enable
;
1019 return tipc_nl_compat_doit(&doit
, msg
);
1020 case TIPC_CMD_DISABLE_BEARER
:
1021 msg
->req_type
= TIPC_TLV_BEARER_NAME
;
1022 doit
.doit
= tipc_nl_bearer_disable
;
1023 doit
.transcode
= tipc_nl_compat_bearer_disable
;
1024 return tipc_nl_compat_doit(&doit
, msg
);
1025 case TIPC_CMD_SHOW_LINK_STATS
:
1026 msg
->req_type
= TIPC_TLV_LINK_NAME
;
1027 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1028 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1029 dump
.dumpit
= tipc_nl_link_dump
;
1030 dump
.format
= tipc_nl_compat_link_stat_dump
;
1031 return tipc_nl_compat_dumpit(&dump
, msg
);
1032 case TIPC_CMD_GET_LINKS
:
1033 msg
->req_type
= TIPC_TLV_NET_ADDR
;
1034 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1035 dump
.dumpit
= tipc_nl_link_dump
;
1036 dump
.format
= tipc_nl_compat_link_dump
;
1037 return tipc_nl_compat_dumpit(&dump
, msg
);
1038 case TIPC_CMD_SET_LINK_TOL
:
1039 case TIPC_CMD_SET_LINK_PRI
:
1040 case TIPC_CMD_SET_LINK_WINDOW
:
1041 msg
->req_type
= TIPC_TLV_LINK_CONFIG
;
1042 doit
.doit
= tipc_nl_link_set
;
1043 doit
.transcode
= tipc_nl_compat_link_set
;
1044 return tipc_nl_compat_doit(&doit
, msg
);
1045 case TIPC_CMD_RESET_LINK_STATS
:
1046 msg
->req_type
= TIPC_TLV_LINK_NAME
;
1047 doit
.doit
= tipc_nl_link_reset_stats
;
1048 doit
.transcode
= tipc_nl_compat_link_reset_stats
;
1049 return tipc_nl_compat_doit(&doit
, msg
);
1050 case TIPC_CMD_SHOW_NAME_TABLE
:
1051 msg
->req_type
= TIPC_TLV_NAME_TBL_QUERY
;
1052 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1053 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1054 dump
.header
= tipc_nl_compat_name_table_dump_header
;
1055 dump
.dumpit
= tipc_nl_name_table_dump
;
1056 dump
.format
= tipc_nl_compat_name_table_dump
;
1057 return tipc_nl_compat_dumpit(&dump
, msg
);
1058 case TIPC_CMD_SHOW_PORTS
:
1059 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1060 msg
->rep_type
= TIPC_TLV_ULTRA_STRING
;
1061 dump
.dumpit
= tipc_nl_sk_dump
;
1062 dump
.format
= tipc_nl_compat_sk_dump
;
1063 return tipc_nl_compat_dumpit(&dump
, msg
);
1064 case TIPC_CMD_GET_MEDIA_NAMES
:
1065 msg
->rep_size
= MAX_MEDIA
* TLV_SPACE(TIPC_MAX_MEDIA_NAME
);
1066 dump
.dumpit
= tipc_nl_media_dump
;
1067 dump
.format
= tipc_nl_compat_media_dump
;
1068 return tipc_nl_compat_dumpit(&dump
, msg
);
1069 case TIPC_CMD_GET_NODES
:
1070 msg
->rep_size
= ULTRA_STRING_MAX_LEN
;
1071 dump
.dumpit
= tipc_nl_node_dump
;
1072 dump
.format
= tipc_nl_compat_node_dump
;
1073 return tipc_nl_compat_dumpit(&dump
, msg
);
1074 case TIPC_CMD_SET_NODE_ADDR
:
1075 msg
->req_type
= TIPC_TLV_NET_ADDR
;
1076 doit
.doit
= tipc_nl_net_set
;
1077 doit
.transcode
= tipc_nl_compat_net_set
;
1078 return tipc_nl_compat_doit(&doit
, msg
);
1079 case TIPC_CMD_SET_NETID
:
1080 msg
->req_type
= TIPC_TLV_UNSIGNED
;
1081 doit
.doit
= tipc_nl_net_set
;
1082 doit
.transcode
= tipc_nl_compat_net_set
;
1083 return tipc_nl_compat_doit(&doit
, msg
);
1084 case TIPC_CMD_GET_NETID
:
1085 msg
->rep_size
= sizeof(u32
);
1086 dump
.dumpit
= tipc_nl_net_dump
;
1087 dump
.format
= tipc_nl_compat_net_dump
;
1088 return tipc_nl_compat_dumpit(&dump
, msg
);
1089 case TIPC_CMD_SHOW_STATS
:
1090 return tipc_cmd_show_stats_compat(msg
);
1096 static int tipc_nl_compat_recv(struct sk_buff
*skb
, struct genl_info
*info
)
1100 struct tipc_nl_compat_msg msg
;
1101 struct nlmsghdr
*req_nlh
;
1102 struct nlmsghdr
*rep_nlh
;
1103 struct tipc_genlmsghdr
*req_userhdr
= info
->userhdr
;
1105 memset(&msg
, 0, sizeof(msg
));
1107 req_nlh
= (struct nlmsghdr
*)skb
->data
;
1108 msg
.req
= nlmsg_data(req_nlh
) + GENL_HDRLEN
+ TIPC_GENL_HDRLEN
;
1109 msg
.cmd
= req_userhdr
->cmd
;
1110 msg
.dst_sk
= info
->dst_sk
;
1111 msg
.net
= genl_info_net(info
);
1113 if ((msg
.cmd
& 0xC000) && (!netlink_net_capable(skb
, CAP_NET_ADMIN
))) {
1114 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN
);
1119 len
= nlmsg_attrlen(req_nlh
, GENL_HDRLEN
+ TIPC_GENL_HDRLEN
);
1120 if (len
&& !TLV_OK(msg
.req
, len
)) {
1121 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED
);
1126 err
= tipc_nl_compat_handle(&msg
);
1127 if ((err
== -EOPNOTSUPP
) || (err
== -EPERM
))
1128 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED
);
1129 else if (err
== -EINVAL
)
1130 msg
.rep
= tipc_get_err_tlv(TIPC_CFG_TLV_ERROR
);
1135 len
= nlmsg_total_size(GENL_HDRLEN
+ TIPC_GENL_HDRLEN
);
1136 skb_push(msg
.rep
, len
);
1137 rep_nlh
= nlmsg_hdr(msg
.rep
);
1138 memcpy(rep_nlh
, info
->nlhdr
, len
);
1139 rep_nlh
->nlmsg_len
= msg
.rep
->len
;
1140 genlmsg_unicast(msg
.net
, msg
.rep
, NETLINK_CB(skb
).portid
);
1145 static struct genl_family tipc_genl_compat_family
= {
1146 .id
= GENL_ID_GENERATE
,
1147 .name
= TIPC_GENL_NAME
,
1148 .version
= TIPC_GENL_VERSION
,
1149 .hdrsize
= TIPC_GENL_HDRLEN
,
1154 static struct genl_ops tipc_genl_compat_ops
[] = {
1156 .cmd
= TIPC_GENL_CMD
,
1157 .doit
= tipc_nl_compat_recv
,
1161 int tipc_netlink_compat_start(void)
1165 res
= genl_register_family_with_ops(&tipc_genl_compat_family
,
1166 tipc_genl_compat_ops
);
1168 pr_err("Failed to register legacy compat interface\n");
1175 void tipc_netlink_compat_stop(void)
1177 genl_unregister_family(&tipc_genl_compat_family
);