Merge tag 'gpio-v5.3-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux...
[sfrench/cifs-2.6.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/tipc_config.h>
43
44 /* The legacy API had an artificial message length limit called
45  * ULTRA_STRING_MAX_LEN.
46  */
47 #define ULTRA_STRING_MAX_LEN 32768
48
49 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
50
51 #define REPLY_TRUNCATED "<truncated>\n"
52
53 struct tipc_nl_compat_msg {
54         u16 cmd;
55         int rep_type;
56         int rep_size;
57         int req_type;
58         struct net *net;
59         struct sk_buff *rep;
60         struct tlv_desc *req;
61         struct sock *dst_sk;
62 };
63
64 struct tipc_nl_compat_cmd_dump {
65         int (*header)(struct tipc_nl_compat_msg *);
66         int (*dumpit)(struct sk_buff *, struct netlink_callback *);
67         int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
68 };
69
70 struct tipc_nl_compat_cmd_doit {
71         int (*doit)(struct sk_buff *skb, struct genl_info *info);
72         int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
73                          struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
74 };
75
76 static 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
90 static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
91 {
92         return TLV_GET_LEN(tlv) - TLV_SPACE(0);
93 }
94
95 static 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
111 static 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
120 static 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
145 static 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
162 static 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
174 static inline bool string_is_valid(char *s, int len)
175 {
176         return memchr(s, '\0', len) ? true : false;
177 }
178
179 static 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;
198         if (__tipc_dump_start(&cb, msg->net)) {
199                 kfree_skb(buf);
200                 return -ENOMEM;
201         }
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
232 err_out:
233         tipc_dump_done(&cb);
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
254 static 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
260         if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
261                 return -EINVAL;
262
263         msg->rep = tipc_tlv_alloc(msg->rep_size);
264         if (!msg->rep)
265                 return -ENOMEM;
266
267         if (msg->rep_type)
268                 tipc_tlv_init(msg->rep, msg->rep_type);
269
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         }
278
279         arg = nlmsg_new(0, GFP_KERNEL);
280         if (!arg) {
281                 kfree_skb(msg->rep);
282                 msg->rep = NULL;
283                 return -ENOMEM;
284         }
285
286         err = __tipc_nl_compat_dumpit(cmd, msg, arg);
287         if (err) {
288                 kfree_skb(msg->rep);
289                 msg->rep = NULL;
290         }
291         kfree_skb(arg);
292
293         return err;
294 }
295
296 static 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
309         attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
310                                 sizeof(struct nlattr *),
311                                 GFP_KERNEL);
312         if (!attrbuf) {
313                 err = -ENOMEM;
314                 goto trans_out;
315         }
316
317         doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
318         if (!doit_buf) {
319                 err = -ENOMEM;
320                 goto attrbuf_out;
321         }
322
323         memset(&info, 0, sizeof(info));
324         info.attrs = attrbuf;
325
326         rtnl_lock();
327         err = (*cmd->transcode)(cmd, trans_buf, msg);
328         if (err)
329                 goto doit_out;
330
331         err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
332                                    (const struct nlattr *)trans_buf->data,
333                                    trans_buf->len, NULL, NULL);
334         if (err)
335                 goto doit_out;
336
337         doit_buf->sk = msg->dst_sk;
338
339         err = (*cmd->doit)(doit_buf, &info);
340 doit_out:
341         rtnl_unlock();
342
343         kfree_skb(doit_buf);
344 attrbuf_out:
345         kfree(attrbuf);
346 trans_out:
347         kfree_skb(trans_buf);
348
349         return err;
350 }
351
352 static 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
372 static 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];
376         int err;
377
378         if (!attrs[TIPC_NLA_BEARER])
379                 return -EINVAL;
380
381         err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
382                                           attrs[TIPC_NLA_BEARER], NULL, NULL);
383         if (err)
384                 return err;
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
391 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
392                                         struct sk_buff *skb,
393                                         struct tipc_nl_compat_msg *msg)
394 {
395         struct nlattr *prop;
396         struct nlattr *bearer;
397         struct tipc_bearer_config *b;
398         int len;
399
400         b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
401
402         bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
403         if (!bearer)
404                 return -EMSGSIZE;
405
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);
412         if (!string_is_valid(b->name, len))
413                 return -EINVAL;
414
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) {
422                 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
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
434 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
435                                          struct sk_buff *skb,
436                                          struct tipc_nl_compat_msg *msg)
437 {
438         char *name;
439         struct nlattr *bearer;
440         int len;
441
442         name = (char *)TLV_DATA(msg->req);
443
444         bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
445         if (!bearer)
446                 return -EMSGSIZE;
447
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);
453         if (!string_is_valid(name, len))
454                 return -EINVAL;
455
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
464 static inline u32 perc(u32 count, u32 total)
465 {
466         return (count * 100 + (total / 2)) / total;
467 }
468
469 static 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
508 static 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];
515         int err;
516         int len;
517
518         if (!attrs[TIPC_NLA_LINK])
519                 return -EINVAL;
520
521         err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
522                                           attrs[TIPC_NLA_LINK], NULL, NULL);
523         if (err)
524                 return err;
525
526         if (!link[TIPC_NLA_LINK_PROP])
527                 return -EINVAL;
528
529         err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
530                                           link[TIPC_NLA_LINK_PROP], NULL,
531                                           NULL);
532         if (err)
533                 return err;
534
535         if (!link[TIPC_NLA_LINK_STATS])
536                 return -EINVAL;
537
538         err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
539                                           link[TIPC_NLA_LINK_STATS], NULL,
540                                           NULL);
541         if (err)
542                 return err;
543
544         name = (char *)TLV_DATA(msg->req);
545
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);
551         if (!string_is_valid(name, len))
552                 return -EINVAL;
553
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
648 static 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;
653         int err;
654
655         if (!attrs[TIPC_NLA_LINK])
656                 return -EINVAL;
657
658         err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
659                                           attrs[TIPC_NLA_LINK], NULL, NULL);
660         if (err)
661                 return err;
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]));
665         nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
666                     TIPC_MAX_LINK_NAME);
667
668         return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
669                             &link_info, sizeof(link_info));
670 }
671
672 static 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
688 static int tipc_nl_compat_media_set(struct sk_buff *skb,
689                                     struct tipc_nl_compat_msg *msg)
690 {
691         struct nlattr *prop;
692         struct nlattr *media;
693         struct tipc_link_config *lc;
694         int len;
695
696         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
697
698         media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
699         if (!media)
700                 return -EMSGSIZE;
701
702         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
703         if (!string_is_valid(lc->name, len))
704                 return -EINVAL;
705
706         if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
707                 return -EMSGSIZE;
708
709         prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
710         if (!prop)
711                 return -EMSGSIZE;
712
713         __tipc_add_link_prop(skb, msg, lc);
714         nla_nest_end(skb, prop);
715         nla_nest_end(skb, media);
716
717         return 0;
718 }
719
720 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
721                                      struct tipc_nl_compat_msg *msg)
722 {
723         struct nlattr *prop;
724         struct nlattr *bearer;
725         struct tipc_link_config *lc;
726         int len;
727
728         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
729
730         bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
731         if (!bearer)
732                 return -EMSGSIZE;
733
734         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
735         if (!string_is_valid(lc->name, len))
736                 return -EINVAL;
737
738         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
739                 return -EMSGSIZE;
740
741         prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
742         if (!prop)
743                 return -EMSGSIZE;
744
745         __tipc_add_link_prop(skb, msg, lc);
746         nla_nest_end(skb, prop);
747         nla_nest_end(skb, bearer);
748
749         return 0;
750 }
751
752 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
753                                      struct tipc_nl_compat_msg *msg)
754 {
755         struct nlattr *prop;
756         struct nlattr *link;
757         struct tipc_link_config *lc;
758
759         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
760
761         link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
762         if (!link)
763                 return -EMSGSIZE;
764
765         if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
766                 return -EMSGSIZE;
767
768         prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
769         if (!prop)
770                 return -EMSGSIZE;
771
772         __tipc_add_link_prop(skb, msg, lc);
773         nla_nest_end(skb, prop);
774         nla_nest_end(skb, link);
775
776         return 0;
777 }
778
779 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
780                                    struct sk_buff *skb,
781                                    struct tipc_nl_compat_msg *msg)
782 {
783         struct tipc_link_config *lc;
784         struct tipc_bearer *bearer;
785         struct tipc_media *media;
786         int len;
787
788         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
789
790         len = TLV_GET_DATA_LEN(msg->req);
791         len -= offsetof(struct tipc_link_config, name);
792         if (len <= 0)
793                 return -EINVAL;
794
795         len = min_t(int, len, TIPC_MAX_LINK_NAME);
796         if (!string_is_valid(lc->name, len))
797                 return -EINVAL;
798
799         media = tipc_media_find(lc->name);
800         if (media) {
801                 cmd->doit = &__tipc_nl_media_set;
802                 return tipc_nl_compat_media_set(skb, msg);
803         }
804
805         bearer = tipc_bearer_find(msg->net, lc->name);
806         if (bearer) {
807                 cmd->doit = &__tipc_nl_bearer_set;
808                 return tipc_nl_compat_bearer_set(skb, msg);
809         }
810
811         return __tipc_nl_compat_link_set(skb, msg);
812 }
813
814 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
815                                            struct sk_buff *skb,
816                                            struct tipc_nl_compat_msg *msg)
817 {
818         char *name;
819         struct nlattr *link;
820         int len;
821
822         name = (char *)TLV_DATA(msg->req);
823
824         link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
825         if (!link)
826                 return -EMSGSIZE;
827
828         len = TLV_GET_DATA_LEN(msg->req);
829         if (len <= 0)
830                 return -EINVAL;
831
832         len = min_t(int, len, TIPC_MAX_BEARER_NAME);
833         if (!string_is_valid(name, len))
834                 return -EINVAL;
835
836         if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
837                 return -EMSGSIZE;
838
839         nla_nest_end(skb, link);
840
841         return 0;
842 }
843
844 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
845 {
846         int i;
847         u32 depth;
848         struct tipc_name_table_query *ntq;
849         static const char * const header[] = {
850                 "Type       ",
851                 "Lower      Upper      ",
852                 "Port Identity              ",
853                 "Publication Scope"
854         };
855
856         ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
857         if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
858                 return -EINVAL;
859
860         depth = ntohl(ntq->depth);
861
862         if (depth > 4)
863                 depth = 4;
864         for (i = 0; i < depth; i++)
865                 tipc_tlv_sprintf(msg->rep, header[i]);
866         tipc_tlv_sprintf(msg->rep, "\n");
867
868         return 0;
869 }
870
871 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
872                                           struct nlattr **attrs)
873 {
874         char port_str[27];
875         struct tipc_name_table_query *ntq;
876         struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
877         struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
878         u32 node, depth, type, lowbound, upbound;
879         static const char * const scope_str[] = {"", " zone", " cluster",
880                                                  " node"};
881         int err;
882
883         if (!attrs[TIPC_NLA_NAME_TABLE])
884                 return -EINVAL;
885
886         err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
887                                           attrs[TIPC_NLA_NAME_TABLE], NULL,
888                                           NULL);
889         if (err)
890                 return err;
891
892         if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
893                 return -EINVAL;
894
895         err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
896                                           nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
897                                           NULL);
898         if (err)
899                 return err;
900
901         ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
902
903         depth = ntohl(ntq->depth);
904         type = ntohl(ntq->type);
905         lowbound = ntohl(ntq->lowbound);
906         upbound = ntohl(ntq->upbound);
907
908         if (!(depth & TIPC_NTQ_ALLTYPES) &&
909             (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
910                 return 0;
911         if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
912                 return 0;
913         if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
914                 return 0;
915
916         tipc_tlv_sprintf(msg->rep, "%-10u ",
917                          nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
918
919         if (depth == 1)
920                 goto out;
921
922         tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
923                          nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
924                          nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
925
926         if (depth == 2)
927                 goto out;
928
929         node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
930         sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
931                 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
932         tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
933
934         if (depth == 3)
935                 goto out;
936
937         tipc_tlv_sprintf(msg->rep, "%-10u %s",
938                          nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
939                          scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
940 out:
941         tipc_tlv_sprintf(msg->rep, "\n");
942
943         return 0;
944 }
945
946 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
947                                       struct nlattr **attrs)
948 {
949         u32 type, lower, upper;
950         struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
951         int err;
952
953         if (!attrs[TIPC_NLA_PUBL])
954                 return -EINVAL;
955
956         err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
957                                           attrs[TIPC_NLA_PUBL], NULL, NULL);
958         if (err)
959                 return err;
960
961         type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
962         lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
963         upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
964
965         if (lower == upper)
966                 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
967         else
968                 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
969
970         return 0;
971 }
972
973 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
974 {
975         int err;
976         void *hdr;
977         struct nlattr *nest;
978         struct sk_buff *args;
979         struct tipc_nl_compat_cmd_dump dump;
980
981         args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
982         if (!args)
983                 return -ENOMEM;
984
985         hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
986                           TIPC_NL_PUBL_GET);
987         if (!hdr) {
988                 kfree_skb(args);
989                 return -EMSGSIZE;
990         }
991
992         nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
993         if (!nest) {
994                 kfree_skb(args);
995                 return -EMSGSIZE;
996         }
997
998         if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
999                 kfree_skb(args);
1000                 return -EMSGSIZE;
1001         }
1002
1003         nla_nest_end(args, nest);
1004         genlmsg_end(args, hdr);
1005
1006         dump.dumpit = tipc_nl_publ_dump;
1007         dump.format = __tipc_nl_compat_publ_dump;
1008
1009         err = __tipc_nl_compat_dumpit(&dump, msg, args);
1010
1011         kfree_skb(args);
1012
1013         return err;
1014 }
1015
1016 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1017                                   struct nlattr **attrs)
1018 {
1019         int err;
1020         u32 sock_ref;
1021         struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1022
1023         if (!attrs[TIPC_NLA_SOCK])
1024                 return -EINVAL;
1025
1026         err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
1027                                           attrs[TIPC_NLA_SOCK], NULL, NULL);
1028         if (err)
1029                 return err;
1030
1031         sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1032         tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1033
1034         if (sock[TIPC_NLA_SOCK_CON]) {
1035                 u32 node;
1036                 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1037
1038                 err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
1039                                                   sock[TIPC_NLA_SOCK_CON],
1040                                                   NULL, NULL);
1041
1042                 if (err)
1043                         return err;
1044
1045                 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1046                 tipc_tlv_sprintf(msg->rep, "  connected to <%u.%u.%u:%u>",
1047                                  tipc_zone(node),
1048                                  tipc_cluster(node),
1049                                  tipc_node(node),
1050                                  nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1051
1052                 if (con[TIPC_NLA_CON_FLAG])
1053                         tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1054                                          nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1055                                          nla_get_u32(con[TIPC_NLA_CON_INST]));
1056                 else
1057                         tipc_tlv_sprintf(msg->rep, "\n");
1058         } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1059                 tipc_tlv_sprintf(msg->rep, " bound to");
1060
1061                 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1062                 if (err)
1063                         return err;
1064         }
1065         tipc_tlv_sprintf(msg->rep, "\n");
1066
1067         return 0;
1068 }
1069
1070 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1071                                      struct nlattr **attrs)
1072 {
1073         struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1074         int err;
1075
1076         if (!attrs[TIPC_NLA_MEDIA])
1077                 return -EINVAL;
1078
1079         err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1080                                           attrs[TIPC_NLA_MEDIA], NULL, NULL);
1081         if (err)
1082                 return err;
1083
1084         return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1085                             nla_data(media[TIPC_NLA_MEDIA_NAME]),
1086                             nla_len(media[TIPC_NLA_MEDIA_NAME]));
1087 }
1088
1089 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1090                                     struct nlattr **attrs)
1091 {
1092         struct tipc_node_info node_info;
1093         struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1094         int err;
1095
1096         if (!attrs[TIPC_NLA_NODE])
1097                 return -EINVAL;
1098
1099         err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
1100                                           attrs[TIPC_NLA_NODE], NULL, NULL);
1101         if (err)
1102                 return err;
1103
1104         node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1105         node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1106
1107         return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1108                             sizeof(node_info));
1109 }
1110
1111 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1112                                   struct sk_buff *skb,
1113                                   struct tipc_nl_compat_msg *msg)
1114 {
1115         u32 val;
1116         struct nlattr *net;
1117
1118         val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1119
1120         net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
1121         if (!net)
1122                 return -EMSGSIZE;
1123
1124         if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1125                 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1126                         return -EMSGSIZE;
1127         } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1128                 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1129                         return -EMSGSIZE;
1130         }
1131         nla_nest_end(skb, net);
1132
1133         return 0;
1134 }
1135
1136 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1137                                    struct nlattr **attrs)
1138 {
1139         __be32 id;
1140         struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1141         int err;
1142
1143         if (!attrs[TIPC_NLA_NET])
1144                 return -EINVAL;
1145
1146         err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
1147                                           attrs[TIPC_NLA_NET], NULL, NULL);
1148         if (err)
1149                 return err;
1150
1151         id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1152
1153         return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1154 }
1155
1156 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1157 {
1158         msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1159         if (!msg->rep)
1160                 return -ENOMEM;
1161
1162         tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1163         tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1164
1165         return 0;
1166 }
1167
1168 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1169 {
1170         struct tipc_nl_compat_cmd_dump dump;
1171         struct tipc_nl_compat_cmd_doit doit;
1172
1173         memset(&dump, 0, sizeof(dump));
1174         memset(&doit, 0, sizeof(doit));
1175
1176         switch (msg->cmd) {
1177         case TIPC_CMD_NOOP:
1178                 msg->rep = tipc_tlv_alloc(0);
1179                 if (!msg->rep)
1180                         return -ENOMEM;
1181                 return 0;
1182         case TIPC_CMD_GET_BEARER_NAMES:
1183                 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1184                 dump.dumpit = tipc_nl_bearer_dump;
1185                 dump.format = tipc_nl_compat_bearer_dump;
1186                 return tipc_nl_compat_dumpit(&dump, msg);
1187         case TIPC_CMD_ENABLE_BEARER:
1188                 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1189                 doit.doit = __tipc_nl_bearer_enable;
1190                 doit.transcode = tipc_nl_compat_bearer_enable;
1191                 return tipc_nl_compat_doit(&doit, msg);
1192         case TIPC_CMD_DISABLE_BEARER:
1193                 msg->req_type = TIPC_TLV_BEARER_NAME;
1194                 doit.doit = __tipc_nl_bearer_disable;
1195                 doit.transcode = tipc_nl_compat_bearer_disable;
1196                 return tipc_nl_compat_doit(&doit, msg);
1197         case TIPC_CMD_SHOW_LINK_STATS:
1198                 msg->req_type = TIPC_TLV_LINK_NAME;
1199                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1200                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1201                 dump.dumpit = tipc_nl_node_dump_link;
1202                 dump.format = tipc_nl_compat_link_stat_dump;
1203                 return tipc_nl_compat_dumpit(&dump, msg);
1204         case TIPC_CMD_GET_LINKS:
1205                 msg->req_type = TIPC_TLV_NET_ADDR;
1206                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1207                 dump.dumpit = tipc_nl_node_dump_link;
1208                 dump.format = tipc_nl_compat_link_dump;
1209                 return tipc_nl_compat_dumpit(&dump, msg);
1210         case TIPC_CMD_SET_LINK_TOL:
1211         case TIPC_CMD_SET_LINK_PRI:
1212         case TIPC_CMD_SET_LINK_WINDOW:
1213                 msg->req_type =  TIPC_TLV_LINK_CONFIG;
1214                 doit.doit = tipc_nl_node_set_link;
1215                 doit.transcode = tipc_nl_compat_link_set;
1216                 return tipc_nl_compat_doit(&doit, msg);
1217         case TIPC_CMD_RESET_LINK_STATS:
1218                 msg->req_type = TIPC_TLV_LINK_NAME;
1219                 doit.doit = tipc_nl_node_reset_link_stats;
1220                 doit.transcode = tipc_nl_compat_link_reset_stats;
1221                 return tipc_nl_compat_doit(&doit, msg);
1222         case TIPC_CMD_SHOW_NAME_TABLE:
1223                 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1224                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1225                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1226                 dump.header = tipc_nl_compat_name_table_dump_header;
1227                 dump.dumpit = tipc_nl_name_table_dump;
1228                 dump.format = tipc_nl_compat_name_table_dump;
1229                 return tipc_nl_compat_dumpit(&dump, msg);
1230         case TIPC_CMD_SHOW_PORTS:
1231                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1232                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1233                 dump.dumpit = tipc_nl_sk_dump;
1234                 dump.format = tipc_nl_compat_sk_dump;
1235                 return tipc_nl_compat_dumpit(&dump, msg);
1236         case TIPC_CMD_GET_MEDIA_NAMES:
1237                 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1238                 dump.dumpit = tipc_nl_media_dump;
1239                 dump.format = tipc_nl_compat_media_dump;
1240                 return tipc_nl_compat_dumpit(&dump, msg);
1241         case TIPC_CMD_GET_NODES:
1242                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1243                 dump.dumpit = tipc_nl_node_dump;
1244                 dump.format = tipc_nl_compat_node_dump;
1245                 return tipc_nl_compat_dumpit(&dump, msg);
1246         case TIPC_CMD_SET_NODE_ADDR:
1247                 msg->req_type = TIPC_TLV_NET_ADDR;
1248                 doit.doit = __tipc_nl_net_set;
1249                 doit.transcode = tipc_nl_compat_net_set;
1250                 return tipc_nl_compat_doit(&doit, msg);
1251         case TIPC_CMD_SET_NETID:
1252                 msg->req_type = TIPC_TLV_UNSIGNED;
1253                 doit.doit = __tipc_nl_net_set;
1254                 doit.transcode = tipc_nl_compat_net_set;
1255                 return tipc_nl_compat_doit(&doit, msg);
1256         case TIPC_CMD_GET_NETID:
1257                 msg->rep_size = sizeof(u32);
1258                 dump.dumpit = tipc_nl_net_dump;
1259                 dump.format = tipc_nl_compat_net_dump;
1260                 return tipc_nl_compat_dumpit(&dump, msg);
1261         case TIPC_CMD_SHOW_STATS:
1262                 return tipc_cmd_show_stats_compat(msg);
1263         }
1264
1265         return -EOPNOTSUPP;
1266 }
1267
1268 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1269 {
1270         int err;
1271         int len;
1272         struct tipc_nl_compat_msg msg;
1273         struct nlmsghdr *req_nlh;
1274         struct nlmsghdr *rep_nlh;
1275         struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1276
1277         memset(&msg, 0, sizeof(msg));
1278
1279         req_nlh = (struct nlmsghdr *)skb->data;
1280         msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1281         msg.cmd = req_userhdr->cmd;
1282         msg.net = genl_info_net(info);
1283         msg.dst_sk = skb->sk;
1284
1285         if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1286                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1287                 err = -EACCES;
1288                 goto send;
1289         }
1290
1291         len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1292         if (!len || !TLV_OK(msg.req, len)) {
1293                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1294                 err = -EOPNOTSUPP;
1295                 goto send;
1296         }
1297
1298         err = tipc_nl_compat_handle(&msg);
1299         if ((err == -EOPNOTSUPP) || (err == -EPERM))
1300                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1301         else if (err == -EINVAL)
1302                 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1303 send:
1304         if (!msg.rep)
1305                 return err;
1306
1307         len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1308         skb_push(msg.rep, len);
1309         rep_nlh = nlmsg_hdr(msg.rep);
1310         memcpy(rep_nlh, info->nlhdr, len);
1311         rep_nlh->nlmsg_len = msg.rep->len;
1312         genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1313
1314         return err;
1315 }
1316
1317 static const struct genl_ops tipc_genl_compat_ops[] = {
1318         {
1319                 .cmd            = TIPC_GENL_CMD,
1320                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1321                 .doit           = tipc_nl_compat_recv,
1322         },
1323 };
1324
1325 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1326         .name           = TIPC_GENL_NAME,
1327         .version        = TIPC_GENL_VERSION,
1328         .hdrsize        = TIPC_GENL_HDRLEN,
1329         .maxattr        = 0,
1330         .netnsok        = true,
1331         .module         = THIS_MODULE,
1332         .ops            = tipc_genl_compat_ops,
1333         .n_ops          = ARRAY_SIZE(tipc_genl_compat_ops),
1334 };
1335
1336 int __init tipc_netlink_compat_start(void)
1337 {
1338         int res;
1339
1340         res = genl_register_family(&tipc_genl_compat_family);
1341         if (res) {
1342                 pr_err("Failed to register legacy compat interface\n");
1343                 return res;
1344         }
1345
1346         return 0;
1347 }
1348
1349 void tipc_netlink_compat_stop(void)
1350 {
1351         genl_unregister_family(&tipc_genl_compat_family);
1352 }