blob: daa7104150125238932024afa14e6762231ab584 [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
Ying Xue7d0cb252019-01-14 17:22:26 +0800397 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
398 if (!string_is_valid(b->name, len))
399 return -EINVAL;
400
Richard Alpe9ab15462015-02-09 09:50:05 +0100401 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
402 return -EMSGSIZE;
403
404 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
405 return -EMSGSIZE;
406
407 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
408 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
409 if (!prop)
410 return -EMSGSIZE;
411 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
412 return -EMSGSIZE;
413 nla_nest_end(skb, prop);
414 }
415 nla_nest_end(skb, bearer);
416
417 return 0;
418}
419
Richard Alpec3d6fb82015-05-06 13:58:54 +0200420static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
421 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100422 struct tipc_nl_compat_msg *msg)
423{
424 char *name;
425 struct nlattr *bearer;
Ying Xue7d0cb252019-01-14 17:22:26 +0800426 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100427
428 name = (char *)TLV_DATA(msg->req);
429
430 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
431 if (!bearer)
432 return -EMSGSIZE;
433
Ying Xue7d0cb252019-01-14 17:22:26 +0800434 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
435 if (!string_is_valid(name, len))
436 return -EINVAL;
437
Richard Alpe9ab15462015-02-09 09:50:05 +0100438 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
439 return -EMSGSIZE;
440
441 nla_nest_end(skb, bearer);
442
443 return 0;
444}
445
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100446static inline u32 perc(u32 count, u32 total)
447{
448 return (count * 100 + (total / 2)) / total;
449}
450
451static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
452 struct nlattr *prop[], struct nlattr *stats[])
453{
454 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
455 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
456
457 tipc_tlv_sprintf(msg->rep,
458 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
459 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
460 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
461 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
462 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
463 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
464
465 tipc_tlv_sprintf(msg->rep,
466 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
467 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
468 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
469 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
470 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
471 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
472
473 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
474 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
475 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
476 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
477
478 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
479 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
480 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
481 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
482
483 tipc_tlv_sprintf(msg->rep,
484 " Congestion link:%u Send queue max:%u avg:%u",
485 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
486 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
487 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
488}
489
490static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
491 struct nlattr **attrs)
492{
493 char *name;
494 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
495 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
496 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800497 int err;
Ying Xue7d0cb252019-01-14 17:22:26 +0800498 int len;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100499
Baozeng Ding297f7d22016-05-24 22:33:24 +0800500 if (!attrs[TIPC_NLA_LINK])
501 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100502
Baozeng Ding297f7d22016-05-24 22:33:24 +0800503 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
504 NULL);
505 if (err)
506 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100507
Baozeng Ding297f7d22016-05-24 22:33:24 +0800508 if (!link[TIPC_NLA_LINK_PROP])
509 return -EINVAL;
510
511 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
512 link[TIPC_NLA_LINK_PROP], NULL);
513 if (err)
514 return err;
515
516 if (!link[TIPC_NLA_LINK_STATS])
517 return -EINVAL;
518
519 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
520 link[TIPC_NLA_LINK_STATS], NULL);
521 if (err)
522 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100523
524 name = (char *)TLV_DATA(msg->req);
Ying Xue7d0cb252019-01-14 17:22:26 +0800525
526 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
527 if (!string_is_valid(name, len))
528 return -EINVAL;
529
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100530 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
531 return 0;
532
533 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
534 nla_data(link[TIPC_NLA_LINK_NAME]));
535
536 if (link[TIPC_NLA_LINK_BROADCAST]) {
537 __fill_bc_link_stat(msg, prop, stats);
538 return 0;
539 }
540
541 if (link[TIPC_NLA_LINK_ACTIVE])
542 tipc_tlv_sprintf(msg->rep, " ACTIVE");
543 else if (link[TIPC_NLA_LINK_UP])
544 tipc_tlv_sprintf(msg->rep, " STANDBY");
545 else
546 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
547
548 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
549 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
550 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
551
552 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
553 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
554 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
555
556 tipc_tlv_sprintf(msg->rep,
557 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
558 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
559 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
560 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
561 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
562 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
563 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
564
565 tipc_tlv_sprintf(msg->rep,
566 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
567 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
568 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
569 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
570 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
571 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
572 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
573
574 tipc_tlv_sprintf(msg->rep,
575 " TX profile sample:%u packets average:%u octets\n",
576 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
577 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
578 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
579
580 tipc_tlv_sprintf(msg->rep,
581 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
582 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
583 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
584 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
585 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
586 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
587 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
588 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
589 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
590
591 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
592 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
593 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
594 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
595 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
596 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
597 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
598
599 tipc_tlv_sprintf(msg->rep,
600 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
601 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
602 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
603 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
604 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
605 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
606
607 tipc_tlv_sprintf(msg->rep,
608 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
609 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
610 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
611 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
612 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
613 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
614
615 tipc_tlv_sprintf(msg->rep,
616 " Congestion link:%u Send queue max:%u avg:%u",
617 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
618 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
619 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
620
621 return 0;
622}
623
Richard Alpe357ebdb2015-02-09 09:50:07 +0100624static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
625 struct nlattr **attrs)
626{
627 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
628 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800629 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100630
Baozeng Ding297f7d22016-05-24 22:33:24 +0800631 if (!attrs[TIPC_NLA_LINK])
632 return -EINVAL;
633
634 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
635 NULL);
636 if (err)
637 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100638
639 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
640 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200641 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400642 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100643
644 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
645 &link_info, sizeof(link_info));
646}
647
Richard Alpec3d6fb82015-05-06 13:58:54 +0200648static int __tipc_add_link_prop(struct sk_buff *skb,
649 struct tipc_nl_compat_msg *msg,
650 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100651{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200652 switch (msg->cmd) {
653 case TIPC_CMD_SET_LINK_PRI:
654 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
655 case TIPC_CMD_SET_LINK_TOL:
656 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
657 case TIPC_CMD_SET_LINK_WINDOW:
658 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
659 }
660
661 return -EINVAL;
662}
663
664static int tipc_nl_compat_media_set(struct sk_buff *skb,
665 struct tipc_nl_compat_msg *msg)
666{
Richard Alpe37e2d482015-02-09 09:50:08 +0100667 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200668 struct nlattr *media;
669 struct tipc_link_config *lc;
Ying Xue7d0cb252019-01-14 17:22:26 +0800670 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200671
672 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
673
674 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
675 if (!media)
676 return -EMSGSIZE;
677
Ying Xue7d0cb252019-01-14 17:22:26 +0800678 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
679 if (!string_is_valid(lc->name, len))
680 return -EINVAL;
681
Richard Alpec3d6fb82015-05-06 13:58:54 +0200682 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
683 return -EMSGSIZE;
684
685 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
686 if (!prop)
687 return -EMSGSIZE;
688
689 __tipc_add_link_prop(skb, msg, lc);
690 nla_nest_end(skb, prop);
691 nla_nest_end(skb, media);
692
693 return 0;
694}
695
696static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
697 struct tipc_nl_compat_msg *msg)
698{
699 struct nlattr *prop;
700 struct nlattr *bearer;
701 struct tipc_link_config *lc;
Ying Xue7d0cb252019-01-14 17:22:26 +0800702 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200703
704 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
705
706 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
707 if (!bearer)
708 return -EMSGSIZE;
709
Ying Xue7d0cb252019-01-14 17:22:26 +0800710 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
711 if (!string_is_valid(lc->name, len))
712 return -EINVAL;
713
Richard Alpec3d6fb82015-05-06 13:58:54 +0200714 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
715 return -EMSGSIZE;
716
717 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
718 if (!prop)
719 return -EMSGSIZE;
720
721 __tipc_add_link_prop(skb, msg, lc);
722 nla_nest_end(skb, prop);
723 nla_nest_end(skb, bearer);
724
725 return 0;
726}
727
728static int __tipc_nl_compat_link_set(struct sk_buff *skb,
729 struct tipc_nl_compat_msg *msg)
730{
731 struct nlattr *prop;
732 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100733 struct tipc_link_config *lc;
734
735 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
736
737 link = nla_nest_start(skb, TIPC_NLA_LINK);
738 if (!link)
739 return -EMSGSIZE;
740
741 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
742 return -EMSGSIZE;
743
744 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
745 if (!prop)
746 return -EMSGSIZE;
747
Richard Alpec3d6fb82015-05-06 13:58:54 +0200748 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100749 nla_nest_end(skb, prop);
750 nla_nest_end(skb, link);
751
752 return 0;
753}
754
Richard Alpec3d6fb82015-05-06 13:58:54 +0200755static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
756 struct sk_buff *skb,
757 struct tipc_nl_compat_msg *msg)
758{
759 struct tipc_link_config *lc;
760 struct tipc_bearer *bearer;
761 struct tipc_media *media;
Ying Xue3644c532019-01-14 17:22:27 +0800762 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200763
764 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
765
Ying Xue3644c532019-01-14 17:22:27 +0800766 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
767 if (!string_is_valid(lc->name, len))
768 return -EINVAL;
769
Richard Alpec3d6fb82015-05-06 13:58:54 +0200770 media = tipc_media_find(lc->name);
771 if (media) {
772 cmd->doit = &tipc_nl_media_set;
773 return tipc_nl_compat_media_set(skb, msg);
774 }
775
776 bearer = tipc_bearer_find(msg->net, lc->name);
777 if (bearer) {
778 cmd->doit = &tipc_nl_bearer_set;
779 return tipc_nl_compat_bearer_set(skb, msg);
780 }
781
782 return __tipc_nl_compat_link_set(skb, msg);
783}
784
785static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
786 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100787 struct tipc_nl_compat_msg *msg)
788{
789 char *name;
790 struct nlattr *link;
Ying Xue4cd995a2019-01-14 17:22:25 +0800791 int len;
Richard Alpe18178772015-02-09 09:50:09 +0100792
793 name = (char *)TLV_DATA(msg->req);
794
795 link = nla_nest_start(skb, TIPC_NLA_LINK);
796 if (!link)
797 return -EMSGSIZE;
798
Ying Xue4cd995a2019-01-14 17:22:25 +0800799 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
800 if (!string_is_valid(name, len))
801 return -EINVAL;
802
Richard Alpe18178772015-02-09 09:50:09 +0100803 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
804 return -EMSGSIZE;
805
806 nla_nest_end(skb, link);
807
808 return 0;
809}
810
Richard Alpe44a8ae92015-02-09 09:50:10 +0100811static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
812{
813 int i;
814 u32 depth;
815 struct tipc_name_table_query *ntq;
816 static const char * const header[] = {
817 "Type ",
818 "Lower Upper ",
819 "Port Identity ",
820 "Publication Scope"
821 };
822
823 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
Ying Xue4c559fb2019-01-14 17:22:28 +0800824 if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query))
825 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100826
827 depth = ntohl(ntq->depth);
828
829 if (depth > 4)
830 depth = 4;
831 for (i = 0; i < depth; i++)
832 tipc_tlv_sprintf(msg->rep, header[i]);
833 tipc_tlv_sprintf(msg->rep, "\n");
834
835 return 0;
836}
837
838static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
839 struct nlattr **attrs)
840{
841 char port_str[27];
842 struct tipc_name_table_query *ntq;
843 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
844 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
845 u32 node, depth, type, lowbound, upbound;
846 static const char * const scope_str[] = {"", " zone", " cluster",
847 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800848 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100849
Baozeng Ding297f7d22016-05-24 22:33:24 +0800850 if (!attrs[TIPC_NLA_NAME_TABLE])
851 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100852
Baozeng Ding297f7d22016-05-24 22:33:24 +0800853 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
854 attrs[TIPC_NLA_NAME_TABLE], NULL);
855 if (err)
856 return err;
857
858 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
859 return -EINVAL;
860
861 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
862 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
863 if (err)
864 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100865
866 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
867
868 depth = ntohl(ntq->depth);
869 type = ntohl(ntq->type);
870 lowbound = ntohl(ntq->lowbound);
871 upbound = ntohl(ntq->upbound);
872
873 if (!(depth & TIPC_NTQ_ALLTYPES) &&
874 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
875 return 0;
876 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
877 return 0;
878 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
879 return 0;
880
881 tipc_tlv_sprintf(msg->rep, "%-10u ",
882 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
883
884 if (depth == 1)
885 goto out;
886
887 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
888 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
889 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
890
891 if (depth == 2)
892 goto out;
893
894 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
895 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
896 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
897 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
898
899 if (depth == 3)
900 goto out;
901
902 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200903 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100904 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
905out:
906 tipc_tlv_sprintf(msg->rep, "\n");
907
908 return 0;
909}
910
Richard Alpe487d2a32015-02-09 09:50:11 +0100911static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
912 struct nlattr **attrs)
913{
914 u32 type, lower, upper;
915 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800916 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100917
Baozeng Ding297f7d22016-05-24 22:33:24 +0800918 if (!attrs[TIPC_NLA_PUBL])
919 return -EINVAL;
920
921 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
922 NULL);
923 if (err)
924 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100925
926 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
927 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
928 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
929
930 if (lower == upper)
931 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
932 else
933 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
934
935 return 0;
936}
937
938static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
939{
940 int err;
941 void *hdr;
942 struct nlattr *nest;
943 struct sk_buff *args;
944 struct tipc_nl_compat_cmd_dump dump;
945
946 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
947 if (!args)
948 return -ENOMEM;
949
950 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
951 TIPC_NL_PUBL_GET);
952
953 nest = nla_nest_start(args, TIPC_NLA_SOCK);
954 if (!nest) {
955 kfree_skb(args);
956 return -EMSGSIZE;
957 }
958
959 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
960 kfree_skb(args);
961 return -EMSGSIZE;
962 }
963
964 nla_nest_end(args, nest);
965 genlmsg_end(args, hdr);
966
967 dump.dumpit = tipc_nl_publ_dump;
968 dump.format = __tipc_nl_compat_publ_dump;
969
970 err = __tipc_nl_compat_dumpit(&dump, msg, args);
971
972 kfree_skb(args);
973
974 return err;
975}
976
977static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
978 struct nlattr **attrs)
979{
980 int err;
981 u32 sock_ref;
982 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
983
Baozeng Ding297f7d22016-05-24 22:33:24 +0800984 if (!attrs[TIPC_NLA_SOCK])
985 return -EINVAL;
986
987 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
988 NULL);
989 if (err)
990 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100991
992 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
993 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
994
995 if (sock[TIPC_NLA_SOCK_CON]) {
996 u32 node;
997 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
998
999 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
1000 NULL);
1001
1002 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
1003 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
1004 tipc_zone(node),
1005 tipc_cluster(node),
1006 tipc_node(node),
1007 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
1008
1009 if (con[TIPC_NLA_CON_FLAG])
1010 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
1011 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
1012 nla_get_u32(con[TIPC_NLA_CON_INST]));
1013 else
1014 tipc_tlv_sprintf(msg->rep, "\n");
1015 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1016 tipc_tlv_sprintf(msg->rep, " bound to");
1017
1018 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1019 if (err)
1020 return err;
1021 }
1022 tipc_tlv_sprintf(msg->rep, "\n");
1023
1024 return 0;
1025}
1026
Richard Alpe5bfc3352015-02-09 09:50:12 +01001027static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1028 struct nlattr **attrs)
1029{
1030 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001031 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001032
Baozeng Ding297f7d22016-05-24 22:33:24 +08001033 if (!attrs[TIPC_NLA_MEDIA])
1034 return -EINVAL;
1035
1036 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
1037 NULL);
1038 if (err)
1039 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001040
1041 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1042 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1043 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1044}
1045
Richard Alpe4b28cb52015-02-09 09:50:13 +01001046static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1047 struct nlattr **attrs)
1048{
1049 struct tipc_node_info node_info;
1050 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001051 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001052
Baozeng Ding297f7d22016-05-24 22:33:24 +08001053 if (!attrs[TIPC_NLA_NODE])
1054 return -EINVAL;
1055
1056 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1057 NULL);
1058 if (err)
1059 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001060
1061 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1062 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1063
1064 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1065 sizeof(node_info));
1066}
1067
Richard Alpec3d6fb82015-05-06 13:58:54 +02001068static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1069 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001070 struct tipc_nl_compat_msg *msg)
1071{
1072 u32 val;
1073 struct nlattr *net;
1074
1075 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1076
1077 net = nla_nest_start(skb, TIPC_NLA_NET);
1078 if (!net)
1079 return -EMSGSIZE;
1080
Richard Alpe964f9502015-02-09 09:50:15 +01001081 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1082 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1083 return -EMSGSIZE;
1084 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1085 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1086 return -EMSGSIZE;
1087 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001088 nla_nest_end(skb, net);
1089
1090 return 0;
1091}
1092
Richard Alpe3c261812015-02-09 09:50:16 +01001093static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1094 struct nlattr **attrs)
1095{
1096 __be32 id;
1097 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001098 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001099
Baozeng Ding297f7d22016-05-24 22:33:24 +08001100 if (!attrs[TIPC_NLA_NET])
1101 return -EINVAL;
1102
1103 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1104 NULL);
1105 if (err)
1106 return err;
1107
Richard Alpe3c261812015-02-09 09:50:16 +01001108 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1109
1110 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1111}
1112
Richard Alpe5a81a632015-02-09 09:50:17 +01001113static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1114{
1115 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1116 if (!msg->rep)
1117 return -ENOMEM;
1118
1119 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1120 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1121
1122 return 0;
1123}
1124
Richard Alped0796d12015-02-09 09:50:04 +01001125static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1126{
1127 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001128 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001129
1130 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001131 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001132
1133 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001134 case TIPC_CMD_NOOP:
1135 msg->rep = tipc_tlv_alloc(0);
1136 if (!msg->rep)
1137 return -ENOMEM;
1138 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001139 case TIPC_CMD_GET_BEARER_NAMES:
1140 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1141 dump.dumpit = tipc_nl_bearer_dump;
1142 dump.format = tipc_nl_compat_bearer_dump;
1143 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001144 case TIPC_CMD_ENABLE_BEARER:
1145 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1146 doit.doit = tipc_nl_bearer_enable;
1147 doit.transcode = tipc_nl_compat_bearer_enable;
1148 return tipc_nl_compat_doit(&doit, msg);
1149 case TIPC_CMD_DISABLE_BEARER:
1150 msg->req_type = TIPC_TLV_BEARER_NAME;
1151 doit.doit = tipc_nl_bearer_disable;
1152 doit.transcode = tipc_nl_compat_bearer_disable;
1153 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001154 case TIPC_CMD_SHOW_LINK_STATS:
1155 msg->req_type = TIPC_TLV_LINK_NAME;
1156 msg->rep_size = ULTRA_STRING_MAX_LEN;
1157 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001158 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001159 dump.format = tipc_nl_compat_link_stat_dump;
1160 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001161 case TIPC_CMD_GET_LINKS:
1162 msg->req_type = TIPC_TLV_NET_ADDR;
1163 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001164 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001165 dump.format = tipc_nl_compat_link_dump;
1166 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001167 case TIPC_CMD_SET_LINK_TOL:
1168 case TIPC_CMD_SET_LINK_PRI:
1169 case TIPC_CMD_SET_LINK_WINDOW:
1170 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001171 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001172 doit.transcode = tipc_nl_compat_link_set;
1173 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001174 case TIPC_CMD_RESET_LINK_STATS:
1175 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001176 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001177 doit.transcode = tipc_nl_compat_link_reset_stats;
1178 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001179 case TIPC_CMD_SHOW_NAME_TABLE:
1180 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1181 msg->rep_size = ULTRA_STRING_MAX_LEN;
1182 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1183 dump.header = tipc_nl_compat_name_table_dump_header;
1184 dump.dumpit = tipc_nl_name_table_dump;
1185 dump.format = tipc_nl_compat_name_table_dump;
1186 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001187 case TIPC_CMD_SHOW_PORTS:
1188 msg->rep_size = ULTRA_STRING_MAX_LEN;
1189 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1190 dump.dumpit = tipc_nl_sk_dump;
1191 dump.format = tipc_nl_compat_sk_dump;
1192 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001193 case TIPC_CMD_GET_MEDIA_NAMES:
1194 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1195 dump.dumpit = tipc_nl_media_dump;
1196 dump.format = tipc_nl_compat_media_dump;
1197 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001198 case TIPC_CMD_GET_NODES:
1199 msg->rep_size = ULTRA_STRING_MAX_LEN;
1200 dump.dumpit = tipc_nl_node_dump;
1201 dump.format = tipc_nl_compat_node_dump;
1202 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001203 case TIPC_CMD_SET_NODE_ADDR:
1204 msg->req_type = TIPC_TLV_NET_ADDR;
1205 doit.doit = tipc_nl_net_set;
1206 doit.transcode = tipc_nl_compat_net_set;
1207 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001208 case TIPC_CMD_SET_NETID:
1209 msg->req_type = TIPC_TLV_UNSIGNED;
1210 doit.doit = tipc_nl_net_set;
1211 doit.transcode = tipc_nl_compat_net_set;
1212 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001213 case TIPC_CMD_GET_NETID:
1214 msg->rep_size = sizeof(u32);
1215 dump.dumpit = tipc_nl_net_dump;
1216 dump.format = tipc_nl_compat_net_dump;
1217 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001218 case TIPC_CMD_SHOW_STATS:
1219 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001220 }
1221
1222 return -EOPNOTSUPP;
1223}
1224
1225static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1226{
1227 int err;
1228 int len;
1229 struct tipc_nl_compat_msg msg;
1230 struct nlmsghdr *req_nlh;
1231 struct nlmsghdr *rep_nlh;
1232 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001233
1234 memset(&msg, 0, sizeof(msg));
1235
1236 req_nlh = (struct nlmsghdr *)skb->data;
1237 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1238 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001239 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001240 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001241
1242 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1243 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1244 err = -EACCES;
1245 goto send;
1246 }
1247
1248 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Ying Xue02862eb2019-01-14 17:22:29 +08001249 if (!len || !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001250 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1251 err = -EOPNOTSUPP;
1252 goto send;
1253 }
1254
1255 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001256 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001257 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1258 else if (err == -EINVAL)
1259 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1260send:
1261 if (!msg.rep)
1262 return err;
1263
1264 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1265 skb_push(msg.rep, len);
1266 rep_nlh = nlmsg_hdr(msg.rep);
1267 memcpy(rep_nlh, info->nlhdr, len);
1268 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001269 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001270
1271 return err;
1272}
1273
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001274static struct genl_family tipc_genl_compat_family = {
1275 .id = GENL_ID_GENERATE,
1276 .name = TIPC_GENL_NAME,
1277 .version = TIPC_GENL_VERSION,
1278 .hdrsize = TIPC_GENL_HDRLEN,
1279 .maxattr = 0,
1280 .netnsok = true,
1281};
1282
1283static struct genl_ops tipc_genl_compat_ops[] = {
1284 {
1285 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001286 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001287 },
1288};
1289
1290int tipc_netlink_compat_start(void)
1291{
1292 int res;
1293
1294 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1295 tipc_genl_compat_ops);
1296 if (res) {
1297 pr_err("Failed to register legacy compat interface\n");
1298 return res;
1299 }
1300
1301 return 0;
1302}
1303
1304void tipc_netlink_compat_stop(void)
1305{
1306 genl_unregister_family(&tipc_genl_compat_family);
1307}