blob: cdc49e680be4b9571ca116f759e861617f0124d6 [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;
Taras Kondratiuk695074c2019-07-29 22:15:07 +000058 int req_size;
Richard Alpec3d6fb82015-05-06 13:58:54 +020059 struct net *net;
Richard Alped0796d12015-02-09 09:50:04 +010060 struct sk_buff *rep;
61 struct tlv_desc *req;
62 struct sock *dst_sk;
63};
64
65struct tipc_nl_compat_cmd_dump {
Richard Alpe44a8ae92015-02-09 09:50:10 +010066 int (*header)(struct tipc_nl_compat_msg *);
Richard Alped0796d12015-02-09 09:50:04 +010067 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
68 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
69};
70
Richard Alpe9ab15462015-02-09 09:50:05 +010071struct tipc_nl_compat_cmd_doit {
72 int (*doit)(struct sk_buff *skb, struct genl_info *info);
Richard Alpec3d6fb82015-05-06 13:58:54 +020073 int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
74 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
Richard Alpe9ab15462015-02-09 09:50:05 +010075};
76
Richard Alped0796d12015-02-09 09:50:04 +010077static int tipc_skb_tailroom(struct sk_buff *skb)
78{
79 int tailroom;
80 int limit;
81
82 tailroom = skb_tailroom(skb);
83 limit = TIPC_SKB_MAX - skb->len;
84
85 if (tailroom < limit)
86 return tailroom;
87
88 return limit;
89}
90
Ying Xue4cd995a2019-01-14 17:22:25 +080091static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
92{
93 return TLV_GET_LEN(tlv) - TLV_SPACE(0);
94}
95
Richard Alped0796d12015-02-09 09:50:04 +010096static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
97{
98 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
99
100 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
101 return -EMSGSIZE;
102
103 skb_put(skb, TLV_SPACE(len));
104 tlv->tlv_type = htons(type);
105 tlv->tlv_len = htons(TLV_LENGTH(len));
106 if (len && data)
107 memcpy(TLV_DATA(tlv), data, len);
108
109 return 0;
110}
111
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100112static void tipc_tlv_init(struct sk_buff *skb, u16 type)
113{
114 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
115
116 TLV_SET_LEN(tlv, 0);
117 TLV_SET_TYPE(tlv, type);
118 skb_put(skb, sizeof(struct tlv_desc));
119}
120
121static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
122{
123 int n;
124 u16 len;
125 u32 rem;
126 char *buf;
127 struct tlv_desc *tlv;
128 va_list args;
129
130 rem = tipc_skb_tailroom(skb);
131
132 tlv = (struct tlv_desc *)skb->data;
133 len = TLV_GET_LEN(tlv);
134 buf = TLV_DATA(tlv) + len;
135
136 va_start(args, fmt);
137 n = vscnprintf(buf, rem, fmt, args);
138 va_end(args);
139
140 TLV_SET_LEN(tlv, n + len);
141 skb_put(skb, n);
142
143 return n;
144}
145
Richard Alped0796d12015-02-09 09:50:04 +0100146static struct sk_buff *tipc_tlv_alloc(int size)
147{
148 int hdr_len;
149 struct sk_buff *buf;
150
151 size = TLV_SPACE(size);
152 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
153
154 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
155 if (!buf)
156 return NULL;
157
158 skb_reserve(buf, hdr_len);
159
160 return buf;
161}
162
163static struct sk_buff *tipc_get_err_tlv(char *str)
164{
165 int str_len = strlen(str) + 1;
166 struct sk_buff *buf;
167
168 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
169 if (buf)
170 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
171
172 return buf;
173}
174
Ying Xue4cd995a2019-01-14 17:22:25 +0800175static inline bool string_is_valid(char *s, int len)
176{
177 return memchr(s, '\0', len) ? true : false;
178}
179
Richard Alped0796d12015-02-09 09:50:04 +0100180static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
181 struct tipc_nl_compat_msg *msg,
182 struct sk_buff *arg)
183{
184 int len = 0;
185 int err;
186 struct sk_buff *buf;
187 struct nlmsghdr *nlmsg;
188 struct netlink_callback cb;
189
190 memset(&cb, 0, sizeof(cb));
191 cb.nlh = (struct nlmsghdr *)arg->data;
192 cb.skb = arg;
193
194 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
195 if (!buf)
196 return -ENOMEM;
197
198 buf->sk = msg->dst_sk;
199
200 do {
201 int rem;
202
203 len = (*cmd->dumpit)(buf, &cb);
204
205 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
206 struct nlattr **attrs;
207
208 err = tipc_nlmsg_parse(nlmsg, &attrs);
209 if (err)
210 goto err_out;
211
212 err = (*cmd->format)(msg, attrs);
213 if (err)
214 goto err_out;
215
216 if (tipc_skb_tailroom(msg->rep) <= 1) {
217 err = -EMSGSIZE;
218 goto err_out;
219 }
220 }
221
222 skb_reset_tail_pointer(buf);
223 buf->len = 0;
224
225 } while (len);
226
227 err = 0;
228
229err_out:
230 kfree_skb(buf);
231
232 if (err == -EMSGSIZE) {
233 /* The legacy API only considered messages filling
234 * "ULTRA_STRING_MAX_LEN" to be truncated.
235 */
236 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
237 char *tail = skb_tail_pointer(msg->rep);
238
239 if (*tail != '\0')
240 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
241 REPLY_TRUNCATED);
242 }
243
244 return 0;
245 }
246
247 return err;
248}
249
250static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
251 struct tipc_nl_compat_msg *msg)
252{
253 int err;
254 struct sk_buff *arg;
255
Taras Kondratiuk695074c2019-07-29 22:15:07 +0000256 if (msg->req_type && (!msg->req_size ||
257 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100258 return -EINVAL;
259
Richard Alped0796d12015-02-09 09:50:04 +0100260 msg->rep = tipc_tlv_alloc(msg->rep_size);
261 if (!msg->rep)
262 return -ENOMEM;
263
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100264 if (msg->rep_type)
265 tipc_tlv_init(msg->rep, msg->rep_type);
266
Xin Longd2618e32019-03-31 22:50:10 +0800267 if (cmd->header) {
268 err = (*cmd->header)(msg);
269 if (err) {
270 kfree_skb(msg->rep);
271 msg->rep = NULL;
272 return err;
273 }
274 }
Richard Alpe44a8ae92015-02-09 09:50:10 +0100275
Richard Alped0796d12015-02-09 09:50:04 +0100276 arg = nlmsg_new(0, GFP_KERNEL);
277 if (!arg) {
278 kfree_skb(msg->rep);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700279 msg->rep = NULL;
Richard Alped0796d12015-02-09 09:50:04 +0100280 return -ENOMEM;
281 }
282
283 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700284 if (err) {
Richard Alped0796d12015-02-09 09:50:04 +0100285 kfree_skb(msg->rep);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700286 msg->rep = NULL;
287 }
Richard Alped0796d12015-02-09 09:50:04 +0100288 kfree_skb(arg);
289
290 return err;
291}
292
Richard Alpe9ab15462015-02-09 09:50:05 +0100293static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
294 struct tipc_nl_compat_msg *msg)
295{
296 int err;
297 struct sk_buff *doit_buf;
298 struct sk_buff *trans_buf;
299 struct nlattr **attrbuf;
300 struct genl_info info;
301
302 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
303 if (!trans_buf)
304 return -ENOMEM;
305
Richard Alpec3d6fb82015-05-06 13:58:54 +0200306 err = (*cmd->transcode)(cmd, trans_buf, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100307 if (err)
308 goto trans_out;
309
310 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
311 sizeof(struct nlattr *), GFP_KERNEL);
312 if (!attrbuf) {
313 err = -ENOMEM;
314 goto trans_out;
315 }
316
317 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
318 (const struct nlattr *)trans_buf->data,
319 trans_buf->len, NULL);
320 if (err)
321 goto parse_out;
322
323 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
324 if (!doit_buf) {
325 err = -ENOMEM;
326 goto parse_out;
327 }
328
329 doit_buf->sk = msg->dst_sk;
330
331 memset(&info, 0, sizeof(info));
332 info.attrs = attrbuf;
333
334 err = (*cmd->doit)(doit_buf, &info);
335
336 kfree_skb(doit_buf);
337parse_out:
338 kfree(attrbuf);
339trans_out:
340 kfree_skb(trans_buf);
341
342 return err;
343}
344
345static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
346 struct tipc_nl_compat_msg *msg)
347{
348 int err;
349
Taras Kondratiuk695074c2019-07-29 22:15:07 +0000350 if (msg->req_type && (!msg->req_size ||
351 !TLV_CHECK_TYPE(msg->req, msg->req_type)))
Richard Alpe9ab15462015-02-09 09:50:05 +0100352 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,
376 attrs[TIPC_NLA_BEARER], NULL);
377 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;
Ying Xue7d0cb252019-01-14 17:22:26 +0800392 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100393
394 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
395
396 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
397 if (!bearer)
398 return -EMSGSIZE;
399
Xin Longa3791722019-03-31 22:50:08 +0800400 len = TLV_GET_DATA_LEN(msg->req);
401 len -= offsetof(struct tipc_bearer_config, name);
402 if (len <= 0)
403 return -EINVAL;
404
405 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
Ying Xue7d0cb252019-01-14 17:22:26 +0800406 if (!string_is_valid(b->name, len))
407 return -EINVAL;
408
Richard Alpe9ab15462015-02-09 09:50:05 +0100409 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
410 return -EMSGSIZE;
411
412 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
413 return -EMSGSIZE;
414
415 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
416 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
417 if (!prop)
418 return -EMSGSIZE;
419 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
420 return -EMSGSIZE;
421 nla_nest_end(skb, prop);
422 }
423 nla_nest_end(skb, bearer);
424
425 return 0;
426}
427
Richard Alpec3d6fb82015-05-06 13:58:54 +0200428static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
429 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100430 struct tipc_nl_compat_msg *msg)
431{
432 char *name;
433 struct nlattr *bearer;
Ying Xue7d0cb252019-01-14 17:22:26 +0800434 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100435
436 name = (char *)TLV_DATA(msg->req);
437
438 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
439 if (!bearer)
440 return -EMSGSIZE;
441
Xin Longc09cff82019-06-25 00:28:19 +0800442 len = TLV_GET_DATA_LEN(msg->req);
443 if (len <= 0)
444 return -EINVAL;
445
446 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
Ying Xue7d0cb252019-01-14 17:22:26 +0800447 if (!string_is_valid(name, len))
448 return -EINVAL;
449
Richard Alpe9ab15462015-02-09 09:50:05 +0100450 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
451 return -EMSGSIZE;
452
453 nla_nest_end(skb, bearer);
454
455 return 0;
456}
457
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100458static inline u32 perc(u32 count, u32 total)
459{
460 return (count * 100 + (total / 2)) / total;
461}
462
463static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
464 struct nlattr *prop[], struct nlattr *stats[])
465{
466 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
467 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
468
469 tipc_tlv_sprintf(msg->rep,
470 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
471 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
472 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
473 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
474 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
475 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
476
477 tipc_tlv_sprintf(msg->rep,
478 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
479 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
480 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
481 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
482 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
483 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
484
485 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
486 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
487 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
488 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
489
490 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
491 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
492 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
493 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
494
495 tipc_tlv_sprintf(msg->rep,
496 " Congestion link:%u Send queue max:%u avg:%u",
497 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
498 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
499 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
500}
501
502static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
503 struct nlattr **attrs)
504{
505 char *name;
506 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
507 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
508 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800509 int err;
Ying Xue7d0cb252019-01-14 17:22:26 +0800510 int len;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100511
Baozeng Ding297f7d22016-05-24 22:33:24 +0800512 if (!attrs[TIPC_NLA_LINK])
513 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100514
Baozeng Ding297f7d22016-05-24 22:33:24 +0800515 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
516 NULL);
517 if (err)
518 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100519
Baozeng Ding297f7d22016-05-24 22:33:24 +0800520 if (!link[TIPC_NLA_LINK_PROP])
521 return -EINVAL;
522
523 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
524 link[TIPC_NLA_LINK_PROP], NULL);
525 if (err)
526 return err;
527
528 if (!link[TIPC_NLA_LINK_STATS])
529 return -EINVAL;
530
531 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
532 link[TIPC_NLA_LINK_STATS], NULL);
533 if (err)
534 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100535
536 name = (char *)TLV_DATA(msg->req);
Ying Xue7d0cb252019-01-14 17:22:26 +0800537
Xin Longc09cff82019-06-25 00:28:19 +0800538 len = TLV_GET_DATA_LEN(msg->req);
539 if (len <= 0)
540 return -EINVAL;
541
John Rutherford38e88172019-11-26 13:52:55 +1100542 len = min_t(int, len, TIPC_MAX_LINK_NAME);
Ying Xue7d0cb252019-01-14 17:22:26 +0800543 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
650 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
651 NULL);
652 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 Xue7d0cb252019-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
690 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
691 if (!media)
692 return -EMSGSIZE;
693
Ying Xue7d0cb252019-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
701 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
702 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 Xue7d0cb252019-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
722 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
723 if (!bearer)
724 return -EMSGSIZE;
725
Ying Xue7d0cb252019-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
733 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
734 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
753 link = nla_nest_start(skb, TIPC_NLA_LINK);
754 if (!link)
755 return -EMSGSIZE;
756
757 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
758 return -EMSGSIZE;
759
760 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
761 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 Xue3644c532019-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 Long7b7e51f2019-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 Xue3644c532019-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) {
793 cmd->doit = &tipc_nl_media_set;
794 return tipc_nl_compat_media_set(skb, msg);
795 }
796
797 bearer = tipc_bearer_find(msg->net, lc->name);
798 if (bearer) {
799 cmd->doit = &tipc_nl_bearer_set;
800 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 Xue4cd995a2019-01-14 17:22:25 +0800812 int len;
Richard Alpe18178772015-02-09 09:50:09 +0100813
814 name = (char *)TLV_DATA(msg->req);
815
816 link = nla_nest_start(skb, TIPC_NLA_LINK);
817 if (!link)
818 return -EMSGSIZE;
819
Xin Longc09cff82019-06-25 00:28:19 +0800820 len = TLV_GET_DATA_LEN(msg->req);
821 if (len <= 0)
822 return -EINVAL;
823
John Rutherford38e88172019-11-26 13:52:55 +1100824 len = min_t(int, len, TIPC_MAX_LINK_NAME);
Ying Xue4cd995a2019-01-14 17:22:25 +0800825 if (!string_is_valid(name, len))
826 return -EINVAL;
827
Richard Alpe18178772015-02-09 09:50:09 +0100828 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
829 return -EMSGSIZE;
830
831 nla_nest_end(skb, link);
832
833 return 0;
834}
835
Richard Alpe44a8ae92015-02-09 09:50:10 +0100836static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
837{
838 int i;
839 u32 depth;
840 struct tipc_name_table_query *ntq;
841 static const char * const header[] = {
842 "Type ",
843 "Lower Upper ",
844 "Port Identity ",
845 "Publication Scope"
846 };
847
848 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
Ying Xue4c559fb2019-01-14 17:22:28 +0800849 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
850 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100851
852 depth = ntohl(ntq->depth);
853
854 if (depth > 4)
855 depth = 4;
856 for (i = 0; i < depth; i++)
857 tipc_tlv_sprintf(msg->rep, header[i]);
858 tipc_tlv_sprintf(msg->rep, "\n");
859
860 return 0;
861}
862
863static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
864 struct nlattr **attrs)
865{
866 char port_str[27];
867 struct tipc_name_table_query *ntq;
868 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
869 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
870 u32 node, depth, type, lowbound, upbound;
871 static const char * const scope_str[] = {"", " zone", " cluster",
872 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800873 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100874
Baozeng Ding297f7d22016-05-24 22:33:24 +0800875 if (!attrs[TIPC_NLA_NAME_TABLE])
876 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100877
Baozeng Ding297f7d22016-05-24 22:33:24 +0800878 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
879 attrs[TIPC_NLA_NAME_TABLE], NULL);
880 if (err)
881 return err;
882
883 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
884 return -EINVAL;
885
886 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
887 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
888 if (err)
889 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100890
891 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
892
893 depth = ntohl(ntq->depth);
894 type = ntohl(ntq->type);
895 lowbound = ntohl(ntq->lowbound);
896 upbound = ntohl(ntq->upbound);
897
898 if (!(depth & TIPC_NTQ_ALLTYPES) &&
899 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
900 return 0;
901 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
902 return 0;
903 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
904 return 0;
905
906 tipc_tlv_sprintf(msg->rep, "%-10u ",
907 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
908
909 if (depth == 1)
910 goto out;
911
912 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
913 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
914 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
915
916 if (depth == 2)
917 goto out;
918
919 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
920 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
921 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
922 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
923
924 if (depth == 3)
925 goto out;
926
927 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200928 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100929 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
930out:
931 tipc_tlv_sprintf(msg->rep, "\n");
932
933 return 0;
934}
935
Richard Alpe487d2a32015-02-09 09:50:11 +0100936static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
937 struct nlattr **attrs)
938{
939 u32 type, lower, upper;
940 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800941 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100942
Baozeng Ding297f7d22016-05-24 22:33:24 +0800943 if (!attrs[TIPC_NLA_PUBL])
944 return -EINVAL;
945
946 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
947 NULL);
948 if (err)
949 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100950
951 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
952 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
953 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
954
955 if (lower == upper)
956 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
957 else
958 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
959
960 return 0;
961}
962
963static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
964{
965 int err;
966 void *hdr;
967 struct nlattr *nest;
968 struct sk_buff *args;
969 struct tipc_nl_compat_cmd_dump dump;
970
971 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
972 if (!args)
973 return -ENOMEM;
974
975 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
976 TIPC_NL_PUBL_GET);
Gustavo A. R. Silvaec5676b2019-01-05 10:52:23 -0600977 if (!hdr) {
978 kfree_skb(args);
Kangjie Lu10a0e002018-12-26 00:09:04 -0600979 return -EMSGSIZE;
Gustavo A. R. Silvaec5676b2019-01-05 10:52:23 -0600980 }
Richard Alpe487d2a32015-02-09 09:50:11 +0100981
982 nest = nla_nest_start(args, TIPC_NLA_SOCK);
983 if (!nest) {
984 kfree_skb(args);
985 return -EMSGSIZE;
986 }
987
988 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
989 kfree_skb(args);
990 return -EMSGSIZE;
991 }
992
993 nla_nest_end(args, nest);
994 genlmsg_end(args, hdr);
995
996 dump.dumpit = tipc_nl_publ_dump;
997 dump.format = __tipc_nl_compat_publ_dump;
998
999 err = __tipc_nl_compat_dumpit(&dump, msg, args);
1000
1001 kfree_skb(args);
1002
1003 return err;
1004}
1005
1006static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
1007 struct nlattr **attrs)
1008{
1009 int err;
1010 u32 sock_ref;
1011 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
1012
Baozeng Ding297f7d22016-05-24 22:33:24 +08001013 if (!attrs[TIPC_NLA_SOCK])
1014 return -EINVAL;
1015
1016 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
1017 NULL);
1018 if (err)
1019 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +01001020
1021 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1022 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1023
1024 if (sock[TIPC_NLA_SOCK_CON]) {
1025 u32 node;
1026 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1027
1028 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
1029 NULL);
1030
1031 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1032 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1033 tipc_zone(node),
1034 tipc_cluster(node),
1035 tipc_node(node),
1036 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1037
1038 if (con[TIPC_NLA_CON_FLAG])
1039 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1040 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1041 nla_get_u32(con[TIPC_NLA_CON_INST]));
1042 else
1043 tipc_tlv_sprintf(msg->rep, "\n");
1044 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1045 tipc_tlv_sprintf(msg->rep, " bound to");
1046
1047 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1048 if (err)
1049 return err;
1050 }
1051 tipc_tlv_sprintf(msg->rep, "\n");
1052
1053 return 0;
1054}
1055
Richard Alpe5bfc3352015-02-09 09:50:12 +01001056static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1057 struct nlattr **attrs)
1058{
1059 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001060 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001061
Baozeng Ding297f7d22016-05-24 22:33:24 +08001062 if (!attrs[TIPC_NLA_MEDIA])
1063 return -EINVAL;
1064
1065 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
1066 NULL);
1067 if (err)
1068 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001069
1070 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1071 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1072 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1073}
1074
Richard Alpe4b28cb52015-02-09 09:50:13 +01001075static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1076 struct nlattr **attrs)
1077{
1078 struct tipc_node_info node_info;
1079 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001080 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001081
Baozeng Ding297f7d22016-05-24 22:33:24 +08001082 if (!attrs[TIPC_NLA_NODE])
1083 return -EINVAL;
1084
1085 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1086 NULL);
1087 if (err)
1088 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001089
1090 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1091 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1092
1093 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1094 sizeof(node_info));
1095}
1096
Richard Alpec3d6fb82015-05-06 13:58:54 +02001097static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1098 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001099 struct tipc_nl_compat_msg *msg)
1100{
1101 u32 val;
1102 struct nlattr *net;
1103
1104 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1105
1106 net = nla_nest_start(skb, TIPC_NLA_NET);
1107 if (!net)
1108 return -EMSGSIZE;
1109
Richard Alpe964f9502015-02-09 09:50:15 +01001110 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1111 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1112 return -EMSGSIZE;
1113 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1114 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1115 return -EMSGSIZE;
1116 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001117 nla_nest_end(skb, net);
1118
1119 return 0;
1120}
1121
Richard Alpe3c261812015-02-09 09:50:16 +01001122static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1123 struct nlattr **attrs)
1124{
1125 __be32 id;
1126 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001127 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001128
Baozeng Ding297f7d22016-05-24 22:33:24 +08001129 if (!attrs[TIPC_NLA_NET])
1130 return -EINVAL;
1131
1132 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1133 NULL);
1134 if (err)
1135 return err;
1136
Richard Alpe3c261812015-02-09 09:50:16 +01001137 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1138
1139 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1140}
1141
Richard Alpe5a81a632015-02-09 09:50:17 +01001142static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1143{
1144 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1145 if (!msg->rep)
1146 return -ENOMEM;
1147
1148 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1149 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1150
1151 return 0;
1152}
1153
Richard Alped0796d12015-02-09 09:50:04 +01001154static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1155{
1156 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001157 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001158
1159 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001160 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001161
1162 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001163 case TIPC_CMD_NOOP:
1164 msg->rep = tipc_tlv_alloc(0);
1165 if (!msg->rep)
1166 return -ENOMEM;
1167 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001168 case TIPC_CMD_GET_BEARER_NAMES:
1169 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1170 dump.dumpit = tipc_nl_bearer_dump;
1171 dump.format = tipc_nl_compat_bearer_dump;
1172 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001173 case TIPC_CMD_ENABLE_BEARER:
1174 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1175 doit.doit = tipc_nl_bearer_enable;
1176 doit.transcode = tipc_nl_compat_bearer_enable;
1177 return tipc_nl_compat_doit(&doit, msg);
1178 case TIPC_CMD_DISABLE_BEARER:
1179 msg->req_type = TIPC_TLV_BEARER_NAME;
1180 doit.doit = tipc_nl_bearer_disable;
1181 doit.transcode = tipc_nl_compat_bearer_disable;
1182 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001183 case TIPC_CMD_SHOW_LINK_STATS:
1184 msg->req_type = TIPC_TLV_LINK_NAME;
1185 msg->rep_size = ULTRA_STRING_MAX_LEN;
1186 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001187 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001188 dump.format = tipc_nl_compat_link_stat_dump;
1189 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001190 case TIPC_CMD_GET_LINKS:
1191 msg->req_type = TIPC_TLV_NET_ADDR;
1192 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001193 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001194 dump.format = tipc_nl_compat_link_dump;
1195 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001196 case TIPC_CMD_SET_LINK_TOL:
1197 case TIPC_CMD_SET_LINK_PRI:
1198 case TIPC_CMD_SET_LINK_WINDOW:
1199 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001200 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001201 doit.transcode = tipc_nl_compat_link_set;
1202 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001203 case TIPC_CMD_RESET_LINK_STATS:
1204 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001205 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001206 doit.transcode = tipc_nl_compat_link_reset_stats;
1207 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001208 case TIPC_CMD_SHOW_NAME_TABLE:
1209 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1210 msg->rep_size = ULTRA_STRING_MAX_LEN;
1211 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1212 dump.header = tipc_nl_compat_name_table_dump_header;
1213 dump.dumpit = tipc_nl_name_table_dump;
1214 dump.format = tipc_nl_compat_name_table_dump;
1215 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001216 case TIPC_CMD_SHOW_PORTS:
1217 msg->rep_size = ULTRA_STRING_MAX_LEN;
1218 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1219 dump.dumpit = tipc_nl_sk_dump;
1220 dump.format = tipc_nl_compat_sk_dump;
1221 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001222 case TIPC_CMD_GET_MEDIA_NAMES:
1223 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1224 dump.dumpit = tipc_nl_media_dump;
1225 dump.format = tipc_nl_compat_media_dump;
1226 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001227 case TIPC_CMD_GET_NODES:
1228 msg->rep_size = ULTRA_STRING_MAX_LEN;
1229 dump.dumpit = tipc_nl_node_dump;
1230 dump.format = tipc_nl_compat_node_dump;
1231 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001232 case TIPC_CMD_SET_NODE_ADDR:
1233 msg->req_type = TIPC_TLV_NET_ADDR;
1234 doit.doit = tipc_nl_net_set;
1235 doit.transcode = tipc_nl_compat_net_set;
1236 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001237 case TIPC_CMD_SET_NETID:
1238 msg->req_type = TIPC_TLV_UNSIGNED;
1239 doit.doit = tipc_nl_net_set;
1240 doit.transcode = tipc_nl_compat_net_set;
1241 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001242 case TIPC_CMD_GET_NETID:
1243 msg->rep_size = sizeof(u32);
1244 dump.dumpit = tipc_nl_net_dump;
1245 dump.format = tipc_nl_compat_net_dump;
1246 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001247 case TIPC_CMD_SHOW_STATS:
1248 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001249 }
1250
1251 return -EOPNOTSUPP;
1252}
1253
1254static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1255{
1256 int err;
1257 int len;
1258 struct tipc_nl_compat_msg msg;
1259 struct nlmsghdr *req_nlh;
1260 struct nlmsghdr *rep_nlh;
1261 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001262
1263 memset(&msg, 0, sizeof(msg));
1264
1265 req_nlh = (struct nlmsghdr *)skb->data;
1266 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1267 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001268 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001269 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001270
1271 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1272 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1273 err = -EACCES;
1274 goto send;
1275 }
1276
Taras Kondratiuk695074c2019-07-29 22:15:07 +00001277 msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1278 if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) {
Richard Alped0796d12015-02-09 09:50:04 +01001279 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1280 err = -EOPNOTSUPP;
1281 goto send;
1282 }
1283
1284 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001285 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001286 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1287 else if (err == -EINVAL)
1288 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1289send:
1290 if (!msg.rep)
1291 return err;
1292
1293 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1294 skb_push(msg.rep, len);
1295 rep_nlh = nlmsg_hdr(msg.rep);
1296 memcpy(rep_nlh, info->nlhdr, len);
1297 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001298 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001299
1300 return err;
1301}
1302
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001303static struct genl_family tipc_genl_compat_family = {
1304 .id = GENL_ID_GENERATE,
1305 .name = TIPC_GENL_NAME,
1306 .version = TIPC_GENL_VERSION,
1307 .hdrsize = TIPC_GENL_HDRLEN,
1308 .maxattr = 0,
1309 .netnsok = true,
1310};
1311
1312static struct genl_ops tipc_genl_compat_ops[] = {
1313 {
1314 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001315 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001316 },
1317};
1318
1319int tipc_netlink_compat_start(void)
1320{
1321 int res;
1322
1323 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1324 tipc_genl_compat_ops);
1325 if (res) {
1326 pr_err("Failed to register legacy compat interface\n");
1327 return res;
1328 }
1329
1330 return 0;
1331}
1332
1333void tipc_netlink_compat_stop(void)
1334{
1335 genl_unregister_family(&tipc_genl_compat_family);
1336}