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