blob: 974169059b9cd7facec6a435bdb647182c02ee36 [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 Alpef2b3b2d42015-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 Alpef2b3b2d42015-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 Alpef2b3b2d42015-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 Alpef2b3b2d42015-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 Alpef2b3b2d42015-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);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700261 msg->rep = NULL;
Richard Alped0796d12015-02-09 09:50:04 +0100262 return -ENOMEM;
263 }
264
265 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700266 if (err) {
Richard Alped0796d12015-02-09 09:50:04 +0100267 kfree_skb(msg->rep);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700268 msg->rep = NULL;
269 }
Richard Alped0796d12015-02-09 09:50:04 +0100270 kfree_skb(arg);
271
272 return err;
273}
274
Richard Alpe9ab15462015-02-09 09:50:05 +0100275static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
276 struct tipc_nl_compat_msg *msg)
277{
278 int err;
279 struct sk_buff *doit_buf;
280 struct sk_buff *trans_buf;
281 struct nlattr **attrbuf;
282 struct genl_info info;
283
284 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
285 if (!trans_buf)
286 return -ENOMEM;
287
Richard Alpe9ab15462015-02-09 09:50:05 +0100288 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
289 sizeof(struct nlattr *), GFP_KERNEL);
290 if (!attrbuf) {
291 err = -ENOMEM;
292 goto trans_out;
293 }
294
Richard Alpe9ab15462015-02-09 09:50:05 +0100295 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
296 if (!doit_buf) {
297 err = -ENOMEM;
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800298 goto attrbuf_out;
Richard Alpe9ab15462015-02-09 09:50:05 +0100299 }
300
Richard Alpe9ab15462015-02-09 09:50:05 +0100301 memset(&info, 0, sizeof(info));
302 info.attrs = attrbuf;
303
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800304 err = (*cmd->transcode)(cmd, trans_buf, msg);
305 if (err)
306 goto doit_out;
307
308 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
309 (const struct nlattr *)trans_buf->data,
310 trans_buf->len, NULL, NULL);
311 if (err)
312 goto doit_out;
313
314 doit_buf->sk = msg->dst_sk;
315
Richard Alpe9ab15462015-02-09 09:50:05 +0100316 err = (*cmd->doit)(doit_buf, &info);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800317doit_out:
Richard Alpe9ab15462015-02-09 09:50:05 +0100318
319 kfree_skb(doit_buf);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800320attrbuf_out:
Richard Alpe9ab15462015-02-09 09:50:05 +0100321 kfree(attrbuf);
322trans_out:
323 kfree_skb(trans_buf);
324
325 return err;
326}
327
328static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
329 struct tipc_nl_compat_msg *msg)
330{
331 int err;
332
333 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
334 return -EINVAL;
335
336 err = __tipc_nl_compat_doit(cmd, msg);
337 if (err)
338 return err;
339
340 /* The legacy API considered an empty message a success message */
341 msg->rep = tipc_tlv_alloc(0);
342 if (!msg->rep)
343 return -ENOMEM;
344
345 return 0;
346}
347
Richard Alped0796d12015-02-09 09:50:04 +0100348static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
349 struct nlattr **attrs)
350{
351 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800352 int err;
Richard Alped0796d12015-02-09 09:50:04 +0100353
Baozeng Ding297f7d22016-05-24 22:33:24 +0800354 if (!attrs[TIPC_NLA_BEARER])
355 return -EINVAL;
356
357 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200358 attrs[TIPC_NLA_BEARER], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800359 if (err)
360 return err;
Richard Alped0796d12015-02-09 09:50:04 +0100361
362 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
363 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
364 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
365}
366
Richard Alpec3d6fb82015-05-06 13:58:54 +0200367static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
368 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100369 struct tipc_nl_compat_msg *msg)
370{
371 struct nlattr *prop;
372 struct nlattr *bearer;
373 struct tipc_bearer_config *b;
374
375 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
376
377 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
378 if (!bearer)
379 return -EMSGSIZE;
380
381 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
382 return -EMSGSIZE;
383
384 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
385 return -EMSGSIZE;
386
387 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
388 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
389 if (!prop)
390 return -EMSGSIZE;
391 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
392 return -EMSGSIZE;
393 nla_nest_end(skb, prop);
394 }
395 nla_nest_end(skb, bearer);
396
397 return 0;
398}
399
Richard Alpec3d6fb82015-05-06 13:58:54 +0200400static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
401 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100402 struct tipc_nl_compat_msg *msg)
403{
404 char *name;
405 struct nlattr *bearer;
406
407 name = (char *)TLV_DATA(msg->req);
408
409 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
410 if (!bearer)
411 return -EMSGSIZE;
412
413 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
414 return -EMSGSIZE;
415
416 nla_nest_end(skb, bearer);
417
418 return 0;
419}
420
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100421static inline u32 perc(u32 count, u32 total)
422{
423 return (count * 100 + (total / 2)) / total;
424}
425
426static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
427 struct nlattr *prop[], struct nlattr *stats[])
428{
429 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
430 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
431
432 tipc_tlv_sprintf(msg->rep,
433 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
434 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
435 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
436 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
437 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
438 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
439
440 tipc_tlv_sprintf(msg->rep,
441 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
442 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
443 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
444 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
445 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
446 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
447
448 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
449 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
450 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
451 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
452
453 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
454 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
455 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
456 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
457
458 tipc_tlv_sprintf(msg->rep,
459 " Congestion link:%u Send queue max:%u avg:%u",
460 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
461 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
462 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
463}
464
465static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
466 struct nlattr **attrs)
467{
468 char *name;
469 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
470 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
471 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800472 int err;
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100473
Baozeng Ding297f7d22016-05-24 22:33:24 +0800474 if (!attrs[TIPC_NLA_LINK])
475 return -EINVAL;
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100476
Baozeng Ding297f7d22016-05-24 22:33:24 +0800477 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200478 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800479 if (err)
480 return err;
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100481
Baozeng Ding297f7d22016-05-24 22:33:24 +0800482 if (!link[TIPC_NLA_LINK_PROP])
483 return -EINVAL;
484
485 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200486 link[TIPC_NLA_LINK_PROP], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800487 if (err)
488 return err;
489
490 if (!link[TIPC_NLA_LINK_STATS])
491 return -EINVAL;
492
493 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200494 link[TIPC_NLA_LINK_STATS], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800495 if (err)
496 return err;
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100497
498 name = (char *)TLV_DATA(msg->req);
499 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
500 return 0;
501
502 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
503 nla_data(link[TIPC_NLA_LINK_NAME]));
504
505 if (link[TIPC_NLA_LINK_BROADCAST]) {
506 __fill_bc_link_stat(msg, prop, stats);
507 return 0;
508 }
509
510 if (link[TIPC_NLA_LINK_ACTIVE])
511 tipc_tlv_sprintf(msg->rep, " ACTIVE");
512 else if (link[TIPC_NLA_LINK_UP])
513 tipc_tlv_sprintf(msg->rep, " STANDBY");
514 else
515 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
516
517 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
518 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
519 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
520
521 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
522 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
523 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
524
525 tipc_tlv_sprintf(msg->rep,
526 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
527 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
528 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
529 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
530 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
531 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
532 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
533
534 tipc_tlv_sprintf(msg->rep,
535 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
536 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
537 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
538 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
539 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
540 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
541 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
542
543 tipc_tlv_sprintf(msg->rep,
544 " TX profile sample:%u packets average:%u octets\n",
545 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
546 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
547 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
548
549 tipc_tlv_sprintf(msg->rep,
550 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
551 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
552 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
553 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
554 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
555 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
556 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
557 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
558 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
559
560 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
561 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
562 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
563 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
564 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
565 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
566 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
567
568 tipc_tlv_sprintf(msg->rep,
569 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
570 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
571 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
572 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
573 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
574 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
575
576 tipc_tlv_sprintf(msg->rep,
577 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
578 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
579 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
580 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
581 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
582 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
583
584 tipc_tlv_sprintf(msg->rep,
585 " Congestion link:%u Send queue max:%u avg:%u",
586 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
587 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
588 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
589
590 return 0;
591}
592
Richard Alpe357ebdb2015-02-09 09:50:07 +0100593static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
594 struct nlattr **attrs)
595{
596 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
597 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800598 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100599
Baozeng Ding297f7d22016-05-24 22:33:24 +0800600 if (!attrs[TIPC_NLA_LINK])
601 return -EINVAL;
602
603 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200604 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800605 if (err)
606 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100607
608 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
609 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200610 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400611 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100612
613 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
614 &link_info, sizeof(link_info));
615}
616
Richard Alpec3d6fb82015-05-06 13:58:54 +0200617static int __tipc_add_link_prop(struct sk_buff *skb,
618 struct tipc_nl_compat_msg *msg,
619 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100620{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200621 switch (msg->cmd) {
622 case TIPC_CMD_SET_LINK_PRI:
623 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
624 case TIPC_CMD_SET_LINK_TOL:
625 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
626 case TIPC_CMD_SET_LINK_WINDOW:
627 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
628 }
629
630 return -EINVAL;
631}
632
633static int tipc_nl_compat_media_set(struct sk_buff *skb,
634 struct tipc_nl_compat_msg *msg)
635{
Richard Alpe37e2d482015-02-09 09:50:08 +0100636 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200637 struct nlattr *media;
638 struct tipc_link_config *lc;
639
640 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
641
642 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
643 if (!media)
644 return -EMSGSIZE;
645
646 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
647 return -EMSGSIZE;
648
649 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
650 if (!prop)
651 return -EMSGSIZE;
652
653 __tipc_add_link_prop(skb, msg, lc);
654 nla_nest_end(skb, prop);
655 nla_nest_end(skb, media);
656
657 return 0;
658}
659
660static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
661 struct tipc_nl_compat_msg *msg)
662{
663 struct nlattr *prop;
664 struct nlattr *bearer;
665 struct tipc_link_config *lc;
666
667 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
668
669 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
670 if (!bearer)
671 return -EMSGSIZE;
672
673 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
674 return -EMSGSIZE;
675
676 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
677 if (!prop)
678 return -EMSGSIZE;
679
680 __tipc_add_link_prop(skb, msg, lc);
681 nla_nest_end(skb, prop);
682 nla_nest_end(skb, bearer);
683
684 return 0;
685}
686
687static int __tipc_nl_compat_link_set(struct sk_buff *skb,
688 struct tipc_nl_compat_msg *msg)
689{
690 struct nlattr *prop;
691 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100692 struct tipc_link_config *lc;
693
694 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
695
696 link = nla_nest_start(skb, TIPC_NLA_LINK);
697 if (!link)
698 return -EMSGSIZE;
699
700 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
701 return -EMSGSIZE;
702
703 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
704 if (!prop)
705 return -EMSGSIZE;
706
Richard Alpec3d6fb82015-05-06 13:58:54 +0200707 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100708 nla_nest_end(skb, prop);
709 nla_nest_end(skb, link);
710
711 return 0;
712}
713
Richard Alpec3d6fb82015-05-06 13:58:54 +0200714static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
715 struct sk_buff *skb,
716 struct tipc_nl_compat_msg *msg)
717{
718 struct tipc_link_config *lc;
719 struct tipc_bearer *bearer;
720 struct tipc_media *media;
721
722 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
723
724 media = tipc_media_find(lc->name);
725 if (media) {
726 cmd->doit = &tipc_nl_media_set;
727 return tipc_nl_compat_media_set(skb, msg);
728 }
729
730 bearer = tipc_bearer_find(msg->net, lc->name);
731 if (bearer) {
732 cmd->doit = &tipc_nl_bearer_set;
733 return tipc_nl_compat_bearer_set(skb, msg);
734 }
735
736 return __tipc_nl_compat_link_set(skb, msg);
737}
738
739static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
740 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100741 struct tipc_nl_compat_msg *msg)
742{
743 char *name;
744 struct nlattr *link;
745
746 name = (char *)TLV_DATA(msg->req);
747
748 link = nla_nest_start(skb, TIPC_NLA_LINK);
749 if (!link)
750 return -EMSGSIZE;
751
752 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
753 return -EMSGSIZE;
754
755 nla_nest_end(skb, link);
756
757 return 0;
758}
759
Richard Alpe44a8ae92015-02-09 09:50:10 +0100760static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
761{
762 int i;
763 u32 depth;
764 struct tipc_name_table_query *ntq;
765 static const char * const header[] = {
766 "Type ",
767 "Lower Upper ",
768 "Port Identity ",
769 "Publication Scope"
770 };
771
772 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
773
774 depth = ntohl(ntq->depth);
775
776 if (depth > 4)
777 depth = 4;
778 for (i = 0; i < depth; i++)
779 tipc_tlv_sprintf(msg->rep, header[i]);
780 tipc_tlv_sprintf(msg->rep, "\n");
781
782 return 0;
783}
784
785static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
786 struct nlattr **attrs)
787{
788 char port_str[27];
789 struct tipc_name_table_query *ntq;
790 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
791 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
792 u32 node, depth, type, lowbound, upbound;
793 static const char * const scope_str[] = {"", " zone", " cluster",
794 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800795 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100796
Baozeng Ding297f7d22016-05-24 22:33:24 +0800797 if (!attrs[TIPC_NLA_NAME_TABLE])
798 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100799
Baozeng Ding297f7d22016-05-24 22:33:24 +0800800 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200801 attrs[TIPC_NLA_NAME_TABLE], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800802 if (err)
803 return err;
804
805 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
806 return -EINVAL;
807
808 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200809 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800810 if (err)
811 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100812
813 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
814
815 depth = ntohl(ntq->depth);
816 type = ntohl(ntq->type);
817 lowbound = ntohl(ntq->lowbound);
818 upbound = ntohl(ntq->upbound);
819
820 if (!(depth & TIPC_NTQ_ALLTYPES) &&
821 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
822 return 0;
823 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
824 return 0;
825 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
826 return 0;
827
828 tipc_tlv_sprintf(msg->rep, "%-10u ",
829 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
830
831 if (depth == 1)
832 goto out;
833
834 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
835 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
836 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
837
838 if (depth == 2)
839 goto out;
840
841 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
842 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
843 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
844 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
845
846 if (depth == 3)
847 goto out;
848
849 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200850 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100851 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
852out:
853 tipc_tlv_sprintf(msg->rep, "\n");
854
855 return 0;
856}
857
Richard Alpe487d2a32015-02-09 09:50:11 +0100858static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
859 struct nlattr **attrs)
860{
861 u32 type, lower, upper;
862 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800863 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100864
Baozeng Ding297f7d22016-05-24 22:33:24 +0800865 if (!attrs[TIPC_NLA_PUBL])
866 return -EINVAL;
867
868 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
Johannes Bergfceb6432017-04-12 14:34:07 +0200869 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800870 if (err)
871 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100872
873 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
874 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
875 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
876
877 if (lower == upper)
878 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
879 else
880 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
881
882 return 0;
883}
884
885static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
886{
887 int err;
888 void *hdr;
889 struct nlattr *nest;
890 struct sk_buff *args;
891 struct tipc_nl_compat_cmd_dump dump;
892
893 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
894 if (!args)
895 return -ENOMEM;
896
897 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
898 TIPC_NL_PUBL_GET);
899
900 nest = nla_nest_start(args, TIPC_NLA_SOCK);
901 if (!nest) {
902 kfree_skb(args);
903 return -EMSGSIZE;
904 }
905
906 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
907 kfree_skb(args);
908 return -EMSGSIZE;
909 }
910
911 nla_nest_end(args, nest);
912 genlmsg_end(args, hdr);
913
914 dump.dumpit = tipc_nl_publ_dump;
915 dump.format = __tipc_nl_compat_publ_dump;
916
917 err = __tipc_nl_compat_dumpit(&dump, msg, args);
918
919 kfree_skb(args);
920
921 return err;
922}
923
924static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
925 struct nlattr **attrs)
926{
927 int err;
928 u32 sock_ref;
929 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
930
Baozeng Ding297f7d22016-05-24 22:33:24 +0800931 if (!attrs[TIPC_NLA_SOCK])
932 return -EINVAL;
933
934 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200935 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800936 if (err)
937 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100938
939 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
940 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
941
942 if (sock[TIPC_NLA_SOCK_CON]) {
943 u32 node;
944 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
945
Johannes Bergfceb6432017-04-12 14:34:07 +0200946 nla_parse_nested(con, TIPC_NLA_CON_MAX,
947 sock[TIPC_NLA_SOCK_CON], NULL, NULL);
Richard Alpe487d2a32015-02-09 09:50:11 +0100948
949 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
950 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
951 tipc_zone(node),
952 tipc_cluster(node),
953 tipc_node(node),
954 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
955
956 if (con[TIPC_NLA_CON_FLAG])
957 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
958 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
959 nla_get_u32(con[TIPC_NLA_CON_INST]));
960 else
961 tipc_tlv_sprintf(msg->rep, "\n");
962 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
963 tipc_tlv_sprintf(msg->rep, " bound to");
964
965 err = tipc_nl_compat_publ_dump(msg, sock_ref);
966 if (err)
967 return err;
968 }
969 tipc_tlv_sprintf(msg->rep, "\n");
970
971 return 0;
972}
973
Richard Alpe5bfc3352015-02-09 09:50:12 +0100974static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
975 struct nlattr **attrs)
976{
977 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800978 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +0100979
Baozeng Ding297f7d22016-05-24 22:33:24 +0800980 if (!attrs[TIPC_NLA_MEDIA])
981 return -EINVAL;
982
Johannes Bergfceb6432017-04-12 14:34:07 +0200983 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX,
984 attrs[TIPC_NLA_MEDIA], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800985 if (err)
986 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +0100987
988 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
989 nla_data(media[TIPC_NLA_MEDIA_NAME]),
990 nla_len(media[TIPC_NLA_MEDIA_NAME]));
991}
992
Richard Alpe4b28cb52015-02-09 09:50:13 +0100993static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
994 struct nlattr **attrs)
995{
996 struct tipc_node_info node_info;
997 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800998 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +0100999
Baozeng Ding297f7d22016-05-24 22:33:24 +08001000 if (!attrs[TIPC_NLA_NODE])
1001 return -EINVAL;
1002
1003 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
Johannes Bergfceb6432017-04-12 14:34:07 +02001004 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001005 if (err)
1006 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001007
1008 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1009 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1010
1011 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1012 sizeof(node_info));
1013}
1014
Richard Alpec3d6fb82015-05-06 13:58:54 +02001015static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1016 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001017 struct tipc_nl_compat_msg *msg)
1018{
1019 u32 val;
1020 struct nlattr *net;
1021
1022 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1023
1024 net = nla_nest_start(skb, TIPC_NLA_NET);
1025 if (!net)
1026 return -EMSGSIZE;
1027
Richard Alpe964f9502015-02-09 09:50:15 +01001028 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1029 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1030 return -EMSGSIZE;
1031 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1032 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1033 return -EMSGSIZE;
1034 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001035 nla_nest_end(skb, net);
1036
1037 return 0;
1038}
1039
Richard Alpe3c261812015-02-09 09:50:16 +01001040static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1041 struct nlattr **attrs)
1042{
1043 __be32 id;
1044 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001045 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001046
Baozeng Ding297f7d22016-05-24 22:33:24 +08001047 if (!attrs[TIPC_NLA_NET])
1048 return -EINVAL;
1049
1050 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
Johannes Bergfceb6432017-04-12 14:34:07 +02001051 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001052 if (err)
1053 return err;
1054
Richard Alpe3c261812015-02-09 09:50:16 +01001055 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1056
1057 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1058}
1059
Richard Alpe5a81a632015-02-09 09:50:17 +01001060static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1061{
1062 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1063 if (!msg->rep)
1064 return -ENOMEM;
1065
1066 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1067 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1068
1069 return 0;
1070}
1071
Richard Alped0796d12015-02-09 09:50:04 +01001072static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1073{
1074 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001075 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001076
1077 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001078 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001079
1080 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001081 case TIPC_CMD_NOOP:
1082 msg->rep = tipc_tlv_alloc(0);
1083 if (!msg->rep)
1084 return -ENOMEM;
1085 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001086 case TIPC_CMD_GET_BEARER_NAMES:
1087 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1088 dump.dumpit = tipc_nl_bearer_dump;
1089 dump.format = tipc_nl_compat_bearer_dump;
1090 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001091 case TIPC_CMD_ENABLE_BEARER:
1092 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1093 doit.doit = tipc_nl_bearer_enable;
1094 doit.transcode = tipc_nl_compat_bearer_enable;
1095 return tipc_nl_compat_doit(&doit, msg);
1096 case TIPC_CMD_DISABLE_BEARER:
1097 msg->req_type = TIPC_TLV_BEARER_NAME;
1098 doit.doit = tipc_nl_bearer_disable;
1099 doit.transcode = tipc_nl_compat_bearer_disable;
1100 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d42015-02-09 09:50:06 +01001101 case TIPC_CMD_SHOW_LINK_STATS:
1102 msg->req_type = TIPC_TLV_LINK_NAME;
1103 msg->rep_size = ULTRA_STRING_MAX_LEN;
1104 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001105 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d42015-02-09 09:50:06 +01001106 dump.format = tipc_nl_compat_link_stat_dump;
1107 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001108 case TIPC_CMD_GET_LINKS:
1109 msg->req_type = TIPC_TLV_NET_ADDR;
1110 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001111 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001112 dump.format = tipc_nl_compat_link_dump;
1113 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001114 case TIPC_CMD_SET_LINK_TOL:
1115 case TIPC_CMD_SET_LINK_PRI:
1116 case TIPC_CMD_SET_LINK_WINDOW:
1117 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001118 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001119 doit.transcode = tipc_nl_compat_link_set;
1120 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001121 case TIPC_CMD_RESET_LINK_STATS:
1122 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001123 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001124 doit.transcode = tipc_nl_compat_link_reset_stats;
1125 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001126 case TIPC_CMD_SHOW_NAME_TABLE:
1127 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1128 msg->rep_size = ULTRA_STRING_MAX_LEN;
1129 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1130 dump.header = tipc_nl_compat_name_table_dump_header;
1131 dump.dumpit = tipc_nl_name_table_dump;
1132 dump.format = tipc_nl_compat_name_table_dump;
1133 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001134 case TIPC_CMD_SHOW_PORTS:
1135 msg->rep_size = ULTRA_STRING_MAX_LEN;
1136 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1137 dump.dumpit = tipc_nl_sk_dump;
1138 dump.format = tipc_nl_compat_sk_dump;
1139 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001140 case TIPC_CMD_GET_MEDIA_NAMES:
1141 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1142 dump.dumpit = tipc_nl_media_dump;
1143 dump.format = tipc_nl_compat_media_dump;
1144 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001145 case TIPC_CMD_GET_NODES:
1146 msg->rep_size = ULTRA_STRING_MAX_LEN;
1147 dump.dumpit = tipc_nl_node_dump;
1148 dump.format = tipc_nl_compat_node_dump;
1149 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001150 case TIPC_CMD_SET_NODE_ADDR:
1151 msg->req_type = TIPC_TLV_NET_ADDR;
1152 doit.doit = tipc_nl_net_set;
1153 doit.transcode = tipc_nl_compat_net_set;
1154 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001155 case TIPC_CMD_SET_NETID:
1156 msg->req_type = TIPC_TLV_UNSIGNED;
1157 doit.doit = tipc_nl_net_set;
1158 doit.transcode = tipc_nl_compat_net_set;
1159 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001160 case TIPC_CMD_GET_NETID:
1161 msg->rep_size = sizeof(u32);
1162 dump.dumpit = tipc_nl_net_dump;
1163 dump.format = tipc_nl_compat_net_dump;
1164 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001165 case TIPC_CMD_SHOW_STATS:
1166 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001167 }
1168
1169 return -EOPNOTSUPP;
1170}
1171
1172static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1173{
1174 int err;
1175 int len;
1176 struct tipc_nl_compat_msg msg;
1177 struct nlmsghdr *req_nlh;
1178 struct nlmsghdr *rep_nlh;
1179 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001180
1181 memset(&msg, 0, sizeof(msg));
1182
1183 req_nlh = (struct nlmsghdr *)skb->data;
1184 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1185 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001186 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001187 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001188
1189 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1190 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1191 err = -EACCES;
1192 goto send;
1193 }
1194
1195 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Richard Alpe8f8ff9132015-08-17 14:15:10 +02001196 if (len && !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001197 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1198 err = -EOPNOTSUPP;
1199 goto send;
1200 }
1201
1202 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001203 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001204 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1205 else if (err == -EINVAL)
1206 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1207send:
1208 if (!msg.rep)
1209 return err;
1210
1211 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1212 skb_push(msg.rep, len);
1213 rep_nlh = nlmsg_hdr(msg.rep);
1214 memcpy(rep_nlh, info->nlhdr, len);
1215 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001216 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001217
1218 return err;
1219}
1220
Arvind Yadav042a9012017-08-23 16:22:20 +05301221static const struct genl_ops tipc_genl_compat_ops[] = {
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001222 {
1223 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001224 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001225 },
1226};
1227
Johannes Berg56989f62016-10-24 14:40:05 +02001228static struct genl_family tipc_genl_compat_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001229 .name = TIPC_GENL_NAME,
1230 .version = TIPC_GENL_VERSION,
1231 .hdrsize = TIPC_GENL_HDRLEN,
1232 .maxattr = 0,
1233 .netnsok = true,
1234 .module = THIS_MODULE,
1235 .ops = tipc_genl_compat_ops,
1236 .n_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1237};
1238
Johannes Berg56989f62016-10-24 14:40:05 +02001239int __init tipc_netlink_compat_start(void)
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001240{
1241 int res;
1242
Johannes Berg489111e2016-10-24 14:40:03 +02001243 res = genl_register_family(&tipc_genl_compat_family);
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001244 if (res) {
1245 pr_err("Failed to register legacy compat interface\n");
1246 return res;
1247 }
1248
1249 return 0;
1250}
1251
1252void tipc_netlink_compat_stop(void)
1253{
1254 genl_unregister_family(&tipc_genl_compat_family);
1255}