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