blob: d405deb16a3c62e8af8bdac21af595b958a3569e [file] [log] [blame]
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001/*
2 * Copyright (c) 2014, Ericsson AB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the names of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "core.h"
Richard Alped0796d12015-02-09 09:50:04 +010035#include "bearer.h"
Richard Alpef2b3b2d2015-02-09 09:50:06 +010036#include "link.h"
Richard Alpe44a8ae92015-02-09 09:50:10 +010037#include "name_table.h"
Richard Alpe487d2a32015-02-09 09:50:11 +010038#include "socket.h"
Richard Alpe4b28cb52015-02-09 09:50:13 +010039#include "node.h"
Richard Alpe964f9502015-02-09 09:50:15 +010040#include "net.h"
Richard Alpebfb3e5d2015-02-09 09:50:03 +010041#include <net/genetlink.h>
42#include <linux/tipc_config.h>
43
Richard Alped0796d12015-02-09 09:50:04 +010044/* The legacy API had an artificial message length limit called
45 * ULTRA_STRING_MAX_LEN.
46 */
47#define ULTRA_STRING_MAX_LEN 32768
48
49#define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
50
51#define REPLY_TRUNCATED "<truncated>\n"
52
53struct tipc_nl_compat_msg {
54 u16 cmd;
Richard Alpef2b3b2d2015-02-09 09:50:06 +010055 int rep_type;
Richard Alped0796d12015-02-09 09:50:04 +010056 int rep_size;
Richard Alpe9ab15462015-02-09 09:50:05 +010057 int req_type;
Richard Alpec3d6fb82015-05-06 13:58:54 +020058 struct net *net;
Richard Alped0796d12015-02-09 09:50:04 +010059 struct sk_buff *rep;
60 struct tlv_desc *req;
61 struct sock *dst_sk;
62};
63
64struct tipc_nl_compat_cmd_dump {
Richard Alpe44a8ae92015-02-09 09:50:10 +010065 int (*header)(struct tipc_nl_compat_msg *);
Richard Alped0796d12015-02-09 09:50:04 +010066 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
67 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
68};
69
Richard Alpe9ab15462015-02-09 09:50:05 +010070struct tipc_nl_compat_cmd_doit {
71 int (*doit)(struct sk_buff *skb, struct genl_info *info);
Richard Alpec3d6fb82015-05-06 13:58:54 +020072 int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd,
73 struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
Richard Alpe9ab15462015-02-09 09:50:05 +010074};
75
Richard Alped0796d12015-02-09 09:50:04 +010076static int tipc_skb_tailroom(struct sk_buff *skb)
77{
78 int tailroom;
79 int limit;
80
81 tailroom = skb_tailroom(skb);
82 limit = TIPC_SKB_MAX - skb->len;
83
84 if (tailroom < limit)
85 return tailroom;
86
87 return limit;
88}
89
Ying Xue4cd995a2019-01-14 17:22:25 +080090static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv)
91{
92 return TLV_GET_LEN(tlv) - TLV_SPACE(0);
93}
94
Richard Alped0796d12015-02-09 09:50:04 +010095static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
96{
97 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
98
99 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
100 return -EMSGSIZE;
101
102 skb_put(skb, TLV_SPACE(len));
103 tlv->tlv_type = htons(type);
104 tlv->tlv_len = htons(TLV_LENGTH(len));
105 if (len && data)
106 memcpy(TLV_DATA(tlv), data, len);
107
108 return 0;
109}
110
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100111static void tipc_tlv_init(struct sk_buff *skb, u16 type)
112{
113 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
114
115 TLV_SET_LEN(tlv, 0);
116 TLV_SET_TYPE(tlv, type);
117 skb_put(skb, sizeof(struct tlv_desc));
118}
119
120static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
121{
122 int n;
123 u16 len;
124 u32 rem;
125 char *buf;
126 struct tlv_desc *tlv;
127 va_list args;
128
129 rem = tipc_skb_tailroom(skb);
130
131 tlv = (struct tlv_desc *)skb->data;
132 len = TLV_GET_LEN(tlv);
133 buf = TLV_DATA(tlv) + len;
134
135 va_start(args, fmt);
136 n = vscnprintf(buf, rem, fmt, args);
137 va_end(args);
138
139 TLV_SET_LEN(tlv, n + len);
140 skb_put(skb, n);
141
142 return n;
143}
144
Richard Alped0796d12015-02-09 09:50:04 +0100145static struct sk_buff *tipc_tlv_alloc(int size)
146{
147 int hdr_len;
148 struct sk_buff *buf;
149
150 size = TLV_SPACE(size);
151 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
152
153 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
154 if (!buf)
155 return NULL;
156
157 skb_reserve(buf, hdr_len);
158
159 return buf;
160}
161
162static struct sk_buff *tipc_get_err_tlv(char *str)
163{
164 int str_len = strlen(str) + 1;
165 struct sk_buff *buf;
166
167 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
168 if (buf)
169 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
170
171 return buf;
172}
173
Ying Xue4cd995a2019-01-14 17:22:25 +0800174static inline bool string_is_valid(char *s, int len)
175{
176 return memchr(s, '\0', len) ? true : false;
177}
178
Richard Alped0796d12015-02-09 09:50:04 +0100179static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
180 struct tipc_nl_compat_msg *msg,
181 struct sk_buff *arg)
182{
183 int len = 0;
184 int err;
185 struct sk_buff *buf;
186 struct nlmsghdr *nlmsg;
187 struct netlink_callback cb;
188
189 memset(&cb, 0, sizeof(cb));
190 cb.nlh = (struct nlmsghdr *)arg->data;
191 cb.skb = arg;
192
193 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
194 if (!buf)
195 return -ENOMEM;
196
197 buf->sk = msg->dst_sk;
198
199 do {
200 int rem;
201
202 len = (*cmd->dumpit)(buf, &cb);
203
204 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
205 struct nlattr **attrs;
206
207 err = tipc_nlmsg_parse(nlmsg, &attrs);
208 if (err)
209 goto err_out;
210
211 err = (*cmd->format)(msg, attrs);
212 if (err)
213 goto err_out;
214
215 if (tipc_skb_tailroom(msg->rep) <= 1) {
216 err = -EMSGSIZE;
217 goto err_out;
218 }
219 }
220
221 skb_reset_tail_pointer(buf);
222 buf->len = 0;
223
224 } while (len);
225
226 err = 0;
227
228err_out:
229 kfree_skb(buf);
230
231 if (err == -EMSGSIZE) {
232 /* The legacy API only considered messages filling
233 * "ULTRA_STRING_MAX_LEN" to be truncated.
234 */
235 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
236 char *tail = skb_tail_pointer(msg->rep);
237
238 if (*tail != '\0')
239 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
240 REPLY_TRUNCATED);
241 }
242
243 return 0;
244 }
245
246 return err;
247}
248
249static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
250 struct tipc_nl_compat_msg *msg)
251{
252 int err;
253 struct sk_buff *arg;
254
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100255 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
256 return -EINVAL;
257
Richard Alped0796d12015-02-09 09:50:04 +0100258 msg->rep = tipc_tlv_alloc(msg->rep_size);
259 if (!msg->rep)
260 return -ENOMEM;
261
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100262 if (msg->rep_type)
263 tipc_tlv_init(msg->rep, msg->rep_type);
264
Richard Alpe44a8ae92015-02-09 09:50:10 +0100265 if (cmd->header)
266 (*cmd->header)(msg);
267
Richard Alped0796d12015-02-09 09:50:04 +0100268 arg = nlmsg_new(0, GFP_KERNEL);
269 if (!arg) {
270 kfree_skb(msg->rep);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700271 msg->rep = NULL;
Richard Alped0796d12015-02-09 09:50:04 +0100272 return -ENOMEM;
273 }
274
275 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700276 if (err) {
Richard Alped0796d12015-02-09 09:50:04 +0100277 kfree_skb(msg->rep);
Eric Dumazet7ad5fb92017-08-16 09:41:54 -0700278 msg->rep = NULL;
279 }
Richard Alped0796d12015-02-09 09:50:04 +0100280 kfree_skb(arg);
281
282 return err;
283}
284
Richard Alpe9ab15462015-02-09 09:50:05 +0100285static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
286 struct tipc_nl_compat_msg *msg)
287{
288 int err;
289 struct sk_buff *doit_buf;
290 struct sk_buff *trans_buf;
291 struct nlattr **attrbuf;
292 struct genl_info info;
293
294 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
295 if (!trans_buf)
296 return -ENOMEM;
297
Richard Alpec3d6fb82015-05-06 13:58:54 +0200298 err = (*cmd->transcode)(cmd, trans_buf, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100299 if (err)
300 goto trans_out;
301
302 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
303 sizeof(struct nlattr *), GFP_KERNEL);
304 if (!attrbuf) {
305 err = -ENOMEM;
306 goto trans_out;
307 }
308
309 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
310 (const struct nlattr *)trans_buf->data,
311 trans_buf->len, NULL);
312 if (err)
313 goto parse_out;
314
315 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
316 if (!doit_buf) {
317 err = -ENOMEM;
318 goto parse_out;
319 }
320
321 doit_buf->sk = msg->dst_sk;
322
323 memset(&info, 0, sizeof(info));
324 info.attrs = attrbuf;
325
326 err = (*cmd->doit)(doit_buf, &info);
327
328 kfree_skb(doit_buf);
329parse_out:
330 kfree(attrbuf);
331trans_out:
332 kfree_skb(trans_buf);
333
334 return err;
335}
336
337static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
338 struct tipc_nl_compat_msg *msg)
339{
340 int err;
341
342 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
343 return -EINVAL;
344
345 err = __tipc_nl_compat_doit(cmd, msg);
346 if (err)
347 return err;
348
349 /* The legacy API considered an empty message a success message */
350 msg->rep = tipc_tlv_alloc(0);
351 if (!msg->rep)
352 return -ENOMEM;
353
354 return 0;
355}
356
Richard Alped0796d12015-02-09 09:50:04 +0100357static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
358 struct nlattr **attrs)
359{
360 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800361 int err;
Richard Alped0796d12015-02-09 09:50:04 +0100362
Baozeng Ding297f7d22016-05-24 22:33:24 +0800363 if (!attrs[TIPC_NLA_BEARER])
364 return -EINVAL;
365
366 err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX,
367 attrs[TIPC_NLA_BEARER], NULL);
368 if (err)
369 return err;
Richard Alped0796d12015-02-09 09:50:04 +0100370
371 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
372 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
373 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
374}
375
Richard Alpec3d6fb82015-05-06 13:58:54 +0200376static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
377 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100378 struct tipc_nl_compat_msg *msg)
379{
380 struct nlattr *prop;
381 struct nlattr *bearer;
382 struct tipc_bearer_config *b;
Ying Xue7d0cb252019-01-14 17:22:26 +0800383 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100384
385 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
386
387 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
388 if (!bearer)
389 return -EMSGSIZE;
390
Ying Xue7d0cb252019-01-14 17:22:26 +0800391 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
392 if (!string_is_valid(b->name, len))
393 return -EINVAL;
394
Richard Alpe9ab15462015-02-09 09:50:05 +0100395 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
396 return -EMSGSIZE;
397
398 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
399 return -EMSGSIZE;
400
401 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
402 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
403 if (!prop)
404 return -EMSGSIZE;
405 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
406 return -EMSGSIZE;
407 nla_nest_end(skb, prop);
408 }
409 nla_nest_end(skb, bearer);
410
411 return 0;
412}
413
Richard Alpec3d6fb82015-05-06 13:58:54 +0200414static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
415 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100416 struct tipc_nl_compat_msg *msg)
417{
418 char *name;
419 struct nlattr *bearer;
Ying Xue7d0cb252019-01-14 17:22:26 +0800420 int len;
Richard Alpe9ab15462015-02-09 09:50:05 +0100421
422 name = (char *)TLV_DATA(msg->req);
423
424 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
425 if (!bearer)
426 return -EMSGSIZE;
427
Ying Xue7d0cb252019-01-14 17:22:26 +0800428 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME);
429 if (!string_is_valid(name, len))
430 return -EINVAL;
431
Richard Alpe9ab15462015-02-09 09:50:05 +0100432 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
433 return -EMSGSIZE;
434
435 nla_nest_end(skb, bearer);
436
437 return 0;
438}
439
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100440static inline u32 perc(u32 count, u32 total)
441{
442 return (count * 100 + (total / 2)) / total;
443}
444
445static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
446 struct nlattr *prop[], struct nlattr *stats[])
447{
448 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
449 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
450
451 tipc_tlv_sprintf(msg->rep,
452 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
453 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
454 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
455 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
456 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
457 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
458
459 tipc_tlv_sprintf(msg->rep,
460 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
461 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
462 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
463 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
464 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
465 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
466
467 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
468 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
469 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
470 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
471
472 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
473 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
474 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
475 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
476
477 tipc_tlv_sprintf(msg->rep,
478 " Congestion link:%u Send queue max:%u avg:%u",
479 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
480 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
481 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
482}
483
484static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
485 struct nlattr **attrs)
486{
487 char *name;
488 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
489 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
490 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800491 int err;
Ying Xue7d0cb252019-01-14 17:22:26 +0800492 int len;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100493
Baozeng Ding297f7d22016-05-24 22:33:24 +0800494 if (!attrs[TIPC_NLA_LINK])
495 return -EINVAL;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100496
Baozeng Ding297f7d22016-05-24 22:33:24 +0800497 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
498 NULL);
499 if (err)
500 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100501
Baozeng Ding297f7d22016-05-24 22:33:24 +0800502 if (!link[TIPC_NLA_LINK_PROP])
503 return -EINVAL;
504
505 err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX,
506 link[TIPC_NLA_LINK_PROP], NULL);
507 if (err)
508 return err;
509
510 if (!link[TIPC_NLA_LINK_STATS])
511 return -EINVAL;
512
513 err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX,
514 link[TIPC_NLA_LINK_STATS], NULL);
515 if (err)
516 return err;
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100517
518 name = (char *)TLV_DATA(msg->req);
Ying Xue7d0cb252019-01-14 17:22:26 +0800519
520 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
521 if (!string_is_valid(name, len))
522 return -EINVAL;
523
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100524 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
525 return 0;
526
527 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
528 nla_data(link[TIPC_NLA_LINK_NAME]));
529
530 if (link[TIPC_NLA_LINK_BROADCAST]) {
531 __fill_bc_link_stat(msg, prop, stats);
532 return 0;
533 }
534
535 if (link[TIPC_NLA_LINK_ACTIVE])
536 tipc_tlv_sprintf(msg->rep, " ACTIVE");
537 else if (link[TIPC_NLA_LINK_UP])
538 tipc_tlv_sprintf(msg->rep, " STANDBY");
539 else
540 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
541
542 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
543 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
544 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
545
546 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
547 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
548 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
549
550 tipc_tlv_sprintf(msg->rep,
551 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
552 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
553 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
554 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
555 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
556 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
557 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
558
559 tipc_tlv_sprintf(msg->rep,
560 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
561 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
562 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
563 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
564 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
565 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
566 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
567
568 tipc_tlv_sprintf(msg->rep,
569 " TX profile sample:%u packets average:%u octets\n",
570 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
571 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
572 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
573
574 tipc_tlv_sprintf(msg->rep,
575 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
576 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
577 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
578 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
579 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
580 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
581 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
582 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
583 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
584
585 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
586 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
587 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
588 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
589 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
590 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
591 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
592
593 tipc_tlv_sprintf(msg->rep,
594 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
595 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
596 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
597 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
598 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
599 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
600
601 tipc_tlv_sprintf(msg->rep,
602 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
603 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
604 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
605 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
606 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
607 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
608
609 tipc_tlv_sprintf(msg->rep,
610 " Congestion link:%u Send queue max:%u avg:%u",
611 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
612 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
613 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
614
615 return 0;
616}
617
Richard Alpe357ebdb2015-02-09 09:50:07 +0100618static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
619 struct nlattr **attrs)
620{
621 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
622 struct tipc_link_info link_info;
Baozeng Ding297f7d22016-05-24 22:33:24 +0800623 int err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100624
Baozeng Ding297f7d22016-05-24 22:33:24 +0800625 if (!attrs[TIPC_NLA_LINK])
626 return -EINVAL;
627
628 err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK],
629 NULL);
630 if (err)
631 return err;
Richard Alpe357ebdb2015-02-09 09:50:07 +0100632
633 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
634 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe55e77a32016-07-01 11:11:21 +0200635 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5d2be142016-06-02 04:04:56 -0400636 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100637
638 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
639 &link_info, sizeof(link_info));
640}
641
Richard Alpec3d6fb82015-05-06 13:58:54 +0200642static int __tipc_add_link_prop(struct sk_buff *skb,
643 struct tipc_nl_compat_msg *msg,
644 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100645{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200646 switch (msg->cmd) {
647 case TIPC_CMD_SET_LINK_PRI:
648 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
649 case TIPC_CMD_SET_LINK_TOL:
650 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
651 case TIPC_CMD_SET_LINK_WINDOW:
652 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
653 }
654
655 return -EINVAL;
656}
657
658static int tipc_nl_compat_media_set(struct sk_buff *skb,
659 struct tipc_nl_compat_msg *msg)
660{
Richard Alpe37e2d482015-02-09 09:50:08 +0100661 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200662 struct nlattr *media;
663 struct tipc_link_config *lc;
Ying Xue7d0cb252019-01-14 17:22:26 +0800664 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200665
666 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
667
668 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
669 if (!media)
670 return -EMSGSIZE;
671
Ying Xue7d0cb252019-01-14 17:22:26 +0800672 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
673 if (!string_is_valid(lc->name, len))
674 return -EINVAL;
675
Richard Alpec3d6fb82015-05-06 13:58:54 +0200676 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
677 return -EMSGSIZE;
678
679 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
680 if (!prop)
681 return -EMSGSIZE;
682
683 __tipc_add_link_prop(skb, msg, lc);
684 nla_nest_end(skb, prop);
685 nla_nest_end(skb, media);
686
687 return 0;
688}
689
690static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
691 struct tipc_nl_compat_msg *msg)
692{
693 struct nlattr *prop;
694 struct nlattr *bearer;
695 struct tipc_link_config *lc;
Ying Xue7d0cb252019-01-14 17:22:26 +0800696 int len;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200697
698 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
699
700 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
701 if (!bearer)
702 return -EMSGSIZE;
703
Ying Xue7d0cb252019-01-14 17:22:26 +0800704 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME);
705 if (!string_is_valid(lc->name, len))
706 return -EINVAL;
707
Richard Alpec3d6fb82015-05-06 13:58:54 +0200708 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
709 return -EMSGSIZE;
710
711 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
712 if (!prop)
713 return -EMSGSIZE;
714
715 __tipc_add_link_prop(skb, msg, lc);
716 nla_nest_end(skb, prop);
717 nla_nest_end(skb, bearer);
718
719 return 0;
720}
721
722static int __tipc_nl_compat_link_set(struct sk_buff *skb,
723 struct tipc_nl_compat_msg *msg)
724{
725 struct nlattr *prop;
726 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100727 struct tipc_link_config *lc;
728
729 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
730
731 link = nla_nest_start(skb, TIPC_NLA_LINK);
732 if (!link)
733 return -EMSGSIZE;
734
735 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
736 return -EMSGSIZE;
737
738 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
739 if (!prop)
740 return -EMSGSIZE;
741
Richard Alpec3d6fb82015-05-06 13:58:54 +0200742 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100743 nla_nest_end(skb, prop);
744 nla_nest_end(skb, link);
745
746 return 0;
747}
748
Richard Alpec3d6fb82015-05-06 13:58:54 +0200749static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
750 struct sk_buff *skb,
751 struct tipc_nl_compat_msg *msg)
752{
753 struct tipc_link_config *lc;
754 struct tipc_bearer *bearer;
755 struct tipc_media *media;
756
757 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
758
759 media = tipc_media_find(lc->name);
760 if (media) {
761 cmd->doit = &tipc_nl_media_set;
762 return tipc_nl_compat_media_set(skb, msg);
763 }
764
765 bearer = tipc_bearer_find(msg->net, lc->name);
766 if (bearer) {
767 cmd->doit = &tipc_nl_bearer_set;
768 return tipc_nl_compat_bearer_set(skb, msg);
769 }
770
771 return __tipc_nl_compat_link_set(skb, msg);
772}
773
774static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
775 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100776 struct tipc_nl_compat_msg *msg)
777{
778 char *name;
779 struct nlattr *link;
Ying Xue4cd995a2019-01-14 17:22:25 +0800780 int len;
Richard Alpe18178772015-02-09 09:50:09 +0100781
782 name = (char *)TLV_DATA(msg->req);
783
784 link = nla_nest_start(skb, TIPC_NLA_LINK);
785 if (!link)
786 return -EMSGSIZE;
787
Ying Xue4cd995a2019-01-14 17:22:25 +0800788 len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME);
789 if (!string_is_valid(name, len))
790 return -EINVAL;
791
Richard Alpe18178772015-02-09 09:50:09 +0100792 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
793 return -EMSGSIZE;
794
795 nla_nest_end(skb, link);
796
797 return 0;
798}
799
Richard Alpe44a8ae92015-02-09 09:50:10 +0100800static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
801{
802 int i;
803 u32 depth;
804 struct tipc_name_table_query *ntq;
805 static const char * const header[] = {
806 "Type ",
807 "Lower Upper ",
808 "Port Identity ",
809 "Publication Scope"
810 };
811
812 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
813
814 depth = ntohl(ntq->depth);
815
816 if (depth > 4)
817 depth = 4;
818 for (i = 0; i < depth; i++)
819 tipc_tlv_sprintf(msg->rep, header[i]);
820 tipc_tlv_sprintf(msg->rep, "\n");
821
822 return 0;
823}
824
825static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
826 struct nlattr **attrs)
827{
828 char port_str[27];
829 struct tipc_name_table_query *ntq;
830 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
831 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
832 u32 node, depth, type, lowbound, upbound;
833 static const char * const scope_str[] = {"", " zone", " cluster",
834 " node"};
Baozeng Ding297f7d22016-05-24 22:33:24 +0800835 int err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100836
Baozeng Ding297f7d22016-05-24 22:33:24 +0800837 if (!attrs[TIPC_NLA_NAME_TABLE])
838 return -EINVAL;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100839
Baozeng Ding297f7d22016-05-24 22:33:24 +0800840 err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
841 attrs[TIPC_NLA_NAME_TABLE], NULL);
842 if (err)
843 return err;
844
845 if (!nt[TIPC_NLA_NAME_TABLE_PUBL])
846 return -EINVAL;
847
848 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX,
849 nt[TIPC_NLA_NAME_TABLE_PUBL], NULL);
850 if (err)
851 return err;
Richard Alpe44a8ae92015-02-09 09:50:10 +0100852
853 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
854
855 depth = ntohl(ntq->depth);
856 type = ntohl(ntq->type);
857 lowbound = ntohl(ntq->lowbound);
858 upbound = ntohl(ntq->upbound);
859
860 if (!(depth & TIPC_NTQ_ALLTYPES) &&
861 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
862 return 0;
863 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
864 return 0;
865 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
866 return 0;
867
868 tipc_tlv_sprintf(msg->rep, "%-10u ",
869 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
870
871 if (depth == 1)
872 goto out;
873
874 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
875 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
876 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
877
878 if (depth == 2)
879 goto out;
880
881 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
882 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
883 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
884 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
885
886 if (depth == 3)
887 goto out;
888
889 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe03aaaa92016-05-17 16:57:37 +0200890 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100891 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
892out:
893 tipc_tlv_sprintf(msg->rep, "\n");
894
895 return 0;
896}
897
Richard Alpe487d2a32015-02-09 09:50:11 +0100898static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
899 struct nlattr **attrs)
900{
901 u32 type, lower, upper;
902 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +0800903 int err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100904
Baozeng Ding297f7d22016-05-24 22:33:24 +0800905 if (!attrs[TIPC_NLA_PUBL])
906 return -EINVAL;
907
908 err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL],
909 NULL);
910 if (err)
911 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100912
913 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
914 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
915 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
916
917 if (lower == upper)
918 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
919 else
920 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
921
922 return 0;
923}
924
925static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
926{
927 int err;
928 void *hdr;
929 struct nlattr *nest;
930 struct sk_buff *args;
931 struct tipc_nl_compat_cmd_dump dump;
932
933 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
934 if (!args)
935 return -ENOMEM;
936
937 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
938 TIPC_NL_PUBL_GET);
939
940 nest = nla_nest_start(args, TIPC_NLA_SOCK);
941 if (!nest) {
942 kfree_skb(args);
943 return -EMSGSIZE;
944 }
945
946 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
947 kfree_skb(args);
948 return -EMSGSIZE;
949 }
950
951 nla_nest_end(args, nest);
952 genlmsg_end(args, hdr);
953
954 dump.dumpit = tipc_nl_publ_dump;
955 dump.format = __tipc_nl_compat_publ_dump;
956
957 err = __tipc_nl_compat_dumpit(&dump, msg, args);
958
959 kfree_skb(args);
960
961 return err;
962}
963
964static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
965 struct nlattr **attrs)
966{
967 int err;
968 u32 sock_ref;
969 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
970
Baozeng Ding297f7d22016-05-24 22:33:24 +0800971 if (!attrs[TIPC_NLA_SOCK])
972 return -EINVAL;
973
974 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK],
975 NULL);
976 if (err)
977 return err;
Richard Alpe487d2a32015-02-09 09:50:11 +0100978
979 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
980 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
981
982 if (sock[TIPC_NLA_SOCK_CON]) {
983 u32 node;
984 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
985
986 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
987 NULL);
988
989 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
990 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
991 tipc_zone(node),
992 tipc_cluster(node),
993 tipc_node(node),
994 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
995
996 if (con[TIPC_NLA_CON_FLAG])
997 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
998 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
999 nla_get_u32(con[TIPC_NLA_CON_INST]));
1000 else
1001 tipc_tlv_sprintf(msg->rep, "\n");
1002 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
1003 tipc_tlv_sprintf(msg->rep, " bound to");
1004
1005 err = tipc_nl_compat_publ_dump(msg, sock_ref);
1006 if (err)
1007 return err;
1008 }
1009 tipc_tlv_sprintf(msg->rep, "\n");
1010
1011 return 0;
1012}
1013
Richard Alpe5bfc3352015-02-09 09:50:12 +01001014static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
1015 struct nlattr **attrs)
1016{
1017 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001018 int err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001019
Baozeng Ding297f7d22016-05-24 22:33:24 +08001020 if (!attrs[TIPC_NLA_MEDIA])
1021 return -EINVAL;
1022
1023 err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
1024 NULL);
1025 if (err)
1026 return err;
Richard Alpe5bfc3352015-02-09 09:50:12 +01001027
1028 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
1029 nla_data(media[TIPC_NLA_MEDIA_NAME]),
1030 nla_len(media[TIPC_NLA_MEDIA_NAME]));
1031}
1032
Richard Alpe4b28cb52015-02-09 09:50:13 +01001033static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
1034 struct nlattr **attrs)
1035{
1036 struct tipc_node_info node_info;
1037 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001038 int err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001039
Baozeng Ding297f7d22016-05-24 22:33:24 +08001040 if (!attrs[TIPC_NLA_NODE])
1041 return -EINVAL;
1042
1043 err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE],
1044 NULL);
1045 if (err)
1046 return err;
Richard Alpe4b28cb52015-02-09 09:50:13 +01001047
1048 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
1049 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
1050
1051 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
1052 sizeof(node_info));
1053}
1054
Richard Alpec3d6fb82015-05-06 13:58:54 +02001055static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
1056 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +01001057 struct tipc_nl_compat_msg *msg)
1058{
1059 u32 val;
1060 struct nlattr *net;
1061
1062 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
1063
1064 net = nla_nest_start(skb, TIPC_NLA_NET);
1065 if (!net)
1066 return -EMSGSIZE;
1067
Richard Alpe964f9502015-02-09 09:50:15 +01001068 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
1069 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
1070 return -EMSGSIZE;
1071 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
1072 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
1073 return -EMSGSIZE;
1074 }
Richard Alped7cc75d2015-02-09 09:50:14 +01001075 nla_nest_end(skb, net);
1076
1077 return 0;
1078}
1079
Richard Alpe3c261812015-02-09 09:50:16 +01001080static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
1081 struct nlattr **attrs)
1082{
1083 __be32 id;
1084 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
Baozeng Ding297f7d22016-05-24 22:33:24 +08001085 int err;
Richard Alpe3c261812015-02-09 09:50:16 +01001086
Baozeng Ding297f7d22016-05-24 22:33:24 +08001087 if (!attrs[TIPC_NLA_NET])
1088 return -EINVAL;
1089
1090 err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET],
1091 NULL);
1092 if (err)
1093 return err;
1094
Richard Alpe3c261812015-02-09 09:50:16 +01001095 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
1096
1097 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
1098}
1099
Richard Alpe5a81a632015-02-09 09:50:17 +01001100static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
1101{
1102 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
1103 if (!msg->rep)
1104 return -ENOMEM;
1105
1106 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
1107 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
1108
1109 return 0;
1110}
1111
Richard Alped0796d12015-02-09 09:50:04 +01001112static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
1113{
1114 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +01001115 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +01001116
1117 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001118 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001119
1120 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001121 case TIPC_CMD_NOOP:
1122 msg->rep = tipc_tlv_alloc(0);
1123 if (!msg->rep)
1124 return -ENOMEM;
1125 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001126 case TIPC_CMD_GET_BEARER_NAMES:
1127 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1128 dump.dumpit = tipc_nl_bearer_dump;
1129 dump.format = tipc_nl_compat_bearer_dump;
1130 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001131 case TIPC_CMD_ENABLE_BEARER:
1132 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1133 doit.doit = tipc_nl_bearer_enable;
1134 doit.transcode = tipc_nl_compat_bearer_enable;
1135 return tipc_nl_compat_doit(&doit, msg);
1136 case TIPC_CMD_DISABLE_BEARER:
1137 msg->req_type = TIPC_TLV_BEARER_NAME;
1138 doit.doit = tipc_nl_bearer_disable;
1139 doit.transcode = tipc_nl_compat_bearer_disable;
1140 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001141 case TIPC_CMD_SHOW_LINK_STATS:
1142 msg->req_type = TIPC_TLV_LINK_NAME;
1143 msg->rep_size = ULTRA_STRING_MAX_LEN;
1144 msg->rep_type = TIPC_TLV_ULTRA_STRING;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001145 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001146 dump.format = tipc_nl_compat_link_stat_dump;
1147 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001148 case TIPC_CMD_GET_LINKS:
1149 msg->req_type = TIPC_TLV_NET_ADDR;
1150 msg->rep_size = ULTRA_STRING_MAX_LEN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001151 dump.dumpit = tipc_nl_node_dump_link;
Richard Alpe357ebdb2015-02-09 09:50:07 +01001152 dump.format = tipc_nl_compat_link_dump;
1153 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001154 case TIPC_CMD_SET_LINK_TOL:
1155 case TIPC_CMD_SET_LINK_PRI:
1156 case TIPC_CMD_SET_LINK_WINDOW:
1157 msg->req_type = TIPC_TLV_LINK_CONFIG;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001158 doit.doit = tipc_nl_node_set_link;
Richard Alpe37e2d482015-02-09 09:50:08 +01001159 doit.transcode = tipc_nl_compat_link_set;
1160 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001161 case TIPC_CMD_RESET_LINK_STATS:
1162 msg->req_type = TIPC_TLV_LINK_NAME;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001163 doit.doit = tipc_nl_node_reset_link_stats;
Richard Alpe18178772015-02-09 09:50:09 +01001164 doit.transcode = tipc_nl_compat_link_reset_stats;
1165 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001166 case TIPC_CMD_SHOW_NAME_TABLE:
1167 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1168 msg->rep_size = ULTRA_STRING_MAX_LEN;
1169 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1170 dump.header = tipc_nl_compat_name_table_dump_header;
1171 dump.dumpit = tipc_nl_name_table_dump;
1172 dump.format = tipc_nl_compat_name_table_dump;
1173 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001174 case TIPC_CMD_SHOW_PORTS:
1175 msg->rep_size = ULTRA_STRING_MAX_LEN;
1176 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1177 dump.dumpit = tipc_nl_sk_dump;
1178 dump.format = tipc_nl_compat_sk_dump;
1179 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001180 case TIPC_CMD_GET_MEDIA_NAMES:
1181 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1182 dump.dumpit = tipc_nl_media_dump;
1183 dump.format = tipc_nl_compat_media_dump;
1184 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001185 case TIPC_CMD_GET_NODES:
1186 msg->rep_size = ULTRA_STRING_MAX_LEN;
1187 dump.dumpit = tipc_nl_node_dump;
1188 dump.format = tipc_nl_compat_node_dump;
1189 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001190 case TIPC_CMD_SET_NODE_ADDR:
1191 msg->req_type = TIPC_TLV_NET_ADDR;
1192 doit.doit = tipc_nl_net_set;
1193 doit.transcode = tipc_nl_compat_net_set;
1194 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001195 case TIPC_CMD_SET_NETID:
1196 msg->req_type = TIPC_TLV_UNSIGNED;
1197 doit.doit = tipc_nl_net_set;
1198 doit.transcode = tipc_nl_compat_net_set;
1199 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001200 case TIPC_CMD_GET_NETID:
1201 msg->rep_size = sizeof(u32);
1202 dump.dumpit = tipc_nl_net_dump;
1203 dump.format = tipc_nl_compat_net_dump;
1204 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001205 case TIPC_CMD_SHOW_STATS:
1206 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001207 }
1208
1209 return -EOPNOTSUPP;
1210}
1211
1212static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1213{
1214 int err;
1215 int len;
1216 struct tipc_nl_compat_msg msg;
1217 struct nlmsghdr *req_nlh;
1218 struct nlmsghdr *rep_nlh;
1219 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001220
1221 memset(&msg, 0, sizeof(msg));
1222
1223 req_nlh = (struct nlmsghdr *)skb->data;
1224 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1225 msg.cmd = req_userhdr->cmd;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001226 msg.net = genl_info_net(info);
Florian Westphal619b1742016-02-24 17:20:17 +01001227 msg.dst_sk = skb->sk;
Richard Alped0796d12015-02-09 09:50:04 +01001228
1229 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1230 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1231 err = -EACCES;
1232 goto send;
1233 }
1234
1235 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Richard Alpe8f8ff9132015-08-17 14:15:10 +02001236 if (len && !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001237 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1238 err = -EOPNOTSUPP;
1239 goto send;
1240 }
1241
1242 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001243 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001244 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1245 else if (err == -EINVAL)
1246 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1247send:
1248 if (!msg.rep)
1249 return err;
1250
1251 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1252 skb_push(msg.rep, len);
1253 rep_nlh = nlmsg_hdr(msg.rep);
1254 memcpy(rep_nlh, info->nlhdr, len);
1255 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001256 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001257
1258 return err;
1259}
1260
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001261static struct genl_family tipc_genl_compat_family = {
1262 .id = GENL_ID_GENERATE,
1263 .name = TIPC_GENL_NAME,
1264 .version = TIPC_GENL_VERSION,
1265 .hdrsize = TIPC_GENL_HDRLEN,
1266 .maxattr = 0,
1267 .netnsok = true,
1268};
1269
1270static struct genl_ops tipc_genl_compat_ops[] = {
1271 {
1272 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001273 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001274 },
1275};
1276
1277int tipc_netlink_compat_start(void)
1278{
1279 int res;
1280
1281 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1282 tipc_genl_compat_ops);
1283 if (res) {
1284 pr_err("Failed to register legacy compat interface\n");
1285 return res;
1286 }
1287
1288 return 0;
1289}
1290
1291void tipc_netlink_compat_stop(void)
1292{
1293 genl_unregister_family(&tipc_genl_compat_family);
1294}