blob: d947b8210399e187ba1c9297d552d7deac66eaba [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 Xue4cd995a2019-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 Xue4cd995a2019-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;
198
199 do {
200 int rem;
201
202 len = (*cmd->dumpit)(buf, &cb);
203
204 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
205 struct nlattr **attrs;
206
207 err = tipc_nlmsg_parse(nlmsg, &attrs);
208 if (err)
209 goto err_out;
210
211 err = (*cmd->format)(msg, attrs);
212 if (err)
213 goto err_out;
214
215 if (tipc_skb_tailroom(msg->rep) <= 1) {
216 err = -EMSGSIZE;
217 goto err_out;
218 }
219 }
220
221 skb_reset_tail_pointer(buf);
222 buf->len = 0;
223
224 } while (len);
225
226 err = 0;
227
228err_out:
229 kfree_skb(buf);
230
231 if (err == -EMSGSIZE) {
232 /* The legacy API only considered messages filling
233 * "ULTRA_STRING_MAX_LEN" to be truncated.
234 */
235 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
236 char *tail = skb_tail_pointer(msg->rep);
237
238 if (*tail != '\0')
239 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
240 REPLY_TRUNCATED);
241 }
242
243 return 0;
244 }
245
246 return err;
247}
248
249static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
250 struct tipc_nl_compat_msg *msg)
251{
252 int err;
253 struct sk_buff *arg;
254
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100255 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
256 return -EINVAL;
257
Richard Alped0796d12015-02-09 09:50:04 +0100258 msg->rep = tipc_tlv_alloc(msg->rep_size);
259 if (!msg->rep)
260 return -ENOMEM;
261
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100262 if (msg->rep_type)
263 tipc_tlv_init(msg->rep, msg->rep_type);
264
Richard Alpe44a8ae92015-02-09 09:50:10 +0100265 if (cmd->header)
266 (*cmd->header)(msg);
267
Richard Alped0796d12015-02-09 09:50:04 +0100268 arg = nlmsg_new(0, GFP_KERNEL);
269 if (!arg) {
270 kfree_skb(msg->rep);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700271 msg->rep = NULL;
Richard Alped0796d12015-02-09 09:50:04 +0100272 return -ENOMEM;
273 }
274
275 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700276 if (err) {
Richard Alped0796d12015-02-09 09:50:04 +0100277 kfree_skb(msg->rep);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700278 msg->rep = NULL;
279 }
Richard Alped0796d12015-02-09 09:50:04 +0100280 kfree_skb(arg);
281
282 return err;
283}
284
Richard Alpe9ab15462015-02-09 09:50:05 +0100285static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
286 struct tipc_nl_compat_msg *msg)
287{
288 int err;
289 struct sk_buff *doit_buf;
290 struct sk_buff *trans_buf;
291 struct nlattr **attrbuf;
292 struct genl_info info;
293
294 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
295 if (!trans_buf)
296 return -ENOMEM;
297
Richard Alpec3d6fb82015-05-06 13:58:54 +0200298 err = (*cmd->transcode)(cmd, trans_buf, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100299 if (err)
300 goto trans_out;
301
302 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
303 sizeof(struct nlattr *), GFP_KERNEL);
304 if (!attrbuf) {
305 err = -ENOMEM;
306 goto trans_out;
307 }
308
309 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
310 (const struct nlattr *)trans_buf->data,
311 trans_buf->len, NULL);
312 if (err)
313 goto parse_out;
314
315 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
316 if (!doit_buf) {
317 err = -ENOMEM;
318 goto parse_out;
319 }
320
321 doit_buf->sk = msg->dst_sk;
322
323 memset(&info, 0, sizeof(info));
324 info.attrs = attrbuf;
325
326 err = (*cmd->doit)(doit_buf, &info);
327
328 kfree_skb(doit_buf);
329parse_out:
330 kfree(attrbuf);
331trans_out:
332 kfree_skb(trans_buf);
333
334 return err;
335}
336
337static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
338 struct tipc_nl_compat_msg *msg)
339{
340 int err;
341
342 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
343 return -EINVAL;
344
345 err = __tipc_nl_compat_doit(cmd, msg);
346 if (err)
347 return err;
348
349 /* The legacy API considered an empty message a success message */
350 msg->rep = tipc_tlv_alloc(0);
351 if (!msg->rep)
352 return -ENOMEM;
353
354 return 0;
355}
356
Richard Alped0796d12015-02-09 09:50:04 +0100357static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
358 struct nlattr **attrs)
359{
360 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800361 int err;
Richard Alped0796d12015-02-09 09:50:04 +0100362
Baozeng Ding297f7d22016-05-24 22:33:24 +0800363 if (!attrs[TIPC_NLA_BEARER])
364 return -EINVAL;
365
366 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
367 attrs[TIPC_NLA_BEARER], NULL);
368 if (err)
369 return err;
Richard Alped0796d12015-02-09 09:50:04 +0100370
371 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
372 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
373 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
374}
375
Richard Alpec3d6fb82015-05-06 13:58:54 +0200376static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
377 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100378 struct tipc_nl_compat_msg *msg)
379{
380 struct nlattr *prop;
381 struct nlattr *bearer;
382 struct tipc_bearer_config *b;
Ying Xue7d0cb252019-01-14 17:22:26 +0800383 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100384
385 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
386
387 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
388 if (!bearer)
389 return -EMSGSIZE;
390
Ying Xue7d0cb252019-01-14 17:22:26 +0800391 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
392 if (!string_is_valid(b->name, len))
393 return -EINVAL;
394
Richard Alpe9ab15462015-02-09 09:50:05 +0100395 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
396 return -EMSGSIZE;
397
398 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
399 return -EMSGSIZE;
400
401 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
402 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
403 if (!prop)
404 return -EMSGSIZE;
405 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
406 return -EMSGSIZE;
407 nla_nest_end(skb, prop);
408 }
409 nla_nest_end(skb, bearer);
410
411 return 0;
412}
413
Richard Alpec3d6fb82015-05-06 13:58:54 +0200414static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
415 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100416 struct tipc_nl_compat_msg *msg)
417{
418 char *name;
419 struct nlattr *bearer;
Ying Xue7d0cb252019-01-14 17:22:26 +0800420 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100421
422 name = (char *)TLV_DATA(msg->req);
423
424 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
425 if (!bearer)
426 return -EMSGSIZE;
427
Ying Xue7d0cb252019-01-14 17:22:26 +0800428 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
429 if (!string_is_valid(name, len))
430 return -EINVAL;
431
Richard Alpe9ab15462015-02-09 09:50:05 +0100432 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
433 return -EMSGSIZE;
434
435 nla_nest_end(skb, bearer);
436
437 return 0;
438}
439
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100440static inline u32 perc(u32 count, u32 total)
441{
442 return (count * 100 + (total / 2)) / total;
443}
444
445static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
446 struct nlattr *prop[], struct nlattr *stats[])
447{
448 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
449 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
450
451 tipc_tlv_sprintf(msg->rep,
452 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
453 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
454 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
455 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
456 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
457 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
458
459 tipc_tlv_sprintf(msg->rep,
460 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
461 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
462 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
463 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
464 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
465 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
466
467 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
468 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
469 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
470 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
471
472 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
473 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
474 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
475 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
476
477 tipc_tlv_sprintf(msg->rep,
478 " Congestion link:%u Send queue max:%u avg:%u",
479 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
480 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
481 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
482}
483
484static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
485 struct nlattr **attrs)
486{
487 char *name;
488 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
489 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
490 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800491 int err;
Ying Xue7d0cb252019-01-14 17:22:26 +0800492 int len;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100493
Baozeng Ding297f7d22016-05-24 22:33:24 +0800494 if (!attrs[TIPC_NLA_LINK])
495 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100496
Baozeng Ding297f7d22016-05-24 22:33:24 +0800497 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
498 NULL);
499 if (err)
500 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100501
Baozeng Ding297f7d22016-05-24 22:33:24 +0800502 if (!link[TIPC_NLA_LINK_PROP])
503 return -EINVAL;
504
505 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
506 link[TIPC_NLA_LINK_PROP], NULL);
507 if (err)
508 return err;
509
510 if (!link[TIPC_NLA_LINK_STATS])
511 return -EINVAL;
512
513 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
514 link[TIPC_NLA_LINK_STATS], NULL);
515 if (err)
516 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100517
518 name = (char *)TLV_DATA(msg->req);
Ying Xue7d0cb252019-01-14 17:22:26 +0800519
520 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
521 if (!string_is_valid(name, len))
522 return -EINVAL;
523
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100524 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
525 return 0;
526
527 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
528 nla_data(link[TIPC_NLA_LINK_NAME]));
529
530 if (link[TIPC_NLA_LINK_BROADCAST]) {
531 __fill_bc_link_stat(msg, prop, stats);
532 return 0;
533 }
534
535 if (link[TIPC_NLA_LINK_ACTIVE])
536 tipc_tlv_sprintf(msg->rep, " ACTIVE");
537 else if (link[TIPC_NLA_LINK_UP])
538 tipc_tlv_sprintf(msg->rep, " STANDBY");
539 else
540 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
541
542 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
543 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
544 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
545
546 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
547 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
548 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
549
550 tipc_tlv_sprintf(msg->rep,
551 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
552 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
553 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
554 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
555 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
556 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
557 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
558
559 tipc_tlv_sprintf(msg->rep,
560 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
561 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
562 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
563 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
564 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
565 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
566 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
567
568 tipc_tlv_sprintf(msg->rep,
569 " TX profile sample:%u packets average:%u octets\n",
570 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
571 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
572 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
573
574 tipc_tlv_sprintf(msg->rep,
575 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
576 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
577 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
578 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
579 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
580 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
581 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
582 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
583 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
584
585 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
586 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
587 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
588 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
589 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
590 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
591 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
592
593 tipc_tlv_sprintf(msg->rep,
594 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
595 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
596 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
597 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
598 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
599 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
600
601 tipc_tlv_sprintf(msg->rep,
602 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
603 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
604 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
605 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
606 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
607 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
608
609 tipc_tlv_sprintf(msg->rep,
610 " Congestion link:%u Send queue max:%u avg:%u",
611 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
612 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
613 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
614
615 return 0;
616}
617
Richard Alpe357ebdb2015-02-09 09:50:07 +0100618static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
619 struct nlattr **attrs)
620{
621 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
622 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800623 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100624
Baozeng Ding297f7d22016-05-24 22:33:24 +0800625 if (!attrs[TIPC_NLA_LINK])
626 return -EINVAL;
627
628 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
629 NULL);
630 if (err)
631 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100632
633 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
634 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200635 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400636 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100637
638 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
639 &link_info, sizeof(link_info));
640}
641
Richard Alpec3d6fb82015-05-06 13:58:54 +0200642static int __tipc_add_link_prop(struct sk_buff *skb,
643 struct tipc_nl_compat_msg *msg,
644 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100645{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200646 switch (msg->cmd) {
647 case TIPC_CMD_SET_LINK_PRI:
648 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
649 case TIPC_CMD_SET_LINK_TOL:
650 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
651 case TIPC_CMD_SET_LINK_WINDOW:
652 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
653 }
654
655 return -EINVAL;
656}
657
658static int tipc_nl_compat_media_set(struct sk_buff *skb,
659 struct tipc_nl_compat_msg *msg)
660{
Richard Alpe37e2d482015-02-09 09:50:08 +0100661 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200662 struct nlattr *media;
663 struct tipc_link_config *lc;
Ying Xue7d0cb252019-01-14 17:22:26 +0800664 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200665
666 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
667
668 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
669 if (!media)
670 return -EMSGSIZE;
671
Ying Xue7d0cb252019-01-14 17:22:26 +0800672 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
673 if (!string_is_valid(lc->name, len))
674 return -EINVAL;
675
Richard Alpec3d6fb82015-05-06 13:58:54 +0200676 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
677 return -EMSGSIZE;
678
679 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
680 if (!prop)
681 return -EMSGSIZE;
682
683 __tipc_add_link_prop(skb, msg, lc);
684 nla_nest_end(skb, prop);
685 nla_nest_end(skb, media);
686
687 return 0;
688}
689
690static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
691 struct tipc_nl_compat_msg *msg)
692{
693 struct nlattr *prop;
694 struct nlattr *bearer;
695 struct tipc_link_config *lc;
Ying Xue7d0cb252019-01-14 17:22:26 +0800696 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200697
698 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
699
700 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
701 if (!bearer)
702 return -EMSGSIZE;
703
Ying Xue7d0cb252019-01-14 17:22:26 +0800704 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
705 if (!string_is_valid(lc->name, len))
706 return -EINVAL;
707
Richard Alpec3d6fb82015-05-06 13:58:54 +0200708 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
709 return -EMSGSIZE;
710
711 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
712 if (!prop)
713 return -EMSGSIZE;
714
715 __tipc_add_link_prop(skb, msg, lc);
716 nla_nest_end(skb, prop);
717 nla_nest_end(skb, bearer);
718
719 return 0;
720}
721
722static int __tipc_nl_compat_link_set(struct sk_buff *skb,
723 struct tipc_nl_compat_msg *msg)
724{
725 struct nlattr *prop;
726 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100727 struct tipc_link_config *lc;
728
729 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
730
731 link = nla_nest_start(skb, TIPC_NLA_LINK);
732 if (!link)
733 return -EMSGSIZE;
734
735 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
736 return -EMSGSIZE;
737
738 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
739 if (!prop)
740 return -EMSGSIZE;
741
Richard Alpec3d6fb82015-05-06 13:58:54 +0200742 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100743 nla_nest_end(skb, prop);
744 nla_nest_end(skb, link);
745
746 return 0;
747}
748
Richard Alpec3d6fb82015-05-06 13:58:54 +0200749static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
750 struct sk_buff *skb,
751 struct tipc_nl_compat_msg *msg)
752{
753 struct tipc_link_config *lc;
754 struct tipc_bearer *bearer;
755 struct tipc_media *media;
Ying Xue3644c532019-01-14 17:22:27 +0800756 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200757
758 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
759
Ying Xue3644c532019-01-14 17:22:27 +0800760 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
761 if (!string_is_valid(lc->name, len))
762 return -EINVAL;
763
Richard Alpec3d6fb82015-05-06 13:58:54 +0200764 media = tipc_media_find(lc->name);
765 if (media) {
766 cmd->doit = &tipc_nl_media_set;
767 return tipc_nl_compat_media_set(skb, msg);
768 }
769
770 bearer = tipc_bearer_find(msg->net, lc->name);
771 if (bearer) {
772 cmd->doit = &tipc_nl_bearer_set;
773 return tipc_nl_compat_bearer_set(skb, msg);
774 }
775
776 return __tipc_nl_compat_link_set(skb, msg);
777}
778
779static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
780 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100781 struct tipc_nl_compat_msg *msg)
782{
783 char *name;
784 struct nlattr *link;
Ying Xue4cd995a2019-01-14 17:22:25 +0800785 int len;
Richard Alpe18178772015-02-09 09:50:09 +0100786
787 name = (char *)TLV_DATA(msg->req);
788
789 link = nla_nest_start(skb, TIPC_NLA_LINK);
790 if (!link)
791 return -EMSGSIZE;
792
Ying Xue4cd995a2019-01-14 17:22:25 +0800793 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
794 if (!string_is_valid(name, len))
795 return -EINVAL;
796
Richard Alpe18178772015-02-09 09:50:09 +0100797 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
798 return -EMSGSIZE;
799
800 nla_nest_end(skb, link);
801
802 return 0;
803}
804
Richard Alpe44a8ae92015-02-09 09:50:10 +0100805static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
806{
807 int i;
808 u32 depth;
809 struct tipc_name_table_query *ntq;
810 static const char * const header[] = {
811 "Type ",
812 "Lower Upper ",
813 "Port Identity ",
814 "Publication Scope"
815 };
816
817 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
Ying Xue4c559fb2019-01-14 17:22:28 +0800818 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
819 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100820
821 depth = ntohl(ntq->depth);
822
823 if (depth > 4)
824 depth = 4;
825 for (i = 0; i < depth; i++)
826 tipc_tlv_sprintf(msg->rep, header[i]);
827 tipc_tlv_sprintf(msg->rep, "\n");
828
829 return 0;
830}
831
832static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
833 struct nlattr **attrs)
834{
835 char port_str[27];
836 struct tipc_name_table_query *ntq;
837 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
838 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
839 u32 node, depth, type, lowbound, upbound;
840 static const char * const scope_str[] = {"", " zone", " cluster",
841 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800842 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100843
Baozeng Ding297f7d22016-05-24 22:33:24 +0800844 if (!attrs[TIPC_NLA_NAME_TABLE])
845 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100846
Baozeng Ding297f7d22016-05-24 22:33:24 +0800847 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
848 attrs[TIPC_NLA_NAME_TABLE], NULL);
849 if (err)
850 return err;
851
852 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
853 return -EINVAL;
854
855 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
856 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
857 if (err)
858 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100859
860 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
861
862 depth = ntohl(ntq->depth);
863 type = ntohl(ntq->type);
864 lowbound = ntohl(ntq->lowbound);
865 upbound = ntohl(ntq->upbound);
866
867 if (!(depth & TIPC_NTQ_ALLTYPES) &&
868 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
869 return 0;
870 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
871 return 0;
872 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
873 return 0;
874
875 tipc_tlv_sprintf(msg->rep, "%-10u ",
876 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
877
878 if (depth == 1)
879 goto out;
880
881 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
882 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
883 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
884
885 if (depth == 2)
886 goto out;
887
888 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
889 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
890 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
891 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
892
893 if (depth == 3)
894 goto out;
895
896 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200897 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100898 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
899out:
900 tipc_tlv_sprintf(msg->rep, "\n");
901
902 return 0;
903}
904
Richard Alpe487d2a32015-02-09 09:50:11 +0100905static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
906 struct nlattr **attrs)
907{
908 u32 type, lower, upper;
909 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800910 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100911
Baozeng Ding297f7d22016-05-24 22:33:24 +0800912 if (!attrs[TIPC_NLA_PUBL])
913 return -EINVAL;
914
915 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
916 NULL);
917 if (err)
918 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100919
920 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
921 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
922 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
923
924 if (lower == upper)
925 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
926 else
927 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
928
929 return 0;
930}
931
932static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
933{
934 int err;
935 void *hdr;
936 struct nlattr *nest;
937 struct sk_buff *args;
938 struct tipc_nl_compat_cmd_dump dump;
939
940 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
941 if (!args)
942 return -ENOMEM;
943
944 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
945 TIPC_NL_PUBL_GET);
946
947 nest = nla_nest_start(args, TIPC_NLA_SOCK);
948 if (!nest) {
949 kfree_skb(args);
950 return -EMSGSIZE;
951 }
952
953 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
954 kfree_skb(args);
955 return -EMSGSIZE;
956 }
957
958 nla_nest_end(args, nest);
959 genlmsg_end(args, hdr);
960
961 dump.dumpit = tipc_nl_publ_dump;
962 dump.format = __tipc_nl_compat_publ_dump;
963
964 err = __tipc_nl_compat_dumpit(&dump, msg, args);
965
966 kfree_skb(args);
967
968 return err;
969}
970
971static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
972 struct nlattr **attrs)
973{
974 int err;
975 u32 sock_ref;
976 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
977
Baozeng Ding297f7d22016-05-24 22:33:24 +0800978 if (!attrs[TIPC_NLA_SOCK])
979 return -EINVAL;
980
981 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
982 NULL);
983 if (err)
984 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100985
986 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
987 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
988
989 if (sock[TIPC_NLA_SOCK_CON]) {
990 u32 node;
991 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
992
993 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
994 NULL);
995
996 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
997 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
998 tipc_zone(node),
999 tipc_cluster(node),
1000 tipc_node(node),
1001 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1002
1003 if (con[TIPC_NLA_CON_FLAG])
1004 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1005 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1006 nla_get_u32(con[TIPC_NLA_CON_INST]));
1007 else
1008 tipc_tlv_sprintf(msg->rep, "\n");
1009 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1010 tipc_tlv_sprintf(msg->rep, " bound to");
1011
1012 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1013 if (err)
1014 return err;
1015 }
1016 tipc_tlv_sprintf(msg->rep, "\n");
1017
1018 return 0;
1019}
1020
Richard Alpe5bfc3352015-02-09 09:50:12 +01001021static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1022 struct nlattr **attrs)
1023{
1024 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001025 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001026
Baozeng Ding297f7d22016-05-24 22:33:24 +08001027 if (!attrs[TIPC_NLA_MEDIA])
1028 return -EINVAL;
1029
1030 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
1031 NULL);
1032 if (err)
1033 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001034
1035 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1036 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1037 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1038}
1039
Richard Alpe4b28cb52015-02-09 09:50:13 +01001040static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1041 struct nlattr **attrs)
1042{
1043 struct tipc_node_info node_info;
1044 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001045 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001046
Baozeng Ding297f7d22016-05-24 22:33:24 +08001047 if (!attrs[TIPC_NLA_NODE])
1048 return -EINVAL;
1049
1050 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1051 NULL);
1052 if (err)
1053 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001054
1055 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1056 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1057
1058 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1059 sizeof(node_info));
1060}
1061
Richard Alpec3d6fb82015-05-06 13:58:54 +02001062static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1063 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001064 struct tipc_nl_compat_msg *msg)
1065{
1066 u32 val;
1067 struct nlattr *net;
1068
1069 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1070
1071 net = nla_nest_start(skb, TIPC_NLA_NET);
1072 if (!net)
1073 return -EMSGSIZE;
1074
Richard Alpe964f9502015-02-09 09:50:15 +01001075 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1076 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1077 return -EMSGSIZE;
1078 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1079 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1080 return -EMSGSIZE;
1081 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001082 nla_nest_end(skb, net);
1083
1084 return 0;
1085}
1086
Richard Alpe3c261812015-02-09 09:50:16 +01001087static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1088 struct nlattr **attrs)
1089{
1090 __be32 id;
1091 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001092 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001093
Baozeng Ding297f7d22016-05-24 22:33:24 +08001094 if (!attrs[TIPC_NLA_NET])
1095 return -EINVAL;
1096
1097 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1098 NULL);
1099 if (err)
1100 return err;
1101
Richard Alpe3c261812015-02-09 09:50:16 +01001102 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1103
1104 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1105}
1106
Richard Alpe5a81a632015-02-09 09:50:17 +01001107static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1108{
1109 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1110 if (!msg->rep)
1111 return -ENOMEM;
1112
1113 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1114 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1115
1116 return 0;
1117}
1118
Richard Alped0796d12015-02-09 09:50:04 +01001119static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1120{
1121 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001122 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001123
1124 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001125 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001126
1127 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001128 case TIPC_CMD_NOOP:
1129 msg->rep = tipc_tlv_alloc(0);
1130 if (!msg->rep)
1131 return -ENOMEM;
1132 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001133 case TIPC_CMD_GET_BEARER_NAMES:
1134 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1135 dump.dumpit = tipc_nl_bearer_dump;
1136 dump.format = tipc_nl_compat_bearer_dump;
1137 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001138 case TIPC_CMD_ENABLE_BEARER:
1139 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1140 doit.doit = tipc_nl_bearer_enable;
1141 doit.transcode = tipc_nl_compat_bearer_enable;
1142 return tipc_nl_compat_doit(&doit, msg);
1143 case TIPC_CMD_DISABLE_BEARER:
1144 msg->req_type = TIPC_TLV_BEARER_NAME;
1145 doit.doit = tipc_nl_bearer_disable;
1146 doit.transcode = tipc_nl_compat_bearer_disable;
1147 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001148 case TIPC_CMD_SHOW_LINK_STATS:
1149 msg->req_type = TIPC_TLV_LINK_NAME;
1150 msg->rep_size = ULTRA_STRING_MAX_LEN;
1151 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001152 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001153 dump.format = tipc_nl_compat_link_stat_dump;
1154 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001155 case TIPC_CMD_GET_LINKS:
1156 msg->req_type = TIPC_TLV_NET_ADDR;
1157 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001158 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001159 dump.format = tipc_nl_compat_link_dump;
1160 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001161 case TIPC_CMD_SET_LINK_TOL:
1162 case TIPC_CMD_SET_LINK_PRI:
1163 case TIPC_CMD_SET_LINK_WINDOW:
1164 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001165 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001166 doit.transcode = tipc_nl_compat_link_set;
1167 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001168 case TIPC_CMD_RESET_LINK_STATS:
1169 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001170 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001171 doit.transcode = tipc_nl_compat_link_reset_stats;
1172 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001173 case TIPC_CMD_SHOW_NAME_TABLE:
1174 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1175 msg->rep_size = ULTRA_STRING_MAX_LEN;
1176 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1177 dump.header = tipc_nl_compat_name_table_dump_header;
1178 dump.dumpit = tipc_nl_name_table_dump;
1179 dump.format = tipc_nl_compat_name_table_dump;
1180 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001181 case TIPC_CMD_SHOW_PORTS:
1182 msg->rep_size = ULTRA_STRING_MAX_LEN;
1183 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1184 dump.dumpit = tipc_nl_sk_dump;
1185 dump.format = tipc_nl_compat_sk_dump;
1186 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001187 case TIPC_CMD_GET_MEDIA_NAMES:
1188 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1189 dump.dumpit = tipc_nl_media_dump;
1190 dump.format = tipc_nl_compat_media_dump;
1191 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001192 case TIPC_CMD_GET_NODES:
1193 msg->rep_size = ULTRA_STRING_MAX_LEN;
1194 dump.dumpit = tipc_nl_node_dump;
1195 dump.format = tipc_nl_compat_node_dump;
1196 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001197 case TIPC_CMD_SET_NODE_ADDR:
1198 msg->req_type = TIPC_TLV_NET_ADDR;
1199 doit.doit = tipc_nl_net_set;
1200 doit.transcode = tipc_nl_compat_net_set;
1201 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001202 case TIPC_CMD_SET_NETID:
1203 msg->req_type = TIPC_TLV_UNSIGNED;
1204 doit.doit = tipc_nl_net_set;
1205 doit.transcode = tipc_nl_compat_net_set;
1206 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001207 case TIPC_CMD_GET_NETID:
1208 msg->rep_size = sizeof(u32);
1209 dump.dumpit = tipc_nl_net_dump;
1210 dump.format = tipc_nl_compat_net_dump;
1211 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001212 case TIPC_CMD_SHOW_STATS:
1213 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001214 }
1215
1216 return -EOPNOTSUPP;
1217}
1218
1219static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1220{
1221 int err;
1222 int len;
1223 struct tipc_nl_compat_msg msg;
1224 struct nlmsghdr *req_nlh;
1225 struct nlmsghdr *rep_nlh;
1226 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001227
1228 memset(&msg, 0, sizeof(msg));
1229
1230 req_nlh = (struct nlmsghdr *)skb->data;
1231 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1232 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001233 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001234 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001235
1236 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1237 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1238 err = -EACCES;
1239 goto send;
1240 }
1241
1242 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Ying Xue02862eb2019-01-14 17:22:29 +08001243 if (!len || !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001244 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1245 err = -EOPNOTSUPP;
1246 goto send;
1247 }
1248
1249 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001250 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001251 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1252 else if (err == -EINVAL)
1253 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1254send:
1255 if (!msg.rep)
1256 return err;
1257
1258 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1259 skb_push(msg.rep, len);
1260 rep_nlh = nlmsg_hdr(msg.rep);
1261 memcpy(rep_nlh, info->nlhdr, len);
1262 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001263 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001264
1265 return err;
1266}
1267
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001268static struct genl_family tipc_genl_compat_family = {
1269 .id = GENL_ID_GENERATE,
1270 .name = TIPC_GENL_NAME,
1271 .version = TIPC_GENL_VERSION,
1272 .hdrsize = TIPC_GENL_HDRLEN,
1273 .maxattr = 0,
1274 .netnsok = true,
1275};
1276
1277static struct genl_ops tipc_genl_compat_ops[] = {
1278 {
1279 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001280 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001281 },
1282};
1283
1284int tipc_netlink_compat_start(void)
1285{
1286 int res;
1287
1288 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1289 tipc_genl_compat_ops);
1290 if (res) {
1291 pr_err("Failed to register legacy compat interface\n");
1292 return res;
1293 }
1294
1295 return 0;
1296}
1297
1298void tipc_netlink_compat_stop(void)
1299{
1300 genl_unregister_family(&tipc_genl_compat_family);
1301}