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