GNU Linux-libre 4.4.288-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);
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
381         nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
382                          NULL);
383
384         return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
385                             nla_data(bearer[TIPC_NLA_BEARER_NAME]),
386                             nla_len(bearer[TIPC_NLA_BEARER_NAME]));
387 }
388
389 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
390                                         struct sk_buff *skb,
391                                         struct tipc_nl_compat_msg *msg)
392 {
393         struct nlattr *prop;
394         struct nlattr *bearer;
395         struct tipc_bearer_config *b;
396         int len;
397
398         b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
399
400         bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
401         if (!bearer)
402                 return -EMSGSIZE;
403
404         len = TLV_GET_DATA_LEN(msg->req);
405         len -= offsetof(struct tipc_bearer_config, name);
406         if (len <= 0)
407                 return -EINVAL;
408
409         len = min_t(int, len, TIPC_MAX_BEARER_NAME);
410         if (!string_is_valid(b->name, len))
411                 return -EINVAL;
412
413         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
414                 return -EMSGSIZE;
415
416         if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
417                 return -EMSGSIZE;
418
419         if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
420                 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
421                 if (!prop)
422                         return -EMSGSIZE;
423                 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
424                         return -EMSGSIZE;
425                 nla_nest_end(skb, prop);
426         }
427         nla_nest_end(skb, bearer);
428
429         return 0;
430 }
431
432 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
433                                          struct sk_buff *skb,
434                                          struct tipc_nl_compat_msg *msg)
435 {
436         char *name;
437         struct nlattr *bearer;
438         int len;
439
440         name = (char *)TLV_DATA(msg->req);
441
442         bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
443         if (!bearer)
444                 return -EMSGSIZE;
445
446         len = TLV_GET_DATA_LEN(msg->req);
447         if (len <= 0)
448                 return -EINVAL;
449
450         len = min_t(int, len, TIPC_MAX_BEARER_NAME);
451         if (!string_is_valid(name, len))
452                 return -EINVAL;
453
454         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
455                 return -EMSGSIZE;
456
457         nla_nest_end(skb, bearer);
458
459         return 0;
460 }
461
462 static inline u32 perc(u32 count, u32 total)
463 {
464         return (count * 100 + (total / 2)) / total;
465 }
466
467 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
468                                 struct nlattr *prop[], struct nlattr *stats[])
469 {
470         tipc_tlv_sprintf(msg->rep, "  Window:%u packets\n",
471                          nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
472
473         tipc_tlv_sprintf(msg->rep,
474                          "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
475                          nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
476                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
477                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
478                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
479                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
480
481         tipc_tlv_sprintf(msg->rep,
482                          "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
483                          nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
484                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
485                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
486                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
487                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
488
489         tipc_tlv_sprintf(msg->rep, "  RX naks:%u defs:%u dups:%u\n",
490                          nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
491                          nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
492                          nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
493
494         tipc_tlv_sprintf(msg->rep, "  TX naks:%u acks:%u dups:%u\n",
495                          nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
496                          nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
497                          nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
498
499         tipc_tlv_sprintf(msg->rep,
500                          "  Congestion link:%u  Send queue max:%u avg:%u",
501                          nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
502                          nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
503                          nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
504 }
505
506 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
507                                          struct nlattr **attrs)
508 {
509         char *name;
510         struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
511         struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
512         struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
513         int len;
514
515         nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
516
517         nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
518                          NULL);
519
520         nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
521                          NULL);
522
523         name = (char *)TLV_DATA(msg->req);
524
525         len = TLV_GET_DATA_LEN(msg->req);
526         if (len <= 0)
527                 return -EINVAL;
528
529         len = min_t(int, len, TIPC_MAX_LINK_NAME);
530         if (!string_is_valid(name, len))
531                 return -EINVAL;
532
533         if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
534                 return 0;
535
536         tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
537                          nla_data(link[TIPC_NLA_LINK_NAME]));
538
539         if (link[TIPC_NLA_LINK_BROADCAST]) {
540                 __fill_bc_link_stat(msg, prop, stats);
541                 return 0;
542         }
543
544         if (link[TIPC_NLA_LINK_ACTIVE])
545                 tipc_tlv_sprintf(msg->rep, "  ACTIVE");
546         else if (link[TIPC_NLA_LINK_UP])
547                 tipc_tlv_sprintf(msg->rep, "  STANDBY");
548         else
549                 tipc_tlv_sprintf(msg->rep, "  DEFUNCT");
550
551         tipc_tlv_sprintf(msg->rep, "  MTU:%u  Priority:%u",
552                          nla_get_u32(link[TIPC_NLA_LINK_MTU]),
553                          nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
554
555         tipc_tlv_sprintf(msg->rep, "  Tolerance:%u ms  Window:%u packets\n",
556                          nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
557                          nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
558
559         tipc_tlv_sprintf(msg->rep,
560                          "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
561                          nla_get_u32(link[TIPC_NLA_LINK_RX]) -
562                          nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
563                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
564                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
565                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
566                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
567
568         tipc_tlv_sprintf(msg->rep,
569                          "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
570                          nla_get_u32(link[TIPC_NLA_LINK_TX]) -
571                          nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
572                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
573                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
574                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
575                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
576
577         tipc_tlv_sprintf(msg->rep,
578                          "  TX profile sample:%u packets  average:%u octets\n",
579                          nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
580                          nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
581                          nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
582
583         tipc_tlv_sprintf(msg->rep,
584                          "  0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
585                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
586                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
587                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
588                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
589                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
590                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
591                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
592                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
593
594         tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
595                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
596                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
597                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
598                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
599                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
600                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
601
602         tipc_tlv_sprintf(msg->rep,
603                          "  RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
604                          nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
605                          nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
606                          nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
607                          nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
608                          nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
609
610         tipc_tlv_sprintf(msg->rep,
611                          "  TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
612                          nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
613                          nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
614                          nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
615                          nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
616                          nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
617
618         tipc_tlv_sprintf(msg->rep,
619                          "  Congestion link:%u  Send queue max:%u avg:%u",
620                          nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
621                          nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
622                          nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
623
624         return 0;
625 }
626
627 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
628                                     struct nlattr **attrs)
629 {
630         struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
631         struct tipc_link_info link_info;
632
633         nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
634
635         link_info.dest = htonl(nla_get_flag(link[TIPC_NLA_LINK_DEST]));
636         link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
637         nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
638                     TIPC_MAX_LINK_NAME);
639
640         return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
641                             &link_info, sizeof(link_info));
642 }
643
644 static int __tipc_add_link_prop(struct sk_buff *skb,
645                                 struct tipc_nl_compat_msg *msg,
646                                 struct tipc_link_config *lc)
647 {
648         switch (msg->cmd) {
649         case TIPC_CMD_SET_LINK_PRI:
650                 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
651         case TIPC_CMD_SET_LINK_TOL:
652                 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
653         case TIPC_CMD_SET_LINK_WINDOW:
654                 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
655         }
656
657         return -EINVAL;
658 }
659
660 static int tipc_nl_compat_media_set(struct sk_buff *skb,
661                                     struct tipc_nl_compat_msg *msg)
662 {
663         struct nlattr *prop;
664         struct nlattr *media;
665         struct tipc_link_config *lc;
666         int len;
667
668         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
669
670         media = nla_nest_start(skb, TIPC_NLA_MEDIA);
671         if (!media)
672                 return -EMSGSIZE;
673
674         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
675         if (!string_is_valid(lc->name, len))
676                 return -EINVAL;
677
678         if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
679                 return -EMSGSIZE;
680
681         prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
682         if (!prop)
683                 return -EMSGSIZE;
684
685         __tipc_add_link_prop(skb, msg, lc);
686         nla_nest_end(skb, prop);
687         nla_nest_end(skb, media);
688
689         return 0;
690 }
691
692 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
693                                      struct tipc_nl_compat_msg *msg)
694 {
695         struct nlattr *prop;
696         struct nlattr *bearer;
697         struct tipc_link_config *lc;
698         int len;
699
700         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
701
702         bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
703         if (!bearer)
704                 return -EMSGSIZE;
705
706         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
707         if (!string_is_valid(lc->name, len))
708                 return -EINVAL;
709
710         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
711                 return -EMSGSIZE;
712
713         prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
714         if (!prop)
715                 return -EMSGSIZE;
716
717         __tipc_add_link_prop(skb, msg, lc);
718         nla_nest_end(skb, prop);
719         nla_nest_end(skb, bearer);
720
721         return 0;
722 }
723
724 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
725                                      struct tipc_nl_compat_msg *msg)
726 {
727         struct nlattr *prop;
728         struct nlattr *link;
729         struct tipc_link_config *lc;
730
731         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
732
733         link = nla_nest_start(skb, TIPC_NLA_LINK);
734         if (!link)
735                 return -EMSGSIZE;
736
737         if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
738                 return -EMSGSIZE;
739
740         prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
741         if (!prop)
742                 return -EMSGSIZE;
743
744         __tipc_add_link_prop(skb, msg, lc);
745         nla_nest_end(skb, prop);
746         nla_nest_end(skb, link);
747
748         return 0;
749 }
750
751 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
752                                    struct sk_buff *skb,
753                                    struct tipc_nl_compat_msg *msg)
754 {
755         struct tipc_link_config *lc;
756         struct tipc_bearer *bearer;
757         struct tipc_media *media;
758         int len;
759
760         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
761
762         len = TLV_GET_DATA_LEN(msg->req);
763         len -= offsetof(struct tipc_link_config, name);
764         if (len <= 0)
765                 return -EINVAL;
766
767         len = min_t(int, len, TIPC_MAX_LINK_NAME);
768         if (!string_is_valid(lc->name, len))
769                 return -EINVAL;
770
771         media = tipc_media_find(lc->name);
772         if (media) {
773                 cmd->doit = &tipc_nl_media_set;
774                 return tipc_nl_compat_media_set(skb, msg);
775         }
776
777         bearer = tipc_bearer_find(msg->net, lc->name);
778         if (bearer) {
779                 cmd->doit = &tipc_nl_bearer_set;
780                 return tipc_nl_compat_bearer_set(skb, msg);
781         }
782
783         return __tipc_nl_compat_link_set(skb, msg);
784 }
785
786 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
787                                            struct sk_buff *skb,
788                                            struct tipc_nl_compat_msg *msg)
789 {
790         char *name;
791         struct nlattr *link;
792         int len;
793
794         name = (char *)TLV_DATA(msg->req);
795
796         link = nla_nest_start(skb, TIPC_NLA_LINK);
797         if (!link)
798                 return -EMSGSIZE;
799
800         len = TLV_GET_DATA_LEN(msg->req);
801         if (len <= 0)
802                 return -EINVAL;
803
804         len = min_t(int, len, TIPC_MAX_LINK_NAME);
805         if (!string_is_valid(name, len))
806                 return -EINVAL;
807
808         if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
809                 return -EMSGSIZE;
810
811         nla_nest_end(skb, link);
812
813         return 0;
814 }
815
816 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
817 {
818         int i;
819         u32 depth;
820         struct tipc_name_table_query *ntq;
821         static const char * const header[] = {
822                 "Type       ",
823                 "Lower      Upper      ",
824                 "Port Identity              ",
825                 "Publication Scope"
826         };
827
828         ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
829         if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
830                 return -EINVAL;
831
832         depth = ntohl(ntq->depth);
833
834         if (depth > 4)
835                 depth = 4;
836         for (i = 0; i < depth; i++)
837                 tipc_tlv_sprintf(msg->rep, header[i]);
838         tipc_tlv_sprintf(msg->rep, "\n");
839
840         return 0;
841 }
842
843 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
844                                           struct nlattr **attrs)
845 {
846         char port_str[27];
847         struct tipc_name_table_query *ntq;
848         struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
849         struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
850         u32 node, depth, type, lowbound, upbound;
851         static const char * const scope_str[] = {"", " zone", " cluster",
852                                                  " node"};
853
854         nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
855                          attrs[TIPC_NLA_NAME_TABLE], NULL);
856
857         nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
858                          NULL);
859
860         ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
861
862         depth = ntohl(ntq->depth);
863         type = ntohl(ntq->type);
864         lowbound = ntohl(ntq->lowbound);
865         upbound = ntohl(ntq->upbound);
866
867         if (!(depth & TIPC_NTQ_ALLTYPES) &&
868             (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
869                 return 0;
870         if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
871                 return 0;
872         if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
873                 return 0;
874
875         tipc_tlv_sprintf(msg->rep, "%-10u ",
876                          nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
877
878         if (depth == 1)
879                 goto out;
880
881         tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
882                          nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
883                          nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
884
885         if (depth == 2)
886                 goto out;
887
888         node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
889         sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
890                 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
891         tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
892
893         if (depth == 3)
894                 goto out;
895
896         tipc_tlv_sprintf(msg->rep, "%-10u %s",
897                          nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
898                          scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
899 out:
900         tipc_tlv_sprintf(msg->rep, "\n");
901
902         return 0;
903 }
904
905 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
906                                       struct nlattr **attrs)
907 {
908         u32 type, lower, upper;
909         struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
910
911         nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL], NULL);
912
913         type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
914         lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
915         upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
916
917         if (lower == upper)
918                 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
919         else
920                 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
921
922         return 0;
923 }
924
925 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
926 {
927         int err;
928         void *hdr;
929         struct nlattr *nest;
930         struct sk_buff *args;
931         struct tipc_nl_compat_cmd_dump dump;
932
933         args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
934         if (!args)
935                 return -ENOMEM;
936
937         hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
938                           TIPC_NL_PUBL_GET);
939         if (!hdr) {
940                 kfree_skb(args);
941                 return -EMSGSIZE;
942         }
943
944         nest = nla_nest_start(args, TIPC_NLA_SOCK);
945         if (!nest) {
946                 kfree_skb(args);
947                 return -EMSGSIZE;
948         }
949
950         if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
951                 kfree_skb(args);
952                 return -EMSGSIZE;
953         }
954
955         nla_nest_end(args, nest);
956         genlmsg_end(args, hdr);
957
958         dump.dumpit = tipc_nl_publ_dump;
959         dump.format = __tipc_nl_compat_publ_dump;
960
961         err = __tipc_nl_compat_dumpit(&dump, msg, args);
962
963         kfree_skb(args);
964
965         return err;
966 }
967
968 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
969                                   struct nlattr **attrs)
970 {
971         int err;
972         u32 sock_ref;
973         struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
974
975         nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK], NULL);
976
977         sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
978         tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
979
980         if (sock[TIPC_NLA_SOCK_CON]) {
981                 u32 node;
982                 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
983
984                 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
985                                  NULL);
986
987                 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
988                 tipc_tlv_sprintf(msg->rep, "  connected to <%u.%u.%u:%u>",
989                                  tipc_zone(node),
990                                  tipc_cluster(node),
991                                  tipc_node(node),
992                                  nla_get_u32(con[TIPC_NLA_CON_SOCK]));
993
994                 if (con[TIPC_NLA_CON_FLAG])
995                         tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
996                                          nla_get_u32(con[TIPC_NLA_CON_TYPE]),
997                                          nla_get_u32(con[TIPC_NLA_CON_INST]));
998                 else
999                         tipc_tlv_sprintf(msg->rep, "\n");
1000         } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1001                 tipc_tlv_sprintf(msg->rep, " bound to");
1002
1003                 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1004                 if (err)
1005                         return err;
1006         }
1007         tipc_tlv_sprintf(msg->rep, "\n");
1008
1009         return 0;
1010 }
1011
1012 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1013                                      struct nlattr **attrs)
1014 {
1015         struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1016
1017         nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
1018                          NULL);
1019
1020         return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1021                             nla_data(media[TIPC_NLA_MEDIA_NAME]),
1022                             nla_len(media[TIPC_NLA_MEDIA_NAME]));
1023 }
1024
1025 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1026                                     struct nlattr **attrs)
1027 {
1028         struct tipc_node_info node_info;
1029         struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1030
1031         nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE], NULL);
1032
1033         node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1034         node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1035
1036         return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1037                             sizeof(node_info));
1038 }
1039
1040 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1041                                   struct sk_buff *skb,
1042                                   struct tipc_nl_compat_msg *msg)
1043 {
1044         u32 val;
1045         struct nlattr *net;
1046
1047         val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1048
1049         net = nla_nest_start(skb, TIPC_NLA_NET);
1050         if (!net)
1051                 return -EMSGSIZE;
1052
1053         if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1054                 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1055                         return -EMSGSIZE;
1056         } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1057                 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1058                         return -EMSGSIZE;
1059         }
1060         nla_nest_end(skb, net);
1061
1062         return 0;
1063 }
1064
1065 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1066                                    struct nlattr **attrs)
1067 {
1068         __be32 id;
1069         struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1070
1071         nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET], NULL);
1072         id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1073
1074         return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1075 }
1076
1077 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1078 {
1079         msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1080         if (!msg->rep)
1081                 return -ENOMEM;
1082
1083         tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1084         tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1085
1086         return 0;
1087 }
1088
1089 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1090 {
1091         struct tipc_nl_compat_cmd_dump dump;
1092         struct tipc_nl_compat_cmd_doit doit;
1093
1094         memset(&dump, 0, sizeof(dump));
1095         memset(&doit, 0, sizeof(doit));
1096
1097         switch (msg->cmd) {
1098         case TIPC_CMD_NOOP:
1099                 msg->rep = tipc_tlv_alloc(0);
1100                 if (!msg->rep)
1101                         return -ENOMEM;
1102                 return 0;
1103         case TIPC_CMD_GET_BEARER_NAMES:
1104                 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1105                 dump.dumpit = tipc_nl_bearer_dump;
1106                 dump.format = tipc_nl_compat_bearer_dump;
1107                 return tipc_nl_compat_dumpit(&dump, msg);
1108         case TIPC_CMD_ENABLE_BEARER:
1109                 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1110                 doit.doit = tipc_nl_bearer_enable;
1111                 doit.transcode = tipc_nl_compat_bearer_enable;
1112                 return tipc_nl_compat_doit(&doit, msg);
1113         case TIPC_CMD_DISABLE_BEARER:
1114                 msg->req_type = TIPC_TLV_BEARER_NAME;
1115                 doit.doit = tipc_nl_bearer_disable;
1116                 doit.transcode = tipc_nl_compat_bearer_disable;
1117                 return tipc_nl_compat_doit(&doit, msg);
1118         case TIPC_CMD_SHOW_LINK_STATS:
1119                 msg->req_type = TIPC_TLV_LINK_NAME;
1120                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1121                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1122                 dump.dumpit = tipc_nl_link_dump;
1123                 dump.format = tipc_nl_compat_link_stat_dump;
1124                 return tipc_nl_compat_dumpit(&dump, msg);
1125         case TIPC_CMD_GET_LINKS:
1126                 msg->req_type = TIPC_TLV_NET_ADDR;
1127                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1128                 dump.dumpit = tipc_nl_link_dump;
1129                 dump.format = tipc_nl_compat_link_dump;
1130                 return tipc_nl_compat_dumpit(&dump, msg);
1131         case TIPC_CMD_SET_LINK_TOL:
1132         case TIPC_CMD_SET_LINK_PRI:
1133         case TIPC_CMD_SET_LINK_WINDOW:
1134                 msg->req_type =  TIPC_TLV_LINK_CONFIG;
1135                 doit.doit = tipc_nl_link_set;
1136                 doit.transcode = tipc_nl_compat_link_set;
1137                 return tipc_nl_compat_doit(&doit, msg);
1138         case TIPC_CMD_RESET_LINK_STATS:
1139                 msg->req_type = TIPC_TLV_LINK_NAME;
1140                 doit.doit = tipc_nl_link_reset_stats;
1141                 doit.transcode = tipc_nl_compat_link_reset_stats;
1142                 return tipc_nl_compat_doit(&doit, msg);
1143         case TIPC_CMD_SHOW_NAME_TABLE:
1144                 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1145                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1146                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1147                 dump.header = tipc_nl_compat_name_table_dump_header;
1148                 dump.dumpit = tipc_nl_name_table_dump;
1149                 dump.format = tipc_nl_compat_name_table_dump;
1150                 return tipc_nl_compat_dumpit(&dump, msg);
1151         case TIPC_CMD_SHOW_PORTS:
1152                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1153                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1154                 dump.dumpit = tipc_nl_sk_dump;
1155                 dump.format = tipc_nl_compat_sk_dump;
1156                 return tipc_nl_compat_dumpit(&dump, msg);
1157         case TIPC_CMD_GET_MEDIA_NAMES:
1158                 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1159                 dump.dumpit = tipc_nl_media_dump;
1160                 dump.format = tipc_nl_compat_media_dump;
1161                 return tipc_nl_compat_dumpit(&dump, msg);
1162         case TIPC_CMD_GET_NODES:
1163                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1164                 dump.dumpit = tipc_nl_node_dump;
1165                 dump.format = tipc_nl_compat_node_dump;
1166                 return tipc_nl_compat_dumpit(&dump, msg);
1167         case TIPC_CMD_SET_NODE_ADDR:
1168                 msg->req_type = TIPC_TLV_NET_ADDR;
1169                 doit.doit = tipc_nl_net_set;
1170                 doit.transcode = tipc_nl_compat_net_set;
1171                 return tipc_nl_compat_doit(&doit, msg);
1172         case TIPC_CMD_SET_NETID:
1173                 msg->req_type = TIPC_TLV_UNSIGNED;
1174                 doit.doit = tipc_nl_net_set;
1175                 doit.transcode = tipc_nl_compat_net_set;
1176                 return tipc_nl_compat_doit(&doit, msg);
1177         case TIPC_CMD_GET_NETID:
1178                 msg->rep_size = sizeof(u32);
1179                 dump.dumpit = tipc_nl_net_dump;
1180                 dump.format = tipc_nl_compat_net_dump;
1181                 return tipc_nl_compat_dumpit(&dump, msg);
1182         case TIPC_CMD_SHOW_STATS:
1183                 return tipc_cmd_show_stats_compat(msg);
1184         }
1185
1186         return -EOPNOTSUPP;
1187 }
1188
1189 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1190 {
1191         int err;
1192         int len;
1193         struct tipc_nl_compat_msg msg;
1194         struct nlmsghdr *req_nlh;
1195         struct nlmsghdr *rep_nlh;
1196         struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1197
1198         memset(&msg, 0, sizeof(msg));
1199
1200         req_nlh = (struct nlmsghdr *)skb->data;
1201         msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1202         msg.cmd = req_userhdr->cmd;
1203         msg.dst_sk = info->dst_sk;
1204         msg.net = genl_info_net(info);
1205
1206         if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1207                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1208                 err = -EACCES;
1209                 goto send;
1210         }
1211
1212         msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1213         if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
1214                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1215                 err = -EOPNOTSUPP;
1216                 goto send;
1217         }
1218
1219         err = tipc_nl_compat_handle(&msg);
1220         if ((err == -EOPNOTSUPP) || (err == -EPERM))
1221                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1222         else if (err == -EINVAL)
1223                 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1224 send:
1225         if (!msg.rep)
1226                 return err;
1227
1228         len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1229         skb_push(msg.rep, len);
1230         rep_nlh = nlmsg_hdr(msg.rep);
1231         memcpy(rep_nlh, info->nlhdr, len);
1232         rep_nlh->nlmsg_len = msg.rep->len;
1233         genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1234
1235         return err;
1236 }
1237
1238 static struct genl_family tipc_genl_compat_family = {
1239         .id             = GENL_ID_GENERATE,
1240         .name           = TIPC_GENL_NAME,
1241         .version        = TIPC_GENL_VERSION,
1242         .hdrsize        = TIPC_GENL_HDRLEN,
1243         .maxattr        = 0,
1244         .netnsok        = true,
1245 };
1246
1247 static struct genl_ops tipc_genl_compat_ops[] = {
1248         {
1249                 .cmd            = TIPC_GENL_CMD,
1250                 .doit           = tipc_nl_compat_recv,
1251         },
1252 };
1253
1254 int tipc_netlink_compat_start(void)
1255 {
1256         int res;
1257
1258         res = genl_register_family_with_ops(&tipc_genl_compat_family,
1259                                             tipc_genl_compat_ops);
1260         if (res) {
1261                 pr_err("Failed to register legacy compat interface\n");
1262                 return res;
1263         }
1264
1265         return 0;
1266 }
1267
1268 void tipc_netlink_compat_stop(void)
1269 {
1270         genl_unregister_family(&tipc_genl_compat_family);
1271 }