OSDN Git Service

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