blob: 056532b41e7ee6b63ac86a893cdbafb12e59a097 [file] [log] [blame]
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001/*
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"
Richard Alped0796d12015-02-09 09:50:04 +010036#include "bearer.h"
Richard Alpef2b3b2d2015-02-09 09:50:06 +010037#include "link.h"
Richard Alpebfb3e5d2015-02-09 09:50:03 +010038#include <net/genetlink.h>
39#include <linux/tipc_config.h>
40
Richard Alped0796d12015-02-09 09:50:04 +010041/* 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;
Richard Alpef2b3b2d2015-02-09 09:50:06 +010052 int rep_type;
Richard Alped0796d12015-02-09 09:50:04 +010053 int rep_size;
Richard Alpe9ab15462015-02-09 09:50:05 +010054 int req_type;
Richard Alped0796d12015-02-09 09:50:04 +010055 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
Richard Alpe9ab15462015-02-09 09:50:05 +010065struct 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
Richard Alped0796d12015-02-09 09:50:04 +010070static 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
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100100static 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
Richard Alped0796d12015-02-09 09:50:04 +0100134static 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
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100239 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
240 return -EINVAL;
241
Richard Alped0796d12015-02-09 09:50:04 +0100242 msg->rep = tipc_tlv_alloc(msg->rep_size);
243 if (!msg->rep)
244 return -ENOMEM;
245
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100246 if (msg->rep_type)
247 tipc_tlv_init(msg->rep, msg->rep_type);
248
Richard Alped0796d12015-02-09 09:50:04 +0100249 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
Richard Alpe9ab15462015-02-09 09:50:05 +0100264static 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
Richard Alped0796d12015-02-09 09:50:04 +0100336static 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
Richard Alpe9ab15462015-02-09 09:50:05 +0100349static 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
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100401static 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
Richard Alpe357ebdb2015-02-09 09:50:07 +0100556static 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
Richard Alpe37e2d482015-02-09 09:50:08 +0100572static int tipc_nl_compat_link_set(struct sk_buff *skb,
573 struct tipc_nl_compat_msg *msg)
574{
575 struct nlattr *link;
576 struct nlattr *prop;
577 struct tipc_link_config *lc;
578
579 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
580
581 link = nla_nest_start(skb, TIPC_NLA_LINK);
582 if (!link)
583 return -EMSGSIZE;
584
585 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
586 return -EMSGSIZE;
587
588 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
589 if (!prop)
590 return -EMSGSIZE;
591
592 if (msg->cmd == TIPC_CMD_SET_LINK_PRI) {
593 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)))
594 return -EMSGSIZE;
595 } else if (msg->cmd == TIPC_CMD_SET_LINK_TOL) {
596 if (nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)))
597 return -EMSGSIZE;
598 } else if (msg->cmd == TIPC_CMD_SET_LINK_WINDOW) {
599 if (nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)))
600 return -EMSGSIZE;
601 }
602
603 nla_nest_end(skb, prop);
604 nla_nest_end(skb, link);
605
606 return 0;
607}
608
Richard Alped0796d12015-02-09 09:50:04 +0100609static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
610{
611 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +0100612 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +0100613
614 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +0100615 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +0100616
617 switch (msg->cmd) {
618 case TIPC_CMD_GET_BEARER_NAMES:
619 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
620 dump.dumpit = tipc_nl_bearer_dump;
621 dump.format = tipc_nl_compat_bearer_dump;
622 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100623 case TIPC_CMD_ENABLE_BEARER:
624 msg->req_type = TIPC_TLV_BEARER_CONFIG;
625 doit.doit = tipc_nl_bearer_enable;
626 doit.transcode = tipc_nl_compat_bearer_enable;
627 return tipc_nl_compat_doit(&doit, msg);
628 case TIPC_CMD_DISABLE_BEARER:
629 msg->req_type = TIPC_TLV_BEARER_NAME;
630 doit.doit = tipc_nl_bearer_disable;
631 doit.transcode = tipc_nl_compat_bearer_disable;
632 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100633 case TIPC_CMD_SHOW_LINK_STATS:
634 msg->req_type = TIPC_TLV_LINK_NAME;
635 msg->rep_size = ULTRA_STRING_MAX_LEN;
636 msg->rep_type = TIPC_TLV_ULTRA_STRING;
637 dump.dumpit = tipc_nl_link_dump;
638 dump.format = tipc_nl_compat_link_stat_dump;
639 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100640 case TIPC_CMD_GET_LINKS:
641 msg->req_type = TIPC_TLV_NET_ADDR;
642 msg->rep_size = ULTRA_STRING_MAX_LEN;
643 dump.dumpit = tipc_nl_link_dump;
644 dump.format = tipc_nl_compat_link_dump;
645 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +0100646 case TIPC_CMD_SET_LINK_TOL:
647 case TIPC_CMD_SET_LINK_PRI:
648 case TIPC_CMD_SET_LINK_WINDOW:
649 msg->req_type = TIPC_TLV_LINK_CONFIG;
650 doit.doit = tipc_nl_link_set;
651 doit.transcode = tipc_nl_compat_link_set;
652 return tipc_nl_compat_doit(&doit, msg);
Richard Alped0796d12015-02-09 09:50:04 +0100653 }
654
655 return -EOPNOTSUPP;
656}
657
658static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
659{
660 int err;
661 int len;
662 struct tipc_nl_compat_msg msg;
663 struct nlmsghdr *req_nlh;
664 struct nlmsghdr *rep_nlh;
665 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
666 struct net *net = genl_info_net(info);
667
668 memset(&msg, 0, sizeof(msg));
669
670 req_nlh = (struct nlmsghdr *)skb->data;
671 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
672 msg.cmd = req_userhdr->cmd;
673 msg.dst_sk = info->dst_sk;
674
675 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
676 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
677 err = -EACCES;
678 goto send;
679 }
680
681 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
682 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
683 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
684 err = -EOPNOTSUPP;
685 goto send;
686 }
687
688 err = tipc_nl_compat_handle(&msg);
689 if (err == -EOPNOTSUPP)
690 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
691 else if (err == -EINVAL)
692 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
693send:
694 if (!msg.rep)
695 return err;
696
697 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
698 skb_push(msg.rep, len);
699 rep_nlh = nlmsg_hdr(msg.rep);
700 memcpy(rep_nlh, info->nlhdr, len);
701 rep_nlh->nlmsg_len = msg.rep->len;
702 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
703
704 return err;
705}
706
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100707static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
708{
709 struct net *net = genl_info_net(info);
710 struct sk_buff *rep_buf;
711 struct nlmsghdr *rep_nlh;
712 struct nlmsghdr *req_nlh = info->nlhdr;
713 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
714 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
715 u16 cmd;
716
717 if ((req_userhdr->cmd & 0xC000) &&
718 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
719 cmd = TIPC_CMD_NOT_NET_ADMIN;
720 else
721 cmd = req_userhdr->cmd;
722
723 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
724 nlmsg_data(req_nlh) + GENL_HDRLEN +
725 TIPC_GENL_HDRLEN,
726 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
727 TIPC_GENL_HDRLEN), hdr_space);
728
729 if (rep_buf) {
730 skb_push(rep_buf, hdr_space);
731 rep_nlh = nlmsg_hdr(rep_buf);
732 memcpy(rep_nlh, req_nlh, hdr_space);
733 rep_nlh->nlmsg_len = rep_buf->len;
734 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
735 }
736
737 return 0;
738}
739
Richard Alped0796d12015-02-09 09:50:04 +0100740/* Temporary function to keep functionality throughout the patchset
741 * without having to mess with the global variables and other trickery
742 * of the old API.
743 */
744static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
745{
746 struct tipc_genlmsghdr *req = info->userhdr;
747
748 switch (req->cmd) {
749 case TIPC_CMD_GET_BEARER_NAMES:
Richard Alpe9ab15462015-02-09 09:50:05 +0100750 case TIPC_CMD_ENABLE_BEARER:
751 case TIPC_CMD_DISABLE_BEARER:
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100752 case TIPC_CMD_SHOW_LINK_STATS:
Richard Alpe357ebdb2015-02-09 09:50:07 +0100753 case TIPC_CMD_GET_LINKS:
Richard Alpe37e2d482015-02-09 09:50:08 +0100754 case TIPC_CMD_SET_LINK_TOL:
755 case TIPC_CMD_SET_LINK_PRI:
756 case TIPC_CMD_SET_LINK_WINDOW:
Richard Alped0796d12015-02-09 09:50:04 +0100757 return tipc_nl_compat_recv(skb, info);
758 }
759
760 return handle_cmd(skb, info);
761}
762
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100763static struct genl_family tipc_genl_compat_family = {
764 .id = GENL_ID_GENERATE,
765 .name = TIPC_GENL_NAME,
766 .version = TIPC_GENL_VERSION,
767 .hdrsize = TIPC_GENL_HDRLEN,
768 .maxattr = 0,
769 .netnsok = true,
770};
771
772static struct genl_ops tipc_genl_compat_ops[] = {
773 {
774 .cmd = TIPC_GENL_CMD,
Richard Alped0796d12015-02-09 09:50:04 +0100775 .doit = tipc_nl_compat_tmp_wrap,
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100776 },
777};
778
779int tipc_netlink_compat_start(void)
780{
781 int res;
782
783 res = genl_register_family_with_ops(&tipc_genl_compat_family,
784 tipc_genl_compat_ops);
785 if (res) {
786 pr_err("Failed to register legacy compat interface\n");
787 return res;
788 }
789
790 return 0;
791}
792
793void tipc_netlink_compat_stop(void)
794{
795 genl_unregister_family(&tipc_genl_compat_family);
796}