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