blob: b2b115b2287108b0df6362ad58ba584fbbe7b7e9 [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
Ying Xue8b66fee2019-01-14 17:22:25 +080090static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
91{
92 return TLV_GET_LEN(tlv) - TLV_SPACE(0);
93}
94
Richard Alped0796d12015-02-09 09:50:04 +010095static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
96{
97 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
98
99 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
100 return -EMSGSIZE;
101
102 skb_put(skb, TLV_SPACE(len));
103 tlv->tlv_type = htons(type);
104 tlv->tlv_len = htons(TLV_LENGTH(len));
105 if (len && data)
106 memcpy(TLV_DATA(tlv), data, len);
107
108 return 0;
109}
110
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100111static void tipc_tlv_init(struct sk_buff *skb, u16 type)
112{
113 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
114
115 TLV_SET_LEN(tlv, 0);
116 TLV_SET_TYPE(tlv, type);
117 skb_put(skb, sizeof(struct tlv_desc));
118}
119
120static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
121{
122 int n;
123 u16 len;
124 u32 rem;
125 char *buf;
126 struct tlv_desc *tlv;
127 va_list args;
128
129 rem = tipc_skb_tailroom(skb);
130
131 tlv = (struct tlv_desc *)skb->data;
132 len = TLV_GET_LEN(tlv);
133 buf = TLV_DATA(tlv) + len;
134
135 va_start(args, fmt);
136 n = vscnprintf(buf, rem, fmt, args);
137 va_end(args);
138
139 TLV_SET_LEN(tlv, n + len);
140 skb_put(skb, n);
141
142 return n;
143}
144
Richard Alped0796d12015-02-09 09:50:04 +0100145static struct sk_buff *tipc_tlv_alloc(int size)
146{
147 int hdr_len;
148 struct sk_buff *buf;
149
150 size = TLV_SPACE(size);
151 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
152
153 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
154 if (!buf)
155 return NULL;
156
157 skb_reserve(buf, hdr_len);
158
159 return buf;
160}
161
162static struct sk_buff *tipc_get_err_tlv(char *str)
163{
164 int str_len = strlen(str) + 1;
165 struct sk_buff *buf;
166
167 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
168 if (buf)
169 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
170
171 return buf;
172}
173
Ying Xue8b66fee2019-01-14 17:22:25 +0800174static inline bool string_is_valid(char *s, int len)
175{
176 return memchr(s, '\0', len) ? true : false;
177}
178
Richard Alped0796d12015-02-09 09:50:04 +0100179static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
180 struct tipc_nl_compat_msg *msg,
181 struct sk_buff *arg)
182{
183 int len = 0;
184 int err;
185 struct sk_buff *buf;
186 struct nlmsghdr *nlmsg;
187 struct netlink_callback cb;
188
189 memset(&cb, 0, sizeof(cb));
190 cb.nlh = (struct nlmsghdr *)arg->data;
191 cb.skb = arg;
192
193 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
194 if (!buf)
195 return -ENOMEM;
196
197 buf->sk = msg->dst_sk;
Cong Wang12a78b02018-09-11 15:12:17 -0700198 if (__tipc_dump_start(&cb, msg->net)) {
199 kfree_skb(buf);
200 return -ENOMEM;
201 }
Richard Alped0796d12015-02-09 09:50:04 +0100202
203 do {
204 int rem;
205
206 len = (*cmd->dumpit)(buf, &cb);
207
208 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
209 struct nlattr **attrs;
210
211 err = tipc_nlmsg_parse(nlmsg, &attrs);
212 if (err)
213 goto err_out;
214
215 err = (*cmd->format)(msg, attrs);
216 if (err)
217 goto err_out;
218
219 if (tipc_skb_tailroom(msg->rep) <= 1) {
220 err = -EMSGSIZE;
221 goto err_out;
222 }
223 }
224
225 skb_reset_tail_pointer(buf);
226 buf->len = 0;
227
228 } while (len);
229
230 err = 0;
231
232err_out:
Cong Wang8f5c5fc2018-09-04 14:54:55 -0700233 tipc_dump_done(&cb);
Richard Alped0796d12015-02-09 09:50:04 +0100234 kfree_skb(buf);
235
236 if (err == -EMSGSIZE) {
237 /* The legacy API only considered messages filling
238 * "ULTRA_STRING_MAX_LEN" to be truncated.
239 */
240 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
241 char *tail = skb_tail_pointer(msg->rep);
242
243 if (*tail != '\0')
244 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
245 REPLY_TRUNCATED);
246 }
247
248 return 0;
249 }
250
251 return err;
252}
253
254static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
255 struct tipc_nl_compat_msg *msg)
256{
257 int err;
258 struct sk_buff *arg;
259
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100260 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
261 return -EINVAL;
262
Richard Alped0796d12015-02-09 09:50:04 +0100263 msg->rep = tipc_tlv_alloc(msg->rep_size);
264 if (!msg->rep)
265 return -ENOMEM;
266
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100267 if (msg->rep_type)
268 tipc_tlv_init(msg->rep, msg->rep_type);
269
Richard Alpe44a8ae92015-02-09 09:50:10 +0100270 if (cmd->header)
271 (*cmd->header)(msg);
272
Richard Alped0796d12015-02-09 09:50:04 +0100273 arg = nlmsg_new(0, GFP_KERNEL);
274 if (!arg) {
275 kfree_skb(msg->rep);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700276 msg->rep = NULL;
Richard Alped0796d12015-02-09 09:50:04 +0100277 return -ENOMEM;
278 }
279
280 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700281 if (err) {
Richard Alped0796d12015-02-09 09:50:04 +0100282 kfree_skb(msg->rep);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700283 msg->rep = NULL;
284 }
Richard Alped0796d12015-02-09 09:50:04 +0100285 kfree_skb(arg);
286
287 return err;
288}
289
Richard Alpe9ab15462015-02-09 09:50:05 +0100290static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
291 struct tipc_nl_compat_msg *msg)
292{
293 int err;
294 struct sk_buff *doit_buf;
295 struct sk_buff *trans_buf;
296 struct nlattr **attrbuf;
297 struct genl_info info;
298
299 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
300 if (!trans_buf)
301 return -ENOMEM;
302
Kees Cook6da2ec52018-06-12 13:55:00 -0700303 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
304 sizeof(struct nlattr *),
305 GFP_KERNEL);
Richard Alpe9ab15462015-02-09 09:50:05 +0100306 if (!attrbuf) {
307 err = -ENOMEM;
308 goto trans_out;
309 }
310
Richard Alpe9ab15462015-02-09 09:50:05 +0100311 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
312 if (!doit_buf) {
313 err = -ENOMEM;
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800314 goto attrbuf_out;
Richard Alpe9ab15462015-02-09 09:50:05 +0100315 }
316
Richard Alpe9ab15462015-02-09 09:50:05 +0100317 memset(&info, 0, sizeof(info));
318 info.attrs = attrbuf;
319
Ying Xueed4ffdf2018-02-14 13:38:04 +0800320 rtnl_lock();
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800321 err = (*cmd->transcode)(cmd, trans_buf, msg);
322 if (err)
323 goto doit_out;
324
325 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
326 (const struct nlattr *)trans_buf->data,
327 trans_buf->len, NULL, NULL);
328 if (err)
329 goto doit_out;
330
331 doit_buf->sk = msg->dst_sk;
332
Richard Alpe9ab15462015-02-09 09:50:05 +0100333 err = (*cmd->doit)(doit_buf, &info);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800334doit_out:
Ying Xueed4ffdf2018-02-14 13:38:04 +0800335 rtnl_unlock();
Richard Alpe9ab15462015-02-09 09:50:05 +0100336
337 kfree_skb(doit_buf);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800338attrbuf_out:
Richard Alpe9ab15462015-02-09 09:50:05 +0100339 kfree(attrbuf);
340trans_out:
341 kfree_skb(trans_buf);
342
343 return err;
344}
345
346static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
347 struct tipc_nl_compat_msg *msg)
348{
349 int err;
350
351 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
352 return -EINVAL;
353
354 err = __tipc_nl_compat_doit(cmd, msg);
355 if (err)
356 return err;
357
358 /* The legacy API considered an empty message a success message */
359 msg->rep = tipc_tlv_alloc(0);
360 if (!msg->rep)
361 return -ENOMEM;
362
363 return 0;
364}
365
Richard Alped0796d12015-02-09 09:50:04 +0100366static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
367 struct nlattr **attrs)
368{
369 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800370 int err;
Richard Alped0796d12015-02-09 09:50:04 +0100371
Baozeng Ding297f7d22016-05-24 22:33:24 +0800372 if (!attrs[TIPC_NLA_BEARER])
373 return -EINVAL;
374
375 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200376 attrs[TIPC_NLA_BEARER], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800377 if (err)
378 return err;
Richard Alped0796d12015-02-09 09:50:04 +0100379
380 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
381 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
382 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
383}
384
Richard Alpec3d6fb82015-05-06 13:58:54 +0200385static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
386 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100387 struct tipc_nl_compat_msg *msg)
388{
389 struct nlattr *prop;
390 struct nlattr *bearer;
391 struct tipc_bearer_config *b;
392
393 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
394
395 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
396 if (!bearer)
397 return -EMSGSIZE;
398
399 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
400 return -EMSGSIZE;
401
402 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
403 return -EMSGSIZE;
404
405 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
406 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
407 if (!prop)
408 return -EMSGSIZE;
409 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
410 return -EMSGSIZE;
411 nla_nest_end(skb, prop);
412 }
413 nla_nest_end(skb, bearer);
414
415 return 0;
416}
417
Richard Alpec3d6fb82015-05-06 13:58:54 +0200418static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
419 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100420 struct tipc_nl_compat_msg *msg)
421{
422 char *name;
423 struct nlattr *bearer;
424
425 name = (char *)TLV_DATA(msg->req);
426
427 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
428 if (!bearer)
429 return -EMSGSIZE;
430
431 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
432 return -EMSGSIZE;
433
434 nla_nest_end(skb, bearer);
435
436 return 0;
437}
438
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100439static inline u32 perc(u32 count, u32 total)
440{
441 return (count * 100 + (total / 2)) / total;
442}
443
444static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
445 struct nlattr *prop[], struct nlattr *stats[])
446{
447 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
448 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
449
450 tipc_tlv_sprintf(msg->rep,
451 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
452 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
453 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
454 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
455 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
456 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
457
458 tipc_tlv_sprintf(msg->rep,
459 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
460 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
461 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
462 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
463 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
464 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
465
466 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
467 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
468 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
469 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
470
471 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
472 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
473 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
474 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
475
476 tipc_tlv_sprintf(msg->rep,
477 " Congestion link:%u Send queue max:%u avg:%u",
478 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
479 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
480 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
481}
482
483static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
484 struct nlattr **attrs)
485{
486 char *name;
487 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
488 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
489 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800490 int err;
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100491
Baozeng Ding297f7d22016-05-24 22:33:24 +0800492 if (!attrs[TIPC_NLA_LINK])
493 return -EINVAL;
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100494
Baozeng Ding297f7d22016-05-24 22:33:24 +0800495 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200496 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800497 if (err)
498 return err;
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100499
Baozeng Ding297f7d22016-05-24 22:33:24 +0800500 if (!link[TIPC_NLA_LINK_PROP])
501 return -EINVAL;
502
503 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200504 link[TIPC_NLA_LINK_PROP], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800505 if (err)
506 return err;
507
508 if (!link[TIPC_NLA_LINK_STATS])
509 return -EINVAL;
510
511 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200512 link[TIPC_NLA_LINK_STATS], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800513 if (err)
514 return err;
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100515
516 name = (char *)TLV_DATA(msg->req);
517 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
518 return 0;
519
520 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
521 nla_data(link[TIPC_NLA_LINK_NAME]));
522
523 if (link[TIPC_NLA_LINK_BROADCAST]) {
524 __fill_bc_link_stat(msg, prop, stats);
525 return 0;
526 }
527
528 if (link[TIPC_NLA_LINK_ACTIVE])
529 tipc_tlv_sprintf(msg->rep, " ACTIVE");
530 else if (link[TIPC_NLA_LINK_UP])
531 tipc_tlv_sprintf(msg->rep, " STANDBY");
532 else
533 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
534
535 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
536 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
537 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
538
539 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
540 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
541 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
542
543 tipc_tlv_sprintf(msg->rep,
544 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
545 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
546 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
547 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
548 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
549 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
550 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
551
552 tipc_tlv_sprintf(msg->rep,
553 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
554 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
555 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
556 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
557 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
558 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
559 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
560
561 tipc_tlv_sprintf(msg->rep,
562 " TX profile sample:%u packets average:%u octets\n",
563 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
564 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
565 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
566
567 tipc_tlv_sprintf(msg->rep,
568 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
569 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
570 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
571 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
572 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
573 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
574 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
575 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
576 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
577
578 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
579 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
580 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
581 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
582 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
583 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
584 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
585
586 tipc_tlv_sprintf(msg->rep,
587 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
588 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
589 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
590 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
591 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
592 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
593
594 tipc_tlv_sprintf(msg->rep,
595 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
596 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
597 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
598 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
599 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
600 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
601
602 tipc_tlv_sprintf(msg->rep,
603 " Congestion link:%u Send queue max:%u avg:%u",
604 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
605 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
606 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
607
608 return 0;
609}
610
Richard Alpe357ebdb2015-02-09 09:50:07 +0100611static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
612 struct nlattr **attrs)
613{
614 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
615 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800616 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100617
Baozeng Ding297f7d22016-05-24 22:33:24 +0800618 if (!attrs[TIPC_NLA_LINK])
619 return -EINVAL;
620
621 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200622 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800623 if (err)
624 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100625
626 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
627 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200628 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400629 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100630
631 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
632 &link_info, sizeof(link_info));
633}
634
Richard Alpec3d6fb82015-05-06 13:58:54 +0200635static int __tipc_add_link_prop(struct sk_buff *skb,
636 struct tipc_nl_compat_msg *msg,
637 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100638{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200639 switch (msg->cmd) {
640 case TIPC_CMD_SET_LINK_PRI:
641 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
642 case TIPC_CMD_SET_LINK_TOL:
643 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
644 case TIPC_CMD_SET_LINK_WINDOW:
645 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
646 }
647
648 return -EINVAL;
649}
650
651static int tipc_nl_compat_media_set(struct sk_buff *skb,
652 struct tipc_nl_compat_msg *msg)
653{
Richard Alpe37e2d482015-02-09 09:50:08 +0100654 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200655 struct nlattr *media;
656 struct tipc_link_config *lc;
657
658 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
659
660 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
661 if (!media)
662 return -EMSGSIZE;
663
664 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
665 return -EMSGSIZE;
666
667 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
668 if (!prop)
669 return -EMSGSIZE;
670
671 __tipc_add_link_prop(skb, msg, lc);
672 nla_nest_end(skb, prop);
673 nla_nest_end(skb, media);
674
675 return 0;
676}
677
678static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
679 struct tipc_nl_compat_msg *msg)
680{
681 struct nlattr *prop;
682 struct nlattr *bearer;
683 struct tipc_link_config *lc;
684
685 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
686
687 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
688 if (!bearer)
689 return -EMSGSIZE;
690
691 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
692 return -EMSGSIZE;
693
694 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
695 if (!prop)
696 return -EMSGSIZE;
697
698 __tipc_add_link_prop(skb, msg, lc);
699 nla_nest_end(skb, prop);
700 nla_nest_end(skb, bearer);
701
702 return 0;
703}
704
705static int __tipc_nl_compat_link_set(struct sk_buff *skb,
706 struct tipc_nl_compat_msg *msg)
707{
708 struct nlattr *prop;
709 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100710 struct tipc_link_config *lc;
711
712 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
713
714 link = nla_nest_start(skb, TIPC_NLA_LINK);
715 if (!link)
716 return -EMSGSIZE;
717
718 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
719 return -EMSGSIZE;
720
721 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
722 if (!prop)
723 return -EMSGSIZE;
724
Richard Alpec3d6fb82015-05-06 13:58:54 +0200725 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100726 nla_nest_end(skb, prop);
727 nla_nest_end(skb, link);
728
729 return 0;
730}
731
Richard Alpec3d6fb82015-05-06 13:58:54 +0200732static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
733 struct sk_buff *skb,
734 struct tipc_nl_compat_msg *msg)
735{
736 struct tipc_link_config *lc;
737 struct tipc_bearer *bearer;
738 struct tipc_media *media;
739
740 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
741
742 media = tipc_media_find(lc->name);
743 if (media) {
Ying Xueed4ffdf2018-02-14 13:38:04 +0800744 cmd->doit = &__tipc_nl_media_set;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200745 return tipc_nl_compat_media_set(skb, msg);
746 }
747
748 bearer = tipc_bearer_find(msg->net, lc->name);
749 if (bearer) {
Ying Xueed4ffdf2018-02-14 13:38:04 +0800750 cmd->doit = &__tipc_nl_bearer_set;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200751 return tipc_nl_compat_bearer_set(skb, msg);
752 }
753
754 return __tipc_nl_compat_link_set(skb, msg);
755}
756
757static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
758 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100759 struct tipc_nl_compat_msg *msg)
760{
761 char *name;
762 struct nlattr *link;
Ying Xue8b66fee2019-01-14 17:22:25 +0800763 int len;
Richard Alpe18178772015-02-09 09:50:09 +0100764
765 name = (char *)TLV_DATA(msg->req);
766
767 link = nla_nest_start(skb, TIPC_NLA_LINK);
768 if (!link)
769 return -EMSGSIZE;
770
Ying Xue8b66fee2019-01-14 17:22:25 +0800771 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
772 if (!string_is_valid(name, len))
773 return -EINVAL;
774
Richard Alpe18178772015-02-09 09:50:09 +0100775 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
776 return -EMSGSIZE;
777
778 nla_nest_end(skb, link);
779
780 return 0;
781}
782
Richard Alpe44a8ae92015-02-09 09:50:10 +0100783static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
784{
785 int i;
786 u32 depth;
787 struct tipc_name_table_query *ntq;
788 static const char * const header[] = {
789 "Type ",
790 "Lower Upper ",
791 "Port Identity ",
792 "Publication Scope"
793 };
794
795 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
796
797 depth = ntohl(ntq->depth);
798
799 if (depth > 4)
800 depth = 4;
801 for (i = 0; i < depth; i++)
802 tipc_tlv_sprintf(msg->rep, header[i]);
803 tipc_tlv_sprintf(msg->rep, "\n");
804
805 return 0;
806}
807
808static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
809 struct nlattr **attrs)
810{
811 char port_str[27];
812 struct tipc_name_table_query *ntq;
813 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
814 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
815 u32 node, depth, type, lowbound, upbound;
816 static const char * const scope_str[] = {"", " zone", " cluster",
817 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800818 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100819
Baozeng Ding297f7d22016-05-24 22:33:24 +0800820 if (!attrs[TIPC_NLA_NAME_TABLE])
821 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100822
Baozeng Ding297f7d22016-05-24 22:33:24 +0800823 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200824 attrs[TIPC_NLA_NAME_TABLE], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800825 if (err)
826 return err;
827
828 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
829 return -EINVAL;
830
831 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +0200832 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800833 if (err)
834 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100835
836 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
837
838 depth = ntohl(ntq->depth);
839 type = ntohl(ntq->type);
840 lowbound = ntohl(ntq->lowbound);
841 upbound = ntohl(ntq->upbound);
842
843 if (!(depth & TIPC_NTQ_ALLTYPES) &&
844 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
845 return 0;
846 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
847 return 0;
848 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
849 return 0;
850
851 tipc_tlv_sprintf(msg->rep, "%-10u ",
852 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
853
854 if (depth == 1)
855 goto out;
856
857 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
858 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
859 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
860
861 if (depth == 2)
862 goto out;
863
864 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
865 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
866 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
867 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
868
869 if (depth == 3)
870 goto out;
871
872 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200873 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100874 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
875out:
876 tipc_tlv_sprintf(msg->rep, "\n");
877
878 return 0;
879}
880
Richard Alpe487d2a32015-02-09 09:50:11 +0100881static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
882 struct nlattr **attrs)
883{
884 u32 type, lower, upper;
885 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800886 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100887
Baozeng Ding297f7d22016-05-24 22:33:24 +0800888 if (!attrs[TIPC_NLA_PUBL])
889 return -EINVAL;
890
891 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
Johannes Bergfceb6432017-04-12 14:34:07 +0200892 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800893 if (err)
894 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100895
896 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
897 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
898 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
899
900 if (lower == upper)
901 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
902 else
903 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
904
905 return 0;
906}
907
908static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
909{
910 int err;
911 void *hdr;
912 struct nlattr *nest;
913 struct sk_buff *args;
914 struct tipc_nl_compat_cmd_dump dump;
915
916 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
917 if (!args)
918 return -ENOMEM;
919
920 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
921 TIPC_NL_PUBL_GET);
Gustavo A. R. Silvaf87d8ad2019-01-05 10:52:23 -0600922 if (!hdr) {
923 kfree_skb(args);
Kangjie Lu46273cf2018-12-26 00:09:04 -0600924 return -EMSGSIZE;
Gustavo A. R. Silvaf87d8ad2019-01-05 10:52:23 -0600925 }
Richard Alpe487d2a32015-02-09 09:50:11 +0100926
927 nest = nla_nest_start(args, TIPC_NLA_SOCK);
928 if (!nest) {
929 kfree_skb(args);
930 return -EMSGSIZE;
931 }
932
933 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
934 kfree_skb(args);
935 return -EMSGSIZE;
936 }
937
938 nla_nest_end(args, nest);
939 genlmsg_end(args, hdr);
940
941 dump.dumpit = tipc_nl_publ_dump;
942 dump.format = __tipc_nl_compat_publ_dump;
943
944 err = __tipc_nl_compat_dumpit(&dump, msg, args);
945
946 kfree_skb(args);
947
948 return err;
949}
950
951static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
952 struct nlattr **attrs)
953{
954 int err;
955 u32 sock_ref;
956 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
957
Baozeng Ding297f7d22016-05-24 22:33:24 +0800958 if (!attrs[TIPC_NLA_SOCK])
959 return -EINVAL;
960
961 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
Johannes Bergfceb6432017-04-12 14:34:07 +0200962 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800963 if (err)
964 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100965
966 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
967 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
968
969 if (sock[TIPC_NLA_SOCK_CON]) {
970 u32 node;
971 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
972
Aditya Pakki89dfd002018-12-23 18:54:53 -0600973 err = nla_parse_nested(con, TIPC_NLA_CON_MAX,
974 sock[TIPC_NLA_SOCK_CON], NULL, NULL);
975
976 if (err)
977 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100978
979 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
980 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
981 tipc_zone(node),
982 tipc_cluster(node),
983 tipc_node(node),
984 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
985
986 if (con[TIPC_NLA_CON_FLAG])
987 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
988 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
989 nla_get_u32(con[TIPC_NLA_CON_INST]));
990 else
991 tipc_tlv_sprintf(msg->rep, "\n");
992 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
993 tipc_tlv_sprintf(msg->rep, " bound to");
994
995 err = tipc_nl_compat_publ_dump(msg, sock_ref);
996 if (err)
997 return err;
998 }
999 tipc_tlv_sprintf(msg->rep, "\n");
1000
1001 return 0;
1002}
1003
Richard Alpe5bfc3352015-02-09 09:50:12 +01001004static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1005 struct nlattr **attrs)
1006{
1007 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001008 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001009
Baozeng Ding297f7d22016-05-24 22:33:24 +08001010 if (!attrs[TIPC_NLA_MEDIA])
1011 return -EINVAL;
1012
Johannes Bergfceb6432017-04-12 14:34:07 +02001013 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX,
1014 attrs[TIPC_NLA_MEDIA], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001015 if (err)
1016 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001017
1018 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1019 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1020 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1021}
1022
Richard Alpe4b28cb52015-02-09 09:50:13 +01001023static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1024 struct nlattr **attrs)
1025{
1026 struct tipc_node_info node_info;
1027 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001028 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001029
Baozeng Ding297f7d22016-05-24 22:33:24 +08001030 if (!attrs[TIPC_NLA_NODE])
1031 return -EINVAL;
1032
1033 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
Johannes Bergfceb6432017-04-12 14:34:07 +02001034 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001035 if (err)
1036 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001037
1038 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1039 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1040
1041 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1042 sizeof(node_info));
1043}
1044
Richard Alpec3d6fb82015-05-06 13:58:54 +02001045static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1046 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001047 struct tipc_nl_compat_msg *msg)
1048{
1049 u32 val;
1050 struct nlattr *net;
1051
1052 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1053
1054 net = nla_nest_start(skb, TIPC_NLA_NET);
1055 if (!net)
1056 return -EMSGSIZE;
1057
Richard Alpe964f9502015-02-09 09:50:15 +01001058 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1059 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1060 return -EMSGSIZE;
1061 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1062 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1063 return -EMSGSIZE;
1064 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001065 nla_nest_end(skb, net);
1066
1067 return 0;
1068}
1069
Richard Alpe3c261812015-02-09 09:50:16 +01001070static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1071 struct nlattr **attrs)
1072{
1073 __be32 id;
1074 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001075 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001076
Baozeng Ding297f7d22016-05-24 22:33:24 +08001077 if (!attrs[TIPC_NLA_NET])
1078 return -EINVAL;
1079
1080 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
Johannes Bergfceb6432017-04-12 14:34:07 +02001081 NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001082 if (err)
1083 return err;
1084
Richard Alpe3c261812015-02-09 09:50:16 +01001085 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1086
1087 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1088}
1089
Richard Alpe5a81a632015-02-09 09:50:17 +01001090static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1091{
1092 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1093 if (!msg->rep)
1094 return -ENOMEM;
1095
1096 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1097 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1098
1099 return 0;
1100}
1101
Richard Alped0796d12015-02-09 09:50:04 +01001102static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1103{
1104 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001105 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001106
1107 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001108 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001109
1110 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001111 case TIPC_CMD_NOOP:
1112 msg->rep = tipc_tlv_alloc(0);
1113 if (!msg->rep)
1114 return -ENOMEM;
1115 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001116 case TIPC_CMD_GET_BEARER_NAMES:
1117 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1118 dump.dumpit = tipc_nl_bearer_dump;
1119 dump.format = tipc_nl_compat_bearer_dump;
1120 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001121 case TIPC_CMD_ENABLE_BEARER:
1122 msg->req_type = TIPC_TLV_BEARER_CONFIG;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001123 doit.doit = __tipc_nl_bearer_enable;
Richard Alpe9ab15462015-02-09 09:50:05 +01001124 doit.transcode = tipc_nl_compat_bearer_enable;
1125 return tipc_nl_compat_doit(&doit, msg);
1126 case TIPC_CMD_DISABLE_BEARER:
1127 msg->req_type = TIPC_TLV_BEARER_NAME;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001128 doit.doit = __tipc_nl_bearer_disable;
Richard Alpe9ab15462015-02-09 09:50:05 +01001129 doit.transcode = tipc_nl_compat_bearer_disable;
1130 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d42015-02-09 09:50:06 +01001131 case TIPC_CMD_SHOW_LINK_STATS:
1132 msg->req_type = TIPC_TLV_LINK_NAME;
1133 msg->rep_size = ULTRA_STRING_MAX_LEN;
1134 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001135 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d42015-02-09 09:50:06 +01001136 dump.format = tipc_nl_compat_link_stat_dump;
1137 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001138 case TIPC_CMD_GET_LINKS:
1139 msg->req_type = TIPC_TLV_NET_ADDR;
1140 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001141 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001142 dump.format = tipc_nl_compat_link_dump;
1143 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001144 case TIPC_CMD_SET_LINK_TOL:
1145 case TIPC_CMD_SET_LINK_PRI:
1146 case TIPC_CMD_SET_LINK_WINDOW:
1147 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001148 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001149 doit.transcode = tipc_nl_compat_link_set;
1150 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001151 case TIPC_CMD_RESET_LINK_STATS:
1152 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001153 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001154 doit.transcode = tipc_nl_compat_link_reset_stats;
1155 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001156 case TIPC_CMD_SHOW_NAME_TABLE:
1157 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1158 msg->rep_size = ULTRA_STRING_MAX_LEN;
1159 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1160 dump.header = tipc_nl_compat_name_table_dump_header;
1161 dump.dumpit = tipc_nl_name_table_dump;
1162 dump.format = tipc_nl_compat_name_table_dump;
1163 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001164 case TIPC_CMD_SHOW_PORTS:
1165 msg->rep_size = ULTRA_STRING_MAX_LEN;
1166 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1167 dump.dumpit = tipc_nl_sk_dump;
1168 dump.format = tipc_nl_compat_sk_dump;
1169 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001170 case TIPC_CMD_GET_MEDIA_NAMES:
1171 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1172 dump.dumpit = tipc_nl_media_dump;
1173 dump.format = tipc_nl_compat_media_dump;
1174 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001175 case TIPC_CMD_GET_NODES:
1176 msg->rep_size = ULTRA_STRING_MAX_LEN;
1177 dump.dumpit = tipc_nl_node_dump;
1178 dump.format = tipc_nl_compat_node_dump;
1179 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001180 case TIPC_CMD_SET_NODE_ADDR:
1181 msg->req_type = TIPC_TLV_NET_ADDR;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001182 doit.doit = __tipc_nl_net_set;
Richard Alped7cc75d2015-02-09 09:50:14 +01001183 doit.transcode = tipc_nl_compat_net_set;
1184 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001185 case TIPC_CMD_SET_NETID:
1186 msg->req_type = TIPC_TLV_UNSIGNED;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001187 doit.doit = __tipc_nl_net_set;
Richard Alpe964f9502015-02-09 09:50:15 +01001188 doit.transcode = tipc_nl_compat_net_set;
1189 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001190 case TIPC_CMD_GET_NETID:
1191 msg->rep_size = sizeof(u32);
1192 dump.dumpit = tipc_nl_net_dump;
1193 dump.format = tipc_nl_compat_net_dump;
1194 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001195 case TIPC_CMD_SHOW_STATS:
1196 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001197 }
1198
1199 return -EOPNOTSUPP;
1200}
1201
1202static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1203{
1204 int err;
1205 int len;
1206 struct tipc_nl_compat_msg msg;
1207 struct nlmsghdr *req_nlh;
1208 struct nlmsghdr *rep_nlh;
1209 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001210
1211 memset(&msg, 0, sizeof(msg));
1212
1213 req_nlh = (struct nlmsghdr *)skb->data;
1214 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1215 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001216 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001217 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001218
1219 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1220 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1221 err = -EACCES;
1222 goto send;
1223 }
1224
1225 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Richard Alpe8f8ff9132015-08-17 14:15:10 +02001226 if (len && !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001227 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1228 err = -EOPNOTSUPP;
1229 goto send;
1230 }
1231
1232 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001233 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001234 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1235 else if (err == -EINVAL)
1236 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1237send:
1238 if (!msg.rep)
1239 return err;
1240
1241 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1242 skb_push(msg.rep, len);
1243 rep_nlh = nlmsg_hdr(msg.rep);
1244 memcpy(rep_nlh, info->nlhdr, len);
1245 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001246 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001247
1248 return err;
1249}
1250
Arvind Yadav042a9012017-08-23 16:22:20 +05301251static const struct genl_ops tipc_genl_compat_ops[] = {
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001252 {
1253 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001254 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001255 },
1256};
1257
Johannes Berg56989f62016-10-24 14:40:05 +02001258static struct genl_family tipc_genl_compat_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001259 .name = TIPC_GENL_NAME,
1260 .version = TIPC_GENL_VERSION,
1261 .hdrsize = TIPC_GENL_HDRLEN,
1262 .maxattr = 0,
1263 .netnsok = true,
1264 .module = THIS_MODULE,
1265 .ops = tipc_genl_compat_ops,
1266 .n_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1267};
1268
Johannes Berg56989f62016-10-24 14:40:05 +02001269int __init tipc_netlink_compat_start(void)
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001270{
1271 int res;
1272
Johannes Berg489111e2016-10-24 14:40:03 +02001273 res = genl_register_family(&tipc_genl_compat_family);
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001274 if (res) {
1275 pr_err("Failed to register legacy compat interface\n");
1276 return res;
1277 }
1278
1279 return 0;
1280}
1281
1282void tipc_netlink_compat_stop(void)
1283{
1284 genl_unregister_family(&tipc_genl_compat_family);
1285}