tipc: convert legacy nl socket dump to nl compat
[pandora-kernel.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 "config.h"
36 #include "bearer.h"
37 #include "link.h"
38 #include "name_table.h"
39 #include "socket.h"
40 #include <net/genetlink.h>
41 #include <linux/tipc_config.h>
42
43 /* The legacy API had an artificial message length limit called
44  * ULTRA_STRING_MAX_LEN.
45  */
46 #define ULTRA_STRING_MAX_LEN 32768
47
48 #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
49
50 #define REPLY_TRUNCATED "<truncated>\n"
51
52 struct tipc_nl_compat_msg {
53         u16 cmd;
54         int rep_type;
55         int rep_size;
56         int req_type;
57         struct sk_buff *rep;
58         struct tlv_desc *req;
59         struct sock *dst_sk;
60 };
61
62 struct tipc_nl_compat_cmd_dump {
63         int (*header)(struct tipc_nl_compat_msg *);
64         int (*dumpit)(struct sk_buff *, struct netlink_callback *);
65         int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
66 };
67
68 struct tipc_nl_compat_cmd_doit {
69         int (*doit)(struct sk_buff *skb, struct genl_info *info);
70         int (*transcode)(struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
71 };
72
73 static int tipc_skb_tailroom(struct sk_buff *skb)
74 {
75         int tailroom;
76         int limit;
77
78         tailroom = skb_tailroom(skb);
79         limit = TIPC_SKB_MAX - skb->len;
80
81         if (tailroom < limit)
82                 return tailroom;
83
84         return limit;
85 }
86
87 static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
88 {
89         struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
90
91         if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
92                 return -EMSGSIZE;
93
94         skb_put(skb, TLV_SPACE(len));
95         tlv->tlv_type = htons(type);
96         tlv->tlv_len = htons(TLV_LENGTH(len));
97         if (len && data)
98                 memcpy(TLV_DATA(tlv), data, len);
99
100         return 0;
101 }
102
103 static void tipc_tlv_init(struct sk_buff *skb, u16 type)
104 {
105         struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
106
107         TLV_SET_LEN(tlv, 0);
108         TLV_SET_TYPE(tlv, type);
109         skb_put(skb, sizeof(struct tlv_desc));
110 }
111
112 static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
113 {
114         int n;
115         u16 len;
116         u32 rem;
117         char *buf;
118         struct tlv_desc *tlv;
119         va_list args;
120
121         rem = tipc_skb_tailroom(skb);
122
123         tlv = (struct tlv_desc *)skb->data;
124         len = TLV_GET_LEN(tlv);
125         buf = TLV_DATA(tlv) + len;
126
127         va_start(args, fmt);
128         n = vscnprintf(buf, rem, fmt, args);
129         va_end(args);
130
131         TLV_SET_LEN(tlv, n + len);
132         skb_put(skb, n);
133
134         return n;
135 }
136
137 static struct sk_buff *tipc_tlv_alloc(int size)
138 {
139         int hdr_len;
140         struct sk_buff *buf;
141
142         size = TLV_SPACE(size);
143         hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
144
145         buf = alloc_skb(hdr_len + size, GFP_KERNEL);
146         if (!buf)
147                 return NULL;
148
149         skb_reserve(buf, hdr_len);
150
151         return buf;
152 }
153
154 static struct sk_buff *tipc_get_err_tlv(char *str)
155 {
156         int str_len = strlen(str) + 1;
157         struct sk_buff *buf;
158
159         buf = tipc_tlv_alloc(TLV_SPACE(str_len));
160         if (buf)
161                 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
162
163         return buf;
164 }
165
166 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
167                                    struct tipc_nl_compat_msg *msg,
168                                    struct sk_buff *arg)
169 {
170         int len = 0;
171         int err;
172         struct sk_buff *buf;
173         struct nlmsghdr *nlmsg;
174         struct netlink_callback cb;
175
176         memset(&cb, 0, sizeof(cb));
177         cb.nlh = (struct nlmsghdr *)arg->data;
178         cb.skb = arg;
179
180         buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
181         if (!buf)
182                 return -ENOMEM;
183
184         buf->sk = msg->dst_sk;
185
186         do {
187                 int rem;
188
189                 len = (*cmd->dumpit)(buf, &cb);
190
191                 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
192                         struct nlattr **attrs;
193
194                         err = tipc_nlmsg_parse(nlmsg, &attrs);
195                         if (err)
196                                 goto err_out;
197
198                         err = (*cmd->format)(msg, attrs);
199                         if (err)
200                                 goto err_out;
201
202                         if (tipc_skb_tailroom(msg->rep) <= 1) {
203                                 err = -EMSGSIZE;
204                                 goto err_out;
205                         }
206                 }
207
208                 skb_reset_tail_pointer(buf);
209                 buf->len = 0;
210
211         } while (len);
212
213         err = 0;
214
215 err_out:
216         kfree_skb(buf);
217
218         if (err == -EMSGSIZE) {
219                 /* The legacy API only considered messages filling
220                  * "ULTRA_STRING_MAX_LEN" to be truncated.
221                  */
222                 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
223                         char *tail = skb_tail_pointer(msg->rep);
224
225                         if (*tail != '\0')
226                                 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
227                                         REPLY_TRUNCATED);
228                 }
229
230                 return 0;
231         }
232
233         return err;
234 }
235
236 static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
237                                  struct tipc_nl_compat_msg *msg)
238 {
239         int err;
240         struct sk_buff *arg;
241
242         if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
243                 return -EINVAL;
244
245         msg->rep = tipc_tlv_alloc(msg->rep_size);
246         if (!msg->rep)
247                 return -ENOMEM;
248
249         if (msg->rep_type)
250                 tipc_tlv_init(msg->rep, msg->rep_type);
251
252         if (cmd->header)
253                 (*cmd->header)(msg);
254
255         arg = nlmsg_new(0, GFP_KERNEL);
256         if (!arg) {
257                 kfree_skb(msg->rep);
258                 return -ENOMEM;
259         }
260
261         err = __tipc_nl_compat_dumpit(cmd, msg, arg);
262         if (err)
263                 kfree_skb(msg->rep);
264
265         kfree_skb(arg);
266
267         return err;
268 }
269
270 static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
271                                  struct tipc_nl_compat_msg *msg)
272 {
273         int err;
274         struct sk_buff *doit_buf;
275         struct sk_buff *trans_buf;
276         struct nlattr **attrbuf;
277         struct genl_info info;
278
279         trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
280         if (!trans_buf)
281                 return -ENOMEM;
282
283         err = (*cmd->transcode)(trans_buf, msg);
284         if (err)
285                 goto trans_out;
286
287         attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
288                         sizeof(struct nlattr *), GFP_KERNEL);
289         if (!attrbuf) {
290                 err = -ENOMEM;
291                 goto trans_out;
292         }
293
294         err = nla_parse(attrbuf, tipc_genl_family.maxattr,
295                         (const struct nlattr *)trans_buf->data,
296                         trans_buf->len, NULL);
297         if (err)
298                 goto parse_out;
299
300         doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
301         if (!doit_buf) {
302                 err = -ENOMEM;
303                 goto parse_out;
304         }
305
306         doit_buf->sk = msg->dst_sk;
307
308         memset(&info, 0, sizeof(info));
309         info.attrs = attrbuf;
310
311         err = (*cmd->doit)(doit_buf, &info);
312
313         kfree_skb(doit_buf);
314 parse_out:
315         kfree(attrbuf);
316 trans_out:
317         kfree_skb(trans_buf);
318
319         return err;
320 }
321
322 static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
323                                struct tipc_nl_compat_msg *msg)
324 {
325         int err;
326
327         if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
328                 return -EINVAL;
329
330         err = __tipc_nl_compat_doit(cmd, msg);
331         if (err)
332                 return err;
333
334         /* The legacy API considered an empty message a success message */
335         msg->rep = tipc_tlv_alloc(0);
336         if (!msg->rep)
337                 return -ENOMEM;
338
339         return 0;
340 }
341
342 static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
343                                       struct nlattr **attrs)
344 {
345         struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
346
347         nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
348                          NULL);
349
350         return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
351                             nla_data(bearer[TIPC_NLA_BEARER_NAME]),
352                             nla_len(bearer[TIPC_NLA_BEARER_NAME]));
353 }
354
355 static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
356                                         struct tipc_nl_compat_msg *msg)
357 {
358         struct nlattr *prop;
359         struct nlattr *bearer;
360         struct tipc_bearer_config *b;
361
362         b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
363
364         bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
365         if (!bearer)
366                 return -EMSGSIZE;
367
368         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
369                 return -EMSGSIZE;
370
371         if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
372                 return -EMSGSIZE;
373
374         if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
375                 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
376                 if (!prop)
377                         return -EMSGSIZE;
378                 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
379                         return -EMSGSIZE;
380                 nla_nest_end(skb, prop);
381         }
382         nla_nest_end(skb, bearer);
383
384         return 0;
385 }
386
387 static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
388                                          struct tipc_nl_compat_msg *msg)
389 {
390         char *name;
391         struct nlattr *bearer;
392
393         name = (char *)TLV_DATA(msg->req);
394
395         bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
396         if (!bearer)
397                 return -EMSGSIZE;
398
399         if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
400                 return -EMSGSIZE;
401
402         nla_nest_end(skb, bearer);
403
404         return 0;
405 }
406
407 static inline u32 perc(u32 count, u32 total)
408 {
409         return (count * 100 + (total / 2)) / total;
410 }
411
412 static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
413                                 struct nlattr *prop[], struct nlattr *stats[])
414 {
415         tipc_tlv_sprintf(msg->rep, "  Window:%u packets\n",
416                          nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
417
418         tipc_tlv_sprintf(msg->rep,
419                          "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
420                          nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
421                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
422                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
423                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
424                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
425
426         tipc_tlv_sprintf(msg->rep,
427                          "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
428                          nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
429                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
430                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
431                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
432                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
433
434         tipc_tlv_sprintf(msg->rep, "  RX naks:%u defs:%u dups:%u\n",
435                          nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
436                          nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
437                          nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
438
439         tipc_tlv_sprintf(msg->rep, "  TX naks:%u acks:%u dups:%u\n",
440                          nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
441                          nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
442                          nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
443
444         tipc_tlv_sprintf(msg->rep,
445                          "  Congestion link:%u  Send queue max:%u avg:%u",
446                          nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
447                          nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
448                          nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
449 }
450
451 static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
452                                          struct nlattr **attrs)
453 {
454         char *name;
455         struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
456         struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
457         struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
458
459         nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
460
461         nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
462                          NULL);
463
464         nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
465                          NULL);
466
467         name = (char *)TLV_DATA(msg->req);
468         if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
469                 return 0;
470
471         tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
472                          nla_data(link[TIPC_NLA_LINK_NAME]));
473
474         if (link[TIPC_NLA_LINK_BROADCAST]) {
475                 __fill_bc_link_stat(msg, prop, stats);
476                 return 0;
477         }
478
479         if (link[TIPC_NLA_LINK_ACTIVE])
480                 tipc_tlv_sprintf(msg->rep, "  ACTIVE");
481         else if (link[TIPC_NLA_LINK_UP])
482                 tipc_tlv_sprintf(msg->rep, "  STANDBY");
483         else
484                 tipc_tlv_sprintf(msg->rep, "  DEFUNCT");
485
486         tipc_tlv_sprintf(msg->rep, "  MTU:%u  Priority:%u",
487                          nla_get_u32(link[TIPC_NLA_LINK_MTU]),
488                          nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
489
490         tipc_tlv_sprintf(msg->rep, "  Tolerance:%u ms  Window:%u packets\n",
491                          nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
492                          nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
493
494         tipc_tlv_sprintf(msg->rep,
495                          "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
496                          nla_get_u32(link[TIPC_NLA_LINK_RX]) -
497                          nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
498                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
499                          nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
500                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
501                          nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
502
503         tipc_tlv_sprintf(msg->rep,
504                          "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
505                          nla_get_u32(link[TIPC_NLA_LINK_TX]) -
506                          nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
507                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
508                          nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
509                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
510                          nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
511
512         tipc_tlv_sprintf(msg->rep,
513                          "  TX profile sample:%u packets  average:%u octets\n",
514                          nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
515                          nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
516                          nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
517
518         tipc_tlv_sprintf(msg->rep,
519                          "  0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
520                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
521                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
522                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
523                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
524                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
525                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
526                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
527                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
528
529         tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
530                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
531                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
532                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
533                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
534                          perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
535                               nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
536
537         tipc_tlv_sprintf(msg->rep,
538                          "  RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
539                          nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
540                          nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
541                          nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
542                          nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
543                          nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
544
545         tipc_tlv_sprintf(msg->rep,
546                          "  TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
547                          nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
548                          nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
549                          nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
550                          nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
551                          nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
552
553         tipc_tlv_sprintf(msg->rep,
554                          "  Congestion link:%u  Send queue max:%u avg:%u",
555                          nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
556                          nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
557                          nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
558
559         return 0;
560 }
561
562 static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
563                                     struct nlattr **attrs)
564 {
565         struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
566         struct tipc_link_info link_info;
567
568         nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
569
570         link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
571         link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
572         strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
573
574         return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
575                             &link_info, sizeof(link_info));
576 }
577
578 static int tipc_nl_compat_link_set(struct sk_buff *skb,
579                                    struct tipc_nl_compat_msg *msg)
580 {
581         struct nlattr *link;
582         struct nlattr *prop;
583         struct tipc_link_config *lc;
584
585         lc = (struct tipc_link_config *)TLV_DATA(msg->req);
586
587         link = nla_nest_start(skb, TIPC_NLA_LINK);
588         if (!link)
589                 return -EMSGSIZE;
590
591         if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
592                 return -EMSGSIZE;
593
594         prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
595         if (!prop)
596                 return -EMSGSIZE;
597
598         if (msg->cmd == TIPC_CMD_SET_LINK_PRI) {
599                 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)))
600                         return -EMSGSIZE;
601         } else if (msg->cmd == TIPC_CMD_SET_LINK_TOL) {
602                 if (nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)))
603                         return -EMSGSIZE;
604         } else if (msg->cmd == TIPC_CMD_SET_LINK_WINDOW) {
605                 if (nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)))
606                         return -EMSGSIZE;
607         }
608
609         nla_nest_end(skb, prop);
610         nla_nest_end(skb, link);
611
612         return 0;
613 }
614
615 static int tipc_nl_compat_link_reset_stats(struct sk_buff *skb,
616                                            struct tipc_nl_compat_msg *msg)
617 {
618         char *name;
619         struct nlattr *link;
620
621         name = (char *)TLV_DATA(msg->req);
622
623         link = nla_nest_start(skb, TIPC_NLA_LINK);
624         if (!link)
625                 return -EMSGSIZE;
626
627         if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
628                 return -EMSGSIZE;
629
630         nla_nest_end(skb, link);
631
632         return 0;
633 }
634
635 static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
636 {
637         int i;
638         u32 depth;
639         struct tipc_name_table_query *ntq;
640         static const char * const header[] = {
641                 "Type       ",
642                 "Lower      Upper      ",
643                 "Port Identity              ",
644                 "Publication Scope"
645         };
646
647         ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
648
649         depth = ntohl(ntq->depth);
650
651         if (depth > 4)
652                 depth = 4;
653         for (i = 0; i < depth; i++)
654                 tipc_tlv_sprintf(msg->rep, header[i]);
655         tipc_tlv_sprintf(msg->rep, "\n");
656
657         return 0;
658 }
659
660 static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
661                                           struct nlattr **attrs)
662 {
663         char port_str[27];
664         struct tipc_name_table_query *ntq;
665         struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
666         struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
667         u32 node, depth, type, lowbound, upbound;
668         static const char * const scope_str[] = {"", " zone", " cluster",
669                                                  " node"};
670
671         nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
672                          attrs[TIPC_NLA_NAME_TABLE], NULL);
673
674         nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
675                          NULL);
676
677         ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
678
679         depth = ntohl(ntq->depth);
680         type = ntohl(ntq->type);
681         lowbound = ntohl(ntq->lowbound);
682         upbound = ntohl(ntq->upbound);
683
684         if (!(depth & TIPC_NTQ_ALLTYPES) &&
685             (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
686                 return 0;
687         if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
688                 return 0;
689         if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
690                 return 0;
691
692         tipc_tlv_sprintf(msg->rep, "%-10u ",
693                          nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
694
695         if (depth == 1)
696                 goto out;
697
698         tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
699                          nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
700                          nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
701
702         if (depth == 2)
703                 goto out;
704
705         node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
706         sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
707                 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
708         tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
709
710         if (depth == 3)
711                 goto out;
712
713         tipc_tlv_sprintf(msg->rep, "%-10u %s",
714                          nla_get_u32(publ[TIPC_NLA_PUBL_REF]),
715                          scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
716 out:
717         tipc_tlv_sprintf(msg->rep, "\n");
718
719         return 0;
720 }
721
722 static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
723                                       struct nlattr **attrs)
724 {
725         u32 type, lower, upper;
726         struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
727
728         nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL], NULL);
729
730         type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
731         lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
732         upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
733
734         if (lower == upper)
735                 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
736         else
737                 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
738
739         return 0;
740 }
741
742 static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
743 {
744         int err;
745         void *hdr;
746         struct nlattr *nest;
747         struct sk_buff *args;
748         struct tipc_nl_compat_cmd_dump dump;
749
750         args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
751         if (!args)
752                 return -ENOMEM;
753
754         hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
755                           TIPC_NL_PUBL_GET);
756
757         nest = nla_nest_start(args, TIPC_NLA_SOCK);
758         if (!nest) {
759                 kfree_skb(args);
760                 return -EMSGSIZE;
761         }
762
763         if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
764                 kfree_skb(args);
765                 return -EMSGSIZE;
766         }
767
768         nla_nest_end(args, nest);
769         genlmsg_end(args, hdr);
770
771         dump.dumpit = tipc_nl_publ_dump;
772         dump.format = __tipc_nl_compat_publ_dump;
773
774         err = __tipc_nl_compat_dumpit(&dump, msg, args);
775
776         kfree_skb(args);
777
778         return err;
779 }
780
781 static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
782                                   struct nlattr **attrs)
783 {
784         int err;
785         u32 sock_ref;
786         struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
787
788         nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK], NULL);
789
790         sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
791         tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
792
793         if (sock[TIPC_NLA_SOCK_CON]) {
794                 u32 node;
795                 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
796
797                 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
798                                  NULL);
799
800                 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
801                 tipc_tlv_sprintf(msg->rep, "  connected to <%u.%u.%u:%u>",
802                                  tipc_zone(node),
803                                  tipc_cluster(node),
804                                  tipc_node(node),
805                                  nla_get_u32(con[TIPC_NLA_CON_SOCK]));
806
807                 if (con[TIPC_NLA_CON_FLAG])
808                         tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
809                                          nla_get_u32(con[TIPC_NLA_CON_TYPE]),
810                                          nla_get_u32(con[TIPC_NLA_CON_INST]));
811                 else
812                         tipc_tlv_sprintf(msg->rep, "\n");
813         } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
814                 tipc_tlv_sprintf(msg->rep, " bound to");
815
816                 err = tipc_nl_compat_publ_dump(msg, sock_ref);
817                 if (err)
818                         return err;
819         }
820         tipc_tlv_sprintf(msg->rep, "\n");
821
822         return 0;
823 }
824
825 static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
826 {
827         struct tipc_nl_compat_cmd_dump dump;
828         struct tipc_nl_compat_cmd_doit doit;
829
830         memset(&dump, 0, sizeof(dump));
831         memset(&doit, 0, sizeof(doit));
832
833         switch (msg->cmd) {
834         case TIPC_CMD_GET_BEARER_NAMES:
835                 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
836                 dump.dumpit = tipc_nl_bearer_dump;
837                 dump.format = tipc_nl_compat_bearer_dump;
838                 return tipc_nl_compat_dumpit(&dump, msg);
839         case TIPC_CMD_ENABLE_BEARER:
840                 msg->req_type = TIPC_TLV_BEARER_CONFIG;
841                 doit.doit = tipc_nl_bearer_enable;
842                 doit.transcode = tipc_nl_compat_bearer_enable;
843                 return tipc_nl_compat_doit(&doit, msg);
844         case TIPC_CMD_DISABLE_BEARER:
845                 msg->req_type = TIPC_TLV_BEARER_NAME;
846                 doit.doit = tipc_nl_bearer_disable;
847                 doit.transcode = tipc_nl_compat_bearer_disable;
848                 return tipc_nl_compat_doit(&doit, msg);
849         case TIPC_CMD_SHOW_LINK_STATS:
850                 msg->req_type = TIPC_TLV_LINK_NAME;
851                 msg->rep_size = ULTRA_STRING_MAX_LEN;
852                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
853                 dump.dumpit = tipc_nl_link_dump;
854                 dump.format = tipc_nl_compat_link_stat_dump;
855                 return tipc_nl_compat_dumpit(&dump, msg);
856         case TIPC_CMD_GET_LINKS:
857                 msg->req_type = TIPC_TLV_NET_ADDR;
858                 msg->rep_size = ULTRA_STRING_MAX_LEN;
859                 dump.dumpit = tipc_nl_link_dump;
860                 dump.format = tipc_nl_compat_link_dump;
861                 return tipc_nl_compat_dumpit(&dump, msg);
862         case TIPC_CMD_SET_LINK_TOL:
863         case TIPC_CMD_SET_LINK_PRI:
864         case TIPC_CMD_SET_LINK_WINDOW:
865                 msg->req_type =  TIPC_TLV_LINK_CONFIG;
866                 doit.doit = tipc_nl_link_set;
867                 doit.transcode = tipc_nl_compat_link_set;
868                 return tipc_nl_compat_doit(&doit, msg);
869         case TIPC_CMD_RESET_LINK_STATS:
870                 msg->req_type = TIPC_TLV_LINK_NAME;
871                 doit.doit = tipc_nl_link_reset_stats;
872                 doit.transcode = tipc_nl_compat_link_reset_stats;
873                 return tipc_nl_compat_doit(&doit, msg);
874         case TIPC_CMD_SHOW_NAME_TABLE:
875                 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
876                 msg->rep_size = ULTRA_STRING_MAX_LEN;
877                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
878                 dump.header = tipc_nl_compat_name_table_dump_header;
879                 dump.dumpit = tipc_nl_name_table_dump;
880                 dump.format = tipc_nl_compat_name_table_dump;
881                 return tipc_nl_compat_dumpit(&dump, msg);
882         case TIPC_CMD_SHOW_PORTS:
883                 msg->rep_size = ULTRA_STRING_MAX_LEN;
884                 msg->rep_type = TIPC_TLV_ULTRA_STRING;
885                 dump.dumpit = tipc_nl_sk_dump;
886                 dump.format = tipc_nl_compat_sk_dump;
887                 return tipc_nl_compat_dumpit(&dump, msg);
888         }
889
890         return -EOPNOTSUPP;
891 }
892
893 static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
894 {
895         int err;
896         int len;
897         struct tipc_nl_compat_msg msg;
898         struct nlmsghdr *req_nlh;
899         struct nlmsghdr *rep_nlh;
900         struct tipc_genlmsghdr *req_userhdr = info->userhdr;
901         struct net *net = genl_info_net(info);
902
903         memset(&msg, 0, sizeof(msg));
904
905         req_nlh = (struct nlmsghdr *)skb->data;
906         msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
907         msg.cmd = req_userhdr->cmd;
908         msg.dst_sk = info->dst_sk;
909
910         if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
911                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
912                 err = -EACCES;
913                 goto send;
914         }
915
916         len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
917         if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
918                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
919                 err = -EOPNOTSUPP;
920                 goto send;
921         }
922
923         err = tipc_nl_compat_handle(&msg);
924         if (err == -EOPNOTSUPP)
925                 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
926         else if (err == -EINVAL)
927                 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
928 send:
929         if (!msg.rep)
930                 return err;
931
932         len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
933         skb_push(msg.rep, len);
934         rep_nlh = nlmsg_hdr(msg.rep);
935         memcpy(rep_nlh, info->nlhdr, len);
936         rep_nlh->nlmsg_len = msg.rep->len;
937         genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
938
939         return err;
940 }
941
942 static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
943 {
944         struct net *net = genl_info_net(info);
945         struct sk_buff *rep_buf;
946         struct nlmsghdr *rep_nlh;
947         struct nlmsghdr *req_nlh = info->nlhdr;
948         struct tipc_genlmsghdr *req_userhdr = info->userhdr;
949         int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
950         u16 cmd;
951
952         if ((req_userhdr->cmd & 0xC000) &&
953             (!netlink_net_capable(skb, CAP_NET_ADMIN)))
954                 cmd = TIPC_CMD_NOT_NET_ADMIN;
955         else
956                 cmd = req_userhdr->cmd;
957
958         rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
959                                   nlmsg_data(req_nlh) + GENL_HDRLEN +
960                                   TIPC_GENL_HDRLEN,
961                                   nlmsg_attrlen(req_nlh, GENL_HDRLEN +
962                                   TIPC_GENL_HDRLEN), hdr_space);
963
964         if (rep_buf) {
965                 skb_push(rep_buf, hdr_space);
966                 rep_nlh = nlmsg_hdr(rep_buf);
967                 memcpy(rep_nlh, req_nlh, hdr_space);
968                 rep_nlh->nlmsg_len = rep_buf->len;
969                 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
970         }
971
972         return 0;
973 }
974
975 /* Temporary function to keep functionality throughout the patchset
976  * without having to mess with the global variables and other trickery
977  * of the old API.
978  */
979 static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
980 {
981         struct tipc_genlmsghdr *req = info->userhdr;
982
983         switch (req->cmd) {
984         case TIPC_CMD_GET_BEARER_NAMES:
985         case TIPC_CMD_ENABLE_BEARER:
986         case TIPC_CMD_DISABLE_BEARER:
987         case TIPC_CMD_SHOW_LINK_STATS:
988         case TIPC_CMD_GET_LINKS:
989         case TIPC_CMD_SET_LINK_TOL:
990         case TIPC_CMD_SET_LINK_PRI:
991         case TIPC_CMD_SET_LINK_WINDOW:
992         case TIPC_CMD_RESET_LINK_STATS:
993         case TIPC_CMD_SHOW_NAME_TABLE:
994         case TIPC_CMD_SHOW_PORTS:
995                 return tipc_nl_compat_recv(skb, info);
996         }
997
998         return handle_cmd(skb, info);
999 }
1000
1001 static struct genl_family tipc_genl_compat_family = {
1002         .id             = GENL_ID_GENERATE,
1003         .name           = TIPC_GENL_NAME,
1004         .version        = TIPC_GENL_VERSION,
1005         .hdrsize        = TIPC_GENL_HDRLEN,
1006         .maxattr        = 0,
1007         .netnsok        = true,
1008 };
1009
1010 static struct genl_ops tipc_genl_compat_ops[] = {
1011         {
1012                 .cmd            = TIPC_GENL_CMD,
1013                 .doit           = tipc_nl_compat_tmp_wrap,
1014         },
1015 };
1016
1017 int tipc_netlink_compat_start(void)
1018 {
1019         int res;
1020
1021         res = genl_register_family_with_ops(&tipc_genl_compat_family,
1022                                             tipc_genl_compat_ops);
1023         if (res) {
1024                 pr_err("Failed to register legacy compat interface\n");
1025                 return res;
1026         }
1027
1028         return 0;
1029 }
1030
1031 void tipc_netlink_compat_stop(void)
1032 {
1033         genl_unregister_family(&tipc_genl_compat_family);
1034 }