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