blob: f7269ce934b511441eac766bf5c65973bf359b91 [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
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 Alpef2b3b2d2015-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 Alpef2b3b2d2015-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 Alpef2b3b2d2015-02-09 09:50:06 +0100267 if (msg->rep_type)
268 tipc_tlv_init(msg->rep, msg->rep_type);
269
Xin Long2ac695d12019-03-31 22:50:10 +0800270 if (cmd->header) {
271 err = (*cmd->header)(msg);
272 if (err) {
273 kfree_skb(msg->rep);
274 msg->rep = NULL;
275 return err;
276 }
277 }
Richard Alpe44a8ae92015-02-09 09:50:10 +0100278
Richard Alped0796d12015-02-09 09:50:04 +0100279 arg = nlmsg_new(0, GFP_KERNEL);
280 if (!arg) {
281 kfree_skb(msg->rep);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700282 msg->rep = NULL;
Richard Alped0796d12015-02-09 09:50:04 +0100283 return -ENOMEM;
284 }
285
286 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700287 if (err) {
Richard Alped0796d12015-02-09 09:50:04 +0100288 kfree_skb(msg->rep);
Eric Dumazet5bfd37b2017-08-16 09:41:54 -0700289 msg->rep = NULL;
290 }
Richard Alped0796d12015-02-09 09:50:04 +0100291 kfree_skb(arg);
292
293 return err;
294}
295
Richard Alpe9ab15462015-02-09 09:50:05 +0100296static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
297 struct tipc_nl_compat_msg *msg)
298{
299 int err;
300 struct sk_buff *doit_buf;
301 struct sk_buff *trans_buf;
302 struct nlattr **attrbuf;
303 struct genl_info info;
304
305 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
306 if (!trans_buf)
307 return -ENOMEM;
308
Kees Cook6da2ec52018-06-12 13:55:00 -0700309 attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1,
310 sizeof(struct nlattr *),
311 GFP_KERNEL);
Richard Alpe9ab15462015-02-09 09:50:05 +0100312 if (!attrbuf) {
313 err = -ENOMEM;
314 goto trans_out;
315 }
316
Richard Alpe9ab15462015-02-09 09:50:05 +0100317 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
318 if (!doit_buf) {
319 err = -ENOMEM;
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800320 goto attrbuf_out;
Richard Alpe9ab15462015-02-09 09:50:05 +0100321 }
322
Richard Alpe9ab15462015-02-09 09:50:05 +0100323 memset(&info, 0, sizeof(info));
324 info.attrs = attrbuf;
325
Ying Xueed4ffdf2018-02-14 13:38:04 +0800326 rtnl_lock();
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800327 err = (*cmd->transcode)(cmd, trans_buf, msg);
328 if (err)
329 goto doit_out;
330
Johannes Berg8cb08172019-04-26 14:07:28 +0200331 err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr,
332 (const struct nlattr *)trans_buf->data,
333 trans_buf->len, NULL, NULL);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800334 if (err)
335 goto doit_out;
336
337 doit_buf->sk = msg->dst_sk;
338
Richard Alpe9ab15462015-02-09 09:50:05 +0100339 err = (*cmd->doit)(doit_buf, &info);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800340doit_out:
Ying Xueed4ffdf2018-02-14 13:38:04 +0800341 rtnl_unlock();
Richard Alpe9ab15462015-02-09 09:50:05 +0100342
343 kfree_skb(doit_buf);
Ying Xuee5d1a1e2018-02-14 13:37:58 +0800344attrbuf_out:
Richard Alpe9ab15462015-02-09 09:50:05 +0100345 kfree(attrbuf);
346trans_out:
347 kfree_skb(trans_buf);
348
349 return err;
350}
351
352static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
353 struct tipc_nl_compat_msg *msg)
354{
355 int err;
356
357 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
358 return -EINVAL;
359
360 err = __tipc_nl_compat_doit(cmd, msg);
361 if (err)
362 return err;
363
364 /* The legacy API considered an empty message a success message */
365 msg->rep = tipc_tlv_alloc(0);
366 if (!msg->rep)
367 return -ENOMEM;
368
369 return 0;
370}
371
Richard Alped0796d12015-02-09 09:50:04 +0100372static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
373 struct nlattr **attrs)
374{
375 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800376 int err;
Richard Alped0796d12015-02-09 09:50:04 +0100377
Baozeng Ding297f7d22016-05-24 22:33:24 +0800378 if (!attrs[TIPC_NLA_BEARER])
379 return -EINVAL;
380
Johannes Berg8cb08172019-04-26 14:07:28 +0200381 err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX,
382 attrs[TIPC_NLA_BEARER], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800383 if (err)
384 return err;
Richard Alped0796d12015-02-09 09:50:04 +0100385
386 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
387 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
388 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
389}
390
Richard Alpec3d6fb82015-05-06 13:58:54 +0200391static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
392 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100393 struct tipc_nl_compat_msg *msg)
394{
395 struct nlattr *prop;
396 struct nlattr *bearer;
397 struct tipc_bearer_config *b;
Ying Xue07622162019-01-14 17:22:26 +0800398 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100399
400 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
401
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200402 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
Richard Alpe9ab15462015-02-09 09:50:05 +0100403 if (!bearer)
404 return -EMSGSIZE;
405
Xin Long6f07e5f2019-03-31 22:50:08 +0800406 len = TLV_GET_DATA_LEN(msg->req);
407 len -= offsetof(struct tipc_bearer_config, name);
408 if (len <= 0)
409 return -EINVAL;
410
411 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
Ying Xue07622162019-01-14 17:22:26 +0800412 if (!string_is_valid(b->name, len))
413 return -EINVAL;
414
Richard Alpe9ab15462015-02-09 09:50:05 +0100415 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
416 return -EMSGSIZE;
417
418 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
419 return -EMSGSIZE;
420
421 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200422 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
Richard Alpe9ab15462015-02-09 09:50:05 +0100423 if (!prop)
424 return -EMSGSIZE;
425 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
426 return -EMSGSIZE;
427 nla_nest_end(skb, prop);
428 }
429 nla_nest_end(skb, bearer);
430
431 return 0;
432}
433
Richard Alpec3d6fb82015-05-06 13:58:54 +0200434static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
435 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100436 struct tipc_nl_compat_msg *msg)
437{
438 char *name;
439 struct nlattr *bearer;
Ying Xue07622162019-01-14 17:22:26 +0800440 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100441
442 name = (char *)TLV_DATA(msg->req);
443
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200444 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
Richard Alpe9ab15462015-02-09 09:50:05 +0100445 if (!bearer)
446 return -EMSGSIZE;
447
Ying Xue07622162019-01-14 17:22:26 +0800448 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
449 if (!string_is_valid(name, len))
450 return -EINVAL;
451
Richard Alpe9ab15462015-02-09 09:50:05 +0100452 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
453 return -EMSGSIZE;
454
455 nla_nest_end(skb, bearer);
456
457 return 0;
458}
459
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100460static inline u32 perc(u32 count, u32 total)
461{
462 return (count * 100 + (total / 2)) / total;
463}
464
465static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
466 struct nlattr *prop[], struct nlattr *stats[])
467{
468 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
469 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
470
471 tipc_tlv_sprintf(msg->rep,
472 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
473 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
474 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
475 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
476 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
477 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
478
479 tipc_tlv_sprintf(msg->rep,
480 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
481 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
482 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
483 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
484 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
485 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
486
487 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
488 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
489 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
490 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
491
492 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
493 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
494 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
495 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
496
497 tipc_tlv_sprintf(msg->rep,
498 " Congestion link:%u Send queue max:%u avg:%u",
499 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
500 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
501 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
502}
503
504static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
505 struct nlattr **attrs)
506{
507 char *name;
508 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
509 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
510 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800511 int err;
Ying Xue07622162019-01-14 17:22:26 +0800512 int len;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100513
Baozeng Ding297f7d22016-05-24 22:33:24 +0800514 if (!attrs[TIPC_NLA_LINK])
515 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100516
Johannes Berg8cb08172019-04-26 14:07:28 +0200517 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
518 attrs[TIPC_NLA_LINK], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800519 if (err)
520 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100521
Baozeng Ding297f7d22016-05-24 22:33:24 +0800522 if (!link[TIPC_NLA_LINK_PROP])
523 return -EINVAL;
524
Johannes Berg8cb08172019-04-26 14:07:28 +0200525 err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX,
526 link[TIPC_NLA_LINK_PROP], NULL,
527 NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800528 if (err)
529 return err;
530
531 if (!link[TIPC_NLA_LINK_STATS])
532 return -EINVAL;
533
Johannes Berg8cb08172019-04-26 14:07:28 +0200534 err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX,
535 link[TIPC_NLA_LINK_STATS], NULL,
536 NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800537 if (err)
538 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100539
540 name = (char *)TLV_DATA(msg->req);
Ying Xue07622162019-01-14 17:22:26 +0800541
542 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
543 if (!string_is_valid(name, len))
544 return -EINVAL;
545
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100546 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
547 return 0;
548
549 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
550 nla_data(link[TIPC_NLA_LINK_NAME]));
551
552 if (link[TIPC_NLA_LINK_BROADCAST]) {
553 __fill_bc_link_stat(msg, prop, stats);
554 return 0;
555 }
556
557 if (link[TIPC_NLA_LINK_ACTIVE])
558 tipc_tlv_sprintf(msg->rep, " ACTIVE");
559 else if (link[TIPC_NLA_LINK_UP])
560 tipc_tlv_sprintf(msg->rep, " STANDBY");
561 else
562 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
563
564 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
565 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
566 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
567
568 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
569 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
570 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
571
572 tipc_tlv_sprintf(msg->rep,
573 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
574 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
575 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
576 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
577 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
578 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
579 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
580
581 tipc_tlv_sprintf(msg->rep,
582 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
583 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
584 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
585 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
586 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
587 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
588 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
589
590 tipc_tlv_sprintf(msg->rep,
591 " TX profile sample:%u packets average:%u octets\n",
592 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
593 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
594 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
595
596 tipc_tlv_sprintf(msg->rep,
597 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
598 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
599 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
600 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
601 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
602 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
603 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
604 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
605 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
606
607 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
608 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
609 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
610 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
611 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
612 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
613 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
614
615 tipc_tlv_sprintf(msg->rep,
616 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
617 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
618 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
619 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
620 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
621 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
622
623 tipc_tlv_sprintf(msg->rep,
624 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
625 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
626 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
627 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
628 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
629 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
630
631 tipc_tlv_sprintf(msg->rep,
632 " Congestion link:%u Send queue max:%u avg:%u",
633 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
634 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
635 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
636
637 return 0;
638}
639
Richard Alpe357ebdb2015-02-09 09:50:07 +0100640static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
641 struct nlattr **attrs)
642{
643 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
644 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800645 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100646
Baozeng Ding297f7d22016-05-24 22:33:24 +0800647 if (!attrs[TIPC_NLA_LINK])
648 return -EINVAL;
649
Johannes Berg8cb08172019-04-26 14:07:28 +0200650 err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX,
651 attrs[TIPC_NLA_LINK], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800652 if (err)
653 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100654
655 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
656 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200657 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400658 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100659
660 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
661 &link_info, sizeof(link_info));
662}
663
Richard Alpec3d6fb82015-05-06 13:58:54 +0200664static int __tipc_add_link_prop(struct sk_buff *skb,
665 struct tipc_nl_compat_msg *msg,
666 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100667{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200668 switch (msg->cmd) {
669 case TIPC_CMD_SET_LINK_PRI:
670 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
671 case TIPC_CMD_SET_LINK_TOL:
672 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
673 case TIPC_CMD_SET_LINK_WINDOW:
674 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
675 }
676
677 return -EINVAL;
678}
679
680static int tipc_nl_compat_media_set(struct sk_buff *skb,
681 struct tipc_nl_compat_msg *msg)
682{
Richard Alpe37e2d482015-02-09 09:50:08 +0100683 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200684 struct nlattr *media;
685 struct tipc_link_config *lc;
Ying Xue07622162019-01-14 17:22:26 +0800686 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200687
688 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
689
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200690 media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA);
Richard Alpec3d6fb82015-05-06 13:58:54 +0200691 if (!media)
692 return -EMSGSIZE;
693
Ying Xue07622162019-01-14 17:22:26 +0800694 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
695 if (!string_is_valid(lc->name, len))
696 return -EINVAL;
697
Richard Alpec3d6fb82015-05-06 13:58:54 +0200698 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
699 return -EMSGSIZE;
700
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200701 prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP);
Richard Alpec3d6fb82015-05-06 13:58:54 +0200702 if (!prop)
703 return -EMSGSIZE;
704
705 __tipc_add_link_prop(skb, msg, lc);
706 nla_nest_end(skb, prop);
707 nla_nest_end(skb, media);
708
709 return 0;
710}
711
712static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
713 struct tipc_nl_compat_msg *msg)
714{
715 struct nlattr *prop;
716 struct nlattr *bearer;
717 struct tipc_link_config *lc;
Ying Xue07622162019-01-14 17:22:26 +0800718 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200719
720 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
721
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200722 bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER);
Richard Alpec3d6fb82015-05-06 13:58:54 +0200723 if (!bearer)
724 return -EMSGSIZE;
725
Ying Xue07622162019-01-14 17:22:26 +0800726 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
727 if (!string_is_valid(lc->name, len))
728 return -EINVAL;
729
Richard Alpec3d6fb82015-05-06 13:58:54 +0200730 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
731 return -EMSGSIZE;
732
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200733 prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP);
Richard Alpec3d6fb82015-05-06 13:58:54 +0200734 if (!prop)
735 return -EMSGSIZE;
736
737 __tipc_add_link_prop(skb, msg, lc);
738 nla_nest_end(skb, prop);
739 nla_nest_end(skb, bearer);
740
741 return 0;
742}
743
744static int __tipc_nl_compat_link_set(struct sk_buff *skb,
745 struct tipc_nl_compat_msg *msg)
746{
747 struct nlattr *prop;
748 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100749 struct tipc_link_config *lc;
750
751 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
752
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200753 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
Richard Alpe37e2d482015-02-09 09:50:08 +0100754 if (!link)
755 return -EMSGSIZE;
756
757 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
758 return -EMSGSIZE;
759
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200760 prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP);
Richard Alpe37e2d482015-02-09 09:50:08 +0100761 if (!prop)
762 return -EMSGSIZE;
763
Richard Alpec3d6fb82015-05-06 13:58:54 +0200764 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100765 nla_nest_end(skb, prop);
766 nla_nest_end(skb, link);
767
768 return 0;
769}
770
Richard Alpec3d6fb82015-05-06 13:58:54 +0200771static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
772 struct sk_buff *skb,
773 struct tipc_nl_compat_msg *msg)
774{
775 struct tipc_link_config *lc;
776 struct tipc_bearer *bearer;
777 struct tipc_media *media;
Ying Xueedf5ff02019-01-14 17:22:27 +0800778 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200779
780 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
781
Xin Long8c63bf92019-03-31 22:50:09 +0800782 len = TLV_GET_DATA_LEN(msg->req);
783 len -= offsetof(struct tipc_link_config, name);
784 if (len <= 0)
785 return -EINVAL;
786
787 len = min_t(int, len, TIPC_MAX_LINK_NAME);
Ying Xueedf5ff02019-01-14 17:22:27 +0800788 if (!string_is_valid(lc->name, len))
789 return -EINVAL;
790
Richard Alpec3d6fb82015-05-06 13:58:54 +0200791 media = tipc_media_find(lc->name);
792 if (media) {
Ying Xueed4ffdf2018-02-14 13:38:04 +0800793 cmd->doit = &__tipc_nl_media_set;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200794 return tipc_nl_compat_media_set(skb, msg);
795 }
796
797 bearer = tipc_bearer_find(msg->net, lc->name);
798 if (bearer) {
Ying Xueed4ffdf2018-02-14 13:38:04 +0800799 cmd->doit = &__tipc_nl_bearer_set;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200800 return tipc_nl_compat_bearer_set(skb, msg);
801 }
802
803 return __tipc_nl_compat_link_set(skb, msg);
804}
805
806static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
807 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100808 struct tipc_nl_compat_msg *msg)
809{
810 char *name;
811 struct nlattr *link;
Ying Xue8b66fee2019-01-14 17:22:25 +0800812 int len;
Richard Alpe18178772015-02-09 09:50:09 +0100813
814 name = (char *)TLV_DATA(msg->req);
815
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200816 link = nla_nest_start_noflag(skb, TIPC_NLA_LINK);
Richard Alpe18178772015-02-09 09:50:09 +0100817 if (!link)
818 return -EMSGSIZE;
819
Ying Xue8b66fee2019-01-14 17:22:25 +0800820 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
821 if (!string_is_valid(name, len))
822 return -EINVAL;
823
Richard Alpe18178772015-02-09 09:50:09 +0100824 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
825 return -EMSGSIZE;
826
827 nla_nest_end(skb, link);
828
829 return 0;
830}
831
Richard Alpe44a8ae92015-02-09 09:50:10 +0100832static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
833{
834 int i;
835 u32 depth;
836 struct tipc_name_table_query *ntq;
837 static const char * const header[] = {
838 "Type ",
839 "Lower Upper ",
840 "Port Identity ",
841 "Publication Scope"
842 };
843
844 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
Ying Xue974cb0e2019-01-14 17:22:28 +0800845 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
846 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100847
848 depth = ntohl(ntq->depth);
849
850 if (depth > 4)
851 depth = 4;
852 for (i = 0; i < depth; i++)
853 tipc_tlv_sprintf(msg->rep, header[i]);
854 tipc_tlv_sprintf(msg->rep, "\n");
855
856 return 0;
857}
858
859static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
860 struct nlattr **attrs)
861{
862 char port_str[27];
863 struct tipc_name_table_query *ntq;
864 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
865 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
866 u32 node, depth, type, lowbound, upbound;
867 static const char * const scope_str[] = {"", " zone", " cluster",
868 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800869 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100870
Baozeng Ding297f7d22016-05-24 22:33:24 +0800871 if (!attrs[TIPC_NLA_NAME_TABLE])
872 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100873
Johannes Berg8cb08172019-04-26 14:07:28 +0200874 err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX,
875 attrs[TIPC_NLA_NAME_TABLE], NULL,
876 NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800877 if (err)
878 return err;
879
880 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
881 return -EINVAL;
882
Johannes Berg8cb08172019-04-26 14:07:28 +0200883 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
884 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL,
885 NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800886 if (err)
887 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100888
889 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
890
891 depth = ntohl(ntq->depth);
892 type = ntohl(ntq->type);
893 lowbound = ntohl(ntq->lowbound);
894 upbound = ntohl(ntq->upbound);
895
896 if (!(depth & TIPC_NTQ_ALLTYPES) &&
897 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
898 return 0;
899 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
900 return 0;
901 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
902 return 0;
903
904 tipc_tlv_sprintf(msg->rep, "%-10u ",
905 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
906
907 if (depth == 1)
908 goto out;
909
910 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
911 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
912 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
913
914 if (depth == 2)
915 goto out;
916
917 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
918 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
919 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
920 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
921
922 if (depth == 3)
923 goto out;
924
925 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200926 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100927 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
928out:
929 tipc_tlv_sprintf(msg->rep, "\n");
930
931 return 0;
932}
933
Richard Alpe487d2a32015-02-09 09:50:11 +0100934static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
935 struct nlattr **attrs)
936{
937 u32 type, lower, upper;
938 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800939 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100940
Baozeng Ding297f7d22016-05-24 22:33:24 +0800941 if (!attrs[TIPC_NLA_PUBL])
942 return -EINVAL;
943
Johannes Berg8cb08172019-04-26 14:07:28 +0200944 err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX,
945 attrs[TIPC_NLA_PUBL], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +0800946 if (err)
947 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100948
949 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
950 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
951 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
952
953 if (lower == upper)
954 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
955 else
956 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
957
958 return 0;
959}
960
961static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
962{
963 int err;
964 void *hdr;
965 struct nlattr *nest;
966 struct sk_buff *args;
967 struct tipc_nl_compat_cmd_dump dump;
968
969 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
970 if (!args)
971 return -ENOMEM;
972
973 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
974 TIPC_NL_PUBL_GET);
Gustavo A. R. Silvaf87d8ad2019-01-05 10:52:23 -0600975 if (!hdr) {
976 kfree_skb(args);
Kangjie Lu46273cf2018-12-26 00:09:04 -0600977 return -EMSGSIZE;
Gustavo A. R. Silvaf87d8ad2019-01-05 10:52:23 -0600978 }
Richard Alpe487d2a32015-02-09 09:50:11 +0100979
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200980 nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK);
Richard Alpe487d2a32015-02-09 09:50:11 +0100981 if (!nest) {
982 kfree_skb(args);
983 return -EMSGSIZE;
984 }
985
986 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
987 kfree_skb(args);
988 return -EMSGSIZE;
989 }
990
991 nla_nest_end(args, nest);
992 genlmsg_end(args, hdr);
993
994 dump.dumpit = tipc_nl_publ_dump;
995 dump.format = __tipc_nl_compat_publ_dump;
996
997 err = __tipc_nl_compat_dumpit(&dump, msg, args);
998
999 kfree_skb(args);
1000
1001 return err;
1002}
1003
1004static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1005 struct nlattr **attrs)
1006{
1007 int err;
1008 u32 sock_ref;
1009 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1010
Baozeng Ding297f7d22016-05-24 22:33:24 +08001011 if (!attrs[TIPC_NLA_SOCK])
1012 return -EINVAL;
1013
Johannes Berg8cb08172019-04-26 14:07:28 +02001014 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
1015 attrs[TIPC_NLA_SOCK], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001016 if (err)
1017 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +01001018
1019 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1020 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1021
1022 if (sock[TIPC_NLA_SOCK_CON]) {
1023 u32 node;
1024 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1025
Johannes Berg8cb08172019-04-26 14:07:28 +02001026 err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX,
1027 sock[TIPC_NLA_SOCK_CON],
1028 NULL, NULL);
Aditya Pakki89dfd002018-12-23 18:54:53 -06001029
1030 if (err)
1031 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +01001032
1033 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1034 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1035 tipc_zone(node),
1036 tipc_cluster(node),
1037 tipc_node(node),
1038 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1039
1040 if (con[TIPC_NLA_CON_FLAG])
1041 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1042 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1043 nla_get_u32(con[TIPC_NLA_CON_INST]));
1044 else
1045 tipc_tlv_sprintf(msg->rep, "\n");
1046 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1047 tipc_tlv_sprintf(msg->rep, " bound to");
1048
1049 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1050 if (err)
1051 return err;
1052 }
1053 tipc_tlv_sprintf(msg->rep, "\n");
1054
1055 return 0;
1056}
1057
Richard Alpe5bfc3352015-02-09 09:50:12 +01001058static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1059 struct nlattr **attrs)
1060{
1061 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001062 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001063
Baozeng Ding297f7d22016-05-24 22:33:24 +08001064 if (!attrs[TIPC_NLA_MEDIA])
1065 return -EINVAL;
1066
Johannes Berg8cb08172019-04-26 14:07:28 +02001067 err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX,
1068 attrs[TIPC_NLA_MEDIA], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001069 if (err)
1070 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001071
1072 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1073 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1074 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1075}
1076
Richard Alpe4b28cb52015-02-09 09:50:13 +01001077static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1078 struct nlattr **attrs)
1079{
1080 struct tipc_node_info node_info;
1081 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001082 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001083
Baozeng Ding297f7d22016-05-24 22:33:24 +08001084 if (!attrs[TIPC_NLA_NODE])
1085 return -EINVAL;
1086
Johannes Berg8cb08172019-04-26 14:07:28 +02001087 err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX,
1088 attrs[TIPC_NLA_NODE], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001089 if (err)
1090 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001091
1092 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1093 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1094
1095 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1096 sizeof(node_info));
1097}
1098
Richard Alpec3d6fb82015-05-06 13:58:54 +02001099static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1100 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001101 struct tipc_nl_compat_msg *msg)
1102{
1103 u32 val;
1104 struct nlattr *net;
1105
1106 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1107
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001108 net = nla_nest_start_noflag(skb, TIPC_NLA_NET);
Richard Alped7cc75d2015-02-09 09:50:14 +01001109 if (!net)
1110 return -EMSGSIZE;
1111
Richard Alpe964f9502015-02-09 09:50:15 +01001112 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1113 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1114 return -EMSGSIZE;
1115 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1116 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1117 return -EMSGSIZE;
1118 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001119 nla_nest_end(skb, net);
1120
1121 return 0;
1122}
1123
Richard Alpe3c261812015-02-09 09:50:16 +01001124static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1125 struct nlattr **attrs)
1126{
1127 __be32 id;
1128 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001129 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001130
Baozeng Ding297f7d22016-05-24 22:33:24 +08001131 if (!attrs[TIPC_NLA_NET])
1132 return -EINVAL;
1133
Johannes Berg8cb08172019-04-26 14:07:28 +02001134 err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX,
1135 attrs[TIPC_NLA_NET], NULL, NULL);
Baozeng Ding297f7d22016-05-24 22:33:24 +08001136 if (err)
1137 return err;
1138
Richard Alpe3c261812015-02-09 09:50:16 +01001139 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1140
1141 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1142}
1143
Richard Alpe5a81a632015-02-09 09:50:17 +01001144static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1145{
1146 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1147 if (!msg->rep)
1148 return -ENOMEM;
1149
1150 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1151 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1152
1153 return 0;
1154}
1155
Richard Alped0796d12015-02-09 09:50:04 +01001156static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1157{
1158 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001159 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001160
1161 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001162 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001163
1164 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001165 case TIPC_CMD_NOOP:
1166 msg->rep = tipc_tlv_alloc(0);
1167 if (!msg->rep)
1168 return -ENOMEM;
1169 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001170 case TIPC_CMD_GET_BEARER_NAMES:
1171 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1172 dump.dumpit = tipc_nl_bearer_dump;
1173 dump.format = tipc_nl_compat_bearer_dump;
1174 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001175 case TIPC_CMD_ENABLE_BEARER:
1176 msg->req_type = TIPC_TLV_BEARER_CONFIG;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001177 doit.doit = __tipc_nl_bearer_enable;
Richard Alpe9ab15462015-02-09 09:50:05 +01001178 doit.transcode = tipc_nl_compat_bearer_enable;
1179 return tipc_nl_compat_doit(&doit, msg);
1180 case TIPC_CMD_DISABLE_BEARER:
1181 msg->req_type = TIPC_TLV_BEARER_NAME;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001182 doit.doit = __tipc_nl_bearer_disable;
Richard Alpe9ab15462015-02-09 09:50:05 +01001183 doit.transcode = tipc_nl_compat_bearer_disable;
1184 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001185 case TIPC_CMD_SHOW_LINK_STATS:
1186 msg->req_type = TIPC_TLV_LINK_NAME;
1187 msg->rep_size = ULTRA_STRING_MAX_LEN;
1188 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001189 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001190 dump.format = tipc_nl_compat_link_stat_dump;
1191 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001192 case TIPC_CMD_GET_LINKS:
1193 msg->req_type = TIPC_TLV_NET_ADDR;
1194 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001195 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001196 dump.format = tipc_nl_compat_link_dump;
1197 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001198 case TIPC_CMD_SET_LINK_TOL:
1199 case TIPC_CMD_SET_LINK_PRI:
1200 case TIPC_CMD_SET_LINK_WINDOW:
1201 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001202 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001203 doit.transcode = tipc_nl_compat_link_set;
1204 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001205 case TIPC_CMD_RESET_LINK_STATS:
1206 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001207 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001208 doit.transcode = tipc_nl_compat_link_reset_stats;
1209 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001210 case TIPC_CMD_SHOW_NAME_TABLE:
1211 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1212 msg->rep_size = ULTRA_STRING_MAX_LEN;
1213 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1214 dump.header = tipc_nl_compat_name_table_dump_header;
1215 dump.dumpit = tipc_nl_name_table_dump;
1216 dump.format = tipc_nl_compat_name_table_dump;
1217 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001218 case TIPC_CMD_SHOW_PORTS:
1219 msg->rep_size = ULTRA_STRING_MAX_LEN;
1220 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1221 dump.dumpit = tipc_nl_sk_dump;
1222 dump.format = tipc_nl_compat_sk_dump;
1223 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001224 case TIPC_CMD_GET_MEDIA_NAMES:
1225 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1226 dump.dumpit = tipc_nl_media_dump;
1227 dump.format = tipc_nl_compat_media_dump;
1228 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001229 case TIPC_CMD_GET_NODES:
1230 msg->rep_size = ULTRA_STRING_MAX_LEN;
1231 dump.dumpit = tipc_nl_node_dump;
1232 dump.format = tipc_nl_compat_node_dump;
1233 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001234 case TIPC_CMD_SET_NODE_ADDR:
1235 msg->req_type = TIPC_TLV_NET_ADDR;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001236 doit.doit = __tipc_nl_net_set;
Richard Alped7cc75d2015-02-09 09:50:14 +01001237 doit.transcode = tipc_nl_compat_net_set;
1238 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001239 case TIPC_CMD_SET_NETID:
1240 msg->req_type = TIPC_TLV_UNSIGNED;
Ying Xueed4ffdf2018-02-14 13:38:04 +08001241 doit.doit = __tipc_nl_net_set;
Richard Alpe964f9502015-02-09 09:50:15 +01001242 doit.transcode = tipc_nl_compat_net_set;
1243 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001244 case TIPC_CMD_GET_NETID:
1245 msg->rep_size = sizeof(u32);
1246 dump.dumpit = tipc_nl_net_dump;
1247 dump.format = tipc_nl_compat_net_dump;
1248 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001249 case TIPC_CMD_SHOW_STATS:
1250 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001251 }
1252
1253 return -EOPNOTSUPP;
1254}
1255
1256static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1257{
1258 int err;
1259 int len;
1260 struct tipc_nl_compat_msg msg;
1261 struct nlmsghdr *req_nlh;
1262 struct nlmsghdr *rep_nlh;
1263 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001264
1265 memset(&msg, 0, sizeof(msg));
1266
1267 req_nlh = (struct nlmsghdr *)skb->data;
1268 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1269 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001270 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001271 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001272
1273 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1274 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1275 err = -EACCES;
1276 goto send;
1277 }
1278
1279 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Ying Xue2753ca52019-01-14 17:22:29 +08001280 if (!len || !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001281 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1282 err = -EOPNOTSUPP;
1283 goto send;
1284 }
1285
1286 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001287 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001288 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1289 else if (err == -EINVAL)
1290 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1291send:
1292 if (!msg.rep)
1293 return err;
1294
1295 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1296 skb_push(msg.rep, len);
1297 rep_nlh = nlmsg_hdr(msg.rep);
1298 memcpy(rep_nlh, info->nlhdr, len);
1299 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001300 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001301
1302 return err;
1303}
1304
Arvind Yadav042a9012017-08-23 16:22:20 +05301305static const struct genl_ops tipc_genl_compat_ops[] = {
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001306 {
1307 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001308 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001309 },
1310};
1311
Johannes Berg56989f62016-10-24 14:40:05 +02001312static struct genl_family tipc_genl_compat_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001313 .name = TIPC_GENL_NAME,
1314 .version = TIPC_GENL_VERSION,
1315 .hdrsize = TIPC_GENL_HDRLEN,
1316 .maxattr = 0,
1317 .netnsok = true,
1318 .module = THIS_MODULE,
1319 .ops = tipc_genl_compat_ops,
1320 .n_ops = ARRAY_SIZE(tipc_genl_compat_ops),
1321};
1322
Johannes Berg56989f62016-10-24 14:40:05 +02001323int __init tipc_netlink_compat_start(void)
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001324{
1325 int res;
1326
Johannes Berg489111e2016-10-24 14:40:03 +02001327 res = genl_register_family(&tipc_genl_compat_family);
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001328 if (res) {
1329 pr_err("Failed to register legacy compat interface\n");
1330 return res;
1331 }
1332
1333 return 0;
1334}
1335
1336void tipc_netlink_compat_stop(void)
1337{
1338 genl_unregister_family(&tipc_genl_compat_family);
1339}