tipc: convert legacy nl media dump to nl compat
[deliverable/linux.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"
44a8ae94 38#include "name_table.h"
487d2a3a 39#include "socket.h"
bfb3e5dd
RA
40#include <net/genetlink.h>
41#include <linux/tipc_config.h>
42
d0796d1e
RA
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
52struct tipc_nl_compat_msg {
53 u16 cmd;
f2b3b2d4 54 int rep_type;
d0796d1e 55 int rep_size;
9ab15465 56 int req_type;
d0796d1e
RA
57 struct sk_buff *rep;
58 struct tlv_desc *req;
59 struct sock *dst_sk;
60};
61
62struct tipc_nl_compat_cmd_dump {
44a8ae94 63 int (*header)(struct tipc_nl_compat_msg *);
d0796d1e
RA
64 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
65 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
66};
67
9ab15465
RA
68struct 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
d0796d1e
RA
73static 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
87static 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
f2b3b2d4
RA
103static 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
112static 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
d0796d1e
RA
137static 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
154static 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
166static 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
215err_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
236static 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
f2b3b2d4
RA
242 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
243 return -EINVAL;
244
d0796d1e
RA
245 msg->rep = tipc_tlv_alloc(msg->rep_size);
246 if (!msg->rep)
247 return -ENOMEM;
248
f2b3b2d4
RA
249 if (msg->rep_type)
250 tipc_tlv_init(msg->rep, msg->rep_type);
251
44a8ae94
RA
252 if (cmd->header)
253 (*cmd->header)(msg);
254
d0796d1e
RA
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
9ab15465
RA
270static 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);
314parse_out:
315 kfree(attrbuf);
316trans_out:
317 kfree_skb(trans_buf);
318
319 return err;
320}
321
322static 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
d0796d1e
RA
342static 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
9ab15465
RA
355static 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
387static 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
f2b3b2d4
RA
407static inline u32 perc(u32 count, u32 total)
408{
409 return (count * 100 + (total / 2)) / total;
410}
411
412static 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
451static 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
357ebdbf
RA
562static 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
37e2d484
RA
578static 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
1817877b
RA
615static 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
44a8ae94
RA
635static 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
660static 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])]);
716out:
717 tipc_tlv_sprintf(msg->rep, "\n");
718
719 return 0;
720}
721
487d2a3a
RA
722static 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
742static 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
781static 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
5bfc335a
RA
825static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
826 struct nlattr **attrs)
827{
828 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
829
830 nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
831 NULL);
832
833 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
834 nla_data(media[TIPC_NLA_MEDIA_NAME]),
835 nla_len(media[TIPC_NLA_MEDIA_NAME]));
836}
837
d0796d1e
RA
838static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
839{
840 struct tipc_nl_compat_cmd_dump dump;
9ab15465 841 struct tipc_nl_compat_cmd_doit doit;
d0796d1e
RA
842
843 memset(&dump, 0, sizeof(dump));
9ab15465 844 memset(&doit, 0, sizeof(doit));
d0796d1e
RA
845
846 switch (msg->cmd) {
847 case TIPC_CMD_GET_BEARER_NAMES:
848 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
849 dump.dumpit = tipc_nl_bearer_dump;
850 dump.format = tipc_nl_compat_bearer_dump;
851 return tipc_nl_compat_dumpit(&dump, msg);
9ab15465
RA
852 case TIPC_CMD_ENABLE_BEARER:
853 msg->req_type = TIPC_TLV_BEARER_CONFIG;
854 doit.doit = tipc_nl_bearer_enable;
855 doit.transcode = tipc_nl_compat_bearer_enable;
856 return tipc_nl_compat_doit(&doit, msg);
857 case TIPC_CMD_DISABLE_BEARER:
858 msg->req_type = TIPC_TLV_BEARER_NAME;
859 doit.doit = tipc_nl_bearer_disable;
860 doit.transcode = tipc_nl_compat_bearer_disable;
861 return tipc_nl_compat_doit(&doit, msg);
f2b3b2d4
RA
862 case TIPC_CMD_SHOW_LINK_STATS:
863 msg->req_type = TIPC_TLV_LINK_NAME;
864 msg->rep_size = ULTRA_STRING_MAX_LEN;
865 msg->rep_type = TIPC_TLV_ULTRA_STRING;
866 dump.dumpit = tipc_nl_link_dump;
867 dump.format = tipc_nl_compat_link_stat_dump;
868 return tipc_nl_compat_dumpit(&dump, msg);
357ebdbf
RA
869 case TIPC_CMD_GET_LINKS:
870 msg->req_type = TIPC_TLV_NET_ADDR;
871 msg->rep_size = ULTRA_STRING_MAX_LEN;
872 dump.dumpit = tipc_nl_link_dump;
873 dump.format = tipc_nl_compat_link_dump;
874 return tipc_nl_compat_dumpit(&dump, msg);
37e2d484
RA
875 case TIPC_CMD_SET_LINK_TOL:
876 case TIPC_CMD_SET_LINK_PRI:
877 case TIPC_CMD_SET_LINK_WINDOW:
878 msg->req_type = TIPC_TLV_LINK_CONFIG;
879 doit.doit = tipc_nl_link_set;
880 doit.transcode = tipc_nl_compat_link_set;
881 return tipc_nl_compat_doit(&doit, msg);
1817877b
RA
882 case TIPC_CMD_RESET_LINK_STATS:
883 msg->req_type = TIPC_TLV_LINK_NAME;
884 doit.doit = tipc_nl_link_reset_stats;
885 doit.transcode = tipc_nl_compat_link_reset_stats;
886 return tipc_nl_compat_doit(&doit, msg);
44a8ae94
RA
887 case TIPC_CMD_SHOW_NAME_TABLE:
888 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
889 msg->rep_size = ULTRA_STRING_MAX_LEN;
890 msg->rep_type = TIPC_TLV_ULTRA_STRING;
891 dump.header = tipc_nl_compat_name_table_dump_header;
892 dump.dumpit = tipc_nl_name_table_dump;
893 dump.format = tipc_nl_compat_name_table_dump;
894 return tipc_nl_compat_dumpit(&dump, msg);
487d2a3a
RA
895 case TIPC_CMD_SHOW_PORTS:
896 msg->rep_size = ULTRA_STRING_MAX_LEN;
897 msg->rep_type = TIPC_TLV_ULTRA_STRING;
898 dump.dumpit = tipc_nl_sk_dump;
899 dump.format = tipc_nl_compat_sk_dump;
900 return tipc_nl_compat_dumpit(&dump, msg);
5bfc335a
RA
901 case TIPC_CMD_GET_MEDIA_NAMES:
902 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
903 dump.dumpit = tipc_nl_media_dump;
904 dump.format = tipc_nl_compat_media_dump;
905 return tipc_nl_compat_dumpit(&dump, msg);
d0796d1e
RA
906 }
907
908 return -EOPNOTSUPP;
909}
910
911static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
912{
913 int err;
914 int len;
915 struct tipc_nl_compat_msg msg;
916 struct nlmsghdr *req_nlh;
917 struct nlmsghdr *rep_nlh;
918 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
919 struct net *net = genl_info_net(info);
920
921 memset(&msg, 0, sizeof(msg));
922
923 req_nlh = (struct nlmsghdr *)skb->data;
924 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
925 msg.cmd = req_userhdr->cmd;
926 msg.dst_sk = info->dst_sk;
927
928 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
929 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
930 err = -EACCES;
931 goto send;
932 }
933
934 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
935 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
936 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
937 err = -EOPNOTSUPP;
938 goto send;
939 }
940
941 err = tipc_nl_compat_handle(&msg);
942 if (err == -EOPNOTSUPP)
943 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
944 else if (err == -EINVAL)
945 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
946send:
947 if (!msg.rep)
948 return err;
949
950 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
951 skb_push(msg.rep, len);
952 rep_nlh = nlmsg_hdr(msg.rep);
953 memcpy(rep_nlh, info->nlhdr, len);
954 rep_nlh->nlmsg_len = msg.rep->len;
955 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
956
957 return err;
958}
959
bfb3e5dd
RA
960static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
961{
962 struct net *net = genl_info_net(info);
963 struct sk_buff *rep_buf;
964 struct nlmsghdr *rep_nlh;
965 struct nlmsghdr *req_nlh = info->nlhdr;
966 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
967 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
968 u16 cmd;
969
970 if ((req_userhdr->cmd & 0xC000) &&
971 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
972 cmd = TIPC_CMD_NOT_NET_ADMIN;
973 else
974 cmd = req_userhdr->cmd;
975
976 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
977 nlmsg_data(req_nlh) + GENL_HDRLEN +
978 TIPC_GENL_HDRLEN,
979 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
980 TIPC_GENL_HDRLEN), hdr_space);
981
982 if (rep_buf) {
983 skb_push(rep_buf, hdr_space);
984 rep_nlh = nlmsg_hdr(rep_buf);
985 memcpy(rep_nlh, req_nlh, hdr_space);
986 rep_nlh->nlmsg_len = rep_buf->len;
987 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
988 }
989
990 return 0;
991}
992
d0796d1e
RA
993/* Temporary function to keep functionality throughout the patchset
994 * without having to mess with the global variables and other trickery
995 * of the old API.
996 */
997static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
998{
999 struct tipc_genlmsghdr *req = info->userhdr;
1000
1001 switch (req->cmd) {
1002 case TIPC_CMD_GET_BEARER_NAMES:
9ab15465
RA
1003 case TIPC_CMD_ENABLE_BEARER:
1004 case TIPC_CMD_DISABLE_BEARER:
f2b3b2d4 1005 case TIPC_CMD_SHOW_LINK_STATS:
357ebdbf 1006 case TIPC_CMD_GET_LINKS:
37e2d484
RA
1007 case TIPC_CMD_SET_LINK_TOL:
1008 case TIPC_CMD_SET_LINK_PRI:
1009 case TIPC_CMD_SET_LINK_WINDOW:
1817877b 1010 case TIPC_CMD_RESET_LINK_STATS:
44a8ae94 1011 case TIPC_CMD_SHOW_NAME_TABLE:
487d2a3a 1012 case TIPC_CMD_SHOW_PORTS:
5bfc335a 1013 case TIPC_CMD_GET_MEDIA_NAMES:
d0796d1e
RA
1014 return tipc_nl_compat_recv(skb, info);
1015 }
1016
1017 return handle_cmd(skb, info);
1018}
1019
bfb3e5dd
RA
1020static struct genl_family tipc_genl_compat_family = {
1021 .id = GENL_ID_GENERATE,
1022 .name = TIPC_GENL_NAME,
1023 .version = TIPC_GENL_VERSION,
1024 .hdrsize = TIPC_GENL_HDRLEN,
1025 .maxattr = 0,
1026 .netnsok = true,
1027};
1028
1029static struct genl_ops tipc_genl_compat_ops[] = {
1030 {
1031 .cmd = TIPC_GENL_CMD,
d0796d1e 1032 .doit = tipc_nl_compat_tmp_wrap,
bfb3e5dd
RA
1033 },
1034};
1035
1036int tipc_netlink_compat_start(void)
1037{
1038 int res;
1039
1040 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1041 tipc_genl_compat_ops);
1042 if (res) {
1043 pr_err("Failed to register legacy compat interface\n");
1044 return res;
1045 }
1046
1047 return 0;
1048}
1049
1050void tipc_netlink_compat_stop(void)
1051{
1052 genl_unregister_family(&tipc_genl_compat_family);
1053}
This page took 0.065702 seconds and 5 git commands to generate.