blob: 6376467e78f862c25ffae531848bd7aded07609e [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;
Cong Wang12a78b02018-09-11 15:12:17 -0700188 if (__tipc_dump_start(&cb, msg->net)) {
189 kfree_skb(buf);
190 return -ENOMEM;
191 }
Richard Alped0796d12015-02-09 09:50:04 +0100192
193 do {
194 int rem;
195
196 len = (*cmd->dumpit)(buf, &cb);
197
198 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
199 struct nlattr **attrs;
200
201 err = tipc_nlmsg_parse(nlmsg, &attrs);
202 if (err)
203 goto err_out;
204
205 err = (*cmd->format)(msg, attrs);
206 if (err)
207 goto err_out;
208
209 if (tipc_skb_tailroom(msg->rep) <= 1) {
210 err = -EMSGSIZE;
211 goto err_out;
212 }
213 }
214
215 skb_reset_tail_pointer(buf);
216 buf->len = 0;
217
218 } while (len);
219
220 err = 0;
221
222err_out:
Cong Wang8f5c5fc2018-09-04 14:54:55 -0700223 tipc_dump_done(&cb);
Richard Alped0796d12015-02-09 09:50:04 +0100224 kfree_skb(buf);
225
226 if (err == -EMSGSIZE) {
227 /* The legacy API only considered messages filling
228 * "ULTRA_STRING_MAX_LEN" to be truncated.
229 */
230 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
231 char *tail = skb_tail_pointer(msg->rep);
232
233 if (*tail != '\0')
234 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
235 REPLY_TRUNCATED);
236 }
237
238 return 0;
239 }
240
241 return err;
242}
243
244static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
245 struct tipc_nl_compat_msg *msg)
246{
247 int err;
248 struct sk_buff *arg;
249
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100250 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
251 return -EINVAL;
252
Richard Alped0796d12015-02-09 09:50:04 +0100253 msg->rep = tipc_tlv_alloc(msg->rep_size);
254 if (!msg->rep)
255 return -ENOMEM;
256
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100257 if (msg->rep_type)
258 tipc_tlv_init(msg->rep, msg->rep_type);
259
Richard Alpe44a8ae92015-02-09 09:50:10 +0100260 if (cmd->header)
261 (*cmd->header)(msg);
262
Richard Alped0796d12015-02-09 09:50:04 +0100263 arg = nlmsg_new(0, GFP_KERNEL);
264 if (!arg) {
265 kfree_skb(msg->rep);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700266 msg->rep = NULL;
Richard Alped0796d12015-02-09 09:50:04 +0100267 return -ENOMEM;
268 }
269
270 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700271 if (err) {
Richard Alped0796d12015-02-09 09:50:04 +0100272 kfree_skb(msg->rep);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700273 msg->rep = NULL;
274 }
Richard Alped0796d12015-02-09 09:50:04 +0100275 kfree_skb(arg);
276
277 return err;
278}
279
Richard Alpe9ab15462015-02-09 09:50:05 +0100280static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
281 struct tipc_nl_compat_msg *msg)
282{
283 int err;
284 struct sk_buff *doit_buf;
285 struct sk_buff *trans_buf;
286 struct nlattr **attrbuf;
287 struct genl_info info;
288
289 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
290 if (!trans_buf)
291 return -ENOMEM;
292
Kees Cook6da2ec52018-06-12 13:55:00 -0700293 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
294 sizeof(struct nlattr *),
295 GFP_KERNEL);
Richard Alpe9ab15462015-02-09 09:50:05 +0100296 if (!attrbuf) {
297 err = -ENOMEM;
298 goto trans_out;
299 }
300
Richard Alpe9ab15462015-02-09 09:50:05 +0100301 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
302 if (!doit_buf) {
303 err = -ENOMEM;
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800304 goto attrbuf_out;
Richard Alpe9ab15462015-02-09 09:50:05 +0100305 }
306
Richard Alpe9ab15462015-02-09 09:50:05 +0100307 memset(&info, 0, sizeof(info));
308 info.attrs = attrbuf;
309
Ying Xueed4ffdf2018-02-14 13:38:04 +0800310 rtnl_lock();
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800311 err = (*cmd->transcode)(cmd, trans_buf, msg);
312 if (err)
313 goto doit_out;
314
315 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
316 (const struct nlattr *)trans_buf->data,
317 trans_buf->len, NULL, NULL);
318 if (err)
319 goto doit_out;
320
321 doit_buf->sk = msg->dst_sk;
322
Richard Alpe9ab15462015-02-09 09:50:05 +0100323 err = (*cmd->doit)(doit_buf, &info);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800324doit_out:
Ying Xueed4ffdf2018-02-14 13:38:04 +0800325 rtnl_unlock();
Richard Alpe9ab15462015-02-09 09:50:05 +0100326
327 kfree_skb(doit_buf);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800328attrbuf_out:
Richard Alpe9ab15462015-02-09 09:50:05 +0100329 kfree(attrbuf);
330trans_out:
331 kfree_skb(trans_buf);
332
333 return err;
334}
335
336static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
337 struct tipc_nl_compat_msg *msg)
338{
339 int err;
340
341 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
342 return -EINVAL;
343
344 err = __tipc_nl_compat_doit(cmd, msg);
345 if (err)
346 return err;
347
348 /* The legacy API considered an empty message a success message */
349 msg->rep = tipc_tlv_alloc(0);
350 if (!msg->rep)
351 return -ENOMEM;
352
353 return 0;
354}
355
Richard Alped0796d12015-02-09 09:50:04 +0100356static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
357 struct nlattr **attrs)
358{
359 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800360 int err;
Richard Alped0796d12015-02-09 09:50:04 +0100361
Baozeng Ding297f7d22016-05-24 22:33:24 +0800362 if (!attrs[TIPC_NLA_BEARER])
363 return -EINVAL;
364
365 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200366 attrs[TIPC_NLA_BEARER], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800367 if (err)
368 return err;
Richard Alped0796d12015-02-09 09:50:04 +0100369
370 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
371 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
372 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
373}
374
Richard Alpec3d6fb82015-05-06 13:58:54 +0200375static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
376 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100377 struct tipc_nl_compat_msg *msg)
378{
379 struct nlattr *prop;
380 struct nlattr *bearer;
381 struct tipc_bearer_config *b;
382
383 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
384
385 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
386 if (!bearer)
387 return -EMSGSIZE;
388
389 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
390 return -EMSGSIZE;
391
392 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
393 return -EMSGSIZE;
394
395 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
396 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
397 if (!prop)
398 return -EMSGSIZE;
399 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
400 return -EMSGSIZE;
401 nla_nest_end(skb, prop);
402 }
403 nla_nest_end(skb, bearer);
404
405 return 0;
406}
407
Richard Alpec3d6fb82015-05-06 13:58:54 +0200408static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
409 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100410 struct tipc_nl_compat_msg *msg)
411{
412 char *name;
413 struct nlattr *bearer;
414
415 name = (char *)TLV_DATA(msg->req);
416
417 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
418 if (!bearer)
419 return -EMSGSIZE;
420
421 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
422 return -EMSGSIZE;
423
424 nla_nest_end(skb, bearer);
425
426 return 0;
427}
428
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100429static inline u32 perc(u32 count, u32 total)
430{
431 return (count * 100 + (total / 2)) / total;
432}
433
434static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
435 struct nlattr *prop[], struct nlattr *stats[])
436{
437 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
438 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
439
440 tipc_tlv_sprintf(msg->rep,
441 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
442 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
443 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
444 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
445 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
446 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
447
448 tipc_tlv_sprintf(msg->rep,
449 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
450 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
451 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
452 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
453 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
454 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
455
456 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
457 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
458 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
459 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
460
461 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
462 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
463 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
464 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
465
466 tipc_tlv_sprintf(msg->rep,
467 " Congestion link:%u Send queue max:%u avg:%u",
468 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
469 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
470 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
471}
472
473static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
474 struct nlattr **attrs)
475{
476 char *name;
477 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
478 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
479 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800480 int err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100481
Baozeng Ding297f7d22016-05-24 22:33:24 +0800482 if (!attrs[TIPC_NLA_LINK])
483 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100484
Baozeng Ding297f7d22016-05-24 22:33:24 +0800485 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200486 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800487 if (err)
488 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100489
Baozeng Ding297f7d22016-05-24 22:33:24 +0800490 if (!link[TIPC_NLA_LINK_PROP])
491 return -EINVAL;
492
493 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200494 link[TIPC_NLA_LINK_PROP], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800495 if (err)
496 return err;
497
498 if (!link[TIPC_NLA_LINK_STATS])
499 return -EINVAL;
500
501 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200502 link[TIPC_NLA_LINK_STATS], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800503 if (err)
504 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100505
506 name = (char *)TLV_DATA(msg->req);
507 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
508 return 0;
509
510 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
511 nla_data(link[TIPC_NLA_LINK_NAME]));
512
513 if (link[TIPC_NLA_LINK_BROADCAST]) {
514 __fill_bc_link_stat(msg, prop, stats);
515 return 0;
516 }
517
518 if (link[TIPC_NLA_LINK_ACTIVE])
519 tipc_tlv_sprintf(msg->rep, " ACTIVE");
520 else if (link[TIPC_NLA_LINK_UP])
521 tipc_tlv_sprintf(msg->rep, " STANDBY");
522 else
523 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
524
525 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
526 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
527 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
528
529 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
530 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
531 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
532
533 tipc_tlv_sprintf(msg->rep,
534 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
535 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
536 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
537 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
538 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
539 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
540 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
541
542 tipc_tlv_sprintf(msg->rep,
543 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
544 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
545 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
546 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
547 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
548 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
549 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
550
551 tipc_tlv_sprintf(msg->rep,
552 " TX profile sample:%u packets average:%u octets\n",
553 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
554 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
555 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
556
557 tipc_tlv_sprintf(msg->rep,
558 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
559 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
560 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
561 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
562 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
563 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
564 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
565 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
566 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
567
568 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
569 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
570 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
571 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
572 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
573 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
574 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
575
576 tipc_tlv_sprintf(msg->rep,
577 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
578 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
579 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
580 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
581 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
582 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
583
584 tipc_tlv_sprintf(msg->rep,
585 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
586 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
587 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
588 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
589 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
590 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
591
592 tipc_tlv_sprintf(msg->rep,
593 " Congestion link:%u Send queue max:%u avg:%u",
594 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
595 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
596 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
597
598 return 0;
599}
600
Richard Alpe357ebdb2015-02-09 09:50:07 +0100601static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
602 struct nlattr **attrs)
603{
604 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
605 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800606 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100607
Baozeng Ding297f7d22016-05-24 22:33:24 +0800608 if (!attrs[TIPC_NLA_LINK])
609 return -EINVAL;
610
611 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200612 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800613 if (err)
614 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100615
616 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
617 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200618 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400619 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100620
621 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
622 &link_info, sizeof(link_info));
623}
624
Richard Alpec3d6fb82015-05-06 13:58:54 +0200625static int __tipc_add_link_prop(struct sk_buff *skb,
626 struct tipc_nl_compat_msg *msg,
627 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100628{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200629 switch (msg->cmd) {
630 case TIPC_CMD_SET_LINK_PRI:
631 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
632 case TIPC_CMD_SET_LINK_TOL:
633 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
634 case TIPC_CMD_SET_LINK_WINDOW:
635 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
636 }
637
638 return -EINVAL;
639}
640
641static int tipc_nl_compat_media_set(struct sk_buff *skb,
642 struct tipc_nl_compat_msg *msg)
643{
Richard Alpe37e2d482015-02-09 09:50:08 +0100644 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200645 struct nlattr *media;
646 struct tipc_link_config *lc;
647
648 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
649
650 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
651 if (!media)
652 return -EMSGSIZE;
653
654 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
655 return -EMSGSIZE;
656
657 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
658 if (!prop)
659 return -EMSGSIZE;
660
661 __tipc_add_link_prop(skb, msg, lc);
662 nla_nest_end(skb, prop);
663 nla_nest_end(skb, media);
664
665 return 0;
666}
667
668static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
669 struct tipc_nl_compat_msg *msg)
670{
671 struct nlattr *prop;
672 struct nlattr *bearer;
673 struct tipc_link_config *lc;
674
675 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
676
677 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
678 if (!bearer)
679 return -EMSGSIZE;
680
681 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
682 return -EMSGSIZE;
683
684 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
685 if (!prop)
686 return -EMSGSIZE;
687
688 __tipc_add_link_prop(skb, msg, lc);
689 nla_nest_end(skb, prop);
690 nla_nest_end(skb, bearer);
691
692 return 0;
693}
694
695static int __tipc_nl_compat_link_set(struct sk_buff *skb,
696 struct tipc_nl_compat_msg *msg)
697{
698 struct nlattr *prop;
699 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100700 struct tipc_link_config *lc;
701
702 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
703
704 link = nla_nest_start(skb, TIPC_NLA_LINK);
705 if (!link)
706 return -EMSGSIZE;
707
708 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
709 return -EMSGSIZE;
710
711 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
712 if (!prop)
713 return -EMSGSIZE;
714
Richard Alpec3d6fb82015-05-06 13:58:54 +0200715 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100716 nla_nest_end(skb, prop);
717 nla_nest_end(skb, link);
718
719 return 0;
720}
721
Richard Alpec3d6fb82015-05-06 13:58:54 +0200722static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
723 struct sk_buff *skb,
724 struct tipc_nl_compat_msg *msg)
725{
726 struct tipc_link_config *lc;
727 struct tipc_bearer *bearer;
728 struct tipc_media *media;
729
730 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
731
732 media = tipc_media_find(lc->name);
733 if (media) {
Ying Xueed4ffdf2018-02-14 13:38:04 +0800734 cmd->doit = &__tipc_nl_media_set;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200735 return tipc_nl_compat_media_set(skb, msg);
736 }
737
738 bearer = tipc_bearer_find(msg->net, lc->name);
739 if (bearer) {
Ying Xueed4ffdf2018-02-14 13:38:04 +0800740 cmd->doit = &__tipc_nl_bearer_set;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200741 return tipc_nl_compat_bearer_set(skb, msg);
742 }
743
744 return __tipc_nl_compat_link_set(skb, msg);
745}
746
747static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
748 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100749 struct tipc_nl_compat_msg *msg)
750{
751 char *name;
752 struct nlattr *link;
753
754 name = (char *)TLV_DATA(msg->req);
755
756 link = nla_nest_start(skb, TIPC_NLA_LINK);
757 if (!link)
758 return -EMSGSIZE;
759
760 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
761 return -EMSGSIZE;
762
763 nla_nest_end(skb, link);
764
765 return 0;
766}
767
Richard Alpe44a8ae92015-02-09 09:50:10 +0100768static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
769{
770 int i;
771 u32 depth;
772 struct tipc_name_table_query *ntq;
773 static const char * const header[] = {
774 "Type ",
775 "Lower Upper ",
776 "Port Identity ",
777 "Publication Scope"
778 };
779
780 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
781
782 depth = ntohl(ntq->depth);
783
784 if (depth > 4)
785 depth = 4;
786 for (i = 0; i < depth; i++)
787 tipc_tlv_sprintf(msg->rep, header[i]);
788 tipc_tlv_sprintf(msg->rep, "\n");
789
790 return 0;
791}
792
793static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
794 struct nlattr **attrs)
795{
796 char port_str[27];
797 struct tipc_name_table_query *ntq;
798 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
799 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
800 u32 node, depth, type, lowbound, upbound;
801 static const char * const scope_str[] = {"", " zone", " cluster",
802 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800803 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100804
Baozeng Ding297f7d22016-05-24 22:33:24 +0800805 if (!attrs[TIPC_NLA_NAME_TABLE])
806 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100807
Baozeng Ding297f7d22016-05-24 22:33:24 +0800808 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200809 attrs[TIPC_NLA_NAME_TABLE], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800810 if (err)
811 return err;
812
813 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
814 return -EINVAL;
815
816 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200817 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800818 if (err)
819 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100820
821 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
822
823 depth = ntohl(ntq->depth);
824 type = ntohl(ntq->type);
825 lowbound = ntohl(ntq->lowbound);
826 upbound = ntohl(ntq->upbound);
827
828 if (!(depth & TIPC_NTQ_ALLTYPES) &&
829 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
830 return 0;
831 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
832 return 0;
833 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
834 return 0;
835
836 tipc_tlv_sprintf(msg->rep, "%-10u ",
837 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
838
839 if (depth == 1)
840 goto out;
841
842 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
843 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
844 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
845
846 if (depth == 2)
847 goto out;
848
849 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
850 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
851 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
852 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
853
854 if (depth == 3)
855 goto out;
856
857 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200858 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100859 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
860out:
861 tipc_tlv_sprintf(msg->rep, "\n");
862
863 return 0;
864}
865
Richard Alpe487d2a32015-02-09 09:50:11 +0100866static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
867 struct nlattr **attrs)
868{
869 u32 type, lower, upper;
870 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800871 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100872
Baozeng Ding297f7d22016-05-24 22:33:24 +0800873 if (!attrs[TIPC_NLA_PUBL])
874 return -EINVAL;
875
876 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
Johannes Bergfceb6432017-04-12 14:34:07 +0200877 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800878 if (err)
879 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100880
881 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
882 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
883 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
884
885 if (lower == upper)
886 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
887 else
888 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
889
890 return 0;
891}
892
893static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
894{
895 int err;
896 void *hdr;
897 struct nlattr *nest;
898 struct sk_buff *args;
899 struct tipc_nl_compat_cmd_dump dump;
900
901 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
902 if (!args)
903 return -ENOMEM;
904
905 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
906 TIPC_NL_PUBL_GET);
907
908 nest = nla_nest_start(args, TIPC_NLA_SOCK);
909 if (!nest) {
910 kfree_skb(args);
911 return -EMSGSIZE;
912 }
913
914 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
915 kfree_skb(args);
916 return -EMSGSIZE;
917 }
918
919 nla_nest_end(args, nest);
920 genlmsg_end(args, hdr);
921
922 dump.dumpit = tipc_nl_publ_dump;
923 dump.format = __tipc_nl_compat_publ_dump;
924
925 err = __tipc_nl_compat_dumpit(&dump, msg, args);
926
927 kfree_skb(args);
928
929 return err;
930}
931
932static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
933 struct nlattr **attrs)
934{
935 int err;
936 u32 sock_ref;
937 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
938
Baozeng Ding297f7d22016-05-24 22:33:24 +0800939 if (!attrs[TIPC_NLA_SOCK])
940 return -EINVAL;
941
942 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200943 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800944 if (err)
945 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100946
947 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
948 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
949
950 if (sock[TIPC_NLA_SOCK_CON]) {
951 u32 node;
952 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
953
Johannes Bergfceb6432017-04-12 14:34:07 +0200954 nla_parse_nested(con, TIPC_NLA_CON_MAX,
955 sock[TIPC_NLA_SOCK_CON], NULL, NULL);
Richard Alpe487d2a32015-02-09 09:50:11 +0100956
957 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
958 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
959 tipc_zone(node),
960 tipc_cluster(node),
961 tipc_node(node),
962 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
963
964 if (con[TIPC_NLA_CON_FLAG])
965 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
966 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
967 nla_get_u32(con[TIPC_NLA_CON_INST]));
968 else
969 tipc_tlv_sprintf(msg->rep, "\n");
970 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
971 tipc_tlv_sprintf(msg->rep, " bound to");
972
973 err = tipc_nl_compat_publ_dump(msg, sock_ref);
974 if (err)
975 return err;
976 }
977 tipc_tlv_sprintf(msg->rep, "\n");
978
979 return 0;
980}
981
Richard Alpe5bfc3352015-02-09 09:50:12 +0100982static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
983 struct nlattr **attrs)
984{
985 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800986 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +0100987
Baozeng Ding297f7d22016-05-24 22:33:24 +0800988 if (!attrs[TIPC_NLA_MEDIA])
989 return -EINVAL;
990
Johannes Bergfceb6432017-04-12 14:34:07 +0200991 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX,
992 attrs[TIPC_NLA_MEDIA], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800993 if (err)
994 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +0100995
996 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
997 nla_data(media[TIPC_NLA_MEDIA_NAME]),
998 nla_len(media[TIPC_NLA_MEDIA_NAME]));
999}
1000
Richard Alpe4b28cb52015-02-09 09:50:13 +01001001static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1002 struct nlattr **attrs)
1003{
1004 struct tipc_node_info node_info;
1005 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001006 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001007
Baozeng Ding297f7d22016-05-24 22:33:24 +08001008 if (!attrs[TIPC_NLA_NODE])
1009 return -EINVAL;
1010
1011 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
Johannes Bergfceb6432017-04-12 14:34:07 +02001012 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001013 if (err)
1014 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001015
1016 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1017 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1018
1019 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1020 sizeof(node_info));
1021}
1022
Richard Alpec3d6fb82015-05-06 13:58:54 +02001023static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1024 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001025 struct tipc_nl_compat_msg *msg)
1026{
1027 u32 val;
1028 struct nlattr *net;
1029
1030 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1031
1032 net = nla_nest_start(skb, TIPC_NLA_NET);
1033 if (!net)
1034 return -EMSGSIZE;
1035
Richard Alpe964f9502015-02-09 09:50:15 +01001036 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1037 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1038 return -EMSGSIZE;
1039 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1040 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1041 return -EMSGSIZE;
1042 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001043 nla_nest_end(skb, net);
1044
1045 return 0;
1046}
1047
Richard Alpe3c261812015-02-09 09:50:16 +01001048static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1049 struct nlattr **attrs)
1050{
1051 __be32 id;
1052 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001053 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001054
Baozeng Ding297f7d22016-05-24 22:33:24 +08001055 if (!attrs[TIPC_NLA_NET])
1056 return -EINVAL;
1057
1058 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
Johannes Bergfceb6432017-04-12 14:34:07 +02001059 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001060 if (err)
1061 return err;
1062
Richard Alpe3c261812015-02-09 09:50:16 +01001063 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1064
1065 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1066}
1067
Richard Alpe5a81a632015-02-09 09:50:17 +01001068static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1069{
1070 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1071 if (!msg->rep)
1072 return -ENOMEM;
1073
1074 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1075 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1076
1077 return 0;
1078}
1079
Richard Alped0796d12015-02-09 09:50:04 +01001080static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1081{
1082 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001083 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001084
1085 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001086 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001087
1088 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001089 case TIPC_CMD_NOOP:
1090 msg->rep = tipc_tlv_alloc(0);
1091 if (!msg->rep)
1092 return -ENOMEM;
1093 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001094 case TIPC_CMD_GET_BEARER_NAMES:
1095 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1096 dump.dumpit = tipc_nl_bearer_dump;
1097 dump.format = tipc_nl_compat_bearer_dump;
1098 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001099 case TIPC_CMD_ENABLE_BEARER:
1100 msg->req_type = TIPC_TLV_BEARER_CONFIG;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001101 doit.doit = __tipc_nl_bearer_enable;
Richard Alpe9ab15462015-02-09 09:50:05 +01001102 doit.transcode = tipc_nl_compat_bearer_enable;
1103 return tipc_nl_compat_doit(&doit, msg);
1104 case TIPC_CMD_DISABLE_BEARER:
1105 msg->req_type = TIPC_TLV_BEARER_NAME;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001106 doit.doit = __tipc_nl_bearer_disable;
Richard Alpe9ab15462015-02-09 09:50:05 +01001107 doit.transcode = tipc_nl_compat_bearer_disable;
1108 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001109 case TIPC_CMD_SHOW_LINK_STATS:
1110 msg->req_type = TIPC_TLV_LINK_NAME;
1111 msg->rep_size = ULTRA_STRING_MAX_LEN;
1112 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001113 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001114 dump.format = tipc_nl_compat_link_stat_dump;
1115 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001116 case TIPC_CMD_GET_LINKS:
1117 msg->req_type = TIPC_TLV_NET_ADDR;
1118 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001119 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001120 dump.format = tipc_nl_compat_link_dump;
1121 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001122 case TIPC_CMD_SET_LINK_TOL:
1123 case TIPC_CMD_SET_LINK_PRI:
1124 case TIPC_CMD_SET_LINK_WINDOW:
1125 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001126 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001127 doit.transcode = tipc_nl_compat_link_set;
1128 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001129 case TIPC_CMD_RESET_LINK_STATS:
1130 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001131 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001132 doit.transcode = tipc_nl_compat_link_reset_stats;
1133 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001134 case TIPC_CMD_SHOW_NAME_TABLE:
1135 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1136 msg->rep_size = ULTRA_STRING_MAX_LEN;
1137 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1138 dump.header = tipc_nl_compat_name_table_dump_header;
1139 dump.dumpit = tipc_nl_name_table_dump;
1140 dump.format = tipc_nl_compat_name_table_dump;
1141 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001142 case TIPC_CMD_SHOW_PORTS:
1143 msg->rep_size = ULTRA_STRING_MAX_LEN;
1144 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1145 dump.dumpit = tipc_nl_sk_dump;
1146 dump.format = tipc_nl_compat_sk_dump;
1147 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001148 case TIPC_CMD_GET_MEDIA_NAMES:
1149 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1150 dump.dumpit = tipc_nl_media_dump;
1151 dump.format = tipc_nl_compat_media_dump;
1152 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001153 case TIPC_CMD_GET_NODES:
1154 msg->rep_size = ULTRA_STRING_MAX_LEN;
1155 dump.dumpit = tipc_nl_node_dump;
1156 dump.format = tipc_nl_compat_node_dump;
1157 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001158 case TIPC_CMD_SET_NODE_ADDR:
1159 msg->req_type = TIPC_TLV_NET_ADDR;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001160 doit.doit = __tipc_nl_net_set;
Richard Alped7cc75d2015-02-09 09:50:14 +01001161 doit.transcode = tipc_nl_compat_net_set;
1162 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001163 case TIPC_CMD_SET_NETID:
1164 msg->req_type = TIPC_TLV_UNSIGNED;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001165 doit.doit = __tipc_nl_net_set;
Richard Alpe964f9502015-02-09 09:50:15 +01001166 doit.transcode = tipc_nl_compat_net_set;
1167 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001168 case TIPC_CMD_GET_NETID:
1169 msg->rep_size = sizeof(u32);
1170 dump.dumpit = tipc_nl_net_dump;
1171 dump.format = tipc_nl_compat_net_dump;
1172 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001173 case TIPC_CMD_SHOW_STATS:
1174 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001175 }
1176
1177 return -EOPNOTSUPP;
1178}
1179
1180static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1181{
1182 int err;
1183 int len;
1184 struct tipc_nl_compat_msg msg;
1185 struct nlmsghdr *req_nlh;
1186 struct nlmsghdr *rep_nlh;
1187 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001188
1189 memset(&msg, 0, sizeof(msg));
1190
1191 req_nlh = (struct nlmsghdr *)skb->data;
1192 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1193 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001194 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001195 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001196
1197 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1198 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1199 err = -EACCES;
1200 goto send;
1201 }
1202
1203 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Richard Alpe8f8ff9132015-08-17 14:15:10 +02001204 if (len && !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001205 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1206 err = -EOPNOTSUPP;
1207 goto send;
1208 }
1209
1210 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001211 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001212 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1213 else if (err == -EINVAL)
1214 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1215send:
1216 if (!msg.rep)
1217 return err;
1218
1219 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1220 skb_push(msg.rep, len);
1221 rep_nlh = nlmsg_hdr(msg.rep);
1222 memcpy(rep_nlh, info->nlhdr, len);
1223 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001224 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001225
1226 return err;
1227}
1228
Arvind Yadav042a9012017-08-23 16:22:20 +05301229static const struct genl_ops tipc_genl_compat_ops[] = {
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001230 {
1231 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001232 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001233 },
1234};
1235
Johannes Berg56989f62016-10-24 14:40:05 +02001236static struct genl_family tipc_genl_compat_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001237 .name = TIPC_GENL_NAME,
1238 .version = TIPC_GENL_VERSION,
1239 .hdrsize = TIPC_GENL_HDRLEN,
1240 .maxattr = 0,
1241 .netnsok = true,
1242 .module = THIS_MODULE,
1243 .ops = tipc_genl_compat_ops,
1244 .n_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1245};
1246
Johannes Berg56989f62016-10-24 14:40:05 +02001247int __init tipc_netlink_compat_start(void)
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001248{
1249 int res;
1250
Johannes Berg489111e2016-10-24 14:40:03 +02001251 res = genl_register_family(&tipc_genl_compat_family);
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001252 if (res) {
1253 pr_err("Failed to register legacy compat interface\n");
1254 return res;
1255 }
1256
1257 return 0;
1258}
1259
1260void tipc_netlink_compat_stop(void)
1261{
1262 genl_unregister_family(&tipc_genl_compat_family);
1263}