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