blob: 899bd94da4678637c1cfbbb7de672ba1a430374f [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 Alped0796d12015-02-09 09:50:04 +0100556static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
557{
558 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +0100559 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +0100560
561 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +0100562 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +0100563
564 switch (msg->cmd) {
565 case TIPC_CMD_GET_BEARER_NAMES:
566 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
567 dump.dumpit = tipc_nl_bearer_dump;
568 dump.format = tipc_nl_compat_bearer_dump;
569 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100570 case TIPC_CMD_ENABLE_BEARER:
571 msg->req_type = TIPC_TLV_BEARER_CONFIG;
572 doit.doit = tipc_nl_bearer_enable;
573 doit.transcode = tipc_nl_compat_bearer_enable;
574 return tipc_nl_compat_doit(&doit, msg);
575 case TIPC_CMD_DISABLE_BEARER:
576 msg->req_type = TIPC_TLV_BEARER_NAME;
577 doit.doit = tipc_nl_bearer_disable;
578 doit.transcode = tipc_nl_compat_bearer_disable;
579 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100580 case TIPC_CMD_SHOW_LINK_STATS:
581 msg->req_type = TIPC_TLV_LINK_NAME;
582 msg->rep_size = ULTRA_STRING_MAX_LEN;
583 msg->rep_type = TIPC_TLV_ULTRA_STRING;
584 dump.dumpit = tipc_nl_link_dump;
585 dump.format = tipc_nl_compat_link_stat_dump;
586 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped0796d12015-02-09 09:50:04 +0100587 }
588
589 return -EOPNOTSUPP;
590}
591
592static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
593{
594 int err;
595 int len;
596 struct tipc_nl_compat_msg msg;
597 struct nlmsghdr *req_nlh;
598 struct nlmsghdr *rep_nlh;
599 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
600 struct net *net = genl_info_net(info);
601
602 memset(&msg, 0, sizeof(msg));
603
604 req_nlh = (struct nlmsghdr *)skb->data;
605 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
606 msg.cmd = req_userhdr->cmd;
607 msg.dst_sk = info->dst_sk;
608
609 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
610 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
611 err = -EACCES;
612 goto send;
613 }
614
615 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
616 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
617 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
618 err = -EOPNOTSUPP;
619 goto send;
620 }
621
622 err = tipc_nl_compat_handle(&msg);
623 if (err == -EOPNOTSUPP)
624 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
625 else if (err == -EINVAL)
626 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
627send:
628 if (!msg.rep)
629 return err;
630
631 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
632 skb_push(msg.rep, len);
633 rep_nlh = nlmsg_hdr(msg.rep);
634 memcpy(rep_nlh, info->nlhdr, len);
635 rep_nlh->nlmsg_len = msg.rep->len;
636 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
637
638 return err;
639}
640
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100641static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
642{
643 struct net *net = genl_info_net(info);
644 struct sk_buff *rep_buf;
645 struct nlmsghdr *rep_nlh;
646 struct nlmsghdr *req_nlh = info->nlhdr;
647 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
648 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
649 u16 cmd;
650
651 if ((req_userhdr->cmd & 0xC000) &&
652 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
653 cmd = TIPC_CMD_NOT_NET_ADMIN;
654 else
655 cmd = req_userhdr->cmd;
656
657 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
658 nlmsg_data(req_nlh) + GENL_HDRLEN +
659 TIPC_GENL_HDRLEN,
660 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
661 TIPC_GENL_HDRLEN), hdr_space);
662
663 if (rep_buf) {
664 skb_push(rep_buf, hdr_space);
665 rep_nlh = nlmsg_hdr(rep_buf);
666 memcpy(rep_nlh, req_nlh, hdr_space);
667 rep_nlh->nlmsg_len = rep_buf->len;
668 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
669 }
670
671 return 0;
672}
673
Richard Alped0796d12015-02-09 09:50:04 +0100674/* Temporary function to keep functionality throughout the patchset
675 * without having to mess with the global variables and other trickery
676 * of the old API.
677 */
678static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
679{
680 struct tipc_genlmsghdr *req = info->userhdr;
681
682 switch (req->cmd) {
683 case TIPC_CMD_GET_BEARER_NAMES:
Richard Alpe9ab15462015-02-09 09:50:05 +0100684 case TIPC_CMD_ENABLE_BEARER:
685 case TIPC_CMD_DISABLE_BEARER:
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100686 case TIPC_CMD_SHOW_LINK_STATS:
Richard Alped0796d12015-02-09 09:50:04 +0100687 return tipc_nl_compat_recv(skb, info);
688 }
689
690 return handle_cmd(skb, info);
691}
692
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100693static struct genl_family tipc_genl_compat_family = {
694 .id = GENL_ID_GENERATE,
695 .name = TIPC_GENL_NAME,
696 .version = TIPC_GENL_VERSION,
697 .hdrsize = TIPC_GENL_HDRLEN,
698 .maxattr = 0,
699 .netnsok = true,
700};
701
702static struct genl_ops tipc_genl_compat_ops[] = {
703 {
704 .cmd = TIPC_GENL_CMD,
Richard Alped0796d12015-02-09 09:50:04 +0100705 .doit = tipc_nl_compat_tmp_wrap,
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100706 },
707};
708
709int tipc_netlink_compat_start(void)
710{
711 int res;
712
713 res = genl_register_family_with_ops(&tipc_genl_compat_family,
714 tipc_genl_compat_ops);
715 if (res) {
716 pr_err("Failed to register legacy compat interface\n");
717 return res;
718 }
719
720 return 0;
721}
722
723void tipc_netlink_compat_stop(void)
724{
725 genl_unregister_family(&tipc_genl_compat_family);
726}