blob: f795b1dd0ccdf3dc088e16e1e9ad3ae9cc978d16 [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"
Richard Alped0796d12015-02-09 09:50:04 +010035#include "bearer.h"
Richard Alpef2b3b2d2015-02-09 09:50:06 +010036#include "link.h"
Richard Alpe44a8ae92015-02-09 09:50:10 +010037#include "name_table.h"
Richard Alpe487d2a32015-02-09 09:50:11 +010038#include "socket.h"
Richard Alpe4b28cb52015-02-09 09:50:13 +010039#include "node.h"
Richard Alpe964f9502015-02-09 09:50:15 +010040#include "net.h"
Richard Alpebfb3e5d2015-02-09 09:50:03 +010041#include <net/genetlink.h>
42#include <linux/tipc_config.h>
43
Richard Alped0796d12015-02-09 09:50:04 +010044/* The legacy API had an artificial message length limit called
45 * ULTRA_STRING_MAX_LEN.
46 */
47#define ULTRA_STRING_MAX_LEN 32768
48
49#define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
50
51#define REPLY_TRUNCATED "<truncated>\n"
52
53struct tipc_nl_compat_msg {
54 u16 cmd;
Richard Alpef2b3b2d2015-02-09 09:50:06 +010055 int rep_type;
Richard Alped0796d12015-02-09 09:50:04 +010056 int rep_size;
Richard Alpe9ab15462015-02-09 09:50:05 +010057 int req_type;
Richard Alpec3d6fb82015-05-06 13:58:54 +020058 struct net *net;
Richard Alped0796d12015-02-09 09:50:04 +010059 struct sk_buff *rep;
60 struct tlv_desc *req;
61 struct sock *dst_sk;
62};
63
64struct tipc_nl_compat_cmd_dump {
Richard Alpe44a8ae92015-02-09 09:50:10 +010065 int (*header)(struct tipc_nl_compat_msg *);
Richard Alped0796d12015-02-09 09:50:04 +010066 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
67 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
68};
69
Richard Alpe9ab15462015-02-09 09:50:05 +010070struct tipc_nl_compat_cmd_doit {
71 int (*doit)(struct sk_buff *skb, struct genl_info *info);
Richard Alpec3d6fb82015-05-06 13:58:54 +020072 int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
73 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
Richard Alpe9ab15462015-02-09 09:50:05 +010074};
75
Richard Alped0796d12015-02-09 09:50:04 +010076static int tipc_skb_tailroom(struct sk_buff *skb)
77{
78 int tailroom;
79 int limit;
80
81 tailroom = skb_tailroom(skb);
82 limit = TIPC_SKB_MAX - skb->len;
83
84 if (tailroom < limit)
85 return tailroom;
86
87 return limit;
88}
89
90static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
91{
92 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
93
94 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
95 return -EMSGSIZE;
96
97 skb_put(skb, TLV_SPACE(len));
98 tlv->tlv_type = htons(type);
99 tlv->tlv_len = htons(TLV_LENGTH(len));
100 if (len && data)
101 memcpy(TLV_DATA(tlv), data, len);
102
103 return 0;
104}
105
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100106static void tipc_tlv_init(struct sk_buff *skb, u16 type)
107{
108 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
109
110 TLV_SET_LEN(tlv, 0);
111 TLV_SET_TYPE(tlv, type);
112 skb_put(skb, sizeof(struct tlv_desc));
113}
114
115static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
116{
117 int n;
118 u16 len;
119 u32 rem;
120 char *buf;
121 struct tlv_desc *tlv;
122 va_list args;
123
124 rem = tipc_skb_tailroom(skb);
125
126 tlv = (struct tlv_desc *)skb->data;
127 len = TLV_GET_LEN(tlv);
128 buf = TLV_DATA(tlv) + len;
129
130 va_start(args, fmt);
131 n = vscnprintf(buf, rem, fmt, args);
132 va_end(args);
133
134 TLV_SET_LEN(tlv, n + len);
135 skb_put(skb, n);
136
137 return n;
138}
139
Richard Alped0796d12015-02-09 09:50:04 +0100140static struct sk_buff *tipc_tlv_alloc(int size)
141{
142 int hdr_len;
143 struct sk_buff *buf;
144
145 size = TLV_SPACE(size);
146 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
147
148 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
149 if (!buf)
150 return NULL;
151
152 skb_reserve(buf, hdr_len);
153
154 return buf;
155}
156
157static struct sk_buff *tipc_get_err_tlv(char *str)
158{
159 int str_len = strlen(str) + 1;
160 struct sk_buff *buf;
161
162 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
163 if (buf)
164 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
165
166 return buf;
167}
168
169static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
170 struct tipc_nl_compat_msg *msg,
171 struct sk_buff *arg)
172{
173 int len = 0;
174 int err;
175 struct sk_buff *buf;
176 struct nlmsghdr *nlmsg;
177 struct netlink_callback cb;
178
179 memset(&cb, 0, sizeof(cb));
180 cb.nlh = (struct nlmsghdr *)arg->data;
181 cb.skb = arg;
182
183 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
184 if (!buf)
185 return -ENOMEM;
186
187 buf->sk = msg->dst_sk;
188
189 do {
190 int rem;
191
192 len = (*cmd->dumpit)(buf, &cb);
193
194 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
195 struct nlattr **attrs;
196
197 err = tipc_nlmsg_parse(nlmsg, &attrs);
198 if (err)
199 goto err_out;
200
201 err = (*cmd->format)(msg, attrs);
202 if (err)
203 goto err_out;
204
205 if (tipc_skb_tailroom(msg->rep) <= 1) {
206 err = -EMSGSIZE;
207 goto err_out;
208 }
209 }
210
211 skb_reset_tail_pointer(buf);
212 buf->len = 0;
213
214 } while (len);
215
216 err = 0;
217
218err_out:
219 kfree_skb(buf);
220
221 if (err == -EMSGSIZE) {
222 /* The legacy API only considered messages filling
223 * "ULTRA_STRING_MAX_LEN" to be truncated.
224 */
225 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
226 char *tail = skb_tail_pointer(msg->rep);
227
228 if (*tail != '\0')
229 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
230 REPLY_TRUNCATED);
231 }
232
233 return 0;
234 }
235
236 return err;
237}
238
239static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
240 struct tipc_nl_compat_msg *msg)
241{
242 int err;
243 struct sk_buff *arg;
244
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100245 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
246 return -EINVAL;
247
Richard Alped0796d12015-02-09 09:50:04 +0100248 msg->rep = tipc_tlv_alloc(msg->rep_size);
249 if (!msg->rep)
250 return -ENOMEM;
251
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100252 if (msg->rep_type)
253 tipc_tlv_init(msg->rep, msg->rep_type);
254
Richard Alpe44a8ae92015-02-09 09:50:10 +0100255 if (cmd->header)
256 (*cmd->header)(msg);
257
Richard Alped0796d12015-02-09 09:50:04 +0100258 arg = nlmsg_new(0, GFP_KERNEL);
259 if (!arg) {
260 kfree_skb(msg->rep);
261 return -ENOMEM;
262 }
263
264 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
265 if (err)
266 kfree_skb(msg->rep);
267
268 kfree_skb(arg);
269
270 return err;
271}
272
Richard Alpe9ab15462015-02-09 09:50:05 +0100273static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
274 struct tipc_nl_compat_msg *msg)
275{
276 int err;
277 struct sk_buff *doit_buf;
278 struct sk_buff *trans_buf;
279 struct nlattr **attrbuf;
280 struct genl_info info;
281
282 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
283 if (!trans_buf)
284 return -ENOMEM;
285
Richard Alpec3d6fb82015-05-06 13:58:54 +0200286 err = (*cmd->transcode)(cmd, trans_buf, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100287 if (err)
288 goto trans_out;
289
290 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
291 sizeof(struct nlattr *), GFP_KERNEL);
292 if (!attrbuf) {
293 err = -ENOMEM;
294 goto trans_out;
295 }
296
297 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
298 (const struct nlattr *)trans_buf->data,
299 trans_buf->len, NULL);
300 if (err)
301 goto parse_out;
302
303 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
304 if (!doit_buf) {
305 err = -ENOMEM;
306 goto parse_out;
307 }
308
309 doit_buf->sk = msg->dst_sk;
310
311 memset(&info, 0, sizeof(info));
312 info.attrs = attrbuf;
313
314 err = (*cmd->doit)(doit_buf, &info);
315
316 kfree_skb(doit_buf);
317parse_out:
318 kfree(attrbuf);
319trans_out:
320 kfree_skb(trans_buf);
321
322 return err;
323}
324
325static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
326 struct tipc_nl_compat_msg *msg)
327{
328 int err;
329
330 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
331 return -EINVAL;
332
333 err = __tipc_nl_compat_doit(cmd, msg);
334 if (err)
335 return err;
336
337 /* The legacy API considered an empty message a success message */
338 msg->rep = tipc_tlv_alloc(0);
339 if (!msg->rep)
340 return -ENOMEM;
341
342 return 0;
343}
344
Richard Alped0796d12015-02-09 09:50:04 +0100345static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
346 struct nlattr **attrs)
347{
348 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800349 int err;
Richard Alped0796d12015-02-09 09:50:04 +0100350
Baozeng Ding297f7d22016-05-24 22:33:24 +0800351 if (!attrs[TIPC_NLA_BEARER])
352 return -EINVAL;
353
354 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
355 attrs[TIPC_NLA_BEARER], NULL);
356 if (err)
357 return err;
Richard Alped0796d12015-02-09 09:50:04 +0100358
359 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
360 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
361 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
362}
363
Richard Alpec3d6fb82015-05-06 13:58:54 +0200364static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
365 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100366 struct tipc_nl_compat_msg *msg)
367{
368 struct nlattr *prop;
369 struct nlattr *bearer;
370 struct tipc_bearer_config *b;
371
372 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
373
374 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
375 if (!bearer)
376 return -EMSGSIZE;
377
378 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
379 return -EMSGSIZE;
380
381 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
382 return -EMSGSIZE;
383
384 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
385 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
386 if (!prop)
387 return -EMSGSIZE;
388 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
389 return -EMSGSIZE;
390 nla_nest_end(skb, prop);
391 }
392 nla_nest_end(skb, bearer);
393
394 return 0;
395}
396
Richard Alpec3d6fb82015-05-06 13:58:54 +0200397static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
398 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100399 struct tipc_nl_compat_msg *msg)
400{
401 char *name;
402 struct nlattr *bearer;
403
404 name = (char *)TLV_DATA(msg->req);
405
406 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
407 if (!bearer)
408 return -EMSGSIZE;
409
410 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
411 return -EMSGSIZE;
412
413 nla_nest_end(skb, bearer);
414
415 return 0;
416}
417
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100418static inline u32 perc(u32 count, u32 total)
419{
420 return (count * 100 + (total / 2)) / total;
421}
422
423static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
424 struct nlattr *prop[], struct nlattr *stats[])
425{
426 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
427 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
428
429 tipc_tlv_sprintf(msg->rep,
430 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
431 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
432 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
433 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
434 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
435 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
436
437 tipc_tlv_sprintf(msg->rep,
438 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
439 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
440 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
441 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
442 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
443 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
444
445 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
446 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
447 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
448 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
449
450 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
451 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
452 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
453 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
454
455 tipc_tlv_sprintf(msg->rep,
456 " Congestion link:%u Send queue max:%u avg:%u",
457 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
458 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
459 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
460}
461
462static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
463 struct nlattr **attrs)
464{
465 char *name;
466 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
467 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
468 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800469 int err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100470
Baozeng Ding297f7d22016-05-24 22:33:24 +0800471 if (!attrs[TIPC_NLA_LINK])
472 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100473
Baozeng Ding297f7d22016-05-24 22:33:24 +0800474 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
475 NULL);
476 if (err)
477 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100478
Baozeng Ding297f7d22016-05-24 22:33:24 +0800479 if (!link[TIPC_NLA_LINK_PROP])
480 return -EINVAL;
481
482 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
483 link[TIPC_NLA_LINK_PROP], NULL);
484 if (err)
485 return err;
486
487 if (!link[TIPC_NLA_LINK_STATS])
488 return -EINVAL;
489
490 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
491 link[TIPC_NLA_LINK_STATS], NULL);
492 if (err)
493 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100494
495 name = (char *)TLV_DATA(msg->req);
496 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
497 return 0;
498
499 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
500 nla_data(link[TIPC_NLA_LINK_NAME]));
501
502 if (link[TIPC_NLA_LINK_BROADCAST]) {
503 __fill_bc_link_stat(msg, prop, stats);
504 return 0;
505 }
506
507 if (link[TIPC_NLA_LINK_ACTIVE])
508 tipc_tlv_sprintf(msg->rep, " ACTIVE");
509 else if (link[TIPC_NLA_LINK_UP])
510 tipc_tlv_sprintf(msg->rep, " STANDBY");
511 else
512 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
513
514 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
515 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
516 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
517
518 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
519 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
520 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
521
522 tipc_tlv_sprintf(msg->rep,
523 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
524 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
525 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
526 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
527 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
528 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
529 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
530
531 tipc_tlv_sprintf(msg->rep,
532 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
533 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
534 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
535 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
536 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
537 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
538 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
539
540 tipc_tlv_sprintf(msg->rep,
541 " TX profile sample:%u packets average:%u octets\n",
542 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
543 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
544 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
545
546 tipc_tlv_sprintf(msg->rep,
547 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
548 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
549 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
550 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
551 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
552 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
553 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
554 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
555 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
556
557 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
558 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
559 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
560 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
561 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
562 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
563 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
564
565 tipc_tlv_sprintf(msg->rep,
566 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
567 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
568 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
569 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
570 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
571 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
572
573 tipc_tlv_sprintf(msg->rep,
574 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
575 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
576 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
577 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
578 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
579 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
580
581 tipc_tlv_sprintf(msg->rep,
582 " Congestion link:%u Send queue max:%u avg:%u",
583 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
584 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
585 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
586
587 return 0;
588}
589
Richard Alpe357ebdb2015-02-09 09:50:07 +0100590static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
591 struct nlattr **attrs)
592{
593 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
594 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800595 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100596
Baozeng Ding297f7d22016-05-24 22:33:24 +0800597 if (!attrs[TIPC_NLA_LINK])
598 return -EINVAL;
599
600 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
601 NULL);
602 if (err)
603 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100604
605 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
606 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
607 strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
608
609 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
610 &link_info, sizeof(link_info));
611}
612
Richard Alpec3d6fb82015-05-06 13:58:54 +0200613static int __tipc_add_link_prop(struct sk_buff *skb,
614 struct tipc_nl_compat_msg *msg,
615 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100616{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200617 switch (msg->cmd) {
618 case TIPC_CMD_SET_LINK_PRI:
619 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
620 case TIPC_CMD_SET_LINK_TOL:
621 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
622 case TIPC_CMD_SET_LINK_WINDOW:
623 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
624 }
625
626 return -EINVAL;
627}
628
629static int tipc_nl_compat_media_set(struct sk_buff *skb,
630 struct tipc_nl_compat_msg *msg)
631{
Richard Alpe37e2d482015-02-09 09:50:08 +0100632 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200633 struct nlattr *media;
634 struct tipc_link_config *lc;
635
636 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
637
638 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
639 if (!media)
640 return -EMSGSIZE;
641
642 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
643 return -EMSGSIZE;
644
645 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
646 if (!prop)
647 return -EMSGSIZE;
648
649 __tipc_add_link_prop(skb, msg, lc);
650 nla_nest_end(skb, prop);
651 nla_nest_end(skb, media);
652
653 return 0;
654}
655
656static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
657 struct tipc_nl_compat_msg *msg)
658{
659 struct nlattr *prop;
660 struct nlattr *bearer;
661 struct tipc_link_config *lc;
662
663 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
664
665 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
666 if (!bearer)
667 return -EMSGSIZE;
668
669 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
670 return -EMSGSIZE;
671
672 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
673 if (!prop)
674 return -EMSGSIZE;
675
676 __tipc_add_link_prop(skb, msg, lc);
677 nla_nest_end(skb, prop);
678 nla_nest_end(skb, bearer);
679
680 return 0;
681}
682
683static int __tipc_nl_compat_link_set(struct sk_buff *skb,
684 struct tipc_nl_compat_msg *msg)
685{
686 struct nlattr *prop;
687 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100688 struct tipc_link_config *lc;
689
690 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
691
692 link = nla_nest_start(skb, TIPC_NLA_LINK);
693 if (!link)
694 return -EMSGSIZE;
695
696 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
697 return -EMSGSIZE;
698
699 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
700 if (!prop)
701 return -EMSGSIZE;
702
Richard Alpec3d6fb82015-05-06 13:58:54 +0200703 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100704 nla_nest_end(skb, prop);
705 nla_nest_end(skb, link);
706
707 return 0;
708}
709
Richard Alpec3d6fb82015-05-06 13:58:54 +0200710static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
711 struct sk_buff *skb,
712 struct tipc_nl_compat_msg *msg)
713{
714 struct tipc_link_config *lc;
715 struct tipc_bearer *bearer;
716 struct tipc_media *media;
717
718 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
719
720 media = tipc_media_find(lc->name);
721 if (media) {
722 cmd->doit = &tipc_nl_media_set;
723 return tipc_nl_compat_media_set(skb, msg);
724 }
725
726 bearer = tipc_bearer_find(msg->net, lc->name);
727 if (bearer) {
728 cmd->doit = &tipc_nl_bearer_set;
729 return tipc_nl_compat_bearer_set(skb, msg);
730 }
731
732 return __tipc_nl_compat_link_set(skb, msg);
733}
734
735static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
736 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100737 struct tipc_nl_compat_msg *msg)
738{
739 char *name;
740 struct nlattr *link;
741
742 name = (char *)TLV_DATA(msg->req);
743
744 link = nla_nest_start(skb, TIPC_NLA_LINK);
745 if (!link)
746 return -EMSGSIZE;
747
748 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
749 return -EMSGSIZE;
750
751 nla_nest_end(skb, link);
752
753 return 0;
754}
755
Richard Alpe44a8ae92015-02-09 09:50:10 +0100756static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
757{
758 int i;
759 u32 depth;
760 struct tipc_name_table_query *ntq;
761 static const char * const header[] = {
762 "Type ",
763 "Lower Upper ",
764 "Port Identity ",
765 "Publication Scope"
766 };
767
768 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
769
770 depth = ntohl(ntq->depth);
771
772 if (depth > 4)
773 depth = 4;
774 for (i = 0; i < depth; i++)
775 tipc_tlv_sprintf(msg->rep, header[i]);
776 tipc_tlv_sprintf(msg->rep, "\n");
777
778 return 0;
779}
780
781static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
782 struct nlattr **attrs)
783{
784 char port_str[27];
785 struct tipc_name_table_query *ntq;
786 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
787 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
788 u32 node, depth, type, lowbound, upbound;
789 static const char * const scope_str[] = {"", " zone", " cluster",
790 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800791 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100792
Baozeng Ding297f7d22016-05-24 22:33:24 +0800793 if (!attrs[TIPC_NLA_NAME_TABLE])
794 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100795
Baozeng Ding297f7d22016-05-24 22:33:24 +0800796 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
797 attrs[TIPC_NLA_NAME_TABLE], NULL);
798 if (err)
799 return err;
800
801 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
802 return -EINVAL;
803
804 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
805 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
806 if (err)
807 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100808
809 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
810
811 depth = ntohl(ntq->depth);
812 type = ntohl(ntq->type);
813 lowbound = ntohl(ntq->lowbound);
814 upbound = ntohl(ntq->upbound);
815
816 if (!(depth & TIPC_NTQ_ALLTYPES) &&
817 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
818 return 0;
819 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
820 return 0;
821 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
822 return 0;
823
824 tipc_tlv_sprintf(msg->rep, "%-10u ",
825 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
826
827 if (depth == 1)
828 goto out;
829
830 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
831 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
832 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
833
834 if (depth == 2)
835 goto out;
836
837 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
838 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
839 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
840 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
841
842 if (depth == 3)
843 goto out;
844
845 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200846 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100847 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
848out:
849 tipc_tlv_sprintf(msg->rep, "\n");
850
851 return 0;
852}
853
Richard Alpe487d2a32015-02-09 09:50:11 +0100854static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
855 struct nlattr **attrs)
856{
857 u32 type, lower, upper;
858 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800859 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100860
Baozeng Ding297f7d22016-05-24 22:33:24 +0800861 if (!attrs[TIPC_NLA_PUBL])
862 return -EINVAL;
863
864 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
865 NULL);
866 if (err)
867 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100868
869 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
870 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
871 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
872
873 if (lower == upper)
874 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
875 else
876 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
877
878 return 0;
879}
880
881static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
882{
883 int err;
884 void *hdr;
885 struct nlattr *nest;
886 struct sk_buff *args;
887 struct tipc_nl_compat_cmd_dump dump;
888
889 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
890 if (!args)
891 return -ENOMEM;
892
893 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
894 TIPC_NL_PUBL_GET);
895
896 nest = nla_nest_start(args, TIPC_NLA_SOCK);
897 if (!nest) {
898 kfree_skb(args);
899 return -EMSGSIZE;
900 }
901
902 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
903 kfree_skb(args);
904 return -EMSGSIZE;
905 }
906
907 nla_nest_end(args, nest);
908 genlmsg_end(args, hdr);
909
910 dump.dumpit = tipc_nl_publ_dump;
911 dump.format = __tipc_nl_compat_publ_dump;
912
913 err = __tipc_nl_compat_dumpit(&dump, msg, args);
914
915 kfree_skb(args);
916
917 return err;
918}
919
920static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
921 struct nlattr **attrs)
922{
923 int err;
924 u32 sock_ref;
925 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
926
Baozeng Ding297f7d22016-05-24 22:33:24 +0800927 if (!attrs[TIPC_NLA_SOCK])
928 return -EINVAL;
929
930 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
931 NULL);
932 if (err)
933 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100934
935 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
936 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
937
938 if (sock[TIPC_NLA_SOCK_CON]) {
939 u32 node;
940 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
941
942 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
943 NULL);
944
945 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
946 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
947 tipc_zone(node),
948 tipc_cluster(node),
949 tipc_node(node),
950 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
951
952 if (con[TIPC_NLA_CON_FLAG])
953 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
954 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
955 nla_get_u32(con[TIPC_NLA_CON_INST]));
956 else
957 tipc_tlv_sprintf(msg->rep, "\n");
958 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
959 tipc_tlv_sprintf(msg->rep, " bound to");
960
961 err = tipc_nl_compat_publ_dump(msg, sock_ref);
962 if (err)
963 return err;
964 }
965 tipc_tlv_sprintf(msg->rep, "\n");
966
967 return 0;
968}
969
Richard Alpe5bfc3352015-02-09 09:50:12 +0100970static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
971 struct nlattr **attrs)
972{
973 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800974 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +0100975
Baozeng Ding297f7d22016-05-24 22:33:24 +0800976 if (!attrs[TIPC_NLA_MEDIA])
977 return -EINVAL;
978
979 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
980 NULL);
981 if (err)
982 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +0100983
984 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
985 nla_data(media[TIPC_NLA_MEDIA_NAME]),
986 nla_len(media[TIPC_NLA_MEDIA_NAME]));
987}
988
Richard Alpe4b28cb52015-02-09 09:50:13 +0100989static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
990 struct nlattr **attrs)
991{
992 struct tipc_node_info node_info;
993 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800994 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +0100995
Baozeng Ding297f7d22016-05-24 22:33:24 +0800996 if (!attrs[TIPC_NLA_NODE])
997 return -EINVAL;
998
999 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1000 NULL);
1001 if (err)
1002 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001003
1004 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1005 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1006
1007 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1008 sizeof(node_info));
1009}
1010
Richard Alpec3d6fb82015-05-06 13:58:54 +02001011static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1012 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001013 struct tipc_nl_compat_msg *msg)
1014{
1015 u32 val;
1016 struct nlattr *net;
1017
1018 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1019
1020 net = nla_nest_start(skb, TIPC_NLA_NET);
1021 if (!net)
1022 return -EMSGSIZE;
1023
Richard Alpe964f9502015-02-09 09:50:15 +01001024 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1025 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1026 return -EMSGSIZE;
1027 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1028 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1029 return -EMSGSIZE;
1030 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001031 nla_nest_end(skb, net);
1032
1033 return 0;
1034}
1035
Richard Alpe3c261812015-02-09 09:50:16 +01001036static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1037 struct nlattr **attrs)
1038{
1039 __be32 id;
1040 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001041 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001042
Baozeng Ding297f7d22016-05-24 22:33:24 +08001043 if (!attrs[TIPC_NLA_NET])
1044 return -EINVAL;
1045
1046 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1047 NULL);
1048 if (err)
1049 return err;
1050
Richard Alpe3c261812015-02-09 09:50:16 +01001051 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1052
1053 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1054}
1055
Richard Alpe5a81a632015-02-09 09:50:17 +01001056static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1057{
1058 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1059 if (!msg->rep)
1060 return -ENOMEM;
1061
1062 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1063 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1064
1065 return 0;
1066}
1067
Richard Alped0796d12015-02-09 09:50:04 +01001068static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1069{
1070 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001071 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001072
1073 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001074 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001075
1076 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001077 case TIPC_CMD_NOOP:
1078 msg->rep = tipc_tlv_alloc(0);
1079 if (!msg->rep)
1080 return -ENOMEM;
1081 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001082 case TIPC_CMD_GET_BEARER_NAMES:
1083 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1084 dump.dumpit = tipc_nl_bearer_dump;
1085 dump.format = tipc_nl_compat_bearer_dump;
1086 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001087 case TIPC_CMD_ENABLE_BEARER:
1088 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1089 doit.doit = tipc_nl_bearer_enable;
1090 doit.transcode = tipc_nl_compat_bearer_enable;
1091 return tipc_nl_compat_doit(&doit, msg);
1092 case TIPC_CMD_DISABLE_BEARER:
1093 msg->req_type = TIPC_TLV_BEARER_NAME;
1094 doit.doit = tipc_nl_bearer_disable;
1095 doit.transcode = tipc_nl_compat_bearer_disable;
1096 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001097 case TIPC_CMD_SHOW_LINK_STATS:
1098 msg->req_type = TIPC_TLV_LINK_NAME;
1099 msg->rep_size = ULTRA_STRING_MAX_LEN;
1100 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001101 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001102 dump.format = tipc_nl_compat_link_stat_dump;
1103 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001104 case TIPC_CMD_GET_LINKS:
1105 msg->req_type = TIPC_TLV_NET_ADDR;
1106 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001107 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001108 dump.format = tipc_nl_compat_link_dump;
1109 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001110 case TIPC_CMD_SET_LINK_TOL:
1111 case TIPC_CMD_SET_LINK_PRI:
1112 case TIPC_CMD_SET_LINK_WINDOW:
1113 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001114 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001115 doit.transcode = tipc_nl_compat_link_set;
1116 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001117 case TIPC_CMD_RESET_LINK_STATS:
1118 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001119 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001120 doit.transcode = tipc_nl_compat_link_reset_stats;
1121 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001122 case TIPC_CMD_SHOW_NAME_TABLE:
1123 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1124 msg->rep_size = ULTRA_STRING_MAX_LEN;
1125 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1126 dump.header = tipc_nl_compat_name_table_dump_header;
1127 dump.dumpit = tipc_nl_name_table_dump;
1128 dump.format = tipc_nl_compat_name_table_dump;
1129 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001130 case TIPC_CMD_SHOW_PORTS:
1131 msg->rep_size = ULTRA_STRING_MAX_LEN;
1132 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1133 dump.dumpit = tipc_nl_sk_dump;
1134 dump.format = tipc_nl_compat_sk_dump;
1135 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001136 case TIPC_CMD_GET_MEDIA_NAMES:
1137 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1138 dump.dumpit = tipc_nl_media_dump;
1139 dump.format = tipc_nl_compat_media_dump;
1140 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001141 case TIPC_CMD_GET_NODES:
1142 msg->rep_size = ULTRA_STRING_MAX_LEN;
1143 dump.dumpit = tipc_nl_node_dump;
1144 dump.format = tipc_nl_compat_node_dump;
1145 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001146 case TIPC_CMD_SET_NODE_ADDR:
1147 msg->req_type = TIPC_TLV_NET_ADDR;
1148 doit.doit = tipc_nl_net_set;
1149 doit.transcode = tipc_nl_compat_net_set;
1150 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001151 case TIPC_CMD_SET_NETID:
1152 msg->req_type = TIPC_TLV_UNSIGNED;
1153 doit.doit = tipc_nl_net_set;
1154 doit.transcode = tipc_nl_compat_net_set;
1155 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001156 case TIPC_CMD_GET_NETID:
1157 msg->rep_size = sizeof(u32);
1158 dump.dumpit = tipc_nl_net_dump;
1159 dump.format = tipc_nl_compat_net_dump;
1160 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001161 case TIPC_CMD_SHOW_STATS:
1162 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001163 }
1164
1165 return -EOPNOTSUPP;
1166}
1167
1168static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1169{
1170 int err;
1171 int len;
1172 struct tipc_nl_compat_msg msg;
1173 struct nlmsghdr *req_nlh;
1174 struct nlmsghdr *rep_nlh;
1175 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001176
1177 memset(&msg, 0, sizeof(msg));
1178
1179 req_nlh = (struct nlmsghdr *)skb->data;
1180 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1181 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001182 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001183 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001184
1185 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1186 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1187 err = -EACCES;
1188 goto send;
1189 }
1190
1191 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Richard Alpe8f8ff9132015-08-17 14:15:10 +02001192 if (len && !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001193 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1194 err = -EOPNOTSUPP;
1195 goto send;
1196 }
1197
1198 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001199 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001200 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1201 else if (err == -EINVAL)
1202 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1203send:
1204 if (!msg.rep)
1205 return err;
1206
1207 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1208 skb_push(msg.rep, len);
1209 rep_nlh = nlmsg_hdr(msg.rep);
1210 memcpy(rep_nlh, info->nlhdr, len);
1211 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001212 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001213
1214 return err;
1215}
1216
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001217static struct genl_family tipc_genl_compat_family = {
1218 .id = GENL_ID_GENERATE,
1219 .name = TIPC_GENL_NAME,
1220 .version = TIPC_GENL_VERSION,
1221 .hdrsize = TIPC_GENL_HDRLEN,
1222 .maxattr = 0,
1223 .netnsok = true,
1224};
1225
1226static struct genl_ops tipc_genl_compat_ops[] = {
1227 {
1228 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001229 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001230 },
1231};
1232
1233int tipc_netlink_compat_start(void)
1234{
1235 int res;
1236
1237 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1238 tipc_genl_compat_ops);
1239 if (res) {
1240 pr_err("Failed to register legacy compat interface\n");
1241 return res;
1242 }
1243
1244 return 0;
1245}
1246
1247void tipc_netlink_compat_stop(void)
1248{
1249 genl_unregister_family(&tipc_genl_compat_family);
1250}