xref: /openbmc/linux/net/tipc/netlink_compat.c (revision 927fff9b)
1bfb3e5ddSRichard Alpe /*
2bfb3e5ddSRichard Alpe  * Copyright (c) 2014, Ericsson AB
3bfb3e5ddSRichard Alpe  * All rights reserved.
4bfb3e5ddSRichard Alpe  *
5bfb3e5ddSRichard Alpe  * Redistribution and use in source and binary forms, with or without
6bfb3e5ddSRichard Alpe  * modification, are permitted provided that the following conditions are met:
7bfb3e5ddSRichard Alpe  *
8bfb3e5ddSRichard Alpe  * 1. Redistributions of source code must retain the above copyright
9bfb3e5ddSRichard Alpe  *    notice, this list of conditions and the following disclaimer.
10bfb3e5ddSRichard Alpe  * 2. Redistributions in binary form must reproduce the above copyright
11bfb3e5ddSRichard Alpe  *    notice, this list of conditions and the following disclaimer in the
12bfb3e5ddSRichard Alpe  *    documentation and/or other materials provided with the distribution.
13bfb3e5ddSRichard Alpe  * 3. Neither the names of the copyright holders nor the names of its
14bfb3e5ddSRichard Alpe  *    contributors may be used to endorse or promote products derived from
15bfb3e5ddSRichard Alpe  *    this software without specific prior written permission.
16bfb3e5ddSRichard Alpe  *
17bfb3e5ddSRichard Alpe  * Alternatively, this software may be distributed under the terms of the
18bfb3e5ddSRichard Alpe  * GNU General Public License ("GPL") version 2 as published by the Free
19bfb3e5ddSRichard Alpe  * Software Foundation.
20bfb3e5ddSRichard Alpe  *
21bfb3e5ddSRichard Alpe  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22bfb3e5ddSRichard Alpe  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23bfb3e5ddSRichard Alpe  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24bfb3e5ddSRichard Alpe  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25bfb3e5ddSRichard Alpe  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26bfb3e5ddSRichard Alpe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27bfb3e5ddSRichard Alpe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28bfb3e5ddSRichard Alpe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29bfb3e5ddSRichard Alpe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30bfb3e5ddSRichard Alpe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31bfb3e5ddSRichard Alpe  * POSSIBILITY OF SUCH DAMAGE.
32bfb3e5ddSRichard Alpe  */
33bfb3e5ddSRichard Alpe 
34bfb3e5ddSRichard Alpe #include "core.h"
35d0796d1eSRichard Alpe #include "bearer.h"
36f2b3b2d4SRichard Alpe #include "link.h"
3744a8ae94SRichard Alpe #include "name_table.h"
38487d2a3aSRichard Alpe #include "socket.h"
394b28cb58SRichard Alpe #include "node.h"
40964f9501SRichard Alpe #include "net.h"
41bfb3e5ddSRichard Alpe #include <net/genetlink.h>
42f1db99c0SAndy Shevchenko #include <linux/string_helpers.h>
43bfb3e5ddSRichard Alpe #include <linux/tipc_config.h>
44bfb3e5ddSRichard Alpe 
45d0796d1eSRichard Alpe /* The legacy API had an artificial message length limit called
46d0796d1eSRichard Alpe  * ULTRA_STRING_MAX_LEN.
47d0796d1eSRichard Alpe  */
48d0796d1eSRichard Alpe #define ULTRA_STRING_MAX_LEN 32768
49d0796d1eSRichard Alpe 
50d0796d1eSRichard Alpe #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
51d0796d1eSRichard Alpe 
52d0796d1eSRichard Alpe #define REPLY_TRUNCATED "<truncated>\n"
53d0796d1eSRichard Alpe 
54d0796d1eSRichard Alpe struct tipc_nl_compat_msg {
55d0796d1eSRichard Alpe 	u16 cmd;
56f2b3b2d4SRichard Alpe 	int rep_type;
57d0796d1eSRichard Alpe 	int rep_size;
589ab15465SRichard Alpe 	int req_type;
594da5f001STaras Kondratiuk 	int req_size;
60c3d6fb85SRichard Alpe 	struct net *net;
61d0796d1eSRichard Alpe 	struct sk_buff *rep;
62d0796d1eSRichard Alpe 	struct tlv_desc *req;
63d0796d1eSRichard Alpe 	struct sock *dst_sk;
64d0796d1eSRichard Alpe };
65d0796d1eSRichard Alpe 
66d0796d1eSRichard Alpe struct tipc_nl_compat_cmd_dump {
6744a8ae94SRichard Alpe 	int (*header)(struct tipc_nl_compat_msg *);
68d0796d1eSRichard Alpe 	int (*dumpit)(struct sk_buff *, struct netlink_callback *);
69d0796d1eSRichard Alpe 	int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
70d0796d1eSRichard Alpe };
71d0796d1eSRichard Alpe 
729ab15465SRichard Alpe struct tipc_nl_compat_cmd_doit {
739ab15465SRichard Alpe 	int (*doit)(struct sk_buff *skb, struct genl_info *info);
74c3d6fb85SRichard Alpe 	int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
75c3d6fb85SRichard Alpe 			 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
769ab15465SRichard Alpe };
779ab15465SRichard Alpe 
tipc_skb_tailroom(struct sk_buff * skb)78d0796d1eSRichard Alpe static int tipc_skb_tailroom(struct sk_buff *skb)
79d0796d1eSRichard Alpe {
80d0796d1eSRichard Alpe 	int tailroom;
81d0796d1eSRichard Alpe 	int limit;
82d0796d1eSRichard Alpe 
83d0796d1eSRichard Alpe 	tailroom = skb_tailroom(skb);
84d0796d1eSRichard Alpe 	limit = TIPC_SKB_MAX - skb->len;
85d0796d1eSRichard Alpe 
86d0796d1eSRichard Alpe 	if (tailroom < limit)
87d0796d1eSRichard Alpe 		return tailroom;
88d0796d1eSRichard Alpe 
89d0796d1eSRichard Alpe 	return limit;
90d0796d1eSRichard Alpe }
91d0796d1eSRichard Alpe 
TLV_GET_DATA_LEN(struct tlv_desc * tlv)928b66fee7SYing Xue static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
938b66fee7SYing Xue {
948b66fee7SYing Xue 	return TLV_GET_LEN(tlv) - TLV_SPACE(0);
958b66fee7SYing Xue }
968b66fee7SYing Xue 
tipc_add_tlv(struct sk_buff * skb,u16 type,void * data,u16 len)97d0796d1eSRichard Alpe static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
98d0796d1eSRichard Alpe {
99d0796d1eSRichard Alpe 	struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
100d0796d1eSRichard Alpe 
101d0796d1eSRichard Alpe 	if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
102d0796d1eSRichard Alpe 		return -EMSGSIZE;
103d0796d1eSRichard Alpe 
104d0796d1eSRichard Alpe 	skb_put(skb, TLV_SPACE(len));
105*927fff9bSShigeru Yoshida 	memset(tlv, 0, TLV_SPACE(len));
106d0796d1eSRichard Alpe 	tlv->tlv_type = htons(type);
107d0796d1eSRichard Alpe 	tlv->tlv_len = htons(TLV_LENGTH(len));
108d0796d1eSRichard Alpe 	if (len && data)
109d0796d1eSRichard Alpe 		memcpy(TLV_DATA(tlv), data, len);
110d0796d1eSRichard Alpe 
111d0796d1eSRichard Alpe 	return 0;
112d0796d1eSRichard Alpe }
113d0796d1eSRichard Alpe 
tipc_tlv_init(struct sk_buff * skb,u16 type)114f2b3b2d4SRichard Alpe static void tipc_tlv_init(struct sk_buff *skb, u16 type)
115f2b3b2d4SRichard Alpe {
116f2b3b2d4SRichard Alpe 	struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
117f2b3b2d4SRichard Alpe 
118f2b3b2d4SRichard Alpe 	TLV_SET_LEN(tlv, 0);
119f2b3b2d4SRichard Alpe 	TLV_SET_TYPE(tlv, type);
120f2b3b2d4SRichard Alpe 	skb_put(skb, sizeof(struct tlv_desc));
121f2b3b2d4SRichard Alpe }
122f2b3b2d4SRichard Alpe 
tipc_tlv_sprintf(struct sk_buff * skb,const char * fmt,...)12379b1119bSAndrew Lunn static __printf(2, 3) int tipc_tlv_sprintf(struct sk_buff *skb,
12479b1119bSAndrew Lunn 					   const char *fmt, ...)
125f2b3b2d4SRichard Alpe {
126f2b3b2d4SRichard Alpe 	int n;
127f2b3b2d4SRichard Alpe 	u16 len;
128f2b3b2d4SRichard Alpe 	u32 rem;
129f2b3b2d4SRichard Alpe 	char *buf;
130f2b3b2d4SRichard Alpe 	struct tlv_desc *tlv;
131f2b3b2d4SRichard Alpe 	va_list args;
132f2b3b2d4SRichard Alpe 
133f2b3b2d4SRichard Alpe 	rem = tipc_skb_tailroom(skb);
134f2b3b2d4SRichard Alpe 
135f2b3b2d4SRichard Alpe 	tlv = (struct tlv_desc *)skb->data;
136f2b3b2d4SRichard Alpe 	len = TLV_GET_LEN(tlv);
137f2b3b2d4SRichard Alpe 	buf = TLV_DATA(tlv) + len;
138f2b3b2d4SRichard Alpe 
139f2b3b2d4SRichard Alpe 	va_start(args, fmt);
140f2b3b2d4SRichard Alpe 	n = vscnprintf(buf, rem, fmt, args);
141f2b3b2d4SRichard Alpe 	va_end(args);
142f2b3b2d4SRichard Alpe 
143f2b3b2d4SRichard Alpe 	TLV_SET_LEN(tlv, n + len);
144f2b3b2d4SRichard Alpe 	skb_put(skb, n);
145f2b3b2d4SRichard Alpe 
146f2b3b2d4SRichard Alpe 	return n;
147f2b3b2d4SRichard Alpe }
148f2b3b2d4SRichard Alpe 
tipc_tlv_alloc(int size)149d0796d1eSRichard Alpe static struct sk_buff *tipc_tlv_alloc(int size)
150d0796d1eSRichard Alpe {
151d0796d1eSRichard Alpe 	int hdr_len;
152d0796d1eSRichard Alpe 	struct sk_buff *buf;
153d0796d1eSRichard Alpe 
154d0796d1eSRichard Alpe 	size = TLV_SPACE(size);
155d0796d1eSRichard Alpe 	hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
156d0796d1eSRichard Alpe 
157d0796d1eSRichard Alpe 	buf = alloc_skb(hdr_len + size, GFP_KERNEL);
158d0796d1eSRichard Alpe 	if (!buf)
159d0796d1eSRichard Alpe 		return NULL;
160d0796d1eSRichard Alpe 
161d0796d1eSRichard Alpe 	skb_reserve(buf, hdr_len);
162d0796d1eSRichard Alpe 
163d0796d1eSRichard Alpe 	return buf;
164d0796d1eSRichard Alpe }
165d0796d1eSRichard Alpe 
tipc_get_err_tlv(char * str)166d0796d1eSRichard Alpe static struct sk_buff *tipc_get_err_tlv(char *str)
167d0796d1eSRichard Alpe {
168d0796d1eSRichard Alpe 	int str_len = strlen(str) + 1;
169d0796d1eSRichard Alpe 	struct sk_buff *buf;
170d0796d1eSRichard Alpe 
171d0796d1eSRichard Alpe 	buf = tipc_tlv_alloc(TLV_SPACE(str_len));
172d0796d1eSRichard Alpe 	if (buf)
173d0796d1eSRichard Alpe 		tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
174d0796d1eSRichard Alpe 
175d0796d1eSRichard Alpe 	return buf;
176d0796d1eSRichard Alpe }
177d0796d1eSRichard Alpe 
__tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump * cmd,struct tipc_nl_compat_msg * msg,struct sk_buff * arg)178d0796d1eSRichard Alpe static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
179d0796d1eSRichard Alpe 				   struct tipc_nl_compat_msg *msg,
180d0796d1eSRichard Alpe 				   struct sk_buff *arg)
181d0796d1eSRichard Alpe {
1826ea67769SJiri Pirko 	struct genl_dumpit_info info;
183d0796d1eSRichard Alpe 	int len = 0;
184d0796d1eSRichard Alpe 	int err;
185d0796d1eSRichard Alpe 	struct sk_buff *buf;
186d0796d1eSRichard Alpe 	struct nlmsghdr *nlmsg;
187d0796d1eSRichard Alpe 	struct netlink_callback cb;
188c6c08614SJiri Pirko 	struct nlattr **attrbuf;
189d0796d1eSRichard Alpe 
190d0796d1eSRichard Alpe 	memset(&cb, 0, sizeof(cb));
191d0796d1eSRichard Alpe 	cb.nlh = (struct nlmsghdr *)arg->data;
192d0796d1eSRichard Alpe 	cb.skb = arg;
1936ea67769SJiri Pirko 	cb.data = &info;
194d0796d1eSRichard Alpe 
195d0796d1eSRichard Alpe 	buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
196d0796d1eSRichard Alpe 	if (!buf)
197d0796d1eSRichard Alpe 		return -ENOMEM;
198d0796d1eSRichard Alpe 
199d0796d1eSRichard Alpe 	buf->sk = msg->dst_sk;
20012a78b02SCong Wang 	if (__tipc_dump_start(&cb, msg->net)) {
20112a78b02SCong Wang 		kfree_skb(buf);
20212a78b02SCong Wang 		return -ENOMEM;
20312a78b02SCong Wang 	}
204d0796d1eSRichard Alpe 
205a7869e5fSYing Xue 	attrbuf = kcalloc(tipc_genl_family.maxattr + 1,
206c6c08614SJiri Pirko 			  sizeof(struct nlattr *), GFP_KERNEL);
207c6c08614SJiri Pirko 	if (!attrbuf) {
208c6c08614SJiri Pirko 		err = -ENOMEM;
209c6c08614SJiri Pirko 		goto err_out;
210c6c08614SJiri Pirko 	}
211c6c08614SJiri Pirko 
2127288dd2fSJakub Kicinski 	info.info.attrs = attrbuf;
213c32c928dSHoang Le 
214c32c928dSHoang Le 	if (nlmsg_len(cb.nlh) > 0) {
2156ea67769SJiri Pirko 		err = nlmsg_parse_deprecated(cb.nlh, GENL_HDRLEN, attrbuf,
2166ea67769SJiri Pirko 					     tipc_genl_family.maxattr,
2176ea67769SJiri Pirko 					     tipc_genl_family.policy, NULL);
2186ea67769SJiri Pirko 		if (err)
2196ea67769SJiri Pirko 			goto err_out;
220c32c928dSHoang Le 	}
221d0796d1eSRichard Alpe 	do {
222d0796d1eSRichard Alpe 		int rem;
223d0796d1eSRichard Alpe 
224d0796d1eSRichard Alpe 		len = (*cmd->dumpit)(buf, &cb);
225d0796d1eSRichard Alpe 
226d0796d1eSRichard Alpe 		nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
227c6c08614SJiri Pirko 			err = nlmsg_parse_deprecated(nlmsg, GENL_HDRLEN,
228c6c08614SJiri Pirko 						     attrbuf,
229c6c08614SJiri Pirko 						     tipc_genl_family.maxattr,
230c6c08614SJiri Pirko 						     tipc_genl_family.policy,
231c6c08614SJiri Pirko 						     NULL);
232d0796d1eSRichard Alpe 			if (err)
233d0796d1eSRichard Alpe 				goto err_out;
234d0796d1eSRichard Alpe 
235c6c08614SJiri Pirko 			err = (*cmd->format)(msg, attrbuf);
236d0796d1eSRichard Alpe 			if (err)
237d0796d1eSRichard Alpe 				goto err_out;
238d0796d1eSRichard Alpe 
239d0796d1eSRichard Alpe 			if (tipc_skb_tailroom(msg->rep) <= 1) {
240d0796d1eSRichard Alpe 				err = -EMSGSIZE;
241d0796d1eSRichard Alpe 				goto err_out;
242d0796d1eSRichard Alpe 			}
243d0796d1eSRichard Alpe 		}
244d0796d1eSRichard Alpe 
245d0796d1eSRichard Alpe 		skb_reset_tail_pointer(buf);
246d0796d1eSRichard Alpe 		buf->len = 0;
247d0796d1eSRichard Alpe 
248d0796d1eSRichard Alpe 	} while (len);
249d0796d1eSRichard Alpe 
250d0796d1eSRichard Alpe 	err = 0;
251d0796d1eSRichard Alpe 
252d0796d1eSRichard Alpe err_out:
253c6c08614SJiri Pirko 	kfree(attrbuf);
2548f5c5fcfSCong Wang 	tipc_dump_done(&cb);
255d0796d1eSRichard Alpe 	kfree_skb(buf);
256d0796d1eSRichard Alpe 
257d0796d1eSRichard Alpe 	if (err == -EMSGSIZE) {
258d0796d1eSRichard Alpe 		/* The legacy API only considered messages filling
259d0796d1eSRichard Alpe 		 * "ULTRA_STRING_MAX_LEN" to be truncated.
260d0796d1eSRichard Alpe 		 */
261d0796d1eSRichard Alpe 		if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
262d0796d1eSRichard Alpe 			char *tail = skb_tail_pointer(msg->rep);
263d0796d1eSRichard Alpe 
264d0796d1eSRichard Alpe 			if (*tail != '\0')
265d0796d1eSRichard Alpe 				sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
266d0796d1eSRichard Alpe 					REPLY_TRUNCATED);
267d0796d1eSRichard Alpe 		}
268d0796d1eSRichard Alpe 
269d0796d1eSRichard Alpe 		return 0;
270d0796d1eSRichard Alpe 	}
271d0796d1eSRichard Alpe 
272d0796d1eSRichard Alpe 	return err;
273d0796d1eSRichard Alpe }
274d0796d1eSRichard Alpe 
tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump * cmd,struct tipc_nl_compat_msg * msg)275d0796d1eSRichard Alpe static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
276d0796d1eSRichard Alpe 				 struct tipc_nl_compat_msg *msg)
277d0796d1eSRichard Alpe {
27847733f9dSCong Wang 	struct nlmsghdr *nlh;
279d0796d1eSRichard Alpe 	struct sk_buff *arg;
28047733f9dSCong Wang 	int err;
281d0796d1eSRichard Alpe 
2824da5f001STaras Kondratiuk 	if (msg->req_type && (!msg->req_size ||
2834da5f001STaras Kondratiuk 			      !TLV_CHECK_TYPE(msg->req, msg->req_type)))
284f2b3b2d4SRichard Alpe 		return -EINVAL;
285f2b3b2d4SRichard Alpe 
286d0796d1eSRichard Alpe 	msg->rep = tipc_tlv_alloc(msg->rep_size);
287d0796d1eSRichard Alpe 	if (!msg->rep)
288d0796d1eSRichard Alpe 		return -ENOMEM;
289d0796d1eSRichard Alpe 
290f2b3b2d4SRichard Alpe 	if (msg->rep_type)
291f2b3b2d4SRichard Alpe 		tipc_tlv_init(msg->rep, msg->rep_type);
292f2b3b2d4SRichard Alpe 
2932ac695d1SXin Long 	if (cmd->header) {
2942ac695d1SXin Long 		err = (*cmd->header)(msg);
2952ac695d1SXin Long 		if (err) {
2962ac695d1SXin Long 			kfree_skb(msg->rep);
2972ac695d1SXin Long 			msg->rep = NULL;
2982ac695d1SXin Long 			return err;
2992ac695d1SXin Long 		}
3002ac695d1SXin Long 	}
30144a8ae94SRichard Alpe 
302d0796d1eSRichard Alpe 	arg = nlmsg_new(0, GFP_KERNEL);
303d0796d1eSRichard Alpe 	if (!arg) {
304d0796d1eSRichard Alpe 		kfree_skb(msg->rep);
3055bfd37b4SEric Dumazet 		msg->rep = NULL;
306d0796d1eSRichard Alpe 		return -ENOMEM;
307d0796d1eSRichard Alpe 	}
308d0796d1eSRichard Alpe 
30947733f9dSCong Wang 	nlh = nlmsg_put(arg, 0, 0, tipc_genl_family.id, 0, NLM_F_MULTI);
31047733f9dSCong Wang 	if (!nlh) {
31147733f9dSCong Wang 		kfree_skb(arg);
31247733f9dSCong Wang 		kfree_skb(msg->rep);
31347733f9dSCong Wang 		msg->rep = NULL;
31447733f9dSCong Wang 		return -EMSGSIZE;
31547733f9dSCong Wang 	}
31647733f9dSCong Wang 	nlmsg_end(arg, nlh);
31747733f9dSCong Wang 
318d0796d1eSRichard Alpe 	err = __tipc_nl_compat_dumpit(cmd, msg, arg);
3195bfd37b4SEric Dumazet 	if (err) {
320d0796d1eSRichard Alpe 		kfree_skb(msg->rep);
3215bfd37b4SEric Dumazet 		msg->rep = NULL;
3225bfd37b4SEric Dumazet 	}
323d0796d1eSRichard Alpe 	kfree_skb(arg);
324d0796d1eSRichard Alpe 
325d0796d1eSRichard Alpe 	return err;
326d0796d1eSRichard Alpe }
327d0796d1eSRichard Alpe 
__tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit * cmd,struct tipc_nl_compat_msg * msg)3289ab15465SRichard Alpe static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
3299ab15465SRichard Alpe 				 struct tipc_nl_compat_msg *msg)
3309ab15465SRichard Alpe {
3319ab15465SRichard Alpe 	int err;
3329ab15465SRichard Alpe 	struct sk_buff *doit_buf;
3339ab15465SRichard Alpe 	struct sk_buff *trans_buf;
3349ab15465SRichard Alpe 	struct nlattr **attrbuf;
3359ab15465SRichard Alpe 	struct genl_info info;
3369ab15465SRichard Alpe 
3379ab15465SRichard Alpe 	trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3389ab15465SRichard Alpe 	if (!trans_buf)
3399ab15465SRichard Alpe 		return -ENOMEM;
3409ab15465SRichard Alpe 
3416da2ec56SKees Cook 	attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
3426da2ec56SKees Cook 				sizeof(struct nlattr *),
3436da2ec56SKees Cook 				GFP_KERNEL);
3449ab15465SRichard Alpe 	if (!attrbuf) {
3459ab15465SRichard Alpe 		err = -ENOMEM;
3469ab15465SRichard Alpe 		goto trans_out;
3479ab15465SRichard Alpe 	}
3489ab15465SRichard Alpe 
3499ab15465SRichard Alpe 	doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3509ab15465SRichard Alpe 	if (!doit_buf) {
3519ab15465SRichard Alpe 		err = -ENOMEM;
352e5d1a1eeSYing Xue 		goto attrbuf_out;
3539ab15465SRichard Alpe 	}
3549ab15465SRichard Alpe 
3559ab15465SRichard Alpe 	memset(&info, 0, sizeof(info));
3569ab15465SRichard Alpe 	info.attrs = attrbuf;
3579ab15465SRichard Alpe 
358ed4ffdfeSYing Xue 	rtnl_lock();
359e5d1a1eeSYing Xue 	err = (*cmd->transcode)(cmd, trans_buf, msg);
360e5d1a1eeSYing Xue 	if (err)
361e5d1a1eeSYing Xue 		goto doit_out;
362e5d1a1eeSYing Xue 
3638cb08174SJohannes Berg 	err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
364e5d1a1eeSYing Xue 				   (const struct nlattr *)trans_buf->data,
365e5d1a1eeSYing Xue 				   trans_buf->len, NULL, NULL);
366e5d1a1eeSYing Xue 	if (err)
367e5d1a1eeSYing Xue 		goto doit_out;
368e5d1a1eeSYing Xue 
369e5d1a1eeSYing Xue 	doit_buf->sk = msg->dst_sk;
370e5d1a1eeSYing Xue 
3719ab15465SRichard Alpe 	err = (*cmd->doit)(doit_buf, &info);
372e5d1a1eeSYing Xue doit_out:
373ed4ffdfeSYing Xue 	rtnl_unlock();
3749ab15465SRichard Alpe 
3759ab15465SRichard Alpe 	kfree_skb(doit_buf);
376e5d1a1eeSYing Xue attrbuf_out:
3779ab15465SRichard Alpe 	kfree(attrbuf);
3789ab15465SRichard Alpe trans_out:
3799ab15465SRichard Alpe 	kfree_skb(trans_buf);
3809ab15465SRichard Alpe 
3819ab15465SRichard Alpe 	return err;
3829ab15465SRichard Alpe }
3839ab15465SRichard Alpe 
tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit * cmd,struct tipc_nl_compat_msg * msg)3849ab15465SRichard Alpe static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
3859ab15465SRichard Alpe 			       struct tipc_nl_compat_msg *msg)
3869ab15465SRichard Alpe {
3879ab15465SRichard Alpe 	int err;
3889ab15465SRichard Alpe 
3894da5f001STaras Kondratiuk 	if (msg->req_type && (!msg->req_size ||
3904da5f001STaras Kondratiuk 			      !TLV_CHECK_TYPE(msg->req, msg->req_type)))
3919ab15465SRichard Alpe 		return -EINVAL;
3929ab15465SRichard Alpe 
3939ab15465SRichard Alpe 	err = __tipc_nl_compat_doit(cmd, msg);
3949ab15465SRichard Alpe 	if (err)
3959ab15465SRichard Alpe 		return err;
3969ab15465SRichard Alpe 
3979ab15465SRichard Alpe 	/* The legacy API considered an empty message a success message */
3989ab15465SRichard Alpe 	msg->rep = tipc_tlv_alloc(0);
3999ab15465SRichard Alpe 	if (!msg->rep)
4009ab15465SRichard Alpe 		return -ENOMEM;
4019ab15465SRichard Alpe 
4029ab15465SRichard Alpe 	return 0;
4039ab15465SRichard Alpe }
4049ab15465SRichard Alpe 
tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)405d0796d1eSRichard Alpe static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
406d0796d1eSRichard Alpe 				      struct nlattr **attrs)
407d0796d1eSRichard Alpe {
408d0796d1eSRichard Alpe 	struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
409297f7d2cSBaozeng Ding 	int err;
410d0796d1eSRichard Alpe 
411297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_BEARER])
412297f7d2cSBaozeng Ding 		return -EINVAL;
413297f7d2cSBaozeng Ding 
4148cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
415fceb6435SJohannes Berg 					  attrs[TIPC_NLA_BEARER], NULL, NULL);
416297f7d2cSBaozeng Ding 	if (err)
417297f7d2cSBaozeng Ding 		return err;
418d0796d1eSRichard Alpe 
419d0796d1eSRichard Alpe 	return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
420d0796d1eSRichard Alpe 			    nla_data(bearer[TIPC_NLA_BEARER_NAME]),
421d0796d1eSRichard Alpe 			    nla_len(bearer[TIPC_NLA_BEARER_NAME]));
422d0796d1eSRichard Alpe }
423d0796d1eSRichard Alpe 
tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)424c3d6fb85SRichard Alpe static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
425c3d6fb85SRichard Alpe 					struct sk_buff *skb,
4269ab15465SRichard Alpe 					struct tipc_nl_compat_msg *msg)
4279ab15465SRichard Alpe {
4289ab15465SRichard Alpe 	struct nlattr *prop;
4299ab15465SRichard Alpe 	struct nlattr *bearer;
4309ab15465SRichard Alpe 	struct tipc_bearer_config *b;
4310762216cSYing Xue 	int len;
4329ab15465SRichard Alpe 
4339ab15465SRichard Alpe 	b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
4349ab15465SRichard Alpe 
435ae0be8deSMichal Kubecek 	bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
4369ab15465SRichard Alpe 	if (!bearer)
4379ab15465SRichard Alpe 		return -EMSGSIZE;
4389ab15465SRichard Alpe 
4396f07e5f0SXin Long 	len = TLV_GET_DATA_LEN(msg->req);
4406f07e5f0SXin Long 	len -= offsetof(struct tipc_bearer_config, name);
4416f07e5f0SXin Long 	if (len <= 0)
4426f07e5f0SXin Long 		return -EINVAL;
4436f07e5f0SXin Long 
4446f07e5f0SXin Long 	len = min_t(int, len, TIPC_MAX_BEARER_NAME);
445f1db99c0SAndy Shevchenko 	if (!string_is_terminated(b->name, len))
4460762216cSYing Xue 		return -EINVAL;
4470762216cSYing Xue 
4489ab15465SRichard Alpe 	if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
4499ab15465SRichard Alpe 		return -EMSGSIZE;
4509ab15465SRichard Alpe 
4519ab15465SRichard Alpe 	if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
4529ab15465SRichard Alpe 		return -EMSGSIZE;
4539ab15465SRichard Alpe 
4549ab15465SRichard Alpe 	if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
455ae0be8deSMichal Kubecek 		prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
4569ab15465SRichard Alpe 		if (!prop)
4579ab15465SRichard Alpe 			return -EMSGSIZE;
4589ab15465SRichard Alpe 		if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
4599ab15465SRichard Alpe 			return -EMSGSIZE;
4609ab15465SRichard Alpe 		nla_nest_end(skb, prop);
4619ab15465SRichard Alpe 	}
4629ab15465SRichard Alpe 	nla_nest_end(skb, bearer);
4639ab15465SRichard Alpe 
4649ab15465SRichard Alpe 	return 0;
4659ab15465SRichard Alpe }
4669ab15465SRichard Alpe 
tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)467c3d6fb85SRichard Alpe static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
468c3d6fb85SRichard Alpe 					 struct sk_buff *skb,
4699ab15465SRichard Alpe 					 struct tipc_nl_compat_msg *msg)
4709ab15465SRichard Alpe {
4719ab15465SRichard Alpe 	char *name;
4729ab15465SRichard Alpe 	struct nlattr *bearer;
4730762216cSYing Xue 	int len;
4749ab15465SRichard Alpe 
4759ab15465SRichard Alpe 	name = (char *)TLV_DATA(msg->req);
4769ab15465SRichard Alpe 
477ae0be8deSMichal Kubecek 	bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
4789ab15465SRichard Alpe 	if (!bearer)
4799ab15465SRichard Alpe 		return -EMSGSIZE;
4809ab15465SRichard Alpe 
4814f07b80cSXin Long 	len = TLV_GET_DATA_LEN(msg->req);
4824f07b80cSXin Long 	if (len <= 0)
4834f07b80cSXin Long 		return -EINVAL;
4844f07b80cSXin Long 
4854f07b80cSXin Long 	len = min_t(int, len, TIPC_MAX_BEARER_NAME);
486f1db99c0SAndy Shevchenko 	if (!string_is_terminated(name, len))
4870762216cSYing Xue 		return -EINVAL;
4880762216cSYing Xue 
4899ab15465SRichard Alpe 	if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
4909ab15465SRichard Alpe 		return -EMSGSIZE;
4919ab15465SRichard Alpe 
4929ab15465SRichard Alpe 	nla_nest_end(skb, bearer);
4939ab15465SRichard Alpe 
4949ab15465SRichard Alpe 	return 0;
4959ab15465SRichard Alpe }
4969ab15465SRichard Alpe 
perc(u32 count,u32 total)497f2b3b2d4SRichard Alpe static inline u32 perc(u32 count, u32 total)
498f2b3b2d4SRichard Alpe {
499f2b3b2d4SRichard Alpe 	return (count * 100 + (total / 2)) / total;
500f2b3b2d4SRichard Alpe }
501f2b3b2d4SRichard Alpe 
__fill_bc_link_stat(struct tipc_nl_compat_msg * msg,struct nlattr * prop[],struct nlattr * stats[])502f2b3b2d4SRichard Alpe static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
503f2b3b2d4SRichard Alpe 				struct nlattr *prop[], struct nlattr *stats[])
504f2b3b2d4SRichard Alpe {
505f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "  Window:%u packets\n",
506f2b3b2d4SRichard Alpe 			 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
507f2b3b2d4SRichard Alpe 
508f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
509f2b3b2d4SRichard Alpe 			 "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
510f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
511f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
512f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
513f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
514f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
515f2b3b2d4SRichard Alpe 
516f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
517f2b3b2d4SRichard Alpe 			 "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
518f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
519f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
520f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
521f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
522f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
523f2b3b2d4SRichard Alpe 
524f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "  RX naks:%u defs:%u dups:%u\n",
525f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
526f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
527f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
528f2b3b2d4SRichard Alpe 
529f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "  TX naks:%u acks:%u dups:%u\n",
530f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
531f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
532f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
533f2b3b2d4SRichard Alpe 
534f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
535f2b3b2d4SRichard Alpe 			 "  Congestion link:%u  Send queue max:%u avg:%u",
536f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
537f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
538f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
539f2b3b2d4SRichard Alpe }
540f2b3b2d4SRichard Alpe 
tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)541f2b3b2d4SRichard Alpe static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
542f2b3b2d4SRichard Alpe 					 struct nlattr **attrs)
543f2b3b2d4SRichard Alpe {
544f2b3b2d4SRichard Alpe 	char *name;
545f2b3b2d4SRichard Alpe 	struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
546f2b3b2d4SRichard Alpe 	struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
547f2b3b2d4SRichard Alpe 	struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
548297f7d2cSBaozeng Ding 	int err;
5490762216cSYing Xue 	int len;
550f2b3b2d4SRichard Alpe 
551297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_LINK])
552297f7d2cSBaozeng Ding 		return -EINVAL;
553f2b3b2d4SRichard Alpe 
5548cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
5558cb08174SJohannes Berg 					  attrs[TIPC_NLA_LINK], NULL, NULL);
556297f7d2cSBaozeng Ding 	if (err)
557297f7d2cSBaozeng Ding 		return err;
558f2b3b2d4SRichard Alpe 
559297f7d2cSBaozeng Ding 	if (!link[TIPC_NLA_LINK_PROP])
560297f7d2cSBaozeng Ding 		return -EINVAL;
561297f7d2cSBaozeng Ding 
5628cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
5638cb08174SJohannes Berg 					  link[TIPC_NLA_LINK_PROP], NULL,
5648cb08174SJohannes Berg 					  NULL);
565297f7d2cSBaozeng Ding 	if (err)
566297f7d2cSBaozeng Ding 		return err;
567297f7d2cSBaozeng Ding 
568297f7d2cSBaozeng Ding 	if (!link[TIPC_NLA_LINK_STATS])
569297f7d2cSBaozeng Ding 		return -EINVAL;
570297f7d2cSBaozeng Ding 
5718cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
5728cb08174SJohannes Berg 					  link[TIPC_NLA_LINK_STATS], NULL,
5738cb08174SJohannes Berg 					  NULL);
574297f7d2cSBaozeng Ding 	if (err)
575297f7d2cSBaozeng Ding 		return err;
576f2b3b2d4SRichard Alpe 
577f2b3b2d4SRichard Alpe 	name = (char *)TLV_DATA(msg->req);
5780762216cSYing Xue 
5794f07b80cSXin Long 	len = TLV_GET_DATA_LEN(msg->req);
5804f07b80cSXin Long 	if (len <= 0)
5814f07b80cSXin Long 		return -EINVAL;
5824f07b80cSXin Long 
583fd567ac2SJohn Rutherford 	len = min_t(int, len, TIPC_MAX_LINK_NAME);
584f1db99c0SAndy Shevchenko 	if (!string_is_terminated(name, len))
5850762216cSYing Xue 		return -EINVAL;
5860762216cSYing Xue 
587f2b3b2d4SRichard Alpe 	if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
588f2b3b2d4SRichard Alpe 		return 0;
589f2b3b2d4SRichard Alpe 
590f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
591b3b7e64bSAndrew Lunn 			 (char *)nla_data(link[TIPC_NLA_LINK_NAME]));
592f2b3b2d4SRichard Alpe 
593f2b3b2d4SRichard Alpe 	if (link[TIPC_NLA_LINK_BROADCAST]) {
594f2b3b2d4SRichard Alpe 		__fill_bc_link_stat(msg, prop, stats);
595f2b3b2d4SRichard Alpe 		return 0;
596f2b3b2d4SRichard Alpe 	}
597f2b3b2d4SRichard Alpe 
598f2b3b2d4SRichard Alpe 	if (link[TIPC_NLA_LINK_ACTIVE])
599f2b3b2d4SRichard Alpe 		tipc_tlv_sprintf(msg->rep, "  ACTIVE");
600f2b3b2d4SRichard Alpe 	else if (link[TIPC_NLA_LINK_UP])
601f2b3b2d4SRichard Alpe 		tipc_tlv_sprintf(msg->rep, "  STANDBY");
602f2b3b2d4SRichard Alpe 	else
603f2b3b2d4SRichard Alpe 		tipc_tlv_sprintf(msg->rep, "  DEFUNCT");
604f2b3b2d4SRichard Alpe 
605f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "  MTU:%u  Priority:%u",
606f2b3b2d4SRichard Alpe 			 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
607f2b3b2d4SRichard Alpe 			 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
608f2b3b2d4SRichard Alpe 
609f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "  Tolerance:%u ms  Window:%u packets\n",
610f2b3b2d4SRichard Alpe 			 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
611f2b3b2d4SRichard Alpe 			 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
612f2b3b2d4SRichard Alpe 
613f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
614f2b3b2d4SRichard Alpe 			 "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
615f2b3b2d4SRichard Alpe 			 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
616f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
617f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
618f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
619f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
620f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
621f2b3b2d4SRichard Alpe 
622f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
623f2b3b2d4SRichard Alpe 			 "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
624f2b3b2d4SRichard Alpe 			 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
625f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
626f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
627f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
628f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
629f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
630f2b3b2d4SRichard Alpe 
631f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
632f2b3b2d4SRichard Alpe 			 "  TX profile sample:%u packets  average:%u octets\n",
633f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
634f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
635f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
636f2b3b2d4SRichard Alpe 
637f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
638f2b3b2d4SRichard Alpe 			 "  0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
639f2b3b2d4SRichard Alpe 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
640f2b3b2d4SRichard Alpe 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
641f2b3b2d4SRichard Alpe 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
642f2b3b2d4SRichard Alpe 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
643f2b3b2d4SRichard Alpe 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
644f2b3b2d4SRichard Alpe 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
645f2b3b2d4SRichard Alpe 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
646f2b3b2d4SRichard Alpe 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
647f2b3b2d4SRichard Alpe 
648f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
649f2b3b2d4SRichard Alpe 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
650f2b3b2d4SRichard Alpe 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
651f2b3b2d4SRichard Alpe 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
652f2b3b2d4SRichard Alpe 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
653f2b3b2d4SRichard Alpe 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
654f2b3b2d4SRichard Alpe 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
655f2b3b2d4SRichard Alpe 
656f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
657f2b3b2d4SRichard Alpe 			 "  RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
658f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
659f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
660f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
661f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
662f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
663f2b3b2d4SRichard Alpe 
664f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
665f2b3b2d4SRichard Alpe 			 "  TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
666f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
667f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
668f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
669f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
670f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
671f2b3b2d4SRichard Alpe 
672f2b3b2d4SRichard Alpe 	tipc_tlv_sprintf(msg->rep,
673f2b3b2d4SRichard Alpe 			 "  Congestion link:%u  Send queue max:%u avg:%u",
674f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
675f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
676f2b3b2d4SRichard Alpe 			 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
677f2b3b2d4SRichard Alpe 
678f2b3b2d4SRichard Alpe 	return 0;
679f2b3b2d4SRichard Alpe }
680f2b3b2d4SRichard Alpe 
tipc_nl_compat_link_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)681357ebdbfSRichard Alpe static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
682357ebdbfSRichard Alpe 				    struct nlattr **attrs)
683357ebdbfSRichard Alpe {
684357ebdbfSRichard Alpe 	struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
685357ebdbfSRichard Alpe 	struct tipc_link_info link_info;
686297f7d2cSBaozeng Ding 	int err;
687357ebdbfSRichard Alpe 
688297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_LINK])
689297f7d2cSBaozeng Ding 		return -EINVAL;
690297f7d2cSBaozeng Ding 
6918cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
6928cb08174SJohannes Berg 					  attrs[TIPC_NLA_LINK], NULL, NULL);
693297f7d2cSBaozeng Ding 	if (err)
694297f7d2cSBaozeng Ding 		return err;
695357ebdbfSRichard Alpe 
6961980d375SHoang Le 	link_info.dest = htonl(nla_get_flag(link[TIPC_NLA_LINK_DEST]));
697357ebdbfSRichard Alpe 	link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
698872f6903SFrancis Laniel 	nla_strscpy(link_info.str, link[TIPC_NLA_LINK_NAME],
6995d2be142SKangjie Lu 		    TIPC_MAX_LINK_NAME);
700357ebdbfSRichard Alpe 
701357ebdbfSRichard Alpe 	return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
702357ebdbfSRichard Alpe 			    &link_info, sizeof(link_info));
703357ebdbfSRichard Alpe }
704357ebdbfSRichard Alpe 
__tipc_add_link_prop(struct sk_buff * skb,struct tipc_nl_compat_msg * msg,struct tipc_link_config * lc)705c3d6fb85SRichard Alpe static int __tipc_add_link_prop(struct sk_buff *skb,
706c3d6fb85SRichard Alpe 				struct tipc_nl_compat_msg *msg,
707c3d6fb85SRichard Alpe 				struct tipc_link_config *lc)
708c3d6fb85SRichard Alpe {
709c3d6fb85SRichard Alpe 	switch (msg->cmd) {
710c3d6fb85SRichard Alpe 	case TIPC_CMD_SET_LINK_PRI:
711c3d6fb85SRichard Alpe 		return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
712c3d6fb85SRichard Alpe 	case TIPC_CMD_SET_LINK_TOL:
713c3d6fb85SRichard Alpe 		return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
714c3d6fb85SRichard Alpe 	case TIPC_CMD_SET_LINK_WINDOW:
715c3d6fb85SRichard Alpe 		return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
716c3d6fb85SRichard Alpe 	}
717c3d6fb85SRichard Alpe 
718c3d6fb85SRichard Alpe 	return -EINVAL;
719c3d6fb85SRichard Alpe }
720c3d6fb85SRichard Alpe 
tipc_nl_compat_media_set(struct sk_buff * skb,struct tipc_nl_compat_msg * msg)721c3d6fb85SRichard Alpe static int tipc_nl_compat_media_set(struct sk_buff *skb,
72237e2d484SRichard Alpe 				    struct tipc_nl_compat_msg *msg)
72337e2d484SRichard Alpe {
72437e2d484SRichard Alpe 	struct nlattr *prop;
725c3d6fb85SRichard Alpe 	struct nlattr *media;
726c3d6fb85SRichard Alpe 	struct tipc_link_config *lc;
727c3d6fb85SRichard Alpe 
728c3d6fb85SRichard Alpe 	lc = (struct tipc_link_config *)TLV_DATA(msg->req);
729c3d6fb85SRichard Alpe 
730ae0be8deSMichal Kubecek 	media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
731c3d6fb85SRichard Alpe 	if (!media)
732c3d6fb85SRichard Alpe 		return -EMSGSIZE;
733c3d6fb85SRichard Alpe 
734c3d6fb85SRichard Alpe 	if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
735c3d6fb85SRichard Alpe 		return -EMSGSIZE;
736c3d6fb85SRichard Alpe 
737ae0be8deSMichal Kubecek 	prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
738c3d6fb85SRichard Alpe 	if (!prop)
739c3d6fb85SRichard Alpe 		return -EMSGSIZE;
740c3d6fb85SRichard Alpe 
741c3d6fb85SRichard Alpe 	__tipc_add_link_prop(skb, msg, lc);
742c3d6fb85SRichard Alpe 	nla_nest_end(skb, prop);
743c3d6fb85SRichard Alpe 	nla_nest_end(skb, media);
744c3d6fb85SRichard Alpe 
745c3d6fb85SRichard Alpe 	return 0;
746c3d6fb85SRichard Alpe }
747c3d6fb85SRichard Alpe 
tipc_nl_compat_bearer_set(struct sk_buff * skb,struct tipc_nl_compat_msg * msg)748c3d6fb85SRichard Alpe static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
749c3d6fb85SRichard Alpe 				     struct tipc_nl_compat_msg *msg)
750c3d6fb85SRichard Alpe {
751c3d6fb85SRichard Alpe 	struct nlattr *prop;
752c3d6fb85SRichard Alpe 	struct nlattr *bearer;
753c3d6fb85SRichard Alpe 	struct tipc_link_config *lc;
754c3d6fb85SRichard Alpe 
755c3d6fb85SRichard Alpe 	lc = (struct tipc_link_config *)TLV_DATA(msg->req);
756c3d6fb85SRichard Alpe 
757ae0be8deSMichal Kubecek 	bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
758c3d6fb85SRichard Alpe 	if (!bearer)
759c3d6fb85SRichard Alpe 		return -EMSGSIZE;
760c3d6fb85SRichard Alpe 
761c3d6fb85SRichard Alpe 	if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
762c3d6fb85SRichard Alpe 		return -EMSGSIZE;
763c3d6fb85SRichard Alpe 
764ae0be8deSMichal Kubecek 	prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
765c3d6fb85SRichard Alpe 	if (!prop)
766c3d6fb85SRichard Alpe 		return -EMSGSIZE;
767c3d6fb85SRichard Alpe 
768c3d6fb85SRichard Alpe 	__tipc_add_link_prop(skb, msg, lc);
769c3d6fb85SRichard Alpe 	nla_nest_end(skb, prop);
770c3d6fb85SRichard Alpe 	nla_nest_end(skb, bearer);
771c3d6fb85SRichard Alpe 
772c3d6fb85SRichard Alpe 	return 0;
773c3d6fb85SRichard Alpe }
774c3d6fb85SRichard Alpe 
__tipc_nl_compat_link_set(struct sk_buff * skb,struct tipc_nl_compat_msg * msg)775c3d6fb85SRichard Alpe static int __tipc_nl_compat_link_set(struct sk_buff *skb,
776c3d6fb85SRichard Alpe 				     struct tipc_nl_compat_msg *msg)
777c3d6fb85SRichard Alpe {
778c3d6fb85SRichard Alpe 	struct nlattr *prop;
779c3d6fb85SRichard Alpe 	struct nlattr *link;
78037e2d484SRichard Alpe 	struct tipc_link_config *lc;
78137e2d484SRichard Alpe 
78237e2d484SRichard Alpe 	lc = (struct tipc_link_config *)TLV_DATA(msg->req);
78337e2d484SRichard Alpe 
784ae0be8deSMichal Kubecek 	link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
78537e2d484SRichard Alpe 	if (!link)
78637e2d484SRichard Alpe 		return -EMSGSIZE;
78737e2d484SRichard Alpe 
78837e2d484SRichard Alpe 	if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
78937e2d484SRichard Alpe 		return -EMSGSIZE;
79037e2d484SRichard Alpe 
791ae0be8deSMichal Kubecek 	prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
79237e2d484SRichard Alpe 	if (!prop)
79337e2d484SRichard Alpe 		return -EMSGSIZE;
79437e2d484SRichard Alpe 
795c3d6fb85SRichard Alpe 	__tipc_add_link_prop(skb, msg, lc);
79637e2d484SRichard Alpe 	nla_nest_end(skb, prop);
79737e2d484SRichard Alpe 	nla_nest_end(skb, link);
79837e2d484SRichard Alpe 
79937e2d484SRichard Alpe 	return 0;
80037e2d484SRichard Alpe }
80137e2d484SRichard Alpe 
tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)802c3d6fb85SRichard Alpe static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
803c3d6fb85SRichard Alpe 				   struct sk_buff *skb,
804c3d6fb85SRichard Alpe 				   struct tipc_nl_compat_msg *msg)
805c3d6fb85SRichard Alpe {
806c3d6fb85SRichard Alpe 	struct tipc_link_config *lc;
807c3d6fb85SRichard Alpe 	struct tipc_bearer *bearer;
808c3d6fb85SRichard Alpe 	struct tipc_media *media;
809edf5ff04SYing Xue 	int len;
810c3d6fb85SRichard Alpe 
811c3d6fb85SRichard Alpe 	lc = (struct tipc_link_config *)TLV_DATA(msg->req);
812c3d6fb85SRichard Alpe 
8138c63bf9aSXin Long 	len = TLV_GET_DATA_LEN(msg->req);
8148c63bf9aSXin Long 	len -= offsetof(struct tipc_link_config, name);
8158c63bf9aSXin Long 	if (len <= 0)
8168c63bf9aSXin Long 		return -EINVAL;
8178c63bf9aSXin Long 
8188c63bf9aSXin Long 	len = min_t(int, len, TIPC_MAX_LINK_NAME);
819f1db99c0SAndy Shevchenko 	if (!string_is_terminated(lc->name, len))
820edf5ff04SYing Xue 		return -EINVAL;
821edf5ff04SYing Xue 
822c3d6fb85SRichard Alpe 	media = tipc_media_find(lc->name);
823c3d6fb85SRichard Alpe 	if (media) {
824ed4ffdfeSYing Xue 		cmd->doit = &__tipc_nl_media_set;
825c3d6fb85SRichard Alpe 		return tipc_nl_compat_media_set(skb, msg);
826c3d6fb85SRichard Alpe 	}
827c3d6fb85SRichard Alpe 
828c3d6fb85SRichard Alpe 	bearer = tipc_bearer_find(msg->net, lc->name);
829c3d6fb85SRichard Alpe 	if (bearer) {
830ed4ffdfeSYing Xue 		cmd->doit = &__tipc_nl_bearer_set;
831c3d6fb85SRichard Alpe 		return tipc_nl_compat_bearer_set(skb, msg);
832c3d6fb85SRichard Alpe 	}
833c3d6fb85SRichard Alpe 
834c3d6fb85SRichard Alpe 	return __tipc_nl_compat_link_set(skb, msg);
835c3d6fb85SRichard Alpe }
836c3d6fb85SRichard Alpe 
tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)837c3d6fb85SRichard Alpe static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
838c3d6fb85SRichard Alpe 					   struct sk_buff *skb,
8391817877bSRichard Alpe 					   struct tipc_nl_compat_msg *msg)
8401817877bSRichard Alpe {
8411817877bSRichard Alpe 	char *name;
8421817877bSRichard Alpe 	struct nlattr *link;
8438b66fee7SYing Xue 	int len;
8441817877bSRichard Alpe 
8451817877bSRichard Alpe 	name = (char *)TLV_DATA(msg->req);
8461817877bSRichard Alpe 
847ae0be8deSMichal Kubecek 	link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
8481817877bSRichard Alpe 	if (!link)
8491817877bSRichard Alpe 		return -EMSGSIZE;
8501817877bSRichard Alpe 
8514f07b80cSXin Long 	len = TLV_GET_DATA_LEN(msg->req);
8524f07b80cSXin Long 	if (len <= 0)
8534f07b80cSXin Long 		return -EINVAL;
8544f07b80cSXin Long 
855fd567ac2SJohn Rutherford 	len = min_t(int, len, TIPC_MAX_LINK_NAME);
856f1db99c0SAndy Shevchenko 	if (!string_is_terminated(name, len))
8578b66fee7SYing Xue 		return -EINVAL;
8588b66fee7SYing Xue 
8591817877bSRichard Alpe 	if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
8601817877bSRichard Alpe 		return -EMSGSIZE;
8611817877bSRichard Alpe 
8621817877bSRichard Alpe 	nla_nest_end(skb, link);
8631817877bSRichard Alpe 
8641817877bSRichard Alpe 	return 0;
8651817877bSRichard Alpe }
8661817877bSRichard Alpe 
tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg * msg)86744a8ae94SRichard Alpe static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
86844a8ae94SRichard Alpe {
86944a8ae94SRichard Alpe 	int i;
87044a8ae94SRichard Alpe 	u32 depth;
87144a8ae94SRichard Alpe 	struct tipc_name_table_query *ntq;
87244a8ae94SRichard Alpe 	static const char * const header[] = {
87344a8ae94SRichard Alpe 		"Type       ",
87444a8ae94SRichard Alpe 		"Lower      Upper      ",
87544a8ae94SRichard Alpe 		"Port Identity              ",
87644a8ae94SRichard Alpe 		"Publication Scope"
87744a8ae94SRichard Alpe 	};
87844a8ae94SRichard Alpe 
87944a8ae94SRichard Alpe 	ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
8801c075b19SXin Long 	if (TLV_GET_DATA_LEN(msg->req) < (int)sizeof(struct tipc_name_table_query))
881974cb0e3SYing Xue 		return -EINVAL;
88244a8ae94SRichard Alpe 
88344a8ae94SRichard Alpe 	depth = ntohl(ntq->depth);
88444a8ae94SRichard Alpe 
88544a8ae94SRichard Alpe 	if (depth > 4)
88644a8ae94SRichard Alpe 		depth = 4;
88744a8ae94SRichard Alpe 	for (i = 0; i < depth; i++)
88844a8ae94SRichard Alpe 		tipc_tlv_sprintf(msg->rep, header[i]);
88944a8ae94SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "\n");
89044a8ae94SRichard Alpe 
89144a8ae94SRichard Alpe 	return 0;
89244a8ae94SRichard Alpe }
89344a8ae94SRichard Alpe 
tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)89444a8ae94SRichard Alpe static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
89544a8ae94SRichard Alpe 					  struct nlattr **attrs)
89644a8ae94SRichard Alpe {
89744a8ae94SRichard Alpe 	char port_str[27];
89844a8ae94SRichard Alpe 	struct tipc_name_table_query *ntq;
89944a8ae94SRichard Alpe 	struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
90044a8ae94SRichard Alpe 	struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
90144a8ae94SRichard Alpe 	u32 node, depth, type, lowbound, upbound;
90244a8ae94SRichard Alpe 	static const char * const scope_str[] = {"", " zone", " cluster",
90344a8ae94SRichard Alpe 						 " node"};
904297f7d2cSBaozeng Ding 	int err;
90544a8ae94SRichard Alpe 
906297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_NAME_TABLE])
907297f7d2cSBaozeng Ding 		return -EINVAL;
908297f7d2cSBaozeng Ding 
9098cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
9108cb08174SJohannes Berg 					  attrs[TIPC_NLA_NAME_TABLE], NULL,
9118cb08174SJohannes Berg 					  NULL);
912297f7d2cSBaozeng Ding 	if (err)
913297f7d2cSBaozeng Ding 		return err;
91444a8ae94SRichard Alpe 
915297f7d2cSBaozeng Ding 	if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
916297f7d2cSBaozeng Ding 		return -EINVAL;
917297f7d2cSBaozeng Ding 
9188cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
9198cb08174SJohannes Berg 					  nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
9208cb08174SJohannes Berg 					  NULL);
921297f7d2cSBaozeng Ding 	if (err)
922297f7d2cSBaozeng Ding 		return err;
92344a8ae94SRichard Alpe 
92444a8ae94SRichard Alpe 	ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
92544a8ae94SRichard Alpe 
92644a8ae94SRichard Alpe 	depth = ntohl(ntq->depth);
92744a8ae94SRichard Alpe 	type = ntohl(ntq->type);
92844a8ae94SRichard Alpe 	lowbound = ntohl(ntq->lowbound);
92944a8ae94SRichard Alpe 	upbound = ntohl(ntq->upbound);
93044a8ae94SRichard Alpe 
93144a8ae94SRichard Alpe 	if (!(depth & TIPC_NTQ_ALLTYPES) &&
93244a8ae94SRichard Alpe 	    (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
93344a8ae94SRichard Alpe 		return 0;
93444a8ae94SRichard Alpe 	if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
93544a8ae94SRichard Alpe 		return 0;
93644a8ae94SRichard Alpe 	if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
93744a8ae94SRichard Alpe 		return 0;
93844a8ae94SRichard Alpe 
93944a8ae94SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "%-10u ",
94044a8ae94SRichard Alpe 			 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
94144a8ae94SRichard Alpe 
94244a8ae94SRichard Alpe 	if (depth == 1)
94344a8ae94SRichard Alpe 		goto out;
94444a8ae94SRichard Alpe 
94544a8ae94SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
94644a8ae94SRichard Alpe 			 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
94744a8ae94SRichard Alpe 			 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
94844a8ae94SRichard Alpe 
94944a8ae94SRichard Alpe 	if (depth == 2)
95044a8ae94SRichard Alpe 		goto out;
95144a8ae94SRichard Alpe 
95244a8ae94SRichard Alpe 	node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
95344a8ae94SRichard Alpe 	sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
95444a8ae94SRichard Alpe 		tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
95544a8ae94SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
95644a8ae94SRichard Alpe 
95744a8ae94SRichard Alpe 	if (depth == 3)
95844a8ae94SRichard Alpe 		goto out;
95944a8ae94SRichard Alpe 
96044a8ae94SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "%-10u %s",
96103aaaa9bSRichard Alpe 			 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
96244a8ae94SRichard Alpe 			 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
96344a8ae94SRichard Alpe out:
96444a8ae94SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "\n");
96544a8ae94SRichard Alpe 
96644a8ae94SRichard Alpe 	return 0;
96744a8ae94SRichard Alpe }
96844a8ae94SRichard Alpe 
__tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)969487d2a3aSRichard Alpe static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
970487d2a3aSRichard Alpe 				      struct nlattr **attrs)
971487d2a3aSRichard Alpe {
972487d2a3aSRichard Alpe 	u32 type, lower, upper;
973487d2a3aSRichard Alpe 	struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
974297f7d2cSBaozeng Ding 	int err;
975487d2a3aSRichard Alpe 
976297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_PUBL])
977297f7d2cSBaozeng Ding 		return -EINVAL;
978297f7d2cSBaozeng Ding 
9798cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
9808cb08174SJohannes Berg 					  attrs[TIPC_NLA_PUBL], NULL, NULL);
981297f7d2cSBaozeng Ding 	if (err)
982297f7d2cSBaozeng Ding 		return err;
983487d2a3aSRichard Alpe 
984487d2a3aSRichard Alpe 	type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
985487d2a3aSRichard Alpe 	lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
986487d2a3aSRichard Alpe 	upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
987487d2a3aSRichard Alpe 
988487d2a3aSRichard Alpe 	if (lower == upper)
989487d2a3aSRichard Alpe 		tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
990487d2a3aSRichard Alpe 	else
991487d2a3aSRichard Alpe 		tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
992487d2a3aSRichard Alpe 
993487d2a3aSRichard Alpe 	return 0;
994487d2a3aSRichard Alpe }
995487d2a3aSRichard Alpe 
tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg * msg,u32 sock)996487d2a3aSRichard Alpe static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
997487d2a3aSRichard Alpe {
998487d2a3aSRichard Alpe 	int err;
999487d2a3aSRichard Alpe 	void *hdr;
1000487d2a3aSRichard Alpe 	struct nlattr *nest;
1001487d2a3aSRichard Alpe 	struct sk_buff *args;
1002487d2a3aSRichard Alpe 	struct tipc_nl_compat_cmd_dump dump;
1003487d2a3aSRichard Alpe 
1004487d2a3aSRichard Alpe 	args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1005487d2a3aSRichard Alpe 	if (!args)
1006487d2a3aSRichard Alpe 		return -ENOMEM;
1007487d2a3aSRichard Alpe 
1008487d2a3aSRichard Alpe 	hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
1009487d2a3aSRichard Alpe 			  TIPC_NL_PUBL_GET);
1010f87d8ad9SGustavo A. R. Silva 	if (!hdr) {
1011f87d8ad9SGustavo A. R. Silva 		kfree_skb(args);
101246273cf7SKangjie Lu 		return -EMSGSIZE;
1013f87d8ad9SGustavo A. R. Silva 	}
1014487d2a3aSRichard Alpe 
1015ae0be8deSMichal Kubecek 	nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
1016487d2a3aSRichard Alpe 	if (!nest) {
1017487d2a3aSRichard Alpe 		kfree_skb(args);
1018487d2a3aSRichard Alpe 		return -EMSGSIZE;
1019487d2a3aSRichard Alpe 	}
1020487d2a3aSRichard Alpe 
1021487d2a3aSRichard Alpe 	if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
1022487d2a3aSRichard Alpe 		kfree_skb(args);
1023487d2a3aSRichard Alpe 		return -EMSGSIZE;
1024487d2a3aSRichard Alpe 	}
1025487d2a3aSRichard Alpe 
1026487d2a3aSRichard Alpe 	nla_nest_end(args, nest);
1027487d2a3aSRichard Alpe 	genlmsg_end(args, hdr);
1028487d2a3aSRichard Alpe 
1029487d2a3aSRichard Alpe 	dump.dumpit = tipc_nl_publ_dump;
1030487d2a3aSRichard Alpe 	dump.format = __tipc_nl_compat_publ_dump;
1031487d2a3aSRichard Alpe 
1032487d2a3aSRichard Alpe 	err = __tipc_nl_compat_dumpit(&dump, msg, args);
1033487d2a3aSRichard Alpe 
1034487d2a3aSRichard Alpe 	kfree_skb(args);
1035487d2a3aSRichard Alpe 
1036487d2a3aSRichard Alpe 	return err;
1037487d2a3aSRichard Alpe }
1038487d2a3aSRichard Alpe 
tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)1039487d2a3aSRichard Alpe static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1040487d2a3aSRichard Alpe 				  struct nlattr **attrs)
1041487d2a3aSRichard Alpe {
1042487d2a3aSRichard Alpe 	int err;
1043487d2a3aSRichard Alpe 	u32 sock_ref;
1044487d2a3aSRichard Alpe 	struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1045487d2a3aSRichard Alpe 
1046297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_SOCK])
1047297f7d2cSBaozeng Ding 		return -EINVAL;
1048297f7d2cSBaozeng Ding 
10498cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
10508cb08174SJohannes Berg 					  attrs[TIPC_NLA_SOCK], NULL, NULL);
1051297f7d2cSBaozeng Ding 	if (err)
1052297f7d2cSBaozeng Ding 		return err;
1053487d2a3aSRichard Alpe 
1054487d2a3aSRichard Alpe 	sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1055487d2a3aSRichard Alpe 	tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1056487d2a3aSRichard Alpe 
1057487d2a3aSRichard Alpe 	if (sock[TIPC_NLA_SOCK_CON]) {
1058487d2a3aSRichard Alpe 		u32 node;
1059487d2a3aSRichard Alpe 		struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1060487d2a3aSRichard Alpe 
10618cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
10628cb08174SJohannes Berg 						  sock[TIPC_NLA_SOCK_CON],
10638cb08174SJohannes Berg 						  NULL, NULL);
1064487d2a3aSRichard Alpe 
106589dfd008SAditya Pakki 		if (err)
106689dfd008SAditya Pakki 			return err;
106789dfd008SAditya Pakki 
1068487d2a3aSRichard Alpe 		node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1069487d2a3aSRichard Alpe 		tipc_tlv_sprintf(msg->rep, "  connected to <%u.%u.%u:%u>",
1070487d2a3aSRichard Alpe 				 tipc_zone(node),
1071487d2a3aSRichard Alpe 				 tipc_cluster(node),
1072487d2a3aSRichard Alpe 				 tipc_node(node),
1073487d2a3aSRichard Alpe 				 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1074487d2a3aSRichard Alpe 
1075487d2a3aSRichard Alpe 		if (con[TIPC_NLA_CON_FLAG])
1076487d2a3aSRichard Alpe 			tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1077487d2a3aSRichard Alpe 					 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1078487d2a3aSRichard Alpe 					 nla_get_u32(con[TIPC_NLA_CON_INST]));
1079487d2a3aSRichard Alpe 		else
1080487d2a3aSRichard Alpe 			tipc_tlv_sprintf(msg->rep, "\n");
1081487d2a3aSRichard Alpe 	} else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1082487d2a3aSRichard Alpe 		tipc_tlv_sprintf(msg->rep, " bound to");
1083487d2a3aSRichard Alpe 
1084487d2a3aSRichard Alpe 		err = tipc_nl_compat_publ_dump(msg, sock_ref);
1085487d2a3aSRichard Alpe 		if (err)
1086487d2a3aSRichard Alpe 			return err;
1087487d2a3aSRichard Alpe 	}
1088487d2a3aSRichard Alpe 	tipc_tlv_sprintf(msg->rep, "\n");
1089487d2a3aSRichard Alpe 
1090487d2a3aSRichard Alpe 	return 0;
1091487d2a3aSRichard Alpe }
1092487d2a3aSRichard Alpe 
tipc_nl_compat_media_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)10935bfc335aSRichard Alpe static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
10945bfc335aSRichard Alpe 				     struct nlattr **attrs)
10955bfc335aSRichard Alpe {
10965bfc335aSRichard Alpe 	struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1097297f7d2cSBaozeng Ding 	int err;
10985bfc335aSRichard Alpe 
1099297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_MEDIA])
1100297f7d2cSBaozeng Ding 		return -EINVAL;
1101297f7d2cSBaozeng Ding 
11028cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1103fceb6435SJohannes Berg 					  attrs[TIPC_NLA_MEDIA], NULL, NULL);
1104297f7d2cSBaozeng Ding 	if (err)
1105297f7d2cSBaozeng Ding 		return err;
11065bfc335aSRichard Alpe 
11075bfc335aSRichard Alpe 	return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
11085bfc335aSRichard Alpe 			    nla_data(media[TIPC_NLA_MEDIA_NAME]),
11095bfc335aSRichard Alpe 			    nla_len(media[TIPC_NLA_MEDIA_NAME]));
11105bfc335aSRichard Alpe }
11115bfc335aSRichard Alpe 
tipc_nl_compat_node_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)11124b28cb58SRichard Alpe static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
11134b28cb58SRichard Alpe 				    struct nlattr **attrs)
11144b28cb58SRichard Alpe {
11154b28cb58SRichard Alpe 	struct tipc_node_info node_info;
11164b28cb58SRichard Alpe 	struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1117297f7d2cSBaozeng Ding 	int err;
11184b28cb58SRichard Alpe 
1119297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_NODE])
1120297f7d2cSBaozeng Ding 		return -EINVAL;
1121297f7d2cSBaozeng Ding 
11228cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
11238cb08174SJohannes Berg 					  attrs[TIPC_NLA_NODE], NULL, NULL);
1124297f7d2cSBaozeng Ding 	if (err)
1125297f7d2cSBaozeng Ding 		return err;
11264b28cb58SRichard Alpe 
11274b28cb58SRichard Alpe 	node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
11284b28cb58SRichard Alpe 	node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
11294b28cb58SRichard Alpe 
11304b28cb58SRichard Alpe 	return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
11314b28cb58SRichard Alpe 			    sizeof(node_info));
11324b28cb58SRichard Alpe }
11334b28cb58SRichard Alpe 
tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)1134c3d6fb85SRichard Alpe static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1135c3d6fb85SRichard Alpe 				  struct sk_buff *skb,
1136d7cc75d3SRichard Alpe 				  struct tipc_nl_compat_msg *msg)
1137d7cc75d3SRichard Alpe {
1138d7cc75d3SRichard Alpe 	u32 val;
1139d7cc75d3SRichard Alpe 	struct nlattr *net;
1140d7cc75d3SRichard Alpe 
1141d7cc75d3SRichard Alpe 	val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1142d7cc75d3SRichard Alpe 
1143ae0be8deSMichal Kubecek 	net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
1144d7cc75d3SRichard Alpe 	if (!net)
1145d7cc75d3SRichard Alpe 		return -EMSGSIZE;
1146d7cc75d3SRichard Alpe 
1147964f9501SRichard Alpe 	if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1148d7cc75d3SRichard Alpe 		if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1149d7cc75d3SRichard Alpe 			return -EMSGSIZE;
1150964f9501SRichard Alpe 	} else if (msg->cmd == TIPC_CMD_SET_NETID) {
1151964f9501SRichard Alpe 		if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1152964f9501SRichard Alpe 			return -EMSGSIZE;
1153964f9501SRichard Alpe 	}
1154d7cc75d3SRichard Alpe 	nla_nest_end(skb, net);
1155d7cc75d3SRichard Alpe 
1156d7cc75d3SRichard Alpe 	return 0;
1157d7cc75d3SRichard Alpe }
1158d7cc75d3SRichard Alpe 
tipc_nl_compat_net_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)11593c26181cSRichard Alpe static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
11603c26181cSRichard Alpe 				   struct nlattr **attrs)
11613c26181cSRichard Alpe {
11623c26181cSRichard Alpe 	__be32 id;
11633c26181cSRichard Alpe 	struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1164297f7d2cSBaozeng Ding 	int err;
11653c26181cSRichard Alpe 
1166297f7d2cSBaozeng Ding 	if (!attrs[TIPC_NLA_NET])
1167297f7d2cSBaozeng Ding 		return -EINVAL;
1168297f7d2cSBaozeng Ding 
11698cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
11708cb08174SJohannes Berg 					  attrs[TIPC_NLA_NET], NULL, NULL);
1171297f7d2cSBaozeng Ding 	if (err)
1172297f7d2cSBaozeng Ding 		return err;
1173297f7d2cSBaozeng Ding 
11743c26181cSRichard Alpe 	id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
11753c26181cSRichard Alpe 
11763c26181cSRichard Alpe 	return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
11773c26181cSRichard Alpe }
11783c26181cSRichard Alpe 
tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg * msg)11795a81a637SRichard Alpe static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
11805a81a637SRichard Alpe {
11815a81a637SRichard Alpe 	msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
11825a81a637SRichard Alpe 	if (!msg->rep)
11835a81a637SRichard Alpe 		return -ENOMEM;
11845a81a637SRichard Alpe 
11855a81a637SRichard Alpe 	tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
11865a81a637SRichard Alpe 	tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
11875a81a637SRichard Alpe 
11885a81a637SRichard Alpe 	return 0;
11895a81a637SRichard Alpe }
11905a81a637SRichard Alpe 
tipc_nl_compat_handle(struct tipc_nl_compat_msg * msg)1191d0796d1eSRichard Alpe static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1192d0796d1eSRichard Alpe {
1193d0796d1eSRichard Alpe 	struct tipc_nl_compat_cmd_dump dump;
11949ab15465SRichard Alpe 	struct tipc_nl_compat_cmd_doit doit;
1195d0796d1eSRichard Alpe 
1196d0796d1eSRichard Alpe 	memset(&dump, 0, sizeof(dump));
11979ab15465SRichard Alpe 	memset(&doit, 0, sizeof(doit));
1198d0796d1eSRichard Alpe 
1199d0796d1eSRichard Alpe 	switch (msg->cmd) {
120022ae7cffSRichard Alpe 	case TIPC_CMD_NOOP:
120122ae7cffSRichard Alpe 		msg->rep = tipc_tlv_alloc(0);
120222ae7cffSRichard Alpe 		if (!msg->rep)
120322ae7cffSRichard Alpe 			return -ENOMEM;
120422ae7cffSRichard Alpe 		return 0;
1205d0796d1eSRichard Alpe 	case TIPC_CMD_GET_BEARER_NAMES:
1206d0796d1eSRichard Alpe 		msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1207d0796d1eSRichard Alpe 		dump.dumpit = tipc_nl_bearer_dump;
1208d0796d1eSRichard Alpe 		dump.format = tipc_nl_compat_bearer_dump;
1209d0796d1eSRichard Alpe 		return tipc_nl_compat_dumpit(&dump, msg);
12109ab15465SRichard Alpe 	case TIPC_CMD_ENABLE_BEARER:
12119ab15465SRichard Alpe 		msg->req_type = TIPC_TLV_BEARER_CONFIG;
1212ed4ffdfeSYing Xue 		doit.doit = __tipc_nl_bearer_enable;
12139ab15465SRichard Alpe 		doit.transcode = tipc_nl_compat_bearer_enable;
12149ab15465SRichard Alpe 		return tipc_nl_compat_doit(&doit, msg);
12159ab15465SRichard Alpe 	case TIPC_CMD_DISABLE_BEARER:
12169ab15465SRichard Alpe 		msg->req_type = TIPC_TLV_BEARER_NAME;
1217ed4ffdfeSYing Xue 		doit.doit = __tipc_nl_bearer_disable;
12189ab15465SRichard Alpe 		doit.transcode = tipc_nl_compat_bearer_disable;
12199ab15465SRichard Alpe 		return tipc_nl_compat_doit(&doit, msg);
1220f2b3b2d4SRichard Alpe 	case TIPC_CMD_SHOW_LINK_STATS:
1221f2b3b2d4SRichard Alpe 		msg->req_type = TIPC_TLV_LINK_NAME;
1222f2b3b2d4SRichard Alpe 		msg->rep_size = ULTRA_STRING_MAX_LEN;
1223f2b3b2d4SRichard Alpe 		msg->rep_type = TIPC_TLV_ULTRA_STRING;
122438206d59SJon Paul Maloy 		dump.dumpit = tipc_nl_node_dump_link;
1225f2b3b2d4SRichard Alpe 		dump.format = tipc_nl_compat_link_stat_dump;
1226f2b3b2d4SRichard Alpe 		return tipc_nl_compat_dumpit(&dump, msg);
1227357ebdbfSRichard Alpe 	case TIPC_CMD_GET_LINKS:
1228357ebdbfSRichard Alpe 		msg->req_type = TIPC_TLV_NET_ADDR;
1229357ebdbfSRichard Alpe 		msg->rep_size = ULTRA_STRING_MAX_LEN;
123038206d59SJon Paul Maloy 		dump.dumpit = tipc_nl_node_dump_link;
1231357ebdbfSRichard Alpe 		dump.format = tipc_nl_compat_link_dump;
1232357ebdbfSRichard Alpe 		return tipc_nl_compat_dumpit(&dump, msg);
123337e2d484SRichard Alpe 	case TIPC_CMD_SET_LINK_TOL:
123437e2d484SRichard Alpe 	case TIPC_CMD_SET_LINK_PRI:
123537e2d484SRichard Alpe 	case TIPC_CMD_SET_LINK_WINDOW:
123637e2d484SRichard Alpe 		msg->req_type =  TIPC_TLV_LINK_CONFIG;
12375be9c086SJon Paul Maloy 		doit.doit = tipc_nl_node_set_link;
123837e2d484SRichard Alpe 		doit.transcode = tipc_nl_compat_link_set;
123937e2d484SRichard Alpe 		return tipc_nl_compat_doit(&doit, msg);
12401817877bSRichard Alpe 	case TIPC_CMD_RESET_LINK_STATS:
12411817877bSRichard Alpe 		msg->req_type = TIPC_TLV_LINK_NAME;
12425be9c086SJon Paul Maloy 		doit.doit = tipc_nl_node_reset_link_stats;
12431817877bSRichard Alpe 		doit.transcode = tipc_nl_compat_link_reset_stats;
12441817877bSRichard Alpe 		return tipc_nl_compat_doit(&doit, msg);
124544a8ae94SRichard Alpe 	case TIPC_CMD_SHOW_NAME_TABLE:
124644a8ae94SRichard Alpe 		msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
124744a8ae94SRichard Alpe 		msg->rep_size = ULTRA_STRING_MAX_LEN;
124844a8ae94SRichard Alpe 		msg->rep_type = TIPC_TLV_ULTRA_STRING;
124944a8ae94SRichard Alpe 		dump.header = tipc_nl_compat_name_table_dump_header;
125044a8ae94SRichard Alpe 		dump.dumpit = tipc_nl_name_table_dump;
125144a8ae94SRichard Alpe 		dump.format = tipc_nl_compat_name_table_dump;
125244a8ae94SRichard Alpe 		return tipc_nl_compat_dumpit(&dump, msg);
1253487d2a3aSRichard Alpe 	case TIPC_CMD_SHOW_PORTS:
1254487d2a3aSRichard Alpe 		msg->rep_size = ULTRA_STRING_MAX_LEN;
1255487d2a3aSRichard Alpe 		msg->rep_type = TIPC_TLV_ULTRA_STRING;
1256487d2a3aSRichard Alpe 		dump.dumpit = tipc_nl_sk_dump;
1257487d2a3aSRichard Alpe 		dump.format = tipc_nl_compat_sk_dump;
1258487d2a3aSRichard Alpe 		return tipc_nl_compat_dumpit(&dump, msg);
12595bfc335aSRichard Alpe 	case TIPC_CMD_GET_MEDIA_NAMES:
12605bfc335aSRichard Alpe 		msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
12615bfc335aSRichard Alpe 		dump.dumpit = tipc_nl_media_dump;
12625bfc335aSRichard Alpe 		dump.format = tipc_nl_compat_media_dump;
12635bfc335aSRichard Alpe 		return tipc_nl_compat_dumpit(&dump, msg);
12644b28cb58SRichard Alpe 	case TIPC_CMD_GET_NODES:
12654b28cb58SRichard Alpe 		msg->rep_size = ULTRA_STRING_MAX_LEN;
12664b28cb58SRichard Alpe 		dump.dumpit = tipc_nl_node_dump;
12674b28cb58SRichard Alpe 		dump.format = tipc_nl_compat_node_dump;
12684b28cb58SRichard Alpe 		return tipc_nl_compat_dumpit(&dump, msg);
1269d7cc75d3SRichard Alpe 	case TIPC_CMD_SET_NODE_ADDR:
1270d7cc75d3SRichard Alpe 		msg->req_type = TIPC_TLV_NET_ADDR;
1271ed4ffdfeSYing Xue 		doit.doit = __tipc_nl_net_set;
1272d7cc75d3SRichard Alpe 		doit.transcode = tipc_nl_compat_net_set;
1273d7cc75d3SRichard Alpe 		return tipc_nl_compat_doit(&doit, msg);
1274964f9501SRichard Alpe 	case TIPC_CMD_SET_NETID:
1275964f9501SRichard Alpe 		msg->req_type = TIPC_TLV_UNSIGNED;
1276ed4ffdfeSYing Xue 		doit.doit = __tipc_nl_net_set;
1277964f9501SRichard Alpe 		doit.transcode = tipc_nl_compat_net_set;
1278964f9501SRichard Alpe 		return tipc_nl_compat_doit(&doit, msg);
12793c26181cSRichard Alpe 	case TIPC_CMD_GET_NETID:
12803c26181cSRichard Alpe 		msg->rep_size = sizeof(u32);
12813c26181cSRichard Alpe 		dump.dumpit = tipc_nl_net_dump;
12823c26181cSRichard Alpe 		dump.format = tipc_nl_compat_net_dump;
12833c26181cSRichard Alpe 		return tipc_nl_compat_dumpit(&dump, msg);
12845a81a637SRichard Alpe 	case TIPC_CMD_SHOW_STATS:
12855a81a637SRichard Alpe 		return tipc_cmd_show_stats_compat(msg);
1286d0796d1eSRichard Alpe 	}
1287d0796d1eSRichard Alpe 
1288d0796d1eSRichard Alpe 	return -EOPNOTSUPP;
1289d0796d1eSRichard Alpe }
1290d0796d1eSRichard Alpe 
tipc_nl_compat_recv(struct sk_buff * skb,struct genl_info * info)1291d0796d1eSRichard Alpe static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1292d0796d1eSRichard Alpe {
1293d0796d1eSRichard Alpe 	int err;
1294d0796d1eSRichard Alpe 	int len;
1295d0796d1eSRichard Alpe 	struct tipc_nl_compat_msg msg;
1296d0796d1eSRichard Alpe 	struct nlmsghdr *req_nlh;
1297d0796d1eSRichard Alpe 	struct nlmsghdr *rep_nlh;
1298bffcc688SJakub Kicinski 	struct tipc_genlmsghdr *req_userhdr = genl_info_userhdr(info);
1299d0796d1eSRichard Alpe 
1300d0796d1eSRichard Alpe 	memset(&msg, 0, sizeof(msg));
1301d0796d1eSRichard Alpe 
1302d0796d1eSRichard Alpe 	req_nlh = (struct nlmsghdr *)skb->data;
1303d0796d1eSRichard Alpe 	msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1304d0796d1eSRichard Alpe 	msg.cmd = req_userhdr->cmd;
1305c3d6fb85SRichard Alpe 	msg.net = genl_info_net(info);
1306619b1745SFlorian Westphal 	msg.dst_sk = skb->sk;
1307d0796d1eSRichard Alpe 
1308d0796d1eSRichard Alpe 	if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1309d0796d1eSRichard Alpe 		msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1310d0796d1eSRichard Alpe 		err = -EACCES;
1311d0796d1eSRichard Alpe 		goto send;
1312d0796d1eSRichard Alpe 	}
1313d0796d1eSRichard Alpe 
13144da5f001STaras Kondratiuk 	msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
13154da5f001STaras Kondratiuk 	if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1316d0796d1eSRichard Alpe 		msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1317d0796d1eSRichard Alpe 		err = -EOPNOTSUPP;
1318d0796d1eSRichard Alpe 		goto send;
1319d0796d1eSRichard Alpe 	}
1320d0796d1eSRichard Alpe 
1321d0796d1eSRichard Alpe 	err = tipc_nl_compat_handle(&msg);
1322b063bc5eSRichard Alpe 	if ((err == -EOPNOTSUPP) || (err == -EPERM))
1323d0796d1eSRichard Alpe 		msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1324d0796d1eSRichard Alpe 	else if (err == -EINVAL)
1325d0796d1eSRichard Alpe 		msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1326d0796d1eSRichard Alpe send:
1327d0796d1eSRichard Alpe 	if (!msg.rep)
1328d0796d1eSRichard Alpe 		return err;
1329d0796d1eSRichard Alpe 
1330d0796d1eSRichard Alpe 	len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1331d0796d1eSRichard Alpe 	skb_push(msg.rep, len);
1332d0796d1eSRichard Alpe 	rep_nlh = nlmsg_hdr(msg.rep);
1333d0796d1eSRichard Alpe 	memcpy(rep_nlh, info->nlhdr, len);
1334d0796d1eSRichard Alpe 	rep_nlh->nlmsg_len = msg.rep->len;
1335c3d6fb85SRichard Alpe 	genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1336d0796d1eSRichard Alpe 
1337d0796d1eSRichard Alpe 	return err;
1338d0796d1eSRichard Alpe }
1339d0796d1eSRichard Alpe 
134066a9b928SJakub Kicinski static const struct genl_small_ops tipc_genl_compat_ops[] = {
1341bfb3e5ddSRichard Alpe 	{
1342bfb3e5ddSRichard Alpe 		.cmd		= TIPC_GENL_CMD,
1343ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
134422ae7cffSRichard Alpe 		.doit		= tipc_nl_compat_recv,
1345bfb3e5ddSRichard Alpe 	},
1346bfb3e5ddSRichard Alpe };
1347bfb3e5ddSRichard Alpe 
134856989f6dSJohannes Berg static struct genl_family tipc_genl_compat_family __ro_after_init = {
1349489111e5SJohannes Berg 	.name		= TIPC_GENL_NAME,
1350489111e5SJohannes Berg 	.version	= TIPC_GENL_VERSION,
1351489111e5SJohannes Berg 	.hdrsize	= TIPC_GENL_HDRLEN,
1352489111e5SJohannes Berg 	.maxattr	= 0,
1353489111e5SJohannes Berg 	.netnsok	= true,
1354489111e5SJohannes Berg 	.module		= THIS_MODULE,
135566a9b928SJakub Kicinski 	.small_ops	= tipc_genl_compat_ops,
135666a9b928SJakub Kicinski 	.n_small_ops	= ARRAY_SIZE(tipc_genl_compat_ops),
13579c5d03d3SJakub Kicinski 	.resv_start_op	= TIPC_GENL_CMD + 1,
1358489111e5SJohannes Berg };
1359489111e5SJohannes Berg 
tipc_netlink_compat_start(void)136056989f6dSJohannes Berg int __init tipc_netlink_compat_start(void)
1361bfb3e5ddSRichard Alpe {
1362bfb3e5ddSRichard Alpe 	int res;
1363bfb3e5ddSRichard Alpe 
1364489111e5SJohannes Berg 	res = genl_register_family(&tipc_genl_compat_family);
1365bfb3e5ddSRichard Alpe 	if (res) {
1366bfb3e5ddSRichard Alpe 		pr_err("Failed to register legacy compat interface\n");
1367bfb3e5ddSRichard Alpe 		return res;
1368bfb3e5ddSRichard Alpe 	}
1369bfb3e5ddSRichard Alpe 
1370bfb3e5ddSRichard Alpe 	return 0;
1371bfb3e5ddSRichard Alpe }
1372bfb3e5ddSRichard Alpe 
tipc_netlink_compat_stop(void)1373bfb3e5ddSRichard Alpe void tipc_netlink_compat_stop(void)
1374bfb3e5ddSRichard Alpe {
1375bfb3e5ddSRichard Alpe 	genl_unregister_family(&tipc_genl_compat_family);
1376bfb3e5ddSRichard Alpe }
1377