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