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