s390/vdso: drop unnecessary cc-ldoption
[linux-2.6-microblaze.git] / net / tipc / netlink_compat.c
1 /*
2  * Copyright (c) 2014, Ericsson AB
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the names of the copyright holders nor the names of its
14  *    contributors may be used to endorse or promote products derived from
15  *    this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU General Public License ("GPL") version 2 as published by the Free
19  * Software Foundation.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "core.h"
35 #include "bearer.h"
36 #include "link.h"
37 #include "name_table.h"
38 #include "socket.h"
39 #include "node.h"
40 #include "net.h"
41 #include <net/genetlink.h>
42 #include <linux/tipc_config.h>
43
44 /* The legacy API had an artificial message length limit called
45  * ULTRA_STRING_MAX_LEN.
46  */
47 #define ULTRA_STRING_MAX_LEN 32768
48
49 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
50
51 #define REPLY_TRUNCATED "<truncated>\n"
52
53 struct tipc_nl_compat_msg {
54         u16 cmd;
55         int rep_type;
56         int rep_size;
57         int req_type;
58         struct net *net;
59         struct sk_buff *rep;
60         struct tlv_desc *req;
61         struct sock *dst_sk;
62 };
63
64 struct tipc_nl_compat_cmd_dump {
65         int (*header)(struct tipc_nl_compat_msg *);
66         int (*dumpit)(struct sk_buff *, struct netlink_callback *);
67         int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
68 };
69
70 struct tipc_nl_compat_cmd_doit {
71         int (*doit)(struct sk_buff *skb, struct genl_info *info);
72         int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
73                          struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
74 };
75
76 static int tipc_skb_tailroom(struct sk_buff *skb)
77 {
78         int tailroom;
79         int limit;
80
81         tailroom = skb_tailroom(skb);
82         limit = TIPC_SKB_MAX - skb->len;
83
84         if (tailroom < limit)
85                 return tailroom;
86
87         return limit;
88 }
89
90 static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
91 {
92         return TLV_GET_LEN(tlv) - TLV_SPACE(0);
93 }
94
95 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
96 {
97         struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
98
99         if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
100                 return -EMSGSIZE;
101
102         skb_put(skb, TLV_SPACE(len));
103         tlv->tlv_type = htons(type);
104         tlv->tlv_len = htons(TLV_LENGTH(len));
105         if (len && data)
106                 memcpy(TLV_DATA(tlv), data, len);
107
108         return 0;
109 }
110
111 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
112 {
113         struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
114
115         TLV_SET_LEN(tlv, 0);
116         TLV_SET_TYPE(tlv, type);
117         skb_put(skb, sizeof(struct tlv_desc));
118 }
119
120 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
121 {
122         int n;
123         u16 len;
124         u32 rem;
125         char *buf;
126         struct tlv_desc *tlv;
127         va_list args;
128
129         rem = tipc_skb_tailroom(skb);
130
131         tlv = (struct tlv_desc *)skb->data;
132         len = TLV_GET_LEN(tlv);
133         buf = TLV_DATA(tlv) + len;
134
135         va_start(args, fmt);
136         n = vscnprintf(buf, rem, fmt, args);
137         va_end(args);
138
139         TLV_SET_LEN(tlv, n + len);
140         skb_put(skb, n);
141
142         return n;
143 }
144
145 static struct sk_buff *tipc_tlv_alloc(int size)
146 {
147         int hdr_len;
148         struct sk_buff *buf;
149
150         size = TLV_SPACE(size);
151         hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
152
153         buf = alloc_skb(hdr_len + size, GFP_KERNEL);
154         if (!buf)
155                 return NULL;
156
157         skb_reserve(buf, hdr_len);
158
159         return buf;
160 }
161
162 static struct sk_buff *tipc_get_err_tlv(char *str)
163 {
164         int str_len = strlen(str) + 1;
165         struct sk_buff *buf;
166
167         buf = tipc_tlv_alloc(TLV_SPACE(str_len));
168         if (buf)
169                 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
170
171         return buf;
172 }
173
174 static inline bool string_is_valid(char *s, int len)
175 {
176         return memchr(s, '\0', len) ? true : false;
177 }
178
179 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
180                                    struct tipc_nl_compat_msg *msg,
181                                    struct sk_buff *arg)
182 {
183         int len = 0;
184         int err;
185         struct sk_buff *buf;
186         struct nlmsghdr *nlmsg;
187         struct netlink_callback cb;
188
189         memset(&cb, 0, sizeof(cb));
190         cb.nlh = (struct nlmsghdr *)arg->data;
191         cb.skb = arg;
192
193         buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
194         if (!buf)
195                 return -ENOMEM;
196
197         buf->sk = msg->dst_sk;
198         if (__tipc_dump_start(&cb, msg->net)) {
199                 kfree_skb(buf);
200                 return -ENOMEM;
201         }
202
203         do {
204                 int rem;
205
206                 len = (*cmd->dumpit)(buf, &cb);
207
208                 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
209                         struct nlattr **attrs;
210
211                         err = tipc_nlmsg_parse(nlmsg, &attrs);
212                         if (err)
213                                 goto err_out;
214
215                         err = (*cmd->format)(msg, attrs);
216                         if (err)
217                                 goto err_out;
218
219                         if (tipc_skb_tailroom(msg->rep) <= 1) {
220                                 err = -EMSGSIZE;
221                                 goto err_out;
222                         }
223                 }
224
225                 skb_reset_tail_pointer(buf);
226                 buf->len = 0;
227
228         } while (len);
229
230         err = 0;
231
232 err_out:
233         tipc_dump_done(&cb);
234         kfree_skb(buf);
235
236         if (err == -EMSGSIZE) {
237                 /* The legacy API only considered messages filling
238                  * "ULTRA_STRING_MAX_LEN" to be truncated.
239                  */
240                 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
241                         char *tail = skb_tail_pointer(msg->rep);
242
243                         if (*tail != '\0')
244                                 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
245                                         REPLY_TRUNCATED);
246                 }
247
248                 return 0;
249         }
250
251         return err;
252 }
253
254 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
255                                  struct tipc_nl_compat_msg *msg)
256 {
257         int err;
258         struct sk_buff *arg;
259
260         if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
261                 return -EINVAL;
262
263         msg->rep = tipc_tlv_alloc(msg->rep_size);
264         if (!msg->rep)
265                 return -ENOMEM;
266
267         if (msg->rep_type)
268                 tipc_tlv_init(msg->rep, msg->rep_type);
269
270         if (cmd->header)
271                 (*cmd->header)(msg);
272
273         arg = nlmsg_new(0, GFP_KERNEL);
274         if (!arg) {
275                 kfree_skb(msg->rep);
276                 msg->rep = NULL;
277                 return -ENOMEM;
278         }
279
280         err = __tipc_nl_compat_dumpit(cmd, msg, arg);
281         if (err) {
282                 kfree_skb(msg->rep);
283                 msg->rep = NULL;
284         }
285         kfree_skb(arg);
286
287         return err;
288 }
289
290 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
291                                  struct tipc_nl_compat_msg *msg)
292 {
293         int err;
294         struct sk_buff *doit_buf;
295         struct sk_buff *trans_buf;
296         struct nlattr **attrbuf;
297         struct genl_info info;
298
299         trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
300         if (!trans_buf)
301                 return -ENOMEM;
302
303         attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
304                                 sizeof(struct nlattr *),
305                                 GFP_KERNEL);
306         if (!attrbuf) {
307                 err = -ENOMEM;
308                 goto trans_out;
309         }
310
311         doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
312         if (!doit_buf) {
313                 err = -ENOMEM;
314                 goto attrbuf_out;
315         }
316
317         memset(&info, 0, sizeof(info));
318         info.attrs = attrbuf;
319
320         rtnl_lock();
321         err = (*cmd->transcode)(cmd, trans_buf, msg);
322         if (err)
323                 goto doit_out;
324
325         err = nla_parse(attrbuf, tipc_genl_family.maxattr,
326                         (const struct nlattr *)trans_buf->data,
327                         trans_buf->len, NULL, NULL);
328         if (err)
329                 goto doit_out;
330
331         doit_buf->sk = msg->dst_sk;
332
333         err = (*cmd->doit)(doit_buf, &info);
334 doit_out:
335         rtnl_unlock();
336
337         kfree_skb(doit_buf);
338 attrbuf_out:
339         kfree(attrbuf);
340 trans_out:
341         kfree_skb(trans_buf);
342
343         return err;
344 }
345
346 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
347                                struct tipc_nl_compat_msg *msg)
348 {
349         int err;
350
351         if (msg->req_type && !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         int err;
371
372         if (!attrs[TIPC_NLA_BEARER])
373                 return -EINVAL;
374
375         err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
376                                attrs[TIPC_NLA_BEARER], NULL, NULL);
377         if (err)
378                 return err;
379
380         return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
381                             nla_data(bearer[TIPC_NLA_BEARER_NAME]),
382                             nla_len(bearer[TIPC_NLA_BEARER_NAME]));
383 }
384
385 static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
386                                         struct sk_buff *skb,
387                                         struct tipc_nl_compat_msg *msg)
388 {
389         struct nlattr *prop;
390         struct nlattr *bearer;
391         struct tipc_bearer_config *b;
392         int len;
393
394         b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
395
396         bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
397         if (!bearer)
398                 return -EMSGSIZE;
399
400         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
401         if (!string_is_valid(b->name, len))
402                 return -EINVAL;
403
404         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
405                 return -EMSGSIZE;
406
407         if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
408                 return -EMSGSIZE;
409
410         if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
411                 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
412                 if (!prop)
413                         return -EMSGSIZE;
414                 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
415                         return -EMSGSIZE;
416                 nla_nest_end(skb, prop);
417         }
418         nla_nest_end(skb, bearer);
419
420         return 0;
421 }
422
423 static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
424                                          struct sk_buff *skb,
425                                          struct tipc_nl_compat_msg *msg)
426 {
427         char *name;
428         struct nlattr *bearer;
429         int len;
430
431         name = (char *)TLV_DATA(msg->req);
432
433         bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
434         if (!bearer)
435                 return -EMSGSIZE;
436
437         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
438         if (!string_is_valid(name, len))
439                 return -EINVAL;
440
441         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
442                 return -EMSGSIZE;
443
444         nla_nest_end(skb, bearer);
445
446         return 0;
447 }
448
449 static inline u32 perc(u32 count, u32 total)
450 {
451         return (count * 100 + (total / 2)) / total;
452 }
453
454 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
455                                 struct nlattr *prop[], struct nlattr *stats[])
456 {
457         tipc_tlv_sprintf(msg->rep, "  Window:%u packets\n",
458                          nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
459
460         tipc_tlv_sprintf(msg->rep,
461                          "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
462                          nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
463                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
464                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
465                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
466                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
467
468         tipc_tlv_sprintf(msg->rep,
469                          "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
470                          nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
471                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
472                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
473                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
474                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
475
476         tipc_tlv_sprintf(msg->rep, "  RX naks:%u defs:%u dups:%u\n",
477                          nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
478                          nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
479                          nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
480
481         tipc_tlv_sprintf(msg->rep, "  TX naks:%u acks:%u dups:%u\n",
482                          nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
483                          nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
484                          nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
485
486         tipc_tlv_sprintf(msg->rep,
487                          "  Congestion link:%u  Send queue max:%u avg:%u",
488                          nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
489                          nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
490                          nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
491 }
492
493 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
494                                          struct nlattr **attrs)
495 {
496         char *name;
497         struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
498         struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
499         struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
500         int err;
501         int len;
502
503         if (!attrs[TIPC_NLA_LINK])
504                 return -EINVAL;
505
506         err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
507                                NULL, NULL);
508         if (err)
509                 return err;
510
511         if (!link[TIPC_NLA_LINK_PROP])
512                 return -EINVAL;
513
514         err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
515                                link[TIPC_NLA_LINK_PROP], NULL, NULL);
516         if (err)
517                 return err;
518
519         if (!link[TIPC_NLA_LINK_STATS])
520                 return -EINVAL;
521
522         err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
523                                link[TIPC_NLA_LINK_STATS], NULL, NULL);
524         if (err)
525                 return err;
526
527         name = (char *)TLV_DATA(msg->req);
528
529         len = min_t(int, TLV_GET_DATA_LEN(msg->req), 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         int err;
633
634         if (!attrs[TIPC_NLA_LINK])
635                 return -EINVAL;
636
637         err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
638                                NULL, NULL);
639         if (err)
640                 return err;
641
642         link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
643         link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
644         nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
645                     TIPC_MAX_LINK_NAME);
646
647         return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
648                             &link_info, sizeof(link_info));
649 }
650
651 static int __tipc_add_link_prop(struct sk_buff *skb,
652                                 struct tipc_nl_compat_msg *msg,
653                                 struct tipc_link_config *lc)
654 {
655         switch (msg->cmd) {
656         case TIPC_CMD_SET_LINK_PRI:
657                 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
658         case TIPC_CMD_SET_LINK_TOL:
659                 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
660         case TIPC_CMD_SET_LINK_WINDOW:
661                 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
662         }
663
664         return -EINVAL;
665 }
666
667 static int tipc_nl_compat_media_set(struct sk_buff *skb,
668                                     struct tipc_nl_compat_msg *msg)
669 {
670         struct nlattr *prop;
671         struct nlattr *media;
672         struct tipc_link_config *lc;
673         int len;
674
675         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
676
677         media = nla_nest_start(skb, TIPC_NLA_MEDIA);
678         if (!media)
679                 return -EMSGSIZE;
680
681         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
682         if (!string_is_valid(lc->name, len))
683                 return -EINVAL;
684
685         if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
686                 return -EMSGSIZE;
687
688         prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
689         if (!prop)
690                 return -EMSGSIZE;
691
692         __tipc_add_link_prop(skb, msg, lc);
693         nla_nest_end(skb, prop);
694         nla_nest_end(skb, media);
695
696         return 0;
697 }
698
699 static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
700                                      struct tipc_nl_compat_msg *msg)
701 {
702         struct nlattr *prop;
703         struct nlattr *bearer;
704         struct tipc_link_config *lc;
705         int len;
706
707         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
708
709         bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
710         if (!bearer)
711                 return -EMSGSIZE;
712
713         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
714         if (!string_is_valid(lc->name, len))
715                 return -EINVAL;
716
717         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
718                 return -EMSGSIZE;
719
720         prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
721         if (!prop)
722                 return -EMSGSIZE;
723
724         __tipc_add_link_prop(skb, msg, lc);
725         nla_nest_end(skb, prop);
726         nla_nest_end(skb, bearer);
727
728         return 0;
729 }
730
731 static int __tipc_nl_compat_link_set(struct sk_buff *skb,
732                                      struct tipc_nl_compat_msg *msg)
733 {
734         struct nlattr *prop;
735         struct nlattr *link;
736         struct tipc_link_config *lc;
737
738         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
739
740         link = nla_nest_start(skb, TIPC_NLA_LINK);
741         if (!link)
742                 return -EMSGSIZE;
743
744         if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
745                 return -EMSGSIZE;
746
747         prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
748         if (!prop)
749                 return -EMSGSIZE;
750
751         __tipc_add_link_prop(skb, msg, lc);
752         nla_nest_end(skb, prop);
753         nla_nest_end(skb, link);
754
755         return 0;
756 }
757
758 static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
759                                    struct sk_buff *skb,
760                                    struct tipc_nl_compat_msg *msg)
761 {
762         struct tipc_link_config *lc;
763         struct tipc_bearer *bearer;
764         struct tipc_media *media;
765         int len;
766
767         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
768
769         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
770         if (!string_is_valid(lc->name, len))
771                 return -EINVAL;
772
773         media = tipc_media_find(lc->name);
774         if (media) {
775                 cmd->doit = &__tipc_nl_media_set;
776                 return tipc_nl_compat_media_set(skb, msg);
777         }
778
779         bearer = tipc_bearer_find(msg->net, lc->name);
780         if (bearer) {
781                 cmd->doit = &__tipc_nl_bearer_set;
782                 return tipc_nl_compat_bearer_set(skb, msg);
783         }
784
785         return __tipc_nl_compat_link_set(skb, msg);
786 }
787
788 static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
789                                            struct sk_buff *skb,
790                                            struct tipc_nl_compat_msg *msg)
791 {
792         char *name;
793         struct nlattr *link;
794         int len;
795
796         name = (char *)TLV_DATA(msg->req);
797
798         link = nla_nest_start(skb, TIPC_NLA_LINK);
799         if (!link)
800                 return -EMSGSIZE;
801
802         len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
803         if (!string_is_valid(name, len))
804                 return -EINVAL;
805
806         if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
807                 return -EMSGSIZE;
808
809         nla_nest_end(skb, link);
810
811         return 0;
812 }
813
814 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
815 {
816         int i;
817         u32 depth;
818         struct tipc_name_table_query *ntq;
819         static const char * const header[] = {
820                 "Type       ",
821                 "Lower      Upper      ",
822                 "Port Identity              ",
823                 "Publication Scope"
824         };
825
826         ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
827         if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
828                 return -EINVAL;
829
830         depth = ntohl(ntq->depth);
831
832         if (depth > 4)
833                 depth = 4;
834         for (i = 0; i < depth; i++)
835                 tipc_tlv_sprintf(msg->rep, header[i]);
836         tipc_tlv_sprintf(msg->rep, "\n");
837
838         return 0;
839 }
840
841 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
842                                           struct nlattr **attrs)
843 {
844         char port_str[27];
845         struct tipc_name_table_query *ntq;
846         struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
847         struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
848         u32 node, depth, type, lowbound, upbound;
849         static const char * const scope_str[] = {"", " zone", " cluster",
850                                                  " node"};
851         int err;
852
853         if (!attrs[TIPC_NLA_NAME_TABLE])
854                 return -EINVAL;
855
856         err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
857                                attrs[TIPC_NLA_NAME_TABLE], NULL, NULL);
858         if (err)
859                 return err;
860
861         if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
862                 return -EINVAL;
863
864         err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
865                                nt[TIPC_NLA_NAME_TABLE_PUBL], NULL, NULL);
866         if (err)
867                 return err;
868
869         ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
870
871         depth = ntohl(ntq->depth);
872         type = ntohl(ntq->type);
873         lowbound = ntohl(ntq->lowbound);
874         upbound = ntohl(ntq->upbound);
875
876         if (!(depth & TIPC_NTQ_ALLTYPES) &&
877             (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
878                 return 0;
879         if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
880                 return 0;
881         if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
882                 return 0;
883
884         tipc_tlv_sprintf(msg->rep, "%-10u ",
885                          nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
886
887         if (depth == 1)
888                 goto out;
889
890         tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
891                          nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
892                          nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
893
894         if (depth == 2)
895                 goto out;
896
897         node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
898         sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
899                 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
900         tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
901
902         if (depth == 3)
903                 goto out;
904
905         tipc_tlv_sprintf(msg->rep, "%-10u %s",
906                          nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
907                          scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
908 out:
909         tipc_tlv_sprintf(msg->rep, "\n");
910
911         return 0;
912 }
913
914 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
915                                       struct nlattr **attrs)
916 {
917         u32 type, lower, upper;
918         struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
919         int err;
920
921         if (!attrs[TIPC_NLA_PUBL])
922                 return -EINVAL;
923
924         err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
925                                NULL, NULL);
926         if (err)
927                 return err;
928
929         type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
930         lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
931         upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
932
933         if (lower == upper)
934                 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
935         else
936                 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
937
938         return 0;
939 }
940
941 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
942 {
943         int err;
944         void *hdr;
945         struct nlattr *nest;
946         struct sk_buff *args;
947         struct tipc_nl_compat_cmd_dump dump;
948
949         args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
950         if (!args)
951                 return -ENOMEM;
952
953         hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
954                           TIPC_NL_PUBL_GET);
955         if (!hdr) {
956                 kfree_skb(args);
957                 return -EMSGSIZE;
958         }
959
960         nest = nla_nest_start(args, TIPC_NLA_SOCK);
961         if (!nest) {
962                 kfree_skb(args);
963                 return -EMSGSIZE;
964         }
965
966         if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
967                 kfree_skb(args);
968                 return -EMSGSIZE;
969         }
970
971         nla_nest_end(args, nest);
972         genlmsg_end(args, hdr);
973
974         dump.dumpit = tipc_nl_publ_dump;
975         dump.format = __tipc_nl_compat_publ_dump;
976
977         err = __tipc_nl_compat_dumpit(&dump, msg, args);
978
979         kfree_skb(args);
980
981         return err;
982 }
983
984 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
985                                   struct nlattr **attrs)
986 {
987         int err;
988         u32 sock_ref;
989         struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
990
991         if (!attrs[TIPC_NLA_SOCK])
992                 return -EINVAL;
993
994         err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
995                                NULL, NULL);
996         if (err)
997                 return err;
998
999         sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1000         tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1001
1002         if (sock[TIPC_NLA_SOCK_CON]) {
1003                 u32 node;
1004                 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1005
1006                 err = nla_parse_nested(con, TIPC_NLA_CON_MAX,
1007                                        sock[TIPC_NLA_SOCK_CON], NULL, NULL);
1008
1009                 if (err)
1010                         return err;
1011
1012                 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1013                 tipc_tlv_sprintf(msg->rep, "  connected to <%u.%u.%u:%u>",
1014                                  tipc_zone(node),
1015                                  tipc_cluster(node),
1016                                  tipc_node(node),
1017                                  nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1018
1019                 if (con[TIPC_NLA_CON_FLAG])
1020                         tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1021                                          nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1022                                          nla_get_u32(con[TIPC_NLA_CON_INST]));
1023                 else
1024                         tipc_tlv_sprintf(msg->rep, "\n");
1025         } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1026                 tipc_tlv_sprintf(msg->rep, " bound to");
1027
1028                 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1029                 if (err)
1030                         return err;
1031         }
1032         tipc_tlv_sprintf(msg->rep, "\n");
1033
1034         return 0;
1035 }
1036
1037 static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1038                                      struct nlattr **attrs)
1039 {
1040         struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
1041         int err;
1042
1043         if (!attrs[TIPC_NLA_MEDIA])
1044                 return -EINVAL;
1045
1046         err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX,
1047                                attrs[TIPC_NLA_MEDIA], NULL, NULL);
1048         if (err)
1049                 return err;
1050
1051         return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1052                             nla_data(media[TIPC_NLA_MEDIA_NAME]),
1053                             nla_len(media[TIPC_NLA_MEDIA_NAME]));
1054 }
1055
1056 static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1057                                     struct nlattr **attrs)
1058 {
1059         struct tipc_node_info node_info;
1060         struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
1061         int err;
1062
1063         if (!attrs[TIPC_NLA_NODE])
1064                 return -EINVAL;
1065
1066         err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1067                                NULL, NULL);
1068         if (err)
1069                 return err;
1070
1071         node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1072         node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1073
1074         return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1075                             sizeof(node_info));
1076 }
1077
1078 static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1079                                   struct sk_buff *skb,
1080                                   struct tipc_nl_compat_msg *msg)
1081 {
1082         u32 val;
1083         struct nlattr *net;
1084
1085         val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1086
1087         net = nla_nest_start(skb, TIPC_NLA_NET);
1088         if (!net)
1089                 return -EMSGSIZE;
1090
1091         if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1092                 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1093                         return -EMSGSIZE;
1094         } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1095                 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1096                         return -EMSGSIZE;
1097         }
1098         nla_nest_end(skb, net);
1099
1100         return 0;
1101 }
1102
1103 static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1104                                    struct nlattr **attrs)
1105 {
1106         __be32 id;
1107         struct nlattr *net[TIPC_NLA_NET_MAX + 1];
1108         int err;
1109
1110         if (!attrs[TIPC_NLA_NET])
1111                 return -EINVAL;
1112
1113         err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1114                                NULL, NULL);
1115         if (err)
1116                 return err;
1117
1118         id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1119
1120         return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1121 }
1122
1123 static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1124 {
1125         msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1126         if (!msg->rep)
1127                 return -ENOMEM;
1128
1129         tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1130         tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1131
1132         return 0;
1133 }
1134
1135 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1136 {
1137         struct tipc_nl_compat_cmd_dump dump;
1138         struct tipc_nl_compat_cmd_doit doit;
1139
1140         memset(&dump, 0, sizeof(dump));
1141         memset(&doit, 0, sizeof(doit));
1142
1143         switch (msg->cmd) {
1144         case TIPC_CMD_NOOP:
1145                 msg->rep = tipc_tlv_alloc(0);
1146                 if (!msg->rep)
1147                         return -ENOMEM;
1148                 return 0;
1149         case TIPC_CMD_GET_BEARER_NAMES:
1150                 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1151                 dump.dumpit = tipc_nl_bearer_dump;
1152                 dump.format = tipc_nl_compat_bearer_dump;
1153                 return tipc_nl_compat_dumpit(&dump, msg);
1154         case TIPC_CMD_ENABLE_BEARER:
1155                 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1156                 doit.doit = __tipc_nl_bearer_enable;
1157                 doit.transcode = tipc_nl_compat_bearer_enable;
1158                 return tipc_nl_compat_doit(&doit, msg);
1159         case TIPC_CMD_DISABLE_BEARER:
1160                 msg->req_type = TIPC_TLV_BEARER_NAME;
1161                 doit.doit = __tipc_nl_bearer_disable;
1162                 doit.transcode = tipc_nl_compat_bearer_disable;
1163                 return tipc_nl_compat_doit(&doit, msg);
1164         case TIPC_CMD_SHOW_LINK_STATS:
1165                 msg->req_type = TIPC_TLV_LINK_NAME;
1166                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1167                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1168                 dump.dumpit = tipc_nl_node_dump_link;
1169                 dump.format = tipc_nl_compat_link_stat_dump;
1170                 return tipc_nl_compat_dumpit(&dump, msg);
1171         case TIPC_CMD_GET_LINKS:
1172                 msg->req_type = TIPC_TLV_NET_ADDR;
1173                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1174                 dump.dumpit = tipc_nl_node_dump_link;
1175                 dump.format = tipc_nl_compat_link_dump;
1176                 return tipc_nl_compat_dumpit(&dump, msg);
1177         case TIPC_CMD_SET_LINK_TOL:
1178         case TIPC_CMD_SET_LINK_PRI:
1179         case TIPC_CMD_SET_LINK_WINDOW:
1180                 msg->req_type =  TIPC_TLV_LINK_CONFIG;
1181                 doit.doit = tipc_nl_node_set_link;
1182                 doit.transcode = tipc_nl_compat_link_set;
1183                 return tipc_nl_compat_doit(&doit, msg);
1184         case TIPC_CMD_RESET_LINK_STATS:
1185                 msg->req_type = TIPC_TLV_LINK_NAME;
1186                 doit.doit = tipc_nl_node_reset_link_stats;
1187                 doit.transcode = tipc_nl_compat_link_reset_stats;
1188                 return tipc_nl_compat_doit(&doit, msg);
1189         case TIPC_CMD_SHOW_NAME_TABLE:
1190                 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1191                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1192                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1193                 dump.header = tipc_nl_compat_name_table_dump_header;
1194                 dump.dumpit = tipc_nl_name_table_dump;
1195                 dump.format = tipc_nl_compat_name_table_dump;
1196                 return tipc_nl_compat_dumpit(&dump, msg);
1197         case TIPC_CMD_SHOW_PORTS:
1198                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1199                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1200                 dump.dumpit = tipc_nl_sk_dump;
1201                 dump.format = tipc_nl_compat_sk_dump;
1202                 return tipc_nl_compat_dumpit(&dump, msg);
1203         case TIPC_CMD_GET_MEDIA_NAMES:
1204                 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1205                 dump.dumpit = tipc_nl_media_dump;
1206                 dump.format = tipc_nl_compat_media_dump;
1207                 return tipc_nl_compat_dumpit(&dump, msg);
1208         case TIPC_CMD_GET_NODES:
1209                 msg->rep_size = ULTRA_STRING_MAX_LEN;
1210                 dump.dumpit = tipc_nl_node_dump;
1211                 dump.format = tipc_nl_compat_node_dump;
1212                 return tipc_nl_compat_dumpit(&dump, msg);
1213         case TIPC_CMD_SET_NODE_ADDR:
1214                 msg->req_type = TIPC_TLV_NET_ADDR;
1215                 doit.doit = __tipc_nl_net_set;
1216                 doit.transcode = tipc_nl_compat_net_set;
1217                 return tipc_nl_compat_doit(&doit, msg);
1218         case TIPC_CMD_SET_NETID:
1219                 msg->req_type = TIPC_TLV_UNSIGNED;
1220                 doit.doit = __tipc_nl_net_set;
1221                 doit.transcode = tipc_nl_compat_net_set;
1222                 return tipc_nl_compat_doit(&doit, msg);
1223         case TIPC_CMD_GET_NETID:
1224                 msg->rep_size = sizeof(u32);
1225                 dump.dumpit = tipc_nl_net_dump;
1226                 dump.format = tipc_nl_compat_net_dump;
1227                 return tipc_nl_compat_dumpit(&dump, msg);
1228         case TIPC_CMD_SHOW_STATS:
1229                 return tipc_cmd_show_stats_compat(msg);
1230         }
1231
1232         return -EOPNOTSUPP;
1233 }
1234
1235 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1236 {
1237         int err;
1238         int len;
1239         struct tipc_nl_compat_msg msg;
1240         struct nlmsghdr *req_nlh;
1241         struct nlmsghdr *rep_nlh;
1242         struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1243
1244         memset(&msg, 0, sizeof(msg));
1245
1246         req_nlh = (struct nlmsghdr *)skb->data;
1247         msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1248         msg.cmd = req_userhdr->cmd;
1249         msg.net = genl_info_net(info);
1250         msg.dst_sk = skb->sk;
1251
1252         if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1253                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1254                 err = -EACCES;
1255                 goto send;
1256         }
1257
1258         len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1259         if (!len || !TLV_OK(msg.req, len)) {
1260                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1261                 err = -EOPNOTSUPP;
1262                 goto send;
1263         }
1264
1265         err = tipc_nl_compat_handle(&msg);
1266         if ((err == -EOPNOTSUPP) || (err == -EPERM))
1267                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1268         else if (err == -EINVAL)
1269                 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1270 send:
1271         if (!msg.rep)
1272                 return err;
1273
1274         len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1275         skb_push(msg.rep, len);
1276         rep_nlh = nlmsg_hdr(msg.rep);
1277         memcpy(rep_nlh, info->nlhdr, len);
1278         rep_nlh->nlmsg_len = msg.rep->len;
1279         genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
1280
1281         return err;
1282 }
1283
1284 static const struct genl_ops tipc_genl_compat_ops[] = {
1285         {
1286                 .cmd            = TIPC_GENL_CMD,
1287                 .doit           = tipc_nl_compat_recv,
1288         },
1289 };
1290
1291 static struct genl_family tipc_genl_compat_family __ro_after_init = {
1292         .name           = TIPC_GENL_NAME,
1293         .version        = TIPC_GENL_VERSION,
1294         .hdrsize        = TIPC_GENL_HDRLEN,
1295         .maxattr        = 0,
1296         .netnsok        = true,
1297         .module         = THIS_MODULE,
1298         .ops            = tipc_genl_compat_ops,
1299         .n_ops          = ARRAY_SIZE(tipc_genl_compat_ops),
1300 };
1301
1302 int __init tipc_netlink_compat_start(void)
1303 {
1304         int res;
1305
1306         res = genl_register_family(&tipc_genl_compat_family);
1307         if (res) {
1308                 pr_err("Failed to register legacy compat interface\n");
1309                 return res;
1310         }
1311
1312         return 0;
1313 }
1314
1315 void tipc_netlink_compat_stop(void)
1316 {
1317         genl_unregister_family(&tipc_genl_compat_family);
1318 }