blob: 0cf9403b4c44b151978b58c1a2db5b74d6d5330a [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
Xin Longd2618e32019-03-31 22:50:10 +0800265 if (cmd->header) {
266 err = (*cmd->header)(msg);
267 if (err) {
268 kfree_skb(msg->rep);
269 msg->rep = NULL;
270 return err;
271 }
272 }
Richard Alpe44a8ae92015-02-09 09:50:10 +0100273
Richard Alped0796d12015-02-09 09:50:04 +0100274 arg = nlmsg_new(0, GFP_KERNEL);
275 if (!arg) {
276 kfree_skb(msg->rep);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700277 msg->rep = NULL;
Richard Alped0796d12015-02-09 09:50:04 +0100278 return -ENOMEM;
279 }
280
281 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700282 if (err) {
Richard Alped0796d12015-02-09 09:50:04 +0100283 kfree_skb(msg->rep);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700284 msg->rep = NULL;
285 }
Richard Alped0796d12015-02-09 09:50:04 +0100286 kfree_skb(arg);
287
288 return err;
289}
290
Richard Alpe9ab15462015-02-09 09:50:05 +0100291static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
292 struct tipc_nl_compat_msg *msg)
293{
294 int err;
295 struct sk_buff *doit_buf;
296 struct sk_buff *trans_buf;
297 struct nlattr **attrbuf;
298 struct genl_info info;
299
300 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
301 if (!trans_buf)
302 return -ENOMEM;
303
Richard Alpec3d6fb82015-05-06 13:58:54 +0200304 err = (*cmd->transcode)(cmd, trans_buf, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100305 if (err)
306 goto trans_out;
307
308 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
309 sizeof(struct nlattr *), GFP_KERNEL);
310 if (!attrbuf) {
311 err = -ENOMEM;
312 goto trans_out;
313 }
314
315 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
316 (const struct nlattr *)trans_buf->data,
317 trans_buf->len, NULL);
318 if (err)
319 goto parse_out;
320
321 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
322 if (!doit_buf) {
323 err = -ENOMEM;
324 goto parse_out;
325 }
326
327 doit_buf->sk = msg->dst_sk;
328
329 memset(&info, 0, sizeof(info));
330 info.attrs = attrbuf;
331
332 err = (*cmd->doit)(doit_buf, &info);
333
334 kfree_skb(doit_buf);
335parse_out:
336 kfree(attrbuf);
337trans_out:
338 kfree_skb(trans_buf);
339
340 return err;
341}
342
343static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
344 struct tipc_nl_compat_msg *msg)
345{
346 int err;
347
348 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
349 return -EINVAL;
350
351 err = __tipc_nl_compat_doit(cmd, msg);
352 if (err)
353 return err;
354
355 /* The legacy API considered an empty message a success message */
356 msg->rep = tipc_tlv_alloc(0);
357 if (!msg->rep)
358 return -ENOMEM;
359
360 return 0;
361}
362
Richard Alped0796d12015-02-09 09:50:04 +0100363static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
364 struct nlattr **attrs)
365{
366 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800367 int err;
Richard Alped0796d12015-02-09 09:50:04 +0100368
Baozeng Ding297f7d22016-05-24 22:33:24 +0800369 if (!attrs[TIPC_NLA_BEARER])
370 return -EINVAL;
371
372 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
373 attrs[TIPC_NLA_BEARER], NULL);
374 if (err)
375 return err;
Richard Alped0796d12015-02-09 09:50:04 +0100376
377 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
378 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
379 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
380}
381
Richard Alpec3d6fb82015-05-06 13:58:54 +0200382static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
383 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100384 struct tipc_nl_compat_msg *msg)
385{
386 struct nlattr *prop;
387 struct nlattr *bearer;
388 struct tipc_bearer_config *b;
Ying Xue7d0cb252019-01-14 17:22:26 +0800389 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100390
391 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
392
393 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
394 if (!bearer)
395 return -EMSGSIZE;
396
Xin Longa3791722019-03-31 22:50:08 +0800397 len = TLV_GET_DATA_LEN(msg->req);
398 len -= offsetof(struct tipc_bearer_config, name);
399 if (len <= 0)
400 return -EINVAL;
401
402 len = min_t(int, len, TIPC_MAX_BEARER_NAME);
Ying Xue7d0cb252019-01-14 17:22:26 +0800403 if (!string_is_valid(b->name, len))
404 return -EINVAL;
405
Richard Alpe9ab15462015-02-09 09:50:05 +0100406 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
407 return -EMSGSIZE;
408
409 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
410 return -EMSGSIZE;
411
412 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
413 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
414 if (!prop)
415 return -EMSGSIZE;
416 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
417 return -EMSGSIZE;
418 nla_nest_end(skb, prop);
419 }
420 nla_nest_end(skb, bearer);
421
422 return 0;
423}
424
Richard Alpec3d6fb82015-05-06 13:58:54 +0200425static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
426 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100427 struct tipc_nl_compat_msg *msg)
428{
429 char *name;
430 struct nlattr *bearer;
Ying Xue7d0cb252019-01-14 17:22:26 +0800431 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100432
433 name = (char *)TLV_DATA(msg->req);
434
435 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
436 if (!bearer)
437 return -EMSGSIZE;
438
Ying Xue7d0cb252019-01-14 17:22:26 +0800439 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
440 if (!string_is_valid(name, len))
441 return -EINVAL;
442
Richard Alpe9ab15462015-02-09 09:50:05 +0100443 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
444 return -EMSGSIZE;
445
446 nla_nest_end(skb, bearer);
447
448 return 0;
449}
450
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100451static inline u32 perc(u32 count, u32 total)
452{
453 return (count * 100 + (total / 2)) / total;
454}
455
456static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
457 struct nlattr *prop[], struct nlattr *stats[])
458{
459 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
460 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
461
462 tipc_tlv_sprintf(msg->rep,
463 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
464 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
465 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
466 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
467 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
468 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
469
470 tipc_tlv_sprintf(msg->rep,
471 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
472 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
473 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
474 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
475 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
476 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
477
478 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
479 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
480 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
481 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
482
483 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
484 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
485 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
486 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
487
488 tipc_tlv_sprintf(msg->rep,
489 " Congestion link:%u Send queue max:%u avg:%u",
490 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
491 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
492 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
493}
494
495static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
496 struct nlattr **attrs)
497{
498 char *name;
499 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
500 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
501 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800502 int err;
Ying Xue7d0cb252019-01-14 17:22:26 +0800503 int len;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100504
Baozeng Ding297f7d22016-05-24 22:33:24 +0800505 if (!attrs[TIPC_NLA_LINK])
506 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100507
Baozeng Ding297f7d22016-05-24 22:33:24 +0800508 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
509 NULL);
510 if (err)
511 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100512
Baozeng Ding297f7d22016-05-24 22:33:24 +0800513 if (!link[TIPC_NLA_LINK_PROP])
514 return -EINVAL;
515
516 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
517 link[TIPC_NLA_LINK_PROP], NULL);
518 if (err)
519 return err;
520
521 if (!link[TIPC_NLA_LINK_STATS])
522 return -EINVAL;
523
524 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
525 link[TIPC_NLA_LINK_STATS], NULL);
526 if (err)
527 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100528
529 name = (char *)TLV_DATA(msg->req);
Ying Xue7d0cb252019-01-14 17:22:26 +0800530
531 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
532 if (!string_is_valid(name, len))
533 return -EINVAL;
534
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100535 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
536 return 0;
537
538 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
539 nla_data(link[TIPC_NLA_LINK_NAME]));
540
541 if (link[TIPC_NLA_LINK_BROADCAST]) {
542 __fill_bc_link_stat(msg, prop, stats);
543 return 0;
544 }
545
546 if (link[TIPC_NLA_LINK_ACTIVE])
547 tipc_tlv_sprintf(msg->rep, " ACTIVE");
548 else if (link[TIPC_NLA_LINK_UP])
549 tipc_tlv_sprintf(msg->rep, " STANDBY");
550 else
551 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
552
553 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
554 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
555 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
556
557 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
558 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
559 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
560
561 tipc_tlv_sprintf(msg->rep,
562 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
563 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
564 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
565 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
566 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
567 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
568 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
569
570 tipc_tlv_sprintf(msg->rep,
571 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
572 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
573 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
574 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
575 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
576 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
577 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
578
579 tipc_tlv_sprintf(msg->rep,
580 " TX profile sample:%u packets average:%u octets\n",
581 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
582 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
583 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
584
585 tipc_tlv_sprintf(msg->rep,
586 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
587 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
588 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
589 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
590 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
591 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
592 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
593 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
594 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
595
596 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
597 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
598 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
599 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
600 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
601 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
602 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
603
604 tipc_tlv_sprintf(msg->rep,
605 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
606 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
607 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
608 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
609 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
610 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
611
612 tipc_tlv_sprintf(msg->rep,
613 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
614 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
615 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
616 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
617 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
618 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
619
620 tipc_tlv_sprintf(msg->rep,
621 " Congestion link:%u Send queue max:%u avg:%u",
622 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
623 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
624 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
625
626 return 0;
627}
628
Richard Alpe357ebdb2015-02-09 09:50:07 +0100629static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
630 struct nlattr **attrs)
631{
632 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
633 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800634 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100635
Baozeng Ding297f7d22016-05-24 22:33:24 +0800636 if (!attrs[TIPC_NLA_LINK])
637 return -EINVAL;
638
639 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
640 NULL);
641 if (err)
642 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100643
644 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
645 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200646 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400647 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100648
649 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
650 &link_info, sizeof(link_info));
651}
652
Richard Alpec3d6fb82015-05-06 13:58:54 +0200653static int __tipc_add_link_prop(struct sk_buff *skb,
654 struct tipc_nl_compat_msg *msg,
655 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100656{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200657 switch (msg->cmd) {
658 case TIPC_CMD_SET_LINK_PRI:
659 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
660 case TIPC_CMD_SET_LINK_TOL:
661 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
662 case TIPC_CMD_SET_LINK_WINDOW:
663 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
664 }
665
666 return -EINVAL;
667}
668
669static int tipc_nl_compat_media_set(struct sk_buff *skb,
670 struct tipc_nl_compat_msg *msg)
671{
Richard Alpe37e2d482015-02-09 09:50:08 +0100672 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200673 struct nlattr *media;
674 struct tipc_link_config *lc;
Ying Xue7d0cb252019-01-14 17:22:26 +0800675 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200676
677 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
678
679 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
680 if (!media)
681 return -EMSGSIZE;
682
Ying Xue7d0cb252019-01-14 17:22:26 +0800683 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
684 if (!string_is_valid(lc->name, len))
685 return -EINVAL;
686
Richard Alpec3d6fb82015-05-06 13:58:54 +0200687 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
688 return -EMSGSIZE;
689
690 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
691 if (!prop)
692 return -EMSGSIZE;
693
694 __tipc_add_link_prop(skb, msg, lc);
695 nla_nest_end(skb, prop);
696 nla_nest_end(skb, media);
697
698 return 0;
699}
700
701static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
702 struct tipc_nl_compat_msg *msg)
703{
704 struct nlattr *prop;
705 struct nlattr *bearer;
706 struct tipc_link_config *lc;
Ying Xue7d0cb252019-01-14 17:22:26 +0800707 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200708
709 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
710
711 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
712 if (!bearer)
713 return -EMSGSIZE;
714
Ying Xue7d0cb252019-01-14 17:22:26 +0800715 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
716 if (!string_is_valid(lc->name, len))
717 return -EINVAL;
718
Richard Alpec3d6fb82015-05-06 13:58:54 +0200719 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
720 return -EMSGSIZE;
721
722 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
723 if (!prop)
724 return -EMSGSIZE;
725
726 __tipc_add_link_prop(skb, msg, lc);
727 nla_nest_end(skb, prop);
728 nla_nest_end(skb, bearer);
729
730 return 0;
731}
732
733static int __tipc_nl_compat_link_set(struct sk_buff *skb,
734 struct tipc_nl_compat_msg *msg)
735{
736 struct nlattr *prop;
737 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100738 struct tipc_link_config *lc;
739
740 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
741
742 link = nla_nest_start(skb, TIPC_NLA_LINK);
743 if (!link)
744 return -EMSGSIZE;
745
746 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
747 return -EMSGSIZE;
748
749 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
750 if (!prop)
751 return -EMSGSIZE;
752
Richard Alpec3d6fb82015-05-06 13:58:54 +0200753 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100754 nla_nest_end(skb, prop);
755 nla_nest_end(skb, link);
756
757 return 0;
758}
759
Richard Alpec3d6fb82015-05-06 13:58:54 +0200760static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
761 struct sk_buff *skb,
762 struct tipc_nl_compat_msg *msg)
763{
764 struct tipc_link_config *lc;
765 struct tipc_bearer *bearer;
766 struct tipc_media *media;
Ying Xue3644c532019-01-14 17:22:27 +0800767 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200768
769 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
770
Xin Long7b7e51f2019-03-31 22:50:09 +0800771 len = TLV_GET_DATA_LEN(msg->req);
772 len -= offsetof(struct tipc_link_config, name);
773 if (len <= 0)
774 return -EINVAL;
775
776 len = min_t(int, len, TIPC_MAX_LINK_NAME);
Ying Xue3644c532019-01-14 17:22:27 +0800777 if (!string_is_valid(lc->name, len))
778 return -EINVAL;
779
Richard Alpec3d6fb82015-05-06 13:58:54 +0200780 media = tipc_media_find(lc->name);
781 if (media) {
782 cmd->doit = &tipc_nl_media_set;
783 return tipc_nl_compat_media_set(skb, msg);
784 }
785
786 bearer = tipc_bearer_find(msg->net, lc->name);
787 if (bearer) {
788 cmd->doit = &tipc_nl_bearer_set;
789 return tipc_nl_compat_bearer_set(skb, msg);
790 }
791
792 return __tipc_nl_compat_link_set(skb, msg);
793}
794
795static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
796 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100797 struct tipc_nl_compat_msg *msg)
798{
799 char *name;
800 struct nlattr *link;
Ying Xue4cd995a2019-01-14 17:22:25 +0800801 int len;
Richard Alpe18178772015-02-09 09:50:09 +0100802
803 name = (char *)TLV_DATA(msg->req);
804
805 link = nla_nest_start(skb, TIPC_NLA_LINK);
806 if (!link)
807 return -EMSGSIZE;
808
Ying Xue4cd995a2019-01-14 17:22:25 +0800809 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
810 if (!string_is_valid(name, len))
811 return -EINVAL;
812
Richard Alpe18178772015-02-09 09:50:09 +0100813 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
814 return -EMSGSIZE;
815
816 nla_nest_end(skb, link);
817
818 return 0;
819}
820
Richard Alpe44a8ae92015-02-09 09:50:10 +0100821static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
822{
823 int i;
824 u32 depth;
825 struct tipc_name_table_query *ntq;
826 static const char * const header[] = {
827 "Type ",
828 "Lower Upper ",
829 "Port Identity ",
830 "Publication Scope"
831 };
832
833 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
Ying Xue4c559fb2019-01-14 17:22:28 +0800834 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
835 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100836
837 depth = ntohl(ntq->depth);
838
839 if (depth > 4)
840 depth = 4;
841 for (i = 0; i < depth; i++)
842 tipc_tlv_sprintf(msg->rep, header[i]);
843 tipc_tlv_sprintf(msg->rep, "\n");
844
845 return 0;
846}
847
848static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
849 struct nlattr **attrs)
850{
851 char port_str[27];
852 struct tipc_name_table_query *ntq;
853 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
854 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
855 u32 node, depth, type, lowbound, upbound;
856 static const char * const scope_str[] = {"", " zone", " cluster",
857 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800858 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100859
Baozeng Ding297f7d22016-05-24 22:33:24 +0800860 if (!attrs[TIPC_NLA_NAME_TABLE])
861 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100862
Baozeng Ding297f7d22016-05-24 22:33:24 +0800863 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
864 attrs[TIPC_NLA_NAME_TABLE], NULL);
865 if (err)
866 return err;
867
868 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
869 return -EINVAL;
870
871 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
872 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
873 if (err)
874 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100875
876 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
877
878 depth = ntohl(ntq->depth);
879 type = ntohl(ntq->type);
880 lowbound = ntohl(ntq->lowbound);
881 upbound = ntohl(ntq->upbound);
882
883 if (!(depth & TIPC_NTQ_ALLTYPES) &&
884 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
885 return 0;
886 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
887 return 0;
888 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
889 return 0;
890
891 tipc_tlv_sprintf(msg->rep, "%-10u ",
892 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
893
894 if (depth == 1)
895 goto out;
896
897 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
898 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
899 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
900
901 if (depth == 2)
902 goto out;
903
904 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
905 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
906 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
907 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
908
909 if (depth == 3)
910 goto out;
911
912 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200913 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100914 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
915out:
916 tipc_tlv_sprintf(msg->rep, "\n");
917
918 return 0;
919}
920
Richard Alpe487d2a32015-02-09 09:50:11 +0100921static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
922 struct nlattr **attrs)
923{
924 u32 type, lower, upper;
925 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800926 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100927
Baozeng Ding297f7d22016-05-24 22:33:24 +0800928 if (!attrs[TIPC_NLA_PUBL])
929 return -EINVAL;
930
931 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
932 NULL);
933 if (err)
934 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100935
936 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
937 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
938 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
939
940 if (lower == upper)
941 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
942 else
943 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
944
945 return 0;
946}
947
948static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
949{
950 int err;
951 void *hdr;
952 struct nlattr *nest;
953 struct sk_buff *args;
954 struct tipc_nl_compat_cmd_dump dump;
955
956 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
957 if (!args)
958 return -ENOMEM;
959
960 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
961 TIPC_NL_PUBL_GET);
962
963 nest = nla_nest_start(args, TIPC_NLA_SOCK);
964 if (!nest) {
965 kfree_skb(args);
966 return -EMSGSIZE;
967 }
968
969 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
970 kfree_skb(args);
971 return -EMSGSIZE;
972 }
973
974 nla_nest_end(args, nest);
975 genlmsg_end(args, hdr);
976
977 dump.dumpit = tipc_nl_publ_dump;
978 dump.format = __tipc_nl_compat_publ_dump;
979
980 err = __tipc_nl_compat_dumpit(&dump, msg, args);
981
982 kfree_skb(args);
983
984 return err;
985}
986
987static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
988 struct nlattr **attrs)
989{
990 int err;
991 u32 sock_ref;
992 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
993
Baozeng Ding297f7d22016-05-24 22:33:24 +0800994 if (!attrs[TIPC_NLA_SOCK])
995 return -EINVAL;
996
997 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
998 NULL);
999 if (err)
1000 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +01001001
1002 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1003 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
1004
1005 if (sock[TIPC_NLA_SOCK_CON]) {
1006 u32 node;
1007 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
1008
1009 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
1010 NULL);
1011
1012 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1013 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1014 tipc_zone(node),
1015 tipc_cluster(node),
1016 tipc_node(node),
1017 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1018
1019 if (con[TIPC_NLA_CON_FLAG])
1020 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1021 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1022 nla_get_u32(con[TIPC_NLA_CON_INST]));
1023 else
1024 tipc_tlv_sprintf(msg->rep, "\n");
1025 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1026 tipc_tlv_sprintf(msg->rep, " bound to");
1027
1028 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1029 if (err)
1030 return err;
1031 }
1032 tipc_tlv_sprintf(msg->rep, "\n");
1033
1034 return 0;
1035}
1036
Richard Alpe5bfc3352015-02-09 09:50:12 +01001037static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1038 struct nlattr **attrs)
1039{
1040 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001041 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001042
Baozeng Ding297f7d22016-05-24 22:33:24 +08001043 if (!attrs[TIPC_NLA_MEDIA])
1044 return -EINVAL;
1045
1046 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
1047 NULL);
1048 if (err)
1049 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001050
1051 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1052 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1053 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1054}
1055
Richard Alpe4b28cb52015-02-09 09:50:13 +01001056static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1057 struct nlattr **attrs)
1058{
1059 struct tipc_node_info node_info;
1060 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001061 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001062
Baozeng Ding297f7d22016-05-24 22:33:24 +08001063 if (!attrs[TIPC_NLA_NODE])
1064 return -EINVAL;
1065
1066 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1067 NULL);
1068 if (err)
1069 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001070
1071 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1072 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1073
1074 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1075 sizeof(node_info));
1076}
1077
Richard Alpec3d6fb82015-05-06 13:58:54 +02001078static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1079 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001080 struct tipc_nl_compat_msg *msg)
1081{
1082 u32 val;
1083 struct nlattr *net;
1084
1085 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1086
1087 net = nla_nest_start(skb, TIPC_NLA_NET);
1088 if (!net)
1089 return -EMSGSIZE;
1090
Richard Alpe964f9502015-02-09 09:50:15 +01001091 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1092 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1093 return -EMSGSIZE;
1094 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1095 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1096 return -EMSGSIZE;
1097 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001098 nla_nest_end(skb, net);
1099
1100 return 0;
1101}
1102
Richard Alpe3c261812015-02-09 09:50:16 +01001103static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1104 struct nlattr **attrs)
1105{
1106 __be32 id;
1107 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001108 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001109
Baozeng Ding297f7d22016-05-24 22:33:24 +08001110 if (!attrs[TIPC_NLA_NET])
1111 return -EINVAL;
1112
1113 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1114 NULL);
1115 if (err)
1116 return err;
1117
Richard Alpe3c261812015-02-09 09:50:16 +01001118 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1119
1120 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1121}
1122
Richard Alpe5a81a632015-02-09 09:50:17 +01001123static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1124{
1125 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1126 if (!msg->rep)
1127 return -ENOMEM;
1128
1129 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1130 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1131
1132 return 0;
1133}
1134
Richard Alped0796d12015-02-09 09:50:04 +01001135static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1136{
1137 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001138 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001139
1140 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001141 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001142
1143 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001144 case TIPC_CMD_NOOP:
1145 msg->rep = tipc_tlv_alloc(0);
1146 if (!msg->rep)
1147 return -ENOMEM;
1148 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001149 case TIPC_CMD_GET_BEARER_NAMES:
1150 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1151 dump.dumpit = tipc_nl_bearer_dump;
1152 dump.format = tipc_nl_compat_bearer_dump;
1153 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001154 case TIPC_CMD_ENABLE_BEARER:
1155 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1156 doit.doit = tipc_nl_bearer_enable;
1157 doit.transcode = tipc_nl_compat_bearer_enable;
1158 return tipc_nl_compat_doit(&doit, msg);
1159 case TIPC_CMD_DISABLE_BEARER:
1160 msg->req_type = TIPC_TLV_BEARER_NAME;
1161 doit.doit = tipc_nl_bearer_disable;
1162 doit.transcode = tipc_nl_compat_bearer_disable;
1163 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001164 case TIPC_CMD_SHOW_LINK_STATS:
1165 msg->req_type = TIPC_TLV_LINK_NAME;
1166 msg->rep_size = ULTRA_STRING_MAX_LEN;
1167 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001168 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001169 dump.format = tipc_nl_compat_link_stat_dump;
1170 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001171 case TIPC_CMD_GET_LINKS:
1172 msg->req_type = TIPC_TLV_NET_ADDR;
1173 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001174 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001175 dump.format = tipc_nl_compat_link_dump;
1176 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001177 case TIPC_CMD_SET_LINK_TOL:
1178 case TIPC_CMD_SET_LINK_PRI:
1179 case TIPC_CMD_SET_LINK_WINDOW:
1180 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001181 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001182 doit.transcode = tipc_nl_compat_link_set;
1183 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001184 case TIPC_CMD_RESET_LINK_STATS:
1185 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001186 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001187 doit.transcode = tipc_nl_compat_link_reset_stats;
1188 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001189 case TIPC_CMD_SHOW_NAME_TABLE:
1190 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1191 msg->rep_size = ULTRA_STRING_MAX_LEN;
1192 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1193 dump.header = tipc_nl_compat_name_table_dump_header;
1194 dump.dumpit = tipc_nl_name_table_dump;
1195 dump.format = tipc_nl_compat_name_table_dump;
1196 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001197 case TIPC_CMD_SHOW_PORTS:
1198 msg->rep_size = ULTRA_STRING_MAX_LEN;
1199 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1200 dump.dumpit = tipc_nl_sk_dump;
1201 dump.format = tipc_nl_compat_sk_dump;
1202 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001203 case TIPC_CMD_GET_MEDIA_NAMES:
1204 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1205 dump.dumpit = tipc_nl_media_dump;
1206 dump.format = tipc_nl_compat_media_dump;
1207 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001208 case TIPC_CMD_GET_NODES:
1209 msg->rep_size = ULTRA_STRING_MAX_LEN;
1210 dump.dumpit = tipc_nl_node_dump;
1211 dump.format = tipc_nl_compat_node_dump;
1212 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001213 case TIPC_CMD_SET_NODE_ADDR:
1214 msg->req_type = TIPC_TLV_NET_ADDR;
1215 doit.doit = tipc_nl_net_set;
1216 doit.transcode = tipc_nl_compat_net_set;
1217 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001218 case TIPC_CMD_SET_NETID:
1219 msg->req_type = TIPC_TLV_UNSIGNED;
1220 doit.doit = tipc_nl_net_set;
1221 doit.transcode = tipc_nl_compat_net_set;
1222 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001223 case TIPC_CMD_GET_NETID:
1224 msg->rep_size = sizeof(u32);
1225 dump.dumpit = tipc_nl_net_dump;
1226 dump.format = tipc_nl_compat_net_dump;
1227 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001228 case TIPC_CMD_SHOW_STATS:
1229 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001230 }
1231
1232 return -EOPNOTSUPP;
1233}
1234
1235static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1236{
1237 int err;
1238 int len;
1239 struct tipc_nl_compat_msg msg;
1240 struct nlmsghdr *req_nlh;
1241 struct nlmsghdr *rep_nlh;
1242 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001243
1244 memset(&msg, 0, sizeof(msg));
1245
1246 req_nlh = (struct nlmsghdr *)skb->data;
1247 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1248 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001249 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001250 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001251
1252 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1253 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1254 err = -EACCES;
1255 goto send;
1256 }
1257
1258 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Ying Xue02862eb2019-01-14 17:22:29 +08001259 if (!len || !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001260 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1261 err = -EOPNOTSUPP;
1262 goto send;
1263 }
1264
1265 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001266 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001267 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1268 else if (err == -EINVAL)
1269 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1270send:
1271 if (!msg.rep)
1272 return err;
1273
1274 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1275 skb_push(msg.rep, len);
1276 rep_nlh = nlmsg_hdr(msg.rep);
1277 memcpy(rep_nlh, info->nlhdr, len);
1278 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001279 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001280
1281 return err;
1282}
1283
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001284static struct genl_family tipc_genl_compat_family = {
1285 .id = GENL_ID_GENERATE,
1286 .name = TIPC_GENL_NAME,
1287 .version = TIPC_GENL_VERSION,
1288 .hdrsize = TIPC_GENL_HDRLEN,
1289 .maxattr = 0,
1290 .netnsok = true,
1291};
1292
1293static struct genl_ops tipc_genl_compat_ops[] = {
1294 {
1295 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001296 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001297 },
1298};
1299
1300int tipc_netlink_compat_start(void)
1301{
1302 int res;
1303
1304 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1305 tipc_genl_compat_ops);
1306 if (res) {
1307 pr_err("Failed to register legacy compat interface\n");
1308 return res;
1309 }
1310
1311 return 0;
1312}
1313
1314void tipc_netlink_compat_stop(void)
1315{
1316 genl_unregister_family(&tipc_genl_compat_family);
1317}