• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2014, Ericsson AB
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "core.h"
35 #include "bearer.h"
36 #include "link.h"
37 #include "name_table.h"
38 #include "socket.h"
39 #include "node.h"
40 #include "net.h"
41 #include <net/genetlink.h>
42 #include <linux/tipc_config.h>
43 
44 /* The legacy API had an artificial message length limit called
45  * ULTRA_STRING_MAX_LEN.
46  */
47 #define ULTRA_STRING_MAX_LEN 32768
48 
49 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
50 
51 #define REPLY_TRUNCATED "<truncated>\n"
52 
53 struct tipc_nl_compat_msg {
54 	u16 cmd;
55 	int rep_type;
56 	int rep_size;
57 	int req_type;
58 	int req_size;
59 	struct net *net;
60 	struct sk_buff *rep;
61 	struct tlv_desc *req;
62 	struct sock *dst_sk;
63 };
64 
65 struct tipc_nl_compat_cmd_dump {
66 	int (*header)(struct tipc_nl_compat_msg *);
67 	int (*dumpit)(struct sk_buff *, struct netlink_callback *);
68 	int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
69 };
70 
71 struct tipc_nl_compat_cmd_doit {
72 	int (*doit)(struct sk_buff *skb, struct genl_info *info);
73 	int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
74 			 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
75 };
76 
tipc_skb_tailroom(struct sk_buff * skb)77 static int tipc_skb_tailroom(struct sk_buff *skb)
78 {
79 	int tailroom;
80 	int limit;
81 
82 	tailroom = skb_tailroom(skb);
83 	limit = TIPC_SKB_MAX - skb->len;
84 
85 	if (tailroom < limit)
86 		return tailroom;
87 
88 	return limit;
89 }
90 
TLV_GET_DATA_LEN(struct tlv_desc * tlv)91 static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
92 {
93 	return TLV_GET_LEN(tlv) - TLV_SPACE(0);
94 }
95 
tipc_add_tlv(struct sk_buff * skb,u16 type,void * data,u16 len)96 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
97 {
98 	struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
99 
100 	if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
101 		return -EMSGSIZE;
102 
103 	skb_put(skb, TLV_SPACE(len));
104 	memset(tlv, 0, TLV_SPACE(len));
105 	tlv->tlv_type = htons(type);
106 	tlv->tlv_len = htons(TLV_LENGTH(len));
107 	if (len && data)
108 		memcpy(TLV_DATA(tlv), data, len);
109 
110 	return 0;
111 }
112 
tipc_tlv_init(struct sk_buff * skb,u16 type)113 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
114 {
115 	struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
116 
117 	TLV_SET_LEN(tlv, 0);
118 	TLV_SET_TYPE(tlv, type);
119 	skb_put(skb, sizeof(struct tlv_desc));
120 }
121 
tipc_tlv_sprintf(struct sk_buff * skb,const char * fmt,...)122 static __printf(2, 3) int tipc_tlv_sprintf(struct sk_buff *skb,
123 					   const char *fmt, ...)
124 {
125 	int n;
126 	u16 len;
127 	u32 rem;
128 	char *buf;
129 	struct tlv_desc *tlv;
130 	va_list args;
131 
132 	rem = tipc_skb_tailroom(skb);
133 
134 	tlv = (struct tlv_desc *)skb->data;
135 	len = TLV_GET_LEN(tlv);
136 	buf = TLV_DATA(tlv) + len;
137 
138 	va_start(args, fmt);
139 	n = vscnprintf(buf, rem, fmt, args);
140 	va_end(args);
141 
142 	TLV_SET_LEN(tlv, n + len);
143 	skb_put(skb, n);
144 
145 	return n;
146 }
147 
tipc_tlv_alloc(int size)148 static struct sk_buff *tipc_tlv_alloc(int size)
149 {
150 	int hdr_len;
151 	struct sk_buff *buf;
152 
153 	size = TLV_SPACE(size);
154 	hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
155 
156 	buf = alloc_skb(hdr_len + size, GFP_KERNEL);
157 	if (!buf)
158 		return NULL;
159 
160 	skb_reserve(buf, hdr_len);
161 
162 	return buf;
163 }
164 
tipc_get_err_tlv(char * str)165 static struct sk_buff *tipc_get_err_tlv(char *str)
166 {
167 	int str_len = strlen(str) + 1;
168 	struct sk_buff *buf;
169 
170 	buf = tipc_tlv_alloc(TLV_SPACE(str_len));
171 	if (buf)
172 		tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
173 
174 	return buf;
175 }
176 
string_is_valid(char * s,int len)177 static inline bool string_is_valid(char *s, int len)
178 {
179 	return memchr(s, '\0', len) ? true : false;
180 }
181 
__tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump * cmd,struct tipc_nl_compat_msg * msg,struct sk_buff * arg)182 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
183 				   struct tipc_nl_compat_msg *msg,
184 				   struct sk_buff *arg)
185 {
186 	struct genl_dumpit_info info;
187 	int len = 0;
188 	int err;
189 	struct sk_buff *buf;
190 	struct nlmsghdr *nlmsg;
191 	struct netlink_callback cb;
192 	struct nlattr **attrbuf;
193 
194 	memset(&cb, 0, sizeof(cb));
195 	cb.nlh = (struct nlmsghdr *)arg->data;
196 	cb.skb = arg;
197 	cb.data = &info;
198 
199 	buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
200 	if (!buf)
201 		return -ENOMEM;
202 
203 	buf->sk = msg->dst_sk;
204 	if (__tipc_dump_start(&cb, msg->net)) {
205 		kfree_skb(buf);
206 		return -ENOMEM;
207 	}
208 
209 	attrbuf = kcalloc(tipc_genl_family.maxattr + 1,
210 			  sizeof(struct nlattr *), GFP_KERNEL);
211 	if (!attrbuf) {
212 		err = -ENOMEM;
213 		goto err_out;
214 	}
215 
216 	info.attrs = attrbuf;
217 
218 	if (nlmsg_len(cb.nlh) > 0) {
219 		err = nlmsg_parse_deprecated(cb.nlh, GENL_HDRLEN, attrbuf,
220 					     tipc_genl_family.maxattr,
221 					     tipc_genl_family.policy, NULL);
222 		if (err)
223 			goto err_out;
224 	}
225 	do {
226 		int rem;
227 
228 		len = (*cmd->dumpit)(buf, &cb);
229 
230 		nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
231 			err = nlmsg_parse_deprecated(nlmsg, GENL_HDRLEN,
232 						     attrbuf,
233 						     tipc_genl_family.maxattr,
234 						     tipc_genl_family.policy,
235 						     NULL);
236 			if (err)
237 				goto err_out;
238 
239 			err = (*cmd->format)(msg, attrbuf);
240 			if (err)
241 				goto err_out;
242 
243 			if (tipc_skb_tailroom(msg->rep) <= 1) {
244 				err = -EMSGSIZE;
245 				goto err_out;
246 			}
247 		}
248 
249 		skb_reset_tail_pointer(buf);
250 		buf->len = 0;
251 
252 	} while (len);
253 
254 	err = 0;
255 
256 err_out:
257 	kfree(attrbuf);
258 	tipc_dump_done(&cb);
259 	kfree_skb(buf);
260 
261 	if (err == -EMSGSIZE) {
262 		/* The legacy API only considered messages filling
263 		 * "ULTRA_STRING_MAX_LEN" to be truncated.
264 		 */
265 		if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
266 			char *tail = skb_tail_pointer(msg->rep);
267 
268 			if (*tail != '\0')
269 				sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
270 					REPLY_TRUNCATED);
271 		}
272 
273 		return 0;
274 	}
275 
276 	return err;
277 }
278 
tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump * cmd,struct tipc_nl_compat_msg * msg)279 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
280 				 struct tipc_nl_compat_msg *msg)
281 {
282 	struct nlmsghdr *nlh;
283 	struct sk_buff *arg;
284 	int err;
285 
286 	if (msg->req_type && (!msg->req_size ||
287 			      !TLV_CHECK_TYPE(msg->req, msg->req_type)))
288 		return -EINVAL;
289 
290 	msg->rep = tipc_tlv_alloc(msg->rep_size);
291 	if (!msg->rep)
292 		return -ENOMEM;
293 
294 	if (msg->rep_type)
295 		tipc_tlv_init(msg->rep, msg->rep_type);
296 
297 	if (cmd->header) {
298 		err = (*cmd->header)(msg);
299 		if (err) {
300 			kfree_skb(msg->rep);
301 			msg->rep = NULL;
302 			return err;
303 		}
304 	}
305 
306 	arg = nlmsg_new(0, GFP_KERNEL);
307 	if (!arg) {
308 		kfree_skb(msg->rep);
309 		msg->rep = NULL;
310 		return -ENOMEM;
311 	}
312 
313 	nlh = nlmsg_put(arg, 0, 0, tipc_genl_family.id, 0, NLM_F_MULTI);
314 	if (!nlh) {
315 		kfree_skb(arg);
316 		kfree_skb(msg->rep);
317 		msg->rep = NULL;
318 		return -EMSGSIZE;
319 	}
320 	nlmsg_end(arg, nlh);
321 
322 	err = __tipc_nl_compat_dumpit(cmd, msg, arg);
323 	if (err) {
324 		kfree_skb(msg->rep);
325 		msg->rep = NULL;
326 	}
327 	kfree_skb(arg);
328 
329 	return err;
330 }
331 
__tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit * cmd,struct tipc_nl_compat_msg * msg)332 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
333 				 struct tipc_nl_compat_msg *msg)
334 {
335 	int err;
336 	struct sk_buff *doit_buf;
337 	struct sk_buff *trans_buf;
338 	struct nlattr **attrbuf;
339 	struct genl_info info;
340 
341 	trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
342 	if (!trans_buf)
343 		return -ENOMEM;
344 
345 	attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
346 				sizeof(struct nlattr *),
347 				GFP_KERNEL);
348 	if (!attrbuf) {
349 		err = -ENOMEM;
350 		goto trans_out;
351 	}
352 
353 	doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
354 	if (!doit_buf) {
355 		err = -ENOMEM;
356 		goto attrbuf_out;
357 	}
358 
359 	memset(&info, 0, sizeof(info));
360 	info.attrs = attrbuf;
361 
362 	rtnl_lock();
363 	err = (*cmd->transcode)(cmd, trans_buf, msg);
364 	if (err)
365 		goto doit_out;
366 
367 	err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
368 				   (const struct nlattr *)trans_buf->data,
369 				   trans_buf->len, NULL, NULL);
370 	if (err)
371 		goto doit_out;
372 
373 	doit_buf->sk = msg->dst_sk;
374 
375 	err = (*cmd->doit)(doit_buf, &info);
376 doit_out:
377 	rtnl_unlock();
378 
379 	kfree_skb(doit_buf);
380 attrbuf_out:
381 	kfree(attrbuf);
382 trans_out:
383 	kfree_skb(trans_buf);
384 
385 	return err;
386 }
387 
tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit * cmd,struct tipc_nl_compat_msg * msg)388 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
389 			       struct tipc_nl_compat_msg *msg)
390 {
391 	int err;
392 
393 	if (msg->req_type && (!msg->req_size ||
394 			      !TLV_CHECK_TYPE(msg->req, msg->req_type)))
395 		return -EINVAL;
396 
397 	err = __tipc_nl_compat_doit(cmd, msg);
398 	if (err)
399 		return err;
400 
401 	/* The legacy API considered an empty message a success message */
402 	msg->rep = tipc_tlv_alloc(0);
403 	if (!msg->rep)
404 		return -ENOMEM;
405 
406 	return 0;
407 }
408 
tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)409 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
410 				      struct nlattr **attrs)
411 {
412 	struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
413 	int err;
414 
415 	if (!attrs[TIPC_NLA_BEARER])
416 		return -EINVAL;
417 
418 	err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
419 					  attrs[TIPC_NLA_BEARER], NULL, NULL);
420 	if (err)
421 		return err;
422 
423 	return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
424 			    nla_data(bearer[TIPC_NLA_BEARER_NAME]),
425 			    nla_len(bearer[TIPC_NLA_BEARER_NAME]));
426 }
427 
tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)428 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
429 					struct sk_buff *skb,
430 					struct tipc_nl_compat_msg *msg)
431 {
432 	struct nlattr *prop;
433 	struct nlattr *bearer;
434 	struct tipc_bearer_config *b;
435 	int len;
436 
437 	b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
438 
439 	bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
440 	if (!bearer)
441 		return -EMSGSIZE;
442 
443 	len = TLV_GET_DATA_LEN(msg->req);
444 	len -= offsetof(struct tipc_bearer_config, name);
445 	if (len <= 0)
446 		return -EINVAL;
447 
448 	len = min_t(int, len, TIPC_MAX_BEARER_NAME);
449 	if (!string_is_valid(b->name, len))
450 		return -EINVAL;
451 
452 	if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
453 		return -EMSGSIZE;
454 
455 	if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
456 		return -EMSGSIZE;
457 
458 	if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
459 		prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
460 		if (!prop)
461 			return -EMSGSIZE;
462 		if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
463 			return -EMSGSIZE;
464 		nla_nest_end(skb, prop);
465 	}
466 	nla_nest_end(skb, bearer);
467 
468 	return 0;
469 }
470 
tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)471 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
472 					 struct sk_buff *skb,
473 					 struct tipc_nl_compat_msg *msg)
474 {
475 	char *name;
476 	struct nlattr *bearer;
477 	int len;
478 
479 	name = (char *)TLV_DATA(msg->req);
480 
481 	bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
482 	if (!bearer)
483 		return -EMSGSIZE;
484 
485 	len = TLV_GET_DATA_LEN(msg->req);
486 	if (len <= 0)
487 		return -EINVAL;
488 
489 	len = min_t(int, len, TIPC_MAX_BEARER_NAME);
490 	if (!string_is_valid(name, len))
491 		return -EINVAL;
492 
493 	if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
494 		return -EMSGSIZE;
495 
496 	nla_nest_end(skb, bearer);
497 
498 	return 0;
499 }
500 
perc(u32 count,u32 total)501 static inline u32 perc(u32 count, u32 total)
502 {
503 	return (count * 100 + (total / 2)) / total;
504 }
505 
__fill_bc_link_stat(struct tipc_nl_compat_msg * msg,struct nlattr * prop[],struct nlattr * stats[])506 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
507 				struct nlattr *prop[], struct nlattr *stats[])
508 {
509 	tipc_tlv_sprintf(msg->rep, "  Window:%u packets\n",
510 			 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
511 
512 	tipc_tlv_sprintf(msg->rep,
513 			 "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
514 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
515 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
516 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
517 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
518 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
519 
520 	tipc_tlv_sprintf(msg->rep,
521 			 "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
522 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
523 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
524 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
525 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
526 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
527 
528 	tipc_tlv_sprintf(msg->rep, "  RX naks:%u defs:%u dups:%u\n",
529 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
530 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
531 			 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
532 
533 	tipc_tlv_sprintf(msg->rep, "  TX naks:%u acks:%u dups:%u\n",
534 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
535 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
536 			 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
537 
538 	tipc_tlv_sprintf(msg->rep,
539 			 "  Congestion link:%u  Send queue max:%u avg:%u",
540 			 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
541 			 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
542 			 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
543 }
544 
tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)545 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
546 					 struct nlattr **attrs)
547 {
548 	char *name;
549 	struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
550 	struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
551 	struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
552 	int err;
553 	int len;
554 
555 	if (!attrs[TIPC_NLA_LINK])
556 		return -EINVAL;
557 
558 	err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
559 					  attrs[TIPC_NLA_LINK], NULL, NULL);
560 	if (err)
561 		return err;
562 
563 	if (!link[TIPC_NLA_LINK_PROP])
564 		return -EINVAL;
565 
566 	err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
567 					  link[TIPC_NLA_LINK_PROP], NULL,
568 					  NULL);
569 	if (err)
570 		return err;
571 
572 	if (!link[TIPC_NLA_LINK_STATS])
573 		return -EINVAL;
574 
575 	err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
576 					  link[TIPC_NLA_LINK_STATS], NULL,
577 					  NULL);
578 	if (err)
579 		return err;
580 
581 	name = (char *)TLV_DATA(msg->req);
582 
583 	len = TLV_GET_DATA_LEN(msg->req);
584 	if (len <= 0)
585 		return -EINVAL;
586 
587 	len = min_t(int, len, TIPC_MAX_LINK_NAME);
588 	if (!string_is_valid(name, len))
589 		return -EINVAL;
590 
591 	if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
592 		return 0;
593 
594 	tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
595 			 (char *)nla_data(link[TIPC_NLA_LINK_NAME]));
596 
597 	if (link[TIPC_NLA_LINK_BROADCAST]) {
598 		__fill_bc_link_stat(msg, prop, stats);
599 		return 0;
600 	}
601 
602 	if (link[TIPC_NLA_LINK_ACTIVE])
603 		tipc_tlv_sprintf(msg->rep, "  ACTIVE");
604 	else if (link[TIPC_NLA_LINK_UP])
605 		tipc_tlv_sprintf(msg->rep, "  STANDBY");
606 	else
607 		tipc_tlv_sprintf(msg->rep, "  DEFUNCT");
608 
609 	tipc_tlv_sprintf(msg->rep, "  MTU:%u  Priority:%u",
610 			 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
611 			 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
612 
613 	tipc_tlv_sprintf(msg->rep, "  Tolerance:%u ms  Window:%u packets\n",
614 			 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
615 			 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
616 
617 	tipc_tlv_sprintf(msg->rep,
618 			 "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
619 			 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
620 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
621 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
622 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
623 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
624 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
625 
626 	tipc_tlv_sprintf(msg->rep,
627 			 "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
628 			 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
629 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
630 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
631 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
632 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
633 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
634 
635 	tipc_tlv_sprintf(msg->rep,
636 			 "  TX profile sample:%u packets  average:%u octets\n",
637 			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
638 			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
639 			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
640 
641 	tipc_tlv_sprintf(msg->rep,
642 			 "  0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
643 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
644 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
645 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
646 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
647 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
648 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
649 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
650 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
651 
652 	tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
653 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
654 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
655 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
656 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
657 			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
658 			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
659 
660 	tipc_tlv_sprintf(msg->rep,
661 			 "  RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
662 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
663 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
664 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
665 			 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
666 			 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
667 
668 	tipc_tlv_sprintf(msg->rep,
669 			 "  TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
670 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
671 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
672 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
673 			 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
674 			 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
675 
676 	tipc_tlv_sprintf(msg->rep,
677 			 "  Congestion link:%u  Send queue max:%u avg:%u",
678 			 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
679 			 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
680 			 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
681 
682 	return 0;
683 }
684 
tipc_nl_compat_link_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)685 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
686 				    struct nlattr **attrs)
687 {
688 	struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
689 	struct tipc_link_info link_info;
690 	int err;
691 
692 	if (!attrs[TIPC_NLA_LINK])
693 		return -EINVAL;
694 
695 	err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
696 					  attrs[TIPC_NLA_LINK], NULL, NULL);
697 	if (err)
698 		return err;
699 
700 	link_info.dest = htonl(nla_get_flag(link[TIPC_NLA_LINK_DEST]));
701 	link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
702 	nla_strscpy(link_info.str, link[TIPC_NLA_LINK_NAME],
703 		    TIPC_MAX_LINK_NAME);
704 
705 	return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
706 			    &link_info, sizeof(link_info));
707 }
708 
__tipc_add_link_prop(struct sk_buff * skb,struct tipc_nl_compat_msg * msg,struct tipc_link_config * lc)709 static int __tipc_add_link_prop(struct sk_buff *skb,
710 				struct tipc_nl_compat_msg *msg,
711 				struct tipc_link_config *lc)
712 {
713 	switch (msg->cmd) {
714 	case TIPC_CMD_SET_LINK_PRI:
715 		return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
716 	case TIPC_CMD_SET_LINK_TOL:
717 		return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
718 	case TIPC_CMD_SET_LINK_WINDOW:
719 		return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
720 	}
721 
722 	return -EINVAL;
723 }
724 
tipc_nl_compat_media_set(struct sk_buff * skb,struct tipc_nl_compat_msg * msg)725 static int tipc_nl_compat_media_set(struct sk_buff *skb,
726 				    struct tipc_nl_compat_msg *msg)
727 {
728 	struct nlattr *prop;
729 	struct nlattr *media;
730 	struct tipc_link_config *lc;
731 
732 	lc = (struct tipc_link_config *)TLV_DATA(msg->req);
733 
734 	media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
735 	if (!media)
736 		return -EMSGSIZE;
737 
738 	if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
739 		return -EMSGSIZE;
740 
741 	prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
742 	if (!prop)
743 		return -EMSGSIZE;
744 
745 	__tipc_add_link_prop(skb, msg, lc);
746 	nla_nest_end(skb, prop);
747 	nla_nest_end(skb, media);
748 
749 	return 0;
750 }
751 
tipc_nl_compat_bearer_set(struct sk_buff * skb,struct tipc_nl_compat_msg * msg)752 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
753 				     struct tipc_nl_compat_msg *msg)
754 {
755 	struct nlattr *prop;
756 	struct nlattr *bearer;
757 	struct tipc_link_config *lc;
758 
759 	lc = (struct tipc_link_config *)TLV_DATA(msg->req);
760 
761 	bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
762 	if (!bearer)
763 		return -EMSGSIZE;
764 
765 	if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
766 		return -EMSGSIZE;
767 
768 	prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
769 	if (!prop)
770 		return -EMSGSIZE;
771 
772 	__tipc_add_link_prop(skb, msg, lc);
773 	nla_nest_end(skb, prop);
774 	nla_nest_end(skb, bearer);
775 
776 	return 0;
777 }
778 
__tipc_nl_compat_link_set(struct sk_buff * skb,struct tipc_nl_compat_msg * msg)779 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
780 				     struct tipc_nl_compat_msg *msg)
781 {
782 	struct nlattr *prop;
783 	struct nlattr *link;
784 	struct tipc_link_config *lc;
785 
786 	lc = (struct tipc_link_config *)TLV_DATA(msg->req);
787 
788 	link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
789 	if (!link)
790 		return -EMSGSIZE;
791 
792 	if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
793 		return -EMSGSIZE;
794 
795 	prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
796 	if (!prop)
797 		return -EMSGSIZE;
798 
799 	__tipc_add_link_prop(skb, msg, lc);
800 	nla_nest_end(skb, prop);
801 	nla_nest_end(skb, link);
802 
803 	return 0;
804 }
805 
tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)806 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
807 				   struct sk_buff *skb,
808 				   struct tipc_nl_compat_msg *msg)
809 {
810 	struct tipc_link_config *lc;
811 	struct tipc_bearer *bearer;
812 	struct tipc_media *media;
813 	int len;
814 
815 	lc = (struct tipc_link_config *)TLV_DATA(msg->req);
816 
817 	len = TLV_GET_DATA_LEN(msg->req);
818 	len -= offsetof(struct tipc_link_config, name);
819 	if (len <= 0)
820 		return -EINVAL;
821 
822 	len = min_t(int, len, TIPC_MAX_LINK_NAME);
823 	if (!string_is_valid(lc->name, len))
824 		return -EINVAL;
825 
826 	media = tipc_media_find(lc->name);
827 	if (media) {
828 		cmd->doit = &__tipc_nl_media_set;
829 		return tipc_nl_compat_media_set(skb, msg);
830 	}
831 
832 	bearer = tipc_bearer_find(msg->net, lc->name);
833 	if (bearer) {
834 		cmd->doit = &__tipc_nl_bearer_set;
835 		return tipc_nl_compat_bearer_set(skb, msg);
836 	}
837 
838 	return __tipc_nl_compat_link_set(skb, msg);
839 }
840 
tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)841 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
842 					   struct sk_buff *skb,
843 					   struct tipc_nl_compat_msg *msg)
844 {
845 	char *name;
846 	struct nlattr *link;
847 	int len;
848 
849 	name = (char *)TLV_DATA(msg->req);
850 
851 	link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
852 	if (!link)
853 		return -EMSGSIZE;
854 
855 	len = TLV_GET_DATA_LEN(msg->req);
856 	if (len <= 0)
857 		return -EINVAL;
858 
859 	len = min_t(int, len, TIPC_MAX_LINK_NAME);
860 	if (!string_is_valid(name, len))
861 		return -EINVAL;
862 
863 	if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
864 		return -EMSGSIZE;
865 
866 	nla_nest_end(skb, link);
867 
868 	return 0;
869 }
870 
tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg * msg)871 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
872 {
873 	int i;
874 	u32 depth;
875 	struct tipc_name_table_query *ntq;
876 	static const char * const header[] = {
877 		"Type       ",
878 		"Lower      Upper      ",
879 		"Port Identity              ",
880 		"Publication Scope"
881 	};
882 
883 	ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
884 	if (TLV_GET_DATA_LEN(msg->req) < (int)sizeof(struct tipc_name_table_query))
885 		return -EINVAL;
886 
887 	depth = ntohl(ntq->depth);
888 
889 	if (depth > 4)
890 		depth = 4;
891 	for (i = 0; i < depth; i++)
892 		tipc_tlv_sprintf(msg->rep, header[i]);
893 	tipc_tlv_sprintf(msg->rep, "\n");
894 
895 	return 0;
896 }
897 
tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)898 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
899 					  struct nlattr **attrs)
900 {
901 	char port_str[27];
902 	struct tipc_name_table_query *ntq;
903 	struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
904 	struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
905 	u32 node, depth, type, lowbound, upbound;
906 	static const char * const scope_str[] = {"", " zone", " cluster",
907 						 " node"};
908 	int err;
909 
910 	if (!attrs[TIPC_NLA_NAME_TABLE])
911 		return -EINVAL;
912 
913 	err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
914 					  attrs[TIPC_NLA_NAME_TABLE], NULL,
915 					  NULL);
916 	if (err)
917 		return err;
918 
919 	if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
920 		return -EINVAL;
921 
922 	err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
923 					  nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
924 					  NULL);
925 	if (err)
926 		return err;
927 
928 	ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
929 
930 	depth = ntohl(ntq->depth);
931 	type = ntohl(ntq->type);
932 	lowbound = ntohl(ntq->lowbound);
933 	upbound = ntohl(ntq->upbound);
934 
935 	if (!(depth & TIPC_NTQ_ALLTYPES) &&
936 	    (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
937 		return 0;
938 	if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
939 		return 0;
940 	if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
941 		return 0;
942 
943 	tipc_tlv_sprintf(msg->rep, "%-10u ",
944 			 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
945 
946 	if (depth == 1)
947 		goto out;
948 
949 	tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
950 			 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
951 			 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
952 
953 	if (depth == 2)
954 		goto out;
955 
956 	node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
957 	sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
958 		tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
959 	tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
960 
961 	if (depth == 3)
962 		goto out;
963 
964 	tipc_tlv_sprintf(msg->rep, "%-10u %s",
965 			 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
966 			 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
967 out:
968 	tipc_tlv_sprintf(msg->rep, "\n");
969 
970 	return 0;
971 }
972 
__tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)973 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
974 				      struct nlattr **attrs)
975 {
976 	u32 type, lower, upper;
977 	struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
978 	int err;
979 
980 	if (!attrs[TIPC_NLA_PUBL])
981 		return -EINVAL;
982 
983 	err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
984 					  attrs[TIPC_NLA_PUBL], NULL, NULL);
985 	if (err)
986 		return err;
987 
988 	type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
989 	lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
990 	upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
991 
992 	if (lower == upper)
993 		tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
994 	else
995 		tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
996 
997 	return 0;
998 }
999 
tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg * msg,u32 sock)1000 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
1001 {
1002 	int err;
1003 	void *hdr;
1004 	struct nlattr *nest;
1005 	struct sk_buff *args;
1006 	struct tipc_nl_compat_cmd_dump dump;
1007 
1008 	args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1009 	if (!args)
1010 		return -ENOMEM;
1011 
1012 	hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
1013 			  TIPC_NL_PUBL_GET);
1014 	if (!hdr) {
1015 		kfree_skb(args);
1016 		return -EMSGSIZE;
1017 	}
1018 
1019 	nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
1020 	if (!nest) {
1021 		kfree_skb(args);
1022 		return -EMSGSIZE;
1023 	}
1024 
1025 	if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
1026 		kfree_skb(args);
1027 		return -EMSGSIZE;
1028 	}
1029 
1030 	nla_nest_end(args, nest);
1031 	genlmsg_end(args, hdr);
1032 
1033 	dump.dumpit = tipc_nl_publ_dump;
1034 	dump.format = __tipc_nl_compat_publ_dump;
1035 
1036 	err = __tipc_nl_compat_dumpit(&dump, msg, args);
1037 
1038 	kfree_skb(args);
1039 
1040 	return err;
1041 }
1042 
tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)1043 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1044 				  struct nlattr **attrs)
1045 {
1046 	int err;
1047 	u32 sock_ref;
1048 	struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1049 
1050 	if (!attrs[TIPC_NLA_SOCK])
1051 		return -EINVAL;
1052 
1053 	err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
1054 					  attrs[TIPC_NLA_SOCK], NULL, NULL);
1055 	if (err)
1056 		return err;
1057 
1058 	sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1059 	tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1060 
1061 	if (sock[TIPC_NLA_SOCK_CON]) {
1062 		u32 node;
1063 		struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1064 
1065 		err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
1066 						  sock[TIPC_NLA_SOCK_CON],
1067 						  NULL, NULL);
1068 
1069 		if (err)
1070 			return err;
1071 
1072 		node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1073 		tipc_tlv_sprintf(msg->rep, "  connected to <%u.%u.%u:%u>",
1074 				 tipc_zone(node),
1075 				 tipc_cluster(node),
1076 				 tipc_node(node),
1077 				 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1078 
1079 		if (con[TIPC_NLA_CON_FLAG])
1080 			tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1081 					 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1082 					 nla_get_u32(con[TIPC_NLA_CON_INST]));
1083 		else
1084 			tipc_tlv_sprintf(msg->rep, "\n");
1085 	} else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1086 		tipc_tlv_sprintf(msg->rep, " bound to");
1087 
1088 		err = tipc_nl_compat_publ_dump(msg, sock_ref);
1089 		if (err)
1090 			return err;
1091 	}
1092 	tipc_tlv_sprintf(msg->rep, "\n");
1093 
1094 	return 0;
1095 }
1096 
tipc_nl_compat_media_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)1097 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1098 				     struct nlattr **attrs)
1099 {
1100 	struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1101 	int err;
1102 
1103 	if (!attrs[TIPC_NLA_MEDIA])
1104 		return -EINVAL;
1105 
1106 	err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1107 					  attrs[TIPC_NLA_MEDIA], NULL, NULL);
1108 	if (err)
1109 		return err;
1110 
1111 	return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1112 			    nla_data(media[TIPC_NLA_MEDIA_NAME]),
1113 			    nla_len(media[TIPC_NLA_MEDIA_NAME]));
1114 }
1115 
tipc_nl_compat_node_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)1116 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1117 				    struct nlattr **attrs)
1118 {
1119 	struct tipc_node_info node_info;
1120 	struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1121 	int err;
1122 
1123 	if (!attrs[TIPC_NLA_NODE])
1124 		return -EINVAL;
1125 
1126 	err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
1127 					  attrs[TIPC_NLA_NODE], NULL, NULL);
1128 	if (err)
1129 		return err;
1130 
1131 	node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1132 	node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1133 
1134 	return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1135 			    sizeof(node_info));
1136 }
1137 
tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit * cmd,struct sk_buff * skb,struct tipc_nl_compat_msg * msg)1138 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1139 				  struct sk_buff *skb,
1140 				  struct tipc_nl_compat_msg *msg)
1141 {
1142 	u32 val;
1143 	struct nlattr *net;
1144 
1145 	val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1146 
1147 	net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
1148 	if (!net)
1149 		return -EMSGSIZE;
1150 
1151 	if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1152 		if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1153 			return -EMSGSIZE;
1154 	} else if (msg->cmd == TIPC_CMD_SET_NETID) {
1155 		if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1156 			return -EMSGSIZE;
1157 	}
1158 	nla_nest_end(skb, net);
1159 
1160 	return 0;
1161 }
1162 
tipc_nl_compat_net_dump(struct tipc_nl_compat_msg * msg,struct nlattr ** attrs)1163 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1164 				   struct nlattr **attrs)
1165 {
1166 	__be32 id;
1167 	struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1168 	int err;
1169 
1170 	if (!attrs[TIPC_NLA_NET])
1171 		return -EINVAL;
1172 
1173 	err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
1174 					  attrs[TIPC_NLA_NET], NULL, NULL);
1175 	if (err)
1176 		return err;
1177 
1178 	id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1179 
1180 	return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1181 }
1182 
tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg * msg)1183 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1184 {
1185 	msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1186 	if (!msg->rep)
1187 		return -ENOMEM;
1188 
1189 	tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1190 	tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1191 
1192 	return 0;
1193 }
1194 
tipc_nl_compat_handle(struct tipc_nl_compat_msg * msg)1195 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1196 {
1197 	struct tipc_nl_compat_cmd_dump dump;
1198 	struct tipc_nl_compat_cmd_doit doit;
1199 
1200 	memset(&dump, 0, sizeof(dump));
1201 	memset(&doit, 0, sizeof(doit));
1202 
1203 	switch (msg->cmd) {
1204 	case TIPC_CMD_NOOP:
1205 		msg->rep = tipc_tlv_alloc(0);
1206 		if (!msg->rep)
1207 			return -ENOMEM;
1208 		return 0;
1209 	case TIPC_CMD_GET_BEARER_NAMES:
1210 		msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1211 		dump.dumpit = tipc_nl_bearer_dump;
1212 		dump.format = tipc_nl_compat_bearer_dump;
1213 		return tipc_nl_compat_dumpit(&dump, msg);
1214 	case TIPC_CMD_ENABLE_BEARER:
1215 		msg->req_type = TIPC_TLV_BEARER_CONFIG;
1216 		doit.doit = __tipc_nl_bearer_enable;
1217 		doit.transcode = tipc_nl_compat_bearer_enable;
1218 		return tipc_nl_compat_doit(&doit, msg);
1219 	case TIPC_CMD_DISABLE_BEARER:
1220 		msg->req_type = TIPC_TLV_BEARER_NAME;
1221 		doit.doit = __tipc_nl_bearer_disable;
1222 		doit.transcode = tipc_nl_compat_bearer_disable;
1223 		return tipc_nl_compat_doit(&doit, msg);
1224 	case TIPC_CMD_SHOW_LINK_STATS:
1225 		msg->req_type = TIPC_TLV_LINK_NAME;
1226 		msg->rep_size = ULTRA_STRING_MAX_LEN;
1227 		msg->rep_type = TIPC_TLV_ULTRA_STRING;
1228 		dump.dumpit = tipc_nl_node_dump_link;
1229 		dump.format = tipc_nl_compat_link_stat_dump;
1230 		return tipc_nl_compat_dumpit(&dump, msg);
1231 	case TIPC_CMD_GET_LINKS:
1232 		msg->req_type = TIPC_TLV_NET_ADDR;
1233 		msg->rep_size = ULTRA_STRING_MAX_LEN;
1234 		dump.dumpit = tipc_nl_node_dump_link;
1235 		dump.format = tipc_nl_compat_link_dump;
1236 		return tipc_nl_compat_dumpit(&dump, msg);
1237 	case TIPC_CMD_SET_LINK_TOL:
1238 	case TIPC_CMD_SET_LINK_PRI:
1239 	case TIPC_CMD_SET_LINK_WINDOW:
1240 		msg->req_type =  TIPC_TLV_LINK_CONFIG;
1241 		doit.doit = tipc_nl_node_set_link;
1242 		doit.transcode = tipc_nl_compat_link_set;
1243 		return tipc_nl_compat_doit(&doit, msg);
1244 	case TIPC_CMD_RESET_LINK_STATS:
1245 		msg->req_type = TIPC_TLV_LINK_NAME;
1246 		doit.doit = tipc_nl_node_reset_link_stats;
1247 		doit.transcode = tipc_nl_compat_link_reset_stats;
1248 		return tipc_nl_compat_doit(&doit, msg);
1249 	case TIPC_CMD_SHOW_NAME_TABLE:
1250 		msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1251 		msg->rep_size = ULTRA_STRING_MAX_LEN;
1252 		msg->rep_type = TIPC_TLV_ULTRA_STRING;
1253 		dump.header = tipc_nl_compat_name_table_dump_header;
1254 		dump.dumpit = tipc_nl_name_table_dump;
1255 		dump.format = tipc_nl_compat_name_table_dump;
1256 		return tipc_nl_compat_dumpit(&dump, msg);
1257 	case TIPC_CMD_SHOW_PORTS:
1258 		msg->rep_size = ULTRA_STRING_MAX_LEN;
1259 		msg->rep_type = TIPC_TLV_ULTRA_STRING;
1260 		dump.dumpit = tipc_nl_sk_dump;
1261 		dump.format = tipc_nl_compat_sk_dump;
1262 		return tipc_nl_compat_dumpit(&dump, msg);
1263 	case TIPC_CMD_GET_MEDIA_NAMES:
1264 		msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1265 		dump.dumpit = tipc_nl_media_dump;
1266 		dump.format = tipc_nl_compat_media_dump;
1267 		return tipc_nl_compat_dumpit(&dump, msg);
1268 	case TIPC_CMD_GET_NODES:
1269 		msg->rep_size = ULTRA_STRING_MAX_LEN;
1270 		dump.dumpit = tipc_nl_node_dump;
1271 		dump.format = tipc_nl_compat_node_dump;
1272 		return tipc_nl_compat_dumpit(&dump, msg);
1273 	case TIPC_CMD_SET_NODE_ADDR:
1274 		msg->req_type = TIPC_TLV_NET_ADDR;
1275 		doit.doit = __tipc_nl_net_set;
1276 		doit.transcode = tipc_nl_compat_net_set;
1277 		return tipc_nl_compat_doit(&doit, msg);
1278 	case TIPC_CMD_SET_NETID:
1279 		msg->req_type = TIPC_TLV_UNSIGNED;
1280 		doit.doit = __tipc_nl_net_set;
1281 		doit.transcode = tipc_nl_compat_net_set;
1282 		return tipc_nl_compat_doit(&doit, msg);
1283 	case TIPC_CMD_GET_NETID:
1284 		msg->rep_size = sizeof(u32);
1285 		dump.dumpit = tipc_nl_net_dump;
1286 		dump.format = tipc_nl_compat_net_dump;
1287 		return tipc_nl_compat_dumpit(&dump, msg);
1288 	case TIPC_CMD_SHOW_STATS:
1289 		return tipc_cmd_show_stats_compat(msg);
1290 	}
1291 
1292 	return -EOPNOTSUPP;
1293 }
1294 
tipc_nl_compat_recv(struct sk_buff * skb,struct genl_info * info)1295 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1296 {
1297 	int err;
1298 	int len;
1299 	struct tipc_nl_compat_msg msg;
1300 	struct nlmsghdr *req_nlh;
1301 	struct nlmsghdr *rep_nlh;
1302 	struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1303 
1304 	memset(&msg, 0, sizeof(msg));
1305 
1306 	req_nlh = (struct nlmsghdr *)skb->data;
1307 	msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1308 	msg.cmd = req_userhdr->cmd;
1309 	msg.net = genl_info_net(info);
1310 	msg.dst_sk = skb->sk;
1311 
1312 	if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1313 		msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1314 		err = -EACCES;
1315 		goto send;
1316 	}
1317 
1318 	msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1319 	if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1320 		msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1321 		err = -EOPNOTSUPP;
1322 		goto send;
1323 	}
1324 
1325 	err = tipc_nl_compat_handle(&msg);
1326 	if ((err == -EOPNOTSUPP) || (err == -EPERM))
1327 		msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1328 	else if (err == -EINVAL)
1329 		msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1330 send:
1331 	if (!msg.rep)
1332 		return err;
1333 
1334 	len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1335 	skb_push(msg.rep, len);
1336 	rep_nlh = nlmsg_hdr(msg.rep);
1337 	memcpy(rep_nlh, info->nlhdr, len);
1338 	rep_nlh->nlmsg_len = msg.rep->len;
1339 	genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1340 
1341 	return err;
1342 }
1343 
1344 static const struct genl_small_ops tipc_genl_compat_ops[] = {
1345 	{
1346 		.cmd		= TIPC_GENL_CMD,
1347 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1348 		.doit		= tipc_nl_compat_recv,
1349 	},
1350 };
1351 
1352 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1353 	.name		= TIPC_GENL_NAME,
1354 	.version	= TIPC_GENL_VERSION,
1355 	.hdrsize	= TIPC_GENL_HDRLEN,
1356 	.maxattr	= 0,
1357 	.netnsok	= true,
1358 	.module		= THIS_MODULE,
1359 	.small_ops	= tipc_genl_compat_ops,
1360 	.n_small_ops	= ARRAY_SIZE(tipc_genl_compat_ops),
1361 };
1362 
tipc_netlink_compat_start(void)1363 int __init tipc_netlink_compat_start(void)
1364 {
1365 	int res;
1366 
1367 	res = genl_register_family(&tipc_genl_compat_family);
1368 	if (res) {
1369 		pr_err("Failed to register legacy compat interface\n");
1370 		return res;
1371 	}
1372 
1373 	return 0;
1374 }
1375 
tipc_netlink_compat_stop(void)1376 void tipc_netlink_compat_stop(void)
1377 {
1378 	genl_unregister_family(&tipc_genl_compat_family);
1379 }
1380