blob: 7578411e73397ba1d7c2bdde5f731d34babd7671 [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;
383
384 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
385
386 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
387 if (!bearer)
388 return -EMSGSIZE;
389
390 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
391 return -EMSGSIZE;
392
393 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
394 return -EMSGSIZE;
395
396 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
397 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
398 if (!prop)
399 return -EMSGSIZE;
400 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
401 return -EMSGSIZE;
402 nla_nest_end(skb, prop);
403 }
404 nla_nest_end(skb, bearer);
405
406 return 0;
407}
408
Richard Alpec3d6fb82015-05-06 13:58:54 +0200409static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
410 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100411 struct tipc_nl_compat_msg *msg)
412{
413 char *name;
414 struct nlattr *bearer;
415
416 name = (char *)TLV_DATA(msg->req);
417
418 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
419 if (!bearer)
420 return -EMSGSIZE;
421
422 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
423 return -EMSGSIZE;
424
425 nla_nest_end(skb, bearer);
426
427 return 0;
428}
429
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100430static inline u32 perc(u32 count, u32 total)
431{
432 return (count * 100 + (total / 2)) / total;
433}
434
435static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
436 struct nlattr *prop[], struct nlattr *stats[])
437{
438 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
439 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
440
441 tipc_tlv_sprintf(msg->rep,
442 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
443 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
444 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
445 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
446 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
447 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
448
449 tipc_tlv_sprintf(msg->rep,
450 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
451 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
452 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
453 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
454 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
455 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
456
457 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
458 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
459 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
460 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
461
462 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
463 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
464 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
465 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
466
467 tipc_tlv_sprintf(msg->rep,
468 " Congestion link:%u Send queue max:%u avg:%u",
469 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
470 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
471 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
472}
473
474static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
475 struct nlattr **attrs)
476{
477 char *name;
478 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
479 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
480 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800481 int err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100482
Baozeng Ding297f7d22016-05-24 22:33:24 +0800483 if (!attrs[TIPC_NLA_LINK])
484 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100485
Baozeng Ding297f7d22016-05-24 22:33:24 +0800486 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
487 NULL);
488 if (err)
489 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100490
Baozeng Ding297f7d22016-05-24 22:33:24 +0800491 if (!link[TIPC_NLA_LINK_PROP])
492 return -EINVAL;
493
494 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
495 link[TIPC_NLA_LINK_PROP], NULL);
496 if (err)
497 return err;
498
499 if (!link[TIPC_NLA_LINK_STATS])
500 return -EINVAL;
501
502 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
503 link[TIPC_NLA_LINK_STATS], NULL);
504 if (err)
505 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100506
507 name = (char *)TLV_DATA(msg->req);
508 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
509 return 0;
510
511 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
512 nla_data(link[TIPC_NLA_LINK_NAME]));
513
514 if (link[TIPC_NLA_LINK_BROADCAST]) {
515 __fill_bc_link_stat(msg, prop, stats);
516 return 0;
517 }
518
519 if (link[TIPC_NLA_LINK_ACTIVE])
520 tipc_tlv_sprintf(msg->rep, " ACTIVE");
521 else if (link[TIPC_NLA_LINK_UP])
522 tipc_tlv_sprintf(msg->rep, " STANDBY");
523 else
524 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
525
526 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
527 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
528 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
529
530 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
531 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
532 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
533
534 tipc_tlv_sprintf(msg->rep,
535 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
536 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
537 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
538 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
539 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
540 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
541 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
542
543 tipc_tlv_sprintf(msg->rep,
544 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
545 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
546 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
547 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
548 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
549 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
550 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
551
552 tipc_tlv_sprintf(msg->rep,
553 " TX profile sample:%u packets average:%u octets\n",
554 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
555 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
556 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
557
558 tipc_tlv_sprintf(msg->rep,
559 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
560 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
561 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
562 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
563 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
564 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
565 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
566 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
567 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
568
569 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
570 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
571 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
572 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
573 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
574 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
575 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
576
577 tipc_tlv_sprintf(msg->rep,
578 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
579 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
580 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
581 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
582 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
583 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
584
585 tipc_tlv_sprintf(msg->rep,
586 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
587 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
588 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
589 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
590 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
591 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
592
593 tipc_tlv_sprintf(msg->rep,
594 " Congestion link:%u Send queue max:%u avg:%u",
595 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
596 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
597 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
598
599 return 0;
600}
601
Richard Alpe357ebdb2015-02-09 09:50:07 +0100602static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
603 struct nlattr **attrs)
604{
605 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
606 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800607 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100608
Baozeng Ding297f7d22016-05-24 22:33:24 +0800609 if (!attrs[TIPC_NLA_LINK])
610 return -EINVAL;
611
612 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
613 NULL);
614 if (err)
615 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100616
617 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
618 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200619 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400620 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100621
622 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
623 &link_info, sizeof(link_info));
624}
625
Richard Alpec3d6fb82015-05-06 13:58:54 +0200626static int __tipc_add_link_prop(struct sk_buff *skb,
627 struct tipc_nl_compat_msg *msg,
628 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100629{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200630 switch (msg->cmd) {
631 case TIPC_CMD_SET_LINK_PRI:
632 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
633 case TIPC_CMD_SET_LINK_TOL:
634 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
635 case TIPC_CMD_SET_LINK_WINDOW:
636 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
637 }
638
639 return -EINVAL;
640}
641
642static int tipc_nl_compat_media_set(struct sk_buff *skb,
643 struct tipc_nl_compat_msg *msg)
644{
Richard Alpe37e2d482015-02-09 09:50:08 +0100645 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200646 struct nlattr *media;
647 struct tipc_link_config *lc;
648
649 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
650
651 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
652 if (!media)
653 return -EMSGSIZE;
654
655 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
656 return -EMSGSIZE;
657
658 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
659 if (!prop)
660 return -EMSGSIZE;
661
662 __tipc_add_link_prop(skb, msg, lc);
663 nla_nest_end(skb, prop);
664 nla_nest_end(skb, media);
665
666 return 0;
667}
668
669static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
670 struct tipc_nl_compat_msg *msg)
671{
672 struct nlattr *prop;
673 struct nlattr *bearer;
674 struct tipc_link_config *lc;
675
676 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
677
678 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
679 if (!bearer)
680 return -EMSGSIZE;
681
682 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
683 return -EMSGSIZE;
684
685 prop = nla_nest_start(skb, TIPC_NLA_BEARER_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, bearer);
692
693 return 0;
694}
695
696static int __tipc_nl_compat_link_set(struct sk_buff *skb,
697 struct tipc_nl_compat_msg *msg)
698{
699 struct nlattr *prop;
700 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100701 struct tipc_link_config *lc;
702
703 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
704
705 link = nla_nest_start(skb, TIPC_NLA_LINK);
706 if (!link)
707 return -EMSGSIZE;
708
709 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
710 return -EMSGSIZE;
711
712 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
713 if (!prop)
714 return -EMSGSIZE;
715
Richard Alpec3d6fb82015-05-06 13:58:54 +0200716 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100717 nla_nest_end(skb, prop);
718 nla_nest_end(skb, link);
719
720 return 0;
721}
722
Richard Alpec3d6fb82015-05-06 13:58:54 +0200723static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
724 struct sk_buff *skb,
725 struct tipc_nl_compat_msg *msg)
726{
727 struct tipc_link_config *lc;
728 struct tipc_bearer *bearer;
729 struct tipc_media *media;
730
731 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
732
733 media = tipc_media_find(lc->name);
734 if (media) {
735 cmd->doit = &tipc_nl_media_set;
736 return tipc_nl_compat_media_set(skb, msg);
737 }
738
739 bearer = tipc_bearer_find(msg->net, lc->name);
740 if (bearer) {
741 cmd->doit = &tipc_nl_bearer_set;
742 return tipc_nl_compat_bearer_set(skb, msg);
743 }
744
745 return __tipc_nl_compat_link_set(skb, msg);
746}
747
748static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
749 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100750 struct tipc_nl_compat_msg *msg)
751{
752 char *name;
753 struct nlattr *link;
Ying Xue4cd995a2019-01-14 17:22:25 +0800754 int len;
Richard Alpe18178772015-02-09 09:50:09 +0100755
756 name = (char *)TLV_DATA(msg->req);
757
758 link = nla_nest_start(skb, TIPC_NLA_LINK);
759 if (!link)
760 return -EMSGSIZE;
761
Ying Xue4cd995a2019-01-14 17:22:25 +0800762 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
763 if (!string_is_valid(name, len))
764 return -EINVAL;
765
Richard Alpe18178772015-02-09 09:50:09 +0100766 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
767 return -EMSGSIZE;
768
769 nla_nest_end(skb, link);
770
771 return 0;
772}
773
Richard Alpe44a8ae92015-02-09 09:50:10 +0100774static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
775{
776 int i;
777 u32 depth;
778 struct tipc_name_table_query *ntq;
779 static const char * const header[] = {
780 "Type ",
781 "Lower Upper ",
782 "Port Identity ",
783 "Publication Scope"
784 };
785
786 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
787
788 depth = ntohl(ntq->depth);
789
790 if (depth > 4)
791 depth = 4;
792 for (i = 0; i < depth; i++)
793 tipc_tlv_sprintf(msg->rep, header[i]);
794 tipc_tlv_sprintf(msg->rep, "\n");
795
796 return 0;
797}
798
799static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
800 struct nlattr **attrs)
801{
802 char port_str[27];
803 struct tipc_name_table_query *ntq;
804 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
805 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
806 u32 node, depth, type, lowbound, upbound;
807 static const char * const scope_str[] = {"", " zone", " cluster",
808 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800809 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100810
Baozeng Ding297f7d22016-05-24 22:33:24 +0800811 if (!attrs[TIPC_NLA_NAME_TABLE])
812 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100813
Baozeng Ding297f7d22016-05-24 22:33:24 +0800814 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
815 attrs[TIPC_NLA_NAME_TABLE], NULL);
816 if (err)
817 return err;
818
819 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
820 return -EINVAL;
821
822 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
823 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
824 if (err)
825 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100826
827 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
828
829 depth = ntohl(ntq->depth);
830 type = ntohl(ntq->type);
831 lowbound = ntohl(ntq->lowbound);
832 upbound = ntohl(ntq->upbound);
833
834 if (!(depth & TIPC_NTQ_ALLTYPES) &&
835 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
836 return 0;
837 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
838 return 0;
839 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
840 return 0;
841
842 tipc_tlv_sprintf(msg->rep, "%-10u ",
843 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
844
845 if (depth == 1)
846 goto out;
847
848 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
849 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
850 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
851
852 if (depth == 2)
853 goto out;
854
855 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
856 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
857 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
858 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
859
860 if (depth == 3)
861 goto out;
862
863 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200864 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100865 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
866out:
867 tipc_tlv_sprintf(msg->rep, "\n");
868
869 return 0;
870}
871
Richard Alpe487d2a32015-02-09 09:50:11 +0100872static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
873 struct nlattr **attrs)
874{
875 u32 type, lower, upper;
876 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800877 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100878
Baozeng Ding297f7d22016-05-24 22:33:24 +0800879 if (!attrs[TIPC_NLA_PUBL])
880 return -EINVAL;
881
882 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
883 NULL);
884 if (err)
885 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100886
887 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
888 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
889 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
890
891 if (lower == upper)
892 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
893 else
894 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
895
896 return 0;
897}
898
899static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
900{
901 int err;
902 void *hdr;
903 struct nlattr *nest;
904 struct sk_buff *args;
905 struct tipc_nl_compat_cmd_dump dump;
906
907 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
908 if (!args)
909 return -ENOMEM;
910
911 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
912 TIPC_NL_PUBL_GET);
913
914 nest = nla_nest_start(args, TIPC_NLA_SOCK);
915 if (!nest) {
916 kfree_skb(args);
917 return -EMSGSIZE;
918 }
919
920 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
921 kfree_skb(args);
922 return -EMSGSIZE;
923 }
924
925 nla_nest_end(args, nest);
926 genlmsg_end(args, hdr);
927
928 dump.dumpit = tipc_nl_publ_dump;
929 dump.format = __tipc_nl_compat_publ_dump;
930
931 err = __tipc_nl_compat_dumpit(&dump, msg, args);
932
933 kfree_skb(args);
934
935 return err;
936}
937
938static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
939 struct nlattr **attrs)
940{
941 int err;
942 u32 sock_ref;
943 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
944
Baozeng Ding297f7d22016-05-24 22:33:24 +0800945 if (!attrs[TIPC_NLA_SOCK])
946 return -EINVAL;
947
948 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
949 NULL);
950 if (err)
951 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100952
953 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
954 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
955
956 if (sock[TIPC_NLA_SOCK_CON]) {
957 u32 node;
958 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
959
960 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
961 NULL);
962
963 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
964 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
965 tipc_zone(node),
966 tipc_cluster(node),
967 tipc_node(node),
968 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
969
970 if (con[TIPC_NLA_CON_FLAG])
971 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
972 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
973 nla_get_u32(con[TIPC_NLA_CON_INST]));
974 else
975 tipc_tlv_sprintf(msg->rep, "\n");
976 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
977 tipc_tlv_sprintf(msg->rep, " bound to");
978
979 err = tipc_nl_compat_publ_dump(msg, sock_ref);
980 if (err)
981 return err;
982 }
983 tipc_tlv_sprintf(msg->rep, "\n");
984
985 return 0;
986}
987
Richard Alpe5bfc3352015-02-09 09:50:12 +0100988static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
989 struct nlattr **attrs)
990{
991 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800992 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +0100993
Baozeng Ding297f7d22016-05-24 22:33:24 +0800994 if (!attrs[TIPC_NLA_MEDIA])
995 return -EINVAL;
996
997 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
998 NULL);
999 if (err)
1000 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001001
1002 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1003 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1004 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1005}
1006
Richard Alpe4b28cb52015-02-09 09:50:13 +01001007static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1008 struct nlattr **attrs)
1009{
1010 struct tipc_node_info node_info;
1011 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001012 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001013
Baozeng Ding297f7d22016-05-24 22:33:24 +08001014 if (!attrs[TIPC_NLA_NODE])
1015 return -EINVAL;
1016
1017 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1018 NULL);
1019 if (err)
1020 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001021
1022 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1023 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1024
1025 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1026 sizeof(node_info));
1027}
1028
Richard Alpec3d6fb82015-05-06 13:58:54 +02001029static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1030 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001031 struct tipc_nl_compat_msg *msg)
1032{
1033 u32 val;
1034 struct nlattr *net;
1035
1036 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1037
1038 net = nla_nest_start(skb, TIPC_NLA_NET);
1039 if (!net)
1040 return -EMSGSIZE;
1041
Richard Alpe964f9502015-02-09 09:50:15 +01001042 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1043 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1044 return -EMSGSIZE;
1045 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1046 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1047 return -EMSGSIZE;
1048 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001049 nla_nest_end(skb, net);
1050
1051 return 0;
1052}
1053
Richard Alpe3c261812015-02-09 09:50:16 +01001054static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1055 struct nlattr **attrs)
1056{
1057 __be32 id;
1058 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001059 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001060
Baozeng Ding297f7d22016-05-24 22:33:24 +08001061 if (!attrs[TIPC_NLA_NET])
1062 return -EINVAL;
1063
1064 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1065 NULL);
1066 if (err)
1067 return err;
1068
Richard Alpe3c261812015-02-09 09:50:16 +01001069 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1070
1071 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1072}
1073
Richard Alpe5a81a632015-02-09 09:50:17 +01001074static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1075{
1076 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1077 if (!msg->rep)
1078 return -ENOMEM;
1079
1080 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1081 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1082
1083 return 0;
1084}
1085
Richard Alped0796d12015-02-09 09:50:04 +01001086static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1087{
1088 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001089 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001090
1091 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001092 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001093
1094 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001095 case TIPC_CMD_NOOP:
1096 msg->rep = tipc_tlv_alloc(0);
1097 if (!msg->rep)
1098 return -ENOMEM;
1099 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001100 case TIPC_CMD_GET_BEARER_NAMES:
1101 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1102 dump.dumpit = tipc_nl_bearer_dump;
1103 dump.format = tipc_nl_compat_bearer_dump;
1104 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001105 case TIPC_CMD_ENABLE_BEARER:
1106 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1107 doit.doit = tipc_nl_bearer_enable;
1108 doit.transcode = tipc_nl_compat_bearer_enable;
1109 return tipc_nl_compat_doit(&doit, msg);
1110 case TIPC_CMD_DISABLE_BEARER:
1111 msg->req_type = TIPC_TLV_BEARER_NAME;
1112 doit.doit = tipc_nl_bearer_disable;
1113 doit.transcode = tipc_nl_compat_bearer_disable;
1114 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001115 case TIPC_CMD_SHOW_LINK_STATS:
1116 msg->req_type = TIPC_TLV_LINK_NAME;
1117 msg->rep_size = ULTRA_STRING_MAX_LEN;
1118 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001119 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001120 dump.format = tipc_nl_compat_link_stat_dump;
1121 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001122 case TIPC_CMD_GET_LINKS:
1123 msg->req_type = TIPC_TLV_NET_ADDR;
1124 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001125 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001126 dump.format = tipc_nl_compat_link_dump;
1127 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001128 case TIPC_CMD_SET_LINK_TOL:
1129 case TIPC_CMD_SET_LINK_PRI:
1130 case TIPC_CMD_SET_LINK_WINDOW:
1131 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001132 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001133 doit.transcode = tipc_nl_compat_link_set;
1134 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001135 case TIPC_CMD_RESET_LINK_STATS:
1136 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001137 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001138 doit.transcode = tipc_nl_compat_link_reset_stats;
1139 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001140 case TIPC_CMD_SHOW_NAME_TABLE:
1141 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1142 msg->rep_size = ULTRA_STRING_MAX_LEN;
1143 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1144 dump.header = tipc_nl_compat_name_table_dump_header;
1145 dump.dumpit = tipc_nl_name_table_dump;
1146 dump.format = tipc_nl_compat_name_table_dump;
1147 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001148 case TIPC_CMD_SHOW_PORTS:
1149 msg->rep_size = ULTRA_STRING_MAX_LEN;
1150 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1151 dump.dumpit = tipc_nl_sk_dump;
1152 dump.format = tipc_nl_compat_sk_dump;
1153 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001154 case TIPC_CMD_GET_MEDIA_NAMES:
1155 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1156 dump.dumpit = tipc_nl_media_dump;
1157 dump.format = tipc_nl_compat_media_dump;
1158 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001159 case TIPC_CMD_GET_NODES:
1160 msg->rep_size = ULTRA_STRING_MAX_LEN;
1161 dump.dumpit = tipc_nl_node_dump;
1162 dump.format = tipc_nl_compat_node_dump;
1163 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001164 case TIPC_CMD_SET_NODE_ADDR:
1165 msg->req_type = TIPC_TLV_NET_ADDR;
1166 doit.doit = tipc_nl_net_set;
1167 doit.transcode = tipc_nl_compat_net_set;
1168 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001169 case TIPC_CMD_SET_NETID:
1170 msg->req_type = TIPC_TLV_UNSIGNED;
1171 doit.doit = tipc_nl_net_set;
1172 doit.transcode = tipc_nl_compat_net_set;
1173 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001174 case TIPC_CMD_GET_NETID:
1175 msg->rep_size = sizeof(u32);
1176 dump.dumpit = tipc_nl_net_dump;
1177 dump.format = tipc_nl_compat_net_dump;
1178 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001179 case TIPC_CMD_SHOW_STATS:
1180 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001181 }
1182
1183 return -EOPNOTSUPP;
1184}
1185
1186static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1187{
1188 int err;
1189 int len;
1190 struct tipc_nl_compat_msg msg;
1191 struct nlmsghdr *req_nlh;
1192 struct nlmsghdr *rep_nlh;
1193 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001194
1195 memset(&msg, 0, sizeof(msg));
1196
1197 req_nlh = (struct nlmsghdr *)skb->data;
1198 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1199 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001200 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001201 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001202
1203 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1204 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1205 err = -EACCES;
1206 goto send;
1207 }
1208
1209 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Richard Alpe8f8ff9132015-08-17 14:15:10 +02001210 if (len && !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001211 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1212 err = -EOPNOTSUPP;
1213 goto send;
1214 }
1215
1216 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001217 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001218 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1219 else if (err == -EINVAL)
1220 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1221send:
1222 if (!msg.rep)
1223 return err;
1224
1225 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1226 skb_push(msg.rep, len);
1227 rep_nlh = nlmsg_hdr(msg.rep);
1228 memcpy(rep_nlh, info->nlhdr, len);
1229 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001230 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001231
1232 return err;
1233}
1234
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001235static struct genl_family tipc_genl_compat_family = {
1236 .id = GENL_ID_GENERATE,
1237 .name = TIPC_GENL_NAME,
1238 .version = TIPC_GENL_VERSION,
1239 .hdrsize = TIPC_GENL_HDRLEN,
1240 .maxattr = 0,
1241 .netnsok = true,
1242};
1243
1244static struct genl_ops tipc_genl_compat_ops[] = {
1245 {
1246 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001247 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001248 },
1249};
1250
1251int tipc_netlink_compat_start(void)
1252{
1253 int res;
1254
1255 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1256 tipc_genl_compat_ops);
1257 if (res) {
1258 pr_err("Failed to register legacy compat interface\n");
1259 return res;
1260 }
1261
1262 return 0;
1263}
1264
1265void tipc_netlink_compat_stop(void)
1266{
1267 genl_unregister_family(&tipc_genl_compat_family);
1268}