blob: 02461233b6d85b2106546f1ccf3e1d52a167fb9e [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 Alpe18178772015-02-09 09:50:09 +0100609static int tipc_nl_compat_link_reset_stats(struct sk_buff *skb,
610 struct tipc_nl_compat_msg *msg)
611{
612 char *name;
613 struct nlattr *link;
614
615 name = (char *)TLV_DATA(msg->req);
616
617 link = nla_nest_start(skb, TIPC_NLA_LINK);
618 if (!link)
619 return -EMSGSIZE;
620
621 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
622 return -EMSGSIZE;
623
624 nla_nest_end(skb, link);
625
626 return 0;
627}
628
Richard Alped0796d12015-02-09 09:50:04 +0100629static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
630{
631 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +0100632 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +0100633
634 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +0100635 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +0100636
637 switch (msg->cmd) {
638 case TIPC_CMD_GET_BEARER_NAMES:
639 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
640 dump.dumpit = tipc_nl_bearer_dump;
641 dump.format = tipc_nl_compat_bearer_dump;
642 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100643 case TIPC_CMD_ENABLE_BEARER:
644 msg->req_type = TIPC_TLV_BEARER_CONFIG;
645 doit.doit = tipc_nl_bearer_enable;
646 doit.transcode = tipc_nl_compat_bearer_enable;
647 return tipc_nl_compat_doit(&doit, msg);
648 case TIPC_CMD_DISABLE_BEARER:
649 msg->req_type = TIPC_TLV_BEARER_NAME;
650 doit.doit = tipc_nl_bearer_disable;
651 doit.transcode = tipc_nl_compat_bearer_disable;
652 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100653 case TIPC_CMD_SHOW_LINK_STATS:
654 msg->req_type = TIPC_TLV_LINK_NAME;
655 msg->rep_size = ULTRA_STRING_MAX_LEN;
656 msg->rep_type = TIPC_TLV_ULTRA_STRING;
657 dump.dumpit = tipc_nl_link_dump;
658 dump.format = tipc_nl_compat_link_stat_dump;
659 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100660 case TIPC_CMD_GET_LINKS:
661 msg->req_type = TIPC_TLV_NET_ADDR;
662 msg->rep_size = ULTRA_STRING_MAX_LEN;
663 dump.dumpit = tipc_nl_link_dump;
664 dump.format = tipc_nl_compat_link_dump;
665 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +0100666 case TIPC_CMD_SET_LINK_TOL:
667 case TIPC_CMD_SET_LINK_PRI:
668 case TIPC_CMD_SET_LINK_WINDOW:
669 msg->req_type = TIPC_TLV_LINK_CONFIG;
670 doit.doit = tipc_nl_link_set;
671 doit.transcode = tipc_nl_compat_link_set;
672 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +0100673 case TIPC_CMD_RESET_LINK_STATS:
674 msg->req_type = TIPC_TLV_LINK_NAME;
675 doit.doit = tipc_nl_link_reset_stats;
676 doit.transcode = tipc_nl_compat_link_reset_stats;
677 return tipc_nl_compat_doit(&doit, msg);
Richard Alped0796d12015-02-09 09:50:04 +0100678 }
679
680 return -EOPNOTSUPP;
681}
682
683static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
684{
685 int err;
686 int len;
687 struct tipc_nl_compat_msg msg;
688 struct nlmsghdr *req_nlh;
689 struct nlmsghdr *rep_nlh;
690 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
691 struct net *net = genl_info_net(info);
692
693 memset(&msg, 0, sizeof(msg));
694
695 req_nlh = (struct nlmsghdr *)skb->data;
696 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
697 msg.cmd = req_userhdr->cmd;
698 msg.dst_sk = info->dst_sk;
699
700 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
701 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
702 err = -EACCES;
703 goto send;
704 }
705
706 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
707 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
708 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
709 err = -EOPNOTSUPP;
710 goto send;
711 }
712
713 err = tipc_nl_compat_handle(&msg);
714 if (err == -EOPNOTSUPP)
715 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
716 else if (err == -EINVAL)
717 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
718send:
719 if (!msg.rep)
720 return err;
721
722 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
723 skb_push(msg.rep, len);
724 rep_nlh = nlmsg_hdr(msg.rep);
725 memcpy(rep_nlh, info->nlhdr, len);
726 rep_nlh->nlmsg_len = msg.rep->len;
727 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
728
729 return err;
730}
731
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100732static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
733{
734 struct net *net = genl_info_net(info);
735 struct sk_buff *rep_buf;
736 struct nlmsghdr *rep_nlh;
737 struct nlmsghdr *req_nlh = info->nlhdr;
738 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
739 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
740 u16 cmd;
741
742 if ((req_userhdr->cmd & 0xC000) &&
743 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
744 cmd = TIPC_CMD_NOT_NET_ADMIN;
745 else
746 cmd = req_userhdr->cmd;
747
748 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
749 nlmsg_data(req_nlh) + GENL_HDRLEN +
750 TIPC_GENL_HDRLEN,
751 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
752 TIPC_GENL_HDRLEN), hdr_space);
753
754 if (rep_buf) {
755 skb_push(rep_buf, hdr_space);
756 rep_nlh = nlmsg_hdr(rep_buf);
757 memcpy(rep_nlh, req_nlh, hdr_space);
758 rep_nlh->nlmsg_len = rep_buf->len;
759 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
760 }
761
762 return 0;
763}
764
Richard Alped0796d12015-02-09 09:50:04 +0100765/* Temporary function to keep functionality throughout the patchset
766 * without having to mess with the global variables and other trickery
767 * of the old API.
768 */
769static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
770{
771 struct tipc_genlmsghdr *req = info->userhdr;
772
773 switch (req->cmd) {
774 case TIPC_CMD_GET_BEARER_NAMES:
Richard Alpe9ab15462015-02-09 09:50:05 +0100775 case TIPC_CMD_ENABLE_BEARER:
776 case TIPC_CMD_DISABLE_BEARER:
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100777 case TIPC_CMD_SHOW_LINK_STATS:
Richard Alpe357ebdb2015-02-09 09:50:07 +0100778 case TIPC_CMD_GET_LINKS:
Richard Alpe37e2d482015-02-09 09:50:08 +0100779 case TIPC_CMD_SET_LINK_TOL:
780 case TIPC_CMD_SET_LINK_PRI:
781 case TIPC_CMD_SET_LINK_WINDOW:
Richard Alpe18178772015-02-09 09:50:09 +0100782 case TIPC_CMD_RESET_LINK_STATS:
Richard Alped0796d12015-02-09 09:50:04 +0100783 return tipc_nl_compat_recv(skb, info);
784 }
785
786 return handle_cmd(skb, info);
787}
788
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100789static struct genl_family tipc_genl_compat_family = {
790 .id = GENL_ID_GENERATE,
791 .name = TIPC_GENL_NAME,
792 .version = TIPC_GENL_VERSION,
793 .hdrsize = TIPC_GENL_HDRLEN,
794 .maxattr = 0,
795 .netnsok = true,
796};
797
798static struct genl_ops tipc_genl_compat_ops[] = {
799 {
800 .cmd = TIPC_GENL_CMD,
Richard Alped0796d12015-02-09 09:50:04 +0100801 .doit = tipc_nl_compat_tmp_wrap,
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100802 },
803};
804
805int tipc_netlink_compat_start(void)
806{
807 int res;
808
809 res = genl_register_family_with_ops(&tipc_genl_compat_family,
810 tipc_genl_compat_ops);
811 if (res) {
812 pr_err("Failed to register legacy compat interface\n");
813 return res;
814 }
815
816 return 0;
817}
818
819void tipc_netlink_compat_stop(void)
820{
821 genl_unregister_family(&tipc_genl_compat_family);
822}