tipc: convert legacy nl link dump to nl compat
[linux-2.6-block.git] / net / tipc / netlink_compat.c
CommitLineData
bfb3e5dd
RA
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"
d0796d1e 36#include "bearer.h"
f2b3b2d4 37#include "link.h"
bfb3e5dd
RA
38#include <net/genetlink.h>
39#include <linux/tipc_config.h>
40
d0796d1e
RA
41/* The legacy API had an artificial message length limit called
42 * ULTRA_STRING_MAX_LEN.
43 */
44#define ULTRA_STRING_MAX_LEN 32768
45
46#define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
47
48#define REPLY_TRUNCATED "<truncated>\n"
49
50struct tipc_nl_compat_msg {
51 u16 cmd;
f2b3b2d4 52 int rep_type;
d0796d1e 53 int rep_size;
9ab15465 54 int req_type;
d0796d1e
RA
55 struct sk_buff *rep;
56 struct tlv_desc *req;
57 struct sock *dst_sk;
58};
59
60struct tipc_nl_compat_cmd_dump {
61 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
62 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
63};
64
9ab15465
RA
65struct tipc_nl_compat_cmd_doit {
66 int (*doit)(struct sk_buff *skb, struct genl_info *info);
67 int (*transcode)(struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
68};
69
d0796d1e
RA
70static int tipc_skb_tailroom(struct sk_buff *skb)
71{
72 int tailroom;
73 int limit;
74
75 tailroom = skb_tailroom(skb);
76 limit = TIPC_SKB_MAX - skb->len;
77
78 if (tailroom < limit)
79 return tailroom;
80
81 return limit;
82}
83
84static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
85{
86 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
87
88 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
89 return -EMSGSIZE;
90
91 skb_put(skb, TLV_SPACE(len));
92 tlv->tlv_type = htons(type);
93 tlv->tlv_len = htons(TLV_LENGTH(len));
94 if (len && data)
95 memcpy(TLV_DATA(tlv), data, len);
96
97 return 0;
98}
99
f2b3b2d4
RA
100static void tipc_tlv_init(struct sk_buff *skb, u16 type)
101{
102 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
103
104 TLV_SET_LEN(tlv, 0);
105 TLV_SET_TYPE(tlv, type);
106 skb_put(skb, sizeof(struct tlv_desc));
107}
108
109static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
110{
111 int n;
112 u16 len;
113 u32 rem;
114 char *buf;
115 struct tlv_desc *tlv;
116 va_list args;
117
118 rem = tipc_skb_tailroom(skb);
119
120 tlv = (struct tlv_desc *)skb->data;
121 len = TLV_GET_LEN(tlv);
122 buf = TLV_DATA(tlv) + len;
123
124 va_start(args, fmt);
125 n = vscnprintf(buf, rem, fmt, args);
126 va_end(args);
127
128 TLV_SET_LEN(tlv, n + len);
129 skb_put(skb, n);
130
131 return n;
132}
133
d0796d1e
RA
134static struct sk_buff *tipc_tlv_alloc(int size)
135{
136 int hdr_len;
137 struct sk_buff *buf;
138
139 size = TLV_SPACE(size);
140 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
141
142 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
143 if (!buf)
144 return NULL;
145
146 skb_reserve(buf, hdr_len);
147
148 return buf;
149}
150
151static struct sk_buff *tipc_get_err_tlv(char *str)
152{
153 int str_len = strlen(str) + 1;
154 struct sk_buff *buf;
155
156 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
157 if (buf)
158 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
159
160 return buf;
161}
162
163static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
164 struct tipc_nl_compat_msg *msg,
165 struct sk_buff *arg)
166{
167 int len = 0;
168 int err;
169 struct sk_buff *buf;
170 struct nlmsghdr *nlmsg;
171 struct netlink_callback cb;
172
173 memset(&cb, 0, sizeof(cb));
174 cb.nlh = (struct nlmsghdr *)arg->data;
175 cb.skb = arg;
176
177 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
178 if (!buf)
179 return -ENOMEM;
180
181 buf->sk = msg->dst_sk;
182
183 do {
184 int rem;
185
186 len = (*cmd->dumpit)(buf, &cb);
187
188 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
189 struct nlattr **attrs;
190
191 err = tipc_nlmsg_parse(nlmsg, &attrs);
192 if (err)
193 goto err_out;
194
195 err = (*cmd->format)(msg, attrs);
196 if (err)
197 goto err_out;
198
199 if (tipc_skb_tailroom(msg->rep) <= 1) {
200 err = -EMSGSIZE;
201 goto err_out;
202 }
203 }
204
205 skb_reset_tail_pointer(buf);
206 buf->len = 0;
207
208 } while (len);
209
210 err = 0;
211
212err_out:
213 kfree_skb(buf);
214
215 if (err == -EMSGSIZE) {
216 /* The legacy API only considered messages filling
217 * "ULTRA_STRING_MAX_LEN" to be truncated.
218 */
219 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
220 char *tail = skb_tail_pointer(msg->rep);
221
222 if (*tail != '\0')
223 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
224 REPLY_TRUNCATED);
225 }
226
227 return 0;
228 }
229
230 return err;
231}
232
233static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
234 struct tipc_nl_compat_msg *msg)
235{
236 int err;
237 struct sk_buff *arg;
238
f2b3b2d4
RA
239 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
240 return -EINVAL;
241
d0796d1e
RA
242 msg->rep = tipc_tlv_alloc(msg->rep_size);
243 if (!msg->rep)
244 return -ENOMEM;
245
f2b3b2d4
RA
246 if (msg->rep_type)
247 tipc_tlv_init(msg->rep, msg->rep_type);
248
d0796d1e
RA
249 arg = nlmsg_new(0, GFP_KERNEL);
250 if (!arg) {
251 kfree_skb(msg->rep);
252 return -ENOMEM;
253 }
254
255 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
256 if (err)
257 kfree_skb(msg->rep);
258
259 kfree_skb(arg);
260
261 return err;
262}
263
9ab15465
RA
264static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
265 struct tipc_nl_compat_msg *msg)
266{
267 int err;
268 struct sk_buff *doit_buf;
269 struct sk_buff *trans_buf;
270 struct nlattr **attrbuf;
271 struct genl_info info;
272
273 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
274 if (!trans_buf)
275 return -ENOMEM;
276
277 err = (*cmd->transcode)(trans_buf, msg);
278 if (err)
279 goto trans_out;
280
281 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
282 sizeof(struct nlattr *), GFP_KERNEL);
283 if (!attrbuf) {
284 err = -ENOMEM;
285 goto trans_out;
286 }
287
288 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
289 (const struct nlattr *)trans_buf->data,
290 trans_buf->len, NULL);
291 if (err)
292 goto parse_out;
293
294 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
295 if (!doit_buf) {
296 err = -ENOMEM;
297 goto parse_out;
298 }
299
300 doit_buf->sk = msg->dst_sk;
301
302 memset(&info, 0, sizeof(info));
303 info.attrs = attrbuf;
304
305 err = (*cmd->doit)(doit_buf, &info);
306
307 kfree_skb(doit_buf);
308parse_out:
309 kfree(attrbuf);
310trans_out:
311 kfree_skb(trans_buf);
312
313 return err;
314}
315
316static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
317 struct tipc_nl_compat_msg *msg)
318{
319 int err;
320
321 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
322 return -EINVAL;
323
324 err = __tipc_nl_compat_doit(cmd, msg);
325 if (err)
326 return err;
327
328 /* The legacy API considered an empty message a success message */
329 msg->rep = tipc_tlv_alloc(0);
330 if (!msg->rep)
331 return -ENOMEM;
332
333 return 0;
334}
335
d0796d1e
RA
336static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
337 struct nlattr **attrs)
338{
339 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
340
341 nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
342 NULL);
343
344 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
345 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
346 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
347}
348
9ab15465
RA
349static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
350 struct tipc_nl_compat_msg *msg)
351{
352 struct nlattr *prop;
353 struct nlattr *bearer;
354 struct tipc_bearer_config *b;
355
356 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
357
358 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
359 if (!bearer)
360 return -EMSGSIZE;
361
362 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
363 return -EMSGSIZE;
364
365 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
366 return -EMSGSIZE;
367
368 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
369 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
370 if (!prop)
371 return -EMSGSIZE;
372 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
373 return -EMSGSIZE;
374 nla_nest_end(skb, prop);
375 }
376 nla_nest_end(skb, bearer);
377
378 return 0;
379}
380
381static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
382 struct tipc_nl_compat_msg *msg)
383{
384 char *name;
385 struct nlattr *bearer;
386
387 name = (char *)TLV_DATA(msg->req);
388
389 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
390 if (!bearer)
391 return -EMSGSIZE;
392
393 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
394 return -EMSGSIZE;
395
396 nla_nest_end(skb, bearer);
397
398 return 0;
399}
400
f2b3b2d4
RA
401static inline u32 perc(u32 count, u32 total)
402{
403 return (count * 100 + (total / 2)) / total;
404}
405
406static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
407 struct nlattr *prop[], struct nlattr *stats[])
408{
409 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
410 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
411
412 tipc_tlv_sprintf(msg->rep,
413 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
414 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
415 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
416 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
417 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
418 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
419
420 tipc_tlv_sprintf(msg->rep,
421 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
422 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
423 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
424 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
425 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
426 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
427
428 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
429 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
430 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
431 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
432
433 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
434 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
435 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
436 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
437
438 tipc_tlv_sprintf(msg->rep,
439 " Congestion link:%u Send queue max:%u avg:%u",
440 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
441 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
442 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
443}
444
445static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
446 struct nlattr **attrs)
447{
448 char *name;
449 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
450 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
451 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
452
453 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
454
455 nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
456 NULL);
457
458 nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
459 NULL);
460
461 name = (char *)TLV_DATA(msg->req);
462 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
463 return 0;
464
465 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
466 nla_data(link[TIPC_NLA_LINK_NAME]));
467
468 if (link[TIPC_NLA_LINK_BROADCAST]) {
469 __fill_bc_link_stat(msg, prop, stats);
470 return 0;
471 }
472
473 if (link[TIPC_NLA_LINK_ACTIVE])
474 tipc_tlv_sprintf(msg->rep, " ACTIVE");
475 else if (link[TIPC_NLA_LINK_UP])
476 tipc_tlv_sprintf(msg->rep, " STANDBY");
477 else
478 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
479
480 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
481 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
482 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
483
484 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
485 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
486 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
487
488 tipc_tlv_sprintf(msg->rep,
489 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
490 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
491 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
492 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
493 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
494 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
495 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
496
497 tipc_tlv_sprintf(msg->rep,
498 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
499 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
500 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
501 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
502 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
503 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
504 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
505
506 tipc_tlv_sprintf(msg->rep,
507 " TX profile sample:%u packets average:%u octets\n",
508 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
509 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
510 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
511
512 tipc_tlv_sprintf(msg->rep,
513 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
514 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
515 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
516 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
517 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
518 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
519 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
520 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
521 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
522
523 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
524 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
525 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
526 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
527 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
528 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
529 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
530
531 tipc_tlv_sprintf(msg->rep,
532 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
533 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
534 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
535 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
536 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
537 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
538
539 tipc_tlv_sprintf(msg->rep,
540 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
541 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
542 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
543 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
544 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
545 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
546
547 tipc_tlv_sprintf(msg->rep,
548 " Congestion link:%u Send queue max:%u avg:%u",
549 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
550 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
551 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
552
553 return 0;
554}
555
357ebdbf
RA
556static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
557 struct nlattr **attrs)
558{
559 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
560 struct tipc_link_info link_info;
561
562 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
563
564 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
565 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
566 strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
567
568 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
569 &link_info, sizeof(link_info));
570}
571
d0796d1e
RA
572static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
573{
574 struct tipc_nl_compat_cmd_dump dump;
9ab15465 575 struct tipc_nl_compat_cmd_doit doit;
d0796d1e
RA
576
577 memset(&dump, 0, sizeof(dump));
9ab15465 578 memset(&doit, 0, sizeof(doit));
d0796d1e
RA
579
580 switch (msg->cmd) {
581 case TIPC_CMD_GET_BEARER_NAMES:
582 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
583 dump.dumpit = tipc_nl_bearer_dump;
584 dump.format = tipc_nl_compat_bearer_dump;
585 return tipc_nl_compat_dumpit(&dump, msg);
9ab15465
RA
586 case TIPC_CMD_ENABLE_BEARER:
587 msg->req_type = TIPC_TLV_BEARER_CONFIG;
588 doit.doit = tipc_nl_bearer_enable;
589 doit.transcode = tipc_nl_compat_bearer_enable;
590 return tipc_nl_compat_doit(&doit, msg);
591 case TIPC_CMD_DISABLE_BEARER:
592 msg->req_type = TIPC_TLV_BEARER_NAME;
593 doit.doit = tipc_nl_bearer_disable;
594 doit.transcode = tipc_nl_compat_bearer_disable;
595 return tipc_nl_compat_doit(&doit, msg);
f2b3b2d4
RA
596 case TIPC_CMD_SHOW_LINK_STATS:
597 msg->req_type = TIPC_TLV_LINK_NAME;
598 msg->rep_size = ULTRA_STRING_MAX_LEN;
599 msg->rep_type = TIPC_TLV_ULTRA_STRING;
600 dump.dumpit = tipc_nl_link_dump;
601 dump.format = tipc_nl_compat_link_stat_dump;
602 return tipc_nl_compat_dumpit(&dump, msg);
357ebdbf
RA
603 case TIPC_CMD_GET_LINKS:
604 msg->req_type = TIPC_TLV_NET_ADDR;
605 msg->rep_size = ULTRA_STRING_MAX_LEN;
606 dump.dumpit = tipc_nl_link_dump;
607 dump.format = tipc_nl_compat_link_dump;
608 return tipc_nl_compat_dumpit(&dump, msg);
d0796d1e
RA
609 }
610
611 return -EOPNOTSUPP;
612}
613
614static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
615{
616 int err;
617 int len;
618 struct tipc_nl_compat_msg msg;
619 struct nlmsghdr *req_nlh;
620 struct nlmsghdr *rep_nlh;
621 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
622 struct net *net = genl_info_net(info);
623
624 memset(&msg, 0, sizeof(msg));
625
626 req_nlh = (struct nlmsghdr *)skb->data;
627 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
628 msg.cmd = req_userhdr->cmd;
629 msg.dst_sk = info->dst_sk;
630
631 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
632 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
633 err = -EACCES;
634 goto send;
635 }
636
637 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
638 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
639 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
640 err = -EOPNOTSUPP;
641 goto send;
642 }
643
644 err = tipc_nl_compat_handle(&msg);
645 if (err == -EOPNOTSUPP)
646 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
647 else if (err == -EINVAL)
648 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
649send:
650 if (!msg.rep)
651 return err;
652
653 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
654 skb_push(msg.rep, len);
655 rep_nlh = nlmsg_hdr(msg.rep);
656 memcpy(rep_nlh, info->nlhdr, len);
657 rep_nlh->nlmsg_len = msg.rep->len;
658 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
659
660 return err;
661}
662
bfb3e5dd
RA
663static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
664{
665 struct net *net = genl_info_net(info);
666 struct sk_buff *rep_buf;
667 struct nlmsghdr *rep_nlh;
668 struct nlmsghdr *req_nlh = info->nlhdr;
669 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
670 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
671 u16 cmd;
672
673 if ((req_userhdr->cmd & 0xC000) &&
674 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
675 cmd = TIPC_CMD_NOT_NET_ADMIN;
676 else
677 cmd = req_userhdr->cmd;
678
679 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
680 nlmsg_data(req_nlh) + GENL_HDRLEN +
681 TIPC_GENL_HDRLEN,
682 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
683 TIPC_GENL_HDRLEN), hdr_space);
684
685 if (rep_buf) {
686 skb_push(rep_buf, hdr_space);
687 rep_nlh = nlmsg_hdr(rep_buf);
688 memcpy(rep_nlh, req_nlh, hdr_space);
689 rep_nlh->nlmsg_len = rep_buf->len;
690 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
691 }
692
693 return 0;
694}
695
d0796d1e
RA
696/* Temporary function to keep functionality throughout the patchset
697 * without having to mess with the global variables and other trickery
698 * of the old API.
699 */
700static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
701{
702 struct tipc_genlmsghdr *req = info->userhdr;
703
704 switch (req->cmd) {
705 case TIPC_CMD_GET_BEARER_NAMES:
9ab15465
RA
706 case TIPC_CMD_ENABLE_BEARER:
707 case TIPC_CMD_DISABLE_BEARER:
f2b3b2d4 708 case TIPC_CMD_SHOW_LINK_STATS:
357ebdbf 709 case TIPC_CMD_GET_LINKS:
d0796d1e
RA
710 return tipc_nl_compat_recv(skb, info);
711 }
712
713 return handle_cmd(skb, info);
714}
715
bfb3e5dd
RA
716static struct genl_family tipc_genl_compat_family = {
717 .id = GENL_ID_GENERATE,
718 .name = TIPC_GENL_NAME,
719 .version = TIPC_GENL_VERSION,
720 .hdrsize = TIPC_GENL_HDRLEN,
721 .maxattr = 0,
722 .netnsok = true,
723};
724
725static struct genl_ops tipc_genl_compat_ops[] = {
726 {
727 .cmd = TIPC_GENL_CMD,
d0796d1e 728 .doit = tipc_nl_compat_tmp_wrap,
bfb3e5dd
RA
729 },
730};
731
732int tipc_netlink_compat_start(void)
733{
734 int res;
735
736 res = genl_register_family_with_ops(&tipc_genl_compat_family,
737 tipc_genl_compat_ops);
738 if (res) {
739 pr_err("Failed to register legacy compat interface\n");
740 return res;
741 }
742
743 return 0;
744}
745
746void tipc_netlink_compat_stop(void)
747{
748 genl_unregister_family(&tipc_genl_compat_family);
749}