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